// BITS — Landing page
const { PINK, PINK_DEEP, PINK_SOFT, YEL, CYAN, INK } = window.BITS_COLORS;
const SCREENS = window.BITS_SCREENS;
const { PhoneShell } = window;

// Real store + legal links (match what's live in the system)
const APPLE_URL = 'https://apps.apple.com/il/app/bits-birthday-reminder/id6775375836?l=he';
const PLAY_URL  = 'https://play.google.com/store/apps/details?id=com.bits.birthday';
const PRIVACY_URL = '/privacy';
const TERMS_URL   = '/terms';

const { useState, useEffect } = React;

// Live count of real registered families (demo family excluded by the API).
function useFamilyCount() {
  const [n, setN] = useState(null);
  useEffect(() => {
    let alive = true;
    fetch('/api/stats')
      .then(r => r.json())
      .then(d => { if (alive) setN(typeof d.families === 'number' ? d.families : null); })
      .catch(() => { if (alive) setN(null); });
    return () => { alive = false; };
  }, []);
  return n;
}
function familyLabel(n) {
  if (n === 1) return 'משפחה אחת';
  return n.toLocaleString('he-IL') + ' משפחות';
}

// ── Scaled, rounded phone mockup that reuses the real app screens ──
function PhoneMock({ screen, width = 280, float = false, delay = 0, tilt = 0 }) {
  const OUTER_W = 740, OUTER_H = 1320; // PhoneShell incl. 10px borders
  const scale = width / OUTER_W;
  const Screen = SCREENS[screen] || SCREENS.home;
  return (
    <div style={{
      width: width,
      height: OUTER_H * scale,
      position: 'relative',
      borderRadius: 64 * scale,
      overflow: 'hidden',
      boxShadow: '0 40px 80px -20px rgba(192,38,211,.35), 0 12px 28px rgba(42,27,46,.18)',
      transform: `rotate(${tilt}deg)`,
      animation: float ? `floaty 6s ease-in-out ${delay}s infinite` : 'none',
      flexShrink: 0,
    }}>
      <div style={{ zoom: scale, width: OUTER_W, height: OUTER_H }}>
        <PhoneShell><Screen/></PhoneShell>
      </div>
    </div>
  );
}

// ── Store badges ──
function AppleBadge({ big = false }) {
  const h = big ? 60 : 50;
  return (
    <a href={APPLE_URL} target="_blank" rel="noopener noreferrer" className="badge" style={{ height: h }}>
      <svg viewBox="0 0 24 24" width={h*0.46} height={h*0.46} fill="#fff" style={{ marginInlineEnd: 4 }}>
        <path d="M17.05 12.04c-.03-2.6 2.12-3.85 2.22-3.91-1.21-1.77-3.1-2.01-3.77-2.04-1.6-.16-3.13.94-3.94.94-.81 0-2.07-.92-3.4-.9-1.75.03-3.36 1.02-4.26 2.58-1.82 3.15-.46 7.81 1.3 10.37.86 1.25 1.88 2.65 3.22 2.6 1.29-.05 1.78-.83 3.34-.83 1.56 0 2 .83 3.37.81 1.39-.03 2.27-1.27 3.12-2.53.98-1.45 1.39-2.85 1.41-2.92-.03-.01-2.7-1.04-2.73-4.12zM14.6 4.42c.71-.86 1.19-2.06 1.06-3.25-1.02.04-2.26.68-2.99 1.54-.66.76-1.23 1.98-1.08 3.15 1.14.09 2.3-.58 3.01-1.44z"/>
      </svg>
      <span style={{ textAlign: 'right', lineHeight: 1.1 }}>
        <span style={{ display: 'block', fontSize: big ? 11 : 10, opacity: .85, fontWeight: 500 }}>הורידו ב־</span>
        <span style={{ display: 'block', fontSize: big ? 21 : 18, fontWeight: 700, letterSpacing: '-.01em' }}>App Store</span>
      </span>
    </a>
  );
}
function GoogleBadge({ big = false }) {
  const h = big ? 60 : 50;
  return (
    <a href={PLAY_URL} target="_blank" rel="noopener noreferrer" className="badge" style={{ height: h }}>
      <svg viewBox="0 0 24 24" width={h*0.46} height={h*0.46} style={{ marginInlineEnd: 4 }}>
        <path d="M3.6 1.3C3.3 1.6 3.1 2.1 3.1 2.7v18.6c0 .6.2 1.1.5 1.4l.1.1L14 12.6v-.2L3.6 1.3z" fill="#00D9FF"/>
        <path d="M17.4 16.1L14 12.7v-.2l3.4-3.4.1.1 4.1 2.3c1.2.7 1.2 1.8 0 2.4l-4.2 2.2z" fill="#FFD23F"/>
        <path d="M17.5 16l-3.5-3.5L3.6 22.7c.4.4 1.1.5 1.8.1l12.1-6.8z" fill="#FF4D6D"/>
        <path d="M17.5 9L5.4 1.2C4.7.8 4 .9 3.6 1.3L14 12.5 17.5 9z" fill="#34D399"/>
      </svg>
      <span style={{ textAlign: 'right', lineHeight: 1.1 }}>
        <span style={{ display: 'block', fontSize: big ? 11 : 10, opacity: .85, fontWeight: 500 }}>זמין ב־</span>
        <span style={{ display: 'block', fontSize: big ? 21 : 18, fontWeight: 700, letterSpacing: '-.01em' }}>Google Play</span>
      </span>
    </a>
  );
}

