:root {
  --bg: #0b0d10;
  --bg-card: #14181d;
  --bg-elev: #1c2128;
  --fg: #f0f3f5;
  --muted: #8a93a0;
  --accent: #ff3a2e;
  --accent-2: #ff8a1c;
  --good: #3ddc84;
  --warn: #ffc02e;
  --bad: #ff5577;
  --border: #2a313a;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#game-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 1;
  background: #000;
}
#hud {
  position: fixed;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}
.hidden { display: none !important; }

/* Screens */
.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 16px;
  min-height: 100%;
}
.screen h1 {
  font-size: 28px;
  margin: 0;
  letter-spacing: -0.02em;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.screen h2 { font-size: 20px; margin: 0; }
.screen p { margin: 0; color: var(--muted); line-height: 1.4; }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}
.brand-mark {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; color: white;
  box-shadow: 0 8px 30px rgba(255, 60, 40, 0.3);
}

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.card.tap { cursor: pointer; transition: transform 0.1s, border-color 0.1s; }
.card.tap:active { transform: scale(0.98); border-color: var(--accent); }

/* Buttons */
.btn {
  -webkit-appearance: none;
  appearance: none;
  border: none;
  background: var(--bg-elev);
  color: var(--fg);
  padding: 14px 18px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform 0.08s, opacity 0.1s, background 0.15s;
  font-family: inherit;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: white;
  box-shadow: 0 6px 20px rgba(255, 60, 40, 0.35);
}
.btn.ghost { background: transparent; border: 1px solid var(--border); }
.btn.danger { background: var(--bad); color: white; }
.btn.full { width: 100%; }
.btn.small { padding: 8px 12px; font-size: 14px; }

/* Inputs */
.input, .select, .textarea {
  -webkit-appearance: none;
  appearance: none;
  background: var(--bg-elev);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 16px;
  font-family: inherit;
  width: 100%;
}
.textarea { min-height: 120px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
}
label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  font-weight: 500;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.back {
  background: var(--bg-elev);
  border: none;
  color: var(--fg);
  width: 36px; height: 36px;
  border-radius: 50%;
  font-size: 20px;
  cursor: pointer;
}

/* Stat row */
.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.stat {
  background: var(--bg-elev);
  border-radius: 12px;
  padding: 10px 12px;
}
.stat-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.stat-value { font-size: 22px; font-weight: 700; }

/* List */
.list { display: flex; flex-direction: column; gap: 8px; }
.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  background: var(--bg-elev);
  border-radius: 10px;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 80px;
  transform: translateX(-50%);
  background: var(--bg-elev);
  color: var(--fg);
  padding: 12px 18px;
  border-radius: 30px;
  border: 1px solid var(--border);
  z-index: 100;
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  animation: toast-in 0.2s;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* HUD */
.hud-top {
  position: absolute;
  top: 0; left: 0; right: 0;
  padding: 16px;
  padding-top: calc(var(--safe-top) + 16px);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
}
.hud-speed {
  font-size: 56px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
  text-shadow: 0 4px 20px rgba(0,0,0,0.7);
  line-height: 1;
}
.hud-speed-unit { font-size: 14px; color: var(--muted); margin-left: 4px; }
.hud-score {
  text-align: right;
}
.hud-score-value {
  font-size: 28px;
  font-weight: 800;
  text-shadow: 0 2px 10px rgba(0,0,0,0.7);
}
.hud-score-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

/* Mini-map */
.hud-minimap {
  position: absolute;
  top: calc(var(--safe-top) + 90px);
  left: 12px;
  width: 120px;
  height: 160px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(11, 13, 16, 0.7);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  pointer-events: none;
}

