// Looping stick-figure scenes used as backgrounds.
// Each scene animates on its own. The hero plays all five in 30-second slots (CSS hero-slot),
// and each industry banner shows the scene that matches its sector.

const SCENE_ORDER = ['office', 'logistics', 'hospital', 'law', 'factory'];

const INDUSTRY_SCENES = {
  proservices: 'law',
  logistics: 'logistics',
  homeservices: 'logistics',
  manufacturing: 'factory',
  medical: 'hospital',
  aerospace: 'factory',
  realestate: 'office',
};

const StickRunner = ({ s = 1.5, facing = 1 }) => (
  <g className="stick" transform={`scale(${facing * s} ${s})`}>
    <g className="fig-bob">
      <circle cx="0" cy="-72" r="11" />
      <line x1="0" y1="-32" x2="0" y2="-58" />
      <g transform="translate(0 -58)">
        <g className="arm-upper a"><line x1="0" y1="0" x2="0" y2="14" />
          <g transform="translate(0 14)"><g className="arm-fore a"><line x1="0" y1="0" x2="0" y2="12" /></g></g>
        </g>
      </g>
      <g transform="translate(0 -58)">
        <g className="arm-upper b"><line x1="0" y1="0" x2="0" y2="14" />
          <g transform="translate(0 14)"><g className="arm-fore b"><line x1="0" y1="0" x2="0" y2="12" /></g></g>
        </g>
      </g>
      <g transform="translate(0 -32)">
        <g className="thigh a"><line x1="0" y1="0" x2="0" y2="17" />
          <g transform="translate(0 17)"><g className="shin a"><line x1="0" y1="0" x2="0" y2="15" /></g></g>
        </g>
      </g>
      <g transform="translate(0 -32)">
        <g className="thigh b"><line x1="0" y1="0" x2="0" y2="17" />
          <g transform="translate(0 17)"><g className="shin b"><line x1="0" y1="0" x2="0" y2="15" /></g></g>
        </g>
      </g>
    </g>
  </g>
);

const StickSeated = ({ x, y, s = 1.4 }) => (
  <g className="stick" transform={`translate(${x} ${y}) scale(${s})`}>
    <circle cx="4" cy="-58" r="11" />
    <line x1="0" y1="0" x2="4" y2="-44" />
    <line x1="0" y1="0" x2="40" y2="0" />
    <line x1="40" y1="0" x2="40" y2="30" />
    <g transform="translate(4 -40)"><g className="type"><line x1="0" y1="0" x2="34" y2="16" /></g></g>
  </g>
);

const RunLane = ({ y, duration, offset = 0, children }) => (
  <g transform={`translate(0 ${y})`}>
    <g style={{
      animation: `run-across ${duration}s linear -${offset}s infinite`,
      '--cycle': `${(duration * 0.0453).toFixed(3)}s`,
    }}>
      {children}
    </g>
  </g>
);

const SceneFrame = ({ accent, children }) => (
  <svg className="scene-svg" viewBox="0 0 1600 900" preserveAspectRatio="xMidYMax slice" aria-hidden="true" focusable="false">
    <g fill="none" strokeWidth="4" stroke={accent} strokeLinecap="round">
      {children}
    </g>
  </svg>
);

