:root {
  --pink: #ff4d8d;
  --pink-soft: #ffd1e3;
  --purple: #7b2cbf;
  --deep: #1d0b33;
  --ink: #2a1840;
  --muted: #7d6a93;
  --card: rgba(255, 250, 253, 0.95);
  --font: 'Fredoka', 'Trebuchet MS', 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; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--deep);
  font-family: var(--font);
  color: var(--ink);
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#app { position: fixed; inset: 0; overflow: hidden; }

#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

button { font-family: inherit; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ HUD */
#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: none;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.45);
}
#hud.show { display: block; }

.hud-panel {
  position: absolute;
  top: calc(10px + var(--safe-t));
  padding: 6px 14px;
  border-radius: 16px;
  background: rgba(29, 11, 51, 0.42);
  line-height: 1.05;
}
.hud-left { left: calc(10px + var(--safe-l)); }
.hud-right { right: calc(64px + var(--safe-r)); text-align: right; }

.hud-pos { font-size: clamp(30px, 5.4vmin, 50px); font-weight: 700; }
.hud-pos sup { font-size: 0.45em; margin-left: 1px; }
.hud-pos small { font-size: 0.42em; opacity: 0.75; margin-left: 3px; }
.hud-label { font-size: clamp(12px, 2.1vmin, 18px); font-weight: 500; letter-spacing: 0.06em; opacity: 0.95; }
.hud-label b { font-weight: 700; }
.hud-time { font-size: clamp(20px, 3.8vmin, 34px); font-weight: 600; font-variant-numeric: tabular-nums; }

.hud-center {
  position: absolute;
  top: calc(10px + var(--safe-t));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.hud-speed { font-size: clamp(24px, 4.6vmin, 42px); font-weight: 700; font-variant-numeric: tabular-nums; }
.hud-speed small { font-size: 0.4em; margin-left: 4px; opacity: 0.8; }

.boost { display: flex; align-items: center; gap: 6px; }
.boost-heart { font-size: clamp(18px, 3.2vmin, 28px); color: #ff8fb8; transition: transform 0.15s; }
.boost-bar {
  width: clamp(90px, 20vmin, 180px);
  height: clamp(10px, 1.8vmin, 14px);
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.22);
  border: 2px solid rgba(255, 255, 255, 0.7);
  overflow: hidden;
}
#boost-fill {
  height: 100%;
  width: 100%;
  transform-origin: left center;
  transform: scaleX(0);
  background: linear-gradient(90deg, #ff9ec4, #ff3d7f);
}
.boost-count { font-size: clamp(13px, 2.2vmin, 18px); font-weight: 600; }
.boost.ready .boost-heart { color: #ff3d7f; animation: beat 0.9s infinite; }
.boost.active .boost-heart { transform: scale(1.35); }
.boost.active #boost-fill { background: linear-gradient(90deg, #fff, #ff8fb8); }

@keyframes beat { 0%, 100% { transform: scale(1); } 20% { transform: scale(1.25); } 40% { transform: scale(1.05); } }

.hud-btn {
  position: absolute;
  top: calc(12px + var(--safe-t));
  right: calc(10px + var(--safe-r));
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.7);
  background: rgba(29, 11, 51, 0.45);
  color: #fff;
  font-size: 16px;
  pointer-events: auto;
  cursor: pointer;
}

/* ------------------------------------------------------------ Touch controls */
#touch { position: absolute; inset: 0; pointer-events: none; display: none; }
#touch.show { display: block; }

#steer-zone, #pedal-zone {
  position: absolute;
  bottom: calc(14px + var(--safe-b));
  display: flex;
  gap: 14px;
  pointer-events: auto;
  touch-action: none;
}
#steer-zone { left: calc(14px + var(--safe-l)); padding: 10px 10px 0 0; }
#pedal-zone { right: calc(14px + var(--safe-r)); align-items: flex-end; }

