import pygame
import sys
import random
import math

# ========== 初始化 ==========
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Delta Force - Ultimate Equipment Draw")
clock = pygame.time.Clock()

# ========== 色彩 ==========
BG_COLOR = (20, 25, 35)
CARD_BG = (40, 45, 55)
GOLD = (255, 215, 0)
SILVER = (192, 192, 192)
BLUE = (70, 130, 240)
PURPLE = (180, 100, 255)
RED = (255, 60, 60)
MAGENTA = (255, 0, 255)      # 炫彩品质
WHITE = (255, 255, 255)
BLACK = (20, 20, 20)
GRAY = (150, 150, 150)
BUTTON_COLOR = (50, 80, 150)
BUTTON_HOVER = (70, 110, 200)
RARITY_COLORS = [GRAY, BLUE, PURPLE, GOLD, RED, MAGENTA]

# ========== 字体 ==========
font_title = pygame.font.Font(None, 52)
font_large = pygame.font.Font(None, 36)
font_med = pygame.font.Font(None, 28)
font_small = pygame.font.Font(None, 20)

# ========== 46件装备数据 ==========
items = [
    # 普通 (Common)
    {"name": "M1911",         "rarity": 0, "shape": "pistol",   "desc": "Classic .45 sidearm"},
    {"name": "Glock 18",      "rarity": 0, "shape": "pistol",   "desc": "Full-auto 9mm"},
    {"name": "M67 Grenade",   "rarity": 0, "shape": "grenade",  "desc": "Fragmentation grenade"},
    {"name": "Smoke Grenade", "rarity": 0, "shape": "grenade",  "desc": "Tactical concealment"},
    {"name": "Flashbang",     "rarity": 0, "shape": "grenade",  "desc": "Non-lethal disorientation"},
    {"name": "M9 Bayonet",    "rarity": 0, "shape": "knife",    "desc": "Close-quarters blade"},
    {"name": "MRE Ration",    "rarity": 0, "shape": "ration",   "desc": "Combat field meal"},
    {"name": "Compass",       "rarity": 0, "shape": "compass",  "desc": "Basic navigation tool"},
    # 稀有 (Rare)
    {"name": "M4A1",          "rarity": 1, "shape": "rifle",    "desc": "5.56 assault rifle"},
    {"name": "AK-47",         "rarity": 1, "shape": "rifle",    "desc": "7.62 iconic power"},
    {"name": "MP5",           "rarity": 1, "shape": "smg",      "desc": "Submachine gun"},
    {"name": "AWP",           "rarity": 1, "shape": "sniper",   "desc": "High-caliber sniper"},
    {"name": "M249 SAW",      "rarity": 1, "shape": "lmg",      "desc": "Light machine gun"},
    {"name": "SPAS-12",       "rarity": 1, "shape": "shotgun",  "desc": "Pump-action shotgun"},
    {"name": "M67 Recoilless","rarity": 1, "shape": "recoilless","desc": "Anti-tank recoilless rifle"},
    {"name": "Claymore Mine", "rarity": 1, "shape": "claymore", "desc": "Directional anti-personnel mine"},
    # 史诗 (Epic)
    {"name": "RPG-7",         "rarity": 2, "shape": "rpg",      "desc": "Anti-tank launcher"},
    {"name": "Stinger",       "rarity": 2, "shape": "stinger",  "desc": "Surface-to-air missile"},
    {"name": "C4 Explosive",  "rarity": 2, "shape": "c4",       "desc": "Remote demolition"},
    {"name": "Night Vision",  "rarity": 2, "shape": "scope",    "desc": "Gen 3 night optics"},
    {"name": "Heavy Armor",   "rarity": 2, "shape": "armor",    "desc": "Level IV plates"},
    {"name": "Medkit",        "rarity": 2, "shape": "medkit",   "desc": "Field medical kit"},
    {"name": "Drone",         "rarity": 2, "shape": "drone",    "desc": "Tactical surveillance"},
    {"name": "Mortar Tube",   "rarity": 2, "shape": "mortar",   "desc": "Light infantry mortar"},
    # 传说 (Legendary)
    {"name": "AH-64 Apache",  "rarity": 3, "shape": "heli",     "desc": "Attack helicopter"},
    {"name": "M1 Abrams",     "rarity": 3, "shape": "tank",     "desc": "Main battle tank"},
    {"name": "AC-130 Gunship","rarity": 3, "shape": "gunship",  "desc": "Heavy air support"},
    {"name": "Nuclear Warhead","rarity": 3,"shape": "nuke",     "desc": "Ultimate destruction"},
    {"name": "Delta Operator", "rarity": 3,"shape": "operator",  "desc": "Elite special forces"},
    {"name": "Black Hawk",     "rarity": 3,"shape": "blackhawk","desc": "Transport helicopter"},
    {"name": "Javelin",        "rarity": 3,"shape": "javelin",  "desc": "Fire-and-forget ATGM"},
    {"name": "Tomahawk",       "rarity": 3,"shape": "tomahawk", "desc": "Cruise missile"},
    {"name": "Railgun Prototype","rarity":3,"shape":"railgun", "desc":"Experimental EM weapon"},
    {"name": "Exoskeleton",   "rarity": 3, "shape": "exo",      "desc": "Powered armor suit"},
    {"name": "Stealth Bomber","rarity": 3, "shape": "stealth",  "desc": "B-2 Spirit bomber"},
    {"name": "Satellite Uplink","rarity":3,"shape":"satellite","desc":"Orbital communication control"},
    # 神话 (Mythic) - 红色
    {"name": "Ocean Tear",    "rarity": 4, "shape": "pearl",    "desc": "Mysterious deep-sea pearl"},
    {"name": "Arctic Star",   "rarity": 4, "shape": "polaris_knife", "desc": "Legendary frost blade"},
    {"name": "African Star",  "rarity": 4, "shape": "diamond",  "desc": "Cullinan Diamond replica"},
    {"name": "Dragonfire",    "rarity": 4, "shape": "flamethrower","desc": "Heavy incendiary weapon"},
    {"name": "EMP Device",    "rarity": 4, "shape": "emp",      "desc": "Electromagnetic pulse generator"},
    {"name": "MOAB",          "rarity": 4, "shape": "moab",     "desc": "Largest conventional bomb"},
    {"name": "Black Box",     "rarity": 4, "shape": "blackbox", "desc": "Flight data recorder"},
    {"name": "GPU Card",      "rarity": 4, "shape": "gpu",      "desc": "High-end graphics card"},
    {"name": "Ventilator",    "rarity": 4, "shape": "ventilator","desc": "Emergency resuscitation device"},
    # 炫彩 (Chroma) - 品红
    {"name": "Wei Xiaolong",  "rarity": 5, "shape": "figure",   "desc": "Limited edition collector figure"},
]

