๐Ÿงถ Ugly Sweater Spot the Difference - Christmas Game ๐ŸŽ„

Two ugly Christmas sweaters, one knitting mistake! Find the square that is different and tap it on either sweater before the timer runs out. Each find adds time, but a wrong tap costs 2 seconds.
Found: 0  |  Best: 0  |  Time: 15.0s
Press "Start" to unpack the sweaters!

Grandma knitted two ugly Christmas sweaters for the office party ๐ŸŽ„, and one of them has a mistake: a reindeer where a snowflake should be, or a square in the wrong colour. Tap the different square on either sweater before the timer runs out. Each find gives you a little more time, but the sweaters get bigger, the mistakes get sneakier, and every wrong tap costs you 2 seconds. When the time is up, the last mistake is highlighted.

How many knitting mistakes can you spot before National Ugly Christmas Sweater Day? ๐Ÿงถ

Here is the code
CSS
.usw-game {
    text-align: center;
}

.usw-game .instructions {
    max-width: 520px;
    margin: 0 auto 15px;
    font-size: 1em;
    color: #555;
}

.usw-info {
    margin-bottom: 8px;
    font-size: 1.15em;
}

.usw-bar {
    max-width: 520px;
    height: 10px;
    margin: 0 auto 14px;
    background: #e5e7eb;
    border-radius: 999px;
    overflow: hidden;
}

.usw-bar-fill {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, #c0392b, #1d6b3a);
    border-radius: 999px;
}

