// agent-detail.jsx — Shared agent page template (consumes window.ECHO_AGENTS[window.AGENT_ID])
const { useState, useEffect, useRef } = React;

// ── Logo ─────────────────────────────────────────────────────────
function LogoMark({ size = 26 }) {
  const w = size * (24 / 28);
  return (
    <svg width={w} height={size} viewBox="0 0 24 28" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
      <circle cx="5" cy="14" r="3.2" fill="#60A5FA"/>
      <path d="M10 9 A 5 5 0 0 1 10 19"   stroke="var(--ink)" strokeOpacity="0.96" strokeWidth="2.2" strokeLinecap="round" fill="none"/>
      <path d="M14 6 A 8 8 0 0 1 14 22"   stroke="var(--ink)" strokeOpacity="0.55" strokeWidth="2.2" strokeLinecap="round" fill="none"/>
      <path d="M18 3 A 11 11 0 0 1 18 25" stroke="var(--ink)" strokeOpacity="0.28" strokeWidth="2.2" strokeLinecap="round" fill="none"/>
    </svg>
  );
}

// ── Nav ──────────────────────────────────────────────────────────
function Nav({ theme, setTheme, scrolled }) {
  const links = [
    { label: 'Agents',       href: '../index.html#agents' },
    { label: 'How It Works', href: '../index.html#how-it-works' },
    { label: 'About',        href: '#' },
  ];
  return (
    <nav style={{
      position: 'fixed', top: 0, left: 0, right: 0, zIndex: 100,
      background: scrolled ? (theme === 'dark' ? 'rgba(14,13,11,0.90)' : 'rgba(247,245,241,0.90)') : 'transparent',
      backdropFilter: scrolled ? 'blur(18px) saturate(160%)' : 'none',
      borderBottom: scrolled ? '1px solid var(--hairline)' : '1px solid transparent',
      transition: 'all 0.4s cubic-bezier(0.4,0,0.2,1)',
    }}>
      <div style={{ maxWidth: 1160, margin: '0 auto', padding: '0 40px', height: 60, display: 'flex', alignItems: 'center', gap: 40 }}>
        <a href="../index.html" style={{ display: 'flex', alignItems: 'center', gap: 8, flexShrink: 0 }}>
          <LogoMark size={26} />
          <span style={{ fontFamily: 'var(--ff-mono)', fontSize: 13, fontWeight: 500, letterSpacing: '0.06em', color: 'var(--ink)' }}>Echo</span>
        </a>
        <div style={{ flex: 1, display: 'flex', gap: 28 }}>
          {links.map(l => (
            <a key={l.label} href={l.href} style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.08em', color: 'var(--muted)', textTransform: 'uppercase', transition: 'color 0.2s' }}
              onMouseEnter={e => e.target.style.color = 'var(--ink)'}
              onMouseLeave={e => e.target.style.color = 'var(--muted)'}
            >{l.label}</a>
          ))}
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
          <button onClick={() => setTheme(t => t === 'light' ? 'dark' : 'light')}
            style={{ background: 'none', border: 'none', cursor: 'pointer', color: 'var(--muted)', padding: 6, borderRadius: 6, display: 'flex', transition: 'color 0.2s, background 0.2s' }}
            onMouseEnter={e => { e.currentTarget.style.color = 'var(--ink)'; e.currentTarget.style.background = 'var(--surface)'; }}
            onMouseLeave={e => { e.currentTarget.style.color = 'var(--muted)'; e.currentTarget.style.background = 'transparent'; }}
          >
            {theme === 'light'
              ? <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>
              : <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/></svg>
            }
          </button>
          <a href="#" className="echo-cta echo-cta--primary echo-cta--nav">
            <span className="echo-cta__dot" />
            <span className="echo-cta__label">Get a demo</span>
            <svg className="echo-cta__arrow" width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d="M5 12h14M12 5l7 7-7 7"/></svg>
          </a>
        </div>
      </div>
    </nav>
  );
}

