:root {
  color-scheme: light;
  --bg: #f4f5f3;
  --surface: #fcfcfb;
  --surface-2: #eef0ec;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #74726c;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d5efeb;
  --pos: #006300;
  --danger: #c0352f;
  --warn-bg: #fff4d6;
  --warn-ink: #6b4a00;
  --info-bg: #e3eefb;
  --info-ink: #184f95;
  --series: #2a78d6;
  --series-soft: rgba(42, 120, 214, 0.12);
  --grid: #e1e0d9;
  --axis: #898781;
  --baseline: #c3c2b7;
  --macro-p: #2a78d6;
  --macro-c: #eda100;
  --macro-f: #e87ba4;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
  --tabbar-h: 64px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9a988f;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #2bb3a5;
    --accent-ink: #062a27;
    --accent-soft: #11332f;
    --pos: #0ca30c;
    --danger: #e66767;
    --warn-bg: #3a2e0c;
    --warn-ink: #f5d27a;
    --info-bg: #142a45;
    --info-ink: #9ec5f4;
    --series: #3987e5;
    --series-soft: rgba(57, 135, 229, 0.16);
    --grid: #2c2c2a;
    --axis: #898781;
    --baseline: #383835;
    --macro-p: #3987e5;
    --macro-c: #c98500;
    --macro-f: #d55181;
    --shadow: none;
  }
}
:root[data-theme='dark'] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #262624;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #9a988f;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #2bb3a5;
  --accent-ink: #062a27;
  --accent-soft: #11332f;
  --pos: #0ca30c;
  --danger: #e66767;
  --warn-bg: #3a2e0c;
  --warn-ink: #f5d27a;
  --info-bg: #142a45;
  --info-ink: #9ec5f4;
  --series: #3987e5;
  --series-soft: rgba(57, 135, 229, 0.16);
  --grid: #2c2c2a;
  --baseline: #383835;
  --macro-p: #3987e5;
  --macro-c: #c98500;
  --macro-f: #d55181;
  --shadow: none;
}

* { box-sizing: border-box; }
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;
}
body.no-scroll { overflow: hidden; }
a { color: var(--accent); }
h1, h2, h3, h4 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h3 { font-size: 1.05rem; }
h4 { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 16px 0 8px; }
p { margin: 0.5em 0; }
code { font-size: 0.85em; background: var(--surface-2); padding: 1px 5px; border-radius: 6px; word-break: break-all; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.block { display: block; }
.pos { color: var(--pos); }
.center { text-align: center; }
.boot, .center-screen { min-height: 100vh; display: grid; place-items: center; font-weight: 700; color: var(--muted); }

/* ---------- Layout ---------- */
.app { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); }
.page { max-width: 680px; margin: 0 auto; padding: max(16px, env(safe-area-inset-top)) 16px 16px; display: flex; flex-direction: column; gap: 12px; }
.page-head { padding: 4px 2px; }
.page-head p { margin: 2px 0 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.card.inset { box-shadow: none; background: var(--surface-2); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sticky-actions { position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 8px); background: var(--bg); padding: 8px 0; z-index: 5; }

/* ---------- Tab bar ---------- */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); display: flex; background: var(--surface); border-top: 1px solid var(--border); z-index: 20; }
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: 0.72rem; }
.tab-icon { font-size: 1.3rem; filter: grayscale(1); opacity: 0.7; }
.tab.active { color: var(--accent); font-weight: 600; }
.tab.active .tab-icon { filter: none; opacity: 1; }
@media (min-width: 760px) {
  .tabbar { max-width: 680px; margin: 0 auto 12px; border: 1px solid var(--border); border-radius: 20px; bottom: 0; }
}

