Geometrical theory of whispering-gallery modes