// Stats — wide animated counter strip
function Stats({ t }) {
  const stats = [
    { num: 500, plus:true, label: "Industrial clients", note: "manufacturing · O&G · plantation"},
    { num: 21,  label: "Years operating", note: "established 2004" },
    { num: 11,  label: "Parameters tracked per IAQ", note: "ICOP IAQ DOSH 2010"},
    { num: 5,   label: "Year CHRA validity", note: "if no significant change" },
  ];
  return (
    <section className="stats section">
      <div className="wrap">
        <div className="stats-grid">
          {stats.map((s,i)=>(
            <StatCard key={i} {...s} motion={t.motion}/>
          ))}
        </div>
      </div>
      <style>{`
        .stats{padding:80px 0;border-top:1px solid var(--line);border-bottom:1px solid var(--line)}
        .stats-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:0}
        @media (max-width: 860px){.stats-grid{grid-template-columns:repeat(2,1fr)}}
        @media (max-width: 480px){.stats-grid{grid-template-columns:1fr}}
        .stat-card{padding:24px 28px;border-right:1px solid var(--line);position:relative;overflow:hidden}
        .stat-card:last-child{border-right:0}
        @media (max-width: 860px){.stat-card{border-bottom:1px solid var(--line)} .stat-card:nth-child(2n){border-right:0} .stat-card:nth-last-child(-n+2){border-bottom:0}}
        .stat-num{font-size:clamp(48px,7vw,84px);line-height:.95;letter-spacing:-.04em;font-weight:600;
          background:var(--grad);-webkit-background-clip:text;background-clip:text;color:transparent}
        .stat-label{margin-top:6px;font-size:14px;color:var(--ink-soft)}
        .stat-note{margin-top:4px;font-family:var(--font-mono);font-size:10.5px;letter-spacing:.14em;text-transform:uppercase;color:var(--ink-mute)}
      `}</style>
    </section>
  );
}

function StatCard({ num, plus, label, note, motion }) {
  const [v, setV] = React.useState(0);
  const ref = React.useRef(null);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const obs = new IntersectionObserver((entries)=>{
      entries.forEach(e=>{
        if (e.isIntersecting){
          let t0 = performance.now(); const dur = 1600;
          const tick = () => {
            const tt = Math.min(1, (performance.now() - t0) / dur);
            const e2 = 1 - Math.pow(1 - tt, 3);
            setV(Math.round(num * e2));
            if (tt < 1) requestAnimationFrame(tick);
          };
          requestAnimationFrame(tick);
          obs.disconnect();
        }
      });
    }, { threshold: .4 });
    obs.observe(el);
    return () => obs.disconnect();
  }, [num]);
  return (
    <div className="stat-card" ref={ref}>
      <div className="stat-num mono">{v}{plus?"+":""}</div>
      <div className="stat-label">{label}</div>
      <div className="stat-note">{note}</div>
    </div>
  );
}

window.Stats = Stats;