const SceneOffice = () => {
  const accent = '#3A86FF';
  return (
    <SceneFrame accent={accent}>
      <g opacity="0.55">
        <rect x="80" y="140" width="340" height="260" />
        <line x1="250" y1="140" x2="250" y2="400" />
        <line x1="80" y1="270" x2="420" y2="270" />
        <rect x="520" y="180" width="120" height="160" />
        <line x1="200" y1="90" x2="1400" y2="90" />
      </g>
      <line x1="0" y1="780" x2="1600" y2="780" strokeWidth="5" opacity="0.7" />
      <g strokeWidth="5" opacity="0.85">
        <line x1="820" y1="740" x2="1080" y2="740" />
        <line x1="840" y1="740" x2="840" y2="780" />
        <line x1="1060" y1="740" x2="1060" y2="780" />
        <rect x="940" y="640" width="100" height="80" />
        <line x1="990" y1="720" x2="990" y2="740" />
        <line x1="1180" y1="740" x2="1420" y2="740" />
        <line x1="1200" y1="740" x2="1200" y2="780" />
        <line x1="1400" y1="740" x2="1400" y2="780" />
        <rect x="1280" y="660" width="100" height="80" />
        <line x1="1330" y1="740" x2="1330" y2="740" />
      </g>
      <rect x="950" y="650" width="80" height="60" fill={accent} opacity="0.18" stroke="none" />
      <rect x="1290" y="670" width="80" height="60" fill={accent} opacity="0.18" stroke="none" />
      <StickSeated x={880} y={756} />
      <StickSeated x={1240} y={756} />
      <rect x="1460" y="700" width="110" height="40" strokeWidth="5" />
      <g className="fly" style={{ animationDelay: '0s' }}><rect x="1480" y="640" width="26" height="32" fill="#E8F1FF" opacity="0.85" /></g>
      <g className="fly" style={{ animationDelay: '2s' }}><rect x="700" y="420" width="26" height="32" fill="#E8F1FF" opacity="0.85" /></g>
      <g className="fly" style={{ animationDelay: '4s' }}><rect x="620" y="500" width="26" height="32" fill="#E8F1FF" opacity="0.85" /></g>
      <g transform="translate(1420 200)" strokeWidth="5">
        <circle r="60" />
        <g className="spin" style={{ animationDuration: '60s' }}><line x1="0" y1="0" x2="0" y2="-44" /></g>
        <g className="spin" style={{ animationDuration: '6s' }}><line x1="0" y1="0" x2="26" y2="0" /></g>
      </g>
      <RunLane y={780} duration={11} offset={0}>
        <StickRunner s={1.5} />
      </RunLane>
      <RunLane y={780} duration={13} offset={6}>
        <StickRunner s={1.5} />
      </RunLane>
    </SceneFrame>
  );
};

const SceneLogistics = () => {
  const accent = '#06D6A0';
  return (
    <SceneFrame accent={accent}>
      <g opacity="0.4">
        <rect x="100" y="520" width="260" height="240" />
        <rect x="400" y="580" width="200" height="180" />
        <line x1="100" y1="520" x2="360" y2="460" />
        <line x1="360" y1="460" x2="360" y2="520" />
        <rect x="1180" y="560" width="120" height="120" />
        <rect x="1200" y="500" width="80" height="60" />
        <rect x="1320" y="540" width="200" height="140" />
      </g>
      <rect x="0" y="760" width="1600" height="140" fill="#142436" stroke="none" />
      <line x1="0" y1="760" x2="1600" y2="760" strokeWidth="5" />
      <line x1="0" y1="840" x2="1600" y2="840" strokeDasharray="60 40" strokeWidth="6" className="dash-flow" opacity="0.5" />
      <g className="truck">
        <g fill="#0D1B2A" strokeWidth="5" strokeLinejoin="round">
          <rect x="0" y="560" width="330" height="200" rx="6" />
          <path d="M340 620 L410 620 L460 690 L460 760 L340 760 Z" />
          <rect x="360" y="636" width="48" height="40" fill="none" strokeWidth="4" />
        </g>
        {[60, 250, 390].map((cx) => (
          <g key={cx} transform={`translate(${cx} 790)`}>
            <circle r="30" fill="#0D1B2A" strokeWidth="5" />
            <g className="spin" style={{ animationDuration: '0.8s' }}>
              <line x1="-22" y1="0" x2="22" y2="0" strokeWidth="4" />
              <line x1="0" y1="-22" x2="0" y2="22" strokeWidth="4" />
            </g>
          </g>
        ))}
      </g>
      <RunLane y={780} duration={9} offset={2}>
        <StickRunner s={1.5} />
      </RunLane>
      <RunLane y={780} duration={12} offset={7}>
        <StickRunner s={1.5} />
      </RunLane>
    </SceneFrame>
  );
};

