/* ============================================
   灵动万象 Multi-Theme System
   5套配色方案 + 风格选择器 + 动画效果
   ============================================ */

/* ============================================
   1. 默认科技蓝 (Default Tech Blue)
   ============================================ */
:root {
  --brand-from: #06B6D4;
  --brand-mid: #3B82F6;
  --brand-to: #8B5CF6;
  --brand-grad: linear-gradient(135deg, var(--brand-from) 0%, var(--brand-mid) 50%, var(--brand-to) 100%);
  --brand-glow: 0 0 20px rgba(59, 130, 246, 0.5), 0 0 40px rgba(6, 182, 212, 0.3), 0 0 60px rgba(139, 92, 246, 0.2);
  --brand-soft: rgba(59, 130, 246, 0.12);
  --brand-border: rgba(59, 130, 246, 0.32);
  --brand-shadow: rgba(59, 130, 246, 0.18);
  --canvas-bg: #0a0e1a;
  --canvas-grid: rgba(59, 130, 246, 0.15);
  --surface-page: #0f172a;
  --surface-card: rgba(255, 255, 255, 0.06);
  --surface-soft: rgba(248, 250, 252, 0.04);
  --text-main: #f1f5f9;
  --text-muted: #94a3b8;

  /* 兼容旧变量名 */
  --page: #0f172a;
  --grid: rgba(59, 130, 246, 0.15);
  --panel: rgba(15, 23, 42, 0.94);
  --card: rgba(30, 41, 59, 0.96);
  --card-solid: #1e293b;
  --soft: #1e293b;
  --soft-2: #334155;
  --line: rgba(59, 130, 246, 0.2);
  --line-2: rgba(59, 130, 246, 0.3);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --faint: #64748b;
  --shadow: rgba(0, 0, 0, 0.3);
  --strong: #f1f5f9;
  --strong-text: #0f172a;
}

/* ============================================
   2. 电商金 (Commerce Gold)
   ============================================ */
.theme-commerce {
  --brand-from: #FCD34D;
  --brand-mid: #F59E0B;
  --brand-to: #D97706;
  --brand-grad: linear-gradient(135deg, var(--brand-from) 0%, var(--brand-mid) 50%, var(--brand-to) 100%);
  --brand-glow: 0 0 20px rgba(245, 158, 11, 0.5), 0 0 40px rgba(252, 211, 77, 0.3), 0 0 60px rgba(217, 119, 6, 0.2);
  --brand-soft: rgba(245, 158, 11, 0.12);
  --brand-border: rgba(245, 158, 11, 0.32);
  --brand-shadow: rgba(245, 158, 11, 0.18);
  --canvas-bg: #1a1410;
  --canvas-grid: rgba(245, 158, 11, 0.18);
  --surface-page: #1a1410;
  --surface-card: rgba(253, 224, 71, 0.06);
  --surface-soft: rgba(252, 211, 77, 0.04);
  --text-main: #fef3c7;
  --text-muted: #d97706;

  /* 兼容旧变量名 */
  --page: #f7f3ec;
  --grid: rgba(180, 155, 110, 0.28);
  --panel: rgba(250, 247, 242, 0.94);
  --card: rgba(255, 252, 247, 0.97);
  --card-solid: #fffcf7;
  --soft: #f8f4ee;
  --soft-2: #f0ebe3;
  --line: #e8dfd0;
  --line-2: #d5c9b0;
  --text: #1e1208;
  --muted: #7a6a52;
  --faint: #b0a08a;
  --shadow: rgba(60, 35, 5, 0.09);
  --strong: #1e1208;
  --strong-text: #fff;
}

/* ============================================
   3. 创意紫 (Creative Purple)
   ============================================ */
