:root {
  --bg: #ececec; --paper: #ffffff; --fg: #141414; --muted: #5c5c5c; --line: #b8b8b8; --tile: #dcdcdc; --active: #c5d5ea;
  --accent: #d0021b; --negative: #1c5fc4; --neutral: #8c8c8c; --grid-dot: #c4c4c4; --select: #0b8a3e; --dot: #141414;
  --probe-0: #d0021b; --probe-1: #1c5fc4; --probe-2: #0b8a3e; --probe-3: #8a3fb5;
  --sans: system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #242424; --paper: #1a1a1a; --fg: #e4e4e4; --muted: #a0a0a0; --line: #4a4a4a; --tile: #363636; --active: #34506f;
    --accent: #f0524f; --negative: #5a9bf0; --neutral: #7a7a7a; --grid-dot: #3a3a3a; --select: #49c46e; --dot: #ffe14d;
    --probe-0: #f0524f; --probe-1: #5a9bf0; --probe-2: #49c46e; --probe-3: #b98ae6;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #242424; --paper: #1a1a1a; --fg: #e4e4e4; --muted: #a0a0a0; --line: #4a4a4a; --tile: #363636; --active: #34506f;
  --accent: #f0524f; --negative: #5a9bf0; --neutral: #7a7a7a; --grid-dot: #3a3a3a; --select: #49c46e; --dot: #ffe14d;
  --probe-0: #f0524f; --probe-1: #5a9bf0; --probe-2: #49c46e; --probe-3: #b98ae6;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg); font: 13px/1.4 var(--sans);
  display: grid; grid-template-rows: auto minmax(0, 1fr) clamp(150px, 26vh, 240px); grid-template-columns: minmax(0, 1fr);
  height: 100vh; height: 100dvh; overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--negative); outline-offset: 1px; }

.mark.for-dark { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .mark.for-light { display: none; }
  :root:not([data-theme="light"]) .mark.for-dark { display: inline-block; }
}
:root[data-theme="dark"] .mark.for-light { display: none; }
:root[data-theme="dark"] .mark.for-dark { display: inline-block; }

.bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 4px 8px; border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; margin: 0 6px 0 0; font-size: 15px; font-weight: 600; }
.brand .mark { width: 44px; height: 44px; margin: -10px -6px -10px -10px; }
.transport, .actions { display: flex; align-items: center; gap: 4px; }
.actions { margin-left: auto; }
.btn { background: var(--tile); border: 1px solid var(--line); border-radius: 3px; padding: 2px 10px; cursor: pointer; }
.btn:hover { background: var(--active); }
.speed { display: flex; align-items: center; gap: 6px; margin-left: 6px; }
.speed input { width: 100px; }
.speed output, .clock { font: 12px var(--mono); color: var(--muted); white-space: nowrap; }
.clock { min-width: 90px; margin-left: 6px; }

.work { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; min-height: 0; }
.tools { display: flex; flex-direction: column; padding: 4px; overflow-y: auto; border-right: 1px solid var(--line); }
.tool {
  display: grid; grid-template-columns: 30px auto; align-items: center; gap: 6px;
  border: 1px solid transparent; background: none; padding: 1px 8px 1px 2px; cursor: pointer; text-align: left; white-space: nowrap;
}
.tool svg { width: 30px; height: 20px; overflow: visible; }
.tool svg * { fill: none; stroke: currentColor; stroke-width: 1.4; }
.tool svg .fill { fill: currentColor; }
.tool kbd { font: 11px var(--mono); color: var(--muted); margin-left: 6px; text-transform: uppercase; }
.tool:hover { background: var(--tile); }
.tool[aria-pressed="true"] { background: var(--active); border-color: var(--negative); }
.tool .short { display: none; }

.stage { position: relative; min-width: 0; min-height: 0; overflow: hidden; background: var(--paper); }
#board { display: block; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; }
#board.placing { cursor: crosshair; }
#board.panning { cursor: grabbing; }

