:root {
  --bg: #F1F3EF;
  --surface: #FFFFFF;
  --ink: #172131;
  --muted: #5D6878;
  --line: #D9DED7;
  --fast: #2747D6;
  --fast-soft: #DDE3FB;
  --food: #1D7A52;
  --food-soft: #D8EDE2;
  --over: #B42A2A;
  --radius-s: 8px;
  --radius-l: 18px;
  --font: 'Archivo', 'Helvetica Neue', Arial, system-ui, sans-serif;
  color-scheme: light dark;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0E131B;
    --surface: #161D28;
    --ink: #E8ECF2;
    --muted: #97A1AF;
    --line: #273142;
    --fast: #8199FF;
    --fast-soft: #1E2A52;
    --food: #53C690;
    --food-soft: #173A2B;
    --over: #FF7B7B;
  }
}
:root[data-theme="dark"] {
  --bg: #0E131B; --surface: #161D28; --ink: #E8ECF2; --muted: #97A1AF; --line: #273142;
  --fast: #8199FF; --fast-soft: #1E2A52; --food: #53C690; --food-soft: #173A2B; --over: #FF7B7B;
}

*, *::before, *::after { box-sizing: inherit; }
html { scroll-padding-top: env(safe-area-inset-top, 0px); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--fast); outline-offset: 2px; }
[hidden] { display: none !important; }

main { max-width: 560px; margin: 0 auto; padding: 12px 20px 120px; }
.view > * + * { margin-top: 28px; }

