// duel-shared.jsx — shared dark-theme UI atoms for the GTO Duel mobile flow.
// Exports (to window): DUEL_BG, SUIT2, MiniCard, Eyebrow, DarkCard, Toggle, Segmented,
//   SelectField, TextField, Accordion, Chevron, Stars, FlowTopBar, ScreenShell, Lock, ProgressBar

const DUEL_BG = 'radial-gradient(ellipse 130% 80% at 50% 0%, #1D2030 0%, #12131D 45%, #0B0C13 100%)';

const SUIT2 = {
  s: { glyph: '\u2660', bg: '#3E4350' },
  c: { glyph: '\u2663', bg: '#27BF69' },
  d: { glyph: '\u2666', bg: '#4A8DF6' },
  h: { glyph: '\u2665', bg: '#E74C3C' },
};

function DuelSharedStyles() {
  return (
    <style>{`
      .op-scroll::-webkit-scrollbar { width: 7px; height: 7px; }
      .op-scroll::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.14); border-radius: 999px; }
      .op-scroll::-webkit-scrollbar-track { background: transparent; }
      .op-scroll { scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.14) transparent; }
      .op-tap { -webkit-tap-highlight-color: transparent; }
      @keyframes accIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
    `}</style>
  );
}

// Small face-up card chip used in logs / history
function MiniCard({ code, w = 26 }) {
  const rank = code.slice(0, -1);
  const suit = SUIT2[code.slice(-1)] || SUIT2.s;
  return (
    <div style={{
      width: w, height: Math.round(w * 1.4), borderRadius: Math.max(4, w * 0.16),
      background: suit.bg, color: '#fff', position: 'relative', flexShrink: 0,
      boxShadow: '0 1px 3px rgba(0,0,0,0.4)', border: '1px solid rgba(255,255,255,0.18)',
    }}>
      <span style={{ position: 'absolute', top: w * 0.04, left: w * 0.14, fontFamily: 'var(--op-font-ui)', fontWeight: 800, fontSize: w * 0.46, lineHeight: 1 }}>{rank}</span>
      <span style={{ position: 'absolute', bottom: w * 0.06, right: w * 0.12, fontSize: w * 0.36, lineHeight: 1 }}>{suit.glyph}</span>
    </div>
  );
}

function CardRow({ codes, w = 26, gap = 4 }) {
  return <div style={{ display: 'flex', gap }}>{codes.map((c, i) => <MiniCard key={c + i} code={c} w={w} />)}</div>;
}

function Eyebrow({ children, style }) {
  return (
    <div style={{
      fontFamily: 'var(--op-font-ui)', fontWeight: 700, fontSize: 11, letterSpacing: '0.16em',
      textTransform: 'uppercase', color: 'rgba(255,255,255,0.4)', ...style,
    }}>{children}</div>
  );
}

function DarkCard({ children, style, pad = 16, onClick }) {
  return (
    <div onClick={onClick} className={onClick ? 'op-tap' : undefined} style={{
      background: 'rgba(255,255,255,0.035)', border: '1px solid rgba(255,255,255,0.07)',
      borderRadius: 16, padding: pad, boxSizing: 'border-box',
      cursor: onClick ? 'pointer' : undefined, ...style,
    }}>{children}</div>
  );
}

function Chevron({ dir = 'down', size = 18, color = 'rgba(255,255,255,0.5)' }) {
  const rot = { down: 0, up: 180, right: -90, left: 90 }[dir] || 0;
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" style={{ transform: `rotate(${rot}deg)`, transition: 'transform 0.2s' }}><path d="M6 9l6 6 6-6" /></svg>
  );
}

function Toggle({ on, onChange }) {
  return (
    <button onClick={() => onChange(!on)} className="op-tap" aria-pressed={on} style={{
      width: 46, height: 26, borderRadius: 999, border: 'none', cursor: 'pointer', flexShrink: 0,
      background: on ? 'var(--op-exodus-70)' : 'rgba(255,255,255,0.16)',
      position: 'relative', transition: 'background 0.2s', padding: 0,
    }}>
      <span style={{
        position: 'absolute', top: 3, left: on ? 23 : 3, width: 20, height: 20, borderRadius: 999,
        background: '#fff', transition: 'left 0.2s', boxShadow: '0 1px 3px rgba(0,0,0,0.35)',
      }}></span>
    </button>
  );
}

