/* Gruel app styles. Ported from design/mockup.html (personality "tavern").
   No literal colours here — every colour comes from tokens.css. */

/* =====================================================================
   1. Base
   ===================================================================== */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { height: 100%; background: var(--bg); overflow: hidden; }   /* iOS rubber-band overscroll must never show white */
body {
  margin: 0;
  /* container-type sits on body, not only on #app: an element cannot be styled by its
     own container query, and the >=900px rule changes the grid columns of #app itself.
     body is always exactly as wide as #app, so every query below reads the same width. */
  container-type: inline-size;
  background: var(--bg);
  color: var(--ink);
  font: var(--fs-0)/var(--lh) var(--font-body);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  margin: 0;
  font-family: var(--font-head);
  font-weight: var(--head-weight);
  font-variation-settings: var(--head-opsz);
  text-wrap: balance;
}
p { margin: 0; }
:focus-visible { outline: 2px solid var(--primary-ink); outline-offset: 2px; }

/* =====================================================================
   2. App shell
   ===================================================================== */
#app {
  container-type: inline-size;
  position: fixed; inset: 0;   /* page never scrolls (iOS keyboard would leave the tab bar stranded); only .content does */
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
}
.pane { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
.hdr { flex: none; padding-top: env(safe-area-inset-top); border-bottom: 1px solid var(--line); }
.hdr .wrap { padding-block: 12px; }
.hdr h1 { font-size: var(--fs-2); }
.content { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding-block: 14px 16px; }
.wrap { width: 100%; margin-inline: auto; padding-inline: var(--pad); }
.stack { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--gap); }

/* =====================================================================
   3. Nav
   ===================================================================== */
.rail {
  display: none;
  flex-direction: column;
  gap: 2px;
  padding: calc(14px + env(safe-area-inset-top)) 8px 14px;
  border-right: 1px solid var(--line);
}
.tabs {
  flex: none; display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid var(--line);
  background: var(--surface);
}
.nav-item {
  appearance: none; border: 0; background: none; font: inherit;
  display: flex; align-items: center; gap: 10px;
  min-height: 44px; padding: 0 12px;
  color: var(--muted); text-decoration: none; cursor: pointer;
  border-radius: var(--r-control);
  transition: background-color 200ms ease, color 200ms ease, transform 160ms var(--ease-out);
}
.tabs .nav-item {
  flex: 1 1 0; min-width: 0; flex-direction: column; justify-content: center;
  gap: 4px; min-height: 56px; padding: 6px 2px; border-radius: 0;
  font-size: var(--fs-2xs);
}
.tabs .nav-item span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item.is-active { color: var(--primary-ink); }
.rail .nav-item.is-active { background: var(--surface-2); }
.ic { width: 22px; height: 22px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }

/* =====================================================================
   4. Controls
   ===================================================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--btn-h); padding: 0 18px;
  border: 1px solid transparent; border-radius: var(--r-control);
  font: inherit; font-weight: 500; text-decoration: none; cursor: pointer;
  transition: transform 160ms var(--ease-out),
              background-color 200ms ease, color 200ms ease, border-color 200ms ease;
}
.btn-primary { background: var(--primary); color: var(--on-primary); box-shadow: var(--btn-shadow); }
.btn-secondary { background: transparent; color: var(--ink); border-color: var(--line); }
.btn-text {
  appearance: none; background: none; border: 0; padding: 0 4px;
  min-height: 44px; display: inline-flex; align-items: center;
  color: var(--primary-ink); font: inherit; cursor: pointer;
  transition: transform 160ms var(--ease-out), color 200ms ease;
}
.btn-block { width: 100%; }
.btn-row { display: flex; flex-wrap: wrap; gap: 12px; }
.btn:active, .btn-text:active, .icon-btn:active,
.entry:active, .res:active, .row:active, .nav-item:active { transform: scale(0.97); }
.btn:disabled, .input:disabled, .icon-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.icon-btn {
  appearance: none; flex: none; width: 44px; height: 44px;
  display: grid; place-items: center;
  background: none; border: 1px solid var(--line); border-radius: var(--r-control);
  color: var(--ink); font: inherit; font-size: var(--fs-1); cursor: pointer;
  transition: transform 160ms var(--ease-out), background-color 200ms ease, border-color 200ms ease;
}

.field { display: grid; gap: 6px; min-width: 0; }
.field > label { font-size: var(--fs-sm); color: var(--muted); }
.input {
  appearance: none; -webkit-appearance: none;
  width: 100%; min-width: 0; min-height: 44px; padding: 0 12px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--r-control);
  font: inherit;
  transition: border-color 200ms ease;
}
.input::placeholder { color: var(--muted); }
.input[readonly] { background: var(--bg); color: var(--muted); }
.input:focus-visible { border-color: var(--primary-ink); }

/* shared text colours. .hint is the mockup name, .muted / .danger the brief aliases. */
.hint, .muted { color: var(--muted); font-size: var(--fs-sm); }
.backup-line, .danger { color: var(--danger); }

