Japan staged another extraordinary World Cup comeback to reach the last 16, overcoming a stunned Spain side who were fortunate to join them in the knockout stage.
In a tournament that has been full of shocks, this was perhaps the most dramatic night so far, with Spain left relying on the result of Germany's game with Costa Rica for them to progress
All four teams had started the evening with a chance of progressing but with Spain needing only a draw to qualify, they were not expected to need any favours from elsewhere.
La Roja were cruising at the break after Alvaro Morata headed home Cesar Azpilicueta's cross from close range, but Japan would quickly turn the game - and the whole of Group E - on its head.
The Samurai Blue sent on substitutes Ritsu Doan and Kaoru Mitoma at half-time and within six minutes of the restart they had both made a huge impact.
Doan equalised with a fierce shot from the edge of the area and, only 142 seconds later, Mitomo retrieved Doan's low cross and found Ao Tanaka to bundle home from close range
The goal was initially ruled out by the assistant referee who signalled Doan's cross had gone out of play, but the Video Assistant Referee ruled Mitomo had reached it in time.
Things got even worse for Spain when Costa Rica took a surprise lead over Germany moments later, because at that stage they were heading out.
The big screens at Khalifa International Stadium showed the updated group table, to the disbelief of the Spain supporters, and pretty much everyone else here too.
Luis Enrique's side were in big trouble, and were showing few signs of knowing how to rescue themselves, with only Germany's late rally sending La Roja through as group runners-up on goal difference.
More follows.
helpHow to play
Rate players out of 10 throughout or after the game. The rater will close 30 minutes after the final whistle.
Rating range key1 = Give it up10 = Pure perfection
Japan
Squad number12Player nameGonda
Squad number4Player nameItakura
Squad number22Player nameYoshida
Squad number3Player nameTaniguchi
Squad number14Player nameIto
Squad number17Player nameTanaka
Squad number13Player nameMorita
Squad number5Player nameNagatomo
Squad number11Player nameKubo
Squad number25Player nameMaeda
Squad number15Player nameKamada
Squad number6Player nameEndo
Squad number8Player nameDoan
Squad number9Player nameMitoma
Squad number16Player nameTomiyasu
Squad number18Player nameAsano
Spain
Squad number23Player nameSimón
Squad number2Player nameAzpilicueta
Squad number16Player nameRodri
Squad number4Player nameP Torres
Squad number14Player nameBalde
Squad number9Player nameGavi
Squad number5Player nameBusquets
Squad number26Player namePedri
Squad number12Player nameNico Williams
Squad number7Player nameMorata
Squad number21Player nameOlmo
Squad number10Player nameAsensio
Squad number11Player nameF Torres
Squad number18Player nameAlba
Squad number20Player nameCarvajal
Squad number25Player nameAnsu Fati
Line-ups
Japan
Formation 3-4-3
12Gonda
4Itakura22Yoshida3Taniguchi
14Ito17Tanaka13Morita5Nagatomo
11Kubo25Maeda15Kamada
12Gonda4ItakuraBooked at 39mins22YoshidaBooked at 45mins3TaniguchiBooked at 44mins14Ito17TanakaSubstituted forEndoat 87'minutes13Morita5NagatomoSubstituted forMitomaat 45'minutes11KuboSubstituted forDoanat 45'minutes25MaedaSubstituted forAsanoat 62'minutes15KamadaSubstituted forTomiyasuat 69'minutesSubstitutes
1Kawashima2Yamane6Endo7Shibasaki8Doan9Mitoma10Minamino16Tomiyasu18Asano19H Sakai20Machino21Ueda23Schmidt24Soma26ItoSpain
Formation 4-3-3
23Simón
2Azpilicueta16Rodri4P Torres14Balde
9Gavi5Busquets26Pedri
12Nico Williams7Morata21Olmo
23Simón2AzpilicuetaSubstituted forCarvajalat 45'minutes16Rodri4P Torres14BaldeSubstituted forAlbaat 68'minutes9GaviSubstituted forAnsu Fatiat 68'minutes5Busquets26Pedri12Nico WilliamsSubstituted forF Torresat 57'minutes7MorataSubstituted forAsensioat 57'minutes21OlmoSubstitutes
1Sánchez3García6Llorente8Koke10Asensio11F Torres13Raya15Guillamón17Pino18Alba19Soler20Carvajal22Sarabia24Laporte25Ansu FatiReferee:Victor GomesAttendance:44,851Live Text
Match ends, Japan 2, Spain 1.
Second Half ends, Japan 2, Spain 1.
Attempt missed. Dani Olmo (Spain) right footed shot from outside the box misses to the left. Assisted by Rodri.
Attempt saved. Dani Olmo (Spain) right footed shot from the right side of the box is saved in the centre of the goal. Assisted by Ferran Torres.
Attempt saved. Marco Asensio (Spain) left footed shot from outside the box is saved in the bottom left corner. Assisted by Rodri.
Attempt blocked. Jordi Alba (Spain) right footed shot from outside the box is blocked.
Substitution, Japan. Wataru Endo replaces Ao Tanaka.
Attempt blocked. Marco Asensio (Spain) left footed shot from outside the box is blocked. Assisted by Dani Olmo.
Marco Asensio (Spain) wins a free kick on the right wing.
Foul by Kaoru Mitoma (Japan).
Attempt saved. Ko Itakura (Japan) header from the centre of the box is saved in the bottom left corner. Assisted by Ritsu Doan with a cross.
Foul by Sergio Busquets (Spain).
Kaoru Mitoma (Japan) wins a free kick on the left wing.
Attempt blocked. Dani Olmo (Spain) right footed shot from outside the box is blocked. Assisted by Pedri.
Attempt blocked. Marco Asensio (Spain) left footed shot from the left side of the box is blocked. Assisted by Jordi Alba.
Attempt missed. Takuma Asano (Japan) right footed shot from outside the box misses to the right. Assisted by Kaoru Mitoma following a fast break.
Ferran Torres (Spain) wins a free kick in the attacking half.
Foul by Kaoru Mitoma (Japan).
Substitution, Japan. Takehiro Tomiyasu replaces Daichi Kamada.
Substitution, Spain. Ansu Fati replaces Gavi.
Get the latest results and goal notifications for any team at the Fifa World Cup by downloading the BBC Sport app: Appleexternal-link - Androidexternal-link - Amazonexternal-link
Get your daily dose of Fifa World Cup reaction, debate & analysis with World Cup Daily on BBC Sounds