/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --bg: #eef1f6;
  --surface: #ffffff;
  --surface-2: #f3f5f9;
  --ink: #0f1319;
  --ink-2: #4b5563;
  --ink-3: #7a8291;
  --line: #d9dee8;
  --line-soft: #e6eaf1;
  --accent: #1d5fd6;
  --accent-fill: #1d5fd6;
  --accent-soft: rgba(29, 95, 214, 0.12);
  --accent-ink: #ffffff;
  --kcal: #d9822b;
  --kcal-soft: rgba(217, 130, 43, 0.14);
  --good: #2a8a4a;
  --warn: #c98a1a;
  --bad: #c9402b;
  --bad-soft: rgba(201, 64, 43, 0.12);
  --shadow: 0 10px 30px rgba(15, 19, 25, 0.12);
  --radius: 14px;
  --font-body: "Karla", "Segoe UI", system-ui, sans-serif;
  --font-display: "Fraunces", "Georgia", serif;
  --ease-out: cubic-bezier(0.2, 0, 0, 1);
  --ease-in: cubic-bezier(0.4, 0, 1, 1);
  --kb: 0px; /* on-screen keyboard height, set from JS */
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0a0c10;
    --surface: #13161c;
    --surface-2: #1b1f27;
    --ink: #e8ecf3;
    --ink-2: #a7b0bf;
    --ink-3: #78828f;
    --line: #262b35;
    --line-soft: #1e232b;
    --accent: #7aaaff;
    --accent-fill: #2f6fe4;
    --accent-soft: rgba(122, 170, 255, 0.16);
    --accent-ink: #ffffff;
    --kcal: #e89a4a;
    --kcal-soft: rgba(232, 154, 74, 0.18);
    --good: #5cc37b;
    --warn: #e0a93a;
    --bad: #e8634f;
    --bad-soft: rgba(232, 99, 79, 0.16);
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a0c10;
  --surface: #13161c;
  --surface-2: #1b1f27;
  --ink: #e8ecf3;
  --ink-2: #a7b0bf;
  --ink-3: #78828f;
  --line: #262b35;
  --line-soft: #1e232b;
  --accent: #7aaaff;
  --accent-fill: #2f6fe4;
  --accent-soft: rgba(122, 170, 255, 0.16);
  --accent-ink: #ffffff;
  --kcal: #e89a4a;
  --kcal-soft: rgba(232, 154, 74, 0.18);
  --good: #5cc37b;
  --warn: #e0a93a;
  --bad: #e8634f;
  --bad-soft: rgba(232, 99, 79, 0.16);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.4;
  overscroll-behavior-y: contain;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
svg { display: block; }
h1, h2, h3 { margin: 0; text-wrap: balance; }
p { margin: 0; }
.muted { color: var(--ink-2); }
.small { font-size: 13px; }
.tnum { font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- app frame ---------- */
#app {
  max-width: 540px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
#topbar {
  padding: calc(14px + env(safe-area-inset-top)) 18px 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 52px;
}
#topbar h1 {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 26px;
  letter-spacing: -0.01em;
}
.daynav { display: flex; align-items: center; gap: 4px; }
.daynav h1 { font-size: 22px; }
.iconbtn {
  width: 40px; height: 40px;
  border: 0; background: transparent; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--ink-2);
}
@media (hover: hover) { .iconbtn:hover { background: var(--surface-2); } }
.iconbtn:disabled { opacity: 0.3; cursor: default; }

/* press feedback: fast in, gentle out; transform and colour only */
.btn, .act, .chip, .iconbtn, #nav button, .link {
  transition: transform 160ms var(--ease-out), background-color 120ms ease-out, color 120ms ease-out, opacity 120ms ease-out;
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
}
.btn:active:not(:disabled), .act:active:not(:disabled), .chip:active { transform: scale(0.97); transition-duration: 50ms; }
.iconbtn:active:not(:disabled) { transform: scale(0.9); transition-duration: 50ms; }
#nav button svg { transition: transform 160ms var(--ease-out); }
#nav button:active svg { transform: scale(0.88); transition-duration: 50ms; }
.entry, tr[data-action] { transition: background-color 120ms ease-out; }
.entry:active, tr[data-action]:active { background: var(--surface-2); transition-duration: 0ms; }
@media (prefers-reduced-motion: reduce) {
  .btn:active, .act:active, .chip:active, .iconbtn:active, #nav button:active svg { transform: none; }
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.iconbtn svg { width: 22px; height: 22px; }

#view {
  flex: 1;
  padding: 6px 16px calc(150px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}
#view:focus { outline: none; }

#nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  margin: 0 auto;
  max-width: 540px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 20;
}
#nav button {
  border: 0; background: transparent;
  padding: 10px 4px 8px;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  color: var(--ink-3);
  font-size: 11.5px; font-weight: 600; letter-spacing: 0.02em;
}
#nav button svg { width: 24px; height: 24px; }
#nav button[aria-current="page"] { color: var(--accent); }

