:root {
  --bg: #edf0eb;
  --surface: #ffffff;
  --surface-2: #f5f7f3;
  --ink: #1b2a41;
  --muted: #566074;
  --line: #d6dcd3;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --protein: #2f6fe4;
  --carbs: #e08a00;
  --fat: #d6336c;
  --over: #c2362b;
  --radius: 14px;
  --nav-h: 64px;
  --font: 'Avenir Next', 'Segoe UI Variable Text', 'Segoe UI', system-ui, -apple-system, 'Helvetica Neue', sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10161d;
    --surface: #19212b;
    --surface-2: #202a36;
    --ink: #e8edf4;
    --muted: #9aa7b8;
    --line: #2b3644;
    --primary: #35c2b2;
    --primary-ink: #06201c;
    --protein: #6fa3ff;
    --carbs: #f2a93b;
    --fat: #f27aa6;
    --over: #ff8a7a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 var(--font);
  font-variant-numeric: tabular-nums;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; } /* flex/grid rules below would otherwise win over the hidden attribute */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

#app { max-width: 640px; margin: 0 auto; padding: calc(env(safe-area-inset-top) + 12px) 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 28px); }

/* ---------- brand ---------- */
.brand { display: inline-flex; align-items: center; gap: 10px; font-weight: 800; font-size: 20px; letter-spacing: -0.02em; }
.brand-mark { width: 22px; height: 22px; color: var(--primary); }
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; min-height: 40px; }
.topbar h1 { font-size: 22px; font-weight: 800; letter-spacing: -0.02em; }

/* ---------- buttons & fields ---------- */
.btn { color: inherit; text-decoration: none; border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 11px 16px; font-weight: 650; min-height: 44px; }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.btn.block { display: block; width: 100%; }
.btn.danger { color: var(--over); }
.btn.quiet { border-color: transparent; background: transparent; color: var(--muted); }
.btn:disabled { opacity: 0.55; cursor: default; }
.link { background: none; border: 0; padding: 6px 2px; color: var(--primary); font-weight: 650; min-height: 36px; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); }
.icon-btn svg { width: 22px; height: 22px; }

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span, .label { font-size: 13px; font-weight: 650; color: var(--muted); }
input[type='text'], input[type='search'], input[type='number'], input[type='password'], input[type='date'], select {
  width: 100%; min-height: 46px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
}
input[type='search'] { -webkit-appearance: none; appearance: none; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.hint { font-size: 13px; color: var(--muted); }
.error { color: var(--over); font-size: 14px; font-weight: 600; }

/* ---------- cards ---------- */
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; border: 1px solid var(--line); }
.card h2 { font-size: 17px; font-weight: 750; letter-spacing: -0.01em; }
.empty { color: var(--muted); padding: 14px 0 4px; font-size: 15px; }

/* ---------- date nav ---------- */
.datebar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 14px; }
.datebar .date { position: relative; flex: 1; text-align: center; }
.datebar .date strong { display: block; font-size: 20px; letter-spacing: -0.02em; }
.datebar .date small { color: var(--muted); }
.datebar input[type='date'] { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }

