/* global React, TeamDisc, Jersey, Form, Panel */
const PLm = window.PL;

// smooth cubic path through points [{x,y}]
function smooth(pts) {
  if (pts.length < 2) return '';
  let d = `M ${pts[0].x} ${pts[0].y}`;
  for (let i = 0; i < pts.length - 1; i++) {
    const p0 = pts[i - 1] || pts[i], p1 = pts[i], p2 = pts[i + 1], p3 = pts[i + 2] || p2;
    const c1x = p1.x + (p2.x - p0.x) / 6, c1y = p1.y + (p2.y - p0.y) / 6;
    const c2x = p2.x - (p3.x - p1.x) / 6, c2y = p2.y - (p3.y - p1.y) / 6;
    d += ` C ${c1x} ${c1y}, ${c2x} ${c2y}, ${p2.x} ${p2.y}`;
  }
  return d;
}
function starPath(cx, cy, r) {
  let p = '';
  for (let i = 0; i < 10; i++) {
    const ang = -Math.PI / 2 + i * Math.PI / 5;
    const rad = i % 2 ? r * 0.46 : r;
    p += (i ? 'L' : 'M') + (cx + rad * Math.cos(ang)).toFixed(1) + ' ' + (cy + rad * Math.sin(ang)).toFixed(1) + ' ';
  }
  return p + 'Z';
}

