:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #2f6feb;
  --accent-dark: #1f52c0;
  --warn: #b4541a;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1d2027;
    --text: #e6e8ec;
    --muted: #9aa2ad;
    --border: #2c313a;
    --accent: #5b8dff;
    --accent-dark: #3f74f0;
    --warn: #e08a4a;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* -------- Шапка -------- */
.topbar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar-inner {
  max-width: 1000px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 18px; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }

.search { display: flex; gap: 6px; flex: 1 1 260px; min-width: 200px; }
.search input {
  flex: 1; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--bg); color: var(--text);
}
.search button, .btn {
  padding: 8px 14px; border: 1px solid var(--accent); background: var(--accent);
  color: #fff; border-radius: var(--radius); cursor: pointer; font-size: 14px;
  white-space: nowrap; display: inline-block;
}
.search button:hover, .btn:hover { background: var(--accent-dark); text-decoration: none; }

.nav { display: flex; gap: 4px; flex-wrap: wrap; }
.nav a {
  padding: 6px 10px; border-radius: var(--radius); color: var(--text);
}
.nav a:hover { background: var(--bg); text-decoration: none; }

/* -------- Основной контейнер -------- */
.container { max-width: 1000px; margin: 0 auto; padding: 20px 16px 60px; }
h1 { font-size: 24px; margin: 0 0 12px; }
h2 { font-size: 19px; margin: 24px 0 12px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.page-head { margin-bottom: 12px; }

.notice {
  background: var(--panel); border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  padding: 12px 16px; border-radius: var(--radius); margin: 16px 0;
}
.notice-error { border-left-color: var(--warn); }
code { background: var(--bg); padding: 1px 5px; border-radius: 4px; }

/* -------- Главная -------- */
.hero { text-align: center; padding: 20px 0 10px; }
.hero h1 { font-size: 30px; }
.search-big { max-width: 560px; margin: 16px auto; }
.stats { display: flex; justify-content: center; gap: 26px; flex-wrap: wrap; margin: 18px 0; }
.stat b { display: block; font-size: 24px; }
.stat span { color: var(--muted); font-size: 13px; }

/* -------- Список книг -------- */
.book-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.book {
  display: flex; gap: 14px; background: var(--panel);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 12px;
}
.book-deleted { opacity: 0.6; }
.book-missing { border-color: var(--warn); }

.cover {
  width: 60px; height: 84px; object-fit: cover; border-radius: 4px;
  flex-shrink: 0; background: var(--bg); border: 1px solid var(--border);
}
.cover-empty {
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 12px; font-weight: 600; letter-spacing: 1px;
}
.book-cover-link { flex-shrink: 0; }

.book-main { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.book-authors { color: var(--muted); font-size: 13px; }
.book-title { font-weight: 600; font-size: 16px; color: var(--text); }
.book-series { font-size: 13px; }
.serno { color: var(--muted); margin-left: 4px; }
.book-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 2px; }

.tag {
  font-size: 12px; padding: 2px 8px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 12px; color: var(--muted);
}
.tag:hover { text-decoration: none; border-color: var(--accent); color: var(--accent); }
.chip { font-size: 12px; color: var(--muted); padding: 2px 6px; }
.chip-warn { color: var(--warn); font-weight: 600; }

.book-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.btn-ghost {
  background: transparent; color: var(--accent); border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--bg); }
.btn.disabled { opacity: 0.5; pointer-events: none; }

.empty { color: var(--muted); padding: 30px 0; text-align: center; }

/* -------- Пагинация / фильтры -------- */
.pager { display: flex; align-items: center; gap: 12px; justify-content: center; margin: 22px 0; }
.pager-info { color: var(--muted); font-size: 14px; }
.missing-toggle { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.filters { display: flex; gap: 10px; align-items: center; margin: 8px 0 16px; flex-wrap: wrap; }
.filters input { padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--panel); color: var(--text); }

/* -------- Страница книги -------- */
.book-page { display: flex; gap: 28px; align-items: flex-start; flex-wrap: wrap; }
.book-page-cover { flex: 0 0 220px; text-align: center; }
.cover-big { width: 220px; height: auto; border-radius: var(--radius); border: 1px solid var(--border); }
.cover.cover-big { width: 220px; height: 300px; font-size: 20px; }
.book-page-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.book-page-main { flex: 1 1 380px; min-width: 0; }
.book-page-authors { color: var(--muted); margin-top: -6px; }

.meta-grid { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 16px 0; }
.meta-grid dt { color: var(--muted); }
.meta-grid dd { margin: 0; }
.annotation-text { background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; white-space: pre-wrap; }

