:root {
  --bg-color: #f3f4f6;
  --text-color: #374151;
  --sidebar-width: 56px;
  --accent-color: #3b82f6;
  --selection-color: #3b82f6;
}

body,
html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg-color);
}

/* Sidebar está em styles-popover.css */

/* Canvas */
#viewport {
  width: 100vw;
  height: 100vh;
  cursor: grab;
  position: relative;
}

#viewport.connection-cursor {
  cursor: crosshair;
}

#viewport:active {
  cursor: grabbing;
}

#board {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
}

#svg-layer,
#handles-layer {
  position: absolute;
  top: -5000px;
  left: -5000px;
  width: 10000px;
  height: 10000px;
  pointer-events: none;
  z-index: 25; /* Acima dos nodes (z-index: 20), abaixo das notas (30) */
}

#handles-layer {
  z-index: 100;
}

#nodes-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 5; /* Abaixo das linhas (z-index: 25), acima do fundo do board */
}

#notes-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 30; /* Acima das linhas (z-index: 25) */
  pointer-events: none; /* Permite clicar através do layer */
}

#notes-layer > * {
  pointer-events: auto; /* Reabilita interação nos filhos */
}

/* Elementos */
.node {
  position: absolute;
  z-index: 20; /* Acima do container (5), mas nodes-layer é z-index 5, então o z-index aqui é relativo ao layer */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: grab;
  user-select: none;
  width: 140px;
  height: 65px; /* Altura fixa igual à do ícone para centralizar no transform */
  /* left/top são setados via JS, translate via transform */
  will-change: transform; /* Otimização para animações */
}

.node.source-node .node-icon {
  border: 3px solid var(--accent-color);
  box-shadow: 0 0 15px var(--accent-color);
}

.node-icon {
  font-size: 35px;
  /* margin-bottom: 8px; */ /* Removido para não deslocar o centro */
  background: white;
  border-radius: 50%;
  width: 65px;
  height: 65px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
  border: 2px solid #eee;
  transition: all 0.2s ease;
  flex-shrink: 0;
  pointer-events: auto;
  cursor: grab !important;
}

.node-icon:active {
  cursor: grabbing !important;
}

.node-icon * {
  pointer-events: none !important;
  cursor: grab !important;
}

.node-icon:active * {
  cursor: grabbing !important;
}

.node-label {
  position: absolute;
  top: 73px; /* 65px do ícone + 8px de margem */
  left: 50%;
  transform: translateX(-50%);
  width: auto;
  min-width: 80px;
  font-size: 11px;
  text-align: center;
  font-weight: 600;
  color: var(--text-color);
  background: rgba(255, 255, 255, 0.7);
  padding: 2px 4px;
  border-radius: 3px;
  white-space: pre-wrap;
  pointer-events: none;
  cursor: inherit;
}

.note {
  position: absolute;
  z-index: 30; /* Acima de todos: nodes (20), shapes (25) */
  padding: 12px;
  border-radius: 4px;
  font-size: 11px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  cursor: grab;
  user-select: none;
  text-align: center;
  line-height: 1.4;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: box-shadow 0.2s ease;
  will-change: transform;
}

.note.source-node {
  border: 2px solid var(--accent-color);
}

/* Estilo de seleção */
.node.selected .node-icon {
  border: 3px solid var(--accent-color);
  box-shadow: 0 0 0 4px rgba(13, 110, 253, 0.2), 0 4px 15px rgba(0, 0, 0, 0.15);
}

.note.selected {
  border: 2px solid var(--accent-color) !important;
  box-shadow: 0 0 0 4px rgba(13, 110, 253, 0.2), 0 4px 15px rgba(0, 0, 0, 0.15);
}

.shape.selected {
  box-shadow: 0 0 0 3px var(--accent-color), 0 4px 15px rgba(0, 0, 0, 0.15);
}