#actions {
  position: fixed;
  left: 0; right: 0;
  margin: 0 auto;
  max-width: 540px;
  bottom: calc(72px + env(safe-area-inset-bottom));
  padding: 0 16px;
  display: grid;
  grid-template-columns: 1fr 1.5fr 1fr;
  gap: 10px;
  z-index: 21;
}
.act {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-2);
  box-shadow: var(--shadow);
  min-height: 52px;
  padding: 8px 10px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font-weight: 700; font-size: 14px;
  white-space: nowrap;
}
.act svg { width: 22px; height: 22px; flex: none; }
.act-primary { background: var(--accent-fill); color: var(--accent-ink); border-color: transparent; font-size: 15px; }

/* ---------- cards & lists ---------- */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  border: 1px solid var(--line-soft);
}
.card-title {
  font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--ink-3);
  margin-bottom: 10px;
}
.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.stack { display: flex; flex-direction: column; gap: 10px; }

.hero { text-align: left; padding: 18px 18px 16px; }
.hero-num {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 58px;
  line-height: 1;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.hero-num.over { color: var(--bad); }
.hero-label { font-size: 15px; color: var(--ink-2); margin-top: 4px; }
.bar {
  height: 10px; border-radius: 999px; background: var(--surface-2);
  overflow: hidden; margin-top: 14px; position: relative; isolation: isolate;
}
/* full-width fill slid into view with a transform, so the bar never re-lays out */
.bar > i { display: block; width: 100%; height: 100%; background: var(--kcal); border-radius: 999px; transform: translateX(-100%); transition: transform 500ms var(--ease-out); }
.bar > i.over { background: var(--bad); }
@media (prefers-reduced-motion: reduce) { .bar > i { transition: none; } }
.macros { display: flex; gap: 14px; margin-top: 12px; flex-wrap: wrap; }
.macro { display: flex; align-items: baseline; gap: 5px; font-size: 14px; color: var(--ink-2); }
.macro b { color: var(--ink); font-size: 16px; font-variant-numeric: tabular-nums; }

.entries { display: flex; flex-direction: column; }
.entry {
  display: grid; grid-template-columns: 52px 1fr auto; gap: 12px; align-items: center;
  padding: 10px 0; border-top: 1px solid var(--line-soft);
  background: transparent; border-left: 0; border-right: 0; border-bottom: 0;
  text-align: left; width: 100%;
  position: relative; isolation: isolate;
}
@media (prefers-reduced-motion: no-preference) {
  .entry.is-new { animation: row-in 250ms var(--ease-out) 150ms both; }
  .entry.is-new::before {
    content: ""; position: absolute; inset: 2px -8px; border-radius: 10px;
    background: var(--accent-soft); z-index: -1;
    animation: row-flash 900ms var(--ease-out) 300ms both;
  }
  @keyframes row-in { from { opacity: 0; transform: translateY(-6px); } }
  @keyframes row-flash { from { opacity: 1; } to { opacity: 0; } }
}
.entry:first-child { border-top: 0; }
.entry .thumb {
  width: 52px; height: 52px; border-radius: 10px; object-fit: cover; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; color: var(--ink-3);
}
.entry .thumb svg { width: 22px; height: 22px; }
.entry .name { font-weight: 600; }
.entry .meta { font-size: 13px; color: var(--ink-3); }
.entry .kcal { font-weight: 700; font-variant-numeric: tabular-nums; font-size: 17px; }
.entry .kcal small { font-weight: 500; color: var(--ink-3); font-size: 12px; margin-left: 2px; }

.empty { text-align: center; padding: 26px 12px; color: var(--ink-2); }
.empty svg { width: 36px; height: 36px; margin: 0 auto 8px; color: var(--ink-3); }

/* tiles */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile { background: var(--surface); border: 1px solid var(--line-soft); border-radius: var(--radius); padding: 12px 14px; }
.tile .t-label { font-size: 12px; color: var(--ink-3); font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; }
.tile .t-val { font-family: var(--font-display); font-size: 26px; font-weight: 500; line-height: 1.15; margin-top: 4px; font-variant-numeric: tabular-nums; }
.tile .t-sub { font-size: 12.5px; color: var(--ink-2); margin-top: 2px; }
.t-val.good { color: var(--good); }
.t-val.bad { color: var(--bad); }

/* chips */
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--surface); color: var(--ink-2);
  border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 600;
}
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.pill.good { background: rgba(42, 138, 74, 0.14); color: var(--good); }
.pill.warn { background: rgba(201, 138, 26, 0.16); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.neutral { background: var(--surface-2); color: var(--ink-2); }

/* tables */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; font-size: 11.5px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-3); font-weight: 700; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.table td { padding: 8px 4px; border-bottom: 1px solid var(--line-soft); font-variant-numeric: tabular-nums; vertical-align: middle; }
.table td.num, .table th.num { text-align: right; }
.table tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }

