/* ============================================
   Drawflow Custom Styles for Behavior Tree
   ============================================ */

/* ========== Drawflow Base Overrides ========== */
#drawflow {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: var(--bg-canvas);
  background-image:
    linear-gradient(var(--canvas-grid-color) 1px, transparent 1px),
    linear-gradient(90deg, var(--canvas-grid-color) 1px, transparent 1px);
  background-size: 20px 20px;
  transition: var(--theme-transition);
}

#drawflow .drawflow {
  transform-origin: 0 0;
}

.drawflow .parent-drawflow {
  position: relative;
}

.drawflow .drawflow-node {
  background: var(--node-bg);
  border: 2px solid var(--node-border);
  border-radius: 8px;
  color: var(--node-text);
  padding: 0;
  min-width: 200px;
  max-width: 280px;
  min-height: 80px;
  box-shadow: var(--shadow-md);
  transition:
    box-shadow 0.2s ease,
    background 0.2s ease,
    border-color 0.2s ease;
  /* 性能优化 */
  will-change: transform;
  transform: translateZ(0);
  backface-visibility: hidden;
  -webkit-font-smoothing: subpixel-antialiased;
  overflow: visible;
}

.drawflow .drawflow-node:hover {
  box-shadow: var(--shadow-lg);
}

.drawflow .drawflow-node.selected {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.5);
  background: var(--bg-tertiary);
}

/* ========== Node Header ========== */
.drawflow-node-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--node-border);
  border-radius: 6px 6px 0 0;
  font-weight: 600;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.drawflow-node-header .node-icon {
  font-size: 18px;
}

.drawflow-node-header .node-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.drawflow-node-header .node-status {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* 折叠/展开按钮样式 */
.drawflow-node-header .btn-collapse {
  width: 20px;
  height: 20px;
  padding: 0;
  margin-left: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(0, 0, 0, 0.25);
  color: #ffffff;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  user-select: none;
  -webkit-appearance: none;
  appearance: none;
}

.drawflow-node-header .btn-collapse:hover {
  /* Deepen the background (not lighten it) so the button stands out
       against the colored header in both themes. The lighter
       rgba(255,255,255,0.08) washes out into the header color. */
  background: rgba(0, 0, 0, 0.5);
  border-color: rgba(255, 255, 255, 0.6);
  transform: translateZ(0) scale(1.05);
  transition: all 0.15s ease;
}

.drawflow-node-header .btn-collapse:active {
  transform: translateZ(0) scale(0.96);
}

.drawflow-node-header .btn-collapse:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.35);
  border-color: #4a90e2;
}

/* Node Status Colors */
.node-status.status-0 {
  background-color: #808080;
} /* Unknown/Idle */
.node-status.status-1 {
  background-color: #ffc107;
} /* Running */
.node-status.status-2 {
  background-color: #28a745;
} /* Success */
.node-status.status-3 {
  background-color: #dc3545;
} /* Failure */