function Logo({ size = 40 }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
      <div style={{
        width: size, height: size, borderRadius: size * 0.3,
        background: `linear-gradient(135deg, ${PINK}, ${PINK_DEEP})`,
        display: 'grid', placeItems: 'center', fontSize: size * 0.58,
        boxShadow: `0 6px 16px ${PINK}55`,
      }}>🎂</div>
      <span style={{ fontWeight: 900, fontSize: size * 0.62, color: INK, letterSpacing: '-.03em' }}>BITS</span>
    </div>
  );
}

// ── Sections ──
function Nav() {
  return (
    <nav className="nav">
      <Logo size={38}/>
      <div className="nav-links">
        <a href="#features">תכונות</a>
        <a href="#how">איך זה עובד</a>
        <a href="#gallery">מסכים</a>
      </div>
      <a href="#download" className="nav-cta">הורידו עכשיו</a>
    </nav>
  );
}

function Hero() {
  const fam = useFamilyCount();
  return (
    <header className="hero">
      <div className="hero-bg"/>
      <div className="hero-inner">
        <div className="hero-text">
          <div className="hero-pill">🎉 חדש · בעברית מלאה</div>
          <h1>אף יום הולדת<br/><span className="hl">לא נשכח שוב</span></h1>
          <p>BITS שומרת לכם את כל ימי ההולדת של המשפחה במקום אחד — עם תזכורות חמות, ברכות בלחיצה, ומשלוח מתנות ישירות מהאפליקציה.</p>
          <div className="hero-badges">
            <AppleBadge big/>
            <GoogleBadge big/>
          </div>
          <div className="hero-meta">
            <span>⭐️ 4.9 דירוג</span>
            {fam != null && fam > 0 && <span>·</span>}
            {fam != null && fam > 0 && <span>👨‍👩‍👧‍👦 {familyLabel(fam)}</span>}
          </div>
        </div>
        <div className="hero-phones">
          <PhoneMock screen="dashboard" width={300} tilt={-4} float delay={0}/>
          <div className="hero-phone-back">
            <PhoneMock screen="message" width={230} tilt={6} float delay={1.2}/>
          </div>
        </div>
      </div>
      <svg className="hero-wave" viewBox="0 0 1440 80" preserveAspectRatio="none"><path d="M0 40 Q 360 80 720 40 T 1440 30 L1440 80 L0 80 Z" fill="#fff"/></svg>
    </header>
  );
}

