:root {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --series-1: #3987e5;
  --series-1-dark: #2160b8; /* octave-centre bars in the spectrum */
  --series-2: #d95926;
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --radius: 8px;
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
}

h1 { font-size: 1.1rem; margin: 0; font-weight: 600; white-space: nowrap; }
h2 { font-size: 0.95rem; margin: 0; font-weight: 600; }
code { font-size: 0.9em; }

.muted { color: var(--muted); }
.small { font-size: 0.8rem; font-weight: 400; }
.tabular { font-variant-numeric: tabular-nums; }

/* Top bar */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 20px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.controls { display: flex; flex-wrap: wrap; align-items: end; gap: 10px; flex: 1; }
.field { display: flex; flex-direction: column; gap: 2px; font-size: 0.75rem; color: var(--muted); }
.field select { min-width: 180px; max-width: 320px; }
select, input[type='number'] {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--axis);
  border-radius: 6px;
  padding: 6px 8px;
  font: inherit;
  font-size: 0.9rem;
}
input[type='number'] { width: 5.5em; }
.clock { display: flex; flex-direction: column; align-items: flex-end; font-variant-numeric: tabular-nums; }
#wallclock { font-size: 1.25rem; font-weight: 600; }
#elapsed { font-size: 0.8rem; }

.btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--axis);
  border-radius: 6px;
  padding: 7px 14px;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--series-1); border-color: var(--series-1); color: #fff; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.recording { border-color: var(--critical); color: var(--text); }
.btn.recording::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--critical); }
:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; }

/* Notices and alerts */
#notices, .alerts { padding: 0 16px; }
.notice {
  margin-top: 10px;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border-left: 4px solid var(--muted);
}
.notice a { color: var(--text); text-decoration: underline; text-underline-offset: 2px; }
.notice.warning { border-left-color: var(--warning); }
.notice.serious { border-left-color: var(--serious); }
.notice.critical { border-left-color: var(--critical); }

.alerts { display: flex; flex-direction: column; gap: 6px; }
.alerts:not(:empty) { margin-top: 10px; }
.alert {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: #3a1514;
  border: 1px solid var(--critical);
  font-weight: 600;
  font-size: 1.05rem;
}
.alert.warning { background: #3a2c0b; border-color: var(--warning); }
.alert .icon { font-size: 1.2rem; }
.alert .since { margin-left: auto; font-weight: 400; color: var(--text-2); font-size: 0.85rem; }
.alert button { background: none; border: none; color: var(--text-2); font-size: 1.1rem; cursor: pointer; padding: 0 4px; }

main { padding: 12px 16px 24px; display: flex; flex-direction: column; gap: 12px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  min-width: 0;
}
.panel-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }

/* Readouts */
.readouts { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-width: 0;
  container-type: inline-size;
}
.tile .label { color: var(--text-2); font-size: 0.85rem; }
.tile .big { font-size: clamp(1.8rem, 31cqi, 4.6rem); font-weight: 650; line-height: 1.1; margin: 4px 0; white-space: nowrap; }
.tile .unit-label { color: var(--muted); }
.tile .sub { color: var(--text-2); font-size: 0.85rem; min-height: 1.3em; }
.tile.alerting { border-color: var(--warning); }

