:root {
  --ink: #f4efe4;
  --ink-dim: #b8b0a0;
  --panel: rgba(18, 16, 28, 0.86);
  --panel-soft: rgba(30, 26, 44, 0.7);
  --accent: #ffb72b;
  --accent-2: #54d8ff;
  --danger: #e7493e;
  font-size: 16px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  background: #12101c;
  color: var(--ink);
  font-family: "Segoe UI", "Trebuchet MS", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
}

/* ---------- menu layer ---------- */
.menu-layer {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at 50% 30%, rgba(60, 48, 110, 0.75), rgba(10, 8, 18, 0.92));
  overflow-y: auto;
  z-index: 30;
}
.menu-layer.is-hidden { display: none; }

.screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: 28px 20px;
  max-height: 100vh;
  overflow-y: auto;
  width: 100%;
}

.title-lockup { text-align: center; }
.title-art {
  width: min(46vh, 300px, 74vw);
  height: auto;
  border-radius: 12%;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.6), 0 0 60px rgba(84, 216, 255, 0.18);
  display: block;
  margin: 0 auto 14px;
}
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.title-kicker {
  text-transform: uppercase;
  letter-spacing: 0.4em;
  font-size: 0.72rem;
  color: var(--accent-2);
}
.title-lockup h1 {
  font-size: clamp(2.4rem, 8vw, 4.6rem);
  font-weight: 900;
  font-style: italic;
  letter-spacing: 0.02em;
  text-shadow: 0 4px 0 rgba(0,0,0,0.45), 0 10px 30px rgba(84, 216, 255, 0.25);
}
.title-lockup h1 span { color: var(--accent); }

h2 { font-size: clamp(1.3rem, 4vw, 2rem); text-align: center; }
.hint { color: var(--ink-dim); text-align: center; line-height: 1.5; font-size: 0.92rem; }

.menu-list { display: flex; flex-direction: column; gap: 10px; width: min(340px, 88vw); }
.menu-list.row { flex-direction: row; justify-content: center; width: auto; }

.menu-btn {
  appearance: none;
  border: 2px solid rgba(255,255,255,0.14);
  border-radius: 14px;
  background: var(--panel-soft);
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  font-size: 1.05rem;
  padding: 13px 20px;
  cursor: pointer;
  transition: transform 0.08s ease, border-color 0.12s ease, background 0.12s ease;
}
.menu-btn:hover { transform: translateY(-2px) scale(1.02); border-color: var(--accent-2); }
.menu-btn:active { transform: scale(0.98); }
.menu-btn.primary { background: linear-gradient(135deg, #e7493e, #c22c74); border-color: rgba(255,255,255,0.3); }
.menu-btn.dim { opacity: 0.65; font-weight: 500; }

.chip {
  appearance: none;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 999px;
  background: transparent;
  color: var(--ink-dim);
  font: inherit;
  font-size: 0.85rem;
  padding: 7px 14px;
  cursor: pointer;
}
.title-footer { display: flex; gap: 10px; }

/* ---------- pre-race controls popup ---------- */
.ctl-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(460px, 92vw);
  padding: 16px 18px;
  border: 2px solid rgba(255,255,255,0.12);
  border-radius: 16px;
  background: var(--panel-soft);
}
.ctl-row {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.92rem;
  line-height: 1.5;
}
.ctl-keys {
  flex: 0 0 auto;
  min-width: 132px;
  text-align: right;
  color: var(--ink-dim);
}
.ctl-row kbd {
  display: inline-block;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 2px 7px;
  border: 1px solid rgba(255,255,255,0.25);
  border-bottom-width: 3px;
  border-radius: 6px;
  background: rgba(255,255,255,0.08);
}

/* ---------- character select ---------- */
.char-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(138px, 1fr));
  gap: 12px;
  width: min(900px, 94vw);
}
.char-card {
  appearance: none;
  border: 2px solid rgba(255,255,255,0.12);
  border-radius: 16px;
  background: var(--panel-soft);
  color: var(--ink);
  font: inherit;
  padding: 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: transform 0.08s ease, border-color 0.12s ease;
}
.char-card:hover { transform: translateY(-3px); border-color: var(--accent); }
.char-card.is-taken { opacity: 0.35; pointer-events: none; }
.char-card img {
  width: 84px;
  height: 84px;
  border-radius: 12px;
  background: radial-gradient(circle at 50% 35%, rgba(84,216,255,0.25), rgba(0,0,0,0.25));
}
.char-card strong { font-size: 0.98rem; }
.char-card small { color: var(--ink-dim); font-size: 0.72rem; }

.stats { width: 100%; display: flex; flex-direction: column; gap: 2px; margin-top: 4px; }
.stats div { display: flex; align-items: center; gap: 6px; }
.stats em { font-style: normal; font-size: 0.58rem; color: var(--ink-dim); width: 24px; text-align: right; }
.stat-bar {
  flex: 1;
  height: 5px;
  border-radius: 3px;
  background: rgba(255,255,255,0.12);
  overflow: hidden;
}
.stat-bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--accent-2), var(--accent)); border-radius: 3px; }

