{
  pkgs,
  inputs,
  ...
}: let
  vicinae-toggle = pkgs.writeShellScriptBin "vicinae-toggle" ''
    VICINAE_BIN="${inputs.vicinae.packages.${pkgs.system}.default}/bin/vicinae"
    ARGS=("$@")
    if [ ''${#ARGS[@]} -eq 0 ]; then
      ARGS=("toggle")
    fi

    if ! "$VICINAE_BIN" "''${ARGS[@]}" 2>/dev/null; then
      ${pkgs.systemd}/bin/systemctl --user restart vicinae || "$VICINAE_BIN" server &
      sleep 0.4
      exec "$VICINAE_BIN" "''${ARGS[@]}"
    fi
  '';
in {
  services.vicinae = {
    enable = true;
    systemd = {
      enable = true;
      autoStart = true;
      environment = {
        USE_LAYER_SHELL = 1;
      };
    };

    settings = {
      # Escape closes window (vim-like :q behavior)
      escape_key_behavior = "close_window";
      pop_to_root_on_close = true;
      close_on_unfocus = true;

      keybinds = {
        # UI shortcuts
        "open-search-filter" = "control+/"; # vim search
        "open-settings" = "control+,";
        "toggle-action-panel" = "control+B";

        # Vim-like action keybinds
        "action.move-up" = "control+shift+K";
        "action.move-down" = "control+shift+J";
        "action.copy" = "control+Y"; # yank
        "action.copy-name" = "control+shift+Y";
        "action.remove" = "control+D"; # delete
        "action.dangerous-remove" = "control+shift+D";
        "action.edit" = "control+E";
        "action.open" = "control+O";
        "action.refresh" = "control+R";
        "action.pin" = "control+P";
        "action.new" = "control+N";
        "action.save" = "control+S";
      };
      theme = {
        light = {
          name = "matugen";
          icon_theme = "Papirus-Dark";
        };
        dark = {
          name = "matugen";
          icon_theme = "Papirus-Dark";
        };
      };
    };
  };

  # Ensure vicinae themes directory exists for matugen output
  home.file.".local/share/vicinae/themes/.keep".text = "";

  home.packages = [vicinae-toggle];

  wayland.windowManager.hyprland.settings = {
    exec-once = [
      "vicinae server"
    ];
    windowrule = [
      "float 1, match:class ^(vicinae)$"
      "pin 1, match:class ^(vicinae)$"
      "stay_focused 1, match:class ^(vicinae)$"
      "dim_around 1, match:class ^(vicinae)$"
    ];
    bindd = [
      "SUPER, SPACE, Open Vicinae, exec, vicinae-toggle"
      "SUPER, period, Open Emoji Picker, exec, vicinae-toggle deeplink vicinae://launch/core/search-emojis"
      # "SUPER, C, Open Calculator, exec, vicinae-toggle deeplink vicinae://launch/calculator/history"
      "SUPER, V, Open Clipboard History, exec, vicinae-toggle deeplink vicinae://launch/clipboard/history"
      # FIXME: find a way to declaratively install vicinae extension -- --
      "SUPER, comma, Open Nerdfont Search, exec, vicinae-toggle deeplink vicinae://launch/@fbosch/store.vicinae.nerdfont-search/nerdfonts-search"
      "SUPER ALT, H, Open Clipboard History, exec, vicinae-toggle deeplink vicinae://launch/@sovereign/store.vicinae.hypr-keybinds/hyprland-keybinds"
    ];
  };
}