.theme-creative {
  --brand-from: #C084FC;
  --brand-mid: #A855F7;
  --brand-to: #EC4899;
  --brand-grad: linear-gradient(135deg, var(--brand-from) 0%, var(--brand-mid) 50%, var(--brand-to) 100%);
  --brand-glow: 0 0 20px rgba(168, 85, 247, 0.5), 0 0 40px rgba(192, 132, 252, 0.3), 0 0 60px rgba(236, 72, 153, 0.2);
  --brand-soft: rgba(168, 85, 247, 0.12);
  --brand-border: rgba(168, 85, 247, 0.32);
  --brand-shadow: rgba(168, 85, 247, 0.18);
  --canvas-bg: #14091a;
  --canvas-grid: rgba(192, 132, 252, 0.16);
  --surface-page: #1e1b4b;
  --surface-card: rgba(192, 132, 252, 0.06);
  --surface-soft: rgba(168, 85, 247, 0.04);
  --text-main: #f3e8ff;
  --text-muted: #c084fc;

  /* 兼容旧变量名 */
  --page: #1a0a24;
  --grid: rgba(192, 132, 252, 0.16);
  --panel: rgba(30, 27, 75, 0.94);
  --card: rgba(46, 16, 101, 0.96);
  --card-solid: #2e1065;
  --soft: #3b0764;
  --soft-2: #581c87;
  --line: rgba(168, 85, 247, 0.2);
  --line-2: rgba(168, 85, 247, 0.3);
  --text: #f3e8ff;
  --muted: #c084fc;
  --faint: #a855f7;
  --shadow: rgba(88, 28, 135, 0.3);
  --strong: #f3e8ff;
  --strong-text: #1e1b4b;
}

/* ============================================
   4. 自然绿 (Nature Green)
   ============================================ */
.theme-nature {
  --brand-from: #34D399;
  --brand-mid: #10B981;
  --brand-to: #14B8A6;
  --brand-grad: linear-gradient(135deg, var(--brand-from) 0%, var(--brand-mid) 50%, var(--brand-to) 100%);
  --brand-glow: 0 0 20px rgba(16, 185, 129, 0.5), 0 0 40px rgba(52, 211, 153, 0.3), 0 0 60px rgba(20, 184, 166, 0.2);
  --brand-soft: rgba(16, 185, 129, 0.12);
  --brand-border: rgba(16, 185, 129, 0.32);
  --brand-shadow: rgba(16, 185, 129, 0.18);
  --canvas-bg: #0a1410;
  --canvas-grid: rgba(52, 211, 153, 0.16);
  --surface-page: #064e3b;
  --surface-card: rgba(52, 211, 153, 0.06);
  --surface-soft: rgba(16, 185, 129, 0.04);
  --text-main: #d1fae5;
  --text-muted: #6ee7b7;

  /* 兼容旧变量名 */
  --page: #0a1410;
  --grid: rgba(52, 211, 153, 0.16);
  --panel: rgba(6, 78, 59, 0.94);
  --card: rgba(6, 95, 70, 0.96);
  --card-solid: #065f46;
  --soft: #047857;
  --soft-2: #059669;
  --line: rgba(16, 185, 129, 0.2);
  --line-2: rgba(16, 185, 129, 0.3);
  --text: #d1fae5;
  --muted: #6ee7b7;
  --faint: #34d399;
  --shadow: rgba(4, 120, 87, 0.3);
  --strong: #d1fae5;
  --strong-text: #064e3b;
}

/* ============================================
   5. 深色黑 (Dark Theme)
   ============================================ */
.theme-dark-theme {
  --brand-from: #64748B;
  --brand-mid: #475569;
  --brand-to: #1E293B;
  --brand-grad: linear-gradient(135deg, var(--brand-from) 0%, var(--brand-mid) 50%, var(--brand-to) 100%);
  --brand-glow: 0 0 20px rgba(71, 85, 105, 0.5), 0 0 40px rgba(100, 116, 139, 0.3), 0 0 60px rgba(30, 41, 59, 0.2);
  --brand-soft: rgba(71, 85, 105, 0.12);
  --brand-border: rgba(71, 85, 105, 0.32);
  --brand-shadow: rgba(71, 85, 105, 0.18);
  --canvas-bg: #000000;
  --canvas-grid: rgba(100, 116, 139, 0.12);
  --surface-page: #0f172a;
  --surface-card: rgba(100, 116, 139, 0.06);
  --surface-soft: rgba(71, 85, 105, 0.04);
  --text-main: #e2e8f0;
  --text-muted: #94a3b8;

  /* 兼容旧变量名 */
  --page: #0b1020;
  --grid: rgba(148, 163, 184, 0.16);
  --panel: rgba(17, 24, 39, 0.9);
  --card: rgba(15, 23, 42, 0.96);
  --card-solid: #111827;
  --soft: #1e293b;
  --soft-2: #263449;
  --line: #334155;
  --line-2: #64748b;
  --text: #f8fafc;
  --muted: #cbd5e1;
  --faint: #94a3b8;
  --shadow: rgba(0, 0, 0, 0.28);
  --strong: #f8fafc;
  --strong-text: #0f172a;
}

