/* =============================================
   DRILL SERGEANT - Math Sprint
   Space theme with falling card timer mechanic
   ============================================= */

/* --- Reset & Base --- */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-deep: #0a0e27;
  --bg-mid: #131740;
  --bg-card: #1a1f4e;
  --accent: #ffd166;
  --accent-glow: #ffd16680;
  --correct: #06d6a0;
  --correct-glow: #06d6a040;
  --wrong: #ef476f;
  --wrong-glow: #ef476f40;
  --text: #e8e8f0;
  --text-dim: #8888aa;
  --text-bright: #ffffff;
  --purple: #7b68ee;
  --purple-glow: #7b68ee60;
  --card-start: #1e2466;
  --card-end: #2a1a5e;
  --font-main: 'Segoe UI', system-ui, -apple-system, sans-serif;
  --font-mono: 'SF Mono', 'Menlo', 'Consolas', monospace;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-main);
  background: var(--bg-deep);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
}

#app {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}

/* --- Starfield Background --- */
.stars-bg {
  position: absolute;
  inset: 0;
  overflow: hidden;
  z-index: 0;
  pointer-events: none;
}

.stars-bg::before,
.stars-bg::after {
  content: '';
  position: absolute;
  width: 2px;
  height: 2px;
  border-radius: 50%;
  background: white;
  box-shadow:
    25px 50px 0 0 rgba(255,255,255,0.3),
    100px 120px 0 0 rgba(255,255,255,0.5),
    200px 80px 0 0 rgba(255,255,255,0.2),
    300px 200px 0 0 rgba(255,255,255,0.4),
    420px 30px 0 0 rgba(255,255,255,0.3),
    500px 180px 0 0 rgba(255,255,255,0.5),
    650px 90px 0 0 rgba(255,255,255,0.2),
    750px 250px 0 0 rgba(255,255,255,0.4),
    850px 40px 0 0 rgba(255,255,255,0.3),
    950px 160px 0 0 rgba(255,255,255,0.5),
    80px 300px 0 0 rgba(255,255,255,0.4),
    180px 400px 0 0 rgba(255,255,255,0.2),
    350px 350px 0 0 rgba(255,255,255,0.5),
    500px 420px 0 0 rgba(255,255,255,0.3),
    620px 380px 0 0 rgba(255,255,255,0.4),
    780px 450px 0 0 rgba(255,255,255,0.2),
    900px 320px 0 0 rgba(255,255,255,0.5),
    1050px 400px 0 0 rgba(255,255,255,0.3),
    150px 550px 0 0 rgba(255,255,255,0.4),
    400px 600px 0 0 rgba(255,255,255,0.3),
    700px 560px 0 0 rgba(255,255,255,0.5),
    1000px 580px 0 0 rgba(255,255,255,0.2);
  animation: twinkle 4s ease-in-out infinite alternate;
}

.stars-bg::after {
  width: 1px;
  height: 1px;
  box-shadow:
    50px 150px 0 0 rgba(255,255,255,0.2),
    150px 30px 0 0 rgba(255,255,255,0.4),
    250px 250px 0 0 rgba(255,255,255,0.3),
    380px 100px 0 0 rgba(255,255,255,0.5),
    480px 300px 0 0 rgba(255,255,255,0.2),
    600px 50px 0 0 rgba(255,255,255,0.4),
    720px 200px 0 0 rgba(255,255,255,0.3),
    830px 350px 0 0 rgba(255,255,255,0.5),
    960px 100px 0 0 rgba(255,255,255,0.2),
    1080px 250px 0 0 rgba(255,255,255,0.4),
    30px 480px 0 0 rgba(255,255,255,0.3),
    280px 520px 0 0 rgba(255,255,255,0.4),
    550px 500px 0 0 rgba(255,255,255,0.2),
    820px 530px 0 0 rgba(255,255,255,0.5),
    1100px 490px 0 0 rgba(255,255,255,0.3);
  animation: twinkle 3s ease-in-out 1s infinite alternate;
}

