How to customize page header with current or previous level 1 headline displayed in corner?

Someone already did this for you! Check out the hydra package:

Regarding placing the heading on the left and the page number on the right, instead of placing left and right at the same time, I recommend placing both at the same line with a filling space between them:

// Displays "Left" to the left and "Right" to the right
Left #h(1fr) Right

For more complex setups (with, say, three sections in the header instead of just two), use a multi-column grid (Grid Function – Typst Documentation).