/* ClientBase — Homepage Hero + feature carousel + savings simulator */

/* Shared reveal hook — triggers a "visible" flag when element enters viewport */
const useReveal = (options = {}) => {
  const ref = React.useRef(null);
  const [visible, setVisible] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    if (typeof IntersectionObserver === "undefined") { setVisible(true); return; }
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) { setVisible(true); io.unobserve(e.target); }
      });
    }, { threshold: options.threshold ?? 0.2, rootMargin: options.rootMargin ?? "0px 0px -40px 0px" });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return [ref, visible];
};

/* ================================================================
   FLOW ARROW — traces itself when scrolled into view
================================================================ */
const FlowArrow = ({ caption, size = "md", step, totalSteps }) => {
  const dim = size === "sm" ? { w: 40, h: 60 } : { w: 60, h: 92 };
  const [ref, visible] = useReveal({ threshold: 0.35 });
  const pathLen = 100;
  return (
    <div ref={ref} style={{
      padding: "22px 0 16px",
      display: "flex", flexDirection: "column", alignItems: "center",
      textAlign: "center",
    }}>
      {step && (
        <div style={{
          display: "inline-flex", alignItems: "center", gap: 9,
          padding: "5px 12px 5px 5px",
          background: "var(--accent-soft)",
          border: "1px solid var(--accent-soft-2)",
          borderRadius: 999, marginBottom: 12,
          opacity: visible ? 1 : 0,
          transform: visible ? "translateY(0) scale(1)" : "translateY(-6px) scale(0.95)",
          transition: "opacity 0.45s ease, transform 0.5s cubic-bezier(0.22, 1, 0.36, 1)",
        }}>
          <span style={{
            width: 22, height: 22, borderRadius: 50,
            background: "var(--accent)", color: "white",
            display: "inline-flex", alignItems: "center", justifyContent: "center",
            fontFamily: "var(--ff-display)", fontWeight: 600, fontSize: 12,
            fontVariantNumeric: "tabular-nums",
          }}>{step}</span>
          <span style={{
            fontSize: 12, fontWeight: 580, color: "var(--accent-ink)",
            letterSpacing: "-0.005em",
          }}>
            Étape {step}{totalSteps ? ` sur ${totalSteps}` : ""}
          </span>
        </div>
      )}
      {caption && (
        <div style={{
          fontSize: 15.5, fontStyle: "italic",
          fontFamily: "var(--ff-display)",
          marginBottom: 14, color: "var(--accent-ink)",
          maxWidth: 460, lineHeight: 1.35,
          fontWeight: 500,
          opacity: visible ? 1 : 0,
          transform: visible ? "translateY(0)" : "translateY(-6px)",
          transition: "opacity 0.5s ease 0.05s, transform 0.5s ease 0.05s",
        }}>
          {caption}
        </div>
      )}
      <svg aria-hidden viewBox="0 0 54 84" style={{
        width: dim.w, height: dim.h,
        color: "var(--accent)",
        overflow: "visible",
        animation: visible ? "cbArrowPulse 2.4s ease-in-out 1.6s infinite" : "none",
      }}>
        <circle cx="27" cy="3" r="2" fill="currentColor"
          opacity={visible ? 0.3 : 0}
          style={{ transition: "opacity 0.3s ease 0.1s" }}/>
        <circle cx="27" cy="10" r="2.5" fill="currentColor"
          opacity={visible ? 0.55 : 0}
          style={{ transition: "opacity 0.3s ease 0.25s" }}/>
        <path d="M 27 18 C 48 34, 6 52, 27 74"
          stroke="currentColor" strokeWidth="3.6" fill="none"
          strokeLinecap="round"
          style={{
            strokeDasharray: pathLen,
            strokeDashoffset: visible ? 0 : pathLen,
            transition: "stroke-dashoffset 0.9s cubic-bezier(0.65, 0, 0.35, 1) 0.3s",
          }}/>
        <path d="M 16 64 L 27 77 L 38 64"
          stroke="currentColor" strokeWidth="3.6" fill="none"
          strokeLinecap="round" strokeLinejoin="round"
          style={{
            strokeDasharray: 40,
            strokeDashoffset: visible ? 0 : 40,
            transition: "stroke-dashoffset 0.35s ease 1.1s",
          }}/>
      </svg>
    </div>
  );
};

/* ================================================================
   HERO — cycling profession word, big centered type
================================================================ */
const PROFESSIONS = [
  "PO",
  "coiffeurs",
  "coiffeuses",
  "esthéticiennes",
  "masseurs",
  "masseuses",
  "maquilleuses",
  "tatoueurs",
  "tatoueuses",
  "barbiers",
  "sophrologues",
  "naturopathes",
  "hypnothérapeutes",
  "ostéopathes",
  "kinés",
  "podologues",
  "réflexologues",
  "diététiciens",
  "coachs sportifs",
  "profs de yoga",
  "profs de pilates",
  "psychologues",
  "orthophonistes",
  "photographes",
  "toiletteurs",
  "couturières",
  "thérapeutes",
  "microblading",
  "extensions de cils",
  "indépendants",
];

/* Comparateur "vs ton outil actuel" — affiché côté mobile dans le hero.
   Pour chaque outil : icône + 3 cons concrets. ClientBase a 3 pros constants. */
const TOOL_COMPARE = [
  { id: "carnet",   icon: "📓", label: "Carnet papier", cons: [
    "Aucun rappel auto = no-show garantis",
    "Si perdu, fichier client perdu",
    "Vous prenez tous les RDV au tél",
  ]},
  { id: "excel",    icon: "📊", label: "Excel / Google",  cons: [
    "Pas conçu pour la prise de RDV",
    "Pas de fiche client, pas de fidélité",
    "Aucune facture conforme",
  ]},
  { id: "planity",  icon: "💸", label: "Planity",       cons: [
    "Commission 1 à 2 € sur chaque RDV",
    "Vos clients voient 50 concurrents",
    "Que de l'agenda, rien d'autre",
  ]},
  { id: "iara",     icon: "🪙", label: "iara",          cons: [
    "Logo iara + bouton « Devenir pro » sur votre page de RDV",
    "Lien iarabeauty.com/pro/vous — pas un vrai domaine à vous",
    "Vos clientes découvrent d'autres pros via le catalogue iara",
  ]},
  { id: "insta",    icon: "📨", label: "Insta + DM",     cons: [
    "30 % des DM finissent dans Demandes",
    "10 à 15 messages pour 1 RDV",
    "Dépendance totale à l'algo Meta",
  ]},
  { id: "rien",     icon: "🤷", label: "Rien pour l'instant", cons: [
    "Tout à la mémoire = oublis garantis",
    "Aucune stat, aucun pilotage",
    "Compta = nuit blanche chaque trimestre",
  ]},
];

const CLIENTBASE_PROS = [
  "ZÉRO commission, jamais",
  "Tout-en-un : agenda + clients + factures + fidélité + RDV en ligne",
  "Vos données 100 % à vous, exportables en 1 clic",
];

/* Panneau de comparaison affiché après tap sur un outil. */
/* Tools that have a dedicated /comparatifs/<id> page — display a CTA. */
const TOOL_COMPARE_PAGES = {
  planity:  "/comparatifs/planity",
  iara:     "/comparatifs/iara",
};

