/* global window */
const CONF = {
  pilot: { label: "Pilot", color: "#2d7a4d" },
  bench: { label: "Bench", color: "#1e6091" },
  lab: { label: "Lab", color: "#a87420" },
  disclosed: { label: "Disclosed", color: "#656a70" },
  proxy: { label: "Proxy", color: "#7a5aa6" },
  testing: { label: "Testing", color: "#c76a2a" },
};

const ENGINES = {
  hpu: { enabled: true, core: true, name: "HPU — Plastics Upcycling", short: "HPU", unit: "tons", tam: 400, tamUnit: "M tons/yr", tamNote: "OECD 2022: 353M t in 2019. HCT targets 70-75% (~280M+ t) that mech. recycling and pyrolysis reject.", share: 0.2, maxShare: 20, salePrice: 1600, royaltyPct: 8, margin: 75, yieldPct: 91, color: "#5ba3d9", confidence: "pilot", tippingFee: 0, overlap: null, plantSize: 25000 },
  hbu_extra: { enabled: true, core: true, name: "Extra-Heavy Bitumen", short: "BIT", unit: "bbl", tam: 1600, tamUnit: "M bbl/yr", tamNote: "API<15. Canada ~1.3B + Venezuela ~300M. Uplift $13-23/bbl.", share: 0.5, maxShare: 30, royaltyPerUnit: 2.25, margin: 75, color: "#d9a95b", confidence: "bench", tippingFee: 0, overlap: "refinery", plantSize: null },
  hbu_sour: { enabled: true, core: true, name: "Heavy / Sour Crude", short: "SOUR", unit: "bbl", tam: 4000, tamUnit: "M bbl/yr", tamNote: "API 15-25, >1% S. Saudi, Mexico, Iraq, Iran, Russia. Uplift $6-11/bbl.", share: 0.3, maxShare: 25, royaltyPerUnit: 1.25, margin: 68, color: "#c89450", confidence: "bench", tippingFee: 0, overlap: "refinery", plantSize: null },
  hru: { enabled: true, core: true, name: "HRU — Renewables Upgrading", short: "HRU", unit: "tons", tam: 80, tamUnit: "M tons/yr", tamNote: "UCO ~18M, fats ~18M, CDO ~7M, other ~37M. SAF mandates driving demand.", share: 0.3, maxShare: 10, salePrice: 1800, royaltyPct: 8, margin: 75, yieldPct: 90, color: "#6fc28a", confidence: "lab", tippingFee: 0, overlap: "tcd", plantSize: 25000 },
  paraffinic: { enabled: true, core: true, name: "Paraffinic Crude", short: "PCO", unit: "bbl", tam: 7900, tamUnit: "M bbl/yr", tamNote: "16.4-27.1M bbl/day (Sousa et al. 2023). Uplift $11-23/bbl.", share: 0.3, maxShare: 25, royaltyPerUnit: 1.75, margin: 70, color: "#a389d4", confidence: "bench", tippingFee: 0, overlap: null, plantSize: null },
  tires: { enabled: false, core: false, name: "Tire Rubber", short: "TIRE", unit: "tons", tam: 20, tamUnit: "M tons/yr", tamNote: "~1B EOL tires. 45-55% rubber + carbon black/steel.", share: 1.0, maxShare: 40, salePrice: 1100, royaltyPct: 8, margin: 72, yieldPct: 50, color: "#e07575", confidence: "proxy", tippingFee: 40, overlap: null, plantSize: 20000 },
  turf: { enabled: false, core: false, name: "Synthetic Turf", short: "TURF", unit: "tons", tam: 1.5, tamUnit: "M tons/yr", tamNote: "6-10K fields/yr. PFAS crisis. Zero competing tech.", share: 5.0, maxShare: 70, salePrice: 1500, royaltyPct: 10, margin: 75, yieldPct: 50, color: "#4fc3d0", confidence: "lab", tippingFee: 100, overlap: null, plantSize: 15000 },
  xlpe: { enabled: false, core: false, name: "Crosslinked Polymers (PEX/XLPE)", short: "XLPE", unit: "tons", tam: 5, tamUnit: "M tons/yr", tamNote: "PEX pipe + XLPE cable. 84% yield from Aduro bench. Zero competing tech.", share: 1.0, maxShare: 60, salePrice: 1500, royaltyPct: 10, margin: 75, yieldPct: 84, color: "#d98fb4", confidence: "bench", tippingFee: 0, overlap: null, plantSize: 15000 },
  refinery: { enabled: false, core: false, name: "Refinery Bottom-of-Barrel", short: "REF", unit: "bbl", tam: 3000, tamUnit: "M bbl/yr", tamNote: "280 global refineries. 50-60% lack cokers.", share: 0.3, maxShare: 25, royaltyPerUnit: 2.00, margin: 72, color: "#9ec85a", confidence: "disclosed", tippingFee: 0, overlap: "hbu_extra", plantSize: null },
  tcd: { enabled: false, core: false, name: "TCD — Bio-oils / CDO", short: "TCD", unit: "tons", tam: 50, tamUnit: "M tons/yr", tamNote: "CDO ~7M + waste bio-oils ~43M. Separately patented.", share: 0.5, maxShare: 15, salePrice: 1600, royaltyPct: 8, margin: 72, yieldPct: 88, color: "#e3b558", confidence: "disclosed", tippingFee: 0, overlap: "hru", plantSize: 25000 },
  pu: { enabled: false, core: false, name: "Polyurethane", short: "PU", unit: "tons", tam: 26, tamUnit: "M tons/yr", tamNote: "Mattresses, foam, auto seats. Testing with TotalEnergies.", share: 1.0, maxShare: 50, salePrice: 1400, royaltyPct: 10, margin: 75, yieldPct: 80, color: "#b89edb", confidence: "testing", tippingFee: 30, overlap: null, plantSize: 20000 },
  agri: { enabled: false, core: false, name: "Agricultural Plastic Waste", short: "AGRI", unit: "tons", tam: 25, tamUnit: "M tons/yr", tamNote: "12M/yr + ~13M/yr stockpile drawdown. Cleanfarms + ECOCE.", share: 2.0, maxShare: 50, salePrice: 1400, royaltyPct: 9, margin: 78, yieldPct: 85, color: "#83c77f", confidence: "disclosed", tippingFee: 55, overlap: null, plantSize: 20000 },
  mfg: { enabled: false, core: false, name: "Manufacturer In-House Waste", short: "MFG", unit: "tons", tam: 35, tamUnit: "M tons/yr", tamNote: "8-10% of 400M production. Modular factory units.", share: 0.5, maxShare: 20, salePrice: 1600, royaltyPct: 12, margin: 80, yieldPct: 93, color: "#6fb8e8", confidence: "disclosed", tippingFee: 0, overlap: null, plantSize: 10000 },
};

