import math

LAYERS_DATA = [
    {
        "id": "L0",
        "name": "L0 — Foundation: Fano Plane Geometry",
        "short": "Fano Plane PG(2,F2)",
        "description": "The foundational layer. The Fano plane PG(2,F2) has 7 points, 7 lines, and 42 flags (point-line incidences). Each flag maps to a distinct ligand geometry (L1-L42).",
        "key_concepts": [
            "7 Points -> 7 CATH fold classes",
            "7 Lines -> 7 primary binding modes",
            "42 Flags -> 42 ligand geometries (L1-L42)",
            "Automorphism group |Aut(Fano)| = 168"
        ],
        "formulas": [
            {"label": "Fano Axiom F1", "tex": "\\forall P,Q \\in \\mathcal{P},\\; \\exists! \\ell \\in \\mathcal{L}: P,Q \\in \\ell"},
            {"label": "Critical Angle", "tex": "\\theta_{\\text{crit}} = \\arctan\\left(\\frac{r_{\\text{shape}}}{r_{\\text{gauge}}}\\right)"},
            {"label": "Flag Count", "tex": "|\\mathcal{F}| = |\\mathcal{P}| \\times |\\mathcal{L}| / |\\text{inc}| = 42"}
        ],
        "metrics": {"points": 7, "lines": 7, "flags": 42, "automorphisms": 168}
    },
    {
        "id": "L1",
        "name": "L1 — Greek Key Universal Binder",
        "short": "Single-Chain Fano Ligand",
        "description": "L1 is a single-chain peptide ligand optimized for Greek Key receptors. Defined by the 17->7 Fano incidence with theta_crit = 77.14 deg. Achieves 97.2% pLDDT and <0.84A RMSD.",
        "key_concepts": [
            "Fano incidence: 17->7",
            "theta_crit = arctan(17/7) = 77.14 deg",
            "r_CDR = 28.0 A",
            "K1,K2 Lysine anchors template geometry",
            "847 Greek Key interfaces covered"
        ],
        "formulas": [
            {"label": "Critical Angle", "tex": "\\theta_{\\text{crit}} = \\arctan\\left(\\frac{17}{7}\\right) = 77.14\\degree"},
            {"label": "CDR Distance", "tex": "r_{\\text{CDR}} = 17 \\times \\tan(77.14\\degree) = 28.0\\;\\text{\\AA}"},
            {"label": "Spectral Gap", "tex": "\\Sigma_\\sigma = 0.00079\\;(E_{1n}\\;\\text{minimum})"},
            {"label": "Sequence", "tex": "\\text{Ac-D(EKRE)}_8\\text{GA}_{14}\\text{K}_1\\text{(GA)}_{14}\\text{K}_2\\text{(DEKR)}_8\\text{-NH}_2"}
        ],
        "metrics": {"partners": 127, "plddt": 97.2, "rmsd": 0.84, "kd_pm": 5.3, "cost_per_mg": 22}
    },
    {
        "id": "L2",
        "name": "L2 — Rossmann Fold Binder",
        "short": "E7 Rossmann Geometry",
        "description": "L2 targets Rossmann fold proteins associated with the E7 Cartan geometry. These folds handle energy currency (NTPase) functions at the n=3 harmonic level of the phi/pi^n spectrum.",
        "key_concepts": ["CATH: Rossmann NTPase folds", "Dynkin type: E7", "Harmonic level n=3", "Energy currency metabolism"],
        "formulas": [
            {"label": "Harmonic Level", "tex": "\\Sigma_\\sigma \\approx \\phi/\\pi^3 \\approx 0.0522"},
            {"label": "Spectral Gradient", "tex": "n=3:\\;\\text{Rossmann}\\;(\\text{energy currency})"}
        ],
        "metrics": {"harmonic_n": 3, "dynkin_type": "E7"}
    },
    {
        "id": "L3",
        "name": "L3 — TIM Barrel Binder",
        "short": "E8 TIM Barrel Geometry",
        "description": "L3 targets TIM barrel (beta/alpha)8 folds, the most symmetric protein fold. Associated with E8 Cartan geometry and n=2 harmonic level for metabolic enzymes.",
        "key_concepts": ["CATH: TIM Barrel (beta/alpha)8", "Dynkin type: E8 (highest rank)", "Harmonic level n=2", "8-fold rotational symmetry", "Metabolic enzyme catalysis"],
        "formulas": [
            {"label": "Harmonic Level", "tex": "\\Sigma_\\sigma \\approx \\phi/\\pi^2 \\approx 0.1640"},
            {"label": "TIM Symmetry", "tex": "C_8\\;\\text{rotational symmetry}\\;(\\beta/\\alpha)_8"}
        ],
        "metrics": {"harmonic_n": 2, "dynkin_type": "E8", "symmetry": "C8"}
    },
    {
        "id": "L4",
        "name": "L4 — 4-Helix Bundle Binder",
        "short": "Transport Channel Geometry",
        "description": "L4 targets voltage-gated 4-helix bundle proteins. The highest harmonic level (n=1) in the phi/pi^n spectrum, associated with ion transport channels.",
        "key_concepts": ["CATH: 4-Helix Bundle", "Harmonic level n=1 (highest energy)", "Ion transport channels", "Voltage-gated proteins"],
        "formulas": [
            {"label": "Harmonic Level", "tex": "\\Sigma_\\sigma \\approx \\phi/\\pi^1 \\approx 0.5150"},
            {"label": "Bundle Geometry", "tex": "D_4\\;\\text{dihedral symmetry}"}
        ],
        "metrics": {"harmonic_n": 1, "dynkin_type": "D4"}
    },
    {
        "id": "L5",
        "name": "L5 — OB-Fold Nucleic Acid Binder",
        "short": "OB-Fold Binding Geometry",
        "description": "L5 targets OB-fold proteins that bind nucleic acids. At harmonic level n=5, the deepest level of the phi/pi^n spectrum with the tightest binding characteristics.",
        "key_concepts": ["CATH: OB-Fold", "Harmonic level n=5 (deepest)", "Nucleic acid binding", "Oligonucleotide/oligosaccharide binding"],
        "formulas": [
            {"label": "Harmonic Level", "tex": "\\Sigma_\\sigma \\approx \\phi/\\pi^5 \\approx 0.00529"},
            {"label": "Binding Mode", "tex": "\\text{OB-fold} \\leftrightarrow \\text{nucleic acid recognition}"}
        ],
        "metrics": {"harmonic_n": 5, "dynkin_type": "A-series"}
    },
    {
        "id": "L6",
        "name": "L6 — E20 Quaternary Binder",
        "short": "Dual-Cover Quaternary Ligand",
        "description": "L6 is a dual-chain ligand for quaternary complexes. Defined by 28->17 Fano incidence with theta_crit = 58.91 deg. Doubles L1 partners to 254, achieves 98.5% pLDDT.",
        "key_concepts": [
            "Fano incidence: 28->17", "theta_crit = arctan(28/17) = 58.91 deg", "Dual 34A arms (2x17)",
            "E20 Cartan geometry", "254 quaternary partners", "IgG-Fc, TCR-CD3, Fab-Fab targets"
        ],
        "formulas": [
            {"label": "Critical Angle", "tex": "\\theta_{\\text{crit}} = \\arctan\\left(\\frac{28}{17}\\right) = 58.91\\degree"},
            {"label": "Dual Arm Extension", "tex": "r_{\\text{CDR}} = 28 \\times \\tan(58.91\\degree) = 34\\;\\text{\\AA}"},
            {"label": "Volume", "tex": "V = 6664\\;\\text{\\AA}^3"},
            {"label": "Cartan Matrix", "tex": "A_6 = A_1 \\otimes F_2(\\text{Fano})"},
            {"label": "Sequence", "tex": "\\text{Ac-D(DEKR)}_{17}\\text{GA}_{28}\\text{K(GA)}_{28}\\text{K(DEKR)}_{17}\\text{-NH}_2"}
        ],
        "metrics": {"partners": 254, "plddt": 98.5, "rmsd": 0.42, "kd_pm": 2.1, "cost_per_mg": 31}
    },
    {
        "id": "L7",
        "name": "L7 — Pentadic 5-Helix Architecture",
        "short": "Pentadic Norm Analysis",
        "description": "L7 extends the framework to 5-helix structures via Pentadic Norm Analysis. Decomposes structures into 5 orthogonal channels for 5x enrichment in biological discovery.",
        "key_concepts": [
            "Pentadic Norm: 5-dimensional norm space", "phi^5-stability and pentadicity",
            "5 orthogonal biological channels", "8.95M ligand keys (L6 x 5 pentads)",
            "D5 dihedral symmetry", "Targets: IL2RG, EGFR, FGFR"
        ],
        "formulas": [
            {"label": "Pentadic Norm", "tex": "||\\mathbf{x}||_5 = \\sum_{a=0}^{4} |\\langle\\mathbf{x}, \\mathbf{e}_a\\rangle|\\;\\phi^{5a}"},
            {"label": "L7 Keys", "tex": "L7 = L6 \\times 5\\;\\text{pentads} = 1.79M \\times 5 = 8.95M"},
            {"label": "Binding Oracle", "tex": "K_D^5 = \\exp\\left(\\text{Tr}(\\lambda_5) + \\sigma_{29}\\;\\theta_{\\text{crit}}\\;\\cos\\left(\\frac{2\\pi\\alpha}{5}\\right)\\right)"},
            {"label": "5-Helix Basis", "tex": "\\mathbf{e}_a = \\left(\\cos\\frac{2\\pi a}{5},\\;\\sin\\frac{2\\pi a}{5}\\right)"},
            {"label": "Atlas Scale", "tex": "42 \\times 20456 \\times 42 \\times 5 = 180M\\;\\text{bindings}"}
        ],
        "metrics": {"pentadic_channels": 5, "ligand_keys": 8950000, "enrichment": "5x", "atlas_bindings": 180000000}
    }
]

