// Pixelcius — Swordsman hero animation (12s loop, cinematic)
//
// Timeline (seconds):
//   0.0–0.7   cut1   First diagonal slash carves \
//   0.7–1.4   cut2   Second diagonal slash carves /  → X-shaped rift
//   1.4–2.0   open   Rift edges peel apart, dark void revealed inside
//   2.0–2.4   tip    Sword TIP only pierces through rift
//   2.4–2.9   arm    Hand + arm + cloak edge emerge (still half-inside)
//   2.9–3.6   step   Full body pushes through, settles
//   3.6–5.4   comboX X slash combo
//   5.4–7.2   comboI I slash combo
//   7.2–8.4   exit   Backs into rift (reverse layered)
//   8.4–9.2   close  Rift edges retract
//   9.2–12.0  rest
//
// Key trick for the realistic emergence: the swordsman SVG is rendered TWICE —
// once clipped to the OUTSIDE of the rift's "void" shape (visible part)
// and once to the INSIDE (dimmed silhouette part). During the emerge phase,
// the void shape moves, revealing more of the character. We use a simple
// approach: a `clip-path` polygon on the hero container that animates from
// "everything cut off" to "no clip", combined with a darkened underlay
// to suggest depth.

function Swordsman() {
  return (
    <div className="sword-scene" aria-hidden="true">
      {/* ─── BACK PLATE: dark void revealed inside the rift cuts ─── */}
      <svg className="sword-scene__void" viewBox="0 0 1000 700" preserveAspectRatio="xMidYMid slice">
        <defs>
          <radialGradient id="void-grad" cx="0.5" cy="0.5" r="0.5">
            <stop offset="0%" stopColor="#000" stopOpacity="1"/>
            <stop offset="55%" stopColor="#000" stopOpacity="0.92"/>
            <stop offset="100%" stopColor="#000" stopOpacity="0"/>
          </radialGradient>
          <radialGradient id="void-haze" cx="0.5" cy="0.5" r="0.5">
            <stop offset="0%" stopColor="var(--c1)" stopOpacity="0.55"/>
            <stop offset="50%" stopColor="var(--c3)" stopOpacity="0.18"/>
            <stop offset="100%" stopColor="var(--c1)" stopOpacity="0"/>
          </radialGradient>
          <filter id="rift-warp" x="-20%" y="-20%" width="140%" height="140%">
            <feTurbulence baseFrequency="0.018 0.04" numOctaves="2" seed="3" result="t"/>
            <feDisplacementMap in="SourceGraphic" in2="t" scale="6"/>
          </filter>
        </defs>
        {/* The dark void behind the rift — clip-path animates to peel open along the X cut */}
        <g className="rift-void">
          {/* Inner abyss (deep black, slight color haze) */}
          <ellipse className="rift-void__abyss" cx="500" cy="350" rx="120" ry="80" fill="url(#void-grad)" filter="url(#rift-warp)"/>
          <ellipse className="rift-void__haze"  cx="500" cy="350" rx="160" ry="100" fill="url(#void-haze)"/>
        </g>
      </svg>

      {/* ─── RIFT EDGES (two slash lines + glow) ─── */}
      <svg className="sword-scene__rift" viewBox="0 0 1000 700" preserveAspectRatio="xMidYMid slice">
        <defs>
          <linearGradient id="rift-glow-a" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor="var(--c1)" stopOpacity="0"/>
            <stop offset="50%" stopColor="var(--c1)" stopOpacity="1"/>
            <stop offset="100%" stopColor="var(--c2)" stopOpacity="0"/>
          </linearGradient>
          <linearGradient id="rift-glow-b" x1="1" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="var(--c3)" stopOpacity="0"/>
            <stop offset="50%" stopColor="var(--c3)" stopOpacity="1"/>
            <stop offset="100%" stopColor="var(--c1)" stopOpacity="0"/>
          </linearGradient>
        </defs>

        {/* Cut 1 (\ diagonal) — three layers: black core (depth), edge (razor light), glow (atmosphere) */}
        <g className="rift rift--a">
          <line className="rift__core" x1="380" y1="220" x2="620" y2="480"/>
          <line className="rift__edge" x1="380" y1="220" x2="620" y2="480"/>
          <line className="rift__glow" x1="380" y1="220" x2="620" y2="480" stroke="url(#rift-glow-a)"/>
        </g>

        {/* Cut 2 (/ diagonal) */}
        <g className="rift rift--b">
          <line className="rift__core" x1="620" y1="220" x2="380" y2="480"/>
          <line className="rift__edge" x1="620" y1="220" x2="380" y2="480"/>
          <line className="rift__glow" x1="620" y1="220" x2="380" y2="480" stroke="url(#rift-glow-b)"/>
        </g>

        {/* Sparks emitted from rift center as it tears open */}
        <g className="rift-sparks">
          {Array.from({ length: 14 }).map((_, i) => {
            const angle = (i / 14) * Math.PI * 2;
            const r = 90 + (i % 3) * 18;
            const x2 = 500 + Math.cos(angle) * r;
            const y2 = 350 + Math.sin(angle) * r;
            return (
              <line
                key={i}
                className="rift-spark"
                x1="500" y1="350"
                x2={x2.toFixed(1)} y2={y2.toFixed(1)}
                stroke="var(--c1)"
                strokeWidth="1.2"
                strokeLinecap="round"
                style={{ animationDelay: `${(i * 0.04).toFixed(2)}s` }}
              />
            );
          })}
        </g>
      </svg>

      {/* ─── SCREEN-WIDE SLASHES (X / I combo) ─── */}
      <svg className="sword-scene__slashes" viewBox="0 0 1000 700" preserveAspectRatio="xMidYMid slice">
        <defs>
          <linearGradient id="slash-x1" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor="#fff" stopOpacity="0"/>
            <stop offset="50%" stopColor="#fff" stopOpacity="1"/>
            <stop offset="100%" stopColor="var(--c2)" stopOpacity="0"/>
          </linearGradient>
          <linearGradient id="slash-x2" x1="1" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#fff" stopOpacity="0"/>
            <stop offset="50%" stopColor="#fff" stopOpacity="1"/>
            <stop offset="100%" stopColor="var(--c3)" stopOpacity="0"/>
          </linearGradient>
          <linearGradient id="slash-i" x1="0.5" y1="0" x2="0.5" y2="1">
            <stop offset="0%" stopColor="#fff" stopOpacity="0"/>
            <stop offset="50%" stopColor="#fff" stopOpacity="1"/>
            <stop offset="100%" stopColor="var(--c1)" stopOpacity="0"/>
          </linearGradient>
        </defs>
        <path className="slash slash--x1" d="M 60 130 Q 500 350 940 580"/>
        <path className="slash slash--x2" d="M 940 130 Q 500 350 60 580"/>
        <path className="slash slash--i1" d="M 460 60 Q 480 350 500 640"/>
        <path className="slash slash--i2" d="M 540 640 Q 520 350 500 60"/>
      </svg>

      {/* ─── CHARACTER (with rift-shaped reveal mask) ─── */}
      <div className="sword-scene__hero-wrap">
        {/* Silhouette layer — visible only when character is INSIDE the rift (darkened) */}
        <div className="sword-scene__hero sword-scene__hero--silhouette">
          <CharacterSVG silhouette/>
        </div>
        {/* Real character — clipped by rift shape during emergence */}
        <div className="sword-scene__hero sword-scene__hero--main">
          <CharacterSVG/>
        </div>
        {/* Rim light flash bursting from rift behind character */}
        <div className="sword-scene__rimflash"></div>
      </div>

      {/* Subtle screen color flash on emergence */}
      <div className="sword-scene__flash"></div>

      {/* Speed lines */}
      <div className="sword-scene__speedlines">
        <span></span><span></span><span></span><span></span><span></span><span></span>
      </div>
    </div>
  );
}

