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

:root {
  --bg-grad: linear-gradient(135deg, #1e1b4b 0%, #312e81 50%, #4c1d95 100%);
  --glass:   rgba(255,255,255,.1);
  --glass-border: rgba(255,255,255,.2);
  --accent:  #6366f1;
  --green:   #4ade80;
  --amber:   #fbbf24;
  --red:     #f87171;
  --white:   #ffffff;
}

html, body {
  height: 100%;
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg-grad);
  color: var(--white);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}

/* ── Screen management ───────────────────────────────── */
.screen {
  display: none;
  width: 100%;
  max-width: 540px;
  min-height: 100vh;
  padding: 24px 16px 32px;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}
.screen.active { display: flex; }

/* ── Glass card ──────────────────────────────────────── */
.glass {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 16px;
  padding: 20px;
  width: 100%;
}

/* ══════════════════════════════════════════════════════
   LOGIN SCREEN
══════════════════════════════════════════════════════ */
#login-screen {
  justify-content: center;
  gap: 16px;
}

.login-title {
  text-align: center;
  font-size: clamp(1.8rem, 6vw, 2.6rem);
  font-weight: 800;
  letter-spacing: 1px;
  text-shadow: 0 4px 20px rgba(0,0,0,.4);
  margin-bottom: 4px;
}
.login-subtitle {
  opacity: .6;
  font-size: .95rem;
  text-align: center;
  margin-top: -8px;
}

.user-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
}

.user-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px 20px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 16px;
  cursor: pointer;
  transition: background .15s, transform .1s;
  min-height: 72px;
  -webkit-tap-highlight-color: transparent;
}
.user-card:active { transform: scale(.97); background: rgba(255,255,255,.18); }

.user-emoji {
  font-size: 2.2rem;
  line-height: 1;
  flex-shrink: 0;
  width: 44px;
  text-align: center;
}
.user-info { flex: 1; }
.user-name {
  font-size: 1.25rem;
  font-weight: 700;
}
.user-hint {
  font-size: .8rem;
  opacity: .55;
  margin-top: 2px;
}
.user-arrow {
  opacity: .4;
  font-size: 1.2rem;
}

/* PIN overlay */
#pin-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10,8,40,.85);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
#pin-overlay.visible {
  opacity: 1;
  pointer-events: all;
}

.pin-sheet {
  background: linear-gradient(180deg, #2d2a6e 0%, #1e1b4b 100%);
  border-top: 1px solid var(--glass-border);
  border-radius: 20px 20px 0 0;
  padding: 20px 16px 32px;
  width: 100%;
  max-width: 420px;
  transform: translateY(100%);
  transition: transform .25s cubic-bezier(.32,1,.45,1);
}
#pin-overlay.visible .pin-sheet { transform: translateY(0); }

.pin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.pin-who {
  font-size: 1.1rem;
  font-weight: 700;
}
.pin-close {
  background: none;
  border: none;
  color: rgba(255,255,255,.5);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

.pin-dots {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-bottom: 20px;
}
.pin-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.4);
  background: transparent;
  transition: background .1s, border-color .1s;
}
.pin-dot.filled {
  background: var(--accent);
  border-color: var(--accent);
}
.pin-dot.error {
  background: var(--red);
  border-color: var(--red);
}

.pin-error {
  text-align: center;
  color: var(--red);
  font-size: .9rem;
  min-height: 1.2em;
  margin-bottom: 8px;
}

/* Shared numpad (used by PIN and game) */
.numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: 100%;
}

.numpad-btn {
  background: rgba(255,255,255,.12);
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 12px;
  color: var(--white);
  font-size: 1.6rem;
  font-weight: 700;
  font-family: inherit;
  min-height: 64px;
  cursor: pointer;
  transition: background .1s, transform .08s;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.numpad-btn:active { background: rgba(255,255,255,.25); transform: scale(.94); }

.numpad-btn.submit {
  background: var(--green);
  border-color: transparent;
  color: #052e16;
  font-size: 1.3rem;
}
.numpad-btn.submit:active { background: #22c55e; }

.numpad-btn.backspace {
  font-size: 1.3rem;
  grid-column: 1;
}
.numpad-btn.zero   { grid-column: 2; }
.numpad-btn.submit { grid-column: 1 / -1; }

/* ══════════════════════════════════════════════════════
   GAME SCREEN
══════════════════════════════════════════════════════ */
#game-screen {
  position: relative;
  padding-top: 16px;
  gap: 0;
}

/* Top HUD */
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0 4px;
  margin-bottom: 8px;
}