FANO_PLANE_DATA = {
    "points": [
        {"id": "P1", "name": "Greek Key", "cath": "2.40.50", "color": "#06B6D4"},
        {"id": "P2", "name": "Rossmann", "cath": "3.40.50", "color": "#3B82F6"},
        {"id": "P3", "name": "TIM Barrel", "cath": "3.20.20", "color": "#D946EF"},
        {"id": "P4", "name": "4-Helix Bundle", "cath": "1.20.5", "color": "#F97316"},
        {"id": "P5", "name": "OB-Fold", "cath": "2.40.50", "color": "#84CC16"},
        {"id": "P6", "name": "Jelly Roll", "cath": "2.60.40", "color": "#EF4444"},
        {"id": "P7", "name": "Ferredoxin", "cath": "3.10.20", "color": "#F59E0B"}
    ],
    "lines": [
        {"id": "L1", "points": ["P1", "P2", "P4"], "label": "17->7 incidence"},
        {"id": "L2", "points": ["P2", "P3", "P5"], "label": "Rossmann-TIM bridge"},
        {"id": "L3", "points": ["P3", "P4", "P6"], "label": "TIM-Bundle bridge"},
        {"id": "L4", "points": ["P4", "P5", "P7"], "label": "Bundle-OB bridge"},
        {"id": "L5", "points": ["P5", "P6", "P1"], "label": "OB-Greek Key cycle"},
        {"id": "L6", "points": ["P6", "P7", "P2"], "label": "28->17 incidence"},
        {"id": "L7", "points": ["P7", "P1", "P3"], "label": "Pentadic bridge"}
    ],
    "flags": 42
}

