: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;
  --primary-soft: #dbe7ff;
  --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[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;
  --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; }
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;
}

/* ----- top bar ----- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  background: rgba(250,247,240,0.96);
  backdrop-filter: blur(10px) saturate(160%);
  -webkit-backdrop-filter: blur(10px) saturate(160%);
  border-bottom: 1px solid var(--line);
  display: flex; gap: 16px; align-items: center;
  padding: 10px 20px;
  font-family: -apple-system, "Segoe UI", system-ui, sans-serif;
}
body[data-theme="dark"] .topbar { background: rgba(21,17,10,0.92); }

.brand {
  display: flex; gap: 8px; align-items: center;
  color: var(--accent-strong); font-weight: 600; font-size: 15px;
}
.brand svg { color: var(--accent); }

.page-nav {
  display: flex; align-items: center; gap: 4px;
  background: var(--surface);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 4px;
}
.page-input-wrap {
  display: flex; align-items: center; padding: 0 8px;
}
.page-input-wrap input {
  border: 0; background: transparent; color: var(--ink);
  font: inherit; font-size: 14px; font-weight: 600;
  width: 56px; text-align: center; padding: 6px 0;
}
.page-input-wrap input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.page-input-wrap .of { font-size: 12px; color: var(--muted); padding-inline-start: 4px; }
.navbtn {
  width: 32px; height: 32px; border: 0; background: transparent;
  color: var(--accent-strong); border-radius: 999px;
  font-size: 18px; font-weight: 600; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.navbtn:hover { background: var(--accent-soft); }

.topbar-meta {
  margin-inline-start: auto;
  display: flex; gap: 10px; align-items: center;
}
.topbar-meta .info { color: var(--muted); font-size: 12px; padding: 0 4px; }

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

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

/* ----- main layout: reader + sidebar ----- */
.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 24px;
  max-width: 1320px; margin: 0 auto;
  padding: 24px 24px 60px;
}

.reader {
  min-width: 0;
}

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

/* ----- Mushaf page ----- */
.mushaf-page {
  background: var(--page-bg);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 40px 56px 36px;
  box-shadow: var(--shadow-1);
  position: relative;
  max-width: 820px; margin: 0 auto;
}
.mushaf-page::before, .mushaf-page::after {
  content: ''; position: absolute; left: 14px; right: 14px; height: 5px;
  background: repeating-linear-gradient(90deg, var(--line) 0, var(--line) 8px, transparent 8px, transparent 18px);
}
.mushaf-page::before { top: 16px; }
.mushaf-page::after  { bottom: 16px; }

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

.ayat-body {
  font-size: 30px; line-height: 2.1; text-align: justify;
  text-align-last: center; word-spacing: 2px;
  direction: rtl;
  user-select: none; -webkit-user-select: none;
}
.word {
  display: inline-block; padding: 1px 5px; margin: 0 1px;
  border-radius: 5px; cursor: pointer;
  transition: background 0.1s, color 0.1s;
}
.word:hover { background: var(--accent-soft); }
.word.sel {
  background: var(--accent-soft); color: var(--accent-strong); font-weight: 700;
}
.ayah-num {
  display: inline-block; margin: 0 4px;
  width: 1.5em; height: 1.5em; line-height: 1.45em; text-align: center;
  border-radius: 50%;
  border: 1.5px solid var(--accent); color: var(--accent-strong);
  font-size: 18px; font-family: -apple-system, sans-serif; font-weight: 600;
  vertical-align: middle; cursor: pointer;
  transition: background 0.1s, transform 0.1s;
}
.ayah-num:hover { background: var(--accent-soft); transform: scale(1.05); }
.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: 22px 0 14px;
  font-family: "Amiri", serif; font-size: 24px; font-weight: 700;
  color: var(--accent-strong); background: var(--accent-soft);
  border: 2px solid var(--accent); border-radius: 12px;
  padding: 6px 24px;
}

