*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

[hidden] {
  display: none !important;
}

/* Reader preferences are set on <html> by app.js, so they live on :root. */
:root {
  --font-ui: "Be Vietnam Pro", system-ui, sans-serif;
  --font-serif: "Literata", Georgia, "Times New Roman", serif;
  --font-sans: "Be Vietnam Pro", system-ui, sans-serif;
  --font-system: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-cjk: "Noto Serif JP", "Yu Mincho", "Hiragino Mincho ProN", "MS Mincho", serif;
  --reader-font-family: var(--font-serif);
  --reader-font-size: var(--text-xl);
  --reader-line-height: 2.02;
  --reader-width: 700px;

  /* Type scale: UI text uses these steps instead of one-off pixel sizes. */
  --text-2xs: 11px;
  --text-xs: 12px;
  --text-sm: 13px;
  --text-base: 14px;
  --text-md: 15px;
  --text-lg: 17px;
  --text-xl: 20px;
  --text-2xl: 24px;
  --text-3xl: 28px;

  /* Stacking layers, lowest first. Overlays opened with showModal() sit in
     the browser's top layer and need no value here. */
  --z-raised: 2;
  --z-dropdown: 30;
  --z-reader-bar: 65;
  --z-floating: 80;
  --z-header: 120;
  --z-bottom-nav: 150;
  --z-overlay-controls: 155;
  --z-toast: 170;
  --z-popover: 180;
  --z-top-progress: 200;
  --z-eink: 1880;
  --z-sheet: 1950;
  --z-loading: 2000;
  --z-lightbox: 2100;
}

/*
 * Theme tokens are declared on body (not :root) so that the derived
 * values below (lines, shadows) re-resolve against each theme's ink color.
 */
body {
  --ink-rgb: 30, 26, 21;
  --bg: #ebe5d8;
  --surface: #f4efe5;
  --surface-2: #e9e2d4;
  --raised: #fbf8f2;
  --paper: #f8f4eb;
  --text: #1e1a15;
  --text-soft: #4a443a;
  --text-muted: #6a6256;
  --ink: #1e1a15;
  --on-ink: #f8f4eb;
  --paper-text: #201c17;
  --paper-muted: #6a6256;
  --line: rgba(var(--ink-rgb), 0.11);
  --line-strong: rgba(var(--ink-rgb), 0.22);
  --shadow-sm: 0 1px 2px rgba(var(--ink-rgb), 0.08);
  --shadow-md: 0 1px 2px rgba(var(--ink-rgb), 0.05), 0 10px 30px -10px rgba(var(--ink-rgb), 0.16);
  --shadow-book: 0 1px 1px rgba(var(--ink-rgb), 0.1), 0 6px 12px -4px rgba(var(--ink-rgb), 0.2), 0 20px 36px -14px rgba(var(--ink-rgb), 0.34);
  --shadow-book-hover: 0 1px 1px rgba(var(--ink-rgb), 0.12), 0 10px 18px -6px rgba(var(--ink-rgb), 0.26), 0 28px 48px -16px rgba(var(--ink-rgb), 0.42);
  --shadow-pop: 0 2px 6px rgba(var(--ink-rgb), 0.06), 0 30px 60px -20px rgba(var(--ink-rgb), 0.35);
  --backdrop-opacity: 0.55;
  --img-tone: grayscale(1);
  --img-filter: grayscale(1) contrast(1.04) brightness(0.98);
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --header-h: 64px;
  --gutter: 28px;
  --shell: 1180px;
  /* E-ink motion: transitions jump in a few discrete frames, like a slow
     e-paper refresh. Turning the effect off makes them smooth instead. */
  --steps-2: steps(2, end);
  --steps-3: steps(3, end);
  --steps-4: steps(4, end);
  --steps-5: steps(5, end);
  --steps-6: steps(6, end);
  --steps-8: steps(8, end);
}

body[data-eink="off"] {
  --ease-smooth: cubic-bezier(0.2, 0, 0, 1);
  --steps-2: var(--ease-smooth);
  --steps-3: var(--ease-smooth);
  --steps-4: var(--ease-smooth);
  --steps-5: var(--ease-smooth);
  --steps-6: var(--ease-smooth);
  --steps-8: var(--ease-smooth);
}

body[data-theme="light"] {
  --ink-rgb: 22, 21, 19;
  --bg: #f2f1ee;
  --surface: #fbfaf8;
  --surface-2: #ecebe7;
  --raised: #ffffff;
  --paper: #ffffff;
  --text: #161513;
  --text-soft: #3e3c37;
  --text-muted: #64625c;
  --ink: #161513;
  --on-ink: #fbfaf8;
  --paper-text: #181715;
  --paper-muted: #64625c;
  --backdrop-opacity: 0.45;
}

/* Dark is true black, so OLED screens switch those pixels off. Pages,
   cards and panels share the black and are told apart by their borders;
   only small controls (hover, active buttons) get a faint grey. */
body[data-theme="dark"] {
  --ink-rgb: 236, 233, 225;
  --bg: #000;
  --surface: #000;
  --surface-2: #121213;
  --raised: #1c1c1e;
  --paper: #000;
  --text: #ece9e1;
  --text-soft: #c4c0b6;
  --text-muted: #9a968c;
  --ink: #ece9e1;
  --on-ink: #000;
  --paper-text: #e3dfd5;
  --paper-muted: #9a968c;
  --line: rgba(var(--ink-rgb), 0.14);
  --line-strong: rgba(var(--ink-rgb), 0.24);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 32px -12px rgba(0, 0, 0, 0.6);
  --shadow-book: 0 1px 1px rgba(0, 0, 0, 0.4), 0 6px 12px -4px rgba(0, 0, 0, 0.5), 0 20px 36px -14px rgba(0, 0, 0, 0.7);
  --shadow-book-hover: 0 1px 1px rgba(0, 0, 0, 0.45), 0 10px 18px -6px rgba(0, 0, 0, 0.6), 0 28px 48px -16px rgba(0, 0, 0, 0.8);
  --shadow-pop: 0 2px 6px rgba(0, 0, 0, 0.3), 0 30px 60px -20px rgba(0, 0, 0, 0.8);
  --backdrop-opacity: 0.26;
  --img-filter: grayscale(1) contrast(1.05) brightness(0.84);
}

/* Colour art is opt-out: the UI stays monochrome, only the artwork gets colour. */
body[data-images="color"] {
  --img-tone: saturate(1.05);
  --img-filter: none;
}

body[data-theme="dark"][data-images="color"] {
  --img-filter: brightness(0.9);
}

/* The paper grain would light up specks of an OLED screen. */
body[data-theme="dark"] .page-noise {
  display: none;
}

/* Larger panels and control tracks: black with a border, like the cards. */
body[data-theme="dark"] .listen-bar {
  background: var(--surface);
}

body[data-theme="dark"] .toolbar-group,
body[data-theme="dark"] .segmented,
body[data-theme="dark"] .settings-font-row {
  background: var(--surface);
  box-shadow: inset 0 0 0 1px var(--line);
}

html {
  font-size: 16px;
  scroll-behavior: auto;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  font-family: var(--font-ui);
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background-color 0.3s var(--steps-4), color 0.3s var(--steps-4);
}

img {
  filter: var(--img-filter);
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

::selection {
  background: rgba(var(--ink-rgb), 0.16);
}

.page-noise {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.5;
  background-image:
    radial-gradient(circle at 20% 20%, rgba(var(--ink-rgb), 0.05) 0 1px, transparent 1px),
    radial-gradient(circle at 70% 35%, rgba(var(--ink-rgb), 0.035) 0 1px, transparent 1px);
  background-size: 30px 30px, 38px 38px;
  z-index: -1;
}

/* E-ink page turn */

#eink-ghost {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  z-index: var(--z-eink);
  overflow: hidden;
  mix-blend-mode: multiply;
}

body[data-theme="dark"] #eink-ghost {
  mix-blend-mode: screen;
}

#eink-ghost.is-active {
  animation: einkGhost 480ms var(--steps-6) forwards;
}

#eink-ghost .ghost-header {
  position: absolute;
  inset: 0 0 auto 0;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  opacity: 0.28;
  filter: grayscale(1) contrast(0.82);
}

#eink-ghost .ghost-main {
  position: absolute;
  inset: 0;
  min-height: 100vh;
  padding-top: calc(var(--header-h) + 36px);
  padding-bottom: 72px;
  opacity: 0.2;
  filter: grayscale(1) contrast(0.8) blur(0.35px);
}

