Manu Trigueros scored a superb late winner as Villarreal reached the Europa League knockout stages with a nervy 2-1 victory over 10-man Steaua Bucharest.
Trigueros lobbed goalkeeper Florin Nita from the edge of the box three minutes from time at El Madrigal to scupper a battling fightback from Steaua, who narrowly missed out on the last 32.
Nicola Sansone had opened the scoring as the Spanish side dominated the first half but Vlad Achim equalised to set up a tense finish despite the sending off of Gabriel Tamas.
Trigueros thought he had settled the affair but, in such a tight Group L, a 2-2 draw would still have been sufficient for Steaua.
They could have snatched that with the last kick of the game but Sergio Asenjo saved from Bojan Golubovic.
All four teams in the group went into the final round of fixtures with a chance of qualifying.
Villarreal looked the most likely after going ahead on 16 minutes when the impressive Mario Gaspar teed up Sansone. Nita parried the striker's initial attempt but Sansone reacted quickly to tap in at the second time of asking.
Steaua threatened an immediate reply as Fernando Boldrin fed Adrian Popa inside the box but his shot was blocked.
That proved a rare attack from Steaua in the first half.
Sansone got behind the defence again as he latched onto a long ball but Nita comfortably saved his long-range effort.
Cedric Bakambu spurned two good chances as he headed over and then failed to take the ball cleanly around Nita, allowing Tamas to get back and clear. Gaspar fired wide after being played in by Sansone on the turn.
The complexion of the game changed after the break as Steaua found another gear. Tamas did brilliantly to skip past a challenge from Jaume Costa on the right and his pinpoint cross was firmly headed in by substitute Achim on 55 minutes.
Bakambu could have put Villarreal back ahead when he seized on a poor backpass from Marko Momcilovic and rounded Nita but missed the target from a tight angle.
William Amorim thought he was going to give the Romanians the lead when he bent a fine low shot through a crowded penalty area but Asenjo produced a fine save.
Bakambu wasted another chance as he was left alone by a Steaua defence mistakenly assuming he was offside but his control let him down. Sansone flicked another effort wide.
Knowing a 1-1 draw would not be good enough on head-to-head rules, Steaua pushed themselves but suffered a blow when Tamas was sent off after pulling back Roberto Soriano to earn a second yellow card.
It seemed the killer blow had come when Trigueros produced a world-class finish to put Villarreal ahead late on. Nita then made further saves from Bruno Soriano and Roberto Soriano as Villarreal sought a third.
But, with the head to head favouring Steaua in the event of a 2-2 draw, it was not quite over and Golubovic almost had the final word.