:root {
  --reader-purple: #352060;
  --reader-purple-mid: #6139a6;
  --reader-teal: #087b78;
  --reader-blue: #176cb3;
  --reader-ink: #20223a;
  --reader-muted: #5e6175;
  --reader-paper: #fff;
  --reader-bg: #efedf6;
  --reader-line: #d9d3e5;
  --reader-focus: #f6bd23;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  color: var(--reader-ink);
  background: var(--reader-bg);
  font-family: "Trebuchet MS", "Avenir Next", Avenir, system-ui, sans-serif;
  line-height: 1.5;
}

button, a { font: inherit; }
button:focus-visible, a:focus-visible, summary:focus-visible {
  outline: 4px solid var(--reader-focus);
  outline-offset: 3px;
}

.skip-link {
  position: fixed;
  z-index: 100;
  top: 10px;
  left: 10px;
  padding: 9px 14px;
  color: #fff;
  background: var(--reader-purple);
  border-radius: 8px;
  transform: translateY(-160%);
}
.skip-link:focus { transform: translateY(0); }

.reader-app { min-height: 100vh; display: flex; flex-direction: column; }

.reader-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 14px clamp(16px, 4vw, 52px);
  color: #fff;
  background: var(--reader-purple);
  box-shadow: 0 4px 18px rgba(26, 13, 50, .2);
}

.library-link {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  flex: 0 0 auto;
  padding: 8px 11px;
  color: #fff;
  border-radius: 9px;
  font-weight: 800;
  text-decoration: none;
}
.library-link:hover { background: rgba(255,255,255,.12); }

.reader-title { min-width: 0; text-align: right; }
.reader-title h1 { margin: 0; overflow: hidden; font-size: clamp(1rem, 2.5vw, 1.35rem); line-height: 1.2; text-overflow: ellipsis; white-space: nowrap; }
.reader-title p { margin: 2px 0 0; color: #d9d0e9; font-size: .8rem; }

.reader-main { width: min(1400px, 100%); flex: 1; margin-inline: auto; padding: clamp(18px, 3vw, 38px); }

.reader-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 14px;
  color: var(--reader-muted);
  font-size: .9rem;
  font-weight: 700;
}

