// 777 Strike — game logic, reels, screens.

const SYM = window.SYMBOLS;
const SYMBOL_ORDER = ['WILD', 'S7R', 'S7G', 'S7B', 'LEMON', 'CHRY', 'A', 'K', 'Q', 'J'];
const REEL_LEN = 30;

// Build a randomized reel strip with realistic weights (low symbols common, high rare).
function buildStrip(seed = 0) {
  const weights = { WILD: 1, S7R: 1, S7G: 2, S7B: 2, LEMON: 3, CHRY: 4, A: 5, K: 5, Q: 6, J: 6, FS: 1 };
  const pool = [];
  for (const [k, w] of Object.entries(weights)) {
    for (let i = 0; i < w; i++) pool.push(k);
  }
  // Deterministic-ish shuffle
  const out = [];
  let s = seed * 7919 + 1;
  for (let i = 0; i < REEL_LEN; i++) {
    s = (s * 16807) % 2147483647;
    out.push(pool[s % pool.length]);
  }
  return out;
}

const SYMBOL_PX = 86; // height of each symbol cell

function SymbolCell({ symId, dim, highlight }) {
  const sym = SYM[symId];
  if (!sym) return null;
  const Render = sym.render;
  return (
    <div style={{
      height: SYMBOL_PX,
      width: '100%',
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center',
      padding: 2,
      boxSizing: 'border-box',
      opacity: dim ? 0.3 : 1,
      filter: highlight ? 'drop-shadow(0 0 8px rgba(255,210,80,0.95)) drop-shadow(0 0 14px rgba(255,180,0,0.7))' : 'none',
      animation: highlight ? 'goldPulse 0.9s ease-in-out infinite' : 'none',
      transition: 'opacity 200ms',
    }}>
      <Render/>
    </div>
  );
}

// Single reel column with animated strip.
function Reel({ strip, finalIndex, spinning, delay, duration, highlightRows }) {
  // finalIndex = strip index that should appear in the MIDDLE row (row 1 of 3 visible).
  // We render 5 rows from the strip: [final-1, final, final+1] visible + buffer above/below.
  const visible = [-1, 0, 1].map(off => {
    const i = ((finalIndex + off) % strip.length + strip.length) % strip.length;
    return strip[i];
  });
  // Spinning blur: render a vertical stack with translateY animated.
  const [phase, setPhase] = React.useState('idle'); // idle | accel | decel | done
  const stackRef = React.useRef(null);

  React.useEffect(() => {
    if (!spinning) return;
    setPhase('accel');
    const stack = stackRef.current;
    if (!stack) return;
    stack.style.transition = 'none';
    stack.style.transform = 'translateY(0)';
    // force reflow
    void stack.offsetHeight;
    const totalDist = SYMBOL_PX * (20 + Math.floor(Math.random() * 6) + finalIndex * 0.1);
    requestAnimationFrame(() => {
      stack.style.transition = `transform ${duration}ms cubic-bezier(0.2, 0.6, 0.2, 1) ${delay}ms`;
      stack.style.transform = `translateY(${totalDist}px)`;
    });
    const t = setTimeout(() => {
      setPhase('done');
    }, delay + duration + 50);
    return () => clearTimeout(t);
  }, [spinning, finalIndex, delay, duration]);

  return (
    <div style={{
      flex: 1,
      height: SYMBOL_PX * 3,
      overflow: 'hidden',
      background: 'linear-gradient(180deg, #f5f4ef 0%, #e9e7df 50%, #d6d5cf 100%)',
      borderLeft: '1px solid rgba(0,0,0,0.08)',
      borderRight: '1px solid rgba(0,0,0,0.08)',
      position: 'relative',
    }}>
      {/* Top + bottom subtle gradient masks */}
      <div style={{
        position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 2,
        background: 'linear-gradient(180deg, rgba(255,255,255,0.5) 0%, transparent 12%, transparent 88%, rgba(0,0,0,0.12) 100%)',
      }}/>
      {spinning ? (
        <div key="spinning" ref={stackRef} style={{ position: 'relative', top: -SYMBOL_PX * 20 }}>
          {Array.from({ length: 22 }, (_, i) => {
            const symId = strip[(finalIndex + i) % strip.length];
            return (
              <div key={i} style={{
                height: SYMBOL_PX,
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                filter: i < 18 ? 'blur(2px)' : 'none',
              }}>
                <SymbolCell symId={symId}/>
              </div>
            );
          })}
        </div>
      ) : (
        <div key="static" style={{ transform: 'none', transition: 'none' }}>
          {visible.map((symId, row) => (
            <SymbolCell key={row} symId={symId} highlight={highlightRows && highlightRows.includes(row)}/>
          ))}
        </div>
      )}
    </div>
  );
}

