/* Nandcraft — touch-first dark UI */

:root {
  --bg: #0a0f1e;
  --panel: #121a31;
  --panel-2: #1a2340;
  --line: #283358;
  --text: #e7edff;
  --muted: #8da0cc;
  --accent: #22d3ee;
  --accent-deep: #0891b2;
  --on: #4ade80;
  --on-dim: #16a34a;
  --off: #45537e;
  --warn: #fbbf24;
  --danger: #f87171;
  --star: #facc15;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: ui-rounded, -apple-system, 'SF Pro Rounded', 'Segoe UI', Roboto, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}
.pal-item:focus-visible { outline-offset: 3px; }

.hidden { display: none !important; }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* ═══════════ Home ═══════════ */

#screen-home { overflow-y: auto; display: block; }

.home-inner {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(28px + env(safe-area-inset-top)) 20px calc(40px + env(safe-area-inset-bottom));
}

.logo {
  font-size: 44px;
  font-weight: 800;
  letter-spacing: 2px;
  background: linear-gradient(120deg, var(--accent), #a78bfa 70%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.logo span { font-weight: 300; }

.tagline { color: var(--muted); margin-top: 4px; font-size: 16px; }

.home-progress { margin: 22px 0 14px; color: var(--muted); font-size: 16px; }
.home-progress b { color: var(--text); }

#btn-continue { width: 100%; margin-bottom: 10px; }

.chapter { margin-top: 30px; }
.chapter h2 { font-size: 20px; letter-spacing: .5px; }
.ch-blurb { color: var(--muted); font-size: 14px; margin: 3px 0 12px; }

.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }

.level-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 92px;
  transition: transform .12s, border-color .12s;
}
.level-card:active { transform: scale(.97); }
.level-card.done { border-color: var(--on-dim); }
.level-card.locked { opacity: .45; }
.level-card.sandbox { border-color: #7c5cd6; }

.lv-num { font-size: 13px; color: var(--muted); }
.level-card.done .lv-num { color: var(--on); }
.lv-name { font-weight: 700; font-size: 15px; line-height: 1.25; }
.lv-stars { color: var(--star); letter-spacing: 2px; font-size: 14px; }
.lv-stars i { color: var(--off); font-style: normal; }
.lv-sub { color: var(--muted); font-size: 13px; line-height: 1.4; }
.lv-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: auto; }
.lv-cost {
  font-size: 11.5px;
  color: var(--muted);
  font-family: ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.lv-cost i { font-style: normal; opacity: .6; }
.lv-cost.beat { color: var(--star); font-weight: 700; }

.home-foot { margin-top: 44px; color: var(--muted); font-size: 13px; display: grid; gap: 8px; justify-items: start; }
.home-foot a { color: var(--accent); }

/* ═══════════ Buttons ═══════════ */

.btn {
  border-radius: 12px;
  padding: 10px 18px;
  font-weight: 700;
  font-size: 15px;
  transition: transform .1s, filter .15s;
}
.btn:active { transform: scale(.96); }
.btn.primary {
  background: linear-gradient(120deg, var(--accent), var(--accent-deep));
  color: #04222b;
  box-shadow: 0 4px 16px rgba(34, 211, 238, .3);
}
.btn.big { padding: 15px 22px; font-size: 17px; }
.btn.ghost { background: var(--panel-2); border: 1px solid var(--line); }
.btn.subtle { background: none; color: var(--muted); text-decoration: underline; font-weight: 500; font-size: 13px; padding: 6px 2px; }
.btn.danger { background: var(--danger); color: #3b0a0a; }
.btn:disabled { opacity: .35; pointer-events: none; }

.icon-btn {
  width: 44px; height: 44px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 20px;
  display: grid;
  place-items: center;
  flex: none;
}
.icon-btn:active { transform: scale(.92); }

/* ═══════════ Level chrome ═══════════ */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}

.title-block { flex: 1; min-width: 0; }
#level-name { font-weight: 800; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title-block .sub { color: var(--muted); font-size: 11px; white-space: nowrap; }

.cost-chip {
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
}
.cost-chip b { color: var(--text); font-size: 14px; }
.cost-chip.over b { color: var(--warn); }
.cost-chip .unit { font-size: 9.5px; letter-spacing: .5px; }
@media (max-width: 430px) {
  .cost-chip .unit { display: none; }
  .topbar { gap: 7px; padding-left: 10px; padding-right: 10px; }
}

#board-wrap { position: relative; flex: 1; overflow: hidden; background: var(--bg); }

#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; }

/* floating controls */
.float {
  position: absolute;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 4;
}
.float.left { left: 12px; flex-direction: row; }
.float.right { right: 12px; }
.float .icon-btn { box-shadow: var(--shadow); }

#action-bar {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translate(-50%, 80px);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 10px 8px 18px;
  box-shadow: var(--shadow);
  transition: transform .18s ease;
  z-index: 6;
}
#action-bar.show { transform: translate(-50%, 0); }

/* clock transport for sequential levels */
#transport {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translate(-50%, 0);
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px;
  box-shadow: var(--shadow);
  transition: transform .18s ease;
  z-index: 5;
}
#transport[hidden] { display: none; }
/* step aside when the selection bar needs the same spot */
#transport.nudged { transform: translate(-50%, -62px); }

