// Sporda — screen mocks for the Phase 1 design canvas.
// Each Screen* component renders a 390×844 phone surface. Shared components
// live at the top. Cite the inspirational reference in a comment above each.
// Replace fictional teams/league copy once the real kit's screens are restored.

const { useState } = React;

// ----- Crest monogram placeholder -----------------------------------------
// Hash a team name to one of 6 brand-safe tints. Use until real SVG crests
// are licensed. The .crest class draws the sheen overlay.
const crestPalette = [
  '#FF6B6B', '#4ECDC4', '#FFE66D', '#7158E2', '#3D5AFE', '#F8B400',
  '#22C55E', '#EC4899', '#0EA5E9', '#A855F7',
];
function hashColor(name) {
  let h = 0;
  for (let i = 0; i < name.length; i++) h = (h * 31 + name.charCodeAt(i)) >>> 0;
  return crestPalette[h % crestPalette.length];
}
const Crest = ({ name, code, size = 'md', bg }) => {
  const sz = size === 'lg' ? 'lg' : size === 'xl' ? 'xl' : '';
  const txt = code || name.slice(0, 3).toUpperCase();
  const color = bg || hashColor(name);
  return (
    <div className={`crest ${sz}`} style={{ background: color }}>
      <span style={{ position: 'relative', zIndex: 1 }}>{txt}</span>
    </div>
  );
};

