/* R9 final candidate overrides. Canonical props stay the same DOM nodes through every transition. */

:root {
  --r9-brass: #cda35f;
  --r9-stage-black: #050303;
  --r9-safe-inline: clamp(12px, 2.2vw, 34px);
}

/* A3: one measured safe content area on the paper, with a scrollable paper fallback. */
@media (min-width: 761px) and (min-height: 561px) {
  .desktop-letter-form {
    left: 34% !important;
    top: 7.5% !important;
    width: 32% !important;
    height: 72% !important;
    overflow: hidden;
    padding: 0 !important;
    border: 1px solid rgba(103, 71, 43, .24);
    background:
      linear-gradient(90deg, rgba(119,86,51,.035) 1px, transparent 1px) 0 0 / 34px 100%,
      linear-gradient(180deg, #f5e8ca, #ead5ae) !important;
    box-shadow: 0 24px 54px rgba(0,0,0,.42), inset 0 0 42px rgba(111,77,42,.08);
  }

  .desktop-letter-form .letter-form-inner {
    display: grid !important;
    grid-template-rows: auto auto minmax(68px, 1fr) auto minmax(18px, auto) auto;
    gap: clamp(4px, .72svh, 8px) !important;
    align-content: stretch;
    padding: clamp(18px, 2.1vw, 32px) clamp(20px, 2.6vw, 38px) clamp(20px, 2.4vw, 36px) !important;
  }

  .desktop-letter-form .paper-field textarea {
    height: 100%;
    min-height: 3.7em !important;
  }

  .desktop-letter-form .letter-kicker { font-size: clamp(16px, 1.12vw, 20px) !important; }
  .desktop-letter-form .paper-field > span,
  .desktop-letter-form .visibility legend,
  .desktop-letter-form .paper-field input,
  .desktop-letter-form .paper-field textarea,
  .desktop-letter-form .visibility,
  .desktop-letter-form .seal-button { font-size: clamp(15px, 1.02vw, 18px) !important; }
  .desktop-letter-form .paper-field small,
  .desktop-letter-form .form-status { font-size: clamp(11px, .76vw, 14px) !important; }
  .desktop-letter-form .seal-button { min-height: 34px; padding: 5px 14px !important; }
}

@media (orientation: portrait) and (max-width: 900px),
       (max-height: 560px) and (max-width: 1000px) {
  .desktop-letter-form { display: none !important; }
  .mobile-letter-form {
    display: block !important;
    top: clamp(18svh, 28svh, 34svh) !important;
    padding: 0 12px max(12px, env(safe-area-inset-bottom)) !important;
    overflow: auto !important;
    overscroll-behavior: contain;
  }

  .mobile-paper {
    display: grid;
    gap: 10px;
    width: min(560px, 100%) !important;
    min-height: min-content;
    padding: clamp(22px, 5vw, 34px) clamp(18px, 5vw, 32px) 34px !important;
    border: 1px solid rgba(104, 73, 46, .18);
    background:
      linear-gradient(90deg, rgba(120,90,58,.035) 1px, transparent 1px) 0 0 / 32px 100%,
      linear-gradient(180deg, #f5e7c8, #ead5ad) !important;
  }

  .mobile-paper .letter-kicker { font-size: 20px !important; }
  .mobile-paper .paper-field > span,
  .mobile-paper .visibility legend,
  .mobile-paper .paper-field input,
  .mobile-paper .paper-field textarea,
  .mobile-paper .visibility,
  .mobile-paper .seal-button { font-size: 17px !important; }
  .mobile-paper .paper-field textarea { min-height: 108px !important; }
  .mobile-paper .visibility { gap: 8px; }
  .mobile-paper .visibility label { min-height: 32px; }
  .mobile-paper .seal-button { min-height: 46px; }
}

@media (orientation: landscape) and (max-height: 560px) and (max-width: 1000px) {
  .mobile-letter-form {
    inset: 0 0 0 45vw !important;
    padding: 8px max(10px, env(safe-area-inset-right)) 8px 8px !important;
    background: linear-gradient(90deg, transparent, rgba(10, 5, 5, .76) 8%) !important;
  }
  .mobile-paper { padding: 16px 18px 20px !important; gap: 6px; }
  .mobile-paper .letter-kicker { font-size: 17px !important; }
  .mobile-paper .paper-field > span,
  .mobile-paper .visibility legend,
  .mobile-paper .paper-field input,
  .mobile-paper .paper-field textarea,
  .mobile-paper .visibility,
  .mobile-paper .seal-button { font-size: 15px !important; }
  .mobile-paper .paper-field textarea { min-height: 58px !important; }
}

/* Shared black-clad stagehands. They never replace a board; they only accompany its own transform. */
.r9-stagehands {
  position: absolute;
  z-index: 8;
  inset: -8% -10% -12%;
  overflow: visible;
  pointer-events: none;
}

.r9-stagehand {
  position: absolute;
  bottom: 1%;
  width: clamp(24px, 4.2vw, 58px);
  height: clamp(86px, 19svh, 190px);
  border-radius: 0;
  clip-path: polygon(40% 0,60% 0,66% 10%,76% 17%,72% 48%,94% 100%,62% 100%,50% 61%,38% 100%,6% 100%,28% 48%,24% 17%,34% 10%);
  background: linear-gradient(90deg, #020202, #171311 48%, #020202);
  box-shadow: 0 14px 24px rgba(0, 0, 0, .78);
  opacity: 0;
  transform: translateY(18%) scale(.96);
  transition: transform 850ms cubic-bezier(.2,.75,.2,1), opacity 450ms ease;
}

.r9-stagehand::before { content: none; }

.r9-stagehand::after {
  content: "";
  position: absolute;
  top: 31%;
  width: 86%;
  height: 10%;
  border-radius: 999px;
  background: #090807;
  transform-origin: 10% 50%;
  transform: rotate(-24deg);
}

.r9-stagehand-left { left: 3%; }
.r9-stagehand-center { left: 47%; }
.r9-stagehand-right { right: 3%; }
.r9-stagehand-right::after { right: 0; transform-origin: 90% 50%; transform: rotate(24deg); }

#chapterMenu.is-stagehands-entering .r9-stagehand,
#chapterMenu.is-stagehands-working .r9-stagehand,
#chapterMenu.is-stagehands-returning .r9-stagehand,
#afterstoryHub.is-stagehands-entering .r9-stagehand,
#afterstoryHub.is-stagehands-working .r9-stagehand {
  opacity: .94;
  transform: translateY(0) scale(1);
}

#chapterMenu.is-stagehands-leaving .r9-stagehand,
#afterstoryHub.is-stagehands-leaving .r9-stagehand {
  opacity: 0;
  transform: translateY(8%) translateX(var(--leave-x, -4vw));
}

.r9-stagehand-right { --leave-x: 4vw; }
.r9-stagehand-center { --leave-x: 0; }

/* Main-board reveal uses the real, final clickable boards. */
#r8TransitionA {
  z-index: 90 !important;
  background:
    linear-gradient(180deg, rgba(0,0,0,.58), rgba(0,0,0,.2) 58%, rgba(0,0,0,.5)),
    url("../image/stage/HovicMap_A7_empty_stage.png") center / cover no-repeat !important;
}

