/* ---------- темы ---------- */
:root, [data-theme="light"] {
  --bg: #ffffff; --fg: #1f2328; --muted: #656d76; --panel: #f6f8fa; --hover: #eaeef2;
  --border: #d0d7de; --accent: #0969da; --accent-fg: #ffffff; --code: #f3f4f6; --sel: #ddf4ff;
  --danger: #cf222e; --shadow: 0 8px 24px rgba(140,149,159,.25);
  color-scheme: light;
}
[data-theme="sepia"] {
  --bg: #f4ecd8; --fg: #4b3a2a; --muted: #8a7357; --panel: #ece1c6; --hover: #e3d5b4;
  --border: #d7c6a2; --accent: #a0522d; --accent-fg: #fff8ec; --code: #eadfc3; --sel: #e6d3a8;
  --danger: #b03a2e; --shadow: 0 8px 24px rgba(91,70,54,.2);
  color-scheme: light;
}
[data-theme="solarized"] {
  --bg: #fdf6e3; --fg: #586e75; --muted: #93a1a1; --panel: #eee8d5; --hover: #e6dfc8;
  --border: #d9d2bc; --accent: #268bd2; --accent-fg: #fdf6e3; --code: #eee8d5; --sel: #e2ecd9;
  --danger: #dc322f; --shadow: 0 8px 24px rgba(88,110,117,.2);
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #0d1117; --fg: #e6edf3; --muted: #8d96a0; --panel: #161b22; --hover: #1f2630;
  --border: #30363d; --accent: #4493f8; --accent-fg: #0d1117; --code: #161b22; --sel: #1c2d44;
  --danger: #f85149; --shadow: 0 8px 24px rgba(1,4,9,.6);
  color-scheme: dark;
}
[data-theme="black"] {
  --bg: #000000; --fg: #cfcfcf; --muted: #7a7a7a; --panel: #0b0b0b; --hover: #161616;
  --border: #242424; --accent: #8ab4f8; --accent-fg: #000; --code: #111111; --sel: #1a2433;
  --danger: #ff6b6b; --shadow: 0 8px 24px rgba(0,0,0,.8);
  color-scheme: dark;
}
[data-theme="nord"] {
  --bg: #2e3440; --fg: #d8dee9; --muted: #8b95a7; --panel: #3b4252; --hover: #434c5e;
  --border: #4c566a; --accent: #88c0d0; --accent-fg: #2e3440; --code: #3b4252; --sel: #434c5e;
  --danger: #bf616a; --shadow: 0 8px 24px rgba(0,0,0,.4);
  color-scheme: dark;
}
[data-theme="gruvbox"] {
  --bg: #282828; --fg: #ebdbb2; --muted: #a89984; --panel: #32302f; --hover: #3c3836;
  --border: #504945; --accent: #fabd2f; --accent-fg: #282828; --code: #32302f; --sel: #45403d;
  --danger: #fb4934; --shadow: 0 8px 24px rgba(0,0,0,.5);
  color-scheme: dark;
}

