:root {
    --primary-color: #2c3e50;
    --accent-color: #3498db;
    --sidebar-bg: #1a1a1a;
    --sidebar-width-collapsed: 60px;
    --sidebar-width-expanded: 240px;
    --text-light: #ecf0f1;
    --bg-light: #f4f6f9;
    --transition-speed: 0.3s;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Roboto', sans-serif;
    background-color: var(--bg-light);
    height: 100vh;
    overflow: hidden;
}

/* --- Auth Overlay (Pantalla de Login) --- */
.auth-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(44, 62, 80, 0.95); /* Fondo oscuro semitransparente */
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: opacity 0.5s ease;
}

.auth-card {
    background: white;
    padding: 2.5rem;
    border-radius: 8px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.3);
    text-align: center;
    max-width: 400px;
    width: 90%;
}

.auth-card h2 { margin-bottom: 1rem; color: var(--primary-color); }
.auth-card p { margin-bottom: 2rem; color: #666; }

/* Utilidad para ocultar elementos */
.hidden { display: none !important; }

/* --- Layout Principal --- */
.app-container {
    display: flex;
    height: 100vh;
    transition: filter 0.3s;
}

.blur-content {
    filter: blur(5px);
    pointer-events: none;
}

/* --- Sidebar (Barra Lateral) --- */
.sidebar {
    background-color: var(--sidebar-bg);
    color: var(--text-light);
    width: var(--sidebar-width-expanded);
    display: flex;
    flex-direction: column;
    transition: width var(--transition-speed) ease;
    white-space: nowrap;
    overflow: hidden;
    box-shadow: 2px 0 5px rgba(0,0,0,0.1);
    z-index: 100;
}

.sidebar.collapsed {
    width: var(--sidebar-width-collapsed);
}

.sidebar-header {
    height: 60px;
    display: flex;
    align-items: center;
    padding: 0 10px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.logo-area {
    flex-grow: 1;
    display: flex;
    align-items: center;
    gap: 12px;
    overflow: hidden;
    padding-left: 8px;
}

.logo-text {
    transition: opacity 0.2s;
    font-weight: 500;
    font-size: 1.2rem;
    letter-spacing: 0.5px;
}

.toggle-btn {
    background: none;
    border: none;
    color: var(--text-light);
    cursor: pointer;
    font-size: 1.2rem;
    padding: 5px;
    min-width: 30px;
    outline: none;
}

.org-qr-btn {
    background: none;
    border: none;
    color: var(--text-light);
    cursor: pointer;
    font-size: 0.85rem;
    padding: 4px 6px;
    opacity: 0.55;
    transition: opacity var(--transition-speed);
    outline: none;
    flex-shrink: 0;
}
.org-qr-btn:hover { opacity: 1; }
.sidebar.collapsed .org-qr-btn { opacity: 0; pointer-events: none; }

.sidebar.collapsed .logo-text { opacity: 0; pointer-events: none; }

/* Menu Items */
/*
 * El scroll va SOLO en menu-items, no en el sidebar entero.
 * min-height: 0 es clave: sin él, un flex-child no puede achicarse
 * por debajo de su contenido, y empuja el footer (logout) fuera de pantalla.
 */
.menu-items {
    list-style: none;
    flex: 1 1 auto;
    overflow-y: auto;
    /*
     * overflow-x: hidden es obligatorio. Sin él, al setear overflow-y: auto el
     * navegador computa el overflow-x (que queda en 'visible') como 'auto', y
     * con white-space: nowrap + la barra vertical robando ancho aparece una
     * barra de scroll horizontal espuria.
     */
    overflow-x: hidden;
    min-height: 0;
    margin-top: 20px;
    padding: 0;
    /* Scrollbar fina y discreta sobre el fondo oscuro del sidebar */
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,0.2) transparent;
}

.menu-items::-webkit-scrollbar { width: 6px; }
.menu-items::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.2);
    border-radius: 3px;
}
.menu-items::-webkit-scrollbar-track { background: transparent; }