/* ---------- Buttons & inputs ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 42px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font: inherit; font-weight: 600; text-decoration: none; cursor: pointer; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 34px; padding: 0 10px; font-size: 0.88rem; border-radius: 10px; }
.btn.big { min-height: 64px; font-size: 1rem; }
.btn.block { width: 100%; }
.icon-btn { border: 0; background: transparent; color: var(--text-2); font-size: 1.2rem; min-width: 40px; min-height: 40px; border-radius: 10px; cursor: pointer; }
.icon-btn.small { font-size: 0.95rem; min-width: 32px; min-height: 32px; }
.icon-btn:hover { background: var(--surface-2); }

input, select, textarea { width: 100%; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; min-height: 44px; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .icon-btn:focus-visible, .chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field-label { font-size: 0.82rem; font-weight: 600; color: var(--text-2); }
.field-hint { font-size: 0.78rem; color: var(--muted); }
.check { display: flex; gap: 8px; align-items: center; font-size: 0.92rem; }
.check input { width: auto; min-height: 0; }
.input-btn { display: flex; gap: 6px; }
.title-input { font-weight: 700; font-size: 1.05rem; border-color: transparent; background: var(--surface-2); }

.segmented { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.segmented button { flex: 1; white-space: nowrap; border: 0; background: transparent; color: var(--text-2); font: inherit; font-size: 0.9rem; font-weight: 600; padding: 8px 6px; border-radius: 9px; cursor: pointer; min-height: 38px; }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 0.88rem; cursor: pointer; min-height: 36px; }
.chip.active { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

/* ---------- Banners, errors, toasts ---------- */
.banner { display: flex; gap: 8px; align-items: center; justify-content: space-between; flex-wrap: wrap; padding: 10px 14px; border-radius: 12px; background: var(--warn-bg); color: var(--warn-ink); text-decoration: none; font-size: 0.92rem; }
.banner.info { background: var(--info-bg); color: var(--info-ink); }
.error-box { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); padding: 10px 12px; border-radius: 10px; font-size: 0.92rem; }
.empty { color: var(--muted); font-size: 0.92rem; margin: 4px 0; }
.toasts { position: fixed; left: 16px; right: 16px; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px); display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 50; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 0.92rem; max-width: 520px; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2); }
.toast.error { background: var(--danger); color: #fff; }

.spinner-wrap { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 16px; color: var(--muted); }
.spinner { width: 22px; height: 22px; border: 3px solid var(--surface-2); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Sheet (bottom modal) ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); display: flex; align-items: flex-end; justify-content: center; z-index: 40; }
.sheet { background: var(--surface); width: 100%; max-width: 680px; max-height: 92vh; border-radius: 20px 20px 0 0; display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 6px 18px; gap: 8px; }
.sheet-head h2 { font-size: 1.1rem; overflow: hidden; text-overflow: ellipsis; }
.sheet-body { padding: 6px 18px 14px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.sheet-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 10px 18px 14px; border-top: 1px solid var(--border); }
.sheet-foot .btn.primary { flex: 1; }
@media (min-width: 760px) { .sheet-backdrop { align-items: center; } .sheet { border-radius: 20px; } }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 28px 22px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-card .field { text-align: left; }
.logo { font-size: 2.6rem; }

/* ---------- Today ---------- */
.date-nav { display: flex; align-items: center; justify-content: space-between; }
.date-title { border: 0; background: transparent; color: var(--text); font: inherit; font-size: 1.3rem; font-weight: 700; text-transform: capitalize; cursor: pointer; }
.summary { flex-direction: row; align-items: center; justify-content: space-around; gap: 12px; flex-wrap: wrap; }
.summary-lines { display: flex; flex-direction: column; gap: 4px; min-width: 150px; }
.summary-lines div { display: flex; justify-content: space-between; gap: 16px; }
.summary-lines .sep { border-top: 1px solid var(--border); padding-top: 4px; margin-top: 2px; }
.ring { position: relative; flex: none; }
.ring-track { stroke: var(--surface-2); }
.ring-value { stroke: var(--accent); transition: stroke-dasharray 0.4s; }
.ring-value.over { stroke: var(--danger); }
.ring-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.big { font-size: 1.9rem; font-weight: 800; line-height: 1; }
.big.over { color: var(--danger); }
.macro-top { display: flex; justify-content: space-between; font-size: 0.88rem; margin-bottom: 4px; }
.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; background: var(--accent); transition: width 0.4s; }
.macro.p .bar-fill { background: var(--macro-p); }
.macro.c .bar-fill { background: var(--macro-c); }
.macro.f .bar-fill { background: var(--macro-f); }

.meal-head .segmented { flex: none; }
.meal-head .segmented button { padding: 6px 10px; min-height: 32px; }
.meal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.meal-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.entry { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; border: 0; border-top: 1px solid var(--border); background: transparent; color: var(--text); font: inherit; padding: 10px 2px; text-decoration: none; cursor: pointer; }
.entry.static { cursor: default; }
.entry-name { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow-wrap: anywhere; }
.entry-kcal { font-weight: 700; white-space: nowrap; }
.entry-icon { font-size: 1.3rem; width: 28px; text-align: center; }
.thumb { width: 40px; height: 40px; border-radius: 8px; object-fit: cover; flex: none; }

