// app.jsx — ITbyPeople landing page: hero, problem-first services, how-it-works, AI hub. Mounts everything.
const { useState: useS, useEffect: useEffectA } = React;

// Option A — services organized by the problem on the visitor's mind. Each card deep-links into services.html.
const PROBLEMS = [
  {
    key: 'security', label: "I'm worried about security",
    items: [
      ['shield-check', 'Cybersecurity', 'Continuous monitoring and threat detection. We catch problems before they become headlines.', 'threat'],
      ['shield-search', 'Security assessment', 'A clear, honest look at your current exposure — no fear tactics, just facts and a plan.', 'sec-assess'],
      ['users', 'Identity & access', 'Control who can access what — inside and outside your organization.', 'identity'],
      ['database-export', 'Backup & recovery', 'Your data is safe and recoverable. If the worst happens, we get you back online fast.', 'backup'],
    ],
  },
  {
    key: 'cloud', label: "We're moving to the cloud",
    items: [
      ['cloud-upload', 'Cloud migration', 'We move your business to AWS or Azure — nothing left behind, no surprises.', 'migration'],
      ['cloud', 'Cloud management', 'Ongoing management of your cloud environment — costs optimized, security maintained.', 'cloud-mgmt'],
      ['mail', 'Email services', 'Microsoft 365 or Google Workspace — configured, secured and managed for your team.', 'email'],
      ['building-store', 'IaaS', 'Virtual servers, storage and networking in the cloud — deployed and managed for you.', 'iaas'],
    ],
  },
  {
    key: 'growing', label: "We're growing fast",
    items: [
      ['topology-star', 'Network design', "A network built for where you're going, not just where you are today.", 'netdesign'],
      ['wifi-2', 'Wi-Fi deployment', 'Enterprise-grade wireless coverage — no dead zones, no guesswork.', 'wifi'],
      ['robot', 'AI adoption', 'We identify where AI saves your team real time, then help you implement it safely.', 'ai'],
      ['transform', 'Digital transformation', 'Swap manual processes for modern systems — your team moves faster, with less friction.', 'digital'],
    ],
  },
  {
    key: 'everything', label: 'I need full IT support',
    items: [
      ['device-laptop', 'Computer & device support', 'Day-to-day help for your whole team — a real person, fast response.', 'hds'],
      ['server', 'Server management', 'Your servers monitored, maintained and kept healthy — on-premise or hosted.', 'server'],
      ['shield-check', 'Patch management', 'Every device updated automatically — no disruptions, no gaps left open.', 'patch'],
      ['wifi', 'Network & infrastructure', 'Routers, switches, firewalls — managed end to end so your team stays connected.', 'network'],
    ],
  },
];

const STEPS = [
  ['clock', 'AI keeps watch', 'It monitors your systems every minute of every day — quietly, in the background.'],
  ['zap', 'We catch it early', 'Most issues are handled before you ever notice them, often overnight.'],
  ['phone', 'A person calls you', 'When something needs you, you talk to someone who already knows your setup.'],
];

function scrollToId(id) {
  const el = document.getElementById(id);
  if (!el) return;
  const top = el.getBoundingClientRect().top + window.scrollY - 76;
  window.scrollTo({ top, behavior: 'smooth' });
}

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "heroLayout": "Split",
  "heroMark": "Show",
  "whyBand": "Navy"
}/*EDITMODE-END*/;

