:root {
    --bg-color: #0f172a;
    /* Slate 900 */
    --sidebar-bg: #1e293b;
    /* Slate 800 */
    --card-bg: #334155;
    /* Slate 700 */
    --text-primary: #f8fafc;
    /* Slate 50 */
    --text-secondary: #94a3b8;
    /* Slate 400 */
    --accent-color: #8b5cf6;
    /* Violet 500 */
    --accent-hover: #7c3aed;
    /* Violet 600 */
    --success-color: #10b981;
    --border-color: #475569;
}

/* Custom Scrollbar Styling (Forces visibility on Mobile/Webkit) */
::-webkit-scrollbar {
    width: 12px;
    height: 12px;
    background-color: var(--bg-color);
    /* Explicit background */
}

::-webkit-scrollbar-track {
    background: var(--bg-color);
    border-radius: 6px;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 6px;
    border: 3px solid var(--bg-color);
    /* Creates padding effect */
}

::-webkit-scrollbar-thumb:hover {
    background: var(--accent-color);
}

/* Firefox Scrollbar */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--border-color) var(--bg-color);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Outfit', sans-serif;
}

/* Global Form Inputs */
input,
select,
textarea,
.form-input {
    background-color: var(--card-bg);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px;
    outline: none;
    width: 100%;
}

select option {
    background-color: var(--bg-color);
    color: var(--text-primary);
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--accent-color);
}

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    overflow: hidden;
    /* App-like feel */
}

a {
    text-decoration: none;
    color: inherit;
}

ul {
    list-style: none;
}

/* Hide mobile nav by default */
.mobile-bottom-nav {
    display: none;
}

.app-container {
    display: flex;
    height: 100vh;
}

/* Sidebar */
.sidebar {
    width: 260px;
    background-color: var(--sidebar-bg);
    padding: 2rem;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    flex-shrink: 0;
    z-index: 20;
    /* Ensure it stays above main content if needed for shadows */
}

.sidebar-toggle-btn {
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 1.5rem;
    cursor: pointer;
    margin-right: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px;
    border-radius: 8px;
    transition: background 0.3s;
}

.sidebar-toggle-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.logo-text {
    font-size: 1.2rem;
    font-weight: 700;
    margin: 0;
    white-space: nowrap;
    opacity: 1;
    transition: opacity 0.3s;
}

.sidebar-logo-container {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 2rem;
    transition: all 0.3s;
    height: 40px;
    overflow: hidden;
}

.logo-img {
    height: 32px;
    width: auto;
    object-fit: contain;
}

.mobile-close-btn {
    display: none;
    /* Hidden on desktop */
    position: absolute;
    top: 20px;
    right: 20px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    font-size: 1.2rem;
    cursor: pointer;
    z-index: 1001;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.mobile-close-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
}

.sidebar-logo-container.text-logo {
    height: auto;
    min-height: 40px;
    display: block;
    /* Allow wrapping */
}

.sidebar-logo-container.text-logo .logo-text {
    white-space: normal;
    line-height: 1.2;
}

.highlight {
    color: var(--accent-color);
}

.nav-links li {
    margin-bottom: 0.5rem;
}

.nav-links a {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 12px 16px;
    border-radius: 12px;
    color: var(--text-secondary);
    transition: all 0.3s ease;
    font-size: 0.95rem;
}

.nav-links a:hover,
.nav-links li.active a {
    background-color: rgba(139, 92, 246, 0.1);
    color: var(--accent-color);
}

.nav-links i {
    font-size: 1.2rem;
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.top-bar {
    height: 80px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 2rem;
    background-color: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(10px);
    position: sticky;
    top: 0;
    z-index: 10;
}

.search-bar {
    display: flex;
    align-items: center;
    background-color: var(--sidebar-bg);
    padding: 10px 20px;
    border-radius: 50px;
    border: 1px solid var(--border-color);
    width: 300px;
}

.search-bar i {
    color: var(--text-secondary);
    margin-right: 10px;
}

.search-bar input {
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    width: 100%;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 10px;
}

.user-profile img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid var(--accent-color);
}

.content-wrapper {
    padding: 2rem;
}

