/* =========================================================
   CarDealer.ai Home — Components
   Reuses sg-parts: Icon, Btn, Badge, AgentAvatar, Bubble,
   VoiceBars, TypingDots, AGENTS, AGENT_BY.
   ========================================================= */
const { useState, useEffect, useRef } = React;

/* ---------- Reusable section header ---------- */
function SectionHead({ kicker, title, body, cta, align = "left", maxTitle = 820, dark }) {
  return (
    <div style={{
      textAlign: align,
      maxWidth: align === "center" ? 820 : "100%",
      margin: align === "center" ? "0 auto" : 0,
      marginBottom: 56,
      display: "flex",
      flexDirection: "column",
      alignItems: align === "center" ? "center" : "flex-start",
    }}>
      {kicker ? <div className="kicker" style={{ color: dark ? "var(--cobalt-300)" : "var(--cobalt-600)" }}>{kicker}</div> : null}
      {title ? <h2 className="h-section-title" style={{ maxWidth: maxTitle, color: dark ? "#fff" : "var(--ink)" }}>{title}</h2> : null}
      {body ? <p className="h-section-body" style={{ color: dark ? "rgba(255,255,255,0.72)" : "var(--body)" }}>{body}</p> : null}
      {cta ? <div style={{ marginTop: 24 }}>{cta}</div> : null}
    </div>
  );
}

/* ========== Navigation ========== */
function HomeNav() {
  const [open, setOpen] = useState(false);

  // Close on outside click
  useEffect(() => {
    function handler(e) {
      if (!e.target.closest("[data-nav-menu]")) setOpen(false);
    }
    document.addEventListener("click", handler);
    return () => document.removeEventListener("click", handler);
  }, []);

  return (
    <header style={{
      position: "sticky", top: 0, zIndex: 50,
      background: "rgba(255,255,255,0.85)",
      backdropFilter: "saturate(180%) blur(12px)",
      WebkitBackdropFilter: "saturate(180%) blur(12px)",
      borderBottom: "1px solid var(--line)",
    }}>
      <div className="page" style={{
        display: "flex", alignItems: "center", height: 72, gap: 32,
      }}>
        <a href="#" style={{ display: "flex", alignItems: "center" }}>
          <img src="assets/logo/logo-horizontal.svg" alt="CarDealer.ai" style={{ height: 28 }} />
        </a>

        <nav style={{ display: "flex", alignItems: "center", gap: 8 }} data-nav-menu>
          <button onClick={(e) => { e.stopPropagation(); setOpen(o => !o); }}
            style={{
              display: "inline-flex", alignItems: "center", gap: 6,
              padding: "10px 14px", borderRadius: 10,
              border: "none", background: open ? "var(--cobalt-50)" : "transparent",
              color: open ? "var(--cobalt-700)" : "var(--ink)",
              fontFamily: "var(--font-body)", fontSize: 15, fontWeight: 500, cursor: "pointer",
            }}>
            Products
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round">
              <path d={open ? "M6 15l6-6 6 6" : "M6 9l6 6 6-6"}/>
            </svg>
          </button>

          {open ? <MegaMenu onClose={() => setOpen(false)} /> : null}

          <a href="#" style={{ padding: "10px 14px", borderRadius: 10, fontSize: 15, fontWeight: 500, color: "var(--ink)" }}>Pricing</a>
          <a href="#" style={{ padding: "10px 14px", borderRadius: 10, fontSize: 15, fontWeight: 500, color: "var(--ink)" }}>Customers</a>
        </nav>

        <div style={{ flex: 1 }}></div>

        <a href="#" style={{ padding: "10px 14px", borderRadius: 10, fontSize: 15, fontWeight: 500, color: "var(--ink)" }}>Login</a>
        <Btn variant="primary" size="md" href="https://calendly.com/charles-cardealer/30min" target="_blank">Book a demo</Btn>
      </div>
    </header>
  );
}

