:root {
  --dq-gold: #c9a227;
  --dq-gold-light: #e8d48b;
  --dq-brown: #3d2914;
  --dq-parchment: #e8dcc4;
  --dq-border: #5c4033;
  --dq-shadow: #1a1209;
  --dq-text: #2c1810;
}

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

body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: "ab-megadot9", sans-serif;
  font-weight: 400;
  font-style: normal;
  color: var(--dq-text);
  background:
    url("images/bg-maps-01.webp") center / cover no-repeat,
    linear-gradient(165deg, #d4c4a0 0%, #e8dcc4 50%, #c9b896 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 2rem 2rem max(calc(1em + 24px), env(safe-area-inset-bottom)) 2rem;
  overflow-x: hidden;
}

/* 背景：外側にマージン、黒縁は画像の表示サイズに沿う（メッセージの position 基準） */
.bg-outer {
  position: fixed;
  inset: 0;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.bg-layer {
  display: inline-block;
  max-width: 800px;
  transform: translateY(-5vh);
  border: 16px solid #0a0a0a;
  box-shadow:
    inset 0 0 0 2px rgba(0, 0, 0, 0.3),
    0 0 0 2px #0a0a0a,
    0 8px 32px rgba(0, 0, 0, 0.5);
  line-height: 0;
}

.bg-layer img {
  display: block;
  max-width: min(100%, calc(100vw - 48px));
  max-height: calc(100vh - 48px);
  width: auto;
  height: auto;
  vertical-align: bottom;
}

.position-relative {
  position: relative;
}

.frame {
  position: relative;
  z-index: 1;
  border-radius: 4px;
  border: 2px solid #0a0a0a;
  box-shadow:
    0 0 0 8px #fff,
    0 0 0 10px #0a0a0a;
  padding: 2rem 3rem;
  background: #2d5a27;
}

/* メッセージ・メニューはビュー下端に固定（bg-outer 直下で transform の影響を受けない） */
.bg-outer > .message-wrap {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  min-height: 180px;
  z-index: 10;
  display: none;
  align-items: flex-end;
  justify-content: center;
  padding: 0 24px max(24px, env(safe-area-inset-bottom)) 24px;
  box-sizing: border-box;
}

.bg-outer > .message-wrap.is-visible {
  display: flex;
}

/* メニュー frame はビュー下端に固定 */
.bg-outer > .frame {
  position: fixed;
  bottom: max(24px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: calc(
    var(--message-max-width, min(1440px, calc(100vw - 48px))) - 2em
  );
  padding-left: 24px;
  padding-right: 24px;
  box-sizing: border-box;
  z-index: 20;
}

.menu-title {
  font-family: "ab-megadot9", sans-serif;
  font-size: 1.3rem;
  color: #fff;
  text-align: center;
  margin-bottom: 1.5rem;
  letter-spacing: 0.1em;
}

/* 分岐メニュー：たたかう・じゅもん・にげる・どうぐ */
.branch-menu {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem 2rem;
  list-style: none;
}

.branch-menu li {
  position: relative;
}

.branch-menu button {
  display: block;
  width: 100%;
  padding: 0.9rem 1.25rem;
  font-family: "ab-megadot9", sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 1.3rem;
  color: #fff;
  background: #2d5a27;
  border: 3px solid #fff;
  border-radius: 4px;
  box-shadow: none;
  cursor: pointer;
  transition:
    transform 0.1s,
    opacity 0.15s;
}

.branch-menu button:hover {
  transform: translate(1px, 1px);
  opacity: 0.95;
}

.branch-menu button:active {
  transform: translate(2px, 2px);
  opacity: 0.9;
}

/* 選択結果の出力エリア */
.output-area {
  margin-top: 1.5rem;
  padding: 1rem 1.25rem;
  min-height: 3rem;
  background: rgba(61, 41, 20, 0.08);
  border: 2px solid var(--dq-border);
  border-radius: 4px;
  font-size: 1rem;
  color: var(--dq-text);
}

.output-area .label {
  font-family: "ab-megadot9", sans-serif;
  font-size: 0.6rem;
  color: var(--dq-gold);
  margin-bottom: 0.35rem;
}

.output-area .result {
  font-weight: 600;
}

/* ========== ローディングページ（戦闘開始エフェクト） ========== */
.loading-wrap {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #0a0602;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.loading-wrap.hidden {
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.6s ease;
}

/* 戦闘開始：白フラッシュ */
.battle-flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  animation: battleFlash 0.4s ease-out 0.2s forwards;
}

@keyframes battleFlash {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

/* 「まものがあらわれた！」 */
.battle-message {
  position: relative;
  z-index: 2;
  font-family: "ab-megadot9", sans-serif;
  font-size: 2rem;
  color: #fff;
  text-align: center;
  line-height: 2;
  letter-spacing: 0.15em;
  opacity: 0;
  text-shadow: 0 0 20px rgba(201, 162, 39, 0.6);
  animation: battleMessageIn 0.5s ease-out 0.7s forwards;
}

@keyframes battleMessageIn {
  0% {
    opacity: 0;
    transform: scale(0.8);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

/* 読込中エリア */
.loading-content {
  position: relative;
  z-index: 2;
  margin-top: 3rem;
  opacity: 0;
  animation: loadingFadeIn 0.5s ease-out 1.4s forwards;
}

@keyframes loadingFadeIn {
  to {
    opacity: 1;
  }
}

.loading-label {
  font-family: "ab-megadot9", sans-serif;
  font-size: 0.5rem;
  color: #8b7355;
  text-align: center;
  margin-bottom: 1rem;
  letter-spacing: 0.2em;
}

.loading-bar-wrap {
  width: 220px;
  height: 16px;
  border: 3px solid #5c4033;
  border-radius: 4px;
  background: #1a1209;
  overflow: hidden;
  box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
}

.loading-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #c9a227, #e8d48b);
  border-radius: 2px;
  animation: loadingBar 2s ease-in-out 1.6s forwards;
}

@keyframes loadingBar {
  to {
    width: 100%;
  }
}

/* ========== hassan.webp（bg-layer 内・中央にフェードイン） ========== */
.bg-layer .hassan-wrap {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.6s ease;
  pointer-events: none;
}

.bg-layer .hassan-wrap.is-visible {
  opacity: 1;
}

.bg-layer .hassan-wrap img {
  max-width: 40%;
  max-height: 40%;
  width: auto;
  height: auto;
  object-fit: contain;
  transition:
    filter 0.4s ease,
    opacity 0.4s ease;
}

/* hassan ダメージアニメーション（たたかう・じゅもん・どうぐ選択後） */
.hassan-img.hassan-img--damage {
  animation: hassanDamage 0.6s ease-out;
}

@keyframes hassanDamage {
  0% {
    filter: brightness(1);
    transform: scale(1);
  }
  15% {
    filter: brightness(2) sepia(0.3);
    transform: scale(1.02);
  }
  30% {
    filter: brightness(1.2);
    transform: translateX(-4px) scale(1);
  }
  45% {
    filter: brightness(2) sepia(0.2);
    transform: translateX(4px) scale(1);
  }
  60% {
    filter: brightness(1.1);
    transform: translateX(-2px);
  }
  75% {
    filter: brightness(1.3);
    transform: translateX(2px);
  }
  100% {
    filter: brightness(1);
    transform: translateX(0);
  }
}

/* 撃破後：「デグチをたおした」完了時は非表示 */
.hassan-img.hassan-img--defeated {
  filter: grayscale(1) brightness(0.7);
  opacity: 0;
  pointer-events: none;
}

/* ========== メッセージ画面（bg-outer 直下・position:relative の関係） ========== */
.message-wrap {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  align-items: flex-end;
  justify-content: center;
  padding: 24px 24px calc(1em + 24px) 24px;
}

.message-wrap.is-visible {
  display: flex;
}

/* 仮想画面より左右1em内側に収めるラッパー */
.message-inner {
  width: 100%;
  max-width: calc(
    var(--message-max-width, min(1440px, calc(100vw - 48px))) - 2em
  );
  margin: 0 1em;
}

/* 選択後メッセージ用ボックスは初期は非表示 */
.message-box--choice {
  display: none;
}

.message-wrap.is-choice .message-box--initial {
  display: none;
}

.message-wrap.is-choice .message-box--choice {
  display: block;
}

.message-box.message-box--choice {
  position: relative;
}

.message-box.message-box--choice .text {
  min-height: 0;
  max-height: none;
  overflow: visible;
}

/* 選択メッセージ枠で「次へ」系ボタンを右下に配置 */
.message-box--choice .message-actions.message-actions--bottom-right {
  position: absolute;
  bottom: 1rem;
  right: 1rem;
  margin: 0;
  padding: 0;
  text-align: right;
}

/* message-box は menuFrame と同じデザイン（緑背景・黒2px→白8px→黒2px） */
.message-box {
  position: relative;
  z-index: 2;
  width: 100%;
  padding: 2rem 3rem;
  box-sizing: border-box;
  overflow: hidden;
  border-radius: 4px;
  border: 2px solid #0a0a0a;
  box-shadow:
    0 0 0 8px #fff,
    0 0 0 10px #0a0a0a;
  background: #2d5a27;
}

.message-box .text {
  font-family: "ab-megadot9", sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 1.3rem;
  line-height: 1.9;
  color: #fff;
  min-height: calc(1em * 1.9 * 3);
  overflow-wrap: break-word;
  word-break: break-word;
}

.message-box .text .cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: #fff;
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: cursorBlink 0.8s step-end infinite;
}

/* 初期メッセージ：ボタンは絶対配置で枠内右下に重ねる（高さを増やさない） */
.message-box--initial {
  position: relative;
}

.message-box--initial .message-actions {
  position: absolute;
  bottom: 1rem;
  right: 1rem;
  margin: 0;
  padding: 0;
  text-align: right;
  opacity: 0;
  transform: translateY(12px);
  transition:
    opacity 0.4s ease,
    transform 0.4s ease;
}

.message-box--initial .message-actions.is-visible {
  opacity: 1;
  transform: translateY(0);
}

/* テキストがボタンに重ならないよう右側に余白 */
.message-box--initial .text {
  padding-right: 6em;
}

.message-actions {
  text-align: center;
}

/* Y/N 分岐用：はい・いいえを横並び・ボタンは他と同様の緑背景・白枠 */
.message-actions .choice-buttons {
  display: inline-flex;
  gap: 1.25rem;
  flex-wrap: wrap;
  justify-content: center;
}

.message-actions .choice-buttons .btn-continue {
  min-width: 6em;
}

.message-actions .btn-continue.is-waiting {
  visibility: hidden;
}

.message-actions .btn-continue {
  font-family: "ab-megadot9", sans-serif;
  font-weight: 400;
  padding: 0.9rem 1.25rem;
  font-size: 1.3rem;
  color: #fff;
  background: #2d5a27;
  border: 3px solid #fff;
  border-radius: 4px;
  box-shadow: none;
  cursor: pointer;
  transition:
    transform 0.1s,
    opacity 0.15s;
}

.message-actions .btn-continue:hover {
  transform: translate(1px, 1px);
  opacity: 0.95;
}

.message-actions .btn-continue:active {
  transform: translate(2px, 2px);
  opacity: 0.9;
}

.message-actions .btn-continue:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.message-actions .message-link {
  display: inline-block;
  margin-top: 0.75rem;
  font-family: "ab-megadot9", sans-serif;
  font-size: 1.3rem;
  color: #fff;
  text-decoration: underline;
}

.message-actions .message-link:hover {
  opacity: 0.9;
}

@keyframes cursorBlink {
  50% {
    opacity: 0;
  }
}

/* どうする？メニュー：メッセージ完了まで非表示 */
.frame.is-waiting {
  visibility: hidden;
  pointer-events: none;
  opacity: 0;
}

.frame.is-visible {
  visibility: visible;
  pointer-events: auto;
  opacity: 1;
  transition: opacity 0.4s ease;
}

/* ========== 画面切り替えホワイトアウト ========== */
.whiteout-overlay {
  position: fixed;
  inset: 0;
  z-index: 10001;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

.whiteout-overlay.is-active {
  opacity: 1;
  pointer-events: auto;
}

/* ========== 仲間加入後のキャラクターステータス画面 ========== */
.character-status-wrap {
  position: fixed;
  inset: 0;
  z-index: 10000;
  background: #0a0a0a;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 1.5rem;
  overflow: auto;
}

.character-status-wrap.is-visible {
  display: flex;
}

.character-status-inner {
  width: 100%;
  max-width: 900px;
  font-family: "ab-megadot9", sans-serif;
  font-size: 1.05rem;
  color: #e0e0e0;
  line-height: 1.6;
}

.character-status-box {
  border: 3px solid #b0b0b0;
  background: rgba(30, 30, 30, 0.95);
  padding: 0.75rem 1rem;
  border-radius: 2px;
}

/* 上部：左＝テキスト、右＝キャラ画像 */
.character-status-upper {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 1rem;
  margin-bottom: 1rem;
}

.character-status-upper-left {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  min-width: 0;
}

.character-status-name {
  font-size: 1.35rem;
}

.character-status-title {
  opacity: 0.9;
  font-size: 1.1rem;
}

/* 区切り線：親の padding を打ち消して横いっぱいに表示 */
.character-status-divider {
  border: none;
  border-top: 3px solid #e0e0e0;
  margin: 0.5rem -1rem;
  height: 0;
  overflow: hidden;
}

.character-status-upper-right.character-status-sprite {
  min-height: 120px;
  min-width: 120px;
  background: rgba(60, 60, 60, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.character-status-sprite img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
}

/* 下部：ステータス見出し＋2カラム＋区切り＋とくぎ */
.character-status-lower {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.character-status-stats-heading {
  font-size: 1.2rem;
  font-weight: 400;
  color: #e0e0e0;
  margin: 0 0 0.25rem 0;
}

.character-status-stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.character-status-stats-col {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.character-status-special {
  margin-top: 0.25rem;
  margin-bottom: 0.25rem;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}

.character-status-footer {
  text-align: center;
  padding-top: 1rem;
}

.character-status-close {
  padding: 0.6rem 1.5rem;
  font-size: 1rem;
}

/* SP: 基本スタイルの後に記述してメディアクエリが確実に適用されるようにする */
@media (max-width: 768px) {
  .character-status-wrap {
    align-items: flex-start;
    justify-content: flex-start;
    padding: 1rem;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .character-status-inner {
    flex: 0 0 auto;
    min-height: 0;
    font-size: 0.85rem;
  }
  .character-status-upper {
    grid-template-columns: 1fr;
    margin-bottom: 0.75rem;
  }
  .character-status-upper-right.character-status-sprite {
    min-height: 80px;
    min-width: 0;
    max-height: 25vh;
  }
  .character-status-sprite img {
    max-height: 25vh;
  }
  .character-status-stats-row {
    gap: 0.75rem;
  }
  .character-status-box {
    padding: 0.5rem 0.75rem;
  }
  .character-status-divider {
    margin-left: -0.75rem;
    margin-right: -0.75rem;
  }
  .character-status-footer {
    padding-top: 0.75rem;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .bg-outer {
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
  }
  .bg-layer {
    transform: translateY(-10vh);
    max-width: 100%;
    box-sizing: border-box;
  }
  .bg-layer img {
    max-height: min(65vh, calc(100vh - 200px));
    max-width: 100%;
  }
  .bg-layer .hassan-wrap img {
    max-width: 52%;
    max-height: 26vh;
  }
  /* SP: メッセージ・メニューは必ずビュー下端に固定（上寄せされないよう明示） */
  .bg-outer > .message-wrap {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    height: 38%;
    min-height: 140px;
    padding: 0 20px max(20px, env(safe-area-inset-bottom)) 20px;
  }
  .bg-outer > .frame {
    position: fixed;
    top: auto;
    bottom: max(20px, env(safe-area-inset-bottom));
    left: 20px;
    right: 20px;
    width: auto;
    max-width: none;
    transform: none;
    padding-left: 20px;
    padding-right: 20px;
    box-sizing: border-box;
  }
  .frame {
    width: 100%;
    max-width: 100%;
    padding: 1.5rem 1.25rem;
    box-sizing: border-box;
  }
  .message-box {
    padding: 1.5rem 1.25rem;
  }
  body {
    padding: 20px;
    overflow-x: hidden;
  }
  .message-wrap {
    padding: 0 20px max(20px, env(safe-area-inset-bottom, 0px)) 20px;
  }
  .message-inner {
    width: 100%;
    max-width: 100%;
  }
  .battle-message {
    font-size: 1.8rem;
  }
  /* SP: 初期メッセージの次へボタンは relative で見やすく・高さを最初から確保 */
  .message-box--initial .message-actions {
    position: relative;
    bottom: auto;
    right: auto;
    margin-top: 1rem;
    padding-top: 0;
    min-height: 3rem;
  }
  .message-box--initial .text {
    padding-right: 0;
  }
  /* SP: 選択メッセージの「次へ」「つぎへ」も同様に relative で見やすく・高さを確保 */
  .message-box--choice .message-actions.message-actions--bottom-right {
    position: relative;
    bottom: auto;
    right: auto;
    margin-top: 1rem;
    padding: 0;
    min-height: 3rem;
  }
  .message-box--choice .text {
    padding-right: 0;
  }
  /* SP: 文字サイズを少し落とす */
  .menu-title {
    font-size: 1.05rem;
  }
  .branch-menu button {
    font-size: 1.05rem;
  }
  .message-box .text {
    font-size: 1.05rem;
  }
  .message-actions .btn-continue,
  .message-actions .message-link {
    font-size: 1.05rem;
  }
}