/* Dashboard Cards */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.card {
    background-color: var(--sidebar-bg);
    border-radius: 16px;
    padding: 1.5rem;
    border: 1px solid var(--border-color);
    transition: transform 0.2s ease;
}

.card:hover {
    transform: translateY(-5px);
    border-color: var(--accent-color);
}

.card h3 {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
    font-weight: 400;
}

.card .value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--text-primary);
}

.card .icon-wrapper {
    float: right;
    background: rgba(139, 92, 246, 0.1);
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    color: var(--accent-color);
    font-size: 1.2rem;
}

/* Family Tree specific styles */
.tree-container {
    width: 100%;
    overflow-x: auto;
    text-align: center;
}

.tree-scroll-container {
    overflow: auto;
    width: 100%;
    height: 75vh;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background-color: var(--sidebar-bg);
    position: relative;
    cursor: grab;

    /* Mobile scrolling momentum */
    -webkit-overflow-scrolling: touch;

    /* Ensure nice scrollbars for this container specifically */
    scrollbar-color: var(--accent-color) var(--sidebar-bg);
    scrollbar-width: auto;
    /* Force standard width, not thin */
}

/* WebKit specifics for this container to override defaults */
.tree-scroll-container::-webkit-scrollbar {
    width: 12px;
    height: 12px;
    display: block;
    /* Force display */
    background: var(--sidebar-bg);
}

.tree-scroll-container::-webkit-scrollbar-track {
    background: var(--sidebar-bg);
    border-radius: 12px;
}

.tree-scroll-container::-webkit-scrollbar-corner {
    background: var(--sidebar-bg);
}

.tree-scroll-container::-webkit-scrollbar-thumb {
    background-color: var(--text-secondary);
    border-radius: 6px;
    border: 3px solid var(--sidebar-bg);
}

.tree-scroll-container::-webkit-scrollbar-thumb:hover {
    background-color: var(--accent-color);
}

.tree-scroll-container:active {
    cursor: grabbing;
}

.tree {
    display: inline-block;
    white-space: nowrap;
}

.tree ul {
    padding-top: 20px;
    position: relative;
    transition: all 0.5s;
    display: flex;
    justify-content: center;
}

.tree li {
    float: left;
    text-align: center;
    list-style-type: none;
    position: relative;
    padding: 20px 5px 0 5px;
    transition: all 0.5s;
}

/* We will use ::before and ::after to draw the connectors */
.tree li::before,
.tree li::after {
    content: '';
    position: absolute;
    top: 0;
    right: 50%;
    border-top: 1px solid var(--text-secondary);
    width: 50%;
    height: 20px;
}

.tree li::after {
    right: auto;
    left: 50%;
    border-left: 1px solid var(--text-secondary);
}

/* We need to remove left-right connectors from elements without any siblings */
.tree li:only-child::after,
.tree li:only-child::before {
    display: none;
}

/* Remove space from the top of single children */
.tree li:only-child {
    padding-top: 0;
}

/* Remove left connector from first child and right connector from last child */
.tree li:first-child::before,
.tree li:last-child::after {
    border: 0 none;
}

/* Adding back the vertical connector to the last nodes */
.tree li:last-child::before {
    border-right: 1px solid var(--text-secondary);
    border-radius: 0 5px 0 0;
}

.tree li:first-child::after {
    border-radius: 5px 0 0 0;
}

/* Time to add downward connectors from parents */
.tree ul ul::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    border-left: 1px solid var(--text-secondary);
    width: 0;
    height: 20px;
}

.tree-member-card {
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    padding: 10px;
    border-radius: 8px;
    display: inline-block;
    min-width: 120px;
    position: relative;
    z-index: 2;
    transition: all 0.3s;
    text-decoration: none;
    color: var(--text-primary);
}

.tree-member-card:hover {
    background-color: var(--sidebar-bg);
    border-color: var(--accent-color);
    transform: scale(1.05);
}

.tree-member-card img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-bottom: 5px;
    border: 2px solid var(--accent-color);
}

.tree-member-card .name {
    font-weight: 600;
    font-size: 0.9rem;
    display: block;
}

.tree-member-card .dates {
    font-size: 0.7rem;
    color: var(--text-secondary);
    display: block;
}

