:root {
  --brand:        #1B4F8A;
  --brand-pale:   #C3D9EF;
  --brand-deep:   #0A2C52;
  --brand-bright: #12B5D4;
  --accent:       #E0714B;
  --accent-pale:  #FAE2D8;
  --good:         #7CB342;
  --good-pale:    #E4F2CE;
  --warn-c:       #E3A13A;
  --warn-pale:    #FAE5C6;

  --bg-page:        #FFFFFF;
  --bg-soft:        #F5F8FB;
  --bg-board:       #E3EDF6;
  --border:         #D9E2EC;
  --border-strong:  #B6C6D6;
  --text:           #0F1D33;
  --text-soft:      #47566B;
  --text-muted:     #65748A;
  --primary:        var(--brand);
  --primary-hover:  var(--brand-deep);
  --error:          #C7402D;
  --ok:             #2E8540;

  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, "JetBrains Mono", Consolas, monospace;

  --shadow-sm: 0 1px 2px rgba(14,27,58,0.06);
  --shadow-md: 0 4px 16px rgba(14,27,58,0.08);
  --radius: 6px;
  --radius-lg: 10px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; min-height: 100%;
  font-family: var(--font-sans);
  font-size: 16px; line-height: 1.5;
  color: var(--text); background: var(--bg-soft);
}
body { display: flex; flex-direction: column; min-height: 100vh; }
.mono { font-family: var(--font-mono); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.app-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 24px;
  background: var(--bg-page);
  border-bottom: 3px solid var(--primary);
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-mark { height: 36px; width: auto; flex-shrink: 0; color: var(--primary); }
.brand-text h1 { margin: 0; font-size: 22px; font-weight: 800; color: var(--primary); letter-spacing: -0.01em; }
.brand-text .subtitle { margin: 0; font-size: 13px; color: var(--text-muted); }
.header-actions { display: flex; gap: 8px; flex-shrink: 0; }

.btn {
  font-family: var(--font-sans); font-size: 15px; font-weight: 700;
  padding: 9px 18px; border-radius: var(--radius);
  border: 1px solid transparent; cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--bg-page); color: var(--primary); border-color: var(--border-strong); }
.btn-secondary:hover { border-color: var(--primary); }
.btn-ghost { background: transparent; color: var(--text-soft); border-color: var(--border); }
.btn-ghost:hover { color: var(--primary); border-color: var(--primary); }
.btn:disabled { opacity: 0.45; cursor: default; pointer-events: none; }
.btn:focus-visible, .chip:focus-visible, .level-btn:focus-visible {
  outline: 3px solid var(--brand-bright); outline-offset: 2px;
}

.game {
  flex: 1; width: 100%; max-width: 1560px; margin: 0 auto; padding: 18px 16px 40px;
  display: grid; grid-template-columns: 336px minmax(0, 1fr) 210px; gap: 16px; align-items: start;
}
.panel {
  background: var(--bg-page); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); padding: 16px 18px;
}
.panel h2 { margin: 0 0 8px; font-size: 20px; color: var(--primary); }
.panel h3 { margin: 16px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }
.panel p { margin: 0 0 10px; font-size: 15px; color: var(--text-soft); }
.panel .eyebrow { margin: 0 0 2px; font-size: 12.5px; font-weight: 700; color: var(--text-muted); }
.panel .extra { font-size: 14px; }
.tip { margin-top: 12px; border-top: 1px solid var(--border); padding-top: 8px; }
.tip summary { cursor: pointer; font-weight: 700; color: var(--primary); font-size: 14px; }
.tip p { margin: 6px 0 0; font-size: 14px; }

