/* ── Variables ─────────────────────────────────────── */
:root {
    --bg: #faf6ef;
    --bg-alt: #f3ece0;
    --surface: #fff8f0;
    --text: #2c1810;
    --text-muted: #7a6553;
    --gold: #b8860b;
    --gold-light: rgba(184, 134, 11, 0.12);
    --gold-hover: rgba(184, 134, 11, 0.08);
    --green: #4a7c5e;
    --green-light: rgba(74, 124, 94, 0.10);
    --border: #e0d5c5;
    --panel-bg: #f7f1e8;
    --panel-width: 50%;
    --header-h: 56px;
    --verse-num: #a08050;
    --highlight: rgba(184, 134, 11, 0.18);
    --font-ar: 'Amiri', serif;
    --font-en: 'Segoe UI', system-ui, -apple-system, sans-serif;
    --radius: 8px;
}

/* ── Group Colors (semantic families) ──────────── */
:root {
    --group-0: #b8860b;   /* الإلهيات — Theology */
    --group-1: #4a7c5e;   /* الأخلاق — Ethics */
    --group-2: #8b4513;   /* الآخرة — Eschatology */
    --group-3: #2e7d6e;   /* الطبيعة — Nature */
    --group-4: #6a5acd;   /* الإنسان — Human */
    --group-5: #cd5c5c;   /* المجتمع — Society */
}

/* ── Dark Mode ─────────────────────────────────── */
.dark-mode {
    --bg: #1a1612;
    --bg-alt: #231e18;
    --surface: #2a2420;
    --text: #e8dfd5;
    --text-muted: #a09080;
    --gold: #d4a020;
    --gold-light: rgba(212, 160, 32, 0.15);
    --gold-hover: rgba(212, 160, 32, 0.10);
    --green: #6aaa80;
    --green-light: rgba(106, 170, 128, 0.12);
    --border: #3a3028;
    --panel-bg: #221c16;
    --verse-num: #c09060;
    --highlight: rgba(212, 160, 32, 0.20);
    --group-0: #d4a020;
    --group-1: #6aaa80;
    --group-2: #c07030;
    --group-3: #45b8a0;
    --group-4: #8a7ae8;
    --group-5: #e07070;
}

.dark-mode #header {
    background: rgba(42, 36, 32, 0.92);
}

.dark-mode .verse {
    border-bottom-color: rgba(255, 255, 255, 0.04);
}

.dark-mode ::-webkit-scrollbar-thumb { background: #4a3e30; }

/* ── Reset ──────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: var(--font-ar);
    background: var(--bg);
    color: var(--text);
    line-height: 1.8;
    min-height: 100vh;
    overflow-x: hidden;
}

/* ── Header ─────────────────────────────────────── */
#header {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-h);
    padding: 0 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(8px);
}

.logo {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--gold);
    white-space: nowrap;
}

.header-center {
    display: flex;
    align-items: center;
    gap: 8px;
}

#surah-select {
    font-family: var(--font-ar);
    font-size: 1.05rem;
    padding: 6px 32px 6px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
    min-width: 200px;
    text-align: center;
}

.nav-btn {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text-muted);
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}
.nav-btn:hover { background: var(--bg-alt); }
.nav-btn:disabled { opacity: 0.3; cursor: default; }

.surah-meta {
    font-family: var(--font-en);
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.themes-link {
    font-size: 0.8rem;
    color: var(--gold);
    text-decoration: none;
    padding: 4px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: background 0.15s;
    white-space: nowrap;
}
.themes-link:hover { background: var(--gold-hover); }

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.theme-toggle-btn,
.trans-toggle-btn {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text-muted);
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.theme-toggle-btn:hover,
.trans-toggle-btn:hover { background: var(--bg-alt); }

.trans-toggle-btn {
    font-family: var(--font-en);
    font-size: 0.75rem;
    font-weight: 700;
}
.trans-toggle-btn.active {
    background: var(--gold-light);
    color: var(--gold);
    border-color: var(--gold);
}

/* ── Main Layout ────────────────────────────────── */
#main {
    min-height: calc(100vh - var(--header-h));
    transition: margin 0.3s ease;
}

