// Dentstyle — Decorations, icons, pearl orbs, gold arcs
const { useEffect, useRef, useState } = React;

// === Icons ===
const Icon = {
  Whatsapp: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" {...p}>
      <path d="M20.52 3.48A11.86 11.86 0 0 0 12 0C5.37 0 0 5.37 0 12c0 2.11.55 4.17 1.6 5.99L0 24l6.18-1.62A11.95 11.95 0 0 0 12 24c6.63 0 12-5.37 12-12 0-3.21-1.25-6.22-3.48-8.52ZM12 21.82c-1.86 0-3.69-.5-5.28-1.45l-.38-.22-3.67.96.98-3.58-.25-.37A9.83 9.83 0 0 1 2.18 12C2.18 6.58 6.58 2.18 12 2.18c2.62 0 5.09 1.02 6.95 2.87A9.77 9.77 0 0 1 21.82 12c0 5.42-4.4 9.82-9.82 9.82Zm5.39-7.36c-.29-.15-1.74-.86-2.01-.96-.27-.1-.47-.15-.66.15-.2.29-.76.96-.93 1.15-.17.2-.34.22-.63.07-.29-.15-1.23-.46-2.35-1.45-.87-.78-1.45-1.74-1.62-2.03-.17-.29-.02-.45.13-.6.13-.13.29-.34.44-.51.15-.17.2-.29.29-.49.1-.2.05-.37-.02-.51-.07-.15-.66-1.6-.9-2.18-.24-.58-.48-.5-.66-.51l-.56-.01c-.2 0-.51.07-.78.37-.27.29-1.03 1-1.03 2.45 0 1.45 1.05 2.84 1.2 3.04.15.2 2.07 3.17 5.03 4.45.7.3 1.25.48 1.68.62.7.22 1.34.19 1.85.12.56-.08 1.74-.71 1.99-1.4.25-.69.25-1.28.17-1.4-.07-.12-.27-.2-.56-.34Z"/>
    </svg>
  ),
  ArrowRight: (p) => (
    <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M5 12h14M13 6l6 6-6 6"/>
    </svg>
  ),
  ChevronL: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M15 18l-6-6 6-6"/></svg>
  ),
  ChevronR: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M9 6l6 6-6 6"/></svg>
  ),
  Menu: (p) => (
    <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" {...p}>
      <path d="M4 7h16M4 12h16M4 17h16"/>
    </svg>
  ),
  Close: (p) => (
    <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" {...p}>
      <path d="M6 6l12 12M18 6L6 18"/>
    </svg>
  ),
  Diamond: (p) => (
    <svg viewBox="0 0 32 32" width="28" height="28" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round" {...p}>
      <path d="M6 12l4-6h12l4 6-10 14L6 12z"/>
      <path d="M6 12h20M12 6l2 6-2 0M20 6l-2 6 2 0M16 26l-2-14h4l-2 14z" strokeWidth="1"/>
    </svg>
  ),
  Tooth: (p) => (
    <svg viewBox="0 0 32 32" width="28" height="28" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round" {...p}>
      <path d="M10 4c-3 0-5 2.5-5 6 0 2 .5 3 .8 5.5.4 3 .2 6 1.4 9 .8 2 1.6 3.5 2.6 3.5 1.4 0 1.6-3 2.4-5 .5-1.4 1.2-2 2.8-2 1.6 0 2.3.6 2.8 2 .8 2 1 5 2.4 5 1 0 1.8-1.5 2.6-3.5 1.2-3 1-6 1.4-9 .3-2.5.8-3.5.8-5.5 0-3.5-2-6-5-6-2 0-3 1-6 1s-4-1-6-1z"/>
    </svg>
  ),
  Shield: (p) => (
    <svg viewBox="0 0 32 32" width="28" height="28" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round" {...p}>
      <path d="M16 3l11 4v8c0 8-5 13-11 14C10 28 5 23 5 15V7l11-4z"/>
      <path d="M11.5 15.5l3 3 6-6.5" strokeWidth="1.4"/>
    </svg>
  ),
  Star: (p) => (
    <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor" {...p}>
      <path d="M12 2.5l2.9 6.4 7 .7-5.2 4.7 1.5 6.9L12 17.8l-6.2 3.4 1.5-6.9L2.1 9.6l7-.7L12 2.5z"/>
    </svg>
  ),
  Spark: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" {...p}>
      <path d="M12 1.5l1.6 6.6 6.6 1.6-6.6 1.6L12 17.9l-1.6-6.6-6.6-1.6 6.6-1.6L12 1.5z"/>
    </svg>
  ),
  Users: (p) => (
    <svg viewBox="0 0 32 32" width="28" height="28" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="11" r="4"/>
      <path d="M4 26c0-4 3.6-7 8-7s8 3 8 7"/>
      <circle cx="22" cy="9" r="3" strokeWidth="1"/>
      <path d="M28 22c0-3-2.6-5-6-5" strokeWidth="1"/>
    </svg>
  ),
  Layers: (p) => (
    <svg viewBox="0 0 32 32" width="28" height="28" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round" {...p}>
      <path d="M16 4L4 11l12 7 12-7-12-7z"/>
      <path d="M4 17l12 7 12-7M4 23l12 7 12-7" opacity=".6"/>
    </svg>
  ),
  ShieldCheck: (p) => (
    <svg viewBox="0 0 32 32" width="28" height="28" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round" {...p}>
      <path d="M16 3l10 4v9c0 7-5 12-10 13-5-1-10-6-10-13V7l10-4z"/>
      <path d="M11.5 16l3 3 6-6.5"/>
    </svg>
  ),
  Bell: (p) => (
    <svg viewBox="0 0 32 32" width="28" height="28" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round" {...p}>
      <path d="M16 5v3M9 22l-2 3h18l-2-3v-7a7 7 0 1 0-14 0v7zM13 28a3 3 0 0 0 6 0"/>
    </svg>
  ),
  Drag: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M8 7l-4 5 4 5M16 7l4 5-4 5"/>
    </svg>
  ),
  Instagram: (p) => (
    <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}>
      <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="currentColor"/>
    </svg>
  ),
  Facebook: (p) => (
    <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" {...p}>
      <path d="M13 22v-8h3l.5-4H13V7.5c0-1 .3-1.7 1.8-1.7H17V2.1C16.6 2 15.4 2 14 2c-3 0-5 1.8-5 5v3H6v4h3v8h4z"/>
    </svg>
  ),
  Tiktok: (p) => (
    <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" {...p}>
      <path d="M21 8.5a7.4 7.4 0 0 1-4.5-1.6v8.3a5.8 5.8 0 1 1-5.8-5.8c.3 0 .6 0 .9.1v3a2.9 2.9 0 1 0 2 2.7V2h3a4.4 4.4 0 0 0 4.4 4.4v2.1z"/>
    </svg>
  ),
};

