// screens-terminal.jsx — Direction 1: OPS TERMINAL
// Pure mono, ASCII rules, scanlines, no rounded corners, signal-orange + white

const TERM = {
  bg: '#0A0A0E',
  panel: '#11111A',
  panelHi: '#16161F',
  line: '#26262F',
  line2: '#3A3A47',
  text: '#E8E8EF',
  textDim: '#7A7A88',
  textMute: '#4D4D58',
  ok: '#3DDC97',
  warn: '#FFB020',
  danger: '#FF3B30',
};

// ─── Atoms ───
const TRule = ({ char = '─', color }) => (
  <div className="mono" style={{
    color: color || TERM.line2, fontSize: 10, letterSpacing: 0,
    whiteSpace: 'nowrap', overflow: 'hidden', lineHeight: 1, userSelect: 'none',
  }}>{char.repeat(80)}</div>
);

const TStat = ({ on = true, char = '●' }) => (
  <span style={{ color: on ? TERM.ok : TERM.textMute, fontSize: 8, lineHeight: 1 }}>{char}</span>
);

const TBtn = ({ children, accent, active, onClick, full, style }) => (
  <button onClick={onClick} className="mono" style={{
    appearance: 'none', border: `1px solid ${active ? accent : TERM.line2}`,
    background: active ? accent : 'transparent',
    color: active ? '#000' : TERM.text,
    padding: '8px 12px', fontSize: 11, letterSpacing: 1, textTransform: 'uppercase',
    cursor: 'pointer', borderRadius: 0, width: full ? '100%' : 'auto',
    fontWeight: 500, ...style,
  }}>{children}</button>
);

const TBracket = ({ children, accent }) => (
  <span className="mono" style={{ color: accent, fontSize: 11, letterSpacing: 1 }}>
    [ <span style={{ color: TERM.text }}>{children}</span> ]
  </span>
);

// ─── Header strip used by most screens ───
function THeader({ title, sub, accent, right }) {
  return (
    <div style={{ padding: '64px 14px 6px', background: TERM.bg }}>
      <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', gap: 8 }}>
        <div>
          <div className="mono" style={{ color: accent, fontSize: 10, letterSpacing: 2, marginBottom: 4 }}>ADLEY // {sub || 'OPS'}</div>
          <div className="mono" style={{ color: TERM.text, fontSize: 22, fontWeight: 600, letterSpacing: 1, textTransform: 'uppercase' }}>{title}</div>
        </div>
        {right}
      </div>
      <div style={{ marginTop: 8 }}><TRule char="═" color={accent} /></div>
    </div>
  );
}

// ─── Bottom tab bar ───
const TABS = [
  { id: 'home', label: 'SRV', icon: 'hard-drives' },
  { id: 'map', label: 'MAP', icon: 'map-pin' },
  { id: 'freqs', label: 'RF', icon: 'broadcast' },
  { id: 'events', label: 'LOG', icon: 'list-bullets' },
  { id: 'squad', label: 'SQD', icon: 'users-three' },
];

function TTabBar({ screen, onChange, accent }) {
  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0,
      background: TERM.bg, borderTop: `1px solid ${accent}`,
      paddingBottom: 28, paddingTop: 6,
      display: 'grid', gridTemplateColumns: 'repeat(5, 1fr)', zIndex: 60,
    }}>
      {TABS.map(t => (
        <button key={t.id} onClick={() => onChange(t.id)} className="mono"
          style={{
            border: 0, background: 'transparent', cursor: 'pointer',
            padding: '8px 4px', display: 'flex', flexDirection: 'column',
            alignItems: 'center', gap: 4,
            color: screen === t.id ? accent : TERM.textDim,
          }}>
          <Icon name={t.icon} size={18} weight={screen === t.id ? 'fill' : 'regular'} />
          <span style={{ fontSize: 9, letterSpacing: 1 }}>{t.label}</span>
        </button>
      ))}
    </div>
  );
}

