// sections1.jsx — Logo, Header, Hero, Metrics, HowItWorks, Advantages
const { useState, useEffect, useRef } = React;

// Scroll-reveal hook
function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const io = new IntersectionObserver((ents) => {
      ents.forEach((e) => { if (e.isIntersecting) { e.target.classList.add("in"); io.unobserve(e.target); } });
    }, { threshold: 0.12 });
    el.querySelectorAll(".reveal").forEach((n) => io.observe(el === n ? n : n));
    if (el.classList.contains("reveal")) io.observe(el);
    return () => io.disconnect();
  }, []);
  return ref;
}

// ---- Logo (original mostpay "bridge" mark — most = мост) ----
const Logo = ({ onDark = false, size = 30 }) => {
  const ink = onDark ? "var(--on-dark)" : "var(--ink)";
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 11, userSelect: "none" }}>
      <svg width={size * 1.32} height={size} viewBox="0 0 46 32" fill="none" style={{ flex: "0 0 auto", overflow: "visible" }}>
        {/* deck */}
        <path d="M3 23 H 43" stroke={ink} strokeWidth="3" strokeLinecap="round" opacity="0.32" />
        {/* suspension arc */}
        <path d="M3 23 C 13 6, 33 6, 43 23" stroke={ink} strokeWidth="3" strokeLinecap="round" fill="none" />
        {/* cables */}
        <path d="M13 12.5 V23 M23 8.5 V23 M33 12.5 V23" stroke={ink} strokeWidth="2" strokeLinecap="round" opacity="0.6" />
        {/* anchors: origin (ink) → destination (accent) */}
        <circle cx="3" cy="23" r="3.4" fill={ink} />
        <circle cx="43" cy="23" r="3.6" fill="var(--accent)" />
      </svg>
      <div style={{ fontFamily: "var(--font-head)", fontWeight: 800, fontSize: size * 0.66, letterSpacing: "-0.035em", color: ink, lineHeight: 1 }}>
        most<span style={{ color: "var(--accent)" }}>pay</span>
      </div>
    </div>
  );
};

