:root {
  color-scheme: light dark; /* native controls (scrollbars, spinners, dialogs) follow the theme */
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --border: #dde1e6;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --ok: #1a7f37;
  --warn: #9a6700;
  --warn-bg: #fff8c5;
  --bad: #cf222e;
  /* Here/peer pass the dataviz palette checks (CVD, contrast) on both surfaces; heat = the problem strip's hue. */
  --chart-here: #1f6feb;
  --chart-peer: #eb6834;
  --chart-heat: #4a3aa7;
  --chart-axis: #b9c0c8;
  --wire-out: #bc4c00;
  --wire-in: #1f6feb;
  --wire-pwr: #cf222e;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #181c22;
    --text: #e6e9ed;
    --muted: #8b96a5;
    --border: #2b323b;
    --accent: #4c8dff;
    --accent-text: #0b0d10;
    --ok: #3fb950;
    --warn: #d29922;
    --warn-bg: #2d2511;
    --bad: #f85149;
    --chart-here: #4c8dff;
    --chart-peer: #d95926;
    --chart-heat: #9085e9;
    --chart-axis: #46505c;
    --wire-out: #f0883e;
    --wire-in: #4c8dff;
    --wire-pwr: #f85149;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  /* Text stays selectable, but clicking it shows no I-beam or caret; fields keep theirs. */
  cursor: default;
  caret-color: transparent;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 14px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
h1 { font-size: 18px; margin: 0; }
.muted { color: var(--muted); }
a { color: var(--accent); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }

button, .button, input, select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 7px; padding: 6px 12px;
}
button, .button { cursor: pointer; display: inline-block; }
button:hover:not(:disabled), .button:hover { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--bad); }
input[type=number] { width: 110px; }
input, textarea, select { caret-color: auto; }
input:not([type]), input[type=text], input[type=number], textarea { cursor: text; }
input[aria-invalid=true] { border-color: var(--bad); }

.banner { padding: 10px 16px; border-bottom: 1px solid var(--border); }
.banner.warn { background: var(--warn-bg); color: var(--text); }

nav { display: flex; gap: 4px; padding: 8px 16px 0; overflow-x: auto; border-bottom: 1px solid var(--border); }
nav button { border: none; border-radius: 7px 7px 0 0; background: none; color: var(--muted); padding: 8px 14px; }
nav button.active { color: var(--text); background: var(--surface); box-shadow: inset 0 -2px 0 var(--accent); }
nav button.pending::after { content: ' •'; color: var(--warn); font-weight: 700; }
nav button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

main { padding: 16px; max-width: 1200px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; }
/* Disconnected: the status cards still show the last values; dim them so they don't read as live. */
body.offline #tab-status .grid { opacity: .55; }
/* The setup checklist and the wiring check are the connected board's: nothing to show without one. */
body.offline #setupCard, body.offline #wiringCheck { display: none; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; min-width: 0; }
.card.narrow { max-width: 640px; }
.card.wide { grid-column: 1 / -1; }
.label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }
.kv { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 4px 0; border-bottom: 1px dashed var(--border); }
.kv:last-child, .kv:last-of-type { border-bottom: none; } /* last-of-type: before a <details> */
/* A reading keeps its one-line width ("-60 dBm / 9.0 dB" split mid-unit before) and the label wraps instead;
   only a value wider than 70 % of the row wraps, at spaces. */
