// Honesty + Customs sections.
const { useState: useStateH, useEffect: useEffectH, useRef: useRefH } = React;

// Thin line icons — 1.5px stroke, no fills
const LI = {
  savings: <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M12 1v22"/><path d="M17 5H9.5a3.5 3.5 0 1 0 0 7h5a3.5 3.5 0 1 1 0 7H6"/></svg>,
  spec:    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="9"/><path d="M12 3v4M12 17v4M3 12h4M17 12h4"/><circle cx="12" cy="12" r="3"/></svg>,
  newer:   <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M3 12a9 9 0 0 1 15-6.7L21 8"/><path d="M21 3v5h-5"/><path d="M21 12a9 9 0 0 1-15 6.7L3 16"/><path d="M3 21v-5h5"/></svg>,
  fresh:   <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M3 17h2l1.5-7h11L19 17h2"/><circle cx="7" cy="17" r="2"/><circle cx="17" cy="17" r="2"/></svg>,
  invoice: <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M5 3h11l3 3v15H5z"/><path d="M16 3v3h3"/><path d="M8 11h8M8 15h6M8 7h4"/></svg>,
  fleet:   <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><rect x="2" y="9" width="9" height="7" rx="1"/><rect x="13" y="6" width="9" height="10" rx="1"/><circle cx="5" cy="18" r="1.5"/><circle cx="9" cy="18" r="1.5"/><circle cx="16" cy="18" r="1.5"/><circle cx="20" cy="18" r="1.5"/></svg>,
  clock:   <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>,
  warranty:<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3l8 3v6c0 5-3.5 8-8 9-4.5-1-8-4-8-9V6z"/><path d="M9 12l2 2 4-4"/></svg>,
  lang:    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18"/></svg>,
  charger: <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><rect x="5" y="3" width="11" height="18" rx="1.5"/><path d="M9 8h3M9 12h3"/><path d="M16 9h3v4a2 2 0 0 1-2 2"/></svg>,
  customs: <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M3 9l9-5 9 5"/><path d="M5 9v10h14V9"/><path d="M10 19v-6h4v6"/></svg>,
  resale:  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M3 17l6-6 4 4 8-8"/><path d="M14 7h7v7"/></svg>,
};

const HONESTY_GAINS = [
  { icon: "savings", title: "Significant savings", body: "30 to 43 percent less than local dealer prices. The savings come from one place: no dealer margin. You pay the factory invoice plus transparent landed costs." },
  { icon: "spec",    title: "Exact spec you want", body: "Local dealers stock two or three trims. China has every trim, every color, every option. You order the car you actually want." },
  { icon: "newer",   title: "Newer models, faster", body: "Chinese EVs hit the China market 12 to 18 months before they reach global dealers. With Icon One, you drive 2026 spec while local showrooms still stock 2024." },
  { icon: "fresh",   title: "Factory fresh", body: "Every car is brand new from the assembly line. Zero kilometers, zero showroom miles, zero test drives." },
  { icon: "invoice", title: "Transparent invoice", body: "You see the China factory invoice. You see the freight cost. You see the customs receipt. No hidden margin." },
  { icon: "fleet",   title: "Fleet pricing unlocked", body: "Buying three or more cars? Factory direct rates drop further. Local dealers can't match this for fleets." },
];

const HONESTY_KNOWS = [
  { icon: "clock",    title: "45 to 60 day wait", body: "Local dealers hand you keys today. Sourcing from China takes 45 to 60 days door to door. Plan around it." },
  { icon: "warranty", title: "Warranty handling", body: "Manufacturer warranty is honored at BYD, Zeekr, and Geely authorized service centers in many markets, but coverage is thinner than for locally-sold cars. We help you map the nearest authorized service before you order." },
  { icon: "lang",     title: "Software language", body: "Some China-spec cars ship with Mandarin as the default infotainment language. We confirm your preferred language before shipping and reflash if needed." },
  { icon: "charger",  title: "Charger compatibility", body: "Most modern Chinese EVs use GB/T DC fast-charging connectors. Many markets support both GB/T and CCS2. Confirm with us per model and destination." },
  { icon: "customs",  title: "Customs valuation risk", body: "Some customs offices occasionally re-assess invoice values. We provide manufacturer-certified invoices and handle disputes. This rarely affects final cost, but it can add one to two weeks." },
  { icon: "resale",   title: "Resale market is younger", body: "Chinese EV resale value in newer markets is still being established. BYD and Zeekr have strong resale globally; expect this to strengthen locally over the next two to three years." },
];

