:root {
  --bg: #121816;
  --bg-raise: #1b2420;
  --bg-sunk: #101714;
  --ink: #e7efe9;
  --muted: #9aada3;
  --line: #2c3b34;
  --paper: #f3efe4;
  --paper-ink: #1c1915;
  --paper-muted: #6d6458;
  --wire: #2a2622;
  --hot: #1f8a4c;
  --copper: #c9843d;
  --accent: #d7a15e;
  --danger: #a33b32;
  --focus: #f0c27a;
  --ok: #8ee0b2;
  --bad: #f0b2a8;
  --sans: "Segoe UI", "Avenir Next", "Trebuchet MS", sans-serif;
  --mono: "Cascadia Mono", ui-monospace, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: radial-gradient(1200px 500px at 10% -10%, rgba(201, 132, 61, 0.16), transparent 50%), var(--bg); color: var(--ink); font-family: var(--sans); }
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: 0.5; }
h1, h2, p { margin: 0; }
h1 { font-size: 1.45rem; letter-spacing: 0.04em; }
h2 { font-size: 0.78rem; letter-spacing: 0.14em; text-transform: uppercase; color: var(--accent); margin-bottom: 0.55rem; }
kbd { font-family: var(--mono); font-size: 0.78em; border: 1px solid var(--line); border-radius: 4px; padding: 0 0.3em; background: var(--bg-sunk); }