/* ============================================
   风格选择器 UI 样式
   ============================================ */

/* 选择器容器 */
.theme-selector {
  position: relative;
  display: inline-block;
  z-index: 1000;
}

/* 侧边栏内的选择器容器 */
.sidebar .theme-selector {
  width: 100%;
  margin: 12px 0;
}

/* 触发按钮（胶囊：品牌色圆点 + 当前主题名） */
.theme-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 14px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(12px);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

/* 侧边栏内的触发按钮 */
.sidebar .theme-trigger {
  width: 44px;
  height: 36px;
  padding: 0;
  justify-content: center;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  transition: width 0.3s var(--fluid-ease) 0.5s, gap 0.3s var(--fluid-ease) 0.5s, background-color 0.2s, color 0.2s, border-color 0.2s;
  overflow: hidden;
}

.sidebar:hover .theme-trigger {
  width: 170px;
  padding: 0 14px;
  justify-content: flex-start;
  gap: 8px;
  transition-delay: 0s;
}

.sidebar .theme-trigger:hover {
  background: var(--nav-hover-bg);
  color: var(--text);
  border-color: var(--stage-border);
}

/* 触发按钮内的品牌色圆点（呼吸光效） */
.theme-icon {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--brand-grad);
  box-shadow: var(--brand-glow);
  animation: breathe 2.4s ease-in-out infinite;
}

#currentThemeName {
  white-space: nowrap;
}

/* 侧边栏内的主题名称 */
.sidebar #currentThemeName {
  opacity: 0;
  max-width: 0;
  overflow: hidden;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.02em;
  transition: opacity 0.3s 0.5s, max-width 0.3s var(--fluid-ease) 0.5s;
}

.sidebar:hover #currentThemeName {
  opacity: 1;
  max-width: 120px;
  transition-delay: 0.15s;
}

/* 下拉菜单 */
.theme-menu {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  transform: translateY(-8px) scale(0.96);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: rgba(15, 23, 42, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  padding: 8px;
  min-width: 200px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 侧边栏内的下拉菜单 */
.sidebar .theme-menu {
  left: auto;
  right: 0;
}

/* JS 通过切换 .show 控制显隐 */
.theme-menu.show {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateY(0) scale(1);
}

/* 菜单项（HTML 实际使用原生 button） */
.theme-menu button {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 14px;
  border: none;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  text-align: left;
}

.theme-menu button:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.theme-menu button.active {
  background: var(--brand-grad);
  color: #fff;
}

/* 色板圆点 */
.theme-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.15);
}


/* ============================================
   切换动画
   ============================================ */
@keyframes themeSwitch {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  25% {
    opacity: 0.5;
    transform: scale(0.98);
  }
  50% {
    opacity: 0.3;
    transform: scale(0.96);
    filter: blur(2px);
  }
  75% {
    opacity: 0.5;
    transform: scale(0.98);
  }
  100% {
    opacity: 1;
    transform: scale(1);
    filter: blur(0);
  }
}

/* 主题切换时应用到body（JS 实际添加到 html 元素，但 body 继承即可） */
html.theme-transitioning,
body.theme-switching {
  animation: themeSwitch 0.5s ease-in-out;
}

/* 元素级别的切换动画 */
.theme-transition {
  transition:
    background 0.4s ease,
    background-color 0.4s ease,
    color 0.4s ease,
    border-color 0.4s ease,
    box-shadow 0.4s ease,
    fill 0.4s ease,
    stroke 0.4s ease;
}

