Are Japanese professors responsible for their students' behavior outside the university? The question How should I approach a Tokyo University professor to join their lab? has an answer by RoboKaren saying:

For professors in Japan, taking on students is a tremendous
responsibility and burden. Unlike in the United States or Europe,
professors are responsible for even the extracurricular activities of
students (i.e., getting arrested; showing up drunk and groping
someone; having an apartment so messy that the landlord complains,
etc.). They are also responsible for the student's career after they
graduate.

This sounds very interesting as I have never heard such things about any other country before.
So, I want to ask:

What happens/could happen to a Japanese professor if their student has e.g. a messy apartment?

Is there something similar for undergraduate students (who, I suppose, do not have a professor in the sense above)? Is the university somehow responsible for them?

Where is this responsibility stated? Is it e.g. in the law, or is it rather some non-official convention that everybody follows, or something in between?
Are Japanese professors responsible for their students' behavior outside the university? Legally perhaps not (I'm no expert), but culturally, yes.

What happens/could happen to a Japanese professor if their student has e.g. a messy apartment?

The professor gets disgraced. It's like being the father of someone who later goes on to become a serial killer. Nothing directly happens to you, but it's still going to be very unpleasant. Did you fail as a father? Even if you think you didn't, perhaps your neighbours will think you did.

Is there something similar for undergraduate students (who, I suppose, do not have a professor in the sense above)? Is the university somehow responsible for them?

Yes, if they are being directly supervised by the professor.

Where is this responsibility stated? Is it e.g. in the law, or is it rather some non-official convention that everybody follows, or something in between?

It's a social expectation, similar to how in many societies children are expected to take care of their parents in their old age.
Check out what happened to stem cell scientist Yoshiki Sasai in 2014: his student faked data, leading to a scientific misconduct probe. Prof. Sasai was cleared of misconduct, but he felt deeply ashamed and later committed suicide. Granted, this was an academic issue, but it illustrates what Japan's expectations of their teachers are.
Edit: for a non-academic situations:

Probably the strangest tasks (in the eyes of Western teachers) are things like guarding the campus and ordering the fuel oil. They are also partly responsible for their students outside school. Japanese students have to follow several rules in their leisure time: they are not allowed to smoke and to drink alcohol, to go to discos or to have a job by the side. In case of disregarding these rules, the teacher is obliged to inform the parents or even to make home visits (cf. SCHÜMER 1999, p. 34). As one can see from all this, the tasks of a Japanese teacher go far beyond giving lessons.