:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #15201b;
  --muted: #66736d;
  --line: #e3e8e5;
  --brand: #0e9f6e;
  --brand-ink: #ffffff;
  --brand-soft: #dcf3ea;
  --carbs: #3b82f6;
  --protein: #e0567a;
  --fat: #f0a020;
  --water: #1fa2d6;
  --warn: #d9480f;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --radius: 16px;
  --tabbar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1311;
    --card: #17201c;
    --text: #e8efeb;
    --muted: #93a29b;
    --line: #26322d;
    --brand: #22c38e;
    --brand-ink: #062118;
    --brand-soft: #173a2d;
    --carbs: #60a5fa;
    --protein: #f17a98;
    --fat: #f5b84a;
    --water: #44bde8;
    --warn: #ff7a45;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg);
  padding: calc(env(safe-area-inset-top) + 10px) 16px 8px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  max-width: 560px; margin: 0 auto;
}
.topbar h1 { font-size: 24px; font-weight: 700; letter-spacing: -.01em; }
.datebar { display: flex; align-items: center; gap: 2px; }
.datebar[hidden] { display: none; }
.date-label {
  border: 0; background: var(--card); padding: 6px 12px; border-radius: 999px;
  font-weight: 600; font-size: 14px; box-shadow: var(--shadow); min-width: 96px;
}
.icon-btn {
  border: 0; background: transparent; width: 36px; height: 36px; border-radius: 50%;
  font-size: 22px; line-height: 1; display: grid; place-items: center; color: var(--muted);
}
.icon-btn:active { background: var(--line); }

main { max-width: 560px; margin: 0 auto; padding: 4px 16px 0; }

/* Cards */
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 12px;
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }
.card-head h3 { font-size: 17px; font-weight: 650; }
.card-head .sub { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

/* Calorie summary */
.summary { display: flex; align-items: center; gap: 16px; }
.ring { width: 132px; height: 132px; flex: none; position: relative; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 11; }
.ring .track { stroke: var(--line); }
.ring .fill { stroke: var(--brand); stroke-linecap: round; transition: stroke-dashoffset .5s cubic-bezier(.2,.8,.2,1); }
.ring.over .fill { stroke: var(--warn); }
.ring-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring-center b { font-size: 26px; font-variant-numeric: tabular-nums; line-height: 1.1; }
.ring.over .ring-center b { color: var(--warn); }
.ring-center span { font-size: 12px; color: var(--muted); }
.eq { flex: 1; display: grid; gap: 8px; }
.eq-row { display: flex; justify-content: space-between; font-size: 14px; }
.eq-row b { font-variant-numeric: tabular-nums; }

/* Macro bars */
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.macro .label { font-size: 13px; color: var(--muted); }
.macro .val { font-weight: 600; font-variant-numeric: tabular-nums; font-size: 15px; }
.bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin-top: 6px; }
.bar i { display: block; height: 100%; border-radius: 3px; transition: width .4s ease; }

/* Water */
.water-row { display: flex; align-items: center; gap: 12px; }
.water-row .grow { flex: 1; }
.round-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--card); font-size: 22px; display: grid; place-items: center; flex: none;
}
.round-btn.primary { background: var(--water); color: #fff; border-color: transparent; }
.round-btn:active { transform: scale(.94); }

/* Quick actions */
.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 12px; }
.quick button {
  background: var(--card); border: 0; border-radius: 14px; padding: 14px 6px; box-shadow: var(--shadow);
  display: grid; gap: 4px; justify-items: center; font-size: 13px; font-weight: 600;
}
.quick button span { font-size: 22px; }
.quick button:active { transform: scale(.97); }

/* Diary */
.meal .entry, .list-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line);
  width: 100%; background: none; border-left: 0; border-right: 0; border-bottom: 0; text-align: left;
}
.entry .info, .list-item .info { flex: 1; min-width: 0; }
.entry .name, .list-item .name { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .meta, .list-item .meta { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .kcal, .list-item .kcal { font-variant-numeric: tabular-nums; font-weight: 600; }
.meal-actions { display: flex; gap: 8px; border-top: 1px solid var(--line); padding-top: 10px; }
.link-btn {
  border: 0; background: none; color: var(--brand); font-weight: 650; padding: 4px 0; font-size: 15px;
}
.link-btn.danger { color: var(--warn); }
.empty { color: var(--muted); font-size: 14px; padding: 4px 0 10px; }

table.totals { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
table.totals th, table.totals td { padding: 6px 4px; text-align: right; }
table.totals th:first-child, table.totals td:first-child { text-align: left; }
table.totals th { color: var(--muted); font-weight: 500; font-size: 12px; }
table.totals tr + tr td { border-top: 1px solid var(--line); }
.neg { color: var(--warn); }

/* Charts */
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 10px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; text-align: center; }
.stats b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.stats span { font-size: 12px; color: var(--muted); }

/* Forms */
label.field { display: grid; gap: 6px; margin-bottom: 14px; font-size: 14px; font-weight: 550; }
label.field small { color: var(--muted); font-weight: 400; }
.input, select.input {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 12px;
  padding: 12px; font-size: 16px; outline: none; appearance: none;
}
.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block; text-align: center; padding: 8px 6px; border-radius: 9px; font-size: 14px; font-weight: 550; color: var(--muted);
}
.seg input:checked + span { background: var(--card); color: var(--text); box-shadow: var(--shadow); }
.seg input:focus-visible + span { outline: 2px solid var(--brand); }
.choices { display: grid; gap: 8px; }
.choice { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.choice input { accent-color: var(--brand); margin-top: 3px; }
.choice b { display: block; font-size: 15px; font-weight: 600; }
.choice small { color: var(--muted); font-weight: 400; }
.choice:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }

.btn {
  display: block; width: 100%; border: 0; border-radius: 14px; padding: 14px; font-weight: 700; font-size: 16px;
  background: var(--brand); color: var(--brand-ink);
}
.btn:active { filter: brightness(.95); transform: scale(.99); }
.btn.secondary { background: var(--bg); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--warn); border: 1px solid var(--line); }
.btn + .btn { margin-top: 10px; }
.btn[disabled] { opacity: .5; }

