// sections.jsx — VENTI in Airbnb listing detail pattern

const { useState: useState_S, useEffect: useEffect_S } = React;

// ─── Top Nav (Airbnb style) ────────────────────────────────────
function TopNav({ t, lang, setLang }) {
  return (
    <header className="top-nav">
      <div className="top-nav-inner">
        <a href="#top" className="brand" aria-label="VENTI">
          <span>VENTI</span>
        </a>

        <nav className="nav-tabs">
          <a className="nav-tab active" href="#listing">
            <svg className="tab-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round" aria-hidden="true">
              <path d="M3 12 L 12 4 L 21 12 V 20 H 3 Z"/>
              <path d="M10 20 V 14 H 14 V 20"/>
            </svg>
            {t.nav?.houses || "Casas"}
          </a>
          <a className="nav-tab" href="#kite">
            <svg className="tab-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round" aria-hidden="true">
              <path d="M12 3 L 19 9 L 12 12 L 5 9 Z"/>
              <path d="M12 12 V 21 M 9 21 H 15"/>
            </svg>
            {t.nav?.kite || "Kitesurf"}
          </a>
          <a className="nav-tab" href="#location">
            <svg className="tab-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden="true">
              <path d="M12 21 C 12 21 5 14 5 9 A 7 7 0 0 1 19 9 C 19 14 12 21 12 21 Z"/>
              <circle cx="12" cy="9" r="2.2"/>
            </svg>
            {t.nav?.location || "Ubicación"}
          </a>
        </nav>

        <div className="nav-right">
          <div className="lang-mini">
            {["en","pt","es"].map((l) => (
              <button key={l} className={lang === l ? "active" : ""} onClick={() => setLang(l)}>
                {l.toUpperCase()}
              </button>
            ))}
          </div>
        </div>
      </div>
    </header>
  );
}

// ─── Hero: Listing title row + photo grid ─────────────────────
function HeroListing({ t }) {
  const photos = [
    "assets/photos/house-pool-hero.jpeg",
    "assets/photos/deck-pool-day.jpeg",
    "assets/photos/living-day.jpeg",
    "assets/photos/bedroom-pool-view.jpeg",
    "assets/photos/sunset-kites-sun.jpeg",
  ];
  return (
    <section id="top" data-screen-label="01 Hero" style={{ paddingTop: "24px" }}>
      <div className="container-narrow">
        {/* Title row */}
        <div className="listing-title-row reveal">
          <div>
            <div className="eyebrow">Ilha do Guajirú · Ceará, Brasil</div>
            <h1 className="listing-title">{t.listing.title}</h1>
          </div>
          <div className="listing-actions">
            <button className="icon-btn-outline" aria-label={t.listing.share}>
              <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden="true">
                <path d="M11 5 L 11 2 L 14 2 L 14 5 M 8 2 L 14 2 L 14 8 M 14 2 L 8 8 M 2 4 L 2 14 L 12 14 L 12 9"/>
              </svg>
              {t.listing.share}
            </button>
            <button className="icon-btn-outline" aria-label={t.listing.save}>
              <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden="true">
                <path d="M8 14 C 8 14 1 9 1 5 A 3 3 0 0 1 8 4 A 3 3 0 0 1 15 5 C 15 9 8 14 8 14 Z"/>
              </svg>
              {t.listing.save}
            </button>
          </div>
        </div>

        {/* Photo grid */}
        <div className="hero-photo-grid reveal">
          {photos.map((src, i) => (
            <div className="hero-photo" key={i}>
              <img src={src} alt="" loading={i === 0 ? "eager" : "lazy"} />
            </div>
          ))}
          <button className="show-all-photos">
            <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden="true">
              <rect x="1" y="1" width="6" height="6"/>
              <rect x="9" y="1" width="6" height="6"/>
              <rect x="1" y="9" width="6" height="6"/>
              <rect x="9" y="9" width="6" height="6"/>
            </svg>
            {t.listing.showAll}
          </button>
        </div>
      </div>
    </section>
  );
}