// === Pearl orb ===
function Pearl({ size = 60, top, left, right, bottom, anim = "a", delay = 0, opacity = 1, style = {}, className = "" }) {
  const [tapped, setTapped] = React.useState(false);
  const handleTouch = () => {
    setTapped(true);
    setTimeout(() => setTapped(false), 750);
  };
  return (
    <div
      className={`pearl float-${anim}${tapped ? " pearl-tapped" : ""}${className ? " " + className : ""}`}
      aria-hidden="true"
      onTouchStart={handleTouch}
      style={{
        width: size, height: size,
        top, left, right, bottom,
        animationDelay: `${delay}s`,
        opacity,
        ...style,
      }}
    />
  );
}

// === Sparkle ===
function Spark({ top, left, right, bottom, size = 14, delay = 0, style = {} }) {
  return (
    <div className="spark" aria-hidden="true" style={{ top, left, right, bottom, width: size, height: size, animation: `twinkle 4s ${delay}s ease-in-out infinite`, ...style }}>
      <Icon.Spark width={size} height={size} />
    </div>
  );
}

// === Gold Arcs (SVG) ===
function GoldArc({ variant = 1, className = "" }) {
  if (variant === 1) {
    return (
      <div className={`arc arc-1 ${className}`} aria-hidden="true">
        <svg viewBox="0 0 600 600" preserveAspectRatio="xMidYMid meet">
          <defs>
            <linearGradient id="ga1" x1="0%" y1="0%" x2="100%" y2="100%">
              <stop offset="0%" stopColor="#D3B775" stopOpacity="0"/>
              <stop offset="50%" stopColor="#C6AC6F" stopOpacity="0.95"/>
              <stop offset="100%" stopColor="#A98C4D" stopOpacity="0.2"/>
            </linearGradient>
          </defs>
          <path
            d="M 80 540 Q 60 300 220 140 Q 380 -20 560 60"
            fill="none"
            stroke="url(#ga1)"
            strokeWidth="2.4"
            strokeLinecap="round"
          />
          <path
            d="M 110 540 Q 90 320 240 170 Q 380 30 540 110"
            fill="none"
            stroke="url(#ga1)"
            strokeWidth="1.2"
            strokeLinecap="round"
            opacity="0.7"
          />
        </svg>
      </div>
    );
  }
  return (
    <div className={`arc arc-2 ${className}`} aria-hidden="true">
      <svg viewBox="0 0 600 600" preserveAspectRatio="xMidYMid meet">
        <defs>
          <linearGradient id="ga2" x1="100%" y1="100%" x2="0%" y2="0%">
            <stop offset="0%" stopColor="#A98C4D" stopOpacity="0.2"/>
            <stop offset="50%" stopColor="#D3B775" stopOpacity="1"/>
            <stop offset="100%" stopColor="#C6AC6F" stopOpacity="0"/>
          </linearGradient>
        </defs>
        <path
          d="M 540 60 Q 600 280 460 460 Q 320 620 60 540"
          fill="none"
          stroke="url(#ga2)"
          strokeWidth="2.4"
          strokeLinecap="round"
        />
        <path
          d="M 520 90 Q 570 290 440 440 Q 320 580 100 510"
          fill="none"
          stroke="url(#ga2)"
          strokeWidth="1.2"
          strokeLinecap="round"
          opacity="0.6"
        />
      </svg>
    </div>
  );
}

