// Single-source color palette tokens for Moonrockz, Minimal, and Warm Editorial
// across Light and Dark appearances. Decorative accent tokens stay vivid while
// foreground text tokens (`--mc-*-fg`, `--rui-primary-foreground`) meet WCAG AA
// (>= 4.5:1) against their surfaces.

///|
/// An sRGB color with 8-bit channels (`0..=255`).
priv struct Rgb {
  r : Int32
  g : Int32
  b : Int32
}

///|
fn Rgb::rgb(r : Int32, g : Int32, b : Int32) -> Rgb {
  { r, g, b, }
}

///|
fn hex_byte(v : Int32) -> String {
  let digits = "0123456789abcdef"
  let hi = ((v >> 4) & 0xf).to_int()
  let lo = (v & 0xf).to_int()
  let buf = StringBuilder()
  buf.write_char(digits.get_char(hi).unwrap_or('0'))
  buf.write_char(digits.get_char(lo).unwrap_or('0'))
  buf.to_string()
}

///|
fn Rgb::hex(self : Rgb) -> String {
  "#\{hex_byte(self.r)}\{hex_byte(self.g)}\{hex_byte(self.b)}"
}

///|
fn Rgb::rgba(self : Rgb, alpha : Double) -> String {
  "rgba(\{self.r},\{self.g},\{self.b},\{alpha})"
}

///|
fn light_dark_rgb(light : Rgb, dark : Rgb) -> String {
  "light-dark(\{light.hex()},\{dark.hex()})"
}

///|
fn light_dark_rgba(
  light : Rgb,
  light_alpha : Double,
  dark : Rgb,
  dark_alpha : Double,
) -> String {
  "light-dark(\{light.rgba(light_alpha)},\{dark.rgba(dark_alpha)})"
}

///|
/// Complete semantic color tokens for one `(ColorScheme, dark)` appearance.
priv struct SchemeTokens {
  background : Rgb
  foreground : Rgb
  card : Rgb
  popover : Rgb
  sidebar : Rgb
  muted : Rgb
  muted_foreground : Rgb
  accent : Rgb
  primary : Rgb
  primary_foreground : Rgb
  brand : Rgb
  brand_fg : Rgb
  brand_surface_alpha : Double
  brand_border_alpha : Double
  cobalt : Rgb
  cobalt_surface_alpha : Double
  good : Rgb
  good_fg : Rgb
  good_surface_alpha : Double
  wait : Rgb
  wait_fg : Rgb
  wait_surface_alpha : Double
  destructive : Rgb
  bad_fg : Rgb
  destructive_surface_alpha : Double
  code_bg : Rgb
}

