Here's another way to think of it:
First, we figure out which direction in the parent space points toward world "up":
Vector3 parentSpaceUp = transform.parent.InverseTransformDirection(Vector3.up);
Then we orient ourselves to point forward in the parent's local coordinate, while aligning our own up vector as close as possible to this vertical:
transform.localRotation = Quaternion.LookRotation(Vector3.forward, parentSpaceUp);
Wherever possible, I like to use direction vectors and quaternions over Euler angles, both to avoid unnecessary trig functions, and also to make sure the code doesn't exhibit bugs at edge cases where the Euler angles wrap around.