.tp {
  min-width: 40px; height: 40px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  font-size: 15px;
  display: grid;
  place-items: center;
  padding: 0 4px;
}
.tp:active { transform: scale(.93); }
.tp.play.running { background: var(--accent); color: #04222b; border-color: var(--accent); }
.tp.speed { font-size: 13px; font-weight: 700; color: var(--muted); }
#action-label { font-size: 14px; color: var(--muted); }

#wire-hint {
  position: absolute;
  top: 10px; left: 50%;
  transform: translate(-50%, -120px);
  visibility: hidden;
  background: var(--panel);
  border: 1px solid var(--accent);
  color: var(--text);
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 999px;
  transition: transform .2s, visibility .2s;
  z-index: 4;
  max-width: 92%;
  text-align: center;
}
#wire-hint.show { transform: translate(-50%, 0); visibility: visible; }
#wire-hint em { color: var(--muted); font-style: normal; }

#badge-unstable {
  position: absolute;
  top: 12px; right: 12px;
  background: #451a03;
  border: 1px solid var(--warn);
  color: var(--warn);
  font-size: 12.5px;
  font-weight: 700;
  padding: 6px 10px;
  border-radius: 999px;
  opacity: 0;
  transition: opacity .2s;
  z-index: 4;
  pointer-events: none;
}
#badge-unstable.show { opacity: 1; }

#badge-short {
  position: absolute;
  top: 12px; right: 12px;
  background: #4c0519;
  border: 1px solid var(--danger);
  color: var(--danger);
  font-size: 12.5px;
  font-weight: 700;
  padding: 6px 10px;
  border-radius: 999px;
  opacity: 0;
  transition: opacity .2s;
  z-index: 4;
  pointer-events: none;
}
#badge-short.show { opacity: 1; }

/* ═══════════ Palette ═══════════ */

#palette {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
  scrollbar-width: none;
  z-index: 5;
}
#palette::-webkit-scrollbar { display: none; }

.pal-item {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 7px 10px 6px;
  min-width: 74px;
  touch-action: none;
  position: relative;
}
.pal-item:active { border-color: var(--accent); }
.pal-item span { font-size: 11px; color: var(--muted); white-space: nowrap; }
.pal-item em {
  position: absolute;
  top: 3px; right: 5px;
  font-style: normal;
  font-size: 9.5px;
  color: var(--accent);
  opacity: .85;
}
.pal-icon { width: 52px; height: 36px; }

.pal-io { width: 46px; height: 30px; border-radius: 9px; border: 1.5px solid var(--off); margin: 3px 0; }
.pal-io.sw { background: linear-gradient(120deg, var(--panel), var(--off)); }
.pal-io.lamp { background: radial-gradient(circle at 50% 50%, var(--on-dim), var(--panel)); }

.pal-empty { color: var(--muted); font-size: 13.5px; padding: 12px 6px; }

/* ═══════════ Board graphics ═══════════ */