function CharacterSVG({ silhouette }) {
  return (
    <svg viewBox="0 0 220 360" xmlns="http://www.w3.org/2000/svg">
      <defs>
        <linearGradient id={silhouette ? "cloak-sil" : "cloak-g"} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor={silhouette ? "#000" : "#1a1a20"}/>
          <stop offset="100%" stopColor={silhouette ? "#000" : "#0a0a0c"}/>
        </linearGradient>
        <linearGradient id={silhouette ? "blade-sil" : "blade-g"} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor={silhouette ? "#1a1a22" : "#f5f7ff"} stopOpacity="0.95"/>
          <stop offset="60%" stopColor={silhouette ? "#0a0a0c" : "#cfd6e8"} stopOpacity="0.85"/>
          <stop offset="100%" stopColor={silhouette ? "#000" : "var(--c1)"} stopOpacity="0.55"/>
        </linearGradient>
        <linearGradient id={silhouette ? "armor-sil" : "armor-g"} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor={silhouette ? "#0a0a0c" : "#20212a"}/>
          <stop offset="100%" stopColor={silhouette ? "#000" : "#0e0e12"}/>
        </linearGradient>
        <radialGradient id={silhouette ? "eye-sil" : "eye-g"} cx="0.5" cy="0.5" r="0.5">
          <stop offset="0%" stopColor="var(--c1)" stopOpacity={silhouette ? "0.4" : "1"}/>
          <stop offset="100%" stopColor="var(--c1)" stopOpacity="0"/>
        </radialGradient>
      </defs>

      {/* Cloak silhouette behind body */}
      <path className="char__cloak char__cloak--l" d="M 30 140 Q 10 250 40 340 L 110 340 L 110 110 Q 60 110 30 140 Z" fill={`url(#${silhouette ? 'cloak-sil' : 'cloak-g'})`}/>
      <path className="char__cloak char__cloak--r" d="M 190 140 Q 210 250 180 340 L 110 340 L 110 110 Q 160 110 190 140 Z" fill={`url(#${silhouette ? 'cloak-sil' : 'cloak-g'})`} opacity="0.85"/>

      <g className="char__legs">
        <path d="M 90 230 L 75 320 L 70 350" stroke={`url(#${silhouette ? 'armor-sil' : 'armor-g'})`} strokeWidth="18" strokeLinecap="round" fill="none"/>
        <path d="M 130 230 L 145 320 L 150 350" stroke={`url(#${silhouette ? 'armor-sil' : 'armor-g'})`} strokeWidth="18" strokeLinecap="round" fill="none"/>
        <circle cx="78" cy="288" r="7" fill={silhouette ? "#000" : "#2a2a35"}/>
        <circle cx="142" cy="288" r="7" fill={silhouette ? "#000" : "#2a2a35"}/>
      </g>

      <g className="char__torso">
        <path d="M 70 130 L 150 130 L 160 220 L 110 240 L 60 220 Z" fill={`url(#${silhouette ? 'armor-sil' : 'armor-g'})`} stroke={silhouette ? "#000" : "#2c2d38"} strokeWidth="1.2"/>
        <path d="M 110 138 L 110 230" stroke="var(--c1)" strokeWidth="1.4" opacity={silhouette ? "0.2" : "0.6"}/>
        <path d="M 95 150 L 110 138 L 125 150" stroke="var(--c1)" strokeWidth="1.4" opacity={silhouette ? "0.25" : "0.7"} fill="none"/>
        <path d="M 60 130 Q 50 125 50 145 L 70 150 Z" fill={silhouette ? "#000" : "#15161c"} stroke={silhouette ? "#000" : "#2c2d38"} strokeWidth="1"/>
        <path d="M 160 130 Q 170 125 170 145 L 150 150 Z" fill={silhouette ? "#000" : "#15161c"} stroke={silhouette ? "#000" : "#2c2d38"} strokeWidth="1"/>
        <rect x="68" y="222" width="84" height="10" fill={silhouette ? "#000" : "#0a0a0c"} stroke={silhouette ? "#000" : "#2c2d38"} strokeWidth="1"/>
        <rect x="105" y="220" width="10" height="14" fill="var(--c2)" opacity={silhouette ? "0.3" : "0.8"}/>
      </g>

      <g className="char__head">
        <path d="M 80 70 Q 70 95 78 130 L 142 130 Q 150 95 140 70 Q 110 50 80 70 Z" fill={`url(#${silhouette ? 'cloak-sil' : 'cloak-g'})`}/>
        <ellipse cx="110" cy="100" rx="20" ry="22" fill={silhouette ? "#000" : "#050507"}/>
        <ellipse className="char__eye" cx="100" cy="98" rx="5" ry="3" fill={`url(#${silhouette ? 'eye-sil' : 'eye-g'})`}/>
        <ellipse className="char__eye" cx="120" cy="98" rx="5" ry="3" fill={`url(#${silhouette ? 'eye-sil' : 'eye-g'})`}/>
        <rect x="98" y="96.5" width="4" height="3" fill="var(--c1)" opacity={silhouette ? "0.5" : "1"}/>
        <rect x="118" y="96.5" width="4" height="3" fill="var(--c1)" opacity={silhouette ? "0.5" : "1"}/>
        <path d="M 78 72 Q 110 52 142 72" stroke={silhouette ? "#000" : "#2c2d38"} strokeWidth="1.2" fill="none"/>
      </g>

      <g className="char__arm-r">
        <path d="M 150 145 L 175 175 L 195 200" stroke={`url(#${silhouette ? 'armor-sil' : 'armor-g'})`} strokeWidth="14" strokeLinecap="round" fill="none"/>
        <circle cx="195" cy="200" r="9" fill={silhouette ? "#000" : "#1a1a22"} stroke={silhouette ? "#000" : "#2c2d38"} strokeWidth="1"/>
        <g className="char__sword">
          <rect x="186" y="194" width="22" height="4" fill={silhouette ? "#1a1a22" : "#3a3a48"} rx="1"/>
          <rect x="192" y="198" width="10" height="14" fill={silhouette ? "#000" : "#1a1a22"}/>
          <rect x="192" y="198" width="10" height="14" fill="none" stroke="var(--c1)" strokeWidth="0.6" opacity={silhouette ? "0.3" : "0.6"}/>
          <circle cx="197" cy="216" r="4" fill={silhouette ? "#1a1a22" : "#3a3a48"}/>
          <path d="M 195 192 L 197 192 L 205 -120 L 193 -120 Z" fill={`url(#${silhouette ? 'blade-sil' : 'blade-g'})`} stroke={silhouette ? "#000" : "#fff"} strokeWidth="0.4" opacity="0.95"/>
          <line x1="199" y1="190" x2="200" y2="-118" stroke={silhouette ? "#1a1a22" : "#ffffff"} strokeWidth="0.6" opacity="0.7"/>
          <circle className="char__sword-tip" cx="199" cy="-118" r="2.2" fill={silhouette ? "#1a1a22" : "#fff"}/>
        </g>
      </g>

      <g className="char__arm-l">
        <path d="M 70 145 L 50 180 L 60 215" stroke={`url(#${silhouette ? 'armor-sil' : 'armor-g'})`} strokeWidth="14" strokeLinecap="round" fill="none"/>
        <circle cx="60" cy="215" r="8" fill={silhouette ? "#000" : "#1a1a22"} stroke={silhouette ? "#000" : "#2c2d38"} strokeWidth="1"/>
      </g>
    </svg>
  );
}

window.Swordsman = Swordsman;
