/* ═══════════════════════════════════════════════════════════════════════════
   FamilyRoots – Premium Stylesheet
   World-class family tree application
   ═══════════════════════════════════════════════════════════════════════════ */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap');

/* ── Design Tokens ─────────────────────────────────────────────────────────── */
:root {
    --ink:        #111827;
    --ink-2:      #374151;
    --muted:      #6b7280;
    --line:       #d1d5db;
    --paper:      #f9fafb;
    --white:      #ffffff;
    --accent:     #166534;
    --accent-2:   #15803d;
    --accent-glow: rgba(22, 101, 52, .18);
    --shadow-sm:  0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
    --shadow:     0 4px 24px rgba(17,24,39,.10), 0 2px 8px rgba(17,24,39,.06);
    --shadow-lg:  0 20px 60px rgba(17,24,39,.14), 0 8px 24px rgba(17,24,39,.08);
    --shadow-xl:  0 30px 80px rgba(17,24,39,.18);
    --radius:     12px;
    --radius-sm:  8px;
    --radius-pill: 999px;
    --transition: .18s cubic-bezier(.4,0,.2,1);

    /* Gender palette */
    --male-color:   #3b82f6;
    --male-bg:      #eff6ff;
    --female-color: #ec4899;
    --female-bg:    #fdf2f8;
    --other-color:  #8b5cf6;
    --other-bg:     #f5f3ff;

    /* Connector palette */
    --connector-color: #9ca3af;
    --connector-heart: #f43f5e;
}

/* ── Reset & Base ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

body {
    margin: 0;
    color: var(--ink);
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    background:
        radial-gradient(ellipse 80% 50% at 20% -10%, rgba(22,101,52,.07), transparent),
        radial-gradient(ellipse 60% 40% at 80% 110%, rgba(21,128,61,.05), transparent),
        linear-gradient(180deg, #f0fdf4 0%, #f9fafb 40%, #f3f4f6 100%);
    -webkit-font-smoothing: antialiased;
}

a { text-decoration: none; }

/* ── Layout Containers ─────────────────────────────────────────────────────── */
main.container-fluid {
    max-width: 1820px;
    margin: 0 auto;
}

/* ── Navigation ────────────────────────────────────────────────────────────── */
.app-navbar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: rgba(255,255,255,.88);
    backdrop-filter: blur(20px) saturate(1.6);
    -webkit-backdrop-filter: blur(20px) saturate(1.6);
    border-bottom: 1px solid rgba(0,0,0,.06);
    box-shadow: 0 1px 20px rgba(17,24,39,.06);
}

.app-navbar .container-fluid { gap: .75rem; }

.app-brand {
    display: inline-flex;
    align-items: center;
    gap: .65rem;
    color: var(--ink);
    font-weight: 900;
    font-size: 1.05rem;
    white-space: nowrap;
    letter-spacing: -.02em;
}

.brand-mark {
    display: inline-grid;
    place-items: center;
    width: 38px;
    height: 38px;
    color: #fff;
    background: linear-gradient(135deg, #166534 0%, #15803d 60%, #16a34a 100%);
    border-radius: 10px;
    font-size: .82rem;
    font-weight: 900;
    letter-spacing: 0;
    box-shadow: 0 4px 14px rgba(22,101,52,.35), inset 0 1px 0 rgba(255,255,255,.2);
}

.nav-link {
    color: var(--ink-2);
    font-weight: 600;
    font-size: .9rem;
    white-space: nowrap;
    padding: .4rem .65rem !important;
    border-radius: var(--radius-sm);
    transition: color var(--transition), background var(--transition);
}
.nav-link:hover {
    color: var(--accent);
    background: var(--accent-glow);
}

/* ── Page Header / Toolbar ─────────────────────────────────────────────────── */
.page-head,
.tree-toolbar {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
    margin-bottom: 1.5rem;
}

.toolbar-title { min-width: 220px; }

.eyebrow {
    margin: 0 0 .2rem;
    color: var(--accent);
    font-size: .75rem;
    font-weight: 700;
    letter-spacing: .07em;
    text-transform: uppercase;
}

h1 {
    margin: 0;
    font-size: clamp(1.6rem, 3vw, 2.4rem);
    font-weight: 900;
    letter-spacing: -.04em;
    line-height: 1.1;
    color: var(--ink);
}

h1 .title-suffix {
    color: var(--accent);
    font-weight: 400;
}

h2 {
    font-size: 1.15rem;
    font-weight: 800;
    letter-spacing: -.02em;
}

/* ── Button Row ────────────────────────────────────────────────────────────── */
.button-row {
    display: flex;
    flex-wrap: wrap;
    gap: .45rem;
    align-items: center;
}

.button-row .btn,
.button-row select,
.button-row .form-control { min-height: 40px; }

.inline-form {
    display: inline-flex;
    margin: 0;
    vertical-align: middle;
}

/* ── Buttons ───────────────────────────────────────────────────────────────── */
.btn {
    font-weight: 600;
    font-size: .875rem;
    border-radius: var(--radius-sm);
    transition: all var(--transition);
    display: inline-flex;
    align-items: center;
    gap: .35rem;
}

.btn-dark {
    background: #111827;
    border-color: #111827;
    color: #fff;
}
.btn-dark:hover {
    background: var(--accent);
    border-color: var(--accent);
    box-shadow: 0 4px 14px rgba(22,101,52,.3);
    transform: translateY(-1px);
}

.btn-outline-dark {
    color: var(--ink-2);
    border-color: #d1d5db;
    background: rgba(255,255,255,.8);
}
.btn-outline-dark:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: var(--accent-glow);
    transform: translateY(-1px);
}

.export-btn {
    padding-left: .75rem;
    padding-right: .75rem;
}

/* ── Tree Search ───────────────────────────────────────────────────────────── */
.tree-search-wrap {
    position: relative;
    flex: 0 0 200px;
}
.tree-search {
    width: 100%;
    border-radius: var(--radius-sm);
    border: 1.5px solid #d1d5db;
    padding: .4rem .75rem;
    font-size: .875rem;
    font-family: inherit;
    background: rgba(255,255,255,.9);
    transition: border-color var(--transition), box-shadow var(--transition);
}
.tree-search:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}
.search-results {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    z-index: 200;
    max-height: 260px;
    overflow-y: auto;
}
.search-result-item {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: .55rem .75rem;
    cursor: pointer;
    font-size: .875rem;
    font-weight: 600;
    transition: background var(--transition);
    border-bottom: 1px solid #f3f4f6;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: #f0fdf4; color: var(--accent); }
.search-result-item .result-gen {
    margin-left: auto;
    font-size: .75rem;
    color: var(--muted);
    font-weight: 500;
}
.search-result-item .result-dot {
    width: 10px; height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}
.result-dot.gender-male   { background: var(--male-color); }
.result-dot.gender-female { background: var(--female-color); }
.result-dot.gender-other  { background: var(--other-color); }

/* ── Tree Stage ────────────────────────────────────────────────────────────── */
.tree-stage {
    position: relative;
    min-height: 78vh;
    overflow: auto;
    padding: 1.25rem;
    background:
        radial-gradient(ellipse 60% 40% at 50% 0%, rgba(22,101,52,.06), transparent 60%),
        linear-gradient(160deg, #fafffe 0%, #f0f9f4 50%, #f5f7f5 100%);
    border: 1.5px solid rgba(22,101,52,.12);
    border-radius: 16px;
    box-shadow: inset 0 2px 20px rgba(22,101,52,.04), var(--shadow-sm);
    cursor: grab;
    user-select: none;
    scroll-behavior: smooth;
}
.tree-stage.is-dragging { cursor: grabbing; }

/* ── SVG Connector Overlay ─────────────────────────────────────────────────── */
.connector-svg {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 2;
    overflow: visible;
}

/* ── Zoom Controls ─────────────────────────────────────────────────────────── */
.zoom-controls {
    position: sticky;
    top: .75rem;
    z-index: 10;
    display: flex;
    gap: .3rem;
    margin-bottom: .75rem;
}

.zoom-btn {
    display: inline-grid;
    place-items: center;
    width: 36px; height: 36px;
    background: rgba(255,255,255,.92);
    border: 1.5px solid rgba(0,0,0,.1);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    transition: all var(--transition);
    color: var(--ink-2);
    font-family: inherit;
}
.zoom-btn:hover {
    background: #fff;
    border-color: var(--accent);
    color: var(--accent);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(22,101,52,.18);
}
.zoom-btn:active { transform: translateY(0); }

/* ── Tree Canvas ───────────────────────────────────────────────────────────── */
.tree-canvas {
    position: relative;
    width: max-content;
    min-width: 100%;
    min-height: 560px;
    padding: 2.5rem 3rem 3rem;
    transform-origin: top left;
    transition: transform .2s cubic-bezier(.4,0,.2,1);
    z-index: 1;
}

/* ── Tree Title ────────────────────────────────────────────────────────────── */
.tree-title {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: .3rem;
    margin-bottom: 2.5rem;
    text-align: center;
}
.tree-title strong {
    font-size: 1.5rem;
    font-weight: 900;
    letter-spacing: -.04em;
    color: var(--ink);
}
.tree-place { color: var(--muted); font-size: .9rem; font-weight: 500; }

.tree-view-badge {
    display: inline-flex;
    align-items: center;
    padding: .2rem .65rem;
    background: rgba(22,101,52,.1);
    color: var(--accent);
    border: 1px solid rgba(22,101,52,.2);
    border-radius: var(--radius-pill);
    font-size: .75rem;
    font-weight: 700;
    letter-spacing: .03em;
}
.tree-stats {
    color: var(--muted);
    font-size: .8rem;
    font-weight: 500;
}

/* ── Family Tree (Organic) ─────────────────────────────────────────────────── */
.family-tree,
.family-tree ul {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 2rem;
    padding: 2.5rem 0 0;
    margin: 0;
    list-style: none;
}

/* Vertical connector from parent down */
.family-tree ul::before {
    content: '';
    position: absolute;
    top: 0; left: 50%;
    width: 2px; height: 2.5rem;
    background: linear-gradient(to bottom, var(--connector-color), rgba(156,163,175,.3));
    border-radius: 1px;
}

/* Horizontal connector bar across siblings */
.family-tree ul::after {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 2px;
    background: var(--connector-color);
}

/* Clean up corners for single/first/last child */
.family-tree ul > li:first-child::after,
.family-tree ul > li:last-child::after {
    content: '';
    position: absolute;
    top: -2.5rem;
    height: 1px;
    background: var(--connector-color);
}
.family-tree ul > li:first-child::after { left: 0; width: 50%; }
.family-tree ul > li:last-child::after  { right: 0; width: 50%; }
.family-tree ul > li:only-child::after  { display: none; }

.family-tree li {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 2.5rem;
}

/* Vertical line from horizontal bar down to node */
.family-tree li::before {
    content: '';
    position: absolute;
    top: 0; left: 50%;
    width: 2px; height: 2.5rem;
    background: linear-gradient(to bottom, var(--connector-color), rgba(156,163,175,.3));
    border-radius: 1px;
}
.family-tree > li::before { display: none; }

/* ── Cluster & Multi-cluster ───────────────────────────────────────────────── */
.tree-cluster { margin-bottom: 3rem; }

.cluster-divider {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.5rem;
    color: var(--muted);
    font-size: .82rem;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
}
.cluster-divider::before,
.cluster-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: linear-gradient(to right, transparent, var(--line), transparent);
}

/* ── Couple Node ───────────────────────────────────────────────────────────── */
.couple-node {
    position: relative;
    display: flex;
    align-items: center;
    gap: 0;
}

/* ── Spouse Connector ──────────────────────────────────────────────────────── */
.spouse-connector {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    flex-shrink: 0;
    position: relative;
    z-index: 1;
}

.spouse-connector::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 0; right: 0;
    height: 2px;
    background: var(--connector-color);
    transform: translateY(-50%);
}

