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, but I don't believe the performance is great.
Your best option is to budget an additional $100 for an external monitor. They are that inexpensive.