.audio-toggles {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 16px);
  right: 16px;
  display: flex;
  gap: 8px;
  pointer-events: auto;
  z-index: 6;
}
.audio-btn {
  width: 44px;
  height: 44px;
  border-radius: 22px;
  border: 1px solid rgba(255,255,255,0.2);
  background: rgba(11, 13, 16, 0.7);
  color: white;
  font-size: 18px;
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.audio-btn:active { transform: scale(0.92); }

/* Latin word card (step 1 of two-step quiz) */
.latin-card {
  -webkit-appearance: none;
  appearance: none;
  background: linear-gradient(135deg, rgba(255,138,28,0.15), rgba(255,58,46,0.15));
  border: 2px solid var(--accent);
  border-radius: 14px;
  padding: 18px 22px;
  font-size: 32px;
  font-weight: 800;
  color: var(--fg);
  font-family: inherit;
  cursor: pointer;
  letter-spacing: -0.01em;
  transition: transform 0.08s, background 0.15s;
  width: 100%;
  text-align: center;
}
.latin-card:active { transform: scale(0.98); background: linear-gradient(135deg, rgba(255,138,28,0.3), rgba(255,58,46,0.3)); }

/* Race leaderboard */
.hud-leaderboard {
  position: absolute;
  top: calc(var(--safe-top) + 90px);
  right: 12px;
  background: rgba(11, 13, 16, 0.7);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 130px;
  pointer-events: none;
}
.hud-lb-row {
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px;
}
.hud-lb-row.me { color: var(--accent-2); font-weight: 700; }
.hud-lb-bar {
  width: 60px; height: 4px; background: rgba(255,255,255,0.12);
  border-radius: 2px; overflow: hidden;
}
.hud-lb-bar-fill { height: 100%; background: var(--accent); transition: width 0.2s; }

/* Question card */
.question-card {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 70px);
  left: 16px; right: 16px;
  background: rgba(20, 24, 29, 0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 -10px 40px rgba(0,0,0,0.5);
}
.question-prompt {
  font-size: 14px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.question-word {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.answer-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.answer-btn {
  -webkit-appearance: none;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--fg);
  padding: 14px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  font-family: inherit;
  text-align: center;
  cursor: pointer;
  transition: all 0.1s;
  min-height: 56px;
}
.answer-btn:active { transform: scale(0.96); }
.answer-btn.correct { background: var(--good); color: #003318; border-color: var(--good); }
.answer-btn.wrong { background: var(--bad); color: white; border-color: var(--bad); }
.answer-btn.disabled { pointer-events: none; opacity: 0.6; }

/* Reward overlay */
.reward {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0,0,0,0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 20px;
  animation: reward-in 0.3s ease-out;
}
@keyframes reward-in { from { opacity: 0; } to { opacity: 1; } }
.reward img {
  max-width: 100%;
  max-height: 70vh;
  border-radius: 16px;
  box-shadow: 0 20px 80px rgba(255, 60, 40, 0.4);
  object-fit: cover;
}
.reward-title {
  font-size: 26px;
  font-weight: 800;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.reward-streak {
  font-size: 14px;
  color: var(--muted);
}

/* Avatar / shop grids */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.tile {
  background: var(--bg-elev);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  text-align: center;
  cursor: pointer;
  transition: all 0.1s;
  position: relative;
}
.tile.active { border-color: var(--accent); }
.tile.locked { opacity: 0.5; }
.tile-swatch {
  height: 48px;
  border-radius: 10px;
  margin-bottom: 8px;
}
.tile-label { font-size: 13px; font-weight: 600; }
.tile-cost { font-size: 11px; color: var(--warn); margin-top: 2px; }

/* Camera scan */
.scan-view {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: black;
  display: flex;
  flex-direction: column;
}
.scan-view video {
  flex: 1;
  width: 100%;
  object-fit: cover;
}
.scan-view canvas { display: none; }
.scan-controls {
  padding: 16px;
  padding-bottom: calc(var(--safe-bottom) + 16px);
  display: flex;
  gap: 12px;
  justify-content: center;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.shutter {
  width: 72px; height: 72px;
  border-radius: 50%;
  background: white;
  border: 4px solid rgba(255,255,255,0.3);
  cursor: pointer;
}
.shutter:active { transform: scale(0.92); }

/* Vocab list */
.vocab-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
  align-items: center;
}
.vocab-row .input { padding: 8px 10px; font-size: 14px; }

/* Lobby */
.lobby-code {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: 0.15em;
  text-align: center;
  font-family: 'SF Mono', Menlo, monospace;
  background: var(--bg-elev);
  padding: 16px;
  border-radius: 12px;
}
.player-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  background: var(--bg-elev);
  border-radius: 10px;
}
.player-name { font-weight: 600; }
.player-meta { font-size: 12px; color: var(--muted); }

/* Garage home screen */
#garage-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  background: #0a0c10;
}
.garage-shell {
  position: fixed;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}
.garage-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 14px 16px 0;
  pointer-events: auto;
}
.garage-top .pill {
  background: rgba(11,13,16,0.6);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 30px;
  padding: 8px 14px;
  border: 1px solid rgba(255,255,255,0.1);
  display: flex; align-items: center; gap: 8px;
  font-size: 13px;
}
.garage-top .pts { color: var(--accent-2); font-weight: 800; }
.garage-bottom {
  margin-top: auto;
  padding: 0 16px 14px;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.garage-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}
.g-action {
  background: rgba(20,24,29,0.75);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 14px;
  padding: 12px 8px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.08s;
}
.g-action:active { transform: scale(0.96); }
.g-action-icon { font-size: 24px; }
.g-action-label { font-size: 12px; font-weight: 600; margin-top: 2px; }
.g-cta {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: white;
  text-align: center;
  padding: 16px;
  border-radius: 14px;
  font-weight: 800;
  font-size: 17px;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(255, 60, 40, 0.35);
  border: none;
  font-family: inherit;
  width: 100%;
}
.g-cta:active { transform: scale(0.98); }
.g-track-pill {
  background: rgba(11,13,16,0.6);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 30px;
  padding: 8px 14px;
  font-size: 13px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
}
.g-tap-label {
  position: absolute;
  background: rgba(0,0,0,0.7);
  color: white;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 11px;
  pointer-events: none;
  border: 1px solid rgba(255,255,255,0.1);
}

/* Friends UI */
.friend-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px;
  background: var(--bg-elev);
  border-radius: 10px;
  gap: 8px;
}
.friend-info { flex: 1; min-width: 0; }
.friend-username { font-size: 13px; color: var(--muted); }
.friend-name { font-weight: 600; }

/* Helper */
.row { display: flex; gap: 8px; }
.row.between { justify-content: space-between; align-items: center; }
.col { display: flex; flex-direction: column; gap: 8px; }
.spacer { flex: 1; }
.center { text-align: center; }
.muted { color: var(--muted); }
.tag {
  display: inline-block;
  background: var(--bg-elev);
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.tag.accent { color: var(--accent-2); }
.tag.good { color: var(--good); }

/* Avatar preview */
.avatar-preview {
  width: 100%;
  aspect-ratio: 1 / 1;
  background: linear-gradient(180deg, #1a2030, #0b0d10);
  border-radius: 16px;
  position: relative;
  overflow: hidden;
}

/* Loading */
.spinner {
  width: 24px; height: 24px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@keyframes spin { to { transform: rotate(360deg); } }
.spin-row { display: flex; align-items: center; gap: 12px; padding: 12px; }

/* Pulses for correct answer */
@keyframes flash-good {
  0% { background: var(--good); }
  100% { background: var(--bg-elev); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
