:root {
  --ink: #eaf3e8;
  --muted: #a9c6b1;
  --accent: #ffd23f;
  --red: #e0392b;
  --panel: rgba(6, 16, 18, .88);
  --line: rgba(126, 191, 148, .55);
}

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

html, body {
  margin: 0;
  height: 100%;
  background: #070b0e;
  color: var(--ink);
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Courier New", monospace;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

#app { position: fixed; inset: 0; display: grid; place-items: center; touch-action: none; }

#stage {
  position: relative;
  width: min(100vw, calc(100vh * 16 / 9));
}

#frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #0b1113;
  overflow: hidden;
  touch-action: none;
  border: 3px solid #2a4a3d;
  box-shadow: 0 0 0 4px #0d1714, 0 0 0 6px #1d3329, 0 18px 60px rgba(0, 0, 0, .8);
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 2.4% 2.6%;
  pointer-events: none;
  font-size: clamp(8px, 1.65vw, 14px);
  text-shadow: 1px 2px 0 #000;
}

.hud-row { display: flex; gap: 2.2%; align-items: stretch; }
.hud-row.second { margin-top: 1.6%; }

.hud-box {
  background: rgba(5, 16, 14, .62);
  border: 2px solid var(--line);
  padding: 1.4% 2.2%;
  min-width: 0;
}
.hud-box.grow { flex: 1; }

