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

:root {
  --bg:          #0d0d1a;
  --surface:     rgba(255,255,255,0.05);
  --surface-2:   rgba(255,255,255,0.09);
  --border:      rgba(255,255,255,0.10);
  --accent:      #7c6fff;
  --accent-glow: rgba(124,111,255,0.35);
  --correct:     #00d4aa;
  --correct-bg:  rgba(0,212,170,0.15);
  --wrong:       #ff4f6d;
  --wrong-bg:    rgba(255,79,109,0.15);
  --text:        #f0f0f8;
  --muted:       rgba(240,240,248,0.5);
  --radius:      14px;
  --radius-lg:   22px;
  --font:        'Inter', system-ui, sans-serif;
  --mono:        'JetBrains Mono', monospace;
}

html {
  height: 100%;
  height: -webkit-fill-available;
}
body {
  height: 100%;
  min-height: 100svh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  overflow: hidden;
  /* iOS rubber-band prevention */
  overscroll-behavior: none;
}

/* Animated gradient background */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 60% at 20% 10%, rgba(124,111,255,0.12) 0%, transparent 60%),
    radial-gradient(ellipse 60% 50% at 80% 90%, rgba(0,212,170,0.08) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

#app {
  position: relative;
  z-index: 1;
  height: 100svh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Screens ── */
.screen {
  display: none;
  width: 100%;
  height: 100svh;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.35s ease;
}
.screen.active { display: flex; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Start Screen ── */
#start-screen {
  overflow-y: auto;
  padding: 20px 0 60px;
  align-items: center;
}

.start-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  text-align: center;
  padding: 32px 28px;
  max-width: 480px;
  width: 100%;
}

.globe-icon {
  font-size: 72px;
  line-height: 1;
  filter: drop-shadow(0 0 24px rgba(124,111,255,0.5));
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-10px); }
}

.title {
  font-size: clamp(2rem, 6vw, 3.2rem);
  font-weight: 800;
  letter-spacing: -0.03em;
  background: linear-gradient(135deg, #fff 30%, var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  font-size: 1rem;
  color: var(--muted);
  line-height: 1.6;
}

.rules {
  display: flex;
  gap: 20px;
  margin: 4px 0;
}

.rule {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--muted);
}

.chip {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-weight: 700;
  font-size: 0.8rem;
}

.correct-chip { background: var(--correct-bg); color: var(--correct); border: 1px solid rgba(0,212,170,0.3); }
.wrong-chip   { background: var(--wrong-bg);   color: var(--wrong);   border: 1px solid rgba(255,79,109,0.3); }

/* ── Button ── */
.btn-primary {
  margin-top: 8px;
  padding: 14px 48px;
  border: none;
  border-radius: 50px;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  color: #fff;
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  cursor: pointer;
  box-shadow: 0 4px 24px var(--accent-glow);
  transition: transform 0.15s, box-shadow 0.15s, filter 0.15s;
}
.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 32px var(--accent-glow);
  filter: brightness(1.1);
}
.btn-primary:active { transform: translateY(0); }