// Paytable strip shown to the right of the logo in the header.
function PaytableStrip({ compact, onClick }) {
  const rows1 = ['WILD', 'S7R', 'S7G', 'S7B', 'LEMON'];
  const rows2 = ['CHRY', 'A', 'K', 'Q', 'J'];
  const Cell = ({ id }) => {
    const s = SYM[id];
    return (
      <div style={{
        flex: 1,
        display: 'flex',
        gap: 4,
        alignItems: 'center',
        justifyContent: 'flex-start',
        padding: '3px 5px',
        borderRight: '1px solid rgba(255,220,140,0.1)',
        position: 'relative',
        background: 'linear-gradient(180deg, #3a2410 0%, #1f1408 100%)',
      }}>
        <div style={{ width: 22, height: 22, flexShrink: 0 }}>
          <s.render/>
        </div>
        <div style={{
          fontFamily: 'Inter, sans-serif',
          fontSize: 9,
          fontWeight: 700,
          color: '#fff',
          lineHeight: '11px',
          textShadow: '0 1px 0 rgba(0,0,0,0.6)',
        }}>
          {s.payouts.map((p, i) => p > 0 ? <div key={i}>x{p}</div> : null)}
        </div>
      </div>
    );
  };
  return (
    <div onClick={onClick} style={{
      flex: 1,
      borderRadius: 4,
      border: '1.5px solid #b58b3a',
      overflow: 'hidden',
      background: '#1a0f06',
      boxShadow: '0 2px 4px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,220,140,0.2)',
      display: 'flex',
      flexDirection: 'column',
    }}>
      <div style={{ display: 'flex', borderBottom: '1px solid rgba(181,139,58,0.5)' }}>
        {rows1.map(id => <Cell key={id} id={id}/>)}
      </div>
      <div style={{ display: 'flex' }}>
        {rows2.map(id => <Cell key={id} id={id}/>)}
      </div>
    </div>
  );
}

// SVG icons (sound, menu, etc.)
const Icons = {
  speakerOn: () => (
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none">
      <path d="M3 10v4h4l5 4V6L7 10H3z" fill="#fff"/>
      <path d="M16 8c1.5 1 2.5 2.5 2.5 4S17.5 15 16 16" stroke="#fff" strokeWidth="1.6" strokeLinecap="round" fill="none"/>
      <path d="M18.5 5c3 2 5 4.5 5 7s-2 5-5 7" stroke="#fff" strokeWidth="1.6" strokeLinecap="round" fill="none"/>
    </svg>
  ),
  speakerOff: () => (
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none">
      <path d="M3 10v4h4l5 4V6L7 10H3z" fill="#fff"/>
      <line x1="15" y1="8" x2="22" y2="15" stroke="#fff" strokeWidth="1.8" strokeLinecap="round"/>
      <line x1="22" y1="8" x2="15" y2="15" stroke="#fff" strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  ),
  menu: () => (
    <svg viewBox="0 0 24 24" width="24" height="24">
      <rect x="3" y="6" width="18" height="1.8" rx="0.9" fill="#fff"/>
      <rect x="3" y="11.1" width="18" height="1.8" rx="0.9" fill="#fff"/>
      <rect x="3" y="16.2" width="18" height="1.8" rx="0.9" fill="#fff"/>
    </svg>
  ),
  spin: () => (
    <svg viewBox="0 0 60 60" width="56" height="56" fill="none">
      {/* Two chasing arrows around the circle, drawn as thick arcs with arrowheads */}
      <g stroke="#3a2410" strokeWidth="6.5" strokeLinecap="round" fill="none">
        <path d="M 16 14 A 18 18 0 0 1 46 24"/>
        <path d="M 44 46 A 18 18 0 0 1 14 36"/>
      </g>
      {/* arrowheads */}
      <polygon points="42,28 52,22 50,32" fill="#3a2410"/>
      <polygon points="18,32 8,38 10,28" fill="#3a2410"/>
    </svg>
  ),
  turbo: () => (
    <svg viewBox="0 0 24 24" width="20" height="20">
      <path d="M 12 3 A 9 9 0 1 0 21 12" stroke="#f0c040" strokeWidth="2" fill="none" strokeLinecap="round"/>
      <path d="M 12 12 L 14 6" stroke="#f0c040" strokeWidth="2" strokeLinecap="round"/>
      <circle cx="12" cy="12" r="1.6" fill="#f0c040"/>
    </svg>
  ),
  home: () => (
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="#aaa" strokeWidth="1.7">
      <path d="M 3 11 L 12 3 L 21 11 V 20 H 14 V 14 H 10 V 20 H 3 Z" strokeLinejoin="round"/>
    </svg>
  ),
  dice: () => (
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="#aaa" strokeWidth="1.7">
      <rect x="3.5" y="3.5" width="17" height="17" rx="2.5"/>
      <circle cx="8.5" cy="8.5" r="1.2" fill="#aaa"/>
      <circle cx="15.5" cy="8.5" r="1.2" fill="#aaa"/>
      <circle cx="12" cy="12" r="1.2" fill="#aaa"/>
      <circle cx="8.5" cy="15.5" r="1.2" fill="#aaa"/>
      <circle cx="15.5" cy="15.5" r="1.2" fill="#aaa"/>
    </svg>
  ),
  news: () => (
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="#aaa" strokeWidth="1.7">
      <rect x="3.5" y="4" width="17" height="16" rx="1.5"/>
      <line x1="7" y1="9" x2="17" y2="9"/>
      <line x1="7" y1="12.5" x2="17" y2="12.5"/>
      <line x1="7" y1="16" x2="13" y2="16"/>
    </svg>
  ),
  menuBottom: () => (
    <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="#aaa" strokeWidth="1.7">
      <line x1="4" y1="7" x2="17" y2="7"/>
      <line x1="4" y1="12" x2="17" y2="12"/>
      <line x1="4" y1="17" x2="17" y2="17"/>
      <path d="M 20 4 L 20 20" strokeLinecap="round"/>
    </svg>
  ),
  close: () => (
    <svg viewBox="0 0 24 24" width="20" height="20" stroke="#fff" strokeWidth="2" strokeLinecap="round">
      <line x1="5" y1="5" x2="19" y2="19"/>
      <line x1="19" y1="5" x2="5" y2="19"/>
    </svg>
  ),
  chevronLeft: () => (
    <svg viewBox="0 0 24 24" width="22" height="22" stroke="#fff" strokeWidth="3" fill="none" strokeLinecap="round" strokeLinejoin="round">
      <polyline points="15 6 9 12 15 18"/>
    </svg>
  ),
  chevronRight: () => (
    <svg viewBox="0 0 24 24" width="22" height="22" stroke="#fff" strokeWidth="3" fill="none" strokeLinecap="round" strokeLinejoin="round">
      <polyline points="9 6 15 12 9 18"/>
    </svg>
  ),
  play: () => (
    <svg viewBox="0 0 24 24" width="22" height="22">
      <polygon points="6 4 22 12 6 20" fill="#5a3a0a"/>
    </svg>
  ),
};

