@font-face {
  font-family: "IndoPak";
  src: url("fonts/indopak.woff2") format("woff2");
  font-display: block;
}
@font-face {
  font-family: "SurahNames";
  src: url("fonts/surah-names.woff") format("woff");
  font-display: block;
}

:root {
  --paper: #FBF6EA;
  --ink: #1C1A17;
  --rule: #8A6A3B;
  --header: #F1E6CC;
  --muted: #7A6E5D;
  --bar: rgba(251, 246, 234, 0.92);
  --line: rgba(28, 26, 23, 0.1);
  --accent: #735124;
  --ribbon: #9E2924;
  --ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
}
body[data-theme="white"] {
  --paper: #FFFFFF; --ink: #111111; --rule: #6B6B6B; --header: #F0F0F0; --muted: #777777;
  --bar: rgba(255, 255, 255, 0.92); --accent: #444444;
}
body[data-theme="night"] {
  --paper: #14130F; --ink: #E9E2D2; --rule: #8C7550; --header: #221F18; --muted: #8E8676;
  --bar: rgba(30, 28, 23, 0.92); --line: rgba(233, 226, 210, 0.12); --accent: #C9A86A;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* The bars set their own display, so make sure "hidden" really hides them. */
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--paper); color: var(--ink);
  font-family: var(--ui);
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }

/* ---------- Book: one horizontal snap scroller, right to left ---------- */
#book {
  position: fixed; inset: 0;
  display: flex; direction: rtl;
  overflow-x: auto; overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
#book::-webkit-scrollbar { display: none; }
.slot {
  flex: 0 0 100vw; width: 100vw; height: 100%;
  scroll-snap-align: start; scroll-snap-stop: always;
  display: flex; direction: rtl; justify-content: center; align-items: stretch;
  padding: max(env(safe-area-inset-top), 10px) max(env(safe-area-inset-right), 6px)
           max(env(safe-area-inset-bottom), 8px) max(env(safe-area-inset-left), 6px);
}
.slot.spread .page-wrap { width: 50%; }
/* With the bars showing, the book already sits between them: drop the safe-area padding. */
body.chrome-on .slot { padding-top: 0; padding-bottom: 0; }

.page-wrap {
  position: relative; direction: ltr;
  width: 100%; height: 100%;
  display: flex; flex-direction: column; align-items: center;
}
.head, .foot {
  flex: 0 0 auto; width: var(--pw, 100%);
  display: flex; justify-content: space-between; align-items: center;
  font-size: clamp(11px, calc(var(--pw-num, 400) * 0.032px), 15px);
  color: var(--muted); font-weight: 500; letter-spacing: 0.01em;
  padding: 4px 3%; min-height: 26px;
}
.foot { justify-content: center; font-variant-numeric: tabular-nums; }

/* The page block: thin double rule, 13 equal rows */
.page {
  position: relative; flex: 1 1 auto;
  width: var(--pw, 100%);
  border: 1.5px solid var(--rule);
  padding: 0.8%;
}
.page::before {
  content: ""; position: absolute; inset: 0.8%;
  border: 0.75px solid var(--rule); pointer-events: none;
}
.lines {
  position: absolute; inset: 1.4% 4.2% 1.2%;
  display: grid; grid-template-rows: repeat(13, 1fr);
  font-family: "IndoPak", serif; font-size: var(--fs, 24px);
  line-height: 1; color: var(--ink);
  direction: rtl;
}
.page.opening .lines { align-content: center; grid-template-rows: repeat(var(--used, 13), calc(100% / 13)); }

.line { position: relative; display: flex; align-items: center; justify-content: flex-start; min-width: 0; overflow: visible; }
.line .in {
  display: flex; direction: rtl; white-space: nowrap;
  gap: 0.28em; transform-origin: right center;
}
.line.justify .in { width: 100%; justify-content: space-between; }
.line.center { justify-content: center; }
.line.center .in { transform-origin: center center; }
.w { display: inline-block; }

.surah-band {
  position: absolute; inset: 10% -2.5%;
  border: 1.5px solid var(--rule); border-radius: 999px;
  background: var(--header);
  display: flex; align-items: center; justify-content: center;
}
.surah-band::before {
  content: ""; position: absolute; inset: 9% 1.5%;
  border: 0.75px solid var(--rule); border-radius: 999px;
}
.surah-band span {
  font-family: "SurahNames"; font-size: calc(var(--fs) * 1.35);
  line-height: 1; direction: ltr; position: relative; white-space: nowrap;
}
.bismillah { font-size: calc(var(--fs) * 1.05); }

.ribbon {
  position: absolute; top: -6px; right: 12%;
  width: 3.5%; min-width: 12px; aspect-ratio: 1 / 2.2;
  background: var(--ribbon); opacity: 0.9;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 78%, 0 100%);
}

