As much as we may try to deny it, Earth’s cycle of day and night rules our lives.

When the sun sets, the encroaching darkness sets off a chain of molecular events spreading from our eyes to our pineal gland, which oozes a hormone called melatonin into the brain. When the melatonin latches onto neurons, it alters their electrical rhythm, nudging the brain into the realm of sleep.

At dawn, sunlight snuffs out the melatonin, forcing the brain back to its wakeful pattern again.

We fight these cycles each time we stay up late reading our smartphones, suppressing our nightly dose of melatonin and waking up grumpy the next day. We fly across continents as if we could instantly reset our inner clocks. But our melatonin-driven sleep cycle lags behind, leaving us drowsy in the middle of the day.

Scientists have long wondered how this powerful cycle got its start. A new study on melatonin hints that it evolved some 700 million years ago. The authors of the study propose that our nightly slumbers evolved from the rise and fall of our tiny oceangoing ancestors, as they swam up to the surface of the sea at twilight and then sank in a sleepy fall through the night.

To explore the evolution of sleep, scientists at the European Molecular Biology Laboratory in Germany study the activity of genes involved in making melatonin and other sleep-related molecules. Over the past few years, they have compared the activity of these genes in vertebrates like us with their activity in a distantly related invertebrate — a marine worm called Platynereis dumerilii.