/* ==========================================================================
   AFJOG Flipbook Reader — Exact FlipHTML5 Match (Zero Gap + Spine Shadow)
   ========================================================================== */

:root {
  color-scheme: dark;

  --bg-body:        #000000;
  --bg-bar:         #0a0a0a;
  --bg-hover:       rgba(255, 255, 255, 0.10);
  --bg-surface:     #1a1a1a;
  --border-line:    rgba(255, 255, 255, 0.08);

  --text-primary:   #e5e5e5;
  --text-secondary: #9ca3af;
  --text-dim:       #6b7280;

  --accent:         #ffffff;
  --accent-blue:    #3ea8ff;
  --bar-h:          48px;

  scrollbar-color: #555 #111;
}

@supports not (scrollbar-color: auto) {
  ::-webkit-scrollbar { width: 8px; height: 8px; }
  ::-webkit-scrollbar-track { background: #111; }
  ::-webkit-scrollbar-thumb { background: #555; border-radius: 4px; }
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%; width: 100%; overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg-body);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.visually-hidden:where(:not(:focus-within, :active)) {
  position: absolute !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  width: 1px !important; height: 1px !important;
  margin: -1px !important; padding: 0 !important; border: 0 !important;
  white-space: nowrap !important;
}

.is-hidden { display: none !important; }

:where(a, button, input, [tabindex]):focus-visible {
  outline: 2px solid var(--accent-blue);
  outline-offset: 2px;
}

/* ---- Full-Height Stage + Single Bottom Bar ---- */
.reader-app {
  display: grid;
  grid-template-rows: 1fr var(--bar-h);
  height: 100dvh; width: 100dvw;
  background: var(--bg-body);
}

/* ---- Main Stage ---- */
.reader-stage {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; outline: none;
  padding: 6px 36px;
  background: var(--bg-body);
}

/* ---- Book Viewport & Zoom ---- */
.book-viewport {
  position: relative; width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}

.book-viewport.is-zoomed {
  overflow: auto; cursor: grab;
  align-items: flex-start; justify-content: flex-start;
}
.book-viewport.is-panning { cursor: grabbing; user-select: none; }

.book-zoom-layer {
  display: flex; align-items: center; justify-content: center;
  width: 100%; height: 100%;
  margin: auto;
  transform-origin: center center;
  transition: transform 160ms ease-out;
}
.book-viewport.is-zoomed .book-zoom-layer {
  transform-origin: top left;
  margin: 0;
}

@media (prefers-reduced-motion: reduce) {
  .book-zoom-layer, .tb-btn, .side-arrow { transition: none; }
}

.flipbook-container {
  position: relative;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.85);
  will-change: transform;
  transition: transform 520ms cubic-bezier(0.22, 1, 0.36, 1);
}

/* Disable 3D texture flattening when idle so 1556x2200 WebP <img> uses crisp 2D Lanczos filtering */
.flipbook-container.is-idle .stf__block {
  perspective: none !important;
}
.flipbook-container.is-idle .stf__item {
  transform-style: flat !important;
}

.flipbook-container.is-cover-front {
  transform: translateX(-25%);
}

.flipbook-container.is-cover-back {
  transform: translateX(25%);
}

/* ---- Flip Page: ZERO gap & Realistic Book Spine Shadow ---- */
.flip-page {
  background: #ffffff;
  overflow: hidden;
  user-select: none;
  position: relative;
}

.flip-page canvas,
.flip-page-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: fill;
  image-rendering: auto;
}

/* Realistic center book-crease shadow on spread inner edges */
.flip-page.--left::after {
  content: "";
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 34px;
  pointer-events: none;
  background: linear-gradient(to left, rgba(0, 0, 0, 0.22) 0%, rgba(0, 0, 0, 0.08) 10px, transparent 100%);
}

.flip-page.--right::after {
  content: "";
  position: absolute;
  top: 0; left: 0; bottom: 0;
  width: 34px;
  pointer-events: none;
  background: linear-gradient(to right, rgba(0, 0, 0, 0.22) 0%, rgba(0, 0, 0, 0.08) 10px, transparent 100%);
}

.flip-page-placeholder {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: #ffffff;
}
.flip-page-placeholder::after {
  content: "";
  width: 40px; height: 40px;
  border: 4.5px solid #e6e6e6;
  border-top-color: #cccccc;
  border-radius: 50%;
  animation: ring-spin 1s linear infinite;
}

