/* 作成日: 2026-11-08 */

/* ============================================================
   もくじ
   1. 全体
   2. 画面の切り替えと、画面共通の文字
   3. ボタン
   4. ゲーム画面（ヘッダー・バー・プレイエリア・メッセージ・仲間）
   5. プレイエリアの中身（鬼・桃太郎・仲間・次に来る鬼）
   6. クリア画面
   7. タイトル・ゲームオーバー・エンディング画面
   8. アニメーション（CSS だけ。JavaScript・setTimeout は使わない）
   9. 動きを減らす設定のときの対応
   ============================================================ */

/* ---------- 1. 全体 ---------- */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #2d3a4a; /* 画像が読み込めないときの色 */
  background-image: url('img/img_bg_pc.jpg'); /* PCで見たときの左右の余白 */
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  font-family: sans-serif;
  color: #222;
}

/* プレイ画面の枠：最大幅430pxで中央寄せ、高さは画面いっぱい */
#app {
  max-width: 430px;
  height: 100vh;   /* dvh非対応ブラウザ向け */
  height: 100dvh;
  margin: 0 auto;
  background: #fff8e7;
  position: relative;
}

/* ---------- 2. 画面の切り替えと、画面共通の文字 ---------- */
/* 通常は非表示。is-active が付いた画面だけ表示する */
.screen {
  display: none;
  height: 100%;
  padding: 16px;
}

.screen.is-active {
  display: flex;
  flex-direction: column;
}

/* タイトル・クリア・ゲームオーバー・エンディングは中央に縦並び */
#screen-title.is-active,
#screen-clear.is-active,
#screen-gameover.is-active,
#screen-ending.is-active {
  justify-content: center;
  align-items: center;
  text-align: center;
  gap: 20px;
}

.title-text {
  font-size: 36px;
  margin: 0;
}

.screen-title {
  font-size: 28px;
  margin: 0;
}

.description {
  font-size: 16px;
  line-height: 1.7;
  margin: 0;
}

/* ---------- 3. ボタン ---------- */
/* 共通：押した瞬間に少し縮んで反応が分かる。長押しで文字選択やダブルタップ拡大をさせない */
.btn {
  border: none;
  border-radius: 12px;
  font-size: 18px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.05s;
}

.btn:active {
  transform: scale(0.96);
  filter: brightness(0.9);
}

/* 大きいボタン（スタート・リトライ・次のステージへ・タイトルへ）。
   タップすると、押している間だけふわっと大きくなる（上の .btn:active より後ろに書いて、こちらを優先する） */
.btn-main {
  width: 80%;
  height: 56px;
  background: #F58F98; /* 桃太郎の桃色 */
  color: #fff;
}

.btn-sub {
  width: 80%;
  height: 48px;
  background: #fff;
  color: #222;
}

.btn-main,
.btn-sub {
  transition: transform 0.25s ease-out;
}

.btn-main:active,
.btn-sub:active {
  transform: scale(1.08);
}

/* プレイ中の小さい [タイトルへ] */
.btn-small {
  height: 36px;
  padding: 0 12px;
  font-size: 14px;
  background: #F2DFBB;
  color: #222;
}

/* 移動ボタン（左によける／前に進む／右によける）。押しやすいよう高さ60px以上 */
.btn-lane {
  flex: 1;
  height: 64px;
  font-size: 16px;      /* 2行になっても収まる大きさ */
  font-weight: bold;
  line-height: 1.3;
  background: #fff;
  color: #222;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25); /* 明るい背景の上でも、ボタンの形が分かるように */
}

/* ---------- 4. ゲーム画面 ---------- */
#screen-game.is-active {
  gap: 12px;
  padding: 16px 8px; /* 左右の余白を狭めて、3マスを広くする */
}

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stage-name {
  font-size: 20px;
  font-weight: bold;
}

/* 進捗バー・体力ゲージ共通 */
.bar-label {
  font-size: 14px;
  margin-bottom: 4px;
}

