/* Reco landing — section components. Exports: RecoLanding */

const { useState, useEffect, useRef } = React;

const DEMO_BIZ = 'קפה הציפור הכחולה';
const goStart = () => { window.location.href = '/onboard.html'; };

// ── Phone showcase: auto-rotating product screens, scaled into the hero
function PhoneShowcase({ scale = 0.66 }) {
  const DAY = 864e5;
  const demoTk = { startsAt: Date.now() + DAY, endsAt: Date.now() + 4 * DAY };       // a gift issued today starts tomorrow
  const friendTk = { startsAt: Date.now() - DAY, endsAt: Date.now() + 2 * DAY };     // the friend sees it once it is usable
  const screens = [
    () => <ReviewLanding business={DEMO_BIZ} />,
    () => <CouponUnlocked unlocking business={DEMO_BIZ} reward="קפה חינם בביקור הראשון" days={3} tk={demoTk} sender="מאיה" />,
    () => <FriendReferral business={DEMO_BIZ} friend="מאיה" reward="קפה חינם בביקור הראשון" days={3} tk={friendTk} views={4} rating={4.9} reviewCount={230} noPhoto onClaim={() => {}} />,
  ];
  const labels = ['סורקים וכותבים ביקורת', 'ה-Reco נפתח', 'משתפים עם חבר'];
  const [i, setI] = useState(0);
  const [round, setRound] = useState(0);   // a new screen is mounted fresh each time, so its opening animation and confetti play
  useEffect(() => {
    const t = setInterval(() => { setI(v => (v + 1) % screens.length); setRound(n => n + 1); }, 5200);
    return () => clearInterval(t);
  }, []);
  return (
    <div style={{ position: 'relative', width: 402 * scale, height: 874 * scale }}>
      <div key={round} style={{
        position: 'absolute', top: 0, left: 0, width: 402, height: 874,
        transform: `scale(${scale})`, transformOrigin: 'top left',
        animation: 'll-fadein 0.5s var(--iv-ease-soft) both', pointerEvents: 'none',
      }}>
        <IOSDevice>{screens[i]()}</IOSDevice>
      </div>
      {/* step dots */}
      <div style={{ position: 'absolute', bottom: -34, left: 0, right: 0, display: 'flex',
                    gap: 8, alignItems: 'center', justifyContent: 'center' }}>
        {labels.map((_, idx) => (
          <span key={idx} style={{ width: i === idx ? 26 : 8, height: 8, borderRadius: 99,
                    background: i === idx ? 'var(--reco-green)' : 'var(--iv-line-strong)',
                    transition: 'all 0.4s var(--iv-ease-out)' }} />
        ))}
      </div>
    </div>
  );
}

// ── Sticky nav
// ── The brand sky frames the page: the hero is the sky and lands on a horizon of clouds; the closing section rises
// back through the clouds into the sky. In between the page stays calm; the only thread is the flight over the steps.
// The parrots are cut from the brand banner (assets/flyer) and always fly right to left, the way Hebrew reads.
const CLOUD_D = 'M0 60 C0 42 14 30 30 32 C34 12 52 0 72 4 C88 -4 110 4 116 22 C134 20 150 34 148 52 C148 57 147 60 146 60 Z';
// each cloud: [left %, top px, width px, opacity, drift seconds]
function Drift({ clouds }) {
  return clouds.map(([x, y, w, o = 1, s = 60], i) => (
    <svg key={i} className="ll-drift" viewBox="0 0 150 62" aria-hidden="true"
         style={{ left: `${x}%`, top: y, width: w, opacity: o, animationDuration: `${s}s`, animationDelay: `${-i * 9}s` }}>
      <path d={CLOUD_D} fill="var(--cloud)" />
    </svg>
  ));
}
const Bird = ({ n, className = '', style }) => <img className={`ll-bird ${className}`} src={`/assets/flyer/p${n}.png`} alt="" aria-hidden="true" style={style} />;

