// ============================================================
// Alumnus member app — onboarding screens 1.1 → 1.7
// ============================================================
const { Icon: I, Crest, Lockup, Avatar, Button, Eyebrow, Hair, Pill, Card, Phone, PermissionCard, MEMBER, PARTNER, SPONSOR } = window.AL;

// ─────────────────────────────────────────────────────────────
// 1.1  Welcome / brand intro
// ─────────────────────────────────────────────────────────────
const M_Welcome = ({ nav = () => {} }) => (
  <div style={{
    background: 'var(--color-ivy)', height: '100%',
    color: 'var(--color-bone)', padding: '120px 32px 48px',
    display: 'flex', flexDirection: 'column', boxSizing: 'border-box',
  }}>
    <div style={{ alignSelf: 'center', marginBottom: 36 }}>
      <Crest size={56} dark/>
    </div>
    <div style={{ textAlign: 'center', marginBottom: 'auto' }}>
      <div style={{
        fontFamily: 'var(--font-display)', fontSize: 38, lineHeight: '44px',
        fontWeight: 400, letterSpacing: '-0.018em', color: 'var(--color-bone)',
      }}>Welcome to the community that comes next.</div>
      <div style={{
        fontFamily: 'var(--font-display)', fontStyle: 'italic',
        fontSize: 17, lineHeight: '25px', color: 'rgba(242,237,226,0.72)',
        marginTop: 22, maxWidth: 280, margin: '22px auto 0',
      }}>You've already done the hard part. From here you don't do it alone.</div>
    </div>
    <Button variant="brass" size="lg" full onClick={() => nav('invite')}>I have an invite code</Button>
    <button onClick={() => nav('invite')} style={{
      background: 'transparent', color: 'rgba(242,237,226,0.7)',
      border: 'none', padding: '14px',
      fontFamily: 'var(--font-body)', fontSize: 14,
      cursor: 'pointer', marginTop: 6,
    }}>Learn more</button>
  </div>
);

// ─────────────────────────────────────────────────────────────
// 1.2  Invite code entry
// ─────────────────────────────────────────────────────────────
const M_Invite = ({ nav = () => {} }) => {
  const code = ['P','I','N','E','4','7'];
  return (
    <div style={{ padding: '88px 28px 40px', height: '100%', boxSizing: 'border-box',
                  display: 'flex', flexDirection: 'column' }}>
      <button onClick={() => nav('welcome')} style={{ background: 'none', border: 'none',
              color: 'var(--fg-secondary)', fontSize: 14, padding: 0, marginBottom: 32,
              cursor: 'pointer', display: 'inline-flex', alignItems: 'center', gap: 6, alignSelf: 'flex-start' }}>
        <I name="caret-left" size={14}/> Back
      </button>
      <Eyebrow tone="brass" style={{ marginBottom: 14 }}>Step 1 of 4 · Invitation</Eyebrow>
      <h1 style={{ fontFamily: 'var(--font-display)', fontSize: 32, lineHeight: '38px',
                   fontWeight: 400, letterSpacing: '-0.015em', margin: '0 0 14px' }}>
        Enter your invite code.
      </h1>
      <p style={{ fontSize: 15, lineHeight: '22px', color: 'var(--fg-secondary)', margin: '0 0 36px' }}>
        You'll find this on the discharge letter from Pine Tree Recovery, or in the email from your accountability partner.
      </p>
      <div style={{ display: 'flex', gap: 8, justifyContent: 'space-between', marginBottom: 16 }}>
        {code.map((c, i) => (
          <div key={i} style={{
            flex: 1, height: 56, borderRadius: 4,
            background: 'var(--bg-surface)',
            border: `1px solid ${i < 4 ? 'var(--color-ivy)' : 'var(--border-hairline)'}`,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontFamily: 'var(--font-display)', fontSize: 26, fontWeight: 500,
            color: i < 4 ? 'var(--color-ivy)' : 'var(--fg-tertiary)',
            fontVariantNumeric: 'tabular-nums',
          }}>{i < 4 ? c : ''}</div>
        ))}
      </div>
      <div style={{ fontSize: 12.5, color: 'var(--fg-tertiary)', marginBottom: 'auto' }}>
        Six characters. Letters and numbers.
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
        <Button variant="primary" full onClick={() => nav('profile')}>Continue</Button>
        <button style={{ background: 'none', border: 'none', color: 'var(--color-brass-dark)',
                fontSize: 14, padding: '10px', cursor: 'pointer' }}>
          I don't have a code · Talk to support
        </button>
      </div>
    </div>
  );
};

