/* ---------- theme tokens ---------- */
:root {
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --border: #e1e0d9;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --ink-3: #75746f;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --series: #2a78d6;
  --band: rgba(12, 163, 12, 0.07);
  --good: #0ca30c;
  --high: #ec835a;
  --low: #d03b3b;
  --noise: #e07b00;
  --warn-bg: #fff4dc;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #242423; --border: #2c2c2a;
    --ink: #f0efec; --ink-2: #c3c2b7; --ink-3: #8f8e86; --grid: #2c2c2a; --axis: #383835;
    --series: #3987e5; --band: rgba(12, 163, 12, 0.10); --warn-bg: #3a2e12;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #242423; --border: #2c2c2a;
  --ink: #f0efec; --ink-2: #c3c2b7; --ink-3: #8f8e86; --grid: #2c2c2a; --axis: #383835;
  --series: #3987e5; --band: rgba(12, 163, 12, 0.10); --warn-bg: #3a2e12;
  --shadow: none; color-scheme: dark;
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--ink-2); }

.ghost {
  background: transparent; border: 1px solid var(--border); border-radius: 8px;
  padding: 5px 11px; color: var(--ink-2);
}
.ghost:hover { background: var(--surface-2); color: var(--ink); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow); padding: 18px 20px;
}

/* ---------- layout ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.topbar-actions form { margin: 0; }
.container { max-width: 1100px; margin: 0 auto; padding: 20px; display: grid; gap: 16px; }

/* ---------- current value ---------- */
.current { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 28px; }
.current-main { display: flex; align-items: baseline; gap: 10px; }
.cur-value { font-size: 64px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -1px; }
.cur-arrow { font-size: 40px; line-height: 1; color: var(--ink-2); }
.cur-unit { color: var(--ink-2); font-size: 16px; }
.current-meta { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; }
.status-chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px 3px 8px;
  border: 1px solid var(--border); border-radius: 999px; color: var(--ink); font-weight: 550; font-size: 14px;
}
.status-chip::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--ink-3); }
.status-chip.good::before { background: var(--good); }
.status-chip.high::before { background: var(--high); }
.status-chip.low::before  { background: var(--low); }
.stale { width: 100%; background: var(--warn-bg); border-radius: 8px; padding: 8px 12px; font-size: 14px; }
.cur-value.stale-value { color: var(--ink-3); text-decoration: line-through; text-decoration-thickness: 2px; }
/* sensor noise (Dexcom sends no usable trend), shown like the TTGO: orange value + label */
.cur-value.noisy { color: var(--noise); }
.noise-chip {
  display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 999px;
  background: var(--noise); color: #fff; font-weight: 650; font-size: 14px;
}

/* ---------- chart ---------- */
.chart-card { position: relative; }
.chart-toolbar { display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.segmented { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; }
.segmented button { border: 0; background: transparent; padding: 4px 11px; border-radius: 7px; color: var(--ink-2); }
.segmented button:hover { color: var(--ink); }
.segmented button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }
.nav { display: flex; align-items: center; gap: 6px; }
.nav .ghost { padding: 4px 10px; }
.range-label { color: var(--ink-2); font-size: 13px; min-width: 150px; text-align: center; font-variant-numeric: tabular-nums; }

.chart { width: 100%; height: 360px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--ink-2); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .band { fill: var(--band); }
.chart .limit { stroke-width: 1; stroke-dasharray: 4 4; }
.chart .limit.high { stroke: var(--high); }
.chart .limit.low { stroke: var(--low); }
.chart .line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { stroke: var(--surface); stroke-width: 1.5; }
.chart .dot.good, .legend .good { fill: var(--good); background: var(--good); }
.chart .dot.high, .legend .high { fill: var(--high); background: var(--high); }
.chart .dot.low,  .legend .low  { fill: var(--low);  background: var(--low); }
.chart .other { stroke: var(--ink); stroke-width: 2; fill: var(--surface); }
.chart .noise { stroke: var(--noise); stroke-width: 2; stroke-linecap: round; }
.legend i.noise { width: auto; height: auto; font-style: normal; font-weight: 700; line-height: 1; color: var(--noise); }
.legend i.noise::before { content: "✕"; }
.noise-text { color: var(--noise); font-weight: 600; }
.chart .crosshair { stroke: var(--ink-3); stroke-width: 1; }
.chart .hover-dot { fill: var(--series); stroke: var(--surface); stroke-width: 2; }
.chart .empty { fill: var(--ink-3); font-size: 14px; }

.tooltip {
  position: absolute; pointer-events: none; background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,.15); white-space: nowrap; z-index: 3;
}
.tooltip b { font-size: 15px; font-variant-numeric: tabular-nums; }
.tooltip .tt-sub { color: var(--ink-2); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 8px; font-size: 13px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.legend i.line { width: 16px; height: 2px; border-radius: 1px; background: var(--series); }
.legend i.other { border: 2px solid var(--ink); background: var(--surface); width: 11px; height: 11px; }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 16px; }
.stat { padding: 14px 16px; }
.stat .label { color: var(--ink-2); font-size: 13px; }
.stat .value { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat .sub { color: var(--ink-2); font-size: 12px; }
.stat.tir { grid-column: span 2; }
.tir-bar { display: flex; height: 12px; gap: 2px; margin: 10px 0 6px; }
.tir-bar div { min-width: 0; }
.tir-bar div:first-child { border-radius: 4px 0 0 4px; }
.tir-bar div:last-child { border-radius: 0 4px 4px 0; }
.tir-bar div:only-child { border-radius: 4px; }
.tir-bar .low { background: var(--low); }
.tir-bar .good { background: var(--good); }
.tir-bar .high { background: var(--high); }
.tir-legend { display: flex; gap: 14px; font-size: 12px; color: var(--ink-2); flex-wrap: wrap; }
.tir-legend i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.tir-legend .low { background: var(--low); }
.tir-legend .good { background: var(--good); }
.tir-legend .high { background: var(--high); }

/* ---------- table ---------- */
.table-card summary { cursor: pointer; font-weight: 600; }
.table-wrap { max-height: 420px; overflow: auto; margin-top: 12px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--border); }
th { position: sticky; top: 0; background: var(--surface); color: var(--ink-2); font-weight: 550; }

/* ---------- login ---------- */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow); text-align: center;
}
.login-card h1 { margin: 8px 0 20px; font-size: 22px; }
.login-card form { display: grid; gap: 10px; text-align: left; }
.login-card label { font-size: 13px; color: var(--ink-2); }
.login-card input {
  padding: 10px 12px; border-radius: 9px; border: 1px solid var(--border); background: var(--page);
}
.login-card input:focus { outline: 2px solid var(--series); outline-offset: 1px; }
.login-card button[type=submit] {
  margin-top: 6px; padding: 10px; border: 0; border-radius: 9px; background: var(--series); color: #fff; font-weight: 600;
}
.login-card .error { color: var(--low); margin: 0; font-size: 14px; }
.login-card .hint { color: var(--ink-3); font-size: 12px; margin: 16px 0 0; }

@media (max-width: 640px) {
  .container { padding: 12px; gap: 12px; }
  .cur-value { font-size: 52px; }
  .chart { height: 280px; }
  .stat.tir { grid-column: span 1; }
  .range-label { min-width: 0; }
}
