// app-shell.jsx — shared primitives, icons, tokens for all app screens
// Loaded BEFORE the screen files. Exports to window.

const T = {
  bg: '#fafafa',
  surface: '#ffffff',
  surface2: '#f4f4f5',
  surface3: '#f9fafb',
  border: '#e5e7eb',
  borderStrong: '#d1d5db',
  ink: '#0f172a',
  ink2: '#334155',
  ink3: '#64748b',
  ink4: '#94a3b8',
  inkMute: '#cbd5e1',
  ok: '#15803d',
  warn: '#a16207',
  bad: '#991b1b',
  serif: "'Newsreader', 'Source Serif 4', Georgia, serif",
  sans: "'Geist', ui-sans-serif, system-ui, sans-serif",
  mono: "'Geist Mono', ui-monospace, Menlo, monospace",
};

// ─────────────────────────────────────────────────────────── Icons
const Icon = ({ name, size = 16, stroke = 1.4, color = 'currentColor' }) => {
  const s = { width: size, height: size, stroke: color, fill: 'none', strokeWidth: stroke, strokeLinecap: 'round', strokeLinejoin: 'round' };
  switch (name) {
    case 'search':   return <svg style={s} viewBox="0 0 24 24"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>;
    case 'plus':     return <svg style={s} viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></svg>;
    case 'chevR':    return <svg style={s} viewBox="0 0 24 24"><path d="m9 6 6 6-6 6"/></svg>;
    case 'chevD':    return <svg style={s} viewBox="0 0 24 24"><path d="m6 9 6 6 6-6"/></svg>;
    case 'chevL':    return <svg style={s} viewBox="0 0 24 24"><path d="m15 6-6 6 6 6"/></svg>;
    case 'sparkle':  return <svg style={s} viewBox="0 0 24 24"><path d="M12 2v6M12 16v6M2 12h6M16 12h6M5 5l4 4M15 15l4 4M5 19l4-4M15 9l4-4"/></svg>;
    case 'check':    return <svg style={s} viewBox="0 0 24 24"><path d="m5 12 4 4 10-10"/></svg>;
    case 'x':        return <svg style={s} viewBox="0 0 24 24"><path d="M6 6l12 12M18 6 6 18"/></svg>;
    case 'doc':      return <svg style={s} viewBox="0 0 24 24"><path d="M14 3H6v18h12V7zM14 3v4h4"/></svg>;
    case 'blocks':   return <svg style={s} viewBox="0 0 24 24"><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"/></svg>;
    case 'people':   return <svg style={s} viewBox="0 0 24 24"><circle cx="9" cy="8" r="3.5"/><path d="M3 21c0-3.3 2.7-6 6-6s6 2.7 6 6"/><circle cx="17" cy="9" r="2.5"/><path d="M14 16c2-1 5-1 7 1"/></svg>;
    case 'gear':     return <svg style={s} viewBox="0 0 24 24"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8L4.2 7a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></svg>;
    case 'map':      return <svg style={s} viewBox="0 0 24 24"><path d="M9 4 3 7v13l6-3 6 3 6-3V4l-6 3z"/><path d="M9 4v13M15 7v13"/></svg>;
    case 'down':     return <svg style={s} viewBox="0 0 24 24"><path d="M12 4v14M6 13l6 6 6-6"/></svg>;
    case 'up':       return <svg style={s} viewBox="0 0 24 24"><path d="M12 20V6M6 11l6-6 6 6"/></svg>;
    case 'grip':     return <svg style={s} viewBox="0 0 24 24"><circle cx="9" cy="6" r="1.2"/><circle cx="15" cy="6" r="1.2"/><circle cx="9" cy="12" r="1.2"/><circle cx="15" cy="12" r="1.2"/><circle cx="9" cy="18" r="1.2"/><circle cx="15" cy="18" r="1.2"/></svg>;
    case 'more':     return <svg style={s} viewBox="0 0 24 24"><circle cx="5" cy="12" r="1.2"/><circle cx="12" cy="12" r="1.2"/><circle cx="19" cy="12" r="1.2"/></svg>;
    case 'edit':     return <svg style={s} viewBox="0 0 24 24"><path d="M4 20h4l11-11-4-4L4 16zM14 5l4 4"/></svg>;
    case 'export':   return <svg style={s} viewBox="0 0 24 24"><path d="M12 4v12M6 10l6-6 6 6M4 20h16"/></svg>;
    case 'preview': return <svg style={s} viewBox="0 0 24 24"><path d="M2 12s3.5-6 10-6 10 6 10 6-3.5 6-10 6S2 12 2 12z"/><circle cx="12" cy="12" r="2.5"/></svg>;
    case 'pin':      return <svg style={s} viewBox="0 0 24 24"><path d="M12 22V13"/><path d="M8 13h8l-2-6 3-3H9l3 3z"/></svg>;
    case 'filter':   return <svg style={s} viewBox="0 0 24 24"><path d="M3 5h18M6 12h12M10 19h4"/></svg>;
    case 'arr-r':    return <svg style={s} viewBox="0 0 24 24"><path d="M5 12h14M13 6l6 6-6 6"/></svg>;
    case 'arr-l':    return <svg style={s} viewBox="0 0 24 24"><path d="M19 12H5M11 18l-6-6 6-6"/></svg>;
    case 'home':     return <svg style={s} viewBox="0 0 24 24"><path d="M3 12 12 4l9 8"/><path d="M5 10v10h14V10"/></svg>;
    case 'pl':       return <svg style={s} viewBox="0 0 24 24"><rect x="3" y="6" width="18" height="6" fill="#fff" stroke={color}/><rect x="3" y="12" width="18" height="6" fill="#dc2626" stroke={color}/></svg>;
    case 'chevU':    return <svg style={s} viewBox="0 0 24 24"><path d="m6 15 6-6 6 6"/></svg>;
    case 'brand':    return <svg style={s} viewBox="0 0 24 24"><rect x="5" y="3" width="14" height="18" rx="1"/><path d="M5 8h14"/><circle cx="9" cy="5.5" r="1"/><path d="M8 12h8M8 15h6"/></svg>;
    case 'building': return <svg style={s} viewBox="0 0 24 24"><rect x="4" y="2" width="16" height="20" rx="1"/><path d="M9 22V12h6v10M9 7h1M14 7h1M9 12h1M14 12h1"/></svg>;
    case 'mail':     return <svg style={s} viewBox="0 0 24 24"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="m2 7 10 8 10-8"/></svg>;
    case 'lock':     return <svg style={s} viewBox="0 0 24 24"><rect x="5" y="11" width="14" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg>;
    case 'sun':      return <svg style={s} viewBox="0 0 24 24"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M2 12h2M20 12h2M5 5l1.4 1.4M17.2 17.2 18.6 18.6M5 19l1.4-1.4M17.2 6.8 18.6 5.4"/></svg>;
    case 'trash':    return <svg style={s} viewBox="0 0 24 24"><path d="M3 6h18M8 6V4h8v2M19 6l-1 14H6L5 6"/></svg>;
    default: return null;
  }
};

