/* ==========================================================
   ドラッグレース - style.css
   ========================================================== */

:root {
  --color-bg: #14141a;
  --color-stage-top: #262b3d;
  --color-accent: #ff3b3b;
  --color-accent-2: #ffd93d;
  --color-good: #2ecc71;
  --color-text: #f5f5fa;
  --color-text-muted: #9a9ab0;
  --color-border: #3a3a4a;
  --header-h: 56px;
  --ad-h: 64px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: "Segoe UI", "Hiragino Kaku Gothic ProN", "Yu Gothic", Roboto, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
}

/* ---------- ヘッダー ---------- */
.game-header {
  height: var(--header-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.8rem;
  background: linear-gradient(180deg, #1c1c26, var(--color-bg));
  border-bottom: 2px solid var(--color-border);
  z-index: 20;
}

.back-link {
  color: var(--color-text);
  text-decoration: none;
  font-size: 1.3rem;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.06);
}

.back-link:active {
  background: rgba(255, 255, 255, 0.15);
}

.game-title {
  font-size: 1rem;
  font-weight: 800;
  letter-spacing: 0.03em;
  margin: 0;
  color: var(--color-accent-2);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.4);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.06);
  color: var(--color-text);
  font-size: 1.1rem;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.15);
}

.icon-btn.is-muted {
  color: var(--color-text-muted);
}

/* ---------- レースステージ ---------- */
.race-stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background: linear-gradient(180deg, var(--color-stage-top), var(--color-bg) 80%);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.6rem 0.9rem 0.4rem;
}

/* ---------- 距離ゲージ ---------- */
.progress-row {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  width: min(90%, 380px);
  margin-bottom: 0.4rem;
}

.distance-readout {
  font-size: 0.7rem;
  font-weight: 800;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

.progress-bar {
  position: relative;
  width: 100%;
  height: 10px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  flex: 0 0 auto;
}

.progress-bar__fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--color-accent), var(--color-accent-2));
  transition: width 0.25s ease;
}

.progress-bar__flag {
  position: absolute;
  right: -14px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1.3rem;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.6));
}

/* ---------- 車の表示エリア ---------- */
.race-track {
  position: relative;
  /* 常に固有のスタッキングコンテキストを持たせる。これが無いと、子要素の z-index:-1
     （.race-bg-image）が親コンテナの外側までエスケープしてしまい、シフト時の
     transformアニメーション（is-punching）がスタッキングコンテキストを作る瞬間
     だけ背景が見える、という不具合になっていた。 */
  isolation: isolate;
  flex: 1 1 auto;
  min-height: 70px;
  width: 100%;
  overflow: hidden;
  border-radius: 14px;
  background-color: #14101c;
}

/* 背景情景（AI生成画像）だけを独立レイヤーにして、車やUIを巻き込まずに
   スピードに応じたズーム／モーションブラーをかけられるようにする */
.race-bg-image {
  position: absolute;
  inset: 0;
  z-index: -1;
  background-image:
    linear-gradient(180deg, rgba(20, 8, 30, 0.25), rgba(10, 5, 15, 0.55)),
    url("assets/bg-track-tile.png");
  /* 横スクロール用にミラータイル生成した画像をrepeat-xで敷き詰める。
     X位置はJSがスピードに応じて毎フレーム更新する（速いほど流れが速くなる）。 */
  background-size: cover, auto 100%;
  background-position: center, 0 center;
  background-repeat: no-repeat, repeat-x;
  transition: filter 0.2s ease;
  pointer-events: none;
}

/* 加速中は画面端が煽られるようなビネット（トンネルビジョン）が濃くなる */
.race-vignette {
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at center, transparent 45%, rgba(6, 4, 10, 0.85) 100%);
  opacity: 0.35;
  z-index: 6;
  pointer-events: none;
  transition: opacity 0.15s ease;
}

/* シフト成功の瞬間に画面全体がグッと詰まる「加速パンチ」演出 */
.race-track.is-punching {
  animation: shift-punch 0.22s ease-out;
}

@keyframes shift-punch {
  0% { transform: scale(1); }
  30% { transform: scale(1.045); }
  100% { transform: scale(1); }
}

