:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ed;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --border-strong: rgba(11, 11, 11, 0.18);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --wash: rgba(11, 11, 11, 0.04);

  /* categorical slots 1-3, validated all-pairs in both modes */
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --s-other: #b5b3ab;

  /* sleep stages */
  --stage-deep: #2a78d6;
  --stage-light: #008300;
  --stage-rem: #e87ba4;
  --stage-awake: #eda100;
  --stage-deep-ink: #ffffff;
  --stage-light-ink: #ffffff;
  --stage-rem-ink: #0b0b0b;
  --stage-awake-ink: #0b0b0b;

  /* heart rate zones: ordinal blue, darker = harder */
  --z1: #86b6ef; --z2: #5598e7; --z3: #2a78d6; --z4: #1c5cab; --z5: #104281;
  --z1-ink: #0b0b0b; --z2-ink: #0b0b0b; --z3-ink: #ffffff; --z4-ink: #ffffff; --z5-ink: #ffffff;

  --status-good: #0ca30c;
  --status-critical: #d03b3b;
  --good-text: #006300;
  --critical-text: #b42f2f;
  --warn-text: #8a5a00;

  --j-str: #0f6e3f;
  --j-num: #1c5cab;
  --j-bool: #7a3596;

  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --border-strong: rgba(255, 255, 255, 0.20);
    --accent: #3987e5;
    --accent-ink: #ffffff;
    --wash: rgba(255, 255, 255, 0.05);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s-other: #5c5b56;
    --stage-deep: #3987e5; --stage-light: #008300; --stage-rem: #d55181; --stage-awake: #c98500;
    --stage-deep-ink: #ffffff; --stage-light-ink: #ffffff; --stage-rem-ink: #ffffff; --stage-awake-ink: #0b0b0b;
    --z1: #184f95; --z2: #256abf; --z3: #3987e5; --z4: #6da7ec; --z5: #9ec5f4;
    --z1-ink: #ffffff; --z2-ink: #ffffff; --z3-ink: #ffffff; --z4-ink: #0b0b0b; --z5-ink: #0b0b0b;
    --good-text: #0ca30c;
    --critical-text: #e66767;
    --warn-text: #fab219;
    --j-str: #6fcf97; --j-num: #86b6ef; --j-bool: #d29bf0;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.20);
  --accent: #3987e5;
  --accent-ink: #ffffff;
  --wash: rgba(255, 255, 255, 0.05);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s-other: #5c5b56;
  --stage-deep: #3987e5; --stage-light: #008300; --stage-rem: #d55181; --stage-awake: #c98500;
  --stage-deep-ink: #ffffff; --stage-light-ink: #ffffff; --stage-rem-ink: #ffffff; --stage-awake-ink: #0b0b0b;
  --z1: #184f95; --z2: #256abf; --z3: #3987e5; --z4: #6da7ec; --z5: #9ec5f4;
  --z1-ink: #ffffff; --z2-ink: #ffffff; --z3-ink: #ffffff; --z4-ink: #0b0b0b; --z5-ink: #0b0b0b;
  --good-text: #0ca30c;
  --critical-text: #e66767;
  --warn-text: #fab219;
  --j-str: #6fcf97; --j-num: #86b6ef; --j-bool: #d29bf0;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 14px/1.45 var(--sans);
}
a { color: var(--accent); }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 24px; }
h3 { font-size: 15px; }
button, input, select { font: inherit; color: inherit; }

/* -- controls ----------------------------------------------------------------- */

