// MSA Electrical — Bolt's Journey
// 10-second seamless loop. A side-cutaway home at night. A bolt of current
// enters from the utility line, cascades through the panel, branches through
// the walls, and lights up each room in sequence — climaxing with the
// central pendant (the MSA bulb mark) glowing in brand cyan.

const HJ_COLORS = {
  night:      '#05070C',     // outdoor night sky
  yard:       '#0A0E18',     // ground / yard
  wall:       '#13171F',     // outer wall fill
  wallInner:  '#1A1F2A',     // inner wall fill (lighter)
  framing:    '#2A2F3A',     // stud lines / outlines
  framingLt:  '#3B4150',     // accent outlines
  trace:      '#00D4FF',     // brand cyan — current
  tracePaper: '#FFFFFF',     // bright pulse core
  panel:      '#0E1219',     // breaker-panel body
  warmGlow:   '#FFC976',     // incandescent warm light
  tvGlow:     '#7FBDFF',     // TV cool blue light
  evGreen:    '#1FBA6B',     // EV charger LED
  porchAmber: '#FFB05A',     // porch light
  paper:      '#FFFFFF',
  slate:      '#6B7280',
};

// ── House geometry ──────────────────────────────────────────────────────────
// Single-story side cutaway. Canvas 1920×1080. House occupies the middle.
//
// Coordinate plan:
//   Ground line:           y = 880
//   Roof apex:             y = 230
//   House left wall:       x = 220
//   House right wall:      x = 1700
//   Interior partitions:   x = 580 | 880 | 1180 | 1440
//
// Rooms (L→R):
//   220 …  580   Living room (TV + couch)
//   580 …  880   Kitchen (counters + appliances)
//   880 … 1180   Hallway + central pendant
//  1180 … 1440   Bedroom (bed + bedside lamp)
//  1440 … 1700   Garage (EV charger + car)

// ── Substrate: night + yard + far skyline ────────────────────────────────────

function HJ_Substrate() {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  return (
    <div style={{ position: 'absolute', inset: 0, opacity: fade }}>
      {/* gradient night sky */}
      <div style={{
        position: 'absolute', inset: 0,
        background:
          `radial-gradient(1200px 600px at 50% 100%, rgba(0,32,55,0.55), transparent 70%),
           linear-gradient(180deg, #05070C 0%, #080C18 60%, #060A12 100%)`,
      }}/>
      {/* faint stars */}
      <svg viewBox="0 0 1920 1080" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        {Array.from({length: 60}).map((_, i) => {
          const x = (i * 113) % 1920;
          const y = ((i * 79) % 440);
          const r = (i % 5 === 0) ? 1.4 : 0.8;
          return <circle key={i} cx={x} cy={y} r={r} fill="#fff" opacity={0.18 + (i % 3) * 0.12} />;
        })}
      </svg>
      {/* far skyline silhouette */}
      <svg viewBox="0 0 1920 1080" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <path
          d="M 0 760 L 80 760 L 80 720 L 160 720 L 160 700 L 250 700 L 250 740 L 340 740 L 340 690 L 430 690 L 430 720 L 520 720 L 520 760 L 620 760 L 620 740 L 700 740 L 700 760 L 1280 760 L 1280 740 L 1360 740 L 1360 760 L 1460 760 L 1460 720 L 1540 720 L 1540 700 L 1620 700 L 1620 740 L 1720 740 L 1720 760 L 1920 760 L 1920 780 L 0 780 Z"
          fill="#0A0E18"
        />
      </svg>
      {/* ground */}
      <div style={{
        position: 'absolute', left: 0, right: 0, top: 880, bottom: 0,
        background: 'linear-gradient(180deg, #0A0E18 0%, #050810 100%)',
      }}/>
    </div>
  );
}

// ── Utility pole + drop line (off-canvas left → into house) ─────────────────

function HJ_Utility() {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  return (
    <svg viewBox="0 0 1920 1080" style={{
      position: 'absolute', inset: 0, width: '100%', height: '100%', opacity: fade,
    }}>
      {/* pole */}
      <rect x="78" y="280" width="14" height="600" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.5" />
      {/* cross-arm */}
      <rect x="40" y="310" width="90" height="8" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1" />
      {/* insulators */}
      <circle cx="50" cy="310" r="5" fill="#2A2F3A" />
      <circle cx="120" cy="310" r="5" fill="#2A2F3A" />
      {/* transformer can */}
      <rect x="60" y="330" width="50" height="60" rx="4" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.5" />
      <line x1="60" y1="345" x2="110" y2="345" stroke="#2A2F3A" strokeWidth="1" />
      <line x1="60" y1="360" x2="110" y2="360" stroke="#2A2F3A" strokeWidth="1" />
      <line x1="60" y1="375" x2="110" y2="375" stroke="#2A2F3A" strokeWidth="1" />
    </svg>
  );
}

