* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg, #1a1a1a);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

.stage {
  position: fixed;
  inset: 0;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, var(--bg2, #2b2b2b) 0%, var(--bg, #1a1a1a) 100%);
}

/* Loader */
.loader {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: #0a0a0a;
  color: #ddd;
  z-index: 50;
  transition: opacity .5s ease;
}
.loader.hide { opacity: 0; pointer-events: none; }
.spinner {
  width: 46px; height: 46px;
  border: 4px solid rgba(255,255,255,.15);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loader-title { font-size: 17px; letter-spacing: .5px; text-align: center; padding: 0 20px; }
.loader-pct { font-size: 13px; opacity: .6; }

/* Preview banner */
.preview-banner {
  position: fixed; top: 0; left: 0; right: 0;
  background: #d97706; color: #fff;
  text-align: center; font-size: 13px; padding: 6px 12px;
  z-index: 80;
}

/* Brand logo */
.brand-logo {
  position: fixed; top: 14px; left: 16px; z-index: 45;
  display: block; opacity: .92;
  transition: opacity .2s, transform .2s;
}
.brand-logo:hover { opacity: 1; transform: scale(1.03); }
.brand-logo img { height: 46px; width: auto; max-width: 180px; display: block; filter: drop-shadow(0 2px 6px rgba(0,0,0,.4)); }

/* Book */
.book-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 10px 84px;
}
.flip-viewport {
  position: relative;
  flex: 0 0 auto;
  transform-origin: center center;
  touch-action: none;
}
.flipbook { display: block; width: 100%; }

/* Page appearance — realistic magazine paper */
.flipbook .page {
  background: #fefefe;
  overflow: hidden;
  position: relative;
}
.flipbook .page img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
}
/* pages whose image has not been attached yet (sliding window) */
.flipbook .page img:not([src]) { visibility: hidden; }
/* soft gutter shading toward the spine */
.flipbook .page.--left::after,
.flipbook .page.--right::after {
  content: "";
  position: absolute; top: 0; bottom: 0; width: 34px;
  pointer-events: none;
}
.flipbook .page.--left::after {
  right: 0;
  background: linear-gradient(to left, rgba(0,0,0,.22), rgba(0,0,0,0));
}
.flipbook .page.--right::after {
  left: 0;
  background: linear-gradient(to right, rgba(0,0,0,.22), rgba(0,0,0,0));
}
/* hard cover feel */
.flipbook .page.--hard {
  background: #f3f3f3;
  box-shadow: inset 0 0 40px rgba(0,0,0,.12);
}

