// Dalflow — abstract art components (flow / orbit / lattice / pulse / mesh)
// Each renders into a full-bleed slot with positioned art.

const { useEffect, useRef, useState } = React;

// ===== Logo mark =====
function LogoMark({ size = 22 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 22 22" fill="none" aria-hidden="true">
      <rect width="22" height="22" rx="7" fill="var(--accent)" />
      <path
        d="M 5 7 Q 11 4 17 7 Q 11 10 5 13 Q 11 16 17 13"
        stroke="var(--accent-ink)"
        strokeWidth="1.6"
        strokeLinecap="round"
        fill="none"
      />
    </svg>
  );
}

// ===== Flow art: organic curves =====
function FlowArt({ seed = 0, hue = 'accent' }) {
  // 5 flowing curves
  const c = hue === 'warm' ? 'var(--warm)' : 'var(--accent)';
  return (
    <svg viewBox="0 0 400 500" preserveAspectRatio="xMidYMid slice" aria-hidden="true">
      <defs>
        <linearGradient id={`flow-g-${seed}`} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor={c} stopOpacity="0.9" />
          <stop offset="100%" stopColor={c} stopOpacity="0.05" />
        </linearGradient>
        <filter id={`flow-blur-${seed}`}>
          <feGaussianBlur stdDeviation="0.6" />
        </filter>
      </defs>
      <rect width="400" height="500" fill="var(--bg-rise)" />
      {/* Glow */}
      <circle cx={120 + seed * 40} cy={180 + seed * 30} r="180" fill={c} opacity="0.18" filter="url(#flow-blur-0)" />
      {/* Curves */}
      {Array.from({ length: 18 }).map((_, i) => {
        const y = 80 + i * 18 + seed * 5;
        const o = 0.15 + (i % 6) * 0.08;
        return (
          <path
            key={i}
            d={`M -20 ${y} Q 100 ${y - 40 + seed * 10} 200 ${y + (i % 3) * 8} T 420 ${y - 20}`}
            stroke={c}
            strokeWidth={i % 4 === 0 ? 1.2 : 0.6}
            fill="none"
            opacity={o}
          />
        );
      })}
      {/* Accent dot */}
      <circle cx="260" cy="340" r="6" fill={c} />
      <circle cx="260" cy="340" r="14" fill="none" stroke={c} strokeWidth="1" opacity="0.5" />
      <circle cx="260" cy="340" r="28" fill="none" stroke={c} strokeWidth="0.5" opacity="0.3" />
    </svg>
  );
}

// ===== Orbit art: concentric rings + nodes =====
function OrbitArt({ seed = 0 }) {
  const c = 'var(--accent)';
  return (
    <svg viewBox="0 0 400 500" preserveAspectRatio="xMidYMid slice" aria-hidden="true">
      <rect width="400" height="500" fill="var(--bg-rise)" />
      <circle cx="200" cy="250" r="180" fill={c} opacity="0.12" />
      <g transform="translate(200 250)">
        {[60, 110, 160, 210].map((r, i) => (
          <circle
            key={i}
            cx="0" cy="0" r={r}
            fill="none"
            stroke={c}
            strokeWidth={i === 1 ? 1.4 : 0.6}
            opacity={0.5 - i * 0.08}
            strokeDasharray={i % 2 === 0 ? '0' : '2 6'}
          />
        ))}
        {/* Nodes around the ring */}
        {Array.from({ length: 8 }).map((_, i) => {
          const ang = (i / 8) * Math.PI * 2 + seed;
          const r = 110;
          const x = Math.cos(ang) * r;
          const y = Math.sin(ang) * r;
          return (
            <g key={i}>
              <circle cx={x} cy={y} r={i === 2 ? 7 : 3} fill={c} />
              {i === 2 && <circle cx={x} cy={y} r="12" fill="none" stroke={c} strokeWidth="1" opacity="0.5" />}
            </g>
          );
        })}
        {/* Center core */}
        <circle cx="0" cy="0" r="20" fill={c} />
        <circle cx="0" cy="0" r="32" fill="none" stroke={c} strokeWidth="1" opacity="0.6" />
      </g>
    </svg>
  );
}

// ===== Lattice art: structured grid =====
function LatticeArt({ seed = 0 }) {
  const c = 'var(--accent)';
  const cells = [];
  for (let r = 0; r < 10; r++) {
    for (let col = 0; col < 8; col++) {
      const fill = ((r * 7 + col * 3 + seed) % 11) < 2;
      cells.push({ r, col, fill });
    }
  }
  return (
    <svg viewBox="0 0 400 500" preserveAspectRatio="xMidYMid slice" aria-hidden="true">
      <rect width="400" height="500" fill="var(--bg-rise)" />
      <g transform="translate(40 50)">
        {cells.map(({ r, col, fill }) => (
          <rect
            key={`${r}-${col}`}
            x={col * 40}
            y={r * 40}
            width="34"
            height="34"
            rx="6"
            fill={fill ? c : 'transparent'}
            stroke={c}
            strokeWidth={fill ? 0 : 0.6}
            opacity={fill ? 0.9 : 0.18}
          />
        ))}
      </g>
    </svg>
  );
}

