// dashboard.jsx — Bot Control Panel with iOS-style swipeable bot pages

const BOT_TYPES = [
  { id: 'viewbot',  label: 'Viewbots',      hue: 142, icon: 'viewers', desc: 'Real-looking concurrent viewers',  unit: 'viewers',   max: 5000 },
  { id: 'chatbot',  label: 'Chatbots',      hue: 295, icon: 'chat',    desc: 'Scripted chatters (not AI)',        unit: 'chatters',  max: 200 },
  { id: 'pollbot',  label: 'Pollbots',      hue: 200, icon: 'spark',   desc: 'Push poll responses on cue',        unit: 'votes',     max: 1000 },
  { id: 'clipbot',  label: 'Clip Viewbots', hue: 25,  icon: 'clip',    desc: 'Boost specific clip view counts',   unit: 'clip views',max: 50000 },
  { id: 'followbot',label: 'Followbots',    hue: 340, icon: 'heart',   desc: 'Drip-fed followers over time',       unit: 'followers', max: 10000 },
];

const DEFAULT_BOT_STATE = {
  viewbot:   { running: true,  channel: 'pump_it_up_live', count: 1200, delay: [4, 12], rampMin: 8 },
  chatbot:   { running: true,  channel: 'pump_it_up_live', count: 24,   delay: [15, 45], rampMin: 0 },
  pollbot:   { running: false, channel: 'pump_it_up_live', count: 350,  delay: [2, 6],   rampMin: 0 },
  clipbot:   { running: false, channel: 'pump_it_up_live', count: 8500, delay: [1, 4],   rampMin: 30, clipUrl: 'kick.com/pump_it_up_live/clip/...' },
  followbot: { running: false, channel: 'pump_it_up_live', count: 500,  delay: [60, 180],rampMin: 240 },
};

const AUTO_TASKS_INITIAL = [
  { id: 1, channel: 'pump_it_up_live', platform: 'kick',   viewers: 1500, delay: 45,  ramp: 6,  status: 'armed',  nextLive: 'Tonight 8:00 PM' },
  { id: 2, channel: 'shadowfern',      platform: 'twitch', viewers: 800,  delay: 30,  ramp: 4,  status: 'firing', nextLive: 'LIVE NOW · firing' },
  { id: 3, channel: 'kicktastic',      platform: 'kick',   viewers: 2400, delay: 120, ramp: 12, status: 'armed',  nextLive: 'Wed 9:00 PM' },
  { id: 4, channel: 'casualkj',        platform: 'kick',   viewers: 600,  delay: 20,  ramp: 5,  status: 'paused', nextLive: 'Paused' },
];

