/* global React */
const { useState } = React;

/* =============================================================
   LOGO MARK
============================================================= */
function LogoMark({ size = 24, color = "currentColor", accent = "var(--accent)" }) {
  return (
    <svg className="logo-mark" width={size} height={size} viewBox="0 0 24 24" fill="none">
      {/* two input vectors converging on a node */}
      <line x1="2" y1="6" x2="11" y2="12" stroke={color} strokeWidth="2" strokeLinecap="square" />
      <line x1="2" y1="18" x2="11" y2="12" stroke={color} strokeWidth="2" strokeLinecap="square" />
      {/* convergence node */}
      <circle cx="11" cy="12" r="1.8" fill={color} />
      {/* small gap, then the aligned output vector */}
      <line x1="13.6" y1="12" x2="19.5" y2="12" stroke={accent} strokeWidth="2" strokeLinecap="square" />
      {/* filled triangle arrowhead */}
      <path d="M19 9 L22 12 L19 15 Z" fill={accent} />
    </svg>
  );
}

/* =============================================================
   HERO VISUAL — big-picture: complexity → clarity
   Left: scattered vectors pointing in different directions
   Center: VECTORSWIFT.CORE convergence
   Right: aligned vectors, all pointing the same way
============================================================= */
function HeroVisual() {
  const [hover, setHover] = useState(null);

  const cx = 380;
  const cy = 320;

  // LEFT — chaotic inputs. Short labeled arrows at random-ish angles.
  // Labels are right-aligned at (s.x - 10) so origin x must leave room for
  // the longest label (~"WORKFLOWS" ≈ 90px wide at our font size).
  const inputs = [
    { x: 150, y: 80,  angle: 26,   label: "TEAMS" },
    { x: 130, y: 170, angle: -8,   label: "TOOLS" },
    { x: 160, y: 250, angle: 14,   label: "DATA" },
    { x: 124, y: 340, angle: -22,  label: "POLICY" },
    { x: 154, y: 430, angle: 6,    label: "WORKFLOWS" },
    { x: 132, y: 520, angle: -16,  label: "VENDORS" },
  ];

  // RIGHT — aligned outcomes, all flowing one direction
  const outputs = [
    { label: "STRATEGY",        y: 110 },
    { label: "TRAINED TEAMS",   y: 188 },
    { label: "SECURE SYSTEMS",  y: 266 },
    { label: "AUTOMATIONS",     y: 344 },
    { label: "MEASURED IMPACT", y: 422 },
    { label: "ADOPTION",        y: 500 },
  ];

  return (
    <div style={{ position: "relative", width: "100%", height: "100%" }}
         onMouseLeave={() => setHover(null)}>
      <svg viewBox="0 0 800 620" width="100%" height="100%" preserveAspectRatio="xMidYMid meet">
        <defs>
          <pattern id="dotgrid" x="0" y="0" width="16" height="16" patternUnits="userSpaceOnUse">
            <circle cx="1" cy="1" r="0.7" fill="rgba(255,255,255,0.08)" />
          </pattern>
          <marker id="arrowAccent" markerWidth="9" markerHeight="9" refX="7" refY="4.5" orient="auto">
            <path d="M0 0 L8 4.5 L0 9 Z" fill="var(--accent)" />
          </marker>
          <marker id="arrowDim" markerWidth="7" markerHeight="7" refX="6" refY="3.5" orient="auto">
            <path d="M0 0 L6 3.5 L0 7 Z" fill="rgba(236,232,223,0.55)" />
          </marker>
          <marker id="arrowSolid" markerWidth="9" markerHeight="9" refX="7" refY="4.5" orient="auto">
            <path d="M0 0 L8 4.5 L0 9 Z" fill="rgba(236,232,223,0.85)" />
          </marker>
        </defs>

        <rect x="0" y="0" width="800" height="620" fill="url(#dotgrid)" />

        {/* axis labels */}
        <text x="20" y="32" fill="rgba(236,232,223,0.55)" fontFamily="Geist Mono, monospace" fontSize="11" letterSpacing="2.2" fontWeight="500">
          COMPLEXITY
        </text>
        <text x="780" y="32" textAnchor="end" fill="rgba(216,242,74,0.95)" fontFamily="Geist Mono, monospace" fontSize="11" letterSpacing="2.2" fontWeight="500">
          CLARITY
        </text>

        {/* vertical zone guides */}
        <line x1="220" y1="56" x2="220" y2="580" stroke="rgba(255,255,255,0.08)" strokeDasharray="2 5" />
        <line x1="540" y1="56" x2="540" y2="580" stroke="rgba(255,255,255,0.08)" strokeDasharray="2 5" />

        {/* CONVERGENCE CORE */}
        <g>
          <circle cx={cx} cy={cy} r="58" fill="none" stroke="rgba(216,242,74,0.16)" />
          <circle cx={cx} cy={cy} r="38" fill="none" stroke="rgba(216,242,74,0.34)" />
          <circle cx={cx} cy={cy} r="20" fill="var(--accent)" />
          <circle cx={cx} cy={cy} r="6" fill="var(--ink)" />
          <text x={cx} y={cy + 92} textAnchor="middle" fill="rgba(236,232,223,0.78)"
                fontFamily="Geist Mono, monospace" fontSize="11" letterSpacing="2.4" fontWeight="500">
            VECTORSWIFT
          </text>
          <text x={cx} y={cy + 108} textAnchor="middle" fill="rgba(236,232,223,0.4)"
                fontFamily="Geist Mono, monospace" fontSize="9" letterSpacing="2.2">
            CORE
          </text>
        </g>

        {/* INPUTS → CORE — splines from chaotic origins */}
        {inputs.map((s, i) => {
          const sx = s.x + 56;
          const sy = s.y;
          const c1x = sx + 100;
          const c1y = sy + (s.angle * 1.5);
          const c2x = cx - 110;
          const c2y = cy;
          const d = `M ${sx} ${sy} C ${c1x} ${c1y}, ${c2x} ${c2y}, ${cx - 22} ${cy}`;
          const isHover = hover === `in-${i}`;
          return (
            <g key={`p-${i}`}>
              <path d={d}
                    stroke={isHover ? "var(--accent)" : "rgba(236,232,223,0.30)"}
                    strokeWidth={isHover ? 1.4 : 1}
                    fill="none"
                    strokeDasharray={i % 2 === 0 ? "0" : "3 4"} />
              <circle r="2" fill="var(--accent)" opacity="0.9">
                <animateMotion dur={`${5 + i * 0.4}s`} repeatCount="indefinite" begin={`${i * 0.55}s`} path={d} />
              </circle>
            </g>
          );
        })}

        {/* LEFT — chaotic vectors */}
        {inputs.map((s, i) => {
          const rad = (s.angle * Math.PI) / 180;
          const x2 = s.x + Math.cos(rad) * 56;
          const y2 = s.y + Math.sin(rad) * 56;
          const isHover = hover === `in-${i}`;
          return (
            <g key={`s-${i}`}
               onMouseEnter={() => setHover(`in-${i}`)}
               style={{ cursor: "default" }}>
              {/* tiny origin dot */}
              <circle cx={s.x} cy={s.y} r="3.5" fill={isHover ? "var(--accent)" : "rgba(236,232,223,0.85)"} />
              {/* directional vector */}
              <line x1={s.x} y1={s.y} x2={x2} y2={y2}
                    stroke={isHover ? "var(--accent)" : "rgba(236,232,223,0.65)"}
                    strokeWidth="1.4"
                    markerEnd={isHover ? "url(#arrowAccent)" : "url(#arrowSolid)"} />
              {/* label */}
              <text x={s.x - 8} y={s.y + 4} textAnchor="end"
                    fill="rgba(236,232,223,0.92)"
                    fontFamily="Geist Mono, monospace"
                    fontSize="11" letterSpacing="1.6" fontWeight="500">
                {s.label}
              </text>
            </g>
          );
        })}

        {/* CORE → outputs (clean parallel) */}
        {outputs.map((n, i) => {
          const tx = 560;
          const ty = n.y;
          const d = `M ${cx + 22} ${cy} C ${cx + 110} ${cy}, ${tx - 100} ${ty}, ${tx} ${ty}`;
          const isHover = hover === `out-${i}`;
          return (
            <g key={`o-${i}`}
               onMouseEnter={() => setHover(`out-${i}`)}>
              <path d={d}
                    stroke={isHover ? "var(--accent)" : "rgba(216,242,74,0.70)"}
                    strokeWidth={isHover ? 1.6 : 1.1}
                    fill="none"
                    markerEnd="url(#arrowAccent)" />
              <circle r="2.4" fill="var(--accent)">
                <animateMotion dur={`${3.4 + i * 0.4}s`} repeatCount="indefinite" begin={`${i * 0.35}s`} path={d} />
              </circle>
            </g>
          );
        })}

        {/* RIGHT — aligned outcome labels */}
        {outputs.map((n, i) => (
          <g key={`l-${i}`}>
            <line x1="566" y1={n.y} x2="592" y2={n.y} stroke="var(--accent)" strokeWidth="1.6" />
            <text x="602" y={n.y + 4}
                  fill="rgba(236,232,223,0.95)"
                  fontFamily="Geist Mono, monospace"
                  fontSize="11.5" letterSpacing="1.6" fontWeight="500">
              {n.label}
            </text>
          </g>
        ))}

        {/* baseline tick scale at bottom */}
        <g>
          <line x1="20" y1="580" x2="780" y2="580" stroke="rgba(255,255,255,0.18)" strokeWidth="1" />
          {Array.from({length: 31}).map((_, i) => (
            <line key={i} x1={20 + i * 25.4} y1="578" x2={20 + i * 25.4} y2="584"
                  stroke={i % 5 === 0 ? "rgba(236,232,223,0.6)" : "rgba(255,255,255,0.18)"}
                  strokeWidth="1" />
          ))}
          <text x="20" y="604" fill="rgba(236,232,223,0.5)" fontFamily="Geist Mono, monospace" fontSize="9.5" letterSpacing="2">
            INPUT
          </text>
          <text x={cx} y="604" textAnchor="middle" fill="rgba(216,242,74,0.85)" fontFamily="Geist Mono, monospace" fontSize="9.5" letterSpacing="2" fontWeight="500">
            CONVERGE
          </text>
          <text x="780" y="604" textAnchor="end" fill="rgba(236,232,223,0.5)" fontFamily="Geist Mono, monospace" fontSize="9.5" letterSpacing="2">
            OUTPUT
          </text>
        </g>

        {/* corner brackets */}
        <g stroke="rgba(236,232,223,0.55)" strokeWidth="1" fill="none">
          <path d="M 10 10 L 28 10 M 10 10 L 10 28" />
          <path d="M 790 10 L 772 10 M 790 10 L 790 28" />
          <path d="M 10 610 L 28 610 M 10 610 L 10 592" />
          <path d="M 790 610 L 772 610 M 790 610 L 790 592" />
        </g>
      </svg>
    </div>
  );
}