/* Heart icon for classic/royal themes — injected by JS as ::after on the span */
.theme-classic .spouse-connector::after,
.theme-royal .spouse-connector::after {
    content: '♥';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: .9rem;
    color: var(--connector-heart);
    background: #fff;
    padding: 0 .2rem;
    line-height: 1;
    animation: heartPulse 2.4s ease-in-out infinite;
    z-index: 2;
}

@keyframes heartPulse {
    0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
    50%       { transform: translate(-50%, -50%) scale(1.2); opacity: .85; }
}

/* Modern/nature: plain bar (connector::after not shown) */
.theme-modern .spouse-connector::after,
.theme-nature .spouse-connector::after {
    content: none;
}

/* ── Person Card ───────────────────────────────────────────────────────────── */
.person-card {
    display: grid;
    justify-items: center;
    align-content: start;
    gap: .4rem;
    width: 200px;
    min-height: 270px;
    padding: 1rem;
    color: var(--ink);
    background: var(--white);
    border: 1.5px solid #e5e7eb;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
    animation: cardReveal .5s ease both;
    position: relative;
    overflow: hidden;
}

/* Card entrance animation */
@keyframes cardReveal {
    from { opacity: 0; transform: translateY(16px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* Stagger by generation */
.family-tree li[data-depth="0"] .person-card { animation-delay: .05s; }
.family-tree li[data-depth="1"] .person-card { animation-delay: .12s; }
.family-tree li[data-depth="2"] .person-card { animation-delay: .2s; }
.family-tree li[data-depth="3"] .person-card { animation-delay: .28s; }
.family-tree li[data-depth="4"] .person-card { animation-delay: .36s; }

.person-card:hover {
    color: var(--ink);
    transform: translateY(-5px);
    box-shadow: var(--shadow-xl);
}

/* ── Gender-Coded Card Borders ─────────────────────────────────────────────── */
.person-card.gender-male {
    border-top: 4px solid var(--male-color);
}
.person-card.gender-male:hover {
    border-color: var(--male-color);
    box-shadow: 0 24px 60px rgba(59,130,246,.2), var(--shadow-lg);
}
.person-card.gender-male::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 60px;
    background: linear-gradient(to bottom, rgba(59,130,246,.04), transparent);
    pointer-events: none;
    border-radius: var(--radius) var(--radius) 0 0;
}

.person-card.gender-female {
    border-top: 4px solid var(--female-color);
}
.person-card.gender-female:hover {
    border-color: var(--female-color);
    box-shadow: 0 24px 60px rgba(236,72,153,.2), var(--shadow-lg);
}
.person-card.gender-female::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 60px;
    background: linear-gradient(to bottom, rgba(236,72,153,.04), transparent);
    pointer-events: none;
    border-radius: var(--radius) var(--radius) 0 0;
}

.person-card.gender-other {
    border-top: 4px solid var(--other-color);
}
.person-card.gender-other:hover {
    border-color: var(--other-color);
    box-shadow: 0 24px 60px rgba(139,92,246,.2), var(--shadow-lg);
}

/* ── Compact Mode ──────────────────────────────────────────────────────────── */
.tree-canvas.tree-compact .family-tree,
.tree-canvas.tree-compact .family-tree ul { gap: 1rem; }

.tree-canvas.tree-compact .person-card {
    width: 148px;
    min-height: 190px;
    padding: .7rem;
}
.tree-canvas.tree-compact .person-photo,
.tree-canvas.tree-compact .person-initial {
    width: 68px; height: 68px;
}
.tree-canvas.tree-compact .person-photo-wrap {
    width: 72px; height: 72px;
}
.tree-canvas.tree-compact .person-bio,
.tree-canvas.tree-compact .person-meta,
.tree-canvas.tree-compact .person-place,
.tree-canvas.tree-compact .person-reference-note { display: none; }
.tree-canvas.tree-compact .person-years { font-size: .8rem; }
.tree-canvas.tree-compact .person-name  { font-size: .95rem; }

/* ── Reference Card ────────────────────────────────────────────────────────── */
.person-card-reference {
    min-height: 160px;
    border-style: dashed;
    background: rgba(255,255,255,.7);
}

.person-link-reference {
    width: 175px;
    min-height: 100px;
    align-content: center;
    padding: .8rem;
    color: var(--accent);
    background: rgba(22,101,52,.06);
    border-style: dashed !important;
    border-color: rgba(22,101,52,.25) !important;
    box-shadow: none;
}
.person-link-reference:hover {
    color: var(--accent);
    background: rgba(22,101,52,.1);
    box-shadow: var(--shadow);
}
.person-link-reference .person-name { font-size: .95rem; }

/* Highlight effect when scrolling to linked person */
.person-card.is-linked-highlight {
    outline: 3px solid var(--accent);
    outline-offset: 4px;
    box-shadow: 0 0 0 6px var(--accent-glow), var(--shadow-lg);
}

/* Search highlight */
.person-card.search-highlight {
    outline: 3px solid #f59e0b;
    outline-offset: 4px;
    box-shadow: 0 0 0 6px rgba(245,158,11,.15), var(--shadow-xl);
    animation: searchPulse .5s ease;
}
@keyframes searchPulse {
    0%   { transform: translateY(-5px) scale(1.03); }
    100% { transform: translateY(-5px) scale(1); }
}

/* ── Photo Wrap & Gender Badge ─────────────────────────────────────────────── */
.person-photo-wrap {
    position: relative;
    display: grid;
    place-items: center;
    width: 96px; height: 96px;
    margin-bottom: .15rem;
}

.person-photo,
.person-initial {
    width: 92px; height: 92px;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid #fff;
    box-shadow: 0 4px 16px rgba(17,24,39,.18), 0 0 0 2px rgba(0,0,0,.06);
}

.person-initial {
    display: grid;
    place-items: center;
    color: #fff;
    font-weight: 800;
    font-size: 2.1rem;
    letter-spacing: -.03em;
}

.gender-male   .person-initial { background: linear-gradient(135deg, #3b82f6, #60a5fa); }
.gender-female .person-initial { background: linear-gradient(135deg, #ec4899, #f472b6); }
.gender-other  .person-initial { background: linear-gradient(135deg, #8b5cf6, #a78bfa); }

/* Gender badge */
.gender-badge {
    position: absolute;
    bottom: 2px; right: 2px;
    display: inline-grid;
    place-items: center;
    width: 22px; height: 22px;
    border-radius: 50%;
    font-size: .75rem;
    font-weight: 800;
    border: 2px solid #fff;
    box-shadow: var(--shadow-sm);
    line-height: 1;
}
.gender-badge-male   { background: var(--male-bg);   color: var(--male-color); }
.gender-badge-female { background: var(--female-bg);  color: var(--female-color); }
.gender-badge-other  { background: var(--other-bg);   color: var(--other-color); }

/* ── Card Text ─────────────────────────────────────────────────────────────── */
.person-name {
    max-width: 100%;
    overflow-wrap: anywhere;
    text-align: center;
    line-height: 1.2;
    font-size: 1rem;
    font-weight: 800;
    letter-spacing: -.02em;
    color: var(--ink);
}

.person-years {
    color: var(--muted);
    font-size: .8rem;
    font-weight: 600;
}

.person-reference-note {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: .15rem .5rem;
    background: rgba(22,101,52,.1);
    color: var(--accent);
    border: 1px solid rgba(22,101,52,.2);
    border-radius: var(--radius-pill);
    font-size: .7rem;
    font-weight: 700;
    text-align: center;
}

.person-link-line {
    color: var(--accent);
    font-size: .75rem;
    font-weight: 600;
}

.person-place {
    color: var(--ink-2);
    font-size: .78rem;
    font-weight: 500;
    text-align: center;
    line-height: 1.35;
}

.person-meta {
    padding: .18rem .5rem;
    color: var(--accent);
    background: rgba(22,101,52,.08);
    border-radius: var(--radius-pill);
    font-size: .72rem;
    font-weight: 700;
    text-align: center;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.person-bio {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    max-width: 100%;
    color: var(--muted);
    font-size: .75rem;
    text-align: center;
    line-height: 1.4;
}

/* ── Generation Grid Mode ──────────────────────────────────────────────────── */
.generation-grid {
    display: flex;
    flex-direction: column;
    gap: 0;
}

.generation-row {
    position: relative;
    display: flex;
    align-items: flex-start;
    gap: 0;
    padding-bottom: 3rem;
    min-height: 320px;
}

/* Vertical timeline line on left */
.generation-row::before {
    content: '';
    position: absolute;
    left: 55px;
    top: 0; bottom: 0;
    width: 2px;
    background: linear-gradient(to bottom, var(--connector-color), rgba(209,213,219,.2));
}

.generation-label {
    position: sticky;
    top: 4rem;
    flex-shrink: 0;
    width: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 32px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: #fff;
    font-size: .7rem;
    font-weight: 800;
    letter-spacing: .05em;
    text-transform: uppercase;
    border-radius: var(--radius-sm);
    box-shadow: 0 4px 12px rgba(22,101,52,.3);
    margin-right: 1.5rem;
    margin-top: .5rem;
    z-index: 3;
}

.generation-members {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    align-items: flex-start;
    padding-top: .5rem;
    flex: 1;
}

.grid-couple-node {
    position: relative;
    display: flex;
    align-items: center;
    gap: 0;
    animation: cardReveal .4s ease both;
}

/* Child connector dot on bottom */
.grid-couple-node.has-children::after {
    content: '';
    position: absolute;
    bottom: -1.5rem;
    left: 50%;
    transform: translateX(-50%);
    width: 2px;
    height: 1.5rem;
    background: var(--connector-color);
}

/* ── Ancestry Layout ───────────────────────────────────────────────────────── */
.tree-layout-ancestry .family-tree > li { gap: 1.5rem; }
.tree-layout-ancestry .ancestor-parents { padding-top: 0; }
.tree-layout-ancestry .ancestor-parents::before,
.tree-layout-ancestry .ancestor-parents::after { background: var(--line); }

/* ── Tree Themes ───────────────────────────────────────────────────────────── */

/* Classic – warm parchment */
.theme-classic {
    --connector-color: #c4a97d;
    --connector-heart: #c0392b;
    --line: #d4b896;
    background:
        url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23b5924c' fill-opacity='0.04'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"),
        linear-gradient(160deg, #fdfaf3 0%, #f9f2e4 50%, #f5ead5 100%);
}
.theme-classic .person-card {
    background: #fffdf7;
    border-color: #e8d5b0;
    box-shadow: 0 4px 24px rgba(180,140,60,.12), 0 2px 8px rgba(100,70,20,.08);
}
.theme-classic .person-card:hover {
    box-shadow: 0 20px 55px rgba(180,140,60,.2), 0 8px 20px rgba(100,70,20,.1);
}
.theme-classic .person-initial { background: linear-gradient(135deg, #92400e, #b45309); }
.theme-classic .tree-stage {
    background:
        radial-gradient(ellipse 60% 40% at 50% 0%, rgba(180,140,60,.08), transparent 60%),
        linear-gradient(160deg, #fdfcf7 0%, #faf6ec 50%, #f7f0e0 100%);
    border-color: rgba(180,140,60,.18);
}

/* Modern – clean crisp */
.theme-modern {
    --connector-color: #93c5fd;
    --line: #bfdbfe;
    background: linear-gradient(160deg, #f8fbff 0%, #f0f7ff 50%, #eef5ff 100%);
}
.theme-modern .person-card {
    background: #fafcff;
    border-color: #dbeafe;
    box-shadow: 0 4px 24px rgba(59,130,246,.1), 0 2px 8px rgba(37,99,235,.06);
}
.theme-modern .person-card:hover {
    box-shadow: 0 20px 55px rgba(59,130,246,.18), 0 8px 20px rgba(37,99,235,.1);
}
.theme-modern .person-initial { background: linear-gradient(135deg, #1d4ed8, #3b82f6); }
.theme-modern .tree-stage {
    background: linear-gradient(160deg, #f8fbff 0%, #f0f7ff 100%);
    border-color: rgba(59,130,246,.12);
}
.theme-modern .generation-label {
    background: linear-gradient(135deg, #1d4ed8, #3b82f6);
}

/* Royal – gold & burgundy */
.theme-royal {
    --connector-color: #d4a843;
    --connector-heart: #9b2335;
    --line: #e8c878;
    background:
        url("data:image/svg+xml,%3Csvg width='52' height='52' viewBox='0 0 52 52' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23b8860b' fill-opacity='0.04'%3E%3Cpath d='M10 10h6v6h-6zM36 10h6v6h-6zM10 36h6v6h-6zM36 36h6v6h-6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"),
        linear-gradient(160deg, #fffdf5 0%, #fdf8e8 50%, #faf0d5 100%);
}
.theme-royal .person-card {
    background: #fffef8;
    border: 1.5px solid #e8c878;
    border-top-width: 4px;
    box-shadow: 0 4px 24px rgba(180,150,40,.15), 0 2px 8px rgba(140,90,20,.08);
}
.theme-royal .person-card:hover {
    box-shadow: 0 20px 55px rgba(180,150,40,.25), 0 8px 20px rgba(140,90,20,.12);
}
.theme-royal .person-initial { background: linear-gradient(135deg, #78350f, #b45309); }
.theme-royal .tree-stage {
    background:
        radial-gradient(ellipse at 50% 0%, rgba(212,168,67,.1), transparent 60%),
        linear-gradient(160deg, #fffdf7 0%, #fdf8e7 100%);
    border-color: rgba(212,168,67,.2);
}
.theme-royal .generation-label {
    background: linear-gradient(135deg, #78350f, #b45309);
}
.theme-royal .person-card.gender-male    { border-top-color: #2563eb; }
.theme-royal .person-card.gender-female  { border-top-color: #be185d; }

/* Nature – green & earthy */
.theme-nature {
    --connector-color: #6abf7b;
    --connector-heart: #15803d;
    --line: #86efac;
    background:
        url("data:image/svg+xml,%3Csvg width='40' height='40' viewBox='0 0 40 40' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%2322c55e' fill-opacity='0.04'%3E%3Cpath d='M20 0C9 0 0 9 0 20s9 20 20 20 20-9 20-20S31 0 20 0zm0 36C11.2 36 4 28.8 4 20S11.2 4 20 4s16 7.2 16 16-7.2 16-16 16z'/%3E%3C/g%3E%3C/svg%3E"),
        linear-gradient(160deg, #f0fdf4 0%, #f0fdf4 50%, #ecfdf5 100%);
}
.theme-nature .person-card {
    background: #f9fef9;
    border-color: #bbf7d0;
    box-shadow: 0 4px 24px rgba(34,197,94,.1), 0 2px 8px rgba(22,101,52,.06);
}
.theme-nature .person-card:hover {
    box-shadow: 0 20px 55px rgba(34,197,94,.18), 0 8px 20px rgba(22,101,52,.1);
}
.theme-nature .person-initial { background: linear-gradient(135deg, #166534, #16a34a); }
.theme-nature .tree-stage {
    background:
        radial-gradient(ellipse at 50% 0%, rgba(34,197,94,.07), transparent 60%),
        linear-gradient(160deg, #f0fdf4 0%, #ecfdf5 100%);
    border-color: rgba(34,197,94,.15);
}
.theme-nature .generation-label {
    background: linear-gradient(135deg, #166534, #16a34a);
}

/* ── Ancestry View Badges ──────────────────────────────────────────────────── */
.tree-layout-descendants .tree-view-badge,
.tree-layout-ancestry .tree-view-badge {
    background: rgba(22,101,52,.12);
    color: var(--accent);
}

/* ── Mini-Map ──────────────────────────────────────────────────────────────── */
.minimap-panel {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    z-index: 50;
    background: rgba(255,255,255,.95);
    backdrop-filter: blur(12px);
    border: 1.5px solid rgba(0,0,0,.1);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    width: 220px;
    transition: opacity var(--transition), transform var(--transition);
}
.minimap-panel[hidden] { display: none !important; }

.minimap-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: .4rem .6rem;
    background: rgba(0,0,0,.03);
    border-bottom: 1px solid rgba(0,0,0,.06);
    font-size: .72rem;
    font-weight: 700;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .06em;
}
.minimap-toggle {
    background: none;
    border: none;
    cursor: pointer;
    font-size: .75rem;
    color: var(--muted);
    padding: 0;
    line-height: 1;
    font-family: inherit;
}
.minimap-toggle:hover { color: var(--ink); }

.minimap-canvas {
    display: block;
    width: 220px !important;
    height: 140px !important;
    cursor: pointer;
}

.minimap-show-btn {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    z-index: 50;
    width: 44px; height: 44px;
    border-radius: 50%;
    background: rgba(255,255,255,.95);
    border: 1.5px solid rgba(0,0,0,.1);
    box-shadow: var(--shadow);
    font-size: 1.1rem;
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: all var(--transition);
}
.minimap-show-btn:hover {
    transform: scale(1.1);
    box-shadow: var(--shadow-lg);
}
.minimap-show-btn[hidden] { display: none !important; }

/* ── Auth & Form Shell ─────────────────────────────────────────────────────── */
.auth-wrap {
    min-height: calc(100vh - 112px);
    display: grid;
    place-items: center;
    padding: 2rem 0;
}

.auth-panel,
.form-shell,
.empty-state,
.panel-form,
.app-table {
    background: var(--white);
    border: 1.5px solid #e5e7eb;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.auth-panel {
    width: min(500px, 100%);
    padding: 2rem;
    border-color: rgba(22,101,52,.16);
}

.auth-brand {
    display: flex;
    align-items: center;
    gap: .9rem;
    margin-bottom: 1.25rem;
}

.auth-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 1.5rem;
}

.auth-shell {
    width: min(860px, 100%);
    display: grid;
    grid-template-columns: 1fr minmax(280px, 380px);
    gap: 1rem;
    align-items: stretch;
}

.auth-shell .auth-brand,
.auth-card,
.panel-card {
    padding: 1.5rem;
    background: #fff;
    border: 1.5px solid #e5e7eb;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.auth-shell .auth-brand {
    display: grid;
    align-content: center;
    gap: .75rem;
    margin: 0;
}
.auth-shell .auth-brand h1,
.auth-shell .auth-brand p { margin: 0; }

.auth-card,
.panel-card {
    display: grid;
    gap: 1rem;
    align-content: start;
}

.form-shell {
    width: min(720px, 100%);
    padding: 1.5rem;
    margin: 0 auto;
}
.form-shell.wide { width: min(1040px, 100%); }

.stacked-form,
.panel-form {
    display: grid;
    gap: 1rem;
}

.member-form {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1rem;
}

label {
    display: grid;
    gap: .4rem;
    color: var(--ink-2);
    font-size: .875rem;
    font-weight: 600;
}

.check-row {
    display: flex;
    align-items: center;
    gap: .55rem;
    font-weight: 600;
}
.check-row input { width: 18px; height: 18px; }

.span-2 { grid-column: span 2; }

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

.family-card {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 1.5rem;
    min-height: 190px;
    padding: 1.5rem;
    background: var(--white);
    border: 1.5px solid #e5e7eb;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
    overflow: hidden;
    position: relative;
}
.family-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    opacity: 0;
    transition: opacity var(--transition);
}
.family-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-xl);
    border-color: rgba(22,101,52,.2);
}
.family-card:hover::before { opacity: 1; }

.family-card p   { color: var(--muted); margin: 0; }
.family-card span { color: var(--accent); font-weight: 700; }

.card-actions,
.button-row { display: flex; flex-wrap: wrap; gap: .45rem; align-items: center; }

.role-form { min-width: 120px; }

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    gap: .75rem;
    padding: 1rem;
    margin-bottom: 1rem;
    background: #fff;
    border: 1.5px solid #e5e7eb;
    border-radius: var(--radius-sm);
}
.filter-bar label { min-width: 180px; flex: 1 1 180px; }

.empty-state {
    padding: 3rem 2rem;
    max-width: 520px;
    text-align: center;
    display: grid;
    gap: 1rem;
    place-items: center;
}
.empty-state-icon { font-size: 4rem; line-height: 1; }
.empty-state p   { color: var(--muted); margin: 0; }

/* ── Relation Badge ────────────────────────────────────────────────────────── */
.relation-badge {
    display: inline-flex;
    align-items: center;
    min-height: 26px;
    margin: .15rem .25rem .15rem 0;
    padding: .2rem .5rem;
    color: var(--accent);
    background: rgba(22,101,52,.08);
    border: 1px solid rgba(22,101,52,.18);
    border-radius: var(--radius-pill);
    font-size: .78rem;
    font-weight: 700;
}

/* ── Split / Relationship Layouts ──────────────────────────────────────────── */
.split-layout {
    display: grid;
    grid-template-columns: minmax(260px, 360px) 1fr;
    gap: 1rem;
}

.relationship-layout {
    display: grid;
    grid-template-columns: minmax(320px, 430px) minmax(360px, 1fr);
    gap: 1.25rem;
    align-items: start;
}
.relationship-layout > .panel-form,
.relationship-layout > .stacked-form { min-width: 0; }

.relationship-summary  { grid-column: 1 / -1; }
.relationship-multi-select { min-height: 150px; max-height: 180px; overflow: auto; }
.relationship-steps {
    display: grid;
    gap: .55rem;
    margin: 0;
    padding-left: 1.2rem;
    color: var(--muted);
    font-weight: 600;
}
.relationship-help-card,
.relationship-advanced-card { box-shadow: none; }
.relationship-advanced-card { border-style: dashed; }
.relationship-child-list { display: flex; flex-wrap: wrap; gap: .4rem; }
.relationship-chip {
    display: inline-flex;
    align-items: center;
    min-height: 30px;
    padding: .2rem .55rem;
    background: rgba(22,101,52,.08);
    border: 1px solid rgba(22,101,52,.18);
    border-radius: var(--radius-pill);
    font-size: .875rem;
    font-weight: 700;
    color: var(--accent);
}

/* ── Panel Form / Card ─────────────────────────────────────────────────────── */
.panel-form { padding: 1.25rem; align-self: start; }
.panel-form .btn, .panel-card .btn { justify-self: start; }
.wide-panel { width: min(980px, 100%); }
.form-hint { margin: 0; color: var(--muted); font-size: .875rem; }

/* ── Table ─────────────────────────────────────────────────────────────────── */
.app-table { overflow: hidden; }
.app-table .table > :not(caption) > * > * {
    padding: .85rem .75rem;
    vertical-align: middle;
}
.app-table table { margin-bottom: 0; }
.app-table thead th {
    color: var(--ink-2);
    background: #f9fafb;
    font-size: .8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .05em;
}

/* ── Profile Layout ────────────────────────────────────────────────────────── */
.profile-layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 2rem;
    max-width: 980px;
}
.profile-photo img,
.avatar-placeholder {
    width: 100%;
    aspect-ratio: 1;
    border-radius: var(--radius);
    object-fit: cover;
}
.avatar-placeholder {
    display: grid;
    place-items: center;
    background: linear-gradient(135deg, #d1fae5, #a7f3d0);
    color: var(--accent);
    font-size: 5rem;
    font-weight: 800;
}
.profile-detail dl {
    display: grid;
    grid-template-columns: 140px 1fr;
    gap: .55rem 1rem;
    margin: 1.2rem 0;
}
.profile-detail dt { color: var(--muted); font-weight: 500; }

/* ── Metrics ───────────────────────────────────────────────────────────────── */
.metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: .75rem;
    margin-bottom: 1rem;
}
.compact-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.metric-card {
    display: grid;
    gap: .2rem;
    padding: 1rem;
    background: #fff;
    border: 1.5px solid #e5e7eb;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
}
.metric-card strong { font-size: 1.7rem; font-weight: 900; letter-spacing: -.04em; }
.metric-card span  { color: var(--muted); font-weight: 600; font-size: .82rem; }

/* ── Status Pill ───────────────────────────────────────────────────────────── */
.status-pill {
    display: inline-flex;
    align-items: center;
    min-height: 26px;
    padding: .2rem .55rem;
    border-radius: var(--radius-pill);
    font-size: .75rem;
    font-weight: 700;
}
.status-pill.ok   { color: #166534; background: #dcfce7; border: 1px solid #bbf7d0; }
.status-pill.warn { color: #92400e; background: #fef3c7; border: 1px solid #fde68a; }

/* ── Duplicate ─────────────────────────────────────────────────────────────── */
.duplicate-list     { display: grid; gap: 1rem; }
.duplicate-group    { padding: 1.25rem; background: #fff; border: 1.5px solid #e5e7eb; border-radius: var(--radius-sm); }
.duplicate-members  { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .75rem; }
.mini-member-card   { display: grid; gap: .35rem; padding: .85rem; border: 1.5px solid #e5e7eb; border-radius: var(--radius-sm); background: #f9fafb; }
.mini-member-card span { color: var(--muted); font-size: .875rem; }

/* ── Theme Select / Print Size ─────────────────────────────────────────────── */
.theme-select     { width: 150px; }
.print-size-select { width: 160px; }

/* ── Tree Mode Toggle ──────────────────────────────────────────────────────── */
.tree-mode-group { flex-shrink: 0; }
.tree-mode-group .btn { font-size: .8rem; }

/* ── Footer ────────────────────────────────────────────────────────────────── */
.app-footer {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    padding: 1.25rem 1.5rem;
    color: var(--muted);
    border-top: 1px solid #f3f4f6;
    font-size: .85rem;
}
.app-footer span { color: var(--ink); font-weight: 800; }

/* ── Nav tablet ────────────────────────────────────────────────────────────── */
@media (min-width: 992px) {
    .app-navbar .navbar-collapse { min-width: 0; }
    .app-navbar .navbar-nav { min-width: 0; overflow-x: auto; scrollbar-width: thin; }
}

/* ── Responsive breakpoints ────────────────────────────────────────────────── */
@media (max-width: 900px) {
    .page-head, .tree-toolbar, .split-layout,
    .relationship-layout, .profile-layout {
        grid-template-columns: 1fr;
        display: grid;
        align-items: start;
    }
    .member-form { grid-template-columns: 1fr; }
    .span-2 { grid-column: auto; }
}

@media (max-width: 640px) {
    main.container-fluid { padding-left: .75rem !important; padding-right: .75rem !important; }
    .auth-panel, .form-shell, .empty-state, .panel-form,
    .panel-card, .auth-card, .auth-shell .auth-brand { padding: 1rem; }
    .auth-shell { grid-template-columns: 1fr; }
    .button-row { width: 100%; }
    .button-row .btn, .theme-select, .print-size-select { flex: 1 1 130px; }
    .tree-stage { padding: .75rem; }
    .tree-canvas { padding: 1rem; }
    .person-card { width: 165px; min-height: 235px; padding: .8rem; }
    .person-photo-wrap { width: 84px; height: 84px; }
    .person-photo, .person-initial { width: 80px; height: 80px; font-size: 1.7rem; }
    .person-name { font-size: .95rem; }
    .minimap-panel { width: 175px; bottom: 1rem; right: 1rem; }
    .minimap-canvas { width: 175px !important; height: 110px !important; }
}

/* ══════════════════════════════════════════════════════════════════════════════
   PRINT STYLES
   ══════════════════════════════════════════════════════════════════════════════ */
@media print {
    @page { size: auto; margin: 10mm; }
    @page a4Landscape { size: A4 landscape; margin: 10mm; }
    @page a4Portrait  { size: A4 portrait;  margin: 10mm; }
    @page a3Landscape { size: A3 landscape; margin: 10mm; }
    @page a3Portrait  { size: A3 portrait;  margin: 10mm; }

    body                { background: #fff !important; }
    body.print-a4-landscape { page: a4Landscape; }
    body.print-a4-portrait  { page: a4Portrait; }
    body.print-a3-landscape { page: a3Landscape; }
    body.print-a3-portrait  { page: a3Portrait; }

    .app-navbar, .tree-toolbar, .zoom-controls,
    .alert, .minimap-panel, .minimap-show-btn { display: none !important; }

    main.container-fluid { padding: 0 !important; max-width: none; }

    .tree-stage {
        min-height: auto;
        overflow: visible;
        padding: 0;
        border: 0 !important;
        background: #fff !important;
        box-shadow: none !important;
        cursor: default;
    }

    .tree-canvas {
        transform: none !important;
        padding: 1rem;
        min-height: auto;
        background: #fff !important;
    }

    .connector-svg { position: absolute; }

    .person-card {
        break-inside: avoid;
        box-shadow: 0 0 0 1px #e5e7eb !important;
    }

    .generation-row::before { display: none; }

    .rel-toolbox,
    .toolbox-open-btn { display: none !important; }
}

/* ══════════════════════════════════════════════════════════════════════════════
   PHOTOSHOP-STYLE RELATIONSHIP TOOLBOX
   ══════════════════════════════════════════════════════════════════════════════ */

/* Floating panel */
.rel-toolbox {
    position: fixed;
    top: 50%;
    left: 1.25rem;
    transform: translateY(-50%);
    z-index: 300;
    width: 196px;
    background: rgba(255,255,255,.97);
    backdrop-filter: blur(20px) saturate(1.6);
    -webkit-backdrop-filter: blur(20px) saturate(1.6);
    border: 1.5px solid rgba(0,0,0,.09);
    border-radius: 14px;
    box-shadow: 0 8px 40px rgba(17,24,39,.16), 0 2px 8px rgba(17,24,39,.08);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    gap: 0;
    animation: toolboxSlideIn .28s cubic-bezier(.4,0,.2,1) both;
    user-select: none;
}

@keyframes toolboxSlideIn {
    from { opacity: 0; transform: translateY(-50%) translateX(-16px); }
    to   { opacity: 1; transform: translateY(-50%) translateX(0); }
}

/* Header */
.rel-toolbox-header {
    display: flex;
    align-items: center;
    gap: .45rem;
    padding: .6rem .75rem .5rem;
    background: linear-gradient(135deg, #111827, #1f2937);
    color: #fff;
    font-size: .78rem;
    font-weight: 700;
    letter-spacing: .04em;
    text-transform: uppercase;
}

.rel-toolbox-header span { flex: 1; }

.toolbox-close {
    background: none;
    border: none;
    color: rgba(255,255,255,.6);
    cursor: pointer;
    font-size: .8rem;
    padding: 0;
    line-height: 1;
    font-family: inherit;
    transition: color var(--transition);
}
.toolbox-close:hover { color: #fff; }

/* Step indicator strip */
.toolbox-steps {
    display: flex;
    border-bottom: 1px solid #f3f4f6;
}

.tstep {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: .15rem;
    padding: .45rem .2rem;
    font-size: .62rem;
    font-weight: 600;
    color: #9ca3af;
    text-align: center;
    transition: color var(--transition), background var(--transition);
    border-right: 1px solid #f3f4f6;
    line-height: 1.2;
}
.tstep:last-child { border-right: none; }

.tstep-num {
    display: grid;
    place-items: center;
    width: 18px; height: 18px;
    border-radius: 50%;
    background: #e5e7eb;
    color: #6b7280;
    font-size: .68rem;
    font-weight: 800;
    transition: background var(--transition), color var(--transition);
}

.tstep.active { color: var(--accent); }
.tstep.active .tstep-num {
    background: var(--accent);
    color: #fff;
}
.tstep.done { color: #059669; }
.tstep.done .tstep-num {
    background: #059669;
    color: #fff;
}

/* Pair display */
.toolbox-pair {
    padding: .55rem .65rem;
    background: #f9fafb;
    border-bottom: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    gap: .35rem;
}

.pair-card {
    display: flex;
    align-items: center;
    gap: .4rem;
    padding: .35rem .5rem;
    background: #fff;
    border: 1.5px solid #e5e7eb;
    border-radius: 8px;
    min-height: 34px;
    transition: border-color var(--transition);
}
.pair-card.filled { border-color: var(--accent); background: rgba(22,101,52,.04); }
.pair-card-b.filled { border-color: #3b82f6; background: rgba(59,130,246,.04); }

.pair-badge {
    display: grid;
    place-items: center;
    width: 20px; height: 20px;
    border-radius: 50%;
    font-size: .65rem;
    font-weight: 800;
    flex-shrink: 0;
}
.pair-badge-a { background: rgba(22,101,52,.12); color: var(--accent); }
.pair-badge-b { background: rgba(59,130,246,.12); color: #3b82f6; }

.pair-name {
    flex: 1;
    font-size: .75rem;
    font-weight: 600;
    color: var(--ink);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.pair-clear {
    background: none;
    border: none;
    cursor: pointer;
    color: #9ca3af;
    font-size: .7rem;
    padding: 0;
    line-height: 1;
    font-family: inherit;
    flex-shrink: 0;
}
.pair-clear:hover { color: #ef4444; }

.pair-connector {
    display: flex;
    align-items: center;
    gap: .35rem;
    padding-left: .6rem;
    color: var(--muted);
}

.pair-rel-label {
    font-size: .68rem;
    font-weight: 700;
    color: var(--accent);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 120px;
}

/* Tool groups */
.toolbox-tools {
    display: flex;
    flex-direction: column;
    gap: 0;
    flex: 1;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: #d1d5db transparent;
    max-height: 52vh;
}

.tool-group {
    border-bottom: 1px solid #f3f4f6;
    padding: .35rem 0 .4rem;
}
.tool-group:last-child { border-bottom: none; }

.tool-group-hd {
    padding: .15rem .7rem .3rem;
    font-size: .62rem;
    font-weight: 800;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: #9ca3af;
}

.tool-btn {
    display: flex;
    align-items: center;
    gap: .5rem;
    width: 100%;
    padding: .42rem .7rem;
    background: none;
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-size: .8rem;
    font-weight: 600;
    color: var(--ink-2);
    text-align: left;
    border-radius: 0;
    transition: background var(--transition), color var(--transition);
    position: relative;
}

.tool-btn:hover {
    background: #f0fdf4;
    color: var(--accent);
}

.tool-btn.active {
    background: linear-gradient(90deg, rgba(22,101,52,.12), rgba(22,101,52,.04));
    color: var(--accent);
    font-weight: 700;
}

.tool-btn.active::before {
    content: '';
    position: absolute;
    left: 0; top: 0; bottom: 0;
    width: 3px;
    background: var(--accent);
    border-radius: 0 2px 2px 0;
}

.tool-icon {
    font-size: 1rem;
    line-height: 1;
    flex-shrink: 0;
    width: 22px;
    text-align: center;
}

.tool-name {
    flex: 1;
    line-height: 1.2;
}

/* Toolbox footer (save/reset) */
.toolbox-footer {
    display: flex;
    gap: .4rem;
    padding: .55rem .65rem;
    border-top: 1px solid #e5e7eb;
    background: #f9fafb;
}

.tbx-save {
    flex: 1;
    padding: .45rem .6rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-family: inherit;
    font-size: .78rem;
    font-weight: 700;
    cursor: pointer;
    transition: all var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: .3rem;
}
.tbx-save:hover {
    background: #15803d;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(22,101,52,.3);
}
.tbx-save:disabled {
    opacity: .55;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.tbx-reset {
    padding: .45rem .6rem;
    background: none;
    border: 1.5px solid #d1d5db;
    border-radius: 8px;
    font-family: inherit;
    font-size: .78rem;
    font-weight: 700;
    color: var(--muted);
    cursor: pointer;
    transition: all var(--transition);
}
.tbx-reset:hover {
    border-color: #ef4444;
    color: #ef4444;
}

/* Toast */
.toolbox-toast {
    padding: .5rem .7rem;
    font-size: .75rem;
    font-weight: 600;
    text-align: center;
    border-top: 1px solid #e5e7eb;
    animation: toastIn .2s ease;
    line-height: 1.3;
}
.toolbox-toast.ok  { color: #166534; background: #f0fdf4; }
.toolbox-toast.err { color: #991b1b; background: #fef2f2; }

@keyframes toastIn {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* Open button */
.toolbox-open-btn {
    position: fixed;
    top: 50%;
    left: 1.25rem;
    transform: translateY(-50%);
    z-index: 300;
    display: flex;
    align-items: center;
    gap: .4rem;
    padding: .55rem .8rem;
    background: rgba(255,255,255,.95);
    backdrop-filter: blur(12px);
    border: 1.5px solid rgba(0,0,0,.1);
    border-radius: 10px;
    box-shadow: var(--shadow);
    font-family: inherit;
    font-size: .78rem;
    font-weight: 700;
    color: var(--ink-2);
    cursor: pointer;
    transition: all var(--transition);
    animation: toolboxSlideIn .28s ease;
}
.toolbox-open-btn:hover {
    background: #fff;
    border-color: var(--accent);
    color: var(--accent);
    box-shadow: var(--shadow-lg);
    transform: translateY(-50%) translateX(2px);
}

/* Card highlight when toolbox is in selection mode */
.tree-link-mode .person-card {
    cursor: crosshair !important;
    transition: outline var(--transition), box-shadow var(--transition);
}
.tree-link-mode .person-card:hover {
    outline: 2.5px dashed var(--accent);
    outline-offset: 3px;
}

.person-card.tbx-selected-a {
    outline: 3px solid var(--accent) !important;
    outline-offset: 4px;
    box-shadow: 0 0 0 6px rgba(22,101,52,.12), var(--shadow-xl) !important;
}
.person-card.tbx-selected-b {
    outline: 3px solid #3b82f6 !important;
    outline-offset: 4px;
    box-shadow: 0 0 0 6px rgba(59,130,246,.12), var(--shadow-xl) !important;
}

/* Responsive: stack toolbox at top on small screens */
@media (max-width: 640px) {
    .rel-toolbox {
        top: auto;
        bottom: 5.5rem;
        left: .75rem;
        transform: none;
        width: calc(100vw - 1.5rem);
        max-width: 360px;
    }

    .toolbox-open-btn {
        top: auto;
        bottom: 5.5rem;
        left: .75rem;
        transform: none;
    }

    .toolbox-tools { max-height: 35vh; }
}

/* ══════════════════════════════════════════════════════════════════════════════
   ACTIVE PERSON SYSTEM
   ══════════════════════════════════════════════════════════════════════════════ */

/* Crown on active card */
.active-crown {
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 1.1rem;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,.2));
    animation: crownBounce 1s ease both;
    z-index: 2;
}

@keyframes crownBounce {
    from { opacity: 0; transform: translateX(-50%) translateY(-8px); }
    to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* Active person card glow */
.person-card.is-active-person {
    border-top-color: #f59e0b !important;
    box-shadow: 0 0 0 3px rgba(245,158,11,.25), var(--shadow-lg) !important;
    position: relative;
}

/* Active person floating badge (inside tree canvas) */
.active-person-badge {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 50;
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .4rem .7rem .4rem .4rem;
    background: rgba(255,255,255,.95);
    backdrop-filter: blur(12px);
    border: 1.5px solid rgba(245,158,11,.4);
    border-radius: 30px;
    box-shadow: 0 4px 16px rgba(0,0,0,.1);
    font-size: .75rem;
    font-weight: 600;
    animation: badgeIn .3s ease both;
}

@keyframes badgeIn {
    from { opacity: 0; transform: translateY(-6px) scale(.95); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

.apb-icon {
    width: 28px; height: 28px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    border: 2px solid #f59e0b;
}
.apb-icon img { width: 100%; height: 100%; object-fit: cover; }
.apb-initial {
    display: grid;
    place-items: center;
    width: 100%; height: 100%;
    font-size: .85rem;
    font-weight: 800;
    color: #fff;
}
.apb-initial.gender-male   { background: linear-gradient(135deg, #3b82f6, #1d4ed8); }
.apb-initial.gender-female { background: linear-gradient(135deg, #ec4899, #be185d); }
.apb-initial.gender-other  { background: linear-gradient(135deg, #8b5cf6, #6d28d9); }

.apb-text { display: flex; flex-direction: column; gap: .05rem; }
.apb-label { font-size: .6rem; font-weight: 800; letter-spacing: .05em; text-transform: uppercase; color: #f59e0b; }
.apb-name  { font-size: .8rem; font-weight: 700; color: var(--ink); }
.apb-hint  { font-size: .6rem; color: var(--muted); font-weight: 500; }

/* Set-active pin button */
.card-hover-actions {
    position: absolute;
    top: 6px;
    right: 6px;
    display: flex;
    gap: .3rem;
    align-items: center;
    opacity: 0;
    transition: opacity var(--transition);
    z-index: 10;
}
.person-card:hover .card-hover-actions { opacity: 1; }

.set-active-btn {
    background: rgba(245,158,11,.1);
    border: 1.5px solid rgba(245,158,11,.4);
    border-radius: 6px;
    cursor: pointer;
    font-size: .7rem;
    padding: .2rem .3rem;
    line-height: 1;
    color: #b45309;
    transition: all var(--transition);
}
.set-active-btn:hover {
    background: #f59e0b;
    border-color: #f59e0b;
    color: #fff;
    transform: scale(1.1);
}

/* ══════════════════════════════════════════════════════════════════════════════
   LINK HANDLE — rubber band drag trigger
   ══════════════════════════════════════════════════════════════════════════════ */

.link-handle {
    display: grid;
    place-items: center;
    width: 24px; height: 24px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-size: .75rem;
    cursor: crosshair;
    opacity: 0;
    transition: opacity var(--transition), transform var(--transition), box-shadow var(--transition);
    flex-shrink: 0;
    user-select: none;
    box-shadow: 0 2px 8px rgba(22,101,52,.4);
}
.person-card:hover .link-handle {
    opacity: 1;
}
.link-handle:hover {
    transform: scale(1.2);
    box-shadow: 0 4px 12px rgba(22,101,52,.5);
}
.link-handle:active {
    transform: scale(.95);
}

/* Card states during rubber-band drag */
.person-card.rb-source {
    opacity: .55;
    transform: scale(.97);
}
.person-card.rb-target-hover {
    outline: 3px solid #16a34a !important;
    outline-offset: 4px;
    box-shadow: 0 0 0 6px rgba(22,101,52,.15), var(--shadow-xl) !important;
    transform: scale(1.03);
}

/* ══════════════════════════════════════════════════════════════════════════════
   LINK PICKER POPUP
   ══════════════════════════════════════════════════════════════════════════════ */

.link-picker-popup {
    position: fixed;
    z-index: 9999;
    background: #fff;
    border: 1.5px solid #e5e7eb;
    border-radius: 14px;
    box-shadow: 0 20px 60px rgba(17,24,39,.2), 0 4px 16px rgba(17,24,39,.1);
    width: 300px;
    overflow: hidden;
    animation: lpIn .18s cubic-bezier(.4,0,.2,1) both;
}

@keyframes lpIn {
    from { opacity: 0; transform: scale(.92) translateY(8px); }
    to   { opacity: 1; transform: scale(1)   translateY(0); }
}

.lp-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .65rem .75rem .5rem;
    background: linear-gradient(135deg, #111827, #1f2937);
    color: #fff;
}

.lp-members {
    display: flex;
    align-items: center;
    gap: .4rem;
    flex: 1;
    overflow: hidden;
}

.lp-badge {
    display: grid;
    place-items: center;
    width: 20px; height: 20px;
    border-radius: 50%;
    font-size: .65rem;
    font-weight: 800;
    flex-shrink: 0;
}
.lp-badge-a { background: rgba(22,101,52,.8); color: #fff; }
.lp-badge-b { background: rgba(59,130,246,.8); color: #fff; }

.lp-member-name {
    font-size: .78rem;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 80px;
    color: #fff;
}

.lp-close {
    background: none;
    border: none;
    color: rgba(255,255,255,.6);
    cursor: pointer;
    font-size: .8rem;
    padding: .2rem;
    font-family: inherit;
}
.lp-close:hover { color: #fff; }

.lp-subtitle {
    padding: .4rem .75rem .2rem;
    font-size: .72rem;
    font-weight: 600;
    color: var(--muted);
    background: #f9fafb;
    border-bottom: 1px solid #f3f4f6;
}

.lp-options-list {
    display: flex;
    flex-direction: column;
    padding: 0.6rem;
    gap: 0.4rem;
}

.lp-btn {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.6rem 0.75rem;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.75rem;
    color: var(--ink-2);
    transition: all var(--transition);
    text-align: left;
    width: 100%;
}
.lp-btn span:first-child { font-size: 1.2rem; line-height: 1; flex-shrink: 0; }
.lp-btn:hover {
    background: #f0fdf4;
    border-color: #16a34a;
    color: #166534;
}
.lp-btn:active { background: #dcfce7; }

.lp-toast {
    padding: .5rem .75rem;
    font-size: .75rem;
    font-weight: 600;
    text-align: center;
    border-top: 1px solid #e5e7eb;
}
.lp-toast.ok  { color: #166534; background: #f0fdf4; }
.lp-toast.err { color: #991b1b; background: #fef2f2; }

/* ══════════════════════════════════════════════════════════════════════════════
   GROUP BUILDER PANEL
   ══════════════════════════════════════════════════════════════════════════════ */

.tbx-group-toggle {
    width: calc(100% - 1.3rem);
    margin: 0 .65rem .55rem;
    padding: .4rem .5rem;
    background: rgba(245,158,11,.1);
    border: 1.5px dashed rgba(245,158,11,.5);
    border-radius: 8px;
    font-family: inherit;
    font-size: .75rem;
    font-weight: 700;
    color: #b45309;
    cursor: pointer;
    transition: all var(--transition);
    text-align: center;
}
.tbx-group-toggle:hover {
    background: rgba(245,158,11,.2);
    border-color: #f59e0b;
    color: #92400e;
}

.group-builder-panel {
    position: fixed;
    top: 50%;
    left: 220px;
    transform: translateY(-50%);
    z-index: 300;
    width: 420px;
    max-height: 88vh;
    overflow-y: auto;
    background: rgba(255,255,255,.97);
    backdrop-filter: blur(20px) saturate(1.6);
    border: 1.5px solid rgba(0,0,0,.09);
    border-radius: 14px;
    box-shadow: 0 8px 40px rgba(17,24,39,.16), 0 2px 8px rgba(17,24,39,.08);
    overflow: hidden;
    animation: toolboxSlideIn .28s cubic-bezier(.4,0,.2,1) both;
}

.gbp-header {
    display: flex;
    align-items: center;
    gap: .45rem;
    padding: .6rem .75rem;
    background: linear-gradient(135deg, #92400e, #b45309);
    color: #fff;
    font-size: .78rem;
    font-weight: 700;
    letter-spacing: .04em;
    text-transform: uppercase;
}
.gbp-header span { flex: 1; }
.gbp-close {
    background: none;
    border: none;
    color: rgba(255,255,255,.6);
    cursor: pointer;
    font-size: .8rem;
    padding: 0;
    font-family: inherit;
    transition: color var(--transition);
}
.gbp-close:hover { color: #fff; }

.gbp-template {
    padding: .7rem .75rem .65rem;
    border-bottom: 1px solid #f3f4f6;
}
.gbp-template:last-child { border-bottom: none; }

/* Tabs strip */
#gbpTabs.gbp-template-tabs {
    padding: .5rem .75rem .35rem;
    background: #f9fafb;
    border-bottom: 1px solid #f3f4f6;
}


.gbp-template-title {
    font-size: .7rem;
    font-weight: 800;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: #9ca3af;
    margin-bottom: .4rem;
}

/* Slot rows */
.gbp-row, .gbp-chain-row {
    display: flex;
    align-items: center;
    gap: .4rem;
    flex-wrap: wrap;
}
.gbp-chain-row { flex-wrap: nowrap; }

.gbp-heart-icon, .gbp-plus, .gbp-chain-arrow {
    font-size: 1rem;
    flex-shrink: 0;
    color: var(--muted);
    font-weight: 700;
}
.gbp-heart-icon { color: #f43f5e; }

/* Drop slots */
.group-slot {
    flex: 1;
    min-width: 80px;
    min-height: 60px;
    border: 2px dashed #d1d5db;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .2rem;
    padding: .4rem .3rem;
    background: #fafafa;
    cursor: pointer;
    transition: all var(--transition);
    text-align: center;
    position: relative;
}
.group-slot:hover {
    border-color: var(--accent);
    background: #f0fdf4;
}
.group-slot.drag-over-slot {
    border-color: var(--accent);
    background: #dcfce7;
    transform: scale(1.03);
    box-shadow: 0 0 0 3px rgba(22,101,52,.15);
}
.group-slot.slot-filled {
    border-color: var(--accent);
    border-style: solid;
    background: #f0fdf4;
}
.group-slot.slot-filled .slot-icon { display: none; }
.group-slot.slot-filled .slot-label { display: none; }

.slot-icon { font-size: 1.2rem; line-height: 1; }
.slot-label {
    font-size: .62rem;
    font-weight: 600;
    color: #9ca3af;
    line-height: 1.3;
}

/* Filled slot content */
.slot-filled-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: .15rem;
    width: 100%;
}
.slot-filled-initial {
    display: grid;
    place-items: center;
    width: 28px; height: 28px;
    border-radius: 50%;
    font-size: .8rem;
    font-weight: 800;
    color: #fff;
}
.slot-filled-initial.gender-male   { background: linear-gradient(135deg, #3b82f6, #1d4ed8); }
.slot-filled-initial.gender-female { background: linear-gradient(135deg, #ec4899, #be185d); }
.slot-filled-initial.gender-other  { background: linear-gradient(135deg, #8b5cf6, #6d28d9); }
.slot-filled-name {
    font-size: .68rem;
    font-weight: 700;
    color: var(--ink);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 90px;
}
.slot-clear-btn {
    position: absolute;
    top: 3px; right: 3px;
    background: rgba(239,68,68,.15);
    border: none;
    border-radius: 4px;
    color: #ef4444;
    font-size: .6rem;
    cursor: pointer;
    padding: .1rem .25rem;
    line-height: 1;
    font-family: inherit;
}
.slot-clear-btn:hover { background: #ef4444; color: #fff; }

/* Children row */
.gbp-children-area { margin-top: .4rem; }
.gbp-children-label {
    font-size: .62rem;
    font-weight: 700;
    color: var(--muted);
    text-align: center;
    margin-bottom: .3rem;
}
.gbp-children-row {
    display: flex;
    flex-wrap: wrap;
    gap: .3rem;
    align-items: center;
}
.group-child-slot { min-width: 64px; flex: none; min-height: 54px; }

.gbp-add-child {
    padding: .35rem .5rem;
    background: none;
    border: 1.5px dashed #d1d5db;
    border-radius: 8px;
    font-family: inherit;
    font-size: .7rem;
    font-weight: 700;
    color: var(--muted);
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}
.gbp-add-child:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.gbp-save {
    width: 100%;
    margin-top: .5rem;
    padding: .45rem .6rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-family: inherit;
    font-size: .78rem;
    font-weight: 700;
    cursor: pointer;
    transition: all var(--transition);
}
.gbp-save:hover:not(:disabled) {
    background: #15803d;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(22,101,52,.3);
}
.gbp-save:disabled {
    opacity: .45;
    cursor: not-allowed;
}

/* ══════════════════════════════════════════════════════════════════════════════
   MEMBER TRAY — bottom strip
   ══════════════════════════════════════════════════════════════════════════════ */

.member-tray {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 250;
    background: rgba(255,255,255,.97);
    backdrop-filter: blur(16px);
    border-top: 1.5px solid #e5e7eb;
    box-shadow: 0 -4px 24px rgba(17,24,39,.1);
    animation: trayIn .3s cubic-bezier(.4,0,.2,1) both;
}

@keyframes trayIn {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
}

.tray-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .35rem .75rem .25rem;
    border-bottom: 1px solid #f3f4f6;
}
.tray-header span {
    font-size: .7rem;
    font-weight: 700;
    color: var(--muted);
    letter-spacing: .03em;
}
.tray-close {
    background: none;
    border: none;
    cursor: pointer;
    color: #9ca3af;
    font-size: .8rem;
    padding: .1rem;
    font-family: inherit;
}
.tray-close:hover { color: #ef4444; }

.tray-chips {
    display: flex;
    gap: .5rem;
    padding: .45rem .75rem .5rem;
    overflow-x: auto;
    scrollbar-width: thin;
    scrollbar-color: #d1d5db transparent;
}

.tray-chip {
    display: flex;
    align-items: center;
    gap: .35rem;
    padding: .3rem .55rem .3rem .35rem;
    background: #f9fafb;
    border: 1.5px solid #e5e7eb;
    border-radius: 30px;
    cursor: grab;
    white-space: nowrap;
    font-size: .75rem;
    font-weight: 600;
    color: var(--ink-2);
    transition: all var(--transition);
    user-select: none;
    flex-shrink: 0;
}
.tray-chip:hover {
    border-color: var(--accent);
    background: #f0fdf4;
    color: var(--accent);
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}
.tray-chip:active { cursor: grabbing; }
.tray-chip.dragging-active {
    opacity: .4;
    transform: scale(.95);
}

.tray-initial {
    display: grid;
    place-items: center;
    width: 22px; height: 22px;
    border-radius: 50%;
    font-size: .7rem;
    font-weight: 800;
    color: #fff;
    flex-shrink: 0;
}
.tray-initial.gender-male   { background: linear-gradient(135deg, #3b82f6, #1d4ed8); }
.tray-initial.gender-female { background: linear-gradient(135deg, #ec4899, #be185d); }
.tray-initial.gender-other  { background: linear-gradient(135deg, #8b5cf6, #6d28d9); }
.tray-chip-name { line-height: 1; }

@media (max-width: 640px) {
    .group-builder-panel {
        left: .75rem;
        top: auto;
        bottom: 7rem;
        transform: none;
        width: calc(100vw - 1.5rem);
        max-width: 360px;
    }
}

/* ══════════════════════════════════════════════════════════════════════════════
   ACTIVE PERSON DIAGRAM PANEL
   ══════════════════════════════════════════════════════════════════════════════ */

.apd-overlay {
    position: fixed;
    inset: 0;
    z-index: 900;
    background: rgba(10, 20, 10, .55);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    animation: apdFadeIn .22s ease both;
}
@keyframes apdFadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.apd-panel {
    position: relative;
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 40px 100px rgba(17,24,39,.22), 0 8px 30px rgba(22,101,52,.12);
    width: 100%;
    max-width: 1100px;
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    animation: apdSlideIn .26s cubic-bezier(.22, .68, 0, 1.2) both;
}
@keyframes apdSlideIn {
    from { opacity: 0; transform: translateY(32px) scale(.97); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

.apd-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.1rem 1.4rem .9rem;
    border-bottom: 1.5px solid #f0f0f0;
    background: linear-gradient(135deg, #f0fdf4 0%, #fff 100%);
    flex-shrink: 0;
}
.apd-header-left {
    display: flex;
    align-items: center;
    gap: .75rem;
}
.apd-header-icon {
    display: grid;
    place-items: center;
    width: 40px; height: 40px;
    background: linear-gradient(135deg, #166534, #16a34a);
    border-radius: 12px;
    font-size: 1.15rem;
    box-shadow: 0 4px 12px rgba(22,101,52,.3);
}
.apd-header-info h3 {
    margin: 0 0 .15rem;
    font-size: 1.05rem;
    font-weight: 800;
    color: #111827;
    letter-spacing: -.02em;
}
.apd-header-info p {
    margin: 0;
    font-size: .78rem;
    color: #6b7280;
    font-weight: 500;
}
.apd-close {
    display: grid;
    place-items: center;
    width: 34px; height: 34px;
    background: #f3f4f6;
    border: none;
    border-radius: 8px;
    font-size: .95rem;
    cursor: pointer;
    color: #6b7280;
    transition: all .15s ease;
    font-family: inherit;
}
.apd-close:hover { background: #fee2e2; color: #ef4444; }

.apd-body {
    overflow-y: auto;
    padding: 1.4rem;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0;
}

/* ── Diagram Grid Layout ── */
.apd-diagram {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0;
    position: relative;
}

/* ── Section rows ── */
.apd-row {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
}

.apd-row-label {
    display: inline-flex;
    align-items: center;
    gap: .45rem;
    padding: .28rem .75rem;
    background: #f8fafc;
    border: 1px solid #e5e7eb;
    border-radius: 30px;
    font-size: .72rem;
    font-weight: 700;
    color: #6b7280;
    letter-spacing: .04em;
    text-transform: uppercase;
    margin-bottom: .6rem;
    position: relative;
    z-index: 2;
}
.apd-row-label span { font-size: .9rem; }

/* Row accent colors */
.apd-row.row-great-grandparents .apd-row-label { background: #fef3c7; border-color: #fbbf24; color: #92400e; }
.apd-row.row-grandparents .apd-row-label       { background: #fde8d8; border-color: #f97316; color: #7c2d12; }
.apd-row.row-parents .apd-row-label            { background: #dbeafe; border-color: #3b82f6; color: #1e3a8a; }
.apd-row.row-siblings .apd-row-label           { background: #f3e8ff; border-color: #a855f7; color: #581c87; }
.apd-row.row-active .apd-row-label             { background: linear-gradient(135deg,#f0fdf4,#dcfce7); border-color: #16a34a; color: #14532d; font-size: .78rem; }
.apd-row.row-children .apd-row-label           { background: #f0fdf4; border-color: #22c55e; color: #14532d; }
.apd-row.row-grandchildren .apd-row-label      { background: #ecfdf5; border-color: #10b981; color: #064e3b; }
.apd-row.row-great-grandchildren .apd-row-label { background: #f0f9ff; border-color: #0ea5e9; color: #0c4a6e; }

/* Cards wrapper for each row */
.apd-cards-row {
    display: flex;
    flex-wrap: wrap;
    gap: .75rem;
    justify-content: center;
    align-items: flex-start;
}

/* Connector line between rows */
.apd-connector-line {
    width: 2px;
    height: 32px;
    background: linear-gradient(to bottom, #d1d5db, #e5e7eb);
    margin: 0 auto;
    position: relative;
}
.apd-connector-line::before {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    width: 8px; height: 8px;
    background: #d1d5db;
    border-radius: 50%;
}

/* Couple group within a row */
.apd-couple-group {
    display: flex;
    align-items: center;
    gap: .35rem;
    padding: .5rem .65rem;
    background: rgba(255,255,255,.9);
    border: 1.5px dashed #d1d5db;
    border-radius: 16px;
    box-shadow: 0 2px 8px rgba(17,24,39,.05);
    position: relative;
}
.apd-couple-heart {
    font-size: .9rem;
    animation: heartPulse 2.4s ease-in-out infinite;
    flex-shrink: 0;
}

/* ── Mini person card for diagram ── */
.apd-mini-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: .3rem;
    padding: .65rem .75rem .55rem;
    min-width: 110px;
    max-width: 140px;
    background: #fff;
    border: 1.5px solid #e5e7eb;
    border-radius: 14px;
    box-shadow: 0 2px 10px rgba(17,24,39,.08);
    cursor: pointer;
    transition: all .18s ease;
    text-decoration: none;
    color: #111827;
    position: relative;
}
.apd-mini-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 24px rgba(17,24,39,.14);
    color: #111827;
}
.apd-mini-card.gender-male   { border-top: 3px solid #3b82f6; }
.apd-mini-card.gender-female { border-top: 3px solid #ec4899; }
.apd-mini-card.gender-other  { border-top: 3px solid #8b5cf6; }

.apd-mini-card.is-active-person {
    border-color: #16a34a;
    border-top-color: #16a34a;
    box-shadow: 0 0 0 3px rgba(22,163,74,.2), 0 8px 24px rgba(22,101,52,.15);
    background: linear-gradient(135deg, #f0fdf4, #fff);
}

.apd-mini-initial {
    display: grid;
    place-items: center;
    width: 52px; height: 52px;
    border-radius: 50%;
    font-size: 1.3rem;
    font-weight: 800;
    color: #fff;
    box-shadow: 0 3px 10px rgba(17,24,39,.18);
    flex-shrink: 0;
}
.apd-mini-card.gender-male   .apd-mini-initial { background: linear-gradient(135deg, #3b82f6, #60a5fa); }
.apd-mini-card.gender-female .apd-mini-initial { background: linear-gradient(135deg, #ec4899, #f472b6); }
.apd-mini-card.gender-other  .apd-mini-initial { background: linear-gradient(135deg, #8b5cf6, #a78bfa); }

.apd-mini-photo {
    width: 52px; height: 52px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid #fff;
    box-shadow: 0 3px 10px rgba(17,24,39,.18);
}

.apd-mini-name {
    font-size: .78rem;
    font-weight: 700;
    text-align: center;
    line-height: 1.2;
    letter-spacing: -.01em;
    max-width: 100%;
    overflow-wrap: anywhere;
}
.apd-mini-years {
    font-size: .68rem;
    color: #6b7280;
    font-weight: 500;
}
.apd-mini-relation {
    display: inline-flex;
    align-items: center;
    padding: .1rem .4rem;
    border-radius: 30px;
    font-size: .64rem;
    font-weight: 700;
    letter-spacing: .02em;
    text-transform: uppercase;
}
.apd-mini-card.gender-male   .apd-mini-relation { background: #eff6ff; color: #1d4ed8; }
.apd-mini-card.gender-female .apd-mini-relation { background: #fdf2f8; color: #be185d; }
.apd-mini-card.gender-other  .apd-mini-relation { background: #f5f3ff; color: #6d28d9; }
.apd-mini-card.is-active-person .apd-mini-relation { background: #dcfce7; color: #14532d; }

/* ── Active person highlight star ── */
.apd-active-star {
    position: absolute;
    top: -8px; right: -8px;
    font-size: 1rem;
    filter: drop-shadow(0 2px 4px rgba(245,158,11,.4));
}

/* ── Empty row placeholder ── */
.apd-empty-row {
    color: #9ca3af;
    font-size: .78rem;
    font-weight: 500;
    padding: .35rem .75rem;
    font-style: italic;
}

/* ── Diagram toolbar ── */
.apd-toolbar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: .5rem;
    padding: .75rem 1.4rem .65rem;
    border-top: 1px solid #f0f0f0;
    background: #fafafa;
    flex-shrink: 0;
    flex-wrap: wrap;
}
.apd-toolbar-btn {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    padding: .38rem .75rem;
    background: #fff;
    border: 1.5px solid #e5e7eb;
    border-radius: 8px;
    font-size: .78rem;
    font-weight: 600;
    color: #374151;
    cursor: pointer;
    font-family: inherit;
    transition: all .15s ease;
    text-decoration: none;
}
.apd-toolbar-btn:hover {
    border-color: #166534;
    color: #166534;
    background: #f0fdf4;
    transform: translateY(-1px);
}
.apd-toolbar-btn.btn-primary {
    background: #166534;
    border-color: #166534;
    color: #fff;
}
.apd-toolbar-btn.btn-primary:hover {
    background: #15803d;
    color: #fff;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(22,101,52,.3);
}

/* ── Diagram button in toolbar ── */
.btn-diagram {
    background: linear-gradient(135deg, #166534, #16a34a);
    border: none;
    color: #fff;
    font-weight: 700;
    box-shadow: 0 4px 14px rgba(22,101,52,.3);
}
.btn-diagram:hover {
    background: #147337;
    color: #fff !important;
    transform: translateY(-1px);
    box-shadow: 0 6px 18px rgba(22,101,52,.4);
}

/* ══════════════════════════════════════════════════════════════════════════════
   ENHANCED GROUP BUILDER — extra templates
   ══════════════════════════════════════════════════════════════════════════════ */

.gbp-template-tabs {
    display: flex;
    gap: .3rem;
    flex-wrap: wrap;
    margin-bottom: .65rem;
}
.gbp-tab-btn {
    padding: .25rem .5rem;
    border: 1.5px solid #e5e7eb;
    border-radius: 20px;
    background: #f9fafb;
    font-size: .68rem;
    font-weight: 700;
    color: #6b7280;
    cursor: pointer;
    font-family: inherit;
    transition: all .15s ease;
    white-space: nowrap;
}
.gbp-tab-btn:hover, .gbp-tab-btn.active {
    border-color: var(--accent);
    color: var(--accent);
    background: #f0fdf4;
}
.gbp-tab-btn.active {
    background: var(--accent);
    color: #fff;
}

.gbp-siblings-row {
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
    justify-content: center;
    margin-bottom: .5rem;
}

.gbp-grandparent-grid {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: .4rem;
    margin-bottom: .5rem;
}
.gbp-gp-side {
    display: flex;
    flex-direction: column;
    gap: .4rem;
}
.gbp-gp-divider {
    color: #d1d5db;
    font-size: 1.2rem;
    text-align: center;
}

.gbp-grandchildren-row {
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
    justify-content: center;
    margin-bottom: .5rem;
}

/* ══════════════════════════════════════════════════════════════════════════════
   SIDEBAR LAYOUT
   ══════════════════════════════════════════════════════════════════════════════ */

.app-layout-wrapper {
    display: flex;
    min-height: 100vh;
    background: #fdfaf6;
    overflow-x: hidden;
}

/* Sidebar */
.app-sidebar {
    width: 250px;
    background: #fff;
    border-right: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 1040;
    transition: transform 0.3s ease;
    box-shadow: 2px 0 8px rgba(0,0,0,0.05);
}

.sidebar-closed .app-sidebar {
    transform: translateX(-100%);
}

.sidebar-header {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.25rem;
    border-bottom: 1px solid #e5e7eb;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem 0;
}

.sidebar-nav {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.sidebar-nav .nav-item {
    padding: 0 1rem;
}

.sidebar-nav .nav-link {
    display: flex;
    align-items: center;
    padding: 0.6rem 1rem;
    color: #4b5563;
    font-weight: 600;
    font-size: 0.85rem;
    border-radius: 8px;
    transition: all 0.2s;
}

.sidebar-nav .nav-link:hover {
    background: #f3f4f6;
    color: #111827;
}

.sidebar-nav .nav-section-title {
    padding: 1rem 1.25rem 0.25rem;
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 800;
    color: #9ca3af;
}

.sidebar-footer {
    padding: 1rem;
    border-top: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.sidebar-footer .user-info {
    display: flex;
    flex-direction: column;
}

.sidebar-footer .user-name {
    font-weight: 600;
    color: #111827;
    text-decoration: none;
}

.sidebar-close-btn {
    background: none;
    border: none;
    color: #9ca3af;
    font-size: 1.25rem;
    padding: 0.25rem;
    cursor: pointer;
}

/* Main Content */
.app-main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin-left: 250px;
    min-height: 100vh;
    transition: margin-left 0.3s ease;
    width: 100%;
}

.sidebar-closed .app-main-content {
    margin-left: 0;
}

.app-topbar {
    height: 60px;
    background: #fff;
    border-bottom: 1px solid #e5e7eb;
    display: flex;
    align-items: center;
    padding: 0 1.25rem;
    position: sticky;
    top: 0;
    z-index: 1030;
}

.sidebar-toggle {
    background: none;
    border: none;
    color: #4b5563;
    padding: 0.5rem;
    margin-left: -0.5rem;
    margin-right: 0.75rem;
    cursor: pointer;
    border-radius: 6px;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.sidebar-toggle:hover {
    background: #f3f4f6;
    color: #111827;
}

.topbar-brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 800;
    color: var(--ink-1);
    font-size: 1.1rem;
}

.topbar-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 1rem;
}

/* Mobile Adjustments */
@media (max-width: 991.98px) {
    .app-sidebar {
        transform: translateX(-100%);
    }
    
    .sidebar-open .app-sidebar {
        transform: translateX(0);
    }

    .app-main-content {
        margin-left: 0;
    }
    
    .sidebar-closed .app-main-content {
        margin-left: 0;
    }
}

