:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-border: #272e38;
  --text: #e6e9ee;
  --muted: #8b95a3;
  --edge: #3a4351;
  --node: #4a5462;
  --boundary: #6b7686;
  --defect: #ffc53d;
  --errors: #ff5c5c;
  --fixed: #b33b3b;
  --player: #4da3ff;
  --mwpm: #3ddc97;
  --chain: #d66bff;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* Top bar */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 28px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--panel-border);
}

.topbar h1 {
  margin: 0 auto 0 0;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: 0.01em;
}

.field {
  display: flex;
  align-items: center;
  gap: 8px;
}

.field-label {
  color: var(--muted);
  font-size: 13px;
}

#seed {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

#defect-count {
  min-width: 2ch;
  color: var(--defect);
  font-size: 18px;
  font-variant-numeric: tabular-nums;
}

select,
button {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
}

select {
  padding: 5px 8px;
}

button {
  padding: 9px 20px;
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--boundary);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

button.primary {
  background: var(--player);
  border-color: var(--player);
  color: #06121f;
  font-weight: 600;
}

.topbar button {
  padding: 6px 12px;
}

:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

/* Levels screen */

.levels {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 24px 20px;
}

.levels-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.mode-switch {
  display: inline-flex;
  padding: 3px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
}

.mode-switch button {
  padding: 6px 18px;
  background: transparent;
  border: 0;
  border-radius: 6px;
  color: var(--muted);
  font-weight: 600;
}

.mode-switch button[aria-selected="true"] {
  background: var(--player);
  color: #06121f;
}

.campaign-blurb {
  margin: 4px 0 2px;
  color: var(--muted);
  font-size: 14px;
}

.levels-progress {
  margin: 0 0 4px;
  font-size: 16px;
  font-weight: 600;
}

/* The map's own width is set from its view box, so a short campaign is not
   blown up to fill the column. */
#level-map {
  display: block;
  width: 100%;
  height: auto;
  margin: 0 auto;
  max-height: calc(100vh - 210px);
}

.map-path {
  fill: none;
  stroke: var(--edge);
  stroke-width: 6;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.map-path.done {
  stroke: var(--player);
}

.tier-label {
  fill: var(--muted);
  font-size: 13px;
  letter-spacing: 0.12em;
  text-anchor: middle;
}

.level {
  cursor: pointer;
}

.level:focus {
  outline: none;
}

.level.locked {
  cursor: not-allowed;
}

.level-node {
  fill: #1b212a;
  stroke: var(--edge);
  stroke-width: 3;
  transition: stroke 90ms ease-out;
}

.level.cleared .level-node {
  fill: var(--player);
  stroke: var(--player);
}

.level.current .level-node {
  fill: var(--defect);
  stroke: var(--defect);
  transform-box: fill-box;
  transform-origin: center;
  animation: pulse 1.6s ease-in-out infinite;
}

.level:not(.locked):hover .level-node,
.level:not(.locked):focus-visible .level-node {
  stroke: var(--text);
}

.level-number {
  fill: var(--muted);
  font-size: 19px;
  font-weight: 650;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.level.cleared .level-number,
.level.current .level-number {
  fill: #06121f;
}

.level-badge {
  fill: var(--panel);
  stroke: var(--panel-border);
  stroke-width: 1.5;
  pointer-events: none;
}

.level-icon {
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

.level.cleared .level-icon {
  stroke: var(--text);
}

.level-star {
  fill: none;
  stroke: var(--boundary);
  stroke-width: 1.5;
  stroke-linejoin: round;
  pointer-events: none;
}

.level-star.earned {
  fill: var(--defect);
  stroke: var(--defect);
}

/* Layout */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 290px;
  gap: 24px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 20px 24px;
}

.play {
  min-width: 0;
}

#board {
  display: block;
  width: 100%;
  height: calc(100vh - 170px);
  min-height: 340px;
  touch-action: manipulation;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px 28px;
  margin-top: 14px;
}

.tool-buttons,
.round-buttons {
  display: flex;
  gap: 10px;
}

.tool-buttons button {
  padding: 9px 14px;
}

/* Side panel */

.banner {
  padding: 14px 16px;
  border-radius: 8px;
  font-size: 20px;
  font-weight: 650;
  text-align: center;
  animation: banner-in 220ms ease-out;
}

.banner.safe {
  background: color-mix(in srgb, var(--mwpm) 18%, var(--panel));
  color: var(--mwpm);
}

.banner.logical {
  background: color-mix(in srgb, var(--errors) 18%, var(--panel));
  color: var(--errors);
}

.banner.gave-up {
  background: var(--panel);
  color: var(--muted);
}

.banner-detail {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

/* Guided tour */

.tour-card {
  margin-bottom: 14px;
  padding: 14px 16px 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
}

.tour-count {
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 13px;
}

.tour-card h2 {
  margin: 0 0 8px;
  font-size: 18px;
}

.tour-card p {
  margin: 0 0 10px;
  font-size: 14px;
}

#tour-body p {
  color: #c4cbd5;
}

.tour-why {
  margin-bottom: 10px;
  padding-left: 12px;
  border-left: 3px solid var(--defect);
}

.tour-why p:last-child {
  margin-bottom: 0;
}

.tour-actions,
.overlay-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.tour-actions button,
.overlay-actions button {
  padding: 7px 16px;
}

.touring .stats,
.breaking .stats,
.breaking #hint,
.breaking #give-up,
.breaking #player-toggle,
.touring .tool-buttons,
.touring #give-up,
.touring #next {
  display: none;
}

