// Shared components for Il Punto Media site
// Loaded after React/Babel; exposes globals at end.

const { useState, useEffect, useRef } = React;

/* ============ Icons ============ */
function ArrowRight({ size = 16 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 12h14M13 6l6 6-6 6" />
    </svg>);

}
function ArrowUpRight({ size = 16 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M7 17 17 7M9 7h8v8" />
    </svg>);

}
function PlayIcon({ color = "#fff", size = 28 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={color}>
      <path d="M8 5v14l11-7z" />
    </svg>);

}
function MenuIcon() {
  return (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round">
      <path d="M4 7h16M4 12h16M4 17h16" />
    </svg>);

}
function CloseIcon() {
  return (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round">
      <path d="M6 6l12 12M18 6 6 18" />
    </svg>);

}
function InstagramGlyph({ size = 24, color = "#6B3FFE" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2">
      <rect x="3" y="3" width="18" height="18" rx="5" />
      <circle cx="12" cy="12" r="4" />
      <circle cx="17.5" cy="6.5" r="1" fill={color} />
    </svg>);

}
function LinkedInGlyph({ size = 24, color = "#0A66C2" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={color}>
      <path d="M4 4h4v4H4zM4 9h4v11H4zM10 9h4v1.6c.7-1.1 2.1-1.9 3.6-1.9 2.7 0 4.4 1.8 4.4 4.7V20h-4v-5.7c0-1.4-.6-2.3-1.9-2.3-1.2 0-2.1.8-2.1 2.3V20h-4z" />
    </svg>);

}
function TikTokGlyph({ size = 24, color = "#0A0A0A" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={color}>
      <path d="M19 8.5a6.5 6.5 0 0 1-3.8-1.2v6.4a5.7 5.7 0 1 1-5.7-5.7c.3 0 .6 0 .9.1V11a2.7 2.7 0 1 0 1.9 2.6V3h2.9c0 .8.2 3.2 3.8 3.6z" />
    </svg>);

}
function YouTubeGlyph({ size = 24 }) {
  return (
    <svg width={size} height={size * 0.7} viewBox="0 0 24 17" fill="#FF0000">
      <path d="M23.5 2.6c-.3-1-1-1.8-2-2C19.6 0 12 0 12 0S4.4 0 2.5.5C1.5.8.7 1.6.5 2.6.1 4.5 0 8.5 0 8.5s.1 4 .5 5.9c.2 1 1 1.8 2 2 1.9.5 9.5.5 9.5.5s7.6 0 9.5-.5c1-.3 1.7-1 2-2 .4-1.9.5-5.9.5-5.9s-.1-4-.5-5.9z" />
      <path fill="#fff" d="M9.6 12.1V4.9l6.3 3.6z" />
    </svg>);

}

