/* «Читалка» */
:root {
  --bar: #2f3b45; --bar-fg: #fff; --bg: #f3f4f6; --card: #fff; --fg: #1f2328; --muted: #6b7280; --line: #e3e5e8;
  --accent: #1f7ae0; --danger: #c0392b; --page-bg: #e9eaec;
  --c-yellow: #ffd400; --c-green: #5fd068; --c-blue: #4ea8ff; --c-pink: #ff6fae; --c-orange: #ff9a2e;
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
}
[data-theme=sepia] { --bar: #5b4a36; --bg: #efe6d2; --card: #f8f1e1; --fg: #3b2f22; --muted: #7d6b55; --line: #dccfb3; --page-bg: #e2d6bd; }
[data-theme=dark] { --bar: #1b1b1b; --bg: #121212; --card: #1e1e1e; --fg: #e3e3e3; --muted: #9a9a9a; --line: #2c2c2c; --accent: #6aa8ff; --page-bg: #0c0c0c; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); font: 15px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; overscroll-behavior: none; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
#app { height: 100%; }
.boot { height: 100%; display: flex; align-items: center; justify-content: center; }
.spinner { width: 34px; height: 34px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.9s linear infinite; margin: 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }

.btn { padding: 9px 16px; border-radius: 8px; background: var(--card); border: 1px solid var(--line); font-weight: 500; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; }
.icon-btn { min-width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center; border-radius: 20px; font-size: 19px; }
.icon-btn.small { min-width: 30px; height: 30px; font-size: 16px; }
.icon-btn:active { background: rgba(127, 127, 127, 0.18); }
.input { width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--fg); font: inherit; }
textarea.input { resize: vertical; min-height: 110px; }

/* вход */
.login-wrap { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 20px; background: var(--bar); }
.login { width: 100%; max-width: 340px; background: var(--card); border-radius: 14px; padding: 26px 22px; display: flex; flex-direction: column; gap: 10px; text-align: center; }
.login h1 { margin: 0; font-size: 24px; }
.login-logo { font-size: 44px; }
.login-sub { margin: 0 0 8px; color: var(--muted); font-size: 13px; }
.login-err { color: var(--danger); min-height: 18px; font-size: 13px; }

/* шапка */
.bar { display: flex; align-items: center; gap: 6px; padding: calc(6px + var(--safe-top)) 8px 6px 14px; background: var(--bar); color: var(--bar-fg); position: sticky; top: 0; z-index: 5; }
.bar-title { font-weight: 600; font-size: 18px; margin-right: 6px; }
.search { flex: 1; min-width: 0; }
.search input { width: 100%; padding: 8px 12px; border-radius: 18px; border: 0; background: rgba(255, 255, 255, 0.15); color: #fff; font: inherit; outline: none; }
.search input::placeholder { color: rgba(255, 255, 255, 0.6); }
.sync-state { gap: 5px; font-size: 12px; padding: 0 8px; opacity: 0.9; }
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: #4cd964; display: inline-block; }
.sync-state.busy .sync-dot { background: #ffcc00; animation: pulse 0.8s ease-in-out infinite alternate; }
.sync-state.err .sync-dot { background: #ff5b4f; }
@keyframes pulse { to { opacity: 0.3; } }
@media (max-width: 480px) { .sync-label { display: none; } .bar-title { display: none; } }

.lib { height: 100%; display: flex; flex-direction: column; }
.tabs { display: flex; background: var(--bar); color: var(--bar-fg); padding: 0 6px; }
.tab { flex: 1; padding: 10px 6px; opacity: 0.7; border-bottom: 3px solid transparent; font-weight: 500; }
.tab.on { opacity: 1; border-bottom-color: #fff; }
.tabs.small { background: none; color: var(--fg); border-bottom: 1px solid var(--line); }
.tabs.small .tab.on { border-bottom-color: var(--accent); color: var(--accent); }
.lib-body { flex: 1; overflow-y: auto; padding: 12px 12px calc(30px + var(--safe-bottom)); -webkit-overflow-scrolling: touch; }
.sec { margin: 22px 4px 10px; font-size: 15px; color: var(--muted); font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; }

.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; margin: 0 0 10px; font-size: 14px; }
.crumb { padding: 4px 6px; border-radius: 6px; color: var(--accent); font-weight: 500; }
.crumb:last-child { color: var(--fg); }
.crumb-sep { color: var(--muted); }
.folders { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 8px; margin-bottom: 14px; }
.folder { display: flex; align-items: center; gap: 10px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; text-align: left; }
.folder-name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-n { color: var(--muted); font-size: 13px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 16px 12px; }
@media (min-width: 700px) { .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); } }
.card { min-width: 0; }
.cover-wrap { position: relative; display: block; width: 100%; aspect-ratio: 0.7; border-radius: 6px; overflow: hidden; background: var(--card); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18); }
.cover { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover.ph { display: flex; flex-direction: column; justify-content: space-between; padding: 12px 10px; color: #fff; text-align: left;
  background: linear-gradient(160deg, hsl(var(--hue) 35% 42%), hsl(calc(var(--hue) + 40) 40% 28%)); font-weight: 600; font-size: 13px; line-height: 1.25; overflow: hidden; word-break: break-word; }
.cover.ph small { opacity: 0.7; font-size: 11px; }
.prog { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; background: rgba(0, 0, 0, 0.35); }
.prog-in { height: 100%; background: var(--accent); }
.badge { position: absolute; top: 6px; right: 6px; font-size: 11px; font-weight: 700; color: #fff; border-radius: 10px; padding: 1px 6px; display: none; }
.badge.saved { display: block; background: rgba(30, 140, 60, 0.9); }
.badge.dl { display: block; background: rgba(0, 0, 0, 0.6); }
.card-info { padding: 6px 2px 0; }
.card-title { font-size: 13px; font-weight: 500; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; word-break: break-word; }
.card-meta { display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 2px; }

/* цитаты */
.quote { position: relative; background: var(--card); border-radius: 8px; padding: 10px 36px 8px 14px; margin: 8px 0; border-left: 5px solid var(--c-yellow); cursor: pointer; }
.quote.c-green { border-left-color: var(--c-green); } .quote.c-blue { border-left-color: var(--c-blue); }
.quote.c-pink { border-left-color: var(--c-pink); } .quote.c-orange { border-left-color: var(--c-orange); }
.quote-text { white-space: pre-wrap; word-break: break-word; }
.quote-note { margin-top: 6px; padding: 6px 8px; background: rgba(127, 127, 127, 0.1); border-radius: 6px; font-size: 14px; white-space: pre-wrap; }
.quote-meta { color: var(--muted); font-size: 12px; margin-top: 4px; }
.quote-more { position: absolute; top: 6px; right: 4px; }
.quote-book { cursor: pointer; color: var(--fg); }
.quotes-tools { display: flex; justify-content: flex-end; margin: 4px 0; }

/* шторки и меню */
.sheet-back { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: fade 0.15s; }
@keyframes fade { from { opacity: 0; } }
.sheet { background: var(--bg); color: var(--fg); width: 100%; max-width: 560px; max-height: 88vh; border-radius: 14px 14px 0 0; display: flex; flex-direction: column; padding-bottom: var(--safe-bottom); }
.sheet.wide { max-width: 720px; height: 86vh; }
@media (min-width: 760px) { .sheet-back { align-items: center; } .sheet { border-radius: 14px; } }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 10px 6px 6px 16px; }
.sheet-head b { flex: 1; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet > :not(.sheet-head) { overflow-y: auto; }
.menu { padding: 4px 12px 16px; display: flex; flex-direction: column; gap: 4px; }
.menu-item { text-align: left; padding: 12px; border-radius: 8px; background: var(--card); }
.menu-item.danger { color: var(--danger); }
.menu-note { color: var(--muted); font-size: 13px; padding: 4px 4px 8px; word-break: break-word; }
.menu-label { color: var(--muted); font-size: 13px; margin: 10px 4px 2px; }
.menu-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 4px; border-bottom: 1px solid var(--line); }
.seg { display: flex; gap: 4px; background: var(--card); border-radius: 10px; padding: 3px; }
.seg-btn { flex: 1; padding: 9px 4px; border-radius: 8px; text-align: center; }
.seg-btn.on { background: var(--accent); color: #fff; }
.seg-btn.static { display: flex; align-items: center; justify-content: center; }
.form { padding: 4px 16px 16px; display: flex; flex-direction: column; gap: 10px; }
.row-btns { display: flex; gap: 8px; justify-content: flex-end; padding: 6px 16px 16px; }
.form .row-btns { padding: 0; }
.colors { display: flex; gap: 12px; padding: 10px 4px; }

#toasts { position: fixed; left: 0; right: 0; bottom: calc(70px + var(--safe-bottom)); z-index: 60; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast { pointer-events: auto; background: rgba(30, 30, 30, 0.92); color: #fff; padding: 10px 14px; border-radius: 10px; max-width: min(92vw, 520px); display: flex; gap: 12px; align-items: center; font-size: 14px; transition: opacity 0.4s; }
.toast.out { opacity: 0; }
.toast-btn { color: #7fb8ff; font-weight: 600; white-space: nowrap; }

/* ── чтение ─────────────────────────────────────────────────────────── */
.reader { position: fixed; inset: 0; background: var(--page-bg); overflow: hidden; }
.r-body { position: absolute; inset: 0; }
.r-top, .r-bottom { position: absolute; left: 0; right: 0; z-index: 10; background: var(--bar); color: var(--bar-fg); transition: transform 0.18s; }
.r-top { top: 0; display: flex; align-items: center; gap: 2px; padding: calc(4px + var(--safe-top)) 4px 4px; transform: translateY(-100%); }
.r-bottom { bottom: 0; padding: 8px 14px calc(8px + var(--safe-bottom)); transform: translateY(100%); }
.reader.bars .r-top, .reader.bars .r-bottom { transform: none; }
.r-title { flex: 1; min-width: 0; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 6px; }
.icon-btn.mini { min-width: 24px; }
.bm-btn { filter: grayscale(1) opacity(0.6); }
.bm-btn.on { filter: none; }
.r-slider { width: 100%; accent-color: #fff; }
.r-pos { display: flex; justify-content: space-between; font-size: 13px; opacity: 0.85; }
.r-mini { position: absolute; right: 8px; bottom: calc(4px + var(--safe-bottom)); z-index: 5; font-size: 11px; color: var(--muted); background: var(--bg); opacity: 0.8; padding: 1px 6px; border-radius: 8px; pointer-events: none; }
.reader.bars .r-mini { display: none; }
.r-loading { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; color: var(--muted); z-index: 3; text-align: center; padding: 20px; }
.r-error { position: absolute; left: 50%; top: 40%; transform: translate(-50%, -50%); z-index: 20; background: var(--card); padding: 18px; border-radius: 12px; text-align: center; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3); max-width: 90vw; }

.pdf-scroll { position: absolute; inset: 0; overflow: auto; -webkit-overflow-scrolling: touch; touch-action: pan-x pan-y; }
.pdf-inner { position: relative; }
.pdf-page { position: absolute; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); overflow: hidden; }
.pdf-num { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #bbb; font-size: 28px; }
.pdf-canvas { position: absolute; inset: 0; z-index: 1; width: 100%; height: 100%; display: block; }
.pdf-scroll[data-theme=dark] .pdf-page { background: #222; }
.pdf-scroll[data-theme=dark] .pdf-canvas { filter: invert(0.9) hue-rotate(180deg); }
.pdf-scroll[data-theme=sepia] .pdf-canvas { filter: sepia(0.35) brightness(0.96); }
.hl-layer { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
.hl { position: absolute; mix-blend-mode: multiply; opacity: 0.45; border-radius: 2px; background: var(--c-yellow); }
.pdf-scroll[data-theme=dark] .hl { mix-blend-mode: screen; opacity: 0.35; }
.hl.c-green { background: var(--c-green); } .hl.c-blue { background: var(--c-blue); } .hl.c-pink { background: var(--c-pink); } .hl.c-orange { background: var(--c-orange); }
.hl-note-mark { position: absolute; font-size: 12px; color: #b07a00; }
.bm-flag { position: absolute; top: 0; right: 18px; width: 16px; height: 26px; background: #e53935; clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 75%, 0 100%); }

/* слой текста PDF.js (из pdf_viewer.css) */
.textLayer { position: absolute; text-align: initial; inset: 0; overflow: clip; opacity: 1; line-height: 1; text-size-adjust: none; -webkit-text-size-adjust: none; forced-color-adjust: none; transform-origin: 0 0; caret-color: CanvasText; z-index: 1; }
.textLayer :is(span, br) { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer > :not(.markedContent), .textLayer .markedContent span:not(.markedContent) { z-index: 1; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(0, 100, 255, 0.3); }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: 0; cursor: default; user-select: none; }
.selecting .textLayer .endOfContent { top: 0; }
.textLayer span.hit { background: rgba(255, 140, 0, 0.35); border-radius: 2px; }

.epub-box { position: absolute; inset: 0; padding: calc(10px + var(--safe-top)) 0 calc(18px + var(--safe-bottom)); background: #fff; }
.epub-box[data-theme=sepia] { background: #f4ecd8; }
.epub-box[data-theme=dark] { background: #121212; }

.sel-menu { position: fixed; z-index: 40; display: flex; align-items: center; gap: 6px; background: #2b2b2b; color: #fff; padding: 6px 8px; border-radius: 22px; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35); user-select: none; -webkit-user-select: none; }
.dot { width: 28px; height: 28px; border-radius: 50%; background: var(--c-yellow); border: 2px solid rgba(255, 255, 255, 0.8); }
.colors .dot { width: 36px; height: 36px; border-color: transparent; }
.dot.on { outline: 3px solid var(--accent); outline-offset: 2px; }
.dot.c-green { background: var(--c-green); } .dot.c-blue { background: var(--c-blue); } .dot.c-pink { background: var(--c-pink); } .dot.c-orange { background: var(--c-orange); }
.sel-btn { padding: 6px 8px; font-size: 14px; font-weight: 500; }

.panel { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.panel-body { overflow-y: auto; padding: 6px 12px 20px; flex: 1; }
.sheet.wide > .panel { overflow: hidden; }
.toc-row { display: flex; justify-content: space-between; gap: 10px; width: 100%; text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.toc-row small { color: var(--muted); }
.bm-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 6px; border-bottom: 1px solid var(--line); cursor: pointer; }
.bm-row small { color: var(--muted); }
.bm-snip { color: var(--muted); font-size: 13px; }
.find-bar { display: flex; gap: 8px; margin: 6px 0; }
.find-row { display: block; width: 100%; text-align: left; padding: 9px 6px; border-bottom: 1px solid var(--line); font-size: 14px; }
.find-row small { display: block; color: var(--muted); }
.find-row mark { background: rgba(255, 200, 0, 0.6); color: inherit; }