const PRESETS = {
  beachhead: { label: "Beachhead", pe: 20, shares: 39, cfg: { hpu: { share: 0.2 }, hbu_extra: { share: 0.5 }, hbu_sour: { share: 0.2 }, hru: { share: 0.3 }, paraffinic: { share: 0.3 }, agri: { share: 1.0 } }, enable: ["hpu","hbu_extra","hbu_sour","hru","paraffinic","agri"] },
  standard: { label: "Standard", pe: 25, shares: 45, cfg: { hpu: { share: 2.0 }, hbu_extra: { share: 5.0, royaltyPerUnit: 2.50, margin: 78 }, hbu_sour: { share: 2.0, royaltyPerUnit: 1.50, margin: 72 }, hru: { share: 1.0, salePrice: 2000, royaltyPct: 10 }, paraffinic: { share: 2.0, royaltyPerUnit: 2.0, margin: 75 }, tires: { share: 5.0 }, xlpe: { share: 10.0 }, turf: { share: 15.0 }, refinery: { share: 1.0 }, pu: { share: 3.0 }, agri: { share: 8.0 }, mfg: { share: 1.0 } }, enable: ["hpu","hbu_extra","hbu_sour","hru","paraffinic","tires","xlpe","turf","refinery","pu","agri","mfg"] },
  blueSky: { label: "Blue Sky", pe: 30, shares: 50, cfg: { hpu: { share: 5.0, salePrice: 2000, royaltyPct: 15, margin: 80, yieldPct: 95 }, hbu_extra: { share: 15.0, royaltyPerUnit: 3.00, margin: 82 }, hbu_sour: { share: 8.0, royaltyPerUnit: 2.00, margin: 78 }, hru: { share: 3.0, salePrice: 2500, royaltyPct: 15, margin: 80, yieldPct: 92 }, paraffinic: { share: 8.0, royaltyPerUnit: 2.25, margin: 80 }, tires: { share: 15.0, salePrice: 1600, royaltyPct: 12, margin: 78 }, turf: { share: 40.0, salePrice: 1800, royaltyPct: 15, margin: 80 }, xlpe: { share: 30.0, salePrice: 1800, royaltyPct: 15, margin: 80 }, refinery: { share: 5.0, royaltyPerUnit: 2.5, margin: 78 }, tcd: { share: 5.0, salePrice: 1800, royaltyPct: 12, margin: 78 }, pu: { share: 15.0, salePrice: 1600, royaltyPct: 12, margin: 78 }, agri: { share: 25.0, salePrice: 1600, royaltyPct: 12, margin: 80 }, mfg: { share: 5.0, royaltyPct: 15, margin: 82 } }, enable: ["hpu","hbu_extra","hbu_sour","hru","paraffinic","tires","turf","xlpe","refinery","tcd","pu","agri","mfg"] },
};

