/* ---------- puzzles on a phone: fixed overlays that never scroll ----------
A bar pinned at the top says how the puzzle stands (its name, the three stars, the money, undo and the menu), the one
thing to do next and what each flow gets, as chips. A city or a link opens a card at the bottom whose content is
split into tabs, so each tab fits on the screen. Desktop keeps the side card (pzRender, pzPanel). */
// A rate in a few characters, for the chips: 30G, 2.5G, 1.2T.
const pzG=g=>g>=1000?(g/1000).toFixed(g>=10000?0:1)+'T':g>=10||g===0?Math.round(g)+'G':g.toFixed(1)+'G';
// One flow as a chip: what it is, how much of it arrives, a circuit's latency against its cap. Tapping it shows the city.
function pzFlowChip(x){const f=x.f,ok=x.full&&x.fast,part=x.g>0&&!ok;
const lat=f.ms?` ยท ${x.ms==null?'โ':x.ms.toFixed(1)}/${f.ms} ms`:'';
return ``;}
// The rules of this puzzle as chips: the content's home, the cities to avoid, no subsea cables, the ring.
function pzRuleChips(e){const P=S.pz;let h='';
if(P.src.length)h+=`๐๏ธ ${P.src.map(c=>esc(CITY[c].name)).join(', ')}`;
for(const t of e.tw)h+=t.k==='avoid'?`๐ซ ${P.avoid.map(c=>esc(CITY[c].name)).join(', ')}`:`${t.ok?'โ':'โ'} ${pzTwistTxt(t)}`;
return h;}
function pzDock(el,e){const P=S.pz,d=pzDef(P.id),gs=pzGoals(e),cur=gs.find(g=>g.cur);UI.pzMark=!UI.pzMk&&cur&&cur.mark||null;
const stars=gs.map((g,i)=>`${g.done?'โญ':i+1}`).join('');
const bud=P.budget?`${fmt$(e.spend)} / ${fmt$(P.budget)}`:`${tr('puzzles.free')}`;
let h=`
`;
// The one thing to do: the goal being worked on, what is wrong and Show me; the monkey once the traffic arrives.
const act=UI.pzMk?btn('โฉ '+tr('puzzles.skip_the_monkey'),'pzMkSkip',{},'sm'):e.goal?btn('๐ '+tr('puzzles.let_the_chaos_monkey_loose'),'pzMonkey',{},'ghost sm'):'';
if(cur)h+=`
`;
if(h!==UI._pzh){UI._pzh=h;morph(el,h);}}
// The bar's height, for what must sit under it (toasts) or stop short of it (the card): it changes with its content and
// when a card opens, so it is watched rather than measured after each render.
try{new ResizeObserver(()=>{const H=$('pzBox').offsetHeight||0;document.documentElement.style.setProperty('--pzdock',H+'px');}).observe($('pzBox'));}catch(e){}
// An architect run on a phone: the same bar. The brief, its sites and rules as chips, and the review with its one button.
function arDock(el){const A=S.ar,run=arRun(),b=arBrief(),e=arEval(),sh=UI.arShow,f=UI.arFail,n=run.briefs.length,nm=c=>esc(CITY[c].name),T=e.tubes;
let h=`
`;
let ch=e.sites.map(s=>`${s.hq?'๐ ':s.gold?'๐จ':s.ok?'โ':'โ'} ${nm(s.c)}${s.hq?'':` ${s.ok&&!s.full?arG(s.got)+'/':''}${arG(s.dem)}`}`).join('');
for(const r of e.rings)ch+=`๐ ${r.cs.map(nm).join(' ยท ')}`;
if(e.lat)ch+=`โฑ ${e.lat.ms==null?'โ':e.lat.ms.toFixed(1)}/${e.lat.cap} ms`;
for(const c of S.pz.avoid)ch+=`๐ซ ${nm(c)}`;
if(T.max!=null)ch+=`${tr('architect.tubes_this_brief')} ${T.u}/${T.max}`;
h+=`
`;
if(h!==UI._pzh){UI._pzh=h;morph(el,h);}}
// The tabs of a card; the one shown is kept per kind of card (city or link) while the puzzle runs.
const pzTabs=(kind,tabs,cur)=>``;
const pzTabOf=(kind,tabs,def)=>{const t=(UI.pzTab||{})[kind];return tabs.some(([k])=>k===t)?t:def;};
// The card's head in two short lines: what it is (and whether it is yours) above the name, the facts beside the name.
const pzHead=(eb,name,facts)=>`
${eb}
${name}${facts?` ${facts}`:''}
`;
function pzPanelPhone(sel){
if(sel.type==='city'){const c=sel.id,k=pzKind(c),p=S.pops[c],C=CITY[c],P=S.pz;if(!k)return '';
const eb=k==='goal'?tr('puzzles.goal_city'):k==='via'?tr('puzzles.stepping_stone_optional'):k==='off'?tr('puzzles.not_in_the_plan'):tr('puzzles.off_limits');
let h=pzHead(`${PZ_ICON[k]} ${eb}${p?` ยท ${tr('common.your_pop')}`:''}`,esc(C.name),'')+'
`;
for(const x of nb){const ls=pairLinks(c,x),ld=ls.reduce((a,l)=>a+(R.load[l.id]||0),0),cp=ls.filter(l=>!linkDown(l)).reduce((a,l)=>a+capOf(l),0);
h+=`
`;}
else if(t==='traffic'){const rec=R.ef[edgeKey(l)],rows=[];
if(rec){S.pz.dem.forEach((f,i)=>{const v=rec.eg['l2:'+pzFlowId(i)];if(v>0.01)rows.push([pzFlowName(f),v]);});
const byc={};for(const k in rec.dst)byc[k]=rec.dst[k];S.pz.dem.forEach((f,i)=>{const v=rec.eg['l2:'+pzFlowId(i)];if(v>0.01)byc[f.b]=(byc[f.b]||0)-v;});
for(const k in byc)if(byc[k]>0.01)rows.push([tr('puzzles.flow_eyeballs_in_city',{city:CITY[k].name}),byc[k]]);}
h+=rows.length?rows.map(([n,v])=>`
';}
// A phone turned or a window resized can switch between the two layouts: draw the puzzle again in the one that fits.
addEventListener('resize',()=>{if(!S||!pzOn())return;const ph=isPhone();if(ph===UI._pzPhone)return;UI._pzh=null;UI._ph=null;pzRender();renderPanel();});