/* 異世界タイピング - Isekai Typing */

:root {
  --bg: #0b0a1c;
  --bg2: #1b1740;
  --panel: #191534;
  --panel-2: #221c46;
  --panel-border: rgba(176, 164, 255, 0.2);
  --text: #ece8ff;
  --text-dim: #9a92c4;
  --mana: #8b7bff;
  --mana-lit: #b3a6ff;
  --gold: #ffd76a;
  --fire: #ff6a3c;
  --ice: #4de1ff;
  --thunder: #ffe14d;
  --wind: #66e08a;
  --earth: #c9a24b;
  --light: #fff2c4;
  --dark: #b06bff;
  --heal: #7ef0b0;
  --arcane: #8b7bff;
  --danger: #ff5a6a;
  --good: #6fe08a;
  --accent: var(--mana);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background:
    radial-gradient(900px 460px at 50% -80px, #2a2160 0%, rgba(11, 10, 28, 0) 70%),
    linear-gradient(180deg, #120f2c 0%, var(--bg) 60%);
  background-attachment: fixed;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }

button {
  font-family: inherit;
  touch-action: manipulation;
}

#app {
  min-height: calc(100vh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  min-height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  display: flex;
  flex-direction: column;
  /* 上のセーフエリアは共通ヘッダーの --sgh-top（body の padding-top）に含まれているので、ここでは空けない */
  padding: 0 env(safe-area-inset-right, 0px) 0 env(safe-area-inset-left, 0px);
}

/* プレイ中はページをスクロールさせず、盤面をビューポートに収める */
body.playing #app {
  height: calc(100vh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  overflow: hidden;
}
body.playing #footer { display: none; }

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-size: 19px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}
.icon-btn:active { transform: scale(0.94); }
.icon-btn[aria-pressed="true"] {
  background: rgba(139, 123, 255, 0.28);
  border-color: var(--mana);
  color: #fff;
}

/* ===================== ホーム画面 ===================== */

#screen-home {
  flex: 1;
  width: min(600px, 100%);
  margin: 0 auto;
  padding: 14px 16px 8px;
}

#home-head { position: relative; text-align: center; }

#home-head-btns {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  gap: 8px;
}

#logo {
  margin: 6px 0 4px;
  font-size: clamp(30px, 8vw, 42px);
  font-weight: 900;
  letter-spacing: 0.06em;
  line-height: 1.15;
  color: #fff;
  text-shadow: 0 0 18px rgba(139, 123, 255, 0.7), 0 0 44px rgba(139, 123, 255, 0.35);
}

.logo-sub {
  display: block;
  font-size: 11px;
  letter-spacing: 0.34em;
  font-weight: 800;
  color: var(--gold);
  text-shadow: none;
  margin-bottom: 2px;
}

#tagline {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--text-dim);
  letter-spacing: 0.04em;
}

.mode-card {
  background: linear-gradient(180deg, rgba(34, 28, 70, 0.9), rgba(20, 17, 44, 0.9));
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

.mode-head { display: flex; gap: 10px; align-items: flex-start; }
.mode-emoji { font-size: 26px; line-height: 1.1; }
.mode-text { flex: 1; min-width: 0; }

.mode-name {
  margin: 0 0 4px;
  font-size: 18px;
  font-weight: 900;
  color: var(--gold);
}

.mode-desc {
  margin: 0;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-dim);
}
.mode-desc b { color: var(--mana-lit); }

.diff-seg {
  display: flex;
  gap: 6px;
  margin: 12px 0 0;
}

