/* ==========================================================================
   Design tokens
   ========================================================================== */
:root {
  /* type */
  --font-display: 'Baloo 2', sans-serif;
  --font-body: 'Inter', sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
  --text-h1: 1.375rem;      /* 22 */
  --text-h2: 1.0625rem;     /* 17 */
  --text-body: 0.9375rem;   /* 15 */
  --text-small: 0.8125rem;  /* 13 */
  --text-tiny: 0.6875rem;   /* 11 */

  /* color — cream/green farm palette, tonal HSL variants */
  --bg-base: hsl(45 45% 95%);
  --bg-elevated: hsl(48 60% 99%);
  --bg-sunk: hsl(45 30% 89%);
  --border-subtle: hsl(90 18% 82%);
  --text-primary: hsl(140 28% 12%);
  --text-mute: hsl(140 12% 42%);

  --primary: hsl(142 42% 32%);
  --primary-hover: hsl(142 46% 26%);
  --primary-soft: hsl(142 42% 32% / 0.12);

  --accent: hsl(36 92% 54%);
  --accent-hover: hsl(36 92% 46%);
  --accent-soft: hsl(36 92% 54% / 0.16);

  --water: hsl(202 80% 52%);
  --water-soft: hsl(202 80% 52% / 0.14);

  --danger: hsl(4 72% 52%);
  --danger-soft: hsl(4 72% 52% / 0.14);

  /* space: 8pt scale */
  --space-1: 8px;
  --space-2: 16px;
  --space-3: 24px;
  --space-4: 32px;
  --space-6: 48px;
  --space-8: 64px;

  --radius-s: 10px;
  --radius-m: 16px;
  --radius-l: 24px;
  --radius-pill: 999px;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ==========================================================================
   Reset
   ========================================================================== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: var(--text-body);
  overscroll-behavior-y: none;
  height: 100%;
}
button { font-family: inherit; border: none; background: none; cursor: pointer; color: inherit; }
h1, h2, h3 { margin: 0; font-family: var(--font-display); }
p { margin: 0; }

#app {
  min-height: 100vh;
  padding-bottom: calc(var(--space-6) + var(--safe-bottom));
  display: flex;
  flex-direction: column;
}

/* ==========================================================================
   Header
   ========================================================================== */
.topbar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: calc(var(--space-2) + var(--safe-top)) var(--space-2) var(--space-1);
}

.topbar__user { position: relative; flex-shrink: 0; }
.avatar {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-pill);
  background: var(--primary-soft);
  border: 2px solid var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.level-badge {
  position: absolute;
  bottom: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: var(--radius-pill);
  background: var(--accent);
  color: var(--bg-elevated);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--bg-base);
}

.topbar__stats {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.stat { display: flex; align-items: baseline; gap: 6px; }
.stat__icon { font-size: 14px; }
.stat--balance .stat__value { font-family: var(--font-display); font-size: var(--text-h2); font-weight: 600; }
.stat--xp .stat__value { font-family: var(--font-mono); font-size: var(--text-small); color: var(--text-mute); }
.stat--xp .stat__label { font-size: var(--text-tiny); color: var(--text-mute); }

.can-widget {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: var(--radius-pill);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
}
.can-widget__icon { font-size: 16px; }
.can-widget__bars { display: flex; flex-direction: column; gap: 3px; width: 52px; }
.mini-bar { height: 4px; border-radius: var(--radius-pill); background: var(--bg-sunk); overflow: hidden; }
.mini-bar__fill { height: 100%; border-radius: var(--radius-pill); background: var(--water); transition: width .3s ease; }
.mini-bar__fill--durability { background: var(--primary); }
.can-widget__charges { font-family: var(--font-mono); font-size: 10px; color: var(--text-mute); text-align: center; }

/* ==========================================================================
   Field switcher
   ========================================================================== */
.field-switcher {
  display: flex;
  gap: var(--space-1);
  padding: var(--space-1) 0;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-behavior: smooth;
  touch-action: pan-x;
  -webkit-overflow-scrolling: touch;
}
.field-switcher::-webkit-scrollbar { display: none; }

.field-switcher-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 var(--space-2);
}

.field-nav {
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  border-radius: var(--radius-pill);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-mute);
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.field-nav:active { background: var(--bg-sunk); }

.field-pill {
  flex-shrink: 0;
  padding: 8px 16px;
  border-radius: var(--radius-pill);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  font-size: var(--text-small);
  font-weight: 600;
  color: var(--text-mute);
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.field-pill.is-active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--bg-elevated);
}
.field-pill--add {
  background: var(--accent-soft);
  border-color: transparent;
  color: var(--accent-hover);
}

.field-meta {
  padding: 0 var(--space-2) var(--space-1);
  font-size: var(--text-tiny);
  color: var(--text-mute);
  font-family: var(--font-mono);
}
.field-meta__dot { margin: 0 4px; }

/* ==========================================================================
   Beds grid
   ========================================================================== */
.beds-viewport { flex: 1; padding: var(--space-1) var(--space-2) var(--space-4); }
.beds-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  gap: var(--space-1);
}

.bed {
  position: relative;
  aspect-ratio: 1;
  border-radius: var(--radius-m);
  background: var(--bg-elevated);
  border: 1.5px dashed var(--border-subtle);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  transition: transform .12s ease, border-color .15s ease, background .15s ease;
}
.bed:active { transform: scale(0.94); }