function HonestyCard({ item, delay, accent }) {
  const ref = useRefH(null);
  const [vis, setVis] = useStateH(false);
  useEffectH(() => {
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver((es) => es.forEach((e) => { if (e.isIntersecting) { setVis(true); io.disconnect(); } }), { threshold: 0.15 });
    io.observe(el); return () => io.disconnect();
  }, []);
  return (
    <div ref={ref}
         style={{
           padding: "22px 22px 24px",
           border: "1px solid var(--line)",
           background: "var(--ink-1)",
           opacity: vis ? 1 : 0,
           transform: vis ? "none" : "translateY(14px)",
           transition: `opacity .6s cubic-bezier(.2,.7,.2,1) ${delay}ms, transform .6s cubic-bezier(.2,.7,.2,1) ${delay}ms, border-color .25s, background .25s`,
         }}
         onMouseEnter={(e) => { e.currentTarget.style.borderColor = accent; e.currentTarget.style.background = "var(--ink-2)"; }}
         onMouseLeave={(e) => { e.currentTarget.style.borderColor = "var(--line)"; e.currentTarget.style.background = "var(--ink-1)"; }}>
      <div style={{ width: 32, height: 32, color: accent, marginBottom: 16 }}>{LI[item.icon]}</div>
      <h4 style={{ margin: 0, fontSize: 15, fontWeight: 500, color: "var(--bone)", letterSpacing: ".005em" }}>{item.title}</h4>
      <p style={{ margin: "10px 0 0", fontSize: 13.5, lineHeight: 1.6, color: "var(--mute)" }}>{item.body}</p>
    </div>
  );
}

function HonestSection() {
  return (
    <section id="honest" style={{ padding: "120px 0", borderTop: "1px solid var(--line)", background: "var(--ink-1)" }}>
      <div className="wrap">
        <SectionLabel eyebrow="The honest case" title="Buying a Chinese EV. Both sides." sub="We won't pretend there are no trade-offs. Here's what we tell every buyer before they commit." lang="en" />

        <div className="honest-grid">
          <div className="honest-col">
            <div className="honest-col-head">
              <span className="mono" style={{ fontSize: 10, letterSpacing: ".18em", color: "var(--amber)", textTransform: "uppercase" }}>What you gain</span>
              <span style={{ flex: 1, height: 1, background: "linear-gradient(90deg, rgba(232,115,46,.4), transparent)", marginInlineStart: 14 }} />
            </div>
            <div className="honest-cards">
              {HONESTY_GAINS.map((it, i) => <HonestyCard key={i} item={it} delay={i * 80} accent="var(--amber)" />)}
            </div>
          </div>

          <div className="honest-divider" aria-hidden="true" />

          <div className="honest-col">
            <div className="honest-col-head">
              <span className="mono" style={{ fontSize: 10, letterSpacing: ".18em", color: "var(--bone-2)", textTransform: "uppercase" }}>What to know</span>
              <span style={{ flex: 1, height: 1, background: "linear-gradient(90deg, rgba(216,210,195,.35), transparent)", marginInlineStart: 14 }} />
            </div>
            <div className="honest-cards">
              {HONESTY_KNOWS.map((it, i) => <HonestyCard key={i} item={it} delay={i * 80 + 120} accent="var(--bone-2)" />)}
            </div>
          </div>
        </div>

        <p data-reveal className="serif" style={{ textAlign: "center", marginTop: 64, fontStyle: "italic", fontSize: 22, color: "var(--mute)", letterSpacing: "-.005em" }}>
          We've shipped over 1,000 vehicles into the region. Every concern above, we have a process for. Ask us anything.
        </p>
      </div>

      <style>{`
        .honest-grid { display: grid; grid-template-columns: 1fr; gap: 40px; position: relative; }
        @media (min-width: 920px) {
          .honest-grid { grid-template-columns: 1fr 1px 1fr; gap: 56px; }
        }
        .honest-divider { display: none; }
        @media (min-width: 920px) {
          .honest-divider { display: block; background: var(--line); width: 1px; }
        }
        .honest-col-head { display: flex; align-items: center; margin-bottom: 24px; }
        .honest-cards { display: grid; grid-template-columns: 1fr; gap: 12px; }
        @media (min-width: 560px) { .honest-cards { grid-template-columns: repeat(2, 1fr); } }
      `}</style>
    </section>
  );
}