function Dashboard() {
  const [platform, setPlatform] = React.useState('kick');
  const [activeBot, setActiveBot] = React.useState('viewbot');
  const [bots, setBots] = React.useState(DEFAULT_BOT_STATE);
  const [tasks, setTasks] = React.useState(AUTO_TASKS_INITIAL);

  const update = (id, patch) => setBots((b) => ({ ...b, [id]: { ...b[id], ...patch } }));
  const activeCount = Object.values(bots).filter((b) => b.running).length;

  return (
    <Stage>
      <div style={{ display: 'grid', gridTemplateColumns: '240px 1fr', minHeight: '100%' }}>
        <DashSidebar active="overview"/>
        <div style={{ padding: '24px 40px 80px', maxWidth: 1320 }}>
          <DashTopbar platform={platform} setPlatform={setPlatform}/>

          <div className="vb-row" style={{ marginTop: 32, marginBottom: 24, alignItems: 'flex-end', flexWrap: 'wrap', gap: 12 }}>
            <div>
              <div style={{ fontSize: 11, color: 'var(--vb-fg-3)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600, marginBottom: 8 }}>Control</div>
              <div className="vb-row" style={{ gap: 12, marginBottom: 6, alignItems: 'center', flexWrap: 'wrap' }}>
                <h1 className="vb-h1" style={{ fontSize: 30, letterSpacing: '-0.02em' }}>Bot Control Panel</h1>
                <span style={{ fontSize: 10, padding: '4px 9px', borderRadius: 100, background: 'oklch(0.85 0.18 142 / 0.14)', color: 'oklch(0.85 0.18 142)', fontWeight: 700, letterSpacing: '0.06em', display: 'inline-flex', alignItems: 'center', gap: 6 }}>
                  <span style={{ width: 6, height: 6, borderRadius: 3, background: 'oklch(0.85 0.18 142)', boxShadow: '0 0 8px oklch(0.85 0.18 142)', animation: 'vb-pulse 1.6s ease-in-out infinite' }}/>
                  {activeCount} ACTIVE
                </span>
              </div>
              <p className="vb-body" style={{ fontSize: 14, color: 'var(--vb-fg-2)', maxWidth: 540 }}>
                One panel, every bot. Pick your platform, pick your bot, dial it in.
              </p>
            </div>
            <span className="vb-spacer"/>
            <Btn variant="glass" icon={<IconBolt size={14}/>}>Pause all</Btn>
            <Btn variant="primary" icon={<IconPlus size={14}/>} style={{ marginLeft: 10 }}>Start all</Btn>
          </div>

          <Glass style={{ padding: 0, overflow: 'hidden', boxShadow: '0 1px 0 rgba(255,255,255,0.04) inset, 0 24px 48px -24px rgba(0,0,0,0.6)' }}>
            <div className="vb-row" style={{ padding: '16px 24px', borderBottom: '1px solid rgba(255,255,255,0.05)', background: 'linear-gradient(180deg, rgba(255,255,255,0.025), rgba(255,255,255,0.005))', gap: 20, flexWrap: 'wrap' }}>
              <span style={{ fontSize: 10, color: 'var(--vb-fg-3)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600 }}>Platform</span>
              <PlatformToggle value={platform} onChange={setPlatform}/>
              <span className="vb-spacer"/>
              <div className="vb-row" style={{ gap: 20 }}>
                <div className="vb-col" style={{ alignItems: 'flex-end', gap: 2 }}>
                  <span style={{ fontSize: 10, color: 'var(--vb-fg-3)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600 }}>Total signal</span>
                  <span className="vb-mono" style={{ fontSize: 17, fontWeight: 600, color: 'var(--vb-fg-0)', letterSpacing: '-0.01em', fontVariantNumeric: 'tabular-nums' }}>
                    <LiveCounter base={Object.values(bots).reduce((s,b) => s + (b.running ? b.count : 0), 0)} jitter={20} interval={1300}/>
                  </span>
                </div>
                <div style={{ width: 1, alignSelf: 'stretch', background: 'rgba(255,255,255,0.08)' }}/>
                <div className="vb-col" style={{ alignItems: 'flex-end', gap: 2 }}>
                  <span style={{ fontSize: 10, color: 'var(--vb-fg-3)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600 }}>Burn rate</span>
                  <span className="vb-mono" style={{ fontSize: 17, fontWeight: 600, color: 'oklch(0.85 0.18 142)', letterSpacing: '-0.01em', fontVariantNumeric: 'tabular-nums' }}>
                    ${Object.entries(bots).filter(([_,b]) => b.running).reduce((sum, [id, b]) => {
                      const rates = { viewbot: 0.0008, chatbot: 0.02, pollbot: 0.001, clipbot: 0.0001, followbot: 0.04 };
                      return sum + b.count * rates[id];
                    }, 0).toFixed(2)}<span style={{ fontSize: 11, color: 'var(--vb-fg-3)', fontWeight: 500, marginLeft: 2 }}>/hr</span>
                  </span>
                </div>
              </div>
            </div>

            <BotPages types={BOT_TYPES} active={activeBot} onChange={setActiveBot} bots={bots} platform={platform} update={update}/>
          </Glass>

          <AutoTasksSection tasks={tasks} setTasks={setTasks}/>
        </div>
      </div>
      <style>{`
        .bc-input { background: rgba(0,0,0,0.35); border: 1px solid rgba(255,255,255,0.06); border-radius: 9px; color: var(--vb-fg-0); padding: 9px 12px; font-family: var(--vb-font-mono); font-size: 13px; outline: none; transition: border-color 0.18s, background 0.18s, box-shadow 0.18s; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
        .bc-input:hover { border-color: rgba(255,255,255,0.1); }
        .bc-input:focus { border-color: oklch(0.78 0.18 295 / 0.5); background: rgba(0,0,0,0.45); box-shadow: 0 0 0 3px oklch(0.78 0.18 295 / 0.12); }
        .bc-input::placeholder { color: var(--vb-fg-3); }
        .bc-input::-webkit-outer-spin-button, .bc-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
        .bc-input[type="number"] { -moz-appearance: textfield; }
        .bc-num { width: 80px; text-align: center; }
        .bc-channel { flex: 1; }
        .bc-btn-secondary { background: rgba(255,255,255,0.04); border: 1px solid rgba(255,255,255,0.07); border-radius: 9px; padding: 9px 14px; color: var(--vb-fg-1); font-size: 12.5px; font-weight: 500; cursor: pointer; font-family: inherit; letter-spacing: -0.005em; transition: all 0.15s; }
        .bc-btn-secondary:hover { background: rgba(255,255,255,0.07); border-color: rgba(255,255,255,0.12); }
        .bc-btn-primary { border: none; border-radius: 9px; padding: 9px 14px; font-size: 12.5px; font-weight: 600; cursor: pointer; font-family: inherit; letter-spacing: -0.005em; transition: transform 0.12s, box-shadow 0.12s; }
        .bc-btn-primary:hover { transform: translateY(-0.5px); }
        .bc-btn-primary:active { transform: translateY(0.5px); }
        .bc-range::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; cursor: grab; }
        .bc-range:active::-webkit-slider-thumb { cursor: grabbing; }
        .bc-task-row:hover { background: rgba(255,255,255,0.018); }
        .bc-icon-btn { background: transparent; border: 1px solid rgba(255,255,255,0.06); border-radius: 7px; padding: 5px 7px; color: var(--vb-fg-3); cursor: pointer; transition: all 0.15s; display: inline-flex; align-items: center; justify-content: center; }
        .bc-icon-btn:hover { background: rgba(255,255,255,0.05); color: var(--vb-fg-1); border-color: rgba(255,255,255,0.12); }
        .bc-mini-btn { background: rgba(255,255,255,0.04); border: 1px solid rgba(255,255,255,0.07); border-radius: 7px; padding: 5px 11px; color: var(--vb-fg-2); font-size: 11.5px; font-weight: 500; cursor: pointer; font-family: inherit; letter-spacing: -0.005em; transition: all 0.15s; }
        .bc-mini-btn:hover { background: rgba(255,255,255,0.07); color: var(--vb-fg-1); border-color: rgba(255,255,255,0.12); }
        @keyframes bp-rise { 0% { opacity: 0; transform: translateY(0) } 15% { opacity: 1 } 100% { opacity: 0; transform: translateY(-50px) } }
        @keyframes bp-chatscroll { 0% { transform: translateY(50%) } 100% { transform: translateY(-50%) } }
        @keyframes bp-scanline { 0% { transform: translateY(0) } 100% { transform: translateY(75px) } }
        @keyframes bp-float { 0% { transform: translate(0, 0) scale(0.8); opacity: 0 } 12% { opacity: 1 } 100% { transform: translate(var(--drift), -180px) scale(1.1); opacity: 0 } }
        .bp-pill:hover { transform: translateY(-1px); }
      `}</style>
    </Stage>
  );
}

function PlatformToggle({ value, onChange }) {
  const opts = [
    { id: 'kick',   label: 'Kick',   icon: <IconKick size={13}/>,    color: 'oklch(0.85 0.18 142)' },
    { id: 'twitch', label: 'Twitch', icon: <IconTwitch size={13}/>,  color: 'oklch(0.78 0.2 295)' },
  ];
  return (
    <div style={{ padding: 3, display: 'inline-flex', borderRadius: 9, background: 'rgba(0,0,0,0.4)', border: '1px solid rgba(255,255,255,0.05)', gap: 0, position: 'relative', boxShadow: 'inset 0 1px 2px rgba(0,0,0,0.2)' }}>
      {opts.map((p) => {
        const active = value === p.id;
        return (
          <button key={p.id} onClick={() => onChange(p.id)}
            style={{
              display: 'flex', alignItems: 'center', gap: 7,
              padding: '6px 14px', borderRadius: 7, border: 'none', cursor: 'pointer',
              background: active ? 'rgba(255,255,255,0.08)' : 'transparent',
              color: active ? p.color : 'var(--vb-fg-2)',
              fontSize: 12.5, fontWeight: 600, fontFamily: 'inherit', letterSpacing: '-0.005em',
              boxShadow: active ? '0 1px 0 rgba(255,255,255,0.06) inset, 0 4px 12px -4px rgba(0,0,0,0.4)' : 'none',
              transition: 'all 0.18s ease',
            }}>
            {p.icon}
            <span>{p.label}</span>
          </button>
        );
      })}
    </div>
  );
}

function BotPages({ types, active, onChange, bots, platform, update }) {
  const idx = types.findIndex((t) => t.id === active);
  const activeType = types[idx];

  return (
    <div style={{ position: 'relative', overflow: 'hidden' }}>
      <div style={{ position: 'absolute', inset: 0, background: `radial-gradient(ellipse at 80% 0%, oklch(0.78 0.18 ${activeType.hue} / 0.08), transparent 60%), radial-gradient(ellipse at 20% 100%, oklch(0.78 0.18 ${activeType.hue} / 0.05), transparent 50%)`, pointerEvents: 'none', transition: 'background 0.5s ease' }}/>

      <BotPagePills types={types} active={active} onChange={onChange} bots={bots}/>

      <div style={{ position: 'relative', overflow: 'hidden' }}>
        <div style={{
          display: 'flex',
          width: `${types.length * 100}%`,
          transform: `translateX(-${idx * (100 / types.length)}%)`,
          transition: 'transform 0.55s cubic-bezier(0.32, 0.72, 0, 1)',
        }}>
          {types.map((t) => (
            <div key={t.id} style={{ width: `${100 / types.length}%`, flexShrink: 0 }}>
              <BotPage type={t} state={bots[t.id]} platform={platform} onChange={(patch) => update(t.id, patch)} isActive={t.id === active}/>
            </div>
          ))}
        </div>

        <BotPageArrow direction="prev" disabled={idx === 0} onClick={() => onChange(types[Math.max(0, idx - 1)].id)}/>
        <BotPageArrow direction="next" disabled={idx === types.length - 1} onClick={() => onChange(types[Math.min(types.length - 1, idx + 1)].id)}/>
      </div>
    </div>
  );
}

function BotPagePills({ types, active, onChange, bots }) {
  const ICONS = { viewers: IconViewers, chat: IconChat, spark: IconSpark, clip: IconClip, heart: IconHeart };
  return (
    <div style={{ display: 'flex', gap: 4, padding: '14px 16px', borderBottom: '1px solid rgba(255,255,255,0.05)', background: 'linear-gradient(180deg, rgba(0,0,0,0.18), rgba(0,0,0,0.04))', position: 'relative', zIndex: 2 }}>
      {types.map((t) => {
        const Icon = ICONS[t.icon];
        const isActive = active === t.id;
        const isRunning = bots[t.id].running;
        const accent = `oklch(0.78 0.18 ${t.hue})`;
        return (
          <button key={t.id} onClick={() => onChange(t.id)}
            className="bp-pill"
            style={{
              flex: isActive ? 1.6 : 1, position: 'relative', overflow: 'hidden',
              padding: '10px 14px', border: 'none', cursor: 'pointer', fontFamily: 'inherit',
              borderRadius: 10,
              background: isActive ? `linear-gradient(180deg, oklch(0.78 0.18 ${t.hue} / 0.18), oklch(0.78 0.18 ${t.hue} / 0.08))` : 'rgba(255,255,255,0.025)',
              border: isActive ? `1px solid oklch(0.78 0.18 ${t.hue} / 0.35)` : '1px solid rgba(255,255,255,0.04)',
              color: isActive ? 'var(--vb-fg-0)' : 'var(--vb-fg-2)',
              transition: 'all 0.5s cubic-bezier(0.32, 0.72, 0, 1)',
              boxShadow: isActive ? `0 0 24px -8px ${accent}, inset 0 1px 0 rgba(255,255,255,0.08)` : 'none',
              display: 'flex', alignItems: 'center', justifyContent: isActive ? 'flex-start' : 'center', gap: 8,
            }}>
            <span style={{ color: isActive ? accent : 'var(--vb-fg-3)', display: 'flex', flexShrink: 0, transition: 'color 0.3s' }}>
              <Icon size={14}/>
            </span>
            {isActive ? (
              <>
                <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', overflow: 'hidden', minWidth: 0 }}>
                  <span style={{ fontSize: 12.5, fontWeight: 600, letterSpacing: '-0.01em', whiteSpace: 'nowrap' }}>{t.label}</span>
                  <span style={{ fontSize: 10, color: 'var(--vb-fg-3)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', maxWidth: '100%' }}>{t.desc}</span>
                </div>
                <span style={{ flex: 1 }}/>
                {isRunning && (
                  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: 10, color: 'oklch(0.85 0.18 142)', fontWeight: 600, letterSpacing: '0.04em', flexShrink: 0 }}>
                    <span style={{ width: 5, height: 5, borderRadius: 3, background: 'oklch(0.85 0.18 142)', boxShadow: '0 0 6px oklch(0.85 0.18 142)', animation: 'vb-pulse 1.4s ease-in-out infinite' }}/>
                    LIVE
                  </span>
                )}
              </>
            ) : (
              <>
                <span style={{ fontSize: 11.5, fontWeight: 500, letterSpacing: '-0.005em' }}>{t.label}</span>
                {isRunning && <span style={{ width: 5, height: 5, borderRadius: 3, background: 'oklch(0.85 0.18 142)', boxShadow: '0 0 6px oklch(0.85 0.18 142)', flexShrink: 0 }}/>}
              </>
            )}
          </button>
        );
      })}
    </div>
  );
}

function BotPageArrow({ direction, disabled, onClick }) {
  const isPrev = direction === 'prev';
  return (
    <button onClick={onClick} disabled={disabled}
      style={{
        position: 'absolute', top: '50%', transform: 'translateY(-50%)',
        [isPrev ? 'left' : 'right']: 14,
        width: 36, height: 36, borderRadius: 18,
        background: 'rgba(0,0,0,0.5)', backdropFilter: 'blur(12px)',
        border: '1px solid rgba(255,255,255,0.08)',
        color: disabled ? 'var(--vb-fg-3)' : 'var(--vb-fg-1)',
        cursor: disabled ? 'default' : 'pointer',
        opacity: disabled ? 0.3 : 1,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        zIndex: 3, transition: 'all 0.18s',
        boxShadow: '0 8px 20px -8px rgba(0,0,0,0.6)',
      }}>
      <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
        <path d={isPrev ? "M9 2L4 7l5 5" : "M5 2l5 5-5 5"} stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
      </svg>
    </button>
  );
}

function BotPage({ type, state, platform, onChange, isActive }) {
  const accent = `oklch(0.78 0.18 ${type.hue})`;
  const HEROES = {
    viewbot: BotHeroViewers,
    chatbot: BotHeroChat,
    pollbot: BotHeroPolls,
    clipbot: BotHeroClips,
    followbot: BotHeroFollows,
  };
  const Hero = HEROES[type.id];

  return (
    <div style={{ padding: '36px 40px 40px', position: 'relative' }}>
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 360px', gap: 32, marginBottom: 36, alignItems: 'stretch' }}>
        <div style={{ position: 'relative', height: 220, borderRadius: 16, overflow: 'hidden', background: `linear-gradient(135deg, oklch(0.78 0.18 ${type.hue} / 0.08), oklch(0.18 0.04 ${type.hue} / 0.4))`, border: `1px solid oklch(0.78 0.18 ${type.hue} / 0.18)` }}>
          {Hero && <Hero hue={type.hue} count={state.count} running={state.running} active={isActive}/>}
          <div style={{ position: 'absolute', top: 16, left: 18, right: 18, display: 'flex', alignItems: 'flex-start', gap: 10, zIndex: 2 }}>
            <span style={{ fontSize: 10, color: accent, textTransform: 'uppercase', letterSpacing: '0.1em', fontWeight: 700 }}>{platform === 'kick' ? 'Kick' : 'Twitch'} · {type.label}</span>
            <span style={{ flex: 1 }}/>
            {state.running ? (
              <span style={{ fontSize: 9.5, padding: '3px 8px', borderRadius: 100, background: 'oklch(0.85 0.18 142 / 0.18)', color: 'oklch(0.85 0.18 142)', fontWeight: 700, letterSpacing: '0.06em', display: 'inline-flex', alignItems: 'center', gap: 5 }}>
                <span style={{ width: 5, height: 5, borderRadius: 3, background: 'oklch(0.85 0.18 142)', boxShadow: '0 0 6px oklch(0.85 0.18 142)' }}/>RUNNING
              </span>
            ) : (
              <span style={{ fontSize: 9.5, padding: '3px 8px', borderRadius: 100, background: 'rgba(255,255,255,0.06)', color: 'var(--vb-fg-3)', fontWeight: 700, letterSpacing: '0.06em' }}>STOPPED</span>
            )}
          </div>
          <div style={{ position: 'absolute', bottom: 18, left: 22, right: 22, zIndex: 2 }}>
            <div style={{ fontSize: 13, color: 'var(--vb-fg-2)', letterSpacing: '-0.005em', marginBottom: 4 }}>{type.desc}</div>
          </div>
        </div>

        <div className="vb-col" style={{ gap: 14, padding: '18px 22px', borderRadius: 16, background: 'linear-gradient(180deg, rgba(255,255,255,0.03), rgba(255,255,255,0.005))', border: '1px solid rgba(255,255,255,0.06)', position: 'relative', overflow: 'hidden' }}>
          {state.running && <div style={{ position: 'absolute', top: -50, right: -50, width: 180, height: 180, background: `radial-gradient(circle, ${accent}, transparent 70%)`, opacity: 0.18, pointerEvents: 'none' }}/>}
          <div style={{ position: 'relative', zIndex: 1 }}>
            <span style={{ fontSize: 10, color: 'var(--vb-fg-3)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600 }}>Now sending</span>
            <div style={{ display: 'flex', alignItems: 'baseline', gap: 8, marginTop: 6 }}>
              <span className="vb-mono" style={{ fontSize: 44, fontWeight: 600, letterSpacing: '-0.035em', color: state.running ? accent : 'var(--vb-fg-3)', fontVariantNumeric: 'tabular-nums', lineHeight: 1 }}>
                {state.running ? <LiveCounter base={state.count} jitter={Math.max(1, Math.round(state.count*0.01))} interval={1500}/> : '—'}
              </span>
            </div>
            <span style={{ fontSize: 12, color: 'var(--vb-fg-3)', letterSpacing: '-0.005em', display: 'block', marginTop: 2 }}>
              {state.running ? `${type.unit}, live` : `${type.unit}, paused`}
            </span>
          </div>

          <span style={{ flex: 1 }}/>

          <BigStartButton running={state.running} accent={accent} hue={type.hue} onClick={() => onChange({ running: !state.running })}/>
        </div>
      </div>

      <div className="vb-col" style={{ gap: 22 }}>
        <BotChannelField platform={platform} value={state.channel} onChange={(v) => onChange({ channel: v })} accent={accent}/>

        {type.id === 'clipbot' && (
          <Field label="Clip URL">
            <input className="bc-input" style={{ padding: '11px 14px', fontSize: 13 }} placeholder="kick.com/.../clip/..." value={state.clipUrl || ''} onChange={(e) => onChange({ clipUrl: e.target.value })}/>
          </Field>
        )}

        <div style={{ display: 'grid', gridTemplateColumns: type.id === 'chatbot' || type.id === 'pollbot' ? '1fr 1fr' : '1fr 1fr 1fr', gap: 20 }}>
          <BotControlCard label={`Number of ${type.unit}`} value={state.count.toLocaleString()} accent={accent}>
            <RefinedSlider min={0} max={type.max} step={Math.max(1, Math.round(type.max/200))} value={state.count} onChange={(v) => onChange({ count: v })} accent={accent}/>
            <div style={{ display: 'flex', justifyContent: 'space-between', fontSize: 10, color: 'var(--vb-fg-3)', fontFamily: 'var(--vb-font-mono)', marginTop: 6 }}>
              <span>0</span><span>max {type.max.toLocaleString()}</span>
            </div>
          </BotControlCard>

          <BotControlCard label="Delay range" value={`${state.delay[0]}–${state.delay[1]}s`} accent={accent}>
            <div className="vb-row" style={{ gap: 8, alignItems: 'center', marginTop: 4 }}>
              <input className="bc-input bc-num" style={{ width: 64, fontWeight: 600 }} type="number" value={state.delay[0]} onChange={(e) => onChange({ delay: [+e.target.value, state.delay[1]] })}/>
              <span style={{ flex: 1, height: 1, background: 'linear-gradient(90deg, transparent, rgba(255,255,255,0.12), transparent)' }}/>
              <input className="bc-input bc-num" style={{ width: 64, fontWeight: 600 }} type="number" value={state.delay[1]} onChange={(e) => onChange({ delay: [state.delay[0], +e.target.value] })}/>
            </div>
            <div style={{ fontSize: 10, color: 'var(--vb-fg-3)', marginTop: 8, letterSpacing: '-0.005em' }}>random within range</div>
          </BotControlCard>

          {type.id !== 'chatbot' && type.id !== 'pollbot' && (
            <BotControlCard label="Ramp-up" value={state.rampMin === 0 ? 'instant' : `${state.rampMin} min`} accent={accent}>
              <RefinedSlider min={0} max={120} value={state.rampMin} onChange={(v) => onChange({ rampMin: v })} accent={accent}/>
              <div style={{ display: 'flex', justifyContent: 'space-between', fontSize: 10, color: 'var(--vb-fg-3)', marginTop: 6 }}>
                <span>instant</span><span>2 hours</span>
              </div>
            </BotControlCard>
          )}
        </div>
      </div>
    </div>
  );
}

function BotChannelField({ platform, value, onChange, accent }) {
  return (
    <div style={{ position: 'relative' }}>
      <span style={{ fontSize: 10, color: 'var(--vb-fg-3)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600, display: 'block', marginBottom: 8 }}>Target channel</span>
      <div className="vb-row" style={{ gap: 0, height: 52, borderRadius: 12, background: 'rgba(0,0,0,0.4)', border: '1px solid rgba(255,255,255,0.06)', overflow: 'hidden', transition: 'border-color 0.18s, box-shadow 0.18s' }}>
        <span style={{ padding: '0 18px', display: 'flex', alignItems: 'center', color: 'var(--vb-fg-3)', fontSize: 14, fontFamily: 'var(--vb-font-mono)', letterSpacing: '-0.01em', borderRight: '1px solid rgba(255,255,255,0.05)', background: 'rgba(0,0,0,0.25)' }}>
          {platform === 'kick' ? 'kick.com/' : 'twitch.tv/'}
        </span>
        <input value={value} onChange={(e) => onChange(e.target.value)}
          style={{ flex: 1, background: 'transparent', border: 'none', outline: 'none', padding: '0 18px', color: 'var(--vb-fg-0)', fontFamily: 'var(--vb-font-mono)', fontSize: 16, fontWeight: 600, letterSpacing: '-0.01em' }}/>
        <span style={{ padding: '0 18px', display: 'flex', alignItems: 'center', gap: 8, color: accent, fontSize: 11, fontWeight: 600, letterSpacing: '0.04em' }}>
          <span style={{ width: 6, height: 6, borderRadius: 3, background: 'oklch(0.85 0.18 142)', boxShadow: '0 0 6px oklch(0.85 0.18 142)' }}/>
          VERIFIED
        </span>
      </div>
    </div>
  );
}

function BotControlCard({ label, value, children, accent }) {
  return (
    <div style={{ padding: 16, borderRadius: 12, background: 'linear-gradient(180deg, rgba(255,255,255,0.025), rgba(255,255,255,0.005))', border: '1px solid rgba(255,255,255,0.06)' }}>
      <div className="vb-row" style={{ marginBottom: 10, alignItems: 'baseline' }}>
        <span style={{ fontSize: 10, color: 'var(--vb-fg-3)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600 }}>{label}</span>
        <span className="vb-spacer"/>
        <span className="vb-mono" style={{ fontSize: 16, fontWeight: 600, letterSpacing: '-0.015em', color: accent, fontVariantNumeric: 'tabular-nums' }}>{value}</span>
      </div>
      {children}
    </div>
  );
}

function BigStartButton({ running, accent, hue, onClick }) {
  return (
    <button onClick={onClick}
      style={{
        position: 'relative', width: '100%', height: 48, borderRadius: 12,
        cursor: 'pointer', fontFamily: 'inherit', fontSize: 14, fontWeight: 600, letterSpacing: '-0.005em',
        background: running ? 'rgba(255,255,255,0.06)' : `linear-gradient(180deg, oklch(0.85 0.18 ${hue}), oklch(0.7 0.18 ${hue}))`,
        color: running ? 'var(--vb-fg-1)' : '#0a0a0a',
        boxShadow: running ? 'inset 0 1px 0 rgba(255,255,255,0.05)' : `0 8px 24px -8px ${accent}, inset 0 1px 0 rgba(255,255,255,0.25)`,
        border: running ? '1px solid rgba(255,255,255,0.1)' : 'none',
        transition: 'all 0.2s',
        display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      }}
      className="bc-btn-primary">
      {running ? (
        <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <svg width="10" height="10" viewBox="0 0 10 10"><rect x="2" y="2" width="2.5" height="6" fill="currentColor"/><rect x="5.5" y="2" width="2.5" height="6" fill="currentColor"/></svg>
          Stop bot
        </span>
      ) : (
        <>
          <svg width="11" height="11" viewBox="0 0 11 11"><path d="M2.5 1.5L9 5.5l-6.5 4z" fill="currentColor"/></svg>
          Start bot
        </>
      )}
    </button>
  );
}

function BotHeroViewers({ hue, running, active }) {
  const cols = 28;
  const heights = React.useMemo(() => Array.from({ length: cols }, (_, i) => 0.3 + Math.sin(i * 0.5) * 0.15 + Math.random() * 0.4), []);
  const accent = `oklch(0.78 0.18 ${hue})`;
  return (
    <div style={{ position: 'absolute', inset: 0 }}>
      <svg viewBox="0 0 280 220" preserveAspectRatio="none" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <defs>
          <linearGradient id={`grad-v-${hue}`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor={accent} stopOpacity="0.4"/>
            <stop offset="100%" stopColor={accent} stopOpacity="0"/>
          </linearGradient>
        </defs>
        {heights.map((h, i) => {
          const x = (i / cols) * 280;
          const w = 280 / cols - 2;
          const barH = 100 + h * 90;
          return <rect key={i} x={x} y={220 - barH} width={w} height={barH} rx={1} fill={`url(#grad-v-${hue})`} stroke={accent} strokeOpacity="0.3"/>;
        })}
        <path
          d={`M 0 ${220 - heights[0] * 100 - 100} ${heights.map((h, i) => `L ${(i / (cols - 1)) * 280} ${220 - h * 90 - 100}`).join(' ')}`}
          fill="none" stroke={accent} strokeWidth="1.5" strokeOpacity="0.7"
        />
      </svg>
      {running && active && Array.from({ length: 4 }).map((_, i) => (
        <span key={i} style={{
          position: 'absolute', bottom: 50 + i * 30, left: `${15 + i * 22}%`,
          fontFamily: 'var(--vb-font-mono)', fontSize: 11, color: accent, fontWeight: 600,
          animation: `bp-rise 4s ease-out infinite ${i * 0.8}s`, opacity: 0,
        }}>+{Math.floor(Math.random() * 8) + 1}</span>
      ))}
    </div>
  );
}

function BotHeroChat({ hue, running, active }) {
  const messages = ["LET'S GO 🔥", 'W', 'pog', 'GG', 'HUGE', 'lmao', 'KEKW', 'crazy', 'goat', 'Kappa'];
  const lines = React.useMemo(() => Array.from({ length: 12 }, (_, i) => ({
    user: ['kj_92', 'shadowf', 'mvp', 'streambro', 'bigplay', 'clipper'][i % 6] + Math.floor(Math.random() * 99),
    msg: messages[i % messages.length],
    hue: [142, 295, 200, 25, 340][i % 5],
    delay: i * 0.3,
  })), []);
  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden' }}>
      <div style={{ position: 'absolute', top: 0, left: 0, right: 0, height: '100%', display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', gap: 4, padding: 16, animation: running && active ? 'bp-chatscroll 12s linear infinite' : 'none' }}>
        {[...lines, ...lines].map((l, i) => (
          <div key={i} style={{ display: 'flex', gap: 8, alignItems: 'baseline', fontSize: 11.5, fontFamily: 'var(--vb-font-mono)', whiteSpace: 'nowrap' }}>
            <span style={{ color: `oklch(0.78 0.16 ${l.hue})`, fontWeight: 700 }}>{l.user}</span>
            <span style={{ color: 'var(--vb-fg-1)' }}>{l.msg}</span>
          </div>
        ))}
      </div>
      <div style={{ position: 'absolute', top: 0, left: 0, right: 0, height: 60, background: `linear-gradient(180deg, oklch(0.18 0.04 ${hue} / 0.95), transparent)`, pointerEvents: 'none' }}/>
    </div>
  );
}

function BotHeroPolls({ hue, running, active }) {
  const accent = `oklch(0.78 0.18 ${hue})`;
  const opts = [
    { label: 'Yes',     pct: 0.72, hue: 142 },
    { label: 'No',      pct: 0.18, hue: 25 },
    { label: 'Maybe',   pct: 0.10, hue: 200 },
  ];
  return (
    <div style={{ position: 'absolute', inset: 0, padding: '50px 24px 24px', display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', gap: 10 }}>
      {opts.map((o, i) => (
        <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <span style={{ fontSize: 10, color: 'var(--vb-fg-2)', width: 36, fontFamily: 'var(--vb-font-mono)' }}>{o.label}</span>
          <div style={{ flex: 1, height: 8, background: 'rgba(255,255,255,0.06)', borderRadius: 4, overflow: 'hidden' }}>
            <div style={{
              height: '100%',
              width: running && active ? `${o.pct * 100}%` : '0%',
              background: `linear-gradient(90deg, oklch(0.78 0.18 ${o.hue} / 0.4), oklch(0.78 0.18 ${o.hue}))`,
              borderRadius: 4,
              transition: 'width 1.2s cubic-bezier(0.32, 0.72, 0, 1)',
              boxShadow: running && active ? `0 0 8px oklch(0.78 0.18 ${o.hue} / 0.6)` : 'none',
            }}/>
          </div>
          <span style={{ fontSize: 10, color: accent, width: 36, fontFamily: 'var(--vb-font-mono)', fontWeight: 600, textAlign: 'right' }}>{Math.round(o.pct * 100)}%</span>
        </div>
      ))}
    </div>
  );
}

function BotHeroClips({ hue, running, active }) {
  const accent = `oklch(0.78 0.18 ${hue})`;
  return (
    <div style={{ position: 'absolute', inset: 0, padding: '50px 28px 24px', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 18 }}>
      <div style={{ width: 130, height: 75, borderRadius: 10, background: `linear-gradient(135deg, oklch(0.3 0.05 ${hue}), oklch(0.18 0.04 ${hue}))`, border: `1px solid ${accent}`, position: 'relative', overflow: 'hidden', flexShrink: 0, boxShadow: `0 0 20px -4px ${accent}` }}>
        <div style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
          <svg width="20" height="20" viewBox="0 0 20 20"><circle cx="10" cy="10" r="9" fill={accent} fillOpacity="0.2" stroke={accent} strokeWidth="1"/><path d="M8 6.5L13 10l-5 3.5z" fill={accent}/></svg>
        </div>
        {running && active && <div style={{ position: 'absolute', top: 0, left: 0, right: 0, height: 2, background: `linear-gradient(90deg, transparent, ${accent}, transparent)`, animation: 'bp-scanline 2.4s linear infinite' }}/>}
        <div style={{ position: 'absolute', bottom: 4, right: 6, fontSize: 9, color: 'var(--vb-fg-1)', fontFamily: 'var(--vb-font-mono)', background: 'rgba(0,0,0,0.6)', padding: '1px 4px', borderRadius: 3 }}>0:24</div>
      </div>
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', gap: 6 }}>
        {[1, 2, 3, 4].map((i) => (
          <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 8, fontSize: 11, fontFamily: 'var(--vb-font-mono)', opacity: running && active ? 1 - i * 0.18 : 0.3, transition: 'opacity 0.4s' }}>
            <svg width="10" height="10" viewBox="0 0 10 10"><path d="M5 1L9 8H1z" fill={accent} fillOpacity={1 - i * 0.15}/></svg>
            <span style={{ color: 'var(--vb-fg-2)' }}>+{(Math.floor(Math.random() * 50) + 50).toLocaleString()}</span>
            <span style={{ flex: 1, height: 1, background: `oklch(0.78 0.18 ${hue} / ${0.3 - i * 0.05})` }}/>
            <span style={{ color: 'var(--vb-fg-3)' }}>{i * 8}s ago</span>
          </div>
        ))}
      </div>
    </div>
  );
}

function BotHeroFollows({ hue, running, active }) {
  const accent = `oklch(0.78 0.18 ${hue})`;
  const hearts = React.useMemo(() => Array.from({ length: 12 }, (_, i) => ({
    x: 8 + (i * 7) + Math.random() * 4,
    delay: i * 0.4,
    size: 10 + Math.random() * 8,
    drift: (Math.random() - 0.5) * 12,
  })), []);
  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden' }}>
      {hearts.map((h, i) => (
        <svg key={i} viewBox="0 0 16 16" width={h.size} height={h.size} style={{
          position: 'absolute', left: `${h.x}%`, bottom: 20,
          color: accent,
          animation: running && active ? `bp-float 5s cubic-bezier(0.32, 0.72, 0, 1) infinite ${h.delay}s` : 'none',
          opacity: running && active ? 1 : 0.2,
          filter: `drop-shadow(0 0 4px ${accent})`,
          ['--drift']: `${h.drift}px`,
        }}>
          <path d="M8 14s-5-3.2-5-7a3 3 0 015-2.2A3 3 0 0113 7c0 3.8-5 7-5 7z" fill="currentColor"/>
        </svg>
      ))}
      <div style={{ position: 'absolute', bottom: 0, left: 0, right: 0, padding: '10px 16px', background: `linear-gradient(0deg, oklch(0.18 0.04 ${hue} / 0.9), transparent)` }}>
        <div style={{ display: 'flex', gap: 6, alignItems: 'center', fontSize: 11, fontFamily: 'var(--vb-font-mono)', color: 'var(--vb-fg-2)' }}>
          <span style={{ color: accent, fontWeight: 600 }}>+ jaytv_22</span>
          <span style={{ color: 'var(--vb-fg-3)' }}>·</span>
          <span style={{ color: accent, fontWeight: 600 }}>+ moonriver</span>
          <span style={{ color: 'var(--vb-fg-3)' }}>·</span>
          <span style={{ color: accent, fontWeight: 600 }}>+ zest_zelda</span>
          <span style={{ color: 'var(--vb-fg-3)', marginLeft: 'auto' }}>just followed</span>
        </div>
      </div>
    </div>
  );
}

function RefinedSlider({ min = 0, max = 100, step = 1, value, onChange, accent }) {
  const pct = ((value - min) / (max - min)) * 100;
  return (
    <div style={{ position: 'relative', height: 24, display: 'flex', alignItems: 'center', cursor: 'pointer' }}>
      <div style={{ position: 'absolute', left: 0, right: 0, height: 4, borderRadius: 2, background: 'rgba(255,255,255,0.06)', overflow: 'hidden' }}>
        <div style={{ width: `${pct}%`, height: '100%', background: accent, borderRadius: 2, boxShadow: `0 0 8px ${accent}` }}/>
      </div>
      <input type="range" min={min} max={max} step={step} value={value} onChange={(e) => onChange(+e.target.value)}
        className="bc-range" style={{ position: 'absolute', inset: 0, width: '100%', opacity: 0, cursor: 'pointer', margin: 0 }}/>
      <div style={{ position: 'absolute', left: `calc(${pct}% - 8px)`, width: 16, height: 16, borderRadius: 8, background: '#fff', boxShadow: '0 1px 3px rgba(0,0,0,0.3), 0 0 0 1px rgba(0,0,0,0.1)', pointerEvents: 'none', transition: 'transform 0.12s' }}/>
    </div>
  );
}

function Field({ label, hint, children }) {
  return (
    <div className="vb-col" style={{ gap: 9 }}>
      <div className="vb-row" style={{ gap: 6, alignItems: 'baseline' }}>
        <span style={{ fontSize: 10.5, color: 'var(--vb-fg-2)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600 }}>{label}</span>
        {hint && <span style={{ fontSize: 11, color: 'var(--vb-fg-3)', letterSpacing: '-0.005em' }}>· {hint}</span>}
      </div>
      {children}
    </div>
  );
}

function AutoTasksSection({ tasks, setTasks }) {
  const [draft, setDraft] = React.useState({ channel: '', platform: 'kick', viewers: 1000, delay: 30, ramp: 5 });

  const addTask = () => {
    if (!draft.channel) return;
    setTasks((t) => [...t, { id: Date.now(), ...draft, status: 'armed', nextLive: 'Waiting for live signal' }]);
    setDraft({ channel: '', platform: draft.platform, viewers: 1000, delay: 30, ramp: 5 });
  };

  const removeTask = (id) => setTasks((t) => t.filter((x) => x.id !== id));
  const toggleTask = (id) => setTasks((t) => t.map((x) => x.id === id ? { ...x, status: x.status === 'armed' ? 'paused' : 'armed' } : x));

  return (
    <Glass style={{ padding: 0, overflow: 'hidden', marginTop: 32, boxShadow: '0 1px 0 rgba(255,255,255,0.04) inset, 0 24px 48px -24px rgba(0,0,0,0.6)' }}>
      <div className="vb-row" style={{ padding: '24px 28px 22px', gap: 14, alignItems: 'center', flexWrap: 'wrap' }}>
        <span style={{ width: 42, height: 42, borderRadius: 12, background: 'linear-gradient(135deg, oklch(0.7 0.23 295), oklch(0.85 0.18 142))', display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#0a0a0a', boxShadow: '0 0 28px -8px oklch(0.7 0.23 295), inset 0 1px 0 rgba(255,255,255,0.3)' }}>
          <IconBolt size={20}/>
        </span>
        <div style={{ flex: 1 }}>
          <h2 className="vb-h2" style={{ fontSize: 20, letterSpacing: '-0.015em', marginBottom: 3 }}>Auto-Viewbot Tasks</h2>
          <p style={{ fontSize: 12.5, color: 'var(--vb-fg-2)', letterSpacing: '-0.005em' }}>
            We watch the channel. The moment they go live, viewers start joining at your configured delay. No babysitting.
          </p>
        </div>
        <div className="vb-row" style={{ gap: 14, fontFamily: 'var(--vb-font-mono)', fontSize: 11 }}>
          <span style={{ color: 'oklch(0.85 0.18 200)', display: 'inline-flex', alignItems: 'center', gap: 5 }}>
            <span style={{ width: 6, height: 6, borderRadius: 3, background: 'oklch(0.85 0.18 200)' }}/>
            <span style={{ fontWeight: 600 }}>{tasks.filter((t) => t.status === 'armed').length}</span>
            <span style={{ color: 'var(--vb-fg-3)' }}>armed</span>
          </span>
          <span style={{ color: 'oklch(0.85 0.18 142)', display: 'inline-flex', alignItems: 'center', gap: 5 }}>
            <span style={{ width: 6, height: 6, borderRadius: 3, background: 'oklch(0.85 0.18 142)', boxShadow: '0 0 6px oklch(0.85 0.18 142)', animation: 'vb-pulse 1.4s ease-in-out infinite' }}/>
            <span style={{ fontWeight: 600 }}>{tasks.filter((t) => t.status === 'firing').length}</span>
            <span style={{ color: 'var(--vb-fg-3)' }}>firing</span>
          </span>
        </div>
      </div>

      <div style={{ borderTop: '1px solid rgba(255,255,255,0.05)', borderBottom: '1px solid rgba(255,255,255,0.05)', padding: '20px 28px', background: 'linear-gradient(180deg, rgba(255,255,255,0.02), rgba(255,255,255,0.005))' }}>
        <div className="vb-row" style={{ gap: 14, alignItems: 'flex-end', flexWrap: 'wrap' }}>
          <Field label="Platform">
            <div style={{ display: 'inline-flex', padding: 3, background: 'rgba(0,0,0,0.4)', border: '1px solid rgba(255,255,255,0.05)', borderRadius: 8, boxShadow: 'inset 0 1px 2px rgba(0,0,0,0.2)' }}>
              {['kick', 'twitch'].map((p) => (
                <button key={p} onClick={() => setDraft({ ...draft, platform: p })}
                  style={{ padding: '6px 12px', borderRadius: 6, border: 'none', cursor: 'pointer', fontFamily: 'inherit', fontSize: 12, fontWeight: 600, letterSpacing: '-0.005em',
                    background: draft.platform === p ? 'rgba(255,255,255,0.08)' : 'transparent',
                    color: draft.platform === p ? (p === 'kick' ? 'oklch(0.85 0.18 142)' : 'oklch(0.78 0.2 295)') : 'var(--vb-fg-2)',
                    boxShadow: draft.platform === p ? '0 1px 0 rgba(255,255,255,0.06) inset' : 'none',
                    transition: 'all 0.18s' }}>
                  {p === 'kick' ? 'Kick' : 'Twitch'}
                </button>
              ))}
            </div>
          </Field>
          <Field label="Channel name">
            <div className="vb-row">
              <span style={{ padding: '9px 12px', background: 'rgba(0,0,0,0.45)', border: '1px solid rgba(255,255,255,0.06)', borderRight: 'none', borderRadius: '9px 0 0 9px', color: 'var(--vb-fg-3)', fontSize: 12.5, fontFamily: 'var(--vb-font-mono)' }}>
                {draft.platform === 'kick' ? 'kick.com/' : 'twitch.tv/'}
              </span>
              <input className="bc-input" style={{ borderRadius: '0 9px 9px 0', minWidth: 220 }} placeholder="streamer_handle"
                value={draft.channel} onChange={(e) => setDraft({ ...draft, channel: e.target.value })}/>
            </div>
          </Field>
          <Field label="Viewers">
            <input className="bc-input" style={{ width: 100, fontWeight: 600 }} type="number" value={draft.viewers} onChange={(e) => setDraft({ ...draft, viewers: +e.target.value })}/>
          </Field>
          <Field label="Delay" hint="sec after live signal">
            <input className="bc-input" style={{ width: 100, fontWeight: 600 }} type="number" value={draft.delay} onChange={(e) => setDraft({ ...draft, delay: +e.target.value })}/>
          </Field>
          <Field label="Ramp" hint="min">
            <input className="bc-input" style={{ width: 80, fontWeight: 600 }} type="number" value={draft.ramp} onChange={(e) => setDraft({ ...draft, ramp: +e.target.value })}/>
          </Field>
          <Btn variant="primary" icon={<IconPlus size={14}/>} onClick={addTask}>Schedule</Btn>
        </div>
      </div>

      <table style={{ width: '100%', borderCollapse: 'collapse' }}>
        <thead>
          <tr style={{ borderBottom: '1px solid rgba(255,255,255,0.05)', background: 'rgba(0,0,0,0.2)' }}>
            {['Status', 'Channel', 'Platform', 'Viewers', 'Delay', 'Ramp', 'Trigger', ''].map((h) => (
              <th key={h} style={{ padding: '13px 18px', textAlign: 'left', fontSize: 10, color: 'var(--vb-fg-3)', textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600 }}>{h}</th>
            ))}
          </tr>
        </thead>
        <tbody>
          {tasks.map((t, i) => (
            <tr key={t.id} className="bc-task-row" style={{ borderBottom: i < tasks.length - 1 ? '1px solid rgba(255,255,255,0.04)' : 'none', transition: 'background 0.15s' }}>
              <td style={{ padding: '15px 18px' }}><TaskStatusPill status={t.status}/></td>
              <td style={{ padding: '15px 18px' }}>
                <div className="vb-row" style={{ gap: 10 }}>
                  <Avatar name={t.channel} size={26}/>
                  <span className="vb-mono" style={{ fontSize: 12.5, fontWeight: 600, letterSpacing: '-0.01em' }}>{t.channel}</span>
                </div>
              </td>
              <td style={{ padding: '15px 18px' }}>
                <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, padding: '4px 9px', borderRadius: 100, fontSize: 11, fontWeight: 600, letterSpacing: '-0.005em',
                  background: t.platform === 'kick' ? 'oklch(0.85 0.18 142 / 0.14)' : 'oklch(0.7 0.23 295 / 0.14)',
                  color: t.platform === 'kick' ? 'oklch(0.85 0.18 142)' : 'oklch(0.78 0.2 295)' }}>
                  {t.platform === 'kick' ? <IconKick size={11}/> : <IconTwitch size={11}/>}
                  {t.platform === 'kick' ? 'Kick' : 'Twitch'}
                </span>
              </td>
              <td style={{ padding: '15px 18px', fontSize: 13.5, color: 'var(--vb-fg-1)', fontFamily: 'var(--vb-font-mono)', fontWeight: 600, fontVariantNumeric: 'tabular-nums', letterSpacing: '-0.01em' }}>{t.viewers.toLocaleString()}</td>
              <td style={{ padding: '15px 18px', fontSize: 12.5, color: 'var(--vb-fg-2)', fontFamily: 'var(--vb-font-mono)', fontVariantNumeric: 'tabular-nums' }}>{t.delay}s</td>
              <td style={{ padding: '15px 18px', fontSize: 12.5, color: 'var(--vb-fg-2)', fontFamily: 'var(--vb-font-mono)', fontVariantNumeric: 'tabular-nums' }}>{t.ramp} min</td>
              <td style={{ padding: '15px 18px', fontSize: 11.5, color: t.status === 'firing' ? 'oklch(0.85 0.18 142)' : 'var(--vb-fg-2)', fontWeight: t.status === 'firing' ? 600 : 500, letterSpacing: '-0.005em' }}>{t.nextLive}</td>
              <td style={{ padding: '15px 18px', textAlign: 'right' }}>
                <div className="vb-row" style={{ gap: 6, justifyContent: 'flex-end' }}>
                  <button onClick={() => toggleTask(t.id)} className="bc-mini-btn">
                    {t.status === 'armed' || t.status === 'firing' ? 'Pause' : 'Arm'}
                  </button>
                  <button onClick={() => removeTask(t.id)} className="bc-icon-btn"><IconClose size={12}/></button>
                </div>
              </td>
            </tr>
          ))}
        </tbody>
      </table>

      <div className="vb-row" style={{ padding: '15px 28px', borderTop: '1px solid rgba(255,255,255,0.05)', background: 'linear-gradient(180deg, rgba(0,0,0,0.15), rgba(0,0,0,0.3))', gap: 14 }}>
        <span style={{ width: 7, height: 7, borderRadius: 4, background: 'oklch(0.85 0.18 142)', boxShadow: '0 0 8px oklch(0.85 0.18 142)', animation: 'vb-pulse 1.6s ease-in-out infinite' }}/>
        <span style={{ fontSize: 12, color: 'var(--vb-fg-2)', letterSpacing: '-0.005em' }}>Live-detection poll every <span className="vb-mono" style={{ color: 'var(--vb-fg-1)', fontWeight: 600 }}>15s</span> · last fired <span className="vb-mono" style={{ color: 'var(--vb-fg-1)', fontWeight: 600 }}>shadowfern</span> 4 min ago, ramp in progress</span>
        <span className="vb-spacer"/>
        <span style={{ fontSize: 11, color: 'var(--vb-fg-3)', letterSpacing: '-0.005em' }}>Tasks roll over month-to-month</span>
      </div>
    </Glass>
  );
}

function TaskStatusPill({ status }) {
  const base = { fontSize: 9.5, padding: '4px 9px', borderRadius: 100, fontWeight: 700, letterSpacing: '0.06em', display: 'inline-flex', alignItems: 'center', gap: 5 };
  if (status === 'armed') return <span style={{ ...base, background: 'oklch(0.78 0.18 200 / 0.16)', color: 'oklch(0.85 0.18 200)' }}><span style={{ width: 5, height: 5, borderRadius: 3, background: 'oklch(0.85 0.18 200)' }}/>ARMED</span>;
  if (status === 'firing') return <span style={{ ...base, background: 'oklch(0.85 0.18 142 / 0.18)', color: 'oklch(0.85 0.18 142)' }}><span style={{ width: 5, height: 5, borderRadius: 3, background: 'oklch(0.85 0.18 142)', boxShadow: '0 0 6px oklch(0.85 0.18 142)', animation: 'vb-pulse 1.2s ease-in-out infinite' }}/>FIRING</span>;
  return <span style={{ ...base, background: 'rgba(255,255,255,0.05)', color: 'var(--vb-fg-3)' }}>PAUSED</span>;
}

Object.assign(window, { Dashboard });
