// homepage.jsx — Lonetti homepage, all 8 sections
// Loaded after shared.jsx and product-art.jsx.

const PRODUCTS = [
  { name: "Prover Tanks",            tag: "50L – 5,000L+",  desc: "Stainless steel volumetric vessels for flow meter calibration and custody transfer.",                 art: "tank",   photo: LONETTI_PHOTOS.proverTank,         href: "prover-tanks.html" },
  { name: "Test Measures",           tag: "1L – 50L",       desc: "Handheld calibration cans for field verification of fuel dispensers and meters.",                   art: "measure",photo: LONETTI_PHOTOS.testMeasure_polished,href: "test-measures.html" },
  { name: "Water Draw Systems",      tag: "Pipe provers",   desc: "Complete waterdraw rigs for gravimetric and volumetric pipe prover calibration.",                   art: "water",  photo: LONETTI_PHOTOS.waterDraw,          href: "water-draw-systems.html" },
  { name: "Tanker Truck Towers",     tag: "Multi-tier",     desc: "Multi-tank tower systems for tanker and cistern vehicle verification.",                             art: "tower",  photo: LONETTI_PHOTOS.tankerTower,        href: "tanker-truck-calibration-towers.html" },
  { name: "Calibration Test Benches",tag: "Fixed / mobile", desc: "Custom fixed or mobile setups for laboratory and workshop meter verification.",                      art: "bench",  photo: LONETTI_PHOTOS.testBench,          href: "calibration-test-benches.html" },
  { name: "MINILAB® Vans",           tag: "Mobile lab",     desc: "Self-contained mobile calibration laboratories built into a van. Traceable, anywhere.",              art: "lab",    photo: LONETTI_PHOTOS.minilab,            href: "minilab.html" },
];

const ART_MAP = {
  tank: ProverTankArt, measure: TestMeasureArt, water: WaterDrawArt,
  tower: TankerTowerArt, bench: TestBenchArt, lab: MinilabArt,
};

const INDUSTRIES = [
  { name: "Oil & Gas",            blurb: "Custody transfer, fiscal metering, and API MPMS compliance for refineries and terminals.", icon: "factory", n: "01", href: "/industry-oil-gas.html" },
  { name: "Chemical Processing",  blurb: "Process calibration in 304/316 stainless steel for aggressive and corrosive liquids.",     icon: "droplet", n: "02", href: "/industry-chemical.html" },
  { name: "Water & Utilities",    blurb: "Bulk water measurement verification for public utilities and treatment plants.",          icon: "droplet", n: "03", href: "/industry-water-utilities.html" },
  { name: "Government Metrology", blurb: "Reference standards and field equipment for national weights & measures agencies.",       icon: "seal",    n: "04", href: "/industry-government-metrology.html" },
];

const WHY = [
  { title: "Custom Manufacturing",       n: "01", body: "Every Lonetti tank is built to your exact specifications. Any capacity, any geometry, any standard." },
  { title: "OIML & API Compliance",      n: "02", body: "All products meet OIML R117, API MPMS Ch. 4.4, and NIST Handbook 105-3 requirements." },
  { title: "Global Shipping",            n: "03", body: "We export to 47 countries — full crating, certificates of origin, and freight coordination included." },
  { title: "Traceable Certification",    n: "04", body: "Calibration certificates issued in coordination with INTI, traceable to international standards." },
];

const CERTS = ["OIML", "API MPMS", "INTI", "INMETRO", "ISO 9001", "NIST"];

const BLOG = [
  { tag: "Buyer's guide", date: "Apr 28, 2026", read: "9 min",
    title: "How to Select the Right Prover Tank: A Complete Buyer's Guide",
    excerpt: "Capacity, accuracy class, material specification, and certification — the four decisions that determine whether your prover tank will serve a custody-transfer application." },
  { tag: "Technical",     date: "Apr 14, 2026", read: "12 min",
    title: "API MPMS Chapter 4.4 Explained: What Operators Need to Know",
    excerpt: "The chapter governing tank prover construction, calibration, and use — translated from standard-speak into practical operator guidance." },
  { tag: "Comparison",    date: "Mar 30, 2026", read: "7 min",
    title: "Water Draw vs. Volumetric Proving: Key Differences",
    excerpt: "When to use each method, and which Lonetti equipment supports both — written for refinery measurement engineers and meter shop technicians." },
];