/* ---------- 遠景（視差でゆっくり流れるネオン電柱のようなシルエット） ---------- */
.race-scenery {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 26%;
  height: 38%;
  background-image: repeating-linear-gradient(90deg,
    rgba(255, 90, 190, 0.35) 0 3px, transparent 3px 65px,
    rgba(80, 220, 255, 0.3) 65px 68px, transparent 68px 130px);
  opacity: 0.7;
  z-index: 0;
  pointer-events: none;
}

/* ---------- 路面（アスファルト＋流れるレーンライン＋ネオン発光） ---------- */
.race-road {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 6%;
  height: 22%;
  background-image:
    repeating-linear-gradient(90deg, var(--color-accent-2) 0 20px, transparent 20px 56px),
    linear-gradient(180deg, #2c2438, #14101c);
  background-size: 56px 5px, 100% 100%;
  background-repeat: repeat-x, no-repeat;
  /* レーンラインは路面の上端（=車のタイヤが接地する位置）ぴったりに描く。
     center寄せだと路面帯の中央（見た目のかなり下）に線が来てしまい、
     タイヤとレーンラインの間に不自然な隙間ができて「車が浮いて見える」
     原因になっていた。 */
  background-position: 0 top, 0 0;
  border-top: 2px solid rgba(255, 217, 61, 0.35);
  border-bottom: 2px solid rgba(0, 0, 0, 0.5);
  box-shadow: 0 -6px 22px rgba(255, 60, 130, 0.25);
  z-index: 1;
  pointer-events: none;
}

/* ---------- 車の足元で脈打つエンジングロー ---------- */
.race-glow {
  position: absolute;
  left: 50%;
  bottom: 22%;
  width: 60vw;
  max-width: 240px;
  height: 60vw;
  max-height: 240px;
  transform: translate(-50%, 35%);
  background: radial-gradient(circle, rgba(255, 140, 40, 0.55) 0%, rgba(255, 60, 130, 0.25) 45%, transparent 72%);
  opacity: 0.35;
  z-index: 1;
  pointer-events: none;
  transition: opacity 0.15s ease;
}

.race-car {
  position: absolute;
  left: 50%;
  bottom: 27%;
  transform: translateX(-50%);
  width: min(38vw, 150px);
  filter: drop-shadow(0 10px 14px rgba(0, 0, 0, 0.5));
  z-index: 2;
}

/* ---------- シフト成功時に弾けるスパーク ---------- */
.spark-layer {
  position: absolute;
  inset: 0;
  z-index: 7;
  pointer-events: none;
  overflow: visible;
}

.spark {
  position: absolute;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  left: 50%;
  bottom: 30%;
  transform: translate(-50%, 50%);
  animation: spark-burst 0.55s ease-out forwards;
}

@keyframes spark-burst {
  0% { opacity: 1; transform: translate(-50%, 50%) scale(1); }
  100% {
    opacity: 0;
    transform:
      translate(calc(-50% + var(--spark-x, 0px)), calc(50% + var(--spark-y, 0px)))
      scale(0.3);
  }
}

.race-car img {
  width: 100%;
  display: block;
}

/* 車ごとに素材の正面の向きがバラバラ（セダン・オフロードは元々右向き、
   それ以外は左向き）なので、右向きに揃えるための反転はJSが車種ごとに
   このクラスを付け外しして制御する（CSS側で一律には反転しない）。 */
.race-car img.is-flipped {
  transform: scaleX(-1);
}

.race-car.is-revving {
  animation: car-shake 0.09s linear infinite;
}

/* --shake-amp はギア段に応じてJSが更新し、高いギアほど振動が大きく＆速くなる */
@keyframes car-shake {
  0% { transform: translateX(-50%) translate(0, 0); }
  25% { transform: translateX(-50%) translate(calc(var(--shake-amp, 1) * -1.5px), calc(var(--shake-amp, 1) * 0.5px)); }
  50% { transform: translateX(-50%) translate(calc(var(--shake-amp, 1) * 1.5px), calc(var(--shake-amp, 1) * -0.5px)); }
  75% { transform: translateX(-50%) translate(calc(var(--shake-amp, 1) * -1px), calc(var(--shake-amp, 1) * 0.5px)); }
  100% { transform: translateX(-50%) translate(0, 0); }
}

/* ---------- スピードメーターHUD ---------- */
.speed-hud {
  position: absolute;
  right: 10px;
  bottom: 8px;
  z-index: 8;
  text-align: right;
  pointer-events: none;
  line-height: 1;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.7);
}