function MegaMenu({ onClose }) {
  const columns = [
    {
      label: "Sales",
      items: [
        { label: "Outbound Sales",  agent: "summer", desc: "Reactivate dormant + trade-cycle.", href: "summer-ai-sales-agent.html" },
        { label: "Inbound Sales",   agent: "summer", desc: "Instant CRM lead response." },
      ],
    },
    {
      label: "Service",
      items: [
        { label: "Outbound Service", agent: "april",  desc: "Re-engage lapsed customers." },
        { label: "Inbound Service",  agent: "taylor", desc: "Book service 24/7." },
        { label: "Recalls",          agent: "riley",  desc: "Turn recalls into appointments." },
      ],
    },
    {
      label: "Reception",
      items: [
        { label: "Receptionist",    agent: "avery", desc: "Never miss an inbound call." },
        { label: "Bilingual Agent", agent: "lily",  desc: "Spanish + multi-language." },
      ],
    },
    {
      label: "CRM",
      items: [
        { label: "Speed2Lead", agent: "summer", desc: "First touch within seconds." },
      ],
    },
  ];

  return (
    <div onClick={(e) => e.stopPropagation()} style={{
      position: "absolute", top: 70, left: 0, right: 0,
      background: "var(--bg)", borderTop: "1px solid var(--line)",
      borderBottom: "1px solid var(--line)",
      boxShadow: "var(--e-3)",
      padding: "32px 0",
      animation: "hRise 200ms ease both",
    }}>
      <div className="page" style={{
        display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 24,
      }}>
        {columns.map(col => (
          <div key={col.label}>
            <div style={{
              fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.18em",
              textTransform: "uppercase", color: "var(--body)", fontWeight: 500, marginBottom: 14,
            }}>{col.label}</div>
            <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
              {col.items.map(item => {
                const agent = AGENT_BY[item.agent];
                return (
                  <a key={item.label} href={item.href || "#"} onClick={onClose} style={{
                    display: "flex", alignItems: "center", gap: 14,
                    padding: "10px 12px", borderRadius: 12, color: "var(--ink)",
                    cursor: item.href ? "pointer" : "default",
                    opacity: item.href ? 1 : 0.55,
                  }}>
                    <AgentAvatar src={agent.src} size={44} ring status="live" />
                    <div style={{ minWidth: 0 }}>
                      <div style={{ fontSize: 14, fontWeight: 600 }}>{item.label}</div>
                      <div style={{ fontSize: 12, color: "var(--body)", marginTop: 1, lineHeight: 1.35 }}>{item.desc}</div>
                    </div>
                  </a>
                );
              })}
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}

/* ========== Phone-capture CTA — replaces "See it in action" ========== */
function PhoneCaptureCTA({ dark = false }) {
  const [stage, setStage] = useState("idle"); // idle | input | submitted
  const [phone, setPhone] = useState("");
  const [counter, setCounter] = useState(10);
  const [error, setError] = useState("");
  const inputRef = useRef(null);

  useEffect(() => {
    if (stage === "input" && inputRef.current) inputRef.current.focus();
  }, [stage]);

  useEffect(() => {
    if (stage !== "submitted") return;
    if (counter <= 0) return;
    const t = setTimeout(() => setCounter(c => c - 1), 1000);
    return () => clearTimeout(t);
  }, [stage, counter]);

  function formatPhone(input) {
    const digits = input.replace(/\D/g, "").slice(0, 10);
    if (digits.length === 0) return "";
    if (digits.length < 4) return `(${digits}`;
    if (digits.length < 7) return `(${digits.slice(0,3)}) ${digits.slice(3)}`;
    return `(${digits.slice(0,3)}) ${digits.slice(3,6)}-${digits.slice(6)}`;
  }
  const isValid = phone.replace(/\D/g, "").length === 10;

  function submit(e) {
    if (e) e.preventDefault();
    if (!isValid) return;
    const digits = phone.replace(/\D/g, "");
    setError("");
    setStage("submitted");
    setCounter(10);
    fetch("/api/test-call", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ phone: digits }),
    })
      .then(res => res.json().catch(() => ({})).then(data => ({ ok: res.ok, data })))
      .then(({ ok, data }) => {
        if (!ok || !data.ok) {
          setError((data && data.error) || "Something went wrong starting the call. Please try again.");
        }
      })
      .catch(() => {
        setError("Couldn't reach the server. Please try again.");
      });
  }
  function reset() {
    setStage("idle"); setPhone(""); setCounter(10); setError("");
  }

  if (stage === "idle") {
    return (
      <button onClick={() => setStage("input")} style={{
        display: "inline-flex", alignItems: "center", gap: 10,
        padding: "12px 20px 12px 14px", height: 48,
        background: dark ? "rgba(255,255,255,0.08)" : "var(--bg)",
        color: dark ? "#fff" : "var(--ink)",
        border: dark ? "1px solid rgba(255,255,255,0.18)" : "1px solid var(--line)",
        borderRadius: 12,
        fontFamily: "var(--font-body)", fontSize: 15, fontWeight: 600,
        whiteSpace: "nowrap",
        cursor: "pointer", boxShadow: dark ? "none" : "var(--e-1)",
      }}>
        <span style={{
          width: 32, height: 32, borderRadius: 999,
          background: dark ? "rgba(31,93,255,0.22)" : "var(--cobalt-50)",
          color: dark ? "#fff" : "var(--cobalt-600)",
          display: "inline-flex", alignItems: "center", justifyContent: "center",
          position: "relative",
        }}>
          <Icon name="phone" size={16} color="currentColor" />
          <span aria-hidden style={{
            position: "absolute", inset: -2, borderRadius: 999,
            border: `2px solid ${dark ? "rgba(124,165,255,0.6)" : "var(--cobalt-400)"}`,
            opacity: 0.5,
            animation: "hPulse 2s ease-out infinite",
          }}></span>
        </span>
        Test an AI agent
      </button>
    );
  }

  if (stage === "input") {
    return (
      <form onSubmit={submit} style={{
        display: "inline-flex", alignItems: "center", gap: 0,
        height: 48, padding: 4, background: "var(--bg)",
        border: "1px solid var(--cobalt-300)", borderRadius: 12,
        boxShadow: "0 0 0 4px rgba(31,93,255,0.10), var(--e-1)",
        animation: "hExpand 220ms ease-out both",
      }}>
        <span style={{
          width: 32, height: 32, borderRadius: 999, background: "var(--cobalt-50)",
          color: "var(--cobalt-600)", display: "inline-flex", alignItems: "center", justifyContent: "center",
          margin: "0 8px 0 6px", flexShrink: 0,
        }}>
          <Icon name="phone" size={16} color="currentColor" />
        </span>
        <input
          ref={inputRef}
          value={phone}
          onChange={e => setPhone(formatPhone(e.target.value))}
          onKeyDown={e => { if (e.key === "Escape") reset(); }}
          placeholder="(555) 123 4567"
          inputMode="tel"
          autoComplete="tel"
          style={{
            border: "none", outline: "none", background: "transparent",
            fontFamily: "var(--font-body)", fontSize: 15, fontWeight: 500, color: "var(--ink)",
            width: 170, padding: "0 8px",
          }}
        />
        <button type="submit" disabled={!isValid} style={{
          height: 40, padding: "0 16px", borderRadius: 8,
          background: isValid ? "var(--cobalt-500)" : "var(--cobalt-200)",
          color: "#fff", border: "none",
          fontFamily: "var(--font-body)", fontSize: 14, fontWeight: 600,
          cursor: isValid ? "pointer" : "not-allowed",
          transition: "background 150ms ease",
        }}>Call me</button>
        <button type="button" onClick={reset} aria-label="Cancel" style={{
          width: 32, height: 32, borderRadius: 999, background: "transparent",
          border: "none", color: "var(--body)", cursor: "pointer",
          display: "inline-flex", alignItems: "center", justifyContent: "center",
          marginLeft: 4,
        }}>
          <Icon name="close" size={16} color="currentColor" />
        </button>
      </form>
    );
  }

  // submitted
  return (
    <div style={{
      display: "inline-flex", alignItems: "center", gap: 12,
      padding: "8px 16px 8px 8px", height: 48,
      background: "var(--cobalt-50)", border: "1px solid var(--cobalt-200)",
      borderRadius: 12, animation: "hExpand 220ms ease-out both",
    }}>
      <AgentAvatar src={AGENT_BY.summer.src} size={32} status="live" animate ring={false} />
      <div style={{ fontSize: 14, color: "var(--ink)", lineHeight: 1.3 }}>
        {error ? (
          <div style={{ fontWeight: 600, color: "var(--negative, #c0392b)" }}>{error}</div>
        ) : (
        <div style={{ fontWeight: 600 }}>Jordan will call you at <span style={{ fontFamily: "var(--font-mono)" }}>{phone}</span></div>
        )}
        {error ? null : (
        <div style={{ fontSize: 12, color: "var(--body)" }}>
          {counter > 0 ? `Connecting in ${counter}s` : "Connecting now"}
          {counter > 0 ? <span style={{
            display: "inline-block", verticalAlign: "middle", marginLeft: 8,
            width: 60, height: 4, borderRadius: 999, background: "var(--cobalt-100)",
            overflow: "hidden", position: "relative",
          }}>
            <span style={{
              display: "block", height: "100%", width: `${((10 - counter) / 10) * 100}%`,
              background: "var(--cobalt-500)", transition: "width 1s linear",
            }}></span>
          </span> : null}
        </div>
        )}
      </div>
      <button onClick={reset} aria-label="Reset" style={{
        width: 32, height: 32, borderRadius: 999,
        background: "transparent", border: "none",
        color: "var(--cobalt-700)", cursor: "pointer",
        display: "inline-flex", alignItems: "center", justifyContent: "center",
      }}>
        <Icon name="close" size={16} color="currentColor" />
      </button>
    </div>
  );
}

/* ========== Hero ========== */
function Hero() {
  return (
    <section style={{
      position: "relative",
      padding: "96px 0 80px",
      background: "linear-gradient(180deg, #FFFFFF 0%, #F8F9FB 100%)",
      overflow: "hidden",
    }}>
      {/* Subtle radial accent */}
      <div aria-hidden style={{
        position: "absolute", inset: 0, pointerEvents: "none",
        background: "radial-gradient(circle at 80% 30%, rgba(31,93,255,0.08), transparent 50%)",
      }}></div>

      <div className="page" style={{
        position: "relative", display: "grid",
        gridTemplateColumns: "1.05fr 1fr",
        gap: 64, alignItems: "center", minHeight: 540,
      }}>
        <div>
          <div style={{
            display: "inline-flex", alignItems: "center", gap: 10,
            padding: "6px 12px 6px 10px", borderRadius: 999,
            background: "var(--cobalt-50)", color: "var(--cobalt-700)",
            fontSize: 13, fontWeight: 600, marginBottom: 28,
          }}>
            <span style={{
              width: 8, height: 8, borderRadius: 999, background: "var(--positive)",
              animation: "hDotPulse 2s ease-in-out infinite",
            }}></span>
            6 AI employees ready for work
          </div>
          <h1 style={{
            fontSize: "clamp(40px, 4.6vw, 76px)",
            lineHeight: 1.04, letterSpacing: "-0.035em",
            fontWeight: 700, margin: "0 0 24px", color: "var(--ink)",
            textWrap: "balance", whiteSpace: "nowrap",
          }}>AI&nbsp;Employees<br/>For&nbsp;Car&nbsp;Dealerships</h1>
          <p style={{
            fontSize: 22, lineHeight: 1.45, color: "var(--body)",
            maxWidth: 560, margin: "0 0 36px", fontWeight: 400,
          }}>
            Get more leads and book more appointments. AI agents that handle the calls,
            messages, and follow-ups your team doesn't have time for.
          </p>
          <div style={{ display: "flex", gap: 12, flexWrap: "wrap", alignItems: "center" }}>
            <Btn variant="primary" size="lg" href="https://calendly.com/charles-cardealer/30min" target="_blank">Book a demo</Btn>
            <PhoneCaptureCTA />
          </div>
        </div>

        <HeroIllustration />
      </div>
    </section>
  );
}

function HeroIllustration() {
  return (
    <div className="sc-hide-mobile" style={{ position: "relative", minHeight: 580 }}>
      {/* Orb backdrop */}
      <div style={{
        position: "absolute", inset: "8% 4%",
        background: "radial-gradient(circle, rgba(31,93,255,0.10), transparent 60%)",
        animation: "hBreathe 5s ease-in-out infinite",
      }}></div>

      {/* Call 1 — Summer outbound buyback */}
      <div style={{
        position: "absolute", top: 0, left: 0, width: 340,
        animation: "hFloat 5s ease-in-out infinite", zIndex: 3,
      }}>
        <VoiceCallCard
          agent={AGENT_BY.summer}
          direction="Outbound"
          number="(404) 555 0148"
          duration="01:12"
          intensity={1}
          transcript="We're seeing strong demand for your '21 CR-V. Want me to send over a quick buyback offer?"
        />
      </div>

      {/* Call 2 — Taylor inbound service */}
      <div style={{
        position: "absolute", top: 210, right: 0, width: 340,
        animation: "hFloat 5s ease-in-out infinite 1.2s", zIndex: 4,
      }}>
        <VoiceCallCard
          agent={AGENT_BY.taylor}
          direction="Inbound"
          number="(770) 555 2199"
          duration="00:47"
          intensity={0.8}
          transcript="Sure, I can book that. Thursday at 10 a.m. works on Mike's calendar."
        />
      </div>

      {/* Call 3 — Riley recall */}
      <div style={{
        position: "absolute", bottom: 4, left: 38, width: 320,
        animation: "hFloat 5s ease-in-out infinite 2.4s", zIndex: 2,
      }}>
        <VoiceCallCard
          agent={AGENT_BY.riley}
          direction="Outbound"
          number="(919) 555 0033"
          duration="00:23"
          intensity={0.6}
          transcript="Your '22 Pilot has an open recall. It's a free 90 minute fix. Want me to book it?"
        />
      </div>
    </div>
  );
}

/* Voice-call card — emphasises VOICE, not text */
function VoiceCallCard({ agent, direction, number, duration, transcript, intensity = 1 }) {
  return (
    <div style={{
      background: "var(--bg)",
      border: "1px solid var(--line)",
      borderRadius: 20,
      padding: 16,
      boxShadow: "var(--e-3)",
      position: "relative",
    }}>
      {/* Top row — live indicator + direction + duration */}
      <div style={{
        display: "flex", alignItems: "center", gap: 8,
        padding: "0 4px 12px", borderBottom: "1px solid var(--line-soft)",
        marginBottom: 12,
      }}>
        <span style={{
          display: "inline-flex", alignItems: "center", gap: 6,
          background: "#FEF2F2", color: "#B91C1C",
          padding: "3px 8px", borderRadius: 999,
          fontSize: 10, fontWeight: 700, letterSpacing: "0.08em", textTransform: "uppercase",
        }}>
          <span style={{
            width: 6, height: 6, borderRadius: 999, background: "#EF4444",
            animation: "hDotPulse 1.4s ease-in-out infinite",
            boxShadow: "0 0 0 0 rgba(239,68,68,0.5)",
          }}></span>
          Live
        </span>
        <span style={{
          fontSize: 11, fontWeight: 600, color: "var(--body)",
          fontFamily: "var(--font-mono)", letterSpacing: "0.04em",
        }}>{direction}</span>
        <span style={{ flex: 1 }}></span>
        <span style={{
          fontSize: 12, fontWeight: 600, color: "var(--ink)",
          fontFamily: "var(--font-mono)",
          padding: "3px 8px", borderRadius: 6,
          background: "var(--bg-alt)",
        }}>{duration}</span>
      </div>

      {/* Caller info row */}
      <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 14 }}>
        <AgentAvatar src={agent.src} size={44} status="live" animate ring />
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 14, fontWeight: 700, color: "var(--ink)", lineHeight: 1.1 }}>{agent.name}</div>
          <div style={{ fontSize: 12, color: "var(--body)", marginTop: 2 }}>{agent.role}</div>
        </div>
        <div style={{ textAlign: "right", flexShrink: 0 }}>
          <div style={{
            fontSize: 11, fontFamily: "var(--font-mono)", color: "var(--body)",
            letterSpacing: "0.04em", textTransform: "uppercase", fontWeight: 500,
          }}>{direction === "Inbound" ? "Caller" : "To"}</div>
          <div style={{ fontSize: 12, fontFamily: "var(--font-mono)", color: "var(--ink)", marginTop: 2, fontWeight: 500 }}>{number}</div>
        </div>
      </div>

      {/* Big animated waveform */}
      <div style={{
        height: 56, borderRadius: 12, background: "var(--cobalt-50)",
        display: "flex", alignItems: "center", justifyContent: "center",
        padding: "0 14px", marginBottom: 12, overflow: "hidden",
      }}>
        <FullWaveform color="var(--cobalt-500)" intensity={intensity} count={42} />
      </div>

      {/* Live transcript caption */}
      <div>
        <div style={{
          fontFamily: "var(--font-mono)", fontSize: 9, fontWeight: 600,
          letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--body)",
          marginBottom: 6, display: "flex", alignItems: "center", gap: 6,
        }}>
          <span style={{ width: 4, height: 4, borderRadius: 999, background: "var(--positive)" }}></span>
          Live transcript · {agent.name}
        </div>
        <div style={{
          fontSize: 14, color: "var(--ink)", lineHeight: 1.45,
          fontWeight: 500, fontStyle: "italic",
        }}>
          "{transcript}"
        </div>
      </div>
    </div>
  );
}