.board-bg { pointer-events: none; }
.grid-dot { fill: #1c2647; }

.part-body.gate, .part-body.chip {
  fill: var(--panel-2);
  stroke: var(--off);
  stroke-width: 1.6;
}
.part.selected .part-body { stroke: var(--accent); stroke-width: 2.4; }
.gate-bubble { fill: var(--bg); stroke: var(--off); stroke-width: 1.6; }
.part.selected .gate-bubble { stroke: var(--accent); }
.xor-arc { fill: none; stroke: var(--off); stroke-width: 1.6; }

.gate-name { fill: var(--muted); font-size: 9.5px; font-weight: 800; letter-spacing: .5px; pointer-events: none; }
.chip-name { fill: var(--text); font-size: 11.5px; font-weight: 800; letter-spacing: .4px; pointer-events: none; }
.pin-name { fill: var(--muted); font-size: 8.5px; pointer-events: none; }

.ghost rect { fill: var(--accent); opacity: .25; stroke: var(--accent); stroke-dasharray: 5 4; }

/* relays */
.relay-path { fill: none; stroke: var(--off); stroke-width: 2.8; stroke-linecap: round; }
.relay-path.on { stroke: var(--on); }
.relay-contact { fill: var(--muted); }
.relay-blade { fill: none; stroke: var(--text); stroke-width: 3.2; stroke-linecap: round; transition: none; }
.relay-coil { fill: none; stroke: var(--muted); stroke-width: 2.4; }
.relay-pull { fill: none; stroke: var(--muted); stroke-width: 1.4; stroke-dasharray: 2 3; }
.energized .relay-coil, .energized .relay-pull { stroke: var(--accent); }
.energized .relay-coil { filter: drop-shadow(0 0 3px rgba(34, 211, 238, .7)); }
.relay-label { fill: var(--muted); font-size: 9.5px; font-weight: 600; pointer-events: none; }
.relay-kind { fill: var(--text); font-size: 9px; font-weight: 800; letter-spacing: .6px; pointer-events: none; }

/* seven-segment display */
.seg { stroke: #1d2647; stroke-width: 7; stroke-linecap: round; transition: stroke .12s; }
.seg.lit { stroke: #ff5f4d; filter: drop-shadow(0 0 6px rgba(255, 95, 77, .75)); }

/* transistors */
.fet-channel { fill: none; stroke: var(--off); stroke-width: 3; stroke-linecap: round; stroke-dasharray: 2 4; opacity: .5; }
.conducting .fet-channel { stroke: var(--on); stroke-dasharray: none; opacity: 1; filter: drop-shadow(0 0 4px rgba(74, 222, 128, .7)); }
.fet-plate { fill: none; stroke: var(--muted); stroke-width: 2.4; stroke-linecap: round; }
.fet-gate { fill: none; stroke: var(--muted); stroke-width: 2.4; stroke-linecap: round; }
.conducting .fet-gate { stroke: var(--accent); }
.fet-bubble { fill: var(--bg); stroke: var(--muted); stroke-width: 1.8; }
.conducting .fet-bubble { stroke: var(--accent); }

/* a wire driven by nothing at all */
.wire.floating .wire-line { stroke: var(--off); stroke-dasharray: 2 6; opacity: .6; animation: none; }

/* pins */
.pin-stub { stroke: var(--off); stroke-width: 2; }
.pin.bus .pin-stub { stroke-width: 4.5; }
.pin-dot { fill: var(--bg); stroke: var(--off); stroke-width: 2; }
.pin-hit { fill: transparent; }
.pin.on .pin-dot, .pin.on .pin-stub { stroke: var(--on); }
.pin.pending .pin-dot {
  stroke: var(--accent);
  fill: var(--accent);
  animation: pulse 1s infinite;
}
@keyframes pulse { 50% { opacity: .4; } }

/* wires */
.wire-hit { fill: none; stroke: transparent; stroke-width: 20; }
.wire-line { fill: none; stroke: var(--off); stroke-width: 2.5; }
.wire.bus .wire-line { stroke-width: 5.5; }
.wire.on .wire-line {
  stroke: var(--on);
  filter: drop-shadow(0 0 4px rgba(74, 222, 128, .55));
  /* movement, not just colour, marks a live wire — readable without colour vision */
  stroke-dasharray: 11 7;
  animation: currentflow .55s linear infinite;
}
@keyframes currentflow { to { stroke-dashoffset: -18; } }
.wire.selected .wire-line { stroke: var(--accent); stroke-width: 4; }
.wire.bus.selected .wire-line { stroke-width: 7; }

.bus-badge rect { fill: var(--panel); stroke: var(--line); }
.bus-badge text { fill: var(--muted); font-size: 11px; font-weight: 700; font-family: ui-monospace, monospace; }
.wire.on .bus-badge text { fill: var(--on); }
.bus-badge { pointer-events: none; }

.wire-rubber {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
  stroke-dasharray: 7 5;
  pointer-events: none;
}
.wire-rubber.snappable { stroke: var(--on); }
.snap-ring { fill: none; stroke: var(--on); stroke-width: 2; pointer-events: none; }

/* IO parts */
.part-body.io { fill: var(--panel-2); stroke: var(--off); stroke-width: 1.6; }
.part.type-in.on .part-body { stroke: var(--on); }
.part.type-out.on .part-body { stroke: var(--on); }
.io-label { fill: var(--muted); font-size: 11px; font-weight: 700; letter-spacing: .4px; pointer-events: none; }
.io-value { fill: var(--text); font-size: 15px; font-weight: 800; font-family: ui-monospace, monospace; pointer-events: none; }
.io-sub { fill: var(--muted); font-size: 9px; font-family: ui-monospace, monospace; pointer-events: none; }

.sw-track { fill: var(--bg); stroke: var(--off); stroke-width: 1.5; transition: fill .15s; }
.sw-dot { fill: var(--off); transition: transform .15s, fill .15s; }
.sw-knob.on .sw-dot { transform: translateX(20px); fill: var(--on); }
.sw-knob.on .sw-track { stroke: var(--on); }

.bit-circle { fill: var(--bg); stroke: var(--off); stroke-width: 1.6; }
.bit-hit { fill: transparent; }
.bit-dot.on .bit-circle { fill: var(--on); stroke: var(--on); }

.led { fill: #17203c; stroke: var(--off); stroke-width: 1.6; }
.led.lit { fill: var(--on); stroke: var(--on); filter: drop-shadow(0 0 7px rgba(74, 222, 128, .8)); }
.led-digit { fill: var(--muted); font-size: 11px; font-weight: 800; font-family: ui-monospace, monospace; pointer-events: none; }
.part.type-out.on .led-digit { fill: #06301a; }

/* ═══════════ Sheets, toast, misc ═══════════ */

#scrim {
  position: fixed; inset: 0;
  background: rgba(3, 6, 16, .6);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 20;
}
#scrim.show { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translate(-50%, 105%);
  width: min(560px, 100%);
  max-height: 82dvh;
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom: none;
  border-radius: 22px 22px 0 0;
  box-shadow: var(--shadow);
  transition: transform .25s cubic-bezier(.3, 1.2, .4, 1);
  z-index: 21;
  display: flex;
}
.sheet.open { transform: translate(-50%, 0); }

.sheet-scroll {
  overflow-y: auto;
  padding: 26px 24px calc(24px + env(safe-area-inset-bottom));
  width: 100%;
  line-height: 1.55;
}
.sheet-scroll h2 { margin-bottom: 8px; }
.sheet-scroll p { margin: 8px 0; font-size: 14.5px; }
.help-blurb { color: var(--accent); font-weight: 600; }

.sheet-close {
  position: absolute;
  top: 10px; right: 12px;
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 15px;
  z-index: 2;
}

.truth {
  border-collapse: collapse;
  margin: 12px 0;
  font-family: ui-monospace, monospace;
  font-size: 13px;
}
.truth th, .truth td { padding: 4px 10px; text-align: center; color: var(--text); }
.truth th { color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--line); }
.truth td.div, .truth th.div { border-right: 1px solid var(--line); padding: 0; }
.truth .note { color: var(--muted); font-size: 11.5px; text-align: left; font-family: inherit; }
.truth.seq td { text-align: left; }
.spec-note { color: var(--muted); font-size: 13px; }

.hint-zone { margin-top: 14px; }
#hint-text { background: var(--panel-2); border-left: 3px solid var(--warn); padding: 10px 12px; border-radius: 8px; }
#hint-text.hidden { display: none; }

.sheet-actions { display: flex; gap: 10px; margin-top: 18px; justify-content: flex-end; flex-wrap: wrap; }

.stars { font-size: 38px; letter-spacing: 6px; margin-bottom: 4px; }
.star { color: var(--off); display: inline-block; }
.star.full { color: var(--star); text-shadow: 0 0 18px rgba(250, 204, 21, .6); }
.star.empty { opacity: .55; }
.star.will-pop { opacity: 0; transform: scale(0); }
.star.pop {
  animation: starpop .5s cubic-bezier(.2, 1.7, .4, 1) forwards;
  color: var(--star);
  text-shadow: 0 0 18px rgba(250, 204, 21, .65);
}
@keyframes starpop {
  0% { opacity: 0; transform: scale(0) rotate(-40deg); }
  60% { opacity: 1; transform: scale(1.45) rotate(10deg); }
  100% { opacity: 1; transform: scale(1) rotate(0deg); }
}

.pop-title { animation: titlerise .4s ease-out both; }
@keyframes titlerise {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: none; }
}

#board-wrap.won::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(circle at 50% 55%, rgba(74, 222, 128, .3), transparent 62%);
  animation: wonflash .95s ease-out forwards;
  z-index: 3;
}
@keyframes wonflash {
  0% { opacity: 0; }
  22% { opacity: 1; }
  100% { opacity: 0; }
}

