There are two basic approaches you can use to react to system theme changes.
First, you can create a static theme that includes the "theme" and "dark_theme" objects in your theme’s manifest.json file. I managed to find an example theme that automatically changes based on the user’s OS light/dark preferences. That theme was originally attacked to Issue 1629228, comment #3.
Second, you can create a dynamic theme. Inside that you could use matchMedia to detect when the user’s prefers-color-scheme changes and dynamically update the theme using theme.update(). See this blog post by David Walsh for an example of how to use matchMedia like this.
I’d recommend going with the first approach as it’s more straightforward.