/* global React */
// ============================================
// QUIROMANTE — Shared visual components
// Starfield, animated hand, icons, sacred geometry
// ============================================

const { useEffect, useMemo, useRef, useState } = React;

// --- Starfield background ----------------------------------
function Starfield({ count = 60 }) {
  const stars = useMemo(() => {
    const arr = [];
    for (let i = 0; i < count; i++) {
      arr.push({
        id: i,
        top: Math.random() * 100,
        left: Math.random() * 100,
        size: Math.random() > 0.85 ? "lg" : "",
        gold: Math.random() > 0.92,
        dur: 3 + Math.random() * 4,
        delay: Math.random() * 4,
      });
    }
    return arr;
  }, [count]);

  return (
    <div className="bg-stars" aria-hidden="true">
      {stars.map((s) => (
        <span
          key={s.id}
          className={`star ${s.size} ${s.gold ? "gold" : ""}`}
          style={{
            top: `${s.top}%`,
            left: `${s.left}%`,
            "--dur": `${s.dur}s`,
            "--delay": `${s.delay}s`,
          }}
        />
      ))}
    </div>
  );
}

// --- Wordmark ----------------------------------------------
function Wordmark() {
  return (
    <div className="wordmark">
      <span className="wordmark-dot" />
      <span>Quiromante</span>
      <span className="wordmark-dot" />
    </div>
  );
}