/* ============================================
   呼吸光效动画
   ============================================ */
@keyframes breathe {
  0%, 100% {
    opacity: 0.6;
    transform: scale(1);
    box-shadow: 0 0 10px currentColor;
  }
  50% {
    opacity: 1;
    transform: scale(1.1);
    box-shadow: 0 0 20px currentColor, 0 0 30px currentColor;
  }
}

/* 呼吸光效应用类 */
.breathe-glow {
  animation: breathe 2s ease-in-out infinite;
}

/* 带品牌色的呼吸光效 */
.breathe-brand {
  animation: breatheBrand 2s ease-in-out infinite;
}

@keyframes breatheBrand {
  0%, 100% {
    box-shadow: var(--brand-glow);
    opacity: 0.8;
  }
  50% {
    box-shadow:
      0 0 30px var(--brand-mid),
      0 0 60px var(--brand-from),
      0 0 90px var(--brand-to);
    opacity: 1;
  }
}

/* 脉冲呼吸效果 */
@keyframes breathePulse {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.05);
    opacity: 0.8;
  }
}

.breathe-pulse {
  animation: breathePulse 1.5s ease-in-out infinite;
}

/* 渐变呼吸背景 */
@keyframes breatheGradient {
  0%, 100% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
}

.breathe-gradient {
  background: var(--brand-grad);
  background-size: 200% 200%;
  animation: breatheGradient 3s ease-in-out infinite;
}

/* ============================================
   辅助工具类
   ============================================ */

/* 品牌渐变背景 */
.bg-brand-grad {
  background: var(--brand-grad);
}