/* ========== Node Type Specific Styles ========== */
.drawflow-node.node-composite .drawflow-node-header {
  background: linear-gradient(135deg, #9b59b6 0%, #8e44ad 100%);
  color: white;
}

.drawflow-node.node-action .drawflow-node-header {
  background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
  color: white;
}

.drawflow-node.node-node-condition .drawflow-node-header {
  background: linear-gradient(135deg, #1abc9c 0%, #16a085 100%);
  color: white;
}

.drawflow-node.node-decorator .drawflow-node-header {
  background: linear-gradient(135deg, #e67e22 0%, #d35400 100%);
  color: white;
}

/* ========== Node Body ========== */
.drawflow-node-body {
  padding: 12px;
  font-size: 12px;
  min-height: 40px;
  overflow: hidden;
}

.node-description {
  color: var(--text-secondary);
  margin-bottom: 8px;
  font-style: italic;
}

.node-ports-list {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 100%;
  box-sizing: border-box;
}

.node-port-item {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  border-bottom: 1px solid var(--node-port-border);
}

.node-port-item:last-child {
  border-bottom: none;
}

.port-name {
  color: var(--node-text);
  font-weight: 500;
}

.port-type {
  color: var(--text-muted);
  font-size: 11px;
}

.port-value {
  color: var(--primary-color);
  font-family: "Courier New", monospace;
}

/* ========== Port Input Styles ========== */
.port-value-input {
  width: 100%;
  max-width: 100%;
  min-width: 60px;
  padding: 3px 6px;
  background-color: var(--node-input-bg);
  border: 1px solid var(--border-light);
  border-radius: 3px;
  color: var(--primary-color);
  font-size: 11px;
  font-family: "Courier New", monospace;
  transition: border-color 0.2s ease;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-sizing: border-box;
}

.port-value-input:focus {
  outline: none;
  border-color: var(--primary-color);
  background-color: var(--bg-canvas);
  box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
}

.port-value-input:hover {
  border-color: var(--text-muted);
}

.port-value-input::placeholder {
  color: var(--text-muted);
  font-style: italic;
}

/* ========== Node Footer ========== */
.drawflow-node-footer {
  padding: 8px 12px;
  border-top: 1px solid var(--node-border);
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.node-type-badge {
  padding: 2px 6px;
  border-radius: 3px;
  background-color: var(--badge-bg);
  color: var(--badge-fg);
  font-size: 10px;
}

/* ========== Connection Points (Inputs/Outputs) ========== */
.drawflow .drawflow-node .input,
.drawflow .drawflow-node .output {
  position: absolute;
  width: 16px;
  height: 16px;
  background: var(--primary-color);
  border: 3px solid var(--node-bg);
  border-radius: 50%;
  cursor: crosshair;
  z-index: 10;
  transition: all 0.2s ease;
}

.drawflow .drawflow-node .input:hover,
.drawflow .drawflow-node .output:hover {
  background: var(--primary-dark);
  width: 20px;
  height: 20px;
  margin-top: -2px;
  margin-left: -2px;
}

.drawflow .drawflow-node .input {
  left: -8px;
  top: 50%;
  transform: translateY(-50%);
}

.drawflow .drawflow-node .output {
  right: -8px;
  top: 50%;
  transform: translateY(-50%);
}

/* ===================================
   Vertical Layout Mode (Top-Bottom)
   =================================== */

/* Vertical layout: input on top, output on bottom */
.layout-vertical .drawflow .drawflow-node .input,
.drawflow.layout-vertical .drawflow-node .input {
  left: 50% !important;
  top: -8px !important;
  right: auto !important;
  bottom: auto !important;
  transform: translateX(-50%) !important;
}

.layout-vertical .drawflow .drawflow-node .output,
.drawflow.layout-vertical .drawflow-node .output {
  left: 50% !important;
  bottom: -8px !important;
  right: auto !important;
  top: auto !important;
  transform: translateX(-50%) !important;
}

/* Hover effect for vertical layout */
.layout-vertical .drawflow .drawflow-node .input:hover,
.drawflow.layout-vertical .drawflow-node .input:hover,
.layout-vertical .drawflow .drawflow-node .output:hover,
.drawflow.layout-vertical .drawflow-node .output:hover {
  margin-top: 0 !important;
  margin-left: -2px !important;
  margin-right: -2px !important;
  margin-bottom: 0 !important;
  width: 20px !important;
  height: 20px !important;
}

.layout-vertical .drawflow .drawflow-node .input:hover,
.drawflow.layout-vertical .drawflow-node .input:hover {
  margin-top: -2px !important;
  margin-bottom: 0 !important;
}

.layout-vertical .drawflow .drawflow-node .output:hover,
.drawflow.layout-vertical .drawflow-node .output:hover {
  margin-bottom: -2px !important;
  margin-top: 0 !important;
}

/* ===================================
   Horizontal Layout Mode (Left-Right) - Default
   =================================== */

/* Horizontal layout: input on left, output on right */
.layout-horizontal .drawflow .drawflow-node .input,
.drawflow.layout-horizontal .drawflow-node .input {
  left: -8px !important;
  top: 50% !important;
  right: auto !important;
  bottom: auto !important;
  transform: translateY(-50%) !important;
}

.layout-horizontal .drawflow .drawflow-node .output,
.drawflow.layout-horizontal .drawflow-node .output {
  right: -8px !important;
  top: 50% !important;
  left: auto !important;
  bottom: auto !important;
  transform: translateY(-50%) !important;
}

/* Hover effect for horizontal layout */
.layout-horizontal .drawflow .drawflow-node .input:hover,
.drawflow.layout-horizontal .drawflow-node .input:hover,
.layout-horizontal .drawflow .drawflow-node .output:hover,
.drawflow.layout-horizontal .drawflow-node .output:hover {
  margin-top: -2px !important;
  margin-left: -2px !important;
  margin-right: -2px !important;
  margin-bottom: 0 !important;
  width: 20px !important;
  height: 20px !important;
}

/* ========== Connection Lines ========== */
.drawflow .connection {
  position: absolute;
  z-index: 1;
  /* 连接线性能优化 */
  will-change: auto;
}

.drawflow .connection .main-path {
  fill: none;
  stroke: var(--primary-color);
  stroke-width: 3px;
  transition:
    stroke 0.15s ease,
    stroke-width 0.15s ease;
}

.drawflow .connection .main-path.selected {
  stroke: var(--success-color);
  stroke-width: 6px;
}

/* Connection points on lines */
.drawflow .connection .point {
  cursor: move;
  fill: var(--primary-color);
  stroke: var(--node-bg);
  stroke-width: 2px;
}

.drawflow .connection .point:hover {
  fill: var(--primary-dark);
}

/* ========== Delete Button on Nodes ========== */
.drawflow-node .btn-delete {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 24px;
  height: 24px;
  background: var(--danger-color);
  border: 2px solid var(--node-bg);
  border-radius: 50%;
  color: white;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.2s ease;
  z-index: 20;
}

.drawflow-node:hover .btn-delete {
  opacity: 1;
}

.drawflow-node .btn-delete:hover {
  background: #c82333;
  transform: scale(1.1);
}

/* ========== Context Menu ========== */
.drawflow-context-menu {
  position: fixed;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  box-shadow: var(--shadow-md);
  padding: 8px 0;
  min-width: 180px;
  z-index: 1000;
  display: none;
}

.drawflow-context-menu.active {
  display: block;
}

.context-menu-item {
  padding: 10px 16px;
  color: var(--text-primary);
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
}

.context-menu-item:hover {
  background: var(--bg-tertiary);
}

.context-menu-item.danger:hover {
  background: var(--danger-color);
  color: white;
}

.context-menu-divider {
  height: 1px;
  background: var(--border-color);
  margin: 4px 0;
}

/* ========== Selection Box ========== */
.drawflow .selection-box {
  position: absolute;
  border: 2px dashed var(--primary-color);
  background: rgba(74, 144, 226, 0.1);
  pointer-events: none;
  z-index: 100;
}

/* ========== Minimap (Optional Feature) ========== */
.drawflow-minimap {
  position: absolute;
  bottom: 20px;
  right: 20px;
  width: 200px;
  height: 150px;
  background: rgba(45, 45, 45, 0.9);
  border: 2px solid var(--node-border);
  border-radius: 6px;
  overflow: hidden;
  z-index: 50;
}

/* ========== Node Templates (for drag preview) ========== */
.node-drag-preview {
  position: fixed;
  pointer-events: none;
  z-index: 10000;
  opacity: 0.7;
  transform: translate(-50%, -50%);
}

/* ========== Loading Overlay ========== */
.drawflow-loading {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--panel-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.drawflow-loading .spinner {
  width: 50px;
  height: 50px;
  border: 4px solid rgba(74, 144, 226, 0.3);
  border-top-color: #4a90e2;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ========== Node Animations ========== */
@keyframes nodeHighlight {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(74, 144, 226, 0.7);
  }
  50% {
    box-shadow: 0 0 0 10px rgba(74, 144, 226, 0);
  }
}

.drawflow-node.highlight {
  animation: nodeHighlight 1s ease-out;
}

/* ========== Running State Animation ========== */
.drawflow-node.running .drawflow-node-header {
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.7;
  }
}

/* ========== Error State ========== */
.drawflow-node.error {
  border-color: #dc3545;
  animation: shake 0.5s ease-in-out;
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-5px);
  }
  75% {
    transform: translateX(5px);
  }
}

/* ========== Success State ========== */
.drawflow-node.success {
  border-color: #28a745;
}

/* ========== Zoom Level Indicator ========== */
.zoom-indicator {
  position: absolute;
  bottom: 20px;
  left: 20px;
  background: rgba(45, 45, 45, 0.9);
  border: 1px solid var(--node-border);
  border-radius: 4px;
  padding: 8px 12px;
  color: var(--text-primary);
  font-size: 12px;
  z-index: 50;
}

/* ========== Grid Lines Enhancement ========== */
.drawflow.show-grid {
  background-image:
    linear-gradient(rgba(74, 144, 226, 0.1) 1px, transparent 1px),
    linear-gradient(90deg, rgba(74, 144, 226, 0.1) 1px, transparent 1px);
}

/* ========== Readonly Mode ========== */
.drawflow.readonly .drawflow-node {
  cursor: default;
}

.drawflow.readonly .drawflow-node .input,
.drawflow.readonly .drawflow-node .output {
  cursor: not-allowed;
  pointer-events: none;
}

.drawflow.readonly .btn-delete {
  display: none !important;
}

/* ========== 拖拽优化 ========== */
.drawflow .drawflow-node.dragging {
  opacity: 0.8;
  cursor: move !important;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.8);
}

/* 拖拽时禁用过渡效果以提高性能 */
.drawflow .parent-drawflow.dragging .drawflow-node {
  transition: none !important;
}

.drawflow .parent-drawflow.dragging .connection {
  transition: none !important;
}

/* 布局重排时禁用所有过渡，确保 getBoundingClientRect 立即获得最终位置 */
.no-transition .drawflow-node,
.no-transition .drawflow-node .input,
.no-transition .drawflow-node .output,
.no-transition .drawflow-node * {
  transition: none !important;
}

/* 防止拖拽时文本选中 */
.drawflow .drawflow-node * {
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}

/* 提高连接点的点击区域 */
.drawflow .drawflow-node .input,
.drawflow .drawflow-node .output {
  cursor: crosshair !important;
  z-index: 20;
}

/* 连接点悬停时放大效果优化 */
.drawflow .drawflow-node .input:hover,
.drawflow .drawflow-node .output:hover {
  transition: all 0.1s ease;
}

/* 确保输入框不会触发节点拖拽 */
.port-value-input {
  cursor: text !important;
}

/* 调整节点端口列表样式 */
.node-port-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
  border-bottom: 1px solid #3a3a3a;
  gap: 6px;
  min-height: 24px;
}