.tbtn {
  width: clamp(64px, 17vmin, 96px);
  height: clamp(64px, 17vmin, 96px);
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: clamp(22px, 5vmin, 34px);
  font-weight: 700;
  background: rgba(29, 11, 51, 0.38);
  border: 3px solid rgba(255, 255, 255, 0.65);
  touch-action: none;
  transition: transform 0.06s, background 0.06s;
}
.tbtn.small { width: clamp(56px, 14vmin, 80px); height: clamp(56px, 14vmin, 80px); font-size: clamp(11px, 2.4vmin, 14px); letter-spacing: 0.05em; }
.tbtn.boost-btn { background: rgba(255, 61, 127, 0.5); }
.tbtn.boost-btn small { font-size: clamp(9px, 2vmin, 12px); letter-spacing: 0.08em; }
.tbtn.on { transform: scale(0.92); background: rgba(255, 255, 255, 0.45); }
.tbtn.boost-btn.on { background: rgba(255, 61, 127, 0.9); }

/* ------------------------------------------------------------ Centre messages */
.msg {
  position: absolute;
  left: 50%;
  top: 34%;
  transform: translate(-50%, -50%);
  font-size: clamp(34px, 11vmin, 110px);
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
  text-shadow: 0 4px 0 #c2185b, 0 8px 24px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  opacity: 0;
}
.msg.small { font-size: clamp(22px, 6vmin, 54px); }
.msg.show { animation: pop 1.1s ease-out forwards; }
.msg.hold { animation: pophold 2.4s ease-out forwards; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.4); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.12); }
  30% { transform: translate(-50%, -50%) scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(1); }
}
@keyframes pophold {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.4); }
  8% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  15% { transform: translate(-50%, -50%) scale(1); }
  85% { opacity: 1; }
  100% { opacity: 0; }
}

#hearts-rain { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
#hearts-rain span {
  position: absolute;
  bottom: -60px;
  animation: floatUp linear forwards;
  opacity: 0;
}
@keyframes floatUp {
  0% { transform: translateY(0) rotate(-10deg); opacity: 0; }
  10% { opacity: 1; }
  50% { transform: translateY(-55vh) rotate(12deg) translateX(20px); }
  90% { opacity: 1; }
  100% { transform: translateY(-115vh) rotate(-8deg) translateX(-10px); opacity: 0; }
}