.menu-items li a {
    display: flex;
    align-items: center;
    padding: 15px 18px;
    color: #bbb;
    text-decoration: none;
    transition: all 0.2s;
    height: 50px;
    border-left: 4px solid transparent;
    position: relative;
    overflow: hidden;
}

.menu-items li a:hover {
    background-color: rgba(255,255,255,0.05);
    color: white;
}

.menu-items li a.active {
    background-color: rgba(52, 152, 219, 0.15); /* Tinte azul */
    color: white;
    border-left-color: var(--accent-color);
}

.menu-separator {
    height: 1px;
    background-color: rgba(255,255,255,0.1);
    margin: 10px 18px;
}

.menu-label {
    padding: 10px 18px 5px 18px;
    color: #7f8c8d;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 500;
}

.sidebar.collapsed .menu-label .link-text {
    display: none;
}

.sidebar.collapsed .menu-separator {
    margin: 10px 8px;
}

.menu-items li a i {
    min-width: 24px;
    text-align: center;
    font-size: 1.1rem;
}

.link-text {
    margin-left: 15px;
    opacity: 1;
    transition: opacity 0.2s;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.sidebar.collapsed .link-text {
    opacity: 0;
    pointer-events: none;
}

/* ── Grupo "Externos" (mdm / erp / gte) ─────────────────────────── */
.menu-items li.external-group > a .external-toggle {
    margin-left: auto;
    font-size: 0.75rem;
    opacity: 0.55;
    transition: transform 0.2s;
}

.menu-items li.external-group.expanded > a .external-toggle {
    transform: rotate(90deg);
}

.menu-items li.external-group > ul.external-children {
    list-style: none;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.25s ease-out;
    padding: 0;
}

.menu-items li.external-group.expanded > ul.external-children {
    max-height: 600px;
}

.menu-items li.external-group > ul.external-children li a {
    padding-left: 42px;
    height: 42px;
    color: #999;
    font-size: 0.93rem;
}

.menu-items li.external-group > ul.external-children li a:hover {
    color: #fff;
}

.sidebar.collapsed .menu-items li.external-group > a .external-toggle,
.sidebar.collapsed .menu-items li.external-group > ul.external-children {
    display: none;
}

/* Sidebar Footer */
.sidebar-footer {
    border-top: 1px solid rgba(255,255,255,0.1);
    padding: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: rgba(0,0,0,0.2);
    flex-shrink: 0;
}

.user-info { display: flex; align-items: center; overflow: hidden; }
.user-info img { width: 32px; height: 32px; border-radius: 50%; margin-right: 10px; border: 2px solid rgba(255,255,255,0.2); }
#logout-btn { background: none; border: none; color: #e74c3c; cursor: pointer; font-size: 1.1rem; padding: 5px; transition: transform 0.2s; flex-shrink: 0; }
#logout-btn:hover { transform: scale(1.1); }

/* ── Notificaciones de módulo ────────────────────────────────────────── */

.menu-notify-cases {
    border-left-color: #e74c3c !important;
    animation: menu-blink-cases 1s ease-in-out 15;
}

.menu-notify-quotes {
    border-left-color: #f39c12 !important;
    animation: menu-blink-quotes 1s ease-in-out 15;
}

/* Si ambos están activos simultáneamente, casos tienen prioridad visual */
.menu-notify-cases.menu-notify-quotes {
    border-left-color: #e74c3c !important;
    animation: menu-blink-cases 1s ease-in-out 15;
}

@keyframes menu-blink-cases {
    0%, 100% { border-left-color: #e74c3c; }
    50%       { border-left-color: transparent; }
}

@keyframes menu-blink-quotes {
    0%, 100% { border-left-color: #f39c12; }
    50%       { border-left-color: transparent; }
}

/* Punto de notificación sobre el ícono (visible en modo colapsado también) */
.notify-dot {
    position: absolute;
    top: 9px;
    left: 32px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    border: 2px solid var(--sidebar-bg);
    pointer-events: none;
    z-index: 1;
}

.notify-dot-cases  { background: #e74c3c; }
.notify-dot-quotes { background: #f39c12; }

/* Texto corto junto al nombre (solo en sidebar expandido) */
.notify-label {
    font-size: 0.68rem;
    font-weight: 400;
    opacity: 0.75;
    white-space: nowrap;
}

.sidebar.collapsed .notify-label { display: none !important; }

/* ── Menú contextual del sidebar ─────────────────────────────────────── */
.menu-ctx {
    position: fixed;
    z-index: 9999;
    background: #2c2c2c;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 6px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5);
    min-width: 190px;
    overflow: hidden;
}

.menu-ctx ul { list-style: none; padding: 4px 0; }

.menu-ctx ul li {
    padding: 9px 16px;
    color: #ddd;
    cursor: pointer;
    font-size: 0.88rem;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.15s;
    white-space: nowrap;
}

.menu-ctx ul li:hover { background: rgba(255,255,255,0.08); color: #fff; }
.menu-ctx ul li i { font-size: 0.8rem; opacity: 0.65; min-width: 14px; text-align: center; }

/* --- Content Area (Frames) --- */
.content-area {
    flex-grow: 1;
    background-color: #fff;
    position: relative;
    display: flex;
    flex-direction: column;
}

.app-frame {
    width: 100%;
    height: 100%;
    border: none;
    display: block;
}

.placeholder-content {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: #ccc;
}
.placeholder-content i { font-size: 5rem; margin-bottom: 20px; display: block; color: #e0e0e0;}
.placeholder-content h3 { font-weight: 300; color: #7f8c8d; }

/* --- Mobile Top Bar --- */
.mobile-topbar {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 50px;
    background-color: var(--sidebar-bg);
    z-index: 200;
    align-items: center;
    padding: 0 15px;
    gap: 12px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}

.mobile-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-light);
    font-size: 1rem;
    font-weight: 500;
}

.mobile-logo img {
    width: 24px;
    height: 24px;
}

/* --- Backdrop para sidebar en mobile --- */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 140;
}

.sidebar-backdrop.active {
    display: block;
}

/* --- Mobile Responsive --- */
@media (max-width: 768px) {
    .mobile-topbar { display: flex; }

    body { padding-top: 50px; }

    .app-container {
        height: calc(100vh - 50px);
        height: calc(100dvh - 50px); /* descuenta barras del navegador en mobile */
        margin-left: 0;
    }

    /* Sidebar: overlay deslizable desde la izquierda */
    .sidebar {
        position: fixed;
        top: 50px;
        left: 0;
        height: calc(100vh - 50px);
        height: calc(100dvh - 50px);
        width: var(--sidebar-width-expanded) !important;
        transform: translateX(-100%);
        transition: transform var(--transition-speed) ease;
        z-index: 150;
        overflow: hidden; /* mantener para no desbordar mientras está cerrado */
    }

    /* Padding extra en el footer para la barra home de iOS y Android */
    .sidebar-footer {
        padding-bottom: calc(15px + env(safe-area-inset-bottom, 0px));
    }

    .sidebar.collapsed {
        width: var(--sidebar-width-expanded) !important;
        transform: translateX(-100%);
    }

    .sidebar.mobile-open {
        transform: translateX(0) !important;
    }

    /* Al abrir, mostrar todos los textos que collapsed oculta */
    .sidebar.mobile-open .link-text {
        opacity: 1;
        pointer-events: auto;
    }

    .sidebar.mobile-open .logo-text {
        opacity: 1;
    }

    .sidebar.mobile-open .menu-label .link-text {
        display: block;
    }

    /* (El scroll de .menu-items está definido a nivel global; aquí no hace
     * falta repetirlo.) */

    /* Ocultar el toggle del sidebar en mobile (está en la topbar) */
    #toggle-btn { display: none; }

    .content-area { width: 100%; }
}
