/* cctv.ae — part 1: brand mark, header, hero, solutions, trust, stats, footer.
   Composes the Al-Futtaim Technologies design system. Copy from window.cctv. */
(function () {
  const data = window.cctv;
  const wrap = { maxWidth: "75rem", marginInline: "auto", paddingInline: "1.5rem" };
  const Icon = ({ name, size = 24, color, stroke }) => (
    <i data-lucide={name} style={{ width: size, height: size, color, display: "inline-flex", strokeWidth: stroke }} />
  );

  /* Al-Futtaim Technologies lock-up + cctv.ae platform identity. */
  function CCTVLogo({ height = 34, showUrl = true }) {
    return (
      <span style={{ display: "inline-flex", alignItems: "center", gap: "0.7rem" }}>
        <img src="assets/AFT-Logo-darkmode.png" alt="Al-Futtaim Technologies" style={{ height, width: "auto", display: "block" }} />
        {showUrl && (
          <>
            <span aria-hidden style={{ width: 1, height: height * 0.78, background: "rgba(255,255,255,0.28)" }} />
            <span style={{ fontFamily: "ui-monospace, monospace", fontSize: height * 0.46, fontWeight: 700, letterSpacing: "-0.01em", color: "var(--af-light-blue)" }}>cctv<span style={{ color: "rgba(255,255,255,0.8)" }}>.ae</span></span>
          </>
        )}
      </span>
    );
  }

  /* ── Site search (one-page: scrolls to + highlights the match) ── */
  function SearchBox() {
    const [open, setOpen] = React.useState(false);
    const [q, setQ] = React.useState("");
    const [msg, setMsg] = React.useState("");
    const inputRef = React.useRef(null);
    React.useEffect(() => { if (window.lucide) window.lucide.createIcons(); }, [open]);
    React.useEffect(() => { if (open && inputRef.current) inputRef.current.focus(); }, [open]);
    const run = (e) => {
      if (e) e.preventDefault();
      const query = q.trim().toLowerCase();
      if (!query) return;
      const root = document.getElementById("root");
      const els = Array.prototype.slice.call(root.querySelectorAll("h1,h2,h3,h4,h5,p,li,a,button,figcaption"))
        .filter((el) => el.childElementCount === 0 && el.textContent && el.textContent.trim().length);
      const hit = els.find((el) => el.textContent.toLowerCase().indexOf(query) !== -1);
      if (hit) {
        const section = hit.closest("section, header, footer") || hit;
        section.scrollIntoView({ behavior: "smooth", block: "start" });
        hit.classList.add("search-hit");
        setTimeout(() => hit.classList.remove("search-hit"), 2000);
        setMsg(""); setOpen(false); setQ("");
      } else {
        setMsg("No matches for “" + q.trim() + "”.");
      }
    };
    return (
      <div style={{ position: "relative" }}>
        <button type="button" aria-label="Search the site" aria-expanded={open} onClick={() => setOpen((o) => !o)}
          style={{ display: "grid", placeItems: "center", width: 34, height: 34, borderRadius: "var(--radius-pill)", border: "1px solid rgba(255,255,255,0.25)", background: "transparent", color: "#fff", cursor: "pointer" }}>
          <Icon name={open ? "x" : "search"} size={16} color="#fff" />
        </button>
        {open && (
          <form onSubmit={run} role="search"
            style={{ position: "absolute", top: "calc(100% + 0.6rem)", right: 0, display: "flex", alignItems: "center", gap: "0.4rem", padding: "0.4rem", background: "rgba(4,30,66,0.97)", border: "1px solid rgba(255,255,255,0.18)", borderRadius: "var(--radius-pill)", boxShadow: "var(--shadow-lg)", zIndex: 60 }}>
            <input ref={inputRef} value={q} type="search" placeholder="Search the site…" aria-label="Search query"
              onChange={(e) => { setQ(e.target.value); setMsg(""); }}
              onKeyDown={(e) => { if (e.key === "Escape") setOpen(false); }}
              style={{ width: "13rem", padding: "0.5rem 0.85rem", fontSize: "var(--text-sm)", color: "#fff", background: "rgba(255,255,255,0.06)", border: "1px solid rgba(255,255,255,0.18)", borderRadius: "var(--radius-pill)", outline: "none", fontFamily: "inherit" }} />
            <button type="submit" aria-label="Search" style={{ display: "grid", placeItems: "center", width: 32, height: 32, flexShrink: 0, borderRadius: "var(--radius-pill)", border: "none", background: "var(--af-light-blue)", color: "#041e42", cursor: "pointer" }}>
              <Icon name="search" size={15} color="#041e42" />
            </button>
            {msg && <span style={{ position: "absolute", top: "calc(100% + 0.4rem)", right: 4, whiteSpace: "nowrap", fontSize: "var(--text-xs)", color: "rgba(255,255,255,0.85)" }}>{msg}</span>}
          </form>
        )}
      </div>
    );
  }

  /* ── Header ─────────────────────────────────────────────── */
  function Header() {
    const { Button } = window.AlFuttaimTechnologiesDesignSystem_4ea487;
    return (
      <header style={{ position: "sticky", top: 0, zIndex: 50, background: "rgba(4,30,66,0.82)", backdropFilter: "blur(10px)", borderBottom: "1px solid rgba(255,255,255,0.08)" }}>
        <div style={{ ...wrap, display: "flex", alignItems: "center", justifyContent: "space-between", paddingBlock: "0.85rem" }}>
          <CCTVLogo height={38} />
          <nav className="nav-links" style={{ display: "flex", alignItems: "center", gap: "2rem" }}>
            {data.brand.nav.map((l, i) => (
              <a key={l} href={i === 0 ? "#top" : i === 1 ? "#solutions" : "#contact"} style={{ fontSize: "var(--text-sm)", fontWeight: "var(--weight-medium)", letterSpacing: "0.04em", textTransform: "uppercase", whiteSpace: "nowrap", color: "rgba(255,255,255,0.85)", textDecoration: "none" }}>{l}</a>
            ))}
            <SearchBox />
          </nav>
        </div>
      </header>
    );
  }

  /* ── Hero ───────────────────────────────────────────────── */
  function Hero() {
    const { Button } = window.AlFuttaimTechnologiesDesignSystem_4ea487;
    return (
      <section id="top" style={{ position: "relative", overflow: "hidden", background: "var(--af-midnight)", color: "#fff" }}>
        <img src="assets/hero-controlroom.jpg" alt="" aria-hidden style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", objectPosition: "center" }} />
        <div aria-hidden style={{ position: "absolute", inset: 0, background: "linear-gradient(100deg, rgba(4,30,66,0.94) 0%, rgba(4,30,66,0.80) 38%, rgba(4,30,66,0.45) 70%, rgba(4,30,66,0.65) 100%)" }} />
        <div aria-hidden style={{ position: "absolute", inset: 0, opacity: 0.5, background: "var(--glow-cyan)", pointerEvents: "none" }} />
        {window.CCTVRadarHUD && React.createElement(window.CCTVRadarHUD)}
        <div style={{ ...wrap, position: "relative", zIndex: 3, paddingBlock: "10rem" }}>
          <div style={{ maxWidth: "42rem" }}>
            <p style={{ fontSize: "var(--text-sm)", fontWeight: "var(--weight-semibold)", letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--af-light-blue)" }}>{data.hero.eyebrow}</p>
            <h1 style={{ marginTop: "1.25rem", fontSize: "clamp(2.4rem, 5vw, 3.75rem)", fontWeight: "var(--weight-semibold)", lineHeight: 1.08, letterSpacing: "-0.02em", color: "#fff", textWrap: "balance" }}>
              {data.hero.titleLead}<span style={{ color: "var(--af-light-blue)" }}>{data.hero.titleHighlight}</span>{data.hero.titleTail}
            </h1>
            <div style={{ marginTop: "2.5rem" }}>
              <Button as="a" href="#contact" variant="accent" size="lg">{data.hero.cta}</Button>
            </div>
          </div>
        </div>
      </section>
    );
  }

  /* ── Solutions (3 cards overlapping hero) ───────────────── */
  function Solutions() {
    const { Card } = window.AlFuttaimTechnologiesDesignSystem_4ea487;
    return (
      <section id="solutions" style={{ position: "relative", background: "var(--af-midnight)", paddingBottom: "5.5rem" }}>
        <div style={{ ...wrap }}>
          <div className="grid-3" style={{ display: "grid", gap: "1.5rem", gridTemplateColumns: "repeat(3,1fr)", transform: "translateY(-4rem)", marginBottom: "-2rem" }}>
            {data.solutions.items.map((s) => (
              <Card key={s.title} variant="elevated" padding="lg" interactive>
                <div style={{ display: "flex", flexDirection: "column", alignItems: "center", textAlign: "center", height: "100%" }}>
                  {React.createElement(window.CCTVIconChip, { name: s.icon, chip: 56, size: 26 })}
                  <h3 style={{ marginTop: "1.25rem", fontSize: "var(--text-lg)", fontWeight: "var(--weight-semibold)", color: "var(--text-strong)" }}>{s.title}</h3>
                  <p style={{ marginTop: "0.75rem", fontSize: "var(--text-sm)", lineHeight: 1.65, color: "var(--text-body)", flex: 1 }}>{s.body}</p>
                  <a href="#contact" style={{ marginTop: "1.25rem", display: "inline-flex", alignItems: "center", gap: "0.35rem", fontSize: "var(--text-sm)", fontWeight: "var(--weight-semibold)", color: "var(--af-dark-blue)", textDecoration: "none" }}>
                    {s.link} <Icon name="chevron-right" size={15} color="var(--af-dark-blue)" />
                  </a>
                </div>
              </Card>
            ))}
          </div>
        </div>
      </section>
    );
  }

  /* ── Why enterprises trust (benefits + camera) ──────────── */
  function Trust() {
    const t = data.trust;
    const IconChip = window.CCTVIconChip;
    const mid = Math.ceil(t.benefits.length / 2);
    const cols = [t.benefits.slice(0, mid), t.benefits.slice(mid)];
    return (
      <section style={{ position: "relative", zIndex: 1, background: "var(--af-midnight)", color: "#fff" }}>
        <div style={{ ...wrap, paddingTop: "3rem", paddingBottom: "2.5rem", display: "flex", alignItems: "flex-end", justifyContent: "space-between", gap: "1.5rem", flexWrap: "wrap" }}>
          <h2 style={{ fontSize: "var(--text-h2)", fontWeight: "var(--weight-semibold)", lineHeight: 1.12, letterSpacing: "-0.02em" }}>
            {t.titleLead}<br /><span style={{ color: "var(--af-light-blue)" }}>{t.titleTail}</span>
          </h2>
          <span style={{ display: "inline-flex", alignItems: "center", gap: "0.6rem", padding: "0.55rem 1.1rem", borderRadius: "var(--radius-pill)", border: "1px solid rgba(0,174,239,0.4)", background: "rgba(0,174,239,0.08)", fontSize: "var(--text-sm)", fontWeight: "var(--weight-semibold)", letterSpacing: "0.04em", color: "var(--af-light-blue)" }}>
            <Icon name="orbit" size={18} color="var(--af-light-blue)" /> {t.side}
          </span>
        </div>
        <div style={{ position: "relative", background: "var(--af-mist)" }}>
          <div style={{ ...wrap, position: "relative", display: "grid", gridTemplateColumns: "1.05fr 0.95fr", gap: "2.5rem", alignItems: "center", paddingBlock: "3.5rem" }} className="grid-2">
            <div className="grid-2" style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: "1.5rem 2rem" }}>
              {cols.map((col, ci) => (
                <ul key={ci} style={{ listStyle: "none", margin: 0, padding: 0, display: "flex", flexDirection: "column", gap: "1.5rem" }}>
                  {col.map((b) => (
                    <li key={b.text} style={{ display: "flex", gap: "0.85rem", alignItems: "flex-start" }}>
                      <IconChip name={b.icon} chip={44} size={21} />
                      <span style={{ fontSize: "var(--text-sm)", lineHeight: 1.5, color: "var(--text-body)", paddingTop: "0.5rem" }}>{b.text}</span>
                    </li>
                  ))}
                </ul>
              ))}
            </div>
            <div style={{ position: "relative", display: "flex", alignItems: "center", justifyContent: "center" }}>
              <span aria-hidden style={{ position: "absolute", width: "17rem", height: "17rem", borderRadius: "50%", background: "radial-gradient(circle, rgba(0,174,239,0.16), transparent 68%)" }} />
              <img src="assets/camera-cutout.png" alt="360° IP security camera" style={{ position: "relative", zIndex: 2, width: "100%", maxWidth: "28rem", height: "auto", marginTop: "-2rem", filter: "drop-shadow(0 24px 38px rgba(4,30,66,0.3))" }} />
            </div>
          </div>
        </div>
      </section>
    );
  }

  /* ── Stats band (dark office photo + portrait) ──────────── */
  function Stats() {
    const s = data.stats;
    return (
      <section style={{ position: "relative", overflow: "hidden", background: "var(--af-midnight)", color: "#fff" }}>
        <img src="assets/stats-bg.jpg" alt="" aria-hidden style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover" }} />
        <div aria-hidden style={{ position: "absolute", inset: 0, background: "linear-gradient(90deg, rgba(4,30,66,0.92), rgba(4,30,66,0.72))" }} />
        <div style={{ ...wrap, position: "relative", display: "grid", gridTemplateColumns: "1.3fr 0.9fr", gap: "2rem", alignItems: "center", paddingBlock: "5.5rem" }} className="grid-2">
          <div>
            <p style={{ fontSize: "var(--text-sm)", fontWeight: "var(--weight-semibold)", letterSpacing: "0.18em", textTransform: "uppercase", color: "rgba(255,255,255,0.7)" }}>{s.eyebrow}</p>
            <h2 style={{ marginTop: "1rem", fontSize: "var(--text-h2)", fontWeight: "var(--weight-semibold)", lineHeight: 1.12, letterSpacing: "-0.02em", maxWidth: "30rem" }}>
              {s.titleParts.map((p, i) => <span key={i} style={{ color: p.accent ? "var(--af-light-blue)" : "#fff" }}>{p.t}</span>)}
            </h2>
            <div style={{ marginTop: "2.5rem", display: "flex", gap: "3.5rem", flexWrap: "wrap" }}>
              {s.figures.map((f) => (
                <div key={f.label} style={{ display: "flex", alignItems: "flex-start", gap: "0.6rem" }}>
                  <Icon name="chevrons-right" size={26} color="var(--af-light-blue)" />
                  <div>
                    <div style={{ fontSize: "2.75rem", fontWeight: "var(--weight-semibold)", lineHeight: 1, color: "#fff" }}>{f.value}</div>
                    <div style={{ marginTop: "0.4rem", fontSize: "var(--text-sm)", color: "rgba(255,255,255,0.7)" }}>{f.label}</div>
                  </div>
                </div>
              ))}
            </div>
          </div>
          <div style={{ display: "flex", justifyContent: "center" }}>
            <div style={{ position: "relative", width: "18rem", height: "18rem", maxWidth: "100%" }}>
              <div aria-hidden style={{ position: "absolute", left: "-1.5rem", bottom: "-1rem", width: "11rem", height: "11rem", borderRadius: "50%", background: "var(--af-light-blue)", opacity: 0.9 }} />
              <img src="assets/portrait.jpg" alt="CCTV.ae security consultant" style={{ position: "relative", width: "100%", height: "100%", objectFit: "cover", borderRadius: "50%", border: "3px solid rgba(255,255,255,0.15)" }} />
            </div>
          </div>
        </div>
      </section>
    );
  }

  /* ── Footer ─────────────────────────────────────────────── */
  function NocTiles({ noc }) {
    const [t, setT] = React.useState(0);
    React.useEffect(() => {
      const reduce = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
      if (reduce) return;
      const id = setInterval(() => setT((x) => x + 1), 2000);
      return () => clearInterval(id);
    }, []);
    const fmt = (tile) => {
      if (tile.fmt === "int") { const jitter = (t * 7 + tile.base) % 53; return (tile.base + jitter).toLocaleString(); }
      if (tile.fmt === "pct") return (tile.base - ((t % 3) * 0.01)).toFixed(2) + "%";
      if (tile.fmt === "sec") return (tile.base + ((t % 2) * 0.1)).toFixed(1) + "s";
      return tile.base;
    };
    return (
      <div className="grid-4" style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: "1rem" }}>
        {noc.tiles.map((tile) => (
          <div key={tile.k} style={{ position: "relative", overflow: "hidden", padding: "1.1rem 1.2rem", borderRadius: "var(--radius-md)", background: "rgba(255,255,255,0.05)", border: "1px solid rgba(0,174,239,0.22)" }}>
            <div style={{ display: "flex", alignItems: "center", gap: 8, color: "rgba(255,255,255,0.6)", fontSize: "var(--text-xs)", letterSpacing: "0.04em", textTransform: "uppercase" }}>
              <Icon name={tile.icon} size={15} color="var(--af-light-blue)" /> {tile.k}
            </div>
            <div style={{ marginTop: "0.5rem", fontFamily: "ui-monospace, monospace", fontSize: "1.5rem", fontWeight: 700, color: "#fff", letterSpacing: "-0.01em" }}>{fmt(tile)}</div>
            <span aria-hidden className="ai-scan" style={{ position: "absolute", left: 0, right: 0, height: 1, background: "linear-gradient(90deg, transparent, rgba(0,174,239,0.6), transparent)" }} />
          </div>
        ))}
      </div>
    );
  }

  function Footer() {
    const f = data.footer;
    const noc = f.noc;
    const [clock, setClock] = React.useState("");
    React.useEffect(() => {
      const tick = () => setClock(new Date().toLocaleTimeString("en-GB", { timeZone: "Asia/Dubai", hour12: false }) + " GST");
      tick(); const id = setInterval(tick, 1000); return () => clearInterval(id);
    }, []);
    return (
      <footer style={{ position: "relative", overflow: "hidden", background: "var(--af-midnight)", color: "#fff" }}>
        <img src="assets/plexus-bg.jpg" alt="" aria-hidden style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", opacity: 0.5 }} />
        <div aria-hidden style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, rgba(4,30,66,0.88), rgba(4,30,66,0.94))" }} />
        <div style={{ ...wrap, position: "relative", paddingTop: "3.5rem" }}>
          <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: "1rem", flexWrap: "wrap", marginBottom: "1.25rem" }}>
            <span style={{ display: "inline-flex", alignItems: "center", gap: 9, fontFamily: "ui-monospace, monospace", fontSize: "var(--text-xs)", fontWeight: 700, letterSpacing: "0.08em", textTransform: "uppercase", color: "#fff" }}>
              <span className="ai-blink" style={{ width: 8, height: 8, borderRadius: "50%", background: "#37d39b" }} />{noc.label}
            </span>
            <span style={{ display: "inline-flex", alignItems: "center", gap: 14, fontFamily: "ui-monospace, monospace", fontSize: "var(--text-xs)", color: "rgba(255,255,255,0.7)" }}>
              <span style={{ color: "#37d39b" }}>● {noc.status}</span><span>{clock}</span>
            </span>
          </div>
          <NocTiles noc={noc} />
          <div style={{ marginTop: "1rem", display: "flex", gap: "0.5rem", flexWrap: "wrap" }}>
            {noc.regions.map((r) => (
              <span key={r} style={{ display: "inline-flex", alignItems: "center", gap: 6, padding: "4px 11px", borderRadius: "var(--radius-pill)", background: "rgba(255,255,255,0.06)", border: "1px solid rgba(255,255,255,0.12)", fontSize: "var(--text-xs)", color: "rgba(255,255,255,0.78)" }}>
                <span className="ai-blink" style={{ width: 5, height: 5, borderRadius: "50%", background: "var(--af-light-blue)" }} />{r}
              </span>
            ))}
          </div>
        </div>
        <div aria-hidden style={{ ...wrap, position: "relative", marginBlock: "2.5rem", height: 1, background: "rgba(255,255,255,0.1)" }} />
        <div style={{ ...wrap, position: "relative", display: "grid", gridTemplateColumns: "1.2fr 1fr 1.4fr", gap: "2.5rem", paddingBottom: "2.5rem" }} className="grid-3">
          <div>
            <CCTVLogo height={42} />
          </div>
          {f.columns.map((c) => (
            <div key={c.heading}>
              <h4 style={{ fontSize: "var(--text-base)", fontWeight: "var(--weight-semibold)", marginBottom: "1.1rem" }}>{c.heading}</h4>
              <ul style={{ listStyle: "none", margin: 0, padding: 0, display: "flex", flexDirection: "column", gap: "0.7rem" }}>
                {c.links.map((l) => <li key={l}><a href="#solutions" style={{ fontSize: "var(--text-sm)", color: "rgba(255,255,255,0.72)", textDecoration: "none" }}>{l}</a></li>)}
              </ul>
            </div>
          ))}
          <div>
            <h4 style={{ fontSize: "var(--text-base)", fontWeight: "var(--weight-semibold)", marginBottom: "1.1rem" }}>{f.contact.heading}</h4>
            <address style={{ fontStyle: "normal", fontSize: "var(--text-sm)", lineHeight: 1.7, color: "rgba(255,255,255,0.72)" }}>
              <div style={{ color: "#fff", fontWeight: "var(--weight-semibold)" }}>{f.contact.org}</div>
              {f.contact.lines.map((l) => <div key={l}>{l}</div>)}
              <div style={{ marginTop: "0.9rem", display: "flex", alignItems: "center", gap: 7, whiteSpace: "nowrap" }}><Icon name="phone" size={14} color="var(--af-light-blue)" /><a href={"tel:" + f.contact.phone.replace(/\s/g, "")} style={{ color: "inherit", textDecoration: "none" }}>{f.contact.phone}</a></div>
              <div style={{ marginTop: "0.35rem", display: "flex", alignItems: "center", gap: 7, whiteSpace: "nowrap" }}><Icon name="mail" size={14} color="var(--af-light-blue)" /><a href={"mailto:" + f.contact.email} style={{ color: "inherit", textDecoration: "none" }}>{f.contact.email}</a></div>
            </address>
          </div>
        </div>
        <div style={{ ...wrap, position: "relative", paddingBottom: "3.5rem" }}>
          <div style={{ position: "relative", borderRadius: "var(--radius-md)", overflow: "hidden", border: "1px solid rgba(0,174,239,0.25)" }}>
            <div style={{ display: "flex", alignItems: "center", gap: 8, padding: "0.6rem 1rem", background: "rgba(0,0,0,0.3)", fontFamily: "ui-monospace, monospace", fontSize: "var(--text-xs)", color: "rgba(255,255,255,0.8)" }}>
              <Icon name="map-pin" size={14} color="var(--af-light-blue)" /> Head Office · {f.contact.mapPin}, Sheikh Zayed Rd., Dubai
            </div>
            <iframe title="Al-Futtaim Technologies — Al-Quoz Business Centre" src={f.contact.mapEmbed} loading="lazy" referrerPolicy="no-referrer-when-downgrade" style={{ width: "100%", height: "260px", border: 0, display: "block", filter: "grayscale(0.15) contrast(1.05)" }} />
          </div>
        </div>
        <div style={{ position: "relative", borderTop: "1px solid rgba(255,255,255,0.1)", background: "rgba(0,0,0,0.25)" }}>
          <div style={{ ...wrap, paddingBlock: "1rem", textAlign: "center", fontSize: "var(--text-xs)", color: "rgba(255,255,255,0.55)" }}>{f.legal}</div>
        </div>
      </footer>
    );
  }

  Object.assign(window, { CCTVLogo, CCTVHeader: Header, CCTVHero: Hero, CCTVSolutions: Solutions, CCTVTrust: Trust, CCTVStats: Stats, CCTVFooter: Footer, cctvWrap: wrap, CCTVIcon: Icon });
})();