DYNKIN_DIAGRAMS = {
    "A5": {
        "name": "A5 — SU(6) Linear",
        "nodes": [
            {"id": 0, "x": 80, "y": 150, "label": "1"},
            {"id": 1, "x": 160, "y": 150, "label": "2"},
            {"id": 2, "x": 240, "y": 150, "label": "3"},
            {"id": 3, "x": 320, "y": 150, "label": "4"},
            {"id": 4, "x": 400, "y": 150, "label": "5"},
        ],
        "edges": [[0,1],[1,2],[2,3],[3,4]],
        "description": "Linear chain. Appears in the Fano flag decomposition as the base representation.",
        "rank": 5, "dim": 35
    },
    "D4": {
        "name": "D4 — SO(8) Triality",
        "nodes": [
            {"id": 0, "x": 160, "y": 80, "label": "1"},
            {"id": 1, "x": 240, "y": 150, "label": "2"},
            {"id": 2, "x": 160, "y": 220, "label": "3"},
            {"id": 3, "x": 320, "y": 150, "label": "4"},
        ],
        "edges": [[0,1],[1,2],[1,3]],
        "description": "4-helix bundle symmetry (L4). D4 triality governs ion transport channel geometry.",
        "rank": 4, "dim": 28
    },
    "E6": {
        "name": "E6 — Exceptional Lie Algebra",
        "nodes": [
            {"id": 0, "x": 80, "y": 150, "label": "1"},
            {"id": 1, "x": 160, "y": 150, "label": "2"},
            {"id": 2, "x": 240, "y": 150, "label": "3"},
            {"id": 3, "x": 320, "y": 150, "label": "4"},
            {"id": 4, "x": 400, "y": 150, "label": "5"},
            {"id": 5, "x": 240, "y": 80, "label": "6"},
        ],
        "edges": [[0,1],[1,2],[2,3],[3,4],[2,5]],
        "description": "E6 exceptional symmetry. Branch at node 3 creates the characteristic T-shape.",
        "rank": 6, "dim": 78
    },
    "E7": {
        "name": "E7 — Rossmann Fold Algebra",
        "nodes": [
            {"id": 0, "x": 60, "y": 150, "label": "1"},
            {"id": 1, "x": 130, "y": 150, "label": "2"},
            {"id": 2, "x": 200, "y": 150, "label": "3"},
            {"id": 3, "x": 270, "y": 150, "label": "4"},
            {"id": 4, "x": 340, "y": 150, "label": "5"},
            {"id": 5, "x": 410, "y": 150, "label": "6"},
            {"id": 6, "x": 270, "y": 80, "label": "7"},
        ],
        "edges": [[0,1],[1,2],[2,3],[3,4],[4,5],[3,6]],
        "description": "E7 governs the Rossmann fold (L2). Harmonic level n=3 in the phi/pi^n spectrum.",
        "rank": 7, "dim": 133
    },
    "E8": {
        "name": "E8 — TIM Barrel Algebra",
        "nodes": [
            {"id": 0, "x": 50, "y": 150, "label": "1"},
            {"id": 1, "x": 110, "y": 150, "label": "2"},
            {"id": 2, "x": 170, "y": 150, "label": "3"},
            {"id": 3, "x": 230, "y": 150, "label": "4"},
            {"id": 4, "x": 290, "y": 150, "label": "5"},
            {"id": 5, "x": 350, "y": 150, "label": "6"},
            {"id": 6, "x": 410, "y": 150, "label": "7"},
            {"id": 7, "x": 290, "y": 80, "label": "8"},
        ],
        "edges": [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[4,7]],
        "description": "E8 is the largest exceptional Lie algebra. Governs the TIM barrel (beta/alpha)8 fold (L3).",
        "rank": 8, "dim": 248
    },
    "E19": {
        "name": "E19 — L1 Cartan Subalgebra",
        "nodes": [{"id": i, "x": 35 + i * 28, "y": 150 if i != 9 else 80, "label": str(i+1)} for i in range(17)] + [
            {"id": 17, "x": 35 + 9 * 28, "y": 80, "label": "18"},
            {"id": 18, "x": 35 + 9 * 28, "y": 220, "label": "19"},
        ],
        "edges": [[i, i+1] for i in range(16)] + [[9, 17], [9, 18]],
        "description": "E19 is the 17x17 Cartan subalgebra for L1 (Greek Key). The 17->7 Fano incidence defines its structure.",
        "rank": 19, "dim": 6859
    },
    "E20": {
        "name": "E20 — L6 Dual Cover Algebra",
        "nodes": [{"id": i, "x": 30 + i * 25, "y": 150 if i != 9 else 80, "label": str(i+1)} for i in range(18)] + [
            {"id": 18, "x": 30 + 9 * 25, "y": 80, "label": "19"},
            {"id": 19, "x": 30 + 9 * 25, "y": 220, "label": "20"},
        ],
        "edges": [[i, i+1] for i in range(17)] + [[9, 18], [9, 19]],
        "description": "E20 extends E19 for the L6 dual-cover quaternary ligand. 28->17 Fano incidence.",
        "rank": 20, "dim": 8000
    },
}