const SceneHospital = () => {
  const accent = '#00B4D8';
  return (
    <SceneFrame accent={accent}>
      {[120, 420, 720, 1020, 1320].map((x) => (
        <rect key={x} x={x} y="100" width="160" height="12" fill={accent} opacity="0.6" stroke="none" />
      ))}
      <g opacity="0.45">
        <line x1="0" y1="300" x2="1600" y2="300" />
        <rect x="60" y="360" width="150" height="420" />
        <rect x="300" y="360" width="150" height="420" />
      </g>
      <g strokeWidth="14" stroke={accent} opacity="0.8">
        <line x1="580" y1="200" x2="580" y2="300" />
        <line x1="530" y1="250" x2="630" y2="250" />
      </g>
      <line x1="0" y1="780" x2="1600" y2="780" strokeWidth="5" opacity="0.7" />
      <g opacity="0.85" strokeWidth="5">
        <rect x="1180" y="480" width="320" height="200" />
        <path className="ecg" d="M1210 600 L1260 600 L1275 560 L1292 640 L1310 580 L1325 600 L1470 600" />
      </g>
      <RunLane y={780} duration={14} offset={0}>
        <g transform="translate(60 0)" strokeWidth="5">
          <line x1="0" y1="-62" x2="150" y2="-62" />
          <line x1="10" y1="-62" x2="10" y2="-14" />
          <line x1="140" y1="-62" x2="140" y2="-14" />
          <circle cx="10" cy="-6" r="8" />
          <circle cx="140" cy="-6" r="8" />
          <line x1="20" y1="-72" x2="110" y2="-72" />
          <circle cx="122" cy="-74" r="10" />
        </g>
        <g transform="translate(-70 0)">
          <StickRunner s={1.5} />
        </g>
      </RunLane>
      <RunLane y={780} duration={10} offset={5}>
        <StickRunner s={1.5} />
      </RunLane>
    </SceneFrame>
  );
};

const SceneLaw = () => {
  const accent = '#3A86FF';
  return (
    <SceneFrame accent={accent}>
      <g opacity="0.55">
        <rect x="60" y="160" width="280" height="620" />
        <line x1="60" y1="300" x2="340" y2="300" />
        <line x1="60" y1="440" x2="340" y2="440" />
        <line x1="60" y1="580" x2="340" y2="580" />
        {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((i) => (
          <rect key={`a${i}`} x={80 + i * 22} y={190} width="14" height={i % 3 === 0 ? 90 : 110} stroke="none" fill={accent} opacity="0.5" />
        ))}
        {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((i) => (
          <rect key={`b${i}`} x={80 + i * 22} y={330} width="14" height={i % 2 === 0 ? 100 : 80} stroke="none" fill={accent} opacity="0.5" />
        ))}
      </g>
      <line x1="0" y1="780" x2="1600" y2="780" strokeWidth="5" opacity="0.7" />
      <g opacity="0.85" strokeWidth="5">
        <line x1="1000" y1="300" x2="1000" y2="780" />
        <line x1="930" y1="780" x2="1070" y2="780" />
        <g transform="translate(1000 300)">
          <g className="scale-beam">
            <line x1="-170" y1="0" x2="170" y2="0" />
            <line x1="0" y1="0" x2="0" y2="-30" />
            <line x1="-170" y1="0" x2="-200" y2="110" />
            <line x1="-170" y1="0" x2="-140" y2="110" />
            <path d="M-210 110 L-130 110" />
            <line x1="170" y1="0" x2="200" y2="110" />
            <line x1="170" y1="0" x2="140" y2="110" />
            <path d="M130 110 L210 110" />
          </g>
        </g>
      </g>
      <g opacity="0.7" strokeWidth="5">
        <line x1="1200" y1="740" x2="1420" y2="740" />
        <line x1="1220" y1="740" x2="1220" y2="780" />
        <line x1="1400" y1="740" x2="1400" y2="780" />
      </g>
      <StickSeated x={1250} y={756} />
      <g className="fly" style={{ animationDelay: '1s' }}><rect x="560" y="420" width="26" height="32" fill="#E8F1FF" opacity="0.85" /></g>
      <g className="fly" style={{ animationDelay: '3.5s' }}><rect x="500" y="520" width="26" height="32" fill="#E8F1FF" opacity="0.85" /></g>
      <RunLane y={780} duration={9} offset={0}>
        <StickRunner s={1.5} />
      </RunLane>
      <RunLane y={780} duration={12} offset={6}>
        <StickRunner s={1.5} />
      </RunLane>
    </SceneFrame>
  );
};