/* Full-width waveform — more bars + varied heights for a real voice look */
function FullWaveform({ color = "currentColor", count = 40, intensity = 1 }) {
  // Pseudorandom but stable heights
  const heights = [];
  for (let i = 0; i < count; i++) {
    const t = (i / count) * Math.PI * 4;
    const h = (0.4 + 0.6 * Math.abs(Math.sin(t) * Math.cos(t * 1.7))) * intensity;
    heights.push(Math.max(0.15, h));
  }
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 3, width: "100%", height: "100%" }}>
      {heights.map((h, i) => (
        <span key={i} style={{
          flex: 1, minWidth: 2, height: `${h * 100}%`, borderRadius: 2,
          background: color, transformOrigin: "center",
          animation: `hWave ${0.8 + (i % 5) * 0.12}s ease-in-out infinite ${(i * 0.04)}s`,
        }}></span>
      ))}
    </div>
  );
}

/* ========== Agent spotlight (Meet the Agents) ========== */
function AgentSpotlight({ agent, idx }) {
  const [hover, setHover] = useState(false);
  const [playing, setPlaying] = useState(false);
  const [progress, setProgress] = useState(0); // 0..1
  const audioRef = useRef(null);
  const hasAudio = !!agent.audio;

  // Listen to audio events
  useEffect(() => {
    const a = audioRef.current;
    if (!a) return;
    const onPlay = () => setPlaying(true);
    const onPause = () => setPlaying(false);
    const onEnded = () => { setPlaying(false); setProgress(0); };
    const onTime = () => {
      if (a.duration) setProgress(a.currentTime / a.duration);
    };
    a.addEventListener("play", onPlay);
    a.addEventListener("pause", onPause);
    a.addEventListener("ended", onEnded);
    a.addEventListener("timeupdate", onTime);
    return () => {
      a.removeEventListener("play", onPlay);
      a.removeEventListener("pause", onPause);
      a.removeEventListener("ended", onEnded);
      a.removeEventListener("timeupdate", onTime);
    };
  }, [agent.audio]);

  function toggle() {
    const a = audioRef.current;
    if (!a) return;
    if (a.paused) {
      // Pause every other agent audio playing on the page
      document.querySelectorAll('audio[data-agent-audio]').forEach(other => {
        if (other !== a && !other.paused) other.pause();
      });
      a.play().catch(() => {});
    } else {
      a.pause();
    }
  }

  return (
    <div
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      data-agent={agent.id}
      style={{
        background: "var(--bg)",
        border: playing ? "1px solid var(--cobalt-300)" : "1px solid var(--line)",
        borderRadius: 24,
        overflow: "hidden",
        boxShadow: playing ? "0 0 0 4px rgba(31,93,255,0.10), var(--e-3)" : "var(--e-2)",
        display: "flex", flexDirection: "column",
        transition: "transform 240ms ease, box-shadow 240ms ease, border-color 240ms ease",
        transform: hover ? "translateY(-4px)" : "translateY(0)",
      }}
    >
      {/* Looping video poster */}
      <div style={{
        position: "relative", aspectRatio: "1 / 1", overflow: "hidden",
        background: "var(--bg-alt)",
      }}>
        {agent.video ? (
          <video
            src={agent.video}
            poster={agent.src}
            autoPlay loop muted playsInline
            style={{
              width: "100%", height: "100%", objectFit: "cover", display: "block",
            }}
          />
        ) : (
          <img src={agent.src} alt={agent.name} style={{
            width: "100%", height: "100%", objectFit: "cover", display: "block",
            animation: "hBreathe 6s ease-in-out infinite",
            transformOrigin: "center 30%",
          }} />
        )}
        {/* Name / role overlay */}
        <div style={{
          position: "absolute", left: 20, right: 20, bottom: 20,
          background: "rgba(11,27,43,0.78)",
          backdropFilter: "blur(8px)",
          WebkitBackdropFilter: "blur(8px)",
          color: "#fff",
          borderRadius: 14, padding: "12px 14px",
        }}>
          <div style={{ fontSize: 22, fontWeight: 700, lineHeight: 1.1 }}>{agent.name}</div>
          <div style={{ fontSize: 13, color: "rgba(255,255,255,0.76)", marginTop: 2 }}>{agent.role}</div>
        </div>
        {/* Now-playing badge */}
        {playing ? (
          <div style={{
            position: "absolute", top: 16, right: 16,
            display: "inline-flex", alignItems: "center", gap: 6,
            background: "rgba(255,255,255,0.96)", color: "var(--cobalt-700)",
            padding: "5px 10px 5px 8px", borderRadius: 999,
            fontSize: 11, fontWeight: 700, letterSpacing: "0.06em", textTransform: "uppercase",
            boxShadow: "var(--e-2)",
          }}>
            <span style={{
              width: 6, height: 6, borderRadius: 999, background: "var(--cobalt-500)",
              animation: "hDotPulse 1.4s ease-in-out infinite",
            }}></span>
            Playing
          </div>
        ) : null}
      </div>

      {/* Body */}
      <div style={{ padding: "24px 24px 26px", display: "flex", flexDirection: "column", gap: 14 }}>
        <div style={{ fontSize: 18, fontWeight: 600, color: "var(--ink)", letterSpacing: "-0.005em" }}>
          {agent.descriptor}
        </div>
        <div style={{ fontSize: 14, lineHeight: 1.55, color: "var(--body)" }}>
          {agent.description}
        </div>

        <div style={{
          display: "flex", alignItems: "center", gap: 12,
          marginTop: 4, paddingTop: 16, borderTop: "1px solid var(--line-soft)",
        }}>
          {hasAudio ? (
            <React.Fragment>
              <audio ref={audioRef} src={agent.audio} preload="metadata" data-agent-audio={agent.id}></audio>
              <button
                onClick={toggle}
                aria-label={playing ? "Pause demo" : "Play demo"}
                style={{
                  width: 44, height: 44, borderRadius: 999,
                  background: playing ? "var(--cobalt-500)" : "var(--cobalt-50)",
                  color: playing ? "#fff" : "var(--cobalt-600)",
                  border: "none", display: "inline-flex", alignItems: "center", justifyContent: "center",
                  cursor: "pointer", position: "relative",
                  flexShrink: 0,
                  transition: "background 160ms ease, color 160ms ease",
                }}>
                {playing ? (
                  <svg width="14" height="14" viewBox="0 0 14 14" fill="currentColor"><rect x="2.5" y="2" width="3" height="10" rx="1"/><rect x="8.5" y="2" width="3" height="10" rx="1"/></svg>
                ) : (
                  <svg width="14" height="14" viewBox="0 0 14 14" fill="currentColor"><path d="M3 1.5v11l9-5.5z"/></svg>
                )}
                {!playing ? (
                  <span aria-hidden style={{
                    position: "absolute", inset: -4, borderRadius: 999,
                    border: "2px solid var(--cobalt-300)", opacity: 0.6,
                    animation: "hPulse 2.2s ease-out infinite",
                  }}></span>
                ) : null}
              </button>
              <div style={{ flex: 1, display: "flex", flexDirection: "column", gap: 4, minWidth: 0 }}>
                {/* Waveform — only animates when playing */}
                <div style={{ height: 18, display: "inline-flex", alignItems: "center" }}>
                  <PlaybackWave on={playing} count={28} color={playing ? "var(--cobalt-500)" : "var(--cobalt-200)"} />
                </div>
                {/* Progress bar */}
                <div style={{
                  height: 3, borderRadius: 999, background: "var(--line)", overflow: "hidden",
                }}>
                  <div style={{
                    height: "100%", width: `${progress * 100}%`, background: "var(--cobalt-500)",
                    transition: "width 200ms linear",
                  }}></div>
                </div>
              </div>
              <Btn variant="primary" size="sm" href="https://calendly.com/charles-cardealer/30min" target="_blank">Hire me</Btn>
            </React.Fragment>
          ) : (
            <React.Fragment>
              <span style={{
                display: "inline-flex", alignItems: "center", gap: 8,
                padding: "8px 12px 8px 10px",
                background: "var(--cobalt-50)", color: "var(--cobalt-700)",
                borderRadius: 999, fontSize: 12, fontWeight: 600,
                letterSpacing: "0.01em",
              }}>
                <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l1.8 6.2L20 10l-6.2 1.8L12 18l-1.8-6.2L4 10l6.2-1.8L12 2z"/></svg>
                Demo coming soon
              </span>
              <div style={{ flex: 1 }}></div>
              <Btn variant="primary" size="sm" href="https://calendly.com/charles-cardealer/30min" target="_blank">Hire me</Btn>
            </React.Fragment>
          )}
        </div>
      </div>
    </div>
  );
}

