function tribunal_mapa_script() {
?>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>

<script>
document.addEventListener('DOMContentLoaded', function () {

    const mapContainer = document.getElementById('jurisdiction-map');
    if (!mapContainer) return;

    const map = L.map('jurisdiction-map', {
        scrollWheelZoom: false,
        attributionControl: false,
        zoomControl: true
    }).setView([-24, -70], 5);

    L.tileLayer('https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}{r}.png', {
        maxZoom: 18
    }).addTo(map);

    // Overlay azul
    L.rectangle([
        [-56, -100],
        [0, 0]
    ], {
        color: "#1F4C6E",
        weight: 0,
        fillOpacity: 0.50
    }).addTo(map);

    // REGIONES OBJETIVO
    const targetRegions = [
        "Región de Arica y Parinacota",
        "Región de Tarapacá",
        "Región de Antofagasta",
        "Región de Atacama",
        "Región de Coquimbo"
    ];

    // POBLACIÓN
    const poblacion = {
        "Región de Arica y Parinacota": "244.569 Habitantes",
        "Región de Tarapacá": "369.806 Habitantes",
        "Región de Antofagasta": "635.416 Habitantes",
        "Región de Atacama": "299.180 Habitantes",
        "Región de Coquimbo": "832.864 Habitantes"
    };

    // ESTILO
    const style = (feature) => {
        const isSede = feature.properties.Region === "Región de Antofagasta";
        return {
            fillColor: isSede ? '#C89B3C' : '#003366',
            weight: 3,
            color: 'white',
            fillOpacity: 0.6
        };
    };

    let geojson;

    const highlightFeature = (e) => {
        e.target.setStyle({
            weight: 5,
            color: '#C89B3C',
            fillOpacity: 0.8
        });
    };

    const resetHighlight = (e) => {
        geojson.resetStyle(e.target);
    };

    // POPUP MODIFICADO
    const onEachFeature = (feature, layer) => {
        layer.on({
            mouseover: highlightFeature,
            mouseout: resetHighlight,
            click: (e) => map.fitBounds(e.target.getBounds())
        });

        layer.bindPopup(`
            <div style="font-family: Arial; font-size: 14px;">
                <strong style="color:#1F4C6E;">${feature.properties.Region}</strong><br>
                <span style="color:#555;">${poblacion[feature.properties.Region] || ''}</span>
            </div>
        `);
    };

    // MARCADORES
    const jurisprudence = [
        { lat: -18.478, lng: -70.312, title: "Fallo Arica - Protección Bofedales", type: "Recurso de Reclamación" },
        { lat: -20.213, lng: -70.150, title: "Iquique - Evaluación Impacto Minero", type: "Demanda por Daño" },
        { lat: -23.650, lng: -70.397, title: "Antofagasta - Sede 1TA", type: "Central Jurisprudencial" },
        { lat: -27.366, lng: -70.332, title: "Atacama - Gestión Hídrica Pascua Lama", type: "Resolución Histórica" },
        { lat: -29.953, lng: -71.339, title: "Coquimbo - Dominga Caso Emblemático", type: "Sentencia Destacada" }
    ];

    jurisprudence.forEach(j => {
        L.circleMarker([j.lat, j.lng], {
            radius: 8,
            fillColor: "#C89B3C",
            color: "#fff",
            weight: 2,
            fillOpacity: 0.8
        }).addTo(map)
        .bindPopup(`<b>${j.title}</b><br>${j.type}`);
    });

    // GEOJSON
    fetch('https://raw.githubusercontent.com/fcortes/Chile-GeoJSON/master/Regional.geojson')
        .then(res => res.json())
        .then(data => {

            const filtered = {
                ...data,
                features: data.features.filter(f => targetRegions.includes(f.properties.Region))
            };

            geojson = L.geoJson(filtered, {
                style: style,
                onEachFeature: onEachFeature
            }).addTo(map);

            map.fitBounds(geojson.getBounds(), { padding: [20, 20] });
        });

});
</script>
<?php
}
add_action('wp_footer', 'tribunal_mapa_script');