// Shared bits across all 3 directions — avatar, tab bar slot, icons.
// Each direction supplies its own styled wrappers around these primitives.

const FC = window.FC_DATA;

// ── Frame wrapper: 402×874, fills the artboard with proper iOS layout.
function Screen({ children, bg = '#FAF1E8', dark = false, statusBarDark = false }) {
  return (
    <div style={{
      width: 402, height: 874, background: bg, position: 'relative', overflow: 'hidden',
      fontFamily: "'General Sans', -apple-system, system-ui, sans-serif",
      color: dark ? '#fff' : '#1A1410',
      WebkitFontSmoothing: 'antialiased',
    }}>
      {/* status bar */}
      <div style={{
        position: 'absolute', top: 0, left: 0, right: 0, height: 54, zIndex: 30,
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        padding: '18px 28px 0', color: statusBarDark ? '#fff' : '#1A1410',
        fontFamily: '-apple-system, "SF Pro", system-ui',
        fontWeight: 600, fontSize: 16,
      }}>
        <div>9:41</div>
        <div style={{ display: 'flex', gap: 6, alignItems: 'center' }}>
          <svg width="18" height="11" viewBox="0 0 18 11" fill={statusBarDark ? '#fff' : '#1A1410'}>
            <rect x="0" y="7" width="3" height="4" rx="0.6"/>
            <rect x="4.5" y="5" width="3" height="6" rx="0.6"/>
            <rect x="9" y="2.5" width="3" height="8.5" rx="0.6"/>
            <rect x="13.5" y="0" width="3" height="11" rx="0.6"/>
          </svg>
          <svg width="15" height="11" viewBox="0 0 16 11" fill={statusBarDark ? '#fff' : '#1A1410'}>
            <path d="M8 2C10.2 2 12.2 2.9 13.6 4.3l1-1C12.9 1.6 10.5.5 8 .5S3.1 1.6 1.4 3.3l1 1C3.8 2.9 5.8 2 8 2zM8 5.3c1.4 0 2.6.5 3.5 1.4l1-1C11.3 4.5 9.7 3.8 8 3.8S4.7 4.5 3.5 5.7l1 1C5.4 5.8 6.6 5.3 8 5.3zM8 8.5a1.5 1.5 0 100 3 1.5 1.5 0 000-3z"/>
          </svg>
          <svg width="24" height="11" viewBox="0 0 24 11">
            <rect x="0.5" y="0.5" width="20" height="10" rx="2.5" stroke={statusBarDark ? '#fff' : '#1A1410'} strokeOpacity="0.35" fill="none"/>
            <rect x="2" y="2" width="17" height="7" rx="1.5" fill={statusBarDark ? '#fff' : '#1A1410'}/>
            <path d="M22 4v3c.6-.2 1-.7 1-1.5S22.6 4.2 22 4z" fill={statusBarDark ? '#fff' : '#1A1410'} fillOpacity="0.5"/>
          </svg>
        </div>
      </div>
      {/* dynamic island */}
      <div style={{
        position: 'absolute', top: 11, left: '50%', transform: 'translateX(-50%)',
        width: 122, height: 36, borderRadius: 22, background: '#000', zIndex: 31,
      }} />
      {/* content */}
      <div style={{ position: 'absolute', top: 54, left: 0, right: 0, bottom: 0 }}>
        {children}
      </div>
      {/* home indicator */}
      <div style={{
        position: 'absolute', bottom: 8, left: '50%', transform: 'translateX(-50%)',
        width: 134, height: 5, borderRadius: 99, background: dark ? 'rgba(255,255,255,0.6)' : 'rgba(26,20,16,0.3)', zIndex: 40,
      }} />
    </div>
  );
}

// ── Avatar: color disc with initials.
function Avatar({ who, size = 32, ring }) {
  const m = FC.mates[who];
  if (!m) return null;
  return (
    <div style={{
      width: size, height: size, borderRadius: size / 2,
      background: m.color, color: '#fff',
      fontSize: size * 0.38, fontWeight: 700,
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      flexShrink: 0, letterSpacing: 0.3,
      boxShadow: ring ? `0 0 0 2px ${ring}` : undefined,
      fontFamily: "'General Sans', system-ui",
    }}>
      {m.initials[0]}
    </div>
  );
}

// ── Stack of avatars (overlapping).
function AvatarStack({ whos, size = 28, ring = '#fff' }) {
  return (
    <div style={{ display: 'inline-flex' }}>
      {whos.map((w, i) => (
        <div key={w} style={{ marginLeft: i === 0 ? 0 : -10 }}>
          <Avatar who={w} size={size} ring={ring} />
        </div>
      ))}
    </div>
  );
}

