Speculation: If your brain is not engaged, especially with learning new stuff during the day, there’s not much for it chew on at night, so it spins into crazy places as if you were hallucinating from free-floating too long in a sensory deprivation tank.
Aside: As discussed here, I don’t agree with Adam Khan’s misgivings here about a wandering mind, although I definitely agree with him about the importance of purpose. (Easier said than done, of course, and I’m not fully convinced that “La lutte elle-même vers les sommets suffit à remplir un cœur d’homme. Il faut imaginer Sisyphe heureux.” or “The struggle itself toward the summit is enough to fill a human heart. One must imagine Sisyphus happy.”)