:root {
  --bg: #faf7f0;
  --page-bg: #fffdf6;
  --surface: #ffffff;
  --ink: #1d1d1f;
  --muted: #6b7280;
  --hint: #9ca3af;
  --line: #d6cfbe;
  --line-soft: #e8e1cc;
  --accent: #7c5e2a;
  --accent-soft: #f5e6c5;
  --accent-strong: #543c10;
  --primary: #1f6feb;
  --success: #15803d;
  --danger: #b91c1c;
  --shadow-1: 0 1px 4px rgba(0,0,0,0.05);
  --shadow-2: 0 6px 24px rgba(0,0,0,0.10);
}

body.dark, body[data-theme="dark"] {
  --bg: #15110a;
  --page-bg: #1c1810;
  --surface: #20180c;
  --ink: #f0e9d8;
  --muted: #a1958a;
  --hint: #6b6358;
  --line: #3d3326;
  --line-soft: #2a2418;
  --accent: #d4a456;
  --accent-soft: #3d2f18;
  --accent-strong: #f5c576;
  --primary: #4a90ff;
  --success: #4ade80;
  --danger: #ef4444;
  --shadow-1: 0 1px 4px rgba(0,0,0,0.4);
  --shadow-2: 0 6px 24px rgba(0,0,0,0.55);
}

@media (prefers-color-scheme: dark) {
  body:not([data-theme="light"]) {
    --bg: #15110a;
    --page-bg: #1c1810;
    --surface: #20180c;
    --ink: #f0e9d8;
    --muted: #a1958a;
    --hint: #6b6358;
    --line: #3d3326;
    --line-soft: #2a2418;
    --accent: #d4a456;
    --accent-soft: #3d2f18;
    --accent-strong: #f5c576;
    --primary: #4a90ff;
    --shadow-1: 0 1px 4px rgba(0,0,0,0.4);
    --shadow-2: 0 6px 24px rgba(0,0,0,0.55);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--ink);
  font-family: "Amiri", "Scheherazade New", "Geeza Pro", -apple-system, "Segoe UI", system-ui, sans-serif;
  min-height: 100vh; min-height: 100dvh;
  overflow-x: hidden;
}

.topbar {
  position: sticky; top: 0; z-index: 50;
  padding: max(8px, env(safe-area-inset-top, 0px) + 4px) 12px 8px;
  display: flex; gap: 10px; align-items: center; justify-content: space-between;
  background: rgba(250, 247, 240, 0.96);
  backdrop-filter: saturate(180%) blur(8px);
  -webkit-backdrop-filter: saturate(180%) blur(8px);
  border-bottom: 1px solid var(--line);
}
body.dark .topbar, body[data-theme="dark"] .topbar {
  background: rgba(21, 17, 10, 0.92);
}

.iconbtn {
  width: 44px; height: 44px; border: 0; background: transparent;
  color: var(--ink); cursor: pointer; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
}
.iconbtn:active { background: var(--line-soft); }

.page-pill {
  display: flex; align-items: center; gap: 4px;
  background: var(--surface);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 4px 4px;
  flex: 1; max-width: 240px; justify-content: center;
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
}
.page-pill input {
  width: 56px; border: 0; background: transparent;
  text-align: center; font: inherit; font-size: 15px; font-weight: 600;
  color: var(--ink); padding: 4px 0;
}
.page-pill .of {
  font-size: 12px; color: var(--muted); padding: 0 6px;
}
.navbtn {
  width: 34px; height: 34px; border: 0; background: transparent;
  color: var(--accent-strong); cursor: pointer; border-radius: 999px;
  font-size: 20px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
}
.navbtn:active { background: var(--accent-soft); }

main {
  padding: 16px 12px 110px;
  min-height: calc(100vh - 56px - 76px); min-height: calc(100dvh - 56px - 76px);
}

.loading {
  text-align: center; color: var(--muted); padding: 60px 20px;
  font-family: -apple-system, "Segoe UI", sans-serif; font-size: 14px;
}

