// ==UserScript==
// @name         TypingGhost v6 - Badge Farmer & Stealth
// @namespace    https://chat.openai.com/
// @version      6.0
// @description  Fully automated TypingClub mod with stealth, badge farming & KaHack-inspired UI
// @author       GhostDev
// @match        https://www.typingclub.com/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const html = `
    <div id="ghost-header">TypingGhost v6</div>
    <div class="ghost-section">
      <label>Target WPM: <input id="ghost-wpm" type="number" value="60" /></label>
      <label>Max Lessons: <input id="ghost-maxlessons" type="number" value="10" /></label>
    </div>
    <div class="ghost-section">
      <label class="ghost-switch"><input type="checkbox" id="ghost-retry"> Retry on Fail</label>
      <label class="ghost-switch"><input type="checkbox" id="ghost-mistakes"> Simulate Mistakes</label>
      <label class="ghost-switch"><input type="checkbox" id="ghost-stealth"> Stealth Mode</label>
    </div>
    <div class="ghost-section">
      <button id="ghost-start">▶ Start Farming</button>
      <button id="ghost-stop">⏹ Stop</button>
    </div>
    <div id="ghost-status">Idle</div>
  `;

  const css = `
    #ghost-container {
      position: fixed;
      top: 80px;
      left: 80px;
      width: 280px;
      font-family: Arial, sans-serif;
      background: #1c1c1c;
      color: #fff;
      border-radius: 10px;
      box-shadow: 0 0 20px #00000088;
      z-index: 999999;
      padding: 10px;
      user-select: none;
    }
    #ghost-header {
      font-size: 18px;
      background: #4b0082;
      padding: 10px;
      border-radius: 8px 8px 0 0;
      cursor: move;
      text-align: center;
      font-weight: bold;
    }
    .ghost-section {
      margin: 10px 0;
    }
    #ghost-container input[type=number] {
      width: 100%;
      padding: 4px;
      border: none;
      border-radius: 5px;
      font-size: 14px;
    }
    .ghost-switch {
      display: block;
      margin: 6px 0;
      font-size: 14px;
    }
    #ghost-container button {
      width: 48%;
      margin: 2px 1%;
      padding: 6px;
      background: #28a745;
      color: white;
      border: none;
      border-radius: 5px;
      font-size: 14px;
      cursor: pointer;
    }
    #ghost-container button:hover {
      background: #218838;
    }
    #ghost-status {
      margin-top: 8px;
      text-align: center;
      font-size: 13px;
      color: #ccc;
    }
  `;

  const style = document.createElement('style');
  style.textContent = css;
  document.head.appendChild(style);

  const box = document.createElement('div');
  box.id = 'ghost-container';
  box.innerHTML = html;
  document.body.appendChild(box);

  // Draggable UI
  let offsetX, offsetY, dragging = false;
  const header = document.getElementById('ghost-header');
  header.addEventListener('mousedown', (e) => {
    dragging = true;
    offsetX = e.clientX - box.getBoundingClientRect().left;
    offsetY = e.clientY - box.getBoundingClientRect().top;
  });
  document.addEventListener('mousemove', (e) => {
    if (dragging) {
      box.style.left = `${e.clientX - offsetX}px`;
      box.style.top = `${e.clientY - offsetY}px`;
    }
  });
  document.addEventListener('mouseup', () => dragging = false);

  // Core logic
  let isRunning = false;
  let hidden = false;
  let lessonCount = 0;

  function setStatus(msg) {
    document.getElementById('ghost-status').textContent = msg;
  }

  function getCharacters() {
    const els = Array.from(document.querySelectorAll('.token span.token_unit'));
    return els.map(el => {
      if (el.firstChild?.classList.contains('_enter')) return '\n';
      let char = el.textContent?.[0] || '';
      return keyOverrides[char] || char;
    });
  }

  function recordKey(c) {
    window.core?.record_keydown_time(c);
  }

  function sleep(ms) {
    return new Promise(res => setTimeout(res, ms));
  }

  async function autoType(chars, wpm, simulateMistakes) {
    const delay = 12000 / (wpm * 5);
    for (let c of chars) {
      if (!isRunning) return;
      if (simulateMistakes && Math.random() < 0.02 && c !== ' ' && c !== '\n') {
        recordKey('x'); recordKey('\b');
        await sleep(100);
      }
      recordKey(c);
      await sleep(delay * (0.9 + Math.random() * 0.2));
    }
  }

  async function waitForEnd() {
    while (isRunning && !document.querySelector('.continue-button')) {
      await sleep(1000);
    }
  }

  async function clickContinue() {
    const btn = document.querySelector('.continue-button');
    if (btn) btn.click();
    await sleep(2000);
  }

  async function runLessons() {
    const maxLessons = +document.getElementById('ghost-maxlessons').value;
    const wpm = +document.getElementById('ghost-wpm').value;
    const retry = document.getElementById('ghost-retry').checked;
    const simulate = document.getElementById('ghost-mistakes').checked;

    lessonCount = 0;
    while (isRunning && lessonCount < maxLessons) {
      lessonCount++;
      setStatus(`Typing lesson ${lessonCount}...`);
      const chars = getCharacters();
      await autoType(chars, wpm, simulate);
      await waitForEnd();
      await clickContinue();
    }

    setStatus("✅ Finished.");
    isRunning = false;
  }

  document.getElementById('ghost-start').onclick = () => {
    if (!isRunning) {
      isRunning = true;
      runLessons();
    }
  };

  document.getElementById('ghost-stop').onclick = () => {
    isRunning = false;
    setStatus("⏹ Stopped.");
  };

  // Stealth toggle: Ctrl + Backspace
  document.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.key === 'Backspace') {
      hidden = !hidden;
      box.style.display = hidden ? 'none' : 'block';
      isRunning = !hidden;
      setStatus(hidden ? "⏳ Paused" : "🟢 Resumed");
    }
  });
})();
 im trying to build a auto typing and lesson can you fix this and make it better