/* Читалка. Цвета — переменные: тема «Как в Telegram» берёт их у клиента
   (--tg-theme-*), остальные три задают свои. */

:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --fg: var(--tg-theme-text-color, #1f1f24);
  --hint: var(--tg-theme-hint-color, #85858f);
  --surface: var(--tg-theme-secondary-bg-color, #f1f1f4);
  --accent: var(--tg-theme-button-color, #2f6fd6);
  --accent-fg: var(--tg-theme-button-text-color, #ffffff);
  --line: rgba(128, 128, 136, .26);
  --shade: rgba(0, 0, 0, .42);

  --font-serif: Georgia, "PT Serif", "Noto Serif", "Times New Roman", serif;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font: var(--font-serif);
  --size: 18px;
  --lh: 1.6;

  --bar-h: 52px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17171b);
    --fg: var(--tg-theme-text-color, #dcdce2);
    --hint: var(--tg-theme-hint-color, #8b8b96);
    --surface: var(--tg-theme-secondary-bg-color, #222228);
    --accent: var(--tg-theme-button-color, #5b93ea);
  }
}

:root[data-theme="light"] {
  --bg: #fcfcfa; --fg: #1f1f24; --hint: #7d7d87; --surface: #efefec;
  --accent: #2f6fd6; --accent-fg: #ffffff;
}
:root[data-theme="sepia"] {
  --bg: #f5ecd9; --fg: #43341f; --hint: #8c7a5e; --surface: #eadfc7;
  --accent: #9a5b22; --accent-fg: #fff8ea; --line: rgba(110, 86, 48, .26);
}
:root[data-theme="dark"] {
  --bg: #131316; --fg: #cfcfd6; --hint: #7f7f8a; --surface: #1f1f24;
  --accent: #6b9ff0; --accent-fg: #0d1522; --line: rgba(150, 150, 165, .22);
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.45 var(--font-sans);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- Полка ---------- */

.shelf-head { padding: calc(20px + var(--safe-top)) 20px 6px; max-width: 42rem; margin: 0 auto; }
.shelf-head h1 { margin: 0; font-size: 26px; line-height: 1.2; font-weight: 700; letter-spacing: -.01em; }
.shelf-sub { margin: 4px 0 0; color: var(--hint); font-size: 14px; }

.shelf-list { max-width: 42rem; margin: 0 auto; padding: 8px 12px calc(24px + var(--safe-bottom)); }

.card {
  display: flex; align-items: stretch; gap: 4px;
  background: var(--surface); border-radius: 14px; margin: 10px 0; overflow: hidden;
}
.card-open { flex: 1; min-width: 0; text-align: left; padding: 14px 6px 14px 16px; display: block; }
.card-title {
  font-family: var(--font-serif); font-size: 18px; line-height: 1.25; font-weight: 700;
  overflow-wrap: anywhere;
}
.card-by { margin-top: 3px; color: var(--hint); font-size: 14px; overflow-wrap: anywhere; }
.card-progress { display: flex; align-items: center; gap: 10px; margin-top: 11px; font-size: 13px; color: var(--hint); }
.card-progress .meter { flex: 1; max-width: 160px; }
.card-drop { flex: none; width: 44px; display: grid; place-items: center; color: var(--hint); }
.card-drop:active { color: var(--fg); }

.meter { display: block; height: 3px; border-radius: 2px; background: var(--line); overflow: hidden; }
.meter > i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 2px; }

.empty { max-width: 24rem; margin: 12vh auto 0; padding: 0 28px; text-align: center; }
.empty-mark { color: var(--hint); }
.empty-title { margin: 10px 0 6px; font-size: 19px; font-weight: 600; }
.empty-text { margin: 0; color: var(--hint); line-height: 1.5; }

/* ---------- Чтение ---------- */

.bar {
  position: fixed; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center;
  background: var(--bg);
  transition: transform .22s ease;
}
.bar-top {
  top: 0; height: calc(var(--bar-h) + var(--safe-top)); padding: var(--safe-top) 6px 0;
  border-bottom: 1px solid var(--line);
}
.bar-bottom {
  bottom: 0; height: calc(34px + var(--safe-bottom)); padding: 0 18px var(--safe-bottom);
  gap: 12px; border-top: 1px solid var(--line);
  font-size: 12.5px; color: var(--hint); font-variant-numeric: tabular-nums;
}
.bar-bottom .meter { flex: 1; }
.pos-label, .pos-pct { white-space: nowrap; }
.bars-hidden .bar-top { transform: translateY(-100%); }
.bars-hidden .bar-bottom { transform: translateY(100%); }

.bar-title { flex: 1; min-width: 0; padding: 0 10px; }
.bar-t1, .bar-t2 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-t1 { font-size: 15px; font-weight: 600; line-height: 1.25; }
.bar-t2 { font-size: 12.5px; color: var(--hint); line-height: 1.3; }

.icon-btn { flex: none; width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; }
.icon-btn:active { background: var(--surface); }
.icon-text { font-family: var(--font-serif); font-size: 19px; font-weight: 700; }

.page {
  max-width: 42rem; margin: 0 auto;
  padding: calc(var(--bar-h) + var(--safe-top) + 20px) 22px calc(64px + var(--safe-bottom));
  min-height: 100vh;
}

.text {
  font-family: var(--font); font-size: var(--size); line-height: var(--lh);
  overflow-wrap: break-word; hyphens: auto; -webkit-hyphens: auto;
  font-kerning: normal; text-rendering: optimizeLegibility;
}
.text .ch-head {
  font-size: 1.4em; line-height: 1.25; font-weight: 700; text-align: center;
  margin: .4em 0 1.3em; hyphens: manual; -webkit-hyphens: manual; text-wrap: balance;
}
.text p { margin: 0; text-indent: 1.5em; }
.text p.c { text-align: center; text-indent: 0; margin: .7em 0; }
.text p.r { text-align: right; text-indent: 0; }
.text h3 { font-size: 1.08em; line-height: 1.3; text-align: center; margin: 1.7em 0 .8em; }
.text hr { border: 0; height: 1px; width: 38%; margin: 1.9em auto; background: var(--line); }
.text sup, .text sub { line-height: 0; }

:root[data-para="gap"] .text p { text-indent: 0; margin-bottom: .8em; }
:root[data-justify="1"] .text p:not(.c):not(.r) { text-align: justify; }
:root[data-font="sans"] { --font: var(--font-sans); }

.pager { display: flex; gap: 12px; margin-top: 44px; }
.pager-btn {
  flex: 1; min-height: 50px; padding: 10px 14px; border-radius: 14px;
  background: var(--surface); font-size: 16px; font-weight: 600;
}
.pager-btn:active { opacity: .7; }
.pager-next { background: var(--accent); color: var(--accent-fg); }
.the-end { margin: 36px 0 0; text-align: center; color: var(--hint); font-size: 15px; }

/* ---------- Шторки ---------- */

.backdrop { position: fixed; inset: 0; z-index: 20; background: var(--shade); animation: fade .18s ease; }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  max-width: 42rem; margin: 0 auto; max-height: 82vh;
  display: flex; flex-direction: column;
  background: var(--bg); border-radius: 18px 18px 0 0;
  padding-bottom: calc(10px + var(--safe-bottom));
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .18);
  animation: rise .22s cubic-bezier(.2, .8, .2, 1);
}
.sheet-grip { flex: none; width: 38px; height: 4px; border-radius: 2px; margin: 9px auto 6px; background: var(--line); }
.sheet-head { flex: none; padding: 6px 20px 14px; border-bottom: 1px solid var(--line); }
.sheet-title { font-family: var(--font-serif); font-size: 19px; font-weight: 700; line-height: 1.25; overflow-wrap: anywhere; }
.sheet-sub { margin-top: 3px; color: var(--hint); font-size: 14px; overflow-wrap: anywhere; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.chip { min-height: 36px; padding: 6px 14px; border-radius: 18px; background: var(--surface); font-size: 14px; font-weight: 500; }
.chip:active { opacity: .7; }
.chip-accent, .chip.on { background: var(--accent); color: var(--accent-fg); }

.toc { list-style: none; margin: 0; padding: 6px 8px; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.toc button {
  display: flex; gap: 12px; width: 100%; text-align: left; padding: 11px 12px; border-radius: 10px;
  font-size: 16px; line-height: 1.3;
}
.toc button:active { background: var(--surface); }
.toc .num { flex: none; min-width: 1.9em; color: var(--hint); font-variant-numeric: tabular-nums; }
.toc .name { min-width: 0; overflow-wrap: anywhere; }
.toc .current { background: var(--surface); font-weight: 600; }
.toc .current .num { color: var(--accent); }

.set-row { display: flex; align-items: center; gap: 14px; padding: 9px 20px; }
.set-label { flex: none; width: 4.6em; color: var(--hint); font-size: 14px; }

.seg, .stepper { flex: 1; display: flex; background: var(--surface); border-radius: 11px; padding: 3px; gap: 3px; min-width: 0; }
.seg button { flex: 1; min-height: 36px; padding: 4px 6px; border-radius: 8px; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.seg button.on { background: var(--bg); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .14); }
.font-serif { font-family: var(--font-serif); }
.font-sans { font-family: var(--font-sans); }

.stepper button { flex: 1; min-height: 36px; border-radius: 8px; font-family: var(--font-serif); font-size: 16px; font-weight: 700; }
.stepper button:active { background: var(--bg); }
.stepper button:disabled { opacity: .35; }
.stepper output { flex: none; width: 3.2em; align-self: center; text-align: center; font-size: 14px; color: var(--hint); font-variant-numeric: tabular-nums; }

.themes { flex: 1; display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; min-width: 0; }
.swatch { min-height: 40px; padding: 4px 8px; border-radius: 11px; border: 2px solid transparent; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.swatch.on { border-color: var(--accent); font-weight: 600; }
.swatch-auto { background: var(--tg-theme-secondary-bg-color, var(--surface)); color: var(--tg-theme-text-color, var(--fg)); }
.swatch-light { background: #fcfcfa; color: #1f1f24; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .1); }
.swatch-sepia { background: #f5ecd9; color: #43341f; }
.swatch-dark { background: #131316; color: #cfcfd6; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .12); }

/* ---------- Загрузка, ошибки, подсказки ---------- */

.veil { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; background: var(--bg); padding: 28px; }
.veil.soft { background: transparent; pointer-events: none; }
.spinner {
  width: 30px; height: 30px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
.veil-msg { max-width: 22rem; text-align: center; }
.veil-msg p { margin: 0 0 16px; line-height: 1.5; }
.veil-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }

.toast {
  position: fixed; left: 50%; bottom: calc(52px + var(--safe-bottom)); z-index: 40;
  transform: translateX(-50%); max-width: calc(100vw - 40px);
  padding: 10px 16px; border-radius: 12px;
  background: var(--fg); color: var(--bg); font-size: 14px; text-align: center;
  animation: fade .18s ease;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }

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