/* =====================================================================
 *  KUNG-FU MASTER - スマホ横画面メイン
 * ===================================================================== */
:root {
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --btn: clamp(58px, 21vh, 92px);
  --gold: #ffd23a;
  --red: #e8382f;
  --font-jp: "DotGothic16", "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Meiryo", sans-serif;
  --font-en: "Press Start 2P", "DotGothic16", monospace;
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  color: #fff;
  font-family: var(--font-jp);
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
body { position: fixed; inset: 0; }

#app { position: fixed; inset: 0; overflow: hidden; }

button {
  font-family: inherit;
  color: inherit;
  border: none;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
canvas { -webkit-touch-callout: none; }

.hidden { display: none !important; }

/* ===== ゲーム画面 ===== */
/* 背景・敵はドット絵（pixelated）、プレイヤーの写真は #game-hi に高解像度で描画 */
#game, #game-hi {
  position: absolute;
  left: 0; top: 0;
  width: 100%;
  height: 100%;
}
#game {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  touch-action: none;
}
#game-hi { pointer-events: none; }

/* ===== HUD ===== */
#hud {
  position: absolute;
  top: calc(var(--sat) + 6px);
  left: calc(var(--sal) + 10px);
  right: calc(var(--sar) + 10px);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
  z-index: 10;
  text-shadow: 2px 2px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000;
}
.hud-left, .hud-right, .hud-center {
  display: flex; align-items: center; gap: 8px;
  background: rgba(0, 0, 0, 0.45);
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-radius: 8px;
  padding: 4px 8px;
}
.hud-center { flex-direction: column; gap: 2px; padding: 3px 12px; }
.hud-icon-wrap {
  width: 36px; height: 36px;
  border: 2px solid var(--gold);
  border-radius: 6px;
  background: #3a6ac8;
  overflow: hidden;
}
#hud-icon { width: 100%; height: 100%; display: block; }
#hud-name { font-size: 15px; line-height: 1.1; }
.hpbar {
  width: clamp(90px, 18vw, 170px); height: 12px;
  background: #400; border: 2px solid #fff; margin-top: 3px;
  border-radius: 2px; overflow: hidden;
}
#hp-fill { height: 100%; width: 100%; background: linear-gradient(#7dff6a, #2fbf2f); transition: width 0.15s; }
#hp-fill.low { background: linear-gradient(#ffb36a, #ff3a2a); }
#hud-stage { font-family: var(--font-en); font-size: 10px; color: var(--gold); }
#hud-time { font-family: var(--font-en); font-size: 13px; }
#hud-time.warn { color: #ff5050; animation: blink 0.5s steps(2) infinite; }
#hud-score { font-family: var(--font-en); font-size: 9px; line-height: 1.5; text-align: right; }
#score-num { font-size: 12px; }
.hud-btn {
  pointer-events: auto;
  width: 40px; height: 40px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.18);
  border: 2px solid rgba(255, 255, 255, 0.6);
  font-family: var(--font-en);
  font-size: 13px;
  display: flex; align-items: center; justify-content: center;
}
.hud-btn.off { opacity: 0.6; }
#boss-bar {
  position: absolute;
  top: 54px; left: 50%;
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px;
  background: rgba(60, 0, 0, 0.6);
  border: 2px solid #ff8080;
  border-radius: 8px;
  padding: 3px 10px;
}
.boss-name { font-size: 14px; white-space: nowrap; }
.bossbar { width: clamp(140px, 32vw, 300px); height: 12px; background: #220; border: 2px solid #fff; overflow: hidden; }
#boss-fill { height: 100%; width: 100%; background: linear-gradient(#ff8a3a, #d0201a); transition: width 0.15s; }

/* ===== バナー（STAGE 1 など） ===== */
#banner {
  position: absolute;
  left: 0; right: 0;
  top: calc(var(--game-top, 0px) + var(--game-h, 100vh) * 0.22);
  text-align: center;
  pointer-events: none;
  z-index: 12;
  opacity: 0;
  transition: opacity 0.2s;
  text-shadow: 3px 3px 0 #000, -2px -2px 0 #000, 2px -2px 0 #000, -2px 2px 0 #000;
}
#banner.show { opacity: 1; animation: pop 0.35s ease-out; }
#banner .b1 { font-family: var(--font-en); font-size: clamp(18px, 6vmin, 34px); color: var(--gold); }
#banner .b2 { font-size: clamp(22px, 8.5vmin, 56px); margin-top: 6px; padding: 0 8px; }
#banner .b3 { font-size: clamp(13px, 4.2vmin, 22px); margin-top: 6px; color: #dff; padding: 0 8px; }
#banner.warn .b1 { color: #ff4040; animation: blink 0.3s steps(2) infinite; }

