// wci26_pwa/m-chrome.jsx — iOS chrome (status bar, large title, tab bar) + SF-symbol-ish icons

// ─────────────────────────────────────────────────────────────
// Icons (drawn fresh, no copying SF Symbols exactly)
// ─────────────────────────────────────────────────────────────
const Icon = {
  bolt: (filled, color) => (
    <svg width="26" height="26" viewBox="0 0 26 26" fill="none">
      <path d="M14.5 2.5 L7 14h5.5L11 23.5 19 11.5h-5.5L15 2.5Z"
        fill={filled ? color : 'none'} stroke={color} strokeWidth="1.7" strokeLinejoin="round" />
    </svg>
  ),
  globe: (filled, color) => (
    <svg width="26" height="26" viewBox="0 0 26 26" fill="none">
      <circle cx="13" cy="13" r="9.5" stroke={color} strokeWidth="1.6" fill={filled ? color + '22' : 'none'} />
      <ellipse cx="13" cy="13" rx="4" ry="9.5" stroke={color} strokeWidth="1.4" fill="none" />
      <line x1="3.5" y1="13" x2="22.5" y2="13" stroke={color} strokeWidth="1.4" />
      <path d="M4 9.5 Q13 12 22 9.5" stroke={color} strokeWidth="1.2" fill="none" />
      <path d="M4 16.5 Q13 14 22 16.5" stroke={color} strokeWidth="1.2" fill="none" />
    </svg>
  ),
  trophy: (filled, color) => (
    <svg width="26" height="26" viewBox="0 0 26 26" fill="none">
      <path d="M7 4h12v5a6 6 0 0 1-12 0V4Z"
        fill={filled ? color : 'none'} stroke={color} strokeWidth="1.6" strokeLinejoin="round" />
      <path d="M7 6H4v2a3 3 0 0 0 3 3M19 6h3v2a3 3 0 0 1-3 3" stroke={color} strokeWidth="1.6" fill="none" />
      <path d="M10 16h6l1 4H9l1-4ZM8 22h10" stroke={color} strokeWidth="1.6" fill={filled ? color + '44' : 'none'} strokeLinejoin="round" strokeLinecap="round" />
    </svg>
  ),
  wallet: (filled, color) => (
    <svg width="26" height="26" viewBox="0 0 26 26" fill="none">
      <rect x="3" y="6" width="20" height="15" rx="3"
        fill={filled ? color + '22' : 'none'} stroke={color} strokeWidth="1.6" />
      <path d="M3 10h20" stroke={color} strokeWidth="1.6" />
      <circle cx="18" cy="15.5" r="1.5" fill={color} />
    </svg>
  ),
  more: (filled, color) => (
    <svg width="26" height="26" viewBox="0 0 26 26" fill="none">
      <circle cx="13" cy="13" r="9.5" stroke={color} strokeWidth="1.6" fill={filled ? color + '22' : 'none'} />
      <circle cx="7.5" cy="13" r="1.4" fill={color} />
      <circle cx="13" cy="13" r="1.4" fill={color} />
      <circle cx="18.5" cy="13" r="1.4" fill={color} />
    </svg>
  ),
  chevR: (color = 'rgba(235,235,245,0.3)') => (
    <svg width="8" height="14" viewBox="0 0 8 14">
      <path d="M1 1l6 6-6 6" stroke={color} strokeWidth="2" fill="none" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  chevL: (color = '#fff') => (
    <svg width="11" height="18" viewBox="0 0 11 18">
      <path d="M9 1L2 9l7 8" stroke={color} strokeWidth="2.3" fill="none" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  search: (color = '#fff') => (
    <svg width="17" height="17" viewBox="0 0 17 17" fill="none">
      <circle cx="7" cy="7" r="5.5" stroke={color} strokeWidth="1.6" />
      <path d="M11 11l4.5 4.5" stroke={color} strokeWidth="1.7" strokeLinecap="round" />
    </svg>
  ),
  xSocial: (color = '#fff') => (
    <svg width="16" height="16" viewBox="0 0 24 24">
      <path d="M4 4l12.9 16h3.1L7.1 4H4Zm15.2 0h-3.1L4.8 20h3.1L19.2 4Z" fill={color} />
    </svg>
  ),
  telegram: (color = '#fff') => (
    <svg width="18" height="18" viewBox="0 0 24 24">
      <path d="M21 4.2 3.8 10.9c-1 .4-1 1.3-.2 1.6l4.4 1.4 1.7 5.2c.2.7.6.8 1.1.3l2.5-2.4 4.5 3.3c.8.4 1.3.2 1.5-.8L22 5.4c.2-.9-.3-1.5-1-1.2ZM9.3 13.4l8.7-5.5c.4-.2.7-.1.4.2l-7 6.3-.3 3.1-1.8-4.1Z" fill={color} />
    </svg>
  ),
  bell: (color = '#fff') => (
    <svg width="18" height="20" viewBox="0 0 18 20" fill="none">
      <path d="M9 1.5a5 5 0 0 0-5 5v3l-2 4h14l-2-4v-3a5 5 0 0 0-5-5ZM7 17a2 2 0 0 0 4 0" stroke={color} strokeWidth="1.6" fill="none" strokeLinejoin="round" strokeLinecap="round" />
    </svg>
  ),
  share: (color = '#fff') => (
    <svg width="20" height="22" viewBox="0 0 20 22" fill="none">
      <path d="M10 1v14M5 6l5-5 5 5" stroke={color} strokeWidth="1.8" fill="none" strokeLinecap="round" strokeLinejoin="round" />
      <path d="M4 11H2v9h16v-9h-2" stroke={color} strokeWidth="1.8" fill="none" strokeLinejoin="round" strokeLinecap="round" />
    </svg>
  ),
  plus: (color = '#fff') => (
    <svg width="14" height="14" viewBox="0 0 14 14"><path d="M7 1v12M1 7h12" stroke={color} strokeWidth="2" strokeLinecap="round" /></svg>
  ),
  star: (filled, color) => (
    <svg width="18" height="18" viewBox="0 0 18 18">
      <path d="M9 1.5l2.3 4.6 5.2.75-3.75 3.6.9 5.05L9 13.2l-4.65 2.3.9-5.05L1.5 6.85l5.2-.75L9 1.5Z"
        fill={filled ? color : 'none'} stroke={color} strokeWidth="1.4" strokeLinejoin="round" />
    </svg>
  ),
  team: (filled, color) => (
    <svg width="26" height="26" viewBox="0 0 26 26" fill="none">
      <path d="M13 3l2.5 5.1 5.6.8-4 3.9.95 5.5L13 15.7 7.95 18.3l.95-5.5-4-3.9 5.6-.8L13 3Z"
        fill={filled ? color + '44' : 'none'} stroke={color} strokeWidth="1.5" strokeLinejoin="round" />
      <path d="M6.5 21.5h13" stroke={color} strokeWidth="1.5" strokeLinecap="round" />
      <path d="M9 19.5h8" stroke={color} strokeWidth="1.5" strokeLinecap="round" opacity="0.72" />
    </svg>
  ),
  flame: (color = '#FF7A1F') => (
    <svg width="14" height="16" viewBox="0 0 14 16">
      <path d="M7 1c1 3-2 4-2 7 0 1 .5 2 2 2s2.5-1 2-3c2 1 3 3 3 5 0 2.2-2 4-5 4s-5-1.8-5-4c0-3 2.5-4 2.5-7C4.5 2.5 6 1 7 1Z"
        fill={color} stroke="rgba(0,0,0,0.3)" strokeWidth="0.6" />
    </svg>
  ),
  spark: (color = '#fff') => (
    <svg width="14" height="14" viewBox="0 0 14 14"><path d="M7 1l1.7 3.6L12 6l-3.3 1.4L7 11l-1.7-3.6L2 6l3.3-1.4L7 1Z" fill={color} /></svg>
  ),
};

// ─────────────────────────────────────────────────────────────
// Status bar (9:41 + signal + wifi + battery)
// ─────────────────────────────────────────────────────────────
const StatusBar = ({ time = '9:41' }) => (
  <div style={{
    position: 'relative', zIndex: 30, height: 54,
    padding: '21px 32px 0', boxSizing: 'border-box',
    display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between',
    pointerEvents: 'none',
  }}>
    <div style={{
      width: 100, textAlign: 'center',
      fontFamily: '-apple-system, "SF Pro"', fontWeight: 590,
      fontSize: 17, color: '#fff', letterSpacing: -0.3,
    }}>{time}</div>
    <div style={{ width: 126 }} /> {/* dynamic island space */}
    <div style={{ width: 100, display: 'flex', alignItems: 'center', justifyContent: 'flex-end', gap: 6 }}>
      <svg width="18" height="11" viewBox="0 0 18 11">
        <rect x="0" y="7" width="3" height="4" rx="0.6" fill="#fff" />
        <rect x="4.5" y="5" width="3" height="6" rx="0.6" fill="#fff" />
        <rect x="9" y="2.5" width="3" height="8.5" rx="0.6" fill="#fff" />
        <rect x="13.5" y="0" width="3" height="11" rx="0.6" fill="#fff" />
      </svg>
      <svg width="25" height="12" viewBox="0 0 25 12">
        <rect x="0.5" y="0.5" width="21" height="11" rx="3" stroke="rgba(255,255,255,0.4)" fill="none" />
        <rect x="2" y="2" width="18" height="8" rx="1.6" fill="#fff" />
        <rect x="22.5" y="3.5" width="1.5" height="5" rx="0.7" fill="rgba(255,255,255,0.4)" />
      </svg>
    </div>
  </div>
);

// ─────────────────────────────────────────────────────────────
// Large nav (iOS 17 large title that compresses on scroll)
// ─────────────────────────────────────────────────────────────
const NavBar = ({ title, eyebrow, trailing, leading, compact = false }) => (
  <div style={{
    paddingTop: 4, paddingBottom: compact ? 4 : 6,
    position: 'relative', zIndex: 5,
  }}>
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '0 16px', minHeight: 44,
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 6, minWidth: 56 }}>
        {leading}
      </div>
      <div style={{
        opacity: compact ? 1 : 0, transition: 'opacity 160ms',
        fontWeight: 600, fontSize: 17, letterSpacing: -0.4,
      }}>{compact ? title : ''}</div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, minWidth: 56, justifyContent: 'flex-end' }}>
        {trailing}
      </div>
    </div>
    {!compact && (
      <div style={{ padding: '4px 20px 6px' }}>
        {eyebrow && (
          <div style={{
            fontSize: 11, fontWeight: 700, color: 'var(--gold)',
            letterSpacing: 0.12 + 'em', textTransform: 'uppercase', marginBottom: 4,
          }}>{eyebrow}</div>
        )}
        <div className="sfd" style={{ fontSize: 34, fontWeight: 700, lineHeight: 1.04, color: '#fff' }}>
          {title}
        </div>
      </div>
    )}
  </div>
);