/* Toolbar */
.toolbar {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 2px;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--toolbar, #000);   /* fallback for browsers without color-mix */
  background: color-mix(in srgb, var(--toolbar, #000) 92%, transparent);
  color: var(--icon, #ecf5fb);
  padding: 6px 8px;
  border-radius: 40px;
  box-shadow: 0 8px 30px rgba(0,0,0,.45);
  z-index: 40;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: opacity .3s, transform .3s;
  max-width: calc(100vw - 16px);
}
.tb-btn {
  background: transparent;
  border: none;
  color: var(--icon, #ecf5fb);
  width: 40px; height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, transform .1s;
  flex: 0 0 auto;
}
.tb-btn:hover { background: rgba(255,255,255,.15); }
.tb-btn:active { transform: scale(.9); }
.tb-btn.active { background: rgba(255,255,255,.28); }
.tb-btn svg { width: 20px; height: 20px; fill: currentColor; }
.pager {
  display: flex; align-items: center; gap: 4px;
  padding: 0 6px;
  font-size: 14px;
  color: var(--icon, #ecf5fb);
  flex: 0 0 auto;
}
.pager input {
  width: 44px;
  background: rgba(255,255,255,.12);
  border: 1px solid rgba(255,255,255,.2);
  color: inherit;
  text-align: center;
  border-radius: 6px;
  padding: 5px 2px;
  font-size: 14px;
  -moz-appearance: textfield;
}
.pager input::-webkit-outer-spin-button,
.pager input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.pager-total { opacity: .8; white-space: nowrap; }
.toolbar::-webkit-scrollbar { display: none; }

/* Thumbnails */
.thumb-panel {
  position: fixed;
  top: 0; left: 0;
  width: 320px;
  max-width: 86vw;
  height: 100%;
  height: 100dvh;
  background: #161616;
  color: #eee;
  z-index: 62;
  transform: translateX(0);
  transition: transform .3s ease;
  display: flex;
  flex-direction: column;
  box-shadow: 6px 0 30px rgba(0,0,0,.5);
}
.thumb-panel.hidden { transform: translateX(-105%); }
.thumb-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 61;
  transition: opacity .3s;
}
.thumb-backdrop.hidden { opacity: 0; pointer-events: none; }
.thumb-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  font-weight: 600;
}
.tp-close { background: none; border: none; color: #eee; font-size: 18px; cursor: pointer; }
.thumb-grid {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  /* The grid has a definite height (flex:1 in a full-height column), so `auto`
     rows get squashed/stretched to fit it instead of scrolling. Size rows by
     their content and let the panel scroll. */
  grid-auto-rows: max-content;
  align-content: start;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.thumb-item {
  cursor: pointer;
  background: #262626;
  border-radius: 5px;
  overflow: hidden;
  border: 2px solid transparent;
  transition: border-color .15s, transform .15s;
}
.thumb-item:hover { transform: translateY(-2px); }
.thumb-item.active { border-color: #4aa3ff; }
.thumb-item img { width: 100%; display: block; background: #333; }
.thumb-item .tnum { text-align: center; font-size: 11px; padding: 4px 0; color: #aaa; }

/* ---------- Zoom / reading overlay ---------- */
.zoom-layer {
  position: fixed;
  inset: 0;
  background: #0b0b0b;
  z-index: 70;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: contain;
  cursor: zoom-in;
}
.zoom-layer.is-zoomed { cursor: grab; }
.zoom-layer.is-zoomed:active { cursor: grabbing; }

.zoom-stage {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  will-change: transform;
  background: #fff;
  box-shadow: 0 10px 40px rgba(0,0,0,.6);
}
.zoom-stage .z-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  -webkit-user-drag: none;
  user-select: none;
}
/* the HD render fades in over the already visible standard page */
.zoom-stage .z-hi { opacity: 0; transition: opacity .25s ease; }
.zoom-stage .z-hi.on { opacity: 1; }

.zoom-top {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  z-index: 72;
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(0,0,0,.6), rgba(0,0,0,0));
}
.zoom-top > * { pointer-events: auto; }
.z-page {
  font-size: 13px; color: #fff; opacity: .85;
  background: rgba(0,0,0,.45); padding: 5px 11px; border-radius: 20px;
}
.z-hd {
  font-size: 12px; color: #fff;
  background: rgba(255,255,255,.14); padding: 5px 11px; border-radius: 20px;
  opacity: 0; transition: opacity .2s;
}
.z-hd.on { opacity: 1; }
.zoom-close {
  margin-left: auto;
  width: 42px; height: 42px; border-radius: 50%;
  background: rgba(255,255,255,.16); color: #fff; border: none;
  font-size: 19px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.zoom-close:hover { background: rgba(255,255,255,.28); }

.zoom-bar {
  position: absolute;
  left: 50%; transform: translateX(-50%);
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 2px;
  background: rgba(20,20,20,.9);
  border-radius: 40px;
  padding: 5px 8px;
  z-index: 72;
  box-shadow: 0 8px 30px rgba(0,0,0,.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.zb-btn {
  width: 40px; height: 40px; border-radius: 50%;
  background: transparent; border: none; color: #fff;
  font-size: 22px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.zb-btn:hover { background: rgba(255,255,255,.16); }
.zb-btn:active { transform: scale(.92); }
.z-level {
  min-width: 54px; text-align: center;
  font-size: 13px; color: #fff; opacity: .85;
  font-variant-numeric: tabular-nums;
}

/* the flipbook toolbar must not sit on top of the reading overlay */
body.zoom-open .toolbar,
body.zoom-open .brand-logo { opacity: 0; pointer-events: none; }

.hidden { display: none !important; }

/* Toast */
.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.85); color: #fff; padding: 9px 18px; border-radius: 22px;
  z-index: 100; font-size: 14px; box-shadow: 0 4px 16px rgba(0,0,0,.4);
}

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
  .book-wrap { padding: 8px 4px 76px; }
  .brand-logo { top: 10px; left: 10px; }
  .brand-logo img { height: 34px; max-width: 120px; }
  .toolbar { gap: 0; padding: 5px 6px; bottom: max(10px, env(safe-area-inset-bottom)); }
  .tb-btn { width: 38px; height: 38px; }
  .tb-btn svg { width: 19px; height: 19px; }
  /* keep the essentials on small screens; hide the rest */
  #btnShare, #btnFirst, #btnLast { display: none; }
  .zb-btn { width: 38px; height: 38px; }
  .toast { bottom: 96px; }
}
@media (max-width: 420px) {
  #btnAuto { display: none; }
  .tb-btn { width: 34px; height: 34px; }
  .pager input { width: 38px; }
}