/* =============================================================
   SERVICE ICONS — simple, readable abstract marks
============================================================= */
function SvcIcon({ kind }) {
  const stroke = "rgba(236,232,223,0.9)";
  const accent = "var(--accent)";
  const ink = "var(--ink)";
  const label = "rgba(236,232,223,0.85)";
  const props = { width: 120, height: 100, viewBox: "0 0 120 100", fill: "none" };

  if (kind === "training") {
    // Knowledge transfer — person node out to many endpoints
    return (
      <svg {...props}>
        <circle cx="18" cy="50" r="11" stroke={stroke} strokeWidth="1.4" fill={accent} />
        <circle cx="18" cy="50" r="3" fill={ink} />
        <path d="M30 50 L62 18" stroke={stroke} strokeWidth="1.2" />
        <path d="M30 50 L62 50" stroke={stroke} strokeWidth="1.2" />
        <path d="M30 50 L62 82" stroke={stroke} strokeWidth="1.2" />
        <rect x="64" y="10" width="42" height="18" stroke={stroke} strokeWidth="1.2" fill="none" />
        <rect x="64" y="42" width="42" height="18" stroke={stroke} strokeWidth="1.2" fill="none" />
        <rect x="64" y="74" width="42" height="18" stroke={stroke} strokeWidth="1.2" fill="none" />
        <line x1="70" y1="19" x2="100" y2="19" stroke={stroke} strokeWidth="1" />
        <line x1="70" y1="51" x2="100" y2="51" stroke={stroke} strokeWidth="1" />
        <line x1="70" y1="83" x2="100" y2="83" stroke={stroke} strokeWidth="1" />
      </svg>
    );
  }

  if (kind === "custom") {
    // Modular composition — building blocks
    return (
      <svg {...props}>
        <rect x="8" y="10" width="40" height="40" stroke={stroke} strokeWidth="1.2" />
        <rect x="52" y="10" width="40" height="40" stroke={stroke} strokeWidth="1.2" />
        <rect x="8" y="54" width="40" height="38" stroke={stroke} strokeWidth="1.2" fill={accent} />
        <rect x="52" y="54" width="40" height="38" stroke={stroke} strokeWidth="1.2" />
        <rect x="96" y="32" width="20" height="38" stroke={stroke} strokeWidth="1.2" />
        <line x1="16" y1="22" x2="40" y2="22" stroke={stroke} strokeWidth="1" />
        <line x1="16" y1="30" x2="34" y2="30" stroke={stroke} strokeWidth="1" />
        <line x1="60" y1="22" x2="84" y2="22" stroke={stroke} strokeWidth="1" />
        <line x1="60" y1="30" x2="78" y2="30" stroke={stroke} strokeWidth="1" />
        <line x1="60" y1="66" x2="84" y2="66" stroke={stroke} strokeWidth="1" />
        <line x1="60" y1="74" x2="80" y2="74" stroke={stroke} strokeWidth="1" />
      </svg>
    );
  }

  if (kind === "doc") {
    // Documents — clean, not over-detailed
    return (
      <svg {...props}>
        <rect x="14" y="14" width="56" height="78" stroke={stroke} strokeWidth="1.2" fill="none" />
        <rect x="28" y="6"  width="56" height="78" stroke={stroke} strokeWidth="1.2" fill="none" />
        <line x1="38" y1="22" x2="74" y2="22" stroke={stroke} strokeWidth="1" />
        <line x1="38" y1="34" x2="74" y2="34" stroke={stroke} strokeWidth="1" />
        <line x1="38" y1="46" x2="68" y2="46" stroke={stroke} strokeWidth="1" />
        <line x1="38" y1="58" x2="74" y2="58" stroke={stroke} strokeWidth="1" />
        <line x1="38" y1="70" x2="62" y2="70" stroke={stroke} strokeWidth="1" />
        {/* lock badge */}
        <rect x="92" y="48" width="22" height="22" stroke={stroke} strokeWidth="1.2" fill={accent} />
        <path d="M97 48 V44 a6 6 0 0 1 12 0 V48" stroke={stroke} strokeWidth="1.2" fill="none" />
        <circle cx="103" cy="59" r="2" fill={ink} />
      </svg>
    );
  }

  if (kind === "strategy") {
    // Roadmap curve with milestones
    return (
      <svg {...props}>
        <path d="M8 78 Q 36 78 36 50 T 70 22 T 112 22" stroke={stroke} strokeWidth="1.4" fill="none" />
        <circle cx="8"   cy="78" r="4" fill={stroke} />
        <circle cx="36"  cy="50" r="4" fill={stroke} />
        <circle cx="70"  cy="22" r="6" fill={accent} stroke={stroke} strokeWidth="1.4" />
        <circle cx="112" cy="22" r="4" fill={stroke} />
        <text x="6"   y="94" fontFamily="Geist Mono, monospace" fontSize="8" letterSpacing="1" fill={label} opacity="0.7">NOW</text>
        <text x="32"  y="94" fontFamily="Geist Mono, monospace" fontSize="8" letterSpacing="1" fill={label} opacity="0.7">Q1</text>
        <text x="66"  y="94" fontFamily="Geist Mono, monospace" fontSize="8" letterSpacing="1" fill={label} opacity="1" fontWeight="600">Q2</text>
        <text x="106" y="94" fontFamily="Geist Mono, monospace" fontSize="8" letterSpacing="1" fill={label} opacity="0.7">Q3</text>
      </svg>
    );
  }
  return null;
}

