/* ESP32 Wi-Fi Tester. Theme-aware instrument console styling.
   Light is the base palette. Dark follows the OS unless the toggle forces a
   theme, in which case data-theme on the root wins in both directions. */

:root {
  --bg: #f4f1ea;
  --bg-grid: rgba(15, 118, 110, 0.05);
  --surface: #fffdf8;
  --surface-2: #f2ede2;
  --surface-3: #e9e3d5;
  --border: #e3ddce;
  --border-strong: #d0c8b4;
  --ink: #211f19;
  --ink-2: #45423a;
  --mute: #6d685b;
  --dim: #8a8474;
  --faint: #a89f8b;

  --accent: #0f766e;
  --accent-strong: #0b5a53;
  --accent-bright: #14b8a6;
  --accent-soft: rgba(15, 118, 110, 0.12);
  --accent-line: rgba(15, 118, 110, 0.34);

  --ok: #157f43;
  --ok-soft: rgba(21, 127, 67, 0.12);
  --warn: #b45309;
  --warn-soft: rgba(180, 83, 9, 0.13);
  --err: #bb2d22;
  --err-soft: rgba(187, 45, 34, 0.11);

  --console-bg: #0d1b1e;
  --console-fg: #82e6df;
  --console-dim: #4f8785;
  --console-line: #06110f;

  --shadow-sm: 0 1px 2px rgba(60, 48, 24, 0.05);
  --shadow-md: 0 6px 22px rgba(60, 48, 24, 0.08);
  --shadow-pop: 0 24px 70px rgba(30, 26, 18, 0.22);

  --sans: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", "Cascadia Code", Consolas, "Liberation Mono", monospace;
  --r-sm: 7px;
  --r-md: 11px;
  --r-lg: 16px;
  --rail-width: 232px;
  --rail-gap: 60px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0d1113;
  --bg-grid: rgba(45, 212, 191, 0.05);
  --surface: #161b1e;
  --surface-2: #1d2327;
  --surface-3: #262d32;
  --border: #293136;
  --border-strong: #3a444b;
  --ink: #e9ebed;
  --ink-2: #c6ccd0;
  --mute: #8c959d;
  --dim: #6e7880;
  --faint: #545f66;

  --accent: #2dd4bf;
  --accent-strong: #5eead4;
  --accent-bright: #5eead4;
  --accent-soft: rgba(45, 212, 191, 0.13);
  --accent-line: rgba(45, 212, 191, 0.32);

  --ok: #4ade80;
  --ok-soft: rgba(74, 222, 128, 0.13);
  --warn: #fbbf24;
  --warn-soft: rgba(251, 191, 36, 0.13);
  --err: #f87171;
  --err-soft: rgba(248, 113, 113, 0.13);

  --console-bg: #08120f;
  --console-fg: #7fe3d8;
  --console-dim: #47786f;
  --console-line: #030a08;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 8px 26px rgba(0, 0, 0, 0.42);
  --shadow-pop: 0 26px 80px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1113;
    --bg-grid: rgba(45, 212, 191, 0.05);
    --surface: #161b1e;
    --surface-2: #1d2327;
    --surface-3: #262d32;
    --border: #293136;
    --border-strong: #3a444b;
    --ink: #e9ebed;
    --ink-2: #c6ccd0;
    --mute: #8c959d;
    --dim: #6e7880;
    --faint: #545f66;
    --accent: #2dd4bf;
    --accent-strong: #5eead4;
    --accent-bright: #5eead4;
    --accent-soft: rgba(45, 212, 191, 0.13);
    --accent-line: rgba(45, 212, 191, 0.32);
    --ok: #4ade80;
    --ok-soft: rgba(74, 222, 128, 0.13);
    --warn: #fbbf24;
    --warn-soft: rgba(251, 191, 36, 0.13);
    --err: #f87171;
    --err-soft: rgba(248, 113, 113, 0.13);
    --console-bg: #08120f;
    --console-fg: #7fe3d8;
    --console-dim: #47786f;
    --console-line: #030a08;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 8px 26px rgba(0, 0, 0, 0.42);
    --shadow-pop: 0 26px 80px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-height: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  font: 15px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
}
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(1000px 340px at 32% -80px, var(--accent-soft), transparent 70%),
    linear-gradient(var(--bg-grid) 1px, transparent 1px) 0 0 / 100% 28px,
    linear-gradient(90deg, var(--bg-grid) 1px, transparent 1px) 0 0 / 28px 100%;
  opacity: 0.7;
}
button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: inherit; }
::selection { background: var(--accent-soft); }

