/* ---------- fonts (bundled so the app works offline) ---------- */
@font-face { font-family: Amiri; font-weight: 400; font-display: swap; src: url(../fonts/amiri-400-arabic.woff2) format("woff2"); }
@font-face { font-family: Amiri; font-weight: 700; font-display: swap; src: url(../fonts/amiri-700-arabic.woff2) format("woff2"); }

/* ---------- theme tokens ---------- */
:root {
  --tb: 58px;
  --bb: 58px;
  --desk: #ebe5d8;
  --desk-glow: #f6f1e6;
  --surface: rgba(255, 252, 245, 0.84);
  --surface-solid: #fffcf5;
  --surface-2: #f4efe3;
  --border: rgba(70, 56, 30, 0.13);
  --text: #1d2722;
  --muted: #6d716a;
  --accent: #1d6a50;
  --accent-soft: rgba(29, 106, 80, 0.12);
  --accent-ink: #ffffff;
  --gold: #b8893a;
  --paper: #fffcf3;
  --page-filter: sepia(0.07) contrast(1.03);
  --mark-blend: multiply;
  --mark-opacity: 0.36;
  --edge-a: #f4efe3;
  --edge-b: #d8cfbb;
  --book-shadow: 0 28px 50px -24px rgba(48, 36, 12, 0.45), 0 10px 22px -12px rgba(48, 36, 12, 0.3);
  --card-shadow: 0 18px 40px -16px rgba(40, 30, 10, 0.35), 0 2px 6px rgba(40, 30, 10, 0.08);
  --danger: #c93a3a;
  color-scheme: light;
}
[data-theme="sepia"] {
  --desk: #d9c9aa;
  --desk-glow: #e8dbc0;
  --surface: rgba(248, 238, 216, 0.86);
  --surface-solid: #f7ecd5;
  --surface-2: #efe1c3;
  --border: rgba(90, 64, 20, 0.16);
  --text: #2f2414;
  --muted: #7a6a4f;
  --accent: #8a5a1c;
  --accent-soft: rgba(138, 90, 28, 0.13);
  --paper: #f3e5c6;
  --page-filter: sepia(0.5) saturate(0.85) brightness(0.97) contrast(1.04);
  --edge-a: #efe0c0;
  --edge-b: #cdb68b;
}
[data-theme="night"] {
  --desk: #0d1311;
  --desk-glow: #17201c;
  --surface: rgba(24, 31, 28, 0.86);
  --surface-solid: #18201c;
  --surface-2: #212a26;
  --border: rgba(220, 230, 220, 0.1);
  --text: #e4e2d9;
  --muted: #929a93;
  --accent: #58c39a;
  --accent-soft: rgba(88, 195, 154, 0.14);
  --accent-ink: #0d1311;
  --gold: #c9a35a;
  --paper: #1d201e;
  --page-filter: invert(0.87) sepia(0.22) brightness(0.92) contrast(1.05);
  --mark-blend: screen;
  --mark-opacity: 0.42;
  --edge-a: #2b302d;
  --edge-b: #171a18;
  --book-shadow: 0 28px 50px -20px rgba(0, 0, 0, 0.8), 0 10px 20px -10px rgba(0, 0, 0, 0.6);
  --card-shadow: 0 18px 40px -16px rgba(0, 0, 0, 0.7), 0 2px 6px rgba(0, 0, 0, 0.3);
  color-scheme: dark;
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--desk);
  color: var(--text);
  font: 14px/1.4 Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
kbd { font: 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace; padding: 3px 6px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border); }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
[hidden] { display: none !important; }

#app {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 70% 60% at 50% 45%, var(--desk-glow), transparent 75%),
    var(--desk);
  transition: background-color .3s;
}

/* ---------- top bar ---------- */
#topbar {
  position: absolute; z-index: 30; top: 0; left: 0; right: 0;
  height: calc(var(--tb) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
  transition: opacity .25s, transform .25s;
}
.tb-group { display: flex; gap: 2px; align-items: center; }
.tb-group.right { justify-content: flex-end; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  display: inline-grid; place-items: center;
  color: var(--text); opacity: .82;
  transition: background .15s, opacity .15s, color .15s;
}
.icon-btn:hover { background: var(--accent-soft); opacity: 1; }
.icon-btn.on { background: var(--accent); color: var(--accent-ink); opacity: 1; }
.icon-btn.sm { width: 30px; height: 30px; border-radius: 9px; }
.icon-btn.sm svg { width: 18px; height: 18px; }
.icon-btn.has-dot { position: relative; }
.icon-btn.has-dot::after { content: ""; position: absolute; top: 8px; right: 8px; width: 7px; height: 7px; border-radius: 50%; background: var(--gold); box-shadow: 0 0 0 2px var(--desk); }
.icon-btn.filled svg { fill: var(--gold); stroke: var(--gold); }

