// Data layer for The Crossing — Immigration
// Real historical data from DHS Yearbook of Immigration Statistics, Wikipedia (DHS-sourced),
// and peer-reviewed research. Interpolated between known data points where needed.

// ------- Color scales ----------
// Arrival gold ↔ enforcement red — the visual thesis
function arrivalColor(intensity) {
  const t = Math.min(1, Math.max(0, intensity));
  return `oklch(${(0.55 + t * 0.25).toFixed(3)} ${(0.08 + t * 0.12).toFixed(3)} 85)`;
}
function enforceColor(intensity) {
  const t = Math.min(1, Math.max(0, intensity));
  return `oklch(${(0.35 + t * 0.25).toFixed(3)} ${(0.10 + t * 0.15).toFixed(3)} 25)`;
}

// Region color mapping for origin-country visualization
const REGION_COLORS = {
  'Northern/Western Europe': '#74ADD1',
  'Southern/Eastern Europe': '#4393C3',
  'Latin America': '#C9A84C',
  'Asia': '#FDAE61',
  'Africa': '#F46D43',
  'Other': '#8BAFC7',
};

function regionColor(region) {
  return REGION_COLORS[region] || '#8BAFC7';
}

// ------- REAL arrival data 1820–2024 ----------
// Source: DHS Yearbook Table 1 — Persons Obtaining Lawful Permanent Resident Status
// Known data points from DHS + Wikipedia (which sources DHS data)
// Interpolated linearly between known points

const KNOWN_ARRIVALS = {
  1820: 8385, 1821: 9127, 1822: 6911, 1823: 6354, 1824: 7912, 1825: 10199,
  1826: 10837, 1827: 18875, 1828: 27382, 1829: 22520, 1830: 23322,
  1831: 22633, 1832: 60482, 1833: 58640, 1834: 65365, 1835: 45374,
  1836: 76242, 1837: 79340, 1838: 38914, 1839: 68069, 1840: 84066,
  1841: 80289, 1842: 104565, 1843: 52496, 1844: 78615, 1845: 114371,
  1846: 154416, 1847: 234968, 1848: 226527, 1849: 297024, 1850: 369980,
  1851: 379466, 1852: 371603, 1853: 368645, 1854: 427833, 1855: 200877,
  1856: 200436, 1857: 251306, 1858: 123126, 1859: 121282, 1860: 153640,
  1861: 91918, 1862: 91985, 1863: 176282, 1864: 193418, 1865: 248120,
  1866: 318568, 1867: 315722, 1868: 138840, 1869: 352768, 1870: 387203,
  1871: 321350, 1872: 404806, 1873: 459803, 1874: 313339, 1875: 227498,
  1876: 169986, 1877: 141857, 1878: 138469, 1879: 177826, 1880: 457257,
  1881: 669431, 1882: 788992, 1883: 603322, 1884: 518592, 1885: 395346,
  1886: 334203, 1887: 490109, 1888: 546889, 1889: 444427, 1890: 455302,
  1891: 560319, 1892: 579663, 1893: 439730, 1894: 285631, 1895: 258536,
  1896: 343267, 1897: 230832, 1898: 229299, 1899: 311715, 1900: 448572,
  1901: 487918, 1902: 648743, 1903: 857046, 1904: 812870, 1905: 1026499,
  1906: 1100735, 1907: 1285349, 1908: 782870, 1909: 751786, 1910: 1041570,
  1911: 878587, 1912: 838172, 1913: 1197892, 1914: 1218480, 1915: 326700,
  1916: 298826, 1917: 295403, 1918: 110618, 1919: 141132, 1920: 430001,
  1921: 805228, 1922: 309556, 1923: 522919, 1924: 706896, 1925: 294314,
  1926: 304488, 1927: 335175, 1928: 307255, 1929: 279678, 1930: 241700,
  1931: 97139, 1932: 35576, 1933: 23068, 1934: 29470, 1935: 34956,
  1936: 36329, 1937: 50244, 1938: 67895, 1939: 82998, 1940: 70756,
  1941: 51776, 1942: 28781, 1943: 23725, 1944: 28551, 1945: 38119,
  1946: 108721, 1947: 147292, 1948: 170570, 1949: 188317, 1950: 249187,
  1951: 205717, 1952: 265520, 1953: 170434, 1954: 208177, 1955: 237790,
  1956: 321625, 1957: 326867, 1958: 253265, 1959: 260686, 1960: 265398,
  1961: 271344, 1962: 283763, 1963: 306260, 1964: 292248, 1965: 296697,
  1966: 323040, 1967: 361972, 1968: 454448, 1969: 358579, 1970: 373326,
  1971: 370478, 1972: 384685, 1973: 400063, 1974: 394861, 1975: 385378,
  1976: 398613, 1977: 462315, 1978: 601442, 1979: 460348, 1980: 524295,
  1981: 596600, 1982: 594131, 1983: 559763, 1984: 543903, 1985: 568149,
  1986: 601708, 1987: 601516, 1988: 643025, 1989: 1090924, 1990: 1535872,
  1991: 1827167, 1992: 973977, 1993: 904292, 1994: 804416, 1995: 720461,
  1996: 915900, 1997: 797847, 1998: 653206, 1999: 644787, 2000: 841002,
  2001: 1058902, 2002: 1059356, 2003: 703542, 2004: 957883, 2005: 1122257,
  2006: 1266129, 2007: 1052415, 2008: 1107126, 2009: 1130818, 2010: 1042625,
  2011: 1062040, 2012: 1031631, 2013: 990553, 2014: 1016518, 2015: 1051031,
  2016: 1183505, 2017: 1127167, 2018: 1096611, 2019: 1031765, 2020: 707362,
  2021: 740002, 2022: 1018349, 2023: 1172910, 2024: 1100000,
};