// ── House structure (walls, roof, interior partitions, ground) ──────────────

function HJ_House() {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  return (
    <svg viewBox="0 0 1920 1080" style={{
      position: 'absolute', inset: 0, width: '100%', height: '100%', opacity: fade,
    }}>
      {/* roof shadow */}
      <path d="M 200 580 L 960 230 L 1720 580 Z"
            fill="#0E1219" stroke="#2A2F3A" strokeWidth="2" strokeLinejoin="round" />
      {/* attic interior */}
      <path d="M 220 580 L 960 250 L 1700 580 Z" fill="#0A0E18" />
      {/* attic supports */}
      <line x1="500" y1="580" x2="700" y2="425" stroke="#2A2F3A" strokeWidth="1.2" opacity="0.5" />
      <line x1="1420" y1="580" x2="1220" y2="425" stroke="#2A2F3A" strokeWidth="1.2" opacity="0.5" />
      <line x1="780" y1="580" x2="860" y2="345" stroke="#2A2F3A" strokeWidth="1.2" opacity="0.5" />
      <line x1="1140" y1="580" x2="1060" y2="345" stroke="#2A2F3A" strokeWidth="1.2" opacity="0.5" />

      {/* main body */}
      <rect x="220" y="580" width="1480" height="300" fill="#13171F" stroke="#2A2F3A" strokeWidth="2.5" />
      {/* floor line */}
      <line x1="220" y1="870" x2="1700" y2="870" stroke="#2A2F3A" strokeWidth="2" />
      {/* interior partitions */}
      <line x1="580" y1="580" x2="580" y2="870" stroke="#2A2F3A" strokeWidth="1.5" />
      <line x1="880" y1="580" x2="880" y2="870" stroke="#2A2F3A" strokeWidth="1.5" />
      <line x1="1180" y1="580" x2="1180" y2="870" stroke="#2A2F3A" strokeWidth="1.5" />
      <line x1="1440" y1="580" x2="1440" y2="870" stroke="#2A2F3A" strokeWidth="1.5" />
      {/* ceiling line (cavity for wires) */}
      <line x1="220" y1="610" x2="1700" y2="610" stroke="#2A2F3A" strokeWidth="1" strokeDasharray="4 4" opacity="0.4" />

      {/* door (front, on left wall) */}
      <rect x="200" y="730" width="22" height="140" fill="#1A1F2A" stroke="#3B4150" strokeWidth="1.2" />
      <circle cx="205" cy="800" r="2" fill="#3B4150" />

      {/* porch slab */}
      <rect x="140" y="870" width="80" height="14" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1" />
    </svg>
  );
}

// ── Furniture / fixtures per room (decorative, unlit base) ──────────────────