// ─────────────────────────────────────────────────────────── App shell
const AppShell = ({ children, breadcrumbs = [], activeNav = 'operaty', rightChrome, navCollapsed = false }) => (
  <div style={{ width: '100%', height: '100%', background: T.bg, color: T.ink, fontFamily: T.sans, fontSize: 13.5, lineHeight: 1.45, display: 'grid', gridTemplateColumns: navCollapsed ? '64px 1fr' : '220px 1fr', gridTemplateRows: '52px 1fr', overflow: 'hidden' }}>
    {/* Topbar */}
    <div style={{ gridColumn: '1 / -1', height: 52, borderBottom: `1px solid ${T.border}`, background: T.surface, display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 18px' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 18 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, fontFamily: T.serif, fontSize: 19, letterSpacing: '-0.01em', color: T.ink }}>
          <img src="logo.png" alt="" style={{ width: 26, height: 26, objectFit: 'contain', display: 'block' }} />
          <span style={{ lineHeight: 1 }}>Szacownia</span>
        </div>
        {breadcrumbs.length > 0 && <div style={{ width: 1, height: 18, background: T.border }} />}
        {breadcrumbs.length > 0 && <Breadcrumb items={breadcrumbs} />}
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
        {rightChrome}
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, height: 30, padding: '0 12px', border: `1px solid ${T.border}`, borderRadius: 4, background: T.bg, color: T.ink3, fontSize: 12.5, width: 260 }}>
          <Icon name="search" size={14} color={T.ink3} />
          <span>Szukaj operatów, bloków…</span>
        </div>
        <button style={{ width: 30, height: 30, borderRadius: 4, border: 'none', background: 'transparent', display: 'inline-flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer' }}>
          <Icon name="sun" size={16} color={T.ink3} />
        </button>
        <div style={{ width: 28, height: 28, borderRadius: '50%', background: T.surface2, border: `1px solid ${T.border}`, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontFamily: T.serif, fontSize: 12.5, fontStyle: 'italic', color: T.ink2 }}>JK</div>
      </div>
    </div>

    {/* Sidebar */}
    <aside style={{ borderRight: `1px solid ${T.border}`, background: T.surface, padding: navCollapsed ? '20px 8px' : '20px 14px', display: 'flex', flexDirection: 'column', gap: 4, overflow: 'hidden' }}>
      {navCollapsed ? (
        <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 18 }}>
          <div style={{ width: 32, height: 32, borderRadius: 4, background: T.ink, color: T.surface, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontFamily: T.serif, fontStyle: 'italic', fontSize: 16 }}>k</div>
        </div>
      ) : (
        <>
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 8 }}>
            <Caps style={{ paddingLeft: 8 }}>Przestrzeń Robocza</Caps>
            <Icon name="chevL" size={13} color={T.ink3} />
          </div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '8px 8px', border: `1px solid ${T.border}`, borderRadius: 4, marginBottom: 18 }}>
            <div style={{ width: 22, height: 22, borderRadius: 3, background: T.ink, color: T.surface, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontFamily: T.serif, fontStyle: 'italic', fontSize: 13 }}>k</div>
            <div style={{ display: 'flex', flexDirection: 'column', flex: 1, minWidth: 0 }}>
              <span style={{ fontSize: 13, color: T.ink, fontWeight: 500, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>Kowalski RM</span>
            </div>
          </div>
        </>
      )}

      <NavItem icon="doc"      label="Operaty"   active={activeNav === 'operaty'}   badge="14" collapsed={navCollapsed} />
      <NavItem icon="blocks"   label="Blokoteka" active={activeNav === 'blokoteka'} badge="86" collapsed={navCollapsed} />
      <NavItem icon="brand"    label="Branding"  active={activeNav === 'branding'} collapsed={navCollapsed} />
      <NavItem icon="edit"     label="Zmienne"   active={activeNav === 'zmienne'} collapsed={navCollapsed} />
      <NavItem icon="building" label="RCN"       active={activeNav === 'rciwn'} collapsed={navCollapsed} />
      <NavItem icon="people"   label="Klienci"   active={activeNav === 'klienci'}   badge="8" collapsed={navCollapsed} />

      <div style={{ height: 1, background: T.border, margin: navCollapsed ? '14px 4px' : '14px 8px' }} />

      {!navCollapsed && <Caps style={{ marginBottom: 8, paddingLeft: 8 }}>Konto</Caps>}
      <NavItem icon="gear"   label="Ustawienia" active={activeNav === 'settings'} collapsed={navCollapsed} />
      <NavItem icon="export" label="Eksporty"   active={activeNav === 'exports'} badge="42" collapsed={navCollapsed} />

      <div style={{ marginTop: 'auto', paddingTop: 12 }}>
        <NavItem icon="mail" label="Zgłoś opinię" collapsed={navCollapsed} />
        <NavItem icon="lock" label="Wyloguj się" collapsed={navCollapsed} />
        <div style={{ height: 1, background: T.border, margin: '10px 0' }} />
        {!navCollapsed && (
          <div style={{ padding: '0 8px', fontFamily: T.mono, fontSize: 10.5, color: T.ink4, letterSpacing: '0.05em', lineHeight: 1.6 }}>
            Plan Solo<br/>
            Twoje operaty · 7/10<br/>
            AI dzisiaj · 12/50
          </div>
        )}
      </div>
    </aside>

    {/* Content */}
    <main style={{ overflow: 'hidden', position: 'relative', background: T.bg }}>
      {children}
    </main>
  </div>
);

