/* Hero v3 — editorial hero with:
   • Background: internet & call waves (concentric sonar ripples + audio waveform)
   • Right side: a single unified product DASHBOARD with tabbed sub-brands
     (Code · Voice · Hub · Addons). No more 3 floating cards.
   • Brand naming: Grippy Code (Websites) is the mother; Grippy Voice = Receptionist,
     Grippy Hub = CRM, Addons = plugins. */

const Hero = () => {
  const sectionRef = React.useRef(null);
  const progress = useScrollProgress(sectionRef);

  return (
    <section ref={sectionRef} className="relative min-h-[100vh] pt-28 pb-20 overflow-hidden">

      {/* Background: call/internet waves only — B148, Mike: constellation dots removed,
          waves carry the signal story on their own */}
      <WaveBackground progress={progress} />

      {/* Bottom fade only — keeps the wave bg from clashing with next section */}
      <div className="absolute bottom-0 left-0 right-0 h-44 bg-gradient-to-b from-transparent to-ink pointer-events-none z-[2]" />

      <div className="relative z-10 mx-auto max-w-[1400px] px-6 w-full">

        {/* Top meta row */}
        <div className="flex items-center justify-between mb-10" data-reveal>
          <div className="flex items-center gap-3">
            <span className="relative flex w-2 h-2">
              <span className="absolute inset-0 rounded-full bg-flame animate-ping opacity-60"></span>
              <span className="relative w-2 h-2 rounded-full bg-flame"></span>
            </span>
            <span className="font-mono text-[10px] tracking-[0.32em] text-white/55 uppercase">GRP / 00 · live build · v0.5</span>
          </div>
          <div className="hidden md:flex items-center gap-2 font-mono text-[10px] tracking-[0.28em] text-white/40 uppercase">
            <span className="w-8 h-px bg-white/20"></span>
            <span>Code · Voice · Hub · Addons</span>
          </div>
        </div>

        {/* Headline + dashboard */}
        <div className="grid lg:grid-cols-[1.05fr_1fr] gap-12 lg:gap-14 items-start">

          {/* LEFT: Headline */}
          <div>
            <h1 className="word-rev text-[clamp(44px,7.6vw,124px)] leading-[0.92] tracking-[-0.04em] font-bold">
              {splitWords("Your shop")}<br/>
              <span className="serif text-[1.05em] font-normal text-flame">{splitWords("never misses")}</span><br/>
              {splitWords("a call.")}
            </h1>

            <p className="mt-9 text-white/70 text-lg md:text-[19px] leading-relaxed max-w-[52ch]" data-reveal data-reveal-delay="2">
              One agency, four products. <b className="text-white">Grippy Code</b> ships the website that closes, <b className="text-white">Grippy Voice</b> answers the phone 24/7, <b className="text-white">Grippy Hub</b> runs the CRM, and Addons plug it into the rest of your stack — built for towing, HVAC, plumbing, and trades that can't afford to ring out.
            </p>

            <div className="mt-9 flex flex-wrap gap-3 items-center" data-reveal data-reveal-delay="3">
              <a href="#cta" className="group relative sheen inline-flex items-center gap-2.5 bg-flame hover:bg-ember text-black font-semibold px-7 py-4 rounded-full transition-colors">
                <span className="w-1.5 h-1.5 rounded-full bg-black/80 animate-pulse-dot"></span>
                Hear it answer live
                <span className="transition-transform group-hover:translate-x-1">→</span>
              </a>
              <a href="#receptionist" className="inline-flex items-center gap-2 border border-line2 hover:border-white/40 text-white/85 hover:text-white px-7 py-4 rounded-full backdrop-blur-sm bg-white/[0.02] transition-colors">
                Meet the receptionists
              </a>
              <a href="#websites" className="inline-flex items-center gap-2 border border-line2 hover:border-white/40 text-white/85 hover:text-white px-7 py-4 rounded-full backdrop-blur-sm bg-white/[0.02] transition-colors">
                See a website
              </a>
              <a href="#stack" className="hidden md:inline-flex items-center gap-2 text-white/55 hover:text-white font-mono text-[11px] tracking-[0.22em] uppercase pl-3 transition-colors">
                <span className="w-6 h-px bg-white/30"></span> See the stack
              </a>
            </div>
          </div>

          {/* RIGHT: Unified product dashboard */}
          <div data-reveal data-reveal-delay="2">
            <ProductDashboard />
          </div>
        </div>

        {/* Trust strip / stats */}
        <div className="mt-20 lg:mt-24 border-y border-white/10" data-reveal data-reveal-delay="4">
          <div className="grid grid-cols-2 md:grid-cols-4 divide-x divide-white/10">
            {[
              ['24/7', 'answer rate',  'Never a missed ring'],
              ['<2s',  'pickup time',  'Faster than a human'],
              ['+38%', 'booked jobs',  'After 30 days live'],
              ['100',  'lighthouse',   'Sites that rank & load'],
            ].map(([n, l, sub]) => (
              <div key={l} className="px-6 py-7 group">
                <div className="font-bold text-3xl md:text-[40px] tracking-tight leading-none">{n}</div>
                <div className="font-mono text-[10px] tracking-[0.22em] text-flame/80 mt-3 uppercase">{l}</div>
                <div className="text-white/45 text-xs mt-1.5">{sub}</div>
              </div>
            ))}
          </div>
        </div>
      </div>

      {/* Scroll cue */}
      <div className="absolute bottom-5 right-6 font-mono text-[10px] tracking-[0.3em] text-white/40 flex items-center gap-2 z-10">
        <span>SCROLL</span><span className="w-8 h-px bg-white/30"></span>
      </div>
    </section>
  );
};

