/* =========================================================
   3NDING — single stylesheet (library, favorites, reader, PWA, streaks)
   Fonts are loaded from Google Fonts in each page's <head>:
   Literata (serif), Inter (sans), JetBrains Mono (mono).
   The stacks below fall back to good system fonts if offline.
   ========================================================= */
:root {
  --bg: #f5f6f8;
  --surface: #fff;
  --ink: #15181e;
  --muted: #5f6877;
  --line: #e3e6eb;
  --accent: #2f3fa6;
  --accent-ink: #fff;
  --fav: #d6336c;
  --ok: #1f9d6b;
  --flame: #e8590c;
  --radius: 16px;
  --gutter: 20px;
  --tabbar: 64px;
  --shadow:
    0 1px 2px rgba(20, 24, 40, 0.06), 0 10px 28px rgba(20, 24, 40, 0.08);
  --serif:
    "Literata", "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans:
    "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue",
    sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #0f1218;
    --surface: #181c25;
    --ink: #eceff4;
    --muted: #9aa4b5;
    --line: #272d39;
    --accent: #8e9cff;
    --accent-ink: #0f1218;
    --flame: #ff922b;
    --shadow: 0 10px 28px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
  font-optical-sizing: auto;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  min-height: 100dvh;
  /* Safety net: nothing may ever make the page scroll sideways */
  overflow-x: clip;
  -webkit-tap-highlight-color: transparent;
}
body.panel-open {
  overflow: hidden;
}
h1,
h2,
h3 {
  text-wrap: balance;
}
img {
  display: block;
  max-width: 100%;
}
a {
  color: inherit;
}
button,
input,
select {
  font: inherit;
  color: inherit;
}
button,
a,
input,
select {
  touch-action: manipulation;
}
button {
  cursor: pointer;
}
[hidden] {
  display: none !important;
}
:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 8px;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Header + navigation ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: calc(14px + env(safe-area-inset-top))
    max(var(--gutter), env(safe-area-inset-right)) 14px
    max(var(--gutter), env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand {
  font: 700 1.35rem var(--serif);
  text-decoration: none;
  letter-spacing: -0.01em;
}
.site-header nav {
  display: flex;
  gap: 4px;
}
.site-header nav a {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 999px;
  text-decoration: none;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.95rem;
}
.site-header nav a:hover {
  color: var(--ink);
}
.site-header nav a[aria-current="page"] {
  background: var(--ink);
  color: var(--bg);
}
/* Icons only appear in the mobile tab bar (the streak chip keeps its flame) */
.site-header nav .ico {
  display: none;
}
.site-header nav a.streak-chip .ico {
  display: inline-grid;
}
.site-header nav a.streak-chip .lbl {
  display: none;
}
.site-header nav .ico svg {
  width: 16px;
  height: 16px;
}
.site-header nav a.streak-chip {
  gap: 6px;
  color: var(--flame);
  font-variant-numeric: tabular-nums;
}
.site-header nav a.streak-chip svg {
  fill: currentColor;
  fill-opacity: 0.25;
}
.site-header nav a.streak-chip.idle {
  color: var(--muted);
}
.site-header nav a.streak-chip.idle svg {
  fill: none;
}
#fav-count {
  min-width: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--fav);
  color: #fff;
  font-size: 0.75rem;
  line-height: 20px;
  text-align: center;
}

main.page {
  max-width: 1180px;
  margin: 0 auto;
  padding: 32px max(var(--gutter), env(safe-area-inset-right))
    calc(80px + env(safe-area-inset-bottom))
    max(var(--gutter), env(safe-area-inset-left));
}
.page-title {
  font: 700 clamp(1.9rem, 5vw, 3rem) / 1.1 var(--serif);
  letter-spacing: -0.02em;
  margin: 0 0 8px;
  overflow-wrap: anywhere;
}
.page-sub {
  margin: 0 0 28px;
  color: var(--muted);
  max-width: 52ch;
  text-wrap: pretty;
}
.section-title {
  font: 700 1.35rem var(--serif);
  margin: 0 0 14px;
}
section + section {
  margin-top: 44px;
}

