/* ── Books: the library, a book's page, listening ────────────
   Loaded by the books pages only (templates/books/*.html). Colours are the
   site's tokens (style.css); covers carry the colour, the page stays quiet.
   Every cover has its box before it loads (2:3 for books, 1:1 for
   recordings), so nothing moves as a shelf fills in.                     */

/* ── The library's hero ──────────────────────────────────── */
.lib-hero {
  background: var(--hero-band);
  padding: clamp(1.5rem, 4vw, 3.5rem) var(--gutter) clamp(1rem, 2.5vw, 2rem);
}
.lib-hero-inner { max-width: 56rem; }
.lib-hero-title { margin: 0 0 0.5rem; }
.lib-hero-lede {
  font-size: var(--step-1);
  color: var(--text-muted);
  max-width: 40rem;
  margin: 0 0 1.25rem;
  text-wrap: pretty;
}
.lib-stat { color: var(--text-dim); font-size: var(--step--1); margin: 0.9rem 0 0; font-variant-numeric: tabular-nums; }

.lib-search {
  display: flex;
  gap: 0.5rem;
  max-width: 40rem;
  flex-wrap: wrap;
}
.lib-select {
  font: inherit;
  padding: 0.7rem 0.75rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--bg-raised);
  color: var(--text);
}
.lib-links { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 1rem; }

/* ── Shelves ─────────────────────────────────────────────── */
.lib-rail-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  padding-inline: var(--gutter);
}
.lib-rail-head .section-title { padding-inline: 0; }
.lib-more {
  font-size: var(--step--1);
  font-weight: 600;
  color: var(--accent-text);
  white-space: nowrap;
}
.lib-more:hover { text-decoration: underline; }

/* Positioned, so what is hidden for screen readers inside a card stays
   inside its rail instead of widening the page. */
.lib-card { position: relative; }
.lib-card.card--poster { flex-basis: 156px; }
.lib-card--square.card--poster { flex-basis: 184px; }
.lib-card--square .card-visual { aspect-ratio: 1 / 1; }
.lib-cover { padding: 0; }
.lib-cover .card-img { object-fit: cover; }
.lib-card .card-title { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.lib-card .card-meta { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.lib-card-extra { color: var(--text-dim); }
.lib-badge {
  position: absolute !important;
  left: 0.45rem;
  top: 0.45rem;
  font-size: var(--step--2);
}
.lib-badge .icon { width: 0.9em; height: 0.9em; }

/* What has been read or heard of it. */
.lib-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 4px;
  background: var(--scrim);
  z-index: 3;
}
.lib-progress-fill { display: block; height: 100%; background: var(--accent); }

/* ── Grids (browse, search) ──────────────────────────────── */
.lib-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(9.5rem, 44vw), 1fr));
  gap: 1.5rem 1rem;
  padding: 0 var(--gutter) 1rem;
}
.lib-grid--square { grid-template-columns: repeat(auto-fill, minmax(min(11rem, 44vw), 1fr)); }
.lib-grid .card { flex: none; width: auto; }

.lib-page-head { padding: clamp(1.25rem, 3vw, 2.5rem) var(--gutter) 0.5rem; max-width: 72rem; }
.lib-page-title { margin: 0.25rem 0 0.5rem; }
.lib-page-desc { color: var(--text-muted); max-width: 46rem; margin: 0 0 1rem; text-wrap: pretty; }
.lib-crumbs { font-size: var(--step--1); color: var(--text-dim); }
.lib-crumbs a { color: var(--text-muted); }
.lib-crumbs a:hover { color: var(--text); text-decoration: underline; }
.lib-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem 1.25rem; margin-top: 0.5rem; }
.lib-choices { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.lib-count { color: var(--text-dim); font-size: var(--step--1); margin: 0; font-variant-numeric: tabular-nums; }
.lib-empty { padding: 2rem var(--gutter); color: var(--text-muted); }
.lib-pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 1rem var(--gutter) 2.5rem;
}
.lib-pager-at { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.lib-fine { font-size: var(--step--1); color: var(--text-dim); }

/* ── Chips: languages, shelves, subjects ─────────────────── */
.lib-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  list-style: none;
  margin: 0;
  padding: 0 var(--gutter);
}
.lib-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 0.45rem;
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: var(--step--1);
  transition: background var(--transition), border-color var(--transition);
}
.lib-chip:hover { background: var(--bg-hover); border-color: var(--border-strong); }
.lib-chip-count, .lib-chip-en { color: var(--text-dim); font-variant-numeric: tabular-nums; font-size: var(--step--2); }
.lib-chip--shelf { background: var(--tint); }
.lib-index .lib-chips { padding-bottom: 0.5rem; }