/* ─────────────────────────  Background: call / internet waves  ───────────────────────── */
const WaveBackground = ({ progress }) => {
  const canvasRef = React.useRef(null);

  // Audio waveform on canvas — flowing horizontal call signal
  React.useEffect(() => {
    const c = canvasRef.current; if (!c) return;
    const ctx = c.getContext('2d');
    const dpr = Math.min(window.devicePixelRatio || 1, 2);
    let raf;
    const resize = () => {
      c.width = c.clientWidth * dpr; c.height = c.clientHeight * dpr;
      ctx.scale(dpr, dpr);
    };
    resize();
    window.addEventListener('resize', resize);

    const draw = (t) => {
      t *= 0.001;
      const w = c.clientWidth, h = c.clientHeight;
      ctx.clearRect(0, 0, w, h);

      // 3 stacked waves at different speeds and amplitudes
      const waves = [
        { amp: 14, freq: 0.011, speed: 1.0,  color: 'rgba(255, 87, 54, 0.55)',  width: 1.4 },
        { amp:  8, freq: 0.020, speed: 0.55, color: 'rgba(255,137, 97, 0.32)',  width: 1.1 },
        { amp:  4, freq: 0.035, speed: 1.6,  color: 'rgba(255,255,255,0.10)',   width: 0.8 },
      ];
      waves.forEach((wv, idx) => {
        ctx.beginPath();
        for (let x = 0; x <= w; x += 4) {
          const envelope = Math.sin((x / w) * Math.PI); // taper edges
          const y = h * 0.5
                  + Math.sin(x * wv.freq + t * wv.speed) * wv.amp * envelope
                  + Math.cos(x * wv.freq * 0.4 + t * (wv.speed * 0.6 + idx*0.3)) * wv.amp * 0.5 * envelope;
          if (x === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y);
        }
        ctx.strokeStyle = wv.color;
        ctx.lineWidth = wv.width;
        ctx.stroke();
      });
    };
    const tick = (t) => { draw(t); raf = requestAnimationFrame(tick); };
    raf = requestAnimationFrame(tick);
    return () => { cancelAnimationFrame(raf); window.removeEventListener('resize', resize); };
  }, []);

  return (
    <div className="absolute inset-0 pointer-events-none z-0"
         style={{ transform: `translateY(${progress * 40}px)` }}>

      {/* Concentric sonar rings — left side transmitter */}
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 100 100" preserveAspectRatio="none" aria-hidden>
        <defs>
          <radialGradient id="ringFade" cx="0.5" cy="0.5" r="0.5">
            <stop offset="0%" stopColor="rgba(255,87,54,0.6)"/>
            <stop offset="100%" stopColor="rgba(255,87,54,0)"/>
          </radialGradient>
        </defs>
      </svg>
      <SonarRings cx="22%" cy="62%" maxR={620} count={5} />
      <SonarRings cx="78%" cy="28%" maxR={520} count={4} delay={1.2} />

      {/* Center waveform canvas */}
      <canvas
        ref={canvasRef}
        className="absolute left-0 right-0 w-full"
        style={{ top: '52%', height: 220 }}
      />

      {/* Vertical comm signal — subtle data ticks streaming across */}
      <SignalTicks />

      {/* Very subtle vignette so headline reads cleanly */}
      <div className="absolute inset-0"
           style={{ background: 'radial-gradient(70% 60% at 30% 40%, transparent 0%, rgba(5,5,5,0.55) 80%)' }} />
    </div>
  );
};

const SonarRings = ({ cx, cy, maxR = 500, count = 4, delay = 0 }) => (
  <div className="absolute inset-0">
    {Array.from({length: count}).map((_, i) => (
      <span
        key={i}
        className="absolute rounded-full border border-flame/35"
        style={{
          left: cx, top: cy,
          width: maxR, height: maxR,
          transform: 'translate(-50%, -50%) scale(0)',
          animation: `sonar-expand 6s cubic-bezier(.2,.7,.2,1) infinite`,
          animationDelay: `${delay + i * 1.4}s`,
        }}
      />
    ))}
    {/* Transmitter core */}
    <span
      className="absolute rounded-full bg-flame/80"
      style={{
        left: cx, top: cy, width: 8, height: 8,
        transform: 'translate(-50%, -50%)',
        boxShadow: '0 0 20px 4px rgba(255,87,54,0.5)',
        animation: 'sonar-core 1.6s ease-in-out infinite',
        animationDelay: `${delay}s`,
      }}
    />
  </div>
);

const SignalTicks = () => (
  <div className="absolute inset-x-0 top-[18%] h-[64%] overflow-hidden">
    {Array.from({length: 22}).map((_, i) => (
      <span
        key={i}
        className="absolute h-px bg-gradient-to-r from-transparent via-flame/40 to-transparent"
        style={{
          top: `${(i * 4.4) + 2}%`,
          left: '-20%',
          width: `${20 + (i*7)%50}%`,
          animation: `signal-flow ${8 + (i%5)*1.4}s linear infinite`,
          animationDelay: `${-(i*0.7)}s`,
          opacity: 0.4 + (i%3)*0.15,
        }}
      />
    ))}
  </div>
);

