// ═══════════════════════════════════════════════════════════════
// UI primitives · Escalab Gestão de Pessoas Design System
// ═══════════════════════════════════════════════════════════════

const Icon = ({ name, size = 18 }) => {
  const paths = {
    home:       <><path d="M3 9.5L12 3l9 6.5V20a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V9.5z"/><polyline points="9 21 9 12 15 12 15 21"/></>,
    users:      <><circle cx="9" cy="7" r="4"/><path d="M3 21v-2a4 4 0 0 1 4-4h4a4 4 0 0 1 4 4v2"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/><path d="M21 21v-2a4 4 0 0 0-3-3.87"/></>,
    user:       <><circle cx="12" cy="8" r="4"/><path d="M4 21v-1a6 6 0 0 1 6-6h4a6 6 0 0 1 6 6v1"/></>,
    chart:      <><path d="M4 20V10M10 20V4M16 20v-7M22 20H2"/></>,
    star:       <><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></>,
    clipboard:  <><rect x="9" y="2" width="6" height="4" rx="1"/><path d="M9 2H7a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2h-2"/><path d="M9 12h6M9 16h4"/></>,
    message:    <><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></>,
    settings:   <><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></>,
    logout:     <><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></>,
    close:      <><path d="M6 6l12 12M18 6L6 18"/></>,
    plus:       <><path d="M12 5v14M5 12h14"/></>,
    check:      <><path d="M5 12l4 4L19 6"/></>,
    chev_right: <><path d="M9 6l6 6-6 6"/></>,
    chev_down:  <><path d="M6 9l6 6 6-6"/></>,
    chev_left:  <><path d="M15 6l-6 6 6 6"/></>,
    search:     <><circle cx="11" cy="11" r="7"/><path d="M20 20l-3.5-3.5"/></>,
    upload:     <><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></>,
    download:   <><path d="M12 3v14M5 12l7 7 7-7"/><path d="M4 21h16"/></>,
    eye:        <><path d="M2 12s4-7 10-7 10 7 10 7-4 7-10 7-10-7-10-7z"/><circle cx="12" cy="12" r="3"/></>,
    eye_off:    <><path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"/><line x1="1" y1="1" x2="23" y2="23"/></>,
    warn:       <><path d="M12 3l10 18H2z"/><path d="M12 10v5M12 18v.5"/></>,
    info:       <><circle cx="12" cy="12" r="9"/><path d="M12 8v.5M12 11v5"/></>,
    file:       <><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></>,
    trophy:     <><path d="M6 9H4.5a2.5 2.5 0 0 1 0-5H6"/><path d="M18 9h1.5a2.5 2.5 0 0 0 0-5H18"/><path d="M4 22h16"/><path d="M10 14.66V17c0 .55-.47.98-.97 1.21C7.85 18.75 7 20.24 7 22"/><path d="M14 14.66V17c0 .55.47.98.97 1.21C16.15 18.75 17 20.24 17 22"/><path d="M18 2H6v7a6 6 0 0 0 12 0V2z"/></>,
    calendar:   <><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></>,
    trend_up:   <><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/><polyline points="17 6 23 6 23 12"/></>,
    lock:       <><rect x="5" y="11" width="14" height="10" rx="2"/><path d="M8 11V8a4 4 0 0 1 8 0v3"/></>,
    menu:       <><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/></>,
    bell:       <><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/></>,
    dot:        <><circle cx="12" cy="12" r="3" fill="currentColor"/></>,
    chev_up:    <><path d="M18 15l-6-6-6 6"/></>,
    unlock:     <><rect x="5" y="11" width="14" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 7.71-1.34"/></>,
    org:        <><rect x="9" y="2" width="6" height="5" rx="1"/><rect x="1" y="15" width="6" height="5" rx="1"/><rect x="17" y="15" width="6" height="5" rx="1"/><path d="M12 7v4M4 15v-2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v2"/></>,
    filter:     <><polygon points="22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3"/></>,
    more_v:     <><circle cx="12" cy="5" r="1.5" fill="currentColor"/><circle cx="12" cy="12" r="1.5" fill="currentColor"/><circle cx="12" cy="19" r="1.5" fill="currentColor"/></>,
    send:       <><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></>,
    drag:       <><circle cx="9" cy="6" r="1.5" fill="currentColor"/><circle cx="15" cy="6" r="1.5" fill="currentColor"/><circle cx="9" cy="12" r="1.5" fill="currentColor"/><circle cx="15" cy="12" r="1.5" fill="currentColor"/><circle cx="9" cy="18" r="1.5" fill="currentColor"/><circle cx="15" cy="18" r="1.5" fill="currentColor"/></>,
    grid:       <><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></>,
    clock:      <><circle cx="12" cy="12" r="9"/><polyline points="12 7 12 12 15 15"/></>,
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
      stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      {paths[name] || <circle cx="12" cy="12" r="8"/>}
    </svg>
  );
};

