// Tax Bridge — shared UI primitives
const T = window.TB;

// ─── Logo ────────────────────────────────────────────────
function Logo({ size = 22, color = T.ink, accent = T.mint, wordmark = true }) {
  return (
    <div style={{ display: 'inline-flex', alignItems: 'center', gap: 9 }}>
      <svg width={size * 1.35} height={size} viewBox="0 0 40 30" fill="none">
        {/* two arcs — bridge */}
        <path d="M2 22 Q 11 8, 20 22" stroke={color} strokeWidth="2.6" strokeLinecap="round" fill="none"/>
        <path d="M20 22 Q 29 8, 38 22" stroke={accent} strokeWidth="2.6" strokeLinecap="round" fill="none"/>
        <circle cx="20" cy="22" r="2.2" fill={color}/>
        <line x1="2" y1="26" x2="38" y2="26" stroke={color} strokeWidth="1.6" strokeLinecap="round" opacity="0.4"/>
      </svg>
      {wordmark && (
        <span style={{
          fontFamily: T.font, fontWeight: 600, fontSize: size * 0.78,
          color, letterSpacing: -0.4,
        }}>
          tax<span style={{ color: T.muted, fontWeight: 400 }}>·</span>bridge
        </span>
      )}
    </div>
  );
}

// ─── Button ────────────────────────────────────────────────
function Button({ children, variant = 'primary', size = 'md', onClick, icon, iconRight, full, style = {}, disabled }) {
  const sizes = {
    sm: { p: '8px 14px', fs: 13, h: 36, r: 10 },
    md: { p: '11px 18px', fs: 14.5, h: 44, r: 12 },
    lg: { p: '15px 24px', fs: 16, h: 54, r: 14 },
  }[size];
  const variants = {
    primary: { bg: T.ink, color: '#fff', bd: T.ink, hover: T.inkDeep },
    mint:    { bg: T.mint, color: T.ink, bd: T.mint, hover: '#2EBE8C' },
    ghost:   { bg: 'transparent', color: T.ink, bd: T.border, hover: T.surface },
    outline: { bg: '#fff', color: T.ink, bd: T.border, hover: T.surface },
    link:    { bg: 'transparent', color: T.ink, bd: 'transparent', hover: 'transparent' },
  }[variant];
  const [hover, setHover] = React.useState(false);
  return (
    <button
      onClick={onClick}
      disabled={disabled}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 8,
        padding: sizes.p, fontSize: sizes.fs, height: sizes.h,
        fontFamily: T.font, fontWeight: 500, letterSpacing: -0.1,
        background: hover && !disabled ? variants.hover : variants.bg,
        color: variants.color,
        border: `1px solid ${variants.bd}`,
        borderRadius: sizes.r,
        cursor: disabled ? 'not-allowed' : 'pointer',
        opacity: disabled ? 0.5 : 1,
        width: full ? '100%' : undefined,
        transition: 'all 140ms ease',
        ...style,
      }}
    >
      {icon}
      <span>{children}</span>
      {iconRight}
    </button>
  );
}

// ─── Card ────────────────────────────────────────────────
function Card({ children, style = {}, pad = 24, soft = false, hover = false, onClick }) {
  const [h, setH] = React.useState(false);
  return (
    <div
      onClick={onClick}
      onMouseEnter={() => hover && setH(true)}
      onMouseLeave={() => hover && setH(false)}
      style={{
        background: soft ? T.surface : '#fff',
        border: `1px solid ${soft ? T.borderSoft : T.border}`,
        borderRadius: 18,
        padding: pad,
        transition: 'transform 160ms ease, box-shadow 160ms ease',
        transform: h ? 'translateY(-2px)' : 'none',
        boxShadow: h ? '0 12px 32px rgba(11,27,59,0.10)' : 'none',
        cursor: onClick ? 'pointer' : 'default',
        ...style,
      }}
    >
      {children}
    </div>
  );
}

// ─── Badge ────────────────────────────────────────────────
function Badge({ children, tone = 'neutral', icon, style = {} }) {
  const tones = {
    neutral: { bg: T.surface, color: T.ink2, bd: T.border },
    mint:    { bg: T.mintSoft, color: T.mintInk, bd: 'transparent' },
    blue:    { bg: T.blueSoft, color: '#2B5BD7', bd: 'transparent' },
    amber:   { bg: T.amberSoft, color: '#8A5A0F', bd: 'transparent' },
    rose:    { bg: T.roseSoft, color: '#A23541', bd: 'transparent' },
    dark:    { bg: T.ink, color: '#fff', bd: T.ink },
    glass:   { bg: 'rgba(255,255,255,0.08)', color: '#fff', bd: 'rgba(255,255,255,0.15)' },
  }[tone];
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: '4px 10px', borderRadius: 999,
      background: tones.bg, color: tones.color,
      border: `1px solid ${tones.bd}`,
      fontFamily: T.font, fontSize: 12, fontWeight: 500,
      letterSpacing: -0.1,
      ...style,
    }}>
      {icon}
      {children}
    </span>
  );
}

