/* --- 口袋棋兵 版面（16:9） --- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  /* 全域文字約 2×（rem 相對根字級）；可再調整或改回 100% */
  font-size: 200%;
  overflow-x: clip;
  max-width: 100%;
}

/* ---- Theme tokens ---- */
:root {
  /* General shared colors */
  --color-white: #fff;

  /* Tooltip tokens */
  --color-tooltip-text: #f0f0f5;
  --color-tooltip-bg-mobile: rgba(20, 20, 30, 0.94);
  --color-tooltip-bg-desktop: rgba(20, 20, 30, 0.96);
  --shadow-tooltip-mobile: 0 4px 24px rgba(0, 0, 0, 0.35);
  --shadow-tooltip-desktop: 0 4px 24px rgba(0, 0, 0, 0.4);

  /* Button tokens */
  --radius-btn-md: 10px;
  --anim-btn-slow: 1s;
  --btn-border-subtle: 2px solid rgba(255, 255, 255, 0.2);
  --btn-border-primary: rgba(180, 210, 255, 0.4);
  --panel-border-soft: 2px solid rgba(255, 255, 255, 0.12);
  --rule-soft: 1px solid rgba(255, 255, 255, 0.08);
  --btn-back-border: 2px solid #bd945f;
  --btn-back-bg: linear-gradient(180deg, #a3715b 0%, #74574b 100%);
  --btn-back-stroke: #a25d3e;
  --btn-back-shadow: #5a3020;
  --btn-gold-text: #ffd277;
  --btn-gold-bg: linear-gradient(to right, #77530a, #ffd277, #77530a, #77530a, #ffd277, #77530a);
  --btn-gold-inner-bg: rgba(0, 0, 0, 0.842);
  --btn-min-w: 108px;
  --btn-pad-md: 10px 16px;
  --btn-font-sm: 0.75rem;

  /* Spacing tokens used by button/tooltip blocks */
  --space-10: 10px;
  --space-12: 12px;
  --space-14: 14px;
  --space-16: 16px;
  --heart-on: #ff84e9;
  --heart-off: #502949;
  --panel-purple: rgba(88, 48, 120, 0.92);
  --panel-blue: #2a6cb8;
  --panel-npc: #2d2d33;
  --text-outline: 0 1px 0 #000, 0 -1px 0 #000, 1px 0 0 #000, -1px 0 0 #000;
  --btn-primary-blue: linear-gradient(180deg, #5a8cff 0%, #2a58c0 100%);
  --card-blue: #4a9eff;
  --card-orange: #ff9a3c;
  --stage-band: rgba(12, 8, 28, 0.55);
  /* 主題／自訂：可在 themes/custom.css 覆寫 */
  --pp-font-sans: "Segoe UI", "Microsoft JhengHei", sans-serif;
  --pp-curtain-ms: 500;
  /* 全身圖／選單縮圖：aspect-ratio 只能用無單位數字，不可寫 198px/122px（會整行無效） */
  --pp-char-art-w-num: 325;
  --pp-char-art-h-num: 359;
  --pp-char-art-w: calc(var(--pp-char-art-w-num) * 1px);
  --pp-char-art-h: calc(var(--pp-char-art-h-num) * 1px);
  --pp-charmin-w-num: 198;
  --pp-charmin-h-num: 122;
  --pp-charmin-w: calc(var(--pp-charmin-w-num) * 1px);
  --pp-charmin-h: calc(var(--pp-charmin-h-num) * 1px);
  /* 選單格底／框（img_list_ui_bg、img_list_ui）原始尺寸；外框以此比例，charmin 在框內 contain */
  --pp-char-list-frame-w-num: 205;
  --pp-char-list-frame-h-num: 160;
  --pp-char-list-frame-w: calc(var(--pp-char-list-frame-w-num) * 1px);
  --pp-char-list-frame-h: calc(var(--pp-char-list-frame-h-num) * 1px);
  --pp-battle-ui-scale: 1;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--pp-font-sans);
  color: #fff;
}

body {
  background:
    radial-gradient(ellipse 120% 80% at 85% 12%, rgba(180, 160, 255, 0.35), transparent 50%),
    radial-gradient(ellipse at 50% 100%, rgba(100, 140, 220, 0.4), transparent 45%),
    linear-gradient(180deg, #0d0820 0%, #1a1038 40%, #2a1a50 100%);
  display: block;
  min-height: 100vh;
  padding: 0;
  overflow-x: clip;
  max-width: 100%;
}

/* 戰鬥可視區域：不再固定 16:9；寬用 100% 避免 100vw 含捲軸寬導致橫向溢出 */
.viewport-16x9 {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  height: 100vh;
  height: 100dvh;
  aspect-ratio: auto;
  max-height: none;
  overflow: hidden;
  position: relative;
  transform-origin: center center;
  box-sizing: border-box;
}

/*
 * 場景圖必須畫在「未做 transform: scale 的層」，否則與 .game-root 的縮放疊加後，
 * background-size: cover 會像在等比滿版上再被放大一段（窄視窗特別明顯）。
 * 由 JS 設定 --pp-battle-field-bg；無場景時僅見底色。
 */
#viewport.viewport-16x9 {
  background-color: #0d0820;
  overflow-x: clip;
}

#viewport.viewport-16x9::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background-image: var(--pp-battle-field-bg, none);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  pointer-events: none;
}

.viewport-16x9.screen-shake {
  animation: screen-shake-anim 1s ease-in-out;
}

@keyframes screen-shake-anim {
  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
  12% {
    transform: translate(-7px, 4px) rotate(-0.6deg);
  }
  25% {
    transform: translate(6px, -3px) rotate(0.5deg);
  }
  38% {
    transform: translate(-5px, -4px) rotate(-0.4deg);
  }
  50% {
    transform: translate(5px, 3px) rotate(0.4deg);
  }
  62% {
    transform: translate(-4px, 2px) rotate(-0.3deg);
  }
  75% {
    transform: translate(3px, -2px) rotate(0.2deg);
  }
  88% {
    transform: translate(-2px, 1px) rotate(0);
  }
}

.viewport-16x9.showy-fx {
  animation: showy-fx-anim 1.6s ease-out;
}

@keyframes showy-fx-anim {
  0%,
  100% {
    filter: brightness(1) saturate(1);
    box-shadow: inset 0 0 0 transparent;
  }
  35% {
    filter: brightness(1.18) saturate(1.15);
    box-shadow: inset 0 0 50px rgba(255, 220, 140, 0.45);
  }
  70% {
    filter: brightness(1.06) saturate(1.08);
    box-shadow: inset 0 0 24px rgba(200, 240, 255, 0.25);
  }
}

.game-root {
  width: calc(100% / var(--pp-battle-ui-scale));
  height: calc(100% / var(--pp-battle-ui-scale));
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, max-content);
  grid-template-rows: auto minmax(120px, 1fr) auto;
  grid-template-areas:
    "top top"
    "stage stage"
    "npc player";
  gap: 8px;
  padding: 8px;
  position: relative;
  z-index: 1;
  /* 場景圖改由 #viewport::before 滿版 cover，此層透明以免重疊兩次縮放 */
  background-color: transparent;
  overflow-x: hidden;
  overflow-y: auto;
  transform: scale(var(--pp-battle-ui-scale));
  transform-origin: top center;
}

.game-root::before,
.game-root::after {
  content: "";
  position: absolute;
  left: 0;
  width: 100%;
  pointer-events: none;
  z-index: 1;
}

