el.classList.toggle('on',i===a));Field.show(a<0?heroSpec:specFor(a));}
if(a>=0){const r=items[a].getBoundingClientRect();Field.beam(r.top+r.height/2);}else Field.beam(vh*.82);
}
items.forEach((el,i)=>{on(el,'pointerenter',()=>{hover=i;dot.classList.add('big');upd();});on(el,'pointerleave',()=>{hover=-1;dot.classList.remove('big');upd();});});
on(window,'scroll',upd,{passive:true});
Field.setFade(0);Field.S.lensT=1;
upd();
}
function renderPost(p){
const i=POSTS.indexOf(p), next=POSTS[i+1]||POSTS[0];
document.title=p.title+' — nate.rip';
document.getElementById('ctr').textContent=`${pad(i+1)} / ${pad(POSTS.length)}`;
app.innerHTML=`
${esc(p.title)}
${KL[p.k]}${dfmt(p.date)}${p.s.words} words${Math.max(1,Math.round(p.s.words/230))} min${p.s.codeLines?`${p.s.codeLines} lines of code`:''}${p.s.imgs?`${p.s.imgs} images`:''}
${p.html}
Next → ${pad(POSTS.indexOf(next)+1)}${esc(next.title)}
`;
const body=document.getElementById('body');
build(body,p);
const box=narrow()?[.05,.12,.9,.6]:p.title.length<12?[.04,.12,.92,.68]:[.04,.1,.92,.72];
Field.show({text:p.title,box,align:p.title.length<12?'center':'left'});
Field.S.lensT=1;
const upd=()=>{const k=Math.min(1,scrollY/(innerHeight*.8));Field.setFade(k);Field.beam(innerHeight*(.86-k*.0));Field.S.lensT=1-k*.7;
// marquee bands
body.querySelectorAll('.band .row').forEach((r,j)=>{const b=r.parentNode.getBoundingClientRect();const x=(b.top-innerHeight)*(j%2?.6:-.6);r.style.transform=`translateX(${x-300}px)`;});
};
on(window,'scroll',upd,{passive:true});upd();
observe(body);
}
function build(body,p){
const h1=body.querySelector('h1');if(h1&&h1.textContent.trim().toLowerCase()===p.title.toLowerCase())h1.remove();
body.querySelectorAll('a[href^="http"]').forEach(a=>{a.target='_blank';a.rel='noopener'});
// code slabs
let c=0;
body.querySelectorAll('pre').forEach(pre=>{
c++;const txt=pre.textContent,ln=pre.querySelectorAll('.line');
const lang=/#\[|fn |let |impl |struct |::|pub /.test(txt)?'Rust':/^\s*(cargo|spool|npm|git)\b/m.test(txt)?'Shell':/display\s*:|grid-/.test(txt)?'CSS':/type |interface /.test(txt)?'TypeScript':'Text';
ln.forEach((l,j)=>l.style.setProperty('--l',j));
const s=document.createElement('figure');s.className='slab';s.innerHTML=`${pad(c)} — ${lang}${ln.length} ln
`;
pre.replaceWith(s);s.appendChild(pre);
});
// images
let m=0;
body.querySelectorAll('img').forEach(img=>{
m++;const host=img.closest('p')||img;const f=document.createElement('figure');f.className='blind';img.loading='lazy';
f.appendChild(img);const cap=document.createElement('figcaption');cap.innerHTML=`${esc(img.alt||'')}Fig. ${pad(m)}`;f.appendChild(cap);host.replaceWith(f);
});
// prose: headings become scrolling bands, parentheticals get marked
if(p.k==='prose'){
body.querySelectorAll('h2').forEach((h,j)=>{
const t=esc(h.textContent.replace(/\.$/,''));const band=document.createElement('div');band.className='band'+(j%2?' alt':'');
band.setAttribute('role','heading');band.setAttribute('aria-level','2');
band.innerHTML=`${Array(6).fill(t).join('✶')}✶
`;h.replaceWith(band);
});
body.querySelectorAll('p').forEach(pp=>{
if(pp.querySelector('a,code'))return;
pp.innerHTML=pp.innerHTML.replace(/\(([^()]{12,})\)/g,'($1)');
});
}
// link-list note becomes a link roll
if(p.k==='note'&&body.querySelectorAll('p > a > strong').length>=4){
const roll=document.createElement('div');roll.className='roll';
const kids=[...body.children];let started=false;const keep=[];let lastLn=null;
kids.forEach(el=>{
if(el.tagName==='H2'||el.tagName==='H3'){started=true;lastLn=null;const g=document.createElement('div');g.className='grp';g.textContent=el.textContent;roll.appendChild(g);return;}
if(!started){keep.push(el);return;}
if(el.tagName==='HR'){started=false;keep.push(roll,el);return;}
if(el.tagName==='P'&&el.querySelector('a > strong')){
const main=[...el.querySelectorAll('a')].filter(a=>a.querySelector('strong'));
const by=el.textContent.split(/\s[-–]\s/).slice(1).join(' ').trim();
main.forEach(a=>{const l=document.createElement('a');l.className='ln';l.href=a.href;l.target='_blank';l.rel='noopener';l.innerHTML=`${esc(a.textContent)}${esc(by)}`;roll.appendChild(l);lastLn=l;});
return;
}
if(el.tagName==='P'&&lastLn){const s=document.createElement('span');s.className='desc';s.textContent=el.textContent;lastLn.appendChild(s);lastLn=null;return;}
if(el.tagName==='P'){el.className='desc';roll.appendChild(el);return;}
roll.appendChild(el);
});
if(!keep.includes(roll))keep.push(roll);
body.innerHTML='';keep.forEach(e=>body.appendChild(e));
}
}
let io;
function observe(body){
io&&io.disconnect();
io=new IntersectionObserver(es=>es.forEach(e=>{if(e.isIntersecting){e.target.classList.add('in');io.unobserve(e.target);}}),{rootMargin:'0px 0px -12% 0px'});
body.querySelectorAll('h2,.slab,.blind').forEach(el=>io.observe(el));
}
function route(){
cleanup.forEach(f=>f());cleanup=[];
const slug=decodeURIComponent(location.hash.slice(1));
const p=POSTS.find(x=>x.slug===slug);
scrollTo(0,0);
p?renderPost(p):renderIndex();
}
Promise.all([fetch('../content/posts.json').then(r=>r.json()),document.fonts.load('900 100px "Anybody Variable"').catch(()=>{})]).then(([list])=>{
POSTS=list.map(p=>{p.s=stats(p);p.k=ART[p.slug]||(p.s.imgs>=4?'image':p.s.codeLines>=30?'code':p.s.words<320?'note':'prose');return p;});
addEventListener('hashchange',route);
route();
});