/* ---- Clean Side Navigation Chevrons (FlipHTML5-style) ---- */
.side-arrow {
  position: absolute; top: 50%; transform: translateY(-50%);
  z-index: 20;
  display: flex; align-items: center; justify-content: center;
  width: 44px; height: 64px;
  border: none; border-radius: 6px;
  background: transparent;
  color: rgba(255, 255, 255, 0.65);
  cursor: pointer;
  transition: color 140ms ease, transform 140ms ease;
}
.side-arrow:hover:not(:disabled) { color: #ffffff; transform: translateY(-50%) scale(1.08); }
.side-arrow:disabled { opacity: 0.15; cursor: default; }
.side-arrow-left { left: 6px; }
.side-arrow-right { right: 6px; }

/* ---- Bottom Toolbar ---- */
.bottom-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 0 14px calc(env(safe-area-inset-bottom, 0px));
  background: var(--bg-bar);
  border-top: 1px solid var(--border-line);
  z-index: 30;
}

.nav-cluster {
  display: flex; align-items: center; gap: 4px; flex-shrink: 0;
}

.scrubber-cluster {
  flex: 1;
  display: flex; align-items: center; gap: 8px;
  min-width: 80px;
}

.tb-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 34px; min-height: 34px;
  padding: 5px;
  border: none; border-radius: 4px;
  background: transparent;
  color: var(--text-primary);
  font: inherit; font-size: 12px;
  cursor: pointer; text-decoration: none;
  transition: background 100ms ease;
}
.tb-btn:hover:not(:disabled) { background: var(--bg-hover); }
.tb-btn:disabled { opacity: 0.3; cursor: default; }
.tb-btn.sm { min-width: 28px; min-height: 28px; padding: 4px; }

.zoom-pct {
  font-size: 11px; font-variant-numeric: tabular-nums;
  color: var(--text-secondary); min-width: 42px;
}

/* Page Indicator: e.g. 2-3 / 42 */
.page-info {
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 13px; font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  padding: 0 6px;
}

.page-num-input {
  width: 46px; padding: 2px 4px;
  border: 1px solid transparent; border-radius: 3px;
  background: transparent;
  color: var(--text-primary);
  font: inherit; font-size: 13px; text-align: right;
}
.page-num-input:hover,
.page-num-input:focus {
  border-color: var(--border-line);
  background: rgba(255, 255, 255, 0.06);
}

.page-sep { color: var(--text-secondary); }
.total-pages { color: var(--text-secondary); }

/* Thin White FlipHTML5 Scrubber */
.slider-wrap { flex: 1; display: flex; align-items: center; }

.page-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 2px;
  background: rgba(255, 255, 255, 0.25);
  border-radius: 999px;
  outline: none;
  cursor: pointer;
}
.page-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: #ffffff;
  cursor: pointer;
  box-shadow: 0 0 4px rgba(0,0,0,0.6);
  transition: transform 120ms ease;
}
.page-slider::-webkit-slider-thumb:hover { transform: scale(1.25); }
.page-slider::-moz-range-thumb {
  width: 12px; height: 12px; border: 0;
  border-radius: 50%; background: #ffffff; cursor: pointer;
}

/* ---- More Options Menu ---- */
.more-menu {
  position: absolute;
  right: 12px; bottom: 8px;
  z-index: 40;
  min-width: 190px;
  padding: 6px;
  border: 1px solid var(--border-line);
  border-radius: 8px;
  background: #18181b;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.75);
  display: flex; flex-direction: column; gap: 2px;
}

.more-menu-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 9px 12px;
  border: 0; border-radius: 5px;
  background: transparent; color: var(--text-primary);
  font: inherit; font-size: 13px; text-align: left; text-decoration: none;
  cursor: pointer;
}
.more-menu-item:hover { background: var(--bg-hover); }