.game-root::before {
  top: 0;
  height: 134px;
  background: linear-gradient(180deg, #2b2726 0%, #58473f 100%);
}

.game-root::after {
  bottom: 0;
  height: 218px;
  background: linear-gradient(180deg, #58473f 0%, #2b2726 100%);
}

.game-root > * {
  position: relative;
  z-index: 2;
}

.top-bar {
  grid-area: top;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  position: relative;
  z-index: 5;
}

.top-bar-right {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 10px;
  flex-shrink: 0;
}

.battle-context-panel {
  background: transparent;
  border-radius: 8px;
  padding: 8px 12px;
  max-width: min(52vw, 16rem);
  min-width: 0;
  box-shadow: none;
  text-align: right;
}

.field-label {
  font-size: 1.02rem;
  font-weight: 600;
  margin-bottom: 4px;
  color: #fff0a8;
  text-shadow: var(--text-outline);
  line-height: 1.3;
}

.field-label-bonus {
  font-size: 0.5em;
  font-weight: 600;
  color: inherit;
  text-shadow: inherit;
}

.phase-badge {
  font-size: 1.08rem;
  font-weight: 700;
  margin-bottom: 0;
  color: #ffd080;
  text-shadow: var(--text-outline);
}

.enemy-hud {
  background: url("assets/label_enemy.png") left top / 100% 100% no-repeat;
  background-color: transparent;
  border-radius: 10px;
  padding: 12px 22px 14px 26px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  width: min(573px, 100%);
  min-width: 0;
  max-width: 100%;
  min-height: 159px;
  height: auto;
  flex: 0 1 auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  overflow: visible;
}

/*
 * PC／寬視窗：敵方底板勿被 flex 擠縮；素質列 gap 勿跟 2vw 漂移、避免換行造成寬度跳動。
 * 窄屏戰鬥 RWD 仍由 @media (max-width:520px) #viewport 覆寫。
 */
@media (min-width: 521px) {
  .enemy-hud {
    flex-shrink: 0;
    box-sizing: border-box;
  }

  .enemy-hud .stat-row {
    flex-wrap: nowrap;
    gap: 30px;
    justify-content: center;
  }
}

.menu-btn {
  flex-shrink: 0;
  background: #3a3a42;
  color: #fff;
  border: none;
  padding: 8px 14px;
  font-weight: 700;
  letter-spacing: 0.06em;
  cursor: pointer;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  font-size: 0.8rem;
}

.menu-btn:hover {
  filter: brightness(1.08);
}

.stat-row {
  display: flex;
  justify-content: center;
  gap: clamp(8px, 2vw, 30px);
  flex-wrap: wrap;
}

.stat-cell {
  text-align: center;
  min-width: 2rem;
}

.stat-value {
  font-size: 1.05rem;
  font-weight: 800;
  color: #ffffff;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.75);
  line-height: 1.1;
}

/* 素質固定色（提升玩家辨識） */
.stat-value--str { color: #ff9290; }
.stat-value--int { color: #62d8ff; }
.stat-value--dex { color: #89ff9f; }
.stat-value--agi { color: #ffc2ef; }
.stat-value--vit { color: #bababa; }
.stat-value--luk { color: #ffa507; }

/* 場景加成：各素質改為「上色到白」漸層 + 白色陰影 */
.enemy-hud .stat-row .stat-value.stat-boosted,
.player-panel .stat-row .stat-value.stat-boosted {
  text-shadow: 0 0 8px #fff;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  -webkit-background-clip: text;
}

.enemy-hud .stat-row .stat-value.stat-boosted.stat-value--str,
.player-panel .stat-row .stat-value.stat-boosted.stat-value--str {
  background-image: linear-gradient(to bottom, #ff9290 0%, #ffffff 100%);
}

.enemy-hud .stat-row .stat-value.stat-boosted.stat-value--int,
.player-panel .stat-row .stat-value.stat-boosted.stat-value--int {
  background-image: linear-gradient(to bottom, #62d8ff 0%, #ffffff 100%);
}

.enemy-hud .stat-row .stat-value.stat-boosted.stat-value--dex,
.player-panel .stat-row .stat-value.stat-boosted.stat-value--dex {
  background-image: linear-gradient(to bottom, #89ff9f 0%, #ffffff 100%);
}

.enemy-hud .stat-row .stat-value.stat-boosted.stat-value--agi,
.player-panel .stat-row .stat-value.stat-boosted.stat-value--agi {
  background-image: linear-gradient(to bottom, #ffc2ef 0%, #ffffff 100%);
}

.enemy-hud .stat-row .stat-value.stat-boosted.stat-value--vit,
.player-panel .stat-row .stat-value.stat-boosted.stat-value--vit {
  background-image: linear-gradient(to bottom, #bababa 0%, #ffffff 100%);
}

.enemy-hud .stat-row .stat-value.stat-boosted.stat-value--luk,
.player-panel .stat-row .stat-value.stat-boosted.stat-value--luk {
  background-image: linear-gradient(to bottom, #ffa507 0%, #ffffff 100%);
}

.stat-label {
  font-size: 0.55rem;
  font-weight: 800;
  opacity: 0.95;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.75);
}

.hearts {
  display: flex;
  gap: 5px;
  margin-top: 6px;
  justify-content: center;
  align-items: center;
}

/* 愛心 HP 圖 100×88，特效 256×256 置中疊加；由 JS 設 z-index(2+i)，播特效時再拉高以免被鄰格蓋住 */
.heart-wrap {
  position: relative;
  width: 100px;
  height: 88px;
  flex-shrink: 0;
  overflow: visible;
}

/* 播特效時主要由 game_card.js 設 style.setProperty(z-index, 120, important)；此處作後備 */
.heart-wrap.heart-wrap--fx-playing {
  z-index: 120 !important;
}

.heart-wrap > img {
  position: relative;
  z-index: 0;
}

.heart-fx-layer {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 256px;
  height: 256px;
  margin-left: -128px;
  margin-top: -128px;
  pointer-events: none;
  z-index: 6;
}

.heart-fx-canvas {
  display: block;
  width: 256px;
  height: 256px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.enemy-hud .hearts {
  margin-top: 8px;
  margin-bottom: -54px;
  justify-content: flex-start;
  overflow: visible;
}

.heart {
  width: 18px;
  height: 17px;
  background: var(--heart-off);
  clip-path: path(
    "M11 18 C4 12 0 8 0 5 C0 2 2 0 5 0 C8 0 10 2 11 4 C12 2 14 0 17 0 C20 0 22 2 22 5 C22 8 18 12 11 18 Z"
  );
  transition: background 0.2s ease;
}

.heart.on {
  background: var(--heart-on);
}

.stage {
  grid-area: stage;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  /* 低於操作區／NPC，小畫面時角色與黑底不壓到手牌；對話氣泡改 fixed 另層置頂 */
  z-index: 1;
}

.stage-band {
  width: 100%;
  max-width: min(720px, 100%);
  height: 200px;
  min-height: 200px;
  background: rgba(0, 0, 0, 0.68);
  border-radius: 0;
  padding: 16px clamp(8px, 3vw, 24px);
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: clamp(8px, 2vw, 16px);
  position: relative;
  z-index: 1;
  overflow: visible;
  transform: translateY(30px);
  box-sizing: border-box;
}

/* 最終值：與名字同 z-index(8)，DOM 置於名字之後以便中央重疊時蓋在最上；皆高於立繪(.fighter z-index:2) */
.clash-numbers {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: min(16px, 3vw);
  pointer-events: none;
  z-index: 8;
  opacity: 1;
  transition: opacity 0.5s ease;
}

.clash-numbers.fade-out {
  opacity: 0;
  transition: opacity 0.5s ease;
}

.clash-num {
  position: relative;
  font-weight: 900;
  font-size: clamp(1.35rem, 4vw, 2.2rem);
  line-height: 1;
  text-shadow: none;
  -webkit-text-stroke: 5px #000;
  paint-order: stroke fill;
  color: #fff;
  flex: 0 1 auto;
  max-width: 42%;
}

.clash-num--player {
  text-align: right;
}

.clash-num--enemy {
  text-align: left;
}

.clash-num-line {
  display: inline-block;
  min-width: 2ch;
  font-variant-numeric: tabular-nums;
}

.fighter {
  position: relative;
  top: 90px;
  /* 低於 .clash-numbers / .fighter-label（均為 8） */
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  transition: filter 0.4s ease, opacity 0.4s ease;
  will-change: transform;
}

.defender-battle-fx {
  position: absolute;
  width: 256px;
  height: 256px;
  transform: translate(-50%, -50%) scale(1.5);
  pointer-events: none;
  z-index: 7;
}

/* 掛在 .fighter 內（見 game_card.js），與立繪同一座標系；勿再相對 #stage-band 用 px，否則遇 scale 會偏位 */
.fighter .defender-battle-fx {
  left: 50%;
  top: 58%;
}

.defender-battle-fx-canvas {
  width: 100%;
  height: 100%;
  display: none;
  image-rendering: pixelated;
}

.defender-battle-fx-text {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) scale(0.6667);
  font-size: 1rem;
  font-weight: 900;
  color: #ffbc2c;
  line-height: 1;
  white-space: nowrap;
  text-shadow:
    -1px -1px 0 #fff,
    1px -1px 0 #fff,
    -1px 1px 0 #fff,
    1px 1px 0 #fff,
    0 0 8px rgba(255, 188, 44, 0.75),
    0 0 16px rgba(193, 100, 0, 0.45);
  opacity: 1;
  transition: opacity 300ms ease;
  animation: defender-battle-fx-text-blink 500ms linear infinite;
}

.defender-battle-fx-text.defender-battle-fx-text--fade {
  opacity: 0;
}

@keyframes defender-battle-fx-text-blink {
  0%,
  100% {
    color: #ffbc2c;
  }
  50% {
    color: #c16400;
  }
}

/* 僅攻方移動：玩家在右側時向左接近敵方；敵在左側時向右接近玩家；守方不動 */
.stage-band--clash.stage-band--clash-player-atk .player-fighter {
  animation: clash-player-atk 0.4s ease-in-out;
}

.stage-band--clash.stage-band--clash-enemy-atk .enemy-fighter {
  animation: clash-enemy-atk 0.4s ease-in-out;
}

@keyframes clash-player-atk {
  0% {
    transform: translateX(0);
  }
  42% {
    transform: translateX(calc(-1 * clamp(96px, 22vw, 220px)));
  }
  52% {
    transform: translateX(calc(-1 * clamp(78px, 18vw, 185px)));
  }
  100% {
    transform: translateX(0);
  }
}

@keyframes clash-enemy-atk {
  0% {
    transform: translateX(0);
  }
  42% {
    transform: translateX(clamp(96px, 22vw, 220px));
  }
  52% {
    transform: translateX(clamp(78px, 18vw, 185px));
  }
  100% {
    transform: translateX(0);
  }
}

/* 比拚扣血成功：守方左右震兩下（總 0.2s） */
.fighter.fighter--hit-shake {
  animation: fighter-hit-shake 0.2s ease-in-out;
}

@keyframes fighter-hit-shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(calc(-1 * clamp(6px, 1.8vw, 14px)));
  }
  40% {
    transform: translateX(clamp(6px, 1.8vw, 14px));
  }
  60% {
    transform: translateX(calc(-1 * clamp(6px, 1.8vw, 14px)));
  }
  80% {
    transform: translateX(clamp(6px, 1.8vw, 14px));
  }
}

/* 撞擊瞬間畫面特效（與 se_attack / se_attack_777 對應；777 版偏金黃閃光） */
.viewport-16x9.viewport--clash-hit {
  animation: clash-screen-flash 0.38s ease-out;
}

.viewport-16x9.viewport--clash-hit-777 {
  animation: clash-screen-flash-777 0.48s ease-out;
}

@keyframes clash-screen-flash {
  0% {
    filter: brightness(1);
  }
  40% {
    filter: brightness(1.32) contrast(1.06);
    box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.35);
  }
  100% {
    filter: brightness(1);
    box-shadow: inset 0 0 0 transparent;
  }
}

@keyframes clash-screen-flash-777 {
  0% {
    filter: brightness(1) saturate(1);
  }
  38% {
    filter: brightness(1.55) saturate(1.4);
    box-shadow:
      inset 0 0 80px rgba(255, 210, 90, 0.55),
      inset 0 0 0 2px rgba(255, 230, 160, 0.5);
  }
  100% {
    filter: brightness(1) saturate(1);
    box-shadow: inset 0 0 0 transparent;
  }
}

.fighter--exit-down {
  animation: fighter-exit-down 0.65s ease-in forwards;
  pointer-events: none;
}

.fighter--exit-down .sprite,
#enemy-sprite.fighter--exit-down ~ #enemy-fighter-label,
#player-sprite.fighter--exit-down ~ #player-fighter-label {
  opacity: 0.2;
}

/* 氣泡掛在 body，用 :has 對應角色狀態（舊 .fighter … .speech-bubble 已失效） */
body:has(#enemy-sprite.fighter--exit-down) #enemy-speech-bubble,
body:has(#player-sprite.fighter--exit-down) #player-speech-bubble {
  filter: none;
  opacity: 1;
}

@keyframes fighter-exit-down {
  to {
    transform: translateY(130%);
  }
}

body:has(#enemy-sprite.fighter.dead) #enemy-speech-bubble,
body:has(#player-sprite.fighter.dead) #player-speech-bubble {
  filter: none;
  opacity: 1;
}

/* 陣亡：立繪 brightness 1→1.5（0.2s）→ brightness 0.1 且 opacity→0（0.5s），總長 0.7s */
@keyframes sprite-death-out {
  0% {
    filter: brightness(1);
    opacity: 1;
  }
  28.571% {
    filter: brightness(1.5);
    opacity: 1;
  }
  100% {
    filter: brightness(0.1);
    opacity: 0;
  }
}

.sprite.sprite--death-animate {
  animation: sprite-death-out 0.7s linear forwards;
}

.sprite.sprite--death-finished {
  opacity: 0;
  filter: brightness(0.1);
}

.fighter-label {
  position: absolute;
  z-index: 8;
  display: block;
  max-width: min(42vw, 9rem);
  font-size: 0.75rem;
  font-weight: 800;
  text-shadow: none;
  -webkit-text-stroke: 4px #000;
  paint-order: stroke fill;
  letter-spacing: 0.04em;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
  bottom: 8px;
}

/* 相對原 -50px Y：再下移 100px → translateY(50px)；敵往右、玩家往左各 20px */
.fighter-label--enemy {
  left: 22%;
  transform: translateX(calc(-50% + 20px)) translateY(50px);
}

.fighter-label--player {
  left: 78%;
  transform: translateX(calc(-50% - 20px)) translateY(50px);
}

.sprite {
  position: relative;
  z-index: 1;
  width: 72px;
  height: 72px;
  border-radius: 50%;
}

/* 全身立繪（詳情頁）：響應式區塊（路徑由 game.js 依角色 id 設定） */
.character-detail-portrait.character-detail-portrait--art {
  aspect-ratio: var(--pp-char-art-w-num) / var(--pp-char-art-h-num);
  max-height: min(72vh, 560px);
  min-height: 280px;
  width: 100%;
  background-repeat: no-repeat;
  background-position: center bottom;
  background-size: contain;
  box-sizing: border-box;
}

/* 戰鬥立繪：素材原始像素尺寸 100%（與 --pp-char-art-w/h 一致） */
.sprite.sprite--character-art {
  width: var(--pp-char-art-w);
  height: var(--pp-char-art-h);
  border-radius: 0;
  border: none;
  box-shadow: none;
  background-color: transparent;
  background-repeat: no-repeat;
  background-position: center bottom;
  background-size: contain;
  box-sizing: border-box;
  flex-shrink: 0;
}

.sprite-enemy {
  background: radial-gradient(circle at 35% 35%, #9f6, #284);
  box-shadow: inset 0 -8px 0 rgba(0, 0, 0, 0.25);
  background-image: repeating-linear-gradient(
    90deg,
    rgba(0, 0, 0, 0.12) 0 4px,
    transparent 4px 10px
  );
}

.sprite-player {
  background: linear-gradient(145deg, #f8a060 0%, #c04030 45%, #6a2020 100%);
  border: 3px solid #4a2020;
  box-shadow: inset 0 0 0 4px rgba(255, 200, 120, 0.35);
}

.sprite.sprite-player.sprite--character-art::after {
  display: none;
}

.sprite-player::after {
  content: "";
  display: block;
  width: 100%;
  height: 45%;
  margin-top: 8%;
  background: repeating-linear-gradient(
    -55deg,
    #e8a040 0 6px,
    #c87820 6px 12px
  );
  border-radius: 4px 4px 40% 40%;
}

.npc-panel {
  grid-area: npc;
  background: transparent;
  border-radius: 8px;
  padding: 0;
  display: block;
  min-height: 0;
  min-width: 0;
  box-shadow: none;
  position: absolute;
  left: 8px;
  bottom: 8px;
  width: min(670px, calc(100% - 16px));
  max-width: min(670px, calc(100% - 16px));
  overflow: visible;
  z-index: 10;
}

#npc-panel .character-select-npc {
  width: 100%;
  height: 194px;
  min-height: 194px;
  padding-top: 8px;
  padding-bottom: 8px;
}

#npc-panel .character-select-npc-face {
  bottom: 0;
}

#npc-panel .character-select-npc-text {
  margin-left: min(200px, 36vw);
  width: calc(100% - 200px);
  max-width: none;
  padding-right: 14px;
}

.npc-portrait {
  width: 64px;
  height: 64px;
  flex-shrink: 0;
  border-radius: 6px;
  background: linear-gradient(180deg, #e8f4ff 0%, #b8d8f8 100%);
  border: 2px solid #8898aa;
  position: relative;
  overflow: hidden;
}

.npc-portrait::before {
  content: "";
  position: absolute;
  inset: 12% 8%;
  background: radial-gradient(circle at 40% 45%, #fff 0 35%, #c8e0ff 36% 100%);
  border-radius: 50%;
}

.npc-portrait::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 55%;
  background: radial-gradient(ellipse 120% 100% at 50% 0%, #fff 0%, #ddeeff 70%);
  border-radius: 50% 50% 0 0;
}

.npc-name {
  font-weight: 700;
  margin-bottom: 4px;
  color: #b8c8e8;
  font-size: 0.85rem;
}

.npc-message {
  margin: 0;
  font-size: 0.78rem;
  line-height: 1.45;
  color: #e8e8ec;
  white-space: pre-line;
}

.player-panel {
  grid-area: player;
  background: #72564a;
  padding: 8px 10px 10px;
  clip-path: none;
  border-radius: 18px 0 0 0;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  min-height: 0;
  min-width: 0;
  position: relative;
  z-index: 10;
  width: min(647px, 100%);
  max-width: 100%;
  height: 341px;
  flex-shrink: 0;
  align-self: end;
  justify-self: end;
  overflow: visible;
}

.player-panel .stat-row {
  margin-bottom: 8px;
}

.player-panel .hearts {
  margin-top: -54px;
  margin-bottom: 8px;
  justify-content: flex-end;
  overflow: visible;
}

.cards-deck {
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 10px;
  flex-wrap: nowrap;
}

/* hidden 須壓過上列 display:flex，否則 JS 設 hidden 仍會顯示手牌（與 .overlay-screen[hidden] 同理） */
.cards-deck[hidden],
.cards-deck.cards-deck--inactive {
  display: none !important;
}

.value-cards {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.special-column {
  flex-shrink: 0;
  width: 143px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
}

.special-hand {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  width: 100%;
}

.card-special-mini {
  width: 143px;
  height: 200px;
  max-width: 143px;
  min-height: 200px;
  padding: 0;
  border-width: 0;
  font-size: 0.65rem;
  touch-action: manipulation;
}

.card-special-mini .card-sub-stat {
  display: block;
  margin-top: 2px;
  font-size: 0.52rem;
  font-weight: 800;
  color: #ffe066;
  line-height: 1.2;
  text-shadow: var(--text-outline);
}

.card-special-mini .card-icon {
  width: 28px;
  height: 28px;
  font-size: 1.1rem;
}

.card-special-mini .card-tag {
  font-size: 0.58rem;
}

.card-special-mini.card-rare {
  border-color: #e8b830;
  box-shadow: 0 0 10px rgba(255, 200, 80, 0.45);
  background: linear-gradient(180deg, rgba(255, 220, 100, 0.15) 0%, rgba(40, 30, 10, 0.45) 100%);
}

.special-placeholder {
  width: 100%;
  padding: 4px 2px;
  font-size: 0.58rem;
  line-height: 1.35;
  text-align: center;
  color: rgba(255, 255, 255, 0.65);
}

.special-placeholder .sub {
  display: block;
  opacity: 0.85;
  margin-top: 2px;
}

.card-special.card-heal {
  border-color: var(--card-orange);
  width: 100%;
  min-height: 88px;
}

.special-slot {
  width: 100%;
  min-height: 100px;
  border-radius: 8px;
  border: 2px dashed rgba(255, 255, 255, 0.35);
  background: rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px;
  font-size: 0.65rem;
  line-height: 1.35;
  color: rgba(255, 255, 255, 0.75);
  text-align: center;
}

.card {
  width: 143px;
  height: 200px;
  min-height: 200px;
  border-radius: 8px;
  border: 0;
  background-color: transparent;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-end;
  padding: 0;
  cursor: pointer;
  transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
  user-select: none;
  touch-action: manipulation;
  flex-shrink: 0;
}

.card:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.4);
}

.card.selected {
  transform: translateY(-10px);
  box-shadow: 0 0 0 3px rgba(255, 224, 102, 0.55) inset, 0 12px 24px rgba(0, 0, 0, 0.35);
}

.card-label {
  margin: 0 auto 8px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.64rem;
  font-weight: 800;
  line-height: 1.2;
  color: #ffffff;
  background: rgba(0, 0, 0, 0.62);
  text-shadow: var(--text-outline);
  letter-spacing: 0.05em;
}

.card-note {
  align-self: center;
  margin-bottom: 18px;
  padding: 0;
  border-radius: 0;
  font-size: 0.72rem;
  font-weight: 700;
  color: #ffffff;
  background: transparent;
  text-shadow: none;
  display: inline-block;
  transform-origin: center bottom;
  transform: translateY(-15px) scale(1.5);
}

.card-icon {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  font-size: 1.35rem;
  line-height: 1;
}

.card-tag {
  font-size: 0.68rem;
  font-weight: 800;
  text-shadow: var(--text-outline);
}

.card-sub {
  font-size: 0.55rem;
  opacity: 0.9;
  text-shadow: var(--text-outline);
}

.card-heal.selected {
  border-color: var(--card-orange);
}

.game-root > .combat-banner {
  z-index: 120;
}

.combat-banner {
  position: absolute;
  left: 50%;
  top: 38%;
  transform: translate(-50%, -50%);
  isolation: isolate;
  background: rgba(0, 0, 0, 0.82);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 700;
  text-align: center;
  z-index: 100;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  max-width: min(92%, 420px);
  pointer-events: none;
  white-space: pre-line;
}

.combat-banner.combat-banner--round-flash {
  left: 0;
  right: 0;
  top: 38%;
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 18px 16px;
  box-sizing: border-box;
  transform: translateY(-50%);
  border-radius: 0;
  font-size: clamp(1.35rem, 3.6vw, 2.05rem);
  letter-spacing: 0.08em;
  text-transform: none;
  text-align: center;
  color: #ffff66;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0) 0%,
    rgba(0, 0, 0, 1) 50%,
    rgba(0, 0, 0, 0) 100%
  );
  box-shadow: none;
  animation: combat-banner-round-pop 1.35s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

@keyframes combat-banner-round-pop {
  0% {
    opacity: 0;
    transform: translateY(-50%) scale(0.98);
  }
  16% {
    opacity: 1;
    transform: translateY(-50%) scale(1);
  }
  70% {
    opacity: 1;
    transform: translateY(-50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translateY(-50%) scale(1.02);
  }
}

.combat-banner[hidden] {
  display: none;
}

/* 窄螢幕：手牌區再縮排（以寬度為準，不依賴 orientation，避免部分手機未回報 portrait 時仍用 max-content 右欄而溢出） */
@media (max-width: 520px) {
  .game-root {
    padding: 6px 0;
    gap: 6px 0;
  }

  .stage-band {
    padding: 12px 6px;
  }

  .card {
    width: min(143px, 29vw);
    min-height: unset;
    height: auto;
    aspect-ratio: 143 / 200;
    max-width: 100%;
  }

  .card-special-mini {
    width: min(143px, 88vw);
    max-width: 100%;
    min-height: unset;
    aspect-ratio: 143 / 200;
  }

  .special-column {
    width: 100%;
    max-width: none;
  }

  #npc-panel .character-select-npc-text {
    margin-left: min(120px, 32vw);
    width: calc(100% - min(120px, 34vw));
    padding-right: 8px;
  }

  .heart-wrap {
    transform: scale(0.85);
    transform-origin: center bottom;
  }

  #viewport .hearts {
    gap: 0;
  }

  .enemy-hud .hearts {
    gap: 0;
  }
}

/*
 * 戰鬥專用：窄螢幕（≤520px；含常見 414/428 與略寬手機，避免 481–520px 未套用而退回預設窄底板）
 * 勿再加 orientation: portrait：否則底層仍為「npc | player」且第二欄 max-content 會依玩家區固定寬（~647px+）整片往右超出。
 * 此處單欄堆疊、各區 100%、縮放與 50% 字級等一律以寬度為準。
 */
@media (max-width: 520px) {
  #viewport {
    --pp-battle-narrow-scale: 0.5;
    /* 列內「間距」目標 -30px：flex gap 不支援負值，改見 .heart-wrap margin-right */
    --pp-heart-gap: 0px;
    --pp-heart-tuck: 30px;
  }

  /* 視窗本身 flex；底部安全區改由 #72564a 玩家底板內距處理，整條棕色可貼齊螢幕下緣 */
  #viewport.viewport-16x9 {
    --pp-battle-ui-scale: min(1, calc(100vw / 390));
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    height: 100dvh;
    max-height: 100dvh;
    padding-bottom: 0;
    box-sizing: border-box;
    overflow-x: visible;
    overflow-y: hidden;
  }

  @supports (width: 1dvi) {
    #viewport.viewport-16x9 {
      --pp-battle-ui-scale: min(1, calc(100dvi / 390));
    }
  }

  /*
   * 單欄改為 flex：中間 .stage flex:1 吃滿剩餘高度，朵雲 + 玩家固定貼在視窗下緣（不懸在畫面中間）
   * （grid 的 1fr 在部分情況下未吃滿高度，下兩塊不會穩定靠底）
   */
  /*
   * 須限制在視窗內：若 height:auto 會隨內容無限變高，底部玩家區整段被 #viewport overflow 裁掉。
   * flex:1 + min-height:0 + overflow:hidden 讓中段 .stage 可被壓縮，手牌區始終留在可視區底部。
   */
  #viewport .game-root {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    flex: 1 1 0;
    min-height: 0;
    align-self: stretch;
    width: 100%;
    height: 100%;
    max-height: 100%;
    /* 覆寫窄螢幕 .game-root { padding: 6px 0 }，否則底部會露出場景縫隙 */
    padding: 0;
    gap: 0;
    box-sizing: border-box;
    overflow-x: visible;
    overflow-y: hidden;
    max-width: 100%;
  }

  /* 與 #viewport::before 場景分工；避免上下漸層與敵方底板／玩家棕底重疊搶戲 */
  #viewport .game-root::before,
  #viewport .game-root::after {
    display: none;
  }

  #viewport .top-bar {
    flex: 0 0 auto;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    overflow-x: visible;
  }

  /*
   * 中段戰鬥演出：可縮小佔高（flex:1 + min-height:0），勿再用 margin-top:auto 把整列推超出版心。
   */
  #viewport .stage {
    flex: 1 1 0;
    min-height: 0;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    overflow: hidden;
    padding-left: 0;
    padding-right: 0;
    margin-top: 0;
    margin-bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* 朵雲＋玩家區：auto 上緣吃滿剩餘空間，讓 #72564a 玩家底板貼齊視窗下緣 */
  #viewport .npc-panel {
    position: relative;
    left: auto;
    right: auto;
    bottom: auto;
    top: auto;
    flex: 0 0 auto;
    margin: 0;
    margin-top: auto;
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  /* 玩家控制區：高度須容納縮放後手牌（~100px）＋素質／愛心；安全區在底板內 */
  #viewport .player-panel {
    flex: 0 0 auto;
    height: auto;
    min-height: 200px;
    max-height: none;
    justify-self: stretch;
    align-self: stretch;
    max-width: 100%;
    width: 100%;
    min-width: 0;
    overflow-x: visible;
    overflow-y: visible;
    border-radius: 0;
    margin-top: 0;
    margin-bottom: 0;
    padding: calc(2px + 14px) 8px max(4px, env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
  }

  #viewport .player-panel .hearts {
    margin-top: -56px;
    margin-bottom: 0;
    justify-content: center;
    align-items: flex-end;
    width: 100%;
    max-width: 100%;
    margin-left: auto;
    margin-right: auto;
    min-height: 0;
    height: auto;
  }

  /* 玩家區：愛心格取消固定 height，避免 100×88 版面框過高（視覺仍靠 transform scale） */
  #viewport .player-panel .heart-wrap {
    height: auto;
    min-height: 0;
  }

  #viewport .hearts {
    gap: var(--pp-heart-gap, 0px);
  }

  /* 負間距：前一顆愛心向右拉出 -30px，與下一顆重疊／靠攏（gap 無法設負值） */
  #viewport .hearts .heart-wrap:not(:last-child) {
    margin-right: calc(-1 * var(--pp-heart-tuck, 30px));
  }

  /* 敵方愛心：置中、滿寬列；勿 shrink；特效層較寬時由 top-bar 可視溢出避免切邊 */
  #viewport .enemy-hud .hearts {
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    flex-shrink: 0;
    /* 與底板 min-height 搭配：愛心列與戰場重疊量略減，底板底緣較接近愛心垂直中心 */
    margin-top: calc(6px - 28px);
    margin-bottom: -76px;
    overflow-x: visible;
    overflow-y: visible;
    scrollbar-width: none;
  }

  #viewport .enemy-hud .hearts::-webkit-scrollbar {
    display: none;
  }

  #viewport .enemy-hud .heart-wrap {
    transform: scale(calc(var(--pp-battle-narrow-scale) * 0.9));
    transform-origin: center bottom;
  }

  #viewport .cards-deck {
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    max-width: 100%;
    min-width: 0;
  }

  #viewport .value-cards {
    flex-wrap: wrap;
    justify-content: center;
  }

  #viewport .special-column {
    flex-shrink: 1;
    min-width: 0;
    max-width: 100%;
  }

  /* 素質格 min-width:2rem × html 200% 在窄屏可超過視窗寬 */
  #viewport .stat-cell {
    min-width: 0;
  }

  #viewport .top-bar,
  #viewport .enemy-hud,
  #viewport .stage,
  #viewport .npc-panel,
  #viewport .player-panel {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  #viewport .enemy-hud {
    align-self: stretch;
    border-radius: 0;
    overflow-x: visible;
    overflow-y: visible;
    /* label_enemy：等比不拉伸，置頂水平置中；勿再加半透明底（愛心後方會像多一塊黑方） */
    background-color: transparent;
    background-image: url("assets/label_enemy.png");
    background-position: center top;
    background-size: 100% auto;
    background-repeat: no-repeat;
    /* 關閉共用 .enemy-hud 的 box-shadow，否則透明區塊仍會像一塊無色底板＋下方影子橫在愛心與戰場之間 */
    box-shadow: none;
    min-height: 158px;
    padding: 8px 10px 12px 12px;
    box-sizing: border-box;
  }

  /* 箭頭②：敵方素質列與愛心列之間 */
  #viewport .enemy-hud .enemy-stats.stat-row {
    margin-bottom: -6px;
  }

  #viewport .enemy-stats.stat-row,
  #viewport .player-stats.stat-row {
    max-width: 100%;
  }

  /* 回合資訊整列：滿寬 48px 方條、無圓角，漸層包住戰場文字與系統鈕（order 見下） */
  #viewport .top-bar-right {
    order: -1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: stretch;
    gap: 8px;
    width: 100%;
    min-width: 0;
    height: 48px;
    min-height: 48px;
    max-height: 48px;
    box-sizing: border-box;
    background: linear-gradient(180deg, #2b2726 0%, #58473f 100%);
    border-radius: 0;
    padding-left: max(0px, env(safe-area-inset-left, 0px));
    padding-right: max(0px, env(safe-area-inset-right, 0px));
  }

  #viewport .battle-context-panel {
    min-width: 0;
    max-width: 100%;
    padding: 2px 6px;
    text-align: left;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-self: stretch;
    box-sizing: border-box;
    background: transparent;
    border-radius: 0;
    box-shadow: none;
  }

  #viewport .field-label,
  #viewport .phase-badge {
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }

  #viewport .field-label {
    font-size: calc(1.02rem * var(--pp-battle-narrow-scale));
    margin-bottom: 0;
    line-height: 1.15;
  }

  #viewport .phase-badge {
    font-size: calc(1.08rem * var(--pp-battle-narrow-scale));
    line-height: 1.15;
  }

  #viewport .top-bar-right > :last-child {
    justify-self: end;
    display: flex;
    align-items: stretch;
    justify-content: flex-end;
  }

  /* 圓角正方形：覆寫共用 .Btn 的 min-width（108px）才不會變橫向長條 */
  #viewport #btn-menu.Btn.character-detail-confirm-btn {
    font-size: calc(1.125rem * var(--pp-battle-narrow-scale));
    min-width: 0 !important;
    width: auto;
    max-width: 100%;
    aspect-ratio: 1;
    height: 100%;
    min-height: 0;
    padding: calc(6px * var(--pp-battle-narrow-scale));
    flex: 0 0 auto;
    align-self: stretch;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    border-radius: calc(10px * var(--pp-battle-narrow-scale));
  }

  #viewport #btn-menu.Btn.character-detail-confirm-btn::before {
    border-radius: calc(7px * var(--pp-battle-narrow-scale));
  }

  /* 素質：字級不變，在敵我底板上左右置中 */
  #viewport .enemy-hud .stat-row,
  #viewport .player-panel .stat-row {
    justify-content: center;
    align-items: center;
    width: 100%;
    margin-left: auto;
    margin-right: auto;
  }

  #viewport .enemy-stats.stat-row,
  #viewport .player-stats.stat-row {
    justify-content: center;
  }

  /* 箭頭④：玩家愛心列與素質列之間 */
  #viewport .player-panel .player-stats.stat-row {
    margin-top: -6px;
    margin-bottom: 2px;
  }

  #viewport .enemy-hud .stat-value,
  #viewport .player-panel .stat-value {
    font-size: 1.05rem;
  }

  #viewport .enemy-hud .stat-label,
  #viewport .player-panel .stat-label {
    font-size: 0.55rem;
  }

  /*
   * 戰鬥帶：允許縮高度（覆寫全域 min-height:200px），必要時裁切上下以免擠掉手牌。
   * 關閉預設深色底：否則縮放後頂緣會像多一條黑漸層／黑帶橫在愛心與立繪之間。
   */
  #viewport .stage-band {
    background: transparent !important;
    box-shadow: none;
    justify-content: center;
    gap: clamp(12px, 6vw, 36px);
    padding-left: max(4px, env(safe-area-inset-left, 0px));
    padding-right: max(4px, env(safe-area-inset-right, 0px));
    padding-top: 6px;
    padding-bottom: 6px;
    transform: scale(var(--pp-battle-narrow-scale, 0.5));
    transform-origin: top center;
    margin-top: calc(8px * var(--pp-battle-narrow-scale, 0.5));
    margin-bottom: calc(-200px * (1 - var(--pp-battle-narrow-scale, 0.5)));
    box-sizing: border-box;
    flex-shrink: 1;
    min-height: 0;
    height: auto;
    max-height: min(128px, 30dvh);
    width: calc(100vw / var(--pp-battle-narrow-scale, 0.5));
    min-width: calc(100vw / var(--pp-battle-narrow-scale, 0.5));
    max-width: none;
    margin-left: auto;
    margin-right: auto;
    border-radius: 0;
  }

  #viewport .enemy-fighter,
  #viewport .player-fighter {
    flex: 1 1 0;
    min-width: 0;
    max-width: 50%;
    align-items: center;
    justify-content: flex-end;
  }

  #viewport .fighter-label--enemy {
    left: 20%;
  }

  #viewport .fighter-label--player {
    left: 80%;
  }

  /* 最終值（中央比數）：手機窄屏黑框線加粗一倍（全站 .clash-num 為 5px） */
  #viewport .clash-num {
    -webkit-text-stroke: 10px #000;
  }

  /* 愛心與疊加特效（窄屏基準倍率）；敵方另見 .enemy-hud .heart-wrap；玩家格高度見上列 .player-panel .heart-wrap */
  #viewport .heart-wrap {
    transform: scale(var(--pp-battle-narrow-scale));
    transform-origin: center bottom;
  }

  /* 朵雲：臉圖 absolute bottom，容器過矮 + overflow:hidden 會切掉頭頂光環 */
  #viewport #npc-panel .character-select-npc {
    --pp-npc-face-scale: var(--pp-battle-narrow-scale);
    border-radius: 0;
    min-height: 84px;
    height: auto;
    max-height: 112px;
    padding-top: 8px;
    padding-bottom: 4px;
    overflow: visible;
  }

  #viewport #npc-panel .character-select-npc-face {
    width: calc(203px * var(--pp-battle-narrow-scale));
    height: auto;
    max-width: calc(203px * var(--pp-battle-narrow-scale));
    max-height: min(84px, calc(239px * var(--pp-battle-narrow-scale)));
    object-fit: contain;
    object-position: left bottom;
  }

  #viewport #npc-panel .character-select-npc-text {
    --pp-npc-text-inset: calc((203px + 10px) * var(--pp-battle-narrow-scale));
    margin-left: var(--pp-npc-text-inset);
    width: calc(100% - var(--pp-npc-text-inset));
    padding-right: 8px;
  }

  #viewport #npc-panel .character-select-npc-name {
    font-size: calc(0.85rem * var(--pp-battle-narrow-scale));
  }

  #viewport #npc-panel .character-select-npc-tag {
    font-size: calc(0.62rem * var(--pp-battle-narrow-scale));
  }

  #viewport #npc-panel .character-select-npc-message,
  #viewport #npc-panel .npc-message {
    font-size: calc(0.78rem * var(--pp-battle-narrow-scale));
  }

  /* 橫幅、手牌與牌面文字 60%；牌尺寸 60% 以利 390px 內排四張 */
  #viewport .combat-banner {
    font-size: calc(0.9rem * var(--pp-battle-narrow-scale));
    /* 開戰／回合中央字：高於立繪與面板，並置於戰鬥區垂直中央 */
    top: 50%;
    z-index: 300;
  }

  #viewport .combat-banner.combat-banner--round-flash {
    font-size: clamp(
      calc(1.35rem * var(--pp-battle-narrow-scale)),
      calc(3.6vw * var(--pp-battle-narrow-scale)),
      calc(2.05rem * var(--pp-battle-narrow-scale))
    );
    top: 50%;
    z-index: 300;
  }

  #viewport .game-root .card {
    width: calc(143px * var(--pp-battle-narrow-scale));
    height: calc(200px * var(--pp-battle-narrow-scale));
    min-height: calc(200px * var(--pp-battle-narrow-scale));
    max-width: calc(143px * var(--pp-battle-narrow-scale));
  }

  #viewport .card-label {
    font-size: calc(0.64rem * var(--pp-battle-narrow-scale));
    margin-bottom: calc(8px * var(--pp-battle-narrow-scale));
  }

  #viewport .card-note {
    font-size: calc(0.72rem * var(--pp-battle-narrow-scale));
    margin-bottom: calc(18px * var(--pp-battle-narrow-scale));
  }

  #viewport .card-icon {
    width: calc(36px * var(--pp-battle-narrow-scale));
    height: calc(36px * var(--pp-battle-narrow-scale));
    font-size: calc(1.35rem * var(--pp-battle-narrow-scale));
  }

  #viewport .card-tag {
    font-size: calc(0.68rem * var(--pp-battle-narrow-scale));
  }

  #viewport .card-sub {
    font-size: calc(0.55rem * var(--pp-battle-narrow-scale));
  }

  #viewport .card-special-mini {
    width: calc(143px * var(--pp-battle-narrow-scale));
    max-width: calc(143px * var(--pp-battle-narrow-scale));
    min-height: calc(200px * var(--pp-battle-narrow-scale));
    height: calc(200px * var(--pp-battle-narrow-scale));
    font-size: calc(0.65rem * var(--pp-battle-narrow-scale));
  }

  #viewport .card-special-mini .card-sub-stat {
    font-size: calc(0.52rem * var(--pp-battle-narrow-scale));
  }

  #viewport .card-special-mini .card-icon {
    width: calc(28px * var(--pp-battle-narrow-scale));
    height: calc(28px * var(--pp-battle-narrow-scale));
    font-size: calc(1.1rem * var(--pp-battle-narrow-scale));
  }

  #viewport .card-special-mini .card-tag {
    font-size: calc(0.58rem * var(--pp-battle-narrow-scale));
  }

  #viewport .special-column {
    width: calc(143px * var(--pp-battle-narrow-scale));
  }

  #viewport .special-placeholder,
  #viewport .special-slot {
    font-size: calc(0.65rem * var(--pp-battle-narrow-scale));
  }

  /*
   * 戰鬥對話氣泡（fixed，掛在 body）：直式窄屏寬 40vw，減少兩顆重疊；字級 50%
   */
  body:has(#viewport:not([hidden])) .speech-bubble {
    width: 40vw;
    min-width: 0;
    max-width: 40vw;
    box-sizing: border-box;
    font-size: calc(0.68rem * 0.5);
    line-height: 1.35;
    padding: 6px 8px;
  }

  body:has(#viewport:not([hidden])) .speech-bubble-text {
    font-size: inherit;
  }
}

