/* global React */
const { useRef, useEffect, useState } = React;

/* ---------- Inline icon set (Lucide-style, 1.75 stroke) ---------- */
const ICON_PATHS = {
  arrow: '<path d="M5 12h14M13 6l6 6-6 6"/>',
  database: '<ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5v14c0 1.7 4 3 9 3s9-1.3 9-3V5"/><path d="M3 12c0 1.7 4 3 9 3s9-1.3 9-3"/>',
  cpu: '<rect x="6" y="6" width="12" height="12" rx="2"/><path d="M9 1v3M15 1v3M9 20v3M15 20v3M1 9h3M1 15h3M20 9h3M20 15h3"/><rect x="9.5" y="9.5" width="5" height="5" rx="1"/>',
  gauge: '<path d="M12 14l4-4"/><path d="M3.3 19a9 9 0 1 1 17.4 0"/><circle cx="12" cy="14" r="1.4"/>',
  shield: '<path d="M12 3l7 3v5c0 5-3.5 8-7 10-3.5-2-7-5-7-10V6z"/><path d="M9 12l2 2 4-4"/>',
  network: '<circle cx="5" cy="6" r="2.4"/><circle cx="19" cy="6" r="2.4"/><circle cx="12" cy="18" r="2.4"/><path d="M6.7 7.6 10.6 16M17.3 7.6 13.4 16M7.4 6h9.2"/>',
  spark: '<path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M18.4 5.6l-2.8 2.8M8.4 15.6l-2.8 2.8"/><circle cx="12" cy="12" r="2.6"/>',
  compass: '<circle cx="12" cy="12" r="9"/><path d="M15.8 8.2 13.2 13.2 8.2 15.8 10.8 10.8z"/>',
  server: '<rect x="3" y="4" width="18" height="7" rx="2"/><rect x="3" y="13" width="18" height="7" rx="2"/><path d="M7 7.5h.01M7 16.5h.01"/>',
  grid: '<rect x="4" y="4" width="7" height="7" rx="1.4"/><rect x="13" y="4" width="7" height="7" rx="1.4"/><rect x="4" y="13" width="7" height="7" rx="1.4"/><rect x="13" y="13" width="7" height="7" rx="1.4"/>',
  megaphone: '<path d="M4 10v4a1 1 0 0 0 1 1h3l5 4V5L8 9H5a1 1 0 0 0-1 1z"/><path d="M16.5 9a3.5 3.5 0 0 1 0 6"/>',
  user: '<circle cx="12" cy="8" r="3.4"/><path d="M5.5 19.5a6.5 6.5 0 0 1 13 0"/>',
  chart: '<path d="M3 20h18"/><path d="M6 20v-6M11 20V6M16 20v-9"/>',
  lock: '<rect x="5" y="11" width="14" height="9" rx="2"/><path d="M8 11V8a4 4 0 0 1 8 0v3"/>',
  scale: '<path d="M12 4v16M7.5 20h9"/><path d="M5 8h14M12 4.5 5 8M12 4.5 19 8"/><path d="M5 8 2.5 13.5a2.6 2.6 0 0 0 5 0L5 8z"/><path d="M19 8l-2.5 5.5a2.6 2.6 0 0 0 5 0L19 8z"/>',
  calendar: '<rect x="4" y="5" width="16" height="16" rx="2"/><path d="M4 9.5h16M8.5 3v4M15.5 3v4"/>',
  check: '<path d="M20 6 9 17l-5-5"/>',
  menu: '<path d="M3 6h18M3 12h18M3 18h18"/>',
  close: '<path d="M18 6 6 18M6 6l12 12"/>',
  sun: '<circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M2 12h2M20 12h2M5 5l1.5 1.5M17.5 17.5 19 19M19 5l-1.5 1.5M6.5 17.5 5 19"/>',
  moon: '<path d="M20 14.5A8 8 0 1 1 9.5 4 6.2 6.2 0 0 0 20 14.5z"/>',
  quote: '<path d="M7 7h4v6c0 2-1.5 3.5-4 4M14 7h4v6c0 2-1.5 3.5-4 4"/>',
};
function Icon({ name, size = 22, color = 'currentColor', stroke = 1.75, style }) {
  return <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
    strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round" style={style}
    dangerouslySetInnerHTML={{ __html: ICON_PATHS[name] || '' }} />;
}

