const game = document.getElementById('carbono-game-frame'); const guide = document.getElementById('carbono-rules-frame'); const playPanel = document.getElementById('carbono-jogar'); const rulesPanel = document.getElementById('carbono-regras'); const origin = window.location.origin; const pauseGame = () => game?.contentWindow?.postMessage({ type: 'super-carbono:pause' }, origin); const measureGuide = () => guide?.contentWindow?.postMessage({ type: 'super-carbono:measure' }, origin); document.querySelectorAll('[data-carbono-open]').forEach(link => { link.addEventListener('click', () => { const panel = document.getElementById(link.dataset.carbonoOpen); if (panel) panel.open = true; }); }); playPanel?.addEventListener('toggle', () => { if (!playPanel.open) pauseGame(); }); rulesPanel?.addEventListener('toggle', () => { if (rulesPanel.open) { pauseGame(); measureGuide(); } }); guide?.addEventListener('load', measureGuide); window.addEventListener('message', event => { if (event.origin !== origin || event.source !== guide?.contentWindow || event.data?.type !== 'super-carbono:resize') return; const height = Math.ceil(event.data.height); if (Number.isFinite(height) && height >= 400 && height <= 50000) guide.style.height = `${height}px`; }); if (game && 'IntersectionObserver' in window) { new IntersectionObserver(entries => { if (!entries[0].isIntersecting) pauseGame(); }, { threshold: 0 }).observe(game); } document.getElementById('carbono-fullscreen')?.addEventListener('click', async () => { const status = document.getElementById('carbono-fullscreen-status'); try { if (!game?.requestFullscreen) throw new Error('unsupported'); await game.requestFullscreen(); status.hidden = true; game.focus(); } catch { status.textContent = 'Este navegador não abriu a tela cheia. Use “Abrir jogo em outra aba” para jogar com mais espaço.'; status.hidden = false; } }); // Links diretos para o jogo ou as regras também abrem o painel correspondente. const openHash = () => { if (location.hash === '#carbono-jogar' && playPanel) playPanel.open = true; if (location.hash === '#carbono-regras' && rulesPanel) rulesPanel.open = true; }; window.addEventListener('hashchange', openHash); openHash();