.spec { border-collapse: collapse; font-size: 14px; width: auto; }
.spec th, .spec td { padding: 3px 9px; text-align: center; border-bottom: 1px solid var(--border); }
.spec th { font-size: 12.5px; color: var(--text-muted); font-weight: 700; }
.spec thead tr:first-child th { color: var(--brand-deep); font-size: 13px; }
.spec .sep { border-left: 2px solid var(--border-strong); }
.spec tr.ok td { background: var(--good-pale); }
.spec tr.bad td { background: var(--accent-pale); }
.spec .mark { font-weight: 900; }
.spec tr.ok .mark { color: var(--ok); }
.spec tr.bad .mark, .spec tr.part .mark { color: var(--error); }
.spec .actual { color: var(--error); font-weight: 700; }
.spec .expected { color: var(--text-muted); font-size: 12px; font-family: var(--font-sans); }
.spec td.cell-ok { background: var(--good-pale); }
.spec td.cell-bad { background: var(--accent-pale); }
.spec td.cell-unknown { background: var(--warn-pale); }
.spec th.col-ok { color: var(--ok); background: var(--good-pale); }
.table-scroll thead th.col-ok { background: var(--good-pale); }
.live-note { margin: 0 0 6px; font-size: 13px; font-weight: 700; color: var(--brand-deep); }
.live-note.all-ok { color: var(--ok); }
.live-note.wait { font-weight: 400; color: var(--text-muted); }
.live-cols { margin: -3px 0 6px; font-size: 12.5px; color: var(--text-muted); }
.live-cols .col-stat { font-family: var(--font-mono); font-weight: 700; }
.live-cols .col-stat.done { color: var(--ok); }
.spec.live { font-size: 12.5px; }
.spec.live th, .spec.live td { padding: 2px 4px; }
.spec.live tbody tr { cursor: pointer; }
.spec.live tbody tr:hover td { filter: brightness(0.96); }
.spec.live tbody tr.current td { font-weight: 700; }
.spec.live tbody tr.current td:first-child { box-shadow: inset 3px 0 0 var(--primary); }
.spec.live tbody tr:focus-visible { outline: 3px solid var(--brand-bright); outline-offset: -3px; }

.table-scroll { max-height: 46vh; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); }
.table-scroll .spec { width: 100%; }
.table-scroll thead th { position: sticky; top: 0; background: var(--bg-page); }

.level-btn {
  display: flex; align-items: center; gap: 8px; width: 100%;
  font: inherit; font-size: 15px; text-align: left; padding: 6px 8px;
  border: none; border-radius: var(--radius); background: transparent; color: var(--text); cursor: pointer;
}
.level-btn:hover { background: var(--bg-soft); }
.level-btn .mark { width: 16px; text-align: center; font-weight: 900; color: var(--primary); flex-shrink: 0; }
.level-btn.solved .mark { color: var(--ok); }
.level-btn.skipped .mark { color: var(--text-muted); }
.level-btn.locked { color: var(--text-muted); cursor: default; }
.level-btn.locked:hover { background: transparent; }
.level-btn.current { background: var(--brand-pale); font-weight: 700; }
.progress { margin: 14px 0 0; font-weight: 700; color: var(--brand-deep); }

.workbench { min-width: 0; }
.toolbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: var(--bg-page); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-sm);
  padding: 10px 14px; margin-bottom: 12px;
}
.status { margin-left: auto; font-size: 14px; color: var(--error); font-weight: 700; }
.status.info { color: var(--brand-deep); font-weight: 600; }
.bench {
  display: grid; grid-template-columns: 164px minmax(0, 1fr); gap: 12px;
  background: var(--bg-page); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-md); padding: 12px;
}
.toolbox { display: flex; flex-direction: column; min-height: 100%; }
.toolbox h2 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }
.chips { display: grid; grid-template-columns: 1fr; gap: 2px; }
.chips.dense { grid-template-columns: 1fr 1fr; }
.chip {
  cursor: grab; border-radius: var(--radius); padding: 2px 3px; touch-action: none;
  user-select: none; -webkit-user-select: none; min-width: 0;
}
.chip:hover { background: var(--bg-soft); }
.chip:active { cursor: grabbing; }
.chip-svg { display: block; width: 100%; height: auto; max-height: 86px; margin: 0 auto; }
.chips.dense .chip-svg { max-height: 68px; }
.chips.dense .chip.wide { grid-column: 1 / -1; }
.chips.dense .chip.wide .chip-svg { max-height: 80px; }
.chip-ghost {
  position: fixed; z-index: 50; pointer-events: none; opacity: 0.85;
  transform: translate(-50%, -50%); background: transparent;
}
.trash {
  margin-top: auto; padding: 14px 8px; text-align: center; font-size: 12.5px; color: var(--text-muted);
  border: 2px dashed var(--border-strong); border-radius: var(--radius); background: var(--bg-soft);
}
.trash.hot { border-color: var(--error); color: var(--error); background: var(--accent-pale); }
.canvas-wrap { min-width: 0; }
#canvas {
  display: block; width: 100%; height: auto; touch-action: none;
  user-select: none; -webkit-user-select: none;
}

