:root {
  --bg: #0f1216;
  --surface-1: #161a20;
  --surface-2: #1d222a;
  --surface-3: #262c36;
  --border: #2e3541;
  --text: #e6e9ee;
  --muted: #8d97a6;
  --accent: #f5b301;
  --accent-ink: #1a1400;
  --ok: #34c77b;
  --warn: #f5b301;
  --err: #ff5a5a;
  --info: #5aa9ff;
  --can-h: #ffb020;
  --can-l: #3ec7ff;
  --wire: #9aa4b2;
  --wire-pos: #ff6b5a;
  --wire-gnd: #6e7785;
  --def: #4fa3ff;
  --scope-bg: #071510;
  --scope-grid: #1c3a2c;
  --lcd-bg: #b9c7a6;
  --lcd-ink: #1b2412;
  --radius: 10px;
  --gap: 14px;
  color-scheme: dark;
}

:root[data-theme='light'] {
  --bg: #f2f4f7;
  --surface-1: #ffffff;
  --surface-2: #f6f7f9;
  --surface-3: #eceff3;
  --border: #d6dbe2;
  --text: #1d232b;
  --muted: #647081;
  --accent: #d99a00;
  --accent-ink: #1a1400;
  --ok: #138a4f;
  --warn: #b77800;
  --err: #d62f2f;
  --info: #1f6fd1;
  --can-h: #d97a00;
  --can-l: #0a8fc2;
  --wire: #6d7785;
  --wire-pos: #d6402f;
  --wire-gnd: #8a93a0;
  --def: #1f6fd1;
  --scope-bg: #071510;
  --scope-grid: #1c3a2c;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, sans-serif;
}

h1, h2, h3 { margin: 0; font-weight: 650; }
h2 { font-size: 0.95rem; margin-bottom: 10px; letter-spacing: 0.01em; }
h3 { font-size: 0.82rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin: 14px 0 6px; }

/* ---------------------------------------------------------------- topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 1.05rem; }
.brand p { margin: 0; color: var(--muted); font-size: 0.8rem; }
.brand__mark {
  width: 34px; height: 34px; border-radius: 8px;
  background: var(--accent);
  box-shadow: inset 0 -10px 0 rgba(0, 0, 0, 0.18);
}
.topbar__controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------------------------------------------------------------- layout */
.layout {
  display: grid;
  grid-template-columns: 290px minmax(0, 1fr) 430px;
  gap: var(--gap);
  padding: var(--gap);
  align-items: start;
}
.col { display: flex; flex-direction: column; gap: var(--gap); min-width: 0; }

@media (max-width: 1320px) {
  .layout { grid-template-columns: 280px minmax(0, 1fr); }
  .col--tools { grid-column: 1 / -1; }
}
@media (max-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 10px; }
  .col--main { order: -1; }
  .topbar { padding: 10px 12px; }
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  min-width: 0;
}
.card--view { padding: 8px; }

/* ---------------------------------------------------------------- controls */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 8px;
  height: 32px;
  padding: 0 12px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-3); }
.btn:active { transform: translateY(1px); }
.btn--accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn--accent:hover { background: var(--accent); filter: brightness(1.06); }
.btn--block { width: 100%; margin-top: 8px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.field { display: block; margin: 12px 0 0; font-size: 0.82rem; color: var(--muted); }
.field output { float: right; color: var(--text); font-variant-numeric: tabular-nums; }
.field input[type='range'] { width: 100%; margin-top: 6px; accent-color: var(--accent); }
.field select, .field--inline select {
  display: block; width: 100%; margin-top: 4px; height: 32px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 0 8px; font: inherit;
}
.field--inline { margin: 0; display: flex; align-items: center; gap: 6px; }
.field--inline select { margin: 0; width: auto; }
.label { display: block; font-size: 0.82rem; color: var(--muted); margin-bottom: 6px; }

.seg {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.seg button {
  appearance: none; border: 0; background: var(--surface-2); color: var(--muted);
  height: 32px; font: inherit; font-size: 0.82rem; font-weight: 600; cursor: pointer;
  border-right: 1px solid var(--border); padding: 0 6px; user-select: none;
}
.seg button:last-child { border-right: 0; }
.seg button.is-on { background: var(--accent); color: var(--accent-ink); }
.seg--wide { margin: 10px 0; }

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs button {
  appearance: none; border: 1px solid var(--border); background: var(--surface-1);
  color: var(--muted); border-radius: 8px; height: 32px; padding: 0 12px;
  font: inherit; font-size: 0.84rem; font-weight: 600; cursor: pointer; white-space: nowrap;
  user-select: none;
}
.tabs button.is-on { color: var(--text); border-color: var(--accent); box-shadow: inset 0 -2px 0 var(--accent); }

.key .seg { margin-bottom: 0; }

.tank {
  position: relative; height: 26px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-2); overflow: hidden;
}
.tank__fill { position: absolute; inset: 0 auto 0 0; width: 85%; background: var(--def); opacity: 0.55; transition: width 0.3s; }
.tank span { position: relative; display: block; text-align: center; line-height: 24px; font-weight: 650; font-size: 0.82rem; }
.tank.is-frozen { background: repeating-linear-gradient(45deg, var(--surface-2) 0 6px, var(--surface-3) 6px 12px); }

.hint { margin: 8px 4px 2px; color: var(--muted); font-size: 0.8rem; }
.note { color: var(--muted); font-size: 0.8rem; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid2 .field { margin-top: 8px; }
.checks { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; font-size: 0.82rem; }
.checks input, .switch input { accent-color: var(--accent); }
.switch { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; }

/* ---------------------------------------------------------------- canvases */
canvas { display: block; width: 100%; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation; }
.view { aspect-ratio: 1000 / 620; border-radius: 8px; background: var(--surface-2); cursor: crosshair; }
.cluster { aspect-ratio: 260 / 300; border-radius: 8px; }
.scope { aspect-ratio: 400 / 250; border-radius: 6px; background: var(--scope-bg); }

/* ---------------------------------------------------------------- meter */
.lcd {
  background: var(--lcd-bg); color: var(--lcd-ink);
  border-radius: 8px; padding: 10px 14px; display: flex; align-items: baseline; justify-content: flex-end; gap: 8px;
  font-family: 'Courier New', ui-monospace, monospace; box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.25);
}
.lcd span { font-size: 2.4rem; font-weight: 700; letter-spacing: 0.04em; font-variant-numeric: tabular-nums; }
.lcd small { font-size: 1rem; font-weight: 700; }
.lcd.is-beep { box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.25), 0 0 0 3px var(--ok); }

