/* --- SHARED VARIABLES --- */
:root {
    --sidebar-collapsed-width: 80px;   /* Width when collapsed */
    --sidebar-expanded-width: 250px;   /* Width when hovered */
    --mobile-sidebar-width: 250px;     /* Width for mobile slide-out */
    --mobile-topbar-height: 56px;
}

/* --- MOBILE-FIRST STYLES (Default) --- */

body {
    /* Add padding for the fixed mobile top bar */
    padding-top: var(--mobile-topbar-height);
    transition: background-color 0.3s ease;
}

/* The mobile hamburger bar */
.mobile-navbar {
    z-index: 1031; /* Above sidebar (THIS IS THE KEY) */
}

/* The overlay for mobile */
#sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 1039; /* Below sidebar, above content */
    display: none; /* Hidden by default */
    transition: opacity 0.3s ease;
    opacity: 0;
}

#sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: var(--mobile-sidebar-width); /* Uses mobile width */
    background-color: #343a40;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center; /* Vertically centers link group */

    /* --- THIS IS THE FIX (PART 1) --- */
    z-index: 1030; /* <-- WAS 1040. Lowered to be BEHIND the navbar. */

    /* Hide off-screen by default */
    transform: translateX(-100%);
    transition: transform 0.3s ease;
}

/* --- MOBILE "OPEN" STATE STYLES --- */
body.sidebar-open {
    overflow: hidden; /* Prevent scrolling of main content */
}

body.sidebar-open #sidebar {
    /* Slide sidebar in */
    transform: translateX(0);
    
    /* --- THIS IS THE FIX (PART 2) --- */
    z-index: 1040; /* <-- ADDED: Now it's on top, but ONLY when open. */
}

body.sidebar-open #sidebar-overlay {
    /* Show the overlay */
    display: block;
    opacity: 1;
}

/* --- SIDEBAR CONTENT STYLES (Shared) --- */

#sidebar .navbar-nav {
    flex-direction: column;
    width: 100%;
}

/* Default link style (for mobile AND desktop-expanded) */
#sidebar .nav-link {
    display: flex;
    align-items: center;
    white-space: nowrap;
    color: rgba(255, 255, 255, 0.7);
    padding: 12px 1rem; 
    transition: all 0.3s ease;
}

#sidebar .nav-link:hover {
    color: #fff;
    background-color: #495057;
}

#sidebar .nav-link .fa {
    font-size: 1.5rem;
    min-width: 3rem; 
    text-align: center;
    margin-right: 0.5rem;
}

/* On mobile, text is ALWAYS visible */
#sidebar .nav-link .nav-text {
    display: inline;
    font-size: 1.1rem;
}

/* --- MAIN CONTENT STYLES (Mobile) --- */

#main-content {
    /* No margin on mobile */
    margin-left: 0;
    padding: 20px;
    transition: margin-left 0.3s ease;
}

/* --- DESKTOP-ONLY STYLES (Overrides) --- */

@media (min-width: 992px) { /* 'lg' breakpoint */

    /* Reset body padding */
    body {
        padding-top: 0;
    }

    /* Hide mobile-specific elements on desktop */
    .mobile-navbar,
    #sidebar-overlay {
        display: none !important;
    }

    /* Restore sidebar desktop styles */
    #sidebar {
        transform: none; /* Reset mobile transform */
        transition: width 0.3s ease; /* Change transition back to width */
        width: var(--sidebar-collapsed-width); /* Back to collapsed width */
        z-index: 1030; /* This z-index is fine for desktop */
    }

    #sidebar:hover {
        width: var(--sidebar-expanded-width);
    }

    /* Hide text when collapsed on desktop */
    #sidebar .nav-link .nav-text {
        display: none;
    }

    /* Show text on hover on desktop */
    #sidebar:hover .nav-link .nav-text {
        display: inline;
    }

    /* Restore main content push for desktop */
    #main-content {
        margin-left: var(--sidebar-collapsed-width);
    }

    #sidebar:hover ~ #main-content {
        margin-left: var(--sidebar-expanded-width);
    }

    /* * --- THE DESKTOP CENTERING FIX ---
     * This centers icons ONLY when sidebar is collapsed on desktop.
    */
    #sidebar:not(:hover) .nav-link {
        justify-content: center; /* Horizontally center */
        padding: 12px 0;         /* Remove horizontal padding */
    }

    #sidebar:not(:hover) .nav-link .fa {
        margin-right: 0;   /* Remove margin */
        min-width: auto;   /* Reset min-width */
    }
}