{ config, pkgs, lib, ... }:

let
  nomarchyLib = import ../../lib { inherit lib; };
  activeThemeName = config.nomarchy.system.theme;
  currentPalette = nomarchyLib.getPalette activeThemeName;

  # Hex color for browser theme (base00 is background)
  themeColor = "#${currentPalette.base00}";

  # Detect light mode from theme name or palette
  isLightTheme = nomarchyLib.isThemeLightMode {
    themeName = activeThemeName;
    assetsPath = ../../themes/palettes;
  };

  browserPolicy = {
    BrowserThemeColor = themeColor;
    BrowserColorScheme = if isLightTheme then "light" else "dark";
  };
in
{
  # Chromium policies
  programs.chromium.extraOpts = lib.mkDefault browserPolicy;

  # Brave browser policies via managed policy file
  environment.etc."brave/policies/managed/nomarchy.json".text = lib.mkDefault (
    builtins.toJSON browserPolicy
  );
}