function Page() {
  const ns = window.ITbyPeopleDesignSystem_44acd8 || {};
  const { Button, Card, Badge, LogoMark } = ns;
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [tab, setTab] = useS('schedule');

  // cross-page deep links: index.html#schedule / #support / #how
  useEffectA(() => {
    const h = (location.hash || '').replace('#', '');
    if (h === 'support') { setTab('ticket'); setTimeout(() => scrollToId('contact'), 80); }
    else if (h === 'schedule') { setTab('schedule'); setTimeout(() => scrollToId('contact'), 80); }
    else if (h === 'how') { setTimeout(() => scrollToId('how'), 80); }
  }, []);

  const openHub = (which) => { setTab(which); setTimeout(() => scrollToId('contact'), 30); };
  const onNav = (key) => {
    if (key === 'services') { window.location.href = 'services.html'; return; }
    if (key === 'how') { scrollToId('how'); return; }
    setTab(key === 'support' ? 'ticket' : 'schedule'); scrollToId('contact');
  };

  return (
    <div>
      <SiteHeader page="home" onNav={onNav} />

      {/* ===== Hero ===== */}
      <section id="top" style={{ background: 'linear-gradient(180deg,#0F2645,#13315B)', color: '#fff', position: 'relative', overflow: 'hidden' }}>
        <Constellation opacity={0.55} seed={5} />
        {t.heroLayout === 'Centered' && (
          <div className="hero-side-mark" aria-hidden="true">
            <LogoMark size={188} tone="white" />
          </div>
        )}
        <div style={wrap(1200, t.heroLayout === 'Split' ? '76px 32px 88px' : '88px 32px 100px', { position: 'relative' })}>
          {t.heroLayout === 'Split' ? (
            <div style={{ display: 'grid', gridTemplateColumns: '1.05fr 0.95fr', gap: 56, alignItems: 'center' }} className="hero-grid">
              <div>
                <h1 style={{ fontFamily: 'var(--font-display)', fontSize: 56, lineHeight: 1.04, letterSpacing: '-0.02em', margin: '0 0 20px' }}>Powered by AI.<br /><span style={{ color: 'var(--color-teal)' }}>Driven by people.</span></h1>
                <p style={{ fontSize: 18.5, lineHeight: 1.6, color: 'rgba(255,255,255,0.8)', maxWidth: 480, margin: '0 0 30px' }}>We use AI to catch problems before you notice them. You still talk to a real person — every time. IT for legal, finance and medical teams that want calm, not jargon.</p>
                <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
                  <Button variant="primary" size="lg" onClick={() => openHub('schedule')} iconLeft={<Icon name="calendar" size={18} color="#fff" />}>Book a call</Button>
                  <button onClick={() => openHub('ticket')} style={ghostDark}><Icon name="ticket" size={18} color="#fff" />Open a ticket</button>
                </div>
              </div>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                <LogoMark size={300} tone="mono-white" />
              </div>
            </div>
          ) : (
            <div style={{ maxWidth: 760, margin: '0 auto', textAlign: 'center' }}>
              {t.heroMark === 'Show' && <LogoMark size={68} tone="white" style={{ marginBottom: 26 }} />}
              <h1 style={{ fontFamily: 'var(--font-display)', fontSize: 64, lineHeight: 1.03, letterSpacing: '-0.02em', margin: '0 0 20px' }}>Powered by AI.<br /><span style={{ color: 'var(--color-teal)' }}>Driven by people.</span></h1>
              <p style={{ fontSize: 19, lineHeight: 1.6, color: 'rgba(255,255,255,0.8)', maxWidth: 540, margin: '0 auto 30px' }}>We use AI to catch problems before you notice them. You still talk to a real person — every time.</p>
              <div style={{ display: 'flex', gap: 12, justifyContent: 'center', flexWrap: 'wrap' }}>
                <Button variant="primary" size="lg" onClick={() => openHub('schedule')} iconLeft={<Icon name="calendar" size={18} color="#fff" />}>Book a call</Button>
                <button onClick={() => openHub('ticket')} style={ghostDark}><Icon name="ticket" size={18} color="#fff" />Open a ticket</button>
              </div>
            </div>
          )}
        </div>
        <div style={{ borderTop: '1px solid rgba(255,255,255,0.1)', position: 'relative' }}>
          <div style={wrap(1200, '16px 32px', { display: 'flex', alignItems: 'center', gap: 14, flexWrap: 'wrap' })}>
            <span style={{ fontSize: 12.5, letterSpacing: '0.06em', textTransform: 'uppercase', color: 'rgba(255,255,255,0.5)', fontWeight: 600 }}>Trusted by teams in</span>
            {['Legal', 'Finance & accounting', 'Medical & healthcare'].map((x) => <span key={x} style={{ fontSize: 14, color: 'rgba(255,255,255,0.82)', fontWeight: 500 }}>{x}</span>)}
          </div>
        </div>
      </section>

      {/* ===== Services (problem-first) ===== */}
      <ProblemServices ns={ns} />

      {/* ===== How it works ===== */}
      <section id="how" style={{ padding: '92px 32px', backgroundColor: t.whyBand === 'Navy' ? 'var(--color-navy)' : 'var(--color-mist)', position: 'relative', overflow: 'hidden' }}>
        {t.whyBand === 'Navy' && <Constellation opacity={0.4} seed={9} />}
        <div style={wrap(1080, 0, { position: 'relative' })}>
          <div style={{ textAlign: 'center', maxWidth: 600, margin: '0 auto 52px' }}>
            <Eyebrow tone={t.whyBand === 'Navy' ? 'dark' : 'light'}>How we work</Eyebrow>
            <h2 style={{ ...h2Style, color: t.whyBand === 'Navy' ? '#fff' : 'var(--color-navy)' }}>AI does the watching. We do the talking.</h2>
            <p style={{ fontSize: 17, lineHeight: 1.6, margin: 0, color: t.whyBand === 'Navy' ? 'rgba(255,255,255,0.78)' : 'var(--color-teal-deep)' }}>You are not a number, and you'll never get a wall of jargon. Here's what working with us feels like.</p>
          </div>
          <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3,1fr)', gap: 22 }} className="steps-grid">
            {STEPS.map(([icon, title, body], i) => (
              <div key={title} style={{ backgroundColor: t.whyBand === 'Navy' ? 'rgba(255,255,255,0.05)' : '#fff', border: t.whyBand === 'Navy' ? '1px solid rgba(255,255,255,0.12)' : '1px solid var(--color-teal-mid)', borderRadius: 'var(--radius-xl)', padding: 28 }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 16 }}>
                  <div style={{ width: 44, height: 44, borderRadius: '50%', backgroundColor: 'var(--color-teal)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}><Icon name={icon} size={21} color="#fff" /></div>
                  <span style={{ fontFamily: 'var(--font-mono)', fontSize: 13, color: t.whyBand === 'Navy' ? 'rgba(255,255,255,0.5)' : 'var(--color-slate)' }}>0{i + 1}</span>
                </div>
                <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 22, margin: '0 0 8px', color: t.whyBand === 'Navy' ? '#fff' : 'var(--color-navy)' }}>{title}</h3>
                <p style={{ fontSize: 15, lineHeight: 1.6, margin: 0, color: t.whyBand === 'Navy' ? 'rgba(255,255,255,0.72)' : 'var(--color-slate)' }}>{body}</p>
              </div>
            ))}
          </div>
        </div>
      </section>

      {/* ===== Contact hub ===== */}
      <section id="contact" style={{ padding: '92px 32px', backgroundColor: 'var(--color-cloud)' }}>
        <div style={wrap(1100, 0)}>
          <div style={{ textAlign: 'center', maxWidth: 600, margin: '0 auto 36px' }}>
            <Eyebrow>Let's talk</Eyebrow>
            <h2 style={h2Style}>Get help the way you like.</h2>
            <p style={{ fontSize: 17, color: 'var(--color-slate)', lineHeight: 1.6, margin: 0 }}>Book a call and an engineer reaches out, or open a ticket and let AI route it. Either way, a real person owns it.</p>
          </div>

          <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 32 }}>
            <div style={{ display: 'inline-flex', padding: 5, backgroundColor: '#fff', border: '1px solid var(--color-line)', borderRadius: 999, boxShadow: 'var(--shadow-xs)' }}>
              {[['schedule', 'calendar', 'Book a call'], ['ticket', 'ticket', 'Open a ticket']].map(([id, icon, label]) => (
                <button key={id} onClick={() => setTab(id)} className={'hub-tab' + (tab === id ? ' active' : '')}>
                  <Icon name={icon} size={17} color={tab === id ? '#fff' : '#8C97A8'} />{label}
                </button>
              ))}
            </div>
          </div>

          <div>{tab === 'schedule' ? <Scheduler /> : <TicketIntake />}</div>

          <div style={{ textAlign: 'center', marginTop: 30, fontSize: 14.5, color: 'var(--color-slate)' }}>
            Prefer email? Reach us at <a href="mailto:info@itbypeople.com" style={{ color: 'var(--color-teal)', fontFamily: 'var(--font-mono)', textDecoration: 'none', fontWeight: 500 }}>info@itbypeople.com</a>
          </div>
        </div>
      </section>

      <SiteFooter onNav={onNav} />

      {/* ===== Tweaks ===== */}
      <TweaksPanel>
        <TweakSection label="Hero" />
        <TweakRadio label="Layout" value={t.heroLayout} options={['Split', 'Centered']} onChange={(v) => setTweak('heroLayout', v)} />
        <TweakRadio label="Logo mark" value={t.heroMark} options={['Show', 'Hide']} onChange={(v) => setTweak('heroMark', v)} />
        <TweakSection label="Theme" />
        <TweakRadio label="How-it-works band" value={t.whyBand} options={['Navy', 'Mist']} onChange={(v) => setTweak('whyBand', v)} />
      </TweaksPanel>
    </div>
  );
}