rarity_names = ["Common", "Rare", "Epic", "Legendary", "Mythic", "Chroma"]
rarity_probs = [0.40, 0.28, 0.17, 0.10, 0.03, 0.02]

# ========== 粒子系统 ==========
class Particle:
    def __init__(self, x, y, color):
        self.x, self.y = x, y
        self.vx = random.uniform(-3, 3)
        self.vy = random.uniform(-6, -2)
        self.color = color
        self.life, self.max_life = 25, 25
    def update(self):
        self.x += self.vx; self.y += self.vy
        self.vy += 0.15; self.life -= 1
    def draw(self, surf):
        if self.life > 0:
            alpha = int(255 * self.life / self.max_life)
            r = max(1, int(4 * self.life / self.max_life))
            pygame.draw.circle(surf, (*self.color, alpha), (int(self.x), int(self.y)), r)

# ========== 按钮类 ==========
class Button:
    def __init__(self, x, y, w, h, text, color, hover_color, text_color=WHITE):
        self.rect = pygame.Rect(x, y, w, h)
        self.text = text
        self.color = color
        self.hover_color = hover_color
        self.text_color = text_color
        self.radius = 12
    def draw(self, surface, mouse_pos):
        col = self.hover_color if self.rect.collidepoint(mouse_pos) else self.color
        pygame.draw.rect(surface, col, self.rect, border_radius=self.radius)
        pygame.draw.rect(surface, WHITE, self.rect, 2, border_radius=self.radius)
        txt = font_med.render(self.text, True, self.text_color)
        surface.blit(txt, txt.get_rect(center=self.rect.center))
    def is_clicked(self, pos): return self.rect.collidepoint(pos)

