// ui.jsx — shared primitives & icons for Kolhida prototype

// Apply persisted theme as early as possible
(function () {
  try {
    const saved = localStorage.getItem('kolhida-theme');
    if (saved === 'dark' || saved === 'light') {
      document.documentElement.setAttribute('data-theme', saved);
    }
  } catch (e) {}
})();

// ────────────────────────────────────────────────────────────
// Brand wordmark
// ────────────────────────────────────────────────────────────
function Wordmark({ size = 18, sub = false }) {
  return (
    <img
      src="assets/kolhida-logo.png"
      alt="Kolhida — Застрахователен Брокер"
      className="brand-logo"
      style={{ height: Math.round(size * 1.5), width: 'auto', display: 'block' }}
    />
  );
}

// ────────────────────────────────────────────────────────────
// Icon set — line-based, 24×24 viewBox
// ────────────────────────────────────────────────────────────
const I = {
  back: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M19 12H5"/><path d="M12 19l-7-7 7-7"/>
    </svg>
  ),
  close: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M18 6L6 18"/><path d="M6 6l12 12"/>
    </svg>
  ),
  bell: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"/>
      <path d="M10.3 21a1.94 1.94 0 0 0 3.4 0"/>
    </svg>
  ),
  orders: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M21 8l-9 4.5L3 8l9-4.5L21 8z"/>
      <path d="M3 8v8l9 4.5 9-4.5V8"/>
      <path d="M12 12.5V21"/>
    </svg>
  ),
  plus: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 5v14M5 12h14"/>
    </svg>
  ),
  chevron: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M9 6l6 6-6 6"/>
    </svg>
  ),
  check: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M4 12l5 5L20 6"/>
    </svg>
  ),
  shield: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 2L4 5v6c0 5 3.5 9.5 8 11 4.5-1.5 8-6 8-11V5l-8-3z"/>
    </svg>
  ),
  shieldCheck: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 2L4 5v6c0 5 3.5 9.5 8 11 4.5-1.5 8-6 8-11V5l-8-3z"/>
      <path d="M9 12l2 2 4-4"/>
    </svg>
  ),
  car: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 13l1.5-4.5A2 2 0 0 1 6.4 7h11.2a2 2 0 0 1 1.9 1.5L21 13"/>
      <path d="M3 13h18v5a1 1 0 0 1-1 1h-2a1 1 0 0 1-1-1v-1H7v1a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-5z"/>
      <circle cx="7.5" cy="16.5" r="1.2"/>
      <circle cx="16.5" cy="16.5" r="1.2"/>
    </svg>
  ),
  doc: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8l-5-5z"/>
      <path d="M14 3v5h5"/>
      <path d="M9 14h6M9 17h4"/>
    </svg>
  ),
  pin: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 22s7-6 7-12a7 7 0 1 0-14 0c0 6 7 12 7 12z"/>
      <circle cx="12" cy="10" r="2.5"/>
    </svg>
  ),
  truck: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 7h11v9H3z"/>
      <path d="M14 10h4l3 3v3h-7z"/>
      <circle cx="7" cy="18" r="1.6"/>
      <circle cx="17.5" cy="18" r="1.6"/>
    </svg>
  ),
  building: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="4" y="3" width="16" height="18" rx="2"/>
      <path d="M9 7h2M13 7h2M9 11h2M13 11h2M9 15h2M13 15h2"/>
    </svg>
  ),
  user: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="8" r="4"/>
      <path d="M4 21c0-4 4-7 8-7s8 3 8 7"/>
    </svg>
  ),
  lock: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="4" y="11" width="16" height="10" rx="2"/>
      <path d="M8 11V7a4 4 0 0 1 8 0v4"/>
    </svg>
  ),
  card: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="3" y="6" width="18" height="13" rx="2"/>
      <path d="M3 10h18"/>
    </svg>
  ),
  globe: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="12" r="9"/>
      <path d="M3 12h18"/>
      <path d="M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18"/>
    </svg>
  ),
  help: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="12" r="9"/>
      <path d="M9 9.5a3 3 0 0 1 6 0c0 1.7-2 2-2 3.5"/>
      <path d="M12 17h.01"/>
    </svg>
  ),
  mail: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="3" y="5" width="18" height="14" rx="2"/>
      <path d="M3 7l9 6 9-6"/>
    </svg>
  ),
  logout: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>
      <path d="M16 17l5-5-5-5M21 12H9"/>
    </svg>
  ),
  arrowRight: (p) => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M5 12h14M13 5l7 7-7 7"/>
    </svg>
  ),
  package: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 7l9 4 9-4-9-4-9 4z"/>
      <path d="M3 7v10l9 4 9-4V7"/>
      <path d="M12 11v10"/>
    </svg>
  ),
  spark: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8"/>
    </svg>
  ),
  euro: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M18 7a7 7 0 1 0 0 10"/>
      <path d="M4 10h9M4 14h9"/>
    </svg>
  ),
  calendar: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="3" y="5" width="18" height="16" rx="2"/>
      <path d="M3 9h18M8 3v4M16 3v4"/>
    </svg>
  ),
  file: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/>
      <path d="M14 3v5h5M9 13h6M9 17h6"/>
    </svg>
  ),
  sticker: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M20 4H4v16h10l6-6V4z"/>
      <path d="M14 20v-6h6"/>
    </svg>
  ),
  scan: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 7V5a2 2 0 0 1 2-2h2M17 3h2a2 2 0 0 1 2 2v2M21 17v2a2 2 0 0 1-2 2h-2M7 21H5a2 2 0 0 1-2-2v-2"/>
      <path d="M3 12h18"/>
    </svg>
  ),
  phone: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.8 19.8 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.8 19.8 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.36 1.9.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.34 1.85.57 2.81.7A2 2 0 0 1 22 16.92z"/>
    </svg>
  ),
  chat: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M21 11.5a8.38 8.38 0 0 1-8.5 8.5 8.5 8.5 0 0 1-3.8-.9L3 21l1.9-5.7A8.5 8.5 0 0 1 4 11.5 8.38 8.38 0 0 1 12.5 3 8.38 8.38 0 0 1 21 11.5z"/>
    </svg>
  ),
  sun: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="12" r="4.2"/>
      <path d="M12 2v2.5M12 19.5V22M4.2 4.2l1.8 1.8M18 18l1.8 1.8M2 12h2.5M19.5 12H22M4.2 19.8l1.8-1.8M18 6l1.8-1.8"/>
    </svg>
  ),
  moon: (p) => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"/>
    </svg>
  ),
  home: (p) => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 10.5L12 3l9 7.5"/>
      <path d="M5 9.5V20a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V9.5"/>
    </svg>
  ),
};

