/* ---------------------------------------------------------------- tokens */
:root {
  --bg: #0b0f14;
  --bg-panel: #121821;
  --bg-sunken: #0d131a;
  --line: #22303f;
  --line-soft: #1a242f;
  --text: #e7eef6;
  --text-dim: #93a4b8;
  --text-faint: #64748b;

  --accent: #4dd4c0;
  --accent-ink: #062b27;
  --ok: #4ade80;
  --watch: #fbbf24;
  --warn: #fb923c;
  --alarm: #f87171;

  --heart: #f2647d;
  --lung: #6aa9f0;
  --heat: #fb7185;
  --cool: #60a5fa;
  --fuel: #facc15;
  --neuro: #c084fc;

  --radius: 12px;
  --radius-sm: 8px;
  --gap: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 8px 24px rgb(0 0 0 / 22%);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f2f5f8;
    --bg-panel: #ffffff;
    --bg-sunken: #eef2f6;
    --line: #d7dfe8;
    --line-soft: #e6ecf2;
    --text: #17222e;
    --text-dim: #4d5e70;
    --text-faint: #7d8c9c;
    --accent: #0d9488;
    --accent-ink: #ffffff;
    --ok: #16a34a;
    --watch: #b45309;
    --warn: #c2410c;
    --alarm: #dc2626;
    --shadow: 0 1px 2px rgb(15 30 45 / 8%), 0 6px 18px rgb(15 30 45 / 8%);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }

a { color: var(--accent); }

.skip {
  position: absolute;
  left: -9999px;
}
.skip:focus {
  left: 12px;
  top: 12px;
  z-index: 10;
  background: var(--bg-panel);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
}

/* ---------------------------------------------------------------- topbar */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--gap);
  padding: 14px clamp(16px, 3vw, 28px);
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, var(--bg-panel), var(--bg));
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand { flex: 1 1 260px; min-width: 0; }
.brand h1 { font-size: 19px; }
.tagline { margin: 2px 0 0; color: var(--text-dim); font-size: 13px; }

.clockbox { text-align: right; }
.clock {
  font-family: var(--mono);
  font-size: 26px;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.elapsed { color: var(--text-faint); font-size: 12px; font-variant-numeric: tabular-nums; }

.transport { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.speeds { display: flex; gap: 4px; }

.btn, .chip {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 7px 12px;
  cursor: pointer;
  transition: background 120ms, border-color 120ms, color 120ms;
}
.btn:hover, .chip:hover { border-color: var(--accent); }
.btn:focus-visible, .chip:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn.speed { padding: 7px 9px; font-variant-numeric: tabular-nums; }
.btn.speed.is-active { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

.error-banner {
  margin: 12px clamp(16px, 3vw, 28px);
  padding: 12px 14px;
  border: 1px solid var(--alarm);
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--alarm) 12%, transparent);
  font-size: 13px;
}

/* ---------------------------------------------------------------- layout */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 320px) minmax(0, 1fr) minmax(0, 400px);
  gap: var(--gap);
  padding: var(--gap) clamp(16px, 3vw, 28px) 28px;
  align-items: start;
}

.column { display: flex; flex-direction: column; gap: var(--gap); min-width: 0; }

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.panel > h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-faint);
  margin-bottom: 10px;
}

/* ---------------------------------------------------------------- fields */
.field { display: block; margin-bottom: 12px; }
.field:last-of-type { margin-bottom: 4px; }
.field-label {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 5px;
}
.field-label b { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 600; }

input[type="range"] {
  width: 100%;
  appearance: none;
  height: 22px;
  background: transparent;
  cursor: pointer;
  margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 5px;
  border-radius: 3px;
  background: var(--line);
}
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -5.5px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-panel);
}
input[type="range"]::-moz-range-track { height: 5px; border-radius: 3px; background: var(--line); }
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--bg-panel);
}

