admin-ajax.php slow website, how to fix

If you want to save the generated css to a file, take a look at file_get_contents() and file_put_contents() native PHP functions. https://www.php.net/manual/en/function.file-put-contents.php

You can also find some Q&A’s on the topic both in SO and WPSE, e.g. https://stackoverflow.com/questions/25559913/write-from-wordpress-plugin-to-text-file-with-php

You can then enqueue the created css file inside wp_enqueue_scripts like any other css file. You might want to wrap the enqueue inside a file_exists conditional check to avoid potential errors.

Another option could be that you enqueue the dynamic css in wp_enqueue_scripts, but wrapped in a suitable if statement.