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

:root {
  --bg: #0a0a0a;
  --surface: #141414;
  --border: #2a2a2a;
  --text: #e8e8e8;
  /* WCAG AA: 4.5:1 contrast required on normal text. #9a9a9a gives
     ~6.8:1 on #141414 and ~7.3:1 on #0a0a0a. */
  --text-dim: #9a9a9a;
  --green: #4ade80;
  --green-dim: rgba(74, 222, 128, 0.15);
  --red: #f87171;
  --food: #fbbf24;
  --focus-ring: #4ade80;
}

/* Universal :focus-visible outline — visible only on keyboard focus,
   never on mouse click. Required for WCAG 2.4.7 (Focus Visible). */
:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Screen-reader-only utility for aria-live regions. Visually hidden
   but still readable by assistive tech. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
}

/* ── HEADER ── */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  z-index: 10;
}

.back-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 500;
  transition: color 0.2s;
}

.back-link:hover {
  color: var(--text);
}

.score-display {
  display: flex;
  gap: 1.5rem;
  font-size: 0.85rem;
  font-weight: 600;
}

.score-display span {
  color: var(--text-dim);
}

.score-display strong {
  color: var(--green);
  display: inline-block;
  transform-origin: center;
}

.high-score strong {
  color: var(--food);
}

/* Score pop animation triggered from JS by toggling .score-pop */
@keyframes score-pop {
  0%   { transform: scale(1); }
  30%  { transform: scale(1.35); }
  100% { transform: scale(1); }
}
.score-display strong.score-pop {
  animation: score-pop 180ms ease-out;
}
@media (prefers-reduced-motion: reduce) {
  .score-display strong.score-pop { animation: none; }
}

/* ── GAME CONTAINER ── */
.game-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}

canvas {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  display: block;
}

/* ── OVERLAY ── */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 10, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: 6px;
  z-index: 5;
  transition: opacity 0.3s;
}

.overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.overlay h1 {
  font-size: clamp(1.5rem, 4vw, 2rem);
  font-weight: 800;
  letter-spacing: -0.5px;
  margin-bottom: 0.5rem;
}

.overlay h1 span {
  color: var(--green);
}

.overlay .subtitle {
  color: var(--text-dim);
  font-size: 0.95rem;
  margin-bottom: 2rem;
}

.overlay .gameover-flavor {
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 0.7rem;
  min-height: 1.2em;
  text-align: center;
}

.overlay .final-score {
  font-size: 1.1rem;
  margin-bottom: 0.3rem;
  color: var(--text-dim);
}

.overlay .final-score strong {
  color: var(--red);
  font-size: 1.3rem;
}

.overlay .gameover-rank {
  color: var(--green);
  font-size: 0.85rem;
  margin-bottom: 1.2rem;
  min-height: 1.2em;
  font-weight: 600;
}

.overlay .gameover-actions {
  display: flex;
  gap: 0.6rem;
  align-items: center;
  justify-content: center;
}

.share-btn {
  background: transparent;
  color: var(--green);
  border: 1px solid var(--green);
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
  font-family: inherit;
}

.share-btn:hover {
  background: var(--green-dim);
}

.share-btn.shared {
  background: var(--green);
  color: #000;
}

.clip-btn {
  background: transparent;
  color: var(--green);
  border: 1px solid var(--green);
  padding: 0.75rem 1.25rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
  font-family: inherit;
}
.clip-btn:hover { background: var(--green-dim); }
.clip-btn.hidden { display: none; }
.clip-btn:disabled { opacity: 0.6; cursor: default; }

