/* UI-kit sections: info bar, ribbon, stats, how-it-works, services,
   the cut-guide lookbook, team roster, reviews, FAQ. Composes DS components.
   Elements tagged `bb-reveal` animate in on scroll (engine in index.html);
   grids stagger via a per-item `--r` index. Stats count up in view. */
(function () {
const D = window.BarbierBellechasseDesignSystem_cae01a;
const IMG = "../../assets/images/";
const { useState, useEffect, useRef } = React;
const { SectionHeading, ServiceCard, GuideCard, MemberCard, ReviewCard, FaqItem, Marquee, StepsBloc, StatBand, InfoBar, Button, Icon } = D;

function TopBar() {
  return <div className="bb-infobar-wrap"><InfoBar items={window.BB_DATA.infobar} /></div>;
}

function ServicesRibbon() {
  return <Marquee items={window.BB_DATA.marquee} />;
}

/* ---- Stats: the "chiffres géants" count up when scrolled into view ---- */
function parseStat(chiffre) {
  const m = String(chiffre).match(/(\D*)(\d+(?:[.,]\d+)?)(.*)/);
  if (!m) return null;
  const sep = m[2].indexOf(",") > -1 ? "," : ".";
  const decimals = (m[2].split(/[.,]/)[1] || "").length;
  return { prefix: m[1], target: parseFloat(m[2].replace(",", ".")), decimals, sep, suffix: m[3] };
}
function fmtStat(p, val) {
  let n = val.toFixed(p.decimals);
  if (p.sep === ",") n = n.replace(".", ",");
  return p.prefix + n + p.suffix;
}
function Stats() {
  const base = window.BB_DATA.stats;
  const parsed = base.map((s) => ({ ...s, p: parseStat(s.chiffre) }));
  const [display, setDisplay] = useState(() => parsed.map((s) => (s.p ? fmtStat(s.p, 0) : s.chiffre)));
  const ref = useRef(null);
  const done = useRef(false);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const finish = () => setDisplay(parsed.map((s) => s.chiffre));
    const run = () => {
      if (done.current) return;
      done.current = true;
      const reduce = window.matchMedia && window.matchMedia("(prefers-reduced-motion:reduce)").matches;
      if (reduce) return finish();
      const dur = 1150, t0 = performance.now();
      const tick = (t) => {
        const k = Math.min(1, (t - t0) / dur);
        const e = 1 - Math.pow(1 - k, 3);
        setDisplay(parsed.map((s) => (s.p ? fmtStat(s.p, s.p.target * e) : s.chiffre)));
        if (k < 1) requestAnimationFrame(tick); else finish();
      };
      requestAnimationFrame(tick);
    };
    if (!("IntersectionObserver" in window)) { run(); return; }
    const io = new IntersectionObserver((ents) => ents.forEach((x) => { if (x.isIntersecting) run(); }), { threshold: 0.4 });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  const items = base.map((s, i) => ({ ...s, chiffre: display[i] }));
  return (
    <section className="sec" style={{ paddingTop: "clamp(52px,7vw,84px)", paddingBottom: "clamp(52px,7vw,84px)" }}>
      <div className="wrap" ref={ref}><StatBand className="bb-reveal" items={items} align="center" /></div>
    </section>
  );
}

function HowItWorks() {
  return (
    <section id="demarche" className="sec">
      <div className="wrap">
        <SectionHeading className="bb-reveal" kicker="Comment ça marche" title="Votre première visite, sans stress" sub="Jamais mis les pieds ici ? Trois étapes, c'est tout." style={{ marginBottom: 48 }} />
        <StepsBloc className="bb-reveal d1" steps={window.BB_DATA.steps} />
      </div>
    </section>
  );
}

function Services() {
  const d = window.BB_DATA;
  return (
    <section id="services" className="sec" style={{ background: "var(--panel)" }}>
      <div className="wrap">
        <SectionHeading className="bb-reveal" kicker="Carte des prestations" title="Coupe, barbe & rasage" sub="Des prestations soignées, sans surprise sur la facture." style={{ marginBottom: 54 }} />
        <div className="g3">
          {d.services.map((s, i) => (
            <div key={i} className="bb-reveal" style={{ display: "grid", "--r": i }}>
              <ServiceCard index={i + 1} icon={s.icon} title={s.titre} desc={s.desc} price={s.prix} />
            </div>
          ))}
        </div>
        <p className="bb-reveal" style={{ textAlign: "center", marginTop: 32, fontFamily: "var(--font-mono)", fontSize: ".72rem", letterSpacing: ".06em", textTransform: "uppercase", color: "var(--on-bg-faint)" }}>{d.servicesNote}</p>
      </div>
    </section>
  );
}

/* Cut guide — the flagship "lookbook": family tickets filter a big feature
   stage (the coupe en vedette) backed by a scrollable filmstrip of every
   cut. Hover/tap a thumbnail to load it into the stage; pick to book. */
/* Le filtre par famille comme une ligne éditoriale : toutes les familles
   s'affichent en gros, les inactives sont fantomatiques. Un seul geste, une
   seule ligne — plus lisible et plus affirmé qu'une rangée de pastilles. */
function FamilyLine({ fams, countOf, fam, onPick }) {
  const [hover, setHover] = useState(null);
  return (
    <div style={{ display: "flex", flexWrap: "wrap", alignItems: "baseline", columnGap: "clamp(16px,2.2vw,30px)", rowGap: 6 }}>
      {fams.map((f) => {
        const on = f === fam, hot = hover === f;
        return (
          <button key={f} type="button" aria-pressed={on} onClick={() => onPick(f)} onMouseEnter={() => setHover(f)} onMouseLeave={() => setHover(null)} style={{
            appearance: "none", background: "none", border: 0, padding: "2px 0 6px", margin: 0, cursor: "pointer",
            fontFamily: "var(--font-giant)", textTransform: "uppercase", fontWeight: 400,
            fontSize: "clamp(1.55rem,3.4vw,2.9rem)", lineHeight: 1.04, letterSpacing: ".01em",
            display: "inline-flex", alignItems: "flex-start", gap: 5, position: "relative",
            color: on ? "var(--on-bg)" : (hot ? "color-mix(in srgb,var(--on-bg) 78%,transparent)" : "color-mix(in srgb,var(--on-bg) 56%,transparent)"),
            transition: "color .22s ease",
          }}>
            {f}
            <span style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: ".55em", lineHeight: 1.1, letterSpacing: ".04em", color: on ? "var(--accent-on-bg)" : "inherit", transition: "color .22s ease" }}>{String(countOf(f)).padStart(2, "0")}</span>
            <span aria-hidden="true" style={{ position: "absolute", left: 0, right: 0, bottom: 0, height: 3, background: "var(--accent)", transformOrigin: "left", transform: on ? "scaleX(1)" : "scaleX(0)", transition: "transform .3s cubic-bezier(.2,.9,.3,1)" }} />
          </button>
        );
      })}
    </div>
  );
}

