// Brutalist · Component Library
// --------------------------------------------------------------------
// Composable primitives for scenes & pages. Consumes CSS variables
// declared in brutalist/tokens.css — change tokens there, not here.
//
// Catalogue:
//   Layout        BRoot · BSection · BSectionHeader · BHeader · BFooter
//   Type          BDisplay · BHeading · BCaption · BTag · BItalic
//   Controls      BButton · BLink
//   Modules       BStat · BImagePlate · BQuote · BManifestoRow ·
//                 BMarqueeRow · BHazardBand · BPlacard · BBadge ·
//                 BListRow · BTicker · BRegMarks
//
// Conventions:
// • Components ship with sensible defaults; override via `style` /
//   inline props.
// • Sizes are token names: 'xxl' | 'xl' | 'l' | 'm' | 's'.
// • Tone props use 'fg' | 'accent' | 'invert' | 'panel'.
// --------------------------------------------------------------------

const _displaySize = {
  xxl: 'var(--b-t-display-xxl)',
  xl:  'var(--b-t-display-xl)',
  l:   'var(--b-t-display-l)',
  m:   'var(--b-t-display-m)',
  s:   'var(--b-t-display-s)',
};
const _headingSize = {
  l: 'var(--b-t-heading-l)',
  m: 'var(--b-t-heading-m)',
  s: 'var(--b-t-heading-s)',
};

// ─── BRoot ──────────────────────────────────────────────────────────
// Wrap every page in this. Sets bg/fg/font defaults and box-sizing.
function BRoot({ children, style }) {
  return (
    <div className="b-root" style={{ width: '100%', height: '100%', position: 'relative', overflow: 'hidden', ...style }}>
      {children}
    </div>
  );
}

// ─── BSection — full-width band with hairline above/below ─────────
function BSection({ children, divide = true, pad = '40px 56px', style }) {
  return (
    <section style={{
      padding: pad,
      borderBottom: divide ? 'var(--b-hair)' : 'none',
      ...style,
    }}>{children}</section>
  );
}

// ─── BSectionHeader — numbered title row (used in long pages) ─────
function BSectionHeader({ n, title, kicker, style }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: '90px 1fr auto',
      alignItems: 'baseline', gap: 24,
      padding: '20px 56px', borderTop: 'var(--b-hair)', borderBottom: 'var(--b-hair)',
      ...style,
    }}>
      <span className="b-mono" style={{ color: 'var(--b-accent)' }}>§ {n}</span>
      <span className="b-display" style={{ fontSize: 'var(--b-t-heading-l)', letterSpacing: '-0.02em' }}>{title}</span>
      {kicker && <span className="b-mono">{kicker}</span>}
    </div>
  );
}

// ─── BHeader — page nav bar ────────────────────────────────────────
function BHeader({ brand = 'RUBENS', tagline = 'Strength Coach', nav = ['Training', 'Athletes', 'Programs', 'Journal'], cta = 'Book a session →' }) {
  return (
    <header style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '24px 56px', borderBottom: 'var(--b-hair)',
      fontSize: 'var(--b-t-caption)', letterSpacing: '0.18em', textTransform: 'uppercase',
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
        <span style={{ width: 10, height: 10, background: 'var(--b-accent)', borderRadius: '50%' }} />
        <span style={{ fontWeight: 600 }}>{brand} / {tagline}</span>
      </div>
      <nav style={{ display: 'flex', gap: 36, color: 'var(--b-dim)' }}>
        {nav.map((n) => <a key={n}>{n}</a>)}
      </nav>
      {cta && <a style={{ color: 'var(--b-fg)', borderBottom: '1px solid var(--b-accent)', paddingBottom: 2 }}>{cta}</a>}
    </header>
  );
}

// ─── BFooter — bottom ticker strip ─────────────────────────────────
function BFooter({ left, middle, right }) {
  return (
    <footer style={{
      borderTop: 'var(--b-hair)', padding: '14px 56px',
      fontFamily: 'var(--b-f-mono)', fontSize: 'var(--b-t-caption)',
      color: 'var(--b-dim)', letterSpacing: '0.18em', textTransform: 'uppercase',
      display: 'flex', justifyContent: 'space-between', gap: 24,
    }}>
      <span>{left}</span><span>{middle}</span><span>{right}</span>
    </footer>
  );
}

