:root {
  --pink: #ff2ea6;
  --cyan: #18e0ff;
  --lime: #7dff3a;
  --gold: #ffd92e;
  --bg: #0a0018;
  --panel: rgba(14, 0, 34, 0.78);
  --font: "Avenir Next", "Futura", "Trebuchet MS", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: fixed;
  inset: 0;
  background: var(--bg);
  color: #fff;
  font-family: var(--font);
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
}

button { font-family: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }

.hidden { display: none !important; }

#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 45; }

/* ---------- logo ---------- */
.logo {
  margin: 0;
  display: flex;
  flex-direction: column;
  line-height: 0.86;
  font-style: italic;
  font-weight: 900;
  letter-spacing: 0.04em;
  font-size: clamp(48px, 9vmin, 92px);
  transform: skewX(-6deg);
}
.logo span { display: block; }
.logo .l1 {
  background: linear-gradient(180deg, #fff 0%, #ffd1f0 35%, #ff2ea6 55%, #7a0bd1 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 14px rgba(255, 46, 166, 0.75));
}
.logo .l2 {
  background: linear-gradient(180deg, #e9fdff 0%, #9af3ff 40%, #18e0ff 58%, #1b4cff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 14px rgba(24, 224, 255, 0.7));
  padding-left: 0.5em;
}
.logo.small { font-size: 54px; }

/* ---------- loading ---------- */
#loading {
  position: fixed; inset: 0; z-index: 100;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 24px;
  background: radial-gradient(ellipse at 50% 70%, #3a0a5a 0%, #0a0018 70%);
}
#load-msg { font-weight: 700; letter-spacing: 0.1em; opacity: 0.85; text-align: center; padding: 0 24px; }

/* ---------- screens ---------- */
.screen { position: fixed; inset: 0; z-index: 30; display: flex; }
.screen.center { align-items: center; justify-content: center; background: rgba(8, 0, 20, 0.55); padding: 16px; }

#title {
  align-items: center;
  justify-content: flex-start;
  background: linear-gradient(90deg, rgba(8, 0, 22, 0.88) 0%, rgba(8, 0, 22, 0.55) 38%, rgba(8, 0, 22, 0) 62%);
  padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b)) calc(24px + var(--safe-l));
}
#title .panel {
  width: min(440px, 100%);
  max-height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  display: flex; flex-direction: column; gap: 16px;
  padding: 20px 28px 48px 20px;
  margin-left: -20px;
}
.tag { margin: -4px 0 4px; font-size: 18px; font-weight: 600; color: #ffd1f0; letter-spacing: 0.04em; }
.group { display: flex; flex-direction: column; gap: 8px; }
.label { font-size: 13px; font-weight: 800; letter-spacing: 0.18em; text-transform: uppercase; color: #9af3ff; }
.row { display: flex; gap: 18px; flex-wrap: wrap; }

.swatches { display: flex; gap: 12px; flex-wrap: wrap; }
.swatch {
  width: 48px; height: 48px; border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.25);
  box-shadow: inset 0 -6px 12px rgba(0, 0, 0, 0.35);
  transition: transform 0.15s, box-shadow 0.15s;
  padding: 0;
}
.swatch.on { border-color: #fff; transform: scale(1.12); box-shadow: 0 0 18px var(--c), inset 0 -6px 12px rgba(0, 0, 0, 0.3); }

.seg { display: inline-flex; border-radius: 14px; overflow: hidden; border: 2px solid rgba(255, 255, 255, 0.2); background: rgba(255, 255, 255, 0.05); }
.seg button {
  border: 0; background: transparent; padding: 10px 16px; font-size: 17px; font-weight: 800; min-width: 52px;
  color: rgba(255, 255, 255, 0.75);
}
.seg button.on { background: linear-gradient(180deg, #ff5cc0, #c2129a); color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4); }

.toggle {
  display: inline-flex; align-items: center; gap: 12px; align-self: flex-start;
  border: 0; background: transparent; padding: 4px 0; font-size: 17px; font-weight: 700;
}
.toggle .box {
  width: 30px; height: 30px; border-radius: 9px; border: 2px solid rgba(255, 255, 255, 0.45);
  display: inline-flex; align-items: center; justify-content: center;
}
.toggle.on .box { background: var(--lime); border-color: var(--lime); box-shadow: 0 0 12px var(--lime); }
.toggle.on .box::after { content: ""; width: 12px; height: 7px; border: 4px solid #0a0018; border-top: 0; border-right: 0; transform: translateY(-2px) rotate(-45deg); }

.big-btn {
  border: 0;
  border-radius: 18px;
  padding: 16px 28px;
  font-size: 28px;
  font-weight: 900;
  font-style: italic;
  letter-spacing: 0.08em;
  color: #fff;
  background: linear-gradient(180deg, #ff6ac8 0%, #ff2ea6 45%, #b10f8a 100%);
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.15) inset, 0 8px 30px rgba(255, 46, 166, 0.55);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.35);
  animation: pulse 1.8s ease-in-out infinite;
}
.big-btn:active { transform: scale(0.96); }
@keyframes pulse { 50% { box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.25) inset, 0 8px 44px rgba(255, 46, 166, 0.9); } }
.btn2 {
  border: 2px solid rgba(255, 255, 255, 0.3); border-radius: 16px; background: rgba(255, 255, 255, 0.06);
  padding: 13px 22px; font-size: 19px; font-weight: 800;
}
.btn2:active { transform: scale(0.96); }

.best { font-size: 15px; font-weight: 700; color: var(--gold); letter-spacing: 0.06em; min-height: 1em; }
.help { font-size: 14px; color: rgba(255, 255, 255, 0.65); line-height: 1.45; }

.card {
  background: var(--panel);
  border: 2px solid rgba(255, 46, 166, 0.45);
  box-shadow: 0 0 40px rgba(255, 46, 166, 0.35), 0 0 0 1px rgba(24, 224, 255, 0.25) inset;
  border-radius: 24px;
  padding: 24px 26px;
  width: min(460px, 100%);
  max-height: 100%;
  overflow-y: auto;
  touch-action: pan-y;
  display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center;
}
.card-title { font-size: 40px; font-weight: 900; font-style: italic; letter-spacing: 0.06em; }
.btns { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; margin-top: 4px; }
.btns.col { flex-direction: column; width: 100%; }

.place {
  font-size: 84px; font-weight: 900; font-style: italic; line-height: 1;
  background: linear-gradient(180deg, #fff 0%, #ffe680 40%, #ffb01c 70%, #ff2ea6 100%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 0 18px rgba(255, 217, 46, 0.6));
  animation: bounceIn 0.7s cubic-bezier(.2, 1.6, .4, 1) both;
}
.place sup { font-size: 0.45em; }
.msg { font-size: 22px; font-weight: 800; color: #ffd1f0; }
@keyframes bounceIn { from { transform: scale(0.2) rotate(-10deg); opacity: 0; } to { transform: none; opacity: 1; } }

.table { width: 100%; display: flex; flex-direction: column; gap: 6px; }
.trow {
  display: grid; grid-template-columns: auto 18px 1fr auto; align-items: center; gap: 10px;
  padding: 8px 12px; border-radius: 12px; background: rgba(255, 255, 255, 0.06); font-weight: 700; font-size: 17px; text-align: left;
}
.trow.me { background: rgba(255, 46, 166, 0.28); box-shadow: 0 0 0 2px rgba(255, 46, 166, 0.6) inset; }
.trow .dot { width: 16px; height: 16px; border-radius: 50%; box-shadow: 0 0 10px currentColor; }
.trow > span:first-child { white-space: nowrap; }
.trow .t { font-variant-numeric: tabular-nums; opacity: 0.85; }
.stats { font-size: 16px; font-weight: 700; color: #9af3ff; line-height: 1.6; }

/* ---------- top buttons ---------- */
#topbtns {
  position: fixed; z-index: 50;
  top: calc(12px + var(--safe-t)); right: calc(12px + var(--safe-r));
  display: flex; gap: 10px;
}
.icon-btn {
  width: 52px; height: 52px; border-radius: 16px; border: 2px solid rgba(255, 255, 255, 0.3);
  background: rgba(14, 0, 34, 0.55); display: flex; align-items: center; justify-content: center; padding: 0;
}
.icon-btn svg { width: 28px; height: 28px; fill: none; stroke: #fff; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .fill { fill: #fff; }
#btn-mute .ico-off { display: none; }
#btn-mute.muted .ico-on { display: none; }
#btn-mute.muted .ico-off { display: block; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; font-style: italic; font-weight: 900; }
.hud-tl { position: absolute; top: calc(12px + var(--safe-t)); left: calc(16px + var(--safe-l)); display: flex; flex-direction: column; gap: 2px; }
.pos { font-size: 64px; line-height: 1; text-shadow: 0 0 18px rgba(255, 46, 166, 0.9), 0 3px 0 #7a0b6a; }
.pos sup { font-size: 0.42em; vertical-align: top; position: relative; top: 0.18em; }
#hud-posof { font-size: 0.42em; opacity: 0.7; margin-left: 4px; }
.lap { font-size: 24px; letter-spacing: 0.06em; color: #9af3ff; text-shadow: 0 0 12px rgba(24, 224, 255, 0.8); }
.stars { font-size: 22px; color: var(--gold); text-shadow: 0 0 12px rgba(255, 217, 46, 0.7); }
#minimap { width: 150px; height: 120px; margin-top: 8px; opacity: 0.95; }
.hud-tr {
  position: absolute; top: calc(76px + var(--safe-t)); right: calc(16px + var(--safe-r));
  text-align: right; display: flex; flex-direction: column; gap: 2px;
}
.time { font-size: 32px; font-variant-numeric: tabular-nums; text-shadow: 0 0 14px rgba(24, 224, 255, 0.8); }
.sub { font-size: 17px; color: var(--gold); font-variant-numeric: tabular-nums; text-shadow: 0 0 10px rgba(255, 217, 46, 0.5); }
.hud-bc {
  position: absolute; left: 50%; bottom: calc(18px + var(--safe-b)); transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.speedo { display: flex; align-items: baseline; gap: 6px; }
#hud-speed { font-size: 58px; line-height: 1; font-variant-numeric: tabular-nums; min-width: 2.2em; text-align: right; text-shadow: 0 0 18px rgba(24, 224, 255, 0.9), 0 3px 0 #0b4a6a; }
.unit { font-size: 16px; color: #9af3ff; letter-spacing: 0.1em; }
.boostbar {
  position: relative; width: 220px; height: 22px; border-radius: 11px; overflow: hidden;
  background: rgba(255, 255, 255, 0.1); border: 2px solid rgba(24, 224, 255, 0.6);
}
#hud-boostfill {
  position: absolute; inset: 0; transform-origin: 0 50%; transform: scaleX(0);
  background: linear-gradient(90deg, #1b4cff, #18e0ff 60%, #e9fdff);
}
.boostbar span { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 13px; letter-spacing: 0.3em; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8); }
.boostbar.full { box-shadow: 0 0 16px var(--cyan); animation: boostReady 0.8s ease-in-out infinite alternate; }
.boostbar.active #hud-boostfill { background: linear-gradient(90deg, #ff8a1c, #ffd92e 60%, #fff); }
@keyframes boostReady { to { box-shadow: 0 0 28px var(--cyan); } }

#wrongway {
  position: absolute; left: 50%; top: 30%; transform: translate(-50%, -50%);
  font-size: 40px; color: #fff; background: rgba(255, 30, 90, 0.75); padding: 10px 24px; border-radius: 16px;
  opacity: 0; transition: opacity 0.2s; white-space: nowrap;
}
#wrongway.show { opacity: 1; animation: blink 0.6s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0.4; } }

#toast {
  position: fixed; z-index: 15; left: 50%; top: 22%; transform: translate(-50%, -50%);
  font-size: 44px; font-weight: 900; font-style: italic; white-space: nowrap; pointer-events: none;
  color: #fff; text-shadow: 0 0 20px var(--pink), 0 0 40px var(--pink); opacity: 0;
}
#toast.show { animation: toast 1.8s ease-out both; }
#toast.gold { text-shadow: 0 0 20px var(--gold), 0 0 40px #ff8a1c; }
#toast.cyan { text-shadow: 0 0 20px var(--cyan), 0 0 40px #1b4cff; }
@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
  12% { opacity: 1; transform: translate(-50%, -50%) scale(1.12); }
  20% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -70%) scale(1); }
}

#countdown {
  position: fixed; z-index: 16; left: 50%; top: 40%; transform: translate(-50%, -50%);
  font-size: 180px; font-weight: 900; font-style: italic; pointer-events: none; opacity: 0;
  color: #fff; text-shadow: 0 0 30px var(--pink), 0 0 60px var(--pink), 0 6px 0 #7a0b6a;
}
#countdown.go { color: var(--lime); text-shadow: 0 0 30px var(--lime), 0 0 70px #2aff7a, 0 6px 0 #1b6a0b; }
#countdown.pop { animation: pop 0.95s ease-out both; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(2.4); }
  18% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(0.7); }
}

/* ---------- touch controls ---------- */
#touch { position: fixed; inset: 0; z-index: 20; touch-action: none; }
.tc { position: absolute; bottom: calc(18px + var(--safe-b)); display: flex; gap: 16px; align-items: flex-end; pointer-events: none; }
.tc-left { left: calc(18px + var(--safe-l)); }
.tc-right { right: calc(18px + var(--safe-r)); }
.tcol { display: flex; flex-direction: column; gap: 14px; align-items: center; }
.tbtn {
  width: 104px; height: 104px; border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.4);
  background: rgba(20, 0, 44, 0.42);
  display: flex; align-items: center; justify-content: center;
  font-weight: 900; font-style: italic; font-size: 18px; letter-spacing: 0.06em;
  color: #fff; transition: transform 0.06s, background 0.06s;
}
.tbtn svg { width: 54px; height: 54px; fill: none; stroke: #fff; stroke-width: 3.4; stroke-linecap: round; stroke-linejoin: round; }
.tbtn.steer { border-color: rgba(255, 46, 166, 0.8); box-shadow: 0 0 16px rgba(255, 46, 166, 0.35); }
.tbtn.gas { width: 128px; height: 128px; font-size: 30px; border-color: rgba(125, 255, 58, 0.9); box-shadow: 0 0 18px rgba(125, 255, 58, 0.35); }
.tbtn.boost { width: 96px; height: 96px; font-size: 16px; border-color: rgba(24, 224, 255, 0.9); box-shadow: 0 0 18px rgba(24, 224, 255, 0.4); }
.tbtn.brake { width: 80px; height: 80px; font-size: 14px; border-color: rgba(255, 180, 60, 0.8); }
.tbtn.on { transform: scale(0.92); background: rgba(255, 46, 166, 0.55); }
.tbtn.gas.on { background: rgba(125, 255, 58, 0.5); }
.tbtn.boost.on { background: rgba(24, 224, 255, 0.55); }
.tbtn.brake.on { background: rgba(255, 160, 40, 0.55); }

body.touch .hud-bc { bottom: calc(24px + var(--safe-b)); }
body.touch #hud-speed { font-size: 46px; }
body.touch .boostbar { width: 170px; }

/* ---------- portrait ---------- */
@media (orientation: portrait) {
  #title {
    align-items: flex-end; justify-content: center;
    background: linear-gradient(0deg, rgba(8, 0, 22, 0.92) 0%, rgba(8, 0, 22, 0.7) 45%, rgba(8, 0, 22, 0) 70%);
    padding: calc(16px + var(--safe-t)) 18px calc(18px + var(--safe-b));
  }
  #title .panel { width: min(520px, 100%); align-items: center; text-align: center; max-height: 64%; }
  #title .logo { align-items: center; }
  #title .logo .l2 { padding-left: 0; }
  #title .row { justify-content: center; }
  #title .group { align-items: center; }
  #title .swatches { justify-content: center; }
  #title .toggle { align-self: center; }
  #title .big-btn { align-self: stretch; }
  .pos { font-size: 52px; }
  #minimap { width: 120px; height: 100px; }
  body.touch .hud-bc { bottom: calc(160px + var(--safe-b)); }
  #countdown { font-size: 150px; top: 36%; }
  #toast { font-size: 34px; }
}