:root {
  --reader-size: 18px;
  --reader-lh: 1.6;
  --reader-width: 800px;
  --reader-font: var(--font-sans);
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, sans-serif;
  --font-serif: "Literata", "PT Serif", "Noto Serif", Georgia, "Times New Roman", serif;
  --font-mono: ui-monospace, "JetBrains Mono", "Cascadia Code", "Fira Code", Menlo, Consolas, monospace;
  --topbar: 52px;
  --sidebar: 300px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 14px/1.5 var(--font-sans);
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; color: inherit; cursor: pointer; }
::selection { background: var(--sel); }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- вход ---------- */
.login-body { display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; padding: 32px 28px; text-align: center;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
}
.login-logo { color: var(--accent); }
.login-logo svg { width: 40px; height: 40px; stroke-width: 1.6; }
.login-card h1 { font-size: 20px; margin: 8px 0 20px; font-weight: 600; }
.login-card input {
  width: 100%; padding: 10px 12px; font: inherit; font-size: 15px; color: var(--fg);
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; outline: none;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.login-card button[type=submit] {
  width: 100%; margin-top: 12px; padding: 10px; border: 0; border-radius: 8px;
  background: var(--accent); color: var(--accent-fg); font-weight: 600; font-size: 15px;
}
.login-error { color: var(--danger); margin: 12px 0 0; }

/* ---------- каркас ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar); z-index: 30;
  display: flex; align-items: center; gap: 6px; padding: 0 10px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.topbar-right { margin-left: auto; display: flex; gap: 2px; }
.topbar form { margin: 0; }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; padding: 0;
  background: none; border: 0; border-radius: 8px; color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--fg); }
.icon-btn.active { color: var(--accent); }

.crumbs { display: flex; align-items: center; min-width: 0; overflow: hidden; white-space: nowrap; font-size: 14px; }
.crumbs a, .crumbs span { overflow: hidden; text-overflow: ellipsis; padding: 4px 6px; border-radius: 6px; color: var(--muted); }
.crumbs a:hover { background: var(--hover); color: var(--fg); text-decoration: none; }
.crumbs .sep { color: var(--border); padding: 0 1px; flex: none; }
.crumbs .cur { color: var(--fg); font-weight: 600; }
.crumbs .home { display: inline-flex; flex: none; }

.layout { padding-top: var(--topbar); height: 100%; }
#sidebar {
  position: fixed; top: var(--topbar); bottom: 0; left: 0; width: var(--sidebar); z-index: 20;
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--panel); border-right: 1px solid var(--border);
  transition: transform .2s ease;
}
body.sidebar-hidden #sidebar { transform: translateX(-100%); }
#main { margin-left: var(--sidebar); min-height: 100%; transition: margin-left .2s ease; }
body.sidebar-hidden #main { margin-left: 0; }
#backdrop { display: none; }

@media (max-width: 860px) {
  #main { margin-left: 0; }
  #sidebar { width: min(86vw, 340px); box-shadow: var(--shadow); }
  body:not(.sidebar-open) #sidebar { transform: translateX(-100%); }
  body.sidebar-open #backdrop { display: block; position: fixed; inset: var(--topbar) 0 0 0; z-index: 15; background: rgba(0,0,0,.35); }
}

/* ---------- боковая панель ---------- */
.search-wrap { position: relative; padding: 10px; border-bottom: 1px solid var(--border); }
.search-wrap svg { position: absolute; left: 20px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 16px; height: 16px; }
#search {
  width: 100%; padding: 7px 10px 7px 32px; font: inherit; color: var(--fg);
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; outline: none;
}
#search:focus { border-color: var(--accent); }

.tree { list-style: none; margin: 0; padding: 6px; overflow: auto; flex: 1; }
.tree ul { list-style: none; margin: 0; padding: 0 0 0 14px; }
.tree .row {
  display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 6px;
  color: var(--fg); cursor: pointer; user-select: none; text-decoration: none; min-width: 0;
}
.tree .row:hover { background: var(--hover); text-decoration: none; }
.tree .row.active { background: var(--sel); font-weight: 600; }
.tree .row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .row .sub { display: block; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .caret { width: 14px; height: 14px; color: var(--muted); transition: transform .15s; }
.tree li.open > .row .caret { transform: rotate(90deg); }
.tree .caret.none { visibility: hidden; }
.tree .ico { color: var(--muted); width: 16px; height: 16px; }
.tree .dir > .row .ico { color: var(--accent); }
.tree .empty { color: var(--muted); padding: 4px 8px 4px 28px; font-size: 13px; }

/* ---------- контент ---------- */
.page { padding: 28px 24px 80px; }
.page-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; max-width: 1100px; margin: 0 auto 18px; }
.page-head h1 { font-size: 22px; margin: 0; font-weight: 600; word-break: break-word; }
.page-head .actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; font-size: 13px;
  background: var(--panel); color: var(--fg); border: 1px solid var(--border); border-radius: 8px;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--hover); text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn svg { width: 16px; height: 16px; }

