:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1f2630;
  --border: #2d3540;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #3b82f6;
  --a: #f5d90a;
  --b: #3fd4f4;
  --d: #c084fc;
  --trig: #ff8c42;
  --ok: #3fb950;
  --warn: #d29922;
  --err: #f85149;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

button, select, input { font: inherit; color: inherit; }

button, select, input[type=number] {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
}

button { cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.active { background: #2b3a55; border-color: var(--accent); }
input[type=number] { width: 8em; }
input[type=range] { flex: 1; min-width: 0; accent-color: var(--accent); }

/* ------------------------------------------------------------------ header */

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 20px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

h1 { font-size: 15px; margin: 0; letter-spacing: 0.02em; }
.conn, .acq { display: flex; align-items: center; gap: 8px; }
.stats { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.status { color: var(--muted); }
.status.ok { color: var(--ok); }
.status.err { color: var(--err); }

#run.running { background: var(--ok); border-color: var(--ok); color: #06140a; }

.badge {
  min-width: 5.5em;
  text-align: center;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 12px;
}
.badge.trig { background: #12361f; color: var(--ok); }
.badge.auto { background: #3a2d0c; color: var(--warn); }
.badge.wait { background: #3a1b12; color: var(--trig); }

/* ------------------------------------------------------------------ layout */

/* Scope and the less-used panels side by side; the front-panel controls across the bottom. */
main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  grid-template-rows: minmax(0, 1fr) auto;
}
.controls {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 10px;
  padding: 8px 12px 10px 10px;
  border-top: 1px solid var(--border);
}
.controls fieldset { margin: 0; }
label.ch-d { color: var(--d); }

.scope { flex: 1; display: flex; flex-direction: column; min-width: 0; padding: 10px; gap: 6px; }
#scope { min-height: 280px; }

#scope, #spectrum {
  flex: 1;
  width: 100%;
  min-height: 240px;
  background: #080b10;
  border: 1px solid var(--border);
  border-radius: 8px;
  touch-action: none;
  cursor: crosshair;
}

.scale-bar, .measure {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 22px;
  font: 12px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
}
.measure { color: var(--muted); min-height: 3em; }
.measure .ch { display: flex; flex-wrap: wrap; gap: 0 14px; }
.measure span { white-space: nowrap; }
.measure b { font-weight: 600; }
.ch-a, .ch-a legend { color: var(--a); }
.ch-b, .ch-b legend { color: var(--b); }
.ch-d legend { color: var(--d); }
.trig, .trig legend { color: var(--trig); }

aside {
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 10px 12px 20px 2px;
}

fieldset {
  min-width: 0;
  margin: 0 0 10px;
  padding: 8px 10px 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}

legend { padding: 0 4px; font-weight: 600; }
legend label { display: flex; align-items: center; gap: 6px; cursor: pointer; }

.row { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; min-height: 28px; }
.row > label:first-child { width: 72px; flex-shrink: 0; color: var(--muted); }
.row output { width: 4.8em; flex-shrink: 0; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.row select { flex: 1; min-width: 0; }
.row .unit { color: var(--muted); }
.row.buttons { flex-wrap: wrap; }
.row[hidden] { display: none; }
#spectrum[hidden] { display: none; }
#spectrum { cursor: crosshair; }
.row.note { min-height: 0; margin-top: -4px; color: var(--muted); font-size: 12px; }
.seg.wide { display: flex; width: 100%; }
.seg.wide button { flex: 1; padding: 3px 4px; }

.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; padding: 3px 11px; }
.seg button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg button:last-child { border-radius: 0 6px 6px 0; }

dl.info { display: grid; grid-template-columns: 72px 1fr; gap: 3px 8px; margin: 0 0 8px; }
dl.info dt { color: var(--muted); }
dl.info dd { margin: 0; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
dl.info dt.sep, dl.info dt.sep + dd { border-top: 1px solid var(--border); padding-top: 5px; margin-top: 4px; }

fieldset:disabled { opacity: 0.55; }

dialog {
  width: min(720px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 18px 20px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog h2 { margin: 0 0 4px; font-size: 16px; }
dialog h3 { margin: 14px 0 4px; font-size: 14px; }
dialog p { margin: 4px 0 8px; }
dialog section { border-top: 1px solid var(--border); margin-top: 10px; }
.muted { color: var(--muted); font-weight: normal; }
.cal-progress { display: flex; align-items: center; gap: 10px; margin: 14px 0 8px; min-height: 20px; }
.cal-progress progress { width: 180px; }
.table-wrap { overflow-x: auto; }
#cal-table { border-collapse: collapse; width: 100%; font: 12px/1.6 ui-monospace, Menlo, Consolas, monospace; font-variant-numeric: tabular-nums; }
#cal-table th, #cal-table td { padding: 1px 8px; text-align: right; border-bottom: 1px solid var(--border); }
#cal-table th { color: var(--muted); font-weight: normal; }
#cal-table td.new { color: var(--ok); }
#cal-table td.none { color: var(--muted); }
#cal-table td.bad { color: var(--err); }
dialog footer { display: flex; gap: 8px; margin-top: 14px; }
dialog footer .spacer { flex: 1; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  max-width: min(90vw, 560px);
  padding: 8px 14px;
  border-radius: 8px;
  background: #3a1b12;
  color: #ffd8cc;
  border: 1px solid #6e2a17;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
#toast.show { opacity: 1; }
#toast.info { background: #102a43; color: #d6e8ff; border-color: #1f4b7a; }

@media (max-width: 820px) {
  main { flex-direction: column; }
  #scope { min-height: 55vh; }
  aside { width: auto; padding: 0 10px 20px; }
  .stats { margin-left: 0; }
}

.save { display: flex; gap: 6px; }
.save button { padding: 3px 9px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chips button { padding: 2px 8px; font-size: 12px; border-radius: 12px; }
.chips button.active { background: #1d3557; border-color: var(--accent); color: var(--text); }
.measure .ch-m b { color: #ff7eb6; }
.measure .ch-cursor b { color: #e6edf3; }

label.check { display: flex; align-items: center; gap: 6px; cursor: pointer; }

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

.help { color: var(--muted); text-decoration: none; border: 1px solid var(--border); border-radius: 50%; width: 22px; height: 22px; display: inline-grid; place-items: center; font-size: 12px; }
.help:hover { color: var(--text); border-color: var(--muted); }

/* Below ~1000 px the page scrolls: scope, then the controls, then the other panels. */
@media (max-width: 1000px) {
  html, body { height: auto; }
  main { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto; }
  .scope { min-height: 0; }
  #scope { height: 62vh; flex: none; }
  #spectrum { height: 40vh; flex: none; }
  .controls { grid-row: 2; border-top: none; padding-top: 0; }
  aside { grid-row: 3; overflow: visible; padding: 0 10px 20px; display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 10px; align-items: start; }
  aside fieldset { margin: 0; }
  .stats { margin-left: 0; }
}

/* ------------------------------------------------------------------ analyzer view */

.view-switch button { padding: 3px 12px; }
.view-switch button.active { background: #2b3a55; border-color: var(--accent); }
body.analyzer .scope, body.analyzer aside, body.analyzer .controls, body.analyzer .acq, body.analyzer .save { display: none; }
body.analyzer #fps, body.analyzer #frame-no, body.analyzer #rate, body.analyzer .stats-sep { display: none; }
.an-view[hidden] { display: none; }
.an-view {
  grid-column: 1 / -1;
  grid-row: 1 / -1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  min-height: 0;
}
.an-plot { padding: 10px; min-height: 0; display: flex; }
#an-plot { flex: 1; width: 100%; min-height: 320px; background: #0d1117; border: 1px solid var(--border); border-radius: 8px; cursor: crosshair; }
.an-panel { min-height: 0; overflow-y: auto; padding: 10px 12px 20px 2px; }
.an-panel .note, .note { color: var(--muted); font-size: 12px; }
.an-panel p.note { margin: 2px 0 6px; }
.an-panel .cal-progress { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.an-panel .cal-progress span { color: var(--muted); font-size: 12px; }
.an-panel .err { color: var(--err); }
.wiring { width: 100%; height: auto; display: block; }
.wiring .box { fill: var(--panel-2); stroke: var(--muted); }
.wiring .lbl { fill: var(--text); font: 11px system-ui, sans-serif; }
.wiring .lbl.gen { fill: var(--trig); }
.wiring .lbl.a { fill: var(--a); font-weight: 600; }
.wiring .lbl.b { fill: var(--b); font-weight: 600; }
.wiring line { stroke-width: 2; }
.wiring .w-gen { stroke: var(--trig); }
.wiring .w-a { stroke: var(--a); }
.wiring .w-b { stroke: var(--b); }
.wiring .w-gnd { stroke: var(--muted); stroke-dasharray: 4 3; }
.wiring .dot-a { fill: var(--a); }
.wiring .dot-b { fill: var(--b); }

@media (max-width: 1000px) {
  .an-view { grid-template-columns: minmax(0, 1fr); }
  #an-plot { height: 60vh; flex: none; }
  .an-panel { padding: 0 10px 20px; overflow: visible; }
}
.wiring .w-wire, .wiring .w-part { stroke: var(--text); }
#an-wiring-response[hidden], #an-wiring-impedance[hidden] { display: none; }
.an-panel > .row { margin: 10px 0 8px; }
#an-re-row .note { flex: 1; }
#an-lvl-from, #an-lvl-to { width: 5.5em; }
