import type { BrandThemeTokens } from './app-config.js'
import {
  DEFAULT_ACCENT_PRESET_ID,
  DEFAULT_DARK_BRAND_THEME,
  DEFAULT_LIGHT_BRAND_THEME,
  DESIGN_ACCENT_PRESETS,
  accentActionForegroundForColors,
  accentForegroundForColor,
  accentTextForBackground,
  applyDesignAccentTokens,
  isDesignAccentPresetId,
  type DesignAccentPresetId,
} from './design-tokens.js'

export type ResolvedColorScheme = 'dark' | 'light'

export type ThemeTokens = BrandThemeTokens & {
  accent2: string
  accentText: string
  accentActionForeground: string
  accentSoft: string
  accentLine: string
  borderStrong: string
}
type ThemeTokenSeed = BrandThemeTokens

type ThemeDefinition = {
  label: string
  description: string
  swatches: string[]
  dark: ThemeTokens
  light: ThemeTokens
}

type ThemeDefinitionSeed = Omit<ThemeDefinition, 'dark' | 'light'> & {
  dark: ThemeTokenSeed
  light: ThemeTokenSeed
}

export const UI_ACCENT_PRESETS = DESIGN_ACCENT_PRESETS
export const DEFAULT_UI_ACCENT_PRESET_ID = DEFAULT_ACCENT_PRESET_ID
export type UiAccentPresetId = DesignAccentPresetId

export function isUiAccentPresetId(value: string | null | undefined): value is UiAccentPresetId {
  return isDesignAccentPresetId(value)
}

function accentSoftToken() {
  return 'color-mix(in srgb,var(--accent) 15%,transparent)'
}

function accentLineToken() {
  return 'color-mix(in srgb,var(--accent) 38%,transparent)'
}

const GRADIENT_NONE = 'none'

const DARK_SHADOW_SOFT = '0 1px 1px rgba(0, 0, 0, 0.28), 0 8px 20px rgba(0, 0, 0, 0.16)'
const DARK_SHADOW_SOFT_LG = '0 2px 6px rgba(0, 0, 0, 0.30), 0 18px 44px rgba(0, 0, 0, 0.24)'
const DARK_SHADOW_FLAT = '0 1px 1px rgba(0, 0, 0, 0.28), 0 3px 8px rgba(0, 0, 0, 0.18)'
const DARK_SHADOW_FLAT_LG = '0 2px 4px rgba(0, 0, 0, 0.32), 0 10px 24px rgba(0, 0, 0, 0.22)'

const LIGHT_SHADOW_SOFT = '0 1px 1px rgba(20, 24, 40, 0.08), 0 6px 18px rgba(20, 24, 40, 0.06)'
const LIGHT_SHADOW_SOFT_LG = '0 2px 5px rgba(20, 24, 40, 0.09), 0 16px 38px rgba(20, 24, 40, 0.08)'

function alpha(value: number) {
  return value.toFixed(3).replace(/0+$/, '').replace(/\.$/, '')
}

function parseHexRgb(value: string) {
  const match = value.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i)
  if (!match) return null
  const hex = match[1]
  const pairs = hex.length === 3
    ? hex.split('').map((part) => part + part)
    : hex.match(/[0-9a-f]{2}/gi)
  if (!pairs || pairs.length !== 3) return null
  return pairs.map((part) => Number.parseInt(part, 16)) as [number, number, number]
}

function strengthenRgba(value: string, minimum: number) {
  return value.replace(
    /rgba\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)\s*\)/g,
    (_match, red: string, green: string, blue: string, rawAlpha: string) => {
      const next = Math.max(Number.parseFloat(rawAlpha), minimum)
      return `rgba(${red}, ${green}, ${blue}, ${alpha(next)})`
    },
  )
}

function alphaColor(value: string, minimum: number) {
  const rgba = value.match(/^rgba\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)\s*\)$/i)
  if (rgba) {
    const [, red, green, blue, rawAlpha] = rgba
    const current = Number.parseFloat(rawAlpha || '1')
    const next = current >= minimum ? Math.min(1, current + 0.08) : minimum
    return `rgba(${red}, ${green}, ${blue}, ${alpha(next)})`
  }

  const rgb = value.match(/^rgb\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)\s*\)$/i)
  if (rgb) {
    const [, red, green, blue] = rgb
    return `rgba(${red}, ${green}, ${blue}, ${alpha(minimum)})`
  }

  const hex = parseHexRgb(value)
  if (hex) {
    const [red, green, blue] = hex
    return `rgba(${red}, ${green}, ${blue}, ${alpha(minimum)})`
  }

  return undefined
}

function deriveBorderStrong(tokens: ThemeTokenSeed, scheme: ResolvedColorScheme) {
  if (tokens.borderStrong) return tokens.borderStrong
  const targetAlpha = scheme === 'dark' ? 0.18 : 0.18
  const alphaBorder = alphaColor(tokens.border, targetAlpha)
  if (alphaBorder) return alphaBorder
  return `color-mix(in srgb, ${tokens.border} 72%, ${tokens.text} 28%)`
}

function softenGradient(value: string) {
  if (value === GRADIENT_NONE) return value
  return value.replace(
    /rgba\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)\s*\)/g,
    (_match, red: string, green: string, blue: string, rawAlpha: string) => {
      const current = Number.parseFloat(rawAlpha)
      const next = Number.isFinite(current) ? Math.max(0.025, Math.min(current * 0.625, 0.08)) : current
      return `rgba(${red}, ${green}, ${blue}, ${alpha(next)})`
    },
  )
}

