const b={names:window.__COMPARE_NAMES__||[],systems:window.__COMPARE_SYSTEMS__||{},bands:window.__COMPARE_BANDS__||[]},w={};for(const a of b.bands)w[a.number]=a.color;const B={};for(const a of b.bands)B[a.number]=a.name;function T(){const a=new URLSearchParams(window.location.search);return{a:a.get("a"),b:a.get("b")}}function _(a,l){const e=new URL(window.location);a?e.searchParams.set("a",a):e.searchParams.delete("a"),l?e.searchParams.set("b",l):e.searchParams.delete("b"),window.history.replaceState({},"",e)}function S(){const a=document.getElementById("system-a"),l=document.getElementById("system-b");for(const i of b.names){const m=document.createElement("option");m.value=i,m.textContent=i,a.appendChild(m);const d=document.createElement("option");d.value=i,d.textContent=i,l.appendChild(d)}const e=T();e.a&&b.systems[e.a]&&(a.value=e.a),e.b&&b.systems[e.b]&&(l.value=e.b),a.value&&l.value&&C()}function L(a,l){const e=a.vector,i=l.vector;let m=0;const d=[];for(let o=0;o<72;o++){const u=e[o]-i[o];m+=u*u,d.push({band:o+1,valA:e[o],valB:i[o],delta:Math.abs(u)})}const c=Math.sqrt(m/72),f=Math.max(...e,...i),x=Math.max(0,Math.min(1,1-c/(f||1))),t=Math.abs(a.cz-l.cz);d.sort((o,u)=>u.delta-o.delta);const p=d.slice(0,10),h=[];for(let o=0;o<8;o++){let u=0,M=0;for(let E=0;E<9;E++){const A=o*9+E;u+=e[A],M+=i[A]}h.push({octave:o+1,meanA:u/9,meanB:M/9})}const n={pos3:[],pos6:[],pos9:[]};for(let o=1;o<=72;o++){const u=o-1;o%3===0&&n.pos3.push({band:o,valA:e[u],valB:i[u]}),o%6===0&&n.pos6.push({band:o,valA:e[u],valB:i[u]}),o%9===0&&n.pos9.push({band:o,valA:e[u],valB:i[u]})}const s={3:0,6:0,9:0},r={3:0,6:0,9:0};for(const o of n.pos3)s[3]+=o.valA,r[3]+=o.valB;for(const o of n.pos6)s[6]+=o.valA,r[6]+=o.valB;for(const o of n.pos9)s[9]+=o.valA,r[9]+=o.valB;const y=Math.max(...e),v=Math.max(...i),g=e.indexOf(y)+1,$=i.indexOf(v)+1,H=g===$?`Band ${g} (${B[g]})`:`A: Band ${g} | B: Band ${$}`;return{inharmony:c,similarity:x,czDelta:t,topMatch:H,diffs:d,topDiffs:p,octaves:h,tesla:n,sumTeslaA:s,sumTeslaB:r}}function C(){const a=document.getElementById("system-a").value,l=document.getElementById("system-b").value,e=document.getElementById("compare-results"),i=document.getElementById("compare-empty");if(!a||!l){e.style.display="none",i.style.display="block";return}_(a,l);const m=b.systems[a],d=b.systems[l];if(!m||!d)return;e.style.display="block",i.style.display="none",document.getElementById("system-a-detail").textContent=`CZ: ${m.cz.toFixed(4)} | Category: ${m.category.dominant}`,document.getElementById("system-b-detail").textContent=`CZ: ${d.cz.toFixed(4)} | Category: ${d.category.dominant}`;const c=L(m,d),f=Math.max(...m.vector,...d.vector)||1;document.getElementById("inharmony-val").textContent=c.inharmony.toFixed(6),document.getElementById("similarity-val").textContent=(c.similarity*100).toFixed(1)+"%",document.getElementById("cz-delta-val").textContent=c.czDelta.toFixed(4),document.getElementById("top-band-val").textContent=c.topMatch,document.getElementById("heatmap-a-label").textContent=a,document.getElementById("heatmap-b-label").textContent=l,I("heatmap-system-a",m.vector,f,!1),I("heatmap-system-b",d.vector,f,!0);const x=document.getElementById("heatmap-label-row");x.innerHTML="";for(let n=0;n<72;n++){const s=document.createElement("div");s.className="heatmap-label",n%9===0||n===0?(s.textContent=n+1+"→"+Math.min(n+9,72),s.style.minWidth="10px",s.style.fontSize="0.6rem",s.style.color="var(--color-text-muted)",s.style.textAlign="center",s.style.flex="1"):s.style.display="none"}x.innerHTML="";for(let n=0;n<8;n++){const s=n*9+1,r=s+8,y=document.createElement("div");y.textContent=`B${s}-${r}`,y.style.flex="9",y.style.fontSize="0.6rem",y.style.color="var(--color-text-muted)",y.style.textAlign="center",y.style.minWidth="0",x.appendChild(y)}F("octave-chart",c.octaves,"meanA","meanB",a,l),z("tesla-chart",c.sumTeslaA,c.sumTeslaB,a,l);const t=m.teslaHarmonics?(m.teslaHarmonics.harmonic3/m.teslaHarmonics.harmonic6).toFixed(2):"—",p=d.teslaHarmonics?(d.teslaHarmonics.harmonic3/d.teslaHarmonics.harmonic6).toFixed(2):"—";document.getElementById("tesla-ratio").innerHTML=`<strong>3/6 Ratio:</strong> ${a}: ${t} | ${l}: ${p}`;const h=document.getElementById("diff-body");h.innerHTML="";for(const n of c.topDiffs){const s=document.createElement("tr"),r=w[n.band]||"#666";s.style.borderLeft=`3px solid ${r}`,s.innerHTML=`
        <td><strong>${n.band}</strong></td>
        <td style="font-size:0.85rem;">${B[n.band]||""}</td>
        <td>${n.valA.toFixed(5)}</td>
        <td>${n.valB.toFixed(5)}</td>
        <td style="color: var(--band-coral); font-weight: 600;">${n.delta.toFixed(5)}</td>
      `,h.appendChild(s)}}function I(a,l,e,i){const m=document.getElementById(a);m.innerHTML="",m.style.display="flex",m.style.gap="1px";const d=[{start:1,end:12,color:"#ff6b6b"},{start:13,end:24,color:"#8844ff"},{start:25,end:36,color:"#44ccff"},{start:37,end:48,color:"#44ff88"},{start:49,end:60,color:"#ffaa44"},{start:61,end:72,color:"#ffdd66"}];for(let c=0;c<72;c++){const f=l[c],x=e>0?f/e:0,t=document.createElement("div");let p="#333";for(const n of d)if(c+1>=n.start&&c+1<=n.end){p=n.color;break}const h=Math.max(.05,Math.min(1,x));t.style.background=p,t.style.opacity=h,t.style.width="10px",t.style.height=i?"20px":"24px",t.style.minWidth="10px",t.style.flex="1",t.style.borderRadius="2px",t.style.transition="opacity 0.2s",t.title=`Band ${c+1}: ${f.toFixed(5)} (${B[c+1]||""})`,t.style.cursor="pointer",t.addEventListener("mouseenter",()=>{t.style.transform="scaleY(1.3)",t.style.zIndex="5"}),t.addEventListener("mouseleave",()=>{t.style.transform="",t.style.zIndex=""}),m.appendChild(t)}}function F(a,l,e,i,m,d){const c=document.getElementById(a);if(!c)return;c.innerHTML="";const f=Math.max(...l.map(t=>Math.max(t[e],t[i])));if(f===0){c.textContent="No data";return}const x=document.createElement("div");x.style.display="flex",x.style.alignItems="flex-end",x.style.gap="4px",x.style.height="100%",x.style.padding="0 4px";for(const t of l){const p=document.createElement("div");p.style.display="flex",p.style.flexDirection="column",p.style.alignItems="center",p.style.flex="1",p.style.gap="2px";const h=t[e]/f*160,n=t[i]/f*160,s=document.createElement("div");s.style.height=h+"px",s.style.width="100%",s.style.maxWidth="20px",s.style.background="var(--band-cyan)",s.style.borderRadius="3px 3px 0 0",s.style.opacity="0.8",s.title=`${m} Oct ${t.octave}: ${t[e].toFixed(5)}`;const r=document.createElement("div");r.style.height=n+"px",r.style.width="100%",r.style.maxWidth="20px",r.style.background="var(--band-coral)",r.style.borderRadius="3px 3px 0 0",r.style.opacity="0.8",r.title=`${d} Oct ${t.octave}: ${t[i].toFixed(5)}`,p.appendChild(s),p.appendChild(r);const y=document.createElement("div");y.textContent=t.octave,y.style.fontSize="0.6rem",y.style.color="var(--color-text-muted)",y.style.marginTop="2px",p.appendChild(y),x.appendChild(p)}c.appendChild(x)}function z(a,l,e,i,m){const d=document.getElementById(a);if(!d)return;d.innerHTML="";const c=Math.max(l[3],l[6],l[9],e[3],e[6],e[9]);if(c===0){d.textContent="No data";return}const f=document.createElement("div");f.style.display="flex",f.style.alignItems="flex-end",f.style.gap="8px",f.style.height="100%";const x=["Harmonic 3","Harmonic 6","Harmonic 9"];for(let t=0;t<3;t++){const p=["3","6","9"][t],h=document.createElement("div");h.style.display="flex",h.style.flexDirection="column",h.style.alignItems="center",h.style.flex="1",h.style.gap="2px";const n=l[p]/c*160,s=e[p]/c*160,r=document.createElement("div");r.style.height=n+"px",r.style.width="100%",r.style.maxWidth="30px",r.style.background="var(--band-cyan)",r.style.borderRadius="3px 3px 0 0",r.style.opacity="0.8",r.title=`${i} ${x[t]}: ${l[p].toFixed(5)}`;const y=document.createElement("div");y.style.height=s+"px",y.style.width="100%",y.style.maxWidth="30px",y.style.background="var(--band-coral)",y.style.borderRadius="3px 3px 0 0",y.style.opacity="0.8",y.title=`${m} ${x[t]}: ${e[p].toFixed(5)}`,h.appendChild(r),h.appendChild(y);const v=document.createElement("div");v.textContent=x[t],v.style.fontSize="0.6rem",v.style.color="var(--color-text-muted)",v.style.marginTop="2px",h.appendChild(v),f.appendChild(h)}d.appendChild(f)}document.getElementById("system-a").addEventListener("change",C);document.getElementById("system-b").addEventListener("change",C);S();
