The student, a Syrian national, was arrested early on Monday in the southern town of Montellano near Seville, and has been charged with belonging to a terrorist organisation and possessing explosives.

Jose Luis de Castro, the judge in charge of the court's section for minors, said in his order that "these felonies of enormous gravity" justified the pre-trial detention.

There is "solid evidence" that the suspect obtained various substances used for homemade explosives, as well as manuals for their manufacture, the judge said, adding that the suspect had also pledged loyalty to Islamic State on social media.

The suspect had been living with his mother and 10-year-old sister in Montellano, a town of 7,000 people, for two years. All three have refugee status, according to Montellano's mayor, Curro Gil.

The judge said the youth was "highly radicalised, obsessed with all things military, has camouflage clothing and is tremendously homophobic and antisemitic".

A police spokesperson declined to comment on the case, citing the fact that the suspect was underage.

Newspaper El Mundo on Tuesday cited police sources saying they were certain an attack on the suspect's high school was imminent, which prompted his arrest.

While already under police surveillance on Sunday, the suspect went to an empty field where detonations were heard, the judge said, suggesting he was testing explosives.

According to state news agency EFE, several parents decided to keep their children from the school on Monday after the suspect sent a message to some of his classmates via WhatsApp reading: "Tomorrow's the big day."

(Reporting by David Latona; Editing by Charlie Devereux and Gareth Jones)