:root {
  --ink: #172033;
  --muted: #627184;
  --paper: #fff4dc;
  --paper-2: #f7e5bd;
  --line: rgba(92, 69, 42, .22);
  --navy: #061f25;
  --teal: #0f766e;
  --teal-2: #14b8a6;
  --gold: #d6a860;
  --surface: rgba(6, 31, 37, .74);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--navy); }

body {
  margin: 0;
  min-height: 100%;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #eef7f5;
  background:
    linear-gradient(90deg, rgba(6, 31, 37, .96), rgba(8, 54, 60, .82), rgba(6, 31, 37, .96)),
    linear-gradient(180deg, #04171c, #082b31);
  overflow-x: hidden;
}

body.storybook-expanded {
  overflow: hidden;
}

body.storybook-reading {
  overflow: hidden;
}

button, a { font: inherit; }

button { cursor: pointer; }

.storybook-app {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

body.storybook-expanded .storybook-app {
  position: fixed;
  inset: 0;
  z-index: 999;
  min-height: 100vh;
  background:
    linear-gradient(90deg, rgba(6, 31, 37, .98), rgba(8, 54, 60, .9), rgba(6, 31, 37, .98)),
    linear-gradient(180deg, #04171c, #082b31);
}

body.storybook-expanded.storybook-reading .storybook-app {
  grid-template-rows: 1fr auto;
}

body.storybook-expanded.storybook-reading .book-topbar,
body.storybook-expanded.storybook-reading .book-sidebar,
body.storybook-expanded.storybook-reading .page-controls,
body.storybook-expanded.storybook-reading .narrator-control,
body.storybook-expanded.storybook-reading .read-progress {
  display: none;
}

body.storybook-expanded.storybook-reading .book-layout {
  grid-template-columns: 1fr;
}

body.storybook-expanded.storybook-reading .book-main {
  padding: 18px;
  align-items: center;
}

body.storybook-expanded.storybook-reading .book-frame {
  width: min(100vw, 1340px);
  height: min(88vh, 760px);
  min-height: 0;
}

body.storybook-expanded.storybook-reading .reader-controls {
  position: static;
  padding: 10px 14px 14px;
  background: rgba(3, 19, 24, .92);
}

body.storybook-expanded.storybook-reading .audio-panel {
  justify-content: center;
}

.book-topbar {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto minmax(260px, 1fr);
  align-items: center;
  gap: 18px;
  padding: 14px 22px;
  border-bottom: 1px solid rgba(255, 255, 255, .12);
  background: rgba(3, 19, 24, .82);
  backdrop-filter: blur(18px);
}

.book-brand, .book-actions { display: flex; align-items: center; gap: 10px; }

.book-brand {
  color: #cceee9;
  text-decoration: none;
  font-weight: 800;
}

.book-brand img { width: 34px; height: 34px; border-radius: 8px; }

.book-topbar h1 {
  margin: 0;
  color: #f6ddb1;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(25px, 3vw, 40px);
  font-weight: 700;
  letter-spacing: 0;
  text-align: center;
}

.book-actions { justify-content: flex-end; }

.book-actions a, .book-actions button,
.page-controls button, .audio-panel button {
  border: 1px solid rgba(255, 255, 255, .16);
  background: rgba(9, 45, 52, .78);
  color: #effaf8;
  border-radius: 8px;
  padding: 10px 13px;
  text-decoration: none;
  font-weight: 800;
  transition: background .18s ease, transform .18s ease, border-color .18s ease;
}

.book-actions button[data-action="fullscreen"] {
  min-width: 118px;
}

.book-actions a:hover, .book-actions button:hover,
.page-controls button:hover, .audio-panel button:hover {
  background: rgba(20, 184, 166, .24);
  border-color: rgba(94, 234, 212, .45);
}

.book-actions a:focus-visible, .book-actions button:focus-visible,
.page-controls button:focus-visible, .audio-panel button:focus-visible,
.range-link:focus-visible {
  outline: 3px solid rgba(94, 234, 212, .55);
  outline-offset: 2px;
}

.book-layout {
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr);
  min-height: 0;
}

.book-sidebar {
  padding: 28px 16px 16px;
  border-right: 1px solid rgba(255, 255, 255, .12);
  background: rgba(2, 25, 31, .76);
}

.book-sidebar h2 {
  margin: 0 0 14px;
  color: #b5c6c4;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0;
}

.range-link {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  padding: 12px 14px;
  border: 1px solid rgba(255, 255, 255, .12);
  border-radius: 8px;
  background: rgba(6, 43, 50, .6);
  color: #d7e8e6;
  font-weight: 800;
  text-align: left;
}

.range-link span {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #b9d87b;
  flex: 0 0 auto;
}

.range-link.active {
  color: #fff;
  background: linear-gradient(135deg, #0f766e, #159978);
  border-color: rgba(94, 234, 212, .52);
}

.read-progress {
  margin-top: 18px;
  padding: 16px 10px;
  border: 1px solid rgba(255, 255, 255, .12);
  border-radius: 8px;
  text-align: center;
  background: rgba(4, 34, 40, .72);
}

.progress-ring {
  width: 86px;
  height: 86px;
  margin: 0 auto 10px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: conic-gradient(#5eead4 var(--progress, 0%), rgba(255, 255, 255, .12) 0);
}

.progress-ring span {
  width: 68px;
  height: 68px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #08242a;
  color: #e5fbf7;
  font-size: 21px;
  font-weight: 800;
}

.read-progress strong { display: block; color: #fff; }
.read-progress small { color: #a8bdba; }

.book-main {
  min-width: 0;
  display: grid;
  align-items: start;
  justify-items: center;
  padding: 16px 18px;
}

.book-frame {
  width: min(100%, 1040px);
  height: min(68vh, 640px);
  min-height: 500px;
  display: grid;
  place-items: center;
}

.flipbook { width: 100%; height: 100%; }

.story-page {
  position: relative;
  overflow: hidden;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(120, 85, 38, .08), transparent 9%, transparent 91%, rgba(120, 85, 38, .08)),
    radial-gradient(circle at 18% 12%, rgba(255, 255, 255, .62), transparent 22%),
    linear-gradient(180deg, var(--paper), #f7e7c5);
  border: 1px solid rgba(92, 69, 42, .24);
  box-shadow: inset 0 0 28px rgba(87, 62, 30, .14);
}

.story-page::after {
  content: "";
  position: absolute;
  inset: 16px;
  pointer-events: none;
  border: 1px solid rgba(95, 70, 34, .16);
  border-radius: 8px;
}

.cover-page, .back-cover {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 44px;
  text-align: center;
  color: #f7ead0;
  background:
    linear-gradient(135deg, #07333a, #0f766e 55%, #113a38);
}

.cover-page {
  padding: 0;
  gap: 0;
  justify-content: flex-end;
  background: #06252b;
}

.cover-page::after, .back-cover::after {
  border-color: rgba(246, 221, 177, .34);
}

.cover-mark {
  width: 92px;
  height: 92px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(246, 221, 177, .5);
  border-radius: 8px;
  font-size: 28px;
  font-weight: 950;
  color: #09282d;
  background: #f6ddb1;
}

.cover-art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cover-title-band {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  width: 100%;
  padding: 24px 22px 28px;
  color: #334155;
  background: rgba(248, 247, 243, .88);
  border-top: 1px solid rgba(255, 255, 255, .45);
  box-shadow: 0 -18px 36px rgba(15, 23, 42, .18);
}

.cover-page h2, .back-cover h2 {
  margin: 0;
  max-width: 420px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(32px, 5vw, 52px);
  line-height: 1.05;
  letter-spacing: 0;
}

.cover-page h2 {
  max-width: none;
  color: #4a5568;
  font-size: clamp(28px, 4.2vw, 44px);
}

.cover-page p, .back-cover p {
  margin: 0;
  max-width: 420px;
  color: #dcefeb;
  font-weight: 700;
}

.cover-page p {
  max-width: none;
  color: #5b6472;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 17px;
  font-weight: 600;
}

.cover-grid {
  display: grid;
  grid-template-columns: repeat(5, 42px);
  gap: 9px;
  justify-content: center;
}

.cover-grid span {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .13);
  font-size: 23px;
}

.back-links { display: grid; gap: 10px; width: min(100%, 320px); }
.back-links a {
  padding: 12px 14px;
  border-radius: 8px;
  color: #082b31;
  background: #f6ddb1;
  text-decoration: none;
  font-weight: 900;
}

.page-label {
  position: absolute;
  top: 26px;
  left: 28px;
  z-index: 1;
  min-width: 78px;
  padding: 8px 14px;
  border-radius: 8px;
  background: #07333a;
  color: #f6ddb1;
  text-align: center;
  font-weight: 950;
  font-size: 20px;
  box-shadow: 0 8px 18px rgba(15, 23, 42, .16);
}

.art-page {
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  padding: 28px;
  min-height: 0;
}

.art-page .page-label,
.art-page .scene-title,
.art-page .scene-caption,
.art-page .key-grid {
  display: none;
}

.scene {
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  gap: 0;
  min-height: 0;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 8px;
  padding: 0;
  background: transparent;
  overflow: hidden;
}

.scene-title {
  color: #173238;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 25px;
  font-weight: 800;
  text-align: center;
}

.scene-media {
  align-self: stretch;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 10px;
  width: 100%;
  height: 100%;
  min-height: 0;
}

.scene-media.split {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.scene-media img {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  object-fit: cover;
  object-position: center;
  border: 1px solid rgba(34, 50, 58, .12);
  border-radius: 8px;
  box-shadow: 0 12px 24px rgba(70, 46, 20, .1);
}

.scene-caption {
  color: #51616f;
  font-size: 13px;
  font-weight: 800;
  text-align: center;
}

.key-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  padding: 0;
  margin: 0;
  list-style: none;
}

.key-grid.nine { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.key-grid li {
  min-width: 0;
  padding: 8px 4px;
  border-radius: 8px;
  color: #10242b;
  background: rgba(255, 255, 255, .5);
  border: 1px solid rgba(34, 50, 58, .1);
  text-align: center;
  font-size: 10px;
  font-weight: 950;
}

.text-page {
  display: grid;
  align-items: center;
  padding: 68px 42px 38px;
}

.story-copy {
  margin: 0;
  color: #172033;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(18px, 1.55vw, 23px);
  line-height: 1.58;
  text-wrap: pretty;
}

.story-copy strong {
  color: #0a2e34;
  font-weight: 950;
}

.story-copy .keyword-mark {
  color: #0a2e34;
  font-weight: 950;
  white-space: nowrap;
}

.story-copy .keyword-mark sup {
  position: relative;
  top: -0.48em;
  margin-left: 2px;
  color: #0f766e;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .42em;
  font-weight: 950;
  line-height: 1;
}

.scene-green { --line: rgba(29, 81, 47, .25); }
.scene-blue { --line: rgba(24, 59, 90, .24); }
.scene-amber { --line: rgba(87, 49, 25, .25); }
.scene-teal { --line: rgba(26, 74, 80, .24); }
.scene-brown { --line: rgba(60, 37, 27, .24); }
.scene-purple { --line: rgba(52, 32, 76, .24); }
.scene-slate { --line: rgba(48, 62, 79, .24); }
.scene-metal { --line: rgba(50, 61, 57, .24); }
.scene-navy { --line: rgba(23, 47, 61, .24); }
.scene-rose { --line: rgba(88, 39, 58, .24); }

.reader-controls {
  position: sticky;
  bottom: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 10px;
  padding: 12px 22px 16px;
  border-top: 1px solid rgba(255, 255, 255, .12);
  background: rgba(3, 19, 24, .86);
}

.page-controls, .audio-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

.page-status {
  min-width: 230px;
  display: grid;
  gap: 5px;
  justify-items: center;
  color: #d6e6e4;
  font-weight: 800;
}

.dots { display: flex; gap: 8px; }
.dots button {
  width: 11px;
  height: 11px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, .22);
}
.dots button.active { background: #5eead4; }

.audio-panel {
  padding-top: 2px;
}

.audio-panel button:disabled {
  cursor: not-allowed;
  opacity: .46;
}

.play-button {
  min-width: 92px;
  background: linear-gradient(135deg, #0f766e, #14b8a6) !important;
}

.narrator-control {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: #d7e8e6;
  font-size: 14px;
  font-weight: 850;
}

.narrator-control select {
  max-width: min(62vw, 260px);
  min-height: 42px;
  padding: 9px 36px 9px 12px;
  border: 1px solid rgba(255, 255, 255, .18);
  border-radius: 8px;
  color: #f4fbfa;
  background:
    linear-gradient(45deg, transparent 50%, #5eead4 50%) right 15px center / 7px 7px no-repeat,
    linear-gradient(135deg, rgba(9, 45, 52, .92), rgba(6, 64, 70, .92));
  font: inherit;
  font-weight: 850;
  appearance: none;
}

.narrator-control select option {
  color: #0f172a;
  background: #fff;
}

.narrator-control select:focus-visible {
  outline: 3px solid rgba(94, 234, 212, .55);
  outline-offset: 2px;
}

body.no-pageflip .flipbook {
  display: grid;
  place-items: center;
}

body.no-pageflip .story-page {
  display: none;
  width: min(100%, 520px);
  min-height: 640px;
}

body.no-pageflip .story-page.fallback-active { display: grid; }

@media (max-width: 980px) {
  .reader-controls { position: static; }

  .book-topbar {
    grid-template-columns: 1fr;
    justify-items: center;
    padding: 12px;
  }

  .book-actions { justify-content: center; flex-wrap: wrap; }
  .book-layout { grid-template-columns: 1fr; }
  .book-sidebar {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 10px 12px;
    border-right: 0;
    border-bottom: 1px solid rgba(255, 255, 255, .12);
  }

  .book-sidebar h2, .read-progress { display: none; }
  .range-link {
    width: auto;
    min-width: 86px;
    margin: 0;
    white-space: nowrap;
  }

  .book-main { padding: 12px; }
  .book-frame {
    height: min(70vh, 660px);
    min-height: 480px;
  }

  body.storybook-expanded.storybook-reading .book-main {
    padding: 10px;
  }

  body.storybook-expanded.storybook-reading .book-frame {
    width: min(100%, 960px);
    height: calc(100vh - 78px);
  }
}

@media (max-width: 980px) and (orientation: portrait) {
  body.storybook-expanded.storybook-reading .storybook-app {
    width: 100vh;
    height: 100vw;
    min-height: 100vw;
    transform: rotate(90deg) translateY(-100%);
    transform-origin: top left;
  }

  body.storybook-expanded.storybook-reading .book-frame {
    width: min(100vh, 960px);
    height: calc(100vw - 74px);
  }
}

@media (max-width: 620px) {
  .book-brand span { font-size: 13px; }
  .book-topbar h1 { font-size: 26px; }
  .book-actions a, .book-actions button,
  .page-controls button, .audio-panel button {
    padding: 9px 10px;
    font-size: 13px;
  }

  .book-frame {
    height: 68vh;
    min-height: 440px;
  }

  .story-page::after { inset: 10px; }
  .art-page { padding: 18px; }
  .text-page { padding: 58px 22px 28px; }
  .page-label { top: 18px; left: 18px; font-size: 16px; min-width: 68px; }
  .scene { padding: 0; gap: 0; }
  .scene-title { font-size: 21px; }
  .scene-media { gap: 7px; }
  .scene-media.split { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .key-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .key-grid.nine { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .story-copy { font-size: 17px; line-height: 1.48; }
  .page-status { min-width: 170px; }
  .reader-controls { padding: 10px; }
}

@media print {
  body { background: #fff; color: #111827; }
  .book-topbar, .book-sidebar, .reader-controls { display: none; }
  .storybook-app, .book-layout, .book-main, .book-frame, .flipbook { display: block; height: auto; min-height: 0; width: auto; }
  .story-page {
    display: block !important;
    position: relative !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    margin: 0 0 8mm !important;
    padding: 20mm !important;
    page-break-after: always;
    box-shadow: none;
  }
  .cover-page, .back-cover { color: #111827; background: #fff4dc; }
  .scene-media img { min-height: 44px; }
  .story-copy { font-size: 17px; line-height: 1.55; }
}