.shell { position: relative; z-index: 1; max-width: 1340px; margin: auto; padding: 34px 48px 84px; }
.layout { display: grid; grid-template-columns: var(--rail-width) minmax(0, 1fr); gap: var(--rail-gap); align-items: start; }

/* Rail */
.rail { position: sticky; top: 30px; align-self: start; max-height: calc(100vh - 60px); overflow-y: auto; padding-right: 8px; }
.rail-inner { min-height: calc(100vh - 60px); display: flex; flex-direction: column; gap: 26px; }
.brand { display: flex; align-items: center; gap: 12px; text-decoration: none; }
.brand-mark { width: 34px; height: 34px; flex: none; display: grid; place-items: center; border-radius: 9px; background: linear-gradient(150deg, var(--accent-bright), var(--accent-strong)); box-shadow: 0 0 0 4px var(--accent-soft); }
.brand-mark::after { content: ""; width: 16px; height: 14px; background: linear-gradient(#fff 0 0) 0 7px/4px 7px no-repeat, linear-gradient(#fff 0 0) 6px 3px/4px 11px no-repeat, linear-gradient(#fff 0 0) 12px 0/4px 14px no-repeat; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-name { font-weight: 680; line-height: 1.2; letter-spacing: -0.01em; }
.brand-sub { color: var(--dim); font: 11.5px/1.4 var(--mono); }
.rail-label { margin-bottom: 9px; color: var(--faint); font: 10.5px/1.2 var(--mono); letter-spacing: 0.09em; text-transform: uppercase; }
.toc { display: flex; flex-direction: column; gap: 2px; }
.toc-item { position: relative; display: grid; grid-template-columns: 28px 1fr; gap: 8px; align-items: center; margin-left: -10px; padding: 8px 10px; border-radius: 8px; color: var(--mute); text-decoration: none; font-size: 13.5px; font-weight: 500; transition: 120ms ease; }
.toc-item:hover { color: var(--ink); background: var(--surface-2); }
.toc-item.is-active { color: var(--ink); background: var(--surface-2); }
.toc-item.is-active::before { content: ""; position: absolute; left: -1px; top: 8px; bottom: 8px; width: 2.5px; border-radius: 2px; background: var(--accent); }
.toc-num { color: var(--faint); font: 11px var(--mono); }
.toc-item.is-active .toc-num { color: var(--accent); }
.rail-status { margin-top: auto; }
.service-line { display: flex; align-items: center; gap: 8px; color: var(--mute); font: 12px var(--mono); }
.status-dot { width: 8px; height: 8px; border-radius: 99px; background: var(--dim); box-shadow: 0 0 0 4px rgba(138, 132, 114, 0.14); }
.status-dot.ok { background: var(--ok); box-shadow: 0 0 0 4px var(--ok-soft); }
.status-dot.err { background: var(--err); box-shadow: 0 0 0 4px var(--err-soft); }
.status-dot.pending { background: var(--warn); box-shadow: 0 0 0 4px var(--warn-soft); }
.status-dot.run { background: var(--accent-bright); box-shadow: 0 0 0 4px var(--ok-soft); animation: run-pulse 1.6s ease-in-out infinite; }
@keyframes run-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
.run-line { display: flex; align-items: center; gap: 8px; margin-top: 8px; padding: 7px 9px; border: 1px solid var(--border); border-radius: var(--r-sm); color: var(--text); font: 12px var(--mono); text-decoration: none; }
.run-line:hover { border-color: var(--ok); }
.run-monitor { margin-bottom: 22px; }
.rail-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding-top: 14px; border-top: 1px solid var(--border); color: var(--faint); font: 10.5px var(--mono); }
.theme-toggle { display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border: 1px solid var(--border-strong); border-radius: 99px; background: var(--surface); color: var(--mute); cursor: pointer; font: 10.5px var(--mono); letter-spacing: 0.04em; transition: 120ms ease; }
.theme-toggle:hover { color: var(--ink); border-color: var(--accent-line); }

/* Main + hero */
.main { min-width: 0; }
.view { display: none; }
.view.is-visible { display: block; animation: reveal 160ms ease-out; }
@keyframes reveal { from { opacity: 0.3; transform: translateY(3px); } }
.hero { padding-top: 2px; margin-bottom: 30px; }
.hero.compact h1 { max-width: 22ch; margin: 0 0 10px; font-size: clamp(28px, 3.6vw, 38px); line-height: 1.08; letter-spacing: -0.03em; font-weight: 680; text-wrap: balance; }
.hero p:not(.eyebrow) { max-width: 66ch; margin: 0; color: var(--mute); font-size: 15px; }
.hero .accent { color: var(--accent); }
.eyebrow { margin: 0 0 10px; color: var(--accent); font: 11px var(--mono); letter-spacing: 0.14em; }
.hero.compact { position: relative; }

/* Panels */
.panel { padding: 30px 0; border-top: 1px solid var(--border); }
.first-panel { padding-top: 24px; }
.panel-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.panel-head .num { padding: 3px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); color: var(--mute); font: 10.5px var(--mono); letter-spacing: 0.05em; }
.panel-head h2 { margin: 0; font-size: 19px; font-weight: 660; letter-spacing: -0.015em; }
.push-right { margin-left: auto; }
.lede { max-width: 68ch; margin: -4px 0 18px; color: var(--mute); }
.empty { margin: 0; color: var(--dim); font-style: italic; }