/* -------- Форма настроек -------- */
.settings-form { display: flex; flex-direction: column; gap: 14px; max-width: 560px; }
.settings-form label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
.settings-form label.checkbox { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.settings-form input[type=text], .settings-form input[type=number],
.settings-form select {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--panel); color: var(--text); font-size: 14px;
}
/* Конвертеры и привязка форматов */
.settings-form h3.sub { font-size: 15px; margin: 18px 0 8px; color: var(--text); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.chip-ok { color: #2e7d32; font-weight: 600; }
@media (prefers-color-scheme: dark) { .chip-ok { color: #7bc47f; } }
.conv-engines { display: flex; flex-direction: column; gap: 10px; }
.conv-engine {
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; display: flex; flex-direction: column; gap: 6px;
  background: var(--panel);
}
.conv-engine-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.conv-map-table {
  width: 100%; border-collapse: collapse; color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
}
.conv-map-table th, .conv-map-table td {
  padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.conv-map-table thead th {
  font-size: 13px; font-weight: 600; color: var(--muted);
  background: var(--panel); text-transform: none;
}
.conv-map-table tbody tr:last-child td { border-bottom: none; }
.conv-map-table td.conv-fmt { font-weight: 600; white-space: nowrap; width: 90px; }
.conv-map-table select { width: 100%; }

/* -------- Алфавитный указатель (авторы / серии) -------- */
.alpha { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 20px; }
.alpha-letter {
  display: inline-flex; align-items: baseline; gap: 4px;
  padding: 5px 10px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--panel); color: var(--text); font-weight: 600;
}
.alpha-letter:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
.alpha-letter.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.alpha-count { font-size: 11px; font-weight: 400; color: var(--muted); }
.alpha-letter.active .alpha-count { color: rgba(255, 255, 255, 0.8); }

/* -------- Списки имён (авторы / серии / жанры) -------- */
.name-list, .genre-list {
  list-style: none; margin: 0; padding: 0;
  columns: 2; column-gap: 28px;
}
.name-list li, .genre-list li {
  break-inside: avoid; padding: 5px 0; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; gap: 10px; align-items: baseline;
}
.name-list .count, .genre-list .count {
  color: var(--muted); font-size: 12px; flex-shrink: 0;
}

/* -------- Дерево жанров -------- */
.meta-genre { margin-bottom: 20px; }
.meta-genre h2 { border-bottom: 2px solid var(--border); padding-bottom: 6px; }

/* -------- Группы серий на странице автора -------- */
.series-group { margin-bottom: 24px; }
.series-group h2 a { color: var(--text); }
.series-group h2 a:hover { color: var(--accent); }

@media (max-width: 600px) {
  .name-list, .genre-list { columns: 1; }
}

/* -------- Настройки: задачи и прогресс -------- */
.settings-actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 4px 0 8px; }
.settings-form h2 { margin-top: 22px; }
.task-box {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px; margin: 4px 0 8px;
}
.task-box.hidden { display: none; }
.task-title { font-weight: 600; margin-bottom: 8px; }
.task-info { color: var(--muted); font-size: 13px; margin-top: 8px; }
.progress {
  height: 10px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 6px; overflow: hidden;
}
.progress-bar {
  height: 100%; width: 0; background: var(--accent);
  transition: width 0.4s ease;
}

/* -------- Избранное (звёздочка) -------- */
.book-headline { display: flex; align-items: flex-start; gap: 8px; }
.book-headline h1 { margin: 0; }
.book-headline .book-title { flex: 1; min-width: 0; }
.fav-toggle {
  flex-shrink: 0; background: none; border: none; cursor: pointer;
  color: var(--border); font-size: 20px; line-height: 1; padding: 0 2px;
}
.fav-toggle:hover { color: var(--accent); }
.fav-toggle.on { color: #e8a92b; }
.fav-name { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
.fav-name .fav-toggle { font-size: 16px; align-self: center; }

/* -------- Список закладок (карточка книги + данные закладки) -------- */
.bm-card.bm-auto { border-left: 3px solid var(--accent); }
.bm-panel {
  display: flex; align-items: center; gap: 10px;
  margin: 8px 0 2px; max-width: 380px;
}
.bm-bar {
  flex: 1; height: 8px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 4px; overflow: hidden;
}
.bm-bar-fill { height: 100%; background: var(--accent); }
.bm-pct { color: var(--muted); font-size: 13px; white-space: nowrap; }
.chip.chip-auto { color: var(--accent); font-weight: 600; }
.bm-note {
  margin-top: 4px; padding: 6px 10px; font-size: 14px;
  background: var(--bg); border-radius: var(--radius);
}
.bm-card .book-actions form { margin: 0; }

/* -------- Читалка -------- */
/* Раскладка колонкой во всю ВИДИМУЮ высоту. Ключевое — 100dvh (dynamic viewport
   height): на мобильных 100vh считается без панели браузера и оказывается выше
   реально видимой области, из-за чего нижние строки уходили за край. */
.reader-body {
  margin: 0; height: 100vh; height: 100dvh;
  display: flex; flex-direction: column; overflow: hidden; background: var(--bg);
}
.reader-bar {
  flex: 0 0 auto; height: 48px; display: flex; align-items: center; gap: 12px;
  padding: 0 12px; background: var(--panel); border-bottom: 1px solid var(--border);
  position: relative;   /* якорь для выпадающей панели настроек */
}
.reader-titles { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.reader-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-author { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-tools { display: flex; gap: 6px; }
.reader-btn {
  background: transparent; border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); cursor: pointer; font-size: 15px; padding: 6px 10px; text-decoration: none;
  display: inline-flex; align-items: center;
}
.reader-btn:hover { background: var(--bg); text-decoration: none; }
.reader-tools { align-items: center; }
.navb { padding: 6px 8px; font-size: 13px; }
@media (max-width: 600px) { .navb { padding: 5px 6px; font-size: 12px; } .reader-tools { gap: 3px; } }

/* Область текста заполняет остаток по флексу. Поля (M) и вертикальное
   центрирование задаёт JS-расчёт (ТЗ), поэтому padding здесь нет. «Бумажный»
   фон для комфортного чтения (не чистый белый). */
.reader-viewport {
  flex: 1 1 auto; min-height: 0; position: relative;
  overflow: hidden; background: #faf9f6;
}
.reader-body { background: #faf9f6; }
.reader-content {
  color: #222222;
  transition: transform 0.25s ease; will-change: transform;
  font-family: "PT Serif", "Literata", "Noto Serif", Georgia, Charter,
               "Times New Roman", serif;
  font-size: 18px; line-height: 1.6;
  text-align: justify; hyphens: auto; -webkit-hyphens: auto;
  orphans: 2; widows: 2;
}
@media (prefers-color-scheme: dark) {
  .reader-viewport, .reader-body { background: #16171a; }
  .reader-content { color: #cdc9c0; }
}
.reader-content .fb2-body { max-width: none; }
/* Абзац: отступ первой строки без вертикальной отбивки (ТЗ — один из способов). */
.reader-content p { margin: 0; text-indent: 1.4em; text-align: justify; hyphens: auto; }
.reader-content .fb2-title + p, .reader-content .fb2-subtitle + p,
.reader-content .fb2-section > p:first-child,
.reader-content .fb2-body > p:first-child { text-indent: 0; }
.reader-content .fb2-title { font-weight: 700; font-size: 1.2em; margin: 0.6em 0 0.8em; }
.reader-content .fb2-title p { text-align: center; margin: 0.2em 0; }
.reader-content .fb2-subtitle { font-weight: 600; text-align: center; margin: 0.8em 0 0.5em; }
.reader-content .fb2-epigraph, .reader-content .fb2-cite {
  margin: 0.8em 1.2em; color: var(--muted); font-style: italic; border: 0;
}
.reader-content .fb2-verse { margin: 0 0 0.2em; text-align: left; text-indent: 0; hyphens: none; }
.reader-content .fb2-stanza { margin: 0.6em 0; }
.reader-content .fb2-text-author { text-align: right; color: var(--muted); font-style: italic; }
.reader-content .fb2-empty { height: 0.9em; }
.reader-content .fb2-note { vertical-align: super; font-size: 0.75em; color: var(--accent); }
.reader-content img.fb2-image { max-width: 100%; max-height: 90%; display: block; margin: 0.6em auto; }

.reader-nav {
  position: absolute; top: 0; bottom: 0; width: 46px; z-index: 2;
  background: transparent; border: none; cursor: pointer;
  color: var(--muted); font-size: 30px; opacity: 0.5;
}
.reader-nav:hover { opacity: 1; background: var(--panel); }
.reader-prev { left: 0; }
.reader-next { right: 0; }

.reader-foot {
  flex: 0 0 auto; height: 34px;
  display: flex; align-items: center; gap: 12px; padding: 0 16px;
  background: var(--panel); border-top: 1px solid var(--border);
}
.reader-progress { flex: 1; height: 6px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 4px; overflow: hidden; }
.reader-progress-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.25s ease; }
.reader-page { color: var(--muted); font-size: 12px; white-space: nowrap; }

.reader-toast {
  position: fixed; left: 50%; bottom: 50px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: var(--radius);
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity 0.3s ease;
}
.reader-toast.show { opacity: 0.95; }

/* -------- Панель настроек чтения -------- */
.reader-panel {
  position: absolute; top: 52px; right: 8px; z-index: 20;
  width: 300px; max-width: calc(100vw - 16px);
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 10px 34px rgba(0, 0, 0, 0.22);
  padding: 12px 14px; display: flex; flex-direction: column; gap: 12px;
}
.reader-panel[hidden] { display: none; }
.rp-row { display: flex; flex-direction: column; gap: 6px; }
.rp-label { font-size: 13px; color: var(--muted); }
.rp-label b { color: var(--text); font-weight: 600; }
.rp-seg { display: flex; gap: 6px; flex-wrap: wrap; }
.rp-seg button, .rp-mini {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--text); cursor: pointer; font-size: 13px;
}
.rp-seg button:hover, .rp-mini:hover { border-color: var(--accent); }
.rp-seg button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.rp-row input[type=range] { width: 100%; }
.rp-mini { align-self: flex-start; }
.reader-fallback { display: flex; align-items: center; justify-content: center; }
.reader-fallback .empty-box { text-align: center; }

@media (max-width: 600px) {
  .reader-nav { display: none; }         /* на телефоне — свайп/тап */
  .reader-author { display: none; }
}

/* -------- Подвал -------- */
.footer { text-align: center; color: var(--muted); font-size: 13px; padding: 20px; }

@media (max-width: 600px) {
  .nav { order: 3; width: 100%; }
  .cover-big, .cover.cover-big { width: 140px; }
  .book-page-cover { flex-basis: 140px; margin: 0 auto; }
}

/* -------- Учётная запись в шапке -------- */
.account { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.account-name {
  padding: 6px 10px; border-radius: var(--radius); color: var(--text);
  font-weight: 600; max-width: 160px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.account-name:hover { background: var(--bg); text-decoration: none; }
.inline { display: inline-flex; align-items: center; gap: 6px; margin: 0; }
.linkish {
  background: none; border: none; padding: 6px 4px; cursor: pointer;
  color: var(--muted); font-size: 14px; font-family: inherit;
}
.linkish:hover { color: var(--text); text-decoration: underline; }

/* -------- Вход -------- */
.auth-box {
  max-width: 380px; margin: 40px auto; background: var(--panel);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 24px;
}
.auth-form { display: flex; flex-direction: column; gap: 14px; margin: 18px 0; }
.auth-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 14px; color: var(--muted);
}
.auth-form input {
  padding: 8px 10px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--bg); color: var(--text);
}
.cmd {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px; overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px; user-select: all;
}

/* -------- Подразделы настроек -------- */
.subnav { display: flex; gap: 14px; flex-wrap: wrap; font-size: 14px; }

/* -------- Таблицы администрирования -------- */
.table-scroll { overflow-x: auto; }
.admin-table, .perm-table {
  width: 100%; border-collapse: collapse; background: var(--panel);
  border: 1px solid var(--border); border-radius: var(--radius);
  margin: 12px 0; font-size: 14px;
}
.admin-table th, .admin-table td,
.perm-table th, .perm-table td {
  padding: 8px 10px; border-bottom: 1px solid var(--border);
  text-align: left; vertical-align: top;
}
.admin-table thead th, .perm-table thead th {
  color: var(--muted); font-weight: 600; font-size: 13px;
}
.admin-table tbody tr:last-child td,
.perm-table tbody tr:last-child td { border-bottom: none; }
.admin-table select, .admin-table input[type=password] {
  padding: 5px 8px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--bg); color: var(--text);
}
.admin-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.admin-actions .btn { padding: 5px 10px; font-size: 13px; }
.row-off { opacity: 0.55; }
.btn.danger { border-color: var(--warn); color: var(--warn); }
.btn.danger:hover { background: var(--warn); color: #fff; }

/* Матрица «право x группа»: строка — право, столбец — группа. */
.perm-table td.perm-name, .perm-table th.perm-name { min-width: 220px; }
.perm-table .perm-cell { text-align: center; vertical-align: middle; }
.perm-table .perm-cell input { width: 18px; height: 18px; cursor: pointer; }
.perm-table .perm-cell input:disabled { cursor: not-allowed; opacity: 0.35; }
.perm-table tbody tr:hover { background: var(--bg); }