/* ---------- Controls ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 24px;
}
.field {
  position: relative;
  flex: 1 1 260px;
}
.field svg {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}
.field input,
.toolbar select {
  width: 100%;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  padding: 0 14px;
}
.field input {
  padding-left: 42px;
}
.toolbar > label:not(.field) {
  flex: 0 0 auto;
}
.toolbar select {
  width: auto;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 40px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
  transition:
    background 0.15s,
    transform 0.1s;
}
.btn:hover {
  background: color-mix(in srgb, var(--ink) 6%, var(--surface));
}
.btn:active {
  transform: scale(0.97);
}
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn-primary:hover {
  background: color-mix(in srgb, var(--accent) 88%, #000);
}
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  color: var(--ink);
  text-decoration: none;
  flex: none;
  transition:
    background 0.15s,
    color 0.15s,
    transform 0.1s;
}
.icon-btn:hover {
  background: color-mix(in srgb, var(--ink) 8%, var(--surface));
}
.icon-btn:active {
  transform: scale(0.92);
}
.icon-btn.on {
  color: var(--fav);
  border-color: color-mix(in srgb, var(--fav) 45%, var(--line));
}
.icon-btn.on svg {
  fill: currentColor;
}
@media (pointer: coarse) {
  .btn,
  .icon-btn {
    min-height: 44px;
  }
  .icon-btn {
    min-width: 44px;
  }
}

.progress {
  height: 6px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}
.progress > i {
  display: block;
  height: 100%;
  width: var(--p, 0%);
  background: var(--accent);
  border-radius: inherit;
  transition: width 0.3s;
}
.progress.done > i {
  background: var(--ok);
}

/* ---------- Offline download buttons ---------- */
.offline-btn[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.offline-btn[aria-busy="true"] {
  cursor: progress;
}
.ring {
  width: 22px;
  height: 22px;
  transform: rotate(-90deg);
}
.ring circle {
  fill: none;
  stroke: currentColor;
  stroke-width: 3.5;
}
.ring .trk {
  opacity: 0.28;
}
.ring .val {
  stroke-linecap: round;
  stroke-dasharray: 88;
  transition: stroke-dashoffset 0.2s linear;
}
.ring.spin {
  animation: ring-spin 0.9s linear infinite;
}
@keyframes ring-spin {
  to {
    transform: rotate(270deg);
  }
}
/* Offline: books that aren't saved can't open, so say so visually */
body.is-offline .card:not(.saved),
body.is-offline .fav-card:not(.saved) {
  opacity: 0.55;
}

/* ---------- Book grid / cards ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 28px 22px;
  list-style: none;
  margin: 0;
  padding: 0;
}
.card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  transition: opacity 0.2s;
}
.card > div:not(.actions) {
  min-width: 0;
}
.cover-wrap {
  position: relative;
}
.cover {
  position: relative;
  display: block;
  aspect-ratio: 3 / 4;
  border-radius: 12px;
  overflow: hidden;
  background: linear-gradient(135deg, var(--line), var(--surface));
  box-shadow: var(--shadow);
}
.cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.4s;
}
.cover img.broken {
  visibility: hidden;
}
@media (hover: hover) {
  .card:hover .cover img {
    transform: scale(1.04);
  }
}
.cover .progress {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  height: 5px;
  background: rgba(255, 255, 255, 0.45);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15);
}
/* Round tools that sit on the cover: favorite (left) and offline (right) */
.chip {
  position: absolute;
  top: 8px;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(14, 17, 24, 0.6);
  color: #fff;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
  transition:
    background 0.15s,
    transform 0.1s;
}
/* 44px touch target without a bigger-looking button */
.chip::after {
  content: "";
  position: absolute;
  inset: -5px;
}
.chip:active {
  transform: scale(0.9);
}
.chip-fav {
  left: 8px;
}
.chip-off {
  right: 8px;
}
.chip.on {
  background: var(--fav);
}
.chip.on svg {
  fill: currentColor;
}
.chip.offline-btn[aria-pressed="true"] {
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35);
}
.card h3 {
  margin: 0;
  font: 700 1.05rem/1.25 var(--serif);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}
.card .meta {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.card .actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: auto;
}
.card .actions .btn {
  flex: 1;
  min-width: 0;
}