/* ── TUTORIAL OVERLAY ── */
.tutorial-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 10, 0.92);
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.tutorial-overlay .tut-card {
  background: var(--surface, #141414);
  border: 1px solid var(--green, #4ade80);
  border-radius: 12px;
  padding: 2rem 1.75rem;
  max-width: 420px;
  width: 100%;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}
.tutorial-overlay .tut-step {
  color: var(--text-dim, #9a9a9a);
  font-size: 0.8rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin: 0 0 0.5rem;
}
.tutorial-overlay .tut-title {
  margin: 0 0 0.75rem;
  font-size: 1.5rem;
  color: var(--green, #4ade80);
}
.tutorial-overlay .tut-body {
  margin: 0 0 1.5rem;
  line-height: 1.5;
  color: #e5e5e5;
}
.tutorial-overlay .tut-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
}
.tutorial-overlay .tut-skip {
  background: transparent;
  color: var(--text-dim, #9a9a9a);
  border: 1px solid #2a2a2a;
  padding: 0.7rem 1.25rem;
  border-radius: 8px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
}
.tutorial-overlay .tut-skip:hover { background: #1a1a1a; color: #e5e5e5; }

/* ── RANK UP OVERLAY ── */
.rankup-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  z-index: 40;
  transition: opacity 250ms ease-out;
}

.rankup-overlay.visible {
  opacity: 1;
}

.rankup-card {
  background: rgba(10, 10, 10, 0.92);
  border: 1px solid var(--green);
  border-radius: 14px;
  padding: 2rem 3rem;
  text-align: center;
  box-shadow: 0 0 60px rgba(74, 222, 128, 0.35);
  transform: scale(0.85);
  transition: transform 250ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

.rankup-overlay.visible .rankup-card {
  transform: scale(1);
}

.rankup-label {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--text-dim);
  margin-bottom: 0.6rem;
}

.rankup-tier {
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: -0.5px;
}

@media (prefers-reduced-motion: reduce) {
  .rankup-overlay,
  .rankup-card {
    transition: none;
  }
}

/* ── CHALLENGE BANNER ── */
.challenge-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--green-dim);
  color: var(--text);
  border-bottom: 1px solid var(--green);
  padding: 0.7rem 1.2rem;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  z-index: 15;
  transform: translateY(0);
  transition: transform 250ms ease-out;
}

.challenge-banner strong {
  color: var(--green);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.challenge-banner.dismissed {
  transform: translateY(-100%);
}

.challenge-banner-x {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.4rem;
}

.challenge-banner-x:hover {
  color: var(--text);
}

.start-btn {
  background: var(--green);
  color: #000;
  border: none;
  padding: 0.8rem 2.5rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: filter 0.2s, transform 0.2s;
}

.start-btn:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
}

.controls-hint {
  color: var(--text-dim);
  font-size: 0.8rem;
  margin-top: 1.25rem;
}

.controls-hint kbd {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.75rem;
}

/* ── MOBILE CONTROLS ── */
.touch-controls {
  display: none;
  gap: 0.5rem;
}

.touch-row {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
}

.touch-btn {
  width: 56px;
  height: 56px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-dim);
  font-size: 1.3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.touch-btn:active {
  background: var(--green-dim);
  border-color: var(--green);
}

.touch-spacer {
  width: 56px;
  height: 56px;
}

/* ── SETTINGS ── */
.settings-wrap {
  position: relative;
}

.settings-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 1.1rem;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
}

.settings-btn:hover {
  color: var(--text);
  border-color: var(--text-dim);
}

.settings-panel {
  position: absolute;
  top: 40px;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  min-width: 180px;
  z-index: 20;
  display: none;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.settings-panel.open {
  display: block;
}

.settings-panel label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  color: var(--text-dim);
  padding: 0.4rem 0;
  cursor: pointer;
}

.settings-panel label + label {
  border-top: 1px solid var(--border);
}

.toggle {
  position: relative;
  width: 36px;
  height: 20px;
  flex-shrink: 0;
  margin-left: 0.75rem;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 10px;
  transition: background 0.2s;
  cursor: pointer;
}

.toggle-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: var(--text-dim);
  border-radius: 50%;
  transition: transform 0.2s, background 0.2s;
}

.toggle input:checked + .toggle-track {
  background: var(--green-dim);
}

.toggle input:checked + .toggle-track::after {
  transform: translateX(16px);
  background: var(--green);
}

/* ── LEADERBOARD ── */
.layout {
  display: flex;
  align-items: flex-start;
  gap: 2rem;
}