.panel-open #main {
    margin-left: var(--panel-width);
}

#reader {
    padding: 32px 5% 80px;
}

/* ── Surah Title ────────────────────────────────── */
.surah-title-block {
    text-align: center;
    margin-bottom: 40px;
    padding: 24px 0;
}

.surah-title-block h2 {
    font-size: 2rem;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 4px;
}

.surah-title-block p {
    font-family: var(--font-en);
    font-size: 0.9rem;
    color: var(--text-muted);
}

/* ── Verses ─────────────────────────────────────── */
.verse {
    padding: 18px 0;
    border-bottom: 1px solid rgba(0,0,0,0.04);
    text-align: center;
}

.verse:last-child { border-bottom: none; }

.verse-text {
    font-size: calc(1.6rem * var(--text-scale, 1));
    line-height: 2.4;
    direction: rtl;
    text-align: center;
}

.verse-num {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    font-family: var(--font-en);
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--verse-num);
    border: 1.5px solid var(--verse-num);
    border-radius: 50%;
    opacity: 0.6;
    margin: 0 6px;
    vertical-align: middle;
    /* clickable — hover/active styles in tafsir section */
    cursor: pointer;
    transition: opacity 0.15s, transform 0.15s, background 0.15s;
}

/* ── Words ──────────────────────────────────────── */
.word {
    cursor: default;
    padding: 2px 1px;
    border-radius: 3px;
    transition: background 0.15s, color 0.15s;
    display: inline;
}

.word-has-root {
    cursor: pointer;
    border-bottom: 1.5px dotted rgba(184, 134, 11, 0.3);
}

.word-has-root:hover {
    background: var(--gold-hover);
    border-bottom-color: var(--gold);
}

.word-selected {
    background: var(--gold-light) !important;
    border-bottom: 2px solid var(--gold) !important;
    color: var(--gold);
}

.word-highlighted {
    background: var(--highlight);
    border-bottom: 1.5px solid rgba(184, 134, 11, 0.4);
}

/* ── Root Panel (Sidebar) ───────────────────────── */
#root-panel {
    position: fixed;
    top: var(--header-h);
    left: 0;
    right: auto;
    width: var(--panel-width);
    height: calc(100vh - var(--header-h));
    background: var(--panel-bg);
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    overflow-y: auto;
    z-index: 50;
    direction: rtl;
}

#root-panel.panel-visible {
    transform: translateX(0);
}

/* ── Panel Backdrop (mobile) ────────────────────── */
.panel-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 49;
    opacity: 0;
    transition: opacity 0.3s ease;
}
.panel-backdrop.visible {
    opacity: 1;
}

/* ── Drawer Handle (mobile) ─────────────────────── */
.drawer-handle {
    display: none;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--panel-bg);
    z-index: 1;
}

.panel-header h3 {
    font-size: 1rem;
    color: var(--text-muted);
}

.close-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
}
.close-btn:hover { background: var(--bg-alt); }

.panel-body {
    padding: 20px;
}

.root-arabic {
    font-size: 2.8rem;
    font-weight: 700;
    color: var(--gold);
    text-align: center;
    margin-bottom: 4px;
    letter-spacing: 4px;
}

.root-translit {
    font-family: var(--font-en);
    font-size: 0.85rem;
    color: var(--text-muted);
    text-align: center;
    margin-bottom: 4px;
    direction: ltr;
}

.root-gloss {
    font-family: var(--font-en);
    font-size: 0.8rem;
    font-style: italic;
    color: var(--gold);
    text-align: center;
    margin-bottom: 12px;
    direction: ltr;
}

.root-meaning {
    font-family: var(--font-en);
    font-size: 0.9rem;
    color: var(--text);
    line-height: 1.6;
    margin-bottom: 16px;
    padding: 12px;
    background: var(--surface);
    border-radius: var(--radius);
    direction: ltr;
    text-align: left;
}

.root-freq {
    font-family: var(--font-en);
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: center;
    margin-bottom: 8px;
}