.hud-label { display: block; font-size: .76em; letter-spacing: .06em; color: var(--muted); margin-bottom: 1px; }
.hud-value { display: block; font-weight: 700; font-size: 1.12em; color: #fff; white-space: nowrap; }
.hud-value b { color: var(--accent); }
.hud-value small { font-size: .68em; color: var(--muted); font-weight: 400; }

.bar { height: 9px; margin-top: 4px; background: #2a1414; border: 1px solid #000; }
.bar i { display: block; height: 100%; width: 100%; background: linear-gradient(90deg, #e0392b, #ffd23f 55%, #57d66a); transition: width .12s linear; }

/* ---------- Toast ---------- */
#toast {
  position: absolute;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -8px);
  padding: 5px 14px;
  background: rgba(6, 16, 18, .82);
  border: 2px solid var(--accent);
  color: var(--accent);
  font-weight: 700;
  font-size: clamp(11px, 2.6vw, 20px);
  letter-spacing: .08em;
  opacity: 0;
  pointer-events: none;
  transition: opacity .16s, transform .16s;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Overlay ---------- */
#overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(3, 9, 11, .58);
  backdrop-filter: blur(2px);
}
#overlay.hidden { display: none; }

.panel {
  width: min(86%, 420px);
  padding: 4% 5%;
  text-align: center;
  background: var(--panel);
  border: 3px solid var(--line);
  box-shadow: inset 0 0 0 3px #0b1c19, 6px 6px 0 rgba(0, 0, 0, .5);
}
.panel.hidden { display: none; }

.panel h1 {
  margin: 0 0 2%;
  font-size: clamp(22px, 6vw, 48px);
  line-height: 1.1;
  letter-spacing: .06em;
  color: var(--accent);
  text-shadow: 3px 3px 0 #a8261d, 6px 6px 0 rgba(0, 0, 0, .45);
}
.panel h2 {
  margin: 0 0 5%;
  font-size: clamp(18px, 4.4vw, 30px);
  color: var(--accent);
  text-shadow: 2px 2px 0 #000;
}
.panel .sub { margin: 0 0 6%; font-size: clamp(9px, 1.9vw, 14px); color: var(--muted); letter-spacing: .1em; }
.panel .hint { margin: 5% 0 0; font-size: clamp(8px, 1.8vw, 12px); line-height: 1.7; color: var(--muted); }

.btn {
  display: block;
  width: 100%;
  margin: 3% 0 0;
  padding: 3.2% 6%;
  font: inherit;
  font-size: clamp(11px, 2.6vw, 17px);
  font-weight: 700;
  letter-spacing: .08em;
  color: var(--ink);
  background: #1c3a31;
  border: 2px solid #5c9d78;
  box-shadow: 3px 3px 0 #07100e;
  cursor: pointer;
}
.btn:hover { background: #275442; }
.btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 #07100e; }
.btn.primary { background: #b5301f; border-color: #ffb03a; color: #fff8dc; }
.btn.primary:hover { background: #d13d26; }
.btn.small { width: auto; margin: 5% auto 0; font-size: clamp(9px, 2vw, 13px); padding: 2% 5%; }

.result-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 12px;
  margin: 0 0 6%;
  padding: 4% 5%;
  text-align: left;
  background: rgba(0, 0, 0, .32);
  border: 2px solid rgba(126, 191, 148, .3);
  font-size: clamp(10px, 2.2vw, 14px);
}
.result-grid span { color: var(--muted); }
.result-grid b { color: var(--accent); text-align: right; }

#langBtn {
  position: absolute;
  top: 2.4%;
  right: 2.2%;
  z-index: 6;
  padding: 5px 9px;
  font: inherit;
  font-size: clamp(9px, 2vw, 13px);
  font-weight: 700;
  color: #0a1412;
  background: var(--accent);
  border: 2px solid #fff3b0;
  box-shadow: 2px 2px 0 rgba(0, 0, 0, .5);
  cursor: pointer;
}
#langBtn:active { transform: translate(1px, 1px); }

/* 横/竖屏切换 + 全屏按钮：紧邻语言按钮左侧，仅触屏显示 */
#orientationBtn, #fullscreenBtn {
  position: absolute;
  top: 2.4%;
  z-index: 6;
  padding: 5px 9px;
  font: inherit;
  font-size: clamp(9px, 2vw, 13px);
  font-weight: 700;
  color: var(--ink);
  background: rgba(6, 16, 18, .82);
  border: 2px solid var(--line);
  box-shadow: 2px 2px 0 rgba(0, 0, 0, .5);
  cursor: pointer;
  display: none;
  touch-action: none;
  -webkit-touch-callout: none;
}
#orientationBtn { right: calc(2.2% + 54px); }
#fullscreenBtn { right: calc(2.2% + 108px); color: var(--accent); border-color: var(--accent); }
#orientationBtn:active, #fullscreenBtn:active { transform: translate(1px, 1px); }
body.is-fullscreen #fullscreenBtn { color: #0a1412; background: var(--accent); }
@media (pointer: coarse) { #orientationBtn, #fullscreenBtn { display: block; } }

/* ---------- Multiplayer room ---------- */
.field { display: block; text-align: left; margin: 8px 0; font-size: clamp(10px, 2.1vw, 13px); color: var(--muted); }
.field span { display: block; margin-bottom: 4px; letter-spacing: .05em; }
.field input {
  width: 100%;
  padding: 8px 10px;
  font: inherit;
  font-size: clamp(12px, 2.6vw, 16px);
  color: #fff;
  background: #0a1715;
  border: 2px solid var(--line);
  outline: none;
}
.field input:focus { border-color: var(--accent); }
.status { min-height: 1.4em; margin: 10px 0 4px; font-size: clamp(9px, 2vw, 12px); color: var(--accent); }
.player-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin: 6px 0 10px;
  font-size: clamp(9px, 2vw, 12px);
}
.player-list span {
  padding: 4px 8px;
  background: rgba(0, 0, 0, .35);
  border: 1px solid var(--line);
  color: #eafff0;
}
.btn.hidden { display: none; }

/* ---------- Touch controls（画面内覆盖层） ---------- */
#touch { display: none; }
#desktopHint {
  margin: 10px 0 0;
  text-align: center;
  font-size: 11px;
  color: #5f7d6b;
}

/* 虚拟摇杆 */
.joystick {
  position: absolute;
  z-index: 3;
  pointer-events: auto;
  touch-action: none;
  -webkit-touch-callout: none;
}
.joy-base {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 40%, rgba(58, 110, 88, .72) 0%, rgba(20, 52, 40, .62) 55%, rgba(8, 24, 18, .74) 100%);
  border: 3px solid rgba(170, 235, 195, .62);
  box-shadow: inset 0 0 0 5px rgba(6, 20, 15, .55), inset 0 3px 10px rgba(0, 0, 0, .6), 0 6px 0 rgba(0, 0, 0, .45), 0 10px 20px rgba(0, 0, 0, .5);
}
.joy-base::before, .joy-base::after {
  content: '';
  position: absolute;
  background: rgba(170, 235, 195, .3);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .25);
}
.joy-base::before { left: 14%; right: 14%; top: 50%; height: 3px; transform: translateY(-50%); }
.joy-base::after { top: 14%; bottom: 14%; left: 50%; width: 3px; transform: translateX(-50%); }
.joy-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 44%;
  height: 44%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: radial-gradient(circle at 38% 30%, #e9fff1 0%, #9fe7b9 18%, #4faf77 55%, #1d6842 100%);
  border: 2px solid #e6fff0;
  box-shadow: inset 0 3px 3px rgba(255, 255, 255, .6), inset 0 -4px 5px rgba(0, 0, 0, .35), 0 5px 0 rgba(0, 0, 0, .5), 0 8px 12px rgba(0, 0, 0, .5);
  pointer-events: none;
}
.joy-knob::after {
  content: '';
  position: absolute;
  left: 22%; top: 16%; width: 34%; height: 24%;
  border-radius: 50%;
  background: rgba(255, 255, 255, .55);
  filter: blur(1px);
}
.joystick.active .joy-base { border-color: var(--accent); box-shadow: inset 0 0 0 5px rgba(255, 210, 63, .2), inset 0 3px 10px rgba(0, 0, 0, .55), 0 6px 0 rgba(0, 0, 0, .45), 0 0 18px rgba(255, 210, 63, .5); }

