```// ==UserScript==
// @name         codecurber 2.0 - fold large markdown <pre> keeping native formatting
// @version      2.0
// @description  Fold large code blocks (pre > code) by limiting <pre> height and allow toggle full view, preserving formatting and syntax highlighting
// @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_LINES = 6;
  const FOLD_MAX_HEIGHT = '8em'; // approx 6-8 lines, adjust as needed

  // Inject fold CSS to override chat styles, with !important
  const style = document.createElement('style');
  style.textContent = `
    pre.codecurber-folded {
      max-height: ${FOLD_MAX_HEIGHT} !important;
      overflow-y: auto !important;
      position: relative !important;
      transition: max-height 0.3s ease !important;
    }
    .codecurber-toggle-btn {
      margin-top: 0.5em !important;
      cursor: pointer !important;
      color: #39ff14 !important;
      background: transparent !important;
      border: 1.5px solid #39ff14 !important;
      padding: 6px 16px !important;
      font-family: monospace !important;
      font-size: 0.8125rem !important; /* 13px */
      border-radius: 8px !important;
      user-select: none !important;
      display: inline-block !important;
      transition: background-color 0.25s ease, color 0.25s ease !important;
    }
    .codecurber-toggle-btn:hover {
      background-color: #39ff14 !important;
      color: black !important;
    }
  `;
  document.head.appendChild(style);

  // Checks if a <pre> > <code> block should be foldable
  function isFoldable(preEl) {
    if (!preEl) return false;
    const codeEl = preEl.querySelector('code');
    if (!codeEl) return false;
    const lineCount = (codeEl.textContent || '').split('\n').length;
    return lineCount >= MIN_LINES;
  }

  // Create fold/unfold toggle button
  function createToggleButton() {
    const btn = document.createElement('button');
    btn.type = 'button';
    btn.className = 'codecurber-toggle-btn';
    btn.textContent = 'Show full code';
    btn.setAttribute('aria-expanded', 'false');
    btn.setAttribute('aria-label', 'Show full code block');
    return btn;
  }

  // Apply folding behavior to a <pre> element
  function foldCodeBlock(preEl) {
    if (preEl.dataset.codecurberFolded === 'true') return;
    preEl.dataset.codecurberFolded = 'true';

    preEl.classList.add('codecurber-folded');

    // Wrap pre in a container for button
    const container = document.createElement('div');
    container.style.display = 'flex';
    container.style.flexDirection = 'column';
    container.style.width = '100%';

    preEl.parentNode.insertBefore(container, preEl);
    container.appendChild(preEl);

    // Append toggle button below pre
    const btn = createToggleButton();
    container.appendChild(btn);

    btn.addEventListener('click', () => {
      const isFolded = preEl.classList.contains('codecurber-folded');
      if (isFolded) {
        preEl.classList.remove('codecurber-folded');
        preEl.style.maxHeight = 'none';
        preEl.style.overflow = 'visible';
        btn.textContent = 'Fold code';
        btn.setAttribute('aria-expanded', 'true');
      } else {
        preEl.classList.add('codecurber-folded');
        preEl.style.maxHeight = FOLD_MAX_HEIGHT;
        preEl.style.overflow = 'auto';
        btn.textContent = 'Show full code';
        btn.setAttribute('aria-expanded', 'false');
      }
    });
  }

  // Fold all large code blocks currently in chat
  function foldAllLargeCodeBlocks() {
    const chat = document.querySelector('.bubble-wrap.svelte-gjtrl6');
    if (!chat) return;
    // Prefer folding <pre> blocks that contain <code>
    chat.querySelectorAll('pre').forEach(preEl => {
      if (isFoldable(preEl)) {
        foldCodeBlock(preEl);
      }
    });
  }

  // Observe chat for new code blocks and fold them
  function observeChat() {
    const chat = document.querySelector('.bubble-wrap.svelte-gjtrl6');
    if (!chat) return;
    let foldingScheduled = false;

    const observer = new MutationObserver(() => {
      if (!foldingScheduled) {
        foldingScheduled = true;
        setTimeout(() => {
          foldAllLargeCodeBlocks();
          foldingScheduled = false;
        }, 100); // debounce folding to avoid thrashing
      }
    });
    observer.observe(chat, { childList: true, subtree: true });
  }

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

})();```