// ---- Header (two-tier: dark utility strip + light sticky main bar) ----
function Header({ t, lang, setLang, onCta }) {
  const [scrolled, setScrolled] = useState(false);
  const [menu, setMenu] = useState(false);
  useEffect(() => {
    const h = () => setScrolled(window.scrollY > 8);
    window.addEventListener("scroll", h); h();
    return () => window.removeEventListener("scroll", h);
  }, []);
  const go = (id) => { setMenu(false); const el = document.getElementById(id); if (el) window.scrollTo({ top: el.offsetTop - 64, behavior: "smooth" }); };

  return (
    <React.Fragment>
      {/* top utility strip */}
      <div className="topbar">
        <div className="wrap" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", height: 40, gap: 16 }}>
          <span className="topbar-note">{t.topBarNote}</span>
          <div style={{ display: "flex", alignItems: "center", gap: 18 }}>
            <a href={`tel:${LINKS.phoneRaw}`} className="topbar-link"><Icon name="phone" size={14} />{LINKS.phone}</a>
            <a href={`mailto:${LINKS.email}`} className="topbar-link topbar-mail"><Icon name="mail" size={14} />{LINKS.email}</a>
            <span style={{ display: "flex", gap: 8 }}>
              <a href={LINKS.telegram} target="_blank" rel="noopener" className="topbar-soc" aria-label="Telegram"><Social name="telegram" size={15} /></a>
              <a href={LINKS.whatsapp} target="_blank" rel="noopener" className="topbar-soc" aria-label="WhatsApp"><Social name="whatsapp" size={15} /></a>
            </span>
          </div>
        </div>
      </div>

      {/* main bar */}
      <div className="mainbar" style={{ boxShadow: scrolled ? "0 1px 0 var(--line), 0 10px 30px rgba(20,16,10,.07)" : "none" }}>
        <div className="wrap" style={{ display: "flex", alignItems: "center", gap: 18, height: 64 }}>
          <a href="#top" onClick={(e) => { e.preventDefault(); window.scrollTo({ top: 0, behavior: "smooth" }); }}><Logo size={28} /></a>
          <nav className="desk-nav" style={{ display: "flex", gap: 2, marginLeft: 16 }}>
            {t.nav.map((n) => (
              <button key={n.id} onClick={() => go(n.id)} className="nav-link">{n.label}</button>
            ))}
          </nav>
          <div style={{ marginLeft: "auto", display: "flex", alignItems: "center", gap: 12 }}>
            <div style={{ display: "flex", background: "var(--surface)", border: "1px solid var(--line)", borderRadius: 999, padding: 3 }}>
              {["ru", "kg"].map((l) => (
                <button key={l} onClick={() => setLang(l)} style={{
                  border: "none", borderRadius: 999, padding: "6px 12px", fontSize: 12.5, fontWeight: 700,
                  fontFamily: "var(--font-mono)", letterSpacing: "0.04em",
                  background: lang === l ? "var(--ink)" : "transparent",
                  color: lang === l ? "var(--bg)" : "var(--muted)", transition: "all .15s",
                }}>{l.toUpperCase()}</button>
              ))}
            </div>
            <button className="btn btn-primary nav-cta" onClick={onCta} style={{ padding: "12px 20px", fontSize: 14.5 }}>{t.navCta}</button>
            <button className="burger" onClick={() => setMenu(!menu)} aria-label="menu" style={{
              display: "none", background: "var(--surface)", border: "1px solid var(--line)", borderRadius: 10, width: 42, height: 42, alignItems: "center", justifyContent: "center",
            }}>
              <svg width="20" height="20" viewBox="0 0 24 24" stroke="var(--ink)" strokeWidth="2" strokeLinecap="round">
                {menu ? <path d="M6 6l12 12M18 6L6 18" /> : <><path d="M4 7h16" /><path d="M4 12h16" /><path d="M4 17h16" /></>}
              </svg>
            </button>
          </div>
        </div>
        {menu && (
          <div className="wrap" style={{ paddingBottom: 16, display: "flex", flexDirection: "column", gap: 6 }}>
            {t.nav.map((n) => (
              <button key={n.id} onClick={() => go(n.id)} style={{ background: "var(--surface)", border: "1px solid var(--line)", borderRadius: 11, padding: "13px 16px", textAlign: "left", fontWeight: 600, color: "var(--ink)" }}>{n.label}</button>
            ))}
            <button className="btn btn-primary" onClick={() => { setMenu(false); onCta(); }} style={{ marginTop: 4 }}>{t.navCta}</button>
          </div>
        )}
      </div>

      <style>{`
        .topbar{ background:var(--dark); color:var(--on-dark); }
        .topbar-note{ font-family:var(--font-mono); font-size:12px; letter-spacing:.04em; color:var(--on-dark-muted); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
        .topbar-link{ display:inline-flex; align-items:center; gap:7px; font-size:13px; font-weight:600; color:var(--on-dark); transition:color .15s; }
        .topbar-link:hover{ color:#ff7a5f; }
        .topbar-link svg{ color:#ff7a5f; }
        .topbar-soc{ width:28px; height:28px; border-radius:7px; border:1px solid rgba(255,255,255,.16); display:grid; place-items:center; color:var(--on-dark); transition:all .15s; }
        .topbar-soc:hover{ background:var(--accent); border-color:var(--accent); }
        .mainbar{ position:sticky; top:0; z-index:50; background:var(--bg); transition:box-shadow .25s ease; }
        .nav-link{ background:none; border:none; color:var(--ink-soft); font-weight:600; font-size:14.5px; padding:8px 13px; border-radius:9px; transition:color .15s, background .15s; }
        .nav-link:hover{ background:var(--surface); color:var(--ink); }
        @media (max-width: 1040px){ .desk-nav{ display:none !important; } }
        @media (max-width: 760px){
          .nav-cta{ display:none !important; } .burger{ display:flex !important; }
          .topbar-mail{ display:none !important; } .topbar-note{ display:none !important; }
        }
      `}</style>
    </React.Fragment>
  );
}