function MomentumChart({ m, light = false }) {
  const W = 600, H = 170, mid = H / 2;
  const n = m.momentum.length, maxV = 80;
  const x = (i) => (i / (n - 1)) * W;
  const y = (v) => mid - (v / maxV) * (mid - 8);
  const pts = m.momentum.map((v, i) => ({ x: x(i), y: y(v) }));
  const line = smooth(pts);
  const area = `${line} L ${W} ${mid} L 0 ${mid} Z`;
  const [hov, setHov] = React.useState(null);
  const [scrub, setScrub] = React.useState(null); // {pct, min}
  const htPct = (45 / m.momMins) * 100;
  const lab = light ? 'var(--text-muted)' : 'rgba(255,255,255,0.65)';
  const labDim = light ? 'var(--text-muted)' : 'rgba(255,255,255,0.5)';
  const lineStroke = light ? 'rgba(14,0,92,0.4)' : 'rgba(255,255,255,0.5)';
  const midStroke = light ? 'rgba(14,0,92,0.18)' : 'rgba(255,255,255,0.25)';
  const htStroke = light ? 'rgba(14,0,92,0.3)' : 'rgba(255,255,255,0.3)';
  const goalBorder = light ? '#fff' : '#0A0044';
  const onMove = (e) => {
    const r = e.currentTarget.getBoundingClientRect();
    let pct = ((e.clientX - r.left) / r.width) * 100;
    pct = Math.max(0, Math.min(100, pct));
    setScrub({ pct, min: Math.round((pct / 100) * m.momMins) });
  };
  return (
    <div>
      <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 6 }}>
        <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 12, letterSpacing: '0.1em', textTransform: 'uppercase', color: lab }}>Attack Momentum</span>
        <span style={{ fontFamily: 'var(--font-mono)', fontSize: 11, color: labDim }}>0' – {m.momMins}'</span>
      </div>
      <div style={{ position: 'relative', height: 110 }} onMouseMove={onMove} onMouseLeave={() => setScrub(null)}>
        <svg viewBox={`0 0 ${W} ${H}`} style={{ width: '100%', height: '100%', display: 'block' }} preserveAspectRatio="none">
          <defs>
            <clipPath id="mom-top"><rect x="0" y="0" width={W} height={mid} /></clipPath>
            <clipPath id="mom-bot"><rect x="0" y={mid} width={W} height={mid} /></clipPath>
            <linearGradient id="mom-lime" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor="#D1F206" stopOpacity="0.85" /><stop offset="1" stopColor="#D1F206" stopOpacity="0.15" /></linearGradient>
            <linearGradient id="mom-blue" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor="#5B8DEF" stopOpacity="0.15" /><stop offset="1" stopColor="#5B8DEF" stopOpacity="0.8" /></linearGradient>
          </defs>
          <path d={area} fill="url(#mom-lime)" clipPath="url(#mom-top)" />
          <path d={area} fill="url(#mom-blue)" clipPath="url(#mom-bot)" />
          <path d={line} fill="none" stroke={lineStroke} strokeWidth="1" vectorEffect="non-scaling-stroke" />
          <line x1="0" y1={mid} x2={W} y2={mid} stroke={midStroke} strokeWidth="1" vectorEffect="non-scaling-stroke" />
          <line x1={(45 / m.momMins) * W} y1="4" x2={(45 / m.momMins) * W} y2={H - 4} stroke={htStroke} strokeWidth="1" strokeDasharray="2 4" vectorEffect="non-scaling-stroke" />
        </svg>
        {/* HT label under the half-time line */}
        <span style={{ position: 'absolute', left: `${htPct}%`, bottom: -2, transform: 'translateX(-50%)', fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: 9, letterSpacing: '0.04em', color: labDim, pointerEvents: 'none' }}>HT</span>
        {/* hover scrubber */}
        {scrub && (
          <div style={{ position: 'absolute', left: `${scrub.pct}%`, top: 0, bottom: 0, width: 0, borderLeft: `1px dashed ${light ? 'rgba(14,0,92,0.5)' : 'rgba(255,255,255,0.7)'}`, transform: 'translateX(-0.5px)', pointerEvents: 'none' }}>
            <span style={{ position: 'absolute', left: '50%', bottom: -20, transform: 'translateX(-50%)', background: 'var(--bs-lime)', color: 'var(--bs-blue-navy)', fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: 10, padding: '2px 6px', borderRadius: 3, whiteSpace: 'nowrap' }}>{scrub.min}'</span>
          </div>
        )}
        {/* goal markers — sit ON the momentum curve */}
        {m.momGoals.map((g, i) => {
          const home = g.t === 'h';
          const leftPct = (g.min / m.momMins) * 100;
          const gi = Math.round((g.min / m.momMins) * (n - 1));
          const gv = m.momentum[Math.max(0, Math.min(n - 1, gi))];
          const topPct = (y(gv) / H) * 100;
          return (
            <div key={i} onMouseEnter={() => setHov(i)} onMouseLeave={() => setHov(null)}
              style={{ position: 'absolute', left: `${leftPct}%`, top: `${topPct}%`, transform: 'translate(-50%,-50%)', cursor: 'pointer', zIndex: 3 }}>
              <span style={{ display: 'block', width: 14, height: 14, borderRadius: '50%', background: home ? 'var(--bs-lime)' : '#5B8DEF', border: `2px solid ${goalBorder}`, boxShadow: '0 1px 4px rgba(0,0,0,0.5)' }} />
              {hov === i && (
                <div style={{ position: 'absolute', left: '50%', bottom: 20, transform: 'translateX(-50%)', zIndex: 5, whiteSpace: 'nowrap', background: 'var(--bs-blue-navy)', border: '1px solid rgba(255,255,255,0.18)', borderRadius: 5, padding: '6px 9px', display: 'flex', alignItems: 'center', gap: 7, pointerEvents: 'none' }}>
                  <span style={{ width: 6, height: 6, borderRadius: '50%', background: home ? 'var(--bs-lime)' : '#5B8DEF' }} />
                  <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 11, color: '#fff' }}>GOAL · {PLm.T[home ? m.h : m.a].abbr}</span>
                  <span style={{ fontFamily: 'var(--font-body)', fontSize: 11, color: 'rgba(255,255,255,0.75)' }}>{g.scorer} {g.min}'</span>
                </div>
              )}
            </div>
          );
        })}
      </div>
      <div style={{ display: 'flex', gap: 18, marginTop: 12 }}>
        <Key c="var(--bs-lime)" label={PLm.T[m.h].name} light={light} />
        <Key c="#5B8DEF" label={PLm.T[m.a].name} light={light} />
        <span style={{ marginLeft: 'auto', fontFamily: 'var(--font-body)', fontSize: 11, color: labDim }}>● goals · hover timeline</span>
      </div>
    </div>
  );
}
function Key({ c, label, light }) {
  return <span style={{ display: 'inline-flex', alignItems: 'center', gap: 7 }}>
    <span style={{ width: 11, height: 11, borderRadius: 2, background: c }} />
    <span style={{ fontFamily: 'var(--font-body)', fontSize: 12, color: light ? 'var(--text-body)' : 'rgba(255,255,255,0.7)' }}>{label}</span>
  </span>;
}

