
:root {
  /* Светлая тема «День» */
  --paper: #F3F4EE;
  --paper-2: #E9ECE3;
  --paper-3: #DFE4D8;
  --ink: #1B2320;
  --ink-2: #46544C;
  --ink-3: #7B8880;
  --rule: #D2D8CB;
  --accent: #2E5D4B;
  --accent-2: #DAE7DD;
  --accent-3: #E7F0E9;
  --danger: #8C4A3F;
  --danger-soft: #E4D2CD;

  --hl-yellow: 243 222 155;
  --hl-yellow-line: 199 168 85;
  --hl-green: 190 220 192;
  --hl-green-line: 120 164 126;
  --hl-blue: 187 211 231;
  --hl-blue-line: 117 154 184;
  --hl-pink: 236 195 199;
  --hl-pink-line: 190 132 139;
  --hl-a: 1;
  --hl-line-a: .55;

  --font-read: 'Literata', Georgia, 'Times New Roman', serif;
  --font-ui: 'Golos Text', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

  --topbar-h: 56px;
  --rail-w: 292px;
  --notes-w: 332px;
  --reader-size: 21px;
  --reader-lh: 1.62;
  --measure: 34em;
  --radius: 4px;
  --shadow-pop: 0 12px 32px rgba(17, 26, 21, .14);
  --shadow-modal: 0 24px 64px rgba(15, 22, 18, .26);
  --shadow-sheet: -12px 0 40px rgba(20, 28, 24, .16);
}

:root[data-theme="night"] {
  --paper: #141A18;
  --paper-2: #1B2320;
  --paper-3: #232C28;
  --ink: #E6EAE2;
  --ink-2: #AEB9B1;
  --ink-3: #7E8B83;
  --rule: #2A342F;
  --accent: #83BCA0;
  --accent-2: #23312B;
  --accent-3: #1D2A24;
  --danger: #D29A90;
  --danger-soft: #3A2724;
  --hl-a: .42;
  --hl-line-a: .38;
  --shadow-pop: 0 12px 32px rgba(0, 0, 0, .5);
  --shadow-modal: 0 24px 64px rgba(0, 0, 0, .6);
  --shadow-sheet: -12px 0 40px rgba(0, 0, 0, .55);
}

:root[data-theme="lamp"] {
  --paper: #EFE6D3;
  --paper-2: #E5D9C1;
  --paper-3: #DCCEB2;
  --ink: #3A2F22;
  --ink-2: #6A5B45;
  --ink-3: #8B7A5F;
  --rule: #D3C4A6;
  --accent: #76562F;
  --accent-2: #E8DBC3;
  --accent-3: #F0E4CE;
  --danger: #8A432F;
  --danger-soft: #E2CDBD;
}

/* ---------------------------------------------------------------- reset -- */
*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2, h3, h4, p, ul, ol, figure { margin: 0; }
ul, ol { padding: 0; list-style: none; }
button { font: inherit; color: inherit; }
input, textarea, select { font: inherit; color: inherit; }
img, svg { display: block; }
a { color: inherit; }
[hidden] { display: none !important; }