// ── Hero ─────────────────────────────────────────────────────────
function AgentHero({ agent }) {
  const accent = agent.accent || 'var(--accent)';
  return (
    <section className="plate-section" style={{ paddingTop: 100, paddingBottom: 40, position: 'relative', overflow: 'hidden' }}>
      <div className="glass-plate glass-plate--rounded" style={{ maxWidth: 1240, margin: '0 auto', padding: '64px 56px', position: 'relative', overflow: 'hidden' }}>
      <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none', background: `radial-gradient(ellipse 50% 50% at 30% 30%, ${accent}22 0%, transparent 70%)` }} />
      <div style={{ position: 'relative' }}>
        {/* Breadcrumb */}
        <div style={{ animation: 'heroUp 0.6s cubic-bezier(0,0,0.2,1) both', marginBottom: 40, display: 'flex', alignItems: 'center', gap: 10 }}>
          <a href="../index.html#agents" style={{ display: 'inline-flex', alignItems: 'center', gap: 8, fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.08em', color: 'var(--muted)', textTransform: 'uppercase', transition: 'color 0.2s' }}
            onMouseEnter={e => e.currentTarget.style.color = 'var(--ink)'}
            onMouseLeave={e => e.currentTarget.style.color = 'var(--muted)'}
          >
            <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
            All agents
          </a>
        </div>

        <div className="agent-hero-grid" style={{ display: 'grid', gridTemplateColumns: '1.6fr 1fr', gap: 80, alignItems: 'end' }}>
          <div style={{ animation: 'heroUp 0.7s cubic-bezier(0,0,0.2,1) both', animationDelay: '0.08s' }}>
            <div style={{ display: 'inline-flex', alignItems: 'center', gap: 8, fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.14em', color: accent, textTransform: 'uppercase', marginBottom: 18 }}>
              <span style={{ width: 6, height: 6, borderRadius: '50%', background: accent, boxShadow: `0 0 8px ${accent}88` }} />
              {agent.domain}
            </div>
            <h1 style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(64px, 9vw, 128px)', fontWeight: 400, letterSpacing: '-0.04em', lineHeight: 0.92, color: 'var(--ink)', marginBottom: 20 }}>
              {agent.name}
            </h1>
            {agent.personality && (
              <p style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(18px, 1.8vw, 22px)', fontWeight: 300, fontStyle: 'italic', color: accent, lineHeight: 1.4, letterSpacing: '-0.01em', maxWidth: 560, marginBottom: 18, opacity: 0.9 }}>
                {agent.personality}
              </p>
            )}
            <p style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(18px, 1.8vw, 22px)', fontWeight: 400, color: 'var(--ink)', lineHeight: 1.45, letterSpacing: '-0.005em', maxWidth: 580 }}>
              {agent.tagline}
            </p>
          </div>

          <div style={{ animation: 'heroUp 0.7s cubic-bezier(0,0,0.2,1) both', animationDelay: '0.18s', display: 'flex', flexDirection: 'column', gap: 14, paddingBottom: 6 }}>
            <div style={{ background: 'var(--surface)', border: '1px solid var(--hairline)', borderLeft: `2px solid ${accent}`, borderRadius: 12, padding: 22 }}>
              <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, letterSpacing: '0.1em', color: 'var(--muted)', textTransform: 'uppercase', marginBottom: 8 }}>Channel</div>
              <div style={{ fontFamily: 'var(--ff-display)', fontSize: 22, fontWeight: 400, letterSpacing: '-0.01em', color: 'var(--ink)', marginBottom: 4 }}>{agent.channel}</div>
              <div style={{ fontSize: 12, color: 'var(--muted)', lineHeight: 1.5 }}>{agent.channelSub}</div>
            </div>
            <div style={{ background: 'var(--surface)', border: '1px solid var(--hairline)', borderLeft: `2px solid ${accent}`, borderRadius: 12, padding: 22, display: 'flex', alignItems: 'flex-start', gap: 12 }}>
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke={accent} strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0, marginTop: 2 }}><polyline points="20 6 9 17 4 12"/></svg>
              <div>
                <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, letterSpacing: '0.1em', color: 'var(--muted)', textTransform: 'uppercase', marginBottom: 6 }}>Outcome</div>
                <div style={{ fontSize: 14, color: 'var(--ink)', fontWeight: 500, lineHeight: 1.5 }}>{agent.outcome}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
      </div>
    </section>
  );
}

