/* ── Fonts ─────────────────────────────────────────────── */
@font-face { font-family: Vazir; font-display: swap; src: url(vazirmatn.woff2) format("woff2"); }
@font-face { font-family: Naskh; font-display: swap; src: url(naskh-arabic.woff2) format("woff2"); }
@font-face { font-family: ScheherazadeNew; font-display: swap; src: url(scheherazade-new.woff2) format("woff2"); }
@font-face { font-family: Lateef; font-display: swap; src: url(lateef.woff2) format("woff2"); }

/* ── Tokens ────────────────────────────────────────────── */
:root {
  --ar-font: Naskh, serif;
  --scale: 1;
  --bg: #eef0ee;
  --paper: #fffefa;
  --surface: #ffffff;
  --surface-2: #f3f4f1;
  --field: #fbfcfa;
  --text: #17222d;
  --muted: #526170;
  --line: #e1e5e2;
  --line-strong: #c7cec9;
  --primary: #1d4e7a;
  --primary-ink: #ffffff;
  --primary-soft: #e3ecf5;
  --gold: #7a5a1e;
  --gold-soft: #f5ecd8;
  --gold-line: #c9aa6c;
  --mark: #fbe7a8;
  --appbar: #16324f;
  --appbar-ink: #f7f2e4;
  --appbar-muted: #c4d0dc;
  --hero-a: #16324f;
  --hero-b: #2a5a7f;
  --scrim: rgb(8 16 24 / 0.45);
  --toast: #1d2731;
  --toast-ink: #f4f1ea;
  --shadow-1: 0 1px 2px rgb(20 30 40 / 0.06), 0 6px 20px rgb(20 30 40 / 0.06);
  --shadow-2: 0 18px 50px rgb(10 20 30 / 0.24), 0 2px 8px rgb(10 20 30 / 0.08);
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --appbar-h: 3.6rem;
  --tabbar-h: 4rem;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
  font-family: Vazir, Tahoma, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}
:root[data-theme="sepia"] {
  --bg: #e9dcc0; --paper: #f8eed6; --surface: #fbf3df; --surface-2: #f0e3c5; --field: #fcf5e4;
  --text: #392a16; --muted: #6a5536; --line: #e0cfa9; --line-strong: #c9b285;
  --primary: #6b4a1c; --primary-soft: #eedcb5; --gold: #7b551b; --gold-soft: #f0deb4; --mark: #ecd38c;
  --appbar: #5b4426; --appbar-ink: #fbf1da; --appbar-muted: #e3d1ad; --hero-a: #5b4426; --hero-b: #876637;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1118; --paper: #111a23; --surface: #15202b; --surface-2: #1b2835; --field: #101820;
  --text: #e6e2d8; --muted: #a4b2bf; --line: #24323f; --line-strong: #3a4b5b;
  --primary: #8fbde6; --primary-ink: #0b1118; --primary-soft: #1d3247;
  --gold: #e0c07e; --gold-soft: #2c2616; --gold-line: #8d7440; --mark: #5a4a1c;
  --appbar: #0e1822; --appbar-ink: #ece6d6; --appbar-muted: #9fb0bf; --hero-a: #13263a; --hero-b: #1f3d5a;
  --scrim: rgb(0 0 0 / 0.6); --toast: #e6e2d8; --toast-ink: #111a23;
  --shadow-1: 0 1px 2px rgb(0 0 0 / 0.3); --shadow-2: 0 18px 50px rgb(0 0 0 / 0.5);
}
:root[data-font="scheherazade"], [data-font="scheherazade"] { --ar-font: ScheherazadeNew, Naskh, serif; }
:root[data-font="lateef"], [data-font="lateef"] { --ar-font: Lateef, Naskh, serif; }
[data-font="naskh"] { --ar-font: Naskh, serif; }

/* ── Base ──────────────────────────────────────────────── */
* { box-sizing: border-box; }
html { scroll-padding-top: calc(var(--appbar-h) + 1rem); }
body { margin: 0; min-height: 100vh; background: var(--bg); line-height: 1.8; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--primary); }
.icon { width: 1.35rem; height: 1.35rem; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }
:focus-visible { outline: 2.5px solid var(--primary); outline-offset: 2px; border-radius: 6px; }
[lang="ar"] { font-family: var(--ar-font); }