/* =============================================================
   INDUSTRY MARKER — distinct geometric glyphs
============================================================= */
function IndMarker({ kind }) {
  const s = "rgba(236,232,223,0.9)";
  const acc = "var(--accent)";
  const props = { width: 64, height: 64, viewBox: "0 0 64 64", fill: "none" };
  if (kind === "legal") {
    return (
      <svg {...props}>
        <line x1="32" y1="6" x2="32" y2="58" stroke={s} strokeWidth="1.2" />
        <path d="M14 18 L32 12 L50 18" stroke={s} strokeWidth="1.2" fill="none" />
        <path d="M6 34 L14 18 L22 34 Z" stroke={s} strokeWidth="1.2" fill="none" />
        <path d="M42 34 L50 18 L58 34 Z" stroke={acc} strokeWidth="1.4" fill="none" />
        <line x1="24" y1="58" x2="40" y2="58" stroke={s} strokeWidth="1.4" />
      </svg>
    );
  }
  if (kind === "retail") {
    return (
      <svg {...props}>
        {/* shopping bag silhouette with handles + accent tag */}
        <path d="M14 22 L14 56 L50 56 L50 22 Z" stroke={s} strokeWidth="1.2" fill="none" />
        <path d="M22 22 C22 14, 42 14, 42 22" stroke={s} strokeWidth="1.2" fill="none" />
        <line x1="14" y1="30" x2="50" y2="30" stroke={s} strokeWidth="1.2" />
        <circle cx="32" cy="42" r="6" fill={acc} />
      </svg>
    );
  }
  if (kind === "enterprise") {
    return (
      <svg {...props}>
        <rect x="6"  y="22" width="14" height="32" stroke={s} strokeWidth="1.2" />
        <rect x="25" y="14" width="14" height="40" stroke={s} strokeWidth="1.2" />
        <rect x="44" y="6"  width="14" height="48" stroke={acc} strokeWidth="1.4" />
        <line x1="4" y1="54" x2="60" y2="54" stroke={s} strokeWidth="1.2" />
      </svg>
    );
  }
  if (kind === "gov") {
    return (
      <svg {...props}>
        <path d="M6 24 L32 8 L58 24" stroke={s} strokeWidth="1.2" fill="none" />
        <line x1="6" y1="54" x2="58" y2="54" stroke={s} strokeWidth="1.2" />
        <line x1="6" y1="24" x2="58" y2="24" stroke={s} strokeWidth="1.2" />
        <line x1="16" y1="24" x2="16" y2="54" stroke={s} strokeWidth="1.2" />
        <line x1="26" y1="24" x2="26" y2="54" stroke={s} strokeWidth="1.2" />
        <line x1="38" y1="24" x2="38" y2="54" stroke={s} strokeWidth="1.2" />
        <line x1="48" y1="24" x2="48" y2="54" stroke={s} strokeWidth="1.2" />
        <circle cx="32" cy="19" r="3" fill={acc} />
      </svg>
    );
  }
  return null;
}