function CompareBar({ label, h, a, suffix = '', light = false }) {
  const total = h + a || 1;
  const hv = light ? 'var(--text-brand)' : 'var(--bs-lime)';
  const av = light ? '#2A6FDB' : '#5B8DEF';
  return (
    <div style={{ marginBottom: 11 }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 6 }}>
        <span style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: 16, color: hv }}>{h}{suffix}</span>
        <span style={{ fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 11, letterSpacing: '0.08em', textTransform: 'uppercase', color: light ? 'var(--text-muted)' : 'rgba(255,255,255,0.55)' }}>{label}</span>
        <span style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: 16, color: av }}>{a}{suffix}</span>
      </div>
      <div style={{ display: 'flex', height: 8, gap: 4 }}>
        <div style={{ flex: h / total, background: 'var(--bs-lime)', borderRadius: '999px 0 0 999px', minWidth: 6 }} />
        <div style={{ flex: a / total, background: '#5B8DEF', borderRadius: '0 999px 999px 0', minWidth: 6 }} />
      </div>
    </div>
  );
}

function OddsTile({ label, value }) {
  return (
    <div style={{ flex: 1, background: 'rgba(255,255,255,0.05)', border: '1px solid rgba(255,255,255,0.14)', borderRadius: 6, padding: '10px 12px', cursor: 'pointer', transition: 'background .15s' }} className="bs-odds">
      <div style={{ fontFamily: 'var(--font-body)', fontSize: 10.5, letterSpacing: '0.04em', textTransform: 'uppercase', color: 'rgba(255,255,255,0.55)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{label}</div>
      <div style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: 18, color: 'var(--bs-lime)', marginTop: 3 }}>{value}</div>
    </div>
  );
}

/* Shot map — full horizontal black pitch. Home attacks right, away attacks left (in 'both').
   view: 'h' | 'a' | 'both'. Single view: lime on-target, grey off, lime goal-stars.
   Both: shots coloured by team (home lime, away blue). */
function ShotMap({ m, view }) {
  const W = 460, H = 268, P = 10;
  const innerW = W - 2 * P, innerH = H - 2 * P;
  const plotX = (d, side) => {
    const f = side === 'r' ? 50 + (d / 100) * 47 : 50 - (d / 100) * 47;
    return P + (f / 100) * innerW;
  };
  const plotY = (acr) => P + 10 + (acr / 100) * (innerH - 20);
  const both = view === 'both';
  const items = [];
  if (view === 'h' || both) m.shotMap.forEach((s) => items.push({ s, side: 'r', t: m.h }));
  if (view === 'a' || both) m.shotMapAway.forEach((s) => items.push({ s, side: both ? 'l' : 'r', t: m.a }));
  const line = 'rgba(255,255,255,0.4)';
  const cy = H / 2, boxH = 116, sixH = 52;
  return (
    <svg viewBox={`0 0 ${W} ${H}`} style={{ width: '100%', height: 'auto', display: 'block' }}>
      <rect x="0" y="0" width={W} height={H} rx="6" fill="#0B0B0B" />
      <rect x={P} y={P} width={innerW} height={innerH} fill="none" stroke={line} strokeWidth="1.2" />
      {/* halfway line + centre circle */}
      <line x1={W / 2} y1={P} x2={W / 2} y2={H - P} stroke={line} strokeWidth="1.2" />
      <circle cx={W / 2} cy={cy} r="34" fill="none" stroke={line} strokeWidth="1.2" />
      <circle cx={W / 2} cy={cy} r="2" fill="rgba(255,255,255,0.55)" />
      {/* left goal + boxes */}
      <rect x={P - 4} y={cy - 18} width="4" height="36" fill="var(--bs-lime)" />
      <rect x={P} y={cy - boxH / 2} width="62" height={boxH} fill="none" stroke={line} strokeWidth="1.2" />
      <rect x={P} y={cy - sixH / 2} width="24" height={sixH} fill="none" stroke={line} strokeWidth="1.2" />
      <path d={`M ${P + 62} ${cy - 26} A 30 30 0 0 1 ${P + 62} ${cy + 26}`} fill="none" stroke={line} strokeWidth="1.2" />
      <circle cx={P + 41} cy={cy} r="1.6" fill="rgba(255,255,255,0.5)" />
      {/* right goal + boxes */}
      <rect x={W - P} y={cy - 18} width="4" height="36" fill="var(--bs-lime)" />
      <rect x={W - P - 62} y={cy - boxH / 2} width="62" height={boxH} fill="none" stroke={line} strokeWidth="1.2" />
      <rect x={W - P - 24} y={cy - sixH / 2} width="24" height={sixH} fill="none" stroke={line} strokeWidth="1.2" />
      <path d={`M ${W - P - 62} ${cy - 26} A 30 30 0 0 0 ${W - P - 62} ${cy + 26}`} fill="none" stroke={line} strokeWidth="1.2" />
      <circle cx={W - P - 41} cy={cy} r="1.6" fill="rgba(255,255,255,0.5)" />
      {/* shots */}
      {items.map(({ s, side, t }, i) => {
        const x = plotX(s.x, side), y = plotY(s.y);
        const col = both ? (t === m.h ? '#D1F206' : '#5B8DEF') : 'var(--bs-lime)';
        if (s.type === 'g') return <path key={i} d={starPath(x, y, 7)} fill={col} stroke="#0B0B0B" strokeWidth="0.6" />;
        if (s.type === 'on') return <circle key={i} cx={x} cy={y} r="5" fill={col} />;
        return <circle key={i} cx={x} cy={y} r="4.5" fill={both ? col : 'rgba(255,255,255,0.5)'} fillOpacity={both ? 0.4 : 1} />;
      })}
    </svg>
  );
}

