Given the uncertainty of lunar surface "flatness" at the time of landing, how did NASA ensure that the axis will be vertical within 12 degree? Was any specific mechanism provided to ensure this? Or was it a calculated risk and optimism?
Calculated risk and optimism, mostly. The landing sites had been surveyed from orbit and promising flat-looking spots had been selected; Apollo 10 flew over the intended Apollo 11 site at an altitude of around 16 km.
On descent, the commander flying the LM would look for a good flat spotspace to land. The LM had enough fuel to hover at low altitude for a little while looking for a good spot to touch down on. Landing dates were chosen to provide low-angle sunlight which would create shadows to help the commander visualize the terrain ahead of him.
It was the commander's responsibility to either ensure that either the LM came down on a patch of reasonably flat terrain, or abort the landing.
I suspect, though I don't know for sure, that the 12-degree limit was very conservatively determined. If the LM came down on a steeper slope than that, and could not be re-leveled before it had to launch, there would be no reason not to attempt the launch anyway.