const PHASES = [
  { name: "Validation", years: "2024–2027", rev: "~$0", plants: "Pilot + FOAK", sm: { hpu: 0.05, hbu_extra: 0.1, hbu_sour: 0, hru: 0.05, paraffinic: 0.05, tires: 0, turf: 0, xlpe: 0, refinery: 0, tcd: 0, pu: 0, agri: 0.1, mfg: 0 }, pe: 15, sh: 39, drivers: "Milestone de-risking. No earnings. Market re-rates on removal of failure risk.", catalysts: "Pilot data, LCA, FOAK groundbreaking, first license. TotalEnergies outcome is key.", precedent: "Catalytic cracking 1938–42. Tech proven at pilot; first industrial unit under construction.", risk: "FOAK scale-up failure. Timeline shifts 2-3 years if continuous ops reveal problems.", current: true },
  { name: "Early Adoption", years: "2027–2030", rev: "$50–150M", plants: "5–10 licensed", sm: { hpu: 0.2, hbu_extra: 0.5, hbu_sour: 0.2, hru: 0.3, paraffinic: 0.3, tires: 0.5, turf: 2, xlpe: 1, refinery: 0.2, tcd: 0.2, pu: 0.5, agri: 1, mfg: 0.2 }, pe: 20, sh: 42, drivers: "First plants operating. Reference customers accelerate deals.", catalysts: "FOAK matching pilot data. 2nd/3rd licenses. HBU Alberta. EU PPWR mandates.", precedent: "Catalytic cracking 1942–47. Qualcomm CDMA 1995–2000.", risk: "First licensee data critical. Inconsistent results stall pipeline." },
  { name: "Acceleration", years: "2030–2035", rev: "$200–800M", plants: "10–50+", sm: { hpu: 2, hbu_extra: 5, hbu_sour: 2, hru: 1, paraffinic: 2, tires: 5, turf: 15, xlpe: 10, refinery: 1, tcd: 1, pu: 3, agri: 8, mfg: 1 }, pe: 25, sh: 48, drivers: "EU recycled content mandates binding. Penalties for non-compliance.", catalysts: "Regulatory deadlines. 3-5 new licenses/yr. Second-gen plant designs.", precedent: "Catalytic cracking 1947–55. ARM 2007–12: smartphone forcing function.", risk: "Funded pyrolysis breakthroughs. Oil collapse <$40/bbl. Mandate reversal." },
  { name: "Platform Standard", years: "2035+", rev: "$500M–3B+", plants: "50–200+", sm: { hpu: 5, hbu_extra: 15, hbu_sour: 8, hru: 3, paraffinic: 8, tires: 15, turf: 40, xlpe: 30, refinery: 5, tcd: 5, pu: 15, agri: 25, mfg: 5 }, pe: 30, sh: 55, drivers: "HCT as industry standard. New applications expand TAM continually.", catalysts: "Global regulatory harmonization. Asia adoption. SAF ramp. Metals recovery.", precedent: "Catalytic cracking today. ARM today: 95%+ mobile devices.", risk: "Obsolescence from new chemistry. Low probability at this stage." },
];

