:root,
[data-theme="light"] {
  color-scheme: light;
  --bg: #f3efe6;
  --bg-glow-a: #fff8e8;
  --bg-glow-b: #efe7d8;
  --ink: #1c1a17;
  --muted: #5c564c;
  --card: #fffdf8;
  --line: #d9d0c0;
  --accent: #0f6b5c;
  --accent-ink: #fff;
  --shadow: 0 10px 30px rgba(28, 26, 23, 0.08);
  --annotation-bg: #f7f1e6;
  --error-bg: #f8e8e8;
  --error-ink: #8a2b2b;
  --cover-bg: #e7dfd0;
  --radius: 14px;
  --control-radius: 12px;
  --pill-radius: 999px;
  --control-height: 44px;
  --control-pad-x: 1rem;
  --top-control-height: 36px;
  --top-radius: 10px;
  --top-font: 0.875rem;
  --font: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --header-bg: rgba(255, 253, 248, 0.92);
  --reader-max: clamp(42rem, 12rem + 40vw, 88rem);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #12161a;
  --bg-glow-a: #1a3030;
  --bg-glow-b: #243038;
  --ink: #e8eee9;
  --muted: #9aa8a3;
  --card: #1b2227;
  --line: #2c3838;
  --accent: #3cb8a0;
  --accent-ink: #06201a;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
  --annotation-bg: #222b30;
  --error-bg: #3a2226;
  --error-ink: #ffb0b8;
  --cover-bg: #2a3338;
  --header-bg: rgba(18, 22, 26, 0.92);
}

* { box-sizing: border-box; }

html {
  scrollbar-gutter: stable;
}

html, body {
  font-family: var(--font);
}

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, var(--bg-glow-a), transparent 40%),
    linear-gradient(180deg, var(--bg-glow-b), var(--bg));
  min-height: 100vh;
  transition: background 0.2s ease, color 0.2s ease;
}

.top {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem 1rem;
  align-items: center;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  padding: 0.55rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background: var(--header-bg);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
  /* Prevent a wide search row from shifting the whole page sideways on iOS. */
  overflow-x: clip;
}

.top-left {
  display: flex;
  align-items: center;
  gap: 1.65rem;
  min-width: 0;
}

.nav {
  display: flex;
  align-items: center;
  gap: 0.2rem;
}

.nav-link {
  appearance: none;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: var(--top-control-height);
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: var(--top-font);
  font-weight: 600;
  line-height: 1;
  padding: 0 0.85rem;
  border-radius: var(--top-radius);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}

.nav-link:hover {
  color: var(--ink);
  background: color-mix(in srgb, var(--line) 35%, transparent);
}

.nav-link.is-active {
  background: var(--card);
  border-color: var(--line);
  color: var(--ink);
}

.top-right {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  white-space: nowrap;
  margin-left: 0;
}

