:root {
  --adventure-green-950: #0c3027;
  --adventure-green-900: #103f32;
  --adventure-green-800: #185442;
  --adventure-green-700: #246c54;
  --adventure-amber: #f3bc58;
  --adventure-amber-deep: #d99432;
  --adventure-paper: #fffaf0;
  --adventure-paper-strong: #fffdf7;
  --adventure-ink: #17352b;
  --adventure-muted: #63746c;
  --adventure-line: rgba(23, 53, 43, 0.14);
  --adventure-danger: #b94337;
  --adventure-shadow: 0 18px 54px rgba(8, 34, 27, 0.16);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-width: 0;
  overflow-x: clip;
}

body.puzzle-adventure-page {
  min-height: 100vh;
  color: var(--adventure-ink);
  background:
    radial-gradient(circle at 12% 18%, rgba(243, 188, 88, 0.12), transparent 27rem),
    linear-gradient(145deg, #eef1e7 0%, #f8f3e7 48%, #e8efe6 100%);
  font-family: "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
}

html[data-theme="dark"] {
  --adventure-ink: #f3f0e8;
  --adventure-muted: #b9bec9;
  --adventure-line: rgba(243, 240, 232, 0.18);
}

html[data-theme="dark"] body.puzzle-adventure-page {
  color: #f3f0e8;
  background: #12121f;
}

html[data-theme="dark"] .library-heading h1,
html[data-theme="dark"] .map-heading h1,
html[data-theme="dark"] .play-copy h1,
html[data-theme="dark"] .summary-card h1 {
  color: #f7e4ac;
}

html[data-theme="dark"] .eyebrow,
html[data-theme="dark"] .book-count,
html[data-theme="dark"] .puzzle-feedback.correct,
html[data-theme="dark"] .summary-metric strong {
  color: #82d4ad;
}

html[data-theme="dark"] .puzzle-feedback.wrong {
  color: #ff9b8f;
}

html[data-theme="dark"] .book-card,
html[data-theme="dark"] .map-controls,
html[data-theme="dark"] .quiet-button,
html[data-theme="dark"] .action-button,
html[data-theme="dark"] .map-pager button,
html[data-theme="dark"] .route-tab,
html[data-theme="dark"] .promotion-options button,
html[data-theme="dark"] .promotion-cancel,
html[data-theme="dark"] .section-control select,
html[data-theme="dark"] .promotion-panel,
html[data-theme="dark"] .summary-card,
html[data-theme="dark"] .summary-metric {
  color: var(--adventure-ink);
  background: #1e1e3a;
  border-color: var(--adventure-line);
}

html[data-theme="dark"] .quiet-button:hover,
html[data-theme="dark"] .action-button:hover,
html[data-theme="dark"] .map-pager button:hover,
html[data-theme="dark"] .route-tab:hover,
html[data-theme="dark"] .promotion-options button:hover,
html[data-theme="dark"] .promotion-cancel:hover {
  background: #292947;
}

html[data-theme="dark"] .route-tab[aria-selected="true"],
html[data-theme="dark"] .action-primary,
html[data-theme="dark"] .action-primary:hover {
  color: #fff;
  background: var(--adventure-green-800);
}

button,
select {
  font: inherit;
}

button {
  touch-action: manipulation;
}

#puzzle-adventure-app {
  width: 100%;
  max-width: 1160px;
  margin: 0 auto;
  padding: 34px 20px 54px;
}

.adventure-view[hidden],
[hidden] {
  display: none !important;
}

.adventure-view:focus {
  outline: none;
}

.page-error {
  margin: 0 0 16px;
  padding: 12px 16px;
  border: 1px solid rgba(185, 67, 55, 0.25);
  border-radius: 12px;
  color: #842c24;
  background: #fff1ed;
  text-align: center;
  font-weight: 700;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--adventure-green-700);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.18em;
}

.library-heading {
  max-width: 670px;
  margin: 32px auto 30px;
  text-align: center;
}

.library-heading h1,
.map-heading h1,
.play-copy h1,
.summary-card h1 {
  margin: 0;
  color: var(--adventure-green-950);
  font-family: Georgia, "Songti SC", serif;
  font-weight: 700;
  letter-spacing: -0.025em;
}

.library-heading h1 {
  font-size: clamp(38px, 6vw, 66px);
}

