/* ClientBase — shared UI: Logo, Nav, Footer, Icons + shared hooks */

// Mets à jour cette constante à chaque release + ajoute une entrée dans CB_CHANGELOG.
const CB_VERSION = "1.1";

const CB_CHANGELOG = [
  {
    version: "1.1",
    date: "2026-04-28",
    tag: "Stabilisation",
    title: "Cycle de bug fixes — pas de nouvelles features",
    items: [
      "V1 clôturée. Ce cycle n'ajoute plus de fonctionnalités : on stabilise.",
      "Corrections de bugs remontés par les bêta-testeurs et polish UI.",
      "Tout retour, signalement de bug ou incohérence : clientbase.fr@gmail.com.",
    ],
  },
  {
    version: "1.0",
    date: "2026-04-28",
    tag: "Bêta — clôturée",
    title: "Première version complète",
    items: [
      "Comptes Supabase + synchro temps réel multi-appareils",
      "Page publique de réservation (?book=) via lien partageable + slug personnalisable",
      "Pseudo réseau social du client à la réservation (Instagram, Facebook, TikTok, Snapchat, WhatsApp)",
      "Anniversaires clients épinglés sur la home + champ JJ/MM dans la fiche",
      "Greeting dynamique (matinée, déjeuner, après-midi, soirée) + fête / saint du jour",
      "Mode pause / vacances — bandeau doux sur la home, blocage des réservations en ligne",
      "Onboarding 3 cartes à la première connexion",
      "Toast Annuler 5s sur les actions importantes (RDV fait & facturé, paiement encaissé)",
      "Sparkline CA animée + compteur RDV en ease-out",
      "Comparatifs SEO ClientBase vs Planity / Treatwell / Fresha / Kiute Pro / iara",
      "Pages légales (RGPD, CGU, mentions, cookies), facture conforme avec mentions légales",
      "Mot de passe oublié, validation stricte, RGPD export et suppression compte",
      "Simulateur de gains, page Guide interactif, page d'invitation personnalisée",
      "Agenda mobile (strip 60 jours), marquer fait & facturer en 1 clic",
    ],
  },
  {
    version: "1.2",
    date: "À venir",
    tag: "Bientôt",
    title: "Rappels SMS + import/export",
    items: [
      "Rappels SMS automatiques 24 h avant chaque RDV (plan Business)",
      "Import des données bêta locales vers le cloud en un clic",
      "Export CSV / PDF de vos factures et clients",
    ],
    upcoming: true,
  },
  {
    version: "2.0",
    date: "À venir",
    tag: "Objectif 2026",
    title: "App native iOS & Android",
    items: [
      "Application mobile publiée sur l'App Store (iOS)",
      "Application mobile publiée sur le Play Store (Android)",
      "Notifications push natives (nouvelle réservation, RDV du jour, paiement reçu)",
      "Mode hors-ligne complet : consultez votre agenda même sans réseau",
      "Lancement commercial officiel — fin de la bêta, structure juridique déclarée",
    ],
    upcoming: true,
  },
];

const useIsMobile = (bp = 720) => {
  const [isMobile, setIsMobile] = React.useState(() =>
    typeof window !== "undefined" && window.matchMedia(`(max-width: ${bp}px)`).matches
  );
  React.useEffect(() => {
    const mq = window.matchMedia(`(max-width: ${bp}px)`);
    const h = (e) => setIsMobile(e.matches);
    if (mq.addEventListener) mq.addEventListener("change", h);
    else mq.addListener(h);
    return () => {
      if (mq.removeEventListener) mq.removeEventListener("change", h);
      else mq.removeListener(h);
    };
  }, [bp]);
  return isMobile;
};