/* Playback-driven waveform — bars are static when paused, animated when on */
function PlaybackWave({ on, count = 24, color = "currentColor" }) {
  const heights = [];
  for (let i = 0; i < count; i++) {
    const t = (i / count) * Math.PI * 4;
    const h = 0.4 + 0.6 * Math.abs(Math.sin(t) * Math.cos(t * 1.7));
    heights.push(Math.max(0.2, h));
  }
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 2, width: "100%", height: "100%" }}>
      {heights.map((h, i) => (
        <span key={i} style={{
          flex: 1, minWidth: 2, height: `${h * 100}%`, borderRadius: 2,
          background: color, transformOrigin: "center",
          animation: on ? `hWave ${0.7 + (i % 5) * 0.12}s ease-in-out infinite ${(i * 0.04)}s` : "none",
          transition: "height 240ms ease, background 240ms ease",
        }}></span>
      ))}
    </div>
  );
}

/* ========== Feature hub (radial composition) ========== */
function FeatureHub() {
  // Layout: 6 agent avatars cluster at center of a 720x720 stage,
  // 7 feature labels orbit at the rim, each connected by a curved electric line.

  const W = 760, H = 640, cx = W / 2, cy = H / 2;

  // Agent cluster positions (small inner ring)
  const innerR = 78;
  const agentLayout = AGENTS.map((a, i) => {
    const angle = (i / AGENTS.length) * Math.PI * 2 - Math.PI / 2;
    return {
      ...a,
      x: cx + Math.cos(angle) * innerR,
      y: cy + Math.sin(angle) * innerR,
    };
  });

  // Feature label positions (outer arc)
  const features = [
    { id: "unsold",   label: "Reactivate Unsold Leads",        agent: "summer", angle: -110 },
    { id: "past",     label: "Reactivate Past Customers",      agent: "summer", angle: -55 },
    { id: "service",  label: "Reactivate Service Customers",   agent: "april",  angle: 0 },
    { id: "voicemail",label: "Don't Let Calls Go To Voicemail",agent: "taylor", angle: 55 },
    { id: "crm",      label: "Answer Every CRM Lead",          agent: "summer", angle: 110 },
    { id: "recall",   label: "Turn Recalls Into Bookings",     agent: "riley",  angle: 165 },
    { id: "missed",   label: "Never Miss A Call Again",        agent: "avery",  angle: -160 },
  ];
  const outerR = 280;
  const featureLayout = features.map((f, i) => {
    const angle = (f.angle * Math.PI) / 180;
    return {
      ...f,
      x: cx + Math.cos(angle) * outerR,
      y: cy + Math.sin(angle) * outerR,
    };
  });

  // Connecting lines
  const lines = featureLayout.map(f => {
    const a = agentLayout.find(x => x.id === f.agent);
    return { from: { x: a.x, y: a.y }, to: { x: f.x, y: f.y }, key: f.id };
  });

  return (
    <div style={{
      position: "relative", width: W, height: H, margin: "0 auto",
      maxWidth: "100%",
    }}>
      <svg width={W} height={H} viewBox={`0 0 ${W} ${H}`} style={{
        position: "absolute", inset: 0, pointerEvents: "none",
      }}>
        <defs>
          <linearGradient id="fHubGrad" x1="0" x2="1">
            <stop offset="0" stopColor="#7CA5FF"/>
            <stop offset="1" stopColor="#1F5DFF"/>
          </linearGradient>
          <radialGradient id="fHubHalo" cx="50%" cy="50%" r="50%">
            <stop offset="0" stopColor="#1F5DFF" stopOpacity="0.10"/>
            <stop offset="1" stopColor="#1F5DFF" stopOpacity="0"/>
          </radialGradient>
        </defs>
        {/* Halo */}
        <circle cx={cx} cy={cy} r="180" fill="url(#fHubHalo)"/>
        {/* Subtle orbit ring */}
        <circle cx={cx} cy={cy} r={outerR - 10} fill="none" stroke="var(--cobalt-100)" strokeWidth="1.5" strokeDasharray="2 6"/>
        {/* Electric routing lines */}
        {lines.map((l, i) => {
          const dx = l.to.x - l.from.x, dy = l.to.y - l.from.y;
          // curve via perpendicular offset for nicer arcs
          const mx = (l.from.x + l.to.x) / 2;
          const my = (l.from.y + l.to.y) / 2;
          const len = Math.hypot(dx, dy);
          const nx = -dy / len, ny = dx / len;
          const offset = 30;
          const path = `M ${l.from.x} ${l.from.y} Q ${mx + nx * offset} ${my + ny * offset} ${l.to.x} ${l.to.y}`;
          return (
            <g key={l.key}>
              <path d={path} fill="none" stroke="var(--cobalt-200)" strokeWidth="1.5"/>
              <path d={path} fill="none" stroke="url(#fHubGrad)" strokeWidth="2"
                strokeDasharray="3 8" strokeLinecap="round"
                style={{ animation: `hElectric ${1.6 + i * 0.15}s linear infinite ${i * 0.1}s` }}/>
            </g>
          );
        })}
      </svg>

      {/* Agent cluster (HTML positioned over SVG) */}
      {agentLayout.map((a, i) => (
        <div key={a.id} style={{
          position: "absolute", left: a.x, top: a.y,
          transform: "translate(-50%, -50%)",
          animation: `hFloatSm ${3 + (i % 3)}s ease-in-out infinite ${i * 0.2}s`,
        }}>
          <AgentAvatar src={a.src} size={64} ring status="live" />
        </div>
      ))}

      {/* Feature chips */}
      {featureLayout.map((f, i) => {
        // Anchor chip on outside of orbit, centered on radial line
        const anchorLeft = f.x < cx;
        return (
          <div key={f.id} style={{
            position: "absolute", left: f.x, top: f.y,
            transform: "translate(-50%, -50%)",
            animation: `hFloatSm ${2.6 + (i % 4) * 0.4}s ease-in-out infinite ${i * 0.25}s`,
          }}>
            <div style={{
              background: "var(--bg)", border: "1px solid var(--line)",
              boxShadow: "var(--e-2)", borderRadius: 999,
              padding: "8px 14px 8px 8px",
              display: "inline-flex", alignItems: "center", gap: 10,
              whiteSpace: "nowrap", maxWidth: 260,
            }}>
              <AgentAvatar src={AGENT_BY[f.agent].src} size={28} ring={false} />
              <span style={{ fontSize: 13, fontWeight: 600, color: "var(--ink)" }}>{f.label}</span>
            </div>
          </div>
        );
      })}
    </div>
  );
}