/* ---------- track select ---------- */
.track-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  width: min(860px, 94vw);
}
.cup-grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); width: min(760px, 94vw); }
.track-card {
  appearance: none;
  border: 2px solid rgba(255,255,255,0.12);
  border-radius: 16px;
  background: var(--panel-soft);
  color: var(--ink);
  font: inherit;
  padding: 16px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  transition: transform 0.08s ease, border-color 0.12s ease;
}
.track-card:hover { transform: translateY(-3px); border-color: var(--accent-2); }
.track-emoji { font-size: 2rem; }
.track-card strong { font-size: 0.98rem; text-align: center; }
.track-card small { color: var(--ink-dim); font-size: 0.74rem; text-align: center; line-height: 1.35; }
.track-card .best { color: var(--accent); }

/* ---------- results ---------- */
.results-table {
  border-collapse: collapse;
  background: var(--panel);
  border-radius: 14px;
  overflow: hidden;
  min-width: min(430px, 92vw);
}
.results-table td { padding: 8px 14px; border-bottom: 1px solid rgba(255,255,255,0.07); font-size: 0.95rem; }
.results-table tr.is-player td { background: rgba(255, 183, 43, 0.14); font-weight: 700; }
.results-table tr:last-child td { border-bottom: none; }
.results-table .pts { text-align: right; }
.results-table em { font-style: normal; font-size: 0.7rem; color: var(--accent-2); }
.swatch {
  display: inline-block;
  width: 12px; height: 12px;
  border-radius: 50%;
  margin-right: 8px;
  border: 1px solid rgba(255,255,255,0.4);
}
.tt-result { text-align: center; display: flex; flex-direction: column; gap: 8px; }
.tt-time { font-size: 3rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.tt-record { color: var(--accent); font-weight: 800; font-size: 1.3rem; animation: pulse 0.9s ease infinite alternate; }
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.08); } }

/* ---------- HUD ---------- */
.hud-layer { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
.phud { position: absolute; display: flex; flex-direction: column; gap: 6px; }
.phud-single { top: max(12px, env(safe-area-inset-top)); left: max(14px, env(safe-area-inset-left)); }
.phud-split.phud-0 { top: max(12px, env(safe-area-inset-top)); left: 14px; }
.phud-split.phud-1 { top: calc(50% + 12px); left: 14px; }

.phud-top { display: flex; align-items: baseline; gap: 12px; }
.phud-pos {
  font-size: 2.1rem;
  font-weight: 900;
  font-style: italic;
  text-shadow: 0 3px 0 rgba(0,0,0,0.5);
  color: var(--accent);
}
.phud-lap {
  font-size: 1rem;
  font-weight: 700;
  background: var(--panel);
  padding: 4px 10px;
  border-radius: 999px;
  width: fit-content;
}
.phud-item {
  width: 58px; height: 58px;
  border-radius: 14px;
  background: var(--panel);
  border: 2px solid rgba(255,255,255,0.22);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.7rem;
}
.phud-speed {
  font-size: 1.25rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 0 rgba(0,0,0,0.5);
}
.phud-speed small { font-size: 0.65rem; color: var(--ink-dim); }
.phud-time { font-size: 1.1rem; font-weight: 700; font-variant-numeric: tabular-nums; background: var(--panel); padding: 4px 10px; border-radius: 8px; width: fit-content; }

.phud-msg {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(2rem, 6vw, 3.4rem);
  font-weight: 900;
  font-style: italic;
  text-shadow: 0 4px 0 rgba(0,0,0,0.55);
  white-space: nowrap;
  pointer-events: none;
}
.phud-single .phud-msg { top: 24vh; }
.phud-split.phud-0 .phud-msg { top: 18vh; }
.phud-split.phud-1 .phud-msg { top: 68vh; }
.phud-msg.pop { animation: msgpop 0.35s ease; }
@keyframes msgpop { 0% { transform: translateX(-50%) scale(1.7); opacity: 0; } 100% { transform: translateX(-50%) scale(1); opacity: 1; } }

.battle-clock {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  font-size: 1.5rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  background: var(--panel);
  padding: 6px 16px;
  border-radius: 999px;
}

.minimap {
  position: absolute;
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  width: 150px;
  height: 150px;
  opacity: 0.9;
}

/* ---------- touch controls ---------- */
.touch-controls {
  position: fixed;
  inset: auto 0 0 0;
  display: none;
  justify-content: space-between;
  align-items: flex-end;
  padding: 0 max(14px, env(safe-area-inset-left)) max(16px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-right));
  z-index: 20;
  pointer-events: none;
}
body.touch-mode.in-race .touch-controls { display: flex; }
.control-bank { display: flex; gap: 12px; pointer-events: auto; }
.touch-button {
  appearance: none;
  border: 2px solid rgba(255,255,255,0.25);
  border-radius: 50%;
  width: 74px; height: 74px;
  background: rgba(18, 16, 28, 0.55);
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  font-size: 0.8rem;
  touch-action: none;
}
.touch-button.small { width: 58px; height: 58px; font-size: 0.66rem; }
.touch-button.is-active { background: rgba(84, 216, 255, 0.35); border-color: var(--accent-2); }
.steering-bank .touch-button { font-size: 1.5rem; }

@media (max-width: 720px) {
  .minimap { width: 104px; height: 104px; }
  .phud-pos { font-size: 1.6rem; }
  .phud-item { width: 46px; height: 46px; font-size: 1.3rem; }
  .char-grid { grid-template-columns: repeat(auto-fit, minmax(112px, 1fr)); }
  .char-card img { width: 62px; height: 62px; }
}