const Icon = ({ name, size = 18, stroke = 1.6, style }) => {
  const s = size;
  const props = {
    width: s, height: s, viewBox: "0 0 24 24", fill: "none",
    stroke: "currentColor", strokeWidth: stroke,
    strokeLinecap: "round", strokeLinejoin: "round",
    style,
  };
  const paths = {
    calendar: <><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M3 9h18M8 3v4M16 3v4"/></>,
    users: <><circle cx="9" cy="8" r="3.5"/><path d="M2.5 20c0-3.6 2.9-6 6.5-6s6.5 2.4 6.5 6"/><circle cx="17" cy="9" r="2.5"/><path d="M21.5 18.5c0-2.5-2-4.5-4.5-4.5"/></>,
    chat: <><path d="M21 12a8 8 0 0 1-11.6 7.1L4 20l1-4.3A8 8 0 1 1 21 12z"/></>,
    heart: <><path d="M12 20s-7-4.5-9-9.2C1.8 7.1 4.1 4 7.3 4c2 0 3.5 1.1 4.7 2.6C13.2 5.1 14.7 4 16.7 4 19.9 4 22.2 7.1 21 10.8 19 15.5 12 20 12 20z"/></>,
    sparkle: <><path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8"/></>,
    chart: <><path d="M3 20V4M3 20h18"/><path d="M7 16l4-5 3 3 5-7"/></>,
    box: <><path d="M3 7l9-4 9 4v10l-9 4-9-4V7z"/><path d="M3 7l9 4 9-4M12 11v10"/></>,
    check: <path d="M5 12l4 4 10-10"/>,
    arrow: <path d="M5 12h14M13 5l7 7-7 7"/>,
    arrowUp: <path d="M7 17L17 7M7 7h10v10"/>,
    shield: <path d="M12 3l8 3v6c0 4.5-3.3 8.4-8 9-4.7-.6-8-4.5-8-9V6l8-3z"/>,
    zap: <path d="M13 2L4 14h7l-1 8 9-12h-7l1-8z"/>,
    menu: <><path d="M4 7h16M4 12h16M4 17h16"/></>,
    close: <><path d="M6 6l12 12M18 6L6 18"/></>,
    plus: <><path d="M12 5v14M5 12h14"/></>,
    search: <><circle cx="11" cy="11" r="7"/><path d="M20 20l-3.5-3.5"/></>,
    bell: <><path d="M18.2 15.5a3 3 0 0 1-.7-2V10a5.5 5.5 0 1 0-11 0v3.5a3 3 0 0 1-.7 2l-.6.7a.5.5 0 0 0 .4.8h12.8a.5.5 0 0 0 .4-.8l-.6-.7z"/><path d="M10 19.5a2 2 0 0 0 4 0"/></>,
    settings: <><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1A2 2 0 1 1 4.3 17l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1A2 2 0 1 1 7 4.3l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></>,
    euro: <><path d="M18 7a7 7 0 1 0 0 10M4 10h9M4 14h9"/></>,
    clock: <><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></>,
    lock: <><rect x="4" y="10" width="16" height="11" rx="2"/><path d="M8 10V7a4 4 0 0 1 8 0v3"/></>,
    eye: <><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="3"/></>,
    eyeOff: <><path d="M3 3l18 18"/><path d="M10.6 6.2A10.9 10.9 0 0 1 12 6c6.5 0 10 6 10 6a14 14 0 0 1-2.4 3M6.6 6.6C3.8 8.3 2 12 2 12s3.5 7 10 7a10.9 10.9 0 0 0 4.3-.9"/><path d="M9.5 9.5a3 3 0 0 0 4.2 4.2"/></>,
    mail: <><rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/></>,
    phone: <><path d="M5 4h4l2 5-2.5 1.5a11 11 0 0 0 5 5L15 13l5 2v4a2 2 0 0 1-2 2A16 16 0 0 1 3 6a2 2 0 0 1 2-2z"/></>,
    star: <path d="M12 3l2.9 5.9 6.5.9-4.7 4.6 1.1 6.5L12 17.8 6.2 20.9l1.1-6.5L2.6 9.8l6.5-.9L12 3z"/>,
    invoice: <><rect x="5" y="3" width="14" height="18" rx="2"/><path d="M8 8h8M8 12h8M8 16h5"/></>,
    tag: <><path d="M20 12L12 4H4v8l8 8 8-8z"/><circle cx="8" cy="8" r="1.2"/></>,
    gift: <><rect x="3" y="8" width="18" height="12" rx="1.5"/><path d="M12 8v12M3 12h18M7 8a2.5 2.5 0 1 1 5-2.5M17 8a2.5 2.5 0 1 0-5-2.5"/></>,
    link: <><path d="M10 14a5 5 0 0 0 7 0l3-3a5 5 0 0 0-7-7l-1 1"/><path d="M14 10a5 5 0 0 0-7 0l-3 3a5 5 0 0 0 7 7l1-1"/></>,
    copy: <><rect x="9" y="9" width="12" height="12" rx="2"/><path d="M5 15V5a2 2 0 0 1 2-2h10"/></>,
    external: <><path d="M14 4h6v6M20 4l-9 9M19 13v6a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h6"/></>,
    trash: <><path d="M4 7h16M10 11v6M14 11v6M5 7l1 13a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1l1-13M9 7V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v3"/></>,
  };
  return <svg {...props}>{paths[name] || null}</svg>;
};

const Logo = ({ size = 24, withWordmark = true }) => (
  <div style={{ display: "inline-flex", alignItems: "center", gap: 9 }}>
    <svg width={size} height={size} viewBox="0 0 32 32" style={{ display: "block" }}>
      <rect x="0" y="0" width="32" height="32" rx="8.5" fill="#5A50E6"/>
      <path d="M11.5 20.5 L20.5 11.5 M13.5 11.5 L20.5 11.5 L20.5 18.5"
        stroke="white" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
    </svg>
    {withWordmark && (
      <span style={{
        fontFamily: "var(--ff-display)", fontWeight: 600, fontSize: 18,
        letterSpacing: "-0.025em", color: "var(--ink)"
      }}>ClientBase</span>
    )}
  </div>
);

