/* ── Book-Style Reader CSS ── */
:root {
  --reader-toolbar-h: 56px;
  --reader-sidebar-w: 260px;
  --book-page-bg: #f8f3e8;
  --book-page-bg-dark: #1e1a14;
  --book-shadow: rgba(0,0,0,0.6);
  --book-spine: rgba(0,0,0,0.15);
}

/* ── Shimmer skeleton ── */
.page-shimmer {
  position: relative;
  overflow: hidden;
  background: #ede8dc;
}
.page-shimmer::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg,
    transparent 0%,
    rgba(255,255,255,0.5) 50%,
    transparent 100%
  );
  animation: shimmerMove 1.4s ease-in-out infinite;
}
@keyframes shimmerMove {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}
.shimmer-bar {
  position: absolute;
  left: 6%;
  height: 10px;
  background: rgba(200,185,150,0.6);
  border-radius: 4px;
}

.reader-body {
  overflow: hidden;
  background: #111;
}

/* ── Toolbar ── */
.reader-toolbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 200;
  height: var(--reader-toolbar-h);
  background: rgba(18,20,20,0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(242,202,80,0.15);
  display: flex; align-items: center; padding: 0 1rem; gap: 0.75rem;
  box-shadow: 0 2px 20px rgba(0,0,0,0.6);
}
.reader-toolbar-logo {
  font-family: 'Libre Caslon Text', serif;
  font-size: 1.1rem; font-weight: 700;
  color: #f2ca50; letter-spacing: -0.02em; margin-right: 0.25rem;
  white-space: nowrap; flex-shrink: 0;
}
.reader-toolbar-title {
  flex: 1; font-size: 0.82rem; color: #d0c5af;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-family: 'Libre Caslon Text', serif; letter-spacing: 0.01em;
}
.reader-toolbar-btn {
  background: none; border: none; cursor: pointer;
  color: #99907c; display: flex; align-items: center; justify-content: center;
  padding: 0.4rem; border-radius: 0.25rem;
  transition: background 0.2s, color 0.2s; flex-shrink: 0;
}
.reader-toolbar-btn:hover { background: rgba(242,202,80,0.12); color: #f2ca50; }
.reader-toolbar-divider { width: 1px; height: 22px; background: rgba(77,70,53,0.5); flex-shrink: 0; }
.scale-display { font-size: 0.78rem; color: #99907c; min-width: 38px; text-align: center; flex-shrink: 0; }

/* ── Layout ── */
.reader-layout {
  display: flex;
  margin-top: var(--reader-toolbar-h);
  height: calc(100vh - var(--reader-toolbar-h));
  background: #111;
}

/* ── Sidebar ── */
.reader-sidebar {
  width: var(--reader-sidebar-w);
  background: #0f1010;
  border-right: 1px solid rgba(77,70,53,0.2);
  display: flex; flex-direction: column;
  transition: transform 0.35s cubic-bezier(0.4,0,0.2,1);
  flex-shrink: 0; overflow: hidden;
}
.reader-sidebar.hidden {
  transform: translateX(calc(-1 * var(--reader-sidebar-w)));
  margin-right: calc(-1 * var(--reader-sidebar-w));
}
/* ── Sidebar mobile header (close button row) ── */
.sidebar-mobile-header {
  display: none;
  align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid rgba(77,70,53,0.2);
  flex-shrink: 0;
}
.sidebar-close-btn {
  background: none; border: none; cursor: pointer;
  color: #99907c; display: flex; align-items: center;
  padding: 0.3rem; border-radius: 0.25rem;
  transition: color 0.2s;
}
.sidebar-close-btn:hover { color: #f2ca50; }

/* ── Mobile backdrop overlay ── */
.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0; z-index: 140;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
.sidebar-backdrop.visible { display: block; }

@media(max-width:768px){
  .sidebar-mobile-header { display: flex; }

  .reader-sidebar {
    position: fixed;
    top: var(--reader-toolbar-h);
    left: 0; bottom: 0;
    width: 88vw;          /* almost full width */
    max-width: 340px;
    z-index: 150;
    transform: translateX(-100%);  /* hidden by default */
    transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
    box-shadow: 4px 0 32px rgba(0,0,0,0.8);
  }
  /* Open state — no .hidden class needed on mobile */
  .reader-sidebar.mobile-open {
    transform: translateX(0);
  }
  /* Keep hidden class working on desktop but noop on mobile */
  .reader-sidebar.hidden {
    transform: translateX(-100%);
    margin-right: 0;
  }
  .reader-viewer { padding: 1.5rem 0.75rem 5rem; }
  .book-page-wrapper::before { display: none; }
}

.sidebar-section-title {
  font-size: 0.6rem; font-weight: 700; letter-spacing: 0.2em; text-transform: uppercase;
  color: #4d4635; padding: 1rem 1rem 0.4rem;
}
.sidebar-book-info { padding: 0.25rem 1rem 1rem; border-bottom: 1px solid rgba(77,70,53,0.15); }
.sidebar-book-title {
  font-family: 'Libre Caslon Text', serif; font-size: 0.9rem; font-weight: 600;
  color: #e2e2e2; margin-bottom: 0.2rem; line-height: 1.3;
}
.sidebar-book-author { font-size: 0.72rem; color: #99907c; }

.sidebar-progress { padding: 0.75rem 1rem; border-bottom: 1px solid rgba(77,70,53,0.15); }
.sidebar-progress-label {
  display: flex; justify-content: space-between;
  font-size: 0.68rem; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase;
  color: #4d4635; margin-bottom: 0.5rem;
}
.sidebar-progress-bar { height: 3px; background: rgba(77,70,53,0.4); border-radius: 2px; }
.sidebar-progress-fill { height: 100%; background: linear-gradient(90deg, #d4af37, #f2ca50); border-radius: 2px; transition: width 0.5s ease; }

.sidebar-toc { flex: 1; overflow-y: auto; padding: 0.25rem 0 4rem; }
.toc-item {
  padding: 0.5rem 1rem; font-size: 0.8rem; color: #99907c; cursor: pointer;
  display: flex; align-items: center; gap: 0.5rem;
  transition: background 0.15s, color 0.15s; border-left: 2px solid transparent;
}
.toc-item:hover { background: rgba(242,202,80,0.05); color: #d0c5af; }
.toc-item.active { color: #f2ca50; background: rgba(242,202,80,0.08); border-left-color: #f2ca50; }
.toc-page { margin-left: auto; font-size: 0.68rem; color: #4d4635; }

/* ── Book Viewer ── */
.reader-viewer {
  flex: 1; overflow-y: auto; overflow-x: auto;
  background: #111;
  background-image:
    radial-gradient(ellipse at 50% 0%, rgba(30,26,20,0.8) 0%, transparent 60%),
    repeating-linear-gradient(90deg, transparent, transparent 200px, rgba(0,0,0,0.03) 200px, rgba(0,0,0,0.03) 201px);
  display: flex; flex-direction: column; align-items: center;
  padding: 3rem 1.5rem 6rem;
  scroll-behavior: smooth;
}

/* ── Book Page Wrapper ── */
.book-page-wrapper {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  margin-bottom: 3rem;
  filter: drop-shadow(0 30px 60px rgba(0,0,0,0.8));
  transition: filter 0.3s;
}
.book-page-wrapper:hover { filter: drop-shadow(0 40px 80px rgba(0,0,0,0.9)) drop-shadow(0 0 40px rgba(242,202,80,0.06)); }

/* Book spine shadow on left edge */
.book-page-wrapper::before {
  content: '';
  position: absolute;
  left: 0; top: 4px; bottom: 4px; width: 22px;
  background: linear-gradient(to right,
    rgba(0,0,0,0.5) 0%,
    rgba(0,0,0,0.2) 40%,
    transparent 100%
  );
  z-index: 2;
  border-radius: 3px 0 0 3px;
  pointer-events: none;
}

/* Right edge slight darkening */
.book-page-wrapper::after {
  content: '';
  position: absolute;
  right: 0; top: 0; bottom: 0; width: 4px;
  background: linear-gradient(to left, rgba(0,0,0,0.25), transparent);
  z-index: 2;
  pointer-events: none;
}

/* The actual PDF canvas styled as a book page */
.pdf-page-surface {
  position: relative;
  display: block;
  user-select: text;
  -webkit-user-select: text;
  overflow: hidden;
}

.pdf-page-canvas {
  display: block;
  border-radius: 2px 4px 4px 2px;
  background: var(--book-page-bg);
  position: relative;
  /* Cream page texture */
  background-image:
    linear-gradient(to right, rgba(212,175,55,0.04) 0%, transparent 8%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='300' height='300' filter='url(%23n)' opacity='0.03'/%3E%3C/svg%3E");
  /* Top binding line */
  border-top: 1px solid rgba(200,180,130,0.3);
}

.pdf-text-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  line-height: 1;
  opacity: 1;
  transform-origin: 0 0;
  user-select: text;
  -webkit-user-select: text;
  z-index: 5;
  pointer-events: auto;
  mix-blend-mode: normal;
}

.pdf-text-layer span,
.pdf-text-layer br {
  color: transparent !important;
  background: transparent !important;
  text-shadow: none !important;
  position: absolute !important;
  white-space: pre !important;
  cursor: text;
  transform-origin: 0% 0%;
}

.pdf-text-layer ::selection {
  background: rgba(242,202,80,0.35);
}

.pdf-text-layer .endOfContent {
  display: block;
  position: absolute;
  inset: 100% 0 0;
  z-index: -1;
  cursor: default;
  user-select: none;
}

/* Page number tab at bottom right */
.book-page-num {
  position: absolute;
  bottom: -2rem;
  left: 50%;
  transform: translateX(-50%);
  font-family: 'Libre Caslon Text', serif;
  font-size: 0.75rem;
  color: #4d4635;
  letter-spacing: 0.15em;
  white-space: nowrap;
}

/* Light mode (white background - default for PDF) */
.reader-viewer .pdf-page-canvas {
  background: #f8f3e8;
}

/* Dark mode — invert page */
.reader-viewer.dark-mode {
  background: #0a0a0a;
}
.reader-viewer.dark-mode .pdf-page-canvas {
  filter: invert(1) hue-rotate(180deg) brightness(0.88) contrast(1.05);
  background: #1e1a14;
}
.reader-viewer.dark-mode .book-page-wrapper::before {
  background: linear-gradient(to right, rgba(255,255,255,0.06) 0%, transparent 100%);
}

/* ── Loading State ── */
.reader-loading {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 400px; color: #4d4635; gap: 1.25rem;
}
.reader-spinner {
  width: 40px; height: 40px;
  border: 3px solid rgba(77,70,53,0.3);
  border-top-color: #f2ca50;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
.reader-loading-text {
  font-family: 'Libre Caslon Text', serif;
  font-size: 0.85rem; color: #4d4635; letter-spacing: 0.08em;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Mobile zoom group (hidden on desktop) ── */
.mobile-zoom-group {
  display: none;
}
@media(max-width:768px) {
  .mobile-zoom-group {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(20,22,22,0.9);
    border: 1px solid rgba(242,202,80,0.2);
    border-radius: 24px;
    padding: 2px 6px;
    flex-shrink: 0;
  }
  .mobile-zoom-pct {
    font-size: 0.72rem;
    font-weight: 700;
    color: #f2ca50;
    font-family: 'Hanken Grotesk', monospace;
    min-width: 38px;
    text-align: center;
    letter-spacing: 0.02em;
  }
}
@media(max-width:768px){
  .mobile-zoom-btn {
    display: flex;
    align-items: center; justify-content: center;
    width: 36px; height: 36px;
    background: rgba(242,202,80,0.1);
    border: 1px solid rgba(242,202,80,0.3);
    border-radius: 50%;
    color: #f2ca50;
    cursor: pointer;
    flex-shrink: 0;
    font-size: 1.35rem;
    font-weight: 700;
    line-height: 1;
    font-family: 'Hanken Grotesk', sans-serif;
    transition: background 0.2s, transform 0.15s;
  }
  .mobile-zoom-btn:active { background: rgba(242,202,80,0.25); transform: scale(0.92); }

  /* Also show scale display on mobile (between zoom buttons) */
  .scale-display { display: block !important; font-size: 0.7rem; min-width: 32px; }
}

/* ── Bottom Nav ── */
.reader-bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;
  background: rgba(12,15,15,0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid rgba(77,70,53,0.2);
  display: flex; align-items: center; justify-content: center;
  gap: 1rem; padding: 0.6rem 1rem;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.4);
}

.reader-page-nav-btn {
  background: rgba(40,42,43,0.8); border: 1px solid rgba(77,70,53,0.4);
  color: #d0c5af; padding: 0.45rem 1.25rem;
  border-radius: 0.25rem; cursor: pointer; font-size: 0.78rem;
  font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase;
  font-family: 'Hanken Grotesk', sans-serif;
  display: flex; align-items: center; gap: 0.35rem;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.reader-page-nav-btn:hover { background: rgba(242,202,80,0.12); color: #f2ca50; border-color: rgba(242,202,80,0.3); }
.reader-page-nav-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.page-indicator {
  display: flex; align-items: center; gap: 0.6rem;
  font-size: 0.8rem; color: #99907c;
  font-family: 'Libre Caslon Text', serif;
}
.page-jump-input {
  width: 52px; background: rgba(30,32,32,0.9);
  border: 1px solid rgba(77,70,53,0.5); color: #e2e2e2;
  font-size: 0.85rem; text-align: center; padding: 0.3rem;
  border-radius: 0.25rem; outline: none; font-family: inherit;
}
.page-jump-input:focus { border-color: #f2ca50; }
.page-total { color: #4d4635; }

/* ── Scrollbar ── */
.reader-viewer::-webkit-scrollbar { width: 5px; }
.reader-viewer::-webkit-scrollbar-track { background: #0a0a0a; }
.reader-viewer::-webkit-scrollbar-thumb { background: #2a2a2a; border-radius: 3px; }
.reader-viewer::-webkit-scrollbar-thumb:hover { background: #4d4635; }

/* Sidebar TOC scrollbar */
.sidebar-toc::-webkit-scrollbar { width: 4px; }
.sidebar-toc::-webkit-scrollbar-track { background: #0a0b0b; }
.sidebar-toc::-webkit-scrollbar-thumb { background: rgba(77,70,53,0.7); border-radius: 2px; transition: background 0.2s; }
.sidebar-toc::-webkit-scrollbar-thumb:hover { background: #f2ca50; }

/* Firefox */
.sidebar-toc { scrollbar-width: thin; scrollbar-color: rgba(77,70,53,0.7) #0a0b0b; }

/* Virtual TOC window items — fixed height must match JS TOC_ITEM_H=36px */
.toc-item {
  box-sizing: border-box;
  height: 36px;
  padding: 0 1rem;
  font-size: 0.8rem; color: #99907c; cursor: pointer;
  display: flex; align-items: center; gap: 0.5rem;
  transition: background 0.15s, color 0.15s; border-left: 2px solid transparent;
  white-space: nowrap; overflow: hidden;
}
.toc-item:hover { background: rgba(242,202,80,0.05); color: #d0c5af; }
.toc-item.active { color: #f2ca50; background: rgba(242,202,80,0.08); border-left-color: #f2ca50; }
.toc-page { margin-left: auto; font-size: 0.68rem; color: #4d4635; flex-shrink: 0; }

/* ── Responsive ── */
@media(max-width:768px){
  /* Hide desktop zoom controls */
  #zoomIn, #zoomOut, .scale-display { display: none !important; }

  /* Viewer: stretch so wrapper fills full width, overflow-x for zoom */
  .reader-viewer {
    padding: 0.75rem 0 5rem;
    align-items: stretch;   /* wrapper takes full width */
    overflow-x: auto;
  }

  /* Wrapper: full width + center canvas inside it */
  .book-page-wrapper {
    width: 100%;
    min-width: fit-content;   /* expand when canvas is wider (zoomed) */
    justify-content: center;  /* center canvas horizontally */
    margin-bottom: 1.5rem;
  }
  .book-page-wrapper::before { display: none; }

  /* ── Bottom nav — compact single row ── */
  .reader-bottom-nav {
    padding: 0.45rem 8px;
    gap: 6px;
    flex-wrap: nowrap;
    justify-content: space-between;
  }

  /* PREV / NEXT — icon only on mobile */
  .nav-btn-text { display: none; }
  .reader-page-nav-btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.7rem;
    min-width: 40px;
    justify-content: center;
    flex-shrink: 0;
  }

  /* Page indicator compact */
  .page-indicator { gap: 4px; flex-shrink: 1; min-width: 0; }
  .page-jump-input { width: 40px; font-size: 0.78rem; padding: 0.25rem; }
  .page-total { font-size: 0.7rem; white-space: nowrap; }
}
