/*
 * Pantry — design system.
 *
 * Scene: a parent at the open fridge at 12:40 on a Tuesday, phone in one hand,
 * hungry, deciding in under a minute — and again in a bright supermarket aisle.
 * That is bright ambient light and one-handed use, which forces a light,
 * high-contrast surface and thumb-reachable navigation.
 *
 * Colour strategy: restrained. Pure white surface, one indigo primary for
 * actions and selection, and a functional urgency ramp for food that needs
 * eating. Warmth comes from the food names on screen, never from the chrome —
 * the cream/sand "farmhouse kitchen" palette is the reflex this deliberately
 * avoids.
 */

/* ─── Tokens ──────────────────────────────────────────────────────────────── */

:root {
  /* Surface */
  --bg:            oklch(1 0 0);
  --surface:       oklch(0.983 0.003 262);
  --surface-sunk:  oklch(0.962 0.005 262);
  --border:        oklch(0.906 0.007 262);
  --border-strong: oklch(0.842 0.010 262);

  /* Ink — muted still clears 4.5:1 on white, which light-grey body text does not */
  --ink:           oklch(0.245 0.021 262);
  --ink-muted:     oklch(0.487 0.019 262);
  --ink-faint:     oklch(0.606 0.016 262);

  /* Brand */
  --primary:       oklch(0.423 0.148 262);
  --primary-hover: oklch(0.372 0.148 262);
  --primary-soft:  oklch(0.951 0.024 262);
  --primary-ink:   oklch(1 0 0);
  --focus:         oklch(0.556 0.170 262);

  /* Functional urgency ramp. Not decoration — this is the app's core signal. */
  --fresh:         oklch(0.520 0.118 156);
  --fresh-soft:    oklch(0.955 0.030 156);
  --soon:          oklch(0.545 0.118 74);
  --soon-soft:     oklch(0.960 0.045 84);
  --urgent:        oklch(0.520 0.170 32);
  --urgent-soft:   oklch(0.958 0.040 40);
  --expired:       oklch(0.478 0.183 26);
  --expired-soft:  oklch(0.950 0.045 26);

  /* Stock levels */
  --level-plenty:  var(--fresh);
  --level-low:     var(--soon);
  --level-out:     oklch(0.560 0.014 262);
  --level-unknown: oklch(0.480 0.100 300);

  /* Type — fixed rem scale at ratio ~1.18. Product UI, not fluid display type. */
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
          'Helvetica Neue', Arial, sans-serif;
  --font-num: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, monospace;
  --text-xs:   0.75rem;
  --text-sm:   0.8125rem;
  --text-base: 0.9375rem;
  --text-md:   1.0625rem;
  --text-lg:   1.25rem;
  --text-xl:   1.5rem;

  /* Space — 4px base */
  --s1: 0.25rem;  --s2: 0.5rem;   --s3: 0.75rem;  --s4: 1rem;
  --s5: 1.25rem;  --s6: 1.5rem;   --s8: 2rem;     --s10: 2.5rem;
  --s12: 3rem;    --s16: 4rem;

  --radius:    10px;
  --radius-lg: 14px;
  --radius-pill: 999px;

  --shadow-sm: 0 1px 2px oklch(0.245 0.021 262 / 0.06);
  --shadow-md: 0 4px 16px oklch(0.245 0.021 262 / 0.09);
  --shadow-lg: 0 12px 32px oklch(0.245 0.021 262 / 0.14);

  /* Motion — product pace: state feedback, never choreography */
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --fast: 140ms;
  --base: 200ms;

  /* Semantic stacking order — never arbitrary numbers */
  --z-sticky: 10;
  --z-tabbar: 20;
  --z-sheet-backdrop: 30;
  --z-sheet: 31;
  --z-toast: 40;

  --tap: 44px;
  --topbar-h: 56px;
  --tabbar-h: 60px;
}

/* ─── Reset ───────────────────────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; }

/* A class-level `display` beats the UA rule for [hidden], so state it here. */
[hidden] { display: none !important; }