/* ─────────────────────────  Right-side product console (premium tier)  ───────────────────────── */
const ProductDashboard = () => {
  const tabs = [
    { id: 'code',   label: 'Code',   sub: 'Websites',        icon: 'globe', accent: '#FF5736' },
    { id: 'voice',  label: 'Voice',  sub: 'AI Receptionist', icon: 'phone', accent: '#FF7E54' },
    { id: 'hub',    label: 'Hub',    sub: 'CRM',             icon: 'hub',   accent: '#FF5736' },
    { id: 'addons', label: 'Addons', sub: 'Integrations',    icon: 'plug',  accent: '#FFB37A' },
  ];
  const [active, setActive] = React.useState(1);
  const [paused, setPaused] = React.useState(false);
  const [time, setTime] = React.useState(() => new Date());

  React.useEffect(() => {
    const tt = setInterval(() => setTime(new Date()), 1000);
    return () => clearInterval(tt);
  }, []);
  React.useEffect(() => {
    if (paused) return;
    const id = setInterval(() => setActive(i => (i + 1) % tabs.length), 5800);
    return () => clearInterval(id);
  }, [paused]);

  const clock = time.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });

  return (
    <div
      className="relative rounded-[18px] overflow-hidden"
      style={{
        background:
          'radial-gradient(120% 100% at 0% 0%, rgba(255,255,255,0.04), rgba(255,255,255,0) 50%),' +
          'linear-gradient(180deg, #0e0e10 0%, #08080a 100%)',
        boxShadow:
          '0 0 0 1px rgba(255,255,255,0.06),' +
          '0 1px 0 rgba(255,255,255,0.05) inset,' +
          '0 -1px 0 rgba(0,0,0,0.4) inset,' +
          '0 30px 80px -20px rgba(0,0,0,0.85),' +
          '0 60px 160px -50px rgba(255,87,54,0.22)',
      }}
      onMouseEnter={() => setPaused(true)}
      onMouseLeave={() => setPaused(false)}
    >
      {/* Top-edge highlight (premium specular line) */}
      <span aria-hidden className="absolute inset-x-6 top-0 h-px bg-gradient-to-r from-transparent via-white/20 to-transparent"/>

      {/* TOOLBAR */}
      <div className="flex items-center gap-3 px-4 py-2.5 border-b border-white/[0.06]"
           style={{ background: 'linear-gradient(180deg, rgba(255,255,255,0.03), rgba(255,255,255,0))' }}>
        <div className="flex items-center gap-2.5">
          <GrippyMark size={20} radius={5}/>
          <div className="leading-none">
            <div className="text-white text-[12.5px] font-semibold tracking-tight">Grippy Code</div>
            <div className="font-mono text-[8.5px] tracking-[0.22em] text-white/40 uppercase mt-0.5">CRM Pro · {tabs[active].sub}</div>
          </div>
        </div>

        {/* Center: command-bar style address */}
        <div className="hidden md:flex flex-1 items-center justify-center">
          <div className="inline-flex items-center gap-2 bg-black/40 border border-white/[0.07] rounded-md px-2.5 py-1.5 shadow-[0_1px_0_rgba(255,255,255,0.04)_inset]">
            <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="#7a7a7a" strokeWidth="2" strokeLinecap="round">
              <rect x="3" y="11" width="18" height="11" rx="2"/>
              <path d="M7 11V7a5 5 0 0 1 10 0v4"/>
            </svg>
            <span className="font-mono text-[10px] tracking-[0.04em] text-white/55">crm.grippycode.com</span>
            <span className="text-white/25">/</span>
            <span className="font-mono text-[10px] tracking-[0.04em] text-white">{tabs[active].sub.toLowerCase().replace(' ', '-')}</span>
            <span className="font-mono text-[9px] tracking-[0.16em] text-white/30 uppercase border-l border-white/10 pl-2 ml-1">⌘ K</span>
          </div>
        </div>

        {/* Right: live + clock + window */}
        <div className="ml-auto flex items-center gap-3">
          <span className="hidden sm:inline-flex items-center gap-1.5 font-mono text-[10px] tracking-[0.16em] text-white/55 tabular-nums">
            <span className="text-emerald-400/80">●</span>{clock}
          </span>
          <span className="hidden lg:inline-flex items-center font-mono text-[9px] tracking-[0.2em] text-white/40 uppercase border-l border-white/10 pl-3">
            US-EAST
          </span>
        </div>
      </div>

      {/* SUB-TOOLBAR — segmented tabs + breadcrumb */}
      <div className="flex items-center gap-3 px-4 py-2.5 border-b border-white/[0.06] bg-white/[0.01]">
        <div className="inline-flex items-center gap-0.5 p-0.5 rounded-lg bg-white/[0.025] border border-white/[0.06] shadow-[0_1px_0_rgba(255,255,255,0.03)_inset]">
          {tabs.map((t, i) => (
            <button
              key={t.id}
              onClick={() => setActive(i)}
              className={`relative inline-flex items-center gap-1.5 px-2.5 py-1.5 rounded-[7px] text-[11.5px] font-medium transition-all duration-300 ${
                active === i
                  ? 'bg-white/[0.07] text-white shadow-[0_1px_0_rgba(255,255,255,0.06)_inset,0_-1px_0_rgba(0,0,0,0.3)_inset]'
                  : 'text-white/55 hover:text-white/80'
              }`}
            >
              <DashIcon name={t.icon} active={active === i}/>
              <span className="leading-none">{t.label}</span>
              {active === i && (
                <span className="ml-1 w-1 h-1 rounded-full bg-flame shadow-[0_0_6px_rgba(255,87,54,0.8)]"></span>
              )}
            </button>
          ))}
        </div>
        <span className="hidden md:inline-flex items-center gap-1.5 font-mono text-[9.5px] tracking-[0.18em] text-white/35 uppercase">
          <span>Workspace</span><span className="text-white/20">/</span><span className="text-white/65">Acme Towing</span><span className="text-white/20">/</span><span className="text-flame">{tabs[active].sub}</span>
        </span>
        <span className="ml-auto inline-flex items-center gap-1.5 font-mono text-[9.5px] tracking-[0.18em] text-emerald-400/90 uppercase">
          <span className="relative flex w-1.5 h-1.5">
            <span className="absolute inset-0 rounded-full bg-emerald-400 animate-ping opacity-60"></span>
            <span className="relative w-1.5 h-1.5 rounded-full bg-emerald-400"></span>
          </span>
          Live
        </span>
      </div>

      {/* PANEL CONTENT — left rail + panel */}
      <div className="flex min-h-[400px]">
        {/* Left rail — vertical product context */}
        <div className="hidden sm:flex flex-col items-center gap-1.5 px-2.5 py-4 border-r border-white/[0.06] bg-white/[0.012]">
          {tabs.map((t, i) => (
            <button
              key={t.id}
              onClick={() => setActive(i)}
              className={`relative group w-9 h-9 rounded-lg grid place-items-center transition-colors ${
                active === i
                  ? 'bg-flame/[0.08] border border-flame/30'
                  : 'border border-transparent hover:bg-white/[0.04]'
              }`}
              aria-label={t.label}
            >
              <PillarIcon name={t.icon} muted={active !== i}/>
              {active === i && (
                <span className="absolute -left-2.5 top-1/2 -translate-y-1/2 w-0.5 h-5 rounded-r bg-flame shadow-[0_0_8px_rgba(255,87,54,0.6)]"></span>
              )}
            </button>
          ))}
          <span className="flex-1"/>
          <button className="w-9 h-9 rounded-lg grid place-items-center border border-white/[0.06] text-white/45 hover:text-white hover:border-white/15 transition-colors" aria-label="Settings">
            <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8">
              <circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09a1.65 1.65 0 0 0-1-1.51 1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09a1.65 1.65 0 0 0 1.51-1 1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33h0a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51h0a1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82v0a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/>
            </svg>
          </button>
        </div>

        {/* Panel */}
        <div className="relative flex-1 min-w-0 overflow-hidden">
          <div key={active} className="animate-[fadeUp_.5s_cubic-bezier(.2,.8,.2,1)]">
            {active === 0 && <PanelCode />}
            {active === 1 && <PanelVoice />}
            {active === 2 && <PanelHub />}
            {active === 3 && <PanelAddons />}
          </div>
        </div>
      </div>

      {/* FOOTER STATUS */}
      <div className="flex items-center justify-between px-4 py-2 border-t border-white/[0.06] bg-white/[0.015]">
        <div className="flex items-center gap-3 font-mono text-[9.5px] tracking-[0.18em] text-white/45 uppercase">
          <span className="flex items-center gap-1.5">
            <span className="w-1 h-1 rounded-full bg-emerald-400 shadow-[0_0_4px_rgba(74,222,128,0.6)]"></span>
            99.9 % uptime
          </span>
          <span className="w-px h-2.5 bg-white/10"></span>
          <span className="text-white/60">Encrypted · TLS 1.3</span>
          <span className="hidden md:inline w-px h-2.5 bg-white/10"></span>
          <span className="hidden md:inline">99 ms · p95</span>
        </div>
        <div className="flex items-center gap-3 font-mono text-[9.5px] tracking-[0.18em] text-white/35 uppercase">
          <span className="hidden md:inline">Workspace · Acme Towing</span>
          <span className="hidden md:inline w-px h-2.5 bg-white/10"></span>
          <span>v 1.0.0</span>
        </div>
      </div>
    </div>
  );
};