.diff-btn {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 14px;
  font-weight: 800;
  padding: 8px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.diff-btn small { font-size: 10px; font-weight: 600; color: var(--text-dim); }
.diff-btn.on {
  background: rgba(139, 123, 255, 0.22);
  border-color: var(--mana);
  color: #fff;
}
.diff-btn.on small { color: var(--mana-lit); }

.start-btn {
  margin-top: 12px;
  border: none;
  border-radius: 14px;
  padding: 12px 18px;
  font-size: 17px;
  font-weight: 900;
  color: #170f36;
  background: linear-gradient(180deg, #c7b6ff, #8b7bff);
  box-shadow: 0 6px 20px rgba(139, 123, 255, 0.4);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.start-btn.wide { width: 100%; }
.start-btn:active { transform: scale(0.98); }
.start-btn .best-chip {
  font-size: 11px;
  font-weight: 800;
  color: #2a1d55;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 999px;
  padding: 2px 9px;
}

/* ===================== ゲーム画面 ===================== */

#screen-game {
  flex: 1;
  min-height: 0;
  width: min(720px, 100%);
  margin: 0 auto;
  padding: 8px 12px 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#game-top {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
}

#game-mode-label {
  flex: 1;
  text-align: center;
  font-size: 14px;
  font-weight: 800;
  color: var(--text-dim);
  letter-spacing: 0.08em;
}

#game-top-btns { display: flex; gap: 8px; }

#hud {
  flex: none;
  display: flex;
  gap: 8px;
}

.hud-cell {
  flex: 1;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 5px 8px;
  text-align: center;
  min-width: 0;
}

.hud-label {
  display: block;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--text-dim);
}

.hud-value {
  display: block;
  font-size: 19px;
  font-weight: 900;
  color: #fff;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}
#hud-right-value.combo-hot { color: var(--gold); text-shadow: 0 0 12px rgba(255, 215, 106, 0.7); }
#hud-left-value.time-low { color: var(--danger); }

/* ---------- 戦場 ---------- */

/* 戦場と呪文パネルのまとまり。通常は縦積み、横向きの低い画面では横並びにする */
#stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#battlefield {
  position: relative;
  flex: 1;
  min-height: 132px;
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  overflow: hidden;
  background:
    radial-gradient(120% 80% at 50% 120%, rgba(139, 123, 255, 0.22) 0%, rgba(11, 10, 28, 0) 62%),
    linear-gradient(180deg, #14102f 0%, #0a0819 100%);
}

#stars {
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(1.4px 1.4px at 12% 22%, rgba(255, 255, 255, 0.85) 50%, transparent 51%),
    radial-gradient(1.2px 1.2px at 32% 12%, rgba(200, 220, 255, 0.7) 50%, transparent 51%),
    radial-gradient(1.6px 1.6px at 58% 26%, rgba(255, 240, 200, 0.8) 50%, transparent 51%),
    radial-gradient(1.2px 1.2px at 78% 15%, rgba(255, 255, 255, 0.7) 50%, transparent 51%),
    radial-gradient(1.4px 1.4px at 88% 34%, rgba(190, 175, 255, 0.8) 50%, transparent 51%),
    radial-gradient(1.1px 1.1px at 22% 42%, rgba(255, 255, 255, 0.5) 50%, transparent 51%),
    radial-gradient(1.3px 1.3px at 68% 46%, rgba(255, 255, 255, 0.45) 50%, transparent 51%);
  animation: twinkle 5s ease-in-out infinite;
  pointer-events: none;
}
@keyframes twinkle { 0%, 100% { opacity: 0.55; } 50% { opacity: 1; } }

#fx-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
}

#enemy-area {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  /* 下は左下のプレイヤー表示ぶんを空けておく */
  padding: 8px 8px 44px;
  z-index: 2;
}

#enemy-wrap {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 残りの高さいっぱいに広がり、狭いときは縮む（画像もそれに追従する） */
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
}

#enemy-img {
  width: auto;
  height: 100%;
  max-height: min(32vh, 240px);
  max-width: min(56vw, 264px);
  object-fit: contain;
  filter: drop-shadow(0 6px 16px rgba(0, 0, 0, 0.6));
  animation: float 3.4s ease-in-out infinite;
}
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }

/* 結果画面の表示中は、ぼかしの下で回り続ける無限アニメーションを止める
   （下が動くと全画面の backdrop-filter を毎フレーム描き直すことになる） */