// ─── Listing detail (the main 2-col below the hero) ───────────
function ListingDetail({ t }) {
  return (
    <section id="listing" data-screen-label="02 Listing" style={{ paddingBottom: "32px" }}>
      <div className="container-narrow">
        <div className="listing-grid">
          {/* LEFT column */}
          <div className="listing-left">
            {/* Subtitle + meta + rating */}
            <div className="reveal" style={{ paddingTop: 16 }}>
              <h2 className="display-lg" style={{ marginBottom: 8 }}>{t.listing.subtitle}</h2>
              <div className="listing-meta">
                <span>{t.listing.meta}</span>
                <span className="dot">·</span>
                <span className="rating-inline">
                  <StarIcon /> {t.listing.rating}
                </span>
                <span className="dot">·</span>
                <a href="#reviews" className="reviews-link">{t.listing.reviewsCount}</a>
              </div>
            </div>

            {/* Highlights */}
            <div className="highlights reveal" data-delay="1">
              <div className="highlights-host">
                <div className="highlights-host-left">
                  <div className="host-avatar lg"><img src="assets/favicon.svg" alt="VENTI Kite Homes" /></div>
                  <div>
                    <div className="highlights-host-title">{t.highlights.hostTitle}</div>
                    <div className="highlights-host-sub">{t.highlights.hostSub}</div>
                  </div>
                </div>
              </div>

              {t.highlights.rows.map((h, i) => (
                <div className="highlight-row" key={i}>
                  <span className="highlight-icon" dangerouslySetInnerHTML={{ __html: highlightIcons[h.icon] }} />
                  <div className="highlight-text">
                    <h4>{h.title}</h4>
                    <p>{h.body}</p>
                  </div>
                </div>
              ))}
            </div>

            {/* About this place */}
            <div className="section-block reveal">
              <h2 className="section-h2">{t.about.title}</h2>
              <p>{t.about.p1}</p>
              <p>{t.about.p2}</p>
              <button className="show-more-btn">
                {t.about.showMore} →
              </button>
            </div>

            {/* Sleeping arrangements */}
            <div className="section-block reveal">
              <h2 className="section-h2">{t.sleeping.title}</h2>
              <div className="sleeping-grid">
                {t.sleeping.beds.map((b, i) => (
                  <div className="sleeping-card" key={i}>
                    <svg className="bed-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden="true">
                      <path d="M2 12 L 22 12 L 22 20 L 2 20 Z"/>
                      <path d="M2 16 L 22 16"/>
                      <rect x="4" y="8" width="6" height="4" rx="1"/>
                      <rect x="14" y="8" width="6" height="4" rx="1"/>
                    </svg>
                    <h4>{b.name}</h4>
                    <p>{b.detail}</p>
                  </div>
                ))}
              </div>
            </div>

            {/* What this place offers */}
            <div className="section-block reveal" id="amenities">
              <h2 className="section-h2">{t.amenitiesList.title}</h2>
              <div className="amenities-grid">
                {t.amenitiesList.items.map((a, i) => (
                  <div className="amenity-row" key={i}>
                    <span dangerouslySetInnerHTML={{ __html: amenityIcons[a.icon] || amenityIcons.default }} />
                    <span>{a.label}</span>
                  </div>
                ))}
              </div>
              <button className="btn btn-secondary" style={{ marginTop: 24 }}>
                {t.amenitiesList.showAll}
              </button>
            </div>
          </div>

          {/* RIGHT column — Reservation card */}
          <div className="listing-right">
            <ReservationCard t={t} />
          </div>
        </div>
      </div>
    </section>
  );
}

// Date input: shows custom display text, native picker opens on click
function DateField({ value, onChange, min, placeholder }) {
  const ref = React.useRef();
  const fmt = (s) => {
    if (!s) return "";
    const [y, m, d] = s.split("-");
    return `${d}/${m}/${y}`;
  };
  const open = () => { try { ref.current.showPicker(); } catch(_) { ref.current.focus(); } };
  return (
    <div className="rc-datefield" onClick={open}>
      <span className={value ? "rc-datefield-val" : "rc-datefield-val placeholder"}>
        {value ? fmt(value) : placeholder}
      </span>
      <input
        ref={ref}
        type="date"
        min={min}
        value={value}
        onChange={onChange}
        className="rc-datefield-native"
        tabIndex={-1}
        aria-hidden="true"
      />
    </div>
  );
}

// ─── Animated number (rolls to the new value, NumberFlow-style) ──
function AnimatedNumber({ value, format }) {
  const [display, setDisplay] = useState_S(value);
  const displayRef = React.useRef(value);
  const rafRef = React.useRef(0);
  useEffect_S(() => {
    const from = displayRef.current;
    const to = value;
    if (from === to) return;
    const duration = 420;
    const start = performance.now();
    cancelAnimationFrame(rafRef.current);
    const tick = (now) => {
      const p = Math.min(1, (now - start) / duration);
      const eased = 1 - Math.pow(1 - p, 3); // easeOutCubic
      const current = Math.round(from + (to - from) * eased);
      displayRef.current = current;
      setDisplay(current);
      if (p < 1) rafRef.current = requestAnimationFrame(tick);
    };
    rafRef.current = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(rafRef.current);
  }, [value]);
  return <span className="rc-num">{format ? format(display) : display}</span>;
}