function HJ_Furnishings() {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  return (
    <svg viewBox="0 0 1920 1080" style={{
      position: 'absolute', inset: 0, width: '100%', height: '100%', opacity: fade,
    }}>
      {/* ===== Living room (220–580) ===== */}
      {/* TV on wall */}
      <rect x="265" y="690" width="170" height="100" rx="3" fill="#0A0D12" stroke="#2A2F3A" strokeWidth="1.5" />
      <rect x="270" y="695" width="160" height="90" fill="#0A0D12" />
      <rect x="335" y="790" width="30" height="6" fill="#1A1F2A" />
      <rect x="320" y="796" width="60" height="4" fill="#1A1F2A" />
      {/* couch */}
      <rect x="270" y="820" width="140" height="46" rx="6" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.2" />
      <rect x="280" y="810" width="40" height="30" rx="4" fill="#13171F" stroke="#2A2F3A" strokeWidth="1" />
      <rect x="365" y="810" width="40" height="30" rx="4" fill="#13171F" stroke="#2A2F3A" strokeWidth="1" />
      {/* lamp */}
      <rect x="445" y="800" width="14" height="60" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1" />
      <path d="M 440 780 L 466 780 L 460 800 L 446 800 Z" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1" />
      <rect x="440" y="860" width="26" height="8" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1" />
      {/* outlet */}
      <rect x="500" y="830" width="14" height="20" rx="1" fill="#0A0D12" stroke="#2A2F3A" strokeWidth="1" />
      <circle cx="504" cy="836" r="1" fill="#3B4150" />
      <circle cx="510" cy="836" r="1" fill="#3B4150" />
      <rect x="503" y="842" width="8" height="2" fill="#3B4150" />
      {/* room label */}
      <text x="400" y="650" textAnchor="middle" fontFamily="'JetBrains Mono', monospace" fontSize="11"
            letterSpacing="0.18em" fill="#3B4150">LIVING</text>

      {/* ===== Kitchen (580–880) ===== */}
      {/* upper cabinets */}
      <rect x="600" y="640" width="260" height="50" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.2" />
      <line x1="690" y1="640" x2="690" y2="690" stroke="#2A2F3A" strokeWidth="1" />
      <line x1="770" y1="640" x2="770" y2="690" stroke="#2A2F3A" strokeWidth="1" />
      {/* under-cabinet rail (where LEDs will glow) */}
      <rect x="600" y="690" width="260" height="3" fill="#0A0D12" />
      {/* counter */}
      <rect x="600" y="770" width="260" height="10" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.2" />
      {/* fridge */}
      <rect x="785" y="780" width="68" height="86" rx="2" fill="#13171F" stroke="#2A2F3A" strokeWidth="1.2" />
      <line x1="785" y1="810" x2="853" y2="810" stroke="#2A2F3A" strokeWidth="1" />
      <rect x="843" y="788" width="2" height="14" fill="#3B4150" />
      <rect x="843" y="818" width="2" height="40" fill="#3B4150" />
      {/* stove */}
      <rect x="610" y="780" width="70" height="86" rx="2" fill="#13171F" stroke="#2A2F3A" strokeWidth="1.2" />
      <circle cx="623" cy="800" r="5" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="0.8" />
      <circle cx="643" cy="800" r="5" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="0.8" />
      <circle cx="663" cy="800" r="5" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="0.8" />
      <rect x="615" y="815" width="60" height="38" rx="2" fill="#0A0D12" stroke="#2A2F3A" strokeWidth="0.8" />
      {/* kitchen base cabinets */}
      <rect x="680" y="780" width="105" height="86" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.2" />
      <line x1="730" y1="780" x2="730" y2="866" stroke="#2A2F3A" strokeWidth="1" />
      <circle cx="725" cy="820" r="1.5" fill="#3B4150" />
      <circle cx="735" cy="820" r="1.5" fill="#3B4150" />
      <text x="730" y="650" textAnchor="middle" fontFamily="'JetBrains Mono', monospace" fontSize="11"
            letterSpacing="0.18em" fill="#3B4150">KITCHEN</text>

      {/* ===== Hallway / Pendant zone (880–1180) ===== */}
      {/* the pendant mount on the ceiling */}
      <rect x="1020" y="610" width="20" height="6" fill="#1A1F2A" />
      <line x1="1030" y1="616" x2="1030" y2="700" stroke="#3B4150" strokeWidth="1.5" />
      {/* (the bulb mark will sit at ~y=700; rendered separately as PNG layer) */}
      {/* a console/cabinet below */}
      <rect x="965" y="820" width="130" height="44" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.2" />
      <line x1="1030" y1="820" x2="1030" y2="864" stroke="#2A2F3A" strokeWidth="1" />
      <text x="1030" y="650" textAnchor="middle" fontFamily="'JetBrains Mono', monospace" fontSize="11"
            letterSpacing="0.18em" fill="#3B4150">HALL</text>

      {/* ===== Bedroom (1180–1440) ===== */}
      {/* bed */}
      <rect x="1200" y="800" width="190" height="66" rx="3" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.2" />
      <rect x="1200" y="788" width="46" height="40" rx="3" fill="#13171F" stroke="#2A2F3A" strokeWidth="1.2" />
      <rect x="1210" y="796" width="120" height="14" rx="3" fill="#2A2F3A" />
      {/* pillows */}
      <rect x="1210" y="804" width="36" height="14" rx="2" fill="#3B4150" />
      {/* bedside table + lamp */}
      <rect x="1390" y="800" width="36" height="66" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1.2" />
      <rect x="1404" y="754" width="8" height="46" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="0.8" />
      <path d="M 1396 730 L 1420 730 L 1416 754 L 1400 754 Z" fill="#1A1F2A" stroke="#2A2F3A" strokeWidth="1" />
      {/* wall switch */}
      <rect x="1190" y="730" width="9" height="14" rx="1" fill="#0A0D12" stroke="#2A2F3A" strokeWidth="0.8" />
      <text x="1310" y="650" textAnchor="middle" fontFamily="'JetBrains Mono', monospace" fontSize="11"
            letterSpacing="0.18em" fill="#3B4150">BEDROOM</text>

      {/* ===== Garage (1440–1700) ===== */}
      {/* garage door */}
      <rect x="1455" y="700" width="225" height="170" fill="#0E1219" stroke="#2A2F3A" strokeWidth="1.2" />
      {Array.from({length: 5}).map((_, i) => (
        <line key={i} x1="1455" y1={730 + i * 32} x2="1680" y2={730 + i * 32}
              stroke="#2A2F3A" strokeWidth="0.8" />
      ))}
      {/* parked car (silhouette) */}
      <path d="M 1500 836
               L 1510 808 L 1545 800 L 1610 800 L 1640 808 L 1656 836
               L 1656 856 L 1500 856 Z"
            fill="#1A1F2A" stroke="#3B4150" strokeWidth="1.2" strokeLinejoin="round" />
      <path d="M 1518 822 L 1538 810 L 1612 810 L 1634 822 Z" fill="#0A0D12" />
      {/* wheels */}
      <circle cx="1525" cy="858" r="9" fill="#0A0D12" stroke="#3B4150" strokeWidth="1" />
      <circle cx="1632" cy="858" r="9" fill="#0A0D12" stroke="#3B4150" strokeWidth="1" />
      {/* EV charger box mounted on the wall */}
      <rect x="1450" y="730" width="22" height="44" rx="3" fill="#0A0D12" stroke="#3B4150" strokeWidth="1.2" />
      {/* cable connecting to car */}
      <path d="M 1461 774 Q 1465 800 1500 808" fill="none" stroke="#1A1F2A" strokeWidth="3" strokeLinecap="round" />
      <text x="1570" y="650" textAnchor="middle" fontFamily="'JetBrains Mono', monospace" fontSize="11"
            letterSpacing="0.18em" fill="#3B4150">GARAGE</text>

      {/* ===== Meter + breaker panel (on left exterior wall) ===== */}
      {/* utility drop entering at top of wall */}
      <line x1="92" y1="340" x2="200" y2="540" stroke="#2A2F3A" strokeWidth="1.8" strokeLinecap="round" />
      {/* meter (outside) */}
      <rect x="148" y="540" width="48" height="60" rx="4" fill="#0E1219" stroke="#3B4150" strokeWidth="1.5" />
      <circle cx="172" cy="568" r="14" fill="#1A1F2A" stroke="#3B4150" strokeWidth="1.2" />
      <text x="172" y="595" textAnchor="middle" fontFamily="'JetBrains Mono', monospace" fontSize="7"
            letterSpacing="0.1em" fill="#3B4150">METER</text>
      {/* breaker panel (interior, just inside left wall) */}
      <rect x="240" y="640" width="60" height="120" rx="3" fill="#0E1219" stroke="#3B4150" strokeWidth="1.5" />
      <line x1="270" y1="640" x2="270" y2="760" stroke="#3B4150" strokeWidth="0.8" />
      {/* main breaker */}
      <rect x="246" y="646" width="48" height="10" rx="1.5" fill="#1A1F2A" stroke="#3B4150" strokeWidth="0.8" />
      <text x="304" y="654" fontFamily="'JetBrains Mono', monospace" fontSize="7"
            fill="#3B4150" letterSpacing="0.1em">200A</text>
    </svg>
  );
}

