:root {
  --bg: #f4eee2;
  --bg-dots: #e6dcc8;
  --surface: #fffdf8;
  --surface-2: #f8f2e6;
  --ink: #2b2118;
  --muted: #6f6253;
  --line: #d6c8b0;
  --link: #8a6a1f;
  --gold: #a7832f;
  --gold-soft: #f1e5c4;
  --amber: #c27c0e;
  --b-aw: #2e7d74;
  --b-dd: #b06f17;
  --b-mh: #a64b36;
  --b-ed: #3f5a99;
  --b-root: #a7832f;
  --b-none: #8a7a68;
  --shadow: 0 10px 30px rgba(60, 40, 10, .14), 0 2px 6px rgba(60, 40, 10, .08);
  --font: 'Noto Naskh Arabic', 'Amiri', 'Segoe UI', Tahoma, sans-serif;
  --font-title: 'Amiri', 'Noto Naskh Arabic', serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15120e; --bg-dots: #241e17; --surface: #1f1a14; --surface-2: #28211a;
    --ink: #efe6d6; --muted: #ad9f8a; --line: #3d342a; --link: #e0bd6a;
    --gold: #d4ae55; --gold-soft: #3a2f18; --amber: #e8a33a;
    --b-aw: #4fb3a7; --b-dd: #e19a3b; --b-mh: #e07a62; --b-ed: #7d97d9; --b-root: #d4ae55; --b-none: #a8977f;
    --shadow: 0 10px 30px rgba(0,0,0,.5), 0 2px 6px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15120e; --bg-dots: #241e17; --surface: #1f1a14; --surface-2: #28211a;
  --ink: #efe6d6; --muted: #ad9f8a; --line: #3d342a; --link: #e0bd6a;
  --gold: #d4ae55; --gold-soft: #3a2f18; --amber: #e8a33a;
  --b-aw: #4fb3a7; --b-dd: #e19a3b; --b-mh: #e07a62; --b-ed: #7d97d9; --b-root: #d4ae55; --b-none: #a8977f;
  --shadow: 0 10px 30px rgba(0,0,0,.5), 0 2px 6px rgba(0,0,0,.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.7;
  overflow: hidden;
}
a { color: var(--link); }
button { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand h1 { font-family: var(--font-title); font-size: 26px; margin: 0; line-height: 1.2; color: var(--ink); }
.brand .sub { margin: 0; font-size: 13px; color: var(--muted); }
.brand .sep { opacity: .5; }
.search { position: relative; margin-inline-start: auto; width: min(340px, 34vw); }
.search input {
  width: 100%; height: 40px; padding: 0 40px 0 12px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--ink); font: inherit; font-size: 15px;
}
.search input:focus { outline: 2px solid var(--gold); outline-offset: 1px; }
.search-ico { position: absolute; right: 13px; top: 11px; width: 18px; height: 18px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; }
.results {
  position: absolute; top: 46px; inset-inline: 0; margin: 0; padding: 6px; list-style: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
  max-height: 60vh; overflow: auto;
}
.results li { padding: 6px 10px; border-radius: 10px; cursor: pointer; }
.results li small { display: block; color: var(--muted); font-size: 12px; line-height: 1.4; }
.results li[aria-selected="true"], .results li:hover { background: var(--gold-soft); }
.results .none { color: var(--muted); cursor: default; }
.actions { display: flex; gap: 6px; }
.actions button {
  height: 38px; padding: 0 14px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); font-size: 14px; white-space: nowrap;
}
.actions button:hover { border-color: var(--gold); }
.actions button.primary { background: var(--gold); border-color: var(--gold); color: #fff; }

/* ---------- stage ---------- */
#stage {
  position: fixed; inset: 0;
  background-color: var(--bg);
  background-image: radial-gradient(var(--bg-dots) 1.2px, transparent 1.3px);
  background-size: 26px 26px;
}
#tree { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#tree:active { cursor: grabbing; }

.link { fill: none; stroke: var(--line); stroke-width: 2px; }
.link.desc { stroke-dasharray: 5 6; }

