Is it just an idiom, or is it a general rule?
I'm wishing a good day to someone, so I guess there is some kind of metaphorical motion involved. Would it be similar if I wanted to say "vielen Spaß"?
As you correctly say, you are wishing a good day to someone. German "wünschen" (to wish) demands accusative case for the object of the wish, hence in
Ich wünsche Ihnen einen guten Tag.
said good day is accusative. The same is true for the shorter "Guten Tag", where the rest of the sentence is implied.
"Viel Spaß" is also accusative. The reason there is no case marker is that "viel" (eng. "much") is not an adjective but a quantifier, in this case for a mass noun. As such, it doesn't change its form. (It does in the sense of "many", but that is a different matter.)
It is a general rule. "Guten Tag" is short for
Ich wünsche Ihnen/Dir einen guten Tag.
I wish you a good day.
Same with "Viel Spaß" or "Gute Besserung".
Ich wünsche dir viel Spaß / eine gute Besserung.
Compare this to
I wish you a Merry Christmas.
You have to ask the question: Who wishes whom what? (Wer wünscht wem was?)
My wish is directed to the other person, so that is dative. But what I wish is the normal accusative, I think in English it's just called direct object.
Greeting another person = Akkusativ as the subject (you) and object (the person) are different. Tag is masculine and so it has the article der. According to the rule, you should end with an 'n' to the adjective/article. So der becomes den and Gute becomes Guten. That's why we say Guten Tag!. Also, we don't say Guten Nacht, rather we say Gute Nacht as Nacht is feminine.
Lesen Sie den Text
It's der Text which became den Text.
I hope this helps.
I agree with the other answers, which basically say the indefinite article "einen" is implied in the common sense of the greeting. But it may also be helpful to consider a dative case:
Ich sah sie an einem guten Tag.
"I saw her on a good day." The indefinite article is different, but the adjective ending is the same, which perhaps led to the confusion. The nominative case is so common and obvious in day to day speech that the indefinite article can be omitted when greeting someone.