/* ── Revelation Indicator (Makki/Madani) ─────────── */
.root-revelation {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 18px;
    font-family: var(--font-en);
    font-size: 0.75rem;
    color: var(--text-muted);
}
.rev-bar {
    display: flex;
    width: 120px;
    height: 6px;
    border-radius: 3px;
    overflow: hidden;
    background: var(--bg-alt);
}
.rev-makki {
    background: #c17d3e;
    height: 100%;
    transition: width 0.3s;
}
.rev-madani {
    background: #3e8a5e;
    height: 100%;
    transition: width 0.3s;
}
.rev-label {
    white-space: nowrap;
}
.rev-label.makki { color: #c17d3e; }
.rev-label.madani { color: #3e8a5e; }

[data-theme="dark"] .rev-makki { background: #d4944f; }
[data-theme="dark"] .rev-madani { background: #56b07a; }
[data-theme="dark"] .rev-label.makki { color: #d4944f; }
[data-theme="dark"] .rev-label.madani { color: #56b07a; }

/* ── Panel Sections (Accordion) ─────────────────── */
.panel-section {
    margin-top: 0;
    padding-top: 0;
    border-top: 1px solid var(--border);
}

.panel-section h4 {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-bottom: 0;
}

/* Accordion toggle header */
.section-toggle {
    cursor: pointer;
    user-select: none;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 4px;
    padding: 12px 0;
    margin: 0;
    transition: color 0.15s;
}
.section-toggle:hover { color: var(--gold); }

.section-toggle .chevron {
    font-size: 0.65rem;
    flex-shrink: 0;
    width: 14px;
    text-align: center;
    opacity: 0.6;
}

.section-toggle .section-title {
    flex: 1;
}

.section-desc {
    display: block;
    width: 100%;
    font-family: var(--font-en);
    font-size: 0.7rem;
    font-weight: 400;
    font-style: italic;
    color: var(--text-muted);
    opacity: 0.6;
    direction: ltr;
    text-align: left;
    margin-top: 1px;
    line-height: 1.4;
}

/* Collapsible body */
.section-body {
    overflow: hidden;
    transition: max-height 0.3s ease, opacity 0.25s ease;
    max-height: 8000px;
    opacity: 1;
}

.panel-section.collapsed .section-body {
    max-height: 0;
    opacity: 0;
    pointer-events: none;
}

/* ── Family Info ────────────────────────────────── */
.family-badge {
    display: inline-block;
    padding: 4px 12px;
    background: var(--green-light);
    color: var(--green);
    border-radius: 16px;
    font-size: 0.85rem;
    margin-bottom: 8px;
}

.family-meaning {
    font-family: var(--font-en);
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 10px;
    direction: ltr;
    text-align: left;
}

.family-roots {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.family-root-chip {
    padding: 4px 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    font-size: 0.95rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.family-root-chip:hover {
    background: var(--gold-light);
    border-color: var(--gold);
}

.family-root-chip.active {
    background: var(--gold-light);
    border-color: var(--gold);
    color: var(--gold);
}

/* ── Mufradat (Classical Lexicon) ───────────────── */
.mufradat-root {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--gold);
    margin-bottom: 8px;
}

.mufradat-text {
    font-size: 1.05rem;
    line-height: 2;
    color: var(--text);
    margin-bottom: 12px;
    padding: 12px;
    background: var(--surface);
    border-radius: var(--radius);
    max-height: 200px;
    overflow-y: auto;
}

.mufradat-verses-label {
    font-size: 0.8rem;
    color: var(--text-muted);
    display: block;
    margin-bottom: 6px;
}

.mufradat-verse-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    direction: ltr;
}

.mufradat-verse-chip {
    font-family: var(--font-en);
    font-size: 0.7rem;
    padding: 2px 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--verse-num);
    text-decoration: none;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.mufradat-verse-chip:hover {
    background: var(--gold-light);
    border-color: var(--gold);
    color: var(--gold);
}

.mufradat-verse-more {
    font-family: var(--font-en);
    font-size: 0.7rem;
    padding: 2px 8px;
    color: var(--text-muted);
}

/* ── Furuq (Linguistic Distinctions) ────────────── */
.furuq-card {
    padding: 10px 12px;
    margin-bottom: 8px;
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid transparent;
    transition: border-color 0.15s;
}
.furuq-card:hover {
    border-color: var(--border);
}

.furuq-pair {
    font-size: 1rem;
    font-weight: 700;
    color: var(--green);
    margin-bottom: 6px;
}

.furuq-text {
    font-size: 0.95rem;
    line-height: 1.9;
    color: var(--text);
}

.furuq-more {
    font-family: var(--font-en);
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
    padding: 6px;
}

/* ── Connected Verses ───────────────────────────── */
/* ── Co-occurrence Section ────────────────────── */
.cooccur-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.cooccur-item {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
    gap: 2px 10px;
    padding: 8px 10px;
    border-radius: 6px;
    background: var(--bg-alt);
    transition: background 0.15s;
}
.cooccur-item:hover {
    background: var(--gold-light);
}
.cooccur-root {
    grid-row: 1 / 3;
    font-family: var(--font-ar);
    font-size: 1.05rem;
    color: var(--gold);
    text-decoration: none;
    cursor: pointer;
    align-self: center;
    white-space: nowrap;
    direction: rtl;
}
.cooccur-root:hover {
    text-decoration: underline;
}
.cooccur-info {
    font-size: 0.72rem;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.cooccur-bw {
    font-family: var(--font-en);
    color: var(--text-muted);
    opacity: 0.7;
}
.cooccur-meaning {
    font-family: var(--font-en);
}
.cooccur-bar-wrap {
    display: flex;
    align-items: center;
    gap: 6px;
    height: 6px;
}
.cooccur-bar-wrap {
    position: relative;
    height: 14px;
}
.cooccur-bar {
    position: absolute;
    left: 0; top: 4px;
    height: 6px;
    border-radius: 3px;
    background: var(--gold);
    opacity: 0.5;
    transition: width 0.3s;
}
.cooccur-stat {
    position: relative;
    font-family: var(--font-en);
    font-size: 0.65rem;
    color: var(--text-muted);
    z-index: 1;
}

.connected-group {
    margin-bottom: 16px;
}

.connected-group-header {
    font-family: var(--font-en);
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--green);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 6px;
    direction: ltr;
    text-align: left;
}

.connected-verse {
    display: block;
    padding: 10px 12px;
    margin-bottom: 4px;
    background: var(--surface);
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
    color: var(--text);
    transition: background 0.15s;
    border: 1px solid transparent;
}

.connected-verse:hover {
    background: var(--gold-hover);
    border-color: var(--gold-light);
}

.connected-verse-key {
    font-family: var(--font-en);
    font-size: 0.75rem;
    color: var(--verse-num);
    font-weight: 600;
    margin-bottom: 2px;
    direction: ltr;
    display: inline-block;
}

.connected-verse-text {
    font-size: 1.05rem;
    line-height: 1.7;
    color: var(--text);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* al-Raghib cited verse highlight */
.connected-verse.raghib-cited {
    border-inline-start: 3px solid var(--gold);
    padding-inline-start: 8px;
}
.connected-verse.raghib-cited .connected-verse-key::after {
    content: '📖';
    font-size: 0.65rem;
    margin-inline-start: 4px;
    opacity: 0.7;
}

.show-more {
    display: block;
    width: 100%;
    padding: 10px;
    margin-top: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-family: var(--font-ar);
    font-size: 0.9rem;
    color: var(--gold);
    cursor: pointer;
    transition: background 0.15s;
}
.show-more:hover { background: var(--gold-hover); }

/* ── Hadith Bridge Section ─────────────────────── */
.hadith-book-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 5px 0;
    font-family: var(--font-en);
    font-size: 0.82rem;
    color: var(--text-muted);
}
.hadith-book-name {
    flex: 1;
    text-align: start;
}
.hadith-count-badge {
    background: var(--gold-light);
    color: var(--gold);
    border-radius: 10px;
    padding: 1px 8px;
    font-size: 0.78rem;
    font-weight: 600;
    margin-inline-start: 8px;
    white-space: nowrap;
}
.hadith-book-bar {
    height: 3px;
    background: var(--gold);
    opacity: 0.35;
    border-radius: 2px;
    margin-bottom: 6px;
    transition: width 0.3s;
}
.hadith-reader-link {
    display: block;
    margin-top: 12px;
    padding: 8px 14px;
    text-align: center;
    background: var(--green-light);
    color: var(--green);
    border-radius: 6px;
    font-family: var(--font-ar);
    font-size: 0.95rem;
    text-decoration: none;
    transition: background 0.15s;
}
.hadith-reader-link:hover {
    background: rgba(74, 124, 94, 0.2);
}

/* ── Verse Number (clickable for tafsir) ───────── */
.verse-num:hover {
    opacity: 1;
    transform: scale(1.15);
    background: var(--gold-light);
}
.verse-num.tafsir-open {
    opacity: 1;
    background: var(--gold-light);
    border-color: var(--gold);
    color: var(--gold);
}

/* ── Tafsir Block ──────────────────────────────── */
.tafsir-block {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.35s ease, opacity 0.25s ease;
    opacity: 0;
}
.tafsir-block.open {
    max-height: 2000px;
    opacity: 1;
}

.tafsir-inner {
    margin: 12px auto;
    max-width: 720px;
    padding: 16px 20px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    direction: rtl;
    text-align: right;
}

.tafsir-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border);
}

.tafsir-label {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 600;
}

.tafsir-select {
    font-family: var(--font-ar);
    font-size: 0.85rem;
    padding: 4px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
    direction: rtl;
}

.tafsir-text {
    font-size: calc(1.15rem * var(--text-scale, 1));
    line-height: 2;
    color: var(--text);
}
.tafsir-text p {
    margin-bottom: 8px;
}
.tafsir-text p:last-child {
    margin-bottom: 0;
}

/* Tafsir optgroup + Urdu/English styles */
.tafsir-select optgroup {
    font-weight: bold;
    font-style: normal;
    font-family: var(--font-ar);
}
.tafsir-text [lang="ur"],
.tafsir-text .ur {
    font-family: var(--font-ar);
    line-height: 2.2;
    direction: rtl;
    text-align: right;
}
.tafsir-text .arabic,
.tafsir-text .qpc-hafs {
    font-family: var(--font-ar);
    font-weight: 600;
}
.tafsir-text[dir="ltr"] {
    direction: ltr;
    text-align: left;
    font-family: var(--font-en);
    line-height: 1.8;
}

/* Tafsir loading spinner */
.tafsir-loading {
    text-align: center;
    padding: 20px;
    color: var(--gold);
    font-size: 0.95rem;
    animation: pulse 1.2s ease-in-out infinite;
}

/* Tafsir error */
.tafsir-error {
    text-align: center;
    padding: 16px;
    color: var(--text-muted);
    font-size: 0.85rem;
}
.tafsir-retry {
    display: inline-block;
    margin-top: 6px;
    padding: 4px 14px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-family: var(--font-ar);
    font-size: 0.85rem;
    color: var(--gold);
    cursor: pointer;
    transition: background 0.15s;
}
.tafsir-retry:hover {
    background: var(--gold-hover);
}

/* ── Loading ────────────────────────────────────── */
#loading-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    transition: opacity 0.3s;
}

#loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.loader-text {
    font-size: 1.4rem;
    color: var(--gold);
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 1; }
}

/* ── Thematic Indicators ──────────────────────── */
.verse-themes {
    display: flex;
    justify-content: center;
    gap: 5px;
    margin-top: 6px;
    opacity: 0.7;
}

.theme-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
    transition: transform 0.15s, opacity 0.15s;
    cursor: default;
}
.theme-dot:hover {
    transform: scale(1.8);
    opacity: 1;
}

