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

:root {
  --bg-base:      #0d0f1a;
  --bg-card:      #161826;
  --bg-panel:     #12141f;
  --bg-toolbar:   rgba(18,20,31,0.96);
  --border:       rgba(255,255,255,0.08);
  --text-primary: #e8eaf6;
  --text-secondary:#a8afc8;
  --text-muted:   #5a6080;
  --accent-1:     #6370ff;
  --accent-2:     #00c9a7;
  --status-down:  #ff4f6b;
  --family-node:  #2a2d45;
  --edge-color:   rgba(99,112,255,0.35);
  --edge-hover:   rgba(99,112,255,0.7);
  --toolbar-h:    52px;
  --panel-w:      340px;
  --card-w:       160px;
  --card-h:       88px;
  --radius:       12px;
}

html, body { height: 100%; background: var(--bg-base); color: var(--text-primary); font-family: 'Inter', sans-serif; overflow: hidden; }

/* ── Toolbar ──────────────────────────────────────────────────────────────── */
.tree-toolbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  height: var(--toolbar-h);
  background: var(--bg-toolbar);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  display: flex; align-items: center; gap: 12px; padding: 0 16px;
}

.toolbar-left  { display: flex; align-items: center; gap: 12px; min-width: 200px; }
.toolbar-center{ flex: 1; display: flex; justify-content: center; }
.toolbar-right { display: flex; align-items: center; gap: 6px; }

.toolbar-home {
  width: 32px; height: 32px; border-radius: 8px;
  background: var(--accent-1); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; text-decoration: none; transition: opacity .2s;
}
.toolbar-home:hover { opacity: .8; }

.toolbar-title { font-size: 0.9rem; font-weight: 600; color: var(--text-primary); white-space: nowrap; }

.tb-btn {
  padding: 5px 12px; border-radius: 6px; border: 1px solid var(--border);
  background: rgba(255,255,255,0.04); color: var(--text-secondary);
  font-size: 0.78rem; font-weight: 500; cursor: pointer; transition: all .15s;
  white-space: nowrap;
}
.tb-btn:hover  { background: rgba(255,255,255,0.09); color: var(--text-primary); border-color: rgba(255,255,255,.15); }
.tb-btn.active { background: rgba(99,112,255,.2); color: var(--accent-1); border-color: var(--accent-1); }
.tb-btn.success{ background: rgba(0,201,167,.15); color: var(--accent-2); border-color: var(--accent-2); }
.tb-btn.danger { background: rgba(255,79,107,.15); color: var(--status-down); border-color: var(--status-down); }

.tb-divider { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }

/* Search */
.search-wrap { position: relative; }
.search-wrap input {
  width: 280px; padding: 6px 14px; border-radius: 8px;
  background: var(--bg-card); border: 1px solid var(--border);
  color: var(--text-primary); font-size: 0.85rem;
  outline: none; transition: border-color .2s;
}
.search-wrap input:focus { border-color: var(--accent-1); }

.search-results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0; z-index: 200;
  background: var(--bg-panel); border: 1px solid var(--border); border-radius: 10px;
  overflow: hidden; box-shadow: 0 8px 32px rgba(0,0,0,.5);
  max-height: 280px; overflow-y: auto;
}
.search-result-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; cursor: pointer; transition: background .15s;
}
.search-result-item:hover { background: rgba(255,255,255,.05); }
.sri-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.7rem; font-weight: 700; color: #fff; flex-shrink: 0;
}
.sri-info .sri-name  { font-size: 0.85rem; font-weight: 600; color: var(--text-primary); }
.sri-info .sri-dates { font-size: 0.72rem; color: var(--text-muted); }

/* ── Canvas ───────────────────────────────────────────────────────────────── */
.tree-canvas {
  position: fixed; top: var(--toolbar-h); left: 0; right: 0; bottom: 0;
  cursor: grab; overflow: hidden;
  background: var(--bg-base);
  background-image:
    radial-gradient(ellipse 80% 60% at 50% -10%, rgba(99,112,255,0.07) 0%, transparent 70%);
}
.tree-canvas.panning { cursor: grabbing; }

.tree-svg { width: 100%; height: 100%; }

