/* Цвета приложения берутся из темы Telegram (--tg-theme-*), вне Telegram — свои. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --fg: var(--tg-theme-text-color, #111418);
  --hint: var(--tg-theme-hint-color, #7d8590);
  --link: var(--tg-theme-link-color, #2b8a6e);
  --accent: var(--tg-theme-button-color, #2b8a6e);
  --accent-fg: var(--tg-theme-button-text-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --section: var(--tg-theme-section-bg-color, var(--bg));
  --line: color-mix(in srgb, var(--fg) 12%, transparent);
  --danger: var(--tg-theme-destructive-text-color, #d93f3f);

  --safe-top: max(env(safe-area-inset-top, 0px), calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px)));
  --safe-bottom: max(env(safe-area-inset-bottom, 0px), calc(var(--tg-safe-area-inset-bottom, 0px) + var(--tg-content-safe-area-inset-bottom, 0px)));
  /* В полноэкранном режиме сверху — строка состояния телефона (--tg-safe-area-inset-top), под ней
     кнопки Telegram «Закрыть/Назад» и «⋯» (--tg-content-safe-area-inset-top). */
  --sys-top: max(env(safe-area-inset-top, 0px), var(--tg-safe-area-inset-top, 0px));
  --tg-controls: var(--tg-content-safe-area-inset-top, 0px);

  /* Читалка: тема «Telegram» повторяет цвета приложения. */
  --r-bg: var(--bg);
  --r-fg: var(--fg);
  --r-hint: var(--hint);
  --r-link: var(--link);
  --r-size: 18px;
  --r-lh: 1.55;
  --r-font: Georgia, "Times New Roman", "PT Serif", serif;
  --r-align: justify;
  --r-margin: 20px;

  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #17191c; --fg: #e8eaed; --hint: #8b929b; --link: #5cc7a4; --accent: #3aa585;
    --bg2: #0f1113; --section: #1e2125;
  }
}

:root[data-theme="light"] { --r-bg: #ffffff; --r-fg: #1b1b1b; --r-hint: #8a8a8a; --r-link: #23705a; }
:root[data-theme="sepia"] { --r-bg: #f4ecd8; --r-fg: #3b2f1e; --r-hint: #8c7b61; --r-link: #8a4b14; }
:root[data-theme="dark"]  { --r-bg: #121212; --r-fg: #c9c9c9; --r-hint: #6f6f6f; --r-link: #7cb9a5; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg2); color: var(--fg); overscroll-behavior: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 0; border-radius: 10px;
  padding: 9px 14px; font-size: 15px; font-weight: 600;
}
.btn svg { width: 18px; height: 18px; }
.btn-primary { background: var(--accent); color: var(--accent-fg); }
.btn-ghost { background: var(--bg2); color: var(--link); }
.btn-danger { background: var(--bg2); color: var(--danger); }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: transparent; flex: none;
}
.icon-btn:active { background: var(--line); }

/* ---------- Библиотека ---------- */
#library { padding: calc(var(--safe-top) + 8px) 16px calc(var(--safe-bottom) + 24px); max-width: 920px; margin: 0 auto; }
.lib-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0 12px; }
.lib-head h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--hint); margin: 22px 0 10px; font-weight: 600; }