select {
  width: 100%;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip.is-active { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

.switch { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 13px; color: var(--text-dim); }
.switch input { accent-color: var(--accent); width: 16px; height: 16px; }

.hint { margin: 8px 0 0; font-size: 12px; color: var(--text-faint); }

.derived {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 10px;
  margin: 10px 0 0;
  padding-top: 10px;
  border-top: 1px solid var(--line-soft);
  font-size: 12px;
}
.derived dt { color: var(--text-faint); }
.derived dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- body */
.bodypanel { display: flex; flex-direction: column; gap: 10px; }

.statusline {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
}
.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 22%, transparent);
  flex: none;
}
.status-dot[data-level="watch"] { background: var(--watch); box-shadow: 0 0 0 4px color-mix(in srgb, var(--watch) 22%, transparent); }
.status-dot[data-level="warn"]  { background: var(--warn);  box-shadow: 0 0 0 4px color-mix(in srgb, var(--warn) 22%, transparent); }
.status-dot[data-level="alarm"] { background: var(--alarm); box-shadow: 0 0 0 4px color-mix(in srgb, var(--alarm) 26%, transparent); }

.bodystage {
  background: radial-gradient(ellipse at 50% 35%, color-mix(in srgb, var(--accent) 7%, transparent), transparent 62%), var(--bg-sunken);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 8px;
  display: flex;
  justify-content: center;
  min-width: 0;
}
.bodystage svg { width: 100%; height: auto; max-height: 62vh; display: block; }

.alerts { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.alerts li {
  font-size: 13px;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border-left: 3px solid var(--watch);
  background: var(--bg-sunken);
  color: var(--text-dim);
}
.alerts li[data-level="warn"]  { border-left-color: var(--warn); }
.alerts li[data-level="alarm"] { border-left-color: var(--alarm); color: var(--text); }

.logpanel { max-height: 220px; display: flex; flex-direction: column; }
.log {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  font-size: 12.5px;
  display: flex;
  flex-direction: column-reverse;
  gap: 4px;
  min-height: 0;
}
.log li { display: flex; gap: 8px; color: var(--text-dim); }
.log time { font-family: var(--mono); color: var(--text-faint); flex: none; }
.log li[data-kind="action"] { color: var(--text); }
.log li[data-kind="alarm"] { color: var(--alarm); }
.log li[data-kind="warn"] { color: var(--warn); }

/* ---------------------------------------------------------------- cards */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
}

.card {
  background: var(--bg-sunken);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  padding: 9px 10px 7px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.card-name { font-size: 11px; color: var(--text-faint); letter-spacing: 0.02em; }
.card-value {
  font-family: var(--mono);
  font-size: 21px;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
  color: var(--text);
}
.card-value .unit { font-size: 11px; color: var(--text-faint); margin-left: 3px; font-family: var(--font); }
.card-status { font-size: 11px; color: var(--text-dim); }
.card-status[data-level="watch"] { color: var(--watch); }
.card-status[data-level="warn"]  { color: var(--warn); }
.card-status[data-level="alarm"] { color: var(--alarm); }
.card canvas { width: 100%; height: 28px; display: block; margin-top: 4px; }

/* ---------------------------------------------------------------- footer */
.footer {
  border-top: 1px solid var(--line);
  padding: 18px clamp(16px, 3vw, 28px) 32px;
  color: var(--text-faint);
  font-size: 12.5px;
  max-width: 80ch;
}
.footer p { margin: 0 0 8px; }
.footer .links { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------------------------------------------------------------- narrow */
@media (max-width: 1180px) {
  .layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .bodycol { grid-column: 2; grid-row: 1 / span 2; }
  .vitals { grid-column: 1 / -1; }
}

@media (max-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .bodycol, .vitals { grid-column: 1; grid-row: auto; }
  /* The body is the point of the page, so it leads on a narrow screen. */
  .bodycol { order: 1; }
  .vitals { order: 2; }
  .controls { order: 3; }
  .topbar { position: static; }
  .clockbox { text-align: left; }
  .bodystage svg { max-height: 52vh; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
