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

html,
body {
    width: 100%;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: #fff;
    overflow: hidden;
}

#map {
    width: 100%;
    height: 100%;
    position: relative;
}

/* Leaflet customization */
.leaflet-control-container {
    z-index: 500;
}

/* Ocultar atribución y créditos */
.leaflet-control-attribution {
    display: none !important;
}

/* Controles de zoom con transparencia */
.leaflet-bar {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
    border: none;
    border-radius: 8px;
    overflow: hidden;
    backdrop-filter: blur(8px);
}

.leaflet-bar a {
    background-color: rgba(255, 255, 255, 0.6) !important;
    color: #2c3e50 !important;
    border-bottom: 1px solid rgba(224, 230, 237, 0.5) !important;
    backdrop-filter: blur(8px);
    transition: background-color 0.2s ease;
}

.leaflet-bar a:last-child {
    border-bottom: none !important;
}

.leaflet-bar a:hover {
    background-color: rgba(255, 255, 255, 0.8) !important;
}

.leaflet-bar a.leaflet-disabled {
    background-color: rgba(255, 255, 255, 0.4) !important;
    color: rgba(44, 62, 80, 0.4) !important;
}

/* Remove focus outline */
.leaflet-container,
.leaflet-container *,
.leaflet-interactive,
.leaflet-popup,
.leaflet-popup-content-wrapper,
.leaflet-marker-icon,
svg path {
    outline: none !important;
}

.leaflet-container:focus,
.leaflet-container *:focus {
    outline: none !important;
}

/* Remove tap highlight on mobile */
.leaflet-container,
.leaflet-interactive {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Controls Container */
.controls-container {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 600;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    padding: 12px;
    width: 280px;
    backdrop-filter: blur(8px);
}

.controls-title {
    font-size: 14px;
    font-weight: 700;
    color: #2c3e50;
    margin: 0 0 6px 0;
    padding-bottom: 8px;
    border-bottom: 2px solid #e0e6ed;
    text-align: center;
}

.controls-subtitle {
    font-size: 11px;
    font-weight: 600;
    color: #5a6c7d;
    margin: 0 0 5px 0;
    text-align: center;
    letter-spacing: 0.3px;
}

.controls-info {
    font-size: 10px;
    font-weight: 500;
    color: #7a8fa6;
    margin: 0 0 10px 0;
    text-align: center;
    line-height: 1.3;
    font-style: italic;
}

.selector-wrapper {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.selector-wrapper label {
    font-size: 11px;
    font-weight: 600;
    color: #2c3e50;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.selector-wrapper select {
    padding: 8px 12px;
    border: 2px solid #e0e6ed;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    background: rgba(255, 255, 255, 0.6);
    color: #2c3e50;
    cursor: pointer;
    transition: all 0.3s ease;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%232c3e50' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 40px;
    backdrop-filter: blur(8px);
}

.selector-wrapper select option {
    font-size: 16px;
    padding: 10px;
}

.selector-wrapper select:hover {
    border-color: #1a6fba;
    box-shadow: 0 0 0 3px rgba(26, 111, 186, 0.05);
}

.selector-wrapper select:focus {
    outline: none;
    border-color: #1a6fba;
    box-shadow: 0 0 0 3px rgba(26, 111, 186, 0.1);
}

/* Search input */
.search-input-container {
    position: relative;
}

.search-input-container input[type="text"] {
    width: 100%;
    padding: 8px 12px;
    border: 2px solid #e0e6ed;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    background: rgba(255, 255, 255, 0.6);
    color: #2c3e50;
    transition: all 0.2s ease;
    backdrop-filter: blur(8px);
    outline: none;
    font-family: inherit;
}

.search-input-container input[type="text"]::placeholder {
    color: #a0aec0;
    font-weight: 400;
}

.search-input-container input[type="text"]:hover {
    border-color: #1a6fba;
    box-shadow: 0 0 0 3px rgba(26, 111, 186, 0.05);
}

.search-input-container input[type="text"]:focus {
    border-color: #1a6fba;
    box-shadow: 0 0 0 3px rgba(26, 111, 186, 0.1);
}

.search-results {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: rgba(255, 255, 255, 0.98);
    border: 1px solid #e0e6ed;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    z-index: 700;
    max-height: 240px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, 0.15) transparent;
}

.search-results::-webkit-scrollbar {
    width: 4px;
}

.search-results::-webkit-scrollbar-track {
    background: transparent;
}

.search-results::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.15);
    border-radius: 2px;
}

.search-result-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    cursor: pointer;
    transition: background 0.15s ease;
    gap: 8px;
}

.search-result-item:hover {
    background: rgba(26, 111, 186, 0.08);
}

.search-result-item:first-child {
    border-radius: 8px 8px 0 0;
}

.search-result-item:last-child {
    border-radius: 0 0 8px 8px;
}

.search-result-item:only-child {
    border-radius: 8px;
}

.search-result-name {
    font-size: 13px;
    font-weight: 600;
    color: #2c3e50;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.search-result-dept {
    font-size: 10px;
    color: #7a8fa6;
    white-space: nowrap;
    flex-shrink: 0;
}