/* ------------------------------------------------------------ Screens & cards */
.screen {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: calc(12px + var(--safe-t)) calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) calc(12px + var(--safe-l));
  background: radial-gradient(ellipse at center, rgba(29, 11, 51, 0.05) 0%, rgba(29, 11, 51, 0.55) 100%);
}
.screen.active { display: flex; animation: fadeIn 0.25s ease-out; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.card {
  width: 100%;
  max-width: 560px;
  max-height: 100%;
  overflow-y: auto;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  background: var(--card);
  border-radius: 26px;
  padding: 22px 24px;
  box-shadow: 0 20px 60px rgba(29, 11, 51, 0.45), 0 0 0 4px rgba(255, 255, 255, 0.35);
  text-align: center;
  animation: cardIn 0.35s cubic-bezier(0.2, 1.4, 0.4, 1);
}
@keyframes cardIn { from { transform: translateY(18px) scale(0.96); } to { transform: none; } }

.card h2 { margin: 4px 0 12px; font-size: 22px; font-weight: 600; color: var(--purple); }
.card h3 { margin: 0 0 6px; font-size: 17px; font-weight: 600; color: var(--purple); }

.title-card { max-width: 520px; }
.logo {
  margin: 0;
  font-size: clamp(40px, 9vmin, 68px);
  font-weight: 700;
  line-height: 1;
  background: linear-gradient(135deg, #ff4d8d 10%, #b5179e 55%, #7b2cbf 90%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 3px 0 rgba(123, 44, 191, 0.18));
}
.subtitle { margin: 8px 0 16px; font-size: 18px; color: var(--muted); }

.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 10px 18px;
  margin: 4px;
  font-size: 16px;
  font-weight: 600;
  color: var(--purple);
  background: #f1e6fb;
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.15s, background 0.15s;
}
.btn:hover { background: #e8d6f8; }
.btn:active { transform: scale(0.96); }
.btn:focus-visible { outline: 3px solid var(--pink); outline-offset: 2px; }
.btn.primary {
  color: #fff;
  background: linear-gradient(135deg, #ff4d8d, #c2185b);
  box-shadow: 0 6px 0 #8e0f44, 0 10px 20px rgba(194, 24, 91, 0.35);
}
.btn.primary:hover { background: linear-gradient(135deg, #ff6b9f, #d81b60); }
.btn.primary:active { transform: translateY(3px); box-shadow: 0 3px 0 #8e0f44; }
.btn.big { font-size: 22px; padding: 14px 42px; margin: 6px 4px 16px; }
.btn.toggle.off { opacity: 0.55; }
.letter-btn {
  color: #fff;
  background: linear-gradient(135deg, #b5179e, #7b2cbf);
  animation: beat 1.6s infinite;
}

.row { display: flex; justify-content: center; align-items: center; gap: 4px; margin-top: 6px; }
.row.wrap { flex-wrap: wrap; }
.hint { font-size: 14px; color: var(--muted); margin: 6px 0; }
.portrait-only { display: none; }
@media (orientation: portrait) and (pointer: coarse) { .portrait-only { display: block; } }

.trophies { margin: 2px 0 10px; }
.trophy-row { display: flex; justify-content: center; gap: 10px; }
.trophy {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 78px;
  padding: 6px 8px;
  border-radius: 14px;
  background: #f6eefc;
  font-size: 12px;
  color: var(--muted);
}
.trophy .t-icon { font-size: 26px; line-height: 1.2; filter: grayscale(1) opacity(0.35); }
.trophy.won { background: #fff3c4; color: #8a5a00; }
.trophy.won .t-icon { filter: none; }

/* setup */
.setup-card { max-width: 720px; }
.track-list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.track-card {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  border: 3px solid transparent;
  border-radius: 18px;
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  background: #eee;
  text-align: left;
  font-family: inherit;
  color: #fff;
  box-shadow: 0 4px 12px rgba(29, 11, 51, 0.18);
  transition: transform 0.12s, border-color 0.12s;
}
.track-card:active { transform: scale(0.97); }
.track-card.sel { border-color: var(--pink); transform: translateY(-2px); box-shadow: 0 8px 18px rgba(255, 77, 141, 0.35); }
.track-thumb { height: 64px; display: flex; align-items: center; justify-content: center; font-size: 34px; }
.track-info { flex: 1; width: 100%; padding: 6px 10px 8px; background: rgba(29, 11, 51, 0.88); }
.track-thumb { width: 100%; }
.track-name { font-weight: 600; font-size: 15px; line-height: 1.15; }
.track-meta { font-size: 12px; opacity: 0.85; display: flex; justify-content: space-between; gap: 4px; }
.track-meta .stars { color: #ffd166; letter-spacing: 1px; }
.track-card .badge { position: absolute; top: 6px; right: 8px; font-size: 18px; }

.setup-split { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 10px; align-items: start; }
#car-preview { width: 100%; max-width: 220px; height: auto; display: block; margin: 0 auto 6px; }
.color-list { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.swatch {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 0 0 2px #d9c8ec;
  cursor: pointer;
  padding: 0;
}
.swatch.sel { box-shadow: 0 0 0 3px var(--pink); transform: scale(1.12); }

.seg-control { display: inline-flex; background: #f1e6fb; border-radius: 999px; padding: 4px; gap: 2px; }
.seg-control button {
  border: 0;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--purple);
  background: transparent;
  cursor: pointer;
}
.seg-control button.sel { background: var(--pink); color: #fff; }

/* how to */
.howto-card { max-width: 620px; text-align: left; }
.howto-card h2, .howto-card > .btn { display: block; text-align: center; margin-left: auto; margin-right: auto; }
.howto-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.howto-block { background: #f6eefc; border-radius: 16px; padding: 12px 14px; }
.howto-block ul, .tips { margin: 0; padding-left: 18px; line-height: 1.7; font-size: 15px; }
.tips { list-style: none; padding: 12px 4px 4px; }
kbd {
  display: inline-block;
  min-width: 22px;
  padding: 0 5px;
  border-radius: 6px;
  background: #fff;
  border: 1px solid #d9c8ec;
  box-shadow: 0 2px 0 #d9c8ec;
  font-family: inherit;
  font-size: 13px;
  text-align: center;
  line-height: 1.5;
}

.small-card { max-width: 320px; display: flex; flex-direction: column; align-items: stretch; }

/* results */
.results-card { max-width: 520px; }
.res-place {
  font-size: clamp(46px, 11vmin, 80px);
  font-weight: 700;
  line-height: 1;
  background: linear-gradient(135deg, #ff4d8d, #7b2cbf);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.res-msg { font-size: 18px; margin: 10px 0 14px; color: var(--ink); }
.res-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.res-stats div { background: #f6eefc; border-radius: 14px; padding: 8px 4px; display: flex; flex-direction: column; }
.res-stats span { font-size: 12px; color: var(--muted); }
.res-stats b { font-size: 18px; font-variant-numeric: tabular-nums; }
.res-record { margin-top: 10px; font-weight: 600; color: #c77d00; }
.res-table { list-style: none; margin: 12px 0 6px; padding: 0; text-align: left; font-size: 14px; }
.res-table li { display: flex; gap: 8px; padding: 4px 10px; border-radius: 10px; }
.res-table li:nth-child(odd) { background: #faf5fe; }
.res-table li.me { background: var(--pink-soft); font-weight: 600; }
.res-table .rk { width: 22px; color: var(--muted); }
.res-table .nm { flex: 1; }
.res-table .tm { font-variant-numeric: tabular-nums; }

/* letter */
.letter-card {
  max-width: 480px;
  background: linear-gradient(180deg, #fff9fb, #ffeef5);
  box-shadow: 0 20px 60px rgba(29, 11, 51, 0.45), 0 0 0 4px rgba(255, 143, 184, 0.6);
}
.letter-card h2 { font-size: 26px; color: #c2185b; }
.letter-body { white-space: pre-line; font-size: 18px; line-height: 1.55; text-align: left; margin: 0 4px; }
.letter-sign { text-align: right; font-size: 18px; font-weight: 600; color: #c2185b; margin: 14px 6px 16px; }

/* loading */
#loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--deep);
  color: #fff;
  font-size: 18px;
  transition: opacity 0.5s;
}
#loading.done { opacity: 0; pointer-events: none; }
.loading-heart { font-size: 64px; color: var(--pink); animation: beat 1s infinite; }

/* ------------------------------------------------------------ Small screens */
@media (max-height: 520px) {
  .card { padding: 14px 18px; border-radius: 20px; }
  .card h2 { font-size: 18px; margin: 2px 0 8px; }
  .subtitle { margin: 4px 0 8px; font-size: 16px; }
  .btn.big { font-size: 19px; padding: 10px 34px; margin: 4px 4px 10px; }
  .btn { padding: 8px 14px; font-size: 14px; }
  .track-thumb { height: 44px; font-size: 26px; }
  #car-preview { max-width: 160px; }
  .res-msg { margin: 6px 0 8px; font-size: 16px; }
  .res-table { display: none; }
}
@media (orientation: portrait) {
  .hud-center { top: calc(84px + var(--safe-t)); }
}
@media (max-width: 560px) {
  .track-name { font-size: 13px; }
  .setup-split, .howto-grid { grid-template-columns: 1fr; }
  .trophy { min-width: 64px; }
  .hud-panel { padding: 4px 10px; }
}