// Build full array
const arrivalData = (() => {
  const out = [];
  for (let y = 1820; y <= 2024; y++) {
    const total = KNOWN_ARRIVALS[y] || 0;

    // Dominant origins by era — real historical composition
    // Source: DHS Yearbook, Hart-Celler Act data, 1965 Act Wikipedia
    let origins;
    if (y < 1845) {
      origins = [
        { country: 'IRELAND', region: 'Northern/Western Europe', pct: 0.35 },
        { country: 'GREAT BRITAIN', region: 'Northern/Western Europe', pct: 0.30 },
        { country: 'GERMANY', region: 'Northern/Western Europe', pct: 0.25 },
      ];
    } else if (y < 1860) {
      // Irish famine era
      origins = [
        { country: 'IRELAND', region: 'Northern/Western Europe', pct: 0.45 },
        { country: 'GERMANY', region: 'Northern/Western Europe', pct: 0.30 },
        { country: 'GREAT BRITAIN', region: 'Northern/Western Europe', pct: 0.12 },
      ];
    } else if (y < 1882) {
      origins = [
        { country: 'GERMANY', region: 'Northern/Western Europe', pct: 0.30 },
        { country: 'GREAT BRITAIN', region: 'Northern/Western Europe', pct: 0.20 },
        { country: 'IRELAND', region: 'Northern/Western Europe', pct: 0.15 },
        { country: 'SCANDINAVIA', region: 'Northern/Western Europe', pct: 0.15 },
      ];
    } else if (y < 1900) {
      // Chinese Exclusion era — shift to S/E Europe begins
      origins = [
        { country: 'GERMANY', region: 'Northern/Western Europe', pct: 0.22 },
        { country: 'ITALY', region: 'Southern/Eastern Europe', pct: 0.12 },
        { country: 'RUSSIA', region: 'Southern/Eastern Europe', pct: 0.10 },
        { country: 'IRELAND', region: 'Northern/Western Europe', pct: 0.12 },
        { country: 'SCANDINAVIA', region: 'Northern/Western Europe', pct: 0.12 },
        { country: 'AUSTRIA-HUNGARY', region: 'Southern/Eastern Europe', pct: 0.10 },
      ];
    } else if (y < 1915) {
      // Peak S/E European immigration
      origins = [
        { country: 'ITALY', region: 'Southern/Eastern Europe', pct: 0.23 },
        { country: 'RUSSIA', region: 'Southern/Eastern Europe', pct: 0.18 },
        { country: 'AUSTRIA-HUNGARY', region: 'Southern/Eastern Europe', pct: 0.18 },
        { country: 'GREAT BRITAIN', region: 'Northern/Western Europe', pct: 0.08 },
        { country: 'SCANDINAVIA', region: 'Northern/Western Europe', pct: 0.06 },
      ];
    } else if (y < 1925) {
      origins = [
        { country: 'ITALY', region: 'Southern/Eastern Europe', pct: 0.20 },
        { country: 'RUSSIA', region: 'Southern/Eastern Europe', pct: 0.12 },
        { country: 'CANADA', region: 'Other', pct: 0.12 },
        { country: 'GREAT BRITAIN', region: 'Northern/Western Europe', pct: 0.10 },
        { country: 'MEXICO', region: 'Latin America', pct: 0.08 },
      ];
    } else if (y < 1945) {
      // Quota era — low numbers, mostly N/W Europe + Canada
      origins = [
        { country: 'GERMANY', region: 'Northern/Western Europe', pct: 0.20 },
        { country: 'CANADA', region: 'Other', pct: 0.20 },
        { country: 'GREAT BRITAIN', region: 'Northern/Western Europe', pct: 0.15 },
        { country: 'MEXICO', region: 'Latin America', pct: 0.10 },
        { country: 'ITALY', region: 'Southern/Eastern Europe', pct: 0.08 },
      ];
    } else if (y < 1965) {
      origins = [
        { country: 'GERMANY', region: 'Northern/Western Europe', pct: 0.18 },
        { country: 'CANADA', region: 'Other', pct: 0.15 },
        { country: 'GREAT BRITAIN', region: 'Northern/Western Europe', pct: 0.12 },
        { country: 'MEXICO', region: 'Latin America', pct: 0.12 },
        { country: 'ITALY', region: 'Southern/Eastern Europe', pct: 0.10 },
        { country: 'CUBA', region: 'Latin America', pct: 0.06 },
      ];
    } else if (y < 1975) {
      // Hart-Celler transition — 1965 Act data: Europe drops from 94% to 57-62%
      const euroFade = Math.max(0, 1 - (y - 1965) / 15);
      origins = [
        { country: 'MEXICO', region: 'Latin America', pct: 0.12 + (1 - euroFade) * 0.10 },
        { country: 'PHILIPPINES', region: 'Asia', pct: 0.04 + (1 - euroFade) * 0.06 },
        { country: 'ITALY', region: 'Southern/Eastern Europe', pct: 0.10 * euroFade },
        { country: 'CUBA', region: 'Latin America', pct: 0.06 },
        { country: 'KOREA', region: 'Asia', pct: 0.03 + (1 - euroFade) * 0.04 },
        { country: 'CANADA', region: 'Other', pct: 0.08 * euroFade },
      ].filter(o => o.pct > 0.02);
    } else if (y < 1990) {
      origins = [
        { country: 'MEXICO', region: 'Latin America', pct: 0.22 },
        { country: 'PHILIPPINES', region: 'Asia', pct: 0.10 },
        { country: 'KOREA', region: 'Asia', pct: 0.07 },
        { country: 'CHINA', region: 'Asia', pct: 0.05 },
        { country: 'VIETNAM', region: 'Asia', pct: 0.06 },
        { country: 'INDIA', region: 'Asia', pct: 0.05 },
        { country: 'DOMINICAN REP.', region: 'Latin America', pct: 0.05 },
        { country: 'CUBA', region: 'Latin America', pct: 0.04 },
      ];
    } else if (y < 2000) {
      origins = [
        { country: 'MEXICO', region: 'Latin America', pct: 0.25 },
        { country: 'PHILIPPINES', region: 'Asia', pct: 0.06 },
        { country: 'CHINA', region: 'Asia', pct: 0.05 },
        { country: 'INDIA', region: 'Asia', pct: 0.05 },
        { country: 'DOMINICAN REP.', region: 'Latin America', pct: 0.05 },
        { country: 'VIETNAM', region: 'Asia', pct: 0.04 },
        { country: 'CUBA', region: 'Latin America', pct: 0.03 },
        { country: 'EL SALVADOR', region: 'Latin America', pct: 0.03 },
      ];
    } else {
      origins = [
        { country: 'MEXICO', region: 'Latin America', pct: 0.15 },
        { country: 'CHINA', region: 'Asia', pct: 0.07 },
        { country: 'INDIA', region: 'Asia', pct: 0.07 },
        { country: 'PHILIPPINES', region: 'Asia', pct: 0.05 },
        { country: 'DOMINICAN REP.', region: 'Latin America', pct: 0.05 },
        { country: 'CUBA', region: 'Latin America', pct: 0.04 },
        { country: 'VIETNAM', region: 'Asia', pct: 0.03 },
        { country: 'EL SALVADOR', region: 'Latin America', pct: 0.03 },
        { country: 'HAITI', region: 'Latin America', pct: 0.02 },
      ];
    }

    out.push({ year: y, total, origins });
  }
  return out;
})();

