/* ===== 全域重置 ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; background: #0a0e1a; }
body {
  font-family: 'Segoe UI', 'Microsoft JhengHei', system-ui, sans-serif;
  color: #fff;
  user-select: none;
}

#game-container {
  position: fixed; inset: 0;
}
#game-container canvas { display: block; }

.hidden { display: none !important; }

/* ===== HUD ===== */
#hud {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 10;
}

#info-panel {
  position: absolute; top: 20px; left: 20px;
  background: rgba(8, 14, 28, 0.72);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-left: 4px solid #00e5ff;
  border-radius: 12px;
  padding: 14px 18px;
  min-width: 170px;
  font-variant-numeric: tabular-nums;
}
.info-row {
  display: flex; justify-content: space-between; align-items: baseline;
  gap: 18px; padding: 3px 0;
}
.info-row .label {
  font-size: 12px; letter-spacing: 1px;
  color: #7e8db3; text-transform: uppercase;
}
.info-row span:last-child {
  font-size: 20px; font-weight: 700; color: #fff;
}

#right-panel {
  position: absolute; top: 20px; right: 20px;
  display: flex; flex-direction: column; align-items: flex-end; gap: 12px;
}
#position-display {
  background: rgba(8, 14, 28, 0.72);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-right: 4px solid #ffd000;
  border-radius: 12px;
  padding: 10px 18px;
  text-align: right;
  line-height: 1;
}
#position-display .big {
  font-size: 44px; font-weight: 900; color: #ffd000;
  text-shadow: 0 0 18px rgba(255, 208, 0, 0.5);
}
#position-display .suffix { font-size: 14px; color: #7e8db3; margin-left: 4px; }

#minimap {
  background: rgba(8, 14, 28, 0.72);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 12px;
}

#speedometer {
  position: absolute; bottom: 24px; left: 24px;
  background: rgba(8, 14, 28, 0.72);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  padding: 12px 22px;
  text-align: center;
  min-width: 120px;
}
#speed-value {
  font-size: 48px; font-weight: 900; line-height: 1;
  color: #fff; font-variant-numeric: tabular-nums;
}
#speed-unit { font-size: 12px; letter-spacing: 2px; color: #7e8db3; margin-top: 2px; }
#boost-bar {
  margin-top: 10px; height: 6px; width: 100%;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px; overflow: hidden;
}
#boost-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, #00e5ff, #ffd000);
  border-radius: 3px;
  transition: width 0.1s linear;
}

