// Home page composition
const { useState: useStateHM, useEffect: useEffectHM } = React;

function LangSwitch() {
  const { lang, setLang } = useT();
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'center',
      border: '1px solid var(--line)', borderRadius: 999,
      padding: 3, background: 'var(--card)',
    }}>
      {['fr', 'en'].map(l => {
        const active = lang === l;
        return (
          <button key={l} onClick={() => setLang(l)}
            style={{
              padding: '4px 10px', borderRadius: 999,
              background: active ? 'var(--ink)' : 'transparent',
              color: active ? 'var(--paper)' : 'var(--ink-soft)',
              fontSize: 11, fontWeight: 600, letterSpacing: '0.06em', textTransform: 'uppercase',
            }}>
            {l}
          </button>
        );
      })}
    </div>
  );
}

function TopNav({ onOpenReserve, onOpenTakeaway }) {
  const [scrolled, setScrolled] = useStateHM(false);
  const { t } = useT();
  useEffectHM(() => {
    const onScroll = () => setScrolled(window.scrollY > 20);
    window.addEventListener('scroll', onScroll);
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  return (
    <header style={{
      position: 'sticky', top: 0, zIndex: 40,
      background: scrolled ? 'color-mix(in oklab, var(--paper) 92%, transparent)' : 'transparent',
      backdropFilter: scrolled ? 'blur(10px)' : 'none',
      borderBottom: scrolled ? '1px solid var(--line)' : '1px solid transparent',
      transition: 'background .2s ease, border-color .2s ease',
    }}>
      <div className="container topnav-row" style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '16px 0', gap: 12 }}>
        <Logo size={40} />
        <nav style={{ display: 'flex', alignItems: 'center', gap: 16 }} className="topnav-links">
          <a href="#services" className="topnav-anchor" style={{ fontSize: 14, fontWeight: 500, textDecoration: 'none', color: 'var(--ink-soft)' }}>{t.nav.services}</a>
          <a href="#menu" className="topnav-anchor" style={{ fontSize: 14, fontWeight: 500, textDecoration: 'none', color: 'var(--ink-soft)' }}>{t.nav.menu}</a>
          <a href="#visit" className="topnav-anchor" style={{ fontSize: 14, fontWeight: 500, textDecoration: 'none', color: 'var(--ink-soft)' }}>{t.nav.visit}</a>
          <span className="topnav-lang"><LangSwitch /></span>
          <span className="topnav-reserve"><Button variant="outline" size="sm" onClick={onOpenReserve}>{t.nav.reserve}</Button></span>
          <Button variant="primary" size="sm" onClick={onOpenTakeaway}>{t.nav.orderPickup}</Button>
          <a href="admin.html" className="topnav-admin" style={{ fontSize: 13, fontWeight: 600, color: 'var(--ink-soft)', textDecoration: 'none', padding: '8px 14px', border: '1px solid var(--line)', borderRadius: 999, whiteSpace: 'nowrap' }}>Admin →</a>
        </nav>
      </div>
      <style>{`
        @media (max-width: 900px) { .topnav-anchor { display: none; } }
        @media (max-width: 720px) {
          .topnav-reserve { display: none; }
          .topnav-lang { display: none; }
          .topnav-links { gap: 8px; }
          .topnav-admin { padding: 6px 10px !important; font-size: 12px !important; }
        }
      `}</style>
    </header>
  );
}