// rows of overlapping cloud puffs, drawn 2400 wide so even a very wide screen never crops their tops. Sizes vary a lot
// (a few big billows between small ones), and each row is shaded light-to-deeper so the clouds have volume.
function puffs(seed, y0, rMin, rMax) {
  let s = seed, x = -120, d = '';
  const rnd = () => { s = (s * 9301 + 49297) % 233280; return s / 233280; };
  while (x < 2520) {
    const r = rMin + Math.pow(rnd(), 1.6) * (rMax - rMin), cy = y0 + r * (0.28 + rnd() * 0.42);
    d += `M${x - r} ${cy}a${r} ${r} 0 1 0 ${2 * r} 0a${r} ${r} 0 1 0 ${-2 * r} 0`;
    x += r * (0.95 + rnd() * 0.55);
  }
  return d;
}
const y0Of = (seed, kind) => HORIZON[kind].find(l => l[0] === seed)[1];
const HORIZON = {   // [seed, baseline, smallest puff, biggest puff, colour at the top, colour at the bottom]
  ground: [[7, 78, 34, 118, '#F1F9F6', '#D3EAE3'], [31, 122, 30, 92, '#FAFDFC', '#E2F1ED'], [53, 164, 26, 70, 'var(--sky)']],
  night:  [[7, 102, 34, 108, '#E2F2EE', '#C2E1D9'], [53, 156, 28, 76, 'var(--reco-navy)']],
  dawn:   [[11, 102, 34, 108, '#2B476B', '#1B2F4D'], [29, 156, 28, 76, 'var(--sky)']],
};
// ground: the sky lands on the page. night: the page sinks into the dark (the 1:1:1 band, the footer). dawn: out of the dark again.
function Horizon({ kind = 'ground' }) {
  const layers = HORIZON[kind];
  return (
    <svg className={`ll-horizon ${kind}`} viewBox="0 0 2400 200" preserveAspectRatio="xMidYMax slice" aria-hidden="true">
      <defs>
        {layers.map(([seed, , , , top, bottom]) => bottom && (
          <linearGradient key={seed} id={`hz-${kind}-${seed}`} gradientUnits="userSpaceOnUse" x1="0" y1={y0Of(seed, kind) - 110} x2="0" y2="200">
            <stop offset="0" stopColor={top} /><stop offset="1" stopColor={bottom} />
          </linearGradient>
        ))}
      </defs>
      {layers.map(([seed, y, r0, r1, top, bottom]) => {
        const fill = bottom ? `url(#hz-${kind}-${seed})` : top;
        return <g key={seed} fill={fill}><path d={puffs(seed, y, r0, r1)} /><rect x="-140" y={y + r0 * 0.25} width="2680" height="260" /></g>;
      })}
    </svg>
  );
}

function Nav() {
  const link = (href, label) => (
    <a href={href} className="ll-navlink">{label}</a>
  );
  return (
    <header className="ll-nav">
      <div className="ll-nav-inner">
        <RecoWordmark height={26} />
        <nav className="ll-nav-links">
          {link('#how', 'איך זה עובד')}
          {link('#pricing', 'תמחור')}
          {link('#dashboard', 'לוח בקרה')}
        </nav>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
          <a href="/onboard.html?signin=1" className="ll-navlink ll-hide-mobile">התחברות</a>
          <RecoButton full={false} size="sm" onClick={goStart}>מתחילים</RecoButton>
        </div>
      </div>
    </header>
  );
}