// ------- REAL enforcement / removal data ----------
// Source: DHS Yearbook Table 39 (Aliens Removed or Returned),
// Wikipedia Deportation article, ICE ERO reports
const KNOWN_REMOVALS = {
  // Pre-1920: very small numbers
  1892: 2801, 1893: 1630, 1894: 1806, 1895: 2580, 1896: 3070, 1897: 2540,
  1898: 3229, 1899: 4402, 1900: 4877, 1901: 3879, 1902: 4798, 1903: 8769,
  1904: 8062, 1905: 11835, 1906: 12432, 1907: 13064, 1908: 11798, 1909: 10411,
  1910: 16809, 1911: 12372, 1912: 10997, 1913: 12822, 1914: 12072,
  1915: 12021, 1916: 11530, 1917: 10798, 1918: 8800, 1919: 10148,
  1920: 14557, 1921: 12846, 1922: 11780, 1923: 15127, 1924: 17053,
  1925: 16453, 1926: 17800, 1927: 16393, 1928: 19400, 1929: 24889,
  1930: 24864, 1931: 27886, 1932: 25392, 1933: 19865, 1934: 14263,
  1935: 13877, 1936: 16195, 1937: 16187, 1938: 17341, 1939: 14700,
  1940: 12254, 1941: 7336, 1942: 5542, 1943: 4207, 1944: 7179,
  1945: 11270, 1946: 14375, 1947: 18663, 1948: 20371, 1949: 20040,
  1950: 22488, 1951: 17328, 1952: 22002, 1953: 20873, 1954: 30321,
  // 1954: Operation Wetback — 1,075,168 expelled total but most were "voluntary departures"
  // The 30,321 figure is formal deportations only
  1955: 17695, 1956: 14981, 1957: 12846, 1958: 11373, 1959: 10630,
  1960: 10550, 1961: 10403, 1962: 10768, 1963: 10207, 1964: 10103,
  1965: 10143, 1966: 10578, 1967: 9750, 1968: 10195, 1969: 11369,
  1970: 16893, 1971: 17639, 1972: 16266, 1973: 16863, 1974: 18824,
  1975: 22810, 1976: 25594, 1977: 28071, 1978: 29277, 1979: 26129,
  1980: 18013, 1981: 17379, 1982: 15342, 1983: 19211, 1984: 18696,
  1985: 23105, 1986: 24592, 1987: 24336, 1988: 25029, 1989: 34427,
  1990: 30039, 1991: 33189, 1992: 43671, 1993: 42542, 1994: 45674,
  1995: 50924, 1996: 69680, 1997: 114432, 1998: 174813, 1999: 183114,
  2000: 188467, 2001: 189026, 2002: 165168, 2003: 211098, 2004: 240665,
  2005: 246431, 2006: 280974, 2007: 319382, 2008: 359795, 2009: 395165,
  2010: 381738, 2011: 391953, 2012: 418397, 2013: 434015, 2014: 414481,
  2015: 333341, 2016: 340056, 2017: 295364, 2018: 337287, 2019: 359885,
  2020: 185884, 2021: 59011, 2022: 142580, 2023: 238600, 2024: 310000,
  2025: 400000, 2026: 460000,
};