const DashIcon = ({ name, active }) => <PillarIcon name={name} muted={!active}/>;

/* ──────────────────────────── PANELS — premium tier ──────────────────────────── */

/* ── Panel · Grippy Code ─────────────────────────────────────────────── */
const PanelCode = () => {
  // Sparkline path — conversion rate over 14 days, lightly noisy uptrend
  const points = [38, 41, 39, 44, 47, 45, 50, 52, 49, 55, 58, 60, 62, 67];
  const max = Math.max(...points), min = Math.min(...points);
  const path = points.map((p, i) => {
    const x = (i / (points.length - 1)) * 100;
    const y = 100 - ((p - min) / (max - min)) * 90 - 5;
    return `${i === 0 ? 'M' : 'L'} ${x} ${y}`;
  }).join(' ');
  const area = `${path} L 100 100 L 0 100 Z`;

  return (
    <div className="p-5 grid grid-cols-5 gap-4">
      {/* Mini browser preview — Under-Control, a real Grippy build */}
      <div className="col-span-3 rounded-xl border border-white/[0.07] overflow-hidden shadow-[0_1px_0_rgba(255,255,255,0.03)_inset]" style={{ background: '#222a1e' }}>
        <div className="flex items-center gap-2 px-3 py-2 border-b border-white/[0.08] bg-black/40">
          <span className="flex gap-1">
            <span className="w-1.5 h-1.5 rounded-full bg-white/12"/>
            <span className="w-1.5 h-1.5 rounded-full bg-white/12"/>
            <span className="w-1.5 h-1.5 rounded-full bg-white/12"/>
          </span>
          <span className="ml-1 font-mono text-[9.5px] tracking-[0.06em] text-white/55">underc.grippycode.com</span>
          <span className="ml-auto inline-flex items-center gap-1 font-mono text-[8.5px] tracking-[0.18em] text-emerald-400/90 uppercase">
            <span className="w-1 h-1 rounded-full bg-emerald-400"></span> SSL
          </span>
        </div>
        <div className="p-3.5" style={{ background: 'linear-gradient(160deg, #2b3325 0%, #1c2318 70%)' }}>
          <div className="font-mono text-[8.5px] tracking-[0.22em] uppercase" style={{ color: '#d8c5a3' }}>A clinical nutrition platform</div>
          <div className="mt-1.5 text-white font-bold text-[18px] leading-[1.05] tracking-tight">Real food.<br/>Real results. <span className="serif italic" style={{ color: '#e8b88a' }}>Real change.</span></div>
          <p className="mt-2 text-white/55 text-[11px] leading-snug">A personalized approach to nutrition that supports your lifestyle and health goals.</p>
          <div className="mt-3 flex gap-1.5">
            <span className="text-[9.5px] font-semibold px-2.5 py-1 rounded-full" style={{ background: '#e9e4d8', color: '#222a1e' }}>Book Your Consultation</span>
            <span className="border border-white/25 text-white/75 text-[9.5px] px-2.5 py-1 rounded-full">Explore Nutrition Plans</span>
          </div>
        </div>
      </div>

      {/* KPI stack with sparkline */}
      <div className="col-span-2 space-y-2.5">
        <KpiTile label="Lighthouse" value="99" sub="performance · perfect"/>
        <KpiTile label="LCP" value="0.9s" sub="largest paint · green"/>
        <div className="rounded-xl border border-flame/25 bg-flame/[0.04] p-3">
          <div className="flex items-center justify-between">
            <span className="font-mono text-[9px] tracking-[0.22em] text-white/55 uppercase">Conv. rate</span>
            <span className="font-mono text-[9px] tracking-[0.18em] text-emerald-400/90 uppercase">↑ 12.4 %</span>
          </div>
          <div className="mt-1.5 flex items-baseline gap-2">
            <span className="font-bold text-[22px] text-flame tabular-nums leading-none">12.4%</span>
            <span className="font-mono text-[9px] text-white/40">vs 8.9% prev</span>
          </div>
          <svg viewBox="0 0 100 100" preserveAspectRatio="none" className="mt-2 w-full h-10 block">
            <defs>
              <linearGradient id="spark-area" x1="0" y1="0" x2="0" y2="1">
                <stop offset="0%" stopColor="#FF5736" stopOpacity="0.45"/>
                <stop offset="100%" stopColor="#FF5736" stopOpacity="0"/>
              </linearGradient>
            </defs>
            <path d={area} fill="url(#spark-area)"/>
            <path d={path} fill="none" stroke="#FF5736" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
            <circle cx="100" cy={100 - ((points[points.length-1] - min) / (max - min)) * 90 - 5} r="2" fill="#FF5736"/>
          </svg>
        </div>
      </div>

      {/* Deploy strip */}
      <div className="col-span-5 rounded-xl border border-white/[0.07] bg-white/[0.015] px-3.5 py-3 flex items-center gap-3">
        <span className="inline-flex items-center gap-1.5 font-mono text-[9.5px] tracking-[0.22em] text-white/55 uppercase shrink-0">
          <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
            <polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/>
          </svg>
          Deploy · main
        </span>
        <span className="font-mono text-[9px] text-white/45">a3f81 · 47s</span>
        <div className="flex-1 h-1 bg-white/[0.06] rounded-full overflow-hidden">
          <span className="block h-full bg-gradient-to-r from-flame to-ember" style={{ width: '78%' }}/>
        </div>
        <span className="font-mono text-[9.5px] text-white/65 tabular-nums">78%</span>
        <span className="font-mono text-[9px] tracking-[0.18em] text-emerald-400/90 uppercase">Healthy</span>
      </div>
    </div>
  );
};

