:root {
  --bg: #06070a;
  --text: #f4f7fb;
  --muted: #a7b2c8;
  --grid-fill: rgba(18, 24, 37, 0.9);
  --grid-line: rgba(173, 188, 255, 0.17);
  --grid-edge: rgba(173, 188, 255, 0.34);
  /* cell must fit 8 cols inside (100vw - 32px body padding - 24px board padding) */
  --cell-size: min(calc((100vw - 56px) / 8), 82px);
  --board-cols: 8;
  --board-rows: 8;
  --dot-size: calc(var(--cell-size) * 0.58);
  --shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: "Trebuchet MS", "Avenir Next", "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top, rgba(91, 124, 255, 0.22), transparent 28%),
    radial-gradient(circle at 20% 80%, rgba(124, 247, 212, 0.12), transparent 24%),
    radial-gradient(circle at 80% 20%, rgba(246, 184, 108, 0.12), transparent 22%),
    linear-gradient(160deg, #05060a 0%, #090b12 45%, #05060a 100%);
}

body {
  display: flex;
  justify-content: center;
  padding: max(env(safe-area-inset-top, 0px), 18px) 16px max(env(safe-area-inset-bottom, 0px), 32px);
  overflow-x: hidden;
}

body::before,
body::after {
  content: "";
  position: fixed;
  pointer-events: none;
  filter: blur(55px);
  opacity: 0.45;
  z-index: 0;
}

body::before {
  width: 18rem;
  height: 18rem;
  top: 4%;
  left: 8%;
  background: rgba(91, 124, 255, 0.18);
}

body::after {
  width: 20rem;
  height: 20rem;
  right: 8%;
  bottom: 8%;
  background: rgba(124, 247, 212, 0.14);
}

.page {
  position: relative;
  z-index: 1;
  width: min(860px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  animation: rise 700ms ease-out both;
}

h1 {
  margin: 0;
  font-size: clamp(2.8rem, 7vw, 4.9rem);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  font-weight: 800;
  background: linear-gradient(135deg, #ffffff 0%, #dce6ff 40%, #7cf7d4 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 0 28px rgba(124, 247, 212, 0.12);
}

.note {
  margin: -4px 0 8px;
  color: var(--muted);
  font-size: 0.98rem;
  letter-spacing: 0.02em;
  text-align: center;
}

.board-shell {
  position: relative;
  /* exactly fits the board grid + its own padding */
  width: calc(var(--cell-size) * var(--board-cols) + 24px);
  max-width: 100%;
  padding: 12px;
  border-radius: 24px;
  background: linear-gradient(180deg, rgba(255,255,255,0.05), rgba(255,255,255,0.015));
  border: 1px solid rgba(152, 173, 255, 0.14);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  overflow: hidden;
  touch-action: none;
}

.board {
  position: relative;
  width: calc(var(--cell-size) * var(--board-cols));
  height: calc(var(--cell-size) * var(--board-rows));
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.grid-layer {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--board-cols), var(--cell-size));
  grid-template-rows: repeat(var(--board-rows), var(--cell-size));
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--grid-fill);
  border-right: 1px solid var(--grid-line);
  border-bottom: 1px solid var(--grid-line);
  transition: background 180ms ease;
}

.cell.edge-top {
  border-top: 1px solid var(--grid-line);
}

.cell.edge-left {
  border-left: 1px solid var(--grid-line);
}

.cell.playable:hover {
  background: rgba(23, 31, 49, 0.96);
}

.cell.blocked {
  background: transparent;
  border-color: transparent;
}

.board-svg,
.dots-layer,
.win-layer {
  position: absolute;
  inset: 0;
}

.board-svg {
  overflow: visible;
  pointer-events: auto;
  z-index: 2;
}

.dots-layer {
  pointer-events: none;
  z-index: 3;
}

.win-layer {
  z-index: 4;
  display: grid;
  place-items: center;
  border-radius: 22px;
  background: rgba(5, 8, 16, 0.78);
  backdrop-filter: blur(12px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease;
}

.win-layer.visible {
  opacity: 1;
  pointer-events: auto;
}

.win-card {
  position: relative;
  z-index: 1;
  padding: 28px 34px;
  border-radius: 24px;
  background: linear-gradient(180deg, rgba(255,255,255,0.11), rgba(255,255,255,0.04));
  border: 1px solid rgba(152, 173, 255, 0.2);
  text-align: center;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.38);
  animation: celebrateCard 420ms ease-out both;
}

.win-card h2 {
  margin: 0 0 10px;
  font-size: clamp(1.8rem, 4vw, 2.8rem);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #ffffff;
  text-shadow: 0 0 24px rgba(124, 247, 212, 0.25);
}

.win-card p {
  margin: 0;
  color: var(--muted);
  font-size: 1rem;
}

.confetti-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.path-group {
  filter: drop-shadow(0 0 12px rgba(255,255,255,0.1));
}

.path-group.active {
  filter: drop-shadow(0 0 18px rgba(255,255,255,0.18));
}

.path-stroke {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 14;
  opacity: 0.94;
  pointer-events: none;
}

.path-hit {
  fill: none;
  stroke: transparent;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 28;
  pointer-events: stroke;
  cursor: pointer;
}

.dot {
  position: absolute;
  width: var(--dot-size);
  height: var(--dot-size);
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(0.96rem, 2.6vw, 1.15rem);
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  color: #0b1020;
  border: 2px solid rgba(255, 255, 255, 0.7);
  padding: 0;
  text-align: center;
  line-height: 1;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.34), 0 0 0 1px rgba(255,255,255,0.08);
  transform: translate(-50%, -50%) scale(1);
  transition: transform 160ms ease, box-shadow 160ms ease, filter 160ms ease;
  cursor: grab;
  pointer-events: auto;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.dot:hover,
.dot.active,
.dot.connected {
  transform: translate(-50%, -50%) scale(1.08);
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.45), 0 0 22px rgba(255,255,255,0.16);
  filter: saturate(1.08);
}

