/* =========================================
   1. VARIABLES & RESET
   ========================================= */
:root {
  /* Colors - Blue Primary Scheme */
  --primary: #3b82f6;
  /* Modern Blue */
  --primary-dark: #2563eb;
  --primary-light: #eff6ff;
  --secondary: #f59e0b;
  /* Amber */

  /* Neutrals */
  --bg-body: #f8fafc;
  --bg-surface: #ffffff;
  --text-main: #1e293b;
  --text-secondary: #64748b;
  --border: #e2e8f0;

  /* Sidebar */
  --sidebar-width: 280px;
  --sidebar-right-width: 300px;

  /* Shadows */
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);

  /* Transitions */
  --transition-fast: 0.2s ease;
  --transition-smooth: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
  background-color: var(--bg-body);
  color: var(--text-main);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* App-like feel */
}

/* =========================================
   2. LAYOUT LAYERS
   ========================================= */

/* Main Layout Wrapper */
.app-container {
  display: flex;
  flex: 1;
  height: 100%;
  /* Fill remaining vertical space */
  position: relative;
  overflow: hidden;
}

/* =========================================
   3. SIDEBAR (TOOLS) - LEFT
   ========================================= */
#sidebar {
  width: var(--sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px;
  overflow-y: auto;
  transition: transform var(--transition-smooth);
  z-index: 20;
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
}

#sidebar.hidden {
  transform: translateX(-100%);
  position: absolute;
  /* Find layout flow */
  height: 100%;
}

/* If absolute positioning when hidden is tricky with flex, 
   we can just use margin-left negative, or handle visibility via a wrapper.
   For now, we will handle the "hidden" state visually. 
*/

/* Category Groups */
.category {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 12px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
  flex-shrink: 0;
  /* Prevent shrinking when sidebar fills up */
}

.category-header {
  padding: 12px 16px;
  background: #f8fafc;
  color: var(--text-main);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background var(--transition-fast);
  user-select: none;
}

.category-header:hover {
  background: #f1f5f9;
}

.category-header span {
  display: flex;
  align-items: center;
  gap: 8px;
}

.category-header i {
  color: var(--text-secondary);
  font-size: 12px;
  transition: transform var(--transition-fast);
}

.category.collapsed .category-content {
  display: none;
}

.category.collapsed .category-header i {
  transform: rotate(-90deg);
}

/* Grid Tools */
.category-content {
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  background: #fff;
}

/* Tool Buttons */
.tool {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: all var(--transition-fast);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  text-align: center;
}

.tool i {
  font-size: 20px;
  color: var(--primary);
  margin-bottom: 4px;
}

.tool:hover {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary-dark);
  transform: translateY(-2px);
  box-shadow: var(--shadow-sm);
}

.tool:active {
  transform: translateY(0);
}

/* Toggle Sidebar Button */
#toggle-sidebar {
  position: absolute;
  top: 40px;
  /* Left position logic based on sidebar state */
  left: calc(var(--sidebar-width) + 12px);
  width: 32px;
  height: 32px;
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  z-index: 100;
  transition: all var(--transition-smooth);
}

#toggle-sidebar:hover {
  color: var(--primary);
  border-color: var(--primary);
}

/* Logic: When sidebar is hidden, move button */
#sidebar.hidden+#toggle-sidebar {
  left: 12px;
}


/* =========================================
   4. MAIN CANVAS AREA
   ========================================= */
#editor-area {
  flex: 1;
  position: relative;
  background: #f1f5f9;
  overflow: hidden;
  /* Scroll handled by canvas container */
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#editor-canvas-container {
  flex: 1;
  overflow: auto;
  position: relative;
  background-color: #f8fafc;
  background-image:
    radial-gradient(#cbd5e1 1px, transparent 1px);
  background-size: 20px 20px;
}

/* SVG Canvas */
#grafcet-canvas {
  background: transparent;
  /* Transparent so container grid shows through */
  margin: 0;
  display: block;
  overflow: visible;
  /* Use default large size from HTML or 100% if dynamic, 
     but user wants it to occupy area. 
     If we want the 'infinite' feel, we keep the large size but remove the border look. */
}

/* Help Button (Floating) */
#help-btn {
  /* Restyled existing inline styles */
  position: absolute;
  bottom: 24px;
  right: 24px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  border: none;
  font-size: 20px;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  transition: transform var(--transition-fast);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
}