.touring .edge {
  cursor: default;
  pointer-events: none;
}

.touring .edge.tour-target {
  cursor: pointer;
  pointer-events: auto;
}

.edge.tour-target:not(.corrected) .edge-line {
  stroke: var(--text);
  animation: target-pulse 1.1s ease-in-out infinite;
}

.verdict {
  margin: 10px 0 0;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  font-size: 14px;
}

.level-note {
  margin: 10px 0 0;
  font-weight: 600;
  text-align: center;
}

.level-stars {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.level-stars .stars {
  display: block;
  color: var(--defect);
  font-size: 24px;
  letter-spacing: 0.15em;
}

.layers {
  display: grid;
  gap: 6px;
  margin: 16px 0 0;
  padding: 10px 14px 12px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
}

.layers legend {
  padding: 0 6px;
  color: var(--muted);
  font-size: 13px;
}

.layer-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.layer-toggle.errors input { accent-color: var(--errors); }
.layer-toggle.player input { accent-color: var(--player); }
.layer-toggle.mwpm input { accent-color: var(--mwpm); }
.layer-toggle.chain input { accent-color: var(--chain); }

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 16px 0 0;
}

.stats > div {
  padding: 8px 12px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
}

.stats .stat-wide {
  grid-column: 1 / -1;
}

.stats dt {
  color: var(--muted);
  font-size: 13px;
}

.stats dd {
  margin: 0;
  font-size: 19px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Board */

.boundary-bar {
  stroke: var(--boundary);
  stroke-width: 5;
  stroke-linecap: round;
}

.boundary-label {
  fill: var(--muted);
  font-size: 13px;
  letter-spacing: 0.08em;
  text-anchor: middle;
}

.edge {
  cursor: pointer;
}

.edge:focus {
  outline: none;
}

.edge-hit {
  stroke: transparent;
  stroke-width: 30;
  pointer-events: stroke;
}

.edge-line {
  stroke: var(--edge);
  stroke-width: 4;
  stroke-linecap: round;
  transition: stroke 90ms ease-out, stroke-width 90ms ease-out;
}

.edge:hover .edge-line,
.edge:focus-visible .edge-line {
  stroke: var(--boundary);
  stroke-width: 8;
}

.edge.corrected .edge-line {
  stroke: var(--player);
  stroke-width: 7;
}

/* In Break mode the player's moves are errors, so they show in red. */
.breaking .edge.corrected .edge-line {
  stroke: var(--errors);
}

.edge.shielded .edge-line,
.edge.shielded:hover .edge-line {
  stroke: var(--boundary);
  stroke-width: 3;
  stroke-dasharray: 1 8;
}

.edge.shielded,
.edge.fixed {
  cursor: not-allowed;
}

.layer-fixed line {
  stroke: var(--fixed);
  stroke-width: 7;
}

.layer-preview.blocked line {
  stroke: var(--errors);
  stroke-dasharray: 4 6;
}

.edge.corrected:hover .edge-line,
.edge.corrected:focus-visible .edge-line {
  stroke-width: 10;
}

.revealed .edge {
  cursor: default;
  pointer-events: none;
}

.check {
  fill: var(--node);
  pointer-events: none;
  transition: fill 120ms ease-out;
}

.check.selected {
  stroke: var(--text);
  stroke-width: 4;
}

/* Hit areas for drawing paths: lit dots start one, and while one is selected
   every dot and both boundary bars can finish it. Dots always catch clicks, so
   clicking a dark dot never flips the line underneath by accident. */
.check-hit,
.boundary-hit {
  fill: transparent;
  stroke: transparent;
  pointer-events: none;
  cursor: pointer;
}

.check-hit {
  pointer-events: all;
  cursor: default;
}

.check-hit.lit,
.selecting .check-hit {
  cursor: pointer;
}

.boundary-hit {
  stroke-width: 26;
}

.selecting .boundary-hit {
  pointer-events: stroke;
}

.selecting .boundary-bar {
  stroke: var(--text);
}

.touring .check-hit,
.touring .boundary-hit,
.revealed .check-hit,
.revealed .boundary-hit {
  pointer-events: none;
}

.check.lit {
  fill: var(--defect);
  transform-box: fill-box;
  transform-origin: center;
  animation: pulse 1.3s ease-in-out infinite;
}

.pop {
  fill: var(--defect);
  pointer-events: none;
  transform-box: fill-box;
  transform-origin: center;
  animation: pop 320ms ease-out forwards;
}

.layer {
  pointer-events: none;
}

.layer line {
  stroke-linecap: round;
}

.layer-preview line {
  stroke: var(--text);
  stroke-width: 7;
  stroke-opacity: 0.4;
}

.layer-hint line {
  stroke: var(--text);
  stroke-width: 5;
  stroke-dasharray: 3 8;
  animation: hint-march 0.8s linear infinite;
}

.pair-ring {
  fill: none;
  stroke-width: 3;
}

.pair-label {
  font-size: 15px;
  font-weight: 700;
}

.layer-errors line {
  stroke: var(--errors);
  stroke-width: 5;
}

.layer-player line {
  stroke: var(--player);
  stroke-width: 5;
}

.layer-mwpm line {
  stroke: var(--mwpm);
  stroke-width: 5;
  stroke-dasharray: 9 8;
  stroke-linecap: butt;
}

.layer-chain line {
  stroke: var(--chain);
  stroke-width: 20;
  stroke-opacity: 0.32;
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    filter: drop-shadow(0 0 3px var(--defect));
  }
  50% {
    transform: scale(1.18);
    filter: drop-shadow(0 0 11px var(--defect));
  }
}