// Small glass pill button for nav
const NavPill = ({ children, onClick, href, ariaLabel, title, style = {} }) => {
  const sharedStyle = {
    width: 34, height: 34, borderRadius: 999,
    background: 'linear-gradient(180deg, rgba(255,255,255,0.10), rgba(255,255,255,0.02))',
    backdropFilter: 'blur(22px) saturate(180%)',
    WebkitBackdropFilter: 'blur(22px) saturate(180%)',
    border: '0.5px solid rgba(255,255,255,0.14)',
    boxShadow:
      'inset 0 1px 0 rgba(255,255,255,0.18), ' +
      'inset 0 -1px 0 rgba(0,0,0,0.2), ' +
      '0 4px 12px -2px rgba(0,0,0,0.45)',
    display: 'grid', placeItems: 'center',
    color: '#fff', cursor: 'pointer',
    ...style,
  };
  if (href) {
    return (
      <a
        href={href}
        target="_blank"
        rel="noopener noreferrer"
        aria-label={ariaLabel}
        title={title || ariaLabel}
        className="press"
        style={{ ...sharedStyle, textDecoration: 'none' }}
      >
        {children}
      </a>
    );
  }
  return (
    <button onClick={onClick} aria-label={ariaLabel} title={title || ariaLabel} className="press" style={sharedStyle}>
      {children}
    </button>
  );
};