.theme-dot[data-group="0"] { background: var(--group-0); }
.theme-dot[data-group="1"] { background: var(--group-1); }
.theme-dot[data-group="2"] { background: var(--group-2); }
.theme-dot[data-group="3"] { background: var(--group-3); }
.theme-dot[data-group="4"] { background: var(--group-4); }
.theme-dot[data-group="5"] { background: var(--group-5); }

.surah-theme-bar {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
    margin-bottom: 28px;
    padding: 0 5%;
}

.surah-theme-chip {
    font-size: 0.8rem;
    padding: 3px 12px;
    border: 1.5px solid;
    border-radius: 14px;
    background: transparent;
    white-space: nowrap;
    transition: background 0.15s;
    cursor: default;
}

.themes-hidden .verse-themes,
.themes-hidden .surah-theme-bar { display: none; }
.themes-hidden .surah-insight { display: none !important; }

/* ── Surah Thematic Insight ─────────────────── */
.surah-insight {
    text-align: center;
    max-width: 680px;
    margin: 0 auto 24px;
    padding: 12px 20px;
    font-family: var(--font-en);
    font-size: 0.82rem;
    line-height: 1.65;
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--gold-light);
}
.surah-insight .insight-label {
    font-family: var(--font-ar);
    font-size: 0.95rem;
    color: var(--gold);
    font-weight: 700;
    display: block;
    margin-bottom: 4px;
}
.surah-insight .insight-distinctive {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 4px;
    opacity: 0.85;
}