// ── Wiring: paths that the bolt travels through ─────────────────────────────
// All paths use pathLength="100" so progress 0..1 ↔ dashoffset 0..100.

const HJ_TRACES = [
  // 0. utility drop into meter, then to panel (the main entry)
  { id: 'main',
    d: 'M 92 340 L 172 540 L 172 600 L 220 600 L 220 700 L 240 700',
    delay: 0.6, dur: 0.9 },

  // 1. panel → ceiling cavity (left): heads up, then runs right inside the ceiling.
  //    Branches drop down to each room.
  { id: 'ceiling',
    d: 'M 270 640 L 270 605 L 1700 605',
    delay: 1.5, dur: 1.3 },

  // 2. drop to living-room TV
  { id: 'tv',
    d: 'M 355 605 L 355 690',
    delay: 1.9, dur: 0.35 },

  // 3. drop to living-room lamp + outlet
  { id: 'lamp',
    d: 'M 505 605 L 505 830',
    delay: 2.0, dur: 0.45 },

  // 4. drop to kitchen under-cabinet rail (and along the rail)
  { id: 'kitchen',
    d: 'M 705 605 L 705 690 L 600 690 L 705 690 L 860 690',
    delay: 2.2, dur: 0.65 },

  // 5. drop to central pendant
  { id: 'pendant',
    d: 'M 1030 605 L 1030 700',
    delay: 2.5, dur: 0.6 },

  // 6. drop to bedside table outlet, then up the lamp stem
  { id: 'bedlamp',
    d: 'M 1410 605 L 1410 800 L 1408 754',
    delay: 2.4, dur: 0.55 },

  // 7. drop to EV charger
  { id: 'ev',
    d: 'M 1461 605 L 1461 730',
    delay: 2.6, dur: 0.5 },

  // 8. exterior porch light (via switched leg out the front door header)
  { id: 'porch',
    d: 'M 220 605 L 220 720 L 175 720',
    delay: 2.7, dur: 0.5 },
];

