:root {
    --bg-color: #F8F5F2;
    --text-dark: #3D3228;
    --text-light: #706355; 
    --line-color: #D6C7B5;
    --card-bg: rgba(255, 253, 250, 0.45);
    --card-border: rgba(255, 255, 255, 0.8);
    --accent-glow: rgba(214, 199, 181, 0.3);
    --highlight-color: #A38A70;
    
    /* Standardized Graph Dimensions */
    --card-width: 180px;
    --card-height: 85px;
    --spouse-gap: 40px;
}

*, *::before, *::after { 
    box-sizing: border-box; 
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -webkit-tap-highlight-color: transparent; 
}

body {
    margin: 0;
    padding: 0;
    background-color: var(--bg-color);
    background-image: 
        radial-gradient(var(--accent-glow) 2px, transparent 2px),
        linear-gradient(135deg, rgba(214,199,181,0.1) 0%, transparent 100%);
    background-size: 30px 30px, 100% 100%;
    background-attachment: fixed;
    font-family: 'Plus Jakarta Sans', sans-serif;
    color: var(--text-dark);
    overflow: hidden; 
}

/* --- Engine Layout --- */
#viewport {
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    cursor: grab;
    position: relative;
    touch-action: none; 
}

#viewport:active {
    cursor: grabbing;
}

#canvas {
    position: absolute;
    transform-origin: 0 0;
    top: 50vh; 
    left: 50vw;
}

#tree-svg {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
    overflow: visible;
    z-index: 1;
}

.link-path {
    fill: none;
    stroke: var(--line-color);
    stroke-width: 1.5px;
    stroke-dasharray: var(--path-length, 2000);
    stroke-dashoffset: var(--path-length, 2000);
    animation: drawLine 1.2s cubic-bezier(0.25, 1, 0.5, 1) forwards;
    transition: stroke 0.3s ease, stroke-width 0.3s ease, opacity 0.5s ease;
}

.spouse-link.divorced {
    stroke-dasharray: 8 8 !important;
    stroke-dashoffset: 0 !important;
    opacity: 0;
    animation: fadeIn 0.5s ease forwards;
}

#tree-nodes {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
}

/* --- Cards --- */
.person-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    width: var(--card-width);
    height: var(--card-height);
    flex-shrink: 0;
    border-radius: 20px;
    color: var(--text-dark);
    cursor: pointer;
    position: absolute;
    z-index: 2;
    -webkit-tap-highlight-color: transparent !important;
    outline: none;
    
    /* Force Hardware Acceleration for cleaner anti-aliasing */
    transform: translateZ(0);
    -webkit-font-smoothing: antialiased;
    
    transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1) var(--delay, 0s), 
                opacity 0.5s ease var(--delay, 0s), 
                filter 0.5s ease var(--delay, 0s);
    animation: fadeInScale 0.6s cubic-bezier(0.16, 1, 0.3, 1) both;
}

/* Layer 1: Background, Filter, and Box Shadow */
.person-card::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow: 
        6px 8px 20px rgba(61, 50, 40, 0.06), 
        -4px -4px 12px rgba(255, 255, 255, 0.9),
        inset 0 0 4px rgba(255, 255, 255, 0.5);
    z-index: -1;
    transition: box-shadow 0.3s ease var(--delay, 0s), 
                background 0.3s ease var(--delay, 0s);
}

/* Layer 2: Isolated Border Line (Prevents clipping bugs) */
.person-card::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    border: 1px solid var(--card-border);
    pointer-events: none;
    z-index: 1;
    transition: border-color 0.4s ease var(--delay, 0s);
}

@media (hover: hover) {
    .person-card:hover:not(.highlight-center) {
        transform: translateY(-4px) scale(1.02);
    }
    .person-card:hover:not(.highlight-center)::before {
        background: rgba(255, 253, 250, 0.65);
        box-shadow: 
            10px 14px 28px rgba(61, 50, 40, 0.1), 
            -6px -6px 16px rgba(255, 255, 255, 0.95);
    }
}

.person-card:active:not(.highlight-center) {
    transform: translateY(1px) scale(0.98) !important;
    transition: transform 0.1s ease;
}

.person-card .name {
    font-family: 'Playfair Display', serif;
    font-weight: 600;
    font-size: 1.05rem;
    margin-bottom: 2px;
    color: var(--text-dark);
    width: 100%;
    text-align: center;
    padding: 5px 10px;
    line-height: 1.2;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    z-index: 2;
}

.person-card .dates {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-light);
    font-weight: 600;
    min-height: 12px;
    z-index: 2;
}