// ─── Type ─────────────────────────────────────────────────────────
function BDisplay({ size = 'l', tone = 'fg', italic, as: Tag = 'h1', children, style }) {
  const color = tone === 'accent' ? 'var(--b-accent)' : tone === 'dim' ? 'var(--b-dim)' : 'inherit';
  return (
    <Tag style={{
      fontFamily: italic ? 'var(--b-f-italic)' : 'var(--b-f-display)',
      fontStyle: italic ? 'italic' : 'normal',
      fontSize: _displaySize[size] || _displaySize.l,
      lineHeight: 0.86, letterSpacing: italic ? '-0.02em' : '-0.045em',
      textTransform: italic ? 'none' : 'uppercase',
      color, margin: 0, ...style,
    }}>{children}</Tag>
  );
}
function BHeading({ size = 'm', children, style }) {
  return (
    <div style={{
      fontFamily: 'var(--b-f-display)', fontSize: _headingSize[size] || _headingSize.m,
      letterSpacing: '-0.02em', lineHeight: 1.05, textTransform: 'uppercase', ...style,
    }}>{children}</div>
  );
}
function BCaption({ tone = 'dim', children, style }) {
  return (
    <div style={{
      fontFamily: 'var(--b-f-mono)', fontSize: 'var(--b-t-caption)',
      letterSpacing: '0.18em', textTransform: 'uppercase',
      color: tone === 'accent' ? 'var(--b-accent)' : tone === 'fg' ? 'var(--b-fg)' : 'var(--b-dim)',
      ...style,
    }}>{children}</div>
  );
}
function BTag({ tone = 'accent', children, style }) {
  const isAccent = tone === 'accent';
  return (
    <span style={{
      display: 'inline-block', padding: '4px 8px',
      background: isAccent ? 'var(--b-accent)' : 'var(--b-fg)',
      color: isAccent ? 'var(--b-bg)' : 'var(--b-bg)',
      fontFamily: 'var(--b-f-mono)', fontSize: 'var(--b-t-tag)',
      letterSpacing: '0.18em', textTransform: 'uppercase', fontWeight: 500,
      ...style,
    }}>{children}</span>
  );
}
function BItalic({ size = 24, children, style }) {
  return (
    <span style={{
      fontFamily: 'var(--b-f-italic)', fontStyle: 'italic',
      fontSize: size, lineHeight: 1.25, letterSpacing: '-0.01em', ...style,
    }}>{children}</span>
  );
}

// ─── Controls ─────────────────────────────────────────────────────
function BButton({ variant = 'primary', children, style, as: Tag = 'a' }) {
  const v = {
    primary: { background: 'var(--b-accent)', color: 'var(--b-bg)', border: 0 },
    outline: { background: 'transparent', color: 'var(--b-fg)', border: '1px solid var(--b-rule)' },
    invert:  { background: 'var(--b-fg)', color: 'var(--b-bg)', border: 0 },
  }[variant];
  return (
    <Tag style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      padding: '16px 24px',
      fontFamily: 'var(--b-f-display)',
      fontSize: 13, letterSpacing: '0.14em', textTransform: 'uppercase',
      ...v, ...style,
    }}>{children}</Tag>
  );
}
function BLink({ children, accent, style }) {
  return (
    <a style={{
      color: 'var(--b-fg)', borderBottom: `1px solid ${accent ? 'var(--b-accent)' : 'var(--b-fg)'}`,
      paddingBottom: 2, fontSize: 13, letterSpacing: '0.06em', ...style,
    }}>{children}</a>
  );
}

// ─── Modules ──────────────────────────────────────────────────────
function BStat({ number, label, size = 'l', style }) {
  const fs = size === 'xl' ? 56 : size === 'l' ? 36 : 28;
  return (
    <div style={{ ...style }}>
      <div style={{ fontFamily: 'var(--b-f-display)', fontSize: fs, lineHeight: 1, letterSpacing: '-0.02em' }}>{number}</div>
      <div style={{
        fontFamily: 'var(--b-f-mono)', fontSize: 'var(--b-t-tag)', color: 'var(--b-dim)',
        letterSpacing: '0.18em', textTransform: 'uppercase', marginTop: 6,
      }}>{label}</div>
    </div>
  );
}

