/* The reading room: a warm desk under a lamp, the book in the middle. */

:root {
  --desk-1: #3e2a1b;
  --desk-2: #24180f;
  --desk-3: #120b06;
  --cream: #f3e7cf;
  --cream-dim: #cdbb9b;
  --brown: #8a5a32;
  --gold: #d9a45b;
  --ui-bg: rgba(20, 12, 7, .55);
  --ui-line: rgba(243, 231, 207, .16);
  --paper: #f8f0de;
  --bar-h: 64px;
  --foot-h: 70px;
  color-scheme: dark;
}
/* the room is always lamp-lit; a light system theme only lifts the desk a touch */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) { --desk-1: #4a3322; --desk-2: #2c1e13; --desk-3: #181009; }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--desk-3);
  color: var(--cream);
  font-family: "Garamond", Georgia, "Times New Roman", serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.scene {
  position: fixed; inset: 0;
  background:
    radial-gradient(ellipse 70% 60% at 50% 46%, rgba(255, 196, 120, .16), transparent 70%),
    radial-gradient(ellipse at 50% 40%, var(--desk-1) 0%, var(--desk-2) 48%, var(--desk-3) 100%);
}
.scene::before { /* fine grain so the backdrop reads as a surface, not a gradient */
  content: ""; position: absolute; inset: 0; pointer-events: none; opacity: .5;
  background: url(../assets/paper-grain.png) repeat; background-size: 180px;
  mix-blend-mode: soft-light;
}

/* ---------- top bar ---------- */
.topbar {
  position: absolute; top: 0; left: 0; right: 0; height: var(--bar-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 22px; z-index: 20;
}
.brand { display: flex; align-items: baseline; gap: 14px; min-width: 0; }
.brand .ur { font-family: "Nastaliq", serif; font-size: 25px; line-height: 1; color: var(--cream); direction: rtl; transform: translateY(4px); }
.brand .en { font-size: 16px; letter-spacing: .04em; color: var(--cream-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand .en i { color: var(--gold); font-style: italic; }
.tools { display: flex; gap: 6px; }
.tbtn {
  appearance: none; border: 1px solid transparent; background: transparent; color: var(--cream);
  height: 40px; min-width: 40px; padding: 0 10px; border-radius: 20px;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  font: 500 15px/1 "Garamond", Georgia, serif; letter-spacing: .03em; cursor: pointer;
  text-decoration: none; transition: background .2s, border-color .2s, color .2s;
}
.tbtn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.tbtn:hover { background: rgba(243, 231, 207, .08); border-color: var(--ui-line); }
.tbtn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.tbtn[aria-pressed="true"] { background: rgba(217, 164, 91, .18); border-color: rgba(217, 164, 91, .45); color: #ffe3b8; }
.tbtn .lbl { display: inline; }
.tbtn[aria-pressed="false"] .on, .tbtn[aria-pressed="true"] .off { display: none; }

/* ---------- stage ---------- */
.stage {
  position: absolute; left: 0; right: 0; top: var(--bar-h); bottom: var(--foot-h);
  display: flex; align-items: center; justify-content: center;
}
.book-wrap { position: absolute; inset: 8px 88px 10px; }
#book { position: absolute; inset: 0; transition: transform .85s cubic-bezier(.45, .05, .25, 1); }
#book > .stf__parent { width: 100%; height: 100%; }
.rebuilding #book, .rebuilding .book-shadow, .rebuilding .edge { opacity: 0; transition: opacity .25s; }
#book { opacity: 1; }
#book .stf__wrapper { height: 100% !important; padding-bottom: 0 !important; }
.book-shadow {
  position: absolute; pointer-events: none; border-radius: 3px;
  box-shadow: 0 30px 60px -12px rgba(0, 0, 0, .75), 0 12px 24px -8px rgba(0, 0, 0, .6), 0 0 0 1px rgba(0, 0, 0, .25);
  transition: left .85s cubic-bezier(.45, .05, .25, 1), width .85s cubic-bezier(.45, .05, .25, 1), opacity .4s;
}
/* stacks of page edges beside the open book, thicker on the side that holds more pages */
.edge { position: absolute; pointer-events: none; opacity: 0; transition: opacity .4s, width .5s, left .5s;
  background:
    repeating-linear-gradient(90deg, #e9dcc0 0 1px, #d2c09c 1px 2px),
    #e2d3b3;
  box-shadow: inset 0 0 6px rgba(80, 50, 20, .35), 0 20px 40px -10px rgba(0, 0, 0, .6);
}
.edge.l { border-radius: 3px 0 0 3px; }
.edge.r { border-radius: 0 3px 3px 0; }
.is-open .edge { opacity: 1; }

/* leaves handed to the page-flip engine */
.leaf { background: transparent; }
.leaf > .page { position: absolute; inset: 0; width: 100%; height: 100%;
  background: var(--paper) url(../assets/paper-grain.png) repeat; background-size: 22cqw; }
.leaf > .page.cover { background: #2b1a10; }
.leaf > .page.endpaper { background: #eadbbd url(../assets/paper-grain.png) repeat; background-size: 30cqw; }
/* spine shading: an Urdu book's right-hand page is bound on its left edge, and vice versa */
.leaf > .page::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 3;
}
.leaf.--right > .page::after { background: linear-gradient(90deg, rgba(60, 35, 15, .28), rgba(60, 35, 15, .08) 4%, transparent 11%), linear-gradient(270deg, rgba(60, 35, 15, .07), transparent 3%); }
.leaf.--left > .page::after  { background: linear-gradient(270deg, rgba(60, 35, 15, .28), rgba(60, 35, 15, .08) 4%, transparent 11%), linear-gradient(90deg, rgba(60, 35, 15, .07), transparent 3%); }
.leaf.--left > .page.cover::after { background: linear-gradient(270deg, rgba(0, 0, 0, .35), rgba(255, 255, 255, .12) 1.4%, transparent 3.5%); }
.leaf.--right > .page.cover::after { background: linear-gradient(90deg, rgba(0, 0, 0, .35), rgba(255, 255, 255, .12) 1.4%, transparent 3.5%); }

/* side arrows */
.nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 15;
  width: 56px; height: 56px; border-radius: 50%; border: 1px solid var(--ui-line);
  background: rgba(20, 12, 7, .35); color: var(--cream); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .2s, opacity .3s, transform .2s;
}
.nav svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.nav:hover { background: rgba(217, 164, 91, .2); }
.nav:active { transform: translateY(-50%) scale(.94); }
.nav:focus-visible { outline: 2px solid var(--gold); outline-offset: 3px; }
.nav[disabled] { opacity: .15; pointer-events: none; }
.nav.fwd { left: 16px; }
.nav.back { right: 16px; }

/* ---------- bottom bar ---------- */
.footbar {
  position: absolute; left: 0; right: 0; bottom: 0; height: var(--foot-h);
  display: flex; align-items: center; justify-content: center; gap: 18px; padding: 0 24px 6px; z-index: 20;
}
.where { min-width: 150px; text-align: center; font-size: 15px; color: var(--cream-dim); letter-spacing: .03em; }
.where b { color: var(--cream); font-weight: 600; }
.scrub { width: min(520px, 52vw); direction: rtl; accent-color: var(--gold); height: 28px; background: transparent; cursor: pointer; }
.scrub::-webkit-slider-runnable-track { height: 3px; background: rgba(243, 231, 207, .22); border-radius: 2px; }
.scrub::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; margin-top: -6.5px; border-radius: 50%; background: var(--cream); border: 3px solid var(--brown); box-shadow: 0 2px 6px rgba(0, 0, 0, .5); }
.scrub::-moz-range-track { height: 3px; background: rgba(243, 231, 207, .22); border-radius: 2px; }
.scrub::-moz-range-thumb { width: 12px; height: 12px; border-radius: 50%; background: var(--cream); border: 3px solid var(--brown); }
.scrub { -webkit-appearance: none; appearance: none; }

/* ---------- contents drawer ---------- */
.drawer {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(380px, 88vw); z-index: 40;
  background: linear-gradient(180deg, #2c1d12, #1c120b); border-left: 1px solid var(--ui-line);
  box-shadow: -30px 0 60px rgba(0, 0, 0, .5);
  transform: translateX(105%); transition: transform .45s cubic-bezier(.3, .7, .2, 1);
  display: flex; flex-direction: column;
}
.drawer.open { transform: none; }
.drawer header { display: flex; align-items: center; justify-content: space-between; padding: 18px 18px 8px 24px; }
.drawer h2 { margin: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; font: 400 30px/1.7 "Nastaliq", serif; color: var(--cream); }
.drawer h2 span { direction: rtl; }
.drawer h2 small { font: italic 400 15px/1 "Garamond", serif; color: var(--cream-dim); letter-spacing: .06em; }
.drawer ol { list-style: none; margin: 0; padding: 6px 12px 24px; overflow: auto; flex: 1; }
.drawer li button {
  width: 100%; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 12px;
  background: transparent; border: 0; border-radius: 10px; color: var(--cream); cursor: pointer;
  padding: 6px 14px; text-align: right; transition: background .2s;
}
.drawer li button:hover, .drawer li button:focus-visible { background: rgba(243, 231, 207, .07); outline: none; }
.drawer li .ur { font: 500 19px/2 "Nastaliq", serif; direction: rtl; }
.drawer li .ur small { font-size: 14px; color: var(--cream-dim); }
.drawer li .en { display: block; font: italic 14px/1.25 "Garamond", "HonorificSign", serif; color: var(--cream-dim); direction: ltr; text-align: right; margin: 0 0 4px; }
.drawer li .pg { font: 600 15px/1 "Garamond", serif; color: var(--gold); order: -1; min-width: 2ch; text-align: left; }
.veil { position: absolute; inset: 0; background: rgba(0, 0, 0, .45); z-index: 35; opacity: 0; pointer-events: none; transition: opacity .35s; }
.veil.on { opacity: 1; pointer-events: auto; }

/* ---------- hint + loader ---------- */
.hint {
  position: absolute; left: 50%; bottom: calc(var(--foot-h) + 14px); transform: translateX(-50%);
  z-index: 16; padding: 8px 16px; border-radius: 18px; background: var(--ui-bg); border: 1px solid var(--ui-line);
  color: var(--cream); font-size: 15px; letter-spacing: .02em; white-space: nowrap;
  transition: opacity .6s; pointer-events: none;
}
.hint .ur { font-family: "Nastaliq", serif; direction: rtl; margin-left: 10px; font-size: 15px; }
.hint.gone { opacity: 0; }

.loader {
  position: fixed; inset: 0; z-index: 100; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: radial-gradient(ellipse at 50% 42%, #3a2718, #150d07 70%);
  transition: opacity .8s ease, visibility .8s;
}
.loader.done { opacity: 0; visibility: hidden; }
.loader .mark { position: relative; font: 700 64px/1.6 "Nastaliq", serif; color: var(--cream); direction: rtl; }
.loader .mark::before { /* the first ray */
  content: ""; position: absolute; left: 50%; top: -30%; width: 3px; height: 80%;
  background: linear-gradient(180deg, transparent, #ffcf8a, transparent);
  transform: translateX(-50%) rotate(28deg); transform-origin: 50% 0; filter: blur(.5px);
  animation: ray 2.2s ease-in-out infinite;
}
@keyframes ray { 0%, 100% { opacity: .15; } 50% { opacity: .9; } }
.loader .sub { font: italic 17px/1 "Garamond", serif; color: var(--cream-dim); letter-spacing: .08em; }

/* the measuring room: pages are laid out here once, then handed to the book */
#measure { position: fixed; left: -99999px; top: 0; width: 560px; height: 904px; visibility: hidden; }
#measure .page { width: 560px; height: 904px; }

/* ---------- small screens ---------- */
@media (max-width: 760px) {
  :root { --bar-h: 54px; --foot-h: 62px; }
  .topbar { padding: 0 10px; }
  .brand .en { display: none; }
  .brand .ur { font-size: 22px; }
  .tools { gap: 0; }
  .tbtn { min-width: 38px; padding: 0 8px; }
  .tbtn .lbl { display: none; }
  .book-wrap { inset: 4px 8px 6px; }
  /* the arrows move down beside the page slider, within thumb reach */
  .nav { position: fixed; top: auto; bottom: calc((var(--foot-h) - 48px) / 2 + env(safe-area-inset-bottom, 0px)); transform: none; width: 48px; height: 48px; z-index: 25; background: rgba(20, 12, 7, .55); }
  .nav:active { transform: scale(.92); }
  .nav.fwd { left: 10px; }
  .nav.back { right: 10px; }
  .footbar { padding: 0 66px 6px; gap: 12px; }
  .scrub { width: 40vw; }
  .where { min-width: 0; font-size: 14px; }
}

/* ---------- single-page turner (phones) ---------- */
.sp { position: absolute; inset: 0; perspective: 1700px; perspective-origin: 50% 50%; touch-action: none; }
.sp-slot, .sp-turn, .sp-cast { position: absolute; }
.sp-slot .leaf, .sp-turn .leaf { position: absolute; inset: 0; }
.sp-turn { transform-origin: 100% 50%; transform-style: preserve-3d; z-index: 3; will-change: transform; }
.sp-turn .leaf { backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.sp-gloss { position: absolute; inset: 0; z-index: 4; pointer-events: none; opacity: 0;
  background: linear-gradient(90deg, rgba(25, 14, 6, .9), rgba(25, 14, 6, .35) 70%, rgba(25, 14, 6, .55));
  backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.sp-back { position: absolute; inset: 0; transform: rotateY(180deg); backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: #eadbbd url(../assets/paper-grain.png) repeat; background-size: 120px;
  box-shadow: inset -18px 0 24px -12px rgba(60, 35, 15, .35); }
.sp-cast { z-index: 2; pointer-events: none; opacity: 0; }
@media (prefers-reduced-motion: reduce) {
  #book, .book-shadow, .drawer, .loader { transition-duration: .01s !important; }
}