.leaderboard {
  width: 240px;
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.1rem;
  max-height: calc(20 * 24px + 2px);
  display: flex;
  flex-direction: column;
}

.leaderboard h2 {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  margin-bottom: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.leaderboard h2::before {
  content: '🏆';
  font-size: 0.9rem;
}

.leaderboard ol {
  list-style: none;
  counter-reset: lb;
  flex: 1;
  overflow-y: auto;
  margin: 0 -0.3rem;
  padding: 0 0.3rem;
}

.leaderboard li {
  counter-increment: lb;
  display: flex;
  align-items: center;
  font-size: 0.8rem;
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--border);
}

.leaderboard li:last-child { border-bottom: none; }

.leaderboard li::before {
  content: counter(lb);
  width: 1.3rem;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 0.72rem;
}

.leaderboard li:nth-child(1)::before { color: #fbbf24; }
.leaderboard li:nth-child(2)::before { color: #d1d5db; }
.leaderboard li:nth-child(3)::before { color: #b45309; }

.leaderboard .lb-name {
  flex: 1;
  color: var(--text);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

/* Highlight the current user's row across the full list width.
   Background fills the full li box — no padding-left hack. The ol's
   negative margin + positive padding gives lis a content box that
   spans the aside's inner width, so the highlight hits both edges. */
.leaderboard li.lb-me {
  background: var(--green-dim);
  border-radius: 4px;
}

.leaderboard li.lb-clickable {
  cursor: pointer;
  transition: background 0.15s;
}
.leaderboard li.lb-clickable:hover {
  background: rgba(255, 255, 255, 0.04);
}
.leaderboard li.lb-clickable.lb-me:hover {
  background: rgba(74, 222, 128, 0.25);
}

.leaderboard .lb-empty, .leaderboard .lb-loading, .leaderboard .lb-error {
  color: var(--text-dim);
  font-size: 0.78rem;
  text-align: center;
  padding: 1rem 0;
}

.leaderboard .lb-footer {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  font-size: 0.72rem;
  color: var(--text-dim);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.leaderboard .lb-footer button {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 0.72rem;
  cursor: pointer;
  font-family: inherit;
  padding: 0;
  text-decoration: underline;
}

.leaderboard .lb-footer button:hover { color: var(--text); }

/* ── LEADERBOARD TABS ── */
.lb-tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.15rem;
  background: #0a0a0a;
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 0.6rem;
}

.lb-tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.35rem 0;
  border-radius: 4px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: background 0.15s, color 0.15s;
}

.lb-tab:hover { color: var(--text); }
.lb-tab.active { background: var(--green-dim); color: var(--green); }

.lb-countdown {
  font-size: 0.68rem;
  color: var(--text-dim);
  text-align: center;
  font-variant-numeric: tabular-nums;
  min-height: 0.9em;
  margin-bottom: 0.25rem;
}

.lb-modifier {
  font-size: 0.7rem;
  color: var(--green);
  text-align: center;
  line-height: 1.3;
  margin-bottom: 0.35rem;
  min-height: 0.9em;
  font-weight: 500;
}

.leaderboard .lb-flag {
  margin-right: 0.35rem;
  font-size: 0.9rem;
  width: 1.4rem;
  flex-shrink: 0;
  text-align: center;
}

.leaderboard .lb-boss {
  margin-right: 0.25rem;
  font-size: 0.9rem;
  flex-shrink: 0;
  filter: drop-shadow(0 0 3px rgba(251, 191, 36, 0.6));
}

/* ── MODE SWITCH (start overlay) ── */
.mode-switch {
  display: flex;
  gap: 0.3rem;
  background: #0a0a0a;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.25rem;
  margin-bottom: 1.5rem;
}

.mode-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 0.5rem 1.1rem;
  border-radius: 6px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: background 0.15s, color 0.15s;
}

.mode-btn:hover { color: var(--text); }
.mode-btn.active { background: var(--green-dim); color: var(--green); }

/* ── NAME PROMPT ── */
.overlay .name-input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  padding: 0.7rem 1rem;
  width: min(260px, 70vw);
  text-align: center;
  margin-bottom: 0.5rem;
  outline: none;
  transition: border-color 0.2s;
}

.overlay .name-input:focus { border-color: var(--green); }

.overlay .name-hint {
  color: var(--text-dim);
  font-size: 0.75rem;
  margin-bottom: 1rem;
  min-height: 1.1em;
}

.overlay .name-hint.error { color: var(--red); }

/* ── MOBILE LEADERBOARD BUTTON ── */
.lb-toggle {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 1rem;
  width: 32px;
  height: 32px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
  margin-right: 0.5rem;
}

.lb-toggle:hover { color: var(--text); border-color: var(--text-dim); }

@media (max-width: 899px) {
  .leaderboard { display: none; }
  .lb-toggle { display: flex; }
  .layout { display: block; }
}

.leaderboard.mobile-open {
  display: flex;
  position: fixed;
  inset: 10% 5%;
  z-index: 30;
  max-height: 80vh;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}

.lb-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 10, 0.8);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 25;
}