// ---- Hero ----
function Hero({ t, lang, onCta, onCalc }) {
  const [vidOk, setVidOk] = useState(true);
  const ticker = ["🇰🇷 Корея", "🇨🇳 Китай", "🇦🇪 ОАЭ", "🇪🇺 Европа", "🇺🇸 США", "🇯🇵 Япония", "SWIFT", "🇰🇷 Корея", "🇨🇳 Китай", "🇦🇪 ОАЭ", "🇪🇺 Европа", "🇺🇸 США", "🇯🇵 Япония", "SWIFT"];
  return (
    <section id="top" className="hero-sec" style={{ position: "relative", overflow: "hidden", background: "var(--dark)", minHeight: "calc(100svh - 104px)", display: "flex", flexDirection: "column" }}>
      {/* background video */}
      <div style={{ position: "absolute", inset: 0, zIndex: 0 }}>
        <video autoPlay muted loop playsInline onError={() => setVidOk(false)}
          style={{ width: "100%", height: "100%", objectFit: "cover", opacity: vidOk ? 0.5 : 0, display: "block" }}>
          <source src="assets/hero-video.webm" type="video/webm" />
          <source src="assets/hero-video.mp4" type="video/mp4" />
        </video>
        {!vidOk && (
          <div style={{ position: "absolute", inset: 0, background: "repeating-linear-gradient(135deg, #1c1813 0 2px, transparent 2px 13px), radial-gradient(120% 100% at 70% 0%, #2a2118 0%, #16130f 60%)" }} />
        )}
        {/* readability gradients — keep right side lighter so video shows */}
        <div style={{ position: "absolute", inset: 0, background: "linear-gradient(90deg, rgba(12,10,8,.95) 0%, rgba(12,10,8,.78) 40%, rgba(12,10,8,.28) 78%, rgba(12,10,8,.12) 100%)" }} />
        <div style={{ position: "absolute", inset: 0, background: "linear-gradient(0deg, var(--dark) 0%, transparent 26%)" }} />
        <div style={{ position: "absolute", left: "-8%", top: "-10%", width: 560, height: 560, borderRadius: "50%", background: "radial-gradient(circle, rgba(226,58,30,.32), transparent 62%)", filter: "blur(24px)" }} />
      </div>

      <div className="wrap hero-grid" style={{ position: "relative", zIndex: 1, flex: 1, display: "flex", alignItems: "center", paddingTop: 52, paddingBottom: 40 }}>
        <div style={{ maxWidth: 720 }}>
          <div className="hero-pill reveal in" style={{ marginBottom: 22 }}>
            <span className="live-dot" />{t.hero.eyebrow}
          </div>
          <h1 className="hero-title reveal in" style={{ color: "var(--on-dark)", marginBottom: 20, textWrap: "balance" }}>
            {t.hero.title[0]}<br />
            <span style={{ color: "var(--accent)" }}>{t.hero.title[1]}</span>
          </h1>
          <p className="lead reveal in" style={{ color: "rgba(243,239,231,.82)", maxWidth: 560, marginBottom: 32 }}>{t.hero.sub}</p>
          <div className="reveal in hero-cta" style={{ display: "flex", gap: 14, flexWrap: "wrap", marginBottom: 32 }}>
            <button className="btn btn-primary btn-lg" onClick={onCta}>{t.hero.ctaPrimary}<Icon name="arrow" size={19} /></button>
            <button className="btn btn-ghost btn-lg on-dark" onClick={onCalc}>{t.hero.ctaSecondary}</button>
          </div>
          <div className="reveal in hero-badges" style={{ display: "flex", gap: 22, flexWrap: "wrap" }}>
            {t.hero.badges.map((b, i) => (
              <div key={i} style={{ display: "flex", alignItems: "center", gap: 8, color: "rgba(243,239,231,.9)", fontSize: 14, fontWeight: 600 }}>
                <span style={{ width: 20, height: 20, borderRadius: 999, background: "rgba(255,122,95,.18)", display: "grid", placeItems: "center", color: "#ff7a5f" }}><Icon name="check" size={13} /></span>
                {b}
              </div>
            ))}
          </div>
        </div>
      </div>

      {/* currency ticker */}
      <div style={{ position: "relative", zIndex: 1, borderTop: "1px solid rgba(255,255,255,.1)", background: "rgba(0,0,0,.28)", overflow: "hidden", flex: "0 0 auto" }}>
        <div className="ticker">
          {ticker.map((x, i) => (
            <span key={i} className="mono" style={{ display: "inline-flex", alignItems: "center", gap: 8, padding: "13px 26px", fontSize: 13, color: x === "SWIFT" ? "#ff7a5f" : "rgba(243,239,231,.62)", letterSpacing: "0.04em" }}>
              {x}<span style={{ color: "rgba(255,255,255,.18)" }}>•</span>
            </span>
          ))}
        </div>
      </div>

      <style>{`
        .hero-title{ font-size: clamp(32px, min(5vw, 7vh), 64px); line-height: 1.04; font-weight: 800; letter-spacing: -0.025em; }
        .hero-pill{ display:inline-flex; align-items:center; gap:9px; font-family:var(--font-mono); font-size:11.5px; font-weight:500; letter-spacing:0.1em; text-transform:uppercase; color:#ff7a5f; background:rgba(226,58,30,.12); border:1px solid rgba(226,58,30,.3); padding:8px 14px; border-radius:999px; }
        .live-dot{ width:7px; height:7px; border-radius:50%; background:#36d27e; box-shadow:0 0 0 0 rgba(54,210,126,.6); animation:pulse 1.8s infinite; flex:0 0 auto; }
        @keyframes pulse{ 0%{box-shadow:0 0 0 0 rgba(54,210,126,.55);} 70%{box-shadow:0 0 0 7px rgba(54,210,126,0);} 100%{box-shadow:0 0 0 0 rgba(54,210,126,0);} }
        .ticker{ display:inline-flex; white-space:nowrap; animation:scrollx 26s linear infinite; }
        @keyframes scrollx{ from{transform:translateX(0);} to{transform:translateX(-50%);} }
        @media(max-width:900px){ .hero-sec{ min-height:auto !important; } .hero-grid{ padding-top:48px !important; padding-bottom:44px !important; } }
        @media(max-width:600px){ .hero-grid{ padding-top:40px !important; padding-bottom:40px !important; } }
        /* fit-to-height compression for short viewports */
        @media(min-width:901px) and (max-height:820px){
          .hero-grid{ padding-top:40px !important; padding-bottom:28px !important; }
          .hero-title{ margin-bottom:16px !important; }
          .hero-sec .lead{ font-size:16px !important; margin-bottom:22px !important; }
          .hero-pill{ margin-bottom:16px !important; }
          .hero-cta{ margin-bottom:22px !important; }
          .hero-sec .btn-lg{ padding:15px 26px !important; font-size:15.5px !important; }
        }
        @media(min-width:901px) and (max-height:700px){
          .hero-grid{ padding-top:32px !important; padding-bottom:20px !important; }
          .hero-title{ margin-bottom:12px !important; }
          .hero-sec .lead{ margin-bottom:18px !important; }
          .hero-cta{ margin-bottom:18px !important; }
        }
      `}</style>
    </section>
  );
}

