:root {
  --bg: #eef1f4;
  --surface: #ffffff;
  --surface-2: #f6f8fa;
  --line: #dde2e8;
  --text: #17202a;
  --muted: #5d6b7a;
  --accent: #ff7a1a;
  --accent-ink: #b34d00;
  --ok: #1f9d55;
  --bad: #d64545;
  --code-bg: #14181d;
  --code-line: #1c2128;
  --code-text: #dfe6ee;
  --radius: 10px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, 'Cascadia Code', Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  display: flex;
  flex-direction: column;
}
button, select, input { font: inherit; color: inherit; }

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 16px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: -0.01em; white-space: nowrap; }
.logo {
  width: 22px; height: 22px; border-radius: 6px;
  background: var(--accent);
  box-shadow: inset 0 0 0 5px #ffb27a, inset 0 0 0 7px var(--accent);
}
.chapters { display: flex; gap: 4px; overflow-x: auto; }
.chapter {
  display: flex; align-items: baseline; gap: 6px;
  border: 0; background: none; padding: 7px 12px; border-radius: 8px;
  color: var(--muted); cursor: pointer; white-space: nowrap;
}
.chapter b {
  display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--line); font-size: 12px; align-self: center;
}
.chapter small { font-size: 12px; color: #8a96a3; }
.chapter:hover { background: var(--surface-2); }
.chapter.active { color: var(--text); background: #fff1e6; }
.chapter.active b { background: var(--accent); border-color: var(--accent); color: #fff; }
.chapter[aria-disabled="true"] { opacity: 0.75; }

/* ---------- layout ---------- */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 470px;
}
.stage { position: relative; min-height: 0; }
.view { position: absolute; inset: 0; }
.view canvas { display: block; }

.panel {
  background: var(--surface);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  min-height: 0;
  overflow-y: auto;
}

/* ---------- HUD ---------- */
.hud {
  position: absolute;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(6px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: 0 4px 16px rgba(23, 32, 42, 0.08);
}
.hud-mission { top: 12px; left: 12px; min-width: 220px; }
.mission-name { font-weight: 600; }
.mission-progress { color: var(--muted); font-size: 13px; }
.mission-time { font: 500 20px var(--mono); margin-top: 2px; font-variant-numeric: tabular-nums; }

.hud-sensors { top: 12px; right: 12px; width: 230px; display: grid; gap: 4px; font-size: 12px; }
.sensor-row { display: grid; grid-template-columns: 52px 1fr 38px; align-items: center; gap: 8px; }
.sensor-row span { color: var(--muted); }
.sensor-row output { font-family: var(--mono); text-align: right; font-variant-numeric: tabular-nums; }
.sensor-row output.wide { grid-column: 2 / 4; text-align: left; }
.sensor-row meter { width: 100%; height: 8px; }
.sensor-row.motors { grid-template-columns: 52px 1fr 1fr; }
.motor { position: relative; height: 8px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 4px; overflow: hidden; }
.motor i { position: absolute; top: 0; bottom: 0; left: 50%; width: 0; background: var(--accent); }
.motor::after { content: ''; position: absolute; left: 50%; top: 0; bottom: 0; width: 1px; background: #b9c2cc; }

.hud-camera { bottom: 12px; left: 12px; display: flex; gap: 4px; align-items: center; padding: 6px; }
.hud-camera button {
  border: 0; background: none; padding: 5px 10px; border-radius: 6px; cursor: pointer; color: var(--muted);
}
.hud-camera button.on { background: var(--text); color: #fff; }
.hud-camera .beam { display: flex; gap: 6px; align-items: center; padding: 0 8px; color: var(--muted); font-size: 13px; }

.result {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: rgba(20, 24, 29, 0.28);
}
.result[hidden] { display: none; }
.result-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 24px 28px;
  text-align: center;
  max-width: 380px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}
.result-card h2 { margin: 6px 0 4px; font-size: 22px; }
.result-card p { margin: 0 0 18px; color: var(--muted); }
.stars { font-size: 30px; letter-spacing: 4px; color: #f5b301; min-height: 36px; }
.stars .off { color: #d9dee4; }
.result-actions { display: flex; gap: 8px; justify-content: center; }

/* ---------- panel ---------- */
.missions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.mission {
  text-align: left;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 8px 10px;
  cursor: pointer;
}
.mission:hover { border-color: #c2cad3; }
.mission[aria-selected="true"] { border-color: var(--accent); background: #fff6ef; }
.mission .n { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.mission .t { font-weight: 600; font-size: 13px; line-height: 1.25; }
.mission .s { font-size: 12px; color: var(--muted); }
.mission .done { color: var(--ok); font-size: 12px; }

.brief .goal { margin: 0 0 4px; }
.brief details { color: var(--muted); }
.brief summary { cursor: pointer; color: var(--accent-ink); font-weight: 500; }
.brief details p { margin: 6px 0 0; }

.toolbar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  font-weight: 500;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: #f06c0c; }
.speed { margin-left: auto; display: flex; gap: 6px; align-items: center; color: var(--muted); font-size: 13px; }
.speed select { border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; background: var(--surface); }
.link { border: 0; background: none; color: var(--accent-ink); cursor: pointer; padding: 0; font-size: 12px; }

/* ---------- editor ---------- */
.editor {
  display: flex;
  background: var(--code-bg);
  border-radius: var(--radius);
  min-height: 300px;
  flex: 1 0 300px;
  overflow: hidden;
  font: 12.5px/1.55 var(--mono);
}
.gutter {
  padding: 10px 8px 10px 0;
  min-width: 40px;
  text-align: right;
  color: #56606c;
  background: var(--code-line);
  user-select: none;
  overflow: hidden;
  white-space: pre;
}
.gutter .err { color: #fff; background: var(--bad); border-radius: 3px; padding: 0 3px; }
.code-wrap { position: relative; flex: 1; min-width: 0; }
.highlight, .code-wrap textarea {
  position: absolute; inset: 0;
  margin: 0;
  padding: 10px 12px;
  font: inherit;
  white-space: pre;
  tab-size: 2;
  overflow: auto;
  border: 0;
}
.highlight { color: var(--code-text); pointer-events: none; overflow: hidden; }
.code-wrap textarea {
  background: transparent;
  color: transparent;
  caret-color: #fff;
  resize: none;
  outline: none;
}
.code-wrap textarea::selection { background: rgba(120, 160, 255, 0.35); color: transparent; }
.tok-c { color: #6d7a88; font-style: italic; }
.tok-s { color: #a5d6a7; }
.tok-n { color: #f8b26a; }
.tok-t { color: #7cc4ff; }
.tok-k { color: #ff9bd0; }
.tok-f { color: #ffd479; }
.tok-p { color: #c3a6ff; }

.editor-foot { display: flex; justify-content: space-between; align-items: center; margin-top: -6px; }
.engine { font-size: 12px; color: var(--muted); }

.serial { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; flex-shrink: 0; }
.serial-head { display: flex; justify-content: space-between; padding: 6px 10px; background: var(--surface-2); font-size: 12px; font-weight: 600; color: var(--muted); }
.serial-out {
  margin: 0; height: 120px; overflow: auto; padding: 8px 10px;
  font: 12px/1.45 var(--mono); background: var(--surface); white-space: pre-wrap; word-break: break-all;
}
.serial-out .err { color: var(--bad); }
.serial-out .sys { color: var(--muted); }

.wiring summary { cursor: pointer; font-weight: 500; color: var(--accent-ink); }
.wiring table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 13px; }
.wiring th, .wiring td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.wiring td:first-child { font-family: var(--mono); white-space: nowrap; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 10px 16px; border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2); max-width: calc(100% - 32px);
}

@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 55vh auto; }
  .panel { border-left: 0; border-top: 1px solid var(--line); overflow: visible; }
  .chapter small { display: none; }
  .hud-sensors { display: none; }
  body { height: auto; }
  html, body { height: auto; }
  .layout { min-height: 0; }
}
@media (max-width: 520px) {
  .top { gap: 10px; }
  .brand { font-size: 0; gap: 0; }
  .missions { grid-template-columns: 1fr; }
  .hud-camera .beam { display: none; }
}

/* ---------- links in the header ---------- */
a.brand, a.chapter { text-decoration: none; color: inherit; }
a.chapter { color: var(--muted); }
a.chapter.active { color: var(--text); }

/* ---------- chips: unlocked parts ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 500;
  border: 1px solid var(--line); background: var(--surface-2); color: var(--muted);
}
.chip.ok { background: #e8f6ee; border-color: #b9e2c9; color: #17693b; }
.chip.locked { opacity: 0.8; }
.parts { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.parts-label { font-size: 12px; color: var(--muted); }
.parts-note { margin: 2px 0 0; width: 100%; font-size: 12.5px; color: var(--muted); }
.parts-note a { color: var(--accent-ink); }

/* ---------- laboratory ---------- */
.lessons { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.lesson {
  width: 100%; display: grid; grid-template-columns: 26px 1fr auto; align-items: center; gap: 10px;
  text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 8px;
  padding: 6px 10px; cursor: pointer;
}
.lesson:hover { border-color: #c2cad3; }
.lesson.on { border-color: var(--accent); background: #fff6ef; }
.lesson .num {
  display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--line); font-size: 12px; font-weight: 600;
}
.lesson.done .num { background: var(--ok); border-color: var(--ok); color: #fff; }
.lesson .name { font-weight: 600; }
.lesson .gives { font-size: 12px; color: var(--muted); }
.theory { margin: 0 0 6px; color: var(--muted); }

.wiring-check { border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 10px; }
.wc-head { display: flex; justify-content: space-between; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.wiring-check ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; font-size: 13px; }
.wiring-check li { display: flex; gap: 8px; align-items: baseline; }
.wiring-check .mark { width: 14px; text-align: center; color: #9aa5b1; font-weight: 700; }
.wiring-check li.ok .mark { color: var(--ok); }
.wiring-check li.bad { color: var(--bad); }
.wiring-check li.bad .mark { color: var(--bad); }

.hud-help { bottom: 12px; right: 12px; max-width: 300px; font-size: 12px; color: var(--muted); }
.hud-controls { bottom: 12px; left: 12px; display: grid; gap: 8px; max-width: 320px; font-size: 13px; }
.hud-controls .ctl[hidden] { display: none; }
.hud-controls label.ctl { display: grid; gap: 2px; }
.range-row { display: flex; gap: 8px; align-items: center; }
.range-row input { flex: 1; accent-color: var(--accent); }
.range-row small { color: var(--muted); }
.hud-controls output { font-family: var(--mono); font-size: 12px; color: var(--muted); }
kbd { font: 11px var(--mono); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; background: var(--surface); }
.btn.small { padding: 4px 10px; font-size: 12.5px; justify-self: start; }

.tip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--text); color: #fff; font-size: 12.5px; padding: 6px 9px; border-radius: 6px;
  max-width: 260px; box-shadow: 0 6px 18px rgba(0,0,0,.2);
}
.tip small { display: block; color: #c4ccd5; font-size: 11.5px; }
.tip[hidden] { display: none; }

.unlock-badge {
  display: inline-block; padding: 5px 12px; border-radius: 999px;
  background: #e8f6ee; color: #17693b; font-weight: 600; font-size: 13px;
}
.inventory { border-top: 1px solid var(--line); padding-top: 10px; }
.inv-head { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.inv-items { display: flex; flex-wrap: wrap; gap: 6px; }

@media (max-width: 980px) {
  .hud-help { display: none; }
}

/* ---------- hub ---------- */
.hub { max-width: 1040px; margin: 0 auto; padding: 32px 16px 48px; width: 100%; }
.hub h1 { font-size: 30px; letter-spacing: -0.02em; margin: 0 0 6px; }
.hub .lead { color: var(--muted); font-size: 16px; margin: 0 0 24px; max-width: 640px; }
.chapter-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.ccard {
  display: flex; flex-direction: column; gap: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 18px;
  text-decoration: none; color: inherit;
}
a.ccard:hover { border-color: var(--accent); }
.ccard .step { font-size: 12px; font-weight: 600; color: var(--accent-ink); text-transform: uppercase; letter-spacing: .05em; }
.ccard h2 { margin: 0; font-size: 20px; }
.ccard p { margin: 0; color: var(--muted); }
.ccard .meta { margin-top: auto; padding-top: 8px; font-size: 13px; font-weight: 500; }
.ccard.soon { opacity: .7; }
.hub-section { margin-top: 28px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 18px; }
.hub-section h3 { margin: 0 0 10px; font-size: 15px; }
.hub-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; color: var(--muted); font-size: 13px; margin-top: 12px; }
@media (max-width: 820px) { .chapter-cards { grid-template-columns: 1fr; } }

/* ---------- workshop ---------- */
.hud-tools { bottom: 12px; left: 12px; display: flex; gap: 4px; padding: 6px; }
.hud-tools button { border: 0; background: none; padding: 6px 10px; border-radius: 6px; cursor: pointer; color: var(--muted); font-weight: 500; }
.hud-tools button.on { background: var(--text); color: #fff; }
.steps { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.step {
  display: grid; grid-template-columns: 24px 1fr; grid-template-rows: auto auto; column-gap: 8px; align-items: center;
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; font-weight: 600; font-size: 13px;
}
.step .num {
  grid-row: span 2; display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--line); font-size: 12px;
}
.step small { color: var(--muted); font-weight: 500; font-size: 12px; }
.step.ok .num { background: var(--ok); border-color: var(--ok); color: #fff; }
.part-info { background: #fff6ef; border: 1px solid #ffd8bb; border-radius: var(--radius); padding: 10px 12px; min-height: 92px; }
.pi-name { font-weight: 600; }
.pi-desc { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.parts-list li { display: flex; justify-content: space-between; gap: 8px; padding: 2px 0; cursor: default; }
.pl-state { font-size: 12px; color: var(--muted); white-space: nowrap; }
.pl-state.mid { color: #b06a00; }
.pl-state.ok { color: var(--ok); font-weight: 600; }
.serial-out.log { height: 150px; }
.atelier-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.atelier-foot a.btn { text-decoration: none; color: inherit; }

a.chip { text-decoration: none; }
a.chip:hover { border-color: var(--accent); }