// ─────────────────────────────────────────────────────────────
// 1.3  Profile setup (multi-step shown as one screen)
// ─────────────────────────────────────────────────────────────
const M_Profile = ({ nav = () => {} }) => {
  const goals = ['Family', 'Work', 'Health', 'Recovery community', 'Education', 'Quiet'];
  const chosen = ['Family', 'Recovery community', 'Health'];
  return (
    <div style={{ padding: '88px 28px 40px', height: '100%', boxSizing: 'border-box',
                  display: 'flex', flexDirection: 'column' }}>
      <button onClick={() => nav('invite')} style={{ background: 'none', border: 'none',
              color: 'var(--fg-secondary)', fontSize: 14, padding: 0, marginBottom: 28,
              cursor: 'pointer', display: 'inline-flex', alignItems: 'center', gap: 6, alignSelf: 'flex-start' }}>
        <I name="caret-left" size={14}/> Back
      </button>
      <Eyebrow tone="brass" style={{ marginBottom: 14 }}>Step 2 of 4 · Your profile</Eyebrow>
      <h1 style={{ fontFamily: 'var(--font-display)', fontSize: 28, lineHeight: '34px',
                   fontWeight: 400, letterSpacing: '-0.012em', margin: '0 0 26px' }}>
        Tell us who you are.
      </h1>

      <Eyebrow style={{ marginBottom: 8 }}>Your name</Eyebrow>
      <div style={{
        padding: '14px 16px', background: 'var(--bg-surface)', borderRadius: 4,
        border: '1px solid var(--color-ivy)', fontSize: 16, marginBottom: 24,
      }}>Anna Reyes</div>

      <Eyebrow style={{ marginBottom: 8 }}>Your class year</Eyebrow>
      <div style={{
        padding: '14px 16px', background: 'var(--bg-surface)', borderRadius: 4,
        border: '1px solid var(--border-hairline)',
        fontSize: 16, marginBottom: 6,
        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
      }}>
        <span>April 4, 2026</span>
        <I name="calendar-blank" size={18} color="var(--color-stone)"/>
      </div>
      <div style={{ fontSize: 12, color: 'var(--fg-tertiary)', marginBottom: 24, fontStyle: 'italic',
                    fontFamily: 'var(--font-display)' }}>
        The day you completed your program. Anchors everything that follows.
      </div>

      <Eyebrow style={{ marginBottom: 10 }}>What matters now</Eyebrow>
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 'auto' }}>
        {goals.map(g => {
          const on = chosen.includes(g);
          return (
            <div key={g} style={{
              padding: '8px 14px', borderRadius: 999, fontSize: 13.5,
              border: `1px solid ${on ? 'var(--color-ivy)' : 'var(--border-hairline)'}`,
              background: on ? 'var(--color-ivy)' : 'transparent',
              color: on ? 'var(--color-bone)' : 'var(--fg-primary)',
              display: 'inline-flex', alignItems: 'center', gap: 6,
            }}>
              {on && <I name="check" size={12}/>}
              {g}
            </div>
          );
        })}
      </div>
      <Button variant="primary" full onClick={() => nav('commitment')} style={{ marginTop: 24 }}>Continue</Button>
    </div>
  );
};

