@import url("https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@500;600;700&family=Inter:wght@400;500;600&family=Noto+Naskh+Arabic:wght@400;600&display=swap");

:root {
  --bg: #f6f1e7;
  --paper: #fffdf8;
  --ink: #1e2621;
  --muted: #6c6f68;
  --line: #e2d9c6;
  --green: #1f5a43;
  --green-2: #2f7a5c;
  --green-soft: #e1efe6;
  --gold: #b8892b;
  --gold-soft: #f6ead0;
  --danger: #a23b2a;
  --shadow: 0 1px 2px rgba(30, 38, 33, .06), 0 4px 14px rgba(30, 38, 33, .06);
  --radius: 12px;
  --serif: "Cormorant Garamond", "Iowan Old Style", Georgia, serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --arabic: "Noto Naskh Arabic", "Geeza Pro", serif;
  --header-h: 64px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.45 var(--sans); color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--green); }
.ar { font-family: var(--arabic); direction: rtl; unicode-bidi: isolate; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* ---------- Header ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--header-h); z-index: 30;
  display: flex; align-items: center; gap: 18px; padding: 0 18px;
  background: var(--paper); border-bottom: 1px solid var(--line);
}
.brand { display: flex; flex-direction: column; line-height: 1.05; min-width: 0; text-decoration: none; color: inherit; }
.brand b { font: 700 24px/1 var(--serif); letter-spacing: .2px; white-space: nowrap; }
.brand .ar { font-size: 12px; color: var(--muted); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; direction: rtl; text-align: left; }
.nav { display: flex; gap: 4px; }
.nav a {
  padding: 7px 12px; border-radius: 8px; text-decoration: none; color: var(--muted); font-weight: 500; position: relative;
}
.nav a:hover { background: var(--bg); color: var(--ink); }
.nav a.active { background: var(--green-soft); color: var(--green); }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 4px; border-radius: 9px; background: var(--gold); color: #fff; font-size: 11px; line-height: 18px; text-align: center; }
.search { margin-left: auto; position: relative; width: min(320px, 34vw); }
.search input {
  width: 100%; padding: 9px 12px 9px 34px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
.search input:focus { outline: 2px solid var(--green-soft); border-color: var(--green-2); background: #fff; }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0; max-height: 60vh; overflow: auto;
  background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); z-index: 40;
}
.results button { display: flex; gap: 10px; align-items: center; width: 100%; padding: 8px 10px; border: 0; background: none; text-align: left; cursor: pointer; }
.results button:hover, .results button.sel { background: var(--bg); }
.results small { color: var(--muted); display: block; font-size: 12px; }
.icon-btn { border: 1px solid var(--line); background: var(--paper); border-radius: 9px; padding: 7px 10px; cursor: pointer; }
.icon-btn:hover { background: var(--bg); }

main { position: fixed; inset: var(--header-h) 0 0 0; }
.view { position: absolute; inset: 0; overflow: auto; }

/* ---------- Tree ---------- */
#view-tree { overflow: hidden; background:
  radial-gradient(circle at 1px 1px, rgba(30,38,33,.07) 1px, transparent 0) 0 0 / 22px 22px, var(--bg); touch-action: none; cursor: grab; }
#view-tree.dragging { cursor: grabbing; }
#canvas { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
#canvas.animate { transition: transform .45s cubic-bezier(.2,.7,.2,1); }
#links { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
#links path { fill: none; stroke: #b9ad94; stroke-width: 1.6; }
#links path.anc { stroke-dasharray: 4 4; }
.col-label { position: absolute; font: 600 13px var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--muted); white-space: nowrap; }
.col-label small { display: block; font: 400 12px var(--sans); letter-spacing: 0; text-transform: none; }