.node { cursor: pointer; }
.node .ring { fill: var(--surface); stroke: var(--c); stroke-width: 3px; transition: stroke-width .15s; }
.node .disc { fill: color-mix(in srgb, var(--c) 16%, var(--surface)); }
.node .initial { font-family: var(--font-title); font-size: 24px; font-weight: 700; fill: var(--c); text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.node .halo { fill: none; stroke: var(--c); stroke-width: 2px; opacity: 0; }
.node:hover .ring, .node:focus-visible .ring { stroke-width: 5px; }
.node:focus { outline: none; }
.node.selected .halo { opacity: .45; animation: pulse 1.8s ease-out infinite; }
@keyframes pulse { 0% { transform: scale(1); opacity: .5 } 100% { transform: scale(1.45); opacity: 0 } }
.node .name { font-size: 14px; font-weight: 600; fill: var(--ink); text-anchor: middle; paint-order: stroke; stroke: var(--bg); stroke-width: 4px; stroke-linejoin: round; }
.node .laqab { font-size: 12px; fill: var(--muted); text-anchor: middle; paint-order: stroke; stroke: var(--bg); stroke-width: 4px; }
.node.root .ring { stroke-width: 4px; }
.node.root .initial { font-size: 30px; }
.node .qbadge circle { fill: var(--amber); stroke: var(--surface); stroke-width: 2px; }
.node .qbadge text { fill: #fff; font-size: 12px; font-weight: 700; text-anchor: middle; dominant-baseline: central; }
.node .fbadge circle { fill: var(--surface); stroke: var(--c); stroke-width: 1.5px; }
.node .fbadge text { fill: var(--c); font-size: 11px; font-weight: 700; text-anchor: middle; dominant-baseline: central; }

.toggle { cursor: pointer; }
.toggle rect { fill: var(--surface); stroke: var(--c); stroke-width: 1.5px; }
.toggle text { fill: var(--c); font-size: 12px; font-weight: 700; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.toggle:hover rect { fill: var(--c); }
.toggle:hover text { fill: #fff; }

.group rect.box { fill: var(--surface-2); stroke: var(--c); stroke-width: 1.5px; stroke-dasharray: 5 4; }
.group text.gname { font-size: 13px; fill: var(--muted); text-anchor: middle; dominant-baseline: central; font-weight: 600; }

/* ---------- legend & hint ---------- */
.legend {
  position: fixed; left: 16px; bottom: 16px; z-index: 5;
  background: color-mix(in srgb, var(--surface) 92%, transparent); border: 1px solid var(--line);
  border-radius: 14px; padding: 10px 14px; font-size: 13px; line-height: 1.9; box-shadow: var(--shadow);
}
.lg-title { font-weight: 700; margin-bottom: 2px; }
.lg-row { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.lg-row i { width: 14px; height: 14px; border-radius: 50%; border: 3px solid var(--c); background: var(--surface); display: inline-block; }
.lg-dash { width: 18px; border-top: 2px dashed var(--muted); display: inline-block; }
.lg-q { width: 16px; height: 16px; border-radius: 50%; background: var(--amber); color: #fff; font-size: 11px; display: inline-grid; place-items: center; font-weight: 700; }
.hint {
  position: fixed; right: 50%; transform: translateX(50%); bottom: 16px; z-index: 4;
  background: color-mix(in srgb, var(--surface) 92%, transparent); border: 1px solid var(--line);
  padding: 6px 16px; border-radius: 999px; font-size: 13px; color: var(--muted); box-shadow: var(--shadow);
  transition: opacity .4s;
}

/* ---------- tooltip ---------- */
.tip {
  position: fixed; z-index: 30; width: 300px; pointer-events: none;
  background: var(--surface); border: 1px solid var(--line); border-top: 4px solid var(--c, var(--gold));
  border-radius: 14px; padding: 12px 14px; box-shadow: var(--shadow); font-size: 14px; line-height: 1.75;
}
.tip .t-name { font-family: var(--font-title); font-size: 20px; font-weight: 700; line-height: 1.3; }
.tip .t-laqab { color: var(--muted); font-size: 13px; }
.tip .t-dates { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.tip .t-sum { margin: 6px 0 0; }
.tip .t-meta { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 1px 9px; border-radius: 999px; font-size: 12px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); }
.chip.warn { background: color-mix(in srgb, var(--amber) 14%, var(--surface)); border-color: color-mix(in srgb, var(--amber) 40%, var(--line)); color: var(--ink); }
.tip .t-cta { margin-top: 8px; font-size: 12px; color: var(--link); font-weight: 600; }

/* ---------- drawer ---------- */
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 40; width: min(520px, 100vw);
  background: var(--surface); border-right: 1px solid var(--line); box-shadow: var(--shadow);
  transform: translateX(-104%); transition: transform .28s ease; overflow-y: auto;
  padding: 22px 26px 40px;
}
.drawer.open { transform: none; }
.close {
  position: absolute; top: 12px; left: 12px; width: 36px; height: 36px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--surface); font-size: 22px; line-height: 1; cursor: pointer;
}
.d-head { display: flex; gap: 16px; align-items: center; margin: 8px 0 6px; padding-left: 40px; }
.d-avatar {
  flex: none; width: 84px; height: 84px; border-radius: 50%; display: grid; place-items: center;
  border: 4px solid var(--c); background: color-mix(in srgb, var(--c) 16%, var(--surface));
  font-family: var(--font-title); font-weight: 700; font-size: 40px; color: var(--c); overflow: hidden;
}
.d-avatar img { width: 100%; height: 100%; object-fit: cover; }
.d-name { font-family: var(--font-title); font-size: 28px; line-height: 1.25; margin: 0; }
.d-laqab { color: var(--muted); font-size: 15px; }
.d-branch { display: inline-block; margin-top: 4px; padding: 0 10px; border-radius: 999px; font-size: 12.5px; color: #fff; background: var(--c); }
.d-dates { margin: 10px 0 0; color: var(--muted); font-size: 14px; }
.d-lineage { margin: 12px 0 0; padding: 10px 12px; background: var(--surface-2); border-radius: 12px; font-size: 14.5px; line-height: 2; }
.d-lineage .lbl { color: var(--muted); font-size: 12.5px; display: block; line-height: 1.4; }
.d-lineage a { text-decoration: none; border-bottom: 1px dotted var(--link); cursor: pointer; }
.d-lineage .gap { color: var(--muted); font-style: italic; }
.notice {
  margin: 14px 0 0; padding: 10px 12px; border-radius: 12px; font-size: 14px;
  background: color-mix(in srgb, var(--amber) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--amber) 40%, var(--line));
}
.notice b { color: var(--amber); }
.drawer h3 {
  font-family: var(--font-title); font-size: 19px; margin: 22px 0 8px; padding-bottom: 4px;
  border-bottom: 1px solid var(--line); display: flex; align-items: baseline; gap: 8px;
}
.drawer h3 small { font-family: var(--font); font-size: 12px; color: var(--muted); font-weight: 400; }
.d-summary { margin: 0; font-size: 15.5px; }
blockquote.q {
  margin: 0 0 12px; padding: 10px 14px 8px; background: var(--surface-2);
  border-right: 4px solid var(--c); border-radius: 10px 4px 4px 10px; font-family: var(--font-title); font-size: 18px; line-height: 1.85;
}
blockquote.q footer { font-family: var(--font); font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-list li { display: flex; gap: 10px; align-items: baseline; padding: 6px 0; border-bottom: 1px dashed var(--line); font-size: 14.5px; }
.toc-list .pg { flex: none; min-width: 58px; font-weight: 700; color: var(--c); }
.toc-list .na { flex: none; font-size: 11.5px; color: var(--muted); margin-inline-start: auto; white-space: nowrap; }
.kids { display: flex; flex-wrap: wrap; gap: 6px; }
.kids button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 2px 12px; cursor: pointer; font-size: 14px; }
.kids button:hover { border-color: var(--c); }
.empty { color: var(--muted); font-size: 14px; margin: 0; }
.d-foot { margin-top: 26px; display: flex; gap: 8px; flex-wrap: wrap; }
.d-foot button { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 4px 14px; cursor: pointer; font-size: 13.5px; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(20, 14, 6, .45); display: grid; place-items: center; padding: 16px; }
.modal[hidden] { display: none; }
.modal-card {
  position: relative; width: min(680px, 100%); max-height: 90vh; overflow: auto;
  background: var(--surface); border-radius: 18px; padding: 26px 30px; box-shadow: var(--shadow);
}
.modal-card h2 { font-family: var(--font-title); font-size: 28px; margin: 0 0 8px; }
.modal-card h3 { font-family: var(--font-title); font-size: 20px; margin: 18px 0 4px; }
.modal-card ul { padding-inline-start: 20px; margin: 4px 0; }
.book { padding: 12px 16px; border-radius: 12px; background: var(--gold-soft); margin: 8px 0; }
.book-title { font-family: var(--font-title); font-size: 22px; font-weight: 700; }
.muted { color: var(--muted); font-size: 14px; }
.noscript { position: fixed; inset: 40% 0 auto; text-align: center; }

/* ---------- small screens ---------- */
@media (max-width: 820px) {
  .topbar { flex-wrap: wrap; padding: 8px 16px; gap: 8px; }
  .brand h1 { font-size: 21px; }
  .brand .sub { display: none; }
  .search { order: 3; width: 100%; margin: 0; }
  .actions { margin-inline-start: auto; }
  .actions button { height: 34px; padding: 0 10px; font-size: 13px; }
  #btn-collapse { display: none; }
  .legend { display: none; }
  .hint { display: none; }
  .drawer { width: 100vw; border-right: 0; padding: 18px 16px 40px; }
  .tip { display: none; }
}
