:root {
  --bg: #0f0f12;
  --bg2: #17171c;
  --card: #1c1c22;
  --text: #e8e6e3;
  --muted: #9a98a0;
  --accent: #3d7bff;
  --line: #2a2a32;
  --bar: rgba(15, 15, 18, .82);
  --fs: 19px;
  --lh: 1.8;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
[data-theme="light"] {
  --bg: #f6f4ef; --bg2: #ece9e2; --card: #ffffff; --text: #1d1d1f;
  --muted: #6b6b72; --accent: #2f5fd0; --line: #dcd8cf; --bar: rgba(246, 244, 239, .85);
}
[data-theme="sepia"] {
  --bg: #f1e6d0; --bg2: #e8dcc2; --card: #f7eedb; --text: #3b2f1e;
  --muted: #7a6a52; --accent: #9b5a1a; --line: #d8c9a6; --bar: rgba(241, 230, 208, .86);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; color-scheme: dark; }
[data-theme="light"], [data-theme="sepia"] { color-scheme: light; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  line-height: 1.5;
  transition: background .3s, color .3s;
}
a { color: inherit; text-decoration: none; }
button, input { font: inherit; color: inherit; }
button { -webkit-tap-highlight-color: transparent; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
h1, h2 { line-height: 1.2; }

.muted { color: var(--muted); }
.center { text-align: center; }
.pad { padding: 80px 16px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- анімації ---------- */
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
.view { animation: fade .3s ease both; }
.view.still { animation: none; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* ---------- шапка ---------- */
.wrap { max-width: 1000px; margin: 0 auto; padding: 0 16px 64px; }
.site-head {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--bar);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; font-size: 1.25rem; letter-spacing: .3px; color: var(--accent); }
.icon {
  display: inline-grid; place-items: center; width: 44px; height: 44px;
  border-radius: 50%; background: none; border: 0; cursor: pointer;
  transition: background .2s, transform .15s;
}
.icon:hover { background: var(--bg2); }
.icon:active { transform: scale(.92); }
.icon.disabled { opacity: .35; pointer-events: none; }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 18px; border-radius: 12px; cursor: pointer; font-weight: 600;
  border: 1px solid var(--line); background: var(--card);
  transition: transform .15s, background .2s, border-color .2s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.disabled { opacity: .45; pointer-events: none; }
.btn.wide { width: 100%; }

/* ---------- каталог ---------- */
.search { display: block; margin: 18px 0 8px; }
.search input {
  width: 100%; padding: 14px 16px; border-radius: 14px; outline: none;
  border: 1px solid var(--line); background: var(--card);
  transition: border-color .2s, box-shadow .2s;
}
.search input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
h2.sec { font-size: 1.05rem; margin: 22px 0 10px; color: var(--muted); font-weight: 600; }
.cont { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: thin; }
.cont a {
  flex: none; min-width: 210px; max-width: 280px; padding: 12px 14px; border-radius: 12px;
  background: var(--card); border: 1px solid var(--line); transition: transform .2s, border-color .2s;
}
.cont a:hover { transform: translateY(-2px); border-color: var(--accent); }
.cont b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cont small { color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 18px 14px; margin-top: 14px; }
.grid .empty { grid-column: 1 / -1; text-align: center; padding: 40px 0; }
.card { display: block; animation: rise .45s ease both; animation-delay: calc(var(--i, 0) * 45ms); }
.card .cover { width: 100%; transition: transform .25s, box-shadow .25s; }
.card:hover .cover { transform: translateY(-4px); box-shadow: 0 12px 30px rgba(0, 0, 0, .45); }
.card-title { margin-top: 8px; font-weight: 600; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-sub { font-size: .82rem; color: var(--muted); margin-top: 2px; }

.cover {
  position: relative; aspect-ratio: 2 / 3; border-radius: 12px; overflow: hidden;
  background: var(--card); box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover .ph {
  position: absolute; inset: 0; display: grid; place-items: center; padding: 10px; text-align: center;
  background: linear-gradient(155deg, hsl(var(--h) 42% 30%), hsl(calc(var(--h) + 40) 45% 12%));
}
.cover .ph span { font-size: clamp(1.1rem, 6vw, 1.8rem); font-weight: 700; line-height: 1.15; color: rgba(255, 255, 255, .88); text-shadow: 0 2px 10px rgba(0, 0, 0, .4); }
.cover .tag {
  position: absolute; left: 8px; top: 8px; padding: 2px 8px; border-radius: 8px; font-size: .72rem; font-weight: 600;
  background: rgba(0, 0, 0, .6); color: #fff; -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}

/* ---------- сторінка твору ---------- */
.hero { display: flex; flex-direction: column; align-items: center; text-align: center; padding: 26px 0 6px; gap: 6px; }
.hero .cover { width: min(220px, 58vw); animation: rise .5s ease both; }
.hero h1 { font-size: 1.6rem; margin: 14px 0 0; animation: rise .5s .06s ease both; }
.hero .orig { color: var(--muted); margin: 0; animation: rise .5s .1s ease both; }
.hero .author { margin: 0; color: var(--muted); font-size: .95rem; animation: rise .5s .14s ease both; }
.hero .actions { margin-top: 14px; display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; animation: rise .5s .18s ease both; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 20px 0 16px; }
.tabs a { padding: 12px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; transition: color .2s; }
.tabs a.on { color: var(--text); border-color: var(--accent); }
.tabs a small { color: var(--muted); margin-left: 4px; }
.desc { white-space: pre-line; max-width: 46rem; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.chip { padding: 6px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); font-size: .9rem; transition: border-color .2s; }
a.chip:hover { border-color: var(--accent); }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 0 24px; margin: 18px 0; max-width: 46rem; }
.facts div { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; text-align: right; }

.tools { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 8px; }
.jump { display: flex; gap: 8px; margin-left: auto; }
.jump input { width: 8.5em; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); outline: none; }
.jump input:focus { border-color: var(--accent); }
.clist .ch { display: flex; gap: 12px; align-items: baseline; padding: 14px 8px; border-bottom: 1px solid var(--line); transition: background .2s; }
.clist .ch:hover { background: var(--bg2); }
.clist .cn { color: var(--muted); white-space: nowrap; min-width: 6em; }
.clist .cur .cn::before { content: "● "; color: var(--accent); }
.clist .more { margin: 16px auto; display: flex; }

/* ---------- читалка ---------- */
.progress { position: fixed; z-index: 40; top: 0; left: 0; right: 0; height: 3px; pointer-events: none; }
.progress i { display: block; height: 100%; background: var(--accent); transform: scaleX(0); transform-origin: left; }
.rbar.top {
  position: fixed; z-index: 30; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--bar); border-bottom: 1px solid var(--line);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  transition: transform .3s ease;
}
.rtitles { flex: 1; min-width: 0; }
.rw, .rc { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rw { font-weight: 600; }
.rc { font-size: .8rem; color: var(--muted); }
.rbar.bottom {
  position: fixed; z-index: 30; left: 50%; bottom: calc(14px + env(safe-area-inset-bottom));
  width: min(420px, calc(100% - 24px));
  display: flex; justify-content: space-around; padding: 6px; border-radius: 18px;
  background: var(--bar); border: 1px solid var(--line); box-shadow: 0 8px 28px rgba(0, 0, 0, .35);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  transform: translate(-50%, 0); transition: transform .3s ease, opacity .3s ease;
}
.bars-hidden .rbar.top { transform: translateY(-100%); }
.bars-hidden .rbar.bottom { transform: translate(-50%, 170%); opacity: 0; }

.chapter { max-width: 42rem; margin: 0 auto; padding: calc(84px + env(safe-area-inset-top)) 20px 150px; animation: rise .45s ease both; }
.chapter .eyebrow { color: var(--accent); font-weight: 600; font-size: .9rem; text-transform: uppercase; letter-spacing: .08em; }
.chapter h1 { margin: 6px 0 28px; font-size: 1.7rem; }
.prose p { margin: 0 0 1em; font-size: var(--fs); line-height: var(--lh); hyphens: auto; -webkit-hyphens: auto; overflow-wrap: break-word; }
[data-align="justify"] .prose p { text-align: justify; text-justify: inter-word; hyphens: auto; -webkit-hyphens: auto; }
[data-align="justify"] .prose p.jl { text-align-last: justify; }
.prose hr { border: 0; text-align: center; margin: 2em 0; }
.prose hr::before { content: "· · ·"; color: var(--muted); letter-spacing: .5em; }
.prose em { font-style: italic; }
.prose p, .chapter h1 { font-family: var(--font); }
.endnav { display: flex; gap: 12px; margin-top: 40px; }
.endnav .btn { flex: 1; }

/* ---------- налаштування читання ---------- */
.sheet { position: fixed; inset: 0; z-index: 60; visibility: hidden; transition: visibility 0s .3s; }
.sheet.open { visibility: visible; transition: none; }
.sheet-bg { position: absolute; inset: 0; background: rgba(0, 0, 0, .5); opacity: 0; transition: opacity .3s; }
.sheet.open .sheet-bg { opacity: 1; }
.sheet-panel {
  position: absolute; left: 50%; bottom: 0; width: min(520px, 100%);
  padding: 14px 18px calc(18px + env(safe-area-inset-bottom)); border-radius: 22px 22px 0 0;
  background: var(--bg); color: var(--text); border: 1px solid var(--line); border-bottom: 0;
  transform: translate(-50%, 105%); transition: transform .32s cubic-bezier(.2, .8, .2, 1);
}
.sheet.open .sheet-panel { transform: translate(-50%, 0); }
.sheet-grab { width: 40px; height: 4px; border-radius: 4px; background: var(--line); margin: 0 auto 12px; }
.sheet .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.sheet .row > span { color: var(--muted); }
.seg, .stepper { display: flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.seg button, .stepper button { padding: 9px 12px; border: 0; background: var(--card); cursor: pointer; transition: background .2s; }
.seg button + button, .stepper button + button, .stepper output + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: #fff; }
.stepper output { min-width: 3.4em; display: grid; place-items: center; background: var(--card); border-left: 1px solid var(--line); }
.sheet .btn { margin-top: 16px; }
.sheet select {
  max-width: 62%; padding: 10px 12px; border-radius: 12px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
}

/* ---------- список розділів поверх читалки ---------- */
.toc { position: fixed; inset: 0; z-index: 65; visibility: hidden; transition: visibility 0s .3s; }
.toc.open { visibility: visible; transition: none; }
.toc-bg { position: absolute; inset: 0; background: rgba(0, 0, 0, .5); opacity: 0; transition: opacity .3s; }
.toc.open .toc-bg { opacity: 1; }
.toc-panel {
  position: absolute; left: 50%; bottom: 0; width: min(640px, 100%); height: 86vh; height: 86dvh;
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--text); border: 1px solid var(--line); border-bottom: 0; border-radius: 22px 22px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  transform: translate(-50%, 105%); transition: transform .32s cubic-bezier(.2, .8, .2, 1);
}
.toc.open .toc-panel { transform: translate(-50%, 0); }
.toc-head { display: flex; align-items: center; gap: 8px; padding: 12px 10px 10px 18px; border-bottom: 1px solid var(--line); }
.toc-head strong { font-size: 1.1rem; }
.toc-head .jump { margin-left: auto; }
.toc-head .jump input { width: 4.5em; }
.toc-head .icon { font-size: 1.6rem; line-height: 1; }
#tmsg { margin: 8px 18px 0; }
.toc .clist { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 0 8px; }
.toc .ch { height: 52px; padding-top: 0; padding-bottom: 0; align-items: center; content-visibility: auto; contain-intrinsic-size: auto 52px; }
.toc .ct { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.toc .cur { background: var(--bg2); }

.hero .meta { display: flex; flex-direction: column; align-items: center; gap: 6px; }

@media (min-width: 720px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
  .hero { flex-direction: row; text-align: left; align-items: flex-end; gap: 28px; padding-top: 34px; }
  .hero .meta { flex: 1; align-items: flex-start; }
  .hero .actions { justify-content: flex-start; }
}
