// Hero, Features, Marquee, Final CTA, Footer

const CRED_PALETTE = {
  red: '#E53E3E',
  yellow: '#E8C547',
  green: '#10B981'
};

// Tri-color credit bar — used as architectural element across the site
const CredBar = ({ height = 3, style }) => (
  <div style={{
    height,
    background: `linear-gradient(90deg, ${CRED_PALETTE.red} 0%, ${CRED_PALETTE.red} 33%, ${CRED_PALETTE.yellow} 33%, ${CRED_PALETTE.yellow} 66%, ${CRED_PALETTE.green} 66%, ${CRED_PALETTE.green} 100%)`,
    width: '100%',
    ...style
  }}></div>
);

// Continuous gradient version
const CredGradient = ({ height = 2, style }) => (
  <div style={{
    height,
    background: `linear-gradient(90deg, ${CRED_PALETTE.red} 0%, ${CRED_PALETTE.yellow} 50%, ${CRED_PALETTE.green} 100%)`,
    width: '100%',
    ...style
  }}></div>
);

const Eyebrow = ({ children, color }) => (
  <div style={{
    fontFamily: 'var(--mono)',
    fontSize: 11,
    letterSpacing: '0.2em',
    textTransform: 'uppercase',
    color: color || 'var(--muted)',
    display: 'flex',
    alignItems: 'center',
    gap: 10
  }}>
    <span style={{
      width: 24, height: 2,
      background: `linear-gradient(90deg, ${CRED_PALETTE.red}, ${CRED_PALETTE.yellow}, ${CRED_PALETTE.green})`
    }}></span>
    {children}
  </div>
);

const EmailForm = ({ ctaText, accent, compact, onSubmit }) => {
  const [email, setEmail] = React.useState('');
  const [status, setStatus] = React.useState(() => window.getSportsCredWaitlistEmail?.() ? 'saved' : 'idle');

  React.useEffect(() => {
    const handleJoined = () => setStatus('saved');
    window.addEventListener('sportscred:waitlist-joined', handleJoined);
    return () => window.removeEventListener('sportscred:waitlist-joined', handleJoined);
  }, []);

  const handle = async (e) => {
    e.preventDefault();
    if (!email || status === 'saving') return;
    setStatus('saving');
    const result = onSubmit
      ? await onSubmit(email)
      : await window.saveWaitlistLead?.({ email, source: 'waitlist_form' });
    setStatus(result?.ok === true ? 'saved' : 'error');
  };
  if (status === 'saved' || status === 'error') {
    const isSaved = status === 'saved';
    return (
      <div className="sc-email-submitted" style={{
        display: 'flex', alignItems: 'center', gap: 12,
        padding: '18px 22px',
        border: `1px solid ${isSaved ? accent : CRED_PALETTE.red}`,
        background: 'rgba(255,255,255,0.02)',
        color: isSaved ? accent : CRED_PALETTE.red,
        fontFamily: 'var(--mono)',
        fontSize: 13,
        letterSpacing: '0.05em'
      }}>
        <span style={{ width: 8, height: 8, background: isSaved ? accent : CRED_PALETTE.red, borderRadius: '50%' }}></span>
        {isSaved ? "You're on the list." : 'SAVE FAILED. CHECK SUPABASE SETUP AND TRY AGAIN.'}
      </div>
    );
  }
  return (
    <form className="sc-email-form" data-waitlist-form="true" onSubmit={handle} style={{
      display: 'flex',
      gap: 0,
      maxWidth: compact ? 480 : 560,
      width: '100%',
      border: '1px solid rgba(255,255,255,0.12)',
      background: 'rgba(255,255,255,0.02)',
      transition: 'border-color 200ms'
    }}
    onFocusCapture={(e) => e.currentTarget.style.borderColor = accent}
    onBlurCapture={(e) => e.currentTarget.style.borderColor = 'rgba(255,255,255,0.12)'}
    >
      <input
        type="email"
        required
        placeholder="you@email.com"
        value={email}
        onChange={(e) => setEmail(e.target.value)}
        style={{
          flex: 1,
          background: 'transparent',
          border: 'none',
          outline: 'none',
          color: '#fff',
          fontFamily: 'var(--body)',
          fontSize: 15,
          padding: '20px 22px',
          letterSpacing: '0.01em'
        }}
      />
      <button className="sc-email-submit" type="submit" disabled={status === 'saving'} style={{
        background: accent,
        color: '#0A0A0A',
        border: 'none',
        padding: '0 28px',
        fontFamily: 'var(--display)',
        fontSize: 18,
        letterSpacing: '0.06em',
        cursor: 'pointer',
        fontWeight: 400,
        opacity: status === 'saving' ? 0.72 : 1,
        transition: 'filter 150ms'
      }}
      onMouseEnter={(e) => e.currentTarget.style.filter = 'brightness(1.1)'}
      onMouseLeave={(e) => e.currentTarget.style.filter = 'brightness(1)'}
      >
        {status === 'saving' ? 'Saving...' : ctaText}
      </button>
    </form>
  );
};

