// ─────────────────────────────────────────────────────────────
// components.jsx — Sidebar, Page header, KPI tile, Button,
// Alert, Segmented control, Chip, CategoryCard.
// All styles use CSS variables so dark mode just flows through.
// ─────────────────────────────────────────────────────────────

// ── BUTTON ───────────────────────────────────────────────────
function LedgerButton({ variant = 'primary', icon, iconRight, children, onClick, type = 'button', disabled, full = false, size = 'md' }) {
  const [hover, setHover] = React.useState(false);
  const [press, setPress] = React.useState(false);
  const styles = {
    primary: { bg: 'var(--brand-magenta)', fg: 'var(--fg-on-accent)', hoverBg: 'var(--brand-magenta-700)' },
    dark: { bg: 'var(--bg-inverse)', fg: 'var(--fg-on-inverse)', hoverBg: 'var(--slate-700)' },
    secondary: { bg: 'var(--bg-inset)', fg: 'var(--fg-strong)', hoverBg: 'var(--slate-200)' },
    ghost: { bg: 'transparent', fg: 'var(--fg)', hoverBg: 'var(--bg-inset)' },
    danger: { bg: 'var(--negative-500)', fg: '#fff', hoverBg: 'var(--negative-700)' }
  }[variant];
  const padY = size === 'sm' ? 8 : size === 'lg' ? 14 : 10;
  const padX = size === 'sm' ? 14 : size === 'lg' ? 24 : 20;
  return (
    <button
      type={type}
      disabled={disabled}
      onClick={onClick}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => {setHover(false);setPress(false);}}
      onMouseDown={() => setPress(true)}
      onMouseUp={() => setPress(false)}
      style={{
        all: 'unset', cursor: disabled ? 'not-allowed' : 'pointer',
        opacity: disabled ? 0.4 : 1,
        boxSizing: 'border-box',
        display: full ? 'flex' : 'inline-flex',
        width: full ? '100%' : 'auto',
        alignItems: 'center', justifyContent: 'center', gap: 8,
        padding: `${padY}px ${padX}px`,
        borderRadius: 'var(--r-3)',
        background: press ? styles.hoverBg : hover ? styles.hoverBg : styles.bg,
        color: styles.fg,
        fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: size === 'sm' ? 13 : 14, lineHeight: '20px',
        whiteSpace: 'nowrap',
        transition: 'background var(--dur-fast) var(--ease-out)'
      }}>
      
      {icon && <LedgerIcon name={icon} size={16} color={styles.fg} />}
      {children}
      {iconRight && <LedgerIcon name={iconRight} size={16} color={styles.fg} />}
    </button>);

}

// ── PAGE HEADER ──────────────────────────────────────────────
function LedgerPageHeader({ eyebrow, title, actions }) {
  return (
    <div className="ledger-page-header" style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', gap: 24, marginBottom: 8, flexWrap: 'wrap' }}>
      <div style={{ minWidth: 0, flex: '1 1 320px' }}>
        <div className="eyebrow" style={{ marginBottom: 8 }}>{eyebrow}</div>
        <h1 className="hero" style={{ textWrap: 'pretty' }}>{title}</h1>
      </div>
      {actions && <div className="ledger-header-actions" style={{ display: 'flex', gap: 12, alignItems: 'center', flexShrink: 0, flexWrap: 'wrap' }}>{actions}</div>}
    </div>);

}

// ── AVATAR (single, monogram) ────────────────────────────────
function LedgerAvatar({ email, size = 40 }) {
  const initials = (email || '?').split('@')[0].slice(0, 2).toUpperCase();
  return (
    <div style={{
      width: size, height: size, borderRadius: 'var(--r-5)',
      background: 'linear-gradient(135deg, var(--brand-magenta-300), var(--brand-magenta))',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      color: '#fff', fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: size * 0.32,
      letterSpacing: '0.4px'
    }}>{initials}</div>);

}

