/* HDGR Theme Utilization study — faithful JS port of the addon's
   palette → scheme composer (Core/HDGR_SchemeConstants.lua : BuildScheme).
   A flat ~30-color palette per theme is composed into the full token tree,
   then flattened to CSS custom properties so a design can use ALL of it. */
(function () {
  function hexToRgb(h) {
    h = h.replace("#", "");
    return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)];
  }
  // rgba(hex, alpha) → "rgba(r,g,b,a)"
  function a(hex, alpha) { const [r, g, b] = hexToRgb(hex); return `rgba(${r},${g},${b},${alpha})`; }

  // ---- Palettes (mirror the .lua hex, incl. the under-used UI tokens) ----
  const P = {
    ColorblindSafe: {
      label: "ColorblindSafe ★", light: false,
      sunken: "#0c1116", bg: "#101418", panel_soft: "#151d27", panel: "#182029",
      panel_footer: "#1e2934", panel_header: "#222c36", raised: "#32414d", border: "#46515d",
      text: "#f1f5f9", text_header: "#ffffff", text_label: "#aab6c3", text_dim: "#7e8b99",
      text_disabled: "#5f6a75", text_inverse: "#ffffff",
      button_normal: "#32414d", button_hover: "#3c4c59", button_active: "#182029",
      accent: "#4ea3f1", accent_brighter: "#93c5fd", accent_darker: "#1d4f75",
      success: "#00a896", warning: "#f2c94c", error: "#d84c8b", error_deep: "#8f2d59",
      text_link: "#4ea3f1", text_link_hover: "#ffa552",
      tab_active_bg: "#182029", tab_active_text: "#f1f5f9", popup_selected_bg: "#3c4c59",
      popup_selected_text: "#f1f5f9", statusline_bg: "#1e2934", text_on_accent: "#0c1116",
      float_bg: "#151d27", float_border: "#4ea3f1",
      diag_error: "#d84c8b", diag_warn: "#f2c94c", diag_info: "#4ea3f1", diag_hint: "#00a896",
    },
    Mocha: {
      label: "Catppuccin Mocha", light: false,
      sunken: "#11111b", bg: "#1e1e2e", panel_soft: "#181825", panel: "#313244",
      panel_footer: "#45475a", panel_header: "#45475a", raised: "#585b70", border: "#6c7086",
      text: "#cdd6f4", text_header: "#cdd6f4", text_label: "#bac2de", text_dim: "#a6adc8",
      text_disabled: "#6c7086", text_inverse: "#1e1e2e",
      button_normal: "#45475a", button_hover: "#585b70", button_active: "#313244",
      accent: "#89b4fa", accent_brighter: "#b4befe", accent_darker: "#1e66f5",
      success: "#a6e3a1", warning: "#f9e2af", error: "#f38ba8", error_deep: "#9d4060",
      text_link: "#89b4fa", text_link_hover: "#b4befe",
      tab_active_bg: "#1e1e2e", tab_active_text: "#cdd6f4", popup_selected_bg: "#313244",
      popup_selected_text: "#cdd6f4", statusline_bg: "#181825", text_on_accent: "#1e1e2e",
      float_bg: "#181825", float_border: "#89b4fa",
      diag_error: "#f38ba8", diag_warn: "#f9e2af", diag_info: "#89b4fa", diag_hint: "#94e2d5",
    },
    TokyonightNight: {
      label: "Tokyonight Night", light: false,
      sunken: "#16161e", bg: "#1a1b26", panel_soft: "#1f2335", panel: "#292e42",
      panel_footer: "#3b4261", panel_header: "#414868", raised: "#545c7e", border: "#3b4261",
      text: "#c0caf5", text_header: "#c0caf5", text_label: "#a9b1d6", text_dim: "#737aa2",
      text_disabled: "#565f89", text_inverse: "#1a1b26",
      button_normal: "#3b4261", button_hover: "#414868", button_active: "#292e42",
      accent: "#7aa2f7", accent_brighter: "#89ddff", accent_darker: "#3d59a1",
      success: "#9ece6a", warning: "#e0af68", error: "#f7768e", error_deep: "#db4b4b",
      text_link: "#7aa2f7", text_link_hover: "#89ddff",
      tab_active_bg: "#7aa2f7", tab_active_text: "#0c0e14", popup_selected_bg: "#2f3549",
      popup_selected_text: "#c0caf5", statusline_bg: "#16161e", text_on_accent: "#0c0e14",
      float_bg: "#1a1b26", float_border: "#29a2c6",
      diag_error: "#db4b4b", diag_warn: "#e0af68", diag_info: "#0db9d7", diag_hint: "#1abc9c",
    },
    RosePineMain: {
      label: "Rosé Pine", light: false,
      sunken: "#191724", bg: "#1f1d2e", panel_soft: "#21202e", panel: "#26233a",
      panel_footer: "#403d52", panel_header: "#403d52", raised: "#524f67", border: "#6e6a86",
      text: "#e0def4", text_header: "#e0def4", text_label: "#908caa", text_dim: "#6e6a86",
      text_disabled: "#524f67", text_inverse: "#191724",
      button_normal: "#403d52", button_hover: "#524f67", button_active: "#26233a",
      accent: "#9ccfd8", accent_brighter: "#ebbcba", accent_darker: "#31748f",
      success: "#31748f", warning: "#f6c177", error: "#eb6f92", error_deep: "#9d4d68",
      text_link: "#9ccfd8", text_link_hover: "#ebbcba",
      tab_active_bg: "#26233a", tab_active_text: "#e0def4", popup_selected_bg: "#403d52",
      popup_selected_text: "#e0def4", statusline_bg: "#1f1d2e", text_on_accent: "#191724",
      float_bg: "#1f1d2e", float_border: "#403d52",
      diag_error: "#eb6f92", diag_warn: "#f6c177", diag_info: "#9ccfd8", diag_hint: "#c4a7e7",
    },
    GruvboxDarkHard: {
      label: "Gruvbox Dark", light: false,
      sunken: "#1d2021", bg: "#282828", panel_soft: "#32302f", panel: "#3c3836",
      panel_footer: "#504945", panel_header: "#504945", raised: "#665c54", border: "#7c6f64",
      text: "#ebdbb2", text_header: "#fbf1c7", text_label: "#d5c4a1", text_dim: "#bdae93",
      text_disabled: "#a89984", text_inverse: "#282828",
      button_normal: "#504945", button_hover: "#665c54", button_active: "#3c3836",
      accent: "#83a598", accent_brighter: "#8ec07c", accent_darker: "#458588",
      success: "#b8bb26", warning: "#fabd2f", error: "#fb4934", error_deep: "#cc241d",
      text_link: "#83a598", text_link_hover: "#8ec07c",
      tab_active_bg: "#3c3836", tab_active_text: "#b8bb26", popup_selected_bg: "#83a598",
      popup_selected_text: "#504945", statusline_bg: "#504945", text_on_accent: "#1d2021",
      float_bg: "#3c3836", float_border: "#83a598",
      diag_error: "#fb4934", diag_warn: "#fabd2f", diag_info: "#83a598", diag_hint: "#8ec07c",
    },
    Nord: {
      label: "Nord", light: false,
      sunken: "#2e3440", bg: "#2e3440", panel_soft: "#3b4252", panel: "#434c5e",
      panel_footer: "#4c566a", panel_header: "#4c566a", raised: "#4c566a", border: "#4c566a",
      text: "#d8dee9", text_header: "#eceff4", text_label: "#e5e9f0", text_dim: "#d8dee9",
      text_disabled: "#4c566a", text_inverse: "#2e3440",
      button_normal: "#434c5e", button_hover: "#4c566a", button_active: "#3b4252",
      accent: "#88c0d0", accent_brighter: "#8fbcbb", accent_darker: "#5e81ac",
      success: "#a3be8c", warning: "#ebcb8b", error: "#bf616a", error_deep: "#8d4147",
      text_link: "#88c0d0", text_link_hover: "#8fbcbb",
      tab_active_bg: "#5e81ac", tab_active_text: "#d8dee9", popup_selected_bg: "#5e81ac",
      popup_selected_text: "#d8dee9", statusline_bg: "#434c5e", text_on_accent: "#2e3440",
      float_bg: "#2e3440", float_border: "#d8dee9",
      // Nord's canonical diag.warn is PURPLE (nord15), NOT semantic yellow.
      diag_error: "#bf616a", diag_warn: "#b48ead", diag_info: "#5e81ac", diag_hint: "#81a1c1",
    },
    SolarizedLight: {
      label: "Solarized Light", light: true,
      sunken: "#d8cfb0", bg: "#e1d9bf", panel_soft: "#e8e0c6", panel: "#efe8d2",
      panel_footer: "#f4eede", panel_header: "#f4eede", raised: "#fdf6e3", border: "#a6b08f",
      text: "#657b83", text_header: "#586e75", text_label: "#657b83", text_dim: "#839496",
      text_disabled: "#93a1a1", text_inverse: "#fdf6e3",
      button_normal: "#fdf6e3", button_hover: "#f4eede", button_active: "#efe8d2",
      accent: "#268bd2", accent_brighter: "#2aa198", accent_darker: "#1a6090",
      success: "#859900", warning: "#b58900", error: "#dc322f", error_deep: "#993b2d",
      text_link: "#268bd2", text_link_hover: "#2aa198",
      tab_active_bg: "#fdf6e3", tab_active_text: "#586e75", popup_selected_bg: "#93a1a1",
      popup_selected_text: "#073642", statusline_bg: "#eee8d5", text_on_accent: "#fdf6e3",
      float_bg: "#eee8d5", float_border: "#586e75",
      diag_error: "#dc322f", diag_warn: "#b58900", diag_info: "#2aa198", diag_hint: "#859900",
    },
  };

  // ---- Composer: palette → flat CSS custom property map ----
  function compose(c) {
    return {
      // surfaces (7-step ramp + derived)
      "--surface-sunken": c.sunken,
      "--surface-canvas-solid": c.bg,
      "--surface-panel-soft": c.panel_soft,
      "--surface-panel": c.panel,
      "--surface-panel-footer": c.panel_footer,
      "--surface-panel-header": c.panel_header,
      "--surface-raised": c.raised,
      "--surface-hover": a(c.accent, 0.12),
      "--surface-selected": a(c.accent, 0.18),
      "--surface-overlay": a(c.bg, 0.6),
      "--surface-divider": a(c.border, 0.55),
      "--surface-statusline": c.statusline_bg,
      // borders
      "--border-default": c.border,
      "--border-subtle": a(c.border, 0.55),
      "--border-strong": c.text_dim,
      "--border-focus": c.accent,
      "--border-selected": a(c.accent, 0.95),
      // text
      "--text-primary": c.text,
      "--text-heading": c.text_header,
      "--text-label": c.text_label,
      "--text-dim": c.text_dim,
      "--text-disabled": c.text_disabled,
      "--text-inverse": c.text_inverse,
      "--text-link": c.text_link || c.accent,
      "--text-link-hover": c.text_link_hover || c.accent_brighter,
      "--text-collected": c.text,
      "--text-uncollected": c.accent,
      "--text-on-accent": c.text_on_accent,
      "--text-numeric": c.warning,
      // semantic + accent steps
      "--accent": c.accent,
      "--accent-brighter": c.accent_brighter,
      "--accent-darker": c.accent_darker,
      "--success": c.success,
      "--warning": c.warning,
      "--error": c.error,
      "--error-deep": c.error_deep,
      // UI-namespace tokens (the under-used ones)
      "--tab-active-bg": c.tab_active_bg,
      "--tab-active-text": c.tab_active_text,
      "--popup-sel-bg": c.popup_selected_bg,
      "--popup-sel-text": c.popup_selected_text,
      "--float-bg": c.float_bg,
      "--float-border": c.float_border,
      "--diag-error": c.diag_error,
      "--diag-warn": c.diag_warn,
      "--diag-info": c.diag_info,
      "--diag-hint": c.diag_hint,
      // region tints (grouping)
      "--region-a": a(c.success, 0.22),
      "--region-b": a(c.accent, 0.20),
      "--region-c": a(c.warning, 0.12),
      // pin states
      "--pin-default": c.accent,
      "--pin-selected": c.warning,
      "--pin-applied": c.success,
      "--pin-done": c.text_dim,
      "--pin-invalid": c.error,
      "--accent-glow": a(c.accent, 0.65),
    };
  }

  function applyScheme(el, key) {
    const c = P[key]; if (!c) return;
    const map = compose(c);
    for (const k in map) el.style.setProperty(k, map[k]);
    el.classList.toggle("is-light", !!c.light);
  }

  window.HDGR_THEMES = { P, compose, applyScheme, keys: Object.keys(P) };
})();