function Hero({ variant, onOpenReserve, onOpenTakeaway }) {
  const { t } = useT();
  if (variant === 'split') {
    return (
      <section style={{ padding: '40px 0 64px' }}>
        <div className="container hero-split">
          <div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 24 }}>
              <span style={{ width: 36, height: 1, background: 'var(--accent)' }} />
              <span className="mono" style={{ fontSize: 11, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'var(--accent-deep)' }}>{t.hero.eyebrow}</span>
            </div>
            <h1 className="serif" style={{ fontSize: 'clamp(48px, 7vw, 96px)', lineHeight: 0.95, margin: 0, fontWeight: 500, letterSpacing: '-0.025em' }}>
              {t.hero.splitA}<br/>
              <em style={{ fontFamily: 'Fraunces', fontStyle: 'italic', color: 'var(--accent)' }}>{t.hero.splitB}</em><br/>
              {t.hero.splitC}
            </h1>
            <p style={{ margin: '24px 0 0', fontSize: 18, color: 'var(--ink-soft)', lineHeight: 1.55, maxWidth: 520 }}>
              {t.hero.splitLede}
            </p>
            <div style={{ display: 'flex', gap: 12, marginTop: 32, flexWrap: 'wrap' }}>
              <Button size="lg" variant="primary" onClick={onOpenTakeaway}>{t.hero.ctaPickup}</Button>
              <Button size="lg" variant="ghost" onClick={onOpenReserve}>{t.hero.ctaReserve}</Button>
            </div>
          </div>
          <div style={{ position: 'relative', aspectRatio: '4/5', borderRadius: 20, overflow: 'hidden', boxShadow: 'var(--shadow-md)' }}>
            <img src="assets/dish-shish-taouk.webp" alt="" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
            <div style={{
              position: 'absolute', left: 16, bottom: 16, right: 16,
              background: 'rgba(255,255,255,.92)', borderRadius: 12, padding: '12px 16px',
              display: 'flex', alignItems: 'center', gap: 10, backdropFilter: 'blur(6px)',
            }}>
              <span style={{ width: 8, height: 8, borderRadius: 999, background: 'var(--ok)', boxShadow: '0 0 0 4px color-mix(in oklab, var(--ok) 20%, transparent)' }}/>
              <span style={{ fontSize: 13, color: '#1a1715', fontWeight: 500 }}>{t.hero.openNow}</span>
            </div>
          </div>
        </div>
      </section>
    );
  }
  return (
    <section style={{ padding: '28px 0 56px', position: 'relative' }}>
      <div className="container">
        <div style={{ position: 'relative', borderRadius: 22, overflow: 'hidden', minHeight: 'min(640px, 80vh)', background: '#1a1210', display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', boxShadow: 'var(--shadow-md)' }}>
          <img src="assets/dish-shish-taouk.webp" alt="" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover', filter: 'brightness(0.75) saturate(1.05)' }} />
          <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(180deg, rgba(26,18,16,.45) 0%, rgba(26,18,16,.2) 40%, rgba(26,18,16,.85) 100%)' }} />
          <div style={{ position: 'relative', padding: 'clamp(28px, 5vw, 56px)', color: '#fff' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 18 }}>
              <span style={{ width: 36, height: 1, background: 'rgba(255,255,255,.6)' }} />
              <span className="mono" style={{ fontSize: 11, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'rgba(255,255,255,.8)' }}>{t.hero.eyebrow}</span>
            </div>
            <h1 className="serif" style={{ fontSize: 'clamp(44px, 8vw, 108px)', lineHeight: 0.94, margin: 0, fontWeight: 500, letterSpacing: '-0.03em', maxWidth: 980 }}>
              {t.hero.titleA}<br/>
              {t.hero.titleB}<br/>
              {t.hero.titleC}<em style={{ fontFamily: 'Fraunces', fontStyle: 'italic', color: '#f2b78c' }}>{t.hero.titleD}</em>
            </h1>
            <p style={{ margin: '22px 0 0', fontSize: 17, color: 'rgba(255,255,255,.82)', lineHeight: 1.55, maxWidth: 540 }}>
              {t.hero.lede}
            </p>
            <a href="#services" style={{ display: 'inline-flex', alignItems: 'center', gap: 8, marginTop: 28, color: '#fff', fontSize: 13, fontWeight: 600, letterSpacing: '0.04em', textTransform: 'uppercase', textDecoration: 'none', borderBottom: '1px solid rgba(255,255,255,.4)', paddingBottom: 4 }}>
              {t.hero.choose} <Icon.arrow size={14} />
            </a>
          </div>
        </div>
      </div>
    </section>
  );
}

function ServiceCard({ index, label, title, desc, cta, img, imgAlt, badge, onClick, layout, mode, modeLabels }) {
  const [hover, setHover] = useStateHM(false);
  const isProminent = layout === 'prominent' && index === 0;
  const dimmed = mode === 'closed';
  return (
    <button
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      onClick={onClick}
      style={{
        textAlign: 'left', background: 'var(--card)', borderRadius: 20,
        border: '1px solid var(--line)', overflow: 'hidden',
        display: 'flex', flexDirection: 'column',
        boxShadow: hover ? 'var(--shadow-md)' : 'var(--shadow-sm)',
        transform: hover ? 'translateY(-3px)' : 'translateY(0)',
        transition: 'transform .2s ease, box-shadow .2s ease',
        cursor: 'pointer', position: 'relative',
        gridColumn: isProminent ? 'span 2' : 'span 1',
        opacity: dimmed ? 0.7 : 1,
      }}>
      <div style={{ position: 'relative', aspectRatio: isProminent ? '16/8' : '5/4', overflow: 'hidden', background: 'var(--paper-warm)' }}>
        <img src={img} alt={imgAlt} style={{ width: '100%', height: '100%', objectFit: 'cover', transform: hover ? 'scale(1.04)' : 'scale(1)', transition: 'transform .5s ease', filter: dimmed ? 'grayscale(0.6)' : 'none' }} />
        <div style={{ position: 'absolute', top: 14, left: 14, background: 'rgba(255,255,255,.94)', color: 'var(--ink)', borderRadius: 999, padding: '6px 12px', display: 'inline-flex', alignItems: 'center', gap: 6, fontSize: 11, fontWeight: 600, letterSpacing: '0.08em', textTransform: 'uppercase' }}>
          <span className="mono" style={{ color: 'var(--accent)' }}>0{index+1}</span>
          {label}
        </div>
        {mode === 'online' && badge && (
          <div style={{ position: 'absolute', top: 14, right: 14, background: 'var(--accent)', color: '#fff', borderRadius: 999, padding: '5px 10px', fontSize: 11, fontWeight: 600, letterSpacing: '0.05em' }}>{badge}</div>
        )}
        {mode === 'phone' && (
          <div style={{ position: 'absolute', top: 14, right: 14, background: '#1a1715', color: '#fff', borderRadius: 999, padding: '5px 10px 5px 8px', fontSize: 11, fontWeight: 600, letterSpacing: '0.05em', display: 'inline-flex', alignItems: 'center', gap: 6 }}>
            <Icon.phone size={12}/> {modeLabels.phone}
          </div>
        )}
        {mode === 'closed' && (
          <div style={{ position: 'absolute', top: 14, right: 14, background: '#6b625a', color: '#fff', borderRadius: 999, padding: '5px 10px', fontSize: 11, fontWeight: 600, letterSpacing: '0.05em' }}>{modeLabels.closed}</div>
        )}
      </div>
      <div style={{ padding: '22px 24px 24px', flex: 1, display: 'flex', flexDirection: 'column' }}>
        <h3 className="serif" style={{ margin: 0, fontSize: 26, fontWeight: 600, letterSpacing: '-0.015em' }}>{title}</h3>
        <p style={{ margin: '10px 0 0', fontSize: 14.5, color: 'var(--ink-soft)', lineHeight: 1.55 }}>{desc}</p>
        <div style={{ marginTop: 'auto', paddingTop: 20, display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, color: mode === 'closed' ? 'var(--muted)' : 'var(--accent)', fontWeight: 600, fontSize: 14 }}>
            {cta}
            <span style={{ display: 'inline-flex', transition: 'transform .2s ease', transform: hover ? 'translateX(4px)' : 'translateX(0)' }}>
              <Icon.arrow size={16} />
            </span>
          </span>
        </div>
      </div>
    </button>
  );
}

function Services({ onOpenReserve, onOpenTakeaway, onOpenDelivery, layout }) {
  const { t } = useT();
  const store = useLnStore();
  const modes = store.serviceModes;
  const modeLabels = t.serviceBadge;
  return (
    <section id="services" style={{ padding: '48px 0 80px' }}>
      <div className="container">
        <div style={{ marginBottom: 36, display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', flexWrap: 'wrap', gap: 16 }}>
          <div>
            <div className="mono" style={{ fontSize: 11, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'var(--accent-deep)', marginBottom: 10 }}>{t.services.eyebrow}</div>
            <h2 className="serif" style={{ margin: 0, fontSize: 'clamp(34px, 4vw, 52px)', fontWeight: 500, letterSpacing: '-0.02em', lineHeight: 1.05, maxWidth: 720 }}>
              {t.services.title}
            </h2>
          </div>
          <p style={{ margin: 0, color: 'var(--ink-soft)', fontSize: 15, maxWidth: 360, lineHeight: 1.55 }}>
            {t.services.sub}
          </p>
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: layout === 'prominent' ? 'repeat(2, 1fr)' : 'repeat(3, 1fr)', gap: 20 }} className="services-grid">
          <ServiceCard index={0} mode={modes.takeaway}    modeLabels={modeLabels} {...{ label: t.services.takeaway.label, title: t.services.takeaway.title, desc: t.services.takeaway.desc, cta: t.services.takeaway.cta, badge: t.services.takeaway.badge }} img="assets/dish-falafel-box.webp" imgAlt="" onClick={onOpenTakeaway} layout={layout} />
          <ServiceCard index={1} mode={modes.reservation} modeLabels={modeLabels} {...{ label: t.services.reserve.label, title: t.services.reserve.title, desc: t.services.reserve.desc, cta: t.services.reserve.cta }} img="assets/dish-tabbouleh.webp" imgAlt="" onClick={onOpenReserve} layout={layout} />
          <ServiceCard index={2} mode={modes.delivery}    modeLabels={modeLabels} {...{ label: t.services.delivery.label, title: t.services.delivery.title, desc: t.services.delivery.desc, cta: t.services.delivery.cta }} img="assets/dish-chicken-wrap.webp" imgAlt="" onClick={onOpenDelivery} layout={layout} />
        </div>
        <style>{`@media (max-width: 980px) { .services-grid { grid-template-columns: 1fr !important; } }`}</style>
      </div>
    </section>
  );
}

function MenuPreview({ onOpenTakeaway }) {
  const { t } = useT();
  const featured = ['moutabal', 'shish-taouk', 'wrap-falafel', 'kebbeh', 'tabbouleh', 'falafel-box'];
  const items = featured.map(id => MENU_ITEMS.find(i => i.id === id));
  return (
    <section id="menu" style={{ padding: '80px 0', background: 'var(--paper-warm)', borderTop: '1px solid var(--line)', borderBottom: '1px solid var(--line)' }}>
      <div className="container">
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', marginBottom: 36, flexWrap: 'wrap', gap: 16 }}>
          <div>
            <div className="mono" style={{ fontSize: 11, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'var(--accent-deep)', marginBottom: 10 }}>{t.menuPreview.eyebrow}</div>
            <h2 className="serif" style={{ margin: 0, fontSize: 'clamp(32px, 3.6vw, 48px)', fontWeight: 500, letterSpacing: '-0.02em' }}>{t.menuPreview.title}</h2>
          </div>
          <Button variant="ghost" onClick={onOpenTakeaway}>{t.menuPreview.seeAll} <Icon.arrow /></Button>
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 20 }} className="menu-grid">
          {items.map(it => {
            const info = t.dishes[it.id];
            return (
              <div key={it.id} style={{ background: 'var(--card)', borderRadius: 16, overflow: 'hidden', border: '1px solid var(--line)' }}>
                <div style={{ aspectRatio: '4/3', background: 'var(--paper)' }}>
                  {it.img ? <img src={it.img} style={{ width: '100%', height: '100%', objectFit: 'cover' }} /> : <DishPlaceholder label={info.name} />}
                </div>
                <div style={{ padding: '16px 18px 18px' }}>
                  <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 12 }}>
                    <h4 className="serif" style={{ margin: 0, fontSize: 19, fontWeight: 600 }}>{info.name}</h4>
                    <span className="mono" style={{ fontSize: 13, fontWeight: 600 }}>{it.price.toFixed(2)} €</span>
                  </div>
                  <p style={{ margin: '6px 0 0', fontSize: 13.5, color: 'var(--ink-soft)', lineHeight: 1.5 }}>{info.desc}</p>
                </div>
              </div>
            );
          })}
        </div>
        <style>{`
          @media (max-width: 980px) { .menu-grid { grid-template-columns: repeat(2, 1fr) !important; } }
          @media (max-width: 620px) { .menu-grid { grid-template-columns: 1fr !important; } }
        `}</style>
      </div>
    </section>
  );
}

