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

:root {
  --bg:         #0d1117;
  --surface:    #161b22;
  --border:     #30363d;
  --green:      #39d353;
  --green-dark: #26a641;
  --green-glow: rgba(57, 211, 83, 0.3);
  --red:        #f85149;
  --blue:       #58a6ff;
  --text:       #e6edf3;
  --muted:      #8b949e;
  --gold:       #f0c030;
  --silver:     #c0c0c0;
  --bronze:     #cd7f32;
}

html, body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-x: hidden;
}

/* ── Screens ──────────────────────────────── */
.screen {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 480px;
  padding: 1rem;
}

.screen.active { display: flex; }

/* ── Logo ─────────────────────────────────── */
.logo { text-align: center; }

.logo h1 {
  font-size: clamp(2.4rem, 10vw, 4rem);
  font-weight: 900;
  letter-spacing: 0.1em;
  color: var(--green);
  text-shadow: 0 0 30px var(--green-glow), 0 0 60px var(--green-glow);
  line-height: 1;
}

.logo p {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 0.25rem;
  letter-spacing: 0.05em;
}

/* ── Leaderboard ──────────────────────────── */
.leaderboard {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.9rem 1.2rem;
}

.leaderboard h2 {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--muted);
  margin-bottom: 0.65rem;
}

.score-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.score-row:last-child { border-bottom: none; }

.rank {
  font-size: 0.95rem;
  width: 1.5rem;
  text-align: center;
  flex-shrink: 0;
}

.rank-1 { color: var(--gold); }
.rank-2 { color: var(--silver); }
.rank-3 { color: var(--bronze); }
.rank-4, .rank-5 { color: var(--muted); font-size: 0.8rem; }

.score-name {
  flex: 1;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.score-pts {
  font-weight: 700;
  color: var(--green);
  font-variant-numeric: tabular-nums;
}

.no-scores {
  color: var(--muted);
  text-align: center;
  padding: 0.4rem 0;
  font-size: 0.85rem;
}

/* ── Form ─────────────────────────────────── */
.form-group {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.form-group label {
  font-size: 0.78rem;
  color: var(--muted);
  letter-spacing: 0.05em;
}

.form-group input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  padding: 0.75rem 1rem;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  width: 100%;
  -webkit-appearance: none;
  min-height: 48px;
}

.form-group input:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px var(--green-glow);
}

.form-group input.error { border-color: var(--red); }

.error-msg {
  font-size: 0.76rem;
  color: var(--red);
  min-height: 1rem;
}

.username-hint {
  font-size: 0.76rem;
  min-height: 1rem;
  transition: color 0.2s;
}

.username-hint.new-user      { color: var(--green); }
.username-hint.existing-user { color: var(--blue); }

/* ── Buttons ──────────────────────────────── */
.btn {
  background: var(--green);
  border: none;
  border-radius: 8px;
  color: #0d1117;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  padding: 0.85rem 2rem;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
  text-transform: uppercase;
  min-height: 48px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.btn-full { width: 100%; }

.btn:hover {
  background: var(--green-dark);
  box-shadow: 0 0 20px var(--green-glow);
}

.btn:active { transform: scale(0.97); }

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}

.btn-secondary:hover {
  background: var(--surface);
  box-shadow: none;
  color: var(--text);
}

/* ── Game Screen ──────────────────────────── */
#game-screen {
  max-width: 100%;
  padding: 0.5rem 0.5rem 0;
  gap: 0.4rem;
  justify-content: flex-start;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.2rem 0.5rem;
  min-height: 48px;
}

.game-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 3.5rem;
}

.stat-label {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

.stat-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--green);
  font-variant-numeric: tabular-nums;
}

#player-name-display {
  flex: 1;
  font-size: 0.82rem;
  color: var(--muted);
  text-align: center;
  padding: 0 0.5rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

canvas {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  touch-action: none;
  max-width: 100%;
}

.controls-hint {
  color: var(--muted);
  font-size: 0.72rem;
  text-align: center;
  padding: 0.25rem 0 0.5rem;
}

/* ── D-pad ────────────────────────────────── */
.dpad {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0.4rem 0 0.6rem;
  user-select: none;
}

.dpad-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}

.dpad-row {
  display: flex;
  gap: 5px;
  align-items: center;
}

.dpad-btn {
  width: 60px;
  height: 60px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.08s, transform 0.05s;
  user-select: none;
  -webkit-user-select: none;
}

.dpad-btn:active,
.dpad-btn.pressed {
  background: var(--border);
  transform: scale(0.9);
}

.dpad-center-btn {
  width: 50px;
  height: 50px;
  font-size: 1.1rem;
  color: var(--muted);
  background: transparent;
}

/* ── Visibility helpers ───────────────────── */
.mobile-only  { display: none; }
.desktop-only { display: block; }

/* ── Game Over Screen ─────────────────────── */
.result-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.4rem;
  text-align: center;
  width: 100%;
}

.result-box h2 {
  font-size: clamp(1.5rem, 6vw, 2rem);
  color: var(--red);
  margin-bottom: 0.2rem;
}

.result-score {
  font-size: clamp(2.5rem, 10vw, 3.5rem);
  font-weight: 900;
  color: var(--green);
  line-height: 1;
  margin: 0.4rem 0;
}

.result-label {
  color: var(--muted);
  font-size: 0.82rem;
}

.result-personal {
  margin-top: 0.6rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.result-personal span {
  color: var(--gold);
  font-weight: 700;
}

.btn-row {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
  width: 100%;
}

.btn-row .btn {
  flex: 1;
  min-width: 130px;
}

/* ── Loading Overlay ──────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(13, 17, 23, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.overlay.hidden { display: none; }

.spinner {
  width: 2.5rem;
  height: 2.5rem;
  border: 3px solid var(--border);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Mobile-first: ≤ 600 px ───────────────── */
@media (max-width: 600px) {
  body {
    align-items: flex-start;
  }

  .screen {
    padding: 0.75rem;
    gap: 0.75rem;
    min-height: 100dvh;
  }

  #game-screen {
    padding: 0.35rem 0.35rem 0;
    gap: 0.35rem;
  }

  .mobile-only  { display: flex; }
  .desktop-only { display: none; }
}

/* ── Desktop: > 600 px ────────────────────── */
@media (min-width: 601px) {
  #game-screen {
    max-width: none;
    align-items: center;
  }

  .game-header { max-width: 520px; }
}