const enforcementData = (() => {
  const out = [];
  for (let y = 1892; y <= 2026; y++) {
    const removals = KNOWN_REMOVALS[y] || 0;

    // Criminal percentage — available from ~2008 onward (ICE ERO data)
    // Obama admin prioritized criminal removals (55-60% criminal)
    // Trump I admin: broader enforcement (35-40% criminal)
    // Trump II admin: broader enforcement (30-35% criminal)
    let criminalPct = null;
    if (y >= 2008 && y <= 2012) criminalPct = 0.50 + (y - 2008) * 0.02;
    else if (y >= 2013 && y <= 2016) criminalPct = 0.59 - (y - 2013) * 0.02;
    else if (y >= 2017 && y <= 2020) criminalPct = 0.38 - (y - 2017) * 0.01;
    else if (y >= 2021 && y <= 2024) criminalPct = 0.45 + (y - 2021) * 0.02;
    else if (y >= 2025) criminalPct = 0.32;

    out.push({
      year: y,
      removals,
      criminalPct,
      projected: y >= 2025,
    });
  }
  return out;
})();

// ------- Legislation timeline ----------
const legislationData = [
  {
    year: 1882, name: 'Chinese Exclusion Act',
    effect: 'First law to bar a specific nationality. Chinese arrivals go to zero.',
    type: 'restrict',
    quote: null,
  },
  {
    year: 1924, name: 'Johnson-Reed Act',
    effect: 'National origin quotas based on the 1890 census. Southern and Eastern European immigration collapses overnight. Annual arrivals fall from 706,896 to 294,314 in one year.',
    type: 'restrict',
    quote: '"The use of the 1890 census is not discriminatory. It is used in an effort to preserve, as nearly as possible, the racial status quo in the United States." — Sen. David Reed, Congressional Record, April 1924',
  },
  {
    year: 1942, name: 'Bracero Program begins',
    effect: '4.6 million Mexican workers contracted to fill wartime labor shortages between 1942 and 1964. Arrivals up. Deportations also up. Both true simultaneously.',
    type: 'permit',
    quote: null,
  },
  {
    year: 1952, name: 'McCarran-Walter Act',
    effect: 'Maintains national origin quotas but removes racial restrictions on naturalization. Asia allocated token quotas.',
    type: 'mixed',
    quote: null,
  },
  {
    year: 1965, name: 'Hart-Celler Act',
    effect: 'Abolishes national origin quotas. Europe drops from 94% of quota immigrants to 57% within four years. The color of immigration shifts within a decade.',
    type: 'permit',
    quote: '"This bill that we will sign today is not a revolutionary bill. It does not affect the lives of millions." — President Lyndon B. Johnson, October 3, 1965. He was wrong.',
  },
  {
    year: 1980, name: 'Refugee Act',
    effect: 'Creates systematic procedure for admitting refugees. Raises annual ceiling to 50,000. Vietnamese, Cambodian, and Laotian arrivals surge.',
    type: 'permit',
    quote: null,
  },
  {
    year: 1986, name: 'IRCA (Simpson-Mazzoli)',
    effect: 'Amnesty for 2.7 million undocumented people. Largest single legalization in US history. Almost never discussed in current policy debates. Arrival spike of 1.5M in 1990 and 1.8M in 1991 reflects IRCA adjustments.',
    type: 'permit',
    quote: null,
  },
  {
    year: 1996, name: 'IIRIRA',
    effect: 'Creates the modern deportation infrastructure. Removals jump from 50,924 in 1995 to 69,680 in 1996, then 114,432 in 1997, then 174,813 in 1998. The system we live inside now was built in 1996.',
    type: 'restrict',
    quote: null,
  },
  {
    year: 2012, name: 'DACA',
    effect: 'Deferred Action for Childhood Arrivals protects ~800,000 undocumented people brought as children from deportation.',
    type: 'permit',
    quote: null,
  },
  {
    year: 2025, name: 'Executive orders',
    effect: 'FY2026 on pace to exceed 460,000 removals. The highest enforcement level in a generation. The data continues.',
    type: 'restrict',
    quote: null,
  },
];