@keyframes twinkle {
  0% { opacity: 0.6; }
  100% { opacity: 1; }
}

/* --- Screen Management --- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
  z-index: 1;
}

.screen.active {
  opacity: 1;
  pointer-events: auto;
  z-index: 10;
}

/* --- Buttons --- */
.big-btn {
  display: inline-block;
  padding: 16px 56px;
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: 2px;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  background: linear-gradient(135deg, var(--accent), #ffb347);
  color: var(--bg-deep);
  box-shadow: 0 4px 24px var(--accent-glow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  text-transform: uppercase;
}

.big-btn:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow: 0 6px 32px var(--accent-glow);
}

.big-btn:active {
  transform: translateY(0) scale(0.98);
}

.big-btn.secondary {
  background: linear-gradient(135deg, var(--purple), #5b4cc4);
  color: var(--text-bright);
  box-shadow: 0 4px 24px var(--purple-glow);
}

.big-btn.secondary:hover {
  box-shadow: 0 6px 32px var(--purple-glow);
}

.text-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1rem;
  cursor: pointer;
  padding: 8px 16px;
  transition: color 0.2s ease;
}

.text-btn:hover {
  color: var(--accent);
}

.pulse {
  animation: pulse-glow 2s ease-in-out infinite;
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 4px 24px var(--accent-glow); }
  50% { box-shadow: 0 4px 40px var(--accent), 0 0 60px var(--accent-glow); }
}

/* ===== START SCREEN ===== */
.start-content {
  text-align: center;
  z-index: 2;
  max-width: 480px;
  width: 100%;
  padding: 20px;
}

.game-title {
  font-size: 2.6rem;
  font-weight: 800;
  letter-spacing: 3px;
  color: var(--text-bright);
  margin-bottom: 4px;
  text-shadow: 0 0 30px var(--purple-glow);
}

.title-icon {
  color: var(--accent);
  font-size: 1.8rem;
  vertical-align: middle;
}

.subtitle {
  font-size: 1.1rem;
  color: var(--text-dim);
  margin-bottom: 40px;
  letter-spacing: 4px;
  text-transform: uppercase;
}

/* Mode Selector */
.mode-selector {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-bottom: 32px;
}

.mode-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 16px 32px;
  border: 2px solid var(--bg-card);
  border-radius: 12px;
  background: var(--bg-mid);
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
}

.mode-btn.selected {
  border-color: var(--accent);
  color: var(--text-bright);
  background: var(--bg-card);
  box-shadow: 0 0 20px var(--accent-glow);
}

.mode-btn.locked {
  opacity: 0.4;
  cursor: not-allowed;
}

.mode-symbol {
  font-size: 2rem;
  font-weight: 700;
}

.mode-label {
  font-size: 0.8rem;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.coming-soon {
  position: absolute;
  top: -8px;
  right: -8px;
  background: var(--purple);
  color: white;
  font-size: 0.55rem;
  padding: 2px 6px;
  border-radius: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Settings Panel */
.settings-panel {
  background: var(--bg-mid);
  border: 1px solid #ffffff10;
  border-radius: 16px;
  padding: 20px 24px;
  margin-bottom: 32px;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
}

.setting-row + .setting-row {
  border-top: 1px solid #ffffff08;
}

.setting-row label {
  font-size: 0.85rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  min-width: 80px;
}

.setting-options {
  display: flex;
  gap: 6px;
}

.setting-btn {
  padding: 6px 16px;
  font-size: 0.8rem;
  border: 1px solid #ffffff15;
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.15s ease;
}

.setting-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

.setting-btn.selected {
  background: var(--accent);
  color: var(--bg-deep);
  border-color: var(--accent);
  font-weight: 600;
}

#start-btn {
  margin-bottom: 20px;
}

.lb-icon {
  color: var(--accent);
}

/* ===== COUNTDOWN SCREEN ===== */
#countdown-screen {
  background: var(--bg-deep);
}