const NAV_LINKS = [
  { id: "features", label: "Guide" },
  { id: "pricing",  label: "Tarif" },
  { id: "contact",  label: "Contact" },
];

const OpenAppButton = ({ onClick, large }) => {
  const [hover, setHover] = React.useState(false);
  return (
    <button onClick={onClick}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        display: "inline-flex", alignItems: "center", gap: 8,
        padding: large ? "12px 20px" : "8px 16px 8px 14px",
        height: large ? 48 : 36,
        background: "linear-gradient(135deg, var(--accent) 0%, oklch(45% 0.22 288) 100%)",
        color: "white",
        border: "none", borderRadius: large ? 12 : 10,
        cursor: "pointer", fontFamily: "inherit",
        fontSize: large ? 15 : 13.5, fontWeight: 560,
        letterSpacing: "-0.005em",
        boxShadow: hover
          ? "0 10px 28px -8px color-mix(in oklab, var(--accent) 60%, transparent), 0 2px 4px color-mix(in oklab, var(--accent) 30%, transparent), inset 0 1px 0 rgba(255,255,255,0.2)"
          : "0 4px 14px -4px color-mix(in oklab, var(--accent) 45%, transparent), 0 1px 2px color-mix(in oklab, var(--accent) 25%, transparent), inset 0 1px 0 rgba(255,255,255,0.2)",
        transform: hover ? "translateY(-1px)" : "translateY(0)",
        transition: "transform 0.18s ease, box-shadow 0.18s ease",
        width: large ? "100%" : "auto",
        justifyContent: "center",
      }}>
      <span>Ouvrir l'app</span>
      <span style={{
        display: "inline-flex",
        transform: hover ? "translateX(3px)" : "translateX(0)",
        transition: "transform 0.2s ease",
      }}>
        <Icon name="arrow" size={large ? 16 : 13}/>
      </span>
    </button>
  );
};

