/* ============================================================
   Tectus — problem + comparison · solution grid · Tectus/GO split
   ============================================================ */
const { Card: S1Card, Eyebrow: S1Eyebrow, Button: S1Button, Badge: S1Badge } = window.TectusDesignSystem_c42b34;

/* ---------- 3. Problem ---------- */
const OLD_WAY = [
  'Calling multiple companies',
  'Slow responses & delays',
  'Unvetted security companies',
  'Unclear pricing & unpredictable costs',
  'No central communication',
];
const NEW_WAY = [
  'Request security in minutes',
  'Real-time updates',
  'Vetted security vendors',
  'Transparent pricing & predictable workflows',
  '24/7 support & centralized communication',
];

function Problem() {
  const [inView, setInView] = useState(false);
  const rightRef = useRef(null);

  useEffect(() => {
    if (!document.getElementById('tt-problem-styles')) {
      const el = document.createElement('style');
      el.id = 'tt-problem-styles';
      el.textContent = `
        @keyframes tt-fly-in { from { opacity:0; transform:translateX(56px); } to { opacity:1; transform:translateX(0); } }
        @keyframes tt-fade-up { from { opacity:0; transform:translateY(12px); } to { opacity:1; transform:translateY(0); } }
        @media(max-width:700px){ .tt-compare{ grid-template-columns:1fr!important; } }
      `;
      document.head.appendChild(el);
    }
    const el = rightRef.current;
    if (!el) return;
    const obs = new IntersectionObserver(([e]) => { if (e.isIntersecting) setInView(true); }, { threshold: 0.15 });
    obs.observe(el);
    return () => obs.disconnect();
  }, []);

  return (
    <section id="problem" style={{ padding: '96px 0' }}>
      <div className="wrap">
        <Reveal style={{ maxWidth: 820, margin: '0 auto 64px', textAlign: 'center' }}>
          <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 'clamp(30px, 4vw, 48px)', letterSpacing: '-0.025em', lineHeight: 1.06, margin: 0, textWrap: 'balance' }}>
            There's a smarter way to protect what matters
          </h2>
        </Reveal>

        <div className="tt-compare" style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 16, alignItems: 'stretch' }}>

          {/* Old ways */}
          <Reveal>
            <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-xl)', padding: '36px 36px 40px', height: '100%', boxSizing: 'border-box' }}>
              <div style={{ fontSize: 16, fontWeight: 600, color: 'var(--color-text-muted)', marginBottom: 28 }}>Old ways</div>
              <ul style={{ listStyle: 'none', margin: 0, padding: 0, display: 'flex', flexDirection: 'column', gap: 20 }}>
                {OLD_WAY.map((x) => (
                  <li key={x} style={{ display: 'flex', gap: 13, alignItems: 'flex-start', fontSize: 15, color: 'var(--color-text-muted)', lineHeight: 1.5 }}>
                    <span style={{ width: 20, height: 20, borderRadius: '50%', border: '1.5px solid #c0402a', display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#c0402a', flexShrink: 0, marginTop: 2 }}><Icon name="x" size={11} /></span>
                    {x}
                  </li>
                ))}
              </ul>
            </div>
          </Reveal>

          {/* Tectus ways — flies in from right */}
          <div ref={rightRef} style={{ opacity: inView ? 1 : 0, animation: inView ? 'tt-fly-in 0.65s cubic-bezier(0.22,1,0.36,1) forwards' : 'none', height: '100%' }}>
            <div style={{ background: 'linear-gradient(160deg, rgba(212,175,55,0.09), transparent 55%), var(--color-surface-3)', border: '1px solid rgba(212,175,55,0.38)', borderRadius: 'var(--radius-xl)', padding: '36px 36px 40px', position: 'relative', overflow: 'hidden', height: '100%', boxSizing: 'border-box' }}>
              <span style={{ position: 'absolute', top: 0, left: 32, right: 32, height: 2, background: 'linear-gradient(90deg, transparent, var(--color-primary), transparent)' }} />
              <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 30 }}>
                <img src={asset('iconWhite', 'assets/tectus-icon-white.webp')} alt="" style={{ height: 26, width: 'auto' }} />
                <span style={{ fontFamily: 'var(--font-display)', fontSize: 17, fontWeight: 700, letterSpacing: '0.04em' }}>TECTUS</span>
              </div>
              <ul style={{ listStyle: 'none', margin: 0, padding: 0, display: 'flex', flexDirection: 'column', gap: 20 }}>
                {NEW_WAY.map((x, idx) => (
                  <li key={x} style={{ display: 'flex', gap: 13, alignItems: 'flex-start', fontSize: 15, color: 'var(--color-text)', lineHeight: 1.5, opacity: inView ? 1 : 0, animation: inView ? `tt-fade-up 0.45s ease ${0.18 + idx * 0.09}s both` : 'none' }}>
                    <span style={{ width: 20, height: 20, borderRadius: '50%', border: '1.5px solid var(--color-primary)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-primary)', flexShrink: 0, marginTop: 2 }}><Icon name="check" size={11} /></span>
                    {x}
                  </li>
                ))}
              </ul>
            </div>
          </div>

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