const ComparatorPanel = ({ tool, onBack }) => (
  <div style={{
    animation: "cbCompareIn 0.45s cubic-bezier(.22,1,.36,1) both",
    display: "flex", flexDirection: "column", gap: 10,
  }}>
    <button onClick={onBack} style={{
      alignSelf: "flex-start",
      background: "transparent", border: "none", cursor: "pointer",
      fontFamily: "inherit", fontSize: 12, color: "var(--ink-4)",
      padding: "2px 4px",
      display: "inline-flex", alignItems: "center", gap: 4,
    }}>
      ← Choisir un autre outil
    </button>

    {/* Avec [outil] — rouge doux */}
    <div style={{
      padding: "12px 14px",
      background: "oklch(97% 0.025 25)", border: "1px solid oklch(90% 0.05 25)",
      borderRadius: 11,
    }}>
      <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 8 }}>
        <span style={{ fontSize: 18, lineHeight: 1 }}>{tool.icon}</span>
        <span style={{ fontSize: 12.5, fontWeight: 580, color: "oklch(38% 0.16 25)", textTransform: "uppercase", letterSpacing: "0.04em" }}>
          Avec {tool.label}
        </span>
      </div>
      <ul style={{ margin: 0, padding: 0, listStyle: "none", display: "flex", flexDirection: "column", gap: 5 }}>
        {tool.cons.map((c, i) => (
          <li key={i} className="cb-compare-line" style={{
            display: "flex", gap: 7, alignItems: "flex-start",
            fontSize: 12.5, color: "oklch(35% 0.10 25)", lineHeight: 1.4,
            animationDelay: `${0.1 + i * 0.06}s`,
          }}>
            <span style={{ color: "oklch(55% 0.18 25)", fontWeight: 700, flexShrink: 0 }}>✗</span>
            <span>{c}</span>
          </li>
        ))}
      </ul>
    </div>

    {/* Avec ClientBase — vert / accent */}
    <div style={{
      padding: "12px 14px",
      background: "var(--accent-soft)", border: "1px solid var(--accent-soft-2)",
      borderRadius: 11,
    }}>
      <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 8 }}>
        <span style={{ fontSize: 18, lineHeight: 1 }}>✦</span>
        <span style={{ fontSize: 12.5, fontWeight: 580, color: "var(--accent-ink)", textTransform: "uppercase", letterSpacing: "0.04em" }}>
          Avec ClientBase
        </span>
      </div>
      <ul style={{ margin: 0, padding: 0, listStyle: "none", display: "flex", flexDirection: "column", gap: 5 }}>
        {CLIENTBASE_PROS.map((p, i) => (
          <li key={i} className="cb-compare-line" style={{
            display: "flex", gap: 7, alignItems: "flex-start",
            fontSize: 12.5, color: "var(--ink)", lineHeight: 1.4,
            animationDelay: `${0.32 + i * 0.06}s`,
          }}>
            <span style={{ color: "var(--accent)", fontWeight: 700, flexShrink: 0 }}>✓</span>
            <span>{p}</span>
          </li>
        ))}
      </ul>
    </div>

    {TOOL_COMPARE_PAGES[tool.id] && (
      <a href={TOOL_COMPARE_PAGES[tool.id]} style={{
        marginTop: 2, alignSelf: "stretch",
        padding: "10px 12px", textAlign: "center",
        background: "var(--surface)", border: "1px solid var(--accent-soft-2)",
        borderRadius: 10, fontFamily: "inherit", fontSize: 12.5,
        fontWeight: 580, color: "var(--accent-ink)", textDecoration: "none",
        animation: "cbCompareIn 0.6s cubic-bezier(.22,1,.36,1) 0.5s both",
      }}>
        Voir le comparatif complet ClientBase vs {tool.label} →
      </a>
    )}
  </div>
);