// Crown-pattern background
function CrownBgPattern({ opacity = 0.06 }) {
  // Renders an SVG pattern of small crowns repeated across the bg.
  return (
    <svg width="100%" height="100%" style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }} preserveAspectRatio="xMidYMid slice">
      <defs>
        <pattern id="crown-pat" width="42" height="42" patternUnits="userSpaceOnUse">
          <g transform="translate(8,12) scale(0.7)" fill="rgba(255,255,255,1)" opacity={opacity}>
            <path d="M 0 16 L 0 6 L 6 12 L 12 0 L 18 12 L 24 6 L 24 16 L 0 16 Z"/>
          </g>
          <g transform="translate(28,30) scale(0.5)" fill="rgba(255,255,255,1)" opacity={opacity}>
            <path d="M 0 16 L 0 6 L 6 12 L 12 0 L 18 12 L 24 6 L 24 16 L 0 16 Z"/>
          </g>
        </pattern>
      </defs>
      <rect width="100%" height="100%" fill="url(#crown-pat)"/>
    </svg>
  );
}

// Green pill button used in bottom controls.
function GreenPill({ children, dim, small, onClick, active }) {
  return (
    <button onClick={onClick} style={{
      border: 'none',
      cursor: 'pointer',
      padding: small ? '8px 14px' : '10px 18px',
      borderRadius: 999,
      background: active
        ? 'linear-gradient(180deg, #f0c040 0%, #c98a1a 100%)'
        : 'linear-gradient(180deg, #1a6e3e 0%, #0d4827 100%)',
      color: active ? '#3a2410' : '#fff',
      fontFamily: 'Inter, sans-serif',
      fontSize: 11,
      fontWeight: 700,
      letterSpacing: 0.5,
      textTransform: 'uppercase',
      boxShadow: '0 2px 0 rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.15), 0 0 0 1.5px rgba(255,220,140,0.25)',
      opacity: dim ? 0.5 : 1,
      display: 'flex',
      flexDirection: 'column',
      alignItems: 'center',
      justifyContent: 'center',
      minWidth: 90,
      lineHeight: 1.1,
    }}>{children}</button>
  );
}

// Big spin button (yellow disc with arrows).
function SpinDisc({ onClick, spinning, freeSpinsLeft }) {
  return (
    <button onClick={onClick} disabled={spinning} style={{
      width: 92, height: 92, borderRadius: '50%',
      border: 'none', cursor: spinning ? 'default' : 'pointer',
      background: 'radial-gradient(circle at 35% 30%, #fff8c0 0%, #f0c040 40%, #c98a1a 90%, #8a560c 100%)',
      boxShadow: '0 4px 0 rgba(0,0,0,0.45), inset 0 -4px 8px rgba(120,70,10,0.5), inset 0 4px 8px rgba(255,255,255,0.5), 0 0 0 3px rgba(0,0,0,0.45)',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      position: 'relative',
      animation: spinning ? 'spinDisc 0.8s linear infinite' : 'none',
    }}>
      <Icons.spin/>
      {freeSpinsLeft > 0 && (
        <div style={{
          position: 'absolute', top: -10, right: -10,
          background: '#e21f2c', color: '#fff',
          borderRadius: 999, padding: '2px 8px',
          fontSize: 12, fontWeight: 900,
          border: '2px solid #fff',
          boxShadow: '0 2px 4px rgba(0,0,0,0.4)',
        }}>{freeSpinsLeft}</div>
      )}
    </button>
  );
}

// Big gold win number (centered over reels area).
function WinNumber({ amount }) {
  return (
    <div style={{
      position: 'absolute', left: 0, right: 0, top: '52%',
      display: 'flex', justifyContent: 'center',
      pointerEvents: 'none', zIndex: 10,
      animation: 'bigWinPop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1)',
    }}>
      <div style={{
        fontFamily: "'Bowlby One', sans-serif",
        fontSize: 64,
        fontWeight: 900,
        background: 'linear-gradient(180deg, #fff4a8 0%, #f0c040 35%, #c98a1a 60%, #fff3c0 75%, #c98a1a 100%)',
        WebkitBackgroundClip: 'text',
        WebkitTextFillColor: 'transparent',
        backgroundClip: 'text',
        textShadow: '0 0 24px rgba(255,200,60,0.7)',
        filter: 'drop-shadow(0 4px 0 rgba(90,58,10,0.9)) drop-shadow(0 0 20px rgba(255,200,60,0.6))',
        letterSpacing: 2,
      }}>{amount.toFixed(2)}</div>
    </div>
  );
}