/* ---- Loading Overlay (Exact Open-Book 3D Flip Loader) ---- */
.loading-overlay {
  position: absolute; inset: 0; z-index: 40;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: #000000;
  padding: 24px; text-align: center;
  transition: opacity 600ms ease-in-out;
}
.flip-book-loader {
  position: relative;
  width: 39.2px; height: 44.8px;
  margin-left: 39.2px; /* offset so spine sits at center */
  perspective: 200px;
  transform-style: preserve-3d;
}
.loader-page {
  position: absolute;
  left: 0; top: 0;
  width: 49px; height: 56px;
  transform-origin: 0 0;
}
.loader-page-right { transform: rotateY(0deg) scale(0.8); }
.loader-page-left  { transform: rotateY(180deg) scale(0.8); }
.loader-page-flip  {
  will-change: transform;
  animation: loadingBookFlip 1.2s infinite ease;
}
@keyframes loadingBookFlip {
  from { transform: rotateY(0deg) scale(0.8); }
  to   { transform: rotateY(-180deg) scale(0.8); }
}
@keyframes ring-spin { to { transform: rotate(360deg); } }
.loading-title {
  font-size: 20px; color: #dddddd; font-weight: 400; margin-top: 6px;
}
.loading-text { font-size: 12px; color: #888888; letter-spacing: 0.02em; }
.progress-track { width: min(220px, 68vw); height: 2px; border-radius: 999px; background: rgba(255,255,255,0.12); overflow: hidden; }
.progress-fill { width: 0%; height: 100%; background: #dddddd; transition: width 140ms ease-out; }

/* ---- Error Panel ---- */
.error-panel {
  position: absolute; inset: 0; z-index: 45;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 24px; background: rgba(0, 0, 0, 0.95); text-align: center;
}
.error-panel h2 { font-size: 16px; }
.error-panel p { font-size: 13px; color: var(--text-secondary); max-width: 420px; }
.accent-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 34px; padding: 6px 16px; border: 0; border-radius: 4px;
  background: var(--accent-blue); color: #fff; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}
.accent-btn.sm { min-height: 30px; padding: 4px 12px; font-size: 12px; }

/* ---- Left Floating Search Card (FlipHTML5-style) ---- */
.search-drawer {
  position: absolute; top: 14px; left: 14px;
  width: min(270px, 86vw); max-height: calc(100% - 28px);
  z-index: 35;
  display: flex; flex-direction: column;
  background: #252527; border: 1px solid rgba(255,255,255,0.12);
  border-radius: 6px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.75);
  padding: 10px; gap: 8px;
}
.search-form { display: flex; align-items: center; gap: 6px; }
.search-input-wrap {
  flex: 1; display: flex; align-items: center;
  background: #ffffff; border-radius: 4px; overflow: hidden;
}
.search-input-wrap input {
  flex: 1; min-width: 0; padding: 7px 8px;
  border: 0; background: transparent; color: #111827; font: inherit; font-size: 13px;
}
.search-input-wrap input:focus { outline: none; }
.search-submit-btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 6px 8px; border: 0; background: transparent; color: #4b5563; cursor: pointer;
}
.search-submit-btn:hover { color: #111827; }
.search-status { font-size: 12px; color: var(--text-secondary); }
.search-results { list-style: none; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.search-result-btn {
  width: 100%; text-align: left; padding: 8px;
  border: 1px solid var(--border-line); border-radius: 4px;
  background: var(--bg-surface); color: var(--text-primary); font: inherit; cursor: pointer;
}
.search-result-btn:hover { background: var(--bg-hover); }
.search-result-page { display: block; font-size: 11px; font-weight: 600; color: var(--accent-blue); margin-bottom: 3px; }
.search-result-snippet { display: block; font-size: 12px; color: var(--text-secondary); line-height: 1.4; }
.search-result-snippet mark { background: rgba(62,168,255,0.2); color: var(--accent-blue); padding: 0 2px; border-radius: 2px; }

/* ---- Session Restore Modal ---- */
.session-modal {
  position: absolute; inset: 0; z-index: 42;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.55);
  padding: 20px;
}
.session-card {
  width: min(320px, 90vw);
  padding: 20px 22px 16px;
  border-radius: 6px;
  background: #141416;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.85);
}
.session-msg {
  font-size: 14px; line-height: 1.5; color: #f3f4f6; margin-bottom: 18px;
}
.session-actions {
  display: flex; justify-content: flex-end; gap: 8px;
}
.session-btn {
  padding: 6px 14px; border: 0; border-radius: 4px;
  background: #3a3a3e; color: #e5e7eb; font: inherit; font-size: 12.5px; font-weight: 500;
  cursor: pointer;
}
.session-btn:hover { background: #4b4b52; }
.session-btn.primary { background: #4b5563; color: #ffffff; }
.session-btn.primary:hover { background: #6b7280; }

/* ---- Share Popover (QR + Socials + URL) ---- */
.share-popover {
  position: absolute; right: 16px; bottom: 10px; z-index: 41;
  width: min(340px, 92vw);
  padding: 16px;
  border-radius: 8px;
  background: #2f2f31;
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.85);
  display: flex; flex-direction: column; gap: 14px;
}
.share-top {
  display: flex; align-items: center; gap: 14px;
}
.share-qr {
  width: 128px; height: 128px; flex-shrink: 0;
  padding: 8px; border-radius: 6px; background: #ffffff;
  display: flex; align-items: center; justify-content: center;
}
.share-qr svg, .share-qr img { width: 100%; height: 100%; display: block; }
.share-divider {
  width: 1px; height: 112px; background: rgba(255, 255, 255, 0.14);
}
.share-socials {
  display: grid; grid-template-columns: repeat(3, 38px); gap: 10px;
}
.social-circle {
  width: 38px; height: 38px; border-radius: 50%; border: 0;
  display: inline-flex; align-items: center; justify-content: center;
  color: #ffffff; font: inherit; font-size: 16px; font-weight: 700; text-decoration: none;
  cursor: pointer; transition: transform 120ms ease, opacity 120ms ease;
}
.social-circle:hover { transform: scale(1.08); opacity: 0.92; }
.social-circle.fb { background: #1877f2; }
.social-circle.x { background: #0ea5e9; }
.social-circle.li { background: #0a66c2; font-size: 14px; }
.social-circle.wa { background: #22c55e; }
.social-circle.mail { background: #b91c1c; font-size: 15px; }
.social-circle.copy { background: #e11d48; font-size: 16px; }

.share-url-row { display: flex; }
.share-url-input {
  width: 100%; padding: 8px 10px; border: 0; border-radius: 4px;
  background: #ffffff; color: #374151; font: inherit; font-size: 12.5px;
}

/* ---- Share Toast ---- */
.share-toast {
  position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
  z-index: 50; padding: 10px 20px; border-radius: 6px;
  background: rgba(24,24,27,0.95); border: 1px solid var(--border-line);
  color: var(--text-primary); font-size: 13px; pointer-events: none;
}

/* ---- Bottom Horizontal Thumbnails Strip (FlipHTML5-style) ---- */
.thumbs-strip {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 118px; z-index: 38;
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px;
  background: rgba(15, 15, 17, 0.92);
  border-top: 1px solid rgba(255, 255, 255, 0.10);
  backdrop-filter: blur(8px);
}
.thumbs-scroll-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 86px; flex-shrink: 0;
  border: 0; border-radius: 4px;
  background: transparent; color: rgba(255, 255, 255, 0.72);
  cursor: pointer; transition: color 120ms ease, background 120ms ease;
}
.thumbs-scroll-btn:hover { background: rgba(255, 255, 255, 0.08); color: #ffffff; }
.thumbs-track {
  flex: 1; display: flex; align-items: center; gap: 10px;
  overflow-x: auto; overflow-y: hidden;
  scroll-behavior: smooth;
  padding: 4px 2px;
  scrollbar-width: none;
}
.thumbs-track::-webkit-scrollbar { display: none; }

.thumb-spread-btn {
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0; height: 92px; padding: 0;
  border: 2px solid transparent; border-radius: 2px;
  background: transparent; cursor: pointer;
  transition: border-color 120ms ease, transform 120ms ease;
}
.thumb-spread-btn:hover { transform: translateY(-2px); border-color: rgba(255, 255, 255, 0.45); }
.thumb-spread-btn.is-active { border-color: #f97316; }

.thumb-spread-btn canvas {
  height: 88px; width: auto; display: block;
  background: #ffffff;
}

/* ---- Print ---- */
.print-container { display: none; }
@media print {
  html, body { height: auto; overflow: visible; background: #fff; }
  .reader-stage, .bottom-bar, .thumbs-dialog { display: none !important; }
  .print-container { display: block !important; }
  .print-page { page-break-after: always; break-after: page; display: flex; justify-content: center; }
  .print-page canvas { max-width: 100%; height: auto; }
}

/* ---- Responsive ---- */
@media (max-width: 768px) {
  .reader-stage { padding: 8px 10px; }
  .scrubber-cluster { display: none; }
}

@media (max-width: 540px) {
  .side-arrow { display: none; }
  .bottom-bar { padding: 0 8px; gap: 4px; }
  .tb-btn { min-width: 32px; min-height: 32px; }
}

@media (pointer: coarse) {
  .tb-btn { min-width: 40px; min-height: 40px; }
}