const Hero = ({ go }) => {
  const [profIdx, setProfIdx] = React.useState(0);
  const [pickedTool, setPickedTool] = React.useState(null);
  React.useEffect(() => {
    const t = setInterval(() => setProfIdx(i => (i + 1) % PROFESSIONS.length), 2000);
    return () => clearInterval(t);
  }, []);

  return (
    <section style={{
      position: "relative", overflow: "hidden",
      padding: "70px 0 60px",
      background: "var(--bg)",
    }}>
      <style>{`
        @keyframes cbFadeSlide {
          from { opacity: 0; transform: translateY(8px); }
          to   { opacity: 1; transform: translateY(0); }
        }
        @keyframes cbArrowPulse {
          0%, 100% { transform: translateY(0); }
          50% { transform: translateY(4px); }
        }
        @keyframes cbWordReveal {
          0%   { opacity: 0; filter: blur(10px); transform: translateY(10px); }
          60%  { opacity: 1; filter: blur(0);    transform: translateY(0); }
        }
        @keyframes cbCompareIn {
          from { opacity: 0; transform: translateY(10px) scale(0.97); }
          to   { opacity: 1; transform: translateY(0) scale(1); }
        }
        @keyframes cbCompareLineIn {
          from { opacity: 0; transform: translateX(-6px); }
          to   { opacity: 1; transform: translateX(0); }
        }
        @keyframes cbCompareTilesIn {
          from { opacity: 0; transform: translateY(6px); }
          to   { opacity: 1; transform: translateY(0); }
        }
        .cb-tool-tile { transition: transform .12s ease, border-color .15s ease, background .15s ease; }
        .cb-tool-tile:hover { border-color: var(--accent-soft-2) !important; background: var(--surface) !important; }
        .cb-tool-tile:active { transform: scale(0.96); }
        .cb-tool-tiles-grid { animation: cbCompareTilesIn .35s cubic-bezier(.22,1,.36,1) both; }
        .cb-compare-line { animation: cbCompareLineIn .4s cubic-bezier(.22,1,.36,1) both; }

        /* DESKTOP — 2 colonnes propres, mosaïque 3 cartes symétriques */
        .cb-hero {
          display: grid;
          grid-template-columns: 1.05fr 1fr;
          gap: 56px;
          align-items: center;
        }
        .cb-hero-bento {
          display: grid;
          grid-template-columns: 1fr 1fr;
          grid-template-rows: 220px 130px;
          gap: 12px;
        }
        .cb-hero-card {
          background: var(--surface);
          border: 1px solid var(--line);
          border-radius: 16px;
          padding: 16px;
          box-shadow: var(--sh-2);
          text-align: left;
          animation: cbFadeSlide 0.6s cubic-bezier(.22,1,.36,1) both;
          min-width: 0; min-height: 0;
        }
        .cb-hero-card.f1 { grid-column: 1 / -1; animation-delay: 0.1s; }
        .cb-hero-card.f2 { animation-delay: 0.2s; }
        .cb-hero-card.f3 { animation-delay: 0.3s; }

        /* MOBILE — design ultra-simple, focalisé, intégré */
        @media (max-width: 920px) {
          .cb-hero {
            grid-template-columns: 1fr !important;
            gap: 28px !important;
            text-align: center;
          }
          .cb-hero-text-col { text-align: center; }
          .cb-hero-text-col .cb-hero-eyebrow { justify-content: center !important; }
          .cb-hero-text-col .cb-hero-sub { margin-left: auto; margin-right: auto; }

          /* Bloc texte allégé */
          .cb-hero-sub-desktop { display: none !important; }
          .cb-hero-sub-mobile  { display: block !important; }
          .cb-hero-trust { display: none !important; }

          /* CTAs symétriques : empilés, même largeur */
          .cb-hero-ctas {
            flex-direction: column !important;
            align-items: center !important;
            gap: 10px !important;
          }
          .cb-hero-ctas .btn {
            width: 100% !important;
            max-width: 340px !important;
          }
          /* On garde le SEUL bouton ghost desktop (.btn .btn-lg) pour que
             les 2 CTAs aient EXACTEMENT la même forme/padding/radius. */
          .cb-hero-cta-secondary-mobile { display: none !important; }

          /* Bento : on cache les 3 cartes desktop, on montre le sélecteur métier */
          .cb-hero-card.f1,
          .cb-hero-card.f2,
          .cb-hero-card.f3 { display: none !important; }
          .cb-hero-card.f-picker { display: flex !important; }
          .cb-hero-bento {
            grid-template-columns: 1fr !important;
            grid-template-rows: auto !important;
            max-width: 420px;
            margin: 0 auto;
          }
        }
        @media (max-width: 480px) {
          .cb-hero-text-col h1 { font-size: clamp(32px, 9.5vw, 44px) !important; }
        }
      `}</style>

      {/* Aurora indigo doux — cohérent avec le reste du site */}
      <div aria-hidden style={{
        position: "absolute", inset: 0, pointerEvents: "none",
        background: "radial-gradient(ellipse 50% 45% at 50% 25%, var(--accent-soft) 0%, transparent 70%)",
        opacity: 0.65,
      }}/>

      <div className="container" style={{ position: "relative" }}>
        <div className="cb-hero">
          {/* ========== COL TEXTE ========== */}
          <div className="cb-hero-text-col">
            <div className="cb-hero-eyebrow" style={{
              display: "inline-flex", alignItems: "center", gap: 8,
              padding: "5px 14px 5px 6px",
              background: "var(--surface)", border: "1px solid var(--line-strong)",
              borderRadius: 999, fontSize: 12.5, fontWeight: 520,
              color: "var(--ink-2)", marginBottom: 22,
            }}>
              <span style={{
                width: 18, height: 18, borderRadius: 50,
                background: "var(--accent)", color: "white",
                display: "inline-flex", alignItems: "center", justifyContent: "center",
                fontSize: 10, fontWeight: 700,
              }}>✦</span>
              Bêta · gratuit jusqu'au lancement
            </div>

            <h1 style={{
              fontSize: "clamp(36px, 5.6vw, 72px)", lineHeight: 1.05,
              letterSpacing: "-0.04em", fontWeight: 580, margin: 0,
            }}>
              Tout votre métier,<br/>
              <span style={{ color: "var(--accent)" }}>dans une seule app.</span>
            </h1>

            {/* Sous-titre — version desktop riche */}
            <p className="cb-hero-sub cb-hero-sub-desktop" style={{
              marginTop: 20, fontSize: 17, color: "var(--ink-2)",
              lineHeight: 1.55, maxWidth: 520,
            }}>
              Agenda, clients, factures, fidélité, RDV en ligne — <strong style={{ color: "var(--ink)" }}>sans jongler entre 4 outils</strong>.
              Pensée pour les{" "}
              <span style={{ color: "var(--accent)", fontWeight: 580, display: "inline-block" }}>
                <span key={profIdx} style={{
                  display: "inline-block",
                  animation: "cbWordReveal 0.55s cubic-bezier(0.22, 1, 0.36, 1)",
                }}>
                  {PROFESSIONS[profIdx]}
                </span>
              </span>.
            </p>

            {/* Sous-titre — version mobile courte */}
            <p className="cb-hero-sub cb-hero-sub-mobile" style={{
              display: "none",
              marginTop: 16, fontSize: 16, color: "var(--ink-2)",
              lineHeight: 1.5, maxWidth: 380,
            }}>
              Agenda, RDV en ligne, factures et fidélité sur une seule plateforme
            </p>

            <div className="cb-hero-ctas" style={{ marginTop: 22, display: "flex", gap: 10, flexWrap: "wrap", alignItems: "center" }}>
              <button className="btn btn-accent btn-lg" onClick={() => go("signup")}>
                Créer mon compte — c'est gratuit <Icon name="arrow" size={15}/>
              </button>
              {/* Desktop : vrai bouton ghost */}
              <button className="btn btn-ghost btn-lg cb-hero-cta-secondary-desktop" onClick={() => go("login")}>
                J'ai déjà un compte
              </button>
              {/* Mobile : simple lien discret sous le bouton principal */}
              <button onClick={() => go("login")}
                className="cb-hero-cta-secondary-mobile"
                style={{
                  display: "none",
                  background: "transparent", border: "none", cursor: "pointer",
                  color: "var(--ink-3)", fontSize: 13.5, fontFamily: "inherit",
                  textDecoration: "underline", padding: "4px 8px",
                  width: "100%", textAlign: "center",
                }}>
                J'ai déjà un compte
              </button>
            </div>

            <div className="cb-hero-trust" style={{
              marginTop: 16, display: "flex", alignItems: "center", gap: 14, flexWrap: "wrap",
              fontSize: 12.5, color: "var(--ink-4)",
            }}>
              <span>✓ Sans carte bancaire</span>
              <span style={{ opacity: 0.4 }}>•</span>
              <span>✓ Prêt en 10 minutes</span>
              <span style={{ opacity: 0.4 }}>•</span>
              <span>✓ ZÉRO commission</span>
            </div>
          </div>

          {/* ========== MOSAÏQUE — 3 cartes propres et symétriques ========== */}
          <div className="cb-hero-bento">
            {/* Carte mobile-only — Comparateur "vs ton outil actuel" */}
            <div className="cb-hero-card f-picker" style={{
              display: "none", // visible uniquement en mobile via CSS
              flexDirection: "column", gap: 12, overflow: "hidden",
            }}>
              {pickedTool ? (
                <ComparatorPanel
                  tool={TOOL_COMPARE.find(t => t.id === pickedTool)}
                  onBack={() => setPickedTool(null)}
                />
              ) : (
                <>
                  <div>
                    <div style={{ fontSize: 11.5, color: "var(--ink-4)", fontWeight: 580, textTransform: "uppercase", letterSpacing: "0.04em" }}>
                      ⚔️ Comparez
                    </div>
                    <div style={{ fontSize: 14.5, fontWeight: 580, color: "var(--ink)", marginTop: 4 }}>
                      Tu utilises quoi aujourd'hui ?
                    </div>
                    <div style={{ fontSize: 12.5, color: "var(--ink-3)", marginTop: 3 }}>
                      Touchez pour voir la différence avec ClientBase
                    </div>
                  </div>
                  <div className="cb-tool-tiles-grid" style={{
                    display: "grid", gridTemplateColumns: "1fr 1fr", gap: 6,
                  }}>
                    {TOOL_COMPARE.map((t, i) => (
                      <button key={t.id} onClick={() => setPickedTool(t.id)}
                        className="cb-tool-tile"
                        style={{
                          padding: "10px 8px",
                          background: "var(--bg-alt)",
                          border: "1px solid var(--line)",
                          borderRadius: 10, cursor: "pointer", fontFamily: "inherit",
                          display: "flex", flexDirection: "column", alignItems: "center", gap: 4,
                          animation: `cbCompareTilesIn .4s cubic-bezier(.22,1,.36,1) ${i * 0.04}s both`,
                        }}>
                        <span style={{ fontSize: 18, lineHeight: 1 }}>{t.icon}</span>
                        <span style={{ fontSize: 11.5, fontWeight: 520, color: "var(--ink-2)" }}>{t.label}</span>
                      </button>
                    ))}
                  </div>
                </>
              )}
            </div>

            {/* Carte hero — Agenda du jour (full width, plus grande) */}
            <div className="cb-hero-card f1" style={{ display: "flex", flexDirection: "column", gap: 10 }}>
              <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
                <div style={{
                  width: 30, height: 30, borderRadius: 9,
                  background: "var(--accent-soft)", color: "var(--accent-ink)",
                  display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
                }}>
                  <Icon name="calendar" size={15}/>
                </div>
                <div style={{ minWidth: 0 }}>
                  <div style={{ fontSize: 13.5, fontWeight: 580, color: "var(--ink)", lineHeight: 1.2 }}>Aujourd'hui</div>
                  <div style={{ fontSize: 11.5, color: "var(--ink-4)", marginTop: 1 }}>5 RDV · 2 créneaux libres</div>
                </div>
              </div>
              <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
                {[
                  ["09:00", "Léa M.",     "Coupe + brushing", "var(--accent)"],
                  ["10:30", "Sophie D.",  "Couleur racines",  "var(--accent)"],
                  ["14:00", "Camille R.", "Balayage long",    "var(--sage)"],
                ].map(([h, n, p, c]) => (
                  <div key={h} style={{
                    display: "flex", alignItems: "center", gap: 8, minWidth: 0,
                    padding: "7px 10px",
                    background: "var(--bg-alt)", borderRadius: 9,
                    borderLeft: `2.5px solid ${c}`,
                    fontSize: 12,
                  }}>
                    <span style={{ fontWeight: 600, color: "var(--ink-2)", minWidth: 38, fontVariantNumeric: "tabular-nums", flexShrink: 0 }}>{h}</span>
                    <span style={{ fontWeight: 520, color: "var(--ink)", flexShrink: 0 }}>{n}</span>
                    <span style={{ color: "var(--ink-4)", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", minWidth: 0 }}>· {p}</span>
                  </div>
                ))}
              </div>
            </div>

            {/* Carte 2 — CA + mini bar chart */}
            <div className="cb-hero-card f2" style={{ display: "flex", flexDirection: "column", justifyContent: "space-between" }}>
              <div>
                <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
                  <Icon name="chart" size={13} style={{ color: "var(--accent-ink)" }}/>
                  <span style={{ fontSize: 11, fontWeight: 580, color: "var(--ink-3)", textTransform: "uppercase", letterSpacing: "0.04em" }}>CA ce mois</span>
                </div>
                <div style={{
                  fontFamily: "var(--ff-display)", fontSize: 26, fontWeight: 600,
                  letterSpacing: "-0.025em", color: "var(--accent-ink)", marginTop: 6,
                  fontVariantNumeric: "tabular-nums",
                }}>
                  4 287 €
                </div>
              </div>
              <div style={{ display: "flex", gap: 3, alignItems: "flex-end", height: 26 }}>
                {[40, 65, 50, 80, 55, 90, 75].map((h, i) => (
                  <div key={i} style={{
                    flex: 1, height: `${h}%`,
                    background: i === 5 ? "var(--accent)" : "var(--accent-soft-2)",
                    borderRadius: "2px 2px 0 0",
                  }}/>
                ))}
              </div>
            </div>

            {/* Carte 3 — Lien public RDV (gradient indigo) */}
            <div className="cb-hero-card f3" style={{
              background: "linear-gradient(135deg, var(--accent) 0%, oklch(50% 0.22 300) 100%)",
              color: "white", border: "none",
              boxShadow: "0 14px 32px -16px var(--accent)",
              display: "flex", flexDirection: "column", justifyContent: "space-between",
            }}>
              <div>
                <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
                  <Icon name="link" size={13}/>
                  <span style={{ fontSize: 11, fontWeight: 580, opacity: 0.9, textTransform: "uppercase", letterSpacing: "0.04em" }}>Lien public</span>
                </div>
                <div style={{ fontSize: 13, fontWeight: 580, marginTop: 6, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
                  clientbase.fr/?book=lea
                </div>
              </div>
              <div style={{ fontSize: 11, opacity: 0.92, fontWeight: 500 }}>
                📲 3 RDV pris cette semaine
              </div>
            </div>
          </div>
        </div>

        {/* Flow arrow vers la suite */}
        <div style={{ marginTop: 56, textAlign: "center" }}>
          <FlowArrow step={1} totalSteps={3}
            caption="Commençons par explorer tout ce que ClientBase propose"/>
        </div>

        <Reveal delay={0.1}>
          <div id="features-preview">
            <FeatureCarousel/>
          </div>
        </Reveal>
      </div>
    </section>
  );
};

/* ================================================================
   FEATURE CAROUSEL — replaces the LiveDemo phone mockup
================================================================ */
const FEATURES = [
  { id: "agenda",   icon: "calendar", name: "Agenda",        desc: "Un agenda clair et interactif. Cliquez sur une case vide pour ajouter un RDV, marquez-le fait en un clic." },
  { id: "bookings", icon: "link",     name: "Prise de RDV",  desc: "Votre lien public à partager. Vos clients choisissent leur créneau elles-mêmes, pauses déjeuner et vacances respectées." },
  { id: "clients",  icon: "users",    name: "Clientes",      desc: "Toutes leurs infos, préférences et historique au même endroit. Contact direct par mail ou téléphone en un clic." },
  { id: "fidelite", icon: "gift",     name: "Fidélité",      desc: "Une carte numérique par client. Partagez un lien, elle suit sa progression en temps réel — vos règles à vous." },
  { id: "stats",    icon: "chart",    name: "Statistiques",  desc: "CA, prestations rentables, tendances. Graphiques interactifs, sans tableur Excel." },
  { id: "factures", icon: "invoice",  name: "Facturation",   desc: "Factures conformes aux normes françaises. SIRET, TVA, mentions légales — on s'occupe de tout." },
  { id: "stock",    icon: "box",      name: "Stock",         desc: "Alertes stock bas, ajustements rapides, vue d'ensemble de vos produits." },
];

const FeatureCard = ({ f, compact }) => (
  <div style={{
    background: "var(--surface)",
    border: "1px solid var(--line)",
    borderRadius: 16,
    padding: compact ? 22 : 32,
    boxShadow: "var(--sh-2)",
    minHeight: compact ? 220 : 260,
    position: "relative", overflow: "hidden",
    width: "100%", boxSizing: "border-box",
    textAlign: "center",
  }}>
    <div aria-hidden style={{
      position: "absolute", top: -40, left: "50%", transform: "translateX(-50%)",
      width: 280, height: 160, borderRadius: "50%",
      background: "radial-gradient(circle, var(--accent-soft) 0%, transparent 70%)",
      opacity: 0.6, pointerEvents: "none",
    }}/>
    <div style={{ position: "relative", display: "flex", flexDirection: "column", alignItems: "center" }}>
      <div style={{
        width: 52, height: 52, borderRadius: 14,
        background: "var(--accent-soft)", color: "var(--accent-ink)",
        display: "inline-flex", alignItems: "center", justifyContent: "center",
      }}>
        <Icon name={f.icon} size={26}/>
      </div>
      <h3 style={{
        fontSize: "clamp(22px, 2.6vw, 28px)", marginTop: 16,
        letterSpacing: "-0.02em",
      }}>{f.name}</h3>
      <p style={{
        marginTop: 10, fontSize: 15.5, color: "var(--ink-2)",
        lineHeight: 1.55, maxWidth: 520, margin: "10px auto 0",
      }}>
        {f.desc}
      </p>
      <div style={{ marginTop: 20, width: "100%", maxWidth: 360 }}>
        <FeatureMock id={f.id}/>
      </div>
    </div>
  </div>
);

const FeatureCarousel = () => {
  const isMobile = useIsMobile(720);
  const [idx, setIdx] = React.useState(0);
  const [auto, setAuto] = React.useState(true);
  const scrollRef = React.useRef(null);
  const programmaticRef = React.useRef(false);

  // Auto-advance only on desktop — never on mobile (avoids conflicts with user swipe)
  React.useEffect(() => {
    if (!auto || isMobile) return;
    const t = setInterval(() => setIdx(i => (i + 1) % FEATURES.length), 4500);
    return () => clearInterval(t);
  }, [auto, isMobile]);

  // Desktop pill/dot click: just update idx. Mobile dot click: also programmatically scroll.
  const pick = (i) => {
    setIdx(i);
    setAuto(false);
    if (isMobile && scrollRef.current) {
      const el = scrollRef.current;
      const card = el.children[i];
      if (card) {
        programmaticRef.current = true;
        // Centre la carte dans le scrollport (compense gap + padding)
        const target = card.offsetLeft - (el.clientWidth - card.clientWidth) / 2;
        el.scrollTo({ left: Math.max(0, target), behavior: "smooth" });
        setTimeout(() => { programmaticRef.current = false; }, 600);
      }
    }
  };

  // Mobile swipe handler — détermine l'index par la carte la plus proche du centre
  const handleScroll = () => {
    if (programmaticRef.current) return;
    if (!scrollRef.current) return;
    const el = scrollRef.current;
    const center = el.scrollLeft + el.clientWidth / 2;
    let nearest = 0, best = Infinity;
    for (let i = 0; i < el.children.length; i++) {
      const c = el.children[i];
      const cCenter = c.offsetLeft + c.clientWidth / 2;
      const d = Math.abs(cCenter - center);
      if (d < best) { best = d; nearest = i; }
    }
    if (nearest !== idx && nearest < FEATURES.length) setIdx(nearest);
  };

  const current = FEATURES[idx];

  if (isMobile) {
    return (
      <div style={{ maxWidth: 720, margin: "0 auto" }}>
        {/* Small helper — just an invitation, no scrollbar */}
        <div style={{
          textAlign: "center", marginBottom: 14,
          fontSize: 13, color: "var(--ink-4)",
          fontStyle: "italic", fontFamily: "var(--ff-display)",
          display: "flex", alignItems: "center", justifyContent: "center", gap: 8,
        }}>
          <span aria-hidden>←</span>
          glissez pour explorer les {FEATURES.length} modules
          <span aria-hidden>→</span>
        </div>

        {/* Scroll-snap swipe carousel */}
        <div
          ref={scrollRef}
          onScroll={handleScroll}
          className="cb-hide-scrollbar"
          style={{
            display: "flex", gap: 12,
            overflowX: "auto",
            scrollSnapType: "x mandatory",
            WebkitOverflowScrolling: "touch",
            marginLeft: -16, marginRight: -16,
            paddingLeft: 16, paddingRight: 16,
            scrollbarWidth: "none",
          }}
        >
          {FEATURES.map((f) => (
            <div key={f.id} style={{
              flexShrink: 0,
              width: "calc(100% - 32px)",
              scrollSnapAlign: "center",
              scrollSnapStop: "always",
            }}>
              <FeatureCard f={f} compact/>
            </div>
          ))}
        </div>

        {/* Dots — clickable for jump nav */}
        <div style={{
          marginTop: 20, display: "flex", gap: 6, justifyContent: "center",
        }}>
          {FEATURES.map((_, i) => (
            <button key={i} onClick={() => pick(i)} aria-label={`Module ${i + 1}`}
              style={{
                width: i === idx ? 24 : 7, height: 7,
                borderRadius: 999,
                background: i === idx ? "var(--accent)" : "var(--line-strong)",
                border: "none", cursor: "pointer", padding: 0,
                transition: "width 0.2s, background 0.2s",
              }}
            />
          ))}
        </div>
      </div>
    );
  }

  // ===== Desktop version — pills + card + dots =====
  return (
    <div style={{ maxWidth: 720, margin: "0 auto" }}>
      <div style={{
        display: "flex", gap: 6, flexWrap: "wrap", justifyContent: "center",
      }}>
        {FEATURES.map((f, i) => {
          const active = i === idx;
          return (
            <button key={f.id} onClick={() => pick(i)} style={{
              display: "inline-flex", alignItems: "center", gap: 6,
              padding: "7px 13px",
              background: active ? "var(--ink)" : "var(--surface)",
              color: active ? "var(--bg)" : "var(--ink-2)",
              border: "1px solid " + (active ? "var(--ink)" : "var(--line)"),
              borderRadius: 999,
              fontSize: 12.5, fontWeight: active ? 520 : 500,
              cursor: "pointer", fontFamily: "inherit",
              transition: "all 0.15s",
            }}>
              <Icon name={f.icon} size={13}/> {f.name}
            </button>
          );
        })}
      </div>

      <div style={{ marginTop: 16 }}>
        <div key={current.id} style={{ animation: "cbFadeSlide 0.35s ease" }}>
          <FeatureCard f={current}/>
        </div>
      </div>

      <div style={{
        marginTop: 18, display: "flex", gap: 6, justifyContent: "center",
      }}>
        {FEATURES.map((_, i) => (
          <button key={i} onClick={() => pick(i)} aria-label={`Module ${i + 1}`}
            style={{
              width: i === idx ? 20 : 6, height: 6,
              borderRadius: 999,
              background: i === idx ? "var(--accent)" : "var(--line-strong)",
              border: "none", cursor: "pointer", padding: 0,
              transition: "width 0.2s, background 0.2s",
            }}
          />
        ))}
      </div>
    </div>
  );
};

/* Tiny interactive mocks per feature — clickable to feel the product */

const AgendaMock = () => {
  const [done, setDone] = React.useState({});
  const list = [
    { time: "09:00", name: "Léa Morel",     svc: "Gel semi-perm.", c: ["var(--accent-soft)", "var(--accent)", "var(--accent-ink)"] },
    { time: "10:30", name: "Camille Petit", svc: "Pose complète",  c: ["var(--sage-soft)", "var(--sage)", "oklch(38% 0.08 160)"] },
    { time: "14:00", name: "Inès Dubois",   svc: "Remplissage",    c: ["oklch(97% 0.025 30)", "oklch(70% 0.14 30)", "oklch(42% 0.14 30)"] },
  ];
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
      {list.map((a, i) => {
        const d = done[i];
        return (
          <button key={i} onClick={() => setDone(s => ({ ...s, [i]: !s[i] }))} style={{
            padding: "8px 12px", background: a.c[0],
            borderLeft: `3px solid ${a.c[1]}`, borderRadius: 6,
            borderTop: "1px solid transparent", borderRight: "1px solid transparent", borderBottom: "1px solid transparent",
            fontSize: 12, display: "flex", gap: 10, alignItems: "center",
            cursor: "pointer", textAlign: "left", width: "100%",
            opacity: d ? 0.55 : 1, transition: "opacity .2s",
          }}>
            <span style={{ color: a.c[2], fontWeight: 600, minWidth: 40, fontVariantNumeric: "tabular-nums" }}>{a.time}</span>
            <div style={{ flex: 1 }}>
              <div style={{ fontWeight: 520, color: "var(--ink)", textDecoration: d ? "line-through" : "none" }}>{a.name}</div>
              <div style={{ fontSize: 11, color: "var(--ink-4)" }}>{a.svc}</div>
            </div>
            <span style={{
              width: 18, height: 18, borderRadius: 50,
              background: d ? a.c[1] : "transparent",
              border: `1.5px solid ${a.c[1]}`,
              display: "flex", alignItems: "center", justifyContent: "center",
              color: "white", flexShrink: 0,
            }}>
              {d && <Icon name="check" size={10} stroke={3}/>}
            </span>
          </button>
        );
      })}
      <div style={{ fontSize: 10.5, color: "var(--ink-4)", textAlign: "center", marginTop: 2 }}>
        Cliquez pour marquer fait
      </div>
    </div>
  );
};

const ClientsMock = () => {
  const [hl, setHl] = React.useState(null);
  const list = [
    { n: "Léa Morel",   v: 14, hue: 30,  mail: "lea@exemple.fr",   tel: "06 12 34 56 78" },
    { n: "Inès Dubois", v: 22, hue: 220, mail: "ines@exemple.fr",  tel: "06 98 76 54 32" },
    { n: "Sarah B.",    v: 5,  hue: 180, mail: "sarah@exemple.fr", tel: "06 11 22 33 44" },
  ];
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
      {list.map((c, i) => (
        <div key={i} style={{
          display: "flex", alignItems: "center", gap: 10,
          padding: "8px 10px", background: "var(--bg-alt)",
          border: "1px solid var(--line)", borderRadius: 8, fontSize: 12,
        }}>
          <div style={{
            width: 28, height: 28, borderRadius: 50,
            background: `linear-gradient(135deg, oklch(80% 0.12 ${c.hue}), oklch(70% 0.14 ${(c.hue+40)%360}))`,
            color: "white", display: "flex", alignItems: "center", justifyContent: "center",
            fontSize: 10.5, fontWeight: 600, flexShrink: 0,
          }}>{c.n.split(" ").map(x => x[0]).join("")}</div>
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ fontWeight: 520, color: "var(--ink)" }}>{c.n}</div>
            <div style={{ fontSize: 10.5, color: "var(--ink-4)", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
              {hl && hl.i === i ? (hl.k === "mail" ? c.mail : c.tel) : `${c.v} visites`}
            </div>
          </div>
          <button onClick={() => setHl({ i, k: "mail" })} style={{
            width: 26, height: 26, borderRadius: 6, cursor: "pointer",
            background: hl && hl.i === i && hl.k === "mail" ? "var(--accent)" : "var(--surface)",
            color: hl && hl.i === i && hl.k === "mail" ? "white" : "var(--ink-3)",
            border: "1px solid var(--line)",
            display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
          }} aria-label="Mail"><Icon name="mail" size={12}/></button>
          <button onClick={() => setHl({ i, k: "tel" })} style={{
            width: 26, height: 26, borderRadius: 6, cursor: "pointer",
            background: hl && hl.i === i && hl.k === "tel" ? "var(--accent)" : "var(--surface)",
            color: hl && hl.i === i && hl.k === "tel" ? "white" : "var(--ink-3)",
            border: "1px solid var(--line)",
            display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
          }} aria-label="Téléphone"><Icon name="phone" size={12}/></button>
        </div>
      ))}
    </div>
  );
};

const BookingsMock = () => {
  const [sel, setSel] = React.useState(1);
  const [copied, setCopied] = React.useState(false);
  const copy = () => { setCopied(true); setTimeout(() => setCopied(false), 1400); };
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
      <button onClick={copy} style={{
        padding: "8px 12px", background: "var(--bg-alt)",
        border: "1px solid var(--line)", borderRadius: 8,
        display: "flex", alignItems: "center", gap: 8,
        fontSize: 12, cursor: "pointer", textAlign: "left", width: "100%",
      }}>
        <Icon name="link" size={12} style={{ color: "var(--accent-ink)", flexShrink: 0 }}/>
        <span style={{ flex: 1, color: "var(--ink-2)", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
          clientbase.fr/?book=ongles-by-lea
        </span>
        <span style={{
          padding: "2px 8px", background: copied ? "var(--sage)" : "var(--ink)", color: "var(--bg)",
          borderRadius: 4, fontSize: 10, fontWeight: 600, transition: "background .2s",
        }}>{copied ? "Copié ✓" : "Copier"}</span>
      </button>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 4 }}>
        {["09:00", "10:30", "14:00", "15:30"].map((t, i) => (
          <button key={i} onClick={() => setSel(i)} style={{
            padding: "6px 4px", textAlign: "center", cursor: "pointer",
            background: i === sel ? "var(--accent)" : "var(--surface)",
            color: i === sel ? "white" : "var(--ink-2)",
            border: "1px solid " + (i === sel ? "var(--accent)" : "var(--line)"),
            borderRadius: 6, fontSize: 12, fontWeight: 520, fontVariantNumeric: "tabular-nums",
            transition: "all .15s",
          }}>{t}</button>
        ))}
      </div>
    </div>
  );
};

const FideliteMock = () => {
  const [stamps, setStamps] = React.useState(8);
  const [copied, setCopied] = React.useState(false);
  const copy = () => { setCopied(true); setTimeout(() => setCopied(false), 1400); };
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
      <div style={{
        padding: 16,
        background: "linear-gradient(135deg, var(--accent) 0%, oklch(45% 0.22 288) 100%)",
        borderRadius: 10, color: "white",
      }}>
        <div style={{ fontSize: 10.5, opacity: 0.85, textTransform: "uppercase", letterSpacing: "0.08em" }}>Léa Morel</div>
        <div style={{ marginTop: 12, display: "grid", gridTemplateColumns: "repeat(10, 1fr)", gap: 4 }}>
          {Array.from({ length: 10 }).map((_, i) => (
            <button key={i} onClick={() => setStamps(i + 1 === stamps ? i : i + 1)} style={{
              aspectRatio: 1, borderRadius: 50, padding: 0, cursor: "pointer",
              background: i < stamps ? "rgba(255,255,255,0.95)" : "rgba(255,255,255,0.15)",
              border: i >= stamps ? "1.5px dashed rgba(255,255,255,0.4)" : "1.5px solid transparent",
              display: "flex", alignItems: "center", justifyContent: "center",
              color: "var(--accent)", transition: "all .18s",
            }}>
              {i < stamps && <Icon name="check" size={10} stroke={3}/>}
            </button>
          ))}
        </div>
      </div>
      <button onClick={copy} style={{
        padding: "7px 10px", background: "var(--bg-alt)",
        border: "1px solid var(--line)", borderRadius: 8, cursor: "pointer",
        display: "flex", alignItems: "center", gap: 8, fontSize: 11.5, color: "var(--ink-2)",
        width: "100%",
      }}>
        <Icon name="link" size={11} style={{ color: "var(--accent-ink)" }}/>
        <span style={{ flex: 1, textAlign: "left" }}>Partager la carte à Léa</span>
        <span style={{
          padding: "2px 7px", background: copied ? "var(--sage)" : "var(--ink)", color: "var(--bg)",
          borderRadius: 4, fontSize: 10, fontWeight: 600,
        }}>{copied ? "Copié ✓" : "Copier"}</span>
      </button>
    </div>
  );
};

