.tree-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
    background-color: var(--bg-color);
}

/* Sidebar */
.sidebar {
    width: 320px;
    background: var(--surface-glass);
    backdrop-filter: blur(12px);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 1.5rem;
    z-index: 20;
    /* Momentum scroll for iOS */
    -webkit-overflow-scrolling: touch;
    overflow-y: auto;
}

.sidebar-header {
    margin-bottom: 2rem;
}

.back-link {
    color: var(--muted);
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
    display: inline-block;
    transition: color 0.2s;
}

.back-link:hover {
    color: var(--primary);
}

.search-box {
    position: relative;
    margin-bottom: 2rem;
}

.search-box input {
    width: 100%;
    background: var(--field-bg);
    border: 1px solid var(--border);
    color: var(--text-color);
    padding: 0.8rem 1rem;
    border-radius: 8px;
    font-family: inherit;
    font-size: 16px; /* Prevent iOS auto-zoom on focus */
    transition: all 0.3s;
    /* Remove 300ms tap delay — keyboard opens immediately */
    touch-action: manipulation;
}

.search-box input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-soft);
}

.search-results {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 8px 8px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 30;
    display: none;
}

.search-result-item {
    padding: 0.8rem 1rem;
    cursor: pointer;
    transition: background 0.2s;
    border-bottom: 1px solid var(--subtle-border);
}

.search-result-item:hover {
    background: var(--primary-tint);
}

.search-result-name {
    font-weight: 500;
    color: var(--primary);
}

.search-result-gen {
    font-size: 0.8rem;
    color: var(--muted);
}

.controls {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 2rem;
}

.btn-icon {
    background: var(--field-bg);
    border: 1px solid var(--border);
    color: var(--text-color);
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    font-size: 1.2rem;
    padding: 0;
    touch-action: manipulation;
}

.btn-icon:hover {
    background: var(--primary);
    color: var(--on-primary);
    border-color: var(--primary);
}

.legend {
    margin-bottom: 2rem;
}

.legend h3 {
    font-size: 1rem;
    color: var(--muted);
    margin-bottom: 1rem;
    font-family: var(--font-body);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    margin-bottom: 0.5rem;
    font-size: 0.9rem;
}

.legend-color {
    width: 16px;
    height: 16px;
    border-radius: 4px;
}