#help-btn:hover {
  transform: scale(1.1);
  background: var(--primary-dark);
}

/* =========================================
   5. SIDEBAR RIGHT (VARIABLES)
   ========================================= */
#sidebar1 {
  width: var(--sidebar-right-width);
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px;
  overflow-y: auto;
  flex-shrink: 0;
  position: relative;
  transition: transform var(--transition-smooth);
  z-index: 20;
  box-shadow: -2px 0 5px rgba(0, 0, 0, 0.05);
}

#sidebar1.hidden {
  transform: translateX(100%);
  position: absolute;
  right: 0;
  height: 100%;
}

/* Toggle Right Sidebar Button */
#toggle-sidebar-right {
  position: absolute;
  top: 40px;
  right: calc(var(--sidebar-right-width) + 12px);
  width: 32px;
  height: 32px;
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  z-index: 100;
  transition: all var(--transition-smooth);
}

#toggle-sidebar-right:hover {
  color: var(--primary);
  border-color: var(--primary);
}

#sidebar1.hidden+#toggle-sidebar-right {
  right: 12px;
}

#sidebar1 h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-main);
  margin: 0 0 16px 0;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

/* Upload Button Style */
label[for="csv-input"] {
  display: block;
  text-align: center;
  background: #10b981;
  /* Success Green */
  color: white;
  padding: 10px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  font-size: 14px;
  transition: background var(--transition-fast);
  margin-bottom: 16px;
}

label[for="csv-input"]:hover {
  background: #059669;
}

/* Variable List */
#variable-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.variable-item {
  padding: 10px 12px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-main);
  cursor: grab;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.variable-item:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.variable-item:active {
  cursor: grabbing;
}

/* Simulation Active States */
.variable-item.active-sim-input {
  background-color: #3b82f6 !important;
  /* Blue Primary */
  color: white !important;
  border-color: #2563eb !important;
}

.variable-item.active-sim-input .var-status-icon {
  color: white !important;
}

.variable-item.active-sim-output {
  background-color: #f59e0b !important;
  /* Amber */
  color: white !important;
  border-color: #d97706 !important;
}

.variable-item.active-sim-output .var-status-icon {
  color: white !important;
}

/* Fallback/Generic */
.variable-item.active-sim {
  background-color: #3b82f6 !important;
  color: white !important;
}


/* =========================================
   6. FOOTER
   ========================================= */
footer {
  height: 48px;
  background: #fff;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  color: var(--text-secondary);
  gap: 20px;
  z-index: 30;
}

footer a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
}

footer a:hover {
  text-decoration: underline;
}

/* =========================================
   7. MODALS & UTILITIES
   ========================================= */

/* Generic Modal */
.modal-overlay {
  display: none;
  /* Toggled via JS style.display */
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(4px);
  z-index: 500;
  align-items: center;
  justify-content: center;
}

.modal-overlay.active {
  display: flex !important;
}

.modal-content {
  background: #ffffff;
  padding: 32px;
  border-radius: 16px;
  max-width: 500px;
  width: 90%;
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
  position: relative;
  border: 1px solid var(--border);
}

.modal-close-btn {
  position: absolute;
  top: 14px;
  right: 14px;
  background: white;
  border: 1px solid var(--border);
  color: #64748b;
  cursor: pointer;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
  z-index: 10;
  box-shadow: var(--shadow-sm);
  font-size: 16px;
}

.modal-close-btn:hover {
  background: #f1f5f9;
  color: #ef4444;
  border-color: #ef4444;
  transform: rotate(90deg);
}

.modal-header {
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h2,
.modal-header h3 {
  margin: 0;
  color: var(--primary);
  font-size: 1.25rem;
  font-weight: 700;
}

.modal-title {
  margin-top: 0;
  color: var(--primary);
  font-size: 20px;
  margin-bottom: 16px;
}

/* Line Controls (Floating Panel) */
#line-controls {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  padding: 12px 20px;
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  display: flex;
  gap: 16px;
  align-items: center;
  z-index: 40;
}

#line-controls h4 {
  margin: 0;
  font-size: 14px;
  color: var(--text-main);
  margin-right: 8px;
}

