const EASE = 'cubic-bezier(0.2,0.7,0.2,1)';
const CADENCES = [{ id: 'daily', label: 'Daily' }, { id: 'mwf', label: 'Mon · Wed · Fri' }];
const cadenceLabel = (id) => (CADENCES.find(c => c.id === id) || {}).label || '—';

function Eyebrow({ children, invert = false }) {
  return <div style={{ fontFamily: 'var(--syn-font-mono)', fontSize: 11, letterSpacing: '0.16em', textTransform: 'uppercase', fontWeight: 500, color: invert ? 'var(--syn-navy-300)' : 'var(--syn-fg-subtle)' }}>{children}</div>;
}

function StepHead({ eyebrow, title, lead }) {
  const { isPhone } = window.useIsMobile();
  return (
    <div style={{ marginBottom: 34 }}>
      <Eyebrow>{eyebrow}</Eyebrow>
      <h1 style={{ fontFamily: 'var(--syn-font-display)', fontSize: isPhone ? 30 : 46, fontWeight: 600, letterSpacing: '-0.035em', lineHeight: 1.06, color: 'var(--syn-navy)', margin: '16px 0 14px', textWrap: 'balance' }}>{title}</h1>
      {lead ? <p style={{ fontFamily: 'var(--syn-font-display)', fontSize: 16, lineHeight: 1.5, color: 'var(--syn-fg-muted)', margin: 0, fontWeight: 400 }}>{lead}</p> : null}
    </div>
  );
}

function lum(hex) {
  const c = hex.replace('#', '');
  const v = [0, 2, 4].map(i => { const x = parseInt(c.substr(i, 2), 16) / 255; return x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); });
  return 0.2126 * v[0] + 0.7152 * v[1] + 0.0722 * v[2];
}

function TeamChip({ team, selected, disabled, onClick }) {
  const light = lum(team.color) > 0.42;
  const ink = light ? 'var(--syn-navy)' : '#FFFFFF';
  return (
    <button type="button" onClick={onClick} disabled={disabled} style={{
      display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', alignItems: 'flex-start',
      width: '100%', height: 84, boxSizing: 'border-box', padding: '12px 14px',
      borderRadius: 'var(--syn-radius-lg)',
      border: selected ? '2px solid #D8D8D4' : (disabled ? '2px solid var(--syn-rule)' : '2px solid transparent'),
      background: selected ? '#F2F2F0' : (disabled ? `color-mix(in srgb, ${team.color} 20%, #FBF7F1)` : team.color),
      color: selected ? '#3A3A3A' : (disabled ? 'var(--syn-fg-muted)' : ink),
      fontFamily: 'var(--syn-font-body)', textAlign: 'left',
      cursor: disabled ? 'not-allowed' : 'pointer',
      boxShadow: selected || disabled ? 'none' : 'var(--syn-shadow-sm)',
      transition: `border-color 160ms ${EASE}, box-shadow 160ms ${EASE}, background-color 200ms ${EASE}, color 200ms ${EASE}`,
    }}>
      <span style={{ fontSize: 14, lineHeight: 1.15, opacity: disabled ? 0.85 : (selected ? 0.7 : 0.8), marginBottom: 4 }}>{team.city}</span>
      <span style={{ display: 'flex', alignItems: 'center', gap: 8, fontFamily: 'var(--syn-font-display)', fontSize: 23, fontWeight: 600, letterSpacing: '-0.02em', lineHeight: 1.05 }}>{team.name}{selected ? <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="#3A3A3A" strokeWidth="3.2" strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0 }}><polyline points="20 6 9 17 4 12"></polyline></svg> : null}</span>
    </button>
  );
}