function Segmented({ options, value, onChange }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: `repeat(${options.length}, 1fr)`, gap: 4,
      background: 'rgba(255,255,255,0.05)', borderRadius: 12, padding: 4,
      border: '1px solid rgba(255,255,255,0.07)',
    }}>
      {options.map((o) => {
        const active = o.value === value;
        return (
          <button key={o.value} onClick={() => onChange(o.value)} className="op-tap" style={{
            border: 'none', cursor: 'pointer', borderRadius: 9, padding: '11px 6px',
            fontFamily: 'var(--op-font-ui)', fontWeight: 700, fontSize: 12, letterSpacing: '0.04em',
            textTransform: 'uppercase', transition: 'all 0.18s',
            background: active ? 'var(--op-exodus-70)' : 'transparent',
            color: active ? '#fff' : 'rgba(255,255,255,0.55)',
          }}>{o.label}</button>
        );
      })}
    </div>
  );
}

// Tappable select that cycles its options (no native dropdown needed in a mock)
function SelectField({ label, options, value, onChange, adornment }) {
  const cycle = () => {
    const i = options.indexOf(value);
    onChange(options[(i + 1) % options.length]);
  };
  return (
    <button onClick={cycle} className="op-tap" style={{
      width: '100%', textAlign: 'left', cursor: 'pointer', position: 'relative',
      background: 'rgba(10,11,17,0.55)', border: '1px solid rgba(255,255,255,0.12)',
      borderRadius: 12, padding: '15px 16px 14px', display: 'flex', alignItems: 'center', justifyContent: 'space-between',
    }}>
      <span style={{
        position: 'absolute', top: -8, left: 12, padding: '0 6px', background: '#12131D',
        fontFamily: 'var(--op-font-ui)', fontWeight: 600, fontSize: 11, color: 'rgba(255,255,255,0.45)',
      }}>{label}</span>
      <span style={{ fontFamily: 'var(--op-font-ui)', fontWeight: 600, fontSize: 16, color: '#fff' }}>{value}</span>
      <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>{adornment}<Chevron /></span>
    </button>
  );
}

function TextField({ label, value, onChange }) {
  return (
    <label style={{ position: 'relative', display: 'block' }}>
      <span style={{
        position: 'absolute', top: -8, left: 12, padding: '0 6px', background: '#12131D', zIndex: 1,
        fontFamily: 'var(--op-font-ui)', fontWeight: 600, fontSize: 11, color: 'rgba(255,255,255,0.45)',
      }}>{label}</span>
      <input value={value} onChange={(e) => onChange(e.target.value)} style={{
        width: '100%', boxSizing: 'border-box', background: 'rgba(10,11,17,0.55)',
        border: '1px solid rgba(255,255,255,0.12)', borderRadius: 12, padding: '15px 16px 14px',
        fontFamily: 'var(--op-font-ui)', fontWeight: 600, fontSize: 16, color: '#fff', outline: 'none',
      }} />
    </label>
  );
}

function Stars({ filled = 3, total = 5, size = 13 }) {
  return (
    <span style={{ display: 'inline-flex', gap: 1 }}>
      {Array.from({ length: total }, (_, i) => (
        <span key={i} style={{ color: i < filled ? 'var(--op-gold-50)' : 'rgba(255,255,255,0.2)', fontSize: size, lineHeight: 1 }}>&#9733;</span>
      ))}
    </span>
  );
}

function Lock({ size = 13 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="rgba(255,255,255,0.4)" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="11" width="18" height="11" rx="2" /><path d="M7 11V7a5 5 0 0110 0v4" /></svg>
  );
}

function ProgressBar({ pct, color = 'var(--op-exodus-70)', h = 6 }) {
  return (
    <div style={{ height: h, borderRadius: 999, background: 'rgba(255,255,255,0.1)', overflow: 'hidden', width: '100%' }}>
      <div style={{ height: '100%', width: `${Math.max(0, Math.min(100, pct))}%`, background: color, borderRadius: 999 }}></div>
    </div>
  );
}

