:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #1c2733;
  --muted: #67788a;
  --line: #9db0c2;
  --male: #2b6cb0;
  --male-bg: #e8f1fb;
  --female: #c05299;
  --female-bg: #fceef7;
  --accent: #2f855a;
  --radius: 10px;
  font-family: "Segoe UI", Tahoma, "Noto Naskh Arabic", Arial, sans-serif;
}
* { box-sizing: border-box; margin: 0; }
body { background: var(--bg); color: var(--ink); height: 100vh; overflow: hidden; }
.hidden { display: none !important; }

#topbar {
  position: fixed; top: 0; inset-inline: 0; height: 52px; z-index: 20;
  display: flex; align-items: center; gap: 8px; padding: 0 12px;
  background: var(--card); box-shadow: 0 1px 4px rgba(0,0,0,.12);
}
.brand { font-weight: 700; white-space: nowrap; }
#treeTitle { font-weight: 600; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spacer { flex: 1; }
#searchBox { width: 200px; padding: 7px 10px; border: 1px solid #cfd8e3; border-radius: 8px; }
#searchResults {
  position: fixed; top: 50px; width: 260px; max-height: 300px; overflow: auto; z-index: 30;
  background: var(--card); border: 1px solid #cfd8e3; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.15);
}
#searchResults div { padding: 8px 12px; cursor: pointer; }
#searchResults div:hover { background: var(--male-bg); }

