My son is doing this (the closest description I could find on the internet is Maladaptive Daydreaming), and I think it's his way of dealing with boredom and anxiety. In his head, he is all-powerful (he has fear of germs, and has separation anxiety) so I think this is this is his way of relaxing/escaping the anxiety and/or boredom. As a child, what I hated most was having to do things I didn't want to do, at a time I didn't want to do them, I hated not being free to do what I wanted to do in life, because I was too young, etc. I hated being bored and having to wait.

In his head he is free and powerful and has amazing adventures and battles. Unfortunately he has discovered that it is an excellent escape and is daydreaming in his class a majority of the time, and if we turn off his trigger (TV) he will melt down, even though he is fairly stable temperment-wise. I think he deep-sixed his chance to get into the gifted program because he likely daydreamed through some instructions. So in our case, it is Maladaptive, but if she is just using it as an escape, what you are doing when you cut into her daydreaming is you are taking away her favorite toy, right while she is playing with it...