// Sync-with-desktop pill (key visible affordance)
const SyncPill = ({ online }) => (
  <div style={{
    display: 'flex', alignItems: 'center', gap: 6,
    padding: '7px 11px 7px 9px', borderRadius: 999,
    background: 'linear-gradient(180deg, rgba(159,214,52,0.18), rgba(159,214,52,0.04))',
    border: '0.5px solid rgba(159,214,52,0.32)',
    backdropFilter: 'blur(22px) saturate(180%)',
    WebkitBackdropFilter: 'blur(22px) saturate(180%)',
    boxShadow:
      'inset 0 1px 0 rgba(255,255,255,0.10), ' +
      '0 4px 12px -4px rgba(159,214,52,0.35)',
  }}>
    <span style={{
      width: 7, height: 7, borderRadius: '50%',
      background: online ? '#9FD634' : 'rgba(255,255,255,0.4)',
      boxShadow: online ? '0 0 8px #9FD634' : 'none',
    }} />
    <span style={{ fontSize: 11, fontWeight: 700, color: '#D7F09A', letterSpacing: -0.1 }}>
      Synced
    </span>
  </div>
);

// ─────────────────────────────────────────────────────────────
// Bottom tab bar — liquid glass
// ─────────────────────────────────────────────────────────────
const MOBILE_TABS = [
  { k: 'live',     label: 'Live',    icon: Icon.bolt },
  { k: 'globe',    label: 'Globe',   icon: Icon.globe },
  { k: 'battle',   label: 'Battle',  icon: Icon.trophy },
  { k: 'rewards',  label: 'Rewards', icon: Icon.wallet },
  { k: 'team',     label: 'Teams',   icon: Icon.team },
];