.library-heading > p:last-child {
  margin: 14px 0 0;
  color: var(--adventure-muted);
  font-size: 17px;
}

.book-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 310px), 1fr));
  gap: 20px;
  max-width: 820px;
  margin: 0 auto;
}

.book-card {
  position: relative;
  display: grid;
  grid-template-columns: 118px 1fr;
  min-height: 190px;
  padding: 18px;
  overflow: hidden;
  border: 1px solid rgba(23, 53, 43, 0.12);
  border-radius: 24px;
  color: var(--adventure-ink);
  background: rgba(255, 253, 247, 0.9);
  box-shadow: var(--adventure-shadow);
  cursor: pointer;
  text-align: left;
  transition: transform 220ms ease, box-shadow 220ms ease, border-color 220ms ease;
}

.book-card:hover,
.book-card:focus-visible {
  transform: translateY(-4px);
  border-color: rgba(217, 148, 50, 0.55);
  box-shadow: 0 24px 64px rgba(8, 34, 27, 0.22);
  outline: none;
}

.book-cover {
  position: relative;
  width: 100px;
  height: 148px;
  align-self: center;
  overflow: hidden;
  border-radius: 9px 15px 15px 9px;
  color: var(--adventure-amber);
  background: linear-gradient(145deg, var(--adventure-green-700), var(--adventure-green-950));
  box-shadow: 8px 10px 22px rgba(11, 43, 34, 0.3);
}

.book-cover img {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-cover img.book-fallback {
  padding: 22px 14px;
  object-fit: contain;
}

.book-copy {
  min-width: 0;
  align-self: center;
}

.book-copy h2 {
  margin: 0 0 8px;
  font-family: Georgia, "Songti SC", serif;
  font-size: 24px;
}

.book-copy p {
  display: -webkit-box;
  margin: 0;
  overflow: hidden;
  color: var(--adventure-muted);
  line-height: 1.65;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.book-count {
  display: inline-flex;
  margin-top: 16px;
  color: var(--adventure-green-700);
  font-size: 13px;
  font-weight: 800;
}

.empty-state {
  padding: 50px 20px;
  color: var(--adventure-muted);
  text-align: center;
}

.map-toolbar,
.play-toolbar {
  display: grid;
  grid-template-columns: 120px minmax(0, 1fr) 120px;
  align-items: start;
  margin-bottom: 18px;
}

.map-heading,
.play-copy {
  grid-column: 2;
  text-align: center;
}

.map-heading h1,
.play-copy h1 {
  font-size: clamp(27px, 4vw, 40px);
}

.map-heading p:last-child,
.play-breadcrumb,
.puzzle-progress {
  margin: 7px 0 0;
  color: var(--adventure-muted);
  font-size: 14px;
}

.quiet-button,
.action-button,
.map-pager button,
.route-tab,
.promotion-options button,
.promotion-cancel {
  min-height: 44px;
  border: 1px solid var(--adventure-line);
  border-radius: 12px;
  color: var(--adventure-ink);
  background: rgba(255, 253, 247, 0.92);
  cursor: pointer;
  font-weight: 800;
  transition: transform 180ms ease, background 180ms ease, border-color 180ms ease;
}

.quiet-button {
  justify-self: start;
  padding: 0 14px;
}

.legacy-resume {
  grid-column: 3;
  justify-self: end;
  padding: 0 12px;
  font-size: 12px;
}

.map-toolbar-actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; }
.map-toolbar-actions .action-button { min-height: 44px; padding: 8px 12px; font-size: 13px; }
#map-review[aria-pressed="true"] { color: #fff; background: var(--adventure-green-800); border-color: var(--adventure-green-800); }
.review-note { margin: 0 0 12px; color: var(--adventure-muted); font-size: 14px; text-align: center; }
.review-progress { margin: 2px 0; color: var(--adventure-muted); font-size: 13px; line-height: 20px; text-align: center; }

.quiet-button:hover,
.action-button:hover,
.map-pager button:hover,
.route-tab:hover,
.promotion-options button:hover,
.promotion-cancel:hover {
  border-color: rgba(36, 108, 84, 0.48);
  background: #fff;
  transform: translateY(-1px);
}

.map-controls {
  display: grid;
  grid-template-columns: minmax(260px, auto) minmax(220px, 1fr) auto;
  gap: 12px;
  align-items: center;
  margin-bottom: 16px;
  padding: 10px;
  border: 1px solid var(--adventure-line);
  border-radius: 18px;
  background: rgba(255, 253, 247, 0.86);
  box-shadow: 0 10px 30px rgba(8, 34, 27, 0.08);
}

.route-tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.route-tab {
  padding: 0 18px;
  white-space: nowrap;
}

.route-tab[aria-selected="true"] {
  border-color: var(--adventure-green-800);
  color: #fff;
  background: var(--adventure-green-800);
}

.section-control {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  color: var(--adventure-muted);
  font-size: 13px;
  font-weight: 800;
}

.section-control > span {
  flex-shrink: 0;
  white-space: nowrap;
}

.section-control select {
  width: 100%;
  min-height: 44px;
  min-width: 0;
  padding: 0 34px 0 12px;
  border: 1px solid var(--adventure-line);
  border-radius: 12px;
  color: var(--adventure-ink);
  background: #fffdf7;
}

.map-pager {
  display: grid;
  grid-template-columns: 68px 62px 68px;
  align-items: center;
  gap: 4px;
}

.map-pager button {
  width: 68px;
  padding: 0;
  font-size: 12px;
}

.map-pager button:disabled,
.level-node:disabled,
.action-button:disabled,
.quiet-button:disabled,
.route-tab:disabled {
  cursor: default;
  opacity: 0.56;
  transform: none;
}

#map-page-label {
  text-align: center;
  font-size: 13px;
  font-weight: 800;
}

