:root {
  color-scheme: dark;
  font-family: system-ui, sans-serif;
  color: #f6f6f0;
  background: #141c20;
  --top: max(16px, env(safe-area-inset-top));
  --left: max(18px, env(safe-area-inset-left));
  --right: max(18px, env(safe-area-inset-right));
  --bottom: max(18px, env(safe-area-inset-bottom));
}
* { box-sizing: border-box; }
body { margin: 0; overflow: hidden; }
#yard, canvas { position: fixed; inset: 0; width: 100%; height: 100%; }
header, #tools { position: absolute; top: var(--top); }
header { left: var(--left); }
h1 { margin: 0; font-size: clamp(1.2rem, 4vw, 2rem); line-height: 1.1; letter-spacing: .04em; }
h1 span { display: block; margin-top: 5px; font-size: .75rem; font-weight: 550; letter-spacing: .08em; text-transform: uppercase; }
.keyboard, .surface-readout, #fps { margin: 8px 0 0; font-size: .75rem; color: #d0dbdf; }
#tools { right: var(--right); display: flex; align-items: center; gap: 12px; }
#fps { margin: 0; font-variant-numeric: tabular-nums; }
button {
  color: inherit;
  font: inherit;
  border: 1px solid #f6f6f05c;
  border-radius: 12px;
  padding: 9px 14px;
  background: #162229dc;
  cursor: pointer;
}
button:disabled { opacity: .55; cursor: default; }
button:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }
#status {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: max-content; max-width: 82vw;
  text-align: center;
  padding: 12px 18px;
  border-radius: 12px;
  background: #162229ef;
}
#controls { display: none; position: absolute; inset: auto var(--right) var(--bottom) var(--left); justify-content: space-between; align-items: end; pointer-events: none; }
.steering { display: flex; gap: clamp(6px, 2vmin, 12px); }
.steering button, .pedals button { pointer-events: auto; touch-action: none; user-select: none; }
.steering button {
  width: clamp(62px, 20vmin, 100px);
  height: clamp(64px, 19vmin, 96px);
  padding: 0;
  font-size: clamp(1.85rem, 7vmin, 3rem);
  background: #162229a8;
}
.steering button[data-held] { background: #a52c3b; }
.pedals { display: flex; gap: 8px; }
.pedals button { min-height: 58px; }
#gas { background: #ba2637ed; }
.pedals button[data-held] { filter: brightness(1.35); }
@media (any-pointer: coarse) {
  #controls:not([hidden]) { display: flex; }
  .keyboard { display: none; }
}

#recovery {
  position: absolute;
  top: calc(var(--top) + 105px);
  left: 50%;
  transform: translateX(-50%);
  max-width: 92%;
  width: max-content;
  padding: 7px 12px;
  border-radius: 8px;
  background: #162229ef;
  color: #f5dfb8;
  font-size: .78rem;
  pointer-events: none;
}

#bonus, #race-test {
  position: absolute;
  top: calc(var(--top) + 58px);
  right: var(--right);
  padding: 6px 10px;
  border-radius: 8px;
  background: #162229ef;
  color: #ffd571;
  font-size: .76rem;
  pointer-events: none;
}
@media (max-width: 460px) {
  #bonus, #race-test { top: calc(var(--top) + 94px); }
  #recovery { top: calc(var(--top) + 126px); }
}

#activate-bonus {
  position: absolute;
  right: var(--right);
  bottom: var(--bottom);
  width: max-content;
  padding: 5px 8px;
  border-radius: 8px;
  background: #162229ef;
  color: #ffd571;
  font-size: .82rem;
  font-weight: 700;
  min-height: 44px;
  touch-action: manipulation;
}

#switch-map { display: inline-block; margin-top: 8px; color: #ffe4ac; font-size: .74rem; }

#speed { font-weight: 700; font-variant-numeric: tabular-nums; }
#laps:not([hidden]) { margin-left: 9px; }

.frozen-map #bonus { top: calc(var(--top) + 93px); }
@media (max-width: 460px) {
  .frozen-map #bonus { top: calc(var(--top) + 128px); }
  .frozen-map #recovery { top: calc(var(--top) + 160px); }
}