/* ---------- forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field .hint { font-size: 12.5px; color: var(--ink-3); }
input[type="text"], input[type="number"], input[type="password"], input[type="date"], input[type="time"], select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 16px;
}
textarea { min-height: 72px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.inline { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.inline-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.unit-input { display: flex; align-items: center; gap: 8px; }
.unit-input input { flex: 1; }
.unit-input span { color: var(--ink-3); font-weight: 600; }
.btn {
  border: 0; border-radius: 12px; padding: 13px 16px; font-weight: 700; font-size: 15px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--surface-2); color: var(--ink);
}
.btn svg { width: 20px; height: 20px; }
.btn-primary { background: var(--accent-fill); color: var(--accent-ink); }
.btn-danger { background: var(--bad-soft); color: var(--bad); }
.btn-ghost { background: transparent; color: var(--ink-2); }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }
.form-error { color: var(--bad); font-size: 14px; font-weight: 600; }
.link { background: none; border: 0; color: var(--accent); font-weight: 700; padding: 0; }

/* ---------- login ---------- */
#login { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 10px; text-align: center; }
.login-card h1 { font-family: var(--font-display); font-size: 34px; font-weight: 500; }
.login-mark { width: 72px; height: 72px; border-radius: 20px; background: #1e56d6; margin: 0 auto 6px; padding: 10px; }
.login-card form { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }

/* ---------- sheet ---------- */
#sheet-overlay {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: flex-end; justify-content: center;
}
#sheet-backdrop { position: absolute; inset: 0; background: rgba(6, 8, 12, 0.5); }
#sheet {
  position: relative;
  width: 100%; max-width: 540px;
  max-height: 92dvh;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 8px 18px calc(18px + env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
  will-change: transform;
  /* solid bleed below the sheet so no gap shows while it slides or is dragged */
  box-shadow: 0 80px 0 0 var(--surface), var(--shadow);
}
.sheet-handle, .sheet-head { touch-action: none; }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 4px auto 14px; position: relative; }
.sheet-handle::before { content: ""; position: absolute; inset: -14px -40px; }
/* on-screen keyboard: keep the sheet's bottom above it */
.kb-open #sheet-overlay { bottom: var(--kb); }
.kb-open #sheet { padding-bottom: 18px; max-height: calc(100% - 12px); }
#sheet { scroll-padding-bottom: 88px; }
.sheet-title { font-family: var(--font-display); font-size: 24px; font-weight: 500; margin-bottom: 12px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet-head .sheet-title { margin: 0; }
#sheet-body { display: flex; flex-direction: column; gap: 14px; }

.preview { width: 100%; max-height: 260px; object-fit: cover; border-radius: 14px; background: var(--surface-2); }
.scale-hint { display: flex; gap: 10px; align-items: center; background: var(--accent-soft); color: var(--accent); border-radius: 12px; padding: 10px 12px; font-size: 14px; font-weight: 600; }
.scale-hint svg { width: 22px; height: 22px; flex: none; }

.items { display: flex; flex-direction: column; }
.item { display: grid; grid-template-columns: 1fr auto; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line-soft); }
.item:first-child { border-top: 0; }
.item .i-name { font-weight: 600; }
.item .i-portion { font-size: 13px; color: var(--ink-3); }
.item .i-kcal { font-weight: 700; font-variant-numeric: tabular-nums; }
.item .i-macro { font-size: 12px; color: var(--ink-3); text-align: right; }

