"""Exact mathematical dependency diagram, CC0. Box areas carry no trace values."""
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch,FancyArrowPatch
HERE=Path(__file__).resolve().parent
plt.rcParams.update({'font.family':'DejaVu Sans','svg.hashsalt':'wm-normal-count-certificates-v30'})
fig,ax=plt.subplots(figsize=(17,13.5),dpi=120);fig.patch.set_facecolor('#f8fafc');ax.set_xlim(0,1700);ax.set_ylim(1350,0);ax.axis('off');fig.subplots_adjust(0,0,1,1)
def text(x,y,s,size=19,color='#142b40',weight='normal',ha='left'):ax.text(x,y,s,fontsize=size,color=color,weight=weight,ha=ha,va='top')
def box(x,y,w,h,color):ax.add_patch(FancyBboxPatch((x,y),w,h,boxstyle='round,pad=8,rounding_size=10',facecolor=color,edgecolor='#b4c4d0',linewidth=1.3))
def arrow(x1,y1,x2,y2):ax.add_patch(FancyArrowPatch((x1,y1),(x2,y2),arrowstyle='-|>',mutation_scale=22,linewidth=1.6,color='#2b6579'))
text(45,25,'Certified normal errors from the original full-group counts',26,weight='bold')
text(45,75,r'$\lambda=1+2^{-31}25^{-198153}$; fixed full words first, normal count depth second',19)
box(45,125,1610,145,'#e9f1f7')
text(65,145,r'$h=(c,v,m)\in H$: all lamps, $v\in\mathbb{Z}^3$, central integer and parity retained',20)
text(65,190,r'$P_n(z)=N_{2n}^+(z)\lambda^{m(z)}/d^n$,  $q_{h,n}\longrightarrow q_h$ in $L^2(\mu_V)$',20)
text(65,235,r'$q_h=d((h^{-1})_*\mu_U)/d\mu_V$,  $0\leq q_h\leq M_h=p_h^{-1}$',18)
arrow(440,283,440,323);arrow(1260,283,1260,323)
box(45,335,765,220,'#e4f2ef');box(880,335,775,220,'#f5efdf')
text(65,355,'Exact reverse-projection second moments',20,weight='bold')
text(65,410,r'$V_{h,n}=\sum_z P_n(z)q_{h,n}(z)^2$',21)
text(65,460,r'$V_{h,n}\downarrow V_{h,\infty}=\|q_h\|_2^2$',21)
text(65,505,r'$\|q_{h,n}-q_h\|_2^2=V_{h,n}-V_{h,\infty}$',20)
text(900,355,'Finite rational lamp-cylinder tests',20,weight='bold')
text(900,404,r'$f=f(\eta|_E)$,  $M=\|f\|_\infty$,  $N=t^2$',20)
text(900,455,r'$\theta_E(N)=\min(1,256|E|/\sqrt{N})$',20)
text(900,505,'Both phase expectations use full endpoint counts.',17)
arrow(445,567,445,606);arrow(1255,567,1255,606)
box(45,620,1610,185,'#edf2f8')
text(65,640,'A computable lower bound on the unknown limiting moment',20,weight='bold')
text(65,690,r'$L_h(f,N)=2I_{-,N}(f;h)-I_{+,N}(f^2;e)-(4M+M^2)\theta_E(N)$',22)
text(65,748,r'$L_h(f,N)\leq V_{h,\infty}$; finite cylinder density gives $\sup_{f,t}L_h(f,t^2)=V_{h,\infty}$',20)
arrow(850,817,850,851)
box(45,865,1610,145,'#e4f2ef')
text(65,885,'The finite certificate persists at every later normal count depth',20,weight='bold')
text(65,938,r'$0\leq V_{h,n}-L_h(f,N)<\zeta^2\quad\Longrightarrow\quad\|q_{h,n}-q_h\|_2<\zeta$',22)
arrow(850,1022,850,1055)
box(45,1070,1610,180,'#f1edf5')
text(65,1090,r'$a=1/S_-$,  $u_{g,n}=q_{g^{-1},n}/\max(a,q_{e,n})$,  $D_g=q_{g^{-1}}/q_e$',20)
text(65,1140,r'$\|u_{g,n}-D_g\|_2\leq a^{-1}e_{g^{-1},n}+M_{g^{-1}}a^{-2}e_{e,n}$',22)
text(65,1190,'The procedure certifies each fixed finite input; it does not determine the endpoint sign.',17,color='#783f22')
text(45,1282,'Proofs: CD29.3–CD29.11. Original full-center/count providers: FWT.26–30 and OM.15–21.',17)
text(45,1320,'Map and bound diagram; box areas encode no probabilities. Text and diagram CC0.',16,color='#5b6b78')
fig.savefig(HERE/'normal-count-certificates-v30.png',metadata={'Software':'Original full-group normal count certificates, CC0'})
fig.savefig(HERE/'normal-count-certificates-v30.svg',metadata={'Date':None})
plt.close(fig)
print('Rendered exact full-group normal-count certificate diagram.')