/* ---------- Continue rail ---------- */
.rail {
  display: flex;
  gap: 16px;
  overflow-x: auto;
  padding: 4px 2px 14px;
  scroll-snap-type: x proximity;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
  list-style: none;
  margin: 0;
}
.rail-item {
  flex: 0 0 min(340px, 86%);
  display: grid;
  grid-template-columns: 84px 1fr;
  gap: 16px;
  padding: 14px;
  scroll-snap-align: start;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.rail-item > div {
  min-width: 0;
}
.rail-item .cover {
  box-shadow: none;
  border-radius: 8px;
}
.rail-item h3 {
  margin: 0;
  font: 700 1rem/1.25 var(--serif);
  overflow-wrap: anywhere;
}
.rail-item p {
  margin: 2px 0 10px;
  color: var(--muted);
  font-size: 0.85rem;
  overflow-wrap: anywhere;
}
.rail-item small {
  display: block;
  margin: 6px 0 10px;
  color: var(--muted);
}
.rail-item .btn {
  width: 100%;
}

/* ---------- Streak + weekly summary ---------- */
#streak-section {
  margin-bottom: 40px;
  scroll-margin-top: 60px;
}
.streak-card {
  display: grid;
  gap: 20px;
  max-width: 860px;
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.streak-card > * {
  min-width: 0;
}
.streak-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 16px;
}
.streak-flame {
  display: grid;
  place-items: center;
  flex: none;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--muted) 14%, transparent);
  color: var(--muted);
}
.streak-flame.on {
  background: color-mix(in srgb, var(--flame) 16%, transparent);
  color: var(--flame);
}
.streak-flame.on svg {
  fill: currentColor;
  fill-opacity: 0.3;
}
.streak-num {
  margin: 0;
  color: var(--muted);
  font-weight: 600;
}
.streak-num strong {
  margin-right: 4px;
  font: 700 2rem/1 var(--serif);
  color: var(--ink);
}
.streak-msg {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  text-wrap: pretty;
}
.streak-sub {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}

/* Last 7 days: one series, bars rise from a shared baseline */
.wk {
  margin: 0;
}
.wk-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  font-size: 0.82rem;
}
.wk-head strong {
  font-weight: 700;
}
.wk-key {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  white-space: nowrap;
}
.wk-key::before {
  content: "";
  width: 16px;
  border-top: 2px dashed color-mix(in srgb, var(--muted) 70%, transparent);
}
.wk-plot {
  position: relative;
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 8px;
  height: 96px;
  border-bottom: 1px solid var(--line);
}
.wk-goal {
  position: absolute;
  left: 0;
  right: 0;
  bottom: var(--g, 0%);
  border-top: 2px dashed color-mix(in srgb, var(--muted) 55%, transparent);
  pointer-events: none;
}
.wk-day {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  height: 100%;
}
.wk-bar {
  display: block;
  width: 100%;
  max-width: 30px;
  height: var(--h, 0%);
  min-height: 3px;
  border-radius: 4px 4px 0 0;
  background: color-mix(in srgb, var(--accent) 34%, var(--line));
}
.wk-day.met .wk-bar {
  background: var(--accent);
}
.wk-day.zero .wk-bar {
  background: var(--line);
}
.wk-labels {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 8px;
  margin: 6px 0 0;
  padding: 0;
  list-style: none;
  text-align: center;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
}
.wk-labels .today {
  color: var(--ink);
  font-weight: 700;
}
.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 16px 0 0;
}
.stat {
  padding: 10px 12px;
  background: var(--bg);
  border-radius: 12px;
}
.stat dt {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
}
.stat dd {
  margin: 2px 0 0;
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}
.stat small {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.65rem;
  line-height: 1.3;
}
.sc-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.85rem;
}
.sc-foot p {
  margin: 0;
}
.goal-pick {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}
.goal-pick select {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}
.install {
  width: 100%;
  height: 44px;
}
.install-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}
@media (min-width: 760px) {
  #streak-section {
    scroll-margin-top: 80px;
  }
  .streak-card {
    grid-template-columns: minmax(0, 5fr) minmax(0, 6fr);
    gap: 24px 40px;
    padding: 24px;
  }
  .sc-main {
    align-self: center;
    padding: 0 32px;
  }
  .sc-foot,
  .install,
  .install-hint {
    grid-column: 1 / -1;
  }
}

