/* ============================
   DevFlow — CSS Design System
   ============================ */

:root {
  /* Colors */
  --bg-primary: #0f1117;
  --bg-secondary: #161825;
  --bg-tertiary: #1e2035;
  --bg-card: #1a1c2e;
  --bg-card-hover: #22253a;
  --bg-surface: #252840;
  --border-color: #2a2d45;
  --border-subtle: #1f2238;
  --text-primary: #e8eaf0;
  --text-secondary: #9ca3b8;
  --text-muted: #6b7280;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --accent-glow: rgba(99, 102, 241, 0.25);
  --green: #10b981;
  --red: #ef4444;
  --orange: #f59e0b;
  --cyan: #06b6d4;
  --pink: #ec4899;
  --purple: #8b5cf6;

  /* Sizing */
  --topbar-height: 52px;
  --sidebar-width: 220px;

  /* Shadows */
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.4);
  --shadow-glow: 0 0 20px var(--accent-glow);

  /* Radius */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;

  /* Transitions */
  --transition-fast: 0.15s ease;
  --transition-normal: 0.25s ease;
}

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  height: 100vh;
  width: 100vw;
}

/* ============================
   Grid Dot Pattern on Canvas
   ============================ */
#canvas-container {
  position: relative;
  flex: 1;
  overflow: hidden;
  background-color: var(--bg-primary);
  background-image:
    radial-gradient(circle, #2a2d4533 1px, transparent 1px);
  background-size: 24px 24px;
  cursor: grab;
}

#canvas-container.grabbing {
  cursor: grabbing;
}

/* ============================
   Top Bar
   ============================ */
#topbar {
  height: var(--topbar-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
  position: relative;
  backdrop-filter: blur(12px);
}

.topbar-left,
.topbar-center,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 16px;
  background: linear-gradient(135deg, #6366f1, #a855f7);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  user-select: none;
}

.topbar-divider {
  width: 1px;
  height: 24px;
  background: var(--border-color);
  margin: 0 6px;
}

.diagram-title-input {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: border-color var(--transition-fast);
  width: 200px;
  font-family: inherit;
}

.diagram-title-input:hover {
  border-color: var(--border-color);
}

.diagram-title-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg-tertiary);
}

.topbar-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 12px;
  font-family: inherit;
  transition: all var(--transition-fast);
}

.topbar-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-color: var(--accent);
}

.topbar-btn.accent {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  font-weight: 600;
}

.topbar-btn.accent:hover {
  background: var(--accent-hover);
  box-shadow: var(--shadow-glow);
}

.zoom-label {
  font-size: 12px;
  color: var(--text-muted);
  min-width: 40px;
  text-align: center;
  user-select: none;
}

/* ============================
   Sidebar
   ============================ */
#sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  padding: 12px 0;
  user-select: none;
}

.sidebar-section {
  margin-bottom: 8px;
}

.sidebar-heading {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 1.2px;
  padding: 8px 16px 4px;
  text-transform: uppercase;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  cursor: grab;
  font-size: 13px;
  color: var(--text-secondary);
  transition: all var(--transition-fast);
  border-left: 3px solid transparent;
}

.sidebar-item:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-left-color: var(--accent);
}

.sidebar-item:active {
  cursor: grabbing;
}

.node-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: white;
  flex-shrink: 0;
}

/* ============================
   Main Layout
   ============================ */
#main-layout {
  display: flex;
  height: calc(100vh - var(--topbar-height));
}

/* ============================
   Canvas
   ============================ */
#canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-origin: 0 0;
}

#connections-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
  overflow: visible;
  transform-origin: 0 0;
}

/* ============================
   Nodes
   ============================ */
.node {
  position: absolute;
  min-width: 200px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  cursor: move;
  z-index: 10;
  transition: box-shadow var(--transition-fast), border-color var(--transition-fast);
  user-select: none;
}

.node:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--border-color);
  z-index: 15;
}

.node.selected {
  border-color: var(--accent);
  box-shadow: var(--shadow-glow), var(--shadow-md);
  z-index: 20;
}

.node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  font-size: 13px;
  font-weight: 600;
  color: white;
  position: relative;
}

