local function SCAR_DisableAIAndLocks()
    local p = Game_GetLocalPlayer()
    if not p then return end

    ----------------------------------------------------------------
    -- 0) HARD KILL-SWITCHES (so leftover listeners do nothing)
    ----------------------------------------------------------------
    -- For your newer guarded systems (if present)
    _SCAR_LOCK_ARMY_ON = false
    _SCAR_LOCK_ARMY_HANDLER_ADDED = false

    -- For the minimal-flag version we discussed
    __ARMY_LOCK_ENABLED = false

    -- If you ever add villager lock flag later
    __VILLAGER_LOCK_ENABLED = false

    -- WaitProd unit kill switch
    __WAITPROD_ENABLED = false

    -- WaitProd building kill switch
    __WAITPROD_BUILDING_ENABLED = false

    ----------------------------------------------------------------
    -- 1) CLEAN UP LOCK RULES (only if they exist)
    ----------------------------------------------------------------
    if type(Rule_Remove) == "function" then
        if type(__EconOnly_VillagerInit_Tick) == "function" then
            pcall(Rule_Remove, __EconOnly_VillagerInit_Tick)
        end
        if type(__EconOnly_VillagerGuard_Tick) == "function" then
            pcall(Rule_Remove, __EconOnly_VillagerGuard_Tick)
        end
        if type(__ArmyLock_Tick) == "function" then
            pcall(Rule_Remove, __ArmyLock_Tick)
        end

        -- Best-effort: remove by string name too (covers string-based scheduling)
        pcall(Rule_Remove, "__EconOnly_VillagerInit_Tick")
        pcall(Rule_Remove, "__EconOnly_VillagerGuard_Tick")
        pcall(Rule_Remove, "__ArmyLock_Tick")
        pcall(Rule_Remove, "Tick_Probe")
        pcall(Rule_Remove, "OnUnitSpawn") -- in case someone registered by name
    end

    ----------------------------------------------------------------
    -- 2) REMOVE SPAWN EVENTS (best effort)
    ----------------------------------------------------------------
    if type(Rule_RemovePlayerEvent) == "function" and type(GE_EntitySpawn) == "number" then
        if type(__VillagerLock_OnSpawn) == "function" then
            pcall(Rule_RemovePlayerEvent, __VillagerLock_OnSpawn, p, GE_EntitySpawn)
        end
        if type(__ArmyLock_OnSpawn) == "function" then
            pcall(Rule_RemovePlayerEvent, __ArmyLock_OnSpawn, p, GE_EntitySpawn)
        end

        -- ✅ FIX: Also remove legacy OnUnitSpawn if it exists (old code compatibility)
        if type(OnUnitSpawn) == "function" then
            pcall(Rule_RemovePlayerEvent, OnUnitSpawn, p, GE_EntitySpawn)
        end

        -- ✅ FIX: Remove RiskyAI handler
        if type(__RiskyAI_OnSpawn) == "function" then
            pcall(Rule_RemovePlayerEvent, __RiskyAI_OnSpawn, p, GE_EntitySpawn)
        end

        -- ✅ Remove WaitProd unit handler
        if type(__WaitProd_OnSpawn) == "function" then
            pcall(Rule_RemovePlayerEvent, __WaitProd_OnSpawn, p, GE_EntitySpawn)
        end

        -- ✅ Remove WaitProd building handler
        if type(__WaitProdBuilding_OnSpawn) == "function" then
            pcall(Rule_RemovePlayerEvent, __WaitProdBuilding_OnSpawn, p, GE_EntitySpawn)
        end

        -- Guarded handler name variants (if present)
        if type(_SCAR_OnUnitSpawn_LockArmy) == "function" then
            pcall(Rule_RemovePlayerEvent, _SCAR_OnUnitSpawn_LockArmy, p, GE_EntitySpawn)
        end
        if type(_SCAR_LockArmy_OnSpawn) == "function" then
            pcall(Rule_RemovePlayerEvent, _SCAR_LockArmy_OnSpawn, p, GE_EntitySpawn)
        end
        if type(_SCAR_OnUnitSpawn_LockCombat) == "function" then
            pcall(Rule_RemovePlayerEvent, _SCAR_OnUnitSpawn_LockCombat, p, GE_EntitySpawn)
        end
    end

    ----------------------------------------------------------------
    -- 3) CLEAR GLOBAL STATE (prevents ON->OFF->ON collisions)
    ----------------------------------------------------------------
    __VillagerLock_OnSpawn = nil
    __ArmyLock_OnSpawn     = nil
    OnUnitSpawn            = nil  -- ✅ FIX: Clear legacy handler too
    __EconOnly_VillagerInit_Tick = nil
    __EconOnly_VillagerGuard_Tick = nil
    __ArmyLock_Tick = nil

    -- Clear common global tables used by lock systems
    __ArmyLock_LockedSquads = nil
    __ArmyLock_Installed = nil

    -- ✅ FIX: Clear RiskyAI globals
    __RiskyAI_OnSpawn = nil
    __RiskyAI_Init = nil
    __RiskyAI_LockedSquads = nil
    __RiskyAI_Installed = nil

    -- ✅ Clear WaitProd unit globals
    __WaitProd_OnSpawn = nil
    __WaitProd_Arm = nil
    __WAITPROD_ENABLED = nil
    __WAITPROD_RULES = nil
    __WAITPROD_STATE = nil
    __WAITPROD_TYPES = nil
    __WAITPROD_HOOKED = nil

    -- ✅ Clear WaitProd building globals
    __WaitProdBuilding_OnSpawn = nil
    __WaitProdBuilding_Arm = nil
    __WAITPROD_BUILDING_ENABLED = nil
    __WAITPROD_BUILDING_RULES = nil
    __WAITPROD_BUILDING_STATE = nil
    __WAITPROD_BUILDING_TYPES = nil
    __WAITPROD_BUILDING_HOOKED = nil

    _SCAR_LOCKED_SQUADS = nil
    _SCAR_LOCK_ARMY_ON = nil
    _SCAR_LOCK_ARMY_HANDLER_ADDED = nil
    _SCAR_COMBAT_LOCK_ON = nil
    _SCAR_COMBAT_LOCKED = nil

    -- Also clear probe globals if any were left behind
    g_done = nil
    g_sq = nil
    g_i = nil
    g_running = nil
    g_tags = nil

    ----------------------------------------------------------------
    -- 4) DISABLE AI (ONLY IF IT'S ENABLED)
    ----------------------------------------------------------------
    if type(AI_IsEnabled) == "function" then
        local ok, enabled = pcall(AI_IsEnabled, p)
        if ok and enabled then
            if type(AI_Enable) == "function" then
                pcall(AI_Enable, p, false)
            end
        end
    elseif type(AI_Enable) == "function" then
        pcall(AI_Enable, p, false)
    end

    -- Clear economy overrides for a clean slate
    if type(AI_DisableAllEconomyOverrides) == "function" then
        pcall(AI_DisableAllEconomyOverrides, p)
    end

    -- Optional: kill tasks (helps avoid “old AI tasks” surviving toggles)
    if type(AI_KillAllTasks) == "function" then
        pcall(AI_KillAllTasks)
    end
end

pcall(SCAR_DisableAIAndLocks)