#eink-refresh {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  z-index: calc(var(--z-eink) + 20);
  background:
    linear-gradient(180deg, rgba(var(--ink-rgb), 0.12), rgba(var(--ink-rgb), 0.02)),
    repeating-linear-gradient(180deg, rgba(var(--ink-rgb), 0.045) 0 2px, transparent 2px 4px);
}

body.is-eink-refreshing #eink-refresh {
  animation: einkRefresh 460ms var(--steps-6);
}

body.is-eink-refreshing #header,
body.is-eink-refreshing .view.active,
body.is-eink-refreshing #progress-fill {
  filter: grayscale(1) contrast(0.92);
}

body.is-eink-busy button,
body.is-eink-busy select {
  pointer-events: none;
}

@keyframes einkRefresh {
  0% { opacity: 0; }
  8% { opacity: 0.52; }
  18% { opacity: 0.08; }
  34% { opacity: 0.34; }
  52% { opacity: 0.06; }
  68% { opacity: 0.22; }
  100% { opacity: 0; }
}

@keyframes einkGhost {
  0% { opacity: 0; transform: translate3d(0, 0, 0); }
  10% { opacity: 0.28; }
  26% { opacity: 0.2; }
  48% { opacity: 0.16; transform: translate3d(1px, 1px, 0); }
  72% { opacity: 0.1; transform: translate3d(-1px, 0, 0); }
  100% { opacity: 0; transform: translate3d(0, 0, 0); }
}

/* Loading */

#loading-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: var(--z-loading);
  transition: opacity 0.35s ease, visibility 0.35s ease;
}

#loading-screen.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.loading-mark {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--ink);
  color: var(--on-ink);
  font-family: var(--font-cjk);
  font-size: var(--text-3xl);
  font-weight: 600;
  line-height: 1;
}

.loading-content p {
  color: var(--text-soft);
  font-family: var(--font-serif);
  font-style: italic;
  font-size: var(--text-md);
}

.loading-bar {
  position: relative;
  width: 120px;
  height: 2px;
  overflow: hidden;
  background: var(--line);
  border-radius: 2px;
}

.loading-bar span {
  position: absolute;
  inset: 0 auto 0 0;
  width: 40%;
  background: var(--ink);
  animation: loadingSlide 1.2s var(--steps-8) infinite;
}

@keyframes loadingSlide {
  from { transform: translateX(-100%); }
  to { transform: translateX(250%); }
}

/* Header */

#header {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: var(--z-header);
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  transition: filter 0.32s var(--steps-4), background-color 0.3s var(--steps-4);
}

.header-inner {
  width: min(var(--shell), calc(100% - 2 * var(--gutter)));
  height: var(--header-h);
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 12px;
  border: none;
  background: none;
  text-align: left;
  border-radius: var(--radius-sm);
}

.brand-mark {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--ink);
  color: var(--on-ink);
  font-family: var(--font-cjk);
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: 1;
}

.brand-copy {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

#header-title {
  font-family: var(--font-serif);
  font-size: var(--text-lg);
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity 0.2s var(--steps-3), transform 0.2s var(--steps-3);
}

/* The page's own large title is in view: don't say it twice. */
body[data-view="shelf"].shelf-title-in-view #header-title,
body[data-view="home"].hero-title-in-view #header-title {
  opacity: 0;
  transform: translateY(4px);
}

body[data-view="shelf"] #btn-reader-tools .header-icon-tools,
body:not([data-view="shelf"]) #btn-reader-tools .header-icon-stats {
  display: none;
}

.header-actions {
  display: flex;
  gap: 6px;
}

.header-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: transparent;
  color: var(--text-soft);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.24s var(--steps-3), color 0.24s var(--steps-3), border-color 0.24s var(--steps-3);
}

.header-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}

.header-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

#progress-bar {
  position: fixed;
  top: var(--header-h);
  left: 0;
  right: 0;
  height: 2px;
  background: transparent;
  z-index: calc(var(--z-header) - 1);
  display: none;
}

#progress-fill {
  height: 100%;
  width: 0;
  background: var(--ink);
}

/* Immersive reading: header and bottom bar slide away while reading;
   the progress line moves up to the very top edge. */
#header,
#progress-bar,
.reader-bottom-nav {
  transition-property: transform, filter, background-color, border-color;
  transition-duration: 0.24s;
  transition-timing-function: var(--steps-4);
}

body.is-chrome-hidden #header {
  transform: translateY(-100%);
}

body.is-chrome-hidden #progress-bar {
  transform: translateY(calc(-1 * var(--header-h)));
}

body.is-chrome-hidden .reader-bottom-nav {
  transform: translateY(100%);
}

/* Layout */

#main {
  min-height: 100vh;
  padding-top: calc(var(--header-h) + 36px);
  padding-bottom: 72px;
}

.view {
  display: none;
}

.view.active {
  display: block;
  animation: fadeIn 0.34s var(--steps-4);
}

/* `translate`, not `transform`: toasts centre themselves with a transform. */
@keyframes fadeIn {
  from { opacity: 0; translate: 0 10px; }
  to { opacity: 1; translate: 0 0; }
}

.view-shell {
  width: min(var(--shell), calc(100% - 2 * var(--gutter)));
  margin: 0 auto;
}

/* Shared type + controls */

.nowrap {
  white-space: nowrap;
}

.eyebrow,
.section-kicker,
.panel-kicker,
.settings-section-title {
  display: block;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.16em;
  font-size: var(--text-2xs);
  font-weight: 600;
}

.btn-primary,
.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
  padding: 0 22px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-family: var(--font-ui);
  font-size: var(--text-base);
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  transition: background-color 0.26s var(--steps-3), border-color 0.26s var(--steps-3), color 0.26s var(--steps-3), opacity 0.26s var(--steps-3);
}

.btn-primary {
  background: var(--ink);
  color: var(--on-ink);
}

.btn-primary:hover {
  opacity: 0.86;
}

.btn-secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--line-strong);
}

.btn-secondary:hover {
  background: var(--surface-2);
  border-color: rgba(var(--ink-rgb), 0.3);
}

.btn-primary:disabled,
.btn-secondary:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

.btn-link {
  border: 0;
  background: none;
  padding: 8px 10px;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  text-decoration: underline;
  text-decoration-color: var(--line-strong);
  text-underline-offset: 4px;
}

.btn-link:hover {
  color: var(--text);
  text-decoration-color: currentColor;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  color: var(--text-soft);
  font-size: var(--text-sm);
  font-weight: 500;
}

/* Book covers */

.book {
  position: relative;
  display: block;
  aspect-ratio: 5 / 7;
  overflow: hidden;
  border-radius: 3px 7px 7px 3px;
  background: var(--surface-2);
  box-shadow: var(--shadow-book);
  container-type: inline-size;
  transition: box-shadow 0.3s var(--steps-3);
}

.book img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Spine crease and a faint sheen, so a flat scan reads as a physical book. */
.book::before,
.book::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: var(--z-raised);
  pointer-events: none;
  border-radius: inherit;
}

.book::before {
  background: linear-gradient(90deg,
    rgba(0, 0, 0, 0.24) 0,
    rgba(255, 255, 255, 0.16) 1.4%,
    rgba(0, 0, 0, 0.1) 3%,
    rgba(255, 255, 255, 0) 7%);
}

.book::after {
  background: linear-gradient(120deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0) 42%);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.cover-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 16% 12% 13%;
  background:
    radial-gradient(circle at 50% 38%, rgba(255, 255, 255, 0.08), transparent 62%),
    #1f1c18;
  color: #f1ece2;
  text-align: center;
}

.cover-fallback::before {
  content: "";
  position: absolute;
  inset: 5.5%;
  border: 1px solid rgba(241, 236, 226, 0.28);
  pointer-events: none;
}

.cover-fallback-series {
  font-size: 9px;
  font-size: max(7px, 6.2cqi);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  opacity: 0.7;
}

.cover-fallback-title {
  font-family: var(--font-serif);
  font-size: 16px;
  font-size: max(11px, 11cqi);
  font-weight: 600;
  line-height: 1.25;
  text-wrap: balance;
}

.cover-fallback-edition {
  display: block;
  margin-top: 6px;
  font-size: var(--text-2xs);
  font-size: max(8px, 7cqi);
  font-weight: 400;
  font-style: italic;
  opacity: 0.75;
}