/* ── Translation Row ─────────────────────────── */
.verse-translation {
    font-family: var(--font-en);
    font-size: calc(0.9rem * var(--text-scale, 1));
    line-height: 1.7;
    color: var(--text-muted);
    text-align: center;
    direction: ltr;
    margin-top: 6px;
    padding: 6px 12px;
    max-width: 720px;
    margin-left: auto;
    margin-right: auto;
    border-top: 1px dashed var(--border);
}

/* ── Transliteration Row ─────────────────────── */
.verse-transliteration {
    font-family: var(--font-en);
    font-size: calc(0.85rem * var(--text-scale, 1));
    font-style: italic;
    line-height: 1.6;
    color: var(--text-muted);
    text-align: center;
    direction: ltr;
    margin-top: 4px;
    padding: 4px 12px;
    max-width: 720px;
    margin-left: auto;
    margin-right: auto;
    border-top: 1px dotted var(--border);
    opacity: 0.85;
}
.verse-transliteration u { text-decoration-color: var(--accent); }
.verse-transliteration b { font-weight: 600; color: var(--text-main); }

/* ── Word-by-Word Tooltip ────────────────────── */
.word-has-wbw {
    position: relative;
}

.word-has-wbw::after {
    content: attr(data-wbw);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 3px 8px;
    font-family: var(--font-en);
    font-size: 0.72rem;
    color: var(--text-muted);
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 10;
    direction: ltr;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.word-has-wbw:hover::after,
.word-has-wbw.wbw-visible::after {
    opacity: 1;
}

/* ── Root Search ────────────────────────────────── */
.root-search-wrap {
    position: relative;
    display: flex;
    align-items: center;
}

.root-search-btn {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text-muted);
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.root-search-btn:hover { background: var(--bg-alt); }
.root-search-btn.active {
    background: var(--gold-light);
    color: var(--gold);
    border-color: var(--gold);
}

.root-search-input {
    font-family: var(--font-ar);
    font-size: 0.95rem;
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text);
    width: 220px;
    margin-right: 6px;
    transition: width 0.2s ease, opacity 0.2s ease;
}
.root-search-input:focus {
    outline: none;
    border-color: var(--gold);
    box-shadow: 0 0 0 2px var(--gold-light);
}
.root-search-input::-webkit-search-cancel-button {
    -webkit-appearance: none;
    cursor: pointer;
    height: 14px;
    width: 14px;
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%237a6553'%3E%3Cpath d='M18 6L6 18M6 6l12 12' stroke='%237a6553' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E") center/contain no-repeat;
}

