${l.name.replace('The ','').replace(' Layer','')}0/${l.total}`;
tabNav.appendChild(t);
});// Score tab
const st = document.createElement('button');
st.className = 'tab';
st.id = 'caftab-score';
st.setAttribute('onclick', 'cafSwitchTab("score")');
st.innerHTML = `Score0/45`;
tabNav.appendChild(st);// Build layer pages
LAYERS.forEach(l => {
const pg = document.createElement('div');
pg.className = 'layer-page' + (l.id===1?' active':'');
pg.id = `cafpage-${l.id}`;const priClass = {High:'pri-high', Medium:'pri-med', Low:'pri-low'};let signals = l.signals.map(s => `
`).join('');pg.innerHTML = `
${l.remember ? `` : ''}
${signals}
${l.closing ? `
${String(s.n).padStart(2,'0')}
${s.t}
${s.p}
If failing
${s.f}
${l.id}
${l.name}
${l.tagline}
0 / ${l.total}
Remember
${l.remember}
#
Signal
Priority
Pass
${l.closing}
` : ''}`;layerPages.appendChild(pg);// Build bottom tab nav (mirror of top tabs)
const bottomNav = pg.querySelector(`#cafBottomNav-${l.id}`);
if(bottomNav){
LAYERS.forEach(bl => {
const bt = document.createElement('button');
bt.className = 'tab' + (bl.id===l.id?' active':'');
bt.id = `cafbtab-${l.id}-${bl.id}`;
bt.setAttribute('onclick', `cafSwitchTab(${bl.id})`);
bt.innerHTML = `${bl.id}
${bl.name.replace('The ','').replace(' Layer','')}0/${bl.total}`;
bottomNav.appendChild(bt);
});
const bst = document.createElement('button');
bst.className = 'tab';
bst.setAttribute('onclick', 'cafSwitchTab("score")');
bst.innerHTML = `Score0/45`;
bottomNav.appendChild(bst);
}
});// Build score page layer rows
const lsr = document.getElementById('cafLayerScoreRows');
LAYERS.forEach((l,i) => {
const row = document.createElement('div');
row.className = 'ls-row';
row.innerHTML = `${l.name.replace('The ','')}
0/${l.total}
`;
lsr.appendChild(row);
});cafBuildSignalMap();
}function cafBuildSignalMap(){
const sm = document.getElementById('cafSignalMap');
sm.innerHTML = '';
LAYERS.forEach((l) => {
const lbl = document.createElement('div');
lbl.className = 'ssl-dot layer-label';
lbl.textContent = 'L'+l.id;
sm.appendChild(lbl);
l.signals.forEach(s => {
const d = document.createElement('div');
d.className = 'ssl-dot' + (cafChecks[s.n] ? ' passed' : '');
d.id = `cafsm-${s.n}`;
d.textContent = String(s.n).padStart(2,'0');
sm.appendChild(d);
});
});
}window.cafSwitchTab = function(id){// REMOVE ACTIVE FROM ALL PAGES
document.querySelectorAll('.layer-page').forEach(page=>{
page.classList.remove('active');
});// REMOVE ACTIVE FROM ALL TOP TABS
document.querySelectorAll('#cafTabNav .tab').forEach(tab=>{
tab.classList.remove('active');
});// REMOVE ACTIVE FROM ALL BOTTOM TABS
document.querySelectorAll('.bottom-tabs .tab').forEach(tab=>{
tab.classList.remove('active');
});// IF SCORE TAB
if(id === 'score'){// SHOW SCORE PAGE
document.getElementById('cafPageScore').classList.add('active');// ACTIVATE TOP SCORE TAB
document.getElementById('caftab-score').classList.add('active');// ACTIVATE ALL BOTTOM SCORE TABS
document.querySelectorAll('.bottom-tabs').forEach(nav=>{
const lastTab = nav.lastElementChild;
if(lastTab){
lastTab.classList.add('active');
}
});// UPDATE SCORE PAGE
cafUpdateScorePage();} else {// SHOW SELECTED PAGE
document.getElementById(`cafpage-${id}`).classList.add('active');// ACTIVATE TOP TAB
document.getElementById(`caftab-${id}`).classList.add('active');// ACTIVATE ALL MATCHING BOTTOM TABS
document.querySelectorAll(`[id^="cafbtab-"][id$="-${id}"]`).forEach(tab=>{
tab.classList.add('active');
});
}// SAVE ACTIVE TAB
cafActiveTab = id;
};window.cafToggle = function(num, cb){
cafChecks[num] = cb.checked;
const mainRow = document.getElementById(`cafmain-${num}`);
const sigText = document.getElementById(`cafst-${num}`);
const row = document.getElementById(`cafrow-${num}`);if(cb.checked){
mainRow.classList.add('checked');
sigText.classList.add('checked');
row.classList.add('passed');
} else {
mainRow.classList.remove('checked');
sigText.classList.remove('checked');
row.classList.remove('passed');
}
cafUpdateScores();
};function cafUpdateScores(){let total = 0;LAYERS.forEach(l => {let layerScore = 0;l.signals.forEach(s => {
if(cafChecks[s.n]) layerScore++;
});total += layerScore;const pct = Math.round((layerScore / l.total) * 100);// TOP PROGRESS
const lp = document.getElementById(`caflp-${l.id}`);
const lpv = document.getElementById(`caflpv-${l.id}`);// TOP TAB COUNT
const tc = document.getElementById(`caftc-${l.id}`);// SCORE PAGE BARS
const lsb = document.getElementById(`caflsb-${l.id}`);
const lsv = document.getElementById(`caflsv-${l.id}`);if(lp) lp.style.width = pct + '%';if(lpv){
lpv.textContent = `${layerScore} / ${l.total}`;
}// UPDATE TOP TAB COUNT
if(tc){
tc.textContent = `${layerScore}/${l.total}`;
}// UPDATE ALL BOTTOM TAB COUNTS
document.querySelectorAll(`[id^="cafbtc-"][id$="-${l.id}"]`).forEach(el=>{
el.textContent = `${layerScore}/${l.total}`;
});// SCORE PAGE
if(lsb){
lsb.style.width = pct + '%';
}if(lsv){
lsv.textContent = `${layerScore}/${l.total}`;
}});// GLOBAL SCORE
const totalPct = Math.round((total / 45) * 100);document.getElementById('globalFill').style.width = totalPct + '%';document.getElementById('globalScore').textContent = `${total} / 45`;// TOP SCORE TAB
document.getElementById('caftc-score').textContent = `${total}/45`;// BOTTOM SCORE TABS
document.querySelectorAll('.bottom-tabs').forEach(nav=>{const scoreTab = nav.lastElementChild;if(scoreTab){const count = scoreTab.querySelector('.tab-count');if(count){
count.textContent = `${total}/45`;
}
}});// TIER
const tier = TIERS.find(t => total >= t.min) || TIERS[TIERS.length - 1];const tb = document.getElementById('globalTier');tb.textContent = tier.label;tb.className = 'tier-badge' + (tier.label === 'Citation-Ready' ? ' ready' : '');// SIGNAL MAP
LAYERS.forEach(l => {l.signals.forEach(s => {const d = document.getElementById(`cafsm-${s.n}`);if(d){
d.className = 'ssl-dot' + (cafChecks[s.n] ? ' passed' : '');
}});});// UPDATE SCORE PAGE
if(cafActiveTab === 'score'){
cafUpdateScorePage();
}}function cafUpdateScorePage(){
let total = 0;
LAYERS.forEach(l => l.signals.forEach(s => { if(cafChecks[s.n]) total++; }));const tier = TIERS.find(t => total >= t.min) || TIERS[TIERS.length-1];
const pct = total/45;
const circ = 201.1;document.getElementById('cafScoreBig').textContent = total;
document.getElementById('cafScoreTierLabel').textContent = tier.label;
document.getElementById('cafScoreTierLabel').style.color = tier.color;
document.getElementById('cafScoreHeadline').textContent = tier.head;
document.getElementById('cafScoreSub').textContent = tier.sub;const rf = document.getElementById('cafRingFg');
rf.style.strokeDashoffset = circ - (pct * circ);
rf.style.stroke = tier.ring;['caftier1','caftier2','caftier3','caftier4'].forEach(id => {
document.getElementById(id).classList.remove('active-tier');
});
if(total>=38) document.getElementById('caftier4').classList.add('active-tier');
else if(total>=28) document.getElementById('caftier3').classList.add('active-tier');
else if(total>=18) document.getElementById('caftier2').classList.add('active-tier');
else document.getElementById('caftier1').classList.add('active-tier');
}window.cafToggleHow = function(){
const b = document.getElementById('howBody');
const t = document.getElementById('howToggle');
const ch = t.querySelector('.chevron');
b.classList.toggle('open');
t.classList.toggle('open');
if(ch) ch.style.transform = b.classList.contains('open') ? 'rotate(180deg)' : 'rotate(0deg)';
};window.cafResetAll = function(){
LAYERS.forEach(l => l.signals.forEach(s => {
cafChecks[s.n] = false;
const cb = document.getElementById(`cafcb-${s.n}`);
if(cb) cb.checked = false;
document.getElementById(`cafmain-${s.n}`)?.classList.remove('checked');
document.getElementById(`cafst-${s.n}`)?.classList.remove('checked');
document.getElementById(`cafrow-${s.n}`)?.classList.remove('passed');
}));
cafUpdateScores();
document.getElementById('globalTier').textContent = 'Not scored';
document.getElementById('globalTier').className = 'tier-badge';
document.getElementById('cafScoreTierLabel').textContent = 'Complete all layers to see your tier';
document.getElementById('cafScoreTierLabel').style.color = 'var(--text-3)';
document.getElementById('cafScoreHeadline').textContent = 'Answer questions above to generate your score';
document.getElementById('cafScoreSub').textContent = 'Work through each layer tab and check every signal that fully passes.';
document.getElementById('cafScoreBig').textContent = '0';
const rf = document.getElementById('cafRingFg');
rf.style.strokeDashoffset = '201.1';
rf.style.stroke = '#1D9E75';
['caftier1','caftier2','caftier3','caftier4'].forEach(id => document.getElementById(id).classList.remove('active-tier'));
};cafBuildUI();})();

