/* ================= INSTALL (PWA) ================= */
let installEvt=null;
addEventListener('beforeinstallprompt',e=>{e.preventDefault();installEvt=e;});
addEventListener('appinstalled',()=>{installEvt=null;track('pwa_installed',true);if(UI.pendGame){const p=UI.pendGame;UI.pendGame=null;UI.pitched=true;beginGame(...p);}toast(tr('install.installed_open_series_of_tubes_from'),'good');});
const isStandalone=()=>{try{return navigator.standalone===true||!!(window.matchMedia&&matchMedia('(display-mode: standalone)').matches);}catch(e){return false;}};
const isIOS=()=>/iPad|iPhone|iPod/.test(navigator.userAgent)||(navigator.platform==='MacIntel'&&navigator.maxTouchPoints>1);
function installRow(){if(isStandalone())return '';
return `
📲 ${tr('install.best_played_as_an_app')}
${tr('install.full_screen_works_offline_one_tap')}${isIOS()?' '+tr('install.install_it_before_you_start_a'):''}
${btn(tr('common.install'),'install',{},'pink sm')}
`;}
/* Phones get a real pitch before a new game: an installed app is full screen, works offline, and on iPhone
keeps its save away from Safari's clean-ups. Storage is separate on iOS, so installing first matters. */
const SHARE_SVG=' ';
const needPitch=()=>isPhone()&&!isStandalone();
function installSteps(){
if(isIOS())return `${tr('install.tap_share_icon_in_the_browser',{icon:SHARE_SVG})} ${tr('install.tap_add_to_home_screen_you')} ${tr('install.open_series_of_tubes_from_your')} `;
if(installEvt)return '';
return `${tr('install.open_your_browser_menu')} ${tr('install.tap_install_app_or_add_to')} ${tr('install.open_series_of_tubes_from_your_e03f')} `;}
function pitchHtml(starting){const ios=isIOS();
return `${starting?`
${tr('install.before_you_start')}
`:''}
📲 ${tr('install.play_it_as_an_app')} ×
${tr('install.series_of_tubes_is_made_for')}
🗺️ ${tr('common.full_screen')} ${tr('install.no_browser_bars_a_lot_more')} 📶 ${tr('common.works_offline')} ${tr('install.build_your_backbone_on_the_train')} ${ios?`💾 ${tr('install.your_save_stays_safe')} ${tr('install.safari_can_clear_the_data_of')} ⚠️ ${tr('common.install_first')} ${tr('install.on_iphone_a_game_started_in')} `:`⚡ ${tr('install.one_tap_away')} ${tr('install.it_opens_straight_into_your_network')} `}
${installSteps()}
${installEvt?btn(tr('install.install_the_app'),'install',{},'pink big'):''}${starting?btn(tr('install.play_in_the_browser_anyway'),'pitchSkip',{},installEvt?'ghost':'ghost big'):btn(tr('common.got_it'),'closeModal',{},installEvt?'ghost':'pink')}
`;}
if('serviceWorker' in navigator&&location.protocol==='https:'&&!IS_DEV)addEventListener('load',()=>{navigator.serviceWorker.register('/sw.js').catch(()=>{});});