const Nav = ({ page, go, user }) => {
  const [open, setOpen] = React.useState(false);
  const loggedIn = !!user;
  // Navigation depuis le menu mobile : ferme d'abord le menu proprement,
  // puis navigue après la fin de la transition pour éviter le "flash" où
  // la nouvelle page apparaît sous le menu encore ouvert.
  const goFromMenu = (p, opts) => {
    setOpen(false);
    setTimeout(() => go(p, opts), 220);
  };
  return (
    <header style={{
      position: "sticky", top: 0, zIndex: 40,
      background: "color-mix(in oklab, var(--bg) 82%, transparent)",
      backdropFilter: "saturate(140%) blur(14px)",
      WebkitBackdropFilter: "saturate(140%) blur(14px)",
      borderBottom: "1px solid var(--line)",
    }}>
      <div className="container" style={{
        display: "flex", alignItems: "center", justifyContent: "space-between",
        height: 64,
      }}>
        <a href="#home" onClick={(e) => { e.preventDefault(); go("home"); }} style={{ display: "inline-flex", alignItems: "center", gap: 8 }}>
          <Logo/>
          <span style={{
            padding: "2px 7px",
            background: "var(--accent-soft)",
            color: "var(--accent-ink)",
            fontSize: 9.5, fontWeight: 700,
            borderRadius: 4, letterSpacing: "0.08em",
            fontFamily: "var(--ff-mono)",
            textTransform: "uppercase",
          }}>bêta</span>
        </a>
        <nav style={{ display: "flex", gap: 2, alignItems: "center" }} className="nav-links">
          {NAV_LINKS.map(l => {
            const active = page === l.id;
            return (
              <a key={l.id} href={`#${l.id}`} onClick={(e) => { e.preventDefault(); go(l.id); }}
                style={{
                  padding: "8px 14px",
                  fontSize: 14, fontWeight: active ? 580 : 460,
                  color: active ? "var(--accent-ink)" : "var(--ink-3)",
                  background: active ? "var(--accent-soft)" : "transparent",
                  borderRadius: 999,
                  transition: "color .15s, background .15s",
                }}
                onMouseEnter={(e) => { if (!active) e.currentTarget.style.color = "var(--ink)"; }}
                onMouseLeave={(e) => { if (!active) e.currentTarget.style.color = "var(--ink-3)"; }}
              >
                {l.label}
              </a>
            );
          })}
        </nav>
        <div style={{ display: "flex", alignItems: "center", gap: 8 }} className="nav-cta">
          {loggedIn ? (
            <OpenAppButton onClick={() => go("app")}/>
          ) : (
            <>
              <a href="#login" onClick={(e) => { e.preventDefault(); go("login"); }}
                className="btn btn-ghost btn-sm" style={{ border: "none" }}>
                Connexion
              </a>
              <a href="#login" onClick={(e) => { e.preventDefault(); go("signup"); }}
                className="btn btn-accent btn-sm">
                Rejoindre la bêta
              </a>
            </>
          )}
        </div>
        <button className="nav-menu-btn" onClick={() => setOpen(!open)} aria-label="Menu" aria-expanded={open}
          style={{
            display: "none", background: open ? "var(--accent-soft)" : "transparent",
            border: `1px solid ${open ? "var(--accent-soft-2)" : "var(--line)"}`, borderRadius: 12,
            width: 42, height: 42, alignItems: "center", justifyContent: "center",
            cursor: "pointer", color: open ? "var(--accent-ink)" : "var(--ink)",
            transition: "background 0.2s ease, border-color 0.2s ease, color 0.2s ease",
          }}>
          <div style={{ width: 18, height: 12, position: "relative" }}>
            <span style={{
              position: "absolute", top: open ? 5 : 0, left: 0,
              width: 18, height: 2, background: "currentColor", borderRadius: 2,
              transform: open ? "rotate(45deg)" : "rotate(0)",
              transition: "transform 0.28s cubic-bezier(0.22, 1, 0.36, 1), top 0.28s cubic-bezier(0.22, 1, 0.36, 1)",
            }}/>
            <span style={{
              position: "absolute", top: 5, left: 0,
              width: 18, height: 2, background: "currentColor", borderRadius: 2,
              opacity: open ? 0 : 1,
              transform: open ? "translateX(8px)" : "translateX(0)",
              transition: "opacity 0.18s ease, transform 0.2s ease",
            }}/>
            <span style={{
              position: "absolute", bottom: open ? 5 : 0, left: 0,
              width: 18, height: 2, background: "currentColor", borderRadius: 2,
              transform: open ? "rotate(-45deg)" : "rotate(0)",
              transition: "transform 0.28s cubic-bezier(0.22, 1, 0.36, 1), bottom 0.28s cubic-bezier(0.22, 1, 0.36, 1)",
            }}/>
          </div>
        </button>
      </div>
      {/* Backdrop */}
      <div
        onClick={() => setOpen(false)}
        aria-hidden="true"
        style={{
          position: "fixed", inset: "64px 0 0 0", zIndex: 39,
          background: "color-mix(in oklab, var(--ink) 18%, transparent)",
          backdropFilter: "blur(2px)", WebkitBackdropFilter: "blur(2px)",
          opacity: open ? 1 : 0,
          pointerEvents: open ? "auto" : "none",
          transition: "opacity 0.28s ease",
        }}
      />
      {/* Mobile menu — animated slide-down */}
      <div style={{
        position: "absolute", top: 64, left: 0, right: 0, zIndex: 40,
        borderTop: "1px solid var(--line)",
        background: "var(--bg)",
        boxShadow: open ? "0 18px 40px -18px rgba(0,0,0,0.18)" : "none",
        maxHeight: open ? 520 : 0,
        opacity: open ? 1 : 0,
        overflow: "hidden",
        transition: "max-height 0.34s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.22s ease, box-shadow 0.3s ease",
      }}>
        <div style={{ padding: "14px 20px 22px" }}>
          {NAV_LINKS.map((l, i) => {
            const active = page === l.id;
            return (
              <a key={l.id} href={`#${l.id}`}
                onClick={(e) => { e.preventDefault(); goFromMenu(l.id); }}
                style={{
                  display: "flex", alignItems: "center", justifyContent: "space-between",
                  padding: "14px 14px",
                  margin: "2px 0",
                  color: active ? "var(--accent-ink)" : "var(--ink)",
                  background: active ? "var(--accent-soft)" : "transparent",
                  fontWeight: active ? 560 : 500, fontSize: 15,
                  borderRadius: 10,
                  opacity: open ? 1 : 0,
                  transform: open ? "translateX(0)" : "translateX(-12px)",
                  transition: `opacity 0.32s ease ${0.06 + i * 0.05}s, transform 0.32s cubic-bezier(0.22, 1, 0.36, 1) ${0.06 + i * 0.05}s`,
                }}>
                <span>{l.label}</span>
                <span aria-hidden style={{
                  color: active ? "var(--accent-ink)" : "var(--ink-4)",
                  fontSize: 18, lineHeight: 1,
                }}>›</span>
              </a>
            );
          })}
          <div style={{
            display: "flex", flexDirection: "column", gap: 10, marginTop: 18,
            paddingTop: 16, borderTop: "1px solid var(--line)",
            opacity: open ? 1 : 0,
            transform: open ? "translateY(0)" : "translateY(10px)",
            transition: `opacity 0.34s ease ${NAV_LINKS.length * 0.05 + 0.08}s, transform 0.34s cubic-bezier(0.22, 1, 0.36, 1) ${NAV_LINKS.length * 0.05 + 0.08}s`,
          }}>
            {loggedIn ? (
              <OpenAppButton large onClick={() => { goFromMenu("app"); }}/>
            ) : (
              <>
                <a onClick={() => { goFromMenu("signup"); }}
                  className="btn btn-accent btn-lg" style={{ cursor: "pointer", width: "100%" }}>
                  Rejoindre la bêta <Icon name="arrow" size={15}/>
                </a>
                <a onClick={() => { goFromMenu("login"); }}
                  className="btn btn-ghost btn-lg" style={{ cursor: "pointer", width: "100%" }}>
                  Se connecter
                </a>
              </>
            )}
          </div>
        </div>
      </div>
    </header>
  );
};