/* ---------- 4. Solution / system — interactive feature showcase ---------- */
const SYSTEM = [
  { icon: 'clipboard-list', t: 'Requests', d: 'Submit coverage details by time, location, and requirement in minutes.' },
  { icon: 'calendar-clock', t: 'Scheduling', d: 'Set time blocks, windows, and recurring coverage across sites.' },
  { icon: 'git-compare-arrows', t: 'Vendor matching', d: 'Connect to qualified, licensed vendors ranked by fit and availability.' },
  { icon: 'bell', t: 'Service updates', d: 'Real-time notifications as each job moves through the workflow.' },
  { icon: 'badge-check', t: 'Compliance review', d: 'License and insurance checks enforced per request, every time.' },
  { icon: 'bar-chart-3', t: 'Reporting', d: 'Exportable, audit-ready records for every engagement.' },
  { icon: 'message-square', t: 'Messaging', d: 'Coordinate with vendors in one shared thread per job.' },
  { icon: 'activity', t: 'Status tracking', d: 'Assigned, on site, completed — every step logged and visible.' },
];

/* -- inline product UI mockups -- */
function MockRequests() {
  const [submitted, setSubmitted] = useState(false);
  return (
    <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', overflow: 'hidden', boxShadow: '0 24px 64px rgba(0,0,0,0.32)', maxWidth: 380, width: '100%', margin: '0 auto' }}>
      <div style={{ padding: '12px 16px', borderBottom: '1px solid var(--color-divider)', display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{ width: 26, height: 26, borderRadius: 7, background: 'rgba(212,175,55,0.1)', border: '1px solid rgba(212,175,55,0.22)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-primary)' }}><Icon name="clipboard-list" size={13} /></span>
        <span style={{ fontSize: 13, fontWeight: 700 }}>New Service Request</span>
        <span style={{ marginLeft: 'auto', fontSize: 10, padding: '2px 7px', background: submitted ? 'rgba(34,197,94,0.1)' : 'rgba(212,175,55,0.1)', color: submitted ? '#22c55e' : 'var(--color-primary)', borderRadius: 'var(--radius-pill)', fontWeight: 700, letterSpacing: '0.05em' }}>{submitted ? 'SUBMITTED' : 'DRAFT'}</span>
      </div>
      <div style={{ padding: '14px 16px 18px' }}>
        {[['Location', 'Downtown Office — Floor 12'], ['Service type', 'Access Control'], ['Date range', 'Jul 14 – Jul 18, 2025'], ['Personnel', '3 guards / 8hr shifts']].map(([label, value]) => (
          <div key={label} style={{ marginBottom: 10 }}>
            <div style={{ fontSize: 10, color: 'var(--color-text-faint)', marginBottom: 3, fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase' }}>{label}</div>
            <div style={{ background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-sm)', padding: '8px 10px', fontSize: 12.5, color: 'var(--color-text)' }}>{value}</div>
          </div>
        ))}
        <button onClick={() => setSubmitted(s => !s)} style={{ marginTop: 14, width: '100%', padding: '10px', background: submitted ? 'rgba(34,197,94,0.1)' : 'var(--color-primary)', border: submitted ? '1px solid rgba(34,197,94,0.3)' : 'none', borderRadius: 'var(--radius-md)', textAlign: 'center', color: submitted ? '#22c55e' : '#111', fontSize: 13, fontWeight: 700, cursor: 'pointer', transition: 'all 0.18s', outline: 'none' }}
          onMouseEnter={e => { if (!submitted) e.currentTarget.style.filter = 'brightness(1.1)'; }}
          onMouseLeave={e => { e.currentTarget.style.filter = ''; }}
        >{submitted ? '✓ Request Submitted' : 'Submit Request'}</button>
      </div>
    </div>
  );
}

function MockScheduling() {
  const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
  const DEFAULT_ON = new Set(['Mon-0','Mon-1','Mon-2','Tue-1','Tue-2','Tue-3','Thu-2','Thu-3','Thu-4','Fri-0','Fri-1','Fri-2','Fri-3','Sat-1','Sat-2']);
  const [active, setActive] = useState(DEFAULT_ON);
  const rows = ['8 AM', '10 AM', '12 PM', '2 PM', '4 PM'];
  const toggle = (key) => setActive(prev => { const next = new Set(prev); if (next.has(key)) next.delete(key); else next.add(key); return next; });
  return (
    <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', overflow: 'hidden', boxShadow: '0 24px 64px rgba(0,0,0,0.32)', maxWidth: 440, width: '100%', margin: '0 auto' }}>
      <div style={{ padding: '12px 16px', borderBottom: '1px solid var(--color-divider)', display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{ width: 26, height: 26, borderRadius: 7, background: 'rgba(212,175,55,0.1)', border: '1px solid rgba(212,175,55,0.22)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-primary)' }}><Icon name="calendar-clock" size={13} /></span>
        <span style={{ fontSize: 13, fontWeight: 700 }}>Coverage Schedule — Jul 14–20</span>
      </div>
      <div style={{ padding: '8px 12px 12px', overflowX: 'auto' }}>
        <div style={{ display: 'grid', gridTemplateColumns: '44px repeat(7, 1fr)', minWidth: 320 }}>
          <div />
          {days.map((d) => <div key={d} style={{ textAlign: 'center', fontSize: 10, fontWeight: 700, color: 'var(--color-text-faint)', padding: '6px 0', letterSpacing: '0.05em' }}>{d}</div>)}
          {rows.map((r, ri) => (
            <React.Fragment key={r}>
              <div style={{ fontSize: 9.5, color: 'var(--color-text-faint)', paddingRight: 6, textAlign: 'right', lineHeight: '26px' }}>{r}</div>
              {days.map((d) => { const key = `${d}-${ri}`; const on = active.has(key); return <div key={d} onClick={() => toggle(key)} style={{ height: 26, margin: '0 2px 2px', borderRadius: 3, background: on ? 'rgba(212,175,55,0.16)' : 'var(--color-bg)', border: `1px solid ${on ? 'rgba(212,175,55,0.32)' : 'var(--color-border)'}`, cursor: 'pointer', transition: 'all 0.15s' }} />; })}
            </React.Fragment>
          ))}
        </div>
      </div>
    </div>
  );
}

function MockVendors() {
  const [accepted, setAccepted] = useState(null);
  const vendors = [
    { name: 'Apex Security Group', score: 98, badges: ['Licensed', 'Insured', 'Top rated'], star: true },
    { name: 'ShieldForce LLC', score: 91, badges: ['Licensed', 'Insured'], star: true },
    { name: 'Metro Guard Co.', score: 84, badges: ['Licensed'], star: false },
  ];
  return (
    <div style={{ maxWidth: 390, width: '100%', margin: '0 auto', display: 'flex', flexDirection: 'column', gap: 9 }}>
      {vendors.map((v, i) => (
        <div key={v.name} style={{ background: 'var(--color-surface)', border: `1px solid ${accepted === i ? 'rgba(34,197,94,0.38)' : i === 0 ? 'rgba(212,175,55,0.38)' : 'var(--color-border)'}`, borderRadius: 'var(--radius-lg)', padding: '13px 14px', boxShadow: i === 0 ? '0 8px 32px rgba(0,0,0,0.22)' : 'none', transition: 'border-color 0.2s' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
            <div style={{ width: 34, height: 34, borderRadius: 8, background: 'var(--color-bg)', border: '1px solid var(--color-border)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-text-muted)', flexShrink: 0 }}><Icon name="shield" size={15} /></div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
                <span style={{ fontSize: 13, fontWeight: 700, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{v.name}</span>
                {v.star && <Icon name="badge-check" size={13} color="var(--color-primary)" />}
              </div>
              <div style={{ display: 'flex', gap: 4, marginTop: 4, flexWrap: 'wrap' }}>
                {v.badges.map((b) => <span key={b} style={{ fontSize: 9.5, padding: '1px 6px', background: 'var(--color-surface-3)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-pill)', color: 'var(--color-text-faint)', fontWeight: 600 }}>{b}</span>)}
              </div>
            </div>
            <div style={{ textAlign: 'right', flexShrink: 0 }}>
              <div style={{ fontFamily: 'var(--font-mono)', fontSize: 19, fontWeight: 700, color: accepted === i ? '#22c55e' : i === 0 ? 'var(--color-primary)' : 'var(--color-text)', lineHeight: 1, transition: 'color 0.2s' }}>{v.score}%</div>
              <div style={{ fontSize: 9.5, color: 'var(--color-text-faint)', marginTop: 2 }}>match</div>
            </div>
          </div>
          {i === 0 && (
            <div style={{ marginTop: 11, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 7 }}>
              <button onClick={() => setAccepted(accepted === 0 ? null : 0)} style={{ padding: '7px', background: accepted === 0 ? 'rgba(34,197,94,0.1)' : 'var(--color-primary)', border: accepted === 0 ? '1px solid rgba(34,197,94,0.3)' : 'none', borderRadius: 'var(--radius-sm)', textAlign: 'center', color: accepted === 0 ? '#22c55e' : '#111', fontSize: 12, fontWeight: 700, cursor: 'pointer', transition: 'all 0.18s', outline: 'none' }}>{accepted === 0 ? '✓ Accepted' : 'Accept'}</button>
              <button style={{ padding: '7px', background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-sm)', textAlign: 'center', color: 'var(--color-text-muted)', fontSize: 12, fontWeight: 600, cursor: 'pointer', outline: 'none' }}>View profile</button>
            </div>
          )}
        </div>
      ))}
    </div>
  );
}

function MockUpdates() {
  const ALL = [
    { icon: 'check-circle', text: 'Vendor confirmed: Apex Security Group', time: 'Just now', hi: true },
    { icon: 'user-check', text: '3 officers assigned to Downtown Office', time: '2m ago', hi: false },
    { icon: 'map-pin', text: 'Personnel on-site at Floor 12 entrance', time: '14m ago', hi: false },
    { icon: 'bell', text: 'Shift change logged — Officer Rodriguez', time: '1h ago', hi: false },
    { icon: 'file-check', text: 'Compliance check passed for this request', time: '2h ago', hi: false },
  ];
  const [items, setItems] = useState(ALL);
  const [hovered, setHovered] = useState(null);
  const dismiss = (idx) => setItems(prev => prev.filter((_, i) => i !== idx));
  return (
    <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', overflow: 'hidden', boxShadow: '0 24px 64px rgba(0,0,0,0.32)', maxWidth: 380, width: '100%', margin: '0 auto' }}>
      <div style={{ padding: '12px 16px', borderBottom: '1px solid var(--color-divider)', display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{ width: 26, height: 26, borderRadius: 7, background: 'rgba(212,175,55,0.1)', border: '1px solid rgba(212,175,55,0.22)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-primary)' }}><Icon name="bell" size={13} /></span>
        <span style={{ fontSize: 13, fontWeight: 700 }}>Service Updates</span>
        <div style={{ marginLeft: 'auto', display: 'flex', alignItems: 'center', gap: 8 }}>
          {items.length === 0 && <button onClick={() => setItems(ALL)} style={{ fontSize: 10, color: 'var(--color-text-faint)', background: 'none', border: 'none', cursor: 'pointer', padding: 0, textDecoration: 'underline' }}>Reset</button>}
          <span style={{ width: 7, height: 7, borderRadius: '50%', background: '#22c55e' }} /><span style={{ fontSize: 10.5, color: 'var(--color-text-muted)' }}>Live</span>
        </div>
      </div>
      <div>
        {items.length === 0 && <div style={{ padding: '28px 16px', textAlign: 'center', color: 'var(--color-text-faint)', fontSize: 12 }}>All updates cleared</div>}
        {items.map((u, i) => (
          <div key={u.text} onMouseEnter={() => setHovered(i)} onMouseLeave={() => setHovered(null)} style={{ display: 'flex', gap: 11, alignItems: 'flex-start', padding: '10px 14px', borderBottom: i < items.length-1 ? '1px solid var(--color-divider)' : 'none', background: hovered === i ? 'rgba(255,255,255,0.02)' : u.hi ? 'rgba(212,175,55,0.03)' : 'transparent', position: 'relative', transition: 'background 0.15s' }}>
            <div style={{ width: 26, height: 26, borderRadius: '50%', background: u.hi ? 'rgba(212,175,55,0.1)' : 'var(--color-bg)', border: `1px solid ${u.hi ? 'rgba(212,175,55,0.28)' : 'var(--color-border)'}`, display: 'flex', alignItems: 'center', justifyContent: 'center', color: u.hi ? 'var(--color-primary)' : 'var(--color-text-muted)', flexShrink: 0, marginTop: 1 }}><Icon name={u.icon} size={11} /></div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 12, color: u.hi ? 'var(--color-text)' : 'var(--color-text-muted)', lineHeight: 1.4, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{u.text}</div>
              <div style={{ fontSize: 10, color: 'var(--color-text-faint)', marginTop: 1 }}>{u.time}</div>
            </div>
            {hovered === i && (
              <button onClick={() => dismiss(i)} style={{ position: 'absolute', right: 10, top: '50%', transform: 'translateY(-50%)', background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: '50%', width: 18, height: 18, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', color: 'var(--color-text-faint)', padding: 0, outline: 'none', flexShrink: 0 }}>
                <Icon name="x" size={9} />
              </button>
            )}
          </div>
        ))}
      </div>
    </div>
  );
}

function MockCompliance() {
  const checks = [['License verified', 'Valid through Dec 2025'], ['General liability insured', '$2M coverage — current'], ['Background checks complete', 'All 3 officers cleared'], ['Drug screening passed', 'Within 90-day window'], ['Uniform requirements met', 'Confirmed by vendor']];
  const [running, setRunning] = useState(false);
  const timerRef = useRef(null);
  const rerun = () => {
    if (running) return;
    setRunning(true);
    clearTimeout(timerRef.current);
    timerRef.current = setTimeout(() => setRunning(false), 2000);
  };
  return (
    <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', overflow: 'hidden', boxShadow: '0 24px 64px rgba(0,0,0,0.32)', maxWidth: 380, width: '100%', margin: '0 auto' }}>
      <div style={{ padding: '12px 16px', borderBottom: '1px solid var(--color-divider)', display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{ width: 26, height: 26, borderRadius: 7, background: 'rgba(212,175,55,0.1)', border: '1px solid rgba(212,175,55,0.22)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-primary)' }}><Icon name="badge-check" size={13} /></span>
        <span style={{ fontSize: 13, fontWeight: 700 }}>Compliance Review</span>
      </div>
      <div>
        {checks.map(([label, sub], i) => (
          <div key={label} style={{ display: 'flex', gap: 11, alignItems: 'center', padding: '11px 14px', borderBottom: i < checks.length-1 ? '1px solid var(--color-divider)' : 'none' }}>
            <div style={{ width: 20, height: 20, borderRadius: '50%', background: running ? 'rgba(212,175,55,0.08)' : 'rgba(34,197,94,0.08)', border: `1px solid ${running ? 'rgba(212,175,55,0.25)' : 'rgba(34,197,94,0.25)'}`, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0, transition: 'all 0.3s' }}><Icon name={running ? 'loader' : 'check'} size={10} color={running ? 'var(--color-primary)' : '#22c55e'} /></div>
            <div><div style={{ fontSize: 12.5, fontWeight: 600, lineHeight: 1.3 }}>{label}</div><div style={{ fontSize: 10.5, color: 'var(--color-text-faint)', marginTop: 1 }}>{sub}</div></div>
          </div>
        ))}
        <div style={{ margin: '10px 14px 14px', display: 'flex', gap: 8 }}>
          <div style={{ flex: 1, padding: '9px 13px', background: running ? 'rgba(212,175,55,0.05)' : 'rgba(34,197,94,0.05)', border: `1px solid ${running ? 'rgba(212,175,55,0.2)' : 'rgba(34,197,94,0.2)'}`, borderRadius: 'var(--radius-md)', display: 'flex', alignItems: 'center', gap: 7, transition: 'all 0.3s' }}>
            <Icon name={running ? 'loader' : 'shield-check'} size={14} color={running ? 'var(--color-primary)' : '#22c55e'} />
            <span style={{ fontSize: 12, fontWeight: 700, color: running ? 'var(--color-primary)' : '#22c55e', transition: 'color 0.3s' }}>{running ? 'Running checks…' : 'All checks passed — vendor approved'}</span>
          </div>
          <button onClick={rerun} disabled={running} style={{ padding: '9px 12px', background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-md)', fontSize: 11, fontWeight: 600, color: 'var(--color-text-muted)', cursor: running ? 'default' : 'pointer', opacity: running ? 0.5 : 1, outline: 'none', transition: 'opacity 0.2s', whiteSpace: 'nowrap' }}>Re-run</button>
        </div>
      </div>
    </div>
  );
}

function MockReporting() {
  const bars = [62, 78, 54, 91, 68, 85, 73];
  const days = ['M', 'T', 'W', 'T', 'F', 'S', 'S'];
  const [exportState, setExportState] = useState('idle');
  const timerRef = useRef(null);
  const handleExport = () => {
    if (exportState !== 'idle') return;
    setExportState('exporting');
    clearTimeout(timerRef.current);
    timerRef.current = setTimeout(() => {
      setExportState('done');
      timerRef.current = setTimeout(() => setExportState('idle'), 2500);
    }, 1400);
  };
  const exportLabel = exportState === 'exporting' ? 'Exporting…' : exportState === 'done' ? '✓ Exported' : 'Export';
  return (
    <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', overflow: 'hidden', boxShadow: '0 24px 64px rgba(0,0,0,0.32)', maxWidth: 410, width: '100%', margin: '0 auto' }}>
      <div style={{ padding: '12px 16px', borderBottom: '1px solid var(--color-divider)', display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{ width: 26, height: 26, borderRadius: 7, background: 'rgba(212,175,55,0.1)', border: '1px solid rgba(212,175,55,0.22)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-primary)' }}><Icon name="bar-chart-3" size={13} /></span>
        <span style={{ fontSize: 13, fontWeight: 700 }}>Weekly Coverage Report</span>
        <button onClick={handleExport} style={{ marginLeft: 'auto', fontSize: 10.5, padding: '2px 8px', border: `1px solid ${exportState === 'done' ? 'rgba(34,197,94,0.3)' : 'var(--color-border)'}`, borderRadius: 'var(--radius-pill)', color: exportState === 'done' ? '#22c55e' : 'var(--color-text-faint)', background: exportState === 'done' ? 'rgba(34,197,94,0.08)' : 'transparent', cursor: exportState === 'idle' ? 'pointer' : 'default', outline: 'none', transition: 'all 0.2s', whiteSpace: 'nowrap' }}>{exportLabel}</button>
      </div>
      <div style={{ padding: '14px 16px' }}>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 8, marginBottom: 16 }}>
          {[['47', 'Total jobs'], ['98%', 'Fill rate'], ['1.8h', 'Avg. response']].map(([v, l]) => (
            <div key={l} style={{ background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-md)', padding: '9px 10px', textAlign: 'center' }}>
              <div style={{ fontFamily: 'var(--font-mono)', fontSize: 20, fontWeight: 700, color: 'var(--color-primary-light)', lineHeight: 1 }}>{v}</div>
              <div style={{ fontSize: 10, color: 'var(--color-text-faint)', marginTop: 3 }}>{l}</div>
            </div>
          ))}
        </div>
        <div style={{ display: 'flex', alignItems: 'flex-end', gap: 5, height: 72 }}>
          {bars.map((h, i) => (
            <div key={i} style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4 }}>
              <div style={{ position: 'relative', width: '100%', height: `${h}%`, background: i === 3 ? 'var(--color-primary)' : 'rgba(212,175,55,0.22)', borderRadius: '3px 3px 0 0' }}>
                {i === 3 && <span style={{ position: 'absolute', top: -16, left: '50%', transform: 'translateX(-50%)', fontSize: 9.5, fontWeight: 700, color: 'var(--color-primary)', whiteSpace: 'nowrap' }}>91%</span>}
              </div>
              <span style={{ fontSize: 9, color: 'var(--color-text-faint)' }}>{days[i]}</span>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

function MockMessaging() {
  const INIT = [
    { s: 'v', t: 'Team confirmed for tomorrow 0700. Officer Reyes will be lead.' },
    { s: 'c', t: 'Perfect. Please check in with building security on arrival.' },
    { s: 'v', t: 'Understood. Any specific access requirements?' },
    { s: 'c', t: "Badge required for floors 10–14. I'll have it ready at the desk." },
  ];
  const [msgs, setMsgs] = useState(INIT);
  const [input, setInput] = useState('');
  const scrollRef = useRef(null);
  const send = () => {
    const text = input.trim();
    if (!text) return;
    setMsgs(m => [...m, { s: 'c', t: text }]);
    setInput('');
    setTimeout(() => { if (scrollRef.current) scrollRef.current.scrollTop = 99999; }, 30);
  };
  return (
    <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', overflow: 'hidden', boxShadow: '0 24px 64px rgba(0,0,0,0.32)', maxWidth: 390, width: '100%', margin: '0 auto' }}>
      <div style={{ padding: '12px 16px', borderBottom: '1px solid var(--color-divider)', display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{ width: 26, height: 26, borderRadius: 7, background: 'rgba(212,175,55,0.1)', border: '1px solid rgba(212,175,55,0.22)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-primary)' }}><Icon name="message-square" size={13} /></span>
        <span style={{ fontSize: 13, fontWeight: 700 }}>Apex Security Group</span>
        <span style={{ marginLeft: 'auto', width: 7, height: 7, borderRadius: '50%', background: '#22c55e' }} />
      </div>
      <div ref={scrollRef} style={{ padding: '12px', display: 'flex', flexDirection: 'column', gap: 9, maxHeight: 200, overflowY: 'auto' }}>
        {msgs.map((m, i) => (
          <div key={i} style={{ display: 'flex', justifyContent: m.s === 'c' ? 'flex-end' : 'flex-start' }}>
            <div style={{ maxWidth: '80%', background: m.s === 'c' ? 'var(--color-primary)' : 'var(--color-bg)', color: m.s === 'c' ? '#111' : 'var(--color-text)', padding: '7px 10px', borderRadius: m.s === 'c' ? '11px 11px 2px 11px' : '11px 11px 11px 2px', fontSize: 12, lineHeight: 1.45, border: m.s === 'c' ? 'none' : '1px solid var(--color-border)' }}>{m.t}</div>
          </div>
        ))}
      </div>
      <div style={{ padding: '8px 12px 12px', borderTop: '1px solid var(--color-divider)' }}>
        <div style={{ background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-md)', padding: '4px 4px 4px 11px', display: 'flex', alignItems: 'center', gap: 7 }}>
          <input value={input} onChange={e => setInput(e.target.value)} onKeyDown={e => e.key === 'Enter' && send()} placeholder="Message Apex Security Group" style={{ flex: 1, background: 'none', border: 'none', outline: 'none', fontSize: 12, color: 'var(--color-text)', minWidth: 0 }} />
          <button onClick={send} style={{ background: input.trim() ? 'var(--color-primary)' : 'var(--color-surface-3)', border: 'none', borderRadius: 'var(--radius-sm)', width: 26, height: 26, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: input.trim() ? 'pointer' : 'default', transition: 'background 0.15s', flexShrink: 0, outline: 'none' }}>
            <Icon name="send" size={12} color={input.trim() ? '#111' : 'var(--color-text-faint)'} />
          </button>
        </div>
      </div>
    </div>
  );
}

function MockStatusTracking() {
  const ALL_STEPS = [
    { label: 'Request submitted', sub: 'Jul 14, 9:02 AM' },
    { label: 'Vendor matched', sub: 'Apex Security Group' },
    { label: 'Vendor confirmed', sub: 'Jul 14, 9:18 AM' },
    { label: 'Personnel on site', sub: 'Arriving Jul 15, 7:00 AM' },
    { label: 'Job completed', sub: 'Scheduled Jul 18, 5:00 PM' },
  ];
  const [progress, setProgress] = useState(3);
  const advance = () => setProgress(p => p < ALL_STEPS.length - 1 ? p + 1 : 0);

  useEffect(() => {
    if (!document.getElementById('tt-status-styles')) {
      const el = document.createElement('style');
      el.id = 'tt-status-styles';
      el.textContent = `
        @keyframes tt-line-fill { from { transform: scaleY(0); transform-origin: top; } to { transform: scaleY(1); transform-origin: top; } }
        @keyframes tt-line-flow {
          0%   { background-position: 0 0; }
          100% { background-position: 0 32px; }
        }
      `;
      document.head.appendChild(el);
    }
  }, []);

  return (
    <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', overflow: 'hidden', boxShadow: '0 24px 64px rgba(0,0,0,0.32)', maxWidth: 360, width: '100%', margin: '0 auto' }}>
      <div style={{ padding: '12px 16px', borderBottom: '1px solid var(--color-divider)', display: 'flex', alignItems: 'center', gap: 8 }}>
        <span style={{ width: 26, height: 26, borderRadius: 7, background: 'rgba(212,175,55,0.1)', border: '1px solid rgba(212,175,55,0.22)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--color-primary)' }}><Icon name="activity" size={13} /></span>
        <span style={{ fontSize: 13, fontWeight: 700 }}>Downtown Office — Jul 14</span>
        <button onClick={advance} style={{ marginLeft: 'auto', fontSize: 10, padding: '2px 8px', background: 'none', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-pill)', color: 'var(--color-text-faint)', cursor: 'pointer', outline: 'none' }}>{progress >= ALL_STEPS.length - 1 ? 'Reset' : 'Next →'}</button>
      </div>
      <div style={{ padding: '16px 20px 20px' }}>
        {ALL_STEPS.map((s, i) => {
          const done = i < progress;
          const active = i === progress;
          const lineType = i < progress - 1 ? 'done' : i === progress - 1 ? 'flowing' : 'pending';
          return (
            <div key={s.label} onClick={() => setProgress(i)} style={{ display: 'flex', gap: 13, position: 'relative', paddingBottom: i < ALL_STEPS.length-1 ? 18 : 0, cursor: 'pointer' }}>
              {i < ALL_STEPS.length-1 && (
                <div style={{ position: 'absolute', left: 10, top: 22, bottom: 0, width: 1, overflow: 'hidden', background: lineType === 'pending' ? 'var(--color-border)' : 'transparent' }}>
                  {lineType === 'done' && (
                    <div key={`fill-${i}-${progress}`} style={{ width: '100%', height: '100%', background: 'rgba(212,175,55,0.45)', transformOrigin: 'top', animation: 'tt-line-fill 0.35s ease forwards' }} />
                  )}
                  {lineType === 'flowing' && (
                    <div style={{ width: '100%', height: '100%', backgroundImage: 'repeating-linear-gradient(to bottom, transparent 0, transparent 8px, rgba(212,175,55,0.9) 12px, rgba(212,175,55,0.55) 16px, transparent 20px, transparent 32px)', backgroundSize: '100% 32px', animation: 'tt-line-flow 0.9s linear infinite' }} />
                  )}
                </div>
              )}
              <div style={{ width: 21, height: 21, borderRadius: '50%', background: done ? 'var(--color-primary)' : active ? 'rgba(212,175,55,0.1)' : 'var(--color-bg)', border: `2px solid ${done ? 'var(--color-primary)' : active ? 'var(--color-primary)' : 'var(--color-border)'}`, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0, zIndex: 1, transition: 'all 0.3s' }}>
                {done && <Icon name="check" size={10} color="#111" />}
                {active && <div style={{ width: 6, height: 6, borderRadius: '50%', background: 'var(--color-primary)' }} />}
              </div>
              <div style={{ paddingTop: 1 }}>
                <div style={{ fontSize: 12.5, fontWeight: done || active ? 600 : 400, color: done || active ? 'var(--color-text)' : 'var(--color-text-faint)', lineHeight: 1.3, transition: 'all 0.2s' }}>{s.label}</div>
                <div style={{ fontSize: 10.5, color: active ? 'var(--color-primary)' : 'var(--color-text-faint)', marginTop: 1, fontWeight: active ? 600 : 400, transition: 'all 0.2s' }}>{s.sub}</div>
              </div>
            </div>
          );
        })}
      </div>
    </div>
  );
}

const SYS_MOCKUPS = [MockRequests, MockScheduling, MockVendors, MockUpdates, MockCompliance, MockReporting, MockMessaging, MockStatusTracking];

function SystemGrid() {
  const [active, setActive] = useState(0);
  const [tick, setTick] = useState(0);
  const timerRef = useRef(null);

  const startTimer = React.useCallback(() => {
    clearInterval(timerRef.current);
    timerRef.current = setInterval(() => {
      setActive((p) => (p + 1) % SYSTEM.length);
      setTick((t) => t + 1);
    }, 4500);
  }, []);

  useEffect(() => {
    if (!document.getElementById('tt-sys-styles')) {
      const el = document.createElement('style');
      el.id = 'tt-sys-styles';
      el.textContent = '@keyframes tt-pg{from{width:0}to{width:100%}}.tt-sys-tab:hover:not(.tt-sys-tab--on){background:rgba(212,175,55,0.03)!important}.tt-sys-link:hover{color:var(--color-text)!important}@media(max-width:860px){.tt-sys-split{grid-template-columns:1fr!important}.tt-sys-left{border-right:none!important;border-bottom:1px solid var(--color-border)!important}.tt-sys-right{min-height:340px!important}}';
      document.head.appendChild(el);
    }
    startTimer();
    return () => clearInterval(timerRef.current);
  }, [startTimer]);

  const handleSelect = (i) => { setActive(i); setTick((t) => t + 1); startTimer(); };

  return (
    <section id="system" style={{ background: 'var(--color-bg)', padding: '128px 0', borderTop: '1px solid var(--color-border)' }}>
      <div className="wrap" style={{ padding: '0 40px' }}>

        <Reveal>
          <div style={{ maxWidth: 640, margin: '0 auto 64px', textAlign: 'center' }}>
            <div style={{ display: 'inline-flex', alignItems: 'center', gap: 10, fontSize: 11.5, fontWeight: 700, letterSpacing: '0.13em', textTransform: 'uppercase', color: 'var(--color-primary)', marginBottom: 28 }}>
              <span style={{ width: 24, height: 1, background: 'var(--color-primary)', display: 'inline-block', opacity: 0.6 }} />
              The system
              <span style={{ width: 24, height: 1, background: 'var(--color-primary)', display: 'inline-block', opacity: 0.6 }} />
            </div>
            <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 'clamp(38px, 5vw, 62px)', letterSpacing: '-0.032em', lineHeight: 1.02, margin: '0 0 18px', textWrap: 'balance' }}>
              One platform.<br />Every workflow.
            </h2>
            <p style={{ fontSize: 18, color: 'var(--color-text-muted)', lineHeight: 1.65, margin: 0 }}>
              The full operating layer for physical security logistics — request to resolution, in one place.
            </p>
          </div>
        </Reveal>

        <Reveal>
          <div className="tt-sys-split" style={{ display: 'grid', gridTemplateColumns: '272px 1fr', background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-xl)', overflow: 'hidden', minHeight: 520 }}>

            {/* left: feature tabs */}
            <div className="tt-sys-left" style={{ borderRight: '1px solid var(--color-border)' }}>
              {SYSTEM.map((s, i) => (
                <button key={s.t} className={`tt-sys-tab${active === i ? ' tt-sys-tab--on' : ''}`} onClick={() => handleSelect(i)}
                  style={{ width: '100%', textAlign: 'left', background: active === i ? 'rgba(212,175,55,0.055)' : 'transparent', border: 'none', borderBottom: '1px solid var(--color-divider)', cursor: 'pointer', padding: 0, position: 'relative', display: 'block', transition: 'background 0.18s' }}>
                  <span style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 2.5, background: active === i ? 'var(--color-primary)' : 'transparent', borderRadius: '0 2px 2px 0', transition: 'background 0.15s' }} />
                  <div style={{ padding: '13px 16px 13px 20px' }}>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 9 }}>
                      <span style={{ width: 28, height: 28, borderRadius: 7, background: active === i ? 'rgba(212,175,55,0.1)' : 'var(--color-bg)', border: `1px solid ${active === i ? 'rgba(212,175,55,0.26)' : 'var(--color-border)'}`, display: 'flex', alignItems: 'center', justifyContent: 'center', color: active === i ? 'var(--color-primary)' : 'var(--color-text-faint)', flexShrink: 0, transition: 'all 0.18s' }}>
                        <Icon name={s.icon} size={13} />
                      </span>
                      <span style={{ fontFamily: 'var(--font-display)', fontSize: 13, fontWeight: active === i ? 700 : 500, color: active === i ? 'var(--color-text)' : 'var(--color-text-muted)', transition: 'all 0.18s', letterSpacing: '-0.01em', lineHeight: 1.3 }}>{s.t}</span>
                    </div>
                    {active === i && (
                      <>
                        <p style={{ fontSize: 12, color: 'var(--color-text-muted)', margin: '7px 0 9px 37px', lineHeight: 1.5 }}>{s.d}</p>
                        <div key={`pg-${tick}`} style={{ marginLeft: 37, height: 2, background: 'rgba(255,255,255,0.08)', borderRadius: 2, overflow: 'hidden' }}>
                          <div style={{ height: '100%', background: 'var(--color-primary)', borderRadius: 2, animation: 'tt-pg 4.5s linear forwards' }} />
                        </div>
                      </>
                    )}
                  </div>
                </button>
              ))}
            </div>

            {/* right: mockup */}
            <div className="tt-sys-right" style={{ background: 'var(--color-bg)', position: 'relative', display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '28px 24px', overflow: 'hidden', minHeight: 460 }}>
              <div aria-hidden="true" style={{ position: 'absolute', inset: 0, background: 'radial-gradient(ellipse at 55% 40%, rgba(212,175,55,0.065) 0%, transparent 62%)', pointerEvents: 'none' }} />
              {SYSTEM.map((s, i) => {
                const M = SYS_MOCKUPS[i];
                return (
                  <div key={s.t} style={{ position: 'absolute', inset: '28px 24px', display: 'flex', alignItems: 'center', justifyContent: 'center', opacity: active === i ? 1 : 0, transform: active === i ? 'translateY(0)' : 'translateY(8px)', transition: 'opacity 0.35s ease, transform 0.35s ease', pointerEvents: active === i ? 'auto' : 'none' }}>
                    {M && <M />}
                  </div>
                );
              })}
            </div>

          </div>
        </Reveal>

        <Reveal>
          <div style={{ textAlign: 'center', marginTop: 48 }}>
            <a href="#/features" className="tt-sys-link" style={{ display: 'inline-flex', alignItems: 'center', gap: 8, fontSize: 14, fontWeight: 600, color: 'var(--color-text-muted)', letterSpacing: '0.01em', textDecoration: 'none', transition: 'color 0.2s' }}>
              Explore all features <Icon name="arrow-right" size={16} />
            </a>
          </div>
        </Reveal>

      </div>
    </section>
  );
}

/* ---------- 5. Tectus + Tectus GO split ---------- */
const TECTUS_PTS = ['Request coverage by time, location & requirement', 'Track status from match to completion', 'Coordinate with vendors in one place'];
const GO_PTS = ['Receive and accept job requests', 'Assign personnel and manage schedules', 'Update job status from the field'];

function TectusGoSplit() {
  return (
    <section id="tectus-go" className="wrap" style={{ padding: '96px 40px' }}>
      <Reveal><SectionHead center eyebrow="Two connected apps" title="Two connected experiences, one modern operating layer" sub="Tectus coordinates the request and Tectus GO runs the operation — both sides of every job, in sync." /></Reveal>
      <div className="tt-split-2 tt-apps-split" style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 20, paddingTop: 96 }}>
        {[
          { tag: 'For clients', tagTone: 'gold', name: 'Tectus', desc: 'Request & manage security services', pts: TECTUS_PTS, cta: 'Request coverage', href: '#/get-quote', screen: 'assets/tectus-app-screen.webp', mark: 'assets/tectus-icon-white.webp', markId: 'iconWhite' },
          { tag: 'For vendors', tagTone: 'neutral', name: 'Tectus GO', desc: 'Coordinate jobs & field execution', pts: GO_PTS, cta: 'Become a vendor', href: '#/become-a-vendor', screen: 'assets/tectus-go-app-screen.webp', mark: 'assets/tectus-go-icon-white.webp', markId: 'goIconWhite' },
        ].map((c, i) => (
          <Reveal key={c.name} delay={i * 100} style={{ height: '100%' }}>
            <div style={{ display: 'flex', flexDirection: 'column', height: '100%' }}>
              {/* floating phone — no box */}
              <div className="tt-app-phonewrap" style={{ height: 380, marginBottom: 24, display: 'flex', alignItems: 'flex-start', justifyContent: 'center', overflow: 'hidden' }}>
                <img src={c.screen} alt={`${c.name} app`} loading="lazy" className="tt-app-phone" style={{ height: 380, width: 'auto', maxWidth: 'none', display: 'block', filter: 'drop-shadow(0 20px 40px rgba(0,0,0,0.45))', transformOrigin: 'top center', transition: 'transform var(--dur-slow) var(--ease-standard), filter var(--dur-slow) var(--ease-standard)' }} />
              </div>
              {/* text box */}
              <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-xl)', padding: 30, display: 'flex', flexDirection: 'column', flex: 1 }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 8 }}>
                  <span style={{ width: 40, height: 40, borderRadius: 10, background: 'var(--color-surface-3)', border: '1px solid var(--color-border)', display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 8 }}><img src={asset(c.markId, c.mark)} alt={c.name} style={{ maxWidth: '100%', maxHeight: '100%', display: 'block' }} /></span>
                  <span style={{ fontFamily: 'var(--font-display)', fontSize: 22, fontWeight: 700, letterSpacing: '-0.01em' }}>{c.name}</span>
                  <S1Badge tone={c.tagTone}>{c.tag}</S1Badge>
                </div>
                <p style={{ fontSize: 15, color: 'var(--color-text-muted)', margin: '0 0 18px', lineHeight: 1.5 }}>{c.desc}</p>
                <ul style={{ listStyle: 'none', margin: '0 0 24px', padding: 0, display: 'flex', flexDirection: 'column', gap: 11 }}>
                  {c.pts.map((p) => (
                    <li key={p} style={{ display: 'flex', gap: 10, alignItems: 'flex-start', fontSize: 14, color: 'var(--color-text)', lineHeight: 1.45 }}>
                      <Icon name="check" size={16} color="var(--color-primary)" style={{ marginTop: 1, flex: '0 0 auto' }} />{p}
                    </li>
                  ))}
                </ul>
                <a href={c.href} style={{ marginTop: 'auto', display: 'inline-flex', alignItems: 'center', gap: 7, fontSize: 14, fontWeight: 600, color: 'var(--color-primary-light)' }} className="tt-arrow-link">{c.cta} <Icon name="arrow-right" size={15} /></a>
              </div>
            </div>
          </Reveal>
        ))}
      </div>
    </section>
  );
}

Object.assign(window, { Problem, SystemGrid, TectusGoSplit });