.result-cost, .result-sub { color: var(--muted); }
.medal {
  display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap;
  background: linear-gradient(120deg, rgba(250,204,21,.14), rgba(250,204,21,.04));
  border: 1px solid var(--star);
  border-radius: var(--radius);
  padding: 11px 14px;
  margin: 12px 0;
  font-size: 14px;
}
.medal b { color: var(--star); }
.medal span { color: var(--muted); font-size: 12.5px; }
.par-nudge { color: var(--muted); font-size: 13px; }
.result-cost b { color: var(--text); }
.hint-note { color: var(--muted); font-size: 13.5px; }

.unlock-card {
  display: flex;
  gap: 14px;
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--accent-deep);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin: 14px 0;
  font-size: 14px;
  position: relative;
  overflow: hidden;
}
.unlock-card.reveal-wait { opacity: 0; transform: translateY(12px); }
.unlock-card.reveal {
  opacity: 1;
  transform: none;
  transition: opacity .35s ease-out, transform .35s ease-out;
}
.unlock-card.reveal::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(115deg, transparent 30%, rgba(34, 211, 238, .22) 50%, transparent 70%);
  transform: translateX(-100%);
  animation: shimmer .9s ease-out .25s forwards;
  pointer-events: none;
}
@keyframes shimmer { to { transform: translateX(100%); } }
.unlock-card span { color: var(--muted); font-size: 12.5px; }
.unlock-icon .pal-icon { width: 58px; height: 42px; }