// ─── ProgressBar ────────────────────────────────────────────
function ProgressBar({ value, max = 100, tone = 'mint', height = 6, showPct = false }) {
  const pct = Math.min(100, Math.max(0, (value / max) * 100));
  const colors = { mint: T.mint, blue: T.blue, amber: T.amber };
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
      <div style={{
        flex: 1, height, borderRadius: height,
        background: T.borderSoft, overflow: 'hidden',
      }}>
        <div style={{
          width: `${pct}%`, height: '100%',
          background: colors[tone],
          borderRadius: height,
          transition: 'width 380ms cubic-bezier(.2,.7,.2,1)',
        }} />
      </div>
      {showPct && (
        <span style={{
          fontFamily: T.mono, fontSize: 11, color: T.muted,
          minWidth: 30, textAlign: 'right',
        }}>{Math.round(pct)}%</span>
      )}
    </div>
  );
}

// ─── Field ────────────────────────────────────────────────
function Field({ label, type = 'text', value, onChange, placeholder, icon, hint, required }) {
  const [focus, setFocus] = React.useState(false);
  return (
    <label style={{ display: 'block' }}>
      {label && (
        <div style={{
          fontFamily: T.font, fontSize: 13, fontWeight: 500,
          color: T.ink2, marginBottom: 6,
        }}>
          {label} {required && <span style={{ color: T.rose }}>*</span>}
        </div>
      )}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 10,
        padding: '0 14px', height: 46,
        background: '#fff',
        border: `1.5px solid ${focus ? T.ink : T.border}`,
        borderRadius: 12,
        transition: 'border-color 120ms ease',
      }}>
        {icon}
        <input
          type={type} value={value} onChange={onChange}
          placeholder={placeholder}
          onFocus={() => setFocus(true)}
          onBlur={() => setFocus(false)}
          style={{
            flex: 1, border: 0, outline: 'none', background: 'transparent',
            fontFamily: T.font, fontSize: 15, color: T.ink2,
            padding: 0, minWidth: 0,
          }}
        />
      </div>
      {hint && (
        <div style={{ fontFamily: T.font, fontSize: 12, color: T.muted, marginTop: 6 }}>
          {hint}
        </div>
      )}
    </label>
  );
}

// ─── OptionCard (for assessment) ─────────────────────────
function OptionCard({ children, selected, onClick, icon, sub, multi }) {
  return (
    <button
      onClick={onClick}
      style={{
        display: 'flex', alignItems: 'center', gap: 14, textAlign: 'left',
        padding: '14px 16px', width: '100%',
        background: selected ? T.ink : '#fff',
        color: selected ? '#fff' : T.ink2,
        border: `1.5px solid ${selected ? T.ink : T.border}`,
        borderRadius: 14,
        cursor: 'pointer',
        fontFamily: T.font, fontSize: 15, fontWeight: 500,
        transition: 'all 140ms ease',
      }}
      onMouseEnter={(e) => { if (!selected) e.currentTarget.style.borderColor = T.ink3; }}
      onMouseLeave={(e) => { if (!selected) e.currentTarget.style.borderColor = T.border; }}
    >
      {icon && (
        <div style={{
          width: 36, height: 36, borderRadius: 10,
          background: selected ? 'rgba(255,255,255,0.12)' : T.surface,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          flexShrink: 0,
        }}>{icon}</div>
      )}
      <div style={{ flex: 1, minWidth: 0 }}>
        <div>{children}</div>
        {sub && (
          <div style={{
            fontSize: 12.5, fontWeight: 400, marginTop: 2,
            color: selected ? 'rgba(255,255,255,0.65)' : T.muted,
          }}>{sub}</div>
        )}
      </div>
      <div style={{
        width: 22, height: 22, borderRadius: multi ? 6 : 999,
        border: `1.5px solid ${selected ? '#fff' : T.border}`,
        background: selected ? T.mint : 'transparent',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        flexShrink: 0,
      }}>
        {selected && (
          <svg width="12" height="12" viewBox="0 0 12 12">
            <path d="M2.5 6L5 8.5L9.5 3.5" stroke={T.ink} strokeWidth="2.2" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
        )}
      </div>
    </button>
  );
}

