/* css/layout.css */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 14px;
    background: #ffffff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    z-index: 1000;
    position: relative;
    height: 56px;
    gap: 8px;
    border-bottom: 1px solid #e2e8f0;
}

.header-title {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.header-title img {
    height: 28px;
    max-width: 100px;
    object-fit: contain;
}

.header-title h1 {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--brand-blue);
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-toggle {
    background: var(--brand-red);
    color: #fff;
    border: none;
    padding: 7px 12px;
    border-radius: 6px;
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 4px;
    white-space: nowrap;
    flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(204, 26, 43, 0.2);
}

.btn-toggle:hover {
    background: var(--brand-red-hover);
    transform: translateY(-1px);
}

.btn-toggle-results {
    background: #003B73;
    box-shadow: 0 2px 4px rgba(0, 59, 115, 0.2);
}

.btn-toggle-results:hover {
    background: #002850;
}

.btn-toggle-menu {
    background: #0f766e;
    box-shadow: 0 2px 4px rgba(15, 118, 110, 0.2);
}

.btn-toggle-menu:hover {
    background: #115e59;
}

.btn-toggle-logout {
    display: inline-flex !important;
}

/* Navegación Directa Superior (Desktop) */
.header-nav {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: 10px;
}

.nav-link-item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 8px;
    text-decoration: none;
    font-size: 0.82rem;
    font-weight: 600;
    color: #475569;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
}

.nav-link-item:hover {
    color: #003B73;
    background: #eff6ff;
    border-color: #93c5fd;
    transform: translateY(-1px);
    box-shadow: 0 2px 5px rgba(0, 59, 115, 0.1);
}

.nav-link-item.active {
    color: #ffffff;
    background: #003B73;
    border-color: #003B73;
    box-shadow: 0 2px 6px rgba(0, 59, 115, 0.28);
}

/* User Badge & Chip */
.user-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #f1f5f9;
    padding: 4px 10px;
    border-radius: 20px;
    border: 1px solid #e2e8f0;
    font-size: 0.8rem;
    color: #1e293b;
    font-weight: 600;
}

.user-badge-role {
    font-size: 0.65rem;
    font-weight: 800;
    padding: 2px 7px;
    border-radius: 12px;
    background: #003B73;
    color: #ffffff;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Enlaces del Menú Móvil en Drawer */
.mob-menu-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 11px 14px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    text-decoration: none;
    color: #1e293b;
    transition: all 0.2s ease;
    font-family: inherit;
    cursor: pointer;
}

.mob-menu-link:hover, .mob-menu-link:focus {
    background: #f1f5f9;
    border-color: #cbd5e1;
    transform: translateY(-1px);
}

.mob-menu-link.active {
    background: #eff6ff;
    border-color: #3b82f6;
    color: #1d4ed8;
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.12);
}

.mob-menu-link strong {
    display: block;
    font-size: 0.88rem;
    font-weight: 700;
}

.mob-menu-link small {
    display: block;
    font-size: 0.74rem;
    color: #64748b;
    margin-top: 1px;
}

.mob-icon {
    font-size: 1.35rem;
    flex-shrink: 0;
}

/* Layout Principal */
.app-layout {
    display: flex;
    height: calc(100vh - 56px);
    height: calc(100dvh - 56px);
    position: relative;
    overflow: hidden;
}

.map-container {
    flex: 1;
    height: 100%;
    position: relative;
    z-index: 1;
}

#map {
    width: 100%;
    height: 100%;
    background: #eef2f5;
}