/* 金色按鈕共用樣式：戰鬥「系統」與角色詳情「確定」 */
.Btn.character-detail-confirm-btn {
  min-width: var(--btn-min-w);
  padding: var(--btn-pad-md);
  border: none;
  border-radius: var(--radius-btn-md);
  appearance: none;
  background: var(--btn-gold-bg);
  background-size: 250%;
  background-position: left;
  color: var(--btn-gold-text);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition-duration: var(--anim-btn-slow);
  overflow: hidden;
  box-sizing: border-box;
  font: inherit;
  font-weight: 800;
  line-height: 1.35;
  letter-spacing: normal;
  box-shadow: none;
}

.Btn.character-detail-confirm-btn::before {
  position: absolute;
  content: attr(data-label);
  color: var(--btn-gold-text);
  display: flex;
  align-items: center;
  justify-content: center;
  inset: 2px 2px;
  border-radius: 7px;
  transition-duration: var(--anim-btn-slow);
  background-color: var(--btn-gold-inner-bg);
  background-size: 200%;
  font-weight: 800;
  font-size: inherit;
  line-height: inherit;
  box-sizing: border-box;
}

.Btn.character-detail-confirm-btn:hover {
  background-position: right;
}

.Btn.character-detail-confirm-btn:hover::before {
  background-position: right;
}

