/* global React */
const { useState: useStateS, useRef: useRefS } = React;

// ---------------- About ----------------
function About({ t }) {
  return (
    <section id="about" className="relative py-20 md:py-28 bg-white">
      <div className="max-w-7xl mx-auto px-5 md:px-8">
        <SectionEyebrow text={t.about.eyebrow} />
        <div className="mt-3 grid lg:grid-cols-[1.1fr_1fr] gap-12 lg:gap-16 items-start">
          <div>
            <h2 className="text-[34px] md:text-[46px] font-extrabold tracking-tight text-[#0A2540] leading-[1.05]" style={{ textWrap: "balance" }}>
              {t.about.title}
            </h2>
            <p className="mt-5 text-[17px] md:text-[18.5px] text-[#0A2540]/75 leading-[1.6]" style={{ textWrap: "pretty" }}>
              {t.about.lead}
            </p>
            <p className="mt-4 text-[15.5px] text-[#0A2540]/65 leading-[1.7]" style={{ textWrap: "pretty" }}>
              {t.about.p1}
            </p>
            <p className="mt-3 text-[15.5px] text-[#0A2540]/65 leading-[1.7]" style={{ textWrap: "pretty" }}>
              {t.about.p2}
            </p>

            <div className="mt-8 grid sm:grid-cols-2 gap-4">
              <FeatureCard
                icon={
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"><path d="M12 3v18M5 10l7-7 7 7M6 21h12" strokeLinecap="round" strokeLinejoin="round" /></svg>
                }
                title={t.about.featureA} desc={t.about.featureADesc} />
              <FeatureCard
                icon={
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"><path d="M4 6h16M5 12h14M7 18h10" strokeLinecap="round" /></svg>
                }
                title={t.about.featureB} desc={t.about.featureBDesc} />
              <FeatureCard
                icon={
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"><circle cx="12" cy="12" r="3" /><path d="M12 2v3M12 19v3M2 12h3M19 12h3M4.93 4.93l2.12 2.12M16.95 16.95l2.12 2.12M4.93 19.07l2.12-2.12M16.95 7.05l2.12-2.12" strokeLinecap="round" /></svg>
                }
                title={t.about.featureC} desc={t.about.featureCDesc} />
              <FeatureCard
                icon={
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"><path d="M9 12l2 2 4-4M12 3l8 4v6c0 5-4 8-8 9-4-1-8-4-8-9V7l8-4z" strokeLinecap="round" strokeLinejoin="round" /></svg>
                }
                title={t.about.featureD} desc={t.about.featureDDesc} />
            </div>
          </div>

          {/* Right visual — cross-section illustration */}
          <div className="relative">
            <div className="relative rounded-[32px] overflow-hidden aspect-[4/5] shadow-[0_40px_80px_-30px_rgba(11,63,122,0.35)] ring-1 ring-white/60">
              {/* Sky → water gradient backdrop */}
              <div className="absolute inset-0 bg-gradient-to-b from-[#E8F4FC] via-[#CFE7F7] to-[#7AC4ED]" />

              {/* Subtle radial sun glow */}
              <div className="absolute top-[8%] right-[12%] w-40 h-40 bg-white/60 rounded-full blur-3xl" />

              {/* Soft drifting clouds */}
              <svg viewBox="0 0 400 200" className="absolute inset-x-0 top-[6%] w-full opacity-80" preserveAspectRatio="none" aria-hidden="true">
                <ellipse cx="80" cy="40" rx="55" ry="14" fill="white" opacity="0.55" />
                <ellipse cx="110" cy="48" rx="40" ry="10" fill="white" opacity="0.45" />
                <ellipse cx="290" cy="55" rx="70" ry="16" fill="white" opacity="0.5" />
                <ellipse cx="320" cy="62" rx="45" ry="11" fill="white" opacity="0.4" />
              </svg>

              {/* Faint horizon line between sky and water */}
              <div className="absolute inset-x-0 top-[52%] h-px bg-white/50" />

              {/* Underwater ripple highlights */}
              <svg viewBox="0 0 400 200" className="absolute inset-x-0 bottom-0 w-full h-[40%] opacity-50" preserveAspectRatio="none" aria-hidden="true">
                <path d="M0,40 Q100,20 200,40 T400,40" stroke="white" strokeWidth="1" fill="none" opacity="0.6" />
                <path d="M0,80 Q100,60 200,80 T400,80" stroke="white" strokeWidth="1" fill="none" opacity="0.4" />
                <path d="M0,130 Q100,110 200,130 T400,130" stroke="white" strokeWidth="1" fill="none" opacity="0.3" />
              </svg>

              {/* Cross-section illustration (transparent) */}
              <img
                src="assets/mountain2-clean.webp"
                alt="Mountain water source cross-section"
                width="1146" height="681" loading="lazy" decoding="async"
                className="absolute inset-x-0 top-[10%] mx-auto w-[94%] object-contain drop-shadow-[0_25px_30px_rgba(11,63,122,0.25)]" />

              {/* Depth scale on the side */}
              <div className="absolute left-5 top-5 bottom-5 w-[3px] bg-[#0A2540]/15 rounded-full">
                <div className="absolute -left-1 top-0 w-2.5 h-2.5 bg-[#0A2540] rounded-full" />
                <div className="absolute -left-1 top-1/4 w-2 h-2 bg-[#0A2540]/40 rounded-full" />
                <div className="absolute -left-1 top-1/2 w-2 h-2 bg-[#0A2540]/40 rounded-full" />
                <div className="absolute -left-1 top-3/4 w-2 h-2 bg-[#0A2540]/40 rounded-full" />
                <div className="absolute -left-1.5 bottom-0 w-3.5 h-3.5 bg-[#0066CC] rounded-full ring-4 ring-white/70" />
              </div>
              <div className="absolute left-12 top-5 text-[11px] font-semibold text-[#0A2540]/70 tracking-wider uppercase">0 m</div>
              <div className="absolute left-12 bottom-5 text-[12.5px] font-extrabold text-[#0066CC] tracking-wider">350 m</div>

              {/* Floating callout */}
              <div className="absolute right-5 bottom-5 bg-white/95 backdrop-blur rounded-2xl p-4 max-w-[220px] shadow-[0_10px_30px_-10px_rgba(11,63,122,0.4)]">
                <div className="text-[11px] font-semibold text-[#0066CC] uppercase tracking-wider">Fergana</div>
                <div className="text-[15px] font-bold text-[#0A2540] mt-0.5 leading-tight">Tien Shan foothills</div>
                <div className="text-[12.5px] text-[#0A2540]/65 mt-1">41.39°N · 71.78°E</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>);

}

