:root {
  --bg: #f6f1e7; --fg: #2b2620; --muted: #8a7f70; --line: #e2d8c6;
  --card: #fffaf0; --accent: #b0602a; --hl: rgba(176, 96, 42, .16);
  --fs: 19px; --lh: 1.6;
  --font: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
}
body[data-theme="light"] { --bg: #ffffff; --fg: #1d1d1f; --muted: #86868b; --line: #e5e5ea; --card: #f5f5f7; --accent: #0a66d6; --hl: rgba(10, 102, 214, .12); }
body[data-theme="dark"]  { --bg: #121212; --fg: #d8d4cc; --muted: #7d786f; --line: #2a2a2a; --card: #1d1d1d; --accent: #e09a5f; --hl: rgba(224, 154, 95, .18); }
body[data-font="sans"] { --font: -apple-system, "SF Pro Text", "Segoe UI", Roboto, sans-serif; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--fg); font: 16px/1.4 -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* Библиотека */
#library { padding: calc(16px + var(--safe-t)) 16px calc(24px + var(--safe-b)); max-width: 720px; margin: 0 auto; }
.lib-head { display: flex; align-items: center; justify-content: space-between; }
.lib-head h1 { font-size: 28px; margin: 8px 0 16px; }
.book-list { display: grid; gap: 12px; }
.book { display: flex; gap: 14px; padding: 12px; border-radius: 14px; background: var(--card); text-align: left; width: 100%; }
.book img, .book .nocover { width: 64px; height: 92px; border-radius: 6px; object-fit: cover; background: var(--line); flex: none; }
.book b { display: block; font-size: 16px; margin-bottom: 4px; }
.book .meta { font-size: 13px; color: var(--muted); }
.book .bar-mini { height: 3px; background: var(--line); border-radius: 2px; margin-top: 10px; overflow: hidden; }
.book .bar-mini i { display: block; height: 100%; background: var(--accent); }

.at-block { margin-top: 20px; padding: 14px; border-radius: 14px; border: 1px dashed var(--line); display: grid; gap: 10px; }
.at-head { display: flex; align-items: center; gap: 8px; }
.at-head #at-status { flex: 1; }
.at-add { display: flex; gap: 8px; }
.at-add input, .at-form input { flex: 1; min-width: 0; font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--fg); }
.at-form { display: grid; gap: 10px; }
.play-wide { padding: 12px; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 600; }
.err { color: #c0392b; min-height: 1em; font-size: 14px; }
.job { font-size: 14px; color: var(--muted); }
.job.error { color: #c0392b; }
#at-lib-wrap summary { cursor: pointer; color: var(--accent); font-weight: 600; padding: 4px 0; }
.at-lib-item { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.at-lib-item span { flex: 1; }
.book { position: relative; }
.book .refresh { position: absolute; right: 8px; bottom: 8px; width: 34px; height: 34px; border-radius: 50%; background: var(--bg); color: var(--accent); font-size: 18px; }

/* Читалка */
#reader { position: fixed; inset: 0; display: flex; flex-direction: column; }
.bar { position: absolute; left: 0; right: 0; z-index: 5; background: var(--bg); transition: transform .2s, opacity .2s; }
.bar-top { top: 0; padding: calc(6px + var(--safe-t)) 8px 6px; display: flex; align-items: center; gap: 4px; border-bottom: 1px solid var(--line); }
.bar-bottom { bottom: 0; padding: 0 12px calc(8px + var(--safe-b)); border-top: 1px solid var(--line); }
#reader.chrome-hidden .bar-top { transform: translateY(-100%); opacity: 0; pointer-events: none; }
#reader.chrome-hidden .bar-bottom { transform: translateY(100%); opacity: 0; pointer-events: none; }
#reader.chrome-hidden.speaking .bar-bottom { transform: none; opacity: 1; pointer-events: auto; }
.bar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; }
.bar-title span, .bar-title small { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-title span { font-weight: 600; font-size: 15px; }
.bar-title small { color: var(--muted); font-size: 12px; }
.icon-btn { min-width: 40px; height: 40px; border-radius: 10px; font-size: 20px; }
.icon-btn:active { background: var(--line); }
.progress { height: 3px; background: var(--line); margin: 0 -12px; }
#progress-fill { height: 100%; width: 0; background: var(--accent); }
.bottom-row { display: flex; align-items: center; justify-content: center; padding-top: 4px; }
.chapter-row { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding-top: 6px; font-size: 13px; }
.ch-btn { padding: 8px 10px; border-radius: 10px; background: var(--card); color: var(--accent); font-weight: 600; white-space: nowrap; }
.ch-btn:disabled { opacity: .35; }
.ch-mid { flex: 1; min-width: 0; padding: 8px 4px; border-radius: 10px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch-mid:active, .ch-btn:active { background: var(--line); }
.text-btn { height: 40px; padding: 0 10px; border-radius: 10px; font-size: 15px; font-weight: 600; color: var(--accent); }
.tts-ctrl { display: flex; align-items: center; gap: 6px; }
.play-btn { width: 52px; height: 52px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; }
.play-btn svg { width: 24px; height: 24px; fill: currentColor; }
.play-btn .i-pause, #reader.speaking .play-btn .i-play { display: none; }
#reader.speaking .play-btn .i-pause { display: block; }
.skip-btn { width: 44px; height: 44px; border-radius: 50%; color: var(--fg); display: grid; place-items: center; }
.skip-btn:active { background: var(--line); }
.skip-btn svg { width: 22px; height: 22px; fill: currentColor; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.tts-ctrl { gap: 18px; }
.log-box { font: 12px/1.4 ui-monospace, Menlo, monospace; white-space: pre-wrap; background: var(--card); padding: 10px; border-radius: 10px; max-height: 50vh; overflow: auto; user-select: text; -webkit-user-select: text; }

.viewport { flex: 1; position: relative; overflow: hidden; }
.content { font-family: var(--font); font-size: var(--fs); line-height: var(--lh); hyphens: auto; -webkit-hyphens: auto; text-align: left; overflow-wrap: break-word; }
body[data-align="justify"] .content { text-align: justify; }
.content p { margin: 0 0 .1em; text-indent: 1.5em; }
.content p.sep { text-indent: 0; text-align: center; }
.content h2 { font-size: 1.25em; text-align: center; line-height: 1.3; margin: 1.2em 0 1em; text-indent: 0; break-after: avoid; }
.content .ch-first h2 { margin-top: 0; }
.content p.speak { background: var(--hl); border-radius: 4px; box-shadow: 0 0 0 4px var(--hl); }

/* Лента */
body[data-mode="scroll"] .viewport { overflow-y: auto; -webkit-overflow-scrolling: touch; }
body[data-mode="scroll"] .content { max-width: 680px; margin: 0 auto; padding: calc(64px + var(--safe-t)) 20px calc(150px + var(--safe-b)); }
body[data-mode="scroll"] .content section + section { margin-top: 2.5em; }

/* Страницы: колонки по ширине экрана, листаем scrollLeft */
/* В страницах панели стоят всегда, а текст — между ними: иначе панели закрывают заголовок и последние строки */
body[data-mode="pages"] .viewport { padding: calc(72px + var(--safe-t)) 0 calc(128px + var(--safe-b)); }
body[data-mode="pages"] #reader.chrome-hidden .bar-top,
body[data-mode="pages"] #reader.chrome-hidden .bar-bottom { transform: none; opacity: 1; pointer-events: auto; }
body[data-mode="pages"] .content { height: 100%; column-gap: 0; column-fill: auto; overflow: hidden; }
body[data-mode="pages"] .content section { break-before: column; }
body[data-mode="pages"] .content p, body[data-mode="pages"] .content h2 { margin-left: 22px; margin-right: 22px; }

.chip { position: absolute; z-index: 6; left: 50%; transform: translateX(-50%); bottom: calc(130px + var(--safe-b)); background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 20px; font-size: 14px; box-shadow: 0 4px 16px rgba(0,0,0,.2); }

body:not([data-engine="system"]) .system-only { display: none; }
#toast { position: fixed; z-index: 30; left: 50%; top: calc(70px + var(--safe-t)); transform: translateX(-50%); max-width: 90vw; background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; }

/* Листы */
.sheet { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.35); display: flex; align-items: flex-end; justify-content: center; }
.sheet-card { background: var(--bg); width: 100%; max-width: 640px; max-height: 85vh; overflow-y: auto; border-radius: 18px 18px 0 0; padding: 8px 16px calc(20px + var(--safe-b)); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; position: sticky; top: -8px; background: var(--bg); padding: 8px 0; }
.sheet h3 { margin: 18px 0 6px; font-size: 15px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.row input[type=range] { flex: 1; max-width: 55%; accent-color: var(--accent); }
.row select { max-width: 60%; font: inherit; padding: 6px; border-radius: 8px; background: var(--card); color: var(--fg); border: 1px solid var(--line); }
.seg { display: flex; background: var(--card); border-radius: 10px; padding: 2px; }
.seg button { padding: 6px 10px; border-radius: 8px; font-size: 14px; }
.seg button.on { background: var(--accent); color: #fff; }
.stepper { display: flex; align-items: center; gap: 10px; }
.stepper button { padding: 6px 12px; border-radius: 8px; background: var(--card); }
.toc-list { list-style: none; padding: 0; margin: 0; }
.toc-list li { padding: 12px 4px; border-bottom: 1px solid var(--line); cursor: pointer; display: flex; gap: 8px; justify-content: space-between; }
.toc-list li.cur { color: var(--accent); font-weight: 600; }
.toc-list li small { color: var(--muted); white-space: nowrap; }

/* Каталог author.today */
.lib-tabs { display: flex; gap: 6px; overflow-x: auto; margin: 0 -16px 12px; padding: 0 16px; scrollbar-width: none; }
.lib-tabs::-webkit-scrollbar { display: none; }
.lib-tabs button { flex: none; padding: 8px 14px; border-radius: 20px; background: var(--card); font-size: 14px; white-space: nowrap; }
.lib-tabs button.on { background: var(--accent); color: #fff; }
.cat-tools { display: grid; gap: 10px; margin-bottom: 14px; }
.cat-tools input[type=search] { font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--fg); }
.cat-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cat-row select { font: inherit; font-size: 14px; padding: 6px 8px; border-radius: 8px; background: var(--card); color: var(--fg); border: 1px solid var(--line); }
.chip-f { padding: 6px 12px; border-radius: 16px; border: 1px solid var(--line); font-size: 13px; }
.chip-f.on { background: var(--fg); color: var(--bg); border-color: var(--fg); }
#cat-count { margin-left: auto; }
.link-btn { color: var(--accent); font-weight: 600; padding: 0; }
.cat-group { margin-bottom: 18px; }
.cat-group header { display: flex; flex-direction: column; margin-bottom: 8px; }
.cat-group header b { font-size: 16px; }
.cat-group header.has-plus small { color: #2e8b57; }
.cat-shelf { display: flex; gap: 10px; overflow-x: auto; margin: 0 -16px; padding: 0 16px 4px; scroll-snap-type: x proximity; scrollbar-width: none; }
.cat-shelf::-webkit-scrollbar { display: none; }
.tile { flex: none; width: 104px; text-align: left; scroll-snap-align: start; display: flex; flex-direction: column; gap: 4px; }
.tile-cover { position: relative; width: 104px; height: 148px; border-radius: 8px; overflow: hidden; background: var(--line); }
.tile-cover img, .tile-cover .nocover { width: 100%; height: 100%; object-fit: cover; }
.tile-cover .num { position: absolute; left: 4px; top: 4px; background: rgba(0,0,0,.65); color: #fff; font-size: 12px; padding: 1px 6px; border-radius: 8px; }
.tile-bdg { position: absolute; left: 4px; right: 4px; bottom: 4px; display: flex; flex-wrap: wrap; gap: 3px; }
.tile-title { font-size: 13px; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.tile-sub { font-size: 11px; }
.bdg { font-size: 11px; padding: 1px 6px; border-radius: 8px; background: rgba(0,0,0,.6); color: #fff; white-space: nowrap; }
.bdg.plus { background: #2e8b57; }
.bdg.prog { background: #b0602a; }
.bdg.dl { background: #3b6fb6; }
.bdgs { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.cat-rows { display: grid; gap: 10px; }
.cat-row-card img, .cat-row-card .nocover { width: 56px; height: 80px; }
.bs-top { display: flex; gap: 14px; }
.bs-top img, .bs-top .nocover { width: 96px; height: 138px; border-radius: 8px; object-fit: cover; flex: none; }
.bs-top h3 { margin: 0 0 4px; font-size: 18px; }
.bs-s { font-size: 14px; margin-top: 4px; }
.bs-facts { font-size: 13px; margin: 12px 0; }
.bs-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.bs-actions .play-wide { flex: 1; min-width: 160px; }
.bs-actions a.ch-btn { text-decoration: none; }
.bs-ann { white-space: pre-line; font-size: 15px; line-height: 1.5; }
.cat-singles { margin-bottom: 18px; }
.cat-tools .seg button { padding: 6px 9px; font-size: 13px; }