// ─────────────────────────────────────────────────────────────
// 1.4  Membership Commitment — the philosophical heart
// ─────────────────────────────────────────────────────────────
const M_Commitment = ({ nav = () => {} }) => (
  <div style={{ padding: '76px 28px 40px', height: '100%', boxSizing: 'border-box',
                display: 'flex', flexDirection: 'column', overflow: 'auto' }}>
    <Eyebrow tone="brass" style={{ marginBottom: 14 }}>Step 3 of 4 · A membership</Eyebrow>
    <h1 style={{ fontFamily: 'var(--font-display)', fontSize: 30, lineHeight: '36px',
                 fontWeight: 400, letterSpacing: '-0.015em', margin: '0 0 22px' }}>
      What we promise. What we ask.
    </h1>

    <p style={{ fontFamily: 'var(--font-display)', fontSize: 16.5, lineHeight: '26px',
                color: 'var(--fg-primary)', fontStyle: 'italic',
                margin: '0 0 22px',
                paddingLeft: 16, borderLeft: '2px solid var(--color-brass)' }}>
      Alumnus is a membership, not a monitor. The product earns its keep by being measured, plain, and yours to leave.
    </p>

    <div style={{ display: 'flex', flexDirection: 'column' }}>
      {[
        { i: 'waveform',    h: 'We observe rhythm, not contents.', b: "Sleep, meeting attendance, time-of-day patterns. We never read your messages, screen, or browsing." },
        { i: 'users-three', h: "Your network sees patterns.",       b: 'Three people you choose. They see whether your week is steady — never the details.' },
        { i: 'hand-palm',   h: 'You can pause or revoke at any time.', b: 'One tap pauses observation. One tap removes a person from your network. No questions, no exit interview.' },
        { i: 'graduation-cap', h: 'You get a community in return.', b: 'A continuing class of alumni. Meeting check-ins. Someone reaching out when a hard week shows up.' },
      ].map((row, i) => (
        <div key={i} style={{
          display: 'flex', gap: 16, padding: '18px 0',
          borderBottom: i < 3 ? '1px solid var(--border-hairline)' : 'none',
          borderTop: i === 0 ? '1px solid var(--border-hairline)' : 'none',
        }}>
          <I name={row.i} size={22} color="var(--color-ivy)" style={{ marginTop: 2 }}/>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 15, fontWeight: 600, color: 'var(--fg-primary)', marginBottom: 4 }}>{row.h}</div>
            <div style={{ fontSize: 13.5, lineHeight: '19px', color: 'var(--fg-secondary)' }}>{row.b}</div>
          </div>
        </div>
      ))}
    </div>

    <div style={{ marginTop: 28, marginBottom: 16, fontSize: 12.5,
                  color: 'var(--fg-tertiary)', textAlign: 'center' }}>
      Signed by you, witnessed by Pine Tree Recovery.
    </div>
    <Button variant="primary" size="lg" full onClick={() => nav('perm-notif')}>I'm in</Button>
  </div>
);

// ─────────────────────────────────────────────────────────────
// 1.5  Permission Request Sequence (5 individual screens)
// ─────────────────────────────────────────────────────────────
const M_PermNotif = ({ nav = () => {} }) => (
  <PermissionCard
    icon="bell"
    title="Get a soft nudge when it matters."
    why="A note before a meeting. An acknowledgment from your sponsor. Nothing else."
    control="No marketing. No badges. You set the quiet hours."
    allowLabel="Allow notifications"
    onAllow={() => nav('perm-location')}
    onSkip={() => nav('perm-location')}
  />
);

const M_PermLocation = ({ nav = () => {} }) => (
  <PermissionCard
    icon="map-pin"
    title="So we know when you've made it to the room."
    why="The app checks you in at the meetings you choose — automatically, without you having to remember."
    control="Only at the addresses you add. We do not log your day."
    allowLabel="Allow location"
    onAllow={() => nav('perm-activity')}
    onSkip={() => nav('perm-activity')}
  />
);

const M_PermActivity = ({ nav = () => {} }) => (
  <PermissionCard
    icon="moon-stars"
    title="So the app can see when sleep drifts."
    why="Sleep is the earliest, quietest signal that a week is getting hard. We compare against your own normal."
    control="We look at start and length. Nothing about what you do awake."
    allowLabel="Allow screen time"
    onAllow={() => nav('perm-content')}
    onSkip={() => nav('perm-content')}
  />
);

