"""NEON LIFELINE — Python standard library only. Run in IDLE with F5."""
import math,random,time,tkinter as tk
TAU=math.tau
class Game:
    def __init__(self,seed=None):
        self.rng=random.Random(seed);self.angle=-math.pi/2;self.rocks=[]
        self.age=0.;self.charge=1.;self.spawn=.8;self.state='playing'
    def step(self,dt,steer=0,slow=False):
        if self.state!='playing':return
        dt=max(0,min(dt,.05));active=slow and self.charge>0
        self.charge=max(0,self.charge-dt*.42) if active else min(1,self.charge+dt*.16)
        speed=.28 if active else 1.;self.age+=dt
        self.angle=(self.angle+steer*3.4*dt)%TAU
        self.spawn-=dt*speed
        if self.spawn<=0:
            self.rocks.append([self.rng.random()*TAU,330.,self.rng.uniform(88,120)+self.age*1.3])
            self.spawn=max(.24,.7-self.age*.012)
        px,py=145*math.cos(self.angle),145*math.sin(self.angle)
        for rock in self.rocks:
            rock[1]-=rock[2]*dt*speed
            x,y=rock[1]*math.cos(rock[0]),rock[1]*math.sin(rock[0])
            if math.hypot(x-px,y-py)<19:self.state='lost'
        self.rocks=[r for r in self.rocks if r[1]>25]
        if self.age>=30 and self.state=='playing':self.state='won'

def launch():
    root=tk.Tk();root.title('Neon Lifeline');root.configure(bg='#0b1020');root.resizable(False,False)
    c=tk.Canvas(root,width=680,height=740,bg='#0b1020',highlightthickness=0);c.pack()
    keys=set();game=Game();mode='menu';last=time.perf_counter()
    def key(e):
        nonlocal game,mode
        k=e.keysym.lower();keys.add(k)
        if k=='r' or (k in ('return','space') and mode=='menu'):
            game=Game();mode='playing';keys.clear()
        elif k=='p' and mode in ('playing','paused'):mode='paused' if mode=='playing' else 'playing';keys.clear()
    root.bind('<KeyPress>',key);root.bind('<KeyRelease>',lambda e:keys.discard(e.keysym.lower()))
    def unfocus(e):
        nonlocal mode
        keys.clear()
        if mode=='playing':mode='paused'
    root.bind('<FocusOut>',unfocus)
    def text(x,y,s,size=14,color='#dce8f5'):
        c.create_text(x,y,text=s,fill=color,font=('Helvetica',size),justify='center')
    def circle(x,y,r,color,outline=None,width=1):
        c.create_oval(x-r,y-r,x+r,y+r,fill=color,outline=outline or color,width=width)
    def tick():
        nonlocal last,mode
        now=time.perf_counter();dt=min(now-last,.05);last=now
        if mode=='playing':
            game.step(dt,int(bool(keys&{'right','d'}))-int(bool(keys&{'left','a'})),'space' in keys)
            if game.state in ('lost','won'):mode=game.state
        c.delete('all');cx,cy=340,370
        for i in range(75):circle((i*113+37)%680,(i*79+109)%650,1,'#34405b')
        text(340,40,'NEON LIFELINE',27);text(340,78,'KEEP YOUR ORBIT. OUTLAST THE STORM.',11,'#8da3bf')
        for r in (60,100,145,230,300):circle(cx,cy,r,'', '#22314b',1)
        circle(cx,cy,27,'#283d52');circle(cx,cy,18,'#ffca76');circle(cx-5,cy-5,6,'#fff0c7')
        for a,r,v in game.rocks:
            x,y=cx+r*math.cos(a),cy+r*math.sin(a)
            c.create_line(x,y,x+23*math.cos(a),y+23*math.sin(a),fill='#773c62',width=3)
            circle(x,y,10,'#f384a4')
        a=game.angle;px,py=cx+145*math.cos(a),cy+145*math.sin(a)
        circle(px,py,15,'','#2b716f',3);circle(px,py,8,'#77ffdf')
        c.create_rectangle(70,665,610,675,fill='#23314b',outline='')
        c.create_rectangle(70,665,70+540*game.charge,675,fill='#77ffdf',outline='')
        text(340,638,f'{min(30,game.age):04.1f} / 30 seconds',16)
        text(340,708,'← → or A / D: steer    SPACE: slow time    P: pause    R: restart',11,'#a2b3c9')
        if mode!='playing':
            c.create_rectangle(80,255,600,475,fill='#111b30',outline='#47617d',width=2)
            messages={'menu':('YOUR STAR NEEDS A PILOT','Dodge the pink debris for 30 seconds.\nHold SPACE to spend your slow-time charge.\nRelease it to recharge.','Press ENTER to launch'), 'paused':('ORBIT PAUSED','Take your time.','Press P to resume'), 'lost':('SIGNAL LOST','The star is still there. Try a new orbit.','Press R to try again'), 'won':('YOU KEPT THE LIGHT','Thirty seconds. One star. Still shining.','Press R for another storm')}
            title,body,footer=messages[mode];text(340,296,title,22,'#77ffdf');text(340,365,body,14);text(340,440,footer,14,'#ffca76')
        root.after(16,tick)
    tick();root.mainloop()
if __name__=='__main__':launch()