export function refineThemeTokens(tokens: ThemeTokenSeed, scheme: ResolvedColorScheme): ThemeTokens {
  const dark = scheme === 'dark'
  return {
    ...tokens,
    accent2: tokens.accent2 || tokens.accentHover,
    accentText: accentTextForBackground(tokens.accent, tokens.accent2 || tokens.accentHover, tokens.base),
    accentActionForeground: accentActionForegroundForColors(tokens.accent, tokens.accent2 || tokens.accentHover),
    accentSoft: tokens.accentSoft || accentSoftToken(),
    accentLine: tokens.accentLine || accentLineToken(),
    accentForeground: tokens.accentForeground || accentForegroundForColor(tokens.accent),
    surface: strengthenRgba(tokens.surface, dark ? 0.055 : 0.04),
    surfaceHover: strengthenRgba(tokens.surfaceHover, dark ? 0.10 : 0.08),
    surfaceActive: strengthenRgba(tokens.surfaceActive, dark ? 0.17 : 0.13),
    border: strengthenRgba(tokens.border, dark ? 0.12 : 0.12),
    borderSubtle: strengthenRgba(tokens.borderSubtle, dark ? 0.04 : 0.055),
    borderStrong: deriveBorderStrong(tokens, scheme),
    shadowCard: tokens.shadowCard,
    shadowElevated: tokens.shadowElevated,
    bgImage: softenGradient(tokens.bgImage),
  }
}

export function applyThemeAccent(tokens: ThemeTokens, accentId: UiAccentPresetId = DEFAULT_UI_ACCENT_PRESET_ID): ThemeTokens {
  const next = applyDesignAccentTokens(tokens, accentId)
  return {
    ...next,
    accentText: accentTextForBackground(next.accent, next.accent2, next.base),
    accentActionForeground: accentActionForegroundForColors(next.accent, next.accent2),
    borderStrong: tokens.borderStrong,
  }
}

function refineThemeDefinition(theme: ThemeDefinitionSeed): ThemeDefinition {
  return {
    ...theme,
    dark: refineThemeTokens(theme.dark, 'dark'),
    light: refineThemeTokens(theme.light, 'light'),
  }
}

