:root {
  color-scheme: dark;
  --ink: #f7fbff;
  --muted: #b6c8d2;
  --panel: rgba(7, 17, 24, 0.74);
  --panel-strong: rgba(8, 22, 31, 0.9);
  --line: rgba(255, 255, 255, 0.18);
  --gold: #ffd157;
  --mint: #78f0d6;
  --coral: #ff736a;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #081117;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--ink);
  font: inherit;
  font-weight: 850;
  cursor: pointer;
  touch-action: manipulation;
}

button:active {
  transform: translateY(1px);
}

.app,
#scene {
  width: 100%;
  height: 100%;
}

.app::after {
  position: fixed;
  inset: 0;
  z-index: 12;
  pointer-events: none;
  background:
    radial-gradient(circle at center, transparent 38%, rgba(255, 31, 31, 0.24) 72%),
    rgba(255, 31, 31, 0.14);
  opacity: 0;
  transition: opacity 0.18s ease;
  content: "";
}

.app.damage-hit::after {
  opacity: 1;
  animation: damagePulse 0.46s ease-out;
}

.app.shield-hit::after {
  background:
    radial-gradient(circle at center, transparent 38%, rgba(120, 240, 214, 0.28) 72%),
    rgba(120, 240, 214, 0.14);
  opacity: 1;
  animation: shieldPulse 0.58s ease-out;
}

#scene {
  display: block;
  touch-action: none;
}

.showroom {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto 1fr auto auto auto auto;
  align-items: start;
  pointer-events: none;
  padding: max(18px, env(safe-area-inset-top)) clamp(14px, 4vw, 42px) max(18px, env(safe-area-inset-bottom));
}

.showroom-head {
  max-width: 520px;
  pointer-events: auto;
}

.showroom-head p,
.finish-copy p {
  margin: 0 0 5px;
  color: var(--gold);
  font-size: 13px;
  font-weight: 950;
}

h1,
h2 {
  margin: 0;
  line-height: 0.96;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(36px, 8vw, 92px);
  text-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

.title-art {
  align-self: end;
  justify-self: end;
  width: min(38vw, 210px);
  max-height: 36vh;
  border: 1px solid rgba(255, 209, 87, 0.38);
  border-radius: 8px;
  object-fit: cover;
  box-shadow: 0 18px 52px rgba(0, 0, 0, 0.44);
  pointer-events: none;
}

.showroom-copy {
  align-self: end;
  justify-self: start;
  display: grid;
  gap: 3px;
  min-width: min(320px, 100%);
  max-width: 430px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  backdrop-filter: blur(11px);
  pointer-events: auto;
}

.showroom-copy strong {
  font-size: clamp(22px, 3vw, 36px);
  line-height: 1;
}

.showroom-copy span,
.showroom-hint,
.finish-copy span {
  color: var(--muted);
  font-weight: 750;
}

.car-choices {
  display: flex;
  align-items: stretch;
  gap: 8px;
  max-width: min(100%, 640px);
  overflow-x: auto;
  padding-top: 10px;
  pointer-events: auto;
  scrollbar-width: none;
}

.car-choices::-webkit-scrollbar {
  display: none;
}

.paint-choices {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  max-width: min(100%, 640px);
  padding-top: 8px;
  pointer-events: auto;
}

.paint-choice {
  display: grid;
  grid-template-columns: 22px auto;
  align-items: center;
  gap: 7px;
  min-height: 38px;
  padding: 6px 10px;
  font-size: 12px;
}

.paint-choice i {
  width: 22px;
  height: 22px;
  border: 2px solid rgba(255, 255, 255, 0.72);
  border-radius: 50%;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.34);
}

.paint-choice.selected {
  border-color: rgba(255, 209, 87, 0.82);
  background: rgba(42, 30, 10, 0.88);
}

.car-choice {
  display: grid;
  gap: 3px;
  flex: 0 0 min(170px, 42vw);
  min-height: 62px;
  padding: 10px 12px;
  text-align: left;
}

.car-choice strong {
  overflow-wrap: anywhere;
}

.car-choice span {
  color: var(--muted);
  font-size: 12px;
}

.car-choice.selected {
  border-color: rgba(255, 209, 87, 0.78);
  background: rgba(42, 30, 10, 0.88);
  box-shadow: inset 0 0 0 1px rgba(255, 209, 87, 0.28);
}

