:root {
  --start_side: 50px;
  --active_side: 56px;
  --box_side: 100px;
  --inner-rect-top: 10px;
  --connector-side: 6px;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: #f8f8f8;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ==========================================================================
   Barra Superior de Ferramentas (Top Toolbar - Padrão Ferramentas de CLP)
   ========================================================================== */
#top-bar {
  height: 48px;
  background: #1e293b; /* Tom escuro industrial sofisticado */
  color: #f8fafc;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid #334155;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
  z-index: 1000;
  user-select: none;
  box-sizing: border-box;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.5px;
  color: #f8fafc;
}

.brand-icon {
  font-size: 18px;
  color: #38bdf8;
}

.project-tag {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #334155;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 11.5px;
  font-weight: 500;
  color: #cbd5e1;
  border: 1px solid rgba(255, 255, 255, 0.08);
  max-width: 250px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.project-tag .file-status-icon {
  font-size: 12px;
}

#current-project-label {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.toolbar-divider {
  width: 1px;
  height: 22px;
  background: #475569;
  margin: 0 4px;
}

.top-btn {
  height: 32px;
  padding: 0 12px;
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 5px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  box-sizing: border-box;
  text-align: center;
  letter-spacing: 0.3px;
  transition: all 0.15s ease-in-out;
}

.top-btn:hover {
  filter: brightness(1.12);
  transform: translateY(-1px);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.top-btn:active {
  transform: translateY(0);
  filter: brightness(0.95);
}

.btn-icon {
  font-size: 13px;
  display: inline-flex;
  align-items: center;
}

.btn-new {
  background: #475569;
  color: #ffffff;
  border-color: #64748b;
}
.btn-new:hover {
  background: #334155;
}

.btn-open {
  background: #0f766e;
  color: #ffffff;
  border-color: #14b8a6;
}
.btn-open:hover {
  background: #115e59;
}

.btn-save {
  background: #0284c7;
  color: #ffffff;
  border-color: #0ea5e9;
}
.btn-save:hover {
  background: #0369a1;
}

.btn-compile {
  background: #2563eb;
  color: #ffffff;
  border-color: #3b82f6;
  font-weight: 700;
  box-shadow: 0 1px 3px rgba(37, 99, 235, 0.35);
}
.btn-compile:hover {
  background: #1d4ed8;
  border-color: #60a5fa;
}

/* Mantendo compatibilidade com classes anteriores */
.btn-export {
  background: #0284c7;
  color: #ffffff;
  border-color: #0ea5e9;
}
.btn-import {
  background: #0f766e;
  color: #ffffff;
  border-color: #14b8a6;
}
.btn-clear {
  background: #dc2626;
  color: #ffffff;
  border-color: #ef4444;
}

/* Toast notification flutuante */
.toast-notification {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #0f172a;
  color: #f8fafc;
  border: 1px solid #334155;
  border-left: 4px solid #10b981;
  padding: 10px 18px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3);
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.toast-notification.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}

/* ==========================================================================
   Área de Trabalho Principal (Paleta Lateral + Canvas Viewport)
   ========================================================================== */
#app-workspace {
  display: flex;
  flex: 1;
  height: calc(100vh - 48px);
  overflow: hidden;
  position: relative;
}

#palette {
  width: 140px;
  background: #f1f5f9;
  padding: 12px 10px;
  border-right: 1px solid #cbd5e1;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-sizing: border-box;
  overflow-y: auto;
  overflow-x: hidden;
  height: 100%;
}

/* Scrollbar fina e moderna para a paleta lateral */
#palette::-webkit-scrollbar {
  width: 5px;
}
#palette::-webkit-scrollbar-track {
  background: transparent;
}
#palette::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 4px;
}
#palette::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* Estilos de compatibilidade para palette-btn se referenciados */
.palette-btn {
  width: 100%;
  height: 36px;
  margin-top: 6px;
  padding: 0 4px;
  font-size: 10.5px;
  font-weight: bold;
  white-space: nowrap;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  text-align: center;
  transition: opacity 0.2s, background-color 0.2s;
}