// ── Overview + Metrics ───────────────────────────────────────────
function AgentOverview({ agent }) {
  return (
    <section className="plate-section" style={{ padding: '40px 0' }}>
      <div className="glass-plate glass-plate--rounded" style={{ maxWidth: 1240, margin: '0 auto', padding: '72px 56px' }}>
        <div className="agent-overview-grid" style={{ display: 'grid', gridTemplateColumns: '1fr 1.3fr', gap: 80, alignItems: 'start' }}>
          <div>
            <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.12em', color: 'var(--muted)', textTransform: 'uppercase', marginBottom: 18 }}>What {agent.name} does</div>
            <h2 style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(28px, 3vw, 40px)', fontWeight: 400, letterSpacing: '-0.025em', lineHeight: 1.1, color: 'var(--ink)' }}>
              The work, <em style={{ fontStyle: 'italic', fontWeight: 300, color: 'var(--muted)' }}>handled.</em>
            </h2>
          </div>
          <div>
            <p style={{ fontSize: 17, color: 'var(--ink)', lineHeight: 1.65, marginBottom: 36, fontWeight: 400 }}>{agent.description}</p>
            <div className="agent-metrics" style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 14 }}>
              {agent.metrics.map((m, i) => (
                <div key={i} style={{ background: 'var(--bg)', border: '1px solid var(--hairline)', borderRadius: 10, padding: '20px 18px' }}>
                  <div style={{ fontFamily: 'var(--ff-display)', fontSize: 32, fontWeight: 400, letterSpacing: '-0.025em', color: 'var(--ink)', lineHeight: 1, marginBottom: 8 }}>{m.value}</div>
                  <div style={{ fontSize: 11, color: 'var(--muted)', lineHeight: 1.45 }}>{m.label}</div>
                </div>
              ))}
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ── Capabilities ─────────────────────────────────────────────────
function AgentCapabilities({ agent }) {
  const accent = agent.accent || 'var(--accent)';
  return (
    <section className="plate-section" style={{ padding: '40px 0' }}>
      <div className="glass-plate glass-plate--rounded" style={{ maxWidth: 1240, margin: '0 auto', padding: '72px 56px' }}>
        <div style={{ marginBottom: 56, maxWidth: 600 }}>
          <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.12em', color: 'var(--muted)', textTransform: 'uppercase', marginBottom: 18 }}>Capabilities</div>
          <h2 style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(32px, 3.6vw, 50px)', fontWeight: 400, letterSpacing: '-0.025em', lineHeight: 1.05, color: 'var(--ink)' }}>
            What {agent.name} ships<br /><em style={{ fontStyle: 'italic', fontWeight: 300, color: 'var(--muted)' }}>out of the box.</em>
          </h2>
        </div>
        <div className="agent-cap-grid" style={{ display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', borderTop: '1px solid var(--hairline)', borderLeft: '1px solid var(--hairline)' }}>
          {agent.capabilities.map((cap, i) => (
            <div key={i} style={{
              padding: '36px 32px', borderRight: '1px solid var(--hairline)', borderBottom: '1px solid var(--hairline)',
              transition: 'background 0.25s',
            }}
              onMouseEnter={e => e.currentTarget.style.background = 'var(--surface)'}
              onMouseLeave={e => e.currentTarget.style.background = 'transparent'}
            >
              <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 16 }}>
                <span style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, color: accent, letterSpacing: '0.08em', fontWeight: 600 }}>{String(i + 1).padStart(2, '0')}</span>
                <span style={{ flex: 1, height: 1, background: 'var(--hairline)' }} />
              </div>
              <h3 style={{ fontFamily: 'var(--ff-display)', fontSize: 24, fontWeight: 400, letterSpacing: '-0.015em', color: 'var(--ink)', marginBottom: 10 }}>{cap.title}</h3>
              <p style={{ fontSize: 14, color: 'var(--muted)', lineHeight: 1.65 }}>{cap.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ── Workflow ─────────────────────────────────────────────────────
function AgentWorkflow({ agent }) {
  const accent = agent.accent || 'var(--accent)';
  return (
    <section className="plate-section" style={{ padding: '40px 0' }}>
      <div className="glass-plate glass-plate--rounded" style={{ maxWidth: 1240, margin: '0 auto', padding: '72px 56px' }}>
        <div style={{ marginBottom: 56, maxWidth: 600 }}>
          <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.12em', color: 'var(--muted)', textTransform: 'uppercase', marginBottom: 18 }}>How it runs</div>
          <h2 style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(32px, 3.6vw, 50px)', fontWeight: 400, letterSpacing: '-0.025em', lineHeight: 1.05, color: 'var(--ink)' }}>
            One workflow,<br /><em style={{ fontStyle: 'italic', fontWeight: 300, color: 'var(--muted)' }}>start to finish.</em>
          </h2>
        </div>
        <div className="agent-workflow-grid" style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 0, position: 'relative' }}>
          {agent.workflow.map((w, i) => (
            <div key={i} style={{ position: 'relative', paddingRight: i < 3 ? 24 : 0 }}>
              <div style={{ background: 'var(--bg)', border: '1px solid var(--hairline)', borderRadius: 12, padding: '28px 22px', minHeight: 168, display: 'flex', flexDirection: 'column', gap: 14 }}>
                <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
                  <span style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.1em', color: accent, fontWeight: 600 }}>STEP {String(i + 1).padStart(2, '0')}</span>
                  {i < 3 && (
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke={accent} strokeWidth="1.8" strokeLinecap="round" style={{ opacity: 0.55 }}><path d="M5 12h14M12 5l7 7-7 7"/></svg>
                  )}
                </div>
                <h4 style={{ fontFamily: 'var(--ff-display)', fontSize: 20, fontWeight: 400, letterSpacing: '-0.01em', color: 'var(--ink)', lineHeight: 1.2 }}>{w.step}</h4>
                <p style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, color: 'var(--muted)', lineHeight: 1.55, letterSpacing: '0.02em', marginTop: 'auto' }}>{w.detail}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ── Integrations ─────────────────────────────────────────────────
function AgentIntegrations({ agent }) {
  const accent = agent.accent || 'var(--accent)';
  return (
    <section className="plate-section" style={{ padding: '40px 0' }}>
      <div className="glass-plate glass-plate--rounded" style={{ maxWidth: 1240, margin: '0 auto', padding: '72px 56px' }}>
        <div className="agent-integrations-grid" style={{ display: 'grid', gridTemplateColumns: '1fr 1.4fr', gap: 80, alignItems: 'start' }}>
          <div>
            <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.12em', color: 'var(--muted)', textTransform: 'uppercase', marginBottom: 18 }}>Integrations</div>
            <h2 style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(32px, 3.6vw, 50px)', fontWeight: 400, letterSpacing: '-0.025em', lineHeight: 1.1, color: 'var(--ink)', marginBottom: 24 }}>
              Plugs into <em style={{ fontStyle: 'italic', fontWeight: 300, color: 'var(--muted)' }}>your stack.</em>
            </h2>
            <p style={{ fontSize: 14, color: 'var(--muted)', lineHeight: 1.7, maxWidth: 360 }}>
              Read-write access in your own tenant. No data egress. Your TruckMate, your Azure, your audit trail.
            </p>
          </div>
          <div>
            <div style={{ display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', gap: 0, border: '1px solid var(--hairline)', borderRadius: 12, overflow: 'hidden', marginBottom: 28 }}>
              {agent.integrations.map((name, i) => (
                <div key={i} style={{
                  padding: '22px 24px',
                  borderRight: i % 2 === 0 ? '1px solid var(--hairline)' : 'none',
                  borderBottom: i < agent.integrations.length - 2 ? '1px solid var(--hairline)' : 'none',
                  display: 'flex', alignItems: 'center', gap: 12,
                }}>
                  <div style={{ width: 8, height: 8, borderRadius: '50%', background: accent, flexShrink: 0, boxShadow: `0 0 8px ${accent}66` }} />
                  <span style={{ fontSize: 13, color: 'var(--ink)', fontWeight: 500 }}>{name}</span>
                </div>
              ))}
            </div>
            <div style={{ borderTop: '1px solid var(--hairline)', paddingTop: 24 }}>
              <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, letterSpacing: '0.1em', color: 'var(--muted)', textTransform: 'uppercase', marginBottom: 12 }}>Escalation triggers</div>
              <p style={{ fontSize: 13, color: 'var(--ink)', lineHeight: 1.7 }}>{agent.escalations}</p>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ── Other agents ─────────────────────────────────────────────────
function OtherAgents({ currentId }) {
  const order = window.ECHO_AGENT_ORDER;
  const others = order.filter(id => id !== currentId).map(id => window.ECHO_AGENTS[id]);
  return (
    <section className="plate-section" style={{ padding: '40px 0' }}>
      <div className="glass-plate glass-plate--rounded" style={{ maxWidth: 1240, margin: '0 auto', padding: '72px 56px' }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', marginBottom: 48 }}>
          <div>
            <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.12em', color: 'var(--muted)', textTransform: 'uppercase', marginBottom: 18 }}>The rest of the team</div>
            <h2 style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(30px, 3.5vw, 46px)', fontWeight: 400, letterSpacing: '-0.025em', lineHeight: 1.05, color: 'var(--ink)' }}>
              Three more agents,<br /><em style={{ fontStyle: 'italic', fontWeight: 300, color: 'var(--muted)' }}>all on the same stack.</em>
            </h2>
          </div>
          <a href="../index.html#agents" style={{ fontFamily: 'var(--ff-mono)', fontSize: 11, letterSpacing: '0.08em', color: 'var(--muted)', display: 'flex', alignItems: 'center', gap: 7, borderBottom: '1px solid var(--hairline)', paddingBottom: 2, textTransform: 'uppercase', transition: 'all 0.2s' }}
            onMouseEnter={e => { e.currentTarget.style.color = 'var(--ink)'; e.currentTarget.style.borderColor = 'var(--ink)'; }}
            onMouseLeave={e => { e.currentTarget.style.color = 'var(--muted)'; e.currentTarget.style.borderColor = 'var(--hairline)'; }}
          >
            All agents
            <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d="M5 12h14M12 5l7 7-7 7"/></svg>
          </a>
        </div>
        <div className="agent-others-grid" style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 14 }}>
          {others.map(a => <OtherAgentCard key={a.id} agent={a} />)}
        </div>
      </div>
    </section>
  );
}

function OtherAgentCard({ agent }) {
  const [hovered, setHovered] = useState(false);
  const accent = agent.accent || 'var(--accent)';
  return (
    <a href={`${agent.id}.html`}
      onMouseEnter={() => setHovered(true)} onMouseLeave={() => setHovered(false)}
      style={{
        background: 'var(--bg)',
        border: `1px solid ${hovered ? accent : 'var(--hairline)'}`,
        borderLeft: `3px solid ${hovered ? accent : 'var(--hairline)'}`,
        borderRadius: 12, padding: '28px 24px', display: 'flex', flexDirection: 'column', gap: 16,
        cursor: 'pointer', transform: hovered ? 'translateY(-3px)' : 'none',
        boxShadow: hovered ? `0 12px 40px ${accent}33` : 'none',
        transition: 'all 0.28s cubic-bezier(0.4,0,0.2,1)', textDecoration: 'none',
      }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start' }}>
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontFamily: 'var(--ff-mono)', fontSize: 10, letterSpacing: '0.1em', color: hovered ? accent : 'var(--muted)' }}>
          <span style={{ width: 6, height: 6, borderRadius: '50%', background: accent, opacity: hovered ? 1 : 0.55, transition: 'opacity 0.25s' }} />
          {agent.domain.toUpperCase()}
        </span>
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={hovered ? accent : 'var(--muted)'} strokeWidth="2" strokeLinecap="round" style={{ transition: 'all 0.28s', transform: hovered ? 'translate(2px, -2px)' : 'none' }}><path d="M7 17L17 7M7 7h10v10"/></svg>
      </div>
      <div>
        <div style={{ fontFamily: 'var(--ff-display)', fontSize: 34, fontWeight: 400, letterSpacing: '-0.02em', lineHeight: 1, marginBottom: 4 }}>{agent.name}</div>
        <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, letterSpacing: '0.08em', color: accent, textTransform: 'uppercase' }}>{agent.channel}</div>
      </div>
      <p style={{ fontSize: 13, color: 'var(--muted)', lineHeight: 1.6 }}>{agent.tagline}</p>
    </a>
  );
}

// ── CTA ──────────────────────────────────────────────────────────
function AgentCTA({ agent }) {
  const accent = agent.accent || 'var(--accent)';
  return (
    <section className="plate-section" style={{ padding: '40px 0' }}>
      <div className="glass-plate glass-plate--rounded" style={{ maxWidth: 880, margin: '0 auto', padding: '64px 56px', textAlign: 'center', position: 'relative', overflow: 'hidden' }}>
        <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none', background: `radial-gradient(ellipse 70% 80% at 50% 100%, ${accent}1f 0%, transparent 65%)` }} />
        <div style={{ position: 'relative' }}>
          <h2 style={{ fontFamily: 'var(--ff-display)', fontSize: 'clamp(36px, 4.6vw, 60px)', fontWeight: 400, letterSpacing: '-0.03em', lineHeight: 1.0, color: 'var(--ink)', marginBottom: 20 }}>
            See {agent.name} <em style={{ fontStyle: 'italic', fontWeight: 300, color: 'var(--muted)' }}>at work.</em>
          </h2>
          <p style={{ fontSize: 16, color: 'var(--muted)', lineHeight: 1.65, marginBottom: 36, maxWidth: 480, margin: '0 auto 36px' }}>
            A 30-minute walkthrough on your TruckMate data. No commitment, no slides, no sales theater.
          </p>
          <div style={{ display: 'flex', gap: 14, justifyContent: 'center', flexWrap: 'wrap' }}>
            <a href="#" className="echo-cta echo-cta--primary" style={{
              display: 'inline-flex', alignItems: 'center', gap: 12,
              background: 'linear-gradient(180deg, #3B82F6 0%, #2563EB 100%)',
              color: '#F4F0E8',
              padding: '16px 26px',
              borderRadius: 10,
              fontFamily: 'var(--ff-mono)', fontSize: 12, letterSpacing: '0.12em', textTransform: 'uppercase',
              fontWeight: 600,
              transition: 'all 0.3s var(--ease-out)',
              boxShadow: '0 14px 40px rgba(37,99,235,0.5), inset 0 1px 0 rgba(255,255,255,0.22)',
              border: '1px solid rgba(96,165,250,0.55)',
            }}
              onMouseEnter={e => { e.currentTarget.style.background = 'linear-gradient(180deg, #60A5FA 0%, #3B82F6 100%)'; e.currentTarget.style.transform = 'translateY(-2px)'; e.currentTarget.style.boxShadow = '0 18px 48px rgba(37,99,235,0.65), inset 0 1px 0 rgba(255,255,255,0.3)'; e.currentTarget.querySelector('.ar').style.transform = 'translateX(4px)'; }}
              onMouseLeave={e => { e.currentTarget.style.background = 'linear-gradient(180deg, #3B82F6 0%, #2563EB 100%)'; e.currentTarget.style.transform = 'translateY(0)'; e.currentTarget.style.boxShadow = '0 14px 40px rgba(37,99,235,0.5), inset 0 1px 0 rgba(255,255,255,0.22)'; e.currentTarget.querySelector('.ar').style.transform = 'translateX(0)'; }}
            >
              <span style={{ width: 7, height: 7, borderRadius: '50%', background: '#7DC9A8', boxShadow: '0 0 10px rgba(125,201,168,0.9)' }}></span>
              Get a demo
              <svg className="ar" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" style={{ transition: 'transform 0.3s var(--ease-out)' }}><path d="M5 12h14M12 5l7 7-7 7"/></svg>
            </a>
            <a href="../index.html#agents" className="echo-cta echo-cta--ghost" style={{
              display: 'inline-flex', alignItems: 'center', gap: 10,
              padding: '15px 22px',
              border: '1px solid var(--hairline)',
              borderRadius: 10,
              fontFamily: 'var(--ff-mono)', fontSize: 12,
              letterSpacing: '0.12em',
              textTransform: 'uppercase',
              fontWeight: 500,
              color: 'var(--ink)',
              background: 'transparent',
              transition: 'all 0.3s var(--ease-out)',
            }}
              onMouseEnter={e => { e.currentTarget.style.borderColor = accent; e.currentTarget.style.color = accent; }}
              onMouseLeave={e => { e.currentTarget.style.borderColor = 'var(--hairline)'; e.currentTarget.style.color = 'var(--ink)'; }}
            >
              Meet the others
              <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d="M5 12h14M12 5l7 7-7 7"/></svg>
            </a>
          </div>
        </div>
      </div>
    </section>
  );
}

// ── Footer ───────────────────────────────────────────────────────
function AgentFooter({ theme, setTheme }) {
  const links = {
    Product: ['Agents', 'How It Works', 'Changelog'],
    Company: ['About', 'Contact', 'Privacy', 'Terms'],
  };
  return (
    <footer className="plate-section" style={{ padding: '40px 0 60px' }}>
      <div className="glass-plate glass-plate--rounded" style={{ maxWidth: 1240, margin: '0 auto', padding: '56px 56px 36px' }}>
        <div style={{ display: 'grid', gridTemplateColumns: '2fr 1fr 1fr 1fr', gap: 40, marginBottom: 56 }}>
          <div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 16 }}>
              <LogoMark size={26} />
              <span style={{ fontFamily: 'var(--ff-mono)', fontSize: 13, fontWeight: 500, letterSpacing: '0.05em' }}>Echo</span>
            </div>
            <p style={{ fontFamily: 'var(--ff-display)', fontSize: 16, color: 'var(--ink)', lineHeight: 1.5, maxWidth: 280, marginBottom: 14, fontWeight: 400, letterSpacing: '-0.01em' }}>
              AI agents for regional carriers.<br/>
              <span style={{ color: 'var(--muted)', fontStyle: 'italic', fontWeight: 300 }}>Built on TruckMate. Owned by you.</span>
            </p>
            <a href="#" className="echo-cta echo-cta--primary echo-cta--sm" style={{
              fontFamily: 'var(--ff-mono)', fontSize: 10.5, letterSpacing: '0.12em', textTransform: 'uppercase',
              color: '#F4F0E8',
              background: 'linear-gradient(180deg, #3B82F6 0%, #2563EB 100%)',
              padding: '8px 14px', borderRadius: 7,
              display: 'inline-flex', alignItems: 'center', gap: 7,
              fontWeight: 600,
              boxShadow: '0 4px 14px rgba(37,99,235,0.3)',
              border: '1px solid rgba(96,165,250,0.5)',
              marginBottom: 14,
            }}>
              <span style={{ width: 5, height: 5, borderRadius: '50%', background: '#7DC9A8', boxShadow: '0 0 6px rgba(125,201,168,0.85)' }}></span>
              Get a demo
              <svg width="9" height="9" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d="M5 12h14M12 5l7 7-7 7"/></svg>
            </a>
            <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 9.5, letterSpacing: '0.1em', color: 'var(--muted)', textTransform: 'uppercase' }}>A vertical of Echo Agents</div>
          </div>
          {Object.entries(links).map(([section, items]) => (
            <div key={section}>
              <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--muted)', marginBottom: 14 }}>{section}</div>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
                {items.map(item => (
                  <a key={item} href="#" style={{ fontSize: 13, color: 'var(--muted)', transition: 'color 0.2s' }}
                    onMouseEnter={e => e.target.style.color = 'var(--ink)'}
                    onMouseLeave={e => e.target.style.color = 'var(--muted)'}
                  >{item}</a>
                ))}
              </div>
            </div>
          ))}
          <div>
            <div style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--muted)', marginBottom: 14 }}>Display</div>
            <button onClick={() => setTheme(t => t === 'light' ? 'dark' : 'light')} style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '7px 12px', borderRadius: 7, border: '1px solid var(--hairline)', background: 'var(--surface)', color: 'var(--ink)', cursor: 'pointer', fontSize: 11, fontFamily: 'var(--ff-mono)', letterSpacing: '0.06em', transition: 'border-color 0.2s' }}
              onMouseEnter={e => e.currentTarget.style.borderColor = 'var(--ink)'}
              onMouseLeave={e => e.currentTarget.style.borderColor = 'var(--hairline)'}
            >
              {theme === 'light' ? 'Dark mode' : 'Light mode'}
            </button>
          </div>
        </div>
        <div style={{ borderTop: '1px solid var(--hairline)', paddingTop: 24, display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: 14 }}>
          <span style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, color: 'var(--muted)', letterSpacing: '0.07em' }}>© 2026 ECHO AGENTS INC.</span>
          <div style={{ display: 'flex', alignItems: 'center', gap: 14, flexWrap: 'wrap' }}>
            <span style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontFamily: 'var(--ff-mono)', fontSize: 10, color: 'var(--muted)', letterSpacing: '0.07em' }}>
              <span style={{ width: 6, height: 6, borderRadius: '50%', background: '#7DC9A8', boxShadow: '0 0 6px rgba(125,201,168,0.6)', animation: 'pulse 2.4s ease-in-out infinite' }} />
              All systems operational
            </span>
            <span style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, color: 'var(--muted)', letterSpacing: '0.07em' }}>v2.4 · deployed today</span>
            <span style={{ fontFamily: 'var(--ff-mono)', fontSize: 10, color: 'var(--muted)', letterSpacing: '0.07em' }}>SOC 2 · TRUCKMATE · AZURE</span>
          </div>
        </div>
      </div>
    </footer>
  );
}