html {
  -webkit-text-size-adjust: 100%;
  height: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  /* The app owns scrolling inside .screen; the body never scrolls sideways. */
  overflow-x: hidden;
  overscroll-behavior-y: none;
}

h1, h2, h3, p, figure { margin: 0; }
h1, h2, h3 { text-wrap: balance; line-height: 1.25; font-weight: 600; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { display: block; }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

::placeholder { color: var(--ink-faint); opacity: 1; }

/* ─── App frame ───────────────────────────────────────────────────────────── */

.app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100dvh;
  max-width: 40rem;
  margin-inline: auto;
  background: var(--bg);
  /* Grid children default to min-content width; without this a long word or a
     wide table makes the whole page scroll sideways. */
  min-width: 0;
}
.app > * { min-width: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
  min-height: var(--topbar-h);
  padding: var(--s2) var(--s4);
  padding-top: max(var(--s2), env(safe-area-inset-top));
  background: oklch(1 0 0 / 0.88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar__lead { display: flex; align-items: center; gap: var(--s2); min-width: 0; overflow: hidden; }
.topbar__title {
  font-size: var(--text-lg);
  letter-spacing: -0.015em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* Without min-width:0 a nowrap title sets the flex item's floor and pushes
     the whole page wider than the viewport instead of truncating. */
  min-width: 0;
}
.topbar__actions { display: flex; align-items: center; gap: var(--s2); flex-shrink: 0; }

.screen {
  padding: var(--s4) var(--s4) var(--s16);
  min-width: 0;
}
.screen:focus { outline: none; }

/* ─── Bottom navigation — thumb reach is the whole point ──────────────────── */

.tabbar {
  position: sticky;
  bottom: 0;
  z-index: var(--z-tabbar);
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: var(--s1);
  padding: var(--s1) var(--s2);
  padding-bottom: max(var(--s1), env(safe-area-inset-bottom));
  background: oklch(1 0 0 / 0.92);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: var(--tabbar-h);
  padding: var(--s1);
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: var(--ink-faint);
  font-size: var(--text-xs);
  font-weight: 500;
  cursor: pointer;
  transition: color var(--fast) var(--ease), background var(--fast) var(--ease);
}
.tab svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.tab:hover { color: var(--ink-muted); }
.tab[aria-current='page'] { color: var(--primary); background: var(--primary-soft); }

.tab__badge {
  position: absolute;
  transform: translate(14px, -14px);
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: var(--radius-pill);
  background: var(--urgent);
  color: oklch(1 0 0);
  font-size: 10px;
  font-weight: 700;
  line-height: 17px;
  text-align: center;
}

/* ─── Buttons ─────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: var(--tap);
  padding: 0 var(--s5);
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: var(--primary);
  color: var(--primary-ink);
  font-size: var(--text-base);
  font-weight: 600;
  cursor: pointer;
  transition: background var(--fast) var(--ease), transform var(--fast) var(--ease),
              opacity var(--fast) var(--ease);
}
.btn:hover:not(:disabled) { background: var(--primary-hover); }
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.btn--secondary {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--border-strong);
}
.btn--secondary:hover:not(:disabled) { background: var(--surface-sunk); }

.btn--ghost { background: none; color: var(--primary); }
.btn--ghost:hover:not(:disabled) { background: var(--primary-soft); }

.btn--danger { background: var(--expired); }
.btn--danger:hover:not(:disabled) { background: oklch(0.418 0.183 26); }

.btn--block { width: 100%; }
.btn--sm { min-height: 36px; padding: 0 var(--s3); font-size: var(--text-sm); }

.btn.is-loading { pointer-events: none; opacity: 0.7; }
.btn.is-loading::after {
  content: '';
  width: 14px; height: 14px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 620ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.iconbtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tap);
  height: var(--tap);
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: var(--ink-muted);
  cursor: pointer;
  transition: background var(--fast) var(--ease), color var(--fast) var(--ease);
}
.iconbtn:hover { background: var(--surface-sunk); color: var(--ink); }
.iconbtn svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

/* ─── Form controls ───────────────────────────────────────────────────────── */

.field { display: flex; flex-direction: column; gap: var(--s2); }
.field__label { font-size: var(--text-sm); font-weight: 600; color: var(--ink-muted); }
.field__hint { font-size: var(--text-sm); color: var(--ink-muted); }
.field__error { font-size: var(--text-sm); color: var(--expired); font-weight: 500; }

.input, .select, .textarea {
  width: 100%;
  min-height: var(--tap);
  padding: var(--s3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg);
  /* 16px minimum stops iOS Safari zooming the viewport on focus. */
  font-size: 1rem;
  transition: border-color var(--fast) var(--ease), box-shadow var(--fast) var(--ease);
}
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--focus);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.textarea { min-height: 7rem; resize: vertical; line-height: 1.55; }
.select { appearance: none; padding-right: var(--s8);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23777f8f' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right var(--s3) center; background-size: 18px;
}

/* ─── Segmented control — the one-tap level switch ────────────────────────── */

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 2px;
  padding: 2px;
  background: var(--surface-sunk);
  border-radius: var(--radius);
}
.segmented__option {
  min-height: 38px;
  padding: 0 var(--s2);
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--ink-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background var(--fast) var(--ease), color var(--fast) var(--ease);
}
.segmented__option:hover { color: var(--ink); }
.segmented__option[aria-pressed='true'] {
  background: var(--bg);
  color: var(--ink);
  box-shadow: var(--shadow-sm);
}
.segmented__option[aria-pressed='true'][data-level='plenty'] { color: var(--level-plenty); }
.segmented__option[aria-pressed='true'][data-level='low']    { color: var(--level-low); }
.segmented__option[aria-pressed='true'][data-level='out']    { color: var(--level-out); }