const StatsMock = () => {
  const [hov, setHov] = React.useState(null);
  const vals = [2.8, 3.1, 3.4, 3.2, 3.9, 4.3];
  const labels = ["N","D","J","F","M","A"];
  const max = 4.5;
  return (
    <div style={{
      padding: 14, background: "var(--bg-alt)",
      border: "1px solid var(--line)", borderRadius: 10,
    }}>
      <div style={{
        fontSize: 12, color: "var(--ink-4)", fontWeight: 500, marginBottom: 10,
        display: "flex", justifyContent: "space-between", alignItems: "center",
      }}>
        <span>CA — 6 derniers mois</span>
        <span style={{
          color: "var(--accent-ink)", fontWeight: 600, fontVariantNumeric: "tabular-nums",
          opacity: hov !== null ? 1 : 0, transition: "opacity .15s",
        }}>
          {hov !== null ? `${vals[hov].toFixed(1)}k €` : "—"}
        </span>
      </div>
      <div style={{ display: "flex", alignItems: "flex-end", gap: 6, height: 70 }}>
        {vals.map((v, i) => (
          <div key={i}
            onMouseEnter={() => setHov(i)} onMouseLeave={() => setHov(null)}
            onClick={() => setHov(hov === i ? null : i)}
            style={{ flex: 1, display: "flex", flexDirection: "column", alignItems: "center", gap: 4, cursor: "pointer" }}>
            <div style={{
              width: "100%", height: `${(v / max) * 100}%`,
              background: hov === i
                ? "var(--accent)"
                : `linear-gradient(to top, var(--accent) 0%, color-mix(in oklab, var(--accent) 60%, transparent) 100%)`,
              borderRadius: 4, transition: "background .15s",
            }}/>
            <div style={{ fontSize: 10.5, color: hov === i ? "var(--accent-ink)" : "var(--ink-4)", fontWeight: hov === i ? 600 : 500 }}>{labels[i]}</div>
          </div>
        ))}
      </div>
    </div>
  );
};