body.result-shown #stars,
body.result-shown #enemy-img { animation-play-state: paused; }

#enemy-circle {
  position: absolute;
  bottom: -8px;
  left: 50%;
  width: 150px;
  height: 34px;
  margin-left: -75px;
  border-radius: 50%;
  border: 2px solid rgba(139, 123, 255, 0.55);
  box-shadow: 0 0 22px rgba(139, 123, 255, 0.45), inset 0 0 18px rgba(139, 123, 255, 0.35);
  background: radial-gradient(closest-side, rgba(139, 123, 255, 0.28), rgba(139, 123, 255, 0) 78%);
  pointer-events: none;
}

#enemy-wrap.hit #enemy-img { animation: enemy-hit 0.28s ease; }
@keyframes enemy-hit {
  0% { transform: translateX(0); filter: brightness(3) drop-shadow(0 0 12px #fff); }
  25% { transform: translateX(-9px) rotate(-3deg); }
  55% { transform: translateX(7px) rotate(2deg); }
  100% { transform: translateX(0); filter: none; }
}

#enemy-wrap.dead #enemy-img { animation: enemy-dead 0.5s ease forwards; }
@keyframes enemy-dead {
  0% { opacity: 1; transform: scale(1) rotate(0); filter: brightness(2.4); }
  100% { opacity: 0; transform: scale(0.4) rotate(22deg) translateY(18px); filter: brightness(4); }
}

#enemy-wrap.enter #enemy-img { animation: enemy-enter 0.42s cubic-bezier(0.2, 1.2, 0.4, 1); }
@keyframes enemy-enter {
  0% { opacity: 0; transform: translateX(64px) scale(0.7); }
  100% { opacity: 1; transform: translateX(0) scale(1); }
}

#enemy-info { text-align: center; flex: none; }

#enemy-name {
  font-size: 14px;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
  letter-spacing: 0.04em;
}
#enemy-name.boss { color: var(--gold); text-shadow: 0 0 14px rgba(255, 215, 106, 0.7); }

#enemy-hp {
  display: flex;
  gap: 3px;
  justify-content: center;
  margin-top: 3px;
  min-height: 6px;
}
.hp-pip {
  width: 16px;
  height: 5px;
  border-radius: 3px;
  background: var(--danger);
  box-shadow: 0 0 8px rgba(255, 90, 106, 0.6);
}
.hp-pip.gone { background: rgba(255, 255, 255, 0.16); box-shadow: none; }

#enemy-cast {
  flex: none;
  width: min(320px, 84%);
  text-align: center;
}

#enemy-cast-bar {
  height: 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  overflow: hidden;
}

#enemy-cast-fill {
  display: block;
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #ff9a5a, #ff5a6a);
  transform-origin: left center;
}
#enemy-cast.urgent #enemy-cast-fill { animation: urgent 0.4s steps(2) infinite; }
@keyframes urgent { 0% { opacity: 1; } 100% { opacity: 0.45; } }

#enemy-cast-label {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  margin-top: 2px;
}

#player-area {
  position: absolute;
  left: 8px;
  bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
  z-index: 3;
}

#wizard-img {
  width: 56px;
  height: 56px;
  object-fit: contain;
  filter: drop-shadow(0 3px 10px rgba(0, 0, 0, 0.6));
}
#player-area.cast #wizard-img { animation: wiz-cast 0.32s ease; }
@keyframes wiz-cast {
  0% { transform: scale(1); }
  40% { transform: scale(1.14) translateY(-4px); filter: drop-shadow(0 0 14px var(--mana)); }
  100% { transform: scale(1); }
}
#player-area.hurt #wizard-img { animation: wiz-hurt 0.4s ease; }
@keyframes wiz-hurt {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); filter: brightness(0.5) sepia(1) hue-rotate(-30deg); }
  75% { transform: translateX(5px); }
}