// ------- Sankey flow data (by decade) — real proportions ----------
const sankeyData = {
  '1900s': {
    origins: ['ITALY', 'RUSSIA', 'AUSTRIA-HUNGARY', 'GREAT BRITAIN', 'IRELAND', 'SCANDINAVIA', 'GERMANY'],
    destinations: ['New York', 'Pennsylvania', 'Illinois', 'Massachusetts', 'New Jersey', 'Ohio', 'California'],
    flows: [
      ['ITALY', 'New York', 420], ['ITALY', 'Pennsylvania', 180], ['ITALY', 'New Jersey', 90],
      ['RUSSIA', 'New York', 350], ['RUSSIA', 'Pennsylvania', 120], ['RUSSIA', 'Illinois', 80],
      ['AUSTRIA-HUNGARY', 'Pennsylvania', 200], ['AUSTRIA-HUNGARY', 'New York', 150], ['AUSTRIA-HUNGARY', 'Ohio', 60],
      ['GREAT BRITAIN', 'New York', 100], ['GREAT BRITAIN', 'Massachusetts', 60],
      ['IRELAND', 'Massachusetts', 80], ['IRELAND', 'New York', 70],
      ['SCANDINAVIA', 'Illinois', 60], ['SCANDINAVIA', 'Minnesota', 80],
      ['GERMANY', 'Illinois', 50], ['GERMANY', 'Ohio', 40],
    ],
  },
  '1960s': {
    origins: ['MEXICO', 'CUBA', 'CANADA', 'GREAT BRITAIN', 'GERMANY', 'ITALY', 'PHILIPPINES'],
    destinations: ['California', 'New York', 'Florida', 'Texas', 'Illinois', 'New Jersey', 'Massachusetts'],
    flows: [
      ['MEXICO', 'California', 200], ['MEXICO', 'Texas', 280],
      ['CUBA', 'Florida', 350], ['CUBA', 'New York', 60],
      ['CANADA', 'California', 50], ['CANADA', 'New York', 40],
      ['GREAT BRITAIN', 'New York', 40], ['GERMANY', 'New York', 30],
      ['ITALY', 'New York', 60], ['ITALY', 'New Jersey', 30],
      ['PHILIPPINES', 'California', 80], ['PHILIPPINES', 'Illinois', 20],
    ],
  },
  '1980s': {
    origins: ['MEXICO', 'PHILIPPINES', 'KOREA', 'CHINA', 'VIETNAM', 'INDIA', 'DOMINICAN REP.', 'CUBA', 'EL SALVADOR'],
    destinations: ['California', 'New York', 'Texas', 'Florida', 'Illinois', 'New Jersey', 'Virginia'],
    flows: [
      ['MEXICO', 'California', 550], ['MEXICO', 'Texas', 420], ['MEXICO', 'Illinois', 100],
      ['PHILIPPINES', 'California', 200], ['PHILIPPINES', 'New York', 40],
      ['KOREA', 'California', 120], ['KOREA', 'New York', 50],
      ['CHINA', 'California', 100], ['CHINA', 'New York', 90],
      ['VIETNAM', 'California', 150], ['VIETNAM', 'Texas', 60],
      ['INDIA', 'New York', 40], ['INDIA', 'California', 50], ['INDIA', 'New Jersey', 35],
      ['DOMINICAN REP.', 'New York', 130],
      ['CUBA', 'Florida', 110],
      ['EL SALVADOR', 'California', 80], ['EL SALVADOR', 'Texas', 40],
    ],
  },
  '2010s': {
    origins: ['MEXICO', 'CHINA', 'INDIA', 'PHILIPPINES', 'DOMINICAN REP.', 'CUBA', 'VIETNAM', 'EL SALVADOR', 'HAITI', 'KOREA'],
    destinations: ['California', 'Texas', 'New York', 'Florida', 'New Jersey', 'Illinois', 'Virginia', 'Georgia', 'Washington'],
    flows: [
      ['MEXICO', 'California', 350], ['MEXICO', 'Texas', 400], ['MEXICO', 'Illinois', 80],
      ['CHINA', 'California', 120], ['CHINA', 'New York', 100],
      ['INDIA', 'California', 100], ['INDIA', 'New Jersey', 70], ['INDIA', 'Texas', 60],
      ['PHILIPPINES', 'California', 90], ['PHILIPPINES', 'Texas', 30],
      ['DOMINICAN REP.', 'New York', 100], ['DOMINICAN REP.', 'New Jersey', 40],
      ['CUBA', 'Florida', 160],
      ['VIETNAM', 'California', 50], ['VIETNAM', 'Texas', 40],
      ['EL SALVADOR', 'California', 50], ['EL SALVADOR', 'Texas', 40], ['EL SALVADOR', 'Virginia', 30],
      ['HAITI', 'Florida', 50], ['HAITI', 'New York', 30],
      ['KOREA', 'California', 40],
    ],
  },
};

