I’m also not sure of how much between St Andrew’s and maybe Kingsbarns I’d want to bother with.
It WAS mostly good from Kingsbarns to Boarhills, a couple of wee fiddly bits in the sand dunes and one section where I think you are committed to the sand, but lots of nice wee trails otherwise. HOWEVER I’d read there has been some recent coastal erosion along that section so not sure what the current state of play is. You can come uphill at Cambo or at Kingsbarnes, take in some lovely singletrack and join and old gravel track that passes by the old RAF airfield (I think) and then minor road back to St Andrews.
Once at Newburgh you could keep it coastal (ish) to Perth and grab train home? This is some of my winter gravel/CX stomping ground, so a couple of squashy bits and quite flat, but nice nonetheless.
https://www.strava.com/routes/3199650264257113000