#line-controls button {
  background: #f1f5f9;
  border: 1px solid var(--border);
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 4px;
  cursor: pointer;
}

#line-controls button:hover {
  background: #e2e8f0;
}

/* Text Editor Modal */
#text-editor-modal,
#text-style-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  width: 300px;
  border: 1px solid var(--border);
  display: none;
  /* toggled by JS */
  flex-direction: column;
  gap: 12px;
}

#text-editor-modal.active,
#text-style-modal.active {
  display: flex;
}

#text-editor-modal h3,
#text-style-modal h3 {
  margin: 0 0 8px 0;
  color: var(--text-main);
}

#text-editor-modal input,
#text-style-modal input,
#text-style-modal select {
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 14px;
  width: 100%;
}

#text-editor-modal button,
#text-style-modal button {
  padding: 10px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
}

#text-editor-modal button.cancel,
#text-style-modal button.cancel {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

/* Message Box */
#message-box {
  position: fixed;
  bottom: 20px;
  right: 20px;
  left: auto;
  transform: none;
  background: #1e293b;
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 2000;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  max-width: 300px;
}

#message-box.show {
  opacity: 1;
  transform: translateY(-10px);
}

#message-box.warning {
  background: #ef4444;
  /* Red */
  color: white;
}




/* --- SIMULATION LOCK OVERLAY --- */
#simulation-overlay {
  display: none !important;
  /* El overlay visual bloqueaba el scroll y la interacción en diagramas grandes.
     Se ha desactivado para permitir "ver como en modo libre". */
}

/* Bloquear edición pero permitir Inputs en Simulación */
body.simulation-active .svg-element {
  pointer-events: none;
}

body.simulation-active .svg-element[data-type="input"] {
  pointer-events: all !important;
  cursor: pointer !important;
}

/* Permitir interacción con botones específicos si los hay */
body.simulation-active .sim-interactive {
  pointer-events: all !important;
  cursor: pointer !important;
}

/* =========================================
   8. GRAFCET ELEMENTS (SVG)
   ========================================= */

.svg-element {
  cursor: pointer;
  transition: filter 0.2s;
}

.svg-element:hover {
  filter: drop-shadow(0 0 2px rgba(59, 130, 246, 0.5));
}

/* Common Text Style */
.svg-text,
.svg-action-text,
.svg-condition-text {
  font-family: 'Arial', sans-serif;
  font-size: 14px;
  fill: #1e293b;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  /* Let clicks pass to the shape */
  user-select: none;
}

/* Custom Text necesita recibir eventos porque NO TIENE rect de fondo */
text.svg-custom-text {
  font-family: 'Arial', sans-serif;
  font-size: 14px;
  fill: #1e293b;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: visiblePainted !important;
  /* IMPORTANTE: Para que detecte clicks */
  user-select: none;
  cursor: grab !important;
}

/* STAGES (Etapas) */
.svg-stage rect {
  fill: #ffffff;
  stroke: #1e293b;
  stroke-width: 2px;
}

.svg-stage.initial rect {
  stroke-width: 4px;
  /* Double border effect simulated or just thicker */
  /* Alternatively for true double border we need two rects, but thickness works for now */
}

.svg-macro-stage rect {
  /* Macro representation is now handled by inner lines in elements.js */
  fill: #f8fafc;
  /* Ligeramente diferente para ayudar visualmente */
}


/* ACTIONS */
.svg-action rect {
  fill: #ffffff;
  stroke: #1e293b;
  stroke-width: 2px;
}

/* TRANSITIONS & CONNECTIONS */
.svg-transition-line,
.svg-transition-shape,
.svg-connection-path {
  stroke: #1e293b;
  stroke-width: 2px;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
  pointer-events: stroke;
  /* Facilitar clic en la línea */
}

.svg-connection-path:hover {
  stroke: var(--primary);
  stroke-width: 3px;
  filter: drop-shadow(0 0 4px rgba(59, 130, 246, 0.3));
}

.svg-connection-path.selected {
  stroke: #ef4444;
  /* Rojo al seleccionar */
  stroke-width: 3px;
  filter: drop-shadow(0 0 2px rgba(239, 68, 68, 0.5));
}


.svg-condition-text {
  text-anchor: start;
  /* Text next to the dash */
  font-style: normal;
}