.user-box {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.user-label {
  color: var(--muted);
  font-size: var(--top-font);
  font-weight: 600;
}

.top-btn,
.icon-btn,
.text-btn {
  appearance: none;
  box-sizing: border-box;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: var(--top-radius);
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  font: inherit;
  font-size: var(--top-font);
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: var(--top-control-height);
  padding: 0 0.85rem;
  white-space: nowrap;
}

.icon-btn {
  width: var(--top-control-height);
  padding: 0;
  font-size: 0.95rem;
  font-weight: 500;
}

.top-btn:hover,
.icon-btn:hover {
  background: color-mix(in srgb, var(--line) 35%, var(--card));
}

/* Legacy plain text buttons outside the header (e.g. user table). */
.text-btn {
  border: 0;
  background: none;
  height: auto;
  padding: 0;
  border-radius: 0;
  font-size: 0.8rem;
}

.text-btn:hover { opacity: 0.8; }

.brand {
  font-weight: 750;
  font-size: 1.08rem;
  line-height: 1;
  color: var(--ink);
  text-decoration: none;
  letter-spacing: -0.04em;
  white-space: nowrap;
}

.search {
  display: flex;
  align-items: stretch;
  flex: 1 1 220px;
  max-width: 380px;
  min-width: 0;
  width: auto;
  height: var(--top-control-height);
  margin-left: auto;
  box-sizing: border-box;
  border: 1px solid var(--line);
  border-radius: var(--top-radius);
  background: var(--card);
  overflow: hidden;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.search:focus-within {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}

.field,
.login-card input,
.user-form input,
.user-form select,
.adv-field > .field,
.catalog-filter .field {
  height: var(--control-height);
  border: 1px solid var(--line);
  border-radius: var(--control-radius);
  padding: 0 var(--control-pad-x);
  font: inherit;
  font-size: 1rem;
  background: var(--card);
  color: var(--ink);
  box-sizing: border-box;
  -webkit-appearance: none;
  appearance: none;
}

.field::placeholder,
.login-card input::placeholder,
.user-form input::placeholder,
.adv-field .field::placeholder,
.catalog-filter .field::placeholder,
.field-combo input::placeholder {
  color: var(--muted);
}

.field::-webkit-search-decoration,
.field::-webkit-search-cancel-button,
.adv-field .field::-webkit-search-decoration,
.adv-field .field::-webkit-search-cancel-button,
.catalog-filter .field::-webkit-search-decoration,
.catalog-filter .field::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
}

.field:focus,
.login-card input:focus,
.user-form input:focus,
.user-form select:focus,
.adv-field > .field:focus,
.catalog-filter .field:focus,
.field-combo:focus-within {
  outline: none;
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}

.search input {
  flex: 1 1 auto;
  min-width: 0;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 0;
  padding: 0 0.75rem;
  margin: 0;
  font: inherit;
  font-size: var(--top-font);
  line-height: 1;
  background: transparent;
  color: var(--ink);
  outline: none;
  box-shadow: none;
  /* iOS Safari / Chromium search chrome can draw odd left edges. */
  -webkit-appearance: none;
  appearance: none;
}

.search input:focus {
  outline: none;
  box-shadow: none;
}

.search input::placeholder {
  color: var(--muted);
}

.search input::-webkit-search-decoration,
.search input::-webkit-search-cancel-button,
.search input::-webkit-search-results-button,
.search input::-webkit-search-results-decoration {
  -webkit-appearance: none;
  appearance: none;
  display: none;
}

.search button,
.btn,
.login-card button,
.user-form .btn {
  height: var(--control-height);
  border: 0;
  border-radius: var(--control-radius);
  padding: 0 1.2rem;
  background: var(--accent);
  color: var(--accent-ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

.search button {
  flex: 0 0 auto;
  height: auto;
  align-self: stretch;
  padding: 0 0.95rem;
  border: 0;
  border-left: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: var(--top-font);
}

.search button:hover {
  color: var(--ink);
  background: color-mix(in srgb, var(--line) 40%, transparent);
  filter: none;
}

.search-adv-toggle {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  align-self: stretch;
  width: 2rem;
  padding: 0;
  border: 0;
  border-left: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
}

.search-adv-toggle:hover,
.search-adv-toggle[aria-expanded="true"] {
  color: var(--ink);
  background: color-mix(in srgb, var(--line) 40%, transparent);
}

.adv-search {
  border-bottom: 1px solid var(--line);
  background: transparent;
  padding: 0.85rem max(1.25rem, env(safe-area-inset-right)) 1rem max(1.25rem, env(safe-area-inset-left));
}

.adv-search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem 0.9rem;
  max-width: 960px;
  margin: 0 auto;
}

.adv-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1 1 11rem;
  min-width: 0;
  font-size: 0.78rem;
  font-weight: 650;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--muted);
}

.adv-field > .field {
  width: 100%;
}

.adv-field-year {
  flex: 0 1 12rem;
}

.field-combo {
  display: flex;
  align-items: stretch;
  height: var(--control-height);
  border: 1px solid var(--line);
  border-radius: var(--control-radius);
  background: var(--card);
  overflow: hidden;
}

.field-combo input {
  flex: 1 1 0;
  min-width: 0;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 0;
  padding: 0 0.7rem;
  font: inherit;
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
  background: transparent;
  color: var(--ink);
  -webkit-appearance: none;
  appearance: none;
}

.field-combo input:focus {
  outline: none;
  box-shadow: none;
}

.field-combo-sep {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  padding: 0 0.1rem;
}

.adv-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  padding-bottom: 1px;
}

.adv-actions .btn {
  height: var(--control-height);
}

.btn:hover,
.login-card button:hover,
.user-form .btn:hover {
  filter: brightness(1.05);
}

