/* ─── BOOKS & FILES DOMAIN ───────────────────────────────────────
   Foundation only — structure/spacing/conventions copied directly from
   Movies' page CSS. No cover-art system, no library-grid tile styling,
   no viewer/upload-dialog CSS yet; those belong to later tasks. ────── */
.books-page {
  position: relative;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 1.5rem;
  padding-top: var(--nav-h);
}

/* The exact same bookshelf image already used by the Home page's Books
   tile (staging/web/home/assets/books.jpg, served at /assets/books.jpg)
   — reused directly, not copied/renamed/regenerated. This div is a
   separate fixed layer behind all page content (z-index:-2), so the
   blur applies only here; foreground content (nav, upload tile, header,
   list) sits in normal flow on top of it, untouched by this filter. */
.books-bg {
  position: fixed;
  inset: 0;
  z-index: -2;
  background-image: url('/assets/backgrounds/books.png');
  background-size: cover;
  background-position: center;
  filter: blur(0.5px);
}

.books-bg::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(20,16,10,0.2), rgba(10,8,6,0.35));
}

[data-theme="light"] .books-bg::after { background: linear-gradient(180deg, rgba(40,32,20,0.08), rgba(30,24,15,0.14)); }

/* ─── LEFT: Upload tile — same shape/position as Movies' single tile ── */
.books-side {
  padding: 1.5rem 0 1.5rem 5%;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.books-tile {
  position: relative;
  width: 56%;
  margin: 0 auto;
  aspect-ratio: 1 / 1;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  box-shadow: var(--shadow-md);
  border: 1.5px solid transparent;
  transition: transform var(--transition), box-shadow var(--transition);
}

.books-tile:hover {
  transform: scale(1.03);
  box-shadow: var(--shadow-lg);
}

.books-tile-icon {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(255,255,255,0.16);
  display: flex;
  align-items: center;
  justify-content: center;
}

.books-tile-upload {
  background: linear-gradient(155deg, #3b7dd8, #2c5fa8);
  border-color: rgba(255,255,255,0.25);
}

/* ─── MAIN: reserved for the future library ───────────────────── */
.books-main {
  padding: 1.5rem 5% 2.5rem 0;
  min-width: 0;
}

.books-header {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 1.4rem;
}

.books-header-icon {
  color: var(--text-secondary);
  opacity: 0.85;
  margin-bottom: 0.15rem;
  filter: drop-shadow(0 1px 6px rgba(0,0,0,0.4));
}

.books-header h1 {
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  text-shadow: 0 1px 3px rgba(0,0,0,0.45), 0 2px 16px rgba(0,0,0,0.3);
}

.books-count {
  font-size: 0.82rem;
  color: var(--text-secondary);
  text-shadow: 0 1px 3px rgba(0,0,0,0.45), 0 2px 12px rgba(0,0,0,0.25);
}

[data-theme="light"] .books-header-icon { filter: drop-shadow(0 1px 6px rgba(255,255,255,0.55)); }
[data-theme="light"] .books-header h1,
[data-theme="light"] .books-count { text-shadow: 0 1px 3px rgba(255,255,255,0.6), 0 2px 14px rgba(255,255,255,0.4); }

/* Compact list — Music's playlist is the visual reference (row height,
   spacing, typography, separators, no per-item card/icon). #booksGrid is
   the same container element books.html already provides; it's a flex
   column here rather than a tile grid. */
.books-grid {
  display: flex;
  flex-direction: column;
}

.books-state {
  padding: 3rem 1rem;
  text-align: center;
  color: var(--text-tertiary);
  font-size: 0.85rem;
}

/* ─── LIBRARY ROWS — same row shape/spacing as Music's .music-row, no
   per-row icon or artwork (documents don't need one). A document row
   opens the file via the existing stream URL; an audiobook row expands
   into its own chapter rows using the identical row style. ─────────── */
.book-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 0.6rem 0.4rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  text-decoration: none;
  transition: background var(--transition);
}

.book-row:hover { background: rgba(127,127,127,0.08); }
.book-row.active { background: rgba(127,127,127,0.14); }

.book-row-text {
  min-width: 0;
  flex: 0 1 auto;
  max-width: 30rem;
  display: flex;
  flex-direction: column;
  gap: 0.05rem;
}

/* Fixed white/light text regardless of theme — same reasoning as Music's
   own row text (.music-row-title/.music-row-artist): the row always sits
   over the same photographic background layer, not the page's normal
   surface color, so it needs the same theme-independent treatment Music
   already uses rather than the light/dark `--text-*` tokens (which go
   dark in light theme and were unreadable over the bookshelf image). */
.book-row-title {
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.55), 0 2px 10px rgba(0,0,0,0.4);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-row-meta {
  font-size: 0.76rem;
  font-weight: 500;
  color: rgba(255,255,255,0.78);
  text-shadow: 0 1px 3px rgba(0,0,0,0.5), 0 2px 8px rgba(0,0,0,0.35);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Audiobook group row — one clean row representing the whole folder, not
   a folder icon/card. The chevron communicates expand/collapse only. */
.book-audiobook-row .book-row-title { display: flex; align-items: center; gap: 0.5rem; }

.book-audiobook-chevron {
  flex-shrink: 0;
  transition: transform var(--transition);
  color: rgba(255,255,255,0.75);
  filter: drop-shadow(0 1px 3px rgba(0,0,0,0.5));
}

.book-audiobook-row.expanded .book-audiobook-chevron { transform: rotate(90deg); }

.book-audiobook-chapters {
  display: flex;
  flex-direction: column;
  padding-left: 1.4rem;
}
/* Without this, the `hidden` attribute has no visual effect here: this
   element's own `display: flex` above is an author-stylesheet rule, which
   always beats the browser's default `[hidden] { display: none }` (a
   user-agent rule) regardless of specificity — so collapsing silently did
   nothing. Same pattern already used for .book-row-menu[hidden] and
   .books-player-bar[hidden] below, just missing here. */
.book-audiobook-chapters[hidden] { display: none; }

.book-audiobook-chapters .book-row { padding-left: 0.6rem; }
/* Chapter rows have no .book-row-meta line, so unlike document rows,
   .book-row-menu-btn's doc-row margin-top above would overshoot here —
   see that rule's own comment for the full reasoning. */
.book-audiobook-chapters .book-row-menu-btn { margin-top: 0; }

.book-row-num {
  flex-shrink: 0;
  width: 1.6rem;
  font-size: 0.78rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: rgba(255,255,255,0.68);
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
  text-align: right;
}

/* Per-row ⋮ menu — copied as-is from Music's own .music-row-menu*
   (same shape, sizing, colors); only the class prefix changed to match
   this file's own naming convention. Deliberately not a permanent trash
   icon on the row (too easy to hit by accident, especially on mobile).
   Compact button, comfortable tap target; the popover itself is reused
   for both the Delete menu item and, in place, the Cancel/Delete
   confirmation. Used by both document rows and chapter rows. */
.book-row-menu-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  margin-left: 0.2rem;
  /* .book-row centers its children against the row's full cross-axis.
     Document rows (title + a .book-row-meta line below it) have a text
     block roughly as tall as this button, so it stays flush at the
     row's own top with barely any centering slack — anchoring the
     button there too (align-self:flex-start) and pulling it up by half
     the title-vs-button height difference re-centers it on the title
     alone rather than the taller title+meta block. Chapter rows have no
     meta line, so their (shorter) text block gets genuinely centered by
     the row against this now-taller button, landing already-aligned —
     the override below cancels the doc-row offset there. Both measured
     directly, not assumed: this is not the same geometry twice. */
  align-self: flex-start;
  margin-top: -8px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: none;
  /* Strong white at all times -- the dots need to read as an available
     action, not a faded/disabled-looking one, without requiring a hover
     to become visible (mouse-only signal anyway; touch has no hover). */
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
}
.book-row-menu-btn:hover { background: rgba(255,255,255,0.1); color: #fff; }

/* Fixed-position, body-appended, single shared element (see the JS side:
   one #bookRowMenu, not one per row) -- positioned from the clicked
   button's real screen coordinates every time it opens, exactly like
   Movies' identical .movie-tile-menu/openMovieMenu, which fixed this
   same class of bug there first: anchoring via `right` relative to
   .book-row (which stretches far past the button, unlike its content)
   made the menu jump to the row's far edge instead of staying next to
   the button that opened it. */
.book-row-menu {
  position: fixed;
  z-index: 1200;
  min-width: 160px;
  background: #24242c;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.45);
  padding: 0.4rem;
  cursor: default;
}
.book-row-menu[hidden] { display: none; }

.book-row-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.55rem 0.7rem;
  border: none;
  border-radius: 6px;
  background: none;
  color: #fff;
  font-size: 0.82rem;
  cursor: pointer;
}
.book-row-menu-item:hover { background: rgba(255,255,255,0.1); }

.book-row-menu-prompt {
  padding: 0.4rem 0.5rem 0.6rem;
  font-size: 0.8rem;
  color: #fff;
  line-height: 1.35;
}
.book-row-menu-actions {
  display: flex;
  gap: 0.5rem;
  padding: 0 0.3rem 0.2rem;
}
.book-row-menu-cancel,
.book-row-menu-confirm {
  flex: 1;
  padding: 0.5rem 0;
  border: none;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}
.book-row-menu-cancel {
  background: rgba(255,255,255,0.1);
  color: #fff;
}
.book-row-menu-cancel:hover { background: rgba(255,255,255,0.16); }
.book-row-menu-confirm {
  background: #d64545;
  color: #fff;
}
.book-row-menu-confirm:hover { background: #e05555; }

/* ─── MINI PLAYER BAR — appears once a chapter has been played; created
   entirely by books.js (no books.html change). Same fmtTime/seek/ended
   pattern as Music's own player, scoped to the currently open audiobook
   so "Next" always means "next chapter in this folder". ─────────────── */
.books-player-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.6rem 1rem;
  padding-bottom: calc(0.6rem + env(safe-area-inset-bottom, 0px));
  background: rgba(18,16,12,0.88);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(255,255,255,0.1);
}

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

.books-player-playpause {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1.5px solid rgba(255,255,255,0.7);
  background: none;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
}

.books-player-playpause:hover { background: rgba(255,255,255,0.12); }

/* Same ring-button shape as Play/Pause, slightly smaller since Stop is a
   secondary transport control sitting right next to it. */
.books-player-stop {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1.5px solid rgba(255,255,255,0.5);
  background: none;
  color: rgba(255,255,255,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
}

.books-player-stop:hover { background: rgba(255,255,255,0.12); color: #fff; }

.books-player-info {
  min-width: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.books-player-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.books-player-sub {
  font-size: 0.68rem;
  color: rgba(255,255,255,0.55);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.books-player-progress {
  position: relative;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: rgba(255,255,255,0.22);
  cursor: pointer;
  touch-action: none;
  margin-top: 0.3rem;
}

.books-player-progress-fill {
  position: absolute;
  top: 0; left: 0; bottom: 0;
  width: 0%;
  border-radius: 2px;
  background: #fff;
}

.books-player-time {
  flex-shrink: 0;
  font-size: 0.68rem;
  color: rgba(255,255,255,0.6);
  font-variant-numeric: tabular-nums;
}

.books-player-nav {
  flex-shrink: 0;
  display: flex;
  gap: 0.3rem;
}

.books-player-nav button {
  width: 28px;
  height: 28px;
  border: none;
  background: none;
  color: rgba(255,255,255,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
}

.books-player-nav button:hover { color: #fff; }

/* ─── VOLUME + MUTE — same button sizing/color as the Prev/Next controls
   above; slider styling matches Music's own .music-volume-slider (plain
   native range, white accent so it reads on the dark bar). ──────────── */
.books-player-volume {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.books-player-volume button {
  width: 28px;
  height: 28px;
  border: none;
  background: none;
  color: rgba(255,255,255,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
  flex-shrink: 0;
}

.books-player-volume button:hover { color: #fff; }

.books-player-volume input[type="range"] {
  width: 80px;
  accent-color: #fff;
  cursor: pointer;
}

@media (max-width: 640px) {
  .books-player-bar { flex-wrap: wrap; gap: 0.4rem 0.9rem; }
  .books-player-info { order: 1; flex-basis: 100%; }
  .books-player-progress { order: 2; flex-basis: 100%; margin-top: 0; }
  .books-player-playpause { order: 0; }
  .books-player-stop { order: 0; }
  .books-player-time { order: 3; }
  .books-player-nav { order: 4; }
  .books-player-volume { order: 5; }
  .books-player-volume input[type="range"] { width: 60px; }
}

/* ─── FOOTER (same pattern as Home/Photos/Movies/Music) ──────── */
.principles-strip {
  position: relative;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem 1.75rem;
  padding: 1.5rem 5%;
  margin-top: 1rem;
  border-top: 1px solid var(--border);
}

.principle-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-tertiary);
  white-space: nowrap;
}

.principle-item svg { flex-shrink: 0; opacity: 0.7; }

@media (max-width: 700px) {
  .principle-item { font-size: 0.66rem; }
}

/* ─── RESPONSIVE — same baseline as Movies, no new mobile architecture ── */
@media (max-width: 900px) {
  .books-page {
    grid-template-columns: 1fr;
  }
  .books-side {
    flex-direction: row;
    padding: 1rem 5% 0;
  }
  .books-tile {
    width: auto;
    margin: 0;
    flex: 1;
    aspect-ratio: auto;
    flex-direction: row;
    padding: 0.75rem 1rem;
    font-size: 0.72rem;
    gap: 0.6rem;
  }
  .books-tile-icon { width: 28px; height: 28px; }
  .books-main { padding: 1rem 5% 2rem; }

  /* Same reasoning as Photos/Movies/Music: the shared navbar hides its
     center nav below 900px, but here it holds the Photos/Movies/Music/
     Books & Files domains, so it needs to stay visible. */
  .navbar { flex-wrap: wrap; height: auto; padding: 0.55rem 4%; row-gap: 0.4rem; }
  .navbar-nav {
    display: flex;
    position: static;
    left: auto;
    transform: none;
    order: 3;
    flex-basis: 100%;
    width: 100%;
    justify-content: center;
    gap: 0.3rem;
  }
  .navbar-nav .nav-item { padding: 0.3rem 0.55rem; font-size: 0.72rem; }
  .books-page { padding-top: 5.5rem; }
}

@media (max-width: 480px) {
  .books-side { flex-direction: column; }
  .book-row-meta { display: none; }
  /* .book-row-menu-btn's -8px above assumes a document row's title+meta
     two-line block; with .book-row-meta hidden at this width every row
     is single-line, the same geometry chapter rows already are, so the
     same margin-top: 0 override applies here too. */
  .book-row-menu-btn { margin-top: 0; }
}