.bar {
  height: 16px;
  background: #ddd;
  border-radius: 8px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: 0;
}

.progress-fill {
  background: #1e88e5;
}

.hp-fill {
  background: #43a047;
  width: 100%;
}

/* 背景画像のエリア：歩数バー・プレイエリア・移動ボタンをまとめて、背景を敷く。
   ゲーム画面の左右の余白（8px）を打ち消して、画面の端まで広げる */
.stage-area {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 0 -8px;
  padding: 12px 12px 14px;     /* 移動ボタンが背景の内側に収まるよう、内側に余白 */
  background-size: cover;      /* 背景画像は縦横比を保って全体を覆う */
  background-position: center;
}

/* 背景の上の歩数バー：暗い背景でも読めるよう、白い文字に影。バーの下地は白 */
.stage-area .bar-label {
  color: #fff;
  font-weight: bold;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}

.stage-area .bar {
  background: #fff;
}

/* プレイエリア：3マスを横に並べる（背景は stage-area に敷く）。左右は画面の端近くまで広げる */
.play-area {
  flex: 1;
  display: flex;
  gap: 4px;
  min-height: 120px;
  margin: 0 -8px;
  pointer-events: none;  /* 中の画像（ボタンに重なった雉など）がタップをじゃましないよう、タップは下のボタンに通す */
}

.lane {
  flex: 1 1 0;
  min-width: 0;         /* 中の画像が大きくても、3マスの幅を等しく保つ */
  container-type: size; /* マスの高さ（cqh）と幅（cqw）を、中の画像の位置と大きさの基準にする */
  overflow: visible;    /* 鬼や桃太郎は、マスの幅より大きくはみ出して表示してよい */
  position: relative;   /* 中の画像（鬼・桃太郎・次に来る鬼）は、マスを基準に置く（5. を参照） */
  background: transparent;        /* ふだんは枠も背景も透明（ぶつかったときだけ .is-hit で赤くなる） */
  border: 2px solid transparent;  /* 枠の太さは残して、赤くなったときにマスがずれないようにする */
  border-radius: 12px;
}

/* ぶつかったマスを赤く目立たせる（背景画像が見えるよう、半透明の赤） */
.lane.is-hit {
  border-color: #e53935;
  background: rgba(229, 57, 53, 0.25);
}

