:root {
  --panel: rgba(6, 10, 22, 0.74);
  --panel-strong: rgba(8, 12, 28, 0.9);
  --line: rgba(255,255,255,0.16);
  --text: #f8fbff;
  --muted: #b8c5dd;
  --cyan: #25d7ff;
  --gold: #ffd369;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(circle at 50% 20%, #18335f 0%, #050711 62%, #02030a 100%);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
/* iOS Safari の下部ツールバーぶんを差し引いた実視覚領域の高さ。
   1) JS が visualViewport から計測して --app-height を毎フレーム更新する
   2) JS 未対応 / 旧ブラウザでは 100vh にフォールバック
   3) 100dvh 対応ブラウザ (Safari 15.4+, Chrome 108+ など) ではそちらを優先 */
#app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: var(--app-height, 100vh);
  height: 100dvh;
  display: grid;
  place-items: center;
}
#game {
  width: min(100vw, calc((var(--app-height, 100vh)) * 16 / 9));
  height: min(var(--app-height, 100vh), calc(100vw * 9 / 16));
  display: block;
  border-radius: 18px;
  background: #000;
  box-shadow: 0 24px 80px rgba(0,0,0,.55), inset 0 0 0 1px rgba(255,255,255,.08);
}
.overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.menu {
  pointer-events: auto;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(1120px, calc(100vw - 32px));
  max-height: min(680px, calc(var(--app-height, 100vh) - 28px));
  overflow: auto;
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 28px;
  background: linear-gradient(180deg, rgba(8, 14, 32, .88), rgba(5, 8, 18, .78));
  box-shadow: 0 24px 100px rgba(0,0,0,.62), inset 0 1px 0 rgba(255,255,255,.1);
  backdrop-filter: blur(12px);
  /* body の touch-action: none を打ち消し、メニュー内では縦スクロールを許可。
     iOS Safari は祖先の touch-action: none を強く尊重するため、メニュー側で
     pan-y を明示しないと内部スクロールがロックされ、ボタン上でも僅かな指の
     ズレで click が抑制されることがある。 */
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}
.menu.narrow { width: min(720px, calc(100vw - 32px)); }
.logo {
  margin: 0;
  font-size: clamp(36px, 6vw, 82px);
  line-height: .9;
  letter-spacing: -0.055em;
  text-shadow: 0 6px 0 rgba(0,0,0,.45), 0 0 24px rgba(37,215,255,.42);
}
.logo span { color: var(--gold); }
.sub {
  margin: 14px 0 24px;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.75;
}
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.btn {
  appearance: none;
  border: 1px solid rgba(255,255,255,.18);
  color: #08111c;
  background: linear-gradient(180deg, #ffed98, #ffb833);
  font-weight: 900;
  letter-spacing: .04em;
  padding: 13px 18px;
  min-width: 160px;
  border-radius: 16px;
  cursor: pointer;
  box-shadow: 0 10px 28px rgba(255,190,47,.24), inset 0 1px 0 rgba(255,255,255,.55);
  /* iOS Safari でのタップ反応性を確保:
     - touch-action: manipulation で 300ms クリック遅延と
       祖先の touch-action: none による click 抑制を回避
     - tap-highlight で押下時の視覚フィードバックを薄く付与 */
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(255, 220, 103, .25);
  -webkit-user-select: none;
  user-select: none;
}
.btn.secondary {
  color: var(--text);
  background: linear-gradient(180deg, rgba(255,255,255,.12), rgba(255,255,255,.04));
  box-shadow: none;
}
.btn:hover { filter: brightness(1.08); transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }
.grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}
.stage-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.card {
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 22px;
  padding: 14px;
  background: linear-gradient(180deg, rgba(255,255,255,.105), rgba(255,255,255,.035));
  box-shadow: inset 0 1px 0 rgba(255,255,255,.08);
  cursor: pointer;
  transition: .16s ease;
  /* キャラ／ステージ選択カードも iOS Safari でタップを取りこぼさないように
     manipulation を指定して click を確実化。 */
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(120, 200, 255, .18);
  -webkit-user-select: none;
  user-select: none;
}
.card:hover { transform: translateY(-4px); border-color: rgba(255,255,255,.36); }
.card.selected {
  border-color: #ffdc67;
  box-shadow: 0 0 0 2px rgba(255,220,103,.22), 0 16px 50px rgba(255,195,47,.18);
}
.card img {
  width: 100%;
  display: block;
  border-radius: 16px;
  background: rgba(0,0,0,.22);
}
.card h3 { margin: 12px 0 4px; font-size: 18px; }
.card p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.stats { margin-top: 10px; display: grid; gap: 6px; }
.stat { display: grid; grid-template-columns: 70px 1fr; gap: 8px; align-items: center; font-size: 12px; color: var(--muted); }
.bar { height: 8px; border-radius: 999px; background: rgba(255,255,255,.11); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 999px; background: linear-gradient(90deg, #25d7ff, #ffdc67); }
.help {
  margin-top: 18px;
  padding: 14px;
  border-radius: 18px;
  border: 1px solid rgba(255,255,255,.10);
  background: rgba(0,0,0,.22);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.8;
}

/* ===== 操作早見表 (showHowTo 用) =====
   PC キー / スマホボタン / 説明 を一目で把握できるカードリスト。 */
.controls-list {
  margin: 14px 0 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.controls-list .ctrl {
  display: grid;
  grid-template-columns: 88px 1fr;
  gap: 12px;
  align-items: start;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid rgba(255,255,255,.12);
  background: linear-gradient(180deg, rgba(255,255,255,.06), rgba(255,255,255,.02));
}
.controls-list .ctrl[data-accent="attack"]  { box-shadow: inset 3px 0 0 #196dff; }
.controls-list .ctrl[data-accent="special"] { box-shadow: inset 3px 0 0 #a04cff; }
.controls-list .ctrl[data-accent="guard"]   { box-shadow: inset 3px 0 0 #78e6ff; }
.controls-list .ctrl[data-accent="jump"]    { box-shadow: inset 3px 0 0 #25d7ff; }
.controls-list .ctrl[data-accent="dash"]    { box-shadow: inset 3px 0 0 #ffd250; }
.controls-list .ctrl[data-accent="move"]    { box-shadow: inset 3px 0 0 #b8c5dd; }
.controls-list .ctrl[data-accent="system"]  { box-shadow: inset 3px 0 0 #6b7794; }

.controls-list .ctrl-icon {
  width: 88px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 900;
  letter-spacing: .04em;
  color: #08111c;
  background: linear-gradient(180deg, #ffed98, #ffb833);
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,.2);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.5);
}
.controls-list .ctrl[data-accent="attack"]  .ctrl-icon { background: linear-gradient(180deg, rgba(25,109,255,1), rgba(8,57,145,1)); color: #fff; }
.controls-list .ctrl[data-accent="special"] .ctrl-icon { background: linear-gradient(180deg, rgba(160,76,255,1), rgba(93,25,183,1)); color: #fff; }
.controls-list .ctrl[data-accent="guard"]   .ctrl-icon { background: linear-gradient(180deg, rgba(120,230,255,1), rgba(23,94,128,1)); color: #06212c; }
.controls-list .ctrl[data-accent="jump"]    .ctrl-icon { background: linear-gradient(180deg, rgba(37,215,255,1), rgba(12,125,164,1)); color: #06212c; }
.controls-list .ctrl[data-accent="dash"]    .ctrl-icon { background: linear-gradient(180deg, rgba(255,210,80,1), rgba(171,103,18,1)); color: #17110a; }
.controls-list .ctrl[data-accent="move"]    .ctrl-icon { background: linear-gradient(180deg, rgba(80,100,140,.95), rgba(40,55,85,.95)); color: #fff; }
.controls-list .ctrl[data-accent="system"]  .ctrl-icon { background: linear-gradient(180deg, rgba(60,70,95,.95), rgba(28,36,55,.95)); color: #cfd6e6; font-size: 12px; }

.controls-list .ctrl-body { min-width: 0; }
.controls-list .ctrl-name {
  font-size: 14px;
  font-weight: 800;
  color: #f3f7ff;
  letter-spacing: .02em;
  margin: 0 0 4px;
}
.controls-list .ctrl-keys {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 6px;
}
.controls-list .kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 24px;
  padding: 0 7px;
  border-radius: 6px;
  border: 1px solid rgba(255,255,255,.22);
  border-bottom-width: 2px;
  background: linear-gradient(180deg, rgba(255,255,255,.16), rgba(255,255,255,.06));
  color: #f8fbff;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .02em;
}
.controls-list .ctrl-or { color: var(--muted); font-size: 11px; align-self: center; padding: 0 2px; }
.controls-list .ctrl-desc {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--muted);
}
.controls-list .ctrl-desc b { color: #ffe49b; font-weight: 700; }

@media (max-width: 720px) {
  .controls-list { grid-template-columns: 1fr; gap: 8px; }
  .controls-list .ctrl { padding: 10px 12px; grid-template-columns: 76px 1fr; gap: 10px; }
  .controls-list .ctrl-icon { width: 76px; height: 44px; font-size: 12px; }
  .controls-list .ctrl-name { font-size: 13px; }
  .controls-list .ctrl-desc { font-size: 12px; }
}
.touch-controls {
  /* #app の bottom (= --app-height で実測した可視領域の下端) に揃える。
     iOS Safari の下部 URL バーや、Android の URL バー／ホームインジケータ
     ぶんを max() で確保し、絶対にボタンが隠れないようにする。 */
  position: absolute;
  left: 0; right: 0;
  bottom: max(env(safe-area-inset-bottom), 14px);
  display: none;
  justify-content: space-between;
  align-items: flex-end;
  padding: 0 14px;
  pointer-events: none;
  z-index: 20;
}
.touch-left, .touch-right { display: flex; gap: 9px; pointer-events: auto; align-items: flex-end; }
.touch-controls button {
  min-width: 70px;
  height: 70px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,.22);
  background: linear-gradient(180deg, rgba(14, 28, 58, .92), rgba(8, 15, 34, .74));
  color: white;
  font-size: 18px;
  font-weight: 900;
  letter-spacing: .02em;
  backdrop-filter: blur(10px);
  box-shadow: 0 12px 30px rgba(0,0,0,.30), inset 0 1px 0 rgba(255,255,255,.1);
  touch-action: none;
}
.touch-controls button:active, .touch-controls button.active {
  transform: scale(.96);
  filter: brightness(1.15);
}
.touch-left button { min-width: 70px; }
.touch-left button[data-touch=dash] { min-width: 82px; background: linear-gradient(180deg, rgba(255, 210, 80, .95), rgba(171, 103, 18, .86)); color:#17110a; }
.touch-right button[data-touch=attack] { min-width: 92px; background: linear-gradient(180deg, rgba(25, 109, 255, .95), rgba(8, 57, 145, .86)); }
.touch-right button[data-touch=guard] { min-width: 88px; background: linear-gradient(180deg, rgba(120, 230, 255, .95), rgba(23, 94, 128, .86)); }
.touch-right button[data-touch=special] { min-width: 92px; background: linear-gradient(180deg, rgba(160, 76, 255, .95), rgba(93, 25, 183, .86)); }
.touch-right button[data-touch=jump] { min-width: 88px; background: linear-gradient(180deg, rgba(37, 215, 255, .95), rgba(12, 125, 164, .86)); }
@media (hover: none), (max-width: 860px) {
  #game { width: 100vw; height: calc(100vw * 9 / 16); max-height: var(--app-height, 100vh); border-radius: 0; }
  .touch-controls { display: flex; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* スマホでは画面下部 ~150px をタッチコントロールが占有しているので、
     メニュー本体をその上に収まる高さ／位置に押し上げる。
     - max-height: タッチコントロール領域を差し引いた高さに制限
     - top    : 残った領域の中央へメニューを再配置 (transform: translate(-50%,-50%) と組合せ)
     - padding-bottom: 内部スクロール末尾にも余白を入れて最後のボタンが完全に見える */
  .menu {
    padding: 18px 18px calc(26px + env(safe-area-inset-bottom, 0px));
    border-radius: 22px;
    --touch-pad: calc(150px + env(safe-area-inset-bottom, 0px));
    max-height: min(680px, calc(var(--app-height, 100vh) - 28px - var(--touch-pad)));
    top: calc((var(--app-height, 100vh) - var(--touch-pad)) / 2);
  }
  .card h3 { font-size: 15px; }
  .card p, .help { font-size: 12px; }
}
@media (max-width: 540px) {
  .grid, .stage-grid { grid-template-columns: 1fr; }
  .btn { min-width: 130px; padding: 12px 14px; }

  /* 縦持ちスマホでは横一列だと 7 ボタンが画面幅に収まらないため、
     左右パネルをそれぞれ 2 列グリッドにして 2 段組みにする。
     これによりタップしやすいサイズを保ったまま全ボタンを画面内に表示できる。 */
  .touch-controls {
    padding: 0 10px;
    gap: 10px;
    align-items: flex-end;
  }

  .touch-left {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: 52px;
    gap: 8px;
    width: min(42vw, 190px);
  }
  .touch-left button {
    width: 100%;
    height: 100%;
    min-width: 0;
    font-size: 18px;
    padding: 0 6px;
  }
  .touch-left button[data-touch=dash] {
    grid-column: 1 / -1;
    height: 46px;
    font-size: 13px;
    min-width: 0;
  }

  .touch-right {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: 52px;
    gap: 8px;
    width: min(48vw, 220px);
  }
  .touch-right button,
  .touch-right button[data-touch=attack],
  .touch-right button[data-touch=special],
  .touch-right button[data-touch=jump],
  .touch-right button[data-touch=guard] {
    width: 100%;
    height: 100%;
    min-width: 0;
    font-size: 12px;
    padding: 0 6px;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  /* 横持ちは画面幅にも余裕があるので従来の横一列レイアウトに戻す。
     上の縦持ち用グリッド指定を打ち消すため display を flex に戻す。 */
  .touch-controls { bottom: max(8px, env(safe-area-inset-bottom)); align-items: flex-end; }
  /* 横持ちはタッチコントロールが 1 段で約 80px のため、メニューの予約領域を縮めて
     縦方向の作業面積を広く取り直す。 */
  .menu {
    --touch-pad: calc(85px + env(safe-area-inset-bottom, 0px));
  }
  .touch-left, .touch-right {
    display: flex;
    width: auto;
    gap: 6px;
    grid-auto-rows: auto;
  }
  .touch-controls button { height: 58px; min-width: 58px; font-size: 13px; padding: 0 12px; width: auto; }
  .touch-left button[data-touch=dash] { min-width: 70px; height: 58px; font-size: 13px; grid-column: auto; }
  .touch-right button,
  .touch-right button[data-touch=attack],
  .touch-right button[data-touch=special],
  .touch-right button[data-touch=jump],
  .touch-right button[data-touch=guard] { min-width: 68px; height: 58px; font-size: 13px; width: auto; }
}