// ── Currency helper. £ before, GBP throughout.
function gbp(n, { sign = false } = {}) {
  const s = Math.abs(n).toLocaleString('en-GB', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
  if (!sign) return '£' + s;
  return (n >= 0 ? '+£' : '−£') + s;
}

// ── Stroked icon set used across directions. 24px viewbox.
const Icons = {
  home: (p = {}) => (
    <svg width={p.size || 22} height={p.size || 22} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 1.8} strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 11.5L12 4l9 7.5V20a1 1 0 01-1 1h-5v-6h-6v6H4a1 1 0 01-1-1v-8.5z"/>
    </svg>
  ),
  split: (p = {}) => (
    <svg width={p.size || 22} height={p.size || 22} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 1.8} strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 3v18M8 7l4-4 4 4M8 17l4 4 4-4"/>
    </svg>
  ),
  reminders: (p = {}) => (
    <svg width={p.size || 22} height={p.size || 22} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 1.8} strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="13" r="8"/><path d="M12 9v4l2.5 2M9 3l-3 2M15 3l3 2"/>
    </svg>
  ),
  lists: (p = {}) => (
    <svg width={p.size || 22} height={p.size || 22} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 1.8} strokeLinecap="round" strokeLinejoin="round">
      <path d="M9 6h11M9 12h11M9 18h11"/><circle cx="4.5" cy="6" r="1.3" fill={p.c || 'currentColor'}/><circle cx="4.5" cy="12" r="1.3" fill={p.c || 'currentColor'}/><circle cx="4.5" cy="18" r="1.3" fill={p.c || 'currentColor'}/>
    </svg>
  ),
  plus: (p = {}) => (
    <svg width={p.size || 22} height={p.size || 22} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 2.2} strokeLinecap="round">
      <path d="M12 5v14M5 12h14"/>
    </svg>
  ),
  back: (p = {}) => (
    <svg width={p.size || 22} height={p.size || 22} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 2} strokeLinecap="round" strokeLinejoin="round">
      <path d="M15 5l-7 7 7 7"/>
    </svg>
  ),
  close: (p = {}) => (
    <svg width={p.size || 22} height={p.size || 22} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 2} strokeLinecap="round">
      <path d="M6 6l12 12M18 6l-12 12"/>
    </svg>
  ),
  more: (p = {}) => (
    <svg width={p.size || 22} height={p.size || 22} viewBox="0 0 24 24" fill={p.c || 'currentColor'}>
      <circle cx="5" cy="12" r="1.8"/><circle cx="12" cy="12" r="1.8"/><circle cx="19" cy="12" r="1.8"/>
    </svg>
  ),
  arrow: (p = {}) => (
    <svg width={p.size || 16} height={p.size || 16} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 2} strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 12h14M13 5l7 7-7 7"/>
    </svg>
  ),
  check: (p = {}) => (
    <svg width={p.size || 16} height={p.size || 16} viewBox="0 0 24 24" fill="none" stroke={p.c || 'currentColor'} strokeWidth={p.w || 2.4} strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 12.5l4 4 10-10"/>
    </svg>
  ),
};

// ── 4-tab bottom nav. Each direction wraps this with its own chrome.
function BottomNavRaw({ active, items, dark = false, accent = '#E07A5F' }) {
  return (
    <div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-around', padding: '10px 8px 0' }}>
      {items.map((it) => {
        const on = active === it.key;
        const Ico = Icons[it.key];
        return (
          <div key={it.key} style={{
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4,
            color: on ? accent : (dark ? 'rgba(255,255,255,0.55)' : 'rgba(26,20,16,0.45)'),
            padding: '6px 12px', minWidth: 56,
          }}>
            <Ico size={24} c="currentColor" w={on ? 2 : 1.7} />
            <div style={{ fontSize: 11, fontWeight: on ? 600 : 500, letterSpacing: 0.1 }}>{it.label}</div>
          </div>
        );
      })}
    </div>
  );
}

const NAV_ITEMS = [
  { key: 'home', label: 'Home' },
  { key: 'split', label: 'Split' },
  { key: 'reminders', label: 'Reminders' },
  { key: 'lists', label: 'Lists' },
];

Object.assign(window, { Screen, Avatar, AvatarStack, gbp, Icons, BottomNavRaw, NAV_ITEMS });