.cover-fallback-mark {
  font-family: var(--font-cjk);
  font-size: var(--text-2xl);
  font-size: max(14px, 17cqi);
  line-height: 1;
  opacity: 0.85;
}

/* A soft halo of the cover's own art, centred behind the book. */
.hero-art,
.volume-overview-art {
  position: relative;
}

.hero-art > .book,
.volume-overview-art > .book {
  z-index: 1;
}

.cover-backdrop {
  position: absolute;
  inset: -30% -55%;
  z-index: 0;
  pointer-events: none;
  -webkit-mask-image: radial-gradient(closest-side, #000 35%, transparent 100%);
  mask-image: radial-gradient(closest-side, #000 35%, transparent 100%);
}

.cover-backdrop img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: var(--backdrop-opacity);
  filter: var(--img-tone) blur(44px);
}

.cover-backdrop img:not([src]),
.cover-backdrop img[src=""] {
  display: none;
}

/* Shelf */

.shelf-shell {
  padding-top: 24px;
}

.shelf-title {
  margin-top: 14px;
  font-family: var(--font-serif);
  font-size: clamp(2.8rem, 6vw, 5rem);
  font-weight: 600;
  letter-spacing: -0.035em;
  line-height: 1.1;
}

.shelf-intro {
  margin-top: 12px;
  color: var(--text-muted);
  font-size: var(--text-md);
}

.shelf-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 18px;
  margin-top: 22px;
}

.shelf-actions .btn-secondary {
  background: var(--surface);
}

.shelf-actions .btn-secondary:hover {
  background: var(--surface-2);
}

.ebook-storage-note, .ebook-help {
  margin-top: 12px;
  color: var(--text-muted);
  font-size: var(--text-xs);
  line-height: 1.7;
}

.ebook-shelf-item { display: flex; flex-direction: column; min-width: 0; }
.ebook-shelf-item .shelf-card { flex: 1; }
.ebook-delete { align-self: flex-end; margin: 10px 8px 0; font-size: var(--text-xs); }

.ebook-dialog {
  width: min(560px, calc(100% - 32px));
  max-height: calc(100dvh - 40px);
  margin: auto;
  padding: 28px;
  overflow: auto;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 20px;
}
.ebook-dialog::backdrop { background: rgba(0, 0, 0, 0.65); }
.ebook-dialog-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.ebook-dialog-head h2 { font-family: var(--font-serif); font-size: var(--text-3xl); }
.ebook-file-label { display: block; margin: 22px 0 10px; font-weight: 600; }
.ebook-dialog-head .tools-close { display: grid; place-items: center; }
/* Hidden from sight, still reachable by screen readers and the label. */
.visually-hidden { position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important; margin: -1px !important; overflow: hidden !important; clip-path: inset(50%) !important; white-space: nowrap !important; border: 0 !important; }
.ebook-dialog input { max-width: 100%; color: var(--text); }
.ebook-dialog input[type="file"] { width: 100%; font-size: var(--text-base); }
.ebook-dialog input::file-selector-button {
  padding: 10px 14px; margin-right: 10px; border: 1px solid var(--line-strong);
  border-radius: 8px; background: var(--surface); color: var(--text); cursor: pointer;
}
.ebook-preview { display: grid; gap: 10px; margin-top: 22px; }
.ebook-preview label { font-size: var(--text-sm); font-weight: 600; }
.ebook-preview label span { color: var(--text-muted); font-weight: 400; }
.ebook-preview input {
  width: 100%; min-width: 0; padding: 12px; font: inherit;
  background: var(--bg); border: 1px solid var(--line-strong); border-radius: 8px;
}
.ebook-preview p { color: var(--text-muted); font-size: var(--text-sm); }
.ebook-status { margin-top: 18px; font-size: var(--text-base); line-height: 1.6; }
.ebook-status:empty { margin-top: 0; }
.ebook-dialog .btn-primary { margin-top: 20px; width: 100%; }

.shelf-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 22px;
  margin-top: 44px;
}

.shelf-card {
  display: flex;
  align-items: flex-start;
  gap: 24px;
  width: 100%;
  min-width: 0;
  min-height: 292px;
  padding: 24px;
  text-align: left;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  cursor: pointer;
  transition: border-color 0.2s var(--steps-3), transform 0.2s var(--steps-3);
}

.shelf-card:focus-visible {
  border-color: var(--line-strong);
  transform: translateY(-2px);
}

/* Only a real pointer lifts the card; a tap would leave it stuck "hovered". */
@media (hover: hover) {
  .shelf-card:hover {
    border-color: var(--line-strong);
    transform: translateY(-2px);
  }
}

.shelf-cover {
  width: 165px;
  flex: 0 0 165px;
}

.shelf-copy {
  display: flex;
  flex-direction: column;
  align-self: stretch;
  min-width: 0;
}

.shelf-card-title {
  font-family: var(--font-serif);
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  font-size: clamp(1.35rem, 2.1vw, 1.8rem);
  text-wrap: balance;
  font-weight: 600;
  line-height: 1.25;
}