/* ── HUD ── */
.hud {
  position: fixed;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 14px 24px 10px;
  /* Respect notch / status bar */
  padding-top: max(14px, env(safe-area-inset-top));
  padding-left:  max(24px, env(safe-area-inset-left));
  padding-right: max(24px, env(safe-area-inset-right));
  background: rgba(13,13,26,0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  z-index: 100;
}

.hud-block {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 70px;
}
.hud-block.center { align-items: center; flex: 1; }
.hud-block.right  { align-items: flex-end; }

.hud-label {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

.hud-value {
  font-size: 1.6rem;
  font-weight: 800;
  line-height: 1;
  transition: color 0.3s;
}
.hud-value.negative { color: var(--wrong); }

.hud-timer {
  font-family: var(--mono);
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1;
  color: var(--text);
  transition: color 0.3s;
}
.hud-timer.warning {
  color: var(--wrong);
  animation: pulse-red 0.5s ease infinite alternate;
}
@keyframes pulse-red {
  from { opacity: 1; }
  to   { opacity: 0.55; }
}

/* ── Quit Button ── */
.btn-quit {
  margin-top: 6px;
  padding: 5px 14px;
  border: 1px solid rgba(255,79,109,0.35);
  border-radius: 20px;
  background: transparent;
  color: var(--wrong);
  font-family: var(--font);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn-quit:hover {
  background: var(--wrong-bg);
  border-color: var(--wrong);
}

/* ── Progress Dots ── */
.progress-dots {
  display: flex;
  gap: 5px;
  margin-bottom: 5px;
}
.dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  transition: background 0.3s, border-color 0.3s;
}
.dot.done-correct { background: var(--correct); border-color: var(--correct); }
.dot.done-wrong   { background: var(--wrong);   border-color: var(--wrong); }
.dot.active       { background: var(--accent);  border-color: var(--accent); box-shadow: 0 0 6px var(--accent); }

/* ── Game Main ── */
.game-main {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 96px 20px 60px;   /* top = HUD height; bottom = footer */
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
  justify-content: center;
}

/* ── Flag Card ── */
.flag-card {
  position: relative;
  width: 100%;
  max-width: 440px;
  /* Cap height so choices always fit below on any screen */
  max-height: min(38svh, 400px);
  aspect-ratio: 3 / 2;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow:
    0 0 0 1px rgba(255,255,255,0.04),
    0 20px 60px rgba(0,0,0,0.5);
  flex-shrink: 0;
}

.flag-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.3s;
}

/* ── Answer Overlay ── */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 1.1rem;
  font-weight: 700;
  backdrop-filter: blur(4px);
  border-radius: var(--radius-lg);
  transition: opacity 0.2s;
}
.overlay.hidden { display: none; }
.overlay.correct-overlay { background: rgba(0,212,170,0.25); color: var(--correct); }
.overlay.wrong-overlay   { background: rgba(255,79,109,0.25); color: var(--wrong); }
#overlay-icon { font-size: 2.5rem; }

/* ── Choices Grid ── */
.choices-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  width: 100%;
  max-width: 440px;
}

.choice-btn {
  padding: 13px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  line-height: 1.3;
  transition: background 0.15s, border-color 0.15s, transform 0.12s, box-shadow 0.15s;
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.choice-btn:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: rgba(124,111,255,0.5);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(124,111,255,0.2);
}
.choice-btn:active:not(:disabled) { transform: translateY(0); }
.choice-btn:disabled { cursor: default; opacity: 0.7; }

/* Answer state styles */
.choice-btn.correct {
  background: var(--correct-bg) !important;
  border-color: var(--correct) !important;
  color: var(--correct) !important;
  opacity: 1 !important;
}
.choice-btn.selected-wrong {
  background: var(--wrong-bg) !important;
  border-color: var(--wrong) !important;
  color: var(--wrong) !important;
  opacity: 1 !important;
  animation: shake 0.4s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%       { transform: translateX(-6px); }
  40%       { transform: translateX(6px); }
  60%       { transform: translateX(-4px); }
  80%       { transform: translateX(4px); }
}

/* ── Results Screen ── */
#results-screen {
  align-items: flex-start;
  overflow-y: auto;
  padding: 40px 24px 80px;
}

.results-layout {
  display: flex;
  flex-direction: column;
  gap: 28px;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  animation: fadeIn 0.4s ease;
}

.results-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  width: 100%;
  text-align: center;
}

.result-emoji {
  font-size: 64px;
  filter: drop-shadow(0 0 20px rgba(255,210,0,0.4));
  animation: float 3s ease-in-out infinite;
}

