ow can I limit rotating an object that uses the device's rotation?

I would like to know how to limit the rotation of a device when it reaches 45 degrees z and x. When I tilt the device, tilts the object too, but it doesnt stop tilting. Anyone know how to limit it?
Since already thank you. Here’s my script.

public Vector3 rotateDir = Vector3.zero;
public float rotateSpeed = 3.0F;


// Update is called once per frame
void Update() {

rotateDir.x = -Input.acceleration.y;
rotateDir.z = Input.acceleration.x;

if (rotateDir.sqrMagnitude > 0.3)
rotateDir.Normalize();

transform.Rotate(rotateDir * rotateSpeed);
}

Use Mathf.Clamp