/* ─── Hero ──────────────────────────────────────────────────────────── */
function Hero({ heroVariant }) {
  return (
    <section className="surface-dark grid-bg" style={{ paddingTop: 80, paddingBottom: 0, position: "relative", overflow: "hidden" }}>
      {/* Full-bleed background photo (only for "photo" variant) */}
      {heroVariant === "photo" && (
        <div style={{ position: "absolute", inset: 0, zIndex: 0 }}>
          <img src={LONETTI_PHOTOS.proverTank} alt="Lonetti prover tank on wheels"
            style={{ width: "100%", height: "100%", objectFit: "cover" }}/>
          {/* Cinematic gradient — heavy left, fading right */}
          <div style={{ position: "absolute", inset: 0, background: "linear-gradient(100deg, rgba(11,15,26,0.94) 0%, rgba(11,15,26,0.82) 36%, rgba(11,15,26,0.45) 66%, rgba(11,15,26,0.12) 100%)" }}/>
          {/* Bottom fade so trust bar reads cleanly */}
          <div style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, transparent 65%, rgba(11,15,26,0.7) 100%)" }}/>
        </div>
      )}

      <div className="container" style={{ position: "relative", zIndex: 1 }}>
        {/* Top status row */}
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 56 }}>
          <span className="chip on-dark"><span className="chip-dot"/> Exporting worldwide</span>
          <span className="mono" style={{ fontSize: 11, letterSpacing: "0.12em", color: "rgba(255,255,255,0.5)", textTransform: "uppercase" }}>
            Cat. 2026 · Rev 04
          </span>
        </div>

        {heroVariant === "split" && <HeroSplit/>}
        {heroVariant === "photo" && <HeroPhoto/>}
        {heroVariant === "diagram" && <HeroDiagram/>}
        {heroVariant === "asymmetric" && <HeroAsymmetric/>}
      </div>

      {/* Trust bar */}
      <div className="hero-trust-bar" style={{
        marginTop: 96,
        borderTop: "1px solid rgba(255,255,255,0.12)",
        borderBottom: "1px solid rgba(255,255,255,0.12)",
        background: "rgba(0,0,0,0.5)",
        backdropFilter: "blur(8px)",
        WebkitBackdropFilter: "blur(8px)",
        position: "relative", zIndex: 1,
      }}>
        <div className="container">
          <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 0 }}>
            {[
              { stat: "1960",      label: "Founded in Buenos Aires" },
              { stat: "47",        label: "Countries served worldwide" },
              { stat: "OIML",      label: "R117 / API MPMS certified" },
              { stat: "304 SS",    label: "Standard construction material" },
            ].map((s, i) => (
              <div key={s.stat} style={{
                padding: "28px 24px",
                borderLeft: i === 0 ? 0 : "1px solid rgba(255,255,255,0.1)",
              }}>
                <div style={{
                  fontFamily: "var(--f-display)", fontSize: 32, fontWeight: 700,
                  letterSpacing: "-0.02em", color: "#fff", lineHeight: 1,
                }}>
                  {s.stat}
                </div>
                <div style={{ marginTop: 8, fontSize: 13, color: "rgba(255,255,255,0.6)" }}>
                  {s.label}
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
}

function HeroCopy() {
  return (
    <>
      <div className="eyebrow on-dark" style={{ marginBottom: 24 }}>Precision instruments · Est. 1960</div>
      <h1 style={{ color: "#fff", maxWidth: "11ch" }}>
        Precision liquid calibration equipment <span style={{ color: "var(--red)" }}>since 1960</span>.
      </h1>
      <p className="lede" style={{ marginTop: 28, color: "rgba(255,255,255,0.72)", maxWidth: "52ch" }}>
        Prover tanks · Test measures · Water draw systems · Calibration towers · Test benches · MINILAB® vans —
        built to OIML R117 & API MPMS standards, exported to 47 countries.
      </p>
      <div style={{ display: "flex", gap: 14, marginTop: 40, flexWrap: "wrap" }}>
        <a className="btn btn-primary" href="#quote">
          Request a Quote <Icon name="arrow" size={16} stroke="#fff" />
        </a>
        <a className="btn btn-ghost-light" href="#products">
          View Products <Icon name="arrow" size={16} />
        </a>
      </div>
    </>
  );
}

/* Hero variant — split layout (default): copy left, photo right */
function HeroSplit() {
  return (
    <div style={{ display: "grid", gridTemplateColumns: "1.05fr 0.95fr", gap: 64, alignItems: "center" }}>
      <div><HeroCopy/></div>
      <div style={{ position: "relative", minHeight: 460 }}>
        <HeroImageSlot/>
      </div>
    </div>
  );
}

/* Hero variant — full photo (background handled by parent section) */
function HeroPhoto() {
  return (
    <div style={{ position: "relative", minHeight: 560, padding: "56px 0 64px" }}>
      <div style={{ maxWidth: 720 }}>
        <HeroCopy/>
      </div>
    </div>
  );
}

/* Hero variant — engineering diagram on the right */
function HeroDiagram() {
  return (
    <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 80, alignItems: "center" }}>
      <div><HeroCopy/></div>
          <div style={{ color: "rgba(255,255,255,0.85)", display: "flex", justifyContent: "center" }}>
        <div style={{ position: "relative" }}>
          <ProverTankArt size={360} accent="#FF6B5A"/>
          {/* Engineering callouts */}
          <div className="hide-on-mobile" style={{ position: "absolute", top: 50, right: -100, fontFamily: "var(--f-mono)", fontSize: 11, color: "rgba(255,255,255,0.6)" }}>
            <div>SIGHT GLASS</div>
            <div style={{ height: 1, background: "rgba(255,255,255,0.3)", width: 80, marginTop: 4 }}/>
          </div>
          <div className="hide-on-mobile" style={{ position: "absolute", bottom: 110, left: -90, fontFamily: "var(--f-mono)", fontSize: 11, color: "rgba(255,255,255,0.6)", textAlign: "right" }}>
            <div>304 SS BODY</div>
            <div style={{ height: 1, background: "rgba(255,255,255,0.3)", width: 80, marginTop: 4, marginLeft: "auto" }}/>
          </div>
          <div className="hide-on-mobile" style={{ position: "absolute", bottom: 20, right: -80, fontFamily: "var(--f-mono)", fontSize: 11, color: "rgba(255,255,255,0.6)" }}>
            <div>DRAIN VALVE</div>
            <div style={{ height: 1, background: "rgba(255,255,255,0.3)", width: 60, marginTop: 4 }}/>
          </div>
        </div>
      </div>
    </div>
  );
}