const FacturesMock = () => {
  const [paid, setPaid] = React.useState([true, true, false]);
  const rows = [
    ["F-0142", "Léa Morel",     "45,00 €"],
    ["F-0141", "Camille Petit", "60,00 €"],
    ["F-0140", "Inès Dubois",   "38,00 €"],
  ];
  return (
    <div style={{
      border: "1px solid var(--line)", borderRadius: 10, overflow: "hidden",
      background: "var(--surface)",
    }}>
      {rows.map(([id, name, amt], i) => (
        <div key={id} style={{
          padding: "9px 12px", fontSize: 12,
          display: "flex", alignItems: "center", gap: 10,
          borderBottom: i < 2 ? "1px solid var(--line)" : "none",
        }}>
          <span style={{ color: "var(--ink-4)", fontSize: 11.5, fontWeight: 500, fontVariantNumeric: "tabular-nums" }}>{id}</span>
          <span style={{ flex: 1, fontWeight: 520 }}>{name}</span>
          <span style={{ fontWeight: 600, fontVariantNumeric: "tabular-nums" }}>{amt}</span>
          <button onClick={() => setPaid(p => p.map((v, j) => j === i ? !v : v))} style={{
            padding: "2px 7px", fontSize: 10, fontWeight: 600, borderRadius: 4, cursor: "pointer",
            background: paid[i] ? "var(--sage-soft)" : "var(--warn-soft)",
            color: paid[i] ? "oklch(38% 0.08 160)" : "var(--warn-ink)",
            border: "none", transition: "background .15s",
          }}>{paid[i] ? "Payée" : "En attente"}</button>
        </div>
      ))}
    </div>
  );
};

