.road-race-shell {
  --rr-safe-top: env(safe-area-inset-top, 0px);
  --rr-safe-bottom: env(safe-area-inset-bottom, 0px);
  --rr-safe-left: env(safe-area-inset-left, 0px);
  --rr-safe-right: env(safe-area-inset-right, 0px);
  margin: 0;
  padding: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: radial-gradient(ellipse at 20% 10%, #287d9c, transparent 65%), #102c46;
  color: #f8fafc;
  animation: none;
}

.road-race-shell::before,
.road-race-shell::after { content: none; }

.road-race-app,
.road-race-app * {
  box-sizing: border-box;
}

.road-race-app {
  position: relative;
  isolation: isolate;
  z-index: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  max-width: none;
  min-height: 0;
  height: 100vh;
  height: 100dvh;
  margin: 0;
  padding: 16px;
  overflow: hidden;
}

.road-race-stage-card {
  position: relative;
  width: min(100%, calc((100vh - 32px) * 0.75));
  width: min(100%, calc((100dvh - 32px) * 0.75));
  height: 100%;
  min-width: 0;
  overflow: hidden;
  border-radius: 24px;
  background: #18364c;
  box-shadow: 0 20px 60px rgb(0 15 30 / 35%), 0 0 0 1px rgb(255 255 255 / 20%);
}

.road-race-playfield {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: inherit;
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#roadRaceCanvas {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 100%;
  touch-action: none;
}

.road-race-topbar {
  position: absolute;
  z-index: 4;
  top: max(12px, var(--rr-safe-top));
  left: max(12px, var(--rr-safe-left));
  right: max(12px, var(--rr-safe-right));
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin: 0;
  padding: 0;
  pointer-events: none;
}

.road-race-topbar .back,
.road-race-stats .stat-pill {
  border: 1px solid rgb(255 255 255 / 40%);
  border-radius: 16px;
  background: rgb(14 40 64 / 86%);
  color: #fff;
  box-shadow: 0 3px 12px rgb(0 24 46 / 15%);
  font-size: clamp(0.8rem, 2.8vw, 0.95rem);
  line-height: 1.25;
  font-weight: 800;
}

.road-race-topbar .back {
  position: static;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  max-width: 42%;
  padding: 8px 12px;
  text-align: center;
  text-decoration: none;
  overflow-wrap: anywhere;
  pointer-events: auto;
  animation: none;
  transform: none;
}

.road-race-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
  min-width: 0;
  direction: inherit;
}

.road-race-stats .stat-pill {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 44px;
  padding: 8px 10px;
  white-space: nowrap;
}

.road-race-stats .stat-pill span { color: inherit; }
#roadRaceTime { font-variant-numeric: tabular-nums; min-width: 2ch; }
#roadRaceLives { font-size: 0.9rem; line-height: 1; letter-spacing: 1px; }

.road-race-rescue,
.road-race-loading {
  position: absolute;
  z-index: 6;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, var(--rr-safe-top)) max(16px, var(--rr-safe-right))
    calc(76px + var(--rr-safe-bottom)) max(16px, var(--rr-safe-left));
  background: rgb(8 26 46 / 58%);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  overscroll-behavior: contain;
}

.road-race-rescue[hidden],
.road-race-loading[hidden] { display: none; }