// ---- Metrics strip ----
function Metrics({ t }) {
  const ref = useReveal();
  return (
    <section ref={ref} style={{ background: "var(--dark-soft)", borderBottom: "1px solid rgba(255,255,255,.06)" }}>
      <div className="wrap" style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 1, background: "rgba(255,255,255,.07)" }}>
        {t.metrics.map((m, i) => (
          <div key={i} className="reveal" style={{ background: "var(--dark-soft)", padding: "34px 26px", transitionDelay: `${i * 70}ms` }}>
            <div className="mono" style={{ fontSize: "clamp(28px,3.4vw,42px)", fontWeight: 700, color: "var(--accent)", letterSpacing: "-0.02em", lineHeight: 1 }}>{m.value}</div>
            <div style={{ color: "var(--on-dark-muted)", fontSize: 14.5, marginTop: 10, lineHeight: 1.4 }}>{m.label}</div>
          </div>
        ))}
      </div>
      <style>{`@media(max-width:760px){ section > .wrap[style*="repeat(4"]{ grid-template-columns:repeat(2,1fr) !important; } }`}</style>
    </section>
  );
}

// ---- How it works ----
function HowItWorks({ t }) {
  const ref = useReveal();
  return (
    <section id="how" className="section" ref={ref}>
      <div className="wrap">
        <div className="reveal" style={{ maxWidth: 640, marginBottom: 56 }}>
          <div className="eyebrow" style={{ marginBottom: 16 }}>{t.how.eyebrow}</div>
          <h2 className="h-section" style={{ marginBottom: 16 }}>{t.how.title}</h2>
          <p className="lead">{t.how.sub}</p>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "repeat(5,1fr)", gap: 16 }} className="how-grid">
          {t.how.steps.map((s, i) => (
            <div key={i} className="reveal card" style={{ padding: "26px 22px", transitionDelay: `${i * 80}ms`, position: "relative" }}>
              <div className="mono" style={{ fontSize: 13, color: "var(--accent)", fontWeight: 700, letterSpacing: "0.1em", marginBottom: 18 }}>{s.n}</div>
              <h3 style={{ fontSize: 19, marginBottom: 8 }}>{s.t}</h3>
              <p style={{ fontSize: 14.5, color: "var(--muted)", lineHeight: 1.5 }}>{s.d}</p>
              {i < t.how.steps.length - 1 && (
                <div className="step-arrow" style={{ position: "absolute", right: -13, top: "50%", transform: "translateY(-50%)", color: "var(--line-strong)", zIndex: 2 }}>
                  <Icon name="arrow" size={20} />
                </div>
              )}
            </div>
          ))}
        </div>
      </div>
      <style>{`
        @media(max-width:1000px){ .how-grid{ grid-template-columns:repeat(2,1fr) !important; } .step-arrow{ display:none; } }
        @media(max-width:560px){ .how-grid{ grid-template-columns:1fr !important; } }
      `}</style>
    </section>
  );
}