function ProblemServices({ ns }) {
  const [active, setActive] = useS('security');
  const prob = PROBLEMS.find((p) => p.key === active);
  return (
    <section id="services" style={{ padding: '92px 32px', backgroundColor: '#fff' }}>
      <div style={wrap(1100, 0)}>
        <div style={{ maxWidth: 640, marginBottom: 32 }}>
          <Eyebrow>What we take care of</Eyebrow>
          <h2 style={h2Style}>Start with what's on your mind.</h2>
          <p style={{ fontSize: 17, color: 'var(--color-slate)', lineHeight: 1.6, margin: 0 }}>Most people come to us with one thing keeping them up at night. Tell us which sounds like you — we'll show the services that fit.</p>
        </div>

        <div style={{ display: 'flex', gap: 10, flexWrap: 'wrap', marginBottom: 24 }}>
          {PROBLEMS.map((p) => (
            <button key={p.key} onClick={() => setActive(p.key)} className={'prob-tab' + (p.key === active ? ' active' : '')}>{p.label}</button>
          ))}
        </div>

        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(2,1fr)', gap: 14 }} className="prob-grid">
          {prob.items.map(([icon, name, desc, id]) => (
            <a key={name} href={'services.html#' + id} className="prob-card">
              <div className="prob-card-name"><Ti name={icon} size={17} />{name}<Ti name="arrow-up-right" size={14} color="var(--color-teal-mid)" style={{ marginLeft: 'auto' }} /></div>
              <div className="prob-card-desc">{desc}</div>
            </a>
          ))}
        </div>

        <div style={{ marginTop: 18, display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 16, flexWrap: 'wrap' }}>
          <div className="prob-note">Services shown are matched to your situation. Every engagement starts with a 30-minute call — no jargon, no pressure.</div>
          <a href="services.html" style={{ display: 'inline-flex', alignItems: 'center', gap: 7, color: 'var(--color-teal-deep)', fontFamily: 'var(--font-sans)', fontSize: 15, fontWeight: 600, textDecoration: 'none', whiteSpace: 'nowrap' }}>
            See all services in detail<Icon name="arrowRight" size={17} color="var(--color-teal)" />
          </a>
        </div>
      </div>
    </section>
  );
}

