"""
Quiet Kiln — Visual Board for doany.ai 2026 Design Direction
Third pass: fixed arches, refined spacing, museum polish.
"""

from PIL import Image, ImageDraw, ImageFont
import math
import random

random.seed(42)

W, H = 2400, 3200
img = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(img)

# --- Palette ---
BG_DEEP       = (20, 18, 21)
BG_WARM       = (28, 25, 27)
CHARCOAL      = (44, 40, 42)
GRAPHITE      = (62, 56, 58)
WARM_STONE    = (148, 132, 118)
CLAY          = (182, 152, 126)
ASH           = (206, 196, 186)
CREAM         = (232, 225, 215)
CELADON       = (138, 168, 150)
OXIDE         = (172, 112, 90)
BRONZE_LIGHT  = (192, 172, 144)
TEXT_DIM      = (82, 74, 70)
TEXT_LIGHT    = (188, 178, 168)
GRID_LINE     = (32, 29, 31)

# --- Fonts ---
def lf(name, size):
    try: return ImageFont.truetype(name, size)
    except: return ImageFont.load_default()

f_movement = lf("Didot", 108)
f_accent   = lf("Didot", 34)
f_subtitle = lf("Avenir Next", 24)
f_caption  = lf("Optima", 17)
f_label    = lf("Avenir", 14)
f_label_xs = lf("Avenir", 12)
f_marker   = lf("Futura", 11)
f_year     = lf("Avenir Next", 18)
f_brand    = lf("Avenir Next", 26)
f_qk       = lf("Didot", 52)

# --- Background ---
for y in range(H):
    t = y / H
    r = int(BG_DEEP[0] + (BG_WARM[0] - BG_DEEP[0]) * t * 0.5)
    g = int(BG_DEEP[1] + (BG_WARM[1] - BG_DEEP[1]) * t * 0.5)
    b = int(BG_DEEP[2] + (BG_WARM[2] - BG_DEEP[2]) * t * 0.5)
    draw.line([(0, y), (W, y)], fill=(r, g, b))

grain = Image.new("RGB", (W, H))
gd = ImageDraw.Draw(grain)
for _ in range(100000):
    gx, gy = random.randint(0, W-1), random.randint(0, H-1)
    v = random.randint(-5, 5)
    gd.point((gx, gy), fill=(128+v, 128+v, 128+v))
img = Image.blend(img, grain, 0.025)
draw = ImageDraw.Draw(img)

# --- Helpers ---