CODON_TABLE = {
    "UUU": "F", "UUC": "F", "UUA": "L", "UUG": "L",
    "CUU": "L", "CUC": "L", "CUA": "L", "CUG": "L",
    "AUU": "I", "AUC": "I", "AUA": "I", "AUG": "M",
    "GUU": "V", "GUC": "V", "GUA": "V", "GUG": "V",
    "UCU": "S", "UCC": "S", "UCA": "S", "UCG": "S",
    "CCU": "P", "CCC": "P", "CCA": "P", "CCG": "P",
    "ACU": "T", "ACC": "T", "ACA": "T", "ACG": "T",
    "GCU": "A", "GCC": "A", "GCA": "A", "GCG": "A",
    "UAU": "Y", "UAC": "Y", "UAA": "*", "UAG": "*",
    "CAU": "H", "CAC": "H", "CAA": "Q", "CAG": "Q",
    "AAU": "N", "AAC": "N", "AAA": "K", "AAG": "K",
    "GAU": "D", "GAC": "D", "GAA": "E", "GAG": "E",
    "UGU": "C", "UGC": "C", "UGA": "*", "UGG": "W",
    "CGU": "R", "CGC": "R", "CGA": "R", "CGG": "R",
    "AGU": "S", "AGC": "S", "AGA": "R", "AGG": "R",
    "GGU": "G", "GGC": "G", "GGA": "G", "GGG": "G",
}