// ─── Reservation Card (custom Airbnb-style → Lodgify checkout) ──
function ReservationCard({ t }) {
  const [checkin,  setCheckin]  = useState_S("");
  const [checkout, setCheckout] = useState_S("");
  const [guests,   setGuests]   = useState_S("2");

  const nights = checkin && checkout
    ? Math.max(0, Math.round((new Date(checkout) - new Date(checkin)) / 86400000))
    : 0;
  const total       = nights * LODGIFY_PRICE_PER_NIGHT;
  const fmt         = (n) => "$" + n.toLocaleString("en-US");
  const nightsWord  = t.reservation.breakdown[0][0].split(" ").pop();
  const guestUnit   = t.reservation.guestsValue.split(" ").slice(1).join(" ");
  const today       = new Date().toISOString().split("T")[0];

  const handleReserve = (e) => {
    e.preventDefault();
    window.open(buildLodgifyURL(checkin, checkout, guests), "_blank");
  };

  return (
    <aside className="reservation-card" id="reserve" data-screen-label="Reservation Card">
      <div className="rc-price-row">
        <div>
          <span className="rc-price">{t.reservation.price}<span className="unit">{t.reservation.unit}</span></span>
        </div>
        <div className="rc-rating-inline">
          <StarIcon /> <b>{t.listing.rating}</b> · <a href="#reviews">{t.listing.reviewsCount}</a>
        </div>
      </div>

      <form className="rc-form" onSubmit={handleReserve}>
        <div className="rc-dates">
          <label className="rc-field">
            <div className="rc-field-label">{t.reservation.checkin}</div>
            <DateField
              value={checkin}
              onChange={(e) => setCheckin(e.target.value)}
              min={today}
              placeholder={t.reservation.addDate || "Añadir fecha"}
            />
          </label>
          <label className="rc-field">
            <div className="rc-field-label">{t.reservation.checkout}</div>
            <DateField
              value={checkout}
              onChange={(e) => setCheckout(e.target.value)}
              min={checkin || today}
              placeholder={t.reservation.addDate || "Añadir fecha"}
            />
          </label>
        </div>
        <label className="rc-field rc-field-guests">
          <div className="rc-field-label">{t.reservation.guests}</div>
          <select
            className="rc-date-input rc-guests-select"
            value={guests}
            onChange={(e) => setGuests(e.target.value)}
          >
            {[1,2,3,4,5,6].map((n) => (
              <option key={n} value={n}>{n} {guestUnit}</option>
            ))}
          </select>
        </label>
        <button type="submit" className="rc-cta">{t.reservation.cta}</button>
      </form>

      <p className="rc-disclaimer">{t.reservation.noCharge}</p>

      <div className="rc-breakdown">
        {nights > 0 ? (
          <>
            <div className="rc-breakdown-row">
              <span className="label">{fmt(LODGIFY_PRICE_PER_NIGHT)} × {nights} {nightsWord}</span>
              <span><AnimatedNumber value={total} format={fmt} /></span>
            </div>
            <div className="rc-breakdown-row">
              <span className="label">{t.reservation.breakdown[1][0]}</span>
              <span>{t.reservation.breakdown[1][1]}</span>
            </div>
            <div className="rc-breakdown-row">
              <span className="label">{t.reservation.breakdown[2][0]}</span>
              <span>{t.reservation.breakdown[2][1]}</span>
            </div>
            <div className="rc-total">
              <span>{t.reservation.totalLabel}</span>
              <span><AnimatedNumber value={total} format={fmt} /></span>
            </div>
          </>
        ) : (
          <>
            {t.reservation.breakdown.map(([k, v], i) => (
              <div className="rc-breakdown-row" key={i}>
                <span className="label">{k}</span>
                <span>{v}</span>
              </div>
            ))}
            <div className="rc-total">
              <span>{t.reservation.totalLabel}</span>
              <span>{t.reservation.total}</span>
            </div>
          </>
        )}
      </div>
    </aside>
  );
}