/* ---------- Favorites ---------- */
.tabs {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  margin-bottom: 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.tabs button {
  border: 0;
  background: none;
  padding: 8px 18px;
  border-radius: 999px;
  font-weight: 600;
  color: var(--muted);
}
.tabs button[aria-pressed="true"] {
  background: var(--ink);
  color: var(--bg);
}
.fav-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 440px), 1fr));
  gap: 20px;
  list-style: none;
  padding: 0;
  margin: 0;
}
.fav-card {
  display: grid;
  grid-template-columns: 112px 1fr;
  /* cover and its offline button stack in the left column; text spans both */
  grid-template-areas:
    "cover text"
    "tool text";
  grid-template-rows: auto 1fr;
  gap: 8px 20px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 0;
  transition: opacity 0.2s;
}
.fav-card > div {
  grid-area: text;
  min-width: 0;
}
.fav-card > .cover {
  grid-area: cover;
}
.fav-card > .offline-btn {
  grid-area: tool;
  justify-self: center;
}
.fav-card h3 {
  margin: 0;
  font: 700 1.2rem/1.25 var(--serif);
  overflow-wrap: anywhere;
}
.fav-card .meta {
  margin: 2px 0 14px;
  color: var(--muted);
  font-size: 0.9rem;
}
.fav-card .status {
  display: flex;
  justify-content: space-between;
  margin: 8px 0 14px;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}
.fav-card .row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.fav-card .row .btn + .icon-btn {
  margin-left: auto;
}
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--ok) 18%, transparent);
  color: var(--ok);
  font-size: 0.78rem;
  font-weight: 700;
}
.more-btn {
  display: flex;
  margin: 24px auto 0;
}
.empty {
  padding: 56px 20px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}
.empty h2 {
  font: 700 1.4rem var(--serif);
  color: var(--ink);
  margin: 0 0 8px;
}
.empty .btn {
  margin-top: 16px;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: 0.25s;
  z-index: 100;
}
#toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- Reader themes ---------- */
:root[data-theme="paper"] {
  --bg: #fbfbf8;
  --surface: #fff;
  --ink: #1c1d20;
  --muted: #6a6e76;
  --line: #e4e4de;
  --accent: #2f3fa6;
  --accent-ink: #fff;
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #f0e4cb;
  --surface: #f7edd8;
  --ink: #46372a;
  --muted: #85715a;
  --line: #dccaa6;
  --accent: #8a5a2b;
  --accent-ink: #fff;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #1c2027;
  --surface: #262b34;
  --ink: #d4d8df;
  --muted: #8e97a6;
  --line: #343a46;
  --accent: #8e9cff;
  --accent-ink: #14161c;
  color-scheme: dark;
}
:root[data-theme="oled"] {
  --bg: #000;
  --surface: #0d0d0f;
  --ink: #bcbfc6;
  --muted: #777c86;
  --line: #202228;
  --accent: #8e9cff;
  --accent-ink: #000;
  color-scheme: dark;
}

/* ---------- Reader ---------- */
body.reader {
  --fs: 18px;
  --measure: 40rem;
  transition:
    background 0.25s,
    color 0.25s;
  touch-action: manipulation;
}
.r-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(10px + env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right)) 12px
    max(12px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(12px);
}
.r-bar .icon-btn {
  border-color: transparent;
  background: transparent;
}
.r-bar .icon-btn:hover {
  background: color-mix(in srgb, var(--ink) 10%, transparent);
}
.r-title {
  flex: 1;
  min-width: 0;
  text-align: center;
  line-height: 1.2;
}
.r-title strong {
  display: block;
  font: 700 0.95rem var(--serif);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.r-title span {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.r-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: var(--line);
}
.r-progress i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.15s linear;
}

#text {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 48px max(22px, env(safe-area-inset-right))
    calc(64px + env(safe-area-inset-bottom))
    max(22px, env(safe-area-inset-left));
  font-size: var(--fs);
  line-height: 1.75;
  outline: none;
}
html[data-font="serif"] #text {
  font-family: var(--serif);
}
html[data-font="sans"] #text {
  font-family: var(--sans);
  line-height: 1.7;
}
html[data-font="mono"] #text {
  font-family: var(--mono);
  line-height: 1.7;
  font-size: calc(var(--fs) * 0.88);
}
html[data-spacing="compact"] #text {
  line-height: 1.55;
}
html[data-spacing="relaxed"] #text {
  line-height: 1.95;
}
html[data-width="narrow"] body.reader {
  --measure: 32rem;
}
html[data-width="wide"] body.reader {
  --measure: 52rem;
}
html[data-align="justify"] #text p {
  text-align: justify;
  hyphens: auto;
}
#text h1 {
  font: 700 clamp(1.35em, 6vw, 1.7em) / 1.2 var(--serif);
  margin: 0 0 1.4em;
  letter-spacing: -0.01em;
  text-align: left;
  overflow-wrap: anywhere;
}
#text .r-eyebrow {
  margin: 0 0 0.6em;
  font: 700 0.72rem/1 var(--sans);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent);
  white-space: normal;
}
#text p {
  margin: 0 0 1.1em;
  white-space: pre-line;
  overflow-wrap: break-word;
}
.r-state {
  max-width: var(--measure);
  margin: 20vh auto;
  padding: 0 22px;
  text-align: center;
  color: var(--muted);
}