.countdown-number {
  font-size: 10rem;
  font-weight: 900;
  color: var(--accent);
  text-shadow: 0 0 60px var(--accent-glow), 0 0 120px var(--accent-glow);
  animation: countdown-pop 0.5s ease-out;
  z-index: 2;
}

@keyframes countdown-pop {
  0% { transform: scale(2); opacity: 0; }
  60% { transform: scale(0.9); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ===== GAME SCREEN ===== */
#game-screen {
  background: var(--bg-deep);
}

/* HUD */
.game-hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 28px;
  z-index: 20;
  background: linear-gradient(to bottom, var(--bg-deep) 60%, transparent);
}

.hud-left, .hud-right {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
}

.hud-label {
  color: var(--text-dim);
  font-size: 0.8rem;
  margin: 0 4px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.streak-display {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--accent);
  opacity: 0;
  transform: scale(0.8);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.streak-display.visible {
  opacity: 1;
  transform: scale(1);
}

.streak-flame {
  font-size: 1.4rem;
  animation: flame-dance 0.5s ease-in-out infinite alternate;
}

@keyframes flame-dance {
  0% { transform: rotate(-5deg) scale(1); }
  100% { transform: rotate(5deg) scale(1.1); }
}

/* Question Track — full screen, card falls from top to landing zone */
.question-track {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 60px;
  z-index: 10;
  overflow: hidden;
}

.question-card {
  width: 420px;
  max-width: 90vw;
  padding: 48px 36px;
  background: linear-gradient(145deg, var(--card-start), var(--card-end));
  border: 2px solid #ffffff15;
  border-radius: 24px;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0,0,0,0.4), 0 0 60px var(--purple-glow);
  position: relative;
  /* The card will be animated via JS using transform: translateY */
  will-change: transform;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.question-card.correct-flash {
  border-color: var(--correct);
  box-shadow: 0 8px 40px rgba(0,0,0,0.4), 0 0 40px var(--correct-glow);
}

.question-card.wrong-flash {
  border-color: var(--wrong);
  box-shadow: 0 8px 40px rgba(0,0,0,0.4), 0 0 40px var(--wrong-glow);
  animation: shake 0.3s ease;
}

@keyframes shake {
  0%, 100% { margin-left: 0; }
  20% { margin-left: -8px; }
  40% { margin-left: 8px; }
  60% { margin-left: -6px; }
  80% { margin-left: 4px; }
}

.question-text {
  font-size: 3.6rem;
  font-weight: 800;
  color: var(--text-bright);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  font-family: var(--font-mono);
}

.operator {
  color: var(--accent);
  font-size: 2.8rem;
}

.equals {
  color: var(--text-dim);
  font-size: 2.8rem;
}

.answer-box {
  display: inline-block;
  min-width: 90px;
  padding: 4px 12px;
  border-bottom: 4px solid var(--accent);
  color: var(--accent);
  font-family: var(--font-mono);
  text-align: center;
}

.answer-box.has-value {
  color: var(--text-bright);
}

/* Landing zone — the "deadline" line the card falls toward */
.landing-zone {
  position: absolute;
  bottom: 28%;
  left: 50%;
  transform: translateX(-50%);
  width: 460px;
  max-width: 92vw;
  height: 3px;
  background: linear-gradient(90deg, transparent, var(--accent) 20%, var(--accent) 80%, transparent);
  opacity: 0.25;
  border-radius: 2px;
  z-index: 5;
  pointer-events: none;
}

/* Danger zone below the landing line — the "abyss" */
.landing-zone::after {
  content: '';
  position: absolute;
  top: 3px;
  left: -20%;
  width: 140%;
  height: 40vh;
  background: linear-gradient(to bottom, rgba(239, 71, 111, 0.06), transparent 60%);
  pointer-events: none;
}

/* Warning glow when card is close to landing zone */
.question-card.time-warning ~ .landing-zone {
  opacity: 0.6;
  background: linear-gradient(90deg, transparent, var(--wrong) 20%, var(--wrong) 80%, transparent);
}

/* Progress dots — positioned below the landing zone */
.progress-track {
  position: absolute;
  bottom: 18%;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  z-index: 20;
}

.progress-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--bg-card);
  border: 1px solid #ffffff15;
  transition: all 0.2s ease;
}

