/* One viewport box governs title, setup, normal gameplay and dialogue.
   The browser's VisualViewport offsets include toolbar/keyboard movement:
   avoid combining different CSS viewport units with fixed-position coordinates. */
:root {
  --palace-view-x:0px;
  --palace-view-y:0px;
  --palace-view-w:100vw;
  --palace-view-h:100svh;
}
body.frame-opening,
body.game-shell {
  --palace-rail-art: none;
  min-height:var(--palace-view-h);
  background-color:#34463e;
  background-image:
    var(--palace-rail-art),
    repeating-linear-gradient(90deg,transparent 0 21px,rgba(219,196,148,.045) 22px 23px,transparent 24px 44px),
    linear-gradient(115deg,#283a34 0%,#52655a 48%,#273a35 100%);
  background-position:center;
  background-size:cover;
  background-repeat:no-repeat;
  overflow:hidden;
  overscroll-behavior:none;
}
/* A dialogue or tapped control must not scroll the whole document. */
html:has(> body:is(.frame-opening,.game-shell)) {
  overflow:hidden;
  overscroll-behavior:none;
}
/* Same 16:9 fitted rectangle for the cover and all gameplay modes.
   The safe-area calculation lives here only, never in a late override. */
body.frame-opening main,
body.game-shell main,
.cover-panel {
  width:min(
    calc(var(--palace-view-w) - env(safe-area-inset-left,0px) - env(safe-area-inset-right,0px)),
    calc((var(--palace-view-h) - env(safe-area-inset-top,0px) - env(safe-area-inset-bottom,0px)) * 16 / 9)
  );
  height:min(
    calc(var(--palace-view-h) - env(safe-area-inset-top,0px) - env(safe-area-inset-bottom,0px)),
    calc((var(--palace-view-w) - env(safe-area-inset-left,0px) - env(safe-area-inset-right,0px)) * 9 / 16)
  );
}
body.frame-opening main,
body.game-shell main {
  position:fixed;
  left:calc(var(--palace-view-x) + env(safe-area-inset-left,0px) + (var(--palace-view-w) - env(safe-area-inset-left,0px) - env(safe-area-inset-right,0px)) / 2);
  top:calc(var(--palace-view-y) + env(safe-area-inset-top,0px) + (var(--palace-view-h) - env(safe-area-inset-top,0px) - env(safe-area-inset-bottom,0px)) / 2);
  transform:translate(-50%,-50%);
  max-width:none;
  min-height:0;
  box-sizing:border-box;
  margin:0;
  padding:0;
  display:block;
  overflow:hidden;
  container-type:size;
  border-inline:1px solid rgba(215,193,147,.66);
  box-shadow:0 0 0 3px rgba(40,57,49,.45),0 0 0 5px rgba(203,183,141,.24),0 0 36px rgba(16,25,20,.45);
}
/* The game shell retains its existing inner rail inset; opening fills the stage. */
body.game-shell main { padding:4px; }
/* Scene sizes refer to the stage, not to the full device viewport. */
body.frame-opening .scene-frame,
body.frame-opening #intro.scene-frame,
body.frame-opening #creation.scene-frame {
  width:100%;
  height:100%;
}
/* Bind text sizing and story/choice cards to the same inner stage as portraits.
   At compact heights, retain readable fonts with short single-column choices. */
body[data-game='palace']:is(.frame-opening,.game-shell) .dialogue-stage {
  --dialogue-height:20cqh;
}
@media (orientation:landscape) and (max-height:500px) {
  body[data-game='palace']:is(.frame-opening,.game-shell) .dialogue-stage {
    --dialogue-height:22cqh;
  }
}
/* The smallest landscape stages still need room for two normal-size lines. */
@media (orientation:landscape) and (max-height:350px) {
  body[data-game='palace']:is(.frame-opening,.game-shell) .dialogue-stage {
    --dialogue-height:28cqh;
  }
}
/* Global story presentation: a stable bottom caption plus floating choices.
   Fixed card coordinates and decoration are shared by palace-ui.css. */
body[data-game='palace'] :is(#game,#intro) .dialogue-box .dialogue-content {
  width:calc(100% - 28px);
  max-width:calc(100% - 28px);
  flex:none;
  text-align:left;
}
/* Having choices must never make the dialogue card grow taller. */
body[data-game='palace'] :is(#game,#intro) .dialogue-box.choice-state {
  height:var(--dialogue-height);
}
/* The central option list lives OUTSIDE the story box in the same stage.
   Height is bounded so even long choices cannot obscure bottom narration. */
body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices {
  position:absolute;
  z-index:7;
  left:50%;
  top:42%;
  transform:translate(-50%,-50%);
  width:min(54%,620px);
  max-height:min(49cqh,390px);
  box-sizing:border-box;
  padding:2px 4px;
  display:grid;
  grid-template-columns:minmax(0,1fr);
  /* Keep short lists centered; long labels must start inside the scrollport. */
  align-content:safe center;
  gap:6px;
  overflow-x:hidden;
  overflow-y:auto;
  scrollbar-width:thin;
  background:none;
  border:0;
}
body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices > button {
  display:block;
  width:100%;
  margin:0;
  min-height:36px;
  padding:5px 12px;
  font-size:var(--font-story);
  line-height:1.3;
  text-align:center;
  white-space:normal;
  overflow-wrap:anywhere;
  background:rgba(255,249,237,.82);
  color:var(--dialogue-ink);
  border:1px solid var(--dialogue-edge);
  border-radius:8px;
  box-shadow:0 2px 8px rgba(79,54,27,.10);
}
body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices > button:hover:not(:disabled) {
  background:rgba(255,251,242,.95);
  border-color:rgba(148,113,69,.48);
}
body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices > :is(p,details,small) {
  margin:0;
  width:100%;
  box-sizing:border-box;
  font-size:var(--font-note);
  text-align:center;
}
body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices details {
  padding:5px 10px;
  border:1px solid var(--dialogue-edge);
  border-radius:8px;
  background:rgba(255,249,237,.82);
}
body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices .choice-pager {
  display:flex;
  justify-content:center;
  gap:8px;
  margin:0;
}
body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices .choice-pager button {
  margin:0;
  width:auto;
  min-height:28px;
  padding:3px 9px;
  font-size:var(--font-body);
  background:rgba(255,249,237,.92);
  color:var(--dialogue-ink);
  border:1px solid var(--dialogue-edge);
  border-radius:7px;
}
@media (orientation:landscape) and (max-height:500px) {
  body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices {
    gap:4px;
    max-height:48cqh;
  }
  body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices > button {
    min-height:34px;
    padding:4px 9px;
  }
}

/* Phone choice tray geometry; palace-ui.css supplies shared card placement. */
html[data-mobile-device='true'] body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices {
  top:40%;
  max-height:56cqh;
  gap:4px;
  align-content:start;
}
html[data-mobile-device='true'] body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices > button {
  min-height:30px;
  padding:2px 8px;
  line-height:1.25;
}
html[data-mobile-device='true'] body[data-game='palace'] .dialogue-stage > .dialogue-floating-choices > details {
  padding:3px 8px;
}
/* Phone captions use their full inner width; only the outer padding remains.
   Safe-area spacing protects the home indicator without desktop changes. */
html[data-mobile-device='true'] body[data-game='palace'] .dialogue-stage > .dialogue-box {
  padding:5px 8px;
}
html[data-mobile-device='true'] body[data-game='palace'] :is(#game,#intro) .dialogue-box .dialogue-content {
  width:100%;
  max-width:100%;
}