.stats {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin: 0 0 0.35rem;
}

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem;
}

.panel { animation: fade 0.25s ease; }
.hidden { display: none !important; }

/* First paint for /?read= — hide catalog before app.js boots. */
html.boot-reader #site-header,
html.boot-reader #site-main {
  display: none !important;
}
html.boot-reader #reader {
  display: flex !important;
  flex-direction: column;
  position: fixed;
  inset: 0;
  height: auto;
  max-height: none;
  min-height: 0;
  overflow: hidden;
}
html.boot-reader body {
  background: var(--bg);
  overflow: hidden;
  height: 100%;
}

.lead { color: var(--muted); max-width: 40rem; line-height: 1.5; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 1rem;
}

.card {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease, border-color 0.2s ease;
}

.card:hover { transform: translateY(-2px); }

.card img {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  background: var(--cover-bg);
  display: block;
}

.card .meta {
  padding: 0.7rem 0.8rem 0.9rem;
}

.card .title {
  font-weight: 650;
  font-size: 0.95rem;
  line-height: 1.25;
  margin: 0 0 0.35rem;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.card .authors {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.card .card-rate {
  margin: 0.35rem 0 0;
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 650;
  letter-spacing: 0.01em;
}

.card .card-progress {
  color: var(--muted);
  font-weight: 600;
}

.book-card {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 1.5rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}

.cover-wrap img {
  width: 220px;
  height: 320px;
  object-fit: cover;
  border-radius: var(--control-radius);
  background: var(--cover-bg);
}

.book-meta .authors {
  font-size: 1.05rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
}
.series, .translators, .edition, .info, .genres { color: var(--muted); }
.translators, .edition { margin: 0.2rem 0 0; }
.author-series {
  margin: 0 0 1.1rem;
}

.author-series-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin: 0 0 0.45rem;
}

.author-series-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.author-series-chip {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  font: inherit;
  font-size: 0.9rem;
  border-radius: var(--control-radius);
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  text-align: left;
  max-width: 100%;
}

.author-series-chip:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
  color: var(--accent);
}

.author-series-chip .author-series-count {
  color: var(--muted);
  font-size: 0.82rem;
  margin-left: 0.35rem;
}

.author-series-chip.author-series-more {
  color: var(--muted);
  border-style: dashed;
}

.list-sort {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0 0 1.1rem;
}

.results-sub {
  margin: -0.4rem 0 1rem;
  color: var(--muted);
}
.edition-badge {
  position: absolute;
  top: 0.45rem;
  right: 0.45rem;
  z-index: 1;
  padding: 0.2rem 0.45rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  border: 1px solid var(--line);
  color: var(--ink);
  font-size: 0.72rem;
  line-height: 1.2;
  backdrop-filter: blur(4px);
}
.book-editions {
  margin: 1rem 0 0.25rem;
  max-width: 42rem;
}
.book-editions.hidden { display: none; }
.editions-heading {
  margin: 0 0 0.55rem;
  font-size: 0.95rem;
  font-weight: 600;
}
.editions-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.edition-row {
  display: block;
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: var(--control-radius);
  padding: 0.55rem 0.75rem;
  font: inherit;
  cursor: pointer;
}
.edition-row:hover {
  border-color: var(--accent);
}
.edition-row.is-current {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--card));
  cursor: default;
}
.edition-row .edition-main {
  display: block;
  font-weight: 500;
}
.edition-row .edition-sub {
  display: block;
  margin-top: 0.15rem;
  color: var(--muted);
  font-size: 0.88rem;
}
.results-pager {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.75rem 1rem;
  margin: 1.25rem 0 0.25rem;
}
.results-pager.hidden { display: none; }
.pager-status {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 10rem;
  text-align: center;
}
.pager-btn {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: var(--control-radius);
  padding: 0.45rem 0.85rem;
  font: inherit;
  cursor: pointer;
}
.pager-btn:hover:not(:disabled) {
  border-color: var(--accent);
}
.pager-btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.inline-link {
  border: 0;
  background: none;
  padding: 0;
  color: var(--accent);
  cursor: pointer;
  font: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.inline-link:hover { opacity: 0.85; }
.annotation {
  margin: 1rem 0 1.25rem;
  padding: 0.9rem 1rem;
  background: var(--annotation-bg);
  border: 1px solid var(--line);
  border-radius: var(--control-radius);
  color: var(--ink);
  line-height: 1.55;
  max-width: 42rem;
}
.annotation p { margin: 0 0 0.7rem; }
.annotation p:last-child { margin-bottom: 0; }
.genres { margin-bottom: 0.5rem; }

.book-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  margin: 0.85rem 0 0.75rem;
}

