// Trainin Center — design system primitives
// Tokens, logo (uses /assets/logo-trainin-center.png in this prototype, which
// maps to /public/logo-trainin-center.png in the Next.js project), buttons,
// chips, image helpers.

const TC = {
  // brand
  sage: '#5a9169',
  sageDeep: '#3d5f47',
  sageInk:  '#2a4a35',
  sageMid:  '#7baa88',
  sageSoft: '#e8efe8',
  sageMist: '#f0f4ef',
  sageHair: 'rgba(90,145,105,0.18)',
  // neutrals
  off: '#f8f9f6',
  white: '#ffffff',
  ink:  '#1a1f1c',
  ink2: '#4a534d',
  ink3: '#7a857f',
  hair:        'rgba(26,31,28,0.08)',
  hairStrong:  'rgba(26,31,28,0.14)',
  // accent
  gold: '#c8a45c',
};

const fontSans  = `'Inter', -apple-system, BlinkMacSystemFont, 'SF Pro Text', system-ui, sans-serif`;
const fontSerif = `'Instrument Serif', 'Cormorant Garamond', Georgia, serif`;
const fontMono  = `'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, monospace`;

// ── Asset paths ─────────────────────────────────────────────
// In the artboard prototype these resolve to /assets/*. In the Next.js
// project they should be swapped for /public/* equivalents.
const ASSET = {
  hero: 'assets/hero-trainin-center.png',           // → /public/hero-trainin-center.png
  logo: 'assets/logo-trainin-center-trimmed.png',   // cropped — no surrounding white padding
  logoCompact: 'assets/logo-trainin-center-trimmed.png',
  naturopathie: 'assets/formation-naturopathie.jpg',
  iridologie: 'assets/module-iridologie.jpg',
  frequences: 'assets/formation-frequences.jpg',
  modules: 'assets/module-a-la-carte.png',
  whyTc: 'assets/why-tc.jpg',
  logoFull: 'assets/logo-trainin-center-full.png',  // full lockup, transparent bg
};

// ── Logo ────────────────────────────────────────────────────
// `compact` = mark only (no "Trainin Center" wordmark — used in tight nav).
// `tagline` = include the "Formations bien-être en ligne" line under.
function TCLogo({ height = 36, compact = false, color }) {
  // Real logo file is the full lockup. For compact, crop visually by
  // setting an explicit width and object-position so only the mark shows.
  if (compact) {
    return (
      <div style={{
        height, width: height,
        backgroundImage: `url(${ASSET.logo})`,
        backgroundSize: 'auto 100%',
        backgroundRepeat: 'no-repeat',
        backgroundPosition: 'left center',
        filter: color ? 'brightness(0) invert(1)' : 'none',
      }} />
    );
  }
  return (
    <img
      src={ASSET.logo}
      alt="Trainin Center"
      style={{
        height,
        width: 'auto',
        display: 'block',
        filter: color === 'white' ? 'brightness(0) invert(1)' : 'none',
      }}
    />
  );
}

// ── Eyebrow chip ────────────────────────────────────────────
function Eyebrow({ children, variant = 'sage', size = 'md' }) {
  const themes = {
    sage:   { bg: TC.sageSoft, fg: TC.sageDeep, dot: TC.sage },
    light:  { bg: 'rgba(255,255,255,0.12)', fg: TC.white,    dot: TC.white },
    outline:{ bg: 'transparent',           fg: TC.sageDeep, dot: TC.sage, border: TC.sageHair },
    gold:   { bg: 'rgba(200,164,92,0.12)', fg: '#8a6a2a',   dot: TC.gold },
  };
  const t = themes[variant];
  const h = size === 'sm' ? 26 : 30;
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      height: h, padding: '0 12px',
      background: t.bg, color: t.fg,
      border: t.border ? `1px solid ${t.border}` : 'none',
      borderRadius: 999,
      fontFamily: fontSans, fontSize: 12.5, fontWeight: 500, letterSpacing: 0.2,
      whiteSpace: 'nowrap',
    }}>
      <span style={{ width: 6, height: 6, borderRadius: 3, background: t.dot, display: 'inline-block' }} />
      {children}
    </div>
  );
}