/* =============================================================
   PROCESS DIAGRAM — clean, readable, big-picture
============================================================= */
function ProcessFlow() {
  return (
    <svg viewBox="0 0 1200 80" width="100%" height="80" preserveAspectRatio="none">
      <defs>
        <marker id="parr" markerWidth="11" markerHeight="11" refX="9" refY="5.5" orient="auto">
          <path d="M0 0 L10 5.5 L0 11 Z" fill="rgba(236,232,223,0.9)" />
        </marker>
      </defs>
      <line x1="0" y1="40" x2="1200" y2="40" stroke="rgba(236,232,223,0.12)" strokeDasharray="2 6" />
      <path d="M 80 40 L 1130 40" stroke="rgba(236,232,223,0.9)" strokeWidth="1.4" fill="none" markerEnd="url(#parr)" />

      {[80, 600, 1120].map((x, i) => (
        <g key={i}>
          <circle cx={x} cy="40" r="16" fill="var(--ink)" stroke="rgba(236,232,223,0.9)" strokeWidth="1.6" />
          <circle cx={x} cy="40" r="6" fill={i === 0 ? "var(--accent)" : "rgba(236,232,223,0.9)"} />
        </g>
      ))}
    </svg>
  );
}

/* =============================================================
   TRUST MARKERS
============================================================= */
function TrustMark({ n }) {
  const acc = "var(--accent)";
  const s = "rgba(236,232,223,0.92)";
  const map = {
    0: <g><circle cx="16" cy="16" r="12" stroke={s} strokeWidth="1.2" fill="none" /><path d="M10 16 L14 20 L22 12" stroke={acc} strokeWidth="2" fill="none" /></g>,
    1: <g><rect x="4" y="4" width="24" height="24" stroke={s} strokeWidth="1.2" fill="none" /><rect x="10" y="10" width="12" height="12" fill={acc} /></g>,
    2: <g><path d="M16 4 L28 12 L28 22 L16 28 L4 22 L4 12 Z" stroke={s} strokeWidth="1.2" fill="none" /><path d="M16 11 L22 14 L22 20 L16 23 L10 20 L10 14 Z" fill={acc} /></g>,
    3: <g><line x1="4" y1="16" x2="28" y2="16" stroke={s} strokeWidth="1.2" /><circle cx="6"  cy="16" r="2.5" fill={acc} /><circle cx="16" cy="16" r="2.5" fill={acc} /><circle cx="26" cy="16" r="2.5" fill={acc} /></g>,
    4: <g><path d="M4 26 L16 6 L28 26 Z" stroke={s} strokeWidth="1.2" fill="none" /><path d="M11 26 L16 16 L21 26" stroke={acc} strokeWidth="1.8" fill="none" /></g>,
  };
  return <svg className="marker" width="32" height="32" viewBox="0 0 32 32" fill="none">{map[n]}</svg>;
}