function Visit() {
  const { t } = useT();
  return (
    <section id="visit" style={{ padding: '80px 0' }}>
      <div className="container grid-2">
        <div>
          <div className="mono" style={{ fontSize: 11, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'var(--accent-deep)', marginBottom: 10 }}>{t.visit.eyebrow}</div>
          <h2 className="serif" style={{ margin: 0, fontSize: 'clamp(30px, 3.4vw, 44px)', fontWeight: 500, letterSpacing: '-0.02em', lineHeight: 1.1 }}>
            {t.visit.title}<br/>{t.visit.title2}<sup style={{ fontSize: '0.5em' }}>{t.visit.titleSup}</sup>{t.visit.period}
          </h2>
          <div style={{ marginTop: 24, display: 'flex', flexDirection: 'column', gap: 12, fontSize: 15 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, color: 'var(--ink-soft)' }}>
              <Icon.clock /> {t.visit.hoursShort}
            </div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, color: 'var(--ink-soft)' }}>
              <Icon.phone /> {RESTAURANT_INFO.phone}
            </div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, color: 'var(--ink-soft)' }}>
              <Icon.pin /> {t.visit.metro}
            </div>
          </div>
        </div>
        <div style={{
          aspectRatio: '4/3', borderRadius: 18, overflow: 'hidden', border: '1px solid var(--line)',
          position: 'relative',
          background: `
            linear-gradient(45deg, var(--paper-warm) 25%, transparent 25%) -10px 0,
            linear-gradient(-45deg, var(--paper-warm) 25%, transparent 25%) -10px 0,
            linear-gradient(45deg, transparent 75%, var(--paper-warm) 75%),
            linear-gradient(-45deg, transparent 75%, var(--paper-warm) 75%),
            var(--paper)
          `,
          backgroundSize: '20px 20px',
        }}>
          <div style={{ position: 'absolute', inset: 0, background: 'repeating-linear-gradient(90deg, transparent 0 80px, color-mix(in oklab, var(--muted) 10%, transparent) 80px 81px), repeating-linear-gradient(0deg, transparent 0 80px, color-mix(in oklab, var(--muted) 10%, transparent) 80px 81px)' }}/>
          <div style={{ position: 'absolute', left: '50%', top: '50%', transform: 'translate(-50%,-50%)', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6 }}>
            <span style={{ width: 44, height: 44, borderRadius: 999, background: 'var(--accent)', color: '#fff', display: 'inline-flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 0 0 8px color-mix(in oklab, var(--accent) 22%, transparent)' }}>
              <Icon.pin size={22}/>
            </span>
            <span className="mono" style={{ fontSize: 11, letterSpacing: '0.15em', textTransform: 'uppercase', color: 'var(--ink-soft)', background: 'var(--card)', padding: '3px 8px', borderRadius: 4 }}>Los Nobles</span>
          </div>
        </div>
      </div>
    </section>
  );
}

