/* global React, INK, CREAM, PULSE, GENS, Sparkle, GradText,
   Reveal, useReveal, useCountUp, SectionHead, ControlPill, GenBtn, SectionShell, SB_EASE */
// § 05 Copilot · § 06 Scheduler

const { useState: csS, useEffect: csE } = React;

// ---------- § 05 — Copilot ----------
const Msg = ({ children, me = false, show, delay = 0, pad = true }) => (
  <div style={{ display: 'flex', justifyContent: me ? 'flex-end' : 'flex-start',
    opacity: show ? 1 : 0, transform: show ? 'none' : 'translateY(14px)',
    transition: `opacity 0.5s ${SB_EASE} ${delay}ms, transform 0.5s ${SB_EASE} ${delay}ms` }}>
    <div style={{ maxWidth: '82%', borderRadius: me ? '16px 16px 4px 16px' : '4px 16px 16px 16px',
      background: me ? INK : 'rgb(246,243,242)', color: me ? '#fff' : INK,
      boxShadow: me ? 'none' : 'inset 0 0 0 1px rgba(28,27,27,0.06)',
      padding: pad ? '11px 15px' : 0, overflow: 'hidden', fontSize: 13.5, lineHeight: 1.5 }}>{children}</div>
  </div>
);

const CopilotSection = () => {
  const [ref, on] = useReveal(0.3);
  const [step, setStep] = csS(0);
  csE(() => {
    if (!on) return;
    const times = [400, 1300, 2300, 3400];
    const ts = times.map((t, i) => setTimeout(() => setStep(i + 1), t));
    return () => ts.forEach(clearTimeout);
  }, [on]);
  return (
    <SectionShell bg="#fff">
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 0.95fr', gap: 72, alignItems: 'center' }}>
        <CopyHead k="copilot" />
        <Reveal delay={160}>
          <div ref={ref} style={{ borderRadius: 20, background: CREAM, boxShadow: '0 20px 40px rgba(28,27,27,0.10), inset 0 0 0 1px rgba(28,27,27,0.08)', overflow: 'hidden' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 9, padding: '13px 18px', borderBottom: '1px solid rgba(28,27,27,0.08)', background: 'rgb(252,248,248)' }}>
              <span style={{ width: 22, height: 22, borderRadius: 9999, background: PULSE, display: 'grid', placeItems: 'center', fontSize: 11, color: '#fff' }}>✦</span>
              <span style={{ fontSize: 13.5, fontWeight: 700, color: INK }}>Copilot</span>
              <span style={{ fontSize: 8.5, fontWeight: 700, color: '#fff', background: 'rgb(129,60,237)', borderRadius: 9999, padding: '2px 7px' }}>NEW</span>
              <span style={{ marginLeft: 'auto', display: 'inline-flex', alignItems: 'center', gap: 6, fontSize: 11, fontWeight: 600, color: 'var(--text-secondary)' }}>
                <span style={{ width: 15, height: 15, borderRadius: '50%', backgroundImage: `url(${GENS[0].src})`, backgroundSize: 'cover', backgroundPosition: 'center 15%' }} />
                Scarlett
              </span>
            </div>
            <div style={{ padding: 18, display: 'flex', flexDirection: 'column', gap: 12 }}>
              <Msg me show={step >= 1}>What's going viral right now?</Msg>
              <Msg show={step >= 2}>Slow push-in reels are outperforming this week — up 3.1× for creators like you. Here's a brief:</Msg>
              <Msg show={step >= 3} pad={false} delay={100}>
                <div style={{ width: 300 }}>
                  <div style={{ display: 'flex', gap: 12, padding: 12 }}>
                    <div style={{ width: 74, height: 94, borderRadius: 10, overflow: 'hidden', flexShrink: 0,
                      backgroundImage: `url(${GENS[1].src})`, backgroundSize: 'cover', backgroundPosition: 'center 15%' }} />
                    <div style={{ minWidth: 0 }}>
                      <div style={{ fontSize: 12.5, fontWeight: 700, color: INK, marginBottom: 4 }}>Golden hour · slow push-in</div>
                      <div style={{ fontSize: 11.5, color: 'var(--text-secondary)', lineHeight: 1.45, marginBottom: 8 }}>Backlit field, hair in the breeze, they turn to camera.</div>
                      <div style={{ display: 'flex', gap: 6 }}>
                        {['9:16', '5s', '1K'].map(c => (
                          <span key={c} style={{ fontSize: 10, fontWeight: 600, color: 'rgb(28,27,27)', background: '#fff',
                            boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.1)', borderRadius: 9999, padding: '3px 8px' }}>{c}</span>
                        ))}
                      </div>
                    </div>
                  </div>
                  <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '10px 12px', borderTop: '1px solid rgba(28,27,27,0.07)' }}>
                    <span style={{ fontSize: 10.5, color: 'var(--text-tertiary)' }}>Option 1 / 3</span>
                    <GenBtn active count={1} style={{ height: 34, borderRadius: 10, padding: '0 13px' }} />
                  </div>
                </div>
              </Msg>
              <Msg show={step >= 4} delay={100}>
                <span style={{ display: 'inline-flex', alignItems: 'center', gap: 7 }}>
                  <svg width="13" height="13" viewBox="0 0 16 16" fill="none"><circle cx="8" cy="8" r="7" fill="#22C55E"/><path d="M5 8.2l2 2 4-4.4" stroke="#fff" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"/></svg>
                  Generated & scheduled — Fanvue, tomorrow 9:00 AM.
                </span>
              </Msg>
            </div>
            <div style={{ display: 'flex', gap: 7, padding: '0 18px 16px', flexWrap: 'wrap' }}>
              {['Create a photo for social', 'Find reels to clone', 'Plan my week'].map(q => (
                <span key={q} style={{ fontSize: 11, fontWeight: 600, color: 'var(--brand-purple-700)', background: 'var(--brand-purple-50)',
                  border: '1px solid var(--brand-purple-200)', borderRadius: 9999, padding: '6px 12px', cursor: 'pointer' }}>{q}</span>
              ))}
            </div>
          </div>
        </Reveal>
      </div>
    </SectionShell>
  );
};