@media (any-pointer: coarse) { #activate-bonus { bottom: calc(var(--bottom) + 65px); } }

#wrong-way {
  position: absolute;
  top: calc(var(--top) + 145px);
  left: 50%;
  transform: translateX(-50%);
  color: #fff3da;
  background: #8c243a;
  border: 1px solid #ffc885;
  border-radius: 6px;
  padding: 7px 12px;
  font-weight: 700;
  letter-spacing: 0.03em;
  pointer-events: none;
  white-space: nowrap;
}

#race-countdown {
  position: absolute;
  inset: 36% 0 auto;
  text-align: center;
  font-size: clamp(3rem, 11vw, 6.5rem);
  font-weight: 900;
  color: #ffe091;
  text-shadow: 0 4px 16px #0f1722;
  pointer-events: none;
}
#race-results {
  position: absolute;
  z-index: 3;
  inset: 0;
  background: #101523c9;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
#race-results:not([hidden]) { display: flex; }
.result-card {
  width: min(100%, 430px);
  max-height: 100%;
  overflow: auto;
  border: 1px solid #8199b3;
  border-radius: 16px;
  background: #192635;
  padding: clamp(14px, 4vw, 26px);
}
.result-card h2 { margin: 0; font-size: 2rem; }
#race-reward { color: #ffd571; margin: 6px 0 20px; font-weight: 700; }
.result-card table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.result-card caption { color: #c6d6e0; text-align: left; margin-bottom: 8px; }
.result-card th, .result-card td {
  border-bottom: 1px solid #657a8f55;
  text-align: left;
  padding: 8px 4px;
}
.result-card th:last-child, .result-card td:last-child { text-align: right; }
.player-result { color: #ffd571; }
#race-retry { margin-top: 18px; width: 100%; background: #a52c3b; }

#landing {
  height: 100dvh;
  overflow: auto;
  background: #0c131d;
  color: #f4f8fc;
}
.landing-shell {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: clamp(18px, 4vw, 46px);
}
.landing-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: clamp(18px, 4vw, 34px);
}
.wordmark {
  margin: 0;
  font-size: clamp(1.05rem, 3.4vw, 1.65rem);
  font-style: italic;
  font-weight: 850;
  letter-spacing: -.06em;
}
.wordmark strong { color: #ff5465; }
.landing-balance {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 0;
  padding: 8px 11px;
  border: 1px solid #495265;
  border-radius: 8px;
  font-size: .85rem;
  font-variant-numeric: tabular-nums;
}
.coin-icon {
  width: 14px;
  height: 14px;
  border: 2px solid #ffe096;
  background: #c38a22;
  border-radius: 50%;
}
.landing-stage {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  min-height: min(660px, 76dvh);
  display: grid;
  align-items: center;
  border: 1px solid #455569;
  border-radius: 16px;
  background: #1a2a3b;
}
.landing-art {
  position: absolute;
  z-index: -1;
  inset: 0;
  background: linear-gradient(90deg, #121e2d 3%, #172735 33%, transparent 68%),
    url('./menu-blaze.jpg') right center / auto 100% no-repeat;
}
.landing-content { padding: clamp(22px, 6vw, 70px); }
#landing-title {
  font-size: clamp(3.75rem, 10vw, 8rem);
  line-height: .83;
  font-weight: 950;
  font-style: italic;
  text-transform: uppercase;
  letter-spacing: -.075em;
  text-shadow: 0 3px 20px #111c2caa;
}
.landing-content p { margin: 20px 0; color: #d3e2ef; font-weight: 650; }
.landing-actions { display: grid; width: min(252px, 100%); gap: 8px; }
.landing-actions a, .race-result-actions a {
  text-decoration: none;
  padding: 11px 15px;
  border-radius: 7px;
  color: inherit;
  font-weight: 650;
}
.landing-primary { display: flex; justify-content: space-between; background: #b8263b; }
.landing-primary:hover { background: #cf2e45; }
.landing-secondary { background: #25364a; border: 1px solid #54677c; }
.landing-practice { color: #d3e2ef; }
.landing-actions a:focus-visible, .race-result-actions a:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }
.menu-return { color: #ffe1a0; font-size: .75rem; margin-right: 10px; }
#race-clock { margin-left: 9px; color: #dde8f0; font-variant-numeric: tabular-nums; }
.race-result-actions { display: flex; gap: 10px; align-items: center; margin-top: 18px; }
.race-result-actions #race-retry { width: auto; margin: 0; flex: 1; }
.race-result-actions a { border: 1px solid #8199b3; }
@media (max-width: 640px) {
  .landing-stage { min-height: 0; padding-top: clamp(140px, 43vw, 260px); }
  .landing-art {
    background: linear-gradient(0deg, #172735 48%, transparent 84%),
      url('./menu-blaze.jpg') center top / 100% auto no-repeat;
  }
  .landing-content { align-self: end; }
  .landing-actions { width: 100%; }
}

#open-garage { background: #192635; }
#open-garage .coin-icon { margin-left: 5px; }
#garage-shop {
  width: min(520px, calc(100% - 24px));
  max-height: calc(100dvh - 24px);
  padding: clamp(16px, 4vw, 26px);
  border: 1px solid #6c849d;
  border-radius: 14px;
  background: #192635;
  color: #f4f8fc;
}
#garage-shop::backdrop { background: #09111cbd; }
.garage-head { display: flex; gap: 12px; align-items: start; justify-content: space-between; }
.garage-head h2, .garage-upgrade h3 { margin: 0; }
.garage-head p { color: #ffd571; margin: 7px 0 15px; }
.garage-upgrade { padding: 14px 0; border-top: 1px solid #657a8f69; }
.garage-upgrade p { margin: 6px 0; color: #d3e2ef; font-size: .9rem; }
.garage-upgrade button { margin-top: 6px; background: #a52c3b; }
#garage-feedback { margin: 0; color: #ffd571; }
#garage-profile-row:not([hidden]) { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
#garage-profile { font: inherit; color: inherit; background: #101c29; border: 1px solid #6c849d; padding: 5px; border-radius: 6px; }

#online-lobby {
  position: absolute;
  z-index: 2;
  left: 50%;
  top: 46%;
  transform: translate(-50%, -50%);
  width: min(390px, calc(100% - 32px));
  border: 1px solid #8199b3;
  border-radius: 14px;
  padding: 18px;
  background: #192635f5;
}
#online-lobby h2 { margin: 0 0 12px; }
#online-lobby label { display: block; margin-bottom: 5px; font-size: .9rem; }
#room-invite { width: 100%; padding: 8px; border: 1px solid #657a8f; border-radius: 6px; background: #101c29; color: inherit; }
#lobby-ready { width: 100%; background: #a52c3b; }


.telegram-app {
  --top: max(10px, env(safe-area-inset-top), var(--telegram-top, 0px));
  --left: max(12px, env(safe-area-inset-left), var(--telegram-left, 0px));
  --right: max(12px, env(safe-area-inset-right), var(--telegram-right, 0px));
  --bottom: max(12px, env(safe-area-inset-bottom), var(--telegram-bottom, 0px));
}
.telegram-app .landing-shell {
  padding-top: max(18px, var(--top));
  padding-left: max(18px, var(--left));
  padding-right: max(18px, var(--right));
  padding-bottom: max(18px, var(--bottom));
}
.telegram-app #yard header h1,
.telegram-app #yard .surface-readout,
.telegram-app #yard .keyboard,
.telegram-app #fps,
.telegram-app #bonus { display: none; }
.telegram-app #race-test { top: calc(var(--top) + 46px); background: transparent; text-shadow: 0 1px 5px #101523; }

.driving-page body { touch-action: manipulation; }
.driving-page .steering { touch-action: none; }


@media (max-width: 430px) {
  #controls { gap: 8px; }
  .steering button { width: clamp(58px, 17vw, 72px); }
  .pedals { gap: 6px; }
  .pedals button { padding-inline: 8px; font-size: .83rem; }
  #brake { max-width: 88px; }
}

#copy-invite { width: 100%; }
#room-invite:not([hidden]) { margin-top: 8px; }
#invite-feedback { margin: 8px 0 0; font-size: .85rem; }

#career-dialog {
  width: min(520px, calc(100% - 24px));
  max-height: calc(100dvh - 24px);
  overflow: auto;
  padding: clamp(16px, 4vw, 26px);
  border: 1px solid #6c849d;
  border-radius: 14px;
  background: #192635;
  color: #f4f8fc;
}
#career-dialog::backdrop { background: #09111cbd; }
.career-track { display: block; padding: 10px 0; color: #ffdb91; }
.career-car { border-top: 1px solid #657a8f69; padding: 10px 0; }
.career-car p { margin: 4px 0 7px; }
.career-car button { margin: 0 6px 6px 0; }
#career-open { border: 0; border-radius: 7px; padding: 11px 15px; font-weight: 750; }
#switch-map[hidden] { display: none; }