/* ============ Nav ============ */
function Nav({ route, navigate }) {
  const [open, setOpen] = useState(false);
  const [menuOpen, setMenuOpen] = useState(null); // "media" | "agency" | null
  const [mobileSub, setMobileSub] = useState(null); // accordion mobile: "media" | "agency" | null
  const closeTimer = useRef(null);
  const ORANGE = "#E94B2C";

  const links = [
  { id: "media", label: "Media", hasMenu: true },
  { id: "agency", label: "Agency", hasMenu: true },
  { id: "casi", label: "Casi studio" },
  { id: "chi", label: "Chi siamo" }];


  const mediaItems = [
  { id: "media-economico", logo: "assets/media/logo-economico.webp", name: "Il Punto Economico",
    desc: "La community per chi vuole davvero capirne di più di economia e attualità." },
  { id: "media-techy", logo: "assets/media/logo-techy.webp", name: "Techy",
    desc: "La community per chi vuole capirne di più di innovazione, tecnologia e AI." }];


  const agencyItems = [
  { id: "agency-yt", name: "YouTube Long-Form",
    desc: "Canali in white label per posizionarti come autorità nella tua nicchia." },
  { id: "agency-shortform", name: "Short Form & UGC",
    desc: "Reels, TikTok e Shorts che fermano il pollice e convertono in adv." },
  { id: "agency-social", name: "Social Media & Content",
    desc: "Presenza social end-to-end su IG, LinkedIn e TikTok." }];


  const openMenu = (id) => {
    if (closeTimer.current) {clearTimeout(closeTimer.current);closeTimer.current = null;}
    setMenuOpen(id);
  };
  const scheduleClose = () => {
    if (closeTimer.current) clearTimeout(closeTimer.current);
    closeTimer.current = setTimeout(() => setMenuOpen(null), 140);
  };

  const go = (id) => {navigate(id);setOpen(false);setMenuOpen(null);setMobileSub(null);};
  const isActiveMedia = route === "media" || route.startsWith("media-");
  const isActiveAgency = route === "agency" || route.startsWith("agency-");

  return (
    <>
    <header className="nav">
      <div className="nav-inner">
        <a href="#" onClick={(e) => {e.preventDefault();go("home");}}
        style={{ display: "inline-flex", alignItems: "center" }}>
          <img src="assets/logo-ilpunto.webp" alt="ilpunto" style={{ height: 36, width: "auto", display: "block" }} />
        </a>
        <nav className="nav-links">
          {links.map((l) => {
            if (!l.hasMenu) {
              return (
                <a key={l.id} href={`#${l.id}`}
                onClick={(e) => {e.preventDefault();go(l.id);}}
                className={"nav-link " + (route === l.id ? "active" : "")}>
                  {l.label}
                </a>);

            }
            const isThisActive = l.id === "media" ? isActiveMedia : isActiveAgency;
            const isHover = menuOpen === l.id;
            const highlighted = isHover || isThisActive;
            const items = l.id === "media" ? mediaItems : agencyItems;
            return (
              <div key={l.id}
              onMouseEnter={() => openMenu(l.id)}
              onMouseLeave={scheduleClose}
              style={{ position: "relative" }}>
                <a href={`#${l.id}`}
                onClick={(e) => {e.preventDefault();go(l.id);}}
                className="nav-link"
                style={{
                  display: "inline-flex", alignItems: "center", gap: 6,
                  padding: "6px 12px",

                  background: highlighted ? ORANGE : "transparent",
                  color: highlighted ? "#fff" : undefined,
                  transition: "background 160ms ease, color 160ms ease", borderRadius: "999px 999px 999px 0px"
                }}>
                  {l.label}
                  <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"
                  style={{ transform: isHover ? "rotate(180deg)" : "rotate(0deg)", transition: "transform 200ms" }}>
                    <path d="M6 9l6 6 6-6" />
                  </svg>
                </a>
                {isHover &&
                <NavDropdown items={items} accent={ORANGE} navigate={go} />
                }
              </div>);

          })}
        </nav>
        <a className="nav-cta-btn desktop-only" href="#contatti"
        onClick={(e) => {e.preventDefault();go("contatti");}} style={{ borderRadius: "999px 999px 999px 0px" }}>
          Contattaci
        </a>
        <button className="menu-btn" aria-label="Menu" onClick={() => setOpen(true)}>
          <MenuIcon />
        </button>
      </div>
      </header>
      {open &&
      <div onClick={() => setOpen(false)} style={{
        position: "fixed", top: 0, left: 0, right: 0, bottom: 0,
        background: "rgba(10,10,10,0.45)", zIndex: 60,
        display: "flex", flexDirection: "column"
      }}>
        {/* Pannello bianco a tendina: alto solo quanto il suo contenuto (fino a Contattaci) */}
        <div onClick={(e) => e.stopPropagation()} style={{
          background: "#fff",
          padding: "20px var(--gutter) 32px",
          display: "flex", flexDirection: "column",
          borderRadius: "0 0 22px 22px",
          boxShadow: "0 18px 40px -16px rgba(0,0,0,0.35)"
        }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
            <img src="assets/logo-ilpunto.webp" alt="ilpunto" style={{ height: 32 }} />
            <button className="menu-btn" onClick={() => setOpen(false)}><CloseIcon /></button>
          </div>
          <div style={{ display: "flex", flexDirection: "column", gap: 4, marginTop: 24 }}>
            {links.map((l) => {
              if (!l.hasMenu) {
                return (
                  <a key={l.id} href={`#${l.id}`} onClick={(e) => {e.preventDefault();go(l.id);}}
                  style={{ fontSize: 26, fontWeight: 800, padding: "10px 0", letterSpacing: "-0.02em" }}>
                    {l.label}
                  </a>);
              }
              const items = l.id === "media" ? mediaItems : agencyItems;
              const expanded = mobileSub === l.id;
              return (
                <div key={l.id}>
                  <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
                    <a href={`#${l.id}`} onClick={(e) => {e.preventDefault();go(l.id);}}
                    style={{ fontSize: 26, fontWeight: 800, padding: "10px 0", letterSpacing: "-0.02em" }}>
                      {l.label}
                    </a>
                    <button aria-label={"Espandi " + l.label} aria-expanded={expanded}
                    onClick={() => setMobileSub(expanded ? null : l.id)}
                    style={{ background: "none", border: "none", padding: "10px 4px", cursor: "pointer", color: "#0A0A0A", display: "flex" }}>
                      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"
                      style={{ transform: expanded ? "rotate(180deg)" : "none", transition: "transform 200ms" }}>
                        <path d="M6 9l6 6 6-6" />
                      </svg>
                    </button>
                  </div>
                  {expanded &&
                  <div style={{ display: "flex", flexDirection: "column", gap: 2, paddingLeft: 14, paddingBottom: 10 }}>
                    {items.map((it) =>
                    <a key={it.id} href={`#${it.id}`} onClick={(e) => {e.preventDefault();go(it.id);}}
                    style={{ fontSize: 17, fontWeight: 600, padding: "8px 0", color: "#0A0A0A" }}>
                        {it.name}
                      </a>
                    )}
                  </div>
                  }
                </div>);
            })}
            <a className="nav-cta-btn" style={{ marginTop: 20, alignSelf: "flex-start" }}
            href="#contatti" onClick={(e) => {e.preventDefault();go("contatti");}}>
              Contattaci
            </a>
          </div>
        </div>
      </div>
      }
    </>);

}

function NavDropdown({ items, accent, navigate }) {
  const [hover, setHover] = useState(null);
  return (
    <div style={{
      position: "absolute", top: "100%", left: "50%", transform: "translateX(-50%)",
      paddingTop: 14, // bridge the gap between trigger and panel for safe cursor travel
      zIndex: 50, animation: "navDropIn 180ms ease-out"
    }}>
      <div style={{
        background: "#fff", borderRadius: 18,
        boxShadow: "0 1px 0 rgba(10,10,10,0.06), 0 24px 60px -22px rgba(10,10,10,0.30)",
        padding: 12,
        minWidth: 380,
        display: "grid", gap: 4
      }}>
        {items.map((it, i) =>
        <a key={it.id} href={`#${it.id}`}
        onClick={(e) => {e.preventDefault();navigate(it.id);}}
        onMouseEnter={() => setHover(i)}
        onMouseLeave={() => setHover(null)}
        style={{
          display: "flex", alignItems: "flex-start", gap: 14,
          padding: "14px 14px",
          borderRadius: 14,
          background: hover === i ? "#F1EFEC" : "transparent",
          transition: "background 160ms",
          textDecoration: "none", color: "#0A0A0A"
        }}>
            {it.logo ?
          <img src={it.logo} alt="" style={{
            width: 44, height: 44, borderRadius: 999, flex: "none",
            background: "#fff", objectFit: "cover"
          }} /> :

          <div style={{
            width: 44, height: 44, borderRadius: 12, flex: "none",
            background: it.id === "agency-yt" ? "#3B5BFF" : it.id === "agency-shortform" ? "#7A4DFF" : it.id === "agency-social" ? "#FF6B9F" : accent,
            color: "#fff",
            display: "grid", placeItems: "center"
          }}>
                <ArrowUpRight size={18} />
              </div>
          }
            <div style={{ minWidth: 0 }}>
              <div style={{
              fontFamily: "var(--font-display)", fontWeight: 700,
              fontSize: 16, letterSpacing: "-0.01em", marginBottom: 4,
              color: "#0A0A0A"
            }}>{it.name}</div>
              <div style={{
              fontSize: 13, lineHeight: 1.45, color: "#5A5A5A",
              fontWeight: 500
            }}>{it.desc}</div>
            </div>
          </a>
        )}
      </div>
      <style>{`
        @keyframes navDropIn {
          from { opacity: 0; transform: translateX(-50%) translateY(-6px); }
          to   { opacity: 1; transform: translateX(-50%) translateY(0); }
        }
      `}</style>
    </div>);

}

/* ============ Browser-style window ============ */
function BWindow({ children, shadowColor, shadowOffset = 14, style, className = "" }) {
  return (
    <div className={"bwin lift " + className} style={style}>
      <div className="bwin-bar">
        <span className="bwin-x">×</span>
      </div>
      <div>{children}</div>
      {shadowColor &&
      <div style={{
        position: "absolute",
        top: shadowOffset, left: shadowOffset,
        right: -shadowOffset, bottom: -shadowOffset,
        background: shadowColor,
        borderRadius: 6, zIndex: -1
      }} />
      }
    </div>);

}
// Wrapper that adds the colored shadow correctly via a relative parent
function BWindowFramed({ children, shadowColor = "var(--c-violet)", offset = 14, ...rest }) {
  return (
    <div style={{ position: "relative", display: "inline-block", width: "100%" }}>
      <div style={{
        position: "absolute", inset: 0,
        transform: `translate(${offset}px, ${offset}px)`,
        background: shadowColor, borderRadius: 6,
        zIndex: 0
      }} />
      <div style={{ position: "relative", zIndex: 1 }}>
        <BWindow {...rest}>{children}</BWindow>
      </div>
    </div>);

}

/* ============ Footer (cream card with curved notch design on orange field) ============ */
function Footer({ navigate }) {
  const ORANGE = "#EE5135";
  const CREAM = "#F7EFE7";
  const PAGE_BG = "#FFFFFF"; // page background showing through top notches

  return (
    <footer style={{ background: ORANGE, position: "relative", marginTop: 0,
      padding: "clamp(40px, 5vw, 80px) clamp(20px, 4vw, 64px) clamp(28px, 4vw, 56px)" }}>

      {/* Top tagline (centered) */}
      <div style={{ display: "flex", justifyContent: "center", maxWidth: 1480, margin: "0 auto",
        padding: "clamp(28px, 3vw, 48px) 8px clamp(28px, 3vw, 48px)" }}>
        <p style={{ color: "#fff", fontSize: "clamp(14px, 1.05vw, 16px)", margin: 0, opacity: 0.95,
          maxWidth: 720, textAlign: "center" }}>
          Vogliamo risolvere il problema della conoscenza attraverso la divulgazione.
        </p>
      </div>

      {/* Cream card */}
      <div style={{ position: "relative", maxWidth: 1480, margin: "0 auto" }}>
        <div className="footer-card" style={{
          position: "relative",
          background: CREAM,

          padding: "clamp(40px, 5vw, 80px) clamp(32px, 5vw, 88px)",
          display: "grid",
          gridTemplateColumns: "1.3fr 1fr 1.2fr 0.9fr",
          gap: "clamp(24px, 4vw, 72px)",
          alignItems: "start", borderRadius: "500px 555px 500px 0px"
        }}>

          {/* Brand col — centered horizontally within its column */}
          <div style={{ position: "relative", display: "flex", flexDirection: "column",
            alignItems: "center", textAlign: "center" }}>
            <img src="assets/ilpunto-logo.webp" alt="ilpunto" className="footer-brand-logo"
            style={{ display: "block", width: "min(100%, 320px)", height: "auto" }} />
            <p style={{
              color: ORANGE, marginTop: 26,
              fontFamily: "var(--font-display)", fontWeight: 700,
              fontSize: "clamp(18px, 1.35vw, 22px)", lineHeight: 1.3,
              letterSpacing: "-0.01em", maxWidth: 320, textAlign: "center"
            }}>
              Creiamo contenuti che la gente sceglie di guardare. Anche per il tuo brand.
            </p>
          </div>

          {/* Media */}
          <FooterColV2 title="Media" color={ORANGE} items={[
          { label: "Il Punto Economico", onClick: () => navigate && navigate("media-economico") },
          { label: "Techy", onClick: () => navigate && navigate("media-techy") }]
          } />

          {/* Agency */}
          <FooterColV2 title="Agency" color={ORANGE} items={[
          { label: "YouTube Management", onClick: () => navigate && navigate("agency-yt") },
          { label: "Short Form & UGC", onClick: () => navigate && navigate("agency-shortform") },
          { label: "Social Media & Content", onClick: () => navigate && navigate("agency-social") }]
          } />

          {/* Misc — title hidden so items align with first row of Media/Agency */}
          <FooterColV2 color={ORANGE} items={[
          { label: "Cookies" },
          { label: "Privacy Policy" },
          { label: "Blog" }]
          } />
        </div>
      </div>

      {/* Bottom legal — single centered line */}
      <div style={{
        maxWidth: 1480, margin: "clamp(20px, 2vw, 32px) auto 0",
        padding: "0 8px",
        display: "flex", justifyContent: "center", alignItems: "center", flexWrap: "wrap", gap: 6,
        fontSize: 12, color: "rgba(255,255,255,0.85)", textAlign: "center"
      }}>
        <span>© {new Date().getFullYear()} Il Punto Media S.r.l. - P.IVA 04709330163 - Bolgare (BG), Italia</span>
      </div>

      <style>{`
        @media (max-width: 980px) {
          .footer-card { grid-template-columns: 1fr 1fr !important; }
        }
        @media (max-width: 560px) {
          .footer-card { grid-template-columns: 1fr !important; padding-top: clamp(48px, 12vw, 72px) !important; }
          .footer-brand-logo { width: min(62%, 220px) !important; margin-top: 6px; }
        }
      `}</style>
    </footer>);

}
function FooterColV2({ title, items, color }) {
  // Reserve title space even when there's no title, so items align horizontally with titled cols.
  return (
    <div style={{ position: "relative" }}>
      <div style={{
        fontSize: 14, fontWeight: 500, color: color, opacity: title ? 0.55 : 0,
        marginBottom: 28, letterSpacing: "-0.005em",
        height: "1.2em", visibility: title ? "visible" : "hidden"
      }}>{title || "."}</div>
      <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "grid", gap: 26 }}>
        {items.map((it, i) =>
        <li key={i}>
            <a href="#" onClick={(e) => {e.preventDefault();it.onClick && it.onClick();}}
          style={{ fontSize: 18, fontWeight: 500, color: "#0A0A0A", textDecoration: "none" }}>
              {it.label}
            </a>
          </li>
        )}
      </ul>
    </div>);

}