h2 {
  font-size: 1.05rem;
  font-weight: 650;
  font-stretch: 115%;
  margin: 0 0 10px;
  letter-spacing: 0.005em;
}
.sub { color: var(--muted); font-size: 0.92rem; margin: 0; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- The clock: the one loud thing ---------- */
.clock { padding-top: 8px; }
.clock .date { color: var(--muted); font-size: 0.95rem; margin: 0 0 2px; }
.clock .big {
  font-size: clamp(96px, 30vw, 156px);
  line-height: 0.86;
  font-weight: 820;
  font-stretch: 62%;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  color: var(--fast);
  margin: 6px 0 10px;
}
.clock.eating .big { color: var(--food); }
.clock .big .unit { font-size: 0.34em; font-weight: 600; font-stretch: 75%; letter-spacing: 0; margin-left: 6px; color: var(--muted); }
.clock .status { font-size: 1.02rem; margin: 0 0 12px; }
.track { height: 10px; border-radius: 99px; background: var(--fast-soft); overflow: hidden; position: relative; }
.track > i { display: block; height: 100%; background: var(--fast); border-radius: inherit; transition: width .4s ease; }
.track.food { background: var(--food-soft); }
.track.food > i { background: var(--food); }
.track.over > i { background: var(--over); }
.track .goal-tick { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ink); opacity: .5; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 11px 18px;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  white-space: nowrap;
}
.btn.primary { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.btn.fast { background: var(--fast); border-color: var(--fast); color: #fff; }
.btn.food { background: var(--food); border-color: var(--food); color: #fff; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .btn.fast, :root:not([data-theme="light"]) .btn.food { color: #0E131B; } }
.btn.danger { color: var(--over); border-color: transparent; background: transparent; }
.btn.small { min-height: 34px; padding: 6px 12px; font-size: 0.9rem; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; }
.btn-row .btn { flex: 1 1 auto; }
.link { background: none; border: 0; padding: 0; color: var(--fast); font-weight: 600; cursor: pointer; }
.icon-btn { background: none; border: 0; font-size: 1.1rem; width: 40px; height: 40px; cursor: pointer; color: var(--muted); border-radius: 50%; }

/* ---------- Meters ---------- */
.meters { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.meter .label { color: var(--muted); font-size: 0.88rem; }
.meter .value { font-size: 1.9rem; font-weight: 750; font-stretch: 75%; line-height: 1.1; font-variant-numeric: tabular-nums; }
.meter .value small { font-size: 0.5em; font-weight: 500; color: var(--muted); font-stretch: 100%; }
.meter .track { margin-top: 6px; height: 6px; }

/* ---------- Lists ---------- */
.list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.list > li { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.list .grow { flex: 1; min-width: 0; }
.list .title { font-weight: 600; }
.list .meta { color: var(--muted); font-size: 0.88rem; }
.list .right { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.list .ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { color: var(--muted); padding: 14px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }

.check-btn {
  width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--line); background: transparent;
  display: grid; place-items: center; cursor: pointer; flex: none; color: transparent; font-weight: 800;
}
.check-btn.on { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.check-btn.auto { background: var(--food); border-color: var(--food); color: #fff; }
.tag.extra { border-color: var(--fast); color: var(--fast); }
.tag { font-size: 0.78rem; color: var(--muted); border: 1px solid var(--line); border-radius: 99px; padding: 1px 8px; margin-left: 6px; white-space: nowrap; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.stat .v { font-size: 1.35rem; font-weight: 720; font-stretch: 80%; font-variant-numeric: tabular-nums; line-height: 1.15; }
.stat .k { color: var(--muted); font-size: 0.82rem; }

.hint { border-left: 3px solid var(--fast); padding: 4px 0 4px 12px; color: var(--ink); font-size: 0.95rem; }

/* ---------- Head rows ---------- */
.head-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.head-row h2 { margin: 0; }
.day-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.day-nav strong { font-stretch: 110%; font-weight: 650; }
.seg { display: inline-flex; border: 1.5px solid var(--line); border-radius: 99px; overflow: hidden; }
.seg button { background: none; border: 0; padding: 6px 12px; cursor: pointer; font-size: 0.9rem; }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--bg); }

.week-day h3 { font-size: 0.98rem; font-weight: 650; font-stretch: 110%; margin: 0 0 4px; display: flex; gap: 8px; align-items: baseline; }
.week-day h3 .d { color: var(--muted); font-weight: 450; font-stretch: 100%; font-size: 0.88rem; }
.week-day.today h3 { color: var(--fast); }
.week-day + .week-day { margin-top: 18px; }

/* ---------- Chips (recent foods) ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1.5px solid var(--line); background: var(--surface); border-radius: 99px; padding: 7px 12px;
  cursor: pointer; font-size: 0.92rem; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip.fav { border-color: var(--food); }

/* ---------- Charts ---------- */
.chart { width: 100%; overflow-x: auto; }
.chart svg { display: block; width: 100%; height: auto; }
.chart text { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.legend { display: flex; gap: 14px; color: var(--muted); font-size: 0.82rem; margin-top: 6px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 12px; height: 3px; vertical-align: middle; margin-right: 5px; border-radius: 2px; }

/* ---------- Forms ---------- */
label { display: block; font-size: 0.86rem; color: var(--muted); margin-top: 12px; }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; padding: 11px 12px; border: 1.5px solid var(--line);
  border-radius: var(--radius-s); background: var(--surface); color: var(--ink); font-size: 16px;
}
textarea { resize: vertical; }
label.check { display: flex; gap: 10px; align-items: center; color: var(--ink); }
label.check input { width: auto; margin: 0; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.inline-form { display: flex; gap: 8px; margin-top: 12px; }
.inline-form input { margin: 0; }
.servings { display: flex; align-items: flex-end; gap: 10px; }
.servings label { flex: 1; }
.step { width: 48px; height: 46px; border: 1.5px solid var(--line); background: var(--surface); border-radius: var(--radius-s); font-size: 1.3rem; cursor: pointer; }
.total { font-weight: 650; margin: 12px 0 0; min-height: 1.4em; font-variant-numeric: tabular-nums; }
details.more { margin-top: 10px; }
details.more summary { cursor: pointer; color: var(--fast); font-weight: 600; font-size: 0.92rem; }
.note { background: var(--fast-soft); padding: 10px 12px; border-radius: var(--radius-s); font-size: 0.92rem; margin: 8px 0 0; color: var(--ink); }
.err { color: var(--over); min-height: 1.2em; }
code, .mono { font-family: ui-monospace, 'SF Mono', Menlo, monospace; font-size: 0.85em; background: var(--fast-soft); padding: 1px 5px; border-radius: 4px; word-break: break-all; }
.steps-list { padding-left: 20px; margin: 8px 0 0; }
.steps-list li { margin-bottom: 6px; }

/* ---------- Dialog sheets ---------- */
dialog.sheet {
  border: 0; padding: 0; width: min(560px, 100%); max-width: 100%; max-height: 92vh; margin: auto auto 0;
  border-radius: var(--radius-l) var(--radius-l) 0 0; background: var(--bg); color: var(--ink);
}
@media (min-width: 640px) { dialog.sheet { margin: auto; border-radius: var(--radius-l); } }
dialog.sheet > form, dialog.sheet > .pad { padding: 8px 20px calc(24px + env(safe-area-inset-bottom, 0px)); }
dialog#dlg-scan { padding: 8px 20px calc(24px + env(safe-area-inset-bottom, 0px)); }
dialog::backdrop { background: rgb(10 14 20 / .45); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--bg); padding-top: 8px; z-index: 1; }
.sheet-head h2 { margin: 0; }
.sheet .btn.primary { width: 100%; margin-top: 18px; }
.sheet .btn-row .btn.primary { margin-top: 0; }
.scan-view { width: 100%; min-height: 220px; background: #000; border-radius: var(--radius-s); overflow: hidden; margin-top: 8px; }
.scan-view video { width: 100% !important; height: auto !important; }

/* ---------- Tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tabs button {
  background: none; border: 0; padding: 14px 0 12px; cursor: pointer; font-size: 0.86rem; font-weight: 600;
  font-stretch: 105%; color: var(--muted); position: relative;
}
.tabs button[aria-current="page"] { color: var(--ink); }
.tabs button[aria-current="page"]::before {
  content: ''; position: absolute; top: 0; left: 28%; right: 28%; height: 3px; background: var(--fast); border-radius: 0 0 3px 3px;
}

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login form { width: min(360px, 100%); }
.wordmark { font-size: 4.2rem; font-weight: 820; font-stretch: 62%; letter-spacing: -0.02em; margin: 0 0 12px; color: var(--fast); line-height: 1; }
.login .btn { width: 100%; margin-top: 16px; }

.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom, 0px)); transform: translate(-50%, 20px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 99px; font-size: 0.92rem;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50; max-width: calc(100% - 40px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 380px) {
  main { padding-left: 16px; padding-right: 16px; }
  .stats { grid-template-columns: 1fr 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* search results */
#search-results li { padding: 0; }
.result { display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px 0; background: none; border: 0; text-align: left; cursor: pointer; }
.result .grow, .result .right { display: flex; flex-direction: column; }
.result .right { align-items: flex-end; max-width: 42%; }
.result .right .meta { white-space: normal; text-align: right; }
.result .tag { margin-left: 0; }
#dlg-search .pad { min-height: 60vh; }
