/* =========================================================
   book.css — «книжный» интерфейс: пергамент, серифы, страницы
   ========================================================= */

:root {
  /* Светлая тема: пергамент / чернила */
  --paper: #f5ecd7;
  --paper-deep: #ece0c4;
  --ink: #3a2f22;
  --ink-soft: #6b5d48;
  --accent: #7a3b2e;          /* «сургучный» акцент для кнопок-выборов */
  --accent-ink: #f7efdd;
  --line: #c9b989;
  --shadow: rgba(58, 47, 34, 0.18);
  --node-fill: #7a3b2e;
  --node-dim: #b3a689;
}

@media (prefers-color-scheme: dark) {
  :root {
    /* Тёплая тёмная тема: старая кожа / свеча */
    --paper: #241d13;
    --paper-deep: #1c160e;
    --ink: #e8dcc0;
    --ink-soft: #a99878;
    --accent: #c96f4a;
    --accent-ink: #241d13;
    --line: #4a3d2a;
    --shadow: rgba(0, 0, 0, 0.5);
    --node-fill: #c96f4a;
    --node-dim: #55482f;
  }
}

* { box-sizing: border-box; }

/* Атрибут hidden должен побеждать любые display: flex у экранов и модалки */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--paper);
  color: var(--ink);
  /* Системный серифный стек.
     TODO-шрифт: сюда позже подключить локальный веб-шрифт через @font-face
     (файл .woff2 положить рядом и добавить в precache списка sw.js). */
  font-family: "Iowan Old Style", "Palatino", Georgia, "Times New Roman", serif;
  font-size: 19px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  /* Лёгкая «пергаментная» виньетка по краям */
  background-image: radial-gradient(ellipse at center,
                    var(--paper) 55%, var(--paper-deep) 100%);
  background-attachment: fixed;
  touch-action: manipulation;
}

/* ---------- Экраны ---------- */

.screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.screen-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
}

/* ---------- Главное меню ---------- */

#screen-menu {
  align-items: center;
  justify-content: center;
}

.menu-book {
  text-align: center;
  padding: 32px 24px;
  max-width: 480px;
  width: 100%;
}

.ornament {
  font-size: 2rem;
  color: var(--accent);
  margin-bottom: 8px;
}

.book-title {
  font-size: 2.2rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  margin: 0 0 4px;
}

.book-subtitle {
  color: var(--ink-soft);
  font-style: italic;
  margin: 0 0 40px;
}

.menu-nav {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- Кнопки ---------- */

.btn {
  font-family: inherit;
  font-size: 1.05rem;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 14px 22px;          /* крупные тап-зоны под палец */
  min-height: 52px;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn:active {
  background: var(--paper-deep);
  transform: scale(0.985);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.btn-danger { color: var(--accent); }

.btn-back {
  border: none;
  min-height: 44px;
  padding: 8px 12px;
  font-size: 1.1rem;
  color: var(--ink-soft);
}

.btn-round {
  min-width: 52px;
  padding: 8px 0;
  font-size: 1.4rem;
  line-height: 1;
}

/* ---------- Страница книги (плеер сцен) ---------- */

.page-wrap {
  flex: 1;
  width: 100%;
  max-width: 640px;            /* колонка чтения по центру */
  margin: 0 auto;
  padding: 8px 24px 48px;
  perspective: 1200px;         /* для эффекта перелистывания */
}

.page {
  transition: opacity 0.28s ease, transform 0.28s ease;
  transform-origin: left center;
  will-change: opacity, transform;
}

/* Кадры «перелистывания»: только transform/opacity, без layout-дёрганий */
.page.turn-out {
  opacity: 0;
  transform: translateX(-28px) rotateY(7deg);
}

.page.turn-in {
  transition: none;
  opacity: 0;
  transform: translateX(28px) rotateY(-7deg);
}

.page-content p {
  margin: 0 0 1.1em;
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
}

.page-content p:first-of-type::first-letter {
  /* Буквица — маленький книжный штрих */
  font-size: 2.6em;
  line-height: 0.85;
  float: left;
  padding: 0.06em 0.12em 0 0;
  color: var(--accent);
}

/* Заголовок концовки */
.ending-title {
  text-align: center;
  font-size: 1.5rem;
  margin: 0.5em 0 1em;
}

.ending-mark {
  display: block;
  text-align: center;
  color: var(--accent);
  font-size: 1.6rem;
  margin-bottom: 0.4em;
}

/* ---------- Выборы: «чернильные» ссылки-кнопки ---------- */

.choices {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 2em;
}

.choice {
  font-family: inherit;
  font-size: 1.05rem;
  font-style: italic;
  text-align: left;
  color: var(--accent);
  background: transparent;
  border: none;
  border-left: 3px solid var(--accent);
  border-radius: 0 6px 6px 0;
  padding: 14px 16px;
  min-height: 52px;
  cursor: pointer;
  transition: background-color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.choice:active { background: var(--paper-deep); }

.next-row {
  display: flex;
  justify-content: center;
  gap: 14px;
  margin-top: 2.2em;
}

/* ---------- Экран «Все концовки» ---------- */

.endings-count {
  text-align: center;
  font-size: 1.4rem;
  margin: 0.4em 0 1.4em;
}

.endings-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ending-slot {
  display: flex;
  align-items: baseline;
  gap: 14px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.ending-slot .diamond {
  font-size: 1.2rem;
  color: var(--accent);
}

.ending-slot.locked {
  color: var(--ink-soft);
  opacity: 0.65;
}

.ending-slot.locked .diamond { color: var(--ink-soft); }

/* ---------- Экран «Дерево» ---------- */

.graph-controls {
  display: flex;
  gap: 10px;
}

/* Фиксированная высота экрана: иначе svg с height:100% раздувает
   flex-контейнер с min-height и дерево уезжает под нижний край. */
#screen-graph {
  height: 100dvh;
  overflow: hidden;
}

.graph-wrap {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  border-top: 1px solid var(--line);
}

#graph-svg {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;          /* пан/зум обрабатываем сами */
  cursor: grab;
}

#graph-svg text {
  font-family: inherit;
  fill: var(--ink);
  font-size: 13px;
}

#graph-svg .edge {
  stroke: var(--ink-soft);
  stroke-width: 1.6;
  fill: none;
}

#graph-svg .edge.dim {
  stroke: var(--node-dim);
  stroke-dasharray: 4 4;
  opacity: 0.6;
}