// ── Customs section ────────────────────────────────────────────────────────

function useCountUp(target, run, duration = 900, delay = 0) {
  const [val, setVal] = useStateH(0);
  useEffectH(() => {
    if (!run) return;
    let raf, t0;
    const id = setTimeout(() => {
      const tick = (t) => {
        if (!t0) t0 = t;
        const p = Math.min(1, (t - t0) / duration);
        const eased = 1 - Math.pow(1 - p, 3);
        setVal(Math.round(target * eased));
        if (p < 1) raf = requestAnimationFrame(tick);
      };
      raf = requestAnimationFrame(tick);
    }, delay);
    return () => { clearTimeout(id); cancelAnimationFrame(raf); };
  }, [run, target, duration, delay]);
  return val;
}

const STAGES = [
  { n: "01", value: "CIF",  label: "CIF value",            sub: "The base",
    body: "China factory price + sea freight + insurance to Beirut port. This is what customs uses to calculate everything else." },
  { n: "02", value: "5%",   label: "Import duty",          sub: "of CIF",
    body: "Lebanese customs duty on new vehicles from China. Same rate as the EU–Lebanon agreement." },
  { n: "03", value: "11%",  label: "VAT (TVA)",            sub: "on CIF + duty",
    body: "Standard Lebanese value-added tax applied after duty is calculated." },
  { n: "04", value: "$700–$1,500", label: "Registration & plates", sub: "Mécanique + naf3a",
    body: "Includes mécanique inspection, plates, naf3a (engine fee), and notary. Final amount depends on engine size and EV vs ICE." },
  { n: "05", value: "5%",   label: "Icon One fee",         sub: "of factory price",
    body: "Our flat fee covers sourcing, factory audit, China handling, shipping coordination, customs clearance, registration, and plates delivery to your door." },
];

function StageCard({ s, i, run }) {
  return (
    <div className="cust-stage" style={{ transitionDelay: (i * 90) + "ms", opacity: run ? 1 : 0, transform: run ? "none" : "translateY(12px)" }}>
      <div className="cust-stage-num mono">{s.n}</div>
      <div className="serif cust-stage-value">{s.value}</div>
      <div className="mono cust-stage-sub">{s.sub}</div>
      <div className="cust-stage-label">{s.label}</div>
      <div className="cust-stage-body">{s.body}</div>
    </div>
  );
}

const EX_LINES = [
  { k: "China factory price (ex-works Guangzhou)", v: 16500 },
  { k: "Sea freight + insurance to Beirut",        v: 1800 },
];
const EX_DERIVED = [
  { k: "CIF value",                                 v: 18300, bold: true },
  { k: "Lebanese import duty (5% of CIF)",          v: 915 },
  { k: "VAT (11% on CIF + duty)",                   v: 2114 },
  { k: "Registration, plates, mécanique, notary",   v: 1200 },
  { k: "Icon One service fee (5% of factory)",      v: 825 },
];
const EX_TOTAL = 23354;
const EX_DEALER = 42000;

