Unfortunately, that setup is not supported by Apple. The iMac cannot be used as a monitor for any of the newest Macs and Target Display mode is no longer a feature of macOS, having been deprecated several years ago.
Please see: Use your iMac as a display with target display mode - Apple Support
Per the link above:
• Your target iMac (monitor) must be using macOS High Sierra 10.13.6 or earlier.
• In all cases the source Mac that you're connecting FROM must have been introduced in 2019 or earlier and have macOS 10.15 Catalina or earlier installed.
You might consider a third-party solution, for example: Luna Display or Duet Display, though I can’t speak for the performance of those.
Do yourself a favor, avoid the headache and just budget an extra $100 for a monitor. Yes, they are that inexpensive.