/* ============================================================================
   Food Inventory — design system
   Tokens first, then components. Nothing below should hard-code a colour, a
   radius or a spacing value; if a component needs one that does not exist,
   add a token rather than a magic number.
   Built mobile-first: the base rules are the phone, and the only media query
   that widens anything is at 640px.
   ========================================================================== */

/* --- tokens --------------------------------------------------------------- */
:root {
  color-scheme: light dark;

  /* Surfaces, from furthest back to closest */
  --bg:        #f6f4f0;
  --surface:   #ffffff;
  --surface-2: #f0ede6;
  --line:      #e2ded4;
  --line-firm: #cdc7b9;

  /* Ink */
  --ink:     #1b1a17;
  --ink-2:   #57534a;
  --ink-3:   #8a857a;

  /* Accents. Warn is used for age and waste, never for errors alone. */
  --accent:    #3f6f52;
  --accent-in: #ffffff;
  --accent-bg: #e8f0ea;
  --warn:      #a8480f;
  --warn-bg:   #fdf0e7;
  --danger:    #a3262a;

  /* Space — 4px scale */
  --s1: 4px;  --s2: 8px;  --s3: 12px; --s4: 16px;
  --s5: 24px; --s6: 32px; --s7: 48px;

  /* Type */
  --t-xs: 11px; --t-sm: 13px; --t-md: 15px; --t-lg: 17px;
  --t-xl: 22px; --t-2xl: 27px;
  --lh-tight: 1.2; --lh: 1.5;

  --radius-sm: 8px; --radius: 12px; --radius-lg: 18px; --radius-pill: 999px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 8px 24px -12px rgba(0,0,0,.18);
  --shadow-up: 0 -2px 12px rgba(0,0,0,.10);

  /* Every interactive element is at least this tall. Cold hands, thick
     gloves, a phone held one-handed in a cellar. */
  --touch: 44px;

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

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #141311;
    --surface:   #1d1c19;
    --surface-2: #26241f;
    --line:      #2f2d28;
    --line-firm: #43403a;
    --ink:       #ece9e3;
    --ink-2:     #a8a296;
    --ink-3:     #7d776c;
    --accent:    #79b491;
    --accent-in: #10231a;
    --accent-bg: #1e2e25;
    --warn:      #e0904f;
    --warn-bg:   #2e2319;
    --danger:    #e0716f;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.7);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* A class that sets `display` beats the UA rule for [hidden], so .gate's
   `display: grid` was keeping the sign-in screen up even after login. Any
   component with a display value needs this to stay hideable. */
[hidden] { display: none !important; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: var(--t-md)/var(--lh) -apple-system, BlinkMacSystemFont, "Segoe UI",
        Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior-y: contain;
}

/* --- layout --------------------------------------------------------------- */
.app { max-width: 720px; margin: 0 auto; padding: 0 var(--s4) var(--s7); }

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  -webkit-backdrop-filter: saturate(1.6) blur(12px);
  padding-top: calc(var(--safe-t) + var(--s3));
  margin: 0 calc(var(--s4) * -1);
  padding-inline: var(--s4);
  border-bottom: 1px solid transparent;
  transition: border-color .15s;
}
.topbar.stuck { border-bottom-color: var(--line); }

.titlerow { display: flex; align-items: baseline; gap: var(--s3); }
.title { font-size: var(--t-xl); font-weight: 700; letter-spacing: -.02em; margin: 0; }
.who   { font-size: var(--t-xs); color: var(--ink-3); margin-left: auto; }

/* --- field ---------------------------------------------------------------- */
.field {
  width: 100%; height: var(--touch);
  padding: 0 var(--s4);
  font-size: 16px;                 /* < 16px makes iOS zoom on focus */
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius);
  -webkit-appearance: none; appearance: none;
}
.field:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.field::placeholder { color: var(--ink-3); }
.field--sm { height: 36px; font-size: var(--t-sm); padding: 0 var(--s3); width: auto; }

/* --- chips (filters) ------------------------------------------------------ */
.chips {
  display: flex; gap: var(--s2);
  overflow-x: auto; scrollbar-width: none;
  padding: var(--s2) var(--s4); margin-inline: calc(var(--s4) * -1);
  scroll-snap-type: x proximity;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; scroll-snap-align: start;
  display: inline-flex; align-items: center; gap: var(--s2);
  min-height: 36px; padding: 0 var(--s3);
  font-size: var(--t-sm); font-weight: 500;
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius-pill);
  cursor: pointer; white-space: nowrap;
}
.chip__n { font-size: var(--t-xs); color: var(--ink-3); font-variant-numeric: tabular-nums; }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-in); }
.chip[aria-pressed="true"] .chip__n { color: var(--accent-in); opacity: .75; }
.chips--secondary .chip { font-size: var(--t-xs); min-height: 32px; }
.chips--secondary .chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }

/* --- card ----------------------------------------------------------------- */
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-lg); padding: var(--s4);
  margin-top: var(--s3);
}
.card__title { font-size: var(--t-lg); font-weight: 650; margin: 0; letter-spacing: -.01em; }
.card__meta  { font-size: var(--t-xs); color: var(--ink-3); margin-top: 2px; }

/* --- list row ------------------------------------------------------------- */
.row {
  display: flex; align-items: center; gap: var(--s3);
  min-height: var(--touch); padding: var(--s2) 0;
  border-top: 1px solid var(--line);
  cursor: pointer;
}
.card__list > .row:first-child { border-top: 0; }
.row:active { background: var(--surface-2); }
.row__main { flex: 1 1 auto; min-width: 0; }
.row__name { font-weight: 600; font-size: var(--t-md); }
.row__sub  { font-size: var(--t-xs); color: var(--ink-3); margin-top: 1px; }
.row__qty  {
  font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap;
  font-size: var(--t-md);
}
.row__alt  { font-size: var(--t-xs); color: var(--ink-3); white-space: nowrap; min-width: 46px; text-align: right; }
.row__chev { color: var(--ink-3); flex: 0 0 auto; }

