:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #e2e3e8;
  --text: #16171a;
  --text-muted: #6b6f76;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --ok: #1f9d55;
  --warn: #c98a02;
  --danger: #d1373f;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1e222a;
    --border: #2a2e37;
    --text: #eceef1;
    --text-muted: #9aa0aa;
  }
}

/* The browser's default [hidden]{display:none} lives in the UA stylesheet,
   so any author rule of equal specificity (.stack/.row's display:flex, etc.)
   wins the cascade and keeps a "hidden" element visible. Restate it here,
   in author CSS, so el.hidden = true always actually hides the element. */
[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px; line-height: 1.5;
  overflow-x: hidden;
  touch-action: pan-x pan-y;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.3rem; margin: 0 0 .25rem; }
h2 { font-size: 1rem; margin: 0 0 .5rem; }
.muted { color: var(--text-muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.right { text-align: right; }
.stack { display: flex; flex-direction: column; gap: 1.25rem; }
.row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; }
.spread { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.grow { flex: 1; }

/* ── shell ──────────────────────────────────────────────────────── */
.shell { display: flex; min-height: 100vh; }
.side {
  width: 220px; flex-shrink: 0;
  border-right: 1px solid var(--border); background: var(--surface); padding: 1rem;
}
.brand { font-weight: 600; padding: .25rem .75rem 1.25rem; }
.side nav { display: flex; flex-direction: column; gap: .15rem; }
.side nav a {
  color: var(--text-muted); padding: .5rem .75rem; border-radius: 8px;
}
.side nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.side nav a.on { background: var(--surface-2); color: var(--text); font-weight: 500; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: .75rem 1.5rem; border-bottom: 1px solid var(--border);
}
.content { padding: 1.5rem 2rem; max-width: none; width: 100%; }
.nav-toggle { display: none; font-size: 1.15rem; padding: .25rem .4rem; }
.side-overlay { display: none; }

@media (max-width: 880px) {
  .nav-toggle { display: inline-flex; }
  .side {
    position: fixed; inset: 0 auto 0 0; z-index: 30;
    transform: translateX(-100%); transition: transform .2s ease;
    width: 240px; overflow-y: auto;
  }
  .shell.nav-open .side { transform: translateX(0); }
  .side-overlay {
    display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4);
    z-index: 20; opacity: 0; pointer-events: none; transition: opacity .2s ease;
  }
  .shell.nav-open .side-overlay { opacity: 1; pointer-events: auto; }
  .topbar { padding: .6rem .9rem; gap: .5rem; }
  .topbar .muted { font-size: .8rem; }
  .content { padding: 1rem .9rem; }
}

/* ── auth ───────────────────────────────────────────────────────── */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.auth-card { width: 100%; max-width: 360px; }

/* ── cards / tables ─────────────────────────────────────────────── */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.25rem;
}
.card.narrow { max-width: 420px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }
.tile .k { color: var(--text-muted); }
.tile .v { font-size: 1.15rem; font-weight: 600; margin-top: .15rem; }
.tile .s { color: var(--text-muted); margin-top: .15rem; font-size: .85rem; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; text-align: left; }
thead { background: var(--surface-2); color: var(--text-muted); }
th, td { padding: .5rem .75rem; vertical-align: top; }
tbody tr { border-top: 1px solid var(--border); }
td form { display: inline; }

/* ── forms ──────────────────────────────────────────────────────── */
label.field { display: flex; flex-direction: column; gap: .25rem; margin-bottom: .25rem; }
input[type=text], input[type=email], input[type=password], input:not([type]), select, textarea {
  background: transparent; color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: .5rem .6rem; font: inherit;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { resize: vertical; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: .75rem; }
legend { color: var(--text-muted); padding: 0 .35rem; }
.check { display: flex; align-items: center; gap: .5rem; }

/* Full-width form layout: fields flow into as many columns as fit; rows
   that read better across the whole frame opt in with .span-all. */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1rem; align-items: start; }
.form-grid > .span-all { grid-column: 1 / -1; }

button, .btn {
  font: inherit; cursor: pointer; border-radius: 8px; padding: .5rem .8rem;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
button:hover, .btn:hover { text-decoration: none; }
button.primary, .btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
button.danger, .btn.danger { border-color: var(--danger); color: var(--danger); }
button.link { border: none; background: none; color: var(--text-muted); padding: 0; }
button.link:hover { color: var(--text); }
button.sm, .btn.sm { padding: .25rem .5rem; font-size: .85rem; }
button:disabled { opacity: .45; cursor: not-allowed; }

/* ── bits ───────────────────────────────────────────────────────── */
.flash { padding: .5rem .75rem; border-radius: 8px; margin-bottom: .75rem; }
.flash-ok { background: color-mix(in srgb, var(--ok) 12%, transparent); color: var(--ok); }
.flash-warn { background: color-mix(in srgb, var(--warn) 14%, transparent); color: var(--warn); }
.flash-error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }

