Okay, I removed M82 from start and end. And I removed M104, M140, and M107 from endcode.
Z drifting down because of M84 doesn't matter, the bed will just go down (Sapphire Pro, CoreXY) and away from the nozzle.
The thing is, when that gcode ends, the printer retracts the filament almost 1 meter! And I don't know why...