const Avatar = ({ nome, cor, iniciais, size = 36 }) => (
  <div style={{
    width: size, height: size, borderRadius: '50%', background: cor || '#00967B',
    color: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'center',
    fontWeight: 700, fontSize: size * 0.36, letterSpacing: '.02em', flexShrink: 0,
  }}>
    {iniciais || (nome || '?').slice(0, 2).toUpperCase()}
  </div>
);

const PhotoAvatar = ({ colaborador, size = 36, canEdit = false }) => {
  const [foto, setFoto] = React.useState(() => getFoto(colaborador.id));
  function handleUpload(e) {
    const file = e.target.files[0];
    if (!file) return;
    const reader = new FileReader();
    reader.onload = ev => { salvarFoto(colaborador.id, ev.target.result); setFoto(ev.target.result); };
    reader.readAsDataURL(file);
  }
  return (
    <div style={{ position: 'relative', flexShrink: 0, width: size, height: size }}>
      {foto
        ? <img src={foto} alt={colaborador.nome} style={{ width: size, height: size, borderRadius: '50%', objectFit: 'cover', display: 'block' }} />
        : <Avatar nome={colaborador.nome} cor={colaborador.cor} iniciais={colaborador.iniciais} size={size} />
      }
      {canEdit && (
        <label style={{ position: 'absolute', bottom: -2, right: -2, width: 20, height: 20, borderRadius: '50%', background: 'var(--escalab-brand)', border: '2px solid #fff', display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', color: '#fff' }} title="Alterar foto">
          <Icon name="upload" size={10} />
          <input type="file" accept="image/*" onChange={handleUpload} style={{ display: 'none' }} />
        </label>
      )}
    </div>
  );
};

const Tag = ({ children, tone = 'neutral', size = 'sm' }) => {
  const tones = {
    neutral: { bg: 'rgba(74,85,96,.09)', fg: '#4A5560', border: 'transparent' },
    brand:   { bg: '#E6F5F1', fg: '#005E4D', border: '#B5E3D7' },
    success: { bg: '#E6F5F1', fg: '#00836B', border: '#B5E3D7' },
    warn:    { bg: '#FFF4E6', fg: '#B56500', border: '#FFD6A3' },
    danger:  { bg: '#FDECEC', fg: '#B3261E', border: '#F4C7C3' },
    info:    { bg: '#EEF3FA', fg: '#1F4A8A', border: '#C9D7EC' },
    dark:    { bg: '#0A0F12', fg: '#fff', border: 'transparent' },
    amber:   { bg: '#FFF7EB', fg: '#7A4A00', border: '#FFD6A3' },
  };
  const t = tones[tone] || tones.neutral;
  const fs = size === 'xs' ? 10 : 11.5;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: fs,
      fontWeight: 700, letterSpacing: '.04em', textTransform: 'uppercase',
      padding: '3px 8px', borderRadius: 6, background: t.bg, color: t.fg,
      border: `1px solid ${t.border}`, whiteSpace: 'nowrap',
    }}>{children}</span>
  );
};

