// Screen 1 — Onboarding
// Three states cycled with a small chip switcher: Sign-in, OAuth consent, Importing.

const ScreenOnboardingSignIn = () => (
  <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: '#1F2A44', color: '#F5F3EF' }}>
    <StatusBar tone="light" />
    <div style={{ flex: 1, display: 'flex', flexDirection: 'column', padding: '32px 28px 28px', position: 'relative' }}>
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'flex-start' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
          <Logo size={56} />
          <span style={{
            fontFamily: '"Mulish", "DM Sans", system-ui, sans-serif',
            fontWeight: 900, fontSize: 32, letterSpacing: '-0.025em',
            lineHeight: 1, color: '#F5F3EF',
          }}>
            work<span style={{ color: '#F08A24' }}>s</span>herpa
          </span>
        </div>
        <div style={{
          fontFamily: '"Bricolage Grotesque"', fontSize: 38, fontWeight: 600, letterSpacing: '-0.025em',
          lineHeight: 1.05, marginTop: 28, color: '#F5F3EF',
        }}>
          The book of business<br/>you never had to type.
        </div>
        <div style={{ fontSize: 16, lineHeight: 1.45, marginTop: 18, color: 'rgba(245,243,239,0.7)', maxWidth: 300 }}>
          Worksherpa reads your inbox, pulls out your customers, and tells you who needs you today.
        </div>
      </div>

      <div style={{ display: 'flex', flexDirection: 'column', gap: 12, marginBottom: 16 }}>
        <Btn kind="primary" size="lg" full>
          <GoogleG />
          Continue with Google
        </Btn>
        <div style={{
          fontFamily: '"JetBrains Mono"', fontSize: 11, color: 'rgba(245,243,239,0.55)',
          letterSpacing: '0.04em', textAlign: 'center', marginTop: 4,
        }}>
          We read only what you let us read. Your data is yours.
        </div>
      </div>
    </div>
    <HomeIndicator tone="light" />
  </div>
);

const GoogleG = () => (
  <svg width="18" height="18" viewBox="0 0 18 18" style={{ marginRight: -2 }}>
    <path fill="#4285F4" d="M17.6 9.2c0-.6-.1-1.2-.2-1.7H9v3.3h4.8c-.2 1.1-.8 2-1.8 2.6v2.1h2.9c1.7-1.6 2.7-3.9 2.7-6.3z"/>
    <path fill="#34A853" d="M9 18c2.4 0 4.5-.8 6-2.2l-2.9-2.1c-.8.5-1.8.8-3.1.8-2.4 0-4.4-1.6-5.1-3.8H.9v2.4C2.4 16 5.4 18 9 18z"/>
    <path fill="#FBBC05" d="M3.9 10.7c-.2-.6-.3-1.2-.3-1.7s.1-1.1.3-1.7V4.9H.9C.3 6 0 7.5 0 9s.3 3 .9 4.1l3-2.4z"/>
    <path fill="#EA4335" d="M9 3.6c1.3 0 2.5.5 3.5 1.4l2.6-2.6C13.5.9 11.4 0 9 0 5.4 0 2.4 2 .9 4.9l3 2.4C4.6 5.1 6.6 3.6 9 3.6z"/>
  </svg>
);

const ScreenOnboardingConsent = () => (
  <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: '#FAF8F4' }}>
    <StatusBar tone="dark" />
    <div style={{ padding: '20px 24px 16px', display: 'flex', alignItems: 'center', gap: 12 }}>
      <Icon kind="back" size={22} />
      <div style={{ fontSize: 14, color: '#828A9C', fontFamily: '"JetBrains Mono"', letterSpacing: '0.04em' }}>
        STEP 2 OF 3
      </div>
    </div>

    <div style={{ padding: '8px 24px', flex: 1, overflowY: 'auto' }}>
      <div style={{
        fontFamily: '"Bricolage Grotesque"', fontSize: 32, fontWeight: 600, letterSpacing: '-0.025em',
        lineHeight: 1.1, marginBottom: 12, color: '#1F2A44',
      }}>
        Worksherpa would like to read your Gmail.
      </div>
      <div style={{ fontSize: 15, color: '#4B5468', lineHeight: 1.5, marginBottom: 24 }}>
        Just enough to find your customers. We don't send mail on your behalf yet — when we do, you'll approve every send.
      </div>

      <div style={{ background: '#fff', border: '1px solid #E5E1D8', borderRadius: 16, overflow: 'hidden', marginBottom: 16 }}>
        <Permission iconColor="#2F7D55" allowed title="Read messages and threads"
          body="We extract names, phone numbers, addresses, and what people asked for."/>
        <Permission iconColor="#2F7D55" allowed title="Read message metadata"
          body="Subject lines and timestamps so we can sort the timeline."/>
        <Permission iconColor="#828A9C" allowed={false} title="Send mail as you"
          body="Not yet. You'll approve every draft before it goes out."/>
        <Permission iconColor="#828A9C" allowed={false} title="Modify or delete mail"
          body="Worksherpa never deletes or moves your messages."/>
      </div>

      <div style={{
        background: '#FCE6CC', borderRadius: 12, padding: '12px 14px',
        display: 'flex', gap: 10, alignItems: 'flex-start', marginBottom: 16,
      }}>
        <Icon kind="info" size={18} color="#B5641A"/>
        <div style={{ fontSize: 13, color: '#7A4810', lineHeight: 1.4 }}>
          Your inbox stays in your account. We store extracted customer data only — never raw messages — and you can disconnect anytime.
        </div>
      </div>
    </div>

    <div style={{ padding: '12px 24px 28px', borderTop: '1px solid #E5E1D8', background: '#FAF8F4', display: 'flex', flexDirection: 'column', gap: 10 }}>
      <Btn kind="primary" size="lg" full>Allow & continue</Btn>
      <Btn kind="ghost" size="md" full>Not now</Btn>
    </div>
    <HomeIndicator tone="dark" />
  </div>
);