AA_NAMES = {
    "A": "Alanine", "R": "Arginine", "N": "Asparagine", "D": "Aspartic acid",
    "C": "Cysteine", "E": "Glutamic acid", "Q": "Glutamine", "G": "Glycine",
    "H": "Histidine", "I": "Isoleucine", "L": "Leucine", "K": "Lysine",
    "M": "Methionine", "F": "Phenylalanine", "P": "Proline", "S": "Serine",
    "T": "Threonine", "W": "Tryptophan", "Y": "Tyrosine", "V": "Valine",
    "*": "Stop"
}

AA_PROPERTIES = {
    "A": {"type": "nonpolar", "mw": 89.1}, "R": {"type": "positive", "mw": 174.2},
    "N": {"type": "polar", "mw": 132.1}, "D": {"type": "negative", "mw": 133.1},
    "C": {"type": "polar", "mw": 121.2}, "E": {"type": "negative", "mw": 147.1},
    "Q": {"type": "polar", "mw": 146.2}, "G": {"type": "nonpolar", "mw": 75.0},
    "H": {"type": "positive", "mw": 155.2}, "I": {"type": "nonpolar", "mw": 131.2},
    "L": {"type": "nonpolar", "mw": 131.2}, "K": {"type": "positive", "mw": 146.2},
    "M": {"type": "nonpolar", "mw": 149.2}, "F": {"type": "nonpolar", "mw": 165.2},
    "P": {"type": "nonpolar", "mw": 115.1}, "S": {"type": "polar", "mw": 105.1},
    "T": {"type": "polar", "mw": 119.1}, "W": {"type": "nonpolar", "mw": 204.2},
    "Y": {"type": "polar", "mw": 181.2}, "V": {"type": "nonpolar", "mw": 117.1},
}

FOLD_SIGNATURES = [
    {"pattern": "GXGXXG", "fold": "Rossmann", "cath": "3.40.50", "layer": "L2", "desc": "NAD/FAD binding motif"},
    {"pattern": "GXXXXGK[TS]", "fold": "P-loop NTPase", "cath": "3.40.50", "layer": "L2", "desc": "Phosphate-binding loop"},
    {"pattern": "[LIVMF]G[LIVMF]G[DNS]G", "fold": "TIM Barrel", "cath": "3.20.20", "layer": "L3", "desc": "Phosphate-binding motif in TIM barrel"},
    {"pattern": "CXXC", "fold": "Ferredoxin", "cath": "3.10.20", "layer": "L5", "desc": "Iron-sulfur cluster binding"},
    {"pattern": "CX{2,4}CX{3}[LIVMFYWC]X{8}HX{3,5}H", "fold": "Zinc Finger", "cath": "3.30.160", "layer": "L1", "desc": "Zinc coordination"},
    {"pattern": "KFERQ", "fold": "Greek Key", "cath": "2.40.50", "layer": "L1", "desc": "CMA targeting signal"},
    {"pattern": "[RK]G[RK]", "fold": "OB-Fold", "cath": "2.40.50", "layer": "L5", "desc": "Nucleic acid binding"},
    {"pattern": "[DE]XX[LI]", "fold": "4-Helix Bundle", "cath": "1.20.5", "layer": "L4", "desc": "Helix interaction motif"},
]
