So I found out that the issue disappears if I set compatibility mode in the settings.
Are there any downsides of using it? Based of its description I'd say that the option should act absolutely reverse
upd: actually now I can see that the focus is not lost in the standard mode, I can see it by the title bar of Safari for example, but it would react to any key press with the system error sound until I click safari again. In compatibility mode a window loses focus but it's correctly returned to it on esc.