/* ========== MOBILE SIDEBAR SOLUTION ========== */

/* Variables */
:root {
    --sidebar-width: 280px;
    --sidebar-collapsed-width: 70px;
    --transition-speed: 0.3s;
    --sidebar-bg: #ffffff;
    --sidebar-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    --overlay-bg: rgba(0, 0, 0, 0.5);
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    :root {
        --sidebar-bg: #2d3239;
        --sidebar-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    }
}

/* Container for sidebar - keep it visible */
.sidebar-container {
    position: relative;
    z-index: 1000;
}

/* Main wrapper to handle sidebar offset */
.main-wrapper {
    transition: margin-left var(--transition-speed) ease;
}

/* Desktop Sidebar (768px and up) */
@media (min-width: 768px) {
    .mobile-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: var(--sidebar-width);
        background-color: var(--sidebar-bg);
        box-shadow: var(--sidebar-shadow);
        transition: width var(--transition-speed) ease;
        z-index: 1000;
        overflow-x: hidden;
        overflow-y: auto;
        display: flex;
        flex-direction: column;
    }
    
    .mobile-sidebar.collapsed {
        width: var(--sidebar-collapsed-width);
    }
    
    .mobile-sidebar.collapsed .sidebar-text {
        display: none;
    }
    
    .mobile-sidebar.collapsed .sidebar-header h5 {
        display: none;
    }
    
    .mobile-sidebar.collapsed .nav-link {
        justify-content: center;
    }
    
    .mobile-sidebar.collapsed .nav-link i {
        margin-right: 0;
        font-size: 1.2rem;
    }
    
    .mobile-sidebar.collapsed .user-info {
        padding: 1rem 0.5rem;
    }
    
    .mobile-sidebar.collapsed .user-details {
        display: none;
    }
    
    .mobile-sidebar.collapsed .user-avatar {
        margin: 0 auto;
    }
    
    /* Aplica margem apenas quando há sidebar (usuário autenticado) */
    body:has(.sidebar-container .mobile-sidebar) .main-wrapper {
        margin-left: var(--sidebar-width);
    }
    
    body:has(.sidebar-container .mobile-sidebar) .main-wrapper.sidebar-collapsed {
        margin-left: var(--sidebar-collapsed-width);
    }
    
    /* Fallback para navegadores sem :has() - usa classe no body */
    body.has-sidebar .main-wrapper {
        margin-left: var(--sidebar-width);
    }
    
    body.has-sidebar .main-wrapper.sidebar-collapsed {
        margin-left: var(--sidebar-collapsed-width);
    }
    
    .mobile-menu-toggle {
        display: none !important;
    }
    
    .sidebar-overlay {
        display: none !important;
    }
}

/* Mobile Sidebar (below 768px) */
@media (max-width: 767px) {
    /* Técnica especial para mobile - isola o sidebar completamente */
    body.sidebar-open {
        overflow: hidden; /* Previne scroll do body quando menu aberto */
    }
    
    .mobile-sidebar {
        position: fixed;
        top: 0;
        left: -100%;
        height: 100vh;
        height: 100dvh; /* Para browsers modernos */
        width: var(--sidebar-width);
        max-width: 85vw;
        background-color: #ffffff !important;
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.3);
        transition: left var(--transition-speed) ease;
        z-index: 999999 !important; /* Z-index absurdamente alto */
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        /* Isola o sidebar do resto da página */
        transform: translate3d(0, 0, 0);
        -webkit-transform: translate3d(0, 0, 0);
        backface-visibility: hidden;
        -webkit-backface-visibility: hidden;
    }
    
    .mobile-sidebar.active {
        left: 0;
        transform: translate3d(0, 0, 0) !important;
    }
    
    /* Garante que TUDO dentro do sidebar seja clicável */
    .mobile-sidebar,
    .mobile-sidebar * {
        pointer-events: auto !important;
        -webkit-touch-callout: default !important;
        -webkit-user-select: auto !important;
        user-select: auto !important;
    }
    
    /* Links específicos */
    .mobile-sidebar a,
    .mobile-sidebar button {
        display: block !important;
        position: relative !important;
        z-index: 999999 !important;
        pointer-events: auto !important;
        cursor: pointer !important;
    }
    
    .mobile-sidebar .nav-link:hover,
    .mobile-sidebar .nav-link.active {
        background-color: rgba(78, 115, 223, 0.1) !important;
        color: #4e73df !important;
    }
    
    .mobile-sidebar.active {
        left: 0;
    }
    
    .main-wrapper {
        margin-left: 0 !important;
    }
    
    .desktop-toggle {
        display: none !important;
    }
    
    /* Adjust main content padding on mobile */
    .main-content {
        padding-top: 60px; /* Space for mobile menu button */
    }
}

/* Sidebar Overlay for mobile */
.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--overlay-bg);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--transition-speed) ease, visibility var(--transition-speed) ease;
    z-index: 998 !important; /* Bem baixo */
    pointer-events: none;
}

