Rotate box with mouse with non-center pivot

I have a debug box that I want to rotate along the z axis with the mouse cursor but have it’s top left position be the pivot point from where it rotates.

Screenshot 2024-02-21 223551

I have the mouse location and the extents of the box. But having trouble with the calculations.

Any tips? Thanks!