/* ─── Rows — the primary list affordance (cards are the lazy answer) ──────── */

.rows { display: flex; flex-direction: column; }

.row {
  display: flex;
  align-items: center;
  gap: var(--s3);
  min-height: 56px;
  padding: var(--s3) 0;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.row:last-child { border-bottom: 0; }

.row__body { flex: 1; min-width: 0; }
.row__title {
  font-weight: 550;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row__meta {
  display: flex;
  align-items: center;
  gap: var(--s2);
  margin-top: 2px;
  font-size: var(--text-sm);
  color: var(--ink-muted);
}
.row__aside { flex-shrink: 0; }

button.row {
  width: 100%;
  border-left: 0; border-right: 0; border-top: 0;
  background: none;
  cursor: pointer;
  transition: background var(--fast) var(--ease);
}
button.row:hover { background: var(--surface); }

/* Expiry state. Marked by a leading dot and a tinted label, never a side stripe. */
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: var(--ink-faint); }
.dot--plenty  { background: var(--level-plenty); }
.dot--low     { background: var(--level-low); }
.dot--out     { background: var(--level-out); }
.dot--unknown { background: var(--level-unknown); }
.dot--fresh   { background: var(--fresh); }
.dot--soon    { background: var(--soon); }
.dot--urgent  { background: var(--urgent); }
.dot--expired { background: var(--expired); }

.is-expiring .row__title { color: var(--ink); }

/* ─── Chips and tags ──────────────────────────────────────────────────────── */

.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--s1);
  padding: 3px var(--s2);
  border-radius: var(--radius-pill);
  background: var(--surface-sunk);
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  white-space: nowrap;
}
.chip--soon    { background: var(--soon-soft);    color: var(--soon); }
.chip--urgent  { background: var(--urgent-soft);  color: var(--urgent); }
.chip--expired { background: var(--expired-soft); color: var(--expired); }
.chip--fresh   { background: var(--fresh-soft);   color: var(--fresh); }
.chip--brand   { background: var(--primary-soft); color: var(--primary); }

.chip--toggle { cursor: pointer; border: 1px solid var(--border-strong); background: var(--bg);
  min-height: 36px; padding: 0 var(--s3); font-size: var(--text-sm); }
