:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --wash: rgba(11, 11, 11, 0.05);
  --good: #006300;
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --div-neg: #2a78d6;
  --div-pos: #e34948;
  --div-mid: #f0efec;
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --wash: rgba(255, 255, 255, 0.06);
    --good: #0ca30c;
    --s1: #3987e5;
    --s2: #d95926;
    --s3: #199e70;
    --div-neg: #3987e5;
    --div-pos: #e66767;
    --div-mid: #383835;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --wash: rgba(255, 255, 255, 0.06);
  --good: #0ca30c;
  --s1: #3987e5;
  --s2: #d95926;
  --s3: #199e70;
  --div-neg: #3987e5;
  --div-pos: #e66767;
  --div-mid: #383835;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 15px/1.5 var(--font);
}
a { color: inherit; }
h1, h2 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 26px; }
h2 { font-size: 15px; }
.muted { color: var(--muted); margin: 0; font-size: 13px; }

.page { max-width: 1180px; margin: 0 auto; padding: 32px 24px 48px; }

/* Header + filters: one row above everything they scope */
.top { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 24px; }
.sub { margin: 4px 0 0; color: var(--ink-2); font-size: 14px; }
.controls { display: flex; gap: 8px; align-items: center; }
.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 3px; }
.seg button {
  font: inherit; font-size: 13px; font-weight: 500; color: var(--ink-2);
  background: none; border: 0; border-radius: 999px; padding: 6px 12px; cursor: pointer;
}
.seg button:hover { background: var(--wash); }
.seg button[aria-checked="true"] { background: var(--ink); color: var(--surface); }
.icon-btn {
  display: grid; place-items: center; width: 36px; height: 36px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink-2); cursor: pointer;
}
.icon-btn:hover { background: var(--wash); }
button:focus-visible, .chart svg:focus-visible, summary:focus-visible { outline: 2px solid var(--s1); outline-offset: 2px; }

main { transition: opacity .2s; }
main.dim { opacity: .55; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  min-width: 0;
}
.label { font-size: 13px; font-weight: 500; color: var(--ink-2); }

/* KPIs */
.kpis { display: grid; grid-template-columns: minmax(240px, 1fr) 3fr; gap: 16px; margin-bottom: 16px; }
.hero { display: flex; flex-direction: column; gap: 4px; }
.hero-value { font-size: 56px; font-weight: 600; line-height: 1.05; letter-spacing: -0.03em; margin-top: 8px; }
.hero-value small { font-size: 20px; font-weight: 500; color: var(--ink-2); margin-left: 6px; letter-spacing: 0; }
.delta { margin: 4px 0 0; font-size: 14px; color: var(--ink-2); font-weight: 500; }
.delta.good { color: var(--good); }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 16px; }
.tile .value { font-size: 28px; font-weight: 600; letter-spacing: -0.02em; margin-top: 6px; line-height: 1.15; }
.tile .value small { font-size: 14px; font-weight: 500; color: var(--ink-2); margin-left: 3px; }
.tile .delta { font-size: 13px; }
.meter { height: 6px; border-radius: 3px; background: color-mix(in oklab, var(--s1) 18%, var(--surface)); margin-top: 12px; overflow: hidden; }
.meter i { display: block; height: 100%; border-radius: 3px; background: var(--s1); }

/* Grid of chart cards */
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.span-2 { grid-column: span 2; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head .muted { margin-top: 2px; }

.legend { list-style: none; margin: 0; padding: 0; display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); }
.legend li { display: flex; align-items: center; gap: 6px; }
.key { display: inline-block; }
.key.rect { width: 10px; height: 10px; border-radius: 3px; }
.key.line { width: 16px; height: 2px; border-radius: 1px; }
.key.dot { width: 8px; height: 8px; border-radius: 50%; }
.key.s1 { background: var(--s1); }
.key.s2 { background: var(--s2); }
.key.s3 { background: var(--s3); }
.key.s1-soft { background: color-mix(in oklab, var(--s1) 60%, var(--surface)); }
.key.goal { background: var(--ink-2); }