/* Grid dots background */
.tree-canvas::before {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background-image:
    radial-gradient(circle, rgba(255,255,255,0.05) 1px, transparent 1px);
  background-size: 32px 32px;
}

.canvas-loading {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; color: var(--text-muted);
}
.loading-ring {
  width: 40px; height: 40px; border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent-1);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Person Card (SVG foreignObject fallback — use rects) ─────────────────── */
.person-card-rect {
  rx: 12; ry: 12;
  fill: var(--bg-card); stroke: var(--border); stroke-width: 1;
  cursor: pointer; transition: stroke .15s;
}
.person-card-rect.focus { stroke: var(--accent-1); stroke-width: 2; }
.person-card-rect:hover { stroke: rgba(255,255,255,.25); }

/* ── Right panel ──────────────────────────────────────────────────────────── */
.tree-panel {
  position: fixed; top: var(--toolbar-h); right: 0; bottom: 0;
  width: var(--panel-w);
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  overflow-y: auto; overflow-x: hidden;
  padding: 24px 20px 40px;
  z-index: 50;
  transition: transform .25s ease;
}
.tree-panel[hidden] { display: none !important; }

.panel-close {
  position: sticky; top: 0; float: right;
  background: transparent; border: none; color: var(--text-muted);
  font-size: 1rem; cursor: pointer; padding: 2px 6px;
}
.panel-close:hover { color: var(--text-primary); }

.panel-avatar {
  width: 72px; height: 72px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem; font-weight: 700; color: #fff;
  margin: 8px auto 12px;
}
.panel-name  { text-align: center; font-size: 1.15rem; font-weight: 700; }
.panel-dates { text-align: center; font-size: 0.8rem; color: var(--text-muted); margin-top: 4px; }
.panel-role  { text-align: center; font-size: 0.75rem; color: var(--text-secondary); margin-top: 4px; text-transform: capitalize; }

.panel-section { margin-top: 20px; }
.panel-section-title { font-size: 0.72rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .08em; margin-bottom: 10px; }

.panel-row { display: flex; gap: 8px; align-items: baseline; margin-bottom: 7px; font-size: 0.83rem; }
.pr-label  { color: var(--text-muted); min-width: 80px; flex-shrink: 0; }
.pr-value  { color: var(--text-primary); word-break: break-word; }

.panel-actions { display: flex; gap: 8px; margin-top: 20px; flex-wrap: wrap; }

/* Person chips in panel */
.person-chip {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: 8px;
  background: var(--bg-card); border: 1px solid var(--border);
  margin-bottom: 6px; cursor: pointer; transition: background .15s;
  font-size: 0.82rem; color: var(--text-primary);
}
.person-chip:hover { background: rgba(255,255,255,.07); }
.chip-avatar {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.65rem; font-weight: 700; color: #fff;
}
.chip-name { font-weight: 500; }
.chip-dates{ font-size: 0.72rem; color: var(--text-muted); }

/* Service account pills */
.service-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 10px; border-radius: 6px; background: var(--bg-card);
  border: 1px solid var(--border); margin-bottom: 5px; font-size: 0.8rem;
}
.service-row.disabled { opacity: .4; }
.service-name { color: var(--text-secondary); text-transform: capitalize; }
.service-user { color: var(--text-primary); font-weight: 500; }