const Footer = ({ go }) => {
  const isMobile = useIsMobile(720);
  return (
    <footer style={{ borderTop: "1px solid var(--line)", background: "var(--bg-section)", marginTop: 0 }}>
      <div className="container" style={{ padding: isMobile ? "40px 24px 24px" : "64px 24px 32px" }}>
        {isMobile ? (
          /* Mobile: équilibré — tagline, email, 2 colonnes de liens, badges, signature */
          <div style={{ display: "flex", flexDirection: "column", gap: 20 }}>
            <div style={{ textAlign: "center" }}>
              <Logo/>
              <p style={{ fontSize: 13.5, color: "var(--ink-3)", lineHeight: 1.5, margin: "10px auto 0", maxWidth: 300 }}>
                L'outil tout-en-un pour les indépendants de service.
              </p>
            </div>

            <a href="mailto:clientbase.fr@gmail.com" style={{
              alignSelf: "center",
              padding: "8px 14px 8px 12px",
              background: "var(--surface)", border: "1px solid var(--line)",
              borderRadius: 999,
              fontSize: 13, color: "var(--accent-ink)", fontWeight: 520,
              display: "inline-flex", alignItems: "center", gap: 7,
              textDecoration: "none",
            }}>
              <Icon name="mail" size={13}/> clientbase.fr@gmail.com
            </a>

            {/* 2 colonnes de petits liens */}
            <nav style={{
              display: "grid", gridTemplateColumns: "1fr 1fr",
              columnGap: 24, rowGap: 2,
              padding: "16px 20px",
              borderTop: "1px solid var(--line)", borderBottom: "1px solid var(--line)",
            }}>
              {[
                { col: "Produit", items: [
                  ["Tarif",      "pricing"],
                  ["Guide",      "features"],
                  ["FAQ",        "faq"],
                  ["Nouveautés", "changelog"],
                ]},
                { col: "ClientBase", items: [
                  ["Contact",  "contact"],
                  ["À propos", "about"],
                  ["Légal",    "legal"],
                ]},
              ].map(group => (
                <div key={group.col} style={{ display: "flex", flexDirection: "column", gap: 4 }}>
                  <div style={{
                    fontSize: 11, fontWeight: 600, color: "var(--ink-4)",
                    textTransform: "uppercase", letterSpacing: "0.04em", marginBottom: 6,
                  }}>{group.col}</div>
                  {group.items.map(([label, page]) => (
                    <a key={page} href={`#${page}`} onClick={(e) => { e.preventDefault(); go(page); }}
                      style={{ fontSize: 13.5, color: "var(--ink-2)", padding: "4px 0", textDecoration: "none" }}>
                      {label}
                    </a>
                  ))}
                </div>
              ))}
            </nav>

            {/* Comparer — chips icônes premium */}
            <div style={{
              padding: "16px 14px",
              background: "var(--surface)", border: "1px solid var(--line)",
              borderRadius: 14, textAlign: "center",
            }}>
              <div style={{
                fontSize: 13, fontWeight: 580, color: "var(--ink)", marginBottom: 10,
              }}>
                ClientBase comparé à
              </div>
              <div style={{ display: "flex", flexWrap: "wrap", gap: 6, justifyContent: "center" }}>
                {[
                  { href: "/comparatifs/planity",   icon: "💸", label: "Planity" },
                  { href: "/comparatifs/iara",      icon: "🪙", label: "iara" },
                  { href: "/comparatifs/treatwell", icon: "🪙", label: "Treatwell" },
                  { href: "/comparatifs/fresha",    icon: "🌿", label: "Fresha" },
                ].map(c => (
                  <a key={c.href} href={c.href} style={{
                    display: "inline-flex", alignItems: "center", gap: 6,
                    padding: "6px 12px",
                    background: "var(--bg-alt)", border: "1px solid var(--line)",
                    borderRadius: 999, fontSize: 12.5, fontWeight: 520,
                    color: "var(--ink-2)", textDecoration: "none",
                  }}>
                    <span>{c.icon}</span><span>vs {c.label}</span>
                  </a>
                ))}
                <a href="/comparatifs" style={{
                  display: "inline-flex", alignItems: "center", gap: 4,
                  padding: "6px 12px",
                  background: "var(--accent-soft)", border: "1px solid var(--accent-soft-2)",
                  borderRadius: 999, fontSize: 12.5, fontWeight: 580,
                  color: "var(--accent-ink)", textDecoration: "none",
                }}>tous →</a>
              </div>
            </div>

            {/* Badges de rassurance */}
            <div style={{ display: "flex", gap: 6, justifyContent: "center", flexWrap: "wrap" }}>
              <span className="badge" style={{ background: "var(--sage-soft)", color: "oklch(38% 0.08 160)", borderColor: "oklch(88% 0.03 160)" }}>
                <Icon name="shield" size={12}/> RGPD · données chez vous
              </span>
              <a href="#changelog" onClick={(e) => { e.preventDefault(); go("changelog"); }}
                className="badge"
                style={{ background: "var(--accent-soft)", color: "var(--accent-ink)", borderColor: "var(--accent-soft-2)", textDecoration: "none" }}>
                <Icon name="sparkle" size={12}/> v{CB_VERSION} — nouveautés
              </a>
            </div>

            {/* Signature */}
            <div style={{
              textAlign: "center", fontSize: 12, color: "var(--ink-4)",
            }}>
              © 2026 ClientBase · clientbase.fr
            </div>
          </div>
        ) : (
          /* Desktop: 3-column layout */
          <>
            <div style={{
              display: "grid",
              gridTemplateColumns: "1.6fr 1fr 1fr",
              gap: 48,
            }}>
              <div>
                <Logo/>
                <p style={{ marginTop: 14, fontSize: 14, maxWidth: 300, color: "var(--ink-3)" }}>
                  L'outil tout-en-un pour les indépendants et petites entreprises de service.
                </p>
                <a href="mailto:clientbase.fr@gmail.com" style={{
                  marginTop: 12, fontSize: 13.5, color: "var(--accent-ink)", fontWeight: 520,
                  display: "inline-flex", alignItems: "center", gap: 6,
                }}>
                  <Icon name="mail" size={13}/> clientbase.fr@gmail.com
                </a>
                <div style={{ marginTop: 18, display: "flex", gap: 8 }}>
                  <span className="badge" style={{ background: "var(--sage-soft)", color: "oklch(38% 0.08 160)", borderColor: "oklch(88% 0.03 160)" }}>
                    <Icon name="shield" size={12}/> RGPD · données chez vous
                  </span>
                </div>
              </div>
              <FooterCol title="Produit" links={[
                ["Tarif", "pricing"],
                ["Guide", "features"],
                ["FAQ", "faq"],
                ["Contact", "contact"],
                ["À propos", "about"],
                ["Nouveautés", "changelog"],
              ]} go={go}/>
              <FooterCol title="Légal" links={[
                ["Confidentialité (RGPD)", "legal", "privacy"],
                ["CGU", "legal", "cgu"],
                ["Cookies", "legal", "cookies"],
                ["Mentions légales", "legal", "mentions"],
              ]} go={go}/>
            </div>
            {/* Comparer — chips premium avec icônes */}
            <div style={{
              marginTop: 40, paddingTop: 24, borderTop: "1px dashed var(--line)",
              display: "flex", alignItems: "center", gap: 12, flexWrap: "wrap",
            }}>
              <span style={{
                fontSize: 13, fontWeight: 580, color: "var(--ink-2)",
              }}>ClientBase comparé à</span>
              {[
                { href: "/comparatifs/planity",          icon: "💸", label: "Planity" },
                { href: "/comparatifs/iara",             icon: "🪙", label: "iara" },
                { href: "/comparatifs/treatwell",        icon: "🪙", label: "Treatwell" },
                { href: "/comparatifs/kiute-pro",        icon: "✂️", label: "Kiute Pro" },
                { href: "/comparatifs/fresha",           icon: "🌿", label: "Fresha" },
              ].map(c => (
                <a key={c.href} href={c.href} style={{
                  display: "inline-flex", alignItems: "center", gap: 6,
                  padding: "6px 12px",
                  background: "var(--surface)", border: "1px solid var(--line)",
                  borderRadius: 999, fontSize: 13, fontWeight: 520,
                  color: "var(--ink-2)", textDecoration: "none",
                  transition: "border-color .15s, color .15s",
                }}
                  onMouseEnter={e => { e.currentTarget.style.borderColor = "var(--accent-soft-2)"; e.currentTarget.style.color = "var(--accent-ink)"; }}
                  onMouseLeave={e => { e.currentTarget.style.borderColor = "var(--line)"; e.currentTarget.style.color = "var(--ink-2)"; }}>
                  <span>{c.icon}</span><span>vs {c.label}</span>
                </a>
              ))}
              <a href="/comparatifs" style={{
                display: "inline-flex", alignItems: "center", gap: 4,
                padding: "6px 12px",
                background: "var(--accent-soft)", border: "1px solid var(--accent-soft-2)",
                borderRadius: 999, fontSize: 13, fontWeight: 580,
                color: "var(--accent-ink)", textDecoration: "none",
              }}>tous les comparatifs →</a>
            </div>
            <div style={{
              marginTop: 24, paddingTop: 24, borderTop: "1px solid var(--line)",
              display: "flex", justifyContent: "space-between", alignItems: "center",
              fontSize: 13, color: "var(--ink-4)", flexWrap: "wrap", gap: 12,
            }}>
              <div>
                © 2026 ClientBase. Tous droits réservés. ·{" "}
                <a href="#changelog" onClick={(e) => { e.preventDefault(); go("changelog"); }}
                  style={{ color: "var(--accent-ink)", fontWeight: 520 }}>
                  v{CB_VERSION} — voir les nouveautés
                </a>
              </div>
              <div style={{ color: "var(--ink-3)", fontWeight: 500 }}>clientbase.fr</div>
            </div>
          </>
        )}
      </div>
    </footer>
  );
};