const Hero = ({ accent, showCounter = true }) => {
  return (
    <section data-screen-label="01 Hero" style={{
      minHeight: '100vh',
      position: 'relative',
      padding: '32px 56px 80px',
      display: 'flex',
      flexDirection: 'column',
      overflow: 'hidden',
      borderBottom: '1px solid rgba(255,255,255,0.06)'
    }}>
      {/* top cred bar — credit-gauge palette anchor */}
      <CredGradient height={3} style={{ position: 'absolute', top: 0, left: 0, right: 0, zIndex: 3 }} />
      {/* animated gradient mesh */}
      <div className="hero-mesh" style={{
        position: 'absolute',
        inset: 0,
        zIndex: 0,
        opacity: 0.5,
        pointerEvents: 'none'
      }}></div>
      {/* grain */}
      <div style={{
        position: 'absolute',
        inset: 0,
        zIndex: 0,
        opacity: 0.04,
        mixBlendMode: 'overlay',
        backgroundImage: 'url("data:image/svg+xml;utf8,<svg xmlns=%22http://www.w3.org/2000/svg%22 width=%22200%22 height=%22200%22><filter id=%22n%22><feTurbulence baseFrequency=%220.9%22/></filter><rect width=%22100%25%22 height=%22100%25%22 filter=%22url(%23n)%22/></svg>")',
        pointerEvents: 'none'
      }}></div>

      {/* nav removed — sticky brand bar at page top handles this now */}

      {/* hero content + ACS card side-by-side */}
      <div style={{
        flex: 1,
        display: 'flex',
        alignItems: 'center',
        gap: 60,
        position: 'relative',
        zIndex: 2,
        marginTop: 0
      }}>
        <div style={{
          flex: 1,
          minWidth: 0,
          maxWidth: 760
        }}>
        <Eyebrow color={accent}>Waitlist · Now Open</Eyebrow>

        <h1 style={{
          fontFamily: 'var(--display)',
          fontSize: 'clamp(56px, 8vw, 124px)',
          lineHeight: 0.92,
          letterSpacing: '-0.01em',
          margin: '32px 0 28px',
          color: '#fff',
          fontWeight: 400
        }}>
          Your sports<br/>
          credibility<br/>
          <span style={{
            background: `linear-gradient(90deg, ${CRED_PALETTE.red}, ${CRED_PALETTE.yellow}, ${CRED_PALETTE.green})`,
            WebkitBackgroundClip: 'text',
            WebkitTextFillColor: 'transparent',
            backgroundClip: 'text',
            fontStyle: 'italic'
          }}>starts here.</span>
        </h1>

        <p style={{
          fontFamily: 'var(--body)',
          fontSize: 'clamp(17px, 1.5vw, 21px)',
          lineHeight: 1.5,
          color: 'var(--muted)',
          maxWidth: 620,
          margin: '0 0 48px'
        }}>
          SportsCred is the platform where sports knowledge is currency.
          Your takes deserve more than likes — join the waitlist and be first in.
        </p>

        <EmailForm ctaText="Join the Waitlist" accent={accent} onSubmit={(email) => window.saveWaitlistLead?.({ email, source: 'waitlist_form' })} />

        {/* Founding member perk pill */}
        <div style={{
          marginTop: 20,
          display: 'inline-flex',
          alignItems: 'center',
          gap: 14,
          padding: '12px 18px',
          border: `1px solid ${CRED_PALETTE.green}40`,
          background: `${CRED_PALETTE.green}08`,
          width: 'fit-content',
          maxWidth: 560,
          flexWrap: 'wrap'
        }}>
          <span style={{
            width: 8, height: 8,
            borderRadius: '50%',
            background: CRED_PALETTE.green,
            boxShadow: `0 0 12px ${CRED_PALETTE.green}`,
            flexShrink: 0
          }}></span>
          <span style={{
            fontFamily: 'var(--mono)',
            fontSize: 11,
            letterSpacing: '0.16em',
            textTransform: 'uppercase',
            color: '#fff',
            fontWeight: 500
          }}>Founding Member Perks</span>
          <span style={{
            fontFamily: 'var(--mono)',
            fontSize: 11,
            letterSpacing: '0.12em',
            textTransform: 'uppercase',
            color: 'var(--muted)'
          }}>
            Verified status · Bonus ACS · Locked in for life
          </span>
        </div>

        <div style={{
          marginTop: 14,
          display: 'flex',
          gap: 28,
          fontFamily: 'var(--mono)',
          fontSize: 11,
          letterSpacing: '0.12em',
          textTransform: 'uppercase',
          color: '#555'
        }}>
          <span>No spam.</span>
          <span>·</span>
          <span>Just early access.</span>
        </div>
        </div>

        {/* Right column — ACS card */}
        <div className="hero-card-col" style={{
          flex: '0 0 auto',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center'
        }}>
          <ACSCard accent={accent} />
        </div>
      </div>

      {/* corner stat — moved to bottom-left so it doesn't clash with the card */}
      {showCounter && <div className="hero-corner" style={{
        position: 'absolute',
        bottom: 28,
        left: 56,
        textAlign: 'left',
        zIndex: 2,
        display: 'flex',
        alignItems: 'baseline',
        gap: 12
      }}>
        <div style={{
          fontFamily: 'var(--display)',
          fontSize: 28,
          color: accent,
          lineHeight: 1,
          letterSpacing: '0.02em'
        }}>
          12,847
        </div>
        <div style={{
          fontFamily: 'var(--mono)',
          fontSize: 11,
          letterSpacing: '0.18em',
          textTransform: 'uppercase',
          color: 'var(--muted)',
          marginTop: 0
        }}>
          fans on the list
        </div>
      </div>}
    </section>
  );
};