// ----- Lucide-ish inline icons (no external dep) --------------------------
// Hand-rolled subset; 1.75 stroke, 20px default. currentColor everywhere.
const I = {
  search: (p) => <svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="7"/><path d="m21 21-4.3-4.3"/></svg>,
  bell: (p) => <svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><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>,
  chevR: (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m9 18 6-6-6-6"/></svg>,
  chevL: (p) => <svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m15 18-6-6 6-6"/></svg>,
  chevD: (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m6 9 6 6 6-6"/></svg>,
  cal: (p) => <svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="18" rx="3"/><path d="M16 2v4M8 2v4M3 10h18"/></svg>,
  home: (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill={p.fill?'currentColor':'none'} stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="m3 10 9-7 9 7v10a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg>,
  ball: (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2 7 7l1 6 4 3 4-3 1-6zM12 22l-1.5-4M12 22l1.5-4M2 12l4 1M22 12l-4 1"/></svg>,
  play: (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="currentColor"><path d="M8 5v14l11-7z"/></svg>,
  playOutline: (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinejoin="round"><path d="M8 5v14l11-7z"/></svg>,
  news: (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M8 9h8M8 13h8M8 17h4"/></svg>,
  user: (p) => <svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="8" r="4"/><path d="M4 21a8 8 0 0 1 16 0"/></svg>,
  more: (p) => <svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="currentColor"><circle cx="12" cy="5" r="1.6"/><circle cx="12" cy="12" r="1.6"/><circle cx="12" cy="19" r="1.6"/></svg>,
  star: (p) => <svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill={p.fill?'currentColor':'none'} stroke="currentColor" strokeWidth="1.75" strokeLinejoin="round"><path d="m12 2 3.1 6.3 7 1-5 4.9 1.2 7-6.3-3.3-6.3 3.3 1.2-7-5-4.9 7-1z"/></svg>,
  heart: (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill={p.fill?'currentColor':'none'} stroke="currentColor" strokeWidth="1.75" strokeLinejoin="round"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/></svg>,
  cmt: (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M21 11.5a8.4 8.4 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.4 8.4 0 0 1-3.8-.9L3 21l1.9-5.7a8.4 8.4 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.4 8.4 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8z"/></svg>,
  share: (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8M16 6l-4-4-4 4M12 2v13"/></svg>,
  plus: (p) => <svg viewBox="0 0 24 24" width={p.size||24} height={p.size||24} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 5v14M5 12h14"/></svg>,
  check: (p) => <svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"><path d="m5 12 5 5L20 7"/></svg>,
  x: (p) => <svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M18 6 6 18M6 6l12 12"/></svg>,
  trophy: (p) => <svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M6 9H4a2 2 0 0 1-2-2V5h4M18 9h2a2 2 0 0 0 2-2V5h-4M12 17V12M8 21h8M9 17h6a2 2 0 0 0 2-2V4H7v11a2 2 0 0 0 2 2"/></svg>,
  goal: (p) => <svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="currentColor"><circle cx="12" cy="12" r="6"/></svg>,
  yellowCard: () => <svg viewBox="0 0 12 16" width="10" height="14"><rect width="12" height="16" rx="1.5" fill="#FFB547"/></svg>,
  redCard: () => <svg viewBox="0 0 12 16" width="10" height="14"><rect width="12" height="16" rx="1.5" fill="#FF3B5C"/></svg>,
  sub: (p) => <svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m17 3 4 4-4 4M21 7H9M7 21l-4-4 4-4M3 17h12"/></svg>,
};

// ----- Phone chrome --------------------------------------------------------
const StatusBar = ({ time = '9:41' }) => (
  <>
    <div className="notch"></div>
    <div className="sbar">
      <div>{time}</div>
      <div className="glyphs">
        <svg viewBox="0 0 18 12" fill="currentColor"><rect x="0" y="8" width="3" height="4" rx="1"/><rect x="5" y="5" width="3" height="7" rx="1"/><rect x="10" y="2" width="3" height="10" rx="1"/><rect x="15" y="0" width="3" height="12" rx="1" opacity="0.4"/></svg>
        <svg viewBox="0 0 16 12" fill="currentColor"><path d="M8 2c2.3 0 4.4.9 6 2.4l-1.3 1.4A7.1 7.1 0 0 0 8 4a7.1 7.1 0 0 0-4.7 1.8L2 4.4A8.9 8.9 0 0 1 8 2zm0 3.5c1.4 0 2.6.5 3.5 1.4l-1.3 1.4A3.7 3.7 0 0 0 8 7.5c-1 0-1.8.4-2.5 1l-1.3-1.6A5.5 5.5 0 0 1 8 5.5zm0 3.5a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3z"/></svg>
        <svg viewBox="0 0 26 12" fill="none" stroke="currentColor"><rect x="0.5" y="0.5" width="22" height="11" rx="2.5"/><rect x="2" y="2" width="17" height="8" rx="1.2" fill="currentColor"/><rect x="24" y="4" width="1.5" height="4" rx="0.5" fill="currentColor"/></svg>
      </div>
    </div>
  </>
);

const BottomNav = ({ active = 'home' }) => (
  <div className="bnav">
    {[
      { id: 'home', label: 'Home', icon: I.home },
      { id: 'matches', label: 'Matches', icon: I.ball },
    ].map(s => (
      <div key={s.id} className={`slot ${active === s.id ? 'active' : ''}`}>
        <s.icon size={22} fill={active === s.id} />
        <span>{s.label}</span>
      </div>
    ))}
    <div className="fab"><I.plus /></div>
    {[
      { id: 'news', label: 'News', icon: I.news },
      { id: 'profile', label: 'Profile', icon: I.user },
    ].map(s => (
      <div key={s.id} className={`slot ${active === s.id ? 'active' : ''}`}>
        <s.icon size={22} fill={active === s.id} />
        <span>{s.label}</span>
      </div>
    ))}
  </div>
);

// Top header — left avatar/back, center title/empty, right icons.
const Header = ({ left, title, right }) => (
  <div style={{ display: 'flex', alignItems: 'center', padding: '8px 20px 16px', gap: 12 }}>
    <div>{left}</div>
    <div style={{ flex: 1, textAlign: title ? 'left' : 'center', fontWeight: 700, fontSize: 17 }}>{title}</div>
    <div style={{ display: 'flex', gap: 12, alignItems: 'center' }}>{right}</div>
  </div>
);

// Round outlined icon button (top header)
const RoundBtn = ({ children, dot }) => (
  <button style={{
    width: 40, height: 40, borderRadius: '50%',
    background: 'var(--bg-surface)',
    border: '1px solid var(--border-hairline)',
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    color: 'var(--text-primary)', position: 'relative',
  }}>
    {children}
    {dot && <span style={{ position: 'absolute', top: 8, right: 8, width: 8, height: 8, borderRadius: '50%', background: 'var(--accent)' }}/>}
  </button>
);

// ----- Match card variants -----
// MatchCardCompact: feed-row card. Inspiration: GoalLine Matches list.
const MatchCardCompact = ({ league, venue, home, away, hs, as, status }) => (
  <div className="card" style={{ marginBottom: 12 }}>
    <div style={{ textAlign: 'center', fontSize: 11, color: 'var(--text-secondary)', fontWeight: 600, marginBottom: 10 }}>
      {league}
    </div>
    <div style={{ display: 'grid', gridTemplateColumns: '1fr auto 1fr', alignItems: 'center', gap: 12 }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, justifyContent: 'flex-end' }}>
        <span style={{ fontWeight: 600, fontSize: 14 }}>{home.name}</span>
        <Crest name={home.name} code={home.code} />
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
        <span className="score sm">{hs}</span>
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 2 }}>
          <span style={{ fontSize: 10, color: 'var(--text-muted)', fontWeight: 700, letterSpacing: '0.05em' }}>{status}</span>
        </div>
        <span className="score sm">{as}</span>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
        <Crest name={away.name} code={away.code} />
        <span style={{ fontWeight: 600, fontSize: 14 }}>{away.name}</span>
      </div>
    </div>
    {venue && <div style={{ textAlign: 'center', fontSize: 11, color: 'var(--text-muted)', marginTop: 8 }}>{venue}</div>}
  </div>
);

// MatchCardHero: featured live match. Inspiration: Eleven Beyond Arena live screen.
const MatchCardHero = ({ league, group, home, away, hs, as, minute, half }) => (
  <div style={{
    borderRadius: 'var(--r-card-lg)',
    background: 'var(--hero-grad-rich)',
    padding: '20px 18px',
    position: 'relative',
    overflow: 'hidden',
    marginBottom: 16,
  }}>
    <div style={{
      position: 'absolute', top: -60, right: -40, width: 220, height: 220,
      background: 'radial-gradient(circle, rgba(200,255,62,0.10) 0%, transparent 60%)',
    }}/>
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 18 }}>
      <div style={{ fontSize: 12, color: 'var(--text-secondary)', fontWeight: 600 }}>{league}</div>
      <div className="pill live"><span className="dot"/>LIVE</div>
    </div>
    <div style={{ display: 'grid', gridTemplateColumns: '1fr auto 1fr', alignItems: 'center', textAlign: 'center', gap: 14 }}>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8 }}>
        <Crest name={home.name} code={home.code} size="lg"/>
        <div style={{ fontWeight: 700, fontSize: 14 }}>{home.name}</div>
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 12 }}>
          <span className="score">{hs}</span>
          <span style={{ color: 'var(--text-muted)', fontSize: 16 }}>:</span>
          <span className="score">{as}</span>
        </div>
        <div className="mono" style={{ fontSize: 12, color: 'var(--text-secondary)', padding: '4px 10px', background: 'rgba(0,0,0,0.3)', borderRadius: 8 }}>
          {minute}' · {half}
        </div>
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8 }}>
        <Crest name={away.name} code={away.code} size="lg"/>
        <div style={{ fontWeight: 700, fontSize: 14 }}>{away.name}</div>
      </div>
    </div>
    {group && (
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 8, marginTop: 18 }}>
        {group.map((o, i) => (
          <div key={i} style={{
            padding: '10px 12px',
            background: 'rgba(255,255,255,0.04)',
            border: '1px solid var(--border-hairline)',
            borderRadius: 12,
            textAlign: 'center',
          }}>
            <div style={{ fontSize: 10, color: 'var(--text-secondary)', fontWeight: 700, letterSpacing: '0.04em', marginBottom: 2 }}>{o.lbl}</div>
            <div className="mono" style={{ fontSize: 15, fontWeight: 700 }}>{o.v}</div>
          </div>
        ))}
      </div>
    )}
  </div>
);

// Multi-match tile carousel (Inspiration: GoalLine Upcomings horizontal cards)
const MatchTile = ({ when, home, away }) => (
  <div style={{
    flex: 'none', width: 220,
    background: 'var(--bg-surface)',
    borderRadius: 'var(--r-card)',
    padding: 14,
    border: '1px solid var(--border-hairline)',
  }}>
    <div style={{ fontSize: 11, color: 'var(--text-secondary)', fontWeight: 600, marginBottom: 10 }}>{when}</div>
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6, flex: 1 }}>
        <Crest name={home.name} code={home.code}/>
        <span style={{ fontSize: 11, fontWeight: 600 }}>{home.code}</span>
      </div>
      <div style={{ fontSize: 11, color: 'var(--text-muted)', fontWeight: 700, padding: '0 8px' }}>vs</div>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6, flex: 1 }}>
        <Crest name={away.name} code={away.code}/>
        <span style={{ fontSize: 11, fontWeight: 600 }}>{away.code}</span>
      </div>
    </div>
  </div>
);

// Make these globally available
Object.assign(window, {
  Crest, I, StatusBar, BottomNav, Header, RoundBtn, MatchCardCompact, MatchCardHero, MatchTile, hashColor, crestPalette,
});
