Lloyd Gunton, 17, armed himself with a hammer and knife and wrote a so-called martyrdom letter as part of his plan to attack and kill "non-believers" on the streets of Cardiff.
The teenager, who suffers from an autism spectrum disorder, was convicted of preparing for terrorist acts after a nine-day trial at Birmingham Crown Court last November.
Gunton, who had conducted "virtual surveillance" of targets, including a Justin Bieber concert at the Principality Stadium, was detained at his home in the Llantrisant area of south Wales on June 30 last year.
Ordering Gunton to be detained at Her Majesty's pleasure, Judge Mark Wall QC told the former A-level student: "The police found a rucksack in your room which contained a knife, a hammer and what has been referred to as a suicide note or a martyrdom letter.
"In the martyrdom letter you referred to yourself as a 'Soldier of Isis'.
"The letter was written in such terms that it was obviously to be found and read after you had carried out a terrorist attack."
The judge described the items found in the rucksack as a "terrorist's kit" and said it was clear from Instagram posts in English and Arabic that Gunton had planned to launch an attack on June 30.
The judge told the teenager: "I sentence you on the basis that at the time of your arrest you were within hours of committing an act of atrocity on the streets of Cardiff.
"It is clear that Cardiff was your target. In particular you had made a number of online searches in connection with a concert that was being given by Justin Bieber at the stadium in Cardiff on 30th June.
"Whether you would have targeted people attending that concert or others going about their lawful business in Cardiff that night is not certain.
"It is not possible to estimate how many people would have been murdered or seriously injured by your actions, as the attack was foiled before you could undertake it.
"I am sure that you planned not just the killing of one person but rather mass murder.
"In my judgment I must pass an indeterminate sentence. Your actions show a total disregard for human life.
"I cannot foresee a time when I can be confident that your danger will have ended or decreased sufficiently to enable me to pass a determinate or extended sentence."
The teenager was also convicted of two counts of encouraging terrorism by posting extremist material on Instagram, and two charges of possessing Isis propaganda magazines.
At the start of Gunton's trial it emerged that he had written a note apparently for distribution after his death reading: "I am a soldier of the Islamic State and I have attacked Cardiff today because your government keep on bombing targets in Syria and Iraq.
"There will be more attacks in the future."
Cardiff's Castle quarter was among the areas researched by the boy using Google maps, while other searches sought details of a shopping centre, the city's Central Library and the New Theatre.
Judge Wall lifted reporting restrictions banning the media from naming Gunton after an application to the court by the Press Association.
The Crown opposed the application to lift the restrictions, but the judge ruled that it was in the public interest to identify Gunton, who is 18 in April.