// ===== Pulse art: radiating arcs =====
function PulseArt({ seed = 0 }) {
  const c = 'var(--accent)';
  return (
    <svg viewBox="0 0 400 500" preserveAspectRatio="xMidYMid slice" aria-hidden="true">
      <rect width="400" height="500" fill="var(--bg-rise)" />
      <g transform="translate(80 250)">
        {Array.from({ length: 12 }).map((_, i) => {
          const r = 60 + i * 30;
          return (
            <path
              key={i}
              d={`M 0 -${r} A ${r} ${r} 0 0 1 0 ${r}`}
              fill="none"
              stroke={c}
              strokeWidth={i === 3 ? 2 : 0.8}
              opacity={0.6 - i * 0.04}
            />
          );
        })}
        <circle cx="0" cy="0" r="14" fill={c} />
      </g>
      {/* Bar chart in corner */}
      <g transform="translate(220 320)">
        {[40, 80, 60, 120, 90, 140].map((h, i) => (
          <rect key={i} x={i * 22} y={-h} width="14" height={h} rx="3" fill={c} opacity={0.3 + i * 0.1} />
        ))}
      </g>
    </svg>
  );
}

// ===== Mesh art: connected nodes (network graph) =====
function MeshArt({ seed = 0 }) {
  const c = 'var(--accent)';
  // Deterministic node positions
  const nodes = [
    { x: 80, y: 100 }, { x: 200, y: 60 }, { x: 320, y: 130 },
    { x: 120, y: 220 }, { x: 230, y: 200 }, { x: 340, y: 260 },
    { x: 80, y: 340 }, { x: 200, y: 380 }, { x: 320, y: 420 },
  ];
  const links = [[0,1],[1,2],[0,3],[1,3],[1,4],[2,5],[3,4],[4,5],[3,6],[4,7],[5,8],[6,7],[7,8]];
  return (
    <svg viewBox="0 0 400 500" preserveAspectRatio="xMidYMid slice" aria-hidden="true">
      <rect width="400" height="500" fill="var(--bg-rise)" />
      {/* Glow */}
      <circle cx="200" cy="250" r="180" fill={c} opacity="0.08" />
      {/* Links */}
      {links.map(([a, b], i) => (
        <line
          key={i}
          x1={nodes[a].x} y1={nodes[a].y}
          x2={nodes[b].x} y2={nodes[b].y}
          stroke={c}
          strokeWidth="0.6"
          opacity="0.4"
        />
      ))}
      {/* Nodes */}
      {nodes.map((n, i) => (
        <g key={i}>
          <circle cx={n.x} cy={n.y} r={i === 4 ? 14 : 6} fill={c} opacity={i === 4 ? 1 : 0.85} />
          {i === 4 && <circle cx={n.x} cy={n.y} r="24" fill="none" stroke={c} strokeWidth="1" opacity="0.5" />}
        </g>
      ))}
    </svg>
  );
}

// ===== Tape art: horizontal stacked tapes =====
function TapeArt({ seed = 0 }) {
  const c = 'var(--accent)';
  return (
    <svg viewBox="0 0 400 500" preserveAspectRatio="xMidYMid slice" aria-hidden="true">
      <rect width="400" height="500" fill="var(--bg-rise)" />
      {Array.from({ length: 6 }).map((_, i) => {
        const w = 200 + ((seed + i * 13) % 5) * 30;
        const x = 30 + ((seed + i * 7) % 4) * 20;
        const y = 70 + i * 60;
        const filled = i % 3 === 0;
        return (
          <g key={i}>
            <rect
              x={x} y={y}
              width={w} height={38}
              rx="19"
              fill={filled ? c : 'transparent'}
              stroke={c}
              strokeWidth={filled ? 0 : 1}
              opacity={filled ? 0.95 : 0.4}
            />
            <rect
              x={x + w + 16} y={y + 10}
              width="18" height="18"
              rx="9"
              fill={c}
              opacity="0.5"
            />
          </g>
        );
      })}
    </svg>
  );
}