.chart { position: relative; height: 240px; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart svg:focus { outline: none; }
.axis text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.gridline { stroke: var(--grid); stroke-width: 1; }
.baseline { stroke: var(--axis); stroke-width: 1; }
.goal-line { stroke: var(--ink-2); stroke-width: 1.5; stroke-dasharray: 4 4; }
.goal-label { fill: var(--ink-2); font-size: 11px; font-weight: 500; paint-order: stroke; stroke: var(--surface); stroke-width: 4px; }
.crosshair { stroke: var(--axis); stroke-width: 1; }
.empty { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: 14px; }

/* Macro split bar */
.split { display: flex; gap: 2px; height: 28px; margin-bottom: 16px; }
.split div { border-radius: 4px; display: flex; align-items: center; padding: 0 8px; font-size: 12px; font-weight: 600; color: #fff; white-space: nowrap; min-width: 0; overflow: visible; }
.split div span { overflow: hidden; text-overflow: clip; }

/* Horizontal bars (meals, foods) */
.hbars { display: grid; gap: 12px; }
.hbar { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; align-items: baseline; font-size: 14px; }
.hbar .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .val { font-weight: 600; font-variant-numeric: tabular-nums; }
.hbar .val small { color: var(--muted); font-weight: 400; margin-left: 4px; }
.hbar .track { grid-column: 1 / -1; height: 8px; }
.hbar .track i { display: block; height: 100%; background: var(--s1); border-radius: 0 4px 4px 0; min-width: 2px; }

/* Heatmap */
.heatmap svg { display: block; width: 100%; height: auto; }
.heatmap rect.none { fill: none; stroke: var(--grid); stroke-width: 1; }
.heatmap rect.cell:hover { stroke: var(--ink); stroke-width: 1.5; }
.scale { display: flex; align-items: center; gap: 3px; font-size: 12px; color: var(--muted); }
.scale span { margin: 0 4px; }
.scale i { width: 12px; height: 12px; border-radius: 3px; }
.scale i.none { border: 1px solid var(--grid); margin-left: 10px; }
.d-n3 { background: var(--div-neg); }
.d-n1 { background: color-mix(in oklab, var(--div-neg) 40%, var(--div-mid)); }
.d-0 { background: var(--div-mid); }
.d-p1 { background: color-mix(in oklab, var(--div-pos) 40%, var(--div-mid)); }
.d-p3 { background: var(--div-pos); }

/* Table view */
.table-card summary { cursor: pointer; display: flex; gap: 12px; align-items: baseline; flex-wrap: wrap; list-style: none; }
.table-card summary::-webkit-details-marker { display: none; }
.table-card summary h2::before { content: "▸ "; color: var(--muted); }
.table-card[open] summary h2::before { content: "▾ "; }
.table-wrap { overflow-x: auto; margin-top: 16px; max-height: 480px; overflow-y: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th { position: sticky; top: 0; background: var(--surface); color: var(--ink-2); font-weight: 600; }

/* Tooltip */
.tt {
  position: fixed; z-index: 10; pointer-events: none;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .12);
  padding: 8px 12px; font-size: 13px; min-width: 140px;
}
.tt .t { color: var(--ink-2); font-size: 12px; margin-bottom: 4px; }
.tt .row { display: flex; align-items: center; gap: 8px; }
.tt .row b { font-weight: 600; font-variant-numeric: tabular-nums; }
.tt .row span { color: var(--ink-2); }
.tt .k { width: 12px; height: 2px; border-radius: 1px; flex: none; }

/* Clickable date in table */
.date-link { color: var(--s1); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.date-link:hover { text-decoration-thickness: 2px; }

/* Modal overlay */
.modal-overlay {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center;
  background: rgba(0,0,0,.45); backdrop-filter: blur(4px);
}
.modal-overlay[hidden] { display: none; }
body.modal-open { overflow: hidden; }
.modal-content {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 16px 48px rgba(0,0,0,.2);
  width: min(420px, calc(100vw - 32px)); max-height: calc(100vh - 64px);
  display: flex; flex-direction: column; overflow: hidden;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px 12px; }
.modal-head h2 { font-size: 17px; margin: 0; }
.modal-close {
  font: inherit; font-size: 22px; line-height: 1; background: none; border: 0;
  color: var(--muted); cursor: pointer; padding: 4px 8px; border-radius: 6px;
}
.modal-close:hover { background: var(--wash); color: var(--ink); }
.modal-tabs {
  display: flex; gap: 0; padding: 0 20px; border-bottom: 1px solid var(--grid);
}
.modal-tabs button {
  font: inherit; font-size: 13px; font-weight: 500;
  background: none; border: 0; border-bottom: 2px solid transparent;
  color: var(--ink-2); cursor: pointer; padding: 8px 14px;
}
.modal-tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--s1); }
.modal-tabs button:hover { color: var(--ink); }
.tab-count { color: var(--muted); font-weight: 400; }
.modal-body { padding: 12px 20px 20px; overflow-y: auto; flex: 1; }
.modal-food { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--grid); font-size: 14px; }
.modal-food:last-child { border-bottom: 0; }
.modal-food .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.modal-food .val { white-space: nowrap; font-weight: 600; font-variant-numeric: tabular-nums; flex: none; }
.modal-food.total { border-top: 2px solid var(--axis); border-bottom: 0; margin-top: 4px; padding-top: 12px; font-weight: 600; }
.empty-tab { color: var(--muted); font-size: 14px; text-align: center; padding: 24px 0; }

.foot { margin-top: 32px; font-size: 13px; color: var(--muted); text-align: center; }

@media (max-width: 820px) {
  .kpis { grid-template-columns: 1fr; }
  .grid { grid-template-columns: minmax(0, 1fr); }
  .span-2 { grid-column: auto; }
}
@media (max-width: 520px) {
  .page { padding: 20px 16px 32px; }
  h1 { font-size: 22px; }
  .hero-value { font-size: 48px; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .tile .value { font-size: 22px; }
  .tiles .tile:last-child:nth-child(odd) { grid-column: span 2; }
  .card { padding: 16px; }
  .chart { height: 200px; }
  .seg button { padding: 6px 10px; }
}

/* SVG marks */
.mark-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.mark-line.s1 { stroke: var(--s1); }
.mark-dot { stroke: var(--surface); stroke-width: 2; }
.mark-dot.s1-soft { fill: color-mix(in oklab, var(--s1) 60%, var(--surface)); }
.mark-bar.s1 { fill: var(--s1); }
.mark-bar.s2 { fill: var(--s2); }
.mark-bar.s3 { fill: var(--s3); }
.mark-bar.hover { filter: brightness(1.12); }
.band-hl { fill: var(--wash); }
.focus-dot { fill: var(--s1); stroke: var(--surface); stroke-width: 2; }

/* Tooltip keys mirror the marks: line for lines, dot for dots, rect for bars */
.tt .k.s1 { background: var(--s1); }
.tt .k.s2 { background: var(--s2); }
.tt .k.s3 { background: var(--s3); }
.tt .k.rect { width: 10px; height: 10px; border-radius: 3px; }
.tt .k.dot { width: 8px; height: 8px; border-radius: 50%; }
.tt .k.s1-soft { background: color-mix(in oklab, var(--s1) 60%, var(--surface)); }
