#book-overlay {
  --ink: #3a2c20;
  --muted: #65533e;
  --line: #c9b18c;
  --paper: #f1e2c7;
  --paper-light: #f7ead0;
  --paper-dark: #e3c9a1;
  --accent: #38564a;
  --grain: url("paper-grain.png");
  --heading-font: "LXGW WenKai TC", "霞鶩文楷 TC", PMingLiU, "Noto Serif TC", serif;
}

#book-overlay {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  border: 0;
  margin: 0;
  padding: 0;
  background: #17130dc9;
  display: grid;
  place-items: center;
  overflow: clip;
}
#book-overlay::backdrop { background: transparent; }
#book-overlay:not([open]) { visibility: hidden; pointer-events: none; }

#book-workspace {
  --binding: 19px;
  --cover-top: 3px;
  --cover-right: 6px;
  --cover-bottom: 8px;
  --tray-edge: 4px;
  --header: 52px;
  --title-size: 20px;
  --module-gap: 10px;
  --turn-lane: 34px;
  --page-chrome: calc(var(--header) + var(--cover-top) + var(--cover-bottom) + 2px);
  --available: calc(100% - var(--page-chrome) - 2 * var(--tray-edge) - var(--module-gap));
  position: relative;
  isolation: isolate;
  width: min(calc(100vw - 60px), clamp(720px, calc(70.4vw - 242px), 1110px));
  height: min(calc(100dvh - 20px), clamp(574px, calc(100dvh - 194px), 874px));
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, calc(var(--page-chrome) + var(--available) * .7)) minmax(0, 1fr);
  gap: var(--module-gap);
}
#book-workspace[data-reading] {
  grid-template-rows: minmax(0, 1fr) 0;
  row-gap: 0;
}
#book-workspace[data-reading] > .tray-frame:not(.tray-fade) { display: none; }
.book-shell {
  position: relative;
  display: grid;
  min-height: 0;
  min-width: 0;
  padding: var(--cover-top) var(--cover-right) var(--cover-bottom) var(--binding);
  border-radius: 9px 11px 11px 9px;
  background:
    repeating-linear-gradient(127deg, transparent 0 3px, #b7814b14 4px 5px),
    linear-gradient(100deg, #4a2b1d, #67432c 12%, #513322 95%);
  box-shadow: inset 0 0 0 1px #a6794d, inset 0 0 0 3px #321b12, 0 4px 12px #140a0690;
}
.book-shell::after {
  content: "";
  position: absolute;
  inset: 12px 3px 3px calc(var(--binding) + 2px);
  border-radius: 3px 6px 6px 3px;
  background: repeating-linear-gradient(0deg, #a88a63 0 1px, #ddc49b 1px 3px);
  box-shadow: 0 1px 1px #25140d;
  pointer-events: none;
}
.binding {
  position: absolute;
  z-index: 2;
  left: 2px;
  top: 3px;
  bottom: 4px;
  width: calc(var(--binding) - 2px);
  border-radius: 6px 1px 1px 6px;
  background:
    repeating-linear-gradient(0deg, #3d24199c 0 1px, #9a6b4566 2px 4px, transparent 5px 33px) 52% 0 / 35% 100% no-repeat,
    linear-gradient(90deg, #361c13, #875d3e 38%, #583821 74%, #301a13);
  box-shadow: inset 1px 0 0 #c694606e, 1px 0 2px #41271672;
  pointer-events: none;
}
.book {
  position: relative;
  z-index: 1;
  isolation: isolate;
  overflow: visible;
  grid-template-rows: var(--header) minmax(0, 1fr);
  border: 1px solid #c09b6e;
  border-radius: 2px 7px 8px 2px;
  --sheet-color: linear-gradient(90deg, #d9bd94 0%, #f5e7cd 3.4%, #f8ecd6 17%, #f7ead1 94%, #e7d1ae 100%);
  background: var(--grain), var(--sheet-color);
  box-shadow: inset 0 0 0 1px #fff3d38c;
}
.book-header {
  position: relative;
  display: flex;
  align-items: center;
  min-height: 0;
  padding: 0 206px 0 18px;
  border-bottom: 1px solid #b99c74;
  background: linear-gradient(0deg, #b895612a, transparent 70%);
}
.book-heading { display: flex; align-items: center; gap: 6px; min-width: 0; }
.book-title {
  display: inline-block;
  font: 700 var(--title-size) / 1.2 var(--heading-font);
  letter-spacing: .04em;
  white-space: nowrap;
}
.book-controls {
  position: absolute;
  z-index: 7;
  top: calc(var(--header) / 2);
  right: 10px;
  transform: translateY(-50%);
}
.book .page-nav { grid-template-columns: 44px 54px 44px; }
.page-number { color: #684f34; font-size: 14px; letter-spacing: .04em; }
.page-arrow, #close-book, .verify, .expand-words {
  border-color: #a8875e;
  background: #f7ead2;
  color: #4a3525;
  box-shadow: inset 0 1px 0 #fff7e0;
}
.page-arrow { font: 1.55rem / 1 Georgia, serif; }
.book :is(.page-arrow, #close-book) { width: 44px; height: 44px; padding: 0; }
#close-book { font-size: 1.26rem; line-height: 1; }
.page-arrow:hover, #close-book:hover, .verify:hover, .expand-words:hover { background: #fdf1dc; }
.page-arrow.guiding { border-color: #568465; background: #e2efda; }
.book-content, .book-content.is-ending { grid-template-rows: minmax(0, 1fr); overflow: hidden; border-radius: 0 0 7px 2px; }
.question-scroll { scrollbar-color: #a98d69 #e5d2b2; scrollbar-width: thin; }
.group { border-color: #bca17d; background: #f9efdce8; box-shadow: 0 1px 2px #67492917; }
.group-head h2 { font-family: var(--heading-font); color: #4a3425; letter-spacing: .04em; }
.group-status { background: #eadcc5; color: #655039; }
.person-group { background: #f6ead5ed; }
.person { border-color: #c4a985; background: #fcf2e0; }
.person .blank { color: #30473b; }
.event-paragraph { color: #3b2d21; }
.tray-frame {
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
  border: var(--tray-edge) solid #74502f;
  border-top-color: #ad8153;
  border-bottom-color: #5d3c24;
  border-radius: 7px;
  background: #7c5736;
  box-shadow: 0 2px 6px #150b06a6;
}
.word-panel { border: 0; background: #c2aa84; }
.word-toolbar {
  border-color: #a58255;
  background: var(--grain), repeating-linear-gradient(4deg, #a77a4730 0 1px, transparent 2px 6px),
    linear-gradient(#d9b78a, #caa16f);
  box-shadow: inset 0 1px 0 #f4d5a5a0;
}
.word-label h2 { color: #442d1e; }
.word-count, .selected-word { color: #4a5946; }
.word-scroll {
  background: var(--grain), repeating-linear-gradient(90deg, #aa91620b 0 1px, transparent 1px 3px),
    linear-gradient(#dfcfb1, #e6d9bd);
  box-shadow: inset 0 2px 4px #553b2454;
}
.word-scroll { scrollbar-color: #a48a65 #dcc8a5; scrollbar-width: thin; }
.word { background: #faf1de; border-color: #a99471; }
.word:hover { background: #f7efd9; }
.word[aria-pressed="true"] { background: #d7eadb; }
.tray-fade {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  overflow: clip;
}
#book-workspace.settling .ending-view { overflow: clip; }
#word-panel.expanded { outline: 2px solid #8d613c; }
.ending-view { background: transparent; }
.ending-feedback {
  margin: 0 auto;
  padding: 0 16px 16px;
  text-align: center;
}
.ending-feedback a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding-inline: 12px;
  color: #60402e;
  text-underline-offset: .24em;
}
.ending-feedback a:hover { color: #352116; }
.ending-feedback a:focus-visible { outline: 2px solid #60402e; outline-offset: 2px; }
.ending-paper { background: repeating-linear-gradient(#f7ead0 0 26px, #e9d5b5 26px 27px); }

/* The folded paper is clipped to the book, never the tray. */
.turn-stage {
  position: absolute;
  z-index: 4;
  inset: 0 0 0 calc(-1 * var(--turn-lane));
  pointer-events: none;
  overflow: hidden;
  overflow: clip;
  --fold-perspective: clamp(1460px, 153vw, 2840px);
}
.book:has(> .turn-stage) > :is(.book-header, .book-content) { visibility: hidden; }
.turn-perspective {
  position: absolute;
  inset: 0;
  perspective: var(--fold-perspective);
  perspective-origin: var(--turn-lane) 51%;
}
.fold-mesh {
  position: absolute;
  inset: 0 0 0 var(--turn-lane);
  transform-style: preserve-3d;
}
.turn-underpage {
  position: absolute;
  inset: 0 0 0 var(--turn-lane);
  background: var(--sheet-color);
}
.turn-shadow {
  position: absolute;
  inset-block: 0;
  left: var(--turn-lane);
  width: min(18%, 158px);
  background: linear-gradient(90deg, transparent, #4e2b1aaa 46%, #87614062 65%, transparent);
  opacity: 0;
}
.fold-slice {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
}
.fold-face, .fold-light {
  position: absolute;
  inset: 0;
  overflow: clip;
  backface-visibility: hidden;
}
/* The slice already transforms in 3D. Keep its front coplanar to avoid
   the extra Paint work Chromium recorded during the first turn. */
.fold-front { background: var(--sheet-color); }
.fold-back {
  transform: rotateY(180deg) translateZ(.02px);
  background: repeating-linear-gradient(0deg, transparent 0 24px, #956f3613 24px 25px),
    linear-gradient(0deg, #dac099, #f6e6c6 25%, #f3e0b9 88%, #c3a67c);
}
.fold-light { pointer-events: none; }
.fold-edge { box-shadow: inset -1px 0 #fff1cf; }
.turn-sheet {
  position: absolute;
  top: 0;
  overflow: hidden;
  background: var(--sheet-color);
}
.turn-sheet .book-header { height: var(--header); }
.turn-reader { position: absolute; inset: var(--header) 0 0; overflow: hidden; }
.turn-sheet-content {
  position: absolute;
  margin: 0;
  overflow: visible;
  pointer-events: none;
  user-select: none;
}
.turn-sealed {
  height: 100%;
  background: radial-gradient(ellipse at center, #fff4dc, #e4caa7);
}
@media (max-width: 850px) {
  #book-workspace {
    --binding: 11px;
    --cover-top: 3px;
    --cover-right: 4px;
    --cover-bottom: 4px;
    --tray-edge: 3px;
    --header: 52px;
    --module-gap: 6px;
    --turn-lane: 12px;
    width: calc(100vw - 20px);
    height: calc(100dvh - 16px);
  }
  .book-shell::after { inset: 8px 2px 2px calc(var(--binding) + 1px); }
  .book-controls { gap: 5px; }
  .book-header { padding-inline-start: 8px; }
}
@media (max-width: 440px) {
  .book-header { padding-inline-end: 214px; }
  .word-toolbar { gap: 5px; }
  .word-label { gap: 4px; }
  .selected-word { min-width: 0; line-height: 1.15; }
}
@media (orientation: landscape) and (max-height: 500px) {
  #book-workspace {
    --binding: 8px;
    --cover-top: 2px;
    --cover-right: 3px;
    --cover-bottom: 2px;
    --tray-edge: 2px;
    --header: 48px;
    --title-size: 17px;
    --module-gap: 2px;
    --turn-lane: 10px;
    width: min(960px, calc(100vw - 20px));
    height: calc(100dvh - 10px);
  }
  .book-shell { border-radius: 6px; }
  .book-shell::after { inset: 6px 2px 1px calc(var(--binding) + 1px); }
  .binding { top: 2px; bottom: 2px; border-radius: 3px 0 0 3px; }
  .book-header { padding: 0 206px 0 9px; }
  .page-number { font-size: 13px; }
  .tray-frame { border-radius: 5px; }
  .word-panel { grid-template-columns: clamp(177px, 22vw, 206px) minmax(0, 1fr); }
  .word-toolbar { grid-template-columns: minmax(0, 1fr) auto; }
  .expand-words { grid-column: 2; }
  /* D75 compact rhythm, scoped to the two event pages and their inert image. */
  .question-content:has(> .group > .event-paragraph) { padding: 8px 14px 12px; }
  .question-content > .group:has(> .event-paragraph) { padding: 7px 11px 10px; margin-bottom: 10px; }
  .question-content > .group:has(> .event-paragraph):last-child { margin-bottom: 0; }
  .group:has(> .event-paragraph) .group-head { gap: 8px; margin-bottom: 6px; }
  .group:has(> .event-paragraph) .verify { min-height: 44px; padding-block: 4px; }
  .event-paragraph { line-height: 1.72; margin-bottom: .58em; }
  .event-paragraph:last-child { margin-bottom: 0; }
  .event-paragraph .blank { min-height: 32px; margin-block: 0; white-space: nowrap; }
}
@media (prefers-reduced-motion: reduce) {
}

/* Development controls are siblings of the book and never affect player layout. */
.book-layout { display: flex; align-items: stretch; gap: 10px; }
.development .book-layout { --dev-width: clamp(164px, 22vw, 258px); }
.development #book-workspace { max-width: calc(100vw - var(--dev-width) - 30px); }
.development .book-layout > .dev-panel { width: var(--dev-width); }
.development .book-layout > .dev-panel .dev-content { flex: 1; }
.book-layout > .dev-panel { min-width: 0; }
@media (max-width: 540px) and (orientation: portrait) {
  .development .book-layout { flex-direction: column; gap: 6px; }
  .development #book-workspace { max-width: none; height: calc(76dvh - 15px); }
  .development .book-layout > .dev-panel { width: 100%; height: 24dvh; }
}
