Adaptive ontogenetic shifts in larval responses to environmental cues