// ────────────────────────────────────────────────────────────
// OAuth provider glyphs (original line/silhouette renditions —
// not the licensed brand marks). Color the call-out via prop.
// ────────────────────────────────────────────────────────────
function OAuthGlyph({ provider }) {
  if (provider === 'google') {
    return (
      <svg width="20" height="20" viewBox="0 0 24 24">
        <path d="M22 12.2c0-.8-.07-1.5-.2-2.2H12v4.18h5.66a4.86 4.86 0 0 1-2.1 3.18v2.65h3.4c1.99-1.84 3.04-4.55 3.04-7.81z" fill="#4285F4"/>
        <path d="M12 22c2.84 0 5.22-.94 6.96-2.55l-3.4-2.65c-.94.63-2.15 1-3.56 1-2.73 0-5.05-1.85-5.88-4.33H2.6v2.72A10 10 0 0 0 12 22z" fill="#34A853"/>
        <path d="M6.12 13.47A6.01 6.01 0 0 1 5.8 12c0-.51.09-1 .24-1.47V7.81H2.6A10 10 0 0 0 2 12c0 1.61.38 3.13 1.06 4.47l3.06-3z" fill="#FBBC05"/>
        <path d="M12 6.2c1.54 0 2.93.53 4.02 1.57l3.02-3.02C17.22 3.18 14.84 2 12 2A10 10 0 0 0 2.6 7.81l3.52 2.72C6.95 8.05 9.27 6.2 12 6.2z" fill="#EA4335"/>
      </svg>
    );
  }
  if (provider === 'apple') {
    return (
      <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
        <path d="M17.05 12.3c-.03-2.78 2.27-4.12 2.37-4.18-1.3-1.9-3.3-2.15-4.01-2.18-1.71-.17-3.33.99-4.2.99-.86 0-2.2-.97-3.61-.94-1.86.03-3.57 1.08-4.52 2.74-1.92 3.34-.49 8.27 1.39 10.97.92 1.32 2.01 2.81 3.44 2.76 1.39-.06 1.91-.9 3.59-.9 1.67 0 2.14.9 3.6.86 1.49-.03 2.43-1.35 3.34-2.68 1.05-1.54 1.49-3.04 1.51-3.12-.03-.01-2.9-1.12-2.92-4.43z"/>
        <path d="M14.6 4.5c.76-.92 1.27-2.19 1.13-3.45-1.09.04-2.41.73-3.19 1.64-.7.81-1.32 2.1-1.15 3.34 1.21.09 2.45-.62 3.21-1.53z"/>
      </svg>
    );
  }
  if (provider === 'meta') {
    return (
      <svg width="22" height="20" viewBox="0 0 24 22" fill="none">
        <path d="M3 11.2C3 6.4 5.6 3 9.2 3c2 0 3.5 1 5 3.4 1.4-2.4 2.9-3.4 4.7-3.4 3.3 0 5.6 3 5.6 7.4 0 4.2-2 7.4-5.1 7.4-2.1 0-3.4-1-5.5-4.5-1.8-3-2.6-4-3.7-4-1.2 0-2.1 1.1-2.1 3.3 0 2 .8 3.3 2 3.3.5 0 1-.1 1.5-.4l-.7 2.3c-.6.2-1.1.3-1.8.3C5.1 18.2 3 15.4 3 11.2z" fill="#0866FF"/>
      </svg>
    );
  }
  return null;
}