.finale { color: var(--on); font-weight: 600; }

/* program editor */
.prog-presets { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 14px; }
.prog-presets .btn { padding: 7px 12px; font-size: 13px; }
.prog-rows { display: grid; gap: 4px; }
.prog-row {
  display: grid;
  grid-template-columns: 26px 1fr 1fr;
  gap: 8px;
  align-items: center;
}
.prog-addr { color: var(--muted); font-family: ui-monospace, monospace; font-size: 12px; text-align: right; }
.prog-row select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
  font: inherit;
  font-size: 14px;
  min-height: 40px;
}
.chip-status { fill: var(--accent); font-size: 8px; font-weight: 700; font-family: ui-monospace, monospace; pointer-events: none; }
.lesson { font-size: 14.5px; }
.lesson b { color: var(--accent); }

#toast {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  left: 50%;
  transform: translate(-50%, -80px);
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 30;
  transition: transform .22s;
  max-width: 92%;
  text-align: center;
  box-shadow: var(--shadow);
}
#toast.show { transform: translate(-50%, 0); }
#toast.warn { border-color: var(--warn); }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 40; overflow: hidden; }
.confetti i {
  position: absolute;
  top: -14px;
  width: 9px; height: 14px;
  border-radius: 2px;
  animation: fall ease-in forwards;
}
@keyframes fall {
  to { transform: translate(var(--sx, 0px), 106vh) rotate(660deg); opacity: .65; }
}

.icon-btn.compact { width: 38px; height: 38px; font-size: 16px; }
@media (max-width: 350px) { .icon-btn.compact { display: none; } }

@media (prefers-reduced-motion: reduce) {
  /* keep the dashes (the non-colour cue) but stop them moving */
  .wire.on .wire-line { animation: none; }
  .confetti { display: none; }
  .star.pop, .star.will-pop { animation: none; opacity: 1; transform: none; color: var(--star); }
  .pop-title { animation: none; }
  #board-wrap.won::after { animation: none; opacity: 0; }
  .unlock-card.reveal-wait, .unlock-card.reveal { opacity: 1; transform: none; transition: none; }
  .unlock-card.reveal::after { animation: none; }
}

/* Larger screens: roomier palette */
@media (min-width: 760px) {
  .pal-item { min-width: 84px; }
  #wire-hint { font-size: 14px; }
}

/* ═══════════ Breadboard edition ═══════════ */

body.skin-breadboard {
  --bb-board: #efe9db;
  --bb-hole: #8c8577;
  --bb-ink: #2b2926;
  --bb-ink-soft: #6b665d;
}
.skin-breadboard #board-wrap { background: var(--bb-board); }
.skin-breadboard #board-wrap.won::after { background: radial-gradient(circle at 50% 55%, rgba(67, 196, 101, .35), transparent 62%); }