/* ========== Feature card ========== */
function FeatureCard({ kicker, title, agent, bullets, tools, accent }) {
  return (
    <div style={{
      background: "var(--bg)", border: "1px solid var(--line)", borderRadius: 20,
      padding: 24, display: "flex", flexDirection: "column", gap: 18,
      boxShadow: "var(--e-1)", height: "100%",
    }}>
      {/* Header — agent + kicker */}
      <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
        <AgentAvatar src={agent.src} size={48} ring status="live" animate />
        <div style={{ minWidth: 0 }}>
          <div style={{
            fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.16em",
            textTransform: "uppercase", color: "var(--cobalt-600)", fontWeight: 500,
          }}>{kicker}</div>
          <div style={{ fontSize: 13, color: "var(--body)", marginTop: 2 }}>
            {agent.name} · {agent.role}
          </div>
        </div>
      </div>

      {/* Title */}
      <div style={{ fontSize: 22, fontWeight: 700, color: "var(--ink)", letterSpacing: "-0.01em", lineHeight: 1.2 }}>
        {title}
      </div>

      {/* Bullets */}
      <ul style={{ margin: 0, padding: 0, listStyle: "none", display: "flex", flexDirection: "column", gap: 10 }}>
        {bullets.map((b, i) => (
          <li key={i} style={{
            display: "flex", gap: 10, alignItems: "flex-start",
            fontSize: 14, color: "var(--ink)",
          }}>
            <span style={{
              width: 18, height: 18, borderRadius: 999, background: "var(--cobalt-50)",
              color: "var(--cobalt-600)", display: "inline-flex", alignItems: "center",
              justifyContent: "center", flexShrink: 0, marginTop: 1,
            }}>
              <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12l5 5L20 7"/></svg>
            </span>
            <span>{b}</span>
          </li>
        ))}
      </ul>

      {/* Tools footer */}
      <div style={{ marginTop: "auto", paddingTop: 16, borderTop: "1px solid var(--line-soft)", display: "flex", justifyContent: "space-between", alignItems: "center" }}>
        <div style={{ display: "flex", gap: 6 }}>
          {tools.map(t => (
            <Badge key={t} tone="info" size="sm">
              <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
                {t === "Voice" ? <svg width="11" height="11" viewBox="0 0 24 24" fill="currentColor"><path d="M5 3h3l2 5-2.5 1.5a12 12 0 0 0 7 7L16 14l5 2v3a2 2 0 0 1-2 2A16 16 0 0 1 3 5a2 2 0 0 1 2-2z"/></svg> : null}
                {t === "SMS"   ? <svg width="11" height="11" viewBox="0 0 24 24" fill="currentColor"><path d="M4 4h16a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H9l-5 4V6a2 2 0 0 1 2-2z"/></svg> : null}
                {t}
              </span>
            </Badge>
          ))}
        </div>
        <a href={DEMO_URL} target="_blank" rel="noopener noreferrer" style={{
          fontSize: 13, fontWeight: 600, color: "var(--cobalt-600)",
          display: "inline-flex", alignItems: "center", gap: 4,
        }}>
          Learn more <span aria-hidden>→</span>
        </a>
      </div>
    </div>
  );
}