// ─── Tiny icons (line, 18px default) ─────────────────────
const Icon = {
  arrow:   <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10m-4-4l4 4-4 4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  arrowL:  <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M13 8H3m4-4L3 8l4 4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  check:   <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M2.5 7L6 10.5L11.5 3.5" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  lock:    <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><rect x="2.5" y="6" width="9" height="6" rx="1.4" stroke="currentColor" strokeWidth="1.4" fill="none"/><path d="M4.5 6V4a2.5 2.5 0 015 0v2" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinecap="round"/></svg>,
  shield:  <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M7 1.5L2 3.5v3c0 3 2 5.5 5 6 3-.5 5-3 5-6v-3l-5-2z" stroke="currentColor" strokeWidth="1.3" fill="none" strokeLinejoin="round"/><path d="M4.8 7.2L6.3 8.7L9.5 5.5" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  spark:   <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M7 1.5l1.4 3.6 3.6 1.4-3.6 1.4L7 11.5l-1.4-3.6L2 6.5l3.6-1.4L7 1.5z" fill="currentColor"/></svg>,
  doc:     <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M3 1.5h5l3 3v8a1 1 0 01-1 1H3a1 1 0 01-1-1V2.5a1 1 0 011-1z" stroke="currentColor" strokeWidth="1.3" fill="none" strokeLinejoin="round"/><path d="M8 1.5v3h3M4.5 7.5h5M4.5 10h3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  euro:    <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M11 3.5C10 2.5 8.5 2 7 2c-2.5 0-4.5 2-5 5m5 5c1.5 0 3-.5 4-1.5M2 6h6.5M2 8h6" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinecap="round"/></svg>,
  user:    <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><circle cx="7" cy="4.5" r="2.2" stroke="currentColor" strokeWidth="1.4" fill="none"/><path d="M2.5 12c.5-2.5 2.4-4 4.5-4s4 1.5 4.5 4" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinecap="round"/></svg>,
  briefcase: <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><rect x="1.5" y="4" width="11" height="8" rx="1" stroke="currentColor" strokeWidth="1.4" fill="none"/><path d="M5 4V2.5a1 1 0 011-1h2a1 1 0 011 1V4" stroke="currentColor" strokeWidth="1.4" fill="none"/></svg>,
  globe:   <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><circle cx="7" cy="7" r="5.5" stroke="currentColor" strokeWidth="1.3" fill="none"/><path d="M1.5 7h11M7 1.5c2 1.8 2 9.2 0 11M7 1.5c-2 1.8-2 9.2 0 11" stroke="currentColor" strokeWidth="1.3" fill="none"/></svg>,
  chart:   <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M2 12h10M3 9V7M6 9V4M9 9V6M12 9V3" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" fill="none"/></svg>,
  coins:   <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><ellipse cx="5" cy="4" rx="3.5" ry="1.5" stroke="currentColor" strokeWidth="1.3" fill="none"/><path d="M1.5 4v3c0 .8 1.6 1.5 3.5 1.5M8.5 7.5c0 .8-1.6 1.5-3.5 1.5M8.5 4.5c.5 2 .5 5 0 7" stroke="currentColor" strokeWidth="1.3" fill="none"/><ellipse cx="9" cy="10" rx="3.5" ry="1.5" stroke="currentColor" strokeWidth="1.3" fill="none"/><path d="M5.5 10v1c0 .8 1.6 1.5 3.5 1.5s3.5-.7 3.5-1.5v-1" stroke="currentColor" strokeWidth="1.3" fill="none"/></svg>,
  home2:   <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M2 6.5L7 2l5 4.5V12a1 1 0 01-1 1H3a1 1 0 01-1-1V6.5z" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinejoin="round"/></svg>,
  upload:  <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M7 2v7m0-7L4.5 4.5M7 2l2.5 2.5M2.5 11.5h9" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" fill="none"/></svg>,
  bell:    <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M3 10.5h8L10 9V6a3 3 0 00-6 0v3l-1 1.5zM6 12.5a1 1 0 002 0" stroke="currentColor" strokeWidth="1.3" fill="none" strokeLinejoin="round"/></svg>,
  search:  <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><circle cx="6.5" cy="6.5" r="3.5" stroke="currentColor" strokeWidth="1.4" fill="none"/><path d="M9 9l3 3" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>,
  filter:  <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M2 3h10l-4 5v4l-2-1V8L2 3z" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinejoin="round"/></svg>,
  flag:    <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M3 13V2m0 0h7l-2 3 2 3H3" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  clock:   <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><circle cx="7" cy="7" r="5.5" stroke="currentColor" strokeWidth="1.3" fill="none"/><path d="M7 4v3.2L9 8.5" stroke="currentColor" strokeWidth="1.4" fill="none" strokeLinecap="round"/></svg>,
};

Object.assign(window, { Logo, Button, Card, Badge, ProgressBar, Field, OptionCard, Icon });