.Btn.character-detail-confirm-btn:active {
  transform: scale(0.95);
}

#btn-menu.Btn.character-detail-confirm-btn {
  font-size: 1.125rem;
}

/* --- 角色選單 / 結算覆蓋層 --- */
/* hidden 屬性須壓過 .overlay-screen 的 display:flex，否則結算層會蓋住標題 */
.overlay-screen[hidden],
.overlay-menu[hidden],
#viewport[hidden],
.screen-curtain[hidden] {
  display: none !important;
}

/* 全畫面黑幕過場（0.5s 淡入 + 0.5s 淡出，由 JS 控制 opacity） */
.screen-curtain {
  position: fixed;
  inset: 0;
  z-index: 2500;
  background: #000;
  opacity: 0;
  transition: opacity 0.5s ease;
  pointer-events: none;
}

.speech-bubble {
  position: fixed;
  left: -9999px;
  top: -9999px;
  z-index: 11000;
  width: 280px;
  min-width: 280px;
  max-width: 280px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.96);
  color: #1a1028;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1.35;
  text-align: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  border: 2px solid rgba(40, 30, 60, 0.2);
  opacity: 1;
  transition: opacity 0.5s ease;
  pointer-events: none;
}

.speech-bubble.speech-bubble--fade {
  opacity: 0;
}

