/* ══════════════════════════════════════════════════════════════
   РАССКАЗКА – Книжная читалка (единственная реализация)
   Подключается вместе с /reader.js. Никаких внешних библиотек.
   Все классы с префиксом rr- чтобы не конфликтовать со страницами.
   ══════════════════════════════════════════════════════════════ */

.rr {
  --rr-fs: 16px;
  --rr-pad-y: 38px;
  --rr-pad-x: 36px;
  --rr-paper: #fffdf7;
  --rr-paper-edge: #f2ece0;
  --rr-ink: var(--dark, #1b1c1a);
  --rr-muted: var(--mid-gray, #88726c);
  --rr-accent: var(--orange-deep, #99462a);
  --rr-gold: var(--gold, #c9a96e);
  --rr-line: var(--border, #e8e2da);

  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  font-family: var(--font-body, Georgia, serif);
}
.rr, .rr *, .rr *::before, .rr *::after { box-sizing: border-box; }

/* ── Сцена: разворот книги ── */
.rr__stage {
  position: relative;
  width: 100%;
}

.rr__book {
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: var(--rr-h, 560px);
  background: var(--rr-paper);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 14px 48px rgba(27, 28, 26, 0.12), 0 2px 6px rgba(27, 28, 26, 0.05);
  position: relative;
}
.rr[data-mode="single"] .rr__book { grid-template-columns: 1fr; }

/* Корешок книги */
.rr[data-mode="spread"] .rr__book::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 18px;
  bottom: 18px;
  width: 22px;
  margin-left: -11px;
  pointer-events: none;
  z-index: 2;
  background:
    linear-gradient(to right, rgba(27,28,26,0) 0%, rgba(27,28,26,0.06) 45%, rgba(27,28,26,0.09) 50%, rgba(27,28,26,0.06) 55%, rgba(27,28,26,0) 100%);
}

/* ── Страница ── */
.rr__page {
  position: relative;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--rr-paper);
  overflow: hidden;
}
.rr[data-mode="spread"] .rr__page--left {
  background: linear-gradient(to left, var(--rr-paper) 82%, var(--rr-paper-edge) 100%);
}
.rr[data-mode="spread"] .rr__page--right {
  background: linear-gradient(to right, var(--rr-paper) 82%, var(--rr-paper-edge) 100%);
}

.rr__body {
  flex: 1;
  min-height: 0;
  padding: var(--rr-pad-y) var(--rr-pad-x);
  font-family: 'Noto Serif', Georgia, serif;
  font-size: var(--rr-fs);
  line-height: 1.75;
  color: var(--rr-ink);
  overflow: hidden;
  hyphens: auto;
  -webkit-hyphens: auto;
}

/* Поток текста страницы. Он же служит измерителем при разбивке на страницы:
   именно поэтому у него ровно та высота, что и у поля страницы, — сравнение
   scrollHeight с clientHeight даёт точный ответ «влезло или нет». */
.rr__flow {
  height: 100%;
  overflow: hidden;
}

.rr__flow p {
  margin: 0 0 0.62em;
  text-indent: 1.4em;
}
.rr__flow p.rr-p--first,
.rr__flow p.rr-p--cont { text-indent: 0; }
.rr__flow p.rr-p--first { display: flow-root; }
.rr__flow p:last-child { margin-bottom: 0; }

/* Номер страницы. Высота жёстко фиксирована: если бы она менялась вместе
   с содержимым, замер страницы и её вёрстка разъезжались бы на строку. */
.rr__folio {
  flex: 0 0 auto;
  height: 30px;
  line-height: 16px;
  padding: 0 var(--rr-pad-x) 14px;
  font-family: var(--font-ui, sans-serif);
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--rr-muted);
  opacity: 0.65;
}
.rr__page--left .rr__folio { text-align: left; }
.rr__page--right .rr__folio { text-align: right; }
.rr[data-mode="single"] .rr__folio { text-align: center; }

/* ── Буквица ── */
.rr-dropcap {
  float: left;
  width: 3.3em;
  height: 3.3em;
  margin: 0.08em 0.42em 0.05em 0;
  shape-outside: margin-box;
}
.rr-dropcap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 3px;
  display: block;
}
.rr-dropcap--css {
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-heading, Georgia, serif);
  font-size: 2.1em;
  font-weight: 900;
  line-height: 1;
  color: var(--rr-accent);
  background: linear-gradient(135deg, rgba(201,169,110,0.16), rgba(217,119,87,0.10));
  border: 1px solid var(--rr-gold);
  border-radius: 3px;
  width: 1.58em;
  height: 1.58em;
  margin: 0.04em 0.2em 0.02em 0;
}