function CutStage({ cut, no, onPick }) {
  return (
    <div key={cut.nom} style={{ animation: "bb-pop .52s cubic-bezier(.16,.86,.3,1) both" }}>
      <div style={{ background: "var(--cream)", border: "var(--border-ink-bold)", borderRadius: "var(--r-l)", overflow: "hidden", boxShadow: "var(--shadow-hard-l)" }}>
        <div aria-hidden="true" style={{ height: 8, backgroundImage: "var(--pole-stripe)", backgroundSize: "22px 100%", borderBottom: "2px solid var(--ink)" }} />
        <div style={{ position: "relative", aspectRatio: "16 / 11", overflow: "hidden", background: "var(--paper-2)" }}>
          <img src={IMG + cut.img} alt={cut.nom} style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", filter: "saturate(.95) contrast(1.05)" }} />
          <span aria-hidden="true" style={{ position: "absolute", inset: 0, mixBlendMode: "multiply", opacity: 0.05, backgroundImage: "var(--halftone)", backgroundSize: "var(--halftone-size)" }} />
          <span style={{ position: "absolute", top: 13, left: 13, fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: ".7rem", letterSpacing: ".06em", color: "var(--ink)", background: "var(--cream)", border: "1.5px solid var(--ink)", padding: "3px 8px", boxShadow: "var(--shadow-hard-xs)" }}>{"Nº " + String(no).padStart(2, "0")}</span>
        </div>
      </div>
      <div style={{ marginTop: "clamp(20px,2.5vw,30px)" }}>
        <p style={{ fontFamily: "var(--font-mono)", fontSize: ".72rem", letterSpacing: ".18em", textTransform: "uppercase", color: "var(--accent-on-bg)", margin: "0 0 12px" }}>La coupe en vedette</p>
        <h3 style={{ fontFamily: "var(--font-giant)", textTransform: "uppercase", fontWeight: 400, fontSize: "clamp(2.6rem,5vw,4.6rem)", lineHeight: 0.86, letterSpacing: ".01em", color: "var(--on-bg)", margin: 0 }}>{cut.nom}</h3>
        <div style={{ display: "flex", alignItems: "center", gap: 12, flexWrap: "wrap", margin: "16px 0 26px" }}>
          <span style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: ".7rem", letterSpacing: ".12em", textTransform: "uppercase", color: "var(--cream)", background: "var(--accent)", border: "1px solid color-mix(in srgb,var(--cream) 26%,transparent)", padding: "4px 10px", borderRadius: "var(--r-s)" }}>{cut.fam}</span>
          <span style={{ fontFamily: "var(--font-mono)", fontSize: ".72rem", letterSpacing: ".06em", color: "var(--on-bg-muted)" }}>Sur mesure au fauteuil · ciseaux &amp; tondeuse</span>
        </div>
        <Button onClick={onPick} href="#reservation" iconRight={<Icon name="ciseaux" size={16} />}>Choisir cette coupe</Button>
      </div>
    </div>
  );
}