/* --- Mushaf page --- */
.mushaf-page {
  background: var(--page-bg);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 24px 18px 22px;
  box-shadow: var(--shadow-1);
  position: relative;
  touch-action: pan-y pinch-zoom;
}
.mushaf-page::before, .mushaf-page::after {
  content: ''; position: absolute; left: 8px; right: 8px; height: 4px;
  background: repeating-linear-gradient(90deg, var(--line) 0, var(--line) 6px, transparent 6px, transparent 12px);
}
.mushaf-page::before { top: 10px; }
.mushaf-page::after  { bottom: 10px; }

.page-header {
  text-align: center; padding-bottom: 12px; margin-bottom: 14px;
  border-bottom: 1px solid var(--line-soft);
  font-family: -apple-system, "Segoe UI", sans-serif;
}
.page-header .surahs {
  display: flex; justify-content: center; gap: 10px; flex-wrap: wrap;
  font-family: "Amiri", "Scheherazade New", serif;
  font-size: 18px; font-weight: 700; color: var(--accent-strong);
  margin-bottom: 4px;
}
.page-header .meta {
  color: var(--muted); font-size: 11px;
}

.ayat-body {
  font-size: 24px; line-height: 2.1; text-align: justify;
  text-align-last: center; word-spacing: 1px;
  direction: rtl;
  -webkit-user-select: none; user-select: none;
}
.word {
  display: inline-block; padding: 2px 5px; margin: 0 1px;
  border-radius: 6px; cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.word.sel {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 700;
}
.word:active { background: var(--accent-soft); }

.ayah-num {
  display: inline-block; margin: 0 4px;
  width: 1.6em; height: 1.6em; line-height: 1.5em;
  text-align: center; border-radius: 50%;
  background: transparent;
  border: 1.5px solid var(--accent);
  color: var(--accent-strong);
  font-size: 14px; font-family: -apple-system, "Segoe UI", sans-serif; font-weight: 600;
  vertical-align: middle;
  cursor: pointer;
  transition: background 0.12s, transform 0.12s;
}
.ayah-num:active { transform: scale(0.9); }
.ayah-num.fully-selected {
  background: var(--accent); color: var(--page-bg);
}
.ayah-num.has-selection {
  background: var(--accent-soft);
}

.surah-divider-wrap { text-align: center; }
.surah-divider {
  display: inline-block; margin: 18px 0 12px;
  font-family: "Amiri", serif; font-size: 20px; font-weight: 700;
  color: var(--accent-strong);
  border: 2px solid var(--accent); border-radius: 10px;
  padding: 4px 18px; background: var(--accent-soft);
}

/* --- bottom action bar --- */
.actionbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  background: rgba(250, 247, 240, 0.96);
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  border-top: 1px solid var(--line);
  display: flex; gap: 8px; align-items: stretch;
  font-family: -apple-system, "Segoe UI", sans-serif;
}
body.dark .actionbar, body[data-theme="dark"] .actionbar {
  background: rgba(21, 17, 10, 0.92);
}

.ab-secondary {
  flex: 0 0 auto; min-width: 72px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 6px 12px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font: inherit; font-size: 11px;
}
.ab-secondary .badge {
  background: var(--accent-soft); color: var(--accent-strong);
  min-width: 24px; padding: 2px 8px; border-radius: 999px;
  font-size: 13px; font-weight: 700;
}
.ab-secondary .badge.highlight {
  background: var(--accent); color: var(--page-bg);
}
.ab-secondary .label { color: var(--muted); font-size: 10px; }
.ab-secondary:disabled { opacity: 0.5; }