/* ----- sidebar ----- */
.sidebar {
  display: flex; flex-direction: column; gap: 16px;
  position: sticky; top: 76px; align-self: start;
  max-height: calc(100vh - 100px); overflow-y: auto;
  padding-inline-start: 4px;
  font-family: -apple-system, sans-serif;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px;
  padding: 16px 18px;
}
.panel-head {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 12px;
}
.panel-head h2 {
  margin: 0; font-size: 13px; font-weight: 600;
  color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em;
}
.panel-head .stats { color: var(--muted); font-size: 12px; margin-inline-start: auto; }
.panel-head .badge {
  background: var(--accent-soft); color: var(--accent-strong);
  padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 700;
  margin-inline-start: auto;
}

/* current card preview */
.preview-body {
  font-size: 18px; line-height: 1.85; text-align: justify;
  text-align-last: center; direction: rtl;
  background: var(--bg); border: 1px dashed var(--line);
  border-radius: 8px; padding: 12px 14px; min-height: 64px;
  max-height: 280px; overflow-y: auto;
  user-select: none;
  margin-bottom: 12px;
}
.preview-body .pword {
  display: inline-block; padding: 1px 4px; margin: 0 1px;
  border-radius: 4px;
}
.preview-body .pword.sel {
  background: var(--accent-soft); color: var(--accent-strong);
  font-weight: 700; cursor: pointer;
}
.preview-body .pword.sel:hover { background: #f8d878; }
.preview-body .ayah-num-mini {
  display: inline-block; padding: 0 6px;
  border: 1px solid var(--accent); color: var(--accent-strong);
  border-radius: 999px; font-size: 11px;
  font-family: -apple-system, sans-serif;
  margin: 0 3px; vertical-align: middle; cursor: pointer;
}
.preview-body .ayah-num-mini:hover { background: var(--accent-soft); }
.preview-body .ellipsis { color: var(--muted); font-size: 16px; margin: 0 8px; }
.preview-body .empty-hint {
  font-family: -apple-system, sans-serif; font-size: 12px;
  color: var(--hint); text-align: center; padding: 18px 8px;
  direction: rtl; line-height: 1.6;
}

.panel-actions {
  display: flex; gap: 8px; flex-wrap: wrap;
}
.panel-actions.stack { flex-direction: column; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-size: 13px;
  border: 1px solid transparent; border-radius: 8px;
  padding: 9px 14px; cursor: pointer;
  transition: background 0.12s, opacity 0.12s;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary  { background: var(--primary); color: white; }
.btn-primary:not(:disabled):hover { filter: brightness(0.95); }
.btn-success  { background: var(--success); color: white; flex: 1; font-weight: 600; }
.btn-success:not(:disabled):hover { background: #166534; }
.btn-light    {
  background: var(--surface); color: var(--ink); border-color: var(--line);
}
.btn-light:not(:disabled):hover { background: var(--line-soft); }
.btn-ghost {
  background: transparent; color: var(--muted); border: 1px dashed var(--line);
  font-size: 12px;
}
.btn-ghost:not(:disabled):hover { color: var(--danger); border-color: var(--danger); }

kbd {
  display: inline-block;
  background: rgba(255,255,255,0.18); color: inherit;
  border: 1px solid rgba(255,255,255,0.25); border-radius: 4px;
  padding: 0 5px; font-size: 10px; font-family: ui-monospace, monospace;
  margin-inline-start: 6px;
}
.btn-light kbd, .btn-success kbd {
  background: rgba(0,0,0,0.07); border-color: rgba(0,0,0,0.1); color: inherit;
}
body[data-theme="dark"] .btn-light kbd { background: rgba(255,255,255,0.07); border-color: rgba(255,255,255,0.15); }

/* saved cards list */
.saved-list {
  display: flex; flex-direction: column; gap: 6px;
  max-height: 340px; overflow-y: auto;
}
.saved-item {
  background: var(--bg); border: 1px solid var(--line-soft);
  border-radius: 8px; padding: 8px 10px;
  font-size: 12px;
}
.saved-item .meta {
  display: flex; justify-content: space-between; align-items: center;
  color: var(--muted); font-size: 11px; margin-bottom: 4px;
}
.saved-item .prev {
  font-family: "Amiri", serif; font-size: 15px;
  direction: rtl; color: var(--ink); line-height: 1.7;
  max-height: 3.2em; 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); cursor: pointer;
  font-size: 11px; padding: 0; font-family: inherit;
}
.saved-empty {
  color: var(--hint); font-size: 12px; text-align: center; padding: 14px;
}

/* export panel */
.field { display: block; margin-bottom: 12px; }
.field-label {
  display: block; font-size: 11px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 4px;
}
.text-input {
  width: 100%; background: var(--bg); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px;
  font: inherit; font-size: 13px; color: var(--ink); direction: ltr;
}
.text-input:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.export-hint {
  margin: 12px 0 0; font-size: 11px; color: var(--hint);
  line-height: 1.6;
}
.export-hint code {
  background: var(--bg); padding: 1px 5px; border-radius: 3px;
  font-family: ui-monospace, monospace; direction: ltr; display: inline-block;
}

/* toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--page-bg);
  padding: 10px 18px; border-radius: 10px;
  font-family: -apple-system, sans-serif; font-size: 13px;
  box-shadow: var(--shadow-2);
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
  z-index: 200; max-width: 80vw;
}
.toast.show { opacity: 1; }

/* modals */
.modal-bg {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
  z-index: 150;
}
.modal-bg.show { opacity: 1; pointer-events: auto; }
.modal {
  position: fixed; left: 50%; top: 50%;
  transform: translate(-50%, -50%) scale(0.95);
  background: var(--surface); color: var(--ink);
  border-radius: 14px; box-shadow: var(--shadow-2);
  width: min(520px, 92vw); max-height: 80vh; overflow-y: auto;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
  z-index: 160;
  font-family: -apple-system, sans-serif;
}
.modal.show {
  opacity: 1; transform: translate(-50%, -50%) scale(1);
  pointer-events: auto;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--line-soft);
}
.modal-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.modal-head .close { margin: -8px; }
.modal-body { padding: 18px 20px 22px; font-size: 14px; line-height: 1.7; }
.modal-body ol { padding-inline-start: 24px; margin: 8px 0; }
.modal-body .note {
  background: var(--bg); border-radius: 8px; padding: 10px 12px;
  font-size: 12px; color: var(--muted); margin-top: 14px;
}
.modal-body code {
  background: var(--bg); padding: 1px 5px; border-radius: 3px;
  font-family: ui-monospace, monospace; direction: ltr; display: inline-block;
}
.modal-body .dont-show {
  display: flex; align-items: center; gap: 8px;
  margin-top: 14px; font-size: 12px; color: var(--muted); cursor: pointer;
}

.shortcuts { width: 100%; border-collapse: collapse; font-size: 13px; }
.shortcuts td { padding: 6px 8px; }
.shortcuts td:first-child { width: 110px; }
.shortcuts kbd {
  background: var(--bg); border: 1px solid var(--line); color: var(--ink);
  padding: 1px 7px; border-radius: 4px; margin: 0;
  font-family: ui-monospace, monospace; font-size: 11px;
}

/* ----- responsive ----- */
@media (max-width: 1000px) {
  .app {
    grid-template-columns: 1fr;
    padding: 16px;
  }
  .sidebar {
    position: static; max-height: none; padding: 0;
  }
  .mushaf-page { padding: 28px 28px 22px; }
  .ayat-body { font-size: 26px; line-height: 2.0; }
}
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; padding: 8px 12px; gap: 8px; }
  .topbar .seg { display: none; }  /* keep card-mode toggle hidden on small */
  .topbar-meta { margin-inline-start: 0; }
  .mushaf-page { padding: 22px 18px 18px; }
}