function HJ_Wiring() {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  return (
    <svg viewBox="0 0 1920 1080" style={{
      position: 'absolute', inset: 0, width: '100%', height: '100%', opacity: fade,
    }}>
      {HJ_TRACES.map((tr) => {
        const local = t - tr.delay;
        const prog = clamp(local / tr.dur, 0, 1);
        const lit = prog >= 1;
        const pulseVisible = local >= 0 && prog < 1;
        return (
          <g key={tr.id}>
            {/* the wire path: revealed as the bolt passes */}
            <path d={tr.d} pathLength="100" fill="none"
                  stroke={HJ_COLORS.trace} strokeWidth="2"
                  strokeLinecap="round" strokeLinejoin="round"
                  strokeDasharray={`${prog * 100} 100`}
                  opacity={lit ? 0.65 : 0.5}
                  style={{ filter: 'drop-shadow(0 0 4px rgba(0,212,255,0.55))' }}
            />
            {/* inner core */}
            <path d={tr.d} pathLength="100" fill="none"
                  stroke={HJ_COLORS.paper} strokeWidth="0.7"
                  strokeLinecap="round"
                  strokeDasharray={`${prog * 100} 100`}
                  opacity={lit ? 0.3 : 0.6}
            />
            {/* leading bolt — short bright dash that travels along the path */}
            {pulseVisible && (
              <path d={tr.d} pathLength="100" fill="none"
                    stroke={HJ_COLORS.paper} strokeWidth="4.5"
                    strokeLinecap="round"
                    strokeDasharray={`2.5 200`}
                    strokeDashoffset={-prog * 100 + 2.5}
                    style={{ filter: 'drop-shadow(0 0 6px #00D4FF) drop-shadow(0 0 14px rgba(0,212,255,0.95))' }}
              />
            )}
          </g>
        );
      })}
    </svg>
  );
}

// ── Room-glow overlays — soft warm light pouring out of a room when lit ─────

function HJ_Glow({ x, y, w, h, color, litAt, intensity = 0.65, rise = 0.4 }) {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  const k = Easing.easeOutCubic(clamp((t - litAt) / rise, 0, 1));
  // gentle flicker on first 0.25s of rise
  const flick = (t > litAt && t < litAt + 0.25)
    ? 0.6 + 0.4 * Math.abs(Math.sin((t - litAt) * 60))
    : 1;
  const op = k * intensity * flick * fade;
  return (
    <div style={{
      position: 'absolute',
      left: x, top: y, width: w, height: h,
      background: `radial-gradient(ellipse at center, ${color} 0%, transparent 70%)`,
      mixBlendMode: 'screen',
      opacity: op,
      pointerEvents: 'none',
      filter: `blur(8px)`,
    }}/>
  );
}

// Small bright point of light at a specific fixture (lamp head, LED, etc.)
function HJ_Point({ x, y, color, litAt, r = 14, intensity = 1, rise = 0.35, flicker = false }) {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  let k = Easing.easeOutCubic(clamp((t - litAt) / rise, 0, 1));
  if (flicker && t > litAt && t < litAt + 0.3) {
    k *= 0.5 + 0.5 * Math.abs(Math.sin((t - litAt) * 70));
  }
  const op = k * intensity * fade;
  return (
    <div style={{
      position: 'absolute',
      left: x - r * 4, top: y - r * 4,
      width: r * 8, height: r * 8,
      background: `radial-gradient(circle, ${color} 0%, ${color}88 25%, transparent 70%)`,
      mixBlendMode: 'screen',
      opacity: op,
      pointerEvents: 'none',
      filter: 'blur(4px)',
    }}/>
  );
}

// ── Meter dial spin + panel breaker cascade ─────────────────────────────────

function HJ_PanelLife() {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  // meter dial: starts spinning at t≈1.0s; runs through 4s
  const meterT = clamp((t - 1.0) / 3.0, 0, 1);
  const angle = meterT * 720; // 2 full rotations
  const meterLit = clamp((t - 1.0) / 0.3, 0, 1);

  // breaker cascade: 8 small breakers light up between 1.4s and 2.2s
  const breakerStart = 1.4;
  const breakerStep = 0.08;
  return (
    <svg viewBox="0 0 1920 1080" style={{
      position: 'absolute', inset: 0, width: '100%', height: '100%', opacity: fade,
      pointerEvents: 'none',
    }}>
      {/* spinning meter needle */}
      <g transform={`translate(172, 568) rotate(${angle})`}>
        <line x1="0" y1="0" x2="0" y2="-10" stroke={HJ_COLORS.trace} strokeWidth="1.5"
              strokeLinecap="round" opacity={meterLit}
              style={{ filter: meterLit > 0 ? 'drop-shadow(0 0 4px #00D4FF)' : 'none' }} />
      </g>
      {/* meter window glow */}
      <circle cx="172" cy="568" r="14" fill={HJ_COLORS.trace} opacity={meterLit * 0.2}
              style={{ filter: 'drop-shadow(0 0 8px rgba(0,212,255,0.7))' }} />

      {/* breakers (cascade) */}
      {Array.from({length: 8}).map((_, i) => {
        const litT = breakerStart + i * breakerStep;
        const k = clamp((t - litT) / 0.15, 0, 1);
        const col = i % 2 === 0 ? 246 : 274;
        const row = 670 + Math.floor(i / 2) * 22;
        return (
          <g key={i}>
            <rect x={col} y={row} width="22" height="8" rx="1.5"
                  fill="#1A1F2A" stroke="#3B4150" strokeWidth="0.7" />
            <rect x={col + (k > 0 ? 0 : 14)} y={row + 2.5} width="6" height="3" rx="0.5"
                  fill={HJ_COLORS.trace} opacity={k}
                  style={{ filter: k > 0 ? 'drop-shadow(0 0 3px #00D4FF)' : 'none' }} />
          </g>
        );
      })}
    </svg>
  );
}

