:root {
  --bg: #0a0d12;
  --bg-elevated: #11151c;
  --bg-card: #141922;
  --border: #1f2630;
  --border-strong: #2a3340;

  --fg: #e6ecf3;
  --fg-muted: #8b96a5;
  --fg-dim: #566271;

  --accent: #00e5a0;
  --accent-dim: #00b37d;
  --accent-glow: rgba(0, 229, 160, 0.18);

  --red: #ff5a5f;
  --red-glow: rgba(255, 90, 95, 0.15);
  --amber: #ffb84d;
  --blue: #5ca2ff;

  --font-ui: 'Geist', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, Menlo, monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%;
  min-height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body {
  display: grid;
  grid-template-rows: auto auto 1fr;
  height: 100vh;
  height: 100dvh;
  position: relative;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 60% 40% at 50% 0%, var(--accent-glow), transparent 70%),
    linear-gradient(to bottom, rgba(17, 21, 28, 0.5), transparent 40%);
  pointer-events: none;
  z-index: 0;
}

body::after {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(to right, var(--border) 1px, transparent 1px),
    linear-gradient(to bottom, var(--border) 1px, transparent 1px);
  background-size: 80px 80px;
  opacity: 0.15;
  pointer-events: none;
  z-index: 0;
}

/* ------------------------------------------------------------
   Topbar
   ------------------------------------------------------------ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: rgba(10, 13, 18, 0.6);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  position: relative;
  z-index: 10;
}

.back {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  padding: 0.5rem 0.85rem;
  color: var(--fg-muted);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  transition: all 0.2s ease;
  white-space: nowrap;
}
.back:hover { color: var(--accent); border-color: var(--accent); }

.title {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  color: var(--fg);
  text-align: center;
}
.title .sub { color: var(--fg-dim); font-weight: 400; }

/* ------------------------------------------------------------
   HUD
   ------------------------------------------------------------ */
.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: var(--border);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 5;
}
.stat {
  background: var(--bg-elevated);
  padding: 0.8rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.stat-label {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.14em;
  color: var(--fg-dim);
}
.stat-value {
  font-family: var(--font-mono);
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--fg);
  line-height: 1;
  letter-spacing: -0.02em;
}

/* ------------------------------------------------------------
   Stage / board
   ------------------------------------------------------------ */
.stage {
  --board-size: min(92vw, calc(100vh - 260px), 480px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.9rem;
  padding: 1rem;
  min-height: 0;
  position: relative;
  z-index: 2;
}
@supports (height: 1dvh) {
  .stage { --board-size: min(92vw, calc(100dvh - 260px), 480px); }
}

.board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  overflow: hidden;
  touch-action: none;
  cursor: pointer;
}
.board:focus-visible {
  outline: 2px solid var(--accent-dim);
  outline-offset: 2px;
}

.cells {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(6, 1fr);
  /* no z-index here: badges (z-index 3) must rise above the thread SVG (z-index 2) */
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 0 0 0.5px var(--border);
  transition: background 0.15s ease;
}
.cell.visited { background: rgba(0, 229, 160, 0.07); }
.cell.blocked { background: var(--red-glow); }

/* Thread (SVG overlay) */
.thread {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
}
.thread-glow {
  fill: none;
  stroke: var(--accent-glow);
  stroke-width: 52;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.thread-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 24;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.9;
}
.thread-head {
  fill: var(--accent);
  transform-box: fill-box;
  transform-origin: center;
  animation: headPulse 1.5s ease-in-out infinite;
}
@keyframes headPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.3); }
}

/* Waypoint medallions */
.badge {
  position: relative;
  z-index: 3;
  width: 58%;
  aspect-ratio: 1;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: calc(var(--board-size) / 6 * 0.34);
  background: var(--bg-card);
  border: 2px solid var(--amber);
  color: var(--amber);
  transition: border-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}