const Logo = ({ accent, size = 28 }) => {
  return (
    <div style={{ display: 'flex', alignItems: 'center', lineHeight: 1 }}>
      <span style={{
        fontFamily: 'var(--display)',
        fontSize: 22,
        letterSpacing: '0.08em',
        color: '#fff',
        lineHeight: 1,
        display: 'inline-block'
      }}>SPORTSCRED</span>
    </div>
  );
};

const Features = ({ accent }) => {
  const items = [
    {
      n: "01",
      t: "Just Sports",
      d: "A platform built for one thing only. No noise, no distractions, no diet content. The whole feed is the game.",
      tier: CRED_PALETTE.red,
      grade: "C"
    },
    {
      n: "02",
      t: "Earn Your Cred",
      d: "Your ACS Score — Analysis, Credibility, Sports IQ — grows the smarter you engage. Followers don't matter. Receipts do.",
      tier: CRED_PALETTE.yellow,
      grade: "B"
    },
    {
      n: "03",
      t: "Real Fan Energy",
      d: "Casuals, die-hards, fantasy minds, and analysts. Every fan tier has a voice — and a way to prove it.",
      tier: CRED_PALETTE.green,
      grade: "A"
    }
  ];
  return (
    <section id="what" data-screen-label="02 What is SportsCred" style={{
      padding: '120px 56px',
      borderBottom: '1px solid rgba(255,255,255,0.06)'
    }}>
      <div style={{ maxWidth: 1400, margin: '0 auto' }}>
        <Eyebrow>What is SportsCred</Eyebrow>
        <h2 style={{
          fontFamily: 'var(--display)',
          fontSize: 'clamp(40px, 5vw, 72px)',
          lineHeight: 1,
          letterSpacing: '-0.01em',
          color: '#fff',
          margin: '24px 0 80px',
          maxWidth: 900,
          fontWeight: 400
        }}>
          A home for fans who actually <span style={{ color: accent, fontStyle: 'italic' }}>watch the tape.</span>
        </h2>

        <div className="sc-feature-grid" style={{
          display: 'grid',
          gridTemplateColumns: 'repeat(3, 1fr)',
          gap: 1,
          background: 'rgba(255,255,255,0.06)',
          border: '1px solid rgba(255,255,255,0.06)'
        }}>
          {items.map((it, i) => (
            <div key={i} className="sc-feature-card" style={{
              padding: '48px 36px 56px',
              background: '#0A0A0A',
              minHeight: 360,
              display: 'flex',
              flexDirection: 'column',
              position: 'relative',
              transition: 'background 250ms',
              borderTop: `2px solid ${it.tier}`
            }}
            onMouseEnter={(e) => e.currentTarget.style.background = '#0F0F0F'}
            onMouseLeave={(e) => e.currentTarget.style.background = '#0A0A0A'}
            >
              {/* tier letter — credit-grade in top right */}
              <div style={{
                position: 'absolute',
                top: 32, right: 32,
                fontFamily: 'var(--display)',
                fontSize: 28,
                color: it.tier,
                letterSpacing: '0.04em',
                opacity: 0.85,
                border: `1.5px solid ${it.tier}`,
                width: 44, height: 44,
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                lineHeight: 1
              }}>{it.grade}</div>
              <div style={{
                fontFamily: 'var(--mono)',
                fontSize: 12,
                letterSpacing: '0.18em',
                color: it.tier,
                marginBottom: 40
              }}>{it.n} · TIER</div>
              <h3 style={{
                fontFamily: 'var(--display)',
                fontSize: 38,
                color: '#fff',
                margin: '0 0 18px',
                fontWeight: 400,
                letterSpacing: '0.01em'
              }}>{it.t}</h3>
              <p style={{
                fontFamily: 'var(--body)',
                fontSize: 16,
                lineHeight: 1.55,
                color: 'var(--muted)',
                margin: 0,
                maxWidth: 360
              }}>{it.d}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
};

const Marquee = ({ accent }) => {
  const lines = [
    "Built for the fan who actually watches the tape.",
    "Your league. Your cred. Your platform.",
    "The sports app fans have been waiting for.",
    "Knowledge is currency.",
    "No followers. Just receipts."
  ];
  const MarqueeGroup = ({ groupIndex }) => (
    <div style={{
      display: 'flex',
      alignItems: 'center',
      gap: 56,
      flex: '0 0 auto',
      paddingRight: 56
    }}>
      {lines.map((ln, i) => {
        const itemIndex = i;
        const credColors = [CRED_PALETTE.red, CRED_PALETTE.yellow, CRED_PALETTE.green];
        const diamondColor = credColors[itemIndex % 3];
        return (
          <React.Fragment key={`${groupIndex}-${ln}`}>
            <span style={{
              fontFamily: 'var(--display)',
              fontSize: 'clamp(36px, 5vw, 64px)',
              color: itemIndex % 2 === 0 ? '#fff' : 'transparent',
              WebkitTextStroke: itemIndex % 2 === 0 ? '0' : `1px ${diamondColor}`,
              letterSpacing: '0.02em',
              fontStyle: itemIndex % 3 === 0 ? 'italic' : 'normal',
              lineHeight: 1
            }}>{ln}</span>
            <span style={{
              color: diamondColor,
              fontFamily: 'var(--display)',
              fontSize: 'clamp(36px, 5vw, 64px)',
              lineHeight: 1
            }}>◆</span>
          </React.Fragment>
        );
      })}
    </div>
  );

  return (
    <section className="sc-marquee-section" data-screen-label="04 Marquee" style={{
      height: 'clamp(132px, 12vw, 184px)',
      borderBottom: '1px solid rgba(255,255,255,0.06)',
      overflow: 'hidden',
      background: '#070707',
      position: 'relative',
      contain: 'layout paint',
      display: 'flex',
      alignItems: 'center'
    }}>
      <div className="marquee-track sc-marquee-track" style={{
        display: 'flex',
        alignItems: 'center',
        whiteSpace: 'nowrap',
        width: 'max-content',
        maxWidth: 'none',
        willChange: 'transform',
        animation: 'marquee 10s linear infinite'
      }}>
        <MarqueeGroup groupIndex={0} />
        <MarqueeGroup groupIndex={1} />
      </div>
    </section>
  );
};

const FinalCTA = ({ accent }) => {
  return (
    <section id="join" className="sc-final-cta" data-screen-label="05 Final CTA" style={{
      padding: '160px 56px',
      textAlign: 'center',
      position: 'relative',
      borderBottom: '1px solid rgba(255,255,255,0.06)'
    }}>
      {/* credit palette gauge above CTA */}
      <CredGradient height={2} style={{ position: 'absolute', top: 0, left: '50%', transform: 'translateX(-50%)', maxWidth: 600 }} />
      <div className="sc-cta-orb" style={{
        position: 'absolute',
        top: '50%', left: '50%',
        transform: 'translate(-50%, -50%)',
        width: 800, height: 800,
        background: `radial-gradient(circle, ${CRED_PALETTE.green}10 0%, ${CRED_PALETTE.yellow}08 35%, transparent 65%)`,
        zIndex: 0,
        pointerEvents: 'none'
      }}></div>

      <div style={{ position: 'relative', zIndex: 1, maxWidth: 900, margin: '0 auto' }}>
        <Eyebrow color={accent}>Limited Early Access</Eyebrow>
        <h2 className="sc-final-heading" style={{
          fontFamily: 'var(--display)',
          fontSize: 'clamp(48px, 7vw, 112px)',
          lineHeight: 0.95,
          letterSpacing: 0,
          color: '#fff',
          margin: '28px 0 24px',
          fontWeight: 400
        }}>
          Be first.<br/>
          <span style={{
            background: `linear-gradient(90deg, ${CRED_PALETTE.red}, ${CRED_PALETTE.yellow}, ${CRED_PALETTE.green})`,
            WebkitBackgroundClip: 'text',
            WebkitTextFillColor: 'transparent',
            backgroundClip: 'text',
            fontStyle: 'italic'
          }}>Build your cred early.</span>
        </h2>
        <p style={{
          fontFamily: 'var(--body)',
          fontSize: 18,
          color: 'var(--muted)',
          maxWidth: 520,
          margin: '0 auto 48px',
          lineHeight: 1.5
        }}>
          SportsCred is coming. The earlier you're in, the bigger the head start.
          Founding members get verified status and ACS bonus points.
        </p>
        <div style={{ display: 'flex', justifyContent: 'center' }}>
          <EmailForm ctaText="Secure My Spot" accent={accent} onSubmit={(email) => window.saveWaitlistLead?.({ email, source: 'waitlist_form' })} />
        </div>
      </div>
    </section>
  );
};

const Footer = ({ accent }) => {
  const Social = ({ label, href }) => (
    <a href={href} target="_blank" rel="noopener noreferrer" aria-label="SportsCred on X" style={{
      width: 38, height: 38,
      border: '1px solid rgba(255,255,255,0.12)',
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center',
      color: '#888',
      textDecoration: 'none',
      fontFamily: 'var(--mono)',
      fontSize: 11,
      transition: 'all 200ms'
    }}
    onMouseEnter={(e) => {
      e.currentTarget.style.borderColor = accent;
      e.currentTarget.style.color = accent;
    }}
    onMouseLeave={(e) => {
      e.currentTarget.style.borderColor = 'rgba(255,255,255,0.12)';
      e.currentTarget.style.color = '#888';
    }}
    >{label}</a>
  );
  return (
    <footer data-screen-label="06 Footer" style={{
      padding: '64px 56px 40px',
      background: '#070707',
      position: 'relative'
    }}>
      <CredGradient height={2} style={{ position: 'absolute', top: 0, left: 0, right: 0 }} />
      <div className="sc-footer-main" style={{
        maxWidth: 1400,
        margin: '0 auto',
        display: 'flex',
        justifyContent: 'space-between',
        alignItems: 'flex-start',
        flexWrap: 'wrap',
        gap: 40
      }}>
        <div>
          <Logo accent={accent} />
          <div style={{
            marginTop: 16,
            fontFamily: 'var(--mono)',
            fontSize: 12,
            letterSpacing: '0.16em',
            textTransform: 'uppercase',
            color: 'var(--muted)'
          }}>
            Credibility.
          </div>
        </div>
        <div style={{ display: 'flex', gap: 36, alignItems: 'center', flexWrap: 'wrap' }}>
          <div style={{ display: 'flex', gap: 8 }}>
            <Social label="X" href="https://x.com/sportscred1?s=11&t=ouyGJkPJD8vAYyC1nx38JQ" />
          </div>
        </div>
      </div>
      <div className="sc-footer-bottom" style={{
        maxWidth: 1400,
        margin: '64px auto 0',
        paddingTop: 28,
        borderTop: '1px solid rgba(255,255,255,0.06)',
        display: 'flex',
        justifyContent: 'space-between',
        fontFamily: 'var(--mono)',
        fontSize: 11,
        letterSpacing: '0.14em',
        textTransform: 'uppercase',
        color: '#555'
      }}>
        <span>© 2026 SportsCred</span>
        <span>v0.1 — Pre-launch</span>
      </div>
    </footer>
  );
};

Object.assign(window, { Hero, Features, Marquee, FinalCTA, Footer, Eyebrow, EmailForm, Logo, CredBar, CredGradient, CRED_PALETTE });