function OptionChip({ label, selected, onClick }) {
  return (
    <button type="button" onClick={onClick} style={{
      padding: '10px 16px', borderRadius: 'var(--syn-radius-pill)',
      border: `1px solid ${selected ? 'var(--syn-navy)' : 'var(--syn-rule)'}`,
      background: selected ? 'var(--syn-navy)' : 'var(--syn-bg-elev)',
      color: selected ? 'var(--syn-paper)' : 'var(--syn-fg)',
      fontFamily: 'var(--syn-font-body)', fontSize: 14.5, cursor: 'pointer',
      transition: `background-color 160ms ${EASE}, border-color 160ms ${EASE}`,
    }}>{label}</button>
  );
}

function Toggle({ on, onChange, disabled, invert = false }) {
  return (
    <button type="button" role="switch" aria-checked={on} onClick={() => !disabled && onChange(!on)} disabled={disabled} style={{
      width: 44, height: 26, borderRadius: 999, border: '1px solid ' + (invert ? '#FFFFFF' : (on ? 'var(--syn-navy)' : 'var(--syn-rule-strong)')),
      background: invert ? '#FFFFFF' : (on ? 'var(--syn-navy)' : 'var(--syn-paper-2)'), position: 'relative', padding: 0,
      cursor: disabled ? 'not-allowed' : 'pointer', opacity: disabled ? 0.4 : 1, flexShrink: 0,
      transition: `background-color 200ms ${EASE}, border-color 200ms ${EASE}`,
    }}>
      <span style={{ position: 'absolute', top: 2, left: on ? 20 : 2, width: 20, height: 20, borderRadius: 999, background: invert ? 'var(--syn-navy)' : (on ? 'var(--syn-paper)' : '#FFFFFF'), boxShadow: invert ? 'none' : 'var(--syn-shadow-sm)', transition: `left 200ms ${EASE}` }} />
    </button>
  );
}

function CadencePicker({ value, onChange, disabled }) {
  return (
    <div style={{ display: 'flex', gap: 4, padding: 3, background: 'var(--syn-paper-2)', borderRadius: 'var(--syn-radius-pill)', border: '1px solid var(--syn-rule)', opacity: disabled ? 0.4 : 1 }}>
      {CADENCES.map(c => {
        const on = value === c.id;
        return (
          <button key={c.id} type="button" disabled={disabled} onClick={() => onChange(c.id)} style={{
            padding: '7px 15px', borderRadius: 999, border: 0,
            background: on ? 'var(--syn-bg-elev)' : 'transparent',
            boxShadow: on ? 'var(--syn-shadow-sm)' : 'none',
            color: on ? 'var(--syn-navy)' : 'var(--syn-fg-subtle)',
            fontFamily: 'var(--syn-font-body)', fontSize: 13, fontWeight: on ? 600 : 400,
            cursor: disabled ? 'not-allowed' : 'pointer', whiteSpace: 'nowrap',
            transition: `background-color 160ms ${EASE}, color 160ms ${EASE}`,
          }}>{c.label}</button>
        );
      })}
    </div>
  );
}

function Panel({ children, muted = false, className, style = {} }) {
  return <div className={className} style={{ background: muted ? 'var(--syn-paper-2)' : 'var(--syn-bg-elev)', border: '1px solid var(--syn-rule)', borderRadius: 'var(--syn-radius-xl)', padding: 24, ...style }}>{children}</div>;
}

function RowLabel({ dot, title, sub, ink, subInk }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12, minWidth: 0 }}>
      {dot ? <span style={{ width: 10, height: 10, borderRadius: 999, background: dot, flexShrink: 0 }} /> : null}
      <div style={{ minWidth: 0 }}>
        <div style={{ fontFamily: 'var(--syn-font-body)', fontSize: 15, fontWeight: 600, color: ink || 'var(--syn-fg)' }}>{title}</div>
        {sub ? <div style={{ fontFamily: 'var(--syn-font-body)', fontSize: 13, color: subInk || 'var(--syn-fg-subtle)', marginTop: 3 }}>{sub}</div> : null}
      </div>
    </div>
  );
}

Object.assign(window, { lum, EASE, CADENCES, cadenceLabel, Eyebrow, StepHead, TeamChip, OptionChip, Toggle, CadencePicker, Panel, RowLabel });