/* ---------- Chrome ---------- */
.chrome {
  position: fixed; left: 12px; right: 12px; z-index: 5;
  background: var(--bar);
  -webkit-backdrop-filter: blur(20px) saturate(1.4); backdrop-filter: blur(20px) saturate(1.4);
  border: 1px solid var(--line); border-radius: 18px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.08);
  animation: fade 0.18s ease-out;
}
@keyframes fade { from { opacity: 0; } }
#topbar {
  top: calc(env(safe-area-inset-top) + 8px);
  display: flex; align-items: center; gap: 14px; padding: 10px 16px;
}
#topbar .title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar strong { font-size: 17px; font-weight: 600; }
#topbar span { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.icon { width: 34px; height: 34px; display: grid; place-items: center; color: var(--accent); border-radius: 10px; }
.icon svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon.on svg { fill: currentColor; }
.icon:active { background: var(--line); }

#bottombar {
  bottom: calc(env(safe-area-inset-bottom) + 8px);
  padding: 14px 18px 10px; display: flex; flex-direction: column; align-items: center; gap: 6px;
}
#scrub { width: 100%; accent-color: var(--accent); }
#scrub-label { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

#loading {
  position: fixed; inset: 0; display: grid; place-items: center;
  color: var(--muted); font-size: 15px;
}

/* ---------- Sheets ---------- */
dialog {
  border: 0; padding: 0; margin: auto auto 0; max-width: 640px; width: 100%;
  max-height: 88dvh; height: 88dvh;
  background: transparent; color: var(--ink);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.28); }
dialog[open] { animation: up 0.22s cubic-bezier(.2, .8, .2, 1); }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@media (min-width: 700px) { dialog { margin: auto; border-radius: 20px; height: 80dvh; } }
.sheet {
  height: 100%; display: flex; flex-direction: column;
  background: var(--paper); border-radius: 20px 20px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  -webkit-user-select: none; user-select: none;
}
@media (min-width: 700px) { .sheet { border-radius: 20px; } }
.sheet.small { height: auto; }
#settings { height: auto; }
.sheet-head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 14px 16px 10px; gap: 8px;
}
.sheet-head > :first-child { justify-self: start; }
.sheet-head > :last-child { justify-self: end; }
.sheet-head strong { font-size: 17px; }
.text { color: var(--accent); font-size: 16px; font-weight: 500; padding: 6px 2px; }
.goto { display: flex; gap: 8px; align-items: center; }
.goto input {
  width: 64px; font: inherit; font-size: 15px; padding: 6px 8px; border-radius: 9px;
  border: 1px solid var(--line); background: transparent; color: inherit; text-align: center;
}
.tabs { display: flex; margin: 0 16px; padding: 3px; border-radius: 10px; background: var(--line); }
.tabs button { flex: 1; padding: 7px 0; border-radius: 8px; font-size: 14px; font-weight: 500; }
.tabs button[aria-selected="true"] { background: var(--paper); box-shadow: 0 1px 3px rgba(0,0,0,0.12); }
#search {
  margin: 12px 16px 6px; font: inherit; font-size: 16px; padding: 9px 12px;
  border-radius: 10px; border: 1px solid var(--line); background: transparent; color: inherit;
  -webkit-user-select: text; user-select: text;
}
.list { list-style: none; margin: 0; padding: 0 0 20px; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.list li { border-bottom: 1px solid var(--line); }
.list button {
  width: 100%; display: grid; grid-template-columns: 34px 1fr auto; gap: 12px; align-items: center;
  padding: 12px 18px 12px 10px; text-align: left;
}
.list button.current { background: color-mix(in srgb, var(--accent) 9%, transparent); }
.list .n { color: var(--muted); font-size: 14px; text-align: right; font-variant-numeric: tabular-nums; }
.list .name { font-size: 16.5px; }
.list .current .name { font-weight: 600; }
.list .sub { display: block; font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.list .right { text-align: right; }
.list .ar { font-family: "IndoPak", serif; font-size: 21px; display: block; direction: rtl; }
.list .pg { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.list .empty { padding: 40px 24px; text-align: center; color: var(--muted); border: 0; }
.list .del { color: var(--ribbon); font-size: 13px; padding: 6px 4px; }
.list li.bm { display: flex; align-items: center; padding-right: 14px; }
.list li.bm > button:first-child { flex: 1; }

.seg { border: 0; margin: 4px 16px 18px; padding: 0; }
.seg legend { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.seg { display: flex; }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; }
.seg span {
  display: block; text-align: center; padding: 9px 0; font-size: 15px; font-weight: 500;
  border: 1px solid var(--line); margin-left: -1px;
}
.seg label:first-of-type span { border-radius: 10px 0 0 10px; }
.seg label:last-of-type span { border-radius: 0 10px 10px 0; }
.seg input:checked + span { background: var(--accent); color: var(--paper); border-color: var(--accent); }
.toggle {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  margin: 0 16px; padding: 12px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-size: 16px;
}
.toggle small { display: block; font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.toggle input { width: 22px; height: 22px; accent-color: var(--accent); }
.credit { font-size: 12px; color: var(--muted); margin: 16px 16px 20px; line-height: 1.5; }