const NavItem = ({ icon, label, active, badge, muted, collapsed }) => (
  <div title={collapsed ? label : undefined} style={{ display: 'flex', alignItems: 'center', justifyContent: collapsed ? 'center' : 'flex-start', gap: 10, padding: collapsed ? '8px 0' : '7px 10px', borderRadius: 4, background: active ? T.surface2 : 'transparent', color: muted ? T.ink4 : (active ? T.ink : T.ink2), fontSize: 13, fontWeight: active ? 500 : 400, cursor: 'pointer' }}>
    <Icon name={icon} size={15} color={muted ? T.ink4 : (active ? T.ink : T.ink3)} />
    {!collapsed && <span style={{ flex: 1 }}>{label}</span>}
    {!collapsed && badge && <span style={{ fontFamily: T.mono, fontSize: 10.5, color: muted ? T.ink4 : T.ink3, letterSpacing: '0.04em' }}>{badge}</span>}
  </div>
);

const Breadcrumb = ({ items }) => (
  <div style={{ display: 'flex', alignItems: 'center', gap: 8, color: T.ink3, fontSize: 13 }}>
    {items.map((it, i) => (
      <React.Fragment key={i}>
        {i > 0 && <Icon name="chevR" size={12} color={T.ink4} />}
        <span style={{ color: i === items.length - 1 ? T.ink : T.ink3 }}>{it}</span>
      </React.Fragment>
    ))}
  </div>
);

