"""Exact exceptional graph labels and branch-compression certificate. CC0."""
from pathlib import Path
from html import escape

parts = [
    '<svg xmlns="http://www.w3.org/2000/svg" width="540" height="1000" viewBox="0 0 540 1000" role="img" aria-labelledby="title desc">',
    '<title id="title">One finite branch test proves E6 and E8 flatness</title>',
    '<desc id="desc">E6 is the chain0 to4 with short tip5 at branch2; E8 is the chain0 to6 with short tip7 at branch4. The blue shortest root-to-short-tip paths have lengths3 and5 and define the extra projections rho. Each axis is generated by cups and rho. The compression C=PBP is a partial isometry at every endpoint: E6 sizes1,3,1 and ranks1,0,1; E8 sizes1,5,11,4 and ranks1,0,1,0. The E8 endpoint4 block has rank one and nonzero squared singular value one, while its suffix Wenzl projection has rank two. Hence P commutes with B adjoint P B and both rooted connections are flat. Lemmas38.1–38.2 and Proposition38.3.</desc>',
    '<rect width="540" height="1000" fill="#fbfcff"/>',
    '<style>text{font-family:Arial,sans-serif;fill:#172c45}.title{font-size:20px;font-weight:bold}.heading{font-size:18px;font-weight:bold}.label{font-size:17px}.small{font-size:16px}</style>'
]

def text(x, y, value, cls="label", anchor="middle"):
    parts.append(f'<text x="{x}" y="{y}" class="{cls}" text-anchor="{anchor}">{escape(value)}</text>')

def edge(x1, y1, x2, y2, blue=False):
    parts.append(f'<path d="M{x1} {y1} L{x2} {y2}" stroke="{("#287c99" if blue else "#9baec5")}" stroke-width="{(4 if blue else 3)}"/>')

def vertex(x, y, label, root=False, labeldx=0, labeldy=-17):
    parts.append(f'<circle cx="{x}" cy="{y}" r="8" fill="{("#287c99" if root else "#172c45")}"/>')
    text(x+labeldx, y+labeldy, str(label))

def box(y, title, lines, height):
    parts.append(f'<rect x="24" y="{y}" width="492" height="{height}" rx="8" fill="#edf3fb" stroke="#7c9bbd"/>')
    text(270, y+29, title, "heading")
    for j, line in enumerate(lines):
        text(270, y+58+26*j, line)

text(24, 33, "The exceptional branch test is finite and exact", "title", "start")
text(270, 78, "E₆: k = 3, h = 12, root 0", "heading")
xs = [60, 165, 270, 375, 480]
for j in range(4):
    edge(xs[j], 125, xs[j+1], 125, j < 2)
edge(xs[2], 125, xs[2], 205, True)
for j, x in enumerate(xs):
    vertex(x, 125, j, j == 0)
vertex(xs[2], 205, 5, labeldx=19, labeldy=6)
text(390, 192, "ξ = (0,1,2,5)", "label")
text(270, 249, "Blue path defines ρ = [ξ,ξ].", "label")
text(270, 295, "E₈: k = 5, h = 30, root 0", "heading")
xs = [60+70*j for j in range(7)]
for j in range(6):
    edge(xs[j], 342, xs[j+1], 342, j < 4)
edge(xs[4], 342, xs[4], 424, True)
for j, x in enumerate(xs):
    vertex(x, 342, j, j == 0)
vertex(xs[4], 424, 7, labeldx=19, labeldy=6)
text(182, 416, "ξ = (0,1,2,3,4,7)", "label")
box(463, "Cups and one branch projection generate each axis", [
    "Only [ρᵥ,ρₕ] remains to be tested.",
    "P = prefix ξ;  Q = B* P B;  C = P B P."
], 116)
box(599, "Every endpoint compression is a partial isometry", [
    "E₆ endpoints: 0, 2, 4; sizes: 1, 3, 1.",
    "Ranks: 1, 0, 1; nonzero squared singular value = 1.",
    "E₈ endpoints: 0, 2, 4, 6; sizes: 1, 5, 11, 4.",
    "Ranks: 1, 0, 1, 0; nonzero squared singular value = 1."
], 167)
box(786, "The E₈ eleven-by-eleven block is the essential test", [
    "C = a b, and ‖a‖²G · ‖b‖²G⁻¹ = 1 exactly.",
    "C has rank 1; its suffix Wenzl projection has rank 2."
], 116)
text(270, 935, "C* C is a projection ⇒ [P,Q] = 0 ⇒ flat axes.", "heading")
text(270, 972, "Indices: 2+√3 and 4cos²(π/30); depths: 4 and 6.", "small")
parts.append("</svg>")
Path(__file__).with_suffix(".svg").write_text("\n".join(parts)+"\n", encoding="utf-8")
