"""One top bar everywhere (19 Nov 2026). Your page, ATO dashboard, Settings and Sign in must draw the same bar:
same height, same title font, same 44px back and gear buttons in the same places, gear icon identical.
Also Settings itself: 0 errors, 0 overflow, at 390 / 820 / 1440 in all four themes.
Run: python3 -m http.server 8952 (this folder), then python3 tests/verify_bar.py"""
import os, sys, json
from playwright.sync_api import sync_playwright
EXE = "/home/box/.cache/ms-playwright/chromium-1243/chrome-linux64/chrome"
BASE = os.environ.get("BASE", "http://127.0.0.1:8952/")
PAGES = ["user/", "ato/", "settings/", "auth/"]
PROBE = """() => {
  const bar = document.querySelector('header.bar'), t = bar.querySelector('h1,.ttl'), b = document.getElementById('back'), g = document.getElementById('gear');
  const cs = getComputedStyle(t), r = x => { const q = x.getBoundingClientRect(); return [Math.round(q.left), Math.round(q.top), Math.round(q.width), Math.round(q.height)]; };
  const d = document.documentElement, out = [];
  if (d.scrollWidth > d.clientWidth) out.push('overflow ' + d.scrollWidth);
  return { h: Math.round(bar.getBoundingClientRect().height), font: cs.fontFamily + ' ' + cs.fontWeight + ' ' + cs.fontSize + '/' + cs.lineHeight,
           back: r(b), gear: r(g), icon: g.querySelector('svg').innerHTML.length, label: g.getAttribute('aria-label'), out };
}"""
fails, runs = [], 0
with sync_playwright() as p:
    br = p.chromium.launch(executable_path=EXE)
    for w, h in [(390, 844), (820, 1180), (1440, 900)]:
        for t in ["pearl", "champagne", "midnight", "amethyst"]:
            ref = None
            for pgname in PAGES:
                runs += 1
                pg = br.new_page(viewport={"width": w, "height": h}); errs = []
                pg.on("console", lambda m: m.type == "error" and errs.append(m.text)); pg.on("pageerror", lambda e: errs.append(str(e)))
                pg.goto(f"{BASE}{pgname}?t={t}&capture=1"); pg.wait_for_timeout(700)
                r = pg.evaluate(PROBE); bad = list(r["out"]) + (["errors " + "; ".join(errs)] if errs else [])
                if pgname == "settings/":
                    for sel in ["#account", "#plan", "#delete"]:
                        if not pg.query_selector(sel): bad.append("missing " + sel)
                    pg.click('[data-theme="pearl"]' if t != "pearl" else '[data-theme="midnight"]'); pg.wait_for_timeout(200)
                    if pg.evaluate("document.documentElement.dataset.t") == t: bad.append("theme pick did nothing")
                if pgname == "auth/":
                    pg.click("#gear"); pg.wait_for_timeout(500)
                    if not pg.evaluate("document.documentElement.classList.contains('sheet-open')"): bad.append("gear sheet did not open")
                key = {k: r[k] for k in ("h", "font", "back", "gear", "icon", "label")}
                if ref is None: ref = key
                elif key != ref: bad.append(f"bar differs from user page: {key} vs {ref}")
                if bad: fails.append(f"{w} {t} {pgname}: {bad}")
                pg.close()
    br.close()
print(f"{runs} runs, {len(fails)} failed"); [print(" -", f) for f in fails]
sys.exit(1 if fails else 0)
