/* Amazing's CryNet — the caregiver's record.
 *
 * Two themes, one identity: the teal of the hub's own glass.  Light mode
 * keeps the warm nursery surface; dark mode is the hub panel's near-black
 * teal, its exact values from hub_display.c (C_BG, C_SURFACE, C_SURFACE_HI,
 * C_TEXT, C_DIM, C_ACCENT).  The page is deliberately quiet so the data is
 * the only loud thing on it.
 *
 * Colour roles come from the validated reference palette.  Slot 1 and the
 * sequential ramp are teal — the hub's accent hue — in place of the
 * reference blue; the other seven slots are unchanged.  Both modes were
 * re-run through the palette validator against their own surfaces:
 *   light (#fcfcfb): lightness PASS · chroma PASS · adjacent CVD PASS
 *     (worst 9.1) · normal-vision PASS (worst 19.6) · contrast WARN on
 *     aqua/yellow/magenta -> the relief channel is shipped: every chart
 *     has a table twin and every cot mark carries a written "Cot n" label.
 *   dark (#070f10): all PASS — worst adjacent CVD 8.4, normal-vision 19.3.
 * Dark slot 1 is #23a893, the nearest passing step to the hub accent
 * #2dd4bf, which is too light for the data band; the hub accent itself is
 * kept for chrome (brand mark, tab underline, focus ring), where the band
 * does not apply.  The theme is stamped on <html data-theme> before first
 * paint by the inline script in index.html, so there is no flash.
 */

:root {
  --radius: 14px;
  --radius-sm: 9px;
  --gap: 16px;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

:root,
[data-theme="light"] {
  color-scheme: light;

  /* surfaces & ink */
  --plane: #f9f9f7;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --border-strong: rgba(11, 11, 11, 0.18);
  --wash: rgba(11, 11, 11, 0.045);

  /* categorical slots — fixed order, assigned by cot, never re-ranked */
  --series-1: #0d9488;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
  --series-other: #898781;

  /* sequential ramp — one hue (teal), light -> dark (magnitude only) */
  --seq-100: #bce1de;
  --seq-150: #a4d6d2;
  --seq-200: #8bcbc7;
  --seq-250: #73c0bb;
  --seq-300: #5bb4af;
  --seq-350: #43a9a3;
  --seq-400: #2b9e97;
  --seq-450: #0d9488;
  --seq-500: #0b7e74;
  --seq-550: #09685f;

  /* status — reserved, always shipped with an icon and a word */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;

  /* chrome accent — the hub's teal, a touch deeper on the light surface */
  --accent: #0d9488;
}

[data-theme="dark"] {
  color-scheme: dark;

  /* the hub panel's own values (hub_display.c) */
  --plane: #020404;          /* C_BG        (2, 4, 4)   */
  --surface-1: #070f10;      /* C_SURFACE   (7, 15, 16) */
  --surface-2: #0c1a1c;      /* C_SURFACE_HI (12, 26, 28) */
  --text-primary: #e4f0ec;   /* C_TEXT      (228, 240, 236) */
  --text-secondary: #7a9692; /* C_DIM       (122, 150, 146) */
  --text-muted: #5f7a76;
  --grid: #182626;
  --axis: #344a48;
  --border: rgba(228, 240, 236, 0.12);
  --border-strong: rgba(228, 240, 236, 0.22);
  --wash: rgba(228, 240, 236, 0.05);

  /* the same eight hues, stepped for the dark surface (validated) */
  --series-1: #23a893;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
  --series-other: #7a9692;

  /* sequential ramp — one hue (teal), dim -> bright on the dark ground;
   * the top step is the hub accent itself */
  --seq-100: #0f2f2c;
  --seq-150: #12413c;
  --seq-200: #16544d;
  --seq-250: #19665d;
  --seq-300: #1c786d;
  --seq-350: #208a7e;
  --seq-400: #239d8e;
  --seq-450: #26af9e;
  --seq-500: #2ac1af;
  --seq-550: #2dd4bf;

  /* status — the same four steps; all clear 3:1 on this darker surface */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;

  /* chrome accent — the hub accent, exactly (C_ACCENT 45, 212, 191) */
  --accent: #2dd4bf;
}

* { box-sizing: border-box; }

/* The `hidden` attribute has to beat every `display` set below it.
 * A class rule like `.control { display: flex }` outranks the browser's
 * own `[hidden] { display: none }`, so anything toggled with `hidden`
 * would stay on the page.  This one line makes `hidden` mean hidden
 * everywhere. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--plane);
  color: var(--text-primary);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

.app {
  max-width: 1180px;
  margin: 0 auto;
  padding: 22px 20px 56px;
}

/* ---------------------------------------------------------------- header */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 10px 16px;
  margin-bottom: 18px;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 9px;
  font-size: 19px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.brand .mark {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  align-self: center;
  flex: none;
}

.brand .sub {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-secondary);
}