/* ---------- the budget bar ---------- */
.budget { padding: 18px 16px 16px; }
.budget-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.budget .big { font-size: 46px; line-height: 1; font-weight: 800; letter-spacing: -0.04em; }
.budget .big small { font-size: 16px; font-weight: 650; letter-spacing: 0; color: var(--muted); margin-left: 4px; }
.budget.over .big { color: var(--over); }
.budget .side { text-align: right; font-size: 14px; color: var(--muted); line-height: 1.35; }
.budget .side b { color: var(--ink); font-size: 16px; }
.stack { position: relative; display: flex; height: 18px; border-radius: 9px; overflow: hidden; background: var(--surface-2); border: 1px solid var(--line); margin: 14px 0 6px; }
.stack i { display: block; height: 100%; }
.stack .p { background: var(--protein); }
.stack .c { background: var(--carbs); }
.stack .f { background: var(--fat); }
.stack .u { background: var(--muted); opacity: 0.6; }
.stack .tick { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ink); }
.macros { display: grid; gap: 10px; margin-top: 14px; }
.macro { display: grid; grid-template-columns: 86px 1fr auto; align-items: center; gap: 10px; font-size: 14px; }
.macro .name { display: flex; align-items: center; gap: 7px; font-weight: 650; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.dot.p { background: var(--protein); } .dot.c { background: var(--carbs); } .dot.f { background: var(--fat); }
.meter { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; border: 1px solid var(--line); }
.meter i { display: block; height: 100%; border-radius: 4px; }
.meter i.p { background: var(--protein); } .meter i.c { background: var(--carbs); } .meter i.f { background: var(--fat); }
.macro .amt { color: var(--muted); min-width: 92px; text-align: right; }
.macro .amt b { color: var(--ink); }

/* ---------- meals ---------- */
.meal-head { display: flex; align-items: baseline; justify-content: space-between; }
.meal-head .kcal { font-weight: 700; color: var(--muted); }
.entries { list-style: none; margin: 8px 0 0; padding: 0; }
.entry { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; width: 100%; text-align: left; background: none; border: 0; border-top: 1px solid var(--line); padding: 11px 0; border-radius: 0; }
.entries li:first-child .entry { border-top: 0; }
.entry .nm { font-weight: 650; overflow-wrap: anywhere; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; }
.entry .sub { color: var(--muted); font-size: 13px; grid-column: 1; overflow-wrap: anywhere; }
.entry .k { grid-row: 1; grid-column: 2; font-weight: 750; text-align: right; }
.entry .mac { grid-row: 2; grid-column: 2; color: var(--muted); font-size: 12px; text-align: right; white-space: nowrap; }
.meal-actions { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 6px; }

/* ---------- bottom nav ---------- */
.nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.nav ul { display: grid; grid-template-columns: repeat(4, 1fr); max-width: 640px; margin: 0 auto; padding: 0; list-style: none; }
.nav button { width: 100%; height: var(--nav-h); border: 0; background: none; display: grid; place-items: center; gap: 2px; align-content: center; color: var(--muted); font-size: 12px; font-weight: 650; border-radius: 0; }
.nav button svg { width: 24px; height: 24px; }
.nav button[aria-current='page'] { color: var(--primary); }

/* ---------- toast ---------- */
#toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 14px); transform: translate(-50%, 12px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity 0.15s, transform 0.15s; z-index: 100; max-width: calc(100vw - 32px); text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast[data-kind='error'] { background: var(--over); color: #fff; }

/* ---------- sheet (add food) ---------- */
dialog.sheet { border: 0; padding: 0; margin: 0 auto; width: 100%; max-width: 640px; height: 100dvh; max-height: 100dvh; background: var(--bg); color: var(--ink); overflow: hidden; }
dialog.sheet::backdrop { background: rgba(10, 16, 24, 0.55); }
@media (min-width: 700px) {
  dialog.sheet { height: min(88dvh, 820px); margin: auto; border-radius: 20px; border: 1px solid var(--line); }
}
.sheet-wrap { display: grid; grid-template-rows: auto 1fr; height: 100%; }
.sheet-head { display: flex; align-items: center; gap: 10px; padding: calc(env(safe-area-inset-top) + 10px) 14px 10px; border-bottom: 1px solid var(--line); background: var(--surface); }
.sheet-head h2 { flex: 1; font-size: 17px; font-weight: 750; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-body { overflow-y: auto; padding: 14px 16px calc(env(safe-area-inset-bottom) + 24px); }
.searchbar { display: flex; gap: 8px; margin-bottom: 10px; }
.chips { display: flex; gap: 8px; margin: 0 0 12px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 7px 14px; font-size: 14px; font-weight: 650; min-height: 36px; }
.chip[aria-pressed='true'] { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.results h3 { font-size: 13px; font-weight: 700; color: var(--muted); margin: 14px 0 6px; }
.results .status { color: var(--muted); font-size: 14px; padding: 8px 0; }
.food { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; width: 100%; text-align: left; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; margin-bottom: 8px; }
.food .nm { font-weight: 650; overflow-wrap: anywhere; }
.food .sub { grid-column: 1; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.food .mac { grid-column: 1; color: var(--muted); font-size: 12px; }
.food .src { grid-row: 1 / span 3; grid-column: 2; align-self: center; font-size: 11px; font-weight: 700; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 2px 6px; white-space: nowrap; }
.detail-head h3 { font-size: 21px; letter-spacing: -0.02em; overflow-wrap: anywhere; }
.detail-head p { color: var(--muted); margin-top: 2px; }
.qty { display: grid; grid-template-columns: 44px 1fr 44px; gap: 8px; }
.qty button { min-height: 46px; border: 1px solid var(--line); background: var(--surface); border-radius: 12px; font-size: 22px; font-weight: 700; }
.qty input { text-align: center; }
.facts { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin: 4px 0 16px; }
.facts .kc { display: flex; align-items: baseline; justify-content: space-between; }
.facts .kc strong { font-size: 40px; font-weight: 800; letter-spacing: -0.04em; line-height: 1; }
.facts .kc span { color: var(--muted); font-weight: 650; }
.facts .three { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 12px 0 4px; }
.facts .three div { border-top: 3px solid var(--line); padding-top: 6px; }
.facts .three div.p { border-color: var(--protein); } .facts .three div.c { border-color: var(--carbs); } .facts .three div.f { border-color: var(--fat); }
.facts .three b { font-size: 20px; display: block; letter-spacing: -0.02em; }
.facts .three span { font-size: 13px; color: var(--muted); }
.facts dl { margin: 10px 0 0; display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; font-size: 14px; }
.facts dt { color: var(--muted); } .facts dd { margin: 0; text-align: right; font-weight: 650; }
.actions { display: grid; gap: 10px; }
.star { font-size: 22px; line-height: 1; }
video.scan { width: 100%; max-height: 60dvh; background: #000; border-radius: 12px; }

/* ---------- progress ---------- */
.seg { display: inline-grid; grid-auto-flow: column; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; margin-bottom: 14px; }
.seg button { border: 0; background: none; padding: 8px 16px; border-radius: 9px; font-weight: 650; color: var(--muted); min-height: 38px; }
.seg button[aria-pressed='true'] { background: var(--primary); color: var(--primary-ink); }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin: 12px 0 0; }
.stats div b { display: block; font-size: 24px; letter-spacing: -0.03em; }
.stats div span { font-size: 13px; color: var(--muted); }
svg.chart { width: 100%; height: auto; display: block; margin-top: 8px; overflow: visible; }
svg.chart text { fill: var(--muted); font-size: 11px; font-family: var(--font); }
svg.chart text.goal-label { paint-order: stroke; stroke: var(--surface); stroke-width: 4px; stroke-linejoin: round; }
svg.chart .grid { stroke: var(--line); stroke-width: 1; }
svg.chart .goal { stroke: var(--ink); stroke-width: 1.5; stroke-dasharray: 5 4; }
svg.chart .bar { fill: var(--primary); }
svg.chart .bar.over { fill: var(--over); }
svg.chart .trend { stroke: var(--primary); stroke-width: 2.5; fill: none; stroke-linejoin: round; stroke-linecap: round; }
svg.chart .raw { fill: var(--muted); opacity: 0.8; }

/* ---------- login ---------- */
.login { max-width: 360px; margin: 14vh auto 0; }
.login .brand { font-size: 30px; margin-bottom: 6px; }
.login .brand-mark { width: 34px; height: 34px; }
.login p { color: var(--muted); margin-bottom: 20px; }

.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; vertical-align: -3px; margin-right: 8px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 3s; }
  #toast { transition: none; }
}