.title-block {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 4px 16px; border-radius: 14px; min-width: 0;
}
.title-block:hover { background: var(--accent-soft); }
.t-ar { font: 700 22px/1.15 Amiri, "Geeza Pro", "Noto Naskh Arabic", serif; direction: rtl; white-space: nowrap; }
.t-meta { font-size: 12px; color: var(--muted); white-space: nowrap; letter-spacing: .01em; }
.t-meta b { color: var(--text); font-weight: 600; }

/* ---------- stage & book ---------- */
#stage {
  position: absolute; left: 0; right: 0;
  top: calc(var(--tb) + env(safe-area-inset-top));
  bottom: calc(var(--bb) + env(safe-area-inset-bottom));
  display: grid; place-items: center;
  overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none;
}
#stage.zoomed { cursor: grab; }
.book-wrap { transform-origin: center center; will-change: transform; }
.book {
  position: relative; display: flex;
  width: calc(var(--pw) * 2); height: var(--ph);
  perspective: calc(var(--pw) * 4.2);
}
.slot.off { visibility: hidden; } /* one-page view: the other page of the spread waits just off-screen */
.slot {
  position: relative; width: var(--pw); height: var(--ph);
  box-shadow: var(--stack, none), var(--book-shadow);
  background: var(--paper);
}
.slot-left { border-radius: 4px 0 0 4px; }
.slot-right { border-radius: 0 4px 4px 0; }
.slot.empty { visibility: hidden; }
.slot-shade { position: absolute; inset: 0; z-index: 4; pointer-events: none; opacity: 0; }
.slot-left .slot-shade { background: linear-gradient(to left, rgba(0,0,0,.5), rgba(0,0,0,.12) 45%, transparent 85%); }
.slot-right .slot-shade { background: linear-gradient(to right, rgba(0,0,0,.5), rgba(0,0,0,.12) 45%, transparent 85%); }

.page { position: absolute; inset: 0; background: var(--paper); }
.page-img {
  position: absolute; inset: 0; width: 100%; height: 100%;
  display: block; object-fit: fill; filter: var(--page-filter);
  pointer-events: none; user-select: none; -webkit-user-drag: none;
  opacity: 0; transition: opacity .25s;
}
.page-img.loaded { opacity: 1; }
.leaf .page-img { transition: none; } /* a running transition makes Chrome hide the image on the back of a turning page */
.page::after { /* gutter shading toward the spine */
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 2;
}
.book .page[data-side="right"]::after { background: linear-gradient(to right, rgba(0,0,0,.16), rgba(0,0,0,.05) 2.5%, transparent 8%, transparent 97%, rgba(0,0,0,.05)); }
.book .page[data-side="left"]::after { background: linear-gradient(to left, rgba(0,0,0,.16), rgba(0,0,0,.05) 2.5%, transparent 8%, transparent 97%, rgba(0,0,0,.05)); }
.page.cover::after { display: none; }
.page-num-fallback {
  position: absolute; inset: 0; display: grid; place-items: center;
  color: var(--muted); font-size: 13px;
}
.page-img.loaded + .page-num-fallback { display: none; }

/* marks */
svg.marks { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; pointer-events: none; mix-blend-mode: var(--mark-blend); }
.mk-hl rect { opacity: var(--mark-opacity); }
.mk-pen path { stroke-width: 7; stroke-linecap: round; stroke-linejoin: round; opacity: .9; }
.mk.preview { opacity: .85; }
#app.marking svg.marks .mk { pointer-events: visiblePainted; cursor: pointer; }
#app.marking.tool-eraser svg.marks .mk:hover { opacity: .45; }
#app.marking #stage { cursor: crosshair; }
#app.marking.tool-eraser #stage { cursor: cell; }