/* ── Титульная страница ── */
.rr-cover {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 14px;
}
.rr-cover__art {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.rr-cover__art img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 4px;
  border: 1px solid var(--rr-gold);
  padding: 5px;
  background: #fff;
}
.rr-cover__title {
  font-family: var(--font-heading, Georgia, serif);
  font-size: 1.45em;
  font-weight: 900;
  line-height: 1.2;
  color: var(--rr-ink);
  margin: 0;
}
.rr-cover__summary {
  font-size: 0.82em;
  font-style: italic;
  line-height: 1.5;
  color: var(--rr-muted);
  max-width: 34ch;
}
.rr-cover__mark {
  font-family: var(--font-ui, sans-serif);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--rr-gold);
}
/* Декоративная рамка, когда обложки нет */
.rr-cover--plain .rr-cover__art {
  flex: 0 0 auto;
}
.rr-cover__ornament {
  width: 74px;
  height: 74px;
  border: 2px solid var(--rr-gold);
  border-radius: 4px;
  position: relative;
  background: linear-gradient(135deg, rgba(201,169,110,0.09), rgba(217,119,87,0.06));
}
.rr-cover__ornament::before,
.rr-cover__ornament::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  border: 1.5px solid var(--rr-gold);
}
.rr-cover__ornament::before { top: -5px; left: -5px; border-right: none; border-bottom: none; }
.rr-cover__ornament::after { bottom: -5px; right: -5px; border-left: none; border-top: none; }

/* ── Страница-иллюстрация ── */
.rr-figure {
  height: 100%;
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.rr-figure__frame {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
  border: 1px solid var(--rr-gold);
  border-radius: 3px;
  background: #fff;
  position: relative;
}
.rr-figure__frame::before,
.rr-figure__frame::after {
  content: '';
  position: absolute;
  width: 14px;
  height: 14px;
  border: 1px solid var(--rr-gold);
  opacity: 0.75;
}
.rr-figure__frame::before { top: 4px; left: 4px; border-right: none; border-bottom: none; }
.rr-figure__frame::after { bottom: 4px; right: 4px; border-left: none; border-top: none; }
.rr-figure__frame img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
}
.rr-figure__ph {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  height: 100%;
  min-height: 120px;
  color: var(--rr-muted);
  font-size: 13px;
  text-align: center;
  background: repeating-linear-gradient(
    45deg,
    rgba(201,169,110,0.05) 0 10px,
    rgba(201,169,110,0.10) 10px 20px
  );
}
.rr-spin {
  width: 22px;
  height: 22px;
  border: 2px solid rgba(201,169,110,0.35);
  border-top-color: var(--rr-accent);
  border-radius: 50%;
  animation: rr-spin 0.9s linear infinite;
}
@keyframes rr-spin { to { transform: rotate(360deg); } }

/* ── Конец сказки ── */
.rr-end {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-top: 1.4em;
  color: var(--rr-muted);
  text-indent: 0;
}
.rr-end__rule { width: 58px; height: 1px; background: var(--rr-line); }
.rr-end__word {
  font-family: 'Noto Serif', Georgia, serif;
  font-size: 0.82em;
  font-style: italic;
  letter-spacing: 0.36em;
  text-indent: 0;
}
/* Своя финальная строка сказки («– Продолжение следует –») вместо «конец» */
.rr__flow p.rr-closing {
  text-align: center;
  text-indent: 0;
  margin-top: 1.4em;
  font-style: italic;
  color: var(--rr-muted);
}

/* ── Зоны клика по краям ── */
.rr__zone {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 11%;
  min-width: 44px;
  border: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
  color: var(--rr-accent);
  font-size: 30px;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.25s ease;
  z-index: 3;
}
.rr__zone--prev { left: 0; }
.rr__zone--next { right: 0; }
.rr__stage:hover .rr__zone:not(:disabled) { opacity: 0.35; }
.rr__zone:hover:not(:disabled) { opacity: 0.9; }
.rr__zone:focus-visible { opacity: 0.9; outline: 2px solid var(--rr-accent); outline-offset: -4px; }
.rr__zone:disabled { cursor: default; }