const FooterCol = ({ title, links, go }) => (
  <div>
    <div style={{
      fontSize: 12, textTransform: "uppercase", letterSpacing: "0.08em",
      color: "var(--ink-4)", fontWeight: 500, marginBottom: 14,
    }}>{title}</div>
    <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
      {links.map(([label, page, section]) => (
        <a key={label} href={`#${page}${section ? "-" + section : ""}`}
          onClick={(e) => {
            e.preventDefault();
            if (section) { try { window.__cbLegalSection = section; } catch {} }
            go(page);
          }}
          style={{ fontSize: 14, color: "var(--ink-2)", width: "fit-content" }}
          onMouseEnter={(e) => e.currentTarget.style.color = "var(--ink)"}
          onMouseLeave={(e) => e.currentTarget.style.color = "var(--ink-2)"}>
          {label}
        </a>
      ))}
    </div>
  </div>
);

/* ============ CHANGELOG PAGE ============ */
const ChangelogPage = ({ go }) => (
  <>
    <section style={{ padding: "80px 0 24px", textAlign: "center" }}>
      <div className="container">
        <div className="eyebrow">Nouveautés</div>
        <h1 style={{ marginTop: 14, fontSize: "clamp(36px, 4.2vw, 54px)" }}>
          Ce qui vient d'arriver sur <span style={{ color: "var(--accent)" }}>ClientBase</span>.
        </h1>
        <p style={{ marginTop: 14, fontSize: 16, maxWidth: 560, marginLeft: "auto", marginRight: "auto", color: "var(--ink-3)" }}>
          On avance chaque semaine. Revenez ici pour voir ce qui change et ce qui arrive bientôt.
        </p>
      </div>
    </section>
    <section style={{ padding: "16px 0 80px" }}>
      <div className="container" style={{ maxWidth: 720 }}>
        <div style={{ position: "relative" }}>
          <div aria-hidden style={{
            position: "absolute", left: 13, top: 8, bottom: 8, width: 2,
            background: "linear-gradient(180deg, var(--accent-soft-2), var(--line))",
            borderRadius: 2,
          }}/>
          {CB_CHANGELOG.map((r, i) => (
            <div key={r.version} style={{ position: "relative", paddingLeft: 42, paddingBottom: i < CB_CHANGELOG.length - 1 ? 28 : 0 }}>
              <div style={{
                position: "absolute", left: 4, top: 4,
                width: 20, height: 20, borderRadius: 50,
                background: r.upcoming ? "var(--bg)" : "var(--accent)",
                border: `2px solid ${r.upcoming ? "var(--accent-soft-2)" : "var(--accent)"}`,
                display: "flex", alignItems: "center", justifyContent: "center",
                color: "white",
              }}>
                {!r.upcoming && <Icon name="check" size={11} stroke={3}/>}
              </div>
              <div style={{
                background: "var(--surface)",
                border: "1px solid var(--line)", borderRadius: 14, padding: "18px 22px",
              }}>
                <div style={{ display: "flex", alignItems: "center", gap: 10, flexWrap: "wrap" }}>
                  <span style={{
                    fontFamily: "inherit", fontSize: 13,
                    padding: "2px 8px", background: r.upcoming ? "var(--bg-alt)" : "var(--accent-soft)",
                    color: r.upcoming ? "var(--ink-3)" : "var(--accent-ink)",
                    borderRadius: 6, fontWeight: 600,
                  }}>v{r.version}</span>
                  <span style={{ fontSize: 12.5, color: "var(--ink-4)" }}>{r.date}</span>
                  <span style={{
                    padding: "2px 8px", fontSize: 11, fontWeight: 600,
                    background: r.upcoming ? "oklch(95% 0.05 300)" : "var(--sage-soft)",
                    color:      r.upcoming ? "oklch(42% 0.18 300)" : "oklch(38% 0.08 160)",
                    borderRadius: 999,
                  }}>{r.tag}</span>
                </div>
                <h3 style={{ marginTop: 10, fontSize: 18, letterSpacing: "-0.015em" }}>{r.title}</h3>
                <ul style={{ listStyle: "none", padding: 0, margin: "12px 0 0", display: "flex", flexDirection: "column", gap: 8 }}>
                  {r.items.map(it => (
                    <li key={it} style={{ display: "flex", gap: 10, alignItems: "flex-start", fontSize: 14, color: "var(--ink-2)", lineHeight: 1.5 }}>
                      <span style={{
                        width: 18, height: 18, borderRadius: 50, flexShrink: 0, marginTop: 1,
                        background: r.upcoming ? "var(--bg-alt)" : "var(--sage-soft)",
                        color: r.upcoming ? "var(--ink-4)" : "var(--sage)",
                        display: "inline-flex", alignItems: "center", justifyContent: "center",
                      }}>
                        <Icon name={r.upcoming ? "clock" : "check"} size={10} stroke={2.6}/>
                      </span>
                      {it}
                    </li>
                  ))}
                </ul>
              </div>
            </div>
          ))}
        </div>

        <div style={{
          marginTop: 32, padding: "16px 20px",
          background: "var(--bg-alt)", border: "1px dashed var(--line-strong)",
          borderRadius: 12, fontSize: 13.5, color: "var(--ink-2)", lineHeight: 1.6,
          display: "flex", gap: 12, alignItems: "center", flexWrap: "wrap",
        }}>
          <Icon name="mail" size={16} style={{ color: "var(--accent-ink)", flexShrink: 0 }}/>
          <div style={{ flex: 1, minWidth: 220 }}>
            Une idée ? Un retour ? Écrivez-nous — chaque message est lu par l'équipe.
          </div>
          <button className="btn btn-ghost btn-sm" onClick={() => go("contact")}>
            Nous écrire
          </button>
        </div>
      </div>
    </section>
  </>
);