.speech-bubble::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 8px solid transparent;
  border-top-color: rgba(255, 255, 255, 0.96);
}

.speech-bubble-text {
  margin: 0;
  white-space: pre-line;
}

.speech-bubble--enemy {
  border-color: rgba(180, 60, 80, 0.35);
}

.speech-bubble--player {
  border-color: rgba(60, 120, 200, 0.35);
}

.character-detail-menu-dlg {
  margin: 0 0 12px;
  font-size: 0.82rem;
  line-height: 1.45;
  color: #ffe8a8;
  font-weight: 700;
  white-space: pre-line;
}

.overlay-screen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: radial-gradient(ellipse 80% 60% at 50% 20%, rgba(80, 60, 140, 0.55), rgba(8, 4, 24, 0.94));
  overflow-y: auto;
}

/* 角色選單／詳情：滿版場景底圖 */
#character-select.overlay-screen.overlay-character-select,
#character-detail.overlay-screen.overlay-character-select {
  background-color: #0d0820;
  background-image: linear-gradient(rgba(8, 4, 24, 0.42), rgba(8, 4, 24, 0.52)), url("assets/fields/bg_character.webp");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

/* 標題畫面：滿版底圖，內容區透明 */
#title-screen.overlay-screen {
  background: #120a18 url("assets/fields/bg_title.webp") center / cover no-repeat;
}

#title-screen .title-inner {
  background: transparent;
  border: none;
  box-shadow: none;
}

/* 窄屏：標題按鈕壓在底圖／立繪視覺之上，整塊於畫面垂直置中 */
@media (max-width: 520px) {
  #title-screen.overlay-screen {
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: max(12px, env(safe-area-inset-top, 0px)) max(14px, env(safe-area-inset-right, 0px))
      max(16px, env(safe-area-inset-bottom, 0px)) max(14px, env(safe-area-inset-left, 0px));
    box-sizing: border-box;
  }

  #title-screen .title-inner {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    max-width: 100%;
    min-height: 0;
    flex: 0 1 auto;
    margin-block: auto;
    position: relative;
    z-index: 2;
  }

  #title-screen .title-logo-wrap {
    position: relative;
    z-index: 1;
  }

  #title-screen .title-actions {
    position: relative;
    z-index: 3;
  }
}