::selection { background: var(--accent-2); color: var(--ink); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.skip {
  position: fixed;
  top: 8px; left: 8px;
  z-index: 200;
  padding: 10px 14px;
  background: var(--accent);
  color: var(--paper);
  border-radius: 4px;
  font-size: 13px;
  text-decoration: none;
  transform: translateY(-160%);
  transition: transform .15s ease;
}
.skip:focus { transform: none; }

/* ----------------------------------------------------------- scrollbars -- */
.scroller, .toc, .books, .notes__list, .dialog__body, .search__results, .pop {
  scrollbar-width: thin;
  scrollbar-color: var(--paper-3) transparent;
}
.scroller::-webkit-scrollbar,
.toc::-webkit-scrollbar,
.books::-webkit-scrollbar,
.notes__list::-webkit-scrollbar,
.dialog__body::-webkit-scrollbar,
.search__results::-webkit-scrollbar,
.pop::-webkit-scrollbar { width: 10px; height: 10px; }
.scroller::-webkit-scrollbar-thumb,
.toc::-webkit-scrollbar-thumb,
.books::-webkit-scrollbar-thumb,
.notes__list::-webkit-scrollbar-thumb,
.dialog__body::-webkit-scrollbar-thumb,
.search__results::-webkit-scrollbar-thumb,
.pop::-webkit-scrollbar-thumb {
  background: var(--paper-3);
  border-radius: 6px;
  border: 3px solid transparent;
  background-clip: padding-box;
}
.scroller::-webkit-scrollbar-track,
.toc::-webkit-scrollbar-track,
.books::-webkit-scrollbar-track,
.notes__list::-webkit-scrollbar-track,
.dialog__body::-webkit-scrollbar-track,
.search__results::-webkit-scrollbar-track,
.pop::-webkit-scrollbar-track { background: transparent; }

/* ------------------------------------------------------------- каркас --- */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

.main {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: var(--rail-w) minmax(0, 1fr) var(--notes-w);
  grid-template-rows: minmax(0, 1fr);
  overflow: hidden;
}

.pane {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
  background: var(--paper);
  overflow: hidden;
}

.scroller {
  flex: 1 1 auto;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 clamp(20px, 4vw, 44px);
  outline: none;
  overscroll-behavior: contain;
}
@media (prefers-reduced-motion: no-preference) {
  .scroller { scroll-behavior: smooth; }
}

/* ------------------------------------------------------------- шапка ---- */
.topbar {
  position: relative;
  z-index: 31;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--topbar-h);
  padding: 0 12px 0 10px;
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
}

.topbar__actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 4px;
}

.brand {
  display: inline-flex;
  align-items: baseline;
  gap: 11px;
  min-width: 0;
  padding: 6px 2px;
  text-decoration: none;
  color: var(--ink);
}
.brand__title {
  font-family: var(--font-read);
  font-weight: 600;
  font-size: 16.5px;
  letter-spacing: .002em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand:hover .brand__title { color: var(--accent); }
.brand__sep { width: 1px; height: 14px; background: var(--rule); flex: 0 0 auto; }
.brand__author {
  font-size: 13px;
  color: var(--ink-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tbtn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 40px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink-2);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .12s ease, color .12s ease;
}
.tbtn:hover { background: var(--paper-2); color: var(--ink); }
.tbtn[aria-expanded="true"], .tbtn.is-on { background: var(--accent-2); color: var(--accent); }
.tbtn--icon { width: 40px; padding: 0; }
.tbtn .ic { width: 16px; height: 16px; }
.tbtn kbd {
  font-family: var(--font-ui);
  font-size: 11px;
  line-height: 1;
  color: var(--ink-2);
  border: 1px solid var(--rule);
  border-radius: 3px;
  padding: 3px 5px;
  background: var(--paper);
}
.tbtn .count {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent-2);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  line-height: 18px;
  text-align: center;
}
.tbtn#settingsBtn { font-size: 14px; font-weight: 500; letter-spacing: .01em; }

.readline { flex: 0 0 auto; height: 2px; background: var(--paper); position: relative; z-index: 31; }
.readline i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .18s linear;
}

/* -------------------------------------------------------------- иконки --- */
.ic { width: 16px; height: 16px; display: block; flex: 0 0 auto; }
.ic svg { width: 100%; height: 100%; }

:root:not(.is-admin) .admin-only { display: none !important; }