.kv span:first-child { flex: 1 1 auto; min-width: 0; }
.kv b { flex: 0 0 auto; max-width: 70%; font-weight: 600; text-align: right; overflow-wrap: break-word; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.row input.mono { flex: 1 1 260px; min-width: 0; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.spacer { flex: 1; }
/* A fieldset only to disable its controls together (disabled there, they stay disabled whatever they say). */
fieldset.bare { border: 0; padding: 0; min-width: 0; }
.check { display: inline-flex; gap: 6px; align-items: center; }

.good { color: var(--ok); }
.bad { color: var(--bad); }
progress { width: 100%; height: 8px; accent-color: var(--accent); }

.pill { display: inline-block; min-width: 38px; text-align: center; padding: 1px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); }
.pill.on { background: var(--ok); color: var(--surface); border-color: var(--ok); }

/* The health line, above every tab. Empty (no verdict), it takes no room but stays the live region, like the toast. */
.health-line { font-weight: 600; border-left: 4px solid var(--border); margin-bottom: 14px; }
.health-line.good { color: var(--ok); border-left-color: var(--ok); }
.health-line.warn { color: var(--warn); border-left-color: var(--warn); }
.health-line.bad { color: var(--bad); border-left-color: var(--bad); }
.health-line:empty { padding: 0; margin: 0; border: 0; }

/* Status tab */
details.more summary { cursor: pointer; color: var(--muted); padding: 4px 0; }
.gate-state { font-size: 34px; font-weight: 700; margin: 4px 0 10px; text-transform: capitalize; }
.gate-state.closed { color: var(--ok); }
.gate-state.open, .gate-state.between { color: var(--warn); }
.gate-state.fault, .gate-state.unknown, .gate-state.no_power { color: var(--bad); }
/* House with the link down: the state shown is the gate's last report, so it mustn't read as live. */
.gate-state.stale { color: var(--muted); text-decoration: line-through; text-decoration-thickness: 2px; }
.stale-note { color: var(--bad); font-weight: 600; margin: -6px 0 10px; }

/* Config tab */
.cfg-groups { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
/* Uniform rows: every setting row is the same height whatever its control (number, select or toggle). */
.field { position: relative; display: grid; grid-template-columns: 1fr 130px 18px; gap: 8px; align-items: center; padding: 4px 0; min-height: 46px; }
.field label { overflow-wrap: anywhere; min-width: 0; }
.field label .pname { display: block; color: var(--muted); font: 11.5px ui-monospace, SFMono-Regular, Consolas, monospace; }
/* The other role's groups and Advanced: folded below the rest, across the whole width. */
.cfg-fold { grid-column: 1 / -1; }
.cfg-fold > summary { cursor: pointer; color: var(--muted); padding: 6px 0; }
.cfg-fold > summary:not([data-dirty=""])::after { content: ' · ' attr(data-dirty); color: var(--warn); font-weight: 600; }
.cfg-fold > .cfg-groups { margin-top: 8px; }
.must { color: var(--warn); font-weight: 700; margin-left: 2px; }
/* Info icon + tooltip for each setting: shown on hover, keyboard focus, or pinned by a tap/click. */
.info {
  flex: none; width: 18px; height: 18px; padding: 0; border-radius: 50%; font: italic 600 11px/16px Georgia, serif;
  text-align: center; color: var(--muted); background: none; border: 1px solid var(--border); cursor: help;
}
.info:hover, .info:focus-visible { color: var(--accent); border-color: var(--accent); }
.info:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tip {
  display: none; position: absolute; left: 0; right: 0; top: calc(100% - 2px); z-index: 5; padding: 8px 10px;
  font-size: 13px; line-height: 1.4; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 4px 16px rgba(0, 0, 0, .18);
}
.info:hover + .tip, .info:focus-visible + .tip, .tip.show { display: block; }
.field input, .field select { width: 100%; height: 36px; }
.field.dirty input, .field.dirty select { border-color: var(--warn); }
.field input.toggle {
  appearance: none; width: 40px; height: 22px; padding: 0; margin: 0; border-radius: 999px; justify-self: end;
  position: relative; cursor: pointer; background: var(--border); border: 1px solid var(--border); transition: background .15s;
}
.field input.toggle::after {
  content: ''; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--surface); transition: transform .15s;
}
.field input.toggle:checked { background: var(--accent); border-color: var(--accent); }
.field input.toggle:checked::after { transform: translateX(18px); }
.field input.toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.field input.toggle:disabled { opacity: .5; cursor: default; }
.field.dirty input.toggle { box-shadow: 0 0 0 2px var(--warn); }

/* Link tab: ping */
#rssiChart { width: 100%; height: 90px; margin-top: 8px; background: var(--bg); border-radius: 6px; }
#rssiChart polyline { fill: none; stroke-width: 2; vector-effect: non-scaling-stroke; }
.legend { display: flex; gap: 10px; align-items: center; font-size: 12px; margin-top: 4px; flex-wrap: wrap; }
.sw { display: inline-block; width: 12px; height: 3px; border-radius: 2px; }
.sw.here { background: var(--chart-here); }
.sw.peer { background: var(--chart-peer); }

/* Link tab: site survey */
.verdict-row { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; margin: 6px 0 10px; }
.verdict { font-size: 22px; font-weight: 700; }
.verdict.good { color: var(--ok); }
.verdict.marginal { color: var(--warn); }
.verdict.poor { color: var(--bad); }
.lvl-good { color: var(--ok); }
.lvl-marginal { color: var(--warn); }
#surveyTable td, #surveyTable th { white-space: nowrap; font-variant-numeric: tabular-nums; }

/* Link tab: history */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin: 4px 0 10px; }
.tile { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-width: 0; }
.tile .t-label { font-size: 12px; color: var(--muted); }
.tile .t-value { font-size: 22px; font-weight: 600; line-height: 1.3; }
.tile .t-sub { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.sw.line { height: 2px; }
.sw.band { height: 10px; opacity: .25; background: var(--muted); }
.sw.heat { height: 10px; background: var(--chart-heat); }
.sw.reset { width: 0; height: 12px; border-left: 1px dashed var(--muted); border-radius: 0; }
.hist-plot { position: relative; margin-top: 6px; transition: opacity .15s; }
.hist-plot.loading { opacity: .5; }
#histChart { display: block; width: 100%; touch-action: pan-y; }
#histChart:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
#histChart text { fill: var(--muted); font: 11px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-variant-numeric: tabular-nums; }
#histChart text.ptitle { fill: var(--text); font-size: 12px; font-weight: 600; }
#histChart .grid { stroke: var(--border); stroke-width: 1; }
#histChart .zero { stroke: var(--chart-axis); stroke-width: 1; }
#histChart .ln { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
#histChart .ln.here { stroke: var(--chart-here); }
#histChart .ln.peer { stroke: var(--chart-peer); }
#histChart .band { stroke: none; opacity: .12; }
#histChart .band.here, #histChart .dot.here { fill: var(--chart-here); }
#histChart .band.peer, #histChart .dot.peer { fill: var(--chart-peer); }
#histChart .dot { stroke: var(--surface); stroke-width: 2; }
#histChart .track { fill: var(--bg); }
#histChart .cross { stroke: var(--muted); stroke-width: 1; }
#histChart line.reset { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 4 3; }
#histTable tr.reset td { border-bottom: 2px dashed var(--muted); }
#histChart .hit { fill: transparent; }
.hist-tip {
  position: absolute; top: 0; z-index: 2; pointer-events: none; min-width: 190px; padding: 8px 10px; font-size: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 4px 16px rgba(0, 0, 0, .18);
}
.hist-tip .tt { font-weight: 600; margin-bottom: 2px; }
.hist-tip .note { color: var(--muted); font-size: 11px; }
.hist-tip .sec { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; margin-top: 6px; }
.hist-tip .r { display: flex; align-items: center; gap: 6px; }
.hist-tip .r b { min-width: 72px; text-align: right; font-variant-numeric: tabular-nums; }
.hist-tip .r span:last-child { color: var(--muted); }
.hist-tip .key { display: inline-block; width: 12px; height: 2px; border-radius: 1px; background: var(--muted); }
.hist-tip .key:not(.here):not(.peer) { visibility: hidden; }
.hist-tip .key.here { background: var(--chart-here); }
.hist-tip .key.peer { background: var(--chart-peer); }
.hist-table { margin-top: 10px; }
.hist-table summary { cursor: pointer; color: var(--muted); }
.hist-table td, .hist-table th { white-space: nowrap; font-variant-numeric: tabular-nums; }

/* Link tab: the gate over the link; Log tab */
pre { white-space: pre-wrap; overflow-wrap: anywhere; margin: 8px 0; }
.log { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; height: 60vh; overflow: auto; font-size: 12px; }
.log div { white-space: pre-wrap; overflow-wrap: anywhere; }
.log .raw { color: var(--muted); }
.log .err { color: var(--bad); }

/* Install tab */
#setupCard { margin-bottom: 14px; }
.setup { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.setup li { display: flex; gap: 8px; align-items: baseline; }
.setup .tick { flex: none; width: 1.2em; text-align: center; font-weight: 700; color: var(--muted); }
.setup li.done .tick { color: var(--ok); }
/* Read out, not shown (the tick says it on screen). */
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 7px; overflow: hidden; }
.seg button { border: none; border-radius: 0; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--accent); color: var(--accent-text); }
.wiring { overflow-x: auto; }
.wiring svg { display: block; width: 100%; max-width: 860px; height: auto; }
.wiring text { fill: var(--text); font: 13px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.wiring text.dim { fill: var(--muted); font-size: 11.5px; }
.wiring text.title { font-weight: 600; font-size: 14px; }
.wiring text.term { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12.5px; }
.wiring .blk { fill: var(--bg); stroke: var(--border); stroke-width: 1.5; }
.wiring .blk.board { stroke: var(--accent); }
.wiring line { stroke-width: 2.5; }
.wiring circle { fill: var(--surface); stroke-width: 2; }
.wiring .w-out, .sw.w-out { stroke: var(--wire-out); background: var(--wire-out); }
.wiring .w-in, .sw.w-in { stroke: var(--wire-in); background: var(--wire-in); }
.wiring .w-pwr, .sw.w-pwr { stroke: var(--wire-pwr); background: var(--wire-pwr); }
.check-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 1fr)); gap: 8px 24px; align-items: start; }
.install-grid { margin-top: 14px; grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); }
.table-wrap { overflow-x: auto; }
.wtable { width: 100%; border-collapse: collapse; }
.wtable th, .wtable td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.wtable th { font-size: 12px; color: var(--muted); font-weight: 600; }
.wtable td.dev small { display: block; color: var(--muted); }
.wtable td.mono { white-space: nowrap; }
.notes { margin: 0; padding-left: 18px; }
.notes li { margin: 0 0 8px; }
.notes code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12.5px; }

