It have many many bugs in my Tetris game, don't try to analysis the problem, just help me to *rewrite* the whole js.

Below is a Tetris game implemented in JavaScript with a simple but functional GUI using HTML5 and CSS.

### HTML (index.html)
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Enhanced Tetris</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <canvas id="gameBoard" width="300" height="600"></canvas>
        <div class="info">
            <h1 class="game-title">Tetris</h1>
            <div class="stats">
                <p>Use Arrow Keys to move and rotate the pieces.</p>
                <p>Score: <span id="score">0</span></p>
                <p>Lines Cleared: <span id="lines">0</span></p>
                <p>Level: <span id="level">1</span></p>
                <p>High Score: <span id="highScore">0</span></p>
            </div>
            <div class="previews">
                <div class="preview">
                    <h3>Next</h3>
                    <canvas id="nextPiece" width="150" height="150"></canvas>
                </div>
                <div class="preview">
                    <h3>Hold</h3>
                    <canvas id="holdPiece" width="150" height="150"></canvas>
                </div>
            </div>
            <div class="controls">
                <button id="holdButton" style="display: none;">Hold</button>
                <button id="pauseButton" style="display: none;">Pause</button>
                <button id="playButton">Play</button>
            </div>
        </div>
    </div>

    <!-- Game Over Modal -->
    <div id="gameOverModal" class="modal">
        <div class="modal-content">
            <h2>Game Over</h2>
            <p>Your Score: <span id="finalScore">0</span></p>
            <p>Your Level: <span id="finalLevel">1</span></p>
            <p>Lines Cleared: <span id="finalLines">0</span></p>
            <div class="modal-buttons">
                <button id="restartButton">Restart</button>
                <button id="exitButton">Exit</button>
            </div>
        </div>
    </div>

    <!-- Audio Elements -->
    <audio id="moveSound" src="sounds/move.mp3" preload="auto"></audio>
    <audio id="rotateSound" src="sounds/rotate.mp3" preload="auto"></audio>
    <audio id="clearSound" src="sounds/clear.mp3" preload="auto"></audio>
    <audio id="gameOverSound" src="sounds/gameover.mp3" preload="auto"></audio>
    <audio id="modalAppearSound" src="sounds/modalAppear.mp3" preload="auto"></audio> <!-- Optional -->
    <script src="tetris.js"></script>
</body>
</html>
```

### CSS (styles.css)
```css
/* Google Fonts Import */
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');

/* Global Styles */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #20232a; /* Dark background for contrast */
    color: #61dafb; /* Light blue text for visibility */
    overflow: hidden;
}

/* Container for Game and Info */
.container {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: 30px;
    padding: 20px;
    background: rgba(255, 255, 255, 0.05); /* Slightly white-transparent background */
    border-radius: 15px;
    box-shadow: 0 4px 30px rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(5px);
}

/* Game Canvas Styling */
canvas#gameBoard {
    background-color: #1e1e1e;
    border: 3px solid #61dafb;
    border-radius: 10px;
    box-shadow: inset 0 0 10px rgba(255, 255, 255, 0.1);
}

/* Information Panel */
.info {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 25px;
    width: 200px;
}

/* Game Title */
.game-title {
    font-family: 'Press Start 2P', cursive;
    font-size: 18px;
    color: #61dafb;
    text-shadow: 2px 2px 4px #000;
    animation: titlePulse 2s infinite;
}

/* Title Animation */
@keyframes titlePulse {
    0%, 100% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.05);
        opacity: 0.8;
    }
}

/* Statistics Section */
.stats {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
}

.stats p {
    font-size: 14px;
    background: rgba(97, 218, 251, 0.1); /* Light blue-transparent background */
    padding: 8px 12px;
    border-radius: 8px;
    text-align: center;
    transition: background 0.3s ease;
}

.stats p:hover {
    background: rgba(97, 218, 251, 0.2);
}

/* Previews Section */
.previews {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
}

.preview {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.preview h3 {
    font-size: 12px;
    color: #61dafb;
    margin-bottom: 5px;
    text-shadow: 1px 1px 2px #000;
}

/* Preview Canvases */
.preview canvas {
    background-color: #1e1e1e;
    border: 2px solid #61dafb;
    border-radius: 8px;
    box-shadow: inset 0 0 5px rgba(255, 255, 255, 0.1);
}

/* Controls Section */
.controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
}