.stage-choices {
  display: flex;
  flex-wrap: wrap; /* 横スクロールだと6枚目(GALAXY)に気付けない端末があるため全カード常時表示 */
  align-items: stretch;
  gap: 8px;
  max-width: min(100%, 640px);
  padding-top: 8px;
  pointer-events: auto;
}

.stage-choice {
  display: grid;
  grid-template-columns: 38px auto;
  gap: 8px;
  align-items: center;
  flex: 0 0 calc((100% - 16px) / 3);
  min-width: 0;
  padding: 7px 9px;
  text-align: left;
}

.stage-choice svg {
  width: 38px;
  height: 38px;
}

.stage-choice svg path {
  fill: none;
  stroke: rgba(247, 251, 255, 0.78);
  stroke-width: 7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.stage-choice.selected {
  border-color: rgba(255, 209, 87, 0.78);
  background: rgba(42, 30, 10, 0.88);
  box-shadow: inset 0 0 0 1px rgba(255, 209, 87, 0.28);
}

.stage-choice.selected svg path {
  stroke: var(--gold);
}

.stage-copy {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.stage-copy strong {
  overflow: hidden;
  font-size: 12px;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.stage-copy span {
  color: var(--muted);
  font-size: 11px;
}

.stage-copy em {
  color: var(--gold);
  font-size: 10px;
  font-style: normal;
  font-weight: 800;
}

.minimap {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  z-index: 8;
  width: 132px;
  height: 132px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(7, 17, 24, 0.55);
  backdrop-filter: blur(8px);
  pointer-events: none;
}

.corner-sign {
  position: fixed;
  top: clamp(120px, 26vh, 230px);
  left: 50%;
  z-index: 7;
  transform: translateX(-50%);
  color: var(--gold);
  font-size: clamp(34px, 6vw, 56px);
  font-weight: 950;
  text-shadow: 0 4px 18px rgba(0, 0, 0, 0.55);
  pointer-events: none;
  animation: cornerBlink 0.6s ease-in-out infinite alternate;
}

@keyframes cornerBlink {
  from {
    opacity: 0.45;
  }
  to {
    opacity: 1;
  }
}

.showroom-actions {
  display: grid;
  grid-template-columns: 54px minmax(154px, 220px) 54px;
  gap: 8px;
  align-items: center;
  width: min(100%, 344px);
  padding-top: 10px;
  pointer-events: auto;
}

.showroom-actions button {
  min-height: 56px;
  font-size: 34px;
}

#prevCar {
  grid-column: 1;
}

#startRace {
  grid-column: 2;
  background: linear-gradient(180deg, #ffe070, #ef9b24);
  color: #221405;
  font-size: 22px;
}

#nextCar {
  grid-column: 3;
}

.two-d-link {
  display: grid;
  place-items: center;
  width: min(100%, 344px);
  min-height: 46px;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(6, 20, 29, 0.84);
  color: var(--ink);
  font-weight: 900;
  text-decoration: none;
  pointer-events: auto;
  backdrop-filter: blur(11px);
}

.showroom-hint {
  margin: 8px 0 0;
  width: fit-content;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(4, 12, 18, 0.52);
  font-size: 13px;
  pointer-events: auto;
}

.hud {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  z-index: 8;
  display: grid;
  grid-template-columns: repeat(5, minmax(62px, 100px));
  gap: 7px;
  transform: translateX(-50%);
}

.race-tools {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(5, 46px);
  gap: 7px;
}

.race-tools button {
  width: 46px;
  height: 46px;
  background: rgba(6, 17, 27, 0.82);
  font-size: 22px;
  backdrop-filter: blur(10px);
}

.race-tools button[aria-pressed="false"] {
  color: rgba(247, 251, 255, 0.42);
}

.hud div {
  display: grid;
  gap: 2px;
  min-height: 62px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  backdrop-filter: blur(10px);
}

.hud span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 850;
}

.hud strong {
  font-size: clamp(18px, 3vw, 30px);
  line-height: 1;
}

.hud.damage-hud div:last-child {
  border-color: rgba(255, 115, 106, 0.9);
  background: rgba(125, 20, 18, 0.72);
  animation: damageShake 0.42s ease-out;
}

