```// ==UserScript==
// @name         codecurber fullscreen-overlay replacement with toggle fit
// @version      1.27
// @description  Replace all code blocks >5 lines with fullscreen-overlay styled textarea + close & fit-toggle buttons
// @match        *://yuntian-deng-chatgpt.hf.space/*
// @match        *://yuntian-deng-o1mini.hf.space/*
// @match        *://yuntian-deng-chatgpt4.hf.space/*
// @match        *://yuntian-deng-o1.hf.space/*
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  const MIN_FOLD_LINES = 6;

  const style = document.createElement('style');
  style.textContent = `
    .fullscreen-overlay {
      position: relative;
      background: linear-gradient(135deg, #000000cc, #003300cc);
      box-shadow:
        0 0 30px #39ff14aa inset,
        0 0 50px #0f0f0f;
      border: 5px solid #39ff14;
      border-radius: 8px;
      padding: 1em;
      box-sizing: border-box;
      color: #39ff14;
      font-family: 'Press Start 2P', monospace;
      user-select: text;
      overflow: hidden;
      max-width: 100%;
      max-height: 80vh;
      display: flex;
      flex-direction: column;
      transition: width 0.25s ease, height 0.25s ease;
    }

    .fullscreen-overlay textarea {
      width: 100% !important;
      height: 100% !important;
      background: #001100 !important;
      border: none !important;
      box-shadow:
        0 0 20px #39ff1499 inset !important;
      color: #39ff14 !important;
      caret-color: #6eff44 !important;
      resize: none !important;
      outline: none !important;
      overflow: auto !important;
      white-space: pre !important;
      font-family: 'Press Start 2P', monospace !important;
      box-sizing: border-box !important;
      user-select: text !important;
    }

    .fullscreen-overlay .btn-container {
      display: flex;
      justify-content: flex-end;
      gap: 10px;
      margin-bottom: 0.5em;
    }

    .btn {
      user-select: none;
      box-shadow:
        0 0 10px #39ff14cc,
        inset 0 0 6px #6eff44cc;
      border-radius: 50%;
      border: 3px solid #39ff14;
      background: #002200;
      color: #39ff14;
      transition: color 0.3s ease, background 0.3s ease;
      width: 36px;
      height: 36px;
      font-size: 26px;
      line-height: 36px;
      text-align: center;
      cursor: pointer;
      flex-shrink: 0;
    }
    .btn:hover {
      background: #39ff14;
      color: #f00;
      box-shadow:
        0 0 20px #39ff44ff,
        inset 0 0 12px #39ff66ff;
    }
  `;
  document.head.appendChild(style);

  function createFullscreenOverlay(text) {
    const container = document.createElement('div');
    container.className = 'fullscreen-overlay';
    container.style.width = '100%';
    container.style.height = '80vh';

    // Button row container
    const btnContainer = document.createElement('div');
    btnContainer.className = 'btn-container';

    // Close button
    const closeBtn = document.createElement('button');
    closeBtn.className = 'btn';
    closeBtn.title = 'Close full code view';
    closeBtn.textContent = '×';

    // Fit toggle button
    const fitBtn = document.createElement('button');
    fitBtn.className = 'btn';
    fitBtn.title = 'Toggle fit fullscreen';
    fitBtn.textContent = '⬜'; // Unicode for square

    btnContainer.appendChild(fitBtn);
    btnContainer.appendChild(closeBtn);
    container.appendChild(btnContainer);

    // Textarea with full code
    const ta = document.createElement('textarea');
    ta.readOnly = true;
    ta.value = text;
    container.appendChild(ta);

    let isFit = false;

    function scaleToFit() {
      const viewportW = window.innerWidth * 0.97;
      const viewportH = window.innerHeight * 0.97;

      // Reset textarea to auto size for measurement
      ta.style.width = 'auto';
      ta.style.height = 'auto';

      // Measure scroll size (natural content size)
      const contentW = ta.scrollWidth;
      const contentH = ta.scrollHeight;

      const paddingBorder = 30;
      const maxW = viewportW - paddingBorder;
      const maxH = viewportH - paddingBorder;

      const scaleX = maxW / contentW;
      const scaleY = maxH / contentH;
      const scale = Math.min(1, scaleX, scaleY);

      // Apply scaled container size and CSS transform
      container.style.width = contentW + 'px';
      container.style.height = contentH + 'px';
      container.style.transformOrigin = 'top left';
      container.style.transform = `translate(-50%, -50%) scale(${scale})`;
      container.style.position = 'fixed';
      container.style.top = '50%';
      container.style.left = '50%';
      container.style.maxWidth = '';
      container.style.maxHeight = '';
      container.style.overflow = 'visible';

      ta.style.width = contentW + 'px';
      ta.style.height = contentH + 'px';
      ta.style.overflow = 'auto';
    }

    function resetSize() {
      container.style.width = '100%';
      container.style.height = '80vh';
      container.style.position = 'relative';
      container.style.transform = '';
      container.style.top = '';
      container.style.left = '';
      container.style.maxWidth = '100%';
      container.style.maxHeight = '80vh';
      container.style.overflow = 'hidden';

      ta.style.width = '100%';
      ta.style.height = '100%';
      ta.style.overflow = 'auto';
    }

    fitBtn.addEventListener('click', () => {
      if (!isFit) {
        scaleToFit();
      } else {
        resetSize();
      }
      isFit = !isFit;
    });

    closeBtn.addEventListener('click', () => {
      cleanup();
    });

    function cleanup() {
      window.removeEventListener('resize', onResize);
      document.removeEventListener('keydown', onKey);
      container.remove();
    }

    function onKey(e) {
      if (e.key === 'Escape') cleanup();
    }

    function onResize() {
      if (isFit) scaleToFit();
    }

    window.addEventListener('resize', onResize);
    document.addEventListener('keydown', onKey);

    return { container, cleanup };
  }

  function replaceCodeBlock(codeEl) {
    if (codeEl.dataset.replaced === 'true') return;

    const text = codeEl.textContent || '';
    const lines = text.split('\n').length;

    if (lines < MIN_FOLD_LINES) return; // Only replace large blocks

    codeEl.dataset.replaced = 'true';

    const parent = codeEl.parentNode;
    if (!parent) return;

    // Create fullscreen-overlay container with textarea and buttons
    const { container, cleanup } = createFullscreenOverlay(text);

    // Insert container before old code and hide old code
    parent.insertBefore(container, codeEl);
    codeEl.style.display = 'none';

    // Nothing else—overlay container now shows the code in folded/fullscreen style but inline in chat flow
  }

  function replaceAllCodeBlocks() {
    const chat = document.querySelector('.bubble-wrap.svelte-gjtrl6');
    if (!chat) return;
    chat.querySelectorAll('code').forEach(replaceCodeBlock);
  }

  function observeChat() {
    const chat = document.querySelector('.bubble-wrap.svelte-gjtrl6');
    if (!chat) return;
    const observer = new MutationObserver(() => {
      replaceAllCodeBlocks();
    });
    observer.observe(chat, { childList: true, subtree: true });
  }

  const initInt = setInterval(() => {
    if (document.querySelector('.bubble-wrap.svelte-gjtrl6')) {
      clearInterval(initInt);
      replaceAllCodeBlocks();
      observeChat();
    }
  }, 300);

})();
```