/* ============================================================
   pdf-library.css - /pages/library.html «คลังหนังสือ» + /pages/read.html (PDF library, phase 2)
   Brand contract only: css/tokens.css tokens (Palette A white / black / brand red #c0392b,
   no gold - Rule 29), fonts self-hosted via css/fonts.css (Rule 24: IBM Plex Sans Thai for
   headings/UI, Sarabun for prose). Scoped by body id so nothing leaks into site chrome.
   Mobile and desktop are DIFFERENT layouts (not a shrunk desktop):
     library  · mobile: horizontal cover shelf + compact list + devices in <details>
              · desktop (≥900px): two columns - shelf grid + sale list | sticky devices aside
     reader   · default: continuous vertical scroll (virtualised), pinch/zoom, fullscreen, page list
              · optional «โหมดหนังสือ»: page-turn view (spread on tablet/desktop, single page on phone)
              · mobile: fixed bottom page bar · desktop: top toolbar (page field, prev/next, zoom)
   ============================================================ */

body#library-page, body#reader-page {
  --lib-chrome: 0px;            /* site chrome is in-flow on mobile, fixed above 768px */
  background: var(--ink);
  color: var(--text);
  font-family: var(--thai);
}

@media (min-width: 769px) { body#library-page, body#reader-page { --lib-chrome: 52px; } }
@media (min-width: 1024px) { body#library-page, body#reader-page { --lib-chrome: 72px; } }
/* `hidden` must win over the display rules below (panels, notices). */
body#library-page [hidden], body#reader-page [hidden] { display: none !important; }

/* ── shared controls ─────────────────────────────────────────── */
.lib-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 20px;
  border: 1px solid var(--heading); border-radius: 999px;
  background: transparent; color: var(--heading);
  font: 600 15px/1.2 var(--serif); text-decoration: none; cursor: pointer;
  transition: background-color .18s ease, color .18s ease, border-color .18s ease;
}
.lib-btn:hover { background: var(--ink-3); }
.lib-btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.lib-btn-primary:hover { background: var(--accent-deep); border-color: var(--accent-deep); color: #fff; }
.lib-btn-quiet { border-color: var(--line-strong); }
.lib-btn:disabled { opacity: .5; cursor: not-allowed; }
.lib-btn:focus-visible, .rd-icon-btn:focus-visible, .rd-page-field input:focus-visible,
.lib-devices-details > summary:focus-visible, .lib-shell a:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 2px;
}
.lib-visually-hidden {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.lib-empty { color: var(--muted); margin: 0; line-height: 1.8; }
.lib-callout {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 14px;
  padding: 18px 20px; border: 1px solid var(--line-strong); border-radius: 14px; background: var(--ink-2);
}
.lib-callout p { margin: 0; line-height: 1.7; }

/* ── library page ───────────────────────────────────────────── */
.lib-shell { max-width: 1180px; margin: 0 auto; padding: calc(var(--lib-chrome) + 28px) 16px 72px; }
.lib-hero { padding: 8px 0 22px; border-bottom: 1px solid var(--line); margin-bottom: 24px; }
.lib-eyebrow { margin: 0 0 6px; font: 600 13px/1.4 var(--serif); letter-spacing: .04em; color: var(--accent); }
.lib-hero h1 { margin: 0 0 8px; font: 700 clamp(28px, 7vw, 44px)/1.25 var(--serif); color: var(--heading); }
.lib-hero p { margin: 0; max-width: 60ch; color: var(--muted); line-height: 1.8; }
.lib-section { margin: 0 0 36px; }
.lib-section h2 {
  margin: 0 0 14px; font: 700 21px/1.4 var(--serif); color: var(--heading);
  display: flex; align-items: center; gap: 10px;
}
.lib-section h2::before { content: ""; width: 4px; height: 1.1em; border-radius: 2px; background: var(--accent); }

.lib-notice {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px;
  padding: 14px 18px; margin: 0 0 24px; border-radius: 12px;
  border: 1px solid var(--line-strong); background: var(--ink-2); line-height: 1.7;
}
.lib-notice-success { border-color: var(--accent); }
.lib-notice-error { border-color: var(--accent); background: var(--ink); }
.lib-notice > span:not(.lib-spinner) { flex: 1 1 260px; }
.lib-spinner {
  width: 18px; height: 18px; flex: 0 0 18px; border-radius: 50%;
  border: 2px solid var(--line-strong); border-top-color: var(--accent);
  animation: lib-spin .9s linear infinite;
}
@keyframes lib-spin { to { transform: rotate(360deg); } }

.lib-cover {
  position: relative; aspect-ratio: 240 / 340; overflow: hidden; border-radius: 6px;
  background: var(--ink-3); box-shadow: 0 1px 0 var(--line), 0 10px 24px rgba(0,0,0,.12);
}
.lib-cover img { display: block; width: 100%; height: 100%; object-fit: cover; }
.lib-cover-blank { display: grid; place-items: center; color: var(--muted); font: 700 16px/1 var(--serif); }
.lib-book-title { margin: 0; font: 600 16px/1.5 var(--serif); color: var(--heading); }
.lib-book-meta, .lib-book-format { margin: 2px 0 0; color: var(--muted); font-size: 14px; line-height: 1.6; }
.lib-price { margin: 0; font: 700 20px/1.3 var(--serif); color: var(--heading); }
.lib-owned-tag { margin: 0; font-size: 14px; color: var(--accent); }

/* mobile shelf: horizontal scroll of covers */
.lib-shelf {
  list-style: none; margin: 0 -16px; padding: 4px 16px 12px;
  display: grid; grid-auto-flow: column; grid-auto-columns: 46%; gap: 14px;
  overflow-x: auto; scroll-snap-type: x mandatory; overscroll-behavior-x: contain;
}
.lib-shelf-item { scroll-snap-align: start; display: flex; flex-direction: column; gap: 10px; }
.lib-shelf-body { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.lib-shelf-body .lib-btn { width: 100%; }

/* mobile sale list: compact rows */
.lib-sale-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.lib-sale-item {
  display: grid; grid-template-columns: 72px 1fr; gap: 6px 14px; padding: 14px 0;
  border-bottom: 1px solid var(--line);
}
.lib-sale-item .lib-cover { grid-row: span 2; }
.lib-sale-action { grid-column: 2; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }

/* devices */
.lib-devices-details { border: 1px solid var(--line-strong); border-radius: 14px; background: var(--ink-2); }
.lib-devices-details > summary {
  list-style: none; cursor: pointer; padding: 16px 18px; min-height: 44px;
  font: 700 18px/1.4 var(--serif); color: var(--heading);
}
.lib-devices-details > summary::-webkit-details-marker { display: none; }
.lib-devices-details > summary::after { content: "＋"; float: right; color: var(--accent); }
.lib-devices-details[open] > summary::after { content: "－"; }
.lib-devices-body, .rd-devices { padding: 0 18px 18px; }
.lib-devices-intro { margin: 0 0 12px; line-height: 1.8; color: var(--text); }
.lib-device-list { list-style: none; margin: 0; padding: 0; }
.lib-device {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 0; border-top: 1px solid var(--line);
}
.lib-device-info { display: flex; flex-direction: column; min-width: 0; }
.lib-device-name { font-weight: 600; color: var(--heading); overflow-wrap: anywhere; }
.lib-device-meta { font-size: 13px; color: var(--muted); }
.lib-help { margin-top: 18px; font-size: 14px; color: var(--muted); line-height: 1.8; }

@media (min-width: 900px) {
  .lib-shell { padding: calc(var(--lib-chrome) + 44px) 32px 96px; }
  .lib-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 44px; align-items: start; }
  .lib-aside { position: sticky; top: 96px; }
  .lib-shelf {
    margin: 0; padding: 0; overflow: visible; scroll-snap-type: none;
    grid-auto-flow: row; grid-auto-columns: auto;
    grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 26px 22px;
  }
  .lib-sale-item {
    grid-template-columns: 96px minmax(0, 1fr) auto; align-items: center; gap: 22px; padding: 20px 0;
  }
  .lib-sale-item .lib-cover { grid-row: auto; }
  .lib-sale-action { grid-column: auto; flex-direction: column; align-items: flex-end; text-align: right; min-width: 170px; }
  .lib-devices-details > summary { pointer-events: none; }
  .lib-devices-details > summary::after { content: none; }
}

/* ── reader ─────────────────────────────────────────────────── */
.rd-shell { min-height: 60vh; padding-top: var(--lib-chrome); }
.rd-shell:has(> .rd-reader:not([hidden])) { min-height: 0; }
.rd-panel {
  max-width: 640px; margin: 48px auto; padding: 28px 22px;
  border: 1px solid var(--line-strong); border-radius: 16px; background: var(--ink-2);
}
.rd-panel h2, .rd-message-title { margin: 0 0 10px; font: 700 22px/1.4 var(--serif); color: var(--heading); }
.rd-panel h2:focus { outline: none; }
.rd-panel p { margin: 0 0 14px; line-height: 1.8; }
.rd-message-actions { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 8px; }
.rd-loading { display: flex; align-items: center; gap: 12px; justify-content: center; color: var(--muted); }
.rd-devices { padding: 0; }
/* Non-blocking "requests too frequent" notice while a 429 is waited out (pdf-reader.js rateNotice). */
.rd-rate-notice {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 50;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px;
  border: 1px solid var(--line-strong); background: var(--ink-2); color: var(--heading);
  font-size: 15px; line-height: 1.6; text-align: center;
}
.rd-rate-notice[hidden] { display: none; }

/* Reader fills the viewport below the site chrome; --rd-top is measured by pdf-reader.js. */
.rd-reader {
  --rd-top: var(--lib-chrome);
  display: flex; flex-direction: column;
  height: calc(100vh - var(--rd-top)); height: calc(100dvh - var(--rd-top));
  min-height: 420px; background: var(--ink-3); position: relative;
}
.rd-toolbar {
  flex: 0 0 auto; z-index: 30;
  display: flex; align-items: center; gap: 8px; padding: 6px 12px;
  background: var(--glass-bg-solid); border-bottom: 1px solid var(--line);
}
.rd-back { flex: 0 0 auto; color: var(--heading); text-decoration: none; font: 600 15px/1 var(--serif); padding: 12px 4px; min-height: 44px; display: inline-flex; align-items: center; }
.rd-back:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.rd-back-text { display: none; }
.rd-title { flex: 1 1 auto; min-width: 0; margin: 0; font: 600 15px/1.4 var(--serif); color: var(--heading); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rd-tools { display: none; }
.rd-quick { flex: 0 0 auto; display: flex; gap: 6px; }
.rd-icon-btn {
  width: 44px; height: 44px; display: inline-grid; place-items: center;
  border: 1px solid var(--line-strong); border-radius: 50%;
  background: var(--ink); color: var(--heading); font: 600 20px/1 var(--serif); cursor: pointer;
}
.rd-icon-btn:hover:not(:disabled) { border-color: var(--heading); }
.rd-icon-btn:disabled { opacity: .35; cursor: not-allowed; }
.rd-icon-btn[aria-pressed="true"], .rd-icon-btn[aria-expanded="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.rd-page-field { display: inline-flex; align-items: center; gap: 6px; font: 500 15px/1 var(--thai); color: var(--text); }
.rd-page-field input {
  width: 4.2em; min-height: 40px; padding: 4px 6px; text-align: center;
  border: 1px solid var(--line-strong); border-radius: 8px; background: var(--ink); color: var(--heading);
  font: 600 15px/1 var(--thai);
}
.rd-zoom-group { display: inline-flex; align-items: center; gap: 10px; }
.rd-zoom-level { min-width: 3.4em; text-align: center; font: 500 14px/1 var(--thai); color: var(--muted); }

.rd-body { flex: 1 1 auto; min-height: 0; position: relative; display: flex; }
.rd-stage {
  position: relative; flex: 1 1 auto; min-width: 0; background: var(--ink-3);
  padding: 12px 8px calc(168px + env(safe-area-inset-bottom));
  overflow: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  touch-action: pan-x pan-y;               /* pinch is handled by the reader (sharp re-render) */
}
.rd-stage:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.rd-doc { width: max-content; min-width: 100%; display: flex; flex-direction: column; align-items: center; gap: 12px; transform-origin: 0 0; }
.rd-paper { position: relative; background: #fff; box-shadow: 0 2px 14px rgba(0,0,0,.16); flex: 0 0 auto; }
.rd-sheet.is-ready { contain: layout paint; }
.rd-sheet:not(.is-ready)::after {
  content: attr(data-page); position: absolute; inset: 0; display: grid; place-items: center;
  font: 600 28px/1 var(--thai); color: var(--line-strong);
}
.rd-paper canvas { display: block; width: 100%; height: 100%; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

/* Book mode: one page (phone) or a two-page spread (tablet / desktop) with a light CSS page turn. */
.rd-stage.is-book { overflow: hidden; display: flex; align-items: center; justify-content: center; touch-action: pan-x pan-y pinch-zoom; padding: 16px 16px calc(150px + env(safe-area-inset-bottom)); }
.rd-book { position: relative; flex: 0 0 auto; perspective: 2400px; }
.rd-slot { position: absolute; top: 0; display: flex; align-items: center; justify-content: center; background: #fff; box-shadow: 0 2px 14px rgba(0,0,0,.16); overflow: hidden; }
.rd-slot-l { left: 0; }
.rd-slot-r { right: 0; }
.rd-slot.is-empty { background: transparent; box-shadow: none; }
.rd-paper-inner { position: relative; flex: 0 0 auto; background: #fff; }
.rd-paper-inner canvas { display: block; }
.rd-book.is-spread::after {            /* spine shade */
  content: ""; position: absolute; top: 0; bottom: 0; left: 50%; width: 36px; margin-left: -18px; pointer-events: none; z-index: 6;
  background: linear-gradient(90deg, transparent, rgba(0,0,0,.10) 50%, transparent);
}
.rd-leaf { position: absolute; top: 0; z-index: 5; transform-origin: left center; transform-style: preserve-3d; will-change: transform; box-shadow: 0 6px 22px rgba(0,0,0,.22); }
.rd-leaf-face { position: absolute; inset: 0; background: #fff; display: flex; align-items: center; justify-content: center; overflow: hidden; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.rd-leaf-back { transform: rotateY(180deg); }
.rd-edge {
  display: none; position: absolute; top: 50%; z-index: 20; width: 48px; height: 96px; margin-top: -48px;
  align-items: center; justify-content: center; border: 1px solid var(--line-strong); background: var(--glass-bg-solid);
  color: var(--heading); font: 600 30px/1 var(--serif); cursor: pointer; border-radius: 12px;
}
.rd-edge:disabled { opacity: .25; cursor: not-allowed; }
.rd-edge:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.rd-edge-prev { left: 10px; }
.rd-edge-next { right: 10px; }
.rd-reader.mode-book .rd-zoom-group { display: none; }

/* Page list (thumbnails): an overlay drawer, so opening it never reflows the page. */
.rd-thumbs {
  position: absolute; top: 0; bottom: 0; left: 0; z-index: 25; width: min(300px, 86%);
  display: flex; flex-direction: column; background: var(--ink); border-right: 1px solid var(--line-strong);
  box-shadow: 8px 0 28px rgba(0,0,0,.18); overflow-y: auto; overscroll-behavior: contain;
}
.rd-thumbs-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; background: var(--ink); border-bottom: 1px solid var(--line); font: 600 15px/1 var(--serif); color: var(--heading); }
.rd-thumbs-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 12px 8px; padding: 12px; }
.rd-thumb { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 4px; border: 2px solid transparent; border-radius: 8px; background: transparent; color: var(--text); cursor: pointer; font: 500 13px/1 var(--thai); min-height: 44px; }
.rd-thumb:hover { background: var(--ink-3); }
.rd-thumb:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.rd-thumb[aria-current="page"] { border-color: var(--accent); }
.rd-thumb-box { display: block; width: 88px; background: #fff; box-shadow: 0 1px 6px rgba(0,0,0,.2); }
.rd-thumb-box canvas { display: block; }

.rd-bottombar {
  /* Floats ABOVE the site's centred mobile menu pill (≈52px + 16px gap) so neither covers the other. */
  position: fixed; left: 12px; right: 12px; bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 40;
  display: grid; grid-template-columns: 56px 1fr 56px; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 999px;
  background: var(--glass-bg-solid); border: 1px solid var(--line-strong);
  box-shadow: 0 10px 28px rgba(0,0,0,.14);
}
.rd-bottombar .rd-page-field { justify-self: center; }
.rd-bottombar .rd-icon-btn { width: 52px; height: 52px; }

@media (min-width: 900px) {
  .rd-toolbar { padding: 8px 28px; gap: 14px; }
  .rd-back-text { display: inline; }
  .rd-title { font-size: 17px; }
  .rd-tools { display: flex; align-items: center; gap: 10px; }
  .rd-tools .rd-sep { width: 1px; height: 28px; background: var(--line-strong); margin: 0 6px; }
  .rd-stage { padding: 28px 40px 80px; }
  .rd-stage.is-book { padding: 24px 72px 32px; }
  .rd-paper, .rd-slot { box-shadow: 0 1px 0 var(--line), 0 18px 48px rgba(0,0,0,.18); }
  .rd-slot.is-empty { box-shadow: none; }
  .rd-bottombar { display: none; }
  .rd-reader.mode-book .rd-edge { display: flex; }
}

/* Fullscreen (Fullscreen API) and the immersive fallback used where element fullscreen is not offered (iPhone). */
.rd-reader:fullscreen { --rd-top: 0px; width: 100%; height: 100%; }
.rd-reader:-webkit-full-screen { --rd-top: 0px; width: 100%; height: 100%; }
html.rd-immersive, html.rd-immersive body { overflow: hidden; }
.rd-reader.is-immersive {
  position: fixed; inset: 0; z-index: 100000; --rd-top: 0px; height: 100vh; height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}
.rd-reader.is-fullscreen .rd-reader.is-fullscreen .rd-toolbar { position: absolute; top: 0; left: 0; right: 0; transition: transform .2s ease; }
.rd-reader.is-fullscreen .rd-bottombar { bottom: calc(16px + env(safe-area-inset-bottom)); transition: transform .2s ease, opacity .2s ease; }
.rd-reader.is-fullscreen .rd-stage { padding-bottom: 96px; }
.rd-reader.is-fullscreen.rd-chrome-off .rd-toolbar { transform: translateY(-110%); }
.rd-reader.is-fullscreen.rd-chrome-off .rd-bottombar { transform: translateY(160%); opacity: 0; pointer-events: none; }
.rd-reader.is-fullscreen .rd-stage.is-book { padding-bottom: 72px; }
.rd-reader.is-fullscreen .rd-thumbs { top: 0; }

/* pdf.js text layer (minimal subset of pdfjs-dist web/pdf_viewer.css, Apache-2.0) */
body#reader-page .textLayer {
  position: absolute; inset: 0; overflow: clip; opacity: 1; line-height: 1;
  text-align: initial; letter-spacing: normal; word-spacing: normal;
  -webkit-text-size-adjust: none; text-size-adjust: none; forced-color-adjust: none;
  transform-origin: 0 0; z-index: 0; caret-color: CanvasText;
  --min-font-size: 1;
  --text-scale-factor: calc(var(--total-scale-factor) * var(--min-font-size));
  --min-font-size-inv: calc(1 / var(--min-font-size));
}
body#reader-page .textLayer :is(span, br) {
  color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%;
}
body#reader-page .textLayer > :not(.markedContent),
body#reader-page .textLayer .markedContent span:not(.markedContent) {
  z-index: 1; --font-height: 0; font-size: calc(var(--text-scale-factor) * var(--font-height));
  --scale-x: 1; --rotate: 0deg;
  transform: rotate(var(--rotate)) scaleX(var(--scale-x)) scale(var(--min-font-size-inv));
}
body#reader-page .textLayer .markedContent { display: contents; }
body#reader-page .textLayer ::selection { background: rgba(216,27,55,.25); }
body#reader-page .textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: 0; cursor: default; user-select: none; }

/* Printing the reader is not offered: the page is hidden on paper (a deterrent, not protection). */
.rd-print-note { display: none; }
@media print {
  body#reader-page .rd-shell, body#reader-page #header-placeholder, body#reader-page #footer-placeholder { display: none !important; }
  body#reader-page .rd-print-note { display: block; font: 16px/1.8 var(--thai); padding: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  .lib-btn { transition: none; }
  .rd-reader.is-fullscreen .rd-toolbar, .rd-reader.is-fullscreen .rd-bottombar { transition: none; }
  .rd-leaf { transition: none !important; }
  .lib-spinner { animation: none; border-top-color: var(--accent); }
}
