const { useState, useEffect, useRef, useMemo } = React;
const T = window.TRANSLATIONS;

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "palette": "burgundy",
  "hero": "split",
  "font": "cormorant",
  "ornaments": true
}/*EDITMODE-END*/;

/* ---------- Icons (Why-section, inspired by poster) ---------- */
function MountainIcon() {
  return (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M4 38 L16 18 L22 26 L32 12 L44 38 Z" />
      <path d="M14 22 L18 26 M28 18 L32 22" opacity="0.6" />
    </svg>
  );
}
function SnowflakeIcon() {
  return (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M24 6 V42 M6 24 H42 M11 11 L37 37 M37 11 L11 37" />
      <path d="M24 12 L20 16 M24 12 L28 16 M24 36 L20 32 M24 36 L28 32 M12 24 L16 20 M12 24 L16 28 M36 24 L32 20 M36 24 L32 28" />
    </svg>
  );
}
function ScoopIcon() {
  return (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 22 Q24 6 36 22 Z" />
      <path d="M10 22 H38 L32 38 H16 Z" />
      <path d="M19 28 V34 M24 28 V35 M29 28 V34" opacity="0.6" />
    </svg>
  );
}
function ClocheIcon() {
  return (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M6 36 H42" />
      <path d="M10 36 Q10 16 24 16 Q38 16 38 36" />
      <circle cx="24" cy="12" r="2" />
      <path d="M22 36 Q22 28 26 28" opacity="0.5" />
    </svg>
  );
}
const whyIcons = [MountainIcon, SnowflakeIcon, ScoopIcon, ClocheIcon];

/* ---------- Decorative SVG ---------- */
function Flourish() {
  return (
    <svg className="orn-flourish" viewBox="0 0 80 24" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round">
      <path d="M2 12 Q14 12 20 6 Q26 0 32 12 Q38 24 44 12 Q50 0 56 12 Q62 24 68 12 Q74 12 78 12" />
      <circle cx="40" cy="12" r="1.5" fill="currentColor" />
    </svg>
  );
}
function OrnRule({ label }) {
  return (
    <div className="orn-rule">
      <span className="l"></span>
      <span className="d"></span>
      {label ? <span>{label}</span> : null}
      <span className="d"></span>
      <span className="r"></span>
    </div>
  );
}

/* ---------- Scroll reveal ---------- */
function useReveal() {
  useEffect(() => {
    document.body.classList.add('js-ready');
    const els = document.querySelectorAll('.reveal');
    const io = new IntersectionObserver(
      (entries) => {
        entries.forEach((e) => {
          if (e.isIntersecting) {
            e.target.classList.add('revealed');
            io.unobserve(e.target);
          }
        });
      },
      { threshold: 0.08, rootMargin: '0px 0px -48px 0px' }
    );
    els.forEach((el) => io.observe(el));
    return () => io.disconnect();
  }, []);
}

/* ---------- Nav ---------- */
function Nav({ t, lang, setLang }) {
  const [open, setOpen] = useState(false);
  const close = () => setOpen(false);

  // Lock body scroll when drawer is open
  useEffect(() => {
    document.body.style.overflow = open ? 'hidden' : '';
    return () => { document.body.style.overflow = ''; };
  }, [open]);

  return (
    <>
      <nav className="nav">
        <div className="wrap nav-inner">
          <a href="#top" className="nav-logo" onClick={close}>
            <img src="assets/logo-transparent.png" alt="Euromaraş" />
            <span className="wordmark">EUROMARAŞ</span>
          </a>
          <div className="nav-links">
            <a href="#tradition">{t.nav.tradition}</a>
            <a href="#why">{t.nav.why}</a>
            <a href="#sorten">{t.nav.sorten}</a>
            <a href="#faq">{t.nav.faq}</a>
            <a href="#contact">{t.nav.contact}</a>
          </div>
          <div className="nav-right">
            <div className="lang" role="group" aria-label="Language">
              {["de", "tr", "en"].map((l) => (
                <button key={l} className={lang === l ? "active" : ""} onClick={() => { setLang(l); close(); }}>
                  {l.toUpperCase()}
                </button>
              ))}
            </div>
            <button
              className={"nav-burger" + (open ? " open" : "")}
              onClick={() => setOpen((v) => !v)}
              aria-label={open ? "Menü schließen" : "Menü öffnen"}
              aria-expanded={open}
            >
              <span /><span /><span />
            </button>
          </div>
        </div>
      </nav>

      <div className={"mobile-menu" + (open ? " open" : "")} aria-hidden={!open} inert={open ? undefined : ""}>
        <a href="#tradition" className="m-link" onClick={close}>{t.nav.tradition}</a>
        <a href="#why"       className="m-link" onClick={close}>{t.nav.why}</a>
        <a href="#sorten"    className="m-link" onClick={close}>{t.nav.sorten}</a>
        <a href="#faq"       className="m-link" onClick={close}>{t.nav.faq}</a>
        <div className="m-cta">
          <a href="#contact" className="btn btn--solid" onClick={close}>
            <span>{t.nav.contact}</span>
            <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5">
              <path d="M2 8 H14 M9 3 L14 8 L9 13" />
            </svg>
          </a>
        </div>
        <div className="m-lang">
          {["de", "tr", "en"].map((l) => (
            <button key={l} className={lang === l ? "active" : ""} onClick={() => { setLang(l); close(); }}>
              {l.toUpperCase()}
            </button>
          ))}
        </div>
      </div>
    </>
  );
}