/* ============ Logo marquee ============ */
function LogoMarquee({ logos }) {
  const set = [...logos, ...logos];
  return (
    <div className="marquee">
      <div className="marquee-track">
        {set.map((l, i) => {
          if (l && typeof l === "object" && l.text) {
            return (
              <span key={i} className="logo-img" style={{
                display: "inline-flex", alignItems: "center",
                fontFamily: "var(--font-display)", fontWeight: 800,
                fontSize: "clamp(22px, 2.4vw, 34px)", letterSpacing: "-0.025em",
                color: "#9E9E9D", whiteSpace: "nowrap", height: 40, lineHeight: 1
              }}>{l.text}</span>);
          }
          return (
            <img key={i} src={`assets/logos/${l}.webp`} alt={l} className="logo-img" />);
        })}
      </div>
    </div>);

}

/* ============ Stat card ============ */
function StatBig({ value, label, color = "var(--c-violet)" }) {
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
      <div style={{
        fontFamily: "var(--font-display)", fontWeight: 800,
        fontSize: "clamp(54px, 7vw, 96px)", lineHeight: 1, letterSpacing: "-0.04em",
        color
      }}>{value}</div>
      <div style={{ fontSize: 16, fontWeight: 600, color: "var(--c-fg-soft)", maxWidth: 260, lineHeight: 1.35 }}>
        {label}
      </div>
    </div>);

}

Object.assign(window, {
  Nav, Footer, BWindow, BWindowFramed, LogoMarquee, StatBig,
  ArrowRight, ArrowUpRight, PlayIcon, InstagramGlyph, LinkedInGlyph, TikTokGlyph, YouTubeGlyph
});