function CutThumb({ cut, no, active, onHover, onPick, id }) {
  return (
    <button id={id} type="button" aria-label={cut.nom} onMouseEnter={onHover} onFocus={onHover} onClick={onPick} style={{
      appearance: "none", WebkitAppearance: "none", margin: 0, padding: 0, cursor: "pointer", textAlign: "left",
      display: "flex", flexDirection: "column", background: "var(--cream)", color: "var(--ink)",
      borderRadius: "var(--r-m)", overflow: "hidden",
      border: "2px solid " + (active ? "var(--accent)" : "var(--ink)"),
      boxShadow: active ? "var(--shadow-hard-s)" : "var(--shadow-hard-xs)",
      transform: active ? "translate(-2px,-2px)" : "none",
      transition: "transform .2s cubic-bezier(.2,.9,.3,1), box-shadow .2s ease, border-color .2s ease",
    }} className="bb-thumb">
      <span style={{ position: "relative", display: "block", aspectRatio: "4 / 5", overflow: "hidden", borderBottom: "2px solid var(--ink)" }}>
        <img src={IMG + cut.img} alt="" style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", filter: active ? "saturate(1) contrast(1.04)" : "saturate(.8) contrast(1.02) brightness(.94)", transition: "filter .2s ease" }} />
        <span aria-hidden="true" style={{ position: "absolute", inset: 0, mixBlendMode: "multiply", opacity: 0.05, backgroundImage: "var(--halftone)", backgroundSize: "var(--halftone-size)" }} />
        <span style={{ position: "absolute", top: 7, left: 7, fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: ".62rem", color: "var(--ink)", background: "var(--cream)", border: "1px solid var(--ink)", padding: "1px 5px" }}>{String(no).padStart(2, "0")}</span>
      </span>
      <span style={{ padding: "8px 9px 9px", fontFamily: "var(--font-giant)", textTransform: "uppercase", fontSize: ".95rem", lineHeight: 1.02, letterSpacing: ".01em", color: active ? "var(--accent)" : "var(--ink)", transition: "color .16s ease" }}>{cut.nom}</span>
    </button>
  );
}