.lib-sources { padding-inline: var(--gutter); max-width: 60rem; }
.lib-sources .section-title { padding-inline: 0; }
.lib-source-list { margin: 0; padding-left: 1.1rem; color: var(--text-muted); display: grid; gap: 0.5rem; }
/* Links inside sentences are underlined, not told apart by colour alone. */
.lib-source-list a, .lib-fine a, .lib-credit a, .lib-page-desc a {
  color: var(--accent-text);
  text-decoration: underline;
  text-underline-offset: 0.15em;
}
.lib-source-list a:hover, .lib-fine a:hover, .lib-credit a:hover { text-decoration-thickness: 2px; }

/* ── A book's page, a recording's page ───────────────────── */
.lib-book-hero, .lib-listen-hero {
  display: grid;
  grid-template-columns: minmax(9rem, 18rem) minmax(0, 1fr);
  gap: clamp(1.25rem, 4vw, 3rem);
  padding: clamp(1.25rem, 3.5vw, 3rem) var(--gutter) 1rem;
  max-width: 76rem;
  align-items: start;
}
.lib-book-cover img, .lib-listen-cover img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: var(--radius-lg);
  background: var(--placeholder);
  box-shadow: var(--shadow-2), 0 0 0 1px var(--hairline);
}
.lib-listen-cover img { aspect-ratio: 1 / 1; }
.lib-book-title {
  font-family: var(--font-serif);
  font-size: var(--step-4);
  font-weight: 600;
  line-height: 1.08;
  letter-spacing: -0.015em;
  margin: 0.35rem 0 0.4rem;
  text-wrap: balance;
}
.lib-book-subtitle { font-family: var(--font-serif); font-size: var(--step-1); color: var(--text-muted); margin: 0 0 0.6rem; text-wrap: pretty; }
.lib-book-by { font-size: var(--step-1); margin: 0 0 0.35rem; }
.lib-book-by a { color: var(--text); text-decoration: underline; text-decoration-color: var(--border-strong); text-underline-offset: 0.18em; }
.lib-book-by a:hover { text-decoration-color: currentColor; }
.lib-book-with { color: var(--text-muted); font-size: var(--step--1); margin: 0 0 0.5rem; }
.lib-actions { display: flex; flex-wrap: wrap; gap: 0.6rem; margin: 1.1rem 0 0.75rem; }
.lib-resume { color: var(--accent-text); font-weight: 600; margin: 0 0 0.75rem; }
.lib-book-blurb { font-size: var(--step-0); max-width: 44rem; margin: 0.5rem 0 1rem; text-wrap: pretty; }
.lib-book-summary { max-width: 44rem; color: var(--text-muted); text-wrap: pretty; }
.lib-book-summary p { margin: 0 0 0.6rem; }
.lib-facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
  gap: 0.75rem 1.5rem;
  margin: 1.25rem 0 0;
  max-width: 44rem;
}
.lib-fact dt { font-size: var(--step--2); text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); margin-bottom: 0.15rem; }
.lib-fact dd { margin: 0; }
.lib-tags .section-title { padding-inline: var(--gutter); }
.lib-credit {
  padding: 1.5rem var(--gutter) 3rem;
  color: var(--text-dim);
  font-size: var(--step--1);
  max-width: 60rem;
}
.lib-credit p { margin: 0 0 0.4rem; }
.lib-fail { color: var(--warning); margin: 1rem 0; }
.lib-fail a { color: inherit; text-decoration: underline; }