.box {
  width: var(--box_side);
  height: var(--box_side);
  background: #fff;
  position: relative;
  cursor: pointer;
  margin-bottom: 10px;
}

/* Destaque de Seleção Múltipla */
.box.selected {
  outline: 2.5px solid #2563eb !important;
  outline-offset: 3px !important;
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.25) !important;
  z-index: 10 !important;
  cursor: grab;
}

.box.selected:active {
  cursor: grabbing;
}

/* Caixa Elástica de Seleção (Marquee) */
#selection-marquee {
  position: absolute;
  border: 1.5px dashed #2563eb;
  background-color: rgba(37, 99, 235, 0.14);
  pointer-events: none;
  z-index: 999;
  border-radius: 2px;
}

body.space-pressed #canvas-viewport {
  cursor: grab !important;
}

body.space-pressed #canvas-viewport:active {
  cursor: grabbing !important;
}

.inner-rect {
  position: absolute;
  top: var(--inner-rect-top);
  left: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  font-size: 18px;
  background: lightblue;
  z-index: 2;
  user-select: none;
  border: 1px solid #333;
  transition: box-shadow 0.2s, border-color 0.2s;
}

.inner-rect.active-state {
  background: darkblue;
  box-shadow: 0 0 5px dodgerblue;
  color: white;
}

.inner-rect.hover-highlight {
  border-color: dodgerblue;
  box-shadow: 0 0 5px dodgerblue;
}

.start_step .inner-rect {
  border: 4px double #333;
  width: var(--start_side);
  height: var(--start_side);
}

.active_step .inner-rect {
  width: var(--active_side);
  height: var(--active_side);
}

.connector {
  width: var(--connector-side);
  height: var(--connector-side);
  background: lightblue;
  position: absolute;
  cursor: crosshair;
  z-index: 3;
}

.connector.top.start,
.connector.top.active {
  top: -5px;
  left: calc(50% - 5px);
}

.connector.bottom.start,
.connector.bottom.active {
  bottom: -5px;
  left: calc(50% - 5px);
}

.line-top,
.line-bottom {
  position: absolute;
  width: 2px;
  background: grey;
  z-index: 1;
}

.line-top {
  top: 0;
  height: var(--inner-rect-top);
}

.line-bottom {
  top: calc(var(--start_side) + var(--inner-rect-top));
  height: calc(var(--box_side) - (var(--start_side) + var(--inner-rect-top)));
}

.line-top.start,
.line-bottom.start,
.line-top.active,
.line-bottom.active {
  left: calc(50% - 3px);
}

.transition {
  position: absolute;
  width: 25px;
  height: 6px;
  background: #333;
  top: calc(var(--box_side) - 20px);
  z-index: 1;
  left: calc(50% - 12.5px);
  cursor: pointer;
  border: 1px solid #222;
}

.connected-to-branch .transition {
  display: none !important;
}

/* --- Estilos para Divergência e Convergência OR e AND --- */
.box.or_divergence,
.box.or_convergence,
.box.and_divergence,
.box.and_convergence {
  width: 360px;
  height: 60px;
  background: transparent;
}

#palette .box.or_divergence,
#palette .box.or_convergence,
#palette .box.and_divergence,
#palette .box.and_convergence {
  width: 100px;
  height: 50px;
  margin-bottom: 8px;
  background: #fff;
  border: 1px dashed #ccc;
  border-radius: 4px;
}

.branch-bar {
  position: absolute;
  height: 3px;
  background: #333;
  left: 20px;
  right: 20px;
  z-index: 1;
}

/* Barra Dupla para AND */
.branch-bar.double {
  height: 8px;
  background: transparent;
  border-top: 3px solid #333;
  border-bottom: 3px solid #333;
  box-sizing: border-box;
}

#palette .branch-bar {
  left: 10px;
  right: 10px;
}

.or_divergence .branch-bar,
.and_divergence .branch-bar {
  top: 15px;
}

.or_convergence .branch-bar {
  bottom: 15px;
}

