"""v7 palette — cool-tech light theme. Proves WCAG AA before HTML write."""
import colour
import numpy as np

def hex_rgb(h):
    h = h.lstrip("#")
    return np.array([int(h[i:i+2], 16)/255 for i in (0,2,4)])
def srgb_hex(rgb):
    rgb = np.clip(rgb, 0, 1)
    return "#" + "".join(f"{round(c*255):02X}" for c in rgb)
def oklch_to_hex(L, C, H):
    H = np.deg2rad(H)
    Lab = np.array([L, C*np.cos(H), C*np.sin(H)])
    XYZ = colour.Oklab_to_XYZ(Lab)
    rgb_lin = colour.XYZ_to_sRGB(XYZ, apply_cctf_encoding=False)
    rgb = colour.cctf_encoding(np.clip(rgb_lin, 0, 1), function='sRGB')
    return srgb_hex(rgb), rgb
def lum(rgb):
    rgb = np.clip(rgb, 0, 1)
    rl = np.where(rgb <= 0.03928, rgb/12.92, ((rgb+0.055)/1.055)**2.4)
    return float(0.2126*rl[0] + 0.7152*rl[1] + 0.0722*rl[2])
def cr(a, b):
    L1, L2 = lum(a), lum(b)
    if L1 < L2: L1, L2 = L2, L1
    return (L1 + 0.05) / (L2 + 0.05)

# Cool-tech tokens — все вокруг hue=240 (нейтральный синеватый), чтобы поверхность читалась
# как "лабораторная" а не "тёплая бумага". Brand accent — Teal #0F6E56 из брендбука,
# его НЕ трогаем.

print("=" * 80)
print("v7 cool-tech tokens (Page surface — neutral cool, hue=240)")
print("=" * 80)

# Светлые поверхности
bg_hex,        bg_rgb        = oklch_to_hex(0.985, 0.003, 240)   # page bg
surface_hex,   surface_rgb   = oklch_to_hex(1.000, 0.000, 240)   # raised — pure white
sunken_hex,    sunken_rgb    = oklch_to_hex(0.970, 0.004, 240)   # sunken
hairline_hex,  hairline_rgb  = oklch_to_hex(0.910, 0.005, 240)   # hairline (UI border, not text)

# Текст
ink_hex,       ink_rgb       = oklch_to_hex(0.180, 0.018, 240)   # primary text
ink2_hex,      ink2_rgb      = oklch_to_hex(0.350, 0.014, 240)   # secondary
ink3_hex,      ink3_rgb      = oklch_to_hex(0.510, 0.012, 240)   # muted (must AA on bg)
inkd_hex,      inkd_rgb      = oklch_to_hex(0.620, 0.008, 240)   # disabled (3:1+)

# Brand accent (Teal #0F6E56) — НЕ пересчитываем, фиксируем из брендбука
accent_rgb = hex_rgb('#0F6E56')

palette = [
    ('bg',          bg_hex,       bg_rgb),
    ('surface',     surface_hex,  surface_rgb),
    ('sunken',      sunken_hex,   sunken_rgb),
    ('hairline',    hairline_hex, hairline_rgb),
    ('ink',         ink_hex,      ink_rgb),
    ('ink-2',       ink2_hex,     ink2_rgb),
    ('ink-3',       ink3_hex,     ink3_rgb),
    ('ink-disabled',inkd_hex,     inkd_rgb),
    ('accent (teal)','#0F6E56',   accent_rgb),
]
for name, h, _ in palette:
    print(f"  {name:<14} {h}")

print()
print("WCAG verifications — text colour on bg/surface/sunken")
print("=" * 80)
checks = [
    ('ink / bg',        ink_rgb, bg_rgb,       4.5),
    ('ink / surface',   ink_rgb, surface_rgb,  4.5),
    ('ink / sunken',    ink_rgb, sunken_rgb,   4.5),
    ('ink-2 / bg',      ink2_rgb, bg_rgb,      4.5),
    ('ink-2 / sunken',  ink2_rgb, sunken_rgb,  4.5),
    ('ink-3 / bg',      ink3_rgb, bg_rgb,      4.5),
    ('ink-3 / surface', ink3_rgb, surface_rgb, 4.5),
    ('ink-3 / sunken',  ink3_rgb, sunken_rgb,  4.5),
    ('ink-disabled / bg', inkd_rgb, bg_rgb,    3.0),  # disabled — 3:1 OK
    ('accent / bg',     accent_rgb, bg_rgb,    4.5),
    ('accent / surface',accent_rgb, surface_rgb, 4.5),
    ('white-on-accent (CTA text)', np.array([1,1,1]), accent_rgb, 4.5),
    ('white-on-ink (active nav)',  np.array([1,1,1]), ink_rgb, 4.5),
    ('hairline / bg (3:1 UI)',     hairline_rgb, bg_rgb, 3.0),
]
for name, a, b, target in checks:
    r = cr(a, b)
    flag = '✓' if r >= target else 'FAIL'
    target_str = f"≥{target}"
    print(f"  {name:<35} {r:5.2f}:1   {target_str:>5}  {flag}")