.dot.connected {
  outline: 2px solid rgba(255, 255, 255, 0.35);
  outline-offset: 2px;
}

.dot.pass-through {
  outline: 3px solid rgba(255, 255, 255, 0.5);
  outline-offset: 4px;
  outline-style: dashed;
}

.dot:active {
  cursor: grabbing;
}

.legend {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.94rem;
  text-align: center;
}


.stats-row {
  display: flex;
  justify-content: center;
  width: 100%;
  margin-top: 2px;
}

.timer-pill {
  padding: 10px 16px;
  border-radius: 999px;
  border: 1px solid rgba(152, 173, 255, 0.16);
  background: linear-gradient(180deg, rgba(255,255,255,0.07), rgba(255,255,255,0.03));
  color: var(--muted);
  font-size: 0.95rem;
  letter-spacing: 0.04em;
}

.timer-pill span {
  color: var(--text);
  font-weight: 700;
  margin-left: 8px;
  font-variant-numeric: tabular-nums;
}

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

.result-controls {
  display: none;
}

.control-button {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid rgba(152, 173, 255, 0.18);
  background: linear-gradient(180deg, rgba(255,255,255,0.07), rgba(255,255,255,0.03));
  color: var(--text);
  border-radius: 999px;
  padding: 11px 16px;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  transition: transform 140ms ease, background 140ms ease, border-color 140ms ease;
  min-height: 44px;
  touch-action: manipulation;
}

.control-button:hover {
  transform: translateY(-1px);
  background: linear-gradient(180deg, rgba(255,255,255,0.1), rgba(255,255,255,0.04));
  border-color: rgba(124, 247, 212, 0.35);
}

.control-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.instructions-layer {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(4, 7, 14, 0.82);
  backdrop-filter: blur(14px);
  z-index: 8;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

.instructions-layer.visible {
  opacity: 1;
  pointer-events: auto;
}

.instructions-card {
  width: min(520px, 100%);
  padding: 30px 28px;
  border-radius: 28px;
  border: 1px solid rgba(152, 173, 255, 0.16);
  background: linear-gradient(180deg, rgba(255,255,255,0.08), rgba(255,255,255,0.03));
  box-shadow: 0 20px 56px rgba(0, 0, 0, 0.38);
  text-align: center;
}

.instructions-card h2 {
  margin: 0 0 14px;
  font-size: clamp(1.7rem, 4vw, 2.4rem);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.instructions-card p {
  margin: 0 0 10px;
  color: var(--muted);
  line-height: 1.55;
}

.win-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  margin-top: 16px;
}

.win-share.copied {
  border-color: rgba(124, 247, 212, 0.5);
  background: linear-gradient(180deg, rgba(124,247,212,0.18), rgba(124,247,212,0.08));
  color: #ffffff;
}

@keyframes celebrateCard {
  from {
    opacity: 0;
    transform: scale(0.92) translateY(16px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ── Mobile: ensure board fits with less padding ── */
@media (max-width: 520px) {
  :root {
    /* 8 cols inside (100vw - 20px body padding - 16px board padding) */
    --cell-size: calc((100vw - 36px) / 8);
  }

  body {
    padding: max(env(safe-area-inset-top, 0px), 12px) 10px max(env(safe-area-inset-bottom, 0px), 20px);
  }

  .page { gap: 10px; }

  .board-shell {
    width: calc(var(--cell-size) * var(--board-cols) + 16px);
    padding: 8px;
    border-radius: 18px;
  }

  .stats-row { width: 100%; }

  .timer-pill {
    width: 100%;
    text-align: center;
  }

  .controls {
    width: 100%;
    gap: 8px;
  }

  .control-button {
    flex: 1 1 130px;
    padding: 12px 10px;
    font-size: 0.88rem;
  }

  .note, .legend { font-size: 0.85rem; }
  .win-card p, .instructions-card p { font-size: 0.88rem; }

  .win-card {
    padding: 20px 18px;
  }

  .instructions-card {
    padding: 22px 18px;
  }

  .path-stroke { stroke-width: 11; }
  .path-hit    { stroke-width: 22; }
}

/* ── Very small phones (SE, Galaxy A series) ── */
@media (max-width: 360px) {
  :root {
    --cell-size: calc((100vw - 28px) / 11);
  }
  body {
    padding: max(env(safe-area-inset-top, 0px), 8px) 8px max(env(safe-area-inset-bottom, 0px), 12px);
  }
  .board-shell {
    width: calc(var(--cell-size) * var(--board-cols) + 12px);
    padding: 6px;
  }
}