/* Stat cards */
.stat-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; margin: 0 0 30px; }
.stat-card { position: relative; min-width: 0; padding: 15px 16px; border: 1px solid var(--border); border-radius: var(--r-md); background: var(--surface); box-shadow: var(--shadow-sm); overflow: hidden; }
.stat-card::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--border-strong); }
.stat-card.pass::before { background: var(--ok); }
.stat-card.warn::before { background: var(--warn); }
.stat-card.fail::before { background: var(--err); }
.stat-value { display: block; color: var(--ink); font: 660 26px/1.1 var(--mono); }
.stat-label { display: block; margin-top: 5px; color: var(--dim); font-size: 11px; text-transform: uppercase; letter-spacing: 0.07em; }
.stat-card.pass .stat-value { color: var(--ok); }
.stat-card.warn .stat-value { color: var(--warn); }
.stat-card.fail .stat-value { color: var(--err); }

/* Cards + grids */
.port-grid, .device-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.port-card, .device-card, .activity-card, .inset-card { padding: 15px 16px; border: 1px solid var(--border); border-radius: var(--r-md); background: var(--surface); }
.port-head, .device-head { display: flex; align-items: center; gap: 10px; }
.port-name, .mac { font-family: var(--mono); font-weight: 600; }
.port-meta, .device-meta { margin-top: 7px; color: var(--dim); font: 12px/1.5 var(--mono); overflow-wrap: anywhere; }
.inset-card { background: var(--surface-2); }
.inset-card.muted { color: var(--mute); }
.custom-profile { margin-top: 16px; }
.device-card { cursor: pointer; transition: border-color 120ms ease, transform 120ms ease, box-shadow 120ms ease; }
.device-card:hover { border-color: var(--accent-line); transform: translateY(-1px); box-shadow: var(--shadow-md); }
.device-photo { width: 46px; height: 46px; flex: none; border-radius: 9px; object-fit: cover; background: var(--surface-3); }
.device-placeholder { display: grid; place-items: center; color: var(--accent); font: 12px var(--mono); }
.device-name { font-weight: 660; }
.device-actions { display: flex; align-items: center; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.device-actions .button { min-height: 32px; padding: 6px 10px; font-size: 12px; }

/* Badges */
.badge { display: inline-flex; align-items: center; min-height: 22px; padding: 2px 9px; border-radius: 99px; background: var(--surface-2); color: var(--mute); font: 10.5px var(--mono); letter-spacing: 0.04em; border: 1px solid transparent; }
.badge.pass, .badge.completed { color: var(--ok); background: var(--ok-soft); }
.badge.warn, .badge.running { color: var(--warn); background: var(--warn-soft); }
.badge.fail, .badge.error { color: var(--err); background: var(--err-soft); }
.badge.inconclusive, .badge.queued { color: var(--accent); background: var(--accent-soft); }

/* Tables */
.table-wrap { width: 100%; overflow-x: auto; border: 1px solid var(--border); border-radius: var(--r-md); background: var(--surface); }
table { width: 100%; border-collapse: collapse; min-width: 680px; }
th { padding: 10px 13px; border-bottom: 1px solid var(--border); color: var(--dim); background: var(--surface-2); font: 10.5px var(--mono); letter-spacing: 0.07em; text-align: left; text-transform: uppercase; position: sticky; top: 0; }
td { padding: 11px 13px; border-bottom: 1px solid var(--border); color: var(--mute); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr[data-link] { cursor: pointer; transition: background 100ms ease; }
tbody tr[data-link]:hover { background: var(--accent-soft); }
.mono { font-family: var(--mono); }

/* Forms */
.form-grid { display: grid; gap: 15px; }
.form-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.field { display: flex; flex-direction: column; gap: 7px; color: var(--dim); font: 10.5px var(--mono); letter-spacing: 0.06em; text-transform: uppercase; }
input, select, textarea { width: 100%; min-height: 43px; padding: 10px 13px; border: 1px solid var(--border-strong); border-radius: var(--r-md); outline: none; background: var(--surface); color: var(--ink); box-shadow: var(--shadow-sm); transition: 120ms ease; text-transform: none; letter-spacing: 0; }
textarea { min-height: 110px; resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--faint); }
input:focus, select:focus, textarea:focus { border-color: var(--accent-line); box-shadow: 0 0 0 3px var(--accent-soft); }
.compact-input { max-width: 260px; min-height: 36px; padding: 7px 11px; }
.field-help { margin: 12px 0 0; color: var(--dim); font-size: 12px; line-height: 1.5; }
.port-check-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.port-check { display: inline-flex; align-items: center; gap: 7px; min-height: 34px; padding: 6px 10px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface); color: var(--mute); font: 11px var(--mono); cursor: pointer; }
.port-check input { width: 15px; min-height: 15px; margin: 0; padding: 0; box-shadow: none; }

