// The Places page — interactive world map of hidden sites.
//
// Loads Leaflet from CDN with CartoDB Dark Matter tiles (free, dark theme
// that matches the site). Drops 30+ markers in six categories. Click a
// marker for its story. If Leaflet/internet fails, falls back to a list
// view so every location remains readable offline.
//
// Phase 1: online-only map. Phase 2 (later): optional offline tile bundle
// in a "with maps" download variant.

(function () {
  const root = document.getElementById("places");
  if (!root) return;
  const view = document.querySelector('.view[data-view="places"]');
  if (!view) return;

  const t = (k, fb) => (window.i18n && window.i18n.t) ? window.i18n.t(k, fb) : fb;

  const CATEGORIES = {
    civilization: { color: "#eab308", label: "Hidden Civilization" },
    sacred:       { color: "#f59e0b", label: "Sacred Site" },
    power:        { color: "#ef4444", label: "Power Center" },
    restricted:   { color: "#a855f7", label: "Restricted Zone" },
    falseflag:    { color: "#fb923c", label: "False Flag Site" },
    elite:        { color: "#3b82f6", label: "Elite Compound" }
  };

  // 30 starter locations. lat/lng in decimal degrees.
  const PLACES = [
    // Hidden Civilization
    { id: "giza",          cat: "civilization", lat: 29.9792, lng: 31.1342, zoom: 16, timeline: "tl_e5"  },
    { id: "gobekli",       cat: "civilization", lat: 37.2231, lng: 38.9226, zoom: 17, timeline: "tl_e2"  },
    { id: "puma_punku",    cat: "civilization", lat:-16.5616, lng:-68.6781, zoom: 17, timeline: "tl_e43" },
    { id: "easter_island", cat: "civilization", lat:-27.1213, lng:-109.367, zoom: 13 },
    { id: "nazca",         cat: "civilization", lat:-14.7390, lng:-75.1300, zoom: 13, timeline: "tl_e46" },
    { id: "baalbek",       cat: "civilization", lat: 34.0067, lng: 36.2039, zoom: 17, timeline: "tl_e44" },
    { id: "yonaguni",      cat: "civilization", lat: 24.4378, lng: 122.999, zoom: 16 },
    { id: "stonehenge",    cat: "civilization", lat: 51.1789, lng: -1.8262, zoom: 17, timeline: "tl_e41" },
    { id: "adams_calendar",cat: "civilization", lat:-25.7479, lng: 30.7903, zoom: 15 },
    { id: "newgrange",     cat: "civilization", lat: 53.6947, lng: -6.4756, zoom: 17, timeline: "tl_e40" },
    { id: "gobekli_malta", cat: "civilization", lat: 36.0467, lng: 14.2697, zoom: 17, timeline: "tl_e42" },

    // Sacred Sites
    { id: "mount_kailash", cat: "sacred", lat: 31.0673, lng: 81.3122, zoom: 13 },
    { id: "uluru",         cat: "sacred", lat:-25.3444, lng: 131.036, zoom: 14 },
    { id: "mecca",         cat: "sacred", lat: 21.4225, lng: 39.8262, zoom: 16 },
    { id: "varanasi",      cat: "sacred", lat: 25.3176, lng: 83.0064, zoom: 14 },

    // Power Centers
    { id: "fed_nyc",       cat: "power", lat: 40.7080, lng: -74.0085, zoom: 18, timeline: "tl_e21" },
    { id: "bis_basel",     cat: "power", lat: 47.5481, lng: 7.5919,   zoom: 18 },
    { id: "city_london",   cat: "power", lat: 51.5145, lng: -0.0908,  zoom: 15 },
    { id: "vatican_bank",  cat: "power", lat: 41.9036, lng: 12.4577,  zoom: 17, timeline: "tl_e132" },
    { id: "who_geneva",    cat: "power", lat: 46.2324, lng: 6.1369,   zoom: 17, timeline: "tl_e111" },
    { id: "wef_davos",     cat: "power", lat: 46.8023, lng: 9.8369,   zoom: 16, timeline: "tl_e85" },
    { id: "nato_brussels", cat: "power", lat: 50.8794, lng: 4.4225,   zoom: 17 },

    // Restricted Zones
    { id: "area_51",       cat: "restricted", lat: 37.2350, lng: -115.811, zoom: 13 },
    { id: "antarctica",    cat: "restricted", lat:-82.5,    lng:  0,       zoom: 4  },
    { id: "denver_airport",cat: "restricted", lat: 39.8617, lng: -104.673, zoom: 14 },
    { id: "cheyenne_mtn",  cat: "restricted", lat: 38.7423, lng: -104.847, zoom: 14 },
    { id: "cern",          cat: "restricted", lat: 46.2333, lng: 6.0556,   zoom: 14 },

    // False Flag / Mass Casualty
    { id: "ground_zero",   cat: "falseflag", lat: 40.7115, lng: -74.0134, zoom: 17, timeline: "tl_e33" },
    { id: "lahaina",       cat: "falseflag", lat: 20.8783, lng: -156.679, zoom: 14, timeline: "tl_e110" },
    { id: "sandy_hook",    cat: "falseflag", lat: 41.4174, lng: -73.2737, zoom: 17, timeline: "tl_e140" },

    // Elite Compounds
    { id: "bohemian_grove",cat: "elite", lat: 38.5193, lng: -123.005, zoom: 14, timeline: "tl_e118" },
    { id: "epstein_island",cat: "elite", lat: 18.3076, lng: -64.8260, zoom: 15, timeline: "tl_e106" },
    { id: "skull_bones",   cat: "elite", lat: 41.3104, lng: -72.9301, zoom: 18, timeline: "tl_e117" },

    // ====== Second wave: +40 ======
    // Hidden Civilizations (10)
    { id: "caral",         cat: "civilization", lat:-10.8930, lng:-77.5240, zoom: 14, timeline: "tl_e43" },
    { id: "machu_picchu",  cat: "civilization", lat:-13.1631, lng:-72.5450, zoom: 15 },
    { id: "teotihuacan",   cat: "civilization", lat: 19.6925, lng:-98.8438, zoom: 15 },
    { id: "chichen_itza",  cat: "civilization", lat: 20.6843, lng:-88.5678, zoom: 16 },
    { id: "catalhoyuk",    cat: "civilization", lat: 37.6669, lng: 32.8278, zoom: 16, timeline: "tl_e38" },
    { id: "cahokia",       cat: "civilization", lat: 38.6594, lng:-90.0608, zoom: 14 },
    { id: "nan_madol",     cat: "civilization", lat:  6.8413, lng: 158.3325, zoom: 16 },
    { id: "angkor_wat",    cat: "civilization", lat: 13.4125, lng: 103.867,  zoom: 14 },
    { id: "mohenjo_daro",  cat: "civilization", lat: 27.3294, lng: 68.1383, zoom: 15 },
    { id: "coral_castle",  cat: "civilization", lat: 25.5005, lng:-80.4445, zoom: 18, timeline: "tl_e72" },

    // Sacred Sites (8)
    { id: "sedona",        cat: "sacred", lat: 34.8697, lng:-111.7610, zoom: 12 },
    { id: "glastonbury",   cat: "sacred", lat: 51.1474, lng: -2.7144, zoom: 14 },
    { id: "mount_shasta",  cat: "sacred", lat: 41.4099, lng:-122.1949, zoom: 11 },
    { id: "bodh_gaya",     cat: "sacred", lat: 24.6951, lng: 84.9914, zoom: 16 },
    { id: "potala",        cat: "sacred", lat: 29.6573, lng: 91.117,  zoom: 16 },
    { id: "lalibela",      cat: "sacred", lat: 12.0319, lng: 39.0473, zoom: 16 },
    { id: "mount_sinai",   cat: "sacred", lat: 28.5392, lng: 33.975,  zoom: 13 },
    { id: "lourdes",       cat: "sacred", lat: 43.0980, lng: -0.0507, zoom: 16 },

    // Power Centers (6)
    { id: "blackrock_hq",  cat: "power", lat: 40.7574, lng:-73.9776, zoom: 18 },
    { id: "tavistock",     cat: "power", lat: 51.5235, lng: -0.1278, zoom: 17 },
    { id: "pentagon",      cat: "power", lat: 38.8719, lng:-77.0563, zoom: 16 },
    { id: "bilderberg_hotel", cat: "power", lat: 52.0967, lng: 5.7898, zoom: 17 },
    { id: "cfr_pratt",     cat: "power", lat: 40.7686, lng:-73.9665, zoom: 18, timeline: "tl_e124" },
    { id: "imf_hq",        cat: "power", lat: 38.8989, lng:-77.0427, zoom: 17, timeline: "tl_e26" },

    // Restricted Zones (7)
    { id: "haarp",         cat: "restricted", lat: 62.3920, lng:-145.150, zoom: 13 },
    { id: "pine_gap",      cat: "restricted", lat:-23.7990, lng: 133.737, zoom: 14 },
    { id: "mauna_kea",     cat: "restricted", lat: 19.8204, lng:-155.468, zoom: 13 },
    { id: "dulce",         cat: "restricted", lat: 36.9358, lng:-106.996, zoom: 12 },
    { id: "tunguska",      cat: "restricted", lat: 60.8855, lng: 101.894, zoom: 10, timeline: "tl_e68" },
    { id: "bermuda_triangle", cat: "restricted", lat: 25.0000, lng:-71.0000, zoom: 5 },
    { id: "autec",         cat: "restricted", lat: 24.5847, lng:-77.7136, zoom: 12 },

    // False Flag / Mass Casualty (7)
    { id: "pearl_harbor",  cat: "falseflag", lat: 21.3666, lng:-157.937, zoom: 14, timeline: "tl_e74" },
    { id: "reichstag",     cat: "falseflag", lat: 52.5186, lng: 13.3762, zoom: 17, timeline: "tl_e73" },
    { id: "okc_murrah",    cat: "falseflag", lat: 35.4731, lng:-97.5181, zoom: 17 },
    { id: "uss_liberty",   cat: "falseflag", lat: 31.5000, lng: 34.5000, zoom: 8, timeline: "tl_e82" },
    { id: "boston_marathon",cat:"falseflag", lat: 42.3496, lng:-71.0786, zoom: 17, timeline: "tl_e141" },
    { id: "las_vegas",     cat: "falseflag", lat: 36.0925, lng:-115.176, zoom: 16, timeline: "tl_e146" },
    { id: "butler_pa",     cat: "falseflag", lat: 40.8625, lng:-79.8950, zoom: 15, timeline: "tl_e112" },

    // Elite Compounds (2)
    { id: "sun_valley",    cat: "elite", lat: 43.6970, lng:-114.352, zoom: 14 },
    { id: "camp_david",    cat: "elite", lat: 39.6478, lng:-77.4669, zoom: 14 },

    // ====== Third wave: +40 ======
    // Hidden Civilizations (8)
    { id: "petra",         cat: "civilization", lat: 30.3285, lng: 35.4444, zoom: 16 },
    { id: "olmec",         cat: "civilization", lat: 18.1029, lng:-94.0359, zoom: 15 },
    { id: "karahan_tepe",  cat: "civilization", lat: 37.0833, lng: 38.9333, zoom: 17 },
    { id: "carnac",        cat: "civilization", lat: 47.5829, lng: -3.0758, zoom: 16 },
    { id: "ur",            cat: "civilization", lat: 30.9633, lng: 46.1031, zoom: 16 },
    { id: "mount_padang",  cat: "civilization", lat: -6.9941, lng: 107.0567, zoom: 16 },
    { id: "sacsayhuaman",  cat: "civilization", lat:-13.5097, lng:-71.9817, zoom: 16 },
    { id: "cienfuegos",    cat: "civilization", lat: 22.2222, lng:-83.4944, zoom: 11 },

    // Sacred Sites (6)
    { id: "hagia_sophia",  cat: "sacred", lat: 41.0086, lng: 28.9802, zoom: 17 },
    { id: "borobudur",     cat: "sacred", lat: -7.6079, lng: 110.2038, zoom: 17 },
    { id: "avebury",       cat: "sacred", lat: 51.4286, lng: -1.8540, zoom: 15 },
    { id: "mont_st_michel",cat: "sacred", lat: 48.6361, lng: -1.5117, zoom: 16 },
    { id: "mount_olympus", cat: "sacred", lat: 40.0858, lng: 22.3585, zoom: 12 },
    { id: "externsteine",  cat: "sacred", lat: 51.8689, lng: 8.9192,  zoom: 17 },

    // Power Centers (6)
    { id: "vanguard_hq",   cat: "power", lat: 40.0379, lng:-75.5311, zoom: 16 },
    { id: "state_street_hq",cat:"power", lat: 42.3596, lng:-71.0563, zoom: 17 },
    { id: "white_house",   cat: "power", lat: 38.8977, lng:-77.0365, zoom: 17 },
    { id: "bank_of_england",cat:"power", lat: 51.5142, lng: -0.0884, zoom: 18 },
    { id: "nyse",          cat: "power", lat: 40.7069, lng:-74.0113, zoom: 18 },
    { id: "goldman_sachs", cat: "power", lat: 40.7178, lng:-74.0142, zoom: 17 },

    // Restricted Zones (6)
    { id: "diego_garcia",  cat: "restricted", lat: -7.3133, lng: 72.4111, zoom: 11 },
    { id: "lake_vostok",   cat: "restricted", lat:-77.5000, lng: 106.000, zoom: 5  },
    { id: "yamantau",      cat: "restricted", lat: 54.2533, lng: 58.1119, zoom: 12 },
    { id: "dugway",        cat: "restricted", lat: 40.1939, lng:-112.749, zoom: 11 },
    { id: "vatican_archive",cat:"restricted", lat: 41.9036, lng: 12.4571, zoom: 18 },
    { id: "kapustin_yar",  cat: "restricted", lat: 48.5731, lng: 46.2933, zoom: 12 },

    // False Flag / Mass Casualty (9)
    { id: "shanksville",   cat: "falseflag", lat: 40.0500, lng:-78.9067, zoom: 14, timeline: "tl_e33" },
    { id: "lockerbie",     cat: "falseflag", lat: 55.1209, lng: -3.3580, zoom: 14 },
    { id: "mh17_crash",    cat: "falseflag", lat: 48.1339, lng: 38.6500, zoom: 12, timeline: "tl_e144" },
    { id: "christchurch",  cat: "falseflag", lat:-43.5310, lng: 172.6280, zoom: 17 },
    { id: "bataclan",      cat: "falseflag", lat: 48.8632, lng: 2.3706,  zoom: 18 },
    { id: "chernobyl",     cat: "falseflag", lat: 51.3893, lng: 30.0991, zoom: 14 },
    { id: "fukushima",     cat: "falseflag", lat: 37.4214, lng: 141.0325, zoom: 14 },
    { id: "hiroshima",     cat: "falseflag", lat: 34.3953, lng: 132.4540, zoom: 16 },
    { id: "beirut_port",   cat: "falseflag", lat: 33.9000, lng: 35.5167, zoom: 16 },

    // Elite Compounds (5)
    { id: "aspen_institute",cat:"elite", lat: 39.1842, lng:-106.829, zoom: 16 },
    { id: "pocantico",     cat: "elite", lat: 41.1093, lng:-73.8431, zoom: 15 },
    { id: "mar_a_lago",    cat: "elite", lat: 26.6770, lng:-80.0379, zoom: 16 },
    { id: "trump_tower",   cat: "elite", lat: 40.7625, lng:-73.9740, zoom: 18 },
    { id: "hearst_castle", cat: "elite", lat: 35.6852, lng:-121.168, zoom: 16 },

    // ====== Fourth wave: +31 ======
    // Sacred (3)
    { id: "temple_mount",  cat: "sacred", lat: 31.7780, lng: 35.2354, zoom: 17 },
    { id: "fatima",        cat: "sacred", lat: 39.6321, lng: -8.6724, zoom: 16 },
    { id: "medjugorje",    cat: "sacred", lat: 43.1856, lng: 17.6789, zoom: 14 },

    // Power Centers (15) — incl. COVID labs, chokepoints, tech HQs, media HQs, Berlin Wall
    { id: "wuhan_wiv",     cat: "power", lat: 30.5417, lng: 114.2614, zoom: 16 },
    { id: "fort_detrick",  cat: "power", lat: 39.4350, lng:-77.4225, zoom: 14 },
    { id: "three_gorges",  cat: "power", lat: 30.8235, lng: 111.0033, zoom: 13 },
    { id: "suez",          cat: "power", lat: 30.0444, lng: 32.3494, zoom: 11 },
    { id: "panama_canal",  cat: "power", lat:  9.0790, lng:-79.6803, zoom: 12 },
    { id: "hormuz",        cat: "power", lat: 26.5577, lng: 56.2497, zoom: 8  },
    { id: "bretton_woods", cat: "power", lat: 44.2581, lng:-71.4419, zoom: 15, timeline: "tl_e26" },
    { id: "googleplex",    cat: "power", lat: 37.4221, lng:-122.0841, zoom: 16 },
    { id: "meta_hq",       cat: "power", lat: 37.4848, lng:-122.1485, zoom: 17 },
    { id: "openai_hq",     cat: "power", lat: 37.7723, lng:-122.4185, zoom: 17 },
    { id: "x_hq",          cat: "power", lat: 37.7762, lng:-122.4172, zoom: 17 },
    { id: "bbc",           cat: "power", lat: 51.5189, lng: -0.1438, zoom: 17 },
    { id: "cnn_hq",        cat: "power", lat: 33.7572, lng:-84.3946, zoom: 17 },
    { id: "fox_hq",        cat: "power", lat: 40.7574, lng:-73.9856, zoom: 17 },
    { id: "berlin_wall",   cat: "power", lat: 52.5163, lng: 13.3777, zoom: 16 },

    // Restricted Zones (4) — UFO/paranormal cluster
    { id: "roswell_town",  cat: "restricted", lat: 33.3940, lng:-104.523, zoom: 13 },
    { id: "skinwalker",    cat: "restricted", lat: 40.2548, lng:-109.889, zoom: 14 },
    { id: "san_luis_valley",cat:"restricted", lat: 37.7136, lng:-105.927, zoom: 9  },
    { id: "phoenix_lights",cat: "restricted", lat: 33.4484, lng:-112.074, zoom: 11, timeline: "tl_e138" },

    // False Flag / Mass Casualty (7)
    { id: "huanan_market", cat: "falseflag", lat: 30.6189, lng: 114.2575, zoom: 17 },
    { id: "tiananmen",     cat: "falseflag", lat: 39.9055, lng: 116.3914, zoom: 15 },
    { id: "auschwitz",     cat: "falseflag", lat: 50.0265, lng: 19.2030, zoom: 15 },
    { id: "wannsee",       cat: "falseflag", lat: 52.4338, lng: 13.1644, zoom: 17 },
    { id: "nuremberg",     cat: "falseflag", lat: 49.4534, lng: 11.0489, zoom: 17 },
    { id: "bhopal",        cat: "falseflag", lat: 23.2787, lng: 77.4106, zoom: 15 },
    { id: "three_mile_island",cat:"falseflag",lat:40.1531, lng:-76.7244, zoom: 14 },

    // Elite Compounds (2)
    { id: "buckingham",    cat: "elite", lat: 51.5014, lng: -0.1419, zoom: 17 },
    { id: "balmoral",      cat: "elite", lat: 57.0392, lng: -3.2300, zoom: 15 },

    // ====== Fifth wave: +35 ======
    // Power Centers (11) — intel HQs, foreign governments, more banking
    { id: "cia_langley",   cat: "power", lat: 38.9517, lng:-77.1467, zoom: 16 },
    { id: "nsa_fort_meade",cat: "power", lat: 39.1080, lng:-76.7706, zoom: 16 },
    { id: "fbi_hq",        cat: "power", lat: 38.8951, lng:-77.0246, zoom: 17 },
    { id: "kremlin",       cat: "power", lat: 55.7520, lng: 37.6175, zoom: 17 },
    { id: "zhongnanhai",   cat: "power", lat: 39.9134, lng: 116.3833, zoom: 16 },
    { id: "elysee",        cat: "power", lat: 48.8703, lng:  2.3169, zoom: 17 },
    { id: "ecb_frankfurt", cat: "power", lat: 50.1099, lng:  8.7019, zoom: 17 },
    { id: "waddesdon",     cat: "power", lat: 51.8417, lng: -0.9408, zoom: 15 },
    { id: "mouton",        cat: "power", lat: 45.2167, lng: -0.7708, zoom: 16 },
    { id: "saudi_aramco",  cat: "power", lat: 26.2885, lng: 50.1538, zoom: 15 },
    { id: "pboc",          cat: "power", lat: 39.9028, lng: 116.3814, zoom: 17 },

    // Restricted Zones (8) — seed vault, supervolcanoes, nuclear sites
    { id: "svalbard_seed", cat: "restricted", lat: 78.2360, lng: 15.4914, zoom: 13 },
    { id: "yellowstone",   cat: "restricted", lat: 44.4280, lng:-110.589, zoom: 9  },
    { id: "new_madrid",    cat: "restricted", lat: 36.5867, lng:-89.5283, zoom: 8  },
    { id: "trinity_site",  cat: "restricted", lat: 33.6776, lng:-106.476, zoom: 13 },
    { id: "bikini_atoll",  cat: "restricted", lat: 11.6090, lng: 165.385, zoom: 11 },
    { id: "mururoa",       cat: "restricted", lat:-21.8333, lng:-138.900, zoom: 10 },
    { id: "semipalatinsk", cat: "restricted", lat: 50.4083, lng: 78.5167, zoom: 9  },
    { id: "novaya_zemlya", cat: "restricted", lat: 73.7000, lng: 54.9667, zoom: 7  },

    // False Flag / Mass Casualty (6)
    { id: "charlie_hebdo", cat: "falseflag", lat: 48.8616, lng:  2.3700, zoom: 18 },
    { id: "london_77",     cat: "falseflag", lat: 51.5167, lng: -0.0775, zoom: 14 },
    { id: "beirut_barracks",cat:"falseflag", lat: 33.8232, lng: 35.5125, zoom: 14 },
    { id: "madrid_2004",   cat: "falseflag", lat: 40.4044, lng: -3.6745, zoom: 17 },
    { id: "utoya",         cat: "falseflag", lat: 60.2667, lng: 10.3667, zoom: 14 },
    { id: "pulse_orlando", cat: "falseflag", lat: 28.5260, lng:-81.3766, zoom: 18 },

    // Hidden Civilizations (8) — cave art, deep Egypt, lost cities
    { id: "lascaux",       cat: "civilization", lat: 45.0541, lng:  1.1722, zoom: 14 },
    { id: "chauvet",       cat: "civilization", lat: 44.3873, lng:  4.4178, zoom: 14 },
    { id: "saqqara",       cat: "civilization", lat: 29.8714, lng: 31.2167, zoom: 15 },
    { id: "dendera",       cat: "civilization", lat: 26.1417, lng: 32.6700, zoom: 17 },
    { id: "osirion",       cat: "civilization", lat: 26.1849, lng: 31.9183, zoom: 18 },
    { id: "knossos",       cat: "civilization", lat: 35.2980, lng: 25.1632, zoom: 16 },
    { id: "mesa_verde",    cat: "civilization", lat: 37.1853, lng:-108.486, zoom: 13 },
    { id: "great_zimbabwe",cat: "civilization", lat:-20.2667, lng: 30.9333, zoom: 15 },

    // Elite Compounds (2)
    { id: "windsor_castle",cat: "elite", lat: 51.4839, lng: -0.6044, zoom: 16 },
    { id: "sandringham",   cat: "elite", lat: 52.8311, lng:  0.5142, zoom: 14 },

    // ====== Sixth wave: +33 ======
    // Power Centers (16)
    { id: "pfizer_hq",     cat: "power", lat: 40.7505, lng:-73.9685, zoom: 17 },
    { id: "moderna_hq",    cat: "power", lat: 42.3622, lng:-71.0850, zoom: 17 },
    { id: "jnj_hq",        cat: "power", lat: 40.4923, lng:-74.4451, zoom: 16 },
    { id: "gates_foundation",cat:"power", lat: 47.6233, lng:-122.3517,zoom: 17 },
    { id: "mossad_hq",     cat: "power", lat: 32.0853, lng: 34.7818, zoom: 15 },
    { id: "knesset",       cat: "power", lat: 31.7767, lng: 35.2052, zoom: 17 },
    { id: "dimona",        cat: "power", lat: 31.0019, lng: 35.1453, zoom: 13 },
    { id: "putin_palace",  cat: "power", lat: 44.4147, lng: 38.3650, zoom: 15 },
    { id: "gchq",          cat: "power", lat: 51.8993, lng: -2.1242, zoom: 16 },
    { id: "cse_ottawa",    cat: "power", lat: 45.3590, lng:-75.6510, zoom: 16 },
    { id: "asd_canberra",  cat: "power", lat:-35.2885, lng: 149.1226, zoom: 16 },
    { id: "nyt_hq",        cat: "power", lat: 40.7561, lng:-73.9903, zoom: 17 },
    { id: "washington_post",cat:"power", lat: 38.8989, lng:-77.0369, zoom: 17 },
    { id: "reuters_london",cat: "power", lat: 51.5050, lng: -0.0237, zoom: 17 },
    { id: "hong_kong",     cat: "power", lat: 22.3193, lng: 114.1694,zoom: 13 },
    { id: "taiwan_strait", cat: "power", lat: 24.5000, lng: 119.5000,zoom: 7  },

    // Restricted Zones (5)
    { id: "mount_weather", cat: "restricted", lat: 39.0625, lng:-77.8917, zoom: 13 },
    { id: "raven_rock",    cat: "restricted", lat: 39.7339, lng:-77.4111, zoom: 14 },
    { id: "greenbrier",    cat: "restricted", lat: 37.7842, lng:-80.3037, zoom: 15 },
    { id: "xinjiang",      cat: "restricted", lat: 41.7480, lng: 84.7150, zoom: 7  },
    { id: "tor_project",   cat: "restricted", lat: 47.6190, lng:-122.3496,zoom: 16 },

    // Hidden Civilizations (4)
    { id: "tikal",         cat: "civilization", lat: 17.2228, lng:-89.6235, zoom: 15 },
    { id: "palenque",      cat: "civilization", lat: 17.4836, lng:-92.0461, zoom: 16 },
    { id: "skara_brae",    cat: "civilization", lat: 59.0490, lng: -3.3431, zoom: 17 },
    { id: "wardenclyffe",  cat: "civilization", lat: 40.9447, lng:-72.8975, zoom: 17, timeline: "tl_e121" },

    // False Flag / Mass Casualty (4)
    { id: "wtc7",          cat: "falseflag", lat: 40.7136, lng:-74.0119, zoom: 18, timeline: "tl_e33" },
    { id: "nashville_christmas",cat:"falseflag", lat: 36.1627, lng:-86.7745, zoom: 17 },
    { id: "parkland",      cat: "falseflag", lat: 26.3068, lng:-80.2674, zoom: 17 },
    { id: "gaza",          cat: "falseflag", lat: 31.5017, lng: 34.4668, zoom: 11, timeline: "tl_e150" },

    // Elite Compounds (4)
    { id: "gates_compound",cat: "elite", lat: 47.6238, lng:-122.2348, zoom: 16 },
    { id: "disneyland",    cat: "elite", lat: 33.8121, lng:-117.919, zoom: 14 },
    { id: "hollywood",     cat: "elite", lat: 34.1341, lng:-118.322, zoom: 14 },
    { id: "burning_man",   cat: "elite", lat: 40.7864, lng:-119.207, zoom: 12 },

    // ====== Seventh wave (final map batch): +34 ======
    // Sacred (8)
    { id: "notre_dame",    cat: "sacred", lat: 48.8530, lng:  2.3499, zoom: 17 },
    { id: "forbidden_city",cat: "sacred", lat: 39.9163, lng: 116.3972, zoom: 15 },
    { id: "sistine",       cat: "sacred", lat: 41.9029, lng: 12.4534, zoom: 18 },
    { id: "christ_redeemer",cat:"sacred", lat:-22.9519, lng:-43.2105, zoom: 16 },
    { id: "ise_shrine",    cat: "sacred", lat: 34.4549, lng: 136.725, zoom: 16 },
    { id: "mount_fuji",    cat: "sacred", lat: 35.3606, lng: 138.727, zoom: 12 },
    { id: "mount_ararat",  cat: "sacred", lat: 39.7019, lng: 44.2983, zoom: 11 },
    { id: "sagrada_familia",cat:"sacred", lat: 41.4036, lng:  2.1744, zoom: 17 },

    // Power Centers (4) — space + finance
    { id: "cape_canaveral",cat: "power", lat: 28.5729, lng:-80.6490, zoom: 13 },
    { id: "spacex_boca",   cat: "power", lat: 25.9962, lng:-97.1556, zoom: 14 },
    { id: "swift_brussels",cat: "power", lat: 50.7370, lng:  4.5990, zoom: 17 },
    { id: "fed_dc",        cat: "power", lat: 38.8920, lng:-77.0455, zoom: 17 },

    // Restricted Zones (15) — nuclear, geological, North Korea, bio, UFO, space, Saudi
    { id: "sellafield",    cat: "restricted", lat: 54.4218, lng: -3.4994, zoom: 14 },
    { id: "hanford",       cat: "restricted", lat: 46.5500, lng:-119.490, zoom: 11 },
    { id: "cumbre_vieja",  cat: "restricted", lat: 28.5683, lng: -17.840, zoom: 12 },
    { id: "toba",          cat: "restricted", lat:  2.6839, lng: 98.8756, zoom: 9  },
    { id: "vesuvius",      cat: "restricted", lat: 40.8210, lng: 14.4263, zoom: 13 },
    { id: "dmz_korea",     cat: "restricted", lat: 38.2658, lng: 127.003, zoom: 9  },
    { id: "pyongyang",     cat: "restricted", lat: 39.0392, lng: 125.762, zoom: 12 },
    { id: "camp_22",       cat: "restricted", lat: 42.0833, lng: 130.067, zoom: 12 },
    { id: "baikonur",      cat: "restricted", lat: 45.9646, lng: 63.3052, zoom: 13 },
    { id: "natanz",        cat: "restricted", lat: 33.7244, lng: 51.7269, zoom: 14 },
    { id: "neom",          cat: "restricted", lat: 27.5114, lng: 35.7350, zoom: 10 },
    { id: "pirbright",     cat: "restricted", lat: 51.2384, lng: -0.6347, zoom: 15 },
    { id: "galveston_lab", cat: "restricted", lat: 29.3060, lng:-94.7720, zoom: 17 },
    { id: "belgian_ufo",   cat: "restricted", lat: 50.5039, lng:  4.4699, zoom: 7  },
    { id: "uss_nimitz",    cat: "restricted", lat: 32.7000, lng:-120.000, zoom: 7  },

    // False Flag / Mass Casualty (5)
    { id: "nagasaki",      cat: "falseflag", lat: 32.7733, lng: 129.864, zoom: 15 },
    { id: "mayak",         cat: "falseflag", lat: 55.7026, lng: 60.8472, zoom: 11 },
    { id: "khashoggi",     cat: "falseflag", lat: 41.0857, lng: 29.0157, zoom: 18 },
    { id: "robben_island", cat: "falseflag", lat:-33.8067, lng: 18.3697, zoom: 15 },
    { id: "battle_la",     cat: "falseflag", lat: 34.0522, lng:-118.244, zoom: 11 },

    // Hidden Civilizations (2)
    { id: "persepolis",    cat: "civilization", lat: 29.9355, lng: 52.8916, zoom: 16 },
    { id: "cappadocia",    cat: "civilization", lat: 38.3733, lng: 34.7322, zoom: 14 }
  ];

  const mapEl     = root.querySelector(".pls-map");
  const detailsEl = root.querySelector(".pls-details");
  const filtersEl = root.querySelector(".pls-filters");
  const listEl    = root.querySelector(".pls-list");
  const fallbackEl= root.querySelector(".pls-fallback");

  let map = null;
  let markers = {};
  let activeFilter = "all";

  // ---- Build filter chips -----------------------------------------------
  const filters = ["all"].concat(Object.keys(CATEGORIES));
  filters.forEach((id) => {
    const chip = document.createElement("button");
    chip.type = "button";
    chip.className = "pls-chip" + (id === "all" ? " active" : "");
    chip.dataset.filter = id;
    chip.style.setProperty("--c", id === "all" ? "#cbd5e1" : CATEGORIES[id].color);
    const i18nKey = id === "all" ? "page.places.pls_all" : "page.places.pls_cat_" + id;
    chip.setAttribute("data-i18n", i18nKey);
    chip.textContent = id === "all" ? "All" : CATEGORIES[id].label;
    filtersEl.appendChild(chip);
  });
  filtersEl.addEventListener("click", (e) => {
    const chip = e.target.closest(".pls-chip");
    if (!chip) return;
    activeFilter = chip.dataset.filter;
    filtersEl.querySelectorAll(".pls-chip").forEach((c) =>
      c.classList.toggle("active", c === chip));
    applyFilter();
  });
  function applyFilter() {
    PLACES.forEach((p) => {
      const visible = (activeFilter === "all" || p.cat === activeFilter);
      const m = markers[p.id];
      if (m && map) {
        if (visible) m.addTo(map); else map.removeLayer(m);
      }
      const card = listEl.querySelector('[data-id="' + p.id + '"]');
      if (card) card.style.display = visible ? "" : "none";
    });
  }

  // ---- Build the offline list (always renders, even if map fails) -------
  function buildList() {
    listEl.innerHTML = "";
    PLACES.forEach((p) => {
      const cat = CATEGORIES[p.cat];
      const card = document.createElement("button");
      card.type = "button";
      card.className = "pls-list-card";
      card.dataset.id = p.id;
      card.style.setProperty("--c", cat.color);
      card.innerHTML =
        '<div class="pls-list-cat">' + t("page.places.pls_cat_" + p.cat, cat.label) + '</div>' +
        '<div class="pls-list-name" data-i18n="page.places.pls_' + p.id + '_name">' + p.id + '</div>' +
        '<div class="pls-list-where" data-i18n="page.places.pls_' + p.id + '_where">…</div>';
      listEl.appendChild(card);
    });
  }
  listEl.addEventListener("click", (e) => {
    const card = e.target.closest(".pls-list-card");
    if (!card) return;
    showPlace(card.dataset.id);
    if (map && markers[card.dataset.id]) {
      const p = PLACES.find((x) => x.id === card.dataset.id);
      map.flyTo([p.lat, p.lng], p.zoom || 13, { duration: 1.2 });
      markers[p.id].openPopup();
    } else {
      detailsEl.scrollIntoView({ behavior: "smooth", block: "nearest" });
    }
  });

  // ---- Details panel ----------------------------------------------------
  let selected = null;
  function showPlace(id) {
    const p = PLACES.find((x) => x.id === id);
    if (!p) return;
    selected = id;
    const cat = CATEGORIES[p.cat];
    detailsEl.innerHTML =
      '<div class="pls-d-head" style="border-color:' + cat.color + '">' +
        '<span class="pls-d-name" style="color:' + cat.color + '">'
          + t("page.places.pls_" + id + "_name", id) + '</span>' +
        '<span class="pls-d-cat" style="color:' + cat.color + '">'
          + t("page.places.pls_cat_" + p.cat, cat.label) + '</span>' +
      '</div>' +
      '<div class="pls-d-where">' + t("page.places.pls_" + id + "_where", "") + '</div>' +
      '<p class="pls-d-body">' + t("page.places.pls_" + id + "_body", "") + '</p>' +
      (p.timeline
        ? '<a class="pls-d-timeline" href="#" data-tl="' + p.timeline + '">↗ ' + t("page.places.pls_see_timeline", "See in Timeline") + '</a>'
        : "");
    detailsEl.classList.add("visible");
    listEl.querySelectorAll(".pls-list-card").forEach((c) =>
      c.classList.toggle("active", c.dataset.id === id));
  }
  detailsEl.addEventListener("click", (e) => {
    const link = e.target.closest(".pls-d-timeline");
    if (!link) return;
    e.preventDefault();
    // Switch to history view.
    const tl = link.dataset.tl;
    const histLink = document.querySelector('[data-view="history"]');
    if (histLink) histLink.click();
    // Try to highlight the matching event after a beat.
    setTimeout(() => {
      const dot = document.querySelector('.tl-dot[data-cat] [data-i18n="page.history.' + tl + '_t"]');
      const parent = dot ? dot.closest(".tl-dot") : null;
      if (parent) parent.click();
    }, 300);
  });

  // ---- Try to load Leaflet from CDN -------------------------------------
  function loadLeaflet() {
    return new Promise((resolve, reject) => {
      if (window.L) return resolve(window.L);
      const css = document.createElement("link");
      css.rel = "stylesheet";
      css.href = "https://unpkg.com/leaflet@1.9.4/dist/leaflet.css";
      css.integrity = "sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=";
      css.crossOrigin = "";
      document.head.appendChild(css);
      const script = document.createElement("script");
      script.src = "https://unpkg.com/leaflet@1.9.4/dist/leaflet.js";
      script.integrity = "sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=";
      script.crossOrigin = "";
      script.onload = () => window.L ? resolve(window.L) : reject(new Error("Leaflet didn't load"));
      script.onerror = () => reject(new Error("Leaflet script failed"));
      document.head.appendChild(script);
    });
  }
  function buildMap(L) {
    map = L.map(mapEl, {
      center: [25, 10],
      zoom: 2,
      worldCopyJump: true,
      attributionControl: true,
      zoomControl: true
    });
    L.tileLayer("https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png", {
      attribution: '© <a href="https://www.openstreetmap.org/copyright">OSM</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
      maxZoom: 19,
      subdomains: "abcd"
    }).addTo(map);

    PLACES.forEach((p) => {
      const cat = CATEGORIES[p.cat];
      const icon = L.divIcon({
        className: "pls-marker pls-marker-" + p.cat,
        html: '<span style="--c:' + cat.color + '"></span>',
        iconSize: [16, 16],
        iconAnchor: [8, 8]
      });
      const m = L.marker([p.lat, p.lng], { icon }).addTo(map);
      const popup = '<div class="pls-popup">' +
        '<div class="pls-popup-name" style="color:' + cat.color + '">'
          + t("page.places.pls_" + p.id + "_name", p.id) + '</div>' +
        '<div class="pls-popup-where">' + t("page.places.pls_" + p.id + "_where", "") + '</div>' +
        '</div>';
      m.bindPopup(popup, { closeButton: false, className: "pls-popup-wrap" });
      m.on("click", () => showPlace(p.id));
      markers[p.id] = m;
    });
    fallbackEl.style.display = "none";
  }

  loadLeaflet().then((L) => {
    try { buildMap(L); } catch (e) {
      console.warn("Places map build failed", e);
      fallbackEl.style.display = "";
    }
  }).catch(() => {
    fallbackEl.style.display = "";
  });

  // ---- Pause / activate -------------------------------------------------
  function isActive() { return view.classList.contains("active"); }
  if (map) {
    document.addEventListener("visibilitychange", () => {
      if (!isActive()) return;
      if (map && !document.hidden) map.invalidateSize();
    });
  }
  if ("MutationObserver" in window) {
    new MutationObserver(() => {
      if (isActive() && map) setTimeout(() => map.invalidateSize(), 100);
    }).observe(view, { attributes: true, attributeFilter: ["class"] });
  }
  window.addEventListener("langchange", () => {
    if (selected) showPlace(selected);
    buildList();
    applyI18nNow();
  });

  // i18n may already have run applyTranslations() before this IIFE built
  // the list. Nudge the translator across any [data-i18n] in our DOM.
  function applyI18nNow() {
    if (!window.i18n || !window.i18n.t) return;
    root.querySelectorAll("[data-i18n]").forEach((el) => {
      const k = el.getAttribute("data-i18n");
      const v = window.i18n.t(k, null);
      if (v && v !== k) el.innerHTML = v;
    });
  }

  buildList();
  applyI18nNow();
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", () => setTimeout(applyI18nNow, 0));
  }
})();
