Group E of the FIFA World Cup was decided in a frenzied finish on the final match day, with Japan topping the Group and Spain going through in second place thanks to goal difference.
Japan beat Spain 2-1 to move to six points in their three games, coming from 1-0 down – Ao Tanaka scored the winner in the 51st minute.
Germany meanwhile won a rollercoaster clash with Costa Rica 4-2 to move to four points, level with Spain but condemned to third place as Spain had a superior goal difference of six.
Germany and Costa Rica now head home from an exciting group.
In Group F, Morocco and Croatia confirmed their advancement to the Round of 16 in first and second place in the group.
Morocco beat Canada 2-1 thanks to goals by Hakim Ziyech and Youssef En-Nesyri in the 4th and 23rd minutes, while Croatia drew 0-0 with Belgium to knock them out.
Belgium have now failed to progress to the knockout stage of a World Cup for the first time since 1998.