// ─────────────────────────────────────────────────────────── Primitives
const Caps = ({ children, style }) => (
  <div style={{ fontFamily: T.mono, fontSize: 10.5, color: T.ink3, letterSpacing: '0.14em', textTransform: 'uppercase', ...style }}>{children}</div>
);

const Pill = ({ children, tone = 'neutral' }) => {
  const dot = { neutral: T.ink3, ok: T.ok, warn: T.warn, bad: T.bad }[tone];
  return (
    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, height: 22, padding: '0 9px', border: `1px solid ${T.border}`, borderRadius: 999, fontSize: 11.5, color: T.ink2, background: T.surface, fontFamily: T.mono, letterSpacing: '0.02em' }}>
      <span style={{ width: 5, height: 5, borderRadius: '50%', background: dot }} />
      {children}
    </span>
  );
};

const Btn = ({ children, variant = 'primary', size = 'md', icon, ...rest }) => {
  const h = size === 'sm' ? 28 : 34;
  const fs = size === 'sm' ? 12.5 : 13.5;
  const px = size === 'sm' ? 10 : 14;
  const styles = {
    primary: { background: T.ink, color: '#fff', border: '1px solid transparent' },
    ghost: { background: 'transparent', color: T.ink, border: `1px solid ${T.borderStrong}` },
    quiet: { background: 'transparent', color: T.ink2, border: '1px solid transparent' },
    danger: { background: 'transparent', color: T.bad, border: `1px solid ${T.border}` },
  }[variant];
  return (
    <button style={{ ...styles, height: h, padding: `0 ${px}px`, borderRadius: 4, fontFamily: T.sans, fontSize: fs, fontWeight: 500, display: 'inline-flex', alignItems: 'center', gap: 7, cursor: 'pointer' }} {...rest}>
      {icon && <Icon name={icon} size={fs} />}
      {children}
    </button>
  );
};

const Card = ({ children, style }) => (
  <div style={{ background: T.surface, border: `1px solid ${T.border}`, borderRadius: 6, ...style }}>{children}</div>
);

const Hr = ({ vertical, style }) => (
  vertical
    ? <div style={{ width: 1, alignSelf: 'stretch', background: T.border, ...style }} />
    : <div style={{ height: 1, width: '100%', background: T.border, ...style }} />
);

// Export to window
Object.assign(window, { T, Icon, AppShell, NavItem, Breadcrumb, Caps, Pill, Btn, Card, Hr });