#r8TransitionA .r8-transition-film { display: none !important; }
#r8TransitionA.is-active { opacity: 1 !important; visibility: visible !important; }
#r8TransitionA.is-finishing { opacity: 0 !important; transition: opacity 420ms ease !important; }

body[data-stage="CHAPTER_MENU"] #chapterMenu.is-stage-dark .chapter-sign.story-board {
  filter: brightness(.34) saturate(.62) !important;
}

/* Keep Web scenic handoffs physical and secondary to performers/lyrics. */
.main-song-web-stage .r8r4-stage-event-props,
.answer-web-stage .r8r4-stage-event-props,
.afterstory-web-stage .r8r4-stage-event-props {
  transform: scale(.84);
  transform-origin: 50% 86%;
  filter: saturate(.88) contrast(1.04) drop-shadow(0 18px 22px rgba(0,0,0,.42));
}

.r8r4-stage-prop--door {
  width: clamp(88px, 12vw, 190px) !important;
  height: clamp(170px, 38svh, 390px) !important;
  border-radius: 48% 48% 2% 2% / 18% 18% 2% 2% !important;
  border-color: rgba(193, 146, 79, .58) !important;
  background:
    linear-gradient(90deg, rgba(255,255,255,.035), transparent 20% 80%, rgba(0,0,0,.24)),
    linear-gradient(155deg, #5b2a20, #26100e 72%) !important;
  box-shadow: inset 0 0 0 7px #160907, inset 0 0 0 9px rgba(205,163,95,.46), 0 20px 34px rgba(0,0,0,.6) !important;
}

.r8r4-stage-prop--mirror {
  left: 68% !important;
  width: clamp(82px, 12vw, 178px) !important;
  height: clamp(125px, 30svh, 290px) !important;
  border-width: 3px !important;
  border-color: rgba(207, 164, 94, .7) !important;
  background: linear-gradient(145deg, rgba(236,194,132,.13), rgba(15,9,8,.48)) !important;
  box-shadow: inset 0 0 26px rgba(232,191,124,.1), 0 18px 34px rgba(0,0,0,.55) !important;
}

.r8r4-stage-prop--home-outline,
.r8r4-stage-prop--mini-proscenium {
  width: min(32vw, 420px) !important;
  height: min(42svh, 390px) !important;
  border-color: rgba(212, 169, 100, .42) !important;
  background: radial-gradient(ellipse at 50% 66%, rgba(224,167,89,.13), transparent 65%) !important;
  box-shadow: inset 0 0 36px rgba(214,162,91,.08), 0 20px 36px rgba(0,0,0,.48) !important;
}

body[data-stage="CHAPTER_MENU"] #chapterMenu.is-stagehands-returning .r9-stagehands {
  z-index: 12;
}