/* ---------- Hero ---------- */
function Hero({ t }) {
  return (
    <section id="top" className="hero velvet">
      <img src="assets/logo-transparent.png" className="hero-bg-logo" alt="" aria-hidden="true" />
      <div className="wrap hero-inner">
        <div className="hero-text">
          <div>
            <span className="eyebrow">{t.hero.eyebrow}</span>
          </div>
          <h1>
            {t.hero.title}<br />
            <em className="accent gold-text">{t.hero.titleAccent}</em>
          </h1>
          <p className="hero-claim">{t.hero.claim}</p>
          <div className="hero-cta-row">
            <a href="#contact" className="btn btn--solid">
              <span>{t.hero.ctaPrimary}</span>
              <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5"><path d="M2 8 H14 M9 3 L14 8 L9 13" /></svg>
            </a>
            <a href="#sorten" className="btn btn--ghost"><span>{t.hero.ctaSecondary}</span></a>
          </div>
          <div className="hero-meta">
            <div>
              <div className="label">{t.hero.m1Label}</div>
              <div className="val">{t.hero.m1Val}</div>
            </div>
            <div>
              <div className="label">{t.hero.m2Label}</div>
              <div className="val">{t.hero.m2Val}</div>
            </div>
            <div>
              <div className="label">{t.hero.m3Label}</div>
              <div className="val">{t.hero.m3Val}</div>
            </div>
          </div>
        </div>
        <div className="hero-product">
          <img src="assets/hero.png" alt="Maraş Eis Sade" />
          <div className="caption">
            <span className="eyebrow">{t.hero.productEyebrow}</span>
            <div className="name">{t.hero.productName}</div>
          </div>
        </div>
      </div>
    </section>
  );
}

/* ---------- Tradition ---------- */
function Tradition({ t }) {
  return (
    <section id="tradition" className="velvet pad-y" data-screen-label="Tradition">
      <div className="wrap">
        <div className="tradition-grid">
          <div className="tradition-img reveal">
            <img src="assets/hero.png" alt="Maraş Eis" />
            <div className="badge ornable">{t.tradition.badge}</div>
          </div>
          <div className="tradition-text">
            <div className="reveal"><span className="eyebrow">{t.tradition.eyebrow}</span></div>
            <h2 className="reveal" data-delay="1">{t.tradition.title}</h2>
            <p className="reveal" data-delay="2" dangerouslySetInnerHTML={{ __html: t.tradition.p1 }} />
            <p className="reveal" data-delay="3" dangerouslySetInnerHTML={{ __html: t.tradition.p2 }} />
            <div className="quote reveal" data-delay="4">{t.tradition.quote}</div>
          </div>
        </div>
      </div>
    </section>
  );
}

/* ---------- Why (poster-styled card) ---------- */
function Why({ t }) {
  return (
    <section id="why" className="why pad-y" data-screen-label="Warum Euromaras">
      <div className="wrap">
        <div className="why-card-frame reveal">
          <span className="frame-corners">
            <span className="tl"></span><span className="tr"></span><span className="bl"></span><span className="br"></span>
          </span>
          <div className="ribbon">{t.why.ribbon}</div>

          <div className="section-head" style={{ marginBottom: 24 }}>
            <span className="eyebrow">{t.why.eyebrow}</span>
            <h2 className="gold-text" style={{ marginTop: 16, marginBottom: 16 }}>{t.why.title}</h2>
            <div className="ornable" style={{ display: "flex", justifyContent: "center", marginTop: 8 }}>
              <Flourish />
            </div>
            <p className="lead" style={{ marginTop: 16 }}>{t.why.lead}</p>
          </div>

          <div className="why-list">
            {t.why.items.map((item, i) => {
              const Icon = whyIcons[i];
              return (
                <div className="why-item reveal" key={i} data-delay={i + 1}>
                  <div className="why-icon"><Icon /></div>
                  <div className="why-content">
                    <h3><span className="num">{i + 1}.</span>{item.title}</h3>
                    <p>{item.text}</p>
                  </div>
                </div>
              );
            })}
          </div>

          <div className="why-foot reveal" data-delay="4">
            <div className="ornable" style={{ display: "flex", justifyContent: "center", marginBottom: 12 }}>
              <Flourish />
            </div>
            {t.why.foot}
          </div>
        </div>
      </div>
    </section>
  );
}