.sidebar-overlay.active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    z-index: 998 !important; /* Mantém baixo mesmo quando ativo */
}

/* Sidebar Header */
.sidebar-header {
    padding: 1.5rem;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: var(--sidebar-bg);
    position: sticky;
    top: 0;
    z-index: 10;
}

.sidebar-header h5 {
    margin: 0;
    font-weight: 600;
    color: var(--primary-color);
    display: flex;
    align-items: center;
}

/* Sidebar Navigation */
.sidebar-nav {
    padding: 1rem 0;
}

.sidebar-nav .nav-link {
    display: flex;
    align-items: center;
    padding: 0.75rem 1.5rem;
    color: var(--text-main);
    text-decoration: none;
    transition: all var(--transition-speed) ease;
    border-left: 3px solid transparent;
    margin: 0.1rem 0;
    position: relative;
}

.sidebar-nav .nav-link:hover {
    background-color: rgba(78, 115, 223, 0.1);
    color: var(--primary-color);
    border-left-color: var(--primary-color);
}

.sidebar-nav .nav-link.active {
    background-color: rgba(78, 115, 223, 0.1);
    color: var(--primary-color);
    border-left-color: var(--primary-color);
    font-weight: 600;
}

.sidebar-nav .nav-link i {
    margin-right: 0.75rem;
    width: 20px;
    text-align: center;
    font-size: 1.1rem;
    flex-shrink: 0;
}

/* Mobile Menu Toggle Button */
.mobile-menu-toggle {
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 9998 !important; /* Abaixo do sidebar mas acima de tudo */
    background-color: #4e73df !important;
    color: white !important;
    border: none;
    border-radius: 8px;
    width: 45px;
    height: 45px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    transition: all var(--transition-speed) ease;
    -webkit-tap-highlight-color: transparent; /* Remove highlight no iOS */
}

.mobile-menu-toggle:hover {
    transform: scale(1.05);
    box-shadow: 0 3px 15px rgba(0, 0, 0, 0.3);
}

.mobile-menu-toggle i {
    font-size: 1.2rem;
    transition: transform var(--transition-speed) ease;
}

.mobile-menu-toggle.active i {
    transform: rotate(90deg);
}

/* Desktop Toggle Button */
.desktop-toggle {
    background: none;
    border: none;
    color: var(--text-main);
    font-size: 1.2rem;
    padding: 0.5rem;
    border-radius: 4px;
    transition: all var(--transition-speed) ease;
    cursor: pointer;
}

.desktop-toggle:hover {
    background-color: rgba(78, 115, 223, 0.1);
    color: var(--primary-color);
}

/* Sidebar Footer */
.sidebar-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
    background-color: var(--sidebar-bg);
    margin-top: auto;
}

.sidebar-footer:last-child {
    position: sticky;
    bottom: 0;
}

/* User Info Section */
.user-info {
    display: flex;
    align-items: center;
    padding: 1rem 1.5rem;
    background-color: rgba(78, 115, 223, 0.05);
    border-radius: 8px;
    margin: 1rem;
}

.user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: var(--primary-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    margin-right: 0.75rem;
    flex-shrink: 0;
}

.user-details h6 {
    margin: 0;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-main);
}

.user-details small {
    color: var(--text-muted);
}

/* Smooth transitions for nav items */
.sidebar-nav .nav-link::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s;
}

.sidebar-nav .nav-link:hover::before {
    left: 100%;
}

/* Fix for scrollbar on desktop when collapsed */
@media (min-width: 768px) {
    .mobile-sidebar.collapsed {
        overflow-y: hidden;
    }
    
    .mobile-sidebar.collapsed:hover {
        overflow-y: auto;
    }
}

/* Ensure content doesn't hide behind mobile button */
@media (max-width: 767px) {
    .navbar {
        padding-left: 70px;
    }
}

/* Animation for mobile sidebar */
@keyframes slideIn {
    from {
        transform: translateX(-100%);
    }
    to {
        transform: translateX(0);
    }
}

/* Responsive font sizes */
@media (max-width: 576px) {
    .sidebar-header {
        padding: 1rem;
    }
    
    .sidebar-nav .nav-link {
        padding: 0.6rem 1rem;
        font-size: 0.95rem;
    }
    
    .sidebar-nav .nav-link i {
        font-size: 1rem;
    }
    
    .user-info {
        padding: 0.75rem;
        margin: 0.75rem;
    }
    
    .user-avatar {
        width: 35px;
        height: 35px;
        font-size: 0.85rem;
    }
}

/* Fix para Bootstrap no mobile */
@media (max-width: 767px) {
    /* Remove qualquer interferência do Bootstrap */
    .mobile-sidebar.active {
        display: block !important;
        visibility: visible !important;
        opacity: 1 !important;
    }
    
    /* Garante que o overlay não bloqueie o sidebar */
    .sidebar-overlay {
        z-index: 1040 !important;
    }
}