/* ── Panel · Grippy Voice (refined) ──────────────────────────────────── */
const PanelVoice = () => {
  const lines = [
    { who: 'Caller', t: "Hey, I need a tow off the 405..." },
    { who: 'Grippy', t: "On it. Pulling your location — truck #4 is 6 minutes out." },
    { who: 'Caller', t: "Can you text my wife the ETA?" },
    { who: 'Grippy', t: "Done. Confirmed at 7:42 PM. Anything else?" },
  ];
  const [idx, setIdx] = React.useState(0);
  React.useEffect(() => {
    const id = setInterval(() => setIdx(i => (i + 1) % lines.length), 2800);
    return () => clearInterval(id);
  }, []);

  // B148 — real voice sample (Mike's curated Sarah recording, served by the platform)
  const [playing, setPlaying] = React.useState(false);
  const audioRef = React.useRef(null);
  const toggleSample = () => {
    if (!audioRef.current) {
      audioRef.current = new Audio('https://crm.grippycode.com/persona-sample/nova');
      audioRef.current.addEventListener('ended', () => setPlaying(false));
    }
    if (playing) { audioRef.current.pause(); audioRef.current.currentTime = 0; setPlaying(false); }
    else { audioRef.current.play().catch(() => {}); setPlaying(true); }
  };
  React.useEffect(() => () => { if (audioRef.current) audioRef.current.pause(); }, []);

  return (
    <div className="p-5">
      {/* Caller card */}
      <div className="flex items-center gap-3 rounded-xl border border-white/[0.07] bg-gradient-to-br from-white/[0.025] to-white/[0.005] px-3.5 py-3 shadow-[0_1px_0_rgba(255,255,255,0.04)_inset]">
        <div className="relative w-10 h-10 rounded-full ring-1 ring-flame/40 grid place-items-center overflow-hidden"
             style={{ background: 'radial-gradient(60% 60% at 30% 30%, #2a1814 0%, #0a0a0a 70%)' }}>
          <span className="font-semibold text-flame text-[14px] leading-none">M</span>
          <span className="absolute -bottom-0.5 -right-0.5 w-3 h-3 rounded-full bg-emerald-400 border-2 border-[#0a0a0a]"></span>
        </div>
        <div className="flex-1 min-w-0">
          <div className="flex items-center gap-2">
            <span className="text-white text-[13.5px] font-semibold leading-tight">Sarah · The Professional</span>
            <span className="font-mono text-[8.5px] tracking-[0.2em] text-white/45 uppercase border-l border-white/10 pl-1.5">en-US</span>
            <span className="font-mono text-[8.5px] tracking-[0.16em] text-emerald-400/90 uppercase border-l border-white/10 pl-1.5">SR · 94 %</span>
          </div>
          <div className="flex items-center gap-1.5 mt-1">
            <span className="font-mono text-[9.5px] tracking-[0.16em] text-white/55">Inbound · live call · 00:47</span>
          </div>
        </div>
        <div className="flex items-center gap-1.5">
          <button onClick={toggleSample}
                  className={`h-8 px-2.5 rounded-md inline-flex items-center gap-1.5 border transition-colors ${playing
                    ? 'bg-flame/15 border-flame/40 text-flame'
                    : 'bg-white/[0.04] border-white/[0.08] text-white/70 hover:text-white hover:border-white/20'}`}
                  aria-label={playing ? 'Stop sample' : 'Hear Sarah'}>
            {playing
              ? <svg width="10" height="10" viewBox="0 0 24 24" fill="currentColor"><rect x="6" y="6" width="12" height="12" rx="2"/></svg>
              : <svg width="10" height="10" viewBox="0 0 24 24" fill="currentColor"><path d="M8 5v14l11-7z"/></svg>}
            <span className="font-mono text-[8.5px] tracking-[0.14em] uppercase">{playing ? 'Stop' : 'Hear Sarah'}</span>
          </button>
          <button className="w-8 h-8 rounded-md grid place-items-center bg-white/[0.04] border border-white/[0.08] text-white/70 hover:text-white" aria-label="Transfer">
            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M16 3h5v5"/><path d="M4 20L21 3"/><path d="M21 16v5h-5"/><path d="M15 15l6 6"/><path d="M4 4l5 5"/></svg>
          </button>
          <button className="w-8 h-8 rounded-md grid place-items-center bg-flame text-black hover:bg-ember shadow-[0_1px_0_rgba(0,0,0,0.25)]" aria-label="End call">
            <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor"><path d="M19 17.5L15 13l-2 2a11 11 0 0 1-5-5l2-2L5.5 4 4 5a2 2 0 0 0-1 2 15 15 0 0 0 14 14 2 2 0 0 0 2-1l.5-1.5z"/></svg>
          </button>
        </div>
      </div>

      {/* Transcript */}
      <div className="mt-3 rounded-xl border border-white/[0.07] bg-black/30 overflow-hidden">
        <div className="flex items-center justify-between px-3.5 py-2 border-b border-white/[0.06]">
          <span className="font-mono text-[9px] tracking-[0.22em] text-white/55 uppercase">Live transcript</span>
          <div className="flex items-center gap-2">
            <span className="font-mono text-[9px] tracking-[0.18em] text-flame uppercase">Intent · Roadside assist</span>
            <span className="w-px h-2.5 bg-white/10"></span>
            <span className="font-mono text-[9px] tracking-[0.18em] text-white/40 uppercase">Auto-saved</span>
          </div>
        </div>
        <div className="px-3.5 py-3 space-y-1.5 min-h-[96px]">
          {lines.slice(0, idx + 1).map((l, i) => (
            <div key={i} className="flex gap-2.5 items-baseline animate-[fadeUp_.4s_ease-out]">
              <span className={`font-mono text-[9px] tracking-[0.16em] uppercase pt-0.5 w-[54px] shrink-0 ${l.who === 'Grippy' ? 'text-flame' : 'text-white/40'}`}>{l.who}</span>
              <span className={`text-[12.5px] leading-snug ${l.who === 'Grippy' ? 'text-white' : 'text-white/65'}`}>{l.t}</span>
            </div>
          ))}
        </div>
        <div className="flex items-center gap-2 px-3.5 py-2.5 border-t border-white/[0.06] bg-white/[0.012]">
          <span className="font-mono text-[9px] text-white/40 tabular-nums">00:47</span>
          <div className="flex-1 flex items-center justify-center gap-[2px] h-6">
            {Array.from({length: 64}).map((_,i) => (
              <span key={i} className="w-[2px] rounded-full bg-flame/85" style={{
                height: `${22 + Math.abs(Math.sin(i * 0.32 + idx * 0.7)) * 60 + Math.abs(Math.cos(i*0.2)) * 18}%`,
                opacity: 0.4 + Math.abs(Math.sin(i * 0.45 + idx)) * 0.5,
              }}/>
            ))}
          </div>
          <span className="font-mono text-[9px] text-white/40 tabular-nums">01:12</span>
        </div>
      </div>

      {/* Outcome chips — the real post-call automations the platform runs */}
      <div className="mt-3 grid grid-cols-3 gap-2.5">
        <ActionPill label="Lead captured" detail="Synced to CRM · New" icon="check"/>
        <ActionPill label="Quote emailed" detail="Owner BCC'd · sent" icon="msg"/>
        <ActionPill label="Booking held" detail="Calendar · 7:42 PM" icon="cal"/>
      </div>
    </div>
  );
};

