/*
 * Онлайн-читалка.
 * Страница книги повторяет макет PDF (A4, 595 × 842 pt): все размеры
 * заданы в «пунктах PDF» через --pt и масштабируются по ширине листа.
 * На узких экранах лист перестраивается в колонку с крупным текстом.
 */

@font-face {
  font-family: "Skazki DejaVu";
  src: url("assets/fonts/DejaVuSans.woff") format("woff");
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: "Skazki DejaVu";
  src: url("assets/fonts/DejaVuSans-Bold.woff") format("woff");
  font-weight: 700;
  font-display: swap;
}

.reader-body {
  --desk: #f3ede2;
  --sheet-w: min(100vw - 32px, 880px, max(560px, (100svh - 240px) * .7071));
  --book-font: "Skazki DejaVu", "DejaVu Sans", Verdana, sans-serif;
  background: var(--desk);
}

.reader-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding-block: 18px;
}

.reader-download { min-height: 46px; padding: 10px 18px; font-size: .95rem; }

.reader-hint {
  width: min(100% - 32px, 880px);
  margin: 0 auto 16px;
  color: var(--muted);
  font-size: .95rem;
  text-align: center;
}

.reader-message { padding: 48px 0; color: var(--muted); text-align: center; }

/* ---------- Лист книги ---------- */

.sheet-wrap {
  container-type: inline-size;
  width: min(100% - 32px, var(--sheet-w));
  margin-inline: auto;
}

.sheet {
  --pt: calc(100cqw / 595.276);
  --ink: #253c38;
  --green: #23634e;
  --muted: #586a64;
  display: flex;
  flex-direction: column;
  aspect-ratio: 595.276 / 841.89;
  padding: 0 calc(42 * var(--pt)) calc(24.6 * var(--pt));
  border-radius: 4px;
  background: #fffcf6;
  color: var(--ink);
  font-family: var(--book-font);
  line-height: 1.2;
  box-shadow: 0 18px 50px rgb(46 64 46 / 13%), 0 2px 6px rgb(46 64 46 / 7%);
}

.sheet[hidden] { display: none; }

/* Вертикальная страница, как в PDF: колонки разворота не нужны,
   элементы выстраиваются в исходном порядке PDF */
.sheet__story, .sheet__art { display: contents; }
.sheet__top { order: 0; }
.sheet__title { order: 1; }
.sheet__subtitle { order: 2; }
.sheet__figure { order: 3; }
.sheet__text { order: 4; }
.sheet__note { order: 4; }
.glossary { order: 5; }
.sheet__foot { order: 6; }
.sheet p, .sheet h2, .sheet h3 { text-wrap: wrap; }

.sheet__top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: calc(27.2 * var(--pt));
  color: var(--green);
  font-size: calc(9 * var(--pt));
}

.sheet__brand { font-weight: 700; }

.sheet__title {
  margin-top: calc(14.3 * var(--pt));
  font-size: calc(31 * var(--pt));
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0;
}

.sheet--cover .sheet__title { margin-top: calc(11.2 * var(--pt)); }

.sheet__subtitle {
  margin-top: calc(.9 * var(--pt));
  color: var(--muted);
  font-size: calc(10 * var(--pt));
}

.sheet__figure {
  margin: calc(20.5 * var(--pt)) 0 0;
  aspect-ratio: 511.276 / 310;
  border-radius: calc(18 * var(--pt));
  overflow: hidden;
  background: #edf3e8;
}

.sheet--cover .sheet__figure { margin-top: calc(10.7 * var(--pt)); }
.sheet__figure img { display: block; width: 100%; height: 100%; object-fit: cover; }

.sheet__text {
  margin-top: calc(20.5 * var(--pt));
  font-size: calc(17 * var(--pt));
  line-height: calc(24 * var(--pt));
  white-space: pre-line; /* переносы строк в book.js разделяют реплики */
}

.sheet__note {
  margin-top: calc(10 * var(--pt));
  padding-top: calc(8 * var(--pt));
  border-top: 1px dashed rgb(35 99 78 / 30%);
  color: var(--muted);
  font-size: calc(10 * var(--pt));
  line-height: 1.4;
}

/* Английское слово — кнопка с переводом */
.en {
  display: inline;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 3px;
  background: none;
  color: var(--green);
  font: inherit;
  font-weight: 700;
  line-height: inherit;
  vertical-align: baseline;
  cursor: pointer;
  text-decoration: underline dotted rgb(35 99 78 / 45%);
  text-decoration-thickness: max(1px, calc(1.2 * var(--pt)));
  text-underline-offset: calc(3.5 * var(--pt));
  transition: background-color .15s, box-shadow .15s;
}