button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
  cursor: pointer; text-decoration: none; color: var(--text); white-space: nowrap;
}
button:hover, .button:hover { background: var(--surface-2); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover, .button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.ghost, .button.ghost { background: transparent; }
button.ghost:hover, .button.ghost:hover { background: var(--wash); }
button.small, .button.small { min-height: 28px; padding: 0 10px; font-size: 13px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input, select {
  min-height: 34px; padding: 0 10px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
  min-width: 0;
}
input[type="date"] { padding-right: 6px; }

.chip {
  min-height: 28px; padding: 0 10px; border-radius: 999px; font-size: 12.5px;
  background: transparent; border: 1px solid var(--border-strong); color: var(--text-2);
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* -- frame --------------------------------------------------------------------- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; flex-direction: column; margin-right: auto; }
.brand-title { font-weight: 650; font-size: 15px; }
.brand-sub { color: var(--text-2); font-size: 12.5px; }
.session { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.session-detail { color: var(--text-2); font-size: 12.5px; }
.key-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.key-form input { width: 280px; max-width: 100%; }
.status-text { font-weight: 600; }
.status-text.good { color: var(--good-text); }
.status-text.critical, .critical { color: var(--critical-text); }

.layout { display: grid; grid-template-columns: 250px minmax(0, 1fr); align-items: start; }
.sidebar {
  position: sticky; top: 59px; max-height: calc(100vh - 59px); overflow: auto;
  padding: 16px 12px 24px; border-right: 1px solid var(--border);
}
.run-all { width: 100%; margin-bottom: 12px; }
.ep-group { margin-bottom: 14px; }
.ep-group-title { font-size: 11.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; padding: 0 8px 4px; }
.ep-list { display: flex; flex-direction: column; }
.ep-link {
  display: flex; align-items: center; gap: 9px; padding: 6px 8px; border-radius: 7px;
  color: var(--text); text-decoration: none;
}
.ep-link:hover { background: var(--wash); }
.ep-link[aria-current="page"] { background: var(--surface-2); font-weight: 600; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; border: 1.5px solid var(--axis); }
.dot.ok { background: var(--status-good); border-color: var(--status-good); }
.dot.err { background: var(--status-critical); border-color: var(--status-critical); }
.dot.loading { border-color: var(--accent); border-top-color: transparent; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.sidebar-foot { color: var(--muted); font-size: 12px; padding: 0 8px; }

main { padding: 28px 32px 80px; max-width: 1180px; outline: none; }

/* -- endpoint header and form -------------------------------------------------- */

.eyebrow { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.blurb { color: var(--text-2); margin: 4px 0 16px; max-width: 70ch; }

.request {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
.method { font: 600 11.5px var(--mono); color: var(--good-text); border: 1px solid currentColor; border-radius: 5px; padding: 1px 6px; }
.url { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.url .host { color: var(--muted); }
.curl { margin: 8px 0 0; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); white-space: pre-wrap; overflow-wrap: anywhere; }

.params { margin: 14px 0 20px; padding: 16px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.fields { display: flex; flex-wrap: wrap; gap: 12px 14px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 150px; }
.field.wide { flex: 1 1 420px; }
.field-label { font-size: 12px; color: var(--text-2); font-weight: 500; }
.presets { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.presets .field-label { margin-right: 2px; }
.note { color: var(--text-2); font-size: 12.5px; margin: 10px 0 0; }
.note.warn { color: var(--warn-text); }
.actions { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.hint { color: var(--muted); font-size: 12px; }

/* -- response ------------------------------------------------------------------ */

#response { transition: opacity 0.15s; }
#response.refreshing { opacity: 0.45; pointer-events: none; }
.placeholder { padding: 36px 20px; text-align: center; color: var(--text-2); border: 1px dashed var(--border-strong); border-radius: 12px; }
.status-bar { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 16px; color: var(--text-2); font-size: 13px; margin-bottom: 10px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tab { border: 0; border-radius: 0; background: transparent; min-height: 36px; padding: 0 14px; color: var(--text-2); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab:hover { background: transparent; color: var(--text); }
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

.error-panel { padding: 16px; border: 1px solid var(--border); border-left: 3px solid var(--status-critical); border-radius: 10px; background: var(--surface); }
.error-title { margin: 0 0 6px; font-weight: 600; }
.error-body { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text-2); }

.viz { display: flex; flex-direction: column; gap: 16px; }
.viz-section { padding: 16px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); min-width: 0; }
.viz-section > h3 + .sub, .viz-section > .sub { margin: 2px 0 12px; }
.viz-section > h3:last-child { margin-bottom: 0; }
.viz-section > h3 { margin-bottom: 12px; }
.viz-section > h3:has(+ .sub) { margin-bottom: 0; }
.sub { color: var(--text-2); font-size: 12.5px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.tile { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); min-width: 0; }
.tile-label { font-size: 12px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-value { font-size: 22px; font-weight: 600; margin-top: 2px; overflow-wrap: anywhere; }
.tile-value .unit { font-size: 13px; font-weight: 500; color: var(--text-2); }
.tile-sub { font-size: 12px; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere; }

.explorer-details > summary { cursor: pointer; list-style: none; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.explorer-details > summary::-webkit-details-marker { display: none; }
.explorer-details > summary::before { content: "▸"; color: var(--muted); font-size: 12px; transition: transform 0.15s; }
.explorer-details[open] > summary::before { transform: rotate(90deg); }
.explorer-controls { display: flex; flex-wrap: wrap; gap: 10px 16px; margin: 14px 0 10px; }
label.inline { display: inline-flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-2); }
label.inline select { max-width: min(420px, 70vw); }
.field-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; max-height: 168px; overflow: auto; }
.multiples { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(420px, 100%), 1fr)); gap: 14px; }
.multiple { margin: 0; min-width: 0; padding: 10px 10px 4px; border-radius: 10px; border: 1px solid var(--border); }
.multiple figcaption { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 6px; font-weight: 600; font-size: 13px; }
.multiple figcaption code { color: var(--muted); font-weight: 400; font-size: 11.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.multiples.three { grid-template-columns: repeat(auto-fill, minmax(min(300px, 100%), 1fr)); }

.table-wrap { overflow: auto; max-height: 560px; border: 1px solid var(--border); border-radius: 10px; }
.table-wrap.compact { max-height: none; }
table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
th, td { padding: 6px 10px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; color: var(--text-2); z-index: 1; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--wash); }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 10px; }
.panel-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.file-info { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.file-info p { margin: 0; color: var(--text-2); }

/* -- dashboard ----------------------------------------------------------------- */

.dash-controls { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 10px; margin: 0 0 18px; }
.dash-status { color: var(--text-2); font-size: 12.5px; }
.dash.refreshing { opacity: 0.45; pointer-events: none; transition: opacity 0.15s; }
.subhead { font-size: 13px; font-weight: 600; margin: 20px 0 8px; }
.split { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(380px, 100%), 1fr)); gap: 0 18px; align-items: start; }
.insights { margin: 14px 0 0; padding-left: 18px; color: var(--text-2); }
.insights.first { margin: 0 0 14px; }
.insights li { margin: 3px 0; }
.insights strong, .corr-lead strong { color: var(--text); font-weight: 600; }
.corr-lead { color: var(--text-2); margin: 0 0 10px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.chip-r { font-variant-numeric: tabular-nums; opacity: 0.75; }

/* -- JSON tree ----------------------------------------------------------------- */

.json { font: 12.5px/1.6 var(--mono); padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); overflow: auto; max-height: 70vh; }
.json details > summary { cursor: pointer; list-style: none; }
.json details > summary::-webkit-details-marker { display: none; }
.json details > summary::before { content: "▸ "; color: var(--muted); }
.json details[open] > summary::before { content: "▾ "; }
.j-children { padding-left: 18px; border-left: 1px solid var(--grid); margin-left: 4px; }
.j-line { white-space: pre-wrap; overflow-wrap: anywhere; padding-left: 14px; }
.j-key { color: var(--text-2); }
.j-str { color: var(--j-str); }
.j-num { color: var(--j-num); }
.j-bool { color: var(--j-bool); }
.j-null, .j-punct { color: var(--muted); }
.j-more { margin: 4px 0 4px 14px; }
.raw { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }

/* -- charts -------------------------------------------------------------------- */

.chart { position: relative; width: 100%; min-width: 0; }
.chart svg { display: block; overflow: visible; touch-action: pan-y; }
.chart svg:focus-visible { outline-offset: 4px; border-radius: 4px; }
.chart .grid { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .axis { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .row-label { fill: var(--text-2); font-size: 12px; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.faint { stroke-width: 1.5; opacity: 0.3; }
.chart .area { opacity: 0.1; stroke: none; }
.chart .band { fill: var(--text-2); opacity: 0.1; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .ref { stroke: var(--text-2); stroke-width: 1; opacity: 0.7; }
.chart .ref-label { fill: var(--text-2); font-size: 11px; }
.chart .goal { stroke: var(--text-2); stroke-width: 2; stroke-linecap: round; }
.chart .crosshair { stroke: var(--text-2); stroke-width: 1; opacity: 0.6; shape-rendering: crispEdges; pointer-events: none; }
.chart .wash { fill: var(--wash); pointer-events: none; }
.chart .end-label, .chart .value-label { fill: var(--text); font-size: 11.5px; font-weight: 600; font-variant-numeric: tabular-nums; }
.chart .inbar { font-size: 11.5px; font-weight: 600; pointer-events: none; }
.chart .seg:focus { outline: none; }
.chart .seg:focus rect, .chart .seg:hover rect { filter: brightness(1.1); }
.chart .route { stroke-width: 2.5; }
.chart .trend { stroke: var(--text-2); stroke-width: 1.5; stroke-dasharray: 5 4; }
.chart .axis-title { fill: var(--text-2); font-size: 11.5px; }
.chart .dot.scatter { fill-opacity: 0.75; }
.chart .empty { color: var(--muted); margin: 8px 0; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 0 0 8px; font-size: 12px; color: var(--text-2); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.swatch.line { width: 14px; height: 2px; border-radius: 1px; }
.swatch.faint { opacity: 0.35; }
.swatch.band { opacity: 0.2; }

.tip {
  position: absolute; z-index: 5; pointer-events: none; min-width: 120px;
  padding: 8px 10px; border-radius: 8px; font-size: 12px; white-space: nowrap;
  background: var(--surface); border: 1px solid var(--border-strong); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.14);
}
.tip-title { color: var(--text-2); margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 8px; }
.tip-row .key { width: 12px; height: 2px; border-radius: 1px; flex: none; }
.tip-row strong { font-variant-numeric: tabular-nums; }
.tip-name { color: var(--text-2); }

/* -- narrow screens ------------------------------------------------------------ */

@media (max-width: 820px) {
  .topbar { position: static; padding: 10px 16px; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--border); padding: 12px 16px; }
  .ep-list { flex-direction: row; flex-wrap: wrap; gap: 2px; }
  .ep-link { padding: 5px 8px; }
  .sidebar-foot { display: none; }
  main { padding: 20px 16px 64px; }
  .key-form input { width: 100%; }
  .field { flex: 1 1 140px; }
}

/* -- run streak ---------------------------------------------------------------- */

.streak-hero { display: flex; align-items: flex-end; flex-wrap: wrap; gap: 8px 24px; margin: 0 0 16px; }
.streak-number { display: flex; align-items: baseline; gap: 8px; }
.streak-days { font-size: 56px; font-weight: 650; line-height: 1; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.streak-unit { font-size: 18px; font-weight: 500; color: var(--text-2); }
.streak-lines { display: flex; flex-direction: column; gap: 2px; padding-bottom: 4px; color: var(--text-2); min-width: 0; }
.streak-today { font-weight: 600; color: var(--text); }
.streak-today.good { color: var(--good-text); }
.cal-scroll { overflow-x: auto; padding: 2px 0 4px; }
.chart .cal-day { fill: var(--surface-2); }
.chart .cal-day.outline { fill: none; stroke: var(--text-2); stroke-width: 1.5; }
.chart .cal-today { fill: none; stroke: var(--text); stroke-width: 1.5; pointer-events: none; }
.chart .cal-focus { fill: none; stroke: var(--accent); stroke-width: 2; pointer-events: none; }
.swatch.outline { border: 1.5px solid var(--text-2); }
.parts { white-space: normal; display: inline-block; min-width: 220px; }
.subhead + .sub { margin: -4px 0 8px; }
.parts .part { white-space: nowrap; }
.parts .part-note { color: var(--warn-text); }