const M_PermContent = ({ nav = () => {} }) => (
  <PermissionCard
    icon="eye-slash"
    title="An on-device filter for a few specific patterns."
    why="Just enough to notice if a known risk pattern shows up — handled entirely on your phone."
    control="Nothing is uploaded. No human ever reads your screen."
    allowLabel="Allow on-device check"
    onAllow={() => nav('perm-network')}
    onSkip={() => nav('perm-network')}
  />
);

const M_PermNetwork = ({ nav = () => {} }) => (
  <PermissionCard
    icon="users-three"
    title="Bring your network in."
    why="Three people who already know you. They see whether your week is steady, and a calm message when something is worth a conversation."
    control="You add and remove them. They can never see contents."
    allowLabel="Set up my network"
    onAllow={() => nav('network')}
    onSkip={() => nav('network')}
  />
);

// ─────────────────────────────────────────────────────────────
// 1.6  Accountability Network Setup
// ─────────────────────────────────────────────────────────────
const M_NetworkSetup = ({ nav = () => {} }) => {
  const people = [
    { name: 'Maria Reyes', role: 'Mother', tag: 'Primary partner', status: 'Invited Apr 5', initials: 'MR', tone: 'ivy' },
    { name: 'Beth Whelan', role: 'Sponsor · 8 months', tag: 'Sponsor', status: 'Accepted Apr 5', initials: 'BW', tone: 'brass' },
    { name: 'Dr. Caleb Singh', role: 'Pine Tree Recovery', tag: 'Clinical', status: 'Connected', initials: 'CS', tone: 'moss' },
  ];
  return (
    <div style={{ padding: '88px 28px 40px', height: '100%', boxSizing: 'border-box',
                  display: 'flex', flexDirection: 'column' }}>
      <Eyebrow tone="brass" style={{ marginBottom: 14 }}>Step 4 of 4 · Your network</Eyebrow>
      <h1 style={{ fontFamily: 'var(--font-display)', fontSize: 28, lineHeight: '34px',
                   fontWeight: 400, letterSpacing: '-0.012em', margin: '0 0 12px' }}>
        Three people, looking out.
      </h1>
      <p style={{ fontSize: 14.5, lineHeight: '21px', color: 'var(--fg-secondary)', margin: '0 0 24px' }}>
        They'll get an invitation to install the partner portal. Each one decides what they're comfortable seeing.
      </p>
      <div>
        {people.map((p, i) => (
          <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 14,
                                padding: '16px 0', borderBottom: '1px solid var(--border-hairline)' }}>
            <Avatar initials={p.initials} tone={p.tone} size={44}/>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 15, fontWeight: 500 }}>{p.name}</div>
              <div style={{ fontSize: 12.5, color: 'var(--fg-tertiary)' }}>{p.role}</div>
            </div>
            <Pill tone="oat" dot={false}>{p.tag}</Pill>
          </div>
        ))}
      </div>
      <button style={{
        marginTop: 16, padding: '14px', background: 'transparent',
        border: '1px dashed var(--color-stone-soft)', borderRadius: 4,
        color: 'var(--color-ivy)', fontSize: 14, fontWeight: 500,
        display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
        cursor: 'pointer',
      }}>
        <I name="user-plus" size={16}/> Add another person
      </button>
      <div style={{ marginTop: 'auto', marginBottom: 16, fontSize: 12.5,
                    color: 'var(--fg-tertiary)', fontStyle: 'italic', fontFamily: 'var(--font-display)' }}>
        You can change this list any time. Pause one, remove one, swap one. It is yours.
      </div>
      <Button variant="primary" full onClick={() => nav('meetings-setup')}>Continue</Button>
    </div>
  );
};