/* ── Modals ───────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(0,0,0,.65); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
}
.modal-overlay[hidden] { display: none !important; }

.modal-box {
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 28px 28px 24px;
  width: min(460px, 94vw); max-height: 90vh; overflow-y: auto;
  position: relative;
}
.modal-box.modal-wide { width: min(760px, 96vw); }

.modal-x {
  position: absolute; top: 16px; right: 18px;
  background: none; border: none; color: var(--text-muted);
  font-size: 1rem; cursor: pointer;
}
.modal-x:hover { color: var(--text-primary); }

.modal-box h3 { font-size: 1.05rem; font-weight: 700; margin-bottom: 16px; }
.modal-box input, .modal-box select, .modal-box textarea {
  width: 100%; padding: 8px 12px; border-radius: 8px;
  background: var(--bg-card); border: 1px solid var(--border);
  color: var(--text-primary); font-size: 0.87rem; font-family: inherit;
  outline: none; transition: border-color .2s; margin-top: 4px;
}
.modal-box input:focus, .modal-box select:focus, .modal-box textarea:focus { border-color: var(--accent-1); }
.modal-box textarea { resize: vertical; }

.modal-error { color: var(--status-down); font-size: 0.82rem; margin-top: 10px; min-height: 18px; }

.modal-footer { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.modal-cancel { padding: 8px 18px; border-radius: 8px; background: transparent; border: 1px solid var(--border); color: var(--text-secondary); font-size: 0.87rem; cursor: pointer; }
.modal-cancel:hover { border-color: rgba(255,255,255,.2); color: var(--text-primary); }
.modal-submit { padding: 8px 22px; border-radius: 8px; background: var(--accent-1); border: none; color: #fff; font-size: 0.87rem; font-weight: 600; cursor: pointer; transition: opacity .2s; }
.modal-submit:hover { opacity: .85; }
.modal-submit.danger { background: var(--status-down); }
.modal-submit:disabled { opacity: .5; cursor: default; }

/* Edit grids */
.edit-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.edit-group { display: flex; flex-direction: column; gap: 4px; }
.edit-group.full { grid-column: span 2; }
.edit-group label { font-size: 0.77rem; color: var(--text-muted); font-weight: 500; }
.edit-section-header { font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--text-muted); border-bottom: 1px solid var(--border); padding-bottom: 4px; margin-top: 4px; }
.edit-hint { font-size: 0.78rem; color: var(--text-muted); }
.inline-check { display: flex; align-items: center; gap: 8px; font-size: 0.83rem; color: var(--text-secondary); cursor: pointer; }
.inline-check input { width: auto; accent-color: var(--accent-1); }
.inline-check-row { display: flex; gap: 20px; flex-wrap: wrap; }

/* Service checkboxes in person form */
.service-edit-row {
  display: grid; grid-template-columns: auto 1fr 1fr; align-items: center;
  gap: 8px 12px; padding: 8px 0;
  border-bottom: 1px solid var(--border); font-size: 0.83rem;
}
.service-edit-row:last-child { border-bottom: none; }
.service-edit-label { color: var(--text-secondary); text-transform: capitalize; font-weight: 500; }

/* ── SVG tree elements ────────────────────────────────────────────────────── */
.tree-edge { stroke: var(--edge-color); stroke-width: 1.5; fill: none; pointer-events: none; }
.tree-edge.thick { stroke-width: 2.5; stroke: rgba(99,112,255,.5); }

.family-diamond { fill: var(--family-node); stroke: rgba(99,112,255,.5); stroke-width: 1.5; cursor: pointer; transition: fill .15s; }
.family-diamond:hover { fill: rgba(99,112,255,.15); }
.family-diamond.selected { fill: rgba(99,112,255,.2); stroke: var(--accent-1); stroke-width: 2; }

/* ── Person card SVG styles (applied via DOM attributes) ─────────────────── */
/* Person cards are rendered as SVG foreignObject elements */
.tree-person-fo {
  overflow: visible;
}
.tree-person-card {
  width: 160px; height: 88px;
  background: var(--bg-card);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 10px 8px;
  cursor: pointer; transition: border-color .15s, background .15s;
  overflow: hidden; box-sizing: border-box; position: relative;
}
.tree-person-card:hover  { border-color: rgba(255,255,255,.22); background: rgba(255,255,255,.03); }
.tree-person-card.focus  { border-color: var(--accent-1); background: rgba(99,112,255,.08); }
.tree-person-card.ghost  { opacity: .5; }

.pc-avatar {
  width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.72rem; font-weight: 700; color: #fff; margin-bottom: 5px;
}
.pc-avatar .member-dot {
  position: absolute; bottom: 1px; right: 1px;
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent-2); border: 1.5px solid var(--bg-card);
}
.pc-name  { font-size: 0.75rem; font-weight: 600; color: var(--text-primary); text-align: center; line-height: 1.2; max-width: 144px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.pc-dates { font-size: 0.67rem; color: var(--text-muted); text-align: center; margin-top: 2px; }

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  :root { --panel-w: 100vw; --card-w: 130px; --card-h: 76px; }
  .toolbar-title  { display: none; }
  .search-wrap input { width: 160px; }
  .tb-btn[data-view] { display: none; }
}