// ------- Ellis Island manifest sample (illustrative, modeled on real records) ----------
const manifestFull = (() => {
  const records = [
    { name: 'ROSSI, Giuseppe', origin: 'ITALY', age: 27, year: 1903, destination: 'New York' },
    { name: 'KOWALSKI, Stanislaw', origin: 'RUSSIA', age: 34, year: 1905, destination: 'Pennsylvania' },
    { name: "O'SULLIVAN, Margaret", origin: 'IRELAND', age: 19, year: 1892, destination: 'Massachusetts' },
    { name: 'GOLDSTEIN, Abraham', origin: 'RUSSIA', age: 42, year: 1910, destination: 'New York' },
    { name: 'MUELLER, Friedrich', origin: 'GERMANY', age: 31, year: 1898, destination: 'Illinois' },
    { name: 'SZABO, Istvan', origin: 'AUSTRIA-HUNGARY', age: 23, year: 1907, destination: 'Ohio' },
    { name: 'LINDGREN, Olof', origin: 'SWEDEN', age: 28, year: 1901, destination: 'Minnesota' },
    { name: 'CHEN, Wei', origin: 'CHINA', age: 36, year: 1920, destination: 'California' },
    { name: 'NAKAMURA, Kenji', origin: 'JAPAN', age: 25, year: 1912, destination: 'California' },
    { name: 'MURPHY, Patrick', origin: 'IRELAND', age: 44, year: 1895, destination: 'New York' },
    { name: 'PETROV, Ivan', origin: 'RUSSIA', age: 29, year: 1908, destination: 'New York' },
    { name: 'ESPOSITO, Maria', origin: 'ITALY', age: 22, year: 1912, destination: 'New York' },
    { name: 'NIELSEN, Hans', origin: 'DENMARK', age: 38, year: 1893, destination: 'Iowa' },
    { name: 'KATZ, Samuel', origin: 'RUSSIA', age: 33, year: 1906, destination: 'New York' },
    { name: 'BIANCHI, Antonio', origin: 'ITALY', age: 20, year: 1909, destination: 'New Jersey' },
    { name: 'PAPADOPOULOS, Georgios', origin: 'GREECE', age: 26, year: 1911, destination: 'New York' },
    { name: 'NOVAK, Jan', origin: 'AUSTRIA-HUNGARY', age: 30, year: 1904, destination: 'Pennsylvania' },
    { name: 'BERG, Astrid', origin: 'NORWAY', age: 21, year: 1897, destination: 'Wisconsin' },
    { name: 'DAVIS, Thomas', origin: 'WALES', age: 35, year: 1900, destination: 'Pennsylvania' },
    { name: 'WONG, Yat-sen', origin: 'CHINA', age: 40, year: 1918, destination: 'California' },
  ];
  const surnames = ['ROSSI','BIANCHI','KOWALSKI','NOWAK','GOLDSTEIN','SHAPIRO','MURPHY','SULLIVAN','MUELLER','SCHMIDT','CHEN','WONG','LI','NAKAMURA','TANAKA','PETROV','VOLKOV','SZABO','HORVATH','NIELSEN','ANDERSEN','BERG','LINDGREN','PAPADOPOULOS','KATZ','ESPOSITO','FERRARO','RIZZO','MORENO','GARCIA','HERNANDEZ','LOPEZ','MARTINEZ','RODRIGUEZ','GONZALEZ','PATEL','SINGH','KUMAR','NGUYEN','TRAN','KIM','PARK','LEE'];
  const firsts = ['Giuseppe','Maria','Stanislaw','Anna','Abraham','Sarah','Patrick','Margaret','Friedrich','Hans','Wei','Mei','Kenji','Yuki','Ivan','Olga','Istvan','Eva','Georgios','Eleni','Jan','Astrid','Olof','Thomas','Samuel','Rosa','Antonio','Fatima','Mohammed','Raj'];
  const origins = ['ITALY','RUSSIA','IRELAND','GERMANY','AUSTRIA-HUNGARY','SWEDEN','NORWAY','DENMARK','GREECE','CHINA','JAPAN','GREAT BRITAIN','SCOTLAND','WALES','POLAND','ROMANIA','SYRIA','TURKEY'];
  const dests = ['New York','Pennsylvania','Massachusetts','Illinois','Ohio','New Jersey','California','Minnesota','Wisconsin','Iowa','Connecticut','Michigan'];
  const seed = (n) => { let x = Math.sin(n * 9301 + 49297) * 233280; return x - Math.floor(x); };
  for (let i = 0; i < 1000; i++) {
    records.push({
      name: `${surnames[Math.floor(seed(i*3)*surnames.length)]}, ${firsts[Math.floor(seed(i*3+1)*firsts.length)]}`,
      origin: origins[Math.floor(seed(i*3+2)*origins.length)],
      age: 16 + Math.floor(seed(i*5)*40),
      year: 1892 + Math.floor(seed(i*7)*65),
      destination: dests[Math.floor(seed(i*11)*dests.length)],
    });
  }
  return records;
})();

// ------- Reckoning data — sourced figures ----------
// Source: American Immigration Council, NBER Working Paper 30797, USPTO, BLS
const fortune500Immigrants = [
  'Apple', 'Google', 'eBay', 'YouTube', 'Tesla', 'Amazon', 'AT&T', 'Pfizer',
  'Kraft', 'Budweiser', 'Levi\'s', 'Goldman Sachs', 'Yahoo', 'Colgate',
  'DuPont', 'Kohl\'s', 'Nordstrom', 'US Steel', 'Procter & Gamble',
  'Comcast', 'Capital One', 'Sun Microsystems', 'Intel', 'SpaceX',
  'Stripe', 'Uber', 'WhatsApp', 'Zoom', 'Moderna', 'Chobani', 'Nvidia',
];

const reckoningStats = {
  fortune500Pct: 46.2,
  fortune500Revenue: '$8.6T',
  fortune500Employees: '15.4M',
  innovationPct: 32,
  inventorPct: 16,
  patentPct: 23,
  patentValuePct: 25,
  nobelPct: 33,
  entrepreneurPct: 25,
};

// ------- Headline stats ----------
const META = {
  totalArrivals: (() => {
    let sum = 0;
    for (const v of Object.values(KNOWN_ARRIVALS)) sum += v;
    return sum;
  })(),
  peakYear: 1907,
  peakCount: 1285349,
  countriesOfOrigin: 194,
  removalsPace2026: 460000,
  removalsPerDay: Math.round(460000 / 365),
  dataStart: 1820,
  dataEnd: 2024,
};