const TabBar = ({ tab, setTab }) => (
  <div style={{
    position: 'absolute', left: 8, right: 8, bottom: 8, zIndex: 40,
    borderRadius: 30, padding: '4px 4px 14px',
    background:
      'linear-gradient(180deg, rgba(41,31,82,0.62), rgba(20,16,40,0.72))',
    backdropFilter: 'blur(36px) saturate(180%)',
    WebkitBackdropFilter: 'blur(36px) saturate(180%)',
    border: '0.5px solid rgba(255,255,255,0.10)',
    boxShadow:
      'inset 0 1px 0 rgba(255,255,255,0.10), ' +
      'inset 0 -1px 0 rgba(0,0,0,0.4), ' +
      '0 -1px 0 rgba(255,255,255,0.04), ' +
      '0 12px 32px rgba(0,0,0,0.55), ' +
      '0 0 40px -16px rgba(245,208,32,0.25)',
    display: 'flex', justifyContent: 'space-between',
  }}>
    {/* Shine highlight along top edge */}
    <div aria-hidden style={{
      position: 'absolute', top: 0, left: 24, right: 24, height: 1,
      background: 'linear-gradient(90deg, transparent, rgba(255,255,255,0.25), transparent)',
      pointerEvents: 'none', borderRadius: 1,
    }} />
    {MOBILE_TABS.map(t => {
      const active = tab === t.k;
      const color = active ? '#F5D020' : 'rgba(235,235,245,0.55)';
      return (
        <button key={t.k} className={'tab-btn' + (active ? ' active' : '')}
          onClick={() => setTab(t.k)} style={{ position: 'relative' }}>
          {active && (
            <span aria-hidden style={{
              position: 'absolute',
              top: 4, left: '50%', transform: 'translateX(-50%)',
              width: 52, height: 36, borderRadius: 12,
              background: 'linear-gradient(180deg, rgba(245,208,32,0.28), rgba(245,208,32,0.08))',
              border: '0.5px solid rgba(245,208,32,0.35)',
              boxShadow:
                'inset 0 1px 0 rgba(255,255,255,0.25), ' +
                'inset 0 -1px 0 rgba(0,0,0,0.25), ' +
                '0 4px 14px -4px rgba(245,208,32,0.6), ' +
                '0 1px 2px rgba(0,0,0,0.4)',
              pointerEvents: 'none',
            }} />
          )}
          <span style={{ position: 'relative', zIndex: 1 }}>{t.icon(active, color)}</span>
          <span style={{ position: 'relative', zIndex: 1 }}>{t.label}</span>
        </button>
      );
    })}
    {/* Home indicator */}
    <div style={{
      position: 'absolute', bottom: -10, left: '50%', transform: 'translateX(-50%)',
      width: 134, height: 5, borderRadius: 999,
      background: 'rgba(255,255,255,0.85)',
    }} />
  </div>
);