#status-msg {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: var(--text-light);
    font-style: italic;
    z-index: 10;
}

/* --- Dynamic Lineage Highlighting --- */

/* 1. Canvas Dimming */
#canvas.is-highlighting .person-card:not(.highlight-center):not(.highlight-node) {
    opacity: 0.35;
    filter: grayscale(65%);
}

#canvas.is-highlighting .person-card:not(.highlight-center):not(.highlight-node)::before {
    box-shadow: none;
}

#canvas.is-highlighting .person-card:not(.highlight-center):not(.highlight-node)::after {
    border-color: rgba(255, 255, 255, 0.3);
}

#canvas.is-highlighting .link-path {
    opacity: 0.15;
}

/* 2. Focused Node Styling */
.person-card.highlight-node {
    opacity: 1 !important;
    z-index: 6;
}

.person-card.highlight-node::before {
    box-shadow: 0 4px 15px rgba(163, 138, 112, 0.25), inset 0 0 5px rgba(255,255,255,0.8) !important;
}

.person-card.highlight-node::after {
    border-color: rgba(163, 138, 112, 0.3) !important;
}

.person-card.highlight-center {
    opacity: 1 !important;
    z-index: 10;
}

.person-card.highlight-center::before {
    box-shadow: 0 10px 25px rgba(61, 50, 40, 0.15), inset 0 0 6px rgba(255,255,255,0.9);
}

.person-card.highlight-center::after {
    border-color: transparent !important;
}

/* 3. SVG Flow Highlight Layer */
.flow-line {
    fill: none;
    stroke: var(--highlight-color);
    stroke-width: 3px;
    stroke-linecap: butt;
}

.flow-line.source {
    stroke: #D4AF37;
    stroke-width: 4px;
    filter: drop-shadow(0 0 6px rgba(212, 175, 55, 0.5));
}

/* --- Keyframes --- */
@keyframes drawLine {
    to { stroke-dashoffset: 0; }
}

@keyframes flowDraw {
    to { stroke-dashoffset: 0; }
}

@keyframes fadeInScale {
    from { opacity: 0; transform: scale(0.9) translateY(15px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes fadeIn {
    to { opacity: 1; }
}

/* --- UI Controls & Legend --- */
.ui-left {
    position: absolute;
    bottom: 30px; 
    left: 30px;
    display: flex; 
    gap: 15px; 
    z-index: 10;
    align-items: flex-end;
}

.ui-right {
    position: absolute;
    bottom: 30px; 
    right: 30px;
    display: flex; 
    gap: 15px; 
    z-index: 10;
    align-items: flex-end;
}

.ctrl-group {
    display: flex; 
    gap: 10px;
}

.legend-wrapper {
    display: flex;
    flex-direction: column;
    align-items: flex-end; 
    gap: 10px;
}

.legend-panel {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 12px 16px;
    box-shadow: 2px 4px 10px rgba(61, 50, 40, 0.05);
    backdrop-filter: blur(5px);
    font-size: 0.85rem;
    display: none; 
    flex-direction: column;
    gap: 8px;
    transform-origin: bottom right;
    animation: fadeInScale 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.legend-panel.show {
    display: flex;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-dark);
    font-weight: 500;
}

.legend-line {
    width: 24px;
    height: 2px;
    background: var(--line-color);
}

.legend-line.dashed {
    background: transparent;
    border-top: 2px dashed var(--line-color);
}

.ctrl-btn {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 8px 12px;
    cursor: pointer;
    color: var(--text-dark);
    font-family: inherit;
    font-weight: 600;
    box-shadow: 2px 4px 10px rgba(61, 50, 40, 0.05);
    backdrop-filter: blur(5px);
    transition: background 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
    outline: none;
    -webkit-tap-highlight-color: transparent !important;
}

@media (hover: hover) {
    .ctrl-btn:hover {
        background: rgba(255, 255, 255, 0.85);
        transform: translateY(-2px);
        box-shadow: 4px 6px 14px rgba(61, 50, 40, 0.08);
    }
}

.ctrl-btn:active {
    transform: translateY(1px) scale(0.95) !important;
    background: rgba(230, 220, 210, 0.8);
    box-shadow: 1px 2px 5px rgba(61, 50, 40, 0.05) !important;
}

/* Mobile Layout */
@media (max-width: 768px) {
    .ui-left {
        left: 15px;
        bottom: 15px;
    }
    .ui-right {
        right: 15px;
        bottom: 15px;
    }
    .ctrl-btn {
        padding: 6px 10px;
        font-size: 0.9rem;
    }
}