/* ===========================================================
   Date helpers — sémantique unifiée pour `appointments.day`.
   La valeur stockée est un OFFSET ABSOLU en jours depuis l'ancre
   (1er janvier 2026, locale). C'est stable dans le temps et
   permet aux agendas pro et aux liens publics de se comprendre.
=========================================================== */
const CB_DAY_ANCHOR = new Date(2026, 0, 1); // 1er janv. 2026, minuit local
const cbDayOffsetFrom = (date) => {
  const d = date || new Date();
  const start = new Date(d.getFullYear(), d.getMonth(), d.getDate());
  return Math.round((start.getTime() - CB_DAY_ANCHOR.getTime()) / 86400000);
};
const cbTodayOffset = () => cbDayOffsetFrom(new Date());
const cbDateForOffset = (off) => {
  const d = new Date(CB_DAY_ANCHOR);
  d.setDate(d.getDate() + off);
  return d;
};
const cbMondayOffset = (refDate) => {
  // Offset absolu du lundi de la semaine de refDate (ou aujourd'hui)
  const d = refDate ? new Date(refDate) : new Date();
  d.setHours(0, 0, 0, 0);
  const dow = d.getDay() || 7; // 1..7
  d.setDate(d.getDate() - (dow - 1));
  return cbDayOffsetFrom(d);
};
const CB_FR_DAYS_SHORT = ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"];
const CB_FR_MONTHS_SHORT = ["janv.", "févr.", "mars", "avril", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."];
const CB_FR_MONTHS_LONG = ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];
const cbYmd = (d) => `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`;

Object.assign(window, {
  Icon, Logo, Nav, Footer, useIsMobile, OpenAppButton,
  ChangelogPage, CB_VERSION,
  CB_DAY_ANCHOR, cbDayOffsetFrom, cbTodayOffset, cbDateForOffset, cbMondayOffset,
  CB_FR_DAYS_SHORT, CB_FR_MONTHS_SHORT, CB_FR_MONTHS_LONG, cbYmd,
});