.speed-hud__value {
  display: block;
  font-size: 1.9rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: #fff;
  transition: color 0.2s ease;
}

.speed-hud__unit {
  font-size: 0.62rem;
  font-weight: 800;
  color: rgba(255, 255, 255, 0.65);
  letter-spacing: 0.06em;
}

.speed-hud.is-redline .speed-hud__value {
  color: var(--color-accent-2);
}

/* ---------- 経過タイムHUD（常時表示） ---------- */
.timer-hud {
  position: absolute;
  left: 10px;
  top: 8px;
  z-index: 8;
  text-align: left;
  pointer-events: none;
  line-height: 1;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.7);
}

.timer-hud__value {
  display: block;
  font-size: 1.9rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: #fff;
}

.timer-hud__unit {
  font-size: 0.62rem;
  font-weight: 800;
  color: rgba(255, 255, 255, 0.65);
  letter-spacing: 0.06em;
}

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3.4rem;
  font-weight: 900;
  color: var(--color-accent-2);
  text-shadow: 0 4px 14px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  opacity: 0;
  z-index: 10;
}

.countdown.is-visible {
  animation: countdown-pop 0.5s ease;
  opacity: 1;
}

@keyframes countdown-pop {
  0% { opacity: 0; transform: scale(0.5); }
  30% { opacity: 1; transform: scale(1.15); }
  100% { opacity: 1; transform: scale(1); }
}

.shift-feedback {
  position: absolute;
  top: 8%;
  left: 50%;
  font-size: 1.3rem;
  font-weight: 900;
  white-space: nowrap;
  transform: translate(-50%, 0);
  pointer-events: none;
  opacity: 0;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
  z-index: 8;
}

.shift-feedback.is-visible {
  animation: shift-float 0.9s ease-out forwards;
}

@keyframes shift-float {
  0% { opacity: 1; transform: translate(-50%, 0) scale(0.85); }
  20% { transform: translate(-50%, -6px) scale(1.15); }
  100% { opacity: 0; transform: translate(-50%, -34px) scale(1); }
}