/* Paneles Laterales */
.panel-sidebar {
    width: 330px;
    background: #fff;
    display: flex;
    flex-direction: column;
    z-index: 500;
    box-shadow: var(--shadow-lg);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: linear-gradient(90deg, #003B73 0%, #D41C3C 100%);
    color: #fff;
    flex-shrink: 0;
}

.panel-header h2 {
    color: #fff;
    font-size: 1.05rem;
    font-weight: 700;
    margin: 0;
}

.btn-close {
    background: rgba(255, 255, 255, 0.15);
    border: none;
    color: #fff;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    cursor: pointer;
    line-height: 1;
    transition: background 0.2s;
}

.btn-close:hover {
    background: rgba(255, 255, 255, 0.3);
}

.panel-content {
    padding: 15px;
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Botón de Minimizar/Colapsar Panel (Desktop) */
.btn-panel-toggle-desk {
    background: rgba(255, 255, 255, 0.18);
    border: 1px solid rgba(255, 255, 255, 0.35);
    color: #ffffff;
    width: 26px;
    height: 26px;
    border-radius: 6px;
    display: none; /* Visible solo en desktop */
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.2s ease;
    line-height: 1;
}

.btn-panel-toggle-desk:hover {
    background: rgba(255, 255, 255, 0.38);
    transform: scale(1.08);
}

/* Botones Flotantes en el Mapa para Restaurar Paneles */
.btn-map-float {
    position: absolute;
    top: 14px;
    z-index: 1000;
    background: #ffffff;
    color: #003B73;
    border: 1.5px solid #003B73;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 700;
    box-shadow: 0 4px 14px rgba(0, 59, 115, 0.22);
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    display: none;
    align-items: center;
    gap: 6px;
    font-family: inherit;
}

.btn-map-float:hover {
    background: #003B73;
    color: #ffffff;
    transform: translateY(-2px);
    box-shadow: 0 6px 18px rgba(0, 59, 115, 0.35);
}

.btn-map-float-left {
    left: 14px;
}

.btn-map-float-right {
    right: 14px;
    color: #cc1a2b;
    border-color: #cc1a2b;
    box-shadow: 0 4px 14px rgba(204, 26, 43, 0.22);
}

.btn-map-float-right:hover {
    background: #cc1a2b;
    color: #ffffff;
    box-shadow: 0 6px 18px rgba(204, 26, 43, 0.35);
}

/* Estado Colapsado de Paneles */
.panel-sidebar.collapsed {
    width: 0 !important;
    min-width: 0 !important;
    max-width: 0 !important;
    padding: 0 !important;
    border: none !important;
    opacity: 0;
    pointer-events: none;
    overflow: hidden !important;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Fondo oscuro / Backdrop para móvil */
.sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    backdrop-filter: blur(2px);
    z-index: 2400;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.sidebar-backdrop.active {
    opacity: 1;
    pointer-events: auto;
}

/* Mobile-First Offcanvas Logic (Móvil Vertical y Celular Horizontal / Pantallas de baja altura) */
@media (max-width: 768px), (max-height: 550px) {
    .app-header {
        padding: 5px 8px;
        gap: 5px;
        height: 50px;
    }

    .header-title h1 {
        font-size: 0.95rem;
    }

    .header-nav {
        display: none !important; /* En móvil se utiliza el botón de Menú (drawer) */
    }

    .btn-toggle-menu,
    .btn-toggle-filters,
    .btn-toggle-results {
        display: inline-flex !important;
    }

    #user-header-box {
        gap: 4px !important;
    }

    #user-header-box #user-header-name {
        display: none !important; /* Ocultar el nombre largo para que no desborde la barra */
    }

    .user-chip {
        padding: 3px 6px !important;
        font-size: 0.72rem !important;
        gap: 3px !important;
    }

    .user-badge-role {
        font-size: 0.6rem !important;
        padding: 2px 5px !important;
        max-width: 85px;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .btn-toggle-logout {
        padding: 4px 8px !important;
        font-size: 0.72rem !important;
    }

    .form-group select, 
    .form-group input {
        font-size: 16px !important; /* Previene auto-zoom molesto en Safari iOS */
    }

    .panel-sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        height: 100%;
        height: 100dvh;
        width: 86vw;
        max-width: 320px;
        z-index: 2500;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
    }

    .panel-content {
        padding-bottom: max(35px, env(safe-area-inset-bottom)) !important;
    }
    
    .offcanvas-left {
        left: 0;
        transform: translateX(-105%);
    }
    
    .offcanvas-left.active {
        transform: translateX(0);
    }
    
    .offcanvas-right {
        right: 0;
        transform: translateX(105%);
    }
    
    .offcanvas-right.active {
        transform: translateX(0);
    }

    .btn-close {
        display: flex !important;
    }

    .btn-panel-toggle-desk {
        display: none !important;
    }

    /* Ajustar badge de candidato en móvil */
    #badge-candidato-activo {
        max-width: 92vw;
        padding: 4px 12px !important;
        font-size: 0.78rem !important;
        gap: 6px !important;
        top: 10px !important;
    }

    #txt-candidato-activo {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 190px;
    }
}

