// fc-auth.jsx, returning-user authentication — the side the onboarding never
// covered. Same editorial "balanced" system as Flow 1: ink primary, warm paper,
// Oswald eyebrows, red accent. Screens:
//   AuthLogin        · email + password, social, forgot/sign-up links
//   AuthLoginError   · same, wrong-credentials state (red field + inline error)
//   AuthForgot       · request a reset link
//   AuthCheckInbox   · reset-link-sent confirmation
//   AuthVerify       · 6-digit email / 2-factor code entry
// Social buttons are intentionally neutral (no brand logos reproduced).

const { Phone: APhone, StatusBar: AStatusBar, HomeIndicator: AHome, Eyebrow: AEyebrow,
        CoachTip: ACoachTip } = window;

const AB = '#a32a18';

// ── shared chrome ────────────────────────────────────────────────────────────
// Lightweight top bar: optional back chip + centred title. No progress bar — the
// athlete already has an account; this isn't a funnel.
function AuthNav({ title, back = true }) {
  return (
    <div style={{ height: 50, padding: '0 14px', display: 'flex', alignItems: 'center', flexShrink: 0 }}>
      <div style={{ minWidth: 40 }}>
        {back && (
          <button aria-label="Back" style={{ width: 44, height: 44, borderRadius: 0, marginLeft: -8, background: 'none', border: 'none', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <svg width="9" height="16" viewBox="0 0 11 18" fill="none"><path d="M9 1 L2 9 L9 17" stroke="var(--ink)" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>
          </button>
        )}
      </div>
      <div style={{ flex: 1, textAlign: 'center', fontFamily: 'var(--display)', fontSize: 13, letterSpacing: '0.14em', textTransform: 'uppercase', fontWeight: 600, color: 'var(--ink-2)' }}>{title}</div>
      <div style={{ minWidth: 40 }} />
    </div>
  );
}

function Wordmark({ size = 48 }) {
  return (
    <div style={{ width: size, height: size, borderRadius: size * 0.29, background: 'var(--ink)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
      {window.AppMark ? <window.AppMark ink="var(--paper)" accent="var(--accent)" size={size * 0.6} /> : null}
    </div>
  );
}

// Field, mirrors the onboarding account field; `error` paints the red edge.
function Field({ label, val, ph, mask = false, error = false, caps = false }) {
  const shown = mask && val ? '\u2022'.repeat(val.length) : val;
  return (
    <div>
      <div style={{ fontFamily: 'var(--display)', fontSize: 10.5, letterSpacing: '0.14em', textTransform: 'uppercase', color: error ? 'var(--accent)' : 'var(--ink-3)', fontWeight: 600, marginBottom: 8 }}>{label}</div>
      <div style={{ background: 'var(--paper)', border: `1.5px solid ${error ? 'var(--accent)' : 'var(--rule)'}`, borderRadius: 'var(--radius-ctl)', minHeight: 52, display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 16px', fontFamily: 'var(--sans)', fontSize: 16, color: val ? 'var(--ink)' : 'var(--ink-3)' }}>
        <span style={{ letterSpacing: mask && val ? '0.18em' : 'normal' }}>{shown || ph}</span>
        {mask && val && (
          <svg width="20" height="14" viewBox="0 0 22 15" fill="none"><path d="M1 7.5 C4 2.5 8 1 11 1 S18 2.5 21 7.5 C18 12.5 14 14 11 14 S4 12.5 1 7.5Z" stroke="var(--ink-3)" strokeWidth="1.3"/><circle cx="11" cy="7.5" r="2.6" stroke="var(--ink-3)" strokeWidth="1.3"/></svg>
        )}
      </div>
    </div>
  );
}

function PrimaryCTA({ label, disabled = false }) {
  return (
    <button style={{ width: '100%', background: disabled ? 'var(--rule)' : 'var(--ink)', color: disabled ? 'var(--ink-3)' : 'var(--paper)', border: 'none', borderRadius: 'var(--radius-ctl)', minHeight: 54, fontFamily: 'var(--display)', fontSize: 14, letterSpacing: '0.14em', fontWeight: 600 }}>{label}</button>
  );
}

function OrRule() {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
      <div style={{ flex: 1, height: 1, background: 'var(--rule)' }} />
      <span style={{ fontFamily: 'var(--mono)', fontSize: 10.5, letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--ink-3)', fontWeight: 600 }}>or</span>
      <div style={{ flex: 1, height: 1, background: 'var(--rule)' }} />
    </div>
  );
}

// Neutral social button — no brand logo reproduced, just a quiet glyph + label.
function SocialButton({ label, glyph }) {
  return (
    <button style={{ width: '100%', background: 'var(--paper)', color: 'var(--ink)', border: '1px solid var(--rule-2)', borderRadius: 'var(--radius-ctl)', minHeight: 52, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 11, fontFamily: 'var(--sans)', fontSize: 15.5, fontWeight: 600 }}>
      <span style={{ width: 20, height: 20, display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--ink-2)' }}>{glyph}</span>
      {label}
    </button>
  );
}
const GlyphApple = <svg width="16" height="18" viewBox="0 0 16 18" fill="none"><path d="M11.2 9.4c0-2 1.5-2.9 1.6-3-0.9-1.3-2.2-1.4-2.7-1.5-1.1-0.1-2.2 0.7-2.8 0.7-0.6 0-1.5-0.6-2.4-0.6-1.2 0-2.4 0.7-3 1.8-1.3 2.2-0.3 5.5 0.9 7.3 0.6 0.9 1.3 1.9 2.2 1.8 0.9 0 1.2-0.6 2.3-0.6 1 0 1.3 0.6 2.3 0.5 0.9 0 1.5-0.9 2.1-1.7 0.4-0.6 0.6-0.9 0.4-1-0.1-0.1-1.9-0.7-1.9-2.9Z" fill="currentColor"/><path d="M9.6 3.6c0.5-0.6 0.8-1.4 0.7-2.3-0.7 0-1.5 0.5-2 1.1-0.4 0.5-0.8 1.3-0.7 2.1 0.8 0.1 1.6-0.4 2-0.9Z" fill="currentColor"/></svg>;
const GlyphGoogle = <svg width="17" height="17" viewBox="0 0 18 18" fill="none"><circle cx="9" cy="9" r="6.5" stroke="currentColor" strokeWidth="1.5"/><path d="M9 9 H15.5" stroke="currentColor" strokeWidth="1.5"/><path d="M9 9 L13.6 13.6" stroke="currentColor" strokeWidth="1.5"/></svg>;

function TextLink({ children, accent = false }) {
  return <button style={{ fontFamily: 'var(--sans)', fontSize: 14.5, color: accent ? 'var(--accent)' : 'var(--ink-2)', fontWeight: 500, minHeight: 30 }}>{children}</button>;
}

// ── LOGIN ─────────────────────────────────────────────────────────────────────
function LoginBody({ error = false }) {
  return (
    <>
      <AStatusBar />
      <AuthNav title="Log in" />
      <div style={{ flex: 1, padding: '6px 26px 22px', display: 'flex', flexDirection: 'column', overflow: 'hidden' }}>
        <Wordmark />
        <div style={{ fontFamily: 'var(--sans)', fontSize: 28, fontWeight: 700, letterSpacing: '-0.02em', lineHeight: 1.1, color: 'var(--ink)', marginTop: 22, textWrap: 'balance' }}>Welcome back.</div>
        <div style={{ fontFamily: 'var(--sans)', fontSize: 15, lineHeight: 1.5, color: 'var(--ink-2)', marginTop: 9 }}>Log in to pick your camp up where you left it.</div>

        <div style={{ display: 'flex', flexDirection: 'column', gap: 14, marginTop: 26 }}>
          <Field label="Email" val="alex.mercer@gmail.com" ph="you@email.com" error={error} />
          <Field label="Password" val="onweight24" ph="Your password" mask error={error} />
          {error && (
            <div style={{ display: 'flex', alignItems: 'flex-start', gap: 8, marginTop: -2 }}>
              <svg width="15" height="15" viewBox="0 0 16 16" fill="none" style={{ flexShrink: 0, marginTop: 1 }}><circle cx="8" cy="8" r="7" stroke="var(--accent)" strokeWidth="1.4"/><line x1="8" y1="4.5" x2="8" y2="9" stroke="var(--accent)" strokeWidth="1.5" strokeLinecap="round"/><circle cx="8" cy="11.5" r="0.9" fill="var(--accent)"/></svg>
              <span style={{ fontFamily: 'var(--sans)', fontSize: 13, color: 'var(--accent)', lineHeight: 1.4, fontWeight: 500 }}>That email and password don’t match. Check them, or reset below.</span>
            </div>
          )}
        </div>

        <div style={{ marginTop: 16 }}><PrimaryCTA label="CONTINUE" /></div>
        <div style={{ textAlign: 'center', marginTop: 12 }}><TextLink accent>Forgot your password?</TextLink></div>

        <div style={{ margin: '20px 0 16px' }}><OrRule /></div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 11 }}>
          <SocialButton label="Continue with Apple" glyph={GlyphApple} />
          <SocialButton label="Continue with Google" glyph={GlyphGoogle} />
        </div>

        <div style={{ flex: 1 }} />
        <div style={{ textAlign: 'center', fontFamily: 'var(--sans)', fontSize: 14.5, color: 'var(--ink-2)' }}>
          New to OnWeight? <span style={{ color: 'var(--accent)', fontWeight: 600 }}>Create account</span>
        </div>
      </div>
      <AHome />
    </>
  );
}
function AuthLogin() { return <APhone styleKey="balanced" label="Log in"><LoginBody /></APhone>; }
function AuthLoginError() { return <APhone styleKey="balanced" label="Log in · error"><LoginBody error /></APhone>; }

// ── FORGOT PASSWORD ────────────────────────────────────────────────────────────
function AuthForgot() {
  return (
    <APhone styleKey="balanced" label="Forgot password">
      <AStatusBar />
      <AuthNav title="Reset password" />
      <div style={{ flex: 1, padding: '10px 26px 22px', display: 'flex', flexDirection: 'column' }}>
        <div style={{ fontFamily: 'var(--sans)', fontSize: 27, fontWeight: 700, letterSpacing: '-0.02em', lineHeight: 1.12, color: 'var(--ink)', textWrap: 'balance' }}>Forgot your password?</div>
        <div style={{ fontFamily: 'var(--sans)', fontSize: 15, lineHeight: 1.5, color: 'var(--ink-2)', marginTop: 10, textWrap: 'pretty' }}>Enter the email on your account and we’ll send a link to set a new one. Your camp data stays exactly where it is.</div>
        <div style={{ marginTop: 26 }}><Field label="Email" val="alex.mercer@gmail.com" ph="you@email.com" /></div>
        <div style={{ marginTop: 18 }}><PrimaryCTA label="SEND RESET LINK" /></div>
        <div style={{ flex: 1 }} />
        <div style={{ textAlign: 'center' }}><TextLink>Back to log in</TextLink></div>
      </div>
      <AHome />
    </APhone>
  );
}

// ── RESET LINK SENT ─────────────────────────────────────────────────────────────
function AuthCheckInbox() {
  return (
    <APhone styleKey="balanced" label="Check inbox">
      <AStatusBar />
      <AuthNav title="Reset password" />
      <div style={{ flex: 1, padding: '10px 26px 22px', display: 'flex', flexDirection: 'column', alignItems: 'center', textAlign: 'center' }}>
        <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center' }}>
          <div style={{ width: 78, height: 78, borderRadius: '50%', background: 'var(--accent-soft)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <svg width="36" height="36" viewBox="0 0 40 40" fill="none"><rect x="5" y="9" width="30" height="22" rx="3" stroke="var(--accent)" strokeWidth="1.8"/><path d="M6 11 L20 22 L34 11" stroke="var(--accent)" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>
          </div>
          <div style={{ fontFamily: 'var(--sans)', fontSize: 25, fontWeight: 700, letterSpacing: '-0.02em', color: 'var(--ink)', marginTop: 26, textWrap: 'balance' }}>Check your inbox.</div>
          <div style={{ fontFamily: 'var(--sans)', fontSize: 15, lineHeight: 1.55, color: 'var(--ink-2)', marginTop: 10, maxWidth: '30ch', textWrap: 'pretty' }}>We sent a reset link to <span style={{ fontWeight: 600, color: 'var(--ink)' }}>alex.mercer@gmail.com</span>. It expires in 30 minutes.</div>
        </div>
        <div style={{ width: '100%' }}><PrimaryCTA label="OPEN MAIL APP" /></div>
        <div style={{ marginTop: 12 }}><span style={{ fontFamily: 'var(--sans)', fontSize: 14, color: 'var(--ink-3)' }}>Didn’t get it? </span><TextLink accent>Resend</TextLink></div>
      </div>
      <AHome />
    </APhone>
  );
}

// ── VERIFY CODE · email verification / 2-factor ───────────────────────────────
function CodeBoxes({ digits = '418', total = 6 }) {
  return (
    <div style={{ display: 'flex', gap: 9, justifyContent: 'center' }}>
      {Array.from({ length: total }).map((_, i) => {
        const filled = i < digits.length, cursor = i === digits.length;
        return (
          <div key={i} style={{ width: 46, height: 58, borderRadius: 'var(--radius-ctl)', background: 'var(--paper)', border: `1.5px solid ${cursor ? 'var(--ink)' : filled ? 'var(--rule-2)' : 'var(--rule)'}`, display: 'flex', alignItems: 'center', justifyContent: 'center', fontFamily: 'var(--num)', fontWeight: 300, fontSize: 30, color: 'var(--ink)' }}>
            {filled ? digits[i] : cursor ? <span style={{ width: 2, height: 26, background: AB, borderRadius: 1 }} /> : ''}
          </div>
        );
      })}
    </div>
  );
}
function AuthVerify() {
  return (
    <APhone styleKey="balanced" label="Verify code">
      <AStatusBar />
      <AuthNav title="Verify" />
      <div style={{ flex: 1, padding: '10px 26px 22px', display: 'flex', flexDirection: 'column' }}>
        <div style={{ fontFamily: 'var(--sans)', fontSize: 27, fontWeight: 700, letterSpacing: '-0.02em', lineHeight: 1.12, color: 'var(--ink)', textWrap: 'balance' }}>Enter your code.</div>
        <div style={{ fontFamily: 'var(--sans)', fontSize: 15, lineHeight: 1.5, color: 'var(--ink-2)', marginTop: 10, textWrap: 'pretty' }}>We texted a 6-digit code to the number ending <span style={{ fontWeight: 600, color: 'var(--ink)' }}>•• 41</span>. Enter it to keep your account secure.</div>
        <div style={{ marginTop: 30 }}><CodeBoxes digits="418" /></div>
        <div style={{ textAlign: 'center', marginTop: 18, fontFamily: 'var(--mono)', fontSize: 12, letterSpacing: '0.06em', color: 'var(--ink-3)', fontWeight: 600 }}>RESEND IN 0:24</div>
        <div style={{ marginTop: 22 }}><PrimaryCTA label="VERIFY" /></div>
        <div style={{ flex: 1 }} />
        <div style={{ textAlign: 'center' }}><TextLink>Use a different method</TextLink></div>
      </div>
      <AHome />
    </APhone>
  );
}

Object.assign(window, { AuthLogin, AuthLoginError, AuthForgot, AuthCheckInbox, AuthVerify });
