// MailTwin hero mockup. Apple Mail window in the back showing both accounts
// (Work + Personal) so the user instantly understands the multi-account model;
// a realistic MailTwin assistant window — modeled directly off the actual app
// screenshots — floats on top.

const { useEffect, useState } = React;

// ── Inline icons matching the real app's stroke style ──
const I = {
  Chevron: () => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M9 6l6 6-6 6"/></svg>,
  Mic: () => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><rect x="9" y="3" width="6" height="12" rx="3"/><path d="M5 11a7 7 0 0 0 14 0M12 18v3M9 21h6"/></svg>,
  History: () => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M3 12a9 9 0 1 0 3-6.7L3 8"/><path d="M3 3v5h5"/><path d="M12 7v5l3 2"/></svg>,
  Caret: () => <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M6 9l6 6 6-6"/></svg>,
  Pin: () => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M12 17v5M9 4h6l-1 6 4 4H6l4-4z"/></svg>,
  Reply: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M9 17l-5-5 5-5M4 12h11a5 5 0 0 1 5 5v3"/></svg>,
  ReplyStyle: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><circle cx="12" cy="11" r="2.5"/><path d="M7 18a5 5 0 0 1 10 0"/></svg>,
  Summarize: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M4 7h12M4 12h16M4 17h10"/></svg>,
  Actions: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="6" cy="7" r="2"/><path d="M11 7h9"/><circle cx="6" cy="17" r="2"/><path d="M11 17h9"/></svg>,
  Translate: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M7 8h6M9 7v1.5c0 2.5-1.5 4.5-3 5M8 11c1 1.5 3 2.5 5 3M14 17l3-7 3 7M15 15h4"/></svg>,
  Improve: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M14 4l3 3-9 9-4 1 1-4z"/><path d="M19 9l1 2 2 1-2 1-1 2-1-2-2-1 2-1z"/></svg>,
  Plus: () => <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M12 5v14M5 12h14"/></svg>,
  Tone: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"><path d="M5 8v8M9 5v14M13 9v6M17 7v10M21 11v2"/></svg>,
  Length: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="7" width="18" height="10" rx="1.5"/><path d="M3 11h18M7 11v3M11 11v3M15 11v3M19 11v3"/></svg>,
  Insert: () => <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3v10M8 9l4 4 4-4M5 21h14"/></svg>,
  Refresh: () => <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M4 12a8 8 0 0 1 14-5l2-2v6h-6l2-2"/><path d="M20 12a8 8 0 0 1-14 5l-2 2v-6h6l-2 2"/></svg>,
  Copy: () => <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"/></svg>,
  Inspect: () => <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><rect x="4" y="4" width="16" height="16" rx="2"/><path d="M8 10h6M8 14h4"/></svg>,
  Sparkle: () => <svg width="13" height="13" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l1.5 5L19 8.5 13.5 10 12 15l-1.5-5L5 8.5 10.5 7z"/></svg>,
  Gear: () => <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1A1.7 1.7 0 0 0 4.6 9a1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8v0a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></svg>,
  Mailbox: () => <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" strokeWidth="1.6"><path d="M3 7l9 6 9-6"/><rect x="3" y="5" width="18" height="14" rx="2"/></svg>,
  Flag: () => <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M5 3v18M5 4h12l-2 4 2 4H5"/></svg>,
  Clock: () => <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>,
  Search: () => <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="11" cy="11" r="7"/><path d="M16 16l4 4"/></svg>,
};

function Dot({ c, size = 12 }) {
  return <span style={{ width: size, height: size, borderRadius: 999, background: c, display: 'inline-block', flexShrink: 0 }} />;
}

