The route to extinction in variable environments