.cell.collected .badge {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--bg-elevated);
  box-shadow: 0 0 16px var(--accent-glow);
}
.cell.next .badge {
  animation: nextPulse 1.6s ease-in-out infinite;
}
@keyframes nextPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 184, 77, 0); }
  50% { box-shadow: 0 0 0 6px rgba(255, 184, 77, 0.14); }
}
.cell.blocked .badge {
  border-color: var(--red);
  color: var(--red);
  animation: shake 0.3s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

/* ------------------------------------------------------------
   Controls
   ------------------------------------------------------------ */
.controls {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  justify-content: center;
}
.ctl {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.1em;
  padding: 0.55rem 1rem;
  color: var(--fg-muted);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
}
.ctl:hover:not(:disabled) { color: var(--accent); border-color: var(--accent); }
.ctl:disabled { opacity: 0.4; cursor: not-allowed; }

/* ------------------------------------------------------------
   Overlays (start / end)
   ------------------------------------------------------------ */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: rgba(10, 13, 18, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 0.3s ease-out;
  overflow-y: auto;
}
.overlay[hidden] { display: none; }
@keyframes fadeIn { from { opacity: 0; } }

.overlay-inner {
  max-width: 560px;
  width: 100%;
  text-align: center;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 2.5rem 2rem;
  margin: auto;
}

.overlay .eyebrow {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  color: var(--accent);
  margin-bottom: 1rem;
}
.overlay h1 {
  font-family: var(--font-mono);
  font-size: clamp(1.5rem, 4vw, 2.2rem);
  font-weight: 600;
  color: var(--fg);
  line-height: 1.1;
  margin-bottom: 1rem;
  letter-spacing: -0.02em;
}
.overlay h1 .accent { color: var(--accent); }
.overlay .lede {
  color: var(--fg-muted);
  font-size: 0.95rem;
  line-height: 1.55;
  margin-bottom: 1.5rem;
}
.overlay .lede strong { color: var(--fg); font-weight: 600; }

.keys {
  display: flex;
  justify-content: center;
  gap: 1.1rem;
  margin-bottom: 1.75rem;
  flex-wrap: wrap;
}
.key-group {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--fg-muted);
  letter-spacing: 0.08em;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.kbd {
  display: inline-block;
  min-width: 1.8em;
  padding: 2px 6px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 3px;
  color: var(--accent);
  font-weight: 600;
  text-align: center;
}

.primary {
  background: var(--accent);
  color: var(--bg);
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.9rem;
  letter-spacing: 0.1em;
  padding: 0.9rem 2.2rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
}
.primary:hover {
  background: var(--fg);
  transform: translateY(-1px);
}

.overlay-cta {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 1.25rem;
}
.ghost {
  background: transparent;
  color: var(--fg-muted);
  font-family: var(--font-mono);
  font-weight: 500;
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  padding: 0.9rem 1.4rem;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.2s ease;
  display: inline-block;
}
.ghost:hover {
  color: var(--fg);
  border-color: var(--fg-muted);
}

.pb-tag {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 3px;
  padding: 0.2rem 0.6rem;
  margin-bottom: 1rem;
}
.pb-tag[hidden] { display: none; }

.note {
  color: var(--fg-muted);
  font-size: 0.85rem;
  margin-bottom: 0.5rem;
  line-height: 1.4;
}

/* ------------------------------------------------------------
   Responsive
   ------------------------------------------------------------ */
@media (max-width: 640px) {
  .topbar { padding: 0.75rem 1rem; }
  .title { font-size: 0.75rem; letter-spacing: 0.12em; }
  .title .sub { display: none; }

  .stat { padding: 0.55rem 0.75rem; }
  .stat-value { font-size: 1.05rem; }
  .stat-label { font-size: 0.56rem; }

  .stage { --board-size: min(92vw, calc(100vh - 235px), 480px); padding: 0.75rem; gap: 0.7rem; }
  .ctl { padding: 0.5rem 0.8rem; font-size: 0.68rem; }

  .overlay-inner { padding: 1.75rem 1.25rem; }
  .keys { gap: 0.7rem; }
}
@supports (height: 1dvh) {
  @media (max-width: 640px) {
    .stage { --board-size: min(92vw, calc(100dvh - 235px), 480px); }
  }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