.hubmeta {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
  font-size: 13px;
  flex: 1 1 auto;
  min-width: 0;
}

/* The hub's own state: green while it is talking to the dashboard,
 * amber once it has gone quiet.  The word beside it carries the state
 * too, so nothing here depends on the colour being seen. */
.hubmeta-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex: none;
}

.hubmeta[data-state="on"] .hubmeta-dot { background: var(--good); }
.hubmeta[data-state="off"] .hubmeta-dot { background: var(--warning); }

.conn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 11px 4px 9px;
  white-space: nowrap;
}

.conn .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--good);
  flex: none;
}

.conn[data-state="stale"] .dot { background: var(--warning); }
.conn[data-state="loading"] .dot { background: var(--axis); }

/* ------------------------------------------------------- tabs & controls */

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 14px;
}

.tab {
  appearance: none;
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  padding: 9px 14px;
  font: inherit;
  font-weight: 550;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 8px 8px 0 0;
}

.tab:hover { background: var(--wash); }
.tab[aria-selected="true"] {
  color: var(--text-primary);
  border-bottom-color: var(--accent);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  margin-bottom: 18px;
}

.control { display: flex; align-items: center; gap: 8px; }

.control > label,
.control-label {
  font-size: 12.5px;
  color: var(--text-secondary);
}

.segmented {
  display: inline-flex;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}

.segmented button {
  appearance: none;
  border: 0;
  background: none;
  font: inherit;
  font-size: 13px;
  color: var(--text-secondary);
  padding: 4px 12px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}

.segmented button:hover { background: var(--wash); }
.segmented button[aria-pressed="true"] {
  background: var(--surface-2);
  color: var(--text-primary);
  font-weight: 600;
}

select {
  appearance: none;
  font: inherit;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 30px 5px 13px;
  cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-secondary) 50%),
                    linear-gradient(135deg, var(--text-secondary) 50%, transparent 50%);
  background-position: calc(100% - 16px) 51%, calc(100% - 11px) 51%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

button:focus-visible,
select:focus-visible,
svg:focus-visible,
a:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* The theme toggle: moon while light (click for dark), sun while dark. */
.theme-toggle {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text-secondary);
  cursor: pointer;
  flex: none;
  align-self: center;
}

.theme-toggle:hover { color: var(--accent); background: var(--wash); }
.theme-toggle .ico { width: 15px; height: 15px; display: block; }
.theme-toggle .ico-sun { display: none; }
[data-theme="dark"] .theme-toggle .ico-sun { display: block; }
[data-theme="dark"] .theme-toggle .ico-moon { display: none; }

.scope-note {
  font-size: 12.5px;
  color: var(--text-secondary);
  margin: 0 0 14px;
}

/* ------------------------------------------------------------ stat tiles */

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(158px, 1fr));
  gap: var(--gap);
  margin-bottom: var(--gap);
}

.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px 13px;
  min-width: 0;
}

.tile .label {
  font-size: 12.5px;
  color: var(--text-secondary);
  margin-bottom: 3px;
}

