';
b.querySelector('.n').textContent=p.badge;
b.querySelector('b').textContent=p.title; b.querySelector('small').textContent=p.hint;
b.onclick=function(){show(i); closeDrawer();}; // 选完自动收起,把整屏还给演示
nav.appendChild(b); buttons.push(b);
});
function setSec(sec){
tabs.forEach(function(t){t.setAttribute('aria-selected', String(t.getAttribute('data-sec')===sec));});
Array.prototype.forEach.call(nav.querySelectorAll('[data-sec]'),function(el){el.hidden = el.getAttribute('data-sec')!==sec;});
}
tabs.forEach(function(t){ t.onclick=function(){ setSec(t.getAttribute('data-sec')); }; });
/* 抽屉:默认收起,把手在左边缘中间;打开时自动切到当前页所在的那一块 */
function openDrawer(){
if(cur>=0) setSec(pages[cur].sec);
drawer.classList.add('open'); scrim.classList.add('on'); edge.classList.add('away');
drawer.setAttribute('aria-hidden','false'); menuBtn.setAttribute('aria-expanded','true');
var act=buttons[cur]; if(act&&act.scrollIntoView)act.scrollIntoView({block:'nearest'});
setTimeout(function(){try{(act||buttons[0]).focus();}catch(e){}},260);
}
function closeDrawer(){
drawer.classList.remove('open'); scrim.classList.remove('on'); edge.classList.remove('away');
drawer.setAttribute('aria-hidden','true'); menuBtn.setAttribute('aria-expanded','false');
}
function isOpen(){return drawer.classList.contains('open');}
menuBtn.onclick=function(){isOpen()?closeDrawer():openDrawer();};
document.getElementById('close').onclick=function(){closeDrawer(); menuBtn.focus();};
scrim.onclick=closeDrawer;
function inSec(sec){ var a=[]; pages.forEach(function(p,i){ if(p.sec===sec) a.push(i); }); return a; }
function show(i){
if(i<0||i>=pages.length||i===cur) return; cur=i;
var p=pages[i];
buttons.forEach(function(b,k){b.setAttribute('aria-current',k===i?'true':'false');});
frame.srcdoc=p.src;
try{location.hash='#'+p.key;}catch(e){}
document.title=(p.sec==='anim'?p.no+'. ':'')+p.title+' · 教学动画';
if(window.__annot){
window.__annot.setPage(i); // 每道题的笔迹各存各的
window.__annot.setVisible(p.sec==='anim'); // 功能区不放画笔
}
var many=inSec(p.sec).length>1; // 这一块只有一项时,上一题/下一题没意义
prevBtn.disabled=nextBtn.disabled=!many;
frame.addEventListener('load',function once(){frame.removeEventListener('load',once);
if(!isOpen()){try{frame.contentWindow.focus();}catch(e){}}});
}
/* 上一题 / 下一题只在当前这一块里翻:不会从第 11 题翻进口算 */
function step(d){
if(cur<0) return;
var list=inSec(pages[cur].sec), k=list.indexOf(cur);
if(list.length<2) return;
show(list[(k+d+list.length)%list.length]);
}
prevBtn.onclick=function(){step(-1);};
nextBtn.onclick=function(){step(1);};
document.getElementById('fs').onclick=function(){
var d=document; if(d.fullscreenElement){d.exitFullscreen();} else {d.documentElement.requestFullscreen&&d.documentElement.requestFullscreen();}
};
window.addEventListener('message',function(e){
if(e.data&&e.data.nav==='PageDown')step(1); else if(e.data&&e.data.nav==='PageUp')step(-1);
});
document.addEventListener('keydown',function(e){
if(e.key==='PageDown'){e.preventDefault();step(1);}
else if(e.key==='PageUp'){e.preventDefault();step(-1);}
else if(e.key==='Escape'&&isOpen()){e.preventDefault();closeDrawer();menuBtn.focus();}
});
var h=decodeURIComponent((location.hash||'').slice(1)), start=0;
pages.forEach(function(p,i){ if(p.key===h) start=i; });
setSec(pages[start].sec);
show(start);
})();