// ── KPI TILE ─────────────────────────────────────────────────
function LedgerKpi({ label, value, footer, variant = 'default', icon }) {
  const styles = {
    default: { bg: 'var(--bg-elevated)', fg: 'var(--fg-strong)', border: '1px solid var(--border)' },
    positive: { bg: 'var(--bg-elevated)', fg: 'var(--fg-positive)', border: '2px solid var(--positive-500)' },
    negative: { bg: 'var(--bg-elevated)', fg: 'var(--brand-magenta)', border: '2px solid var(--brand-magenta)' },
    info: { bg: 'var(--bg-elevated)', fg: 'var(--info-500)', border: '2px solid var(--info-500)' },
    inverse: { bg: 'var(--bg-inverse)', fg: 'var(--fg-on-inverse)', border: 'none' }
  }[variant];
  const labelColor = variant === 'inverse' ? 'var(--fg-on-inverse-2)' : 'var(--fg-3)';
  return (
    <div style={{
      flex: 1, minWidth: 0, background: styles.bg, borderRadius: 'var(--r-4)',
      padding: '24px 28px',
      border: styles.border, boxSizing: 'border-box',
      display: 'flex', flexDirection: 'column', justifyContent: 'space-between', minHeight: 156,
      transition: 'border-color var(--dur-base) var(--ease-out)'
    }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start' }}>
        <div className="label" style={{ color: labelColor, textTransform: 'uppercase' }}>{label}</div>
        {icon && <LedgerIcon name={icon} size={16} color={labelColor} />}
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
        <div className="num" style={{ fontSize: 26, lineHeight: '34px', letterSpacing: '-0.6px', color: styles.fg, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{value}</div>
        {footer && (
        variant === 'inverse' ?
        <div className="italic-meta" style={{ color: 'var(--fg-on-inverse-3)' }}>{footer}</div> :
        <div style={{ fontFamily: 'var(--font-body)', fontSize: 12, color: 'var(--fg-2)' }}>{footer}</div>)
        }
      </div>
    </div>);

}

// ── ALERT ────────────────────────────────────────────────────
function LedgerAlert({ tone = 'negative', title, body, icon = 'trending-down', action }) {
  const tones = {
    negative: { bg: 'var(--brand-magenta-100)', border: 'var(--brand-magenta-300)', iconBg: 'var(--brand-magenta-300)', iconColor: 'var(--brand-magenta-700)', fg: 'var(--fg-strong)', fgBody: 'var(--fg-2)' },
    positive: { bg: 'var(--positive-50)', border: 'var(--positive-300)', iconBg: 'var(--positive-100)', iconColor: 'var(--positive-700)', fg: 'var(--fg-strong)', fgBody: 'var(--fg-2)' },
    info: { bg: 'var(--info-100)', border: 'var(--info-500)', iconBg: 'var(--info-100)', iconColor: 'var(--info-500)', fg: 'var(--fg-strong)', fgBody: 'var(--fg-2)' },
    warning: { bg: 'var(--warning-100)', border: 'var(--warning-500)', iconBg: 'var(--warning-100)', iconColor: 'var(--warning-500)', fg: 'var(--fg-strong)', fgBody: 'var(--fg-2)' }
  }[tone];
  return (
    <div style={{
      background: tones.bg, border: `1px solid ${tones.border}`, borderRadius: 'var(--r-4)',
      padding: 16, display: 'flex', gap: 16, alignItems: 'center'
    }}>
      <div style={{ flex: '0 0 40px', height: 40, borderRadius: 'var(--r-5)', background: tones.iconBg, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
        <LedgerIcon name={icon} size={20} color={tones.iconColor} />
      </div>
      <div style={{ flex: 1 }}>
        <div style={{ fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: 14, lineHeight: '20px', color: tones.fg }}>{title}</div>
        <div style={{ fontFamily: 'var(--font-body)', fontSize: 12, lineHeight: '16px', color: tones.fgBody, marginTop: 2 }}>{body}</div>
      </div>
      {action}
    </div>);

}

// ── SEGMENTED CONTROL ────────────────────────────────────────
function LedgerSegmented({ options, value, onChange, size = 'md' }) {
  return (
    <div style={{ display: 'inline-flex', background: 'var(--bg-inset)', borderRadius: 'var(--r-3)', padding: 4 }}>
      {options.map((opt) => {
        const label = typeof opt === 'string' ? opt : opt.label;
        const v = typeof opt === 'string' ? opt : opt.value;
        const on = v === value;
        return (
          <button key={v} onClick={() => onChange(v)} style={{
            all: 'unset', cursor: 'pointer',
            padding: size === 'sm' ? '3px 10px' : '5px 14px',
            borderRadius: 'var(--r-1)',
            background: on ? 'var(--bg-elevated)' : 'transparent',
            color: on ? 'var(--fg-strong)' : 'var(--fg-3)',
            boxShadow: on ? 'var(--shadow-xs)' : 'none',
            fontFamily: 'var(--font-body)', fontWeight: 700,
            fontSize: size === 'sm' ? 10 : 11, lineHeight: '16px', letterSpacing: '1px',
            textTransform: 'uppercase',
            transition: 'all var(--dur-fast) var(--ease-out)'
          }}>{label}</button>);

      })}
    </div>);

}

// ── CHIP ─────────────────────────────────────────────────────
function LedgerChip({ tone = 'core', children }) {
  const tones = {
    core: { bg: 'rgba(198,198,205,0.25)', fg: 'var(--fg-strong)' },
    growth: { bg: 'var(--info-100)', fg: 'var(--info-500)' },
    warning: { bg: 'var(--negative-100)', fg: 'var(--negative-700)' },
    alpha: { bg: 'var(--positive-100)', fg: 'var(--positive-700)' },
    accent: { bg: 'var(--brand-magenta-100)', fg: 'var(--brand-magenta-700)' },
    neutral: { bg: 'var(--bg-inset)', fg: 'var(--fg-2)' },
    income: { bg: 'var(--positive-100)', fg: 'var(--positive-700)' }
  }[tone] || tones.core;
  return (
    <span style={{
      display: 'inline-block', padding: '2px 8px', borderRadius: 'var(--r-1)',
      background: tones.bg, color: tones.fg,
      fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: 10, letterSpacing: '0.5px',
      textTransform: 'uppercase'
    }}>{children}</span>);

}

// ── SIDEBAR / BOTTOM NAV ─────────────────────────────────────
function LedgerSidebar({ active, onNavigate, user, onSignOut }) {
  const { isMobile } = window.useViewport();
  const items = [
  { id: 'dashboard', label: 'Painel', icon: 'layout-dashboard' },
  { id: 'transactions', label: 'Lançamentos', icon: 'receipt-text' },
  { id: 'forecasts', label: 'Previsões', icon: 'calendar-clock' },
  { id: 'add', label: 'Nova Entrada', icon: 'plus-circle' },
  { id: 'goals', label: 'Metas de Investimento', icon: 'target' },
  { id: 'balance', label: 'Saldo Inicial', icon: 'wallet' },
  { id: 'categories', label: 'Categorias', icon: 'tags' }];


  if (isMobile) {
    // Mobile: top brand bar + bottom tab nav
    return <LedgerMobileNav items={items} active={active} onNavigate={onNavigate} user={user} onSignOut={onSignOut} />;
  }

  return (
    <aside style={{
      width: 256, background: 'var(--bg)', borderRight: '1px solid var(--border-soft)',
      padding: 24, display: 'flex', flexDirection: 'column', gap: 24,
      flex: '0 0 256px', minHeight: '100vh', boxSizing: 'border-box',
      position: 'sticky', top: 0, alignSelf: 'flex-start'
    }}>
      <div style={{ display: 'flex', gap: 12, alignItems: 'center' }}>
        <div style={{
          width: 40, height: 40, borderRadius: 'var(--r-4)', background: 'var(--bg-inverse)',
          display: 'flex', alignItems: 'center', justifyContent: 'center'
        }}>
          <LedgerIcon name="wallet-minimal" size={20} color="var(--fg-on-inverse)" />
        </div>
        <div>
          <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 18, lineHeight: '24px', letterSpacing: '-0.4px', color: 'var(--fg-strong)' }}>The Ledger</div>
          <div className="meta" style={{ color: 'var(--fg-2)' }}>CURADORIA PRECISA</div>
        </div>
      </div>

      <nav style={{ display: 'flex', flexDirection: 'column', gap: 4, marginTop: 8, flex: 1 }}>
        {items.map((it) => {
          const on = it.id === active;
          return (
            <button key={it.id} onClick={() => onNavigate(it.id)} style={{
              all: 'unset', cursor: 'pointer',
              display: 'flex', gap: 12, alignItems: 'center',
              padding: '11px 14px', borderRadius: 'var(--r-2)',
              background: on ? 'var(--bg-elevated)' : 'transparent',
              boxShadow: on ? 'var(--shadow-xs)' : 'none',
              color: on ? 'var(--fg)' : 'var(--fg-2)',
              fontFamily: 'var(--font-body)', fontSize: 14, fontWeight: on ? 700 : 500,
              transition: 'all var(--dur-fast) var(--ease-out)'
            }}
            onMouseEnter={(e) => {if (!on) e.currentTarget.style.background = 'var(--bg-inset)';}}
            onMouseLeave={(e) => {if (!on) e.currentTarget.style.background = 'transparent';}}>
              
              <LedgerIcon name={it.icon} size={18} color={on ? 'var(--fg)' : 'var(--fg-2)'} />
              {it.label}
            </button>);

        })}
      </nav>

      <div style={{ borderTop: '1px solid var(--border-soft)', paddingTop: 16, display: 'flex', flexDirection: 'column', gap: 8 }}>
        <LedgerButton variant="dark" icon="plus" full onClick={() => onNavigate('add')}>
          Nova Transação
        </LedgerButton>

        {/* Botão de Configurações */}
        <button
          onClick={() => onNavigate('settings')}
          style={{
            all: 'unset', cursor: 'pointer',
            display: 'flex', gap: 10, alignItems: 'center',
            padding: '10px 14px', borderRadius: 'var(--r-2)',
            color: 'var(--fg-2)',
            fontFamily: 'var(--font-body)', fontSize: 14, fontWeight: 500,
            transition: 'all var(--dur-fast) var(--ease-out)',
          }}
          onMouseEnter={(e) => e.currentTarget.style.background = 'var(--bg-inset)'}
          onMouseLeave={(e) => e.currentTarget.style.background = 'transparent'}
        >
          <LedgerIcon name="settings" size={18} color="var(--fg-2)" />
          Configurações
        </button>

        {user &&
        <div style={{ display: 'flex', gap: 10, alignItems: 'center', padding: '10px 4px 0' }}>
            <LedgerAvatar email={user.email} size={32} />
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontFamily: 'var(--font-body)', fontWeight: 600, fontSize: 12, color: 'var(--fg)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{user.email}</div>
              <button onClick={onSignOut} style={{
              all: 'unset', cursor: 'pointer',
              fontFamily: 'var(--font-body)', fontSize: 11, color: 'var(--fg-2)',
              textDecoration: 'underline', textUnderlineOffset: 2
            }}>Sair</button>
            </div>
          </div>
        }
      </div>
    </aside>);

}

// ── CATEGORY CARD ────────────────────────────────────────────
function LedgerMobileNav({ items, active, onNavigate, user, onSignOut }) {
  // Show 5 most-used in the bottom bar; the rest live in a "More" sheet.
  const primaryIds = ['dashboard', 'transactions', 'forecasts', 'add', 'goals'];
  const primary = primaryIds.map((id) => items.find((i) => i.id === id)).filter(Boolean);
  const overflow = items.filter((i) => !primaryIds.includes(i.id));
  const [moreOpen, setMoreOpen] = React.useState(false);
  const isMore = overflow.some((i) => i.id === active);

  // Mobile brand at top + sticky bottom tab bar
  return (
    <>
      {/* Top brand bar */}
      <div style={{
        position: 'sticky', top: 0, zIndex: 30,
        background: 'var(--bg)', borderBottom: '1px solid var(--border-soft)',
        padding: '12px 16px', display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        backdropFilter: 'saturate(180%) blur(8px)'
      }}>
        <div style={{ display: 'flex', gap: 10, alignItems: 'center' }}>
          <div style={{
            width: 32, height: 32, borderRadius: 'var(--r-3)', background: 'var(--bg-inverse)',
            display: 'flex', alignItems: 'center', justifyContent: 'center'
          }}>
            <LedgerIcon name="wallet-minimal" size={16} color="var(--fg-on-inverse)" />
          </div>
          <div>
            <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 15, lineHeight: '18px', letterSpacing: '-0.2px', color: 'var(--fg-strong)' }}>The Ledger</div>
            <div className="meta" style={{ color: 'var(--fg-2)', marginTop: 1 }}>{user ? user.email.split('@')[0] : 'PRECISÃO'}</div>
          </div>
        </div>
        {user &&
        <button onClick={() => setMoreOpen(true)} style={{
          all: 'unset', cursor: 'pointer',
          width: 36, height: 36, borderRadius: 'var(--r-3)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          background: 'var(--bg-inset)', color: 'var(--fg)'
        }} aria-label="Mais opções">
            <LedgerIcon name="more-horizontal" size={18} color="currentColor" />
          </button>
        }
      </div>

      {/* Bottom tab bar */}
      <nav style={{
        position: 'fixed', bottom: 0, left: 0, right: 0, zIndex: 40,
        background: 'var(--bg-elevated)', borderTop: '1px solid var(--border)',
        padding: '8px 4px calc(8px + env(safe-area-inset-bottom)) 4px',
        display: 'flex', justifyContent: 'space-around', alignItems: 'stretch',
        boxShadow: '0 -2px 12px rgba(15,23,42,0.06)'
      }}>
        {primary.map((it) => {
          const on = it.id === active;
          const isCenter = it.id === 'add';
          if (isCenter) {
            return (
              <button key={it.id} onClick={() => onNavigate(it.id)} aria-label={it.label} style={{
                all: 'unset', cursor: 'pointer',
                position: 'relative',
                width: 56, height: 56, marginTop: -16, alignSelf: 'center',
                borderRadius: '50%', background: 'var(--brand-magenta)', color: 'var(--fg-on-accent)',
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                boxShadow: '0 8px 20px rgba(176,3,105,0.35)',
                flex: '0 0 56px'
              }}>
                <LedgerIcon name="plus" size={26} color="currentColor" />
              </button>);

          }
          return (
            <button key={it.id} onClick={() => onNavigate(it.id)} aria-label={it.label} style={{
              all: 'unset', cursor: 'pointer',
              flex: 1, minWidth: 0,
              display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 2,
              padding: '6px 4px', borderRadius: 'var(--r-2)',
              color: on ? 'var(--brand-magenta)' : 'var(--fg-2)',
              fontFamily: 'var(--font-body)', fontWeight: on ? 700 : 500, fontSize: 10
            }}>
              <LedgerIcon name={it.icon} size={20} color="currentColor" />
              <span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth: '100%' }}>
                {{ dashboard: 'Painel', transactions: 'Lançamentos', forecasts: 'Previsões', goals: 'Metas' }[it.id] || it.label}
              </span>
            </button>);

        })}
      </nav>

      {/* "More" overflow sheet */}
      {moreOpen &&
      <div onClick={() => setMoreOpen(false)} style={{
        position: 'fixed', inset: 0, background: 'rgba(15,23,42,0.55)', backdropFilter: 'blur(4px)',
        zIndex: 100, display: 'flex', alignItems: 'flex-end'
      }}>
          <div onClick={(e) => e.stopPropagation()} style={{
          width: '100%', background: 'var(--bg-elevated)',
          borderTopLeftRadius: 'var(--r-5)', borderTopRightRadius: 'var(--r-5)',
          padding: '20px 16px calc(24px + env(safe-area-inset-bottom))',
          display: 'flex', flexDirection: 'column', gap: 12
        }}>
            <div style={{ width: 40, height: 4, background: 'var(--border)', borderRadius: 999, margin: '0 auto 8px' }} />
            {user &&
          <div style={{ display: 'flex', gap: 12, alignItems: 'center', padding: '4px 8px 12px', borderBottom: '1px solid var(--border-soft)' }}>
                <LedgerAvatar email={user.email} size={40} />
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: 14, color: 'var(--fg-strong)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{user.email}</div>
                  <div className="meta" style={{ color: 'var(--fg-3)', marginTop: 2 }}>CONECTADO</div>
                </div>
              </div>
          }
            {overflow.map((it) => {
            const on = it.id === active;
            return (
              <button key={it.id} onClick={() => {onNavigate(it.id);setMoreOpen(false);}} style={{
                all: 'unset', cursor: 'pointer',
                display: 'flex', alignItems: 'center', gap: 14,
                padding: '14px 12px', borderRadius: 'var(--r-3)',
                background: on ? 'var(--bg-muted)' : 'transparent',
                color: 'var(--fg-strong)',
                fontFamily: 'var(--font-body)', fontSize: 15, fontWeight: on ? 700 : 500
              }}>
                  <LedgerIcon name={it.icon} size={20} color="var(--fg)" />
                  <span style={{ flex: 1 }}>{it.label}</span>
                  <LedgerIcon name="chevron-right" size={16} color="var(--fg-3)" />
                </button>);

          })}
            <button onClick={() => { setMoreOpen(false); onNavigate('settings'); }} style={{
            all: 'unset', cursor: 'pointer',
            display: 'flex', alignItems: 'center', gap: 14,
            padding: '14px 12px', borderRadius: 'var(--r-3)',
            color: 'var(--fg-strong)', fontFamily: 'var(--font-body)', fontSize: 15, fontWeight: 500
          }}>
              <LedgerIcon name="settings" size={20} color="var(--fg)" />
              <span style={{ flex: 1 }}>Configurações</span>
              <LedgerIcon name="chevron-right" size={16} color="var(--fg-3)" />
            </button>
            <button onClick={() => {setMoreOpen(false);onSignOut();}} style={{
            all: 'unset', cursor: 'pointer',
            display: 'flex', alignItems: 'center', gap: 14,
            padding: '14px 12px', borderRadius: 'var(--r-3)',
            color: 'var(--brand-magenta)', fontFamily: 'var(--font-body)', fontSize: 15, fontWeight: 700,
            marginTop: 4, borderTop: '1px solid var(--border-soft)', paddingTop: 16
          }}>
              <LedgerIcon name="log-out" size={20} color="currentColor" />
              <span style={{ flex: 1 }}>Sair</span>
            </button>
          </div>
        </div>
      }
    </>);

}