function Guide({ onPickCut }) {
  const d = window.BB_DATA;
  const fams = ["Tous", ...new Set(d.guide.map((g) => g.fam))];
  const countOf = (f) => (f === "Tous" ? d.guide.length : d.guide.filter((g) => g.fam === f).length);
  const [fam, setFam] = useState("Tous");
  // Une vignette de la planche-contact de l'accueil pointe #coupe-N : on ouvre
  // la page avec cette coupe déjà en vedette plutôt que sur la première.
  const fromHash = () => {
    try {
      const m = (window.location.hash || "").match(/^#coupe-(\d+)$/);
      const g = m && d.guide[Number(m[1]) - 1];
      return g ? g.nom : null;
    } catch (e) { return null; }
  };
  const [hoverNom, setHoverNom] = useState(fromHash);
  const shown = fam === "Tous" ? d.guide : d.guide.filter((g) => g.fam === fam);
  const active = shown.find((g) => g.nom === hoverNom) || shown[0];
  const activeNo = d.guide.indexOf(active) + 1;
  return (
    <section id="guide" className="sec" style={{ background: "var(--panel)" }}>
      <div className="wrap">
        <SectionHeading className="bb-reveal" kicker="Ce qu'on fait, concrètement" title="Le guide des coupes" sub="Douze coupes signature. Filtrez par famille, parcourez le catalogue en bas — la vedette change à chaque coupe pointée." style={{ marginBottom: 30 }} />
        <div className="bb-reveal" style={{ marginBottom: "clamp(28px,4vw,44px)" }}>
          <p style={{ fontFamily: "var(--font-mono)", fontSize: ".7rem", letterSpacing: ".14em", textTransform: "uppercase", color: "var(--on-bg-faint)", margin: "0 0 14px" }}>Filtrer par famille</p>
          <FamilyLine fams={fams} countOf={countOf} fam={fam} onPick={(f) => { setFam(f); setHoverNom(null); }} />
        </div>
        <div className="bb-guide2 bb-reveal" style={{ display: "grid", gridTemplateColumns: "minmax(0,1.55fr) minmax(250px,.9fr)", gap: "clamp(26px,4vw,52px)", alignItems: "start" }}>
          <CutStage cut={active} no={activeNo} onPick={() => onPickCut && onPickCut(active.nom, activeNo)} />
          <div>
            <p style={{ fontFamily: "var(--font-mono)", fontSize: ".7rem", letterSpacing: ".14em", textTransform: "uppercase", color: "var(--on-bg-faint)", margin: "0 0 12px" }}>Le catalogue — survolez ou pointez</p>
            <div className="bb-cat" onMouseLeave={() => setHoverNom(null)} style={{ display: "grid", gridTemplateColumns: "repeat(2,1fr)", gap: 12, maxHeight: "clamp(440px,60vh,660px)", overflowY: "auto", paddingRight: 6, paddingBottom: 2 }}>
              {shown.map((g) => {
                const no = d.guide.indexOf(g) + 1;
                return <CutThumb key={g.nom} id={"coupe-" + no} cut={g} no={no} active={g.nom === active.nom} onHover={() => setHoverNom(g.nom)} onPick={() => onPickCut && onPickCut(g.nom, no)} />;
              })}
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

/* Team — an editorial roster: three portrait plates (name + role overlaid,
   blurb on hover) capped by a founder pull-quote. */
function MemberPlate({ member, photo, founder }) {
  const [h, setH] = useState(false);
  return (
    <div onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)} style={{ display: "flex", flexDirection: "column", height: "100%", borderRadius: "var(--r-l)", overflow: "hidden", border: "var(--border-ink-bold)", background: "var(--cream)", color: "var(--ink)", boxShadow: h ? "var(--card-shadow-hover, var(--shadow-hard-m-hover))" : "var(--card-shadow, var(--shadow-hard-m))", transform: h ? "translate(-3px,-3px)" : "none", transition: "transform .18s ease, box-shadow .18s ease" }}>
      <span aria-hidden="true" style={{ height: 6, flex: "none", backgroundImage: "var(--pole-stripe)", backgroundSize: "18px 100%", borderBottom: "2px solid var(--ink)" }} />
      <div className="bb-member-photo" style={{ position: "relative", aspectRatio: "4 / 5", overflow: "hidden", borderBottom: "var(--border-ink-bold)", background: "var(--paper-2)" }}>
        <img src={photo} alt={member.nom} style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", filter: "saturate(.95) contrast(1.04)", transform: h ? "scale(1.05)" : "none", transition: "transform .5s cubic-bezier(.2,.7,.2,1)" }} />
        <span aria-hidden="true" style={{ position: "absolute", inset: 0, mixBlendMode: "multiply", opacity: 0.05, backgroundImage: "var(--halftone)", backgroundSize: "var(--halftone-size)" }} />
        {founder && <span style={{ position: "absolute", top: 11, left: 11, fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: ".66rem", letterSpacing: ".12em", textTransform: "uppercase", color: "var(--cream)", background: "var(--accent)", border: "1.5px solid var(--ink)", padding: "3px 8px", boxShadow: "var(--shadow-hard-xs)" }}>Fondateur</span>}
      </div>
      <div style={{ padding: "14px 16px 16px", display: "flex", flexDirection: "column", flex: 1 }}>
        <div style={{ fontFamily: "var(--font-giant)", textTransform: "uppercase", fontSize: "1.5rem", lineHeight: 0.95, letterSpacing: ".01em" }}>{member.nom}</div>
        <div style={{ fontFamily: "var(--font-mono)", fontSize: ".7rem", letterSpacing: ".1em", textTransform: "uppercase", color: "var(--accent)", margin: "8px 0 10px" }}>{member.role}</div>
        <p style={{ margin: 0, fontFamily: "var(--font-body)", fontSize: ".9rem", lineHeight: 1.55, color: "var(--ink-soft)" }}>{member.desc}</p>
      </div>
    </div>
  );
}

/* La note du fondateur, pleine page : son portrait à gauche, sa parole en
   grand à droite sur l'encre. Un seul moment plein cadre dans toute la page —
   c'est ce qui lui donne son échelle. */
function FounderNote({ quote, signature }) {
  return (
    <div className="bb-founder" style={{ marginTop: "clamp(44px,6vw,78px)" }}>
      <style>{".bb-founder{position:relative;left:50%;width:100vw;margin-left:-50vw;display:grid;grid-template-columns:minmax(0,.62fr) minmax(0,1fr);background:var(--ink);border-top:3px solid var(--accent);border-bottom:3px solid var(--accent);overflow:hidden}.bb-founder-photo{position:relative;min-height:clamp(320px,42vw,540px);overflow:hidden}.bb-founder-photo>img{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;object-position:50% 28%;filter:saturate(.9) contrast(1.06)}.bb-founder-body{display:flex;flex-direction:column;justify-content:center;gap:clamp(20px,2.6vw,32px);padding:clamp(34px,5vw,72px) clamp(26px,5vw,78px)}@media(max-width:860px){.bb-founder{grid-template-columns:1fr}.bb-founder-photo{min-height:clamp(260px,58vw,380px)}}"}</style>
      <div className="bb-founder-photo">
        <img src={IMG + "equipe-1.jpg"} alt="" />
        <span aria-hidden="true" style={{ position: "absolute", inset: 0, background: "linear-gradient(90deg,rgba(18,12,6,.28),rgba(18,12,6,0) 42%,rgba(18,12,6,.55))" }} />
      </div>
      <div className="bb-founder-body">
        <span aria-hidden="true" style={{ fontFamily: "var(--font-display)", fontSize: "clamp(3rem,5vw,4.6rem)", lineHeight: 0.5, color: "var(--brick-light)" }}>{"\u201C"}</span>
        <blockquote style={{ margin: 0, fontFamily: "var(--font-display)", fontSize: "clamp(1.5rem,3.1vw,2.7rem)", lineHeight: 1.24, letterSpacing: "-.005em", color: "var(--cream)", maxWidth: "30ch", textWrap: "pretty" }}>{quote}</blockquote>
        <p style={{ margin: 0, fontFamily: "var(--font-mono)", fontSize: ".72rem", letterSpacing: ".14em", textTransform: "uppercase", color: "var(--brick-light)" }}>{signature}</p>
      </div>
    </div>
  );
}

function Team() {
  const d = window.BB_DATA;
  return (
    <section id="equipe" className="sec">
      <div className="wrap">
        <SectionHeading className="bb-reveal" kicker="Les mains de la maison" title="Notre équipe" sub="Trois barbiers, un même souci du détail." style={{ marginBottom: 44 }} />
        <div className="g3">
          {d.team.map((m, i) => (
            <div key={i} className="bb-reveal" style={{ display: "grid", "--r": i }}>
              <MemberPlate member={m} photo={IMG + "equipe-" + (i + 1) + ".jpg"} founder={i === 0} />
            </div>
          ))}
        </div>
        <div className="bb-reveal">
          <FounderNote quote={d.founder.quote} signature={d.founder.signature} />
        </div>
      </div>
    </section>
  );
}

function Reviews() {
  const d = window.BB_DATA;
  return (
    <section id="avis" className="sec" style={{ background: "var(--panel)" }}>
      <div className="wrap">
        <SectionHeading className="bb-reveal" kicker="Depuis le fauteuil" title="Le mot des habitués" sub="Ce qu'on entend entre deux coups de ciseaux." style={{ marginBottom: 54 }} />
        <div className="g3">
          {d.reviews.map((r, i) => (
            <div key={i} className="bb-reveal" style={{ display: "grid", "--r": i }}>
              <ReviewCard rating={r.note} name={r.nom} meta={r.meta} quote={r.texte} />
            </div>
          ))}
        </div>
        <p className="bb-reveal" style={{ textAlign: "center", marginTop: 32, fontFamily: "var(--font-mono)", fontSize: ".72rem", letterSpacing: ".06em", textTransform: "uppercase", color: "var(--on-bg-faint)" }}>{d.reviewsNote}</p>
      </div>
    </section>
  );
}

function Faq() {
  const d = window.BB_DATA;
  return (
    <section id="faq" className="sec">
      <div className="wrap" style={{ maxWidth: 860 }}>
        <SectionHeading className="bb-reveal" kicker="Bon à savoir" title="Questions fréquentes" style={{ marginBottom: 48 }} />
        <div className="bb-reveal d1" style={{ borderTop: "2px solid var(--line-cream)" }}>
          {d.faq.map((f, i) => (
            <FaqItem key={i} index={i + 1} question={f.q} answer={f.r} defaultOpen={i === 0} />
          ))}
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { TopBar, ServicesRibbon, Stats, HowItWorks, Services, Guide, Team, Reviews, Faq });
})();