.root-search-results {
    position: absolute;
    top: 100%;
    right: 0;
    width: 340px;
    max-height: 420px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 8px 24px rgba(0,0,0,0.12);
    list-style: none;
    z-index: 200;
    margin-top: 4px;
    padding: 4px 0;
}

.root-search-results li {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 14px;
    cursor: pointer;
    transition: background 0.1s;
    direction: rtl;
}
.root-search-results li:hover,
.root-search-results li.active {
    background: var(--gold-hover);
}

.root-search-results .sr-root {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--gold);
    min-width: 60px;
    text-align: center;
    letter-spacing: 2px;
}

.root-search-results .sr-info {
    flex: 1;
    min-width: 0;
    direction: ltr;
    text-align: left;
}

.root-search-results .sr-bw {
    font-family: var(--font-en);
    font-size: 0.72rem;
    color: var(--text-muted);
}

.root-search-results .sr-meaning {
    font-family: var(--font-en);
    font-size: 0.78rem;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.root-search-results .sr-count {
    font-family: var(--font-en);
    font-size: 0.68rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.root-search-results .sr-empty {
    padding: 16px;
    text-align: center;
    font-family: var(--font-en);
    font-size: 0.82rem;
    color: var(--text-muted);
}

/* ── Search Mode Toggle ──────────────────────────── */
.search-mode-btn {
    background: var(--gold-light);
    border: 1px solid var(--gold);
    color: var(--gold);
    font-family: var(--font-ar);
    font-size: 0.72rem;
    padding: 2px 7px;
    border-radius: 10px;
    cursor: pointer;
    white-space: nowrap;
    line-height: 1.3;
    transition: background 0.15s, color 0.15s;
}
.search-mode-btn:hover {
    background: var(--gold);
    color: #fff;
}
.search-mode-btn.furuq-mode {
    background: var(--gold);
    color: #fff;
}

/* ── Furuq Search Results ────────────────────────── */
.root-search-results li.furuq-item {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    padding: 10px 12px;
    cursor: default;
}
.furuq-pair-header {
    display: flex;
    align-items: center;
    gap: 8px;
    font-family: var(--font-ar);
    font-size: 0.95rem;
    color: var(--text);
    direction: rtl;
}
.furuq-pair-header .furuq-vs {
    color: var(--gold);
    font-size: 0.75rem;
    font-family: var(--font-en);
}
.furuq-text-preview {
    font-family: var(--font-ar);
    font-size: 0.78rem;
    color: var(--text-muted);
    direction: rtl;
    line-height: 1.5;
    max-height: 2.4em;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: max-height 0.3s;
}
.furuq-item.expanded .furuq-text-preview {
    max-height: 500px;
    overflow: visible;
}
.furuq-expand-hint {
    font-family: var(--font-en);
    font-size: 0.65rem;
    color: var(--text-muted);
    opacity: 0.6;
    text-align: center;
    margin-top: 2px;
}
.furuq-item.expanded .furuq-expand-hint {
    display: none;
}

/* ── Scrollbar ──────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Responsive ─────────────────────────────────── */

/* Tablet: general layout adjustments */
@media (max-width: 900px) {
    #reader { padding: 20px 5% 60px; }
    .verse-text { font-size: calc(1.35rem * var(--text-scale, 1)); }
    .header-left { gap: 4px; }
    .header-left .themes-link,
    .header-left .surah-meta { display: none; }
    .root-search-input { width: 150px; }
    .root-search-results { width: 280px; }
    .logo { font-size: 1.1rem; }
    #surah-select { min-width: 140px; font-size: 0.95rem; }
}

/* Tablet only: panel stays as full-width left slide */
@media (max-width: 900px) and (min-width: 769px) {
    :root { --panel-width: 100vw; }
    #root-panel { top: 0; height: 100vh; }
    .panel-open #main { margin-left: 0; }
}

/* ── Mobile: Bottom Drawer ─────────────────────── */
@media (max-width: 768px) {
    /* Header wraps to two rows on tablet/phone */
    #header {
        flex-wrap: wrap;
        height: auto;
        min-height: var(--header-h);
        gap: 4px;
        padding: 6px 12px;
    }
    .header-right { order: 1; }
    .header-center { order: 2; flex: 1 1 auto; justify-content: flex-end; }
    .header-left {
        order: 3;
        flex: 1 0 100%;
        justify-content: center;
        flex-wrap: wrap;
        gap: 6px;
        padding-top: 4px;
        border-top: 1px solid var(--border);
    }

    #root-panel {
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: 100vh;
        max-height: 100vh;
        border-right: none;
        border-top: none;
        border-radius: 0;
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 200;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    #root-panel.panel-visible {
        transform: translateY(0);
    }
    .panel-open #main {
        margin-left: 0;
    }

    /* Make panel close button prominent on mobile */
    .panel-close {
        width: 44px;
        height: 44px;
        font-size: 1.4rem;
        position: sticky;
        top: 0;
        z-index: 210;
    }

    /* Drawer drag handle */
    .drawer-handle {
        display: flex;
        justify-content: center;
        padding: 10px 0 4px;
        cursor: grab;
        touch-action: none;
    }
    .drawer-handle::after {
        content: '';
        width: 36px;
        height: 4px;
        background: var(--border);
        border-radius: 2px;
    }

    /* Panel header: not sticky inside bottom drawer */
    .panel-header {
        position: relative;
    }

    /* Show backdrop on mobile */
    .panel-backdrop {
        display: block;
    }

    /* Touch targets: larger tappable area for roots */
    .word-has-root {
        padding: 4px 2px;
        -webkit-tap-highlight-color: transparent;
    }

    /* Buttons: 44px minimum touch target */
    .nav-btn,
    #theme-toggle,
    #translation-toggle,
    #translit-toggle,
    #text-scale-btn,
    #root-search-btn {
        min-width: 44px;
        min-height: 44px;
    }

    #surah-select {
        min-width: 120px;
        font-size: 0.9rem;
    }

    /* Verse numbers: visual + padding for touch target */
    .verse-num {
        width: 28px;
        height: 28px;
        padding: 8px;
        box-sizing: content-box;
    }
}