/* ========== Marquee (floating feature words) ========== */
function Marquee({ items, direction = "left", speed = 50 }) {
  const doubled = [...items, ...items];
  return (
    <div style={{
      overflow: "hidden", width: "100%", position: "relative",
      maskImage: "linear-gradient(90deg, transparent 0, #000 8%, #000 92%, transparent 100%)",
      WebkitMaskImage: "linear-gradient(90deg, transparent 0, #000 8%, #000 92%, transparent 100%)",
    }}>
      <div style={{
        display: "inline-flex", gap: 16, padding: "8px 0",
        animation: `hMarquee ${speed}s linear infinite`,
        animationDirection: direction === "right" ? "reverse" : "normal",
        width: "max-content",
      }}>
        {doubled.map((item, i) => (
          <span key={i} style={{
            display: "inline-flex", alignItems: "center", gap: 10,
            background: "var(--bg)", border: "1px solid var(--line)",
            borderRadius: 999, padding: "10px 18px",
            fontSize: 14, fontWeight: 500, color: "var(--ink)",
            whiteSpace: "nowrap", boxShadow: "var(--e-1)",
          }}>
            <span style={{
              width: 6, height: 6, borderRadius: 999, background: "var(--cobalt-500)",
            }}></span>
            {item}
          </span>
        ))}
      </div>
    </div>
  );
}

/* ========== Integration tile ========== */
function IntegrationTile({ name, desc, icon }) {
  return (
    <div style={{
      background: "var(--bg)", border: "1px solid var(--line)", borderRadius: 16,
      padding: "24px 24px 22px", display: "flex", flexDirection: "column", gap: 14,
      transition: "transform 200ms ease, box-shadow 200ms ease",
    }}
    onMouseEnter={e => { e.currentTarget.style.transform = "translateY(-4px)"; e.currentTarget.style.boxShadow = "var(--e-3)"; }}
    onMouseLeave={e => { e.currentTarget.style.transform = ""; e.currentTarget.style.boxShadow = ""; }}
    >
      <Icon name={icon} size={24} color="var(--cobalt-600)" background="var(--cobalt-50)" padding={12} radius={12} />
      <div>
        <div style={{ fontSize: 16, fontWeight: 700, color: "var(--ink)" }}>{name}</div>
        <div style={{ fontSize: 13, color: "var(--body)", marginTop: 4 }}>{desc}</div>
      </div>
      <a href="mailto:hello@cardealer.ai?subject=Integration%20request" style={{ fontSize: 11, fontFamily: "var(--font-mono)", letterSpacing: "0.12em", color: "var(--cobalt-600)", textTransform: "uppercase", fontWeight: 600, textDecoration: "none", display: "inline-flex", alignItems: "center", gap: 4 }}>
        Don't see yours? Just ask <span aria-hidden>→</span>
      </a>
    </div>
  );
}

/* ========== Team Sells Cars hero illustration ========== */
function TeamSellsHero() {
  // Use the supplied ready-made composite (woman with headset + waveform + agent chips).
  // Add a couple of live-floating elements around it for extra polish.
  return (
    <div style={{ position: "relative", width: "100%", maxWidth: 560, margin: "0 auto" }}>
      <div style={{
        position: "relative", borderRadius: 24, overflow: "hidden",
        boxShadow: "var(--e-4)",
        animation: "hFloatSm 6s ease-in-out infinite",
      }}>
        <img src="assets/team-hero.png" alt="A dealer team member working with AI employees handling calls" style={{
          width: "100%", height: "auto", display: "block",
        }} />
      </div>

      {/* Floating bubble chip — Riley */}
      <div style={{
        position: "absolute", top: "8%", right: "-6%",
        animation: "hFloat 4.5s ease-in-out infinite 1s",
        zIndex: 2,
      }}>
        <BubbleChip agent={AGENT_BY.riley} text="Recall fix booked." />
      </div>

      {/* Floating bubble chip — Taylor */}
      <div style={{
        position: "absolute", top: "32%", left: "-8%",
        animation: "hFloat 4.5s ease-in-out infinite 2.2s",
        zIndex: 2,
      }}>
        <BubbleChip agent={AGENT_BY.taylor} text="Service Thursday at 10." />
      </div>

      {/* Live call counter pinned bottom right */}
      <div style={{
        position: "absolute", bottom: "10%", right: "-6%",
        background: "var(--bg)", border: "1px solid var(--line)",
        borderRadius: 999, padding: "10px 16px",
        boxShadow: "var(--e-3)",
        display: "inline-flex", alignItems: "center", gap: 10,
        animation: "hFloatSm 4s ease-in-out infinite",
        zIndex: 2,
      }}>
        <span style={{
          width: 8, height: 8, borderRadius: 999, background: "var(--positive)",
          boxShadow: "0 0 0 4px rgba(16,178,108,0.18)",
        }}></span>
        <span style={{ fontSize: 12, fontWeight: 700, color: "var(--ink)" }}>14 active calls</span>
        <VoiceBars on count={6} color="var(--cobalt-500)" />
      </div>
    </div>
  );
}

function BubbleChip({ agent, text }) {
  return (
    <div style={{
      display: "inline-flex", alignItems: "center", gap: 8,
      background: "var(--bg)", border: "1px solid var(--line)", borderRadius: 999,
      padding: "6px 12px 6px 6px", boxShadow: "var(--e-2)",
      maxWidth: 230,
    }}>
      <AgentAvatar src={agent.src} size={26} ring={false} />
      <span style={{ fontSize: 12, fontWeight: 500, color: "var(--ink)", whiteSpace: "nowrap" }}>{text}</span>
    </div>
  );
}

/* ========== Before / After ========== */
function BeforeAfter({ rows }) {
  return (
    <div style={{
      display: "grid", gridTemplateColumns: "1fr 1fr",
      borderRadius: 24, overflow: "hidden",
      border: "1px solid var(--line)",
      boxShadow: "var(--e-2)",
    }}>
      {/* Header row */}
      <div style={{
        background: "var(--bg-alt)", padding: "24px 32px",
        borderRight: "1px solid var(--line)",
      }}>
        <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--negative)", fontWeight: 600, marginBottom: 6 }}>Before AI</div>
        <div style={{ fontSize: 22, fontWeight: 700, color: "var(--ink)", letterSpacing: "-0.01em" }}>Leaky funnel.</div>
      </div>
      <div style={{
        background: "var(--cobalt-50)", padding: "24px 32px",
      }}>
        <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--cobalt-700)", fontWeight: 600, marginBottom: 6 }}>After AI</div>
        <div style={{ fontSize: 22, fontWeight: 700, color: "var(--ink)", letterSpacing: "-0.01em" }}>Every lead, captured.</div>
      </div>

      {/* Comparison rows */}
      {rows.map((r, i) => (
        <React.Fragment key={i}>
          <div style={{
            background: "var(--bg)", padding: "20px 32px",
            borderTop: "1px solid var(--line)", borderRight: "1px solid var(--line)",
            display: "flex", alignItems: "center", gap: 14,
          }}>
            <span style={{
              width: 24, height: 24, borderRadius: 999, background: "var(--negative-bg)",
              color: "var(--negative)", display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
            }}>
              <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round"><path d="M6 6l12 12M18 6l-12 12"/></svg>
            </span>
            <span style={{ color: "var(--body)", fontSize: 15, lineHeight: 1.45 }}>{r.before}</span>
          </div>
          <div style={{
            background: "var(--bg)", padding: "20px 32px",
            borderTop: "1px solid var(--line)",
            display: "flex", alignItems: "center", gap: 14,
          }}>
            <span style={{
              width: 24, height: 24, borderRadius: 999, background: "var(--positive-bg)",
              color: "var(--positive)", display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
            }}>
              <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12l5 5L20 7"/></svg>
            </span>
            <span style={{ color: "var(--ink)", fontSize: 15, fontWeight: 500, lineHeight: 1.45 }}>{r.after}</span>
          </div>
        </React.Fragment>
      ))}
    </div>
  );
}