.shelf-card-author,
.shelf-card-meta {
  margin-top: 8px;
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.shelf-card-description {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  margin-top: 16px;
  color: var(--text-soft);
  font-size: var(--text-sm);
  line-height: 1.6;
}

.shelf-card-action {
  overflow: hidden;
  margin-top: auto;
  padding-top: 16px;
  color: var(--text);
  font-size: var(--text-xs);
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 900px) {
  .shelf-grid { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 520px) {
  .shelf-shell { padding-top: 8px; }
  .shelf-grid { gap: 14px; margin-top: 28px; }
  .shelf-card { gap: 16px; min-height: 210px; padding: 16px; border-radius: 16px; }
  .shelf-cover { width: 100px; flex-basis: 100px; }
  .shelf-card-title { font-size: 1.15rem; }
  .shelf-card-description { -webkit-line-clamp: 2; margin-top: 10px; font-size: var(--text-xs); }
}

/* Home: hero */

.home-shell {
  display: grid;
  gap: 64px;
}

.series-hero {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  grid-template-areas:
    "art head"
    "art stats"
    "art body";
  column-gap: 64px;
  align-items: center;
  padding: 56px 64px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 24px;
}

.hero-art {
  grid-area: art;
  display: flex;
  justify-content: center;
}

.hero-book {
  width: 100%;
  /* Two sheets peeking out behind the cover: a small stack of volumes. */
  box-shadow:
    5px 5px 0 -1px var(--paper),
    5px 5px 0 0 var(--line-strong),
    10px 10px 0 -1px var(--paper),
    10px 10px 0 0 var(--line-strong),
    var(--shadow-book);
}

.hero-head,
.hero-body {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
}

.hero-head {
  grid-area: head;
  align-self: end;
}

.hero-body {
  grid-area: body;
  align-self: start;
}

.hero-title {
  margin-top: 14px;
  font-family: var(--font-serif);
  font-size: clamp(2.6rem, 5vw, 4.1rem);
}

.shelf-card-title.is-long {
  font-size: clamp(1.2rem, 1.65vw, 1.45rem);
}

.hero-title.is-long {
  font-size: clamp(2rem, 3.3vw, 2.75rem);
  line-height: 1.12;
}

.hero-title {
  font-weight: 600;
  line-height: 1.04;
  letter-spacing: -0.025em;
  text-wrap: balance;
}

.hero-subtitle {
  margin-top: 12px;
  max-width: 52ch;
  text-wrap: balance;
  font-family: var(--font-serif);
  font-style: italic;
  font-size: var(--text-md);
  line-height: 1.55;
  color: var(--text-muted);
}

.hero-byline {
  margin-top: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 22px;
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.hero-byline strong {
  margin-left: 4px;
  color: var(--text);
  font-weight: 600;
}

.hero-description {
  margin-top: 18px;
  max-width: 60ch;
  color: var(--text-soft);
  font-size: var(--text-md);
  line-height: 1.8;
}

/* A long blurb shows its first lines, fading out above "Xem thêm". */
.hero-description.is-clamped {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 5;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(#000 60%, transparent);
  mask-image: linear-gradient(#000 60%, transparent);
}

.hero-description-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  margin-top: 4px;
  padding: 6px 0;
  border: 0;
  background: none;
  color: var(--text);
  font: 600 var(--text-sm)/1.4 var(--font-ui);
  cursor: pointer;
}

.hero-description-toggle::after {
  content: "";
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: translateY(-2px) rotate(45deg);
  transition: transform 0.2s var(--steps-3);
}

.hero-description-toggle[aria-expanded="true"]::after {
  transform: translateY(2px) rotate(225deg);
}

.hero-description-toggle:hover {
  text-decoration: underline;
  text-underline-offset: 4px;
}

.hero-description-toggle:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 3px;
  border-radius: 4px;
}

.hero-description-toggle[hidden] {
  display: none;
}

.series-hero .chip-row {
  margin-top: 18px;
}

/* Returning readers already know the premise; lead with where they left off. */
.series-hero.is-returning .hero-description,
.series-hero.is-returning .hero-description-toggle,
.series-hero.is-returning .chip-row {
  display: none;
}

/* Where the reader left off: the chapter, and how far through the whole
   series, in a card of its own above the buttons. */
.hero-continue {
  margin-top: 24px;
  width: 100%;
  max-width: 480px;
  padding: 16px 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: rgba(var(--ink-rgb), 0.035);
  text-align: left;
}

.continue-info {
  display: grid;
  gap: 4px;
}

.continue-progress {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}

.continue-track {
  flex: 1;
  height: 4px;
  overflow: hidden;
  border-radius: 3px;
  background: var(--line);
}

.continue-track span {
  display: block;
  height: 100%;
  background: var(--ink);
}

.continue-percent {
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.continue-volume {
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.continue-kicker {
  color: var(--text-muted);
  font-size: var(--text-2xs);
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.continue-title {
  font-family: var(--font-serif);
  font-size: var(--text-xl);
  font-weight: 600;
  line-height: 1.3;
}

.hero-actions {
  margin-top: 30px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.series-hero.is-returning .hero-actions {
  margin-top: 18px;
}

/* Facts about the series: one quiet line under the title, "13 tập ·
   194 chương · 12 bộ minh họa", so they inform without competing with it. */
.hero-stats {
  grid-area: stats;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  align-self: start;
  margin-top: 14px;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.stat-card {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  white-space: nowrap;
}

.stat-card + .stat-card::before {
  content: "·";
  margin: 0 10px;
  color: var(--text-muted);
}

.stat-card strong {
  color: var(--text);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  text-transform: lowercase;
}

/* Home: library */

.home-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 56px;
  align-items: start;
}

.home-main {
  min-width: 0;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 24px;
  margin-bottom: 28px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.section-header h2 {
  font-family: var(--font-serif);
  font-size: 30px;
  font-weight: 600;
  line-height: 1.1;
  letter-spacing: -0.015em;
}

.section-title {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  min-width: 0;
}

.section-title .offline-btn {
  margin-top: 8px;
}

/* Library tabs: every volume, or what is saved on this device. */
.library-tabs {
  margin: -8px 0 26px;
}

/* A readable width for the list, like the contents page. */
.downloads {
  max-width: 760px;
}

.tab-count:not(:empty)::before {
  content: " · ";
}

.home-main.is-saved .search-box,
.home-main.is-saved .section-title #btn-save-all {
  display: none;
}

.downloads-summary {
  margin-bottom: 16px;
  color: var(--text-soft);
  font-size: var(--text-base);
  line-height: 1.6;
}

.downloads-list {
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
}

.downloads-empty {
  padding: 32px 24px;
  color: var(--text-muted);
  font-size: var(--text-base);
  line-height: 1.6;
  text-align: center;
}

.download-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-top: 1px solid var(--line);
}

.download-row:first-child {
  border-top: 0;
}

.download-open {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
}

.download-open:hover .download-title {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 4px;
}

.download-cover {
  flex-shrink: 0;
  width: 46px;
}

.download-copy {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.download-title {
  font-family: var(--font-serif);
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: 1.3;
}

.download-subtitle {
  color: var(--text-soft);
  font-size: var(--text-sm);
  line-height: 1.4;
}

.download-meta {
  color: var(--text-muted);
  font-size: var(--text-sm);
  line-height: 1.4;
}

.download-row.is-full .download-meta {
  color: var(--text-soft);
}

.download-track {
  display: block;
  width: min(180px, 100%);
  height: 3px;
  overflow: hidden;
  border-radius: 3px;
  background: var(--line);
}

.download-track span {
  display: block;
  height: 100%;
  background: var(--ink);
}

.download-finish {
  flex-shrink: 0;
  min-height: 34px;
  padding: 0 14px;
  font-size: var(--text-sm);
}

.download-delete {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
}

.download-delete:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
}

.download-row button:disabled {
  opacity: 0.4;
  cursor: default;
}

.downloads-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 18px;
}

.search-box {
  position: relative;
  width: 100%;
  max-width: 300px;
}

.search-input {
  width: 100%;
  min-height: 42px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  padding: 0 16px 0 40px;
  font-size: var(--text-base);
  transition: border-color 0.24s var(--steps-3), background-color 0.24s var(--steps-3);
}

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

.search-input:focus {
  outline: none;
  border-color: var(--ink);
  background: var(--raised);
}

.search-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
  display: flex;
}

.search-results {
  display: grid;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
}

.search-empty {
  padding: 22px;
  color: var(--text-muted);
  font-size: var(--text-base);
}

.search-result-item {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 20px;
  border: 0;
  border-top: 1px solid var(--line);
  background: transparent;
  text-align: left;
  transition: background-color 0.2s var(--steps-2);
}

.search-result-item:first-child {
  border-top: 0;
}

.search-result-item:hover {
  background: var(--surface-2);
}

.search-result-volume {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.search-result-title {
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 600;
}

.search-content {
  display: contents;
}

.search-section,
.search-note {
  padding: 14px 20px 10px;
  border-top: 1px solid var(--line);
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.search-note {
  font-weight: 400;
  letter-spacing: 0;
  text-transform: none;
  font-size: var(--text-sm);
}

.search-results > .search-content:first-child > :first-child {
  border-top: 0;
}

.search-snippet {
  font-family: var(--font-serif);
  font-size: var(--text-md);
  line-height: 1.55;
  color: var(--text-soft);
}

/* Search matches: a soft ink underline-highlight that suits every theme. */
mark.search-hit {
  color: inherit;
  background: rgba(var(--ink-rgb), 0.12);
  box-shadow: inset 0 -2px 0 rgba(var(--ink-rgb), 0.55);
  border-radius: 2px;
}

.volume-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 40px 26px;
}

.volume-card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
}

.volume-card:focus-visible {
  outline: none;
}

.volume-card:focus-visible .book {
  outline: 2px solid var(--ink);
  outline-offset: 4px;
}

@media (hover: hover) {
  .volume-card:hover .book {
    box-shadow: var(--shadow-book-hover);
  }

  .volume-card:hover .volume-card-title {
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 4px;
  }
}

.volume-progress {
  display: block;
  margin-top: 12px;
  height: 2px;
  overflow: hidden;
  border-radius: 2px;
  background: var(--line);
}

.volume-progress span {
  display: block;
  height: 100%;
  background: var(--ink);
}

.volume-card-title {
  display: block;
  margin-top: 14px;
  font-family: var(--font-serif);
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: 1.3;
}

.volume-progress + .volume-card-title {
  margin-top: 10px;
}

.volume-card-subtitle {
  display: block;
  margin-top: 4px;
  color: var(--text-soft);
  font-family: var(--font-serif);
  font-size: var(--text-base);
  line-height: 1.35;
}

.volume-card-meta {
  display: block;
  margin-top: 4px;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.volume-card-status {
  color: var(--text);
  font-weight: 600;
}

/* Home: sidebar */

.home-sidebar {
  display: grid;
  gap: 36px;
  position: sticky;
  top: calc(var(--header-h) + 36px);
}

/* The sidebar only holds reading history; without any, the shelf takes the full width. */
.home-grid:not(.has-sidebar) {
  grid-template-columns: minmax(0, 1fr);
}

.home-grid:not(.has-sidebar) .home-sidebar {
  display: none;
}

.panel-head {
  margin-bottom: 8px;
}

.panel-head h3 {
  font-family: var(--font-serif);
  font-size: var(--text-xl);
  font-weight: 600;
  line-height: 1.2;
}

.recent-chapter-list {
  display: grid;
}

.recent-item {
  width: 100%;
  display: grid;
  gap: 4px;
  padding: 15px 0;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: none;
  text-align: left;
}

.recent-item .recent-volume {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.recent-item .recent-title {
  font-family: var(--font-serif);
  font-size: var(--text-md);
  font-weight: 600;
  line-height: 1.4;
}

.recent-item .recent-time {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.recent-item:hover .recent-title {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 4px;
}

/* Volume view */

/* A narrower column than the home page, so a chapter row reads in one glance. */
.volume-shell {
  --shell: 920px;
}

.volume-overview {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  display: grid;
  grid-template-columns: 170px minmax(0, 1fr);
  grid-template-areas:
    "art copy"
    "art cta";
  column-gap: 48px;
  padding: 44px 52px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 24px;
}

.volume-overview-art {
  grid-area: art;
  align-self: center;
}

.volume-overview-copy {
  grid-area: copy;
  align-self: end;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.volume-overview-cta {
  grid-area: cta;
  align-self: start;
  margin-top: 26px;
}

.volume-name {
  margin-top: 10px;
  font-family: var(--font-serif);
  font-size: clamp(2.2rem, 4.4vw, 3.3rem);
  font-weight: 600;
  line-height: 1.06;
  letter-spacing: -0.02em;
  text-wrap: balance;
}

.volume-meta {
  margin-top: 12px;
  color: var(--text-soft);
  font-size: var(--text-md);
}

.volume-summary {
  margin-top: 6px;
  color: var(--text-muted);
  font-size: var(--text-base);
  line-height: 1.6;
}

.volume-summary:empty {
  display: none;
}

.volume-overview-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 18px;
}

.volume-overview-actions .volume-secondary-action { min-height: 44px; padding: 10px 4px; font-weight: 500; text-decoration: none; }
.volume-overview-actions .volume-secondary-action:hover { color: var(--text); text-decoration: underline; }
.volume-utilities {
  margin-top: 16px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 12px;
}
.volume-utilities:has(> #btn-save-offline[hidden]):has(> #volume-more[hidden]) { display: none; }
.volume-download { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 8px 0; max-width: 100%; font-size: var(--text-xs); font-weight: 500; line-height: 1.6; text-align: left; text-decoration: none; }
.volume-download:hover:not(:disabled) { color: var(--text); }
.volume-download::before { content: ""; flex: 0 0 16px; width: 16px; height: 16px; background: currentColor; -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 4v11M7 10l5 5 5-5M5 20h14'/%3E%3C/svg%3E") center / contain no-repeat; mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 4v11M7 10l5 5 5-5M5 20h14'/%3E%3C/svg%3E") center / contain no-repeat; }
.volume-download:disabled { cursor: default; opacity: 0.7; }
.volume-more { position: relative; margin-left: auto; }
.volume-more summary { display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 8px 4px 8px 8px; list-style: none; color: var(--text-muted); font: 500 var(--text-xs)/1.5 var(--font-ui); cursor: pointer; }
.volume-more summary::-webkit-details-marker { display: none; }
.volume-more summary:hover, .volume-more[open] summary { color: var(--text); }
.volume-more .shelf-more-menu { top: auto; bottom: calc(100% + 8px); width: min(240px, calc(100vw - 76px)); }
#volume-more #btn-reset-volume { border: 0; background: transparent; border-radius: 8px; }
#volume-more #btn-reset-volume:hover { background: rgba(var(--ui-danger-rgb), 0.1); }
#volume-more #btn-reset-volume:focus-visible { outline-offset: -2px; }
.volume-overview-cta :is(.volume-secondary-action, .volume-download, .volume-more summary, .shelf-menu-action):focus-visible { outline: 2px solid currentColor; outline-offset: 3px; border-radius: 6px; }

/* "Download for offline" reads as a button with an icon, not a footnote.
   The icon is drawn in CSS so code can swap the label freely. */
.offline-btn:not(#btn-reset-volume) {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 6px 14px 6px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-xs);
  text-decoration: none;
  transition: background-color 0.2s var(--steps-3), border-color 0.2s var(--steps-3);
}

.offline-btn:not(#btn-reset-volume)::before {
  content: "";
  flex: 0 0 auto;
  width: 15px;
  height: 15px;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 4v11M7 10l5 5 5-5M5 20h14'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 4v11M7 10l5 5 5-5M5 20h14'/%3E%3C/svg%3E") center / contain no-repeat;
}

.offline-btn:not(#btn-reset-volume):hover:not(:disabled) {
  background: var(--surface-2);
  border-color: rgba(var(--ink-rgb), 0.3);
}

.offline-btn:not(#btn-reset-volume):disabled {
  border-style: dashed;
  color: var(--text-muted);
  opacity: 1;
}

.offline-btn:not(#btn-reset-volume):disabled::before {
  display: none;
}

@media (hover: none) {
  .offline-btn { min-height: 44px; }
}

.offline-btn:disabled {
  cursor: default;
  text-decoration: none;
}

.toolbar-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin: 36px 0 14px;
}

.toolbar-group {
  display: inline-flex;
  gap: 2px;
  padding: 4px;
  border-radius: 999px;
  background: var(--surface-2);
}

.filter-btn {
  min-height: 34px;
  padding: 0 16px;
  white-space: nowrap;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  transition: background-color 0.24s var(--steps-3), color 0.24s var(--steps-3);
}

.filter-btn:hover {
  color: var(--text);
}

.filter-btn.is-active {
  background: var(--raised);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.sort-btn {
  flex-shrink: 0;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 16px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: transparent;
  color: var(--text-soft);
  font-size: var(--text-sm);
  font-weight: 600;
  transition: background-color 0.24s var(--steps-3);
}

.sort-btn:hover {
  background: var(--surface-2);
}

.chapter-list {
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
}

.chapter-empty {
  padding: 28px;
  color: var(--text-muted);
  font-size: var(--text-base);
  text-align: center;
}

.chapter-item {
  width: 100%;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  gap: 18px;
  align-items: center;
  padding: 17px 24px;
  border: 0;
  border-top: 1px solid var(--line);
  background: transparent;
  text-align: left;
  transition: background-color 0.2s var(--steps-2);
}

.chapter-item:first-child {
  border-top: 0;
}

.chapter-item:hover {
  background: var(--surface-2);
}

.chapter-item:focus-visible {
  outline-offset: -2px;
}

.chapter-num {
  display: flex;
  justify-content: center;
  font-family: var(--font-serif);
  font-size: var(--text-xl);
  font-weight: 500;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums lining-nums;
}

.chapter-copy {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.chapter-title {
  font-family: var(--font-serif);
  font-size: var(--text-lg);
  font-weight: 500;
  line-height: 1.4;
  color: var(--text);
}

.chapter-meta {
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.chapter-state {
  display: flex;
  align-items: center;
  color: var(--text-muted);
}

.state-pill {
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--on-ink);
  font-size: var(--text-2xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
}

.state-progress {
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.chapter-item.is-read .chapter-title {
  color: var(--text-soft);
}

.chapter-item.is-resume {
  background: rgba(var(--ink-rgb), 0.045);
  box-shadow: inset 3px 0 0 var(--ink);
}

.chapter-item.is-resume .chapter-num {
  color: var(--text);
}

/* Reader */

.reader-shell {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr);
  gap: 48px;
  align-items: start;
}

.reader-sidebar {
  position: sticky;
  top: calc(var(--header-h) + 36px);
  display: grid;
  gap: 22px;
}

.reader-side-book {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr);
  gap: 14px;
  align-items: center;
}

.reader-side-cover {
  border-radius: 2px 4px 4px 2px;
  box-shadow: var(--shadow-md);
}

.reader-side-cover .cover-fallback {
  padding: 10%;
}

.reader-side-cover .cover-fallback-series,
.reader-side-cover .cover-fallback-title {
  display: none;
}

.reader-side-book-copy {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.reader-sidebar-volume {
  color: var(--text);
  font-family: var(--font-serif);
  font-size: var(--text-md);
  font-weight: 600;
  line-height: 1.35;
}

.reader-sidebar-title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  padding-top: 20px;
  border-top: 1px solid var(--line);
  font-family: var(--font-serif);
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: 1.4;
}

.reader-side-progress {
  display: grid;
  gap: 10px;
}

.reader-side-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.reader-side-row strong {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.reader-side-track {
  height: 3px;
  overflow: hidden;
  border-radius: 3px;
  background: var(--line);
}

.reader-side-track span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--ink);
}

.select-label {
  display: block;
  margin-bottom: 8px;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.select-wrap {
  position: relative;
}

.select-wrap::after {
  content: "";
  position: absolute;
  right: 15px;
  top: 50%;
  width: 7px;
  height: 7px;
  margin-top: -5px;
  border-right: 1.5px solid var(--text-muted);
  border-bottom: 1.5px solid var(--text-muted);
  transform: rotate(45deg);
  pointer-events: none;
}

.reader-select {
  width: 100%;
  min-height: 42px;
  padding: 0 36px 0 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-base);
  appearance: none;
  -webkit-appearance: none;
  text-overflow: ellipsis;
}

.reader-side-nav {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) 42px;
  gap: 6px;
}

.icon-btn {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  border: 1px solid var(--line-strong);
  background: transparent;
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: 600;
  transition: background-color 0.24s var(--steps-3);
}

.icon-btn:hover:not(:disabled) {
  background: var(--surface-2);
}

.icon-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.side-settings-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 500;
  text-align: left;
}

.side-settings-btn:hover {
  color: var(--text);
}

.side-settings-glyph {
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--font-serif);
  font-size: var(--text-base);
  font-weight: 600;
}

.reader-stage,
.reader-chapters {
  display: grid;
  gap: 24px;
  min-width: 0;
}

.reader-frame {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  background: var(--paper);
  color: var(--paper-text);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  transition: background-color 0.3s var(--steps-4);
}

.reader-head {
  padding: 72px 48px 0;
  text-align: center;
}

.reader-meta {
  margin-top: 12px;
  color: var(--paper-muted);
  font-size: var(--text-sm);
}

.reader-breadcrumb {
  color: var(--paper-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.reader-stage-title {
  max-width: 26ch;
}

.reader-stage-title.is-long {
  max-width: 32ch;
  font-size: clamp(1.5rem, 2.6vw, 2.05rem);
  line-height: 1.24;
}

.reader-stage-title {
  margin: 16px auto 0;
  font-family: var(--font-serif);
  font-size: clamp(1.8rem, 3.4vw, 2.6rem);
  font-weight: 600;
  line-height: 1.18;
  letter-spacing: -0.015em;
  text-wrap: balance;
}

.reader-ornament,
.reader-fin {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--paper-muted);
}

.reader-ornament {
  margin-top: 30px;
}

.reader-ornament i {
  font-style: normal;
  font-size: var(--text-2xs);
}

.reader-ornament span,
.reader-fin span {
  width: 44px;
  height: 1px;
  background: currentColor;
  opacity: 0.5;
}

.reader-content {
  width: min(100%, var(--reader-width));
  margin: 0 auto;
  padding: 40px 48px 16px;
  font-family: var(--reader-font-family);
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
}

.reader-content p {
  margin-bottom: 0.85em;
  text-align: justify;
  text-indent: 1.6em;
}

/* A phone's narrow column has too few words per line to justify without
   wide gaps, so "auto" sets it ragged-right there. */
@media (max-width: 599px) {
  :root:not([data-text-align="justify"]) .reader-content p {
    text-align: start;
  }
}

:root[data-text-align="start"] .reader-content p {
  text-align: start;
}

.reader-content > p:first-child {
  text-indent: 0;
}

.reader-content .section-break {
  margin: 1.8em 0;
  color: var(--paper-muted);
  font-size: var(--text-sm);
  letter-spacing: 0.6em;
  text-align: center;
}

.reader-content .section-heading-inline {
  margin: 0.4em 0 1.4em;
  text-align: center;
  text-indent: 0;
  font-weight: 600;
}

.reader-content .illustration-container {
  margin: 36px 0;
  text-align: center;
}

.illustration-open {
  display: inline-block;
  max-width: 100%;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: none;
  cursor: zoom-in;
}

.reader-content .illustration-img {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  box-shadow: var(--shadow-md);
}

.reader-top {
  display: none;
  position: fixed;
  z-index: var(--z-overlay-controls);
  right: calc(20px + env(safe-area-inset-right));
  bottom: calc(24px + env(safe-area-inset-bottom));
  width: 44px;
  height: 44px;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text-soft);
  box-shadow: var(--shadow-md);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s var(--steps-3), bottom 0.24s var(--steps-4), color 0.2s;
}

body.is-past-start .reader-top {
  opacity: 0.92;
  pointer-events: auto;
}

body[data-view="reader"] .reader-top {
  display: grid;
}

.reader-top:focus-visible,
.reader-top:active {
  opacity: 1;
  color: var(--text);
}

@media (hover: hover) {
  body.is-past-start .reader-top:hover {
    opacity: 1;
    color: var(--text);
  }
}

/* Resume toast */

.reader-toast {
  position: fixed;
  z-index: calc(var(--z-toast) - 10);
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 10px 10px 18px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--on-ink);
  font-size: var(--text-base);
  box-shadow: var(--shadow-pop);
  animation: fadeIn 0.3s var(--steps-4);
}

.reader-toast[hidden] {
  display: none;
}

.app-toast {
  z-index: var(--z-toast);
  padding: 12px 18px;
  text-align: center;
}

/* A chapter's text on its way: a thin bar along the top edge, shown only
   after a short delay so a quick fetch never flashes it. */
.fetch-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: var(--z-top-progress);
  height: 2px;
  overflow: hidden;
  pointer-events: none;
  visibility: hidden;
}

body.is-fetching .fetch-bar {
  visibility: visible;
  transition: visibility 0s linear 0.15s;
}

body.is-fetching {
  cursor: progress;
}

.fetch-bar span {
  position: absolute;
  inset: 0 auto 0 0;
  width: 40%;
  background: var(--ink);
  animation: loadingSlide 1.2s var(--steps-8) infinite;
}

.reader-toast button {
  flex-shrink: 0;
  min-height: 34px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: var(--on-ink);
  color: var(--ink);
  font-size: var(--text-sm);
  font-weight: 600;
}

/* Listening: the player bar and the paragraph being read aloud */

.listen-bar {
  position: fixed;
  z-index: var(--z-overlay-controls);
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(560px, calc(100% - 20px));
  display: grid;
  gap: 4px;
  padding: 8px 12px 10px;
  background: var(--raised);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow-pop);
  animation: fadeIn 0.3s var(--steps-4);
}

.listen-bar[hidden] {
  display: none;
}

.listen-top {
  display: flex;
  align-items: center;
  gap: 8px;
}

.listen-where {
  flex: 1;
  min-width: 0;
  display: grid;
  gap: 1px;
  padding: 4px 6px;
  border: 0;
  border-radius: 10px;
  background: none;
  color: inherit;
  text-align: left;
}

.listen-where:hover {
  background: var(--surface-2);
}

.listen-state,
.listen-title {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.listen-state {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.listen-title {
  font-family: var(--font-serif);
  font-size: var(--text-md);
  font-weight: 600;
}

.listen-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.listen-icon,
.listen-toggle {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
}

.listen-icon {
  width: 42px;
  height: 42px;
  background: none;
  color: var(--text-soft);
}

.listen-icon:hover {
  background: var(--surface-2);
  color: var(--text);
}

.listen-toggle {
  width: 52px;
  height: 52px;
  background: var(--ink);
  color: var(--on-ink);
}

.listen-toggle .icon-play,
.listen-toggle.is-playing .icon-pause {
  display: block;
}

.listen-toggle .icon-pause,
.listen-toggle.is-playing .icon-play {
  display: none;
}

.listen-chip {
  min-width: 84px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-soft);
  font-size: var(--text-sm);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.listen-chip.is-on {
  border-color: transparent;
  background: var(--ink);
  color: var(--on-ink);
}

/* Room for the player under the last lines, and toasts above it. */
body.is-listening #main {
  padding-bottom: 190px;
}

body.is-listening .reader-toast,
body.is-listening[data-view="reader"]:not(.is-chrome-hidden) .reader-toast,
body.is-listening[data-view="reader"] .reader-top {
  bottom: calc(150px + env(safe-area-inset-bottom));
}

/* The paragraph being read aloud: a soft tint that bleeds past the text
   without moving it. */
.reader-content > .is-speaking,
.reader-stage-title.is-speaking {
  border-radius: 4px;
  background: rgba(var(--ink-rgb), 0.07);
  box-shadow: 0 0 0 8px rgba(var(--ink-rgb), 0.07);
  transition: background-color 0.2s var(--steps-2), box-shadow 0.2s var(--steps-2);
}

/* Sentence and word being spoken (CSS Custom Highlight API). */
::highlight(tts-sentence) {
  background-color: rgba(var(--ink-rgb), 0.1);
}

::highlight(tts-word) {
  background-color: rgba(var(--ink-rgb), 0.26);
  text-decoration: underline 2px;
  text-underline-offset: 4px;
}

/* Illustration viewer */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: var(--z-lightbox);
  background: rgba(0, 0, 0, 0.96);
  color: #f1eee6;
  animation: fadeIn 0.24s var(--steps-4);
}

.lightbox[hidden] {
  display: none;
}

.lightbox-stage {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 64px 72px;
}

.lightbox-img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 4px;
  user-select: none;
  -webkit-user-drag: none;
}

.lightbox-count {
  position: absolute;
  top: calc(20px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  font-size: var(--text-sm);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  opacity: 0.75;
}

.lightbox-btn {
  position: absolute;
  width: 46px;
  height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  color: inherit;
  transition: background-color 0.2s var(--steps-2), opacity 0.2s var(--steps-2);
}

.lightbox-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.2);
}

.lightbox-btn:disabled {
  opacity: 0.25;
  cursor: default;
}

.lightbox-close {
  top: calc(10px + env(safe-area-inset-top));
  right: 12px;
}

.lightbox-prev,
.lightbox-next {
  top: 50%;
  margin-top: -23px;
}

.lightbox-prev {
  left: 14px;
}

.lightbox-next {
  right: 14px;
}

.lightbox.is-single .lightbox-prev,
.lightbox.is-single .lightbox-next,
.lightbox.is-single .lightbox-count {
  display: none;
}

body.is-lightbox-open {
  overflow: hidden;
}

.reader-fin {
  padding: 28px 0 60px;
  font-family: var(--font-serif);
  font-style: italic;
  font-size: var(--text-base);
}

.reader-end {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.end-card {
  min-width: 0;
  min-height: 92px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
  padding: 18px 22px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--line);
  background: var(--surface);
  text-align: left;
  transition: background-color 0.24s var(--steps-3), opacity 0.24s var(--steps-3);
}

.end-card:hover:not(:disabled) {
  background: var(--surface-2);
}

.end-next {
  align-items: flex-end;
  text-align: right;
  background: var(--ink);
  color: var(--on-ink);
  border-color: transparent;
}

.end-next:hover:not(:disabled) {
  background: var(--ink);
  opacity: 0.88;
}

.end-card:disabled {
  opacity: 0.45;
  cursor: default;
}

.end-dir {
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.66;
}

.end-title {
  max-width: 100%;
  font-family: var(--font-serif);
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.reader-end-foot {
  text-align: center;
}

/* No visible scrollbars anywhere. The page, the settings sheet and every
   other scroll area still scroll by wheel, touch and keyboard; the reader
   shows its own progress line instead. */
* {
  scrollbar-width: none;
}

::-webkit-scrollbar {
  display: none;
}

/* Settings panel */

.settings-overlay {
  position: fixed;
  inset: 0;
  z-index: var(--z-sheet);
  background: rgba(0, 0, 0, 0.28);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s var(--steps-4);
}

.settings-overlay.is-open {
  opacity: 1;
  pointer-events: auto;
}

.settings-panel {
  position: fixed;
  z-index: calc(var(--z-sheet) + 10);
  right: max(16px, calc((100% - var(--shell)) / 2));
  top: calc(var(--header-h) + 10px);
  width: min(360px, calc(100% - 32px));
  max-height: calc(100vh - var(--header-h) - 24px);
  overflow-y: auto;
  padding: 22px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow-pop);
  opacity: 0;
  transform: translateY(-8px);
  pointer-events: none;
  transition: opacity 0.28s var(--steps-5), transform 0.28s var(--steps-5), background-color 0.3s var(--steps-4);
}

.settings-panel.is-open {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.settings-head h3 {
  font-family: var(--font-serif);
  font-size: var(--text-xl);
  font-weight: 600;
}

.settings-close {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 0;
  background: var(--surface-2);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.settings-section + .settings-section {
  margin-top: 26px;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.settings-section-title {
  margin-bottom: 16px;
}

.settings-group {
  margin-bottom: 18px;
}

.settings-group:last-child {
  margin-bottom: 0;
}

.settings-label {
  display: block;
  margin-bottom: 9px;
  color: var(--text);
  font-size: var(--text-base);
  font-weight: 600;
}

.settings-group[hidden] {
  display: none;
}

/* Settings that only make sense on a touch screen. */
@media (hover: hover) and (pointer: fine) {
  .touch-only {
    display: none;
  }
}

.settings-hint {
  margin: -4px 0 10px;
  color: var(--text-muted);
  font-size: var(--text-sm);
  line-height: 1.5;
}

.settings-action {
  width: 100%;
  min-height: 42px;
  font-size: var(--text-base);
}

.settings-progress-card {
  --settings-danger-rgb: 148, 60, 54;
  min-width: 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--raised);
}

body[data-theme="dark"] .settings-progress-card {
  --settings-danger-rgb: 218, 153, 144;
}

.settings-progress-book {
  display: grid;
  gap: 5px;
  margin-bottom: 14px;
  overflow-wrap: anywhere;
}

.settings-progress-book > span {
  color: var(--text-muted);
  font-size: var(--text-2xs);
}

.settings-progress-book strong {
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: 600;
  line-height: 1.6;
}

.settings-danger {
  max-width: 100%;
  min-width: 0;
  min-height: 46px;
  padding: 11px 16px;
  border-color: rgba(var(--settings-danger-rgb), .3);
  border-radius: 10px;
  background: rgba(var(--settings-danger-rgb), .06);
  color: rgb(var(--settings-danger-rgb));
  line-height: 1.5;
  white-space: normal;
  overflow-wrap: anywhere;
}

.settings-danger svg { flex-shrink: 0; }
.settings-danger > span { min-width: 0; }
.settings-danger:hover { border-color: rgba(var(--settings-danger-rgb), .5); background: rgba(var(--settings-danger-rgb), .12); }
.settings-danger:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }

.segmented,
.settings-font-row {
  display: flex;
  gap: 2px;
  padding: 4px;
  border-radius: 14px;
  background: var(--surface-2);
}

.segmented button {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 8px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  white-space: nowrap;
  transition: background-color 0.24s var(--steps-3), color 0.24s var(--steps-3);
}

.segmented button:hover {
  color: var(--text);
}

.segmented button.is-active {
  background: var(--raised);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.swatch {
  flex-shrink: 0;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.18);
}

.swatch-light { background: #ffffff; }
.swatch-sepia { background: #eadfc9; }
.swatch-dark { background: #000; border-color: rgba(255, 255, 255, 0.28); }
.swatch-color { background: conic-gradient(#f2a7b8, #f5d58a, #9fd6c2, #a9bff0, #d7aef0, #f2a7b8); }
.swatch-mono { background: linear-gradient(90deg, #000 50%, #f4f4f2 50%); }

#font-family-switch button {
  flex-direction: column;
  gap: 1px;
  min-height: 58px;
  font-size: var(--text-xs);
}

.font-sample {
  font-size: var(--text-lg);
  line-height: 1.2;
  color: var(--text);
}

.font-sample-serif { font-family: var(--font-serif); }
.font-sample-sans { font-family: var(--font-sans); }
.font-sample-system { font-family: var(--font-system); }

.font-btn {
  width: 56px;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 10px;
  background: var(--raised);
  color: var(--text);
  font-family: var(--font-serif);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

.font-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.font-btn-small { font-size: var(--text-sm); }
.font-btn-large { font-size: var(--text-xl); }

.settings-font-value {
  flex: 1;
  align-self: center;
  text-align: center;
  font-size: var(--text-base);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

/* Reader bottom nav (mobile) */

.reader-bottom-nav {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: var(--z-bottom-nav);
  gap: 4px;
  background: var(--bg);
  border-top: 1px solid var(--line);
  padding: 6px max(10px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
}

.reader-bottom-nav button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 54px;
  border-radius: 12px;
  border: none;
  background: none;
  color: var(--text-soft);
  font-size: var(--text-2xs);
  font-weight: 600;
}

.reader-bottom-nav button svg {
  width: 20px;
  height: 20px;
}

.reader-bottom-nav button:disabled {
  opacity: 0.32;
  cursor: not-allowed;
}

.reader-bottom-nav button:active:not(:disabled) {
  background: var(--surface-2);
}

/* Responsive */

@media (max-width: 1080px) {
  .home-grid {
    grid-template-columns: 1fr;
    gap: 56px;
  }

  .home-sidebar {
    position: static;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    align-items: start;
  }

  .series-hero {
    grid-template-columns: 210px minmax(0, 1fr);
    grid-template-areas:
      "art head"
      "art stats"
      "art body";
    column-gap: 48px;
    padding: 44px;
  }

  .reader-shell {
    grid-template-columns: 1fr;
  }

  /* Bottom nav + end-of-chapter cards cover navigation on narrow screens. */
  .reader-sidebar {
    display: none;
  }

  body[data-view="reader"] .reader-bottom-nav {
    display: flex;
  }

  /* The player replaces the bottom bar while listening. */
  body.is-listening[data-view="reader"] .reader-bottom-nav {
    display: none;
  }

  body[data-view="reader"] #main {
    padding-bottom: 96px;
  }

  body[data-view="reader"]:not(.is-chrome-hidden) .reader-toast,
  body[data-view="reader"]:not(.is-chrome-hidden):not(.is-listening) .reader-top {
    bottom: calc(84px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 840px) {
  .series-hero {
    grid-template-columns: 1fr;
    grid-template-areas:
      "art"
      "head"
      "stats"
      "body";
    padding: 40px 28px 32px;
    text-align: center;
  }

  .hero-art {
    margin-bottom: 32px;
  }

  .hero-book {
    width: 180px;
  }

  .hero-head,
  .hero-body {
    align-items: center;
  }

  .hero-byline,
  .series-hero .chip-row,
  .hero-actions {
    justify-content: center;
  }

  .hero-continue {
    margin-inline: auto;
    text-align: center;
  }

  .hero-stats {
    justify-content: center;
  }

  .volume-overview {
    grid-template-columns: 110px minmax(0, 1fr);
    column-gap: 24px;
    padding: 28px;
  }

  .volume-overview-copy {
    align-self: center;
  }

  .volume-overview-cta {
    margin-top: 20px;
  }

  .section-header {
    flex-direction: column;
    align-items: stretch;
  }

  .search-box {
    max-width: none;
  }

  .toolbar-row {
    gap: 10px;
  }

  .toolbar-group {
    flex: 1;
    display: flex;
  }

  .toolbar-group .filter-btn {
    flex: 1;
    padding: 0 10px;
  }
}

@media (max-width: 640px) {
  body {
    --header-h: 58px;
    --gutter: 16px;
  }

  #main {
    padding-top: calc(var(--header-h) + 20px);
    padding-bottom: 48px;
  }

  #eink-ghost .ghost-main {
    padding-top: calc(var(--header-h) + 20px);
    padding-bottom: 48px;
  }

  .home-shell {
    gap: 44px;
  }

  /* Cover beside the title instead of above it: saves most of a screen. */
  .series-hero {
    grid-template-columns: 92px minmax(0, 1fr);
    grid-template-areas:
      "art head"
      "stats stats"
      "body body";
    column-gap: 18px;
    padding: 22px 18px 24px;
    border-radius: 20px;
    text-align: left;
  }

  .hero-art {
    margin-bottom: 0;
  }

  .hero-book {
    width: 92px;
    box-shadow:
      3px 3px 0 -1px var(--paper),
      3px 3px 0 0 var(--line-strong),
      6px 6px 0 -1px var(--paper),
      6px 6px 0 0 var(--line-strong),
      var(--shadow-book);
  }

  .hero-head {
    align-self: center;
    align-items: flex-start;
  }

  .eyebrow {
    font-size: 10px;
    letter-spacing: 0.12em;
  }

  .eyebrow-extra {
    display: none;
  }

  .hero-title {
    margin-top: 8px;
    font-size: 1.8rem;
  }

  .hero-title.is-long {
    font-size: 1.38rem;
    line-height: 1.2;
  }

  .hero-subtitle {
    margin-top: 6px;
    font-size: var(--text-sm);
    line-height: 1.45;
  }

  .hero-body {
    margin-top: 22px;
    align-items: stretch;
  }

  .hero-byline,
  .series-hero .chip-row {
    justify-content: flex-start;
  }

  .hero-byline {
    margin-top: 0;
    gap: 4px 16px;
  }

  .hero-description {
    font-size: var(--text-md);
  }

  .hero-description.is-clamped {
    -webkit-line-clamp: 4;
  }

  .hero-continue {
    max-width: none;
    margin-top: 18px;
    padding: 14px 16px;
    text-align: left;
  }

  .continue-title {
    font-size: var(--text-xl);
  }

  /* Side by side: two stacked full-width pills made the card very tall. */
  .hero-actions > button {
    flex: 1 1 0;
    min-width: 0;
    padding-inline: 14px;
  }

  .hero-stats {
    justify-content: flex-start;
    margin-top: 16px;
  }

  .section-header h2 {
    font-size: 26px;
  }

  .download-row {
    padding: 12px 14px;
  }

  .download-open {
    gap: 12px;
  }

  .download-cover {
    width: 40px;
  }

  .downloads-actions > button {
    flex: 1 1 100%;
  }

  .volume-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 30px 18px;
  }

  /* Cover beside the title, buttons across the bottom: the chapter list
     starts within the first screen. */
  .volume-overview {
    grid-template-columns: 96px minmax(0, 1fr);
    grid-template-areas:
      "art copy"
      "cta cta";
    column-gap: 18px;
    padding: 20px 18px;
    border-radius: 20px;
  }

  .volume-name {
    margin-top: 6px;
    font-size: 1.9rem;
  }

  .volume-meta {
    margin-top: 8px;
    font-size: var(--text-base);
  }

  .volume-summary {
    font-size: var(--text-base);
    line-height: 1.5;
  }

  .volume-overview-cta {
    margin-top: 18px;
  }

  .volume-overview-actions > .btn-primary {
    flex: 1 1 auto;
    min-width: 0;
    padding-inline: 12px;
  }

  .volume-overview-actions > .volume-secondary-action { flex: 0 1 auto; padding-inline: 8px; }

  .toolbar-row {
    margin-top: 24px;
  }

  /* The back button already leads out; the mark only crowds the title. */
  body[data-view="home"] .brand-mark,
  body[data-view="volume"] .brand-mark,
  body[data-view="reader"] .brand-mark {
    display: none;
  }

  .sort-btn {
    width: 42px;
    min-height: 42px;
    padding: 0;
  }

  .sort-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  .toolbar-group .filter-btn {
    min-height: 34px;
  }

  .chapter-item {
    grid-template-columns: 32px minmax(0, 1fr) auto;
    gap: 12px;
    padding: 15px 16px;
  }

  .chapter-num {
    font-size: 16px;
  }

  .chapter-title {
    font-size: 16px;
  }

  body[data-view="reader"] #main {
    padding-top: var(--header-h);
  }

  /* Paper goes edge to edge on phones so the text gets every pixel. */
  .reader-frame {
    width: auto;
    margin-inline: calc(-1 * var(--gutter));
    border-radius: 0;
    border-inline: 0;
    box-shadow: none;
  }

  .reader-head {
    padding: 44px 24px 0;
  }

  .reader-content {
    padding: 30px 22px 12px;
  }

  /* Justified text opens wide gaps at phone widths without hyphenation. */
  .reader-content p {
    text-align: left;
    text-indent: 1.3em;
  }

  .reader-end {
    grid-template-columns: 1fr;
  }

  .end-next {
    order: -1;
  }

  .settings-panel {
    left: 0;
    right: 0;
    top: auto;
    bottom: 0;
    width: 100%;
    max-height: 86vh;
    overflow-y: auto;
    padding: 12px 20px calc(24px + env(safe-area-inset-bottom));
    border-radius: 22px 22px 0 0;
    transform: translateY(100%);
  }

  .settings-panel::before {
    content: "";
    display: block;
    width: 38px;
    height: 4px;
    margin: 0 auto 16px;
    border-radius: 4px;
    background: var(--line-strong);
  }

  .settings-panel.is-open {
    transform: translateY(0);
  }

  .lightbox-stage {
    padding: 60px 0;
  }

  .lightbox-img {
    border-radius: 0;
  }

  .lightbox-prev,
  .lightbox-next {
    top: auto;
    bottom: calc(14px + env(safe-area-inset-bottom));
    margin-top: 0;
  }
}

/* Smooth page changes (e-ink effect off): a short cross-fade, and the
   opened book's cover travelling from the shelf into the series page. */
::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 0.22s;
  animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
}

::view-transition-group(book-cover) {
  animation-duration: 0.38s;
  animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
}

/* People who ask the system for less motion get state changes without the
   animation in between; the loading bar then holds still. */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none !important;
  }
}