/* Hero variant — asymmetric, big wordmark style */
function HeroAsymmetric() {
  return (
    <div style={{ position: "relative", paddingBottom: 80 }}>
      <div style={{ fontFamily: "var(--f-display)", fontWeight: 800, fontSize: "clamp(120px, 22vw, 280px)",
        lineHeight: 0.85, letterSpacing: "-0.05em", color: "var(--red)", marginBottom: -10 }}>
        LONETTI
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 80, alignItems: "end" }}>
        <h1 style={{ color: "#fff", fontSize: "clamp(28px, 3.4vw, 44px)", maxWidth: "20ch" }}>
          Precision liquid calibration equipment since 1960.
        </h1>
        <div>
          <p className="lede" style={{ color: "rgba(255,255,255,0.72)" }}>
            Prover tanks · Test measures · Water draw systems · Calibration towers ·
            Test benches · MINILAB® vans.
          </p>
          <div style={{ display: "flex", gap: 14, marginTop: 28 }}>
            <a className="btn btn-primary" href="#quote">Request a Quote <Icon name="arrow" size={16} stroke="#fff"/></a>
            <a className="btn btn-ghost-light" href="#products">View Products</a>
          </div>
        </div>
      </div>
    </div>
  );
}

/* Image slot (placeholder shown until user drags real photo via image-slot component) */
function HeroImageSlot() {
  return (
    <div style={{ position: "absolute", inset: 0 }}>
      {/* Use image-slot web component for drag-drop photo */}
      <image-slot id="hero-photo" shape="rect" radius="6" placeholder="Drag a photo of a Lonetti prover tank here">
        <img src={LONETTI_PHOTOS.proverTank} alt="Lonetti prover tank on wheels"
          style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover" }}/>
      </image-slot>
    </div>
  );
}

function HeroPhotoBg() {
  return (
    <image-slot id="hero-bg" shape="rect" radius="6" placeholder="Drag a Lonetti hero photo here" style={{ position: "absolute", inset: 0 }}>
      <img src={LONETTI_PHOTOS.proverTank} alt="Lonetti prover tank on wheels"
        style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover" }}/>
    </image-slot>
  );
}

/* ─── Section: Products ─────────────────────────────────────────────── */
function ProductsSection() {
  return (
    <section id="products">
      <div className="container">
        <SectionHead
          eyebrow="01 · Equipment portfolio"
          h2="Six product lines. One reference for liquid measurement."
          lede="From handheld 1L test measures to multi-tier truck calibration towers — every Lonetti product is built in stainless steel, certified to international standards, and customized to your application."
          cta={{ label: "Full product catalog", href: "#" }}
        />

        <div style={{
          marginTop: 64,
          display: "grid",
          gridTemplateColumns: "repeat(3, 1fr)",
          gap: 0,
          border: "1px solid var(--line)",
          borderRadius: 6,
          overflow: "hidden",
        }}>
          {PRODUCTS.map((p, i) => {
            const ArtComp = ART_MAP[p.art];
            const isRightEdge = (i + 1) % 3 === 0;
            const isBottomRow = i >= 3;
            return (
              <a key={p.name} href={p.href} className="prod-card" style={{
                display: "flex", flexDirection: "column",
                padding: "0 0 28px",
                background: "#fff",
                borderRight: isRightEdge ? 0 : "1px solid var(--line)",
                borderTop: isBottomRow ? "1px solid var(--line)" : 0,
                position: "relative", overflow: "hidden",
                transition: "background 200ms ease",
              }}>
                <div style={{
                  height: 240,
                  background: p.photo ? "var(--bg-mid)" : "var(--bg-alt)",
                  position: "relative",
                  overflow: "hidden",
                  borderBottom: "1px solid var(--line)",
                }}>
                  {p.photo ? (
                    <img src={p.photo} alt={p.name} loading="lazy"
                      style={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }}/>
                  ) : (
                    <div style={{ position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", color: "var(--charcoal)" }}>
                      <ArtComp size={200}/>
                    </div>
                  )}
                  <span className="chip" style={{
                    position: "absolute", top: 14, left: 14, background: "rgba(255,255,255,0.95)",
                    backdropFilter: "blur(4px)",
                  }}>
                    0{i + 1} / {p.tag}
                  </span>
                </div>
                <div style={{ padding: "24px 28px 0", display: "flex", flexDirection: "column", gap: 8, flex: 1 }}>
                  <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 12 }}>
                    <h3 className="h3" style={{ margin: 0 }}>{p.name}</h3>
                    <Icon name="arrow" size={16} stroke="var(--text-3)"/>
                  </div>
                  <p style={{ margin: 0, fontSize: 14, color: "var(--text-2)", lineHeight: 1.6 }}>{p.desc}</p>
                </div>
              </a>
            );
          })}
        </div>
      </div>
      <style>{`
        .prod-card:hover { background: var(--bg-alt) !important; }
        .prod-card:hover h3 { color: var(--red); }
      `}</style>
    </section>
  );
}

/* ─── Section head ──────────────────────────────────────────────────── */
function SectionHead({ eyebrow, h2, lede, cta, dark }) {
  return (
    <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 80, alignItems: "end" }}>
      <div>
        <div className={`eyebrow ${dark ? "on-dark" : ""}`} style={{ marginBottom: 18 }}>{eyebrow}</div>
        <h2 className="h2" style={{ color: dark ? "#fff" : "var(--text)", maxWidth: "18ch" }}>{h2}</h2>
      </div>
      <div>
        {lede && <p className="lede" style={{ color: dark ? "rgba(255,255,255,0.7)" : "var(--text-2)" }}>{lede}</p>}
        {cta && (
          <a className={`btn ${dark ? "btn-ghost-light" : "btn-ghost-dark"}`} href={cta.href} style={{ marginTop: 24 }}>
            {cta.label} <Icon name="arrow" size={16} className="ar"/>
          </a>
        )}
      </div>
    </div>
  );
}

