-- Apex Aeon: Standalone Theme Engine
-- Philosophy: Signal over decoration. Red = danger. Violet = attention.
-- Neither red nor violet ever appears inside the code buffer (spec §1, §12).

local M = {}

M.palette = {
  -- NEUTRALS — the readable field --------------------------------------------
  void    = "#f1f3f7",      -- Background (also: ink on a bright accent fill)
  panel   = "#e7eaf0", -- Dark surface (statusline, floats, cursorline)
  border  = "#c2c8d4",  -- Borders, separators
  stealth = "#c8cdd8",  -- Ignored text only (sub-threshold by design)
  dim     = "#5c6270",   -- Comments, muted labels (ghost)
  text    = "#16181d",    -- Body text (stark / ink)
  punct   = "#7d8390", -- Punctuation / delimiters (lifted neutral)
  op      = "#5c6270",    -- Operators (neutral)
  prop    = "#2e333d",    -- Fields / properties (dim body)

  -- ATTENTION (violet) — cursor, search, matched pairs. NEVER syntax. --------
  focus        = "#7c2fd6",    -- Cursor — the crosshair (hottest mark)
  focus_active = "#8c3dd0", -- Current match, matched bracket / pair
  focus_field  = "#efe2fb",  -- Search-set / selection background

  -- DANGER (red) — error, destructive, alarm. NEVER syntax. ------------------
  danger       = "#c8003a",   -- Error figure, destructive, active border
  danger_field = "#fbe4e9",  -- Error region / delete background
  danger_hot   = "#9e0030", -- Escalation

  -- CODE HUES (buffer-safe) --------------------------------------------------
  info    = "#0080a6",     -- Cyan — info / links
  action  = "#0e6e92", -- Cyan — functions / methods
  builtin = "#3a7d9c",  -- Cyan, dim — self / builtin
  kind    = "#2148c0",     -- Blue — types / kinds
  storage = "#1633a0",  -- Blue, heavy — storage / modifier / attribute
  flow    = "#1633a0",  -- Bright-blue — control-flow navigation
  value   = "#00734d",   -- Green — strings / values
  escape  = "#2b8f63",   -- Green, bright — char / escape
  const   = "#00875a", -- Green, cool — numbers / const
  ok      = "#00875a",  -- Green — success state
  warn    = "#a86b00",  -- Amber — warning (UI only)
  special = "#6a1fb8",  -- Violet-deep — root / special only

  -- DIFF grounds -------------------------------------------------------------
  diff_add = "#e2f3ea",
  diff_chg = "#f5ecda",
  diff_del = "#fbe4e9",

  -- ON-ACCENT INK ------------------------------------------------------------
  ink = "#16181d", -- fg on a dim violet field (search / selection)
}