# ========== 完整图案系统（所有形状均自定义） ==========
def draw_shape(surface, shape, x, y, size, color):
    light = tuple(min(255, c+60) for c in color)
    dark = tuple(max(0, c-60) for c in color)
    shadow = 3

    if shape == "pistol":
        slide = pygame.Rect(x-size//2, y-size//6, size*2//3, size//4)
        barrel = pygame.Rect(x+size//6, y-size//8, size//2, size//6)
        grip = pygame.Rect(x-size//4, y+size//8, size//3, size//2)
        trigger = pygame.Rect(x-size//6, y+size//5, size//4, size//4)
        for part in [slide, barrel, grip]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
        pygame.draw.ellipse(surface, dark, trigger.move(shadow, shadow))
        pygame.draw.rect(surface, color, slide, border_radius=4)
        pygame.draw.rect(surface, color, barrel, border_radius=2)
        pygame.draw.rect(surface, color, grip, border_radius=4)
        pygame.draw.ellipse(surface, color, trigger, 2)
        pygame.draw.rect(surface, light, slide.inflate(-6, -4), border_radius=3)
        for part in [slide, barrel, grip]:
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4)
        pygame.draw.ellipse(surface, BLACK, trigger, 2)
    elif shape == "grenade":
        body = pygame.Rect(x-size//3, y-size//3, size*2//3, size*2//3)
        fuse = pygame.Rect(x-4, y-size//3-10, 8, 12)
        ring = pygame.Rect(x-8, fuse.y-6, 16, 8)
        pygame.draw.ellipse(surface, dark, body.move(shadow, shadow))
        pygame.draw.ellipse(surface, color, body)
        pygame.draw.ellipse(surface, BLACK, body, 2)
        for i in range(3):
            ly = body.y + i*body.h//3
            pygame.draw.line(surface, dark, (body.x, ly), (body.right, ly), 1)
        pygame.draw.rect(surface, (160,160,160), fuse, border_radius=2)
        pygame.draw.rect(surface, BLACK, fuse, 2, border_radius=2)
        pygame.draw.ellipse(surface, (160,160,160), ring, 2)
        pygame.draw.ellipse(surface, BLACK, ring, 1)
    elif shape == "rifle":
        upper = pygame.Rect(x-size, y-size//4, size*2, size//5)
        barrel = pygame.Rect(x+size//2, y-size//5, size, size//5)
        handguard = pygame.Rect(x-size//3, y-size//5, size, size//4)
        grip = pygame.Rect(x-size//4, y+size//6, size//4, size//2)
        mag = pygame.Rect(x+size//5, y+size//5, size//5, size//2)
        stock = pygame.Rect(x-size*1.2, y-size//3, size//2, size*2//5)
        for part in [upper, barrel, handguard, grip, mag, stock]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        pygame.draw.rect(surface, light, upper.inflate(-6, -4), border_radius=2)
    elif shape == "smg":
        body = pygame.Rect(x-size, y-size//5, size*2, size*2//5)
        grip = pygame.Rect(x-size//3, y+size//5, size//4, size//3)
        mag = pygame.Rect(x+size//4, y+size//4, size//5, size//3)
        stock = pygame.Rect(x-size*1.1, y-size//3, size//2, size//2)
        for part in [body, grip, mag, stock]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
    elif shape == "sniper":
        barrel = pygame.Rect(x-size, y-size//8, size*2, size//4)
        scope = pygame.Rect(x-size//3, y-size//2, size//2, size//2)
        stock = pygame.Rect(x-size*1.3, y-size//3, size//2, size*2//3)
        bipod = pygame.Rect(x+size//3, y+size//4, size//2, size//6)
        for part in [barrel, scope, stock, bipod]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4)
        lens = pygame.Rect(scope.x+4, scope.y+4, scope.w-8, scope.h-8)
        pygame.draw.rect(surface, (150,200,255), lens, border_radius=3)
        pygame.draw.rect(surface, BLACK, lens, 1, border_radius=3)
    elif shape == "lmg":
        body = pygame.Rect(x-size, y-size//3, size*2, size*2//3)
        box = pygame.Rect(x+size//2, y, size//2, size//2)
        bipod = pygame.Rect(x-size//2, y+size//3, size, size//6)
        for part in [body, box, bipod]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        for i in range(4):
            vent = pygame.Rect(x - size//2 + i*size//3, y-size//6, size//6, size//6)
            pygame.draw.rect(surface, dark, vent)
            pygame.draw.rect(surface, BLACK, vent, 1)
    elif shape == "rpg":
        tube = pygame.Rect(x-size, y-size//8, size*2, size//4)
        warhead = pygame.Rect(x+size, y-size//3, size//2, size*2//3)
        grip = pygame.Rect(x-size//3, y+size//6, size//4, size//3)
        for part in [tube, warhead, grip]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        tip = pygame.Rect(warhead.right-4, warhead.top+4, 6, warhead.h-8)
        pygame.draw.rect(surface, RED, tip, border_radius=2)
    elif shape == "stinger":
        tube = pygame.Rect(x-size, y-size//8, size*2, size//4)
        grip = pygame.Rect(x-size//2, y+size//6, size//3, size//3)
        sight = pygame.Rect(x+size//3, y-size//3, size//3, size//3)
        for part in [tube, grip, sight]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        pygame.draw.circle(surface, (150,200,255), sight.center, size//6)
        pygame.draw.circle(surface, BLACK, sight.center, size//6, 2)
    elif shape == "c4":
        block = pygame.Rect(x-size//2, y-size//3, size, size*2//3)
        detonator = pygame.Rect(x+size//3, y-size//2, size//4, size//2)
        antenna = pygame.Rect(detonator.right-2, detonator.top-8, 2, 10)
        for part in [block, detonator]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4)
        pygame.draw.rect(surface, (160,160,160), antenna)
        pygame.draw.rect(surface, BLACK, antenna, 1)
    elif shape == "scope":
        tube = pygame.Rect(x-size//2, y-size//4, size, size//2)
        lens_l = pygame.Rect(tube.x-4, tube.y+4, 6, tube.h-8)
        lens_r = pygame.Rect(tube.right-2, tube.y+4, 6, tube.h-8)
        mount = pygame.Rect(x-size//6, y+size//4, size//3, size//4)
        for part in [tube, mount]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4)
        pygame.draw.rect(surface, (150,200,255), lens_l, border_radius=2)
        pygame.draw.rect(surface, (150,200,255), lens_r, border_radius=2)
        pygame.draw.rect(surface, BLACK, lens_l, 1, border_radius=2)
        pygame.draw.rect(surface, BLACK, lens_r, 1, border_radius=2)
        pygame.draw.line(surface, BLACK, (tube.centerx, tube.top+4), (tube.centerx, tube.bottom-4), 2)
        pygame.draw.line(surface, BLACK, (tube.left+4, tube.centery), (tube.right-4, tube.centery), 2)
    elif shape == "armor":
        chest = pygame.Rect(x-size//3, y-size//4, size*2//3, size)
        shoulder_l = pygame.Rect(x-size//2, y-size//6, size//4, size//3)
        shoulder_r = pygame.Rect(x+size//4, y-size//6, size//4, size//3)
        collar = pygame.Rect(x-size//5, y-size//3, size*2//5, size//6)
        for part in [chest, shoulder_l, shoulder_r, collar]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=6)
            pygame.draw.rect(surface, color, part, border_radius=6)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=6)
        pygame.draw.line(surface, light, (chest.x+8, chest.centery), (chest.right-8, chest.centery), 3)
        visor = pygame.Rect(x-size//6, y-size//4, size//3, size//6)
        pygame.draw.rect(surface, (100,200,255), visor, border_radius=3)
        pygame.draw.rect(surface, BLACK, visor, 1, border_radius=3)
    elif shape == "medkit":
        bag = pygame.Rect(x-size//2, y-size//3, size, size*2//3)
        cross_h = pygame.Rect(x-size//5, y-size//6, size*2//5, size//6)
        cross_v = pygame.Rect(x-size//6, y-size//3, size//3, size*2//3)
        pygame.draw.rect(surface, dark, bag.move(shadow, shadow), border_radius=6)
        pygame.draw.rect(surface, color, bag, border_radius=6)
        pygame.draw.rect(surface, BLACK, bag, 2, border_radius=6)
        pygame.draw.rect(surface, RED, cross_h, border_radius=3)
        pygame.draw.rect(surface, RED, cross_v, border_radius=3)
    elif shape == "drone":
        body = pygame.Rect(x-size//3, y-size//4, size*2//3, size//2)
        arm_l = pygame.Rect(x-size//2, y-size//6, size//6, size//3)
        arm_r = pygame.Rect(x+size//3, y-size//6, size//6, size//3)
        rotor_l = pygame.Rect(x-size//2-2, y-size//3-4, size//4, 6)
        rotor_r = pygame.Rect(x+size//4, y-size//3-4, size//4, 6)
        for part in [body, arm_l, arm_r, rotor_l, rotor_r]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 1, border_radius=3)
        camera = pygame.Rect(x-size//6, y+size//6, size//3, size//6)
        pygame.draw.rect(surface, (150,200,255), camera, border_radius=3)
        pygame.draw.rect(surface, BLACK, camera, 1, border_radius=3)
    elif shape == "heli":
        body = pygame.Rect(x-size//2, y-size//3, size, size*2//3)
        tail = pygame.Rect(x+size//3, y-size//4, size//2, size//4)
        rotor = pygame.Rect(x-size//2-6, y-size//2-6, size+12, 8)
        missile_l = pygame.Rect(x-size//3, y+size//4, size//5, size//6)
        missile_r = pygame.Rect(x+size//6, y+size//4, size//5, size//6)
        for part in [body, tail, rotor, missile_l, missile_r]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 1, border_radius=4)
        window = pygame.Rect(x-size//4, y-size//5, size//2, size//4)
        pygame.draw.rect(surface, (150,200,255), window, border_radius=3)
        pygame.draw.rect(surface, BLACK, window, 1, border_radius=3)
    elif shape == "tank":
        track = pygame.Rect(x-size//2, y+size//6, size, size//3)
        hull = pygame.Rect(x-size//3, y-size//4, size*2//3, size//2)
        turret = pygame.Rect(x-size//4, y-size//2, size//2, size//3)
        barrel = pygame.Rect(x+size//4, y-size//2, size//2, size//8)
        for part in [track, hull, turret, barrel]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        for i in range(3):
            wx = track.x + track.w//4 + i*track.w//4
            pygame.draw.circle(surface, BLACK, (wx, track.centery), 5)
            pygame.draw.circle(surface, GRAY, (wx, track.centery), 3)
    elif shape == "gunship":
        body = pygame.Rect(x-size//2, y-size//3, size, size*2//3)
        wing_l = pygame.Rect(x-size*2//3, y-size//5, size//2, size//3)
        wing_r = pygame.Rect(x+size//6, y-size//5, size//2, size//3)
        for part in [body, wing_l, wing_r]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4)
        for dx in [-size//4, size//4]:
            pygame.draw.ellipse(surface, (160,160,160), (x+dx-6, y-size//2-4, 12, 8))
            pygame.draw.ellipse(surface, BLACK, (x+dx-6, y-size//2-4, 12, 8), 2)
    elif shape == "nuke":
        base = pygame.Rect(x-size//4, y+size//3, size//2, size//6)
        silo = pygame.Rect(x-size//6, y, size//3, size//3)
        warhead = [(x, y-size//2), (x-size//2, y), (x+size//2, y)]
        pygame.draw.rect(surface, dark, base.move(shadow, shadow), border_radius=3)
        pygame.draw.rect(surface, dark, silo.move(shadow, shadow), border_radius=3)
        pygame.draw.polygon(surface, dark, [(p[0]+shadow, p[1]+shadow) for p in warhead])
        pygame.draw.rect(surface, color, base, border_radius=3)
        pygame.draw.rect(surface, color, silo, border_radius=3)
        pygame.draw.polygon(surface, color, warhead)
        pygame.draw.rect(surface, BLACK, base, 2, border_radius=3)
        pygame.draw.rect(surface, BLACK, silo, 2, border_radius=3)
        pygame.draw.polygon(surface, BLACK, warhead, 2)
        pygame.draw.circle(surface, BLACK, (x, y-size//4), size//6, 2)
        for a in [0, 120, 240]:
            rad = math.radians(a)
            ex = x + int(math.cos(rad)*size//5)
            ey = y - size//4 - int(math.sin(rad)*size//5)
            pygame.draw.line(surface, BLACK, (x, y-size//4), (ex, ey), 2)
    elif shape == "operator":
        head = pygame.Rect(x-size//5, y-size//2, size*2//5, size//3)
        body = pygame.Rect(x-size//3, y-size//6, size*2//3, size*2//3)
        for part in [head, body]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=6)
            pygame.draw.rect(surface, color, part, border_radius=6)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=6)
        visor = pygame.Rect(head.x+4, head.y+4, head.w-8, head.h//2)
        pygame.draw.rect(surface, (100,200,255), visor, border_radius=3)
        pygame.draw.rect(surface, BLACK, visor, 1, border_radius=3)
    elif shape == "blackhawk":
        body = pygame.Rect(x-size//2, y-size//3, size, size*2//3)
        tail = pygame.Rect(x+size//3, y-size//4, size//2, size//4)
        rotor = pygame.Rect(x-size//2-6, y-size//2-6, size+12, 8)
        for part in [body, tail, rotor]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4)
        window = pygame.Rect(x-size//4, y-size//5, size//2, size//4)
        pygame.draw.rect(surface, (150,200,255), window, border_radius=3)
        pygame.draw.rect(surface, BLACK, window, 1, border_radius=3)
    elif shape == "javelin":
        tube = pygame.Rect(x-size, y-size//8, size*2, size//4)
        sight = pygame.Rect(x-size//3, y-size//3, size//3, size//3)
        for part in [tube, sight]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        pygame.draw.circle(surface, (150,200,255), sight.center, size//6)
        pygame.draw.circle(surface, BLACK, sight.center, size//6, 2)
    elif shape == "tomahawk":
        body = pygame.Rect(x-size, y-size//6, size*2, size//3)
        wing_l = pygame.Rect(x-size//2, y-size//3, size//2, size//6)
        wing_r = pygame.Rect(x+size//6, y-size//3, size//2, size//6)
        for part in [body, wing_l, wing_r]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        pygame.draw.polygon(surface, RED, [(x+size*2//3, y-size//6), (x+size, y), (x+size*2//3, y+size//6)])
    elif shape == "knife":
        blade = pygame.Rect(x-size//2, y-size//4, size, size//6)
        handle = pygame.Rect(x+size//2, y-size//3, size//2, size*2//3)
        for part in [blade, handle]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=2)
            pygame.draw.rect(surface, color, part, border_radius=2)
            pygame.draw.rect(surface, BLACK, part, 1, border_radius=2)
        pygame.draw.polygon(surface, color, [(x-size//2, y-size//4), (x-size//2-6, y), (x-size//2, y+size//6)])
    elif shape == "ration":
        box = pygame.Rect(x-size//2, y-size//3, size, size*2//3)
        label = pygame.Rect(x-size//4, y-size//6, size//2, size//3)
        pygame.draw.rect(surface, dark, box.move(shadow, shadow), border_radius=4)
        pygame.draw.rect(surface, color, box, border_radius=4)
        pygame.draw.rect(surface, BLACK, box, 2, border_radius=4)
        pygame.draw.rect(surface, (100,120,70), label, border_radius=2)
        pygame.draw.rect(surface, BLACK, label, 1, border_radius=2)
        pygame.draw.line(surface, BLACK, (label.x, label.centery), (label.right, label.centery), 1)
    elif shape == "compass":
        pygame.draw.circle(surface, dark, (x+shadow, y+shadow), size//2)
        pygame.draw.circle(surface, color, (x, y), size//2)
        pygame.draw.circle(surface, BLACK, (x, y), size//2, 2)
        pygame.draw.line(surface, RED, (x, y-size//3), (x, y+size//3), 2)
        pygame.draw.line(surface, BLACK, (x-size//3, y), (x+size//3, y), 1)
        pygame.draw.polygon(surface, RED, [(x, y-size//2), (x-3, y-size//4), (x+3, y-size//4)])
    elif shape == "shotgun":
        barrel = pygame.Rect(x-size, y-size//6, size*2, size//3)
        grip = pygame.Rect(x-size//3, y+size//6, size//4, size//2)
        for part in [barrel, grip]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        for i in range(2):
            tube_y = y - size//6 + i*size//4
            pygame.draw.rect(surface, BLACK, (x+size//2, tube_y, size//2, size//8))
    elif shape == "recoilless":
        tube = pygame.Rect(x-size, y-size//8, size*2, size//4)
        shield = pygame.Rect(x+size//3, y-size//3, size//6, size*2//3)
        for part in [tube, shield]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        pygame.draw.rect(surface, RED, (x+size*3//4, y-size//6, size//4, size//3))
    elif shape == "claymore":
        body = pygame.Rect(x-size//3, y-size//4, size*2//3, size//2)
        stand = pygame.Rect(x-size//6, y+size//4, size//3, size//4)
        for part in [body, stand]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        pygame.draw.rect(surface, RED, (body.x+4, body.centery-2, body.w-8, 4))
    elif shape == "mortar":
        tube = pygame.Rect(x-size//6, y-size//3, size//3, size*2//3)
        base = pygame.Rect(x-size//3, y+size//4, size*2//3, size//4)
        for part in [tube, base]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4)
        pygame.draw.circle(surface, BLACK, (x, y-size//3), size//6)
    elif shape == "railgun":
        barrel = pygame.Rect(x-size, y-size//8, size*2, size//4)
        coils = [pygame.Rect(x-size//2 + i*size//4, y-size//6, size//6, size//3) for i in range(5)]
        for part in [barrel] + coils:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=2)
            pygame.draw.rect(surface, color, part, border_radius=2)
            pygame.draw.rect(surface, BLACK, part, 1, border_radius=2)
        pygame.draw.rect(surface, (255,100,100), (x+size//2, y-size//6, size//4, size//3))
    elif shape == "exo":
        torso = pygame.Rect(x-size//3, y-size//3, size*2//3, size*2//3)
        arm_l = pygame.Rect(x-size//2, y-size//6, size//4, size//2)
        arm_r = pygame.Rect(x+size//4, y-size//6, size//4, size//2)
        leg_l = pygame.Rect(x-size//4, y+size//3, size//5, size//2)
        leg_r = pygame.Rect(x+size//6, y+size//3, size//5, size//2)
        for part in [torso, arm_l, arm_r, leg_l, leg_r]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=6)
            pygame.draw.rect(surface, color, part, border_radius=6)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=6)
        visor = pygame.Rect(torso.x+4, torso.y+4, torso.w-8, torso.h//3)
        pygame.draw.rect(surface, (100,200,255), visor, border_radius=3)
        pygame.draw.rect(surface, BLACK, visor, 1, border_radius=3)
    elif shape == "stealth":
        body = pygame.Rect(x-size//2, y-size//3, size, size*2//3)
        wing = pygame.Rect(x-size*2//3, y-size//4, size*4//3, size//2)
        for part in [body, wing]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 1, border_radius=4)
        pygame.draw.ellipse(surface, (150,200,255), (x-size//4, y-size//3, size//2, size//4))
    elif shape == "satellite":
        body = pygame.Rect(x-size//4, y-size//3, size//2, size//3)
        panel_l = pygame.Rect(x-size//2, y-size//4, size//4, size//6)
        panel_r = pygame.Rect(x+size//4, y-size//4, size//4, size//6)
        antenna = pygame.Rect(x-2, y-size//2, 4, size//6)
        for part in [body, panel_l, panel_r, antenna]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 1, border_radius=3)
        pygame.draw.circle(surface, RED, (x, y-size//4), 4)
    # 新增形状
    elif shape == "pearl":
        for r in range(size//2 + 4, size//2, -2):
            alpha = 40
            s = pygame.Surface((r*2, r*2), pygame.SRCALPHA)
            pygame.draw.circle(s, (*color, alpha), (r, r), r)
            surface.blit(s, (x - r, y - r))
        pygame.draw.circle(surface, (255,255,255), (x, y), size//2)
        pygame.draw.circle(surface, color, (x, y), size//2, 2)
        pygame.draw.circle(surface, (255,255,255), (x - size//6, y - size//6), size//6)
    elif shape == "polaris_knife":
        blade = pygame.Rect(x - size//2, y - size//4, size*2//3, size//6)
        handle = pygame.Rect(x + size//6, y - size//3, size//2, size*2//3)
        guard = pygame.Rect(x + size//6 - 4, y - size//2, 8, size)
        pygame.draw.rect(surface, dark, blade.move(shadow, shadow), border_radius=2)
        pygame.draw.rect(surface, (100, 200, 255), blade, border_radius=2)
        pygame.draw.rect(surface, BLACK, blade, 2, border_radius=2)
        pygame.draw.rect(surface, (30,30,30), handle.move(shadow, shadow), border_radius=4)
        pygame.draw.rect(surface, (60,60,60), handle, border_radius=4)
        pygame.draw.rect(surface, BLACK, handle, 2, border_radius=4)
        pygame.draw.rect(surface, (80,80,80), guard.move(shadow, shadow), border_radius=2)
        pygame.draw.rect(surface, (120,120,120), guard, border_radius=2)
        pygame.draw.rect(surface, BLACK, guard, 2, border_radius=2)
    elif shape == "diamond":
        pts = [
            (x, y - size//2), (x + size//3, y - size//6), (x + size//2, y),
            (x + size//3, y + size//6), (x, y + size//2), (x - size//3, y + size//6),
            (x - size//2, y), (x - size//3, y - size//6)
        ]
        pygame.draw.polygon(surface, dark, [(px+shadow, py+shadow) for px, py in pts])
        pygame.draw.polygon(surface, color, pts)
        pygame.draw.polygon(surface, BLACK, pts, 2)
        inner_pts = [(x, y - size//4), (x + size//6, y), (x, y + size//4), (x - size//6, y)]
        pygame.draw.polygon(surface, light, inner_pts)
    elif shape == "flamethrower":
        tank = pygame.Rect(x - size//3, y - size//3, size//2, size*2//3)
        nozzle = pygame.Rect(x + size//6, y - size//6, size//3, size//3)
        flame = [(x + size//3, y - size//4), (x + size, y), (x + size//3, y + size//4)]
        for part in [tank, nozzle]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        pygame.draw.polygon(surface, (255,100,0), [(px+shadow, py+shadow) for px, py in flame])
        pygame.draw.polygon(surface, (255,200,0), flame)
        pygame.draw.polygon(surface, RED, flame, 2)
    elif shape == "emp":
        dish = pygame.Rect(x - size//2, y - size//4, size, size//2)
        base = pygame.Rect(x - size//4, y + size//4, size//2, size//3)
        pulse = pygame.Rect(x - size//6, y - size//2 - 6, size//3, 6)
        for part in [dish, base, pulse]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=4)
            pygame.draw.rect(surface, color, part, border_radius=4)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4)
        for i in range(3):
            arc_rect = pygame.Rect(x - size//4 + i*4, y - size//2 - i*8, size//2 - i*8, 12)
            pygame.draw.arc(surface, (150,150,255), arc_rect, math.pi/4, 3*math.pi/4, 2)
    elif shape == "moab":
        body = pygame.Rect(x - size//3, y - size//2, size*2//3, size)
        fin_l = pygame.Rect(x - size//3, y + size//3, size//4, size//4)
        fin_r = pygame.Rect(x + size//6, y + size//3, size//4, size//4)
        nose = [(x + size//3, y - size//3), (x + size//2, y), (x + size//3, y + size//3)]
        for part in [body, fin_l, fin_r]:
            pygame.draw.rect(surface, dark, part.move(shadow, shadow), border_radius=3)
            pygame.draw.rect(surface, color, part, border_radius=3)
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=3)
        pygame.draw.polygon(surface, dark, [(px+shadow, py+shadow) for px, py in nose])
        pygame.draw.polygon(surface, (255,100,0), nose)
        pygame.draw.polygon(surface, BLACK, nose, 2)
    elif shape == "blackbox":
        box = pygame.Rect(x - size//2, y - size//3, size, size*2//3)
        stripe = pygame.Rect(x - size//2, y - size//12, size, size//6)
        circle = pygame.Rect(x - size//8, y - size//8, size//4, size//4)
        pygame.draw.rect(surface, dark, box.move(shadow, shadow), border_radius=6)
        pygame.draw.rect(surface, (255,150,0), box, border_radius=6)
        pygame.draw.rect(surface, BLACK, box, 2, border_radius=6)
        pygame.draw.rect(surface, BLACK, stripe)
        pygame.draw.ellipse(surface, GRAY, circle)
        pygame.draw.ellipse(surface, BLACK, circle, 2)
    elif shape == "gpu":
        board = pygame.Rect(x - size//2, y - size//4, size, size//2)
        chip = pygame.Rect(x - size//6, y - size//6, size//3, size//3)
        fan = pygame.Rect(x + size//6, y - size//6, size//4, size//3)
        pygame.draw.rect(surface, dark, board.move(shadow, shadow), border_radius=4)
        pygame.draw.rect(surface, color, board, border_radius=4)
        pygame.draw.rect(surface, BLACK, board, 2, border_radius=4)
        pygame.draw.rect(surface, GRAY, chip, border_radius=2)
        pygame.draw.rect(surface, BLACK, chip, 1, border_radius=2)
        pygame.draw.rect(surface, (100,100,100), fan, border_radius=2)
        pygame.draw.rect(surface, BLACK, fan, 1, border_radius=2)
        for i in range(3):
            line_y = fan.y + i * fan.h//3
            pygame.draw.line(surface, BLACK, (fan.x, line_y), (fan.right, line_y), 1)
    elif shape == "ventilator":
        body = pygame.Rect(x - size//2, y - size//3, size, size*2//3)
        screen = pygame.Rect(x - size//4, y - size//4, size//2, size//2)
        tube = pygame.Rect(x + size//2 - 4, y - size//6, 8, size//3)
        pygame.draw.rect(surface, dark, body.move(shadow, shadow), border_radius=6)
        pygame.draw.rect(surface, color, body, border_radius=6)
        pygame.draw.rect(surface, BLACK, body, 2, border_radius=6)
        pygame.draw.rect(surface, (150,200,255), screen, border_radius=4)
        pygame.draw.rect(surface, BLACK, screen, 2, border_radius=4)
        pygame.draw.rect(surface, GRAY, tube, border_radius=2)
        pygame.draw.rect(surface, BLACK, tube, 1, border_radius=2)
        for i in range(2):
            y_off = screen.y + i * screen.h//2
            pygame.draw.line(surface, BLACK, (screen.x, y_off), (screen.right, y_off), 2)
    elif shape == "figure":
        # 威小龙手办：一个Q版小龙站在底座上
        base = pygame.Rect(x - size//3, y + size//4, size*2//3, size//4)
        body = pygame.Rect(x - size//5, y - size//6, size*2//5, size//2)
        head = pygame.Rect(x - size//4, y - size//2, size//2, size//2)
        tail = pygame.Rect(x + size//4, y - size//6, size//3, size//6)
        # 阴影
        pygame.draw.rect(surface, dark, base.move(shadow, shadow), border_radius=4)
        pygame.draw.rect(surface, dark, body.move(shadow, shadow), border_radius=6)
        pygame.draw.rect(surface, dark, head.move(shadow, shadow), border_radius=8)
        pygame.draw.rect(surface, dark, tail.move(shadow, shadow), border_radius=3)
        # 主体
        pygame.draw.rect(surface, GRAY, base, border_radius=4)
        pygame.draw.rect(surface, color, body, border_radius=6)
        pygame.draw.rect(surface, color, head, border_radius=8)
        pygame.draw.rect(surface, color, tail, border_radius=3)
        # 边框
        for part in [base, body, head, tail]:
            pygame.draw.rect(surface, BLACK, part, 2, border_radius=4 if part==base else 6 if part!=tail else 3)
        # 眼睛
        eye_l = pygame.Rect(head.x + 4, head.y + 4, 6, 6)
        eye_r = pygame.Rect(head.right - 10, head.y + 4, 6, 6)
        pygame.draw.ellipse(surface, WHITE, eye_l)
        pygame.draw.ellipse(surface, WHITE, eye_r)
        pygame.draw.ellipse(surface, BLACK, eye_l, 2)
        pygame.draw.ellipse(surface, BLACK, eye_r, 2)
        # 角
        horn_l = [(head.x, head.y), (head.x - 8, head.y - 12), (head.x + 4, head.y)]
        horn_r = [(head.right, head.y), (head.right + 8, head.y - 12), (head.right - 4, head.y)]
        pygame.draw.polygon(surface, MAGENTA, horn_l)
        pygame.draw.polygon(surface, MAGENTA, horn_r)
        pygame.draw.polygon(surface, BLACK, horn_l, 2)
        pygame.draw.polygon(surface, BLACK, horn_r, 2)
    else:
        # 备用默认
        pygame.draw.rect(surface, color, (x-size//2, y-size//2, size, size))
        pygame.draw.rect(surface, BLACK, (x-size//2, y-size//2, size, size), 2)

# ========== 主游戏类 ==========
class GachaGame:
    def __init__(self):
        self.state = "main"
        self.collected = set()
        self.current_item = None
        self.draw_anim_timer = 0
        self.card_alpha = 0
        self.particles = []
        self.scroll_y = 0
        self.btn_draw = Button(WIDTH//2-90, 470, 180, 60, "Draw", BUTTON_COLOR, BUTTON_HOVER)
        self.btn_collection = Button(WIDTH-160, 20, 130, 45, "Armory", BUTTON_COLOR, BUTTON_HOVER)
        self.btn_back = Button(30, 20, 100, 45, "Back", BUTTON_COLOR, BUTTON_HOVER)
        self.btn_continue = Button(WIDTH//2-90, 470, 180, 60, "Again", BUTTON_COLOR, BUTTON_HOVER)

    def draw_card(self):
        rand = random.random()
        cum, chosen = 0, 0
        for i, p in enumerate(rarity_probs):
            cum += p
            if rand <= cum: chosen = i; break
        pool = [it for it in items if it["rarity"] == chosen]
        return random.choice(pool)

    def perform_draw(self):
        if self.state == "main":
            self.current_item = self.draw_card()
            idx = items.index(self.current_item)
            self.collected.add(idx)
            self.state = "draw_anim"
            self.draw_anim_timer = 0
            self.card_alpha = 0
            self.particles.clear()

    def update(self):
        if self.state == "draw_anim":
            self.draw_anim_timer += 1
            if self.draw_anim_timer > 30:
                self.state = "show_card"
                cx, cy = WIDTH//2, HEIGHT//2
                for _ in range(30):
                    self.particles.append(Particle(cx, cy, RARITY_COLORS[self.current_item["rarity"]]))
        for p in self.particles[:]:
            p.update()
            if p.life <= 0: self.particles.remove(p)
        if self.state == "show_card" and self.card_alpha < 255:
            self.card_alpha = min(255, self.card_alpha+10)

    def draw_background(self):
        for y in range(HEIGHT):
            r = int(20 + 15*y/HEIGHT)
            g = int(25 + 20*y/HEIGHT)
            b = int(35 + 20*y/HEIGHT)
            pygame.draw.line(screen, (r,g,b), (0,y), (WIDTH,y))

    def draw_main_menu(self):
        mp = pygame.mouse.get_pos()
        t = font_title.render("Delta Force Equipment Draw", True, GOLD)
        screen.blit(t, (WIDTH//2 - t.get_width()//2, 120))
        self.btn_draw.draw(screen, mp)
        self.btn_collection.draw(screen, mp)
        h = font_small.render("Click Draw to get random gear", True, GRAY)
        screen.blit(h, (WIDTH//2 - h.get_width()//2, 550))

    def draw_card_display(self):
        item = self.current_item
        if not item: return
        gc = RARITY_COLORS[item["rarity"]]
        cw, ch = 240, 340
        cx, cy = WIDTH//2-cw//2, 80
        for i in range(4):
            r = pygame.Rect(cx-i*4, cy-i*4, cw+i*8, ch+i*8)
            s = pygame.Surface((r.w, r.h), pygame.SRCALPHA)
            pygame.draw.rect(s, (*gc, max(0,100-i*25)), s.get_rect(), border_radius=20)
            screen.blit(s, (r.x, r.y))
        card = pygame.Surface((cw, ch), pygame.SRCALPHA)
        card.fill((40,45,55, min(self.card_alpha,240)))
        pygame.draw.rect(card, gc, card.get_rect(), 3, border_radius=16)
        draw_shape(card, item["shape"], cw//2, 70, 40, WHITE)
        card.blit(font_med.render(item["name"], True, WHITE), (cw//2 - font_med.render(item["name"], True, WHITE).get_width()//2, 140))
        card.blit(font_small.render(rarity_names[item["rarity"]], True, gc), (cw//2 - font_small.render(rarity_names[item["rarity"]], True, gc).get_width()//2, 185))
        card.blit(font_small.render(item["desc"], True, GRAY), (cw//2 - font_small.render(item["desc"], True, GRAY).get_width()//2, 225))
        card.blit(font_small.render("Collected", True, GOLD), (cw//2 - font_small.render("Collected", True, GOLD).get_width()//2, 270))
        screen.blit(card, (cx, cy))

    def draw_collection_room(self):
        screen.fill(BG_COLOR)
        title_surf = font_title.render("Armory Collection", True, GOLD)
        screen.blit(title_surf, (WIDTH//2 - title_surf.get_width()//2, 10))
        self.btn_back.draw(screen, pygame.mouse.get_pos())

        cols = 4
        card_w, card_h = 130, 155
        gap_x, gap_y = 15, 12
        total_width = cols * card_w + (cols - 1) * gap_x
        start_x = (WIDTH - total_width) // 2
        start_y = 70

        rows = (len(items) + cols - 1) // cols
        total_height = rows * (card_h + gap_y) - gap_y
        visible_height = HEIGHT - 80
        max_scroll = max(0, total_height - visible_height)

        if self.scroll_y < 0: self.scroll_y = 0
        elif self.scroll_y > max_scroll: self.scroll_y = max_scroll

        if max_scroll > 0:
            scroll_bar_x = WIDTH - 15
            scroll_bar_top = 70
            scroll_bar_height = visible_height
            pygame.draw.rect(screen, (50,50,60), (scroll_bar_x, scroll_bar_top, 8, scroll_bar_height), border_radius=4)
            thumb_height = max(30, int(visible_height * (visible_height / (total_height + 1))))
            thumb_y = scroll_bar_top + int((self.scroll_y / max_scroll) * (scroll_bar_height - thumb_height))
            pygame.draw.rect(screen, (120,120,140), (scroll_bar_x, thumb_y, 8, thumb_height), border_radius=4)

        current_y = start_y - self.scroll_y
        for i, item in enumerate(items):
            col = i % cols
            row = i // cols
            x = start_x + col * (card_w + gap_x)
            y = current_y + row * (card_h + gap_y)
            if y + card_h < 60 or y > HEIGHT: continue

            if i in self.collected:
                gc = RARITY_COLORS[item["rarity"]]
                pygame.draw.rect(screen, gc, (x-2, y-2, card_w+4, card_h+4), border_radius=10)
                pygame.draw.rect(screen, CARD_BG, (x, y, card_w, card_h), border_radius=10)
                draw_shape(screen, item["shape"], x + card_w//2, y + 55, 25, WHITE)
                name_surf = font_small.render(item["name"], True, WHITE)
                screen.blit(name_surf, (x + card_w//2 - name_surf.get_width()//2, y + 105))
                rare_surf = font_small.render(rarity_names[item["rarity"]], True, gc)
                screen.blit(rare_surf, (x + card_w//2 - rare_surf.get_width()//2, y + 130))
            else:
                pygame.draw.rect(screen, (60,60,70), (x, y, card_w, card_h), border_radius=10)
                q_surf = font_large.render("?", True, GRAY)
                screen.blit(q_surf, (x + card_w//2 - q_surf.get_width()//2, y + card_h//2 - q_surf.get_height()//2))

    def draw(self):
        self.draw_background()
        mp = pygame.mouse.get_pos()
        if self.state == "main": self.draw_main_menu()
        elif self.state == "draw_anim":
            a = abs(math.sin(self.draw_anim_timer*0.2))*255
            s = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
            s.fill((255,255,255,int(a))); screen.blit(s, (0,0))
            if self.draw_anim_timer>15:
                t = font_med.render("Drawing...", True, WHITE)
                screen.blit(t, (WIDTH//2 - t.get_width()//2, HEIGHT//2-20))
        elif self.state == "show_card":
            self.draw_card_display()
            self.btn_continue.draw(screen, mp)
            self.btn_collection.draw(screen, mp)
            for p in self.particles: p.draw(screen)
        elif self.state == "collection": self.draw_collection_room()

    def handle_events(self):
        for ev in pygame.event.get():
            if ev.type == pygame.QUIT: return False
            if ev.type == pygame.MOUSEBUTTONDOWN and ev.button == 1:
                p = ev.pos
                if self.state == "main":
                    if self.btn_draw.is_clicked(p): self.perform_draw()
                    elif self.btn_collection.is_clicked(p): self.state = "collection"
                elif self.state == "show_card":
                    if self.btn_continue.is_clicked(p): self.state = "main"
                    elif self.btn_collection.is_clicked(p): self.state = "collection"
                elif self.state == "collection":
                    if self.btn_back.is_clicked(p):
                        self.state = "main"
                        self.scroll_y = 0
            if ev.type == pygame.MOUSEWHEEL:
                if self.state == "collection":
                    self.scroll_y -= ev.y * 20
        return True

    def run(self):
        running = True
        while running:
            running = self.handle_events()
            self.update()
            self.draw()
            pygame.display.flip()
            clock.tick(60)
        pygame.quit()
        sys.exit()

if __name__ == "__main__":
    game = GachaGame()
    game.run()