.map-range-label {
  margin: -6px 0 12px;
  color: var(--adventure-muted);
  font-size: 13px;
  font-weight: 800;
  text-align: center;
}

.expedition-map {
  position: relative;
  aspect-ratio: 1672 / 941;
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 26px;
  background: var(--adventure-green-900);
  box-shadow: var(--adventure-shadow);
  isolation: isolate;
}

.map-picture,
.map-picture img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.map-picture img {
  object-fit: cover;
}

.expedition-map::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(7, 34, 27, 0.06), rgba(7, 34, 27, 0.24));
}

.level-grid {
  position: absolute;
  inset: 0;
  z-index: 1;
}

.level-node {
  position: absolute;
  left: var(--road-x);
  top: var(--road-y);
  transform: translate(-50%, -50%);
  display: grid;
  place-items: center;
  width: clamp(44px, 5vw, 60px);
  aspect-ratio: 1;
  padding: 0;
  border: 3px solid var(--adventure-green-800);
  border-radius: 50%;
  color: var(--adventure-green-950);
  background: #fffdf7;
  box-shadow: 0 3px 6px rgba(4, 25, 19, 0.32);
  cursor: pointer;
  font-size: clamp(15px, 2vw, 22px);
  font-weight: 900;
  text-shadow: none;
  transition: transform 180ms ease, box-shadow 180ms ease;
}

.level-node:not(:disabled):hover,
.level-node:not(:disabled):focus-visible {
  transform: translate(-50%, -50%) scale(1.08);
  outline: 3px solid #fffdf7;
  outline-offset: 3px;
  box-shadow: 0 4px 8px rgba(4, 25, 19, 0.4);
}

.level-node.completed {
  border-color: #fffdf7;
  color: #fff;
  background: var(--adventure-green-700);
}

.level-node.in_progress {
  border-color: #fff6d5;
  color: var(--adventure-green-950);
  background: var(--adventure-amber);
}

.level-node.review {
  border-color: #fffdf7;
  color: #fff;
  background: var(--adventure-danger);
}

