The
clicking you heard is the motor jumping splines, indicating something is either seized or mis-aligned with your sunroof.
Typically the clicking indicates your cables are frozen. There's a
TSB Toyota put out for maintenance, I'd start with that and see if it fixes your issues. And you should find plenty of threads for reference, this seems to be a weak point of the 4th gen sunroofs.
Let us know what you find once you dig in...