/* ── App bar & tabs ────────────────────────────────────── */
.appbar {
  position: sticky; top: 0; z-index: 20; height: var(--appbar-h);
  display: flex; align-items: center; gap: 0.5rem; padding: 0 0.75rem;
  background: var(--appbar); color: var(--appbar-ink); box-shadow: 0 2px 12px rgb(0 0 0 / 0.15);
}
.brand { display: flex; align-items: center; gap: 0.4rem; color: inherit; text-decoration: none; font-weight: 700; white-space: nowrap; }
.brand .icon { color: var(--gold-line); width: 1.6rem; height: 1.6rem; }
.appbar-title { flex: 1; margin: 0; font-size: 1rem; min-width: 0; text-align: center; }
.appbar-title button { background: none; border: 0; padding: 0.2rem 0.6rem; border-radius: 10px; display: inline-flex; flex-direction: column; align-items: center; max-width: 100%; line-height: 1.35; }
.appbar-title button:hover { background: rgb(255 255 255 / 0.08); }
#title { font-weight: 700; }
#subtitle { color: var(--appbar-muted); font-size: 0.75rem; max-width: 60vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#subtitle:empty { display: none; }
.appbar-end { display: flex; align-items: center; gap: 0.25rem; }
.icon-button { background: none; border: 0; width: 2.6rem; height: 2.6rem; display: grid; place-items: center; border-radius: 50%; color: inherit; }
.icon-button:hover { background: color-mix(in srgb, currentColor 10%, transparent); }
.tabbar { display: flex; gap: 0.2rem; }
.tab { background: none; border: 0; color: var(--appbar-muted); display: flex; align-items: center; gap: 0.35rem; padding: 0.4rem 0.7rem; border-radius: 999px; font-size: 0.9rem; }
.tab:hover, .tab.active { color: var(--appbar-ink); background: rgb(255 255 255 / 0.1); }

@media (max-width: 759px) {
  .brand span { display: none; }
  .tabbar {
    position: fixed; inset: auto 0 0 0; z-index: 30; height: calc(var(--tabbar-h) + var(--safe-b));
    padding: 0.3rem 0.5rem var(--safe-b); background: var(--surface); border-top: 1px solid var(--line);
    justify-content: space-around; box-shadow: 0 -4px 18px rgb(0 0 0 / 0.06);
  }
  .tab { flex-direction: column; gap: 0.1rem; font-size: 0.72rem; color: var(--muted); border-radius: 14px; padding: 0.35rem 0.6rem; flex: 1; }
  .tab:hover, .tab.active { color: var(--primary); background: var(--primary-soft); }
  body { padding-bottom: calc(var(--tabbar-h) + var(--safe-b)); }
}

/* ── Main ──────────────────────────────────────────────── */
main { max-width: 52rem; margin: 0 auto; padding: 1rem 1rem 3rem; outline: none; }
.loading { text-align: center; color: var(--muted); padding: 3rem 0; }
.card { display: block; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 1.1rem 1.25rem; box-shadow: var(--shadow-1); margin: 1rem 0; color: inherit; text-decoration: none; }