// ────────────────────────────────────────────────────────────
// 3D Tile icons (CSS + SVG combo) for the hub
// ────────────────────────────────────────────────────────────
function Tile3D({ kind, active }) {
  // Each variant: a geometric "3D" composition using gradients + soft shadow.
  const base = {
    car:   { hue: 200, tone: '#00B0F0' },
    house: { hue: 28,  tone: '#F2B441' },
    person:{ hue: 158, tone: '#3DBE8C' },
  }[kind];

  const lit = active ? '#ffffff' : base.tone;
  const lit2 = active ? 'rgba(255,255,255,.7)' : '#ffffff';
  const shadow = active ? 'rgba(0,0,0,.5)' : 'rgba(10,14,26,.18)';

  if (kind === 'car') {
    return (
      <svg width="74" height="74" viewBox="0 0 100 100" style={{ filter: `drop-shadow(0 8px 10px ${shadow})` }}>
        <defs>
          <linearGradient id={`carBody-${active?1:0}`} x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor={lit2}/>
            <stop offset="100%" stopColor={lit}/>
          </linearGradient>
          <linearGradient id={`carRoof-${active?1:0}`} x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor={active ? 'rgba(255,255,255,.45)' : '#ffffff'}/>
            <stop offset="100%" stopColor={active ? 'rgba(255,255,255,.2)' : '#E7ECFF'}/>
          </linearGradient>
        </defs>
        {/* roof */}
        <path d="M28 50 L40 30 Q42 27 46 27 L62 27 Q66 27 70 30 L84 50 Z" fill={`url(#carRoof-${active?1:0})`} opacity={active ? .85 : 1}/>
        {/* body */}
        <path d="M14 64 Q14 56 22 54 L80 54 Q92 56 94 64 L94 72 Q94 76 90 76 L84 76 Q82 80 78 80 Q74 80 72 76 L36 76 Q34 80 30 80 Q26 80 24 76 L18 76 Q14 76 14 72 Z" fill={`url(#carBody-${active?1:0})`}/>
        {/* window highlight */}
        <path d="M40 36 L60 36 L66 49 L34 49 Z" fill="rgba(255,255,255,.4)" opacity={active ? .25 : .55}/>
        {/* wheels */}
        <circle cx="30" cy="76" r="6" fill={active ? '#0A0E1A' : '#0A0E1A'} opacity=".75"/>
        <circle cx="30" cy="76" r="2.5" fill={active ? 'rgba(255,255,255,.5)' : '#5B6172'}/>
        <circle cx="78" cy="76" r="6" fill={active ? '#0A0E1A' : '#0A0E1A'} opacity=".75"/>
        <circle cx="78" cy="76" r="2.5" fill={active ? 'rgba(255,255,255,.5)' : '#5B6172'}/>
        {/* underbelly shadow */}
        <ellipse cx="54" cy="85" rx="38" ry="3" fill="rgba(10,14,26,.16)" opacity={active ? .3 : 1}/>
      </svg>
    );
  }
  if (kind === 'house') {
    return (
      <svg width="74" height="74" viewBox="0 0 100 100" style={{ filter: `drop-shadow(0 8px 10px ${shadow})` }}>
        <defs>
          <linearGradient id={`hRoof-${active?1:0}`} x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor={active ? '#ffffff' : '#F2B441'}/>
            <stop offset="100%" stopColor={active ? 'rgba(255,255,255,.6)' : '#D58E0E'}/>
          </linearGradient>
          <linearGradient id={`hBody-${active?1:0}`} x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor={active ? 'rgba(255,255,255,.6)' : '#FFFFFF'}/>
            <stop offset="100%" stopColor={active ? 'rgba(255,255,255,.3)' : '#F5EAD2'}/>
          </linearGradient>
        </defs>
        {/* roof */}
        <path d="M50 18 L18 50 L26 50 L50 28 L74 50 L82 50 Z" fill={`url(#hRoof-${active?1:0})`}/>
        {/* body */}
        <path d="M26 48 L74 48 L74 82 L26 82 Z" fill={`url(#hBody-${active?1:0})`} stroke={active ? 'rgba(255,255,255,.3)' : '#E7D7B0'} strokeWidth="1"/>
        {/* door */}
        <rect x="42" y="62" width="16" height="20" rx="2" fill={active ? 'rgba(0,0,0,.4)' : '#2A2F3D'}/>
        <circle cx="54" cy="73" r="1" fill={active ? 'rgba(255,255,255,.5)' : '#F2B441'}/>
        {/* window */}
        <rect x="32" y="56" width="8" height="8" rx="1" fill={active ? 'rgba(255,255,255,.4)' : '#9DC4FF'}/>
        <rect x="60" y="56" width="8" height="8" rx="1" fill={active ? 'rgba(255,255,255,.4)' : '#9DC4FF'}/>
        {/* underbelly shadow */}
        <ellipse cx="50" cy="86" rx="32" ry="3" fill="rgba(10,14,26,.16)" opacity={active ? .3 : 1}/>
      </svg>
    );
  }
  if (kind === 'person') {
    return (
      <svg width="74" height="74" viewBox="0 0 100 100" style={{ filter: `drop-shadow(0 8px 10px ${shadow})` }}>
        <defs>
          <linearGradient id={`pBody-${active?1:0}`} x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor={active ? '#ffffff' : '#3DBE8C'}/>
            <stop offset="100%" stopColor={active ? 'rgba(255,255,255,.6)' : '#1B8A60'}/>
          </linearGradient>
          <linearGradient id={`pHead-${active?1:0}`} x1="0" x2="0" y1="0" y2="1">
            <stop offset="0%" stopColor={active ? 'rgba(255,255,255,.85)' : '#FFD7B5'}/>
            <stop offset="100%" stopColor={active ? 'rgba(255,255,255,.5)' : '#EAB68A'}/>
          </linearGradient>
        </defs>
        {/* shoulders */}
        <path d="M18 80 Q22 58 50 58 Q78 58 82 80 L82 84 L18 84 Z" fill={`url(#pBody-${active?1:0})`}/>
        {/* head */}
        <circle cx="50" cy="42" r="16" fill={`url(#pHead-${active?1:0})`}/>
        {/* hair / cap */}
        <path d="M34 38 Q40 26 50 26 Q60 26 66 38 L66 42 Q60 36 50 36 Q40 36 34 42 Z" fill={active ? 'rgba(0,0,0,.4)' : '#3B2A1F'}/>
        {/* underbelly shadow */}
        <ellipse cx="50" cy="88" rx="30" ry="3" fill="rgba(10,14,26,.16)" opacity={active ? .3 : 1}/>
      </svg>
    );
  }
  return null;
}