.upload { background: var(--section); border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; }
.upload-text { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; margin-bottom: 8px; }
.upload-text span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-text span:last-child { color: var(--hint); flex: none; }
.bar { height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

.continue {
  display: flex; gap: 14px; background: var(--section); border-radius: 16px; padding: 14px; align-items: center;
  border: 0; width: 100%; text-align: left;
}
.continue .cover { width: 76px; flex: none; }
.continue-body { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.continue-label { font-size: 12px; color: var(--accent); font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.continue-title { font-size: 17px; font-weight: 700; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.continue-author { font-size: 14px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.continue .bar { margin-top: 6px; }
.continue-pct { font-size: 12px; color: var(--hint); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 18px 14px; }
.card { position: relative; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.card-open { border: 0; padding: 0; background: none; text-align: left; display: flex; flex-direction: column; gap: 6px; }
.card-title { font-size: 14px; font-weight: 600; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-author { font-size: 12px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .bar { height: 3px; }
.card-more {
  position: absolute; top: 4px; right: 4px; width: 30px; height: 30px; border-radius: 50%; border: 0;
  background: rgba(0,0,0,.45); color: #fff; display: grid; place-items: center; backdrop-filter: blur(4px);
}
.card-more svg { width: 18px; height: 18px; }
.badge-new { font-size: 11px; color: var(--accent); font-weight: 700; }

.cover {
  aspect-ratio: 2 / 3; width: 100%; border-radius: 6px; overflow: hidden; position: relative;
  box-shadow: 0 1px 2px rgba(0,0,0,.12), 0 4px 14px rgba(0,0,0,.10); background: var(--line);
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-gen {
  position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: space-between;
  padding: 10px 9px; color: #fff; font-family: Georgia, serif;
}
.cover-gen b { font-size: 13px; line-height: 1.2; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }
.cover-gen span { font-size: 10px; opacity: .85; font-family: -apple-system, sans-serif; }
.continue .cover-gen b { font-size: 10px; }
.continue .cover-gen span { display: none; }

.empty { text-align: center; color: var(--hint); padding: 48px 24px; font-size: 15px; line-height: 1.5; }
.empty b { color: var(--fg); font-size: 17px; }
.empty-icon { font-size: 48px; }

/* ---------- Читалка ---------- */
#reader { position: fixed; inset: 0; background: var(--r-bg); color: var(--r-fg); overflow: hidden; touch-action: pan-y; }
#viewport {
  position: absolute; left: 0; right: 0; top: calc(var(--safe-top) + 28px); bottom: calc(var(--safe-bottom) + 34px);
  overflow: hidden; overflow: clip; user-select: text; -webkit-user-select: text;
}
#pages {
  position: absolute; top: 0; left: 0; height: 100%; width: 100%;
  padding: 0 var(--r-margin); column-fill: auto; will-change: transform;
}
#pages.animate { transition: transform .26s cubic-bezier(.25,.8,.3,1); }

.book-text { font-family: var(--r-font); font-size: var(--r-size); line-height: var(--r-lh); text-align: var(--r-align); hyphens: auto; -webkit-hyphens: auto; overflow-wrap: break-word; }
.book-text p { margin: 0; text-indent: 1.5em; }
.book-text p + p { margin-top: .15em; }
.book-text h1, .book-text h2, .book-text h3, .book-text h4, .book-text h5, .book-text h6 {
  text-align: center; text-indent: 0; line-height: 1.3; margin: 1.2em 0 .9em; break-after: avoid; hyphens: manual;
}
.book-text h1 { font-size: 1.45em; } .book-text h2 { font-size: 1.3em; } .book-text h3 { font-size: 1.15em; }
.book-text h4, .book-text h5, .book-text h6 { font-size: 1.05em; }
.book-text > :first-child, .book-text > span:first-child + * { margin-top: 0; }
.book-text a { color: var(--r-link); text-decoration: none; }
.book-text a[data-note] { font-size: .75em; vertical-align: super; line-height: 0; padding: 0 2px; }
.book-text img { max-width: 100%; max-height: calc(var(--page-h, 80vh) - 2em); height: auto; display: block; margin: .8em auto; break-inside: avoid; }
.book-text blockquote { margin: 1em 0 1em 1.5em; }
.book-text blockquote p { text-indent: 0; }
.book-text .epigraph { margin-left: 30%; font-size: .92em; font-style: italic; }
.book-text .text-author { text-align: right; font-style: italic; text-indent: 0; }
.book-text .subtitle { text-align: center; font-weight: 600; text-indent: 0; margin: 1em 0; }
.book-text .empty-line { height: 1em; }
.book-text .poem { margin: 1em 0 1em 10%; }
.book-text .stanza { margin: .7em 0; }
.book-text .v { text-indent: 0; text-align: left; }
.book-text .image { text-indent: 0; }
.book-text table { border-collapse: collapse; margin: 1em 0; font-size: .9em; }
.book-text td, .book-text th { border: 1px solid color-mix(in srgb, var(--r-fg) 25%, transparent); padding: 4px 6px; text-indent: 0; }
.book-text pre { white-space: pre-wrap; font-size: .85em; }
.book-text hr { border: 0; border-top: 1px solid color-mix(in srgb, var(--r-fg) 25%, transparent); margin: 1.5em 20%; }
.book-text li p { text-indent: 0; }

#status {
  position: absolute; left: 0; right: 0; bottom: var(--safe-bottom); height: 34px; padding: 0 var(--r-margin);
  display: flex; align-items: center; justify-content: space-between; gap: 16px; font-size: 12px; color: var(--r-hint);
  font-variant-numeric: tabular-nums; pointer-events: none;
}
#status-chapter { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Полный экран: название главы — в строке кнопок Telegram, между ними; текст начинается сразу под ней. */
#status-top {
  display: none; position: absolute; top: var(--sys-top); left: 50%; transform: translateX(-50%);
  height: var(--tg-controls); max-width: calc(100% - 220px); align-items: center; justify-content: center;
  font-size: 12px; color: var(--r-hint); pointer-events: none;
}
#status-top span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fullscreen #status-top { display: flex; }
.fullscreen #status-chapter { visibility: hidden; }
.fullscreen #viewport { top: calc(var(--safe-top) + 6px); }
/* «Назад» в полноэкранном режиме — кнопка Telegram в левом верхнем углу. */
.fullscreen #btn-back { display: none; }
.fullscreen #topbar .topbar-title { padding-left: 12px; }
#status-page { flex: none; }

.bar-ui {
  position: absolute; left: 0; right: 0; display: flex; align-items: center; gap: 4px; padding: 6px 8px;
  background: color-mix(in srgb, var(--r-bg) 92%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  color: var(--r-fg); transition: transform .2s, opacity .2s; z-index: 5;
}
#topbar { top: 0; padding-top: calc(var(--safe-top) + 6px); border-bottom: 1px solid color-mix(in srgb, var(--r-fg) 10%, transparent); }
#bottombar { bottom: 0; padding-bottom: calc(var(--safe-bottom) + 8px); border-top: 1px solid color-mix(in srgb, var(--r-fg) 10%, transparent); }
#reader:not(.ui) #topbar { transform: translateY(-100%); opacity: 0; pointer-events: none; }
#reader:not(.ui) #bottombar { transform: translateY(100%); opacity: 0; pointer-events: none; }
.topbar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; padding: 0 4px; }
.topbar-title b, .topbar-title span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title b { font-size: 15px; }
.topbar-title span { font-size: 12px; color: var(--r-hint); }
.slider-wrap { flex: 1; display: flex; flex-direction: column; align-items: stretch; gap: 2px; padding: 0 6px; }
#slider { width: 100%; accent-color: var(--accent); }
#slider-label { font-size: 12px; color: var(--r-hint); text-align: center; font-variant-numeric: tabular-nums; }

.loading { position: absolute; inset: 0; display: grid; place-items: center; z-index: 3; background: var(--r-bg); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid color-mix(in srgb, var(--r-fg) 15%, transparent); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Панели ---------- */
#scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--bg); color: var(--fg);
  border-radius: 16px 16px 0 0; padding: 4px 16px calc(var(--safe-bottom) + 16px); max-height: min(85vh, calc(100vh - var(--safe-top) - 12px)); overflow-y: auto;
  max-width: 640px; margin: 0 auto; animation: up .2s ease-out; box-shadow: 0 -4px 24px rgba(0,0,0,.15);
}
.sheet-tall { height: min(85vh, calc(100vh - var(--safe-top) - 12px)); }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 0 8px; position: sticky; top: -4px; background: var(--bg); z-index: 1; }
.sheet-head b { font-size: 17px; }

.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-list button {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 11px 8px; font-size: 15px;
  border-bottom: 1px solid var(--line); line-height: 1.3;
}
.toc-list button.current { color: var(--link); font-weight: 700; }
.toc-list button:active { background: var(--bg2); }

.setting { display: flex; flex-direction: column; gap: 8px; padding: 10px 0; }
.setting > span { font-size: 13px; color: var(--hint); }
.seg { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.seg button { flex: 1; min-width: 0; padding: 10px 6px; border-radius: 10px; border: 2px solid transparent; background: var(--bg2); font-size: 14px; }
.seg button.on { border-color: var(--accent); }
.seg output { min-width: 48px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.seg .serif { font-family: Georgia, serif; }
.themes .t-light { background: #fff; color: #1b1b1b; }
.themes .t-sepia { background: #f4ecd8; color: #3b2f1e; }
.themes .t-dark { background: #121212; color: #c9c9c9; }
.themes button { box-shadow: inset 0 0 0 1px var(--line); }

.info-top { display: flex; gap: 16px; margin: 4px 0 16px; }
.info-top .cover { width: 110px; flex: none; }
.info-meta { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.info-meta h3 { margin: 0; font-size: 19px; line-height: 1.25; }
.info-meta .author { color: var(--hint); }
.info-meta .facts { font-size: 13px; color: var(--hint); }
.info-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.info-actions .btn { flex: 1; justify-content: center; }
.annotation { font-size: 15px; line-height: 1.5; white-space: pre-line; }

.note-body { max-height: 50vh; overflow-y: auto; margin-bottom: 12px; --r-fg: var(--fg); }
.note-body p { text-indent: 0; }
.note-body h1, .note-body h2, .note-body h3, .note-body h4, .note-body h5 { display: none; }
#note-go { width: 100%; justify-content: center; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 24px); transform: translateX(-50%); z-index: 30;
  background: rgba(20,20,20,.9); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: calc(100% - 32px);
}