const StockMock = () => {
  const [qty, setQty] = React.useState([12, 3, 6]);
  const items = ["Base coat OPI", "Top coat Gelish", "Vernis nude 42"];
  const bump = (i, d) => setQty(q => q.map((v, j) => j === i ? Math.max(0, v + d) : v));
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
      {items.map((n, i) => {
        const ok = qty[i] >= 5;
        return (
          <div key={i} style={{
            display: "flex", alignItems: "center", gap: 8,
            padding: "8px 12px", background: "var(--bg-alt)",
            border: "1px solid var(--line)", borderRadius: 8, fontSize: 12,
          }}>
            <div style={{ flex: 1, fontWeight: 520 }}>{n}</div>
            <button onClick={() => bump(i, -1)} style={{
              width: 22, height: 22, borderRadius: 5, border: "1px solid var(--line)",
              background: "var(--surface)", color: "var(--ink-2)", cursor: "pointer",
              display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 600,
            }}>−</button>
            <div style={{ minWidth: 22, textAlign: "center", color: "var(--ink-2)", fontWeight: 600, fontVariantNumeric: "tabular-nums" }}>{qty[i]}</div>
            <button onClick={() => bump(i, +1)} style={{
              width: 22, height: 22, borderRadius: 5, border: "1px solid var(--line)",
              background: "var(--surface)", color: "var(--ink-2)", cursor: "pointer",
              display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 600,
            }}>+</button>
            <span style={{
              padding: "2px 7px", fontSize: 10, fontWeight: 600, borderRadius: 4,
              background: ok ? "var(--sage-soft)" : "oklch(96% 0.04 30)",
              color: ok ? "oklch(38% 0.08 160)" : "oklch(48% 0.18 30)",
              transition: "all .15s",
            }}>{ok ? "OK" : "Bas"}</span>
          </div>
        );
      })}
    </div>
  );
};

const FeatureMock = ({ id }) => {
  if (id === "agenda")   return <AgendaMock/>;
  if (id === "clients")  return <ClientsMock/>;
  if (id === "bookings") return <BookingsMock/>;
  if (id === "fidelite") return <FideliteMock/>;
  if (id === "stats")    return <StatsMock/>;
  if (id === "factures") return <FacturesMock/>;
  if (id === "stock")    return <StockMock/>;
  return null;
};