.and_convergence .branch-bar {
  bottom: 26px;
}

.branch-stem-top {
  position: absolute;
  width: 2px;
  background: #333;
  z-index: 1;
}

.or_divergence .branch-stem-top,
.and_divergence .branch-stem-top {
  top: 0;
  height: 15px;
  left: calc(50% - 1px);
}

.or_convergence .branch-legs.top .branch-stem-top {
  top: 0;
  height: 45px;
  width: 2px;
  background: #333;
  left: calc(50% - 1px);
}

.and_convergence .branch-legs.top .branch-stem-top {
  top: 0;
  height: 34px;
  width: 2px;
  background: #333;
  left: calc(50% - 1px);
}

#palette .or_convergence .branch-legs.top .branch-stem-top {
  height: 35px;
}

#palette .and_convergence .branch-legs.top .branch-stem-top {
  height: 25px;
}

.branch-stem-bottom {
  position: absolute;
  width: 2px;
  background: #333;
  z-index: 1;
}

.or_divergence .branch-legs.bottom .branch-stem-bottom,
.and_divergence .branch-legs.bottom .branch-stem-bottom {
  top: 0;
  height: 45px;
  width: 2px;
  background: #333;
  left: calc(50% - 1px);
}

.or_convergence .branch-stem-bottom {
  bottom: 0;
  height: 15px;
  left: calc(50% - 1px);
}

.and_convergence .branch-stem-bottom {
  bottom: 0;
  height: 26px;
  left: calc(50% - 1px);
}

.branch-legs {
  position: absolute;
  width: 100%;
  left: 0;
}

.or_divergence .branch-legs.bottom,
.and_divergence .branch-legs.bottom {
  top: 15px;
  height: 45px;
}

.or_convergence .branch-legs.top {
  top: 0;
  height: 45px;
}

.and_convergence .branch-legs.top {
  top: 0;
  height: 34px;
}

.branch-leg {
  position: absolute;
  width: 30px;
  height: 100%;
}

.branch-leg.left {
  left: 40px;
}

.branch-leg.right {
  right: 40px;
}

#palette .branch-leg.left {
  left: 12px;
}

#palette .branch-leg.right {
  right: 12px;
}

.branch-transition {
  position: absolute;
  width: 25px;
  height: 6px;
  background: #333;
  top: 18px;
  left: calc(50% - 12.5px);
  z-index: 2;
  cursor: pointer;
  border: 1px solid #222;
  transition: background-color 0.2s, transform 0.15s;
}

.and_convergence .branch-transition.center {
  top: auto;
  bottom: 10px;
  left: calc(50% - 12.5px);
}

.branch-transition:hover {
  background: dodgerblue;
  transform: scale(1.15);
}

.connector.center {
  left: calc(50% - 3px) !important;
}

.or_divergence .connector.top,
.and_divergence .connector.top {
  top: -3px;
  left: calc(50% - 3px);
}

.or_divergence .connector.bottom,
.and_divergence .connector.bottom {
  bottom: -3px;
  left: calc(50% - 3px);
}

.or_convergence .connector.top,
.and_convergence .connector.top {
  top: -3px;
  left: calc(50% - 3px);
}

.or_convergence .connector.bottom,
.and_convergence .connector.bottom {
  bottom: -3px;
  left: calc(50% - 3px);
}

#canvas-viewport {
  flex: 1;
  position: relative;
  overflow: auto;
  background-color: #dbe2ea;
  background-image: radial-gradient(#cbd5e1 1.2px, transparent 1.2px);
  background-size: 24px 24px;
  scroll-behavior: smooth;
  user-select: none;
  display: flex;
}

#canvas-viewport.panning {
  cursor: grabbing !important;
  scroll-behavior: auto !important;
}

#canvas-wrapper {
  margin: auto;
  padding: 60px;
  position: relative;
  flex-shrink: 0;
  box-sizing: content-box;
  display: block;
}