function SectionEyebrow({ text }) {
  return (
    <div className="inline-flex items-center gap-2">
      <span className="w-6 h-px bg-[#0066CC]" />
      <span className="text-[12.5px] font-semibold uppercase tracking-[0.18em] text-[#0066CC]">{text}</span>
    </div>);

}

function FeatureCard({ icon, title, desc }) {
  return (
    <div className="p-5 rounded-2xl bg-[#F4FAFE] border border-[#E6F4FB]">
      <div className="w-10 h-10 rounded-xl bg-white text-[#0066CC] flex items-center justify-center shadow-sm">
        <span className="w-5 h-5 block">{icon}</span>
      </div>
      <div className="mt-3 text-[15.5px] font-bold text-[#0A2540]">{title}</div>
      <div className="mt-1 text-[13.5px] text-[#0A2540]/65 leading-relaxed">{desc}</div>
    </div>);

}

// ---------------- Process (7 filtration steps) ----------------
function Process({ t }) {
  return (
    <section className="relative py-20 md:py-28 bg-gradient-to-b from-[#F4FAFE] to-white">
      <div className="max-w-7xl mx-auto px-5 md:px-8">
        <div className="max-w-3xl">
          <SectionEyebrow text={t.process.eyebrow} />
          <h2 className="mt-3 text-[34px] md:text-[46px] font-extrabold tracking-tight text-[#0A2540] leading-[1.05]" style={{ textWrap: "balance" }}>
            {t.process.title}
          </h2>
          <p className="mt-4 text-[17px] text-[#0A2540]/70 max-w-[640px]" style={{ textWrap: "pretty" }}>
            {t.process.sub}
          </p>
        </div>

        <div className="mt-12 relative">
          {/* Connecting line */}
          <div className="hidden lg:block absolute top-[34px] left-[5%] right-[5%] h-[2px] bg-gradient-to-r from-[#7AC4ED] via-[#0066CC] to-[#0B3F7A] opacity-30" />

          <ol className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-7 gap-x-4 gap-y-8">
            {t.process.steps.map((s, i) =>
            <li key={i} className="relative">
                <div className="flex flex-col items-center text-center">
                  <div className="relative w-[70px] h-[70px] rounded-full bg-white border-2 border-[#E6F4FB] shadow-[0_8px_20px_-8px_rgba(11,63,122,0.25)] flex items-center justify-center">
                    <span className="text-[22px] font-extrabold text-[#0066CC]">{i + 1}</span>
                    <span className="absolute -top-1 -right-1 w-6 h-6 rounded-full bg-[#0066CC] text-white text-[10px] font-bold flex items-center justify-center">
                      <svg viewBox="0 0 12 12" className="w-3 h-3" fill="currentColor"><path d="M6 1c0 0-4 5-4 7a4 4 0 0 0 8 0c0-2-4-7-4-7z" /></svg>
                    </span>
                  </div>
                  <div className="mt-4 text-[13.5px] font-bold text-[#0A2540]">{s.t}</div>
                  <div className="mt-1 text-[12.5px] text-[#0A2540]/60 leading-snug max-w-[160px]">{s.d}</div>
                </div>
              </li>
            )}
          </ol>
        </div>
      </div>
    </section>);

}