const Permission = ({ allowed, title, body, iconColor }) => (
  <div style={{
    display: 'flex', gap: 12, padding: '14px 16px', borderBottom: '1px solid #F0EEE7',
  }}>
    <div style={{
      width: 24, height: 24, borderRadius: 12, flexShrink: 0,
      background: allowed ? '#E1EFE6' : '#EDEAE2',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
    }}>
      <Icon kind={allowed ? 'check' : 'x'} size={14} color={iconColor} strokeWidth={2.5}/>
    </div>
    <div style={{ flex: 1 }}>
      <div style={{ fontSize: 14, fontWeight: 600, color: '#1F2A44', marginBottom: 2 }}>{title}</div>
      <div style={{ fontSize: 12.5, color: '#4B5468', lineHeight: 1.45 }}>{body}</div>
    </div>
  </div>
);

const ScreenOnboardingImporting = () => {
  const [pct, setPct] = React.useState(64);
  React.useEffect(() => {
    const t = setInterval(() => setPct((p) => (p >= 96 ? 64 : p + 2)), 280);
    return () => clearInterval(t);
  }, []);
  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: '#1F2A44', color: '#F5F3EF' }}>
      <StatusBar tone="light" />
      <div style={{ flex: 1, padding: '20px 28px 28px', display: 'flex', flexDirection: 'column' }}>
        <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
          <Logo size={56} />
          <div style={{
            fontFamily: '"Bricolage Grotesque"', fontSize: 30, fontWeight: 600,
            letterSpacing: '-0.025em', lineHeight: 1.1, marginTop: 24, color: '#F5F3EF',
          }}>
            Reading your last<br/>6 months of email.
          </div>
          <div style={{ fontSize: 15, color: 'rgba(245,243,239,0.7)', marginTop: 14, lineHeight: 1.45 }}>
            This takes a couple of minutes. You can keep working — we'll ping you when it's done.
          </div>

          <div style={{ marginTop: 32, marginBottom: 12 }}>
            <div style={{
              height: 8, background: 'rgba(245,243,239,0.12)', borderRadius: 4, overflow: 'hidden',
            }}>
              <div style={{ height: '100%', width: `${pct}%`, background: '#F08A24', borderRadius: 4, transition: 'width .3s' }}/>
            </div>
            <div style={{
              display: 'flex', justifyContent: 'space-between', marginTop: 8,
              fontFamily: '"JetBrains Mono"', fontSize: 11, color: 'rgba(245,243,239,0.6)', letterSpacing: '0.04em',
            }}>
              <span>READING THREAD {Math.round(pct * 24)} of 3,742</span>
              <span>{pct}%</span>
            </div>
          </div>

          <div style={{ marginTop: 28, display: 'flex', flexDirection: 'column', gap: 10 }}>
            <ImportRow done count="187" label="customers found" />
            <ImportRow done count="42" label="duplicates merged" />
            <ImportRow active count="11" label="addresses validated" />
            <ImportRow count="—" label="conversations classified" />
          </div>
        </div>

        <Btn kind="secondary" size="md" full
          style={{ background: 'transparent', color: '#F5F3EF', borderColor: 'rgba(245,243,239,0.3)' }}>
          Run in background
        </Btn>
      </div>
      <HomeIndicator tone="light" />
    </div>
  );
};

const ImportRow = ({ done, active, count, label }) => (
  <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
    <div style={{
      width: 22, height: 22, borderRadius: 11, flexShrink: 0,
      background: done ? '#2F7D55' : active ? 'rgba(240,138,36,0.2)' : 'rgba(245,243,239,0.08)',
      border: active ? '2px solid #F08A24' : 'none',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
    }}>
      {done && <Icon kind="check" size={12} color="#fff" strokeWidth={3}/>}
      {active && <div style={{ width: 8, height: 8, borderRadius: 4, background: '#F08A24' }}/>}
    </div>
    <div style={{
      fontFamily: '"Bricolage Grotesque"', fontSize: 22, fontWeight: 600,
      color: done || active ? '#F5F3EF' : 'rgba(245,243,239,0.4)',
      width: 56, letterSpacing: '-0.02em',
    }}>{count}</div>
    <div style={{ fontSize: 14, color: 'rgba(245,243,239,0.7)' }}>{label}</div>
  </div>
);

Object.assign(window, { ScreenOnboardingSignIn, ScreenOnboardingConsent, ScreenOnboardingImporting });