const MILESTONES = [
  { y: "2026 Q1", e: "NGP Pilot Commissioned", s: "done", p: [18, 22], note: "Proves HCT works in continuous flow at pre-commercial scale. Transitions from R&D to technology demonstration. Enables third-party testing." },
  { y: "2026 Q1", e: "Chemelot FOAK Site + EPC MOU + Offtake LOI", s: "done", p: null, note: "Three milestones proving commercialization readiness: Europe's premier chemical park, a global EPC engineering firm committed to build plants, and a buyer committed to purchase output before construction." },
  { y: "2026 Q1", e: "WTR Coverage + CRE Membership", s: "done", p: null, note: "Water Tower Research bridges the institutional visibility gap at 1.6% institutional ownership. CRE membership positions FOAK output for ISCC+ certification." },
  { y: "2026 Q2", e: "Paraffinic Crude Extension + CIP Patent", s: "done", p: null, note: "Proves HCT works on a fourth feedstock category (waxy crude), expanding TAM by 7.9B bbl/yr. CIP patent filing shows strong enough results for IP protection." },
  { y: "2026 Q2", e: "Third-Party Yield Validation", s: "next", p: [25, 32], note: "Independent confirmation that HCT achieves 90%+ yield. Removes the 'company says it works' objection. Critical for institutional investors." },
  { y: "2026 Q3", e: "LCA Results + TotalEnergies Outcome", s: "wait", p: [32, 38], note: "Life Cycle Assessment by Delphi proves the environmental case. TotalEnergies converting from R&D to commercial engagement would be the strongest validation signal possible." },
  { y: "2026 Q4", e: "FOAK Construction Commences", s: "wait", p: [40, 45], note: "Physical construction is the point of no return. Engineering, permitting, financing, partner commitments all locked." },
  { y: "2027 H1", e: "First Commercial License Signed", s: "wait", p: [70, 100], note: "The single most important milestone. An industrial operator commits real capital based on demonstrated performance. Creates a reference customer for every subsequent deal." },
  { y: "2027 H2", e: "FOAK Plant Completed", s: "wait", p: null, note: "Operating FOAK at Chemelot produces actual Circular Naphtha in commercial quantities. Real revenue begins." },
  { y: "2028", e: "First Licensed Plant Operating", s: "wait", p: [80, 150], note: "A third-party operator running an HCT plant on their own feedstock, generating royalties. Proves technology transfers successfully." },
];

const VERTICALS = [
  { n: "Mixed Plastics", k: "HPU", tam: 400, u: "M t/yr", stage: "Pilot", c: "#2b6aa0" },
  { n: "Extra-Heavy Bitumen", k: "BIT", tam: 1600, u: "M bbl/yr", stage: "Bench", c: "#8a5a28" },
  { n: "Heavy/Sour Crude", k: "SOUR", tam: 4000, u: "M bbl/yr", stage: "Bench", c: "#a87420" },
  { n: "Renewable Oils", k: "HRU", tam: 80, u: "M t/yr", stage: "Lab", c: "#3d7a4a" },
  { n: "Paraffinic Crude", k: "PCO", tam: 7900, u: "M bbl/yr", stage: "Bench", c: "#6b4a8f" },
  { n: "Tire Rubber", k: "TIRE", tam: 20, u: "M t/yr", stage: "Proxy", c: "#a6443c" },
  { n: "Synthetic Turf", k: "TURF", tam: 1.5, u: "M t/yr", stage: "Lab", c: "#2d8a99" },
  { n: "Crosslinked (PEX/XLPE)", k: "XLPE", tam: 5, u: "M t/yr", stage: "Bench", c: "#a85280" },
  { n: "Refinery Bottom", k: "REF", tam: 3000, u: "M bbl/yr", stage: "Disclosed", c: "#5f7a2f" },
  { n: "Bio-oils (TCD)", k: "TCD", tam: 50, u: "M t/yr", stage: "Disclosed", c: "#a68234" },
  { n: "Polyurethane", k: "PU", tam: 26, u: "M t/yr", stage: "Testing", c: "#8a6dab" },
  { n: "Agri Plastics", k: "AGRI", tam: 25, u: "M t/yr", stage: "Disclosed", c: "#527a4a" },
  { n: "Manufacturer Waste", k: "MFG", tam: 35, u: "M t/yr", stage: "Disclosed", c: "#4a7fa5" },
];

function fmt(n) {
  if (Math.abs(n) >= 1e9) return "$" + (n / 1e9).toFixed(2) + "B";
  if (Math.abs(n) >= 1e6) return "$" + (n / 1e6).toFixed(1) + "M";
  if (Math.abs(n) >= 1e3) return "$" + (n / 1e3).toFixed(0) + "K";
  return "$" + n.toFixed(0);
}

function calc(e) {
  if (!e.enabled) return { profit: 0, rev: 0, vol: 0, tipRev: 0 };
  const vol = e.tam * (e.share / 100) * 1e6;
  const rev = e.salePrice !== undefined
    ? vol * ((e.yieldPct || 90) / 100) * e.salePrice * (e.royaltyPct / 100)
    : vol * e.royaltyPerUnit;
  return { profit: rev * (e.margin / 100), rev, vol, tipRev: vol * (e.tippingFee || 0) };
}

Object.assign(window, { CONF, ENGINES, PRESETS, PHASES, MILESTONES, VERTICALS, fmt, calc });
