Issue with rotating player towards global downwards vector

I’m creating a flight simulator game, and have been trying to write a script to rotate the player towards the global downwards vector. My code is as follows:

func counterPitch(delta):
	if velocity.length() < 0.1:
		return Quaternion.IDENTITY
	
	var dir = -self.transform.basis.z
	if dir.length() < 0.001:
		return Quaternion.IDENTITY
	#just in case. i dont know whats going on here.
	#im praying to the little men that run my laptop for this to work
	var angle = dir.angle_to(Vector3.DOWN)
	var axis = dir.cross(Vector3.DOWN)
	#axis to rotate around is perpendicular to both down vector and forward vector
	#so it SHOULD in theory be rotating directly towards the down vector
	#which it does not in fact do :(
	if axis.length() < 0.001:
		return Quaternion.IDENTITY
	#no rotation if aircraft is going directly up or down
	axis = axis.normalized()
	var magnitude = gravCurve.sample(velocity.length()) * angle * delta
	
	return Quaternion(axis, magnitude)

The axis the player has been rotating around is what I assume is the issue, but I can’t find where the issue itself is in the code.
I’ll link a video demonstrating the issue - it only works as intended when the player is aligned with the global forward axis as far as I can tell.

The issue turned out to be that this calculated the axis within global space, and was then applying it in global space. All that was needed as a fix was to change
axis = axis.normalized
to
axis = (global_transform.basis.inverse()*axis).normalized()

this took far too long to figure out :c