const UI_THEME_PRESET_SEEDS = {
  studio: {
    label: 'Studio',
    description: 'Warm graphite coworking studio — plum focus, cream typography, and calm operational contrast.',
    swatches: ['#181516', '#cfa0e6', '#f0e9e1', '#8bc9d8'],
    dark: {
      base: '#181516',
      surface: 'rgba(239, 220, 206, 0.06)',
      surfaceHover: 'rgba(239, 220, 206, 0.11)',
      surfaceActive: 'rgba(207, 160, 230, 0.18)',
      elevated: '#242021',
      border: 'rgba(239, 220, 206, 0.13)',
      borderSubtle: 'rgba(239, 220, 206, 0.06)',
      borderStrong: 'rgba(239, 220, 206, 0.22)',
      text: '#f0e9e1',
      textSecondary: '#d2c3b6',
      textMuted: '#a8988e',
      accent: '#cfa0e6',
      accentHover: '#e0b7f0',
      green: '#82d3a2',
      amber: '#f0b86e',
      red: '#ff9bb4',
      info: '#8bc9d8',
      accentForeground: '#1b121d',
      shadowCard: '0 1px 1px rgba(0, 0, 0, 0.30), 0 12px 28px rgba(0, 0, 0, 0.20)',
      shadowElevated: '0 2px 8px rgba(0, 0, 0, 0.34), 0 24px 60px rgba(0, 0, 0, 0.28)',
      bgImage: 'radial-gradient(120% 80% at 50% -10%, rgba(207, 160, 230, 0.112), transparent 55%), radial-gradient(80% 64% at 92% 12%, rgba(139, 201, 216, 0.072), transparent 62%)',
    },
    light: {
      base: '#f7f1ea',
      surface: 'rgba(129, 82, 154, 0.05)',
      surfaceHover: 'rgba(129, 82, 154, 0.09)',
      surfaceActive: 'rgba(129, 82, 154, 0.14)',
      elevated: '#fffaf4',
      border: 'rgba(70, 52, 44, 0.14)',
      borderSubtle: 'rgba(70, 52, 44, 0.08)',
      borderStrong: 'rgba(70, 52, 44, 0.24)',
      text: '#241d1f',
      textSecondary: '#5a4c48',
      textMuted: '#746760',
      accent: '#81529a',
      accentHover: '#6d4085',
      green: '#186a42',
      amber: '#8b4e10',
      red: '#a22f4d',
      info: '#306f7b',
      accentForeground: '#fffaf4',
      shadowCard: '0 1px 1px rgba(54, 41, 33, 0.08), 0 10px 24px rgba(54, 41, 33, 0.08)',
      shadowElevated: '0 2px 6px rgba(54, 41, 33, 0.10), 0 20px 48px rgba(54, 41, 33, 0.12)',
      bgImage: 'radial-gradient(120% 80% at 50% -10%, rgba(129, 82, 154, 0.128), transparent 55%), radial-gradient(80% 64% at 92% 12%, rgba(48, 111, 123, 0.072), transparent 62%)',
    },
  },
  mercury: {
    label: 'Mercury',
    description: 'Graphite Studio default — cool black surfaces, crisp lines, and the Azure signature accent.',
    swatches: ['#0c0d0f', '#141619', '#2f6bf0', '#eceef1'],
    dark: {
      ...DEFAULT_DARK_BRAND_THEME,
    },
    light: {
      ...DEFAULT_LIGHT_BRAND_THEME,
    },
  },
  tokyostorm: {
    label: 'Tokyo Storm',
    description: 'Lifted Tokyo Night — electric indigo on soft storm slate.',
    swatches: ['#24283b', '#7aa2f7', '#bb9af7', '#7dcfff'],
    dark: {
      base: '#24283b',
      surface: 'rgba(122, 162, 247, 0.05)',
      surfaceHover: 'rgba(122, 162, 247, 0.1)',
      surfaceActive: 'rgba(122, 162, 247, 0.16)',
      elevated: '#2f334d',
      border: 'rgba(122, 162, 247, 0.075)',
      borderSubtle: 'rgba(122, 162, 247, 0.038)',
      text: '#c8d3f5',
      textSecondary: '#abb3d7',
      textMuted: '#848db9',
      accent: '#7aa2f7',
      accentHover: '#9ab8ff',
      green: '#c3e88d',
      amber: '#ffc777',
      red: '#ff757f',
      info: '#7dcfff',
      accentForeground: '#111321',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(80% 60% at 15% 0%, rgba(122, 162, 247, 0.1), transparent 55%), radial-gradient(60% 50% at 100% 100%, rgba(187, 154, 247, 0.07), transparent 55%)',
    },
    light: {
      base: '#e6e7ed',
      surface: 'rgba(52, 84, 138, 0.03)',
      surfaceHover: 'rgba(52, 84, 138, 0.07)',
      surfaceActive: 'rgba(52, 84, 138, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(52, 84, 138, 0.07)',
      borderSubtle: 'rgba(52, 84, 138, 0.04)',
      text: '#2e3141',
      textSecondary: '#3d4c65',
      textMuted: '#56659d',
      accent: '#34548a',
      accentHover: '#2a4472',
      green: '#587539',
      amber: '#8f5e15',
      red: '#8c4351',
      info: '#0f4b6e',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(80% 60% at 15% 0%, rgba(52, 84, 138, 0.06), transparent 55%)',
    },
  },
  frappe: {
    label: 'Catppuccin Frappé',
    description: 'Pillowy pastel dusk — softer than Mocha, cozy lavender and peach.',
    swatches: ['#303446', '#ca9ee6', '#8caaee', '#f4b8e4'],
    dark: {
      base: '#303446',
      surface: 'rgba(202, 158, 230, 0.05)',
      surfaceHover: 'rgba(202, 158, 230, 0.1)',
      surfaceActive: 'rgba(202, 158, 230, 0.16)',
      elevated: '#414559',
      border: 'rgba(202, 158, 230, 0.07)',
      borderSubtle: 'rgba(202, 158, 230, 0.035)',
      text: '#c6d0f5',
      textSecondary: '#b9c3e4',
      textMuted: '#959cb4',
      accent: '#ca9ee6',
      accentHover: '#d7b0ef',
      green: '#a6d189',
      amber: '#e5c890',
      red: '#e78284',
      info: '#8caaee',
      accentForeground: '#232634',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 100% 0%, rgba(244, 184, 228, 0.06), transparent 55%), radial-gradient(90% 70% at 0% 100%, rgba(140, 170, 238, 0.06), transparent 55%)',
    },
    light: {
      base: '#eff1f5',
      surface: 'rgba(136, 57, 239, 0.03)',
      surfaceHover: 'rgba(136, 57, 239, 0.07)',
      surfaceActive: 'rgba(136, 57, 239, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(136, 57, 239, 0.07)',
      borderSubtle: 'rgba(136, 57, 239, 0.04)',
      text: '#4c4f69',
      textSecondary: '#4d5064',
      textMuted: '#6b6d7b',
      accent: '#8839ef',
      accentHover: '#7328d4',
      green: '#40a02b',
      amber: '#df8e1d',
      red: '#d20f39',
      info: '#1e66f5',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 100% 0%, rgba(234, 118, 203, 0.05), transparent 55%)',
    },
  },
  rosepine: {
    label: 'Rosé Pine Moon',
    description: 'Muted rose and iris dusk — writerly, aesthetic, quiet.',
    swatches: ['#232136', '#c4a7e7', '#ea9a97', '#9ccfd8'],
    dark: {
      base: '#232136',
      surface: 'rgba(196, 167, 231, 0.05)',
      surfaceHover: 'rgba(196, 167, 231, 0.09)',
      surfaceActive: 'rgba(196, 167, 231, 0.15)',
      elevated: '#2a283e',
      border: 'rgba(196, 167, 231, 0.07)',
      borderSubtle: 'rgba(196, 167, 231, 0.035)',
      text: '#e0def4',
      textSecondary: '#c2bed4',
      textMuted: '#908caa',
      accent: '#c4a7e7',
      accentHover: '#d4bdef',
      green: '#9ccfd8',
      amber: '#f6c177',
      red: '#eb6f92',
      info: '#3e8fb0',
      accentForeground: '#16141f',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 0% 100%, rgba(235, 111, 146, 0.06), transparent 55%), radial-gradient(80% 60% at 100% 0%, rgba(196, 167, 231, 0.07), transparent 55%)',
    },
    light: {
      base: '#faf4ed',
      surface: 'rgba(144, 122, 169, 0.04)',
      surfaceHover: 'rgba(144, 122, 169, 0.08)',
      surfaceActive: 'rgba(144, 122, 169, 0.13)',
      elevated: '#fffaf3',
      border: 'rgba(144, 122, 169, 0.08)',
      borderSubtle: 'rgba(144, 122, 169, 0.04)',
      text: '#575279',
      textSecondary: '#545166',
      textMuted: '#726e7c',
      accent: '#7b6991',
      accentHover: '#7a6491',
      green: '#56949f',
      amber: '#ea9d34',
      red: '#b4637a',
      info: '#286983',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 0% 100%, rgba(180, 99, 122, 0.04), transparent 55%)',
    },
  },
  nord: {
    label: 'Nord',
    description: 'Crisp Arctic frost — flat, composed, clarity first.',
    swatches: ['#2e3440', '#88c0d0', '#81a1c1', '#a3be8c'],
    dark: {
      base: '#2e3440',
      surface: 'rgba(136, 192, 208, 0.045)',
      surfaceHover: 'rgba(136, 192, 208, 0.09)',
      surfaceActive: 'rgba(136, 192, 208, 0.15)',
      elevated: '#3b4252',
      border: 'rgba(136, 192, 208, 0.075)',
      borderSubtle: 'rgba(136, 192, 208, 0.04)',
      text: '#e5e9f0',
      textSecondary: '#d8dee9',
      textMuted: '#939cab',
      accent: '#88c0d0',
      accentHover: '#8fbcbb',
      green: '#a3be8c',
      amber: '#ebcb8b',
      red: '#bf616a',
      info: '#81a1c1',
      accentForeground: '#1b1f27',
      shadowCard: DARK_SHADOW_FLAT,
      shadowElevated: DARK_SHADOW_FLAT_LG,
      bgImage: GRADIENT_NONE,
    },
    light: {
      base: '#eceff4',
      surface: 'rgba(94, 129, 172, 0.03)',
      surfaceHover: 'rgba(94, 129, 172, 0.07)',
      surfaceActive: 'rgba(94, 129, 172, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(94, 129, 172, 0.08)',
      borderSubtle: 'rgba(94, 129, 172, 0.045)',
      text: '#2e3440',
      textSecondary: '#3b4252',
      textMuted: '#5c6e88',
      accent: '#516f93',
      accentHover: '#4a6a92',
      green: '#5e815b',
      amber: '#d08770',
      red: '#bf616a',
      info: '#5e81ac',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: GRADIENT_NONE,
    },
  },
  everforest: {
    label: 'Everforest',
    description: 'Mossy forest floor — sage greens and warm bark, low stimulus.',
    swatches: ['#2f383e', '#a7c080', '#7fbbb3', '#dbbc7f'],
    dark: {
      base: '#2f383e',
      surface: 'rgba(167, 192, 128, 0.045)',
      surfaceHover: 'rgba(167, 192, 128, 0.09)',
      surfaceActive: 'rgba(167, 192, 128, 0.15)',
      elevated: '#3a454b',
      border: 'rgba(167, 192, 128, 0.07)',
      borderSubtle: 'rgba(167, 192, 128, 0.035)',
      text: '#d3c6aa',
      textSecondary: '#c2c9bd',
      textMuted: '#96a29a',
      accent: '#a7c080',
      accentHover: '#b9d094',
      green: '#83c092',
      amber: '#dbbc7f',
      red: '#e67e80',
      info: '#7fbbb3',
      accentForeground: '#1e2326',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 50% 100%, rgba(167, 192, 128, 0.05), transparent 55%)',
    },
    light: {
      base: '#fdf6e3',
      surface: 'rgba(141, 161, 1, 0.04)',
      surfaceHover: 'rgba(141, 161, 1, 0.08)',
      surfaceActive: 'rgba(141, 161, 1, 0.12)',
      elevated: '#fffbef',
      border: 'rgba(141, 161, 1, 0.08)',
      borderSubtle: 'rgba(141, 161, 1, 0.04)',
      text: '#3a4338',
      textSecondary: '#4a565c',
      textMuted: '#6b7369',
      accent: '#6a7801',
      accentHover: '#768700',
      green: '#8da101',
      amber: '#df9a02',
      red: '#f85552',
      info: '#3a94c5',
      accentForeground: '#fffdf6',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 50% 100%, rgba(141, 161, 1, 0.04), transparent 55%)',
    },
  },
  gruvbox: {
    label: 'Gruvbox Soft',
    description: 'Warm retro terminal — amber on toasted bark, flat and friendly.',
    swatches: ['#32302f', '#fabd2f', '#b8bb26', '#fe8019'],
    dark: {
      base: '#32302f',
      surface: 'rgba(250, 189, 47, 0.045)',
      surfaceHover: 'rgba(250, 189, 47, 0.09)',
      surfaceActive: 'rgba(250, 189, 47, 0.15)',
      elevated: '#3c3836',
      border: 'rgba(250, 189, 47, 0.07)',
      borderSubtle: 'rgba(250, 189, 47, 0.035)',
      text: '#ebdbb2',
      textSecondary: '#d5c4a1',
      textMuted: '#a89984',
      accent: '#fabd2f',
      accentHover: '#fcd757',
      green: '#b8bb26',
      amber: '#fe8019',
      red: '#fb4934',
      info: '#83a598',
      accentForeground: '#1d2021',
      shadowCard: DARK_SHADOW_FLAT,
      shadowElevated: DARK_SHADOW_FLAT_LG,
      bgImage: GRADIENT_NONE,
    },
    light: {
      base: '#fbf1c7',
      surface: 'rgba(181, 118, 20, 0.04)',
      surfaceHover: 'rgba(181, 118, 20, 0.08)',
      surfaceActive: 'rgba(181, 118, 20, 0.12)',
      elevated: '#fffaeb',
      border: 'rgba(181, 118, 20, 0.08)',
      borderSubtle: 'rgba(181, 118, 20, 0.04)',
      text: '#3c3836',
      textSecondary: '#504945',
      textMuted: '#786c60',
      accent: '#976311',
      accentHover: '#9e650f',
      green: '#79740e',
      amber: '#af3a03',
      red: '#9d0006',
      info: '#076678',
      accentForeground: '#fffdf6',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: GRADIENT_NONE,
    },
  },
  synthwave: {
    label: 'Synthwave',
    description: 'Retrowave neon — magenta glow on deep violet crt dusk.',
    swatches: ['#1e1033', '#ff7edb', '#36f9f6', '#fede5d'],
    dark: {
      base: '#1e1033',
      surface: 'rgba(255, 126, 219, 0.045)',
      surfaceHover: 'rgba(255, 126, 219, 0.09)',
      surfaceActive: 'rgba(255, 126, 219, 0.15)',
      elevated: '#2a1a4a',
      border: 'rgba(255, 126, 219, 0.075)',
      borderSubtle: 'rgba(255, 126, 219, 0.038)',
      text: '#f5edff',
      textSecondary: '#c9b8e4',
      textMuted: '#8c7aa8',
      accent: '#ff7edb',
      accentHover: '#ff9ae4',
      green: '#72f1b8',
      amber: '#fede5d',
      red: '#fe4450',
      info: '#36f9f6',
      accentForeground: '#1a0a2e',
      shadowCard: '0 0 28px rgba(255, 126, 219, 0.1), 0 8px 24px rgba(10, 2, 30, 0.5)',
      shadowElevated: '0 0 44px rgba(255, 126, 219, 0.16), 0 18px 48px rgba(10, 2, 30, 0.6)',
      bgImage: 'linear-gradient(180deg, rgba(54, 26, 98, 0.55) 0%, rgba(30, 16, 51, 0) 45%), radial-gradient(60% 50% at 100% 100%, rgba(54, 249, 246, 0.14), transparent 55%), radial-gradient(60% 50% at 0% 0%, rgba(255, 126, 219, 0.12), transparent 55%)',
    },
    light: {
      base: '#faf4ff',
      surface: 'rgba(199, 50, 155, 0.035)',
      surfaceHover: 'rgba(199, 50, 155, 0.07)',
      surfaceActive: 'rgba(199, 50, 155, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(199, 50, 155, 0.08)',
      borderSubtle: 'rgba(199, 50, 155, 0.04)',
      text: '#2b1b3d',
      textSecondary: '#5f4478',
      textMuted: '#7d6a8d',
      accent: '#c5329a',
      accentHover: '#a8267f',
      green: '#04876a',
      amber: '#b07b00',
      red: '#c81d35',
      info: '#0e7490',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(80% 60% at 100% 0%, rgba(199, 50, 155, 0.05), transparent 55%), radial-gradient(80% 60% at 0% 100%, rgba(14, 116, 144, 0.04), transparent 55%)',
    },
  },

  // ─── Eight new themes ───────────────────────────────────────────

  dracula: {
    label: 'Dracula',
    description: 'The classic — violet, pink, and cyan on deep graphite. Vibrant but composed.',
    swatches: ['#282a36', '#bd93f9', '#ff79c6', '#8be9fd'],
    dark: {
      base: '#282a36',
      surface: 'rgba(189, 147, 249, 0.05)',
      surfaceHover: 'rgba(189, 147, 249, 0.1)',
      surfaceActive: 'rgba(189, 147, 249, 0.16)',
      elevated: '#313442',
      border: 'rgba(189, 147, 249, 0.07)',
      borderSubtle: 'rgba(189, 147, 249, 0.035)',
      text: '#f8f8f2',
      textSecondary: '#d8d8d2',
      textMuted: '#9090a4',
      accent: '#bd93f9',
      accentHover: '#cda5fb',
      green: '#50fa7b',
      amber: '#ffb86c',
      red: '#ff5555',
      info: '#8be9fd',
      accentForeground: '#1a1b22',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(90% 60% at 100% 0%, rgba(255, 121, 198, 0.07), transparent 55%), radial-gradient(80% 60% at 0% 100%, rgba(139, 233, 253, 0.06), transparent 55%)',
    },
    light: {
      base: '#f8f8f2',
      surface: 'rgba(99, 42, 129, 0.03)',
      surfaceHover: 'rgba(99, 42, 129, 0.07)',
      surfaceActive: 'rgba(99, 42, 129, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(99, 42, 129, 0.08)',
      borderSubtle: 'rgba(99, 42, 129, 0.04)',
      text: '#282a36',
      textSecondary: '#44475a',
      textMuted: '#6170a2',
      accent: '#7c3aed',
      accentHover: '#6929d0',
      green: '#1f9268',
      amber: '#c97f1a',
      red: '#cd1d57',
      info: '#0e7490',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(90% 60% at 100% 0%, rgba(205, 29, 87, 0.04), transparent 55%)',
    },
  },
  ayu: {
    label: 'Ayu Mirage',
    description: 'Warm mid-dark — saffron, teal, and ember over dusty graphite.',
    swatches: ['#1f2430', '#ffcc66', '#5ccfe6', '#ff8f40'],
    dark: {
      base: '#1f2430',
      surface: 'rgba(255, 204, 102, 0.04)',
      surfaceHover: 'rgba(255, 204, 102, 0.085)',
      surfaceActive: 'rgba(255, 204, 102, 0.14)',
      elevated: '#262d3d',
      border: 'rgba(255, 204, 102, 0.07)',
      borderSubtle: 'rgba(255, 204, 102, 0.035)',
      text: '#cbccc6',
      textSecondary: '#a8afc4',
      textMuted: '#828b9a',
      accent: '#ffcc66',
      accentHover: '#ffd988',
      green: '#bae67e',
      amber: '#ff8f40',
      red: '#ff6666',
      info: '#5ccfe6',
      accentForeground: '#15181f',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(90% 60% at 100% 0%, rgba(255, 143, 64, 0.07), transparent 55%), radial-gradient(80% 60% at 0% 100%, rgba(92, 207, 230, 0.05), transparent 55%)',
    },
    light: {
      base: '#fafafa',
      surface: 'rgba(245, 122, 0, 0.03)',
      surfaceHover: 'rgba(245, 122, 0, 0.07)',
      surfaceActive: 'rgba(245, 122, 0, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(245, 122, 0, 0.08)',
      borderSubtle: 'rgba(245, 122, 0, 0.04)',
      text: '#5c6773',
      textSecondary: '#4c5763',
      textMuted: '#707479',
      accent: '#b35c00',
      accentHover: '#d66800',
      green: '#86b300',
      amber: '#f29718',
      red: '#f07171',
      info: '#399ee6',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: GRADIENT_NONE,
    },
  },
  kanagawa: {
    label: 'Kanagawa Wave',
    description: 'Japanese ukiyo-e — crystal blue and sakura on inked paper.',
    swatches: ['#1f1f28', '#7e9cd8', '#957fb8', '#d27e99'],
    dark: {
      base: '#1f1f28',
      surface: 'rgba(126, 156, 216, 0.045)',
      surfaceHover: 'rgba(126, 156, 216, 0.09)',
      surfaceActive: 'rgba(126, 156, 216, 0.15)',
      elevated: '#2a2a37',
      border: 'rgba(126, 156, 216, 0.07)',
      borderSubtle: 'rgba(126, 156, 216, 0.035)',
      text: '#dcd7ba',
      textSecondary: '#c8c093',
      textMuted: '#878780',
      accent: '#7e9cd8',
      accentHover: '#9cb6e2',
      green: '#98bb6c',
      amber: '#e6c384',
      red: '#e46876',
      info: '#7fb4ca',
      accentForeground: '#16161d',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(90% 60% at 0% 0%, rgba(126, 156, 216, 0.08), transparent 55%), radial-gradient(80% 60% at 100% 100%, rgba(210, 126, 153, 0.06), transparent 55%)',
    },
    light: {
      base: '#f2ecbc',
      surface: 'rgba(79, 94, 127, 0.04)',
      surfaceHover: 'rgba(79, 94, 127, 0.08)',
      surfaceActive: 'rgba(79, 94, 127, 0.12)',
      elevated: '#fffbe7',
      border: 'rgba(79, 94, 127, 0.08)',
      borderSubtle: 'rgba(79, 94, 127, 0.04)',
      text: '#44524a',
      textSecondary: '#4c4c5b',
      textMuted: '#6b6a64',
      accent: '#4f5e7f',
      accentHover: '#3d4a66',
      green: '#6f894e',
      amber: '#b9761a',
      red: '#cc504b',
      info: '#5d7c9e',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: GRADIENT_NONE,
    },
  },
  horizon: {
    label: 'Horizon',
    description: 'Warm sunset dusk — coral, lavender, and mint on velvet slate.',
    swatches: ['#1c1e26', '#e95678', '#b877db', '#59e1e3'],
    dark: {
      base: '#1c1e26',
      surface: 'rgba(233, 86, 120, 0.04)',
      surfaceHover: 'rgba(233, 86, 120, 0.085)',
      surfaceActive: 'rgba(233, 86, 120, 0.14)',
      elevated: '#232530',
      border: 'rgba(233, 86, 120, 0.07)',
      borderSubtle: 'rgba(233, 86, 120, 0.035)',
      text: '#cbced0',
      textSecondary: '#a8afc4',
      textMuted: '#8083a2',
      accent: '#e95678',
      accentHover: '#ec6e8a',
      green: '#09f7a0',
      amber: '#fab795',
      red: '#f43e5c',
      info: '#59e1e3',
      accentForeground: '#13141a',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 100% 0%, rgba(233, 86, 120, 0.08), transparent 55%), radial-gradient(80% 60% at 0% 100%, rgba(184, 119, 219, 0.06), transparent 55%)',
    },
    light: {
      base: '#fdf0ed',
      surface: 'rgba(218, 16, 60, 0.03)',
      surfaceHover: 'rgba(218, 16, 60, 0.07)',
      surfaceActive: 'rgba(218, 16, 60, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(218, 16, 60, 0.08)',
      borderSubtle: 'rgba(218, 16, 60, 0.04)',
      text: '#1d1139',
      textSecondary: '#503f65',
      textMuted: '#766b81',
      accent: '#da103c',
      accentHover: '#bd0a32',
      green: '#09875f',
      amber: '#c97f1a',
      red: '#da103c',
      info: '#0095a8',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 100% 0%, rgba(218, 16, 60, 0.04), transparent 55%)',
    },
  },
  oxocarbon: {
    label: 'Oxocarbon',
    description: 'IBM Carbon minimalism — deep graphite with electric cyan accents.',
    swatches: ['#161616', '#33b1ff', '#be95ff', '#42be65'],
    dark: {
      base: '#1a1a1a',
      surface: 'rgba(51, 177, 255, 0.04)',
      surfaceHover: 'rgba(51, 177, 255, 0.085)',
      surfaceActive: 'rgba(51, 177, 255, 0.14)',
      elevated: '#242424',
      border: 'rgba(51, 177, 255, 0.07)',
      borderSubtle: 'rgba(51, 177, 255, 0.035)',
      text: '#f2f4f8',
      textSecondary: '#c6c6c6',
      textMuted: '#828282',
      accent: '#33b1ff',
      accentHover: '#58c3ff',
      green: '#42be65',
      amber: '#ee5396',
      red: '#ff7eb6',
      info: '#82cfff',
      accentForeground: '#0b0b0b',
      shadowCard: DARK_SHADOW_FLAT,
      shadowElevated: DARK_SHADOW_FLAT_LG,
      bgImage: GRADIENT_NONE,
    },
    light: {
      base: '#f2f4f8',
      surface: 'rgba(0, 120, 212, 0.03)',
      surfaceHover: 'rgba(0, 120, 212, 0.07)',
      surfaceActive: 'rgba(0, 120, 212, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(0, 120, 212, 0.08)',
      borderSubtle: 'rgba(0, 120, 212, 0.04)',
      text: '#161616',
      textSecondary: '#393939',
      textMuted: '#6f6f6f',
      accent: '#0f62fe',
      accentHover: '#0549db',
      green: '#198038',
      amber: '#ee5396',
      red: '#da1e28',
      info: '#1192e8',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: GRADIENT_NONE,
    },
  },
  poimandres: {
    label: 'Poimandres',
    description: 'Deep ocean — soft sky and sea-glass on midnight turquoise.',
    swatches: ['#1b1e28', '#add7ff', '#5de4c7', '#fae4fc'],
    dark: {
      base: '#1b1e28',
      surface: 'rgba(173, 215, 255, 0.04)',
      surfaceHover: 'rgba(173, 215, 255, 0.085)',
      surfaceActive: 'rgba(173, 215, 255, 0.14)',
      elevated: '#252b37',
      border: 'rgba(173, 215, 255, 0.07)',
      borderSubtle: 'rgba(173, 215, 255, 0.035)',
      text: '#e4f0fb',
      textSecondary: '#a6accd',
      textMuted: '#7e84a3',
      accent: '#add7ff',
      accentHover: '#c6e3ff',
      green: '#5de4c7',
      amber: '#fffac2',
      red: '#d0679d',
      info: '#89ddff',
      accentForeground: '#12141b',
      shadowCard: DARK_SHADOW_SOFT,
      shadowElevated: DARK_SHADOW_SOFT_LG,
      bgImage: 'radial-gradient(100% 70% at 0% 0%, rgba(93, 228, 199, 0.07), transparent 55%), radial-gradient(80% 60% at 100% 100%, rgba(173, 215, 255, 0.06), transparent 55%)',
    },
    light: {
      base: '#fbfbfb',
      surface: 'rgba(23, 102, 153, 0.03)',
      surfaceHover: 'rgba(23, 102, 153, 0.07)',
      surfaceActive: 'rgba(23, 102, 153, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(23, 102, 153, 0.08)',
      borderSubtle: 'rgba(23, 102, 153, 0.04)',
      text: '#001b33',
      textSecondary: '#465679',
      textMuted: '#6a748e',
      accent: '#176699',
      accentHover: '#0f5282',
      green: '#1f877a',
      amber: '#c17e1d',
      red: '#ab2966',
      info: '#2e8dcc',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: GRADIENT_NONE,
    },
  },
  cyberdream: {
    label: 'Cyberdream',
    description: 'Neo-Tokyo cyberpunk — electric cyan and magenta on obsidian.',
    swatches: ['#16181a', '#5ea1ff', '#ff5ea1', '#5eff8a'],
    dark: {
      base: '#1a1c20',
      surface: 'rgba(94, 161, 255, 0.04)',
      surfaceHover: 'rgba(94, 161, 255, 0.09)',
      surfaceActive: 'rgba(94, 161, 255, 0.15)',
      elevated: '#22252b',
      border: 'rgba(94, 161, 255, 0.075)',
      borderSubtle: 'rgba(94, 161, 255, 0.038)',
      text: '#ffffff',
      textSecondary: '#bec0c5',
      textMuted: '#81838a',
      accent: '#5ea1ff',
      accentHover: '#7db5ff',
      green: '#5eff8a',
      amber: '#ffbd5e',
      red: '#ff6e5e',
      info: '#5ef1ff',
      accentForeground: '#0a0c10',
      shadowCard: '0 0 24px rgba(94, 161, 255, 0.08), 0 8px 24px rgba(0, 0, 0, 0.4)',
      shadowElevated: '0 0 40px rgba(94, 161, 255, 0.14), 0 18px 44px rgba(0, 0, 0, 0.5)',
      bgImage: 'radial-gradient(70% 50% at 0% 0%, rgba(94, 161, 255, 0.08), transparent 55%), radial-gradient(70% 50% at 100% 100%, rgba(255, 94, 161, 0.06), transparent 55%)',
    },
    light: {
      base: '#f5f6f8',
      surface: 'rgba(30, 108, 230, 0.03)',
      surfaceHover: 'rgba(30, 108, 230, 0.07)',
      surfaceActive: 'rgba(30, 108, 230, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(30, 108, 230, 0.08)',
      borderSubtle: 'rgba(30, 108, 230, 0.04)',
      text: '#16181a',
      textSecondary: '#3d4148',
      textMuted: '#6e7178',
      accent: '#1e6be5',
      accentHover: '#1258ca',
      green: '#08875e',
      amber: '#c47f00',
      red: '#d43f3f',
      info: '#0891b2',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: GRADIENT_NONE,
    },
  },
  matrix: {
    label: 'Matrix',
    description: 'OpenCode Matrix — hacker green on black with cyan and purple rain.',
    swatches: ['#0a0e0a', '#2eff6a', '#62ff94', '#c770ff'],
    dark: {
      base: '#0a0e0a',
      surface: '#0e130d',
      surfaceHover: '#141c12',
      surfaceActive: '#1e2a1b',
      elevated: '#0e130d',
      border: '#1e2a1b',
      borderSubtle: '#141c12',
      text: '#62ff94',
      textSecondary: '#8ca391',
      textMuted: '#6d7e72',
      accent: '#2eff6a',
      accentHover: '#62ff94',
      green: '#62ff94',
      amber: '#e6ff57',
      red: '#ff4b4b',
      info: '#30b3ff',
      accentForeground: '#061006',
      shadowCard: '0 0 18px rgba(46, 255, 106, 0.08), 0 8px 22px rgba(0, 0, 0, 0.42)',
      shadowElevated: '0 0 30px rgba(46, 255, 106, 0.14), 0 18px 44px rgba(0, 0, 0, 0.52)',
      bgImage: 'linear-gradient(180deg, rgba(46, 255, 106, 0.05), transparent 34%), repeating-linear-gradient(0deg, rgba(98, 255, 148, 0.035) 0 1px, transparent 1px 11px)',
    },
    light: {
      base: '#eef3ea',
      surface: '#e4ebe1',
      surfaceHover: '#dae1d7',
      surfaceActive: 'rgba(28, 194, 75, 0.16)',
      elevated: '#f7faf4',
      border: 'rgba(116, 132, 118, 0.28)',
      borderSubtle: 'rgba(116, 132, 118, 0.14)',
      text: '#203022',
      textSecondary: '#475548',
      textMuted: '#647266',
      accent: '#127f31',
      accentHover: '#159a3b',
      green: '#1cc24b',
      amber: '#745a00',
      red: '#d53a3a',
      info: '#1875ad',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: 'linear-gradient(180deg, rgba(28, 194, 75, 0.06), transparent 34%), repeating-linear-gradient(0deg, rgba(28, 194, 75, 0.04) 0 1px, transparent 1px 12px)',
    },
  },
  moonfly: {
    label: 'Moonfly',
    description: 'Monochrome minimalism — ghostly lavender glyphs on true graphite.',
    swatches: ['#1c1c1c', '#b8a3ff', '#80a0ff', '#85dc85'],
    dark: {
      base: '#1c1c1c',
      surface: 'rgba(184, 163, 255, 0.04)',
      surfaceHover: 'rgba(184, 163, 255, 0.085)',
      surfaceActive: 'rgba(184, 163, 255, 0.14)',
      elevated: '#252525',
      border: 'rgba(184, 163, 255, 0.07)',
      borderSubtle: 'rgba(184, 163, 255, 0.035)',
      text: '#e4e4e4',
      textSecondary: '#b0b0b0',
      textMuted: '#848484',
      accent: '#b8a3ff',
      accentHover: '#c8b6ff',
      green: '#85dc85',
      amber: '#e3c78a',
      red: '#ff5454',
      info: '#80a0ff',
      accentForeground: '#141414',
      shadowCard: DARK_SHADOW_FLAT,
      shadowElevated: DARK_SHADOW_FLAT_LG,
      bgImage: GRADIENT_NONE,
    },
    light: {
      base: '#f5f5f5',
      surface: 'rgba(90, 70, 170, 0.03)',
      surfaceHover: 'rgba(90, 70, 170, 0.07)',
      surfaceActive: 'rgba(90, 70, 170, 0.12)',
      elevated: '#ffffff',
      border: 'rgba(90, 70, 170, 0.07)',
      borderSubtle: 'rgba(90, 70, 170, 0.035)',
      text: '#1c1c1c',
      textSecondary: '#3e3e3e',
      textMuted: '#707070',
      accent: '#5a46aa',
      accentHover: '#473689',
      green: '#2e7d32',
      amber: '#b17c00',
      red: '#c62828',
      info: '#1565c0',
      accentForeground: '#ffffff',
      shadowCard: LIGHT_SHADOW_SOFT,
      shadowElevated: LIGHT_SHADOW_SOFT_LG,
      bgImage: GRADIENT_NONE,
    },
  },
} satisfies Record<string, ThemeDefinitionSeed>

export const UI_THEME_PRESETS = Object.fromEntries(
  Object.entries(UI_THEME_PRESET_SEEDS).map(([id, theme]) => [id, refineThemeDefinition(theme)]),
) as Record<keyof typeof UI_THEME_PRESET_SEEDS, ThemeDefinition>