/* ── The player ──────────────────────────────────────────── */
.lib-player {
  margin: 1.25rem 0 0.5rem;
  padding: 1rem 1.1rem 0.9rem;
  border-radius: var(--radius-lg);
  background: var(--bg-raised);
  box-shadow: 0 0 0 1px var(--hairline) inset;
  max-width: 40rem;
}
.lib-now { margin: 0 0 0.6rem; display: flex; flex-direction: column; gap: 0.1rem; }
.lib-now-label { font-size: var(--step--2); text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); }
.lib-now-title { font-weight: 650; font-size: var(--step-0); }
.lib-scrub { display: flex; align-items: center; gap: 0.6rem; }
.lib-time { font-size: var(--step--2); color: var(--text-muted); font-variant-numeric: tabular-nums; min-width: 3.4em; }
.lib-time:last-child { text-align: right; }
.lib-seek { flex: 1; accent-color: var(--accent); min-width: 0; }
.lib-transport { display: flex; align-items: center; justify-content: center; gap: clamp(0.5rem, 2vw, 1.25rem); margin: 0.6rem 0 0.4rem; }
.lib-ctl {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  border: 0;
  background: var(--tint);
  color: var(--text);
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
}
.lib-ctl:hover { background: var(--tint-strong); }
.lib-ctl:active { transform: scale(0.94); }
.lib-ctl .icon { width: 1.35rem; height: 1.35rem; }
.lib-ctl-num { position: absolute; font-size: 0.55rem; font-weight: 700; top: 50%; left: 50%; transform: translate(-50%, -32%); pointer-events: none; }
.lib-ctl--play { width: 4.25rem; height: 4.25rem; background: var(--action); color: var(--on-action); }
.lib-ctl--play:hover { background: var(--action-hover); }
.lib-ctl--play .icon { width: 1.75rem; height: 1.75rem; fill: currentColor; }
.lib-pause-icon { display: none; }
.lib-playing .lib-play-icon { display: none; }
.lib-playing .lib-pause-icon { display: inline; }
.lib-ctl:focus-visible, .lib-chapter:focus-visible { outline: 3px solid var(--focus-ring); outline-offset: 3px; }
.lib-options { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.5rem; margin-top: 0.4rem; }
.lib-opt { padding: 0.5rem 0.85rem; font-size: var(--step--1); }
.lib-opt-value { font-variant-numeric: tabular-nums; }
.lib-note { min-height: 1.2em; margin: 0.5rem 0 0; text-align: center; font-size: var(--step--1); color: var(--text-muted); }
.lib-symbols { position: absolute; width: 0; height: 0; overflow: hidden; }

.lib-chapters .section-title, .lib-about .section-title { padding-inline: var(--gutter); }
.lib-chapter-list {
  list-style: none;
  margin: 0;
  padding: 0 var(--gutter);
  display: grid;
  gap: 2px;
  max-width: 60rem;
  counter-reset: none;
}
.lib-chapter {
  width: 100%;
  display: grid;
  grid-template-columns: 2.5rem minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: start;
  cursor: pointer;
}
.lib-chapter:hover { background: var(--tint); }
.lib-chapter-n { color: var(--text-dim); font-variant-numeric: tabular-nums; text-align: right; }
.lib-chapter-t { min-width: 0; }
.lib-chapter-r { display: block; color: var(--text-dim); font-size: var(--step--2); }
.lib-chapter-d { color: var(--text-muted); font-variant-numeric: tabular-nums; font-size: var(--step--1); }
.lib-chapter--done .lib-chapter-n { color: var(--success); }
.lib-chapter--now { background: var(--accent-tint); }
.lib-chapter--now .lib-chapter-n, .lib-chapter--now .lib-chapter-t { color: var(--accent-text); font-weight: 650; }
.lib-about { padding-bottom: 0.5rem; }
.lib-about > p { padding-inline: var(--gutter); max-width: 48rem; color: var(--text-muted); text-wrap: pretty; }
.lib-about .lib-chips { margin-top: 0.75rem; }