// Stat strip — N tiles divided by hairlines. Pass [{number, label}, ...]
function BStatStrip({ stats, style }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: `repeat(${stats.length}, 1fr)`,
      border: 'var(--b-hair)', ...style,
    }}>
      {stats.map((s, i) => (
        <div key={i} style={{
          padding: '20px 24px',
          borderRight: i < stats.length - 1 ? 'var(--b-hair)' : 'none',
        }}>
          <BStat number={s.number} label={s.label} size={s.size || 'l'} />
        </div>
      ))}
    </div>
  );
}

// Image placeholder with diagonal stripe fill + corner regmarks
function BImagePlate({ caption = 'PHOTO PLATE · 4:5', index, ratio, style }) {
  return (
    <div style={{
      position: 'relative', background: 'var(--b-panel)',
      backgroundImage: 'var(--b-stripe-panel)',
      display: 'flex', alignItems: 'flex-end', padding: 20,
      aspectRatio: ratio, ...style,
    }}>
      {index && (
        <div style={{
          position: 'absolute', top: 20, right: 20,
          fontFamily: 'var(--b-f-mono)', fontSize: 'var(--b-t-tag)',
          color: 'var(--b-dim)', letterSpacing: '0.18em',
        }}>{index}</div>
      )}
      <div style={{
        fontFamily: 'var(--b-f-mono)', fontSize: 'var(--b-t-tag)',
        color: 'var(--b-dim)', letterSpacing: '0.18em', textTransform: 'uppercase',
      }}>[ {caption} ]</div>
    </div>
  );
}

function BQuote({ children, attribution, style }) {
  return (
    <blockquote style={{ margin: 0, ...style }}>
      <BItalic size={28}>“{children}”</BItalic>
      {attribution && (
        <div style={{
          fontFamily: 'var(--b-f-mono)', fontSize: 'var(--b-t-tag)',
          color: 'var(--b-dim)', letterSpacing: '0.18em', textTransform: 'uppercase',
          marginTop: 12,
        }}>— {attribution}</div>
      )}
    </blockquote>
  );
}

function BManifestoRow({ n, rule, note, divide = true }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: '70px 1fr auto', gap: 16,
      alignItems: 'center', padding: '14px 0',
      borderBottom: divide ? 'var(--b-hair)' : 'none',
    }}>
      <span style={{
        fontFamily: 'var(--b-f-mono)', fontSize: 14, color: 'var(--b-accent)',
        letterSpacing: '0.12em',
      }}>{String(n).padStart(2, '0')}.</span>
      <BHeading size="m">{rule}</BHeading>
      {note && <BCaption>{note}</BCaption>}
    </div>
  );
}

function BMarqueeRow({ accent, italic, big, children }) {
  return (
    <div style={{
      borderTop: 'var(--b-hair)', padding: '6px 56px',
      fontFamily: italic ? 'var(--b-f-italic)' : 'var(--b-f-display)',
      fontStyle: italic ? 'italic' : 'normal',
      fontSize: big ? 144 : 96, lineHeight: 0.92, letterSpacing: '-0.04em',
      color: accent ? 'var(--b-accent)' : 'var(--b-fg)',
      textTransform: italic ? 'none' : 'uppercase',
    }}>{children}</div>
  );
}

function BHazardBand({ height = 32, style }) {
  return <div style={{ height, background: 'var(--b-hazard)', ...style }} />;
}

