:root {
  --verdant: #00824A;
  --verdant-700: #005A33;
  --cream: #E2E2DA;
  --surface: #FAFAF7;
  --ink: #15170F;
  --charcoal: #6A6A6A;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(120% 120% at 50% 0%, var(--surface) 0%, var(--cream) 100%);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

/* Top bar */
header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: rgba(250, 250, 247, 0.7);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(21, 23, 15, 0.08);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: -0.01em; }
.brand .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--verdant); flex: 0 0 auto; }
.brand .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 50vw; }
.toolbar { display: flex; align-items: center; gap: 8px; }
.counter {
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  color: var(--charcoal);
  min-width: 84px;
  text-align: center;
  direction: ltr;            /* always read "1 / 52", even on RTL pages */
  unicode-bidi: isolate;
}
button.icon {
  appearance: none;
  border: 1px solid rgba(21, 23, 15, 0.12);
  background: #fff;
  color: var(--ink);
  width: 40px; height: 40px;
  border-radius: 12px;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease, background .12s ease;
  box-shadow: 0 1px 2px rgba(21,23,15,0.05);
}
button.icon:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(21,23,15,0.1); }
button.icon:active { transform: translateY(0); }
button.icon:disabled { opacity: .35; cursor: default; transform: none; box-shadow: none; }
button.icon svg { width: 20px; height: 20px; }
button.icon.active { background: var(--verdant); color: #fff; border-color: var(--verdant); }

/* Stage */
main {
  flex: 1 1 auto;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;            /* spacing handled in fit() so the book never overflows/crops */
  min-height: 0;
}
#flipbook { margin: 0 auto; touch-action: none; transform-origin: center center; transition: transform .3s ease; }
/* Arabic / RTL: mirror the whole canvas; pages are pre-mirrored during render so text stays
   readable. Once loaded, JS controls the transform inline (mirror + zoom). */
#flipbook.rtl { transform: scaleX(-1); }

/* Pan layer — sits over the book only while zoomed in, to drag-pan */
#panlayer {
  position: absolute; inset: 0;
  z-index: 12;
  pointer-events: none;     /* toggled to auto by JS when zoomed */
  touch-action: none;
}

/* Clickable PDF hyperlink hotspots — overlay inside #flipbook, so they ride the same
   zoom/pan transform (and RTL mirror) as the rasterised page canvas. */
#linklayer {
  position: absolute; inset: 0;
  pointer-events: none;     /* empty space falls through to the canvas for swiping */
  z-index: 6;               /* above the page canvas, below the pan layer (12) */
}
#linklayer a.pdf-link {
  position: absolute;
  pointer-events: auto;
  cursor: pointer;
  border-radius: 2px;
  background: transparent;  /* invisible until hovered, like a real link */
  transition: background-color .15s ease, box-shadow .15s ease;
  -webkit-tap-highlight-color: transparent;
}
#linklayer a.pdf-link:hover,
#linklayer a.pdf-link:focus-visible {
  background: rgba(70, 130, 180, 0.18);
  box-shadow: 0 0 0 1px rgba(70, 130, 180, 0.5) inset;
  outline: none;
}

/* Side nav arrows */
.nav-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 48px; height: 48px;
  border-radius: 50%;
  border: none;
  background: rgba(21,23,15,0.55);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  z-index: 15;
  transition: background .12s ease, opacity .12s ease;
}
.nav-arrow:hover { background: var(--verdant); }
.nav-arrow:disabled { opacity: 0; pointer-events: none; }
.nav-arrow.prev { left: 14px; }
.nav-arrow.next { right: 14px; }
.nav-arrow svg { width: 24px; height: 24px; }

/* Loading overlay */
#overlay {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 18px;
  background: var(--surface);
  z-index: 30;
  text-align: center;
  padding: 24px;
}
.spinner {
  width: 46px; height: 46px;
  border: 3px solid rgba(0,130,74,0.18);
  border-top-color: var(--verdant);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.progress-wrap { width: min(280px, 70vw); }
.progress-track { height: 6px; background: rgba(21,23,15,0.08); border-radius: 99px; overflow: hidden; }
.progress-bar { height: 100%; width: 0%; background: var(--verdant); border-radius: 99px; transition: width .2s ease; }
.progress-label { margin-top: 10px; font-size: 13px; color: var(--charcoal); }
.overlay-title { font-size: 17px; font-weight: 600; }
.error { color: #b3261e; max-width: 440px; line-height: 1.5; font-size: 14px; }
.error strong { display: block; font-size: 16px; margin-bottom: 6px; color: var(--ink); }
.error code { background: rgba(21,23,15,0.06); padding: 1px 6px; border-radius: 5px; }

.hint {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  font-size: 12px; color: var(--charcoal);
  background: rgba(250,250,247,0.85);
  padding: 6px 12px; border-radius: 99px;
  border: 1px solid rgba(21,23,15,0.08);
  pointer-events: none;
  transition: opacity .4s ease;
}
@media (max-width: 640px) {
  .nav-arrow { display: none; }
  /* button.icon (0-1-1) outranks a bare class, so match it for the hide rules too */
  button.icon.zoombtn { display: none; }  /* mobile zooms via the custom pinch instead */
  #fsBtn { display: none; }               /* fullscreen is unreliable on mobile (esp. iOS Safari) */
  .brand .title { max-width: 34vw; }
}