@media (hover: hover) and (pointer: fine) {
  .btn-primary:hover { background: var(--primary-hi); }
  .btn-secondary:hover, .icon-btn:hover { border-color: var(--muted); background: var(--surface); }
  .btn-text:hover { text-decoration: underline; }
  .nav-item:hover { color: var(--ink); }
  .rail .nav-item:hover { background: var(--surface); }
  .input:hover { border-color: var(--muted); }
  .entry:hover, .res:hover, .row:hover { background: var(--surface); }
}
@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0ms !important; }
  .btn:active, .btn-text:active, .icon-btn:active,
  .entry:active, .res:active, .row:active, .nav-item:active, .seg button:active { transform: none; }
  .ios-banner .btn-text:active { transform: none; }
}

/* =====================================================================
   5. Screen pieces — scan
   ===================================================================== */
.scan { min-height: 100%; grid-template-rows: minmax(0, 1fr) auto; }   /* camera takes every pixel above the lookup row */
.viewfinder {
  position: relative; height: 100%; min-height: 0; width: 100%;
  display: grid; place-items: center; overflow: hidden;
  background: var(--surface); border-radius: var(--r-card);
}
.viewfinder video { width: 100%; height: 100%; object-fit: cover; }
/* L-shaped brackets: base sets style + colour, each corner draws only its two outer edges */
.viewfinder i { position: absolute; width: 28px; height: 28px; border: 0 solid var(--primary-ink); }
.viewfinder .vf-tl { top: 12px; left: 12px; border-top-width: 2px; border-left-width: 2px; }
.viewfinder .vf-tr { top: 12px; right: 12px; border-top-width: 2px; border-right-width: 2px; }
.viewfinder .vf-bl { bottom: 12px; left: 12px; border-bottom-width: 2px; border-left-width: 2px; }
.viewfinder .vf-br { bottom: 12px; right: 12px; border-bottom-width: 2px; border-right-width: 2px; }
.vf-hint { position: absolute; bottom: 14px; color: var(--muted); font-size: var(--fs-sm); }
.lookup { display: flex; gap: 10px; align-items: stretch; }
.lookup .input { flex: 1 1 auto; }
.lookup .btn { flex: none; }