.node-header .node-icon {
  width: 24px;
  height: 24px;
  border-radius: 5px;
  font-size: 12px;
  background: rgba(255, 255, 255, 0.2);
}

.node-body {
  padding: 10px 12px;
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.node-body .node-url {
  color: var(--text-muted);
  font-size: 11px;
  word-break: break-all;
  display: flex;
  align-items: flex-start;
  gap: 4px;
}

.node-body .node-url::before {
  content: '\2197';
  font-size: 10px;
  flex-shrink: 0;
  margin-top: 1px;
  font-style: normal;
}

.node-body .node-notes {
  font-size: 11px;
  color: var(--text-muted);
  font-style: italic;
  margin-top: 2px;
}

.node-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px 12px 8px;
}

.node-tag {
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 10px;
  background: var(--bg-surface);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
}

/* Connection ports — 14px visible + 28px invisible hit area */
.node-port {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--bg-surface);
  border: 2px solid var(--accent);
  cursor: crosshair;
  z-index: 25;
  transition: transform var(--transition-fast), background var(--transition-fast), box-shadow var(--transition-fast);
}

/* Invisible hit area ring (28px) around each 14px port */
.node-port::before {
  content: '';
  position: absolute;
  top: -10px;
  left: -10px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
}

.node-port:hover {
  transform: scale(1.4);
  background: var(--accent);
  box-shadow: 0 0 10px rgba(99, 102, 241, 0.6);
}

.node-port.port-right {
  right: -7px;
  top: 50%;
  transform: translateY(-50%);
}

.node-port.port-right:hover {
  transform: translateY(-50%) scale(1.4);
}

.node-port.port-left {
  left: -7px;
  top: 50%;
  transform: translateY(-50%);
}

.node-port.port-left:hover {
  transform: translateY(-50%) scale(1.4);
}

.node-port.port-top {
  top: -7px;
  left: 50%;
  transform: translateX(-50%);
}

.node-port.port-top:hover {
  transform: translateX(-50%) scale(1.4);
}

.node-port.port-bottom {
  bottom: -7px;
  left: 50%;
  transform: translateX(-50%);
}

.node-port.port-bottom:hover {
  transform: translateX(-50%) scale(1.4);
}

/* Drop-target highlight during connection drag */
.node.drop-target {
  border-color: var(--green) !important;
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.25), 0 0 20px rgba(16, 185, 129, 0.15) !important;
}

/* ============================
   Group Node
   ============================ */
.node.group-node {
  min-width: 320px;
  min-height: 200px;
  background: rgba(30, 32, 53, 0.6);
  border: 2px dashed var(--border-color);
  border-radius: var(--radius-lg);
  z-index: 5;
}

.node.group-node .node-header {
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 8px 14px;
  font-size: 12px;
  background: transparent !important;
  color: var(--text-secondary);
}

/* ============================
   Node Resizing
   ============================ */
.node-resize-handle {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 20px;
  height: 20px;
  cursor: nwse-resize;
  opacity: 0;
  transition: opacity var(--transition-fast);
  z-index: 30;
}

.node:hover .node-resize-handle {
  opacity: 0.6;
}

.node.group-node .node-resize-handle {
  opacity: 0.4;
}

.node.group-node:hover .node-resize-handle {
  opacity: 0.8;
}

.node-resize-handle::after {
  content: '';
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
}

/* ============================
   SVG Connections
   ============================ */
.connection-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-dasharray: 8 4;
  opacity: 0.6;
  transition: opacity var(--transition-fast);
  pointer-events: stroke;
  cursor: pointer;
}

.connection-line:hover {
  opacity: 1;
  stroke-width: 3;
}

.connection-line-hit {
  fill: none;
  stroke: rgba(0, 0, 0, 0.01);
  stroke-width: 24;
  pointer-events: stroke;
  cursor: crosshair;
}

.connection-line-hit:hover+.connection-line {
  stroke-width: 4;
  opacity: 1;
  filter: drop-shadow(0 0 6px var(--accent));
}

.connection-label {
  font-size: 10px;
  fill: var(--text-muted);
  text-anchor: middle;
  pointer-events: none;
}

.temp-connection {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
  stroke-dasharray: 6 4;
  opacity: 0.7;
  pointer-events: none;
  filter: drop-shadow(0 0 4px rgba(99, 102, 241, 0.4));
}