/* ---- top bar */
.topbar { display: flex; justify-content: space-between; gap: 1rem; align-items: center; padding: 0.9rem 1.2rem; border-bottom: 1px solid var(--line); background: rgba(18, 24, 22, 0.92); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; gap: 0.75rem; align-items: center; }
.mark { width: 42px; height: 42px; flex: 0 0 auto; border: 2px solid var(--copper); border-radius: 6px; background: linear-gradient(var(--copper), var(--copper)) 6px 50% / 3px 70% no-repeat, linear-gradient(var(--copper), var(--copper)) 33px 50% / 3px 70% no-repeat, linear-gradient(var(--ink), var(--ink)) 9px 50% / 10px 3px no-repeat, linear-gradient(var(--hot), var(--hot)) 22px 50% / 8px 3px no-repeat; }
.eyebrow { color: var(--muted); font-size: 0.72rem; letter-spacing: 0.16em; text-transform: uppercase; }
.run-cluster { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.run-cluster .speed { width: auto; margin: 0; padding: 0.45rem 0.5rem; }
.scan-readout { font-family: var(--mono); font-size: 0.78rem; color: var(--muted); white-space: nowrap; }

/* ---- layout: 3 columns → 2 → 1 */
.layout { display: grid; grid-template-columns: 290px minmax(0, 1fr) 310px; gap: 0.9rem; padding: 0.9rem; align-items: start; }
.panel, .sheet-wrap { background: var(--bg-raise); border: 1px solid var(--line); border-radius: 14px; padding: 0.95rem; min-width: 0; }
.hint, .primer, .check-result, .trace-status { color: var(--muted); font-size: 0.86rem; line-height: 1.45; }
.hint { margin-bottom: 0.8rem; }

/* ---- palette */
.tool-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.4rem; margin-bottom: 0.9rem; }
.tool, .btn, .file-btn { border: 1px solid var(--line); background: #121a17; border-radius: 8px; padding: 0.45rem 0.55rem; text-align: left; }
.tool small { display: block; color: var(--muted); font-size: 0.68rem; }
.tool.is-active { border-color: var(--accent); background: #2a2418; box-shadow: inset 0 0 0 1px var(--accent); }
.field { display: block; font-size: 0.75rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin-bottom: 0.3rem; }
select, .inspector input { width: 100%; background: var(--bg-sunk); border: 1px solid var(--line); border-radius: 8px; padding: 0.45rem 0.5rem; margin-bottom: 0.8rem; }
.row-actions, .challenge-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 0.45rem; }
.btn-accent { background: #3c2c18; border-color: var(--copper); }
.btn-run[aria-pressed="true"] { background: #163526; border-color: var(--hot); color: #d9f5e4; }
.file-btn { display: inline-block; cursor: pointer; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); border: 0; }

/* ---- exercises */
.challenge-list { margin-bottom: 0.6rem; max-height: 46vh; overflow-y: auto; padding-right: 0.2rem; }
.challenge-card { border: 1px solid var(--line); border-radius: 8px; padding: 0.45rem; margin-bottom: 0.4rem; }
.challenge-card.is-loaded { border-color: var(--accent); }
.challenge-card.is-done { border-left: 3px solid var(--hot); }
.challenge-head { display: flex; justify-content: space-between; align-items: center; gap: 0.4rem; }
.pass-badge { font-size: 0.66rem; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ok); border: 1px solid var(--hot); border-radius: 999px; padding: 0.05rem 0.45rem; }
.challenge-card p { color: var(--muted); font-size: 0.78rem; margin: 0.2rem 0 0.4rem; }
.challenge-hint { border-left: 2px solid var(--accent); padding-left: 0.45rem; color: var(--ink); }
.challenge-actions .btn { font-size: 0.78rem; padding: 0.3rem 0.5rem; }
.check-result { min-height: 1.3em; }
.check-result.is-ok { color: var(--ok); }
.check-result.is-bad { color: var(--bad); }

/* ---- ladder sheet */
.sheet-head { display: flex; justify-content: space-between; gap: 1rem; align-items: baseline; margin-bottom: 0.7rem; flex-wrap: wrap; }
.sheet-head p { color: var(--muted); font-size: 0.85rem; }
.ladder { background: linear-gradient(transparent 31px, rgba(90, 78, 54, 0.08) 32px) 0 0 / 100% 32px, var(--paper); color: var(--paper-ink); border-radius: 10px; padding: 0.8rem; overflow-x: auto; min-height: 280px; }
.ladder .hint { color: var(--paper-muted); }
.rung { margin-bottom: 0.6rem; border-radius: 8px; padding: 0.2rem 0.1rem; min-width: max-content; }
.rung.is-solving { outline: 2px solid var(--copper); outline-offset: 2px; background: rgba(201, 132, 61, 0.1); }
.rung.is-pending { opacity: 0.55; }
.rung-head { display: flex; align-items: center; gap: 0.5rem; padding: 0 0.2rem; }
.rung-num { font-family: var(--mono); font-size: 0.72rem; font-weight: 700; color: var(--paper-muted); white-space: nowrap; }
.rung-comment { flex: 1 1 160px; min-width: 0; max-width: 420px; border: 0; border-bottom: 1px dashed #b7ad9d; background: transparent; color: var(--paper-ink); font-size: 0.78rem; font-style: italic; padding: 0.15rem 0.2rem; }
.rung-comment::placeholder { color: #a39a8b; }
.rung-state { font-family: var(--mono); font-size: 0.68rem; font-weight: 700; letter-spacing: 0.08em; color: var(--paper-muted); margin-left: auto; }
.rung-state.is-true { color: var(--hot); }
.rung-wires { display: grid; grid-template-columns: 18px minmax(max-content, 1fr) 18px; }
.rail { background: var(--wire); border-radius: 3px; margin: 8px 0; }
.rail.hot { background: var(--hot); }
.slots { display: flex; min-height: 92px; }
.column { flex: 1 0 120px; display: flex; flex-direction: column; justify-content: center; position: relative; padding: 0.2rem 0; }
.branch { display: flex; align-items: center; min-height: 74px; position: relative; }
.wire { flex: 1; height: 3px; background: var(--wire); }
.wire.hot { background: var(--hot); }
.vbar { position: absolute; left: 0; width: 3px; background: var(--wire); }
.column.has-parallel .vbar { top: 37px; bottom: 37px; }
.vbar.hot { background: var(--hot); }
.element, .slot-btn, .mini { border: 0; background: transparent; color: var(--paper-ink); position: relative; z-index: 1; }
.element { width: 84px; min-height: 58px; border-radius: 6px; background: rgba(255, 255, 255, 0.55); padding: 0.15rem; }
.element.is-selected { outline: 2px solid var(--copper); outline-offset: 2px; }
.element.is-hot .glyph { color: var(--hot); }
.element.is-hot { background: rgba(31, 138, 76, 0.12); }
.glyph { font-family: var(--mono); font-size: 1.05rem; display: block; }
.tag { font-family: var(--mono); font-size: 0.72rem; font-weight: 700; }
.element small { display: block; color: var(--paper-muted); font-size: 0.66rem; font-family: var(--mono); white-space: nowrap; }
.slot-btn, .mini { border: 1px dashed #b7ad9d; border-radius: 6px; color: var(--paper-muted); font-size: 0.72rem; padding: 0.2rem 0.35rem; margin: 0.15rem; }
.slot-btn:hover, .mini:hover { border-style: solid; color: var(--paper-ink); }
.mini.danger { color: var(--danger); border-color: #d4a39d; }
.rung-tools { display: flex; flex-wrap: wrap; gap: 0.3rem; padding: 0 0.2rem 0.3rem; }
.trace-status { margin-top: 0.5rem; color: var(--accent); min-height: 1.3em; }

/* ---- lint */
.lint { margin-top: 0.5rem; }
.lint-ok { color: var(--ok); font-size: 0.82rem; }
.lint-list { margin: 0; padding-left: 1.1rem; font-size: 0.84rem; line-height: 1.45; }
.lint-warn { color: var(--bad); }
.lint-info { color: var(--muted); }

/* ---- truth table */
.primer { margin-top: 0.8rem; }
.primer summary { cursor: pointer; color: var(--ink); }
.primer ol, .primer ul { margin: 0.4rem 0 0; padding-left: 1.1rem; }
.primer li { margin-bottom: 0.25rem; }
.truth-wrap { overflow-x: auto; margin: 0.5rem 0; }
.truth { border-collapse: collapse; font-family: var(--mono); font-size: 0.82rem; }
.truth th, .truth td { border: 1px solid var(--line); padding: 0.25rem 0.6rem; text-align: center; }
.truth th { color: var(--accent); }
.truth .th-out, .truth .td-out { border-left: 2px solid var(--copper); }
.truth .one { color: var(--ok); font-weight: 700; }
.truth .zero { color: var(--muted); }
.truth tr.is-current td { background: rgba(215, 161, 94, 0.18); }
.primer .btn { margin-top: 0.3rem; }

/* ---- I/O monitor */
.io-head { display: flex; justify-content: space-between; align-items: baseline; gap: 0.5rem; }
.io-head .mini { color: var(--muted); border-color: var(--line); margin: 0 0 0.5rem; }
.io-head .mini:hover { color: var(--ink); }
.io-group { margin-bottom: 0.85rem; }
.bits { display: grid; grid-template-columns: 1fr 1fr; gap: 0.35rem; }
.bit { display: flex; align-items: center; gap: 0.4rem; border: 1px solid var(--line); background: var(--bg-sunk); border-radius: 8px; padding: 0.35rem 0.45rem; text-align: left; }
.bit:disabled { opacity: 1; }
.lamp { width: 0.7rem; height: 0.7rem; border-radius: 50%; background: #3a403c; flex: 0 0 auto; }
.bit.is-on .lamp { background: var(--hot); box-shadow: 0 0 8px var(--hot); }
.bit.is-output.is-on .lamp { background: #e07a45; box-shadow: 0 0 8px #e07a45; }
.bit.is-memory.is-on .lamp { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.bit span, .timer-line { font-family: var(--mono); font-size: 0.78rem; }
.timer-line { margin-bottom: 0.2rem; }
.inspector { border-top: 1px solid var(--line); padding-top: 0.8rem; }
.inspector .danger { border-color: var(--danger); color: var(--bad); }
.why { font-size: 0.84rem; line-height: 1.45; color: var(--ink); background: var(--bg-sunk); border-left: 3px solid var(--accent); border-radius: 0 8px 8px 0; padding: 0.5rem 0.6rem; margin-bottom: 0.8rem; min-height: 2.5em; }

button:focus-visible, select:focus-visible, input:focus-visible, summary:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---- responsive */
@media (max-width: 1180px) {
  .layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .sheet-wrap { grid-column: 1 / -1; order: 0; }
  .palette { order: 1; }
  .io-panel { order: 2; }
  .tool-grid { grid-template-columns: repeat(3, 1fr); }
  .challenge-list { max-height: none; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 0.6rem; gap: 0.6rem; }
  .topbar { flex-direction: column; align-items: stretch; padding: 0.7rem 0.8rem; position: static; }
  .run-cluster { justify-content: flex-start; }
  .tool-grid { grid-template-columns: 1fr 1fr; }
  .rung-wires { grid-template-columns: 12px minmax(max-content, 1fr) 12px; }
  .column { flex-basis: 100px; }
  .element { width: 72px; }
  .ladder { padding: 0.5rem; }
  .bits { grid-template-columns: repeat(4, 1fr); }
  .bit span { font-size: 0.72rem; }
}
@media (max-width: 420px) {
  .bits { grid-template-columns: 1fr 1fr; }
}
@media (prefers-reduced-motion: no-preference) {
  .wire, .rail, .vbar, .lamp { transition: background-color 80ms linear; }
}