/* ================================================================
   SAVINGS SIMULATOR — transparent calculations, explained hypotheses
================================================================ */
// Hypotheses (sources publiques, avril 2026) :
//   - 3 min gagnées par client (rappels SMS + réservation en ligne + fiche auto)
//   - avgTicket ~45 € (moyenne observée chez les indépendant·es prestation/esthétique)
//   - retentionBoost = +12 % de visites/an (étude NailPro 2023 sur les rappels auto)
//   - no-show : 7 % en moyenne avant, réduit à 2 % avec les rappels automatiques
//     (source : NoShow.fr baromètre 2024)
//   - toolsReplaced = 50 €/mois (Planity Essentiel)
//   - workDayHours = 7 h (journée type indé)
const SAVINGS_HYPOTHESES = {
  minPerClient: 3,
  avgTicket: 45,
  toolsReplaced: 50,
  retentionBoost: 0.12,
  avgVisitsPerClient: 3,
  noShowBefore: 0.07,
  noShowAfter: 0.02,
  workDayHours: 7,
};

const _fmtEUR = (n) => `${Math.round(n).toLocaleString("fr-FR")} €`;

const SavingsSimulator = () => {
  const [clients, setClients] = React.useState(60);
  const [ticket, setTicket] = React.useState(45);
  const [showDetails, setShowDetails] = React.useState(false);

  const h = SAVINGS_HYPOTHESES;
  const hoursPerMonth   = (clients * h.minPerClient) / 60;
  const hoursPerYear    = hoursPerMonth * 12;
  const daysPerYear     = hoursPerYear / h.workDayHours;

  const toolSavedYear   = h.toolsReplaced * 12;

  const extraVisitsYear = clients * h.avgVisitsPerClient * h.retentionBoost;
  const retentionRevYear= extraVisitsYear * ticket;

  const noShowAvoidedYr = clients * 12 * (h.noShowBefore - h.noShowAfter);
  const noShowRevenue   = noShowAvoidedYr * ticket;

  // Bonus : RDV pris tout seuls via le lien public (≈ 40 % des RDV, étude Planity 2024)
  const onlineShare     = 0.40;
  const onlineBookings  = Math.round(clients * 12 * onlineShare);

  // Bonus : appels téléphoniques évités (≈ 60 % des prises de RDV passaient par téléphone)
  const callsAvoided    = Math.round(clients * 12 * 0.6);

  const totalGainYear   = toolSavedYear + retentionRevYear + noShowRevenue;

  return (
    <section id="simulator" style={{ padding: "40px 0 48px" }}>
      <div className="container">
        <div style={{ maxWidth: 760, margin: "0 auto", textAlign: "center" }}>
          <h2 style={{ fontSize: "clamp(28px, 3.4vw, 42px)", letterSpacing: "-0.03em" }}>
            Combien pouvez-vous <span style={{ color: "var(--accent)" }}>générer avec ClientBase</span>&nbsp;?
          </h2>
          <p style={{
            marginTop: 8, fontSize: 14.5, color: "var(--ink-3)",
            maxWidth: 480, margin: "8px auto 0", lineHeight: 1.5,
          }}>
            Bougez les curseurs — les chiffres s'ajustent en direct sur vos vrais volumes.
          </p>

          {/* Sliders block */}
          <div style={{
            marginTop: 20, padding: "20px 22px",
            background: "var(--surface)", border: "1px solid var(--line)",
            borderRadius: 16,
            textAlign: "left",
          }}>
            <SimSlider
              label="Vous voyez environ"
              suffix="clients/mois"
              value={clients} min={10} max={250} step={5}
              ticks={["10", "80", "150", "250"]}
              onChange={setClients}
            />
            <div style={{ height: 10 }}/>
            <SimSlider
              label="Panier moyen"
              suffix="€ / prestation"
              value={ticket} min={15} max={150} step={5}
              ticks={["15 €", "60 €", "100 €", "150 €"]}
              onChange={setTicket}
            />
          </div>

          {/* HERO — gain annuel total */}
          <div style={{
            marginTop: 14, padding: "22px 24px",
            background: "linear-gradient(135deg, var(--accent) 0%, oklch(45% 0.22 300) 100%)",
            borderRadius: 18, color: "white",
            position: "relative", overflow: "hidden", textAlign: "left",
          }}>
            <div aria-hidden style={{
              position: "absolute", top: -40, right: -40,
              width: 180, height: 180, borderRadius: "50%",
              background: "rgba(255,255,255,0.14)", pointerEvents: "none",
            }}/>
            <div style={{ position: "relative" }}>
              <div style={{ fontSize: 12.5, opacity: 0.88, textTransform: "uppercase", letterSpacing: "0.08em", fontWeight: 520 }}>
                Gain estimé
              </div>
              <div style={{
                fontFamily: "var(--ff-display)", fontWeight: 600,
                fontSize: "clamp(44px, 7vw, 72px)", lineHeight: 1.02,
                letterSpacing: "-0.04em", marginTop: 6,
                fontVariantNumeric: "tabular-nums",
              }}>
                + <AnimatedNumber value={Math.round(totalGainYear)}/> €
                <span style={{ fontSize: "0.36em", opacity: 0.82, fontWeight: 500, fontFamily: "var(--ff-text)", marginLeft: 10 }}>
                  / an
                </span>
              </div>
              <div style={{ marginTop: 8, fontSize: 13.5, opacity: 0.92, lineHeight: 1.5, maxWidth: 480 }}>
                Soit environ <strong>{_fmtEUR(totalGainYear / 12)}</strong> de plus chaque mois dans votre poche,
                sans travailler une minute de plus.
              </div>
            </div>
          </div>

          {/* Breakdown cards — 2x2 (mobile) / 1x4 (desktop), toutes dynamiques */}
          <div style={{
            marginTop: 10, display: "grid",
            gridTemplateColumns: "repeat(4, 1fr)", gap: 8,
          }} className="sim-grid">
            <SimBreakdown
              icon="clock"
              value={<><AnimatedNumber value={Math.round(daysPerYear)}/> j</>}
              label="de journées libérées / an"
              sub={`≈ ${Math.round(hoursPerYear)} h économisées`}
              tone="violet"
            />
            <SimBreakdown
              icon="heart"
              value={<>+<AnimatedNumber value={Math.round(retentionRevYear)}/> €</>}
              label="de CA en plus (fidélité)"
              sub={`+${Math.round(extraVisitsYear)} visites / an`}
              tone="pink"
            />
            <SimBreakdown
              icon="shield"
              value={<><AnimatedNumber value={Math.round(noShowRevenue)}/> €</>}
              label="d'absences évitées"
              sub={`${Math.round(noShowAvoidedYr)} no-show en moins / an`}
              tone="sage"
            />
            <SimBreakdown
              icon="link"
              value={<><AnimatedNumber value={onlineBookings}/></>}
              label="RDV pris en ligne / an"
              sub={`${callsAvoided} appels évités`}
              tone="accent"
            />
          </div>

          {/* Method toggle */}
          <button onClick={() => setShowDetails(!showDetails)} style={{
            marginTop: 14,
            display: "inline-flex", alignItems: "center", gap: 6,
            padding: "6px 12px",
            background: "transparent", border: "1px solid var(--line)",
            borderRadius: 999, cursor: "pointer",
            fontSize: 12.5, color: "var(--ink-2)", fontFamily: "inherit",
            transition: "background 0.2s ease, border-color 0.2s ease, color 0.2s ease",
          }}
            onMouseEnter={e => { e.currentTarget.style.background = "var(--bg-alt)"; e.currentTarget.style.borderColor = "var(--line-strong)"; }}
            onMouseLeave={e => { e.currentTarget.style.background = "transparent"; e.currentTarget.style.borderColor = "var(--line)"; }}
          >
            <Icon name="sparkle" size={12} style={{
              transition: "transform .3s cubic-bezier(0.22, 1, 0.36, 1)",
              transform: showDetails ? "rotate(180deg)" : "rotate(0)",
            }}/>
            {showDetails ? "Masquer le détail du calcul" : "Comment on calcule ?"}
          </button>

          <div style={{
            display: "grid",
            gridTemplateRows: showDetails ? "1fr" : "0fr",
            opacity: showDetails ? 1 : 0,
            transition: "grid-template-rows .4s cubic-bezier(0.22, 1, 0.36, 1), opacity .3s ease",
          }}>
            <div style={{ overflow: "hidden" }}>
              <div style={{
                marginTop: 12, padding: "16px 18px", textAlign: "left",
                background: "var(--bg-alt)", border: "1px dashed var(--line-strong)",
                borderRadius: 12, fontSize: 13.5, color: "var(--ink-2)", lineHeight: 1.55,
              }}>
                <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 10 }}>
                  <li>
                    <strong>⏱ Temps gagné</strong> — <strong>{h.minPerClient} min par client</strong> grâce aux rappels SMS auto,
                    à la réservation en ligne et aux fiches mises à jour automatiquement.
                    Sur {clients} clients/mois → {hoursPerYear.toFixed(0)}&nbsp;h / an, soit <strong>{daysPerYear.toFixed(1)} journées</strong> de {h.workDayHours}&nbsp;h rendues à votre vie perso.
                  </li>
                  <li>
                    <strong>♥ Fidélité</strong> — les rappels auto + la carte numérique génèrent <strong>+{Math.round(h.retentionBoost * 100)}&nbsp;%</strong> de visites en plus&nbsp;:
                    {` `}{Math.round(extraVisitsYear)}&nbsp;visites × {ticket}&nbsp;€ = <strong>{_fmtEUR(retentionRevYear)}</strong>.
                    Source&nbsp;: <em>Impact of Automated Reminders on Client Retention</em> (NailPro, 2023).
                  </li>
                  <li>
                    <strong>🛡 Absences évitées</strong> — le no-show moyen passe de <strong>{Math.round(h.noShowBefore * 100)}&nbsp;%</strong> à <strong>{Math.round(h.noShowAfter * 100)}&nbsp;%</strong> avec les rappels SMS.
                    Soit {Math.round(noShowAvoidedYr)} RDV sauvés × {ticket}&nbsp;€ = <strong>{_fmtEUR(noShowRevenue)}</strong> par an.
                    Source&nbsp;: <em>Baromètre du no-show</em> (NoShow.fr, 2024).
                  </li>
                  <li>
                    <strong>🔗 RDV pris en ligne</strong> — ≈ <strong>40 %</strong> des réservations se font directement via votre lien public
                    (étude Planity 2024). Sur {clients} clients/mois × 12 → <strong>{onlineBookings} RDV pris tout seuls / an</strong> —
                    et autant d'appels que vous ne prenez plus pendant vos prestations.
                  </li>
                  <li>
                    <strong>€ Outils remplacés</strong> — un seul abonnement à la place de Planity (~{h.toolsReplaced}&nbsp;€/mois) = <strong>{toolSavedYear}&nbsp;€ / an</strong> inclus dans le total.
                  </li>
                </ul>
                <div style={{
                  marginTop: 12, paddingTop: 10, borderTop: "1px solid var(--line)",
                  fontSize: 12, color: "var(--ink-4)", fontStyle: "italic",
                }}>
                  Estimations. Votre situation varie selon votre métier et votre clientèle — écrivez-nous pour une estimation sur mesure.
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
};

