"""One menu-only ± tile, without changing any native font/resource allocation.

The native text format treats high-bit bytes as positioning commands. ASCII '~'
is an otherwise blank internal token. A guarded native character lookup selects
the one scratch tile only for that token in the small-font path; every original
character and font resource stays byte-identical.
"""
ORIGINAL = 0xBE26A
GLYPH = 0x25A000

def install(data, patch, with_cosmetic_ui=False):
    glyph = bytearray(data[ORIGINAL + 10:ORIGINAL + 42])
    foreground = {(3, 1), (3, 2), (3, 4)}
    foreground.update((x, y) for y in (3, 6) for x in range(1, 6))
    shadow = {(x + 1, y + 1) for x, y in foreground if x < 7 and y < 7} - foreground
    for colour, pixels in ((4, shadow), (1, foreground)):
        for x, y in pixels:
            offset = 4 * y + x // 2
            shift = 0 if x & 1 else 4
            glyph[offset] = (glyph[offset] & ~(15 << shift)) | (colour << shift)
    patch(GLYPH, b'\xff' * 32, glyph)
    patch(0x11C38, bytes.fromhex('3032000408380003c2f8'), bytes.fromhex('4eb9002580144e714e71'))

    if with_cosmetic_ui:
        # The native ASCII$ tile is a copyright-like glyph, not a dollar sign.
        # A modal-owned scratch glyph avoids changing that native font globally.
        rows=['00011000','00111110','01111000','00111100','00011110','01111100','00011000','00000000']
        foreground={(x,y)for y,row in enumerate(rows)for x,c in enumerate(row)if c=='1'}
        shadow={(x+1,y+1)for x,y in foreground if x<7 and y<7}-foreground
        dollar=bytearray(data[0xAAC52+10:0xAAC52+42])
        for colour,pixels in[(4,shadow),(1,foreground)]:
            for x,y in pixels:
                at=4*y+x//2;shift=0 if x&1 else 4;dollar[at]=(dollar[at]&~(15<<shift))|(colour<<shift)
        patch(GLYPH+32,b'\xff'*32,dollar)