def vessel_circle(cx, cy, radius, color):
    # Shadow
    for s in range(8, 0, -1):
        sc = tuple(max(0, BG_DEEP[i] - int(s*1.5)) for i in range(3))
        draw.ellipse([cx-radius+s//3, cy-radius+2+s//3, cx+radius+s//3, cy+radius+2+s//3], fill=sc)
    # Radial gradient sphere
    for r in range(radius, 0, -1):
        t = r / radius
        c = tuple(int(color[i] * (0.6 + 0.4 * (1 - t))) for i in range(3))
        draw.ellipse([cx-r, cy-r, cx+r, cy+r], fill=c)
    # Highlight
    hl_r = radius // 3
    hx, hy = cx - radius // 5, cy - radius // 5
    for r in range(hl_r, 0, -1):
        t = 1 - r / hl_r
        c = tuple(min(255, int(color[i] + (255 - color[i]) * 0.2 * t)) for i in range(3))
        draw.ellipse([hx-r, hy-r, hx+r, hy+r], fill=c)

def vessel_rect(cx, cy, w, h, color, radius=8):
    x, y = cx - w//2, cy - h//2
    sc = tuple(max(0, BG_DEEP[i] - 8) for i in range(3))
    draw.rounded_rectangle([x+3, y+4, x+w+3, y+h+4], radius=radius, fill=sc)
    draw.rounded_rectangle([x, y, x+w, y+h], radius=radius, fill=color)
    band_h = h // 5
    for row in range(band_h):
        t = 1 - row / band_h
        c = tuple(min(255, int(color[i] + (255 - color[i]) * 0.1 * t)) for i in range(3))
        inset = 0 if row > radius else int(radius - math.sqrt(max(0, radius**2 - (radius - row)**2)))
        draw.line([(x+inset, y+row), (x+w-inset, y+row)], fill=c)

def vessel_arch(cx, cy_base, w, h, color):
    """Proper arch: a tall rounded-top shape like a kiln window or vessel silhouette."""
    # Build as a filled polygon with a semicircular top
    half_w = w // 2
    arch_top = cy_base - h
    straight_h = h * 3 // 5  # straight walls
    curve_h = h - straight_h  # domed top

    # Shadow
    sc = tuple(max(0, BG_DEEP[i] - 6) for i in range(3))
    # Shadow polygon
    pts_s = []
    for deg in range(180, -1, -5):
        a = math.radians(deg)
        px = (cx + 3) + int(half_w * math.cos(a))
        py = (arch_top + curve_h + 3) - int(curve_h * math.sin(a))
        pts_s.append((px, py))
    pts_s.append(((cx+3) + half_w, cy_base + 3))
    pts_s.append(((cx+3) - half_w, cy_base + 3))
    draw.polygon(pts_s, fill=sc)

    # Main shape polygon
    pts = []
    for deg in range(180, -1, -5):
        a = math.radians(deg)
        px = cx + int(half_w * math.cos(a))
        py = (arch_top + curve_h) - int(curve_h * math.sin(a))
        pts.append((px, py))
    pts.append((cx + half_w, cy_base))
    pts.append((cx - half_w, cy_base))
    draw.polygon(pts, fill=color)

    # Top highlight gradient
    hl_c = tuple(min(255, c + 16) for c in color)
    hl_h = curve_h // 2
    for row in range(hl_h):
        t = 1 - row / hl_h
        yy = arch_top + row
        # Width at this height in the dome
        if row < curve_h:
            dome_frac = math.sqrt(max(0, 1 - ((curve_h - row) / curve_h)**2))
            hw = int(half_w * dome_frac * 0.6)
        else:
            hw = int(half_w * 0.6)
        c = tuple(min(255, int(color[i] + (hl_c[i] - color[i]) * t * 0.6)) for i in range(3))
        if hw > 0:
            draw.line([(cx - hw, yy), (cx + hw, yy)], fill=c)

def vessel_ring(cx, cy, outer_r, inner_r, color):
    vessel_circle(cx, cy, outer_r, color)
    bg_at = tuple(int(BG_DEEP[i] * 0.92 + color[i] * 0.08) for i in range(3))
    for r in range(inner_r + 3, inner_r - 1, -1):
        t = (r - inner_r + 1) / 4
        c = tuple(int(bg_at[i] * (1 - t) + color[i] * t * 0.4) for i in range(3))
        draw.ellipse([cx-r, cy-r, cx+r, cy+r], fill=c)
    draw.ellipse([cx-inner_r, cy-inner_r, cx+inner_r, cy+inner_r], fill=bg_at)

def vessel_diamond(cx, cy, size, color):
    pts = [(cx, cy-size), (cx+size, cy), (cx, cy+size), (cx-size, cy)]
    sc = tuple(max(0, BG_DEEP[i]-6) for i in range(3))
    draw.polygon([(p[0]+3, p[1]+3) for p in pts], fill=sc)
    draw.polygon(pts, fill=color)
    draw.polygon([(cx, cy-size), (cx-size, cy), (cx, cy)],
                 fill=tuple(min(255, c+12) for c in color))

def cross_marker(cx, cy, size, color):
    draw.line([(cx-size, cy), (cx+size, cy)], fill=color, width=1)
    draw.line([(cx, cy-size), (cx, cy+size)], fill=color, width=1)

def hline(y, x0, x1, color, w=1):
    draw.line([(x0, y), (x1, y)], fill=color, width=w)

# --- Layout ---
M = 150

# ============================================================
# TITLE
# ============================================================
draw.text((M, 150), "QUIET", font=f_movement, fill=WARM_STONE)
qb = draw.textbbox((M, 150), "QUIET", font=f_movement)
draw.text((qb[2] + 24, 150), "KILN", font=f_movement, fill=CLAY)

hline(290, M, M + 420, GRAPHITE)
draw.text((M, 314), "A Design Philosophy for doany.ai", font=f_subtitle, fill=TEXT_DIM)
draw.text((M + 440, 318), "2026", font=f_year, fill=TEXT_DIM)
draw.text((W-M-110, 168), "04.2026", font=f_year, fill=(50,46,48))
cross_marker(W-M-18, 178, 5, (50,46,48))

# ============================================================
# VESSEL TAXONOMY — 5x4 grid
# ============================================================
IW = W - 2*M
GRID_TOP = 420
GRID_H = 1680
COLS, ROWS = 5, 4
CW = IW // COLS
CH = GRID_H // ROWS

# Grid lines
for c in range(COLS+1):
    x = M + c * CW
    draw.line([(x, GRID_TOP), (x, GRID_TOP+GRID_H)], fill=GRID_LINE, width=1)
for r in range(ROWS+1):
    y = GRID_TOP + r * CH
    draw.line([(M, y), (W-M, y)], fill=GRID_LINE, width=1)

# Row 0: Spheres
for col, (color, rad) in enumerate([
    (CLAY, 62), (WARM_STONE, 48), (CELADON, 56), (OXIDE, 44), (BRONZE_LIGHT, 52)
]):
    cx = M + col*CW + CW//2
    cy = GRID_TOP + CH//2
    vessel_circle(cx, cy, rad, color)
    draw.ellipse([cx-rad+14, cy-rad+14, cx+rad-14, cy+rad-14],
                 outline=tuple(max(0, c-30) for c in color), width=1)

# Row 1: Tiles
for col, (color, w, h, rad) in enumerate([
    (WARM_STONE, 88, 108, 6), (CLAY, 64, 118, 10), (ASH, 96, 72, 4),
    (CELADON, 74, 100, 8), (GRAPHITE, 88, 88, 6)
]):
    cx = M + col*CW + CW//2
    cy = GRID_TOP + CH + CH//2
    vessel_rect(cx, cy, w, h, color, rad)

# Row 2: Arches — proper kiln window shapes
for col, (color, w, h) in enumerate([
    (OXIDE, 72, 120), (WARM_STONE, 60, 100), (BRONZE_LIGHT, 80, 130),
    (CLAY, 56, 95), (CELADON, 66, 110)
]):
    cx = M + col*CW + CW//2
    cy_base = GRID_TOP + 2*CH + CH//2 + h//3
    vessel_arch(cx, cy_base, w, h, color)

# Row 3: Rings + Diamonds
for col, (shape, color, p1, p2) in enumerate([
    ("ring", WARM_STONE, 40, 17),
    ("diamond", CLAY, 38, 0),
    ("ring", CELADON, 44, 19),
    ("diamond", OXIDE, 34, 0),
    ("ring", BRONZE_LIGHT, 42, 18),
]):
    cx = M + col*CW + CW//2
    cy = GRID_TOP + 3*CH + CH//2
    if shape == "ring":
        vessel_ring(cx, cy, p1, p2, color)
    else:
        vessel_diamond(cx, cy, p1, color)

# Labels
for col in range(COLS):
    lbl = ["i.", "ii.", "iii.", "iv.", "v."][col]
    lx = M + col*CW + CW//2
    bb = draw.textbbox((0,0), lbl, font=f_marker)
    draw.text((lx - (bb[2]-bb[0])//2, GRID_TOP+GRID_H+14), lbl, font=f_marker, fill=TEXT_DIM)

for row in range(ROWS):
    lbl = ["form", "surface", "profile", "detail"][row]
    ly = GRID_TOP + row*CH + CH//2
    bb = draw.textbbox((0,0), lbl, font=f_marker)
    draw.text((M-55, ly - (bb[3]-bb[1])//2), lbl, font=f_marker, fill=TEXT_DIM)

# ============================================================
# PALETTE
# ============================================================
PAL_Y = 2180
SW = 48; GAP = 24
draw.text((M, PAL_Y-28), "material palette", font=f_label, fill=TEXT_DIM)
for i, (color, name) in enumerate([
    (BG_DEEP, "ground"), (CHARCOAL, "depth"), (GRAPHITE, "edge"),
    (WARM_STONE, "stone"), (CLAY, "clay"), (BRONZE_LIGHT, "bronze"),
    (ASH, "ash"), (CREAM, "bone"), (CELADON, "celadon"), (OXIDE, "oxide"),
]):
    sx = M + i*(SW+GAP)
    sy = PAL_Y + 6
    draw.rounded_rectangle([sx, sy, sx+SW, sy+SW], radius=3, fill=color)
    if sum(color) < 130:
        draw.rounded_rectangle([sx, sy, sx+SW, sy+SW], radius=3, outline=(48,44,46), width=1)
    bb = draw.textbbox((0,0), name, font=f_label_xs)
    draw.text((sx + SW//2 - (bb[2]-bb[0])//2, sy+SW+8), name, font=f_label_xs, fill=TEXT_DIM)

# ============================================================
# QUOTE
# ============================================================
QY = 2340
hline(QY, M, M+280, GRAPHITE)
for i, (txt, fnt, col) in enumerate([
    ("The kiln does not rush.", f_accent, CREAM),
    ("It holds temperature with unwavering patience", f_caption, TEXT_LIGHT),
    ("until raw material becomes something irreversible.", f_caption, TEXT_LIGHT),
]):
    draw.text((M, QY+22 + i*40), txt, font=fnt, fill=col)

# ============================================================
# TYPOGRAPHY SPECIMEN — below quote, left side
# ============================================================
TY = 2490
draw.text((M, TY), "Didot  /  Avenir Next  /  Optima", font=f_label, fill=TEXT_DIM)
bar_data = [(WARM_STONE, 200), (CLAY, 140), (CELADON, 90)]
for j, (bc, bw) in enumerate(bar_data):
    by = TY + 28 + j * 16
    faded = tuple(int(bc[k] * 0.35) for k in range(3))
    draw.rounded_rectangle([M, by, M+bw, by+4], radius=2, fill=faded)

# ============================================================
# HERO VESSEL
# ============================================================
HCX = W - M - 240
HCY = 2520
HR = 155

# Glow
for r in range(HR+50, HR, -1):
    t = (r-HR)/50
    gc = tuple(int(BG_DEEP[i] + (CLAY[i]-BG_DEEP[i]) * 0.06 * (1-t)) for i in range(3))
    draw.ellipse([HCX-r, HCY-r, HCX+r, HCY+r], fill=gc)

# Sphere
for r in range(HR, 0, -1):
    t = r / HR
    c = tuple(int(CLAY[i] * (0.55 + 0.45 * (1-t))) for i in range(3))
    draw.ellipse([HCX-r, HCY-r, HCX+r, HCY+r], fill=c)

# Highlight
for r in range(HR//3, 0, -1):
    t = 1 - r/(HR//3)
    hx, hy = HCX - HR//4, HCY - HR//4
    c = tuple(min(255, int(CLAY[i] + (CREAM[i]-CLAY[i]) * 0.45 * t)) for i in range(3))
    draw.ellipse([hx-r, hy-r, hx+r, hy+r], fill=c)

# Glaze rings
for rr, rc in [(HR-12, WARM_STONE), (HR-32, OXIDE), (HR-56, BRONZE_LIGHT), (HR-82, CLAY)]:
    draw.ellipse([HCX-rr, HCY-rr, HCX+rr, HCY+rr],
                 outline=tuple(max(0, c-20) for c in rc), width=1)

draw.ellipse([HCX-5, HCY-5, HCX+5, HCY+5], fill=CREAM)

for deg in [0, 90, 180, 270]:
    a = math.radians(deg)
    mx = HCX + int((HR+28)*math.cos(a))
    my = HCY + int((HR+28)*math.sin(a))
    cross_marker(mx, my, 4, (48,44,46))

draw.text((HCX-22, HCY+HR+38), "no. 01", font=f_marker, fill=TEXT_DIM)

# ============================================================
# MATERIAL STUDY — dense marks
# ============================================================
MSY = 2720
MSH = 80
draw.text((M, MSY-20), "material study", font=f_marker, fill=TEXT_DIM)
mark_colors = [WARM_STONE, CLAY, OXIDE, BRONZE_LIGHT, CELADON]
for px in range(M, M+660, 9):
    for py in range(MSY, MSY+MSH, 9):
        if random.random() < 0.58:
            c = random.choice(mark_colors)
            size = random.choice([2, 3, 4, 5])
            fade = random.uniform(0.4, 0.85)
            fc = tuple(int(c[i]*fade) for i in range(3))
            if random.random() < 0.6:
                draw.ellipse([px, py, px+size, py+size], fill=fc)
            else:
                draw.rectangle([px, py, px+size-1, py+size-1], fill=fc)

# ============================================================
# BOTTOM SIGNATURE
# ============================================================
hline(2880, M, W-M, GRAPHITE)
draw.text((M, 2910), "QK", font=f_qk, fill=TEXT_DIM)
ct = "visual direction  /  design board  /  2026"
bb = draw.textbbox((0,0), ct, font=f_label)
draw.text((W//2 - (bb[2]-bb[0])//2, 2935), ct, font=f_label, fill=TEXT_DIM)
bt = "doany.ai"
bb = draw.textbbox((0,0), bt, font=f_brand)
draw.text((W-M-(bb[2]-bb[0]), 2918), bt, font=f_brand, fill=WARM_STONE)

for xp in [M, W//4, W//2, 3*W//4, W-M]:
    cross_marker(xp, 3020, 4, (36,33,35))

# ============================================================
out = "/private/var/folders/t6/_sx_03q50_v9w2jpnq683jmw0000gn/T/claude-run-szy3aspw/quiet-kiln-board.png"
img.save(out, "PNG")
print(f"Saved: {out}")
