* { margin: 0; padding: 0; box-sizing: border-box; }

/* 全域：manipulation 移除 double-tap zoom 與 300ms 點擊延遲（仍保留正常捲動）；text-size-adjust 防 iOS 轉向放大字 */
html, body {
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

:root {
  --bg: #070b12;
  --panel: rgba(13, 20, 33, 0.92);
  --accent: #4ea1ff;
  --accent2: #ff4df0;
}

body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at top, #11203a, #05080d 70%);
  font-family: -apple-system, "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: #e2e8f0;
  padding: 16px;
}

#game-wrap { width: 100%; max-width: 960px; }

#stage {
  position: relative;
  width: 100%;
  aspect-ratio: 960 / 540;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(255, 255, 255, 0.06);
  background: #05080d;
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: auto;
}

#btn-mute {
  position: absolute;
  top: 10px; right: 10px;
  z-index: 20;
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  width: 34px; height: 34px;
  font-size: 16px;
  cursor: pointer;
}

#btn-fs {
  position: absolute;
  top: 10px; right: 52px;
  z-index: 20;
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  width: 34px; height: 34px;
  font-size: 16px;
  color: #fff;
  cursor: pointer;
}

/* 全螢幕：畫面維持 960:540 置中，黑邊吸收不同手機尺寸差異，觸控鍵相對畫面定位 */
#stage:fullscreen, #stage:-webkit-full-screen {
  width: 100vw; height: 100vh;
  max-width: none;
  border-radius: 0;
  aspect-ratio: auto;
  display: flex; align-items: center; justify-content: center;
  background: #000;
}
#stage:fullscreen #game-canvas, #stage:-webkit-full-screen #game-canvas {
  width: 100%; height: 100%;
  object-fit: contain;
}

/* iOS Safari 不支援 Fullscreen API → 用 CSS 假全螢幕：fixed 撐滿整個視窗。
   100dvh（dynamic viewport height）會扣掉瀏覽器 URL bar，比 100vh 更貼合可視範圍。 */
body.fs-fallback { overflow: hidden; }
body.fs-fallback #game-wrap {
  position: fixed;
  inset: 0;
  z-index: 9999;
  max-width: none;
  width: 100vw;
  height: 100vh;     /* 舊瀏覽器 fallback */
  height: 100dvh;
  margin: 0;
  display: flex; align-items: center; justify-content: center;
}
body.fs-fallback #stage {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: none;
  border-radius: 0;
  aspect-ratio: auto;
  display: flex; align-items: center; justify-content: center;
  background: #000;
}
body.fs-fallback #game-canvas {
  width: 100%; height: 100%;
  object-fit: contain;
}
body.fs-fallback .hint { display: none; }

/* iOS「加到主畫面」引導：點全螢幕後從底部滑入，點一下或數秒後消失 */
#a2hs-hint {
  position: fixed;
  left: 50%; bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
  transform: translateX(-50%) translateY(140%);
  z-index: 10000;
  max-width: min(92vw, 420px);
  padding: 11px 16px;
  background: rgba(13, 20, 33, 0.96);
  border: 1px solid rgba(78, 161, 255, 0.5);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  color: #eaf2ff;
  font-size: 13.5px;
  line-height: 1.5;
  text-align: center;
  opacity: 0;
  transition: transform .35s ease, opacity .35s ease;
  pointer-events: auto;
}
#a2hs-hint.show { transform: translateX(-50%) translateY(0); opacity: 1; }
#a2hs-hint b { color: var(--accent); }
#a2hs-hint .a2hs-close { color: #93a4bd; font-size: 12px; margin-top: 4px; display: block; }

/* ---- 觸控操作疊層 ---- */
#touch-controls {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;     /* 容器不擋，只有按鈕可點 */
  display: none;
}
body.touch #touch-controls { display: block; }
body.touch #stage { touch-action: none; }

#touch-controls button {
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  color: #eaf2ff;
  font-family: inherit;
  cursor: pointer;
}

.tc-dpad { position: absolute; left: 3%; bottom: 4%; display: flex; gap: 12px; }
.tc-actions { position: absolute; right: 3%; bottom: 4%; display: flex; gap: 12px; align-items: flex-end; }
.tc-powers {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: 17%; display: flex; gap: 1.2%;
}

.tc-btn {
  width: 64px; height: 64px;
  border-radius: 50%;
  font-size: 26px;
  background: rgba(20, 32, 50, 0.5);
  border: 2px solid rgba(150, 190, 255, 0.45);
  backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center;
}
.tc-jump { background: rgba(30, 60, 110, 0.55); border-color: rgba(120, 200, 255, 0.6); }
.tc-attack { background: rgba(110, 40, 40, 0.55); border-color: rgba(255, 140, 140, 0.6); }
.tc-btn:active { transform: scale(0.92); background: rgba(78, 161, 255, 0.55); }