#player-life { display: flex; flex-direction: column; gap: 2px; }
.life-row { display: flex; gap: 2px; }
.life-pip {
  width: 11px;
  height: 11px;
  border-radius: 3px;
  background: linear-gradient(180deg, #ff8fa0, #e23a52);
  box-shadow: 0 0 8px rgba(255, 90, 106, 0.5);
}
.life-pip.gone { background: rgba(255, 255, 255, 0.14); box-shadow: none; }

/* 画面フラッシュ（被弾） */
#battlefield.flash-hurt::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255, 60, 80, 0.35);
  animation: fade-out 0.35s ease forwards;
  z-index: 5;
  pointer-events: none;
}
@keyframes fade-out { to { opacity: 0; } }

/* ---------- 魔法エフェクト ---------- */

.bolt {
  position: absolute;
  width: 22px;
  height: 22px;
  margin: -11px 0 0 -11px;
  border-radius: 50%;
  background: radial-gradient(circle, #fff 8%, var(--fx) 45%, rgba(255, 255, 255, 0) 72%);
  box-shadow: 0 0 20px var(--fx), 0 0 44px var(--fx);
  animation: bolt-fly var(--dur) cubic-bezier(0.4, 0, 0.7, 1) forwards;
}
@keyframes bolt-fly {
  0% { transform: translate(0, 0) scale(0.5); opacity: 0; }
  12% { opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.5); opacity: 1; }
}

.burst {
  position: absolute;
  width: 26px;
  height: 26px;
  margin: -13px 0 0 -13px;
  border-radius: 50%;
  border: 3px solid var(--fx);
  box-shadow: 0 0 26px var(--fx);
  animation: burst-grow 0.45s ease-out forwards;
}
@keyframes burst-grow {
  0% { transform: scale(0.3); opacity: 1; }
  100% { transform: scale(4.6); opacity: 0; }
}

.spark {
  position: absolute;
  width: 6px;
  height: 6px;
  margin: -3px 0 0 -3px;
  border-radius: 50%;
  background: var(--fx);
  box-shadow: 0 0 10px var(--fx);
  animation: spark-fly 0.55s ease-out forwards;
}
@keyframes spark-fly {
  0% { transform: translate(0, 0) scale(1); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(0.2); opacity: 0; }
}

.dmg {
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: 24px;
  font-weight: 900;
  color: var(--fx);
  text-shadow: 0 0 12px var(--fx), 0 2px 4px rgba(0, 0, 0, 0.9);
  animation: dmg-pop 0.85s ease-out forwards;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
@keyframes dmg-pop {
  0% { transform: translate(-50%, -50%) scale(0.5); opacity: 0; }
  20% { transform: translate(-50%, -60%) scale(1.2); opacity: 1; }
  100% { transform: translate(-50%, -150%) scale(1); opacity: 0; }
}

/* ---------- 呪文パネル ---------- */

#spell-panel {
  flex: none;
  background: linear-gradient(180deg, rgba(34, 28, 70, 0.95), rgba(19, 16, 42, 0.95));
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 10px 12px 11px;
  text-align: center;
}

#spell-name {
  font-size: clamp(19px, 5.2vw, 28px);
  font-weight: 900;
  color: var(--gold);
  letter-spacing: 0.04em;
  line-height: 1.25;
  text-shadow: 0 0 18px rgba(255, 215, 106, 0.35);
  word-break: keep-all;
  overflow-wrap: anywhere;
}

#spell-kana {
  font-size: clamp(11px, 3vw, 14px);
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-top: 2px;
  overflow-wrap: anywhere;
}
#spell-kana .done { color: var(--mana-lit); }