/* Onboarding */
.onboard { padding-top: 8px; }
.onboard .hero { text-align: center; padding: 12px 0 20px; }
.onboard .hero .logo { width: 64px; height: 64px; margin: 0 auto 12px; }
.onboard .hero h2 { font-size: 26px; margin-bottom: 6px; }
.onboard .hero p { color: var(--muted); margin: 0; }
.goal-preview { text-align: center; background: var(--brand-soft); border-radius: 14px; padding: 14px; margin-bottom: 14px; }
.goal-preview b { font-size: 28px; display: block; font-variant-numeric: tabular-nums; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
}
.tabbar[hidden] { display: none; }
.tabbar button {
  border: 0; background: none; display: grid; justify-items: center; gap: 2px; color: var(--muted);
  font-size: 11px; font-weight: 600; height: 100%; align-content: center;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.active { color: var(--brand); }
.tabbar .fab {
  width: 52px; height: 52px; border-radius: 50%; background: var(--brand); color: var(--brand-ink);
  justify-self: center; align-self: center; margin-top: -18px; box-shadow: 0 4px 14px rgba(14,159,110,.35);
}
.tabbar .fab svg { width: 26px; height: 26px; stroke-width: 2.4; }
.tabbar .fab:active { transform: scale(.94); }

/* Bottom sheet */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; }
.sheet-wrap[hidden] { display: none; }
.sheet-scrim { position: absolute; inset: 0; background: rgba(0,0,0,.4); animation: fade .2s ease; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; max-width: 560px; margin: 0 auto;
  background: var(--card); border-radius: 20px 20px 0 0; max-height: 92dvh; display: flex; flex-direction: column;
  animation: up .28s cubic-bezier(.2,.8,.2,1);
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px 16px; }
.sheet-head h2 { font-size: 18px; }
.sheet-body { overflow-y: auto; padding: 6px 16px 20px; -webkit-overflow-scrolling: touch; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sheet, .sheet-scrim { animation: none; } }

.tabs { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 12px; scrollbar-width: none; }
.tabs button {
  border: 1px solid var(--line); background: var(--bg); padding: 7px 12px; border-radius: 999px;
  font-size: 14px; font-weight: 600; white-space: nowrap; color: var(--muted);
}
.tabs button.active { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.search-row { display: flex; gap: 8px; margin-bottom: 8px; }
.search-row .input { flex: 1; }
.search-row .round-btn { width: 48px; height: 48px; border-radius: 12px; font-size: 18px; }
.section-label { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin: 14px 0 2px; }
.list-item .add { color: var(--brand); font-size: 24px; font-weight: 400; padding-left: 4px; }

.nut-grid { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; background: var(--bg); border-radius: 14px; padding: 12px 4px; margin-bottom: 16px; }
.nut-grid b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.nut-grid span { font-size: 12px; color: var(--muted); }

.scanner { position: relative; border-radius: 14px; overflow: hidden; background: #000; aspect-ratio: 4/3; margin-bottom: 12px; }
.scanner video { width: 100%; height: 100%; object-fit: cover; }
.scanner .reticle { position: absolute; inset: 30% 12%; border: 2px solid rgba(255,255,255,.85); border-radius: 10px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600;
  transition: opacity .2s, transform .2s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.spinner { width: 18px; height: 18px; border: 2px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: middle; margin-right: 8px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Meds & supplements */
.dose {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 0; text-align: left;
  background: none; border: 0; border-top: 1px solid var(--line);
}
.dose .info { flex: 1; min-width: 0; }
.dose .name { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dose .meta { font-size: 13px; color: var(--muted); }
.dose .check {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--line); flex: none;
  display: grid; place-items: center; transition: background .15s, border-color .15s;
}
.dose:active .check { transform: scale(.9); }
.dose.taken .check { background: var(--brand); border-color: var(--brand); }
.dose.taken .check::after { content: ''; width: 6px; height: 11px; border: solid var(--brand-ink); border-width: 0 2.5px 2.5px 0; transform: translateY(-1px) rotate(45deg); }
.dose.taken .name { color: var(--muted); text-decoration: line-through; }
.dose.due .check { border-color: var(--warn); }
.dose.due .meta, .dose.missed .meta { color: var(--warn); font-weight: 600; }
.chips { display: flex; gap: 4px; }
.chips label { flex: 1; min-width: 0; }
.chips input { position: absolute; opacity: 0; pointer-events: none; }
.chips span {
  display: block; padding: 8px 0; text-align: center; border-radius: 999px; border: 1px solid var(--line); background: var(--bg);
  font-size: 14px; font-weight: 600; color: var(--muted);
}
.chips input:checked + span { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.chips input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 2px; }
.time-row { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.time-row .input { flex: 1; }
div.field { display: grid; gap: 6px; margin-bottom: 14px; font-size: 14px; font-weight: 550; }