.tile .value {
  font-size: 26px;
  font-weight: 620;
  letter-spacing: -0.015em;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.tile .sub {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 3px;
}

.tile.hero { grid-column: span 2; }
.tile.hero .value { font-size: 48px; letter-spacing: -0.02em; }
.tile.hero .spark { margin-top: 8px; display: block; }

@media (max-width: 520px) {
  .tile.hero { grid-column: span 2; }
  .tile.hero .value { font-size: 40px; }
  .kpis { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .tile { padding: 12px 13px; }
  .tile .value { font-size: 22px; }
}

/* small figures inside a card (gaps, confidence) — not nested cards */
.figures {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(112px, 1fr));
  gap: 12px 14px;
}
.figure .label { font-size: 12.5px; color: var(--text-secondary); }
.figure .value { font-size: 22px; font-weight: 620; letter-spacing: -0.01em; }
.figure .sub { font-size: 12px; color: var(--text-secondary); }

/* ---------------------------------------------------------------- system */

.system-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: var(--gap);
  font-size: 13px;
}

.system-strip .strip-label {
  color: var(--text-secondary);
  font-size: 12.5px;
  margin-right: 2px;
}

.health-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px 3px 8px;
  background: var(--plane);
  color: var(--text-primary);
  white-space: nowrap;
}

.health-chip .icon {
  width: 14px;
  height: 14px;
  flex: none;
  display: block;
}

.health-chip .when { color: var(--text-secondary); font-size: 12px; }

.linkish {
  appearance: none;
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  font-size: 12.5px;
  color: var(--text-secondary);
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
  margin-left: auto;
}

.linkish:hover { color: var(--text-primary); }

/* ----------------------------------------------------------------- cards */

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--gap);
  align-items: start;
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 15px 17px 16px;
  min-width: 0;
}

.card.span2 { grid-column: 1 / -1; }

.card-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 4px;
}

.card-title {
  font-size: 14.5px;
  font-weight: 620;
  margin: 0;
  letter-spacing: -0.005em;
}

.card-sub {
  font-size: 12.5px;
  color: var(--text-secondary);
  margin: 2px 0 0;
}

.card-head .spacer { flex: 1 1 auto; }

.toggle {
  appearance: none;
  font: inherit;
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--plane);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 11px;
  cursor: pointer;
  flex: none;
}

.toggle:hover { background: var(--wash); color: var(--text-primary); }
.toggle[aria-pressed="true"] { background: var(--surface-2); color: var(--text-primary); }

.card-body { margin-top: 10px; }
.chart-host { display: block; }
.chart-host svg { display: block; width: 100%; height: auto; overflow: visible; }

/* ------------------------------------------------------------- presence */

/* Who is here.  The hub, and each cot it can see, as pills on a strip
 * of their own -- the same shape as the system strip below it, because
 * they answer the same kind of question: what is the state of the
 * machine right now. */
.presence-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: var(--gap);
  font-size: 13px;
}

.presence-strip .strip-label {
  color: var(--text-secondary);
  font-size: 12.5px;
  margin-right: 2px;
}

.presence-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px 3px 8px;
  background: var(--plane);
  color: var(--text-primary);
  white-space: nowrap;
}

.presence-chip .presence-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex: none;
  display: block;
}

.presence-chip[data-state="on"] .presence-dot { background: var(--good); }
.presence-chip[data-state="off"] .presence-dot { background: var(--text-muted); }
.presence-chip[data-state="deaf"] .presence-dot { background: var(--warning); }

.presence-chip .p-word { color: var(--text-secondary); }
.presence-chip .p-detail { color: var(--text-muted); font-size: 12px; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  margin-top: 10px;
  font-size: 12.5px;
  color: var(--text-secondary);
}

.legend .item { display: inline-flex; align-items: center; gap: 6px; }
.legend .swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }

.scale-legend {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  font-size: 12px;
  color: var(--text-secondary);
}

.scale-legend .ramp { display: flex; gap: 2px; }
.scale-legend .ramp i { width: 20px; height: 9px; border-radius: 2px; display: block; }

.card-note {
  font-size: 12px;
  color: var(--text-secondary);
  margin: 10px 0 0;
}

/* ---------------------------------------------------------------- tables */

.table-wrap { overflow-x: auto; }

table.data {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
}

table.data th,
table.data td {
  text-align: left;
  padding: 7px 10px 7px 0;
  border-bottom: 1px solid var(--grid);
  white-space: nowrap;
}

table.data th {
  font-weight: 550;
  color: var(--text-secondary);
  font-size: 12px;
  border-bottom: 1px solid var(--axis);
}

