/* ====== Componentes reutilizáveis ====== */
const { useState, useRef, useEffect, useMemo } = React;

const Icon = {
  back: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M19 12H5M12 19l-7-7 7-7"/></svg>),
  check: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M20 6L9 17l-5-5"/></svg>),
  chevron: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M6 9l6 6 6-6"/></svg>),
  info: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="10"/><path d="M12 16v-4M12 8h.01"/></svg>),
  spark: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 3l1.6 5.4L19 10l-5.4 1.6L12 17l-1.6-5.4L5 10l5.4-1.6L12 3z"/></svg>),
  tag: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M20.6 13.4l-7.2 7.2a2 2 0 0 1-2.8 0l-7-7A2 2 0 0 1 3 12.2V5a2 2 0 0 1 2-2h7.2a2 2 0 0 1 1.4.6l7 7a2 2 0 0 1 0 2.8z"/><circle cx="7.5" cy="7.5" r="1.3" fill="currentColor"/></svg>),
  wa: (p) => (<svg viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M17.5 14.4c-.3-.15-1.77-.87-2.04-.97-.27-.1-.47-.15-.67.15-.2.3-.77.97-.94 1.17-.17.2-.35.22-.65.07-.3-.15-1.26-.46-2.4-1.48-.89-.79-1.49-1.77-1.66-2.07-.17-.3-.02-.46.13-.61.13-.13.3-.35.45-.52.15-.17.2-.3.3-.5.1-.2.05-.37-.02-.52-.07-.15-.67-1.62-.92-2.22-.24-.58-.49-.5-.67-.51l-.57-.01c-.2 0-.52.07-.8.37-.27.3-1.04 1.02-1.04 2.48 0 1.46 1.07 2.88 1.22 3.08.15.2 2.1 3.2 5.08 4.49.71.31 1.26.49 1.69.62.71.23 1.36.2 1.87.12.57-.08 1.77-.72 2.02-1.42.25-.7.25-1.29.17-1.42-.07-.13-.27-.2-.57-.35zM12.05 21.5h-.01a9.4 9.4 0 0 1-4.8-1.32l-.34-.2-3.57.94.95-3.48-.22-.36a9.42 9.42 0 0 1-1.44-5.01c0-5.2 4.24-9.43 9.45-9.43 2.52 0 4.89.99 6.67 2.77a9.36 9.36 0 0 1 2.76 6.67c0 5.2-4.24 9.43-9.45 9.43zM20.36 3.7A11.32 11.32 0 0 0 12.05.27C5.79.27.7 5.36.7 11.62c0 2 .52 3.95 1.52 5.67L.6 23.4l6.27-1.65a11.3 11.3 0 0 0 5.4 1.38h.01c6.26 0 11.35-5.09 11.35-11.35 0-3.03-1.18-5.88-3.27-8.02z"/></svg>),
  whatsLine: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/></svg>),
};

/* Option card (single or multi) */
function Option({ label, selected, multi, onClick }) {
  return (
    <button type="button" className={"opt" + (multi ? " multi" : "") + (selected ? " selected" : "")} onClick={onClick}>
      <span className="tick"><Icon.check /></span>
      <span className="label">{label}</span>
    </button>
  );
}

/* Before / after showcase — grid de 3 colunas */
function ResultsShowcase({ photos }) {
  return (
    <div className="results">
      <div className="ba-grid">
        {photos.map((src, k) => (
          <div className="ba-cell" key={k}>
            <img src={src} alt="Antes e depois — harmonização facial" loading="lazy" />
          </div>
        ))}
      </div>
      <div className="rlabel">Resultados reais · Dra. Thayna Gonçalves</div>
    </div>
  );
}

/* Country selector + phone */
function PhonePicker({ country, setCountry, phone, setPhone, onEnter }) {
  const [open, setOpen] = useState(false);
  const [q, setQ] = useState("");
  const wrapRef = useRef(null);
  const list = useMemo(() => {
    const s = q.trim().toLowerCase();
    if (!s) return window.COUNTRIES;
    return window.COUNTRIES.filter(c =>
      c.n.toLowerCase().includes(s) || c.d.includes(s) || c.c.toLowerCase().includes(s)
    );
  }, [q]);

  useEffect(() => {
    function onDoc(e) { if (wrapRef.current && !wrapRef.current.contains(e.target)) setOpen(false); }
    document.addEventListener("mousedown", onDoc);
    return () => document.removeEventListener("mousedown", onDoc);
  }, []);

  return (
    <div className="field" style={{ position: "relative" }} ref={wrapRef}>
      <div className="phone-row">
        <button type="button" className="cc-btn" onClick={() => setOpen(o => !o)}>
          <span className="flag">{country.f}</span>
          <span className="dial">{country.d}</span>
          <Icon.chevron />
        </button>
        <input
          className="phone-input"
          type="tel"
          inputMode="tel"
          placeholder="11 99999-9999"
          value={phone}
          onChange={e => setPhone(e.target.value)}
          onKeyDown={e => { if (e.key === "Enter") onEnter && onEnter(); }}
          autoFocus
        />
      </div>
      {open && (
        <div className="cc-pop">
          <input className="cc-search" placeholder="Buscar país…" value={q} onChange={e => setQ(e.target.value)} autoFocus />
          <div className="cc-list">
            {list.map(c => (
              <button key={c.c} type="button" className={"cc-item" + (c.c === country.c ? " active" : "")}
                onClick={() => { setCountry(c); setOpen(false); setQ(""); }}>
                <span className="flag">{c.f}</span>
                <span className="nm">{c.n}</span>
                <span className="dl">{c.d}</span>
              </button>
            ))}
            {list.length === 0 && <div style={{ padding: "16px", color: "var(--ink-3)", fontSize: "14px" }}>Nenhum país encontrado.</div>}
          </div>
        </div>
      )}
    </div>
  );
}

Object.assign(window, { Icon, Option, ResultsShowcase, PhonePicker });
