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

:root {
  color-scheme: dark;
  --line: rgba(255,255,255,0.08);
  --text: #eef2f5;
  --muted: #8b95a0;
}

html,
body {
  width: 100%;
  min-height: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(circle at 50% 20%, rgba(111, 134, 164, 0.18), transparent 36%),
    linear-gradient(180deg, #11161d 0%, #090c10 100%);
  color: var(--text);
  font-family:
    Inter,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    Roboto,
    Helvetica,
    Arial,
    sans-serif;
  overscroll-behavior: none;
}

button {
  font: inherit;
}

.page {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.game-card {
  width: min(1080px, 100%);
  overflow: hidden;
  border-radius: 20px;
  border: 1px solid var(--line);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.02), rgba(255,255,255,0)),
    rgba(20,25,32,0.96);
  box-shadow: 0 30px 90px rgba(0,0,0,0.42);
}

.topbar {
  min-height: 54px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  color: #adb5bf;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.74rem;
  letter-spacing: 0.12em;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #6c7683;
  box-shadow: 0 0 0 4px rgba(255,255,255,0.03);
}

.stats {
  margin-left: auto;
  display: flex;
  gap: 18px;
  color: #78828f;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.7rem;
  letter-spacing: 0.06em;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.stats strong {
  color: #d8dde3;
  font-weight: 700;
}

.canvas-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  background: #0d1219;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 7;
  background: #92d8ff;
}

.start-overlay,
.game-over-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 10px;
  background: rgba(8, 11, 14, 0.72);
  backdrop-filter: blur(4px);
}

.start-overlay[hidden],
.game-over-overlay[hidden] {
  display: none;
}

.overlay-box {
  width: min(920px, calc(100% - 8px));
  padding: 14px 16px 15px;
  border-radius: 18px;
  border: 1px solid rgba(255,255,255,0.08);
  background: rgba(20,25,32,0.97);
  box-shadow: 0 22px 60px rgba(0,0,0,0.42);
  text-align: center;
  overflow: visible;
}

.overlay-box.compact {
  width: min(420px, 100%);
}

.overlay-box h1,
.overlay-box h2 {
  font-size: 1.42rem;
  line-height: 1.1;
}

.subtitle {
  margin-top: 6px;
  color: var(--muted);
  line-height: 1.35;
  font-size: 0.88rem;
}

.car-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 9px;
  margin-top: 10px;
}

.car-card {
  appearance: none;
  min-width: 0;
  border: 1px solid rgba(255,255,255,0.09);
  border-radius: 13px;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.08), rgba(255,255,255,0.02)),
    #1a2028;
  padding: 7px 7px 6px;
  color: var(--text);
  cursor: pointer;
  transition:
    border-color 130ms ease,
    transform 130ms ease,
    background 130ms ease;
}

.car-card:hover {
  border-color: rgba(255,255,255,0.20);
}

.car-card.selected {
  border-color: rgba(245, 205, 74, 0.75);
  background:
    linear-gradient(180deg, rgba(245,205,74,0.13), rgba(255,255,255,0.02)),
    #1a2028;
}

.car-card img {
  width: 100%;
  height: 68px;
  object-fit: contain;
  display: block;
}

.car-title {
  display: block;
  margin-top: 4px;
  font-size: 0.78rem;
  line-height: 1.15;
  font-weight: 700;
}

.rules {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 24px;
  row-gap: 2px;
  text-align: left;
  margin: 8px auto 0;
  width: min(780px, 100%);
  color: #b4bcc6;
  line-height: 1.3;
  font-size: 0.76rem;
  padding-left: 20px;
}

.primary-button,
.secondary-button,
.small-button,
.mobile-lane-button {
  appearance: none;
  border-radius: 12px;
  cursor: pointer;
  transition:
    transform 120ms ease,
    border-color 120ms ease,
    background 120ms ease;
}

.primary-button {
  margin-top: 9px;
  min-width: 190px;
  min-height: 40px;
  padding: 9px 22px;
  border: 1px solid rgba(255,255,255,0.16);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.10), rgba(255,255,255,0.03)),
    #1c222b;
  color: #f4f7fa;
  font-weight: 700;
}

.primary-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.secondary-button,
.small-button {
  min-height: 42px;
  padding: 0 14px;
  border: 1px solid rgba(255,255,255,0.10);
  background: rgba(255,255,255,0.04);
  color: #c7ced6;
}

.overlay-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 14px;
}

.results {
  margin-top: 12px;
  color: #c0c7cf;
  line-height: 1.45;
}

.game-help {
  min-height: 58px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-top: 1px solid var(--line);
}

.desktop-help {
  color: #8e97a2;
  font-size: 0.82rem;
}

kbd {
  padding: 3px 7px;
  border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.11);
  background: rgba(255,255,255,0.05);
  color: #f3f6f8;
}

.mobile-lane-buttons {
  display: none;
}