/* --------------------------------------------------------- кнопки/поля --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background-color .12s ease, border-color .12s ease, color .12s ease;
}
.btn:hover { background: var(--paper-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn--primary:hover { background: var(--accent); filter: brightness(1.06); }
:root[data-theme="night"] .btn--primary { color: #10201A; }
.btn--danger { color: var(--danger); border-color: var(--danger-soft); }
.btn--danger:hover { background: var(--danger-soft); }
.btn--sm { min-height: 34px; padding: 0 12px; font-size: 12.5px; }

.mini {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
}
.mini:hover { background: var(--paper-2); color: var(--ink); }
.mini .ic { width: 15px; height: 15px; }

.linkbtn {
  border: 0;
  background: none;
  padding: 4px 2px;
  color: var(--ink-2);
  font-size: 12.5px;
  cursor: pointer;
  text-decoration: none;
  border-bottom: 1px solid transparent;
}
.linkbtn:hover { color: var(--accent); border-bottom-color: var(--accent); }

.input, textarea.input {
  width: 100%;
  min-height: 40px;
  padding: 9px 11px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--paper-2);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.45;
}
.input::placeholder, textarea.input::placeholder { color: var(--ink-2); }
.input:focus { border-color: var(--accent); }
textarea.input { resize: vertical; min-height: 72px; }

/* ------------------------------------------------------------- слой scrim */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(20, 28, 24, .3);
  animation: fadeIn .16s ease-out;
}
.scrim[hidden] { display: none; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ------------------------------------------------------------- модалки --- */
.ovl {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background: rgba(20, 28, 24, .34);
  animation: fadeIn .14s ease-out;
}

.dialog {
  display: flex;
  flex-direction: column;
  width: min(560px, 100%);
  max-height: min(86vh, 760px);
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: var(--shadow-modal);
  animation: dialogIn .18s cubic-bezier(.2, .7, .3, 1);
  overflow: hidden;
}
.dialog--s { width: min(430px, 100%); }
.dialog--m { width: min(560px, 100%); }

@keyframes dialogIn {
  from { opacity: 0; transform: translateY(8px) scale(.99); }
  to { opacity: 1; transform: none; }
}

.dialog__head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 18px 14px 12px 20px;
}
.dialog__title {
  flex: 1;
  font-family: var(--font-ui);
  font-size: 17px;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: -.005em;
}
.dialog__body { padding: 0 20px 6px; overflow-y: auto; min-height: 0; }
.dialog__foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px 18px;
  margin-top: 8px;
  border-top: 1px solid var(--rule);
  flex: 0 0 auto;
}
.dialog__text { font-size: 14px; line-height: 1.55; color: var(--ink-2); margin: 0 0 8px; }
.dialog__text strong { color: var(--ink); font-weight: 600; }
.dialog__field { margin: 10px 0 4px; }
.dialog__field label { display: block; font-size: 12.5px; color: var(--ink-2); margin: 0 0 6px; }