.progress-track { height: 7px; overflow: hidden; border-radius: 999px; background: #d8d4e1; }
.progress-bar { width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--reader-purple-mid), #278fc5, #18a9a4); transition: width 260ms ease; }

.stage-wrap { position: relative; margin-top: 20px; }
.book-stage {
  min-height: min(68vh, 850px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(10px, 2vw, 24px);
  overflow: hidden;
  border: 1px solid var(--reader-line);
  border-radius: 24px;
  background: radial-gradient(circle at 50% 35%, #fff 0, #f8f7fb 46%, #e2deea 100%);
  box-shadow: inset 0 1px 0 #fff, 0 16px 36px rgba(53,32,96,.1);
  touch-action: pan-y;
  perspective: 1600px;
}

.pages {
  width: min(100%, 980px);
  height: min(64vh, 790px);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  gap: 0;
}

.pages.is-spread::after {
  content: "";
  width: 12px;
  position: absolute;
  z-index: 3;
  top: 0;
  bottom: 0;
  left: 50%;
  pointer-events: none;
  transform: translateX(-50%);
  background: linear-gradient(90deg, rgba(34,21,60,.16), rgba(255,255,255,.34) 48%, rgba(34,21,60,.14));
  box-shadow: 0 0 11px rgba(34,21,60,.12);
}

.page {
  height: 100%;
  min-width: 0;
  position: relative;
  display: flex;
  justify-content: center;
  margin: 0;
  overflow: hidden;
  border: 1px solid #cfc9d8;
  background: #fff;
  box-shadow: 0 12px 28px rgba(33, 22, 57, .16);
  transform-origin: left center;
}
.page:first-child { border-radius: 8px 2px 2px 8px; }
.page:last-child { border-radius: 2px 8px 8px 2px; }
.pages.is-spread .page:first-child { border-right: 0; box-shadow: -8px 12px 28px rgba(33,22,57,.14), inset -10px 0 16px -15px rgba(20,12,38,.75); }
.pages.is-spread .page:last-child { border-left: 0; box-shadow: 8px 12px 28px rgba(33,22,57,.14), inset 10px 0 16px -15px rgba(20,12,38,.75); }
.page.single { border-radius: 8px; }
.page img { width: 100%; height: 100%; display: block; object-fit: contain; background: #fff; }
.page-error { position: absolute; inset: 0; display: grid; place-content: center; padding: 28px; color: #722f37; background: #fff5f6; text-align: center; }
.page-error strong { display: block; margin-bottom: 8px; }

.pages.turn-forward { animation: turnForward 300ms ease both; }
.pages.turn-back { animation: turnBack 300ms ease both; }
@keyframes turnForward { 0% { opacity: .72; transform: rotateY(-7deg) translateX(12px); } 100% { opacity: 1; transform: none; } }
@keyframes turnBack { 0% { opacity: .72; transform: rotateY(7deg) translateX(-12px); } 100% { opacity: 1; transform: none; } }

.reader-controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 16px;
  margin-top: 20px;
}
.control-group { display: flex; gap: 10px; }
.control-group:last-child { justify-content: flex-end; }
.control-button {
  min-height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  color: #fff;
  border: 0;
  border-radius: 12px;
  background: var(--reader-purple-mid);
  font-weight: 800;
  cursor: pointer;
}
.control-button:hover:not(:disabled) { background: var(--reader-purple); }
.control-button.secondary { color: var(--reader-purple); border: 2px solid #b8accd; background: #fff; }
.control-button.secondary:hover:not(:disabled) { background: #f5f1fb; }
.control-button:disabled { color: #74717b; background: #d9d6de; cursor: not-allowed; }
.page-count { min-width: 150px; margin: 0; color: var(--reader-purple); font-weight: 800; text-align: center; }

.page-text { margin-top: 20px; border: 1px solid var(--reader-line); border-radius: 16px; background: #fff; }
.page-text summary { padding: 15px 18px; color: var(--reader-purple); border-radius: 16px; font-weight: 800; cursor: pointer; }
.page-text[open] summary { border-bottom: 1px solid var(--reader-line); border-radius: 16px 16px 0 0; }
.page-text-content { padding: 8px 20px 20px; }
.page-text-content article + article { margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--reader-line); }
.page-text-content h2 { margin-bottom: 8px; color: var(--reader-purple); font-size: 1.15rem; }
.page-text-content p { margin: 0; }

.completion-panel {
  width: min(900px, 100%);
  min-height: min(54vh, 590px);
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: clamp(20px, 4vw, 42px);
  margin: 0;
  padding: clamp(26px, 5vw, 48px);
  overflow: hidden;
  color: var(--reader-ink);
  border: 1px solid #c9b8e4;
  border-radius: 24px;
  background: linear-gradient(120deg, #f5efff 0%, #ebfbf8 100%);
  box-shadow: 0 14px 34px rgba(53,32,96,.1);
}
.completion-panel::after {
  content: "";
  width: 190px;
  height: 190px;
  position: absolute;
  right: -75px;
  bottom: -110px;
  border-radius: 50%;
  background: rgba(24,169,164,.14);
}
.completion-icon {
  width: clamp(74px, 10vw, 112px);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  position: relative;
  z-index: 1;
  color: #fff;
  border: 7px solid rgba(255,255,255,.8);
  border-radius: 28px;
  background: linear-gradient(145deg, var(--reader-purple-mid), var(--reader-blue));
  box-shadow: 0 12px 24px rgba(53,32,96,.2);
  font-size: clamp(2.1rem, 5vw, 3.4rem);
  transform: rotate(-5deg);
}
.completion-content { position: relative; z-index: 1; }
.completion-content h2 { margin: 0 0 10px; color: var(--reader-purple); font-size: clamp(1.55rem, 3.6vw, 2.35rem); line-height: 1.15; }
.completion-content > p { max-width: 760px; }
.completion-button {
  min-height: 52px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 8px;
  padding: 12px 22px;
  color: #fff;
  border-radius: 14px;
  background: var(--reader-purple-mid);
  box-shadow: 0 8px 18px rgba(97,57,166,.22);
  font-weight: 800;
  text-decoration: none;
}
.completion-button:hover { background: var(--reader-purple); }
.completion-supporting { margin-bottom: 0; color: var(--reader-teal); font-size: .92rem; font-weight: 800; }
.completion-unavailable { color: #6c3d20; font-weight: 800; }
.completion-back {
  display: block;
  margin-top: 20px;
  padding: 8px 0;
  color: var(--reader-purple);
  border: 0;
  background: transparent;
  font-weight: 800;
  text-decoration: underline;
  text-underline-offset: 4px;
  cursor: pointer;
}
.completion-back:hover { color: var(--reader-teal); }
.book-stage.completion-view { background: radial-gradient(circle at 50% 30%, #fff 0, #f4f0fb 55%, #e4dfee 100%); }

.reader-message { max-width: 700px; margin: 70px auto; padding: 32px; border: 2px solid #d3a6ab; border-radius: 18px; background: #fff7f8; text-align: center; }
.reader-message h2 { color: #722f37; }
.reader-message a { color: var(--reader-purple); font-weight: 800; }
[hidden] { display: none !important; }

.reader-app:fullscreen { overflow: auto; background: var(--reader-bg); }
.reader-app:fullscreen .book-stage { min-height: calc(100vh - 245px); }

@media (max-width: 760px) {
  .reader-header { align-items: flex-start; }
  .reader-title p { display: none; }
  .reader-main { padding: 16px 12px 24px; }
  .book-stage { min-height: auto; padding: 8px; border-radius: 16px; }
  .pages { width: 100%; height: min(67vh, 700px); }
  .page { width: 100%; border-radius: 7px !important; }
  .pages.is-spread::after { display: none; }
  .reader-controls { grid-template-columns: 1fr 1fr; }
  .reader-controls .page-count { grid-column: 1 / -1; grid-row: 1; }
  .reader-controls .control-group:first-child { grid-column: 1; grid-row: 2; }
  .reader-controls .control-group:last-child { grid-column: 2; grid-row: 2; }
  .control-button { width: 100%; padding-inline: 11px; font-size: .9rem; }
  .control-label-optional { display: none; }
  .completion-panel { grid-template-columns: 1fr; text-align: center; }
  .completion-icon { margin-inline: auto; }
  .completion-content > p { margin-inline: auto; }
  .completion-back { margin-inline: auto; }
}

@media (max-width: 420px) {
  .reader-header { gap: 10px; }
  .library-link { padding-inline: 5px; font-size: .85rem; }
  .reader-title h1 { max-width: 185px; }
  .reader-status { font-size: .8rem; }
}

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