.result-title {
  font-size: 1.8rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.score-big {
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.score-big span:first-child {
  font-size: 5rem;
  font-weight: 800;
  line-height: 1;
  background: linear-gradient(135deg, #fff 30%, var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.score-big span:first-child.negative-score {
  background: linear-gradient(135deg, var(--wrong), #ff8fa3);
  -webkit-background-clip: text;
  background-clip: text;
}
.score-unit {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--muted);
}

.stats-row {
  display: flex;
  gap: 12px;
  width: 100%;
}

.stat-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 16px 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.correct-card { border-color: rgba(0,212,170,0.25); }
.wrong-card   { border-color: rgba(255,79,109,0.25); }

.stat-num {
  font-size: 1.8rem;
  font-weight: 800;
  line-height: 1;
}
.correct-card .stat-num { color: var(--correct); }
.wrong-card   .stat-num { color: var(--wrong); }

.stat-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.mono { font-family: var(--mono); font-size: 1.4rem !important; }

/* ── Results actions row ── */
.results-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  align-items: center;
}

.results-actions-row {
  display: flex;
  gap: 10px;
  align-items: center;
  width: 100%;
  justify-content: center;
}

.btn-home {
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s, transform 0.12s;
}
.btn-home:hover {
  background: var(--surface-2);
  border-color: rgba(255,255,255,0.25);
  transform: translateY(-2px);
}

.btn-leaderboard-add {
  padding: 11px 32px;
  border: 1px solid rgba(124,111,255,0.4);
  border-radius: 50px;
  background: transparent;
  color: var(--accent);
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.12s;
}
.btn-leaderboard-add:hover {
  background: rgba(124,111,255,0.12);
  border-color: var(--accent);
  transform: translateY(-2px);
}
.btn-leaderboard-add:disabled {
  opacity: 0.4;
  cursor: default;
  transform: none;
}

/* ── Leaderboard Panel ── */
.leaderboard-panel {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.lb-title {
  font-size: 1rem;
  font-weight: 700;
  text-align: center;
  letter-spacing: 0.02em;
}

.lb-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.lb-row {
  display: grid;
  grid-template-columns: 22px 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid transparent;
  transition: border-color 0.2s;
  font-size: 0.82rem;
}

.lb-row.lb-current {
  border-color: var(--accent);
  background: rgba(124,111,255,0.12);
}

.lb-rank {
  font-weight: 700;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}
.lb-row:nth-child(1) .lb-rank { color: #ffd700; }
.lb-row:nth-child(2) .lb-rank { color: #c0c0c0; }
.lb-row:nth-child(3) .lb-rank { color: #cd7f32; }

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

.lb-score {
  font-weight: 700;
  font-size: 0.78rem;
  color: var(--correct);
  white-space: nowrap;
}

.lb-time {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--muted);
  white-space: nowrap;
}

.lb-separator {
  text-align: center;
  font-size: 0.68rem;
  color: var(--muted);
  padding: 4px 0;
  letter-spacing: 0.06em;
}

.lb-loading, .lb-empty {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  padding: 16px 0;
}

/* ── Name Modal ── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: fadeIn 0.2s ease;
}
.modal-backdrop.hidden { display: none; }

.modal-box {
  background: #1a1a2e;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 24px 80px rgba(0,0,0,0.6);
}

.modal-title {
  font-size: 1.2rem;
  font-weight: 700;
  text-align: center;
}

.modal-sub {
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
  margin-top: -8px;
}

.name-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 600;
  outline: none;
  letter-spacing: 0.04em;
  transition: border-color 0.15s;
}
.name-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

.name-error {
  font-size: 0.8rem;
  color: var(--wrong);
  text-align: center;
  padding: 6px 10px;
  background: var(--wrong-bg);
  border-radius: 8px;
  border: 1px solid rgba(255,79,109,0.3);
}
.name-error.hidden { display: none; }

.modal-actions {
  display: flex;
  gap: 10px;
}
.modal-actions .btn-primary { flex: 1; }

.btn-ghost {
  flex: 0 0 auto;
  padding: 14px 20px;
  border: 1px solid var(--border);
  border-radius: 50px;
  background: transparent;
  color: var(--muted);
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

/* ══════════════════════════════════════════════
   RESPONSIVE
   ══════════════════════════════════════════════ */

/* ── Mobile portrait (≤ 480px wide) ── */
@media (max-width: 480px) {
  .globe-icon  { font-size: 52px; }
  .title       { font-size: 1.9rem; }
  .start-inner { gap: 14px; padding: 20px; }
  .subtitle    { font-size: 0.9rem; }
  .rules       { gap: 14px; }

  .hud         { padding: 10px 14px 8px; padding-top: max(10px, env(safe-area-inset-top)); }
  .hud-value   { font-size: 1.25rem; }
  .hud-timer   { font-size: 1.15rem; }
  .hud-label   { font-size: 0.6rem; }
  .btn-quit    { font-size: 0.62rem; padding: 4px 10px; }

  .game-main   { padding: 80px 14px 54px; gap: 12px; }
  .flag-card   { max-height: 36vh; }

  .choices-grid { grid-template-columns: 1fr 1fr; gap: 8px; max-width: 100%; }
  .choice-btn   { padding: 10px 6px; font-size: 0.76rem; min-height: 44px; }
}

/* ── Landscape phones & small tablets: height ≤ 500px ── */
/* (covers all landscape phones — avoids relying on orientation media feature) */
@media (max-height: 500px) {
  /* ── Start screen: compact horizontal ── */
  #start-screen  { padding: 0; }
  .start-inner {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    max-width: 760px;
    gap: 14px;
    padding: 8px 36px 56px;
  }
  .globe-icon  { font-size: 42px; animation: none; flex: 0 0 auto; }
  .title       { font-size: 1.5rem; }
  .subtitle    { font-size: 0.78rem; }
  .rules       { margin: 0; }
  .btn-primary { padding: 10px 36px; }

  /* ── HUD: single tight row ── */
  .hud { padding: 6px 16px; padding-top: max(6px, env(safe-area-inset-top)); }
  .hud-value  { font-size: 1.05rem; }
  .hud-timer  { font-size: 1rem; }
  .hud-label  { font-size: 0.52rem; }
  .hud-block  { gap: 1px; min-width: 56px; }
  .btn-quit   { padding: 3px 9px; font-size: 0.58rem; margin-top: 2px; }
  .progress-dots { display: none; }

  /* ── Game: flag left — choices right ── */
  .game-main {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding: 50px 16px 42px;
    padding-left:  max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    gap: 14px;
    max-width: 100%;
  }
  .flag-card {
    flex: 0 0 auto;
    width: clamp(160px, 40vw, 280px);
    max-height: calc(100vh - 96px);
    aspect-ratio: 3 / 2;
  }
  .choices-grid {
    flex: 1;
    grid-template-columns: 1fr 1fr;
    max-width: 360px;
    gap: 7px;
    align-content: center;
  }
  .choice-btn {
    padding: 7px 5px;
    font-size: 0.7rem;
    min-height: 36px;
  }

  /* ── Results: stacked compact ── */
  #results-screen { padding: 50px 16px 56px; }
}

/* ── Tablet portrait (481px – 768px wide) ── */
@media (min-width: 481px) and (max-width: 768px) and (min-height: 501px) {
  .flag-card    { max-height: 40vh; }
  .choices-grid { grid-template-columns: 1fr 1fr 1fr; }
}

/* ── Desktop (≥ 1024px wide) ── */
@media (min-width: 1024px) {
  .flag-card { max-height: 48vh; }
  .game-main { gap: 28px; }
}

/* ── Site Footer ── */
.site-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 16px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  padding-left:   max(16px, env(safe-area-inset-left));
  padding-right:  max(16px, env(safe-area-inset-right));
  background: rgba(13,13,26,0.85);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  font-size: 0.72rem;
  color: var(--muted);
}

.footer-sep { opacity: 0.4; }

.footer-link {
  color: var(--muted);
  text-decoration: none;
  transition: color 0.15s;
}
.footer-link:hover { color: var(--text); }

/* ── Loading / Error States ── */
.loading-flag {
  background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 50%, var(--surface) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
}
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ── Responsive ── */
@media (max-width: 520px) {
  .choices-grid { grid-template-columns: 1fr 1fr; }
  .hud { padding: 14px 16px; }
  .hud-timer { font-size: 1.2rem; }
  .hud-value { font-size: 1.3rem; }
  .game-main { padding-top: 90px; }
  .score-big span:first-child { font-size: 3.5rem; }
}