function ShotKey() { return null; }

function StatsHero({ headless = false, light = false, solid = false }) {
  const [idx, setIdx] = React.useState(0);
  const [shotView, setShotView] = React.useState('h');
  const matches = PLm.matches;
  const m = matches[idx];
  const hc = PLm.T[m.h], ac = PLm.T[m.a];
  const go = (d) => { setIdx((i) => (i + d + matches.length) % matches.length); setShotView('h'); };
  const homeShots = m.shotMap, awayShots = m.shotMapAway;
  const viewShots = shotView === 'h' ? homeShots : shotView === 'a' ? awayShots : [...homeShots, ...awayShots];
  const cnt = (arr, ty) => arr.filter((s) => s.type === ty).length;
  const goals = cnt(viewShots, 'g'), on = cnt(viewShots, 'on'), off = cnt(viewShots, 'off');
  const both = shotView === 'both';
  const readoutName = both ? 'Both teams' : PLm.T[shotView === 'a' ? m.a : m.h].name;
  const xgVal = both ? (m.xg.h + m.xg.a).toFixed(2) : (shotView === 'a' ? m.xg.a : m.xg.h);
  const shotsVal = both ? (m.shots.h + m.shots.a) : (shotView === 'a' ? m.shots.a : m.shots.h);

  const ink = light ? 'var(--text-primary)' : 'var(--bs-white)';
  const inkDim = light ? 'var(--text-muted)' : 'rgba(255,255,255,0.55)';
  const liveLabel = light ? 'var(--bs-success)' : 'var(--bs-lime)';

  const Team = ({ code, sub, align }) => (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12, flexDirection: align === 'end' ? 'row-reverse' : 'row', flex: 1, minWidth: 0 }}>
      <TeamDisc code={code} size={40} ring />
      <div style={{ textAlign: align === 'end' ? 'right' : 'left', minWidth: 0 }}>
        <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 20, color: ink, textTransform: 'uppercase', lineHeight: 1, letterSpacing: '-0.01em' }}>{PLm.T[code].name}</div>
        <div style={{ fontFamily: 'var(--font-body)', fontSize: 11, letterSpacing: '0.07em', textTransform: 'uppercase', color: inkDim, marginTop: 5 }}>{sub}</div>
      </div>
    </div>
  );
  const Arrow = ({ d }) => (
    <button onClick={() => go(d)} className="bs-hero-arrow" style={{ width: 28, height: 28, borderRadius: '50%', border: `1px solid ${light ? 'var(--border-mid)' : 'rgba(255,255,255,0.25)'}`, background: light ? 'var(--bs-white)' : 'rgba(255,255,255,0.06)', color: light ? 'var(--text-brand)' : '#fff', cursor: 'pointer', display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
      <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round" style={{ transform: d < 0 ? 'scaleX(-1)' : 'none' }}><polyline points="9 6 15 12 9 18" /></svg>
    </button>
  );
  const card = light
    ? { background: 'var(--bs-neutral-100)', border: '1px solid var(--border-subtle)', borderRadius: 8, position: 'relative' }
    : { background: 'rgba(255,255,255,0.04)', border: '1px solid rgba(255,255,255,0.09)', borderRadius: 8, position: 'relative' };
  const ShotTab = ({ id, label }) => {
    const on = shotView === id;
    return (
      <button onClick={() => setShotView(id)} style={{
        fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 11, letterSpacing: '0.04em', padding: '5px 11px', borderRadius: 4, cursor: 'pointer',
        border: '1px solid', borderColor: on ? 'var(--bs-lime)' : (light ? 'var(--border-mid)' : 'rgba(255,255,255,0.2)'),
        background: on ? 'var(--bs-lime)' : 'transparent', color: on ? 'var(--bs-blue-navy)' : (light ? 'var(--text-muted)' : 'rgba(255,255,255,0.7)'),
      }}>{label}</button>
    );
  };
  const LegendDot = ({ c, ring, star, label }) => (
    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
      {star
        ? <svg width="13" height="13" viewBox="-7 -7 14 14"><path d={starPath(0, 0, 6.5)} fill={c} /></svg>
        : <span style={{ width: 11, height: 11, borderRadius: '50%', background: c, opacity: ring ? 0.4 : 1 }} />}
      <span style={{ fontFamily: 'var(--font-body)', fontSize: 11.5, color: light ? 'var(--text-body)' : 'rgba(255,255,255,0.7)' }}>{label}</span>
    </span>
  );

  return (
    <section className="gut" style={{ position: 'relative', background: light ? 'transparent' : solid ? 'var(--bs-blue-navy)' : 'var(--bs-blue-electric) url(assets/hero-bg.svg) center top / cover no-repeat', paddingTop: solid ? 20 : (headless && !light) ? 4 : (light ? 24 : 28), paddingBottom: solid ? 0 : (light ? 8 : 28), marginTop: (headless && !light && !solid) ? -2 : 0 }}>
      <div style={{ position: 'relative' }}>
        <div style={{ position: 'relative', background: light ? 'var(--bs-white)' : solid ? 'rgba(255,255,255,0.05)' : 'rgba(8,2,52,0.42)', backdropFilter: (light || solid) ? 'none' : 'blur(16px) saturate(135%)', WebkitBackdropFilter: (light || solid) ? 'none' : 'blur(16px) saturate(135%)', border: light ? '1px solid var(--border-subtle)' : solid ? '1px solid rgba(255,255,255,0.12)' : '1px solid rgba(255,255,255,0.16)', borderRadius: 10, padding: '18px 20px 20px', overflow: 'hidden', boxShadow: (light || solid) ? 'none' : '0 20px 50px rgba(5,0,40,0.32)' }}>
          {!light && !solid && <div style={{ position: 'absolute', top: -120, right: -80, width: 420, height: 420, borderRadius: '50%', background: 'radial-gradient(circle, rgba(58,26,255,0.38), transparent 70%)', pointerEvents: 'none' }} />}

          {!headless && (
          <div className="hero-top" style={{ position: 'relative', display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: 16, marginBottom: 14 }}>
            <div>
              <div style={{ display: 'inline-flex', alignItems: 'center', gap: 9, marginBottom: 10 }}>
                <span style={{ display: 'inline-block', width: 12, height: 16, background: 'var(--bs-lime)', transform: 'skewX(-18deg)' }} />
                <span style={{ fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 13, letterSpacing: '0.16em', textTransform: 'uppercase', color: 'var(--bs-lime)' }}>Stats Centre · {PLm.season} · Matchweek {PLm.matchweek}</span>
              </div>
              <h1 className="hero-h1" style={{ margin: 0, fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 48, lineHeight: '52px', letterSpacing: '-0.01em', textTransform: 'uppercase', color: 'var(--bs-white)' }}>Premier League</h1>
            </div>
            <span style={{ display: 'inline-flex', alignItems: 'center', gap: 9, border: '1px solid rgba(255,255,255,0.25)', borderRadius: 999, padding: '9px 16px', flex: '0 0 auto' }}>
              <span className="bs-live-dot" style={{ width: 9, height: 9, borderRadius: '50%', background: '#3DF07A' }} />
              <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 12, letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--bs-white)' }}>Live Stats Centre</span>
            </span>
          </div>
          )}
          {headless && (
          <div style={{ position: 'relative', display: 'inline-flex', alignItems: 'center', gap: 9, marginBottom: 14 }}>
            <span style={{ display: 'inline-block', width: 12, height: 16, background: 'var(--bs-lime)', transform: 'skewX(-18deg)' }} />
            <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 14, letterSpacing: '0.12em', textTransform: 'uppercase', color: ink }}>Live Now · Matchweek {PLm.matchweek}</span>
          </div>
          )}

          <div style={{ position: 'relative', display: 'grid', gridTemplateColumns: '1.5fr 1fr', gap: 14, alignItems: 'stretch' }} className="hero-grid">
            {/* LEFT */}
            <div style={{ ...card, padding: '16px 18px' }}>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 14 }}>
                <span style={{ display: 'inline-flex', alignItems: 'center', gap: 8, fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 13, letterSpacing: '0.1em', textTransform: 'uppercase', color: liveLabel }}>
                  <span className="bs-live-dot" style={{ width: 8, height: 8, borderRadius: '50%', background: '#3DF07A' }} />Live · {m.min}
                </span>
                <span style={{ display: 'inline-flex', alignItems: 'center', gap: 10 }}>
                  <span style={{ fontFamily: 'var(--font-mono)', fontSize: 12, color: inkDim }}>{idx + 1}/{matches.length} live</span>
                  <Arrow d={-1} /><Arrow d={1} />
                </span>
              </div>
              <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginBottom: 14 }}>
                <Team code={m.h} sub={`Home · ${m.hpos}`} align="start" />
                <div className="bs-matchscore" style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: 38, color: ink, lineHeight: 1, whiteSpace: 'nowrap', flex: '0 0 auto' }}>{m.hs}<span style={{ color: light ? 'var(--text-muted)' : 'rgba(255,255,255,0.32)', margin: '0 9px' }}>–</span>{m.as}</div>
                <Team code={m.a} sub={`Away · ${m.apos}`} align="end" />
              </div>
              <div style={{ background: light ? 'var(--bs-white)' : 'rgba(0,0,0,0.22)', border: light ? '1px solid var(--border-subtle)' : 'none', borderRadius: 8, padding: '10px 14px' }}>
                <MomentumChart m={m} light={light} />
              </div>
              <div style={{ marginTop: 14 }}>
                <CompareBar label="Expected Goals (xG)" h={m.xg.h} a={m.xg.a} light={light} />
                <CompareBar label="Shots (on target)" h={m.shots.h} a={m.shots.a} light={light} />
                <CompareBar label="Possession" h={m.poss.h} a={m.poss.a} suffix="%" light={light} />
              </div>
              <div style={{ marginTop: 14 }}>
                <WinProb m={m} hc={hc} ac={ac} light={light} />
              </div>
            </div>

            {/* RIGHT */}
            <div style={{ ...card, padding: '14px 16px', display: 'flex', flexDirection: 'column' }}>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 10, marginBottom: 12, flexWrap: 'wrap' }}>
                <div style={{ position: 'relative', display: 'inline-flex', alignItems: 'center' }}>
                  <FixedSlash />
                  <h3 style={{ position: 'relative', zIndex: 1, margin: 0, marginLeft: -12, fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 24, lineHeight: '40px', letterSpacing: '-0.01em', textTransform: 'uppercase', color: ink }}>Shot Map</h3>
                </div>
                <span style={{ display: 'inline-flex', gap: 6 }}>
                  <ShotTab id="h" label={hc.abbr} /><ShotTab id="a" label={ac.abbr} /><ShotTab id="both" label="Both" />
                </span>
              </div>
              <ShotMap m={m} view={shotView} />
              {/* legend */}
              <div style={{ display: 'flex', gap: 16, flexWrap: 'wrap', marginTop: 12 }}>
                {both ? (
                  <React.Fragment>
                    <LegendDot c="#D1F206" label={hc.name} />
                    <LegendDot c="#5B8DEF" label={ac.name} />
                    <LegendDot c="#fff" star label="Goal" />
                  </React.Fragment>
                ) : (
                  <React.Fragment>
                    <LegendDot c="var(--bs-lime)" label="On target" />
                    <LegendDot c="rgba(255,255,255,0.5)" label="Off target" />
                    <LegendDot c="var(--bs-lime)" star label="Goal" />
                  </React.Fragment>
                )}
              </div>
              <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginTop: 14 }}>
                <span style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: 26, color: ink, lineHeight: 1 }}>{xgVal}</span>
                <span style={{ fontFamily: 'var(--font-body)', fontSize: 11.5, color: inkDim }}>xG · {readoutName}<br />{goals} goals · {on} on target · {off} off from {shotsVal} shots</span>
              </div>
              {/* ratings */}
              <div style={{ marginTop: 12, borderTop: `1px solid ${light ? 'var(--border-subtle)' : 'rgba(255,255,255,0.1)'}`, paddingTop: 10 }}>
                {m.ratings.slice(0, 2).map((p) => (
                  <div key={p.name} style={{ display: 'flex', alignItems: 'center', gap: 11, padding: '5px 0' }}>
                    <Jersey code={p.t} number={p.n} size={30} />
                    <div style={{ flex: 1, minWidth: 0 }}>
                      <div style={{ fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: 13.5, color: ink }}>{p.name}</div>
                      <div style={{ fontFamily: 'var(--font-body)', fontSize: 11, color: inkDim, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{p.note}</div>
                    </div>
                    <span style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: 14, padding: '4px 9px', borderRadius: 3, background: p.top ? 'var(--bs-lime)' : (light ? 'var(--bs-neutral-200)' : 'rgba(255,255,255,0.1)'), color: p.top ? 'var(--bs-blue-navy)' : ink }}>{p.r}</span>
                  </div>
                ))}
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