.ab-primary {
  flex: 1; background: var(--success); color: white;
  border: 0; border-radius: 12px; padding: 12px;
  cursor: pointer; font: inherit; font-size: 14px; font-weight: 600;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  transition: background 0.15s, opacity 0.15s;
}
.ab-primary:disabled {
  background: var(--line); color: var(--muted); cursor: not-allowed;
}
.ab-primary:active:not(:disabled) { background: #166534; }

/* --- bottom sheets --- */
.sheet-bg {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s;
}
.sheet-bg.show { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 100;
  background: var(--surface); color: var(--ink);
  border-radius: 18px 18px 0 0;
  padding: 8px 18px max(20px, env(safe-area-inset-bottom)) ;
  box-shadow: var(--shadow-2);
  max-height: 85vh; max-height: 85dvh;
  overflow-y: auto;
  transform: translateY(105%); transition: transform 0.25s ease;
  font-family: -apple-system, "Segoe UI", sans-serif;
}
.sheet.show { transform: translateY(0); }
.sheet-handle {
  width: 38px; height: 4px; background: var(--line);
  border-radius: 999px; margin: 6px auto 14px;
}
.sheet h2 {
  margin: 0 0 12px; font-size: 17px; font-weight: 600;
}
.sheet h3 {
  margin: 18px 0 10px; font-size: 14px; font-weight: 600; color: var(--muted);
}
.row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 0; border-bottom: 1px solid var(--line-soft);
  font-size: 14px;
}
.row:last-child { border-bottom: 0; }
.row-label { color: var(--ink); }
.text-input {
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 12px; font: inherit; font-size: 14px; color: var(--ink);
  min-width: 180px; max-width: 220px;
}
.text-input.small { min-width: 80px; max-width: 100px; }

/* segmented control */
.seg {
  display: inline-flex; background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 2px; gap: 2px;
}
.seg-btn {
  background: transparent; border: 0; padding: 6px 12px; border-radius: 8px;
  font: inherit; font-size: 12px; color: var(--ink); cursor: pointer;
}
.seg-btn.active { background: var(--accent-soft); color: var(--accent-strong); font-weight: 600; }

/* switch */
.switch { position: relative; width: 44px; height: 26px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; inset: 0; background: var(--line);
  border-radius: 999px; transition: background 0.2s;
}
.switch .slider::before {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 20px; height: 20px; background: white;
  border-radius: 50%; transition: transform 0.2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(18px); }

/* preview-sheet content */
.preview-head {
  display: flex; align-items: center; gap: 12px; margin-bottom: 10px;
}
.preview-head .stats { color: var(--muted); font-size: 12px; }
.preview-body {
  font-size: 20px; line-height: 1.9; text-align: justify;
  text-align-last: center; direction: rtl;
  background: var(--bg);
  border: 1px dashed var(--line); border-radius: 10px;
  padding: 14px 16px; min-height: 80px;
  user-select: none;
}
.preview-body .pword {
  display: inline-block; padding: 1px 4px; margin: 0 1px;
  border-radius: 5px;
}
.preview-body .pword.sel {
  background: var(--accent-soft); color: var(--accent-strong);
  font-weight: 700; cursor: pointer;
}
.preview-body .ayah-num-mini {
  display: inline-block; padding: 0 7px;
  border: 1px solid var(--accent); color: var(--accent-strong);
  border-radius: 999px; font-size: 11px;
  font-family: -apple-system, "Segoe UI", sans-serif;
  margin: 0 4px; vertical-align: middle; cursor: pointer;
}
.preview-body .ellipsis { color: var(--muted); font-size: 18px; margin: 0 10px; }

/* saved list */
.saved-list {
  max-height: 240px; overflow-y: auto;
  border: 1px solid var(--line-soft); border-radius: 10px;
  background: var(--bg);
}
.saved-item {
  padding: 10px 12px; border-bottom: 1px solid var(--line-soft);
  font-size: 13px;
}
.saved-item:last-child { border-bottom: 0; }
.saved-item .meta { color: var(--muted); font-size: 11px; margin-bottom: 4px; }
.saved-item .prev {
  font-family: "Amiri", serif; font-size: 16px; direction: rtl;
  color: var(--ink); line-height: 1.6;
  max-height: 3em; overflow: hidden;
}
.saved-item .prev .ph {
  background: var(--accent-soft); color: var(--accent-strong);
  padding: 0 4px; border-radius: 3px; font-weight: 700;
}
.saved-item .remove {
  background: none; border: 0; color: var(--danger);
  font-size: 11px; cursor: pointer; padding: 0;
  font-family: inherit;
}
.empty-saved { padding: 16px; text-align: center; color: var(--hint); font-size: 13px; }