/* LINES */
.svg-line {
  stroke: #1e293b;
  stroke-width: 2px;
  stroke-linecap: round;
  fill: none;
}

.svg-double-line .svg-line-part {
  stroke: #1e293b;
  stroke-width: 2px;
}

/* SELECTION STATES */
.selected rect,
.selected .svg-line-part {
  stroke: var(--primary) !important;
  stroke-width: 3px !important;
  filter: drop-shadow(0 0 4px var(--primary-light));
}

/* Specific red highlighting for Line elements when selected, similar to connection paths */
.svg-line.selected,
.selected line.svg-line {
  stroke: #ef4444 !important;
  /* Rojo */
  stroke-width: 3px !important;
  filter: drop-shadow(0 0 2px rgba(239, 68, 68, 0.5));
}

.selected text {
  fill: var(--primary) !important;
  font-weight: bold;
}

/* Connecting Mode Feedback */
.connect-target {
  stroke: #10b981 !important;
  /* Green */
  stroke-width: 3px !important;
  stroke-dasharray: 4;
}

/* Draw Line Mode */
.svg-free-polyline {
  fill: none;
  stroke: #1e293b;
  stroke-width: 2px;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.svg-arrow line {
  stroke: #1e293b;
  stroke-width: 2px;
}

.svg-arrow polygon {
  fill: #1e293b;
}

/* Port Styling */
.port {
  r: 5px;
  fill: #ffffff;
  stroke: #3b82f6;
  stroke-width: 2px;
  opacity: 0;
  /* Hidden by default */
  cursor: crosshair;
  transition: all 0.2s ease;
}

.svg-element:hover .port {
  opacity: 1;
  /* Show on hover */
}

.port:hover {
  r: 7px;
  fill: #3b82f6;
  /* Blue fill on active hover */
  stroke: #1e40af;
}

/* Temp Connection Line */
.temp-connection-path {
  stroke: #3b82f6;
  stroke-width: 2px;
  stroke-dasharray: 5, 5;
  fill: none;
  pointer-events: none;
}

/* Alignment Guides */
.alignment-guide {
  stroke: #ff0000;
  stroke-width: 1px;
  stroke-dasharray: 4, 4;
  pointer-events: none;
  opacity: 0.8;
}

/* =========================================
   9. PRINT STYLES
   ========================================= */
@media print {
  @page {
    size: auto;
    margin: 0mm;
  }

  body {
    background: white;
    height: auto;
    overflow: visible;
  }

  /* =========================================
   10. CONDITION EDITOR STYLES
   ========================================= */

  #condition-editor-modal .modal {
    width: 600px;
    /* Más ancho para código */
    max-width: 95%;
  }

  #condition-preview {
    background: #1e293b;
    /* Dark theme for code */
    color: #e2e8f0;
    border: 1px solid #334155;
    font-family: 'Consolas', 'Monaco', monospace;
    padding: 12px;
    border-radius: 6px;
    min-height: 48px;
    margin-bottom: 16px;
    white-space: pre-wrap;
    word-break: break-all;
    display: flex;
    align-items: center;
    box-shadow: inset 0 2px 4px 0 rgb(0 0 0 / 0.05);
  }

  /* Syntax Highlighting in Preview */
  #condition-preview span {
    display: inline-block;
    margin: 0 1px;
  }

  #condition-input {
    background: #ffffff;
    border: 1px solid var(--border);
    color: var(--text-main);
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 14px;
    border-radius: 6px;
    padding: 12px;
    margin-bottom: 16px !important;
    /* Override inline styles if any */
    resize: vertical;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  }

  #condition-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
    outline: none;
  }

  /* Syntax Buttons Container */
  .syntax-toolbar {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 24px;
    padding: 12px;
    background: #f8fafc;
    border-radius: 8px;
    border: 1px solid var(--border);
  }

  /* Syntax Buttons */
  .syntax-btn {
    background: #ffffff;
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    font-family: 'Consolas', monospace;
    cursor: pointer;
    transition: all 0.1s ease;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  }

  .syntax-btn:hover {
    background: var(--primary-light);
    border-color: var(--primary);
    color: var(--primary-dark);
    transform: translateY(-1px);
  }

  .syntax-btn:active {
    transform: translateY(0);
  }

  /* Categorized Syntax Colors (optional hint borders) */
  .syntax-btn:nth-child(-n+3) {
    /* AND, OR, NOT */
    border-bottom: 2px solid #d946ef;
  }

  .syntax-btn:nth-child(n+6) {
    /* Edges */
    border-bottom: 2px solid #f59e0b;
  }

  /* Validation Message */
  .validation-msg {
    background: #fef2f2;
    color: #b91c1c;
    padding: 10px;
    border-radius: 6px;
    font-size: 13px;
    border: 1px solid #fecaca;
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .validation-msg::before {
    content: '\f071';
    /* FontAwesome Warning */
    font-family: 'Font Awesome 6 Free';
    font-weight: 900;
  }

  footer,
  #toggle-sidebar,
  #help-btn,
  #message-box,
  #line-controls,
  .modal-overlay,
  #text-editor-modal,
  #text-style-modal {
    display: none !important;
  }

  /* Adjust Layout */
  .app-container {
    display: block !important;
    position: relative;
    width: 100%;
    height: auto;
    overflow: visible;
  }

  #editor-area {
    display: block !important;
    position: relative;
    width: 100%;
    height: auto;
    background: white;
    overflow: visible;
  }

  #editor-canvas-container {
    background: white !important;
    background-image: none !important;
    /* No grid dots */
    overflow: visible !important;
  }

  #grafcet-canvas {
    transform: none !important;
    /* Reset zoom */
    display: block;
    margin: 0 auto;
  }

  /* Hide UI elements inside canvas */
  .port,
  .alignment-guide,
  .temp-connection-path {
    display: none !important;
  }
}