// ── Hero
function Hero() {
  return (
    <section className="ll-hero">
      <Drift clouds={[[6, 130, 190, 0.9, 70], [70, 96, 140, 0.8, 90], [40, 300, 120, 0.55, 80], [84, 420, 170, 0.7, 75]]} />
      <Horizon />
      <Bird n={1} className="hb1" />
      <Bird n={3} className="hb2" />
      <div className="ll-hero-grid">
        <div className="ll-hero-copy">
          <span className="ll-eyebrow">
            <span className="ll-eyebrow-dot" /> שוברי המלצה לעסקים מקומיים
          </span>
          <h1 className="ll-h1">
            השיגו לקוחות מפה לאוזן -<br /> <em>באופן שיטתי</em>.
          </h1>
          <p className="ll-lede">
            הפכו כל לקוח מרוצה להזדמנות להביא לקוח חדש. Reco מאפשרת ללקוחות שלכם לשתף חברים בשובר המלצה לעסק,
            אחרי כל דירוג חדש בגוגל.
          </p>
          <div className="ll-hero-cta">
            <RecoButton full={false} onClick={goStart}>מתחילים בחינם</RecoButton>
            <RecoButton full={false} variant="secondary" onClick={() => document.getElementById('video')?.scrollIntoView({ behavior: 'smooth', block: 'start' })} icon={<Icon name="play" size={18} color="var(--reco-navy)" />}>
              צפו בסרטון
            </RecoButton>
          </div>
          <div className="ll-hero-trust">
            <div className="ll-stars">
              {[0,1,2,3,4].map(n => <Icon key={n} name="star" size={16} color="#FFC93C" />)}
            </div>
            <span>בלי דמי הקמה · בלי מנוי חודשי</span>
          </div>
        </div>
        <div className="ll-hero-visual">
          <div className="ll-hero-glow" />
          {/* floating review chip */}
          <div className="ll-float ll-float-review">
            <GMapsGlyph size={22} />
            <div>
              <div style={{ display: 'flex', gap: 1 }}>
                {[0,1,2,3,4].map(n => <Icon key={n} name="star" size={11} color="#FFC93C" />)}
              </div>
              <div style={{ fontSize: 12, color: 'var(--iv-ink-soft)', marginTop: 2 }}>ביקורת 5★ חדשה</div>
            </div>
          </div>
          {/* floating redeemed chip */}
          <div className="ll-float ll-float-redeem">
            <span className="ll-float-ic"><Icon name="check" size={16} color="#fff" /></span>
            <div>
              <div style={{ fontSize: 13, fontWeight: 600, color: 'var(--reco-navy)' }}>השובר מומש</div>
              <div style={{ fontSize: 12, color: 'var(--iv-ink-soft)' }}>לקוח חדש אחד</div>
            </div>
          </div>
          <PhoneShowcase scale={0.66} />
        </div>
      </div>
    </section>
  );
}