function M.load()
  vim.cmd "hi clear"
  if vim.fn.exists "syntax_on" then vim.cmd "syntax reset" end
  vim.o.background = "light"
  vim.g.colors_name = "apex-aeon"

  -- Optional transparency: set g:apex_blend or g:apex_transparent to opt in.
  local blend = vim.g.apex_blend
  if type(blend) == "number" then
    vim.o.winblend = blend
    vim.o.pumblend = blend
  elseif vim.g.apex_transparent == true then
    vim.o.winblend = 20
    vim.o.pumblend = 20
  end

  local p = M.palette
  local groups = {
    -- CANVAS & UI -----------------------------------------------------------
    Normal = { fg = p.text, bg = p.void },
    NormalNC = { fg = p.dim, bg = p.void }, -- Non-focused windows
    SignColumn = { bg = p.void },
    FoldColumn = { fg = p.stealth, bg = p.void },
    VertSplit = { fg = p.border }, -- Deprecated in nvim 0.10, but good fallback
    WinSeparator = { fg = p.border },
    EndOfBuffer = { fg = p.void }, -- Hide tildes
    NormalFloat = { fg = p.text, bg = p.panel },
    FloatBorder = { fg = p.border, bg = p.panel },
    MsgArea = { fg = p.text, bg = p.void },
    WinBar = { fg = p.text, bg = p.panel },
    WinBarNC = { fg = p.dim, bg = p.panel },

    -- CURSOR & NAVIGATION ("Attention" — violet) ----------------------------
    Cursor = { fg = p.void, bg = p.focus }, -- Violet crosshair
    TermCursor = { fg = p.void, bg = p.focus },
    CursorLine = { bg = p.panel },
    CursorColumn = { bg = p.panel },
    ColorColumn = { bg = p.panel },
    CursorLineNr = { fg = p.focus, bold = true }, -- Violet line number (you are here)
    LineNr = { fg = p.stealth }, -- Other lines fade out

    -- SELECTION & SEARCH ("Region scanned" — violet field) ------------------
    Visual = { fg = p.ink, bg = p.focus_field }, -- Dim violet field, fg preserved
    VisualNOS = { fg = p.ink, bg = p.focus_field },

    Search = { fg = p.ink, bg = p.focus_field }, -- Other matches: the set
    IncSearch = { fg = p.void, bg = p.focus_active }, -- Acquiring
    CurSearch = { fg = p.void, bg = p.focus_active, bold = true }, -- The one you are on

    -- STATUS & MESSAGES -----------------------------------------------------
    StatusLine = { fg = p.text, bg = p.panel },
    StatusLineNC = { fg = p.dim, bg = p.void },
    WildMenu = { fg = p.void, bg = p.focus_active },
    Pmenu = { fg = p.text, bg = p.panel },
    PmenuSel = { fg = p.void, bg = p.focus, bold = true }, -- Violet menu selection
    PmenuKind = { fg = p.dim, bg = p.panel },
    PmenuKindSel = { fg = p.void, bg = p.focus, bold = true },
    PmenuExtra = { fg = p.dim, bg = p.panel },
    PmenuExtraSel = { fg = p.void, bg = p.focus, bold = true },
    PmenuMatch = { fg = p.info, bg = p.panel, bold = true },
    PmenuMatchSel = { fg = p.void, bg = p.focus, bold = true },
    PmenuSbar = { bg = p.panel },
    PmenuThumb = { bg = p.stealth },
    ErrorMsg = { fg = p.danger },
    WarningMsg = { fg = p.warn },
    MoreMsg = { fg = p.info },
    ModeMsg = { fg = p.text },
    TabLine = { fg = p.dim, bg = p.panel },
    TabLineSel = { fg = p.text, bg = p.void, bold = true },
    TabLineFill = { fg = p.panel, bg = p.panel },
    QuickFixLine = { fg = p.text, bg = p.panel, bold = true },

    -- SYNTAX: typed by grammatical role (spec §7) ---------------------------
    -- blue = kinds · cyan = actions · green = values · neutral = connective
    Comment = { fg = p.dim, italic = true }, -- Ghost — not a target

    Constant = { fg = p.const }, -- Green (cool) — const values
    String = { fg = p.value }, -- Green — strings
    Character = { fg = p.value },
    Number = { fg = p.const },
    Boolean = { fg = p.const },
    Float = { fg = p.const },

    Identifier = { fg = p.text }, -- Variables (calm body)
    Property = { fg = p.prop }, -- Fields / properties (dim body)
    Function = { fg = p.action }, -- Cyan — actions
    Builtin = { fg = p.builtin, italic = true }, -- Cyan dim — self / builtin

    Statement = { fg = p.flow, bold = true }, -- Bright-blue — control flow
    Conditional = { fg = p.flow, bold = true },
    Repeat = { fg = p.flow, bold = true },
    Label = { fg = p.flow },
    Operator = { fg = p.op }, -- Neutral
    Keyword = { fg = p.storage }, -- Blue — storage / modifier
    Exception = { fg = p.flow, bold = true }, -- Control flow (NOT danger — never red in code)

    PreProc = { fg = p.action }, -- Cyan — preprocessor / macro ceremony
    Include = { fg = p.storage }, -- Blue — declarative (use / import)
    Define = { fg = p.action },
    Macro = { fg = p.action, italic = true }, -- Cyan — bang-macros (action + ceremony)
    PreCondit = { fg = p.action },

    Type = { fg = p.kind }, -- Blue — kinds
    StorageClass = { fg = p.storage, bold = true }, -- Blue heavy
    Structure = { fg = p.kind },
    Typedef = { fg = p.kind },
    Attribute = { fg = p.storage, italic = true }, -- Blue declarative — #[derive], decorators

    Special = { fg = p.escape }, -- Green — special tokens (escapes-adjacent)
    SpecialChar = { fg = p.escape }, -- Green bright — string escapes / regex
    Tag = { fg = p.kind }, -- Blue — structural HTML/XML tags
    Delimiter = { fg = p.punct }, -- Neutral, lifted
    Debug = { fg = p.special },

    Underlined = { fg = p.info, underline = true },
    Ignore = { fg = p.stealth },
    Error = { fg = p.danger },
    Todo = { fg = p.void, bg = p.warn, bold = true },
    Title = { fg = p.info, bold = true },

    MatchParen = { fg = p.focus_active, bold = true }, -- Attention — matched pair (violet)
    Whitespace = { fg = p.border },
    NonText = { fg = p.border },
    SpecialKey = { fg = p.border },
    SpellBad = { sp = p.danger, undercurl = true },
    SpellCap = { sp = p.warn, undercurl = true },
    SpellRare = { sp = p.info, undercurl = true },
    SpellLocal = { sp = p.dim, undercurl = true },
    LspReferenceText = { bg = p.panel },
    LspReferenceRead = { bg = p.panel },
    LspReferenceWrite = { bg = p.panel, bold = true },
    LspSignatureActiveParameter = { fg = p.void, bg = p.info, bold = true },
    LspInlayHint = { fg = p.dim, bg = p.panel },
    LspCodeLens = { fg = p.dim },
    LspCodeLensSeparator = { fg = p.stealth },
    LspInfoBorder = { fg = p.border, bg = p.panel },
    LspInfoTitle = { fg = p.dim, bg = p.panel },

    -- DIAGNOSTICS -----------------------------------------------------------
    DiagnosticError = { fg = p.danger },
    DiagnosticWarn = { fg = p.warn },
    DiagnosticInfo = { fg = p.info },
    DiagnosticHint = { fg = p.dim },
    DiagnosticOk = { fg = p.ok },
    DiagnosticDeprecated = { fg = p.dim, strikethrough = true },
    DiagnosticUnnecessary = { fg = p.dim },

    DiagnosticUnderlineError = { undercurl = true, sp = p.danger },
    DiagnosticUnderlineWarn = { undercurl = true, sp = p.warn },
    DiagnosticUnderlineInfo = { undercurl = true, sp = p.info },
    DiagnosticUnderlineHint = { undercurl = true, sp = p.dim },
    DiagnosticUnderlineOk = { undercurl = true, sp = p.ok },
    DiagnosticVirtualTextError = { fg = p.danger, bg = p.panel },
    DiagnosticVirtualTextWarn = { fg = p.warn, bg = p.panel },
    DiagnosticVirtualTextInfo = { fg = p.info, bg = p.panel },
    DiagnosticVirtualTextHint = { fg = p.dim, bg = p.panel },
    DiagnosticVirtualTextOk = { fg = p.ok, bg = p.panel },
    DiagnosticVirtualLinesError = { fg = p.danger, bg = p.panel },
    DiagnosticVirtualLinesWarn = { fg = p.warn, bg = p.panel },
    DiagnosticVirtualLinesInfo = { fg = p.info, bg = p.panel },
    DiagnosticVirtualLinesHint = { fg = p.dim, bg = p.panel },
    DiagnosticVirtualLinesOk = { fg = p.ok, bg = p.panel },
    DiagnosticSignError = { fg = p.danger, bg = p.void },
    DiagnosticSignWarn = { fg = p.warn, bg = p.void },
    DiagnosticSignInfo = { fg = p.info, bg = p.void },
    DiagnosticSignHint = { fg = p.dim, bg = p.void },
    DiagnosticSignOk = { fg = p.ok, bg = p.void },
    DiagnosticFloatingError = { fg = p.danger, bg = p.panel },
    DiagnosticFloatingWarn = { fg = p.warn, bg = p.panel },
    DiagnosticFloatingInfo = { fg = p.info, bg = p.panel },
    DiagnosticFloatingHint = { fg = p.dim, bg = p.panel },
    DiagnosticFloatingOk = { fg = p.ok, bg = p.panel },
    ApexMarkupStrong = { bold = true },
    ApexMarkupItalic = { italic = true },
    ApexMarkupLink = { fg = p.info, underline = true },

    -- DIFF (figure / ground — spec §8) --------------------------------------
    DiffAdd = { fg = p.ok, bg = p.diff_add },
    DiffChange = { fg = p.warn, bg = p.diff_chg },
    DiffDelete = { fg = p.danger, bg = p.diff_del },
    DiffText = { fg = p.void, bg = p.warn, bold = true },

    -- PLUGINS: TELESCOPE ("The HUD") ----------------------------------------
    TelescopeNormal = { bg = p.void },
    TelescopeBorder = { fg = p.border, bg = p.void },
    TelescopePromptNormal = { fg = p.text, bg = p.void },
    TelescopePromptBorder = { fg = p.info, bg = p.void }, -- Cyan input border
    TelescopePromptTitle = { fg = p.void, bg = p.info }, -- Cyan label
    TelescopePreviewTitle = { fg = p.void, bg = p.ok }, -- Green label
    TelescopeResultsTitle = { fg = p.dim, bg = p.panel },
    TelescopeSelection = { fg = p.text, bg = p.focus_field, bold = true }, -- Violet field
    TelescopeMatching = { fg = p.info, bold = true },

    -- PLUGINS: NEO-TREE -----------------------------------------------------
    NeoTreeNormal = { bg = p.void },
    NeoTreeNormalNC = { bg = p.void },
    NeoTreeVertSplit = { fg = p.panel, bg = p.void },
    NeoTreeWinSeparator = { fg = p.panel, bg = p.void }, -- Fade out tree border
    NeoTreeRootName = { fg = p.focus, bold = true }, -- Root = location (violet)
    NeoTreeGitAdded = { fg = p.ok },
    NeoTreeGitConflict = { fg = p.warn },
    NeoTreeGitDeleted = { fg = p.danger },
    NeoTreeGitModified = { fg = p.info },

    -- PLUGINS: GITSIGNS -----------------------------------------------------
    GitSignsAdd = { fg = p.ok, bg = p.void },
    GitSignsChange = { fg = p.warn, bg = p.void },
    GitSignsDelete = { fg = p.danger, bg = p.void },

    -- PLUGINS: CMP (Completion) ---------------------------------------------
    CmpItemAbbrDeprecated = { fg = p.dim, strikethrough = true },
    CmpItemAbbrMatch = { fg = p.info, bold = true },
    CmpItemAbbrMatchFuzzy = { fg = p.info, bold = true },
    CmpItemKindFunction = { fg = p.action },
    CmpItemKindMethod = { fg = p.action },
    CmpItemKindKeyword = { fg = p.flow },
    CmpItemKindVariable = { fg = p.text },

    -- PLUGINS: BLINK.CMP (Completion) ----------------------------------------
    BlinkCmpMenu             = { fg = p.text, bg = p.panel },
    BlinkCmpMenuBorder       = { fg = p.border, bg = p.panel },
    BlinkCmpMenuSelection    = { fg = p.void, bg = p.focus, bold = true },
    BlinkCmpScrollBarThumb   = { bg = p.stealth },
    BlinkCmpScrollBarGutter  = { bg = p.panel },
    BlinkCmpLabel            = { fg = p.text },
    BlinkCmpLabelDeprecated  = { fg = p.dim, strikethrough = true },
    BlinkCmpLabelMatch       = { fg = p.info, bold = true },
    BlinkCmpKindText         = { fg = p.text },
    BlinkCmpKindMethod       = { fg = p.action },
    BlinkCmpKindFunction     = { fg = p.action },
    BlinkCmpKindConstructor  = { fg = p.kind },
    BlinkCmpKindField        = { fg = p.prop },
    BlinkCmpKindVariable     = { fg = p.text },
    BlinkCmpKindClass        = { fg = p.kind },
    BlinkCmpKindInterface    = { fg = p.kind },
    BlinkCmpKindModule       = { fg = p.kind },
    BlinkCmpKindProperty     = { fg = p.prop },
    BlinkCmpKindUnit         = { fg = p.const },
    BlinkCmpKindValue        = { fg = p.const },
    BlinkCmpKindEnum         = { fg = p.kind },
    BlinkCmpKindKeyword      = { fg = p.flow },
    BlinkCmpKindSnippet      = { fg = p.value },
    BlinkCmpKindColor        = { fg = p.info },
    BlinkCmpKindFile         = { fg = p.text },
    BlinkCmpKindReference    = { fg = p.info },
    BlinkCmpKindFolder       = { fg = p.info },
    BlinkCmpKindEnumMember   = { fg = p.const },
    BlinkCmpKindConstant     = { fg = p.const },
    BlinkCmpKindStruct       = { fg = p.kind },
    BlinkCmpKindEvent        = { fg = p.kind },
    BlinkCmpKindOperator     = { fg = p.op },
    BlinkCmpKindTypeParameter = { fg = p.kind },
    BlinkCmpGhostText        = { fg = p.stealth, italic = true },
    BlinkCmpDoc              = { fg = p.text, bg = p.panel },
    BlinkCmpDocBorder        = { fg = p.border, bg = p.panel },
    BlinkCmpDocSeparator     = { fg = p.border, bg = p.panel },
    BlinkCmpDocCursorLine    = { bg = p.border },
    BlinkCmpSignatureHelp    = { fg = p.text, bg = p.panel },
    BlinkCmpSignatureHelpBorder = { fg = p.border, bg = p.panel },
    BlinkCmpSignatureHelpActiveParameter = { fg = p.void, bg = p.info, bold = true },
    BlinkCmpSource           = { fg = p.dim },

    -- PLUGINS: BUFFERLINE -----------------------------------------------------
    BufferLineBackground        = { fg = p.dim, bg = p.panel },
    BufferLineFill              = { bg = p.panel },
    BufferLineBuffer            = { fg = p.dim, bg = p.panel },
    BufferLineBufferSelected    = { fg = p.text, bg = p.void, bold = true },
    BufferLineBufferVisible     = { fg = p.dim, bg = p.panel },
    BufferLineTab               = { fg = p.dim, bg = p.panel },
    BufferLineTabSelected       = { fg = p.text, bg = p.void, bold = true },
    BufferLineTabClose          = { fg = p.dim, bg = p.panel },
    BufferLineModified          = { fg = p.info, bg = p.panel },
    BufferLineModifiedSelected  = { fg = p.info, bg = p.void },
    BufferLineModifiedVisible   = { fg = p.info, bg = p.panel },
    BufferLineSeparator         = { fg = p.border, bg = p.panel },
    BufferLineSeparatorSelected = { fg = p.border, bg = p.void },
    BufferLineSeparatorVisible  = { fg = p.border, bg = p.panel },
    BufferLineIndicatorSelected = { fg = p.focus, bg = p.void }, -- Active = violet
    BufferLineCloseButton       = { fg = p.dim, bg = p.panel },
    BufferLineCloseButtonSelected = { fg = p.focus, bg = p.void },
    BufferLineCloseButtonVisible  = { fg = p.dim, bg = p.panel },
    BufferLineDiagnostic           = { fg = p.dim, bg = p.panel },
    BufferLineDiagnosticSelected   = { fg = p.dim, bg = p.void },
    BufferLineError                = { fg = p.danger, bg = p.panel },
    BufferLineErrorSelected        = { fg = p.danger, bg = p.void, bold = true },
    BufferLineWarning              = { fg = p.warn, bg = p.panel },
    BufferLineWarningSelected      = { fg = p.warn, bg = p.void, bold = true },
    BufferLineInfo                 = { fg = p.info, bg = p.panel },
    BufferLineInfoSelected         = { fg = p.info, bg = p.void, bold = true },
    BufferLineHint                 = { fg = p.dim, bg = p.panel },
    BufferLineHintSelected         = { fg = p.dim, bg = p.void, bold = true },
    BufferLinePick                 = { fg = p.focus, bg = p.panel, bold = true },
    BufferLinePickSelected         = { fg = p.focus, bg = p.void, bold = true },
    BufferLinePickVisible          = { fg = p.focus, bg = p.panel, bold = true },

    -- PLUGINS: WHICH-KEY ------------------------------------------------------
    WhichKey            = { fg = p.flow, bold = true }, -- The key trigger = navigation
    WhichKeySeparator   = { fg = p.stealth },
    WhichKeyGroup       = { fg = p.info },
    WhichKeyDesc        = { fg = p.text },
    WhichKeyFloat       = { bg = p.panel },
    WhichKeyBorder      = { fg = p.border, bg = p.panel },
    WhichKeyTitle       = { fg = p.void, bg = p.focus, bold = true },
    WhichKeyNormal      = { fg = p.text, bg = p.panel },
    WhichKeyValue       = { fg = p.dim },
    WhichKeyIcon        = { fg = p.action },
    WhichKeyIconAzure   = { fg = p.kind },
    WhichKeyIconGreen   = { fg = p.ok },
    WhichKeyIconYellow  = { fg = p.warn },
    WhichKeyIconRed     = { fg = p.danger },
    WhichKeyIconPurple  = { fg = p.special },
    WhichKeyIconCyan    = { fg = p.info },

    -- PLUGINS: TROUBLE --------------------------------------------------------
    TroubleNormal       = { fg = p.text, bg = p.void },
    TroubleNormalNC     = { fg = p.dim, bg = p.void },
    TroubleText         = { fg = p.text },
    TroubleCount        = { fg = p.void, bg = p.focus_active, bold = true },
    TroubleError        = { fg = p.danger },
    TroubleWarning      = { fg = p.warn },
    TroubleHint         = { fg = p.dim },
    TroubleInfo         = { fg = p.info },
    TroubleSource       = { fg = p.dim },
    TroubleCode         = { fg = p.stealth },
    TroubleLocation     = { fg = p.dim },
    TroubleFile         = { fg = p.info, bold = true },
    TroubleIndent       = { fg = p.border },
    TroublePos          = { fg = p.dim },
    TroubleSignError    = { fg = p.danger },
    TroubleSignWarning  = { fg = p.warn },
    TroubleSignHint     = { fg = p.dim },
    TroubleSignInfo     = { fg = p.info },
    TroublePreview      = { fg = p.text, bg = p.focus_field },
    TroubleFocusText    = { fg = p.text, bold = true },

    -- PLUGINS: INDENT-BLANKLINE -----------------------------------------------
    IblIndent     = { fg = p.border },
    IblScope      = { fg = p.flow }, -- Current scope = structural (blue)
    IblWhitespace = { fg = p.border },

    -- PLUGINS: NEOGIT ---------------------------------------------------------
    NeogitBranch                = { fg = p.flow, bold = true }, -- Ref / location
    NeogitRemote                = { fg = p.info },
    NeogitHunkHeader            = { fg = p.text, bg = p.panel, bold = true },
    NeogitHunkHeaderHighlight   = { fg = p.void, bg = p.info, bold = true },
    NeogitDiffAdd               = { fg = p.ok, bg = p.void },
    NeogitDiffDelete            = { fg = p.danger, bg = p.void },
    NeogitDiffContext           = { fg = p.dim, bg = p.void },
    NeogitDiffAddHighlight      = { fg = p.ok, bg = p.diff_add },
    NeogitDiffDeleteHighlight   = { fg = p.danger, bg = p.diff_del },
    NeogitDiffContextHighlight  = { fg = p.text, bg = p.panel },
    NeogitCommitViewHeader      = { fg = p.void, bg = p.info, bold = true },
    NeogitFilePath              = { fg = p.info, underline = true },
    NeogitDiffHeader            = { fg = p.warn, bold = true },
    NeogitDiffHeaderHighlight   = { fg = p.void, bg = p.warn, bold = true },
    NeogitObjectId              = { fg = p.dim },
    NeogitStashes               = { fg = p.special },
    NeogitRebaseDone            = { fg = p.ok },
    NeogitFold                  = { fg = p.stealth },

    -- PLUGINS: AERIAL (Symbols Outline) --------------------------------------
    AerialLine          = { fg = p.ink, bg = p.focus_field },
    AerialLineNC        = { fg = p.dim, bg = p.panel },
    AerialNormal        = { fg = p.text, bg = p.void },
    AerialGuide         = { fg = p.border },
    AerialClass         = { fg = p.kind },
    AerialClassIcon     = { fg = p.kind },
    AerialFunction      = { fg = p.action },
    AerialFunctionIcon  = { fg = p.action },
    AerialMethod        = { fg = p.action },
    AerialMethodIcon    = { fg = p.action },
    AerialConstructor   = { fg = p.kind },
    AerialField         = { fg = p.prop },
    AerialVariable      = { fg = p.text },
    AerialEnum          = { fg = p.kind },
    AerialEnumIcon      = { fg = p.kind },
    AerialInterface     = { fg = p.kind },
    AerialModule        = { fg = p.kind },
    AerialNamespace     = { fg = p.kind },
    AerialPackage       = { fg = p.kind },
    AerialProperty      = { fg = p.prop },
    AerialStruct        = { fg = p.kind },
    AerialType          = { fg = p.kind },
    AerialTypeParameter = { fg = p.kind },
    AerialConstant      = { fg = p.const },
    AerialString        = { fg = p.value },
    AerialNumber        = { fg = p.const },
    AerialBoolean       = { fg = p.const },
    AerialKey           = { fg = p.flow },
    AerialKeyword       = { fg = p.flow },
    AerialOperator      = { fg = p.op },
    AerialNull          = { fg = p.dim },
    AerialArray         = { fg = p.const },
    AerialObject        = { fg = p.kind },
    AerialEvent         = { fg = p.kind },

    -- PLUGINS: DAP-UI ---------------------------------------------------------
    DapUIScope                    = { fg = p.info, bold = true },
    DapUIType                     = { fg = p.kind },
    DapUIDecoration               = { fg = p.border },
    DapUIThread                   = { fg = p.ok },
    DapUIStoppedThread            = { fg = p.danger, bold = true }, -- Halted = alarm
    DapUICurrentFrameName         = { fg = p.danger, bold = true },
    DapUISource                   = { fg = p.dim },
    DapUILineNumber               = { fg = p.stealth },
    DapUIFloatBorder              = { fg = p.border, bg = p.panel },
    DapUIFloatNormal              = { fg = p.text, bg = p.panel },
    DapUIWatchesEmpty             = { fg = p.dim },
    DapUIWatchesValue             = { fg = p.value },
    DapUIWatchesError             = { fg = p.danger },
    DapUIBreakpointsPath          = { fg = p.info },
    DapUIBreakpointsInfo          = { fg = p.dim },
    DapUIBreakpointsCurrentLine   = { fg = p.danger, bold = true },
    DapUIBreakpointsDisabledLine  = { fg = p.stealth },
    DapUIEndofBuffer              = { fg = p.void },
    DapUIModifiedValue            = { fg = p.warn, bold = true },
    DapUIStop                     = { fg = p.danger },
    DapUIStepOver                 = { fg = p.info },
    DapUIStepInto                 = { fg = p.info },
    DapUIStepBack                 = { fg = p.info },
    DapUIStepOut                  = { fg = p.info },
    DapUIRestart                  = { fg = p.ok },
    DapUIUnavailable              = { fg = p.stealth },
    DapUIPlayPause                = { fg = p.ok },

    -- PLUGINS: DAP VIRTUAL TEXT -----------------------------------------------
    NvimDapVirtualText        = { fg = p.dim, italic = true },
    NvimDapVirtualTextChanged = { fg = p.warn, italic = true },
    NvimDapVirtualTextError   = { fg = p.danger, italic = true },
    NvimDapVirtualTextInfo    = { fg = p.info, italic = true },

    -- PLUGINS: NVIM-NOTIFY ----------------------------------------------------
    NotifyERRORBorder = { fg = p.danger },
    NotifyWARNBorder  = { fg = p.warn },
    NotifyINFOBorder  = { fg = p.info },
    NotifyDEBUGBorder = { fg = p.stealth },
    NotifyTRACEBorder = { fg = p.special },
    NotifyERRORIcon   = { fg = p.danger },
    NotifyWARNIcon    = { fg = p.warn },
    NotifyINFOIcon    = { fg = p.info },
    NotifyDEBUGIcon   = { fg = p.stealth },
    NotifyTRACEIcon   = { fg = p.special },
    NotifyERRORTitle  = { fg = p.danger, bold = true },
    NotifyWARNTitle   = { fg = p.warn, bold = true },
    NotifyINFOTitle   = { fg = p.info, bold = true },
    NotifyDEBUGTitle  = { fg = p.stealth, bold = true },
    NotifyTRACETitle  = { fg = p.special, bold = true },
    NotifyERRORBody   = { fg = p.text, bg = p.panel },
    NotifyWARNBody    = { fg = p.text, bg = p.panel },
    NotifyINFOBody    = { fg = p.text, bg = p.panel },
    NotifyDEBUGBody   = { fg = p.dim, bg = p.panel },
    NotifyTRACEBody   = { fg = p.dim, bg = p.panel },

    -- PLUGINS: SATELLITE (Scrollbar) -----------------------------------------
    SatelliteBar      = { bg = p.panel },
    SatelliteCursor   = { fg = p.focus }, -- Cursor position = violet
    SatellitePosition = { fg = p.dim },
    SatelliteError    = { fg = p.danger },
    SatelliteWarning  = { fg = p.warn },
    SatelliteHint     = { fg = p.dim },
    SatelliteInfo     = { fg = p.info },
    SatelliteSearch   = { fg = p.focus_active }, -- Search = violet
    SatelliteGit      = { fg = p.ok },
    SatelliteMark     = { fg = p.warn },

    -- PLUGINS: OIL.NVIM (File Explorer) --------------------------------------
    OilDir            = { fg = p.info, bold = true },
    OilDirIcon        = { fg = p.info },
    OilLink           = { fg = p.flow, italic = true }, -- Symlink = structural (blue)
    OilLinkTarget     = { fg = p.flow },
    OilCopy           = { fg = p.warn, bold = true },
    OilMove           = { fg = p.warn },
    OilChange         = { fg = p.info },
    OilCreate         = { fg = p.ok, bold = true },
    OilDelete         = { fg = p.danger, bold = true }, -- Destructive = danger
    OilPermissionNone = { fg = p.stealth },
    OilPermissionRead = { fg = p.info },
    OilPermissionWrite = { fg = p.warn },
    OilPermissionExecute = { fg = p.ok },
    OilTypeDir        = { fg = p.info },
    OilTypeFile       = { fg = p.text },
    OilTypeLink       = { fg = p.flow },
    OilTypeSpecial    = { fg = p.special },
    OilSize           = { fg = p.dim },
    OilMtime          = { fg = p.stealth },

    -- PLUGINS: NEOTEST --------------------------------------------------------
    NeotestPassed       = { fg = p.ok },
    NeotestFailed       = { fg = p.danger },
    NeotestRunning      = { fg = p.warn },
    NeotestSkipped      = { fg = p.dim },
    NeotestUnknown      = { fg = p.stealth },
    NeotestTest         = { fg = p.text },
    NeotestFile         = { fg = p.info },
    NeotestDir          = { fg = p.info, bold = true },
    NeotestNamespace    = { fg = p.kind },
    NeotestMarked       = { fg = p.focus, bold = true }, -- Mark = attention
    NeotestTarget       = { fg = p.focus },
    NeotestAdapterName  = { fg = p.info, bold = true },
    NeotestWinSelect    = { fg = p.info, bold = true },
    NeotestFocused      = { bold = true },
    NeotestIndent       = { fg = p.border },
    NeotestExpandMarker = { fg = p.dim },
    NeotestWatching     = { fg = p.warn },

    -- PLUGINS: GRUG-FAR (Search & Replace) -----------------------------------
    GrugFarResultsHeader      = { fg = p.void, bg = p.focus, bold = true }, -- Search = violet
    GrugFarResultsMatch       = { fg = p.ink, bg = p.focus_field, bold = true },
    GrugFarResultsMatchAdded  = { fg = p.ok },
    GrugFarResultsMatchRemoved = { fg = p.danger },
    GrugFarResultsLineNo      = { fg = p.dim },
    GrugFarResultsPath        = { fg = p.info, underline = true },
    GrugFarResultsStats       = { fg = p.dim },
    GrugFarInputLabel         = { fg = p.warn, bold = true },
    GrugFarInputPlaceholder   = { fg = p.stealth },
    GrugFarHelpHeader         = { fg = p.dim, bold = true },
    GrugFarHelpWinHeader      = { fg = p.text, bg = p.panel, bold = true },

    -- PLUGINS: TWINNY (AI Ghost Text) ----------------------------------------
    TwinnyAccept       = { fg = p.stealth, italic = true },
    TwinnyHint         = { fg = p.stealth, italic = true },

    -- PLUGINS: GITSIGNS (extended) -------------------------------------------
    GitSignsCurrentLineBlame  = { fg = p.stealth, italic = true },
    GitSignsAddNr             = { fg = p.ok },
    GitSignsChangeNr          = { fg = p.warn },
    GitSignsDeleteNr          = { fg = p.danger },
    GitSignsAddLn             = { fg = p.ok, bg = p.diff_add },
    GitSignsChangeLn          = { fg = p.warn, bg = p.diff_chg },
    GitSignsDeleteLn          = { fg = p.danger, bg = p.diff_del },
    GitSignsAddPreview        = { fg = p.ok, bg = p.diff_add },
    GitSignsDeletePreview     = { fg = p.danger, bg = p.diff_del },
    GitSignsAddInline         = { fg = p.void, bg = p.ok },
    GitSignsDeleteInline      = { fg = p.void, bg = p.danger },
    GitSignsChangeInline      = { fg = p.void, bg = p.warn },
    GitSignsUntracked         = { fg = p.dim },

    -- PLUGINS: RENDER-MARKDOWN ------------------------------------------------
    -- Heading ramp avoids red + violet (both reserved); uses code-safe hues only.
    RenderMarkdownH1         = { fg = p.info, bold = true },
    RenderMarkdownH2         = { fg = p.flow, bold = true },
    RenderMarkdownH3         = { fg = p.kind, bold = true },
    RenderMarkdownH4         = { fg = p.ok, bold = true },
    RenderMarkdownH5         = { fg = p.warn, bold = true },
    RenderMarkdownH6         = { fg = p.dim, bold = true },
    RenderMarkdownH1Bg       = { bg = p.panel },
    RenderMarkdownH2Bg       = { bg = p.panel },
    RenderMarkdownH3Bg       = { bg = p.panel },
    RenderMarkdownH4Bg       = { bg = p.panel },
    RenderMarkdownH5Bg       = { bg = p.panel },
    RenderMarkdownH6Bg       = { bg = p.panel },
    RenderMarkdownCode       = { bg = p.panel },
    RenderMarkdownCodeInline = { fg = p.value, bg = p.panel },
    RenderMarkdownBullet     = { fg = p.info },
    RenderMarkdownQuote      = { fg = p.dim, italic = true },
    RenderMarkdownDash       = { fg = p.border },
    RenderMarkdownLink       = { fg = p.info, underline = true },
    RenderMarkdownSign       = { bg = p.void },
    RenderMarkdownMath       = { fg = p.info },
    RenderMarkdownTableHead  = { fg = p.void, bg = p.info, bold = true },
    RenderMarkdownTableRow   = { fg = p.text },
    RenderMarkdownTableFill  = { fg = p.border },
    RenderMarkdownTodo       = { fg = p.void, bg = p.warn, bold = true },
    RenderMarkdownUnchecked  = { fg = p.stealth },
    RenderMarkdownChecked    = { fg = p.ok },
  }

  for group, highlight in pairs(groups) do
    vim.api.nvim_set_hl(0, group, highlight)
  end

  local links = {
    ["@annotation"] = "Attribute",
    ["@attribute"] = "Attribute",
    ["@character"] = "Character",
    ["@comment"] = "Comment",
    ["@constant"] = "Constant",
    ["@constant.builtin"] = "Constant",
    ["@constant.macro"] = "Macro",
    ["@constructor"] = "Type",
    ["@debug"] = "Debug",
    ["@define"] = "Define",
    ["@exception"] = "Exception",
    ["@field"] = "Property",
    ["@float"] = "Float",
    ["@function"] = "Function",
    ["@function.builtin"] = "Function",
    ["@function.call"] = "Function",
    ["@function.macro"] = "Macro",
    ["@include"] = "Include",
    ["@keyword"] = "Keyword",
    ["@keyword.conditional"] = "Conditional",
    ["@keyword.repeat"] = "Repeat",
    ["@keyword.return"] = "Statement",
    ["@keyword.function"] = "Keyword",
    ["@keyword.operator"] = "Operator",
    ["@keyword.exception"] = "Exception",
    ["@label"] = "Label",
    ["@method"] = "Function",
    ["@method.call"] = "Function",
    ["@module"] = "Structure",
    ["@namespace"] = "Structure",
    ["@number"] = "Number",
    ["@operator"] = "Operator",
    ["@parameter"] = "Identifier",
    ["@preproc"] = "PreProc",
    ["@property"] = "Property",
    ["@punctuation"] = "Delimiter",
    ["@punctuation.bracket"] = "Delimiter",
    ["@punctuation.delimiter"] = "Delimiter",
    ["@punctuation.special"] = "Delimiter",
    ["@repeat"] = "Repeat",
    ["@string"] = "String",
    ["@string.escape"] = "SpecialChar",
    ["@string.regex"] = "SpecialChar",
    ["@string.special"] = "SpecialChar",
    ["@tag"] = "Tag",
    ["@tag.attribute"] = "Property",
    ["@tag.delimiter"] = "Delimiter",
    ["@text"] = "Normal",
    ["@text.title"] = "Title",
    ["@markup.heading"] = "Title",
    ["@markup.heading.1"] = "Title",
    ["@markup.heading.2"] = "Title",
    ["@markup.heading.3"] = "Title",
    ["@markup.heading.4"] = "Title",
    ["@markup.heading.5"] = "Title",
    ["@markup.heading.6"] = "Title",
    ["@markup.link"] = "ApexMarkupLink",
    ["@markup.link.label"] = "ApexMarkupLink",
    ["@markup.link.url"] = "ApexMarkupLink",
    ["@markup.strong"] = "ApexMarkupStrong",
    ["@markup.italic"] = "ApexMarkupItalic",
    ["@markup.strikethrough"] = "DiagnosticDeprecated",
    ["@markup.underline"] = "Underlined",
    ["@markup.raw"] = "String",
    ["@markup.raw.block"] = "String",
    ["@markup.raw.delimiter"] = "Delimiter",
    ["@markup.quote"] = "Comment",
    ["@markup.list"] = "Delimiter",
    ["@markup.list.checked"] = "String",
    ["@markup.list.unchecked"] = "Comment",
    ["@markup.math"] = "Constant",
    ["@markup.environment"] = "Type",
    ["@markup.environment.name"] = "Type",
    ["@type"] = "Type",
    ["@type.builtin"] = "Type",
    ["@type.definition"] = "Type",
    ["@type.qualifier"] = "Keyword",
    ["@variable"] = "Identifier",
    ["@variable.builtin"] = "Builtin",
    ["@variable.parameter"] = "Identifier",
    ["@variable.member"] = "Property",

    ["@lsp.type.boolean"] = "Boolean",
    ["@lsp.type.builtinType"] = "Type",
    ["@lsp.type.class"] = "Type",
    ["@lsp.type.comment"] = "Comment",
    ["@lsp.type.decorator"] = "Attribute",
    ["@lsp.type.enum"] = "Type",
    ["@lsp.type.enumMember"] = "Constant",
    ["@lsp.type.event"] = "Type",
    ["@lsp.type.function"] = "Function",
    ["@lsp.type.interface"] = "Type",
    ["@lsp.type.keyword"] = "Keyword",
    ["@lsp.type.macro"] = "Macro",
    ["@lsp.type.method"] = "Function",
    ["@lsp.type.modifier"] = "Keyword",
    ["@lsp.type.namespace"] = "Structure",
    ["@lsp.type.number"] = "Number",
    ["@lsp.type.operator"] = "Operator",
    ["@lsp.type.parameter"] = "Identifier",
    ["@lsp.type.property"] = "Property",
    ["@lsp.type.regexp"] = "SpecialChar",
    ["@lsp.type.string"] = "String",
    ["@lsp.type.struct"] = "Structure",
    ["@lsp.type.type"] = "Type",
    ["@lsp.type.typeParameter"] = "Type",
    ["@lsp.type.variable"] = "Identifier",

    ["@lsp.mod.deprecated"] = "DiagnosticDeprecated",
    ["@lsp.mod.abstract"] = "Type",
    ["@lsp.mod.declaration"] = "Keyword",
    ["@lsp.mod.defaultLibrary"] = "Constant",
    ["@lsp.mod.definition"] = "Keyword",
    ["@lsp.mod.documentation"] = "Comment",
    ["@lsp.mod.modification"] = "Operator",
    ["@lsp.mod.readonly"] = "Constant",
    ["@lsp.mod.static"] = "Constant",
    ["@lsp.mod.async"] = "Keyword",

    ["@lsp.typemod.function.async"] = "Keyword",
    ["@lsp.typemod.method.async"] = "Keyword",
    ["@lsp.typemod.function.deprecated"] = "DiagnosticDeprecated",
    ["@lsp.typemod.method.deprecated"] = "DiagnosticDeprecated",
    ["@lsp.typemod.variable.deprecated"] = "DiagnosticDeprecated",
    ["@lsp.typemod.variable.readonly"] = "Constant",
    ["@lsp.typemod.parameter.readonly"] = "Constant",
    ["@lsp.typemod.property.readonly"] = "Constant",
    ["@lsp.typemod.variable.static"] = "Constant",
    ["@lsp.typemod.property.static"] = "Constant",
  }

  for group, target in pairs(links) do
    vim.api.nvim_set_hl(0, group, { link = target })
  end
