:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede7;
  --text: #1c1d1a;
  --muted: #62645d;
  --line: #e2dfd7;
  --accent: #2f6b3f;
  --accent-ink: #ffffff;
  --accent-soft: #e2efe4;
  --warn: #9a5b00;
  --warn-soft: #fbeed6;
  --danger: #a3302a;
  --danger-soft: #f8e1df;
  --info-soft: #e4ecf7;
  --info: #2b5a94;
  --carbs: #c47f17;
  --protein: #2f6b3f;
  --salt: #5a6b80;
  --radius: 14px;
  --tap: 44px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.05);
  --tabbar-h: calc(60px + env(safe-area-inset-bottom));
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141513; --surface: #1d1f1c; --surface-2: #262924; --text: #ecebe6; --muted: #a3a59d;
    --line: #33362f; --accent: #7cc48d; --accent-ink: #0f1a12; --accent-soft: #223a28;
    --warn: #f0b453; --warn-soft: #3a2d14; --danger: #f08a82; --danger-soft: #3d1f1c;
    --info-soft: #1d2a3a; --info: #8db6ea; --carbs: #e5a647; --protein: #7cc48d; --salt: #9fb0c4;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
/* Component rules set display (grid, block, flex), which would beat the UA's [hidden] rule. */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: var(--tabbar-h);
}
main { max-width: 640px; margin: 0 auto; padding: calc(12px + env(safe-area-inset-top)) 16px 24px; outline: none; }
h1 { font-size: 1.6rem; margin: 4px 0 2px; letter-spacing: -.01em; }
h2 { font-size: 1.02rem; margin: 22px 0 8px; color: var(--muted); font-weight: 600; text-transform: none; }
h3 { font-size: 1rem; margin: 0; }
p { margin: 4px 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.row { display: flex; align-items: center; gap: 10px; }
.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }
.num { font-variant-numeric: tabular-nums; }
.skip-link { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; z-index: 100; }
.skip-link:focus { left: 8px; }
.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; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

/* ---------- header */
.top { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.top .sub { color: var(--muted); font-size: .9rem; }

/* ---------- cards */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.card + .card { margin-top: 10px; }
.card.tight { padding: 10px 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.stat .v { font-size: 1.15rem; font-weight: 650; }
.stat .l { font-size: .75rem; color: var(--muted); }
.divider { height: 1px; background: var(--line); margin: 12px 0; }

/* ---------- buttons */
button, .btn {
  font: inherit; min-height: var(--tap); padding: 0 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); cursor: pointer; display: inline-flex; align-items: center;
  justify-content: center; gap: 8px; text-decoration: none; touch-action: manipulation;
}
button:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-danger { color: var(--danger); }
.btn-ghost.btn-danger { color: var(--danger); }
.btn-danger-fill { background: var(--danger); border-color: var(--danger); color: var(--accent-ink); font-weight: 600; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--accent); padding: 0 10px; }
.btn-block { width: 100%; }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: .9rem; border-radius: 10px; }
.icon-btn { width: var(--tap); padding: 0; border-radius: 50%; }
button:active:not(:disabled) { transform: scale(.98); }

/* ---------- chips */
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: .75rem; font-weight: 600; padding: 2px 8px;
  border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.danger { background: var(--danger-soft); color: var(--danger); }
.chip.ok { background: var(--accent-soft); color: var(--accent); }
.chip.info { background: var(--info-soft); color: var(--info); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }

/* ---------- banners */
.banner { border-radius: var(--radius); padding: 12px 14px; margin: 10px 0; }
.banner.warn { background: var(--warn-soft); color: var(--text); }
.banner.info { background: var(--info-soft); }
.banner.ok { background: var(--accent-soft); }
.banner.danger { background: var(--danger-soft); color: var(--text); }
.pre { white-space: pre-line; }
.supplements { margin: 8px 0 0; padding-left: 18px; }
.banner ul { margin: 6px 0 0; padding-left: 18px; }
.banner .row { margin-top: 8px; }
details > summary { cursor: pointer; min-height: 32px; display: flex; align-items: center; gap: 6px; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
/* display:flex drops the native disclosure triangle, so draw a chevron that turns when open. */
details > summary::before { content: ""; flex: 0 0 auto; width: 7px; height: 7px; border: solid currentColor;
  border-width: 0 2px 2px 0; transform: rotate(-45deg); margin: 0 4px 0 2px; transition: transform .15s; }
details[open] > summary::before { transform: rotate(45deg); }
@media (prefers-reduced-motion: reduce) { details > summary::before { transition: none; } }
.field-error { color: var(--danger); }
.banner.offline { background: var(--surface-2); }

/* ---------- target bars */
.target { margin-top: 10px; }
.target .lbl { display: flex; justify-content: space-between; font-size: .9rem; }
.bar { position: relative; height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin-top: 4px; }
.bar > span { position: absolute; inset: 0 auto 0 0; border-radius: 999px; }
.bar .plan { opacity: .3; }
.bar.carbs span { background: var(--carbs); }
.bar.protein span { background: var(--protein); }
.bar.salt span { background: var(--salt); }
.bar.over { outline: 2px solid var(--warn); outline-offset: 1px; }
.over-txt { color: var(--warn); font-weight: 600; }

/* ---------- meals */
.meal { display: flex; gap: 4px; align-items: stretch; padding: 0; overflow: hidden; }
.meal .tick { flex: 0 0 56px; min-height: 64px; border: 0; border-radius: 0; background: transparent; font-size: 1.3rem; }
.tick .box { width: 28px; height: 28px; border: 2px solid var(--muted); border-radius: 8px; display: grid; place-items: center; font-size: 1rem; }
.meal.eaten .tick .box { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.meal .body { flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; padding: 12px 12px 12px 0;
  display: block; border-radius: 0; min-height: 64px; }
.meal .body > span:not(.chips):not(.sr-only) { display: block; }
.meal .time { font-size: .8rem; color: var(--muted); font-weight: 600; }
.meal .title { font-weight: 600; margin: 1px 0 2px; }
.meal .items { font-size: .88rem; color: var(--muted); }
.meal .macros { font-size: .8rem; color: var(--muted); margin-top: 4px; }
.meal.eaten .title, .meal.skipped .title { color: var(--muted); }
.meal.skipped .title { text-decoration: line-through; }
.meal .chev { align-self: center; color: var(--muted); padding-right: 12px; }
.ut { color: var(--warn); font-weight: 600; }

/* ---------- lists */
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list > * + * { border-top: 1px solid var(--line); }
.li { display: flex; align-items: center; gap: 10px; min-height: 56px; padding: 8px 14px; width: 100%;
  background: transparent; border: 0; border-radius: 0; text-align: left; color: inherit; }
button.li:active { background: var(--surface-2); transform: none; }
.li .main { flex: 1; min-width: 0; }
.li .name { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li .meta { font-size: .85rem; color: var(--muted); }
.li.done .name { text-decoration: line-through; color: var(--muted); }
.check { width: 24px; height: 24px; accent-color: var(--accent); flex: 0 0 auto; }

.search { width: 100%; min-height: var(--tap); border-radius: 12px; border: 1px solid var(--line); background: var(--surface);
  padding: 0 14px; font: inherit; color: inherit; margin: 6px 0 4px; }

/* ---------- forms */
label.field { display: block; font-size: .82rem; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  min-height: var(--tap); padding: 0 12px; width: 100%; margin-top: 3px;
}
textarea { padding: 10px 12px; min-height: 88px; resize: vertical; }
input[type=checkbox] { width: 24px; min-height: 24px; margin: 0; accent-color: var(--accent); }
.fields { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.fields .span2 { grid-column: span 2; }
.fields .span-all { grid-column: 1 / -1; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: transparent; min-height: 40px; border-radius: 9px; font-weight: 550; }
.seg button[aria-pressed=true] { background: var(--surface); box-shadow: var(--shadow); color: var(--accent); }
.amounts { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.amounts button[aria-pressed=true] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 650; }

/* ---------- receipt lines */
.line { padding: 12px 14px; }
.line.off { opacity: .6; }
.line .raw { font-family: ui-monospace, Menlo, monospace; font-size: .75rem; color: var(--muted); }
.line .fields { margin-top: 8px; grid-template-columns: repeat(3, 1fr); }
.preview { width: 100%; max-height: 260px; object-fit: contain; border-radius: var(--radius); background: var(--surface-2); }
.sticky-foot { position: sticky; bottom: calc(var(--tabbar-h) + 8px); margin-top: 14px; z-index: 5; }
.sticky-foot .btn { box-shadow: 0 6px 20px rgba(0,0,0,.18); }

/* ---------- tab bar */
.tabbar { position: fixed; inset: auto 0 0 0; height: var(--tabbar-h); padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line); display: grid; grid-template-columns: repeat(4, 1fr); z-index: 20; }
.tabbar a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 600; min-height: var(--tap); }
.tabbar .ico { line-height: 0; }
.tabbar a[aria-current=page] { color: var(--accent); }
.tabbar .tab-scan .ico { background: var(--accent); color: var(--accent-ink); width: 44px; height: 30px; border-radius: 10px;
  display: grid; place-items: center; }
.tabbar .tab-scan .ico svg { width: 20px; height: 20px; }

/* ---------- sheet */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; opacity: 0; transition: opacity .2s; }
.backdrop.open { opacity: 1; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-height: 92vh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--bg); border-radius: 20px 20px 0 0; padding: 8px 16px calc(20px + env(safe-area-inset-bottom));
  z-index: 41; transform: translateY(100%); transition: transform .25s cubic-bezier(.2,.8,.2,1); max-width: 640px; margin: 0 auto; }
.sheet.open { transform: none; }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 10px; }
.sheet-handle { margin: -8px -16px 0; padding: 8px 16px 0; }  /* the swipe-to-close zone: grab bar and title */
.item-more { margin-top: 4px; }
.sheet h2 { margin-top: 18px; }
.sheet-head { display: flex; align-items: flex-start; gap: 10px; }
.sheet-head h3 { font-size: 1.15rem; flex: 1; padding-top: 8px; }
@media (prefers-reduced-motion: reduce) { .sheet, .backdrop { transition: none; } }

/* ---------- toast */
.toast { position: fixed; left: 16px; right: 16px; bottom: calc(var(--tabbar-h) + 12px); max-width: 600px; margin: 0 auto;
  background: var(--text); color: var(--bg); border-radius: 12px; padding: 10px 10px 10px 16px; z-index: 60;
  display: flex; align-items: center; gap: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.25); }
.toast[hidden] { display: none; }
.toast .grow { font-size: .95rem; }
.toast button { background: transparent; color: var(--bg); border-color: transparent; font-weight: 700; min-height: 36px; }

/* ---------- loading */
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--line), var(--surface-2)); background-size: 200% 100%;
  animation: shimmer 1.2s infinite; border-radius: var(--radius); height: 72px; margin-top: 10px; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } }
.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%;
  animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { text-align: center; padding: 28px 12px; color: var(--muted); }
.empty .big { font-size: 2.4rem; }
.center { text-align: center; }
.login { max-width: 360px; margin: 18vh auto 0; }
.hidden-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* settings */
.settings .cost { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.settings .row.between > :last-child { text-align: right; white-space: nowrap; }