.search-no-results {
    padding: 10px 12px;
    font-size: 12px;
    color: #7a8fa6;
    text-align: center;
    font-style: italic;
}

/* Legend */
.legend {
    position: absolute;
    bottom: 20px;
    left: 20px;
    z-index: 600;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    padding: 14px;
    max-width: 300px;
    backdrop-filter: blur(8px);
}

.legend-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
    padding-bottom: 6px;
    border-bottom: 2px solid #e0e6ed;
}

.legend-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    background: rgba(0, 0, 0, 0.07);
    border: none;
    border-radius: 50%;
    font-size: 10px;
    color: #2c3e50;
    cursor: pointer;
    flex-shrink: 0;
    line-height: 1;
}

.legend-toggle:hover {
    background: rgba(0, 0, 0, 0.13);
}

.legend-body {
    max-height: calc(50vh - 60px);
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, 0.15) transparent;
}

.legend-body::-webkit-scrollbar {
    width: 4px;
}

.legend-body::-webkit-scrollbar-track {
    background: transparent;
}

.legend-body::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.15);
    border-radius: 2px;
}

.legend-collapsed .legend-body {
    display: none;
}

.legend-title {
    font-size: 11px;
    font-weight: 700;
    color: #2c3e50;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.legend-gradient {
    margin-bottom: 12px;
}

.gradient-stops {
    display: flex;
    gap: 4px;
    margin-bottom: 8px;
}

.legend-stop {
    flex: 1;
}

.legend-color-box {
    width: 100%;
    height: 30px;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s ease;
}

.legend-color-box:hover {
    transform: scale(1.05);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

.legend-stop-label {
    font-size: 11px;
    color: #2c3e50;
    font-weight: 500;
    word-break: break-word;
}

.legend-info {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid #e0e6ed;
    font-size: 12px;
    color: #5a6c7d;
    text-align: center;
}

.legend-items {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 12px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 13px;
    color: #2c3e50;
}

.legend-color {
    width: 24px;
    height: 24px;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    flex-shrink: 0;
}

.legend-label {
    font-weight: 500;
}

/* Credit */
.credit {
    position: absolute;
    bottom: 20px;
    right: 20px;
    z-index: 600;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 10px;
    color: #2c3e50;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(8px);
    line-height: 1.6;
}

.credit div {
    text-align: right;
}

.credit a {
    color: #41E68C;
    text-decoration: none;
    font-weight: 600;
    transition: color 0.2s ease;
}

.credit a:hover {
    color: #2ecc71;
    text-decoration: underline;
}


/* Popups */
.leaflet-popup-content-wrapper {
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    border: 1px solid rgba(0, 0, 0, 0.05);
}

.leaflet-popup-content {
    margin: 0;
    padding: 0;
}

.popup-inner {
    padding: 16px;
    font-family: inherit;
}

.popup-title {
    font-size: 14px;
    font-weight: 700;
    color: #2c3e50;
    margin-bottom: 12px;
    border-bottom: 2px solid #e0e6ed;
    padding-bottom: 10px;
}

.popup-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    font-size: 13px;
    color: #2c3e50;
}

.popup-item:last-child {
    padding-bottom: 0;
}

.popup-party {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
}

.popup-color-box {
    width: 16px;
    height: 16px;
    border-radius: 3px;
    flex-shrink: 0;
}

.popup-value {
    text-align: right;
    font-weight: 500;
}

.popup-percentage {
    font-size: 12px;
    color: #5a6c7d;
    font-weight: 400;
}

/* Leaflet popup arrow */
.leaflet-popup-tip {
    background: rgba(255, 255, 255, 0.98);
    border: 1px solid rgba(0, 0, 0, 0.05);
}

/* Pie chart markers */
.pie-chart-marker {
    background: none !important;
    border: none !important;
}

/* Custom tooltip */
.custom-tooltip {
    background: rgba(44, 62, 80, 0.95);
    border: none;
    border-radius: 6px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    padding: 6px 12px;
    white-space: nowrap;
}

.custom-tooltip::before {
    border-top-color: rgba(44, 62, 80, 0.95);
}