/* ── Panel · Grippy Hub — replica of the REAL Command Center dashboard
      (B148, Mike: "our Dashboard looks like this") ─────────────────────── */
const PanelHub = () => {
  const agents = [
    { ini: 'SJ', name: 'Sarah Johnson', role: 'Manager', pipe: '$0',      deals: 0, tasks: 0, won: 0, c: '#38bdf8' },
    { ini: 'MC', name: 'Mike Chen',     role: 'SDR',     pipe: '$12,990', deals: 3, tasks: 2, won: 0, c: '#FF5736' },
    { ini: 'EW', name: 'Emma Wilson',   role: 'SDR',     pipe: '$2,680',  deals: 1, tasks: 1, won: 1, c: '#fbbf24' },
  ];
  return (
    <div className="p-5">
      {/* Command Center hero strip */}
      <div className="mb-3 rounded-xl border border-white/[0.07] bg-white/[0.015] px-4 py-3.5 flex items-center justify-between gap-3 relative overflow-hidden">
        <span aria-hidden className="absolute left-0 top-0 bottom-0 w-[3px] bg-flame"/>
        <div className="min-w-0">
          <div className="font-mono text-[8.5px] tracking-[0.24em] text-flame uppercase flex items-center gap-1.5">
            <span className="w-1 h-1 rounded-full bg-emerald-400"></span> Command Center
          </div>
          <div className="mt-1 text-white font-extrabold text-[17px] tracking-tight leading-tight truncate">
            Welcome back, <span className="text-flame">Acme Towing</span>
          </div>
          <div className="text-white/40 text-[10px] mt-0.5 truncate">Your pipeline, team performance, and action items — all in one place.</div>
        </div>
        <div className="text-right shrink-0">
          <div className="font-extrabold text-[20px] leading-none tabular-nums text-white">$12,990</div>
          <div className="font-mono text-[8px] tracking-[0.2em] text-white/40 uppercase mt-1">Total pipeline</div>
        </div>
      </div>

      {/* KPI tiles — same four as the real dashboard */}
      <div className="grid grid-cols-4 gap-2.5 mb-3">
        <KpiTile label="Total contacts"  value="12"      sub="View contacts →"/>
        <KpiTile label="Open deals"      value="3"       sub="View deals →" flame/>
        <KpiTile label="Pipeline value"  value="$12,990" sub="View pipeline →"/>
        <KpiTile label="Won this month"  value="2"       sub="View won deals →"/>
      </div>

      {/* Agent Performance — per-team-member cards, like the real thing */}
      <div className="rounded-xl border border-white/[0.07] bg-black/30 overflow-hidden">
        <div className="flex items-center justify-between px-3.5 py-2 border-b border-white/[0.06]">
          <span className="font-mono text-[9px] tracking-[0.22em] text-white/55 uppercase">Agent performance</span>
          <span className="font-mono text-[8.5px] tracking-[0.16em] text-white/40 uppercase border border-white/10 rounded-md px-2 py-0.5">All agents</span>
        </div>
        <div className="grid grid-cols-3 divide-x divide-white/[0.05]">
          {agents.map(a => (
            <div key={a.ini} className="px-3 py-2.5">
              <div className="flex items-center gap-2 min-w-0">
                <span className="w-6 h-6 rounded-full grid place-items-center text-[9px] font-bold shrink-0"
                      style={{ background: a.c + '22', color: a.c, border: `1px solid ${a.c}55` }}>{a.ini}</span>
                <div className="min-w-0">
                  <div className="text-[11px] text-white font-semibold truncate leading-tight">{a.name}</div>
                  <div className="font-mono text-[7.5px] tracking-[0.18em] text-white/40 uppercase">{a.role}</div>
                </div>
              </div>
              <div className="mt-2 font-bold text-[14px] tabular-nums text-white">{a.pipe}<span className="font-mono text-[7.5px] tracking-[0.16em] text-white/35 uppercase ml-1">pipeline</span></div>
              <div className="mt-1.5 flex items-center gap-2.5 font-mono text-[8px] tracking-[0.1em] text-white/45 uppercase">
                <span><b className="text-white/80">{a.deals}</b> deals</span>
                <span><b className="text-white/80">{a.tasks}</b> tasks</span>
                <span><b className="text-white/80">{a.won}</b> won</span>
              </div>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
};

/* ── Panel · Addons ──────────────────────────────────────────────────── */
const PanelAddons = () => {
  const addons = [
    { n: 'Stripe',          d: 'Payments & invoicing',  on: true,  cat: 'Billing',   color: '#635BFF' },
    { n: 'Gmail / Outlook', d: 'Two-way email inbox',   on: true,  cat: 'Email',     color: '#EA4335' },
    { n: 'Calendar',        d: 'Booking & dispatch',    on: true,  cat: 'Schedule',  color: '#4285F4' },
    { n: 'CSV Import',      d: 'Migrate from any CRM',  on: true,  cat: 'Data',      color: '#34A853' },
    { n: 'WhatsApp',        d: 'Business messaging',    on: false, cat: 'Messaging', color: '#25D366' },
    { n: 'Instagram DM',    d: 'Social inbox',          on: false, cat: 'Messaging', color: '#E1306C' },
  ];
  return (
    <div className="p-5">
      <div className="flex items-center justify-between mb-3.5">
        <div>
          <div className="font-mono text-[9px] tracking-[0.22em] text-flame uppercase">Addon marketplace</div>
          <div className="text-white font-semibold text-[15px] mt-1 tracking-tight">Plug in the rest of your stack</div>
        </div>
        <div className="flex items-center gap-2 font-mono text-[9.5px] tracking-[0.18em] text-white/45 uppercase">
          <span><span className="text-flame">4</span> active</span>
          <span className="w-px h-2.5 bg-white/10"></span>
          <span>6 available</span>
        </div>
      </div>
      <div className="grid grid-cols-2 gap-2.5">
        {addons.map(a => (
          <div key={a.n}
               className={`group rounded-xl border px-3 py-2.5 flex items-center gap-3 transition-colors ${a.on
                 ? 'border-white/[0.1] bg-white/[0.025]'
                 : 'border-white/[0.06] bg-white/[0.012] hover:border-white/15'}`}>
            <span className="w-8 h-8 rounded-lg grid place-items-center shrink-0 shadow-[0_1px_0_rgba(255,255,255,0.05)_inset]"
                  style={{ background: a.on ? `linear-gradient(135deg, ${a.color}33, ${a.color}11)` : 'rgba(255,255,255,0.04)',
                           border: `1px solid ${a.on ? a.color + '55' : 'rgba(255,255,255,0.07)'}` }}>
              <span className="w-2 h-2 rounded-full" style={{ background: a.on ? a.color : 'rgba(255,255,255,0.3)' }}></span>
            </span>
            <div className="min-w-0 flex-1">
              <div className="flex items-center gap-1.5">
                <span className="text-[12.5px] text-white font-semibold leading-tight truncate">{a.n}</span>
                <span className="font-mono text-[8px] tracking-[0.18em] text-white/40 uppercase border-l border-white/10 pl-1.5">{a.cat}</span>
              </div>
              <span className="block text-[11px] text-white/55 truncate mt-0.5">{a.d}</span>
            </div>
            <span className={`font-mono text-[9px] tracking-[0.2em] uppercase px-2 py-1 rounded-md ${a.on
              ? 'bg-emerald-400/15 text-emerald-300 border border-emerald-400/30'
              : 'border border-white/10 text-white/55 group-hover:text-white group-hover:border-white/25'}`}>
              {a.on ? '● ON' : '+ ADD'}
            </span>
          </div>
        ))}
      </div>
    </div>
  );
};

/* ──────────────────────────── ATOMS ──────────────────────────── */
const KpiTile = ({ label, value, sub, flame }) => (
  <div className={`rounded-xl border ${flame ? 'border-flame/30 bg-flame/[0.06]' : 'border-white/[0.07] bg-white/[0.015]'} px-3 py-2.5 shadow-[0_1px_0_rgba(255,255,255,0.03)_inset]`}>
    <div className="font-mono text-[9px] tracking-[0.22em] text-white/45 uppercase">{label}</div>
    <div className={`mt-1.5 font-bold text-[20px] leading-none tabular-nums tracking-tight ${flame ? 'text-flame' : 'text-white'}`}>{value}</div>
    <div className="font-mono text-[9px] tracking-[0.12em] text-white/45 mt-1.5">{sub}</div>
  </div>
);

const ActionPill = ({ label, detail, icon }) => (
  <div className="rounded-xl border border-white/[0.07] bg-white/[0.02] px-3 py-2.5 shadow-[0_1px_0_rgba(255,255,255,0.03)_inset]">
    <div className="flex items-center gap-1.5">
      <span className="w-3.5 h-3.5 rounded-md bg-emerald-400/15 border border-emerald-400/35 grid place-items-center">
        {icon === 'check' && <svg width="7" height="7" viewBox="0 0 24 24" fill="none" stroke="#34d399" strokeWidth="3.5" strokeLinecap="round" strokeLinejoin="round"><polyline points="20 6 9 17 4 12"/></svg>}
        {icon === 'msg'   && <svg width="7" height="7" viewBox="0 0 24 24" fill="none" stroke="#34d399" strokeWidth="3" strokeLinecap="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>}
        {icon === 'cal'   && <svg width="7" height="7" viewBox="0 0 24 24" fill="none" stroke="#34d399" strokeWidth="3" strokeLinecap="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="3" y1="10" x2="21" y2="10"/></svg>}
      </span>
      <span className="font-mono text-[8.5px] tracking-[0.18em] text-white/55 uppercase">{label}</span>
    </div>
    <div className="text-[12px] text-white font-semibold mt-1 tracking-tight">{detail}</div>
  </div>
);

const PillarIcon = ({ name, muted }) => {
  const stroke = muted ? "#8a8a8a" : "#FF8961";
  if (name === 'globe') return (
    <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke={stroke} strokeWidth="1.7">
      <circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3c3 3.5 3 14 0 18M12 3c-3 3.5-3 14 0 18"/>
    </svg>
  );
  if (name === 'phone') return (
    <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke={stroke} strokeWidth="1.7" strokeLinecap="round">
      <path d="M5 4h4l2 5-2.5 1.5a11 11 0 0 0 5 5L15 13l5 2v4a2 2 0 0 1-2 2A15 15 0 0 1 3 6a2 2 0 0 1 2-2z"/>
    </svg>
  );
  if (name === 'hub') return (
    <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke={stroke} strokeWidth="1.7">
      <circle cx="12" cy="12" r="3"/>
      <circle cx="4"  cy="6"  r="2"/>
      <circle cx="20" cy="6"  r="2"/>
      <circle cx="4"  cy="18" r="2"/>
      <circle cx="20" cy="18" r="2"/>
      <path d="M6 7l4 4M18 7l-4 4M6 17l4-4M18 17l-4-4"/>
    </svg>
  );
  return (
    <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke={stroke} strokeWidth="1.7" strokeLinecap="round">
      <path d="M9 2v6M15 2v6M6 8h12v4a6 6 0 0 1-12 0V8zM12 18v4"/>
    </svg>
  );
};

window.Hero = Hero;