/* ========== Closer / Growth illustration ========== */
function GrowthIllo() {
  // Animated bar chart with agent face at top
  const bars = [38, 54, 49, 70, 64, 88, 96];
  return (
    <div style={{ position: "relative", width: "100%", maxWidth: 560, margin: "0 auto", aspectRatio: "1 / 0.9" }}>
      {/* Agent floating at top right */}
      <div style={{ position: "absolute", top: "4%", right: "4%", animation: "hFloat 4s ease-in-out infinite" }}>
        <AgentAvatar src={AGENT_BY.summer.src} size={88} ring status="live" animate />
      </div>
      {/* Bubble from agent */}
      <div style={{ position: "absolute", top: "18%", right: "26%", animation: "hFloat 4s ease-in-out infinite 0.6s" }}>
        <BubbleChip agent={AGENT_BY.summer} text="+27 sold this month." />
      </div>

      {/* Bars */}
      <svg viewBox="0 0 560 460" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <linearGradient id="growBar" x1="0" x2="0" y1="0" y2="1">
            <stop offset="0" stopColor="#1F5DFF"/>
            <stop offset="1" stopColor="#7CA5FF"/>
          </linearGradient>
        </defs>
        {/* Baseline */}
        <line x1="60" y1="400" x2="540" y2="400" stroke="var(--line)" strokeWidth="1.5"/>
        {bars.map((h, i) => {
          const bw = 50, gap = 18;
          const x = 80 + i * (bw + gap);
          const y = 400 - (h * 3.2);
          return (
            <g key={i}>
              <rect x={x} y={y} width={bw} height={400 - y} rx="10"
                fill="url(#growBar)" opacity={0.85}
                style={{ transformOrigin: `${x + bw / 2}px 400px`, animation: `hBreathe ${2 + i * 0.15}s ease-in-out infinite ${i * 0.12}s` }}/>
              <text x={x + bw / 2} y="430" textAnchor="middle" fontSize="11" fill="#7B7C7E" fontFamily="Roboto">M{i + 1}</text>
            </g>
          );
        })}
        {/* Trend line */}
        <path d="M 105 297 L 173 240 L 241 256 L 309 176 L 377 195 L 445 119 L 513 96"
              stroke="var(--ink)" strokeWidth="2.5" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
        {/* End point dot */}
        <circle cx="513" cy="96" r="8" fill="var(--ink)"/>
        <circle cx="513" cy="96" r="14" fill="none" stroke="var(--ink)" strokeWidth="2" opacity="0.2"
          style={{ animation: "hPing 2.4s ease-out infinite" }}/>
      </svg>
    </div>
  );
}

/* ========== Footer ========== */
function HomeFooter() {
  return (
    <footer style={{ background: "var(--ink)", color: "#fff", padding: "80px 0 32px" }}>
      <div className="page">
        <div style={{
          display: "grid", gridTemplateColumns: "1.6fr 1fr 1fr 1fr", gap: 40,
          paddingBottom: 56, borderBottom: "1px solid rgba(255,255,255,0.1)",
        }}>
          <div>
            <img src="logo-exports/horizontal/horizontal-white.svg" alt="CarDealer.ai" style={{ height: 32 }} />
            <p style={{ color: "rgba(255,255,255,0.6)", fontSize: 14, lineHeight: 1.55, marginTop: 18, maxWidth: 280 }}>
              AI employees for car dealerships. Built to talk, listen, and convert at any hour.
            </p>
            <div style={{ display: "flex", gap: 10, marginTop: 24 }}>
              {[
                { name: "instagram", url: "https://www.instagram.com/cardealerai/" },
                { name: "facebook",  url: "https://www.facebook.com/cardealerai" },
                { name: "linkedin",  url: "https://www.linkedin.com/company/cardealerai/" },
                { name: "youtube",   url: "https://www.youtube.com/@cardealerai" },
              ].map(s => (
                <a key={s.name} href={s.url} target="_blank" rel="noopener noreferrer" aria-label={s.name} style={{
                  width: 38, height: 38, borderRadius: 10,
                  background: "rgba(255,255,255,0.06)",
                  display: "inline-flex", alignItems: "center", justifyContent: "center",
                  color: "rgba(255,255,255,0.7)",
                  transition: "background 160ms ease, color 160ms ease",
                }}
                onMouseEnter={e => { e.currentTarget.style.background = "rgba(255,255,255,0.12)"; e.currentTarget.style.color = "#fff"; }}
                onMouseLeave={e => { e.currentTarget.style.background = "rgba(255,255,255,0.06)"; e.currentTarget.style.color = "rgba(255,255,255,0.7)"; }}
                >
                  <SocialGlyph name={s.name} />
                </a>
              ))}
            </div>
          </div>
          <FooterCol label="Products" links={["Sales", "Service", "Receptionist", "CRM · Speed2Lead", "Bilingual"]} />
          <FooterCol label="Company"  links={["About", "Pricing", "Blog"]} />
          <FooterCol label="Legal"    links={["Privacy Policy", "Terms & Conditions"]} />
        </div>
        <div style={{
          display: "flex", justifyContent: "space-between", alignItems: "center",
          paddingTop: 28, color: "rgba(255,255,255,0.5)", fontSize: 13,
          flexWrap: "wrap", gap: 12,
        }}>
          <div>© 2026 CarDealer.ai. All rights reserved.</div>
          <div style={{ fontFamily: "var(--font-mono)", letterSpacing: "0.12em", textTransform: "uppercase", fontSize: 11 }}>
            AI for Car Dealerships
          </div>
        </div>
      </div>
    </footer>
  );
}

function FooterCol({ label, links }) {
  return (
    <div>
      <div style={{
        fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.18em",
        textTransform: "uppercase", color: "rgba(255,255,255,0.5)", fontWeight: 500, marginBottom: 16,
      }}>{label}</div>
      <ul style={{ margin: 0, padding: 0, listStyle: "none", display: "flex", flexDirection: "column", gap: 10 }}>
        {links.map(l => (
          <li key={l}><a href="#" style={{ color: "rgba(255,255,255,0.78)", fontSize: 14 }}>{l}</a></li>
        ))}
      </ul>
    </div>
  );
}

