They knew the war was lost at the time of Hiroshima, but inside Japan there was no consensus about the endgame. There was a widespread belief inside the ruling elite that a peace with honor could be had by exacting huge losses on the enemy in the first battle. That is why they did not surrender.
My high school teacher said in a class that they new before Pearl Harbor that it was a lost war. That was ofcourse a somewhat casual comment. It was a way to emphasize their military thinking. They understood at the time that the US was overall a more powerful country and that they would not be able to defeat them militarily. The objective of Pearl Harbor was to destroy their fleet so that Japan could occupy as much of Asia as possible. Then they would negotiate to keep most of these assets. They saw the US as reluctant to wage a war. They did not expect them to be willing to fight to the bitter end. They did not see the Nuclear bomb coming, or the USSR invading - and they did not foresee how the public mood in the US would fundamentally change after Pearl Harbor.
They didn't.
There was even a failed coup against Hirohito shortly before the signing of the formal declaration of surrender.
The military of Japan would never admit they were defeated, and would sooner bleed themselves dry before admitting to that. It's mainly a cultural thing.