/* Active Tool Mode */
.tool.active-mode {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary-dark);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
  transform: translateY(1px);
}

/* --- PUERTOS DE CONEXIÓN --- */
.port {
  r: 6px;
  fill: transparent;
  stroke: transparent;
  cursor: crosshair;
  transition: all 0.2s ease;
  pointer-events: all;
  /* Asegura detección por el ratón */
}

.svg-element:hover .port,
.port:hover {
  fill: rgba(59, 130, 246, 0.5);
  /* Azul semitransparente */
  stroke: #3b82f6;
  stroke-width: 2px;
}


/* Arrow Element */
.svg-arrow {
  cursor: move;
}

.svg-arrow-shape {
  transition: stroke 0.2s;
}

.svg-arrow:hover .svg-arrow-shape {
  stroke: var(--primary);
  stroke-width: 3;
}


.svg-arrow.selected .svg-arrow-shape {
  stroke: var(--primary);
  stroke-width: 3;
  filter: drop-shadow(0 0 4px var(--primary-light));
}

/* Resize Handle */
.resize-handle {
  display: none;
  fill: var(--primary);
  stroke: white;
  stroke-width: 1.5px;
  cursor: pointer;
  z-index: 10;
}

.selected .resize-handle {
  display: block;
}

/* Polyline Vertex Handle */
.polyline-handle {
  display: none;
  fill: var(--primary);
  /* Blue (Primary) */
  stroke: white;
  stroke-width: 1.5px;
  cursor: grab;
  /* Indicates separate movability */
  z-index: 10;
}

.selected .polyline-handle {
  display: block;
}

/* =========================================
   10. CONDITION EDITOR STYLES - MODERN
   ========================================= */

/* Modal Width */
#condition-editor-modal .modal {
  width: 600px;
  max-width: 95%;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
}

/* Preview Area */
#condition-preview {
  background: #1e293b;
  color: #e2e8f0;
  border: 1px solid #334155;
  font-family: 'Consolas', 'Monaco', monospace;
  padding: 16px;
  border-radius: 8px;
  min-height: 56px;
  margin-bottom: 20px;
  white-space: pre-wrap;
  display: flex;
  align-items: center;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
  font-size: 14px;
}

/* Syntax Highlighting */
#condition-preview span {
  display: inline-block;
  margin: 0 1px;
}

/* Input Area */
#condition-input {
  background: #ffffff;
  border: 1px solid var(--border);
  color: var(--text-main);
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 14px;
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 20px !important;
  resize: vertical;
  transition: all var(--transition-fast);
}

#condition-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
  outline: none;
}

/* Toolbar Container */
.syntax-toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 24px;
  padding: 16px;
  background: #f8fafc;
  border-radius: 12px;
  border: 1px solid var(--border);
}