// ─── Rating block (the big 64px moment) ───────────────────────
function RatingBlock({ t }) {
  return (
    <section className="rating-block" id="reviews" data-screen-label="03 Reviews">
      <div className="container-narrow">
        <div className="rating-row reveal">
          <div>
            <div className="rating-big">
              <svg className="laurel" viewBox="0 0 32 64" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden="true">
                <path d="M 26 6 Q 12 16 12 32 Q 12 48 26 58"/>
                <path d="M 22 14 Q 16 18 18 26"/>
                <path d="M 20 26 Q 14 30 16 38"/>
                <path d="M 22 40 Q 16 44 18 52"/>
              </svg>
              <div>
                <div className="num">{t.listing.rating}</div>
              </div>
              <svg className="laurel r" viewBox="0 0 32 64" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden="true">
                <path d="M 26 6 Q 12 16 12 32 Q 12 48 26 58"/>
                <path d="M 22 14 Q 16 18 18 26"/>
                <path d="M 20 26 Q 14 30 16 38"/>
                <path d="M 22 40 Q 16 44 18 52"/>
              </svg>
            </div>
            <div className="rating-tag">{t.reviews.tag}</div>
            <div className="rating-sub">{t.reviews.sub}</div>
          </div>

          <div className="rating-bars">
            {t.reviews.categories.map(([k, v], i) => (
              <div className="rating-bar-col" key={i}>
                <h5>{k}</h5>
                <div className="rating-bar"><span style={{ width: `${v * 100 / 5}%` }} /></div>
                <div className="rating-bar-val">{v.toFixed(1)}</div>
              </div>
            ))}
          </div>
        </div>

        {/* Review excerpts */}
        <div className="reviews-grid reveal" data-delay="1">
          {t.reviews.items.map((r, i) => (
            <div className="review" key={i}>
              <div className="review-head">
                <div className="review-avatar">{r.name.charAt(0)}</div>
                <div>
                  <div className="review-name">{r.name}</div>
                  <div className="review-date">{r.from} · {r.date}</div>
                </div>
              </div>
              <div className="review-stars">
                <StarIcon size={11} /><StarIcon size={11} /><StarIcon size={11} /><StarIcon size={11} /><StarIcon size={11} />
                <span>·</span><span>{r.stayLength}</span>
              </div>
              <p>"{r.text}"</p>
            </div>
          ))}
        </div>

        <button className="btn btn-secondary" style={{ marginTop: 24 }}>
          {t.reviews.showAll}
        </button>
      </div>
    </section>
  );
}

// ─── Map / Where you'll be ────────────────────────────────────
function MapBlock({ t }) {
  return (
    <section className="map-block" id="location" data-screen-label="04 Location">
      <div className="container-narrow">
        <div className="coords-row reveal">
          <div className="coords-title">{t.coords?.title || t.location.title}</div>
          <div className="coord">{t.coords?.value || "02° 48′ S · 39° 56′ W"}</div>
        </div>
        <div className="map-frame reveal" data-delay="1">
          <iframe
            src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3984.750078969773!2d-39.917457899999995!3d-2.8882561000000004!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x7c1ff7c8c95cd5b%3A0x5028c0ae0b3e0032!2sVenti%20Kite%20Homes!5e0!3m2!1ses-419!2suy!4v1780058695703!5m2!1ses-419!2suy"
            title="Venti Kite Homes — Ilha do Guajirú"
            style={{ border: 0 }}
            allowFullScreen
            loading="lazy"
            referrerPolicy="no-referrer-when-downgrade"
          ></iframe>
        </div>
        <div className="map-caption">{t.location.caption}</div>
        <div className="map-sub">{t.location.sub}</div>
      </div>
    </section>
  );
}

