/* ===== BookHaven 3D — глобальные стили и темы ===== */

:root {
  --font-serif: Georgia, 'Times New Roman', serif;
  --font-sans: 'Inter', -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;

  --toolbar-h: 56px;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
}

/* --- Тема: Бумага --- */
:root, [data-theme="paper"] {
  --bg: #e8e2d4;
  --bg-texture: radial-gradient(ellipse at 50% 40%, #efe9db 0%, #e0d8c6 70%, #d5ccb8 100%);
  --page-bg: #fbf8f0;
  --page-edge: #efe9db;
  --text: #2e2a24;
  --text-dim: #8a8172;
  --accent: #a06b32;
  --panel-bg: rgba(251, 248, 240, 0.92);
  --shadow-book: rgba(60, 48, 30, 0.35);
  --shadow-page: rgba(60, 48, 30, 0.18);
  /* Твёрдая обложка (кожа) */
  --cover-a: #6b4226;
  --cover-b: #452818;
  --cover-edge: #2e1a0e;
  --cover-line: rgba(216, 178, 106, 0.6);
}

/* --- Тема: Сепия --- */
[data-theme="sepia"] {
  --bg: #d9c9a3;
  --bg-texture: radial-gradient(ellipse at 50% 40%, #e6d7b4 0%, #d3c096 70%, #c2ad7f 100%);
  --page-bg: #f6ead0;
  --page-edge: #ecdfc2;
  --text: #4a3520;
  --text-dim: #9a7f5c;
  --accent: #8a5a24;
  --panel-bg: rgba(246, 234, 208, 0.92);
  --shadow-book: rgba(70, 50, 20, 0.4);
  --shadow-page: rgba(70, 50, 20, 0.2);
  /* Твёрдая обложка (кожа) */
  --cover-a: #7a4a20;
  --cover-b: #523012;
  --cover-edge: #38200a;
  --cover-line: rgba(228, 190, 120, 0.65);
}

/* --- Тема: Ночной --- */
[data-theme="night"] {
  --bg: #101318;
  --bg-texture: radial-gradient(ellipse at 50% 40%, #181c23 0%, #0e1116 70%, #080a0d 100%);
  --page-bg: #1e232c;
  --page-edge: #262c37;
  --text: #d6dae2;
  --text-dim: #6b7280;
  --accent: #7aa2d8;
  --panel-bg: rgba(24, 28, 35, 0.94);
  --shadow-book: rgba(0, 0, 0, 0.6);
  --shadow-page: rgba(0, 0, 0, 0.4);
  /* Твёрдая обложка (кожа) */
  --cover-a: #2a3140;
  --cover-b: #161b24;
  --cover-edge: #0c1016;
  --cover-line: rgba(122, 162, 216, 0.45);
}

/* --- Тема: Лес --- */
[data-theme="forest"] {
  --bg: #ccd6bd;
  --bg-texture: radial-gradient(ellipse at 50% 40%, #dde6cf 0%, #c4d0b0 70%, #aebb97 100%);
  --page-bg: #eef3e2;
  --page-edge: #e2ead2;
  --text: #26331f;
  --text-dim: #6f7d5e;
  --accent: #4d7a3a;
  --panel-bg: rgba(238, 243, 226, 0.92);
  --shadow-book: rgba(40, 60, 30, 0.35);
  --shadow-page: rgba(40, 60, 30, 0.18);
  /* Твёрдая обложка (кожа) */
  --cover-a: #3d5c30;
  --cover-b: #24381c;
  --cover-edge: #16240f;
  --cover-line: rgba(200, 220, 150, 0.5);
}

/* --- Тема: Небо --- */
[data-theme="sky"] {
  --bg: #c3d8e8;
  --bg-texture: radial-gradient(ellipse at 50% 40%, #d6e6f2 0%, #b8cfe2 70%, #a2bcd4 100%);
  --page-bg: #f2f8fc;
  --page-edge: #e4eef6;
  --text: #1f2c3a;
  --text-dim: #64798c;
  --accent: #3a6ea8;
  --panel-bg: rgba(242, 248, 252, 0.92);
  --shadow-book: rgba(30, 50, 70, 0.35);
  --shadow-page: rgba(30, 50, 70, 0.18);
  /* Твёрдая обложка (кожа) */
  --cover-a: #2f5878;
  --cover-b: #1c3a52;
  --cover-edge: #122636;
  --cover-line: rgba(190, 220, 240, 0.5);
}

/* --- Тема: Угли (OLED) --- */
/* Для светодиодных экранов (OLED/QLED): тёплый тёмно-коричневый фон —
   не чисто чёрный, чтобы не резать глаза контрастом, но достаточно
   тёмный, чтобы светодиоды почти не светили и экран не выгорал.
   Тёплые тона снижают нагрузку на глаза вечером. */
[data-theme="ember"] {
  --bg: #1c1410;
  --bg-texture: radial-gradient(ellipse at 50% 40%, #241a14 0%, #1a120d 70%, #120c08 100%);
  --page-bg: #241a14;
  --page-edge: #2e211a;
  --text: #e8d5b5;
  --text-dim: #9a8266;
  --accent: #d4954a;
  --panel-bg: rgba(30, 22, 16, 0.94);
  --shadow-book: rgba(0, 0, 0, 0.65);
  --shadow-page: rgba(0, 0, 0, 0.45);
  /* Твёрдая обложка (кожа) */
  --cover-a: #3a2417;
  --cover-b: #241610;
  --cover-edge: #160d07;
  --cover-line: rgba(212, 149, 74, 0.5);
}

/* --- Тема: Янтарь (OLED, максимальная защита) --- */
/* Чистый чёрный фон (светодиоды полностью выключены — нулевой износ)
   с янтарно-жёлтым текстом: тёплый спектр меньше утомляет глаза
   и не мешает выработке мелатонина вечером. */
[data-theme="amber"] {
  --bg: #000000;
  --bg-texture: radial-gradient(ellipse at 50% 40%, #0d0a06 0%, #060504 70%, #000000 100%);
  --page-bg: #18110e;
  --page-edge: #2e211a;
  --text: #e8d5b5;
  --text-dim: #9a8266;
  --accent: #ffb020;
  --panel-bg: rgba(10, 8, 6, 0.94);
  --shadow-book: rgba(0, 0, 0, 0.8);
  --shadow-page: rgba(0, 0, 0, 0.55);
  /* Твёрдая обложка (кожа) */
  --cover-a: #241a08;
  --cover-b: #140f05;
  --cover-edge: #0a0703;
  --cover-line: rgba(255, 176, 32, 0.45);
}

/* ===== ОТЛАДКА: границы текстовой области ===== */
/* Показываются при ?debug=1 или кнопкой. Рамка = РЕАЛЬНЫЕ границы,
   в которых пагинатор размещает текст (padTop/padBottom/margins из JS).
   Текст НЕ должен выходить за рамку; пустота ВНУТРИ рамки внизу —
   недобор плотности (какое-то правило переноса помешало). */
body.debug-planes .page-content::after {
  content: '';
  position: absolute;
  top: var(--dbg-top, 48px);
  bottom: var(--dbg-bottom, 48px);
  left: var(--dbg-left, 60px);
  right: var(--dbg-right, 60px);
  border: 1px dashed rgba(255, 0, 100, 0.55);
  background: rgba(255, 0, 100, 0.04);
  pointer-events: none;
  z-index: 5;
}

/* Индикатор заполненности: полоска высотой с ИСПОЛЬЗОВАННУЮ часть рамки */
body.debug-planes .page-content::before {
  content: '';
  position: absolute;
  top: var(--dbg-top, 48px);
  left: var(--dbg-left, 60px);
  width: 3px;
  height: var(--dbg-used, 0px);
  background: rgba(0, 160, 255, 0.6);
  pointer-events: none;
  z-index: 5;
}

/* ===== База ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  background-image: var(--bg-texture);
  color: var(--text);
  transition: background-color 0.4s, color 0.4s;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

/* ===== Нижняя панель ===== */
.toolbar {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%) translateY(20px);
  display: flex;
  align-items: center;
  gap: 18px;
  height: var(--toolbar-h);
  padding: 0 22px;
  background: var(--panel-bg);
  border-radius: 28px;
  box-shadow: 0 8px 30px var(--shadow-page);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px); /* Safari ≤ 17 */
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s var(--ease-out), transform 0.3s var(--ease-out);
  z-index: 100;
}

.toolbar.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

.tb-btn {
  font-size: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  transition: background 0.2s, transform 0.2s;
}

.tb-btn:hover {
  background: var(--shadow-page);
  transform: scale(1.08);
}

.tb-progress {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  min-width: 180px;
}

.progress-line {
  width: 100%;
  height: 3px;
  background: var(--shadow-page);
  border-radius: 2px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.4s var(--ease-out);
}

.progress-text {
  font-size: 12px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

/* ===== Индикатор загрузки ===== */
.loading-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--bg);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s var(--ease-out);
}

.loading-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 3px solid var(--shadow-page);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.loading-text {
  font-size: 14px;
  color: var(--text-dim);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Боковые зоны навигации ===== */
.nav-zone {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 12%;
  z-index: 20;
  cursor: pointer;
}

.nav-left  { left: 0; }
.nav-right { right: 0; }

/* На сенсорных экранах зоны тапа шире — проще попадать пальцем */
@media (hover: none), (pointer: coarse) {
  .nav-zone { width: 18%; }
}

/* Когда открыта библиотека, зоны листания не должны перехватывать клики:
   .nav-zone (z-index 20) лежит поверх .library (z-index 2), и .nav-left
   накрывала левую половину первой книги у края экрана. */
.library.open ~ .nav-zone {
  pointer-events: none;
}

/* ===== Панель настроек ===== */
.settings-panel {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%) translateY(105%);
  width: min(480px, 92vw);
  background: var(--panel-bg);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px); /* Safari ≤ 17 */
  border-radius: 20px 20px 0 0;
  padding: 42px 28px 32px;
  box-shadow: 0 -8px 40px var(--shadow-book);
  transition: transform 0.35s var(--ease-out);
  z-index: 200;
  overflow: visible;
}

.settings-panel.open {
  transform: translateX(-50%) translateY(0);
}

.settings-panel-header {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 8px;
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
}

.settings-close {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 22px;
  color: var(--text-dim);
  transition: background 0.2s, transform 0.2s;
}

.settings-close:hover {
  background: var(--shadow-page);
  transform: scale(1.06);
}

.settings-section { margin-bottom: 20px; }
.settings-section:last-child { margin-bottom: 0; }

.settings-section h3 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 12px;
}

.settings-section label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  margin-bottom: 10px;
}

.settings-section input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

/* ===== macOS-style переключатель ===== */
.toggle-switch {
  position: relative;
  display: inline-flex;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
  cursor: pointer;
  user-select: none;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--shadow-page);
  border-radius: 24px;
  transition: background 0.25s var(--ease-out);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--text-dim);
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.25s var(--ease-out);
}

.toggle-switch input:checked + .toggle-track {
  background: var(--accent);
}

.toggle-switch input:checked + .toggle-track + .toggle-thumb,
.toggle-switch input:checked + .toggle-track .toggle-thumb {
  transform: translateX(20px);
}

.theme-picker {
  display: flex;
  gap: 14px;
}

.theme-dot {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--dot);
  border: 2px solid transparent;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.12), 0 2px 6px var(--shadow-page);
  transition: transform 0.2s, border-color 0.2s;
}

.theme-dot:hover { transform: scale(1.12); }

.theme-dot.active {
  border-color: var(--accent);
  transform: scale(1.12);
}
