:root {
  --ink: #1a1410;
  --ink-soft: #4a3a30;
  --paper: #f6ebda;
  --seal: #c42b22;
  --gold: #d4b06a;
  --night: #0a1220;
  --ok: #2f6b4f;
  --danger: #9b1c1c;
  --line: rgba(40, 28, 18, 0.16);
  --glass: rgba(248, 238, 220, 0.58);
  --glass-strong: rgba(248, 238, 220, 0.78);
  --glass-dark: rgba(10, 16, 28, 0.42);
  --text-on-dark: #f7efe2;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  --shadow-soft: 0 6px 18px rgba(0, 0, 0, 0.18);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 4px;
  font-family: "Songti SC", "Noto Serif SC", "PingFang SC", "Microsoft YaHei", serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--night);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ===== Stage: full-bleed cinematic background ===== */
#app {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--night);
  isolation: isolate;
}

#app::before {
  content: "";
  position: absolute;
  inset: -4%;
  z-index: 0;
  background-color: var(--night);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  transform: scale(1.06);
  animation: bgDrift 22s ease-in-out infinite alternate;
  will-change: transform;
}

#app::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  /* light vignette only — keep scene readable */
  background:
    radial-gradient(ellipse 90% 70% at 50% 42%, transparent 35%, rgba(4, 8, 16, 0.28) 100%),
    linear-gradient(180deg, rgba(6, 10, 18, 0.12) 0%, transparent 28%, transparent 58%, rgba(6, 10, 18, 0.45) 100%);
}

#app[data-bg="title"]::before {
  background-image: url("../art/runtime/bg-title-taisho-night.webp");
}
#app[data-bg="roster"]::before {
  background-image: url("../art/runtime/bg-roster-desk.webp");
}
#app[data-bg="day"]::before {
  background-image: url("../art/runtime/bg-butterfly-mansion-day.webp");
}
#app[data-bg="train"]::before {
  background-image: url("../art/runtime/bg-mugen-train-night.webp");
}

/* day scenes: even lighter top wash so mansion shows */
#app[data-bg="day"]::after {
  background:
    radial-gradient(ellipse 95% 75% at 50% 40%, transparent 40%, rgba(20, 28, 36, 0.18) 100%),
    linear-gradient(180deg, rgba(255, 250, 240, 0.08) 0%, transparent 30%, transparent 55%, rgba(10, 16, 24, 0.38) 100%);
}
#app[data-bg="roster"]::after {
  background:
    radial-gradient(ellipse 90% 70% at 50% 45%, transparent 30%, rgba(10, 8, 6, 0.35) 100%),
    linear-gradient(180deg, rgba(20, 12, 8, 0.2) 0%, transparent 40%, rgba(10, 8, 6, 0.4) 100%);
}

@keyframes bgDrift {
  from { transform: scale(1.05) translate3d(0, 0, 0); }
  to { transform: scale(1.1) translate3d(-1.2%, -0.8%, 0); }
}

.screen {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  padding: calc(12px + var(--safe-top)) 14px calc(12px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}
.screen > * { pointer-events: auto; }

/* glass paper surfaces */
.paper {
  background:
    linear-gradient(165deg, rgba(255, 250, 240, 0.72), rgba(236, 220, 190, 0.55)),
    url("../art/runtime/paper-panel-texture.webp");
  background-size: auto, 240px;
  background-blend-mode: soft-light, normal;
  border: 1px solid rgba(255, 245, 230, 0.45);
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255, 255, 255, 0.35);
  color: var(--ink);
  backdrop-filter: blur(10px) saturate(1.05);
  -webkit-backdrop-filter: blur(10px) saturate(1.05);
}
.paper.solid {
  background:
    linear-gradient(180deg, rgba(255, 250, 240, 0.9), rgba(236, 220, 190, 0.86)),
    url("../art/runtime/paper-panel-texture.webp");
  background-size: auto, 240px;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}
.glass-dark {
  background: linear-gradient(180deg, rgba(14, 20, 34, 0.55), rgba(10, 14, 24, 0.48));
  border: 1px solid rgba(255, 230, 190, 0.18);
  color: var(--text-on-dark);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px) saturate(1.1);
  -webkit-backdrop-filter: blur(12px) saturate(1.1);
}