// ─── Things to know ──────────────────────────────────────────
function ThingsToKnow({ t }) {
  return (
    <section className="section-block" data-screen-label="05 Things to know">
      <div className="container-narrow">
        <h2 className="section-h2 reveal">{t.know.title}</h2>
        <div className="know-grid reveal" data-delay="1">
          {t.know.columns.map((c, i) => (
            <div className="know-col" key={i}>
              <h4>{c.title}</h4>
              <ul>
                {c.items.map((it, j) => <li key={j}>{it}</li>)}
              </ul>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ─── Other houses (3 property cards) ─────────────────────────
function OtherHouses({ t }) {
  const goToOverview = () => window.open(LODGIFY_BASE_URL + "/en/overview", "_blank", "noopener");
  return (
    <section className="other-houses" id="houses" data-screen-label="06 Houses">
      <div className="container-narrow">
        <h2 className="section-h2 reveal">{t.others.title}</h2>
        <p className="body-md reveal" style={{ color: "var(--muted)", marginTop: 4 }} data-delay="1">{t.others.sub}</p>
        <div className="props-grid">
          {t.others.list.map((h, i) => (
            <div
              className="prop-card reveal from-scale"
              data-delay={String(Math.min(i + 1, 5))}
              key={i}
              role="link"
              tabIndex={0}
              aria-label={h.title}
              onClick={goToOverview}
              onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); goToOverview(); } }}
            >
              <div className="prop-photo">
                <img src={h.img} alt={h.title} loading="lazy" />
                {h.badge && <span className="prop-fav-badge">{h.badge}</span>}
                <svg className="prop-heart" viewBox="0 0 32 32" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden="true">
                  <path d="M16 28 C 16 28 4 20 4 12 A 6 6 0 0 1 16 8 A 6 6 0 0 1 28 12 C 28 20 16 28 16 28 Z" fill="rgba(0,0,0,0.5)"/>
                </svg>
                <div className="prop-dots">
                  <span className="active"/><span/><span/><span/><span/>
                </div>
              </div>
              <div className="prop-meta">
                <div className="prop-title">
                  <span>{h.title}</span>
                  <span className="rating"><StarIcon /> {h.rating}</span>
                </div>
                <div className="prop-sub">{h.sub}</div>
                <div className="prop-sub">{h.dates}</div>
                <div className="prop-price"><b>{h.price}</b> {h.priceUnit}</div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ─── Footer (Airbnb style) ────────────────────────────────────
function Footer({ t }) {
  return (
    <footer className="footer" data-screen-label="07 Footer">
      <div className="footer-inner">
        <div className="footer-cols">
          {t.foot.cols.map((c, i) => (
            <div className="footer-col" key={i}>
              <h4>{c.title}</h4>
              <ul>
                {c.links.map((l, j) => <li key={j}><a href={l.href}>{l.label}</a></li>)}
              </ul>
            </div>
          ))}
        </div>
        <div className="legal-band">
          <div className="legal-left">
            <span>© 2026 VENTI</span>
            <span>·</span>
            <a href="#">{t.foot.legal.privacy}</a>
            <span>·</span>
            <a href="#">{t.foot.legal.terms}</a>
          </div>
          <div className="legal-right">
            <div className="socials">
              <a href="https://www.instagram.com/ventikite" target="_blank" rel="noopener noreferrer" aria-label="Instagram">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden="true">
                  <rect x="2" y="2" width="20" height="20" rx="5"/>
                  <circle cx="12" cy="12" r="4"/>
                  <circle cx="18" cy="6" r="1" fill="currentColor"/>
                </svg>
              </a>
              <a href="#contact" aria-label="WhatsApp">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
                  <path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.92.54 3.79 1.57 5.41L2 22l4.81-1.6a9.9 9.9 0 0 0 5.23 1.51h.01c5.46 0 9.91-4.45 9.91-9.91 0-2.65-1.03-5.14-2.9-7.01A9.86 9.86 0 0 0 12.04 2zm5.74 14.13c-.25.69-1.42 1.31-1.97 1.39-.5.07-1.15.1-1.86-.12-.43-.13-.98-.31-1.69-.62-2.97-1.28-4.92-4.28-5.07-4.47-.15-.2-1.21-1.61-1.21-3.07s.77-2.18 1.04-2.48c.27-.3.59-.37.79-.37l.57.01c.18.01.42-.07.66.5.25.59.85 2.05.93 2.2.07.15.13.32.02.52-.1.2-.15.32-.3.5-.15.18-.31.4-.45.54-.15.15-.3.31-.13.61.18.3.78 1.29 1.68 2.09 1.16 1.03 2.13 1.35 2.43 1.51.3.15.48.13.66-.08.18-.2.76-.89.96-1.19.2-.3.4-.25.67-.15.27.1 1.74.82 2.04.97.3.15.5.22.57.35.08.13.08.74-.17 1.45z"/>
                </svg>
              </a>
            </div>
          </div>
        </div>
      </div>
    </footer>
  );
}

// ─── Sticky bottom bar (mobile) ───────────────────────────────
function StickyBottomBar({ t }) {
  return (
    <div className="sticky-bottom-bar" aria-hidden="false">
      <div className="sbb-left">
        <div className="sbb-price">{t.reservation.price}<span style={{fontSize:14,fontWeight:400}}> {t.reservation.unit}</span></div>
        <a href="#reviews" className="sbb-sub">{t.listing.rating} ★ · {t.listing.reviewsCount}</a>
      </div>
      <a className="sbb-cta" href={LODGIFY_BASE_URL + "/en/overview"} target="_blank" rel="noopener noreferrer">{t.reservation.cta}</a>
    </div>
  );
}

// ─── Helpers ──────────────────────────────────────────────────
function StarIcon({ size = 12 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 12 12" fill="currentColor" aria-hidden="true" style={{ display: "inline-block", verticalAlign: "middle" }}>
      <path d="M6 1 L 7.5 4.5 L 11 5 L 8.5 7.5 L 9 11 L 6 9.25 L 3 11 L 3.5 7.5 L 1 5 L 4.5 4.5 Z"/>
    </svg>
  );
}

const highlightIcons = {
  pool: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M2 18 c 2 0 2 -1 4 -1 s 2 1 4 1 s 2 -1 4 -1 s 2 1 4 1 s 2 -1 4 -1"/><path d="M2 22 c 2 0 2 -1 4 -1 s 2 1 4 1 s 2 -1 4 -1 s 2 1 4 1 s 2 -1 4 -1"/><path d="M7 14 V 6 a 2 2 0 0 1 4 0 M 14 14 V 6 a 2 2 0 0 1 4 0"/></svg>',
  kite: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"><path d="M 12 3 L 4 11 L 12 14 L 20 11 Z"/><path d="M 12 14 V 21 M 9 21 H 15"/></svg>',
  home: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M 3 10 L 12 3 L 21 10 V 21 H 3 Z"/><path d="M 9 21 V 14 H 15 V 21"/></svg>',
  key: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="8" cy="12" r="5"/><path d="M 13 12 L 22 12 M 18 12 V 16 M 21 12 V 14"/></svg>',
};

const amenityIcons = {
  pool: highlightIcons.pool,
  wifi: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"><path d="M 3 9 a 14 14 0 0 1 18 0"/><path d="M 6 13 a 9 9 0 0 1 12 0"/><path d="M 9 17 a 4 4 0 0 1 6 0"/><circle cx="12" cy="20" r="0.9" fill="currentColor"/></svg>',
  ac: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="2" y="5" width="20" height="9" rx="1.5"/><path d="M 6 18 c 1 0 1 -2 2 -2 s 1 2 2 2 m 4 0 c 1 0 1 -2 2 -2 s 1 2 2 2"/></svg>',
  kitchen: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="3" y="3" width="18" height="18" rx="1"/><circle cx="9" cy="9" r="2"/><circle cx="15" cy="9" r="2"/><path d="M 5 14 H 19 V 19 H 5 Z"/></svg>',
  tv: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="3" y="4" width="18" height="12" rx="1"/><path d="M 8 20 H 16 M 12 16 V 20"/></svg>',
  parking: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M 9 17 V 7 H 13 a 3 3 0 0 1 0 6 H 9"/></svg>',
  kite: highlightIcons.kite,
  bath: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M 4 11 V 6 a 2 2 0 0 1 4 0"/><path d="M 2 11 H 22 V 16 a 2 2 0 0 1 -2 2 H 4 a 2 2 0 0 1 -2 -2 Z"/><path d="M 5 18 V 21 M 19 18 V 21"/></svg>',
  shower: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M 8 4 V 7 M 4 7 H 12 V 11"/><path d="M 6 14 V 17 M 9 13 V 16 M 12 14 V 17 M 15 13 V 16"/></svg>',
  default: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="12" cy="12" r="9"/><path d="M 12 7 V 13 M 12 16 V 17"/></svg>',
};

Object.assign(window, {
  TopNav, HeroListing, ListingDetail, ReservationCard, RatingBlock, MapBlock, ThingsToKnow, OtherHouses, Footer, StickyBottomBar
});

// ─── Pillars (numbered concept block) ─────────────────────────
function PillarsBlock({ t }) {
  if (!t.pillars) return null;
  return (
    <section className="pillars-block" data-screen-label="02 Pillars">
      <div className="container-narrow">
        <div className="pillars-eyebrow reveal from-left">{t.pillars.eyebrow}</div>
        <h2 className="pillars-headline reveal from-left" data-delay="1">{t.pillars.headline}</h2>
        <div className="pillars-grid">
          {t.pillars.items.map((p, i) => (
            <div className="pillar reveal from-scale" data-delay={String(i + 1)} key={i}>
              <div className="pillar-num">{p.num}</div>
              <div className="pillar-title">{p.title}</div>
              <p className="pillar-body">{p.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

const WA_NUMBER = "59899347428";
const TOCHAT_WIDGET_ID = "whatapp-people-widget-f02ef37e-18c9-4363-afc7-8b2f3c10086e";
const LODGIFY_BASE_URL = "https://venti-brasil.lodgify.com";
const LODGIFY_CHECKOUT_URL = "https://checkout.lodgify.com/venti-brasil/en/#/807174";
const LODGIFY_PRICE_PER_NIGHT = 270;

function buildLodgifyURL(arrival, departure, guests) {
  if (arrival && departure) {
    const params = new URLSearchParams();
    params.set("arrival",   arrival);
    params.set("departure", departure);
    if (guests) params.set("adults", String(guests));
    return LODGIFY_CHECKOUT_URL + "?" + params.toString();
  }
  return LODGIFY_BASE_URL + "/en/overview";
}

function openWhatsApp(message) {
  window.open("https://wa.me/" + WA_NUMBER + "?text=" + encodeURIComponent(message), "_blank");
}

function openChatWidget() {
  // Try clicking the tochat.be floating button rendered by their script
  const widget = document.getElementById(TOCHAT_WIDGET_ID);
  if (widget) {
    const btn = widget.querySelector("button") ||
                widget.parentElement?.querySelector("button[class*='wpp'], button[class*='tochat'], button[class*='float']");
    if (btn) { btn.click(); return; }
    // Some versions use a sibling element as trigger
    const sibling = widget.previousElementSibling || widget.nextElementSibling;
    if (sibling && sibling.tagName !== "SCRIPT") { sibling.click(); return; }
  }
  // Fallback: open WhatsApp directly
  openWhatsApp("Hola! Me interesa saber más sobre VENTI Kite Homes.");
}

// ─── Contact block (tochat.be widget inline) ──────────────────
function ContactBlock({ t }) {
  if (!t.contact) return null;
  const c = t.contact;

  React.useEffect(() => {
    const PLACEHOLDER = "tochat-inline-placeholder";
    const moveAndOpen = () => {
      const widget = document.getElementById(TOCHAT_WIDGET_ID);
      const placeholder = document.getElementById(PLACEHOLDER);
      if (!widget || !placeholder) return false;
      if (widget.parentNode === placeholder) return true;
      placeholder.appendChild(widget);
      // Programmatically open the widget (click the main trigger button)
      const btn = widget.querySelector(".chatwith-mainbutton");
      if (btn) btn.click();
      return true;
    };

    if (!moveAndOpen()) {
      const timers = [200, 600, 1200, 2500].map(d => setTimeout(moveAndOpen, d));
      return () => timers.forEach(clearTimeout);
    }
  }, []);

  return (
    <section className="contact-block" id="contact" data-screen-label="07 Contact">
      <div className="container-narrow">
        <div className="contact-grid">
          <div className="contact-copy reveal from-left">
            <div className="eyebrow">{c.eyebrow}</div>
            <h2 className="contact-headline">{c.headline}</h2>
            <p className="contact-sub">{c.sub}</p>
            <div className="contact-meta">
              {c.meta.map(([k, v], i) => (
                <div className="contact-meta-row" key={i}>
                  <span className="k">{k}</span>
                  <span className="v">{v}</span>
                </div>
              ))}
            </div>
          </div>

          <div id="tochat-inline-placeholder" className="reveal" data-delay="1" />
        </div>
      </div>
    </section>
  );
}

// ─── Editorial Hero (display headline + overlapping image) ───
function EditorialHero({ t, layout = "overlap", photo = "assets/photos/house-pool-hero.jpeg" }) {
  if (!t.hero) return null;
  const h = t.hero;
  return (
    <section className={"editorial-hero layout-" + layout} id="hero" data-screen-label="01 Editorial Hero">
      <div className="container-narrow">
        <div className="eh-inner">
          <div className="eh-eyebrow reveal from-left">{h.eyebrow}</div>
          <h1 className="eh-headline reveal from-left" data-delay="1">{h.headline}</h1>

          <div className="eh-image">
            <img src={photo} alt={h.imageMeta} />
            <div className="eh-image-meta">
              <span className="meta-dot" aria-hidden="true"></span>
              <span>{h.imageMeta}</span>
            </div>
          </div>

          <div className="eh-body-row reveal" data-delay="2">
            <p className="eh-copy">{h.copy}</p>
            <div className="eh-actions">
              <a href="#listing" className="eh-pill">
                {h.cta}
                <span className="arrow" aria-hidden="true">
                  <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
                    <path d="M3 8 H 13 M 9 4 L 13 8 L 9 12"/>
                  </svg>
                </span>
              </a>
              <a href="#contact" className="eh-pill-ghost">
                {h.ctaSecondary}
              </a>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { EditorialHero, PillarsBlock, ContactBlock });

// ─── Place block ("El Lugar") ─────────────────────────────────
function PlaceBlock({ t }) {
  if (!t.place) return null;
  const p = t.place;
  return (
    <section className="place-block" id="kite" data-screen-label="03 The Place">
      <div className="container-narrow place-inner">
        <div className="place-eyebrow reveal from-left">{p.eyebrow}</div>
        <h2 className="place-headline reveal from-left" data-delay="1">{p.headline}</h2>

        <div className="place-grid">
          {p.cards.map((c, i) => (
            <div className="place-card reveal from-scale" data-delay={String(i + 1)} key={i}>
              <h3>{c.title}</h3>
              <p>{c.body}</p>
            </div>
          ))}
        </div>

        <div className="place-stats">
          {p.stats.map((s, i) => (
            <div className="place-stat reveal from-scale" data-delay={String(i + 1)} key={i}>
              <div className="stat-num">{s.num}</div>
              <div className="stat-label">{s.label}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ─── Benefits grid (6 motivos) ────────────────────────────────
const benefitIconSet = {
  wind:   <svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" aria-hidden="true"><path d="M3 10 H 18 a 4 4 0 1 0 -4 -4"/><path d="M3 16 H 24 a 4 4 0 1 1 -4 4"/><path d="M3 22 H 16"/></svg>,
  kite:   <svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round" aria-hidden="true"><path d="M 16 3 L 26 12 L 16 16 L 6 12 Z"/><path d="M 16 16 V 28 M 12 28 H 20"/></svg>,
  buggy:  <svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden="true"><circle cx="9" cy="22" r="3.5"/><circle cx="23" cy="22" r="3.5"/><path d="M 6 22 H 4 V 14 L 10 14 L 13 9 H 22 L 26 14 H 28 V 22 H 26 M 13 22 H 19"/></svg>,
  shield: <svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round" aria-hidden="true"><path d="M 16 3 L 26 7 V 16 C 26 22 21 27 16 29 C 11 27 6 22 6 16 V 7 Z"/><path d="M 11 16 L 14 19 L 21 12"/></svg>,
  pool:   <svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" aria-hidden="true"><path d="M4 22 c3 0 3-2 6-2 s3 2 6 2 s3-2 6-2 s3 2 6 2"/><path d="M4 27 c3 0 3-2 6-2 s3 2 6 2 s3-2 6-2 s3 2 6 2"/><path d="M10 20 V10 a6 6 0 0 1 12 0 v10"/><path d="M10 14 H22"/></svg>,
  house:  <svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden="true"><path d="M 4 14 L 16 4 L 28 14 V 28 H 4 Z"/><path d="M 12 28 V 18 H 20 V 28"/></svg>,
};
function BenefitsBlock({ t }) {
  if (!t.benefits) return null;
  const b = t.benefits;
  return (
    <section className="benefits-block" id="benefits" data-screen-label="04 Benefits">
      <div className="container-narrow">
        <div className="benefits-header">
          <div className="reveal from-left">
            <div className="eyebrow">{b.eyebrow}</div>
            <h2 className="benefits-headline">{b.headline}</h2>
          </div>
          <p className="benefits-sub reveal" data-delay="1">{b.sub}</p>
        </div>
        <div className="benefits-grid">
          {b.items.map((it, i) => (
            <div className="benefit reveal from-scale" data-delay={String(Math.min(i + 1, 5))} key={i}>
              <span className="benefit-icon">{benefitIconSet[it.icon] || benefitIconSet.house}</span>
              <h4>{it.title}</h4>
              <p>{it.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ─── Partners strip ──────────────────────────────────────────
function PartnersStrip({ t }) {
  if (!t.partners) return null;
  const p = t.partners;
  return (
    <section className="partners-strip" data-screen-label="Partners">
      <div className="container-narrow">
        <div className="partners-label">{p.label}</div>
        <div className="partners-row">
          {p.list.map((it, i) => (
            <span className={"partner " + (it.style || "")} key={i}>{it.name}</span>
          ))}
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { PlaceBlock, BenefitsBlock, PartnersStrip });

// ─── FAQ accordion ───────────────────────────────────────────
function FAQBlock({ t }) {
  if (!t.faq) return null;
  const f = t.faq;
  const [open, setOpen] = useState_S(0);
  return (
    <section className="faq-block" id="faq" data-screen-label="10 FAQ">
      <div className="container-narrow">
        <div className="faq-grid">
          <div className="faq-side">
            <div className="eyebrow reveal from-left">{f.eyebrow}</div>
            <h2 className="faq-headline reveal from-left" data-delay="1">{f.headline}</h2>
            <p className="reveal" data-delay="2">{f.sub}</p>
          </div>
          <div className="faq-list">
            {f.items.map((it, i) => (
              <div className="faq-item reveal" data-open={open === i ? "true" : "false"} data-delay={String(Math.min(i, 5))} key={i}>
                <button
                  type="button"
                  className="faq-q"
                  aria-expanded={open === i}
                  onClick={() => setOpen(open === i ? -1 : i)}
                >
                  <span>{it.q}</span>
                  <span className="faq-toggle" aria-hidden="true">
                    <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round">
                      <path d="M8 3 V 13 M 3 8 H 13"/>
                    </svg>
                  </span>
                </button>
                <div className="faq-a">
                  <div className="faq-a-inner">{it.a}</div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
}

// ─── Final CTA (dramatic full-bleed) ─────────────────────────
function FinalCTA({ t }) {
  if (!t.finalCta) return null;
  const fc = t.finalCta;
  return (
    <section className="final-cta" id="reserve-now" data-screen-label="12 Final CTA">
      <div
        className="final-cta-bg"
        style={{ backgroundImage: "url(assets/photos/sunset-kites-sun.jpeg)" }}
        aria-hidden="true"
      ></div>
      <div className="container-narrow final-cta-inner">
        <div className="eyebrow">{fc.eyebrow}</div>
        <h2 className="final-cta-headline">{fc.headline}</h2>
        <p className="final-cta-sub">{fc.sub}</p>
        <div className="final-cta-actions">
          <a href={LODGIFY_BASE_URL + "/en/overview"} target="_blank" rel="noopener noreferrer" className="fc-pill-light">
            {fc.ctaPrimary}
            <span className="arrow" aria-hidden="true">
              <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
                <path d="M3 8 H 13 M 9 4 L 13 8 L 9 12"/>
              </svg>
            </span>
          </a>
          <a href="#reserve" className="fc-pill-ghost">
            {fc.ctaSecondary}
          </a>
        </div>
        <div className="final-cta-meta">
          {fc.meta.map((m, i) => (
            <React.Fragment key={i}>
              {i > 0 && <span className="dot" aria-hidden="true"></span>}
              <span>{m}</span>
            </React.Fragment>
          ))}
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { FAQBlock, FinalCTA });