#graph-svg .node-shape.visited {
  fill: var(--node-fill);
  stroke: var(--node-fill);
}

#graph-svg .node-shape.dim {
  fill: var(--paper);
  stroke: var(--node-dim);
  stroke-width: 1.5;
  opacity: 0.8;
}

#graph-svg .node-label.dim {
  fill: var(--ink-soft);
  opacity: 0.7;
  font-style: italic;
}

/* ---------- Модалка экспорта/импорта ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 10;
}

.modal-box {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 40px var(--shadow);
  width: 100%;
  max-width: 520px;
  padding: 24px;
}

.modal-box h3 { margin: 0 0 8px; }

.modal-box p { margin: 0 0 12px; color: var(--ink-soft); }

#modal-code {
  width: 100%;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 0.85rem;
  color: var(--ink);
  background: var(--paper-deep);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  resize: vertical;
  word-break: break-all;
}

.modal-error {
  color: var(--accent);
  font-weight: 600;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 16px;
}

/* ---------- Статус офлайн-режима в меню ---------- */

.offline-status {
  display: block;
  width: 100%;
  margin-top: 30px;
  padding: 10px 8px;
  font-family: inherit;
  font-size: 0.82rem;
  font-style: italic;
  color: var(--ink-soft);
  background: none;
  border: none;
  opacity: 0.7;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* Проблемы с офлайном подсвечиваем акцентом — их видно сразу */
.offline-status[data-state="insecure"],
.offline-status[data-state="unsupported"],
.offline-status[data-state="error"],
.offline-status[data-state="updated"] {
  color: var(--accent);
  opacity: 1;
}

/* ---------- Петля времени: дополнения ---------- */

/* Ремарка Ани на странице концовки / подсказка на экране глав */
.loop-note {
  text-align: center;
  font-style: italic;
  color: var(--ink-soft);
  margin-top: 1.6em;
}

/* Экран «Начать с главы» */
.chapters-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 1.2em;
}

.chapter-slot.locked {
  color: var(--ink-soft);
  opacity: 0.55;
  font-style: italic;
}

/* Дерево: гейт-рёбра (условные переходы) — пунктир */
#graph-svg .edge.gated {
  stroke-dasharray: 6 5;
}

/* Дерево: настоящая концовка (breaksLoop) — акцентный ромб */
#graph-svg .node-shape.true-ending.visited {
  fill: var(--accent);
  stroke: var(--ink);
  stroke-width: 2.5;
}

#graph-svg .node-shape.true-ending.dim {
  stroke: var(--accent);
  stroke-dasharray: 3 3;
  opacity: 0.7;
}

/* Дерево: полосы глав */
#graph-svg .chapter-line {
  stroke: var(--line);
  stroke-width: 1;
  stroke-dasharray: 2 7;
}

#graph-svg .chapter-caption {
  fill: var(--ink-soft);
  font-style: italic;
  font-size: 15px;
}