/* Board selection list */
.muted { color: var(--dim); }
.port-select-toolbar { display: flex; align-items: flex-end; gap: 14px; margin-bottom: 14px; }
.port-select-toolbar .field { flex: 1 1 auto; }
.port-select-list { display: flex; flex-direction: column; gap: 8px; }
.port-row { display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 12px; padding: 11px 14px; border: 1px solid var(--border-strong); border-radius: var(--r-md); background: var(--surface); transition: border-color 120ms ease, background 120ms ease, box-shadow 120ms ease; }
.port-row.is-checked { border-color: var(--accent-line); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent-line); }
.port-row.is-reference { border-style: dashed; background: var(--surface-2); }
.port-row-main { display: flex; align-items: center; gap: 12px; min-width: 0; margin: 0; cursor: pointer; color: var(--mute); font: 12px/1.5 var(--mono); text-transform: none; letter-spacing: 0; }
.port-row-main input { width: 17px; min-height: 17px; flex: none; margin: 0; padding: 0; box-shadow: none; accent-color: var(--accent); }
.port-row.is-reference .port-row-main { cursor: default; }
.port-row-name { flex: none; color: var(--ink); font-weight: 660; font-size: 13px; }
.port-row-id { min-width: 0; overflow-wrap: anywhere; }
.port-row-tags { display: flex; gap: 6px; }
.ap-pick-row { display: flex; gap: 8px; }
.ap-pick-row select { flex: 1 1 auto; }
.ap-pick-row .button { flex: none; }