/* the product card is the only card */
.card {
  display: grid; gap: 10px; padding: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-card);
}
.prod-name { font-size: var(--fs-2); }
.prod-brand { color: var(--ink); font-size: var(--fs-sm); }
.prod-code { color: var(--muted); font-variant-numeric: tabular-nums; }
.prod-head { display: grid; gap: 3px; }
.prod-head > * { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.per100 { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
.per100 caption { text-align: left; padding-bottom: 6px; color: var(--muted); font-size: var(--fs-xs); }
.per100 th { text-align: left; font-weight: 400; color: var(--muted); }
.per100 td { text-align: right; font-variant-numeric: tabular-nums; }
.per100 th, .per100 td { padding: 4px 0; border-bottom: 1px solid var(--line); }
.per100 tr:last-child th, .per100 tr:last-child td { border-bottom: 0; }

.when { display: grid; gap: 8px; }
.seg { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; padding: 4px; background: var(--bg); border: 1px solid var(--line); border-radius: var(--r-control); }
.seg button { appearance: none; border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600; min-height: 40px; border-radius: calc(var(--r-control) - 4px); transition: background 200ms ease, color 200ms ease, transform 160ms var(--ease-out); }
.seg button:active { transform: scale(.97); }
.seg button[aria-pressed="true"] { background: var(--surface-2); color: var(--ink); }
.when .input[type="date"] { color-scheme: dark; }
.grams-field { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.grams-field .input { max-width: 120px; font-variant-numeric: tabular-nums; }
.grams-field .unit { color: var(--muted); }
.computed { font-size: var(--fs-1); font-variant-numeric: tabular-nums; }
.computed strong { color: var(--primary-ink); font-weight: 600; }

.notice { color: var(--ink); }
.form { display: grid; gap: 14px; }
.macros { display: grid; gap: 14px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.req { color: var(--muted); }

/* =====================================================================
   6. Screen pieces — search
   ===================================================================== */
.search-field { position: relative; }
.search-field .ic { position: absolute; left: 12px; top: 11px; width: 20px; height: 20px; color: var(--muted); }
.search-field .input { padding-left: 40px; }
.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-top: 1px solid var(--line); }
/* .res is the mockup name for a tappable list row (an <a> there); .row is the brief alias, used
   on a <button> in search.js — the reset properties strip default button chrome so both match. */
.res, .row {
  display: flex; align-items: center; gap: 12px;
  width: 100%; min-height: 56px; padding: 8px 4px;
  background: none; border: 0; text-align: left;
  color: inherit; text-decoration: none; font: inherit; cursor: pointer;
  transition: background-color 200ms ease, transform 160ms var(--ease-out);
}
.res-name { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.res-kcal { flex: none; color: var(--muted); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
.pill {
  flex: none; padding: 2px 8px; border-radius: var(--r-pill);
  background: var(--surface-2); color: var(--ink); font-size: var(--fs-xs);
}

/* =====================================================================
   7. Screen pieces — log
   ===================================================================== */
.daybar { display: flex; align-items: center; gap: 8px; }
.daybar h2 { flex: 1 1 auto; min-width: 0; text-align: center; font-size: var(--fs-1); }
.nudge { color: var(--muted); font-size: var(--fs-sm); }
.nudge a { color: var(--primary-ink); }
.log-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--gap); }
.totals { display: grid; gap: 12px; align-content: start; }
.total-top { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 5px; font-size: var(--fs-sm); }
.total-top span:last-child { color: var(--muted); font-variant-numeric: tabular-nums; }
.bar { height: var(--bar-h); background: var(--surface-2); border-radius: var(--r-pill); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: var(--r-pill); }
.entry {
  display: flex; align-items: flex-start; gap: 12px;
  min-height: 56px; padding: 10px 4px;
  color: inherit; text-decoration: none; cursor: pointer;
  transition: background-color 200ms ease, transform 160ms var(--ease-out);
}
.e-main { flex: 1 1 auto; min-width: 0; }
/* name and grams share a row: only the name ellipsises, the grams are never eaten */
.e-top { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
.e-name { display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.e-grams { flex: none; font-variant-numeric: tabular-nums; }
.e-sub { display: block; margin-top: 2px; color: var(--muted); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
.e-kcal { flex: none; padding-top: 1px; font-variant-numeric: tabular-nums; }
/* the whole entry row is the tap target, as in the mockup; there it is an <a>, here a <button>. */
button.entry { appearance: none; width: 100%; background: none; border: 0; text-align: left; font: inherit; }
/* inline grams edit: name on its own line, then the field and the three actions, wrapping at 390px */
.edit { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 10px 4px; }
.edit .e-name { flex: 1 1 100%; }
.edit .input { flex: 0 1 96px; font-variant-numeric: tabular-nums; }

.empty { display: grid; justify-items: center; gap: 16px; padding: 30px 0 8px; text-align: center; }
.empty img { width: 96px; height: 96px; opacity: 0.4; }
.empty p { max-width: 32ch; color: var(--muted); }

/* =====================================================================
   8. Screen pieces — settings / about
   ===================================================================== */
.section + .section { margin-top: 26px; padding-top: 24px; border-top: 1px solid var(--line); }
.section > h2 { margin-bottom: 12px; font-size: var(--fs-1); }
.targets { display: grid; gap: 14px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.targets .input { font-variant-numeric: tabular-nums; }
.data-line { color: var(--muted); font-variant-numeric: tabular-nums; }
.attrib { list-style: none; margin: 0; padding: 0; display: grid; gap: 14px; color: var(--muted); font-size: var(--fs-sm); line-height: 1.55; }
.attrib li { overflow-wrap: anywhere; }
.attrib a { color: var(--primary-ink); }
.version { color: var(--muted); font-size: var(--fs-sm); }
.mark { width: 64px; height: 64px; border-radius: var(--r-control); }

/* =====================================================================
   9. iOS add-to-home-screen banner
   ===================================================================== */
.ios-banner {
  position: relative; z-index: var(--z-banner); flex: none;
  display: flex; align-items: center; gap: 12px;
  padding: 10px var(--pad);
  background: var(--surface-2); border-top: 1px solid var(--line);
}
.ios-banner p { flex: 1 1 auto; min-width: 0; font-size: var(--fs-sm); }
.ios-banner .ic { width: 20px; height: 20px; color: var(--primary-ink); }
.ios-banner .btn-text {
  appearance: none; flex: none; width: 44px; height: 44px;
  display: grid; place-items: center;
  background: none; border: 0; padding: 0;
  color: var(--muted); font: inherit; font-size: var(--fs-1); cursor: pointer;
  transition: color 200ms ease, transform 160ms var(--ease-out);
}
.ios-banner .btn-text:active { transform: scale(0.97); }

/* =====================================================================
   10. Toast
   ===================================================================== */
/* enter and exit both animate: opacity + 8px translateY over 200ms. `translate` is a separate
   property from the centring `transform: translateX(-50%)`, so the two compose without fighting.
   visibility (not [hidden]/display:none) keeps the element transitionable while inert. */
.toast {
  position: fixed; left: 50%; bottom: calc(72px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  translate: 0 8px;
  max-width: calc(100% - var(--pad) * 2);
  background: var(--surface-2); color: var(--ink);
  padding: 10px 14px; border-radius: var(--r-control);
  z-index: var(--z-toast);
  opacity: 0; visibility: hidden; pointer-events: none;
  transition: opacity 200ms var(--ease-out), translate 200ms var(--ease-out), visibility 200ms var(--ease-out);
}
.toast.is-open { opacity: 1; visibility: visible; translate: 0 0; }
.toast.danger { color: var(--danger); }
/* has to live here, after .toast: the section 4 reduced-motion block is earlier in the file and
   would lose the specificity tie against `.toast { translate: 0 8px }`. */
@media (prefers-reduced-motion: reduce) { .toast, .toast.is-open { translate: none; } }

/* =====================================================================
   11. Modal
   ===================================================================== */
.modal {
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--r-card);
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  overflow: auto; padding: 0; margin: auto;
  z-index: var(--z-modal);   /* the dialog top layer ignores z-index; kept for token consistency */
  opacity: 1; scale: 1;
  transition: opacity 200ms var(--ease-out), scale 200ms var(--ease-out);
}
.modal::backdrop { background: oklch(0 0 0 / .55); }
.modal-body { padding: 0; }
.modal .card { border: 0; border-radius: 0; }   /* the dialog is the frame; no card-in-a-card */
@starting-style { .modal { opacity: 0; scale: .97; } }
/* reduced motion: the section 4 rule already forces transition-duration: 0ms on everything,
   so this enter animation is already instant there — no extra override needed. */

/* =====================================================================
   12. Container queries — one markup set, four form factors
   ===================================================================== */
@container (min-width: 600px) {
  .wrap { max-width: calc(640px + var(--pad) * 2); }
  .ios-banner { display: none; }
  /* the add-to-Home-Screen advice is phone-only; the missing-food-book warning is not */
  .ios-banner.data-banner { display: flex; }
  .targets { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .totals { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 28px; }
  .content { padding-block: 24px 40px; }
}
@container (min-width: 900px) {
  #app { grid-template-columns: 200px minmax(0, 1fr); }
  .rail { display: flex; }
  .tabs { display: none; }
  .wrap { max-width: calc(720px + var(--pad) * 2); }
  .hdr .wrap { padding-block: 16px; }
  .daybar { max-width: 360px; margin-inline: auto; justify-content: center; gap: 6px; }
  .daybar h2 { flex: 0 1 auto; text-align: center; }
}
@container (min-width: 1200px) {
  .wrap:has(.log-grid) { max-width: calc(1000px + var(--pad) * 2); }
  .log-grid { grid-template-columns: minmax(0, 1fr) 250px; gap: 36px; }
  .log-grid > .entries { grid-column: 1; grid-row: 1; }
  .log-grid > .totals { grid-column: 2; grid-row: 1; grid-template-columns: minmax(0, 1fr); align-self: start; position: sticky; top: 0; z-index: var(--z-sticky); }
  /* the daybar sits outside .log-grid, so centre it over the entries column (wrap minus panel and gap)
     instead of over the whole 1000px wrap, where it drifted 143px right of the list it labels. */
  .daybar { max-width: calc(100% - 250px - 36px); margin-inline: 0; }
}