function CustomsSection() {
  const ref = useRefH(null);
  const [run, setRun] = useStateH(false);
  useEffectH(() => {
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver((es) => es.forEach((e) => { if (e.isIntersecting) { setRun(true); io.disconnect(); } }), { threshold: 0.2 });
    io.observe(el); return () => io.disconnect();
  }, []);

  const total   = useCountUp(EX_TOTAL,   run, 1100, 700);
  const dealer  = useCountUp(EX_DEALER,  run, 1000, 800);
  const savings = useCountUp(EX_DEALER - EX_TOTAL, run, 1200, 1000);

  return (
    <section id="customs" ref={ref} style={{ padding: "120px 0", borderTop: "1px solid var(--line)", background: "var(--ink-0)" }}>
      <div className="wrap">
        <SectionLabel eyebrow="Customs breakdown · May 2026" title="Where the money actually goes." sub="Most importers hide this. We don't. Here's exactly what Lebanese customs charges on a new China-sourced vehicle." lang="en" />

        {/* Stages flow */}
        <div className="cust-flow" style={{ position: "relative" }}>
          <div className="cust-line-bg" aria-hidden="true">
            <div className="cust-line-fill" style={{ transform: run ? "scaleX(1)" : "scaleX(0)" }} />
          </div>
          <div className="cust-stages">
            {STAGES.map((s, i) => <StageCard key={i} s={s} i={i} run={run} />)}
          </div>
        </div>

        {/* Worked example */}
        <div className="cust-example" data-reveal>
          <div className="cust-example-head">
            <span className="mono" style={{ fontSize: 10, letterSpacing: ".18em", color: "var(--amber)", textTransform: "uppercase" }}>Worked example</span>
            <h3 className="serif" style={{ margin: "8px 0 0", fontSize: "clamp(24px, 3vw, 32px)", color: "var(--bone)", letterSpacing: "-.01em" }}>BYD Atto 3 Standard</h3>
          </div>

          <div className="cust-example-body">
            {EX_LINES.map((l, i) => (
              <ExRow key={"a" + i} k={l.k} v={l.v} run={run} delay={300 + i * 130} />
            ))}
            <div className="cust-example-rule" />
            {EX_DERIVED.map((l, i) => (
              <ExRow key={"b" + i} k={l.k} v={l.v} run={run} delay={620 + i * 130} bold={l.bold} />
            ))}
            <div className="cust-example-rule strong" />
            <div className="cust-example-total" style={{ opacity: run ? 1 : 0, transition: "opacity .6s ease 1.4s" }}>
              <div>
                <div className="mono" style={{ fontSize: 10, letterSpacing: ".18em", color: "var(--mute-2)", textTransform: "uppercase" }}>Total landed in Beirut · with plates</div>
                <div className="serif" style={{ fontSize: "clamp(34px, 4.5vw, 52px)", color: "var(--bone)", letterSpacing: "-.02em", lineHeight: 1.05, marginTop: 6 }}>
                  ${total.toLocaleString("en-US")}
                </div>
              </div>
              <div className="cust-example-compare">
                <div style={{ fontSize: 12, color: "var(--mute-2)" }}>Lebanese BYD dealer price</div>
                <div className="num mono" style={{ fontSize: 16, color: "var(--mute)", textDecoration: "line-through", marginTop: 4 }}>
                  ~${dealer.toLocaleString("en-US")}
                </div>
                <div style={{ marginTop: 14, color: "var(--green)" }}>
                  <div className="mono" style={{ fontSize: 10, letterSpacing: ".18em", textTransform: "uppercase", opacity: .85 }}>You save</div>
                  <div className="serif" style={{ fontSize: "clamp(22px, 2.6vw, 30px)", letterSpacing: "-.01em", lineHeight: 1.1, marginTop: 4 }}>
                    ${savings.toLocaleString("en-US")} <span style={{ fontStyle: "italic", color: "rgba(109,190,126,.7)", fontSize: ".7em" }}>· 44%</span>
                  </div>
                </div>
              </div>
            </div>

            <div className="cust-example-cta">
              <a href={WA.beirut} target="_blank" rel="noopener" className="btn btn-wa" style={{ flex: "1 1 280px" }}>
                <WAIcon /> Get my exact landed quote on WhatsApp
              </a>
              <a href="#how" className="cust-ghost-link">
                How does your fee structure work? <span className="flip">→</span>
              </a>
            </div>
          </div>
        </div>
      </div>

      <style>{`
        .cust-flow { margin-bottom: 80px; }
        .cust-line-bg {
          position: absolute; left: 0; right: 0; top: 38px; height: 1px;
          background: var(--line); display: none;
        }
        @media (min-width: 980px) { .cust-line-bg { display: block; } }
        .cust-line-fill {
          height: 100%;
          background: linear-gradient(90deg, var(--amber), #ffb27a);
          transform-origin: left center; transform: scaleX(0);
          transition: transform 1.1s cubic-bezier(.4,.05,.2,1) .15s;
        }
        [dir="rtl"] .cust-line-fill { transform-origin: right center; }
        .cust-stages {
          display: grid; grid-template-columns: 1fr; gap: 24px;
          position: relative; z-index: 1;
        }
        @media (min-width: 720px)  { .cust-stages { grid-template-columns: repeat(2, 1fr); } }
        @media (min-width: 980px)  { .cust-stages { grid-template-columns: repeat(5, 1fr); gap: 16px; } }
        .cust-stage {
          padding: 0 16px 4px; transition: opacity .6s ease, transform .6s cubic-bezier(.2,.7,.2,1);
        }
        .cust-stage-num {
          display: inline-flex; align-items: center; justify-content: center;
          width: 32px; height: 32px; border-radius: 50%;
          background: var(--ink-0); border: 1px solid var(--line-2);
          font-size: 11px; color: var(--amber); letterSpacing: .05em;
          margin-bottom: 16px;
        }
        @media (min-width: 980px) { .cust-stage-num { background: var(--ink-0); } }
        .cust-stage-value { font-size: 32px; line-height: 1; color: var(--bone); letter-spacing: -.015em; font-style: italic; }
        .cust-stage-sub   { font-size: 10px; letter-spacing: .15em; text-transform: uppercase; color: var(--mute-2); margin-top: 6px; }
        .cust-stage-label { font-size: 14px; color: var(--bone); margin-top: 14px; font-weight: 500; }
        .cust-stage-body  { font-size: 13px; line-height: 1.55; color: var(--mute); margin-top: 6px; }

        .cust-example {
          border: 1px solid rgba(232,115,46,.35);
          border-radius: 4px;
          background: linear-gradient(180deg, rgba(232,115,46,.03) 0%, transparent 100%), var(--ink-1);
          padding: clamp(28px, 4vw, 48px);
        }
        .cust-example-head { margin-bottom: 32px; padding-bottom: 24px; border-bottom: 1px solid var(--line); }
        .cust-example-rule { height: 1px; background: var(--line); margin: 12px 0; }
        .cust-example-rule.strong { background: var(--line-2); margin: 16px 0; }
        .cust-example-total {
          display: grid; grid-template-columns: 1fr; gap: 28px;
          padding: 24px 0 8px;
        }
        @media (min-width: 720px) { .cust-example-total { grid-template-columns: 1.2fr 1fr; gap: 48px; } }
        .cust-example-compare { padding-inline-start: 24px; border-inline-start: 1px solid var(--line); }
        @media (max-width: 719px) { .cust-example-compare { padding-inline-start: 0; border-inline-start: 0; padding-top: 24px; border-top: 1px solid var(--line); } }
        .cust-example-cta { display: flex; flex-wrap: wrap; gap: 16px 24px; align-items: center; margin-top: 36px; padding-top: 28px; border-top: 1px solid var(--line); }
        .cust-ghost-link { color: var(--mute); text-decoration: none; font-size: 14px; border-bottom: 1px solid var(--line-2); padding-bottom: 3px; transition: color .2s, border-color .2s; }
        .cust-ghost-link:hover { color: var(--bone); border-bottom-color: var(--amber); }

        .ex-row { display: flex; align-items: baseline; justify-content: space-between; padding: 10px 0; gap: 16px; transition: opacity .55s ease, transform .55s cubic-bezier(.2,.7,.2,1); }
        .ex-row .ex-k { font-size: 14px; color: var(--mute); }
        .ex-row .ex-v { font-family: var(--mono); font-size: 14px; color: var(--bone); font-variant-numeric: tabular-nums; }
        .ex-row.bold .ex-k { color: var(--bone); font-weight: 500; }
        .ex-row.bold .ex-v { color: var(--bone); font-weight: 500; }
      `}</style>
    </section>
  );
}

function ExRow({ k, v, run, delay, bold }) {
  const val = useCountUp(v, run, 700, delay);
  return (
    <div className={"ex-row" + (bold ? " bold" : "")}
         style={{ opacity: run ? 1 : 0, transform: run ? "none" : "translateY(6px)", transitionDelay: delay + "ms" }}>
      <span className="ex-k">{k}</span>
      <span className="ex-v">${val.toLocaleString("en-US")}</span>
    </div>
  );
}

window.HonestSection = HonestSection;
window.CustomsSection = CustomsSection;
