/* ============================================================
   Book OS — Reader (immersive full-bleed reading)
   ============================================================ */

.reader {
  --rbg: #121212;
  --rtool: rgba(18,18,18,.92);
  --rfg: #f2f2f2;
  position: fixed; inset: 0; background: var(--rbg);
  /* Stage is always full-screen; chrome floats on top (true immersive reading). */
  display: block;
  overflow: hidden; user-select: none; -webkit-user-select: none;
  color: var(--rfg);
  touch-action: none;
}
.reader.light { --rbg: #d8d8d8; --rtool: rgba(255,255,255,.94); --rfg: #1e2447; }
.reader.sepia { --rbg: #e8dcc4; --rtool: rgba(247,241,227,.94); --rfg: #3d3426; }
.reader.night { --rbg: #0a0a0a; --rtool: rgba(14,14,14,.94); --rfg: #e7e9f0; }

/* ---- floating chrome (does not shrink the page stage) ---- */
.r-top, .r-bottom {
  position: absolute; left: 0; right: 0; z-index: 30; color: inherit;
  background: var(--rtool);
  display: flex; align-items: center; gap: 4px;
  padding: 6px 8px;
  padding-left: max(8px, env(safe-area-inset-left, 0px));
  padding-right: max(8px, env(safe-area-inset-right, 0px));
  box-shadow: 0 1px 12px rgba(0,0,0,.22);
  transition: transform .28s ease, opacity .28s ease;
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
}
.r-top {
  top: 0;
  padding-top: max(6px, env(safe-area-inset-top, 0px));
}
.r-bottom {
  bottom: 0;
  padding-bottom: max(6px, env(safe-area-inset-bottom, 0px));
}
.reader.chrome-hidden .r-top { transform: translateY(-110%); opacity: 0; pointer-events: none; }
.reader.chrome-hidden .r-bottom { transform: translateY(110%); opacity: 0; pointer-events: none; }
.reader.chrome-hidden .r-zoomctl { opacity: 0; pointer-events: none; transform: translateY(20px); }
.reader.chrome-hidden .r-progress { opacity: .55; }

/* Always-tappable strips so toolbar/slider can be brought back */
.r-chrome-peek {
  display: none;
  position: absolute; left: 0; right: 0; z-index: 40;
  border: 0; padding: 0; margin: 0;
  background: transparent; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.r-chrome-peek.top {
  top: 0; height: max(36px, env(safe-area-inset-top, 0px) + 28px);
}
.r-chrome-peek.bottom {
  bottom: 0; height: max(40px, env(safe-area-inset-bottom, 0px) + 32px);
}
.reader.chrome-hidden .r-chrome-peek { display: block; }
.reader.chrome-hidden .r-chrome-peek.bottom::after {
  content: '';
  position: absolute; left: 50%; bottom: 10px;
  width: 42px; height: 4px; margin-left: -21px;
  border-radius: 4px; background: rgba(108, 99, 255, 0.55);
  pointer-events: none;
}

.r-top .r-title { min-width: 0; }
.r-top .r-title b { font-size: 13px; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.r-top .r-title span { font-size: 11px; opacity: .65; }

.r-btn {
  border: 0; background: transparent; cursor: pointer; color: inherit;
  width: 40px; height: 40px; border-radius: 11px; display: grid; place-items: center;
  transition: .14s; flex: none; -webkit-tap-highlight-color: transparent;
}
.r-btn:active { background: rgba(255,255,255,.1); }
.r-btn.on { background: rgba(108,99,255,.25); color: #c4bfff; }
.reader.light .r-btn.on { background: #edeaff; color: var(--primary-ink); }

/* ---- stage fills entire screen ---- */
.r-stage {
  position: absolute; inset: 0;
  overflow: hidden;
  display: grid; place-items: center; touch-action: none;
}
.r-tapzones {
  position: absolute; inset: 0; z-index: 15; display: grid;
  grid-template-columns: 22% 56% 22%; pointer-events: none;
}
.r-tapzones span { pointer-events: none; }
.r-viewport {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: flex-start;
  transition: transform .05s linear;
}

.r-track { display: flex; height: 100%; align-items: center; will-change: transform; }
.r-track.animate { transition: transform .32s cubic-bezier(.22,.61,.36,1); }
.r-spread { display: flex; align-items: center; justify-content: center; gap: 0; height: 100%; flex: none; }
.r-page { height: 100%; display: flex; align-items: center; justify-content: center; perspective: 1800px; }
.r-page img {
  max-height: 100%; max-width: 100%; width: auto; height: auto;
  object-fit: contain;
  background: #fff;
  box-shadow: 0 2px 24px rgba(0,0,0,.35);
}
.reader.light .r-page img { box-shadow: 0 4px 20px rgba(20,22,29,.14); }

.r-scroll { position: absolute; inset: 0; overflow-y: auto; padding: 0; text-align: center; touch-action: pan-y; }
.r-scroll img { max-width: 100%; width: 100%; margin: 0; box-shadow: none; display: block; }

/* StPageFlip — edge-to-edge book */
.r-flipwrap {
  position: absolute; inset: 0; z-index: 12;
  display: flex; align-items: center; justify-content: center;
  gap: 0; padding: 0;
}
.r-flipbook {
  /* Sized by JS — must NOT be 100% or StPageFlip stays in 2-page landscape mode */
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}
.r-flipbook.r-single-leaf {
  overflow: hidden;
  background: #fff;
  box-shadow: 0 8px 36px rgba(0, 0, 0, 0.28);
  border-radius: 2px;
}
.r-single-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #fff;
  user-select: none;
  -webkit-user-drag: none;
}
.r-single-img.turn-next {
  animation: r-leaf-next 0.42s ease;
}
.r-single-img.turn-prev {
  animation: r-leaf-prev 0.42s ease;
}
@keyframes r-leaf-next {
  from { transform: perspective(1400px) rotateY(-16deg) translateX(8px); opacity: 0.65; }
  to { transform: none; opacity: 1; }
}
@keyframes r-leaf-prev {
  from { transform: perspective(1400px) rotateY(16deg) translateX(-8px); opacity: 0.65; }
  to { transform: none; opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .r-single-img.turn-next, .r-single-img.turn-prev { animation: none; }
}
.r-flipbook .stf__parent,
.r-flipbook .stf__wrapper { margin: 0 auto !important; }
.r-flipbook .stf__item {
  background: #fff !important;
  box-shadow: 0 4px 28px rgba(0,0,0,.32);
}
.r-flipbook .stf__item img,
.r-flipbook img.--page {
  width: 100%; height: 100%; object-fit: contain; display: block;
  background: #fff; user-select: none; -webkit-user-drag: none;
}

/* Invisible edge hit zones — no big arrows over the page */
.r-side-nav {
  position: absolute; top: 0; bottom: 0; z-index: 18;
  width: 18%; max-width: 72px; border: 0; border-radius: 0;
  background: transparent; color: transparent; cursor: pointer;
  display: block; padding: 0; margin: 0;
  -webkit-tap-highlight-color: transparent;
  opacity: 0;
}
.r-side-nav.prev { left: 0; }
.r-side-nav.next { right: 0; }
.r-side-nav:focus { outline: none; }
.reader.mode-flip .r-zoomctl,
.reader.mode-flip .r-tapzones,
.reader.mode-flip .r-edge-hint { display: none !important; }

.reader.view-single .r-flipwrap,
.reader.view-double .r-flipwrap {
  max-width: 100%; margin: 0 auto; left: 0; right: 0;
}

/* Rotate tip for landscape page scans on portrait phones */
.r-rotate-tip {
  position: absolute; left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 28; max-width: min(92vw, 360px);
  background: rgba(0,0,0,.78); color: #fff;
  font-size: 12px; font-weight: 650; line-height: 1.35;
  padding: 10px 14px; border-radius: 12px; text-align: center;
  pointer-events: none;
  opacity: 0; transition: opacity .35s ease;
}
.r-rotate-tip.show { opacity: 1; }
.reader.chrome-hidden .r-rotate-tip { bottom: max(28px, env(safe-area-inset-bottom, 0px)); }

.r-viewseg {
  display: inline-flex; align-items: center; gap: 2px;
  padding: 3px; border-radius: 10px;
  background: rgba(255,255,255,.1); flex: none;
}
.reader.light .r-viewseg { background: rgba(30,36,71,.06); }
.r-viewseg button {
  border: 0; cursor: pointer; color: inherit;
  background: transparent; border-radius: 8px;
  padding: 6px 9px; font-size: 12px; font-weight: 800;
  display: inline-flex; align-items: center; gap: 4px;
  opacity: .7;
}
.r-viewseg button.active {
  background: rgba(108,99,255,.35); color: #fff; opacity: 1;
}
.reader.light .r-viewseg button.active {
  background: #fff; color: var(--primary-ink, #4b45c9);
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}

.r-edge-hint {
  position: absolute; top: 0; bottom: 0; width: 14%; z-index: 14;
  pointer-events: none; opacity: 0; transition: opacity .35s ease;
}
.r-edge-hint.left {
  left: 0;
  background: linear-gradient(90deg, rgba(0,0,0,.12), transparent 80%);
}
.r-edge-hint.right {
  right: 0;
  background: linear-gradient(270deg, rgba(0,0,0,.12), transparent 80%);
}
.r-edge-hint::after { display: none; }
.reader.chrome-hidden .r-edge-hint { opacity: .4; }

.r-page.load { position: relative; }
.r-page.load::after {
  content: ''; position: absolute; inset: 8% 12%; border-radius: 2px;
  background: linear-gradient(100deg, #2a2a2a 30%, #3a3a3a 50%, #2a2a2a 70%);
  background-size: 200% 100%; animation: shimmer 1.2s infinite;
}
.reader.light .r-page.load::after {
  background: linear-gradient(100deg, #e9ecf3 30%, #f4f6fb 50%, #e9ecf3 70%);
  background-size: 200% 100%;
}
@keyframes shimmer { to { background-position: -200% 0; } }
.r-page.err::after { content: 'Page failed to load'; position: absolute; inset: 0;
  display: grid; place-items: center; color: #999; font-size: 13px; background: #222; }

.r-bottom { justify-content: center; gap: 8px; }
.r-slider { flex: 1; max-width: 560px; display: flex; align-items: center; gap: 10px; }
.r-slider input[type=range] { flex: 1; accent-color: #6c63ff; height: 28px; }
.r-count { font-size: 12px; font-weight: 700; min-width: 72px; text-align: center; opacity: .9; }
.r-progress { position: absolute; top: 0; left: 0; height: 2px; background: #6c63ff; z-index: 40; transition: width .2s, opacity .28s; }

.r-zoomctl {
  position: absolute; right: max(10px, env(safe-area-inset-right, 0px));
  bottom: max(10px, env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; gap: 8px; z-index: 25;
  transition: transform .28s ease, opacity .28s ease;
}
.r-zoomctl button {
  width: 42px; height: 42px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--rtool); color: inherit; box-shadow: 0 4px 16px rgba(0,0,0,.35); display: grid; place-items: center;
}
.r-zoomlabel { position: absolute; left: 14px; top: 12px; z-index: 25;
  background: rgba(0,0,0,.72); color:#fff; font-size:12px; font-weight:700; padding:4px 10px; border-radius:999px; display:none; }

.r-overlay {
  position: absolute; inset: 0; z-index: 60; background: rgba(0,0,0,.55);
  display: none; opacity: 0; transition: opacity .2s;
}
.r-overlay.show { display: block; opacity: 1; }
.r-panel {
  position: absolute; top: 0; bottom: 0; width: min(360px, 90vw);
  background: #161616; color: #eee; box-shadow: var(--shadow-lg); overflow-y: auto;
  transform: translateX(-100%); transition: transform .24s;
  padding-top: env(safe-area-inset-top, 0);
}
.reader.light .r-panel { background: #fff; color: #1e2447; }
.r-panel.right { right: 0; left: auto; transform: translateX(100%); }
.r-overlay.show .r-panel { transform: none; }
.r-panel header { position: sticky; top: 0; background: inherit; padding: 16px 18px; border-bottom: 1px solid rgba(255,255,255,.08); display: flex; align-items: center; justify-content: space-between; }
.reader.light .r-panel header { border-bottom-color: var(--border); }
.r-panel header b { font-size: 16px; }
.r-panel-body { padding: 12px 14px; }

.toc-unit { margin-bottom: 6px; border-radius: 12px; overflow: hidden; }
.toc-unit > button { width: 100%; text-align: left; border: 0; background: rgba(255,255,255,.06); cursor: pointer;
  padding: 12px 14px; font: inherit; font-weight: 700; color: inherit; display: flex; align-items: center; gap: 10px; }
.reader.light .toc-unit > button { background: var(--surface-2); color: var(--ink); }
.toc-ch { display: block; width: 100%; text-align: left; border: 0; background: transparent; cursor: pointer;
  padding: 10px 16px 10px 40px; font: inherit; font-size: 14px; color: inherit; opacity: .88; border-radius: 8px; }
.toc-ch:hover { background: rgba(255,255,255,.06); }
.toc-ch .pg { float: right; opacity: .5; font-size: 12px; }
.toc-body { display: none; padding: 4px; }
.toc-unit.open .toc-body { display: block; }

.thumbs-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.thumbs-grid figure { margin: 0; cursor: pointer; border-radius: 8px; overflow: hidden; border: 2px solid transparent; }
.thumbs-grid figure.active { border-color: #6c63ff; }
.thumbs-grid img { width: 100%; aspect-ratio: 3/4; object-fit: cover; background: #333; }
.thumbs-grid figcaption { text-align: center; font-size: 11px; opacity: .6; padding: 3px; }

.set-group { margin-bottom: 18px; }
.set-group > label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; opacity: .55; font-weight: 700; }
.set-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; }
.swatches { display: flex; gap: 10px; }
.swatch { width: 34px; height: 34px; border-radius: 50%; cursor: pointer; border: 2px solid rgba(255,255,255,.2); }
.swatch.active { border-color: #6c63ff; box-shadow: 0 0 0 3px rgba(108,99,255,.25); }

.tgl { width: 44px; height: 26px; border-radius: 999px; background: rgba(255,255,255,.2); position: relative; cursor: pointer; transition: .18s; border: 0; }
.tgl::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: .18s; }
.tgl.on { background: #6c63ff; }
.tgl.on::after { transform: translateX(18px); }

.reader .seg { background: rgba(255,255,255,.08); }
.reader.light .seg { background: var(--surface-2); }
.reader .seg button { color: inherit; }
.reader .seg button.active { background: rgba(108,99,255,.35); color: #fff; }
.reader.light .seg button.active { background: #fff; color: var(--primary-ink); }

@media (max-width: 640px) {
  .r-top .r-title b { max-width: 28vw; }
  .r-top .r-btn.hide-sm { display: none; }
  .thumbs-grid { grid-template-columns: repeat(2, 1fr); }
  .r-slider { max-width: none; }
  .r-viewseg { display: inline-flex; flex: none; }
  .r-viewseg button { padding: 5px 8px; font-size: 11px; gap: 3px; }
}
@media (orientation: landscape) {
  .r-rotate-tip { display: none !important; }
  .r-side-nav { width: 12%; max-width: 56px; }
}