function Footer() {
  const { t } = useT();
  return (
    <footer style={{ background: 'var(--ink)', color: 'color-mix(in oklab, var(--paper) 75%, transparent)', padding: '56px 0 36px' }}>
      <div className="container grid-4" style={{ alignItems: 'flex-start' }}>
        <div>
          <Logo size={48} invert />
          <p style={{ margin: '20px 0 0', fontSize: 13, lineHeight: 1.55, maxWidth: 280, color: 'rgba(246,240,230,.55)' }}>{t.footer.about}</p>
        </div>
        <div>
          <div className="mono" style={{ fontSize: 10, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'rgba(246,240,230,.45)', marginBottom: 14 }}>{t.footer.hours}</div>
          <div style={{ fontSize: 14, lineHeight: 1.8 }}>{t.footer.hoursBody.map((l,i) => <div key={i}>{l}</div>)}</div>
        </div>
        <div>
          <div className="mono" style={{ fontSize: 10, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'rgba(246,240,230,.45)', marginBottom: 14 }}>{t.footer.contact}</div>
          <div style={{ fontSize: 14, lineHeight: 1.8 }}>
            27 rue des Oliviers<br/>75011 Paris<br/>{RESTAURANT_INFO.phone}
          </div>
        </div>
        <div>
          <div className="mono" style={{ fontSize: 10, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'rgba(246,240,230,.45)', marginBottom: 14 }}>{t.footer.follow}</div>
          <div style={{ fontSize: 14, lineHeight: 1.8 }}>{t.footer.social.map((l,i) => <div key={i}>{l}</div>)}</div>
        </div>
      </div>
      <div className="container" style={{ marginTop: 48, paddingTop: 20, borderTop: '1px solid rgba(246,240,230,.1)', display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: 12 }}>
        <span className="mono" style={{ fontSize: 11, color: 'rgba(246,240,230,.4)' }}>{t.footer.rights.replace('{year}', new Date().getFullYear())}</span>
        <span className="mono" style={{ fontSize: 11, color: 'rgba(246,240,230,.4)' }}>{t.footer.built}</span>
      </div>
    </footer>
  );
}

// Section padding adjustments for small screens
const _homeStyle = document.createElement('style');
_homeStyle.textContent = `
@media (max-width: 720px) {
  #services { padding: 32px 0 56px !important; }
  #menu { padding: 56px 0 !important; }
  #visit { padding: 56px 0 !important; }
  footer { padding: 40px 0 28px !important; }
}
`;
document.head.appendChild(_homeStyle);

Object.assign(window, { TopNav, Hero, Services, MenuPreview, Visit, Footer });