.node-port-item:last-child {
  border-bottom: none;
}

.node-port-item .port-name {
  color: var(--node-text);
  font-weight: 500;
  font-size: 11px;
  flex-shrink: 0;
  white-space: nowrap;
}

/* 端口输入框容器 */
.port-input-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}

/* ========== Replay Status — Whole-Node Visual Override ========== */

/* Idle: dim the node to make active nodes stand out */
.drawflow-node.replay-idle {
  opacity: 0.45;
  filter: saturate(0.3);
  border-color: #555 !important;
  transition:
    opacity 0.3s,
    filter 0.3s,
    border-color 0.3s,
    box-shadow 0.3s,
    background 0.3s;
}

/* Running: vivid blue glow + full opacity */
.drawflow-node.replay-running {
  opacity: 1;
  filter: none;
  border: 2.5px solid #42a5f5 !important;
  background: linear-gradient(135deg, #1a237e 0%, #1e3a5f 100%) !important;
  box-shadow:
    0 0 18px 4px rgba(66, 165, 245, 0.55),
    inset 0 0 8px rgba(66, 165, 245, 0.15);
  animation: replayPulse 1.4s ease-in-out infinite;
  transition:
    opacity 0.2s,
    filter 0.2s,
    border-color 0.2s,
    box-shadow 0.2s,
    background 0.2s;
}

/* Success: green fill */
.drawflow-node.replay-success {
  opacity: 1;
  filter: none;
  border: 2.5px solid #66bb6a !important;
  background: linear-gradient(135deg, #1b3d1e 0%, #2e4a30 100%) !important;
  box-shadow: 0 0 12px 2px rgba(102, 187, 106, 0.45);
  transition:
    opacity 0.3s,
    filter 0.3s,
    border-color 0.3s,
    box-shadow 0.3s,
    background 0.3s;
}

/* Failure: red fill */
.drawflow-node.replay-failure {
  opacity: 1;
  filter: none;
  border: 2.5px solid #ef5350 !important;
  background: linear-gradient(135deg, #4a1414 0%, #5c2020 100%) !important;
  box-shadow: 0 0 12px 2px rgba(239, 83, 80, 0.45);
  transition:
    opacity 0.3s,
    filter 0.3s,
    border-color 0.3s,
    box-shadow 0.3s,
    background 0.3s;
}

/* Header overrides — keep header legible on new backgrounds */
.drawflow-node.replay-running .drawflow-node-header {
  background: rgba(33, 150, 243, 0.25) !important;
}
.drawflow-node.replay-success .drawflow-node-header {
  background: rgba(76, 175, 80, 0.25) !important;
}
.drawflow-node.replay-failure .drawflow-node-header {
  background: rgba(244, 67, 54, 0.25) !important;
}

/* Pulsing glow for running nodes */
@keyframes replayPulse {
  0%,
  100% {
    box-shadow:
      0 0 18px 4px rgba(66, 165, 245, 0.55),
      inset 0 0 8px rgba(66, 165, 245, 0.15);
  }
  50% {
    box-shadow:
      0 0 30px 8px rgba(66, 165, 245, 0.75),
      inset 0 0 12px rgba(66, 165, 245, 0.25);
  }
}

/* Status dot: bigger + glow */
.replay-running .node-status {
  width: 14px !important;
  height: 14px !important;
  background-color: #42a5f5 !important;
  box-shadow: 0 0 8px #42a5f5;
}
.replay-success .node-status {
  width: 14px !important;
  height: 14px !important;
  background-color: #66bb6a !important;
  box-shadow: 0 0 8px #66bb6a;
}
.replay-failure .node-status {
  width: 14px !important;
  height: 14px !important;
  background-color: #ef5350 !important;
  box-shadow: 0 0 8px #ef5350;
}

/* ============================================
   Light Theme Tweaks
   ============================================ */
:root[data-theme="light"] .drawflow-minimap,
:root[data-theme="light"] .zoom-indicator {
  background: rgba(255, 255, 255, 0.92);
}

/* Replay-idle: in light theme, full desaturation loses too much contrast */
:root[data-theme="light"] .drawflow-node.replay-idle {
  opacity: 0.5;
  filter: saturate(0.4) brightness(1.05);
}