/* Responsive adjustments */
@media (max-width: 768px) {

    /* Ocultar controles de zoom en móvil */
    .leaflet-control-zoom {
        display: none !important;
    }

    .controls-container {
        top: 5px;
        left: 5px;
        right: 5px;
        min-width: auto;
        width: auto;
        max-width: none;
        padding: 8px 12px;
        /* Vertical reducido, horizontal normal */
    }

    .controls-title {
        font-size: 14px;
        margin: 0 0 8px 0;
        padding-bottom: 8px;
    }

    .selector-wrapper {
        gap: 4px;
        /* Reducido aún más */
    }

    .selector-wrapper label {
        font-size: 12px;
    }

    .selector-wrapper select {
        font-size: 15px;
        padding: 6px 12px;
    }

    .selector-wrapper select option {
        font-size: 16px;
        padding: 12px;
    }

    .search-input-container input[type="text"] {
        font-size: 15px;
        padding: 6px 12px;
    }

    .search-results {
        max-height: 180px;
    }

    .legend {
        bottom: 5px;
        left: 5px;
        right: auto;
        max-width: 240px;
        width: calc(100vw - 90px);
        padding: 6px 8px 6px 8px;
        margin-bottom: 26px;
        overflow: hidden;
    }

    .legend-header {
        margin-bottom: 4px;
        padding-bottom: 3px;
    }

    .legend-title {
        font-size: 9px;
    }

    .legend-info {
        margin-top: 4px;
        padding-top: 4px;
        font-size: 9px;
    }

    /* Hacer el gradiente más bajo en móvil */
    .legend-gradient-bar {
        height: 16px !important;
    }

    /* Leyenda de partidos en 2 columnas */
    .legend-party-items {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0 8px;
    }

    .legend-party-item {
        margin-bottom: 4px !important;
    }

    .legend-candidate {
        display: none;
    }

    .legend-party-item {
        min-width: 0;
    }

    .legend-party-row {
        gap: 4px !important;
    }

    .legend-party-item img,
    .legend-party-item>div>div>div[style*="border-radius:50%"] {
        width: 14px !important;
        height: 14px !important;
        min-width: 14px;
    }

    .legend-party-name {
        font-size: 9px !important;
    }

    .legend-party-pct {
        font-size: 10px !important;
        margin-left: 2px !important;
    }

    .legend-party-bar {
        margin-left: 0 !important;
        height: 3px !important;
    }

    .legend-party-bar>div {
        height: 3px !important;
    }

    .party-chip-name {
        max-width: 100px;
    }

    .party-mun-count {
        font-size: 11px;
    }

    /* Credit más compacto en móvil */
    .credit {
        bottom: 5px;
        right: 5px;
        padding: 3px 6px;
        font-size: 7px;
        line-height: 1.4;
    }

    /* Popup scrolleable y compacto (~4:3) en móvil */
    .leaflet-popup-content-wrapper {
        max-height: 80vw;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }

    .leaflet-popup-content {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        max-height: 80vw;
        flex: 1;
    }
}

/* Loading animation */
.loading {
    opacity: 0.6;
    pointer-events: none;
}

/* Hover effects */
.leaflet-interactive:hover {
    opacity: 0.9;
    filter: brightness(1.05);
}

/* Loader */
.loader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 20px;
}

.loader-spinner {
    width: 60px;
    height: 60px;
    border: 5px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.loader p {
    color: #fff;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

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

/* New Popup Styles */
.popup-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    font-size: 13px;
    color: #2c3e50;
}

.popup-label {
    font-weight: 600;
    color: #5a6c7d;
}

.popup-divider {
    height: 1px;
    background: #e0e6ed;
    margin: 8px 0;
}

/* Party mode — chips in legend */
.party-mun-count {
    font-size: 12px;
    font-weight: 700;
    color: #2c3e50;
    text-align: center;
    margin-bottom: 8px;
    padding-bottom: 6px;
    border-bottom: 1px solid #e0e6ed;
}

.party-chips-container {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.party-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px 3px 7px;
    border-radius: 20px;
    border: 1px solid transparent;
    cursor: pointer;
    transition: transform 0.12s ease, box-shadow 0.12s ease;
    font-family: inherit;
    text-align: left;
    max-width: 100%;
}

.party-chip:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
}

.party-chip-name {
    font-size: 11px;
    font-weight: 600;
    color: #2c3e50;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 130px;
}

.party-chip-dept {
    font-size: 9px;
    font-weight: 700;
    color: #7a8fa6;
    white-space: nowrap;
    flex-shrink: 0;
    letter-spacing: 0.3px;
}

/* Separator between municipality search and party select */
#party-search-wrapper {
    padding-top: 8px;
    border-top: 1px solid #e0e6ed;
    margin-top: 4px;
}

/* Fragmentation toggle button */
.frag-toggle-btn {
    display: block;
    width: 100%;
    padding: 8px 10px;
    margin-top: 8px;
    background: none;
    border: none;
    font-family: inherit;
    font-size: 11px;
    font-weight: 500;
    color: #7a8fa6;
    cursor: pointer;
    text-align: center;
    border-radius: 6px;
    transition: color 0.2s ease, background 0.2s ease;
    letter-spacing: 0.2px;
}

.frag-toggle-btn:hover {
    color: #2c3e50;
    background: rgba(0, 0, 0, 0.05);
}

.frag-toggle-btn.active {
    color: #de3028;
    font-weight: 700;
    background: rgba(222, 48, 40, 0.07);
}

/* Toast notification */
.toast {
    position: fixed;
    bottom: 60px;
    left: 50%;
    transform: translateX(-50%) translateY(12px);
    background: rgba(44, 62, 80, 0.92);
    color: #fff;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    z-index: 9000;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.25s ease, transform 0.25s ease;
    white-space: nowrap;
}

.toast-visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}