@keyframes pop {
  from {
    transform: scale(1);
    opacity: 0.9;
  }
  to {
    transform: scale(3.2);
    opacity: 0;
  }
}

@keyframes hint-march {
  to {
    stroke-dashoffset: -11;
  }
}

@keyframes target-pulse {
  0%,
  100% {
    stroke-width: 5;
    opacity: 0.65;
  }
  50% {
    stroke-width: 9;
    opacity: 1;
  }
}

@keyframes banner-in {
  from {
    transform: translateY(-6px);
    opacity: 0;
  }
}

/* How to play overlay */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgb(5 7 10 / 72%);
}

.overlay-card {
  max-width: 460px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 22px 26px 24px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
}

.overlay-card h2 {
  margin: 0 0 10px;
  font-size: 20px;
}

.overlay-card p {
  margin: 0 0 12px;
  color: #c4cbd5;
}

.key {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin: 0 0 16px;
  padding: 0;
  list-style: none;
  font-size: 13px;
}

.key li {
  display: flex;
  align-items: center;
  gap: 6px;
}

.key li::before {
  content: "";
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: var(--swatch);
}

.key .key-defect { --swatch: var(--defect); }
.key .key-player { --swatch: var(--player); }
.key .key-errors { --swatch: var(--errors); }
.key .key-mwpm { --swatch: var(--mwpm); }
.key .key-chain { --swatch: var(--chain); }
.key .key-fixed { --swatch: var(--fixed); }
.key .key-shielded { --swatch: var(--boundary); }

.break-card .key {
  margin: 8px 0 0;
}

.break-par {
  margin: 0;
  font-weight: 600;
}

@media (max-width: 860px) {
  .topbar {
    padding: 12px 16px;
  }

  .topbar h1 {
    flex-basis: 100%;
  }

  .layout {
    grid-template-columns: minmax(0, 1fr);
    padding: 12px 16px;
  }

  .levels {
    padding: 12px 16px;
  }

  .touring .side,
  .breaking .side {
    order: -1;
  }

  #level-map {
    max-height: none;
  }

  #board {
    height: auto;
    aspect-ratio: 1 / 1;
    min-height: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .check.lit,
  .level.current .level-node {
    animation: none;
    filter: drop-shadow(0 0 7px var(--defect));
  }

  .banner,
  .layer-hint line,
  .edge.tour-target:not(.corrected) .edge-line {
    animation: none;
  }

  .edge.tour-target:not(.corrected) .edge-line {
    stroke-width: 7;
  }
}