.usw-board {
    display: flex;
    flex-wrap: nowrap;
    justify-content: center;
    gap: 16px;
    max-width: 100%;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

.usw-sweater {
    position: relative;
    flex: 1 1 0;
    min-width: 0;
    max-width: 250px;
    padding: 22px 12px 14px;
    background: #b8232f;
    border-radius: 28px 28px 14px 14px;
    box-shadow: inset 0 -10px 0 #8e1b25, 0 4px 10px rgba(0, 0, 0, 0.15);
}

.usw-collar {
    position: absolute;
    top: 0;
    left: 50%;
    width: 70px;
    height: 16px;
    transform: translateX(-50%);
    background: #fff;
    border-radius: 0 0 40px 40px;
    border: 4px solid #1d6b3a;
    border-top: none;
}

.usw-grid {
    display: grid;
    gap: 3px;
    background: #f7f1e3;
    padding: 4px;
    border-radius: 8px;
    border-top: 6px dashed #1d6b3a;
    border-bottom: 6px dashed #1d6b3a;
}

.usw-cell {
    aspect-ratio: 1;
    border: none;
    border-radius: 5px;
    padding: 0;
    font-size: clamp(11px, 3.6vw, 22px);
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
}

.usw-cell.usw-wrong {
    outline: 3px solid #111;
    animation: usw-shake 0.3s;
}

.usw-cell.usw-found {
    outline: 4px solid #f1c40f;
    transform: scale(1.12);
}

@keyframes usw-shake {
    25% { transform: translateX(-3px); }
    75% { transform: translateX(3px); }
}

.usw-message {
    min-height: 1.6em;
    margin: 14px auto 10px;
    font-size: 1.1em;
    font-weight: bold;
}

@media (max-width: 420px) {
    .usw-board {
        gap: 8px;
    }

    .usw-sweater {
        padding: 18px 6px 10px;
    }
}
HTML
<div class="usw-game">
    <h1>๐Ÿงถ Ugly Sweater Spot the Difference - Christmas Game ๐ŸŽ„</h1>
    <div class="instructions">
        Two ugly Christmas sweaters, one knitting mistake! Find the square that is different and tap it on either sweater before the timer runs out. Each find adds time, but a wrong tap costs 2 seconds.
    </div>
    <div class="usw-info">Found: <span id="uswScore">0</span> &nbsp;|&nbsp; Best: <span id="uswBest">0</span> &nbsp;|&nbsp; Time: <span id="uswTime">15.0</span>s</div>
    <div class="usw-bar"><div class="usw-bar-fill" id="uswBar"></div></div>
    <div class="usw-board" id="uswBoard">
        <div class="usw-sweater"><div class="usw-collar"></div><div class="usw-grid" id="uswLeft"></div></div>
        <div class="usw-sweater"><div class="usw-collar"></div><div class="usw-grid" id="uswRight"></div></div>
    </div>
    <div class="usw-message" id="uswMessage">Press "Start" to unpack the sweaters!</div>
    <button class="start-btn" id="uswStart">Start</button>
</div>
JS
(function () {
    var MOTIFS = ['โ„๏ธ', '๐ŸŽ„', 'โญ', '๐ŸฆŒ', '๐ŸŽ', 'โค๏ธ', '๐Ÿ””', 'โ›„', '๐Ÿช'];
    var COLORS = ['#f7f1e3', '#1d6b3a', '#e74c3c', '#2e86c1', '#f1c40f', '#ffffff'];
    var START_TIME = 15;
    var MAX_TIME = 20;
    var PENALTY = 2;

    var leftEl = document.getElementById('uswLeft');
    var rightEl = document.getElementById('uswRight');
    var scoreEl = document.getElementById('uswScore');
    var bestEl = document.getElementById('uswBest');
    var timeEl = document.getElementById('uswTime');
    var barEl = document.getElementById('uswBar');
    var messageEl = document.getElementById('uswMessage');
    var startBtn = document.getElementById('uswStart');
    var hidden = document.getElementById('hiddenScore');

    var score = 0;
    var best = 0;
    var timeLeft = START_TIME;
    var playing = false;
    var locked = false;
    var lastTime = 0;
    var diffIndex = -1;
    var size = 4;

    try {
        best = parseInt(localStorage.getItem('uswBest'), 10) || 0;
    } catch (e) {
        best = 0;
    }
    bestEl.textContent = best;

    function setHidden(value) {
        if (hidden) {
            hidden.value = value;
        }
    }

    function showShare(show) {
        // The share buttons come after the game in the page, so look them up when needed.
        var share = document.querySelector('.share-buttons');
        if (share) {
            share.style.display = show ? 'flex' : 'none';
        }
    }

    function pick(list) {
        return list[Math.floor(Math.random() * list.length)];
    }

    function gridSize() {
        if (score < 3) return 4;
        if (score < 8) return 5;
        return 6;
    }

    function makeCell(cell, index) {
        var button = document.createElement('button');
        button.type = 'button';
        button.className = 'usw-cell';
        button.style.background = cell.color;
        button.textContent = cell.motif;
        button.setAttribute('data-index', index);
        button.setAttribute('aria-label', 'Sweater square ' + (index + 1));
        return button;
    }

    function newRound() {
        size = gridSize();
        var cells = [];
        var count = size * size;
        // A symmetric knitted pattern: mirror the left half so it looks like a real sweater.
        for (var row = 0; row < size; row++) {
            var rowCells = [];
            for (var col = 0; col < Math.ceil(size / 2); col++) {
                rowCells.push({ motif: Math.random() < 0.7 ? pick(MOTIFS) : '', color: pick(COLORS) });
            }
            for (var c = 0; c < size; c++) {
                var source = c < rowCells.length ? rowCells[c] : rowCells[size - 1 - c];
                cells.push({ motif: source.motif, color: source.color });
            }
        }

        diffIndex = Math.floor(Math.random() * count);
        var changed = { motif: cells[diffIndex].motif, color: cells[diffIndex].color };
        // Early rounds change the motif; later ones may only change the colour.
        if (score < 4 || Math.random() < 0.5) {
            var motif = changed.motif;
            while (motif === changed.motif) {
                motif = pick(MOTIFS);
            }
            changed.motif = motif;
        } else {
            var color = changed.color;
            while (color === changed.color) {
                color = pick(COLORS);
            }
            changed.color = color;
        }

        var flipRight = Math.random() < 0.5;
        render(leftEl, cells, flipRight ? null : changed);
        render(rightEl, cells, flipRight ? changed : null);
        locked = false;
    }

    function render(gridEl, cells, changed) {
        gridEl.innerHTML = '';
        gridEl.style.gridTemplateColumns = 'repeat(' + size + ', 1fr)';
        for (var i = 0; i < cells.length; i++) {
            var cell = (changed && i === diffIndex) ? changed : cells[i];
            gridEl.appendChild(makeCell(cell, i));
        }
    }

    function updateHud() {
        scoreEl.textContent = score;
        timeEl.textContent = Math.max(0, timeLeft).toFixed(1);
        barEl.style.width = Math.max(0, Math.min(100, timeLeft / MAX_TIME * 100)) + '%';
    }

    function markCells(index, className) {
        var buttons = document.querySelectorAll('.usw-cell[data-index="' + index + '"]');
        for (var i = 0; i < buttons.length; i++) {
            buttons[i].classList.add(className);
        }
    }

    function onTap(event) {
        var target = event.target.closest ? event.target.closest('.usw-cell') : null;
        if (!target || !playing || locked) return;
        event.preventDefault();
        var index = parseInt(target.getAttribute('data-index'), 10);
        if (index === diffIndex) {
            locked = true;
            score++;
            setHidden(score);
            timeLeft = Math.min(MAX_TIME, timeLeft + Math.max(1.5, 3 - score * 0.08));
            markCells(index, 'usw-found');
            messageEl.textContent = pick(['Found it! ๐ŸŽ‰', 'Sharp eyes! ๐Ÿ‘€', 'Nice catch! ๐Ÿงถ', 'Ho ho ho! ๐ŸŽ…']);
            updateHud();
            setTimeout(function () {
                if (playing) newRound();
            }, 350);
        } else {
            timeLeft -= PENALTY;
            target.classList.remove('usw-wrong');
            void target.offsetWidth;
            target.classList.add('usw-wrong');
            messageEl.textContent = 'Not that one! -' + PENALTY + 's';
            updateHud();
        }
    }

    function loop(now) {
        if (!playing) return;
        var dt = Math.min(0.1, (now - lastTime) / 1000);
        lastTime = now;
        timeLeft -= dt;
        updateHud();
        if (timeLeft <= 0) {
            endGame();
            return;
        }
        requestAnimationFrame(loop);
    }

    function endGame() {
        playing = false;
        timeLeft = 0;
        updateHud();
        markCells(diffIndex, 'usw-found');
        if (score > best) {
            best = score;
            bestEl.textContent = best;
            try {
                localStorage.setItem('uswBest', String(best));
            } catch (e) {
                // Storage may be blocked; the best score just won't persist.
            }
        }
        messageEl.textContent = "Time's up! You found " + score + (score === 1 ? ' mistake' : ' mistakes') + '. The last one is highlighted.';
        startBtn.textContent = 'Play again';
        startBtn.style.display = 'inline-block';
        showShare(true);
    }

    function startGame() {
        score = 0;
        timeLeft = START_TIME;
        setHidden(0);
        showShare(false);
        startBtn.style.display = 'none';
        messageEl.textContent = 'Find the difference!';
        newRound();
        updateHud();
        playing = true;
        lastTime = performance.now();
        requestAnimationFrame(loop);
    }

    leftEl.addEventListener('click', onTap);
    rightEl.addEventListener('click', onTap);
    startBtn.addEventListener('click', startGame);

    // A first, still pattern so the page doesn't look empty before Start.
    newRound();
    locked = true;
    updateHud();
})();