/* Board picker: the page's own list of granted ports, named by each board's role. */
dialog {
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; width: min(460px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.pick-list { list-style: none; margin: 0 0 4px; padding: 0; display: grid; gap: 8px; }
.pick-item { display: flex; gap: 8px; }
.pick-main { flex: 1; min-width: 0; text-align: left; display: flex; flex-direction: column; gap: 2px; padding: 10px 12px; }
.pick-main b { font-weight: 600; }
.pick-main small { color: var(--muted); }
.pick-item:not(.ok) .pick-main b { color: var(--muted); }

/* Passphrase dialog (Board: key backups). */
.pass-field { display: grid; gap: 4px; margin: 10px 0; }
.pass-field input { width: 100%; }
#passError:empty, #passWarn:empty { display: none; }

/* Toast: non-modal notices. Always present (it's the live region); empty = invisible. */
.toast {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 10;
  width: max-content; max-width: calc(100% - 32px); padding: 10px 14px; cursor: pointer;
  background: var(--surface); color: var(--text); border: 1px solid var(--accent); border-left-width: 4px;
  border-radius: var(--radius); box-shadow: 0 4px 16px rgba(0, 0, 0, .18); overflow-wrap: anywhere;
}
.toast.err { border-color: var(--bad); }
.toast:empty { padding: 0; border: 0; box-shadow: none; }

@media (max-width: 520px) {
  /* Every tab in view on a phone (Install is the one that works there): tighter, and wrapping if still too wide. */
  nav { padding: 6px 8px 0; gap: 0; flex-wrap: wrap; }
  nav button { padding: 8px 10px; }
  .install-grid { grid-template-columns: 1fr; }
  .gate-state { font-size: 28px; }
}

/* Touch: taller rows and bigger targets (16 px text also stops phones zooming into a field on tap). */
@media (max-width: 520px), (pointer: coarse) {
  .field { min-height: 56px; grid-template-columns: 1fr 130px 28px; }
  .field input, .field select { height: 44px; font-size: 16px; }
  .field input.toggle { width: 50px; height: 30px; }
  .field input.toggle::after { width: 24px; height: 24px; }
  .field input.toggle:checked::after { transform: translateX(20px); }
  .info { width: 28px; height: 28px; font-size: 14px; line-height: 26px; }
}
@media (max-width: 520px) {
  .field { grid-template-columns: 1fr 112px 28px; }
}