/* ---------- Sorten ---------- */
function Sorten({ t }) {
  // Use hero image for first; CSS gradients for others (placeholder colours)
  const bgs = [
    "url(assets/hero.png)",
    "radial-gradient(ellipse at 50% 40%, #3d1810 0%, #1a0506 80%)",
    "radial-gradient(ellipse at 50% 40%, #2e3a14 0%, #0e1a06 80%)",
  ];
  return (
    <section id="sorten" className="velvet pad-y" data-screen-label="Sorten">
      <div className="wrap">
        <div className="section-head">
          <div className="reveal"><span className="eyebrow">{t.sorten.eyebrow}</span></div>
          <h2 className="reveal" data-delay="1">{t.sorten.title}</h2>
          <div className="reveal ornable" data-delay="2" style={{ display: "flex", justifyContent: "center", marginTop: 12, marginBottom: 16 }}>
            <Flourish />
          </div>
          <p className="lead reveal" data-delay="2">{t.sorten.lead}</p>
        </div>
        <div className="sorten-grid">
          {t.sorten.items.map((item, i) => (
            <article className="sorte reveal" key={i} data-delay={i + 1}>
              <div className="sorte-img" style={{ background: bgs[i], backgroundSize: "cover", backgroundPosition: "center" }}></div>
              <div className="sorte-content">
                <span className="sorte-num">№ {item.num}</span>
                <h3 className="sorte-name">{item.name}</h3>
                <div className="sorte-alt">{item.alt}</div>
                <p className="sorte-desc">{item.desc}</p>
              </div>
            </article>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------- FAQ ---------- */
function FAQ({ t }) {
  const [open, setOpen] = useState(0);
  return (
    <section id="faq" className="velvet pad-y" data-screen-label="FAQ">
      <div className="wrap">
        <div className="section-head">
          <div className="reveal"><span className="eyebrow">{t.faq.eyebrow}</span></div>
          <h2 className="reveal" data-delay="1">{t.faq.title}</h2>
          <div className="reveal ornable" data-delay="2" style={{ display: "flex", justifyContent: "center", marginTop: 12 }}>
            <Flourish />
          </div>
        </div>
        <div className="faq-list reveal">
          {t.faq.items.map((it, i) => {
            const isOpen = open === i;
            return (
              <div className={"faq-item" + (isOpen ? " open" : "")} key={i}>
                <button className="faq-q" onClick={() => setOpen(isOpen ? -1 : i)} aria-expanded={isOpen}>
                  <span>{it.q}</span>
                  <span className="faq-toggle" aria-hidden="true">
                    <svg width="12" height="12" viewBox="0 0 12 12" fill="none" stroke="currentColor" strokeWidth="1.5"><path d="M6 2 V10 M2 6 H10" /></svg>
                  </span>
                </button>
                <div className="faq-a">
                  <div className="faq-a-inner">{it.a}</div>
                </div>
              </div>
            );
          })}
        </div>
      </div>
    </section>
  );
}

/* ---------- CTA / Anfrage ---------- */
function CTA({ t }) {
  return (
    <section id="contact" className="cta pad-y" data-screen-label="Anfrage">
      <img src="assets/logo-transparent.png" className="cta-logo" alt="" aria-hidden="true" />
      <div className="wrap cta-inner">
        <div className="reveal"><span className="eyebrow">{t.cta.eyebrow}</span></div>
        <h2 className="reveal gold-text" data-delay="1">{t.cta.title}</h2>
        <div className="reveal ornable" data-delay="1" style={{ display: "flex", justifyContent: "center", marginTop: 16, marginBottom: 16 }}>
          <Flourish />
        </div>
        <p className="cta-sub reveal" data-delay="2">{t.cta.sub}</p>
        <div className="reveal" data-delay="3">
          <a href="mailto:info@euromaras.de" className="btn btn--solid">
            <span>{t.cta.button}</span>
            <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5"><path d="M2 8 H14 M9 3 L14 8 L9 13" /></svg>
          </a>
        </div>
        <p className="small reveal" data-delay="4" style={{ marginTop: 24 }}>{t.cta.note}</p>
      </div>
    </section>
  );
}

/* ---------- Legal Modal ---------- */
function LegalModal({ kind, lang, onClose }) {
  useEffect(() => {
    const onKey = (e) => { if (e.key === "Escape") onClose(); };
    document.addEventListener("keydown", onKey);
    const prevOverflow = document.body.style.overflow;
    document.body.style.overflow = "hidden";
    return () => {
      document.removeEventListener("keydown", onKey);
      document.body.style.overflow = prevOverflow;
    };
  }, [onClose]);

  if (!kind) return null;
  const content = window.LEGAL_CONTENT[kind][lang] || window.LEGAL_CONTENT[kind].de;

  return (
    <div className="modal-backdrop" onClick={onClose} role="dialog" aria-modal="true">
      <div className="modal" onClick={(e) => e.stopPropagation()}>
        <div className="modal-head">
          <h3>{content.title}</h3>
          <button className="modal-close" onClick={onClose} aria-label="Close">
            <svg width="14" height="14" viewBox="0 0 14 14" fill="none" stroke="currentColor" strokeWidth="1.5">
              <path d="M2 2 L12 12 M12 2 L2 12" />
            </svg>
          </button>
        </div>
        <div className="modal-body" dangerouslySetInnerHTML={{ __html: content.html }} />
      </div>
    </div>
  );
}

/* ---------- Footer ---------- */
function Footer({ t, onLegal }) {
  return (
    <footer>
      <div className="wrap">
        <div className="footer-grid">
          <div className="footer-brand">
            <img src="assets/logo-transparent.png" alt="Euromaraş" />
            <div className="wordmark">EUROMARAŞ</div>
            <p>{t.footer.tagline}</p>
          </div>
          <div className="footer-col">
            <h4>{t.footer.brand}</h4>
            <a href="#tradition">{t.nav.tradition}</a>
            <a href="#why">{t.nav.why}</a>
            <a href="#sorten">{t.nav.sorten}</a>
            <a href="#faq">{t.nav.faq}</a>
          </div>
          <div className="footer-col">
            <h4>{t.footer.address}</h4>
            <p>Gelatios GmbH</p>
            <p>Hönnetalstr. 150a</p>
            <p>58675 Hemer</p>
            <p>Deutschland</p>
          </div>
          <div className="footer-col">
            <h4>{t.footer.contact}</h4>
            <a href="mailto:info@euromaras.de">info@euromaras.de</a>
            <a href="tel:+492372000000">+49 2372 — auf Anfrage</a>
            <a href="#contact">{t.cta.button}</a>
          </div>
        </div>
        <div className="footer-bottom">
          <span>{t.footer.copyright}</span>
          <div className="legal">
            <a href="#" onClick={(e) => { e.preventDefault(); onLegal("imprint"); }}>{t.footer.legal.imprint}</a>
            <a href="#" onClick={(e) => { e.preventDefault(); onLegal("privacy"); }}>{t.footer.legal.privacy}</a>
            <a href="#" onClick={(e) => { e.preventDefault(); onLegal("terms"); }}>{t.footer.legal.terms}</a>
          </div>
        </div>
      </div>
    </footer>
  );
}

/* ---------- App ---------- */
function App() {
  const [lang, setLang] = useState("de");
  const [legal, setLegal] = useState(null);
  const tweaks = TWEAK_DEFAULTS;
  const t = T[lang];

  // Sync tweaks to body attributes
  useEffect(() => {
    document.body.dataset.palette = tweaks.palette;
    document.body.dataset.hero = tweaks.hero;
    document.body.dataset.font = tweaks.font;
    if (tweaks.ornaments) document.body.classList.remove("no-orn");
    else document.body.classList.add("no-orn");
    document.documentElement.lang = lang;
  }, [tweaks, lang]);

  useReveal();

  return (
    <>
      <Nav t={t} lang={lang} setLang={setLang} />
      <Hero t={t} />
      <Tradition t={t} />
      <Why t={t} />
      <Sorten t={t} />
      <FAQ t={t} />
      <CTA t={t} />
      <Footer t={t} onLegal={setLegal} />
      <LegalModal kind={legal} lang={lang} onClose={() => setLegal(null)} />

    </>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
