:root {
  --bg: #1d1833;
  --bg-2: #2a2250;
  --paper: #fff9e6;
  --ink: #1a1333;
  --accent: #ffd84d;
  --accent-2: #ff6b9d;
  --player: #4ad6ff;
  --ai: #ff6b9d;
  --line: rgba(255, 255, 255, 0.1);
  --shadow: 0 6px 0 rgba(0, 0, 0, 0.3);
  /* Height reserved for HUD + controls hint + padding, subtracted from the
     viewport when computing the stage width. Tuned per breakpoint below. */
  --stage-reserve: 160px;
  --stage-w: min(960px, 95vw, calc((100dvh - var(--stage-reserve)) * 16 / 9));
  font-family: 'Fredoka', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background:
    radial-gradient(1200px 800px at 20% 0%, #3a2970 0%, transparent 60%),
    radial-gradient(1000px 700px at 90% 100%, #2a6b7a 0%, transparent 60%),
    var(--bg);
  color: var(--paper);
  overflow: hidden;
}

#app {
  height: 100vh;
  height: 100dvh;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.screen.visible {
  display: flex;
}

/* ── Buttons ───────────────────────────────────────────────────────────────── */

.primary-btn,
.secondary-btn {
  font-family: 'Bangers', sans-serif;
  font-size: 28px;
  letter-spacing: 2px;
  padding: 12px 28px;
  border-radius: 14px;
  border: 4px solid var(--ink);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.primary-btn {
  background: var(--accent);
  color: var(--ink);
}
.secondary-btn {
  background: var(--paper);
  color: var(--ink);
}
.primary-btn:hover,
.secondary-btn:hover {
  transform: translate(-2px, -2px);
  box-shadow: 0 10px 0 rgba(0, 0, 0, 0.35);
}
.primary-btn:active,
.secondary-btn:active {
  transform: translate(0, 2px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
}
.primary-btn.disabled,
.primary-btn[disabled] {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: var(--shadow);
}

/* ── HUD ───────────────────────────────────────────────────────────────────── */

.hud {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: var(--stage-w);
  margin: 0 auto 10px;
  gap: 16px;
}
.hud-lap {
  font-family: 'Bangers', sans-serif;
  letter-spacing: 1.5px;
  font-size: 14px;
  margin-top: 2px;
  color: var(--accent);
  opacity: 0.85;
}
.hud-side {
  flex: 1;
  background: var(--paper);
  color: var(--ink);
  border: 4px solid var(--ink);
  border-radius: 14px;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
}
.hud-ai {
  justify-content: space-between;
}
.hud-label {
  font-family: 'Bangers', sans-serif;
  font-size: 22px;
  letter-spacing: 1.5px;
}
.hud-score {
  font-family: 'Bangers', sans-serif;
  font-size: 40px;
  line-height: 1;
}
.hud-center {
  flex: 0 0 auto;
  text-align: center;
  font-size: 16px;
  opacity: 0.85;
}

/* ── Stage / canvas ────────────────────────────────────────────────────────── */

.stage {
  position: relative;
  width: var(--stage-w);
  aspect-ratio: 16 / 9;
  background: linear-gradient(180deg, #2f1c59 0%, #13051f 100%);
  border: 5px solid var(--ink);
  border-radius: 24px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
}

.controls-hint {
  margin-top: 10px;
  font-size: 14px;
  opacity: 0.7;
}

/* ── Bubble ────────────────────────────────────────────────────────────────── */

.bubble {
  position: absolute;
  top: 10%;
  right: 6%;
  max-width: 42%;
  background: var(--paper);
  color: var(--ink);
  border: 4px solid var(--ink);
  border-radius: 20px;
  padding: 10px 14px;
  font-size: 17px;
  line-height: 1.25;
  box-shadow: var(--shadow);
  transform-origin: bottom left;
  animation: bubblePop 0.18s ease-out;
}
.bubble.hidden {
  display: none;
}
.bubble-name {
  display: block;
  font-family: 'Bangers', sans-serif;
  font-size: 18px;
  letter-spacing: 1px;
  margin-bottom: 2px;
  color: var(--accent-2);
}
.bubble-tail {
  position: absolute;
  bottom: -18px;
  left: 22px;
  width: 28px;
  height: 22px;
  background: var(--paper);
  border-right: 4px solid var(--ink);
  border-bottom: 4px solid var(--ink);
  transform: skewX(-25deg) rotate(10deg);
  border-bottom-right-radius: 10px;
}
@keyframes bubblePop {
  0% {
    transform: scale(0.4) rotate(-8deg);
    opacity: 0;
  }
  60% {
    transform: scale(1.06) rotate(2deg);
    opacity: 1;
  }
  100% {
    transform: scale(1) rotate(0deg);
  }
}

/* ── Popups (BONK!, WHAM!) ─────────────────────────────────────────────────── */

.popups {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.popup {
  position: absolute;
  font-family: 'Bangers', sans-serif;
  font-size: 46px;
  letter-spacing: 2px;
  color: var(--accent);
  -webkit-text-stroke: 3px var(--ink);
  text-shadow: 0 4px 0 var(--ink);
  transform: translate(-50%, -50%) rotate(-6deg);
  animation: popup 0.7s ease-out forwards;
}
.popup.pink {
  color: var(--accent-2);
}
.popup.blue {
  color: var(--player);
}
@keyframes popup {
  0% {
    transform: translate(-50%, -50%) rotate(-12deg) scale(0.2);
    opacity: 0;
  }
  30% {
    transform: translate(-50%, -50%) rotate(-6deg) scale(1.15);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -80%) rotate(-2deg) scale(1);
    opacity: 0;
  }
}

/* ── Countdown / pause ─────────────────────────────────────────────────────── */

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Bangers', sans-serif;
  font-size: 180px;
  color: var(--accent);
  -webkit-text-stroke: 6px var(--ink);
  text-shadow: 0 10px 0 var(--ink);
  pointer-events: none;
  animation: countdown 1s ease-out forwards;
}
.countdown.hidden {
  display: none;
}
@keyframes countdown {
  0% {
    transform: scale(0.4);
    opacity: 0;
  }
  30% {
    transform: scale(1.1);
    opacity: 1;
  }
  100% {
    transform: scale(0.9);
    opacity: 0;
  }
}

.pause-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
}
.pause-overlay.hidden {
  display: none;
}
.pause-card {
  background: var(--paper);
  color: var(--ink);
  border: 5px solid var(--ink);
  border-radius: 18px;
  padding: 20px 36px;
  text-align: center;
  box-shadow: var(--shadow);
}
.pause-card h2 {
  font-family: 'Bangers', sans-serif;
  font-size: 50px;
  margin: 0;
  letter-spacing: 2px;
}

/* ── Game over ─────────────────────────────────────────────────────────────── */

.over-card {
  background: var(--paper);
  color: var(--ink);
  border: 5px solid var(--ink);
  border-radius: 22px;
  padding: 28px 32px;
  width: min(520px, 95vw);
  box-shadow: var(--shadow);
  text-align: center;
}
.over-title {
  font-family: 'Bangers', sans-serif;
  font-size: 56px;
  margin: 0;
  letter-spacing: 2px;
}
.over-summary {
  margin: 8px 0 16px;
  font-size: 16px;
}
.over-score-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  background: rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 10px 16px;
  margin-bottom: 18px;
}
.over-score-label {
  font-size: 16px;
  font-weight: 600;
}
.over-score {
  font-family: 'Bangers', sans-serif;
  font-size: 38px;
}
.submit-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 18px;
  text-align: left;
}
.submit-form label {
  font-size: 14px;
  font-weight: 600;
}
.submit-form input {
  font-family: 'Fredoka', sans-serif;
  font-size: 18px;
  padding: 10px 12px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
}
.submit-form button {
  align-self: flex-start;
}
.submit-status {
  min-height: 20px;
  font-size: 14px;
}
.submit-status.ok {
  color: #1f8a3a;
}
.submit-status.err {
  color: #b01f1f;
}
.over-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

/* ── Responsive tweaks ─────────────────────────────────────────────────────── */

/* Mobile: squeeze surrounding chrome so the stage can fill the phone's full
   viewport height. Controls hint is touch-implicit so we drop it, and the
   screen padding/HUD are trimmed to claw back vertical space. */
@media (max-width: 900px) {
  :root {
    --stage-reserve: 95px;
    --stage-w: min(100vw, calc((100dvh - var(--stage-reserve)) * 16 / 9));
  }
  .screen {
    padding: 6px;
  }
  .hud {
    margin-bottom: 6px;
    gap: 8px;
  }
  .hud-side {
    padding: 4px 10px;
    border-width: 3px;
  }
  .hud-label {
    font-size: 14px;
  }
  .hud-score {
    font-size: 26px;
  }
  .hud-center {
    font-size: 13px;
  }
  .hud-lap {
    font-size: 12px;
  }
  .controls-hint {
    display: none;
  }
  .stage {
    border-width: 3px;
    border-radius: 14px;
  }
  .bubble {
    font-size: 15px;
    max-width: 62%;
  }
  .over-title {
    font-size: 40px;
  }
}
