/* ============================================
   ÁREA DO CANVAS - PROFESSIONAL DESIGN SUITE
   ============================================ */

:root {
  /* Tokens específicos do canvas */
  --ruler-size: 24px;
  --ruler-bg: var(--surface-0);
  --ruler-mark-color: var(--surface-400);
  --ruler-text-color: var(--surface-500);
  --canvas-bg: #2a2a2a;
  --canvas-grid-color: rgba(255, 255, 255, 0.08);
  --canvas-grid-major: rgba(255, 255, 255, 0.15);
  --minimap-width: 200px;
  --minimap-height: 150px;
  --statusbar-height: 28px;
}

/* ============================================
   CONTAINER PRINCIPAL
   ============================================ */

.canvas-area {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
  background: var(--canvas-bg);
  isolation: isolate; /* Cria novo stacking context */
}

/* ============================================
   SISTEMA DE RÉGUAS
   ============================================ */

.ruler-container {
  display: grid;
  grid-template-columns: var(--ruler-size) 1fr;
  grid-template-rows: var(--ruler-size) 1fr;
  flex-shrink: 0;
  z-index: 10;
}

/* Canto superior esquerdo */
.ruler-corner {
  grid-column: 1;
  grid-row: 1;
  background: var(--ruler-bg);
  border-right: 1px solid var(--surface-200);
  border-bottom: 1px solid var(--surface-200);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition-fast);
}

.ruler-corner:hover {
  background: var(--surface-100);
}

.ruler-corner:active {
  background: var(--surface-200);
}

/* Ícone de origem */
.ruler-corner::after {
  content: '';
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 0 8px 8px;
  border-color: transparent transparent var(--ruler-text-color) transparent;
  opacity: 0.5;
}

/* Régua horizontal */
.ruler-horizontal {
  grid-column: 2;
  grid-row: 1;
  height: var(--ruler-size);
  background: var(--ruler-bg);
  border-bottom: 1px solid var(--surface-200);
  position: relative;
  overflow: hidden;
  cursor: col-resize;
}

/* Régua vertical */
.ruler-vertical {
  grid-column: 1;
  grid-row: 2;
  width: var(--ruler-size);
  background: var(--ruler-bg);
  border-right: 1px solid var(--surface-200);
  position: relative;
  overflow: hidden;
  cursor: row-resize;
}

/* Marcações das réguas */
.ruler-marks {
  position: absolute;
  pointer-events: none;
}

.ruler-horizontal .ruler-marks {
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-image: 
    linear-gradient(to right, var(--ruler-mark-color) 1px, transparent 1px),
    linear-gradient(to right, var(--ruler-mark-color) 1px, transparent 1px);
  background-size: 
    100px 100%,
    10px 40%;
  background-position: 
    0 100%,
    0 100%;
  background-repeat: repeat-x;
}

.ruler-vertical .ruler-marks {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    linear-gradient(to bottom, var(--ruler-mark-color) 1px, transparent 1px),
    linear-gradient(to bottom, var(--ruler-mark-color) 1px, transparent 1px);
  background-size: 
    100% 100px,
    40% 10px;
  background-position: 
    100% 0,
    100% 0;
  background-repeat: repeat-y;
}

/* Números das réguas (pseudo-elementos para performance) */
.ruler-numbers {
  position: absolute;
  font-size: 9px;
  color: var(--ruler-text-color);
  font-family: var(--font-mono);
  user-select: none;
}

