  <script>
    (function() {
      let GG_ALL_GAME_CONFIG = {
        clickCount: 0, // Number of times the shape has been clicked
        shapeColor: "red", // Color of the clickable shape
        shapeSize: 100, // Size of the shape in pixels
        innerShapeColor: "#ff6666", // Color of the inner shape
        shapes: ['triangle', 'square', 'pentagon', 'hexagon', 'heptagon', 'octagon'], // Array of shape classes
        colors: ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'], // Array of ROYGBIV colors
        clicksPerChange: 10, // Number of clicks before changing shape
        gameStarted: false, // Whether the game has started
        normalVisitAchieved: false, // Whether the normal visit achievement has been unlocked
        mobileVisitAchieved: false, // Whether the mobile visit achievement has been unlocked
        firefoxVisitAchieved: false, // Whether the Firefox visit achievement has been unlocked
        edgeVisitAchieved: false, // Whether the Edge visit achievement has been unlocked
        particleCount: 50, // Number of particles to create when shape changes
        particleLifetime: 1000 // Lifetime of particles in milliseconds
      };
      const shape = document.getElementById('shape');
      const clickCounter = document.getElementById('click-count');
      const progressBar = document.getElementById('progress');
      const startScreen = document.getElementById('start-screen');
      const startButton = document.getElementById('start-button');
      const trophyButton = document.getElementById('trophy-button');
      const trophyMenu = document.getElementById('trophy-menu');
      const trophyMenuClose = document.getElementById('trophy-menu-close');
      const settingsButton = document.getElementById('settings-button');
      const settingsMenu = document.getElementById('settings-menu');
      const settingsMenuClose = document.getElementById('settings-menu-close');
      const clicksPerChangeInput = document.getElementById('clicks-per-change');
      const gameContainer = document.getElementById('game-container');
      const phonePlaceholder = document.getElementById('phone-placeholder');
      const phoneMenu = document.getElementById('phone-menu');

      function updateClickCount() {
        if (!GG_ALL_GAME_CONFIG.gameStarted) return;
        GG_ALL_GAME_CONFIG.clickCount++;
        clickCounter.textContent = `Clicks: ${GG_ALL_GAME_CONFIG.clickCount}`;
        updateProgressBar();
        if (GG_ALL_GAME_CONFIG.clickCount % GG_ALL_GAME_CONFIG.clicksPerChange === 0) {
          updateShape();
          createParticles();
        }
        saveGame({
          clickCount: GG_ALL_GAME_CONFIG.clickCount,
          normalVisitAchieved: GG_ALL_GAME_CONFIG.normalVisitAchieved,
          mobileVisitAchieved: GG_ALL_GAME_CONFIG.mobileVisitAchieved
        });
      }

      function updateProgressBar() {
        const progress = (GG_ALL_GAME_CONFIG.clickCount % GG_ALL_GAME_CONFIG.clicksPerChange) / GG_ALL_GAME_CONFIG.clicksPerChange * 100;
        progressBar.style.width = `${progress}%`;
      }

      function updateShape() {
        const shapeIndex = Math.floor(GG_ALL_GAME_CONFIG.clickCount / GG_ALL_GAME_CONFIG.clicksPerChange) % GG_ALL_GAME_CONFIG.shapes.length;
        const colorIndex = Math.floor(GG_ALL_GAME_CONFIG.clickCount / (GG_ALL_GAME_CONFIG.clicksPerChange * GG_ALL_GAME_CONFIG.shapes.length)) % GG_ALL_GAME_CONFIG.colors.length;
        shape.className = GG_ALL_GAME_CONFIG.shapes[shapeIndex];
        const mainColor = GG_ALL_GAME_CONFIG.colors[colorIndex];
        const lighterColor = getLighterColor(mainColor);
        document.documentElement.style.setProperty('--shape-color', mainColor);
        document.documentElement.style.setProperty('--inner-shape-color', lighterColor);
      }

      function createParticles() {
        for (let i = 0; i < GG_ALL_GAME_CONFIG.particleCount; i++) {
          setTimeout(() => {
            const particle = document.createElement('div');
            particle.classList.add('particle');
            const angle = Math.random() * Math.PI * 2;
            const distance = Math.random() * 150 + 50; // Random distance between 50 and 200 pixels
            const size = Math.random() * 5 + 5;
            particle.style.width = `${size}px`;
            particle.style.height = `${size}px`;
            particle.style.left = `${shape.offsetLeft + shape.offsetWidth / 2}px`;
            particle.style.top = `${shape.offsetTop + shape.offsetHeight / 2}px`;
            gameContainer.appendChild(particle);
            const animation = particle.animate([{
                transform: `translate(0, 0)`
              },
              {
                transform: `translate(${Math.cos(angle) * distance}px, ${Math.sin(angle) * distance + 50}px)`
              }
            ], {
              duration: GG_ALL_GAME_CONFIG.particleLifetime,
              easing: 'cubic-bezier(0.25, 0.1, 0.25, 1)'
            });
            animation.onfinish = () => {
              particle.remove();
            };
          }, Math.random() * 500); // Random delay between 0 and 500 milliseconds
        }
      }

      function getLighterColor(color) {
        const colorMap = {
          'red': '#ff6666',
          'orange': '#ffb366',
          'yellow': '#ffff66',
          'green': '#66ff66',
          'blue': '#66a3ff',
          'indigo': '#9966ff',
          'violet': '#ff66ff'
        };
        return colorMap[color] || color;
      }

      function startGame() {
        GG_ALL_GAME_CONFIG.gameStarted = true;
        startScreen.style.display = 'none';
        loadGame();
        initializeTrophyDescriptions();
        if (!GG_ALL_GAME_CONFIG.normalVisitAchieved) {
          unlockFirstTrophy();
        }
        checkBrowserVisits();
        showPhonePlaceholder();
        initDraggablePhone();
      }

      function initDraggablePhone() {
        const phone = document.getElementById('phone-placeholder');
        let isDragging = false;
        let startX, startY;

        function onMouseDown(e) {
          isDragging = true;
          startX = e.clientX - phone.offsetLeft;
          startY = e.clientY - phone.offsetTop;
          e.preventDefault();
        }

        function onMouseMove(e) {
          if (!isDragging) return;
          let newX = e.clientX - startX;
          let newY = e.clientY - startY;
          phone.style.left = `${newX}px`;
          phone.style.top = `${newY}px`;
          phone.style.bottom = 'auto';
          phone.style.right = 'auto';
        }

        function onMouseUp() {
          isDragging = false;
        }
        phone.addEventListener('mousedown', onMouseDown);
        document.addEventListener('mousemove', onMouseMove);
        document.addEventListener('mouseup', onMouseUp);
        // Start floating animation
        phone.style.animation = 'float 3s ease-in-out infinite';
      }

      function applyLoadedState() {
        clickCounter.textContent = `Clicks: ${GG_ALL_GAME_CONFIG.clickCount}`;
        updateShape();
        updateProgressBar();
      }

      function toggleTrophyMenu() {
        trophyMenu.style.display = trophyMenu.style.display === 'none' ? 'block' : 'none';
      }

      function toggleSettingsMenu() {
        settingsMenu.style.display = settingsMenu.style.display === 'none' ? 'block' : 'none';
      }

      function unlockTrophy(trophyId) {
        const trophyItem = document.querySelector(`.trophy-item[data-id="${trophyId}"]`);
        if (trophyItem && trophyItem.classList.contains('trophy-locked')) {
          trophyItem.classList.remove('trophy-locked');
          const checkmark = trophyItem.querySelector('.trophy-checkmark');
          if (checkmark) {
            checkmark.style.opacity = '1';
          }
          const description = trophyItem.querySelector('.trophy-description');
          if (description) {
            trophyItem.setAttribute('title', description.textContent);
          }
          // Trophy unlocked notification removed
          // Save the game state after unlocking a trophy
          GG_ALL_GAME_CONFIG[`${trophyId}Achieved`] = true;
          saveGame({
            clickCount: GG_ALL_GAME_CONFIG.clickCount,
            normalVisitAchieved: GG_ALL_GAME_CONFIG.normalVisitAchieved,
            mobileVisitAchieved: GG_ALL_GAME_CONFIG.mobileVisitAchieved
          });
        }
      }

      function applyTrophyState() {
        if (GG_ALL_GAME_CONFIG.normalVisitAchieved) {
          unlockTrophy('normal-visit');
        }
        if (GG_ALL_GAME_CONFIG.mobileVisitAchieved) {
          unlockTrophy('mobile-visit');
        }
        if (GG_ALL_GAME_CONFIG.firefoxVisitAchieved) {
          unlockTrophy('firefox-visit');
        }
        if (GG_ALL_GAME_CONFIG.edgeVisitAchieved) {
          unlockTrophy('edge-visit');
        }
        showPhonePlaceholder();
      }

      function checkBrowserVisits() {
        const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
        const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
        const isEdge = navigator.userAgent.toLowerCase().indexOf('edg') > -1;
        if (isMobile || GG_ALL_GAME_CONFIG.mobileVisitAchieved) {
          unlockTrophy('mobile-visit');
          if (!GG_ALL_GAME_CONFIG.mobileVisitAchieved) {
            showAchievementNotification('Method 2: Pocket Portal Complete!');
            showAchievementNotification('Unlocked Phone!');
            GG_ALL_GAME_CONFIG.mobileVisitAchieved = true;
          }
        }
        if (isFirefox || GG_ALL_GAME_CONFIG.firefoxVisitAchieved) {
          unlockTrophy('firefox-visit');
          if (!GG_ALL_GAME_CONFIG.firefoxVisitAchieved) {
            showAchievementNotification('Method 3: Fire Complete!');
            GG_ALL_GAME_CONFIG.firefoxVisitAchieved = true;
          }
        }
        if (isEdge || GG_ALL_GAME_CONFIG.edgeVisitAchieved) {
          unlockTrophy('edge-visit');
          if (!GG_ALL_GAME_CONFIG.edgeVisitAchieved) {
            showAchievementNotification('Method 4: Edge Complete!');
            GG_ALL_GAME_CONFIG.edgeVisitAchieved = true;
          }
        }
        showPhonePlaceholder();
        saveGame({
          clickCount: GG_ALL_GAME_CONFIG.clickCount,
          normalVisitAchieved: GG_ALL_GAME_CONFIG.normalVisitAchieved,
          mobileVisitAchieved: GG_ALL_GAME_CONFIG.mobileVisitAchieved,
          firefoxVisitAchieved: GG_ALL_GAME_CONFIG.firefoxVisitAchieved,
          edgeVisitAchieved: GG_ALL_GAME_CONFIG.edgeVisitAchieved
        });
      }

      function initializeTrophyDescriptions() {
        const trophyItems = document.querySelectorAll('.trophy-item');
        trophyItems.forEach(item => {
          const description = item.querySelector('.trophy-description');
          if (description && !item.classList.contains('trophy-locked')) {
            item.setAttribute('title', description.textContent);
          } else {
            item.removeAttribute('title');
          }
        });
      }

      function unlockFirstTrophy() {
        unlockTrophy('normal-visit');
        showAchievementNotification('Method 1: Mistake Complete!');
      }

      function checkBrowserVisits() {
        // Check if the user is on a mobile device
        const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
        // Check if the user is using Firefox
        const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
        // Check if the user is using Edge
        const isEdge = navigator.userAgent.toLowerCase().indexOf('edg') > -1;
        let stateChanged = false;
        // Mobile visit check
        if ((isMobile || GG_ALL_GAME_CONFIG.mobileVisitAchieved) && !GG_ALL_GAME_CONFIG.mobileVisitAchieved) {
          unlockTrophy('mobile-visit');
          showAchievementNotification('Method 2: Pocket Portal Complete!');
          showAchievementNotification('Unlocked Phone!');
          GG_ALL_GAME_CONFIG.mobileVisitAchieved = true;
          stateChanged = true;
        }
        // Firefox visit check
        if ((isFirefox || GG_ALL_GAME_CONFIG.firefoxVisitAchieved) && !GG_ALL_GAME_CONFIG.firefoxVisitAchieved) {
          unlockTrophy('firefox-visit');
          showAchievementNotification('Method 3: Fire Complete!');
          GG_ALL_GAME_CONFIG.firefoxVisitAchieved = true;
          stateChanged = true;
        }
        // Edge visit check
        if ((isEdge || GG_ALL_GAME_CONFIG.edgeVisitAchieved) && !GG_ALL_GAME_CONFIG.edgeVisitAchieved) {
          unlockTrophy('edge-visit');
          showAchievementNotification('Method 4: Edge Complete!');
          GG_ALL_GAME_CONFIG.edgeVisitAchieved = true;
          stateChanged = true;
        }
        showPhonePlaceholder();
        // Always save the game state to ensure persistence across different browsers
        saveGame({
          clickCount: GG_ALL_GAME_CONFIG.clickCount,
          normalVisitAchieved: GG_ALL_GAME_CONFIG.normalVisitAchieved,
          mobileVisitAchieved: GG_ALL_GAME_CONFIG.mobileVisitAchieved,
          firefoxVisitAchieved: GG_ALL_GAME_CONFIG.firefoxVisitAchieved,
          edgeVisitAchieved: GG_ALL_GAME_CONFIG.edgeVisitAchieved
        });
        // Problem: The issue was caused by the function only saving the state when a change was detected.
        // This led to a situation where achievements weren't being saved consistently across different browsers.
        //
        // Solution: We've modified the logic to check for both the current browser condition AND the saved state.
        // This ensures that if a trophy was unlocked in a different browser, it remains unlocked.
        // We've also removed the 'stateChanged' condition for saving, so the game state is always saved after this function runs.
        // This guarantees that all browser-specific achievements are consistently saved and persisted across different browsers.
      }

      function showPhonePlaceholder() {
        const phonePlaceholder = document.getElementById('phone-placeholder');
        if (GG_ALL_GAME_CONFIG.gameStarted && GG_ALL_GAME_CONFIG.mobileVisitAchieved) {
          phonePlaceholder.style.display = 'block';
          phonePlaceholder.style.opacity = '1';
        } else {
          phonePlaceholder.style.display = 'none';
          phonePlaceholder.style.opacity = '0';
        }
      }
      // This bug might have happened because:
      // 1. The trophy state was only being set when on a mobile device.
      // 2. When switching to a laptop, the code wasn't checking if the trophy had been previously unlocked.
      // 3. The trophy display was being reset based on the current device, not the saved state.
      //
      // To fix this, we've added a condition to unlock the trophy even on non-mobile devices
      // if it has been previously achieved. This ensures the trophy stays unlocked
      // regardless of the current device being used.
      let achievementNotificationsContainer;

      function showAchievementNotification(message) {
        if (!achievementNotificationsContainer) {
          achievementNotificationsContainer = document.createElement('div');
          achievementNotificationsContainer.id = 'achievement-notifications';
          document.body.appendChild(achievementNotificationsContainer);
        }
        const notification = document.createElement('div');
        notification.className = 'achievement-notification';
        notification.textContent = message;
        notification.style.backgroundColor = message.includes('Unlocked Phone') ? 'rgba(255, 255, 0, 0.9)' : 'rgba(0, 255, 0, 0.9)';
        achievementNotificationsContainer.appendChild(notification);
        setTimeout(() => {
          notification.classList.add('show');
        }, 100);
        setTimeout(() => {
          notification.classList.remove('show');
          setTimeout(() => {
            notification.remove();
            if (achievementNotificationsContainer.childElementCount === 0) {
              achievementNotificationsContainer.remove();
              achievementNotificationsContainer = null;
            }
          }, 300);
        }, 3000);
      }

      function updateClicksPerChange() {
        const newValue = parseInt(clicksPerChangeInput.value);
        if (!isNaN(newValue) && newValue >= 1 && newValue <= 100) {
          GG_ALL_GAME_CONFIG.clicksPerChange = newValue;
          updateProgressBar();
        }
      }

      function resetData() {
        if (confirm("Are you sure you want to reset all game data? This action cannot be undone.")) {
          GG_ALL_GAME_CONFIG.clickCount = 0;
          GG_ALL_GAME_CONFIG.normalVisitAchieved = false;
          GG_ALL_GAME_CONFIG.mobileVisitAchieved = false;
          GG_ALL_GAME_CONFIG.firefoxVisitAchieved = false;
          GG_ALL_GAME_CONFIG.edgeVisitAchieved = false;
          applyLoadedState();
          resetTrophies();
          saveGame({
            clickCount: GG_ALL_GAME_CONFIG.clickCount,
            normalVisitAchieved: GG_ALL_GAME_CONFIG.normalVisitAchieved,
            mobileVisitAchieved: GG_ALL_GAME_CONFIG.mobileVisitAchieved,
            firefoxVisitAchieved: GG_ALL_GAME_CONFIG.firefoxVisitAchieved,
            edgeVisitAchieved: GG_ALL_GAME_CONFIG.edgeVisitAchieved
          });
          alert("Game data and trophies have been reset.");
        }
      }

      function saveGame(gameState) {
        const saveGameEvent = {
          type: 'REQUEST_SAVE_GAME_EVENT',
          save_data: gameState
        };
        window.parent.postMessage(saveGameEvent, '*');
        // Additional fix: Add a small delay before allowing the next save
        // This helps prevent rapid successive saves that could lead to data loss
        saveGame = debounce(saveGame, 300);
      }
      // Debounce function to limit the rate at which saveGame can be called
      function debounce(func, wait) {
        let timeout;
        return function executedFunction(...args) {
          const later = () => {
            clearTimeout(timeout);
            func(...args);
          };
          clearTimeout(timeout);
          timeout = setTimeout(later, wait);
        };
      }

      function resetTrophies() {
        const trophyItems = document.querySelectorAll('.trophy-item');
        trophyItems.forEach(item => {
          item.classList.add('trophy-locked');
          const checkmark = item.querySelector('.trophy-checkmark');
          if (checkmark) {
            checkmark.style.opacity = '0';
          }
        });
      }

      function togglePhoneMenu() {
        phoneMenu.style.display = phoneMenu.style.display === 'none' ? 'block' : 'none';
        showPhoneEffect();
      }

      function showPhoneEffect() {
        const phone = document.getElementById('phone-placeholder');
        phone.style.animation = 'phoneEffect 0.3s ease-in-out';
        setTimeout(() => {
          phone.style.animation = 'float 3s ease-in-out infinite';
        }, 300);
      }
      shape.addEventListener('click', updateClickCount);
      startButton.addEventListener('click', startGame);
      trophyButton.addEventListener('click', toggleTrophyMenu);
      trophyMenuClose.addEventListener('click', toggleTrophyMenu);
      settingsButton.addEventListener('click', toggleSettingsMenu);
      settingsMenuClose.addEventListener('click', toggleSettingsMenu);
      clicksPerChangeInput.addEventListener('change', updateClicksPerChange);
      document.getElementById('reset-data-button').addEventListener('click', resetData);
      phonePlaceholder.addEventListener('click', togglePhoneMenu);

      function loadGame() {
        const loadGameEvent = {
          type: 'REQUEST_LOAD_GAME_EVENT'
        };
        window.parent.postMessage(loadGameEvent, '*');
      }
      window.addEventListener('message', (event) => {
        const {
          type,
          save_data
        } = event.data;
        if (type === 'RESPONSE_LOAD_GAME_EVENT') {
          if (save_data) {
            GG_ALL_GAME_CONFIG.clickCount = save_data.clickCount;
            GG_ALL_GAME_CONFIG.normalVisitAchieved = save_data.normalVisitAchieved || false;
            GG_ALL_GAME_CONFIG.mobileVisitAchieved = save_data.mobileVisitAchieved || false;
            GG_ALL_GAME_CONFIG.firefoxVisitAchieved = save_data.firefoxVisitAchieved || false;
            GG_ALL_GAME_CONFIG.edgeVisitAchieved = save_data.edgeVisitAchieved || false;
          }
          applyLoadedState();
          applyTrophyState();
          // Check browser visits after loading saved data
          checkBrowserVisits();
        }
      });
      // Problem: The checkBrowserVisits function was only being called if the game had started.
      // This could lead to a situation where browser-specific achievements weren't being properly
      // recognized if the game hadn't officially "started" yet.
      //
      // Solution: We now call checkBrowserVisits unconditionally after loading the game state.
      // This ensures that all browser-specific achievements are properly recognized and applied,
      // regardless of whether the game has officially started or not.
      setInterval(() => {
        if (GG_ALL_GAME_CONFIG.gameStarted) {
          saveGame({
            clickCount: GG_ALL_GAME_CONFIG.clickCount,
            normalVisitAchieved: GG_ALL_GAME_CONFIG.normalVisitAchieved,
            mobileVisitAchieved: GG_ALL_GAME_CONFIG.mobileVisitAchieved,
            firefoxVisitAchieved: GG_ALL_GAME_CONFIG.firefoxVisitAchieved,
            edgeVisitAchieved: GG_ALL_GAME_CONFIG.edgeVisitAchieved
          });
        }
      }, 3000);

      function saveGame(gameState) {
        const saveGameEvent = {
          type: 'REQUEST_SAVE_GAME_EVENT',
          save_data: gameState
        };
        window.parent.postMessage(saveGameEvent, '*');
        // Additional fix: Add a small delay before allowing the next save
        // This helps prevent rapid successive saves that could lead to data loss
        saveGame = debounce(saveGame, 300);
      }
      // Debounce function to limit the rate at which saveGame can be called
      function debounce(func, wait) {
        let timeout;
        return function executedFunction(...args) {
          const later = () => {
            clearTimeout(timeout);
            func(...args);
          };
          clearTimeout(timeout);
          timeout = setTimeout(later, wait);
        };
      }
      updateShape();
      updateProgressBar();
      loadGame();
      initializeTrophyDescriptions();
      // Remove these event listeners as they are no longer needed
      // The checkMobileVisit function is now called in the startGame function
    })();
  </script>