// ────────────────────────────────────────────────────────────
// Language chip + dropdown (reused on login & top bar)
// ────────────────────────────────────────────────────────────
function LangChip({ lang, onChange }) {
  const [open, setOpen] = React.useState(false);
  const label = lang === 'bg' ? 'БГ' : 'EN';
  return (
    <div className="lang-wrap">
      <button className="lang-chip" onClick={() => setOpen(o => !o)} aria-label="Език">
        {label}
        <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" style={{ transform: open ? 'rotate(180deg)' : 'none', transition:'transform .2s' }}><path d="M6 9l6 6 6-6"/></svg>
      </button>
      {open && (
        <>
          <div className="lang-scrim" onClick={() => setOpen(false)}></div>
          <div className="lang-menu">
            <button className={lang === 'bg' ? 'active' : ''} onClick={() => { onChange('bg'); setOpen(false); }}>
              <span>Български</span>{lang === 'bg' && I.check({ width:14, height:14 })}
            </button>
            <button className={lang === 'en' ? 'active' : ''} onClick={() => { onChange('en'); setOpen(false); }}>
              <span>English</span>{lang === 'en' && I.check({ width:14, height:14 })}
            </button>
          </div>
        </>
      )}
    </div>
  );
}

// ────────────────────────────────────────────────────────────
// Top bar (used inside app shell)
// ────────────────────────────────────────────────────────────
function TopBar({ user, onAvatar, onBell, onOrders, elevated, hasNotifs = true, lang, onLang }) {
  return (
    <div className={`topbar ${elevated ? 'elevated' : ''}`}>
      <button className="avatar-btn" onClick={onAvatar} aria-label="Профил">
        {user.initials}
      </button>
      <Wordmark />
      <div className="topbar-right">
        {onLang && <LangChip lang={lang} onChange={onLang} />}
        <ThemeToggle />
      </div>
    </div>
  );
}