// ── How it works
function HowItWorks() {
  const steps = [
    { n: '01', icon: 'scan', title: 'הלקוח סורק וכותב ביקורת', body: 'סורקים את קוד ה-QR בקופה וכותבים ביקורת כנה בגוגל. בלי אפליקציה ובלי הרשמה.' },
    { n: '02', icon: 'gift', title: 'הוא מקבל Reco לשיתוף', body: 'אחרי הביקורת נפתח שובר מתנה בעיצוב העסק שלכם, והוא שולח אותו לחבר בוואטסאפ.' },
    { n: '03', icon: 'users', title: 'החבר מגיע ומממש', body: 'החבר מציג את השובר בקופה ומממש אותו פעם אחת, כלקוח חדש.' },
  ];
  return (
    <section id="how" className="ll-section" style={{ paddingTop: 120 }}>
      <Drift clouds={[[3, 70, 150, 0.6, 85], [82, 210, 120, 0.5, 95]]} />
      <div className="ll-container">
        <div className="ll-head">
          <span className="ll-kicker">איך זה עובד</span>
          <h2 className="ll-h2">שלושה צעדים מלקוח מרוצה<br className="ll-hide-mobile" /> ללקוח חדש לגמרי.</h2>
        </div>
        <div className="ll-steps">
          <svg className="ll-flight" viewBox="0 0 1000 100" preserveAspectRatio="none" aria-hidden="true">
            <path d="M 916 96 C 760 0, 240 0, 84 96" fill="none" stroke="var(--reco-navy)" strokeWidth="2" strokeLinecap="round" strokeDasharray="0.1 9" opacity=".35" vectorEffect="non-scaling-stroke" />
          </svg>
          <Bird n={3} className="ll-flight-bird" />
          {steps.map((s, i) => (
            <div key={s.n} className="ll-step">
              <div className="ll-step-top">
                <span className="ll-step-ic"><Icon name={s.icon} size={26} color="var(--reco-green-deep)" /></span>
                <span className="ll-step-n">{s.n}</span>
              </div>
              <h3 className="ll-step-title">{s.title}</h3>
              <p className="ll-step-body">{s.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ── Benefits grid
function Benefits() {
  const items = [
    { icon: 'star', title: 'יותר ביקורות בגוגל', body: 'כל לקוח מרוצה מקבל סיבה טובה לכתוב ביקורת ברגע הנכון, ליד הקופה, והדירוג שלכם עולה לאורך זמן.' },
    { icon: 'users', title: 'המלצה שמגיעה לחבר', body: 'ביקורת נשארת באינטרנט, ושובר מתנה מגיע ישר לחבר. פה לאוזן, אבל הפעם אפשר למדוד.' },
    { icon: 'trend', title: 'רק לקוחות חדשים', body: 'השובר תקף פעם אחת בלבד, ללקוח חדש. אתם לא מחלקים הנחות ללקוחות שכבר שלכם.' },
    { icon: 'gift', title: 'אתם קובעים את ההטבה', body: 'לשובר גמישות מלאה לעבוד לפי התנאים שלכם, מגדירים פעם אחת - ומעדכנים כשצריך.' },
  ];
  return (
    <section className="ll-section ll-section-warm ll-into-night">
      <Horizon kind="night" />
      <div className="ll-container">
        <div className="ll-head">
          <span className="ll-kicker">למה Reco</span>
          <h2 className="ll-h2">פה לאוזן שעובד בשבילכם –<br className="ll-hide-mobile" /> ואפשר לראות כמה הוא הביא.</h2>
        </div>
        <div className="ll-benefits">
          {items.map(it => (
            <div key={it.title} className="ll-bcard">
              <span className="ll-bcard-ic"><Icon name={it.icon} size={24} color="var(--reco-green-deep)" /></span>
              <h3 className="ll-bcard-title">{it.title}</h3>
              <p className="ll-bcard-body">{it.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ── Trust: one review, one voucher, one redemption, drawn as a chain on a dark band
function Trust() {
  const steps = [
    { title: 'ביקורת אחת', body: 'השובר נפתח רק אחרי כניסה עם חשבון Google, וכל חשבון מקבל שובר אחד בלבד לעסק.',
      art: <div className="ll-tr-art">
        <GMapsGlyph size={30} />
        <div className="ll-tr-stars">{[0, 1, 2, 3, 4].map(n => <Icon key={n} name="star" size={15} color="#FFC93C" />)}</div>
      </div> },
    { title: 'שובר אחד', body: 'לכל לקוח שכתב ביקורת יש שובר המלצה אחד לשלוח לחבר. לא שניים, לא חבילה.',
      art: <div className="ll-tr-art"><div className="ll-tr-ticket"><span /><i /><b /></div></div> },
    { title: 'מימוש אחד', body: 'לפני המימוש החבר מאמת את הטלפון שלו בקוד SMS. כל מספר מממש פעם אחת בעסק, גם אם קיבל כמה שוברים.',
      art: <div className="ll-tr-art"><div className="ll-tr-sms" dir="ltr">{['4', '8', '2', '1'].map((d, i) => <span key={i}>{d}</span>)}</div></div> },
  ];
  const checks = ['השובר בתוקף', 'עוד לא מומש', 'לא בוטל', 'הטלפון אומת ב-SMS'];
  return (
    <section className="ll-trust">
      <div className="ll-container">
        <div className="ll-tr-head">
          <span className="ll-tr-kicker"><Icon name="check" size={14} color="var(--reco-green-light)" /> מאובטח לאורך כל הדרך</span>
          <h2 className="ll-tr-title">היחס תמיד <span dir="ltr">1:1:1</span></h2>
          <p className="ll-tr-sub">כל שלב בדרך מאומת, כדי שתשלמו רק על לקוחות חדשים אמיתיים. אין שוברים כפולים ואין מימושים כפולים.</p>
        </div>
        <ol className="ll-tr-chain">
          {steps.map((st, i) => (
            <li key={st.title} className="ll-tr-step">
              {st.art}
              <h3><span className="ll-tr-one">1</span>{st.title}</h3>
              <p>{st.body}</p>
            </li>
          ))}
        </ol>
        <div className="ll-tr-receipt">
          <div className="ll-tr-rhead">
            <b>מה נבדק בקופה, בכל מימוש</b>
            <span>הבדיקה נעשית בשרת, לא בטלפון של הלקוח, ולכן צילום מסך של שובר לא יעבוד.</span>
          </div>
          <ul>{checks.map(c => <li key={c}><span className="ll-tr-tick"><Icon name="check" size={14} color="#fff" /></span>{c}</li>)}</ul>
        </div>
      </div>
      <Horizon kind="dawn" />
    </section>
  );
}

// ── Reward examples
function Rewards() {
  return (
    <section className="ll-section">
      <div className="ll-container">
        <div className="ll-head">
          <span className="ll-kicker">דוגמאות להטבות</span>
          <h2 className="ll-h2">שוברים שאנשים באמת רוצים לשתף.</h2>
          <p className="ll-subhead">בחרו הטבה שמתאימה לרווחים שלכם: נעימה מספיק כדי להעביר הלאה, ושווה מספיק כדי שהחבר יגיע.</p>
        </div>
        <div className="ll-vouchers">
          {[
            ['green', 'קפה חינם בביקור הראשון', 'קפה הציפור הכחולה', -3, 'coffee'],
            ['navy', "מסאז' זוגי בחצי מחיר", 'ספא נווה', 2, 'spa'],
            ['light', '2 קוקטיילים על חשבוננו', 'החדר האחורי', -1.5, 'bar'],
            ['cream', 'תספורת ראשונה ב-40 שקלים', 'Maison Lou', 3, 'salon'],
            ['green', '25% הנחה על ארוחת הערב', 'זית וטימין', -2, 'restaurant'],
          ].map(([tone, reward, biz, rot, kind]) => (
            <div key={biz} style={{ width: 290, transform: `rotate(${rot}deg)` }}>
              <VoucherTicket icon={kind} tone={tone} reward={reward} business={biz} from="" place={{ href: 'https://www.google.com/maps' }} />
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ── Pricing
function Pricing() {
  const incl = [
    { icon: 'check', t: 'הקמה בחינם' },
    { icon: 'gift', t: '5 הלקוחות החדשים הראשונים עלינו' },
    { icon: 'infinity', t: 'סריקות ושוברים ללא הגבלה' },
    { icon: 'check', t: 'כמה סניפים שרוצים, קוד QR לכל סניף' },
    { icon: 'wallet', t: 'בלי מנוי' },
    { icon: 'check', t: 'בלי מינימום חודשי' },
    { icon: 'bolt', t: 'משלמים רק על מימוש של לקוח חדש' },
  ];
  return (
    <section id="pricing" className="ll-section ll-section-warm">
      <div className="ll-container ll-pricing">
        <div className="ll-pricing-copy">
          <span className="ll-kicker">תמחור</span>
          <h2 className="ll-h2">משלמים רק על תוצאות.</h2>
          <p className="ll-subhead" style={{ marginInlineStart: 0 }}>
            ההקמה בחינם, ו-5 הלקוחות החדשים הראשונים עלינו. אחריהם משלמים ₪3 רק כשחבר מממש שובר אצלכם בקופה, בחיוב אחד בסוף החודש. אם אף אחד לא הגיע, לא שילמתם כלום.
          </p>
          <ul className="ll-incl">
            {incl.map(x => (
              <li key={x.t}><span className="ll-incl-ic"><Icon name={x.icon} size={15} color="var(--reco-green-deep)" /></span>{x.t}</li>
            ))}
          </ul>
        </div>
        <div className="ll-price-card">
          <div className="ll-price-badge">תשלום לפי תוצאה</div>
          <div className="ll-price-amount" dir="ltr">
            <span className="ll-price-cur">₪</span>3
          </div>
          <div className="ll-price-unit">לכל שובר שמומש</div>
          <div className="ll-price-divider" />
          <div className="ll-price-note">
            <span className="ll-incl-ic"><Icon name="check" size={15} color="var(--reco-green-deep)" /></span>
            5 לקוחות ראשונים חינם · בלי מנוי · בלי מינימום
          </div>
          <RecoButton style={{ marginTop: 20 }} onClick={goStart}>מתחילים בחינם</RecoButton>
          <div className="ll-price-fine">ההקמה לוקחת כמה דקות, ואפשר להתחיל היום.</div>
        </div>
      </div>
    </section>
  );
}

// ── Dashboard preview: a simplified copy of the real owner app home screen, inside a phone
function DashboardPreview() {
  const sc = 0.88;
  return (
    <section id="dashboard" className="ll-section">
      <div className="ll-container">
        <div className="ll-dash2">
          <div className="ll-dash2-copy">
            <span className="ll-kicker">האפליקציה שלכם</span>
            <h2 className="ll-h2" style={{ textAlign: 'start' }}>כל מה שקורה, בכף היד.</h2>
            <p className="ll-subhead" style={{ marginInlineStart: 0, textAlign: 'start' }}>השובר שלכם, הדירוג בגוגל והלקוחות החדשים. בלי טבלאות ובלי מאמץ.</p>
          </div>
          <div className="ll-dash2-phone">
            <div className="ll-hero-glow" />
            <div style={{ position: 'relative', width: 402 * sc, height: 874 * sc }}>
              <div style={{ position: 'absolute', top: 0, left: 0, width: 402, height: 874, transform: `scale(${sc})`, transformOrigin: 'top left' }}>
                <IOSDevice>
                  {/* the real owner app in demo mode, so this always looks exactly like the app */}
                  <iframe src="/onboard.html?demo=1" title="אפליקציית Reco לבעלי עסקים" loading="lazy" tabIndex={-1} aria-hidden="true"
                    style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', border: 0, display: 'block', pointerEvents: 'none', background: '#F5F7F8' }} />
                </IOSDevice>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ── Final CTA + footer
function FinalCTA() {
  return (
    <section className="ll-cta">
      <Drift clouds={[[4, 60, 170, 0.75, 80], [76, 250, 150, 0.7, 70], [30, 330, 110, 0.5, 90]]} />
      <Horizon kind="night" />
      <Bird n={4} className="cb1" />
      <Bird n={2} className="cb2" />
      <div className="ll-cta-inner">
        <h2 className="ll-cta-title">תנו להמלצות <em>כנפיים</em>.</h2>
        <p className="ll-cta-sub">הלקוח המרוצה של היום הוא הלקוח החדש של מחר.</p>
        <RecoButton full={false} style={{ marginTop: 6 }} onClick={goStart}>מתחילים בחינם</RecoButton>
        <div className="ll-cta-fine">ההקמה בחינם · בלי מנוי חודשי</div>
      </div>
    </section>
  );
}

function Footer() {
  return (
    <footer className="ll-footer">
      <div className="ll-footer-inner">
        <div className="ll-footer-brand">
          <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <RecoLogomark size={32} />
            <span className="reco-brand" style={{ fontFamily: 'var(--iv-font-sans)', fontWeight: 700, fontSize: 24, lineHeight: 1, letterSpacing: '-0.04em', color: '#fff' }}>Reco</span>
          </div>
          <p>תנו להמלצות כנפיים.</p>
        </div>
        <div className="ll-footer-cols">
          <div>
            <span className="ll-footer-h">מוצר</span>
            <a href="#how">איך זה עובד</a>
            <a href="#pricing">תמחור</a>
            <a href="#dashboard">לוח בקרה</a>
          </div>
          <div>
            <span className="ll-footer-h">חברה</span>
            <a href="mailto:tuvia000@gmail.com">צור קשר</a>
          </div>
          <div>
            <span className="ll-footer-h">משפטי</span>
            <a href="/privacy.html">פרטיות</a>
            <a href="/terms.html">תנאי שימוש</a>
            <a href="/refund.html">מדיניות החזרים</a>
          </div>
        </div>
      </div>
      <div className="ll-footer-base">
        <span>© 2026 Reco · תנו להמלצות כנפיים.</span>
        <span>נבנה בשביל עסקים מקומיים.</span>
      </div>
    </footer>
  );
}

function RecoLanding() {
  return (
    <div className="ll-page">
      <Nav />
      <Hero />
      <ExplainerSection />
      <HowItWorks />
      <Rewards />
      <Benefits />
      <Trust />
      <Pricing />
      <DashboardPreview />
      <FinalCTA />
      <Footer />
    </div>
  );
}

Object.assign(window, { RecoLanding });