.btn-secondary {
  background: color-mix(in srgb, var(--line) 65%, var(--card));
  color: var(--ink);
}

.shelf-controls,
.shelf-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.35rem 0 0.5rem;
}

.shelf-pill {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--muted);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 0.4rem 0.8rem;
  border-radius: var(--pill-radius);
  cursor: pointer;
}

.shelf-pill:hover { color: var(--ink); }

.shelf-pill.is-active {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--card);
}

.shelf-clear { color: var(--muted); }

.continue-block {
  margin-top: 2rem;
}

.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;
}

.catalog-filter {
  display: block;
  margin: 0.85rem 0 0;
}

.catalog-filter .field {
  width: 100%;
  max-width: 28rem;
}

.letter-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin: 0.75rem 0 1rem;
  /* Keep one row reserved so genres ↔ authors does not jump the page. */
  min-height: 2rem;
}

.letter-strip.is-filtered .letter-btn.is-active {
  background: var(--card);
  border-color: var(--line);
  color: var(--muted);
}

.letter-strip.is-idle {
  visibility: hidden;
  pointer-events: none;
}

.letter-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--muted);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  min-width: 2rem;
  height: 2rem;
  padding: 0 0.45rem;
  border-radius: var(--control-radius);
  cursor: pointer;
}

.letter-btn:hover { color: var(--ink); }

.letter-btn.is-active {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--card);
}

.catalog-body {
  position: relative;
  min-height: 12rem;
}

.catalog-loading {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  color: var(--muted);
  font-size: 0.95rem;
  background: color-mix(in srgb, var(--card) 92%, transparent);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.catalog-spinner {
  width: 1.15rem;
  height: 1.15rem;
  border: 2px solid color-mix(in srgb, var(--accent) 25%, var(--line));
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: catalog-spin 0.7s linear infinite;
}

@keyframes catalog-spin {
  to { transform: rotate(360deg); }
}

.catalog-list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-height: 12rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

.catalog-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  font: inherit;
  text-align: left;
  padding: 0.7rem 1rem;
  cursor: pointer;
}

.catalog-row:last-child { border-bottom: 0; }
.catalog-row:hover { background: color-mix(in srgb, var(--line) 35%, transparent); }

.catalog-name {
  font-weight: 600;
  min-width: 0;
}

.catalog-count {
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 0.85rem;
}

.section-head h2 {
  margin: 0;
  font-size: 1.15rem;
}

.reader {
  min-height: 100vh;
}

.reader-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.65rem;
  border-bottom: 1px solid var(--line);
  background: var(--header-bg);
  backdrop-filter: blur(8px);
}

