:root {
  color-scheme: dark;
  --bg: #0c1117;
  --panel: #151d26;
  --panel-2: #1b2631;
  --line: #2b3946;
  --text: #e8f0f4;
  --muted: #82929e;
  --cyan: #52d5d0;
  --amber: #f6b94d;
  --red: #ef6a65;
  --green: #77d49b;
  --sorter: #7fa3f5;
  --counter: #9fd468;
  --marker: #f08fb0;
  --router: #ef8a5e;
 #9b6ac5;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-width: 940px; min-height: 650px; height: 100%; overflow: hidden; }
body { display: flex; flex-direction: column; background: var(--bg); color: var(--text); font: 14px/1.4 Inter, ui-sans-serif, system-ui, sans-serif; }
button, select { font: inherit; }
button { color: var(--text); background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; cursor: pointer; }
button:hover { border-color: #597080; background: #23313d; }
button:disabled { cursor: default; opacity: .42; }
button:disabled:hover { border-color: var(--line); background: var(--panel-2); }
button:focus-visible, select:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

.topbar { min-height: 70px; padding: 8px 20px; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px; border-bottom: 1px solid var(--line); background: #10171e; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { display: grid; place-items: center; width: 38px; height: 38px; border: 2px solid var(--cyan); border-radius: 8px; color: var(--cyan); font-weight: 900; font-size: 21px; transform: rotate(-3deg); }
h1, h2, p { margin: 0; }
h1 { font-size: 17px; letter-spacing: .24em; }
.brand p { color: var(--muted); font-size: 11px; letter-spacing: .05em; }
.sim-controls { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 6px; }
.level-badge { min-width: 76px; color: var(--cyan); font-size: 10px; font-weight: 800; letter-spacing: .08em; text-align: center; }
.objective-badge { padding: 7px 10px; border: 1px solid #3d7665; border-radius: 6px; color: #baf4d4; background: #17342e; font: 800 11px ui-monospace, monospace; white-space: nowrap; }
.objective-badge.won { border-color: #9af0b7; background: #24533a; }
.sim-controls button, .sim-controls select { height: 36px; padding: 0 13px; }
.sim-controls button[hidden] { display: none; }
.sim-controls label { display: flex; align-items: center; gap: 7px; color: var(--muted); }
.sim-controls select { color: var(--text); background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; }
.sim-controls .danger { color: #ffaaa6; }

main { display: grid; grid-template-columns: minmax(680px, 1fr) 290px; flex: 1; min-height: 0; }
.workspace { min-width: 0; display: flex; flex-direction: column; }
.toolbar { min-height: 58px; padding: 10px 14px; display: flex; gap: 7px; border-bottom: 1px solid var(--line); background: #121a22; }
.tool { position: relative; display: flex; align-items: center; gap: 7px; padding: 8px 10px; }
.tool[hidden] { display: none; }
.tool span { color: var(--cyan); font-size: 17px; }
.tool.active { border-color: var(--cyan); background: #173039; box-shadow: inset 0 -2px var(--cyan); }
kbd { margin-left: 5px; padding: 1px 5px; color: var(--muted); background: #0d141b; border: 1px solid #33424f; border-radius: 3px; font-size: 10px; }
.board-shell { position: relative; flex: 1; min-height: 0; padding: 14px; display: grid; place-items: center; background: radial-gradient(circle at center, #17222b 0, #0b1117 74%); }
#gameCanvas { max-width: 100%; max-height: 100%; border: 1px solid #334450; border-radius: 5px; box-shadow: 0 18px 50px #0008; background: #101820; cursor: crosshair; }
#gameCanvas.dragging { cursor: grabbing; }

#gameCanvas { cursor: alias; }
.tutorial-highlight { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.tutorial-highlight[hidden] { display: none; }
.tutorial-mark { position: absolute; box-sizing: border-box; display: grid; place-items: center; border: 2px solid #ffe28a; border-radius: 5px; box-shadow: 0 0 0 3px #f6b94d30; background: #f6b94d0a; animation: tutorial-pulse 1.8s ease-in-out infinite; }
.tutorial-mark.satisfied { border-color: #93e8b5; background: transparent; box-shadow: none; opacity: .55; animation: none; }
.tutorial-marker-label { position: absolute; bottom: calc(100% + 3px); left: 50%; transform: translateX(-50%); padding: 2px 5px; border-radius: 3px; background: #342c19; color: #ffe6a1; font-size: 10px; font-weight: 800; white-space: nowrap; }
.tutorial-marker-arrow { color: #ffe6a199; font-size: 25px; font-weight: 800; text-shadow: 0 1px 3px #000; }
@keyframes tutorial-pulse { 50% { box-shadow: 0 0 0 6px #f6b94d22; } }
@media (prefers-reduced-motion: reduce) { .tutorial-mark { animation: none; } }
.setup-tooltip { position: absolute; z-index: 4; max-width: 300px; padding: 9px 11px; border: 1px solid var(--red); border-radius: 6px; background: #211b20f2; box-shadow: 0 8px 24px #0009; color: #f6e5e3; font-size: 12px; line-height: 1.4; pointer-events: none; }
.setup-tooltip[hidden] { display: none; }
.setup-tooltip strong { display: block; margin-bottom: 4px; color: #ffaaa6; }
.setup-tooltip ul { margin: 0; padding-left: 16px; }
.setup-tooltip li + li { margin-top: 3px; }
.toast { position: absolute; bottom: 26px; left: 50%; transform: translate(-50%, 12px); padding: 8px 13px; border: 1px solid #47606e; border-radius: 5px; background: #111b22e8; opacity: 0; pointer-events: none; transition: .18s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.victory-overlay { position: absolute; inset: 14px; z-index: 6; display: grid; place-items: center; background: #07120fc9; }
.victory-overlay[hidden] { display: none; }
.victory-card { width: min(360px, 90%); padding: 28px; border: 2px solid #9af0b7; border-radius: 12px; background: #16352c; box-shadow: 0 20px 60px #000b; text-align: center; }
.victory-card strong { display: block; margin-bottom: 10px; color: #d5ffe5; font-size: 24px; }
.victory-card p { color: #badbcb; }
.victory-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 9px; margin-top: 20px; }
.victory-actions button { padding: 8px 14px; }
.escape-menu { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; background: #071016dc; }
.escape-menu[hidden] { display: none; }
.escape-menu-card { width: min(380px, calc(100% - 40px)); padding: 28px; border: 2px solid #5d8791; border-radius: 12px; background: #17252e; box-shadow: 0 20px 60px #000b; text-align: center; }
.escape-menu-card strong { display: block; margin-bottom: 10px; color: #e8f0f4; font-size: 24px; }
.escape-menu-card p { color: #aebfc7; }
.escape-menu-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; margin-top: 20px; }
.escape-menu-actions button { padding: 8px 14px; }
.escape-menu-actions .danger { border-color: #a84e4c; color: #ffaaa6; }
.menu-board-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-top: 20px; padding-top: 15px; border-top: 1px solid var(--line); }
.menu-board-actions span { grid-column: 1 / -1; color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .12em; text-align: left; text-transform: uppercase; }
.menu-board-actions button { padding: 8px; }
.menu-board-actions button[hidden] { display: none; }
.menu-board-actions .danger { grid-column: 1 / -1; border-color: #a84e4c; color: #ffaaa6; }
.main-menu { background: radial-gradient(circle at 50% 25%, #183038, #0b1219 72%); }
.main-menu-card { width: min(780px, calc(100% - 48px)); max-height: calc(100vh - 48px); overflow-y: auto; padding: 28px; border: 1px solid #41616a; border-radius: 14px; background: #14222b; box-shadow: 0 25px 75px #0009; }
.main-menu-header { display: flex; align-items: center; gap: 18px; padding-bottom: 22px; border-bottom: 1px solid var(--line); }
.main-menu-header .brand-mark { width: 52px; height: 52px; font-size: 30px; }
.menu-eyebrow { display: block; color: var(--cyan); font-size: 11px; font-weight: 800; letter-spacing: .24em; }
.main-menu-header strong { display: block; color: var(--text); font-size: 28px; }
.main-menu-header p { color: #aebfc7; }
.main-menu-section-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-top: 20px; }
.main-menu-section-heading span { color: var(--muted); font-size: 12px; }
.flow-card { width: min(650px, calc(100% - 40px)); max-height: calc(100vh - 60px); overflow-y: auto; padding: 24px; border: 2px solid #5d8791; border-radius: 12px; background: #17252e; box-shadow: 0 20px 60px #000b; }
.flow-card > strong { display: block; margin-bottom: 10px; font-size: 24px; }
.flow-card > p, .flow-card li { color: #b8c9d1; line-height: 1.5; }
.flow-card > button { margin-top: 18px; padding: 8px 14px; }
.flow-card > label { display: block; margin: 15px 0; }
.flow-card select { margin-left: 10px; padding: 5px; color: var(--text); background: var(--panel-2); border: 1px solid var(--line); }
.scenario-list { display: grid; gap: 8px; margin-top: 12px; }
.scenario-row { display: flex; align-items: center; justify-content: space-between; gap: 15px; padding: 10px; border: 1px solid var(--line); border-radius: 6px; background: #101b23; }
.scenario-row strong { color: var(--cyan); }
.scenario-current { margin-left: 9px; padding: 2px 5px; border: 1px solid #397f7b; border-radius: 4px; color: #a5eee7; font-size: 10px; font-weight: 700; vertical-align: middle; }
.scenario-row p { margin-top: 3px; color: #aebfc7; font-size: 12px; }
.scenario-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 5px; min-width: 170px; }
.scenario-actions button { padding: 6px 9px; font-size: 12px; }
.debug-console { position: absolute; left: 28px; right: 28px; bottom: 24px; z-index: 5; border: 1px solid #7b9cac; border-radius: 5px; background: #080d12f2; box-shadow: 0 12px 35px #000b; font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.debug-console[hidden] { display: none; }
.debug-console-title { display: flex; justify-content: space-between; padding: 7px 10px; color: #bfe8ef; border-bottom: 1px solid #31444f; font-weight: 800; letter-spacing: .08em; }
.debug-console-title span { color: #708994; font-weight: 400; letter-spacing: 0; }
.debug-console-output { min-height: 31px; padding: 7px 10px; color: #83d99d; }
.debug-console form { display: flex; align-items: center; gap: 7px; padding: 7px 10px; border-top: 1px solid #263944; color: #52d5d0; }
.debug-console input { min-width: 0; flex: 1; padding: 3px 0; color: #edf7fa; border: 0; outline: 0; background: transparent; font: inherit; }

.sidebar { overflow-y: auto; border-left: 1px solid var(--line); background: var(--panel); }
.sidebar section { padding: 17px; border-bottom: 1px solid var(--line); }
.sidebar section[hidden] { display: none; }
.sidebar .tutorial-panel { background: #173039; border-bottom: 2px solid #398f8c; }
.tutorial-progress { color: var(--cyan); font-size: 11px; font-weight: 800; letter-spacing: .14em; }
.tutorial-panel h2 { margin: 8px 0 10px; color: #e8f7f5; font-size: 17px; letter-spacing: 0; text-transform: none; }
.tutorial-panel p { color: #c5dddc; font-size: 13px; line-height: 1.5; }
.tutorial-panel .tutorial-status { margin-top: 10px; color: #aee6e1; font-size: 11px; }
.tutorial-checklist { display: grid; gap: 6px; margin: 12px 0 0; padding: 0; list-style: none; color: #c5dddc; font-size: 12px; line-height: 1.4; }
.tutorial-checklist[hidden] { display: none; }
.tutorial-checklist .done { color: #93e8b5; }
.tutorial-panel .tutorial-hint { margin-top: 12px; padding: 9px; border: 1px solid #3d6972; border-radius: 5px; background: #10272e; color: #aee6e1; font-size: 12px; }
.tutorial-actions { display: grid; gap: 7px; margin-top: 14px; }
.tutorial-actions button { padding: 8px 10px; }
.tutorial-actions button[hidden] { display: none; }
#tutorialNextButton { border-color: #398f8c; background: #1c4b4d; font-weight: 700; }
#tutorialRestartButton { border-color: #46616c; font-size: 12px; }
#tutorialSkipButton { color: #a6b9c1; background: transparent; }
.objective-panel { background: #142820; }
.objective-panel p { color: #b6d4c4; font-size: 12px; line-height: 1.5; }
.objective-panel p + p { margin-top: 9px; }
.objective-count { color: #adf1c7; font: 800 16px ui-monospace, monospace; }
.objective-blocked { color: #ffaaa6 !important; }
.section-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 11px; }
h2 { color: #c7d5dc; font-size: 12px; letter-spacing: .15em; text-transform: uppercase; }
.badge { padding: 2px 7px; border-radius: 10px; color: var(--muted); background: #0d141a; font-size: 11px; }
.empty-state, .hint { color: var(--muted); font-size: 12px; }
.selection-card { display: grid; gap: 9px; }
.selection-title { display: flex; align-items: center; justify-content: space-between; font-size: 16px; font-weight: 700; }
.reliability-row { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 7px; min-height: 32px; font-size: 13px; font-weight: 700; }
.reliability-row button { padding: 5px 9px; font-size: 12px; }
.quality { color: var(--green); }
.quality.bad { color: var(--amber); }
.unknown { color: var(--muted); }
.identify-unavailable { color: var(--muted); font-size: 11px; font-weight: 400; }
.details { color: var(--muted); font-size: 12px; }
.route-outputs { display: grid; gap: 5px; margin: 3px 0; }
.route-output { padding: 5px 7px; border: 1px solid #3a4d5a; border-radius: 5px; background: #17232c; font-weight: 700; letter-spacing: .02em; }
.route-output.blue { color: #76baff; border-color: #375875; }
.route-output.red { color: #ff8f89; border-color: #714347; }
.route-output.match { color: #a6e4bb; border-color: #426d58; }
.route-output.other { color: #c7d5dc; }
.route-output.active { box-shadow: inset 3px 0 currentColor; background: #253746; }
.behavior-rules { padding: 10px; border: 1px solid #344b55; border-radius: 6px; background: #17232b; color: #c5d6dc; font-size: 12px; line-height: 1.45; }
.behavior-rules h3 { margin: 0 0 7px; color: #d9e9ed; font-size: 11px; letter-spacing: .1em; text-transform: uppercase; }
.behavior-rules p { margin: 0; }
.behavior-rules p + p { margin-top: 6px; color: #aebfc7; }
.behavior-rules strong { color: #dce7e9; }
.machine-state { display: grid; gap: 4px; margin-top: 9px; padding-top: 9px; border-top: 1px solid #344b55; color: #c7d5dc; }
.tick-counts { display: flex; gap: 7px; flex-wrap: wrap; }
.tick-count { display: inline-flex; align-items: center; gap: 7px; padding: 5px 8px; border: 1px solid currentColor; border-radius: 5px; font-size: 12px; }
.tick-count.green { color: #8df0a9; background: #19332a; }
.tick-count.orange { color: #f6a04d; background: #38291c; }
.tick-count strong { font-variant-numeric: tabular-nums; }
.actions { display: flex; flex-wrap: wrap; gap: 7px; }
.actions button { padding: 7px 9px; font-size: 12px; }
.actions .primary { border-color: #398f8c; background: #173a3a; }
.hint { margin-bottom: 10px; }
.component-panel h2 { margin-bottom: 11px; }
.key { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 6px; color: #c7d5dc; font-size: 12px; }
.key-entry { display: inline-flex; align-items: center; gap: 8px; padding: 7px 8px; border: 1px solid var(--line); border-radius: 5px; background: #17232b; white-space: nowrap; }
.dot { width: 10px; height: 10px; flex: none; border-radius: 50%; background: var(--muted); }


.dot.sorter { background: var(--sorter); }
.dot.counter { background: var(--counter); }
.dot.marker { background: var(--marker); }
.dot.router { background: var(--router); }

.dot.signal { background: var(--green); box-shadow: 0 0 8px var(--green); }

@media (max-width: 1050px) {
  main { grid-template-columns: minmax(650px, 1fr) 250px; }
  .tool { font-size: 12px; }
  .tool kbd { display: none; }
}

#gameCanvas.deleting {
  cursor: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPScyNCcgaGVpZ2h0PScyNCc+PHBhdGggZD0nTTYgNkwxOCAxOE0xOCA2TDYgMTgnIHN0cm9rZT0nI2UwNDgzZicgc3Ryb2tlLXdpZHRoPSczLjUnIHN0cm9rZS1saW5lY2FwPSdyb3VuZCcvPjwvc3ZnPg==") 12 12, crosshair;
}

#gameCanvas.rotating {
  cursor: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPScyNCcgaGVpZ2h0PScyNCc+PHBhdGggZD0nTTEyIDUgQTcgNyAwIDEgMSA1IDEyJyBmaWxsPSdub25lJyBzdHJva2U9JyM3ZmQ0YzknIHN0cm9rZS13aWR0aD0nMycgc3Ryb2tlLWxpbmVjYXA9J3JvdW5kJy8+PHBhdGggZD0nTTUgMTAgTDEuNSAxNiBMOC41IDE2IFonIGZpbGw9JyM3ZmQ0YzknLz48L3N2Zz4=") 12 12, crosshair;
}

#gameCanvas.building-belts, #gameCanvas.building-belts.painting {
  cursor: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIgdmlld0JveD0iMCAwIDMyIDMyIj48cmVjdCB4PSIzIiB5PSI3IiB3aWR0aD0iMjYiIGhlaWdodD0iMTgiIHJ4PSI1IiBmaWxsPSIjMTAyMDI3IiBzdHJva2U9IiNhZWU2ZTEiIHN0cm9rZS13aWR0aD0iMiIvPjxwYXRoIGQ9Ik05IDEwdjEyTTIzIDEwdjEyIiBzdHJva2U9IiM1YTc2ODQiIHN0cm9rZS13aWR0aD0iMiIvPjxwYXRoIGQ9Im0xMyAxMSA1IDUtNSA1bTYtMTAgNSA1LTUgNSIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjN2ZkNGM5IiBzdHJva2Utd2lkdGg9IjIuNSIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIi8+PC9zdmc+") 16 16, crosshair;
}

.main-menu-actions { display: grid; gap: 10px; width: min(300px, 100%); margin: 25px auto 5px; }
.main-menu-actions button { padding: 12px 18px; font-size: 16px; text-align: center; }
.main-menu-actions[hidden], .main-menu-actions button[hidden], #scenarioPicker[hidden] { display: none; }
#newGameButton, #homeBackButton { border-color: #398f8c; background: #173a3a; }
#homeOptionsButton { margin-top: 8px; padding: 8px; color: var(--muted); font-size: 13px; }
.picker-description { margin: 10px 0; color: #aebfc7; font-size: 13px; }
.picker-back { margin-top: 18px; padding: 8px 20px; }

.menu-status { margin-top: 14px; color: #aee6e1; text-align: center; font-size: 13px; }