function Features() {
  const items = [
    { e: '🔔', t: 'תזכורות חכמות', d: 'התראה כמה ימים לפני כל יום הולדת — לעולם לא תפספסו.', c: PINK },
    { e: '👨‍👩‍👧‍👦', t: 'משפחה אחת משותפת', d: 'כל בני המשפחה רואים את אותם התאריכים, הגילאים והפרטים.', c: CYAN },
    { e: '💌', t: 'ברכות בלחיצה', d: 'הודעות מוכנות עם אימוג׳ים — שלחו אהבה בשנייה.', c: YEL },
    { e: '🎁', t: 'משלוח מתנות', d: 'פרחים, עוגות ומתנות מספקים מובחרים — ישר לחוגג.', c: PINK },
    { e: '👑', t: 'ניהול מלא', d: 'המנהל מוסיף, מסיר וממנה — שליטה מלאה על המשפחה.', c: CYAN },
    { e: '🔐', t: 'מאובטח ופרטי', d: 'אימות OTP והגבלת שתי משפחות למספר — בטוח לחלוטין.', c: YEL },
  ];
  return (
    <section id="features" className="section">
      <div className="section-head">
        <div className="eyebrow">למה BITS</div>
        <h2>הכל ליום הולדת מושלם<br/>במקום אחד 🎂</h2>
      </div>
      <div className="feat-grid">
        {items.map((it, i) => (
          <div className="feat-card" key={i}>
            <div className="feat-icon" style={{ background: `${it.c}22`, color: it.c }}>{it.e}</div>
            <h3>{it.t}</h3>
            <p>{it.d}</p>
          </div>
        ))}
      </div>
    </section>
  );
}

function HowItWorks() {
  const steps = [
    { n: '1', screen: 'create', t: 'הקימו משפחה', d: 'צרו משפחה חדשה והוסיפו בני משפחה מאנשי הקשר תוך שניות.', c: PINK },
    { n: '2', screen: 'dashboard', t: 'קבלו תזכורות', d: 'ראו מי החוגג הבא, ספירה לאחור, גיל ותאריך לכל אחד.', c: CYAN },
    { n: '3', screen: 'gift', t: 'חגגו ושלחו', d: 'שלחו ברכה חמה או מתנה אמיתית — הכל מתוך האפליקציה.', c: YEL },
  ];
  return (
    <section id="how" className="section how">
      <div className="section-head">
        <div className="eyebrow">פשוט להתחיל</div>
        <h2>שלושה צעדים לחגיגה 🎈</h2>
      </div>
      <div className="how-rows">
        {steps.map((s, i) => (
          <div className={`how-row ${i % 2 ? 'rev' : ''}`} key={i}>
            <div className="how-phone">
              <PhoneMock screen={s.screen} width={260} tilt={i % 2 ? 3 : -3}/>
            </div>
            <div className="how-copy">
              <div className="how-num" style={{ background: s.c }}>{s.n}</div>
              <h3>{s.t}</h3>
              <p>{s.d}</p>
            </div>
          </div>
        ))}
      </div>
    </section>
  );
}

function Gallery() {
  const screens = ['home', 'dashboard', 'message', 'gift', 'settings', 'join'];
  return (
    <section id="gallery" className="section gallery">
      <div className="section-head">
        <div className="eyebrow">הצצה פנימה</div>
        <h2>מעוצב לשמח 💖</h2>
      </div>
      <div className="gallery-scroll">
        {screens.map((s, i) => (
          <PhoneMock key={s} screen={s} width={230} tilt={0}/>
        ))}
      </div>
    </section>
  );
}

function DownloadCTA() {
  return (
    <section id="download" className="cta">
      <div className="cta-inner">
        <div style={{ fontSize: 64 }}>🎂</div>
        <h2>מוכנים לזכור כל חגיגה?</h2>
        <p>הצטרפו למשפחות ש־BITS עוזרת להן לשמור על הקשר. הורדה חינם.</p>
        <div className="cta-badges">
          <AppleBadge big/>
          <GoogleBadge big/>
        </div>
      </div>
    </section>
  );
}

function Footer() {
  return (
    <footer className="footer">
      <Logo size={34}/>
      <div className="footer-links">
        <a href="#features">תכונות</a>
        <a href="#how">איך זה עובד</a>
        <a href="#download">הורדה</a>
        <a href={PRIVACY_URL}>פרטיות</a>
        <a href={TERMS_URL}>תנאי שימוש</a>
      </div>
      <div className="footer-copy">© 2026 BITS · נבנה באהבה ❤️</div>
    </footer>
  );
}

function Landing() {
  return (
    <div>
      <Nav/>
      <Hero/>
      <Features/>
      <HowItWorks/>
      <Gallery/>
      <DownloadCTA/>
      <Footer/>
    </div>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<Landing/>);