function Chip({ icon, label, active, onClick }) {
  return (
    <button onClick={onClick} style={{
      display:'inline-flex', alignItems:'center', gap: 7,
      padding:'8px 12px', borderRadius: 9,
      background: active ? '#E6F0FE' : 'white',
      border: `1px solid ${active ? '#B6D2F8' : 'rgba(0,0,0,0.1)'}`,
      color: active ? '#1166DA' : '#1d1d1f',
      fontSize: 13.5, fontWeight: 500,
      cursor:'pointer', transition:'all .15s ease',
      fontFamily: 'inherit',
    }}>
      <span style={{ display:'inline-flex' }}>{icon}</span>
      {label}
    </button>
  );
}

function MiniChip({ icon, label }) {
  return (
    <button style={{
      display:'inline-flex', alignItems:'center', gap: 5,
      padding:'4px 8px', borderRadius: 6,
      background: 'transparent', border: 'none',
      color:'#3a3a3c', fontSize: 12, fontWeight: 500,
      cursor:'pointer', fontFamily: 'inherit',
    }}>
      {icon}
      {label}
    </button>
  );
}

// ── Mail.app row ──
function MailRow({ from, subject, preview, time, unread, selected, accentColor }) {
  return (
    <div style={{
      padding: '11px 14px 11px 22px',
      borderBottom: '1px solid rgba(0,0,0,0.05)',
      background: selected ? 'linear-gradient(180deg, #b58af0, #8d6cd9)' : 'transparent',
      color: selected ? 'white' : 'inherit',
      position: 'relative',
      cursor: 'pointer',
    }}>
      {unread && !selected && <span style={{ position:'absolute', left: 8, top: 21, width: 8, height: 8, borderRadius: 999, background: accentColor || '#3b82f6' }} />}
      <div style={{ display:'flex', justifyContent:'space-between', alignItems:'baseline', gap: 8 }}>
        <div style={{ fontSize: 13, fontWeight: 600, whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis' }}>{from}</div>
        <div style={{ fontSize: 11, opacity: selected ? 0.85 : 0.5, whiteSpace:'nowrap' }}>{time}</div>
      </div>
      <div style={{ fontSize: 12.5, fontWeight: 500, marginTop: 1, whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis' }}>{subject}</div>
      <div style={{ fontSize: 11.5, opacity: selected ? 0.85 : 0.55, marginTop: 1, whiteSpace:'nowrap', overflow:'hidden', textOverflow:'ellipsis' }}>{preview}</div>
    </div>
  );
}

function SidebarItem({ label, count, icon, active, indent }) {
  return (
    <div style={{
      display:'flex', alignItems:'center', gap: 8,
      padding:`5px ${indent ? 20 : 10}px`,
      borderRadius: 6,
      background: active ? 'rgba(0,0,0,0.07)' : 'transparent',
      fontSize: 12.5, fontWeight: active ? 600 : 500,
      color: '#2a2434',
    }}>
      <span style={{ width: 16, color: '#7B7BE8', display:'inline-flex', justifyContent:'center' }}>{icon}</span>
      <span style={{ flex: 1 }}>{label}</span>
      {count !== undefined && <span style={{ fontSize: 11, opacity: 0.5, fontWeight: 500 }}>{count}</span>}
    </div>
  );
}

// ── Mail.app backdrop window ──
function MailBackdrop({ account }) {
  // Each account has its own selected message + a list of inbox messages
  const WORK_INBOX = [
    { from: "Anya Pereira", subject: "Q2 partnership review — schedule", preview: "Hi Jordan, ahead of next week's review I've pulled together…", time: "9:14 AM", unread: true, selected: account === 'business' },
    { from: "Stripe", subject: "Your weekly summary", preview: "$18,420 in net volume this week — up 12% vs last week…", time: "8:02 AM" },
    { from: "Linear", subject: "3 issues assigned to you", preview: "ENG-1184 Fix attachment race in CGEvent paste pipeline…", time: "Yesterday", unread: true },
    { from: "Marcus Lin", subject: "contract redlines", preview: "Took a pass at the MSA. Most of it is fine — flagged a few…", time: "Yesterday" },
    { from: "Apple Developer", subject: "Notarization successful", preview: "Your submission MailAI-1.4.2.dmg has been notarized.", time: "Tue" },
    { from: "Calendly", subject: "New event: Intro call w/ Priya", preview: "Confirmed — Thursday 11:00 AM PT, 30 min, video…", time: "Tue" },
    { from: "GitHub", subject: "[smilodon/mailtwin] PR #248 ready", preview: "dyemenchuk approved your pull request…", time: "Mon" },
  ];

  const PERSONAL_INBOX = [
    { from: "Sam", subject: "movie thursday??", preview: "hey — saw the new Villeneuve is finally out. wanna do…", time: "8:42 AM", unread: true, selected: account === 'personal' },
    { from: "Mom", subject: "recipe for that thing", preview: "Hi sweetie, here's the lasagna one finally. Don't skip…", time: "7:30 AM" },
    { from: "Letterboxd", subject: "Your week in film", preview: "You logged 4 films this week. Top rated: Perfect Days (5★)…", time: "Yesterday", unread: true },
    { from: "Resy", subject: "Your reservation is confirmed", preview: "Tuesday, 7:30 PM at Lilia for 2. Cancel up to 4 hours…", time: "Yesterday" },
    { from: "Dad", subject: "bike trip — labor day weekend", preview: "So I was looking at that loop near Point Reyes — about 80mi…", time: "Mon" },
    { from: "Spotify", subject: "Your 2026 Wrapped is here", preview: "You listened to 47,213 minutes this year. Top artist: Caroline…", time: "Mon" },
    { from: "The Cabin", subject: "rebooking june", preview: "Hey — gentle nudge on the june dates. We've got the back…", time: "Sat" },
  ];

  // Use the account that's currently active for the message list
  const inbox = account === 'business' ? WORK_INBOX : PERSONAL_INBOX;
  const selected = inbox.find(m => m.selected) || inbox[0];

  return (
    <div style={{
      width: 1280, borderRadius: 14, overflow:'hidden',
      background:'#F5F2F8', border:'1px solid rgba(0,0,0,0.1)',
      boxShadow:'0 30px 80px -20px rgba(50, 30, 100, 0.25), 0 12px 24px -8px rgba(50, 30, 100, 0.15)',
      fontFamily: '-apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif',
    }}>
      {/* Title bar */}
      <div style={{
        height: 38, padding:'0 14px',
        display:'flex', alignItems:'center', gap: 8,
        background: 'linear-gradient(180deg, #ECE6F2, #DCD3E8)',
        borderBottom:'1px solid rgba(0,0,0,0.08)',
      }}>
        <Dot c="#FF5F57" /><Dot c="#FEBC2E" /><Dot c="#28C840" />
        <div style={{ flex: 1 }} />
        <div style={{
          width: 320, height: 22, borderRadius: 6,
          background:'rgba(255,255,255,0.7)', border:'1px solid rgba(0,0,0,0.08)',
          fontSize: 11, color:'#9b94a8', display:'flex', alignItems:'center', gap: 6, padding:'0 8px',
        }}>
          <I.Search /> Search Mailbox
        </div>
        <div style={{ flex: 1 }} />
        <div style={{ display:'flex', gap: 14, color:'#7a7286' }}>
          {['M7 12h10M12 7v10','M3 6h18M3 12h18M3 18h18','M5 12h14M12 5l7 7-7 7'].map((d, i) => (
            <svg key={i} width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"><path d={d}/></svg>
          ))}
        </div>
      </div>

      {/* Three-pane layout */}
      <div style={{ display:'grid', gridTemplateColumns:'220px 340px 1fr', height: 642 }}>

        {/* Sidebar — both accounts visible, current one highlighted */}
        <div style={{ background:'#EFEAF4', borderRight:'1px solid rgba(0,0,0,0.06)', padding:'14px 8px', overflow:'hidden' }}>
          <div style={{ fontSize: 10, color:'#9b94a8', textTransform:'uppercase', letterSpacing:'.08em', padding:'0 10px 6px', fontWeight: 600 }}>Favorites</div>
          <SidebarItem icon={<I.Mailbox />} label="All Inboxes" count={428} active />
          <SidebarItem icon={<I.Flag />} label="Flagged" count={12} />
          <SidebarItem icon={<I.Clock />} label="Today" count={31} />

          <div style={{ fontSize: 10, color:'#9b94a8', textTransform:'uppercase', letterSpacing:'.08em', padding:'16px 10px 6px', fontWeight: 600 }}>Accounts</div>

          {/* Work account */}
          <SidebarItem
            icon={<span style={{ width: 9, height: 9, borderRadius: 999, background:'#7B7BE8', display:'inline-block' }} />}
            label="Work"
            active={account === 'business'}
          />
          <div style={{ paddingLeft: 4 }}>
            <SidebarItem indent label="Inbox" count={31} />
            <SidebarItem indent label="Sent" />
            <SidebarItem indent label="Drafts" count={2} />
          </div>

          {/* Personal account */}
          <div style={{ marginTop: 6 }}>
            <SidebarItem
              icon={<span style={{ width: 9, height: 9, borderRadius: 999, background:'#E879B5', display:'inline-block' }} />}
              label="Personal"
              active={account === 'personal'}
            />
            <div style={{ paddingLeft: 4 }}>
              <SidebarItem indent label="Inbox" count={14} />
              <SidebarItem indent label="Sent" />
              <SidebarItem indent label="Archive" />
            </div>
          </div>
        </div>

        {/* Message list */}
        <div style={{ background:'#FFFFFF', borderRight:'1px solid rgba(0,0,0,0.06)', overflow:'hidden' }}>
          <div style={{
            padding:'10px 14px',
            display:'flex', justifyContent:'space-between', alignItems:'center',
            borderBottom:'1px solid rgba(0,0,0,0.05)',
            fontSize: 11, color:'#9b94a8',
          }}>
            <span style={{ display:'inline-flex', alignItems:'center', gap: 6 }}>
              <span style={{ width: 7, height: 7, borderRadius: 999, background: account === 'business' ? '#7B7BE8' : '#E879B5' }} />
              <span style={{ fontWeight: 600, color:'#2a2434' }}>{account === 'business' ? 'Work' : 'Personal'}</span> · Inbox
            </span>
            <span>By Date ▾</span>
          </div>
          {inbox.map((m, i) => (
            <MailRow key={i} {...m} accentColor={account === 'business' ? '#7B7BE8' : '#E879B5'} />
          ))}
        </div>

        {/* Reading pane */}
        <div style={{ background:'#FFFFFF', display:'flex', flexDirection:'column', overflow:'hidden' }}>
          <div style={{ padding:'18px 22px 14px', borderBottom:'1px solid rgba(0,0,0,0.05)' }}>
            <div style={{ fontSize: 17, fontWeight: 600, letterSpacing:'-0.01em', color:'#1d1d1f' }}>{selected.subject}</div>
            <div style={{ display:'flex', alignItems:'center', gap: 10, marginTop: 10 }}>
              <div style={{
                width: 30, height: 30, borderRadius: 999,
                background: account === 'business'
                  ? 'linear-gradient(135deg,#7B7BE8,#6BA0F0)'
                  : 'linear-gradient(135deg,#E879B5,#C26FD9)',
                color:'white', display:'flex', alignItems:'center', justifyContent:'center', fontWeight: 600, fontSize: 12,
              }}>{selected.from[0]}</div>
              <div style={{ flex: 1 }}>
                <div style={{ fontSize: 13, fontWeight: 600, color:'#1d1d1f' }}>{selected.from}</div>
                <div style={{ fontSize: 11.5, color:'#9b94a8', marginTop: 1 }}>To: {account === 'business' ? 'jordan@horizonlabs.io' : 'jordan@me.com'} · 9:14 AM</div>
              </div>
            </div>
          </div>
          <div style={{ padding:'18px 22px', fontSize: 13, color:'#3b3447', lineHeight: 1.6, flex: 1, overflow:'hidden' }}>
            {account === 'business' ? (
              <>
                <p style={{ margin: 0 }}>Hi Jordan,</p>
                <p style={{ marginTop: 10 }}>Ahead of next week's review I've pulled together the Q2 numbers across both pipelines. Headline: activation lift held at <b>+18%</b> after the rework, and we're trending to beat the H1 target by ~9%.</p>
                <p style={{ marginTop: 10 }}>I'd love to walk you through the deck live — easier to talk through the cohort framing in person. Can we lock something in for early next week? <b>Tuesday 2pm PT</b> or <b>Wed 10am PT</b> both work for me.</p>
                <p style={{ marginTop: 10 }}>Anya</p>
              </>
            ) : (
              <>
                <p style={{ margin: 0 }}>hey —</p>
                <p style={{ marginTop: 10 }}>saw the new Villeneuve is finally out. wanna do thursday after work? could grab dinner at that ramen place on 14th first if you're hungry.</p>
                <p style={{ marginTop: 10 }}>also down for the imax one if it's playing somewhere not insane to get to. lmk what works 🙂</p>
                <p style={{ marginTop: 10 }}>x s</p>
              </>
            )}
          </div>
        </div>
      </div>
    </div>
  );
}

// ── MailTwin assistant window (the real app) ──
function AssistantWindow({ account, streaming }) {
  const SCRIPTS = {
    business: {
      sender: 'anya@horizonlabs.io',
      subject: 'Q2 partnership review — schedule',
      reply: [
        "Hi Anya,\n\n",
        "Thanks for pulling the Q2 numbers together — the conversion lift on the activation flow is genuinely impressive.\n\n",
        "Tuesday at 2pm PT works on my end. I'll have Mark join so we can walk through the pipeline assumptions live. Could you share the deck a day prior so I can pre-read?\n\n",
        "One quick ask: it would help to see the Q1 retention cohort alongside, so we're comparing on the same axis.\n\n",
        "Best,\nJordan",
      ],
    },
    personal: {
      sender: 'sam.k@me.com',
      subject: 'movie thursday??',
      reply: [
        "yes please 🎬\n\n",
        "thursday works — anything but the 3hr one. that new villeneuve is supposed to be wild though, dealer's choice.\n\n",
        "want me to grab tickets? i can do the 7:40 at the alamo if it's still got seats.\n\n",
        "x j",
      ],
    },
  };

  const script = SCRIPTS[account];
  const fullText = script.reply.join('');
  const [text, setText] = useState(streaming ? '' : fullText);
  const [done, setDone] = useState(!streaming);
  const [activeAction, setActiveAction] = useState(1);

  useEffect(() => {
    if (!streaming) { setText(fullText); setDone(true); return; }
    setText(''); setDone(false); setActiveAction(1);
    let i = 0;
    const speeds = [12, 14, 16, 18, 22, 28];
    let timer;
    const tick = () => {
      i += Math.floor(Math.random() * 3) + 1;
      if (i >= fullText.length) { setText(fullText); setDone(true); return; }
      setText(fullText.slice(0, i));
      timer = setTimeout(tick, speeds[Math.floor(Math.random() * speeds.length)]);
    };
    timer = setTimeout(tick, 700);
    return () => clearTimeout(timer);
  }, [account, streaming]);

  return (
    <div style={{
      width: 760,
      borderRadius: 12, overflow: 'hidden',
      background: 'white',
      border: '1px solid rgba(0,0,0,0.08)',
      boxShadow: '0 50px 90px -20px rgba(50, 30, 100, 0.45), 0 25px 40px -12px rgba(123, 99, 200, 0.35)',
      display:'flex', flexDirection:'column',
      fontFamily: '-apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif',
      color:'#1d1d1f',
    }}>
      {/* Title bar */}
      <div style={{
        height: 36, padding:'0 14px',
        display:'flex', alignItems:'center', gap: 8,
        background:'#FBFBFD',
        borderBottom:'1px solid rgba(0,0,0,0.04)',
        flexShrink: 0,
      }}>
        <Dot c="#C7C7CC" /><Dot c="#C7C7CC" /><Dot c="#C7C7CC" />
      </div>

      {/* Sender pill */}
      <div style={{
        margin:'16px 18px 0',
        padding:'9px 13px',
        background:'#F4F4F6',
        borderRadius: 8,
        display:'flex', alignItems:'center', gap: 8,
        fontSize: 13.5,
      }}>
        <Dot c="#0A84FF" size={8} />
        <span style={{ fontWeight: 600 }}>{script.sender}</span>
        <span style={{ color:'#8E8E93', margin:'0 4px' }}>·</span>
        <span style={{ color:'#3A3A3C' }}>{script.subject}</span>
        <div style={{ flex: 1 }} />
        <span style={{ color:'#8E8E93' }}><I.Pin /></span>
      </div>

      {/* Steer prompt */}
      <div style={{
        margin:'12px 18px 0',
        padding:'11px 13px',
        border:'1px solid rgba(0,0,0,0.1)',
        borderRadius: 9,
        display:'flex', alignItems:'center', gap: 10,
        fontSize: 13.5,
        background:'white',
      }}>
        <span style={{ color:'#8E8E93' }}><I.Chevron /></span>
        <span style={{ color:'#9A9AA0', flex: 1 }}>Steer the reply (optional) — push back, ask for a date, decline politely…</span>
        <span style={{ color:'#8E8E93' }}><I.Mic /></span>
        <span style={{ color:'#8E8E93', display:'inline-flex', alignItems:'center', gap: 2 }}><I.History /><I.Caret /></span>
      </div>

      {/* Action chips */}
      <div style={{ margin:'10px 18px 0', display:'flex', gap: 7, flexWrap:'wrap', alignItems:'center' }}>
        <Chip icon={<I.Reply />}      label="Reply"            active={activeAction === 0} onClick={() => setActiveAction(0)} />
        <Chip icon={<I.ReplyStyle />} label="Reply (my style)" active={activeAction === 1} onClick={() => setActiveAction(1)} />
        <Chip icon={<I.Summarize />}  label="Summarize"        active={activeAction === 2} onClick={() => setActiveAction(2)} />
        <Chip icon={<I.Actions />}    label="Action items"     active={activeAction === 3} onClick={() => setActiveAction(3)} />
        <Chip icon={<I.Translate />}  label="Translate"        active={activeAction === 4} onClick={() => setActiveAction(4)} />
        <Chip icon={<I.Improve />}    label="Improve draft"    active={activeAction === 5} onClick={() => setActiveAction(5)} />
        <button style={{
          display:'inline-flex', alignItems:'center', justifyContent:'center',
          width: 30, height: 30, borderRadius: 8,
          background:'white', border:'1px solid rgba(0,0,0,0.1)',
          color:'#1d1d1f', cursor:'pointer',
        }}><I.Plus /></button>
      </div>

      {/* Tone / Length */}
      <div style={{ margin:'8px 18px 0', display:'flex', gap: 6 }}>
        <MiniChip icon={<I.Tone />} label="Tone" />
        <MiniChip icon={<I.Length />} label="Length" />
      </div>

      {/* Output */}
      <div style={{
        margin:'12px 18px 0',
        background:'#F6F6F8',
        borderRadius: 10,
        padding:'18px 20px',
        fontSize: 13.5, lineHeight: 1.55,
        color:'#1d1d1f',
        whiteSpace:'pre-wrap',
        minHeight: 200,
        flex: 1,
      }}>
        {text === '' && !done ? (
          <div style={{ display:'flex', alignItems:'center', gap: 8, color:'#8E8E93' }}>
            <Dot c="#0A84FF" size={6} />
            <span>Run an action to see the result here.</span>
          </div>
        ) : (
          <>
            {text}
            {!done && <span style={{
              display:'inline-block', width: 7, height: 16, marginLeft: 1,
              background:'#1d1d1f', verticalAlign:'middle',
              animation:'mailtwin-blink 0.9s steps(2) infinite', borderRadius: 1,
            }} />}
          </>
        )}
      </div>

      {/* Insert bar */}
      <div style={{
        margin:'14px 18px 0',
        display: text === '' ? 'none' : 'flex',
        gap: 7,
        alignItems:'center',
      }}>
        <button style={{
          flex: 1, height: 50, borderRadius: 13, border:'none',
          background:'linear-gradient(90deg, #F49AB8 0%, #C57FE0 50%, #7B7BE8 100%)',
          color:'white', fontSize: 14, fontWeight: 600,
          display:'inline-flex', alignItems:'center', justifyContent:'center', gap: 9,
          cursor:'pointer',
          boxShadow:'0 6px 20px -6px rgba(155, 111, 224, 0.55), inset 0 1px 0 rgba(255,255,255,0.3)',
          fontFamily: 'inherit',
        }}>
          <I.Insert /> Insert reply into Mail
        </button>
        {[<I.Refresh />, <I.Copy />, <I.Inspect />].map((ic, i) => (
          <button key={i} style={{
            width: 38, height: 38, borderRadius: 10,
            background:'white', border:'1px solid rgba(0,0,0,0.1)',
            color:'#3A3A3C', cursor:'pointer',
            display:'inline-flex', alignItems:'center', justifyContent:'center',
          }}>{ic}</button>
        ))}
      </div>

      {/* Status footer */}
      <div style={{
        marginTop: 12,
        padding:'12px 18px',
        display:'flex', alignItems:'center', gap: 10,
        fontSize: 12.5,
        borderTop:'1px solid rgba(0,0,0,0.04)',
      }}>
        <span style={{ display:'inline-flex', alignItems:'center', gap: 6 }}>
          <span style={{ color:'#1d1d1f' }}><I.Sparkle /></span>
          <span style={{ color:'#1d1d1f', fontWeight: 500 }}>Claude</span>
        </span>
        <span style={{ display:'inline-flex', alignItems:'center', gap: 6, color:'#3A3A3C' }}>
          <Dot c="#34C759" size={7} />
          ready
        </span>
        <div style={{ flex: 1 }} />
        <span style={{
          display:'inline-flex', alignItems:'center', gap: 5,
          padding:'4px 9px', borderRadius: 999,
          background:'#E8F8EC', color:'#0F8A2E',
          fontSize: 11.5, fontWeight: 500,
        }}>
          <span style={{ color:'#0F8A2E' }}>✦</span> 14d trial
        </span>
        <span style={{ color:'#8E8E93' }}><I.Gear /></span>
      </div>
    </div>
  );
}

// ── Composite: Mail.app backdrop + assistant overlay ──
function MailMockup({ scale = 1, streaming = true, account = 'business' }) {
  const stageW = 1280;
  const stageH = 720;

  return (
    <div style={{
      width: stageW, height: stageH,
      transformOrigin: 'top left',
      transform: `scale(${scale})`,
      position: 'relative',
    }}>
      {/* Mail.app in the back */}
      <div style={{ position:'absolute', top: 0, left: 0 }}>
        <MailBackdrop account={account} />
      </div>

      {/* MailTwin on top — positioned bottom-right, overlapping the reading pane */}
      <div style={{ position:'absolute', right: 30, bottom: -10, zIndex: 2 }}>
        <AssistantWindow account={account} streaming={streaming} />
      </div>
    </div>
  );
}

if (typeof document !== 'undefined' && !document.getElementById('mailtwin-kf')) {
  const s = document.createElement('style');
  s.id = 'mailtwin-kf';
  s.textContent = `@keyframes mailtwin-blink { 0%,49%{opacity:1} 50%,100%{opacity:0} }`;
  document.head.appendChild(s);
}

window.MailMockup = MailMockup;