// ── App ──────────────────────────────────────────────────────────
function AgentApp() {
  const agent = window.ECHO_AGENTS[window.AGENT_ID];
  const [theme, setThemeState] = useState('dark');
  const [scrolled, setScrolled] = useState(false);

  const setTheme = (fn) => setThemeState(t => typeof fn === 'function' ? fn(t) : fn);

  useEffect(() => { document.documentElement.setAttribute('data-theme', theme); }, [theme]);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 20);
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  useEffect(() => { document.title = `${agent.name} · Echo Maritime`; }, [agent.name]);

  // Plate halo + scroll-reveal observers
  useEffect(() => {
    const obs = new IntersectionObserver(entries => {
      entries.forEach(e => { if (e.isIntersecting) { e.target.classList.add('visible'); obs.unobserve(e.target); } });
    }, { threshold: 0.07 });
    document.querySelectorAll('.reveal').forEach(el => obs.observe(el));

    const plateObs = new IntersectionObserver(entries => {
      entries.forEach(e => {
        if (e.isIntersecting) { e.target.classList.add('in-view'); plateObs.unobserve(e.target); }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -8% 0px' });
    document.querySelectorAll('.glass-plate').forEach(el => plateObs.observe(el));

    return () => { obs.disconnect(); plateObs.disconnect(); };
  }, []);

  return (
    <>
      <Nav theme={theme} setTheme={setTheme} scrolled={scrolled} />
      <main>
        <AgentHero agent={agent} />
        <AgentOverview agent={agent} />
        <AgentCapabilities agent={agent} />
        <AgentWorkflow agent={agent} />
        <AgentIntegrations agent={agent} />
        <OtherAgents currentId={agent.id} />
        <AgentCTA agent={agent} />
      </main>
      <AgentFooter theme={theme} setTheme={setTheme} />
    </>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<AgentApp />);