/* ===== Title: scene first, compact card bottom ===== */
.title-screen {
  justify-content: flex-end;
  align-items: center;
  padding-bottom: calc(28px + var(--safe-bottom));
}
.title-screen::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(180deg, transparent 35%, rgba(6, 10, 18, 0.55) 100%);
}
.title-card {
  position: relative;
  width: min(420px, 94%);
  padding: 22px 20px 18px;
  text-align: center;
  animation: riseIn 0.55s ease both;
}
.title-card h1 {
  margin: 0;
  font-size: clamp(34px, 9vw, 50px);
  letter-spacing: 0.2em;
  text-indent: 0.2em;
  color: var(--seal);
  text-shadow: 0 1px 0 rgba(255,255,255,0.35), 0 8px 24px rgba(120, 20, 10, 0.2);
}
.subtitle {
  margin: 8px 0 18px;
  color: var(--ink-soft);
  letter-spacing: 0.1em;
  font-size: 14px;
}
.eyebrow {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.22em;
  color: var(--ink-soft);
}
.hint, .muted { color: var(--ink-soft); font-size: 12px; line-height: 1.5; }
.hint { margin-top: 12px; opacity: 0.9; }

@keyframes riseIn {
  from { opacity: 0; transform: translateY(18px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Create: right dock, left scene open ===== */
.create-screen {
  flex-direction: row;
  align-items: stretch;
  justify-content: flex-end;
  gap: 0;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
}
.create-screen .scene-spacer {
  flex: 1.15;
  min-width: 0;
  pointer-events: none;
}
.create-screen .panel {
  width: min(440px, 100%);
  max-height: 100%;
  overflow: auto;
  overscroll-behavior: contain;
  animation: slideInRight 0.4s ease both;
}
@keyframes slideInRight {
  from { opacity: 0; transform: translateX(24px); }
  to { opacity: 1; transform: translateX(0); }
}

.panel {
  padding: 14px 16px;
  width: min(720px, 100%);
  max-height: 100%;
  overflow: auto;
  overscroll-behavior: contain;
}
.panel.wide { width: min(760px, 100%); }
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}
.panel h2, .panel h3, .modal h3 {
  margin: 0;
  letter-spacing: 0.08em;
}

/* ===== Hub: open center, floating chrome ===== */
.hub-screen {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
  padding: calc(10px + var(--safe-top)) 12px calc(10px + var(--safe-bottom));
}
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  width: min(960px, 100%);
  margin: 0 auto;
  flex: 0 0 auto;
}
.name-line { font-size: 16px; }
.meta { font-size: 12px; color: var(--ink-soft); margin-top: 2px; }
.top-actions { display: flex; gap: 6px; }

.hub-stage {
  position: relative;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: stretch;
  gap: 10px;
  /* this middle band is intentionally empty so the mansion fills the view */
}

.hub-float-stats {
  align-self: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  max-width: min(720px, 100%);
  padding: 8px 12px;
  margin-top: auto;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid rgba(255, 245, 230, 0.35);
  background: rgba(12, 18, 30, 0.38);
  color: var(--text-on-dark);
  font-size: 12px;
  letter-spacing: 0.04em;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: var(--shadow-soft);
}
.chip strong { color: #ffe7b8; font-weight: 700; }
.chip.fate strong { color: #ffb4a8; }

.hub-bottom {
  display: grid;
  grid-template-columns: 1.25fr 0.85fr;
  gap: 10px;
  width: min(960px, 100%);
  margin: 0 auto;
  max-height: min(42vh, 340px);
}
.hub-bottom .panel {
  width: auto;
  max-height: 100%;
  height: 100%;
  padding: 12px;
}
.log-panel { display: flex; flex-direction: column; min-height: 0; }
.log {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  font-size: 12.5px;
  line-height: 1.55;
  padding-right: 4px;
}
.log p { margin: 0 0 8px; }

.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin: 8px 0 10px;
}
.btn-col, .btn-row { display: flex; gap: 8px; }
.btn-col { flex-direction: column; }
.btn-row { flex-wrap: wrap; margin-top: 12px; }
.btn-col .btn, .btn.block { width: 100%; }

.btn {
  appearance: none;
  border: 1.5px solid rgba(50, 30, 18, 0.4);
  background: linear-gradient(180deg, rgba(255, 248, 235, 0.95), rgba(237, 215, 176, 0.9));
  color: var(--ink);
  border-radius: var(--radius);
  min-height: 44px;
  padding: 10px 14px;
  font: inherit;
  font-weight: 700;
  letter-spacing: 0.06em;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
  box-shadow: 0 2px 0 rgba(80, 50, 20, 0.12);
}
.btn:hover { filter: brightness(1.03); }
.btn:active { transform: translateY(1px); }
.btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
  transform: none;
  filter: none;
}
.btn.primary {
  background: linear-gradient(180deg, #d44538, #9a241c);
  border-color: #5c100c;
  color: #fff8f0;
}
.btn.accent {
  background: linear-gradient(180deg, #355f9c, #1a3a68);
  border-color: #0d213f;
  color: #f3f7ff;
}
.btn.ghost, .btn.small {
  min-height: 36px;
  padding: 6px 10px;
  background: rgba(255, 248, 235, 0.55);
}
.btn.skill {
  text-align: left;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}
.btn.skill small {
  font-weight: 500;
  opacity: 0.75;
  letter-spacing: 0;
}
.btn.choice { text-align: left; }

.breath-box, .fate-box {
  border-top: 1px dashed var(--line);
  padding-top: 8px;
  margin-top: 4px;
  font-size: 13px;
}
.fate-box { color: var(--seal); font-weight: 700; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; margin-bottom: 10px; }
.field input, .modal input, #nameInput, #breathName {
  width: 100%;
  min-height: 44px;
  border: 1px solid rgba(50,30,18,0.3);
  background: rgba(255,252,245,0.82);
  padding: 10px 12px;
  font: inherit;
  color: var(--ink);
  border-radius: var(--radius);
}
.points { margin: 4px 0 10px; font-size: 13px; }
.stats-grid { display: grid; gap: 6px; margin-bottom: 12px; }
.stat-row, .stat-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper button {
  width: 36px;
  height: 36px;
  border: 1px solid rgba(50,30,18,0.3);
  background: rgba(255, 248, 235, 0.9);
  font-size: 18px;
  cursor: pointer;
  border-radius: var(--radius);
}
.stepper button:disabled { opacity: 0.35; }

.choice-grid { display: grid; gap: 8px; margin: 8px 0 12px; }
.choice-card {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px;
  border: 1px solid var(--line);
  background: rgba(255,252,245,0.55);
  cursor: pointer;
  border-radius: var(--radius);
}
.choice-card.on {
  border-color: var(--seal);
  box-shadow: inset 0 0 0 1px rgba(180, 35, 24, 0.22);
  background: rgba(255, 245, 235, 0.75);
}
.choice-card input { margin-top: 4px; }
.choice-card strong { display: block; }
.choice-card span { display: block; font-size: 12px; color: var(--ink-soft); margin-top: 2px; }

/* ===== Battle / Train / Ending: bottom sheet, open stage ===== */
.battle-screen,
.train-screen,
.ending-screen {
  justify-content: flex-end;
  align-items: center;
  padding-top: calc(12px + var(--safe-top));
}
.battle-screen::before,
.train-screen::before,
.ending-screen::before {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 55%;
  pointer-events: none;
  background: linear-gradient(180deg, transparent, rgba(6, 10, 18, 0.55));
}

.sheet {
  position: relative;
  width: min(820px, 100%);
  max-height: min(58vh, 520px);
  overflow: auto;
  overscroll-behavior: contain;
  padding: 14px 16px 16px;
  animation: riseIn 0.35s ease both;
}
.sheet.compact { max-height: min(48vh, 420px); }

.battle-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
}
.fighter {
  padding: 8px 10px;
  background: rgba(255,252,245,0.45);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.fighter.enemy .fname { color: var(--danger); }
.fname { font-weight: 700; margin-bottom: 6px; }
.fmeta { font-size: 12px; color: var(--ink-soft); margin-top: 4px; }
.vs { font-weight: 900; color: var(--seal); text-shadow: 0 0 12px rgba(196, 43, 34, 0.35); }
.bar {
  height: 10px;
  background: rgba(0,0,0,0.12);
  border: 1px solid rgba(0,0,0,0.1);
  overflow: hidden;
  border-radius: 999px;
}
.bar i {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #2f6b4f, #64a37f);
  transition: width 0.2s ease;
}
.bar.red i { background: linear-gradient(90deg, #8e1f18, #d3553d); }
.battle-log {
  min-height: 72px;
  max-height: 110px;
  overflow: auto;
  background: rgba(255,252,245,0.4);
  border: 1px solid var(--line);
  padding: 8px 10px;
  margin-bottom: 10px;
  font-size: 13px;
  border-radius: var(--radius);
}
.battle-log p { margin: 0 0 4px; }
.skill-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.story {
  font-size: 15px;
  line-height: 1.75;
  margin: 8px 0 14px;
  text-shadow: 0 1px 0 rgba(255,255,255,0.25);
}
.risk {
  font-size: 12px;
  color: #ffd0c8;
  font-weight: 700;
  white-space: nowrap;
  padding: 4px 8px;
  background: rgba(120, 24, 18, 0.55);
  border: 1px solid rgba(255, 180, 160, 0.25);
}
.end-meta {
  display: grid;
  gap: 6px;
  padding: 10px;
  background: rgba(255,252,245,0.45);
  border: 1px solid var(--line);
  margin-bottom: 10px;
  font-size: 13px;
  border-radius: var(--radius);
}
.costs ul { margin: 6px 0 0; padding-left: 1.1em; }
.costs li { margin-bottom: 4px; }

/* train top caption floating over scene */
.train-caption {
  position: absolute;
  top: calc(14px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  width: min(680px, 92%);
  padding: 10px 14px;
  text-align: center;
  pointer-events: none;
  z-index: 2;
}
.train-caption .risk { pointer-events: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  z-index: 5;
  background: rgba(6, 10, 18, 0.42);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 12px;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
.modal {
  width: min(560px, 100%);
  max-height: min(78vh, 680px);
  overflow: auto;
  overscroll-behavior: contain;
  padding: 14px 16px 16px;
  animation: riseIn 0.28s ease both;
  margin-bottom: calc(4px + var(--safe-bottom));
}
.modal.wide { width: min(680px, 100%); }
.list { display: grid; gap: 8px; }
.list-item {
  appearance: none;
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: rgba(255,252,245,0.62);
  padding: 12px;
  font: inherit;
  cursor: pointer;
  display: grid;
  gap: 4px;
  border-radius: var(--radius);
}
.list-item strong { color: var(--ink); }
.list-item span { color: var(--ink-soft); font-size: 12px; }
.list-item:disabled { opacity: 0.45; cursor: not-allowed; }
.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}
.tab {
  border: 1px solid var(--line);
  background: rgba(255,252,245,0.55);
  min-height: 34px;
  padding: 6px 10px;
  font: inherit;
  cursor: pointer;
  border-radius: var(--radius);
}
.tab.on {
  background: var(--seal);
  border-color: #5c100c;
  color: #fff8f0;
}
.roster-body { font-size: 14px; line-height: 1.6; }
.comment { margin: 0 0 8px; padding-bottom: 8px; border-bottom: 1px dashed var(--line); }

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(18px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 8;
  background: rgba(16, 12, 10, 0.88);
  color: #f7efe2;
  padding: 10px 14px;
  border: 1px solid rgba(255, 210, 150, 0.22);
  font-size: 13px;
  max-width: min(92vw, 420px);
  text-align: center;
  pointer-events: none;
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

@media (max-width: 760px) {
  .create-screen {
    flex-direction: column;
    justify-content: flex-end;
  }
  .create-screen .scene-spacer {
    flex: 1;
    min-height: 22vh;
  }
  .create-screen .panel {
    width: 100%;
    max-height: min(68vh, 640px);
    animation: riseIn 0.35s ease both;
  }
  .hub-bottom {
    grid-template-columns: 1fr;
    max-height: min(48vh, 380px);
  }
  .hub-bottom .log-panel { max-height: 140px; }
  .btn-grid { grid-template-columns: 1fr 1fr; }
  .skill-grid { grid-template-columns: 1fr; }
  .battle-row { grid-template-columns: 1fr; }
  .vs { display: none; }
  .title-card h1 { letter-spacing: 0.14em; text-indent: 0.14em; }
  .sheet { max-height: min(62vh, 560px); }
  .hub-stage { min-height: 18vh; }
}

@media (orientation: landscape) and (max-height: 480px) {
  .title-card, .panel, .sheet { padding: 10px; }
  .btn { min-height: 40px; }
  .hub-bottom { grid-template-columns: 1.2fr 0.8fr; max-height: 46vh; }
  .create-screen .scene-spacer { flex: 0.8; }
  #app::before { animation: none; transform: scale(1.08); }
}

/* reduce motion */
@media (prefers-reduced-motion: reduce) {
  #app::before { animation: none; }
  .title-card, .sheet, .modal, .create-screen .panel { animation: none; }
}