/* Cursor de guia */
.ruler-cursor {
  position: absolute;
  background: var(--primary-500);
  pointer-events: none;
  z-index: 5;
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.ruler-horizontal:hover .ruler-cursor,
.ruler-vertical:hover .ruler-cursor {
  opacity: 1;
}

.ruler-horizontal .ruler-cursor {
  width: 1px;
  height: 100%;
  top: 0;
}

.ruler-vertical .ruler-cursor {
  width: 100%;
  height: 1px;
  left: 0;
}

/* ============================================
   ÁREA DO CANVAS
   ============================================ */

.canvas-wrapper {
  grid-column: 2;
  grid-row: 2;
  position: relative;
  overflow: auto;
  background: var(--canvas-bg);
  
  /* Grid de fundo do canvas */
  background-image: 
    linear-gradient(var(--canvas-grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--canvas-grid-major) 1px, transparent 1px),
    linear-gradient(var(--canvas-grid-color) 1px, transparent 1px),
    linear-gradient(90deg, var(--canvas-grid-color) 1px, transparent 1px);
  background-size: 
    100px 100px,
    100px 100px,
    20px 20px,
    20px 20px;
  
  /* Checkerboard pattern para transparência */
  background-color: #2a2a2a;
  background-image: 
    linear-gradient(45deg, #333 25%, transparent 25%),
    linear-gradient(-45deg, #333 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #333 75%),
    linear-gradient(-45deg, transparent 75%, #333 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
  
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-8);
  
  /* Scrollbar styling */
  scrollbar-width: thin;
  scrollbar-color: var(--surface-500) var(--surface-700);
}

.canvas-wrapper::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}

.canvas-wrapper::-webkit-scrollbar-track {
  background: var(--surface-700);
}

.canvas-wrapper::-webkit-scrollbar-thumb {
  background: var(--surface-500);
  border-radius: var(--radius-full);
  border: 3px solid var(--surface-700);
}

.canvas-wrapper::-webkit-scrollbar-thumb:hover {
  background: var(--surface-400);
}

.canvas-wrapper::-webkit-scrollbar-corner {
  background: var(--surface-700);
}

/* Canvas principal */
#mainCanvas {
  background: white;
  box-shadow: 
    0 0 0 1px var(--surface-300),
    0 20px 60px rgba(0, 0, 0, 0.3),
    0 0 0 0 rgba(0, 0, 0, 0);
  border-radius: 2px;
  display: block;
  max-width: none;
  max-height: none;
  
  /* Transição suave para transformações */
  transition: box-shadow var(--transition-normal);
  will-change: transform;
}

#mainCanvas.is-dragging {
  cursor: grabbing;
}

#mainCanvas.has-selection {
  box-shadow: 
    0 0 0 1px var(--primary-500),
    0 20px 60px rgba(0, 0, 0, 0.3),
    0 0 0 4px rgba(14, 165, 233, 0.1);
}

/* ============================================
   OVERLAYS DO CANVAS
   ============================================ */

/* Grid overlay toggle */
.grid-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 1;
  
  background-image: 
    linear-gradient(var(--primary-500) 1px, transparent 1px),
    linear-gradient(90deg, var(--primary-500) 1px, transparent 1px);
  background-size: 20px 20px;
  opacity: 0;
  transition: opacity var(--transition-normal);
}

.grid-overlay.visible {
  opacity: 0.15;
}

.grid-overlay.snap-to-grid {
  opacity: 0.25;
  background-size: 10px 10px;
}

/* Guias (guides) */
.guides-layer {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 2;
}

.guide-line {
  position: absolute;
  background: var(--primary-500);
  pointer-events: auto;
  cursor: move;
}

.guide-line.horizontal {
  width: 100%;
  height: 1px;
  left: 0;
}

.guide-line.vertical {
  width: 1px;
  height: 100%;
  top: 0;
}

.guide-line:hover {
  background: var(--primary-400);
  box-shadow: 0 0 0 2px rgba(14, 165, 233, 0.3);
}

.guide-line.is-dragging {
  background: var(--primary-600);
  pointer-events: none;
}

/* Pontos de snap visuais */
.snap-points {
  position: absolute;
  width: 8px;
  height: 8px;
  background: var(--primary-500);
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 0 0 1px var(--primary-600);
  opacity: 0;
  transform: scale(0);
  transition: all var(--transition-bounce);
  pointer-events: none;
  z-index: 3;
}

.snap-points.active {
  opacity: 1;
  transform: scale(1);
}

/* ============================================
   MINI NAVEGADOR (MINIMAP)
   ============================================ */

.minimap {
  position: absolute;
  bottom: calc(var(--statusbar-height) + var(--space-4));
  right: var(--space-4);
  width: var(--minimap-width);
  background: var(--surface-0);
  border-radius: var(--radius-lg);
  box-shadow: 
    0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -4px rgba(0, 0, 0, 0.1),
    0 0 0 1px var(--surface-200);
  z-index: 20;
  overflow: hidden;
  transition: all var(--transition-normal);
}

.minimap:hover {
  box-shadow: 
    0 20px 25px -5px rgba(0, 0, 0, 0.1),
    0 8px 10px -6px rgba(0, 0, 0, 0.1),
    0 0 0 1px var(--surface-300);
}

.minimap.collapsed {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  cursor: pointer;
}

.minimap-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-3);
  background: var(--surface-50);
  border-bottom: 1px solid var(--surface-200);
}

.minimap-title {
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.minimap-controls {
  display: flex;
  gap: var(--space-1);
}

.minimap-btn {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.75rem;
  transition: all var(--transition-fast);
}

.minimap-btn:hover {
  background: var(--surface-200);
  color: var(--text-primary);
}

.minimap-content {
  padding: var(--space-2);
  background: var(--surface-100);
  position: relative;
}

#minimapCanvas {
  width: 100%;
  height: auto;
  background: white;
  border-radius: var(--radius-sm);
  display: block;
  cursor: crosshair;
}