.lb-backdrop.open { display: block; }

@media (pointer: coarse) {
  .touch-controls {
    display: flex;
    flex-direction: column;
  }
  .controls-hint {
    display: none;
  }
}

@media (max-height: 600px) {
  .header {
    padding: 0.5rem 1rem;
  }
  .touch-btn {
    width: 48px;
    height: 48px;
    font-size: 1.1rem;
  }
  .touch-spacer {
    width: 48px;
    height: 48px;
  }
  .game-wrap {
    gap: 0.75rem;
  }
}

/* ── REPLAY VIEWER OVERLAY ── */
.replay-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
}

.replay-overlay.hidden {
  display: none;
}

.replay-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 5, 5, 0.9);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  /* z-index: 1 so the backdrop always sits below the panel, no
     matter the DOM order. */
  z-index: 1;
}

.replay-panel {
  position: relative;
  /* z-index: 2 keeps the panel visibly on top of the backdrop, so
     the canvas never gets blurred. */
  z-index: 2;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.1rem 1.2rem 1.2rem;
  max-width: calc(100vw - 2rem);
  max-height: calc(100vh - 2rem);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}

.replay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.8rem;
}

.replay-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
}

.replay-close {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.4rem;
}

.replay-close:hover {
  color: var(--text);
}

.replay-canvas {
  display: block;
  max-width: calc(100vw - 3rem);
  max-height: calc(100vh - 10rem);
  width: 480px;
  height: 480px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.replay-progress {
  height: 3px;
  background: var(--green);
  width: 0%;
  margin-top: 0.6rem;
  border-radius: 2px;
  transition: width 100ms linear;
}

.replay-status {
  color: var(--text-dim);
  font-size: 0.75rem;
  margin-top: 0.5rem;
  text-align: center;
}

/* ── TOASTS ── */
.toast-container {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: min(360px, calc(100vw - 2rem));
  pointer-events: none;
}

.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left-width: 3px;
  border-radius: 6px;
  color: var(--text);
  padding: 0.65rem 0.9rem;
  font-size: 0.82rem;
  line-height: 1.35;
  cursor: pointer;
  pointer-events: auto;
  transform: translateX(110%);
  opacity: 0;
  transition: transform 220ms cubic-bezier(0.22, 0.61, 0.36, 1), opacity 220ms ease-out;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  word-wrap: break-word;
}

.toast.visible {
  transform: translateX(0);
  opacity: 1;
}

.toast.leaving {
  transform: translateX(110%);
  opacity: 0;
}

.toast-info    { border-left-color: var(--green); }
.toast-success { border-left-color: var(--green); }
.toast-warn    { border-left-color: var(--food); }
.toast-error   { border-left-color: var(--red); }

@media (prefers-reduced-motion: reduce) {
  .toast {
    transition: none;
    transform: none;
  }
  .toast.leaving {
    transform: none;
  }
}

@media (max-width: 500px) {
  .toast-container {
    top: 0.5rem;
    right: 0.5rem;
    left: 0.5rem;
    max-width: none;
  }
}