const SceneFactory = () => {
  const accent = '#06D6A0';
  const boxes = [-1, 0, 1, 2, 3, 4, 5].map((k) => 260 + k * 200 + 40);
  return (
    <SceneFrame accent={accent}>
      <g opacity="0.5">
        <path d="M260 420 L360 340 L360 420 L460 340 L460 420 L560 340 L560 420 L660 340 L660 420 L1260 420" />
        <rect x="150" y="300" width="60" height="300" />
        <line x1="260" y1="420" x2="260" y2="640" />
        <line x1="1260" y1="420" x2="1260" y2="640" />
      </g>
      <g className="smoke-group" opacity="0.6">
        {[0, 1, 2].map((i) => (
          <circle key={i} className="smoke" cx="180" cy="300" r="22" fill="#E8F1FF" stroke="none" style={{ animationDelay: `${i * 1.3}s` }} />
        ))}
      </g>
      <line x1="0" y1="780" x2="1600" y2="780" strokeWidth="5" opacity="0.7" />
      <defs>
        <clipPath id="factory-belt-clip">
          <rect x="260" y="560" width="1000" height="100" />
        </clipPath>
      </defs>
      <rect x="260" y="640" width="1000" height="36" strokeWidth="5" />
      {[300, 500, 700, 900, 1100, 1240].map((cx) => (
        <circle key={cx} cx={cx} cy="658" r="14" strokeWidth="4" />
      ))}
      <g clipPath="url(#factory-belt-clip)" stroke={accent} strokeWidth="5">
        <g className="conveyor">
          {boxes.map((x) => (
            <rect key={x} x={x} y="578" width="90" height="62" fill="#0D1B2A" />
          ))}
        </g>
      </g>
      <g strokeWidth="22" stroke={accent} opacity="0.7">
        <circle cx="720" cy="520" r="60" strokeDasharray="22 16" />
      </g>
      <g transform="translate(720 520)" strokeWidth="5">
        <g className="spin" style={{ animationDuration: '8s' }}><circle r="30" /></g>
      </g>
      <g strokeWidth="18" stroke={accent} opacity="0.7">
        <circle cx="850" cy="545" r="42" strokeDasharray="18 14" />
      </g>
      <g transform="translate(850 545)" strokeWidth="5">
        <g className="spin-reverse" style={{ animationDuration: '6s' }}><circle r="18" /></g>
      </g>
      <g transform="translate(1400 700)" strokeWidth="6">
        <rect x="-40" y="0" width="80" height="80" />
        <g className="robot-a">
          <line x1="0" y1="0" x2="0" y2="-180" />
          <g transform="translate(0 -180)">
            <g className="robot-b">
              <line x1="0" y1="0" x2="120" y2="0" />
              <circle cx="120" cy="0" r="10" />
            </g>
          </g>
        </g>
      </g>
      <RunLane y={780} duration={10} offset={2}>
        <StickRunner s={1.5} />
      </RunLane>
      <RunLane y={780} duration={15} offset={8}>
        <StickRunner s={1.5} />
      </RunLane>
    </SceneFrame>
  );
};

const SCENE_COMPONENTS = {
  office: SceneOffice,
  logistics: SceneLogistics,
  hospital: SceneHospital,
  law: SceneLaw,
  factory: SceneFactory,
};

const HeroScenes = () => (
  <div className="absolute inset-0 overflow-hidden bg-[#0D1B2A]" aria-hidden="true">
    {SCENE_ORDER.map((key, i) => {
      const Scene = SCENE_COMPONENTS[key];
      return (
        <div key={key} className="hero-scene-layer" style={{ animationDelay: `${i * 5}s` }}>
          <Scene />
        </div>
      );
    })}
  </div>
);

const IndustryScene = ({ sceneKey }) => {
  const Scene = SCENE_COMPONENTS[sceneKey];
  return (
    <div className="absolute inset-0 overflow-hidden" aria-hidden="true">
      <Scene />
    </div>
  );
};

Object.assign(window, { HeroScenes, IndustryScene, INDUSTRY_SCENES });