/* Empty stage, then the same Answer plaque rises and becomes the clickable entry. */
#trilogyAnswerTransition {
  z-index: 91 !important;
  background:
    radial-gradient(ellipse at 50% 72%, rgba(196, 125, 59, .18), transparent 31%),
    linear-gradient(180deg, rgba(0,0,0,.48), rgba(0,0,0,.18) 58%, rgba(0,0,0,.52)),
    url("../image/stage/HovicMap_A7_empty_stage.png") center / cover no-repeat !important;
}

#trilogyAnswerTransition > .r8-transition-film,
#trilogyAnswerTransition > .r8-film-grade { display: none !important; }

.r9-answer-empty-stage {
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at 50% 72%, rgba(238, 178, 95, .12), transparent 28%);
  opacity: 0;
  transition: opacity 420ms ease;
}

#trilogyAnswerTransition.is-empty-confirmed .r9-answer-empty-stage { opacity: 1; }

#trilogyAnswerTransition .answer-reveal-plaque,
#trilogyAnswerTransition.is-active .answer-reveal-plaque {
  top: 54% !important;
  opacity: 0 !important;
  transform: translate(-50%, 72svh) scale(.94) !important;
  transition: transform 1500ms cubic-bezier(.17,.79,.2,1), opacity 520ms ease !important;
}

#trilogyAnswerTransition.is-answer-ready .answer-reveal-plaque {
  opacity: 1 !important;
  transform: translate(-50%, -50%) scale(1) !important;
}

#trilogyAnswerTransition.is-entering-answer .answer-reveal-plaque {
  opacity: .78 !important;
  transform: translate(-50%, -55%) scale(.91) !important;
  filter: brightness(.72);
}

#answerStageBuildTransition {
  z-index: 101 !important;
  background:
    linear-gradient(90deg, rgba(9,3,3,.96), transparent 22% 78%, rgba(9,3,3,.96)),
    radial-gradient(ellipse at 50% 58%, rgba(221, 161, 84, .31), transparent 42%),
    url("../image/stage/舞台R6R2/第二幕.png") center / cover no-repeat !important;
}

#answerStageBuildTransition > video,
#answerStageBuildTransition > .r8-film-grade { display: none !important; }

#answerStageBuildTransition::before,
#answerStageBuildTransition::after {
  /* Scenic handoff only. Real physical build footage is still pending. */
  content: none;
}

/* R9 chase clips are the full visual body. Text remains accessible DOM. */
#answerAfterstoryTransition .r8-transition-film {
  width: 100%;
  height: 100%;
  object-fit: contain !important;
  background: #050303;
}

#answerAfterstoryTransition .r8-character-line {
  z-index: 5;
  max-width: min(76vw, 720px);
  padding: 10px 18px;
  border: 1px solid rgba(238, 200, 137, .54);
  border-radius: 4px;
  background: rgba(9, 4, 4, .82);
  font-size: clamp(22px, 3vw, 42px);
  text-shadow: 0 3px 12px #000;
}

/* Afterstory build reveals the exact same three DOM props used by the Hub. */
#afterstoryStageBuildTransition {
  z-index: 98 !important;
  background:
    linear-gradient(180deg, rgba(4,2,2,.42), rgba(4,2,2,.12) 55%, rgba(4,2,2,.5)),
    url("../image/stage/HovicMap_A7_empty_stage.png") center / cover no-repeat !important;
}
#afterstoryStageBuildTransition > video,
#afterstoryStageBuildTransition > .r8-film-grade { display: none !important; }

#afterstoryHub .r9-afterstory-stagehands {
  z-index: 25;
  inset: 20% 4% 5%;
}

#afterstoryHub.is-stage-building .afterstory-programs { z-index: 24 !important; }
#afterstoryHub.is-stage-building .afterstory-hub-heading,
#afterstoryHub.is-stage-building .afterstory-hub-navigation { visibility: hidden !important; opacity: 0 !important; }