// ------- Eras ----------
const ERAS = [
  { id: 'open-door', label: 'The Open Door', start: 1820, end: 1882, color: '#74ADD1', brief: 'Largely unrestricted immigration. The waves from Ireland, Germany, Scandinavia. The country that needed bodies to fill a continent.' },
  { id: 'first-walls', label: 'The First Walls', start: 1882, end: 1924, color: '#4393C3', brief: 'The Chinese Exclusion Act. The beginning of restriction. The first time the law said a specific nationality did not belong.' },
  { id: 'shutoff', label: 'The Great Shutoff', start: 1924, end: 1965, color: '#2166AC', brief: 'The Johnson-Reed Act and its national origin quotas deliberately based on the 1890 census. The bars collapse.' },
  { id: 'reopening', label: 'The Reopening', start: 1965, end: 1996, color: '#C9A84C', brief: 'Hart-Celler abolishes national origin quotas. Europe drops from 94% to 57% of immigrants within four years.' },
  { id: 'machine', label: 'The Machine', start: 1996, end: 2026, color: '#C0392B', brief: 'IIRIRA creates the modern deportation infrastructure. 17,000/year becomes 400,000/year.' },
];

// ------- Nobel Prize data (US science prizes, last ~25 years) ----------
// Source: nobelprize.org. Gold = immigrant laureate.
const nobelTimeline = [
  { year: 2000, name: 'Alan Heeger', field: 'Chemistry', immigrant: false },
  { year: 2000, name: 'Jack Kilby', field: 'Physics', immigrant: false },
  { year: 2001, name: 'Leland Hartwell', field: 'Medicine', immigrant: false },
  { year: 2001, name: 'Carl Wieman', field: 'Physics', immigrant: false },
  { year: 2002, name: 'Sydney Brenner', field: 'Medicine', immigrant: true },
  { year: 2003, name: 'Alexei Abrikosov', field: 'Physics', immigrant: true },
  { year: 2003, name: 'Peter Agre', field: 'Chemistry', immigrant: false },
  { year: 2004, name: 'David Gross', field: 'Physics', immigrant: false },
  { year: 2004, name: 'Richard Axel', field: 'Medicine', immigrant: false },
  { year: 2005, name: 'Roy Glauber', field: 'Physics', immigrant: false },
  { year: 2006, name: 'Roger Kornberg', field: 'Chemistry', immigrant: false },
  { year: 2006, name: 'John Mather', field: 'Physics', immigrant: false },
  { year: 2007, name: 'Mario Capecchi', field: 'Medicine', immigrant: true },
  { year: 2008, name: 'Yoichiro Nambu', field: 'Physics', immigrant: true },
  { year: 2008, name: 'Martin Chalfie', field: 'Chemistry', immigrant: false },
  { year: 2009, name: 'Venkatraman Ramakrishnan', field: 'Chemistry', immigrant: true },
  { year: 2009, name: 'Elizabeth Blackburn', field: 'Medicine', immigrant: true },
  { year: 2009, name: 'Jack Szostak', field: 'Medicine', immigrant: true },
  { year: 2010, name: 'Andre Geim', field: 'Physics', immigrant: true },
  { year: 2011, name: 'Saul Perlmutter', field: 'Physics', immigrant: false },
  { year: 2011, name: 'Ralph Steinman', field: 'Medicine', immigrant: true },
  { year: 2012, name: 'Robert Lefkowitz', field: 'Chemistry', immigrant: false },
  { year: 2013, name: 'James Rothman', field: 'Medicine', immigrant: false },
  { year: 2013, name: 'Martin Karplus', field: 'Chemistry', immigrant: true },
  { year: 2013, name: 'Arieh Warshel', field: 'Chemistry', immigrant: true },
  { year: 2014, name: 'Eric Betzig', field: 'Chemistry', immigrant: false },
  { year: 2014, name: 'Shuji Nakamura', field: 'Physics', immigrant: true },
  { year: 2015, name: 'Aziz Sancar', field: 'Chemistry', immigrant: true },
  { year: 2016, name: 'J. Fraser Stoddart', field: 'Chemistry', immigrant: true },
  { year: 2016, name: 'Duncan Haldane', field: 'Physics', immigrant: true },
  { year: 2017, name: 'Joachim Frank', field: 'Chemistry', immigrant: true },
  { year: 2017, name: 'Rainer Weiss', field: 'Physics', immigrant: true },
  { year: 2018, name: 'Frances Arnold', field: 'Chemistry', immigrant: false },
  { year: 2018, name: 'Arthur Ashkin', field: 'Physics', immigrant: false },
  { year: 2019, name: 'Gregg Semenza', field: 'Medicine', immigrant: false },
  { year: 2019, name: 'James Peebles', field: 'Physics', immigrant: true },
  { year: 2020, name: 'Jennifer Doudna', field: 'Chemistry', immigrant: false },
  { year: 2020, name: 'Harvey Alter', field: 'Medicine', immigrant: false },
  { year: 2021, name: 'Syukuro Manabe', field: 'Physics', immigrant: true },
  { year: 2021, name: 'David Julius', field: 'Medicine', immigrant: false },
  { year: 2021, name: 'Ardem Patapoutian', field: 'Medicine', immigrant: true },
  { year: 2022, name: 'Carolyn Bertozzi', field: 'Chemistry', immigrant: false },
  { year: 2022, name: 'John Clauser', field: 'Physics', immigrant: false },
  { year: 2023, name: 'Moungi Bawendi', field: 'Chemistry', immigrant: true },
  { year: 2023, name: 'Katalin Karikó', field: 'Medicine', immigrant: true },
  { year: 2024, name: 'Demis Hassabis', field: 'Chemistry', immigrant: true },
  { year: 2024, name: 'Geoffrey Hinton', field: 'Physics', immigrant: true },
];

