:root {
  color-scheme: dark;
  --page: #090b0e;
  --surface: #161a1e;
  --surface-2: #1e2329;
  --border: rgba(255, 255, 255, 0.08);
  --text: #ffffff;
  --text-2: #c3c6cc;
  --muted: #8a8f98;
  --grid: #262b31;
  --axis: #3a4048;
  /* Status (recovery zones): always paired with a text label. */
  --good: #16ec75;
  --warn: #ffd900;
  --bad: #ff2d42;
  /* Series */
  --strain: #0a9eff;
  --series: #3987e5;
  --sleep: #8fa3ff;
  /* Sleep depth: one hue, light (awake) to dark (deep); validated as an ordinal ramp. */
  --stage-awake: #b7d3f6;
  --stage-rem: #6da7ec;
  --stage-core: #2a78d6;
  --stage-deep: #184f95;
  --radius: 14px;
  font-family: system-ui, -apple-system, "PingFang SC", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--page); color: var(--text); }
body { min-height: 100vh; font-size: 14px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
button, select, input { font: inherit; color: inherit; }
a { color: var(--series); }

/* Shell */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 12px max(16px, calc((100vw - 1200px) / 2));
  background: rgba(9, 11, 14, 0.86); backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; letter-spacing: 0.02em; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--good); box-shadow: 0 0 12px var(--good); }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.tab {
  border: 0; background: none; padding: 7px 14px; border-radius: 999px;
  color: var(--muted); cursor: pointer; white-space: nowrap; font-weight: 600;
}
.tab:hover { color: var(--text-2); background: var(--surface); }
.tab[aria-selected="true"] { color: var(--text); background: var(--surface-2); }
.ghost-btn {
  border: 1px solid var(--border); background: none; color: var(--text-2);
  padding: 6px 12px; border-radius: 999px; cursor: pointer;
}
.ghost-btn:hover { background: var(--surface); color: var(--text); }

main { max-width: 1232px; margin: 0 auto; padding: 20px 16px 64px; }
.view[hidden] { display: none; }
.loading { opacity: 0.45; transition: opacity 0.2s; }

/* Filter row: one row, above everything it scopes */
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 16px; }
.segmented { display: inline-flex; background: var(--surface); border-radius: 999px; padding: 3px; }
.segmented button {
  border: 0; background: none; padding: 5px 12px; border-radius: 999px; cursor: pointer; color: var(--muted); font-weight: 600;
}
.segmented button[aria-pressed="true"] { background: var(--surface-2); color: var(--text); }
.day-nav { display: inline-flex; align-items: center; gap: 6px; }
.day-nav button { width: 32px; height: 32px; border-radius: 50%; border: 0; background: var(--surface); cursor: pointer; }
.day-nav button:disabled { opacity: 0.35; cursor: default; }
.day-nav input {
  background: var(--surface); border: 0; border-radius: 999px; padding: 6px 12px; color-scheme: dark;
}
.day-title { font-size: 20px; font-weight: 700; margin-right: 6px; }
.meta { color: var(--muted); font-size: 12px; }
select {
  background: var(--surface); border: 0; border-radius: 999px; padding: 6px 12px; cursor: pointer;
}

/* Cards & grid */
.grid { display: grid; gap: 16px; grid-template-columns: repeat(12, 1fr); }
.span-12 { grid-column: span 12; }
.span-8 { grid-column: span 8; }
.span-6 { grid-column: span 6; }
.span-4 { grid-column: span 4; }
@media (max-width: 900px) {
  .span-8, .span-6, .span-4 { grid-column: span 12; }
}
.card {
  background: var(--surface); border-radius: var(--radius); padding: 16px 18px;
  border: 1px solid var(--border); min-width: 0;
}
.card-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 12px; }
.card-title { font-weight: 650; font-size: 15px; }
.card-sub { color: var(--muted); font-size: 12px; }
.card-head .spacer { flex: 1; }
.link-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 12px; padding: 2px 4px; }
.link-btn:hover { color: var(--text); }
.empty { color: var(--muted); padding: 24px 0; text-align: center; }

/* Hero rings */
.rings { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.ring-card { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 18px 8px; }
.ring-wrap { position: relative; width: min(180px, 100%); aspect-ratio: 1; }
.ring-wrap svg { width: 100%; height: 100%; display: block; }
.ring-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring-value { font-size: clamp(30px, 6vw, 46px); font-weight: 700; letter-spacing: -0.02em; }
.ring-label { color: var(--muted); font-size: 12px; font-weight: 600; letter-spacing: 0.08em; }
.ring-caption { color: var(--text-2); font-size: 13px; text-align: center; min-height: 1.4em; }
.zone-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-2); }
.zone-chip i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
@media (max-width: 600px) {
  .rings { gap: 8px; }
  .ring-card { padding: 12px 4px; }
  .ring-caption { font-size: 11px; }
}

.alert {
  display: flex; gap: 12px; align-items: flex-start;
  background: rgba(255, 45, 66, 0.1); border: 1px solid rgba(255, 45, 66, 0.35);
  border-radius: var(--radius); padding: 12px 16px;
}
.alert strong { display: block; margin-bottom: 2px; }
.alert ul { margin: 4px 0 0; padding-left: 18px; color: var(--text-2); }