// ---- Advantages ----
function Advantages({ t }) {
  const ref = useReveal();
  return (
    <section id="advantages" className="section" ref={ref} style={{ background: "var(--surface)", borderTop: "1px solid var(--line)", borderBottom: "1px solid var(--line)" }}>
      <div className="wrap">
        <div className="reveal" style={{ maxWidth: 640, marginBottom: 52 }}>
          <div className="eyebrow" style={{ marginBottom: 16 }}>{t.advantages.eyebrow}</div>
          <h2 className="h-section">{t.advantages.title}</h2>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "repeat(3,1fr)", gap: 18 }} className="adv-grid">
          {t.advantages.items.map((a, i) => (
            <div key={i} className="reveal" style={{ transitionDelay: `${(i % 3) * 70}ms`, padding: "30px 26px", borderRadius: "var(--radius)", border: "1px solid var(--line)", background: "var(--bg)" }}>
              <div style={{ width: 50, height: 50, borderRadius: 13, background: "var(--accent-tint)", color: "var(--accent)", display: "grid", placeItems: "center", marginBottom: 20 }}>
                <Icon name={a.icon} size={26} />
              </div>
              <h3 style={{ fontSize: 21, marginBottom: 9 }}>{a.t}</h3>
              <p style={{ color: "var(--muted)", fontSize: 15.5, lineHeight: 1.55 }}>{a.d}</p>
            </div>
          ))}
        </div>
      </div>
      <style>{`
        @media(max-width:900px){ .adv-grid{ grid-template-columns:repeat(2,1fr) !important; } }
        @media(max-width:560px){ .adv-grid{ grid-template-columns:1fr !important; } }
      `}</style>
    </section>
  );
}

Object.assign(window, { useReveal, Logo, Header, Hero, Metrics, HowItWorks, Advantages });