/* 品牌渐变文字 */
.text-brand-grad {
  background: var(--brand-grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 品牌光晕 */
.shadow-brand-glow {
  box-shadow: var(--brand-glow);
}

/* 品牌边框 */
.border-brand-grad {
  border: 2px solid transparent;
  background:
    linear-gradient(var(--bg-color, #0f172a), var(--bg-color, #0f172a)) padding-box,
    var(--brand-grad) border-box;
}

/* ============================================
   动效增强 (Motion Enhancements)
   ============================================ */

/* --------------------------------------------
   1. 主题切换过渡动画
   -------------------------------------------- */

/* body 整体背景/文字颜色过渡 */
body {
  transition: background-color 0.4s ease, color 0.3s ease;
}

/* 使用品牌色变量的元素统一过渡 */
.bg-brand-grad,
.text-brand-grad,
.shadow-brand-glow,
.border-brand-grad,
.theme-trigger,
.theme-menu button.active,
.breathe-gradient,
[class*="brand"] {
  transition:
    background 0.4s ease,
    background-color 0.4s ease,
    color 0.3s ease,
    border-color 0.4s ease,
    box-shadow 0.4s ease,
    fill 0.4s ease,
    stroke 0.4s ease;
}

/* --------------------------------------------
   2. 风格选择器动效
   -------------------------------------------- */

/* 触发按钮 hover 轻微上浮 */
.theme-trigger {
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.3s ease, background 0.3s ease, border-color 0.3s ease;
}

.theme-trigger:hover {
  transform: translateY(-2px);
  border-color: rgba(255, 255, 255, 0.28);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.theme-trigger:active {
  transform: translateY(0) scale(0.97);
}

/* 菜单项进入时的轻微级联渐入 */
.theme-menu.show button {
  animation: menuItemIn 0.3s ease both;
}
.theme-menu.show button:nth-child(1) { animation-delay: 0.02s; }
.theme-menu.show button:nth-child(2) { animation-delay: 0.06s; }
.theme-menu.show button:nth-child(3) { animation-delay: 0.10s; }
.theme-menu.show button:nth-child(4) { animation-delay: 0.14s; }
.theme-menu.show button:nth-child(5) { animation-delay: 0.18s; }

@keyframes menuItemIn {
  from {
    opacity: 0;
    transform: translateX(-6px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* --------------------------------------------
   3. 按钮和交互元素动效
   -------------------------------------------- */

/* 主按钮 hover glow 增强 */
.btn-primary,
.brand-btn,
.bg-brand-grad.btn,
button.bg-brand-grad {
  transition:
    transform 0.25s ease,
    box-shadow 0.3s ease,
    filter 0.3s ease;
}

.btn-primary:hover,
.brand-btn:hover,
.bg-brand-grad.btn:hover,
button.bg-brand-grad:hover {
  transform: translateY(-1px);
  box-shadow:
    var(--brand-glow),
    0 0 18px var(--brand-mid),
    0 0 36px var(--brand-from);
  filter: brightness(1.08) saturate(1.1);
}

.btn-primary:active,
.brand-btn:active,
button.bg-brand-grad:active {
  transform: translateY(0) scale(0.98);
}

/* 节点选中时边框呼吸动画 */
.node.selected,
.node-selected,
.node.is-selected {
  animation: nodeSelectBreathe 1.8s ease-in-out infinite;
}

@keyframes nodeSelectBreathe {
  0%, 100% {
    box-shadow:
      0 0 0 2px var(--brand-mid),
      0 0 10px var(--brand-from);
  }
  50% {
    box-shadow:
      0 0 0 2px var(--brand-from),
      0 0 22px var(--brand-mid),
      0 0 36px var(--brand-to);
  }
}

/* --------------------------------------------
   4. 连线和光效动效
   -------------------------------------------- */

/* 连线流动光效：用于 SVG path 的 stroke-dasharray 流动 */
@keyframes wireFlow {
  to {
    stroke-dashoffset: -24;
  }
}

.wire-flow,
.connection-line.active,
path.wire-active {
  stroke-dasharray: 6 6;
  animation: wireFlow 0.8s linear infinite;
}

/* 连线光晕脉冲（用于高亮连线整体亮度） */
@keyframes wireGlow {
  0%, 100% {
    filter: drop-shadow(0 0 2px var(--brand-from));
    opacity: 0.85;
  }
  50% {
    filter: drop-shadow(0 0 6px var(--brand-mid));
    opacity: 1;
  }
}

.wire-glow {
  animation: wireGlow 2s ease-in-out infinite;
}

/* 节点端口脉冲动画 */
.node-port,
.port-dot,
.node .port {
  animation: portPulse 2s ease-in-out infinite;
}

@keyframes portPulse {
  0%, 100% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.5);
  }
  50% {
    transform: scale(1.25);
    box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.0);
  }
}

/* 端口 hover 时强化脉冲 */
.node-port:hover,
.port-dot:hover {
  animation-duration: 1s;
  box-shadow: 0 0 8px 2px var(--brand-mid);
}

/* --------------------------------------------
   5. 页面加载动效
   -------------------------------------------- */

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in-up {
  animation: fadeInUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
}

/* 首次加载的级联渐入（配合 .page-loaded 标记触发） */
body.page-loaded .fade-in-up-1 { animation: fadeInUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) 0.05s both; }
body.page-loaded .fade-in-up-2 { animation: fadeInUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) 0.12s both; }
body.page-loaded .fade-in-up-3 { animation: fadeInUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) 0.20s both; }
body.page-loaded .fade-in-up-4 { animation: fadeInUp 0.5s cubic-bezier(0.22, 1, 0.36, 1) 0.28s both; }

/* --------------------------------------------
   无障碍：尊重 prefers-reduced-motion
   关闭装饰性动画与位移，保留极短的颜色过渡以避免突兀闪烁
   -------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }

  /* 禁用位移类 hover/动画反馈 */
  .theme-trigger:hover,
  .theme-trigger:active,
  .btn-primary:hover,
  .brand-btn:hover,
  button.bg-brand-grad:hover,
  .btn-primary:active,
  .brand-btn:active,
  button.bg-brand-grad:active {
    transform: none;
  }

  /* 关闭持续循环的装饰动画 */
  .theme-icon,
  .node.selected,
  .node-selected,
  .node.is-selected,
  .wire-flow,
  .connection-line.active,
  path.wire-active,
  .wire-glow,
  .node-port,
  .port-dot,
  .node .port,
  .breathe-glow,
  .breathe-brand,
  .breathe-pulse,
  .breathe-gradient {
    animation: none !important;
  }
}