/* ===== タッチボタン ===== */
#controls { position: absolute; inset: 0; pointer-events: none; z-index: 20; }
.pad {
  position: absolute;
  bottom: calc(var(--sab) + 12px);
  pointer-events: auto;
  touch-action: none;
  display: grid;
  gap: 10px;
}
.pad-left {
  left: calc(var(--sal) + 14px);
  grid-template-columns: repeat(2, calc(var(--btn) * 1.12));
  grid-template-rows: calc(var(--btn) * 1.08);
}
.pad-right {
  right: calc(var(--sar) + 14px);
  grid-template-columns: repeat(2, var(--btn));
  grid-template-rows: repeat(2, var(--btn));
  grid-template-areas: ". jump" "punch kick";
}
.btn {
  width: 100%; height: 100%;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.85);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.45), inset 0 -6px 0 rgba(0, 0, 0, 0.18);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  pointer-events: auto;
  touch-action: none;
  opacity: 0.74;
  transition: transform 0.05s, filter 0.05s;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.6);
}
.btn .ico { font-size: calc(var(--btn) * 0.32); line-height: 1; }
.btn .lbl { font-family: var(--font-en); font-size: calc(var(--btn) * 0.13); margin-top: 4px; }
.btn-move { border-radius: 18px; background: rgba(40, 60, 110, 0.7); font-size: calc(var(--btn) * 0.42); }
.btn-jump { grid-area: jump; background: rgba(40, 170, 80, 0.8); }
.btn-punch { grid-area: punch; background: rgba(220, 50, 50, 0.82); }
.btn-kick { grid-area: kick; background: rgba(40, 110, 230, 0.82); }
.btn.pressed { transform: scale(0.9) translateY(3px); filter: brightness(1.45); opacity: 1; box-shadow: 0 1px 0 rgba(0, 0, 0, 0.45); }

