:root {
  --bg: #fbf8f3; --fg: #2b2622; --muted: #8a8177; --card: #ffffff; --line: #e8e0d4;
  --accent: #d98a1f; --accent-fg: #fff; --danger: #c0392b; --shadow: 0 6px 24px rgba(40, 30, 10, 0.14);
  --kb: 0px;
}
body[data-theme="sepia"] { --bg: #f1e6cf; --fg: #4a3b27; --muted: #8b7a5f; --card: #f8f0de; --line: #dccfb0; }
body[data-theme="dark"] { --bg: #1c1b1a; --fg: #d8d4cc; --muted: #8f8a82; --card: #272624; --line: #3a3835; --shadow: 0 6px 24px rgba(0,0,0,.5); }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--fg);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior: none; -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
h1 { font-size: 20px; margin: 0; }
button { font: inherit; color: inherit; }
input, textarea { font: inherit; font-size: 16px; color: var(--fg); }

.view { position: fixed; inset: 0; display: flex; flex-direction: column; background: var(--bg); }
.topbar {
  display: flex; align-items: center; gap: 4px; padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  border-bottom: 1px solid var(--line); background: var(--bg); min-height: 52px;
}
.icon-btn { background: none; border: 0; width: 40px; height: 40px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; position: relative; color: var(--fg); }
.icon-btn:active { background: var(--line); }
.icon-btn.small { width: 32px; height: 32px; font-size: 18px; }
.ic svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; display: block; }
.aa { font-weight: 700; font-size: 17px; letter-spacing: -1px; }
.btn { border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 10px 14px; cursor: pointer; }
.btn:active { filter: brightness(0.95); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn.primary:disabled { opacity: 0.45; }
.btn.wide { width: 100%; }
.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 6px 8px; font-size: 14px; }
.error { color: var(--danger); font-size: 14px; margin: 8px 0; }

/* Вход */
#view-login { align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-card .logo { font-size: 44px; }
.login-card input { padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); }