const SimSlider = ({ label, suffix, value, min, max, step, ticks, onChange }) => (
  <div>
    <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", flexWrap: "wrap", gap: 8 }}>
      <div style={{ fontSize: 13, color: "var(--ink-3)", fontWeight: 500 }}>{label}</div>
      <div style={{
        fontFamily: "var(--ff-display)", fontSize: "clamp(22px, 3vw, 30px)",
        fontWeight: 580, letterSpacing: "-0.025em", color: "var(--accent-ink)",
        fontVariantNumeric: "tabular-nums",
      }}>
        <AnimatedNumber value={value}/>
        <span style={{ fontSize: 13, color: "var(--ink-3)", fontWeight: 450, fontFamily: "var(--ff-text)", marginLeft: 6 }}>
          {suffix}
        </span>
      </div>
    </div>
    <input type="range" min={min} max={max} step={step} value={value}
      onChange={(e) => onChange(+e.target.value)}
      style={{ width: "100%", accentColor: "var(--accent)", cursor: "pointer", marginTop: 8 }}
    />
    <div style={{ display: "flex", justifyContent: "space-between", marginTop: 2, fontSize: 11, color: "var(--ink-4)", fontVariantNumeric: "tabular-nums" }}>
      {ticks.map((t, i) => <span key={i}>{t}</span>)}
    </div>
  </div>
);

const SIM_TONES = {
  violet: { bg: "oklch(95% 0.06 268)", ink: "oklch(38% 0.18 268)" },
  pink:   { bg: "oklch(95% 0.05 340)", ink: "oklch(42% 0.14 340)" },
  sage:   { bg: "var(--sage-soft)",    ink: "oklch(38% 0.08 160)" },
  accent: { bg: "var(--accent-soft)",  ink: "var(--accent-ink)"   },
};
const SimBreakdown = ({ icon, value, label, sub, tone }) => {
  const t = SIM_TONES[tone] || SIM_TONES.accent;
  return (
    <div style={{
      padding: "14px 12px", textAlign: "left",
      background: "var(--surface)", border: "1px solid var(--line)",
      borderRadius: 12,
    }}>
      <div style={{
        width: 26, height: 26, borderRadius: 7,
        background: t.bg, color: t.ink,
        display: "flex", alignItems: "center", justifyContent: "center",
      }}>
        <Icon name={icon} size={13}/>
      </div>
      <div style={{
        fontFamily: "var(--ff-display)", fontSize: "clamp(18px, 2.4vw, 22px)",
        fontWeight: 600, letterSpacing: "-0.025em", color: "var(--ink)",
        marginTop: 8, fontVariantNumeric: "tabular-nums",
      }}>
        {value}
      </div>
      <div style={{ fontSize: 12, color: "var(--ink-3)", marginTop: 2, lineHeight: 1.35 }}>
        {label}
      </div>
      <div style={{ fontSize: 11, color: "var(--ink-4)", marginTop: 4, fontStyle: "italic" }}>
        {sub}
      </div>
    </div>
  );
};

/* Smoothly tween an integer — gives the slider numbers a bit of life */
const AnimatedNumber = ({ value }) => {
  const [display, setDisplay] = React.useState(value);
  const rafRef = React.useRef(null);
  const fromRef = React.useRef(value);
  const startRef = React.useRef(0);
  React.useEffect(() => {
    fromRef.current = display;
    startRef.current = performance.now();
    const dur = 260;
    const tick = (now) => {
      const t = Math.min(1, (now - startRef.current) / dur);
      const eased = 1 - Math.pow(1 - t, 3);
      const v = Math.round(fromRef.current + (value - fromRef.current) * eased);
      setDisplay(v);
      if (t < 1) rafRef.current = requestAnimationFrame(tick);
    };
    rafRef.current = requestAnimationFrame(tick);
    return () => { if (rafRef.current) cancelAnimationFrame(rafRef.current); };
    // eslint-disable-next-line
  }, [value]);
  return <>{display}</>;
};

const SimResultCard = ({ icon, value, label, basis }) => (
  <div style={{
    padding: "18px 14px", textAlign: "left",
    background: "var(--surface)", border: "1px solid var(--line)",
    borderRadius: 12,
  }}>
    <div style={{
      width: 30, height: 30, borderRadius: 8,
      background: "var(--accent-soft)", color: "var(--accent-ink)",
      display: "flex", alignItems: "center", justifyContent: "center",
    }}>
      <Icon name={icon} size={15}/>
    </div>
    <div style={{
      fontFamily: "var(--ff-display)", fontSize: "clamp(22px, 3vw, 30px)",
      fontWeight: 580, letterSpacing: "-0.03em", color: "var(--ink)",
      marginTop: 10,
    }}>
      {value}
    </div>
    <div style={{ fontSize: 12, color: "var(--ink-3)", marginTop: 1 }}>
      {label}
    </div>
    <div style={{
      fontSize: 11.5, color: "var(--ink-4)",
      marginTop: 8, lineHeight: 1.4, fontStyle: "italic",
    }}>
      {basis}
    </div>
  </div>
);

// Backwards-compat aliases
const SimNumber = SimResultCard;
const SimCard = SimResultCard;

/* Generic reveal wrapper — fades in + slides up when entering viewport */
const Reveal = ({ children, delay = 0, as = "div", ...rest }) => {
  const [ref, visible] = useReveal({ threshold: 0.15 });
  const Tag = as;
  return (
    <Tag ref={ref} {...rest} style={{
      ...rest.style,
      opacity: visible ? 1 : 0,
      transform: visible ? "translateY(0)" : "translateY(24px)",
      transition: `opacity 0.7s cubic-bezier(0.22, 1, 0.36, 1) ${delay}s, transform 0.7s cubic-bezier(0.22, 1, 0.36, 1) ${delay}s`,
      willChange: "opacity, transform",
    }}>{children}</Tag>
  );
};

Object.assign(window, {
  Hero, FeatureCarousel, FeatureCard, FeatureMock,
  SavingsSimulator, SimResultCard, SimNumber, SimCard,
  FlowArrow, Reveal, useReveal,
});