#spell-romaji {
  font-family: var(--mono);
  font-size: clamp(15px, 4.4vw, 23px);
  font-weight: 700;
  letter-spacing: 0.02em;
  margin-top: 6px;
  line-height: 1.35;
  /* 英語の詠唱文はスペースで折り返す（単語の途中では切らない）。スペースも見た目どおり残す */
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
#spell-romaji .r-done { color: rgba(160, 150, 205, 0.5); }
#spell-romaji .r-next {
  color: #171034;
  background: var(--gold);
  border-radius: 4px;
  padding: 0 1px;
  box-shadow: 0 0 14px rgba(255, 215, 106, 0.6);
}
#spell-romaji .r-rest { color: #fff; }

#spell-panel.miss { animation: panel-miss 0.26s ease; }
@keyframes panel-miss {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-7px); box-shadow: 0 0 0 2px rgba(255, 90, 106, 0.6); }
  75% { transform: translateX(6px); }
}

#cast-bar {
  height: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
  margin-top: 8px;
}

#cast-fill {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--mana), var(--gold));
  box-shadow: 0 0 12px rgba(255, 215, 106, 0.5);
  transition: width 0.08s linear;
}

/* ---------- 画面キーボード ---------- */

#keypad {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 5px;
  /* 広告が出ているときは --ad-space が下のセーフエリアを含んでいるので、その分は重ねて空けない */
  padding: 6px 0 calc(6px + max(0px, env(safe-area-inset-bottom, 0px) - var(--ad-space, 0px)));
  user-select: none;
  -webkit-user-select: none;
}

.kp-row { display: flex; gap: 4px; justify-content: center; }
.kp-row.r2 { padding: 0 4.6%; }
.kp-row.r3 { padding: 0 4.6%; }
/* スペースキー（3段目の右端・幅広） */
.kp-key.kp-space { flex: 2.6; font-size: 12px; text-transform: none; letter-spacing: 0.04em; }

.kp-key {
  flex: 1;
  min-width: 0;
  height: 40px;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(60, 52, 112, 0.9), rgba(32, 27, 66, 0.9));
  color: var(--text);
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 700;
  text-transform: uppercase;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.kp-key:active, .kp-key.press {
  background: linear-gradient(180deg, #a596ff, #7a68f0);
  color: #14102c;
  transform: translateY(1px);
}
.kp-key.kp-next {
  background: linear-gradient(180deg, #ffe9a8, #ffd76a);
  color: #2a1d05;
  border-color: var(--gold);
  box-shadow: 0 0 12px rgba(255, 215, 106, 0.55);
}

/* ===================== 結果 / 遊び方 ===================== */

#result-overlay, #howto-overlay {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px) + var(--ad-space, 0px));
  background: rgba(6, 5, 18, 0.8);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}

#result-box, #howto-box {
  width: min(420px, 100%);
  /* 縦に短い画面でも上端が共通ヘッダー（--sgh-top）の下に潜らないようにする */
  max-height: min(84vh, calc(100% - 2 * var(--sgh-top, 0px)));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: linear-gradient(180deg, #2a2258, #171233);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 22px 18px 18px;
  text-align: center;
  box-shadow: 0 22px 60px rgba(0, 0, 0, 0.6);
}

#result-title {
  font-size: 22px;
  font-weight: 900;
  color: #fff;
  letter-spacing: 0.06em;
}

#result-sub {
  margin-top: 4px;
  font-size: 13px;
  font-weight: 800;
  color: var(--gold);
  text-shadow: 0 0 14px rgba(255, 215, 106, 0.6);
}

#result-main {
  margin: 12px 0 4px;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 4px;
}
#result-value {
  font-size: 46px;
  font-weight: 900;
  color: var(--gold);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  text-shadow: 0 0 22px rgba(255, 215, 106, 0.45);
}
#result-unit { font-size: 15px; font-weight: 800; color: var(--text-dim); }

#result-rankname {
  font-size: 15px;
  font-weight: 900;
  color: var(--mana-lit);
  margin-bottom: 8px;
}