.chip { display: inline-block; min-width: 3.5rem; text-align: center; padding: .1rem .5rem; border-radius: 999px; font-size: .8rem; }
.chip.ok { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.chip.stale { background: color-mix(in srgb, var(--warn) 15%, transparent); color: var(--warn); }
.chip.down { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }
.chip.idle { background: color-mix(in srgb, var(--text-muted) 15%, transparent); color: var(--text-muted); }
.chip.off { color: var(--text-muted); }
.fresh-at { font-size: .75rem; white-space: nowrap; cursor: help; }

.dot { font-size: .7rem; }
.dot.ok { color: var(--ok); } .dot.warn { color: var(--warn); } .dot.crit, .dot.down { color: var(--danger); }
.dot.muted { color: var(--text-muted); }

tr.row-configured { background: color-mix(in srgb, var(--ok) 8%, transparent); }
tr.row-attention td { background: color-mix(in srgb, var(--warn) 12%, transparent); }
.attention-flag { color: var(--warn); cursor: help; }

.tabs { display: flex; gap: 1rem; }
.tabs a { color: var(--text-muted); padding-bottom: .25rem; }
.tabs a.on { color: var(--text); border-bottom: 2px solid var(--accent); }

pre.log {
  max-height: 16rem; overflow: auto; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px; padding: .75rem;
  font-size: .8rem; white-space: pre-wrap;
}
.sparkline { width: 100%; height: 120px; display: block; }
.recording-detail { margin: .45rem 0 .1rem; }
.recording-slot { margin: 0; font-size: .85rem; }
.current-audio-list { display: grid; gap: .75rem; margin-top: 1rem; }
.current-audio { padding-top: .75rem; border-top: 1px solid var(--border); }

@media (max-width: 720px) {
  .tiles { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .6rem; }
  .card { padding: 1rem; }
  th, td { padding: .4rem .5rem; font-size: .85rem; }
  .tabs { gap: .6rem; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; }
  h1 { font-size: 1.15rem; }
}

/* ── waveform audio player ──────────────────────────────────────── */
.wave-player {
  display: flex; flex-direction: column; gap: .5rem;
  width: 100%; margin: .5rem 0;
}
.wave-canvas-wrap {
  position: relative; width: 100%; height: 80px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  overflow: hidden; cursor: pointer;
}
.wave-canvas-wrap canvas { display: block; width: 100%; height: 100%; }
.wave-canvas-wrap:focus, .wave-canvas-wrap:focus-visible { outline: none; }
.wave-canvas-wrap .wave-loading {
  position: absolute; inset: 0; display: grid; place-items: center;
  color: var(--text-muted); font-size: .8rem;
}
.wave-canvas-wrap .wave-loading[hidden] { display: none; }
.wave-canvas-wrap .wave-loading.is-loading {
  color: var(--text); font-weight: 500; letter-spacing: .02em;
  background: color-mix(in srgb, var(--surface) 78%, transparent);
}
.wave-controls { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.wave-controls .wave-btn { min-width: 2.4rem; }
.wave-time { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .8rem; color: var(--text-muted); }
/* Icon action buttons (e.g. download / delete in the Recorded audio table) */
.audio-actions { white-space: nowrap; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 1.9rem; height: 1.9rem; padding: 0; font-size: .95rem; line-height: 1;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); cursor: pointer;
}
.icon-btn + .icon-btn { margin-left: .35rem; }
.icon-btn:hover { background: var(--surface-2); text-decoration: none; }
.icon-btn.danger { border-color: var(--danger); color: var(--danger); }
.icon-btn.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }

.wave-row-toggle { cursor: pointer; }
.wave-row-toggle:hover { text-decoration: underline; }
.wave-detail-cell { background: var(--surface-2); }
/* Inside a scrollable table (see WavePlayer.fitWidth in app.js), the player
   would drift off-screen with its full-width <td> when the table is
   scrolled horizontally. `position: sticky` looked right but iOS Safari
   hit-tests touches at the element's un-stuck position while painting it
   stuck, making the whole widget untappable — so instead app.js resets the
   table's scroll to the left edge when a row is expanded (see the
   data-wave-toggle handler). */

/* ── listeners chart ───────────────────────────────────────────── */
.listeners-chart { position: relative; width: 100%; }
.listeners-chart svg { display: block; width: 100%; height: 220px; overflow: visible; }
.listeners-chart .lc-axis { stroke: var(--border); stroke-width: 1; }
.listeners-chart .lc-grid { stroke: var(--border); stroke-width: 1; stroke-dasharray: 2 3; opacity: .5; }
.listeners-chart .lc-line { fill: none; stroke: var(--accent); stroke-width: 1.5; }
.listeners-chart .lc-tick { fill: var(--text-muted); font-size: 10px; }
.listeners-chart .lc-cursor { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.listeners-chart .lc-dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.listeners-chart .lc-tooltip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: .35rem .55rem; font-size: .78rem; white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0,0,0,.14); transform: translate(-50%, -120%);
}
.listeners-chart .lc-tooltip .lc-tt-v { font-weight: 600; }
.listeners-chart .lc-tooltip .lc-tt-t { color: var(--text-muted); }

/* ── "duplicate config" target typeahead (/processes/<stream>) ──── */
.stream-suggest { position: relative; display: block; }
.stream-suggest > input { width: 100%; }
.stream-suggest-list {
  position: absolute; z-index: 10; left: 0; right: 0; top: calc(100% + 3px);
  margin: 0; padding: .25rem; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  max-height: 16rem; overflow-y: auto;
  box-shadow: 0 6px 20px rgba(0,0,0,.14);
}
/* dropped upward when there isn't room below (set by app.js) */
.stream-suggest-list.up { top: auto; bottom: calc(100% + 3px); }
.stream-suggest-list li {
  display: flex; flex-direction: column; gap: .1rem;
  padding: .35rem .5rem; border-radius: 6px; cursor: pointer;
}
.stream-suggest-list li.on,
.stream-suggest-list li:hover { background: var(--surface-2); }
.stream-suggest-list li[data-configured] { opacity: .55; }
.stream-suggest-list .ss-name { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.stream-suggest-list .ss-meta { font-size: .8rem; color: var(--text-muted); }