/* Stat tiles */
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
@media (max-width: 900px) { .tiles { grid-template-columns: repeat(2, 1fr); } }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; min-width: 0; }
.tile-label { color: var(--muted); font-size: 12px; font-weight: 600; }
.tile-value { font-size: 24px; font-weight: 650; margin-top: 2px; white-space: nowrap; }
.tile-value small { font-size: 13px; color: var(--muted); font-weight: 500; margin-left: 3px; }
.tile-delta { font-size: 12px; color: var(--text-2); display: flex; align-items: center; gap: 4px; min-height: 18px; }
.tile-delta .up-good, .tile-delta .down-good { color: var(--good); }
.tile-delta .up-bad, .tile-delta .down-bad { color: var(--warn); }
.tile svg.spark { display: block; width: 100%; height: 28px; margin-top: 6px; }

/* Charts */
.chart { display: block; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .base-line { stroke: var(--axis); stroke-width: 1; }
.chart .crosshair { stroke: var(--text-2); stroke-width: 1; pointer-events: none; }
.chart .hit { fill: transparent; cursor: crosshair; outline: none; }
.chart .bar-hit { fill: transparent; cursor: pointer; outline: none; }
.chart .bar-hit:focus-visible + .bar, .chart .bar-hit:hover + .bar, .chart g.bar-group:hover .bar { filter: brightness(1.25); }
.chart .band-label { fill: var(--text-2); font-size: 11px; }
.chart .end-label { fill: var(--text-2); font-size: 11px; font-weight: 600; }
.chart-box { position: relative; min-height: 40px; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 10px; color: var(--text-2); font-size: 12px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.legend i.line { height: 2px; width: 14px; border-radius: 1px; }

.tip {
  position: fixed; z-index: 100; pointer-events: none; display: none;
  background: #23292f; border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 10px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45); min-width: 120px; max-width: 260px;
}
.tip-title { color: var(--muted); font-size: 11px; margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.tip-row strong { font-variant-numeric: tabular-nums; }
.tip-row span:last-child { color: var(--text-2); font-size: 12px; }
.tip-key { width: 12px; height: 2px; border-radius: 1px; flex: none; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
td { padding: 8px 10px; border-bottom: 1px solid var(--grid); color: var(--text-2); white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr:last-child td { border-bottom: 0; }
td .muted { color: var(--muted); }

/* Workout list */
.workouts { display: flex; flex-direction: column; gap: 10px; }
.workout { display: flex; align-items: center; gap: 12px; padding: 10px 12px; background: var(--surface-2); border-radius: 10px; }
.workout-main { flex: 1; min-width: 0; }
.workout-name { font-weight: 600; }
.workout-meta { color: var(--muted); font-size: 12px; }
.workout-strain { font-size: 20px; font-weight: 650; }
.workout-strain small { display: block; font-size: 11px; color: var(--muted); font-weight: 500; text-align: right; }

/* Live */
.live-hero { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 28px; margin-bottom: 12px; }
.live-hr { font-size: 72px; font-weight: 700; line-height: 1; letter-spacing: -0.03em; }
.live-hr small { font-size: 16px; color: var(--muted); font-weight: 500; margin-left: 4px; letter-spacing: 0; }
.live-stat .v { font-size: 26px; font-weight: 650; }
.live-stat .l { color: var(--muted); font-size: 12px; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--text-2); }
.pill.live::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--bad); animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) { .pill.live::before { animation: none; } }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(360px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 28px; display: flex; flex-direction: column; gap: 14px; }
.login h1 { margin: 0 0 4px; font-size: 22px; display: flex; align-items: center; gap: 10px; }
.login p { margin: 0; color: var(--muted); }
.login input { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 11px 12px; }
.login input:focus { outline: 2px solid var(--series); outline-offset: 1px; }
.login button { border: 0; border-radius: 10px; padding: 11px; background: var(--text); color: #000; font-weight: 650; cursor: pointer; }
.login button:disabled { opacity: 0.6; }
.login .error { color: var(--bad); font-size: 13px; min-height: 1.2em; }

@media (max-width: 600px) {
  .topbar { gap: 10px; }
  .brand { font-size: 15px; }
  .tab { padding: 6px 10px; }
  .ghost-btn { padding: 5px 10px; }
}

/* Advice */
.advice-head { display: flex; gap: 12px; align-items: stretch; }
.advice-mark { width: 4px; border-radius: 2px; flex: none; }
.advice-title { font-size: 19px; font-weight: 700; }
.advice-detail { color: var(--text-2); margin-top: 2px; }
.advice-list { list-style: none; margin: 14px 0 0; padding: 12px 0 0; border-top: 1px solid var(--grid); display: grid; gap: 8px; }
.advice-list li { display: flex; gap: 10px; align-items: baseline; color: var(--text-2); }
.advice-list li i { width: 8px; height: 8px; border-radius: 50%; flex: none; transform: translateY(-1px); }

/* Period comparison */
.compare { margin-bottom: 16px; }
.section-label { color: var(--muted); font-size: 12px; font-weight: 600; margin: 0 0 8px 2px; }
.tiles-5 { grid-template-columns: repeat(5, 1fr); }
@media (max-width: 900px) { .tiles-5 { grid-template-columns: repeat(2, 1fr); } }

/* Insights */
.finding { margin: 12px 0 0; color: var(--text-2); font-size: 13px; }