.grid-dot { fill: var(--grid-dot); }
.part .hit { stroke: transparent; stroke-width: 16; stroke-linecap: round; }
.part .lead { stroke-width: 2.5; stroke-linecap: square; fill: none; }
.part .body { fill: none; stroke: var(--fg); stroke-width: 1.8; }
.part .body.thick { stroke-width: 3; }
.part .body.filled { fill: var(--fg); }
.part.regulator .body { fill: var(--paper); }
.part .chip { font: 600 11px var(--mono); fill: var(--fg); pointer-events: none; }
.part .pin { font: 9px var(--mono); fill: var(--muted); pointer-events: none; }
.part .contact { fill: var(--paper); stroke: var(--fg); stroke-width: 1.6; }
.part .glow { fill: var(--accent); opacity: 0; filter: blur(6px); }
.part .label { font: 11px var(--mono); fill: var(--muted); pointer-events: none; }
.part .label .name { fill: var(--fg); }
.part .rail-text { font: 600 12px var(--mono); fill: var(--fg); pointer-events: none; }
.part .logic-box { stroke: var(--fg); stroke-width: 1.4; fill: var(--paper); }
.part .logic-box.on { fill: var(--accent); stroke: var(--accent); }
.part .logic-text { font: 600 12px var(--mono); fill: var(--fg); pointer-events: none; }
.part .logic-text.on { fill: #ffffff; }
.part.selected .body, .part.selected .logic-box { stroke: var(--select); }
.part.selected .body.filled { fill: var(--select); }
.part.selected .hit { stroke: color-mix(in srgb, var(--select) 14%, transparent); }
.part.problem .hit { stroke: color-mix(in srgb, #dd2222 22%, transparent); }
.part:hover .hit { stroke: color-mix(in srgb, var(--fg) 7%, transparent); }
.select-tool .part, .select-tool .handle { cursor: pointer; }
.junction { fill: var(--fg); }
.loose-end { fill: var(--paper); stroke: var(--fg); stroke-width: 1.5; }
#current-dots { stroke: var(--dot); stroke-width: 4; stroke-linecap: round; fill: none; pointer-events: none; }
.handle { fill: var(--paper); stroke: var(--select); stroke-width: 2; }
.ghost-part { opacity: .55; pointer-events: none; }
.probe-flag { pointer-events: none; }
.probe-flag text { font: 600 11px var(--mono); fill: #ffffff; }
.hover-point { fill: none; stroke: var(--fg); stroke-width: 1.5; opacity: .5; pointer-events: none; }

.inspector { position: relative; width: 250px; overflow: auto; padding: 8px 10px; border-left: 1px solid var(--line); }
.inspector h2 { margin: 0 20px 6px 0; font-size: 14px; font-weight: 600; }
.inspector h2 .kind { font-weight: 400; color: var(--muted); margin-left: 6px; }
.inspector h3 { margin: 12px 0 4px; padding-top: 8px; border-top: 1px solid var(--line); font-size: 13px; font-weight: 600; }
.inspector p { margin: 0 0 8px; }
.inspector .note.small { font-size: 12px; color: var(--muted); }
.inspector .field { display: grid; grid-template-columns: 1fr 104px; align-items: center; gap: 6px; margin: 4px 0; }
.inspector input[type="text"], .inspector select { width: 100%; background: var(--paper); border: 1px solid var(--line); border-radius: 2px; padding: 2px 4px; }
.inspector input[type="text"] { font: 12px var(--mono); }
.inspector input.bad { border-color: #dd2222; }
.inspector .check { display: flex; gap: 6px; align-items: center; margin: 4px 0; cursor: pointer; }
.readout { margin: 10px 0 0; padding-top: 8px; border-top: 1px solid var(--line); display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; }
.readout dd { margin: 0; text-align: right; font: 12px var(--mono); }
.inspector .row { display: flex; gap: 4px; margin-top: 10px; flex-wrap: wrap; }
.inspector .collapse { position: absolute; right: 4px; top: 4px; width: 20px; border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 16px; line-height: 1; }
.inspector.folded { width: 28px; padding: 0; overflow: hidden; }
.inspector.folded > :not(.collapse) { display: none; }
.inspector .advice { padding: 6px 8px; border: 1px solid #c9a400; background: #fff6cc; color: #141414; }
.inspector .advice b { display: block; }
.inspector .field-problem { color: #c00000; font-size: 12px; margin: 2px 0 6px; }
.inspector .text-button { border: 0; background: none; padding: 2px 0; color: var(--negative); cursor: pointer; font-size: 12px; text-decoration: underline; }

.flip { position: absolute; z-index: 6; border: 1px solid var(--line); background: var(--bg); padding: 1px 8px; font-size: 12px; cursor: pointer; white-space: nowrap; }
.flip:hover { background: var(--active); }
.flip[hidden] { display: none; }
.tip {
  position: absolute; pointer-events: none; z-index: 5; background: #ffffd8; color: #141414; border: 1px solid #777777;
  font: 11px var(--mono); padding: 2px 5px; white-space: pre; transform: translate(14px, 14px);
}
.banner { position: absolute; left: 0; right: 0; bottom: 0; background: #b3261e; color: #ffffff; padding: 4px 10px; }
.banner.soft { background: #fff6cc; color: #141414; border-top: 1px solid #c9a400; }
.toast { position: absolute; left: 50%; top: 8px; transform: translateX(-50%); background: var(--fg); color: var(--bg); padding: 3px 10px; }

.scope { position: relative; border-top: 1px solid var(--line); min-height: 0; }
#scope { display: block; width: 100%; height: 100%; }
.legend { position: absolute; left: 54px; right: 150px; top: 6px; display: flex; gap: 14px; flex-wrap: wrap; z-index: 1; }
.chip { display: inline-flex; align-items: center; gap: 5px; font: 12px var(--mono); }
.chip i { width: 10px; height: 10px; display: inline-block; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0 2px; }
.legend .empty { color: var(--muted); }
.scope-mode { position: absolute; right: 8px; top: 5px; z-index: 1; display: flex; }
.scope-mode button { border: 1px solid var(--line); background: var(--tile); padding: 0 8px; cursor: pointer; font-size: 12px; }
.scope-mode button + button { border-left: 0; }
.scope-mode button[aria-pressed="true"] { background: var(--active); }

.help { width: min(760px, calc(100% - 32px)); max-height: calc(100% - 48px); border: 1px solid var(--muted); background: var(--bg); color: var(--fg); padding: 0; }
.help::backdrop { background: rgb(0 0 0 / .3); }
.help-head { position: relative; padding: 8px 14px; border-bottom: 1px solid var(--line); }
.help-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.help .close { position: absolute; right: 8px; top: 3px; border: 0; background: none; font-size: 20px; color: var(--muted); cursor: pointer; }
.help-body { display: grid; grid-template-columns: 1.3fr 1fr; gap: 24px; padding: 10px 14px; }
.help h3 { margin: 12px 0 3px; font-size: 13px; font-weight: 600; }
.help h3:first-child { margin-top: 0; }
.help p { margin: 0 0 6px; }
.keys { display: grid; grid-template-columns: auto 1fr auto 1fr; gap: 3px 8px; margin: 0; align-items: center; }
.keys dd { margin: 0; }
.help kbd { display: inline-block; min-width: 24px; padding: 0 4px; text-align: center; border: 1px solid var(--line); background: var(--paper); font: 11px var(--mono); }
.help-foot { padding: 8px 14px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }
.help-foot p { margin: 0; }
.help a { color: var(--negative); }

.help-small { display: none; justify-self: end; }

@media (max-width: 760px) {
  body { grid-template-rows: auto minmax(0, 1fr) auto auto clamp(120px, 22vh, 180px); }
  .bar { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 6px 8px; padding: 4px 8px; }
  .preset select { width: 100%; }
  .transport, .actions { grid-row: 2; }
  .transport { grid-column: 1 / 3; min-width: 0; }
  .actions { grid-column: 3; margin-left: 0; }
  .actions #help { display: none; }
  .help-small { display: inline-block; }
  .speed { flex: 1; min-width: 0; }
  .speed input { width: 100%; min-width: 50px; }
  #speed-out, .clock { display: none; }
  .btn { padding: 4px 9px; }
  .work { display: contents; }
  .stage { grid-row: 2; }
  .inspector { grid-row: 3; width: auto; max-height: 34vh; border-left: 0; border-top: 1px solid var(--line); }
  .inspector.folded { width: auto; padding: 6px 10px; }
  .inspector.folded > h2:not(.collapse) { display: block; margin-bottom: 0; }
  .tools { grid-row: 4; flex-direction: row; overflow-x: auto; overflow-y: hidden; border-right: 0; border-top: 1px solid var(--line); }
  .tool { grid-template-columns: 30px; justify-items: center; gap: 1px; padding: 2px 4px; min-width: 54px; }
  .tool .name { font-size: 10px; }
  .tool .long, .tool kbd { display: none; }
  .tool .short { display: inline; }
  .scope { grid-row: 5; }
  .legend { left: 50px; right: 120px; gap: 8px; }
  .help-body { grid-template-columns: 1fr; gap: 6px; }
}

.embed-sign, .embed-readout { display: none; }
.embed body { grid-template-rows: minmax(0, 1fr) 34%; }
.embed .work, .embed .stage { grid-row: 1; }
.embed .scope { grid-row: 2; border-top: 0; }
.embed .bar, .embed .tools, .embed .inspector, .embed .legend, .embed .banner, .embed .toast, .embed .flip, .embed .tip, .embed .scope-mode { display: none !important; }
.embed .work { grid-template-columns: minmax(0, 1fr); }
.embed .stage, .embed body { background: var(--paper); }
.embed .part .hit { stroke: none !important; }
.embed #board { pointer-events: none; }
.embed .embed-sign { display: flex; align-items: center; position: absolute; right: 24px; top: 18px; font-size: 22px; font-weight: 600; }
.embed .embed-sign .mark { width: 72px; height: 72px; margin: -16px -10px -16px -16px; }
.embed .embed-readout { display: block; position: absolute; right: 26px; top: 50%; transform: translateY(-50%); text-align: right; }
.embed .embed-readout p { margin: 0; }
.embed .embed-readout .name { color: var(--muted); }
.embed .embed-readout .value { font: clamp(36px, 6vw, 72px)/1.1 var(--mono); font-variant-numeric: tabular-nums; min-width: 4.2ch; }
.embed .embed-readout .bits { font: 15px var(--mono); letter-spacing: .2em; color: var(--muted); }
.embed .embed-readout .bits b { color: var(--accent); }
@media (max-width: 560px) {
  .embed .embed-sign { display: none; }
  .embed .embed-readout { right: 12px; }
  .embed .embed-readout .name { font-size: 12px; }
  .embed .embed-readout .value { font-size: 26px; }
  .embed .embed-readout .bits { font-size: 10px; }
}