.listing { max-width: 1100px; margin: 0 auto; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.listing .item { display: flex; align-items: center; gap: 12px; padding: 9px 14px; border-top: 1px solid var(--border); }
.listing .item:first-child { border-top: 0; }
.listing .item:hover { background: var(--panel); }
.listing .item .ico { color: var(--muted); }
.listing .item.dir .ico { color: var(--accent); }
.listing .item a.name { color: var(--fg); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.listing .meta { color: var(--muted); font-size: 13px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.listing .meta.size { width: 80px; text-align: right; }
.listing .meta.date { width: 130px; text-align: right; }
.listing .tools { display: flex; gap: 2px; opacity: 0; }
.listing .item:hover .tools, .listing .item:focus-within .tools { opacity: 1; }
.listing .tools .icon-btn { width: 30px; height: 30px; }
.listing .tools .icon-btn.del:hover { color: var(--danger); }
@media (hover: none) { .listing .tools { opacity: 1; } }
@media (max-width: 640px) {
  .listing .meta.date { display: none; }
  .page { padding: 18px 16px 60px; }
}
.empty-state { max-width: 1100px; margin: 0 auto; padding: 48px 16px; text-align: center; color: var(--muted); border: 2px dashed var(--border); border-radius: 12px; }

/* ---------- читалка ---------- */
.reader-bar {
  position: sticky; top: var(--topbar); z-index: 10; display: flex; align-items: center; gap: 6px;
  padding: 6px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.reader-bar .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.reader-bar .progress { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

.doc {
  max-width: var(--reader-width); margin: 0 auto; padding: 32px 24px 120px;
  font-family: var(--reader-font); font-size: var(--reader-size); line-height: var(--reader-lh);
  overflow-wrap: break-word;
}
@media (max-width: 640px) { .doc { padding: 20px 16px 100px; } }
.doc h1, .doc h2, .doc h3, .doc h4 { line-height: 1.25; margin: 1.6em 0 .6em; font-weight: 650; }
.doc h1 { font-size: 1.9em; }
.doc h2 { font-size: 1.5em; padding-bottom: .25em; border-bottom: 1px solid var(--border); }
.doc h3 { font-size: 1.25em; }
.doc > :first-child { margin-top: 0; }
.doc [id] { scroll-margin-top: calc(var(--topbar) + 64px); }
.doc p, .doc ul, .doc ol, .doc blockquote, .doc pre, .doc table { margin: 0 0 1em; }
.doc img, .doc video { max-width: 100%; height: auto; border-radius: 4px; }
.doc blockquote { margin-left: 0; padding: .2em 1em; color: var(--muted); border-left: 4px solid var(--border); }
.doc code { font-family: var(--font-mono); font-size: .86em; background: var(--code); padding: .15em .35em; border-radius: 5px; }
.doc pre { background: var(--code); padding: 14px 16px; border-radius: 8px; overflow: auto; line-height: 1.45; border: 1px solid var(--border); }
.doc pre code { background: none; padding: 0; font-size: .82em; }
.doc pre code.hljs { background: none; padding: 0; }
.doc table { border-collapse: collapse; display: block; overflow: auto; max-width: 100%; font-size: .9em; }
.doc th, .doc td { border: 1px solid var(--border); padding: 6px 12px; }
.doc th { background: var(--panel); }
.doc hr { border: 0; border-top: 1px solid var(--border); margin: 2em 0; }
.doc ul > li:has(> input[type=checkbox]), .doc ul > li:has(> p > input[type=checkbox]) { list-style: none; }
.doc ul > li > input[type=checkbox], .doc ul > li > p > input[type=checkbox] { margin: 0 .5em 0 -1.4em; vertical-align: middle; }
.doc ol > li > input[type=checkbox] { margin: 0 .4em 0 0; vertical-align: middle; }
.doc del { opacity: .7; }
.doc details { margin: 0 0 1em; padding: .5em .8em; border: 1px solid var(--border); border-radius: 8px; }
.doc summary { cursor: pointer; font-weight: 600; }
.doc .footnotes { margin-top: 3em; padding-top: 1em; border-top: 1px solid var(--border); font-size: .88em; color: var(--muted); }
.doc sup a[data-footnote-ref] { text-decoration: none; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.doc a:not([href]) { color: inherit; }

.doc.plain { white-space: pre-wrap; }
.doc.code { max-width: none; font-size: calc(var(--reader-size) * .8); }
.doc.code pre { margin: 0; }

/* FB2 */
.fb2 .book-head { text-align: center; margin-bottom: 2.5em; }
.fb2 .book-head img { max-height: 60vh; box-shadow: var(--shadow); }
.fb2 .book-head .author { color: var(--muted); font-size: 1.05em; margin-top: 1em; }
.fb2 .book-head h1 { margin-top: .3em; border: 0; }
.fb2 section { margin-bottom: 1.5em; }
.fb2 p { margin: 0; text-indent: 1.5em; text-align: justify; hyphens: auto; }
.fb2 .title p, .fb2 .subtitle, .fb2 .epigraph p, .fb2 .poem p, .fb2 .author-line { text-indent: 0; text-align: inherit; }
.fb2 .title { text-align: center; margin: 2em 0 1em; font-weight: 650; font-size: 1.3em; line-height: 1.3; }
.fb2 .subtitle { text-align: center; font-weight: 600; margin: 1em 0; }
.fb2 .epigraph { margin: 1em 0 1.5em 30%; font-style: italic; color: var(--muted); }
.fb2 .poem { margin: 1em 0 1em 2em; }
.fb2 .stanza { margin-bottom: 1em; }
.fb2 .author-line { text-align: right; font-style: italic; }
.fb2 .notes { margin-top: 4em; font-size: .9em; border-top: 1px solid var(--border); padding-top: 1em; }
.fb2 img { display: block; margin: 1em auto; }

/* EPUB */
.epub-wrap { position: relative; height: calc(100vh - var(--topbar) - 49px); height: calc(100dvh - var(--topbar) - 49px); }
.epub-view { position: absolute; inset: 0 56px; }
.epub-nav {
  position: absolute; top: 0; bottom: 0; width: 56px; display: grid; place-items: center;
  background: none; border: 0; color: var(--muted); opacity: .6;
}
.epub-nav:hover { opacity: 1; background: var(--panel); }
.epub-nav.prev { left: 0; } .epub-nav.next { right: 0; }
.epub-nav svg { width: 26px; height: 26px; }
@media (max-width: 640px) { .epub-view { inset: 0 32px; } .epub-nav { width: 32px; } .epub-nav svg { width: 20px; height: 20px; } }

.frame { display: block; width: 100%; height: calc(100vh - var(--topbar) - 49px); height: calc(100dvh - var(--topbar) - 49px); border: 0; background: #fff; }
.media { display: grid; place-items: center; padding: 24px; }
.media img, .media video { max-width: 100%; max-height: calc(100vh - 160px); border-radius: 6px; }
.media audio { width: min(600px, 100%); }
.notice { max-width: 560px; margin: 60px auto; text-align: center; color: var(--muted); padding: 0 16px; }
.notice .btn { margin-top: 12px; }
.loading { padding: 60px; text-align: center; color: var(--muted); }

/* ---------- всплывающие окна ---------- */
.popover {
  position: fixed; z-index: 50; top: calc(var(--topbar) + 6px); right: 10px; width: 300px; max-width: calc(100vw - 20px);
  padding: 14px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
}
.popover.menu { padding: 6px; width: auto; min-width: 240px; max-height: 70vh; overflow: auto; }
.menu a { display: block; padding: 6px 10px; border-radius: 6px; color: var(--fg); font-size: 13px; }
.menu a:hover { background: var(--hover); text-decoration: none; }
.menu a.l2 { padding-left: 22px; } .menu a.l3 { padding-left: 34px; color: var(--muted); }
.set-title { font-size: 12px; color: var(--muted); margin: 12px 0 6px; text-transform: uppercase; letter-spacing: .04em; }
.set-title:first-child { margin-top: 0; }
.themes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.themes button {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 2px 4px; font-size: 11px;
  background: none; border: 2px solid transparent; border-radius: 8px; color: var(--muted);
}
.themes button.on { border-color: var(--accent); color: var(--fg); }
.themes .sw { width: 34px; height: 24px; border-radius: 6px; border: 1px solid var(--border); display: grid; place-items: center; font: 600 12px/1 serif; }
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { flex: 1; padding: 6px 4px; font-size: 12px; background: none; border: 0; border-left: 1px solid var(--border); }
.seg button:first-child { border-left: 0; }
.seg button.on { background: var(--accent); color: var(--accent-fg); }
.set-row { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.set-row span { flex: 1; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.set-row output { min-width: 44px; text-align: center; font-variant-numeric: tabular-nums; }
.step { padding: 4px 12px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; }

#drop-overlay {
  position: fixed; inset: 0; z-index: 80; display: grid; place-items: center; pointer-events: none;
  background: color-mix(in srgb, var(--accent) 18%, transparent); border: 3px dashed var(--accent);
}
#drop-overlay > div { background: var(--panel); padding: 16px 24px; border-radius: 12px; box-shadow: var(--shadow); font-size: 16px; }
#drop-overlay[hidden] { display: none; }

#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 90; display: flex; flex-direction: column; gap: 8px; max-width: calc(100vw - 32px); }
.toast { min-width: 240px; padding: 10px 14px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); }
.toast.err { border-color: var(--danger); color: var(--danger); }
.toast .bar { height: 4px; margin-top: 8px; background: var(--hover); border-radius: 2px; overflow: hidden; }
.toast .bar > i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .15s; }