const Button = ({ variant = 'primary', size = 'md', icon, iconRight, children, onClick, disabled, style, type = 'button', full }) => {
  const sizes = { sm: { padding: '7px 13px', fontSize: 13 }, md: { padding: '10px 18px', fontSize: 14 }, lg: { padding: '13px 22px', fontSize: 15 } };
  const variants = {
    primary: { background: 'var(--escalab-brand)', color: '#fff', border: '1px solid var(--escalab-brand)' },
    dark:    { background: 'var(--escalab-ink)', color: '#fff', border: '1px solid var(--escalab-ink)' },
    outline: { background: '#fff', color: 'var(--escalab-brand-deep)', border: '1px solid var(--escalab-line)' },
    ghost:   { background: 'transparent', color: 'var(--escalab-slate)', border: '1px solid transparent' },
    danger:  { background: '#fff', color: '#B3261E', border: '1px solid #F4C7C3' },
  };
  return (
    <button type={type} onClick={onClick} disabled={disabled} style={{
      display: 'inline-flex', alignItems: 'center', gap: 7, borderRadius: 8,
      fontWeight: 500, letterSpacing: '-.005em', fontFamily: 'var(--font-sans)',
      cursor: disabled ? 'not-allowed' : 'pointer', opacity: disabled ? 0.5 : 1,
      transition: 'all .18s var(--ease-out)', width: full ? '100%' : undefined,
      justifyContent: full ? 'center' : undefined,
      ...sizes[size], ...variants[variant], ...style
    }}>
      {icon && <Icon name={icon} size={size === 'sm' ? 14 : 16} />}
      <span>{children}</span>
      {iconRight && <Icon name={iconRight} size={size === 'sm' ? 14 : 16} />}
    </button>
  );
};

const Card = ({ children, pad = 22, style, onClick, hover }) => (
  <div onClick={onClick} style={{
    background: '#fff', border: '1px solid var(--escalab-line)', borderRadius: 12,
    padding: pad, cursor: onClick ? 'pointer' : 'default',
    transition: hover ? 'box-shadow .18s, border-color .18s' : undefined,
    ...style
  }}>
    {children}
  </div>
);

const KpiCard = ({ label, value, sublabel, dark, icon, trend }) => (
  <div style={{
    background: dark ? 'var(--escalab-ink)' : '#fff',
    border: '1px solid var(--escalab-line)', borderRadius: 12, padding: '22px 24px',
  }}>
    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: 14 }}>
      <div style={{ fontSize: 11, fontWeight: 700, letterSpacing: '.12em', textTransform: 'uppercase', color: dark ? 'rgba(255,255,255,.82)' : 'var(--escalab-slate)' }}>{label}</div>
      {icon && <div style={{ color: dark ? 'var(--escalab-brand-accent)' : 'var(--escalab-brand)', opacity: .85 }}><Icon name={icon} size={18} /></div>}
    </div>
    <div style={{ fontFamily: 'var(--font-sans)', fontSize: 34, fontWeight: 800, letterSpacing: '-.03em', color: dark ? '#fff' : 'var(--escalab-brand-deep)', lineHeight: 1 }}>{value}</div>
    {sublabel && <div style={{ fontSize: 12.5, color: dark ? 'rgba(255,255,255,.78)' : 'var(--escalab-slate)', marginTop: 8, fontWeight: 500 }}>{sublabel}</div>}
    {trend != null && (
      <div style={{ display: 'inline-flex', alignItems: 'center', gap: 4, marginTop: 10, fontSize: 11.5, fontWeight: 600, color: trend >= 0 ? 'var(--escalab-brand-accent)' : '#FFB4B0' }}>
        {trend >= 0 ? '▲' : '▼'} {Math.abs(trend).toFixed(1)}% vs. ciclo anterior
      </div>
    )}
  </div>
);

const Field = ({ label, hint, children, span }) => (
  <div style={{ display: 'flex', flexDirection: 'column', gap: 6, gridColumn: span ? `span ${span}` : undefined }}>
    <label style={{ fontSize: 11, fontWeight: 700, letterSpacing: '.08em', textTransform: 'uppercase', color: 'var(--escalab-slate)' }}>{label}</label>
    {children}
    {hint && <div style={{ fontSize: 12, color: 'var(--escalab-mute)' }}>{hint}</div>}
  </div>
);