.hero {
  border-radius: 22px; padding: 2rem 1.25rem 1.4rem; text-align: center; color: var(--appbar-ink);
  background: radial-gradient(circle at 20% 0%, rgb(255 255 255 / 0.12), transparent 50%), linear-gradient(140deg, var(--hero-a), var(--hero-b));
  box-shadow: var(--shadow-1);
}
.hero-ar { font-size: 2.6rem; margin: 0; line-height: 1.6; color: #f3dfaa; }
.hero-sub { margin: 0.2rem 0 1.2rem; color: var(--appbar-muted); }
.hero-stats { display: flex; gap: 0.6rem; justify-content: center; flex-wrap: wrap; }
.hero-stats button { background: rgb(255 255 255 / 0.1); border: 1px solid rgb(255 255 255 / 0.18); border-radius: 14px; padding: 0.5rem 1.1rem; color: inherit; display: flex; flex-direction: column; align-items: center; min-width: 5.5rem; line-height: 1.5; }
.hero-stats button:hover { background: rgb(255 255 255 / 0.18); }
.hero-stats b { font-size: 1.35rem; }
.resume { display: flex; justify-content: space-between; align-items: center; gap: 1rem; border-color: var(--gold-line); background: var(--gold-soft); }
.resume small { color: var(--muted); display: block; }
.daily h2 { margin: 0 0 0.4rem; font-size: 0.95rem; color: var(--gold); }
.daily-ar { font-size: calc(1.5rem * var(--scale)); line-height: 2; margin: 0.2rem 0; }
.daily-fa { color: var(--muted); margin: 0.3rem 0 0.6rem; }
.daily-link { font-size: 0.9rem; }
.home-note { color: var(--muted); font-size: 0.9rem; text-align: center; padding: 0 1rem; }

/* ── Reader ────────────────────────────────────────────── */
.item-head { text-align: center; padding: 1.2rem 0.5rem 0.6rem; }
.item-kind { margin: 0; color: var(--gold); font-weight: 700; letter-spacing: 0.02em; }
.item-head h2 { margin: 0.1rem 0 0.4rem; font-size: 1.4rem; line-height: 1.6; }
.item-intro { color: var(--muted); font-size: calc(1.15rem * var(--scale)); margin: 0 auto 0.8rem; max-width: 40rem; line-height: 1.9; }
.item-intro-fa { color: var(--muted); font-size: 0.9rem; margin: -0.4rem auto 0.8rem; max-width: 40rem; }
.item-intro:empty, .item-intro-fa:empty { display: none; }
:root[data-mode="ar"] .item-intro-fa, :root[data-mode="fa"] .item-intro { display: none; }
.item-tools { display: flex; gap: 0.5rem; justify-content: center; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: 0.35rem; border: 1px solid var(--line-strong); background: var(--surface); border-radius: 999px; padding: 0.3rem 0.85rem; font-size: 0.88rem; color: var(--text); }
.chip:hover { border-color: var(--primary); color: var(--primary); }
.chip .icon { width: 1.1rem; height: 1.1rem; }
.chip[aria-pressed="true"], .chip.on { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }

.part { background: var(--paper); border: 1px solid var(--line); border-radius: 18px; margin: 1rem 0; box-shadow: var(--shadow-1); overflow: hidden; scroll-margin-top: calc(var(--appbar-h) + 0.75rem); }
.part.target { border-color: var(--gold-line); box-shadow: 0 0 0 3px var(--gold-soft), var(--shadow-1); }
.part-label { display: flex; align-items: center; gap: 0.5rem; padding: 0.55rem 1.1rem 0; color: var(--gold); font-size: 0.82rem; font-weight: 700; }
.part-label::after { content: ""; flex: 1; border-top: 1px dashed var(--gold-line); opacity: 0.6; }
.part-ar { font-family: var(--ar-font); font-size: calc(1.6rem * var(--scale)); line-height: 2.15; padding: 0.6rem 1.25rem 0.4rem; margin: 0; text-align: justify; }
.part-ar p { margin: 0 0 0.6rem; }
.part-fa { font-size: calc(1.02rem * var(--scale)); line-height: 2.1; padding: 0.3rem 1.25rem 0.6rem; margin: 0; text-align: justify; color: var(--text); border-top: 1px solid var(--line); background: var(--surface); }
.part-fa p { margin: 0.4rem 0; }
:root[data-mode="ar"] .part-fa, :root[data-mode="fa"] .part-ar { display: none; }
:root[data-mode="fa"] .part-fa { border-top: 0; }
.part-actions { display: flex; gap: 0.15rem; padding: 0.3rem 0.6rem 0.5rem; background: var(--surface); flex-wrap: wrap; }
.part-actions button { display: inline-flex; align-items: center; gap: 0.3rem; background: none; border: 0; padding: 0.35rem 0.65rem; border-radius: 999px; color: var(--muted); font-size: 0.86rem; }
.part-actions button:hover { background: var(--primary-soft); color: var(--primary); }
.part-actions button.primary { color: var(--primary); font-weight: 700; }
.part-actions button[aria-pressed="true"] { color: var(--gold); }
.part-actions button[aria-pressed="true"] .icon { fill: currentColor; }
.part-actions .icon { width: 1.15rem; height: 1.15rem; }
.part-actions .spacer { flex: 1; }
.saying .part-ar { text-align: center; font-size: calc(1.8rem * var(--scale)); }
.saying .part-fa { text-align: center; }

.pager { display: flex; justify-content: space-between; gap: 0.75rem; margin-top: 1.5rem; }
.pager-link { flex: 1; display: flex; align-items: center; gap: 0.4rem; padding: 0.75rem 1rem; border-radius: 14px; background: var(--surface); border: 1px solid var(--line); text-decoration: none; color: var(--text); font-size: 0.9rem; min-width: 0; }
.pager-link span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pager-link#next { justify-content: flex-end; text-align: left; }
.pager-link:hover { border-color: var(--primary); color: var(--primary); }
.pager-link[aria-disabled="true"] { visibility: hidden; }

/* ── Sheets ────────────────────────────────────────────── */
.sheet {
  border: 0; padding: 0 1.1rem 1.2rem; background: var(--surface); color: var(--text);
  width: min(34rem, 100vw); max-width: 100vw; max-height: 100dvh; overflow-y: auto; overscroll-behavior: contain;
  margin: 0 0 0 auto; height: 100dvh; box-shadow: var(--shadow-2);
  animation: slide-in 0.28s var(--ease);
}
.wide-sheet { width: min(46rem, 100vw); }
.sheet::backdrop { background: var(--scrim); animation: fade 0.25s; }
.sheet.closing { animation: slide-out 0.22s ease-in forwards; }
.sheet.closing::backdrop { animation: fade 0.22s reverse forwards; }
@keyframes slide-in { from { transform: translateX(-100%); } }
@keyframes slide-out { to { transform: translateX(-100%); } }
@keyframes fade { from { opacity: 0; } }
.sheet-head { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 0.5rem; background: var(--surface); padding: 0.75rem 0 0.5rem; margin-bottom: 0.25rem; }
.sheet-head h2 { flex: 1; margin: 0; font-size: 1.1rem; }
.grip { display: none; }
@media (max-width: 759px) {
  .sheet, .wide-sheet { width: 100vw; height: auto; max-height: 88dvh; margin: auto 0 0; border-radius: 22px 22px 0 0; animation: rise 0.3s var(--ease); padding-bottom: calc(1.2rem + var(--safe-b)); }
  .sheet.closing { animation: sink 0.22s ease-in forwards; }
  .sheet.dragging { animation: none; transition: none; }
  .grip { display: block; position: absolute; top: 0.4rem; left: 50%; translate: -50% 0; width: 2.6rem; height: 0.3rem; border-radius: 99px; background: var(--line-strong); }
  .sheet-head { padding-top: 1.1rem; touch-action: none; }
  #study-sheet { height: 88dvh; }
}
@keyframes rise { from { transform: translateY(100%); } }
@keyframes sink { to { transform: translateY(100%); } }

.segmented { display: flex; background: var(--surface-2); border-radius: 12px; padding: 0.25rem; gap: 0.25rem; margin: 0.25rem 0 0.75rem; }
.segmented button { flex: 1; border: 0; background: none; border-radius: 9px; padding: 0.45rem 0.5rem; font-size: 0.88rem; color: var(--muted); line-height: 1.5; }
.segmented button[aria-selected="true"] { background: var(--surface); color: var(--primary); font-weight: 700; box-shadow: var(--shadow-1); }
.segmented button:disabled { opacity: 0.45; cursor: not-allowed; }
.field { display: flex; align-items: center; gap: 0.5rem; background: var(--field); border: 1px solid var(--line-strong); border-radius: 12px; padding: 0 0.75rem; color: var(--muted); }
.field:focus-within { border-color: var(--primary); }
.field input { flex: 1; border: 0; background: none; padding: 0.65rem 0; outline: none; color: var(--text); min-width: 0; }
.hint { color: var(--muted); font-size: 0.84rem; margin: 0.6rem 0; }
.group { font-size: 0.9rem; color: var(--gold); margin: 1rem 0 0.3rem; }
.button { border: 1px solid var(--primary); background: var(--primary); color: var(--primary-ink); border-radius: 12px; padding: 0.55rem 1rem; }
.button.wide { width: 100%; margin-top: 0.75rem; }

.directory, .results, .marks { list-style: none; margin: 0.5rem 0 0; padding: 0; }
.directory a, .results a, .marks a { display: flex; gap: 0.75rem; align-items: baseline; padding: 0.6rem 0.5rem; border-bottom: 1px solid var(--line); text-decoration: none; color: var(--text); border-radius: 8px; }
.directory a:hover, .results a:hover, .marks a:hover { background: var(--surface-2); }
.directory a[aria-current="page"] { background: var(--primary-soft); }
.num { flex: none; min-width: 2.4rem; height: 2.4rem; display: grid; place-items: center; border-radius: 50%; background: var(--gold-soft); color: var(--gold); font-weight: 700; font-size: 0.88rem; border: 1px solid var(--gold-line); align-self: center; }
.row-body { min-width: 0; flex: 1; line-height: 1.7; }
.row-body small { display: block; color: var(--muted); font-size: 0.8rem; }
.row-ar { font-family: var(--ar-font); font-size: 1.15rem; }
.results mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }
.results .snippet { display: block; font-size: 0.95rem; }
.results .snippet.ar { font-family: var(--ar-font); font-size: 1.15rem; }
.marks li { display: flex; align-items: center; }
.marks li a { flex: 1; }