.reader-title {
  flex: 1;
  min-width: 0;
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reader-tools {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex-wrap: nowrap;
  justify-content: flex-end;
  flex-shrink: 0;
}

.reader-stage {
  position: relative;
}

.reader-viewport {
  min-width: 0;
}

.reader-page-nav {
  display: none;
}

.reader-tools .reader-ico {
  width: 1.15rem;
  height: 1.15rem;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.reader-tools .reader-ico-pages-h,
.reader-tools .reader-ico-pages-v,
.reader-tools .reader-ico-scroll {
  fill: none;
}
.reader-tools .reader-ico-pages-h rect,
.reader-tools .reader-ico-pages-v rect,
.reader-tools .reader-ico-scroll rect {
  fill: color-mix(in srgb, var(--card) 55%, transparent);
  stroke: currentColor;
}
.reader-tools .reader-ico-scroll rect:last-child {
  fill: currentColor;
  stroke: none;
}
.reader-content {
  max-width: var(--reader-max);
  margin: 0 auto;
  padding: 1.5rem 1.25rem 4rem;
  line-height: 1.7;
  font-size: 1rem;
  text-align: left;
  hyphens: manual;
}

body.reader-align-justify .reader-content {
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
  hyphenate-limit-chars: 6 3 3;
}

body.reader-align-justify .reader-content .chapter,
body.reader-align-justify .reader-content h3,
body.reader-align-justify .reader-content .fb2-img {
  text-align: start;
  hyphens: none;
}

.reader-content .chapter {
  margin: 2rem 0 1rem;
  font-size: 1.35em;
  line-height: 1.3;
}

.reader-content h3 {
  margin: 1.4rem 0 0.7rem;
  font-size: 1.1em;
}

.reader-content p { margin: 0 0 0.9rem; }

.reader-toc-bar {
  display: flex;
  align-items: center;
  gap: 0.1rem;
  flex: 0 0 auto;
  padding: 0.2rem 0.4rem 0.3rem;
  border-top: 1px solid var(--line);
  background: var(--header-bg);
  backdrop-filter: blur(8px);
  z-index: 20;
}
.reader-toc-bar .reader-ico {
  width: 1.15rem;
  height: 1.15rem;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
}
.reader-toc-now {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  padding: 0.4rem 0.35rem;
  cursor: pointer;
  text-align: left;
}
.reader-toc-title {
  flex: 1;
  min-width: 0;
  font-size: 0.82rem;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.reader-toc-pct {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}
.toc-pop-card {
  max-height: min(75dvh, 36rem);
}
.toc-pop-list {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  max-height: min(60dvh, 28rem);
  overflow: auto;
  margin: 0 -0.25rem;
}
.toc-item {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
  line-height: 1.35;
  text-align: left;
  padding: 0.55rem 0.65rem;
  cursor: pointer;
}
.toc-item.is-current {
  color: var(--accent);
  font-weight: 650;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

.reader-content a.fb2-note-ref,
.reader-content a.fb2-ref {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dotted currentColor;
  cursor: pointer;
  font-weight: 650;
}
.reader-content a.fb2-note-ref {
  font-size: 0.78em;
  vertical-align: super;
  line-height: 0;
  padding: 0 0.05em;
}
.reader-content a.fb2-ext {
  color: var(--accent);
}

.note-pop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0 0.75rem calc(0.75rem + env(safe-area-inset-bottom, 0px));
}
.note-pop.hidden {
  display: none;
}
.note-pop-backdrop {
  position: absolute;
  inset: 0;
  border: 0;
  padding: 0;
  margin: 0;
  background: rgba(0, 0, 0, 0.45);
  cursor: pointer;
}
.note-pop-card {
  position: relative;
  width: min(36rem, 100%);
  max-height: min(70dvh, 32rem);
  overflow: auto;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 14px 14px 10px 10px;
  box-shadow: var(--shadow);
  padding: 0.85rem 1rem 1.1rem;
}
.note-pop-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.55rem;
}
.note-pop-title {
  flex: 1;
  font-weight: 700;
  font-size: 0.95rem;
}
.note-pop-body {
  font-size: 0.98em;
  line-height: 1.65;
}
.note-pop-body p {
  margin: 0 0 0.7rem;
}
.note-pop-body p:last-child {
  margin-bottom: 0;
}
@media (min-width: 720px) {
  .note-pop {
    align-items: center;
    padding: 1.5rem;
  }
  .note-pop-card {
    border-radius: 14px;
  }
}

.reader-content .fb2-img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1rem auto;
  border-radius: 4px;
}

.reader-content blockquote {
  margin: 0.8rem 0;
  padding-left: 1rem;
  border-left: 3px solid var(--line);
  color: var(--muted);
}

body.reading-mode {
  background: var(--bg);
  overflow: hidden;
  position: fixed;
  inset: 0;
  height: auto;
  max-height: none;
}

/* Scroll the article, not the window — tapping the toolbar otherwise
   jumps iOS/Android back to the start of the book. */
body.reading-mode #reader {
  display: flex;
  flex-direction: column;
  position: fixed;
  inset: 0;
  height: auto;
  max-height: none;
  min-height: 0;
  overflow: hidden;
}

body.reading-mode:not(.reader-pages) .reader-bar {
  position: relative;
  flex: 0 0 auto;
}

