"""Site-style desktop + mobile SVG for #binary-tetrahedral-pure-zero-side (panels b and c of tetra-pure-zero-side).
Light palette matches the rest of the page; dark twins are made by dark_figs."""
import json, html
from pathlib import Path
HERE=Path(__file__).resolve().parent; FIG=HERE.parent/'figures'
R=json.loads((HERE/'results-b.a.json').read_text())['rows']
BG='#faf8f2'; INK='#233d36'; GREEN='#3f7763'; GOLD='#b28b4e'; MUTED='#65746c'; GRID='#dce2d8'; PUR='#7a5c8f'; BLUE='#3f6f8f'; RUST='#a8553a'
E=[r for r in R if r['kind']=='E' and r['x0']==0 and r['T']<=8]
G=[r for r in R if r['kind']=='G']
def make(mobile=False):
    W,Ht=(400,1000) if mobile else (1240,630)
    a=[f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {Ht}" role="img" aria-labelledby="title desc">',
       '<title id="title">Where the extra quaternion direction lives, binary tetrahedral field 2.26569.24t7.b.a</title>',
       '<desc id="desc">Two panels. Left: the u-direction of the pure part against the Gaussian width T, split into the zero-side shares of zeta, L of omega and omega-bar together, and minus a quarter of L of psi-3. Zeta dominates. Right: the n-direction. For the odd test function the zeros of L of rho tensor omega above the real axis give a large positive sum, those below give a large negative sum, and their small difference is the n-direction; at T equal to 1 it is plus 0.940, minus 1.022, total minus 0.082.</desc>',
       f'<rect width="{W}" height="{Ht}" fill="{BG}"/>',
       f'<style>text{{fill:{INK};font-family:Georgia,serif}}.sans{{font-family:Arial,sans-serif}}.muted{{fill:{MUTED}}}</style>']
    def text(x,y,s,size=16,cl='',anchor='start'):
        a.append(f'<text x="{x:.1f}" y="{y:.1f}" font-size="{size}" class="{cl}" text-anchor="{anchor}">'+html.escape(s)+'</text>')
    def line(x1,y1,x2,y2,c=GRID,dash=None,w=1):
        a.append(f'<path d="M{x1:.2f},{y1:.2f}L{x2:.2f},{y2:.2f}" stroke="{c}" stroke-width="{w}" fill="none"'+(f' stroke-dasharray="{dash}"' if dash else '')+'/>')
    def poly(p,c,w=2.2,dash=None):
        a.append('<path d="M'+'L'.join(f'{x:.1f},{y:.1f}' for x,y in p)+f'" stroke="{c}" stroke-width="{w}" fill="none"'+(f' stroke-dasharray="{dash}"' if dash else '')+'/>')
    def dot(x,y,c,r=4): a.append(f'<circle cx="{x:.1f}" cy="{y:.1f}" r="{r}" fill="{c}" stroke="{BG}" stroke-width="1"/>')
    if mobile:
        text(20,28,'PURE PART · 2T FIELD',11,'sans muted'); text(20,56,'Where the extra direction lives',20)
        PB=(56,W-24,150,400); PC=(56,W-24,600,850)
    else:
        text(36,32,'THE PURE (i, j, k) PART  ·  BINARY TETRAHEDRAL FIELD 2.26569.24t7.b.a',12,'sans muted')
        text(36,70,'Which L-functions carry the extra direction',24)
        text(1204,70,'[project, float numerics]',13,'sans muted','end')
        PB=(80,520,160,470); PC=(740,1200,160,470)
    def frame(box,xs,xlo,xhi,ylo,yhi,yt,fmt):
        L,Rr,T,B=box
        xx=lambda v:L+(v-xlo)/(xhi-xlo)*(Rr-L); yy=lambda v:B-(v-ylo)/(yhi-ylo)*(B-T)
        for v in yt:
            line(L,yy(v),Rr,yy(v),INK if v==0 else GRID,None,1.2 if v==0 else 1); text(L-8,yy(v)+4,fmt(v),10,'sans muted','end')
        for v in xs: line(xx(v),T,xx(v),B); text(xx(v),B+16,str(v),10,'sans muted','middle')
        text((L+Rr)/2,B+36,'T  (width of the test function)  →',11,'sans muted','middle')
        return xx,yy
    # (b)
    L,Rr,T,B=PB
    xx,yy=frame(PB,[1,2,3,4,5,6,7,8],0.7,8.3,-0.5,0.1,[-0.4,-0.2,0,],lambda v:f'{v:+.1f}' if v else '0')
    text(L-8,T-22,'(b) u-direction, share of each L-function',13,'sans')
    Tt=[r['T'] for r in E]
    ser=(('ζ',RUST,[r['u']['contrib_zero_minus_arch']['zeta'] for r in E]),
         ('L(ω) + L(ω̄)',GREEN,[r['u']['contrib_zero_minus_arch']['w']+r['u']['contrib_zero_minus_arch']['wbar'] for r in E]),
         ('−¼ L(ψ3)',GOLD,[r['u']['contrib_zero_minus_arch']['psi3'] for r in E]))
    for lab,c,v in ser:
        p=[(xx(t),yy(y)) for t,y in zip(Tt,v)]; poly(p,c,2.2)
        for q in p: dot(*q,c)
    ly=T+150 if not mobile else T+120
    LX=L+(Rr-L)*0.55
    for lab,c,v in ser:
        line(LX,ly,LX+20,ly,c,None,2.4); text(LX+26,ly+4,lab,11,'sans muted'); ly+=17
    text(L,B+54,'ζ supplies most of it; L(ρ) itself, weight 0, is not in the list.',11,'sans muted')
    # (c)
    L,Rr,T,B=PC
    xx,yy=frame(PC,[1,2,3,5,8],0.6,8.5,-6.5,6.5,[-6,-3,0,3,6],lambda v:f'{v:+d}' if v else '0')
    text(L-8,T-22,'(c) n-direction, upper zeros against lower zeros',13,'sans')
    Tt=[r['T'] for r in G]
    up=[r['n']['zero_pos_part'] for r in G]; lo=[r['n']['zero_neg_part'] for r in G]; sm=[r['n']['zero_side'] for r in G]
    for v,c,d in ((up,BLUE,None),(lo,RUST,None),(sm,INK,None)):
        p=[(xx(t),yy(y)) for t,y in zip(Tt,v)]; poly(p,c,2.2 if c!=INK else 2.8)
        for q in p: dot(*q,c,4 if c!=INK else 5)
    ly=T+14; LX=L+(Rr-L)*0.08
    for lab,c in (('zeros above the axis',BLUE),('zeros below the axis',RUST),('difference = n-direction',INK)):
        line(LX,ly,LX+20,ly,c,None,2.4); text(LX+26,ly+4,lab,11,'sans muted'); ly+=17
    x1=xx(1)
    bx=L+(Rr-L)*0.08; by=B-58
    text(bx,by,'at T = 1:',11,'sans muted')
    for j,(lab,c) in enumerate((('+0.940 above',BLUE),('−1.022 below',RUST),('−0.082 difference',INK))):
        text(bx+(70 if not mobile else 66),by+17*j,lab,11,'sans',fill=None) if False else (a.append(f'<text x="{bx+68:.1f}" y="{by+17*j:.1f}" font-size="11" class="sans" style="fill:{c}">{lab}</text>'))
    text(L,B+54,'Small difference of two large sums:' if mobile else 'A small difference of two large sums, from zeros that are not mirror-symmetric.',11,'sans muted')
    if mobile: text(L,B+70,'the zeros are not mirror-symmetric.',11,'sans muted')
    if mobile:
        for j,s in enumerate(('Floating-point PARI zeros to height 110, not interval','certificates. No claim about the Riemann Hypothesis.')): text(20,955+16*j,s,11,'sans muted')
    else:
        text(36,600,'Floating-point PARI zeros to height 110, not interval certificates. No claim about the Riemann Hypothesis.',12,'sans muted')
    a.append('</svg>'); return '\n'.join(a)
for m in (False,True):
    p=FIG/f'tetra-pure-zero-side-site{"-mobile" if m else ""}.svg'; p.write_text(make(m)); print('wrote',p)