// ────────────────────────────────────────────────────────────
// Bottom navigation bar (with raised center action)
// ────────────────────────────────────────────────────────────
function BottomNav({ activeTab, onHome, onCars, onOrders, onBell, hasNotifs = true, showCars = true }) {
  const tabs = [
    { id: 'home',   icon: I.home,   label: t('navHome'),   onClick: onHome },
    { id: 'cars',   icon: I.car,    label: t('navCars'),   onClick: onCars },
    { id: 'orders', icon: I.orders, label: t('navOrders'), onClick: onOrders },
    { id: 'notif',  icon: I.bell,   label: t('navNotifs'), onClick: onBell, badge: hasNotifs },
  ].filter(tab => tab.id !== 'cars' || showCars);
  return (
    <nav className="bottom-nav">
      {tabs.map(tab => {
        const active = activeTab === tab.id;
        return (
          <button key={tab.id} className={`bn-tab ${active ? 'active' : ''}`} onClick={tab.onClick}>
            <span className="bn-ico">
              {tab.icon({ width: active ? 24 : 22, height: active ? 24 : 22 })}
              {tab.badge && !active && <span className="bn-dot"></span>}
            </span>
            <span className="bn-label">{tab.label}</span>
          </button>
        );
      })}
    </nav>
  );
}

// ────────────────────────────────────────────────────────────
// Theme toggle (light / dark) — single tap, no menu
// ────────────────────────────────────────────────────────────
function ThemeToggle() {
  const [dark, setDark] = React.useState(() => document.documentElement.getAttribute('data-theme') === 'dark');
  const toggle = () => {
    const next = dark ? 'light' : 'dark';
    const de = document.documentElement;
    de.classList.add('theme-switching');
    de.setAttribute('data-theme', next);
    try { localStorage.setItem('kolhida-theme', next); } catch (e) {}
    setDark(!dark);
    // Force a repaint, then re-enable transitions on the next frame
    void de.offsetWidth;
    requestAnimationFrame(() => requestAnimationFrame(() => de.classList.remove('theme-switching')));
  };
  return (
    <button className="icon-btn" onClick={toggle} aria-label={dark ? 'Светла тема' : 'Тъмна тема'}>
      {dark ? I.sun() : I.moon()}
    </button>
  );
}

// ────────────────────────────────────────────────────────────
// Page head (subscreens)
// ────────────────────────────────────────────────────────────
function PageHead({ title, onBack, right }) {
  return (
    <div className="page-head">
      <button className="back" onClick={onBack} aria-label="Назад">{I.back()}</button>
      <div className="title">{title}</div>
      {right}
    </div>
  );
}

// ────────────────────────────────────────────────────────────
// Toast helper
// ────────────────────────────────────────────────────────────
function Toast({ msg, show }) {
  return (
    <div className={`toast ${show ? 'show' : ''}`}>
      <span style={{ width:6, height:6, borderRadius:99, background:'#45E29D', display:'inline-block' }}/>
      {msg}
    </div>
  );
}

Object.assign(window, { Wordmark, OAuthGlyph, Tile3D, TopBar, LangChip, PageHead, Toast, I });