body.reading-mode:not(.reader-pages) .reader-stage {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

body.reading-mode:not(.reader-pages) .reader-viewport {
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* Fullscreen reading — fill the screen after Fullscreen API. */
:fullscreen body.reading-mode,
:-webkit-full-screen body.reading-mode {
  background: var(--bg);
}
:fullscreen #reader,
:-webkit-full-screen #reader,
:fullscreen body.reader-pages .reader,
:-webkit-full-screen body.reader-pages .reader {
  height: 100%;
  min-height: 100%;
}
:fullscreen html.reader-pages-lock,
:fullscreen html.reader-pages-lock body,
:-webkit-full-screen html.reader-pages-lock,
:-webkit-full-screen html.reader-pages-lock body {
  height: 100%;
}

/* Lock document scroll in page mode — nothing native should pan/scroll. */
html.reader-pages-lock,
html.reader-pages-lock body {
  overflow: hidden !important;
  overscroll-behavior: none;
  touch-action: none;
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  width: 100%;
}

html.reader-pages-lock body {
  position: fixed;
  inset: 0;
  height: auto;
  max-height: none;
  box-sizing: border-box;
}

body.reading-mode.reader-pages {
  overflow: hidden;
}

body.reader-pages .reader {
  height: 100%;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
  touch-action: none;
  box-sizing: border-box;
}

body.reader-pages .reader-bar {
  position: relative;
  flex: 0 0 auto;
  touch-action: manipulation;
}

body.reader-pages .reader-stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  touch-action: none;
}

/* Absolute fill so clientHeight is real (%, flex % was sometimes ~0). */
body.reader-pages .reader-viewport {
  position: absolute;
  inset: 0;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  touch-action: none;
}

body.reader-pages .reader-page-nav {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  opacity: 0.4;
  user-select: none;
  touch-action: manipulation;
}

body.reader-pages .reader-page-prev { left: 0; }
body.reader-pages .reader-page-next { right: 0; }

body.reader-pages .reader-page-nav:hover {
  opacity: 0.9;
  color: var(--ink);
  background: color-mix(in srgb, var(--line) 22%, transparent);
}

/* Tap text to hide chrome and use more of the screen. */
body.reader-pages.reader-chrome-hidden .reader-bar,
body.reader-pages.reader-chrome-hidden .reader-page-nav,
body.reader-pages.reader-chrome-hidden .reader-toc-bar {
  display: none;
}

/*
  Page modes (вбок / вниз): one vertical article, translateY page windows.
  "Вбок" only changes swipe axis — full-book CSS columns were too slow on phones.
*/
body.reader-pages .reader-content {
  box-sizing: border-box;
  width: min(var(--reader-max), 100%);
  height: auto;
  max-width: var(--reader-max);
  max-height: none;
  margin: 0;
  padding: 1.15rem 1.25rem 1.5rem;
  overflow: visible;
  overscroll-behavior: none;
  touch-action: none;
  will-change: transform;
  column-width: auto !important;
  column-count: auto !important;
}

body.reader-pages .reader-content .fb2-img {
  max-width: 100%;
  max-height: 45vh;
  object-fit: contain;
}

.reader-face-btn {
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.reader-face-btn.is-serif {
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic;
  font-weight: 600;
}

body[data-reader-font="serif"] .reader-content {
  font-feature-settings: "liga" 1, "kern" 1;
}

@media (max-width: 640px) {
  .reader-bar {
    gap: 0.3rem;
    padding: 0.35rem 0.4rem;
  }
  .reader-title {
    display: none;
  }
  .reader-tools {
    flex: 1 1 auto;
    justify-content: flex-end;
    gap: 0.25rem;
  }
  .reader-bar .top-btn {
    height: 32px;
    padding: 0 0.45rem;
    font-size: 0.78rem;
  }
  .reader-bar .icon-btn {
    width: 32px;
    height: 32px;
    font-size: 0.85rem;
  }
  body.reader-pages .reader-page-nav {
    width: 1.85rem;
    font-size: 1.35rem;
    opacity: 0.28;
  }
  body.reader-pages .reader-content {
    padding: 0.85rem 0.85rem 1.1rem;
  }
}

.linkish {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 0;
  background: none;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
  margin-bottom: 1rem;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 550;
  line-height: 1.2;
}

.linkish::before {
  content: "";
  flex: 0 0 auto;
  width: 0.42em;
  height: 0.42em;
  margin-top: 0.02em;
  border-left: 1.75px solid currentColor;
  border-bottom: 1.75px solid currentColor;
  transform: rotate(45deg);
}

.empty { color: var(--muted); }

@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.login-card {
  width: min(100%, 380px);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  position: relative;
}

.setup-card {
  width: min(100%, 520px);
}
.setup-card .field-row {
  display: flex;
  gap: 0.45rem;
  align-items: center;
  margin-top: 0.35rem;
}
.setup-card .field-row .field {
  flex: 1 1 auto;
  min-width: 0;
  margin-top: 0;
}
.setup-card .field-row .top-btn {
  flex: 0 0 auto;
}
.setup-card select.field {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
}
.lang-fieldset {
  margin: 0.85rem 0 0;
  padding: 0.75rem 0.85rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: var(--control-radius);
}
.lang-fieldset legend {
  padding: 0 0.35rem;
  font-weight: 600;
}
.setup-hint {
  margin: 0 0 0.55rem;
  font-size: 0.85rem;
}
.lang-fieldset .check {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0.25rem 0;
  font-weight: 400;
}
.lang-fieldset .check input {
  width: auto;
  margin: 0;
}
.setup-status {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 0.5rem 0 0;
}
.setup-creds {
  margin: 0.75rem 0 0;
  padding: 0.75rem 0.85rem;
  border-radius: var(--control-radius);
  background: color-mix(in srgb, var(--accent) 12%, var(--card));
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line));
  font-size: 0.9rem;
  line-height: 1.45;
}
.setup-creds .muted { color: var(--muted); font-size: 0.85rem; }
.setup-creds code {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.9em;
}

