:root {
  --bg: #f5f6f8; --card: #ffffff; --text: #1d2330; --muted: #626977; --line: #e6e8ec; --track: #e6e8ec;
  --accent: #2a6fdb; --accent-ink: #1f5bbf; --soft: #e8effc; --soft-2: #eef0f3; --input: #ffffff;
  --danger-bg: #fdecef; --danger-ink: #b42318; --shadow: rgba(0,0,0,.05);
  /* charts (validated with the dataviz palette checker) */
  --carb: #2a78d6; --protein: #eb6834; --fat: #1baf7a; --water: #4aa8ff; --grid: #e1e0d9;
  --under: #2a78d6; --overbar: #e34948;
  --good: #0ca30c; --warn: #fab219; --serious: #ec835a; --over: #d03b3b;
  --act0: #6cbf6c; --act1: #2e9e2e; --act2: #0f7f0f; --act3: #006300;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text); background: var(--bg);
}
/* dark: follows the phone unless chosen in the profile (data-theme) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
  --bg: #111113; --card: #1c1c1f; --text: #f0efec; --muted: #a8a8b0; --line: #2e2e33; --track: #2e2e33;
  --accent: #2f6fd0; --accent-ink: #8fb8f5; --soft: #22324d; --soft-2: #2a2a30; --input: #16161a;
  --danger-bg: #3a1d1d; --danger-ink: #ff9b8f; --shadow: rgba(0,0,0,.4);
  --carb: #3987e5; --protein: #d95926; --fat: #199e70; --water: #4aa8ff; --grid: #2c2c2a;
  --under: #3987e5; --overbar: #e66767;
  --good: #0ca30c; --warn: #fab219; --serious: #ec835a; --over: #d03b3b;
  --act0: #2a7a2a; --act1: #3a9e3a; --act2: #5cc25c; --act3: #8fe08f;
  color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111113; --card: #1c1c1f; --text: #f0efec; --muted: #a8a8b0; --line: #2e2e33; --track: #2e2e33;
  --accent: #2f6fd0; --accent-ink: #8fb8f5; --soft: #22324d; --soft-2: #2a2a30; --input: #16161a;
  --danger-bg: #3a1d1d; --danger-ink: #ff9b8f; --shadow: rgba(0,0,0,.4);
  --carb: #3987e5; --protein: #d95926; --fat: #199e70; --water: #4aa8ff; --grid: #2c2c2a;
  --under: #3987e5; --overbar: #e66767;
  --good: #0ca30c; --warn: #fab219; --serious: #ec835a; --over: #d03b3b;
  --act0: #2a7a2a; --act1: #3a9e3a; --act2: #5cc25c; --act3: #8fe08f;
  color-scheme: dark;
}
body { background: var(--bg); color: var(--text); }
* { box-sizing: border-box; }
body { margin: 0; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
main { max-width: 560px; margin: 0 auto; padding: 12px 16px; }
h1, h2, h3 { margin: 0; }
h2 { font-size: 1.05rem; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
.btn { background: var(--accent); color: #fff; border-radius: 10px; padding: 10px 14px; font-weight: 600; }
.btn.ghost { background: var(--soft); color: var(--accent-ink); }
.btn.danger { background: var(--danger-bg); color: var(--danger-ink); }
.icon-btn { width: 36px; height: 36px; border-radius: 50%; background: var(--soft); color: var(--accent-ink); font-size: 1.3rem; line-height: 1; }
input, select { width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--input); }
label { display: block; font-size: .85rem; color: var(--muted); margin: 10px 0 4px; }
.row { display: flex; gap: 10px; align-items: center; }
.row > * { flex: 1; }
.card { background: var(--card); border-radius: 16px; padding: 14px; margin: 12px 0; box-shadow: 0 1px 3px var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: center; }
.muted { color: var(--muted); font-size: .85rem; }

.datebar { display: flex; justify-content: space-between; align-items: center; }
.datebar strong { font-size: 1.1rem; }

.summary { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; text-align: center; }
.summary .big { font-size: 1.3rem; font-weight: 700; }
.ring { width: 140px; height: 140px; }
.ring circle { fill: none; stroke-width: 10; }
.ring .track { stroke: var(--track); }
.ring .val { stroke-linecap: round; transform: rotate(-90deg); transform-origin: center; transition: stroke-dashoffset .4s; }
.ring text { text-anchor: middle; dominant-baseline: middle; fill: var(--text); }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 12px; }
.bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin: 4px 0; }
.bar i { display: block; height: 100%; border-radius: 3px; }

.item { display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); width: 100%; text-align: left; }
.item:first-of-type { border-top: 0; }
.item .name { font-weight: 500; }
.item .kcal { white-space: nowrap; }

.water { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.glass { width: 34px; height: 42px; border: 2px solid var(--water); border-radius: 4px 4px 8px 8px; background: transparent; }
.glass.full { background: var(--water); }

.tabs { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.tabs button { flex: 1; padding: 14px 0; color: var(--muted); font-weight: 600; }
.tabs button.active { color: var(--accent-ink); }

dialog { border: 0; border-radius: 18px 18px 0 0; padding: 0; width: 100%; max-width: 560px; max-height: 90vh; margin: auto auto 0; background: var(--card); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.35); }
#sheet-body { padding: 16px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.seg { display: flex; gap: 6px; margin: 10px 0; overflow-x: auto; }
.seg button { padding: 6px 12px; border-radius: 99px; background: var(--soft-2); white-space: nowrap; }
.seg button.active { background: var(--accent); color: #fff; }
.results { max-height: 50vh; overflow-y: auto; }
video { width: 100%; border-radius: 12px; background: #000; }
.chart { width: 100%; height: 180px; }
.chart polyline { fill: none; stroke: var(--accent); stroke-width: 2; }
.chart .goal { stroke: var(--muted); stroke-dasharray: 4 4; }
.chart text { font-size: 10px; fill: var(--muted); }

/* nutrition detail */
.kcal-big { text-align: center; margin: 16px 0 10px; }
.kcal-big b { font-size: 2.2rem; }
.kcal-big small { display: block; color: var(--muted); }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile { background: var(--bg); border-radius: 12px; padding: 10px 8px; text-align: center; display: flex; flex-direction: column; gap: 2px; }
.tile i { width: 22px; height: 4px; border-radius: 2px; margin: 0 auto 4px; }
.tile b { font-size: 1.1rem; }
.tile span { font-size: .8rem; }
.tile small { color: var(--muted); font-size: .72rem; }
table.nutr { width: 100%; border-collapse: collapse; margin: 14px 0; font-size: .9rem; }
table.nutr td, table.nutr th { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: right; }
table.nutr td:first-child, table.nutr th:first-child { text-align: left; }
table.nutr tbody th { text-align: left; padding-top: 14px; font-size: .95rem; }
table.nutr thead th { color: var(--muted); font-weight: 500; }
tr.sub td:first-child { padding-left: 16px; color: var(--muted); }
textarea { width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 10px; font: inherit; }
.item .icon-btn { width: 28px; height: 28px; font-size: 1rem; margin-left: 6px; vertical-align: middle; }