const Input = ({ value, onChange, type = 'text', placeholder, prefix, suffix, style }) => {
  const [show, setShow] = useState(false);
  const t = type === 'password' && show ? 'text' : type;
  return (
    <div style={{ display: 'flex', alignItems: 'center', border: '1px solid var(--escalab-line)', borderRadius: 8, background: '#fff', overflow: 'hidden', ...style }}>
      {prefix && <span style={{ padding: '0 8px 0 12px', color: 'var(--escalab-mute)', fontSize: 14, flexShrink: 0 }}>{prefix}</span>}
      <input value={value ?? ''} onChange={e => onChange?.(e.target.value)} type={t} placeholder={placeholder}
        style={{ border: 0, outline: 0, flex: 1, padding: '10px 12px', fontSize: 14, fontFamily: 'var(--font-sans)', background: 'transparent', color: 'var(--escalab-ink)' }} />
      {type === 'password' && (
        <button onClick={() => setShow(s => !s)} type="button" style={{ border: 0, background: 'transparent', padding: '0 12px', cursor: 'pointer', color: 'var(--escalab-mute)' }}>
          <Icon name={show ? 'eye_off' : 'eye'} size={16} />
        </button>
      )}
      {suffix && <span style={{ padding: '0 12px 0 6px', color: 'var(--escalab-mute)', fontSize: 13 }}>{suffix}</span>}
    </div>
  );
};

const SelectInput = ({ value, onChange, options, placeholder }) => (
  <div style={{ position: 'relative', border: '1px solid var(--escalab-line)', borderRadius: 8, background: '#fff' }}>
    <select value={value ?? ''} onChange={e => onChange?.(e.target.value)}
      style={{ width: '100%', border: 0, outline: 0, padding: '10px 36px 10px 12px', fontSize: 14, fontFamily: 'var(--font-sans)', background: 'transparent', appearance: 'none', color: 'var(--escalab-ink)' }}>
      {placeholder && <option value="">{placeholder}</option>}
      {options.map(o => <option key={o.value ?? o} value={o.value ?? o}>{o.label ?? o}</option>)}
    </select>
    <div style={{ position: 'absolute', right: 10, top: '50%', transform: 'translateY(-50%)', pointerEvents: 'none', color: 'var(--escalab-slate)' }}>
      <Icon name="chev_down" size={14} />
    </div>
  </div>
);

const Progress = ({ value, max = 5, tone = 'brand', label, showValue }) => {
  const pct = Math.min(100, (value / max) * 100);
  const color = { brand: 'var(--escalab-brand)', accent: 'var(--escalab-brand-accent)', warn: '#E89B3B', danger: '#E05050' }[tone] || 'var(--escalab-brand)';
  return (
    <div>
      {(label || showValue) && (
        <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 5 }}>
          {label && <span style={{ fontSize: 12, color: 'var(--escalab-slate)' }}>{label}</span>}
          {showValue && <span style={{ fontSize: 12, fontWeight: 600, color: 'var(--escalab-brand-deep)' }}>{value?.toFixed ? value.toFixed(1) : value}</span>}
        </div>
      )}
      <div style={{ width: '100%', height: 6, background: 'var(--escalab-line)', borderRadius: 999, overflow: 'hidden' }}>
        <div style={{ width: pct + '%', height: '100%', background: color, transition: 'width .5s var(--ease-out)', borderRadius: 999 }} />
      </div>
    </div>
  );
};