#canvas {
  width: 3200px;
  height: 2400px;
  position: absolute;
  top: 60px;
  left: 60px;
  background: #ffffff;
  background-image: radial-gradient(#cbd5e1 1px, transparent 1px);
  background-size: 20px 20px;
  transform-origin: 0 0;
  box-shadow: 0 16px 48px rgba(15, 23, 42, 0.22), 0 2px 8px rgba(15, 23, 42, 0.08);
  border: 1px solid #cbd5e1;
  border-radius: 4px;
}

/* Divisores de Folhas (Quadrantes 2x2) */
.quadrant-divider {
  position: absolute;
  pointer-events: none;
  z-index: 1;
  opacity: 0.35;
}

.quadrant-divider.vertical {
  top: 0;
  bottom: 0;
  left: 50%;
  width: 1px;
  border-left: 1.5px dashed #94a3b8;
}

.quadrant-divider.horizontal {
  left: 0;
  right: 0;
  top: 50%;
  height: 1px;
  border-top: 1.5px dashed #94a3b8;
}

.sheet-label {
  position: absolute;
  font-size: 12px;
  font-weight: bold;
  color: #94a3b8;
  padding: 3px 8px;
  background: rgba(248, 250, 252, 0.6);
  border: 1px dashed #cbd5e1;
  border-radius: 4px;
  pointer-events: none;
  user-select: none;
  opacity: 0.6;
}

.sheet-label.sheet-1 {
  top: 15px;
  left: 15px;
}

.sheet-label.sheet-2 {
  top: 15px;
  right: 15px;
}

.sheet-label.sheet-3 {
  bottom: 15px;
  left: 15px;
}

.sheet-label.sheet-4 {
  bottom: 15px;
  right: 15px;
}

/* Controles Flutuantes de Navegação */
#nav-controls {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: rgba(255, 255, 255, 0.9);
  padding: 8px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(4px);
  z-index: 1000;
}

.nav-divider {
  width: 100%;
  height: 1px;
  background: #e2e8f0;
  margin: 2px 0;
}

.nav-row {
  display: flex;
  gap: 4px;
}

.nav-btn {
  width: 34px;
  height: 34px;
  margin: 0 !important;
  padding: 0;
  border: 1px solid #cbd5e1;
  background: #ffffff;
  color: #334155;
  border-radius: 6px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

.nav-btn.nav-btn-zoom-level {
  width: auto;
  min-width: 44px;
  padding: 0 4px;
  font-size: 11px;
  font-family: monospace;
}

.nav-btn:hover {
  background: #f1f5f9;
  color: #2563eb;
  border-color: #93c5fd;
  transform: scale(1.05);
}

.nav-btn:active {
  transform: scale(0.95);
}

#nav-center {
  background: #eff6ff;
  color: #2563eb;
  border-color: #bfdbfe;
}

svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

svg polyline {
  pointer-events: stroke;
  transition: stroke 0.2s;
}

svg polyline.hover-highlight {
  stroke: dodgerblue;
}

.palette-title {
  font-weight: bold;
  margin: 5px 0;
  user-select: none;
  width: 100%;
  text-align: center;
  font-size: 75%;
}

button {
  margin-top: 5px;
  padding: 4px 8px;
  font-size: 12px;
}

.box .transition.hover-highlight {
  background: dodgerblue;
  height: 8px;
}

.receptivity-label {
  white-space: nowrap;
}

/* Linha que liga o Step à ação */
.action-line {
  position: absolute;
  width: 20px;
  height: 2px;
  background: grey;
  pointer-events: none;
  z-index: 3;
}

/* Retângulo da ação */
.action-box {
  position: absolute;
  background: lightblue;
  border: 1px solid #333;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  white-space: nowrap;
  cursor: pointer;
  width: var(--active_side);
  height: var(--active_side);
  z-index: 10;
  user-select: none;
}

/* Tooltip customizado */
.action-tooltip {
  position: absolute;
  background: #fff;
  border: 1px solid #333;
  padding: 5px;
  font-size: 12px;
  z-index: 999;
  box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.2);
  max-width: 200px;
}