/* voice capture */
.listen { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 22px 10px 14px; text-align: center; }
.listen-ring {
  position: relative; width: 84px; height: 84px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
}
.listen-ring svg { width: 34px; height: 34px; }
.listen-ring i { position: absolute; inset: 0; border-radius: 50%; border: 2px solid var(--accent); opacity: 0.35; }
@media (prefers-reduced-motion: no-preference) {
  .listen-ring i { animation: ring 1.4s var(--ease-out) infinite; }
  @keyframes ring { from { transform: scale(0.85); opacity: 0.7; } to { transform: scale(1.4); opacity: 0; } }
}
.listen-text { font-family: var(--font-display); font-size: 24px; font-weight: 500; }
.said { font-family: var(--font-display); font-size: 20px; line-height: 1.3; padding: 2px 2px 6px; }
.thinking { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 30px 10px; color: var(--ink-2); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2s; } }

/* ---------- charts ---------- */
.chart { position: relative; width: 100%; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; touch-action: pan-y; }
/* draw-in: a surface-coloured curtain slides off the plot area on the compositor */
.chart-curtain { position: absolute; top: 0; background: var(--surface); transform-origin: right center; pointer-events: none; will-change: transform; }
@media (prefers-reduced-motion: no-preference) {
  .chart-curtain { animation: curtain 450ms var(--ease-out) both; }
  @keyframes curtain { from { transform: scaleX(1); } to { transform: scaleX(0); } }
}
.chart .grid line { stroke: var(--line-soft); stroke-width: 1; }
.chart .axis text { fill: var(--ink-3); font-size: 11px; font-family: var(--font-body); }
.chart .ref { stroke: var(--ink-3); stroke-width: 1; }
.chart .ref-label { fill: var(--ink-3); font-size: 11px; font-weight: 600; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--accent); opacity: 0.1; }
.chart .dot { fill: var(--accent); opacity: 0.55; }
.chart .dot-end { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .bar { fill: var(--kcal); }
.chart .bar.over { fill: var(--bad); }
.chart .bar.dim { opacity: 0.35; }
.chart .hair { stroke: var(--ink-2); stroke-width: 1; pointer-events: none; }
.chart .hit { fill: transparent; }
.tooltip {
  position: absolute; top: 0; left: 0;
  transform: translate(-50%, -110%);
  background: var(--ink); color: var(--bg);
  padding: 6px 9px; border-radius: 8px; font-size: 12.5px; font-weight: 600;
  pointer-events: none; white-space: nowrap; z-index: 2;
}
.tooltip small { display: block; font-weight: 500; opacity: 0.75; }
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--ink-2); flex-wrap: wrap; margin-top: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 3px; border-radius: 2px; background: var(--accent); display: inline-block; }
.legend i.dot { width: 8px; height: 8px; border-radius: 50%; opacity: 0.55; }
.legend i.kcal { background: var(--kcal); width: 10px; height: 10px; border-radius: 3px; }
.legend i.bad { background: var(--bad); width: 10px; height: 10px; border-radius: 3px; }
.legend i.ref { background: var(--ink-3); height: 1px; }

/* ---------- misc ---------- */
/* #toast is a permanent live region; the card inside is what shows and animates */
#toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(146px + env(safe-area-inset-bottom));
  display: flex; justify-content: center; pointer-events: none; z-index: 40;
}
body:has(#actions[hidden]) #toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
.kb-open #toast { bottom: calc(16px + var(--kb)); }
.toast-card {
  pointer-events: auto;
  background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: 14px;
  max-width: 100%; text-align: center; box-shadow: var(--shadow);
}
.toast-card.error { background: var(--bad); color: #fff; }
.toast-card button { background: none; border: 0; color: inherit; font-weight: 800; text-decoration: underline; margin-left: 8px; }
#offline {
  position: fixed; top: env(safe-area-inset-top); left: 0; right: 0; text-align: center;
  background: var(--warn); color: #1a1400; font-size: 12.5px; font-weight: 700; padding: 4px; z-index: 50;
}
.settings-list .row { padding: 12px 0; border-top: 1px solid var(--line-soft); }
.settings-list .row:first-child { border-top: 0; }
.status-ok { color: var(--good); font-weight: 700; }
.status-bad { color: var(--bad); font-weight: 700; }
code { font-family: ui-monospace, Consolas, monospace; font-size: 13px; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
