Repository: neovim
Issue: 'background' detection occurs too late, causing colorscheme to load twice
Labels: documentation,defaults,startup

### Problem

Neovim automatically sets `'background'` based on whether the terminal is in light or dark mode, both on startup and (on nightly) when the terminal's appearance changes. In the latter case, at least on my machine, the automatic setting of the option occurs too late when the terminal is in light mode, meaning:
- Start up Nvim in a light mode terminal with a configured colorscheme
- The `'bg'` option is initially `"dark"` even though the terminal and OS are in light mode
- The configured colorscheme is applied while `'bg'` is still set to `"dark"`. Depending on the implementation of the colorscheme this could mean that it applies designated colors for its dark version.
- The automatic `'bg'` detection kicks in and sets the option to `"light"`.
- Because of the change the colorscheme is reloaded, this time with a `'bg'` value of `"light"` (which again may cause it to apply specific – in this case the correct – colors).

This is obviously bad because it's expensive and unnecessary, but particularly annoying with themes that adapt to the background because it means that there is a flash of dark before the correct colors are loaded.

### Steps to reproduce

1. Create a minimal configuration using `NVIM_APPNAME` (couldn't figure out how to do what needs to be done with just the issue template), with this structure:
   ```
   .
   ├── colors
   │   └── mycolors.lua
   └── init.lua
   ```
   Contents of `mycolors.lua`:
   ```lua
   -- a very simple mock colorscheme that follows the background

   local bg = vim.o.background

   if bg == "dark" then
       print("the value of 'bg' is: 'dark', thus applying the dark colors")
       vim.api.nvim_set_hl(0, "Normal", {bg = "#000000", fg = "#ffffff"})
   elseif bg == "light" then
       print("the value of 'bg' is: 'light', thus applying the light colors")
       vim.api.nvim_set_hl(0, "Normal", {bg = "#ffffff", fg = "#000000"})
   end

   vim.g.colors_name = "mycolors"
   ```
   Contents of `init.lua`:
   ```lua
   -- this part is optional:

   --vim.api.nvim_create_autocmd("ColorScheme", { callback = function(data)
   --    print(vim.inspect(data))
   --end})

   --vim.api.nvim_create_autocmd("OptionSet", { pattern = "background", callback = function(data)
   --    print(vim.inspect(data))
   --end})

   vim.cmd.colorscheme("mycolors")

   ```
2. Run `NVIM_APPNAME=<your name> nvim` in a light mode terminal (can reproduce in Kitty and Ghostty). A black flicker will be visible before the background becomes bright white as expected (Because nvim is so fast, this is barely noticable, but with a heavy config full of plugins, it is very noticable).
3. Check `:messages`. The order of the messages will be:
   - `the value of 'bg' is: 'dark', thus applying the dark colors`
   - `the value of 'bg' is: 'light', thus applying the light colors`

### Expected behavior

The value of `'background'` is determined and set as early as possible so that it does not trigger reloading of the colorscheme.

### Nvim version (nvim -v)

NVIM v0.11.0-dev-6696ea7

### Vim (not Nvim) behaves the same?

no

### Operating system/version

macOS 15.2

### Terminal name/version

Ghostty 1.0.1, kitty 0.39.0

### $TERM environment variable

xterm-ghostty

### Installation

build from repo