* { box-sizing: border-box; }

body {
  font-family: 'Scheherazade New', 'Amiri', 'Traditional Arabic', serif;
  font-size: 1.15rem;
  line-height: 2;
  direction: rtl;
  background: #faf9f7;
  color: #1a1a1a;
  margin: 0;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
header {
  background: #1a472a;
  color: white;
  padding: 1rem 1.5rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

h1 {
  margin: 0 0 0.5rem 0;
  font-size: 1.8rem;
  letter-spacing: 0.02em;
}

#search-bar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}

#search-input {
  flex: 1;
  min-width: 200px;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  font-family: inherit;
  direction: rtl;
  border: 1px solid #ccc;
  border-radius: 4px;
}

select {
  padding: 0.5rem 0.75rem;
  font-family: inherit;
  font-size: 0.95rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
  background: white;
}

button {
  padding: 0.5rem 1rem;
  font-family: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  background: #2d6a4f;
  color: white;
  border: none;
  border-radius: 4px;
  transition: background 0.15s;
}

button:hover { background: #1b4332; }

nav {
  display: flex;
  gap: 0.5rem;
}

.nav-btn {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.4);
  font-size: 0.9rem;
  padding: 0.3rem 0.9rem;
}

.nav-btn:hover,
.nav-btn.active {
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.8);
}

/* ── Main content ─────────────────────────────────────────────────────────── */
main {
  margin: 1.5rem auto;
  padding: 0 1.5rem;
}

.view.hidden { display: none; }

.loading {
  text-align: center;
  color: #888;
  padding: 3rem;
  font-size: 1rem;
}

.error-msg {
  background: #fee2e2;
  color: #991b1b;
  border: 1px solid #fca5a5;
  border-radius: 6px;
  padding: 1rem;
  margin: 1rem 0;
}

/* ── Books grid ───────────────────────────────────────────────────────────── */
.category-group { margin-bottom: 2rem; }

.category-title {
  font-size: 1.05rem;
  font-weight: bold;
  color: #2d6a4f;
  border-bottom: 2px solid #2d6a4f;
  padding-bottom: 0.25rem;
  margin-bottom: 0.75rem;
}

.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 0.65rem;
}