// ─────────────────────────────────────────────────────────────
// 1.7  Meeting Schedule Setup
// ─────────────────────────────────────────────────────────────
const M_MeetingsSetup = ({ nav = () => {} }) => {
  const types = [
    { i: 'users-three',     n: 'AA / NA',          d: 'Local chapter, weekly' },
    { i: 'first-aid-kit',   n: 'IOP',              d: 'Intensive outpatient' },
    { i: 'chat-circle',     n: 'Individual therapy', d: 'With your clinician' },
    { i: 'church',          n: 'SMART Recovery',   d: 'Peer-led meeting' },
  ];
  const added = [
    { i: 'users-three', n: "St. Margaret's Wed night", d: 'Wednesdays · 7:30 pm', loc: '442 Bay St, St. Petersburg' },
    { i: 'first-aid-kit', n: 'Pine Tree IOP', d: 'Mon / Thu · 6:00 pm', loc: 'Pine Tree Recovery, Clearwater' },
  ];
  return (
    <div style={{ padding: '88px 28px 40px', height: '100%', boxSizing: 'border-box',
                  display: 'flex', flexDirection: 'column' }}>
      <Eyebrow tone="brass" style={{ marginBottom: 14 }}>Almost there · meetings</Eyebrow>
      <h1 style={{ fontFamily: 'var(--font-display)', fontSize: 28, lineHeight: '34px',
                   fontWeight: 400, letterSpacing: '-0.012em', margin: '0 0 8px' }}>
        Tell us about the rooms you'll be in.
      </h1>
      <p style={{ fontSize: 14, lineHeight: '20px', color: 'var(--fg-secondary)', margin: '0 0 22px' }}>
        Add a meeting and you'll be checked in automatically when you arrive.
      </p>

      <Eyebrow style={{ marginBottom: 10 }}>Quick add</Eyebrow>
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginBottom: 24 }}>
        {types.map(t => (
          <div key={t.n} style={{
            padding: '14px', background: 'var(--bg-surface)', borderRadius: 4,
            border: '1px solid var(--border-hairline)',
          }}>
            <I name={t.i} size={20} color="var(--color-ivy)" style={{ marginBottom: 8 }}/>
            <div style={{ fontSize: 13.5, fontWeight: 500 }}>{t.n}</div>
            <div style={{ fontSize: 11.5, color: 'var(--fg-tertiary)' }}>{t.d}</div>
          </div>
        ))}
      </div>

      <Eyebrow style={{ marginBottom: 10 }}>On your schedule</Eyebrow>
      {added.map((m, i) => (
        <div key={i} style={{
          padding: '14px 16px', background: 'var(--bg-elevated)',
          border: '1px solid var(--border-hairline)', borderRadius: 4, marginBottom: 8,
          display: 'flex', alignItems: 'center', gap: 14,
        }}>
          <div style={{
            width: 36, height: 36, borderRadius: 4, background: 'var(--color-bone-soft)',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
          }}><I name={m.i} size={18} color="var(--color-ivy)"/></div>
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ fontSize: 14, fontWeight: 500 }}>{m.n}</div>
            <div style={{ fontSize: 11.5, color: 'var(--fg-tertiary)' }}>{m.d} · {m.loc}</div>
          </div>
          <I name="check-circle" size={20} color="var(--color-moss)" weight="fill"/>
        </div>
      ))}

      <button style={{
        marginTop: 4, padding: '14px', background: 'transparent',
        border: '1px dashed var(--color-stone-soft)', borderRadius: 4,
        color: 'var(--color-ivy)', fontSize: 14, fontWeight: 500,
        display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
        cursor: 'pointer',
      }}>
        <I name="plus" size={16}/> Add a meeting
      </button>

      <div style={{ marginTop: 'auto' }}/>
      <Button variant="primary" full onClick={() => nav('home')} style={{ marginTop: 20 }}>Finish setup</Button>
    </div>
  );
};

window.MemberOnboarding = {
  M_Welcome, M_Invite, M_Profile, M_Commitment,
  M_PermNotif, M_PermLocation, M_PermActivity, M_PermContent, M_PermNetwork,
  M_NetworkSetup, M_MeetingsSetup,
};