// ── The MSA pendant (center bulb) — the climax ──────────────────────────────

function HJ_Pendant() {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);

  // power-on starts at 5.5s, settles at 6.5s; flicker for first 0.4s
  const onStart = 5.5;
  const baseK = Easing.easeOutCubic(clamp((t - onStart) / 1.0, 0, 1));
  let intensity = baseK;
  if (t > onStart && t < onStart + 0.4) {
    intensity *= 0.55 + 0.45 * Math.abs(Math.sin((t - onStart) * 70));
  }
  // breathing during hold (6.5–8.5)
  if (t > 6.5 && t < 8.5) {
    intensity *= 0.92 + 0.08 * Math.sin((t - 6.5) * 2.4);
  }

  const k = intensity;
  const size = 220;
  return (
    <div style={{
      position: 'absolute',
      left: 1030 - size / 2,
      top: 700 - size / 2 + 30,
      width: size, height: size,
      pointerEvents: 'none',
      opacity: fade,
    }}>
      {/* huge halo */}
      <div style={{
        position: 'absolute', inset: -size,
        background: `radial-gradient(circle, rgba(0,212,255,${0.42 * k}) 0%, rgba(0,153,204,${0.22 * k}) 25%, transparent 60%)`,
        filter: `blur(${18 + k * 28}px)`,
        mixBlendMode: 'screen',
      }}/>
      {/* tighter halo */}
      <div style={{
        position: 'absolute', inset: -28,
        background: `radial-gradient(circle, rgba(0,212,255,${0.65 * k}) 0%, transparent 65%)`,
        filter: 'blur(6px)',
        mixBlendMode: 'screen',
      }}/>
      <img
        src="assets/MSALogo-mark.png"
        alt=""
        style={{
          width: '100%', height: '100%',
          objectFit: 'contain',
          opacity: 0.18 + 0.82 * k,
          transform: `scale(${0.94 + k * 0.08})`,
          transformOrigin: 'center',
          filter: `drop-shadow(0 0 ${60 + k * 80}px rgba(0,212,255,${0.9 * k})) drop-shadow(0 0 ${20 + k * 30}px rgba(0,212,255,${k}))`,
        }}
      />
    </div>
  );
}

// ── Whole-house ambient glow (the moment the pendant is full) ───────────────

function HJ_AmbientWash() {
  const t = useTime();
  const fade = clamp(t / 0.5, 0, 1) * (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);
  const k = Easing.easeOutCubic(clamp((t - 6.0) / 1.0, 0, 1));
  return (
    <div style={{
      position: 'absolute', left: 220, top: 580, width: 1480, height: 290,
      background: `radial-gradient(ellipse at 55% 50%, rgba(0,212,255,${0.10 * k}) 0%, rgba(255,201,118,${0.04 * k}) 50%, transparent 75%)`,
      mixBlendMode: 'screen',
      opacity: fade,
      pointerEvents: 'none',
    }}/>
  );
}

// ── Tagline & branding overlay ──────────────────────────────────────────────

