// Abstract hero background — combines:
//  · soft indigo color wells (blobs) — ambient base
//  · a low perspective grid receding into the back — depth anchor
//  · 3 particle layers (far, mid, front) with different sizes & speeds
//  · a few thin geometric outline shapes at the mid layer for texture
//
// The Hero parallax (mouse + scroll) reads .iso-layer + data-depth and
// translates each layer at its own ratio — the grid stays nearly fixed,
// far stars drift gently, foreground particles move most. Clear depth read.
//
// Exposes window.IsoScene (kept for app wiring compatibility).

// Deterministic RNG so positions are stable across renders
function rngFactory(seed) {
  return (n) => {
    const v = Math.sin(n * 12.9898 + seed * 78.233) * 43758.5453;
    return v - Math.floor(v);
  };
}

function Blobs() {
  // 3 large blurred indigo wells — the ambient backdrop color
  return (
    <g filter="url(#abs-blob-blur)">
      <circle cx="240"  cy="220" r="280" fill="#4338CA" opacity="0.40" className="blob-orbit-1"/>
      <circle cx="980"  cy="320" r="320" fill="#6366F1" opacity="0.45" className="blob-orbit-2"/>
      <circle cx="640"  cy="650" r="240" fill="#7C3AED" opacity="0.35" className="blob-orbit-3"/>
    </g>
  );
}

function PerspectiveGrid() {
  // Vanishing-point grid in the lower 55% of the hero — provides a fixed
  // depth reference (parallax of THIS layer is near zero).
  const vpX = 600;
  const vpY = 420;           // vanishing point ~52% down the viewBox
  const farY = 800;          // bottom edge
  const lines = [];

  // Horizontal lines, spaced with perspective acceleration toward viewer
  const rows = 14;
  for (let i = 1; i <= rows; i++) {
    const t = i / rows;
    const y = vpY + Math.pow(t, 1.7) * (farY - vpY);
    const op = 0.06 + Math.pow(t, 0.7) * 0.22;
    lines.push(
      <line key={`h${i}`} x1="-80" y1={y} x2="1280" y2={y}
            stroke="#818CF8" strokeOpacity={op} strokeWidth="0.85"/>
    );
  }

  // Radial lines fanning from the vanishing point, downward
  const cols = 18;
  for (let i = -cols; i <= cols; i++) {
    const angle = (i / cols) * 0.62; // narrower spread feels more natural
    const dx = Math.tan(angle) * (farY - vpY);
    const op = 0.05 + (1 - Math.abs(i) / cols) * 0.10;
    lines.push(
      <line key={`v${i}`} x1={vpX} y1={vpY} x2={vpX + dx} y2={farY + 40}
            stroke="#818CF8" strokeOpacity={op} strokeWidth="0.7"/>
    );
  }

  // Soft horizon glow on the vanishing-point line
  return (
    <g>
      <line x1="-80" y1={vpY} x2="1280" y2={vpY}
            stroke="#818CF8" strokeOpacity="0.55" strokeWidth="1.2"/>
      <line x1="-80" y1={vpY} x2="1280" y2={vpY}
            stroke="#A78BFA" strokeOpacity="0.25" strokeWidth="6"/>
      {lines}
    </g>
  );
}

// Particle layer — small filled dots scattered across the viewBox
function Particles({ count, w = 1200, h = 800, seed, palette, sizeRange, opacityRange, yLimit }) {
  const rng = rngFactory(seed);
  const items = [];
  for (let i = 0; i < count; i++) {
    const x = rng(i * 2) * w;
    const y = rng(i * 2 + 1) * (yLimit || h);
    const sz = sizeRange[0] + rng(i * 3) * (sizeRange[1] - sizeRange[0]);
    const op = opacityRange[0] + rng(i * 5) * (opacityRange[1] - opacityRange[0]);
    const pickColor = rng(i * 7);
    const color = pickColor < 0.08 ? '#FBBF24' : palette[Math.floor(pickColor * palette.length)];
    items.push(
      <circle key={i} cx={x} cy={y} r={sz} fill={color} opacity={op}/>
    );
  }
  return <g>{items}</g>;
}