/* leaf (turning page) */
.leaf {
  position: absolute; top: 0; z-index: 5;
  width: var(--pw); height: var(--ph);
  transform-style: preserve-3d;
  will-change: transform;
}
.leaf.on-left { left: 0; transform-origin: right center; }
.leaf.on-right { left: var(--pw); transform-origin: left center; }
.leaf-face {
  position: absolute; inset: 0; overflow: hidden;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--paper);
  box-shadow: 0 0 calc(18px * var(--lift, 0)) rgba(0,0,0,calc(.28 * var(--lift, 0)));
}
.leaf-back { transform: rotateY(180deg); }
.leaf-back.blank { background: linear-gradient(to left, rgba(0,0,0,.05), transparent 30%), var(--paper); }
.leaf-shade { position: absolute; inset: 0; z-index: 3; pointer-events: none; opacity: 0; }
.leaf.on-left .leaf-front .leaf-shade { background: linear-gradient(to left, rgba(0,0,0,.45), rgba(0,0,0,.08) 50%, rgba(255,255,255,.06) 80%, rgba(0,0,0,.1)); }
.leaf.on-left .leaf-back .leaf-shade { background: linear-gradient(to right, rgba(0,0,0,.45), rgba(0,0,0,.08) 50%, rgba(255,255,255,.06) 80%, rgba(0,0,0,.1)); }
.leaf.on-right .leaf-front .leaf-shade { background: linear-gradient(to right, rgba(0,0,0,.45), rgba(0,0,0,.08) 50%, rgba(255,255,255,.06) 80%, rgba(0,0,0,.1)); }
.leaf.on-right .leaf-back .leaf-shade { background: linear-gradient(to left, rgba(0,0,0,.45), rgba(0,0,0,.08) 50%, rgba(255,255,255,.06) 80%, rgba(0,0,0,.1)); }

/* ribbons (bookmarks on the page) */
.ribbons { position: absolute; top: 0; left: 0; right: 0; height: 0; z-index: 6; }
.ribbon {
  position: absolute; top: -12px;
  width: clamp(11px, calc(var(--pw) * .028), 16px);
  height: calc(var(--ph) * .062 + 12px);
  background: linear-gradient(90deg, rgba(0,0,0,.18), transparent 30%, transparent 70%, rgba(0,0,0,.18)), var(--c);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 84%, 0 100%);
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.25));
  border-radius: 2px 2px 0 0;
}
.ribbon::after { content: ""; position: absolute; inset: 0 2px; border-left: 1px dashed rgba(255,255,255,.45); border-right: 1px dashed rgba(255,255,255,.45); }
.page[data-side="right"] .ribbon { left: calc(1.6% + var(--i) * 18px); }
.page[data-side="left"] .ribbon { right: calc(1.6% + var(--i) * 18px); }

