/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --pb-blue:    #1B4F8A;
  --pb-blue-dk: #133a68;
  --pb-gold:    #C8922A;
  --cream:      #FAF8F5;
  --text:       #2A2A2A;
  --text-muted: #6B6B6B;
  --divider:    #E4DED5;
  --chapter-bg: #EEF3FA;
  --sidebar-w:  280px;
  --topbar-h:   56px;
  --radius:     10px;
  --font-body:  Georgia, 'Times New Roman', serif;
  --font-ui:    -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

html { scroll-behavior: smooth; font-size: 16px; }

body {
  font-family: var(--font-body);
  background: var(--cream);
  color: var(--text);
  line-height: 1.75;
  min-height: 100vh;
}

/* ===== LIBRARY / HOME ===== */
.library-header {
  background: var(--pb-blue);
  color: #fff;
  padding: 24px 20px 28px;
  text-align: center;
}

.library-header .brand {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  opacity: .8;
  margin-bottom: 8px;
}

.library-header h1 {
  font-size: 26px;
  font-weight: 700;
  font-family: var(--font-ui);
  line-height: 1.25;
}

.library-header .subtitle {
  font-size: 14px;
  opacity: .75;
  margin-top: 6px;
  font-family: var(--font-ui);
}

.library-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  padding: 28px 20px;
  max-width: 760px;
  margin: 0 auto;
}

@media (min-width: 560px) {
  .library-grid { grid-template-columns: 1fr 1fr; }
}

.book-card {
  background: #fff;
  border: 1px solid var(--divider);
  border-radius: var(--radius);
  padding: 24px 20px 20px;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: box-shadow .2s, transform .15s;
  position: relative;
  overflow: hidden;
}

.book-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0;
  width: 4px; height: 100%;
  background: var(--pb-blue);
}

.book-card:hover, .book-card:focus {
  box-shadow: 0 6px 20px rgba(0,0,0,.12);
  transform: translateY(-2px);
  outline: none;
}

.book-card .card-tag {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--pb-blue);
}

.book-card h2 {
  font-size: 18px;
  line-height: 1.3;
  font-family: var(--font-ui);
  font-weight: 700;
  color: var(--text);
}

.book-card .card-subtitle {
  font-family: var(--font-ui);
  font-size: 12px;
  font-weight: 600;
  color: var(--pb-blue);
  opacity: .7;
  margin-top: -4px;
}

.book-card .card-author {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
}

.book-card .card-desc {
  font-family: var(--font-ui);
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.5;
}

.book-card .card-meta {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text-muted);
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--divider);
  display: flex;
  align-items: center;
  gap: 6px;
}

.card-arrow {
  margin-left: auto;
  font-size: 16px;
  color: var(--pb-blue);
}

.library-footer {
  text-align: center;
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 20px 32px;
}

/* ===== READER TOP BAR ===== */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  background: var(--pb-blue);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  z-index: 202;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}

.topbar-btn {
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
  transition: background .15s;
}

.topbar-btn:hover { background: rgba(255,255,255,.15); }

.topbar-title {
  flex: 1;
  font-family: var(--font-ui);
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-page {
  font-family: var(--font-ui);
  font-size: 13px;
  opacity: .8;
  flex-shrink: 0;
  white-space: nowrap;
}

/* ===== TOC SIDEBAR ===== */
.toc-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 200;
}

.toc-overlay.open { display: block; }

.toc-drawer {
  position: fixed;
  top: 0; left: 0;
  width: var(--sidebar-w);
  height: 100vh;
  background: #fff;
  z-index: 201;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform .25s ease;
  box-shadow: 4px 0 20px rgba(0,0,0,.15);
}

.toc-overlay.open .toc-drawer { transform: translateX(0); }

.toc-header {
  background: var(--pb-blue);
  color: #fff;
  padding: 16px 16px 14px;
  flex-shrink: 0;
}

.toc-header h2 {
  font-family: var(--font-ui);
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 2px;
}

.toc-header p {
  font-family: var(--font-ui);
  font-size: 12px;
  opacity: .75;
}

.toc-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

.toc-list li a {
  display: block;
  padding: 10px 16px;
  font-family: var(--font-ui);
  font-size: 14px;
  color: var(--text);
  text-decoration: none;
  border-left: 3px solid transparent;
  transition: background .15s, border-color .15s, color .15s;
  line-height: 1.4;
}

.toc-list li a:hover { background: var(--chapter-bg); }

.toc-list li.active a {
  background: var(--chapter-bg);
  border-left-color: var(--pb-blue);
  color: var(--pb-blue);
  font-weight: 600;
}

