:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #1d2330;
  --muted: #5f6b7a;
  --line: #dde1e7;
  --accent: #1a73e8;
  --accent-ink: #ffffff;
  --danger: #c62828;
  --warn: #c62828;
  --chip: #eef1f5;
  --note: #fff7d6;
  --note-ink: #4a3c00;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c; --panel: #1d2128; --ink: #e8ebf0; --muted: #9aa5b4; --line: #2d333d;
    --accent: #6ea8ff; --accent-ink: #0d1420; --danger: #ff6b6b; --warn: #ff8a80; --chip: #262b33;
    --note: #3a3418; --note-ink: #f5e7a8; --shadow: 0 1px 3px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14171c; --panel: #1d2128; --ink: #e8ebf0; --muted: #9aa5b4; --line: #2d333d;
  --accent: #6ea8ff; --accent-ink: #0d1420; --danger: #ff6b6b; --warn: #ff8a80; --chip: #262b33;
  --note: #3a3418; --note-ink: #f5e7a8; --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, select, input { font: inherit; color: inherit; }
button, select {
  background: var(--chip); border: 1px solid var(--line); border-radius: 8px;
  padding: 5px 10px; cursor: pointer; white-space: nowrap;
}
button:hover, select:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--danger); }
input[type=text], input[type=search] {
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; width: 100%;
}

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); box-shadow: var(--shadow);
}
.topbar h1 { font-size: 18px; margin: 0; white-space: nowrap; }
.toolbar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.toolbar .sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.toolbar label.check, .toolbar label.compact { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); white-space: nowrap; }
#run.paused { background: var(--accent); color: var(--accent-ink); }

/* layout */
.layout { display: grid; grid-template-columns: 170px minmax(0, 1fr) 300px; gap: 12px; padding: 12px 16px; align-items: start; }
#palette, #inspector {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px; box-shadow: var(--shadow);
}
#palette { display: flex; flex-direction: column; gap: 4px; position: sticky; top: 12px; }
#palette h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 10px 4px 2px; }
.tool { display: flex; align-items: center; gap: 8px; text-align: left; background: transparent; border-color: transparent; }
.tool .icon { width: 22px; text-align: center; font-size: 16px; }
.tool.active { background: var(--accent); color: var(--accent-ink); }