/* ── Wikisource results ──────────────────────────────────── */
.lib-ws-list { list-style: none; margin: 0; padding: 0 var(--gutter); display: grid; gap: 0.9rem; max-width: 52rem; }
.lib-ws-item a { font-family: var(--font-serif); font-size: var(--step-1); color: var(--accent-text); }
.lib-ws-item a:hover { text-decoration: underline; }
.lib-ws-item p { margin: 0.2rem 0 0; color: var(--text-muted); font-size: var(--step--1); }
.lib-ws-form { display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem; padding: 0 var(--gutter) 1rem; }
.lib-ws-form select { font: inherit; padding: 0.5rem; border-radius: var(--radius); border: 1px solid var(--border-strong); background: var(--bg-raised); color: var(--text); }

/* ── Phones ──────────────────────────────────────────────── */
@media (max-width: 640px) {
  .lib-book-hero, .lib-listen-hero { grid-template-columns: minmax(0, 1fr); }
  .lib-book-cover, .lib-listen-cover { max-width: 13rem; }
  .lib-listen-cover { max-width: 15rem; }
  .lib-card.card--poster { flex-basis: 132px; }
  .lib-card--square.card--poster { flex-basis: 150px; }
  .lib-chapter { grid-template-columns: 2rem minmax(0, 1fr) auto; padding-inline: 0.5rem; }
  .lib-ctl { width: 2.75rem; height: 2.75rem; }
  .lib-ctl--play { width: 3.75rem; height: 3.75rem; }
}

/* ── TV: big covers, big targets, read from across the room ─ */
:root[data-tv] .lib-card.card--poster { flex-basis: clamp(200px, 12vw, 420px); }
:root[data-tv] .lib-card--square.card--poster { flex-basis: clamp(240px, 14vw, 480px); }
:root[data-tv] .lib-grid { grid-template-columns: repeat(auto-fill, minmax(clamp(200px, 12vw, 420px), 1fr)); gap: 2rem 1.5rem; }
:root[data-tv] .lib-hero-lede { font-size: var(--step-2); }
:root[data-tv] .lib-search { display: none; }
:root[data-tv] .lib-book-hero, :root[data-tv] .lib-listen-hero { grid-template-columns: minmax(16rem, 28vw) minmax(0, 1fr); }
:root[data-tv] .lib-player { max-width: 52rem; padding: 1.5rem; }
:root[data-tv] .lib-ctl { width: 4.5rem; height: 4.5rem; }
:root[data-tv] .lib-ctl .icon { width: 2rem; height: 2rem; }
:root[data-tv] .lib-ctl--play { width: 6.5rem; height: 6.5rem; }
:root[data-tv] .lib-ctl--play .icon { width: 2.75rem; height: 2.75rem; }
:root[data-tv] .lib-ctl-num { font-size: 0.8rem; }
:root[data-tv] .lib-chapter { padding: 1rem 1.25rem; font-size: var(--step-1); }
:root[data-tv] .lib-ctl:is(:focus-visible, .controller-focus),
:root[data-tv] .lib-chapter:is(:focus-visible, .controller-focus) {
  outline: 4px solid var(--tv-focus);
  box-shadow: 0 0 0 8px var(--tv-focus-halo);
}

@media (prefers-reduced-motion: reduce) {
  .lib-ctl, .lib-chip { transition: none; }
}