.sheet-actions {
  display: flex; gap: 8px; padding: 16px 0 0;
}
.btn-primary {
  flex: 1; background: var(--primary); color: white;
  border: 0; border-radius: 10px; padding: 12px;
  cursor: pointer; font: inherit; font-size: 14px; font-weight: 600;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-primary.share {
  background: var(--success); padding: 14px;
  font-size: 15px;
}
.btn-primary.share:active { background: #166534; }
.btn-secondary {
  flex: 0 0 auto; background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px;
  cursor: pointer; font: inherit; font-size: 13px;
}
.hint-text {
  margin: 14px 0 4px; color: var(--hint); font-size: 11px;
  line-height: 1.5;
}

/* toast */
.toast {
  position: fixed; bottom: calc(90px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--page-bg);
  padding: 10px 16px; border-radius: 12px;
  font-family: -apple-system, "Segoe UI", sans-serif; font-size: 13px;
  box-shadow: var(--shadow-2);
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
  z-index: 200; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; }
body.dark .toast, body[data-theme="dark"] .toast {
  background: var(--page-bg); color: var(--ink);
  border: 1px solid var(--line);
}

/* install hint */
.install-hint {
  position: fixed; bottom: calc(90px + env(safe-area-inset-bottom)); left: 12px; right: 12px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 14px;
  display: flex; align-items: center; gap: 8px;
  font-family: -apple-system, sans-serif; font-size: 13px;
  box-shadow: var(--shadow-1); z-index: 90;
}
.install-hint span { flex: 1; }
.install-hint button {
  background: var(--primary); color: white; border: 0;
  border-radius: 8px; padding: 6px 12px; cursor: pointer;
  font: inherit; font-size: 13px;
}
.install-hint button[aria-label="إخفاء"] {
  background: transparent; color: var(--muted); padding: 4px 8px;
}

/* swipe-page animation */
.mushaf-page.swiping { transition: transform 0.15s; }

/* --- post-download how-to modal --- */
.howto-bg {
  position: fixed; inset: 0; z-index: 150;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
.howto-bg.show { opacity: 1; pointer-events: auto; }
.howto {
  position: fixed; left: 16px; right: 16px;
  top: 50%; z-index: 160;
  background: var(--surface); color: var(--ink);
  border-radius: 18px;
  padding: 24px 22px max(24px, env(safe-area-inset-bottom));
  box-shadow: var(--shadow-2);
  max-width: 480px; margin: 0 auto;
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
  transform: translateY(-50%) scale(0.94); opacity: 0;
  pointer-events: none; transition: opacity 0.2s, transform 0.2s;
}
.howto.show {
  opacity: 1; transform: translateY(-50%) scale(1); pointer-events: auto;
}
.howto-head {
  display: flex; gap: 10px; align-items: center;
  color: var(--success); margin-bottom: 12px;
}
.howto-head h2 { margin: 0; font-size: 18px; font-weight: 600; color: var(--ink); }
.howto-sub { margin: 0 0 14px; color: var(--muted); font-size: 13px; }
.howto-steps {
  margin: 0 0 14px; padding-inline-start: 24px;
  font-size: 14px; line-height: 1.85;
}
.howto-steps li { margin-bottom: 4px; }
.howto-steps b { color: var(--accent-strong); }
body.dark .howto-steps b, body[data-theme="dark"] .howto-steps b {
  color: var(--accent-strong);
}
.howto-note {
  font-size: 12px; color: var(--muted);
  background: var(--bg); border-radius: 8px;
  padding: 10px 12px; margin: 0 0 12px;
  line-height: 1.7;
}
.howto-dont {
  display: flex; gap: 8px; align-items: center;
  font-size: 12px; color: var(--muted); margin-bottom: 14px;
  cursor: pointer;
}
.howto-ok {
  width: 100%; background: var(--primary); color: white;
  border: 0; border-radius: 10px; padding: 12px;
  font: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.howto-ok:active { opacity: 0.85; }