const Modal = ({ open, onClose, title, children, width = 560, footer }) => {
  if (!open) return null;
  const isMob = window.innerWidth <= 768;
  return (
    <div onClick={onClose} style={{
      position: 'fixed', inset: 0, background: 'rgba(10,15,18,.55)', backdropFilter: 'blur(4px)',
      display: 'flex', alignItems: isMob ? 'flex-end' : 'center', justifyContent: 'center',
      zIndex: 1000, padding: isMob ? 0 : 20, animation: 'fadeIn .18s var(--ease-out)'
    }}>
      <div onClick={e => e.stopPropagation()} style={{
        background: '#fff', borderRadius: isMob ? '20px 20px 0 0' : 14,
        width: '100%', maxWidth: isMob ? '100%' : width, maxHeight: '92vh',
        boxShadow: 'var(--shadow-lg)', overflow: 'hidden', animation: 'popIn .22s var(--ease-out)',
        display: 'flex', flexDirection: 'column',
      }}>
        <div style={{ padding: '18px 20px', borderBottom: '1px solid var(--escalab-line)', display: 'flex', alignItems: 'center', justifyContent: 'space-between', flexShrink: 0 }}>
          <h3 style={{ fontSize: 17, fontWeight: 600, margin: 0 }}>{title}</h3>
          <button onClick={onClose} style={{ border: 0, background: 'transparent', cursor: 'pointer', color: 'var(--escalab-slate)', padding: 4 }}><Icon name="close" size={18} /></button>
        </div>
        <div style={{ padding: isMob ? 16 : 24, maxHeight: '75vh', overflowY: 'auto', flex: 1 }}>{children}</div>
        {footer && (
          <div style={{ padding: '13px 20px', borderTop: '1px solid var(--escalab-line)', background: 'var(--escalab-paper)', display: 'flex', justifyContent: 'flex-end', gap: 10, flexShrink: 0 }}>
            {footer}
          </div>
        )}
      </div>
    </div>
  );
};

const Banner = ({ tone = 'info', children, onClose }) => {
  const s = { info: { bg: '#EEF3FA', fg: '#1F4A8A', border: '#C9D7EC', icon: 'info' }, success: { bg: '#E6F5F1', fg: '#005E4D', border: '#B5E3D7', icon: 'check' }, warn: { bg: '#FFF7EB', fg: '#7A4A00', border: '#FFD6A3', icon: 'warn' }, danger: { bg: '#FDECEC', fg: '#B3261E', border: '#F4C7C3', icon: 'warn' } }[tone];
  return (
    <div style={{ display: 'flex', gap: 10, alignItems: 'flex-start', background: s.bg, border: `1px solid ${s.border}`, color: s.fg, padding: '11px 14px', borderRadius: 10, fontSize: 13.5, lineHeight: 1.5 }}>
      <div style={{ flexShrink: 0, marginTop: 1 }}><Icon name={s.icon} size={16} /></div>
      <div style={{ flex: 1 }}>{children}</div>
      {onClose && <button onClick={onClose} style={{ border: 0, background: 'transparent', cursor: 'pointer', color: 'inherit', opacity: .6, padding: 0 }}><Icon name="close" size={13} /></button>}
    </div>
  );
};