.hearts {
  display: flex;
  gap: 6px;
  font-size: 1.6rem;
  line-height: 1;
}
.heart { transition: transform .2s; }
.heart.lost { opacity: .25; transform: scale(.8); }

.score-display {
  text-align: right;
}
.score-label {
  font-size: .7rem;
  opacity: .55;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.score-value {
  font-size: 1.8rem;
  font-weight: 800;
  line-height: 1;
  color: var(--amber);
}

/* Level barometer — positioned at far right */
.barometer-wrap {
  position: absolute;
  right: 0;
  top: 16px;
  bottom: 0;
  width: 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0 180px;
  gap: 6px;
  pointer-events: none;
}

.barometer-label {
  font-size: .6rem;
  opacity: .5;
  text-transform: uppercase;
  letter-spacing: .5px;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  transform: rotate(180deg);
}

.barometer-track {
  flex: 1;
  width: 18px;
  background: rgba(255,255,255,.08);
  border: 1px solid rgba(255,255,255,.15);
  border-radius: 9px;
  overflow: hidden;
  display: flex;
  flex-direction: column-reverse;
}

.barometer-fill {
  width: 100%;
  background: linear-gradient(0deg, #4ade80 0%, #6366f1 60%, #fbbf24 100%);
  border-radius: 9px;
  transition: height .4s cubic-bezier(.34,1.56,.64,1);
}

.barometer-level {
  font-size: .75rem;
  font-weight: 800;
  color: var(--amber);
}

/* Game content area (left of barometer) */
.game-inner {
  width: calc(100% - 52px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  flex: 1;
}

/* Equation display */
.equation-area {
  width: 100%;
  min-height: 160px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 20px;
  padding: 20px 16px;
  flex-shrink: 0;
}

.equation-text {
  font-size: clamp(2.8rem, 12vw, 4.5rem);
  font-weight: 900;
  letter-spacing: 4px;
  text-align: center;
  line-height: 1;
}

.answer-display {
  font-size: clamp(1.8rem, 7vw, 3rem);
  font-weight: 700;
  color: var(--green);
  min-height: 1.2em;
  letter-spacing: 2px;
}
.answer-display:empty::after {
  content: '?';
  opacity: .25;
}

.streak-bar {
  display: flex;
  gap: 4px;
  margin-top: 4px;
}
.streak-pip {
  width: 10px;
  height: 6px;
  border-radius: 3px;
  background: rgba(255,255,255,.15);
  transition: background .2s;
}
.streak-pip.on { background: var(--green); }

/* Game numpad */
.game-numpad {
  width: 100%;
}
.game-numpad .numpad-btn {
  min-height: 68px;
  font-size: 1.7rem;
}
.game-numpad .numpad-btn.submit {
  font-size: 1.1rem;
}

/* ── Animations ──────────────────────────────────────── */
@keyframes shake {
  0%,100% { transform: translateX(0); }
  15%      { transform: translateX(-10px) rotate(-1.5deg); }
  30%      { transform: translateX(10px) rotate(1.5deg); }
  45%      { transform: translateX(-8px); }
  60%      { transform: translateX(8px); }
  75%      { transform: translateX(-4px); }
}
.shake { animation: shake .4s ease; }

@keyframes flash-correct {
  0%   { background: rgba(74,222,128,.35); }
  100% { background: var(--glass); }
}
.flash-correct { animation: flash-correct .4s ease; }

@keyframes flash-wrong {
  0%,50% { background: rgba(248,113,113,.35); }
  100%   { background: var(--glass); }
}
.flash-wrong { animation: flash-wrong .5s ease; }

@keyframes level-up-pulse {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.18); }
  70%  { transform: scale(.95); }
  100% { transform: scale(1); }
}
.level-up-pulse { animation: level-up-pulse .5s cubic-bezier(.34,1.56,.64,1); }

@keyframes score-pop {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.35); color: #fef08a; }
  100% { transform: scale(1); }
}
.score-pop { animation: score-pop .3s ease; }