// --- Icons -------------------------------------------------
const Icon = {
  Camera: (p) => (
    <svg viewBox="0 0 24 24" width="28" height="28" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}>
      <path d="M3 8.5C3 7.4 3.9 6.5 5 6.5h2.2L8.5 4.5h7l1.3 2H19c1.1 0 2 .9 2 2V18c0 1.1-.9 2-2 2H5c-1.1 0-2-.9-2-2V8.5Z" />
      <circle cx="12" cy="13" r="3.6" />
    </svg>
  ),
  Lock: (p) => (
    <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.5" {...p}>
      <rect x="5" y="11" width="14" height="9" rx="2" />
      <path d="M8 11V8a4 4 0 0 1 8 0v3" />
    </svg>
  ),
  ShieldCheck: (p) => (
    <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.5" {...p}>
      <path d="M12 3l8 3v5c0 5-3.5 8.5-8 10-4.5-1.5-8-5-8-10V6l8-3Z" />
      <path d="M9 12l2 2 4-4" />
    </svg>
  ),
  Sparkle: (p) => (
    <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" {...p}>
      <path d="M12 2l1.6 6.4L20 10l-6.4 1.6L12 18l-1.6-6.4L4 10l6.4-1.6L12 2Z" />
    </svg>
  ),
  Stars: (p) => (
    <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.4" {...p}>
      <path d="M12 3l1.5 4 4 1.5-4 1.5L12 14l-1.5-4-4-1.5 4-1.5L12 3Z" fill="currentColor" />
      <path d="M18 14l.8 2 2 .8-2 .8L18 19.6l-.8-2-2-.8 2-.8L18 14Z" fill="currentColor" opacity="0.7" />
      <path d="M5.5 15l.6 1.5L7.6 17l-1.5.6L5.5 19l-.6-1.5L3.4 17l1.5-.6L5.5 15Z" fill="currentColor" opacity="0.6" />
    </svg>
  ),
  ArrowLeft: (p) => (
    <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.5" {...p}>
      <path d="M15 6l-6 6 6 6" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  ArrowRight: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.6" {...p}>
      <path d="M5 12h14M13 6l6 6-6 6" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  Share: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.5" {...p}>
      <circle cx="6" cy="12" r="2.2" />
      <circle cx="18" cy="6" r="2.2" />
      <circle cx="18" cy="18" r="2.2" />
      <path d="M8 11l8-4M8 13l8 4" strokeLinecap="round" />
    </svg>
  ),
  WhatsApp: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" {...p}>
      <path d="M12.04 2C6.58 2 2.15 6.43 2.15 11.89c0 1.94.55 3.75 1.5 5.3L2 22l4.95-1.6c1.5.82 3.23 1.29 5.09 1.29 5.46 0 9.89-4.43 9.89-9.89S17.5 2 12.04 2Zm5.74 14.06c-.24.68-1.17 1.24-1.93 1.4-.52.11-1.21.2-3.54-.74-2.97-1.23-4.89-4.25-5.04-4.45-.15-.2-1.2-1.6-1.2-3.05s.76-2.17 1.03-2.47c.27-.3.59-.37.78-.37l.56.01c.18 0 .42-.07.66.5.24.59.82 2.04.89 2.19.07.15.12.32.02.52-.1.2-.15.32-.3.5-.15.18-.32.4-.46.54-.15.15-.31.31-.13.61.18.3.79 1.31 1.7 2.12 1.17 1.04 2.16 1.36 2.46 1.51.3.15.48.13.66-.08.18-.2.76-.88.96-1.18.2-.3.4-.25.68-.15.27.1 1.72.81 2.02.96.3.15.5.22.57.35.07.12.07.72-.17 1.4Z" />
    </svg>
  ),
  Lifeline: (p) => (
    <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.3" {...p}>
      <path d="M6 4C8 9 7 14 11 20" strokeLinecap="round" />
      <circle cx="6" cy="4" r="1" fill="currentColor" />
      <circle cx="11" cy="20" r="1" fill="currentColor" />
      <path d="M14 8l1.5 1.5L17 8" strokeLinecap="round" opacity="0.6" />
    </svg>
  ),
  Heartline: (p) => (
    <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.3" {...p}>
      <path d="M3 9c4 0 6 3 9 3s5-2 9-3" strokeLinecap="round" />
      <path d="M12 14.5c.6.9 1.8 1.5 3 1.5 1.7 0 3-1.3 3-3 0-2-2-2.5-3-1.5-1-1-3-.5-3 1.5 0 .5.1 1 .3 1.5h-.3Z" opacity="0.85" />
    </svg>
  ),
  Fate: (p) => (
    <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.3" {...p}>
      <path d="M12 3v18" strokeLinecap="round" />
      <path d="M12 8l-3-3M12 12l3-3M12 16l-3-3" strokeLinecap="round" opacity="0.7" />
      <circle cx="12" cy="3" r="1" fill="currentColor" />
    </svg>
  ),
  Gift: (p) => (
    <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.3" {...p}>
      <path d="M12 3l2 3-2 1-2-1 2-3Z" fill="currentColor" />
      <path d="M5 9h14v3H5z" />
      <path d="M6 12v8h12v-8M12 9v11" />
    </svg>
  ),
  Moon: (p) => (
    <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.3" {...p}>
      <path d="M19 14A8 8 0 0 1 10 5a7 7 0 1 0 9 9Z" strokeLinejoin="round" />
      <circle cx="6" cy="7" r="0.6" fill="currentColor" />
      <circle cx="15" cy="18" r="0.6" fill="currentColor" />
    </svg>
  ),
  Pix: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" {...p}>
      <path d="M12 2.5l3.5 3.5L12 9.5 8.5 6 12 2.5Zm-5.5 5.5L10 11.5 6.5 15 3 11.5 6.5 8Zm11 0L21 11.5 17.5 15 14 11.5 17.5 8ZM12 13.5l3.5 3.5L12 20.5 8.5 17 12 13.5Z" />
    </svg>
  ),
  Card: (p) => (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.5" {...p}>
      <rect x="3" y="6" width="18" height="13" rx="2" />
      <path d="M3 10h18" />
      <path d="M7 15h4" strokeLinecap="round" />
    </svg>
  ),
};