.status-tile { grid-column: span 2; color: #fff; }
.status-tile .label, .status-tile .sub { color: rgba(255, 255, 255, 0.85); }
.status-tile .big { display: flex; align-items: center; gap: 12px; font-size: clamp(1.8rem, 12cqi, 4rem); }
.status-tile[data-status='IDLE'] { background: var(--surface); color: var(--text-2); }
.status-tile[data-status='CLEAN'] { background: #0b5f0b; border-color: var(--good); }
.status-tile[data-status='LIMITED'] { background: #7a5406; border-color: var(--warning); }
.status-tile[data-status='CLIPPING'] { background: #9e1f1f; border-color: var(--critical); }
@media (max-width: 560px) { .status-tile { grid-column: span 1; } }
/* Wide windows: no line in the row wraps, so the row keeps its height. Widths fit the widest
   line with worst-case readings and chips, measured with DejaVu Sans (wider than Segoe UI).
   The four number cards share one width (that of Short-term and True peak, whose lines are
   longest) so their numbers stay the same size; the rest goes to Clipping status and Stereo. */
@media (min-width: 101.25em) {
  .readouts { grid-template-columns: minmax(17.75rem, 1.9fr) repeat(4, 15.125rem) minmax(15.75rem, 1.55fr); }
  .status-tile { grid-column: auto; }
  .status-tile .big { font-size: clamp(1.8rem, 14cqi, 4rem); }
}
/* Narrower windows: the Stereo line can need two lines, so it always keeps room for two. */
@media (width < 101.25em) {
  #stereo-detail { min-height: 2.6em; }
}

.badge { display: inline-block; font-size: 0.6em; padding: 2px 12px; border-radius: 6px; border: 2px solid var(--axis); color: var(--text-2); }
.badge.mono { border-color: var(--text); color: var(--text); }
/* Stereo status word: status colour on the border and a light tint, label in text colour. */
.badge[data-stereo='ok'] { border-color: var(--good); color: var(--text); }
.badge[data-stereo='warn'] { border-color: var(--warning); color: var(--text); background: rgba(250, 178, 25, 0.12); }
.badge[data-stereo='crit'] { border-color: var(--critical); color: var(--text); background: rgba(208, 59, 59, 0.22); }
.badge.long { font-size: clamp(0.75rem, 6.5cqi, 1.3rem); padding: 4px 10px; white-space: nowrap; }

.chip { display: inline-block; font-size: 0.75rem; padding: 0 8px; border-radius: 10px; border: 1px solid currentColor; margin-left: 6px; }
.chip.good { color: var(--good); }
.chip.warning { color: var(--warning); }
.chip.critical { color: #ff7b7b; }

/* Charts */
.charts { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 1200px) { .charts { grid-template-columns: 3fr 2fr; } }
.chart-wrap { position: relative; width: 100%; }
.chart-wrap canvas { display: block; width: 100%; }
#timeline { cursor: pointer; }

.seg { display: inline-flex; border: 1px solid var(--axis); border-radius: 6px; overflow: hidden; }
.seg button { background: transparent; color: var(--text-2); border: none; padding: 4px 12px; font: inherit; font-size: 0.85rem; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--axis); }
.seg button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }

.spectrum { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 900px) { .spectrum { grid-template-columns: minmax(0, 2.2fr) minmax(340px, 1fr); align-items: start; } }
.panel-head.sub { margin-top: 14px; }
.side-stack { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.stereo-legend { display: flex; flex-wrap: wrap; align-items: center; gap: 3px; margin-top: 6px; color: var(--text-2); }
.stereo-legend .sw { width: 14px; height: 10px; border-radius: 2px; }
.stereo-legend > span:not(.sw) { margin: 0 3px; }
.stereo-legend .sep { margin-left: 10px; }
.stereo-legend .zone { display: inline-block; width: 16px; height: 8px; border: 1px dashed rgba(255, 255, 255, 0.45); border-top: none; border-radius: 0 0 8px 8px; background: rgba(255, 255, 255, 0.05); }
.stereo-note { margin: 6px 0 0; color: var(--text-2); }

.balance { display: grid; grid-template-columns: 4.2em 1fr 3.6em; gap: 2px 8px; align-items: center; font-size: 0.85rem; }
.balance .f { color: var(--text-2); text-align: right; }
.balance .v { text-align: right; font-variant-numeric: tabular-nums; }
.balance .track { position: relative; height: 12px; }
.balance .track::before { content: ''; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--axis); }
.balance .bar { position: absolute; top: 1px; height: 10px; background: var(--series-1); border-radius: 2px; }
.balance .ref .bar { background: var(--muted); }

/* Waveform */
.wave-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.wave-actions select { max-width: 22em; }
.wave-actions .seg button:disabled { opacity: 0.4; cursor: default; }
#waveform { cursor: grab; }
.wave-foot { display: flex; flex-wrap: wrap; gap: 4px 18px; margin-top: 6px; color: var(--text-2); }
.wave-foot .legend { display: inline-flex; flex-wrap: wrap; gap: 4px 14px; }
.wave-foot .legend > span { display: inline-flex; align-items: center; gap: 6px; }
.wave-foot .sw { display: inline-block; width: 14px; height: 3px; border-radius: 2px; }
.wave-foot .sw.dash { height: 0; border-top: 1px dashed var(--muted); border-radius: 0; }
/* Log and detectors */
.bottom { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 900px) { .bottom { grid-template-columns: 3fr 1fr; } }
.log-actions, .file-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
/* Keyboard shortcuts for scrubbing a saved CSV, under the file timeline. */
.scrub-keys { display: flex; flex-wrap: wrap; gap: 4px 18px; margin-top: 6px; color: var(--muted); }
kbd { font: inherit; color: var(--text-2); padding: 0 5px; border: 1px solid var(--axis); border-bottom-width: 2px; border-radius: 4px; background: var(--surface-2); }
.log-scroll { max-height: 320px; overflow: auto; }
table.log { width: 100%; border-collapse: collapse; font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.log th { text-align: left; color: var(--muted); font-weight: 500; position: sticky; top: 0; background: var(--surface); padding: 4px 8px; border-bottom: 1px solid var(--axis); }
.log td { padding: 5px 8px; border-bottom: 1px solid var(--grid); vertical-align: top; }
.log tr.seekable { cursor: pointer; }
.log .wave-link { margin-left: 8px; padding: 0 8px; font: inherit; font-size: 0.8rem; color: var(--text-2); background: var(--surface-2); border: 1px solid var(--axis); border-radius: 10px; cursor: pointer; }
.log .wave-link:hover { color: var(--text); border-color: var(--muted); }
.log tr.seekable:hover td { background: var(--surface-2); }
.ev { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.ev::before { content: ''; width: 9px; height: 9px; border-radius: 2px; background: var(--muted); }
.ev-CLIPPING::before { background: var(--critical); }
.ev-LIMITED::before { background: var(--warning); }
.ev-CLEAN::before { background: var(--good); }
.ev-alert::before { background: var(--serious); border-radius: 50%; }
.ev-hum::before { background: var(--serious); }
.ev-stereo::before { background: var(--series-1); }
.empty { margin: 8px; }

.detectors dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0; font-size: 0.85rem; }
.detectors dt { color: var(--text-2); }
.detectors dd { margin: 0; }

/* File mode */
.dropzone {
  border: 2px dashed var(--axis);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
}
.dropzone.over { border-color: var(--series-1); background: var(--surface-2); }
.dropzone p { margin: 6px 0; }
.progress { position: relative; height: 26px; background: var(--surface-2); border-radius: 6px; overflow: hidden; margin: 12px 0; }
.progress .bar { position: absolute; inset: 0 auto 0 0; width: 0; background: var(--series-1); opacity: 0.6; }
.progress .label { position: relative; padding: 0 10px; line-height: 26px; font-size: 0.85rem; }
.file-panel { display: flex; flex-direction: column; gap: 10px; }
.file-summary { display: flex; flex-wrap: wrap; gap: 6px 22px; font-size: 0.9rem; color: var(--text-2); margin-bottom: 8px; }
.file-summary b { color: var(--text); font-variant-numeric: tabular-nums; }

/* Tooltip */
.tooltip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  background: var(--surface-2);
  border: 1px solid var(--axis);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  max-width: 260px;
}
.tooltip .row { display: flex; align-items: center; gap: 6px; }
.tooltip .sw { width: 10px; height: 3px; border-radius: 2px; }
.tooltip .k { color: var(--text-2); }
.tooltip .val { margin-left: auto; padding-left: 10px; font-weight: 600; }

/* Settings dialog */
dialog.settings {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--axis);
  border-radius: 10px;
  padding: 18px 20px;
  width: min(520px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.settings fieldset { border: 1px solid var(--grid); border-radius: 6px; margin: 10px 0; padding: 8px 12px; display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.settings legend { padding: 0 4px; font-weight: 600; }
.settings label { font-size: 0.9rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.dialog-actions #settings-reset { margin-right: auto; }

/* Hover / focus help on cards (src/ui/help.js) */
[data-help] { cursor: help; }
/* A small circled "i" drawn with CSS, so it doesn't depend on the font having one. */
[data-help] > .label::after,
h2[data-help]::after,
span[data-help]:not(.badge)::after {
  content: 'i';
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 13px;
  height: 13px;
  margin-left: 6px;
  border: 1px solid currentColor;
  border-radius: 50%;
  color: var(--muted);
  font: italic 600 9px/1 Georgia, serif;
  vertical-align: 1px;
}
.status-tile[data-help] > .label::after { color: rgba(255, 255, 255, 0.75); }
/* Icon-only help (side panel): real icon elements replace the drawn ones, and only they show help. */
.help-icon-only [data-help] { cursor: inherit; }
.help-icon-only [data-help] > .label::after,
.help-icon-only h2[data-help]::after,
.help-icon-only span[data-help]:not(.badge)::after { content: none; }
.help-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 13px;
  height: 13px;
  margin-left: 6px;
  border: 1px solid currentColor;
  border-radius: 50%;
  color: var(--muted);
  font: italic 600 9px/1 Georgia, serif;
  vertical-align: 1px;
  cursor: help;
  position: relative;
}
/* A bigger invisible target than the 13 px circle. */
.help-icon::before { content: ''; position: absolute; inset: -6px; }
.status-tile .help-icon { color: rgba(255, 255, 255, 0.75); }
.tooltip.help { max-width: 320px; font-size: 0.82rem; line-height: 1.45; font-variant-numeric: normal; padding: 8px 12px; }

/* Resettable meters (click the card): hint on hover / focus, brief flash when reset. */
.tile.resettable { position: relative; cursor: pointer; }
.tile.resettable:hover::after,
.tile.resettable:focus-visible::after { content: '\21BA  reset'; position: absolute; bottom: 10px; right: 12px; font-size: 0.75rem; color: var(--text-2); }
.tile.was-reset { animation: reset-flash 0.6s ease-out; }
@keyframes reset-flash { from { background: var(--surface-2); border-color: var(--series-1); } }

/* Mains hum panel */
.hum { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 1100px) { .hum { grid-template-columns: minmax(260px, 1fr) 1.5fr 1.3fr; } }
.hum-listen { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.field.inline { flex-direction: row; align-items: center; gap: 6px; }
.field.inline select { min-width: 0; }
.hum-body { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 560px) { .hum-body { grid-template-columns: minmax(200px, 1fr) 1.4fr; } }
.hum-status { align-self: start; border-radius: var(--radius); padding: 10px 12px; background: var(--surface-2); border: 1px solid var(--border); container-type: inline-size; }
.hum-status .big { font-size: clamp(1.4rem, 13cqi, 2.4rem); font-weight: 650; display: flex; gap: 10px; align-items: center; white-space: nowrap; }
.hum-status .sub { color: var(--text-2); font-size: 0.82rem; margin-top: 4px; }
.hum-status[data-status='PRESENT'] { background: #5c2a16; border-color: var(--serious); }
.hum-status[data-status='NOT DETECTED'] { background: #0b3f0b; border-color: var(--good); }
.hum-status[data-status='MASKED'] { background: var(--surface-2); border-color: var(--axis); }
.hum-facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: 0.85rem; align-content: start; }
.hum-facts dt { color: var(--text-2); }
.hum-facts dd { margin: 0; }
.hum-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 6px; color: var(--text-2); }
.hum-legend > span { white-space: nowrap; }
.hum-legend .sw { display: inline-block; width: 12px; height: 10px; margin-right: 5px; vertical-align: -1px; border-radius: 2px; }
.hum-legend .sw.solid { background: var(--series-2); }
.hum-legend .sw.faded { background: var(--series-2); opacity: 0.45; }
.hum-legend .sw.outline { border: 1px solid var(--muted); }
.hum-line { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.hum-line .hum-status { padding: 4px 10px; container-type: normal; }
.hum-line .hum-status .big { font-size: 1.05rem; }