.hud.shield-hud div:last-child {
  border-color: rgba(120, 240, 214, 0.9);
  background: rgba(15, 95, 94, 0.72);
  animation: shieldPulse 0.58s ease-out;
}

.touch-controls {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  left: max(12px, env(safe-area-inset-left));
  z-index: 9;
  display: grid;
  grid-template-columns: 1fr minmax(126px, 1.35fr) 1fr;
  gap: 9px;
  max-width: 560px;
  margin: 0 auto;
}

.touch-controls button {
  min-height: clamp(58px, 12vh, 92px);
  background: rgba(6, 17, 27, 0.82);
  font-size: clamp(30px, 6vw, 48px);
  backdrop-filter: blur(10px);
  touch-action: none;
  -webkit-touch-callout: none;
}

#boost {
  background: linear-gradient(180deg, rgba(120, 240, 214, 0.96), rgba(47, 152, 189, 0.92));
  color: #06161b;
  font-size: clamp(19px, 4vw, 32px);
}

.race-overlay {
  position: fixed;
  inset: 0;
  z-index: 11;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(3, 9, 14, 0.62);
}

.finish-copy {
  display: grid;
  gap: 11px;
  width: min(100%, 460px);
  padding: clamp(18px, 4vw, 34px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(6, 20, 29, 0.88);
  text-align: center;
  backdrop-filter: blur(16px);
}

.finish-video,
.finish-image {
  width: 100%;
  height: clamp(220px, 54vh, 520px);
  border: 1px solid rgba(255, 209, 87, 0.54);
  border-radius: 8px;
  background: rgba(2, 8, 12, 0.86);
  object-fit: contain;
  box-shadow: 0 16px 42px rgba(0, 0, 0, 0.42);
}

.finish-video {
  object-position: center center;
}

.finish-image {
  object-fit: cover;
  object-position: center 24%;
}

.finish-copy h2 {
  font-size: clamp(34px, 7vw, 72px);
}

.finish-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.finish-actions button {
  min-height: 52px;
}

#retry {
  background: linear-gradient(180deg, #ffe070, #ef9b24);
  color: #241506;
}

.hidden {
  display: none !important;
}

@media (min-width: 721px) {
  .showroom {
    grid-template-columns: minmax(390px, 640px) minmax(240px, 1fr);
    grid-template-rows: auto 1fr auto auto auto auto;
    column-gap: clamp(18px, 4vw, 60px);
    align-items: end;
  }

  .showroom-head,
  .showroom-copy,
  .car-choices,
  .paint-choices,
  .stage-choices,
  .showroom-actions,
  .two-d-link,
  .showroom-hint {
    grid-column: 1;
    z-index: 3;
  }

  .showroom-head {
    align-self: start;
  }

  .title-art {
    grid-column: 2;
    grid-row: 1 / -1;
    align-self: center;
    justify-self: end;
    width: min(32vw, 280px);
    max-height: min(58vh, 520px);
  }

  .showroom-copy {
    margin-top: 12px;
  }

  .car-choices,
  .paint-choices,
  .stage-choices {
    max-width: 100%;
  }
}

@media (max-width: 720px) {
  .showroom {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    gap: clamp(6px, 1.4vh, 8px);
    padding: max(14px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
    background:
      linear-gradient(180deg, rgba(5, 13, 21, 0.2), rgba(5, 13, 21, 0.2) 38%, rgba(5, 13, 21, 0.82) 62%, rgba(5, 13, 21, 0.95)),
      linear-gradient(90deg, rgba(5, 13, 21, 0.66), transparent 54%);
    overflow: hidden;
  }

  .showroom-head {
    position: absolute;
    top: max(14px, env(safe-area-inset-top));
    left: 12px;
    z-index: 2;
    max-width: min(58vw, 270px);
  }

  h1 {
    max-width: 7.4ch;
    font-size: clamp(34px, 10.5vw, 54px);
  }

  .title-art {
    position: absolute;
    top: max(118px, calc(env(safe-area-inset-top) + 94px));
    right: 12px;
    left: auto;
    z-index: 2;
    width: min(28vw, 118px);
    max-height: 18vh;
    opacity: 0.92;
  }

  .showroom-copy {
    display: none;
  }

  .car-choices {
    z-index: 3;
    width: 100%;
    max-width: none;
    padding-top: 0;
  }

  .car-choice {
    flex-basis: min(39vw, 154px);
    min-height: 46px;
    padding: 8px 10px;
  }

  .car-choice span {
    display: none;
  }

  .paint-choices {
    z-index: 3;
    gap: 6px;
    width: 100%;
    max-width: none;
    padding-top: 0;
  }

  .paint-choice {
    grid-template-columns: 18px auto;
    min-height: 32px;
    padding: 4px 8px;
    font-size: 11px;
  }

  .paint-choice i {
    width: 18px;
    height: 18px;
  }

  .stage-choices {
    z-index: 3;
    gap: 5px;
    width: 100%;
    max-width: none;
    padding-top: 0;
  }

  .stage-choice {
    flex-basis: calc((100% - 5px) / 2);
    grid-template-columns: 26px auto;
    gap: 6px;
    padding: 4px 7px;
  }

  .stage-choice svg {
    width: 26px;
    height: 26px;
  }

  .stage-copy strong {
    font-size: 10.5px;
  }

  .stage-copy span {
    display: none;
  }

  .stage-copy em {
    font-size: 9px;
  }

  .showroom-actions,
  .two-d-link,
  .showroom-hint {
    align-self: stretch;
    z-index: 3;
    width: calc(100vw - 24px);
    max-width: calc(100vw - 24px);
  }

  .showroom-actions {
    grid-template-columns: 1fr;
    gap: 0;
    padding-top: 0;
  }

  #prevCar,
  #nextCar {
    display: none;
  }

  #startRace {
    grid-column: 1;
  }

  .showroom-actions button {
    width: 100%;
    min-width: 0;
    min-height: 50px;
    overflow: hidden;
  }

  .two-d-link {
    margin-top: 0;
    min-height: 42px;
  }

  .showroom-hint {
    margin: 0;
    font-size: 12px;
  }

  .hud {
    top: max(64px, calc(env(safe-area-inset-top) + 52px));
    width: calc(100% - 16px);
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }

  .minimap {
    top: auto;
    left: 8px;
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + clamp(56px, 10vh, 76px) + 16px);
    width: 92px;
    height: 92px;
  }

  .hud div {
    min-height: 44px;
    padding: 6px 8px;
  }

  .hud span {
    font-size: 10px;
  }

  .hud strong {
    font-size: clamp(17px, 5.2vw, 22px);
  }

  .race-tools {
    top: max(10px, env(safe-area-inset-top));
    left: 50%;
    right: auto;
    grid-template-columns: repeat(5, 40px);
    gap: 5px;
    transform: translateX(-50%);
  }

  .race-tools button {
    width: 40px;
    height: 40px;
    font-size: 19px;
  }

  .touch-controls {
    gap: 7px;
  }

  .touch-controls button {
    min-height: clamp(56px, 10vh, 76px);
  }

  .finish-copy {
    width: min(100%, 390px);
    max-height: calc(100svh - 28px);
    padding: 14px;
    overflow: auto;
  }

  .finish-video,
  .finish-image {
    height: clamp(190px, 46svh, 430px);
  }

  .finish-copy h2 {
    font-size: clamp(30px, 9vw, 44px);
  }
}

/* 380px以下：‹›ボタンは非表示(≤720pxルール)なのに3列グリッドが残り
   3D Driveが46px幅に潰れていた。スタートボタンを全幅に */
@media (max-width: 380px) {
  #startRace {
    grid-column: 1 / -1;
  }
}

@media (max-width: 380px), (max-height: 680px) {
  .showroom-head p,
  .showroom-hint {
    display: none;
  }

  h1 {
    font-size: clamp(28px, 9.2vw, 44px);
  }

  .title-art {
    width: min(24vw, 92px);
  }

  .showroom-actions {
    grid-template-columns: 46px 1fr 46px;
  }

  .showroom-actions button,
  .two-d-link {
    min-height: 40px;
  }

  #startRace {
    font-size: 19px;
  }
}

@keyframes damagePulse {
  0%,
  100% {
    opacity: 0;
  }
  18%,
  54% {
    opacity: 1;
  }
}

@keyframes shieldPulse {
  0%,
  100% {
    opacity: 0;
  }
  28%,
  66% {
    opacity: 1;
  }
}

@keyframes damageShake {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-3px);
  }
  50% {
    transform: translateX(3px);
  }
  75% {
    transform: translateX(-2px);
  }
}