/* ===== 倒數 ===== */
#countdown {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 180px; font-weight: 900;
  color: #ffd000;
  text-shadow: 0 0 40px rgba(255, 208, 0, 0.8), 0 6px 0 rgba(0,0,0,0.4);
  animation: pop 0.9s ease-out;
}
@keyframes pop {
  0%   { transform: scale(0.3); opacity: 0; }
  40%  { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ===== 單圈提示 ===== */
#lap-notice {
  position: absolute; top: 30%; left: 50%;
  transform: translateX(-50%);
  font-size: 42px; font-weight: 800;
  text-align: center;
  color: #00e5ff;
  text-shadow: 0 0 24px rgba(0, 229, 255, 0.7);
  animation: fadein 0.3s ease-out;
}
@keyframes fadein {
  from { opacity: 0; transform: translate(-50%, -12px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

/* ===== 覆蓋層（選單 / 結算） ===== */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at center, #131c34 0%, #070b16 100%);
}

/* ===== 觸控控制按鈕（配合雙手橫握的大拇指人體工學）===== */
#touch-controls {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 15;
  display: none; /* 桌機預設隱藏，觸控裝置顯示 */
}
.touch-pad {
  position: absolute;
  bottom: 28px;
  display: flex;
  align-items: flex-end;
}
/* 左手大拇指區：貼左下、往內移，轉向鈕並排 */
.touch-left  { left: 30px; gap: 16px; }
/* 右手大拇指區：貼右下、往內移，踏板由下到上（油門最常按、在最下） */
.touch-right { right: 30px; flex-direction: column-reverse; gap: 10px; align-items: center; }

.tbtn {
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  user-select: none;
  width: 104px; height: 104px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.4);
  background: rgba(20, 30, 55, 0.55);
  backdrop-filter: blur(6px);
  color: #fff;
  font-size: 30px;
  font-weight: 800;
  font-family: inherit;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.06s, background 0.06s, border-color 0.06s;
}
.tbtn:active, .tbtn.pressed {
  transform: scale(0.88);
  background: rgba(0, 229, 255, 0.5);
  border-color: #00e5ff;
}
/* 轉向鈕：橢圓，符合左右滑動直覺 */
.tbtn-steer {
  width: 116px; height: 104px;
  border-radius: 54px;
  font-size: 40px;
}
/* 油門：最大、最顯眼（右手大拇指主力） */
.tbtn-gas {
  width: 116px; height: 116px;
  background: rgba(40, 170, 80, 0.62);
  border: 3px solid rgba(130, 255, 175, 0.65);
  font-size: 30px;
}
.tbtn-gas:active, .tbtn-gas.pressed { background: rgba(60, 225, 115, 0.8); border-color: #8effbb; transform: scale(0.9); }
/* 煞車：中等 */
.tbtn-brake {
  width: 92px; height: 92px;
  background: rgba(190, 70, 70, 0.6);
  border-color: rgba(255, 150, 150, 0.6);
  font-size: 23px;
}
.tbtn-brake:active, .tbtn-brake.pressed { background: rgba(225, 95, 95, 0.78); border-color: #ff9696; }
/* 漂移：較小，放最上 */
.tbtn-drift {
  width: 76px; height: 76px;
  background: rgba(160, 60, 200, 0.52);
  border-color: rgba(220, 150, 255, 0.55);
  font-size: 20px;
}
.tbtn-drift:active, .tbtn-drift.pressed { background: rgba(190, 90, 230, 0.72); border-color: #dc96ff; }

/* 手機／平板顯示觸控鈕：改由 body.touch-device 控制（JS 偵測，比媒體查詢可靠） */
body.touch-device #touch-controls { display: block; }
/* 桌機測試用：勾選或 URL 加 ?touch 強制顯示 */
body.force-touch #touch-controls { display: block; }

/* 選單中的觸控鈕開關 */
.touch-toggle {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin-top: 16px; font-size: 13px; color: #aab4d4; cursor: pointer;
}
.touch-toggle input { width: 16px; height: 16px; cursor: pointer; accent-color: #00e5ff; }
/* 觸控裝置的 HUD 縮小，避免擋到觸控鈕 */
body.touch-device #speedometer { padding: 8px 14px; }
body.touch-device #speed-value { font-size: 32px; }
body.touch-device #info-panel { padding: 8px 12px; min-width: 122px; }
body.touch-device .info-row span:last-child { font-size: 15px; }
body.touch-device #minimap { width: 120px !important; height: 120px !important; }
body.touch-device #position-display .big { font-size: 30px; }
/* 觸控裝置：右側面板（名次+小地圖）移到頂部中央，空出右下角給踏板 */
body.touch-device #right-panel {
  top: 10px; right: auto; left: 50%; transform: translateX(-50%);
  flex-direction: row; align-items: center; gap: 10px;
}
body.touch-device #position-display { padding: 6px 12px; }
body.touch-device #position-display .big { font-size: 26px; }
body.touch-device #position-display .suffix { font-size: 11px; }
/* 小螢幕(手機)按鈕略微縮小 */
@media (max-height: 500px) {
  body.touch-device .tbtn { width: 88px; height: 88px; font-size: 25px; }
  body.touch-device .tbtn-steer { width: 100px; height: 88px; font-size: 34px; }
  body.touch-device .tbtn-gas { width: 108px; height: 108px; font-size: 27px; }
  body.touch-device .tbtn-brake { width: 88px; height: 88px; font-size: 22px; }
  body.touch-device .tbtn-drift { width: 72px; height: 72px; font-size: 19px; }
  body.touch-device .touch-left  { left: 20px; gap: 12px; }
  body.touch-device .touch-right { right: 20px; gap: 10px; }
}

/* ===== 直向提示遮罩（手機直向時顯示）===== */
#rotate-prompt {
  position: fixed; inset: 0; z-index: 200;
  display: none;
  flex-direction: column; align-items: center; justify-content: center;
  gap: 18px;
  background: radial-gradient(ellipse at center, #131c34 0%, #070b16 100%);
  color: #fff;
  text-align: center;
  padding: 40px;
}
#rotate-prompt .rotate-icon { font-size: 72px; animation: rotshake 1.6s ease-in-out infinite; }
#rotate-prompt p { font-size: 20px; color: #cdd6ec; letter-spacing: 1px; }
@keyframes rotshake {
  0%, 100% { transform: rotate(0deg); }
  25%      { transform: rotate(-30deg); }
  75%      { transform: rotate(30deg); }
}
/* 觸控裝置直向（寬 < 高）才顯示轉向提示 */
@media (orientation: portrait) {
  body.touch-device #rotate-prompt { display: flex; }
}
.menu-card {
  width: min(640px, 92vw);
  background: rgba(14, 22, 42, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 22px;
  padding: 36px 40px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
  text-align: center;
}
.title {
  font-size: 64px; font-weight: 900; letter-spacing: -2px;
  line-height: 1;
  color: #fff;
}
.title span {
  display: block;
  background: linear-gradient(90deg, #00e5ff, #ffd000);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
  font-size: 84px;
}
.subtitle { color: #7e8db3; letter-spacing: 6px; margin: 6px 0 26px; text-transform: uppercase; font-size: 13px; }
.menu-card h2 {
  font-size: 18px; color: #aab4d4; font-weight: 600;
  margin-bottom: 14px; letter-spacing: 1px;
}

.track-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin-bottom: 26px;
}
.track-card {
  cursor: pointer; pointer-events: auto;
  background: rgba(255, 255, 255, 0.04);
  border: 2px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  padding: 16px 10px;
  transition: all 0.18s ease;
}
.track-card:hover {
  background: rgba(0, 229, 255, 0.12);
  border-color: #00e5ff;
  transform: translateY(-3px);
}
.track-card .track-icon {
  width: 100%; height: 64px;
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 8px;
}
.track-card .track-icon svg { width: 100%; height: 100%; }
.track-card .track-name { font-weight: 700; font-size: 15px; }
.track-card .track-desc { font-size: 11px; color: #7e8db3; margin-top: 3px; }

/* 選單自適應：窄螢幕（手機直向 9:16）壓縮排版，確保完整可見 */
@media (max-width: 600px) {
  .menu-card { width: 94vw; padding: 22px 18px; }
  .title { font-size: 44px; }
  .title span { font-size: 58px; }
  .subtitle { letter-spacing: 4px; margin: 4px 0 18px; font-size: 11px; }
  .menu-card h2 { font-size: 15px; margin-bottom: 10px; }
  /* 賽道卡片：直向時單欄，橫向窄螢幕時 2 欄 */
  .track-grid { grid-template-columns: 1fr; gap: 10px; margin-bottom: 16px; }
  .track-card { padding: 12px 10px; }
  .track-card .track-icon { height: 52px; }
  .track-card .track-name { font-size: 14px; }
  .controls-info { gap: 6px 12px; font-size: 11px; }
}
/* 手機橫向：賽道卡片 2 欄（3 欄太擠） */
@media (max-height: 500px) and (min-width: 600px) {
  .menu-card { width: 96vw; padding: 18px 22px; }
  .title { font-size: 40px; }
  .title span { font-size: 52px; }
  .subtitle { margin: 4px 0 14px; }
  .track-grid { grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
  .controls-info { gap: 6px 12px; }
}

.controls-info {
  display: flex; flex-wrap: wrap; justify-content: center;
  gap: 8px 16px;
  font-size: 12px; color: #aab4d4;
}
.ctrl { display: flex; align-items: center; gap: 5px; }
kbd {
  display: inline-block;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 2px 7px;
  font-size: 11px; font-family: monospace;
  color: #fff;
  min-width: 22px; text-align: center;
}

.btn-row { display: flex; gap: 12px; justify-content: center; margin-top: 20px; }
.btn {
  cursor: pointer; pointer-events: auto;
  font-family: inherit; font-weight: 700; font-size: 15px;
  padding: 12px 26px;
  border: none; border-radius: 10px;
  background: linear-gradient(90deg, #00e5ff, #00a8c8);
  color: #04121a;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.btn:hover { transform: translateY(-2px); box-shadow: 0 8px 22px rgba(0, 229, 255, 0.35); }
.btn-ghost {
  background: transparent; color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.25);
}
.btn-ghost:hover { background: rgba(255, 255, 255, 0.08); box-shadow: none; }

#finish-title { font-size: 44px; font-weight: 900; margin-bottom: 10px; }
#finish-stats {
  font-size: 17px; line-height: 2; color: #dfe6f5;
  background: rgba(0,0,0,0.25); border-radius: 12px;
  padding: 16px; margin-top: 12px;
  font-variant-numeric: tabular-nums;
}
#finish-stats .stat-label { display: inline-block; width: 130px; color: #7e8db3; text-align: right; margin-right: 14px; }
#finish-stats .stat-val { font-weight: 700; color: #ffd000; }
#finish-stats .rank-1 { color: #ffd000; }
#finish-stats .rank-2 { color: #c0c8da; }
#finish-stats .rank-3 { color: #cd7f32; }
