const CW = window.CoworkOSDesignSystem_82f831;
const cwWrap = { maxWidth:'var(--layout-max)', margin:'0 auto', padding:'0 var(--layout-gutter)' };
const cwH2 = { fontSize:'clamp(32px, 4.2vw, 52px)', lineHeight:1.1, letterSpacing:'var(--ls-display)', fontWeight:'var(--fw-display)', margin:0, textWrap:'balance' };
const cwStatusTone = { revisar:'warn', atualizada:'ok', prioridade:'risk', Revisar:'warn', Atualizada:'ok', Prioridade:'risk', Ativo:'ok', Resolvida:'ok', Concluída:'ok', 'Em acompanhamento':'warn' };
const cwSignalTone = { low:'info', medium:'warn', high:'risk' };

function useNarrow(bp) {
  const [n, setN] = React.useState(() => window.innerWidth < bp);
  React.useEffect(() => { const on = () => setN(window.innerWidth < bp); window.addEventListener('resize', on); return () => window.removeEventListener('resize', on); }, [bp]);
  return n;
}
const cwReduced = () => window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
function useInView(opts) {
  const ref = React.useRef(null);
  const [seen, setSeen] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current; if (!el) return;
    if (cwReduced() || !('IntersectionObserver' in window)) { setSeen(true); return; }
    const io = new IntersectionObserver(([e]) => { if (e.isIntersecting) { setSeen(true); io.disconnect(); } }, { threshold: 0.15, rootMargin: '0px 0px -8% 0px', ...opts });
    io.observe(el); return () => io.disconnect();
  }, []);
  return [ref, seen];
}
function Reveal({ children, delay = 0, y = 16, as = 'div', style, ...rest }) {
  const [ref, seen] = useInView();
  const Tag = as;
  return <Tag ref={ref} {...rest} style={{ opacity: seen ? 1 : 0, transform: seen ? 'none' : `translateY(${y}px)`, transition: `opacity var(--dur-reveal) var(--ease-emphasis) ${delay}ms, transform var(--dur-reveal) var(--ease-emphasis) ${delay}ms`, ...style }}>{children}</Tag>;
}
function CountUp({ to, pad = 0, dur = 1100, delay = 0 }) {
  const [ref, seen] = useInView();
  const [v, setV] = React.useState(0);
  React.useEffect(() => {
    if (!seen) return;
    if (cwReduced()) { setV(to); return; }
    let raf, t0;
    const tick = (t) => { if (t0 == null) t0 = t + delay; const p = Math.min(1, Math.max(0, (t - t0) / dur)); setV(Math.round(to * (1 - Math.pow(1 - p, 3)))); if (p < 1) raf = requestAnimationFrame(tick); };
    raf = requestAnimationFrame(tick); return () => cancelAnimationFrame(raf);
  }, [seen]);
  return <span ref={ref} style={{ fontVariantNumeric: 'tabular-nums' }}>{String(v).padStart(pad, '0')}</span>;
}
const goTo = (href) => () => { window.location.href = href; };

function Overline({ children, dark, style }) {
  return <div style={{fontSize:'var(--fs-12)',fontWeight:700,letterSpacing:'var(--ls-caps)',textTransform:'uppercase',color:dark?'var(--orange-400)':'var(--navy-600)',...style}}>{children}</div>;
}

function PageHeader({ crumb, title, text, action, big }) {
  return (
    <div style={{display:'flex',alignItems:'flex-start',justifyContent:'space-between',gap:20,flexWrap:'wrap'}}>
      <div style={{minWidth:0}}>
        <Overline>{crumb}</Overline>
        <h1 style={{fontSize:big?'var(--fs-30)':'var(--fs-24)',fontWeight:'var(--fw-display)',letterSpacing:'var(--ls-display)',color:'var(--text-heading)',margin:'8px 0 0',lineHeight:1.2}}>{title}</h1>
        {text ? <p style={{fontSize:'var(--fs-14)',color:'var(--text-muted)',margin:'6px 0 0'}}>{text}</p> : null}
      </div>
      {action ? <div style={{flex:'none'}}>{action}</div> : null}
    </div>
  );
}

function StatusBadge({ label }) {
  return <CW.Badge tone={cwStatusTone[label] || 'neutral'} dot style={{whiteSpace:'nowrap'}}>{label}</CW.Badge>;
}

function CnpjCell({ name, cnpj, sub }) {
  return <div><div style={{fontWeight:600,color:'var(--text-heading)'}}>{name}</div>{cnpj || sub ? <div style={{fontFamily:cnpj?'var(--font-mono)':'var(--font-ui)',fontSize:12,color:'var(--text-muted)',marginTop:2,whiteSpace:'nowrap'}}>{cnpj || sub}</div> : null}</div>;
}

Object.assign(window, { useInView, Reveal, CountUp, CW, cwWrap, cwH2, cwStatusTone, cwSignalTone, useNarrow, goTo, Overline, PageHeader, StatusBadge, CnpjCell });