/* ---------- Eyebrow / kicker ---------- */
function Eyebrow({ children, color, style }) {
  return <span className="ds-eyebrow" style={{ color, ...style }}>{children}</span>;
}

/* ---------- Button ---------- */
function Button({ children, variant = 'primary', onClick, icon, size = 'md', as = 'button', href }) {
  const cls = `ds-btn ds-btn-${variant} ds-btn-${size}`;
  const inner = <>{children}{icon && <Icon name={icon} size={size === 'lg' ? 19 : 17} />}</>;
  if (as === 'a') return <a className={cls} href={href} onClick={onClick}>{inner}</a>;
  return <button className={cls} onClick={onClick}>{inner}</button>;
}

/* ---------- Badge ---------- */
function Badge({ children, tone = 'magenta', solid = false }) {
  return <span className={`ds-badge ds-badge-${tone}${solid ? ' solid' : ''}`}>{children}</span>;
}

/* ---------- IconBadge (gradient-soft tile) ---------- */
function IconBadge({ name, color = '#FF1893', tint = 'rgba(255,24,147,.12)' }) {
  return <span className="ds-iconbadge" style={{ background: tint }}><Icon name={name} size={22} color={color} /></span>;
}

/* ---------- Animated constellation background (canvas) ---------- */
function Constellation({ density = 0.00012, className, style }) {
  const ref = useRef(null);
  useEffect(() => {
    const canvas = ref.current; if (!canvas) return;
    const ctx = canvas.getContext('2d');
    const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    let w, h, nodes = [], raf, t = 0;
    const COLORS = ['#9C64FF', '#FF1893', '#49EEF4'];
    function resize() {
      const r = canvas.parentElement.getBoundingClientRect();
      w = canvas.width = r.width * devicePixelRatio;
      h = canvas.height = r.height * devicePixelRatio;
      canvas.style.width = r.width + 'px'; canvas.style.height = r.height + 'px';
      const count = Math.max(26, Math.min(70, Math.floor(r.width * r.height * density)));
      nodes = Array.from({ length: count }, () => ({
        x: Math.random() * w, y: Math.random() * h,
        vx: (Math.random() - .5) * 0.12 * devicePixelRatio,
        vy: (Math.random() - .5) * 0.12 * devicePixelRatio,
        c: COLORS[Math.floor(Math.random() * COLORS.length)],
        ph: Math.random() * Math.PI * 2,
      }));
    }
    resize();
    const ro = new ResizeObserver(resize); ro.observe(canvas.parentElement);
    const LINK = 150 * devicePixelRatio;
    function draw() {
      t += 0.016;
      ctx.clearRect(0, 0, w, h);
      for (let i = 0; i < nodes.length; i++) {
        const a = nodes[i];
        for (let j = i + 1; j < nodes.length; j++) {
          const b = nodes[j];
          const dx = a.x - b.x, dy = a.y - b.y, d = Math.hypot(dx, dy);
          if (d < LINK) {
            const o = (1 - d / LINK) * 0.5;
            ctx.strokeStyle = a.c; ctx.globalAlpha = o; ctx.lineWidth = 1 * devicePixelRatio;
            ctx.beginPath(); ctx.moveTo(a.x, a.y); ctx.lineTo(b.x, b.y); ctx.stroke();
          }
        }
      }
      ctx.globalAlpha = 1;
      for (const n of nodes) {
        if (!reduced) { n.x += n.vx; n.y += n.vy;
          if (n.x < 0 || n.x > w) n.vx *= -1;
          if (n.y < 0 || n.y > h) n.vy *= -1; }
        const tw = 0.6 + 0.4 * Math.sin(t * 1.2 + n.ph);
        ctx.fillStyle = n.c; ctx.globalAlpha = tw;
        ctx.beginPath(); ctx.arc(n.x, n.y, 2.1 * devicePixelRatio, 0, 7); ctx.fill();
        ctx.globalAlpha = tw * 0.28;
        ctx.beginPath(); ctx.arc(n.x, n.y, 6 * devicePixelRatio, 0, 7); ctx.fill();
      }
      ctx.globalAlpha = 1;
      raf = requestAnimationFrame(draw);
    }
    draw();
    return () => { cancelAnimationFrame(raf); ro.disconnect(); };
  }, []);
  return <canvas ref={ref} className={className} style={{ display: 'block', ...style }} />;
}

Object.assign(window, { Icon, Eyebrow, Button, Badge, IconBadge, Constellation });