/* Optimización para Celular Apaisado / Horizontal (Landscape Móvil: 844x390, 896x414, etc.) */
@media (max-height: 550px) and (orientation: landscape) {
    .app-header {
        height: 42px !important;
        padding: 2px 8px !important;
        gap: 5px !important;
    }

    .header-title h1 {
        display: none !important; /* Ocultar título para dejar espacio a los botones */
    }

    .header-title img {
        height: 20px !important;
        max-width: 65px !important;
    }

    .header-nav {
        display: none !important;
    }

    .btn-toggle {
        padding: 3px 7px !important;
        font-size: 0.72rem !important;
        height: 28px !important;
        gap: 3px !important;
    }

    .btn-toggle-menu,
    .btn-toggle-filters,
    .btn-toggle-results {
        display: inline-flex !important;
    }

    #user-header-name {
        display: none !important;
    }

    .panel-sidebar {
        width: 270px !important; /* Más pequeño en horizontal */
        max-width: 48vw !important;
    }

    .panel-header {
        padding: 6px 10px !important;
    }

    .panel-header h2 {
        font-size: 0.9rem !important;
    }

    .panel-content {
        padding: 8px 10px !important;
    }

    .btn-close {
        display: flex !important;
        width: 24px !important;
        height: 24px !important;
        font-size: 1rem !important;
    }
}

/* Optimización extra para teléfonos de ancho estrecho (320px - 480px) */
@media (max-width: 480px) {
    .app-header {
        padding: 3px 5px;
        gap: 3px;
        height: 48px;
    }

    .header-title h1 {
        display: none; /* Mantener limpio el header mostrando solo el logo oficial en móviles pequeños */
    }

    .header-title img {
        height: 22px;
        max-width: 60px;
    }

    .btn-toggle {
        padding: 4px 6px;
        font-size: 0.72rem;
        gap: 2px;
    }

    .btn-text-logout {
        display: none; /* En pantallas muy pequeñas mostrar solo icono 🚪 */
    }

    #user-header-box #user-header-name {
        display: none;
    }

    #btn-header-admin {
        padding: 3px 6px !important;
        font-size: 0.72rem !important;
    }

    #badge-candidato-activo {
        font-size: 0.72rem !important;
        padding: 3px 8px !important;
    }

    #txt-candidato-activo {
        max-width: 140px;
    }
}

/* Desktop layout (Side-by-side) - Activo cuando ancho >= 769px Y altura >= 551px */
@media (min-width: 769px) and (min-height: 551px) {
    .btn-toggle {
        display: none; /* Ocultar botones toggle en desktop estándar */
    }

    .btn-toggle-logout {
        display: inline-flex !important; /* El botón de salir siempre está visible */
    }

    #panel-menu {
        display: none !important; /* El menú drawer solo existe en móvil; en desktop está el header-nav */
    }
    
    .btn-close {
        display: none; /* En desktop se usa el botón de minimizar ◀ / ▶ */
    }

    .btn-panel-toggle-desk {
        display: flex !important;
    }

    .sidebar-backdrop {
        display: none !important;
    }
    
    .panel-sidebar {
        width: 295px; /* Más compacto que 330px para dar más amplitud al mapa */
    }

    .offcanvas-left {
        transform: translateX(0);
        position: relative;
        border-right: 1px solid var(--border-color);
        box-shadow: none;
    }
    
    #panel-results.offcanvas-right {
        transform: translateX(0);
        position: relative;
        border-left: 1px solid var(--border-color);
        box-shadow: none;
    }
}

