import {settingsFrom,accentInk} from './hub-model.js';
const root=document.documentElement;
const media=matchMedia('(prefers-color-scheme: light)');
function preferences(){
let raw={};try{raw=JSON.parse(localStorage.getItem('zs.settings')||'{}');}catch{}
const settings=settingsFrom(raw);
root.dataset.theme=settings.theme==='system'?(media.matches?'light':'dark'):settings.theme;
root.dataset.accent=settings.accent;root.dataset.motion=String(settings.motion);
root.dataset.density=settings.density;
const vars={backgroundHex:'--hub-bg',panelHex:'--hub-panel',textHex:'--hub-text',borderHex:'--hub-hairline',scrollbarHex:'--scroll-thumb'};
for(const [key,name] of Object.entries(vars))if(settings[key])root.style.setProperty(name,settings[key]);else root.style.removeProperty(name);
if(settings.panelHex||settings.backgroundHex){root.style.setProperty('--hub-raised','color-mix(in srgb,var(--hub-panel) 80%,var(--hub-text) 4%)');root.style.setProperty('--hub-input','color-mix(in srgb,var(--hub-bg) 70%,var(--hub-panel))');}
else{root.style.removeProperty('--hub-raised');root.style.removeProperty('--hub-input');}
if(settings.textHex)root.style.setProperty('--hub-muted','color-mix(in srgb,var(--hub-text) 68%,var(--hub-panel))');else root.style.removeProperty('--hub-muted');
if(settings.accentHex){
root.style.setProperty('--hub-accent',settings.accentHex);
root.style.setProperty('--hub-accent-ink',accentInk(settings.accentHex));
root.style.setProperty('--hub-soft',settings.accentHex+'18');
}else for(const key of ['--hub-accent','--hub-accent-ink','--hub-soft'])root.style.removeProperty(key);
root.style.setProperty('--ui-radius',settings.radius+'px');root.style.setProperty('--scroll-size',settings.scrollbarSize+'px');
root.style.zoom=String(settings.textScale/100);
let sheet=document.getElementById('user-design-css');if(!sheet){sheet=document.createElement('style');sheet.id='user-design-css';document.head.append(sheet);}sheet.textContent=settings.customCss;
}
preferences();media.addEventListener('change',preferences);
addEventListener('storage',event=>{if(event.key==='zs.settings'||event.key===null)preferences();});
const main=document.querySelector('main');
if(main){if(!main.id)main.id='main-content';const skip=document.createElement('a');skip.className='skip-link';skip.href='#'+main.id;skip.textContent='Skip to content';document.body.prepend(skip);main.tabIndex=-1;}
const sections=[...document.querySelectorAll('[data-profile-section]')];
const tabs=[...document.querySelectorAll('[data-profile-tab]')];
function profileSection(name,focus=false){
for(const tab of tabs){const active=tab.dataset.profileTab===name;tab.setAttribute('aria-selected',String(active));tab.tabIndex=active?0:-1;if(active&&focus)tab.focus();}
for(const section of sections)section.hidden=section.dataset.profileSection!==name;
}
if(tabs.length){
profileSection('basics');
tabs.forEach((tab,index)=>{
tab.addEventListener('click',()=>profileSection(tab.dataset.profileTab));
tab.addEventListener('keydown',event=>{
if(!['ArrowLeft','ArrowRight','Home','End'].includes(event.key))return;
event.preventDefault();const next=event.key==='Home'?0:event.key==='End'?tabs.length-1:(index+(event.key==='ArrowRight'?1:-1)+tabs.length)%tabs.length;
profileSection(tabs[next].dataset.profileTab,true);
});
});
// Native validation must reveal invalid inputs before the browser tries to focus them.
document.getElementById('profile-form').addEventListener('invalid',event=>{
const section=event.target.closest('[data-profile-section]');if(section)profileSection(section.dataset.profileSection);
},true);
}
addEventListener('zs:prefs-updated',preferences);
// Every non-launcher page uses the same shell. Page scripts retain their IDs and forms.
const page=document.body.dataset.sitePage;
if(page&&main&&!document.querySelector('.unified-shell')){
const shell=document.createElement('div');shell.className='unified-shell';
const aside=document.createElement('aside');aside.className='unified-sidebar';aside.setAttribute('aria-label','Main navigation');
const nav=document.createElement('nav');nav.setAttribute('aria-label','Browse stuff');
const symbols={home:'',search:'',game:'',star:'',friends:'',account:''};
const active=['friends','people'].includes(page)||page==='profile'&&location.pathname.startsWith('/users/')?'Friends':['account','profile','library','requests','signin'].includes(page)?'Account':null;
for(const [name,url,icon] of [['Home','/','home'],['Discover','/?section=browse','search'],['Games','/?section=games','game'],['Favorites','/?section=favorites','star'],['Friends','/friends','friends'],['Account','/account','account']]){
const a=document.createElement('a');a.href=url;const mark=document.createElement('span');mark.innerHTML='';a.append(mark,document.createTextNode(name));if(name===active)a.setAttribute('aria-current','page');nav.append(a);
}
aside.append(nav);main.before(shell);shell.append(aside,main);for(const footer of document.querySelectorAll('body>footer'))main.append(footer);
document.querySelectorAll('.unified-header .site-nav a').forEach(a=>{if(a.getAttribute('href')==='/account'&&active==='Account')a.setAttribute('aria-current','page');});
if(!main.querySelector('h1')){const h=document.createElement('h1');h.className='unified-page-title';h.textContent=page==='profile'?'Profile':page.replaceAll('-',' ');main.prepend(h);}
}