/* Библиотека */
.who { color: var(--muted); font-size: 14px; margin-right: 4px; }
.library { flex: 1; overflow-y: auto; padding: 14px 14px calc(96px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 12px; -webkit-overflow-scrolling: touch; }
.empty { text-align: center; color: var(--muted); margin: 60px 20px; line-height: 1.6; }
.book { display: flex; gap: 14px; background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 12px; cursor: pointer; position: relative; }
.book:active { filter: brightness(0.97); }
.cover { width: 72px; height: 104px; border-radius: 6px; object-fit: cover; flex: none; box-shadow: 0 2px 8px rgba(0,0,0,.18); background: var(--line); }
.cover.ph { display: flex; align-items: center; justify-content: center; color: #fff; font-size: 34px; font-weight: 700; font-family: Georgia, serif; }
.book-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.book-title { font-weight: 650; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; padding-right: 26px; }
.book-author { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.prog { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); }
.prog .name { width: 84px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: none; }
.prog .pbar { flex: 1; height: 5px; border-radius: 3px; background: var(--line); overflow: hidden; }
.prog .pbar i { display: block; height: 100%; border-radius: 3px; }
.prog .pct { width: 34px; text-align: right; flex: none; }
.new-badge { align-self: flex-start; background: var(--accent); color: #fff; font-size: 12px; border-radius: 10px; padding: 2px 8px; margin-top: 2px; }
.book .more { position: absolute; top: 4px; right: 4px; }
.fab { position: fixed; right: 18px; bottom: calc(22px + env(safe-area-inset-bottom)); width: 58px; height: 58px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 32px; display: flex; align-items: center; justify-content: center; box-shadow: var(--shadow); cursor: pointer; }
.upload-status { padding: 8px 14px; font-size: 14px; background: var(--card); border-bottom: 1px solid var(--line); }
.upload-status .ubar { height: 4px; background: var(--line); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.upload-status .ubar i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }

/* Ридер */
.reader-top { gap: 0; padding-left: 4px; padding-right: 4px; }
.r-title { flex: 1; min-width: 0; text-align: center; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 4px; }
.badge { position: absolute; top: 2px; right: 0; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; }
#stage { position: relative; flex: 1; min-height: 0; }
#viewer { position: absolute; inset: 0; }
.r-loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--bg); color: var(--muted); }
.sel-bar { position: absolute; left: 10px; right: 10px; bottom: 10px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); display: flex; align-items: center; gap: 8px; padding: 8px 8px 8px 12px; z-index: 5; }
.sel-quote { flex: 1; min-width: 0; font-size: 13px; color: var(--muted); font-style: italic; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.reader-bottom { padding: 8px 14px calc(env(safe-area-inset-bottom) + 8px); border-top: 1px solid var(--line); background: var(--bg); }
.bar { height: 34px; position: relative; cursor: pointer; touch-action: manipulation; }
.track { position: absolute; left: 0; right: 0; top: 14px; height: 6px; border-radius: 3px; background: var(--line); }
.fill, .maxfill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 3px; width: 0; }
.fill { background: var(--accent); }
.maxfill { background: var(--accent); opacity: 0.3; }
.pmark { position: absolute; top: -13px; width: 20px; height: 20px; margin-left: -10px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg); border: 2px solid var(--bg); display: flex; align-items: center; justify-content: center; }
.pmark span { transform: rotate(45deg); color: #fff; font-size: 10px; font-weight: 700; line-height: 1; }
.cmark { position: absolute; top: -2px; width: 10px; height: 10px; margin-left: -5px; border-radius: 50%; border: 2px solid var(--bg); cursor: pointer; padding: 0; }
.cmark::before { content: ""; position: absolute; inset: -9px; }
.cmark.locked { background: var(--bg) !important; border: 2px solid; }
.cmark.unseen { box-shadow: 0 0 0 3px rgba(217, 138, 31, .35); }
.info { display: flex; align-items: center; gap: 10px; font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.pos { flex: none; max-width: 52%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.partners { flex: 1; text-align: right; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.partners button { background: none; border: 0; color: inherit; padding: 2px 0; cursor: pointer; text-decoration: underline dotted; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: 0; }

/* Шиты */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; }
.backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.38); }
.sheet {
  position: absolute; left: 0; right: 0; bottom: var(--kb); max-height: calc(100% - var(--kb) - 40px); overflow-y: auto;
  background: var(--bg); border-radius: 20px 20px 0 0; padding: 14px 16px calc(env(safe-area-inset-bottom) + 16px);
  box-shadow: var(--shadow); max-width: 640px; margin: 0 auto; animation: up .18s ease-out;
}
.sheet.tall { height: 82%; display: flex; flex-direction: column; }
@keyframes up { from { transform: translateY(40px); } to { transform: none; } }
.sheet-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.sheet-head b { font-size: 17px; flex: 1; }
.sheet-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 12px; }
.who-line { flex: 1; font-weight: 600; }
.quote { margin: 0 0 12px; padding: 8px 12px; border-left: 3px solid var(--accent); color: var(--muted); font-style: italic; font-size: 14.5px; max-height: 120px; overflow: auto; background: var(--card); border-radius: 0 8px 8px 0; }
.quote:empty { display: none; }
.cbody { white-space: pre-wrap; word-break: break-word; font-size: 16.5px; line-height: 1.45; }
textarea { width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); resize: none; }
.row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--line); }
.seg { display: inline-flex; gap: 6px; align-items: center; }
.seg .btn { padding: 8px 12px; }
.seg .btn.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.fs-val { min-width: 48px; text-align: center; color: var(--muted); }
.stack { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.hint { font-size: 13px; color: var(--muted); line-height: 1.4; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 16px; padding: 5px 12px; font-size: 14px; cursor: pointer; }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.clist { flex: 1; overflow-y: auto; margin: 0 -4px; padding: 0 4px; -webkit-overflow-scrolling: touch; }
.chap-h { position: sticky; top: 0; background: var(--bg); font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 0 4px; z-index: 1; }
.citem { border: 1px solid var(--line); background: var(--card); border-radius: 12px; padding: 10px 12px; margin: 6px 0; position: relative; cursor: pointer; }
.citem.flash { animation: flash 1.4s ease-out; }
@keyframes flash { 0% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 0 0 0 transparent; } }
.citem .meta { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 4px; margin-bottom: 4px; }
.citem .meta b { color: var(--fg); font-weight: 600; }
.citem .q { font-style: italic; color: var(--muted); font-size: 13.5px; border-left: 2px solid var(--line); padding-left: 8px; margin-bottom: 4px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.citem .b { white-space: pre-wrap; word-break: break-word; }
.citem.unseen::after { content: ""; position: absolute; top: 12px; right: 12px; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); }
.citem.lockedc { background: transparent; border-style: dashed; }
.citem .lockrow { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.citem .lockrow .btn { margin-left: auto; padding: 5px 12px; font-size: 14px; flex: none; }
.citem .del { position: absolute; right: 6px; bottom: 4px; }
.toc-list { overflow-y: auto; flex: 1; }
.toc-item { display: block; width: 100%; text-align: left; background: none; border: 0; border-bottom: 1px solid var(--line); padding: 12px 4px; cursor: pointer; }
.toc-item.cur { color: var(--accent); font-weight: 650; }
.toc-item.sub { padding-left: 22px; font-size: 15px; }

.toast { position: fixed; left: 50%; transform: translateX(-50%); top: calc(env(safe-area-inset-top) + 62px); z-index: 80; background: #2b2622; color: #fff; padding: 10px 14px; border-radius: 12px; font-size: 14px; display: flex; gap: 12px; align-items: center; max-width: 90%; box-shadow: var(--shadow); }
.toast button { background: none; border: 0; color: #ffc46b; font-weight: 600; padding: 0; cursor: pointer; }

/* --- выбор текста: две кнопки --- */
.sel-bar { flex-direction: column; align-items: stretch; gap: 8px; padding: 10px 10px 10px 12px; }
.sel-quote { flex: none; }
.sel-actions { display: flex; align-items: center; gap: 8px; }
.sel-actions .btn { flex: 1; padding: 10px 8px; white-space: nowrap; }
.sel-actions .icon-btn { flex: none; }

/* --- лист комментария с веткой --- */
.cv-scroll { flex: 1; overflow-y: auto; min-height: 0; -webkit-overflow-scrolling: touch; }
.mini-actions { display: flex; gap: 14px; margin: 6px 0 2px; }
.mini-actions button, .rhead button { background: none; border: 0; color: var(--accent); font-size: 14px; padding: 4px 0; cursor: pointer; }
.mini-actions button.danger, .rhead button.danger { color: var(--danger); }
.cmeta { font-weight: 400; color: var(--muted); font-size: 13px; }
.hl-note { color: var(--muted); font-style: italic; }
.thread { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.reply { border-left: 3px solid var(--line); padding: 4px 0 4px 10px; }
.reply .rhead { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.reply .rhead b { color: var(--fg); font-weight: 600; }
.reply .rbody { white-space: pre-wrap; word-break: break-word; margin-top: 2px; }
.reply.unseen .rhead::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.composer { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 8px; }
.composer-mode { display: flex; align-items: center; justify-content: space-between; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer-row textarea { flex: 1; max-height: 140px; }
.composer-row .btn { width: 46px; height: 46px; padding: 0; font-size: 20px; flex: none; }
.pending-tag { font-size: 12px; color: var(--muted); }
.citem .replies { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.citem .reply { font-size: 14.5px; }
.citem .foot { display: flex; gap: 16px; margin-top: 8px; }
.citem .foot button { background: none; border: 0; color: var(--accent); font-size: 14px; padding: 2px 0; cursor: pointer; }
.citem.hlonly .b { color: var(--muted); font-style: italic; font-size: 13.5px; }
.tag { font-size: 11.5px; border: 1px solid var(--line); border-radius: 8px; padding: 0 6px; color: var(--muted); }
.cmark.hl { width: 8px; height: 8px; margin-left: -4px; top: -1px; opacity: .75; }

/* --- сеть, офлайн-метки --- */
.net-banner { position: fixed; left: 0; right: 0; top: calc(env(safe-area-inset-top) + 54px); z-index: 45; text-align: center; font-size: 12.5px; background: #8a6d3b; color: #fff; padding: 3px 10px; pointer-events: none; }
.chip-off { align-self: flex-start; font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 10px; padding: 1px 8px; background: transparent; cursor: pointer; margin-top: 2px; }
.chip-off.ok { color: #2f8a5f; border-color: #2f8a5f55; cursor: default; }
.check { display: flex; align-items: center; gap: 10px; padding: 8px 0; font-size: 16px; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.status { padding: 10px 12px; border-radius: 12px; background: var(--card); border: 1px solid var(--line); }