/* 製作名單：與標題畫面相同底圖，無漸層 */
#credits-screen.overlay-screen {
  background: #120a18 url("assets/fields/bg_title.webp") center / cover no-repeat;
  /* 內容較高時勿垂直置中，否則頂部會被裁切；頂部預留安全區 */
  align-items: flex-start;
  justify-content: center;
  padding-top: max(16px, env(safe-area-inset-top, 0px));
  padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
}

#credits-screen .credits-inner {
  background: transparent;
  border: none;
  box-shadow: none;
}

#credits-screen .credits-list-panel {
  margin-bottom: 14px;
}

#credits-screen .credits-list-panel .overlay-title {
  margin-bottom: 12px;
  color: #fff;
  text-align: center;
}

#credits-screen .credits-body {
  color: #fff;
  opacity: 1;
  margin-bottom: 0;
}

#credits-screen .credits-body p {
  margin: 0 0 0.85em;
}

#credits-screen .credits-body p:last-child {
  margin-bottom: 0;
}

#credits-screen .credits-footer-actions {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
}

/* 與原 settlement 返回鈕同寬（整列） */
#credits-screen .credits-footer-actions .character-select-back-btn {
  display: block;
  width: 100%;
  max-width: none;
  box-sizing: border-box;
  text-align: center;
}

@media (max-width: 640px) {
  #credits-screen .credits-inner {
    padding: 14px 14px 18px;
  }

  #credits-screen #credits-title,
  #credits-screen .credits-list-panel .overlay-title {
    font-size: 0.92rem;
  }

  #credits-screen .credits-body {
    font-size: 0.75rem;
    line-height: 1.5;
  }

  #credits-screen .credits-list-panel {
    padding: 10px 9px 9px;
  }
}

.overlay-menu {
  z-index: 1100;
  background: rgba(0, 0, 0, 0.65);
}

.menu-panel {
  background: rgba(28, 22, 48, 0.96);
  border: 2px solid rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  padding: 20px 24px;
  min-width: 220px;
  max-width: 90vw;
}

.menu-panel-title {
  margin: 0 0 14px;
  font-size: 1.1rem;
  text-align: center;
}

.menu-quit {
  margin-top: 8px;
  background: linear-gradient(180deg, #a04050 0%, #602030 100%) !important;
}

.overlay-title-screen,
.overlay-title .title-inner {
  text-align: center;
}

.title-logo-wrap {
  margin: 0 auto 28px;
  display: flex;
  justify-content: center;
  padding: 0 max(8px, env(safe-area-inset-left, 0px)) 0 max(8px, env(safe-area-inset-right, 0px));
  box-sizing: border-box;
  width: 100%;
}

.title-logo-img {
  display: block;
  width: min(100%, 640px);
  max-width: 100%;
  height: auto;
}

/* ---- Button blocks ---- */
.title-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-12);
  align-items: stretch;
  max-width: 280px;
  margin: 0 auto;
}

.title-btn,
.character-select-back-btn,
.settlement-btn {
  font: inherit;
  color: var(--color-white);
  cursor: pointer;
}

.title-btn {
  padding: 10px 12px;
  border: none;
  border-radius: 0;
  background: transparent;
  font-weight: 700;
  color: #fff;
  transition: transform 0.18s ease, text-shadow 0.18s ease, filter 0.18s ease;
  /* 白字＋深色描邊框＋投影 */
  text-shadow:
    -1.5px -1.5px 0 #31322d,
    1.5px -1.5px 0 #31322d,
    -1.5px 1.5px 0 #31322d,
    1.5px 1.5px 0 #31322d,
    -1.5px 0 0 #31322d,
    1.5px 0 0 #31322d,
    0 -1.5px 0 #31322d,
    0 1.5px 0 #31322d,
    2px 3px 8px rgba(0, 0, 0, 0.45),
    0 1px 3px rgba(0, 0, 0, 0.35);
}

.title-btn-primary {
  /* 與一般標題按鈕同視覺，僅語意區分 */
  background: transparent;
  border: none;
}

.title-btn:hover {
  filter: none;
  transform: scale(1.06);
  text-shadow:
    -1.5px -1.5px 0 #c5773d,
    1.5px -1.5px 0 #c5773d,
    -1.5px 1.5px 0 #c5773d,
    1.5px 1.5px 0 #c5773d,
    -1.5px 0 0 #c5773d,
    1.5px 0 0 #c5773d,
    0 -1.5px 0 #c5773d,
    0 1.5px 0 #c5773d,
    2px 4px 10px rgba(0, 0, 0, 0.5),
    0 2px 4px rgba(0, 0, 0, 0.4);
}

.credits-body {
  font-size: 0.88rem;
  line-height: 1.6;
}

.stat-sort-wrap {
  margin-bottom: 0;
}

.character-select-header {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px 14px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.character-select-title {
  margin: 0;
  flex: 0 0 auto;
  align-self: center;
}

.character-select-header .stat-sort-wrap {
  flex: 1 1 200px;
  min-width: 0;
  display: flex;
  justify-content: flex-end;
}

.character-select-header .stat-sort-row {
  justify-content: flex-end;
}

.character-select-header .stat-sort-btn {
  flex: 0 1 auto;
  min-width: 40px;
  padding: 6px 5px;
  font-size: 0.68rem;
}

.stat-sort-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-start;
  align-items: stretch;
  margin-bottom: 0;
}

.stat-sort-btn {
  flex: 1 1 calc(16.66% - 8px);
  min-width: 48px;
  max-width: none;
  padding: 8px 6px;
  border-radius: 8px;
  border: 2px solid rgba(140, 180, 255, 0.4);
  background: rgba(30, 40, 80, 0.65);
  color: #fff;
  font: inherit;
  font-weight: 800;
  font-size: 0.75rem;
  cursor: pointer;
}

.stat-sort-btn:hover {
  filter: brightness(1.1);
}

.stat-sort-btn.is-active {
  border-color: #ffe066;
  box-shadow: 0 0 12px rgba(255, 200, 80, 0.35);
  background: rgba(80, 60, 120, 0.85);
}

#character-select .character-select-inner {
  max-width: 1173px;
  width: min(1173px, 100%);
  margin-left: auto;
  margin-right: auto;
  box-sizing: border-box;
  background: transparent;
  border: none;
  box-shadow: none;
  padding-top: 12px;
}

/*
 * 角色選單／詳情共用字級 RWD（視窗寬；與內容區 max-width 1173px 對齊）：
 * - 約 1173px：倍率 1
 * - 390px：UI（標題、篩選、詳情按鈕等）0.5 倍、朵雲 0.7 倍
 * - 783px = 1173 − 390
 */
:is(#character-select, #character-detail) {
  --pp-char-select-ui-scale: clamp(0.5, calc(0.5 + (100vw - 390px) * 0.5 / 783px), 1);
  --pp-char-select-cloudy-scale: clamp(0.7, calc(0.7 + (100vw - 390px) * 0.3 / 783px), 1);
  /* 清單／詳情：上方面板↔朵雲（約 1173px 為 100%、390px 為 30%） */
  --pp-char-select-list-cloudy-gap-scale: clamp(0.3, calc(0.3 + (100vw - 390px) * 0.7 / 783px), 1);
}

/* 詳情頁立繪：約 1173px 原寸 → 390px 約 0.6 倍 */
#character-detail {
  --pp-detail-portrait-scale: clamp(0.6, calc(0.6 + (100vw - 390px) * 0.4 / 783px), 1);
}

/* 視窗寬小於 390px：版面以 390px 寬設計後整塊等比縮放（與 game_card.js 分頁邏輯一致） */
@media (max-width: 389px) {
  #character-select .character-select-inner,
  #character-detail .character-detail-inner {
    width: 390px;
    max-width: 390px;
    --pp-char-select-scale: calc((100vw - 32px) / 390);
    transform: scale(var(--pp-char-select-scale));
    transform-origin: top center;
  }
}

/* 標題 + 屬性篩選 + 角色格共用同一塊 #2b1b15 底（原僅在 .character-scroll-outer） */
.character-select-list-panel {
  width: 100%;
  background: #2b1b15;
  border: 2px solid #5e402b;
  border-radius: 10px;
  padding: 12px 10px 10px;
  margin-bottom: 12px;
}

#character-select .character-select-header {
  margin-bottom: 10px;
}

#character-select .character-select-title {
  padding: 0.2em 0.55em;
  color: #f6e6d8;
  text-shadow: -2px -2px 0 #a25d3e,
  2px -2px 0 #a25d3e,
  -2px 2px 0 #a25d3e,
  2px 2px 0 #a25d3e;
  border-radius: 6px;
  display: inline-block;
  margin-bottom: 0;
  position: relative;
  z-index: 2;
}

#character-select .stat-sort-btn {
  border-radius: 14px;
  border: 2px solid #bd945f;
  background: linear-gradient(180deg, #a3715b 0%, #74574b 100%);
  color: #fff;
  box-shadow: none;
}

#character-select .stat-sort-btn:hover {
  filter: brightness(1.08);
}

#character-select .stat-sort-btn.is-active {
  border-color: #ffe8a8;
  box-shadow: 0 0 12px rgba(255, 210, 140, 0.5);
  background: linear-gradient(180deg, #b08068 0%, #806050 100%);
}

#character-select .character-select-header .stat-sort-btn {
  padding: 8px 12px;
  min-width: 44px;
  font-size: 0.72rem;
}

.character-select-footer {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 12px;
  margin-top: 58px;
}

/* 選單／詳情：上方內容與朵雲區塊間距（見 --pp-char-select-list-cloudy-gap-scale） */
#character-select .character-select-list-panel,
#character-detail .character-detail-panel {
  margin-bottom: calc(12px * var(--pp-char-select-list-cloudy-gap-scale, 1));
}

#character-select .character-select-footer,
#character-detail .character-select-footer {
  margin-top: calc(78px * var(--pp-char-select-list-cloudy-gap-scale, 1));
}

/* 中等寬度（4×3 格線）：朵雲下方、操作鈕置中（與角色列表同邏輯） */
@media (min-width: 520px) and (max-width: 899px) {
  #character-select .character-select-footer,
  #character-detail .character-select-footer {
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
  }

  #character-select .character-select-footer [data-action-buttons] {
    display: flex;
    justify-content: center;
    width: 100%;
  }

  #character-select .character-select-footer .character-select-back-btn {
    margin-left: 0;
  }

  #character-detail .character-detail-footer .character-detail-footer-actions {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 420px;
    margin-left: auto;
    margin-right: auto;
  }

  #character-detail .character-detail-footer .character-select-back-btn {
    margin-left: 0;
  }
}

