:root {
  --navy: #2e3157;
  --copper: #c8864f;
  --copper-soft: rgba(200, 134, 79, .16);
  --book-bg: #2e3157;

  --bg: #f4f2ee;
  --surface: #ffffff;
  --surface-2: #f0eee9;
  --text: #1f2140;
  --muted: #6d6f86;
  --line: rgba(31, 33, 64, .1);
  --shadow: 0 10px 30px -12px rgba(31, 33, 64, .35);
  --glass: rgba(255, 255, 255, .78);
  --reader-bg: #e9e7e2;
  --reader-bg: color-mix(in srgb, var(--book-bg) 9%, #f1efea);

  --safe-t: max(env(safe-area-inset-top), var(--tg-content-safe-area-inset-top, 0px) + var(--tg-safe-area-inset-top, 0px));
  --safe-b: env(safe-area-inset-bottom);
  --ease: cubic-bezier(.22, .8, .24, 1);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #121427;
  --surface: #1c1f38;
  --surface-2: #24284a;
  --text: #eceaf6;
  --muted: #9b9db8;
  --line: rgba(236, 234, 246, .1);
  --shadow: 0 14px 34px -14px rgba(0, 0, 0, .7);
  --glass: rgba(22, 24, 46, .74);
  --reader-bg: #10121f;
  --reader-bg: color-mix(in srgb, var(--book-bg) 38%, #07080f);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 Manrope, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  transition: background .3s;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
[hidden] { display: none !important; }
img { -webkit-user-drag: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }

.icon-btn {
  width: 42px; height: 42px; flex: none;
  display: grid; place-items: center;
  border-radius: 12px;
  color: var(--text);
  transition: background .2s, transform .15s, color .2s;
}
.icon-btn:hover { background: var(--line); }
.icon-btn:active { transform: scale(.92); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn.on { color: var(--copper); }

/* ───────────── Полиця ───────────── */
.shelf {
  max-width: 960px; margin: 0 auto;
  padding: calc(var(--safe-t) + 18px) 16px calc(var(--safe-b) + 28px);
  animation: rise .5s var(--ease);
}
.shelf-head { display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 12px; }
.logo-badge { width: 50px; height: 50px; flex: none; border-radius: 50%; background: #f6f3ec; display: grid; place-items: center; box-shadow: 0 0 0 1px rgba(31,33,64,.08), 0 6px 16px -8px rgba(31,33,64,.4); }
.logo-badge img { width: 41px; height: 41px; }
.wordmark { display: flex; flex-direction: column; align-items: flex-start; line-height: 1; }
.wm-top { font-weight: 700; font-size: 20px; letter-spacing: .2em; }
.wm-bottom { font-weight: 600; font-size: 8px; letter-spacing: .62em; margin-top: 5px; color: var(--copper); }
.shelf-intro { margin: 34px 0 22px; }
.shelf-intro h1 { font-size: clamp(26px, 6vw, 36px); line-height: 1.1; margin: 0 0 8px; font-weight: 800; letter-spacing: -.02em; }
.shelf-intro p { margin: 0; color: var(--muted); }

.continue {
  display: flex; align-items: center; gap: 16px;
  padding: 14px; margin-bottom: 26px;
  border-radius: 20px;
  background: var(--navy);
  background: linear-gradient(135deg, var(--cbg, var(--navy)), color-mix(in srgb, var(--cbg, var(--navy)) 70%, #000));
  color: #fff; cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .2s var(--ease);
}
.continue:active { transform: scale(.98); }
.continue img { width: 64px; border-radius: 3px 7px 7px 3px; box-shadow: 0 6px 16px rgba(0,0,0,.35); }
.continue .c-body { flex: 1; min-width: 0; }
.continue .c-label { font-size: 12px; text-transform: uppercase; letter-spacing: .12em; color: var(--copper); font-weight: 700; }
.continue .c-title { font-weight: 700; font-size: 17px; margin: 2px 0 8px; }
.continue .c-meta { font-size: 13px; opacity: .75; margin-top: 6px; }
.continue .c-go { width: 44px; height: 44px; border-radius: 50%; background: var(--copper); display: grid; place-items: center; flex: none; }
.continue .c-go svg { width: 22px; height: 22px; }

.progress { height: 4px; border-radius: 4px; background: rgba(127, 127, 127, .22); overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--copper); border-radius: inherit; }

.books {
  display: grid; gap: 28px 18px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
@media (min-width: 640px) { .books { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 36px 28px; } }
.book { cursor: pointer; animation: rise .55s var(--ease) both; }
.book:nth-child(2) { animation-delay: .06s; }
.book:nth-child(3) { animation-delay: .12s; }
.book:nth-child(4) { animation-delay: .18s; }
/* усі обкладинки однакового формату; різниця пропорцій ховається легким кадруванням */
.cover {
  position: relative;
  aspect-ratio: .76;
  border-radius: 4px 12px 12px 4px;
  overflow: hidden;
  background: var(--surface-2);
  box-shadow: 0 1px 1px rgba(0,0,0,.08), 0 18px 30px -16px rgba(20, 22, 50, .6), 0 4px 10px -4px rgba(20, 22, 50, .3);
  transform-origin: left center;
  transition: transform .35s var(--ease), box-shadow .35s var(--ease);
}
.cover img { display: block; width: 100%; height: 100%; object-fit: cover; object-position: center; }
.cover::before { /* корінець */
  content: ""; position: absolute; inset: 0 auto 0 0; width: 14px; z-index: 1;
  background: linear-gradient(90deg, rgba(0,0,0,.24), rgba(255,255,255,.1) 45%, rgba(0,0,0,.05) 70%, transparent);
}
.cover::after { /* відблиск */
  content: ""; position: absolute; inset: 0; z-index: 1;
  background: linear-gradient(115deg, rgba(255,255,255,.09), transparent 34%);
  pointer-events: none;
}
.book:hover .cover { transform: perspective(900px) rotateY(-9deg) translateY(-4px); box-shadow: 0 1px 1px rgba(0,0,0,.08), 22px 26px 34px -18px rgba(20, 22, 50, .6); }
.book:active .cover { transform: scale(.97); }
.book h3 { margin: 14px 0 2px; font-size: 16px; font-weight: 700; line-height: 1.25; }
.book .b-sub { color: var(--muted); font-size: 13px; }
.book .progress { margin-top: 10px; }
.book .b-pct { font-size: 12px; color: var(--muted); margin-top: 5px; }

.shelf-empty { grid-column: 1 / -1; display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; padding: 50px 20px; color: var(--muted); }
.shelf-empty img { width: 120px; margin-bottom: 8px; }
.shelf-empty b { color: var(--text); font-size: 18px; }
:root[data-theme="dark"] .shelf-empty img { filter: invert(1) hue-rotate(180deg) brightness(1.4); }
.hint-logo { width: 74px; height: 74px; margin: -60px auto 8px; border-radius: 50%; background: #f6f3ec; display: grid; place-items: center; box-shadow: 0 10px 24px -8px rgba(0,0,0,.45), 0 0 0 4px var(--surface); }
.hint-logo img { width: 60px; }
.shelf-foot { margin-top: 48px; text-align: center; font-size: 12px; color: var(--muted); letter-spacing: .04em; }

@keyframes rise { from { opacity: 0; transform: translateY(14px); } }

/* ───────────── Читалка ───────────── */
.reader {
  position: fixed; inset: 0;
  background: var(--reader-bg);
  overflow: hidden;
  transition: background .3s;
}
.reader::before { /* м'яке світло за книгою */
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(80% 60% at 50% 45%, rgba(255,255,255,.35), transparent 70%);
}
:root[data-theme="dark"] .reader::before { background: radial-gradient(70% 55% at 50% 45%, color-mix(in srgb, var(--book-bg) 40%, transparent), transparent 75%); }

.stage {
  position: absolute; inset: 0;
  touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  cursor: grab;
}
.stage.zoomed { cursor: move; }
.track { position: absolute; inset: 0; will-change: transform; }
.view { position: absolute; top: 0; height: 100%; }
.sheet {
  position: absolute;
  display: flex;
  transform-origin: 0 0;
  will-change: transform;
}
.page {
  position: relative; flex: 1 1 0; min-width: 0; height: 100%;
  background: #f4f4f6 center / cover no-repeat;
  overflow: hidden;
}
.page { box-shadow: 0 14px 30px -10px rgba(10, 12, 30, .35), 0 2px 5px rgba(10, 12, 30, .16); }
.page img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; opacity: 0; transition: opacity .35s; }
.page img.on { opacity: 1; }
.sheet:not(.spread) .page { border-radius: 3px; }
.sheet.spread .page:first-child { border-radius: 4px 0 0 4px; }
.sheet.spread .page:last-child { border-radius: 0 4px 4px 0; }
.sheet.spread .page::after { /* згин посередині розвороту */
  content: ""; position: absolute; top: 0; bottom: 0; width: 9%; pointer-events: none; z-index: 2;
}
.sheet.spread .page:first-child::after { right: 0; background: linear-gradient(90deg, transparent, rgba(0,0,0,.05) 60%, rgba(0,0,0,.16)); }
.sheet.spread .page:last-child::after { left: 0; background: linear-gradient(270deg, transparent, rgba(0,0,0,.04) 60%, rgba(0,0,0,.14)); }

.hl { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
.hl i {
  position: absolute; border-radius: 3px;
  background: rgba(200, 134, 79, .38);
  box-shadow: 0 0 0 2px rgba(200, 134, 79, .55);
  mix-blend-mode: multiply;
  animation: hlpulse 1.4s ease-out 2;
}
@keyframes hlpulse { 0% { box-shadow: 0 0 0 2px rgba(200,134,79,.6), 0 0 0 0 rgba(200,134,79,.5); } 100% { box-shadow: 0 0 0 2px rgba(200,134,79,.6), 0 0 0 14px rgba(200,134,79,0); } }

/* міні-мапа при зумі */
.mini {
  position: absolute; left: 14px; bottom: calc(var(--safe-b) + 84px);
  padding: 4px; border-radius: 10px;
  background: var(--glass); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
  opacity: 0; transform: translateY(8px) scale(.96); transition: opacity .25s, transform .25s var(--ease);
  pointer-events: none; z-index: 5;
}
.mini.show { opacity: 1; transform: none; }
.mini-pages { display: flex; }
.mini-pages img { display: block; height: 96px; border-radius: 2px; }
.mini-view { position: absolute; border: 2px solid var(--copper); border-radius: 3px; background: rgba(200, 134, 79, .14); }

.pill {
  position: absolute; left: 50%; top: calc(var(--safe-t) + 74px);
  transform: translate(-50%, -6px); opacity: 0;
  padding: 6px 14px; border-radius: 99px;
  background: var(--glass); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  font-weight: 700; font-size: 14px; font-variant-numeric: tabular-nums;
  box-shadow: var(--shadow);
  transition: opacity .2s, transform .2s var(--ease);
  pointer-events: none; z-index: 6;
}
.pill.show { opacity: 1; transform: translate(-50%, 0); }

.loupe {
  position: fixed; left: 0; top: 0; z-index: 30;
  width: 180px; height: 180px; border-radius: 50%;
  pointer-events: none;
  opacity: 0; transform: scale(.4);
  transition: opacity .18s, transform .22s var(--ease);
  box-shadow: 0 0 0 4px #fff, 0 0 0 6px var(--copper), 0 20px 40px -8px rgba(0,0,0,.5);
  overflow: hidden; background: #f4f4f6;
}
.loupe.show { opacity: 1; transform: scale(1); }
.loupe-img { position: absolute; inset: 0; background-repeat: no-repeat; }
.loupe::after { content: ""; position: absolute; inset: 0; border-radius: 50%; box-shadow: inset 0 0 18px rgba(0,0,0,.18); background: radial-gradient(circle at 30% 25%, rgba(255,255,255,.35), transparent 40%); }

/* панелі читалки */
.bar {
  position: absolute; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; gap: 4px;
  background: var(--glass);
  backdrop-filter: blur(18px) saturate(1.4); -webkit-backdrop-filter: blur(18px) saturate(1.4);
  transition: transform .35s var(--ease), opacity .3s;
}
.top-bar { top: 0; padding: calc(var(--safe-t) + 6px) 8px 6px; border-bottom: 1px solid var(--line); }
.bottom-bar { bottom: 0; padding: 8px 14px calc(var(--safe-b) + 10px); border-top: 1px solid var(--line); gap: 10px; }
.reader.ui-off .top-bar { transform: translateY(-100%); opacity: 0; pointer-events: none; }
.reader.ui-off .bottom-bar { transform: translateY(100%); opacity: 0; pointer-events: none; }
.bar-title { flex: 1; min-width: 0; padding: 0 4px; }
.r-title { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-count { font-weight: 700; font-size: 13px; font-variant-numeric: tabular-nums; min-width: 64px; text-align: right; color: var(--muted); }
.r-count b { color: var(--text); }
.nav-btn { display: none; }
@media (hover: hover) and (min-width: 720px) { .nav-btn { display: grid; } }

.scrub { position: relative; flex: 1; height: 36px; display: flex; align-items: center; cursor: pointer; touch-action: none; }
.scrub-track { position: relative; width: 100%; height: 6px; border-radius: 6px; background: var(--line); }
.scrub-fill { position: absolute; inset: 0 auto 0 0; border-radius: inherit; background: var(--copper); }
.scrub-knob {
  position: absolute; top: 50%; width: 20px; height: 20px; margin: -10px 0 0 -10px;
  border-radius: 50%; background: #fff; box-shadow: 0 0 0 3px var(--copper), 0 4px 10px rgba(0,0,0,.25);
  transition: transform .2s var(--ease);
}
.scrub.drag .scrub-knob { transform: scale(1.25); }
.scrub-ticks i { position: absolute; top: 50%; width: 2px; height: 10px; margin: -5px 0 0 -1px; border-radius: 2px; background: var(--muted); opacity: .45; }
.scrub-bubble {
  position: absolute; bottom: 44px; left: 0;
  width: 104px; padding: 5px 5px 4px; border-radius: 12px;
  background: var(--surface); box-shadow: var(--shadow);
  transform: translateX(-50%) translateY(8px) scale(.9); opacity: 0;
  transition: opacity .18s, transform .2s var(--ease);
  pointer-events: none; text-align: center;
}
.scrub.drag .scrub-bubble { opacity: 1; transform: translateX(-50%); }
.scrub-bubble img { display: block; width: 100%; border-radius: 6px; background: var(--surface-2); }
.scrub-bubble span { display: block; font-size: 12px; font-weight: 700; margin-top: 3px; }

.hint { position: absolute; inset: 0; z-index: 40; display: grid; place-items: center; background: rgba(8, 9, 20, .5); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); animation: fade .3s; padding: 16px; }
.hint-card { background: var(--surface); border-radius: 22px; padding: 22px 20px 18px; margin-top: 40px; max-width: 340px; width: 100%; box-shadow: var(--shadow); animation: rise .4s var(--ease); }
.hint-row { display: flex; gap: 14px; align-items: center; padding: 9px 0; font-weight: 600; }
.hint-ic { font-size: 24px; width: 34px; text-align: center; }
.hint-ok { margin-top: 12px; width: 100%; padding: 13px; border-radius: 14px; background: var(--copper); color: #fff; font-weight: 700; }
@keyframes fade { from { opacity: 0; } }

/* ───────────── Панель ───────────── */
.panel { position: fixed; inset: 0; z-index: 50; }
.panel-backdrop { position: absolute; inset: 0; background: rgba(8, 9, 20, .45); animation: fade .25s; }
.panel-sheet {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 84vh; height: 84vh;
  display: flex; flex-direction: column;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  box-shadow: 0 -10px 40px rgba(0,0,0,.25);
  animation: sheet-up .35s var(--ease);
  padding-bottom: var(--safe-b);
}
.panel.closing .panel-sheet { animation: sheet-down .25s ease-in forwards; }
.panel.closing .panel-backdrop { animation: fade .25s reverse forwards; }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes sheet-down { to { transform: translateY(100%); } }
@media (min-width: 760px) {
  .panel-sheet { left: auto; top: 0; width: 400px; height: auto; max-height: none; border-radius: 22px 0 0 22px; animation-name: sheet-left; padding-top: var(--safe-t); }
  .panel.closing .panel-sheet { animation-name: sheet-right; }
  .panel-grip { display: none; }
  @keyframes sheet-left { from { transform: translateX(100%); } }
  @keyframes sheet-right { to { transform: translateX(100%); } }
}
.panel-grip { width: 42px; height: 5px; border-radius: 5px; background: var(--line); margin: 10px auto 4px; flex: none; }
.tabs { display: flex; gap: 4px; padding: 8px 12px 10px; border-bottom: 1px solid var(--line); flex: none; overflow-x: auto; }
.tabs button { padding: 8px 14px; border-radius: 99px; font-weight: 600; font-size: 14px; color: var(--muted); white-space: nowrap; transition: background .2s, color .2s; }
.tabs button.on { background: var(--copper-soft); color: var(--copper); }
.panel-body { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.tab { display: none; padding: 10px 12px 24px; }
.tab.on { display: block; animation: fade .2s; }

.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-list li { display: flex; align-items: baseline; gap: 10px; padding: 11px 12px; border-radius: 12px; cursor: pointer; transition: background .15s; }
.toc-list li:hover { background: var(--surface-2); }
.toc-list li.l1 { font-weight: 700; margin-top: 6px; }
.toc-list li.l2 { padding-left: 24px; font-size: 14px; }
.toc-list li.cur { background: var(--copper-soft); color: var(--copper); }
.toc-list .t { flex: 1; }
.toc-list .p { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 14px 10px; }
.thumb { cursor: pointer; text-align: center; font-size: 12px; color: var(--muted); font-weight: 600; }
.thumb img { display: block; width: 100%; border-radius: 4px; background: var(--surface-2); box-shadow: 0 2px 8px -2px rgba(0,0,0,.25); margin-bottom: 5px; transition: transform .2s var(--ease); }
.thumb:hover img { transform: translateY(-2px); }
.thumb.cur img { box-shadow: 0 0 0 3px var(--copper); }
.thumb.cur { color: var(--copper); }

.marks .mark { display: flex; align-items: center; gap: 12px; padding: 8px; border-radius: 12px; cursor: pointer; }
.marks .mark:hover { background: var(--surface-2); }
.marks .mark img { width: 46px; border-radius: 3px; box-shadow: 0 2px 6px rgba(0,0,0,.2); }
.marks .mark .m-body { flex: 1; }
.marks .mark .m-t { font-weight: 700; }
.marks .mark .m-s { font-size: 13px; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 48px 20px; }
.empty b { display: block; color: var(--text); font-size: 16px; margin-bottom: 6px; }

.search-box { position: sticky; top: -10px; background: var(--surface); padding: 2px 0 10px; z-index: 1; }
.search-box input {
  width: 100%; padding: 13px 16px; border-radius: 14px;
  border: 1.5px solid var(--line); background: var(--surface-2); color: var(--text);
  font: inherit; font-size: 16px; outline: none; transition: border-color .2s;
}
.search-box input:focus { border-color: var(--copper); }
.s-meta { font-size: 13px; color: var(--muted); padding: 2px 4px 8px; }
.s-results { list-style: none; margin: 0; padding: 0; }
.s-results li { padding: 11px 12px; border-radius: 12px; cursor: pointer; display: flex; gap: 12px; }
.s-results li:hover { background: var(--surface-2); }
.s-results .sp { font-weight: 700; color: var(--copper); font-size: 13px; min-width: 38px; font-variant-numeric: tabular-nums; }
.s-results .ss { flex: 1; font-size: 14px; }
.s-results mark { background: var(--copper-soft); color: inherit; border-radius: 3px; padding: 0 2px; font-weight: 700; }
.s-results .sc { font-size: 12px; color: var(--muted); }

.fly { position: fixed; z-index: 60; pointer-events: none; border-radius: 4px 10px 10px 4px; box-shadow: 0 30px 60px -20px rgba(0,0,0,.6); object-fit: cover; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