/* =============================================================
   CTA BG — flowing field
============================================================= */
function CTABg() {
  return (
    <svg className="cta-bg" viewBox="0 0 1400 400" preserveAspectRatio="none">
      <defs>
        <pattern id="ctaDots" x="0" y="0" width="22" height="22" patternUnits="userSpaceOnUse">
          <circle cx="1" cy="1" r="0.7" fill="rgba(236,232,223,0.12)" />
        </pattern>
      </defs>
      <rect x="0" y="0" width="1400" height="400" fill="url(#ctaDots)" />
      {Array.from({length: 8}).map((_, i) => (
        <path key={i}
              d={`M -20 ${50 + i * 32} C 360 ${20 + i * 32}, 880 ${130 + i * 32}, 1420 ${30 + i * 32}`}
              stroke={i === 3 ? "var(--orange)" : "rgba(236,232,223,0.14)"}
              strokeWidth={i === 3 ? 1.6 : 0.8}
              fill="none" />
      ))}
      <circle cx="1000" cy="170" r="8"  fill="var(--orange)" />
      <circle cx="1000" cy="170" r="18" fill="none" stroke="var(--orange)" strokeOpacity="0.55" />
      <circle cx="1000" cy="170" r="32" fill="none" stroke="var(--orange)" strokeOpacity="0.22" />
    </svg>
  );
}

/* =============================================================
   ARROWS
============================================================= */
function ArrowEast({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 14 14" fill="none">
      <line x1="1" y1="7" x2="12" y2="7" stroke="currentColor" strokeWidth="1.6" />
      <path d="M8 3 L12 7 L8 11" stroke="currentColor" strokeWidth="1.6" fill="none" />
    </svg>
  );
}

function ArrowNE({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 14 14" fill="none">
      <line x1="2" y1="12" x2="11" y2="3" stroke="currentColor" strokeWidth="1.6" />
      <path d="M5 3 L11 3 L11 9" stroke="currentColor" strokeWidth="1.6" fill="none" />
    </svg>
  );
}

Object.assign(window, { LogoMark, HeroVisual, SvcIcon, IndMarker, ProcessFlow, TrustMark, CTABg, ArrowEast, ArrowNE });