/* 窄版（≤519px）：返回／確定全寬在朵雲下方 */
@media (max-width: 519px) {
  #character-select .character-select-footer,
  #character-detail .character-select-footer {
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
  }

  #character-select .character-select-footer [data-action-buttons] {
    display: flex;
    width: 100%;
  }

  #character-select .character-select-footer .character-select-back-btn {
    width: 100%;
    max-width: none;
    margin-left: 0;
    align-self: stretch;
    box-sizing: border-box;
  }

  #character-detail .character-detail-footer .character-detail-footer-actions {
    width: 100%;
    max-width: none;
  }

  #character-detail .character-detail-footer .character-select-back-btn,
  #character-detail .character-detail-footer .Btn.character-detail-confirm-btn {
    width: 100%;
    max-width: none;
    margin-left: 0;
    align-self: stretch;
    box-sizing: border-box;
  }
}

.character-select-npc {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 120px;
  padding: 12px 14px 14px 0;
  padding-left: 0;
  background: rgba(20, 12, 10, 0.55);
  border-radius: 8px;
  border: 1px solid rgba(94, 64, 43, 0.65);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  overflow: visible;
}

/* 角色選單／詳情：與清單格線一致 4×3 → 75%、2×4 → 50%（勿影響戰鬥 #npc-panel） */
#character-select .character-select-npc,
#character-detail .character-select-npc {
  --pp-npc-face-scale: 1;
}

@media (min-width: 520px) and (max-width: 899px) {
  #character-select .character-select-npc,
  #character-detail .character-select-npc {
    --pp-npc-face-scale: 0.75;
  }
}

@media (max-width: 519px) {
  #character-select .character-select-npc,
  #character-detail .character-select-npc {
    --pp-npc-face-scale: 0.5;
  }
}

.character-select-npc-face {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 203px;
  height: 239px;
  max-width: min(203px, 46vw);
  max-height: min(239px, 52vh);
  object-fit: contain;
  object-position: left bottom;
  pointer-events: none;
  z-index: 1;
}

#character-select .character-select-npc-face,
#character-detail .character-select-npc-face {
  width: calc(203px * var(--pp-npc-face-scale));
  height: calc(239px * var(--pp-npc-face-scale));
  max-width: min(calc(203px * var(--pp-npc-face-scale)), 46vw);
  max-height: min(calc(239px * var(--pp-npc-face-scale)), 52vh);
}

.character-select-npc-text {
  position: relative;
  z-index: 2;
  margin-left: min(200px, 42vw);
  padding: 4px 8px 4px 4px;
  flex: 1;
  min-width: 0;
}

/* 文字區須對齊立繪實際寬（203px×縮放 + 間隙），並限寬避免長文壓到臉 */
#character-select .character-select-npc-text,
#character-detail .character-select-npc-text {
  --pp-npc-text-inset: calc((203px + 14px) * var(--pp-npc-face-scale));
  margin-left: var(--pp-npc-text-inset);
  width: calc(100% - var(--pp-npc-text-inset));
  max-width: none;
  box-sizing: border-box;
}

.character-select-npc-title {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 6px;
}

.character-select-npc-name {
  font-weight: 800;
  font-size: 0.85rem;
  color: #f6cd6c;
}

.character-select-npc-tag {
  font-size: 0.62rem;
  font-weight: 700;
  color: #ffbb63;
  letter-spacing: 0.04em;
}

.character-select-npc .character-select-npc-message,
.character-select-npc-message {
  margin: 0;
  font-size: 0.78rem;
  line-height: 1.45;
  color: #e8e8ec;
  white-space: pre-line;
}

/* 選單標題／篩選／朵雲字級（須寫在 .character-select-npc-message 通用規則之後） */
#character-select .character-select-list-panel .character-select-header .character-select-title {
  font-size: calc(1.2em * var(--pp-char-select-ui-scale, 1));
}

#character-select .character-select-list-panel .character-select-header .stat-sort-btn {
  font-size: calc(0.72rem * var(--pp-char-select-ui-scale, 1));
  min-width: calc(44px * var(--pp-char-select-ui-scale, 1));
  padding: calc(8px * var(--pp-char-select-ui-scale, 1)) calc(12px * var(--pp-char-select-ui-scale, 1));
}

#character-select .character-select-npc .character-select-npc-name,
#character-detail .character-select-npc .character-select-npc-name {
  font-size: calc(0.85rem * var(--pp-char-select-cloudy-scale, 1));
}

#character-select .character-select-npc .character-select-npc-tag,
#character-detail .character-select-npc .character-select-npc-tag {
  font-size: calc(0.62rem * var(--pp-char-select-cloudy-scale, 1));
}

#character-select .character-select-npc .character-select-npc-message.npc-message,
#character-detail .character-select-npc .character-select-npc-message.npc-message {
  font-size: calc(0.78rem * var(--pp-char-select-cloudy-scale, 1));
  line-height: 1.45;
}

.character-select-back-btn {
  flex: 0 0 auto;
  align-self: flex-end;
  margin-top: 0;
  padding: var(--btn-pad-md);
  min-width: var(--btn-min-w);
  max-width: none;
  font-size: var(--btn-font-sm);
  font-weight: 800;
  border-radius: var(--radius-btn-md);
  border: var(--btn-back-border);
  background: var(--btn-back-bg);
  -webkit-text-stroke: 0.4px var(--btn-back-stroke);
  paint-order: stroke fill;
  text-shadow:
    0 0 1px var(--btn-back-stroke),
    0 1px 0 var(--btn-back-shadow);
}

.character-select-back-btn:hover {
  filter: brightness(1.06);
}

#settlement-btn-back.character-select-back-btn {
  display: block;
  width: 90%;
  max-width: 90%;
  box-sizing: border-box;
  margin: var(--space-14) auto 0;
}

@media (max-width: 420px) {
  .character-select-footer {
    flex-wrap: wrap;
  }

  .character-select-back-btn {
    margin-left: auto;
  }
}

.character-scroll-outer {
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  margin-bottom: 0;
  border-radius: 0;
  border: none;
  background: transparent;
}

.character-strip {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 100%;
  width: 100%;
}

/* 角色清單分頁格：直式窄版 2×4 → 中等 4×3 → 寬版 5×2（與 game_card.js getCharacterListPageSize 斷點一致） */
.character-page {
  width: 100%;
  max-width: 100%;
  min-width: 100%;
  box-sizing: border-box;
  scroll-snap-align: start;
  padding: 10px;
  display: grid;
  grid-auto-flow: row;
  gap: 10px;
  justify-content: start;
  align-content: start;
  align-items: start;
  justify-items: start;
  /* 預設：≤519px，2 欄×4 列、每頁 8 格 */
  --pp-char-list-col-w: min(var(--pp-char-list-frame-w), calc((100% - 10px) / 2));
  grid-template-columns: repeat(2, var(--pp-char-list-col-w));
  grid-template-rows: repeat(4, minmax(0, auto));
}

@media (min-width: 520px) and (max-width: 899px) {
  .character-page {
    /* 4×3，每頁 12 格（不足處由 JS 隱形占位補滿） */
    --pp-char-list-col-w: min(var(--pp-char-list-frame-w), calc((100% - 30px) / 4));
    grid-template-columns: repeat(4, var(--pp-char-list-col-w));
    grid-template-rows: repeat(3, minmax(0, auto));
  }
}

@media (min-width: 900px) {
  .character-page {
    /* 5×2，每頁 10 格 */
    --pp-char-list-col-w: min(var(--pp-char-list-frame-w), calc((100% - 40px) / 5));
    grid-template-columns: repeat(5, var(--pp-char-list-col-w));
    grid-template-rows: repeat(2, minmax(0, auto));
  }
}

.character-card-mini {
  min-height: 0;
  min-width: 0;
  width: 100%;
  padding: 8px 4px 6px;
  border-radius: 0;
  border: none;
  background: transparent;
  color: #fff;
  cursor: pointer;
  font: inherit;
  text-align: center;
  display: flex;
  flex-direction: column;
  /* stretch：子圖層皆 absolute，交叉軸用 center 會讓縮圖區寬度塌成 0 */
  align-items: stretch;
  justify-content: flex-start;
}

/* hover：整格（含框內名字）一體縮放 */
.character-card-stack {
  position: relative;
  width: 100%;
  max-width: 100%;
  margin-inline: auto;
  aspect-ratio: var(--pp-char-list-frame-w-num) / var(--pp-char-list-frame-h-num);
  height: auto;
  flex-shrink: 0;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.character-card-mini:not(.character-card-mini--placeholder):hover .character-card-stack {
  transform: scale(1.03);
  filter: brightness(1.06);
}

/* 隱形占位：佔滿與真實卡片相同格位，不顯示、不可點、不參與朗讀 */
.character-card-mini--placeholder {
  visibility: hidden;
  pointer-events: none;
}

.character-card-layer {
  position: absolute;
  inset: 0;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  pointer-events: none;
}

.character-card-layer--bg {
  z-index: 0;
  /* 圖檔由 game.js resolveAssetUrl 設定，與縮圖同一基準路徑 */
}

.character-card-thumb {
  position: absolute;
  inset: 0;
  background-repeat: no-repeat;
  /* 上移 11px：勿用 transform，否則與父層 hover scale 合成後與底圖／外框縮放不一致 */
  background-position: 50% calc(50% - 11px);
  background-size: contain;
  z-index: 1;
  pointer-events: none;
}

.character-card-layer--fg {
  z-index: 2;
}

.character-card-mini .character-card-name {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 5px;
  z-index: 3;
  box-sizing: border-box;
  padding: 0 6px;
  font-size: 0.6rem;
  line-height: 1.2;
  color: #f5e6d8;
  text-shadow: var(--text-outline);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
  text-align: center;
}

/* 與角色選單 #character-select .character-select-inner 同層級：透明外框、不套用 .overlay-inner 紫底 */
#character-detail .character-detail-inner {
  max-width: 1173px;
  width: min(1173px, 100%);
  margin-left: auto;
  margin-right: auto;
  box-sizing: border-box;
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  padding: 12px 16px 20px;
}

/* 手機/小平板：內容靠上，避免裁切 */
@media (max-width: 1130px) {
  #character-select,
  #character-detail {
    align-items: flex-start;
  }
}

#character-detail .character-detail-panel {
  width: 100%;
  margin-top: 4px;
}

.character-detail-layout {
  display: grid;
  grid-template-columns: minmax(200px, 44%) 1fr;
  gap: 18px 20px;
  align-items: start;
}

/* 與角色列表窄版斷點對齊：窄螢幕改單欄 */
@media (max-width: 519px) {
  .character-detail-layout {
    grid-template-columns: 1fr;
  }
}

.character-detail-portrait {
  width: 100%;
  border-radius: 12px;
  border: 2px solid rgba(255, 255, 255, 0.15);
}

/* 立繪：約 1173px 原寸 → 390px 約 0.6 倍（倍率見 #character-detail --pp-detail-portrait-scale） */
#character-detail .character-detail-portrait {
  width: calc(100% * var(--pp-detail-portrait-scale, 1));
  max-width: 100%;
  margin-left: auto;
  margin-right: auto;
  display: block;
}

.character-detail-portrait.character-detail-portrait--art {
  background-color: rgba(0, 0, 0, 0.22);
}