// ───────────────────────────────────────────────
// HOME — Server list
// ───────────────────────────────────────────────
function TerminalHome({ accent, onOpenServer }) {
  return (
    <div style={{ height: '100%', background: TERM.bg, color: TERM.text, position: 'relative' }}>
      <THeader title="SERVERS" sub="HOME" accent={accent} right={
        <button className="mono" style={{ border: `1px solid ${accent}`, background: 'transparent', color: accent, padding: '6px 8px', fontSize: 10, letterSpacing: 1, cursor: 'pointer' }}>
          [+] ADD
        </button>
      } />
      <div style={{ padding: '4px 14px 100px', overflow: 'auto', height: 'calc(100% - 120px)' }}>
        <div className="mono" style={{ display: 'flex', justifyContent: 'space-between', fontSize: 9, color: TERM.textMute, letterSpacing: 1, padding: '8px 0' }}>
          <span>{SERVERS.length} LINKED</span>
          <span>2 OWNED · 2 JOINED</span>
        </div>
        {SERVERS.map((s, i) => (
          <div key={s.id} onClick={() => onOpenServer && onOpenServer(s)}
            style={{ cursor: 'pointer', padding: '12px 0', borderTop: `1px solid ${TERM.line}`, position: 'relative' }}>
            <div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: 8 }}>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 4 }}>
                  <TStat on />
                  <span className="mono" style={{ fontSize: 9, color: accent, letterSpacing: 1 }}>{s.tag}</span>
                  {s.verified && <span className="mono" style={{ fontSize: 9, color: TERM.ok, border: `1px solid ${TERM.ok}`, padding: '0 4px' }}>VFY</span>}
                  {s.owner && <span className="mono" style={{ fontSize: 9, color: TERM.warn, border: `1px solid ${TERM.warn}`, padding: '0 4px' }}>OWN</span>}
                </div>
                <div className="mono" style={{ fontSize: 14, fontWeight: 600, letterSpacing: 0.5, marginBottom: 2 }}>{s.name}</div>
                <div className="mono" style={{ fontSize: 10, color: TERM.textDim, letterSpacing: 0.5 }}>
                  {s.region} · WIPE +{s.wipe} · {s.host.split(':')[0]}
                </div>
              </div>
              <div style={{ textAlign: 'right', minWidth: 84 }}>
                <div className="mono" style={{ fontSize: 22, fontWeight: 600, color: TERM.text, lineHeight: 1, fontVariantNumeric: 'tabular-nums' }}>
                  {String(s.players).padStart(3, '0')}
                </div>
                <div className="mono" style={{ fontSize: 9, color: TERM.textMute }}>/ {s.max} POP</div>
                <div className="mono" style={{ fontSize: 9, color: TERM.textDim, marginTop: 4 }}>{s.fps}FPS · UP {s.uptime}</div>
              </div>
            </div>
            {/* mini load bar */}
            <div style={{ display: 'flex', gap: 1, marginTop: 8, height: 4 }}>
              {Array.from({ length: 30 }).map((_, j) => (
                <div key={j} style={{
                  flex: 1,
                  background: j < (s.players / s.max) * 30 ? accent : TERM.line,
                }} />
              ))}
            </div>
            {s.squad && (
              <div className="mono" style={{ fontSize: 9, color: TERM.textMute, marginTop: 6, letterSpacing: 1 }}>
                SQD :: {s.squad}
              </div>
            )}
          </div>
        ))}
        <div style={{ borderTop: `1px solid ${TERM.line}`, padding: '14px 0 8px' }}>
          <div className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 1, marginBottom: 6 }}>// DISCOVER</div>
          <button className="mono" style={{ width: '100%', padding: '10px 12px', background: 'transparent', border: `1px dashed ${TERM.line2}`, color: TERM.textDim, fontSize: 11, letterSpacing: 1, cursor: 'pointer', textAlign: 'left' }}>
            ▸ BROWSE PUBLIC CE DIRECTORY ─────────────►
          </button>
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// SERVER SNAPSHOT
// ───────────────────────────────────────────────
function TerminalSnapshot({ accent, onBack, onOpenMap }) {
  const s = SERVERS[0];
  return (
    <div style={{ height: '100%', background: TERM.bg, color: TERM.text, position: 'relative' }}>
      <div style={{ padding: '52px 14px 6px', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <button onClick={onBack} className="mono" style={{ border: 0, background: 'transparent', color: TERM.textDim, fontSize: 11, letterSpacing: 1, cursor: 'pointer' }}>
          ◂ HOME
        </button>
        <span className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 1 }}>SYNC :: 02:14:08</span>
      </div>
      <div style={{ padding: '4px 14px 100px', overflow: 'auto', height: 'calc(100% - 80px)' }}>
        <div className="mono" style={{ color: accent, fontSize: 10, letterSpacing: 2 }}>SNAPSHOT // {s.tag}</div>
        <div className="mono" style={{ fontSize: 20, fontWeight: 600, letterSpacing: 0.5, marginTop: 2 }}>{s.name}</div>
        <div className="mono" style={{ fontSize: 10, color: TERM.textDim, marginTop: 2 }}>{s.host} · {s.region}</div>

        <div style={{ marginTop: 14 }}><TRule color={TERM.line2}/></div>

        {/* Stat grid */}
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 0, marginTop: 8 }}>
          {[
            ['POP', `${s.players}/${s.max}`, accent],
            ['FPS', s.fps, TERM.ok],
            ['UPTIME', s.uptime, TERM.text],
            ['WIPE +', s.wipe, TERM.text],
          ].map(([k, v, col], i) => (
            <div key={k} className="mono" style={{
              padding: '12px 8px',
              borderRight: i % 2 === 0 ? `1px solid ${TERM.line}` : 0,
              borderBottom: i < 2 ? `1px solid ${TERM.line}` : 0,
            }}>
              <div style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 1, marginBottom: 4 }}>{k}</div>
              <div style={{ fontSize: 22, fontWeight: 600, color: col, fontVariantNumeric: 'tabular-nums', letterSpacing: 0.5 }}>{v}</div>
            </div>
          ))}
        </div>

        <div style={{ marginTop: 14 }}><TRule color={TERM.line2}/></div>

        {/* Armed RF list */}
        <div className="mono" style={{ fontSize: 9, color: accent, letterSpacing: 2, marginTop: 12, marginBottom: 6 }}>┌─ ARMED RF · 2</div>
        {FREQS.slice(0, 2).map(f => (
          <div key={f.id} style={{ display: 'flex', alignItems: 'center', padding: '8px 0', borderBottom: `1px solid ${TERM.line}` }}>
            <span className="mono" style={{ color: accent, fontSize: 10, marginRight: 8 }}>│</span>
            <span className="mono" style={{ fontSize: 14, fontWeight: 600, fontVariantNumeric: 'tabular-nums', marginRight: 10, color: TERM.text }}>{f.freq}</span>
            <div style={{ flex: 1 }}>
              <div className="mono" style={{ fontSize: 11, letterSpacing: 0.5 }}>{f.label}</div>
              <div className="mono" style={{ fontSize: 9, color: TERM.textDim }}>{f.grid} · {f.scope} · {f.locked ? 'LOCK' : 'OPEN'}</div>
            </div>
            <span className="mono" style={{ fontSize: 9, color: f.triggered.includes('ago') ? TERM.warn : TERM.textMute }}>{f.triggered}</span>
          </div>
        ))}

        {/* Entities */}
        <div className="mono" style={{ fontSize: 9, color: accent, letterSpacing: 2, marginTop: 12, marginBottom: 6 }}>┌─ ENTITIES · 4</div>
        {ENTITIES.map(e => (
          <div key={e.id} className="mono" style={{ display: 'flex', justifyContent: 'space-between', fontSize: 11, padding: '6px 0', borderBottom: `1px solid ${TERM.line}`, letterSpacing: 0.5 }}>
            <span><span style={{ color: e.status === 'armed' ? accent : TERM.textMute }}>[{e.kind}]</span> {e.label}</span>
            <span style={{ color: TERM.textDim }}>{e.grid}</span>
          </div>
        ))}

        <div style={{ marginTop: 14, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 6 }}>
          <TBtn onClick={onOpenMap} accent={accent}>▸ MAP</TBtn>
          <TBtn accent={accent}>▸ SAY/BROADCAST</TBtn>
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// WORLD MAP ★ HERO
// ───────────────────────────────────────────────
function TerminalMap({ accent }) {
  const W = 372; const H = 372;
  return (
    <div style={{ height: '100%', background: TERM.bg, color: TERM.text, position: 'relative', overflow: 'hidden' }}>
      <THeader title="WORLD MAP" sub="OPS // EU-3X" accent={accent} right={
        <span className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 1 }}>4500 · PROCEDURAL</span>
      }/>
      {/* Map canvas */}
      <div style={{ padding: '8px 14px' }}>
        <div style={{ position: 'relative', width: '100%', aspectRatio: '1 / 1', background: '#04040A', border: `1px solid ${accent}33`, overflow: 'hidden' }}>
          {/* grid */}
          <svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none" style={{ position: 'absolute', inset: 0 }}>
            {Array.from({ length: 25 }).map((_, i) => (
              <line key={`v${i}`} x1={i * 4} y1={0} x2={i * 4} y2={100} stroke={accent} strokeOpacity={0.08} strokeWidth={0.15} />
            ))}
            {Array.from({ length: 25 }).map((_, i) => (
              <line key={`h${i}`} x1={0} y1={i * 4} x2={100} y2={i * 4} stroke={accent} strokeOpacity={0.08} strokeWidth={0.15} />
            ))}
            {/* center crosshair */}
            <line x1={50} y1={45} x2={50} y2={55} stroke={accent} strokeOpacity={0.4} strokeWidth={0.3} />
            <line x1={45} y1={50} x2={55} y2={50} stroke={accent} strokeOpacity={0.4} strokeWidth={0.3} />
            <circle cx={50} cy={50} r={2} stroke={accent} strokeOpacity={0.3} strokeWidth={0.2} fill="none" />
            {/* range rings — concentric dotted circles centered on map */}
            {[12, 22, 32, 42].map(r => (
              <circle key={r} cx={50} cy={50} r={r} fill="none"
                stroke={accent} strokeOpacity={0.28} strokeWidth={0.25}
                strokeDasharray="0.4,1.2" />
            ))}
            {/* off-center secondary cluster — gives depth without the peanut blob */}
            {[6, 11, 16].map(r => (
              <circle key={`b${r}`} cx={72} cy={32} r={r} fill="none"
                stroke={accent} strokeOpacity={0.18} strokeWidth={0.2}
                strokeDasharray="0.3,1" />
            ))}
            {[5, 9, 13].map(r => (
              <circle key={`c${r}`} cx={26} cy={70} r={r} fill="none"
                stroke={accent} strokeOpacity={0.18} strokeWidth={0.2}
                strokeDasharray="0.3,1" />
            ))}
            {/* axes */}
            <path d="M10,50 L90,50" stroke={TERM.textMute} strokeWidth={0.2} fill="none" strokeDasharray="0.5,1.5" />
            <path d="M50,10 L50,90" stroke={TERM.textMute} strokeWidth={0.2} fill="none" strokeDasharray="0.5,1.5" />
          </svg>
          {/* grid letters/numbers on edges */}
          {'ABCDEFGHIJKLMNOPQR'.split('').map((c, i) => (
            <div key={c} className="mono" style={{ position: 'absolute', top: 2, left: `${(i + 0.5) * (100 / 18)}%`, transform: 'translateX(-50%)', fontSize: 6, color: TERM.textMute, letterSpacing: 0 }}>{c}</div>
          ))}
          {Array.from({ length: 18 }).map((_, i) => (
            <div key={i} className="mono" style={{ position: 'absolute', left: 2, top: `${(i + 0.5) * (100 / 18)}%`, transform: 'translateY(-50%)', fontSize: 6, color: TERM.textMute }}>{String(i).padStart(2, '0')}</div>
          ))}
          {/* Monuments */}
          {MONUMENTS.map(m => (
            <div key={m.id} className="mono" style={{
              position: 'absolute', left: `${m.x * 100}%`, top: `${m.y * 100}%`, transform: 'translate(-50%, -50%)',
              fontSize: 8, color: m.type === 'safe' ? TERM.ok : m.type === 'tier3' ? accent : TERM.textDim,
              display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 2,
            }}>
              <span style={{ fontSize: 6, opacity: 0.4 }}>◆</span>
              <span style={{ fontSize: 7, letterSpacing: 0.5, textShadow: '0 0 4px #000' }}>{m.name}</span>
            </div>
          ))}
          {/* Entities */}
          {ENTITIES.map(e => (
            <div key={e.id} style={{
              position: 'absolute', left: `${e.x * 100}%`, top: `${e.y * 100}%`, transform: 'translate(-50%, -50%)',
            }}>
              <div style={{
                width: 10, height: 10, border: `1px solid ${e.status === 'armed' ? TERM.danger : accent}`,
                background: e.status === 'armed' ? `${TERM.danger}33` : 'transparent',
                animation: e.status === 'armed' ? 'adley-pulse 1.5s infinite' : 'none',
              }} />
              <div className="mono" style={{ fontSize: 6, color: e.status === 'armed' ? TERM.danger : accent, letterSpacing: 0.5, position: 'absolute', top: 11, left: '50%', transform: 'translateX(-50%)', whiteSpace: 'nowrap', textShadow: '0 0 3px #000' }}>
                {e.kind}·{e.grid}
              </div>
            </div>
          ))}
          {/* radar sweep */}
          <div style={{
            position: 'absolute', inset: 0, pointerEvents: 'none',
            background: `conic-gradient(from 0deg at 50% 50%, transparent 0deg, ${accent}22 30deg, transparent 60deg)`,
            animation: 'adley-sweep 8s linear infinite',
          }} />
          <div className="adley-scanline-overlay" style={{ opacity: 0.5 }} />
        </div>

        {/* Filter row */}
        <div style={{ display: 'flex', gap: 4, marginTop: 10, flexWrap: 'wrap' }}>
          {['MONUMENTS', 'TC', 'BAGS', 'RAID', 'PVP', 'CARGO'].map((f, i) => (
            <button key={f} className="mono" style={{
              border: `1px solid ${i === 0 || i === 3 ? accent : TERM.line2}`,
              background: i === 0 || i === 3 ? `${accent}22` : 'transparent',
              color: i === 0 || i === 3 ? accent : TERM.textDim, padding: '4px 8px', fontSize: 9, letterSpacing: 1, cursor: 'pointer',
            }}>{f}</button>
          ))}
        </div>
        <div style={{ marginTop: 8 }}><TRule color={TERM.line2}/></div>

        {/* Map legend */}
        <div className="mono" style={{ fontSize: 9, color: TERM.textDim, marginTop: 8, display: 'grid', gridTemplateColumns: '1fr 1fr', rowGap: 4, columnGap: 8 }}>
          <span><span style={{ color: TERM.danger }}>■</span> TC ARMED · 1</span>
          <span><span style={{ color: accent }}>■</span> TC COLD · 1</span>
          <span><span style={{ color: accent }}>◆</span> T3 MON · 3</span>
          <span><span style={{ color: TERM.ok }}>◆</span> SAFE · 2</span>
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// RAID ALERT (full screen takeover) — TACTICAL HUD treatment
// ───────────────────────────────────────────────
function TerminalRaidAlert({ accent }) {
  return (
    <div style={{ height: '100%', background: '#0A0000', color: TERM.text, position: 'relative', overflow: 'hidden' }}>
      {/* Strobe bg */}
      <div style={{
        position: 'absolute', inset: 0,
        background: TERM.danger, opacity: 0.08,
        animation: 'adley-strobe-soft 0.6s steps(1) infinite',
      }} />
      {/* Corner crosshairs */}
      {[['top:20px;left:20px', 'M0,0 L20,0 M0,0 L0,20'],
        ['top:20px;right:20px', 'M40,0 L20,0 M40,0 L40,20'],
        ['bottom:20px;left:20px', 'M0,40 L20,40 M0,40 L0,20'],
        ['bottom:20px;right:20px', 'M40,40 L20,40 M40,40 L40,20']].map(([pos, d], i) => (
        <svg key={i} width="40" height="40" viewBox="0 0 40 40" style={{ position: 'absolute', ...Object.fromEntries(pos.split(';').map(p => p.split(':'))) }}>
          <path d={d} stroke={TERM.danger} strokeWidth="2" fill="none"/>
        </svg>
      ))}

      <div style={{ padding: '60px 24px 0', textAlign: 'center', position: 'relative', zIndex: 5 }}>
        <div className="mono" style={{ color: TERM.danger, fontSize: 10, letterSpacing: 4, fontWeight: 600, animation: 'adley-blink 0.8s infinite' }}>
          ▲ INCOMING ▲
        </div>
        <div className="mono" style={{ color: '#fff', fontSize: 56, fontWeight: 700, letterSpacing: 4, marginTop: 24, lineHeight: 0.9 }}>RAID</div>
        <div className="mono" style={{ color: TERM.danger, fontSize: 14, letterSpacing: 6, fontWeight: 600, marginTop: 4 }}>┃ TC TRIPPED ┃</div>

        {/* Big freq display */}
        <div style={{ marginTop: 36, border: `2px solid ${TERM.danger}`, padding: '18px 12px', position: 'relative' }}>
          <div className="mono" style={{ fontSize: 9, color: TERM.danger, letterSpacing: 3, marginBottom: 8 }}>FREQUENCY</div>
          <div className="mono" style={{ fontSize: 72, fontWeight: 700, color: '#fff', lineHeight: 0.9, letterSpacing: 4, fontVariantNumeric: 'tabular-nums' }}>4763</div>
          <div className="mono" style={{ fontSize: 11, color: TERM.textDim, marginTop: 8, letterSpacing: 1 }}>MAIN TC · STORM-7</div>
        </div>

        {/* Grid + base */}
        <div style={{ marginTop: 18, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 1, background: TERM.danger }}>
          <div style={{ background: '#0A0000', padding: '14px 8px' }}>
            <div className="mono" style={{ fontSize: 9, color: TERM.danger, letterSpacing: 2 }}>GRID</div>
            <div className="mono" style={{ fontSize: 36, fontWeight: 700, color: '#fff', letterSpacing: 2 }}>G14</div>
          </div>
          <div style={{ background: '#0A0000', padding: '14px 8px' }}>
            <div className="mono" style={{ fontSize: 9, color: TERM.danger, letterSpacing: 2 }}>BASE</div>
            <div className="mono" style={{ fontSize: 18, fontWeight: 600, color: '#fff', letterSpacing: 1, marginTop: 4 }}>BLDG ALPHA</div>
            <div className="mono" style={{ fontSize: 9, color: TERM.textDim, marginTop: 2 }}>EU 3X MAX</div>
          </div>
        </div>

        <div className="mono" style={{ fontSize: 9, color: TERM.danger, marginTop: 14, letterSpacing: 2, animation: 'adley-blink 1s infinite' }}>
          ▶ SIREN :: raid_siren.caf · LOOP
        </div>
        <div className="mono" style={{ fontSize: 10, color: TERM.textDim, marginTop: 24, letterSpacing: 1 }}>
          T+00:08 · 6 SQUADMATES NOTIFIED
        </div>

        <div style={{ marginTop: 22, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 6 }}>
          <TBtn accent={TERM.danger} active style={{ borderColor: TERM.danger, background: TERM.danger, color: '#fff', fontWeight: 700 }}>⌖ JUMP TO MAP</TBtn>
          <TBtn accent={TERM.danger}>SILENCE 60s</TBtn>
        </div>
      </div>
      <div className="adley-scanline-overlay" />
    </div>
  );
}