/* 动作按钮组 */
.touch-actions {
  position: absolute;
  z-index: 3;
  display: grid;
  grid-template-columns: repeat(2, auto);
  gap: clamp(8px, 1.6vh, 14px);
  pointer-events: none;
}
.ctrl {
  pointer-events: auto;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-weight: 800;
  letter-spacing: .04em;
  color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, .55);
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, .5);
  box-shadow: inset 0 4px 4px rgba(255, 255, 255, .38), inset 0 -6px 8px rgba(0, 0, 0, .4), 0 6px 0 var(--sh, rgba(0, 0, 0, .55)), 0 9px 14px rgba(0, 0, 0, .5);
  backdrop-filter: blur(2px);
  touch-action: none;
  -webkit-touch-callout: none;
  transition: transform .04s, box-shadow .04s, filter .08s;
}
.ctrl::before {
  content: var(--ico, '●');
  font-size: 1.55em;
  line-height: 1;
  margin-bottom: 1px;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, .4));
}
.ctrl::after {
  content: '';
  position: absolute;
  left: 14%; right: 14%; top: 8%; height: 24%;
  border-radius: 50%;
  background: linear-gradient(rgba(255, 255, 255, .42), rgba(255, 255, 255, 0));
  pointer-events: none;
}
.ctrl:active, .ctrl.active { transform: translateY(5px); box-shadow: inset 0 3px 5px rgba(0, 0, 0, .4), 0 1px 0 var(--sh, rgba(0, 0, 0, .55)), 0 2px 6px rgba(0, 0, 0, .4); filter: brightness(1.18); }
.ctrl.gas { --ico: '▲'; --sh: #155a2c; background: linear-gradient(160deg, #7fe89a 0%, #35b45c 45%, #1c7a3d 100%); border-color: #c9ffd8; color: #062a14; text-shadow: 0 1px 0 rgba(255, 255, 255, .35); }
.ctrl.brake { --ico: '▼'; --sh: #6e1b12; background: linear-gradient(160deg, #ff9a86 0%, #e54733 45%, #9c2115 100%); border-color: #ffd2c6; }
.ctrl.punch { --ico: '✦'; --sh: #8a5a08; background: linear-gradient(160deg, #ffe98a 0%, #f0b41e 45%, #a8740a 100%); border-color: #fff3bd; color: #3a2600; text-shadow: 0 1px 0 rgba(255, 255, 255, .4); }
.ctrl.kick { --ico: '➤'; --sh: #5a164f; background: linear-gradient(160deg, #f6a6e4 0%, #c142a0 45%, #7d1e68 100%); border-color: #ffc9ee; }

/* 面板打开时隐藏触控层，避免遮挡菜单/暂停/结算 */
#frame.overlay-open #touch { display: none !important; }

/* ---------- 横屏引导层（仅触屏竖屏显示） ---------- */
#rotateHint { display: none; }
@media (pointer: coarse) and (orientation: portrait) {
  #rotateHint.show {
    display: flex;
    position: fixed;
    inset: 0;
    z-index: 50;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: rgba(3, 9, 11, .96);
  }
  .rotate-card {
    width: min(88%, 360px);
    padding: 26px 22px;
    text-align: center;
    background: var(--panel);
    border: 3px solid var(--line);
    box-shadow: inset 0 0 0 3px #0b1c19, 6px 6px 0 rgba(0, 0, 0, .5);
  }
  .rotate-icon { height: 76px; display: flex; align-items: center; justify-content: center; }
  .rphone {
    display: block;
    position: relative;
    width: 38px; height: 66px;
    border: 3px solid var(--accent);
    border-radius: 7px;
    box-shadow: 0 0 18px rgba(255, 210, 63, .35);
    animation: rotatePhone 1.9s ease-in-out infinite;
  }
  .rphone::before { content: ''; position: absolute; left: 5px; right: 5px; top: 6px; bottom: 7px; background: rgba(255, 210, 63, .16); border-radius: 2px; }
  .rphone::after { content: ''; position: absolute; left: 50%; bottom: 2px; transform: translateX(-50%); width: 8px; height: 3px; border-radius: 2px; background: var(--accent); }
  @keyframes rotatePhone {
    0%, 18% { transform: rotate(0deg); }
    46%, 72% { transform: rotate(-90deg); }
    100% { transform: rotate(0deg); }
  }
  .rotate-card h2 { margin: 8px 0 6px; font-size: 22px; color: var(--accent); text-shadow: 2px 2px 0 #000; }
  .rotate-card p { margin: 0 0 18px; font-size: 12px; line-height: 1.6; color: var(--muted); }
  .rotate-tip { min-height: 1.5em; margin: -8px 0 12px !important; font-size: 12px; color: var(--accent) !important; }
  .rotate-tip:empty { display: none; }
}

/* 横屏：画面按比例最大化，摇杆左下，按钮右下 */
@media (pointer: coarse) and (orientation: landscape) {
  #desktopHint { display: none; }
  #stage { width: min(100vw, calc(100vh * 16 / 9)); }
  /* 右上角全屏/横屏/语言按钮的专属留白，避免压住时间与车况 HUD */
  #hud { padding-right: clamp(175px, 26vw, 220px); }
  #touch { display: block; position: absolute; inset: 0; pointer-events: none; }
  .joystick { left: 4%; bottom: 8%; width: clamp(96px, 22vh, 150px); aspect-ratio: 1; }
  .touch-actions { right: 4%; bottom: 8%; gap: clamp(10px, 2vh, 18px); }
  .ctrl { width: clamp(58px, 12vh, 82px); height: clamp(58px, 12vh, 82px); font-size: clamp(12px, 2.4vh, 16px); }
}

/* 竖屏：摇杆与按钮置于画面内底部 */
@media (pointer: coarse) and (orientation: portrait) {
  body { background: radial-gradient(circle at 50% 24%, #183a2d 0%, #0a1512 45%, #070b0e 80%); }
  #stage { width: 100vw; }
  #frame { border-width: 2px; }
  #desktopHint { display: none; }
  #touch { display: block; position: absolute; inset: 0; pointer-events: none; }
  .joystick { left: 4%; bottom: 4%; width: 30%; max-width: 130px; aspect-ratio: 1; }
  .touch-actions { right: 4%; bottom: 4%; gap: 8px; }
  .ctrl { width: clamp(46px, 13vw, 62px); height: clamp(46px, 13vw, 62px); font-size: 12px; }
  #hud { font-size: 9px; padding-right: 156px; }
  #langBtn { padding: 4px 7px; }
  #orientationBtn, #fullscreenBtn { padding: 4px 6px; }
  #orientationBtn { right: calc(2.2% + 46px); }
  #fullscreenBtn { right: calc(2.2% + 92px); }
  .panel { width: 84%; padding: 16px 18px; }
  .panel h1 { font-size: 30px; }
  .btn { padding: 10px; }
}

@media (max-height: 420px) and (orientation: landscape) {
  #stage { width: min(100vw, calc(100vh * 16 / 9)); }
  .panel { padding: 2% 4%; }
  .panel h1 { font-size: clamp(18px, 5vw, 32px); }
  .panel .sub, .panel .hint { margin: 2% 0; }
  .btn { margin-top: 2%; padding: 2% 5%; }
}

/* ---------- 强制横屏 ----------
   物理仍是竖屏、但玩家想横着玩时：把整个画面旋转 90° 并铺满。
   不依赖 screen.orientation.lock（iOS / 锁方向设备不支持），纯 CSS 100% 生效。
   物理真正横屏时不会加此类，走原生横屏布局。 */
body.force-landscape #app {
  inset: auto;
  top: 50%;
  left: 50%;
  right: auto;
  bottom: auto;
  width: 100vh;
  height: 100vw;
  transform: translate(-50%, -50%) rotate(90deg);
  transform-origin: center center;
}
body.force-landscape #stage { width: min(100vh, calc(100vw * 16 / 9)); }
body.force-landscape #rotateHint { display: none !important; }
body.force-landscape #desktopHint { display: none !important; }
/* 旋转后控件按横屏比例放大（可用高度此时等于物理视口宽度 100vw） */
body.force-landscape .joystick { left: 4%; bottom: 8%; width: clamp(96px, 22vw, 150px); }
body.force-landscape .touch-actions { right: 4%; bottom: 8%; gap: clamp(8px, 2vw, 16px); }
body.force-landscape .ctrl { width: clamp(50px, 12vw, 82px); height: clamp(50px, 12vw, 82px); font-size: clamp(11px, 2.4vw, 16px); }
body.force-landscape #hud { font-size: clamp(8px, 1.5vw, 13px); padding-right: clamp(175px, 26vw, 220px); }

/* ---------- 全屏 ----------
   原生全屏时浏览器接管视口，#app 仍铺满，画面比例不变；
   iOS iPhone 等不支持 Fullscreen API 时用填充视口的伪全屏兜底。 */
#langBtn { touch-action: none; -webkit-touch-callout: none; }
body.pseudo-fullscreen #app { background: #070b0e; z-index: 100; }
body.pseudo-fullscreen #stage { width: min(100vw, calc(100vh * 16 / 9)); }
body.is-fullscreen #frame, body.pseudo-fullscreen #frame {
  border-width: 0;
  box-shadow: none;
}
:fullscreen #app, :-webkit-full-screen #app { background: #070b0e; }
/* 任意界面元素都不允许浏览器缩放手势介入 */
#overlay, #hud, #toast, #rotateHint { touch-action: none; }