/* ===== 共通スクリーン ===== */
.screen {
  position: absolute; inset: 0;
  z-index: 30;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: calc(var(--sat) + 6px) calc(var(--sar) + 12px) calc(var(--sab) + 6px) calc(var(--sal) + 12px);
}
.screen.dim { background: rgba(0, 0, 0, 0.7); }
.panel {
  background: #1a1a3a;
  border: 4px solid #fff;
  box-shadow: 0 0 0 4px #000, 0 0 0 7px var(--gold);
  border-radius: 6px;
  padding: 18px 28px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  max-width: 92vw;
}
.panel-title { font-family: var(--font-en); font-size: clamp(20px, 7vh, 32px); color: var(--gold); }
.panel-title.red { color: #ff4a4a; }
.panel-msg { font-size: 18px; }
.big-btn {
  font-family: var(--font-en);
  font-size: clamp(14px, 4.6vh, 22px);
  padding: 0.75em 1.6em;
  background: linear-gradient(#ff5a4a, #c81e1e);
  border: 3px solid #fff;
  border-radius: 10px;
  box-shadow: 0 5px 0 #6a0a0a, 0 0 0 3px #000;
  text-shadow: 2px 2px 0 #000;
  min-height: 48px;
}
.big-btn:active { transform: translateY(3px); box-shadow: 0 2px 0 #6a0a0a, 0 0 0 3px #000; }
.big-btn.small { font-size: clamp(12px, 3.6vh, 16px); font-family: var(--font-jp); }
#pause .big-btn, #gameover .big-btn { font-family: var(--font-jp); font-size: clamp(16px, 5vh, 22px); }
.sub-btn {
  font-size: 16px;
  padding: 10px 22px;
  border: 2px solid #aaa;
  border-radius: 8px;
  background: #333;
  min-height: 44px;
}

/* ===== タイトル / キャラクター選択（モダンUI） ===== */
#title {
  --ui-font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", "Meiryo", sans-serif;
  --accent: #2f6bff;
  font-family: var(--ui-font);
  background: #f3f5fa;
  color: #1d2433;
  justify-content: space-between;
  gap: clamp(6px, 2vh, 14px);
  padding: calc(var(--sat) + clamp(8px, 2.5vh, 18px)) calc(var(--sar) + 16px) calc(var(--sab) + clamp(8px, 2.5vh, 16px)) calc(var(--sal) + 16px);
}
.title-bg { display: none; }
.title-logo { text-align: center; line-height: 1.15; }
.logo-main {
  font-family: var(--ui-font);
  font-weight: 800;
  font-size: clamp(20px, 6.5vh, 34px);
  letter-spacing: 0.04em;
  color: #1d2433;
}
.title-bgm {
  position: absolute;
  top: calc(var(--sat) + 10px);
  right: calc(var(--sar) + 12px);
  font-family: var(--ui-font);
  font-size: 13px;
  font-weight: 700;
  color: #2f6bff;
  background: #fff;
  border: 2px solid #d5deef;
  border-radius: 999px;
  padding: 6px 12px;
  min-height: 34px;
  z-index: 2;
}
.title-bgm.off { color: #8a93a6; }
.logo-sub { font-size: clamp(12px, 3.4vh, 15px); font-weight: 600; color: #6b7385; margin-top: 2px; }

.cards {
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: clamp(10px, 2.5vw, 24px);
  width: 100%;
  max-width: 980px;
  max-height: 560px;
  flex: 1;
  min-height: 0;
}
.card {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  max-width: 280px;
  background: #ffffff;
  border: 2px solid #e3e7ef;
  border-radius: 20px;
  box-shadow: 0 4px 14px rgba(20, 30, 60, 0.08);
  padding: clamp(8px, 2vh, 14px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(2px, 0.8vh, 6px);
  cursor: pointer;
  touch-action: manipulation;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s, transform 0.12s;
}
.card:active { transform: scale(0.98); }
.card.selected {
  border-color: var(--accent);
  background: #eef4ff;
  box-shadow: 0 6px 20px rgba(47, 107, 255, 0.22);
}
.card-check {
  position: absolute;
  top: 10px; right: 10px;
  z-index: 2;
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 800;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 6px rgba(47, 107, 255, 0.4);
  opacity: 0;
  transform: scale(0.6);
  transition: opacity 0.15s, transform 0.15s;
}
.card.selected .card-check { opacity: 1; transform: scale(1); }
.card-img {
  flex: 1;
  min-height: 0;
  width: 100%;
  position: relative;
  border-radius: 14px;
  background: #f1f3f8;
  overflow: hidden;
  transition: background 0.15s;
}
.card.selected .card-img { background: #dde8ff; }
.card-img canvas {
  position: absolute;
  left: 4px; right: 4px; top: 4px; bottom: 0;
  width: calc(100% - 8px);
  height: calc(100% - 4px);
  object-fit: contain;
  object-position: 50% 100%;
}
.card-name { font-size: min(clamp(16px, 4.8vh, 22px), 4.2vw); font-weight: 800; margin-top: 2px; white-space: nowrap; }
.card-feature { font-size: clamp(12px, 3.4vh, 15px); color: #5b6475; text-align: center; line-height: 1.3; }
.card-play {
  width: 100%;
  min-height: 44px;
  margin-top: 2px;
  padding: 8px;
  border-radius: 12px;
  border: 2px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font-family: var(--ui-font);
  font-weight: 700;
  font-size: min(clamp(14px, 4vh, 17px), 3.4vw);
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.card.selected .card-play { background: #fff; color: var(--accent); }

.title-bottom { display: flex; align-items: center; gap: 16px; }
#select-hint { font-size: clamp(13px, 3.6vh, 16px); font-weight: 600; color: #6b7385; }
#title #btn-start {
  font-family: var(--ui-font);
  margin-bottom: 2px;
  font-weight: 800;
  font-size: clamp(16px, 4.8vh, 22px);
  letter-spacing: 0.06em;
  color: #fff;
  background: #ff5a3c;
  border: none;
  border-radius: 999px;
  padding: 0.6em 2.4em;
  min-height: 48px;
  text-shadow: none;
  box-shadow: 0 6px 16px rgba(255, 90, 60, 0.35);
  transition: background 0.15s, box-shadow 0.15s, transform 0.1s;
}
#title #btn-start:active { transform: translateY(1px); box-shadow: 0 3px 10px rgba(255, 90, 60, 0.35); }
#title #btn-start[disabled] { background: #c9ced8; box-shadow: none; cursor: default; transform: none; }

@media (max-height: 440px) {
  .logo-sub { display: none; }
  .logo-main { font-size: clamp(18px, 5.6vh, 26px); }
  #title { gap: 8px; }
  .card { gap: 2px; padding: 8px; }
  .card-play { min-height: 42px; padding: 6px; }
}
@media (max-width: 640px) {
  .cards { gap: 8px; }
  .card { padding: 6px; border-radius: 14px; }
  .card-feature { font-size: max(11px, 2.6vw); }
  .card-play { white-space: normal; line-height: 1.2; padding: 6px 4px; }
  #select-hint { display: none; }
}

/* ===== エンディング ===== */
#ending {
  background: linear-gradient(#2a6ad8, #7ac8ff 60%, #3a9a3a 60%, #3a9a3a 66%, #8a5a2b 66%);
  justify-content: space-around;
  overflow: hidden;
}
#confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 0; }
#ending > *:not(#confetti) { position: relative; z-index: 1; }
.ending-title {
  font-family: var(--font-en);
  font-size: clamp(24px, 10vh, 54px);
  color: var(--gold);
  text-shadow: 4px 4px 0 #c81e1e, 7px 7px 0 #000;
  animation: pop 0.6s ease-out, bob 1.6s ease-in-out 0.6s infinite;
}
.ending-chars {
  display: flex; align-items: flex-end; justify-content: center;
  gap: clamp(20px, 6vw, 70px);
  height: 46vh;
}
.ending-char { position: relative; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; }
.ending-char canvas { height: 84%; width: auto; transform-origin: 50% 100%; }
.ending-char .ename { font-size: clamp(13px, 4vh, 18px); text-shadow: 2px 2px 0 #000; margin-top: 2px; }
.ending-char .bubble {
  position: absolute; top: -6%;
  background: #fff; color: #000;
  font-size: clamp(11px, 3.4vh, 15px);
  padding: 4px 8px; border-radius: 10px; border: 2px solid #000;
  white-space: nowrap;
  opacity: 0;
  animation: fadein 0.4s 1.2s forwards;
}
.ending-char .crown { display: inline-block; margin-right: 4px; animation: bob 0.8s ease-in-out infinite; }
.ending-char.jump canvas { animation: jump 0.7s ease-in-out infinite; }
.ending-char.wave canvas { animation: wave 0.5s ease-in-out infinite alternate; }
.ending-char.cheer canvas { animation: cheer 0.45s ease-in-out infinite alternate; }
.ending-char .hand { position: absolute; font-size: clamp(16px, 5vh, 26px); top: 22%; right: 0; animation: handwave 0.35s ease-in-out infinite alternate; transform-origin: 50% 100%; }
.ending-thanks {
  font-family: var(--font-en);
  font-size: clamp(11px, 4vh, 20px);
  text-shadow: 3px 3px 0 #000;
  opacity: 0;
  animation: fadein 1s 1.8s forwards, blink 1.4s 3s steps(2) infinite;
}
.ending-score { font-family: var(--font-en); font-size: clamp(10px, 3vh, 14px); text-shadow: 2px 2px 0 #000; }

/* ===== ローディング ===== */
#loading { background: #000; z-index: 50; }
.loading-text { font-family: var(--font-en); font-size: 16px; animation: blink 0.8s steps(2) infinite; }

/* ===== 縦画面（上：ゲーム画面 / 下：操作ボタン） ===== */
html.is-portrait #app { background: #15172a; }
html.is-portrait { --btn: min(20vw, 92px, 13vh); }
html.is-portrait .pad { bottom: calc(var(--sab) + 20px); }
html.is-portrait .pad-left {
  left: calc(var(--sal) + 12px);
  grid-template-columns: repeat(2, calc(var(--btn) * 1.05));
  grid-template-rows: calc(var(--btn) * 1.2);
}
html.is-portrait .pad-right { right: calc(var(--sar) + 12px); }
html.is-portrait .btn { opacity: 0.95; }
html.is-portrait #hud { left: calc(var(--sal) + 6px); right: calc(var(--sar) + 6px); }
html.is-portrait .hud-left, html.is-portrait .hud-right { gap: 5px; padding: 3px 6px; }
html.is-portrait .hud-center { padding: 3px 6px; }
html.is-portrait .hud-icon-wrap { width: 32px; height: 32px; }
html.is-portrait #hud-name { font-size: 12px; }
html.is-portrait .hpbar { width: 20vw; min-width: 60px; }
html.is-portrait #hud-stage { font-size: 8px; }
html.is-portrait #hud-time { font-size: 10px; }
html.is-portrait #hud-score { font-size: 7px; }
html.is-portrait #score-num { font-size: 9px; }
html.is-portrait .hud-btn { width: 32px; height: 32px; font-size: 11px; }
html.is-portrait #boss-bar { top: calc(var(--game-top, 60px) - var(--sat) - 2px); }
html.is-portrait .bossbar { width: 44vw; }
html.is-portrait .ending-chars { height: min(46vh, 62vw); gap: 4vw; margin-top: 6vh; }
html.is-portrait .ending-title { font-size: min(9vw, 54px); text-align: center; }
html.is-portrait .ending-thanks { font-size: min(3.6vw, 20px); text-align: center; }
html.is-portrait .ending-char .bubble { font-size: min(3.2vw, 15px); white-space: normal; width: 24vw; text-align: center; top: -14%; }
html.is-portrait .ending-char .ename { font-size: min(3.8vw, 18px); white-space: nowrap; }

/* ===== アニメーション ===== */
@keyframes blink { 50% { opacity: 0.25; } }
@keyframes pop { 0% { transform: scale(0.4); } 70% { transform: scale(1.12); } 100% { transform: scale(1); } }
@keyframes glow {
  from { box-shadow: 0 0 6px 2px rgba(255, 210, 58, 0.6); }
  to { box-shadow: 0 0 22px 8px rgba(255, 210, 58, 0.95); }
}
@keyframes hop { 50% { transform: translateY(-6%); } }
@keyframes pulse { 50% { transform: scale(1.07); } }
@keyframes bob { 50% { transform: translateY(-8px); } }
@keyframes jump {
  0%, 100% { transform: translateY(0) scale(1.06, 0.94); }
  15% { transform: translateY(0) scale(1, 1); }
  50% { transform: translateY(-30%) scale(0.96, 1.05); }
  85% { transform: translateY(0) scale(1, 1); }
}
@keyframes wave { from { transform: rotate(-6deg); } to { transform: rotate(6deg); } }
@keyframes handwave { from { transform: rotate(-30deg); } to { transform: rotate(30deg); } }
@keyframes cheer { from { transform: scale(1, 1) translateY(0); } to { transform: scale(1.06, 0.95) translateY(-6%); } }
@keyframes fadein { to { opacity: 1; } }
html.is-portrait .title-logo { padding-top: 40px; }