/* -------------------------------------------------------------- тосты --- */
.toasts {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 140;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  width: max-content;
  max-width: calc(100vw - 24px);
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: min(560px, calc(100vw - 32px));
  padding: 10px 14px;
  border-radius: 5px;
  background: var(--ink);
  color: var(--paper);
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 10px 28px rgba(0, 0, 0, .22);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .16s ease, transform .16s ease;
}
.toast.is-in { opacity: 1; transform: none; }
.toast__act {
  border: 0;
  padding: 0;
  background: none;
  color: inherit;
  font-size: 13px;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
  white-space: nowrap;
}
.toast--error { background: var(--danger); color: #FFF8F5; }
:root[data-theme="night"] .toast--error { color: #1A1210; }

/* ---------------------------------------------- плавающая кнопка заметки --- */
.fab-note {
  position: fixed;
  right: 14px;
  bottom: calc(96px + env(safe-area-inset-bottom, 0px));
  z-index: 70;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 52px;
  height: 52px;
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: 50%;
  background: var(--paper);
  color: var(--accent);
  box-shadow: var(--shadow-pop);
  cursor: pointer;
  transition: transform .12s ease, background-color .12s ease, color .12s ease;
}
.fab-note:hover { transform: translateY(-1px); }
.fab-note:active { transform: scale(.96); }
.fab-note.is-active { background: var(--accent); border-color: var(--accent); color: #FFF; }
:root[data-theme="night"] .fab-note.is-active { color: #10201A; }
.fab-note .ic { width: 22px; height: 22px; }
.fab-note[hidden] { display: none !important; }

.safe-probe {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 1px;
  height: env(safe-area-inset-bottom, 0px);
  visibility: hidden;
  pointer-events: none;
}

/* --------------------------------------------------------- скелетоны ---- */
.skel { display: flex; flex-direction: column; gap: 12px; padding: 6px 0 4px; }
.skel__line {
  height: 12px;
  border-radius: 3px;
  background: var(--paper-2);
  animation: skelPulse 1.7s ease-in-out infinite;
}
.skel__line--title { height: 22px; width: 62%; margin-bottom: 10px; }
.skel__line--h2 { height: 17px; width: 48%; margin-top: 8px; }
.skel__gap { height: 26px; }
.skel--reader { gap: 13px; padding-top: 26px; }
.skel--reader .skel__line { height: 13px; }
.skel--reader .skel__line--title { height: 30px; width: 70%; }
@keyframes skelPulse { 0%, 100% { opacity: .5; } 50% { opacity: 1; } }

/* -------------------------------------------------------- состояния ----- */
.state {
  display: grid;
  place-items: center;
  text-align: center;
  padding: 64px 24px;
  font-family: var(--font-ui);
}
.state__inner { max-width: 40ch; }
.state h2 { font-size: 19px; font-weight: 600; margin: 0 0 8px; color: var(--ink); }
.state p { font-size: 14px; line-height: 1.6; color: var(--ink-2); margin: 0 0 20px; }
.state .btn { margin-top: 2px; }

.noscript { padding: 40px; text-align: center; font-size: 16px; }

/* -------------------------------------------------------- утилит-классы --- */
.pop-surface {
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 8px;
  box-shadow: var(--shadow-pop);
}

/* ------------------------------------------------------------ медиа ----- */
@media (max-width: 1439px) {
  :root { --rail-w: 260px; --notes-w: 300px; }
  .scroller { padding: 0 22px; }
}

@media (max-width: 1279px) {
  #quickNoteBtn { width: 40px; padding: 0; }
  #quickNoteBtn .tbtn__label { display: none; }
  .main { grid-template-columns: var(--rail-w) minmax(0, 1fr); }
  .scroller { padding: 0 clamp(24px, 4vw, 44px); }
  .notes {
    position: fixed;
    top: calc(var(--topbar-h) + 2px);
    right: 0;
    bottom: 0;
    z-index: 60;
    width: min(360px, 92vw);
    transform: translateX(102%);
    transition: transform .22s cubic-bezier(.2, .7, .3, 1);
    box-shadow: var(--shadow-sheet);
    border-left: 1px solid var(--rule);
  }
  .notes.is-open { transform: none; }
  .notes__grab { display: none; }
}

@media (max-width: 999px) {
  .main { grid-template-columns: minmax(0, 1fr); }
  .rail {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 60;
    width: min(324px, 88vw);
    transform: translateX(-102%);
    transition: transform .22s cubic-bezier(.2, .7, .3, 1);
    box-shadow: var(--shadow-sheet);
    background: var(--paper);
    border-right: 1px solid var(--rule);
  }
  .rail.is-open { transform: none; }
  .rail__close { display: inline-flex; }
  .rail__lib { max-height: 52%; }
}

@media (min-width: 1000px) {
  .rail__close { display: none; }
  .app--rail-collapsed .main { grid-template-columns: 0 minmax(0, 1fr) var(--notes-w); }
  .app--rail-collapsed .rail { overflow: hidden; border-right-color: transparent; }
}
@media (min-width: 1280px) {
  .app--no-notes .main { grid-template-columns: var(--rail-w) minmax(0, 1fr) 0; }
  .app--no-notes .notes { display: none; }
}

@media (max-width: 759px) {
  :root { --topbar-h: 52px; }
  .linkbtn { min-height: 40px; padding: 8px 2px; }
  .topbar { padding: 0 6px; gap: 4px; }
  .topbar__actions { gap: 0; }
  .brand { padding: 4px 4px; gap: 0; }
  .brand__title { font-size: 15px; }
  .brand__sep, .brand__author { display: none; }
  .tbtn { width: 40px; padding: 0; }
  .tbtn__label, .tbtn kbd { display: none; }
  .tbtn#settingsBtn { font-size: 13px; }
  .scroller { padding: 0 20px; }
  .notes {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 70vh;
    max-height: 70vh;
    border-left: 0;
    border-top: 1px solid var(--rule);
    border-radius: 12px 12px 0 0;
    transform: translateY(102%);
    box-shadow: 0 -12px 44px rgba(15, 22, 18, .22);
    transition: transform .24s cubic-bezier(.2, .7, .3, 1);
  }
  .notes.is-open { transform: none; }
  .notes__grab {
    display: block;
    width: 38px;
    height: 4px;
    border-radius: 2px;
    background: var(--rule);
    margin: 8px auto 2px;
    flex: 0 0 auto;
  }
  .dialog { max-height: 90vh; border-radius: 12px; }
  .toasts { bottom: calc(12px + env(safe-area-inset-bottom)); }
}

@media (max-width: 380px) {
  .scroller { padding: 0 16px; }
}

@media (max-width: 359px) {
  .brand { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
    scroll-behavior: auto !important;
  }
}