/* Modern Buttons (Matching Sidebar .tool) */
.syntax-btn {
  background: #ffffff;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 8px 16px;
  border-radius: 6px;
  /* Matching .tool */
  font-size: 13px;
  font-weight: 500;
  font-family: 'Inter', system-ui, sans-serif;
  cursor: pointer;
  transition: all var(--transition-fast);
  box-shadow: var(--shadow-sm);
  min-width: 48px;
  text-align: center;
}

.syntax-btn:hover {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary-dark);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.syntax-btn:active {
  transform: translateY(0);
}

/* Action Buttons (Footer) */
#condition-editor-modal button:not(.syntax-btn):not(.modal-close-btn) {
  padding: 10px 20px;
  border-radius: 6px;
  font-weight: 500;
  font-size: 14px;
  cursor: pointer;
  transition: all var(--transition-fast);
}

/* Save Button (Primary) */
#condition-editor-modal button[onclick*="saveCondition"] {
  background: var(--primary);
  color: white;
  border: 1px solid var(--primary);
  box-shadow: var(--shadow-sm);
}

#condition-editor-modal button[onclick*="saveCondition"]:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

/* Cancel Button (Secondary) */
#condition-editor-modal button.cancel {
  background: white;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

#condition-editor-modal button.cancel:hover {
  background: #f1f5f9;
  color: var(--text-main);
  border-color: #cbd5e1;
}

/* Validation Msg */
.validation-msg {
  background: #fef2f2;
  color: #b91c1c;
  padding: 12px;
  border-radius: 8px;
  font-size: 13px;
  border: 1px solid #fecaca;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
}

/* --- CAJA DE SELECCIÓN (Rubberband) --- */
.selection-box {
  fill: rgba(0, 120, 215, 0.1);
  stroke: #0078d7;
  stroke-width: 1;
  stroke-dasharray: 4;
  pointer-events: none;
  vector-effect: non-scaling-stroke;
}

/* --- ESTILOS DE SELECCIÓN --- */
/* Elementos Generales (Etapas, Acciones, Transiciones) */
.svg-element.selected rect,
.svg-element.selected path,
.svg-element.selected circle,
.svg-element.multi-selected rect,
.svg-element.multi-selected path,
.svg-element.multi-selected circle {
  stroke: var(--primary);
  stroke-width: 2px;
  filter: drop-shadow(0 0 4px var(--primary-light));
}

/* Líneas */
.svg-line-group.selected line,
.svg-line-group.multi-selected line {
  stroke: var(--primary);
  stroke-width: 3px;
  filter: drop-shadow(0 0 3px var(--primary-light));
}

.svg-polyline-group.selected polyline,
.svg-polyline-group.multi-selected polyline {
  stroke: var(--primary);
  filter: drop-shadow(0 0 3px var(--primary-light));
}

/* Custom Text */
text.svg-custom-text.selected,
text.svg-custom-text.multi-selected {
  fill: var(--primary);
  font-weight: bold;
  filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.2));
}

/* --- ACCIONES CONDICIONALES --- */
.svg-action-condition-line {
  stroke: #1e293b;
  stroke-width: 2px;
  pointer-events: none;
}

.svg-action-condition-text {
  font-size: 12px;
  fill: #1e293b;
  text-anchor: middle;
  pointer-events: visiblePainted;
  /* Permitir click para editar */
  cursor: text;
}

.action-condition-group {
  overflow: visible;
}

/* --- CONNECTION PORTS --- */
.port {
  fill: #ffffff;
  stroke: #94a3b8;
  /* Slate 400 */
  stroke-width: 1px;
  opacity: 0;
  /* Ocultos por defecto para limpieza visual */
  transition: all 0.2s;
  cursor: crosshair;
}

.svg-element:hover .port,
.port:hover,
.connecting .port {
  /* Si existe clase connecting en body o canvas */
  opacity: 1;
  r: 5px;
  /* Un poco más grande al interactuar */
}

.port:hover {
  fill: var(--primary);
  stroke: var(--primary-dark);
  stroke-width: 2px;
}

/* --- POLYLINE HANDLES --- */
.polyline-handle {
  fill: #ffffff;
  stroke: #64748b;
  stroke-width: 1px;
  cursor: move;
  opacity: 0;
}