/* note flag */
.note-flag {
  position: absolute; bottom: 0; z-index: 6;
  width: clamp(26px, calc(var(--pw) * .075), 36px); aspect-ratio: 1;
  background: linear-gradient(135deg, transparent 50%, #e9c46a 50%);
  filter: drop-shadow(-1px -1px 1px rgba(0,0,0,.15));
  display: grid; place-items: end; padding: 3px;
}
.note-flag svg { width: 11px; height: 11px; stroke: #6b4a10; stroke-width: 2.4; }
.page[data-side="right"] .note-flag { right: 0; }
.page[data-side="left"] .note-flag { left: 0; transform: scaleX(-1); }

/* hifz cover */
.hifz-cover { display: none; }
#app.hifz .hifz-cover {
  display: block; position: absolute; z-index: 5;
  left: 3.2%; right: 3.2%; bottom: 2.6%;
  top: var(--cover-top, 4.6%);
  background:
    repeating-linear-gradient(0deg, transparent 0 calc(var(--ph) * .057 - 1px), rgba(0,0,0,.035) calc(var(--ph) * .057 - 1px) calc(var(--ph) * .057)),
    var(--paper);
  border-top: 2px solid var(--accent);
  box-shadow: 0 -6px 14px -6px rgba(0,0,0,.25);
  transition: top .28s cubic-bezier(.2,.8,.2,1);
  cursor: pointer;
  border-radius: 2px;
}
#app.hifz .hifz-cover.done { top: 97.4%; border-top-color: transparent; box-shadow: none; }
.hifz-cover .grip {
  position: absolute; top: -13px; left: 50%; transform: translateX(-50%);
  height: 24px; padding: 0 12px; border-radius: 12px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 11.5px; font-weight: 600; display: flex; align-items: center; gap: 6px; white-space: nowrap;
  box-shadow: 0 4px 10px -4px rgba(0,0,0,.4);
}
.hifz-cover .grip svg { width: 14px; height: 14px; stroke-width: 2.4; }
.hifz-cover.done .grip { display: none; }

/* covers */
.page.cover { overflow: hidden; background: #123227; }
.page.cover .cover-art { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.slot .page.cover { border-radius: inherit; }

/* nav arrows */
.nav-arrow {
  position: absolute; z-index: 15; top: 50%; transform: translateY(-50%);
  width: 44px; height: 64px; border-radius: 14px; display: grid; place-items: center;
  color: var(--text); opacity: .35; transition: opacity .2s, background .2s;
}
.nav-arrow:hover { opacity: .9; background: var(--surface); }
.nav-arrow svg { width: 26px; height: 26px; }
.nav-left { left: max(6px, env(safe-area-inset-left)); }
.nav-right { right: max(6px, env(safe-area-inset-right)); }
.nav-arrow[disabled] { opacity: .08; pointer-events: none; }
@media (pointer: coarse) { .nav-arrow { display: none; } }

/* ---------- bottom bar ---------- */
#bottombar {
  position: absolute; z-index: 30; left: 0; right: 0; bottom: 0;
  height: calc(var(--bb) + env(safe-area-inset-bottom));
  padding: 0 max(16px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(16px, env(safe-area-inset-left));
  display: flex; align-items: center; gap: 16px;
  transition: opacity .25s, transform .25s;
}
.bb-side { font-size: 12px; color: var(--muted); white-space: nowrap; min-width: 92px; display: flex; align-items: center; gap: 6px; }
.bb-side:last-child { justify-content: flex-end; }
.bb-side svg { width: 15px; height: 15px; }
.bb-side b { color: var(--text); font-weight: 600; }
.scrub-wrap { position: relative; flex: 1; height: 36px; display: flex; align-items: center; max-width: 1100px; margin: 0 auto; }
#scrub { width: 100%; margin: 0; appearance: none; -webkit-appearance: none; background: transparent; height: 36px; cursor: pointer; position: relative; z-index: 2; }
#scrub::-webkit-slider-runnable-track { height: 4px; border-radius: 4px; background: linear-gradient(to left, var(--accent) var(--fill, 0%), var(--border) var(--fill, 0%)); }
#scrub::-moz-range-track { height: 4px; border-radius: 4px; background: var(--border); }
#scrub::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--accent); }
#scrub::-webkit-slider-thumb { -webkit-appearance: none; width: 18px; height: 18px; margin-top: -7px; border-radius: 50%; background: var(--surface-solid); border: 4px solid var(--accent); box-shadow: 0 2px 6px rgba(0,0,0,.25); }
#scrub::-moz-range-thumb { width: 12px; height: 12px; border-radius: 50%; background: var(--surface-solid); border: 4px solid var(--accent); }
#scrub-ticks { position: absolute; left: 9px; right: 9px; top: 0; bottom: 0; pointer-events: none; z-index: 1; }
.tick { position: absolute; top: 50%; width: 1px; height: 8px; margin-top: 4px; background: var(--muted); opacity: .35; transform: translateX(50%); }
.tick.dot { width: 5px; height: 5px; border-radius: 50%; margin-top: -11px; background: var(--c); opacity: .95; }
.tick.bm { width: 7px; height: 9px; margin-top: -15px; background: var(--c); opacity: 1; clip-path: polygon(0 0,100% 0,100% 100%,50% 75%,0 100%); }
#scrub-tip {
  position: absolute; bottom: 44px; transform: translateX(-50%); z-index: 5;
  background: var(--surface-solid); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--card-shadow); padding: 6px; display: flex; flex-direction: column; align-items: center; gap: 4px;
  pointer-events: none; width: 116px;
}
#scrub-tip img { width: 104px; aspect-ratio: 492/754; border-radius: 4px; background: var(--paper); filter: var(--page-filter); }
.tip-txt { font-size: 11.5px; text-align: center; line-height: 1.3; }
.tip-txt .ar { font: 700 15px/1.2 Amiri, serif; display: block; }

/* ---------- floating bars ---------- */
.floating-bar {
  position: absolute; z-index: 35; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--bb) + env(safe-area-inset-bottom) + 10px);
  display: flex; align-items: center; gap: 4px; padding: 6px;
  background: var(--surface-solid); border: 1px solid var(--border); border-radius: 18px;
  box-shadow: var(--card-shadow);
  animation: rise .22s cubic-bezier(.2,.8,.2,1);
  max-width: calc(100vw - 20px); overflow-x: auto; scrollbar-width: none;
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 10px); } }
.floating-bar .sep { width: 1px; height: 26px; background: var(--border); margin: 0 4px; flex: none; }
.fb-label { font-size: 12.5px; font-weight: 600; padding: 0 8px; color: var(--muted); white-space: nowrap; }
.seg { display: flex; gap: 2px; background: var(--surface-2); border-radius: 13px; padding: 3px; flex: none; }
.seg button { height: 34px; min-width: 38px; padding: 0 10px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; font-size: 13px; font-weight: 500; color: var(--muted); white-space: nowrap; }
.seg button svg { width: 19px; height: 19px; }
.seg button.on { background: var(--surface-solid); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.14); }
.seg.small button { height: 28px; font-size: 12px; }
.cats { display: flex; gap: 2px; flex: none; }
.cat { height: 34px; padding: 0 10px 0 8px; border-radius: 10px; display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 500; color: var(--muted); white-space: nowrap; }
.cat i { width: 14px; height: 14px; border-radius: 50%; background: var(--c); box-shadow: inset 0 0 0 2px rgba(255,255,255,.35); }
.cat.on { background: color-mix(in srgb, var(--c) 16%, transparent); color: var(--text); }
.cat.on i { box-shadow: 0 0 0 2px var(--surface-solid), 0 0 0 4px var(--c); }
.pill-btn {
  height: 36px; padding: 0 16px; border-radius: 12px; flex: none;
  background: var(--accent); color: var(--accent-ink); font-weight: 600; font-size: 13px;
  display: inline-flex; align-items: center; gap: 8px; white-space: nowrap;
}
.pill-btn svg { width: 17px; height: 17px; }
.pill-btn.ghost { background: var(--surface-2); color: var(--text); font-weight: 500; }
.pill-btn.danger { background: transparent; color: var(--danger); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--danger) 40%, transparent); font-weight: 500; }
.pill-btn:disabled { opacity: .5; }