function HJ_Overlay() {
  const t = useTime();
  const fade = (t > 9.5 ? clamp((10 - t) / 0.5, 0, 1) : 1);

  const introT = Easing.easeOutCubic(clamp((t - 0.4) / 0.5, 0, 1));
  const tagT  = Easing.easeOutCubic(clamp((t - 7.4) / 0.55, 0, 1));
  const tag2T = Easing.easeOutCubic(clamp((t - 7.8) / 0.55, 0, 1));
  const botT  = Easing.easeOutCubic(clamp((t - 8.3) / 0.5, 0, 1));

  return (
    <div style={{
      position: 'absolute', inset: 0,
      color: HJ_COLORS.paper,
      opacity: fade,
      fontFamily: "'Inter', system-ui, sans-serif",
      pointerEvents: 'none',
    }}>
      {/* top-left status */}
      <div style={{
        position: 'absolute', top: 56, left: 64,
        opacity: introT,
        transform: `translateY(${(1 - introT) * 6}px)`,
        display: 'flex', alignItems: 'center', gap: 14,
        fontFamily: "'JetBrains Mono', monospace",
        fontSize: 14,
        letterSpacing: '0.22em',
        textTransform: 'uppercase',
        color: HJ_COLORS.trace,
      }}>
        <span style={{
          width: 8, height: 8, borderRadius: 8, background: HJ_COLORS.trace,
          boxShadow: `0 0 12px ${HJ_COLORS.trace}`,
          animation: 'msaPulse 1.2s infinite',
        }}/>
        <span>MSA Electrical · Service call</span>
      </div>

      {/* top-right meta */}
      <div style={{
        position: 'absolute', top: 56, right: 64,
        opacity: introT,
        transform: `translateY(${(1 - introT) * 6}px)`,
        fontFamily: "'JetBrains Mono', monospace",
        fontSize: 12,
        letterSpacing: '0.18em',
        textTransform: 'uppercase',
        color: 'rgba(255,255,255,0.55)',
        textAlign: 'right',
        lineHeight: 1.7,
      }}>
        <div>23:47 · Edmonton, AB</div>
        <div>Power restored</div>
      </div>

      {/* main tagline */}
      <div style={{
        position: 'absolute',
        left: 0, right: 0, top: 100,
        textAlign: 'center',
        fontFamily: "'Montserrat', system-ui, sans-serif",
        fontWeight: 900,
        fontSize: 82,
        lineHeight: 0.95,
        letterSpacing: '-0.03em',
      }}>
        <div style={{
          opacity: tagT,
          transform: `translateY(${(1 - tagT) * 14}px)`,
        }}>
          Reliable Power.
        </div>
        <div style={{
          opacity: tag2T,
          transform: `translateY(${(1 - tag2T) * 14}px)`,
          color: HJ_COLORS.trace,
          textShadow: `0 0 30px rgba(0, 212, 255, 0.65)`,
        }}>
          Unmatched Performance.
        </div>
      </div>

      {/* bottom MSA line */}
      <div style={{
        position: 'absolute',
        left: 0, right: 0, bottom: 64,
        textAlign: 'center',
        opacity: botT,
        transform: `translateY(${(1 - botT) * 6}px)`,
        fontFamily: "'JetBrains Mono', monospace",
        fontSize: 14,
        letterSpacing: '0.32em',
        textTransform: 'uppercase',
        color: 'rgba(255,255,255,0.6)',
      }}>
        <span style={{ color: HJ_COLORS.paper, fontWeight: 600 }}>MSA Electrical Ltd.</span>
        <span style={{ margin: '0 16px', color: 'rgba(255,255,255,0.25)' }}>·</span>
        <span>780.278.7443</span>
        <span style={{ margin: '0 16px', color: 'rgba(255,255,255,0.25)' }}>·</span>
        <span>Residential · Commercial · Edmonton</span>
      </div>

      {/* small process labels that fly in as each room lights */}
      <RoomLabel x={400} y={920} label="01 · TV"      time={2.25} />
      <RoomLabel x={730} y={920} label="02 · KITCHEN" time={2.85} />
      <RoomLabel x={1030} y={920} label="03 · PENDANT" time={3.1} />
      <RoomLabel x={1310} y={920} label="04 · BEDROOM" time={2.95} />
      <RoomLabel x={1570} y={920} label="05 · EV"     time={3.1} />
      <RoomLabel x={175} y={760}  label="06 · PORCH"  time={3.2} />

      <style>{`@keyframes msaPulse {
        0%, 100% { opacity: 1; }
        50% { opacity: 0.3; }
      }`}</style>
    </div>
  );
}

function RoomLabel({ x, y, label, time }) {
  const t = useTime();
  const k = Easing.easeOutCubic(clamp((t - time) / 0.4, 0, 1));
  const fadeOut = clamp((t - (time + 2.5)) / 0.5, 0, 1);
  const op = k * (1 - fadeOut);
  return (
    <div style={{
      position: 'absolute', left: x, top: y,
      transform: `translate(-50%, ${(1 - k) * 6}px)`,
      opacity: op,
      fontFamily: "'JetBrains Mono', monospace",
      fontSize: 11,
      letterSpacing: '0.18em',
      textTransform: 'uppercase',
      color: 'rgba(0, 212, 255, 0.9)',
      whiteSpace: 'nowrap',
    }}>
      <span style={{
        display: 'inline-block', width: 6, height: 6, borderRadius: 6,
        background: HJ_COLORS.trace, marginRight: 8, verticalAlign: 'middle',
        boxShadow: `0 0 8px ${HJ_COLORS.trace}`,
      }}/>
      {label}
    </div>
  );
}