/* Chapter navigation: a fixed bar that tucks away with the header on phones */
.r-foot {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 18;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px
    max(22px, calc((100% - var(--measure)) / 2), env(safe-area-inset-right))
    calc(10px + env(safe-area-inset-bottom))
    max(22px, calc((100% - var(--measure)) / 2), env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.r-foot span {
  flex: 1;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.35;
  text-align: center;
  font-variant-numeric: tabular-nums;
  text-wrap: balance;
}
.r-foot .btn {
  min-width: 104px;
  white-space: nowrap;
}
.r-foot .btn:disabled {
  opacity: 0.35;
  pointer-events: none;
}

.scrim {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}
.scrim.show {
  opacity: 1;
  pointer-events: auto;
}
.panel {
  position: fixed;
  z-index: 31;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(380px, 100%);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: calc(20px + env(safe-area-inset-top)) 20px
    calc(24px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow);
  transform: translateX(100%);
  visibility: hidden;
  transition:
    transform 0.28s,
    visibility 0.28s;
}
.panel.open {
  transform: none;
  visibility: visible;
}
.panel header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.panel h2 {
  margin: 0;
  font: 700 1.2rem var(--serif);
}
.hint {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 0.82rem;
}
.group {
  margin-bottom: 22px;
}
.group > span {
  display: block;
  margin-bottom: 8px;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--muted);
}
.seg {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 6px;
}
.seg button {
  height: 42px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-weight: 600;
}
.seg button[aria-pressed="true"] {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
  color: var(--accent);
}
.seg.themes button {
  border-width: 2px;
}
.seg.themes button[data-val="paper"] {
  background: #fbfbf8;
  color: #1c1d20;
}
.seg.themes button[data-val="sepia"] {
  background: #f0e4cb;
  color: #46372a;
}
.seg.themes button[data-val="dark"] {
  background: #1c2027;
  color: #d4d8df;
}
.seg.themes button[data-val="oled"] {
  background: #000;
  color: #bcbfc6;
}
.seg.themes button[aria-pressed="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}
.size {
  display: flex;
  align-items: center;
  gap: 12px;
}
.size input {
  flex: 1;
  min-width: 0;
  accent-color: var(--accent);
}
.size output {
  min-width: 3.2ch;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.toc {
  list-style: none;
  padding: 0;
  margin: 0;
}
.toc button {
  display: flex;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border: 0;
  border-radius: 10px;
  background: none;
  text-align: left;
  overflow-wrap: anywhere;
}
.toc button:hover {
  background: var(--bg);
}
.toc button[aria-current="true"] {
  background: var(--bg);
  color: var(--accent);
  font-weight: 700;
}
.toc em {
  font-style: normal;
  color: var(--muted);
  min-width: 1.6em;
}

#exit-fs {
  position: fixed;
  top: calc(12px + env(safe-area-inset-top));
  right: 12px;
  z-index: 25;
  display: none;
  background: var(--surface);
  box-shadow: var(--shadow);
}
body.immersive .r-bar {
  display: none;
}
body.immersive #exit-fs {
  display: inline-grid;
  opacity: 0.55;
}
body.immersive #text {
  padding-top: calc(32px + env(safe-area-inset-top));
}