.login-card .icon-btn {
  position: absolute;
  top: 1rem;
  right: 1rem;
}

.login-card h1 { margin: 0 0 0.4rem; }
.login-card label {
  display: block;
  margin: 0.8rem 0;
  color: var(--muted);
  font-size: 0.9rem;
}
.login-card input {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
}
.login-card button[type="submit"] {
  width: 100%;
  margin-top: 0.75rem;
}
.error {
  color: var(--error-ink);
  background: var(--error-bg);
  border-radius: var(--control-radius);
  padding: 0.55rem 0.75rem;
  font-size: 0.9rem;
}
.ok-msg {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--card));
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line));
  border-radius: var(--control-radius);
  padding: 0.55rem 0.75rem;
  font-size: 0.9rem;
  margin: 0.5rem 0 1rem;
}
.user-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.55rem;
  margin: 1rem 0 1.25rem;
}
.user-form .field {
  min-width: 140px;
}
.user-form select.field {
  min-width: 150px;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) calc(50% - 3px), calc(100% - 12px) calc(50% - 3px);
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 2rem;
}
.users-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.users-table th, .users-table td {
  text-align: left;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
}
.users-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.9rem;
}
.users-table tr:last-child td { border-bottom: 0; }
.users-table .users-seen {
  color: var(--muted);
  font-size: 0.92rem;
  white-space: nowrap;
}
.users-table .actions { text-align: right; }

@media (max-width: 960px) {
  .top {
    --top-control-height: 40px;
    padding-left: max(0.85rem, env(safe-area-inset-left));
    padding-right: max(0.85rem, env(safe-area-inset-right));
  }
  .search {
    order: 3;
    flex: 1 1 100%;
    width: 100%;
    max-width: 100%;
    margin-left: 0;
  }
  .top-right {
    margin-left: auto;
  }
  /* 16px avoids iOS focus-zoom that shifts the sticky header sideways. */
  .search input,
  .field,
  .field-combo input,
  .login-card input,
  .user-form input,
  .user-form select {
    font-size: 16px;
  }
  .adv-search-form {
    max-width: none;
  }
  .adv-field-year {
    flex: 1 1 calc(50% - 0.45rem);
  }
  .catalog-filter .field {
    max-width: none;
  }
}

@media (max-width: 640px) {
  .top-left {
    flex: 1 1 auto;
    min-width: 0;
    flex-wrap: wrap;
    gap: 0.5rem 0.85rem;
  }
  .nav {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  main {
    padding: 1rem max(1rem, env(safe-area-inset-right)) calc(1.25rem + env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  }
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  }
}

@media (max-width: 720px) {
  .book-card {
    grid-template-columns: 1fr;
  }
  .cover-wrap img {
    width: 160px;
    height: 240px;
  }
  .user-form .field,
  .user-form .btn {
    flex: 1 1 100%;
  }
}
