/* eslint-disable */
const { useState, useEffect, useRef, useMemo } = React;

// ===== Icons (inline SVG, simple line style) =====
const Icon = {
  Round: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M12 4 V12 L17 15"/><circle cx="12" cy="12" r="9"/></svg>,
  Cards: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><rect x="6" y="3" width="13" height="17"/><path d="M3 7 V21 H17"/></svg>,
  Shop: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M3 7 H21 L19 20 H5 L3 7Z"/><path d="M8 7 V5 a4 4 0 0 1 8 0 V7"/></svg>,
  Spawn: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M12 3 L21 8 V16 L12 21 L3 16 V8 Z"/><path d="M12 3 V21 M3 8 L21 8 M3 16 L21 16"/></svg>,
  Player: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><circle cx="12" cy="8" r="4"/><path d="M4 21 c0-5 4-7 8-7 s8 2 8 7"/></svg>,
  More: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><circle cx="5" cy="12" r="1.5"/><circle cx="12" cy="12" r="1.5"/><circle cx="19" cy="12" r="1.5"/></svg>,
  Close: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M5 5 L19 19 M19 5 L5 19"/></svg>,
  Plus: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M12 5 V19 M5 12 H19"/></svg>,
  Minus: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M5 12 H19"/></svg>,
  Dice: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><rect x="3" y="3" width="18" height="18"/><circle cx="8" cy="8" r="1.2" fill="currentColor"/><circle cx="16" cy="16" r="1.2" fill="currentColor"/><circle cx="16" cy="8" r="1.2" fill="currentColor"/><circle cx="8" cy="16" r="1.2" fill="currentColor"/><circle cx="12" cy="12" r="1.2" fill="currentColor"/></svg>,
  Refresh: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M3 12 a9 9 0 0 1 15-6.5 L21 8 M21 4 V8 H17"/><path d="M21 12 a9 9 0 0 1 -15 6.5 L3 16 M3 20 V16 H7"/></svg>,
  Eye: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M2 12 s4-7 10-7 10 7 10 7 -4 7 -10 7 -10 -7 -10 -7Z"/><circle cx="12" cy="12" r="3"/></svg>,
  Skull: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M5 11 c0-5 3-8 7-8 s7 3 7 8 v3 a2 2 0 0 1 -2 2 h-1 v3 h-8 v-3 h-1 a2 2 0 0 1 -2 -2 z"/><circle cx="9" cy="12" r="1.5" fill="currentColor"/><circle cx="15" cy="12" r="1.5" fill="currentColor"/></svg>,
  Crate: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><rect x="3" y="6" width="18" height="14"/><path d="M3 6 L12 11 L21 6 M9 8 V18 M15 8 V18"/></svg>,
  Target: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="5"/><circle cx="12" cy="12" r="1.5" fill="currentColor"/></svg>,
  NoEntry: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><circle cx="12" cy="12" r="9"/><path d="M5 5 L19 19"/></svg>,
  Trash: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M5 7 H19 M9 7 V4 H15 V7 M7 7 L8 21 H16 L17 7"/></svg>,
  Flag: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M5 21 V4 H15 L13 8 L15 12 H5"/></svg>,
  Book: (p) => <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...p}><path d="M4 4 H11 V20 H4 Z M13 4 H20 V20 H13 Z M11 4 V20 M13 4 V20"/></svg>,
};

// ===== Card image component =====
function CardImage({ card, onClick, size = 'normal' }) {
  return (
    <div className={`card-png lift ${size === 'large' ? 'large' : ''}`} onClick={onClick}>
      <img src={card.image} alt={card.name} loading="lazy" />
    </div>
  );
}

// ===== Drawer =====
function Drawer({ open, onClose, title, children, footer, accent }) {
  if (!open) return null;
  return (
    <div className="modal-back" onClick={onClose}>
      <div className="drawer" onClick={e => e.stopPropagation()}>
        <div className="drawer-handle"/>
        <div className="drawer-header">
          {accent && <span className={`chip ${accent}`}>{accent.replace('-c','').toUpperCase()}</span>}
          <h3 className="h3 grow" style={{color:'var(--ink)'}}>{title}</h3>
          <button className="icon-btn" onClick={onClose}><Icon.Close width="20" height="20"/></button>
        </div>
        <div className="drawer-body">{children}</div>
        {footer && <div className="drawer-footer">{footer}</div>}
      </div>
    </div>
  );
}

function Modal({ open, onClose, title, children, footer }) {
  if (!open) return null;
  return (
    <div className="modal-back" onClick={onClose} style={{alignItems:'center'}}>
      <div className="drawer modal" onClick={e => e.stopPropagation()} style={{maxWidth:420}}>
        <div className="drawer-header">
          <h3 className="h3 grow" style={{color:'var(--ink)'}}>{title}</h3>
          <button className="icon-btn" onClick={onClose}><Icon.Close width="20" height="20"/></button>
        </div>
        <div className="drawer-body">{children}</div>
        {footer && <div className="drawer-footer">{footer}</div>}
      </div>
    </div>
  );
}

// ===== Stat block (CP/SP) =====
function StatBlock({ label, value, onAdd, onSub, kind }) {
  return (
    <div className={`stat-block ${kind || ''}`}>
      <div style={{flex:1}}>
        <div className="label">{label}</div>
        <div className="value">{value}</div>
      </div>
      {onAdd && (
        <div className="ctrl">
          <button onClick={onAdd}>+</button>
          <button onClick={onSub}>−</button>
        </div>
      )}
    </div>
  );
}

// ===== Stepper =====
function Stepper({ value, onChange, min = -10, max = 10 }) {
  return (
    <div className="modifier-stepper">
      <button onClick={() => onChange(Math.max(min, value - 1))}>−</button>
      <span className="val">{value > 0 ? `+${value}` : value}</span>
      <button onClick={() => onChange(Math.min(max, value + 1))}>+</button>
    </div>
  );
}

// ===== Empty =====
function Empty({ title, subtitle, action }) {
  return (
    <div className="empty">
      <div className="h-display h3">{title}</div>
      {subtitle && <div className="muted" style={{fontSize:13, marginTop:4}}>{subtitle}</div>}
      {action && <div style={{marginTop:12}}>{action}</div>}
    </div>
  );
}

// ===== Card viewer (full-screen tap to view) =====
function CardLightbox({ card, onClose, accent }) {
  if (!card) return null;
  return (
    <div className="modal-back" onClick={onClose} style={{alignItems:'center', padding:'16px'}}>
      <div onClick={e => e.stopPropagation()} style={{width:'100%', maxWidth:380, position:'relative'}}>
        <img src={card.image} alt={card.name} style={{width:'100%', display:'block'}}/>
        <button onClick={onClose} className="icon-btn" style={{position:'absolute', top:8, right:8, background:'rgba(0,0,0,0.5)', borderRadius:'50%'}}>
          <Icon.Close width="22" height="22"/>
        </button>
      </div>
    </div>
  );
}

Object.assign(window, { Icon, CardImage, Drawer, Modal, StatBlock, Stepper, Empty, CardLightbox });