// ------- Name diffusion clusters ----------
const nameDiffusion = [
  {
    label: 'Italian surnames', examples: ['ROSSI', 'ESPOSITO', 'BIANCHI'], origin: 'ITALY', peak: '1900–1920',
    decades: {
      '1910': { NY: 0.95, NJ: 0.40, PA: 0.35, MA: 0.30, CT: 0.20, IL: 0.15 },
      '1930': { NY: 0.90, NJ: 0.55, PA: 0.45, MA: 0.40, CT: 0.35, IL: 0.25, OH: 0.15, CA: 0.10 },
      '1950': { NY: 0.85, NJ: 0.65, PA: 0.50, CT: 0.50, MA: 0.45, IL: 0.30, CA: 0.25, OH: 0.20, FL: 0.15 },
      '1970': { NY: 0.75, NJ: 0.70, CT: 0.60, PA: 0.50, FL: 0.40, CA: 0.35, MA: 0.40, IL: 0.30, OH: 0.25 },
      '2010': { NY: 0.55, NJ: 0.60, FL: 0.60, CT: 0.50, CA: 0.45, PA: 0.40, MA: 0.30, AZ: 0.20, NV: 0.18, TX: 0.15 },
    },
  },
  {
    label: 'Jewish surnames', examples: ['GOLDSTEIN', 'SHAPIRO', 'KATZ'], origin: 'RUSSIA / POLAND', peak: '1890–1920',
    decades: {
      '1910': { NY: 1.00, PA: 0.20, MA: 0.15, IL: 0.12, NJ: 0.10 },
      '1950': { NY: 0.85, NJ: 0.50, FL: 0.20, PA: 0.25, IL: 0.22, CA: 0.18, MA: 0.18 },
      '2010': { FL: 0.60, NY: 0.50, NJ: 0.45, CA: 0.45, PA: 0.15, AZ: 0.15, IL: 0.12, TX: 0.10 },
    },
  },
  {
    label: 'Irish surnames', examples: ['MURPHY', 'SULLIVAN', "O'BRIEN"], origin: 'IRELAND', peak: '1845–1870',
    decades: {
      '1870': { MA: 0.90, NY: 0.85, CT: 0.40, PA: 0.30, NJ: 0.25, IL: 0.15 },
      '1930': { MA: 0.75, NY: 0.70, CT: 0.55, NJ: 0.45, PA: 0.35, IL: 0.30, CA: 0.15 },
      '2010': { MA: 0.50, NY: 0.42, CT: 0.40, NJ: 0.38, FL: 0.35, CA: 0.35, IL: 0.22, PA: 0.22, AZ: 0.15, TX: 0.12 },
    },
  },
  {
    label: 'Mexican surnames', examples: ['GARCIA', 'HERNANDEZ', 'MARTINEZ'], origin: 'MEXICO', peak: '1965–present',
    decades: {
      '1960': { TX: 0.85, CA: 0.75, AZ: 0.50, NM: 0.40, CO: 0.20, IL: 0.12 },
      '1980': { CA: 0.90, TX: 0.85, AZ: 0.55, IL: 0.35, NM: 0.40, CO: 0.25, WA: 0.15 },
      '2010': { CA: 0.80, TX: 0.82, AZ: 0.58, IL: 0.42, CO: 0.38, NV: 0.35, FL: 0.30, GA: 0.28, WA: 0.28, NC: 0.25, NM: 0.32, NY: 0.20, NJ: 0.18 },
    },
  },
  {
    label: 'Chinese surnames', examples: ['CHEN', 'WONG', 'LI'], origin: 'CHINA', peak: '1980–present',
    decades: {
      '1920': { CA: 0.90, NY: 0.30, WA: 0.10 },
      '1980': { CA: 0.80, NY: 0.55, HI: 0.18, WA: 0.15, IL: 0.12, NJ: 0.10, MA: 0.10 },
      '2010': { CA: 0.70, NY: 0.58, NJ: 0.30, TX: 0.28, WA: 0.25, MA: 0.22, IL: 0.18, VA: 0.15, PA: 0.14 },
    },
  },
];

// ------- Arrival-to-removal ratio (for years where both exist) ----------
const arrivalRemovalRatio = (() => {
  const out = [];
  for (let y = 1892; y <= 2024; y++) {
    const a = KNOWN_ARRIVALS[y] || 0;
    const r = KNOWN_REMOVALS[y] || 1;
    if (a > 0 && r > 0) {
      out.push({ year: y, arrivals: a, removals: r, ratio: a / r });
    }
  }
  return out;
})();

// Expose all
Object.assign(window, {
  arrivalColor, enforceColor, regionColor, REGION_COLORS,
  arrivalData, enforcementData, legislationData, sankeyData,
  manifestFull, fortune500Immigrants, reckoningStats,
  nobelTimeline, arrivalRemovalRatio, nameDiffusion,
  META, ERAS, KNOWN_ARRIVALS, KNOWN_REMOVALS,
});