function Accordion({ title, right, defaultOpen = false, children }) {
  const [open, setOpen] = React.useState(defaultOpen);
  return (
    <div style={{ borderBottom: '1px solid rgba(255,255,255,0.06)' }}>
      <button onClick={() => setOpen(!open)} className="op-tap" style={{
        width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        background: 'none', border: 'none', cursor: 'pointer', padding: '14px 2px', textAlign: 'left',
      }}>
        <span style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <Chevron dir={open ? 'up' : 'down'} size={16} />
          <span style={{ fontFamily: 'var(--op-font-ui)', fontWeight: 700, fontSize: 13, color: '#fff' }}>{title}</span>
        </span>
        <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>{right}</span>
      </button>
      {open && <div style={{ paddingBottom: 12, animation: 'accIn 0.2s ease-out' }}>{children}</div>}
    </div>
  );
}

// Sticky top bar with optional back button + title + right slot
function FlowTopBar({ title, onBack, backLabel, right, eyebrow, landscape }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 12,
      padding: landscape ? '16px 32px 12px 60px' : '54px 16px 14px', boxSizing: 'border-box',
      borderBottom: '1px solid rgba(255,255,255,0.06)', background: 'rgba(11,12,19,0.6)', backdropFilter: 'blur(8px)',
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12, minWidth: 0 }}>
        {onBack && (
          <button onClick={onBack} className="op-tap" style={{
            width: 36, height: 36, borderRadius: 999, flexShrink: 0, cursor: 'pointer',
            background: 'rgba(255,255,255,0.06)', border: '1px solid rgba(255,255,255,0.12)', color: '#fff',
            display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 0,
          }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 18l-6-6 6-6" /></svg>
          </button>
        )}
        <div style={{ minWidth: 0 }}>
          {eyebrow && <Eyebrow style={{ fontSize: 9.5, marginBottom: 2 }}>{eyebrow}</Eyebrow>}
          <div style={{ fontFamily: 'var(--op-font-display)', fontWeight: 700, fontSize: 21, color: '#fff', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{title}</div>
        </div>
      </div>
      {right}
    </div>
  );
}

// Scrollable screen scaffold (sticky header + scroll body + optional sticky footer)
function ScreenShell({ header, footer, children, padBody = 16, landscape, sidebar }) {
  // Landscape split view: fixed summary rail on the left, scrolling detail on the right.
  if (landscape && sidebar) {
    return (
      <div style={{ height: '100%', display: 'flex', background: DUEL_BG, color: '#fff', fontFamily: 'var(--op-font-ui)', position: 'relative' }}>
        <DuelSharedStyles />
        <div style={{ width: '42%', maxWidth: 392, flexShrink: 0, boxSizing: 'border-box', padding: '18px 18px 18px 60px', borderRight: '1px solid rgba(255,255,255,0.07)', display: 'flex', flexDirection: 'column' }}>{sidebar}</div>
        <div className="op-scroll" style={{ flex: 1, overflowY: 'auto', WebkitOverflowScrolling: 'touch', padding: '18px 26px', boxSizing: 'border-box' }}>{children}</div>
      </div>
    );
  }
  const pad = landscape ? '18px 32px 22px 60px' : padBody;
  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: DUEL_BG, color: '#fff', fontFamily: 'var(--op-font-ui)', position: 'relative' }}>
      <DuelSharedStyles />
      {header && <div style={{ flexShrink: 0, zIndex: 3 }}>{header}</div>}
      <div className="op-scroll" style={{ flex: 1, overflowY: 'auto', WebkitOverflowScrolling: 'touch', padding: pad, boxSizing: 'border-box' }}>{children}</div>
      {footer && <div style={{ flexShrink: 0, zIndex: 3 }}>{footer}</div>}
    </div>
  );
}

Object.assign(window, {
  DUEL_BG, SUIT2, MiniCard, CardRow, Eyebrow, DarkCard, Toggle, Segmented,
  SelectField, TextField, Accordion, Chevron, Stars, Lock, ProgressBar, FlowTopBar, ScreenShell, DuelSharedStyles,
});