.shift-feedback.tier-perfect { color: var(--color-good); }
.shift-feedback.tier-great { color: var(--color-accent-2); }
.shift-feedback.tier-ok { color: #ff9a3c; }
.shift-feedback.tier-miss { color: var(--color-accent); }

/* ---------- ギア・RPM表示 ---------- */
.gear-row {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  width: min(90%, 380px);
  margin-bottom: 0.3rem;
}

.gear-badge, .rpm-readout {
  font-size: 0.72rem;
  font-weight: 800;
  color: var(--color-text-muted);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 0.25rem 0.8rem;
}

/* ---------- タコメーター ---------- */
.tach-wrap {
  position: relative;
  width: min(74vw, 280px);
  height: min(37vw, 140px);
  flex: 0 0 auto;
  margin: 0.2rem auto 0.3rem;
  overflow: hidden;
  pointer-events: none;
}

.tach-circle {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 200%;
  border-radius: 50%;
  background: conic-gradient(from -90deg, #5a2020 0% 3%, #5a2020 3% 50%, transparent 50% 100%);
  box-shadow: inset 0 0 0 6px rgba(0, 0, 0, 0.3);
}

.tach-needle {
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 4px;
  height: 92%;
  background: linear-gradient(#fff, #ccc);
  border-radius: 3px;
  transform-origin: bottom center;
  transform: translateX(-50%) rotate(-90deg);
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.7);
  z-index: 3;
  transition: transform 0.03s linear;
}

.tach-pivot {
  position: absolute;
  left: 50%;
  bottom: -7px;
  width: 16px;
  height: 16px;
  background: #1c1c26;
  border: 2px solid #fff;
  border-radius: 50%;
  transform: translateX(-50%);
  z-index: 4;
}

/* ---------- SHIFTボタン ---------- */
.shift-btn {
  flex: 0 0 auto;
  width: min(90%, 380px);
  min-height: 78px;
  border: none;
  border-radius: 20px;
  background: linear-gradient(135deg, var(--color-accent), #ff8c3c);
  color: #fff;
  font-size: 1.5rem;
  font-weight: 900;
  letter-spacing: 0.05em;
  cursor: pointer;
  margin-bottom: 0.4rem;
  box-shadow: 0 10px 24px rgba(255, 59, 59, 0.35);
  touch-action: manipulation;
}

.shift-btn:active {
  filter: brightness(0.9);
  transform: scale(0.98);
}

/* ---------- オーバーレイ ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(6, 6, 10, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  padding: 1rem;
}

.overlay.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.overlay__box {
  text-align: center;
  padding: 1.6rem 2rem;
  background: #1c1c26;
  border: 2px solid var(--color-border);
  border-radius: 18px;
  max-width: 85%;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.55);
}

.overlay__box--wide {
  max-width: min(92%, 460px);
  max-height: 88%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.overlay__title {
  font-size: 1.5rem;
  font-weight: 900;
  margin: 0 0 0.5rem;
  color: var(--color-accent-2);
}

.overlay__desc {
  color: var(--color-text-muted);
  margin: 0 0 1.3rem;
  font-size: 0.9rem;
}

.overlay__result-emoji {
  font-size: 2.6rem;
  margin: 0 0 0.2rem;
}

.overlay__time {
  font-size: 2.1rem;
  font-weight: 900;
  color: #fff;
  margin: 0 0 0.3rem;
}

.overlay__btn {
  border: none;
  background: linear-gradient(135deg, var(--color-accent), #ff8c3c);
  color: #fff;
  font-weight: bold;
  font-size: 0.95rem;
  padding: 0.8rem 1.8rem;
  border-radius: 999px;
  cursor: pointer;
}

.overlay__btn:active {
  filter: brightness(0.9);
}

.overlay__btn--secondary {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--color-border);
  margin-top: 0.6rem;
  margin-left: 0.6rem;
}

/* ---------- ガイド画面 ---------- */
.guide-section {
  text-align: left;
  margin: 1rem 0;
}

.guide-section__title {
  font-size: 0.92rem;
  color: var(--color-accent-2);
  margin: 0 0 0.4rem;
}

.guide-section__text {
  font-size: 0.82rem;
  color: var(--color-text-muted);
  margin: 0;
  line-height: 1.6;
}

.guide-section__list {
  margin: 0;
  padding-left: 1.1rem;
  font-size: 0.8rem;
  color: var(--color-text-muted);
  line-height: 1.7;
}

.guide-section__list li {
  margin-bottom: 0.3rem;
}

/* ---------- 車選択画面 ---------- */
.car-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.8rem;
  margin: 1.2rem 0 1.4rem;
}

@media (min-width: 420px) {
  .car-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

.car-card {
  background: rgba(255, 255, 255, 0.04);
  border: 2px solid var(--color-border);
  border-radius: 14px;
  padding: 0.7rem 0.5rem;
  cursor: pointer;
  text-align: center;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.car-card.is-selected {
  border-color: var(--color-accent-2);
  background: rgba(255, 217, 61, 0.1);
}

.car-card__img {
  width: 68px;
  height: 68px;
  object-fit: contain;
  margin: 0 auto 0.4rem;
}

.car-card__name {
  font-size: 0.78rem;
  font-weight: bold;
  margin: 0 0 0.3rem;
}

.car-card__desc {
  font-size: 0.64rem;
  color: var(--color-text-muted);
  margin: 0 0 0.5rem;
  line-height: 1.4;
  min-height: 2.6em;
}

.car-card__stats {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.car-stat {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.6rem;
  color: var(--color-text-muted);
}

.car-stat__label {
  flex: 0 0 38px;
  text-align: left;
}

.car-stat__bar {
  flex: 1;
  height: 5px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.car-stat__bar-fill {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--color-accent), var(--color-accent-2));
  border-radius: 999px;
}

/* ---------- 広告スペース ---------- */
.ad-slot {
  flex: 0 0 var(--ad-h);
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 2px dashed var(--color-border);
  background: #100f16;
  color: var(--color-text-muted);
  font-size: 0.75rem;
  z-index: 20;
  overflow: hidden;
}

@media (max-height: 620px) {
  .race-track { min-height: 54px; }
  .tach-wrap { width: min(68vw, 220px); height: min(34vw, 110px); }
  .shift-btn { min-height: 64px; font-size: 1.25rem; }
  .ad-slot {
    --ad-h: 44px;
    font-size: 0.65rem;
  }
}