table.data tr:last-child td { border-bottom: 0; }
table.data td.n, table.data th.n { text-align: right; font-variant-numeric: tabular-nums; }
table.data td .sub { display: block; font-size: 11.5px; color: var(--text-secondary); }

.cot-chip { display: inline-flex; align-items: center; gap: 6px; }
.cot-chip i { width: 9px; height: 9px; border-radius: 3px; flex: none; display: block; }

.kind-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-secondary);
}
.kind-tag .icon { width: 13px; height: 13px; flex: none; }

.meter {
  display: inline-block;
  vertical-align: middle;
  width: 62px;
  height: 7px;
  border-radius: 4px;
  background: var(--seq-100);
  overflow: hidden;
  margin-right: 8px;
}
.meter i { display: block; height: 100%; background: var(--seq-450); border-radius: 4px; }

.table-foot {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-secondary);
}

.row-toggle { margin-bottom: 10px; }

/* --------------------------------------------------------------- insights */

.insights { list-style: none; margin: 0; padding: 0; }

.insights li {
  display: flex;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--grid);
  font-size: 14px;
  line-height: 1.45;
}

.insights li:last-child { border-bottom: 0; }

.insights .bullet {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--seq-300);
  flex: none;
  margin-top: 7px;
}

/* ------------------------------------------------------------ empty state */

.empty {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 44px 26px 40px;
  text-align: center;
}

.empty svg { display: block; margin: 0 auto 16px; }
.empty .art-line { stroke: var(--axis); }
.empty .art-soft { stroke: var(--grid); }
.empty .art-dot { fill: var(--seq-100); }
.empty h2 { font-size: 18px; font-weight: 620; margin: 0 0 8px; }
.empty p {
  margin: 0 auto 6px;
  max-width: 46ch;
  color: var(--text-secondary);
  font-size: 14px;
}
.empty code {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12.5px;
  background: var(--surface-2);
  border-radius: 5px;
  padding: 1px 6px;
  color: var(--text-primary);
}

.mini-empty {
  color: var(--text-secondary);
  font-size: 13px;
  padding: 18px 0 10px;
}

/* Heatmap cell numbers wear ink, never the cell's colour: white on the
 * deep end of the light ramp, near-black on the bright end of the dark
 * ramp (which runs the other way). */
.heat-lo { fill: var(--text-primary); }
.heat-hi { fill: #ffffff; }
[data-theme="dark"] .heat-hi { fill: #020404; }

/* --------------------------------------------------------------- tooltip */

.tooltip {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: 0 6px 22px rgba(11, 11, 11, 0.10);
  padding: 8px 11px 9px;
  font-size: 12.5px;
  max-width: 260px;
  color: var(--text-primary);
}

.tooltip[hidden] { display: none; }
[data-theme="dark"] .tooltip { box-shadow: 0 6px 22px rgba(0, 0, 0, 0.5); }
.tooltip .tt-title { color: var(--text-secondary); font-size: 11.5px; margin-bottom: 5px; }
.tooltip .tt-row { display: flex; align-items: center; gap: 7px; margin-top: 3px; }
.tooltip .tt-key { width: 12px; height: 3px; border-radius: 2px; flex: none; }
.tooltip .tt-val { font-weight: 640; font-variant-numeric: tabular-nums; }
.tooltip .tt-name { color: var(--text-secondary); }
.tooltip .tt-total { margin-top: 6px; padding-top: 5px; border-top: 1px solid var(--grid); }

/* --------------------------------------------------------------- refresh */

.refreshing { opacity: 0.62; transition: opacity 140ms ease; }
#view-overview, #view-patterns { transition: opacity 140ms ease; }

.footer {
  margin-top: 26px;
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ----------------------------------------------------------- responsive */

@media (max-width: 720px) {
  .app { padding: 16px 13px 44px; }
  .grid { grid-template-columns: minmax(0, 1fr); }
  .card.span2 { grid-column: auto; }
  .topbar { margin-bottom: 12px; }
  .hubmeta { flex-basis: 100%; order: 3; }
  .controls { gap: 8px 10px; }
  .card { padding: 13px 13px 14px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}