/* ---------- Add food ---------- */
.search-row { display: flex; gap: 6px; }
.search-row input { flex: 1; min-width: 0; }
.search-row .btn { flex: none; }
.photo-strip { display: flex; gap: 8px; overflow-x: auto; }
.photo-thumb { position: relative; flex: none; }
.photo-thumb img { width: 110px; height: 110px; object-fit: cover; border-radius: 12px; display: block; }
.photo-strip.small .photo-thumb img { width: 64px; height: 64px; }
.photo-thumb .icon-btn { position: absolute; top: 2px; right: 2px; background: rgba(0, 0, 0, 0.55); color: #fff; min-width: 28px; min-height: 28px; font-size: 0.85rem; }
.nutri-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.nutri-grid div { background: var(--surface-2); border-radius: 10px; padding: 8px 4px; display: flex; flex-direction: column; }
.nutri-grid strong { font-size: 1.1rem; }
.nutri-grid span { font-size: 0.75rem; color: var(--muted); }
.scanner { width: 100%; border-radius: 12px; background: #000; }

/* ---------- AI chat ---------- */
.chat { display: flex; flex-direction: column; gap: 10px; }
.bubble { max-width: 88%; padding: 10px 14px; border-radius: 16px; background: var(--surface); border: 1px solid var(--border); }
.bubble p { margin: 0.2em 0; white-space: pre-wrap; }
.bubble.user { align-self: flex-end; background: var(--accent-soft); border-color: transparent; }
.bubble.ai { align-self: flex-start; }
.bubble-images { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 4px; }
.bubble-images img { max-width: 100%; max-height: 260px; border-radius: 10px; display: block; }
.question { border-top: 1px solid var(--border); margin-top: 8px; padding-top: 8px; }
.items { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.items th { text-align: left; font-size: 0.75rem; color: var(--muted); font-weight: 600; padding: 4px; }
.items td { border-top: 1px solid var(--border); padding: 8px 4px; vertical-align: top; }
.items th:nth-child(2), .items th:nth-child(3), .items .num { text-align: right; white-space: nowrap; }
.items input.grams { width: 70px; min-height: 36px; padding: 6px; text-align: right; }
.items tfoot td { border-top: 2px solid var(--border); }
.conf { margin-left: 4px; letter-spacing: -1px; }
.log-options { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.composer { position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 8px); background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 8px; display: flex; flex-direction: column; gap: 6px; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08); z-index: 5; }
.composer-row { display: flex; gap: 6px; align-items: flex-end; }
.composer textarea { min-height: 44px; max-height: 140px; resize: none; }

/* ---------- Sport ---------- */
.sport-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.sport-tile { display: flex; align-items: center; gap: 10px; padding: 14px; border-radius: 14px; background: var(--surface); border: 1px solid var(--border); text-decoration: none; color: var(--text); font-weight: 600; }
.sport-icon { font-size: 1.6rem; }
.stats-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 10px; display: flex; flex-direction: column; gap: 2px; }
.exercise { border: 1px solid var(--border); border-radius: 12px; padding: 10px; display: flex; flex-direction: column; gap: 6px; }
.exercise-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.sets { display: flex; flex-direction: column; gap: 6px; }
.set-row { display: flex; align-items: center; gap: 8px; }
.set-num { width: 22px; height: 22px; border-radius: 50%; background: var(--surface-2); font-size: 0.78rem; display: grid; place-items: center; flex: none; }
.set-input { display: flex; align-items: center; gap: 4px; flex: 1; }
.set-input input { min-height: 40px; padding: 6px 8px; text-align: center; }
.set-input span { font-size: 0.8rem; color: var(--muted); }
.pick-list { display: flex; flex-direction: column; }
.pick { display: flex; justify-content: space-between; align-items: center; gap: 8px; text-align: left; border: 0; border-top: 1px solid var(--border); background: transparent; color: var(--text); font: inherit; padding: 10px 2px; cursor: pointer; }

/* ---------- Progress / charts ---------- */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 10px; display: flex; flex-direction: column; gap: 2px; }
.tile strong { font-size: 1.15rem; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; touch-action: pan-y; }
.chart-empty { padding: 30px 0; text-align: center; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--baseline); stroke-width: 1; }
.chart .axis { fill: var(--axis); font-size: 11px; }
.chart .ref-line { stroke: var(--pos); stroke-width: 1; }
.chart .series-line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .series-dot { fill: var(--series); stroke: var(--surface); stroke-width: 2; }
.chart .series-dot.hover { stroke-width: 3; }
.chart .direct-label { fill: var(--text); font-size: 12px; font-weight: 700; }
.chart .crosshair { stroke: var(--baseline); stroke-width: 1; }
.chart .bar { fill: var(--series); }
.chart .bar.hover { fill: color-mix(in srgb, var(--series) 75%, var(--text)); }
.chart .budget-tick { stroke: var(--text-2); stroke-width: 2; stroke-linecap: round; }
.tooltip { position: absolute; top: 4px; width: 170px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 6px 10px; font-size: 0.85rem; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12); pointer-events: none; }
.legend { display: flex; gap: 14px; font-size: 0.8rem; color: var(--text-2); margin-top: 4px; }
.swatch { display: inline-block; width: 10px; height: 10px; margin-right: 5px; vertical-align: -1px; }
.bar-swatch { background: var(--series); border-radius: 3px; }
.tick-swatch { height: 2px; width: 12px; background: var(--text-2); vertical-align: 3px; }

/* ---------- Settings ---------- */
.calc { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.calc div { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; display: flex; flex-direction: column; font-size: 0.85rem; }
.calc strong { font-size: 1rem; }
.steps { padding-left: 20px; display: flex; flex-direction: column; gap: 12px; margin: 0; }
.steps form { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }
details summary { cursor: pointer; }

@media (max-width: 400px) {
  .tiles { grid-template-columns: 1fr 1fr; }
  .nutri-grid strong { font-size: 1rem; }
}