.tc-power {
  width: 42px; height: 42px;
  border-radius: 12px;
  background: rgba(14, 22, 36, 0.7);
  border: 2px solid rgba(120, 150, 190, 0.5);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  line-height: 1;
}
.tc-power b { font-size: 14px; font-weight: 800; }
.tc-power i { font-size: 8px; font-style: normal; color: #9fb8d6; margin-top: 2px; }
.tc-power:active { transform: scale(0.9); background: rgba(78, 161, 255, 0.5); }
.tc-power.locked { opacity: 0.32; }
.tc-power.cooling { opacity: 0.5; }
.tc-power.ult-ready {
  border-color: #ff4df0;
  box-shadow: 0 0 12px rgba(255, 77, 240, 0.8);
  animation: ultpulse 0.9s infinite;
}
@keyframes ultpulse { 0%,100% { box-shadow: 0 0 8px rgba(255,77,240,.6); } 50% { box-shadow: 0 0 18px rgba(255,77,240,1); } }

/* ---- 疊層 ---- */
#overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, rgba(6, 12, 22, 0.82), rgba(3, 5, 9, 0.95));
  backdrop-filter: blur(2px);
  padding: 24px;
  text-align: center;
}
#overlay.show { display: flex; }

.screen { display: none; max-width: 640px; width: 100%; animation: fade 0.3s ease; }
.screen.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.title {
  font-size: clamp(22px, 4vw, 38px);
  font-weight: 800;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
  background: linear-gradient(90deg, #ff8a2a, #ffd84d, #ff4df0);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.title.sm { font-size: clamp(20px, 3vw, 28px); -webkit-text-fill-color: #cfe3ff; background: none; }
.title.win { background: linear-gradient(90deg, #5ff0c8, #ffd84d); -webkit-background-clip: text; background-clip: text; }
.title.lose { background: linear-gradient(90deg, #ff4d5e, #b026ff); -webkit-background-clip: text; background-clip: text; }

.subtitle { color: #9fb3c8; line-height: 1.7; font-size: 14px; margin-bottom: 20px; }

.name-row { margin-bottom: 18px; }
.name-row label { display: block; font-size: 13px; color: #7f93a8; margin-bottom: 8px; letter-spacing: 1px; }
#name-input {
  width: 100%;
  max-width: 320px;
  padding: 12px 16px;
  font-size: 16px;
  text-align: center;
  border-radius: 10px;
  border: 1px solid rgba(78, 161, 255, 0.5);
  background: rgba(10, 18, 30, 0.9);
  color: #fff;
  outline: none;
}
#name-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(78, 161, 255, 0.2); }

.btn {
  margin-top: 6px;
  padding: 12px 32px;
  font-size: 16px;
  font-weight: 700;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.2s;
  color: #fff;
}
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(0); }
.btn.primary { background: linear-gradient(135deg, #2a7de1, #4ea1ff); box-shadow: 0 6px 20px rgba(78, 161, 255, 0.4); }
.btn.danger { background: linear-gradient(135deg, #e1342a, #ff5d6c); box-shadow: 0 6px 20px rgba(255, 93, 108, 0.4); }

.controls { margin-top: 26px; padding-top: 20px; border-top: 1px solid rgba(255, 255, 255, 0.08); }
.controls h3 { font-size: 13px; color: #7f93a8; margin-bottom: 12px; letter-spacing: 1px; }
.control-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  font-size: 12px;
  color: #b8c6d6;
}
kbd {
  display: inline-block;
  padding: 1px 7px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 5px;
  font-family: monospace;
  font-size: 11px;
}

.unlock-label { font-size: 13px; color: #7f93a8; margin: 14px 0 10px; }
.power-list { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-bottom: 22px; }
.power-pill {
  padding: 6px 14px;
  font-size: 13px;
  border-radius: 999px;
  background: rgba(78, 161, 255, 0.14);
  border: 1px solid rgba(78, 161, 255, 0.4);
  color: #cfe3ff;
}

.score-line { font-size: 17px; color: #ffd84d; font-weight: 700; margin: 8px 0 22px; }

.hint { text-align: center; font-size: 12px; color: #5f7286; margin-top: 14px; line-height: 1.6; }
.hint b { color: #ff8acb; }

@media (max-width: 520px) {
  .control-grid { grid-template-columns: repeat(2, 1fr); }
}