// ---------- § 06 — Scheduler ----------
const SCHED_PLATS = [
  { n: 'Instagram', c: '#E1306C', onn: true },
  { n: 'TikTok', c: '#1C1B1B', onn: true },
  { n: 'X', c: '#1C1B1B', onn: false },
  { n: 'Fanvue', c: '#1FB85A', onn: true },
  { n: 'Fansly', c: '#2699F7', onn: false },
];

const KPI = ({ label, value, suffix = '', on, delay = 0 }) => {
  const v = useCountUp(value, on);
  return (
    <div style={{ borderRadius: 14, background: '#fff', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.08)', padding: '14px 16px' }}>
      <div style={{ fontSize: 10.5, fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--text-tertiary)', marginBottom: 6 }}>{label}</div>
      <div style={{ fontSize: 24, fontWeight: 800, letterSpacing: '-0.02em', color: INK, fontVariantNumeric: 'tabular-nums' }}>
        {v >= 1000000 ? (v / 1000000).toFixed(1) + 'M' : v >= 1000 ? (v / 1000).toFixed(v >= 100000 ? 0 : 1) + 'k' : v}{suffix}
      </div>
    </div>
  );
};

const SchedSection = () => {
  const [ref, on] = useReveal(0.3);
  return (
    <SectionShell>
      <div ref={ref}>
        <CopyHead center k="sched" />
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 0.9fr', gap: 26, marginTop: 52, alignItems: 'start' }}>
          {/* composer — the real Create Post modal */}
          <Reveal delay={140}>
            <div style={{ borderRadius: 20, background: '#fff', boxShadow: '0 24px 48px rgba(28,27,27,0.12), inset 0 0 0 1px rgba(28,27,27,0.08)', overflow: 'hidden',
              transform: 'perspective(1400px) rotateX(1.5deg) rotate(1.6deg)', transformOrigin: '50% 40%' }}>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '14px 18px', borderBottom: '1px solid rgba(28,27,27,0.08)' }}>
                <span style={{ fontSize: 15, fontWeight: 800, color: INK }}>Create Post</span>
                <span style={{ width: 26, height: 26, borderRadius: '50%', background: 'rgb(246,243,242)', display: 'grid', placeItems: 'center', cursor: 'pointer' }}>
                  <svg width="10" height="10" viewBox="0 0 16 16" fill="none"><path d="M4 4l8 8M12 4l-8 8" stroke="rgba(28,27,27,0.6)" strokeWidth="1.8" strokeLinecap="round"/></svg>
                </span>
              </div>
              <div style={{ padding: '14px 18px 16px' }}>
                <div style={{ fontSize: 10, fontWeight: 700, letterSpacing: '0.1em', textTransform: 'uppercase', color: 'var(--text-mute)', marginBottom: 8 }}>Post to</div>
                <div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', marginBottom: 3 }}>
                  {[['Twitter/X', '#5B6470', false], ['Instagram', '#E1306C', true], ['Facebook', '#1877F2', false], ['TikTok', '#1C1B1B', false], ['Reddit', '#FF4500', false], ['Fanvue', '#1FB85A', false]].map(([n, c, on]) => (
                    <span key={n} style={{ display: 'inline-flex', alignItems: 'center', gap: 5, borderRadius: 9999, padding: '6px 12px', fontSize: 11, fontWeight: on ? 700 : 600, cursor: 'pointer',
                      boxShadow: on ? 'inset 0 0 0 1.5px var(--brand-purple-500)' : 'inset 0 0 0 1px rgba(28,27,27,0.1)',
                      color: on ? INK : 'rgba(28,27,27,0.45)' }}>
                      <span style={{ width: 7, height: 7, borderRadius: '50%', background: c, opacity: on ? 1 : 0.4 }} />{n}
                    </span>
                  ))}
                </div>
                <div style={{ fontSize: 10, color: 'var(--text-mute)', marginBottom: 12 }}>@sozee.ai</div>
                <div style={{ borderRadius: 14, background: 'rgb(250,248,247)', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.08)', padding: '12px 14px', marginBottom: 12 }}>
                  <div style={{ fontSize: 12.5, lineHeight: 1.5, color: INK, minHeight: 38 }}>poolside, golden light, zero meetings. new set live now ↗</div>
                  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5, marginTop: 6, fontSize: 11, fontWeight: 700,
                    color: 'var(--brand-purple-700)', cursor: 'pointer' }}>✦ Rewrite in my voice</span>
                </div>
                <div style={{ display: 'flex', alignItems: 'center', gap: 7, marginBottom: 12 }}>
                  <span style={{ fontSize: 10, fontWeight: 700, letterSpacing: '0.1em', textTransform: 'uppercase', color: 'var(--text-mute)', marginRight: 3 }}>Format</span>
                  {[['Photo', true], ['Carousel', false], ['Reel', false], ['Story', false]].map(([f, on]) => (
                    <span key={f} style={{ borderRadius: 9999, padding: '5px 12px', fontSize: 11, fontWeight: on ? 700 : 600, cursor: 'pointer',
                      background: on ? '#E1306C' : 'rgb(246,243,242)', color: on ? '#fff' : 'rgba(28,27,27,0.6)',
                      boxShadow: on ? 'none' : 'inset 0 0 0 1px rgba(28,27,27,0.08)' }}>{f}</span>
                  ))}
                </div>
                <div style={{ display: 'flex', alignItems: 'center', gap: 7, marginBottom: 8 }}>
                  <span style={{ fontSize: 10, fontWeight: 700, letterSpacing: '0.1em', textTransform: 'uppercase', color: 'var(--text-mute)' }}>Media</span>
                  <span style={{ marginLeft: 'auto', borderRadius: 9999, padding: '6px 13px', fontSize: 11, fontWeight: 600, color: 'rgba(28,27,27,0.7)',
                    boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.12)', cursor: 'pointer' }}>↑ Upload</span>
                  <span style={{ borderRadius: 9999, padding: '6px 13px', fontSize: 11, fontWeight: 700, color: 'var(--brand-purple-700)',
                    background: 'var(--brand-purple-50)', boxShadow: 'inset 0 0 0 1px var(--brand-purple-200)', cursor: 'pointer' }}>Browse Vault</span>
                </div>
                <div style={{ display: 'flex', alignItems: 'center', gap: 10, borderRadius: 12, padding: 8, outline: '1.5px dashed rgba(28,27,27,0.15)', outlineOffset: -1, marginBottom: 13 }}>
                  <div style={{ width: 40, height: 50, borderRadius: 8, overflow: 'hidden', flexShrink: 0,
                    backgroundImage: `url(${GENS[7].src})`, backgroundSize: 'cover', backgroundPosition: 'center 20%' }} />
                  <span style={{ fontSize: 11.5, color: 'var(--text-secondary)' }}>poolside-set-04.png · from your Vault</span>
                </div>
                <div style={{ display: 'flex', alignItems: 'center', gap: 8, borderTop: '1px solid rgba(28,27,27,0.08)', paddingTop: 12 }}>
                  <div>
                    <div style={{ fontSize: 8.5, fontWeight: 700, letterSpacing: '0.1em', textTransform: 'uppercase', color: 'var(--text-mute)' }}>Publishing at</div>
                    <div style={{ fontSize: 12, fontWeight: 700, color: 'var(--brand-purple-600)' }}>7/28/2026, 01:17 PM</div>
                  </div>
                  <span style={{ marginLeft: 'auto', borderRadius: 9999, padding: '8px 16px', fontSize: 11, fontWeight: 700, letterSpacing: '0.05em', textTransform: 'uppercase',
                    color: 'rgba(28,27,27,0.6)', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.12)', cursor: 'pointer' }}>Discard</span>
                  <span style={{ whiteSpace: 'nowrap', display: 'inline-flex', alignItems: 'center', borderRadius: 9999, padding: '9px 18px',
                    background: PULSE, color: '#fff', fontSize: 11, fontWeight: 700, letterSpacing: '0.05em', textTransform: 'uppercase',
                    boxShadow: '0 8px 20px -4px rgba(104,19,212,0.4)', cursor: 'pointer' }}>Schedule Post</span>
                </div>
              </div>
            </div>
          </Reveal>
          {/* mini calendar — posts schedule themselves */}
          <Reveal delay={240} style={{ height: '100%' }}>
            <MiniCalendar />
          </Reveal>
        </div>
      </div>
    </SectionShell>
  );
};

// Mini calendar — mirrors the product's Scheduler month view; posts pop into slots on a loop
const CAL_FILLS = [ // [cellIndex, slotIndex(0=1PM,1=5PM,2=9PM), GENS index]
  [1, 0, 5], [3, 1, 1], [5, 0, 7], [8, 2, 2], [10, 1, 3], [12, 0, 0], [6, 2, 8], [2, 1, 4],
];
const MiniCalendar = () => {
  const [filled, setFilled] = React.useState(0);
  React.useEffect(() => {
    const t = setInterval(() => setFilled(f => (f >= CAL_FILLS.length ? 0 : f + 1)), 900);
    return () => clearInterval(t);
  }, []);
  const SLOT_LABELS = ['01:00 PM', '05:00 PM', '09:00 PM'];
  return (
    <div style={{ borderRadius: 20, background: '#fff', boxShadow: '0 24px 48px rgba(28,27,27,0.14), inset 0 0 0 1px rgba(28,27,27,0.08)', padding: 20,
      height: '100%', boxSizing: 'border-box', display: 'flex', flexDirection: 'column',
      transform: 'perspective(1400px) rotateX(1.5deg) rotate(-1.6deg)', transformOrigin: '50% 40%' }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 12 }}>
        <span style={{ fontSize: 14, fontWeight: 700, color: INK }}>July 2026</span>
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, fontSize: 11, fontWeight: 700, color: '#22C55E' }}>
          <span style={{ width: 7, height: 7, borderRadius: '50%', background: '#22C55E', animation: 'sz-pulse-dot 1.4s ease-in-out infinite' }} />
          Auto-publishing
        </span>
      </div>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(7, 1fr)', gap: 4, marginBottom: 5 }}>
        {['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'].map(d => (
          <span key={d} style={{ fontSize: 8.5, fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--text-mute)', textAlign: 'center' }}>{d}</span>
        ))}
      </div>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(7, 1fr)', gap: 4, flex: 1, alignContent: 'stretch' }}>
        {Array.from({ length: 14 }, (_, ci) => {
          const fillsHere = CAL_FILLS.map((f, oi) => ({ f, oi })).filter(({ f }) => f[0] === ci);
          return (
            <div key={ci} style={{ borderRadius: 8, background: 'rgb(252,248,248)', boxShadow: 'inset 0 0 0 1px rgba(28,27,27,0.07)', padding: '5px 4px 6px', minHeight: 108, display: 'flex', flexDirection: 'column' }}>
              <div style={{ fontSize: 8.5, fontWeight: 700, color: 'rgba(28,27,27,0.55)', marginBottom: 4, paddingLeft: 2 }}>{ci + 5}</div>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 4, flex: 1, justifyContent: 'space-between' }}>
                {SLOT_LABELS.map((sl, si) => {
                  const hit = fillsHere.find(({ f }) => f[1] === si);
                  const on = hit && hit.oi < filled;
                  return (
                    <div key={sl} style={{ position: 'relative', flex: 1, minHeight: 22, borderRadius: 5, overflow: 'hidden',
                      outline: on ? 'none' : '1px dashed var(--brand-purple-200)', outlineOffset: -1,
                      background: on ? 'var(--brand-purple-50)' : 'transparent', transition: `background 0.35s ${SB_EASE}` }}>
                      {on ? (
                        <span style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', gap: 4, padding: '0 4px', animation: `sz-status-in 0.4s ${SB_EASE}` }}>
                          <span style={{ width: 13, height: 13, borderRadius: 3, flexShrink: 0, backgroundImage: `url(${GENS[hit.f[2]].src})`, backgroundSize: 'cover', backgroundPosition: 'center 20%' }} />
                          <span style={{ fontSize: 8, fontWeight: 700, color: 'var(--brand-purple-700)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{sl}</span>
                        </span>
                      ) : (
                        <span style={{ position: 'absolute', inset: 0, display: 'grid', placeItems: 'center', fontSize: 8, fontWeight: 600, color: 'var(--brand-purple-300)' }}>{sl}</span>
                      )}
                    </div>
                  );
                })}
              </div>
            </div>
          );
        })}
      </div>
      <div style={{ display: 'flex', justifyContent: 'space-between', marginTop: 10, fontSize: 11 }}>
        <span style={{ fontWeight: 700, color: 'var(--brand-purple-700)' }}>{Math.min(filled, CAL_FILLS.length)} posts queued this week</span>
        <span style={{ color: 'var(--text-tertiary)' }}>Month · Week · Day</span>
      </div>
    </div>
  );
};

Object.assign(window, { CopilotSection, SchedSection });