.card {
  position: absolute; width: 272px; padding: 6px; background: var(--paper);
  border: 1px solid var(--line); border-left: 4px solid var(--branch, var(--line)); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card.founder { border: 1.5px solid var(--gold); border-left-width: 4px; background: #fffaf0; }
.card.ancestor { opacity: .92; border-style: dashed; border-left-style: solid; }
.row {
  display: flex; align-items: center; gap: 8px; height: 40px; padding: 0 6px; border-radius: 8px; cursor: pointer; position: relative;
}
.row + .row { height: 32px; }
.row:hover { background: var(--bg); }
.row.jamea { background: var(--green-soft); }
.row.jamea:hover { background: #d3e8da; }
.row.selected { outline: 2px solid var(--gold); outline-offset: -2px; }
.row .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; font-size: 14px; }
.row.sp .nm { font-weight: 400; font-size: 13px; color: #3b433d; }
.row .plus { color: var(--muted); font-size: 12px; width: 12px; text-align: center; margin-left: 4px; }
.row .late { font-size: 10px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); display: block; font-weight: 500; line-height: 1; margin-bottom: 1px; }
.star { color: var(--gold); font-size: 13px; }
.av {
  flex: none; width: 30px; height: 30px; border-radius: 50%; background: #e8e1d2; color: #5d5a50;
  display: grid; place-items: center; font: 600 12px var(--sans); overflow: hidden;
}
.row.sp .av { width: 24px; height: 24px; font-size: 10px; }
.av.F { background: #efe0e3; color: #7b4c55; }
.av.M { background: #dfe7ef; color: #47596b; }
.av img { width: 100%; height: 100%; object-fit: cover; }
.toggle {
  position: absolute; right: -13px; top: 50%; transform: translateY(-50%); width: 26px; height: 26px; border-radius: 50%;
  border: 1px solid var(--line); background: #fff; font-size: 11px; font-weight: 600; color: var(--muted); cursor: pointer; padding: 0;
  box-shadow: var(--shadow); z-index: 2;
}
.toggle:hover { color: var(--green); border-color: var(--green-2); }
.dup-note { font-size: 11px; color: var(--muted); padding: 0 8px 2px; }

.toolbar {
  position: absolute; left: 14px; top: 14px; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; z-index: 5;
  background: rgba(255,253,248,.92); backdrop-filter: blur(6px); border: 1px solid var(--line); border-radius: 12px; padding: 6px;
  max-width: calc(100% - 28px);
}
.toolbar .sep { width: 1px; align-self: stretch; background: var(--line); margin: 0 2px; }
.chip {
  border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 4px 10px; font-size: 13px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.chip i { width: 9px; height: 9px; border-radius: 50%; background: var(--c); display: inline-block; }
.chip:hover { border-color: var(--green-2); }
.legend { position: absolute; left: 14px; bottom: 14px; z-index: 5; display: flex; gap: 14px; font-size: 12.5px; color: var(--muted);
  background: rgba(255,253,248,.92); border: 1px solid var(--line); border-radius: 10px; padding: 7px 12px; flex-wrap: wrap; }
.legend span { display: inline-flex; gap: 6px; align-items: center; }
.legend .sw { width: 14px; height: 14px; border-radius: 4px; background: var(--green-soft); border: 1px solid #b9d8c4; }

/* ---------- Panels & pages ---------- */
.page { max-width: 1100px; margin: 0 auto; padding: 28px 20px 80px; }
.page h1 { font: 700 34px/1.1 var(--serif); margin: 0 0 4px; }
.page .lead { color: var(--muted); margin: 0 0 22px; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.filters input, .filters select, .field input, .field select, .field textarea {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; background: #fff;
}
.filters input { flex: 1; min-width: 200px; }
.list { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list .item { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.list .item:last-child { border-bottom: 0; }
.list .item:hover { background: var(--bg); }
.list .item .av { width: 38px; height: 38px; }
.list .grow { flex: 1; min-width: 0; }
.list .meta { font-size: 13px; color: var(--muted); }
.list .ar { color: var(--muted); font-size: 15px; }
.branch-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; background: var(--c); margin-right: 6px; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px; border-radius: 9px; border: 1px solid var(--line); background: #fff; cursor: pointer; font-weight: 500; text-decoration: none; color: var(--ink); }
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-2); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: .6; cursor: default; }

/* Person drawer */
#panel {
  position: fixed; top: var(--header-h); right: 0; bottom: 0; width: min(420px, 100vw); z-index: 25;
  background: var(--paper); border-left: 1px solid var(--line); box-shadow: -8px 0 30px rgba(30,38,33,.08);
  transform: translateX(105%); transition: transform .25s ease; overflow: auto;
}
#panel.open { transform: none; }
.p-head { padding: 22px 22px 14px; border-bottom: 1px solid var(--line); position: relative; }
.p-close { position: absolute; right: 12px; top: 12px; }
.p-photo { width: 96px; height: 96px; border-radius: 50%; font-size: 30px; margin-bottom: 12px; }
.p-photo.av { width: 96px; height: 96px; }
.p-name { font: 700 28px/1.1 var(--serif); margin: 0; }
.p-head .ar { font-size: 20px; color: #4b524c; margin-top: 4px; display: block; text-align: left; }
.tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.tag { font-size: 12px; padding: 3px 9px; border-radius: 999px; background: var(--bg); border: 1px solid var(--line); }
.tag.green { background: var(--green-soft); border-color: #b9d8c4; color: var(--green); }
.tag.gold { background: var(--gold-soft); border-color: #e6cf9e; color: #7d5c17; }
.p-sec { padding: 14px 22px; border-bottom: 1px solid var(--line); }
.p-sec h3 { font: 600 12px var(--sans); text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 8px; }
.rel { display: flex; align-items: center; gap: 10px; padding: 6px; margin: 0 -6px; border-radius: 8px; cursor: pointer; }
.rel:hover { background: var(--bg); }
.rel .av { width: 30px; height: 30px; }
.rel small { color: var(--muted); display: block; font-size: 12px; }
.callout { background: var(--gold-soft); border: 1px solid #e6cf9e; border-radius: 10px; padding: 10px 12px; font-size: 13.5px; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .btn { justify-content: center; }
.strip { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.strip img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; cursor: pointer; }
dl.facts { display: grid; grid-template-columns: 90px 1fr; gap: 4px 10px; margin: 0; font-size: 14px; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; white-space: pre-wrap; }

/* Modal */
.modal-bg { position: fixed; inset: 0; background: rgba(20,26,22,.45); z-index: 50; display: grid; place-items: center; padding: 16px; }
.modal { width: min(520px, 100%); max-height: calc(100vh - 32px); overflow: auto; background: var(--paper); border-radius: 16px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal header { padding: 18px 22px 6px; }
.modal h2 { font: 700 26px/1.15 var(--serif); margin: 0; }
.modal .sub { color: var(--muted); font-size: 14px; margin: 4px 0 0; }
.modal form { padding: 10px 22px 20px; display: grid; gap: 12px; }
.field { display: grid; gap: 5px; }
.field label { font-size: 13px; font-weight: 600; color: #3b433d; }
.field .hint { font-size: 12px; color: var(--muted); }
.field input[dir=rtl] { font-family: var(--arabic); font-size: 17px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.checks { display: flex; gap: 16px; flex-wrap: wrap; font-size: 14px; }
.checks label { display: inline-flex; gap: 6px; align-items: center; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; padding-top: 6px; }
.preview { width: 100%; max-height: 260px; object-fit: contain; border-radius: 10px; background: var(--bg); }
hr.soft { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

/* Photos */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.ph { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; cursor: pointer; }
.ph img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; }
.ph div { padding: 8px 10px; font-size: 13px; }
.ph small { color: var(--muted); display: block; }
.empty { text-align: center; padding: 50px 20px; color: var(--muted); background: var(--paper); border: 1px dashed var(--line); border-radius: 14px; }
.lightbox { position: fixed; inset: 0; background: rgba(10,12,11,.88); z-index: 60; display: grid; place-items: center; padding: 20px; }
.lightbox img { max-width: 100%; max-height: calc(100vh - 140px); border-radius: 8px; }
.lightbox .cap { color: #eee; text-align: center; margin-top: 12px; font-size: 14px; }
.lightbox .bar { display: flex; gap: 8px; justify-content: center; margin-top: 10px; }

/* Admin */
.tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; align-items: center; }
.change { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; margin-bottom: 12px; }
.change .top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.change h4 { margin: 0; font-size: 15px; }
.change table { border-collapse: collapse; width: 100%; margin: 10px 0; font-size: 14px; }
.change td { padding: 5px 8px; border-top: 1px solid var(--line); vertical-align: top; }
.change td:first-child { color: var(--muted); width: 130px; }
.change del { color: var(--danger); }
.change ins { color: var(--green); text-decoration: none; font-weight: 600; }
.change img { max-width: 220px; max-height: 180px; border-radius: 8px; display: block; margin: 8px 0; }
.change .btns { display: flex; gap: 8px; }

/* Toast */
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(calc(100% + 40px)); visibility: hidden; background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 10px; z-index: 70; transition: transform .25s; max-width: calc(100vw - 32px); font-size: 14px; }
#toast.show { transform: translateX(-50%); visibility: visible; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(circle at 1px 1px, rgba(30,38,33,.07) 1px, transparent 0) 0 0 / 22px 22px, var(--bg); }
.login { width: min(400px, 100%); background: var(--paper); border: 1px solid var(--line); border-radius: 18px; padding: 34px 30px; box-shadow: var(--shadow); text-align: center; }
.login .ar { font-size: 19px; color: var(--muted); display: block; margin-bottom: 6px; }
.login h1 { font: 700 38px/1.05 var(--serif); margin: 0 0 6px; }
.login p { color: var(--muted); margin: 0 0 22px; }
.login input { width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; font-size: 16px; margin-bottom: 12px; background: #fff; }
.login .btn { width: 100%; justify-content: center; padding: 12px; font-size: 16px; }
.login .err { color: var(--danger); font-size: 14px; min-height: 20px; margin-top: 10px; }
.ornament { color: var(--gold); letter-spacing: 6px; margin-bottom: 10px; }

@media (max-width: 760px) {
  :root { --header-h: 108px; }
  .topbar { flex-wrap: wrap; height: auto; min-height: var(--header-h); padding: 8px 12px; gap: 6px 10px; align-content: center; }
  .brand b { font-size: 21px; }
  .brand .ar { display: none; }
  .search { order: 3; width: 100%; margin: 0; }
  .nav { margin-left: auto; }
  .nav a { padding: 6px 8px; font-size: 14px; }
  .topbar .icon-btn { display: none; }
  .legend { display: none; }
  .toolbar .chip.br span { display: none; }
  .toolbar { flex-wrap: nowrap; overflow-x: auto; left: 8px; right: 8px; top: 8px; max-width: none; scrollbar-width: none; }
  .toolbar .chip { flex: none; }
  .two { grid-template-columns: 1fr; }
  .page h1 { font-size: 28px; }
}
