// MAZZU — Shared atoms: Reveal (IO-based fade-up), wordmark, icons, navbar

const { useEffect, useRef, useState } = React;

// ---------- Reveal (scroll-listener fade-up, robust inside iframes) ----------
function Reveal({ delay = 0, as = "div", style, className, children }) {
  const ref = React.useRef(null);
  const [v, setV] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    let raf = 0;
    const check = () => {
      raf = 0;
      const rect = el.getBoundingClientRect();
      if (rect.top < window.innerHeight - 80) {
        setV(true);
        window.removeEventListener("scroll", onScroll);
        window.removeEventListener("resize", onScroll);
      }
    };
    const onScroll = () => { if (!raf) raf = requestAnimationFrame(check); };
    check();
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onScroll);
    return () => {
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onScroll);
      if (raf) cancelAnimationFrame(raf);
    };
  }, []);
  const Tag = as;
  return (
    <Tag
      ref={ref}
      className={className}
      style={{
        ...style,
        opacity: v ? 1 : 0,
        transform: v ? "translateY(0)" : "translateY(20px)",
        transition: `opacity 0.7s ${delay}s ease-out, transform 0.7s ${delay}s ease-out`
      }}
    >
      {children}
    </Tag>
  );
}

// ---------- Icons (lucide-style, minimal monochrome) ----------
const Icon = ({ d, size = 18, stroke = 1.6 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
       stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round">
    {d}
  </svg>
);
const InstagramIcon = (props) => (
  <Icon {...props} d={<>
    <rect x="2.5" y="2.5" width="19" height="19" rx="5"/>
    <circle cx="12" cy="12" r="4.2"/>
    <circle cx="17.4" cy="6.6" r="0.6" fill="currentColor" stroke="none"/>
  </>}/>
);
const FacebookIcon = (props) => (
  <Icon {...props} d={<>
    <path d="M14 8h2.5V5H14c-2 0-3.5 1.5-3.5 3.5V11H8v3h2.5v8h3v-8H16l.5-3h-3V8.7c0-.4.3-.7.7-.7H14z"/>
  </>}/>
);
const WhatsappIcon = (props) => (
  <Icon {...props} d={<>
    <path d="M20.5 12a8.5 8.5 0 1 1-15.7 4.5L3 21l4.6-1.7A8.5 8.5 0 0 0 20.5 12z"/>
    <path d="M9.2 9c.1-.3.4-.4.7-.4h.6c.2 0 .4.1.5.4l.6 1.6a.5.5 0 0 1-.1.5l-.5.6c.5 1 1.3 1.7 2.3 2.2l.6-.5a.5.5 0 0 1 .5-.1l1.6.6c.2.1.4.3.4.5v.6c0 .3-.2.6-.5.7-1.6.5-3.3 0-4.6-1.2-1.2-1.2-1.7-2.9-1.2-4.5z"/>
  </>}/>
);
const MapPinIcon = (props) => (
  <Icon {...props} d={<>
    <path d="M12 22s7-6.2 7-12a7 7 0 1 0-14 0c0 5.8 7 12 7 12z"/>
    <circle cx="12" cy="10" r="2.5"/>
  </>}/>
);
const ArrowRightIcon = (props) => (
  <Icon {...props} d={<><line x1="5" y1="12" x2="19" y2="12"/><polyline points="13 6 19 12 13 18"/></>}/>
);

// ---------- Wordmark — stacked MAZZU/MAZZU ----------
function Wordmark({ size = "sm" }) {
  const fs = size === "lg" ? 16 : 11;
  const lh = size === "lg" ? 0.95 : 0.92;
  const tr = size === "lg" ? 4 : 2.4;
  return (
    <span style={{
      display: "inline-flex",
      flexDirection: "column",
      lineHeight: lh,
      fontSize: fs,
      fontWeight: 600,
      letterSpacing: `${tr}px`,
      textTransform: "uppercase",
      fontFamily: "'Inter', sans-serif",
      paddingLeft: 1,
    }}>
      <span>MAZZU</span>
      <span>MAZZU</span>
    </span>
  );
}

// ---------- Large Editorial Mark (for CTA section) ----------
function EditorialMark() {
  return (
    <span style={{
      display: "inline-flex",
      flexDirection: "column",
      alignItems: "center",
      lineHeight: 0.95,
      fontFamily: "'Instrument Serif', serif",
      fontStyle: "italic",
      fontSize: 44,
      color: "hsla(0,0%,100%,0.95)",
    }}>
      <span>Mazzu</span>
      <span style={{ marginTop: -2 }}>Mazzu</span>
    </span>
  );
}

// ---------- Avatar Stack ----------
function AvatarStack() {
  return (
    <div style={{ display: "flex", alignItems: "center", marginRight: 12 }}>
      <div className="avatar-ring avatar-1" style={{ marginRight: -8, zIndex: 3 }}/>
      <div className="avatar-ring avatar-2" style={{ marginRight: -8, zIndex: 2 }}/>
      <div className="avatar-ring avatar-3" style={{ zIndex: 1 }}/>
    </div>
  );
}

// ---------- Navbar (cinematic-style pill) ----------
function Navbar() {
  const [open, setOpen] = useState(false);
  const links = [
    { label: "Home", href: "#" },
    { label: "Beratung", href: "#beratung" },
    { label: "Story", href: "#story" },
    { label: "Labels", href: "#labels" },
    { label: "Store", href: "store.html" },
  ];
  const close = () => setOpen(false);

  return (
    <nav style={{
      position: "fixed", top: 16, left: 0, right: 0, zIndex: 50,
      display: "flex", alignItems: "center", justifyContent: "space-between",
      padding: "0 clamp(20px, 6vw, 64px)",
    }}>
      {/* Left: circular liquid-glass logo with italic serif 'MM' */}
      <a href="index.html" className="liquid-glass" style={{
        width: 48, height: 48, borderRadius: 9999,
        display: "inline-flex", alignItems: "center", justifyContent: "center",
        textDecoration: "none", flexShrink: 0
      }} aria-label="MAZZU MAZZU Home">
        <span style={{
          fontFamily: "'Instrument Serif', serif",
          fontStyle: "italic",
          fontSize: 20,
          lineHeight: 1,
          color: "white",
          marginTop: -1,
          letterSpacing: "-1px"
        }}>MM</span>
      </a>

      {/* Center: pill nav (desktop only) */}
      <div className="hidden-md liquid-glass" style={{
        padding: "6px 6px", borderRadius: 9999,
        display: "flex", alignItems: "center", gap: 0
      }}>
        {links.map((l) => (
          <a key={l.label} href={l.href} className="font-body" style={{
            padding: "8px 14px", fontSize: 13, fontWeight: 500,
            color: "hsla(0,0%,100%,0.9)", textDecoration: "none",
            borderRadius: 9999
          }}>{l.label}</a>
        ))}
        <a href="#kontakt" style={{
          background: "white", color: "black",
          padding: "8px 16px", borderRadius: 9999,
          fontSize: 13, fontWeight: 500, textDecoration: "none",
          display: "inline-flex", alignItems: "center", gap: 6,
          whiteSpace: "nowrap", marginLeft: 6
        }}>
          Termin
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor"
               strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <line x1="7" y1="17" x2="17" y2="7"/>
            <polyline points="7 7 17 7 17 17"/>
          </svg>
        </a>
      </div>

      {/* Right: Instagram + mobile menu toggle */}
      <div style={{ display: "flex", gap: 8, alignItems: "center", flexShrink: 0 }}>
        <a href="https://www.instagram.com/mazzumazzustore/" target="_blank" rel="noopener noreferrer"
           className="liquid-glass" style={navIconBtn} aria-label="Instagram">
          <InstagramIcon size={16}/>
        </a>
        <button type="button" onClick={() => setOpen((o) => !o)}
          className="show-md liquid-glass"
          aria-label={open ? "Menü schließen" : "Menü öffnen"} aria-expanded={open}
          style={{
            width: 38, height: 38, borderRadius: 9999,
            alignItems: "center", justifyContent: "center",
            color: "hsla(0,0%,100%,0.9)", cursor: "pointer",
            border: "none", background: "rgba(255,255,255,0.01)", padding: 0
          }}>
          {open ? (
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                 strokeWidth="1.8" strokeLinecap="round">
              <line x1="6" y1="6" x2="18" y2="18"/><line x1="18" y1="6" x2="6" y2="18"/>
            </svg>
          ) : (
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                 strokeWidth="1.8" strokeLinecap="round">
              <line x1="3.5" y1="7" x2="20.5" y2="7"/>
              <line x1="3.5" y1="12" x2="20.5" y2="12"/>
              <line x1="3.5" y1="17" x2="20.5" y2="17"/>
            </svg>
          )}
        </button>
      </div>

      {/* Mobile menu: dim backdrop + glass panel */}
      {open ? (
        <React.Fragment>
          <div onClick={close} style={{
            position: "fixed", inset: 0, zIndex: -1,
            background: "rgba(0,0,0,0.55)",
            WebkitBackdropFilter: "blur(2px)", backdropFilter: "blur(2px)"
          }}/>
          <div className="liquid-glass" style={{
            position: "absolute", top: 64,
            left: "clamp(20px, 6vw, 64px)", right: "clamp(20px, 6vw, 64px)",
            borderRadius: 24, padding: 14,
            display: "flex", flexDirection: "column", gap: 2
          }}>
            {links.map((l) => (
              <a key={l.label} href={l.href} onClick={close} className="font-body" style={{
                padding: "14px 16px", fontSize: 16, fontWeight: 500,
                color: "hsla(0,0%,100%,0.92)", textDecoration: "none", borderRadius: 14
              }}>{l.label}</a>
            ))}
            <a href="#kontakt" onClick={close} style={{
              marginTop: 6, background: "white", color: "black",
              padding: "15px 18px", borderRadius: 14, fontSize: 15, fontWeight: 600,
              textDecoration: "none",
              display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 8
            }}>
              Termin vereinbaren
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                   strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                <line x1="7" y1="17" x2="17" y2="7"/><polyline points="7 7 17 7 17 17"/>
              </svg>
            </a>
          </div>
        </React.Fragment>
      ) : null}
    </nav>
  );
}

const navIconBtn = {
  width: 38, height: 38, borderRadius: 9999,
  display: "inline-flex", alignItems: "center", justifyContent: "center",
  color: "hsla(0,0%,100%,0.85)", cursor: "pointer",
  border: "none",
};

window.MazzuShared = {
  Reveal, Wordmark, EditorialMark, AvatarStack, Navbar,
  InstagramIcon, FacebookIcon, WhatsappIcon, MapPinIcon, ArrowRightIcon
};