function HeroPreview({ onBook, ns }) {
  const { Button } = ns;
  return (
    <div style={{ backgroundColor: '#fff', borderRadius: 'var(--radius-xl)', boxShadow: 'var(--shadow-lg)', padding: 22, position: 'relative' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 16 }}>
        <div style={{ width: 34, height: 34, borderRadius: '50%', backgroundColor: 'var(--color-navy)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}><Icon name="sparkles" size={17} color="var(--color-teal)" /></div>
        <div style={{ fontSize: 13.5, color: 'var(--color-slate)' }}>Schedule with AI</div>
        <span style={{ marginLeft: 'auto', display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: 11.5, fontWeight: 600, color: 'var(--color-teal-deep)' }}><span style={{ width: 7, height: 7, borderRadius: '50%', backgroundColor: 'var(--color-teal)' }} />Online now</span>
      </div>
      <div style={{ backgroundColor: 'var(--color-cloud)', borderRadius: 'var(--radius-md)', padding: '11px 14px', fontSize: 14, color: 'var(--color-ink)', lineHeight: 1.5, marginBottom: 10 }}>Our firewall has been flaky since this morning.</div>
      <div style={{ backgroundColor: 'var(--color-mist)', border: '1px solid var(--color-teal-mid)', borderRadius: 'var(--radius-md)', padding: '11px 14px', fontSize: 14, color: 'var(--color-teal-deep)', lineHeight: 1.5, marginBottom: 14 }}>Sounds like a network issue — here are a few times an engineer can call you today.</div>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3,1fr)', gap: 8, marginBottom: 16 }}>
        {['1:30 PM', '2:00 PM', '3:30 PM'].map((s, i) => (
          <div key={s} style={{ height: 38, borderRadius: 'var(--radius-md)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 12.5, fontWeight: 600, border: i === 1 ? '1.5px solid var(--color-teal)' : '1px solid var(--color-line)', backgroundColor: i === 1 ? 'var(--color-teal)' : '#fff', color: i === 1 ? '#fff' : 'var(--color-navy)' }}>{s}</div>
        ))}
      </div>
      <Button variant="primary" fullWidth onClick={onBook} iconRight={<Icon name="arrowRight" size={17} color="#fff" />}>Find my time</Button>
    </div>
  );
}

(function mount() {
  const tryMount = () => {
    if (!window.ITbyPeopleDesignSystem_44acd8 || !window.Scheduler || !window.TicketIntake || !window.useTweaks || !window.SiteHeader) { setTimeout(tryMount, 40); return; }
    ReactDOM.createRoot(document.getElementById('root')).render(<Page />);
  };
  tryMount();
})();

Object.assign(window, { Page });