/* ============================
   Connection Mode Indicator
   ============================ */
#connection-mode-indicator {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: white;
  padding: 10px 20px;
  border-radius: var(--radius-lg);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 50;
  animation: slideUp 0.3s ease;
}

#connection-mode-indicator kbd {
  background: rgba(255, 255, 255, 0.2);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* ============================
   Edit Panel
   ============================ */
#edit-panel {
  position: fixed;
  right: 0;
  top: var(--topbar-height);
  width: 320px;
  height: calc(100vh - var(--topbar-height));
  background: var(--bg-secondary);
  border-left: 1px solid var(--border-color);
  z-index: 90;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--transition-normal);
}

#edit-panel.visible {
  transform: translateX(0);
}

.edit-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
}

.edit-panel-header h3 {
  font-size: 14px;
  font-weight: 600;
}

.edit-panel-body {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  flex: 1;
}

.edit-panel-body label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: -6px;
}

.edit-panel-body input,
.edit-panel-body textarea {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  transition: border-color var(--transition-fast);
}

.edit-panel-body input:focus,
.edit-panel-body textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.color-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all var(--transition-fast);
}

.color-swatch:hover {
  transform: scale(1.15);
}

.color-swatch.active {
  border-color: white;
  box-shadow: 0 0 0 2px var(--bg-secondary), 0 0 12px rgba(255, 255, 255, 0.2);
}

.edit-panel-actions {
  margin-top: auto;
  padding-top: 16px;
  border-top: 1px solid var(--border-color);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.15);
  color: var(--red);
  border: 1px solid rgba(239, 68, 68, 0.3);
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  width: 100%;
  transition: all var(--transition-fast);
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.25);
}

/* ============================
   Context Menu
   ============================ */
#context-menu {
  position: fixed;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  z-index: 200;
  padding: 6px;
  min-width: 180px;
  animation: contextPopIn 0.15s ease;
}

@keyframes contextPopIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

#context-menu button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  text-align: left;
  transition: all var(--transition-fast);
}

#context-menu button:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

#context-menu button.danger:hover {
  background: rgba(239, 68, 68, 0.15);
  color: var(--red);
}

#context-menu hr {
  border: none;
  height: 1px;
  background: var(--border-color);
  margin: 4px 0;
}

/* ============================
   Toast Notifications
   ============================ */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 300;
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
}

.toast {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 12px 18px;
  font-size: 13px;
  color: var(--text-primary);
  box-shadow: var(--shadow-lg);
  animation: toastIn 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.toast.success {
  border-left: 3px solid var(--green);
}

.toast.error {
  border-left: 3px solid var(--red);
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes toastOut {
  from {
    opacity: 1;
    transform: translateY(0);
  }

  to {
    opacity: 0;
    transform: translateY(10px);
  }
}

/* ============================
   Utility
   ============================ */
.hidden {
  display: none !important;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Selection highlight */
::selection {
  background: var(--accent);
  color: white;
}

/* Drop zone indicator */
#canvas-container.drag-over::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 2px dashed var(--accent);
  background: rgba(99, 102, 241, 0.05);
  pointer-events: none;
  z-index: 5;
  border-radius: 4px;
}

/* ============================
   Template Modal
   ============================ */
#template-modal,
#projects-modal {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.2s ease;
}

#template-modal.hidden,
#projects-modal.hidden {
  display: none !important;
}

.template-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.template-dialog {
  position: relative;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 28px 32px;
  max-width: 560px;
  width: 90%;
  box-shadow: var(--shadow-lg);
  animation: dialogPop 0.25s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes dialogPop {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(10px);
  }

  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.template-dialog h2 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 4px;
}

.template-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 20px;
}

.template-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 20px;
}

.template-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  color: var(--text-primary);
  transition: all var(--transition-fast);
}

.template-card:hover {
  border-color: var(--accent);
  background: var(--bg-surface);
  box-shadow: var(--shadow-glow);
}

.template-card strong {
  font-size: 13px;
  font-weight: 600;
}

.template-card span {
  font-size: 11px;
  color: var(--text-muted);
}

.template-close {
  margin: 0 auto;
  display: block !important;
}