import math
P,K='#f5f5f0','#0d0d0d'
HEAD='''<!doctype html><html><head><meta charset="utf-8"><style>html,body{margin:0;background:%s}svg{display:block;width:1500px;height:1000px}</style></head><body>
<svg viewBox="0 0 1500 1000" xmlns="http://www.w3.org/2000/svg">'''
TAIL='</svg></body></html>'

def robot(x,y,s,fg,bg,op=1):
    """配膳ロボット（正面）。x,y=足元の中心。s=倍率（1で高さ約400）"""
    g=f'<g transform="translate({x},{y}) scale({s}) translate(-100,-400)" opacity="{op}">'
    g+=f'<rect x="38" y="0" width="124" height="86" rx="26" fill="{fg}"/>'
    g+=f'<rect x="56" y="18" width="88" height="48" rx="12" fill="{bg}"/>'
    g+=f'<circle cx="82" cy="42" r="7" fill="{fg}"/><circle cx="118" cy="42" r="7" fill="{fg}"/>'
    g+=f'<rect x="84" y="84" width="32" height="250" fill="{fg}"/>'
    for ty,dish in [(150,'bowl'),(225,'cup'),(300,'plate')]:
        g+=f'<rect x="16" y="{ty}" width="168" height="18" rx="8" fill="{fg}"/>'
        if dish=='bowl': g+=f'<path d="M36 {ty} a30 26 0 0 0 60 0 z" fill="{fg}"/>'
        if dish=='cup': g+=f'<rect x="120" y="{ty-36}" width="28" height="36" rx="4" fill="{fg}"/><rect x="128" y="{ty-50}" width="4" height="16" fill="{fg}"/>'
        if dish=='plate': g+=f'<ellipse cx="62" cy="{ty-6}" rx="34" ry="7" fill="{fg}"/><rect x="118" y="{ty-30}" width="44" height="30" rx="6" fill="{fg}"/>'
    g+=f'<rect x="6" y="332" width="188" height="62" rx="30" fill="{fg}"/>'
    g+=f'<rect x="40" y="356" width="120" height="6" rx="3" fill="{bg}"/>'
    return g+'</g>'

def person(x,y,s,fg,op=1,tray=False):
    """スタッフ（正面）。x,y=足元。高さ約560*s"""
    g=f'<g transform="translate({x},{y}) scale({s})" opacity="{op}">'
    g+=f'<circle cx="0" cy="-500" r="52" fill="{fg}"/>'
    g+=f'<rect x="-70" y="-430" width="140" height="250" rx="44" fill="{fg}"/>'
    g+=f'<rect x="-58" y="-210" width="50" height="210" rx="22" fill="{fg}"/><rect x="8" y="-210" width="50" height="210" rx="22" fill="{fg}"/>'
    if tray:
        g+=f'<rect x="60" y="-300" width="110" height="12" rx="6" fill="{fg}"/><path d="M86 -300 a24 20 0 0 1 48 0 z" fill="{fg}"/>'
        g+=f'<rect x="56" y="-330" width="26" height="80" rx="13" fill="{fg}" transform="rotate(-30 70 -330)"/>'
    return g+'</g>'

# ── X 導入の前と後（左：人3人／右：人2人＋ロボット2台）
x=HEAD%P+f'<rect width="1500" height="1000" fill="{P}"/><rect x="750" width="750" height="1000" fill="{K}"/>'
x+=f'<line x1="80" y1="800" x2="690" y2="800" stroke="{K}" stroke-width="4"/>'
x+=f'<line x1="810" y1="800" x2="1420" y2="800" stroke="{P}" stroke-width="4"/>'
for px in [210,385,560]: x+=person(px,800,0.9,K)
x+=person(870,800,0.9,P)+person(1030,800,0.9,P)
x+=robot(1200,800,0.82,P,K)+robot(1355,800,0.82,P,K)
# 床の下：入店待ちの列（丸の数）
for i in range(3): x+=f'<circle cx="{120+i*46}" cy="890" r="16" fill="none" stroke="{K}" stroke-width="3"/>'
for i in range(6): x+=f'<circle cx="{850+i*46}" cy="890" r="16" fill="{P}"/>'
# 真ん中の矢印
x+=f'<circle cx="750" cy="500" r="62" fill="{P}" stroke="{K}" stroke-width="4"/><path d="M722 500 H772 M752 476 L778 500 L752 524" fill="none" stroke="{K}" stroke-width="9" stroke-linecap="round" stroke-linejoin="round"/>'
open('X_before_after.html','w').write(x+TAIL)