/* board silkscreen text */
.skin-breadboard .io-label { fill: var(--bb-ink); }
.skin-breadboard .io-value { fill: var(--bb-ink); }
.skin-breadboard .io-sub { fill: var(--bb-ink-soft); }
.skin-breadboard .bus-badge rect { fill: #fffaf0; stroke: #c9bda3; }
.skin-breadboard .bus-badge text { fill: var(--bb-ink); }
.skin-breadboard .wire.on .bus-badge text { fill: #1a7f3c; }

/* DIP packages */
.skin-breadboard .dip-body { fill: #1a1b1f; stroke: #000; stroke-width: 1.2; }
.skin-breadboard .dip-notch { fill: var(--bb-board); stroke: #000; stroke-width: 1; }
.skin-breadboard .dip-pin1 { fill: #d8d8d8; }
.skin-breadboard .dip-name { fill: #f4f4f4; font-size: 11px; font-weight: 800; letter-spacing: .4px; pointer-events: none; }
.skin-breadboard .dip-name.small { font-size: 9px; }
.skin-breadboard .dip-num { fill: #a9afb8; font-size: 6.5px; font-family: ui-monospace, monospace; letter-spacing: .3px; pointer-events: none; }
.skin-breadboard .dip-logo { fill: none; stroke: #e8e8e8; stroke-width: 2.2; }
.skin-breadboard .dip-logo.bubble { fill: #1a1b1f; }
.skin-breadboard .pin-name { fill: #d0d4da; font-size: 7px; }
.skin-breadboard .chip-status { fill: #ffd23f; }
.skin-breadboard .part.selected .dip-body { stroke: var(--accent); stroke-width: 2.4; }

/* metal legs into holes */
.skin-breadboard .pin-stub { stroke: #b9bec8; stroke-width: 3; stroke-linecap: butt; }
.skin-breadboard .pin.bus .pin-stub { stroke-width: 4.5; }
.skin-breadboard .pin-dot { fill: #5f584d; stroke: #3d3832; stroke-width: 1; }
.skin-breadboard .pin.on .pin-dot { fill: #43c465; stroke: #2a8f46; }
.skin-breadboard .pin.on .pin-stub { stroke: #b9bec8; }
.skin-breadboard .pin.pending .pin-dot { fill: var(--accent); stroke: var(--accent); }

/* jumper wires: a dark outline and a solid colour, glowing when live */
.skin-breadboard .wire-outline { fill: none; stroke: #1c1a17; stroke-width: 6; stroke-linecap: round; }
.skin-breadboard .wire.bus .wire-outline { stroke-width: 8.5; }
.skin-breadboard .wire.bus8 .wire-outline { stroke-width: 10; }
.skin-breadboard .wire-line { stroke: var(--jc, #888); stroke-width: 3.5; opacity: .72; stroke-dasharray: none; animation: none; filter: none; }
.skin-breadboard .wire.bus .wire-line { stroke-width: 6; }
.skin-breadboard .wire.bus8 .wire-line { stroke-width: 7.5; }
.skin-breadboard .wire.on .wire-line {
  opacity: 1;
  stroke: var(--jc, #888);
  filter: drop-shadow(0 0 3px var(--jc, #888));
  stroke-dasharray: 10 6;
  animation: currentflow .55s linear infinite;
}
.skin-breadboard .wire.floating .wire-line { stroke-dasharray: 2 6; opacity: .45; animation: none; }
.skin-breadboard .wire.selected .wire-line { stroke: var(--accent); }
.skin-breadboard .wire-rubber { stroke: #1c1a17; }

/* DIP switches */
.skin-breadboard .in-body { fill: #2457c5; stroke: #143a8c; stroke-width: 1.4; }
.skin-breadboard .part.type-in.on .part-body { stroke: #143a8c; }
.skin-breadboard .dip-track { fill: #e9edf3; stroke: #a7b0c0; stroke-width: .8; }
.skin-breadboard .dip-cap { fill: #2b2f36; transition: transform .12s ease; }
.skin-breadboard .bit-dot.on .dip-cap, .skin-breadboard .sw-knob.on .dip-cap { transform: translateY(-9px); }
.skin-breadboard .dip-on { fill: #ffffff; font-size: 6px; font-weight: 800; pointer-events: none; }
.skin-breadboard .type-in .io-value { fill: #ffffff; }
.skin-breadboard .type-in .io-sub { fill: #cfe0ff; }

/* LEDs with a resistor leg */
.skin-breadboard .out-body { fill: var(--bb-board); stroke: none; }
.skin-breadboard .led { fill: #7a1212; stroke: #3a0909; stroke-width: 1; filter: none; }
.skin-breadboard .led.lit { fill: #ff3b3b; stroke: #ff7a7a; filter: drop-shadow(0 0 7px rgba(255, 70, 70, .85)); }
.skin-breadboard .led-base { fill: #b9bec8; }
.skin-breadboard .led-digit { fill: #ffd0d0; }
.skin-breadboard .part.type-out.on .led-digit { fill: #3a0000; }

/* relays and transistors as the parts they are */
.skin-breadboard .relay-body { fill: #1c4fa8; stroke: #0f2f6b; }
.skin-breadboard .relay-path { stroke: #cfd8ea; }
.skin-breadboard .relay-path.on { stroke: #43c465; }
.skin-breadboard .relay-contact { fill: #e6ecf7; }
.skin-breadboard .relay-blade { stroke: #ffffff; }
.skin-breadboard .relay-coil { stroke: #cfd8ea; }
.skin-breadboard .relay-pull { stroke: #cfd8ea; }
.skin-breadboard .relay-label { fill: #cfd8ea; }
.skin-breadboard .relay-kind { fill: #ffffff; }
.skin-breadboard .fet-body { fill: #141414; stroke: #000; }
.skin-breadboard .fet-plate, .skin-breadboard .fet-gate { stroke: #d8dbe0; }
.skin-breadboard .fet-bubble { fill: #141414; stroke: #d8dbe0; }
.skin-breadboard .fet-channel { stroke: #6e7480; }
.skin-breadboard .conducting .fet-channel { stroke: #43c465; }
.skin-breadboard .part-num { fill: #a9afb8; font-size: 6.5px; font-family: ui-monospace, monospace; pointer-events: none; }

/* selection ghost from the tray */
.skin-breadboard .ghost rect { fill: #1a1b1f; opacity: .35; stroke: #000; }

/* home-screen edition toggle */
.skin-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.skin-toggle button { padding: 7px 14px; font-size: 13px; font-weight: 600; color: var(--muted); }
.skin-toggle button.active { background: var(--panel-2); color: var(--text); }
.edition-note { color: var(--muted); font-size: 13px; margin-top: 4px; }
.edition-note b { color: #ff9f43; }
/* LED banks sit straight on the board — no schematic-style frame */
.skin-breadboard .part.type-out .part-body,
.skin-breadboard .part.type-out.on .part-body { fill: transparent; stroke: none; }
.skin-breadboard .part.type-out.selected .part-body { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 4 3; }

/* ═══════════ Modular boards ═══════════ */
.board-outline { fill: rgba(255,255,255,.015); stroke: var(--line); stroke-width: 1.2; stroke-dasharray: 6 5; }
.board-tag rect { fill: var(--panel-2); stroke: var(--line); }
.board-tag text { fill: var(--muted); font-size: 9px; font-weight: 800; letter-spacing: 1px; }
.skin-breadboard .board-shadow { fill: rgba(0,0,0,.45); transform: translate(3px, 4px); }
.skin-breadboard .board-face { stroke: #cfc7b5; stroke-width: 1.2; }
.skin-breadboard .board-tag rect { fill: #1d1b19; stroke: none; }
.skin-breadboard .board-tag text { fill: #f3efe6; }
.skin-breadboard .bus-trace { stroke: #6f6a60; stroke-width: 2; }
.skin-breadboard .board.bus .board-face { fill: #e6dfd0; }

/* the bus bar */
.busbar-body { fill: var(--panel-2); stroke: var(--accent-deep); stroke-width: 1.6; }
.part.floating .busbar-body { stroke-dasharray: 5 4; }
.skin-breadboard .busbar-body { fill: #1d1b19; stroke: #4a4540; }
.skin-breadboard .busbar-body + .chip-name { fill: #f3efe6; }

/* microcode editor */
.uc-grid { display: grid; grid-template-columns: 46px repeat(5, 1fr); gap: 4px; margin-top: 6px; }
.uc-head { text-align: center; color: var(--muted); font-size: 11px; font-weight: 700; padding: 4px 0; }
.uc-op { align-self: center; font-weight: 800; font-size: 12px; color: var(--muted); font-family: ui-monospace, monospace; }
.uc-cell {
  min-height: 38px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2);
  font-family: ui-monospace, monospace; font-size: 10px; line-height: 1.25; padding: 4px 3px; color: var(--text);
  word-break: break-word;
}
.uc-cell.fetch { color: var(--muted); }
.uc-cell.sel { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(34, 211, 238, .25); }
.uc-edit { margin-top: 14px; }
.uc-sel { font-weight: 800; margin-bottom: 6px; }
.uc-toggles { display: flex; flex-wrap: wrap; gap: 6px; }
.uc-tog { padding: 7px 10px; border-radius: 999px; border: 1px solid var(--line); font-size: 12px; font-weight: 700; color: var(--muted); min-width: 44px; }
.uc-tog.on { background: var(--accent); color: #04222b; border-color: var(--accent); }

/* conditional microcode cells (chapter 21) */
.uc-cell i { display: block; font-style: normal; font-size: 10px; color: var(--accent); }
.uc-gap { flex-basis: 100%; height: 0; }
.uc-tog.cond { border-style: dashed; }
.uc-tog.cond.on { border-style: solid; }

/* segment-table editor (chapter 22) */
.sg-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 8px; }
.sg-cell { position: relative; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 6px 4px 2px; }
.sg-code { position: absolute; top: 4px; left: 7px; font-size: 11px; font-weight: 800; color: var(--muted); font-family: ui-monospace, monospace; }
.sg-digit { display: block; width: 100%; height: auto; }
.sg { stroke: #1d2647; stroke-width: 8; stroke-linecap: round; pointer-events: none; transition: stroke .1s; }
.sg.lit { stroke: #ff5f4d; filter: drop-shadow(0 0 5px rgba(255, 95, 77, .7)); }
.sg-hit { stroke: transparent; stroke-width: 20; stroke-linecap: round; cursor: pointer; pointer-events: stroke; }

/* ——— part cards: hover on desktop, press-and-hold on touch ——— */
#part-tip {
  position: fixed; z-index: 40; pointer-events: none;
  max-width: min(340px, calc(100vw - 24px)); max-height: 60vh; overflow: hidden;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  padding: 9px 12px 10px; font-size: 12.5px; line-height: 1.35; color: var(--text);
}
#part-tip[hidden] { display: none; }
.tip-name { display: flex; align-items: baseline; gap: 8px; margin-bottom: 3px; }
.tip-name b { font-size: 13.5px; }
.tip-chip { color: var(--muted); font-family: ui-monospace, monospace; font-size: 11px; }
.tip-desc { color: var(--text); opacity: .9; }
.tip-pins { color: var(--muted); font-family: ui-monospace, monospace; font-size: 11px; margin-top: 4px; }
.tip-label { margin-top: 7px; font-size: 10px; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
#part-tip .truth { margin: 4px 0 0; font-size: 11.5px; }
#part-tip .truth th, #part-tip .truth td { padding: 1px 7px; }
.tip-more { color: var(--muted); font-size: 11px; margin-top: 4px; }

/* ——— the spec overlay: the level's table, pinned over the board ——— */
#spec-overlay {
  position: absolute; top: 12px; left: 12px; z-index: 5;
  max-width: min(440px, calc(100% - 24px)); max-height: 46%; overflow: auto;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  padding: 8px 12px 10px; font-size: 12.5px; opacity: .96;
}
#spec-overlay[hidden] { display: none; }
.spec-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.spec-toggle { background: none; border: 0; color: var(--text); font-weight: 800; font-size: 13px; padding: 2px 0; cursor: pointer; display: flex; align-items: center; gap: 6px; font-family: inherit; }
.spec-chev { display: inline-block; color: var(--muted); transition: transform .15s; }
#spec-overlay.collapsed .spec-chev { transform: rotate(-90deg); }
#spec-overlay.collapsed #spec-body { display: none; }
#spec-overlay.collapsed { padding: 4px 12px 4px 10px; border-radius: 999px; max-height: none; overflow: hidden; }
@media (max-width: 700px) { #spec-overlay { max-width: calc(100% - 24px); max-height: 36%; } }
.spec-close { background: none; border: 0; color: var(--muted); font-size: 15px; padding: 2px 4px; cursor: pointer; }
.spec-blurb { margin: 4px 0 2px; color: var(--text); opacity: .9; }
#spec-overlay .truth { margin: 6px 0 2px; font-size: 11.5px; }
#spec-overlay .truth th, #spec-overlay .truth td { padding: 2px 7px; }
#spec-overlay .spec-note { margin: 6px 0 0; }
.spec-runs { margin: 6px 0 0; padding-left: 18px; }
.spec-runs li { margin: 2px 0; }
#btn-spec.active { border-color: var(--accent); color: var(--accent); }

/* the ⓘ on every tray item — the touch way to a part's card */
.pal-info {
  position: absolute; top: 3px; left: 5px; width: 17px; height: 17px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--muted);
  font: italic 700 11px/15px Georgia, 'Times New Roman', serif; text-align: center; touch-action: none;
}
.pal-item:active .pal-info { border-color: var(--line); }

/* acts on the home screen, chapter intros in the brief */
.act-head { margin: 38px 0 -14px; padding-top: 16px; border-top: 1px solid var(--line); }
.act-head h2 { font-size: 13px; letter-spacing: .12em; text-transform: uppercase; color: var(--accent); margin: 0 0 4px; }
.act-head p { color: var(--muted); font-size: 13px; margin: 0; }
.chapter-intro { background: var(--panel-2); border-left: 3px solid var(--accent); border-radius: 8px; padding: 8px 12px; margin: 0 0 14px; font-size: 13px; }
.chapter-tag { display: block; font-size: 10.5px; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); font-weight: 800; margin-bottom: 3px; }
.chapter-intro p { margin: 0; color: var(--text); opacity: .9; }

/* the overlay's "yours" columns: what the circuit gives right now */
.truth th.yours { color: var(--accent); }
.truth td.ok { color: #3ddc84; }
.truth td.bad { color: #ff5c5c; font-weight: 800; }
.truth tr.after td { opacity: .4; }
.spec-score { margin: 6px 0 0; font-size: 12px; font-weight: 700; color: #ff9f43; }
.spec-score.all { color: #3ddc84; }