/* --- vintage sub-list ----------------------------------------------------- */
.vintages { margin-top: var(--s1); display: grid; gap: 2px; }
.vintage {
  display: flex; gap: var(--s2); align-items: baseline;
  font-size: var(--t-xs); color: var(--ink-2);
}
.vintage__y { min-width: 58px; font-variant-numeric: tabular-nums; }
.vintage--old { color: var(--warn); }

/* --- badge ---------------------------------------------------------------- */
.badge {
  display: inline-block; font-size: var(--t-xs); font-weight: 600;
  padding: 1px var(--s2); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--ink-2);
  vertical-align: 1px; margin-left: var(--s2);
}
.badge--warn { background: var(--warn-bg); color: var(--warn); }

/* --- buttons -------------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: var(--touch); min-width: var(--touch); padding: 0 var(--s4);
  font-size: var(--t-md); font-weight: 600; font-family: inherit;
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius);
  cursor: pointer; user-select: none;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: default; transform: none; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-in); }
.btn--ghost   { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn--danger  { color: var(--danger); }
.btn--block   { width: 100%; }
.btn--lg      { min-height: 52px; font-size: var(--t-lg); border-radius: var(--radius-lg); }

/* --- stepper -------------------------------------------------------------- */
.stepper { display: flex; align-items: center; gap: var(--s2); }
.stepper__btn {
  width: var(--touch); height: var(--touch); flex: 0 0 auto;
  display: grid; place-items: center;
  font-size: var(--t-lg); font-weight: 600; font-family: inherit;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--ink); cursor: pointer;
}
.stepper__btn:active { background: var(--surface-2); transform: translateY(1px); }
.stepper__btn--minus { color: var(--warn); }
.stepper__btn--plus  { color: var(--accent); }
.stepper__val {
  width: 64px; height: var(--touch); text-align: center; font-size: 16px;
  font-variant-numeric: tabular-nums;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--ink); -webkit-appearance: none; appearance: none;
}
.stepper__label { font-size: var(--t-sm); color: var(--ink-2); min-width: 64px; }

/* --- sheet (the editor) --------------------------------------------------- */
.scrim {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0,0,0,.35); opacity: 0; pointer-events: none;
  transition: opacity .18s;
}
.scrim[data-open] { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  background: var(--surface);
  border-top-left-radius: var(--radius-lg); border-top-right-radius: var(--radius-lg);
  box-shadow: var(--shadow-up);
  padding: var(--s3) var(--s4) calc(var(--safe-b) + var(--s5));
  transform: translateY(101%); transition: transform .22s cubic-bezier(.32,.72,0,1);
  max-height: 86vh; overflow-y: auto;
}
.sheet[data-open] { transform: translateY(0); }
.sheet__grab {
  width: 36px; height: 4px; border-radius: var(--radius-pill);
  background: var(--line-firm); margin: 0 auto var(--s3);
}
.sheet__title { font-size: var(--t-lg); font-weight: 650; margin: 0; }
.sheet__meta  { font-size: var(--t-xs); color: var(--ink-3); margin: 2px 0 var(--s4); }
.sheet__group { padding: var(--s3) 0; border-top: 1px solid var(--line); }
.sheet__group:first-of-type { border-top: 0; }
.sheet__grouptitle {
  font-size: var(--t-xs); text-transform: uppercase; letter-spacing: .06em;
  color: var(--ink-3); margin-bottom: var(--s2);
}
.sheet__actions { display: flex; gap: var(--s2); margin-top: var(--s2); }

/* --- status / empty ------------------------------------------------------- */
.msg { font-size: var(--t-sm); margin-top: var(--s3); min-height: 18px; }
.msg--ok  { color: var(--accent); }
.msg--err { color: var(--danger); }
.empty { color: var(--ink-3); font-size: var(--t-sm); padding: var(--s5) var(--s2); text-align: center; }
.empty b { color: var(--ink); display: block; margin-bottom: var(--s1); }

/* --- sign-in gate --------------------------------------------------------- */
.gate {
  min-height: 100dvh; display: grid; place-content: center; justify-items: center;
  text-align: center; padding: var(--s5); gap: var(--s3);
}
.gate__mark { width: 64px; height: 64px; }
.gate__title { font-size: var(--t-2xl); font-weight: 700; margin: 0; letter-spacing: -.02em; }
.gate__sub { color: var(--ink-3); font-size: var(--t-sm); margin: 0 0 var(--s4); max-width: 30ch; }

/* --- toast ---------------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + var(--s5));
  transform: translate(-50%, 16px); z-index: 60;
  background: var(--ink); color: var(--bg);
  font-size: var(--t-sm); font-weight: 500;
  padding: var(--s3) var(--s4); border-radius: var(--radius-pill);
  box-shadow: var(--shadow); opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s;
  max-width: calc(100vw - var(--s6));
}
.toast[data-show] { opacity: 1; transform: translate(-50%, 0); }
.toast--err { background: var(--danger); color: #fff; }

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* --- wider screens -------------------------------------------------------- */
@media (min-width: 640px) {
  .app { padding-inline: var(--s5); }
  .sheet {
    left: 50%; right: auto; bottom: var(--s5); width: min(560px, calc(100vw - var(--s6)));
    transform: translate(-50%, calc(100% + var(--s5)));
    border-radius: var(--radius-lg);
  }
  .sheet[data-open] { transform: translate(-50%, 0); }
  .sheet__grab { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