.stage { min-width: 0; }
.board-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px; box-shadow: var(--shadow); }
#board { width: 100%; height: auto; display: block; touch-action: none; user-select: none; }
.below { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.hint, .statusbar { margin: 6px 4px; color: var(--muted); font-size: 13px; }
.statusbar { font-variant-numeric: tabular-nums; }
.note {
  position: relative; background: var(--note); color: var(--note-ink); border-radius: 10px;
  padding: 8px 34px 8px 12px; margin-bottom: 8px; font-size: 13.5px;
}
.note .close { position: absolute; top: 4px; right: 4px; background: transparent; border: 0; font-size: 18px; color: inherit; }

/* inspector */
#inspector h2 { font-size: 17px; margin: 0 0 4px; }
#inspector .blurb { color: var(--muted); margin: 0 0 6px; font-size: 13px; }
#inspector .legs { font-size: 12.5px; margin: 0 0 8px; color: var(--muted); }
.props { display: grid; gap: 6px; margin: 8px 0; }
.prop { display: grid; grid-template-columns: 1fr 110px; align-items: center; gap: 6px; font-size: 13px; }
.prop select { width: 110px; padding: 4px; }
.prop input[type=checkbox] { justify-self: start; width: 18px; height: 18px; }
table.readouts { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 13px; font-variant-numeric: tabular-nums; }
table.readouts th { text-align: left; font-weight: 500; color: var(--muted); padding: 4px 6px 0 0; vertical-align: top; }
table.readouts td { text-align: right; padding: 4px 0 0; font-weight: 600; }
table.readouts tr.eqrow td { text-align: left; font-weight: 400; font-size: 11.5px; color: var(--muted); padding: 0 0 4px; border-bottom: 1px dashed var(--line); font-family: ui-monospace, Menlo, Consolas, monospace; }
.warn { color: var(--warn) !important; }
.muted { color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

/* board drawing (physical colours, the same in light and dark mode) */
.bench { fill: #3b3f46; }
.bench-label { font-size: 10px; fill: #c5cad3; }
.post { fill: #c9a34a; stroke: #8a6d22; stroke-width: 1; }
.post-hole { fill: #5a4710; }
.breadboard { fill: #f4efe1; stroke: #d8d0b8; stroke-width: 1.5; }
.channel { fill: #e3dcc6; }
.rail-plus { stroke: #d93025; stroke-width: 2; }
.rail-minus { stroke: #1a73e8; stroke-width: 2; }
.rail-sign { font-size: 15px; font-weight: 700; }
.rail-sign.plus { fill: #d93025; }
.rail-sign.minus { fill: #1a73e8; }
.col-label, .row-label { font-size: 9px; fill: #8c8470; }
.hole { fill: #5b5547; }
.connections line { stroke: #cbbf98; stroke-width: 5; stroke-linecap: round; opacity: 0; transition: opacity .2s; }
#board.show-connections .connections line { opacity: .4; }
.hover-ring { fill: none; stroke: #1a73e8; stroke-width: 2; pointer-events: none; }
.pending-ring { fill: rgba(26, 115, 232, .25); stroke: #1a73e8; stroke-width: 2; pointer-events: none; }
.ghost { fill: none; stroke: #1a73e8; stroke-width: 3; stroke-dasharray: 6 5; opacity: .7; pointer-events: none; }

.part { cursor: pointer; }
#board.placing .parts { pointer-events: none; }
.lead { stroke: #9aa0a6; stroke-width: 2; }
.lead-hit, .wire-hit { stroke: transparent; stroke-width: 12; fill: none; }
.wire { fill: none; stroke-width: 4; stroke-linecap: round; }
.wire-shadow { fill: none; stroke: rgba(0, 0, 0, .25); stroke-width: 6; stroke-linecap: round; transform: translate(1px, 1.5px); }
.leg { fill: #777; }
.part.selected .leg { fill: #1a73e8; r: 5; cursor: grab; }
.part.selected .body, .part.selected .fx, .part.selected .wire { filter: drop-shadow(0 0 3px #1a73e8) drop-shadow(0 0 1px #1a73e8); }
.flow { fill: none; stroke: #ffe14d; stroke-width: 2.5; stroke-dasharray: 3 11; stroke-linecap: round; pointer-events: none; opacity: .95; }
.value { font-size: 9.5px; fill: #333; paint-order: stroke; stroke: #f4efe1; stroke-width: 3px; pointer-events: none; }
.label { pointer-events: none; }
.warning-text { font-size: 11px; font-weight: 800; fill: #c62828; paint-order: stroke; stroke: #fff; stroke-width: 3px; }

.resistor-body { fill: #e6cfa4; stroke: #b39662; }
.lamp-glass { fill: rgba(255, 252, 235, .85); stroke: #a8a8a8; stroke-width: 1.5; }
.filament { fill: none; stroke: #6b6b6b; stroke-width: 1.4; stroke-linejoin: round; }
.broken-mark { stroke: #c62828; stroke-width: 2; }
.led-dome { stroke: rgba(0, 0, 0, .45); stroke-width: 1; }
.led-flat { stroke: rgba(0, 0, 0, .55); stroke-width: 1.5; }
.led-symbol { fill: rgba(0, 0, 0, .35); stroke: rgba(0, 0, 0, .5); stroke-width: 1; }
.diode-body { fill: #222; }
.diode-band { fill: #ccc; }
.battery-body { stroke: rgba(0, 0, 0, .4); }
.battery-end { fill: #1b1b1b; opacity: .85; }
.battery-nub { fill: #bbb; stroke: #777; }
.battery-sign { font-size: 13px; font-weight: 800; fill: #fff; }
.battery-text { font-size: 10px; font-weight: 700; fill: #fff; }
.tiny-sign { font-size: 8px; fill: #ddd; }
.buzzer-body { fill: #1f1f1f; stroke: #000; }
.buzzer-hole { fill: #000; stroke: #555; }
.sound-waves path { fill: none; stroke: #1a73e8; stroke-width: 1.8; stroke-linecap: round; }
.sound-waves.active { animation: pulse .25s infinite alternate; }
@keyframes pulse { from { opacity: .4; } to { opacity: 1; } }
.motor-can { fill: #b9bec7; stroke: #6d737d; stroke-width: 1.5; }
.motor-spoke { stroke: #555; stroke-width: 2.5; stroke-linecap: round; }
.motor-flag { stroke: #d93025; stroke-width: 3; stroke-linecap: round; }
.motor-hub { fill: #333; }
.switch-base { fill: #34373d; }
.contact { fill: #d7d7d7; }
.switch-lever { stroke: #e6e6e6; stroke-width: 3; stroke-linecap: round; }
.button-cap { fill: #d93025; stroke: #8f1d16; }
.button-cap.pressed { fill: #8f1d16; r: 6; }
.cap-body { fill: #2050a8; stroke: #12326d; }
.cap-stripe { fill: #a9c1ec; }
.cap-charge { fill: #ffe14d; }
.fuse-glass { fill: rgba(225, 235, 245, .9); stroke: #8e9aa6; }
.fuse-cap { fill: #b8b8b8; }
.fuse-wire { stroke: #555; stroke-width: 1.2; fill: none; }
.fuse-wire.blown { stroke: #333; }
.meter { stroke: #333; stroke-width: 1.5; }
.voltmeter-face { fill: #ffd54a; }
.ammeter-face { fill: #8fd3ff; }
.meter-letter { font-size: 8px; font-weight: 800; fill: #333; }
.meter-read { font-size: 7.5px; font-weight: 700; fill: #111; font-variant-numeric: tabular-nums; }

/* lexicon */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal[hidden] { display: none; }
.modal-box {
  background: var(--panel); border-radius: 14px; width: min(1000px, 100%); height: min(720px, 100%);
  display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 10px 40px rgba(0, 0, 0, .3);
}
.modal-head { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal-head h2 { font-size: 17px; margin: 0; white-space: nowrap; }
.modal-head input { max-width: 280px; margin-left: auto; }
#lex-close { font-size: 18px; line-height: 1; }
.modal-body { display: grid; grid-template-columns: 260px 1fr; min-height: 0; flex: 1; }
#lex-list { overflow-y: auto; border-right: 1px solid var(--line); padding: 8px; display: flex; flex-direction: column; gap: 2px; }
#lex-list button { text-align: left; background: transparent; border-color: transparent; white-space: normal; display: flex; flex-direction: column; }
#lex-list button small { color: var(--muted); font-size: 11.5px; }
#lex-list button.active { background: var(--chip); border-color: var(--line); }
#lex-list button[hidden] { display: none; }
#lex-content { overflow-y: auto; padding: 16px 22px 24px; }
#lex-content h2 { margin: 0 0 2px; font-size: 21px; }
#lex-content .lede { color: var(--muted); margin-top: 0; }
#lex-content var { font-style: italic; font-family: "Cambria Math", "STIX Two Math", Georgia, serif; }
.eq {
  font-family: "Cambria Math", "STIX Two Math", Georgia, serif; font-size: 18px; background: var(--chip);
  border-left: 4px solid var(--accent); padding: 10px 14px; border-radius: 6px; margin: 12px 0; overflow-x: auto;
}
.lex-table { border-collapse: collapse; margin: 10px 0; font-size: 13.5px; }
.lex-table th, .lex-table td { border: 1px solid var(--line); padding: 4px 10px; text-align: left; }
.lex-table th { background: var(--chip); }
.try { margin-top: 12px; }

#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translate(-50%, 30px); opacity: 0;
  background: #222; color: #fff; padding: 9px 16px; border-radius: 10px; transition: all .25s; pointer-events: none;
  max-width: calc(100% - 32px); z-index: 20;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* narrow screens */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 150px minmax(0, 1fr); }
  #inspector { grid-column: 1 / -1; }
}
@media (max-width: 720px) {
  .topbar { padding: 8px 16px; }
  .layout { grid-template-columns: minmax(0, 1fr); padding: 8px 16px; }
  #palette { position: static; flex-direction: row; flex-wrap: wrap; }
  #palette h3 { display: none; }
  .tool { padding: 4px 8px; border-color: var(--line); }
  .board-wrap { overflow-x: auto; }
  #board { min-width: 640px; }
  .modal-body { grid-template-columns: 1fr; grid-template-rows: 160px 1fr; }
  #lex-list { border-right: 0; border-bottom: 1px solid var(--line); }
  .modal-head { flex-wrap: wrap; }
  .modal-head input { max-width: none; margin-left: 0; order: 3; }
}

.view-link { font-size: 13px; font-weight: 500; margin-left: 10px; color: var(--accent); text-decoration: none; }
.view-link:hover { text-decoration: underline; }