# ── Y ワイヤーフレームの床（アイソメ）に置いたロボットと卓
def iso(gx,gy,ox=750,oy=250,u=72):
    return ox+(gx-gy)*u, oy+(gx+gy)*u*0.5
y=HEAD%K+f'<rect width="1500" height="1000" fill="{K}"/>'
N=10
for i in range(N+1):
    a=iso(i,0);b=iso(i,N);y+=f'<line x1="{a[0]:.0f}" y1="{a[1]:.0f}" x2="{b[0]:.0f}" y2="{b[1]:.0f}" stroke="{P}" stroke-width="1.4" opacity=".35"/>'
    a=iso(0,i);b=iso(N,i);y+=f'<line x1="{a[0]:.0f}" y1="{a[1]:.0f}" x2="{b[0]:.0f}" y2="{b[1]:.0f}" stroke="{P}" stroke-width="1.4" opacity=".35"/>'
def box(gx,gy,w,d,h,fill=False):
    pts=[iso(gx,gy),iso(gx+w,gy),iso(gx+w,gy+d),iso(gx,gy+d)]
    top=[(px,py-h) for px,py in pts]
    s=''
    # 側面
    for a,b in [(1,2),(2,3)]:
        poly=[pts[a],pts[b],top[b],top[a]]
        s+='<polygon points="'+' '.join(f'{p[0]:.0f},{p[1]:.0f}' for p in poly)+f'" fill="{K}" stroke="{P}" stroke-width="2.4"/>'
    s+='<polygon points="'+' '.join(f'{p[0]:.0f},{p[1]:.0f}' for p in top)+f'" fill="{P if fill else K}" stroke="{P}" stroke-width="2.4"/>'
    return s
# 厨房（奥の壁）
y+=box(0,0,4,1,90,True)
tables=[(6,1),(8,1),(1,4),(4,4),(7,4),(1,7),(4,7),(7,7)]
# 通り道（点線）
path=[iso(2,1.5),iso(2,3.2),iso(6.2,3.2),iso(6.2,6.2),iso(8.6,6.2)]
y+='<polyline points="'+' '.join(f'{p[0]:.0f},{p[1]:.0f}' for p in path)+f'" fill="none" stroke="{P}" stroke-width="4" stroke-dasharray="12 10"/>'
for (tx,ty) in sorted(tables,key=lambda t:t[0]+t[1]):
    y+=box(tx,ty,1.4,1.4,34)
# ロボット（手前寄り・正面の絵を床に立てる）
rx,ry=iso(6.3,3.3); y+=robot(rx,ry,0.55,P,K)
# スタッフ
sx,sy=iso(2.6,6.0); y+=person(sx,sy,0.34,P)
open('Y_wireframe.html','w').write(y+TAIL)

# ── Z コマ送り（ロボットの残像が卓へ向かう）
z=HEAD%P+f'<rect width="1500" height="1000" fill="{P}"/>'
z+=f'<line x1="0" y1="760" x2="1500" y2="760" stroke="{K}" stroke-width="4"/>'
# 卓とお客さん（右）
z+=f'<rect x="1130" y="560" width="220" height="18" rx="4" fill="{K}"/><rect x="1230" y="578" width="20" height="182" fill="{K}"/><rect x="1180" y="740" width="120" height="20" rx="6" fill="{K}"/>'
z+=f'<circle cx="1395" cy="470" r="40" fill="{K}"/><rect x="1355" y="520" width="80" height="150" rx="34" fill="{K}"/>'
for i,(px,op) in enumerate([(190,.1),(390,.18),(590,.3),(790,.5),(1000,1)]):
    z+=robot(px,760,1.0,K,P,op)
# コマの目盛り（時間）
z+=f'<line x1="120" y1="860" x2="1100" y2="860" stroke="{K}" stroke-width="3"/>'
for i,px in enumerate([190,390,590,790,1000]):
    z+=f'<line x1="{px}" y1="840" x2="{px}" y2="880" stroke="{K}" stroke-width="3"/>'
z+=f'<circle cx="1000" cy="860" r="14" fill="{K}"/>'
open('Z_frames.html','w').write(z+TAIL)