// ===== Service icon (small inline) =====
function ServiceIcon({ kind, size = 28 }) {
  const c = 'currentColor';
  switch (kind) {
    case 'agent':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <circle cx="14" cy="11" r="6" stroke={c} strokeWidth="1.5" />
          <circle cx="14" cy="11" r="2" fill={c} />
          <path d="M 6 24 Q 14 18 22 24" stroke={c} strokeWidth="1.5" strokeLinecap="round" />
        </svg>
      );
    case 'auto':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <rect x="4" y="6" width="8" height="8" rx="2" stroke={c} strokeWidth="1.5" />
          <rect x="16" y="14" width="8" height="8" rx="2" stroke={c} strokeWidth="1.5" />
          <path d="M 12 10 Q 18 10 18 16" stroke={c} strokeWidth="1.5" fill="none" />
        </svg>
      );
    case 'strat':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <path d="M 4 22 L 10 14 L 14 18 L 22 8" stroke={c} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
          <circle cx="22" cy="8" r="2" fill={c} />
          <circle cx="14" cy="18" r="2" fill={c} />
        </svg>
      );
    case 'integ':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <rect x="3" y="9" width="9" height="10" rx="2" stroke={c} strokeWidth="1.5" />
          <rect x="16" y="9" width="9" height="10" rx="2" stroke={c} strokeWidth="1.5" />
          <path d="M 12 14 L 16 14" stroke={c} strokeWidth="1.5" />
          <circle cx="14" cy="14" r="1.4" fill={c} />
        </svg>
      );
    case 'dash':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <rect x="4" y="16" width="4" height="8" rx="1" fill={c} />
          <rect x="12" y="10" width="4" height="14" rx="1" fill={c} />
          <rect x="20" y="6" width="4" height="18" rx="1" fill={c} />
        </svg>
      );
    case 'partner':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <circle cx="10" cy="11" r="4" stroke={c} strokeWidth="1.5" />
          <circle cx="19" cy="11" r="4" stroke={c} strokeWidth="1.5" />
          <path d="M 3 24 Q 10 19 14 22 Q 18 19 25 24" stroke={c} strokeWidth="1.5" strokeLinecap="round" fill="none" />
        </svg>
      );
    case 'email':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <rect x="3" y="7" width="22" height="15" rx="2.5" stroke={c} strokeWidth="1.5" />
          <path d="M 4 9 L 14 16 L 24 9" stroke={c} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" fill="none" />
          <circle cx="22" cy="6" r="2.5" fill={c} />
        </svg>
      );
    case 'portal':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <rect x="3" y="5" width="22" height="18" rx="2.5" stroke={c} strokeWidth="1.5" />
          <line x1="10" y1="5" x2="10" y2="23" stroke={c} strokeWidth="1.2" />
          <circle cx="6.5" cy="10" r="0.9" fill={c} />
          <circle cx="6.5" cy="14" r="0.9" fill={c} />
          <circle cx="6.5" cy="18" r="0.9" fill={c} />
          <rect x="13" y="9" width="9" height="3" rx="1" fill={c} opacity="0.7" />
          <rect x="13" y="14" width="6" height="2" rx="1" fill={c} opacity="0.45" />
          <rect x="13" y="18" width="8" height="2" rx="1" fill={c} opacity="0.45" />
        </svg>
      );
    // Why-card glyphs
    case 'founder':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <circle cx="14" cy="14" r="9" stroke={c} strokeWidth="1.5" />
          <circle cx="14" cy="14" r="3" fill={c} />
          <path d="M 14 5 L 14 23 M 5 14 L 23 14" stroke={c} strokeWidth="0.8" strokeDasharray="2 3" opacity="0.6" />
        </svg>
      );
    case 'speed':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <path d="M 16 4 L 8 16 L 13 16 L 11 24 L 20 11 L 14 11 Z" fill={c} />
        </svg>
      );
    case 'simple':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <rect x="4" y="4" width="20" height="20" rx="6" stroke={c} strokeWidth="1.5" />
          <circle cx="14" cy="14" r="3" fill={c} />
        </svg>
      );
    case 'roi':
      return (
        <svg width={size} height={size} viewBox="0 0 28 28" fill="none">
          <path d="M 4 20 L 10 14 L 14 17 L 24 6" stroke={c} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
          <path d="M 18 6 L 24 6 L 24 12" stroke={c} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
        </svg>
      );
    default: return null;
  }
}

// ===== Reveal hook (intersection observer) =====
function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    if (!ref.current) return;
    const el = ref.current;
    const obs = new IntersectionObserver(
      (entries) => {
        entries.forEach((e) => {
          if (e.isIntersecting) {
            e.target.classList.add('in');
            obs.unobserve(e.target);
          }
        });
      },
      { threshold: 0.15 }
    );
    obs.observe(el);
    return () => obs.disconnect();
  }, []);
  return ref;
}

Object.assign(window, {
  LogoMark,
  FlowArt, OrbitArt, LatticeArt, PulseArt, MeshArt, TapeArt,
  ServiceIcon,
  useReveal,
});