// ── Responsive device hook (real breakpoint, no design-canvas) ──
// < 768px → 'mobile', otherwise 'desktop'. Every component already
// branches its layout on this string, so swapping the two static
// design-review artboards for one live-resize hook makes the exact
// same markup genuinely responsive.
function useDevice() {
  const get = () => (typeof window !== 'undefined' && window.innerWidth < 768 ? 'mobile' : 'desktop');
  const [device, setDevice] = React.useState(get);
  React.useEffect(() => {
    const onResize = () => setDevice(get());
    window.addEventListener('resize', onResize);
    return () => window.removeEventListener('resize', onResize);
  }, []);
  return device;
}

// ── Buttons ─────────────────────────────────────────────────
function BtnPrimary({ children, size = 'md', dark = false, fullWidth = false, icon = true, style, ...rest }) {
  const h = size === 'lg' ? 54 : size === 'sm' ? 42 : 48;
  const px = size === 'lg' ? 26 : size === 'sm' ? 18 : 22;
  const fs = size === 'lg' ? 15.5 : size === 'sm' ? 14 : 15;
  return (
    <button {...rest} style={{
      height: h, padding: `0 ${px}px`,
      background: dark ? TC.white : TC.sage,
      color: dark ? TC.ink : TC.white,
      border: 'none', borderRadius: 999,
      fontFamily: fontSans, fontSize: fs, fontWeight: 600, letterSpacing: -0.15,
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 9,
      cursor: 'pointer', width: fullWidth ? '100%' : 'auto',
      boxShadow: dark ? '0 1px 2px rgba(0,0,0,0.08)' :
        '0 1px 2px rgba(0,0,0,0.06), 0 6px 18px rgba(90,145,105,0.22)',
      ...style,
    }}>
      {children}
      {icon && (
        <svg width="14" height="14" viewBox="0 0 14 14" fill="none" style={{ flexShrink: 0 }}>
          <path d="M3 7h8M8 4l3 3-3 3" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      )}
    </button>
  );
}

function BtnGhost({ children, size = 'md', dark = false, fullWidth = false, style, ...rest }) {
  const h = size === 'lg' ? 54 : size === 'sm' ? 42 : 48;
  const px = size === 'lg' ? 26 : size === 'sm' ? 18 : 22;
  const fs = size === 'lg' ? 15.5 : size === 'sm' ? 14 : 15;
  return (
    <button {...rest} style={{
      height: h, padding: `0 ${px}px`,
      background: 'transparent',
      color: dark ? TC.white : TC.ink,
      border: `1px solid ${dark ? 'rgba(255,255,255,0.28)' : TC.hairStrong}`,
      borderRadius: 999,
      fontFamily: fontSans, fontSize: fs, fontWeight: 500, letterSpacing: -0.15,
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      cursor: 'pointer', width: fullWidth ? '100%' : 'auto',
      ...style,
    }}>
      {children}
    </button>
  );
}

// ── Small thin-line icons (only geometric primitives) ───────
function IconDot({ size = 22, color = TC.sage, ring = TC.sageSoft }) {
  return (
    <div style={{
      width: size, height: size, borderRadius: size / 2,
      background: ring, display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
    }}>
      <div style={{ width: size * 0.36, height: size * 0.36, borderRadius: '50%', background: color }} />
    </div>
  );
}

