Size first, then quality — and pixels only as a last resort
The tool looks for the best image that fits your limit. It lowers quality before it touches the picture's dimensions, because that costs less visible detail.
It first checks whether your image fits at full size with a sensible quality setting. If it does, it searches for the highest quality that still fits, and your picture keeps every pixel. Only when even a modest quality is too heavy does it make the picture smaller, and at that point it searches for the largest size that fits at around 70 percent quality, then spends any bytes left over on sharpening the quality back up. That order is deliberate: a slightly smaller, clean picture looks better than a full-size one covered in the square blocks that very low JPG quality produces.