/* Buttons */
.button { display: inline-flex; justify-content: center; align-items: center; gap: 6px; min-height: 37px; padding: 8px 14px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface); color: var(--ink-2); cursor: pointer; font-weight: 600; transition: 120ms ease; }
.button:hover { transform: translateY(-1px); border-color: var(--accent-line); box-shadow: var(--shadow-sm); }
.button:active { transform: none; }
.button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }
.button.primary { border-color: var(--accent-strong); background: var(--accent); color: #04211e; }
.button.primary:hover { background: var(--accent-bright); }
.button.danger { border-color: color-mix(in srgb, var(--err) 60%, #000); background: var(--err); color: #fff; }
.button.ghost { background: transparent; }
.button.small { min-height: 30px; padding: 5px 11px; font-size: 12px; }
.button.large { min-height: 46px; padding: 11px 22px; }
.button-group { display: flex; gap: 8px; }
.action-row { display: flex; align-items: center; gap: 14px; padding: 20px 0; flex-wrap: wrap; }
.status-line { color: var(--mute); font: 12px var(--mono); }
.status-line.ok { color: var(--ok); }
.status-line.warn { color: var(--warn); }
.status-line.error { color: var(--err); }

/* Console + charts */
/* Tall enough for a whole stage without scrolling, and draggable from the
   bottom edge when a run has more to say than that. */
.crt-console { min-height: 300px; max-height: 75vh; overflow: auto; resize: vertical; margin: 14px 0 0; padding: 15px; border: 1px solid var(--console-line); border-radius: var(--r-md); background: var(--console-bg); color: var(--console-fg); font: 12px/1.55 var(--mono); white-space: pre-wrap; box-shadow: inset 0 0 30px rgba(0, 0, 0, 0.32); }
.chart-frame { height: 340px; padding: 14px; border: 1px solid var(--border); border-radius: var(--r-md); background: var(--surface); }
.chart-frame.comparison-chart { height: 430px; }
/* The summary strip grows with the number of boards, so its height is set from
   the script rather than here. */
.chart-frame.comparison-summary { height: auto; margin-bottom: 12px; }
.chart-caption { margin: 0 0 6px; color: var(--faint); font: 10.5px/1.2 var(--mono); letter-spacing: 0.09em; text-transform: uppercase; }
.preflight-card { margin-bottom: 8px; padding: 13px 15px; border: 1px solid var(--accent-line); border-radius: var(--r-md); background: var(--accent-soft); color: var(--accent-strong); font: 12px/1.5 var(--mono); }
.preflight-card.error { border-color: var(--err); background: var(--err-soft); color: var(--err); }
.preflight-card a { font-weight: 700; }

/* Detail views */
.details-grid { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(260px, 0.6fr); gap: 18px; }
.spec-list { margin: 0; }
.spec-row { display: grid; grid-template-columns: 150px 1fr; gap: 14px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.spec-row dt { color: var(--dim); font: 11px var(--mono); text-transform: uppercase; }
.spec-row dd { margin: 0; overflow-wrap: anywhere; }
.back-link { margin: 2px 0 22px; padding: 0; border: 0; background: transparent; color: var(--accent); cursor: pointer; font-weight: 600; }
.back-link::before { content: "\2190  "; }
.warning-box { padding: 12px; border: 1px solid var(--err); border-radius: 8px; background: var(--err-soft); color: var(--err); }

/* Dialog + toast */
.dialog { width: min(480px, calc(100% - 32px)); padding: 24px; border: 1px solid var(--border-strong); border-radius: var(--r-lg); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-pop); }
.dialog::backdrop { background: rgba(10, 14, 16, 0.5); backdrop-filter: blur(2px); }
.dialog h2 { margin: 7px 0 10px; font-size: 23px; }
.dialog-kicker { color: var(--err); font: 10.5px var(--mono); letter-spacing: 0.11em; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 22px; }
.toast { position: fixed; z-index: 10; right: 24px; bottom: 24px; max-width: 380px; padding: 12px 15px; border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--ink); opacity: 0; transform: translateY(8px); pointer-events: none; transition: 160ms ease; box-shadow: var(--shadow-pop); }
.toast.show { opacity: 1; transform: none; }

/* Responsive */
@media (max-width: 980px) {
  .shell { padding: 24px 26px 64px; }
  .layout { grid-template-columns: 186px minmax(0, 1fr); gap: 34px; }
  .stat-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .port-grid, .device-grid { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
  .shell { padding: 16px 14px 48px; }
  .layout { display: block; }
  .rail { position: relative; top: 0; max-height: none; padding: 0 0 22px; }
  .rail-inner { min-height: 0; gap: 16px; }
  .toc { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); overflow-x: auto; }
  .toc .rail-label, .toc-num, .rail-status { display: none; }
  .toc-item { display: block; margin: 0; padding: 8px 6px; text-align: center; font-size: 11px; white-space: nowrap; }
  .toc-item.is-active::before { left: 8px; right: 8px; top: auto; bottom: 0; width: auto; height: 2px; }
  .hero.compact h1 { font-size: 29px; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid.two, .details-grid { grid-template-columns: 1fr; }
  .panel-head { flex-wrap: wrap; }
  .compact-input { max-width: none; flex-basis: 100%; margin-left: 0; }
  .action-row, .button-group, .port-select-toolbar { flex-wrap: wrap; }
  .action-row .button { flex: 1 1 auto; }
  .port-row { grid-template-columns: 1fr auto; }
  .port-row .button.small { grid-column: 1 / -1; }
}

.analysis-why {
  margin: 6px 0 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
  color: var(--ink-2);
}