/* statistics */
.stat-tiles { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; }
.stat-tiles b { display: block; font-size: 1.3rem; }
.stat-tiles span { color: var(--muted); font-size: .8rem; }
.stat-caption { font-size: .85rem; margin: 10px 0 0; text-align: center; }
.barchart { width: 100%; height: auto; display: block; margin-top: 6px; }
.barchart .gridline { stroke: var(--grid); stroke-width: 1; }
.barchart .goalmark { stroke: var(--text); stroke-width: 2; stroke-dasharray: 3 2; }
.barchart .axis { font-size: 10px; fill: var(--muted); }
.barchart .axis.strong { fill: var(--text); font-weight: 700; }
.barchart g.dim path, .barchart g.dim rect:not([fill="transparent"]) { opacity: .35; }
.barchart rect[data-action] { cursor: pointer; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: .8rem; margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.table-scroll { overflow-x: auto; margin-top: 8px; }
table.week { border-collapse: collapse; font-size: .8rem; min-width: 100%; }
table.week th, table.week td { padding: 5px 6px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
table.week th:first-child, table.week td:first-child { text-align: left; position: sticky; left: 0; background: var(--card); }
table.week thead th { color: var(--muted); font-weight: 500; }
.datebar button[disabled] { opacity: .3; }
.tabs button { font-size: .85rem; }

/* week strip + calendar */
.weekstrip { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-top: 10px; }
.weekstrip button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 0; border-radius: 12px; }
.weekstrip small { color: var(--muted); font-size: .72rem; }
.weekstrip b { font-size: 1rem; }
.weekstrip i { width: 5px; height: 5px; border-radius: 50%; }
.weekstrip i.dot { background: var(--accent); }
.weekstrip .today b { color: var(--accent); }
.weekstrip .active { background: var(--accent); }
.weekstrip .active small, .weekstrip .active b { color: #fff; }
.weekstrip .active i.dot { background: #fff; }
.cal-btn { position: relative; display: inline-flex; align-items: center; justify-content: center; font-size: 1rem; }
.cal-btn input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; padding: 0; border: 0; cursor: pointer; }
.datebar strong { text-align: center; flex: 1; }

/* status labels: color never carries the meaning alone */
.status { display: inline-flex; align-items: center; gap: 5px; font-size: .78rem; color: var(--text); margin-top: 2px; }
.status i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.ring-sm { width: 76px; height: 76px; }
.ring-sm circle { fill: none; stroke-width: 7; }
.summary > div { display: flex; flex-direction: column; align-items: center; }
.weekstrip .today b { color: var(--accent-ink); }
.weekstrip i.dot { background: var(--accent-ink); }
/* coverage */
.cov { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; align-items: center; font-size: .85rem; margin-top: 8px; }
.cov .bar { grid-column: 1 / -1; height: 8px; margin: 0 0 6px; position: relative; overflow: visible; }
.cov .bar b { position: absolute; right: 0; top: -2px; bottom: -2px; width: 2px; background: var(--muted); }
.cov h3 { grid-column: 1 / -1; font-size: .9rem; margin-top: 10px; }
.trend-text { font-size: .9rem; margin: 8px 0 0; }
.chart .raw { fill: var(--muted); opacity: .55; }
.chart .trend { fill: none; stroke: var(--accent-ink); stroke-width: 2; }
.chart .forecast { fill: none; stroke: var(--accent-ink); stroke-width: 2; stroke-dasharray: 5 4; opacity: .8; }
.chart .zero { stroke: var(--muted); stroke-width: 1; }
.weekstrip .active small, .weekstrip .active b { color: #fff; }
.weekstrip .active i.dot { background: #fff; }
.train-badge { display: inline-block; font-size: .8rem; padding: 4px 10px; border-radius: 99px; background: var(--soft-2); margin-bottom: 6px; }
.train-badge.done { background: var(--soft); color: var(--accent-ink); }