/* Small phones */
@media (max-width: 500px) {
    #header {
        padding: 0 8px;
        flex-wrap: wrap;
        height: auto;
        min-height: var(--header-h);
        gap: 4px;
        padding-top: 6px;
        padding-bottom: 6px;
    }

    /* Logo row: logo + surah nav share full width */
    .header-right { order: 1; flex: 0 0 auto; }
    .header-center { order: 2; flex: 1 1 auto; justify-content: flex-end; }
    .header-left {
        order: 3;
        flex: 1 0 100%;
        justify-content: center;
        flex-wrap: wrap;
        gap: 6px;
        padding-top: 4px;
        border-top: 1px solid var(--border);
    }

    .logo { font-size: 1rem; }
    #surah-select { min-width: 100px; font-size: 0.85rem; padding: 4px 24px 4px 8px; }

    /* Compact toolbar buttons */
    .theme-toggle-btn,
    .trans-toggle-btn,
    .nav-btn,
    #root-search-btn {
        width: 38px;
        height: 38px;
        min-width: 38px;
        min-height: 38px;
        font-size: 0.95rem;
    }

    .root-search-input { width: 120px !important; font-size: 0.85rem; }
    .root-search-results { width: 90vw; left: 5vw; right: 5vw; }

    /* Verses */
    .verse-num { width: 24px; height: 24px; font-size: 0.6rem; }
    .verse-text { font-size: calc(1.2rem * var(--text-scale, 1)); line-height: 2.1; }

    /* Panel */
    .root-arabic { font-size: 2.2rem; }
    .tafsir-inner { padding: 12px 14px; }
    .tafsir-text { font-size: calc(1.05rem * var(--text-scale, 1)); line-height: 1.9; }

    /* Reader padding */
    #reader { padding: 12px 3% 60px; }
}

/* Very small phones (iPhone SE, etc.) */
@media (max-width: 380px) {
    .logo { font-size: 0.9rem; }
    #surah-select { min-width: 80px; font-size: 0.8rem; }
    .verse-text { font-size: calc(1.1rem * var(--text-scale, 1)); }
    .header-left { gap: 4px; }
    .theme-toggle-btn, .trans-toggle-btn, .nav-btn, #root-search-btn {
        width: 34px; height: 34px; min-width: 34px; min-height: 34px;
    }
}