// ── Compose the whole scene ─────────────────────────────────────────────────

function BoltJourneyScene() {
  return (
    <React.Fragment>
      <HJ_Substrate />
      <HJ_Utility />
      <HJ_House />
      <HJ_Furnishings />
      <HJ_Wiring />
      <HJ_PanelLife />

      {/* per-room light states — these are positioned in absolute coords matching the SVG canvas */}
      {/* TV glow — cool blue */}
      <HJ_Glow x={245} y={680} w={210} h={130} color={HJ_COLORS.tvGlow} litAt={2.25} intensity={0.7} />
      <HJ_Point x={350} y={740} color={HJ_COLORS.tvGlow} litAt={2.25} r={28} intensity={0.55} flicker />
      {/* Living room lamp */}
      <HJ_Glow x={400} y={760} w={170} h={130} color={HJ_COLORS.warmGlow} litAt={2.45} intensity={0.65} />
      <HJ_Point x={453} y={790} color={HJ_COLORS.warmGlow} litAt={2.45} r={26} intensity={0.7} />
      {/* Kitchen under-cabinet */}
      <HJ_Glow x={595} y={665} w={270} h={90} color={HJ_COLORS.warmGlow} litAt={2.85} intensity={0.75} />
      <HJ_Point x={730} y={695} color={HJ_COLORS.warmGlow} litAt={2.85} r={30} intensity={0.55} />
      {/* Bedroom lamp */}
      <HJ_Glow x={1370} y={720} w={120} h={120} color={HJ_COLORS.warmGlow} litAt={2.95} intensity={0.7} />
      <HJ_Point x={1408} y={742} color={HJ_COLORS.warmGlow} litAt={2.95} r={22} intensity={0.7} flicker />
      {/* EV charger LED */}
      <HJ_Point x={1461} y={748} color={HJ_COLORS.evGreen} litAt={3.1} r={14} intensity={1} flicker />
      <HJ_Glow x={1440} y={720} w={50} h={70} color={HJ_COLORS.evGreen} litAt={3.1} intensity={0.6} />
      {/* Porch light */}
      <HJ_Point x={175} y={720} color={HJ_COLORS.porchAmber} litAt={3.2} r={28} intensity={0.85} flicker />
      <HJ_Glow x={120} y={680} w={140} h={140} color={HJ_COLORS.porchAmber} litAt={3.2} intensity={0.55} />

      {/* small EV charger panel light (a tiny green dot on the wall box) */}
      <div style={{ position: 'absolute', left: 0, top: 0, right: 0, bottom: 0, pointerEvents: 'none' }}>
        <svg viewBox="0 0 1920 1080" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
          <HJ_EVStatusDot />
          <HJ_TVScreen />
        </svg>
      </div>

      <HJ_AmbientWash />
      <HJ_Pendant />
      <HJ_Overlay />
    </React.Fragment>
  );
}

// Bright SVG dot on the EV charger (on top of the wall box rendering)
function HJ_EVStatusDot() {
  const t = useTime();
  const k = Easing.easeOutCubic(clamp((t - 3.1) / 0.4, 0, 1));
  const pulse = 0.7 + 0.3 * Math.sin((t - 3.1) * 4);
  return (
    <g>
      <circle cx="1461" cy="744" r="3" fill={HJ_COLORS.evGreen}
              opacity={k * pulse}
              style={{ filter: 'drop-shadow(0 0 6px rgba(31,186,107,0.95))' }} />
    </g>
  );
}

// TV screen content (a soft gradient swatch on the TV face)
function HJ_TVScreen() {
  const t = useTime();
  const k = Easing.easeOutCubic(clamp((t - 2.25) / 0.5, 0, 1));
  const flick = t > 2.25 && t < 2.55 ? 0.5 + 0.5 * Math.abs(Math.sin((t - 2.25) * 60)) : 1;
  return (
    <g opacity={k * flick}>
      <defs>
        <linearGradient id="hj-tv" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0%" stopColor="#7FBDFF" />
          <stop offset="100%" stopColor="#3676C4" />
        </linearGradient>
      </defs>
      <rect x="270" y="695" width="160" height="90" fill="url(#hj-tv)" opacity="0.75" />
      {/* scanlines */}
      {Array.from({length: 8}).map((_, i) => (
        <rect key={i} x="270" y={697 + i * 11} width="160" height="0.7" fill="#fff" opacity="0.08" />
      ))}
    </g>
  );
}

Object.assign(window, { BoltJourneyScene, HJ_COLORS, HJ_TRACES });