.bed__icon { font-size: 26px; line-height: 1; }
.bed__timer { font-family: var(--font-mono); font-size: 10px; color: var(--text-mute); }
.bed__ring {
  position: absolute;
  inset: 0;
  border-radius: var(--radius-m);
  padding: 3px;
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  pointer-events: none;
}
.bed__droplet {
  position: absolute;
  top: 4px;
  right: 4px;
  font-size: 11px;
}
.bed__label {
  position: absolute;
  bottom: 4px;
  font-size: 9px;
  font-weight: 700;
  color: var(--bg-elevated);
  background: var(--primary);
  padding: 1px 6px;
  border-radius: var(--radius-pill);
  letter-spacing: .02em;
}

.bed--empty { border-style: dashed; }
.bed--empty .bed__icon { color: var(--border-subtle); font-size: 22px; }

.bed--growing { border-style: solid; border-color: var(--border-subtle); }
.bed--growing.is-watered { background: var(--water-soft); }

.bed--ready {
  border-style: solid;
  border-color: var(--accent);
  background: var(--accent-soft);
  animation: bed-pulse 1.4s ease-in-out infinite;
}
.bed--ready .bed__label { background: var(--accent); }

.bed--locked {
  border-style: solid;
  border-color: transparent;
  background: repeating-linear-gradient(135deg, var(--bg-sunk), var(--bg-sunk) 6px, var(--bg-base) 6px, var(--bg-base) 12px);
  opacity: .55;
}
.bed--locked .bed__icon { font-size: 18px; }

@keyframes bed-pulse {
  0%, 100% { box-shadow: 0 0 0 0 var(--accent-soft); }
  50% { box-shadow: 0 0 0 6px transparent; }
}

/* ==========================================================================
   Bottom sheet
   ========================================================================== */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: hsl(140 30% 8% / 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease;
  z-index: 40;
}
.sheet-backdrop.is-open { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-elevated);
  border-radius: var(--radius-l) var(--radius-l) 0 0;
  padding: var(--space-1) var(--space-2) calc(var(--space-3) + var(--safe-bottom));
  transform: translateY(105%);
  transition: transform .25s cubic-bezier(.32, .72, 0, 1);
  z-index: 41;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
}
.sheet.is-open { transform: translateY(0); }

.sheet__handle {
  width: 36px;
  height: 4px;
  border-radius: var(--radius-pill);
  background: var(--border-subtle);
  margin: 6px auto var(--space-2);
}
.sheet__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-2);
}
.sheet__title { font-size: var(--text-h1); }
.sheet__close {
  width: 30px; height: 30px;
  border-radius: var(--radius-pill);
  background: var(--bg-sunk);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  color: var(--text-mute);
}
.sheet__body { overflow-y: auto; display: flex; flex-direction: column; gap: var(--space-1); }

.seed-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-m);
  background: var(--bg-sunk);
  text-align: left;
}
.seed-row:active { transform: scale(0.98); }
.seed-row__icon { font-size: 26px; }
.seed-row__info { flex: 1; }
.seed-row__name { font-weight: 600; font-size: var(--text-body); }
.seed-row__meta { font-size: var(--text-tiny); color: var(--text-mute); font-family: var(--font-mono); }
.seed-row__amount {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: var(--text-small);
  background: var(--primary-soft);
  color: var(--primary);
  padding: 4px 10px;
  border-radius: var(--radius-pill);
}
.seed-empty {
  text-align: center;
  padding: var(--space-4) var(--space-2);
  color: var(--text-mute);
  font-size: var(--text-small);
}

/* ==========================================================================
   Toasts
   ========================================================================== */
.toast-stack {
  position: fixed;
  top: calc(var(--space-2) + var(--safe-top));
  left: var(--space-2);
  right: var(--space-2);
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  padding: 10px 14px;
  border-radius: var(--radius-m);
  background: var(--text-primary);
  color: var(--bg-elevated);
  font-size: var(--text-small);
  font-weight: 500;
  box-shadow: 0 8px 20px hsl(140 30% 8% / 0.25);
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity .2s ease, transform .2s ease;
}
.toast.is-visible { opacity: 1; transform: translateY(0); }
.toast--success { background: var(--primary); }
.toast--error { background: var(--danger); }
.toast--info { background: var(--text-primary); }

/* ==========================================================================
   Loading / fatal
   ========================================================================== */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg-base);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  z-index: 100;
  transition: opacity .25s ease;
}
.loading-overlay.is-hidden { opacity: 0; pointer-events: none; }
.loading-sprout { font-size: 40px; animation: sprout-bounce 1s ease-in-out infinite; }
.loading-text { color: var(--text-mute); font-size: var(--text-small); }
@keyframes sprout-bounce {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-6px) scale(1.08); }
}

.fatal {
  position: fixed;
  inset: 0;
  background: var(--bg-base);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-4);
  text-align: center;
  z-index: 90;
}
.fatal[hidden] { display: none; }
.fatal__icon { font-size: 40px; }
.fatal__text { color: var(--text-mute); font-size: var(--text-body); }

.btn {
  padding: 10px 20px;
  border-radius: var(--radius-pill);
  font-weight: 600;
  font-size: var(--text-body);
}
.btn--primary { background: var(--primary); color: var(--bg-elevated); }
.btn--primary:active { background: var(--primary-hover); }