/* ── Панель управления ── */
.rr__bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}
.rr__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 42px;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--rr-line);
  border-radius: 999px;
  background: #fff;
  color: var(--rr-ink);
  font-family: var(--font-ui, sans-serif);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.rr__btn:hover:not(:disabled) { background: var(--rr-accent); border-color: var(--rr-accent); color: #fff; }
.rr__btn:focus-visible { outline: 2px solid var(--rr-accent); outline-offset: 2px; }
.rr__btn:disabled { opacity: 0.35; cursor: default; }
.rr__progress {
  font-family: var(--font-ui, sans-serif);
  font-size: 13px;
  font-weight: 600;
  color: var(--rr-muted);
  min-width: 132px;
  text-align: center;
}
.rr__tools { display: inline-flex; gap: 6px; margin-left: 8px; }
.rr__tools .rr__btn { font-size: 13px; min-width: 42px; }

/* ── Оверлей (полноэкранный режим) ── */
.rr-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(ellipse at top right, rgba(255, 219, 208, 0.22), var(--surface-low, #f4f4f0) 62%);
}
.rr-overlay__inner {
  position: relative;
  width: min(96vw, 1000px);
}
.rr-overlay__close {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 5;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 50%;
  background: #fff;
  color: var(--dark, #1b1c1a);
  font-size: 17px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(27, 28, 26, 0.1);
}
.rr-overlay__close:hover { transform: scale(1.08); }
.rr-overlay__close:focus-visible { outline: 2px solid var(--orange-deep, #99462a); outline-offset: 2px; }

/* ── Мобильная раскладка ── */
@media (max-width: 899px) {
  .rr { --rr-pad-y: 26px; --rr-pad-x: 22px; }
  .rr__book { border-radius: 10px; }
  .rr__zone { width: 22%; }
}

@media (max-width: 600px) {
  .rr-overlay { padding: 8px 0; align-items: center; }
  .rr-overlay__inner { width: 100vw; }
  .rr-overlay .rr { gap: 8px; }
  .rr-overlay .rr__book { border-radius: 0; box-shadow: none; }
  .rr-overlay .rr__bar { padding-bottom: env(safe-area-inset-bottom, 0px); }
}

@media (prefers-reduced-motion: reduce) {
  .rr__zone, .rr__btn { transition: none; }
  .rr-spin { animation: none; }
}

/* ══════════════════════════════════════════════════════════════
   Печать / «Сохранить PDF» – печатается отдельный поток .rr-print,
   по одному развороту книги на лист, вместе с иллюстрациями.
   ══════════════════════════════════════════════════════════════ */
.rr-print { display: none; }

@media print {
  @page { size: A4 portrait; margin: 16mm 14mm; }

  body > *:not(.rr-print) { display: none !important; }
  body { background: #fff !important; }

  .rr-print {
    display: block !important;
    font-family: 'Noto Serif', Georgia, serif;
    font-size: 12pt;
    line-height: 1.7;
    color: #000;
  }
  .rr-print__sheet {
    break-after: page;
    page-break-after: always;
  }
  /* Обложку и иллюстрации не рвём между листами; длинную сцену рвать можно */
  .rr-print__sheet--art { break-inside: avoid; page-break-inside: avoid; }
  .rr-print__sheet:last-child { break-after: auto; page-break-after: auto; }

  .rr-print__cover { text-align: center; padding-top: 12mm; }
  .rr-print__cover img { max-width: 84%; max-height: 150mm; object-fit: contain; }
  .rr-print__cover h1 { font-size: 22pt; font-weight: 900; margin: 8mm 0 3mm; }
  .rr-print__cover .rr-print__sub { font-size: 11pt; font-style: italic; color: #555; }

  .rr-print__figure { text-align: center; }
  .rr-print__figure img { max-width: 100%; max-height: 210mm; object-fit: contain; }

  .rr-print__scene p { margin: 0 0 0.6em; text-indent: 1.4em; orphans: 3; widows: 3; }
  .rr-print__scene p:first-child { text-indent: 0; }
  .rr-print__scene p:first-child::first-letter {
    float: left;
    font-size: 3.1em;
    line-height: 0.82;
    margin: 0.02em 0.1em 0 0;
    font-weight: 900;
    color: #99462a;
  }
  .rr-print__end { text-align: center; margin-top: 10mm; font-style: italic; letter-spacing: 0.3em; color: #555; }
  .rr-print img { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