///|
fn SchemeTokens::of(scheme : ColorScheme, dark~ : Bool) -> SchemeTokens {
  let cobalt = if dark { Rgb::rgb(91, 155, 255) } else { Rgb::rgb(41, 98, 204) }
  let cobalt_surface_alpha = if dark { 0.13 } else { 0.09 }
  let good = if dark { Rgb::rgb(54, 211, 153) } else { Rgb::rgb(15, 138, 95) }
  let good_fg = if dark { Rgb::rgb(90, 226, 174) } else { Rgb::rgb(11, 99, 60) }
  let good_surface_alpha = if dark { 0.13 } else { 0.1 }
  let wait = if dark { Rgb::rgb(246, 184, 59) } else { Rgb::rgb(184, 116, 0) }
  let wait_fg = if dark { Rgb::rgb(248, 198, 92) } else { Rgb::rgb(124, 71, 0) }
  let wait_surface_alpha = if dark { 0.13 } else { 0.1 }
  let destructive = if dark {
    Rgb::rgb(255, 107, 129)
  } else {
    Rgb::rgb(185, 28, 58)
  }
  let bad_fg = if dark {
    Rgb::rgb(255, 133, 152)
  } else {
    Rgb::rgb(168, 23, 51)
  }
  let destructive_surface_alpha = if dark { 0.14 } else { 0.09 }
  match (scheme, dark) {
    (Moonrockz, false) =>
      {
        background: Rgb::rgb(248, 250, 255),
        foreground: Rgb::rgb(12, 23, 46),
        card: Rgb::rgb(255, 255, 255),
        popover: Rgb::rgb(255, 255, 255),
        sidebar: Rgb::rgb(241, 245, 252),
        muted: Rgb::rgb(238, 243, 251),
        muted_foreground: Rgb::rgb(74, 92, 125),
        accent: Rgb::rgb(231, 238, 249),
        primary: Rgb::rgb(196, 30, 111),
        primary_foreground: Rgb::rgb(255, 255, 255),
        brand: Rgb::rgb(217, 59, 136),
        brand_fg: Rgb::rgb(166, 20, 91),
        brand_surface_alpha: 0.1,
        brand_border_alpha: 0.32,
        cobalt,
        cobalt_surface_alpha,
        good,
        good_fg,
        good_surface_alpha,
        wait,
        wait_fg,
        wait_surface_alpha,
        destructive,
        bad_fg,
        destructive_surface_alpha,
        code_bg: Rgb::rgb(238, 243, 251),
      }
    (Moonrockz, true) =>
      {
        background: Rgb::rgb(8, 17, 36),
        foreground: Rgb::rgb(238, 243, 255),
        card: Rgb::rgb(13, 24, 48),
        popover: Rgb::rgb(19, 33, 64),
        sidebar: Rgb::rgb(11, 21, 43),
        muted: Rgb::rgb(18, 32, 61),
        muted_foreground: Rgb::rgb(147, 165, 203),
        accent: Rgb::rgb(25, 42, 79),
        primary: Rgb::rgb(242, 92, 162),
        primary_foreground: Rgb::rgb(20, 6, 17),
        brand: Rgb::rgb(242, 92, 162),
        brand_fg: Rgb::rgb(255, 134, 190),
        brand_surface_alpha: 0.14,
        brand_border_alpha: 0.42,
        cobalt,
        cobalt_surface_alpha,
        good,
        good_fg,
        good_surface_alpha,
        wait,
        wait_fg,
        wait_surface_alpha,
        destructive,
        bad_fg,
        destructive_surface_alpha,
        code_bg: Rgb::rgb(5, 12, 27),
      }
    (Minimal, false) =>
      {
        background: Rgb::rgb(250, 250, 250),
        foreground: Rgb::rgb(24, 24, 27),
        card: Rgb::rgb(255, 255, 255),
        popover: Rgb::rgb(255, 255, 255),
        sidebar: Rgb::rgb(244, 244, 245),
        muted: Rgb::rgb(244, 244, 245),
        muted_foreground: Rgb::rgb(82, 82, 91),
        accent: Rgb::rgb(236, 236, 239),
        primary: Rgb::rgb(24, 24, 27),
        primary_foreground: Rgb::rgb(255, 255, 255),
        brand: Rgb::rgb(24, 24, 27),
        brand_fg: Rgb::rgb(24, 24, 27),
        brand_surface_alpha: 0.08,
        brand_border_alpha: 0.22,
        cobalt,
        cobalt_surface_alpha,
        good,
        good_fg,
        good_surface_alpha,
        wait,
        wait_fg,
        wait_surface_alpha,
        destructive,
        bad_fg,
        destructive_surface_alpha,
        code_bg: Rgb::rgb(244, 244, 245),
      }
    (Minimal, true) =>
      {
        background: Rgb::rgb(16, 16, 18),
        foreground: Rgb::rgb(244, 244, 245),
        card: Rgb::rgb(22, 22, 25),
        popover: Rgb::rgb(30, 30, 34),
        sidebar: Rgb::rgb(20, 20, 23),
        muted: Rgb::rgb(30, 30, 34),
        muted_foreground: Rgb::rgb(161, 161, 170),
        accent: Rgb::rgb(37, 37, 43),
        primary: Rgb::rgb(228, 228, 231),
        primary_foreground: Rgb::rgb(9, 9, 11),
        brand: Rgb::rgb(228, 228, 231),
        brand_fg: Rgb::rgb(244, 244, 245),
        brand_surface_alpha: 0.1,
        brand_border_alpha: 0.24,
        cobalt,
        cobalt_surface_alpha,
        good,
        good_fg,
        good_surface_alpha,
        wait,
        wait_fg,
        wait_surface_alpha,
        destructive,
        bad_fg,
        destructive_surface_alpha,
        code_bg: Rgb::rgb(11, 11, 13),
      }
    (WarmEditorial, false) =>
      {
        background: Rgb::rgb(245, 242, 234),
        foreground: Rgb::rgb(34, 29, 23),
        card: Rgb::rgb(250, 248, 243),
        popover: Rgb::rgb(250, 248, 243),
        sidebar: Rgb::rgb(239, 234, 224),
        muted: Rgb::rgb(239, 234, 224),
        muted_foreground: Rgb::rgb(92, 83, 72),
        accent: Rgb::rgb(231, 225, 212),
        primary: Rgb::rgb(168, 68, 35),
        primary_foreground: Rgb::rgb(255, 255, 255),
        brand: Rgb::rgb(198, 97, 63),
        brand_fg: Rgb::rgb(148, 55, 24),
        brand_surface_alpha: 0.11,
        brand_border_alpha: 0.34,
        cobalt,
        cobalt_surface_alpha,
        good,
        good_fg,
        good_surface_alpha,
        wait,
        wait_fg,
        wait_surface_alpha,
        destructive,
        bad_fg,
        destructive_surface_alpha,
        code_bg: Rgb::rgb(238, 233, 223),
      }
    (WarmEditorial, true) =>
      {
        background: Rgb::rgb(29, 27, 24),
        foreground: Rgb::rgb(244, 239, 230),
        card: Rgb::rgb(36, 34, 30),
        popover: Rgb::rgb(44, 41, 36),
        sidebar: Rgb::rgb(26, 24, 21),
        muted: Rgb::rgb(44, 41, 36),
        muted_foreground: Rgb::rgb(178, 168, 152),
        accent: Rgb::rgb(52, 48, 42),
        primary: Rgb::rgb(217, 119, 87),
        primary_foreground: Rgb::rgb(28, 14, 7),
        brand: Rgb::rgb(217, 119, 87),
        brand_fg: Rgb::rgb(240, 152, 122),
        brand_surface_alpha: 0.16,
        brand_border_alpha: 0.42,
        cobalt,
        cobalt_surface_alpha,
        good,
        good_fg,
        good_surface_alpha,
        wait,
        wait_fg,
        wait_surface_alpha,
        destructive,
        bad_fg,
        destructive_surface_alpha,
        code_bg: Rgb::rgb(21, 19, 17),
      }
  }
}