.probes { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.probe {
  appearance: none; display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 8px;
  text-align: left; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 0.78rem; cursor: pointer; min-width: 0;
}
.probe .dot { grid-row: 1 / 3; width: 14px; height: 14px; border-radius: 50%; align-self: center; }
.probe b { font-size: 0.82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.probe--red .dot { background: var(--err); }
.probe--black .dot { background: var(--text); border: 2px solid var(--muted); }
.probe.is-armed { border-color: var(--accent); }
.probe.is-armed .dot { animation: blink 0.9s infinite; }
@keyframes blink { 50% { opacity: 0.25; } }

/* ---------------------------------------------------------------- tables */
.table-wrap { overflow: auto; max-height: 260px; border: 1px solid var(--border); border-radius: 8px; }
.table-wrap--tall { max-height: 320px; margin-top: 10px; }
.table { width: 100%; border-collapse: collapse; font-size: 0.8rem; }
.table th, .table td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.table th { position: sticky; top: 0; background: var(--surface-3); color: var(--muted); font-weight: 600; font-size: 0.74rem; text-transform: uppercase; letter-spacing: 0.04em; }
.table tr:last-child td { border-bottom: 0; }
.table--mono td { font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace; font-size: 0.76rem; white-space: nowrap; }
.table tbody tr.is-click { cursor: pointer; }
.table tbody tr.is-click:hover, .table tbody tr.is-sel { background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .empty { color: var(--muted); text-align: center; padding: 14px; }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 0.72rem; font-weight: 650; border: 1px solid currentColor; white-space: nowrap; }
.pill--ok { color: var(--ok); }
.pill--warn { color: var(--warn); }
.pill--err { color: var(--err); }
.pill--off { color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.stat small { display: block; color: var(--muted); font-size: 0.72rem; }
.stat b { font-size: 1.05rem; font-variant-numeric: tabular-nums; }

.decode { margin-top: 10px; font-size: 0.8rem; }
.decode:empty { display: none; }
.decode .bits { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.74rem; word-break: break-all; background: var(--surface-2); padding: 8px; border-radius: 8px; border: 1px solid var(--border); }
.decode .bits i { font-style: normal; padding: 0 1px; border-radius: 3px; }
.decode .bits .b-p { background: rgba(255, 90, 90, 0.25); }
.decode .bits .b-g { background: rgba(245, 179, 1, 0.25); }
.decode .bits .b-s { background: rgba(90, 169, 255, 0.25); }

/* ---------------------------------------------------------------- events */
.events { list-style: none; padding: 0; margin: 0; max-height: 180px; overflow: auto; font-size: 0.8rem; }
.events li { padding: 4px 0; border-bottom: 1px solid var(--border); display: grid; grid-template-columns: 60px 1fr; gap: 8px; }
.events li time { color: var(--muted); font-variant-numeric: tabular-nums; }
.events .k-warn { color: var(--warn); }
.events .k-err { color: var(--err); }
.events .k-ok { color: var(--ok); }

/* ---------------------------------------------------------------- challenge */
.challenge { margin-top: 12px; font-size: 0.85rem; }
.challenge .complaint { background: var(--surface-2); border-left: 3px solid var(--accent); padding: 10px 12px; border-radius: 6px; margin: 8px 0; }
.challenge .score { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0; }
.challenge select { width: 100%; height: 34px; background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; font: inherit; padding: 0 8px; }
.challenge .result { margin-top: 10px; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); }
.challenge .result--ok { border-color: var(--ok); }
.challenge .result--err { border-color: var(--err); }
.challenge ol { padding-left: 18px; margin: 6px 0; }
.instructor { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 10px; font-size: 0.85rem; }
.instructor summary { cursor: pointer; font-weight: 650; }
.fault-list { display: grid; gap: 6px; margin-top: 8px; }
.fault-list label { display: grid; grid-template-columns: auto 1fr; gap: 8px; align-items: start; font-size: 0.82rem; }
.fault-list small { color: var(--muted); display: block; }

/* ---------------------------------------------------------------- theory */
.theory {
  width: min(900px, calc(100vw - 32px)); max-height: calc(100vh - 48px);
  padding: 0; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-1); color: var(--text);
}
.theory::backdrop { background: rgba(0, 0, 0, 0.55); }
.theory__head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface-1); }
.theory__body { padding: 6px 18px 18px; font-size: 0.9rem; }
.theory__body h3 { color: var(--accent); margin-top: 18px; font-size: 0.85rem; }
.theory__body code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 0.85em; }
.theory__body table { border-collapse: collapse; margin: 8px 0; font-size: 0.82rem; }
.theory__body td, .theory__body th { border: 1px solid var(--border); padding: 4px 8px; }
.theory__body th { background: var(--surface-3); }

@media (max-width: 520px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .lcd span { font-size: 2rem; }
  .topbar__controls { width: 100%; }
  .topbar__controls .btn { flex: 1 1 auto; }
}