/* Navigation gets its own band, never the player control row. */
body[data-stage="CHAPTER_MEMORY"] #trilogyNavigation,
body[data-stage="CHAPTER_PRESENT"] #trilogyNavigation,
body[data-stage="CHAPTER_MAHJONG"] #trilogyNavigation {
  top: 50%;
  bottom: auto !important;
  transform: translateY(-50%);
}

body[data-stage="ANSWER_ENCORE"] #answerNavigation {
  top: max(12px, env(safe-area-inset-top));
  bottom: auto !important;
}

body[data-stage="AFTERSTORY_TRACK"] .afterstory-track-toolbar {
  top: max(12px, env(safe-area-inset-top)) !important;
  bottom: auto !important;
}

.main-song-player,
.answer-player,
.afterstory-player { z-index: 70 !important; }

/* Finale finishes with a real curtain layer after the generated bow/confetti shot. */
.r9-finale-curtain {
  position: absolute;
  z-index: 4;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.r9-finale-curtain i {
  position: absolute;
  top: -3%;
  bottom: -3%;
  width: 51%;
  background:
    repeating-linear-gradient(90deg, rgba(255,255,255,.055) 0 3%, transparent 5% 10%, rgba(0,0,0,.18) 13% 18%),
    linear-gradient(90deg, #31070c, #8b1d2a 45%, #3b080f);
  box-shadow: inset 0 0 44px rgba(0,0,0,.6), 0 0 30px rgba(0,0,0,.8);
  transition: transform 2100ms cubic-bezier(.55,.03,.42,.99);
}
.r9-finale-curtain i:first-child { left: 0; transform: translateX(-102%); }
.r9-finale-curtain i:last-child { right: 0; transform: translateX(102%) scaleX(-1); }
.finale-performance-panel.is-curtain-closing .r9-finale-curtain i:first-child { transform: translateX(0); }
.finale-performance-panel.is-curtain-closing .r9-finale-curtain i:last-child { transform: translateX(0) scaleX(-1); }

@media (max-width: 760px) {
  body[data-stage="CHAPTER_MENU"] #chapterMenu.canonical-board-deck,
  body[data-stage="CHAPTER_MENU"] #chapterMenu.canonical-board-deck.is-active {
    width: min(96vw, 620px) !important;
    gap: clamp(5px, 1.8vw, 10px) !important;
  }

  .r9-stagehand { width: clamp(17px, 5vw, 30px); height: clamp(64px, 15svh, 118px); }

  .main-song-web-stage .r8r4-stage-event-props,
  .answer-web-stage .r8r4-stage-event-props,
  .afterstory-web-stage .r8r4-stage-event-props {
    transform: scale(.64);
    transform-origin: 50% 72%;
  }

  .r8r4-stage-prop--door { width: 21vw !important; height: 32svh !important; bottom: 24% !important; }
  .r8r4-stage-prop--mirror { left: 70% !important; width: 21vw !important; height: 27svh !important; bottom: 24% !important; }

  body[data-stage="CHAPTER_MEMORY"] #trilogyNavigation,
  body[data-stage="CHAPTER_PRESENT"] #trilogyNavigation,
  body[data-stage="CHAPTER_MAHJONG"] #trilogyNavigation {
    top: 51%;
    right: 5px;
    left: 5px;
  }

  body[data-stage="CHAPTER_MEMORY"] #trilogyNavigation button,
  body[data-stage="CHAPTER_PRESENT"] #trilogyNavigation button,
  body[data-stage="CHAPTER_MAHJONG"] #trilogyNavigation button {
    min-width: 0;
    max-width: 104px;
    min-height: 40px;
    padding: 7px 9px;
    font-size: 11px;
  }

  body[data-stage="ANSWER_ENCORE"] #answerNavigation {
    right: 8px;
    left: 8px;
  }

  #answerAfterstoryTransition .r8-character-line {
    bottom: max(22px, env(safe-area-inset-bottom));
    font-size: clamp(18px, 5.2vw, 25px);
  }
}

@media (max-width: 390px) {
  body[data-stage="CHAPTER_MENU"] #chapterMenu .story-board__subtitle { display: none !important; }
  body[data-stage="CHAPTER_MENU"] #chapterMenu .story-board__title { font-size: clamp(9px, 7.5cqw, 13px) !important; }
  .afterstory-program.afterstory-board .board-copy em { display: none !important; }
}

@media (max-width: 340px) {
  body[data-stage="CHAPTER_MENU"] #chapterMenu .story-board__eyebrow { font-size: 7px !important; }
  body[data-stage="CHAPTER_MENU"] #chapterMenu .story-board__title { font-size: 9px !important; }
  .mobile-paper { padding-inline: 14px !important; }
}

@media (prefers-reduced-motion: reduce) {
  .r9-stagehand,
  .r9-finale-curtain i { transition-duration: 1ms !important; }
}