/* ---------- bookmarks dock ---------- */
.dock { position: absolute; z-index: 25; right: max(12px, env(safe-area-inset-right)); top: calc(var(--tb) + env(safe-area-inset-top) + 12px); }
.dock-panel {
  width: 272px; max-height: min(60vh, 520px); display: flex; flex-direction: column;
  background: var(--surface); backdrop-filter: blur(18px) saturate(1.3); -webkit-backdrop-filter: blur(18px) saturate(1.3);
  border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--card-shadow);
  animation: pop .2s cubic-bezier(.2,.8,.2,1); transform-origin: top right;
}
@keyframes pop { from { opacity: 0; transform: scale(.94); } }
.dock.min .dock-panel { display: none; }
.dock:not(.min) .dock-tab { display: none; }
.dock-tab {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 10px 8px 8px; border-radius: 14px;
  background: var(--surface); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--border); box-shadow: var(--card-shadow);
  font-size: 12px; font-weight: 700; color: var(--gold);
}
.dock-tab svg { width: 20px; height: 20px; fill: var(--gold); stroke: var(--gold); }
.dock-head { display: flex; align-items: center; gap: 2px; padding: 10px 8px 6px 16px; }
.dock-title { flex: 1; font-weight: 600; font-size: 13.5px; }
.dock-list { list-style: none; margin: 0; padding: 0 6px 8px; overflow-y: auto; }
.dock-empty { padding: 4px 12px 12px; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
.bm-item { display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 8px; border-radius: 12px; cursor: pointer; position: relative; }
.bm-item:hover { background: var(--accent-soft); }
.bm-item.here { background: var(--accent-soft); }
.bm-rib { width: 12px; height: 30px; flex: none; background: var(--c); clip-path: polygon(0 0,100% 0,100% 100%,50% 80%,0 100%); border: 0; cursor: pointer; }
.bm-text { flex: 1; min-width: 0; }
.bm-label { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; outline: none; border-radius: 4px; }
.bm-label[contenteditable="true"] { background: var(--surface-solid); box-shadow: 0 0 0 2px var(--accent); padding: 0 4px; }
.bm-sub { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bm-actions { display: flex; opacity: 0; transition: opacity .15s; }
.bm-item:hover .bm-actions, .bm-item:focus-within .bm-actions { opacity: 1; }
@media (pointer: coarse) { .bm-actions { opacity: 1; } }
.bm-actions .icon-btn { width: 28px; height: 28px; border-radius: 8px; }
.bm-actions svg { width: 16px; height: 16px; }

/* ---------- drawer ---------- */
#drawer {
  position: absolute; z-index: 50; top: 0; bottom: 0; left: 0;
  width: min(400px, 94vw);
  padding-top: env(safe-area-inset-top); padding-left: env(safe-area-inset-left);
  background: var(--surface-solid); box-shadow: var(--card-shadow);
  display: flex; flex-direction: column;
  transform: translateX(-105%); transition: transform .3s cubic-bezier(.2,.8,.2,1);
}
#app.drawer-open #drawer { transform: none; }
#scrim { position: absolute; inset: 0; z-index: 45; background: rgba(10, 14, 12, .32); opacity: 0; pointer-events: none; transition: opacity .3s; }
#app.drawer-open #scrim { opacity: 1; pointer-events: auto; }
.drawer-head { display: flex; align-items: center; gap: 6px; padding: 12px 10px 8px 14px; }
.tabs { flex: 1; display: flex; gap: 2px; background: var(--surface-2); border-radius: 13px; padding: 3px; overflow-x: auto; scrollbar-width: none; }
.tab { flex: 1; height: 32px; padding: 0 8px; border-radius: 10px; font-size: 12.5px; font-weight: 500; color: var(--muted); white-space: nowrap; }
.tab.on { background: var(--surface-solid); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.drawer-search { margin: 4px 14px 8px; display: flex; align-items: center; gap: 8px; padding: 0 12px; height: 40px; border-radius: 12px; background: var(--surface-2); }
.drawer-search svg { width: 18px; height: 18px; color: var(--muted); }
.drawer-search input { flex: 1; border: 0; outline: 0; background: transparent; height: 100%; }
.drawer-body { flex: 1; overflow-y: auto; padding: 0 8px calc(16px + env(safe-area-inset-bottom)); overscroll-behavior: contain; }
.row { display: flex; align-items: center; gap: 12px; padding: 9px 10px; border-radius: 12px; cursor: pointer; width: 100%; text-align: left; }
.row:hover { background: var(--accent-soft); }
.row.current { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.num { position: relative; width: 36px; height: 36px; flex: none; display: grid; place-items: center; font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.num::before, .num::after { content: ""; position: absolute; inset: 7px; border: 1.5px solid var(--gold); border-radius: 3px; opacity: .8; }
.num::after { transform: rotate(45deg); }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-ar { font: 700 19px/1 Amiri, serif; direction: rtl; color: var(--text); white-space: nowrap; }
.row-page { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 32px; text-align: right; }
.section-title { font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); padding: 14px 10px 6px; display: flex; align-items: center; gap: 8px; }
.section-title .grow { flex: 1; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 2px 10px 8px; }
.chip { height: 30px; padding: 0 12px; border-radius: 15px; font-size: 12.5px; background: var(--surface-2); display: inline-flex; align-items: center; gap: 6px; color: var(--text); }
.chip.on { background: var(--accent); color: var(--accent-ink); }
.chip i { width: 9px; height: 9px; border-radius: 50%; background: var(--c); }
.thumb { width: 38px; aspect-ratio: 492/754; border-radius: 3px; flex: none; background: var(--paper); box-shadow: 0 1px 3px rgba(0,0,0,.2); filter: var(--page-filter); object-fit: cover; }
.counts { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 3px; }
.count { font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 9px; background: color-mix(in srgb, var(--c) 18%, transparent); color: color-mix(in srgb, var(--c) 75%, var(--text)); }
.empty { padding: 28px 18px; text-align: center; color: var(--muted); font-size: 13px; line-height: 1.55; }
.empty svg { width: 34px; height: 34px; opacity: .5; display: block; margin: 0 auto 10px; }
.note-preview { font-size: 12.5px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; white-space: normal; }
.manzil { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; padding: 2px 10px 6px; }
.manzil button { height: 40px; border-radius: 11px; background: var(--surface-2); font-weight: 600; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1; }
.manzil button small { font-size: 10px; color: var(--muted); font-weight: 500; }
.manzil button:hover { background: var(--accent-soft); }

/* progress */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 6px 10px; }
.stat { background: var(--surface-2); border-radius: 14px; padding: 12px; }
.stat b { display: block; font-size: 22px; font-weight: 700; letter-spacing: -.02em; }
.stat span { font-size: 11.5px; color: var(--muted); }
.khatm { display: flex; align-items: center; gap: 16px; padding: 10px 12px; }
.ring { width: 84px; height: 84px; flex: none; }
.ring circle { fill: none; stroke-width: 8; }
.ring .bg { stroke: var(--surface-2); }
.ring .fg { stroke: var(--accent); stroke-linecap: round; transform: rotate(-90deg); transform-origin: center; transition: stroke-dashoffset .6s; }
.ring text { fill: var(--text); stroke: none; font: 700 17px Inter, sans-serif; }
.bars { display: flex; align-items: flex-end; gap: 4px; height: 90px; padding: 6px 12px 0; }
.bar { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; }
.bar i { width: 100%; border-radius: 4px 4px 2px 2px; background: var(--accent); min-height: 2px; opacity: .85; }
.bar i.zero { background: var(--border); }
.bar span { font-size: 10px; color: var(--muted); }
.juzgrid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; padding: 6px 10px; }
.juzgrid button { aspect-ratio: 1; border-radius: 8px; font-size: 11px; font-weight: 600; background: color-mix(in srgb, var(--accent) calc(var(--f) * 100%), var(--surface-2)); color: var(--text); }
.juzgrid button.full { color: var(--accent-ink); }

/* ---------- dialogs ---------- */
dialog.sheet {
  border: 0; padding: 0; border-radius: 22px; width: min(540px, calc(100vw - 24px)); max-height: calc(100dvh - 40px);
  background: var(--surface-solid); color: var(--text); box-shadow: var(--card-shadow);
}
dialog.sheet[open] { animation: pop .2s cubic-bezier(.2,.8,.2,1); display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(10, 14, 12, .35); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
.sheet-head { display: flex; align-items: center; gap: 10px; padding: 16px 12px 10px 20px; }
.sheet-head h3 { margin: 0; font-size: 17px; flex: 1; }
.sheet-foot { display: flex; align-items: center; gap: 8px; padding: 12px 16px 16px 20px; }
.sheet-foot .muted { flex: 1; font-size: 12px; }
#note-text { margin: 0 16px; padding: 14px; border-radius: 14px; border: 1px solid var(--border); background: var(--surface-2); resize: vertical; outline: none; line-height: 1.55; min-height: 140px; }
#note-text:focus { box-shadow: 0 0 0 2px var(--accent); }
.set-body { padding: 4px 20px 20px; overflow-y: auto; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; flex-wrap: wrap; }
.set-row > label { font-weight: 600; font-size: 13.5px; }
.sw { width: 14px; height: 14px; border-radius: 50%; display: inline-block; border: 1px solid var(--border); }
.sw-light { background: #fffcf3; } .sw-sepia { background: #f0dfbb; } .sw-night { background: #1d201e; }
.set-toggle { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 10px 0; border-top: 1px solid var(--border); cursor: pointer; }
.set-toggle span { display: flex; flex-direction: column; }
.set-toggle b { font-size: 13.5px; font-weight: 600; }
.set-toggle small { color: var(--muted); font-size: 12px; }
.set-toggle input { appearance: none; -webkit-appearance: none; width: 44px; height: 26px; border-radius: 13px; background: var(--border); position: relative; flex: none; cursor: pointer; transition: background .2s; }
.set-toggle input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s; }
.set-toggle input:checked { background: var(--accent); }
.set-toggle input:checked::after { transform: translateX(18px); }
.set-actions { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 0 4px; }
.set-group-title { font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); }
.shortcuts { display: flex; flex-wrap: wrap; gap: 8px 16px; font-size: 12.5px; color: var(--muted); padding-top: 8px; }
.shortcuts kbd { margin-right: 3px; }

.palette { width: min(560px, calc(100vw - 24px)); margin-top: 12vh; }
.pal-input { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.pal-input svg { color: var(--muted); }
.pal-input input { flex: 1; border: 0; outline: 0; background: transparent; font-size: 17px; }
#goto-results { list-style: none; margin: 0; padding: 6px; max-height: 50vh; overflow-y: auto; }
#goto-results li { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 12px; cursor: pointer; }
#goto-results li.sel { background: var(--accent-soft); }
#goto-results .kind { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); width: 46px; flex: none; }
#goto-results .main { flex: 1; min-width: 0; }
#goto-results .ar { font: 700 18px/1 Amiri, serif; direction: rtl; }

/* popover */
.popover {
  position: absolute; z-index: 60; padding: 6px; border-radius: 16px;
  background: var(--surface-solid); border: 1px solid var(--border); box-shadow: var(--card-shadow);
  display: flex; align-items: center; gap: 2px; animation: pop .15s ease-out;
}
.popover .cat { height: 32px; }
.popover .sep { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }

/* zoom & toast */
#zoom-pill {
  position: absolute; z-index: 32; left: 50%; transform: translateX(-50%);
  top: calc(var(--tb) + env(safe-area-inset-top) + 8px);
  display: flex; align-items: center; gap: 8px; padding: 4px 4px 4px 12px; border-radius: 14px;
  background: var(--surface-solid); box-shadow: var(--card-shadow); font-size: 12.5px; font-weight: 600;
}
#zoom-pill button { height: 28px; padding: 0 10px; border-radius: 10px; background: var(--surface-2); font-size: 12px; }
#toast {
  position: absolute; z-index: 70; left: 50%; bottom: calc(var(--bb) + env(safe-area-inset-bottom) + 70px);
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  padding: 10px 10px 10px 16px; border-radius: 14px; background: #1d2722; color: #f2efe6;
  font-size: 13px; box-shadow: var(--card-shadow); display: flex; align-items: center; gap: 12px;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 24px);
}
[data-theme="night"] #toast { background: #e9e6dc; color: #1a1f1c; }
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { color: var(--gold); font-weight: 700; padding: 4px 8px; border-radius: 8px; }