.chip--toggle[aria-pressed='true'] {
  background: var(--primary); border-color: var(--primary); color: var(--primary-ink);
}

/* ─── Section headings ────────────────────────────────────────────────────── */

.section { margin-bottom: var(--s8); }
.section__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s3);
  margin-bottom: var(--s2);
}
.section__title { font-size: var(--text-md); letter-spacing: -0.01em; }
.section__count { font-size: var(--text-sm); color: var(--ink-faint); font-variant-numeric: tabular-nums; }

/* ─── Empty states — teach the interface, don't just say "nothing here" ───── */

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--s3);
  padding: var(--s10) var(--s4);
  text-align: center;
}
.empty__icon {
  display: grid; place-items: center;
  width: 52px; height: 52px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
}
.empty__icon svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.empty__title { font-size: var(--text-md); }
.empty__body { max-width: 34ch; color: var(--ink-muted); text-wrap: pretty; }

/* ─── Skeletons — not spinners in the middle of content ───────────────────── */

.skeleton {
  border-radius: 6px;
  background: linear-gradient(90deg, var(--surface-sunk) 25%, var(--surface) 50%, var(--surface-sunk) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s linear infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }
.skeleton-row { display: flex; flex-direction: column; gap: var(--s2); padding: var(--s3) 0;
  border-bottom: 1px solid var(--border); }

/* ─── Sheet — used only where inline genuinely does not fit ───────────────── */

.sheet-backdrop {
  position: fixed; inset: 0;
  z-index: var(--z-sheet-backdrop);
  background: oklch(0.245 0.021 262 / 0.4);
  animation: fade var(--base) var(--ease);
}
.sheet {
  position: fixed;
  left: 50%; bottom: 0;
  transform: translateX(-50%);
  z-index: var(--z-sheet);
  width: min(40rem, 100%);
  max-height: 88dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--s5) var(--s4) calc(var(--s6) + env(safe-area-inset-bottom));
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  background: var(--bg);
  box-shadow: var(--shadow-lg);
  animation: rise var(--base) var(--ease);
}
.sheet__grip { width: 36px; height: 4px; margin: 0 auto var(--s4);
  border-radius: var(--radius-pill); background: var(--border-strong); }
.sheet__title { font-size: var(--text-lg); margin-bottom: var(--s4); }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translate(-50%, 12px); opacity: 0; } }

/* ─── Toast ───────────────────────────────────────────────────────────────── */

.toaster {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--s3));
  transform: translateX(-50%);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--s2);
  width: min(36rem, calc(100vw - var(--s8)));
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: var(--s3) var(--s4);
  border-radius: var(--radius);
  background: var(--ink);
  color: oklch(1 0 0);
  font-size: var(--text-sm);
  box-shadow: var(--shadow-md);
  animation: rise var(--base) var(--ease);
  pointer-events: auto;
}
.toast--error { background: var(--expired); }
.toast__action { margin-left: auto; border: 0; background: none; color: inherit;
  font-weight: 700; text-decoration: underline; cursor: pointer; padding: var(--s1); }

/* ─── Banner — degraded mode and other standing notices ──────────────────── */

.banner {
  display: flex;
  gap: var(--s3);
  padding: var(--s3) var(--s4);
  margin-bottom: var(--s5);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: var(--text-sm);
  color: var(--ink-muted);
}
.banner--warn { background: var(--soon-soft); border-color: var(--soon); color: var(--soon); }

/* ─── Utilities ───────────────────────────────────────────────────────────── */

.stack   { display: flex; flex-direction: column; gap: var(--s4); }
.stack-2 { display: flex; flex-direction: column; gap: var(--s2); }
.cluster { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.muted   { color: var(--ink-muted); }
.faint   { color: var(--ink-faint); }
.num     { font-variant-numeric: tabular-nums; }
.prose   { max-width: 68ch; text-wrap: pretty; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ─── Reduced motion ──────────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .sheet, .toast { animation: fade 0.01ms; }
  .skeleton { animation: none; background: var(--surface-sunk); }
}