.level-node.last-visited { outline: 3px dashed #fffdf7; outline-offset: 4px; }
.level-last-visited, .level-review-progress { position: absolute; left: 50%; transform: translateX(-50%); padding: 3px 6px; border-radius: 5px; color: #fffdf7; background: #103f32; font-size: 11px; line-height: 16px; font-weight: 700; white-space: nowrap; }
.level-last-visited { bottom: calc(100% + 8px); }
.level-review-progress { top: calc(100% + 4px); }
.level-node:nth-child(1) .level-review-progress { top: 50%; left: auto; right: calc(100% + 8px); transform: translateY(-50%); }

/* Centers follow the road in map-landscape.webp, from foreground to castle. */
.level-node:nth-child(1) { --road-x: 14%; --road-y: 92%; }
.level-node:nth-child(2) { --road-x: 22%; --road-y: 80%; }
.level-node:nth-child(3) { --road-x: 29%; --road-y: 68%; }
.level-node:nth-child(4) { --road-x: 38%; --road-y: 60%; }
.level-node:nth-child(5) { --road-x: 44%; --road-y: 52%; }
.level-node:nth-child(6) { --road-x: 56%; --road-y: 50%; }
.level-node:nth-child(7) { --road-x: 66%; --road-y: 45%; }
.level-node:nth-child(8) { --road-x: 58%; --road-y: 38%; }
.level-node:nth-child(9) { --road-x: 71%; --road-y: 32.5%; }
.level-node:nth-child(10) { --road-x: 77%; --road-y: 19%; }

body.puzzle-playing #puzzle-adventure-app {
  padding: 12px 16px max(12px, env(safe-area-inset-bottom));
}

.play-stage {
  display: grid;
  grid-template-areas: "heading" "board" "tools";
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(160px, 1fr) auto;
  gap: 8px;
  width: 100%;
  max-width: 720px;
  height: min(calc(100svh - 100px - env(safe-area-inset-bottom)), calc(100vw + 224px));
  margin: 0 auto;
}

.play-toolbar {
  grid-area: heading;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0;
  min-width: 0;
}

.play-toolbar .quiet-button { min-height: 44px; height: 44px; padding: 0 10px; font-size: 13px; }
.puzzle-identity { display: flex; align-items: baseline; justify-content: flex-end; flex-wrap: wrap; gap: 3px 12px; font-size: 14px; min-width: 0; }
.puzzle-identity strong { font-size: 16px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.puzzle-rating { white-space: nowrap; font-variant-numeric: tabular-nums; }

.puzzle-board-area {
  grid-area: board;
  container-type: size;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
}

.puzzle-board {
  width: min(100cqw, 100cqh);
  aspect-ratio: 1;
  max-width: 100%;
  overflow: hidden;
  border-radius: 6px;
}

.play-panel { grid-area: tools; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.play-copy { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; text-align: left; }
.play-copy h1 { margin: 0; font-size: 16px; line-height: 24px; white-space: nowrap; }
.play-context { min-width: 0; font-size: 15px; line-height: 1.5; text-align: right; }
.play-context summary, .study-notes summary { cursor: pointer; padding: 4px 0; }
.play-context[open] { flex: 1; }
.play-breadcrumb, .puzzle-progress { font-size: 15px; line-height: 1.5; margin: 3px 0 0; overflow-wrap: anywhere; }

.play-result { display: flex; flex-direction: column; justify-content: center; gap: 2px; min-height: 52px; }
.puzzle-feedback { padding: 4px 10px; border-radius: 6px; color: var(--adventure-muted); font-size: 14px; font-weight: 700; text-align: center; line-height: 22px; }
.puzzle-feedback:empty, .rating-change:empty { display: none; }

/* Chessground measures this container immediately after setting its size. */
.puzzle-board cg-container { transition: none !important; }

.puzzle-feedback.correct {
  color: var(--adventure-green-800);
  background: rgba(36, 108, 84, 0.09);
  animation: feedback-in 460ms ease both;
}

.puzzle-feedback.wrong {
  color: var(--adventure-danger);
  background: rgba(185, 67, 55, 0.09);
  animation: wrong-shake 300ms ease both;
}

.auto-next-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}

.auto-next-control input { width: 18px; height: 18px; accent-color: var(--adventure-green-800); }
.auto-next-control input:focus-visible { outline: 3px solid #8a4f00; outline-offset: 3px; }
.rating-change { font-size: 13px; line-height: 20px; text-align: center; font-variant-numeric: tabular-nums; }
.rating-change.rating-gain { color: var(--adventure-green-800); font-weight: 700; }
.rating-change.rating-loss { color: var(--adventure-danger); font-weight: 700; }
html[data-theme="dark"] .rating-change.rating-gain { color: #82d4ad; }
html[data-theme="dark"] .rating-change.rating-loss { color: #ff9b8f; }
.play-actions { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 4px 12px; }
.play-actions > .action-button { flex: 1; min-height: 44px; height: auto; padding: 8px 12px; font-size: 16px; border-radius: 8px; }
.play-actions #play-recovery { flex-basis: 100%; }
.study-panel { width: 100%; margin: 0; text-align: center; }
.study-navigation { display: flex; justify-content: center; gap: 6px; }
.study-navigation .study-icon { width: 44px; height: 44px; min-height: 44px; padding: 10px; border: 0; border-radius: 6px; background: transparent; }
.study-icon svg { display: block; width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.study-icon:focus-visible { outline: 2px solid var(--adventure-green-700); outline-offset: 2px; }
@media (any-pointer: coarse) {
  .study-navigation { gap: 8px; flex-wrap: wrap; }
  .study-navigation .study-icon { flex: 0 0 56px; width: 56px; height: 56px; min-height: 56px; padding: 14px; border-radius: 8px; background: rgba(36, 108, 84, 0.08); }
  .study-icon svg { width: 28px; height: 28px; }
}
.study-position { margin: 0; font-size: 12px; line-height: 18px; color: var(--adventure-muted); }
.study-notes { font-size: 13px; text-align: left; }
.study-notes[open] { max-height: 160px; overflow-y: auto; }
.study-explanation { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 14px; line-height: 1.6; }
.study-explanation:empty { display: none; }
.study-ai { margin: 0; border-bottom: 1px solid var(--adventure-line); text-align: left; }
.study-ai-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; height: 44px; }
.study-ai-toggle { display: inline-flex; align-items: center; gap: 8px; flex: none; min-height: 44px; padding: 4px 10px; border: 1px solid #9ca3a9; border-radius: 6px; background: transparent; color: var(--adventure-ink); font: inherit; font-size: 14px; font-weight: 700; cursor: pointer; }
.study-ai-switch { display: inline-block; width: 30px; height: 18px; padding: 3px; border-radius: 12px; background: #9ca3a9; }
.study-ai-switch::after { content: ''; display: block; width: 12px; height: 12px; border-radius: 50%; background: #fff; }
.study-ai-toggle[aria-checked="true"] { border-color: #246c54; background: #246c54; color: #fff; }
.study-ai-toggle[aria-checked="true"] .study-ai-switch { background: #18503e; }
.study-ai-toggle[aria-checked="true"] .study-ai-switch::after { transform: translateX(12px); }
.study-ai-toggle:disabled { opacity: .55; cursor: default; }
.study-ai-toggle:focus-visible { outline: 3px solid var(--adventure-amber-deep); outline-offset: 2px; }
.study-ai-state { min-width: 0; text-align: right; color: var(--adventure-muted); font-size: 12px; line-height: 18px; }
.study-ai-status, .study-ai-depth { display: block; }
.study-ai-depth { font-variant-numeric: tabular-nums; }
.study-ai[aria-busy="true"] .study-ai-status::before { content: ''; display: inline-block; width: 10px; height: 10px; margin-right: 5px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; vertical-align: -1px; animation: study-ai-spin .8s linear infinite; }
@keyframes study-ai-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .study-ai[aria-busy="true"] .study-ai-status::before { animation: none; } }
.study-ai-details { height: 100px; overflow-y: auto; overscroll-behavior: contain; }
.study-ai-evaluation { margin: 4px 0; font-size: 12px; color: var(--adventure-muted); }
.study-ai-state, .study-ai-evaluation { color: var(--adventure-ink); }
.study-ai-lines { display: grid; gap: 8px; }
.study-ai-line { display: grid; grid-template-columns: 48px minmax(0, 1fr); gap: 8px; margin: 0; font-size: 13px; line-height: 20px; }
.study-ai-score { font-variant-numeric: tabular-nums; white-space: nowrap; }
.study-ai-pv { min-width: 0; overflow-wrap: normal; }
.study-ai-move { display: inline-block; white-space: nowrap; }
@media (max-width: 899px) and (min-height: 501px) {
  .play-stage:has(#study-ai:not([hidden])) { height: calc(100svh - 100px - env(safe-area-inset-bottom)); }
  /* Keep the board origin stable when study closes but board size is unchanged. */
  .puzzle-board-area { align-items: start; }
}
@media (max-width: 899px) and (max-height: 700px) {
  .study-ai-details { height: 72px; }
  .play-panel:has(#study-ai:not([hidden])) { gap: 4px; }
  .play-panel:has(#study-ai:not([hidden])) .play-result { min-height: 0; }
}
#next-puzzle { min-width: 112px; }

/* Keep accumulated results outside the fixed board/tool grid so new rows cannot move the board. */
.answer-history { max-width: 720px; margin: 16px auto 0; padding: 12px 0; border-top: 1px solid var(--adventure-line); }
.answer-history-summary { margin: 0 0 8px; color: var(--adventure-muted); font-size: 13px; }
.answer-history-list { display: flex; flex-wrap: wrap; gap: 6px; list-style: none; margin: 0; padding: 0; }
.answer-history-item { min-width: 44px; padding: 7px 8px; border: 1px solid transparent; border-radius: 5px; text-align: center; font-size: 13px; line-height: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.answer-history-link { display: block; color: inherit; text-decoration: none; }
.answer-history-link:hover { text-decoration: underline; }
.answer-history-link:focus-visible { outline: 3px solid var(--adventure-amber-deep); outline-offset: 4px; border-radius: 2px; }
.answer-history-correct { color: #18533b; background: #e1f1e7; border-color: #a4cdb4; }
.answer-history-wrong { color: #922f24; background: #fbe5e1; border-color: #e2b4ac; }
html[data-theme="dark"] .answer-history-correct { color: #b3eccb; background: #163f2d; border-color: #39734e; }
html[data-theme="dark"] .answer-history-wrong { color: #ffc1b8; background: #542b27; border-color: #96584e; }
@media (min-width: 900px) { .answer-history { max-width: none; } }

.wrong-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  width: 100%;
}

.wrong-actions .action-button { height: auto; min-height: 44px; padding: 6px 12px; font-size: 14px; border-radius: 8px; }

@media (max-width: 899px) {
  body.puzzle-playing #cr-nav-root { min-height: 56px; }
  body.puzzle-playing #cr-nav-root .cr-header-nav { display: none; }
  body.puzzle-playing #cr-nav-root .cr-header { min-height: 56px; grid-template-areas: "brand actions"; row-gap: 0; }
}

@media (min-width: 900px), (min-width: 600px) and (max-height: 500px) {
  .play-stage {
    max-width: none;
    height: min(760px, calc(100svh - 100px - env(safe-area-inset-bottom)));
    grid-template-areas: "board heading" "board tools";
    grid-template-columns: minmax(0, 1fr) 300px;
    grid-template-rows: auto minmax(0, 1fr);
    column-gap: 24px;
    row-gap: 20px;
  }
  .puzzle-board-area { align-self: stretch; }
  .play-panel { gap: 20px; overflow-y: auto; padding: 2px; }
  .play-copy { display: block; }
  .play-copy h1 { font-size: 22px; margin-bottom: 8px; }
  .play-context { text-align: left; }
  .puzzle-identity { flex-direction: column; align-items: flex-end; gap: 4px; }
  .puzzle-feedback { padding: 8px; font-size: 16px; }
  .play-actions > #next-puzzle { flex-basis: 100%; }
  .play-actions { gap: 4px; }
}

@media (min-width: 900px) and (min-height: 501px) {
  body.puzzle-playing #puzzle-adventure-app { max-width: 1440px; }
  .play-stage { grid-template-columns: minmax(0, 1fr) clamp(340px, 32vw, 480px); row-gap: 16px; }
  .play-panel { gap: 16px; }
  .study-ai-details { height: 160px; }
  .study-ai-evaluation { margin: 4px 0 12px; }
  .study-ai-lines { gap: 12px; }
  .study-ai-line { grid-template-columns: 52px minmax(0, 1fr); gap: 12px; font-size: 14px; line-height: 24px; }
}

@media (min-width: 1200px) and (min-height: 501px) {
  .play-stage { grid-template-columns: minmax(0, 1fr) clamp(420px, 32vw, 480px); }
}

@media (min-width: 900px) and (min-height: 850px) {
  .study-ai-details { height: 192px; }
}

@media (min-width: 600px) and (max-height: 500px) {
  body.puzzle-playing #cr-nav-root { display: none; }
  body.puzzle-playing #puzzle-adventure-app { padding: 8px max(8px, env(safe-area-inset-right)) 8px max(8px, env(safe-area-inset-left)); }
  .play-stage { height: calc(100svh - 16px - env(safe-area-inset-bottom)); grid-template-columns: minmax(0, 1fr) 280px; gap: 8px 16px; }
  .play-panel { gap: 6px; }
  .play-copy { display: flex; }
  .play-copy h1 { font-size: 16px; margin: 0; }
  .puzzle-feedback { font-size: 14px; padding: 4px 8px; }
  .play-actions > #next-puzzle { flex-basis: auto; }
}

.action-button {
  padding: 0 18px;
}

.action-primary {
  border-color: var(--adventure-green-800);
  color: #fff;
  background: var(--adventure-green-800);
}

.action-primary:hover {
  color: #fff;
  background: var(--adventure-green-700);
}

@keyframes feedback-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes wrong-shake {
  0%, 100% { transform: translateX(0); }
  33% { transform: translateX(-5px); }
  66% { transform: translateX(5px); }
}

.promotion-dialog {
  width: min(92vw, 430px);
  padding: 0;
  border: 0;
  border-radius: 22px;
  color: var(--adventure-ink);
  background: transparent;
  box-shadow: 0 30px 90px rgba(4, 25, 19, 0.42);
}

.promotion-dialog::backdrop {
  background: rgba(7, 34, 27, 0.58);
  backdrop-filter: blur(4px);
}

.promotion-panel {
  padding: 24px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 22px;
  background: var(--adventure-paper-strong);
  text-align: center;
}

.promotion-panel h2 {
  margin: 0 0 18px;
  font-family: Georgia, "Songti SC", serif;
}

.promotion-options {
  display: grid;
  grid-template-columns: repeat(4, minmax(44px, 1fr));
  gap: 8px;
}

.promotion-options button {
  display: grid;
  place-items: center;
  min-width: 56px;
  min-height: 68px;
  padding: 0;
}
.promotion-options img { display: block; width: 56px; height: 56px; object-fit: contain; }
.promotion-options button:focus-visible { outline: 3px solid var(--adventure-amber-deep); outline-offset: 2px; }

.promotion-cancel {
  width: 100%;
  margin-top: 10px;
}

.summary-card {
  max-width: 820px;
  margin: 42px auto 0;
  padding: 44px;
  border: 1px solid var(--adventure-line);
  border-radius: 30px;
  background: rgba(255, 253, 247, 0.94);
  box-shadow: var(--adventure-shadow);
  text-align: center;
  animation: summary-in 620ms cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

.summary-emblem {
  display: grid;
  place-items: center;
  width: 90px;
  aspect-ratio: 1;
  margin: 0 auto 18px;
  border-radius: 50%;
  color: var(--adventure-green-950);
  background: var(--adventure-amber);
  box-shadow: 0 10px 25px rgba(217, 148, 50, 0.34);
}

.summary-emblem img {
  width: 66px;
  height: 66px;
  object-fit: contain;
}

.summary-card h1 {
  font-size: clamp(30px, 5vw, 48px);
}

.summary-note {
  margin: 16px 0 0;
  color: var(--adventure-muted);
  font-weight: 700;
}

.summary-metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin: 32px 0;
}

.summary-metric {
  padding: 18px 10px;
  border: 1px solid var(--adventure-line);
  border-radius: 16px;
  background: #fff;
}

.summary-metric strong,
.summary-metric span {
  display: block;
}

.summary-metric strong {
  color: var(--adventure-green-800);
  font-family: Georgia, serif;
  font-size: 32px;
}

.summary-metric span {
  margin-top: 5px;
  color: var(--adventure-muted);
  font-size: 12px;
  font-weight: 700;
}

.summary-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

@keyframes summary-in {
  from { opacity: 0; transform: translateY(20px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (max-width: 760px) {
  .expedition-map { aspect-ratio: 1122 / 1402; }
  .level-node { width: clamp(44px, 8vw, 56px); font-size: 16px; }
  .level-last-visited { top: 50%; bottom: auto; left: auto; right: calc(100% + 8px); transform: translateY(-50%); padding: 3px; font-size: 10px; }
  .level-node:nth-child(even) .level-last-visited { left: calc(100% + 8px); right: auto; }
  .level-node:nth-child(1) .level-last-visited { transform: translateY(calc(-50% - 26px)); }
  /* Portrait has its own road; do not crop the landscape under these centers. */
  .level-node:nth-child(1) { --road-x: 48%; --road-y: 92%; }
  .level-node:nth-child(2) { --road-x: 64%; --road-y: 86%; }
  .level-node:nth-child(3) { --road-x: 49%; --road-y: 77%; }
  .level-node:nth-child(4) { --road-x: 67%; --road-y: 69%; }
  .level-node:nth-child(5) { --road-x: 51%; --road-y: 61%; }
  .level-node:nth-child(6) { --road-x: 66%; --road-y: 52%; }
  .level-node:nth-child(7) { --road-x: 49%; --road-y: 43%; }
  .level-node:nth-child(8) { --road-x: 64%; --road-y: 34%; }
  .level-node:nth-child(9) { --road-x: 45%; --road-y: 27%; }
  .level-node:nth-child(10) { --road-x: 58%; --road-y: 17%; }
  .map-controls {
    grid-template-columns: 1fr auto;
  }

  .route-tabs {
    grid-column: 1 / -1;
  }
}

@media (max-width: 600px) {
  body.puzzle-adventure-page .cr-header-nav .cr-nav-link:not(.active),
  body.puzzle-adventure-page .cr-user-info {
    display: none;
  }

  #puzzle-adventure-app {
    padding: 18px 8px 34px;
  }

  .library-heading {
    margin: 22px auto 24px;
  }

  .library-heading h1 {
    font-size: 40px;
  }

  .library-heading > p:last-child {
    padding: 0 12px;
    font-size: 15px;
  }

  .book-card {
    grid-template-columns: 90px 1fr;
    min-height: 158px;
    padding: 14px;
    border-radius: 19px;
  }

  .book-cover {
    width: 76px;
    height: 116px;
  }

  .book-copy h2 {
    font-size: 21px;
  }

  .book-copy p {
    font-size: 13px;
    -webkit-line-clamp: 2;
  }

  .book-count {
    margin-top: 9px;
  }

  .map-toolbar {
    grid-template-columns: 72px minmax(0, 1fr) 72px;
    margin-bottom: 12px;
  }

  .quiet-button {
    padding: 0 8px;
    font-size: 12px;
  }

  .legacy-resume {
    width: 72px;
    padding: 4px;
    line-height: 1.25;
  }

  .map-toolbar-actions .action-button { width: 72px; padding: 4px; font-size: 12px; line-height: 1.25; }

  .map-heading h1 {
    font-size: 25px;
  }

  .map-heading p:last-child,
  .play-breadcrumb,
  .puzzle-progress {
    font-size: 12px;
  }

  .map-controls {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 8px;
    border-radius: 15px;
  }

  .route-tabs,
  .section-control,
  .map-pager {
    grid-column: 1;
  }

  .section-control {
    width: 100%;
  }

  .map-pager {
    justify-self: center;
  }

  .expedition-map {
    aspect-ratio: 1122 / 1402;
    border-radius: 18px;
  }

  body.puzzle-playing #puzzle-adventure-app { padding: 8px 8px max(8px, env(safe-area-inset-bottom)); }
  .play-stage { height: min(calc(100svh - 80px - env(safe-area-inset-bottom)), calc(100vw + 224px)); }

  .wrong-actions {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
  }

  .summary-card {
    margin-top: 20px;
    padding: 28px 14px;
    border-radius: 22px;
  }

  .summary-emblem {
    width: 72px;
  }

  .summary-metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin: 24px 0;
  }

  .summary-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .summary-actions .action-primary {
    grid-column: 1 / -1;
    grid-row: 1;
  }
}

.puzzle-reduced-motion *,
.puzzle-reduced-motion *::before,
.puzzle-reduced-motion *::after {
  scroll-behavior: auto !important;
  animation-duration: 1ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 1ms !important;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
}

/* Reserve room for the session summary and result tiles in a desktop viewport. */
@media (min-width: 900px) and (min-height: 501px) {
  .play-stage { height: min(680px, calc(100svh - 220px - env(safe-area-inset-bottom))); }
  .answer-history-list { max-height: 80px; overflow-y: auto; }
}

/* Study controls may grow below the board; they must not consume its row. */
@media (max-width: 899px) and (min-height: 501px) {
  .play-stage,
  .play-stage:has(#study-ai:not([hidden])) {
    height: auto;
    grid-template-rows: auto auto auto;
  }
  .puzzle-board-area {
    height: min(720px, calc(100vw - 32px), calc(100svh - 260px));
    align-items: start;
  }
}
@media (max-width: 480px) and (min-height: 501px) {
  .puzzle-board-area { height: min(calc(100vw - 16px), calc(100svh - 260px)); }
}