.svg-element.selected .polyline-handle,
.svg-element:hover .polyline-handle {
  opacity: 1;
}

.polyline-handle:hover {
  fill: var(--primary);
  r: 6px;
}

/* --- CSV Variable List Styles --- */
#variable-list {
  list-style: none;
  padding: 10px;
  margin: 0;
}

#variable-list li {
  background-color: #f8fafc;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  padding: 8px 12px;
  margin-bottom: 6px;
  cursor: grab;
  transition: all 0.2s ease;
  font-size: 14px;
  color: #334155;
  display: flex;
  align-items: center;
}

#variable-list li:hover {
  background-color: #e2e8f0;
  border-color: #94a3b8;
  transform: translateX(2px);
}

#variable-list li:active {
  cursor: grabbing;
}

#variable-list li i {
  margin-right: 8px;
  color: #64748b;
}

.drag-over {
  outline: 2px dashed #0ea5e9;
  outline-offset: 2px;
}

/* =========================================
   10. SIMULATION STYLES
   ========================================= */
.sim-btn {
  background: #f8fafc;
  border: 1px solid var(--border);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all 0.2s ease;
  box-shadow: var(--shadow-sm);
}

.sim-btn:hover {
  background: white;
  transform: scale(1.1);
  box-shadow: var(--shadow-md);
  border-color: var(--primary);
}

.sim-btn:active {
  transform: scale(0.95);
}

.variable-item.active-sim {
  background: #10b98122 !important;
  border-color: #10b981 !important;
  font-weight: 700;
  box-shadow: inset 0 0 5px rgba(16, 185, 129, 0.1);
}

.active-simulation-step rect {
  transition: stroke 0.3s ease, filter 0.3s ease, stroke-width 0.3s ease, fill 0.3s ease;
  fill: #fffbeb !important;
  /* Pale Amber/Yellow */
  stroke: #f59e0b !important;
  /* Amber stroke to match fired transition */
  stroke-width: 3px !important;
  filter: drop-shadow(0 0 6px rgba(245, 158, 11, 0.5));
}

body.simulation-active #editor-canvas-container {
  outline: 3px solid #10b981;
  outline-offset: -3px;
}

/* --- New Simulation Highlights --- */

/* Active simulation transition (Fired) */
.active-sim-transition line,
.active-sim-transition path,
.active-sim-transition .svg-transition-shape {
  stroke: #f59e0b !important;
  /* Amber */
  stroke-width: 4px !important;
  filter: drop-shadow(0 0 8px rgba(245, 158, 11, 0.8));
}

/* Active simulation action box */
.active-sim-action rect {
  stroke: #10b981 !important;
  /* Green */
  stroke-width: 3px !important;
  fill: #f0fdf4 !important;
  /* Very light green */
  filter: drop-shadow(0 0 5px rgba(16, 185, 129, 0.4));
}

/* Variable highlight refinements */
.variable-item.active-sim-input {
  background: rgba(59, 130, 246, 0.1) !important;
  /* Blue for Input */
  border-left: 3px solid #3b82f6 !important;
}

.variable-item.active-sim-output {
  background: rgba(16, 185, 129, 0.1) !important;
  /* Green for Output */
  border-left: 3px solid #10b981 !important;
}

body.simulation-active .variable-item {
  cursor: pointer !important;
}

body.simulation-active #variable-list .variable-item:hover {
  background: #f8fafc;
  border-color: #10b981;
}

/* --- Real-time Sensor Feedback --- */

/* Transition cross (line) when its condition is TRUE */
.sensor-true-sim .svg-transition-shape,
.sensor-true-sim line {
  stroke: #3b82f6 !important;
  /* Blue for "Condition True" */
  stroke-width: 3px !important;
  filter: drop-shadow(0 0 5px rgba(59, 130, 246, 0.6));
}

/* Stage when it's active AND its successor is ready to fire */
.exit-ready-sim rect {
  stroke: #10b981 !important;
  stroke-width: 6px !important;
  filter: drop-shadow(0 0 12px rgba(16, 185, 129, 0.8));
  animation: pulse-exit-ready 1.5s infinite;
}