/* chrome hidden (tap centre of the page) */
#app.chrome-hidden #topbar { opacity: 0; transform: translateY(-8px); pointer-events: none; }
#app.chrome-hidden #bottombar { opacity: 0; transform: translateY(8px); pointer-events: none; }
#app.chrome-hidden .dock, #app.chrome-hidden .nav-arrow { opacity: 0; pointer-events: none; }

/* ---------- small screens ---------- */
@media (max-width: 760px) {
  :root { --tb: 54px; --bb: 52px; }
  #btn-goto { display: none; }
  .icon-btn { width: 36px; height: 36px; border-radius: 11px; }
  .icon-btn svg { width: 20px; height: 20px; }
  .title-block { padding: 2px 6px; }
  .t-ar { font-size: 19px; }
  .t-meta { font-size: 11px; }
  .bb-side { min-width: 0; }
  .bb-side .lbl { display: none; }
  .dock-panel { width: min(272px, calc(100vw - 24px)); }
  .dock.min { right: 0; }
  .dock-tab { padding: 8px 4px 6px 6px; border-radius: 10px 0 0 10px; border-right: 0; font-size: 10.5px; }
  .dock-tab svg { width: 15px; height: 15px; }
  .cat span { display: none; }
  .cat { padding: 0 9px; }
}
@media (max-width: 420px) {
  #topbar { grid-template-columns: auto 1fr auto; gap: 2px; }
  .tb-group.right .icon-btn { width: 34px; }
  .t-meta .opt { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* offline prompt */
.offline-card {
  position: absolute; z-index: 40; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--bb) + env(safe-area-inset-bottom) + 12px);
  width: min(440px, calc(100vw - 24px));
  display: flex; gap: 14px; padding: 16px;
  background: var(--surface-solid); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--card-shadow);
  animation: rise .25s cubic-bezier(.2,.8,.2,1);
}
.oc-icon { width: 40px; height: 40px; flex: none; border-radius: 12px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.oc-body { flex: 1; min-width: 0; }
.oc-body b { font-size: 14.5px; }
.oc-body p { margin: 4px 0 10px; font-size: 13px; color: var(--muted); line-height: 1.5; }
.oc-actions { display: flex; gap: 8px; }
.oc-bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin: 2px 0 12px; }
.oc-bar i { display: block; height: 100%; width: var(--p, 0%); background: var(--accent); transition: width .3s; }
.share-ico { display: inline-block; width: 15px; height: 15px; vertical-align: -2px; }