// ---------------- Products ----------------
function Products({ t }) {
  const sizes = t.products.sizes;
  // Map images by index — order matches the array
  const imgs = [
  { src: "assets/safo-033.webp",        w: 542, h: 542 }, // 0.33L
  { src: "assets/safo-05.webp",         w: 592, h: 735 }, // 0.5L
  { src: "assets/safo-1.webp",          w: 463, h: 882 }, // 1L
  { src: "assets/bottle-5l-clean.webp", w: 800, h: 640 }, // 5L
  { src: "assets/bottle-10l-clean.webp",w: 800, h: 640 }, // 10L
  { src: "assets/bottle-19l-clean.webp",w: 1200,h: 1200 } // 18.9L
  ];
  // All images now have their own transparent PNG
  const bgPositions = [null, null, null, null, null, null];

  return (
    <section id="products" className="relative py-20 md:py-28 bg-white">
      <div className="max-w-7xl mx-auto px-5 md:px-8">
        <div className="flex flex-wrap items-end justify-between gap-6">
          <div className="max-w-2xl">
            <SectionEyebrow text={t.products.eyebrow} />
            <h2 className="mt-3 text-[34px] md:text-[46px] font-extrabold tracking-tight text-[#0A2540] leading-[1.05]" style={{ textWrap: "balance" }}>
              {t.products.title}
            </h2>
            <p className="mt-4 text-[17px] text-[#0A2540]/70" style={{ textWrap: "pretty" }}>
              {t.products.sub}
            </p>
          </div>
        </div>

        <div className="mt-12 grid sm:grid-cols-2 lg:grid-cols-3 gap-5 md:gap-6">
          {sizes.map((s, i) => {
            const isLarge = i >= 3;
            const popular = i === 4; // 10L popular
            const freeDelivery = i === 5; // 18.9L
            return (
              <article key={i} className={
              "group relative rounded-3xl overflow-hidden border transition-all duration-300 " + (
              popular ? "border-[#0066CC] bg-gradient-to-b from-[#E6F4FB] to-white shadow-[0_20px_50px_-20px_rgba(0,102,204,0.35)]" :
              "border-[#E6F4FB] bg-[#F4FAFE] hover:-translate-y-1 hover:shadow-[0_20px_40px_-20px_rgba(11,63,122,0.25)]")
              }>
                {/* Badges */}
                <div className="absolute top-4 left-4 right-4 flex items-start justify-between z-10">
                  <div className="text-[11px] font-bold uppercase tracking-[0.14em] text-[#0066CC]/70">SAFO</div>
                  <div className="flex flex-col items-end gap-1.5">
                    {popular &&
                    <span className="px-2.5 py-1 bg-[#0066CC] text-white text-[10.5px] font-bold rounded-full uppercase tracking-wide">
                        {t.products.pop}
                      </span>
                    }
                    {freeDelivery &&
                    <span className="px-2.5 py-1 bg-[#0A2540] text-white text-[10.5px] font-bold rounded-full uppercase tracking-wide">
                        {t.products.free}
                      </span>
                    }
                  </div>
                </div>

                {/* Image */}
                <div className="relative h-[280px] flex items-end justify-center pt-10 pb-2 overflow-hidden">
                  {bgPositions[i] ?
                  <div
                    className="w-full h-full"
                    style={{
                      backgroundImage: `url(${imgs[i].src})`,
                      backgroundSize: "220% auto",
                      backgroundPosition: bgPositions[i],
                      backgroundRepeat: "no-repeat"
                    }} /> :


                  <img src={imgs[i].src} alt={`Safo ${s.v}`} width={imgs[i].w} height={imgs[i].h} loading="lazy" decoding="async" className="h-full w-auto object-contain transition-transform duration-500 group-hover:scale-105"
                  style={{ filter: "drop-shadow(0 14px 18px rgba(11,63,122,0.18))" }} />
                  }
                </div>

                {/* Bottom info */}
                <div className="relative px-6 pb-6 pt-3 bg-white/80 backdrop-blur-sm border-t border-[#E6F4FB]">
                  <div className="flex items-baseline justify-between gap-3">
                    <div className="text-[26px] font-extrabold text-[#0A2540] tracking-tight">{s.v}</div>
                    <div className="text-[13px] font-semibold text-[#0066CC]">{s.t}</div>
                  </div>
                  <div className="mt-1 text-[13.5px] text-[#0A2540]/65 leading-snug">{s.d}</div>
                </div>
              </article>);

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

}

// ---------------- Benefits ----------------
function Benefits({ t }) {
  const icons = [
  // hydration
  <svg key="i0" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"><path d="M12 3c0 0-7 8-7 13a7 7 0 0 0 14 0c0-5-7-13-7-13z" strokeLinejoin="round" /></svg>,
  // heart
  <svg key="i1" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78L12 21.23l8.84-8.84a5.5 5.5 0 0 0 0-7.78z" strokeLinejoin="round" /></svg>,
  // digestion / leaf
  <svg key="i2" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"><path d="M11 20A7 7 0 0 1 4 13a7 7 0 0 1 7-7c0 7 5 14 11 14a7 7 0 0 1-11 0z" strokeLinejoin="round" /></svg>,
  // bones
  <svg key="i3" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"><path d="M17 10a3 3 0 1 1 3-3 3 3 0 0 1-1 2.2L11.2 17a3 3 0 0 1-2.2 1 3 3 0 1 1 3-3l6.8-6.8A3 3 0 0 1 17 10z" strokeLinejoin="round" /></svg>,
  // detox
  <svg key="i4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"><path d="M12 22a8 8 0 0 0 8-8c0-3.5-3-7-8-12-5 5-8 8.5-8 12a8 8 0 0 0 8 8z" strokeLinejoin="round" /><path d="M8 14c1 2 3 3 5 2" strokeLinecap="round" /></svg>,
  // immunity
  <svg key="i5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"><path d="M12 3l8 4v6c0 5-4 8-8 9-4-1-8-4-8-9V7l8-4z" strokeLinejoin="round" /><path d="M9 12l2 2 4-4" strokeLinecap="round" strokeLinejoin="round" /></svg>];


  return (
    <section id="benefits" className="relative py-20 md:py-28 bg-[#0A2540] text-white overflow-hidden">
      {/* Soft blue glow */}
      <div className="absolute -top-32 -right-20 w-[420px] h-[420px] rounded-full bg-[#0066CC]/30 blur-[100px] pointer-events-none" />
      <div className="absolute -bottom-32 -left-20 w-[420px] h-[420px] rounded-full bg-[#7AC4ED]/20 blur-[100px] pointer-events-none" />

      <div className="relative max-w-7xl mx-auto px-5 md:px-8">
        <div className="max-w-3xl">
          <div className="inline-flex items-center gap-2">
            <span className="w-6 h-px bg-[#7AC4ED]" />
            <span className="text-[12.5px] font-semibold uppercase tracking-[0.18em] text-[#7AC4ED]">{t.benefits.eyebrow}</span>
          </div>
          <h2 className="mt-3 text-[34px] md:text-[46px] font-extrabold tracking-tight leading-[1.05]" style={{ textWrap: "balance" }}>
            {t.benefits.title}
          </h2>
          <p className="mt-4 text-[17px] text-white/70" style={{ textWrap: "pretty" }}>
            {t.benefits.sub}
          </p>
        </div>

        <div className="mt-12 grid sm:grid-cols-2 lg:grid-cols-3 gap-4 md:gap-5">
          {t.benefits.items.map((it, i) =>
          <div key={i} className="group p-6 rounded-2xl bg-white/[0.04] hover:bg-white/[0.07] border border-white/10 transition-colors">
              <div className="w-11 h-11 rounded-xl bg-[#7AC4ED]/15 text-[#7AC4ED] flex items-center justify-center">
                <span className="w-5 h-5 block">{icons[i]}</span>
              </div>
              <div className="mt-4 text-[17px] font-bold">{it.t}</div>
              <div className="mt-1.5 text-[14px] text-white/65 leading-[1.6]">{it.d}</div>
            </div>
          )}
        </div>
      </div>
    </section>);

}

// ---------------- Delivery ----------------
function Delivery({ t }) {
  return (
    <section id="delivery" className="relative py-20 md:py-28 bg-white overflow-hidden">
      <div className="max-w-7xl mx-auto px-5 md:px-8">
        <div className="relative rounded-[32px] overflow-hidden bg-gradient-to-br from-[#0066CC] via-[#1E5FA8] to-[#0B3F7A] text-white">
          {/* Decor */}
          <div className="absolute inset-0 opacity-30">
            <svg viewBox="0 0 1440 600" preserveAspectRatio="none" className="w-full h-full" aria-hidden="true">
              <path fill="rgba(255,255,255,0.1)" d="M0,420 C240,520 480,360 720,420 C960,480 1200,360 1440,440 L1440,600 L0,600 Z" />
              <path fill="rgba(255,255,255,0.06)" d="M0,460 C240,560 480,400 720,460 C960,520 1200,400 1440,480 L1440,600 L0,600 Z" />
            </svg>
          </div>
          <div className="absolute -top-20 -right-20 w-[400px] h-[400px] bg-white/10 rounded-full blur-[80px]" />

          <div className="relative grid md:grid-cols-[1.3fr_1fr] gap-8 md:gap-10 p-8 md:p-14 items-center">
            <div>
              <div className="inline-flex items-center gap-2">
                <span className="w-6 h-px bg-white/60" />
                <span className="text-[12.5px] font-semibold uppercase tracking-[0.18em] text-white/80">{t.delivery.eyebrow}</span>
              </div>
              <h2 className="mt-3 text-[32px] md:text-[44px] font-extrabold tracking-tight leading-[1.05]" style={{ textWrap: "balance" }}>
                {t.delivery.title}
              </h2>
              <p className="mt-4 text-[16.5px] text-white/80 max-w-[520px]" style={{ textWrap: "pretty" }}>
                {t.delivery.sub}
              </p>
              <ul className="mt-6 space-y-2.5">
                {t.delivery.points.map((p, i) =>
                <li key={i} className="flex items-center gap-3 text-[15px]">
                    <span className="w-6 h-6 rounded-full bg-white/15 flex items-center justify-center flex-shrink-0">
                      <svg viewBox="0 0 24 24" className="w-3.5 h-3.5" fill="none" stroke="white" strokeWidth="3">
                        <path d="M5 12l5 5L20 7" strokeLinecap="round" strokeLinejoin="round" />
                      </svg>
                    </span>
                    <span className="text-white/90">{p}</span>
                  </li>
                )}
              </ul>
              <div className="mt-8 flex flex-wrap gap-3">
                <a href="https://t.me/Safosuvbot" target="_blank" rel="noopener"
                className="inline-flex items-center gap-2.5 px-6 py-3.5 bg-white text-[#0066CC] text-[15px] font-bold rounded-full hover:bg-[#F4FAFE] transition-colors">
                  <svg viewBox="0 0 24 24" className="w-4 h-4" fill="currentColor"><path d="M22.05 3.55L2.4 11.2c-1.34.52-1.33 1.27-.24 1.6l5 1.55 1.92 5.83c.24.65.12.91.8.91.53 0 .76-.24 1.05-.53 0 0 2.05-1.99 3.15-3.06l5.18 3.83c.95.53 1.64.26 1.88-.88l3.4-16.05c.34-1.4-.55-2.03-1.49-1.65z" /></svg>
                  {t.delivery.cta}
                </a>
                <a href="tel:+998886661111"
                className="inline-flex items-center gap-2.5 px-6 py-3.5 bg-white/10 hover:bg-white/15 border border-white/20 text-white text-[15px] font-semibold rounded-full transition-colors">
                  +998 88 666-11-11
                </a>
              </div>
            </div>

            <div className="relative h-[300px] md:h-[420px]">
              <img src="assets/bottle-19l-clean.webp" alt="Safo 18.9L" width="1200" height="1200" loading="lazy" decoding="async" className="absolute inset-0 w-full h-full object-contain drop-shadow-[0_30px_40px_rgba(0,0,0,0.3)]" />
              {/* Floating tag */}
              <div className="hidden md:block absolute top-4 left-4 md:left-0 bg-white text-[#0A2540] rounded-2xl px-4 py-3 shadow-lg">
                <div className="text-[10.5px] font-bold uppercase tracking-wider text-[#0066CC]">18.9 L</div>
                <div className="text-[13.5px] font-bold leading-tight">{t.products.free}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>);

}

// ---------------- Cooperation form ----------------
function Cooperation({ t, lang, tgConfig }) {
  const PHONE_PREFIX = "+998 ";
  const [form, setForm] = useStateS({ name: "", phone: PHONE_PREFIX, message: "" });
  const [errors, setErrors] = useStateS({});
  const [status, setStatus] = useStateS("idle"); // idle | sending | success | error
  const formRef = useRefS(null);

  const update = (k) => (e) => {
    let v = e.target.value;
    if (k === "phone" && !v.startsWith(PHONE_PREFIX)) {
      // strip any partial prefix attempts and re-apply
      v = PHONE_PREFIX + v.replace(/^\+?9?9?8?\s*/, "");
    }
    setForm((f) => ({ ...f, [k]: v }));
    if (errors[k]) setErrors((er) => ({ ...er, [k]: undefined }));
  };

  const onPhoneFocus = (e) => {
    if (!e.target.value) {
      setForm((f) => ({ ...f, phone: PHONE_PREFIX }));
    }
    // place caret at the end
    requestAnimationFrame(() => {
      const el = e.target;
      const end = el.value.length;
      try {el.setSelectionRange(end, end);} catch (_) {}
    });
  };

  const validate = () => {
    const e = {};
    if (!form.name.trim()) e.name = t.coop.required;
    if (form.phone.trim() === PHONE_PREFIX.trim() || !form.phone.trim()) e.phone = t.coop.required;else
    if (!/^\+998\s*\d{6,}/.test(form.phone.replace(/[\s\-()]/g, ""))) e.phone = t.coop.invalidPhone;
    return e;
  };

  const onSubmit = async (ev) => {
    ev.preventDefault();
    const e = validate();
    if (Object.keys(e).length) {setErrors(e);return;}
    setStatus("sending");

    const text =
    "🆕 *Safo — Hamkorlik so'rovi / Запрос на сотрудничество*\n" +
    `\n👤 *${t.coop.name}:* ${form.name}` +
    `\n📞 *${t.coop.phone}:* ${form.phone}` + (
    form.message ? `\n💬 *${t.coop.message}:* ${form.message}` : "") +
    `\n🌐 Lang: ${lang.toUpperCase()}` +
    `\n⏱ ${new Date().toLocaleString()}`;

    try {
      // If bot token is configured, send directly to Telegram Bot API.
      // For production safety, route this through your own backend instead.
      if (tgConfig.BOT_TOKEN && !tgConfig.BOT_TOKEN.startsWith("REPLACE_")) {
        const res = await fetch(`https://api.telegram.org/bot${tgConfig.BOT_TOKEN}/sendMessage`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({
            chat_id: tgConfig.CHAT_ID,
            text,
            parse_mode: "Markdown"
          })
        });
        if (!res.ok) throw new Error("bot");
      } else {
        // Demo path — just simulate latency so the UX is testable
        await new Promise((r) => setTimeout(r, 900));
        console.log("[Safo demo] Would send to Telegram:\n" + text);
      }
      setStatus("success");
      setForm({ name: "", phone: PHONE_PREFIX, message: "" });
    } catch (err) {
      console.error(err);
      setStatus("error");
    }
  };

  return (
    <section id="cooperation" className="relative py-20 md:py-28 bg-gradient-to-b from-white to-[#E6F4FB]">
      <div className="max-w-5xl mx-auto px-5 md:px-8">
        <div className="text-center max-w-2xl mx-auto">
          <SectionEyebrow text={t.coop.eyebrow} />
          <h2 className="mt-3 text-[34px] md:text-[46px] font-extrabold tracking-tight text-[#0A2540] leading-[1.05]" style={{ textWrap: "balance" }}>
            {t.coop.title}
          </h2>
          <p className="mt-4 text-[17px] text-[#0A2540]/70" style={{ textWrap: "pretty" }}>
            {t.coop.sub}
          </p>
        </div>

        <form ref={formRef} onSubmit={onSubmit} noValidate
        className="mt-10 md:mt-12 relative bg-white rounded-[28px] p-6 md:p-10 shadow-[0_30px_60px_-30px_rgba(11,63,122,0.3)] border border-[#E6F4FB]">
          {/* Decorative droplet */}
          <div className="hidden md:block absolute -top-8 -right-8 w-20 h-20 bg-[#0066CC] rounded-full opacity-10" />
          <div className="hidden md:block absolute -top-3 -right-3 w-10 h-10 bg-[#7AC4ED] rounded-full opacity-30" />

          <div className="grid md:grid-cols-2 gap-5">
            <Field label={t.coop.name} required error={errors.name}>
              <input
                type="text"
                value={form.name}
                onChange={update("name")}
                autoComplete="name"
                className={inputCls(errors.name)}
                placeholder={t.coop.name} />
              
            </Field>
            <Field label={t.coop.phone} required error={errors.phone}>
              <input
                type="tel"
                value={form.phone}
                onChange={update("phone")}
                onFocus={onPhoneFocus}
                inputMode="tel"
                autoComplete="tel"
                className={inputCls(errors.phone)}
                placeholder="+998 __ ___ __ __" />
              
            </Field>
          </div>
          <div className="mt-5">
            <Field label={t.coop.message}>
              <textarea
                value={form.message}
                onChange={update("message")}
                rows={4}
                className={inputCls() + " resize-none"}
                placeholder={t.coop.message} />
              
            </Field>
          </div>

          <div className="mt-7 flex flex-wrap items-center gap-4 justify-between">
            <button
              type="submit"
              disabled={status === "sending"}
              className="inline-flex items-center gap-2.5 px-7 py-3.5 bg-[#0066CC] hover:bg-[#0058B0] disabled:bg-[#0066CC]/60 disabled:cursor-not-allowed text-white text-[15px] font-bold rounded-full shadow-[0_8px_24px_-8px_rgba(0,102,204,0.6)] transition-colors">
              {status === "sending" ?
              <>
                  <svg className="w-4 h-4 animate-spin" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3"><circle cx="12" cy="12" r="10" opacity="0.25" /><path d="M22 12a10 10 0 0 1-10 10" strokeLinecap="round" /></svg>
                  {t.coop.sending}
                </> :

              <>
                  {t.coop.submit}
                  <svg viewBox="0 0 24 24" className="w-4 h-4" fill="currentColor"><path d="M22.05 3.55L2.4 11.2c-1.34.52-1.33 1.27-.24 1.6l5 1.55 1.92 5.83c.24.65.12.91.8.91.53 0 .76-.24 1.05-.53 0 0 2.05-1.99 3.15-3.06l5.18 3.83c.95.53 1.64.26 1.88-.88l3.4-16.05c.34-1.4-.55-2.03-1.49-1.65z" /></svg>
                </>
              }
            </button>

            {status === "success" &&
            <div className="flex items-center gap-2 px-4 py-2.5 bg-[#E8F7EF] text-[#1F7A4A] rounded-full text-[14px] font-semibold">
                <svg viewBox="0 0 24 24" className="w-4 h-4" fill="none" stroke="currentColor" strokeWidth="2.5"><path d="M5 13l4 4L19 7" strokeLinecap="round" strokeLinejoin="round" /></svg>
                {t.coop.success}
              </div>
            }
            {status === "error" &&
            <div className="flex items-center gap-2 px-4 py-2.5 bg-[#FEEBEC] text-[#B42318] rounded-full text-[14px] font-semibold">
                <svg viewBox="0 0 24 24" className="w-4 h-4" fill="none" stroke="currentColor" strokeWidth="2.5"><circle cx="12" cy="12" r="10" /><path d="M12 8v4M12 16h.01" strokeLinecap="round" /></svg>
                {t.coop.error}
              </div>
            }
          </div>
        </form>
      </div>
    </section>);

}

function Field({ label, required, error, children }) {
  return (
    <label className="block">
      <div className="text-[13px] font-semibold text-[#0A2540]/80 mb-1.5">
        {label}{required && <span className="text-[#0066CC] ml-0.5">*</span>}
      </div>
      {children}
      {error && <div className="mt-1 text-[12.5px] text-[#B42318]">{error}</div>}
    </label>);

}

function inputCls(err) {
  return (
    "w-full px-4 py-3 bg-[#F4FAFE] border rounded-xl text-[15px] text-[#0A2540] placeholder-[#0A2540]/35 " +
    "focus:outline-none focus:bg-white focus:ring-4 transition-all " + (
    err ?
    "border-[#FBA9AE] focus:ring-[#FEEBEC] focus:border-[#B42318]" :
    "border-[#E6F4FB] focus:ring-[#E6F4FB] focus:border-[#0066CC]"));

}

// ---------------- Contact ----------------
function Contact({ t }) {
  return (
    <section id="contact" className="relative py-20 md:py-28 bg-[#E6F4FB]">
      <div className="max-w-7xl mx-auto px-5 md:px-8">
        <div className="text-center max-w-2xl mx-auto">
          <SectionEyebrow text={t.contact.eyebrow} />
          <h2 className="mt-3 text-[34px] md:text-[46px] font-extrabold tracking-tight text-[#0A2540] leading-[1.05]" style={{ textWrap: "balance" }}>
            {t.contact.title}
          </h2>
        </div>

        <div className="mt-12 grid md:grid-cols-[1.2fr_1fr] gap-6">
          {/* Contact cards */}
          <div className="grid sm:grid-cols-2 gap-4">
            <ContactCard
              icon={<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" /><circle cx="12" cy="10" r="3" /></svg>}
              label={t.contact.addressLabel}
              text={t.contact.address} />
            
            <ContactCard
              icon={<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.37 1.9.72 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.35 1.85.59 2.81.72A2 2 0 0 1 22 16.92z" strokeLinejoin="round" /></svg>}
              label={t.contact.phoneLabel}
              text="+998 88 666-11-11"
              href="tel:+998886661111" />
            
            <ContactCard
              icon={<svg viewBox="0 0 24 24" fill="currentColor"><path d="M22.05 3.55L2.4 11.2c-1.34.52-1.33 1.27-.24 1.6l5 1.55 1.92 5.83c.24.65.12.91.8.91.53 0 .76-.24 1.05-.53 0 0 2.05-1.99 3.15-3.06l5.18 3.83c.95.53 1.64.26 1.88-.88l3.4-16.05c.34-1.4-.55-2.03-1.49-1.65z" /></svg>}
              label={t.contact.telegramLabel}
              text="@Safosuvbot"
              href="https://t.me/Safosuvbot" />
            
            <ContactCard
              icon={<svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2.16c3.2 0 3.58.01 4.85.07 1.17.05 1.8.25 2.23.42.56.22.96.48 1.38.9.42.42.68.82.9 1.38.16.43.36 1.06.42 2.23.06 1.27.07 1.65.07 4.85s-.01 3.58-.07 4.85c-.05 1.17-.25 1.8-.42 2.23-.22.56-.48.96-.9 1.38-.42.42-.82.68-1.38.9-.43.16-1.06.36-2.23.42-1.27.06-1.65.07-4.85.07s-3.58-.01-4.85-.07c-1.17-.05-1.8-.25-2.23-.42a3.7 3.7 0 0 1-1.38-.9 3.7 3.7 0 0 1-.9-1.38c-.16-.43-.36-1.06-.42-2.23-.06-1.27-.07-1.65-.07-4.85s.01-3.58.07-4.85c.05-1.17.25-1.8.42-2.23.22-.56.48-.96.9-1.38.42-.42.82-.68 1.38-.9.43-.16 1.06-.36 2.23-.42 1.27-.06 1.65-.07 4.85-.07M12 0C8.74 0 8.33.01 7.05.07 5.77.13 4.9.33 4.14.63 3.34.95 2.67 1.37 2.01 2.01 1.37 2.67.95 3.34.63 4.14.33 4.9.13 5.77.07 7.05.01 8.33 0 8.74 0 12s.01 3.67.07 4.95c.06 1.28.26 2.15.56 2.91.32.8.74 1.47 1.38 2.13.66.64 1.33 1.06 2.13 1.38.76.3 1.63.5 2.91.56 1.28.06 1.69.07 4.95.07s3.67-.01 4.95-.07c1.28-.06 2.15-.26 2.91-.56.8-.32 1.47-.74 2.13-1.38.64-.66 1.06-1.33 1.38-2.13.3-.76.5-1.63.56-2.91.06-1.28.07-1.69.07-4.95s-.01-3.67-.07-4.95c-.06-1.28-.26-2.15-.56-2.91A5.85 5.85 0 0 0 21.99 2.01 5.85 5.85 0 0 0 19.86.63C19.1.33 18.23.13 16.95.07 15.67.01 15.26 0 12 0zm0 5.84a6.16 6.16 0 1 0 0 12.32 6.16 6.16 0 0 0 0-12.32zm0 10.16A4 4 0 1 1 12 8a4 4 0 0 1 0 8zm6.4-11.85a1.44 1.44 0 1 0 0 2.88 1.44 1.44 0 0 0 0-2.88z" /></svg>}
              label={t.contact.instagramLabel}
              text="@safo_water"
              href="https://www.instagram.com/safo_water/" />
            
          </div>

          {/* Map embed */}
          <div className="rounded-3xl overflow-hidden bg-white border border-[#CFE7F7] shadow-[0_20px_50px_-20px_rgba(11,63,122,0.2)] h-[420px] md:h-auto">
            <iframe
              title="Safo Water · Fergana"
              src="https://maps.google.com/maps?q=Fergana%2C%20Uzbekistan%2C%20Saylgoh%2018%2F22&t=&z=13&ie=UTF8&iwloc=&output=embed"
              loading="lazy"
              className="w-full h-full block min-h-[300px]"
              style={{ border: 0 }}
              referrerPolicy="no-referrer-when-downgrade" />
            
          </div>
        </div>
      </div>
    </section>);

}

function ContactCard({ icon, label, text, href }) {
  const Tag = href ? "a" : "div";
  return (
    <Tag href={href} target={href && href.startsWith("http") ? "_blank" : undefined} rel={href && href.startsWith("http") ? "noopener" : undefined}
    className={"block p-5 rounded-2xl bg-white border border-white hover:border-[#7AC4ED] hover:shadow-[0_10px_30px_-15px_rgba(0,102,204,0.35)] transition-all " + (href ? "cursor-pointer" : "")}>
      <div className="w-10 h-10 rounded-xl bg-[#E6F4FB] text-[#0066CC] flex items-center justify-center">
        <span className="w-5 h-5 block">{icon}</span>
      </div>
      <div className="mt-3 text-[12px] font-semibold uppercase tracking-[0.14em] text-[#0A2540]/80">{label}</div>
      <div className="mt-1 text-[15.5px] font-bold text-[#0A2540] leading-snug">{text}</div>
    </Tag>);

}

// Make available globally for app.jsx
Object.assign(window, { About, Process, Products, Benefits, Delivery, Cooperation, Contact });