.progress-dot.correct {
  background: var(--correct);
  border-color: var(--correct);
  box-shadow: 0 0 6px var(--correct-glow);
}

.progress-dot.wrong {
  background: var(--wrong);
  border-color: var(--wrong);
}

.progress-dot.current {
  background: var(--accent);
  border-color: var(--accent);
  transform: scale(1.3);
}

/* Feedback */
.feedback-area {
  position: absolute;
  bottom: 10%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  pointer-events: none;
}

.feedback-correct,
.feedback-wrong,
.feedback-timeout {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 12px 28px;
  border-radius: 12px;
  font-size: 1.1rem;
  font-weight: 600;
  animation: feedback-pop 0.3s ease-out;
  white-space: nowrap;
}

@keyframes feedback-pop {
  0% { transform: scale(0.5) translateY(20px); opacity: 0; }
  100% { transform: scale(1) translateY(0); opacity: 1; }
}

.feedback-correct {
  background: var(--correct);
  color: var(--bg-deep);
}

.feedback-wrong {
  background: var(--wrong);
  color: white;
}

.feedback-timeout {
  background: #ff8c00;
  color: var(--bg-deep);
}

.feedback-icon {
  font-size: 1.3rem;
}

/* ===== END SCREEN ===== */
.end-content {
  text-align: center;
  z-index: 2;
  max-width: 500px;
  width: 100%;
  padding: 20px;
}

.end-title {
  font-size: 2rem;
  font-weight: 800;
  color: var(--text-bright);
  margin-bottom: 16px;
}

.star-rating {
  font-size: 3rem;
  margin-bottom: 12px;
  display: flex;
  justify-content: center;
  gap: 8px;
}

.star {
  color: #333;
  transition: color 0.3s ease, transform 0.3s ease, text-shadow 0.3s ease;
}

.star.earned {
  color: var(--accent);
  text-shadow: 0 0 20px var(--accent-glow);
  animation: star-earn 0.4s ease-out backwards;
}

@keyframes star-earn {
  0% { transform: scale(0) rotate(-30deg); opacity: 0; }
  60% { transform: scale(1.3) rotate(5deg); }
  100% { transform: scale(1) rotate(0); }
}

.personal-best-badge {
  color: var(--accent);
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 2px;
  margin-bottom: 16px;
  animation: best-pulse 1.5s ease-in-out infinite;
}

@keyframes best-pulse {
  0%, 100% { opacity: 1; text-shadow: 0 0 10px var(--accent-glow); }
  50% { opacity: 0.7; text-shadow: 0 0 30px var(--accent); }
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 28px;
}

.stat-box {
  background: var(--bg-mid);
  border: 1px solid #ffffff10;
  border-radius: 14px;
  padding: 16px;
}

.stat-value {
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--text-bright);
  font-family: var(--font-mono);
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-top: 4px;
}

.name-entry {
  margin-bottom: 24px;
}

.name-entry label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 8px;
}

.name-entry input {
  width: 200px;
  padding: 12px 20px;
  font-size: 1.3rem;
  font-weight: 700;
  text-align: center;
  text-transform: uppercase;
  background: var(--bg-card);
  border: 2px solid #ffffff20;
  border-radius: 12px;
  color: var(--text-bright);
  outline: none;
  font-family: var(--font-mono);
  letter-spacing: 3px;
  transition: border-color 0.2s ease;
}