.options { border: 0; padding: 0; margin: 0.9rem 0; display: flex; flex-wrap: wrap; gap: 0.4rem; }
.options legend { font-weight: 700; margin-bottom: 0.4rem; font-size: 0.92rem; padding: 0; }
.options label { display: flex; align-items: center; gap: 0.4rem; border: 1px solid var(--line-strong); border-radius: 12px; padding: 0.35rem 0.75rem; cursor: pointer; }
.options label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }
.options input { accent-color: var(--primary); }
.options [lang="ar"] { font-size: 1.15rem; }
.range { display: flex; align-items: center; gap: 0.75rem; margin: 0.9rem 0; font-weight: 700; font-size: 0.92rem; }
.range input { flex: 1; accent-color: var(--primary); }

/* ── Study (commentary) ────────────────────────────────── */
.study-meta { color: var(--muted); font-size: 0.82rem; margin: 0 0 0.5rem; }
.study-meta a { color: inherit; }
.study { font-size: calc(1rem * var(--scale)); line-height: 2.1; text-align: justify; }
.study p { margin: 0 0 0.8rem; }
.study h3 { font-size: 1.05rem; color: var(--primary); margin: 1.4rem 0 0.5rem; line-height: 1.7; }
.study h4 { font-size: 0.98rem; color: var(--gold); margin: 1.1rem 0 0.4rem; }
.study .ar { font-family: var(--ar-font); font-size: 1.3em; line-height: 2; color: var(--text); background: var(--surface-2); border-radius: 12px; padding: 0.5rem 0.9rem; }
.study .note-ref { font-size: 0.72em; vertical-align: super; color: var(--primary); text-decoration: none; padding: 0 1px; }
.study .notes { border-top: 1px solid var(--line); margin-top: 1.4rem; padding-top: 0.6rem; font-size: 0.85rem; color: var(--muted); }
.study .notes p { margin: 0 0 0.3rem; }
.study .tr { border-inline-start: 3px solid var(--gold-line); padding-inline-start: 0.9rem; }
.study details { border: 1px solid var(--line); border-radius: 12px; padding: 0.3rem 0.9rem; margin: 0 0 1rem; background: var(--surface-2); }
.study summary { cursor: pointer; font-weight: 700; color: var(--primary); padding: 0.3rem 0; }
.study .empty { color: var(--muted); text-align: center; padding: 2rem 0.5rem; }
.study .more { display: flex; gap: 0.5rem; justify-content: center; margin: 1rem 0; flex-wrap: wrap; }
.study .src-head { margin: 1.6rem 0 0.4rem; padding: 0.4rem 0.8rem; border-radius: 10px; background: var(--primary-soft); color: var(--primary); font-weight: 700; font-size: 0.95rem; }

.menu { display: flex; flex-direction: column; }
.menu button { display: flex; align-items: center; gap: 0.75rem; background: none; border: 0; padding: 0.8rem 0.5rem; border-bottom: 1px solid var(--line); text-align: start; border-radius: 8px; }
.menu button:hover { background: var(--surface-2); }

.about ul { padding-inline-start: 1.1rem; }
.about li { margin-bottom: 0.7rem; }
.about small { color: var(--muted); display: block; }

.toast {
  position: fixed; inset: auto 1rem calc(var(--tabbar-h) + 1rem + var(--safe-b)) auto; margin: 0 auto; left: 0; right: 0; width: fit-content; max-width: calc(100vw - 2rem);
  background: var(--toast); color: var(--toast-ink); border: 0; padding: 0.6rem 1.1rem; border-radius: 12px; box-shadow: var(--shadow-2);
  opacity: 0; translate: 0 0.5rem; transition: opacity 0.2s, translate 0.2s;
}
.toast.show { opacity: 1; translate: 0 0; }
.toast.error { background: #a3302f; color: #fff; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media print {
  .appbar, .tabbar, .part-actions, .pager, .item-tools { display: none !important; }
  .part { box-shadow: none; break-inside: avoid; }
}
