' + (nxt <= PLAN.maxL ? 'Buying level ' + nxt + ' (' + $$(price(nxt)) + ') ' + where + how + skipped + '.' : 'At the top of the ladder.') + '
'
+ (nxt <= PLAN.maxL ? '' : '')
+ '
Directs: ' + n.directs + '
'
+ [0, 1, 2].filter(k => k !== n.directs).map(k => '').join('');
pop.hidden = false;
const w = 250, h = pop.offsetHeight || 140;
let left = r.left + r.width / 2 - w / 2, top = r.bottom + 8;
left = Math.max(8, Math.min(left, window.innerWidth - w - 8));
if (top + h > window.innerHeight - 8) top = r.top - h - 8;
pop.style.left = left + 'px'; pop.style.top = top + 'px';
const pb = document.getElementById('popBuy'); if (pb) pb.onclick = () => { closePop(); buy(n); };
pop.querySelectorAll('[data-dir]').forEach(b => b.onclick = () => {
closePop(); const k = Number(b.dataset.dir);
if (k >= 1 && n.directs < 1) n.cashFromHalf = nextId; // the first direct arrives now: depths 1-2 pay from here on
if (k >= 2 && n.directs < 2) n.cashFrom = nextId; // the second: full reach from here on
n.directs = k; refreshNode(n); paintPays();
const older = cnt(nodes.filter(x => x.id < nextId && x !== n).length);
const msg = k === 2 ? name(n) + ' now has two directs: full reach, cash from its own directs and from positions that join from now on. The ' + older + ' existing positions keep passing it.'
: k === 1 ? name(n) + ' now has one direct: ' + (PLAN.half ? 'cash from depths 1-2 (levels 2-3), from its own directs and from positions that join from now on' : 'no cash until the second (half step is off)') + (PLAN.unq === 'licence' ? '; licences still reach it.' : '.')
: name(n) + ' now has no directs: cash passes over it' + (PLAN.unq === 'licence' ? ', licences still reach it.' : '.');
renderLast(null, null, null, msg, k ? 'cash' : 'none');
addLog([{ kind: k ? 'cash' : 'skip', text: msg }], name(n));
});
}
document.addEventListener('click', e => { if (!pop.hidden && !pop.contains(e.target) && !e.target.closest('.node')) closePop(); });
document.addEventListener('keydown', e => { if (e.key === 'Escape') closePop(); });
function renderLast(n, L, steps, override, cls){
const el = document.getElementById('last');
if (override) { el.className = 'last ' + (cls || ''); el.innerHTML = override; return; }
const lic = steps.filter(s => s.kind === 'lic');
const cash = steps.filter(s => s.kind === 'cash');
const youLic = lic.some(s => /^YOU held/.test(s.text));
const youCash = cash.some(s => /^YOU already/.test(s.text));
let text = '' + name(n) + ' bought level ' + L + ' (' + product(L) + ') for ' + $$(price(L)) + '. ';
if (youLic) text += 'That payment was your licence: you were upgraded in the same transaction and the cost went on above you.';
else if (youCash) text += 'It reached you as cash' + (lic.length ? ', after issuing ' + lic.length + ' licence' + (lic.length > 1 ? 's' : '') + ' on the way up' : '') + '.';
else if (lic.length) text += 'It became a licence for ' + lic[0].text.split(' held')[0] + ' and the money climbed on from there.';
else if (cash.length && !/above you/.test(cash[0].text)) text += 'It paid ' + cash[0].text.split(' already')[0] + ' as cash.';
else text += 'It went above you, to your own upline.';
const why = youLic || youCash ? '' : 'Not yours: a depth-' + n.depth + ' position pays you only when it buys level ' + (n.depth + 1) + '. It holds level ' + L + ' now' + (L < n.depth + 1 ? ', so ' + (n.depth + 1 - L) + ' more purchase' + (n.depth + 1 - L > 1 ? 's' : '') + ' until its next one reaches you.' : '.') + '';
el.className = 'last ' + (youLic ? 'lic' : youCash ? 'cash' : 'none');
el.innerHTML = text + why;
}
function buy(n){
const L = n.level + 1;
if (L > PLAN.maxL) return;
clearHot();
n.level = L; refreshNode(n);
const steps = [], flashes = [[n, 'flash-src']];
purchase(n, L, steps, flashes);
for (const [m, c] of flashes) { const g = document.getElementById('n' + m.id); if (g) g.classList.add(c); }
addLog(steps, `${name(n)} bought level ${L} for ${$$(price(L))}`);
renderLast(n, L, steps);
paintPays();
return steps;
}
function paintPays(){
for (const n of nodes) {
if (n === you) continue;
const g = document.getElementById('n' + n.id); if (!g) continue;
const r = n.level + 1 <= PLAN.maxL ? route(n, n.level + 1, n.level - 1, false, n) : { to: null };
g.classList.toggle('pays', r.to === you);
}
}
function wave(){
clearHot();
const before = you.level, cashBefore = totals.cash;
const steps = [];
quiet = true; tally = {};
const order = nodes.filter(n => n !== you).sort((a, b) => a.depth - b.depth || a.idx - b.idx);
let bought = 0, licYou = 0, skipYou = 0;
for (const n of order) {
if (!n.qual) continue;
const L = n.level + 1; if (L > PLAN.maxL) continue;
tallyOrigin = n.depth; const tf = tallyFor(n.depth); tf.n++; tf.L = L;
n.level = L; refreshNode(n); bought++;
const sub = [], fl = [];
const licBefore = totals.lic;
purchase(n, L, sub, fl);
if (totals.lic > licBefore) licYou++;
if (sub.some(s => /went on above you/.test(s.text))) skipYou++;
}
quiet = false;
renderWaveTable();
const after = you.level;
steps.push({ kind: 'lic', text: `${bought} positions each bought one level, shallow to deep.` });
steps.push({ kind: after > before ? 'lic' : 'cash', text: after > before
? `YOU went from level ${before} to ${after} without buying anything: ${licYou} licence${licYou === 1 ? '' : 's'} were funded from below.`
: (before >= DEPTHS
? `YOU stayed at level ${before}: your org is only ${DEPTHS} deep, and licence ${before + 1} is funded by a depth-${before} position buying level ${before + 1}. Nothing that deep exists here. Grow the org to keep climbing.`
: `YOU stayed at level ${before}: nothing at depth ${before} bought level ${before + 1} this wave.`) });
steps.push({ kind: 'cash', text: `Cash to you this wave: ${$$(totals.cash - cashBefore)}.` });
if (skipYou) steps.push({ kind: 'skip', text: `${skipYou} payment${skipYou === 1 ? '' : 's'} went on above you: levels that pay uplines higher than you. Not a miss.` });
addLog(steps, 'Wave');
renderLast(null, null, null, 'Wave: ' + steps.map(s => s.text).join(' '), after > before ? 'lic' : 'cash');
paintPays();
}
function renderWaveTable(){
const box = document.getElementById('waveBox'), tb = document.querySelector('#waveTable tbody');
if (!box || !tb || !tally) return;
tb.innerHTML = '';
for (const d of Object.keys(tally).map(Number).sort((a, b) => a - b)) {
const t = tally[d];
const toYou = [t.youLic ? '' + t.youLic + ' licence' + (t.youLic > 1 ? 's' : '') + '' : '', t.youCash ? '' + $$(t.youCash) + '' : ''].filter(Boolean).join(' + ') || '—';
const others = Object.keys(t.cash).concat(Object.keys(t.lic)).filter((v, i, a) => a.indexOf(v) === i).map(Number).sort((a, b) => a - b)
.map(k => 'depth ' + k + ': ' + (t.cash[k] ? $$(t.cash[k]) : '') + (t.lic[k] ? (t.cash[k] ? ' + ' : '') + t.lic[k] + ' licence' + (t.lic[k] > 1 ? 's' : '') : '')).join(' ') || '—';
const tr = document.createElement('tr');
const above = [t.above ? $$(t.above) : '', t.licUp ? $$(t.licUp) + ' (your licence cost, climbing)' : ''].filter(Boolean).join(' + ') || '—';
tr.innerHTML = '
' + d + '
' + t.L + ' (' + $$(price(t.L)) + ')
' + t.n + '
' + toYou + '
' + others + '
' + above + '
';
tb.appendChild(tr);
}
box.hidden = false;
}
function addLog(steps, head){
const ol = document.getElementById('logList');
if (ol.firstChild && /Nothing yet|Reset\./.test(ol.firstChild.textContent)) ol.innerHTML = '';
const h = document.createElement('li'); h.className = 'head'; h.textContent = head; ol.prepend(h);
let after = h;
for (const s of steps) {
const li = document.createElement('li');
const chip = document.createElement('span'); chip.className = 'chip ' + s.kind; chip.textContent = s.kind === 'lic' ? 'licence' : s.kind === 'skip' ? 'skip' : 'cash';
li.appendChild(chip); li.appendChild(document.createTextNode(' ' + s.text));
after.after(li); after = li;
}
while (ol.children.length > 60) ol.removeChild(ol.lastChild);
}
function clearLog(msg){ document.getElementById('logList').innerHTML = `
${msg}
`; }
const lastShown = {};
function setV(id, text){
const el = document.getElementById(id); if (!el) return;
if (lastShown[id] !== undefined && lastShown[id] !== text) { el.classList.remove('bump'); void el.offsetWidth; el.classList.add('bump'); }
lastShown[id] = text; el.textContent = text;
}
function renderTotals(){
if (!you) return;
setV('mLevel', you.level);
const sl = document.getElementById('youLevel'); if (sl && Number(sl.value) !== you.level && you.level <= Number(sl.max)) { sl.value = you.level; document.getElementById('youLevelVal').textContent = you.level; }
const mr = document.getElementById('mReach'); if (mr) mr.textContent = you.level;
setV('mEntry', $$(totals.entry));
const en = document.getElementById('mEntryN'); if (en) en.textContent = you.children.length + ' directs × ' + $$(net(1)) + ' (the ' + $$(price(1)) + ' entry after the fee), no qualification';
setV('mCash', $$(totals.cash));
const cn = document.getElementById('mCashN'); if (cn) cn.textContent = totals.cashN + ' payment' + (totals.cashN === 1 ? '' : 's');
setV('mLic', totals.lic);
const lv = document.getElementById('mLicV'); if (lv) lv.textContent = 'worth ' + $$(totals.licV) + ', paid by your org';
setV('mTotal', $$(totals.entry + totals.cash + totals.licV));
setV('mFuel', $$(AUD.treasury)); const fs = document.getElementById('mFuelS'); if (fs) fs.textContent = PLAN.fee ? Math.round(PLAN.fee * 100) + '% out of every price; the other ' + Math.round((1 - PLAN.fee) * 100) + '% climbs' : 'no fee: the whole price climbs';
const ol = document.getElementById('youLedger');
if (ol) {
ol.innerHTML = '';
if (!youLedger.length) ol.innerHTML = '
Nothing yet.
';
for (const e of youLedger.slice(0, 10)) {
const li = document.createElement('li');
li.innerHTML = '' + (e.kind === 'lic' ? 'licence' : 'cash') + ' ' + (e.kind === 'lic' ? 'your licence ' + e.L + ', funded by ' : 'level-' + e.L + ' money from ') + name(e.from) + '' + (e.kind === 'lic' ? 'licence ' + e.L + ' (' + $$(e.amt) + ')' : '+' + $$(e.amt)) + '';
ol.appendChild(li);
}
}
const ln = document.getElementById('ledgerNote'); if (ln) ln.textContent = 'Each position pays you at most once per level, so the count is what matters: at level L there are ' + PLAN.w + '^' + '(L−1) positions at the depth that pays you, and each pays the level-L price.';
ladder();
}
document.getElementById('youLevel').addEventListener('input', e => {
document.getElementById('youLevelVal').textContent = e.target.value;
you.level = Number(e.target.value); refreshNode(you); paintReach(); paintPays(); renderTotals();
});
document.getElementById('wave').addEventListener('click', wave);
function at(path){ let n = you; for (const ch of path) n = n.children[ch === 'L' ? 0 : 1]; return n; }
const TOUR = [
{ step: 'Step 1 of 7 · your licence', say: 'You hold level 1, like every new member, and so do your directs (labelled). Their entries already paid you. Your FIRST direct buys level 2. Level-2 money pays the first upline, which is you, and you hold exactly one level below, so this payment IS your licence 2: you are upgraded in the same transaction and the money continues above you. No cash yet.',
act: () => buy(at('L')) },
{ step: 'Step 2 of 7 · cash', say: 'Your SECOND direct buys level 2. You now hold level 2, so this one is cash: the level-2 price to you. Watch the bar.',
act: () => buy(at('R')) },
{ step: 'Step 3 of 7 · not yours', say: 'A grandchild, under your first direct, buys level 2. Level-2 money stops at the FIRST upline, its own parent, who already holds level 2, so it is that parent\'s cash and never reaches you. That is why the rings matter: a position pays you only at the one level that maps to its depth.',
act: () => buy(at('LL')) },
{ step: 'Step 4 of 7 · your licence 3', say: 'The same grandchild buys level 3. Level-3 money skips one upline and lands on you; you hold level 2, exactly one below, so this is your licence 3. The amber band steps down a depth and the rings move.',
act: () => buy(at('LL')) },
{ step: 'Step 5 of 7 · cascade', say: 'One purchase can pay two people. A depth-3 position under your second direct buys level 2. It pays its parent (depth 2), who holds exactly level 1: that parent gets licence 2, and the parent\'s own level-2 cost climbs to your second direct as cash. One buyer, one licence issued, one cash payment.',
act: () => buy(at('RLL')) },
{ step: 'Step 6 of 7 · skipped', say: 'Your first direct now has NO directs of its own (dashed). Its other child buys level 2. That money would pay the parent, but the parent has recruited nobody, so cash passes over it and keeps climbing: you catch it. Qualification decides who is skipped; it never holds money back. (With one direct, the half step would let the parent keep this one.)',
act: () => { const l = at('L'); l.directs = 0; refreshNode(l); paintPays(); buy(at('LR')); } },
{ step: 'Step 7 of 7 · the wave', say: 'Everything at once: every position buys one level, shallow to deep. Read the table that appears under the strip: one row per depth, where that depth\'s money went. Then press Run a wave again and watch your level climb without buying anything.',
act: () => wave() },
{ step: 'Done', say: 'That is the whole plan. Now switch the gap rule and the width at the top and run waves again: the tree resets, and the difference shows up in the money bar.', act: null }
];
let ti = -1;
function tourShow(){
const t = TOUR[ti]; document.getElementById('tourStep').textContent = t.step; document.getElementById('tourSay').textContent = t.say;
const nx = document.getElementById('tourNext'); nx.hidden = false; nx.textContent = t.act ? 'Show me' : 'Finish';
document.getElementById('tourStart').textContent = 'Restart tour';
}
document.getElementById('tourStart').addEventListener('click', () => { document.getElementById('youLevel').value = 1; document.getElementById('youLevelVal').textContent = '1'; build(); ti = 0; tourShow(); });
document.getElementById('tourNext').addEventListener('click', () => {
const t = TOUR[ti];
if (t.act) { t.act(); }
if (ti < TOUR.length - 1) { ti++; tourShow(); }
else { document.getElementById('tourNext').hidden = true; }
});
document.getElementById('reset').addEventListener('click', () => { document.getElementById('youLevel').value = 1; document.getElementById('youLevelVal').textContent = '1'; build(); });
document.getElementById('grow').addEventListener('click', grow);
function depthLabel(){ const el = document.getElementById('depthLabel'); if (el) el.textContent = DEPTHS + ' deep, ' + cnt(nodes.length - 1) + ' positions'; const g = document.getElementById('grow'); if (g) g.disabled = DEPTHS >= MAXD[PLAN.w]; }
function grow(){
if (DEPTHS >= MAXD[PLAN.w]) return;
const leaves = nodes.filter(n => n.depth === DEPTHS);
DEPTHS++;
let entries = 0;
for (const n of leaves) for (let k = 0; k < PLAN.w; k++) { mkNode(n.depth + 1, n, n.idx * PLAN.w + k); entries += net(1); }
geometry(); layout(); draw(); paintPays(); renderTotals(); depthLabel();
addLog([{ kind: 'lic', text: `The org is now ${DEPTHS} deep: ${leaves.length * PLAN.w} new level-1 positions joined at the bottom and paid ${$$(entries)} of entries to the row above. Everything else is as it was.` }], 'Grew one depth');
}
// ── ladder table ──
const priceEl = document.getElementById('price');
function ladder(){
document.getElementById('priceVal').textContent = '$' + priceEl.value;
const tb = document.querySelector('#ladder tbody'); if (!tb) return; tb.innerHTML = '';
const rows = [1]; const top = Math.min(12, PLAN.maxL);
for (let L = 2; L <= top; L++) rows.push(L);
if (PLAN.maxL > 12) rows.push('gap', 20, 'gap', PLAN.maxL);
let climb = 0; for (let L = 1; L <= PLAN.maxL; L++) climb += price(L);
for (const L of rows) {
const tr = document.createElement('tr');
if (L === 'gap') { tr.className = 'gap'; tr.innerHTML = '
⋮
'; tb.appendChild(tr); continue; }
const d = L === 1 ? 1 : L - 1, pos = Math.pow(PLAN.w, d);
if (you && L === you.level + 1) tr.className = 'you';
tr.innerHTML = `
${L}${you && L === you.level + 1 ? ' your next' : ''}
${product(L)} placeholder
${L === 1 ? 'entries from depth 1, no qualification' : 'depth ' + d + ', buying level ' + L}
${cnt(pos)}
${$$(pos * net(L))}
${$$(price(L))}${L === 1 ? ' everyone pays this to enter' : ' or the first payment from depth ' + d + ''}
`;
tb.appendChild(tr);
}
const ln = document.getElementById('ladderNote'); if (ln) ln.textContent = PLAN.w + '-wide, ' + PLAN.maxL + ' levels. Potential is one payment from every position at the depth that pays that level, so it grows ' + PLAN.w + '× per level on its own; the price ladder only sets the dollars, and ' + Math.round(PLAN.fee * 100) + '% of every price goes to the admin wallet before anything climbs. Climbing all ' + PLAN.maxL + ' levels yourself would cost ' + $$(climb) + ', which nobody has to do: the first payment at each depth is the licence.';
}
priceEl.addEventListener('input', () => { renderTotals(); for (const n of nodes) refreshNode(n); if (dp) deepRender(); });
// ── deep simulator: the same rules on counts per depth ──
let dp = null;
function deepReset(){
const fill = Number(document.getElementById('deepFill').value) / 100, D = PLAN.maxL;
dp = { D, levels: [], counts: [], lic: [], licV: [], cash: [], entry: [], pays: [], waves: 0, above: 0, spent: 0, treasury: 0 };
window.deepAudit = () => { const rec = dp.cash.reduce((a, b) => a + b, 0) + dp.entry.reduce((a, b) => a + b, 0) + dp.above + dp.treasury; return { spent: Math.round(dp.spent), received: Math.round(rec), ok: Math.abs(dp.spent - rec) <= 1 + 1e-9 * dp.spent }; };
for (let d = 0; d <= D; d++) {
dp.levels[d] = 1;
dp.counts[d] = d === 0 ? 1 : Math.max(1, Math.round(Math.pow(PLAN.w * fill, d)));
dp.lic[d] = 0; dp.licV[d] = 0; dp.cash[d] = 0; dp.entry[d] = 0; dp.pays[d] = 0;
}
for (let d = 1; d <= D; d++) { dp.entry[d - 1] += dp.counts[d] * net(1); dp.spent += dp.counts[d] * price(1); dp.treasury += dp.counts[d] * price(1) * PLAN.fee; } // Rule 0, once, when the org is built
deepRender('Reset. Everyone at level 1, you included; entries paid.');
const ol = document.getElementById('deepStory'); if (ol) ol.innerHTML = '
Start. You and everyone under you hold level 1. Your ' + cnt(dp.counts[1]) + ' directs paid their entries: ' + $$(dp.entry[0]) + ' to you after the fee. Press Run one wave.
';
}
// n payments of `amt` for level L from depth `from`: skip `skipN`, then the gap rule on aggregate levels
function deepPay(n, from, L, amt, skipN, rem){
if (n <= 0) return;
let r = from - 1 - skipN;
while (r >= 0 && !(rem ? dp.levels[r] >= L : (PLAN.gap === 'fund' || dp.levels[r] >= L - 1))) r--;
if (r < 0) { dp.above += n * amt; return; }
if (dp.levels[r] >= L) { dp.cash[r] += n * amt; dp.pays[r] += n; return; }
const nl = dp.levels[r] + 1, cost = net(nl);
const granted = Math.min(dp.counts[r], n);
dp.lic[r] += granted; dp.licV[r] += granted * cost; dp.levels[r] = nl;
deepPay(granted, r, nl, cost, nl - 2); // the licence costs climb from depth r
if (amt - cost > 0.005) deepPay(granted, r, L, amt - cost, 0, true); // the remainder climbs on, cash only
deepPay(n - granted, from, L, amt, skipN); // the rest of this depth's payments, now that r has moved up
}
function deepWave(){
if (!dp) deepReset();
for (let d = 1; d <= dp.D; d++) { const L = dp.levels[d] + 1; if (L > PLAN.maxL) continue; dp.spent += dp.counts[d] * price(L); dp.treasury += dp.counts[d] * price(L) * PLAN.fee; deepPay(dp.counts[d], d, L, net(L), L - 2); dp.levels[d] = Math.max(dp.levels[d], L); }
dp.waves++;
}
function deepRender(note){
document.getElementById('dLevel').textContent = dp.levels[0];
document.getElementById('dReach').textContent = Math.min(dp.levels[0], dp.D);
document.getElementById('dEntry').textContent = $$(dp.entry[0]);
document.getElementById('dCash').textContent = $$(dp.cash[0]);
document.getElementById('dCashN').textContent = cnt(dp.pays[0]) + ' payment' + (dp.pays[0] === 1 ? '' : 's');
document.getElementById('dLic').textContent = dp.lic[0];
document.getElementById('dLicV').textContent = 'worth ' + $$(dp.licV[0]);
document.getElementById('dWaves').textContent = dp.waves;
document.getElementById('dAbove').textContent = $$(dp.above);
document.getElementById('dFuel').textContent = $$(dp.treasury);
const tb = document.querySelector('#deepTable tbody'); tb.innerHTML = '';
const maxCash = Math.max(1, ...dp.cash.map((c, d) => d === 0 ? 0 : c));
for (let d = 0; d <= dp.D; d++) {
const tr = document.createElement('tr');
tr.className = d === 0 ? 'you' : d <= dp.levels[0] ? 'reach' : d === dp.levels[0] + 1 ? 'next' : '';
const per = dp.counts[d] ? (dp.cash[d] + dp.entry[d]) / dp.counts[d] : 0;
tr.innerHTML = '
' + (d === 0 ? 'YOU' : d) + '
' + cnt(dp.counts[d]) + '
' + dp.levels[d] + '
' + cnt(dp.lic[d]) + '
' + $$(dp.entry[d]) + '
' + $$(dp.cash[d]) + '
'
+ '
' + (d === 0 ? '' : '') + '
' + $$(per) + '
';
tb.appendChild(tr);
}
if (note) document.getElementById('deepNote').textContent = note;
}
function storyLine(html, cls){ const ol = document.getElementById('deepStory'); if (!ol) return; if (ol.firstChild && /Nothing has happened/.test(ol.firstChild.textContent)) ol.innerHTML = ''; const li = document.createElement('li'); if (cls) li.className = cls; li.innerHTML = html; ol.prepend(li); while (ol.children.length > 40) ol.removeChild(ol.lastChild); }
function deepRun(k){
if (!dp) deepReset();
let ran = 0;
for (let i = 0; i < k; i++) {
if (dp.levels[0] >= PLAN.maxL) break;
const lv0 = dp.levels[0], cash0 = dp.cash[0], lic0 = dp.lic[0], adm0 = dp.treasury, above0 = dp.above;
deepWave(); ran++;
const lv1 = dp.levels[0], dCash = dp.cash[0] - cash0, dLic = dp.lic[0] - lic0;
const d = lv0; // the depth whose purchase this wave could reach you
const L = d + 1;
let html = 'Wave ' + dp.waves + '. Every position bought its next level. ';
if (d <= dp.D) html += 'The ' + cnt(dp.counts[d]) + ' positions at depth ' + d + ' bought level ' + L + ' (' + product(L) + ', ' + $$(price(L)) + ' each, ' + $$(net(L)) + ' after the fee), and level-' + L + ' money is yours. ';
if (dLic) html += 'The first one was your licence ' + lv1 + ': you were upgraded in the same block and its cost climbed on above you. ';
if (dCash > 0) html += 'The rest reached you as cash: ' + $$(dCash) + '. ';
if (!dLic && dCash <= 0) html += 'Nothing reached you: that depth is empty at this fill, or you are at the top. ';
html += 'Everything the other depths bought paid the people between you and them, or climbed past you. Admin wallet took ' + $$(dp.treasury - adm0) + ' this wave. ';
html += 'You: level ' + lv1 + ', reach depth 1 to ' + Math.min(lv1, dp.D) + '';
storyLine(html, dLic ? 'lic' : 'cash');
}
const after = dp.levels[0];
if (after >= PLAN.maxL) storyLine('Top of the ladder. You hold level ' + PLAN.maxL + '. From here every payment from every depth is cash, and there is no level 9: positions ' + PLAN.maxL + ' deep pay the people between you and them.', 'head');
deepRender(ran + ' wave' + (ran === 1 ? '' : 's') + ' run.');
}
document.getElementById('deepWave').addEventListener('click', () => deepRun(1));
document.getElementById('deepWave5').addEventListener('click', () => deepRun(5));
document.getElementById('deepToTop').addEventListener('click', () => deepRun(80));
document.getElementById('deepReset').addEventListener('click', deepReset);
document.getElementById('deepFill').addEventListener('input', e => { document.getElementById('deepFillVal').textContent = e.target.value + '%'; deepReset(); });
// ── plan controls ──
function applyPlan(){
PLAN.w = Number(document.getElementById('width').value);
PLAN.maxL = Number(document.getElementById('levels').value);
PLAN.shape = document.getElementById('shape').value;
PLAN.steps = document.getElementById('steps').value.split(',').map(s => Number(s.trim())).filter(n => n > 0);
PLAN.products = document.getElementById('products').value.split(',').map(s => s.trim()).filter(Boolean);
PLAN.fee = Number(document.getElementById('fee').value) || 0;
if (!PLAN.steps.length) PLAN.steps = [10];
document.getElementById('stepsCtl').hidden = PLAN.shape !== 'stepped';
document.getElementById('priceCtl').hidden = PLAN.shape === 'stepped';
document.getElementById('gapSkip').setAttribute('aria-pressed', String(PLAN.gap === 'skip'));
document.getElementById('gapFund').setAttribute('aria-pressed', String(PLAN.gap === 'fund'));
document.getElementById('unqSkip').setAttribute('aria-pressed', String(PLAN.unq === 'skip'));
document.getElementById('unqLic').setAttribute('aria-pressed', String(PLAN.unq === 'licence'));
document.getElementById('halfOff').setAttribute('aria-pressed', String(!PLAN.half));
document.getElementById('halfOn').setAttribute('aria-pressed', String(PLAN.half));
document.getElementById('wmOff').setAttribute('aria-pressed', String(!PLAN.wm));
document.getElementById('wmOn').setAttribute('aria-pressed', String(PLAN.wm));
const yl = document.getElementById('youLevel'); yl.max = PLAN.maxL; yl.value = 1; // a new plan starts you at level 1, like everyone
document.getElementById('youLevelVal').textContent = yl.value;
{ const dh = document.getElementById('deepHead'); if (dh) dh.textContent = 'Watch one position climb all ' + PLAN.maxL + ' levels'; }
svg.setAttribute('aria-label', PLAN.w + '-wide matrix under you');
build(); deepReset();
}
for (const id of ['width', 'levels', 'shape']) document.getElementById(id).addEventListener('change', applyPlan);
document.getElementById('steps').addEventListener('change', applyPlan);
document.getElementById('products').addEventListener('change', applyPlan);
document.getElementById('fee').addEventListener('change', applyPlan);
document.getElementById('gapSkip').addEventListener('click', () => { PLAN.gap = 'skip'; applyPlan(); });
document.getElementById('gapFund').addEventListener('click', () => { PLAN.gap = 'fund'; applyPlan(); });
document.getElementById('unqSkip').addEventListener('click', () => { PLAN.unq = 'skip'; applyPlan(); });
document.getElementById('unqLic').addEventListener('click', () => { PLAN.unq = 'licence'; applyPlan(); });
document.getElementById('halfOff').addEventListener('click', () => { PLAN.half = false; applyPlan(); });
document.getElementById('halfOn').addEventListener('click', () => { PLAN.half = true; applyPlan(); });
document.getElementById('wmOff').addEventListener('click', () => { PLAN.wm = false; applyPlan(); });
document.getElementById('wmOn').addEventListener('click', () => { PLAN.wm = true; applyPlan(); });
applyPlan();
})();