/* ---------- Site footer (library pages) ---------- */
body:has(.site-footer) {
  display: flex;
  flex-direction: column;
}
body:has(.site-footer) main.page {
  flex: 1 0 auto;
  width: 100%;
  padding-bottom: 48px;
}
.site-footer {
  margin-top: auto;
  padding: 36px max(var(--gutter), env(safe-area-inset-right))
    calc(28px + env(safe-area-inset-bottom))
    max(var(--gutter), env(safe-area-inset-left));
  background: var(--surface);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.9rem;
}
.sf-inner,
.sf-base {
  max-width: 1180px;
  margin: 0 auto;
}
.sf-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px 48px;
}
.sf-brand {
  max-width: 34ch;
}
.sf-brand .brand {
  display: inline-block;
  color: var(--ink);
  margin-bottom: 6px;
}
.sf-brand p {
  margin: 0;
  text-wrap: pretty;
}
.sf-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 24px;
}
.sf-links a {
  color: var(--ink);
  font-weight: 600;
  text-decoration: none;
}
.sf-links a:hover {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 4px;
}
.sf-base {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 6px 24px;
  margin-top: 28px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  font-size: 0.82rem;
}
.sf-base p {
  margin: 0;
}

/* ---------- Pagination ---------- */
#all-title {
  scroll-margin-top: 84px;
  outline: none;
}
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 36px;
}
.pager .pg {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 40px;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 600;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
  transition:
    background 0.15s,
    transform 0.1s;
}
.pager .pg:hover:not(:disabled) {
  background: color-mix(in srgb, var(--ink) 8%, var(--surface));
}
.pager .pg:active:not(:disabled) {
  transform: scale(0.96);
}
.pager .pg[aria-current="page"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--bg);
}
.pager .pg:disabled {
  opacity: 0.4;
  cursor: default;
}
.pager .gap {
  padding: 0 4px;
  color: var(--muted);
}
.pager .pg-info {
  display: none;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}
.pager-count {
  flex: 1 0 100%;
  order: 2;
  margin: 10px 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}
@media (pointer: coarse) {
  .pager .pg {
    min-height: 44px;
  }
}

/* =========================================================
   Phones (≤ 560px): tab bar, 2-up grid, compact cards
   ========================================================= */
