:root {
  color-scheme: light;
  font-family: system-ui, "Microsoft JhengHei", sans-serif;
  font-size: 16px;
  color: #23302e;
  background: #ecede9;
  --paper: #fcfcf8;
  --ink: #23302e;
  --muted: #54605b;
  --line: #cbd2cb;
  --accent: #305b50;
  --accent-soft: #dce9df;
}

* { box-sizing: border-box; }
body { margin: 0; }
button, select { font: inherit; color: inherit; }
button {
  background: var(--paper);
  border: 1px solid #aebbb3;
  border-radius: 6px;
  padding: .45rem .65rem;
  cursor: pointer;
}
button:hover { background: #edf1ea; }
button:focus-visible, select:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid #296e64;
  outline-offset: 2px;
}
button:disabled { color: #58645c; cursor: default; }
[hidden] { display: none !important; }

.app-header { background: #23322f; color: #f8f9f4; }
.app-header-inner {
  min-height: 66px;
  max-width: 1550px;
  margin: auto;
  padding: 8px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 30px; height: 30px; color: #b8d4c0; }
.brand-title { font-size: 1.22rem; font-weight: 750; letter-spacing: .12em; }
.chapter-title { color: #d2ded5; font-size: .78rem; }
.prototype-tag {
  color: #e3e9e1;
  border: 1px solid #768981;
  border-radius: 5px;
  padding: 4px 8px;
  font-size: .75rem;
  white-space: nowrap;
}
.workspace {
  width: min(calc(100vw - 48px), calc(max(620px, var(--scene-panorama-width, 100vw) * .70) + var(--dev-extra-width, 0px)));
  height: calc(var(--scene-height, 100dvh) * .95);
  display: grid;
  grid-template-columns: minmax(0, 1fr) 278px;
  gap: 18px;
}
.development .workspace { --dev-extra-width: 296px; }
.book, .dev-panel {
  min-width: 0;
  min-height: 0;
  border: 1px solid #bac4ba;
  border-radius: 10px;
  overflow: hidden;
  background: var(--paper);
}
.book { display: grid; grid-template-rows: auto minmax(0, 1fr); }
.book-header {
  min-height: 68px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.book-controls { display: flex; align-items: center; gap: 8px; }
.eyebrow { color: var(--muted); font-size: .76rem; letter-spacing: .12em; margin: 0; }
h1 { margin: 1px 0 0; font-size: 1.32rem; line-height: 1.3; }
.page-nav {
  display: grid;
  grid-template-columns: 42px 70px 42px;
  align-items: center;
  gap: 7px;
}
#previous-page { grid-column: 1; }
.page-number {
  grid-column: 2;
  text-align: center;
  font-size: .86rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
#next-page { grid-column: 3; }
.page-arrow { padding: 0; height: 38px; font-size: 1.26rem; background: #eef2ec; }
.page-arrow.guiding {
  background: #e2efda;
  color: #234c3d;
  border-color: #568465;
  animation: page-pulse 1.9s ease-in-out infinite;
}
@keyframes page-pulse {
  0%, 100% { box-shadow: 0 0 0 2px #79a67b55; }
  50% { box-shadow: 0 0 0 5px #79a67b66, 0 0 12px #79a67b66; }
}

.book-content {
  position: relative;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 7fr) minmax(0, 3fr);
}
.question-scroll, .word-scroll, .ending-view {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}
.question-content { max-width: 1120px; margin: auto; padding: 20px 24px 30px; }
.group {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffefa;
  margin: 0 0 15px;
  padding: 13px 16px 16px;
}
.group:last-child { margin-bottom: 0; }
.group-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin: 0 0 12px;
}
.group-head h2 { margin: 0; font-size: 1.12rem; }
.group-actions { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; justify-content: flex-end; }
.verify { font-size: .84rem; white-space: nowrap; }
.group-status {
  background: #eff0eb;
  color: #49544d;
  border-radius: 4px;
  padding: 3px 7px;
  white-space: nowrap;
  font-size: .76rem;
}
.group-status[data-result="wrong"] { color: #6b4632; background: #f6eadf; }
.group-status[data-result="passed"] { color: #204c3a; background: #deede0; }
.question-content:has(.person-group) { padding: 8px 12px 12px; }
.person-group {
  padding: 6px 9px 8px;
  border-color: #ccbaa0;
  background: #f9f3e7;
}
.person-group .group-head { margin-bottom: 5px; }
.person-group .verify { min-height: 32px; padding-block: 4px; }
.people {
  --portrait-width: 226px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 268px));
  justify-content: center;
  gap: 8px;
}
.person {
  display: grid;
  align-content: start;
  gap: 6px;
  min-width: 0;
  border: 1px solid #cbb89e;
  border-radius: 6px;
  background: #fcf7ed;
  padding: 7px;
}
.portrait {
  justify-self: center;
  width: min(100%, var(--portrait-width));
  aspect-ratio: 1.04;
  overflow: hidden;
  border-radius: 4px;
  background: #ae8867;
}
.portrait img { display: block; width: 100%; height: 100%; object-fit: cover; object-position: 50% 0%; }
.person-fields { display: grid; gap: 4px; min-width: 0; }
.person-field { display: grid; grid-template-columns: max-content minmax(0, 1fr); align-items: center; gap: 6px; }
.person-field > span { color: var(--muted); font-size: .78rem; white-space: nowrap; }
.blank {
  display: inline-block;
  vertical-align: baseline;
  min-width: 5.5em;
  min-height: 28px;
  max-width: 100%;
  line-height: 1.25;
  margin: 1px 2px;
  padding: 2px 8px;
  border: 1px dashed #809689;
  color: #314a41;
  background: #f0f4ed;
  font-size: .9em;
}
.blank[data-filled="true"] { background: #e6efe6; border-style: solid; border-color: #9cafa0; font-weight: 650; }
.blank[aria-pressed="true"] { background: #dcebe0; border-style: solid; border-color: #49765e; box-shadow: inset 0 0 0 1px #49765e; }
.blank.drop-target { background: #d5eadc; border-color: #28634e; outline: 2px solid #6c977d; outline-offset: 1px; }
.blank:disabled { opacity: .86; }
.person .blank {
  width: 100%;
  min-width: 0;
  min-height: 34px;
  margin: 0;
  padding-inline: 4px;
  font-size: .9rem;
  overflow-wrap: anywhere;
}
.event-paragraph {
  margin: 0 0 .85em;
  line-height: 1.95;
  font-size: 1rem;
  overflow-wrap: anywhere;
}
.event-paragraph:last-child { margin-bottom: 0; }

.word-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border-top: 1px solid #abbcaf;
  background: #edf1eb;
}
.word-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 38px;
  padding: 4px 12px;
  border-bottom: 1px solid #cbd7cc;
  background: #e3eae2;
}
.word-label { display: flex; gap: 8px; align-items: center; white-space: nowrap; }
.word-label h2 { margin: 0; font-size: .9rem; }
.word-count { color: #3c594b; font-size: .8rem; font-variant-numeric: tabular-nums; }
.selected-word { margin-left: auto; text-align: right; color: #40584d; font-size: .8rem; }
.word-scroll { position: relative; background: #f0f3ed; }
.word-canvas { position: relative; min-height: 100%; min-width: 100%; }
.empty-words { margin: 0; padding: 13px; color: var(--muted); font-size: .9rem; }
.word {
  position: absolute;
  left: 0;
  top: 0;
  border-radius: 6px;
  border-color: #a6b7a8;
  background: #fffefa;
  color: #26372e;
  padding: 5px 10px;
  font-size: .93rem;
  font-weight: 580;
  line-height: 1.3;
  white-space: nowrap;
  cursor: grab;
  touch-action: pan-y;
  user-select: none;
}
.word[aria-pressed="true"] { background: #d7eadb; border-color: #427658; box-shadow: inset 0 0 0 1px #427658; }
.word.drag-origin { opacity: .42; }
.word.drag-ghost { position: fixed; z-index: 20; pointer-events: none; cursor: grabbing; box-shadow: 0 4px 12px #23302e35; }
.word.word-measure { position: fixed; visibility: hidden; pointer-events: none; }
.expand-words { display: none; font-size: .78rem; padding: 2px 6px; }

.book-content.is-ending { grid-template-rows: minmax(0, 1fr); }
.ending-view { position: relative; isolation: isolate; background: #f7f7ee; }
.ending-paper {
  position: absolute;
  inset: 12px;
  z-index: -1;
  border: 1px solid #b7c3b5;
  border-radius: 8px;
  background: repeating-linear-gradient(#f8f8f0 0 26px, #e7ece0 26px 27px);
  opacity: .55;
  filter: blur(5px);
}
.ending-intro, .ending-body {
  position: relative;
  max-width: 710px;
  margin: 0 auto;
  padding: 28px 24px;
  font-size: 1.04rem;
  line-height: 1.9;
}
.ending-intro {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
}
.ending-intro p { max-width: 460px; }
.ending-body h2 { margin: 0 0 12px; font-size: 1.36rem; }
.ending-body p { margin: 0 0 1em; }
.ending-view.revealed .ending-paper { opacity: 0; }
.ending-view.reveal-now .ending-paper { transition: opacity .65s ease-out; }
.ending-view.reveal-now .ending-body { animation: text-reveal .68s ease-out both; }
@keyframes text-reveal { from { opacity: 0; } to { opacity: 1; } }

.dev-panel { display: flex; flex-direction: column; background: #f8f8f3; }
.dev-header {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 46px;
  padding: 8px 14px;
  background: #f0ede1;
  border-bottom: 1px solid #d6d1bc;
}
.dev-header h2 { margin: 0; font-size: .96rem; }
.dev-dot { width: 8px; height: 8px; border-radius: 50%; background: #876c38; }
.dev-content { overflow: auto; padding: 12px 13px; font-size: .84rem; }
.dev-intro { margin: 0 0 12px; color: var(--muted); }
.dev-section { display: grid; gap: 7px; margin-bottom: 13px; }
.dev-section + .dev-section { padding-top: 11px; border-top: 1px solid var(--line); }
.dev-section h3 { font-size: .9rem; margin: 0; }
.primary-button { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 650; }
.primary-button:hover { background: #264b41; }
.inline-controls, .dev-buttons { display: flex; gap: 6px; }
.inline-controls select { flex: 1 1 0; min-width: 0; padding: 4px 5px; background: #fffefa; border: 1px solid #aebbb3; border-radius: 6px; }
.inline-controls button, .dev-buttons button { white-space: nowrap; }
.dev-buttons { flex-wrap: wrap; }
.dev-caption { margin: 0; font-size: .75rem; color: var(--muted); }
.group-progress { margin: 1px 0; padding: 0; list-style: none; display: grid; gap: 2px; }
.group-progress li { display: flex; justify-content: space-between; gap: 6px; padding: 1px 2px; font-size: .77rem; }
.group-progress b { color: #525e55; }
.group-progress [data-passed="true"] b { color: #2d6950; }
.ending-states { display: grid; gap: 2px; font-size: .76rem; color: var(--muted); }
.dev-feedback { margin: 0; color: #2e5a47; min-height: 1.3em; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }

@media (max-width: 850px) {
  .app-header-inner { padding-inline: 12px; }
  .workspace {
    width: calc(100vw - 24px);
    height: calc(100dvh - 28px);
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    gap: 10px;
  }
  .development .workspace { grid-template-rows: minmax(0, 1fr) minmax(0, min(29%, 220px)); }
  .book-header { padding-inline: 10px; }
  .question-content { padding: 12px 10px 22px; }
  .group { padding: 9px; }
  .people { grid-template-columns: minmax(0, 258px); }
  .person .blank, .person-group .verify { min-height: 44px; }
  .book:has(.person-group) :is(.page-arrow, #close-book) { min-width: 44px; min-height: 44px; }
  .book:has(.person-group) .page-nav { grid-template-columns: 44px 58px 44px; }
  .expand-words { display: inline-block; }
  .word-panel.expanded { position: absolute; inset: 25% 0 0; z-index: 3; box-shadow: 0 -4px 16px #23302e2c; }
}
@media (max-width: 440px) {
  .prototype-tag { font-size: .66rem; padding-inline: 5px; }
  .page-nav { grid-template-columns: 34px 58px 34px; gap: 4px; }
  .group-head { flex-wrap: wrap; }
  .group-actions { justify-content: flex-start; }
  .word-toolbar { padding-inline: 6px; }
  .selected-word { font-size: .72rem; }
}
@media (851px <= width <= 1440px) {
  .people { --portrait-width: 192px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  /* Keep the shared canvas viewport in the same place when turning pages. */
  dialog#book-overlay { padding-block: 0; }
  .workspace {
    width: min(960px, calc(100vw - 24px));
    height: calc(100dvh - 20px);
  }
  .development .workspace {
    grid-template-columns: minmax(0, 1fr) clamp(180px, 24vw, 238px);
    grid-template-rows: minmax(0, 1fr);
    gap: 10px;
  }
  .book-header { min-height: 49px; padding: 2px 10px; }
  .book-heading .eyebrow { display: none; }
  .book .page-nav { grid-template-columns: 44px 58px 44px; }
  .book :is(.page-arrow, #close-book) { width: 44px; height: 44px; }
  .question-content:has(.person-group) { padding: 6px 8px 9px; }
  .person-group { padding: 5px 6px 6px; margin-bottom: 8px; }
  .person-group .group-head h2 { font-size: 1.06rem; }
  .people { grid-template-columns: repeat(2, minmax(0, 352px)); }
  .person {
    --landscape-portrait: clamp(106px, calc(30dvh - 1px), 132px);
    grid-template-columns: min(40%, var(--landscape-portrait)) minmax(0, 1fr);
    align-items: center;
    align-content: center;
    gap: 9px;
    padding: 6px;
  }
  .portrait { width: 100%; }
  .person .blank, .person-group .verify { min-height: 44px; }
  .word-panel {
    grid-template-columns: clamp(120px, 18vw, 150px) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
  }
  .word-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    align-content: center;
    gap: 0 4px;
    padding: 2px 5px;
    border-bottom: 0;
    border-right: 1px solid #cbd7cc;
  }
  .word-label { display: grid; gap: 0; }
  .selected-word { grid-column: 1 / -1; grid-row: 2; margin: 0; text-align: left; font-size: .72rem; }
  .expand-words { display: inline-block; grid-column: 2; grid-row: 1; min-width: 44px; min-height: 44px; }
  .word-panel.expanded { position: absolute; inset: 25% 0 0; z-index: 3; box-shadow: 0 -4px 16px #23302e2c; }
  .empty-words { padding: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  .page-arrow.guiding { animation: none; box-shadow: 0 0 0 3px #79a67b66; }
  .ending-view.reveal-now .ending-paper { transition: none; }
  .ending-view.reveal-now .ending-body { animation: none; }
}