/* Jerarquía Territorial Tree Styles */
.tree-ul {
    list-style: none;
    padding-left: 16px;
    margin: 4px 0;
    border-left: 1px dashed #cbd5e1;
}
.tree-root {
    padding-left: 0;
    border-left: none;
}
.tree-li {
    margin: 4px 0;
}
.tree-label {
    display: flex;
    align-items: center;
    font-size: 0.76rem;
    color: #334155;
    cursor: pointer;
    user-select: none;
    padding: 3px 4px;
    border-radius: 4px;
    transition: background 0.15s;
}
.tree-label:hover {
    background: #f1f5f9;
    color: #1e3a8a;
}
.tree-label.jerarquia-highlighted {
    background: #dbeafe !important;
    color: #1e3a8a !important;
    font-weight: 700 !important;
    border-left: 4px solid #1e3a8a !important;
    border-radius: 4px;
    box-shadow: 0 0 10px rgba(30, 58, 138, 0.3) !important;
    animation: pulseJerarquia 1.8s infinite alternate ease-in-out;
}
@keyframes pulseJerarquia {
    0% { background: #dbeafe; }
    100% { background: #bfdbfe; }
}
.tree-toggle-icon {
    display: inline-block;
    width: 14px;
    font-size: 0.7rem;
    color: #94a3b8;
    margin-right: 4px;
    transition: transform 0.15s ease;
}
.tree-toggle-icon.expanded {
    transform: rotate(90deg);
}
.tree-badge {
    margin-left: auto;
    font-size: 0.68rem;
    color: #64748b;
    background: #f1f5f9;
    padding: 2px 6px;
    border-radius: 10px;
    font-weight: 600;
}
.tree-badge-votos {
    color: #b91c1c;
    font-weight: 700;
    background: #fee2e2;
    cursor: pointer;
    transition: all 0.15s ease-in-out;
}
.tree-badge-votos:hover {
    background: #b91c1c;
    color: #ffffff;
    transform: scale(1.05);
}
.jerarquia-container {
    max-height: 450px;
    overflow-y: auto;
    margin-top: 8px;
    padding-right: 4px;
}
.mesa-pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
    padding: 2px 6px;
    font-size: 0.72rem;
    margin: 2px;
    cursor: pointer;
    transition: all 0.15s;
}
.mesa-pill:hover {
    border-color: #93c5fd;
    background: #eff6ff;
}
.mesa-votos {
    color: #b91c1c;
    font-weight: 700;
}

/* Modal Desglose Candidatos */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(2px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}
.modal-card {
    background: white;
    width: 90%;
    max-width: 440px;
    max-height: 80vh;
    border-radius: 10px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: modalPop 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes modalPop {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 12px 16px;
    border-bottom: 1px solid #e2e8f0;
    background: #f8fafc;
}
.modal-close-btn {
    background: none;
    border: none;
    font-size: 1.3rem;
    color: #64748b;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}
.modal-close-btn:hover {
    color: #0f172a;
}
.modal-body {
    padding: 12px 16px;
    overflow-y: auto;
}

/* Fila Candidato Interactiva */
.fila-candidato {
    cursor: pointer;
    transition: all 0.15s ease-in-out;
}
.fila-candidato:hover {
    background-color: #f1f5f9 !important;
}
.fila-candidato.activa {
    background-color: #eff6ff !important;
    border-left: 3.5px solid #2563eb !important;
}

/* Polígonos del mapa interactivos */
.leaflet-interactive,
path.leaflet-interactive {
    cursor: pointer !important;
    pointer-events: auto !important;
}

/* Asegurar que los tooltips nunca bloqueen los clics en los polígonos del mapa */
.leaflet-tooltip,
.leaflet-tooltip *,
.leaflet-tooltip-pane,
.leaflet-tooltip-pane * {
    pointer-events: none !important;
}

@media (max-width: 768px) {
    .modal-card {
        width: 95vw !important;
        max-width: 95vw !important;
        max-height: 88vh !important;
    }
    .modal-header {
        padding: 10px 14px;
    }
    .modal-body {
        padding: 10px 12px;
    }
    .tree-label {
        padding: 6px 4px; /* Mayor área táctil en móvil */
        font-size: 0.8rem;
    }
    .mesa-pill {
        padding: 4px 8px;
        font-size: 0.76rem;
    }
}

/* ========================================================
   MÓDULO DE RESEÑAS DE CANDIDATOS
   ======================================================== */
.btn-resena-ojo {
    user-select: none;
    -webkit-user-select: none;
    line-height: 1;
}
.btn-resena-ojo:hover {
    transform: scale(1.3) !important;
    opacity: 1 !important;
    filter: drop-shadow(0 2px 4px rgba(30, 58, 138, 0.4));
}

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

.candidato-foto-container {
    width: 130px;
    height: 160px;
    min-width: 130px;
    max-width: 130px;
    max-height: 160px;
    overflow: hidden;
    border-radius: 8px;
    border: 2px solid #0d2b5c;
    box-shadow: 0 4px 12px rgba(13, 43, 92, 0.2);
    flex-shrink: 0;
    background: #f1f5f9;
}
.candidato-foto-img {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover !important;
    object-position: top center !important;
    display: block;
}
.candidato-foto-placeholder {
    width: 130px;
    height: 160px;
    min-width: 130px;
    background: #e2e8f0;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 3rem;
    color: #94a3b8;
    border: 2px solid #cbd5e1;
    flex-shrink: 0;
}