function WinProb({ m, hc, ac, light = false }) {
  const hv = light ? 'var(--text-brand)' : 'var(--bs-lime)';
  const av = light ? '#2A6FDB' : '#5B8DEF';
  const drawSeg = light ? 'var(--bs-neutral-300)' : 'rgba(255,255,255,0.3)';
  return (
    <div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 9, marginBottom: 8 }}>
        <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 11, letterSpacing: '0.1em', textTransform: 'uppercase', color: light ? 'var(--text-muted)' : 'rgba(255,255,255,0.7)' }}>Live Win Probability</span>
      </div>
      <div style={{ display: 'flex', height: 10, borderRadius: 999, overflow: 'hidden', gap: 2 }}>
        <div style={{ width: m.winProb.h + '%', background: 'var(--bs-lime)' }} />
        <div style={{ width: m.winProb.d + '%', background: drawSeg }} />
        <div style={{ width: m.winProb.a + '%', background: '#5B8DEF' }} />
      </div>
      <div style={{ display: 'flex', justifyContent: 'space-between', marginTop: 7 }}>
        <span style={{ fontFamily: 'var(--font-body)', fontSize: 12, color: hv, fontWeight: 600 }}>{hc.name} {m.winProb.h}%</span>
        <span style={{ fontFamily: 'var(--font-body)', fontSize: 12, color: light ? 'var(--text-muted)' : 'rgba(255,255,255,0.6)', fontWeight: 600 }}>Draw {m.winProb.d}%</span>
        <span style={{ fontFamily: 'var(--font-body)', fontSize: 12, color: av, fontWeight: 600 }}>{ac.name} {m.winProb.a}%</span>
      </div>
    </div>
  );
}

Object.assign(window, { StatsHero });
