Japan stun Spain but both reach World Cup last 16