/* Container/Quadro */
.container {
  position: absolute;
  z-index: 5; /* Abaixo das linhas (z-index: 10) e nodes (z-index: 20) */
  background: var(--container-bg, #ffffff);
  border: 2px solid var(--container-border, #9ca3af);
  border-radius: 12px;
  cursor: grab;
  user-select: none;
  will-change: transform;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.container-header {
  padding: 12px 16px;
  font-weight: 600;
  font-size: 13px;
  color: #374151;
  border-bottom: 1px solid var(--container-border, #9ca3af);
  display: flex;
  align-items: center;
  gap: 8px;
}

.container-header i,
.container-header svg {
  pointer-events: none;
}

.container-content {
  padding: 16px;
  min-height: 60px;
}

.container.selected {
  border-color: var(--accent-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2), 0 4px 20px rgba(0, 0, 0, 0.1);
}

.sequence-participant {
  position: absolute;
  z-index: 8;
  width: 160px;
  height: 420px;
  pointer-events: auto;
  user-select: none;
  cursor: grab;
  will-change: transform;
}

.sequence-participant-head {
  min-height: 54px;
  padding: 10px 12px;
  background: var(--sequence-bg, #ffffff);
  border: 2px solid var(--sequence-border, #94a3b8);
  border-radius: 14px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: #334155;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.08);
}

.sequence-participant-line {
  position: absolute;
  top: 54px;
  bottom: 0;
  left: 50%;
  width: 0;
  border-left: 2px dashed rgba(100, 116, 139, 0.75);
  transform: translateX(-50%);
  pointer-events: none;
}

.sequence-participant.selected .sequence-participant-head {
  border-color: var(--accent-color);
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.14), 0 10px 24px rgba(15, 23, 42, 0.12);
}

.sequence-participant.source-node .sequence-participant-head {
  border-color: var(--accent-color);
}

.sequence-activation {
  position: absolute;
  z-index: 18;
  width: 18px;
  height: 120px;
  border: 2px solid #2563eb;
  border-radius: 999px;
  background: #93c5fd;
  box-shadow: 0 12px 18px rgba(37, 99, 235, 0.18);
  cursor: grab;
  user-select: none;
  will-change: transform;
}

.sequence-activation.selected {
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.2), 0 12px 18px rgba(37, 99, 235, 0.18);
}

.sequence-activation.source-node {
  border-color: var(--accent-color);
}

.sequence-activation-label {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 8px);
  transform: translateX(-50%);
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.95);
  color: #334155;
  font-size: 10px;
  font-weight: 600;
  white-space: nowrap;
}

/* Handles de redimensionamento */
.resize-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: white;
  border: 2px solid var(--accent-color);
  border-radius: 50%;
  z-index: 100;
  cursor: pointer;
}

.resize-handle:hover {
  background: var(--accent-color);
  transform: scale(1.2);
}

.resize-handle.se {
  bottom: -6px;
  right: -6px;
  cursor: se-resize;
}

.resize-handle.sw {
  bottom: -6px;
  left: -6px;
  cursor: sw-resize;
}

.resize-handle.ne {
  top: -6px;
  right: -6px;
  cursor: ne-resize;
}

.resize-handle.nw {
  top: -6px;
  left: -6px;
  cursor: nw-resize;
}

.resize-handle.e {
  top: 50%;
  right: -6px;
  transform: translateY(-50%);
  cursor: e-resize;
}

.resize-handle.w {
  top: 50%;
  left: -6px;
  transform: translateY(-50%);
  cursor: w-resize;
}

.resize-handle.n {
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
  cursor: n-resize;
}

.resize-handle.s {
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  cursor: s-resize;
}

/* Formas Geométricas */
.shape {
  position: absolute;
  z-index: 25; /* Acima de nodes (20) */
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  user-select: none;
  transition: box-shadow 0.2s ease;
  will-change: transform;
}

.shape * {
  pointer-events: none !important;
  cursor: inherit !important;
}

.shape-label {
  font-size: 11px;
  text-align: center;
  font-weight: 600;
  z-index: 2;
  padding: 4px;
  pointer-events: none;
}

/* Retângulo */
.shape-rectangle {
  width: 120px;
  height: 80px;
  background: var(--shape-color, #ffffff);
  border: 2px solid var(--shape-text, #374151);
  border-radius: 8px;
  color: var(--shape-text, #374151);
}

/* Círculo */
.shape-circle {
  width: 100px;
  height: 100px;
  background: var(--shape-color, #dbeafe);
  border: 2px solid var(--shape-text, #3b82f6);
  border-radius: 50%;
  color: var(--shape-text, #3b82f6);
}

/* Triângulo usando clip-path */
.shape-triangle {
  width: 120px;
  height: 100px;
  position: relative;
}

.shape-triangle::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 60px solid transparent;
  border-right: 60px solid transparent;
  border-bottom: 100px solid var(--shape-color, #fef3c7);
  filter: drop-shadow(0 0 2px var(--shape-text, #f59e0b));
}

.shape-triangle .shape-label {
  position: relative;
  z-index: 2;
  margin-top: 30px;
  color: var(--shape-text, #f59e0b);
}

/* Losango usando transform */
.shape-diamond {
  width: 100px;
  height: 100px;
  background: var(--shape-color, #fce7f3);
  border: 2px solid var(--shape-text, #ec4899);
  /* O rotate é aplicado via JS no transform */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.shape-diamond .shape-label {
  transform: rotate(-45deg);
  color: var(--shape-text, #ec4899);
  max-width: 80px;
}



/* Efeito ao clicar (pressed) */
.node:active .node-icon {
  transform: scale(0.95);
}

.note:active {
  transform: translate(-50%, -50%) scale(0.98);
}

/* Texto puro - sem fundo, sem borda */
.text-node {
  position: absolute;
  z-index: 30;
  padding: 4px 6px;
  font-size: 13px;
  color: #374151;
  cursor: grab;
  user-select: none;
  text-align: center;
  line-height: 1.5;
  white-space: pre-wrap;
  will-change: transform;
  min-width: 60px;
}

.text-node.selected {
  outline: 2px dashed var(--accent-color);
  outline-offset: 4px;
  border-radius: 3px;
}

.text-node.source-node {
  outline: 2px solid var(--accent-color);
  outline-offset: 4px;
  border-radius: 3px;
}

.text-node:active {
  transform: translate(-50%, -50%) scale(0.98);
}

/* Connection Ports - Pontos de conexão nos nodes */
.connection-port {
  position: absolute;
  width: 8px; /* Reduzido de 14px */
  height: 8px; /* Reduzido de 8px */
  background: var(--accent-color);
  border: 2px solid white; /* Borda mais fina */
  border-radius: 50%;
  transform: translate(-50%, -50%);
  z-index: 200;
  opacity: 0;
  transition: all 0.15s ease;
  pointer-events: auto;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  cursor: crosshair;
}

/* Removido o ::before que aumentava a área de clique */
.connection-port::before {
  display: none;
}

/* Ativar interação apenas em modos específicos */
.connection-mode .connection-port,
.alt-pressed .connection-port {
  pointer-events: auto !important;
}

/* Mostrar ports no hover do node apenas se não estiver arrastando */
body:not(.dragging) .node:hover .connection-port,
body:not(.dragging) .shape:hover .connection-port,
body:not(.dragging) .container:hover .connection-port,
body:not(.dragging) .sequence-participant:hover .connection-port,
body:not(.dragging) .sequence-activation:hover .connection-port,
body:not(.dragging) .note:hover .connection-port,
body:not(.dragging) .text-node:hover .connection-port {
  opacity: 1;
}

/* Hover no port */
.connection-port:hover {
  background: var(--accent-color);
  transform: translate(-50%, -50%) scale(1.4);
  box-shadow: 0 0 0 4px rgba(13, 110, 253, 0.3);
}

/* Port ativo (durante conexão) */
.connection-port.active {
  opacity: 1;
  background: var(--accent-color);
  transform: translate(-50%, -50%) scale(1.4);
}

/* Estilos das Linhas */
.edge-path {
  cursor: pointer;
  pointer-events: auto;
  transition: stroke 0.2s, stroke-width 0.2s;
}

.edge-path:hover {
  stroke: var(--accent-color);
  stroke-width: 4;
}

.edge-path.selected {
  stroke: var(--selection-color);
  stroke-width: 4;
}

.edge-hit-area {
  cursor: pointer;
  pointer-events: auto;
  opacity: 0;
  fill: none;
  stroke: transparent;
  stroke-width: 15;
}

.edge-label-group {
  cursor: pointer;
  pointer-events: auto;
}

.edge-label-bg {
  fill: white;
  opacity: 0.9;
  rx: 3;
  ry: 3;
}

.edge-label {
  font-size: 10px;
  fill: #555;
  font-weight: bold;
  text-anchor: middle;
}

.edge-label.placeholder {
  fill: #ccc;
}

/* Inputs de edição */
.edit-input {
  font-family: inherit;
  font-size: inherit;
  width: 100%;
  border: none;
  border-radius: 2px;
  text-align: center;
  padding: 2px;
  outline: none;
  background: transparent;
}

textarea.edit-input {
  resize: none;
  min-height: 40px;
}

/* Input flutuante para a linha */
.floating-edit {
  position: absolute;
  z-index: 1000;
  transform: translate(-50%, -50%);
  min-width: 120px;
  width: auto;
  min-height: 24px;
  padding: 4px 8px;
  font-size: 12px;
  box-sizing: border-box;
}

#viewport.drag-over {
  background-color: #e9ecef;
}

/* Sidebar principal está em styles-popover.css */

/* Handles de reconexão das edges */
.edge-handle {
  pointer-events: all !important;
  cursor: crosshair;
}

.edge-handle:hover {
  r: 7; /* Reduzido de 12 para 7 */
  stroke-width: 3;
}

/* Menu de Contexto */
.context-menu {
  position: fixed;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  min-width: 180px;
  z-index: 10000;
  padding: 8px 0;
  font-size: 13px;
  border: 1px solid #e9ecef;
}

.context-menu-header {
  padding: 8px 16px;
  font-weight: 600;
  color: #6c757d;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.context-menu-items {
  padding: 0;
}

.context-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background-color 0.15s;
  position: relative;
}

.context-menu-item:hover {
  background-color: #f8f9fa;
}

.context-menu-item.active {
  background-color: #e7f1ff;
  color: #0d6efd;
}

.context-menu-item.active:hover {
  background-color: #d0e3ff;
}

.context-menu-item i {
  width: 16px;
  text-align: center;
  font-size: 14px;
}

.context-menu-item .check-icon {
  margin-left: auto;
  color: #0d6efd;
}

.context-menu-divider {
  height: 1px;
  background-color: #e9ecef;
  margin: 8px 0;
}

.context-menu-item.delete:hover {
  background-color: #fff5f5;
}

.context-menu-section-label {
  padding: 6px 16px 8px;
  font-size: 11px;
  font-weight: 600;
  color: #64748b;
}

.context-menu-color-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  padding: 0 16px 12px;
}

.context-menu-color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  border: 2px solid rgba(148, 163, 184, 0.4);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.85);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
}

.context-menu-color-swatch:hover {
  transform: scale(1.08);
  border-color: #64748b;
}

.context-menu-color-swatch.active {
  border-color: #0d6efd;
  box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.18);
}

.context-menu-color-picker-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px 4px;
}

.context-menu-color-preview {
  width: 18px;
  height: 18px;
  border-radius: 999px;
  border: 1px solid rgba(148, 163, 184, 0.5);
  flex: 0 0 auto;
}

.context-menu-color-value {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  color: #64748b;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.context-menu-picker-trigger {
  border: 1px solid #dbe2ea;
  background: #f8fafc;
  color: #334155;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.context-menu-picker-trigger:hover {
  background: #eef2f7;
}

.pcr-app.diagram-color-pickr {
  z-index: 11001;
}

.active-diagram-badge {
  margin-top: 10px;
  max-width: 40px;
  padding: 6px 4px;
  border-radius: 12px;
  background: #eef6ff;
  color: #334155;
  font-size: 9px;
  line-height: 1.3;
  text-align: center;
  word-break: break-word;
  border: 1px solid #dbeafe;
}

.diagram-picker {
  position: fixed;
  inset: 0;
  z-index: 11000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(circle at top left, rgba(14, 165, 233, 0.2), transparent 35%),
    radial-gradient(circle at bottom right, rgba(16, 185, 129, 0.18), transparent 30%),
    rgba(15, 23, 42, 0.46);
  backdrop-filter: blur(8px);
}

.diagram-picker.visible {
  display: flex;
}

.diagram-picker-panel {
  width: min(760px, 100%);
  max-height: min(86vh, 820px);
  overflow: auto;
  border-radius: 28px;
  padding: 28px;
  background: #f8fafc;
  box-shadow: 0 24px 90px rgba(15, 23, 42, 0.28);
}

.diagram-picker-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.diagram-picker-eyebrow {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #0369a1;
  margin-bottom: 8px;
}

.diagram-picker-header h1 {
  margin: 0 0 8px;
  font-size: clamp(28px, 4vw, 38px);
  line-height: 1;
  color: #0f172a;
}

.diagram-picker-header p {
  margin: 0;
  color: #475569;
  font-size: 14px;
}

.diagram-picker-refresh {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 14px;
  background: #e2e8f0;
  color: #0f172a;
  cursor: pointer;
}

.diagram-picker-create {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  margin-bottom: 16px;
}

.diagram-picker-create input {
  min-width: 0;
  padding: 14px 16px;
  border: 1px solid #cbd5e1;
  border-radius: 14px;
  background: white;
  font-size: 14px;
  outline: none;
}

.diagram-picker-create input:focus {
  border-color: #0ea5e9;
  box-shadow: 0 0 0 4px rgba(14, 165, 233, 0.15);
}

.diagram-picker-create button {
  border: none;
  border-radius: 14px;
  padding: 0 18px;
  background: linear-gradient(135deg, #0284c7, #10b981);
  color: white;
  font-weight: 600;
  cursor: pointer;
}

.diagram-picker-status {
  min-height: 20px;
  margin-bottom: 12px;
  font-size: 13px;
  color: #475569;
}

.diagram-picker-status.error {
  color: #b91c1c;
}

.diagram-picker-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}

.diagram-card {
  border: 1px solid #dbe4ee;
  border-radius: 18px;
  padding: 16px;
  background: white;
  text-align: left;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}

.diagram-card:hover {
  transform: translateY(-2px);
  border-color: #0ea5e9;
  box-shadow: 0 12px 28px rgba(14, 165, 233, 0.12);
}

.diagram-card-title {
  font-size: 16px;
  font-weight: 700;
  color: #0f172a;
  margin-bottom: 8px;
}

.diagram-card-meta {
  color: #64748b;
  font-size: 12px;
  line-height: 1.5;
}

.diagram-picker-empty {
  grid-column: 1 / -1;
  padding: 28px;
  border: 1px dashed #94a3b8;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.7);
  color: #475569;
  text-align: center;
}

@media (max-width: 720px) {
  .diagram-picker {
    padding: 14px;
  }

  .diagram-picker-panel {
    padding: 20px;
    border-radius: 22px;
  }

  .diagram-picker-create {
    grid-template-columns: 1fr;
  }
}