.name-entry input:focus {
  border-color: var(--accent);
}

.end-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.end-actions .big-btn {
  width: 260px;
}

/* ===== LEADERBOARD SCREEN ===== */
.leaderboard-content {
  z-index: 2;
  max-width: 560px;
  width: 100%;
  padding: 20px;
  text-align: center;
}

.lb-title {
  font-size: 2rem;
  font-weight: 800;
  color: var(--text-bright);
  margin-bottom: 20px;
}

.lb-star {
  color: var(--accent);
}

.lb-mode-tabs {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 20px;
}

.lb-tab {
  padding: 8px 24px;
  border: 1px solid #ffffff15;
  border-radius: 10px;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.lb-tab.selected {
  background: var(--accent);
  color: var(--bg-deep);
  border-color: var(--accent);
  font-weight: 600;
}

.lb-tab:disabled:not(.selected) {
  opacity: 0.3;
  cursor: not-allowed;
}

.lb-table-container {
  background: var(--bg-mid);
  border: 1px solid #ffffff10;
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 20px;
  min-height: 200px;
}

.lb-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

.lb-table th {
  font-size: 0.7rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 8px 12px;
  border-bottom: 1px solid #ffffff10;
}

.lb-table td {
  padding: 10px 12px;
  font-size: 0.9rem;
  border-bottom: 1px solid #ffffff05;
}

.lb-table tbody tr:hover {
  background: #ffffff05;
}

.lb-rank { width: 40px; text-align: center; }
.lb-score { text-align: right; }
.lb-accuracy { text-align: center; }
.lb-date { text-align: right; color: var(--text-dim); }

.lb-table td.lb-rank {
  font-weight: 700;
  color: var(--accent);
  text-align: center;
}

.lb-table td.lb-score {
  font-weight: 700;
  font-family: var(--font-mono);
  text-align: right;
  color: var(--text-bright);
}

.lb-table td.lb-accuracy {
  text-align: center;
}

.lb-table td.lb-date {
  text-align: right;
  font-size: 0.8rem;
}

.lb-empty {
  padding: 40px;
  color: var(--text-dim);
  font-style: italic;
  display: none;
}

.lb-empty.visible {
  display: block;
}

/* Rank medals */
.rank-1 { color: var(--accent) !important; }
.rank-2 { color: #c0c0c0 !important; }
.rank-3 { color: #cd7f32 !important; }

/* ===== SOUND TOGGLE ===== */
.sound-toggle {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 100;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--bg-mid);
  border: 1px solid #ffffff15;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 1.1rem;
}

.sound-toggle:hover {
  border-color: var(--accent);
}

.sound-toggle.muted {
  opacity: 0.4;
}

/* ===== CARD FALLING ANIMATION (controlled by JS) ===== */
.question-card.falling {
  transition: none;
}

/* Card enter animation */
@keyframes card-enter {
  0% {
    opacity: 0;
    transform: translateY(-40px) scale(0.9);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.question-card.entering {
  animation: card-enter 0.3s ease-out forwards;
}

/* Card exit animations */
@keyframes card-exit-correct {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.05);
  }
  100% {
    opacity: 0;
    transform: scale(0.8) translateY(-30px);
  }
}

@keyframes card-exit-wrong {
  0% {
    opacity: 1;
    transform: translateX(0);
  }
  100% {
    opacity: 0;
    transform: translateX(-60px);
  }
}

.question-card.exit-correct {
  animation: card-exit-correct 0.3s ease-in forwards;
}

.question-card.exit-wrong {
  animation: card-exit-wrong 0.25s ease-in forwards;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 600px) {
  .game-title { font-size: 1.8rem; }
  .question-text { font-size: 2.6rem; gap: 10px; }
  .question-card { padding: 36px 24px; }
  .answer-box { min-width: 70px; }
  .stats-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat-value { font-size: 1.4rem; }
}