.spouse-container {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.spouse-connector {
    height: 2px;
    width: 20px;
    background-color: #ef4444;
}

/* Visual Updates for Tree Nodes */
.tree-member-card.is-dead img {
    filter: grayscale(100%);
    border-color: #64748b;
}

.rip-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: black;
    color: white;
    font-size: 0.6rem;
    padding: 2px 5px;
    border-radius: 4px;
    font-weight: bold;
}

.social-icons {
    margin-top: 5px;
    display: flex;
    justify-content: center;
    gap: 5px;
    opacity: 0.7;
}

.social-icons i {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

/* Responsive Design */
@media (max-width: 768px) {

    /* Sidebar - Off-canvas on Mobile */
    .sidebar {
        /* display: none; Removed - we want it available */
        position: fixed;
        left: 0;
        top: 0;
        height: 100vh;
        z-index: 1000;
        transform: translateX(-100%);
        transition: transform 0.3s ease-in-out;
        width: 260px;
        /* Standard width */
        box-shadow: 4px 0 15px rgba(0, 0, 0, 0.3);
    }

    .mobile-close-btn {
        display: flex;
        /* Flex to center icon */
    }

    .sidebar.active {
        transform: translateX(0);
    }

    /* Overlay styles */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(2px);
        z-index: 999;
    }

    .sidebar-overlay.active {
        display: block;
    }

    .main-content {
        margin-left: 0;
        height: calc(100vh - 70px);
        /* Space for bottom nav */
    }

    /* Mobile Bottom Nav Styles */
    .mobile-bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 70px;
        background-color: var(--sidebar-bg);
        border-top: 1px solid var(--border-color);
        justify-content: space-around;
        align-items: center;
        z-index: 900;
        padding-bottom: env(safe-area-inset-bottom);
    }

    .mobile-bottom-nav .nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: var(--text-secondary);
        font-size: 0.75rem;
        gap: 4px;
        text-decoration: none;
        width: 100%;
        height: 100%;
    }

    .mobile-bottom-nav .nav-item i {
        font-size: 1.5rem;
        margin-bottom: 2px;
    }

    .mobile-bottom-nav .nav-item.active {
        color: var(--accent-color);
    }

    .top-bar {
        padding: 0 1rem;
        height: 60px;
        position: relative;
    }

    #sidebarToggle {
        display: flex;
    }

    .search-bar {
        width: 100%;
        margin-bottom: 10px;
        display: none;
        /* Hide search bar on mobile to save space, or adjust */
    }

    /* Show search icon or smaller search? For now hide to prevent layout break or keep if fits */
    /* Let's keep search hidden or minimal */

    .content-wrapper {
        padding: 1rem;
        padding-bottom: 90px;
        /* Ensure content doesn't get hidden behind nav */
    }

    /* Adjust grid forms to single column */
    .stats-grid {
        grid-template-columns: 1fr;
    }
}

/* Horizontal Tree Layout */
.tree-horizontal .tree {
    display: flex;
    justify-content: center;
}

.tree-horizontal .tree ul {
    flex-direction: column;
    /* Stack children vertically */
    padding-top: 0;
    padding-left: 20px;
    /* Space for connectors on left */
    align-items: flex-start;
}

.tree-horizontal .tree li {
    float: none;
    text-align: left;
    padding: 10px 0 10px 20px;
    /* Space between nodes vertically */
    display: flex;
    align-items: center;
}

/* Connectors for Horizontal */
.tree-horizontal .tree li::before,
.tree-horizontal .tree li::after {
    top: 50%;
    left: 0;
    width: 20px;
    height: 50%;
    border-top: 1px solid var(--text-secondary);
    border-left: 1px solid var(--text-secondary);
    right: auto;
}

.tree-horizontal .tree li::after {
    top: 50%;
    left: 0;
    height: 100%;
    border-top: 0;
}

/* Remove connectors for single children */
.tree-horizontal .tree li:only-child::after,
.tree-horizontal .tree li:only-child::before {
    display: none;
    /* Or maybe just a single dash? */
}

/* Revert standard specific rules */
.tree-horizontal .tree li:first-child::before,
.tree-horizontal .tree li:last-child::after {
    border: 0 none;
}