#result-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin: 12px 0 16px;
}
.stat-cell {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
  padding: 7px 6px;
}
.stat-cell .k { display: block; font-size: 10px; font-weight: 800; color: var(--text-dim); letter-spacing: 0.08em; }
.stat-cell .v { display: block; font-size: 17px; font-weight: 900; color: #fff; font-variant-numeric: tabular-nums; }

#result-buttons { display: flex; gap: 8px; }

.btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  padding: 12px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 900;
  cursor: pointer;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  color: #170f36;
  background: linear-gradient(180deg, #c7b6ff, #8b7bff);
  box-shadow: 0 4px 16px rgba(139, 123, 255, 0.4);
}
.btn-sub {
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--panel-border);
}

.howto-title {
  margin: 0 0 10px;
  font-size: 19px;
  font-weight: 900;
  color: var(--gold);
}

#howto-list {
  margin: 0 0 14px;
  padding-left: 20px;
  text-align: left;
  font-size: 13px;
  line-height: 1.75;
  color: var(--text-dim);
}
#howto-list b { color: var(--mana-lit); }
#howto-list code, #howto-list b.key { font-family: var(--mono); }

/* ===================== トースト / フッター ===================== */

#toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(20px + env(safe-area-inset-bottom, 0px) + var(--ad-space, 0px));
  z-index: 130;
  background: rgba(20, 16, 44, 0.95);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  padding: 9px 16px;
  border-radius: 999px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}

#footer {
  flex: none;
  text-align: center;
  font-size: 11px;
  line-height: 1.8;
  color: var(--text-dim);
  padding: 14px 12px;
  padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px) + var(--ad-space, 0px));
}
#footer a { color: var(--mana-lit); }

/* ===================== レスポンシブ ===================== */

@media (min-width: 700px) {
  #enemy-img { max-width: 260px; }
  #wizard-img { width: 76px; height: 76px; }
  .life-pip { width: 13px; height: 13px; }
  #enemy-name { font-size: 16px; }
  .kp-key { height: 44px; font-size: 16px; }
}

/* 縦が短いとき（横向きスマホ等）は各パーツを詰める */
@media (max-height: 620px) {
  #hud { display: none; }
  #enemy-img { max-height: 132px; }
  #enemy-circle { width: 110px; margin-left: -55px; height: 26px; }
  #enemy-area { padding: 6px 8px 34px; }
  #wizard-img { width: 44px; height: 44px; }
  .life-pip { width: 9px; height: 9px; }
  .kp-key { height: 34px; font-size: 13px; }
  #spell-panel { padding: 7px 10px 8px; }
}

/* 横向きスマホ: 戦場と呪文パネルを左右に並べ、下にキーボードを敷く */
@media (max-height: 540px) and (min-width: 620px) {
  #screen-game { gap: 6px; padding: 6px 10px 0; }
  #stage { flex-direction: row; gap: 8px; }
  #battlefield { flex: 1 1 46%; min-height: 0; }
  #spell-panel {
    flex: 1 1 54%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-width: 0;
  }
  #enemy-img { max-height: min(26vh, 104px); max-width: 40vw; }
  #enemy-area { padding: 4px 6px 28px; gap: 3px; }
  #enemy-circle { width: 92px; margin-left: -46px; height: 22px; }
  #enemy-name { font-size: 12px; }
  #enemy-cast-label { font-size: 9px; }
  #wizard-img { width: 34px; height: 34px; }
  #player-area { left: 6px; bottom: 4px; gap: 4px; }
  .icon-btn { width: 32px; height: 32px; font-size: 16px; border-radius: 10px; }
  #keypad { gap: 4px; padding: 4px 0 calc(4px + env(safe-area-inset-bottom, 0px)); }
  .kp-key { height: 28px; font-size: 12px; border-radius: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  #enemy-img, #stars { animation: none !important; }
  .bolt, .burst, .spark, .dmg { animation-duration: 0.01s !important; }
}

/* ---------- 全国ランキング（SGRanking 共通モジュール） ---------- */
.sgr-hidden {
  display: none !important;
}