.btn {
  padding: 7px 14px; border: 1px solid #cfd8e3; background: var(--card); color: var(--ink);
  border-radius: 8px; cursor: pointer; font-size: 14px; white-space: nowrap;
}
.btn:hover { background: #eef2f6; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #276749; }
.btn.danger { color: #c53030; border-color: #e3b3b3; }
.btn.danger:hover { background: #fdeaea; }

#home { display: flex; align-items: center; justify-content: center; height: 100vh; padding-top: 52px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: 0 4px 24px rgba(0,0,0,.10); padding: 28px; }
.home-card { width: min(440px, 92vw); text-align: center; }
.home-card h1 { margin-bottom: 10px; }
.home-card p { color: var(--muted); margin-bottom: 18px; }
.home-card form { display: flex; gap: 8px; margin-bottom: 8px; }
.home-card input { flex: 1; padding: 9px 12px; border: 1px solid #cfd8e3; border-radius: 8px; }
.home-card hr { border: none; border-top: 1px solid #e3e9f0; margin: 18px 0; }

#canvasWrap { position: fixed; inset: 52px 0 0 0; }
#canvas { width: 100%; height: 100%; cursor: grab; touch-action: none; }
#canvas.dragging { cursor: grabbing; }

#zoomCtrls { position: absolute; bottom: 18px; inset-inline-start: 14px; display: flex; flex-direction: column; gap: 6px; }
#zoomCtrls .btn { width: 40px; font-size: 17px; }

#emptyTree { position: absolute; inset: 0; display: flex; flex-direction: column; gap: 14px; align-items: center; justify-content: center; color: var(--muted); }
#readOnlyBadge {
  position: absolute; top: 12px; inset-inline-start: 14px; background: #fffbe6; border: 1px solid #e6d78a;
  color: #7a6410; padding: 4px 12px; border-radius: 20px; font-size: 13px;
}

/* SVG node cards */
.node-card { cursor: pointer; }
.node-rect { fill: var(--card); stroke-width: 2; }
.node-rect.male { fill: var(--male-bg); stroke: var(--male); rx: 4; }
.node-rect.female { fill: var(--female-bg); stroke: var(--female); rx: 16; }
.node-rect.deceased { fill: #eceff2; stroke: #8a99a8; }
.node-rect.highlight { stroke: #d69e2e; stroke-width: 4; }
.node-name { font-size: 14px; font-weight: 600; fill: var(--ink); }
.node-years { font-size: 11px; fill: var(--muted); }
.dead-badge { font-size: 11px; fill: #6b7a89; }
.edge { stroke: var(--line); stroke-width: 1.6; fill: none; }
.family-node { fill: #fdf6e3; stroke: #b7791f; stroke-width: 2.5; }
.family-name { font-size: 18px; font-weight: 700; fill: #7a5a10; }
.marriage-dot { fill: var(--card); stroke: var(--line); stroke-width: 1.6; cursor: pointer; }
.collapse-btn circle { fill: var(--card); stroke: var(--line); stroke-width: 1.4; cursor: pointer; }
.collapse-btn text { font-size: 13px; fill: var(--muted); cursor: pointer; user-select: none; }

/* Side panel */
#panel {
  position: fixed; top: 52px; bottom: 0; inset-inline-end: 0; width: min(320px, 90vw); z-index: 25;
  background: var(--card); box-shadow: 0 0 24px rgba(0,0,0,.18); padding: 20px; overflow: auto;
}
#panel .close { position: absolute; top: 10px; inset-inline-start: 10px; border: none; background: none; font-size: 17px; cursor: pointer; color: var(--muted); }
#panelPhoto { text-align: center; margin-bottom: 10px; }
#panelPhoto img { width: 96px; height: 96px; object-fit: cover; border-radius: 50%; border: 3px solid #dde5ee; }
#panelPhoto .placeholder { font-size: 52px; }
#panel h2 { text-align: center; margin-bottom: 4px; }
#panelMeta { text-align: center; color: var(--muted); font-size: 14px; margin-bottom: 10px; }
#panelNotes { font-size: 14px; color: var(--ink); background: #f6f8fa; border-radius: 8px; padding: 10px; margin-bottom: 14px; white-space: pre-wrap; }
#panelNotes:empty { display: none; }
#panelActions { display: flex; flex-direction: column; gap: 8px; }
#panelActions .grp { font-size: 12px; color: var(--muted); margin-top: 8px; }

.rel-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 6px; }
.rel-label { font-size: 12px; color: var(--muted); min-width: 52px; }
.rel-chip {
  border: 1px solid #cfd8e3; border-radius: 14px; padding: 3px 10px; font-size: 13px;
  cursor: pointer; background: #f6f8fa; color: var(--ink);
}
.rel-chip.male { background: var(--male-bg); border-color: var(--male); }
.rel-chip.female { background: var(--female-bg); border-color: var(--female); }
.rel-chip:hover { filter: brightness(0.95); }
.kid-row { display: flex; gap: 8px; margin-bottom: 8px; }
.kid-row .k-name { flex: 1; padding: 8px 10px; border: 1px solid #cfd8e3; border-radius: 8px; }
.kid-row .k-g { min-width: 92px; border: 2px solid #cfd8e3; border-radius: 8px; background: var(--card); cursor: pointer; font-size: 13px; }
.kid-row .k-g.male-on { border-color: var(--male); background: var(--male-bg); }
.kid-row .k-g.female-on { border-color: var(--female); background: var(--female-bg); }
#modal label.chk { display: flex; align-items: center; gap: 8px; color: var(--ink); font-size: 14px; margin-top: 12px; }

/* Modal */
#modalBack { position: fixed; inset: 0; background: rgba(20,30,40,.45); z-index: 40; display: flex; align-items: center; justify-content: center; }
#modal { width: min(420px, 94vw); max-height: 90vh; overflow: auto; }
#modal h3 { margin-bottom: 14px; }
#modal label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
#modal input[type=text], #modal input[type=number], #modal textarea, #modal select {
  width: 100%; padding: 8px 10px; border: 1px solid #cfd8e3; border-radius: 8px; font-family: inherit;
}
#modal textarea { min-height: 70px; }
#modal .row { display: flex; gap: 10px; }
#modal .row > div { flex: 1; }
#modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
#modal .gender-pick { display: flex; gap: 8px; }
#modal .gender-pick label {
  flex: 1; text-align: center; border: 2px solid #cfd8e3; border-radius: 8px; padding: 8px; cursor: pointer; margin: 0; font-size: 14px; color: var(--ink);
}
#modal .gender-pick input { display: none; }
#modal .gender-pick label.male-on { border-color: var(--male); background: var(--male-bg); }
#modal .gender-pick label.female-on { border-color: var(--female); background: var(--female-bg); }
.share-line { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.share-line input { flex: 1; font-size: 12px; direction: ltr; }

.btn .ico { display: none; }

@media (max-width: 760px) {
  #topbar { gap: 5px; padding: 0 8px; }
  .brand .lbl { display: none; }
  #treeTitle { font-size: 13px; max-width: 26vw; }
  #searchBox { flex: 1; width: auto; min-width: 60px; }
  .btn { padding: 8px 10px; }
  .btn .lbl { display: none; }
  .btn .ico { display: inline; }
  #panel { width: 100vw; }
  #zoomCtrls .btn { width: 46px; height: 42px; }
  #searchResults { width: calc(100vw - 16px); left: 8px !important; }
}

#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: #1c2733; color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 14px;
}