/* Adjust connector specific to horizontal */
.tree-horizontal .tree li:only-child {
    padding-left: 0;
    /* No connector space needed if root? No, children need it */
}

/* Fix connectors logic for horizontal is distinct. 
   Top-down: Parent -> Children (row). Connectors above.
   Left-Right: Parent -> Children (column). Connectors on left.
*/

/* Resetting default tree styles for horizontal override */
.tree-horizontal .tree li::before {
    right: auto;
    top: 0;
    bottom: 50%;
    height: 50%;
    width: 20px;
    border-top: 0;
    border-bottom: 1px solid var(--text-secondary);
    border-left: 1px solid var(--text-secondary);
}

.tree-horizontal .tree li::after {
    right: auto;
    top: 50%;
    bottom: 0;
    height: 100%;
    width: 20px;
    border-top: 0;
    border-left: 1px solid var(--text-secondary);
}

.tree-horizontal .tree li:first-child::before {
    border-left: 0;
}

.tree-horizontal .tree li:last-child::after {
    border-left: 0;
}

.tree-horizontal .tree li:only-child::after,
.tree-horizontal .tree li:only-child::before {
    display: none;
}

/* Parent Connector (Right side of parent to Left side of children group) */
.tree-horizontal .tree ul ul::before {
    top: 50%;
    left: 0;
    width: 20px;
    height: 0;
    border-left: 0;
    border-top: 1px solid var(--text-secondary);
}

.tree-horizontal .tree-member-card {
    margin-right: 20px;
}

/* Fix spouse container for horizontal */
.tree-horizontal .spouse-container {
    flex-direction: row;
    /* Keep spouses side-by-side */
}

/* Collapsed Sidebar */
@media (min-width: 769px) {
    .sidebar {
        transition: width 0.3s ease;
    }

    .sidebar.collapsed {
        width: 70px;
        padding: 2rem 10px;
        /* Reduced padding */
    }

    .sidebar.collapsed .logo-text {
        display: none;
        opacity: 0;
    }

    .sidebar.collapsed .sidebar-logo-container {
        justify-content: center;
        gap: 0;
        margin-bottom: 2rem;
    }

    .sidebar.collapsed .logo-img {
        margin-right: 0;
        height: 32px;
    }

    .sidebar.collapsed .link-text {
        display: none;
    }

    .sidebar.collapsed .nav-links a {
        justify-content: center;
        padding: 12px;
    }

    .sidebar.collapsed .nav-links a i {
        margin-right: 0;
    }
}

/* --- Tree Dynamic Colors by Generation --- */
.tree-member-card {
    border-top: 3px solid transparent;
}

/* Default */
.gen-0 .tree-member-card {
    border-top: 3px solid #60a5fa;
}

/* Blue */
.gen-1 .tree-member-card {
    border-top: 3px solid #f472b6;
}

/* Pink */
.gen-2 .tree-member-card {
    border-top: 3px solid #4ade80;
}

/* Green */
.gen-3 .tree-member-card {
    border-top: 3px solid #facc15;
}

/* Yellow */
.gen-4 .tree-member-card {
    border-top: 3px solid #fb923c;
}

/* Orange */
/* Repeat or fallback generic */

/* Toggle Button */
.toggle-btn {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    position: absolute;
    bottom: -30px;
    /* Position betweeen card and connector */
    left: 50%;
    transform: translateX(-50%);
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.toggle-btn:hover {
    border-color: var(--accent-color);
    color: var(--accent-color);
    transform: translateX(-50%) scale(1.2);
}

.toggle-btn.collapsed {
    background: var(--accent-color);
    color: white;
}

/* Hide children when toggled */
.children-hidden {
    display: none !important;
}

.children-hidden+ul {
    display: none;
}

/* Tree Page Header Responsive */
.tree-page-header {
    margin-bottom: 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

@media (max-width: 768px) {
    .tree-page-header {
        margin-bottom: 1rem;
        flex-direction: column;
        align-items: stretch;
    }

    .tree-page-header>div {
        width: 100%;
    }

    .tree-controls {
        width: 100%;
        justify-content: space-between;
    }
}