.board { fill: var(--bg-board); stroke: var(--brand-deep); stroke-width: 2; }
.board-hint { font-family: var(--font-sans); font-size: 17px; fill: var(--text-muted); text-anchor: middle; pointer-events: none; }
.board-hint.small { font-size: 14px; }
.io-caption { font-family: var(--font-sans); font-size: 12.5px; font-weight: 700; fill: var(--text-muted); }

.node { cursor: grab; }
.node:active { cursor: grabbing; }
.node-body { fill: var(--bg-page); stroke: var(--brand-deep); stroke-width: 2.2; }
.node.selected .node-body { stroke: var(--accent); stroke-width: 3; }
.node-label { font-family: var(--font-sans); font-size: 16px; font-weight: 700; fill: var(--brand-deep); text-anchor: middle; pointer-events: none; }
.node-label.with-sub { font-size: 14px; }
.node-sub { font-family: var(--font-sans); font-size: 9.5px; fill: var(--text-muted); text-anchor: middle; pointer-events: none; }
.node-del circle { fill: var(--accent); stroke: var(--bg-page); stroke-width: 2; cursor: pointer; }
.node-del text { font-family: var(--font-sans); font-size: 14px; font-weight: 900; fill: #fff; text-anchor: middle; pointer-events: none; }

.conn { cursor: crosshair; }
.conn-hit { fill: transparent; }
.conn-dot { fill: var(--bg-page); stroke: var(--brand-deep); stroke-width: 2; }
.conn:hover .conn-dot { fill: var(--brand-pale); stroke: var(--primary); }
.conn.pending .conn-dot { fill: var(--brand-bright); stroke: var(--brand-deep); stroke-width: 3; }
.conn.pending .conn-hit { fill: rgba(18,181,212,0.18); }
.conn-label { font-family: var(--font-mono); font-size: 11px; font-weight: 700; fill: var(--brand-deep); text-anchor: middle; pointer-events: none; }
.conn-label.tiny { font-size: 8px; }
.conn-width { font-family: var(--font-sans); font-size: 9px; fill: var(--text-muted); pointer-events: none; }
.badge { fill: #E7ECF4; stroke: none; pointer-events: none; }
.badge.on { fill: var(--primary); }
.badge-text { font-family: var(--font-mono); font-size: 10.5px; font-weight: 700; fill: var(--text-soft); text-anchor: middle; pointer-events: none; }
.badge.on + .badge-text { fill: #fff; }

.wire { stroke: #8593AB; stroke-width: 2; stroke-linecap: round; pointer-events: none; }
.wire.on { stroke: var(--primary); stroke-width: 3.4; }
.wire.selected { stroke: var(--accent); stroke-width: 4.5; }
.wire.ghost { stroke: var(--brand-bright); stroke-width: 3; stroke-dasharray: 6 5; }
.wire-hit { stroke: transparent; stroke-width: 16; cursor: pointer; }
.wire-hit:hover + .wire, .wires .wire-hit:hover ~ .wire { }

.io-body { fill: var(--bg-page); stroke: var(--brand-deep); stroke-width: 2.2; }
.iobox.toggle { cursor: pointer; }
.iobox.toggle:hover .io-body { fill: var(--brand-pale); }
.io-val { font-family: var(--font-mono); font-size: 24px; font-weight: 700; fill: var(--text); text-anchor: middle; pointer-events: none; }
.io-bin { font-family: var(--font-mono); font-size: 12.5px; font-weight: 700; fill: var(--text); text-anchor: middle; pointer-events: none; }
.io-sub { font-family: var(--font-sans); font-size: 10.5px; fill: var(--text-muted); text-anchor: middle; pointer-events: none; }
.io-sub.strong { font-weight: 700; fill: var(--brand-deep); font-size: 12px; }

.inputs-panel {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  margin-top: 12px; padding: 10px 14px;
  background: var(--bg-page); border: 1px solid var(--border); border-radius: var(--radius-lg);
  font-size: 14px;
}
.panel-label { font-weight: 700; color: var(--brand-deep); }
.in-bit { display: inline-flex; align-items: center; gap: 4px; font-family: var(--font-mono); font-weight: 700; cursor: pointer; }
.in-bit input { width: 18px; height: 18px; accent-color: var(--primary); }
.in-const { color: var(--text-muted); font-family: var(--font-mono); }
.in16 { display: inline-flex; align-items: center; gap: 10px; }
.in16 .in-name { font-family: var(--font-mono); font-weight: 700; }
.in16 label { display: inline-flex; align-items: center; gap: 4px; color: var(--text-muted); }
.in-hex, .in-dec {
  font-size: 14px; padding: 4px 6px; border: 1.5px solid var(--border-strong); border-radius: var(--radius);
  text-align: right;
}
.in-hex { width: 6ch; } .in-dec { width: 8ch; }
.in-hex:focus, .in-dec:focus { outline: none; border-color: var(--primary); }

.modal {
  position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center;
  background: rgba(14,27,58,0.45); padding: 16px;
}
.modal[hidden] { display: none; }
.modal-card {
  position: relative; background: var(--bg-page); border-radius: var(--radius-lg); box-shadow: var(--shadow-md);
  padding: 22px 24px; width: min(680px, 100%); max-height: 92vh; overflow: auto;
}
.modal-card h2 { margin: 0 0 12px; color: var(--primary); font-size: 20px; }
.modal-card h3 { margin: 18px 0 6px; font-size: 15px; color: var(--brand-deep); }
.modal-card p { margin: 0 0 10px; font-size: 15px; color: var(--text-soft); }
.modal-close {
  position: absolute; top: 10px; right: 12px; border: none; background: transparent;
  font-size: 26px; line-height: 1; color: var(--text-muted); cursor: pointer;
}
.modal-close:hover { color: var(--primary); }
.modal-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.banner { font-weight: 700; padding: 10px 14px; border-radius: var(--radius); }
.banner.ok { background: var(--good-pale); color: var(--ok); }
.banner.bad { background: var(--accent-pale); color: var(--error); }
.result .table-scroll { margin: 12px 0; }
.result .verdict { font-weight: 700; color: var(--brand-deep); }
.result .grants { background: var(--bg-soft); padding: 8px 12px; border-radius: var(--radius); border-left: 3px solid var(--primary); }
.solution {
  margin-top: 14px; padding: 12px 14px; background: var(--bg-soft);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.solution h3 { margin: 0 0 2px; }
.solution .meta { margin: 0 0 8px; font-size: 12.5px; color: var(--text-muted); }
.solution p:last-of-type { margin-bottom: 10px; }
.progress-dialog textarea {
  width: 100%; font-size: 12px; padding: 8px; border: 1.5px solid var(--border-strong); border-radius: var(--radius);
  resize: vertical;
}
.progress-dialog input[type="file"] { margin-top: 8px; font-size: 14px; }
.io-msg { font-weight: 700; color: var(--brand-deep); }

.app-footer {
  padding: 14px 24px; font-size: 13px; color: var(--text-muted);
  border-top: 1px solid var(--border); background: var(--bg-page);
}
.app-footer a { color: var(--primary); }

.fs .app-footer { display: none; }
.fs .app-header { padding: 5px 16px; }
.fs .brand-mark { height: 26px; }
.fs .brand-text h1 { font-size: 17px; }
.fs .brand-text .subtitle { display: none; }
.fs .game { max-width: none; padding: 10px 12px 10px; gap: 12px; }
.fs .toolbar { margin-bottom: 10px; padding: 8px 12px; }
.fs .inputs-panel { margin-top: 10px; padding: 8px 12px; }
.fs #canvas { height: calc(100vh - 232px); min-height: 320px; }
.fs .panel-help, .fs .panel-levels { max-height: calc(100vh - 80px); overflow: auto; }
.fs .modal-card { max-height: 94vh; }

@media (max-width: 1180px) {
  .game { grid-template-columns: minmax(0, 1fr); }
  .panel-levels { order: 3; }
  .panel-help { order: 1; }
  .workbench { order: 2; }
}
@media (max-width: 640px) {
  .bench { grid-template-columns: minmax(0, 1fr); }
  .chips { flex-direction: row; flex-wrap: wrap; }
  .chip { width: 110px; }
  .trash { margin-top: 8px; }
  .app-header { flex-wrap: wrap; }
}

.spec .stepcol { color: var(--text-muted); font-weight: 700; border-right: 2px solid var(--border-strong); }
.spec.seq tbody tr { cursor: default; }