/* Viewport indicator no minimap */
.minimap-viewport {
  position: absolute;
  border: 2px solid var(--primary-500);
  background: rgba(14, 165, 233, 0.1);
  border-radius: 2px;
  cursor: move;
  transition: all var(--transition-fast);
}

.minimap-viewport:hover {
  background: rgba(14, 165, 233, 0.2);
  border-color: var(--primary-400);
}

/* ============================================
   STATUS BAR PROFISSIONAL
   ============================================ */

.canvas-statusbar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--statusbar-height);
  background: linear-gradient(
    180deg,
    var(--surface-50) 0%,
    var(--surface-0) 100%
  );
  border-top: 1px solid var(--surface-200);
  padding: 0 var(--space-4);
  
  display: flex;
  align-items: center;
  justify-content: space-between;
  
  font-size: var(--text-xs);
  color: var(--text-secondary);
  z-index: 15;
}

.status-left,
.status-right {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.status-item {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  transition: background var(--transition-fast);
  cursor: default;
}

.status-item:hover {
  background: var(--surface-100);
}

.status-item.interactive {
  cursor: pointer;
}

.status-item.interactive:hover {
  background: var(--surface-200);
  color: var(--text-primary);
}

.status-icon {
  font-size: 0.875rem;
  opacity: 0.7;
}

.status-label {
  font-weight: var(--font-medium);
}

.status-value {
  font-family: var(--font-mono);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
}

.separator {
  width: 1px;
  height: 16px;
  background: var(--surface-300);
}

/* Indicadores de modo */
.status-mode {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px 8px;
  background: var(--surface-200);
  border-radius: var(--radius-full);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
}

.status-mode.active {
  background: var(--primary-100);
  color: var(--primary-700);
}

/* ============================================
   CONTROLES FLUTUANTES DO CANVAS
   ============================================ */

.canvas-controls {
  position: absolute;
  top: var(--space-4);
  right: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: 10;
}

.canvas-control-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-0);
  border: 1px solid var(--surface-200);
  border-radius: var(--radius-lg);
  color: var(--text-secondary);
  font-size: 1rem;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition-fast);
}

.canvas-control-btn:hover {
  background: var(--surface-50);
  color: var(--primary-600);
  border-color: var(--primary-200);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.canvas-control-btn:active {
  transform: translateY(0);
}

.canvas-control-btn.active {
  background: var(--primary-50);
  color: var(--primary-600);
  border-color: var(--primary-300);
}

/* ============================================
   RESPONSIVE & ACCESSIBILITY
   ============================================ */

@media (max-width: 1024px) {
  .minimap {
    width: 160px;
    --minimap-height: 120px;
  }
  
  .canvas-controls {
    top: var(--space-2);
    right: var(--space-2);
  }
  
  .canvas-control-btn {
    width: 32px;
    height: 32px;
    font-size: 0.875rem;
  }
}

@media (max-width: 768px) {
  .ruler-container {
    --ruler-size: 20px;
  }
  
  .minimap {
    display: none; /* Esconde em mobile */
  }
  
  .canvas-statusbar {
    font-size: 10px;
    padding: 0 var(--space-2);
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .canvas-wrapper,
  #mainCanvas,
  .minimap,
  .grid-overlay,
  .snap-points,
  .canvas-control-btn {
    transition: none;
  }
  
  .ruler-cursor {
    opacity: 1;
  }
}

/* High contrast mode */
@media (prefers-contrast: high) {
  .ruler-marks {
    background-image: 
      linear-gradient(to right, black 1px, transparent 1px),
      linear-gradient(to right, black 1px, transparent 1px);
  }
  
  #mainCanvas {
    box-shadow: 0 0 0 2px black;
  }
}

/* ============================================
   ESTADOS DE INTERAÇÃO
   ============================================ */

/* Seleção ativa no canvas */
.canvas-wrapper.has-selection::after {
  content: '';
  position: absolute;
  pointer-events: none;
  border: 2px dashed var(--primary-500);
  border-radius: 2px;
  animation: selection-pulse 2s ease infinite;
}

@keyframes selection-pulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

/* Zoom indicator */
.zoom-indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: var(--space-3) var(--space-6);
  border-radius: var(--radius-xl);
  font-size: var(--text-2xl);
  font-weight: var(--font-bold);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-fast);
  z-index: 100;
}

.zoom-indicator.visible {
  opacity: 1;
}

/* Loading state */
.canvas-wrapper.is-loading::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(42, 42, 42, 0.8);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
}

.canvas-wrapper.is-loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  border: 3px solid var(--surface-500);
  border-top-color: var(--primary-500);
  border-radius: 50%;
  animation: canvas-spinner 0.8s linear infinite;
  z-index: 51;
}

@keyframes canvas-spinner {
  to { transform: rotate(360deg); }
}