end

M.load()

-- PLUGINS: LUALINE --------------------------------------------------------
-- Export lualine theme from palette for use in lualine config.
-- Mode hues stay distinct: normal=violet(attention), insert=green, visual=cyan,
-- replace=amber, command=violet-deep. fg = void for contrast on bright accents.
do
  local p = M.palette
  M.lualine = {
    normal = {
      a = { fg = p.void, bg = p.focus, gui = "bold" },
      b = { fg = p.text, bg = p.panel },
      c = { fg = p.dim, bg = p.void },
    },
    insert = {
      a = { fg = p.void, bg = p.ok, gui = "bold" },
      b = { fg = p.text, bg = p.panel },
      c = { fg = p.dim, bg = p.void },
    },
    visual = {
      a = { fg = p.void, bg = p.info, gui = "bold" },
      b = { fg = p.text, bg = p.panel },
      c = { fg = p.dim, bg = p.void },
    },
    replace = {
      a = { fg = p.void, bg = p.warn, gui = "bold" },
      b = { fg = p.text, bg = p.panel },
      c = { fg = p.dim, bg = p.void },
    },
    command = {
      a = { fg = p.void, bg = p.special, gui = "bold" },
      b = { fg = p.text, bg = p.panel },
      c = { fg = p.dim, bg = p.void },
    },
    inactive = {
      a = { fg = p.dim, bg = p.panel },
      b = { fg = p.dim, bg = p.panel },
      c = { fg = p.dim, bg = p.void },
    },
  }
end

return M