///|
fn build_theme_tokens() -> String {
  let l = SchemeTokens::of(Moonrockz, dark=false)
  let d = SchemeTokens::of(Moonrockz, dark=true)
  [
    "color-scheme:light dark",
    "--rui-radius:0.5rem",
    "--rui-background:\{light_dark_rgb(l.background, d.background)}",
    "--rui-foreground:\{light_dark_rgb(l.foreground, d.foreground)}",
    "--rui-card:\{light_dark_rgb(l.card, d.card)}",
    "--rui-card-foreground:var(--rui-foreground)",
    "--rui-popover:\{light_dark_rgb(l.popover, d.popover)}",
    "--rui-popover-foreground:var(--rui-foreground)",
    "--rui-primary:\{light_dark_rgb(l.primary, d.primary)}",
    "--rui-primary-foreground:\{light_dark_rgb(l.primary_foreground, d.primary_foreground)}",
    "--rui-secondary:light-dark(#f1f5fc,#132140)",
    "--rui-secondary-foreground:var(--rui-foreground)",
    "--rui-muted:\{light_dark_rgb(l.muted, d.muted)}",
    "--rui-muted-foreground:\{light_dark_rgb(l.muted_foreground, d.muted_foreground)}",
    "--rui-accent:\{light_dark_rgb(l.accent, d.accent)}",
    "--rui-accent-foreground:var(--rui-foreground)",
    "--rui-destructive:\{light_dark_rgb(l.destructive, d.destructive)}",
    "--rui-destructive-border:var(--rui-destructive)",
    "--rui-destructive-subtle:\{light_dark_rgba(l.destructive, l.destructive_surface_alpha, d.destructive, d.destructive_surface_alpha)}",
    "--rui-destructive-surface:var(--rui-destructive-subtle)",
    "--rui-destructive-ring:light-dark(rgba(185,28,58,0.22),rgba(255,107,129,0.3))",
    "--rui-border:light-dark(rgba(15,31,61,0.12),rgba(143,163,201,0.18))",
    "--rui-input:light-dark(rgba(15,31,61,0.24),rgba(143,163,201,0.32))",
    "--rui-input-background:light-dark(#ffffff,#091327)",
    "--rui-outline-background:transparent",
    "--rui-outline-border:light-dark(rgba(15,31,61,0.18),rgba(143,163,201,0.28))",
    "--rui-outline-hover-bg:var(--rui-accent)",
    "--rui-ghost-hover-bg:var(--rui-accent)",
    "--rui-ring:\{light_dark_rgb(l.brand, d.brand)}",
    "--rui-sidebar:\{light_dark_rgb(l.sidebar, d.sidebar)}",
    "--rui-sidebar-foreground:var(--rui-foreground)",
    "--rui-sidebar-primary:var(--rui-primary)",
    "--rui-sidebar-primary-foreground:var(--rui-primary-foreground)",
    "--rui-sidebar-accent:light-dark(#e7eef9,#172748)",
    "--rui-sidebar-accent-foreground:var(--rui-foreground)",
    "--rui-sidebar-border:var(--rui-border)",
    "--rui-sidebar-ring:var(--rui-ring)",
    "--rui-sidebar-width:17rem",
    "--rui-sidebar-width-mobile:20rem",
    "--rui-font-sans:system-ui,-apple-system,\"Segoe UI\",Roboto,\"Noto Sans\",sans-serif",
    "--mc-mono:ui-monospace,\"Cascadia Code\",\"SF Mono\",Menlo,Consolas,monospace",
    "--mc-brand:\{light_dark_rgb(l.brand, d.brand)}",
    "--mc-brand-fg:\{light_dark_rgb(l.brand_fg, d.brand_fg)}",
    "--mc-brand-surface:\{light_dark_rgba(l.brand, l.brand_surface_alpha, d.brand, d.brand_surface_alpha)}",
    "--mc-brand-border:\{light_dark_rgba(l.brand, l.brand_border_alpha, d.brand, d.brand_border_alpha)}",
    "--mc-cobalt:\{light_dark_rgb(l.cobalt, d.cobalt)}",
    "--mc-cobalt-surface:\{light_dark_rgba(l.cobalt, l.cobalt_surface_alpha, d.cobalt, d.cobalt_surface_alpha)}",
    "--mc-good:\{light_dark_rgb(l.good, d.good)}",
    "--mc-good-fg:\{light_dark_rgb(l.good_fg, d.good_fg)}",
    "--mc-good-surface:\{light_dark_rgba(l.good, l.good_surface_alpha, d.good, d.good_surface_alpha)}",
    "--mc-wait:\{light_dark_rgb(l.wait, d.wait)}",
    "--mc-wait-fg:\{light_dark_rgb(l.wait_fg, d.wait_fg)}",
    "--mc-wait-surface:\{light_dark_rgba(l.wait, l.wait_surface_alpha, d.wait, d.wait_surface_alpha)}",
    "--mc-bad-fg:\{light_dark_rgb(l.bad_fg, d.bad_fg)}",
    "--mc-code-bg:\{light_dark_rgb(l.code_bg, d.code_bg)}",
    "--vscode-editor-foreground:var(--rui-foreground)",
    "--vscode-editor-background:var(--mc-code-bg)",
    "--vscode-editorLineNumber-foreground:var(--rui-muted-foreground)",
    "--vscode-token-keyword:light-dark(#af00db,#c586c0)",
    "--vscode-token-variable:light-dark(#001080,#9cdcfe)",
    "--vscode-token-string:light-dark(#a31515,#ce9178)",
    "--vscode-token-number:light-dark(#098658,#b5cea8)",
    "--vscode-token-comment:light-dark(#008000,#6a9955)",
    "--vscode-token-punctuation:light-dark(#3b4252,#d4d4d4)",
    "--vscode-token-type:light-dark(#267f99,#4ec9b0)",
    "--vscode-token-function:light-dark(#795e26,#dcdcaa)",
    "--vscode-token-string-escape:light-dark(#ee0000,#d7ba7d)",
    "--vscode-token-regexp:light-dark(#811f3f,#d16969)",
    "--vscode-token-invalid:light-dark(#cd3131,#f44747)",
  ].join(";")
}