function IconCheck({ size = 18, color = TC.sage }) {
  return (
    <svg width={size} height={size} viewBox="0 0 18 18" fill="none">
      <circle cx="9" cy="9" r="8" stroke={color} strokeWidth="1.4" />
      <path d="M5.5 9.4l2.4 2.3 4.6-5" stroke={color} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

function StarRow({ color = TC.gold, size = 13 }) {
  return (
    <div style={{ display: 'inline-flex', gap: 2 }}>
      {[0,1,2,3,4].map((i) => (
        <svg key={i} width={size} height={size} viewBox="0 0 13 13" fill={color}>
          <path d="M6.5 1l1.65 3.35 3.7.54-2.68 2.6.63 3.68L6.5 9.43 3.2 11.17l.63-3.68L1.15 4.89l3.7-.54L6.5 1z"/>
        </svg>
      ))}
    </div>
  );
}

// Tiny abstract icon for benefit/formation cards. Themes are sage-toned
// geometric compositions — circles, leaves, abstract shapes. No real
// illustration.
function ThemeMark({ theme = 'leaf', size = 56, color = TC.sage, bg = TC.sageSoft }) {
  const shapes = {
    // online: monitor frame
    online: (
      <g>
        <rect x="10" y="14" width="36" height="24" rx="3" stroke={color} strokeWidth="1.6" fill="none"/>
        <path d="M22 42h12M28 38v4" stroke={color} strokeWidth="1.6" strokeLinecap="round"/>
        <circle cx="28" cy="26" r="4" fill={color} opacity="0.55"/>
      </g>
    ),
    // holistic: three overlapping circles (trinity)
    holistic: (
      <g>
        <circle cx="22" cy="22" r="10" stroke={color} strokeWidth="1.6" fill="none"/>
        <circle cx="34" cy="22" r="10" stroke={color} strokeWidth="1.6" fill="none"/>
        <circle cx="28" cy="34" r="10" stroke={color} strokeWidth="1.6" fill="none"/>
      </g>
    ),
    // accompagnement: two circles linked (hands)
    accompagnement: (
      <g>
        <circle cx="20" cy="28" r="7" stroke={color} strokeWidth="1.6" fill="none"/>
        <circle cx="36" cy="28" r="7" stroke={color} strokeWidth="1.6" fill="none"/>
        <path d="M28 28h0" stroke={color} strokeWidth="2" strokeLinecap="round"/>
      </g>
    ),
    // reconversion: arrow loop
    reconversion: (
      <g>
        <path d="M14 30c0-7 6-12 14-12 6 0 11 3 13 8" stroke={color} strokeWidth="1.6" strokeLinecap="round" fill="none"/>
        <path d="M37 22l4 4h-6" stroke={color} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
        <path d="M42 32c0 7-6 12-14 12-6 0-11-3-13-8" stroke={color} strokeWidth="1.6" strokeLinecap="round" fill="none"/>
        <path d="M19 40l-4-4h6" stroke={color} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
      </g>
    ),
    // leaf
    leaf: (
      <g>
        <path d="M16 40c0-12 8-22 22-24-2 14-10 22-22 24z" stroke={color} strokeWidth="1.6" fill="none"/>
        <path d="M16 40c8-7 14-13 22-24" stroke={color} strokeWidth="1.4" />
      </g>
    ),
    // drop (sophrologie - breath)
    drop: (
      <g>
        <path d="M28 14c-6 8-10 13-10 19a10 10 0 0020 0c0-6-4-11-10-19z" stroke={color} strokeWidth="1.6" fill="none"/>
      </g>
    ),
    // bowl (nutrition)
    bowl: (
      <g>
        <path d="M12 26h32a16 16 0 01-32 0z" stroke={color} strokeWidth="1.6" fill="none"/>
        <path d="M10 26h36" stroke={color} strokeWidth="1.6" strokeLinecap="round"/>
        <circle cx="22" cy="22" r="2" fill={color}/>
        <circle cx="30" cy="20" r="2" fill={color}/>
        <circle cx="36" cy="22" r="1.6" fill={color}/>
      </g>
    ),
    // wave (frequences)
    wave: (
      <g>
        <path d="M10 28c4-6 8-6 12 0s8 6 12 0 8-6 12 0" stroke={color} strokeWidth="1.6" fill="none" strokeLinecap="round"/>
        <circle cx="28" cy="28" r="14" stroke={color} strokeWidth="1.2" fill="none" opacity="0.4"/>
      </g>
    ),
    // book (modules)
    book: (
      <g>
        <path d="M12 16h14a4 4 0 014 4v22a4 4 0 00-4-4H12V16z" stroke={color} strokeWidth="1.6" fill="none"/>
        <path d="M44 16H30a4 4 0 00-4 4v22a4 4 0 014-4h14V16z" stroke={color} strokeWidth="1.6" fill="none"/>
      </g>
    ),
    // iris (iridologie) — concentric circles / eye
    iris: (
      <g>
        <circle cx="28" cy="28" r="15" stroke={color} strokeWidth="1.6" fill="none"/>
        <circle cx="28" cy="28" r="7" stroke={color} strokeWidth="1.6" fill="none"/>
        <circle cx="28" cy="28" r="2.4" fill={color}/>
      </g>
    ),
  };
  return (
    <div style={{
      width: size, height: size, borderRadius: size / 2,
      background: bg, display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      flexShrink: 0,
    }}>
      <svg width={size * 0.74} height={size * 0.74} viewBox="0 0 56 56">{shapes[theme] || shapes.leaf}</svg>
    </div>
  );
}

// ── WhatsApp floating button ────────────────────────────────
// Floating CTA — sits bottom-right of the viewport at all times,
// including while scrolling (position: fixed).
function WhatsAppFAB({ size = 56, absolute = false, bottom = 24, right = 24 }) {
  return (
    <div style={{
      position: absolute ? 'absolute' : 'fixed',
      bottom, right,
      display: 'flex', alignItems: 'center', gap: 10,
      zIndex: 60,
    }}>
      <div style={{
        background: TC.white, color: TC.ink,
        fontFamily: fontSans, fontSize: 13, fontWeight: 500,
        padding: '10px 14px', borderRadius: 999,
        border: `1px solid ${TC.hair}`,
        boxShadow: '0 1px 2px rgba(0,0,0,0.04), 0 14px 30px rgba(26,31,28,0.08)',
        letterSpacing: -0.05,
      }}>
        Une question ? <span style={{ color: TC.sageDeep, fontWeight: 600 }}>Écrivez-nous</span>
      </div>
      <a href="https://wa.me/33644643537" target="_blank" rel="noopener noreferrer" aria-label="WhatsApp" data-cta="whatsapp:direct" style={{
        width: size, height: size, borderRadius: size / 2,
        background: TC.sage, border: 'none',
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        cursor: 'pointer', textDecoration: 'none',
        boxShadow: '0 6px 18px rgba(90,145,105,0.35), 0 2px 6px rgba(0,0,0,0.08)',
      }}>
        <svg width={size * 0.5} height={size * 0.5} viewBox="0 0 24 24" fill={TC.white}>
          <path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2 22l5.25-1.38a9.9 9.9 0 004.79 1.22h.01c5.46 0 9.91-4.45 9.91-9.91A9.83 9.83 0 0019.07 4.9 9.83 9.83 0 0012.04 2zm0 18.13a8.2 8.2 0 01-4.17-1.14l-.3-.18-3.1.81.83-3.02-.2-.31a8.18 8.18 0 01-1.26-4.38c0-4.52 3.69-8.2 8.21-8.2 2.19 0 4.25.85 5.8 2.4a8.16 8.16 0 012.4 5.8c0 4.52-3.68 8.22-8.21 8.22zm4.5-6.15c-.25-.12-1.46-.72-1.69-.8-.23-.08-.39-.12-.56.12-.16.25-.64.8-.79.97-.14.16-.29.18-.54.06-.25-.12-1.04-.38-1.98-1.22-.73-.65-1.23-1.46-1.37-1.71-.14-.25-.01-.38.11-.5.11-.11.25-.29.37-.43.12-.14.16-.25.25-.41.08-.16.04-.31-.02-.43-.06-.12-.56-1.35-.77-1.85-.2-.49-.41-.42-.56-.43-.14-.01-.31-.01-.48-.01-.16 0-.43.06-.66.31-.23.25-.86.85-.86 2.07 0 1.22.89 2.4 1.01 2.56.12.16 1.74 2.66 4.22 3.74.59.25 1.05.41 1.41.52.59.19 1.13.16 1.56.1.47-.07 1.46-.6 1.66-1.17.21-.58.21-1.07.14-1.18-.06-.1-.22-.16-.47-.28z"/>
        </svg>
      </a>
    </div>
  );
}
// Real photo (the hero asset) with object-position to crop into different
// framings. For sections where we want richer imagery without more assets,
// this lets one source carry the page tastefully.
function LifestyleImg({ pos = 'center', radius = 18, height = '100%', filter = 'none', children, style = {} }) {
  return (
    <div style={{
      position: 'relative',
      width: '100%', height,
      borderRadius: radius,
      overflow: 'hidden',
      backgroundColor: TC.sageMist,
      ...style,
    }}>
      <img
        src={ASSET.hero}
        alt=""
        style={{
          width: '100%', height: '100%', objectFit: 'cover',
          objectPosition: pos, display: 'block', filter,
        }}
      />
      {children}
    </div>
  );
}

// Styled placeholder tile — warm sage gradient, monospace caption.
// Used for formation cards / supporting imagery where we don't yet have
// a real photo. Looks intentional, not broken.
function PhotoPlaceholder({ caption, tone = 'sage', radius = 18, height = '100%', tall = false }) {
  const tones = {
    sage:  { bg: `linear-gradient(135deg, ${TC.sageMist} 0%, ${TC.sageSoft} 100%)`, fg: TC.sageDeep, ring: 'rgba(90,145,105,0.18)' },
    warm:  { bg: `linear-gradient(135deg, #f5ede0 0%, #ead9c2 100%)`, fg: '#8a6a2a', ring: 'rgba(200,164,92,0.22)' },
    ink:   { bg: `linear-gradient(135deg, #2a3530 0%, #1a221e 100%)`, fg: 'rgba(255,255,255,0.85)', ring: 'rgba(255,255,255,0.12)' },
    cream: { bg: `linear-gradient(135deg, #fbf9f3 0%, #efeadd 100%)`, fg: TC.sageDeep, ring: 'rgba(0,0,0,0.06)' },
  };
  const t = tones[tone];
  return (
    <div style={{
      position: 'relative',
      width: '100%', height,
      borderRadius: radius,
      background: t.bg,
      overflow: 'hidden',
      boxShadow: `inset 0 0 0 1px ${t.ring}`,
    }}>
      {/* subtle diagonal grain */}
      <div style={{
        position: 'absolute', inset: 0,
        background: `repeating-linear-gradient(135deg, transparent 0 18px, rgba(0,0,0,0.025) 18px 19px)`,
        pointerEvents: 'none',
      }} />
      <div style={{
        position: 'absolute', inset: 0,
        display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 16,
      }}>
        <div style={{
          padding: '7px 12px',
          background: tone === 'ink' ? 'rgba(255,255,255,0.08)' : 'rgba(255,255,255,0.82)',
          backdropFilter: 'blur(6px)',
          borderRadius: 999,
          fontFamily: fontMono, fontSize: 10.5, color: t.fg,
          letterSpacing: 0.3,
          border: `1px solid ${t.ring}`,
          textAlign: 'center',
        }}>
          {caption}
        </div>
      </div>
    </div>
  );
}

// ── Desktop container ──────────────────────────────────────
// Single source of truth for the site's desktop content width. max-width
// caps the *whole* box (gutter included) and centers it, so every section
// — header, hero, cards, CTAs, footer — resolves to the same visible left
// and right edge on wide screens. Never binds below 1280px, so this is
// safe to apply unconditionally without touching mobile layout.
const TC_CONTAINER = 1280;
function containerBox(padX = 24) {
  return { maxWidth: TC_CONTAINER, margin: '0 auto', width: '100%', paddingLeft: padX, paddingRight: padX };
}

// ── Section container ──────────────────────────────────────
// Common vertical rhythm + optional eyebrow + section title.
function Section({ tone = 'white', children, padY, padX, style = {}, id }) {
  const bg = {
    white: TC.white,
    off:   TC.off,
    sage:  TC.sageDeep,
    ink:   '#1c2520',
    cream: '#faf7f1',
  }[tone];
  return (
    <section id={id} style={{
      background: bg,
      padding: `${padY?.[0] ?? 80}px 0 ${padY?.[1] ?? 80}px`,
      position: 'relative',
      ...style,
    }}>
      <div style={containerBox(padX ?? 24)}>{children}</div>
    </section>
  );
}

function SectionHead({ eyebrow, title, sub, align = 'left', dark = false, maxWidth = 720 }) {
  const fg = dark ? TC.white : TC.ink;
  const fgSub = dark ? 'rgba(255,255,255,0.72)' : TC.ink2;
  return (
    <div style={{ textAlign: align, marginBottom: 36, maxWidth, marginLeft: align === 'center' ? 'auto' : 0, marginRight: align === 'center' ? 'auto' : 0 }}>
      {eyebrow && (
        <div style={{ display: align === 'center' ? 'flex' : 'block', justifyContent: 'center', marginBottom: 18 }}>
          <Eyebrow variant={dark ? 'light' : 'sage'}>{eyebrow}</Eyebrow>
        </div>
      )}
      {title && (
        <h2 style={{
          fontFamily: fontSans, fontSize: 'clamp(28px, 5vw, 44px)',
          fontWeight: 500, letterSpacing: -1.2, lineHeight: 1.08,
          color: fg, margin: 0, textWrap: 'balance',
        }}>{title}</h2>
      )}
      {sub && (
        <p style={{
          fontFamily: fontSans, fontSize: 17, lineHeight: 1.55,
          color: fgSub, margin: '14px 0 0', letterSpacing: -0.1,
        }}>{sub}</p>
      )}
    </div>
  );
}

Object.assign(window, {
  TC, fontSans, fontSerif, fontMono, ASSET,
  TCLogo, Eyebrow, BtnPrimary, BtnGhost,
  IconDot, IconCheck, StarRow, ThemeMark,
  LifestyleImg, PhotoPlaceholder, WhatsAppFAB,
  Section, SectionHead, useDevice,
  TC_CONTAINER, containerBox,
});