.book-card {
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 0.75rem 1rem;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.book-card:hover {
  border-color: #2d6a4f;
  box-shadow: 0 2px 6px rgba(45,106,79,0.12);
}

.book-name { font-weight: bold; line-height: 1.6; }

.book-meta {
  font-size: 0.82rem;
  color: #777;
  margin-top: 0.2rem;
  line-height: 1.5;
}

.book-count {
  display: inline-block;
  background: #f0fdf4;
  color: #166534;
  border: 1px solid #bbf7d0;
  border-radius: 10px;
  font-size: 0.75rem;
  padding: 0 0.5rem;
  margin-top: 0.3rem;
}

/* ── Search results ───────────────────────────────────────────────────────── */
.search-header {
  color: #555;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.result-item {
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 1rem;
  margin-bottom: 0.65rem;
  cursor: pointer;
  transition: border-color 0.15s;
}

.result-item:hover { border-color: #2d6a4f; }

.result-book {
  font-size: 0.82rem;
  color: #2d6a4f;
  font-weight: bold;
  margin-bottom: 0.3rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.result-text { line-height: 1.9; }

mark {
  background: #fff3cd;
  padding: 0 2px;
  border-radius: 2px;
}

.pagination {
  display: flex;
  gap: 1rem;
  justify-content: center;
  align-items: center;
  margin-top: 1.5rem;
  flex-wrap: wrap;
}

.pagination span { color: #666; font-size: 0.9rem; }

/* ── Hadith detail ────────────────────────────────────────────────────────── */
.back-btn {
  background: transparent;
  color: #2d6a4f;
  border: 1px solid #2d6a4f;
  font-size: 0.9rem;
  padding: 0.3rem 0.9rem;
  margin-bottom: 1rem;
}

.back-btn:hover { background: #f0fdf4; }

.hadith-detail {
  background: white;
  border-radius: 8px;
  padding: 1.5rem;
  border: 1px solid #e0e0e0;
}

.hadith-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1rem;
  border-bottom: 1px solid #eee;
  padding-bottom: 0.75rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.hadith-book { font-weight: bold; color: #2d6a4f; font-size: 1.05rem; }
.hadith-num { color: #999; font-size: 0.9rem; }

.hadith-text {
  font-size: 1.2rem;
  line-height: 2.2;
  margin: 1rem 0;
}

.hadith-meta {
  font-size: 0.85rem;
  color: #666;
  margin-top: 1rem;
  border-top: 1px solid #f0f0f0;
  padding-top: 0.75rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.hadith-meta span { display: inline-flex; align-items: center; gap: 0.25rem; }

/* hokm badges */
.hokm-badge {
  display: inline-block;
  padding: 0.15rem 0.6rem;
  border-radius: 12px;
  font-size: 0.8rem;
  font-weight: normal;
}

.hokm-0, .hokm-1 { background: #d1fae5; color: #065f46; }
.hokm-2, .hokm-3 { background: #fee2e2; color: #991b1b; }
.hokm-4, .hokm-5 { background: #f3f4f6; color: #374151; }
.hokm-unknown     { background: #f3f4f6; color: #9ca3af; }

/* ── Cluster occurrences ─────────────────────────────────────────────────── */
.cluster-section {
  margin-top: 1.5rem;
  border-top: 1px solid #eee;
  padding-top: 1rem;
}

.cluster-title {
  font-weight: bold;
  color: #555;
  margin-bottom: 0.75rem;
  font-size: 0.95rem;
}

.cluster-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.1s;
  font-size: 0.9rem;
  border-bottom: 1px solid #f5f5f5;
}

.cluster-item:hover { background: #f0fdf4; }

/* ── Sanad chain ──────────────────────────────────────────────────────────── */
.sanad-section {
  margin-top: 1.5rem;
  border-top: 1px solid #eee;
  padding-top: 1rem;
}

.sanad-title {
  font-weight: bold;
  margin-bottom: 0.75rem;
  color: #555;
  font-size: 0.95rem;
}

.sanad-chain {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
  margin-bottom: 0.5rem;
}

.narrator-chip {
  background: #f0fdf4;
  border: 1px solid #86efac;
  border-radius: 20px;
  padding: 0.2rem 0.75rem;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

.narrator-chip:hover { background: #dcfce7; }

.chain-arrow { color: #aaa; font-size: 0.75rem; user-select: none; }

/* ── Narrator detail ──────────────────────────────────────────────────────── */
.narrator-detail {
  background: white;
  border-radius: 8px;
  padding: 1.5rem;
  border: 1px solid #e0e0e0;
}

.narrator-name {
  font-size: 1.4rem;
  font-weight: bold;
  margin-bottom: 0.25rem;
  color: #1a1a1a;
}

.narrator-shohra {
  color: #555;
  font-size: 1rem;
  margin-bottom: 1rem;
}

.narrator-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.3rem 1.5rem;
  margin: 1rem 0;
  font-size: 0.95rem;
}

.narrator-grid dt { color: #777; white-space: nowrap; }
.narrator-grid dd { margin: 0; }

.opinions-section { margin-top: 1.5rem; border-top: 1px solid #eee; padding-top: 1rem; }
.opinions-title { font-weight: bold; color: #555; margin-bottom: 0.75rem; font-size: 0.95rem; }

.opinion-item {
  padding: 0.6rem 0.75rem;
  border-right: 3px solid #2d6a4f;
  margin-bottom: 0.5rem;
  background: #f9fafb;
  border-radius: 0 4px 4px 0;
}

.opinion-critic {
  font-size: 0.8rem;
  color: #888;
  margin-bottom: 0.2rem;
}

.opinion-text { line-height: 1.8; }

/* ── Hadith colored text (isnad = red, matn = green) ────────────────────────── */
.hadith-isnad {
  color: #7a1a1a;
}
.hadith-matn {
  color: #14532d;
}
.rawi-name {
  color: #b91c1c;
  cursor: pointer;
  border-bottom: 1px dotted #b91c1c;
  transition: color 0.1s;
}
.rawi-name:hover { color: #7f1d1d; }
.sahabi-name { font-weight: bold; }
.quran-cite {
  color: #5b21b6;
  font-style: italic;
}

/* ── Morphological search banner ─────────────────────────────────────────────── */
.morph-banner {
  background: #ecfdf5;
  border: 1px solid #6ee7b7;
  border-radius: 6px;
  padding: 0.45rem 0.9rem;
  font-size: 0.88rem;
  color: #065f46;
  margin-bottom: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.morph-root { font-weight: bold; font-size: 1rem; letter-spacing: 0.03em; }

/* ── Commentary accordion ────────────────────────────────────────────────────── */
.commentary-section {
  margin-top: 1.5rem;
  border-top: 1px solid #eee;
  padding-top: 1rem;
}
.commentary-section-title { font-weight: bold; color: #555; margin-bottom: 0.6rem; font-size: 0.95rem; }
.commentary-item { border: 1px solid #e8e8e8; border-radius: 6px; margin-bottom: 0.5rem; overflow: hidden; }
.commentary-toggle {
  width: 100%;
  text-align: right;
  background: #f9fafb;
  border: none;
  padding: 0.65rem 0.9rem;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.95rem;
  color: #1a1a1a;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background 0.1s;
}
.commentary-toggle:hover { background: #f0fdf4; }
.commentary-toggle .arrow { font-size: 0.75rem; color: #aaa; transition: transform 0.2s; }
.commentary-toggle.open .arrow { transform: rotate(90deg); }
.commentary-body { display: none; padding: 0.9rem 1rem; line-height: 2.1; font-size: 1.05rem; border-top: 1px solid #eee; white-space: pre-wrap; }
.commentary-body.open { display: block; }

/* ── Hokm text (sanad detail) ────────────────────────────────────────────────── */
.hokm-text-box {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 4px;
  padding: 0.5rem 0.75rem;
  font-size: 0.88rem;
  line-height: 1.9;
  margin-top: 0.35rem;
  color: #78350f;
}

/* ── Topic tree ──────────────────────────────────────────────────────────────── */
.topics-breadcrumb {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
  align-items: center;
  font-size: 0.9rem;
  margin-bottom: 1rem;
  color: #555;
}
.topics-breadcrumb .crumb { color: #2d6a4f; cursor: pointer; text-decoration: underline; }
.topics-breadcrumb .sep { color: #bbb; }

.topic-node {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.9rem;
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  margin-bottom: 0.45rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.1s;
}
.topic-node:hover { border-color: #2d6a4f; background: #f0fdf4; }
.topic-node-text { flex: 1; line-height: 1.7; }
.topic-node-count { font-size: 0.8rem; color: #888; white-space: nowrap; }
.topic-node-arrow { color: #aaa; font-size: 0.85rem; }
.topic-node.leaf { border-color: #86efac; }
.topic-node.leaf:hover { border-color: #16a34a; }

/* ── Dictionary panel ────────────────────────────────────────────────────────── */
.dict-search-bar { display: flex; gap: 0.5rem; margin-bottom: 1.25rem; }
.dict-search-bar input {
  flex: 1;
  padding: 0.5rem 0.75rem;
  font-family: inherit;
  font-size: 1rem;
  direction: rtl;
  border: 1px solid #ccc;
  border-radius: 4px;
}
.dict-result { background: white; border: 1px solid #e0e0e0; border-radius: 8px; padding: 1.25rem 1.5rem; }
.dict-root { font-size: 1.6rem; font-weight: bold; color: #1a472a; margin-bottom: 0.5rem; letter-spacing: 0.04em; }
.dict-source-item { border-bottom: 1px solid #f0f0f0; padding: 0.6rem 0; }
.dict-source-item:last-child { border-bottom: none; }
.dict-source-name { font-size: 0.82rem; font-weight: bold; color: #2d6a4f; margin-bottom: 0.2rem; }
.dict-source-text { line-height: 2; }
.dict-forms { margin-top: 1rem; border-top: 1px solid #eee; padding-top: 0.75rem; }
.dict-forms-title { font-size: 0.85rem; color: #888; margin-bottom: 0.4rem; }
.dict-forms-list { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.dict-form-chip { background: #f0f9ff; border: 1px solid #bae6fd; border-radius: 12px; padding: 0.1rem 0.55rem; font-size: 0.85rem; color: #0369a1; }

/* ── Isnad tree visualization ────────────────────────────────────────────── */
.isnad-tree-wrap {
  margin-top: 1.5rem;
  border-top: 1px solid #eee;
  padding-top: 1rem;
}

#isnad-svg-wrap {
  overflow-x: auto;
}

.isnad-tree-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.isnad-tree-title {
  font-weight: bold;
  color: #555;
  font-size: 0.95rem;
}

.isnad-tree-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  font-size: 0.88rem;
}

.rotba-filter-toggle {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
  color: #444;
  user-select: none;
  font-family: inherit;
}

.rotba-filter-toggle input[type="checkbox"] {
  cursor: pointer;
  width: 14px;
  height: 14px;
  accent-color: #2d6a4f;
}

.isnad-tree-pagination {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: #666;
}

.isnad-pg-btn {
  background: transparent;
  color: #2d6a4f;
  border: 1px solid #2d6a4f;
  padding: 0.2rem 0.6rem;
  font-size: 0.82rem;
  font-family: inherit;
}
.isnad-pg-btn:hover:not(:disabled) { background: #f0fdf4; }
.isnad-pg-btn:disabled { opacity: 0.4; cursor: not-allowed; background: transparent; }

.isnad-chain-count { font-size: 0.85rem; color: #888; }

.isnad-svg { display: block; }
.isnad-svg g { cursor: pointer; }
.isnad-svg g:hover > rect { opacity: 0.85; }

/* Popup when a compiler leaf has multiple hadith versions */
.isnad-leaf-picker {
  position: fixed;
  z-index: 9999;
  background: #fff;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  min-width: 200px;
  max-width: 320px;
  overflow: hidden;
}
.isnad-leaf-picker-item {
  padding: 0.5rem 0.85rem;
  font-size: 0.9rem;
  cursor: pointer;
  direction: rtl;
  color: #1f2937;
  border-bottom: 1px solid #f3f4f6;
}
.isnad-leaf-picker-item:last-child { border-bottom: none; }
.isnad-leaf-picker-item:hover { background: #f0fdf4; }

.sanad-hokm-texts { margin-top: 0.75rem; }

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  header { padding: 0.75rem 1rem; }
  h1 { font-size: 1.4rem; }
  #search-input { min-width: 100%; }
  select { flex: 1; }
  .books-grid { grid-template-columns: 1fr; }
  nav { flex-wrap: wrap; }
}