// A few thin geometric outline shapes — texture in the mid layer
function GeoShapes() {
  return (
    <g fill="none" stroke="#818CF8" strokeOpacity="0.30" strokeWidth="1.1" strokeLinejoin="round">
      {/* Tilted diamond */}
      <path d="M 200,180 L 250,220 L 200,260 L 150,220 Z" transform="rotate(8 200 220)"/>
      {/* Small triangle, far right */}
      <path d="M 1020,140 L 1075,210 L 965,210 Z"/>
      {/* Thin horizontal accent line with a chevron */}
      <line x1="320" y1="60" x2="450" y2="60"/>
      <path d="M 445,55 L 455,60 L 445,65"/>
      {/* Tiny crosshair */}
      <g transform="translate(880, 80)" stroke="#A78BFA" strokeOpacity="0.55" strokeWidth="1.2">
        <line x1="-7" y1="0" x2="7" y2="0"/>
        <line x1="0" y1="-7" x2="0" y2="7"/>
        <circle cx="0" cy="0" r="3" fill="none"/>
      </g>
      {/* Square outline floating left-mid */}
      <rect x="60" y="500" width="36" height="36" transform="rotate(12 78 518)"/>
      {/* Hex hint, lower right */}
      <path d="M 1080,560 L 1110,545 L 1140,560 L 1140,590 L 1110,605 L 1080,590 Z" strokeOpacity="0.25"/>
    </g>
  );
}

function AbstractScene() {
  return (
    <svg
      className="abstract-scene"
      viewBox="0 0 1200 800"
      preserveAspectRatio="xMidYMid slice"
      xmlns="http://www.w3.org/2000/svg"
      aria-hidden="true"
    >
      <defs>
        <filter id="abs-blob-blur" x="-20%" y="-20%" width="140%" height="140%">
          <feGaussianBlur stdDeviation="80"/>
        </filter>
        {/* Mask: fade out grid at edges so it doesn't slam into the page bg */}
        <linearGradient id="grid-fade" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%"  stopColor="white" stopOpacity="0"/>
          <stop offset="40%" stopColor="white" stopOpacity="0.05"/>
          <stop offset="60%" stopColor="white" stopOpacity="0.85"/>
          <stop offset="100%" stopColor="white" stopOpacity="0"/>
        </linearGradient>
        <mask id="grid-mask">
          <rect x="0" y="0" width="1200" height="800" fill="url(#grid-fade)"/>
        </mask>
      </defs>

      {/* Layer: ambient color wells (deepest) */}
      <g className="iso-layer" data-depth="0.03">
        <Blobs/>
      </g>

      {/* Layer: perspective grid — nearly fixed; anchors depth */}
      <g className="iso-layer" data-depth="0.06">
        <g mask="url(#grid-mask)">
          <PerspectiveGrid/>
        </g>
      </g>

      {/* Layer: far stars — many, small, dim */}
      <g className="iso-layer" data-depth="0.14">
        <Particles
          count={120}
          w={1200} h={800}
          seed={3}
          palette={['#3F3868', '#4338CA', '#5B4FE0']}
          sizeRange={[0.6, 1.4]}
          opacityRange={[0.35, 0.75]}
        />
      </g>

      {/* Layer: mid stars + thin geometric outlines */}
      <g className="iso-layer" data-depth="0.26">
        <Particles
          count={45}
          w={1200} h={800}
          seed={7}
          palette={['#6366F1', '#818CF8', '#A78BFA']}
          sizeRange={[1.2, 2.4]}
          opacityRange={[0.55, 0.95]}
        />
        <GeoShapes/>
      </g>

      {/* Layer: front bright particles + occasional larger glowing dots */}
      <g className="iso-layer" data-depth="0.48">
        <Particles
          count={18}
          w={1200} h={800}
          seed={11}
          palette={['#A78BFA', '#C4B5FD', '#FBBF24']}
          sizeRange={[2.4, 4.2]}
          opacityRange={[0.85, 1.0]}
        />
        {/* A couple of soft glows around the brightest particles */}
        <g opacity="0.6">
          <circle cx="200"  cy="120" r="14" fill="#FBBF24" opacity="0.25"/>
          <circle cx="940"  cy="540" r="16" fill="#A78BFA" opacity="0.25"/>
          <circle cx="520"  cy="180" r="12" fill="#C4B5FD" opacity="0.20"/>
        </g>
      </g>
    </svg>
  );
}

window.IsoScene = AbstractScene;