// Coin rain for big win.
function CoinRain({ active }) {
  if (!active) return null;
  const coins = Array.from({ length: 30 }, (_, i) => ({
    id: i,
    left: Math.random() * 100,
    delay: Math.random() * 1.5,
    duration: 1.5 + Math.random() * 1.5,
    size: 16 + Math.random() * 14,
  }));
  return (
    <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none', overflow: 'hidden', zIndex: 8 }}>
      {coins.map(c => (
        <div key={c.id} style={{
          position: 'absolute',
          left: `${c.left}%`,
          top: 0,
          width: c.size, height: c.size,
          borderRadius: '50%',
          background: 'radial-gradient(circle at 35% 30%, #fff8c0, #f0c040 50%, #8a560c)',
          boxShadow: '0 0 8px rgba(255,200,60,0.7)',
          animation: `coinRain ${c.duration}s ease-in ${c.delay}s forwards`,
        }}/>
      ))}
    </div>
  );
}

// Sidebar menu (slide-in).
function SidebarMenu({ open, onClose, onNav, sound, onSound }) {
  return (
    <>
      <div onClick={onClose} style={{
        position: 'absolute', inset: 0,
        background: open ? 'rgba(0,0,0,0.5)' : 'transparent',
        zIndex: 99, opacity: open ? 1 : 0, transition: 'opacity 240ms',
        pointerEvents: open ? 'auto' : 'none',
      }}/>
      <div style={{
        position: 'absolute', top: 0, right: 0, bottom: 0,
        width: 240,
        background: 'linear-gradient(180deg, #14502e 0%, #0a2f1c 100%)',
        boxShadow: '-6px 0 24px rgba(0,0,0,0.5)',
        transform: open ? 'translateX(0)' : 'translateX(100%)',
        transition: 'transform 280ms cubic-bezier(0.22, 1, 0.36, 1)',
        zIndex: 100,
        display: 'flex', flexDirection: 'column',
        paddingTop: 12,
        color: '#fff',
        fontFamily: 'Inter, sans-serif',
      }}>
        <div style={{ display: 'flex', justifyContent: 'flex-end', padding: '8px 14px' }}>
          <button onClick={onClose} style={{ background: 'rgba(255,255,255,0.1)', border: '1.5px solid rgba(255,220,140,0.3)', borderRadius: 999, padding: 8, cursor: 'pointer' }}>
            <Icons.close/>
          </button>
        </div>
        <div style={{ padding: '0 18px 18px', borderBottom: '1px solid rgba(255,220,140,0.15)' }}>
          <div style={{ fontSize: 10, letterSpacing: 1, color: '#f4c243', fontWeight: 700 }}>BALANCE</div>
          <div style={{ fontSize: 22, fontWeight: 800 }}>1,009.00 <span style={{ fontSize: 12, opacity: 0.7 }}>BGN</span></div>
        </div>
        {[
          ['Pay table', 'paytable'],
          ['Game rules', null],
          ['Bet history', null],
          ['Game info', null],
          ['Lobby', 'lobby'],
        ].map(([label, target]) => (
          <button key={label} onClick={() => target ? onNav(target) : null} style={{
            background: 'transparent', border: 'none',
            textAlign: 'left', padding: '14px 18px',
            color: '#fff', fontSize: 14, fontWeight: 600,
            borderBottom: '1px solid rgba(255,255,255,0.06)',
            cursor: target ? 'pointer' : 'default',
            opacity: target ? 1 : 0.6,
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
          }}>
            <span>{label}</span>
            <span style={{ color: '#f4c243' }}>›</span>
          </button>
        ))}
        <div style={{ padding: '14px 18px', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
          <span style={{ fontSize: 14, fontWeight: 600 }}>Sound</span>
          <button onClick={onSound} style={{
            width: 46, height: 26, borderRadius: 999,
            background: sound ? '#1a6e3e' : 'rgba(255,255,255,0.18)',
            border: '1.5px solid rgba(255,220,140,0.4)',
            position: 'relative', cursor: 'pointer',
          }}>
            <div style={{
              position: 'absolute', top: 1.5, left: sound ? 22 : 2,
              width: 19, height: 19, borderRadius: '50%',
              background: '#fff', transition: 'left 180ms',
            }}/>
          </button>
        </div>
        <div style={{ marginTop: 'auto', padding: '12px 18px', fontSize: 10, opacity: 0.5 }}>
          v 2.4.7 · 777 Strike · Red Tiger Gaming
        </div>
      </div>
    </>
  );
}

// Stake selector modal.
function StakeModal({ open, value, onChange, onClose }) {
  const stakes = [0.20, 0.50, 1.00, 2.00, 5.00, 10.00, 20.00, 50.00, 100.00];
  if (!open) return null;
  return (
    <div onClick={onClose} style={{
      position: 'absolute', inset: 0, zIndex: 110,
      background: 'rgba(0,0,0,0.65)',
      display: 'flex', alignItems: 'flex-end',
    }}>
      <div onClick={e => e.stopPropagation()} style={{
        width: '100%',
        background: 'linear-gradient(180deg, #14502e 0%, #0a2f1c 100%)',
        borderTopLeftRadius: 18, borderTopRightRadius: 18,
        boxShadow: '0 -10px 30px rgba(0,0,0,0.5)',
        padding: 20, paddingTop: 14,
        borderTop: '2px solid rgba(255,220,140,0.4)',
      }}>
        <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 8 }}>
          <div style={{ width: 50, height: 5, borderRadius: 999, background: 'rgba(255,255,255,0.25)' }}/>
        </div>
        <div style={{ fontFamily: 'Inter', fontSize: 11, color: '#f4c243', letterSpacing: 2, fontWeight: 700, textAlign: 'center' }}>STAKE PER SPIN</div>
        <div style={{ fontFamily: "'Bowlby One', sans-serif", fontSize: 36, color: '#fff', textAlign: 'center', marginTop: 4, marginBottom: 14, textShadow: '0 2px 0 rgba(0,0,0,0.4)' }}>
          {value.toFixed(2)}
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 8 }}>
          {stakes.map(s => {
            const sel = Math.abs(s - value) < 0.001;
            return (
              <button key={s} onClick={() => onChange(s)} style={{
                padding: '12px 0',
                borderRadius: 10,
                border: sel ? '2px solid #f4c243' : '1.5px solid rgba(255,220,140,0.25)',
                background: sel
                  ? 'linear-gradient(180deg, #f0c040 0%, #c98a1a 100%)'
                  : 'linear-gradient(180deg, #1a6e3e 0%, #0d4827 100%)',
                color: sel ? '#3a2410' : '#fff',
                fontFamily: 'Inter', fontSize: 15, fontWeight: 800,
                cursor: 'pointer',
                boxShadow: '0 2px 0 rgba(0,0,0,0.35)',
              }}>{s.toFixed(2)}</button>
            );
          })}
        </div>
        <button onClick={onClose} style={{
          marginTop: 16,
          width: '100%',
          padding: 14,
          borderRadius: 999,
          border: 'none',
          background: 'linear-gradient(180deg, #f0c040 0%, #c98a1a 100%)',
          color: '#3a2410',
          fontWeight: 900, fontSize: 14, letterSpacing: 1.5,
          textTransform: 'uppercase',
          cursor: 'pointer',
          boxShadow: '0 4px 0 rgba(0,0,0,0.4)',
        }}>Confirm</button>
      </div>
    </div>
  );
}