/* 桃太郎の情報：顔アイコンと右側の情報 */
.status-area {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.status-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* 顔の右側：見出し・詳細・仲間・体力を、左揃えで縦に並べる */
.status-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.face {
  flex: none;
  width: 68px;
  height: 68px;
  overflow: hidden;
  border-radius: 50%;
  background: #ffe082;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: bold;
}

/* 顔アイコンの状態：普通（黄）／痛い（赤）／嬉しい（緑） */
.face.mood-normal { background: #ffe082; }
.face.mood-hit    { background: #ef9a9a; }
.face.mood-happy  { background: #a5d6a7; }

/* 顔アイコンの画像：円の中に縦横比を保って収める */
.face-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* メッセージの見出し：大きい太字。状態の色が付く（当たりは赤、回避は緑） */
.message-head {
  margin: 0;
  font-size: 20px;
  font-weight: bold;
  line-height: 1.4;
}

.message-head.mood-hit   { color: #c62828; }
.message-head.mood-happy { color: #2e7d32; }

/* メッセージの詳細：ふつうの文字。空のときも1行ぶんの高さを取り、下の行がずれないようにする */
.message-detail {
  margin: 0;
  min-height: 1.5em;
  font-size: 15px;
  line-height: 1.5;
  color: #222;
}

/* 移動ボタン：雉など、プレイエリアからはみ出した画像よりも手前に表示する */
.controls {
  position: relative;
  z-index: 2;
  display: flex;
  gap: 8px;
}

/* 仲間表示：顔アイコンだけを横に並べる。仲間が0人でも高さを取り、プレイエリアの高さが変わらないようにする */
.allies {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
}

/* 体力ゲージ：ゲージ（伸びる）と数値（右）を1行に並べる */
.hp-area {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hp-area .bar {
  flex: 1;
}

.hp-label {
  flex: none;
  font-size: 14px;
  white-space: nowrap;
}

.ally-face {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: contain;
}

/* ---------- 5. プレイエリアの中身 ---------- */
/* マスの中は「3つの行」で考える。位置と大きさは、マスの高さ（cqh）と幅（cqw）を基準にした変数で決める。
   ・上の行　：次に来る鬼（中央のマスだけ）
   ・真ん中の行：判定（ぶつかる・よける）。鬼と桃太郎が、選ばれたマスに出る。同じマスなら重なる
   ・下　　　：待ち（ボタンを押す前）の桃太郎。移動ボタンの近く（雉の足元は、ボタンに少し重なってよい）
   位置を調整したいときは、ここの変数だけを書き換える */
.lane {
  --next-size:   min(70cqw, 16cqh);   /* 次に来る鬼の大きさ */
  --next-top:    1cqh;                /* 次に来る鬼の上端 */
  --enemy-size:  min(120cqw, 40cqh);  /* 鬼の大きさ（画像は正方形なので、幅＝高さ） */
  --enemy-top:   18cqh;               /* 判定のときの鬼の上端（次に来る鬼の下） */
  --boss-size:   min(210cqw, 60cqh);  /* ボスの大きさ（鬼の 1.5 倍） */
  --boss-top:    4cqh;                /* 判定のときのボスの上端（ボス戦は次の鬼を出さないので、上に寄せる） */
  --player-size: min(120cqw, 40cqh);  /* 桃太郎の大きさ（仲間なし） */
  --player-size-allies: min(135cqw, 36cqh); /* 桃太郎の大きさ（仲間あり。仲間の分だけ縦に場所を使う） */
  --player-current: var(--player-size);     /* 今の桃太郎の大きさ（仲間がいれば、下の .has-allies で切り替わる） */
  --wait-top:    58cqh;               /* 待ちのときの桃太郎の上端（移動ボタンの近く。雉の足元は、ボタンに少し重なってよい） */
  --lane-step:   calc(100cqw + 8px);  /* マス1つぶんの横の距離（中身の幅 100cqw ＋ 左右の枠 2px×2 ＋ マスのすき間 4px） */
}

/* 中央のマスから見て、そのマスが何マスぶん横にあるか（移動の動きの始まりの位置に使う）。
   左のマスは、中央から1マスぶん右（+1）からやって来る。右のマスは -1 */
#lane-0 { --lane-shift: 1; }
#lane-1 { --lane-shift: 0; }
#lane-2 { --lane-shift: -1; }

/* 仲間がいるときは、桃太郎の大きさを切り替える（かわした鬼の高さ合わせにも使う） */
.play-area.has-allies .lane {
  --player-current: var(--player-size-allies);
}

/* 鬼：判定の行に、マスの左右の真ん中で出す。マスの幅より少し大きくてよい。
   上端は --enemy-y で決める（移動の動きでも使う）。
   出てくるときは、上の行の「次に来る鬼」の位置から、小さい姿でサッとやって来る（8. の enemy-move-in） */
.enemy-img {
  --enemy-y: var(--enemy-top);
  --from-y: var(--next-top);   /* やって来る前の上端（次に来る鬼の位置） */
  --from-scale: 0.45;          /* やって来る前の大きさ（次に来る鬼くらいの小ささ） */
  position: absolute;
  top: var(--enemy-y);
  left: calc(50cqw - var(--enemy-size) / 2);
  width: var(--enemy-size);
  height: auto;
  object-fit: contain;
  transform-origin: top center;
  animation: enemy-move-in 0.25s ease-out;
}

/* ボス戦のボス：鬼より大きく、上に寄せて出す。画面の上の中央からやって来る */
.enemy-img.boss-img {
  --enemy-y: var(--boss-top);
  --from-y: 0px;
  --from-scale: 0.6;
  left: calc(50cqw - var(--boss-size) / 2);
  width: var(--boss-size);
}

/* かわしたとき：鬼を下げて、桃太郎と縦の真ん中をそろえる（横に並ぶ）。
   桃太郎は「鬼の上端＋鬼の高さの半分」に出るので、鬼を「桃太郎の高さの半分」だけ下げると、真ん中がそろう */
.enemy-img.is-avoided {
  --enemy-y: calc(var(--enemy-top) + var(--player-current) / 2);
}

.enemy-img.boss-img.is-avoided {
  --enemy-y: calc(var(--boss-top) + var(--player-current) / 2);
}

/* 画像がない敵のための、色付きの箱と名前（予備） */
.enemy-box {
  position: absolute;
  top: var(--enemy-top);
  left: 0;
  right: 0;
  margin: 0 auto;
  width: fit-content;
  padding: 8px 6px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: bold;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6); /* 明るい色でも名前が読めるように */
}

/* 桃太郎と、後ろをついてくる仲間たち（いつも鬼より手前）。
   仲間は桃太郎の少し後ろ（下）に置く。1体＝真後ろ／2体＝犬と猿が横並び／3体＝犬と猿の後ろに雉 */
.player-group {
  --size: var(--player-current);
  position: absolute;
  z-index: 1;
  left: calc(50cqw - var(--size) / 2);
  width: var(--size);
}

/* 待ち（ボタンを押す前）：中央のマスの下寄り */
.player-group.is-waiting {
  top: var(--wait-top);
}

/* 判定：鬼の高さの半分だけ下。同じマスに鬼がいれば、鬼の下半分に重なる。
   上端は --judge-top で決める（移動の動きでも使う）。
   待ちの位置（中央のマスの下寄り）から、選んだマスへサッと移動する（8. の player-move-in） */
.player-group.is-judging {
  --judge-top: calc(var(--enemy-top) + var(--enemy-size) / 2);
  top: var(--judge-top);
  animation: player-move-in 0.25s ease-out;
}

/* ボス戦の判定：ボスの高さの半分だけ下 */
.play-area.is-boss .player-group.is-judging {
  --judge-top: calc(var(--boss-top) + var(--boss-size) / 2);
}

.player-group .player-img {
  display: block;
  width: 100%;
  height: auto;
}

.player-group .ally-img {
  position: absolute;
  width: 45%;
  height: auto;
}

/* 位置：ally-pos-（仲間の人数）-（何番目か。0=犬, 1=猿, 2=雉） */
.ally-pos-1-0 { left: 27%; top: 76%; }
.ally-pos-2-0 { left: 2%;  top: 76%; }
.ally-pos-2-1 { left: 53%; top: 76%; }
.ally-pos-3-0 { left: 2%;  top: 74%; }
.ally-pos-3-1 { left: 53%; top: 74%; }
.ally-pos-3-2 { left: 27%; top: 100%; width: 42%; }

/* ぶつかったとき：サッと移動したあと（0.25秒後）に、桃太郎が震える（「8. アニメーション」の hit-shake） */
.lane.is-hit .player-group.is-judging {
  animation: player-move-in 0.25s ease-out, hit-shake 0.4s ease-in-out 0.25s;
}

/* 次に来る鬼：中央のマスの上の行（桃太郎から見て前方）に置く */
.next-enemy-img {
  position: absolute;
  top: var(--next-top);
  left: 50%;
  transform: translateX(-50%);
  width: var(--next-size);
  height: auto;
  object-fit: contain;
  animation: next-enemy-float 1.6s ease-in-out infinite; /* 上下にフワフワ動く */
}

/* ---------- 6. クリア画面 ---------- */
/* 再会シーン：画面幅いっぱい */
.clear-scene {
  width: 100%;
  max-height: 50%;
  object-fit: contain;
  border-radius: 12px;
}

/* 「○○が仲間になった！」だけ、ふだんの 16px の約1.5倍（体力の表示は 16px のまま） */
#clear-ally {
  font-size: 24px;
}

/* ---------- 7. タイトル・ゲームオーバー・エンディング画面 ---------- */
/* 画像：画面幅いっぱい、縦横比は保つ */
.scene-img {
  width: 100%;
  max-height: 45%;
  object-fit: contain;
  border-radius: 12px;
}

/* エンディングの仲間：顔アイコンを大きめに、横に並べる */
.ending-allies {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.ending-allies .ally-face {
  width: 56px;
  height: 56px;
}

/* エンディングの紙吹雪を入れる枠：画面いっぱいに広げて、クリックは下のボタンに通す */
#screen-ending {
  position: relative;
  overflow: hidden;
}

.confetti {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  pointer-events: none;
}

/* 紙吹雪1枚：色・大きさ・横位置・速さ・ずれは、HTML の style で渡す変数（--c, --s, --x, --t, --d, --sway） */
.confetti i {
  position: absolute;
  top: -20px;
  left: var(--x);
  width: var(--s);
  height: calc(var(--s) * 1.6);
  background: var(--c);
  border-radius: 2px;
  opacity: 0;
  animation: confetti-fall var(--t) linear var(--d) infinite;
  will-change: transform;
}

/* ---------- 8. アニメーション（transform だけを動かすので軽い） ---------- */
/* 紙吹雪：上から下へ、左右にゆれながら回転して落ちる */
@keyframes confetti-fall {
  0%   { transform: translate3d(0, 0, 0) rotate(0deg);                  opacity: 1; }
  100% { transform: translate3d(var(--sway), 110vh, 0) rotate(720deg); opacity: 1; }
}

/* ぶつかったとき：左右に細かく揺れて、元の位置に戻る。
   マスは描画のたびに作り直されるので、ぶつかるたびに毎回動く */
@keyframes hit-shake {
  0%   { transform: translateX(0); }
  15%  { transform: translateX(-7px) rotate(-2deg); }
  30%  { transform: translateX(7px)  rotate(2deg); }
  45%  { transform: translateX(-5px) rotate(-1deg); }
  60%  { transform: translateX(5px)  rotate(1deg); }
  80%  { transform: translateX(-2px); }
  100% { transform: translateX(0); }
}

/* 判定のときの桃太郎：中央のマスの待ちの位置から、今の位置へサッと移動する。
   横のずれ＝何マスぶんか（--lane-shift）× マス1つぶんの距離（--lane-step）。
   縦のずれ＝待ちの上端 − 判定の上端 */
@keyframes player-move-in {
  from {
    transform: translate(calc(var(--lane-shift) * var(--lane-step)), calc(var(--wait-top) - var(--judge-top)));
  }
  to {
    transform: none;
  }
}

/* 鬼：上の行の「次に来る鬼」の位置から、小さい姿で今の位置へサッとやって来る */
@keyframes enemy-move-in {
  from {
    transform: translate(calc(var(--lane-shift) * var(--lane-step)), calc(var(--from-y) - var(--enemy-y))) scale(var(--from-scale));
  }
  to {
    transform: none;
  }
}

/* 次に来る鬼：上下に6pxずつ、ゆっくり行き来する（横の中央寄せの translateX も書いておく） */
@keyframes next-enemy-float {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50%      { transform: translateX(-50%) translateY(6px); }
}

/* ---------- 9. 動きを減らす設定のときは、動かさない ---------- */
@media (prefers-reduced-motion: reduce) {
  .confetti { display: none; }
  .player-group.is-judging,
  .lane.is-hit .player-group.is-judging,
  .enemy-img { animation: none; }
  .next-enemy-img { animation: none; }
  .btn-main,
  .btn-sub { transition: none; }
  .btn-main:active,
  .btn-sub:active { transform: none; }
}