/* 與立繪寬度倍率一致，避免窄螢幕仍被 min-height 撑高 */
#character-detail .character-detail-portrait.character-detail-portrait--art {
  min-height: calc(280px * var(--pp-detail-portrait-scale, 1));
  max-height: min(72vh, calc(560px * var(--pp-detail-portrait-scale, 1)));
}

.character-detail-name {
  margin: 0 0 10px;
  font-size: 1.25rem;
  color: #f6cd6c;
  font-weight: 800;
}

#character-detail .character-detail-name {
  font-size: calc(1.25rem * var(--pp-char-select-ui-scale, 1));
}

/* 與戰鬥面板相同：.stat-row + .stat-cell + .stat-value / .stat-label */
.character-detail-stats {
  display: flex;
  align-items: stretch;
  margin-bottom: 12px;
  justify-content: space-between;
  flex-wrap: nowrap;
  overflow: visible;
  gap: 12px;
  padding-bottom: 0;
}

#character-detail .character-detail-stats {
  font-size: calc(0.75rem * var(--pp-char-select-ui-scale, 1));
}

.character-detail-stats .stat-cell {
  flex: 1 1 0;
  min-width: 0;
  text-align: center;
}

.character-detail-meta {
  margin: 0 0 12px;
  font-size: 0.78rem;
}

#character-detail .character-detail-meta {
  font-size: calc(0.78rem * var(--pp-char-select-cloudy-scale, 1));
}

.character-detail-meta-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 0;
  border-bottom: var(--rule-soft);
}

.character-detail-meta-row span:first-child {
  opacity: 0.85;
}

.character-detail-meta-row span:last-child {
  font-weight: 700;
  text-align: right;
}

.character-detail-footer-actions {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-10);
  flex: 0 0 auto;
}

#character-detail .character-detail-footer .character-select-back-btn {
  margin-top: 0;
  text-align: center;
  font-size: var(--btn-font-sm);
}

/* Detail confirm：與角色列表「返回標題」同字級（--btn-font-sm） */
#character-detail .Btn.character-detail-confirm-btn {
  width: 100%;
  flex-shrink: 0;
  margin: 0;
  font-size: var(--btn-font-sm);
}

#character-detail .Btn.character-detail-confirm-btn .character-detail-confirm-btn-sizer {
  opacity: 0;
  width: 100%;
  font-size: inherit;
  font-weight: inherit;
  line-height: inherit;
  pointer-events: none;
  user-select: none;
  white-space: nowrap;
}

.overlay-inner {
  width: min(960px, 100%);
  background: rgba(20, 14, 40, 0.92);
  border: var(--panel-border-soft);
  border-radius: 14px;
  padding: 20px 22px 24px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55);
}

.overlay-title {
  position: relative;
  z-index: 1;
  margin: 0 0 8px;
  font-size: 1.45rem;
  font-weight: 800;
  text-shadow: var(--text-outline);
}

.overlay-hint {
  margin: 0 0 18px;
  font-size: 0.82rem;
  opacity: 0.88;
  line-height: 1.45;
}

.character-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px;
}

.character-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  padding: 12px;
  border-radius: 10px;
  border: 2px solid rgba(120, 160, 255, 0.35);
  background: linear-gradient(165deg, rgba(60, 80, 160, 0.4) 0%, rgba(20, 16, 48, 0.85) 100%);
  color: #fff;
  cursor: pointer;
  text-align: left;
  font: inherit;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.character-card:hover {
  transform: translateY(-3px);
  border-color: rgba(255, 220, 120, 0.55);
}

.character-card-name {
  font-weight: 800;
  font-size: 1.05rem;
  text-shadow: var(--text-outline);
}

.character-card-stats {
  margin: 0;
  font-size: 0.68rem;
  line-height: 1.35;
}

.character-card-stats div {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 4px;
  padding-top: 4px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.character-card-stats dt {
  opacity: 0.85;
  font-weight: 600;
}

.character-card-stats dd {
  margin: 0;
  font-weight: 700;
  text-align: right;
}

.character-card-portrait {
  width: var(--pp-char-list-frame-w);
  max-width: 100%;
  margin-inline: auto;
  aspect-ratio: var(--pp-char-list-frame-w-num) / var(--pp-char-list-frame-h-num);
  height: auto;
  box-sizing: border-box;
  border-radius: 10px;
  border: 2px solid rgba(255, 255, 255, 0.15);
  background-color: rgba(0, 0, 0, 0.22);
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

/* 結算層：整卡於視窗垂直＋水平置中（窄屏另有 scale，見下方 media） */
#settlement.overlay-screen {
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  box-sizing: border-box;
}

.overlay-settlement .settlement-inner {
  max-width: 780px;
  width: min(780px, 96vw);
  box-sizing: border-box;
  flex: 0 0 auto;
  align-self: center;
}

.settlement-ending {
  min-height: 120px;
}

.settlement-ending-card {
  text-align: center;
}

/* 預定素材：assets/ending/img_ending*.png，設計尺寸 540×140（窄屏整卡再見 #settlement .settlement-inner 縮放） */
.ending-art {
  width: 100%;
  max-width: 540px;
  aspect-ratio: 540 / 140;
  height: auto;
  min-height: 80px;
  margin: 0 auto 12px;
  border-radius: 12px;
  border: 2px solid rgba(255, 255, 255, 0.15);
  box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.35);
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}

.ending-art--777 {
  background: linear-gradient(145deg, #ffd000 0%, #ff6000 45%, #a02080 100%);
}

.ending-art--777::after {
  content: "777";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 2.8rem;
  font-weight: 900;
  text-shadow: 2px 2px 0 #000;
}

.ending-art--nodmg {
  background: linear-gradient(180deg, #b8f0ff 0%, #4080c0 100%);
}

.ending-art--nodmg::after {
  content: "0 HP";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 1.8rem;
  font-weight: 900;
}

.ending-art--showy {
  background: radial-gradient(circle at 30% 30%, #fff8a0 0%, #ff60c0 55%, #402060 100%);
}

.ending-art--showy::after {
  content: "✨";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 3rem;
}

.ending-art--luk {
  background: linear-gradient(90deg, #ffd860 0%, #60d080 50%, #4080ff 100%);
}

.ending-art--luk::after {
  content: "LUK";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 2.2rem;
  font-weight: 900;
}

.ending-art--lock {
  background: linear-gradient(180deg, #ff4040 0%, #400018 100%);
}

.ending-art--lock::after {
  content: "1 HP";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 1.6rem;
  font-weight: 900;
}

.ending-art--red {
  background: linear-gradient(160deg, #ff2040 0%, #400010 100%);
}

.ending-art--red::after {
  content: "♥";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 3rem;
}

.ending-art--3card {
  background: linear-gradient(145deg, #304878 0%, #102030 100%);
}

.ending-art--3card::after {
  content: "3";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 3rem;
  font-weight: 900;
}

.ending-art--record {
  background: linear-gradient(135deg, #ffe080 0%, #ff8000 40%, #c04020 100%);
}

.ending-art--record::after {
  content: "No.1";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 2rem;
  font-weight: 900;
}

.ending-art--plain {
  background: linear-gradient(180deg, #a8d0ff 0%, #6080c0 100%);
}

.ending-art--plain::after {
  content: "☀";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 3rem;
}

.settlement-ending-title {
  margin: 0 0 8px;
  font-size: 1.2rem;
  font-weight: 800;
  overflow-wrap: break-word;
  word-break: break-word;
}

.settlement-ending-line {
  margin: 0;
  font-size: 0.88rem;
  line-height: 1.55;
  opacity: 0.95;
  overflow-wrap: break-word;
  word-break: break-word;
  max-width: 100%;
}

.settlement-details {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
}

.settlement-stat-list {
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 0.82rem;
  line-height: 1.6;
}

.settlement-stat-list li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.35fr);
  gap: 12px 16px;
  align-items: start;
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.settlement-stat-list span {
  opacity: 0.85;
  overflow-wrap: break-word;
  word-break: break-word;
  min-width: 0;
}

.settlement-stat-list strong {
  text-align: right;
  font-weight: 700;
  overflow-wrap: anywhere;
  word-break: break-word;
  min-width: 0;
}

.settlement-record {
  margin-top: 14px;
  padding: 12px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.25);
}

.settlement-record-hint {
  margin: 0 0 8px;
  font-size: 0.78rem;
  line-height: 1.45;
  overflow-wrap: break-word;
  word-break: break-word;
}

.settlement-record-input {
  width: 100%;
  box-sizing: border-box;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
  margin-bottom: 8px;
  font: inherit;
}

.settlement-btn {
  display: block;
  width: 100%;
  margin-top: var(--space-10);
  padding: 10px 14px;
  border-radius: 8px;
  border: none;
  background: #3a3a52;
  font-weight: 700;
}

.settlement-btn.primary {
  background: var(--btn-primary-blue);
}

.settlement-btn:hover {
  filter: brightness(1.08);
}

/*
 * 結算 UI 窄屏 RWD（與戰鬥 #viewport 同斷點）：整卡 0.6 倍，結算圖與文字一併縮放。
 * 版面寬先放大 1/0.6 再 transform:scale(0.6)，視覺上貼齊螢幕寬且不裁切。
 */
@media (max-width: 520px) {
  #settlement.overlay-screen {
    align-items: center;
    justify-content: center;
    padding: max(12px, env(safe-area-inset-top, 0px)) max(12px, env(safe-area-inset-right, 0px))
      max(16px, env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));
  }

  #settlement .settlement-inner {
    --pp-settlement-scale: 0.6;
    flex: 0 0 auto;
    width: calc(100% / var(--pp-settlement-scale));
    max-width: calc(780px / var(--pp-settlement-scale));
    transform: scale(var(--pp-settlement-scale));
    /* 須與 overlay 的 align-items:center 搭配，否則 top origin 會讓視覺整塊偏上 */
    transform-origin: center center;
    margin-left: auto;
    margin-right: auto;
  }
}

/* ---- Tooltip blocks ---- */
/* Mobile long-press tip + desktop hover tooltip shared styles */
.mobile-tip-toast,
.card-hover-tooltip {
  position: fixed;
  max-width: min(92vw, 22rem);
  padding: 0.65rem 1rem;
  color: var(--color-tooltip-text);
  border-radius: var(--radius-btn-md);
  font-size: 0.88rem;
  line-height: 1.45;
  word-break: break-word;
  overflow-wrap: anywhere;
}

/* Mobile long-press card/special tip */
.mobile-tip-toast {
  bottom: max(var(--space-16), env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-tooltip-bg-mobile);
  z-index: 9999;
  box-shadow: var(--shadow-tooltip-mobile);
  pointer-events: none;
  text-align: center;
}

/* Desktop hover tooltip (kept inside viewport bounds by JS) */
.card-hover-tooltip {
  z-index: 10000;
  background: var(--color-tooltip-bg-desktop);
  box-shadow: var(--shadow-tooltip-desktop);
  pointer-events: auto;
  text-align: left;
}

.mobile-tip-toast[hidden],
.card-hover-tooltip[hidden] {
  display: none;
}
