:root {
  color-scheme: dark;
  --bg: #101113;
  --white: #f0f1f3;
  --muted: #92959c;
  --edge: #646970;
  --blue: #79caff;
  --yellow: #f1d26d;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--white); font-family: "Segoe UI", "Malgun Gothic", sans-serif; }
button { font: inherit; color: inherit; border: 0; background: transparent; cursor: pointer; }
button:focus-visible, summary:focus-visible { outline: 2px solid var(--blue); outline-offset: 5px; }
button:disabled { color: #555960; cursor: default; }
.game-shell { max-width: 1120px; margin: 0 auto; padding: 36px 48px 24px; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 24px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { font-size: 32px; line-height: 1; color: var(--muted); }
h1 { margin: 0; font-size: 18px; font-weight: 500; letter-spacing: .06em; }
.stages { display: flex; gap: 14px; flex-wrap: wrap; }
.stages button { padding: 10px 8px; min-width: 42px; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; border-bottom: 1px solid transparent; }
.stages button[aria-pressed="true"] { color: var(--white); border-bottom-color: var(--white); }
.stages button:hover { color: var(--white); }
.selection-toggle { padding: 10px 8px; color: #c1c4cb; font-size: 13px; }
.selection-toggle:hover { color: var(--white); }
.world-tabs { display: flex; gap: 20px; margin-top: 20px; }
.world-tabs button { min-height: 40px; padding: 8px 4px; font-size: 13px; color: var(--muted); border-bottom: 1px solid transparent; }
.world-tabs button[aria-pressed="true"] { color: var(--white); border-bottom-color: var(--white); }
.world-tabs button:disabled { color: #555960; }
.round-selection { margin-top: 42px; }
.campaign-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; }
.campaign-progress { color: var(--blue); font-size: 13px; white-space: nowrap; margin: 24px 0 0; }
.unlock-summary { margin: 26px 0 32px; }
.unlock-summary p { margin: 0 0 12px; color: var(--muted); font-size: 12px; }
.unlock-meter { height: 2px; max-width: 340px; background: #2b2f35; }
.unlock-meter span { display: block; height: 100%; width: 0; background: var(--blue); }
.difficulty-group { margin-top: 30px; }
.difficulty-group h3 { font-size: 13px; font-weight: 400; color: #c1c4cb; margin: 0 0 15px; letter-spacing: .06em; }
.round-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.round-card { display: flex; flex-direction: column; align-items: stretch; padding: 15px; border: 1px solid #2b2f35; text-align: left; }
.round-card:hover, .round-card[aria-pressed="true"] { border-color: #747c86; }
.round-card.cleared { border-color: #35556a; }
.round-card-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; font-size: 13px; font-variant-numeric: tabular-nums; }
.clear-badge { font-size: 10px; color: var(--muted); }
.round-card.cleared .clear-badge { color: var(--blue); }
.round-thumbnail { display: block; width: 100%; height: 85px; margin: 8px 0; pointer-events: none; }
.round-name { display: block; font-size: 12px; line-height: 1.7; min-height: 41px; }
.round-moves { display: block; font-size: 10px; color: var(--muted); margin-top: 8px; }
.world-next { margin-top: 36px; padding-top: 24px; border-top: 1px solid #2b2f35; }
.world-next h3 { font-size: 12px; font-weight: 400; letter-spacing: .1em; color: var(--muted); margin: 0 0 12px; }
.world-next p { color: var(--muted); font-size: 12px; line-height: 1.8; margin: 0; }
.world-next.unlocked h3, .world-next.unlocked p { color: var(--blue); }
.storage-note { color: var(--yellow); font-size: 12px; line-height: 1.8; margin: 22px 0; }
h2[tabindex="-1"]:focus { outline: none; }
.puzzle { position: relative; margin-top: 38px; }
.stage-heading { padding-right: 306px; min-height: 93px; }
.eyebrow { color: var(--muted); font-size: 10px; letter-spacing: .16em; margin: 0 0 13px; }
h2 { margin: 0; font-size: 21px; font-weight: 400; letter-spacing: -.025em; }
.stage-hint { margin: 12px 0 0; font-size: 13px; color: var(--muted); line-height: 1.7; }
.target-preview { position: absolute; top: 0; right: 0; width: 261px; text-align: center; }
.target-preview .eyebrow { margin-bottom: 0; }
#target-svg { display: block; width: 100%; height: auto; }
.board-wrap { display: flex; justify-content: center; padding-top: 8px; }
#game-svg { display: block; width: min(100%, 600px); height: auto; max-height: 55vh; overflow: visible; touch-action: none; user-select: none; -webkit-user-select: none; }
#game-svg.dragging, #game-svg.dragging .vertex { cursor: grabbing; }
.drag-preview { stroke: #a2dcff; stroke-width: 4; stroke-linecap: round; pointer-events: none; }
.drag-preview[hidden] { display: none; }
.edge { stroke: var(--edge); stroke-width: 4; stroke-linecap: round; opacity: 1; transition: opacity 180ms ease-out; }
.edge.once { stroke: var(--blue); }
.edge.deleted { opacity: 0; pointer-events: none; }
.direction-arrow { fill: none; stroke: #c6ebff; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; opacity: 1; transition: opacity 180ms ease-out; pointer-events: none; }
.direction-arrow.deleted { opacity: 0; }
.target-edge { stroke: #c4c7cd; stroke-width: 6; stroke-linecap: round; }
.target-node { fill: var(--white); }
.vertex { cursor: grab; outline: none; }
.vertex .hit-area { fill: transparent; }
.vertex .node { fill: var(--white); }
.vertex.current .node { fill: var(--yellow); }
.vertex .focus-ring { fill: none; stroke: var(--white); stroke-width: 1.5; opacity: 0; }
.vertex:focus-visible .focus-ring { opacity: 1; }
.vertex.available .focus-ring { opacity: .45; }
.vertex.terminal { cursor: default; }
.vertex .node-label { fill: var(--muted); font-size: 12px; text-anchor: middle; pointer-events: none; user-select: none; }
.game-footer { max-width: 620px; margin: 0 auto; }
.feedback { text-align: center; min-height: 70px; padding: 8px 0 16px; }
#status-title { font-size: 15px; font-weight: 400; margin: 0; line-height: 1.7; }
#status-detail { font-size: 12px; margin: 6px 0 0; color: var(--muted); line-height: 1.7; }
.feedback[data-status="won"] #status-title { color: var(--blue); }
.feedback[data-status="lost"] #status-title { color: var(--yellow); }
.control-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 13px 0; }
.move-count, .position { display: flex; gap: 8px; align-items: baseline; font-size: 11px; color: var(--muted); margin: 0; min-width: 60px; }
#move-count { font-size: 21px; color: var(--white); font-variant-numeric: tabular-nums; }
.position { justify-content: flex-end; }
#current-position { font-size: 14px; color: var(--white); }
.actions { display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; }
.actions button { padding: 8px; font-size: 12px; color: #c1c4cb; }
.actions button:disabled { color: #555960; }
.actions button:not(:disabled):hover { color: var(--white); }
.actions button span { font-size: 18px; margin-right: 3px; }
.legend { display: flex; justify-content: center; gap: 24px; margin-top: 20px; color: var(--muted); font-size: 11px; }
.legend span { display: flex; align-items: center; gap: 8px; }
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--yellow); }
.line { display: inline-block; width: 18px; height: 2px; background: var(--edge); }
.line.passed { background: var(--blue); }
.rules { margin-top: 30px; color: var(--muted); font-size: 12px; line-height: 1.9; }
.rules summary { cursor: pointer; width: fit-content; margin: 0 auto; }
.rules p { margin: 16px 0 0; }
noscript { display: block; text-align: center; padding: 20px; }
@media (min-width: 641px) and (max-width: 900px) {
  .stage-heading { min-height: 232px; }
}
@media (max-width: 640px) {
  .game-shell { padding: 24px 22px; }
  .round-selection { margin-top: 30px; }
  .campaign-heading { flex-wrap: wrap; gap: 12px; }
  .campaign-progress { margin: 0; }
  .round-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .round-card { padding: 12px; }
  .puzzle { margin-top: 30px; }
  .stage-heading { padding-right: 195px; min-height: 172px; }
  h2 { font-size: 17px; line-height: 1.65; }
  .target-preview { width: 171px; }
  .stage-hint { font-size: 12px; }
  #game-svg { max-height: none; }
  .legend { gap: 14px; font-size: 10px; flex-wrap: wrap; }
  .control-row { gap: 6px; }
  .actions { gap: 2px; }
  .actions button { font-size: 11px; padding: 8px 5px; }
  .move-count, .position { min-width: 40px; }
}
@media (prefers-reduced-motion: reduce) { .edge, .direction-arrow { transition: none; } }

/* The mobile document never scrolls. The round list and opened instructions
   have their own bounded scroll areas; gameplay gestures stay on the board. */
@media (max-width: 640px), (hover: none) and (pointer: coarse) {
  html { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body {
    position: fixed; inset: 0; width: 100%; height: 100vh; height: 100dvh;
    overflow: hidden; overscroll-behavior: none; touch-action: manipulation;
  }
  .game-shell {
    display: flex; flex-direction: column; gap: 4px;
    width: 100%; height: 100%; max-height: 100svh; min-height: 0;
    padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
      max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    overflow: hidden;
  }
  .topbar { flex: 0 0 auto; gap: 8px; flex-wrap: nowrap; min-height: 34px; touch-action: none; }
  .brand { gap: 8px; }
  .brand-mark { font-size: 26px; }
  h1 { font-size: 15px; white-space: nowrap; }
  .selection-toggle { min-height: 34px; padding: 6px; font-size: 12px; }
  .storage-note { flex: 0 0 auto; margin: 0; font-size: 10px; line-height: 1.4; }
  .round-selection {
    flex: 1 1 0; min-height: 0; margin-top: 12px; padding: 0 2px 16px;
    overflow-x: hidden; overflow-y: auto; overscroll-behavior: none;
    touch-action: pan-y; -webkit-overflow-scrolling: touch;
  }
  .campaign-heading { flex-wrap: wrap; gap: 12px; }
  .campaign-progress { margin: 0; }
  .round-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
  .round-card { padding: 12px; touch-action: pan-y; }
  #play-view:not([hidden]) { display: flex; flex-direction: column; flex: 1 1 0; min-height: 0; }
  .puzzle {
    display: grid; grid-template-columns: minmax(0, 1fr) clamp(86px, 24vw, 128px);
    grid-template-rows: auto minmax(0, 1fr); column-gap: 10px;
    flex: 1 1 0; min-height: 0; margin-top: 0; touch-action: none;
  }
  .stage-heading { min-height: 0; padding-right: 0; }
  .stage-heading .eyebrow { margin-bottom: 4px; font-size: 9px; letter-spacing: .08em; }
  .stage-heading h2 { font-size: 15px; line-height: 1.35; }
  .stage-heading .stage-hint { margin-top: 4px; font-size: 11px; line-height: 1.35; }
  .target-preview { position: static; width: 100%; align-self: start; }
  .target-preview .eyebrow { font-size: 9px; }
  .board-wrap { grid-column: 1 / -1; min-height: 0; min-width: 0; padding-top: 0; align-items: center; overflow: hidden; }
  #game-svg {
    width: 100%; height: 100%; max-width: 600px; max-height: none;
    overflow: hidden; touch-action: none; overscroll-behavior: none;
  }
  .game-footer { position: relative; flex: 0 0 auto; width: 100%; max-width: none; }
  .feedback { min-height: 0; padding: 2px 0; touch-action: none; }
  #status-title { font-size: 12px; line-height: 1.4; }
  #status-detail { margin-top: 2px; font-size: 10px; line-height: 1.4; }
  .control-row {
    display: grid; grid-template-columns: 30px minmax(0, 1fr) 30px;
    grid-template-areas: "moves actions position";
    gap: 3px; padding: 2px 0 0; touch-action: none;
  }
  .move-count { grid-area: moves; }
  .position { grid-area: position; }
  .move-count, .position { flex-direction: column; gap: 0; min-width: 0; font-size: 10px; }
  .move-count { align-items: flex-start; }
  .position { align-items: flex-end; }
  #move-count { font-size: 18px; }
  #current-position { font-size: 12px; }
  .actions { grid-area: actions; gap: 4px; flex-wrap: wrap; }
  .actions button { min-height: 40px; padding: 6px; font-size: 11px; touch-action: none; }
  .actions button span { font-size: 16px; }
  .legend { gap: 12px; margin-top: 2px; font-size: 9px; flex-wrap: wrap; touch-action: none; }
  .legend span { gap: 5px; }
  .rules { margin-top: 2px; font-size: 11px; line-height: 1.6; }
  .rules summary { padding: 4px 6px; touch-action: none; }
  .rules[open] {
    position: absolute; z-index: 2; left: 0; right: 0; bottom: 0;
    max-height: calc(100vh - 70px); max-height: calc(100svh - 70px);
    margin: 0; padding: 0 12px 12px; background: var(--bg);
    overflow-y: auto; overscroll-behavior: none; touch-action: pan-y;
  }
  .rules[open] summary { position: sticky; top: 0; background: var(--bg); touch-action: pan-y; }
  .rules p { margin-top: 10px; }
}

@media (max-width: 640px) and (max-height: 600px),
       (hover: none) and (pointer: coarse) and (max-height: 600px) {
  .game-shell { gap: 4px; }
  .puzzle { grid-template-columns: minmax(0, 1fr) 86px; }
  .stage-heading h2 { font-size: 14px; }
  .stage-heading .stage-hint { font-size: 10px; margin-top: 4px; }
  .rules summary { padding: 4px; }
}

@media (max-width: 360px) {
  .actions { gap: 2px; }
  .actions button { padding: 6px 4px; font-size: 10px; }
  .actions button span { display: none; }
  .legend { gap: 8px; }
}

/* Landscape phones put controls beside the puzzle instead of consuming
   the already limited board height. Desktop's normal layout is untouched. */
@media (min-width: 600px) and (max-width: 640px) and (max-height: 520px),
       (min-width: 600px) and (max-height: 520px) and (hover: none) and (pointer: coarse) {
  #play-view:not([hidden]) { display: grid; grid-template-columns: minmax(0, 1fr) 200px; gap: 12px; }
  .puzzle { height: 100%; }
  .game-footer { align-self: center; }
  .control-row { grid-template-columns: 1fr 1fr; grid-template-areas: "moves position" "actions actions"; gap: 0; }
  .move-count, .position { flex-direction: row; gap: 6px; align-items: baseline; }
  .actions { gap: 2px; }
  .actions button { padding: 6px 4px; font-size: 10px; }
  .actions button span { display: none; }
  .legend { gap: 8px; }
}