const Tabs = ({ tabs, active, onChange }) => {
  const scrollRef = useRef(null);
  const [overflow, setOverflow] = useState({ left: false, right: false });

  const checkOverflow = () => {
    const el = scrollRef.current;
    if (!el) return;
    setOverflow({
      left: el.scrollLeft > 2,
      right: el.scrollLeft + el.clientWidth < el.scrollWidth - 2,
    });
  };

  useEffect(() => {
    checkOverflow();
    const el = scrollRef.current;
    if (!el) return;
    el.addEventListener('scroll', checkOverflow, { passive: true });
    window.addEventListener('resize', checkOverflow);
    return () => {
      el.removeEventListener('scroll', checkOverflow);
      window.removeEventListener('resize', checkOverflow);
    };
  }, [tabs.length]);

  // Auto-scroll para a aba ativa ficar visível
  useEffect(() => {
    const el = scrollRef.current;
    if (!el) return;
    const btn = el.querySelector(`[data-tab-id="${active}"]`);
    if (btn && btn.scrollIntoView) btn.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'nearest' });
  }, [active]);

  const scrollBy = (delta) => {
    if (scrollRef.current) scrollRef.current.scrollBy({ left: delta, behavior: 'smooth' });
  };

  const arrowBtn = {
    position: 'absolute', top: 0, bottom: 1, width: 32,
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    border: 0, cursor: 'pointer', zIndex: 2,
    color: 'var(--escalab-slate)',
  };

  return (
    <div style={{ position: 'relative', borderBottom: '1px solid var(--escalab-line)' }}>
      <div ref={scrollRef} style={{
        display: 'flex', gap: 2, overflowX: 'auto', WebkitOverflowScrolling: 'touch',
        scrollbarWidth: 'none', msOverflowStyle: 'none',
        paddingLeft: overflow.left ? 28 : 0, paddingRight: overflow.right ? 28 : 0,
        transition: 'padding .15s',
      }}>
        {tabs.map(t => (
          <button key={t.id} data-tab-id={t.id} onClick={() => onChange(t.id)} style={{
            border: 0, background: 'transparent', padding: '10px 14px', fontSize: 13.5, fontWeight: 500,
            cursor: 'pointer', color: active === t.id ? 'var(--escalab-brand-deep)' : 'var(--escalab-slate)',
            borderBottom: active === t.id ? '2px solid var(--escalab-brand)' : '2px solid transparent',
            marginBottom: -1, transition: 'color .15s, border-color .15s', display: 'flex', alignItems: 'center', gap: 7,
            whiteSpace: 'nowrap', flexShrink: 0, fontFamily: 'var(--font-sans)',
          }}>
            {t.icon && <Icon name={t.icon} size={14} />}
            {t.label}
            {t.count != null && (
              <span style={{ background: active === t.id ? 'var(--escalab-brand-tint)' : 'var(--escalab-paper)', color: active === t.id ? 'var(--escalab-brand-deep)' : 'var(--escalab-mute)', borderRadius: 999, padding: '1px 7px', fontSize: 11, fontWeight: 600 }}>{t.count}</span>
            )}
          </button>
        ))}
      </div>

      {/* Fade + seta esquerda */}
      {overflow.left && (
        <>
          <div style={{ position: 'absolute', left: 0, top: 0, bottom: 1, width: 56, background: 'linear-gradient(90deg, #fff 30%, rgba(255,255,255,0) 100%)', pointerEvents: 'none', zIndex: 1 }} />
          <button onClick={() => scrollBy(-160)} style={{ ...arrowBtn, left: 0 }} aria-label="Rolar para esquerda">
            <Icon name="chev_left" size={16} />
          </button>
        </>
      )}

      {/* Fade + seta direita */}
      {overflow.right && (
        <>
          <div style={{ position: 'absolute', right: 0, top: 0, bottom: 1, width: 56, background: 'linear-gradient(-90deg, #fff 30%, rgba(255,255,255,0) 100%)', pointerEvents: 'none', zIndex: 1 }} />
          <button onClick={() => scrollBy(160)} style={{ ...arrowBtn, right: 0 }} aria-label="Rolar para direita">
            <Icon name="chev_right" size={16} />
          </button>
        </>
      )}
    </div>
  );
};

const ScoreBar = ({ nota, label, max = 5 }) => {
  if (nota == null) return null;
  const pct = (nota / max) * 100;
  const color = nota >= 4.5 ? 'var(--escalab-brand-accent)' : nota >= 4 ? 'var(--escalab-brand)' : nota >= 3 ? '#E89B3B' : '#E05050';
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
      {label && <span style={{ fontSize: 12.5, color: 'var(--escalab-slate)', minWidth: 120 }}>{label}</span>}
      <div style={{ flex: 1, height: 7, background: 'var(--escalab-line)', borderRadius: 999 }}>
        <div style={{ width: pct + '%', height: '100%', background: color, borderRadius: 999, transition: 'width .6s var(--ease-out)' }} />
      </div>
      <span style={{ fontSize: 13, fontWeight: 700, color: 'var(--escalab-brand-deep)', minWidth: 30, textAlign: 'right' }}>{nota.toFixed(1)}</span>
    </div>
  );
};

function useMobile() {
  const [w, setW] = useState(window.innerWidth);
  useEffect(() => {
    const h = () => setW(window.innerWidth);
    window.addEventListener('resize', h);
    return () => window.removeEventListener('resize', h);
  }, []);
  return w <= 768;
}

Object.assign(window, { Icon, Avatar, PhotoAvatar, Tag, Button, Card, KpiCard, Field, Input, SelectInput, Progress, Modal, Banner, Tabs, ScoreBar, useMobile });