@keyframes pulse-exit-ready {
  0% {
    filter: drop-shadow(0 0 8px rgba(16, 185, 129, 0.6));
  }

  50% {
    filter: drop-shadow(0 0 15px rgba(16, 185, 129, 1));
  }

  100% {
    filter: drop-shadow(0 0 8px rgba(16, 185, 129, 0.6));
  }
}

/* =========================================
   9. PRINT MEDIA QUERY (Professional Output)
   ========================================= */
@media print {

  /* Hide UI Chrome */
  header,
  #sidebar,
  #sidebar1,
  #toggle-sidebar,
  #toggle-sidebar-right,
  #simulation-toolbar,
  #help-btn,
  #message-box,
  #global-announcement-banner,
  .modal-overlay,
  #user-section-header,
  footer,
  /* Hide scrollbars if any */
  ::-webkit-scrollbar {
    display: none !important;
  }

  /* Reset Body */
  body {
    background: white !important;
    overflow: visible !important;
    height: auto !important;
    width: 100% !important;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  .app-container {
    display: block !important;
    height: auto !important;
    width: 100% !important;
  }

  /* Maximize Drawing Area */
  #editor-area {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    background: white !important;
    z-index: 9999;
    display: block !important;
  }

  #editor-canvas-container {
    background: white !important;
    /* Remove grid dots */
    background-image: none !important;
    overflow: visible !important;
    position: static !important;
    display: block !important;
    height: auto !important;
  }

  /* Canvas Optimization */
  #grafcet-canvas {
    margin: 0 !important;
    /* Ensure vector quality */
    shape-rendering: geometricPrecision;
  }

  /* Ensure texts are black */
  text {
    fill: black !important;
  }

  /* Ensure lines are crisp black */
  path,
  line,
  rect {
    stroke: black !important;
  }

  /* Hide Hit Rects in Parallel Nodes (Avoid ugly box around lines) */
  .svg-parallel rect {
    stroke: none !important;
    display: none !important;
  }

  /* Stages white background */
  .svg-stage rect,
  .svg-action rect {
    fill: white !important;
  }

  /* Remove shadow effects for cleaner CAD look */
  .svg-element:hover {
    filter: none !important;
  }

  #tabs-container,
  #add-tab-btn {
    display: none !important;
  }

  /* Print Header (Visible only in print) */
  #print-header {
    display: block !important;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    text-align: left;
    font-size: 10px;
    color: #64748b;
    border-bottom: 1px solid #cbd5e1;
    padding-bottom: 5px;
    margin-bottom: 10px;
    font-family: 'Arial', sans-serif;
    z-index: 10000;
  }
}

/* Default state for print header: Hidden */
#print-header {
  display: none;
}

/* Simulator Credit Restriction Style */
.btn-disabled-credits {
  filter: grayscale(1);
  opacity: 0.6;
  cursor: not-allowed;
  background-color: #e2e8f0;
  /* Force light gray bg */
}

/* Port Magnet Highlight */
.port.port-magnet-highlight {
  fill: #10b981 !important;
  /* Green success - overrides invisible/gray */
  r: 8px !important;
  /* Scale up slightly more than 4px default */
  stroke: #ffffff;
  stroke-width: 2px;
  opacity: 1 !important;
  /* Ensure visibility */
  transition: all 0.1s ease;
  filter: drop-shadow(0 0 4px rgba(16, 185, 129, 0.6));
}


/* --- COOKIE BANNER --- */
.cookie-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: rgba(15, 23, 42, 0.95);
  color: white;
  padding: 15px 20px;
  z-index: 9999;
  display: none;
  /* Hidden by default */
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 -4px 6px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(4px);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  animation: slideUp 0.5s ease-out;
}

.cookie-banner.visible {
  display: flex;
}

.cookie-content {
  flex: 1;
  margin-right: 20px;
  font-size: 0.9rem;
  line-height: 1.5;
}

.cookie-content a {
  color: #3b82f6;
  text-decoration: underline;
}

.cookie-actions {
  display: flex;
  gap: 10px;
}

.cookie-btn-accept {
  background: #10b981;
  color: white;
  border: none;
  padding: 8px 24px;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.cookie-btn-accept:hover {
  background: #059669;
}

.cookie-btn-cancel {
  background: transparent;
  color: #cbd5e1;
  border: 1px solid #475569;
  padding: 8px 24px;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.cookie-btn-cancel:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border-color: #cbd5e1;
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}