/* Confetti particles */
.confetti-particle {
  position: fixed;
  width: 10px;
  height: 10px;
  pointer-events: none;
  z-index: 999;
  border-radius: 2px;
}
@keyframes confetti-fall {
  0%   { transform: translateY(-20px) rotate(0deg); opacity: 1; }
  100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}

/* Level up banner */
.level-banner {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  border: 2px solid rgba(255,255,255,.3);
  border-radius: 20px;
  padding: 20px 36px;
  text-align: center;
  z-index: 200;
  pointer-events: none;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
  transition: transform .25s cubic-bezier(.34,1.56,.64,1);
}
.level-banner.show { transform: translate(-50%, -50%) scale(1); }
.level-banner h2 { font-size: 1.8rem; font-weight: 900; }
.level-banner p  { opacity: .75; margin-top: 4px; }
.banner-bonus    { color: #4ade80; font-weight: 800; font-size: 1.1rem; opacity: 1 !important; margin-top: 6px; }

/* ══════════════════════════════════════════════════════
   GAME OVER SCREEN
══════════════════════════════════════════════════════ */
#gameover-screen {
  justify-content: center;
  gap: 16px;
}

.gameover-title {
  text-align: center;
  font-size: clamp(2rem, 7vw, 2.8rem);
  font-weight: 900;
  text-shadow: 0 4px 20px rgba(0,0,0,.4);
}

.final-score-wrap {
  text-align: center;
  padding: 24px;
}
.final-score-label {
  font-size: .85rem;
  opacity: .55;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.final-score-value {
  font-size: clamp(3rem, 14vw, 5rem);
  font-weight: 900;
  color: var(--amber);
  line-height: 1;
  text-shadow: 0 0 30px rgba(251,191,36,.4);
}
.personal-best-badge {
  display: inline-block;
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  color: #78350f;
  font-weight: 800;
  font-size: .85rem;
  padding: 4px 12px;
  border-radius: 20px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity .4s .5s;
}
.personal-best-badge.show { opacity: 1; }

.leaderboard {
  width: 100%;
}
.leaderboard-title {
  font-size: .8rem;
  opacity: .5;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 10px;
  text-align: center;
}
.leaderboard-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 12px;
  margin-bottom: 8px;
  background: rgba(255,255,255,.06);
  border: 1px solid rgba(255,255,255,.1);
  transition: background .2s;
}
.leaderboard-row.is-me {
  background: rgba(99,102,241,.25);
  border-color: rgba(99,102,241,.5);
}
.lb-rank {
  font-weight: 800;
  font-size: 1rem;
  width: 24px;
  text-align: center;
  color: var(--amber);
}
.lb-rank.gold   { color: #fbbf24; }
.lb-rank.silver { color: #94a3b8; }
.lb-rank.bronze { color: #cd7f32; }
.lb-emoji { font-size: 1.3rem; }
.lb-name  { flex: 1; font-weight: 600; }
.lb-score { font-weight: 800; color: var(--amber); font-size: 1.05rem; }

.gameover-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 14px;
  font-weight: 700;
  font-family: inherit;
  font-size: 1.05rem;
  cursor: pointer;
  padding: 18px 22px;
  min-height: 60px;
  transition: transform .1s, opacity .1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  width: 100%;
}
.btn:active { transform: scale(.97); opacity: .88; }

.btn-primary {
  background: var(--accent);
  color: var(--white);
}
.btn-secondary {
  background: rgba(255,255,255,.12);
  border: 1px solid rgba(255,255,255,.2);
  color: var(--white);
}

/* ── Misc ─────────────────────────────────────────────── */
.hidden { display: none !important; }

/* Speed indicator (small text under equation) */
.speed-hint {
  font-size: .75rem;
  opacity: .4;
  min-height: 1em;
}
