-- Tokyo Night, following the desktop's color scheme.
--
-- Moon when Panama is dark, Day when it is light. Same theme family either way,
-- so the editor keeps the identity the rest of the desktop has rather than
-- becoming a different-looking application when the scheme flips.
--
-- The readability fixes below are deliberately dark-only. They were written
-- against Moon's palette -- a pale comment color, a mid-gray gutter -- and
-- applying them to Day would put light gray text on a light background, which
-- is exactly the legibility problem they exist to solve, inverted.

local panama = require("config.panama")

return {
  {
    "folke/tokyonight.nvim",
    opts = function()
      local light = panama.is_light()

      return {
        style = panama.tokyonight_style(),
        light_style = "day",
        transparent = true,

        on_colors = function(colors)
          if light then
            -- Day's defaults are tuned for a light ground and mostly need no
            -- help. Comments are the exception: the shipped #848cb5 measures
            -- 2.54:1 against the #e1e2e7 background, well under the 3:1 floor
            -- for secondary text. This is 3.25:1 -- readable, and still clearly
            -- dimmer than Normal's 4.52:1 so it does not compete with code.
            colors.comment = "#7079a8"
            return
          end

          colors.comment = "#a0a7c5"
          colors.fg_gutter = "#787f93"
          colors.terminal_black = "#828bb8"
        end,

        on_highlights = function(highlights, colors)
          -- Inline code in markdown is invisible at both lightnesses without
          -- an explicit background, because the theme leaves it unset.
          highlights["@markup.raw.markdown_inline"] = {
            bg = light and colors.bg_highlight or colors.terminal_black,
            fg = colors.fg,
          }
          highlights["RenderMarkdownCodeInline"] = {
            bg = light and colors.bg_highlight or colors.terminal_black,
            fg = colors.fg,
          }

          -- LspReference* readability: DiagnosticUnnecessary dims fg for unused
          -- imports, making text nearly invisible against LspReferenceText's
          -- background.
          local reference = {
            bg = light and colors.bg_visual or colors.fg_gutter,
            fg = colors.fg,
          }
          highlights["LspReferenceText"] = reference
          highlights["LspReferenceRead"] = reference
          highlights["LspReferenceWrite"] = reference
        end,
      }
    end,
  },
  {
    "LazyVim/LazyVim",
    opts = {
      colorscheme = "tokyonight",
    },
  },
}