// ─────────────────────────────────────────────────────────────
// Notification toast banner (iOS push)
// ─────────────────────────────────────────────────────────────
const NotifToast = ({ visible, country, amount }) => {
  if (!visible) return null;
  return (
    <div className="ios-toast">
      <div style={{
        width: 38, height: 38, borderRadius: 9,
        background: 'linear-gradient(135deg, var(--gold), var(--fifa-orange))',
        display: 'grid', placeItems: 'center', flexShrink: 0,
        boxShadow: '0 0 14px -4px var(--gold)',
      }}>
        <img src="assets/logo.png" width="26" height="26" />
      </div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <span style={{ fontSize: 13, fontWeight: 600, color: '#fff', letterSpacing: -0.2 }}>WCI26</span>
          <span style={{ fontSize: 11, color: 'rgba(235,235,245,0.5)' }}>now</span>
        </div>
        <div style={{ fontSize: 14, color: '#fff', letterSpacing: -0.2, marginTop: 1 }}>
          {country && (
            <>
              <FlagRect code={country.code} w={16} h={11} style={{ marginRight: 6, verticalAlign: '-2px', borderRadius: 1 }} />
              <strong style={{ fontWeight: 600 }}>{country.name}</strong> just pumped ${'+'}{amount}% in the last 5m
            </>
          )}
        </div>
      </div>
    </div>
  );
};

// ─────────────────────────────────────────────────────────────
// Mini sparkline (synthetic from country momentum)
// ─────────────────────────────────────────────────────────────
const MobileSparkline = ({ data, color = '#34C759', w = 60, h = 24, fill = true }) => {
  const min = Math.min(...data);
  const max = Math.max(...data);
  const range = max - min || 1;
  const pts = data.map((v, i) => {
    const x = (i / (data.length - 1)) * w;
    const y = h - ((v - min) / range) * (h - 2) - 1;
    return [x, y];
  });
  const d = pts.map(([x, y], i) => (i === 0 ? `M${x},${y}` : `L${x},${y}`)).join(' ');
  const last = pts[pts.length - 1];
  return (
    <svg className="m-spark" width={w} height={h} viewBox={`0 0 ${w} ${h}`}>
      {fill && (
        <path d={`${d} L${w},${h} L0,${h} Z`} fill={color} opacity="0.18" />
      )}
      <path d={d} fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" />
      <circle cx={last[0]} cy={last[1]} r="2" fill={color} />
    </svg>
  );
};

// Deterministic 24-pt sparkline from country code + change
const makeSpark = (code, change24h) => {
  const seed = code.charCodeAt(0) + code.charCodeAt(1) * 7 + code.charCodeAt(2) * 13;
  let s = seed;
  const next = () => { s = (s * 9301 + 49297) % 233280; return s / 233280; };
  const pts = [];
  let v = 50;
  for (let i = 0; i < 24; i++) {
    v += (next() - 0.5) * 12;
    pts.push(v);
  }
  // Force last point in direction of change
  const trend = change24h / 8;
  return pts.map((p, i) => p + (i / 24) * trend * 12);
};

Object.assign(window, {
  Icon, StatusBar, NavBar, NavPill, SyncPill, TabBar, NotifToast, MobileSparkline, makeSpark, MOBILE_TABS,
});