/* ─── Section: Industries ───────────────────────────────────────────── */
function IndustriesSection() {
  return (
    <section style={{ background: "var(--bg-alt)" }}>
      <div className="container">
        <SectionHead
          eyebrow="02 · Industries served"
          h2="Trusted by the industries where measurement accuracy is the law."
          lede="Lonetti equipment is in service at YPF, ExxonMobil, Petrobras, and Shell refineries — alongside national metrology institutes and water utilities across four continents."
        />

        <div style={{ marginTop: 64, display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 24 }}>
          {INDUSTRIES.map((ind, i) => (
            <a key={ind.name} href={ind.href} style={{
              display: "flex", flexDirection: "column",
              padding: 28,
              background: "#fff",
              border: "1px solid var(--line)",
              borderRadius: 6,
              position: "relative",
              transition: "all 200ms ease",
              minHeight: 280,
            }}
            onMouseEnter={(e) => { e.currentTarget.style.borderColor = "var(--navy)"; e.currentTarget.style.transform = "translateY(-3px)"; }}
            onMouseLeave={(e) => { e.currentTarget.style.borderColor = "var(--line)"; e.currentTarget.style.transform = "translateY(0)"; }}
            >
              <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 32 }}>
                <span className="mono" style={{ fontSize: 11, letterSpacing: "0.12em", color: "var(--red)" }}>{ind.n}</span>
                <div style={{ width: 44, height: 44, borderRadius: 4, background: "var(--bg-alt)", display: "flex", alignItems: "center", justifyContent: "center", color: "var(--navy)" }}>
                  <Icon name={ind.icon} size={22}/>
                </div>
              </div>
              <div style={{ flex: 1 }}/>
              <h3 className="h3" style={{ fontSize: 20, marginBottom: 10 }}>{ind.name}</h3>
              <p style={{ margin: 0, fontSize: 13.5, color: "var(--text-2)", lineHeight: 1.55 }}>{ind.blurb}</p>
              <div style={{ marginTop: 20, display: "flex", alignItems: "center", gap: 8, color: "var(--red)", fontSize: 13, fontWeight: 500 }}>
                <span>Read brief</span><Icon name="arrow" size={14} stroke="var(--red)"/>
              </div>
            </a>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ─── Section: Why Lonetti ──────────────────────────────────────────── */
function WhySection() {
  return (
    <section className="surface-dark" style={{ position: "relative", overflow: "hidden" }}>
      <div style={{ position: "absolute", inset: 0, opacity: 0.4, pointerEvents: "none" }} className="grid-bg"/>
      <div className="container" style={{ position: "relative" }}>
        <SectionHead
          dark
          eyebrow="03 · Why Lonetti"
          h2="Sixty-six years of building tanks that don't drift."
          lede="Our customers come back because the equipment outlasts the contracts. Custom-built, certified, and serviced by the same shop that delivered Exxon Mobil's first prover in 1960."
        />

        <div style={{ marginTop: 64, display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 0,
          borderTop: "1px solid rgba(255,255,255,0.14)" }}>
          {WHY.map((w, i) => (
            <div key={w.title} style={{
              padding: "40px 28px 32px",
              borderRight: i < WHY.length - 1 ? "1px solid rgba(255,255,255,0.1)" : 0,
              borderBottom: "1px solid rgba(255,255,255,0.14)",
              position: "relative",
            }}>
              <div className="mono" style={{ fontSize: 11, letterSpacing: "0.12em", color: "var(--red)", marginBottom: 36 }}>{w.n}</div>
              <h3 style={{ fontFamily: "var(--f-display)", fontWeight: 600, fontSize: 22, color: "#fff", marginBottom: 14, letterSpacing: "-0.01em" }}>
                {w.title}
              </h3>
              <p style={{ margin: 0, fontSize: 14, lineHeight: 1.6, color: "rgba(255,255,255,0.65)" }}>{w.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ─── Section: Certifications ───────────────────────────────────────── */
function CertificationsSection() {
  return (
    <section style={{ paddingTop: 80, paddingBottom: 80 }}>
      <div className="container">
        <div style={{ display: "grid", gridTemplateColumns: "auto 1fr", gap: 80, alignItems: "center" }}>
          <div style={{ maxWidth: 360 }}>
            <div className="eyebrow" style={{ marginBottom: 14 }}>04 · Certifications & standards</div>
            <h3 style={{ fontFamily: "var(--f-display)", fontSize: 24, fontWeight: 600, letterSpacing: "-0.01em", color: "var(--text)", margin: 0, lineHeight: 1.3 }}>
              All Lonetti products are traceable to national and international metrology standards.
            </h3>
          </div>
          <div data-mobile-cols="2" style={{ display: "grid", gridTemplateColumns: "repeat(6, 1fr)", gap: 0, borderTop: "1px solid var(--line)", borderBottom: "1px solid var(--line)" }}>
            {CERTS.map((c, i) => (
              <div key={c} style={{
                padding: "28px 12px", textAlign: "center",
                borderRight: i < CERTS.length - 1 ? "1px solid var(--line)" : 0,
                fontFamily: "var(--f-display)", fontWeight: 700, fontSize: 18, letterSpacing: "0.04em",
                color: "var(--text)",
              }}>
                {c}
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
}

/* ─── Section: Reference / proof ───────────────────────────────────── */
function ReferenceSection() {
  return (
    <section style={{ background: "var(--bg-alt)", paddingTop: 96, paddingBottom: 96 }}>
      <div className="container">
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 80, alignItems: "center" }}>
          <div>
            <div className="eyebrow" style={{ marginBottom: 18 }}>Reference deployments</div>
            <h2 className="h2" style={{ fontSize: 40, maxWidth: "18ch", marginBottom: 24 }}>
              Trusted by petroleum companies in 47 countries.
            </h2>
            <p className="lede" style={{ marginBottom: 32 }}>
              From Exxon Mobil's first order in 1960 to today's deployments with YPF, Petrobras, and Shell —
              Lonetti equipment serves at refineries, terminals, and metrology institutes on four continents.
            </p>
            <div style={{ display: "flex", flexWrap: "wrap", gap: 12 }}>
              {["YPF", "ExxonMobil", "Petrobras", "Shell", "TotalEnergies", "Pluspetrol", "Axion", "INTI"].map((n) => (
                <span key={n} className="chip">{n}</span>
              ))}
            </div>
          </div>
          <WorldMap/>
        </div>
      </div>
    </section>
  );
}

function WorldMap() {
  // Equirectangular projection: lon -180..180 → x 0..100, lat 90..-90 → y 0..50
  const proj = (lat, lon) => [(lon + 180) / 3.6, (90 - lat) / 3.6];

  // 47 deployments — countries served by Lonetti
  const COUNTRIES = [
    // Americas (14)
    { name: "Argentina",    lat: -34.6, lon: -58.4, hq: true },
    { name: "Brazil",       lat: -15.8, lon: -47.9 },
    { name: "Chile",        lat: -33.4, lon: -70.6 },
    { name: "Peru",         lat: -12.0, lon: -77.0 },
    { name: "Colombia",     lat:   4.7, lon: -74.0 },
    { name: "Ecuador",      lat:  -0.2, lon: -78.5 },
    { name: "Panama",       lat:   8.9, lon: -79.5 },
    { name: "Costa Rica",   lat:   9.9, lon: -84.0 },
    { name: "El Salvador",  lat:  13.7, lon: -89.2 },
    { name: "Honduras",     lat:  14.1, lon: -87.2 },
    { name: "Guatemala",    lat:  14.6, lon: -90.5 },
    { name: "Dom. Rep.",    lat:  18.5, lon: -69.9 },
    { name: "Mexico",       lat:  19.4, lon: -99.1 },
    { name: "United States",lat:  38.9, lon: -77.0 },
    // Europe (7)
    { name: "Portugal",     lat:  38.7, lon:  -9.1 },
    { name: "Spain",        lat:  40.4, lon:  -3.7 },
    { name: "France",       lat:  48.9, lon:   2.4 },
    { name: "United Kingdom",lat: 51.5, lon:  -0.1 },
    { name: "Netherlands",  lat:  52.4, lon:   4.9 },
    { name: "Germany",      lat:  52.5, lon:  13.4 },
    { name: "Italy",        lat:  41.9, lon:  12.5 },
    // Africa (6)
    { name: "Morocco",      lat:  33.6, lon:  -7.6 },
    { name: "Algeria",      lat:  28.0, lon:   3.0 },
    { name: "Ghana",        lat:   5.6, lon:  -0.2 },
    { name: "Nigeria",      lat:   9.1, lon:   7.5 },
    { name: "Kenya",        lat:  -1.3, lon:  36.8 },
    { name: "South Africa", lat: -26.2, lon:  28.0 },
    // Middle East (14)
    { name: "Egypt",        lat:  30.0, lon:  31.2 },
    { name: "Turkey",       lat:  39.9, lon:  32.9 },
    { name: "Lebanon",      lat:  33.9, lon:  35.5 },
    { name: "Israel",       lat:  31.8, lon:  35.2 },
    { name: "Jordan",       lat:  31.9, lon:  35.9 },
    { name: "Iraq",         lat:  33.3, lon:  44.4 },
    { name: "Iran",         lat:  35.7, lon:  51.4 },
    { name: "Kuwait",       lat:  29.4, lon:  47.9 },
    { name: "Bahrain",      lat:  26.2, lon:  50.6 },
    { name: "Qatar",        lat:  25.3, lon:  51.5 },
    { name: "Saudi Arabia", lat:  24.7, lon:  46.7 },
    { name: "UAE",          lat:  24.5, lon:  54.4 },
    { name: "Oman",         lat:  23.6, lon:  58.5 },
    { name: "Yemen",        lat:  15.4, lon:  44.2 },
    // Asia (6)
    { name: "Pakistan",     lat:  33.7, lon:  73.1 },
    { name: "India",        lat:  28.6, lon:  77.2 },
    { name: "Thailand",     lat:  13.7, lon: 100.5 },
    { name: "Vietnam",      lat:  21.0, lon: 105.8 },
    { name: "Malaysia",     lat:   3.1, lon: 101.7 },
    { name: "Indonesia",    lat:  -6.2, lon: 106.8 },
  ];

  // Approximate land mask — unions of polygonal regions, equirectangular coords (0–100, 0–50)
  const inLand = (x, y) => {
    // North America bulk
    if (x>=12 && x<=30 && y>=8 && y<=20) {
      if (x<16 && y>14) return false;          // Pacific notch
      if (x>26 && y<11) return false;          // NE Atlantic notch
      if (x>28 && y>16) return false;          // Gulf of Mexico
      return true;
    }
    // Alaska
    if (x>=4 && x<=14 && y>=6 && y<=11) return true;
    // Central America (narrow)
    if (x>=22 && x<=28 && y>=19 && y<=22.5) {
      const w = 22 + (y-19)*0.5; // shift east as we go south
      return x>=w-1.5 && x<=w+2;
    }
    // South America — wedge narrowing south
    if (y>=22 && y<=40) {
      const cx = 33 - (y-22)*0.15;
      const half = Math.max(1.2, 5 - (y-22)*0.22);
      if (x>=cx-half && x<=cx+half) return true;
    }
    // Greenland
    if (x>=35 && x<=44 && y>=3 && y<=11) {
      if (x<37 && y<6) return false;
      return true;
    }
    // Europe
    if (x>=44 && x<=58 && y>=8 && y<=17) {
      if (x<46 && y>13) return false;          // Atlantic
      if (x<48 && y>14) return false;          // Iberia notch
      return true;
    }
    // Scandinavia
    if (x>=49 && x<=56 && y>=5 && y<=10) return true;
    // Africa
    if (y>=17 && y<=37) {
      const cx = 54 + (y-17)*0.15;
      const half = y<24 ? (7 - Math.abs(y-21)*0.3) : (7.5 - (y-24)*0.45);
      if (half>0 && x>=cx-half && x<=cx+half) {
        if (x<50 && y>30) return false;        // SW Atlantic
        return true;
      }
    }
    // Middle East
    if (x>=58 && x<=68 && y>=14 && y<=22) {
      if (x>66 && y<16) return false;
      return true;
    }
    // Russia / Siberia (wide band)
    if (x>=50 && x<=95 && y>=4 && y<=10) return true;
    // Central Asia
    if (x>=60 && x<=85 && y>=10 && y<=14) return true;
    // South Asia (India peninsula)
    if (x>=68 && x<=78 && y>=14 && y<=23) {
      const cx = 73 - (y-14)*0.25;
      const half = 5 - (y-14)*0.4;
      return half>0 && x>=cx-half && x<=cx+half;
    }
    // China / East Asia
    if (x>=75 && x<=92 && y>=10 && y<=20) {
      if (x>90 && y>17) return false;
      return true;
    }
    // SE Asia mainland
    if (x>=77 && x<=84 && y>=18 && y<=24) return true;
    // Indonesia / archipelago (scattered)
    if (x>=78 && x<=92 && y>=24 && y<=28) {
      return ((Math.floor(x) + Math.floor(y)) % 2 === 0);
    }
    // Japan
    if (x>=89 && x<=93 && y>=11 && y<=15) return true;
    // Philippines
    if (x>=84 && x<=87 && y>=18 && y<=23) return ((Math.floor(x)+Math.floor(y)) % 2 === 0);
    // Australia
    if (x>=80 && x<=93 && y>=29 && y<=36) {
      if (x>91 && y<31) return false;
      if (x<82 && y>33) return false;
      return true;
    }
    // New Zealand
    if (x>=92 && x<=95 && y>=34 && y<=37) return true;
    return false;
  };

  const STEP = 1;       // grid spacing
  const COLS = 100;
  const ROWS = 50;
  const landDots = [];
  for (let y = 2; y < ROWS; y += STEP) {
    for (let x = 0; x < COLS; x += STEP) {
      if (inLand(x, y)) landDots.push([x, y]);
    }
  }

  return (
    <div className="world-map" style={{ position: "relative", aspectRatio: "16/10", background: "#fff", border: "1px solid var(--line)", borderRadius: 6, padding: 24, overflow: "hidden" }}>
      <div className="eyebrow muted" style={{ position: "absolute", top: 18, left: 24, zIndex: 2 }}>Global presence</div>
      <div className="mono" style={{ position: "absolute", top: 18, right: 24, fontSize: 11, color: "var(--text-3)", zIndex: 2 }}>47 / 195</div>

      <svg viewBox="0 0 100 50" preserveAspectRatio="xMidYMid meet" style={{ width: "100%", height: "100%", marginTop: 24, display: "block" }}>
        {/* Land dots */}
        {landDots.map(([x, y], i) => (
          <circle key={i} cx={x} cy={y} r="0.32" fill="var(--line-2)"/>
        ))}
        {/* Deployment pins */}
        {COUNTRIES.map((c, i) => {
          const [x, y] = proj(c.lat, c.lon);
          return (
            <g key={c.name}>
              {c.hq && (
                <>
                  <circle cx={x} cy={y} r="2.6" fill="var(--red)" opacity="0.12"/>
                  <circle cx={x} cy={y} r="1.6" fill="var(--red)" opacity="0.22"/>
                </>
              )}
              <circle cx={x} cy={y} r={c.hq ? 0.95 : 0.7} fill="var(--red)">
                <title>{c.name}</title>
              </circle>
            </g>
          );
        })}
      </svg>

      <div style={{ position: "absolute", bottom: 18, left: 24, right: 24, display: "flex", justifyContent: "space-between", alignItems: "center", gap: 16, fontSize: 11, color: "var(--text-3)", fontFamily: "var(--f-mono)" }}>
        <div style={{ display: "flex", gap: 16 }}>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 6 }}>
            <span style={{ width: 8, height: 8, borderRadius: "50%", background: "var(--red)" }}/> Deployments
          </span>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 6 }}>
            <span style={{ position: "relative", width: 12, height: 12, display: "inline-block" }}>
              <span style={{ position: "absolute", inset: 0, borderRadius: "50%", background: "var(--red)", opacity: 0.18 }}/>
              <span style={{ position: "absolute", inset: 3, borderRadius: "50%", background: "var(--red)" }}/>
            </span>
            HQ — Buenos Aires
          </span>
        </div>
        <span style={{ textTransform: "uppercase", letterSpacing: "0.1em" }}>Lat / Lon · Equirect.</span>
      </div>
    </div>
  );
}

/* ─── Section: Blog ─────────────────────────────────────────────────── */
function BlogSection() {
  return (
    <section>
      <div className="container">
        <SectionHead
          eyebrow="05 · Knowledge hub"
          h2="Technical guides for measurement engineers."
          lede="Standards explained in plain English, buyer's guides for procurement, and field notes from six decades of calibration practice."
          cta={{ label: "All articles", href: "#" }}
        />

        <div style={{ marginTop: 64, display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 24 }}>
          {BLOG.map((b, i) => {
            const blogPhoto = [LONETTI_PHOTOS.proverTank, LONETTI_PHOTOS.waterDraw, LONETTI_PHOTOS.testMeasure][i];
            return (
              <a key={b.title} href="#" style={{
                display: "flex", flexDirection: "column",
                border: "1px solid var(--line)",
                borderRadius: 6,
                background: "#fff",
                overflow: "hidden",
                transition: "all 200ms ease",
              }}
              onMouseEnter={(e) => { e.currentTarget.style.borderColor = "var(--charcoal)"; e.currentTarget.style.transform = "translateY(-2px)"; }}
              onMouseLeave={(e) => { e.currentTarget.style.borderColor = "var(--line)"; e.currentTarget.style.transform = "translateY(0)"; }}
              >
                <div style={{ aspectRatio: "16/10", background: "var(--bg-mid)", position: "relative", overflow: "hidden" }}>
                  <img src={blogPhoto} alt={b.title} loading="lazy"
                    style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover" }}/>
                  <div style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, rgba(0,0,0,0.2) 0%, transparent 40%, transparent 100%)" }}/>
                  <span className="chip on-dark" style={{ position: "absolute", top: 16, left: 16, background: "rgba(11,15,26,0.7)", backdropFilter: "blur(6px)" }}>{b.tag}</span>
                </div>
                <div style={{ padding: "24px 24px 28px", display: "flex", flexDirection: "column", gap: 14, flex: 1 }}>
                  <div className="mono" style={{ fontSize: 11, letterSpacing: "0.1em", color: "var(--text-3)", textTransform: "uppercase" }}>
                    {b.date} · {b.read} read
                  </div>
                  <h3 className="h3" style={{ fontSize: 19, lineHeight: 1.3 }}>{b.title}</h3>
                  <p style={{ margin: 0, fontSize: 14, color: "var(--text-2)", lineHeight: 1.55 }}>{b.excerpt}</p>
                  <div style={{ marginTop: "auto", display: "flex", alignItems: "center", gap: 8, color: "var(--red)", fontSize: 13, fontWeight: 500 }}>
                    <span>Continue reading</span><Icon name="arrow" size={14} stroke="var(--red)"/>
                  </div>
                </div>
              </a>
            );
          })}
        </div>
      </div>
    </section>
  );
}

/* ─── Section: CTA / Quote form ────────────────────────────────────── */
function CtaQuoteSection() {
  return (
    <section id="quote" style={{ background: "var(--red)", color: "#fff", padding: "120px 0", position: "relative", overflow: "hidden" }}>
      {/* Diagonal pattern accent */}
      <div style={{
        position: "absolute", inset: 0, opacity: 0.08, pointerEvents: "none",
        backgroundImage: "linear-gradient(45deg, #fff 1px, transparent 1px)",
        backgroundSize: "10px 10px",
      }}/>
      <div className="container" style={{ position: "relative" }}>
        <div style={{ display: "grid", gridTemplateColumns: "1.1fr 1fr", gap: 80, alignItems: "start" }}>
          <div>
            <div className="eyebrow on-dark" style={{ color: "#fff", opacity: 0.7, marginBottom: 24 }}>06 · Talk to engineering</div>
            <h2 className="h2" style={{ color: "#fff", maxWidth: "14ch", marginBottom: 28, fontSize: "clamp(40px, 4.4vw, 64px)" }}>
              Ready to elevate your liquid measurement?
            </h2>
            <p className="lede" style={{ color: "rgba(255,255,255,0.85)", marginBottom: 36 }}>
              Tell us what you need to measure, and at what accuracy. Our engineering team in Buenos Aires
              will return with a configuration and a quote within two business days.
            </p>
            <div style={{ display: "flex", flexDirection: "column", gap: 16, color: "rgba(255,255,255,0.9)", fontSize: 14 }}>
              <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
                <Icon name="whatsapp" size={20}/>
                <span><b>WhatsApp:</b> +54 9 11 2788 4190 · EN / ES / PT</span>
              </div>
              <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
                <Icon name="mail" size={20}/>
                <span><b>Email:</b> info@lonetti.com.ar</span>
              </div>
              <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
                <Icon name="pin" size={20}/>
                <span><b>Workshop:</b> Buenos Aires, Argentina</span>
              </div>
            </div>
          </div>

          <QuoteForm/>
        </div>
      </div>
    </section>
  );
}

function QuoteForm() {
  const [submitted, setSubmitted] = useState(false);
  const [loading, setLoading] = useState(false);
  const [serverError, setServerError] = useState(null);
  const [errors, setErrors] = useState({});
  const [data, setData] = useState({ name: "", company: "", country: "", email: "", product: "", message: "" });

  const onSubmit = async (e) => {
    e.preventDefault();
    const next = {};
    ["name", "company", "country", "email"].forEach((k) => { if (!data[k]) next[k] = "Required"; });
    if (data.email && !/^\S+@\S+\.\S+$/.test(data.email)) next.email = "Invalid email";
    setErrors(next);
    if (Object.keys(next).length > 0) return;
    setLoading(true);
    setServerError(null);
    try {
      await submitForm({ ...data, _subject: `Quote request — ${data.product || "General"} — ${data.company}` });
      setSubmitted(true);
    } catch {
      setServerError("Something went wrong. Please try again or email us directly at info@lonetti.com.ar");
    } finally {
      setLoading(false);
    }
  };

  const field = (k, label, type = "text") => (
    <label style={{ display: "flex", flexDirection: "column", gap: 6 }}>
      <span style={{ fontSize: 11, fontFamily: "var(--f-mono)", letterSpacing: "0.1em", textTransform: "uppercase", color: "rgba(255,255,255,0.7)" }}>{label}</span>
      <input type={type} value={data[k]} onChange={(e) => setData({...data, [k]: e.target.value})}
        style={{
          padding: "12px 14px",
          background: "rgba(255,255,255,0.08)",
          border: `1px solid ${errors[k] ? "#FFDC8A" : "rgba(255,255,255,0.2)"}`,
          borderRadius: 4,
          color: "#fff",
          fontSize: 14,
          fontFamily: "var(--f-body)",
        }}/>
      {errors[k] && <span style={{ fontSize: 11, color: "#FFDC8A" }}>{errors[k]}</span>}
    </label>
  );

  if (submitted) {
    return (
      <div style={{ background: "rgba(255,255,255,0.08)", border: "1px solid rgba(255,255,255,0.2)", borderRadius: 6, padding: 36, minHeight: 480, display: "flex", flexDirection: "column", justifyContent: "center" }}>
        <div style={{ width: 56, height: 56, borderRadius: "50%", background: "#fff", color: "var(--red)", display: "flex", alignItems: "center", justifyContent: "center", marginBottom: 24 }}>
          <Icon name="check" size={28} stroke="var(--red)" strokeWidth={2}/>
        </div>
        <h3 style={{ fontFamily: "var(--f-display)", fontSize: 28, color: "#fff", marginBottom: 12 }}>Quote request received.</h3>
        <p style={{ margin: 0, color: "rgba(255,255,255,0.85)" }}>An engineer will be in touch within two business days. For urgent requirements, message us on WhatsApp.</p>
      </div>
    );
  }

  return (
    <form onSubmit={onSubmit} style={{ background: "rgba(0,0,0,0.18)", border: "1px solid rgba(255,255,255,0.18)", borderRadius: 6, padding: 36, display: "flex", flexDirection: "column", gap: 18 }}>
      <div className="eyebrow on-dark" style={{ color: "#fff", opacity: 0.85, marginBottom: 4 }}>Quote request</div>
      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 14 }}>
        {field("name", "Full name")}
        {field("company", "Company")}
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 14 }}>
        {field("country", "Country")}
        {field("email", "Work email", "email")}
      </div>
      <label style={{ display: "flex", flexDirection: "column", gap: 6 }}>
        <span style={{ fontSize: 11, fontFamily: "var(--f-mono)", letterSpacing: "0.1em", textTransform: "uppercase", color: "rgba(255,255,255,0.7)" }}>Product of interest</span>
        <select value={data.product} onChange={(e) => setData({...data, product: e.target.value})}
          style={{
            padding: "12px 14px", background: "rgba(255,255,255,0.08)",
            border: "1px solid rgba(255,255,255,0.2)", borderRadius: 4,
            color: "#fff", fontSize: 14, fontFamily: "var(--f-body)",
          }}>
          <option value="" style={{ color: "#333" }}>Select…</option>
          {PRODUCTS.map((p) => <option key={p.name} value={p.name} style={{ color: "#333" }}>{p.name}</option>)}
          <option value="Other" style={{ color: "#333" }}>Other / not sure</option>
        </select>
      </label>
      <label style={{ display: "flex", flexDirection: "column", gap: 6 }}>
        <span style={{ fontSize: 11, fontFamily: "var(--f-mono)", letterSpacing: "0.1em", textTransform: "uppercase", color: "rgba(255,255,255,0.7)" }}>Requirements (optional)</span>
        <textarea rows="3" value={data.message} onChange={(e) => setData({...data, message: e.target.value})}
          placeholder="Capacity, accuracy class, standards required, application…"
          className="quote-textarea"
          style={{
            padding: "12px 14px", background: "rgba(255,255,255,0.08)",
            border: "1px solid rgba(255,255,255,0.2)", borderRadius: 4,
            color: "#fff", fontSize: 14, fontFamily: "var(--f-body)", resize: "vertical",
          }}/>
        <style>{`
          .quote-textarea::placeholder { color: rgba(255,255,255,0.85); opacity: 1; }
          .quote-textarea::-webkit-input-placeholder { color: rgba(255,255,255,0.85); }
          .quote-textarea::-moz-placeholder { color: rgba(255,255,255,0.85); opacity: 1; }
        `}</style>
      </label>
      {serverError && (
        <p style={{ margin: 0, fontSize: 13, color: "#FFDC8A", background: "rgba(255,220,138,0.1)", padding: "10px 14px", borderRadius: 4 }}>
          {serverError}
        </p>
      )}
      <button type="submit" disabled={loading} style={{
        marginTop: 6, padding: "14px 22px", background: loading ? "rgba(255,255,255,0.6)" : "#fff", color: "var(--red)",
        border: 0, borderRadius: 4, fontFamily: "var(--f-body)", fontSize: 14,
        fontWeight: 700, letterSpacing: "0.01em", display: "inline-flex", justifyContent: "center", alignItems: "center", gap: 10,
        cursor: loading ? "not-allowed" : "pointer",
      }}>
        {loading ? "Sending…" : <><span>Submit request</span><Icon name="arrow" size={16} stroke="var(--red)"/></>}
      </button>
    </form>
  );
}

/* ─── Homepage root ─────────────────────────────────────────────────── */
function Homepage({ tweaks }) {
  return (
    <>
      <Nav active="home"/>
      <main>
        <Hero heroVariant={tweaks.heroVariant}/>
        <window.ClientLogoStrip/>
        <ProductsSection/>
        <IndustriesSection/>
        <WhySection/>
        <CertificationsSection/>
        <ReferenceSection/>
        <BlogSection/>
        <CtaQuoteSection/>
      </main>
      <Footer/>
      <FloatingWhatsApp/>
    </>
  );
}

Object.assign(window, { Homepage, SectionHead, CtaQuoteSection });