.toc-list li a .toc-ch {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.toc-list li.active a .toc-ch { color: var(--pb-blue); opacity: .7; }

/* Desktop: permanent sidebar below topbar */
@media (min-width: 900px) {
  .toc-overlay { display: block !important; background: none; pointer-events: none; }
  .toc-drawer {
    transform: translateX(0) !important;
    pointer-events: all;
    box-shadow: 1px 0 0 var(--divider);
    top: var(--topbar-h);
    height: calc(100vh - var(--topbar-h));
  }
  .toc-overlay:not(.open) .toc-drawer { transform: translateX(0); }
  .btn-toc { display: none !important; }
  /* On desktop, TOC header is redundant — topbar shows the title */
  .toc-header { display: none; }
}

/* ===== READER MAIN CONTENT ===== */
.reader-main {
  padding-top: var(--topbar-h);
  transition: margin-left .25s ease;
}

@media (min-width: 900px) {
  .reader-main { margin-left: var(--sidebar-w); }
}

.reader-inner {
  max-width: 700px;
  margin: 0 auto;
  padding: 0 20px 80px;
}

/* ===== PAGE BLOCKS ===== */
.page-block {
  padding: 32px 0 28px;
  border-bottom: 1px solid var(--divider);
  position: relative;
}

.page-block:last-child { border-bottom: none; }

.page-num {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .08em;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-num::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--divider);
}

.chapter-banner {
  background: var(--chapter-bg);
  border-left: 4px solid var(--pb-blue);
  border-radius: 0 6px 6px 0;
  padding: 14px 16px;
  margin-bottom: 24px;
}

.chapter-banner .ch-label {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--pb-blue);
  margin-bottom: 4px;
}

.chapter-banner h2 {
  font-family: var(--font-ui);
  font-size: 20px;
  font-weight: 700;
  color: var(--pb-blue-dk);
  line-height: 1.25;
}

.page-text {
  font-size: 16.5px;
  line-height: 1.8;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
}

/* Rendered PDF page images */
.page-image {
  display: block;
  width: 100%;
  max-width: 100%;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0,0,0,.1);
}

/* Placeholder shown before images are extracted */
.page-image-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 120px;
  background: #f4f4f0;
  border: 1px dashed var(--divider);
  border-radius: 6px;
  color: var(--text-muted);
  font-family: var(--font-ui);
  font-size: 13px;
  font-style: italic;
}

/* ===== PROGRESS BAR ===== */
.progress-bar {
  position: fixed;
  top: var(--topbar-h);
  left: 0; right: 0;
  height: 3px;
  background: rgba(255,255,255,.2);
  z-index: 99;
}

.progress-fill {
  height: 100%;
  background: var(--pb-gold);
  transition: width .2s;
  width: 0%;
}

/* ===== BACK-TO-TOP ===== */
.back-to-top {
  position: fixed;
  bottom: 24px;
  right: 20px;
  background: var(--pb-blue);
  color: #fff;
  border: none;
  border-radius: 50px;
  padding: 10px 16px;
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity .2s, transform .2s;
  pointer-events: none;
}

.back-to-top.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: all;
}

/* ===== LOADING STATE ===== */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 80px 20px;
  font-family: var(--font-ui);
  color: var(--text-muted);
}

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--divider);
  border-top-color: var(--pb-blue);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== RICH CONTENT BLOCKS ===== */
.page-text-block {
  font-family: var(--font-body);
  font-size: 16.5px;
  line-height: 1.8;
  color: var(--text);
  margin-bottom: 18px;
  white-space: pre-wrap;
  word-break: break-word;
}

.page-text-block.heading {
  font-family: var(--font-ui);
  font-size: 19px;
  font-weight: 700;
  color: var(--pb-blue-dk);
  line-height: 1.3;
  margin-top: 8px;
  margin-bottom: 14px;
  white-space: normal;
}

.page-text-block.bullet {
  padding-left: 18px;
}

.page-figure {
  display: block;
  width: 100%;
  max-width: 100%;
  border-radius: 6px;
  box-shadow: 0 2px 14px rgba(0,0,0,.1);
  margin: 22px 0;
  filter: contrast(1.2) brightness(1.05);
}

/* ===== SCROLLBAR ===== */
.toc-list::-webkit-scrollbar { width: 4px; }
.toc-list::-webkit-scrollbar-track { background: transparent; }
.toc-list::-webkit-scrollbar-thumb { background: var(--divider); border-radius: 2px; }