/* ---------- small screens ---------- */
@media (max-height: 560px) and (orientation: landscape) {
  #title .panel { gap: 8px; padding-top: 8px; }
  .logo { font-size: 36px; }
  .group { gap: 4px; }
  .row { flex-wrap: nowrap; gap: 12px; }
  .seg button { padding: 7px 10px; font-size: 15px; min-width: 40px; }
  .toggle { font-size: 15px; }
  .toggle .box { width: 24px; height: 24px; }
  .tag { display: none; }
  .swatch { width: 34px; height: 34px; }
  .swatches { gap: 9px; }
  .big-btn { font-size: 22px; padding: 11px 20px; }
  .help { display: none; }
  .pos { font-size: 44px; }
  #minimap { width: 110px; height: 88px; }
  .tbtn { width: 84px; height: 84px; }
  .tbtn.gas { width: 100px; height: 100px; }
  .tbtn.boost { width: 76px; height: 76px; }
  .tbtn.brake { width: 64px; height: 64px; }
  .place { font-size: 60px; }
  .card { padding: 16px 18px; gap: 8px; }
  .trow { padding: 5px 10px; font-size: 15px; }
}
@media (max-width: 420px) {
  .tbtn { width: 84px; height: 84px; }
  .tbtn.gas { width: 100px; height: 100px; }
  .tbtn.boost { width: 76px; height: 76px; }
  .tbtn.brake { width: 64px; height: 64px; }
  .tc { gap: 10px; }
  .hud-tr .time { font-size: 24px; }
}

#title .panel, .card { scrollbar-width: none; }
#title .panel::-webkit-scrollbar, .card::-webkit-scrollbar { display: none; }