function SocialGlyph({ name }) {
  const paths = {
    instagram: "M7 2h10a5 5 0 0 1 5 5v10a5 5 0 0 1-5 5H7a5 5 0 0 1-5-5V7a5 5 0 0 1 5-5zm5 5a5 5 0 1 0 0 10 5 5 0 0 0 0-10zm6.5-.5a1 1 0 1 0 0 2 1 1 0 0 0 0-2z",
    facebook:  "M13 22v-9h3l.5-4H13V7c0-1 .5-2 2-2h2V1.5A22 22 0 0 0 14 1c-3 0-5 2-5 5v3H6v4h3v9h4z",
    linkedin:  "M4 4a2 2 0 1 1 0 4 2 2 0 0 1 0-4zM3 9h2v13H3V9zm6 0h2v2c.5-1 2-2 4-2 3 0 5 2 5 6v7h-2v-7c0-2-1-3-3-3s-3 1-3 3v7H9V9z",
    youtube:   "M2 7a3 3 0 0 1 3-3h14a3 3 0 0 1 3 3v10a3 3 0 0 1-3 3H5a3 3 0 0 1-3-3V7zm8 3v6l6-3-6-3z",
  };
  return (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d={paths[name]}/></svg>
  );
}

/* ========== Conversation side-panel — for portal demo ========== */
function ConversationPanel({ row, onClose }) {
  // Looping demo script: caller (customer) ↔ AI agent on a voice call.
  // Lines reveal sequentially, then reset.
  const script = [
    { who: "caller", text: "Hey, I'm calling about service for my CR-V." },
    { who: "agent",  text: "Of course. What kind of service do you need?" },
    { who: "caller", text: "Just an oil change and tire rotation." },
    { who: "agent",  text: "I can get you in this Thursday at 10 a.m. Does that work?" },
    { who: "caller", text: "Yeah, that works." },
    { who: "agent",  text: "Booked. I'll text you a confirmation right now." },
  ];

  const [step, setStep] = useState(0);

  // Advance through messages, looping
  useEffect(() => {
    const stepDelay = step === script.length - 1 ? 2400 : 1400;
    const t = setTimeout(() => {
      setStep(s => s >= script.length - 1 ? 0 : s + 1);
    }, stepDelay);
    return () => clearTimeout(t);
  }, [step]);

  const visible = script.slice(0, step + 1);
  const totalDuration = "2:14";
  const channelLabel = row?.channel === "email" ? "Email" :
                       row?.channel === "sms"   ? "SMS"   :
                       row?.channel === "chat"  ? "Chat"  : "Voice call";

  return (
    <div style={{
      position: "absolute", inset: 0, zIndex: 5,
      display: "flex", justifyContent: "flex-end",
      pointerEvents: "none",
    }}>
      {/* Backdrop scrim */}
      <div onClick={onClose} style={{
        position: "absolute", inset: 0, background: "rgba(11,27,43,0.32)",
        animation: "hExpand 220ms ease-out both",
        pointerEvents: "auto",
      }}></div>

      {/* Panel */}
      <div style={{
        position: "relative", width: 440, maxWidth: "92%",
        background: "var(--bg)", borderLeft: "1px solid var(--line)",
        boxShadow: "var(--e-4)",
        display: "flex", flexDirection: "column",
        animation: "hSlideIn 320ms cubic-bezier(0.2, 0, 0, 1) both",
        pointerEvents: "auto",
      }}>
        {/* Header */}
        <div style={{
          padding: "20px 24px", borderBottom: "1px solid var(--line)",
          display: "flex", alignItems: "center", gap: 14,
        }}>
          <AgentAvatar src={row.agent.src} size={44} status="live" animate ring />
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ fontSize: 15, fontWeight: 700, color: "var(--ink)" }}>{row.agent.name}</div>
            <div style={{ fontSize: 12, color: "var(--body)" }}>{row.agent.role}</div>
          </div>
          <button onClick={onClose} aria-label="Close" style={{
            width: 36, height: 36, borderRadius: 999, border: "none",
            background: "var(--bg-alt)", color: "var(--body)",
            cursor: "pointer", display: "inline-flex", alignItems: "center", justifyContent: "center",
          }}>
            <Icon name="close" size={16} color="currentColor" />
          </button>
        </div>

        {/* Call meta strip */}
        <div style={{
          padding: "14px 24px", borderBottom: "1px solid var(--line-soft)",
          display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 16,
        }}>
          <Meta label="Customer" value={row.name} />
          <Meta label="Channel"  value={channelLabel} />
          <Meta label="Duration" value={totalDuration} mono />
        </div>

        {/* Transcript */}
        <div style={{ flex: 1, overflow: "hidden", padding: "20px 24px", display: "flex", flexDirection: "column", gap: 12 }}>
          <div style={{
            fontFamily: "var(--font-mono)", fontSize: 10, fontWeight: 600,
            letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--body)",
            display: "flex", alignItems: "center", gap: 8,
          }}>
            <span style={{
              width: 6, height: 6, borderRadius: 999, background: "var(--positive)",
              animation: "hDotPulse 1.6s ease-in-out infinite",
            }}></span>
            Live transcript
          </div>

          <div style={{
            flex: 1, overflow: "auto", display: "flex", flexDirection: "column", gap: 10, paddingRight: 4,
          }}>
            {visible.map((m, i) => (
              <TranscriptLine key={i} who={m.who} text={m.text} agentSrc={row.agent.src} />
            ))}
            {step < script.length - 1 ? (
              <div style={{ marginTop: 2 }}>
                <TypingDots />
              </div>
            ) : null}
          </div>
        </div>

        {/* Footer player */}
        <div style={{
          padding: "16px 24px", borderTop: "1px solid var(--line)",
          display: "flex", alignItems: "center", gap: 14,
          background: "var(--bg-soft)",
        }}>
          <button style={{
            width: 40, height: 40, borderRadius: 999, border: "none",
            background: "var(--cobalt-500)", color: "#fff", cursor: "pointer",
            display: "inline-flex", alignItems: "center", justifyContent: "center",
          }}>
            <svg width="12" height="12" viewBox="0 0 14 14" fill="currentColor"><path d="M3 1.5v11l9-5.5z"/></svg>
          </button>
          <div style={{ flex: 1 }}>
            <div style={{ height: 22, display: "flex", alignItems: "center" }}>
              <FullWaveform color="var(--cobalt-400)" count={42} intensity={0.9} />
            </div>
            <div style={{
              display: "flex", justifyContent: "space-between",
              fontFamily: "var(--font-mono)", fontSize: 10, color: "var(--body)",
              marginTop: 4,
            }}>
              <span>00:{String(Math.min(step * 22, 134)).padStart(2, "0")}</span>
              <span>{totalDuration}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

function Meta({ label, value, mono }) {
  return (
    <div>
      <div style={{
        fontFamily: "var(--font-mono)", fontSize: 10, fontWeight: 500,
        letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--body)",
        marginBottom: 4,
      }}>{label}</div>
      <div style={{
        fontSize: 13, color: "var(--ink)", fontWeight: 600,
        fontFamily: mono ? "var(--font-mono)" : "var(--font-body)",
        whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis",
      }}>{value}</div>
    </div>
  );
}

function TranscriptLine({ who, text, agentSrc }) {
  const isAgent = who === "agent";
  return (
    <div style={{
      display: "flex", alignItems: "flex-end", gap: 8,
      flexDirection: isAgent ? "row" : "row-reverse",
      animation: "hExpand 240ms ease-out both",
    }}>
      {isAgent ? <AgentAvatar src={agentSrc} size={24} ring={false} /> : null}
      <div style={{
        background: isAgent ? "var(--cobalt-500)" : "var(--bg-alt)",
        color: isAgent ? "#fff" : "var(--ink)",
        padding: "9px 13px", borderRadius: 16, fontSize: 13, lineHeight: 1.45,
        maxWidth: 300, fontWeight: 400,
      }}>{text}</div>
    </div>
  );
}

/* ---------- Export ---------- */
Object.assign(window, {
  SectionHead, HomeNav, Hero, VoiceCallCard, FullWaveform,
  PhoneCaptureCTA,
  AgentSpotlight, FeatureHub, FeatureCard,
  Marquee, IntegrationTile, TeamSellsHero, BeforeAfter, GrowthIllo,
  HomeFooter, BubbleChip,
  ConversationPanel,
});