// ───────────────────────────────────────────────
// LOCK SCREEN + LIVE ACTIVITY
// ───────────────────────────────────────────────
function TerminalLockScreen({ accent }) {
  return (
    <div style={{ height: '100%', background: '#000', color: TERM.text, position: 'relative', overflow: 'hidden' }}>
      {/* Wallpaper: dark satellite-ish */}
      <div style={{ position: 'absolute', inset: 0,
        background: `radial-gradient(ellipse at 30% 20%, ${accent}15, transparent 50%), radial-gradient(ellipse at 80% 60%, ${TERM.danger}10, transparent 60%), #050508`,
      }}>
        <svg width="100%" height="100%" style={{ position: 'absolute', inset: 0, opacity: 0.15 }}>
          {Array.from({ length: 40 }).map((_, i) => (
            <line key={i} x1={0} y1={i * 22} x2="100%" y2={i * 22} stroke={accent} strokeWidth="0.4" />
          ))}
          {Array.from({ length: 20 }).map((_, i) => (
            <line key={`v${i}`} x1={i * 22} y1={0} x2={i * 22} y2="100%" stroke={accent} strokeWidth="0.4" />
          ))}
        </svg>
      </div>

      {/* Time */}
      <div style={{ position: 'absolute', top: 88, left: 0, right: 0, textAlign: 'center', zIndex: 10 }}>
        <div className="mono" style={{ fontSize: 14, color: TERM.textDim, letterSpacing: 2, marginBottom: 4 }}>FRI 22 MAY</div>
        <div className="mono" style={{ fontSize: 84, fontWeight: 300, color: '#fff', letterSpacing: -2, lineHeight: 1 }}>02:14</div>
      </div>

      {/* Live Activity card (lockscreen) */}
      <div style={{ position: 'absolute', top: 280, left: 14, right: 14, zIndex: 10 }}>
        <div style={{
          background: 'rgba(15,15,20,0.85)', border: `1px solid ${accent}66`, padding: 14,
          backdropFilter: 'blur(20px)', WebkitBackdropFilter: 'blur(20px)',
        }}>
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 10 }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
              <div style={{ width: 22, height: 22, background: accent, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                <span className="mono" style={{ color: '#000', fontSize: 11, fontWeight: 700 }}>A</span>
              </div>
              <span className="mono" style={{ fontSize: 11, color: TERM.text, letterSpacing: 2, fontWeight: 600 }}>ADLEY · EU-3X</span>
            </div>
            <span className="mono" style={{ fontSize: 9, color: TERM.ok, letterSpacing: 1 }}>● LIVE</span>
          </div>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 8 }}>
            <div>
              <div className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 1 }}>POP</div>
              <div className="mono" style={{ fontSize: 22, fontWeight: 600, color: '#fff', lineHeight: 1 }}>187</div>
              <div className="mono" style={{ fontSize: 9, color: TERM.textMute }}>/250</div>
            </div>
            <div>
              <div className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 1 }}>FPS</div>
              <div className="mono" style={{ fontSize: 22, fontWeight: 600, color: TERM.ok, lineHeight: 1 }}>252</div>
              <div className="mono" style={{ fontSize: 9, color: TERM.textMute }}>STABLE</div>
            </div>
            <div>
              <div className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 1 }}>RF ARMED</div>
              <div className="mono" style={{ fontSize: 22, fontWeight: 600, color: accent, lineHeight: 1 }}>2</div>
              <div className="mono" style={{ fontSize: 9, color: TERM.textMute }}>SQUAD</div>
            </div>
          </div>
        </div>
        <div className="mono" style={{ fontSize: 8, color: TERM.textMute, letterSpacing: 2, textAlign: 'center', marginTop: 6 }}>
          LIVE ACTIVITY · WIDGETKIT
        </div>
      </div>

      {/* Notification stack (raid alert pushed) */}
      <div style={{ position: 'absolute', top: 440, left: 14, right: 14, zIndex: 10 }}>
        <div style={{
          background: 'rgba(40,5,5,0.92)', border: `1px solid ${TERM.danger}`,
          padding: 14, animation: 'adley-strobe-soft 0.8s steps(1) infinite',
          backdropFilter: 'blur(20px)', WebkitBackdropFilter: 'blur(20px)',
        }}>
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 6 }}>
            <span className="mono" style={{ fontSize: 11, color: TERM.danger, letterSpacing: 2, fontWeight: 700 }}>▲ RAID · ADLEY</span>
            <span className="mono" style={{ fontSize: 9, color: TERM.textDim }}>now</span>
          </div>
          <div className="mono" style={{ fontSize: 13, color: '#fff', fontWeight: 600, letterSpacing: 1, marginBottom: 4 }}>TC TRIPPED · 4763</div>
          <div className="mono" style={{ fontSize: 11, color: TERM.textDim, letterSpacing: 1 }}>G14 · BLDG ALPHA · STORM-7</div>
        </div>
      </div>

      {/* Dynamic island in raid mode */}
      <div style={{
        position: 'absolute', top: 11, left: '50%', transform: 'translateX(-50%)',
        background: '#000', borderRadius: 24, height: 37, padding: '0 14px 0 50px',
        display: 'flex', alignItems: 'center', gap: 8, zIndex: 51,
      }}>
        <div style={{ width: 8, height: 8, borderRadius: '50%', background: TERM.danger, animation: 'adley-blink 0.5s infinite' }} />
        <span className="mono" style={{ fontSize: 11, color: TERM.danger, fontWeight: 700, letterSpacing: 1 }}>RAID · G14</span>
      </div>

      {/* Bottom flashlight + camera */}
      <div style={{ position: 'absolute', bottom: 60, left: 0, right: 0, display: 'flex', justifyContent: 'space-between', padding: '0 32px', zIndex: 10 }}>
        <div style={{ width: 44, height: 44, borderRadius: '50%', background: 'rgba(40,40,50,0.6)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
          <Icon name="flashlight" size={18} color="#fff"/>
        </div>
        <div style={{ width: 44, height: 44, borderRadius: '50%', background: 'rgba(40,40,50,0.6)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
          <Icon name="camera" size={18} color="#fff"/>
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// ARMED FREQUENCIES
// ───────────────────────────────────────────────
function TerminalFreqs({ accent }) {
  return (
    <div style={{ height: '100%', background: TERM.bg, color: TERM.text, position: 'relative' }}>
      <THeader title="ARMED RF" sub="TRIP-WIRES" accent={accent} right={
        <button className="mono" style={{ border: `1px solid ${accent}`, background: 'transparent', color: accent, padding: '6px 8px', fontSize: 10, cursor: 'pointer', letterSpacing: 1 }}>
          [+] ARM
        </button>
      } />
      <div style={{ padding: '4px 14px 100px', overflow: 'auto', height: 'calc(100% - 120px)' }}>
        <div className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 1, padding: '6px 0' }}>
          5 ARMED · 3 SQUAD · 2 USER
        </div>
        {FREQS.map(f => (
          <div key={f.id} style={{ padding: '12px 0', borderTop: `1px solid ${TERM.line}`, display: 'flex', alignItems: 'flex-start', gap: 12 }}>
            <div className="mono" style={{
              fontSize: 28, fontWeight: 700, color: f.triggered.includes('ago') ? TERM.danger : accent,
              fontVariantNumeric: 'tabular-nums', letterSpacing: 1, lineHeight: 1,
              minWidth: 70, textAlign: 'left',
            }}>{f.freq}</div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 4 }}>
                <span className="mono" style={{ fontSize: 13, fontWeight: 600, letterSpacing: 0.5 }}>{f.label}</span>
                {f.locked && <Icon name="lock-simple" size={11} color={TERM.warn}/>}
              </div>
              <div className="mono" style={{ fontSize: 10, color: TERM.textDim, letterSpacing: 0.5 }}>
                <span style={{ color: accent }}>{f.grid}</span> · {f.scope}{f.squad ? ` :: ${f.squad}` : ''}
              </div>
              <div className="mono" style={{ fontSize: 9, color: f.triggered.includes('ago') ? TERM.danger : TERM.textMute, marginTop: 4, letterSpacing: 1 }}>
                LAST :: {f.triggered.toUpperCase()}
              </div>
            </div>
            <button className="mono" style={{ border: `1px solid ${TERM.line2}`, background: 'transparent', color: TERM.textDim, padding: '4px 6px', fontSize: 9, cursor: 'pointer', letterSpacing: 1 }}>···</button>
          </div>
        ))}

        <div style={{ marginTop: 16, padding: 12, border: `1px dashed ${accent}55` }}>
          <div className="mono" style={{ fontSize: 9, color: accent, letterSpacing: 2, marginBottom: 8 }}>┌─ ARM NEW FREQ</div>
          <div style={{ display: 'flex', gap: 4 }}>
            {[4,7,6,3].map((d, i) => (
              <div key={i} className="mono" style={{
                flex: 1, height: 44, border: `1px solid ${accent}66`,
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                fontSize: 24, color: accent, fontWeight: 600,
              }}>{d}</div>
            ))}
          </div>
          <div className="mono" style={{ display: 'flex', justifyContent: 'space-between', marginTop: 10, fontSize: 9, color: TERM.textDim, letterSpacing: 1 }}>
            <span>RANGE 1–9999</span>
            <span>SCOPE: <span style={{ color: accent }}>SQUAD</span></span>
          </div>
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// SQUAD
// ───────────────────────────────────────────────
function TerminalSquad({ accent }) {
  const sq = SQUAD;
  return (
    <div style={{ height: '100%', background: TERM.bg, color: TERM.text, position: 'relative' }}>
      <THeader title={sq.name} sub={`SQUAD // ${sq.server}`} accent={accent}/>
      <div style={{ padding: '4px 14px 100px', overflow: 'auto', height: 'calc(100% - 120px)' }}>
        {/* Invite code block */}
        <div style={{ border: `1px solid ${accent}`, padding: 12, marginTop: 8, position: 'relative' }}>
          <div className="mono" style={{ fontSize: 9, color: accent, letterSpacing: 2, marginBottom: 6 }}>INVITE CODE</div>
          <div className="mono" style={{ fontSize: 28, fontWeight: 700, color: '#fff', letterSpacing: 3, fontVariantNumeric: 'tabular-nums' }}>{sq.code}</div>
          <div style={{ display: 'flex', gap: 6, marginTop: 10 }}>
            <button className="mono" style={{ flex: 1, padding: '6px 8px', border: `1px solid ${TERM.line2}`, background: 'transparent', color: TERM.text, fontSize: 10, letterSpacing: 1, cursor: 'pointer' }}>COPY</button>
            <button className="mono" style={{ flex: 1, padding: '6px 8px', border: `1px solid ${TERM.line2}`, background: 'transparent', color: TERM.text, fontSize: 10, letterSpacing: 1, cursor: 'pointer' }}>SHARE</button>
            <button className="mono" style={{ flex: 1, padding: '6px 8px', border: `1px solid ${TERM.line2}`, background: 'transparent', color: TERM.textDim, fontSize: 10, letterSpacing: 1, cursor: 'pointer' }}>REGEN</button>
          </div>
        </div>

        <div className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 2, marginTop: 18, marginBottom: 4 }}>
          ┌─ MEMBERS · {sq.members.length}
        </div>
        {sq.members.map(m => (
          <div key={m.id} style={{ padding: '10px 0', borderBottom: `1px solid ${TERM.line}`, display: 'flex', alignItems: 'center', gap: 10 }}>
            <div className="mono" style={{
              width: 32, height: 32, border: `1px solid ${m.status === 'ONLINE' ? TERM.ok : TERM.line2}`,
              color: m.status === 'ONLINE' ? TERM.ok : TERM.textMute,
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              fontWeight: 700, fontSize: 12, letterSpacing: 1,
            }}>{m.name.slice(0, 2)}</div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
                <span className="mono" style={{ fontSize: 13, fontWeight: 600, letterSpacing: 0.5 }}>{m.name}</span>
                {m.role === 'LEADER' && <span className="mono" style={{ fontSize: 8, color: accent, border: `1px solid ${accent}`, padding: '0 4px', letterSpacing: 1 }}>LEAD</span>}
              </div>
              <div className="mono" style={{ fontSize: 9, color: TERM.textDim }}>
                K/D {m.kdr} · {m.hours}H · <span style={{ color: m.status === 'ONLINE' ? TERM.ok : TERM.textMute }}>{m.status}</span>
              </div>
            </div>
            <button className="mono" style={{ border: 0, background: 'transparent', color: TERM.textMute, fontSize: 12, cursor: 'pointer' }}>···</button>
          </div>
        ))}

        <button className="mono" style={{ marginTop: 14, width: '100%', padding: 10, border: `1px solid ${TERM.danger}`, background: 'transparent', color: TERM.danger, fontSize: 11, letterSpacing: 2, cursor: 'pointer' }}>
          LEAVE SQUAD
        </button>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// EVENT FEED (SSE)
// ───────────────────────────────────────────────
function TerminalEvents({ accent }) {
  const sev = (s) => s === 'crit' ? TERM.danger : s === 'warn' ? TERM.warn : TERM.textDim;
  return (
    <div style={{ height: '100%', background: TERM.bg, color: TERM.text, position: 'relative' }}>
      <THeader title="EVENT LOG" sub="SSE // LIVE" accent={accent} right={
        <span className="mono" style={{ fontSize: 9, color: TERM.ok, letterSpacing: 1 }}>● STREAM</span>
      } />
      <div style={{ padding: '4px 14px 100px', overflow: 'auto', height: 'calc(100% - 120px)' }}>
        {/* Filter chips */}
        <div style={{ display: 'flex', gap: 4, padding: '8px 0', flexWrap: 'wrap' }}>
          {['ALL', 'RAID', 'PVP', 'WORLD', 'RF'].map((f, i) => (
            <button key={f} className="mono" style={{
              border: `1px solid ${i === 0 ? accent : TERM.line2}`,
              background: i === 0 ? `${accent}22` : 'transparent',
              color: i === 0 ? accent : TERM.textDim, padding: '4px 8px', fontSize: 9, letterSpacing: 1, cursor: 'pointer',
            }}>{f}</button>
          ))}
        </div>
        <div><TRule color={TERM.line2}/></div>
        {EVENTS.map((e, i) => (
          <div key={i} style={{ padding: '8px 0', borderBottom: `1px solid ${TERM.line}`, display: 'flex', gap: 8, alignItems: 'flex-start' }}>
            <span className="mono" style={{ fontSize: 10, color: TERM.textMute, fontVariantNumeric: 'tabular-nums', minWidth: 56 }}>{e.t}</span>
            <span className="mono" style={{ fontSize: 9, color: sev(e.sev), border: `1px solid ${sev(e.sev)}`, padding: '0 4px', letterSpacing: 1, minWidth: 38, textAlign: 'center' }}>{e.kind.toUpperCase()}</span>
            <span className="mono" style={{ fontSize: 11, color: e.sev === 'crit' ? TERM.danger : TERM.text, flex: 1, letterSpacing: 0.5 }}>{e.text}</span>
          </div>
        ))}
        <div className="mono" style={{ fontSize: 9, color: TERM.textMute, textAlign: 'center', padding: 12, letterSpacing: 2 }}>
          ─── LOAD OLDER ───
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// STATS
// ───────────────────────────────────────────────
function TerminalStats({ accent }) {
  return (
    <div style={{ height: '100%', background: TERM.bg, color: TERM.text, position: 'relative' }}>
      <THeader title="STATS" sub="EU-3X · 24H" accent={accent}/>
      <div style={{ padding: '4px 14px 100px', overflow: 'auto', height: 'calc(100% - 120px)' }}>
        {[
          ['RAID EVENTS', STATS_24H.raids, TERM.danger, 9, 'TC trips · explosions'],
          ['PVP KILLS', STATS_24H.pvp, accent, 64, 'players · sleepers'],
          ['WORLD EVENTS', STATS_24H.world, TERM.warn, 71, 'cargo · heli · supply'],
        ].map(([label, data, color, total, sub]) => (
          <div key={label} style={{ marginTop: 14 }}>
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 6 }}>
              <div className="mono" style={{ fontSize: 10, color: TERM.textDim, letterSpacing: 2 }}>{label}</div>
              <div className="mono" style={{ fontSize: 22, fontWeight: 600, color, fontVariantNumeric: 'tabular-nums' }}>{total}</div>
            </div>
            <Bars data={data} color={color} height={56}/>
            <div className="mono" style={{ display: 'flex', justifyContent: 'space-between', fontSize: 8, color: TERM.textMute, marginTop: 2, letterSpacing: 1 }}>
              <span>−24H</span><span>−18</span><span>−12</span><span>−06</span><span>NOW</span>
            </div>
            <div className="mono" style={{ fontSize: 9, color: TERM.textMute, marginTop: 4, letterSpacing: 1 }}>{sub}</div>
          </div>
        ))}
        <div style={{ marginTop: 16 }}><TRule color={TERM.line2}/></div>
        <div className="mono" style={{ fontSize: 9, color: TERM.textDim, marginTop: 8, letterSpacing: 1 }}>
          // PEAK :: 20:00 · 8 KILLS / HR
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// RCON ONBOARDING
// ───────────────────────────────────────────────
function TerminalRcon({ accent }) {
  return (
    <div style={{ height: '100%', background: TERM.bg, color: TERM.text, position: 'relative' }}>
      <div style={{ padding: '52px 14px 6px', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <span className="mono" style={{ fontSize: 11, color: TERM.textDim, letterSpacing: 1 }}>◂ CANCEL</span>
        <span className="mono" style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 2 }}>STEP 2 / 3</span>
      </div>
      <div style={{ padding: '0 14px' }}>
        <div className="mono" style={{ color: accent, fontSize: 10, letterSpacing: 2, marginTop: 14 }}>ADLEY // ADD SERVER</div>
        <div className="mono" style={{ fontSize: 22, fontWeight: 600, letterSpacing: 0.5, textTransform: 'uppercase', marginTop: 4 }}>RCON CREDS</div>
        <div className="mono" style={{ fontSize: 11, color: TERM.textDim, marginTop: 6, letterSpacing: 0.5, lineHeight: 1.5 }}>
          Adley connects via WebRCON. Use port + password from{' '}
          <span style={{ color: accent }}>server.cfg</span>.
        </div>
        <div style={{ marginTop: 16 }}><TRule color={TERM.line2}/></div>

        {[
          { l: 'HOST / IP', v: '185.244.218.42', mono: true, ok: true },
          { l: 'RCON PORT', v: '28016', mono: true, ok: true },
          { l: 'PASSWORD', v: '••••••••••••', mono: true, ok: true },
          { l: 'NICKNAME', v: 'EU 3X MAX', mono: false, ok: true },
        ].map(f => (
          <div key={f.l} style={{ padding: '12px 0', borderBottom: `1px solid ${TERM.line}` }}>
            <div className="mono" style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 4 }}>
              <span style={{ fontSize: 9, color: TERM.textMute, letterSpacing: 2 }}>{f.l}</span>
              {f.ok && <span style={{ fontSize: 9, color: TERM.ok, letterSpacing: 1 }}>✓ OK</span>}
            </div>
            <div className="mono" style={{
              fontSize: 14, padding: '8px 10px', border: `1px solid ${TERM.line2}`, background: TERM.panel,
              fontFamily: f.mono ? "'JetBrains Mono', monospace" : "'JetBrains Mono', monospace",
              letterSpacing: 0.5, color: TERM.text,
            }}>{f.v}</div>
          </div>
        ))}

        <div style={{ marginTop: 14, padding: 10, border: `1px solid ${TERM.ok}33`, background: `${TERM.ok}08` }}>
          <div className="mono" style={{ fontSize: 10, color: TERM.ok, letterSpacing: 2, marginBottom: 6 }}>● HANDSHAKE OK</div>
          <div className="mono" style={{ fontSize: 10, color: TERM.textDim, lineHeight: 1.5 }}>
            &gt; identify… <span style={{ color: TERM.ok }}>Rust 2503 · build 87642</span><br/>
            &gt; subscribe sse… <span style={{ color: TERM.ok }}>OK</span><br/>
            &gt; entities snapshot… <span style={{ color: TERM.ok }}>4 found</span>
          </div>
        </div>

        <button className="mono" style={{
          marginTop: 18, width: '100%', padding: 14, background: accent, color: '#000',
          border: 0, fontSize: 13, fontWeight: 700, letterSpacing: 3, cursor: 'pointer',
        }}>
          ▸ LINK SERVER
        </button>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────
// FLOW WRAPPER — phone shell with bottom nav
// ───────────────────────────────────────────────
function TerminalFlow({ accent, initial = 'home' }) {
  const [screen, setScreen] = React.useState(initial);
  const [serverOpen, setServerOpen] = React.useState(false);

  const view = serverOpen ? <TerminalSnapshot accent={accent} onBack={() => setServerOpen(false)} onOpenMap={() => { setServerOpen(false); setScreen('map'); }} />
    : screen === 'home' ? <TerminalHome accent={accent} onOpenServer={() => setServerOpen(true)}/>
    : screen === 'map' ? <TerminalMap accent={accent}/>
    : screen === 'freqs' ? <TerminalFreqs accent={accent}/>
    : screen === 'events' ? <TerminalEvents accent={accent}/>
    : screen === 'squad' ? <TerminalSquad accent={accent}/>
    : <TerminalHome accent={accent}/>;

  return (
    <div style={{ height: '100%', position: 'relative', background: TERM.bg }}>
      {view}
      <TTabBar screen={serverOpen ? '' : screen} onChange={(id) => { setServerOpen(false); setScreen(id); }} accent={accent}/>
      <div className="adley-scanline-overlay" style={{ opacity: 0.4 }} />
    </div>
  );
}

Object.assign(window, {
  TerminalFlow, TerminalHome, TerminalSnapshot, TerminalMap, TerminalRaidAlert,
  TerminalLockScreen, TerminalFreqs, TerminalSquad, TerminalEvents, TerminalStats, TerminalRcon,
});