@media (max-width: 560px) {
  :root {
    --gutter: 16px;
  }
  main.page {
    padding-top: 20px;
    padding-bottom: calc(var(--tabbar) + 32px + env(safe-area-inset-bottom));
  }
  .page-title {
    font-size: 1.85rem;
  }
  .page-sub {
    margin-bottom: 20px;
    font-size: 0.94rem;
  }
  .section-title {
    font-size: 1.2rem;
  }
  section + section {
    margin-top: 32px;
  }
  body:has(.site-footer) main.page {
    padding-bottom: 32px;
  }
  .site-footer {
    padding-top: 28px;
    padding-bottom: calc(var(--tabbar) + 24px + env(safe-area-inset-bottom));
  }

  /* Header keeps only the brand; navigation moves to a bottom tab bar.
     (backdrop-filter would make the fixed nav relative to the header.) */
  .site-header {
    padding-block: calc(12px + env(safe-area-inset-top)) 12px;
    background: var(--bg);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .brand {
    font-size: 1.2rem;
  }
  .site-header nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 15;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 2px;
    padding: 6px max(8px, env(safe-area-inset-right))
      calc(6px + env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
    background: var(--surface);
    border-top: 1px solid var(--line);
  }
  .site-header nav a {
    flex-direction: column;
    justify-content: center;
    gap: 2px;
    min-height: calc(var(--tabbar) - 12px);
    padding: 4px;
    border-radius: 12px;
    font-size: 0.72rem;
  }
  .site-header nav .ico {
    display: inline-grid;
  }
  .site-header nav .ico svg {
    width: 22px;
    height: 22px;
  }
  .site-header nav a[aria-current="page"] {
    background: color-mix(in srgb, var(--accent) 12%, transparent);
    color: var(--accent);
  }
  .site-header nav a.streak-chip {
    order: 3;
    gap: 2px;
    padding: 4px;
  }
  .site-header nav a.streak-chip .lbl {
    display: inline;
  }
  .site-header nav a.streak-chip .num,
  #fav-count {
    position: absolute;
    top: 3px;
    left: calc(50% + 7px);
    min-width: 18px;
    padding: 0 5px;
    border-radius: 999px;
    font-size: 0.66rem;
    font-weight: 700;
    line-height: 18px;
    text-align: center;
    box-shadow: 0 0 0 2px var(--surface);
  }
  .site-header nav a.streak-chip .num {
    background: var(--flame);
    color: #fff;
  }
  .site-header nav a.streak-chip.idle .num {
    display: none;
  }
  /* The on-screen keyboard would otherwise float the tab bar over the field */
  body:has(input:focus) .site-header nav {
    display: none;
  }
  body:not(.reader) #toast {
    bottom: calc(var(--tabbar) + 14px + env(safe-area-inset-bottom));
  }

  /* Streak card */
  #streak-section {
    margin-bottom: 32px;
  }
  .streak-card {
    padding: 16px;
    gap: 16px;
  }
  .streak-head {
    margin-bottom: 12px;
  }
  .streak-flame {
    width: 46px;
    height: 46px;
  }
  .streak-num strong {
    font-size: 1.75rem;
  }
  .stat {
    padding: 8px 9px;
  }
  .stat dd {
    font-size: 1rem;
  }
  .sc-foot {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .goal-pick {
    justify-content: space-between;
  }

  .toolbar {
    gap: 10px;
    margin-bottom: 20px;
  }
  .toolbar > label:not(.field) {
    flex: 1 1 100%;
  }
  .toolbar select {
    width: 100%;
  }

  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 22px 12px;
  }
  .card {
    gap: 10px;
  }
  .card h3 {
    font-size: 0.98rem;
  }
  .chip {
    top: 6px;
    width: 32px;
    height: 32px;
  }
  .chip-fav {
    left: 6px;
  }
  .chip-off {
    right: 6px;
  }
  .card .actions .btn {
    padding: 0 8px;
    font-size: 0.85rem;
  }
  .card .actions .btn svg {
    display: none;
  }

  .rail {
    margin-left: calc(-1 * max(var(--gutter), env(safe-area-inset-left)));
    margin-right: calc(-1 * max(var(--gutter), env(safe-area-inset-right)));
    padding-left: max(var(--gutter), env(safe-area-inset-left));
    padding-right: max(var(--gutter), env(safe-area-inset-right));
    scroll-padding-inline: max(var(--gutter), env(safe-area-inset-left));
  }
  .rail-item {
    flex-basis: min(340px, 92%);
    grid-template-columns: 72px 1fr;
    gap: 12px;
    padding: 12px;
  }
  .rail-item small {
    white-space: nowrap;
  }

  .tabs {
    display: flex;
    width: 100%;
  }
  .tabs button {
    flex: 1;
    padding: 8px 6px;
  }
  .fav-list {
    gap: 14px;
  }
  .fav-card {
    grid-template-columns: 72px 1fr;
    gap: 8px 12px;
    padding: 12px;
  }
  .fav-card h3 {
    font-size: 1.05rem;
  }
  .fav-card .meta {
    margin-bottom: 10px;
  }
  .fav-card .status {
    margin: 6px 0 12px;
  }
  .fav-card {
    position: relative;
  }
  .fav-card h3 {
    padding-right: 30px;
  }
  .fav-card [data-action="remove"] {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 40px;
    height: 40px;
    border-color: transparent;
    background: transparent;
    color: var(--muted);
  }
  .fav-card .row .btn-primary {
    flex: 1 1 100%;
  }
  .fav-card .row .btn:not(.btn-primary) {
    flex: 1 1 auto;
    padding: 0 12px;
  }

  /* Reader: no fullscreen button on phones; bar tucks away while reading;
     panels become bottom sheets */
  #btn-fs {
    display: none;
  }
  .r-bar,
  .r-foot {
    transition: transform 0.25s;
  }
  /* :has(:focus-visible) = keyboard focus only, so a tapped button can't pin the bar open */
  body.bar-hidden .r-bar:not(:has(:focus-visible)) {
    transform: translateY(-100%);
  }
  body.bar-hidden .r-foot:not(:has(:focus-visible)) {
    transform: translateY(100%);
  }
  #text {
    padding-top: 28px;
  }
  .r-foot .btn {
    min-width: 92px;
  }
  .panel {
    top: auto;
    left: 0;
    width: 100%;
    max-height: 85vh;
    max-height: 85dvh;
    padding-top: 12px;
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 20px 20px 0 0;
    transform: translateY(100%);
  }
  .panel::before {
    content: "";
    display: block;
    width: 40px;
    height: 4px;
    margin: 0 auto 12px;
    border-radius: 2px;
    background: var(--line);
  }
}
@media (max-width: 340px) {
  .stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }
  .stat small {
    font-size: 0.66rem;
  }
  .r-bar .icon-btn {
    width: 36px;
    height: 36px;
  }
}
@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
  .ring.spin {
    animation-duration: 2.4s;
  }
}
@media (display-mode: standalone) {
  body {
    overscroll-behavior-y: none;
  }
}