// ── CATEGORY CARD ────────────────────────────────────────────
function LedgerCategoryCard({ category, value, delta, onClick }) {
  const T = window.LedgerStore;
  const [hover, setHover] = React.useState(false);
  return (
    <div
      onClick={onClick}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        flex: 1, minWidth: 0,
        background: 'var(--bg-muted)', borderRadius: 'var(--r-4)', padding: 22,
        display: 'flex', flexDirection: 'column', boxSizing: 'border-box', minHeight: 200,
        cursor: onClick ? 'pointer' : 'default',
        transition: 'transform var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out)',
        transform: hover && onClick ? 'translateY(-1px)' : 'none',
        boxShadow: hover && onClick ? 'var(--shadow-sm)' : 'none',
        border: '1px solid var(--border-soft)'
      }}>
      
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start' }}>
        <div style={{ width: 38, height: 34, background: 'var(--bg-elevated)', borderRadius: 'var(--r-2)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
          <LedgerIcon name={category.icon} size={18} color="var(--fg-strong)" />
        </div>
        <LedgerChip tone={category.chipTone}>{category.chip}</LedgerChip>
      </div>
      <div style={{ marginTop: 24 }}>
        <div style={{ fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: 17, color: 'var(--fg-strong)' }}>{category.name}</div>
        <div style={{ fontFamily: 'var(--font-body)', fontSize: 12, color: 'var(--fg-2)', marginTop: 2, lineHeight: '16px' }}>
          {category.subs.slice(0, 3).map((s) => s.name).join(' · ')}
        </div>
      </div>
      <div style={{ flex: 1 }} />
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end' }}>
        <div className="num" style={{ fontSize: 20, lineHeight: '28px', letterSpacing: '-0.3px' }}>{T.fmtMoney(value)}</div>
        {delta &&
        <div style={{
          fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: 11,
          color: delta.tone === 'positive' ? 'var(--fg-positive)' :
          delta.tone === 'negative' ? 'var(--brand-magenta)' :
          'var(--fg-2)'
        }}>{delta.label}</div>
        }
      </div>
    </div>);

}

window.LedgerButton = LedgerButton;
window.LedgerPageHeader = LedgerPageHeader;
window.LedgerAvatar = LedgerAvatar;
window.LedgerKpi = LedgerKpi;
window.LedgerAlert = LedgerAlert;
window.LedgerSegmented = LedgerSegmented;
window.LedgerChip = LedgerChip;
window.LedgerSidebar = LedgerSidebar;
window.LedgerMobileNav = LedgerMobileNav;
window.LedgerCategoryCard = LedgerCategoryCard;