// Autoplay modal.
function AutoplayModal({ open, value, onChange, onStart, onClose }) {
  const counts = [10, 25, 50, 100, 250, 500];
  if (!open) return null;
  return (
    <div onClick={onClose} style={{
      position: 'absolute', inset: 0, zIndex: 110,
      background: 'rgba(0,0,0,0.65)',
      display: 'flex', alignItems: 'flex-end',
    }}>
      <div onClick={e => e.stopPropagation()} style={{
        width: '100%',
        background: 'linear-gradient(180deg, #14502e 0%, #0a2f1c 100%)',
        borderTopLeftRadius: 18, borderTopRightRadius: 18,
        padding: 20, paddingTop: 14,
        borderTop: '2px solid rgba(255,220,140,0.4)',
      }}>
        <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 8 }}>
          <div style={{ width: 50, height: 5, borderRadius: 999, background: 'rgba(255,255,255,0.25)' }}/>
        </div>
        <div style={{ fontSize: 11, color: '#f4c243', letterSpacing: 2, fontWeight: 700, textAlign: 'center' }}>AUTOPLAY</div>
        <div style={{ fontSize: 12, color: '#fff', opacity: 0.75, textAlign: 'center', marginTop: 4, marginBottom: 14 }}>
          Number of spins
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 8 }}>
          {counts.map(c => {
            const sel = c === value;
            return (
              <button key={c} onClick={() => onChange(c)} style={{
                padding: '14px 0', borderRadius: 10,
                border: sel ? '2px solid #f4c243' : '1.5px solid rgba(255,220,140,0.25)',
                background: sel
                  ? 'linear-gradient(180deg, #f0c040 0%, #c98a1a 100%)'
                  : 'linear-gradient(180deg, #1a6e3e 0%, #0d4827 100%)',
                color: sel ? '#3a2410' : '#fff',
                fontSize: 16, fontWeight: 800,
                cursor: 'pointer',
              }}>{c}</button>
            );
          })}
        </div>
        <div style={{ marginTop: 14, paddingTop: 14, borderTop: '1px solid rgba(255,220,140,0.15)' }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '8px 0' }}>
            <span style={{ color: '#fff', fontSize: 13 }}>Stop if single win exceeds</span>
            <span style={{ color: '#f4c243', fontWeight: 800 }}>50.00</span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '8px 0' }}>
            <span style={{ color: '#fff', fontSize: 13 }}>Stop on any win</span>
            <div style={{ width: 40, height: 22, borderRadius: 999, background: 'rgba(255,255,255,0.15)', position: 'relative' }}>
              <div style={{ position: 'absolute', top: 1.5, left: 1.5, width: 19, height: 19, borderRadius: '50%', background: '#fff' }}/>
            </div>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '8px 0' }}>
            <span style={{ color: '#fff', fontSize: 13 }}>Stop on free spins</span>
            <div style={{ width: 40, height: 22, borderRadius: 999, background: '#1a6e3e', position: 'relative' }}>
              <div style={{ position: 'absolute', top: 1.5, left: 19, width: 19, height: 19, borderRadius: '50%', background: '#fff' }}/>
            </div>
          </div>
        </div>
        <button onClick={onStart} style={{
          marginTop: 16, width: '100%', padding: 14, borderRadius: 999, border: 'none',
          background: 'linear-gradient(180deg, #f0c040 0%, #c98a1a 100%)',
          color: '#3a2410', fontWeight: 900, fontSize: 14, letterSpacing: 1.5,
          textTransform: 'uppercase', cursor: 'pointer',
          boxShadow: '0 4px 0 rgba(0,0,0,0.4)',
        }}>Start autoplay</button>
      </div>
    </div>
  );
}