.mobile-lane-button {
  min-height: 52px;
  padding: 0 14px;
  border: 1px solid rgba(255,255,255,0.14);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.12), rgba(255,255,255,0.04)),
    #1c222b;
  color: #f3f6f8;
  font-weight: 800;
}

.game-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
  align-items: center;
}


/* ---------- Mobile adaptation ---------- */

.mobile-help {
  display: none;
  color: #8e97a2;
  font-size: 0.76rem;
}

canvas {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.mobile-lane-button,
.small-button,
.primary-button,
.secondary-button,
.car-card {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Tablets and phones */
@media (max-width: 900px) {
  html,
  body {
    min-height: 100dvh;
  }

  body {
    overflow-x: hidden;
  }

  .page {
    min-height: 100dvh;
    padding:
      max(6px, env(safe-area-inset-top))
      max(6px, env(safe-area-inset-right))
      max(6px, env(safe-area-inset-bottom))
      max(6px, env(safe-area-inset-left));
    place-items: stretch;
  }

  .game-card {
    width: 100%;
    min-height: calc(
      100dvh
      - max(6px, env(safe-area-inset-top))
      - max(6px, env(safe-area-inset-bottom))
    );
    border-radius: 14px;
    display: flex;
    flex-direction: column;
  }

  .topbar {
    min-height: auto;
    padding: 8px 10px;
    gap: 7px;
    align-items: stretch;
    flex-direction: column;
  }

  .brand {
    font-size: 0.62rem;
    letter-spacing: 0.09em;
  }

  .status-dot {
    width: 7px;
    height: 7px;
  }

  .stats {
    margin-left: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 5px 12px;
    font-size: 0.62rem;
    line-height: 1.25;
  }

  .stats > span {
    min-width: 0;
    white-space: nowrap;
  }

  .stats strong {
    overflow: hidden;
    text-overflow: ellipsis;
    vertical-align: bottom;
  }

  .canvas-wrap {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
  }

  .desktop-help {
    display: none;
  }

  .mobile-help {
    display: block;
  }

  .game-help {
    flex: 0 0 auto;
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
    min-height: 0;
    padding:
      8px
      max(10px, env(safe-area-inset-right))
      max(10px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
  }

  .mobile-lane-buttons {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    width: 100%;
  }

  .mobile-lane-button {
    min-height: 56px;
    padding: 0 10px;
    font-size: 0.9rem;
    font-weight: 800;
  }

  .game-actions {
    margin-left: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    width: 100%;
  }

  .game-actions .small-button {
    min-height: 42px;
    width: 100%;
  }

  /* Mobile menu uses the full screen instead of being constrained by canvas height. */
  .start-overlay,
  .game-over-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    padding:
      max(10px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(10px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
    background: rgba(8, 11, 14, 0.90);
  }

  .overlay-box {
    width: min(620px, 100%);
    max-height: none;
    overflow: visible;
    padding: 14px 12px 15px;
    border-radius: 16px;
  }

  .overlay-box h1,
  .overlay-box h2 {
    font-size: 1.22rem;
  }

  .subtitle {
    margin-top: 5px;
    font-size: 0.76rem;
    line-height: 1.3;
  }

  .car-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 9px;
  }

  .car-card {
    padding: 7px 6px 6px;
    border-radius: 11px;
  }

  .car-card img {
    height: 60px;
  }

  .car-title {
    margin-top: 3px;
    font-size: 0.72rem;
  }

  .rules {
    grid-template-columns: 1fr;
    gap: 2px;
    width: 100%;
    margin-top: 8px;
    padding-left: 18px;
    font-size: 0.68rem;
    line-height: 1.25;
  }

  .primary-button {
    margin-top: 9px;
    min-height: 44px;
    min-width: min(220px, 100%);
    padding: 8px 18px;
  }
}


/* Portrait phones are intentionally blocked: the game is designed for landscape. */
.orientation-lock {
  display: none;
}

@media (max-width: 900px) and (orientation: portrait) {
  body {
    overflow: hidden;
  }

  .orientation-lock {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: grid;
    place-items: center;
    padding:
      max(24px, env(safe-area-inset-top))
      max(24px, env(safe-area-inset-right))
      max(24px, env(safe-area-inset-bottom))
      max(24px, env(safe-area-inset-left));
    background:
      radial-gradient(circle at 50% 35%, rgba(94,106,128,0.18), transparent 38%),
      #090c10;
    color: #eef2f5;
    text-align: center;
  }

  .orientation-lock__content {
    width: min(360px, 100%);
  }

  .orientation-lock h2 {
    margin-top: 22px;
    font-size: 1.55rem;
  }

  .orientation-lock p {
    margin-top: 9px;
    color: #89939e;
    line-height: 1.45;
  }

  .phone-rotate-icon {
    position: relative;
    width: 138px;
    height: 118px;
    margin: 0 auto;
  }

  .phone-shape {
    position: absolute;
    left: 47px;
    top: 2px;
    width: 44px;
    height: 80px;
    border: 3px solid #d5dbe2;
    border-radius: 11px;
  }

  .rotate-arrow {
    position: absolute;
    left: 12px;
    bottom: 0;
    width: 110px;
    height: 50px;
    color: #7f8995;
    font-size: 4.8rem;
    line-height: 0.62;
    transform: rotate(-18deg);
  }
}

/*
  Landscape mobile:
  canvas-wrap gets all remaining vertical space.
  The canvas itself fills that box in both dimensions.
  JS adjusts the logical world width to the real aspect ratio,
  so there is no stretching, cropping or empty blue area.
*/
@media (orientation: landscape) and (max-height: 650px) {
  html,
  body {
    width: 100%;
    height: 100%;
    min-height: 100%;
    overflow: hidden;
  }

  .page {
    width: 100%;
    height: 100dvh;
    min-height: 0;
    padding:
      max(2px, env(safe-area-inset-top))
      max(3px, env(safe-area-inset-right))
      max(2px, env(safe-area-inset-bottom))
      max(3px, env(safe-area-inset-left));
    display: block;
  }

  .game-card {
    width: 100%;
    height: calc(
      100dvh
      - max(2px, env(safe-area-inset-top))
      - max(2px, env(safe-area-inset-bottom))
    );
    min-height: 0;
    display: flex;
    flex-direction: column;
    border-radius: 8px;
  }

  .topbar {
    flex: 0 0 auto;
    min-height: 36px;
    display: grid;
    grid-template-columns: auto 1fr;
    align-items: center;
    gap: 8px;
    padding: 4px 8px;
  }

  .brand {
    font-size: 0.58rem;
  }

  .stats {
    margin-left: 0;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 10px;
    font-size: 0.54rem;
    white-space: nowrap;
  }

  .canvas-wrap {
    flex: 1 1 auto;
    width: 100%;
    min-height: 0;
    height: auto;
    overflow: hidden;
    display: block;
    background: #92d8ff;
  }

  .canvas-wrap canvas {
    display: block;
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    aspect-ratio: auto;
    object-fit: fill;
    transform: none;
  }

  .game-help {
    flex: 0 0 auto;
    min-height: 48px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 6px;
    padding:
      4px
      max(6px, env(safe-area-inset-right))
      max(4px, env(safe-area-inset-bottom))
      max(6px, env(safe-area-inset-left));
  }

  .desktop-help,
  .mobile-help {
    display: none;
  }

  .mobile-lane-buttons {
    display: grid;
    grid-template-columns: repeat(2, minmax(130px, 1fr));
    gap: 6px;
    width: 100%;
  }

  .mobile-lane-button {
    min-height: 40px;
    padding: 0 10px;
    font-size: 0.72rem;
  }

  .game-actions {
    margin-left: 0;
    display: grid;
    grid-template-columns: repeat(2, auto);
    gap: 6px;
    width: auto;
  }

  .game-actions .small-button {
    min-height: 38px;
    width: auto;
    padding: 0 12px;
  }

  /* Start screen remains fully visible without scrolling. */
  .start-overlay,
  .game-over-overlay {
    position: fixed;
    inset: 0;
    z-index: 200;
  }

  .start-overlay .overlay-box {
    width: min(930px, calc(100% - 16px));
    padding: 7px 9px 8px;
  }

  .start-overlay .overlay-box h1 {
    font-size: 1rem;
  }

  .start-overlay .subtitle {
    margin-top: 2px;
    font-size: 0.61rem;
    line-height: 1.12;
  }

  .start-overlay .car-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    margin-top: 5px;
  }

  .start-overlay .car-card {
    padding: 4px;
  }

  .start-overlay .car-card img {
    height: 42px;
  }

  .start-overlay .car-title {
    margin-top: 2px;
    font-size: 0.57rem;
  }

  .start-overlay .rules {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 3px 9px;
    margin-top: 5px;
    padding-left: 14px;
    font-size: 0.52rem;
    line-height: 1.08;
  }

  .start-overlay .primary-button {
    margin-top: 5px;
    min-height: 30px;
    padding: 4px 16px;
    font-size: 0.68rem;
  }
}


/* ---------- Lives ---------- */

.lives-hearts {
  display: inline-flex;
  gap: 3px;
  align-items: center;
  font-size: 1rem;
  letter-spacing: 0;
}

.life-heart {
  display: inline-block;
  line-height: 1;
  transition:
    transform 180ms ease,
    opacity 180ms ease,
    filter 180ms ease;
}

.life-heart.alive {
  color: #ff6470;
  text-shadow: 0 0 8px rgba(255, 80, 95, 0.32);
}

.life-heart.lost {
  color: #4f5863;
  opacity: 0.48;
  filter: grayscale(1);
}

.life-heart.just-lost {
  animation: heartBreak 360ms ease;
}

@keyframes heartBreak {
  0% {
    transform: scale(1);
  }
  32% {
    transform: scale(1.45) rotate(-8deg);
  }
  58% {
    transform: scale(0.72) rotate(7deg);
  }
  100% {
    transform: scale(1);
  }
}