.en[aria-expanded="true"] { background: #fff0b9; box-shadow: 0 0 0 calc(2.5 * var(--pt)) #fff0b9; text-decoration-color: transparent; }
.en:focus-visible { outline: 3px solid #8255b5; outline-offset: 2px; }

@media (hover: hover) {
  .en:hover { text-decoration-color: var(--green); }
}

/* Мини-словарик: крупнее, чем в PDF, чтобы его было удобно читать с экрана */
.glossary {
  margin-top: auto;
  min-height: calc(126 * var(--pt));
  padding: calc(13 * var(--pt)) calc(16 * var(--pt)) calc(13 * var(--pt));
  border-radius: calc(16 * var(--pt));
  background: #f8ead7;
}

.sheet--green .glossary { background: #edf3e8; }

.glossary__head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 2px 12px; }
.glossary__title { white-space: nowrap; }
.glossary__title { font-size: calc(14 * var(--pt)); font-weight: 700; line-height: 1.2; }
.glossary__note { color: var(--muted); font-size: calc(9.5 * var(--pt)); }

.glossary__list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: calc(5 * var(--pt)) calc(14 * var(--pt));
  margin: calc(9 * var(--pt)) 0 0;
  padding: 0;
  list-style: none;
  font-size: calc(12 * var(--pt));
  line-height: 1.3;
}

.glossary__item { display: flex; align-items: center; gap: calc(7 * var(--pt)); min-width: 0; }
.glossary__text { min-width: 0; }
.glossary__list b { color: var(--green); }

/* Кнопка «послушать» */
.play {
  flex: none;
  display: grid;
  place-items: center;
  width: calc(22 * var(--pt));
  height: calc(22 * var(--pt));
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 calc(2 * var(--pt)) calc(5 * var(--pt)) rgb(35 99 78 / 22%);
  transition: background-color .15s, transform .15s;
}

.play svg { width: 58%; height: 58%; margin-left: 8%; }
.play.is-playing { background: #a75427; animation: play-pulse .9s ease-in-out infinite; }
.play:focus-visible { outline: 3px solid #8255b5; outline-offset: 2px; }

@keyframes play-pulse {
  50% { transform: scale(1.12); }
}

@media (hover: hover) {
  .play:hover { background: var(--green-hover); }
}

.sheet__foot {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-top: calc(14.4 * var(--pt));
  color: var(--muted);
  font-size: calc(8 * var(--pt));
}

.sheet__num { color: var(--green); font-size: calc(10 * var(--pt)); font-weight: 700; }

/* Узкий экран: тот же стиль, но текст крупный и лист растёт по содержимому */
@container (max-width: 559px) {
  .sheet {
    --pt: 1px;
    aspect-ratio: auto;
    padding: 0 18px 16px;
    border-radius: 18px;
  }
  .sheet__top { flex-wrap: wrap; margin-top: 16px; font-size: 11px; }
  .sheet__title, .sheet--cover .sheet__title { margin-top: 12px; font-size: 28px; }
  .sheet__subtitle { margin-top: 4px; font-size: 13px; }
  .sheet__figure, .sheet--cover .sheet__figure { margin-top: 14px; border-radius: 14px; }
  .sheet__text { margin-top: 16px; font-size: 18px; line-height: 29px; }
  .sheet__note { margin-top: 14px; padding-top: 12px; font-size: 14px; }
  .glossary { margin-top: 22px; min-height: 0; padding: 16px 16px 14px; border-radius: 14px; }
  .glossary__title { font-size: 18px; }
  .glossary__note { font-size: 12px; }
  .glossary__list { grid-template-columns: 1fr; gap: 10px; margin-top: 12px; font-size: 17px; line-height: 1.35; }
  .glossary__item { gap: 12px; }
  .play { width: 40px; height: 40px; }
  .sheet__foot { flex-wrap: wrap; margin-top: 14px; font-size: 11px; }
  .sheet__num { font-size: 13px; }
}

/* ---------- Разворот для широких экранов ----------
 * Вертикальная страница A4 с крупным текстом не помещается в горизонтальный
 * экран, поэтому на компьютере страница раскладывается как разворот книги
 * 1200 × 680 условных единиц (--u) и целиком вписывается в окно:
 * слева иллюстрация и мини-словарик, справа заголовок и текст.
 */
@media (min-width: 1024px) and (min-aspect-ratio: 5/4) {
  .reader-body { --u: min((100vw - 216px) / 1200, (100svh - 112px) / 680, 1.3px); }

  .reader-bar { position: relative; }
  .reader-hint {
    position: absolute;
    top: 30px;
    left: 50%;
    width: auto;
    margin: 0;
    transform: translateX(-50%);
    white-space: nowrap;
  }

  .sheet-wrap { width: calc(1200 * var(--u)); }

  .sheet {
    --pt: calc(1.45 * var(--u));
    display: grid;
    grid-template-columns: calc(530 * var(--u)) 1fr;
    grid-template-rows: auto 1fr auto;
    column-gap: calc(44 * var(--u));
    aspect-ratio: auto;
    min-height: calc(680 * var(--u));
    padding: 0 calc(40 * var(--u)) calc(20 * var(--u));
    border-radius: calc(8 * var(--u));
  }

  .sheet__top { grid-column: 1 / -1; margin-top: calc(22 * var(--u)); }
  .sheet__foot { grid-column: 1 / -1; margin-top: calc(14 * var(--u)); }

  .sheet__art, .sheet__story { display: flex; flex-direction: column; min-width: 0; margin-top: calc(18 * var(--u)); }
  .sheet__art { gap: calc(16 * var(--u)); }
  .sheet__art { grid-column: 1; grid-row: 2; }
  .sheet__story { grid-column: 2; grid-row: 2; }

  .sheet__title, .sheet--cover .sheet__title { margin-top: calc(-6 * var(--u)); }
  .sheet__figure, .sheet--cover .sheet__figure { margin-top: 0; }
  .sheet__text { margin-top: calc(16 * var(--u)); font-size: calc(26 * var(--u)); line-height: calc(37.5 * var(--u)); }
  .sheet__note { margin-top: calc(16 * var(--u)); padding-top: calc(12 * var(--u)); font-size: calc(15 * var(--u)); }

  .glossary {
    margin-top: auto;
    min-height: 0;
    padding: calc(13 * var(--u)) calc(16 * var(--u)) calc(14 * var(--u));
  }
  .glossary__title { font-size: calc(18 * var(--u)); }
  .glossary__note { font-size: calc(12 * var(--u)); }
  .glossary__list { gap: calc(6 * var(--u)) calc(12 * var(--u)); margin-top: calc(9 * var(--u)); font-size: calc(15.5 * var(--u)); }
  .glossary__item { gap: calc(8 * var(--u)); }
  .play { width: calc(28 * var(--u)); height: calc(28 * var(--u)); }
}

/* ---------- Навигация ---------- */

.reader-nav {
  position: sticky;
  bottom: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 12px;
  padding: 18px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(to top, var(--desk) 72%, rgb(243 237 226 / 0));
}

.reader-nav__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 128px;
  min-height: 54px;
  padding: 10px 20px;
  border: 0;
  border-radius: 16px;
  background: var(--green);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 8px 20px rgb(35 99 78 / 16%);
  transition: background .2s, transform .2s;
}

.reader-nav__btn:disabled { background: #dcdcd2; color: #6c7771; box-shadow: none; cursor: default; }
.reader-nav__count { min-width: 76px; font-weight: 800; text-align: center; }

@media (hover: hover) {
  .reader-nav__btn:not(:disabled):hover { background: var(--green-hover); transform: translateY(-2px); }
}

/* ---------- Подсказка с переводом ---------- */

.word-pop {
  position: absolute;
  z-index: 20;
  top: 0;
  left: 0;
  max-width: min(300px, calc(100vw - 24px));
  padding: 12px 18px 13px;
  border: 1px solid #e8dfcc;
  border-radius: 14px;
  background: #fff;
  color: #253c38;
  font-family: var(--book-font);
  line-height: 1.3;
  box-shadow: 0 14px 34px rgb(46 64 46 / 18%);
  animation: pop-in .16s ease;
}

.word-pop[hidden] { display: none; }

.word-pop::after {
  content: "";
  position: absolute;
  left: var(--arrow-x, 50%);
  width: 12px;
  height: 12px;
  margin-left: -6px;
  background: #fff;
  border: solid #e8dfcc;
  transform: rotate(45deg);
}

.word-pop[data-side="top"]::after { bottom: -7px; border-width: 0 1px 1px 0; }
.word-pop[data-side="bottom"]::after { top: -7px; border-width: 1px 0 0 1px; }

.word-pop__word { color: #23634e; font-size: 1.05rem; font-weight: 700; }
.word-pop__ipa { color: #586a64; font-size: .9rem; }
.word-pop__ru { margin-top: 3px; font-size: 1.25rem; font-weight: 700; }

@keyframes pop-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

/* Широкий экран: кнопки листания по бокам листа, чтобы не закрывать страницу */
@media (min-width: 1120px), (min-width: 1024px) and (min-aspect-ratio: 5/4) {
  .reader { padding-bottom: 40px; }
  .reader-nav {
    position: fixed;
    top: 50%;
    left: 50%;
    width: calc(var(--sheet-w) + 2 * (64px + 32px));
    justify-content: space-between;
    margin: 0;
    padding: 0;
    background: none;
    transform: translate(-50%, -50%);
    pointer-events: none;
  }
  .reader-nav__btn { min-width: 0; width: 64px; height: 64px; padding: 0; border-radius: 50%; pointer-events: auto; }
  .reader-nav__btn svg { width: 26px; height: 26px; }
  .reader-nav__btn span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .reader-nav__count { display: none; }
}

/* В развороте кнопки листания стоят вплотную к его краям */
@media (min-width: 1024px) and (min-aspect-ratio: 5/4) {
  .reader { padding-bottom: 16px; }
  .reader-nav { width: calc(1200 * var(--u) + 2 * (64px + 28px)); }
}

@media (max-width: 700px) {
  .reader-bar { padding-block: 14px; }
  .reader-download { min-height: 44px; padding: 8px 14px; font-size: .9rem; }
  .reader-nav__btn { min-width: 0; flex: 1; max-width: 160px; }
}

@media (max-width: 380px) {
  .reader-bar .logo { font-size: 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  .word-pop { animation: none; }
  .en, .reader-nav__btn, .play { transition: none; }
  .play.is-playing { animation: none; }
}