.legend-color.male { background: #3b82f6; }
.legend-color.female { background: #ec4899; }
.legend-line {
    width: 16px;
    height: 2px;
    background: var(--line-color);
}

.node-info {
    background: var(--field-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.5rem;
    margin-top: auto;
}

.node-info.hidden {
    display: none;
}

.node-info h3 {
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.node-meta {
    font-size: 0.85rem;
    color: var(--muted);
    margin-bottom: 1rem;
}

.node-spouses {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--subtle-border);
}

.node-spouses h4 {
    font-size: 0.9rem;
    color: var(--muted-strong);
    margin-bottom: 0.5rem;
}

.node-spouses ul {
    list-style: none;
    font-size: 0.9rem;
}

/* Main Tree Area */
.tree-main {
    flex: 1;
    position: relative;
    cursor: grab;
    /* Prevent browser from intercepting touch events intended for D3 zoom/pan */
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

.tree-main:active {
    cursor: grabbing;
}

#d3-container {
    width: 100%;
    height: 100%;
    /* Required for D3 touch zoom to work — override any inherited touch-action */
    touch-action: none;
}

#d3-container svg {
    /* Prevent iOS callout & selection on long-press */
    -webkit-touch-callout: none;
    touch-action: none;
}

.breadcrumb {
    position: absolute;
    top: 1.5rem;
    left: 1.5rem;
    background: var(--surface-glass);
    backdrop-filter: blur(8px);
    padding: 0.8rem 1.5rem;
    border-radius: 9999px;
    border: 1px solid var(--border);
    z-index: 10;
    display: flex;
    gap: 0.5rem;
    align-items: center;
    font-size: 0.9rem;
}

.breadcrumb-item {
    cursor: pointer;
    color: var(--muted);
    transition: color 0.2s;
}

.breadcrumb-item:hover {
    color: var(--primary);
}

.breadcrumb-separator {
    color: var(--line-color);
}

.breadcrumb-current {
    color: var(--primary);
    font-weight: 500;
}

.loading-overlay {
    position: absolute;
    inset: 0;
    background: var(--bg-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 50;
    transition: opacity 0.3s;
}

.loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(203, 160, 82, 0.2);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 1rem;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* D3 Elements */
.link {
    fill: none;
    stroke: var(--line-color);
    stroke-width: 2px;
    transition: stroke 0.3s;
}

.link.highlight {
    stroke: var(--primary);
    stroke-width: 3px;
}

.node-box {
    border: 2px solid var(--border);
    border-radius: 8px;
    padding: 10px;
    background: var(--node-bg);
    box-shadow: var(--soft-shadow);
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    box-sizing: border-box;
    cursor: pointer;
    transition: all 0.2s;
}

.box-son {
    border-left: 5px solid #3b82f6;
}

.box-daughter {
    border-left: 5px solid #ec4899;
}

.node-box:hover {
    border-color: var(--primary);
    background: var(--node-hover-bg);
}

.node.selected .node-box {
    border-color: var(--primary);
    box-shadow: 0 0 15px var(--primary-shadow-strong);
}

.person-name {
    font-size: 14px;
    font-weight: 700;
    margin-bottom: 4px;
    font-family: var(--font-body);
}

.person-name.male { color: #60a5fa; }
.person-name.female { color: #f472b6; }

.spouses-list {
    margin-top: 6px;
    padding-top: 6px;
    border-top: 1px dashed var(--subtle-border);
}

.spouse {
    font-size: 12px;
    font-weight: 500;
    color: #f472b6; /* pink for wives */
    margin-bottom: 2px;
}

.meta-info {
    font-size: 11px;
    color: var(--muted);
}

.node-notes {
    font-size: 11px;
    color: var(--muted-strong);
    margin-top: 6px;
    font-style: italic;
    background: var(--note-bg);
    padding: 6px;
    border-radius: 4px;
    line-height: 1.4;
}

/* Sidebar Toggle Button — hidden on desktop */
.btn-toggle-sidebar {
    display: none;
    position: absolute;
    top: 1rem;
    left: 1rem;
    z-index: 25;
    background: var(--surface-glass-strong);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    color: var(--text-color);
    width: 44px;
    height: 44px;
    border-radius: 10px;
    font-size: 1.4rem;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-toggle-sidebar:hover {
    background: var(--primary);
    color: var(--on-primary);
    border-color: var(--primary);
}

/* Sidebar overlay backdrop — hidden by default */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--overlay-bg);
    z-index: 15;
}

/* ==================== Responsive — Tablet (<=900px) ==================== */
@media (max-width: 900px) {
    .sidebar {
        width: 280px;
        padding: 1.2rem;
    }

    .sidebar-header {
        margin-bottom: 1.2rem;
    }

    .search-box {
        margin-bottom: 1.2rem;
    }

    .controls {
        margin-bottom: 1.2rem;
    }

    .breadcrumb {
        top: 1rem;
        left: 1rem;
        padding: 0.6rem 1rem;
        font-size: 0.8rem;
        max-width: calc(100% - 2rem);
        overflow-x: auto;
        white-space: nowrap;
    }
}

/* ==================== Responsive — Mobile (<=768px) ==================== */
@media (max-width: 768px) {
    .tree-container {
        flex-direction: column;
    }

    /* Show toggle button on mobile */
    .btn-toggle-sidebar {
        display: flex;
    }

    /* Sidebar becomes an overlay drawer */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: 300px;
        max-width: 85vw;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        z-index: 20;
        padding: 1.2rem;
        overflow-y: auto;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-overlay.visible {
        display: block;
    }

    /* Breadcrumb: shift right to avoid overlap with hamburger */
    .breadcrumb {
        top: 1rem;
        left: 4rem;
        right: 1rem;
        padding: 0.5rem 1rem;
        font-size: 0.75rem;
        max-width: none;
        overflow-x: auto;
        white-space: nowrap;
    }

    .search-box input {
        padding: 0.7rem 0.8rem;
        font-size: 0.9rem;
    }

    .search-results {
        max-height: 40vh;
    }

    .search-result-item {
        padding: 0.6rem 0.8rem;
    }

    .controls {
        margin-bottom: 1rem;
    }

    .btn-icon {
        width: 36px;
        height: 36px;
        font-size: 1rem;
    }

    .legend {
        margin-bottom: 1rem;
    }

    .legend h3 {
        font-size: 0.9rem;
        margin-bottom: 0.5rem;
    }

    .legend-item {
        font-size: 0.8rem;
        margin-bottom: 0.3rem;
    }

    .node-info {
        padding: 1rem;
    }

    .node-info h3 {
        font-size: 1rem;
    }
}

/* ==================== Responsive — Small Phone (<=480px) ==================== */
@media (max-width: 480px) {
    .sidebar {
        width: 100vw;
        max-width: 100vw;
    }

    .breadcrumb {
        font-size: 0.7rem;
        padding: 0.4rem 0.8rem;
    }

    .btn-toggle-sidebar {
        width: 40px;
        height: 40px;
        font-size: 1.2rem;
        top: 0.75rem;
        left: 0.75rem;
    }
}