// --- Hand SVG: open palm with glowing chiromantic lines ----
function MysticHand({ size = 280, animated = true }) {
  return (
    <div className="hand-stage" style={{ width: size, height: size }}>
      <div className="hand-aura" />
      <svg
        viewBox="0 0 200 240"
        width={size}
        height={size}
        fill="none"
        style={{ position: "relative", zIndex: 1 }}
        aria-hidden="true"
      >
        <defs>
          <linearGradient id="palmGrad" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#2A2058" stopOpacity="0.95" />
            <stop offset="100%" stopColor="#15102E" stopOpacity="0.95" />
          </linearGradient>
          <linearGradient id="goldLine" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor="#E8C975" />
            <stop offset="100%" stopColor="#C9A84C" />
          </linearGradient>
          <radialGradient id="palmCenter" cx="0.5" cy="0.55" r="0.5">
            <stop offset="0%" stopColor="#C9A84C" stopOpacity="0.25" />
            <stop offset="100%" stopColor="#C9A84C" stopOpacity="0" />
          </radialGradient>
          <filter id="goldGlow" x="-50%" y="-50%" width="200%" height="200%">
            <feGaussianBlur stdDeviation="2" result="blur" />
            <feMerge>
              <feMergeNode in="blur" />
              <feMergeNode in="SourceGraphic" />
            </feMerge>
          </filter>
        </defs>

        {/* Hand silhouette — stylized open palm */}
        <g>
          {/* Palm body */}
          <path
            d="M60 130
               C 58 115, 56 105, 56 95
               L 56 70
               C 56 64, 60 60, 66 60
               C 70 60, 73 63, 73 68
               L 73 95
               L 74 50
               C 74 44, 78 40, 84 40
               C 90 40, 93 44, 93 50
               L 93 95
               L 95 42
               C 95 36, 99 32, 105 32
               C 111 32, 114 36, 114 42
               L 114 96
               L 117 52
               C 117 46, 121 42, 127 42
               C 133 42, 136 46, 136 52
               L 136 100
               L 140 78
               C 142 72, 146 70, 150 72
               C 154 74, 156 78, 154 84
               L 144 124
               C 142 132, 140 144, 140 158
               C 140 178, 132 200, 110 210
               C 90 218, 70 210, 62 192
               C 56 178, 58 158, 60 130 Z"
            fill="url(#palmGrad)"
            stroke="#3B2F6F"
            strokeWidth="1"
          />
          {/* Center glow */}
          <ellipse cx="100" cy="155" rx="34" ry="40" fill="url(#palmCenter)" />
        </g>

        {/* Chiromantic lines */}
        <g
          filter="url(#goldGlow)"
          style={animated ? { animation: "linePulse 4s ease-in-out infinite" } : undefined}
        >
          {/* Life line — curves around thumb */}
          <path
            d="M82 90 Q 70 120, 72 155 Q 76 185, 92 200"
            stroke="url(#goldLine)"
            strokeWidth="1.6"
            fill="none"
            strokeLinecap="round"
            strokeDasharray="200"
            style={animated ? { animation: "lineDraw 2.4s var(--ease-out) forwards", strokeDashoffset: 200 } : undefined}
          />
          {/* Head line — horizontal across middle palm */}
          <path
            d="M78 130 Q 100 134, 128 138"
            stroke="url(#goldLine)"
            strokeWidth="1.4"
            fill="none"
            strokeLinecap="round"
            strokeDasharray="120"
            style={animated ? { animation: "lineDraw 2.6s 0.3s var(--ease-out) forwards", strokeDashoffset: 120 } : undefined}
          />
          {/* Heart line — upper curve */}
          <path
            d="M76 108 Q 100 100, 132 115"
            stroke="url(#goldLine)"
            strokeWidth="1.4"
            fill="none"
            strokeLinecap="round"
            strokeDasharray="120"
            style={animated ? { animation: "lineDraw 2.6s 0.6s var(--ease-out) forwards", strokeDashoffset: 120 } : undefined}
          />
          {/* Fate line — vertical from wrist up */}
          <path
            d="M105 200 Q 102 170, 104 130 Q 105 110, 108 95"
            stroke="url(#goldLine)"
            strokeWidth="1.2"
            fill="none"
            strokeLinecap="round"
            strokeDasharray="150"
            style={animated ? { animation: "lineDraw 2.8s 0.9s var(--ease-out) forwards", strokeDashoffset: 150 } : undefined}
            opacity="0.85"
          />
        </g>

        {/* Decorative star marks at line endings */}
        <g fill="#E8C975" opacity="0.9">
          <circle cx="82" cy="90" r="1.4" />
          <circle cx="92" cy="200" r="1.4" />
          <circle cx="128" cy="138" r="1.2" />
          <circle cx="132" cy="115" r="1.2" />
        </g>

        {/* Tiny stars floating around */}
        <g fill="#E8C975" opacity="0.6">
          <circle cx="40" cy="60" r="0.8">
            <animate attributeName="opacity" values="0.3;1;0.3" dur="3s" repeatCount="indefinite" />
          </circle>
          <circle cx="160" cy="80" r="0.8">
            <animate attributeName="opacity" values="0.3;1;0.3" dur="4s" begin="1s" repeatCount="indefinite" />
          </circle>
          <circle cx="170" cy="180" r="1">
            <animate attributeName="opacity" values="0.3;1;0.3" dur="3.5s" begin="0.5s" repeatCount="indefinite" />
          </circle>
          <circle cx="30" cy="200" r="0.8">
            <animate attributeName="opacity" values="0.3;1;0.3" dur="4.2s" begin="1.5s" repeatCount="indefinite" />
          </circle>
        </g>
      </svg>
    </div>
  );
}