.road-race-rescue-panel,
.road-race-loading-card {
  width: min(100%, 520px);
  max-height: 100%;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: clamp(16px, 3vw, 26px);
  border: 2px solid rgb(255 255 255 / 85%);
  border-radius: 26px;
  background: linear-gradient(165deg, #fffcf0, #eef8ff);
  color: #123452;
  box-shadow: 0 16px 48px rgb(0 14 34 / 35%);
  text-align: center;
}

.road-race-rescue-prompt { line-height: 1.3; overflow-wrap: anywhere; }
.road-race-rescue-context { color: #46627c; font-size: clamp(1rem, 3vw, 1.2rem); font-weight: 700; }
.road-race-rescue-context:empty { display: none; }
.road-race-rescue-challenge { font-size: clamp(1.4rem, 5vw, 2.2rem); font-weight: 900; }
.road-race-rescue-context:not(:empty) + .road-race-rescue-challenge { margin-top: 10px; }

.road-race-rescue-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 22px;
}

.road-race-choice {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-width: 0;
  min-height: 64px;
  margin: 0;
  padding: 12px;
  border: 2px solid #a8d2e9;
  border-radius: 17px;
  background: #e1f1fc;
  color: #143c61;
  font: inherit;
  font-size: clamp(1rem, 3.4vw, 1.3rem);
  font-weight: 800;
  line-height: 1.3;
  text-align: center;
  overflow-wrap: anywhere;
  box-shadow: 0 4px 0 #b0d2e6;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 120ms ease, transform 120ms ease;
}

.road-race-choice:active:not(:disabled) { transform: translateY(2px); }
.road-race-choice:disabled { cursor: default; opacity: 0.8; }
.road-race-choice.is-correct { background: #dcfce7; border: 3px solid #187547; color: #125032; box-shadow: 0 4px 0 #187547; }
.road-race-choice.is-wrong { background: #fff0e9; border: 3px dashed #ad3b23; color: #802614; box-shadow: 0 4px 0 #ad3b23; }
.road-race-choice.is-correct::after { content: "✓"; font-size: 1.4em; flex-shrink: 0; }
.road-race-choice.is-wrong::after { content: "×"; font-size: 1.4em; flex-shrink: 0; }
.road-race-choice.is-correct:disabled,
.road-race-choice.is-wrong:disabled { opacity: 1; }

.road-race-loading-card { width: min(100%, 320px); display: grid; justify-items: center; gap: 18px; font-weight: 800; }
.road-race-loader {
  width: 48px;
  height: 48px;
  border: 5px solid #cae3f0;
  border-top-color: #176b9c;
  border-right-color: #ec9b25;
  border-radius: 50%;
  animation: road-race-spin 900ms linear infinite;
}

@keyframes road-race-spin { to { transform: rotate(360deg); } }

.road-race-app :focus-visible,
body.road-race-shell #pvpSearchBtn:focus-visible,
body.road-race-shell #pvpCancelBtn:focus-visible {
  outline: 3px solid #ffc94c;
  outline-offset: 3px;
}
#roadRaceCanvas:focus-visible { outline-offset: -5px; }

/* Shared presence appends controls to body, outside this isolated game layer. */
body.pvp-searching .road-race-app {
  filter: grayscale(1) brightness(0.58);
  pointer-events: none;
  user-select: none;
}
body.pvp-searching .road-race-app * { pointer-events: none; }
body.pvp-searching .road-race-app::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 50;
  background: rgb(155 169 180 / 25%);
  pointer-events: auto;
  cursor: wait;
}

body.road-race-shell #pvpSearchBtn,
body.road-race-shell #pvpCancelBtn,
body.road-race-shell #pvpHint {
  box-sizing: border-box;
  bottom: calc(12px + var(--rr-safe-bottom));
  z-index: 9999;
}
body.road-race-shell #pvpSearchBtn,
body.road-race-shell #pvpCancelBtn {
  min-width: 112px;
  min-height: 48px;
  max-width: calc((100vw - var(--rr-safe-left) - var(--rr-safe-right) - 36px) * 0.48);
  padding: 10px 14px;
  white-space: normal;
  overflow-wrap: anywhere;
  pointer-events: auto;
}
body.road-race-shell #pvpHint {
  max-width: calc((100vw - var(--rr-safe-left) - var(--rr-safe-right) - 36px) * 0.48);
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid rgb(255 255 255 / 35%);
  background: #163c59;
  opacity: 1;
  font-size: 0.82rem;
  line-height: 1.3;
  white-space: normal;
  overflow-wrap: anywhere;
  text-overflow: clip;
}
html body.road-race-shell #pvpHint { left: calc(12px + var(--rr-safe-left)); right: auto; }
html body.road-race-shell #pvpSearchBtn,
html body.road-race-shell #pvpCancelBtn { right: calc(12px + var(--rr-safe-right)); left: auto; }
html[dir="rtl"] body.road-race-shell #pvpHint { right: calc(12px + var(--rr-safe-right)); left: auto; }
html[dir="rtl"] body.road-race-shell #pvpSearchBtn,
html[dir="rtl"] body.road-race-shell #pvpCancelBtn { left: calc(12px + var(--rr-safe-left)); right: auto; }

@media (hover: hover) {
  .road-race-choice:hover:not(:disabled) { background: #cde9fb; }
  .road-race-topbar .back:hover { background: #235377; transform: none; }
}

@media (max-width: 760px), (max-height: 520px) {
  .road-race-app { padding: 0; }
  .road-race-stage-card { width: 100%; border-radius: 0; box-shadow: none; }
}

@media (max-width: 360px) {
  .road-race-topbar { gap: 6px; }
  .road-race-stats { gap: 4px; }
  .road-race-stats .stat-pill { padding: 7px; }
  .road-race-rescue-options { gap: 8px; }
  .road-race-choice { padding: 10px 8px; }
}

@media (max-height: 520px) and (min-width: 540px) {
  .road-race-rescue-panel { width: min(100%, 640px); padding: 14px; border-radius: 20px; }
  .road-race-rescue-context { font-size: 0.95rem; }
  .road-race-rescue-challenge { font-size: 1.35rem; }
  .road-race-rescue-options { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-top: 12px; }
  .road-race-choice { min-height: 52px; padding: 8px; font-size: 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  .road-race-app *,
  .road-race-app *::before,
  .road-race-app *::after { animation: none; transition: none; }
}