// Splash / intro overlay.
function SplashOverlay({ onPlay, onPrev, onNext, slide = 0 }) {
  const slides = [
    {
      title: '7th Heaven',
      icons: ['FS', 'S7R', 'WILD'],
      iconLabels: ['Free\nSpins', 'Win\nSpins', 'Wild\nSpins'],
      blurb: '3 Free Spins symbols on the 1st, 3rd and 5th reel trigger Free Spins! When RETRIGGERED the Free Spins mode UPGRADES for the chance of even BIGGER WINS!',
    },
    {
      title: 'Wild Power',
      icons: ['WILD', 'WILD', 'WILD'],
      iconLabels: ['Wild', 'x2', 'x3'],
      blurb: 'WILD symbols substitute for all symbols except Free Spins. During Free Spins, Wilds carry a 2x or 3x multiplier for massive wins!',
    },
    {
      title: 'Royal Pays',
      icons: ['A', 'K', 'Q'],
      iconLabels: ['x77', 'x47', 'x35'],
      blurb: 'Match 5 of a kind across paylines for up to 77x your stake. The crown letters and 7s are the high-value symbols.',
    },
  ];
  const s = slides[slide];
  return (
    <div style={{
      position: 'absolute', inset: 0, zIndex: 90,
      background: 'linear-gradient(180deg, #14502e 0%, #0a2f1c 100%)',
      display: 'flex', flexDirection: 'column',
      paddingTop: 80,
    }}>
      <CrownBgPattern opacity={0.05}/>
      <div style={{
        flex: 1,
        display: 'flex', flexDirection: 'column',
        alignItems: 'center', justifyContent: 'center',
        padding: '0 24px',
        position: 'relative',
      }}>
        <div style={{
          fontFamily: 'Cinzel, serif',
          fontSize: 44, fontWeight: 900, fontStyle: 'italic',
          background: 'linear-gradient(180deg, #fff4a8 0%, #f0c040 50%, #c98a1a 100%)',
          WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent',
          backgroundClip: 'text',
          filter: 'drop-shadow(0 3px 0 rgba(90,58,10,0.6))',
          marginBottom: 36,
        }}>{s.title}</div>
        <div style={{ display: 'flex', gap: 8, alignItems: 'flex-end', marginBottom: 28 }}>
          {s.icons.map((id, i) => (
            <div key={i} style={{
              width: i === 1 ? 110 : 90,
              height: i === 1 ? 110 : 90,
              position: 'relative',
              transform: i === 1 ? 'translateY(-12px) scale(1.05)' : 'none',
              filter: 'drop-shadow(0 6px 12px rgba(0,0,0,0.5))',
            }}>
              {SYM[id] && React.createElement(SYM[id].render)}
              {i < s.icons.length - 1 && (
                <svg viewBox="0 0 40 40" width="20" height="20" style={{
                  position: 'absolute', right: -14, bottom: -8, zIndex: 2,
                }}>
                  <path d="M 4 14 Q 14 34 34 22 L 28 18 L 34 22 L 30 30"
                    stroke="#f0c040" strokeWidth="3" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
                </svg>
              )}
            </div>
          ))}
        </div>
        <div style={{
          maxWidth: 320, textAlign: 'center',
          fontSize: 14, color: '#fff', lineHeight: 1.45,
          textShadow: '0 1px 0 rgba(0,0,0,0.5)',
        }}>{s.blurb}</div>
        <div style={{ marginTop: 8, color: 'rgba(244,194,67,0.8)', fontSize: 11, letterSpacing: 2, fontWeight: 700 }}>
          {slide + 1} / {slides.length}
        </div>
      </div>
      <div style={{
        display: 'flex', gap: 14, alignItems: 'center', justifyContent: 'center',
        paddingBottom: 30,
      }}>
        <button onClick={onPrev} style={{
          width: 56, height: 56, borderRadius: '50%',
          border: 'none', cursor: 'pointer',
          background: 'linear-gradient(180deg, #1a6e3e 0%, #0d4827 100%)',
          boxShadow: '0 4px 0 rgba(0,0,0,0.45), inset 0 1px 0 rgba(255,255,255,0.15)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}><Icons.chevronLeft/></button>
        <button onClick={onPlay} style={{
          padding: '14px 64px', borderRadius: 999, border: 'none', cursor: 'pointer',
          background: 'linear-gradient(180deg, #fff4a8 0%, #f0c040 50%, #c98a1a 100%)',
          color: '#3a2410',
          fontFamily: 'Inter, sans-serif',
          fontSize: 20, fontWeight: 900, letterSpacing: 4,
          boxShadow: '0 5px 0 rgba(0,0,0,0.45), inset 0 -3px 6px rgba(120,70,10,0.4), inset 0 2px 4px rgba(255,255,255,0.5)',
        }}>PLAY</button>
        <button onClick={onNext} style={{
          width: 56, height: 56, borderRadius: '50%',
          border: 'none', cursor: 'pointer',
          background: 'linear-gradient(180deg, #1a6e3e 0%, #0d4827 100%)',
          boxShadow: '0 4px 0 rgba(0,0,0,0.45), inset 0 1px 0 rgba(255,255,255,0.15)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}><Icons.chevronRight/></button>
      </div>
    </div>
  );
}

// Full-screen paytable view.
function PaytableScreen({ onClose }) {
  const rows = [
    { syms: ['WILD'], payouts: [['5x', '77'], ['4x', '—'], ['3x', '—']], label: 'WILD substitutes for any symbol except Free Spins.' },
    { syms: ['S7R'], payouts: [['5x', '77'], ['4x', '21'], ['3x', '10']] },
    { syms: ['S7G'], payouts: [['5x', '47'], ['4x', '15'], ['3x', '7']] },
    { syms: ['S7B'], payouts: [['5x', '35'], ['4x', '11'], ['3x', '5']] },
    { syms: ['LEMON'], payouts: [['5x', '23'], ['4x', '7'], ['3x', '3']] },
    { syms: ['CHRY'], payouts: [['5x', '9.2'], ['4x', '4.6'], ['3x', '2.3']] },
    { syms: ['A'], payouts: [['5x', '5.2'], ['4x', '2.6'], ['3x', '1.3']] },
    { syms: ['K'], payouts: [['5x', '3.6'], ['4x', '1.8'], ['3x', '0.9']] },
    { syms: ['Q'], payouts: [['5x', '2.8'], ['4x', '1.4'], ['3x', '0.7']] },
    { syms: ['J'], payouts: [['5x', '2.4'], ['4x', '1.2'], ['3x', '0.6']] },
    { syms: ['FS'], payouts: [['Scatter', 'triggers Free Spins']], label: '3 Free Spins on reels 1, 3 & 5 trigger the bonus.' },
  ];
  return (
    <div style={{
      position: 'absolute', inset: 0, zIndex: 95,
      background: 'linear-gradient(180deg, #0a2f1c 0%, #04180e 100%)',
      display: 'flex', flexDirection: 'column',
    }}>
      <div style={{
        padding: '14px 18px',
        display: 'flex', alignItems: 'center',
        borderBottom: '1px solid rgba(255,220,140,0.2)',
        background: 'rgba(0,0,0,0.3)',
      }}>
        <button onClick={onClose} style={{
          width: 38, height: 38, borderRadius: '50%',
          background: 'rgba(255,255,255,0.08)', border: '1.5px solid rgba(255,220,140,0.3)',
          display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer',
        }}><Icons.chevronLeft/></button>
        <div style={{
          flex: 1, textAlign: 'center',
          fontFamily: 'Cinzel, serif', fontWeight: 900, fontSize: 18,
          color: '#f4c243', letterSpacing: 3, textTransform: 'uppercase',
          marginRight: 38,
        }}>Paytable</div>
      </div>
      <div style={{ flex: 1, overflow: 'auto', padding: '14px 16px 30px' }}>
        <div style={{
          fontSize: 11, color: '#fff', opacity: 0.7, textAlign: 'center',
          marginBottom: 14, fontStyle: 'italic',
        }}>All wins are multiplied by the stake. Stake: <b style={{ color: '#f4c243' }}>1.00</b></div>
        {rows.map((r, i) => (
          <div key={i} style={{
            display: 'flex', alignItems: 'center',
            gap: 12,
            padding: '12px 12px',
            background: i % 2 === 0 ? 'rgba(255,255,255,0.04)' : 'transparent',
            borderRadius: 8,
            marginBottom: 4,
            border: '1px solid rgba(255,220,140,0.08)',
          }}>
            <div style={{ width: 54, height: 54, flexShrink: 0 }}>
              {React.createElement(SYM[r.syms[0]].render)}
            </div>
            <div style={{ flex: 1 }}>
              <div style={{ display: 'flex', gap: 14, flexWrap: 'wrap' }}>
                {r.payouts.map(([n, v], j) => (
                  <div key={j} style={{ color: '#fff', fontSize: 13 }}>
                    <span style={{ color: '#aaa', fontSize: 11 }}>{n}</span>
                    <span style={{ color: '#f4c243', fontWeight: 800, marginLeft: 6 }}>{v}</span>
                  </div>
                ))}
              </div>
              {r.label && (
                <div style={{ color: '#fff', opacity: 0.6, fontSize: 11, marginTop: 4, fontStyle: 'italic' }}>
                  {r.label}
                </div>
              )}
            </div>
          </div>
        ))}
        <div style={{
          marginTop: 20, padding: 14, borderRadius: 10,
          background: 'rgba(244,194,67,0.08)', border: '1px solid rgba(244,194,67,0.25)',
          color: '#fff', fontSize: 12, lineHeight: 1.5,
        }}>
          <b style={{ color: '#f4c243' }}>10 paylines.</b> Wins pay left to right on adjacent reels starting from reel 1.
          The highest win per line is paid. RTP: 95.74%.
        </div>
      </div>
    </div>
  );
}

// Lobby with other games grid.
function LobbyScreen({ onClose }) {
  const games = [
    { name: '777 Strike', tag: 'PLAYING', color: '#197a3e', symbol: 'S7R' },
    { name: 'Mega Fire Blaze: Roulette', tag: 'HOT', color: '#9a1f2c', symbol: 'A' },
    { name: 'Dragon Lucky 8', tag: 'NEW', color: '#0a2f6e', symbol: 'WILD' },
    { name: 'Pirates Plenty', tag: '', color: '#16486e', symbol: 'LEMON' },
    { name: 'Mystery Reels', tag: 'JACKPOT', color: '#5a1a8a', symbol: 'Q' },
    { name: 'Wild Wild Chest', tag: '', color: '#6e4a14', symbol: 'CHRY' },
    { name: 'Diamond Blitz', tag: 'NEW', color: '#0e5a6e', symbol: 'S7B' },
    { name: 'Lion the Lord', tag: '', color: '#3a1a0a', symbol: 'K' },
    { name: 'Phoenix Fire Power', tag: 'HOT', color: '#7a2a14', symbol: 'S7R' },
  ];
  return (
    <div style={{
      position: 'absolute', inset: 0, zIndex: 95,
      background: 'linear-gradient(180deg, #0a2f1c 0%, #04180e 100%)',
      display: 'flex', flexDirection: 'column',
    }}>
      <div style={{ padding: '14px 18px', display: 'flex', alignItems: 'center', borderBottom: '1px solid rgba(255,220,140,0.2)', background: 'rgba(0,0,0,0.3)' }}>
        <button onClick={onClose} style={{
          width: 38, height: 38, borderRadius: '50%',
          background: 'rgba(255,255,255,0.08)', border: '1.5px solid rgba(255,220,140,0.3)',
          display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer',
        }}><Icons.chevronLeft/></button>
        <div style={{ flex: 1, textAlign: 'center', fontFamily: 'Cinzel, serif', fontWeight: 900, fontSize: 18, color: '#f4c243', letterSpacing: 3, textTransform: 'uppercase', marginRight: 38 }}>Lobby</div>
      </div>
      <div style={{ display: 'flex', gap: 8, padding: '12px 14px', overflowX: 'auto', borderBottom: '1px solid rgba(255,255,255,0.06)' }}>
        {['All', 'Slots', 'Live', 'Jackpot', 'New', 'Hot'].map((t, i) => (
          <button key={t} style={{
            padding: '7px 14px', borderRadius: 999,
            border: '1.5px solid rgba(255,220,140,0.3)',
            background: i === 0 ? 'linear-gradient(180deg, #f0c040 0%, #c98a1a 100%)' : 'rgba(255,255,255,0.05)',
            color: i === 0 ? '#3a2410' : '#fff',
            fontSize: 12, fontWeight: 700, cursor: 'pointer',
            flexShrink: 0,
          }}>{t}</button>
        ))}
      </div>
      <div style={{ flex: 1, overflow: 'auto', padding: 12 }}>
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10 }}>
          {games.map((g, i) => (
            <div key={i} style={{
              aspectRatio: '1 / 1.05',
              borderRadius: 10,
              background: `linear-gradient(135deg, ${g.color} 0%, ${g.color}aa 60%, #000 100%)`,
              position: 'relative', overflow: 'hidden',
              border: i === 0 ? '2px solid #f4c243' : '1px solid rgba(255,220,140,0.15)',
              boxShadow: '0 3px 8px rgba(0,0,0,0.4)',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              cursor: 'pointer',
            }}>
              <CrownBgPattern opacity={0.08}/>
              <div style={{ width: '55%', height: '55%', filter: 'drop-shadow(0 4px 8px rgba(0,0,0,0.6))' }}>
                {React.createElement(SYM[g.symbol].render)}
              </div>
              {g.tag && (
                <div style={{
                  position: 'absolute', top: 6, left: 6,
                  background: g.tag === 'PLAYING' ? '#1a6e3e' : g.tag === 'HOT' ? '#e21f2c' : g.tag === 'NEW' ? '#3a8edb' : '#c98a1a',
                  color: '#fff', fontSize: 9, fontWeight: 800,
                  padding: '3px 6px', borderRadius: 4, letterSpacing: 1,
                }}>{g.tag}</div>
              )}
              <div style={{
                position: 'absolute', bottom: 0, left: 0, right: 0,
                padding: '14px 8px 8px',
                background: 'linear-gradient(180deg, transparent 0%, rgba(0,0,0,0.85) 100%)',
                color: '#fff', fontSize: 11, fontWeight: 700,
              }}>{g.name}</div>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

Object.assign(window, {
  Reel, PaytableStrip, Icons, CrownBgPattern, GreenPill, SpinDisc, WinNumber, CoinRain,
  SidebarMenu, StakeModal, AutoplayModal, SplashOverlay, PaytableScreen, LobbyScreen,
  buildStrip, SYMBOL_PX, SYMBOL_ORDER,
});
