```// ==UserScript==
// @name         codecurber mini-preview folding with auto-fit fullscreen expand
// @version      1.26
// @description  Fold all large code blocks (>5 lines) into mini-preview frames with expand button; fullscreen preview auto-scales to viewport
// @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;    // Fold code blocks with at least this many lines
  const MAX_PREVIEW_LINES = 5; // Mini-preview visible lines in folded state
  const LINE_HEIGHT_PX = 19.2;

  // Inject styles
  const style = document.createElement('style');
  style.textContent = `
    .mini-preview-container {
      position: relative;
      width: 100%;
      max-width: 600px;
      margin: 1em 0 1.5em;
      font-family: 'Press Start 2P', monospace;
      color: #39ff14;
      user-select: text;
    }

    .mini-preview-textarea {
      width: 100% !important;
      height: calc(${MAX_PREVIEW_LINES} * ${LINE_HEIGHT_PX}px);
      background: #0a1b0a !important;
      border: 1px solid #39ff14 !important;
      border-radius: 6px;
      padding: 10px !important;
      resize: none !important;
      overflow: hidden !important;
      white-space: pre !important;
      box-sizing: border-box !important;
      font-size: 0.85rem !important;
      cursor: text;
      outline: none;
      box-shadow:
        inset 0 0 10px #39ff14bb;
      caret-color: #6eff44 !important;
    }

    .mini-preview-btn {
      margin-top: 6px;
      padding: 6px 18px;
      background: #f00;
      border: 3px solid #39ff14;
      color: #39ff14;
      border-radius: 4px;
      font-family: 'Press Start 2P', monospace;
      cursor: pointer;
      text-shadow:
        1px 1px 0 #000,
        -1px -1px 0 #000,
        1px -1px 0 #000,
        -1px 1px 0 #000;
      box-shadow:
        inset 0 0 2px #39ff14,
        0 0 12px #39ff1444;
      transition: background-color 0.15s ease, color 0.15s ease;
      user-select: none;
      align-self: flex-start;
    }
    .mini-preview-btn:hover {
      background: #39ff14;
      color: #0f0;
      box-shadow:
        inset 0 0 6px #fff,
        0 0 16px #39ff14;
    }

    .fullscreen-overlay {
      position: fixed !important;
      top: 50% !important;
      left: 50% !important;
      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;
      z-index: 1000000;
      display: flex;
      flex-direction: column;
      max-width: 97vw;
      max-height: 97vh;
      transform-origin: top left;
      transform: translate(-50%, -50%) scale(1);
    }

    .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-close-btn {
      position: absolute !important;
      top: 10px !important;
      right: 10px !important;
      width: 40px !important;
      height: 40px !important;
      border-radius: 50% !important;
      border: 3px solid #39ff14 !important;
      background: #002200 !important;
      color: #39ff14 !important;
      font-size: 28px !important;
      line-height: 40px !important;
      text-align: center;
      cursor: pointer;
      z-index: 1000001;
      box-shadow:
        0 0 10px #39ff14cc,
        inset 0 0 6px #6eff44cc;
      user-select: none;
      transition: color 0.3s ease, background 0.3s ease;
    }
    .fullscreen-close-btn:hover {
      background: #39ff14 !important;
      color: #f00 !important;
      box-shadow:
        0 0 20px #39ff44ff,
        inset 0 0 12px #39ff66ff;
    }
  `;
  document.head.appendChild(style);

  // Replace large code blocks with mini-preview frames + expand button
  function foldCodeBlock(codeEl) {
    if (codeEl.dataset.folded === 'true') return;

    const text = codeEl.textContent || '';
    const lines = text.split('\n').length;
    if (lines < MIN_FOLD_LINES) return; // skip small blocks: leave normal

    codeEl.dataset.folded = 'true';

    // Container for mini preview + button
    const container = document.createElement('div');
    container.className = 'mini-preview-container';

    // Mini textarea preview (cropped with ellipsis if necessary)
    const previewLines = text.split('\n').slice(0, MAX_PREVIEW_LINES).join('\n');
    const displayText = previewLines.length < text.length ? previewLines + '\n…' : previewLines;

    const miniTA = document.createElement('textarea');
    miniTA.readOnly = true;
    miniTA.className = 'mini-preview-textarea';
    miniTA.value = displayText;

    container.appendChild(miniTA);

    // Expand button
    const btn = document.createElement('button');
    btn.type = 'button';
    btn.className = 'mini-preview-btn';
    btn.textContent = 'Expand full code';
    container.appendChild(btn);

    // Hide original code block, insert mini-preview container before it
    const parent = codeEl.parentNode;
    if (!parent) return;
    parent.insertBefore(container, codeEl);
    codeEl.style.display = 'none';

    btn.addEventListener('click', () => openFullscreenOverlay(text));
  }

  // Opens fullscreen overlay scaled to viewport for full code viewing
  function openFullscreenOverlay(fullText) {
    const overlay = document.createElement('div');
    overlay.className = 'fullscreen-overlay';

    const fullTA = document.createElement('textarea');
    fullTA.readOnly = true;
    fullTA.value = fullText;

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

    overlay.appendChild(fullTA);
    overlay.appendChild(closeBtn);
    document.body.appendChild(overlay);

    fullTA.focus();
    fullTA.scrollTop = 0;

    function scaleOverlay() {
      const viewportW = window.innerWidth * 0.97;
      const viewportH = window.innerHeight * 0.97;
      const paddingBorder = 50;

      fullTA.style.width = 'auto';
      fullTA.style.height = 'auto';

      const contentW = fullTA.scrollWidth;
      const contentH = fullTA.scrollHeight;

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

      overlay.style.width = contentW + 'px';
      overlay.style.height = contentH + 'px';
      overlay.style.transform = `translate(-50%, -50%) scale(${scale})`;

      fullTA.style.width = contentW + 'px';
      fullTA.style.height = contentH + 'px';
    }

    scaleOverlay();

    const onResize = () => scaleOverlay();
    window.addEventListener('resize', onResize);

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

    closeBtn.addEventListener('click', cleanup);

    function onKey(e) {
      if (e.key === 'Escape') cleanup();
    }
    document.addEventListener('keydown', onKey);
  }

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

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

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