///|
fn build_scheme_override_css(
  scheme : ColorScheme,
  border_css : String,
) -> String {
  let l = SchemeTokens::of(scheme, dark=false)
  let d = SchemeTokens::of(scheme, dark=true)
  let body = [
    "--rui-background:\{light_dark_rgb(l.background, d.background)}",
    "--rui-foreground:\{light_dark_rgb(l.foreground, d.foreground)}",
    "--rui-card:\{light_dark_rgb(l.card, d.card)}",
    "--rui-popover:\{light_dark_rgb(l.popover, d.popover)}",
    "--rui-sidebar:\{light_dark_rgb(l.sidebar, d.sidebar)}",
    "--rui-muted:\{light_dark_rgb(l.muted, d.muted)}",
    "--rui-muted-foreground:\{light_dark_rgb(l.muted_foreground, d.muted_foreground)}",
    "--rui-accent:\{light_dark_rgb(l.accent, d.accent)}",
    "--rui-border:\{border_css}",
    "--rui-primary:\{light_dark_rgb(l.primary, d.primary)}",
    "--rui-primary-foreground:\{light_dark_rgb(l.primary_foreground, d.primary_foreground)}",
    "--rui-ring:var(--rui-primary)",
    "--mc-brand:\{light_dark_rgb(l.brand, d.brand)}",
    "--mc-brand-fg:\{light_dark_rgb(l.brand_fg, d.brand_fg)}",
    "--mc-brand-surface:\{light_dark_rgba(l.brand, l.brand_surface_alpha, d.brand, d.brand_surface_alpha)}",
    "--mc-brand-border:\{light_dark_rgba(l.brand, l.brand_border_alpha, d.brand, d.brand_border_alpha)}",
    "--mc-code-bg:\{light_dark_rgb(l.code_bg, d.code_bg)}",
  ].join(";")
  ".mc-shell-root[data-scheme=\"\{scheme.stored()}\"] { \{body}; }"
}