.controls button {
    background: #61dafb;
    color: #1e1e1e;
    border: none;
    padding: 10px;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    transition: transform 0.1s ease, box-shadow 0.3s ease;
}

.controls button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.4);
}

.controls button:active {
    transform: translateY(0px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.controls button:disabled {
    background: #555;
    color: #aaa;
    cursor: not-allowed;
    box-shadow: none;
}

/* Score Displays */
#score, #level, #lines, #highScore,
#finalScore, #finalLevel, #finalLines {
    color: #ffffff;
    font-weight: bold;
}

/* Game Over Modal */
.modal {
    display: none; /* Hidden by default */
    position: fixed;
    z-index: 20;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    justify-content: center;
    align-items: center;
    animation: fadeIn 0.5s;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: #282c34;
    padding: 30px 20px;
    border-radius: 12px;
    text-align: center;
    width: 80%;
    max-width: 400px;
    box-shadow: 0 8px 16px rgba(0,0,0,0.6);
    animation: slideDown 0.5s;
}

@keyframes slideDown {
    from { transform: translateY(-50px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.modal-content h2 {
    font-family: 'Press Start 2P', cursive;
    font-size: 16px;
    color: #61dafb;
    text-shadow: 1px 1px 2px #000;
    margin-bottom: 15px;
}

.modal-content p {
    font-size: 14px;
    color: #ffffff;
    margin: 10px 0;
}

.modal-buttons {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin-top: 20px;
}

.modal-buttons button {
    background: #61dafb;
    color: #1e1e1e;
    border: none;
    padding: 10px 20px;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    transition: transform 0.1s ease, box-shadow 0.3s ease;
}

.modal-buttons button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.4);
}

.modal-buttons button:active {
    transform: translateY(0px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* Responsive Design */
@media (max-width: 800px) {
    .container {
        flex-direction: column;
        align-items: center;
    }

    canvas#gameBoard {
        width: 250px;
        height: 500px;
    }

    .info {
        width: 100%;
        max-width: 300px;
    }

    .previews {
        flex-direction: row;
        justify-content: center;
    }

    .preview {
        flex: 1;
    }
}

@media (max-width: 500px) {
    .game-title {
        font-size: 14px;
    }

    .stats p {
        font-size: 12px;
    }

    .preview h3 {
        font-size: 10px;
    }

    .controls button {
        font-size: 12px;
        padding: 8px;
    }

    .modal-content {
        width: 90%;
    }

    .modal-content h2 {
        font-size: 14px;
    }

    .modal-content p {
        font-size: 12px;
    }

    .modal-buttons button {
        font-size: 12px;
        padding: 8px 16px;
    }
}
```

### JavaScript (tetris.js)
```javascript
const canvas = document.getElementById('gameBoard');
const ctx = canvas.getContext('2d');
const playButton = document.getElementById('playButton');
const scoreDisplay = document.getElementById('score');
const levelDisplay = document.getElementById('level');
const linesDisplay = document.getElementById('lines');
const highScoreDisplay = document.getElementById('highScore');
const nextPieceCanvas = document.getElementById('nextPiece');
const nextCtx = nextPieceCanvas.getContext('2d');
const holdPieceCanvas = document.getElementById('holdPiece');
const holdCtx = holdPieceCanvas.getContext('2d');
const holdButton = document.getElementById('holdButton'); // Ensure this line is present

// Modal Elements
const gameOverModal = document.getElementById('gameOverModal');
const finalScoreDisplay = document.getElementById('finalScore');
const finalLevelDisplay = document.getElementById('finalLevel');
const finalLinesDisplay = document.getElementById('finalLines');
const restartButton = document.getElementById('restartButton');
const exitButton = document.getElementById('exitButton');

// Audio Elements with Error Handling
const moveSound = document.getElementById('moveSound');
const rotateSound = document.getElementById('rotateSound');
const clearSound = document.getElementById('clearSound');
const gameOverSound = document.getElementById('gameOverSound');
const modalAppearSound = document.getElementById('modalAppearSound'); // Optional

const ROWS = 20;
const COLS = 10;
const BLOCK_SIZE = 30; // Main game block size
const PREVIEW_BLOCK_SIZE = 20; // Preview canvases block size
const EMPTY_COLOR = '#111';
const COLORS = ['#FF5733', '#33FF57', '#3357FF', '#FF33A8', '#FF8F33', '#FFD433', '#33FFF6'];

let board = [];
let currentPiece;
let nextPiece = null;
let holdPiece = null;
let score = 0;
let level = 1;
let lines = 0;
let gameInterval;
let gameSpeed = 500; // initial speed in ms
const SPEED_INCREMENT = 50; // speed increase every level
const LINES_PER_LEVEL = 10;
let canHold = true;
let keyCooldown = false;
const KEY_DELAY = 100; // milliseconds

// Tetromino shapes
const SHAPES = [
    [[1, 1, 1, 1]], // I
    [[1, 1], [1, 1]], // O
    [[0, 1, 1], [1, 1, 0]], // S
    [[1, 1, 0], [0, 1, 1]], // Z
    [[1, 1, 1], [0, 1, 0]], // T
    [[1, 1, 1], [1, 0, 0]], // L
    [[1, 1, 1], [0, 0, 1]]  // J
];

// Create and initialize the board
function createBoard() {
    board = Array.from({ length: ROWS }, () => Array(COLS).fill(EMPTY_COLOR));
}

// Draw a single block at (x, y) with the specified color on the main game board
function drawBlock(x, y, color) {
    const blockX = x * BLOCK_SIZE;
    const blockY = y * BLOCK_SIZE;

    // Create gradient
    const gradient = ctx.createLinearGradient(blockX, blockY, blockX + BLOCK_SIZE, blockY + BLOCK_SIZE);
    gradient.addColorStop(0, lightenColor(color, 0.3));
    gradient.addColorStop(1, color);

    ctx.fillStyle = gradient;
    ctx.fillRect(blockX, blockY, BLOCK_SIZE, BLOCK_SIZE);

    // Add shadow
    ctx.shadowColor = 'rgba(0, 0, 0, 0.3)';
    ctx.shadowBlur = 10;
    ctx.shadowOffsetX = 2;
    ctx.shadowOffsetY = 2;

    ctx.strokeStyle = '#333';
    ctx.lineWidth = 2;
    ctx.strokeRect(blockX, blockY, BLOCK_SIZE, BLOCK_SIZE);

    // Reset shadow
    ctx.shadowColor = 'transparent';
    ctx.shadowBlur = 0;
    ctx.shadowOffsetX = 0;
    ctx.shadowOffsetY = 0;
}

// Utility function to lighten a color
function lightenColor(color, percent) {
    const num = parseInt(color.replace('#',''),16),
          amt = Math.round(2.55 * percent * 100),
          R = (num >> 16) + amt,
          G = ((num >> 8) & 0x00FF) + amt,
          B = (num & 0x0000FF) + amt;
    return '#' + (
        0x1000000 + 
        (R<255?R<1?0:R:255)*0x10000 + 
        (G<255?G<1?0:G:255)*0x100 + 
        (B<255?B<1?0:B:255)
    ).toString(16).slice(1);
}

// Clear the entire main game canvas
function clearCanvas() {
    ctx.fillStyle = EMPTY_COLOR;
    ctx.fillRect(0, 0, canvas.width, canvas.height);
}

// Render the board and the current piece
function render() {
    clearCanvas();
    drawBoard();
    if (currentPiece) {
        currentPiece.draw();
    }
}

// Draw the board blocks
function drawBoard() {
    for (let row = 0; row < ROWS; row++) {
        for (let col = 0; col < COLS; col++) {
            if (board[row][col] !== EMPTY_COLOR) {
                drawBlock(col, row, board[row][col]);
            }
        }
    }
}

// Generate a random Tetromino piece
function randomPiece() {
    if (!nextPiece) {
        nextPiece = new Piece(SHAPES[Math.floor(Math.random() * SHAPES.length)], COLORS[Math.floor(Math.random() * COLORS.length)]);
    }
    const piece = nextPiece;
    nextPiece = new Piece(SHAPES[Math.floor(Math.random() * SHAPES.length)], COLORS[Math.floor(Math.random() * COLORS.length)]);
    drawNextPiece();
    return piece;
}

// Piece class representing a Tetromino
class Piece {
    constructor(shape, color) {
        this.shape = shape;
        this.color = color;
        this.x = Math.floor(COLS / 2) - Math.floor(this.shape[0].length / 2);
        this.y = 0;
    }

    // Draw the piece on the main game canvas
    draw() {
        this.shape.forEach((row, dy) => {
            row.forEach((value, dx) => {
                if (value) {
                    drawBlock(this.x + dx, this.y + dy, this.color);
                }
            });
        });
    }

    // Move the piece by (dx, dy)
    move(dx, dy) {
        if (!this.collides(this.x + dx, this.y + dy, this.shape)) {
            this.x += dx;
            this.y += dy;
            return true;
        }
        return false;
    }

    // Rotate the piece clockwise
    rotate() {
        const rotatedShape = this.shape[0].map((_, i) => this.shape.map(row => row[i]).reverse());
        if (!this.collides(this.x, this.y, rotatedShape)) {
            this.shape = rotatedShape;
            rotateSound.play();
        } else {
            // Attempt to shift piece to the left or right if rotation causes collision
            if (!this.collides(this.x - 1, this.y, rotatedShape)) {
                this.x -= 1;
                this.shape = rotatedShape;
                rotateSound.play();
            } else if (!this.collides(this.x + 1, this.y, rotatedShape)) {
                this.x += 1;
                this.shape = rotatedShape;
                rotateSound.play();
            }
        }
    }

    // Check if the piece collides with the board or boundaries
    collides(x, y, shape) {
        return shape.some((row, dy) => {
            return row.some((value, dx) => {
                let newX = x + dx;
                let newY = y + dy;
                // Allow pieces to spawn above the board
                if (newY < 0) return false;
                return (
                    value &&
                    (newX < 0 || newX >= COLS || newY >= ROWS || board[newY][newX] !== EMPTY_COLOR)
                );
            });
        });
    }

    // Lock the piece into the board
    lock() {
        this.shape.forEach((row, dy) => {
            row.forEach((value, dx) => {
                if (value) {
                    const newY = this.y + dy;
                    const newX = this.x + dx;
                    if (newY >= 0 && newY < ROWS && newX >= 0 && newX < COLS) {
                        board[newY][newX] = this.color;
                    }
                }
            });
        });
    }
}

// Clear completed lines with animation and update the score
function clearLinesWithAnimation(callback) {
    let linesCleared = [];
    for (let row = ROWS - 1; row >= 0; row--) {
        if (board[row].every(cell => cell !== EMPTY_COLOR)) {
            linesCleared.push(row);
        }
    }

    if (linesCleared.length === 0) {
        callback(0);
        return;
    }

    // Animate all cleared lines simultaneously
    animateMultipleLineClears(linesCleared).then(() => {
        // Remove cleared lines
        linesCleared.forEach(row => {
            board.splice(row, 1);
            board.unshift(Array(COLS).fill(EMPTY_COLOR));
        });

        // Update the score and lines after animation
        if (linesCleared.length > 0) {
            clearSound.play();
            score += calculateScore(linesCleared.length);
            lines += linesCleared.length;
            scoreDisplay.textContent = score;
            linesDisplay.textContent = lines;
            callback(linesCleared.length);
        }
    });
}

// Function to animate multiple lines simultaneously with a fading effect
function animateMultipleLineClears(rows) {
    return new Promise((resolve) => {
        const totalFrames = 20;
        let currentFrame = 0;
        const originalColors = rows.map(row => board[row].slice());

        function animate() {
            if (currentFrame < totalFrames) {
                const fadeFactor = currentFrame / totalFrames;
                rows.forEach((row, index) => {
                    board[row] = originalColors[index].map(color => fadeColor(color, fadeFactor));
                });

                render();
                currentFrame++;
                requestAnimationFrame(animate);
            } else {
                // Lines have been fully faded, proceed to clear them
                resolve();
            }
        }

        animate();
    });
}

// Function to calculate score based on lines cleared
function calculateScore(cleared) {
    const points = [0, 40, 100, 300, 1200];
    return points[cleared] || 0;
}

// Function to animate the line clearing with a fading effect
function animateLineClear(row) {
    return new Promise((resolve) => {
        const totalFrames = 20;
        let currentFrame = 0;
        const originalColors = board[row].slice(); // Store original colors

        function animate() {
            if (currentFrame < totalFrames) {
                const fadeFactor = currentFrame / totalFrames;
                board[row].forEach((_, col) => {
                    board[row][col] = fadeColor(originalColors[col], fadeFactor);
                });

                render();
                currentFrame++;
                requestAnimationFrame(animate);
            } else {
                // Line has been cleared, proceed
                resolve();
            }
        }

        animate();
    });
}

// Utility function to fade a color based on the fade factor (0 to 1)
function fadeColor(color, factor) {
    // Convert HEX to RGB
    let r, g, b;
    if (color.startsWith('#')) {
        const hex = color.substring(1);
        const bigint = parseInt(hex, 16);
        r = (bigint >> 16) & 255;
        g = (bigint >> 8) & 255;
        b = bigint & 255;
    } else if (color.startsWith('rgb')) {
        const rgbValues = color.match(/\d+/g);
        r = parseInt(rgbValues[0]);
        g = parseInt(rgbValues[1]);
        b = parseInt(rgbValues[2]);
    }

    // Calculate faded color towards EMPTY_COLOR (#111)
    const targetR = 17; // HEX for #111
    const targetG = 17;
    const targetB = 17;

    const newR = Math.floor(r + (targetR - r) * factor);
    const newG = Math.floor(g + (targetG - g) * factor);
    const newB = Math.floor(b + (targetB - b) * factor);

    return `rgb(${newR}, ${newG}, ${newB})`;
}

// The main game loop that updates the game state
function gameLoop() {
    if (!currentPiece.move(0, 1)) {
        currentPiece.lock();
        canHold = true; // Reset hold ability
        clearInterval(gameInterval);
        
        // Initiate animation and then proceed
        clearLinesWithAnimation((cleared) => {
            if (cleared > 0) {
                if (Math.floor(lines / LINES_PER_LEVEL) + 1 > level) {
                    levelUp();
                }
            }
            if (board[0].some(cell => cell !== EMPTY_COLOR)) {
                gameOver();
                return;
            }
            currentPiece = randomPiece();
            // Resume the game loop
            gameInterval = setInterval(gameLoop, gameSpeed);
        });

    }
    render();
}

// Handle game over state
function gameOver() {
    clearInterval(gameInterval);
    gameOverSound.play();
    if (modalAppearSound) modalAppearSound.play(); // Optional
    
    // Update final score, level, and lines in the modal
    finalScoreDisplay.textContent = score;
    finalLevelDisplay.textContent = level;
    finalLinesDisplay.textContent = lines;
    
    // Update high score if necessary
    updateHighScore();
    
    // Display the Game Over modal
    gameOverModal.style.display = 'flex';
    
    // Hide the Hold button
    holdButton.style.display = 'none';
    
    // Disable Play button to prevent multiple loops
    playButton.disabled = false;
}

// Handle user input for controlling the Tetromino
document.addEventListener('keydown', (e) => {
    // Prevent input if modal is active
    if (gameOverModal.style.display === 'flex') return;
    
    if (!currentPiece || keyCooldown) return;
    let moved = false;
    switch (e.key) {
        case 'ArrowLeft':
            moved = currentPiece.move(-1, 0);
            if (moved) moveSound.play();
            break;
        case 'ArrowRight':
            moved = currentPiece.move(1, 0);
            if (moved) moveSound.play();
            break;
        case 'ArrowDown':
            moved = currentPiece.move(0, 1);
            if (moved) moveSound.play();
            break;
        case 'ArrowUp':
            currentPiece.rotate();
            // Rotation sound is handled inside the rotate method
            moved = true;
            break;
        case ' ':
            // Hard drop
            while (currentPiece.move(0, 1)) {}
            moveSound.play();
            break;
    }
    if (moved) {
        render();
        keyCooldown = true;
        setTimeout(() => { keyCooldown = false; }, KEY_DELAY);
    }
});

// Start or restart the game
function startGame() {
    createBoard();
    currentPiece = randomPiece();
    score = 0;
    level = 1;
    lines = 0;
    scoreDisplay.textContent = score;
    levelDisplay.textContent = level;
    linesDisplay.textContent = lines;
    holdPiece = null;
    drawHoldPiece();
    drawNextPiece();
    playButton.style.display = 'none';
    playButton.disabled = true;
    clearInterval(gameInterval);
    gameSpeed = 500; // Reset game speed
    gameInterval = setInterval(gameLoop, gameSpeed);
    canHold = true;
    keyCooldown = false;
    
    // Reset and hide the Game Over modal if it's currently displayed
    gameOverModal.style.display = 'none';
    
    // Show the Hold and Pause buttons
    holdButton.style.display = 'inline-block';
    const pauseButton = document.getElementById('pauseButton');
    pauseButton.style.display = 'inline-block';
    pauseButton.textContent = 'Pause';
    
    render();
}

// Play button event listener
playButton.addEventListener('click', startGame);

// Restart button in the modal
restartButton.addEventListener('click', () => {
    startGame();
});

// Exit button in the modal
exitButton.addEventListener('click', () => {
    // Define what "exit" should do. For this example, we'll reload the page.
    window.location.reload();
});

// Pause functionality
let isPaused = false;
document.getElementById('pauseButton').addEventListener('click', () => {
    if (isPaused) {
        // Resume the game
        gameInterval = setInterval(gameLoop, gameSpeed);
        document.getElementById('pauseButton').textContent = 'Pause';
        isPaused = false;
    } else {
        // Pause the game
        clearInterval(gameInterval);
        document.getElementById('pauseButton').textContent = 'Resume';
        isPaused = true;
    }
});

// Hold feature implementation
holdButton.addEventListener('click', () => {
    if (!canHold || !currentPiece) return;
    holdSound.play();
    if (holdPiece) {
        [holdPiece, currentPiece] = [currentPiece, holdPiece];
        currentPiece.x = Math.floor(COLS / 2) - Math.floor(currentPiece.shape[0].length / 2);
        currentPiece.y = 0;
    } else {
        holdPiece = currentPiece;
        currentPiece = randomPiece();
    }
    drawHoldPiece();
    drawNextPiece();
    canHold = false;
    render();
});

/**
 * Draws a tetromino piece on a given canvas context with centering.
 * @param {CanvasRenderingContext2D} context - The canvas context to draw on.
 * @param {Piece} piece - The tetromino piece to draw.
 * @param {number} canvasWidth - Width of the canvas.
 * @param {number} canvasHeight - Height of the canvas.
 */
function drawPreviewPiece(context, piece, canvasWidth, canvasHeight) {
    context.fillStyle = EMPTY_COLOR;
    context.fillRect(0, 0, canvasWidth, canvasHeight);

    if (!piece) return;

    const shape = piece.shape;
    const color = piece.color;

    const rows = shape.length;
    const cols = shape[0].length;

    // Calculate piece dimensions
    const pieceWidth = cols * PREVIEW_BLOCK_SIZE;
    const pieceHeight = rows * PREVIEW_BLOCK_SIZE;

    // Calculate starting position to center the piece
    const startX = Math.floor((canvasWidth - pieceWidth) / 2);
    const startY = Math.floor((canvasHeight - pieceHeight) / 2);

    shape.forEach((row, dy) => {
        row.forEach((value, dx) => {
            if (value) {
                const x = startX + dx * PREVIEW_BLOCK_SIZE;
                const y = startY + dy * PREVIEW_BLOCK_SIZE;

                // Draw block with border
                context.fillStyle = color;
                context.fillRect(x, y, PREVIEW_BLOCK_SIZE, PREVIEW_BLOCK_SIZE);
                context.strokeStyle = '#333';
                context.strokeRect(x, y, PREVIEW_BLOCK_SIZE, PREVIEW_BLOCK_SIZE);
            }
        });
    });
}

// Draw the next piece in the preview canvas
function drawNextPiece() {
    drawPreviewPiece(nextCtx, nextPiece, nextPieceCanvas.width, nextPieceCanvas.height);
}

// Draw the hold piece in the preview canvas
function drawHoldPiece() {
    drawPreviewPiece(holdCtx, holdPiece, holdPieceCanvas.width, holdPieceCanvas.height);
}

// Level up function
function levelUp() {
    clearInterval(gameInterval);
    level++;
    levelDisplay.textContent = level;
    gameSpeed = Math.max(100, gameSpeed - SPEED_INCREMENT);
    gameInterval = setInterval(gameLoop, gameSpeed);
}

// Initialize hold and next piece displays
function initializeDisplays() {
    drawNextPiece();
    drawHoldPiece();
}

// High Score Management
function updateHighScore() {
    let highScore = localStorage.getItem('tetrisHighScore') || 0;
    if (score > highScore) {
        localStorage.setItem('tetrisHighScore', score);
        highScoreDisplay.textContent = score;
    } else {
        highScoreDisplay.textContent = highScore;
    }
}

// Initialize high score display on page load
window.onload = () => {
    let highScore = localStorage.getItem('tetrisHighScore') || 0;
    highScoreDisplay.textContent = highScore;
    initializeDisplays();
};
```