// Placard — small flat card with number, headline, tone variant
function BPlacard({ n, label = 'Placard', headline, tone = 'panel' }) {
  const t = {
    panel:  { bg: 'var(--b-panel)',       fg: 'var(--b-fg)',  border: 'var(--b-hair)', dim: 'var(--b-dim)' },
    invert: { bg: 'var(--b-fg)',          fg: 'var(--b-bg)',  border: 'none',          dim: 'var(--b-bg)' },
    accent: { bg: 'var(--b-accent)',      fg: 'var(--b-bg)',  border: 'none',          dim: 'var(--b-bg)' },
  }[tone];
  return (
    <div style={{
      background: t.bg, color: t.fg, border: t.border,
      padding: '18px 22px',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 16,
    }}>
      <div>
        <div style={{
          fontFamily: 'var(--b-f-mono)', fontSize: 'var(--b-t-tag)',
          letterSpacing: '0.22em', textTransform: 'uppercase',
          color: t.dim, opacity: tone === 'panel' ? 1 : 0.7,
        }}>{label}</div>
        <div style={{
          fontFamily: 'var(--b-f-display)', fontSize: 28, marginTop: 4,
          letterSpacing: '-0.01em', textTransform: 'uppercase',
        }}>{headline}</div>
      </div>
      <div style={{ fontFamily: 'var(--b-f-display)', fontSize: 22, opacity: tone === 'panel' ? 0.5 : 0.9 }}>{n}</div>
    </div>
  );
}

// Badge — corner stamp / sticker
function BBadge({ children, rotate = 4, tone = 'accent', style }) {
  const t = tone === 'accent'
    ? { background: 'var(--b-accent)', color: 'var(--b-bg)' }
    : { background: 'var(--b-fg)', color: 'var(--b-bg)' };
  return (
    <span style={{
      display: 'inline-block', padding: '6px 12px',
      fontFamily: 'var(--b-f-display)', fontSize: 12, letterSpacing: '0.16em',
      textTransform: 'uppercase', transform: `rotate(${rotate}deg)`,
      whiteSpace: 'nowrap', ...t, ...style,
    }}>{children}</span>
  );
}

// List row — start-list / roster pattern. Cols by ratio array.
function BListRow({ cells, accent, divide = true, cols = ['90px', '1.4fr', '1fr', '0.8fr'] }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: cols.join(' '),
      padding: '14px 0', alignItems: 'center', gap: 16,
      borderBottom: divide ? 'var(--b-hair)' : 'none',
      background: accent ? 'rgba(255,59,31,0.07)' : 'transparent',
    }}>
      {cells.map((cell, i) => {
        if (i === 0) return <span key={i} style={{ fontFamily: 'var(--b-f-display)', fontSize: 26, color: accent ? 'var(--b-accent)' : 'var(--b-fg)', letterSpacing: '-0.02em' }}>{cell}</span>;
        if (i === 1) return <span key={i} style={{ fontFamily: 'var(--b-f-display)', fontSize: 15, letterSpacing: '0.02em' }}>{cell}</span>;
        return <span key={i} style={{ fontFamily: 'var(--b-f-mono)', fontSize: 11, color: i === cells.length - 1 ? 'var(--b-accent)' : 'var(--b-dim)', letterSpacing: '0.14em' }}>{cell}</span>;
      })}
    </div>
  );
}

// Ticker — three-up bottom strip (alias for BFooter, semantically different)
function BTicker(props) { return <BFooter {...props} />; }

// Registration marks — corner brackets that frame a hero/poster
function BRegMarks({ inset = 24, color = 'var(--b-accent)' }) {
  const arm = 18;
  const w = '2px';
  const s = (extra) => ({
    position: 'absolute', width: arm, height: arm, ...extra,
  });
  return (
    <>
      <div style={{ ...s({ top: inset, left: inset, borderTop: `${w} solid ${color}`, borderLeft: `${w} solid ${color}` }) }} />
      <div style={{ ...s({ top: inset, right: inset, borderTop: `${w} solid ${color}`, borderRight: `${w} solid ${color}` }) }} />
      <div style={{ ...s({ bottom: inset, left: inset, borderBottom: `${w} solid ${color}`, borderLeft: `${w} solid ${color}` }) }} />
      <div style={{ ...s({ bottom: inset, right: inset, borderBottom: `${w} solid ${color}`, borderRight: `${w} solid ${color}` }) }} />
    </>
  );
}

Object.assign(window, {
  BRoot, BSection, BSectionHeader, BHeader, BFooter, BTicker,
  BDisplay, BHeading, BCaption, BTag, BItalic,
  BButton, BLink,
  BStat, BStatStrip, BImagePlate, BQuote, BManifestoRow, BMarqueeRow,
  BHazardBand, BPlacard, BBadge, BListRow, BRegMarks,
});