.sgr-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: rgba(6, 5, 18, 0.82);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.sgr-panel {
  width: min(420px, 100%);
  max-height: min(80vh, 640px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: linear-gradient(180deg, #2a2258, #171233);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 20px 18px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.sgr-title {
  margin: 0;
  font-size: 20px;
  font-weight: 900;
  text-align: center;
}

.sgr-tabs {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.sgr-tab {
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 6px 14px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  cursor: pointer;
}

.sgr-tab-active {
  background: rgba(139, 123, 255, 0.24);
  border-color: var(--mana);
  color: #fff;
}

.sgr-register-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 800;
  color: var(--gold);
  text-align: center;
}

.sgr-form {
  display: flex;
  gap: 8px;
}

.sgr-name {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 16px;
  padding: 8px 10px;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.35);
  color: var(--text);
}

.sgr-name:focus {
  outline: none;
  border-color: var(--mana);
}

.sgr-submit {
  border: none;
  border-radius: 10px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  background: linear-gradient(180deg, #c7b6ff, #8b7bff);
  color: #170f36;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(139, 123, 255, 0.35);
}

.sgr-submit:hover {
  filter: brightness(1.08);
}

.sgr-submit:disabled {
  opacity: 0.5;
  cursor: default;
}

.sgr-error {
  margin: 4px 0 0;
  font-size: 12px;
  font-weight: 700;
  color: var(--danger);
}

.sgr-status {
  margin: 0;
  font-size: 13px;
  color: var(--text-dim);
  text-align: center;
}

.sgr-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
}

.sgr-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 14px;
}

.sgr-row:nth-child(even) {
  background: rgba(255, 255, 255, 0.05);
}

.sgr-row.sgr-row-me {
  background: rgba(139, 123, 255, 0.26);
  color: #fff;
}

.sgr-rank {
  width: 2em;
  text-align: right;
  font-weight: 800;
  flex-shrink: 0;
}

.sgr-row-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sgr-row-score {
  font-weight: 800;
  color: var(--gold);
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
  font-size: 13px;
}

.sgr-row.sgr-row-me .sgr-row-score {
  color: #fff;
}

.sgr-close {
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  cursor: pointer;
}
.sgr-close:active { transform: scale(0.97); }

.sgr-cta {
  margin: 0 0 14px;
  text-align: center;
}

.sgr-cta-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 800;
  color: var(--gold);
}

.sgr-cta-btn {
  border: none;
  border-radius: 12px;
  padding: 10px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  background: linear-gradient(180deg, #ffe9a8, #ffd76a);
  color: #2a1d05;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(255, 215, 106, 0.35);
}
.sgr-cta-btn:active { transform: scale(0.97); }

.sgr-cta-link {
  border: none;
  background: none;
  color: var(--gold);
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  text-decoration: underline;
  cursor: pointer;
}

/* ==========================================================================
   フッター固定バナー広告（スマホ表示時のみ / js/ad-banner.js が挿入する）
   --ad-h    : 広告そのものの高さ。広告なしのときは 0px
   --ad-space: レイアウトが空けるべき高さ（広告 + セーフエリア）
   ========================================================================== */
:root {
  --ad-h: 0px;
  --ad-space: 0px;
}
html.has-ad-banner {
  --ad-space: calc(var(--ad-h, 0px) + env(safe-area-inset-bottom, 0px));
}

.ad-banner {
  /* height は広告そのものの高さ。セーフエリアぶんの padding は外側に足す */
  box-sizing: content-box;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  /* ランキング・リザルト等の全画面オーバーレイ（2147483000）よりは下に置く */
  z-index: 2147482000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  height: var(--ad-h, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  overflow: hidden;
  background: rgba(14, 16, 20, 0.86);
  /* border ではなく inset shadow にして、枠線ぶん高さがずれないようにする */
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.ad-banner__frame {
  display: block;
  width: 100%;
  height: var(--ad-h, 0px);
  border: 0;
  color-scheme: light;
}

@media print {
  .ad-banner { display: none; }
}