// === Twinkle keyframes via injected style ===
(function injectKeyframes() {
  if (document.getElementById("dent-keyframes")) return;
  const s = document.createElement("style");
  s.id = "dent-keyframes";
  s.textContent = `
    @keyframes twinkle {
      0%, 100% { opacity: 0.25; transform: scale(0.9); }
      50% { opacity: 1; transform: scale(1.1); }
    }
    @keyframes drift-arc-1 {
      0%, 100% { transform: translate3d(0, 0, 0); }
      50% { transform: translate3d(-12px, 6px, 0); }
    }
    @keyframes drift-arc-2 {
      0%, 100% { transform: translate3d(0, 0, 0); }
      50% { transform: translate3d(10px, -8px, 0); }
    }
    .arc-1 { animation: drift-arc-1 14s ease-in-out infinite; }
    .arc-2 { animation: drift-arc-2 16s ease-in-out infinite; }
  `;
  document.head.appendChild(s);
})();

// === Scroll reveal hook (observa .reveal y .reveal-3d) ===
function useReveal() {
  useEffect(() => {
    const obs = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting) {
          e.target.classList.add("in");
          obs.unobserve(e.target);
        }
      });
    }, { threshold: 0.12, rootMargin: "0px 0px -40px 0px" });
    document.querySelectorAll(".reveal, .reveal-3d").forEach((el) => obs.observe(el));
    return () => obs.disconnect();
  }, []);
}

// === Tilt 3D (mouse en desktop / scroll ambient en móvil) ===
function Tilt3D({ children, className = "", style = {} }) {
  const ref = React.useRef(null);
  const rafRef = React.useRef(null);
  const [tilt, setTilt] = React.useState({ x: 0, y: 0 });

  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;

    const handleMouseMove = (e) => {
      if (window.innerWidth < 1024) return;
      if (rafRef.current) return;
      rafRef.current = requestAnimationFrame(() => {
        const rect = el.getBoundingClientRect();
        const xc = rect.width / 2;
        const yc = rect.height / 2;
        setTilt({
          x: ((yc - (e.clientY - rect.top)) / yc) * 9,
          y: (((e.clientX - rect.left) - xc) / xc) * 9,
        });
        rafRef.current = null;
      });
    };

    const handleMouseLeave = () => {
      if (rafRef.current) { cancelAnimationFrame(rafRef.current); rafRef.current = null; }
      setTilt({ x: 0, y: 0 });
    };

    const handleScroll = () => {
      if (window.innerWidth >= 1024) return;
      if (rafRef.current) return;
      rafRef.current = requestAnimationFrame(() => {
        const rect = el.getBoundingClientRect();
        const winH = window.innerHeight;
        if (rect.top < winH && rect.bottom > 0) {
          const diff = (winH / 2 - (rect.top + rect.height / 2)) / (winH / 2);
          setTilt({ x: diff * 6, y: 0 });
        }
        rafRef.current = null;
      });
    };

    el.addEventListener("mousemove", handleMouseMove);
    el.addEventListener("mouseleave", handleMouseLeave);
    window.addEventListener("scroll", handleScroll, { passive: true });

    return () => {
      el.removeEventListener("mousemove", handleMouseMove);
      el.removeEventListener("mouseleave", handleMouseLeave);
      window.removeEventListener("scroll", handleScroll);
      if (rafRef.current) cancelAnimationFrame(rafRef.current);
    };
  }, []);

  return (
    <div
      ref={ref}
      className={className}
      style={{
        ...style,
        transform: `perspective(1000px) rotateX(${tilt.x}deg) rotateY(${tilt.y}deg) scale3d(1.015, 1.015, 1.015)`,
        transition: "transform 0.2s cubic-bezier(0.25, 1, 0.5, 1)",
        transformStyle: "preserve-3d",
      }}
    >
      {children}
    </div>
  );
}

Object.assign(window, { Icon, Pearl, Spark, GoldArc, useReveal, Tilt3D });