// --- Sacred geometry (loading screen centerpiece) ----------
function SacredGeometry({ size = 240 }) {
  return (
    <svg viewBox="0 0 240 240" width={size} height={size} aria-hidden="true">
      <defs>
        <radialGradient id="sgCenter" cx="0.5" cy="0.5" r="0.5">
          <stop offset="0%" stopColor="#E8C975" stopOpacity="0.45" />
          <stop offset="60%" stopColor="#C9A84C" stopOpacity="0.1" />
          <stop offset="100%" stopColor="#C9A84C" stopOpacity="0" />
        </radialGradient>
        <linearGradient id="sgStroke" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#E8C975" />
          <stop offset="100%" stopColor="#8E7530" />
        </linearGradient>
      </defs>

      {/* Glowing center */}
      <circle cx="120" cy="120" r="80" fill="url(#sgCenter)">
        <animate attributeName="r" values="70;90;70" dur="4s" repeatCount="indefinite" />
        <animate attributeName="opacity" values="0.6;1;0.6" dur="4s" repeatCount="indefinite" />
      </circle>

      {/* Outer hexagram (Star of David) — rotating */}
      <g
        stroke="url(#sgStroke)"
        strokeWidth="0.8"
        fill="none"
        opacity="0.7"
        style={{ transformOrigin: "120px 120px", animation: "spin-slow 40s linear infinite" }}
      >
        <polygon points="120,40 190,160 50,160" />
        <polygon points="120,200 50,80 190,80" />
      </g>

      {/* Inner hexagon */}
      <g
        stroke="#C9A84C"
        strokeWidth="0.8"
        fill="none"
        opacity="0.85"
        style={{ transformOrigin: "120px 120px", animation: "spin-slow 28s linear infinite reverse" }}
      >
        <polygon points="120,70 163,95 163,145 120,170 77,145 77,95" />
        <polygon points="120,85 150,102 150,138 120,155 90,138 90,102" />
      </g>

      {/* Flower of life — petal arrangement */}
      <g
        stroke="#E8C975"
        strokeWidth="0.7"
        fill="none"
        opacity="0.6"
        style={{ transformOrigin: "120px 120px", animation: "spin-slow 60s linear infinite" }}
      >
        <circle cx="120" cy="120" r="22" />
        <circle cx="120" cy="98" r="22" />
        <circle cx="120" cy="142" r="22" />
        <circle cx="139" cy="109" r="22" />
        <circle cx="139" cy="131" r="22" />
        <circle cx="101" cy="109" r="22" />
        <circle cx="101" cy="131" r="22" />
      </g>

      {/* Center dot */}
      <circle cx="120" cy="120" r="3" fill="#E8C975">
        <animate attributeName="r" values="2;4;2" dur="2s" repeatCount="indefinite" />
      </circle>

      {/* Outer ring with tick marks */}
      <g
        stroke="#C9A84C"
        strokeWidth="0.5"
        opacity="0.5"
        style={{ transformOrigin: "120px 120px", animation: "spin-slow 80s linear infinite" }}
      >
        <circle cx="120" cy="120" r="115" fill="none" strokeDasharray="2 6" />
      </g>
    </svg>
  );
}

// --- Decorative divider with star --------------------------
function Divider({ label = "✦" }) {
  return (
    <div className="result-divider">
      <span className="result-divider-mark">{label}</span>
    </div>
  );
}

// Expose to other Babel scripts
Object.assign(window, {
  Starfield,
  Wordmark,
  Icon,
  MysticHand,
  SacredGeometry,
  Divider,
});
