.editor-viewport {
  position: absolute;
  inset: 0;
  overflow: auto;
  /* Changed from grid to flex for mobile stability */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.canvas-wrap {
  position: relative;
  transform-origin: center center;
  filter: drop-shadow(var(--shadow-3));
  /* These two properties prevent the canvas from disappearing on mobile */
  flex-shrink: 0; 
  margin: auto;   
}

.canvas {
  position: relative;
  width: 1080px;
  height: 1080px;
  background: var(--canvas-bg);
  overflow: hidden;
  outline: none;
  transform-origin: 0 0;
  
  /* CRITICAL FOR MOBILE: Prevents page scrolling/zooming when dragging elements */
  touch-action: none; 
  /* Prevents the annoying Copy/Paste popup on long-press */
  -webkit-user-select: none;
  user-select: none;
}

/* SELECTION OVERLAY */
.selection-overlay {
  position: absolute;
  pointer-events: none;
  border: 2px solid var(--accent);
  display: none;
  z-index: 999;
}
.selection-overlay.active { display: block; }
.selection-overlay .handle {
  position: absolute;
  width: 10px; height: 10px;
  background: #fff;
  border: 2px solid var(--accent);
  border-radius: 2px;
  pointer-events: auto;
  transform: translate(-50%, -50%);
  
  /* CRITICAL FOR MOBILE: Ensures dragging a handle doesn't scroll the page */
  touch-action: none; 
}
.selection-overlay .handle.rot {
  border-radius: 999px;
  top: -22px; left: 50%;
  background: var(--accent);
  border-color: #fff;
  cursor: grab;
}
.selection-overlay .handle[data-dir="nw"] { top: 0; left: 0; cursor: nwse-resize; }
.selection-overlay .handle[data-dir="n"]  { top: 0; left: 50%; cursor: ns-resize; }
.selection-overlay .handle[data-dir="ne"] { top: 0; left: 100%; cursor: nesw-resize; }
.selection-overlay .handle[data-dir="e"]  { top: 50%; left: 100%; cursor: ew-resize; }
.selection-overlay .handle[data-dir="se"] { top: 100%; left: 100%; cursor: nwse-resize; }
.selection-overlay .handle[data-dir="s"]  { top: 100%; left: 50%; cursor: ns-resize; }
.selection-overlay .handle[data-dir="sw"] { top: 100%; left: 0; cursor: nesw-resize; }
.selection-overlay .handle[data-dir="w"]  { top: 50%; left: 0; cursor: ew-resize; }

/* ELEMENTS */
.el {
  position: absolute;
  cursor: move;
  user-select: none;
  transform-origin: center center;
}
.el.locked { pointer-events: none; opacity: 0.9; }
.el.hidden { display: none; }

.el-text {
  display: inline-block;
  padding: 2px;
  min-width: 20px;
  white-space: pre-wrap;
  word-wrap: break-word;
  outline: none;
}
.el-text[contenteditable="true"] { 
  cursor: text; 
  /* Re-enable text selection ONLY when editing text */
  -webkit-user-select: text;
  user-select: text;
}

.el-shape svg, .el-frame svg { display: block; width: 100%; height: 100%; overflow: visible; }
.el-image {
  overflow: hidden;
  background-size: cover;
  background-position: center;
}
.el-image img { width: 100%; height: 100%; object-fit: cover; pointer-events: none; display: block; }

/* =========================================
   MOBILE RESPONSIVE: EDITOR & HANDLES
========================================= */
@media (max-width: 768px) {
  /* Reclaim wasted screen real estate by reducing padding */
  .editor-viewport {
    padding: 16px;
  }

  /* Make resize handles larger for touch screens (fingers) */
  .selection-overlay .handle {
    width: 20px; 
    height: 20px;
  }

  /* Adjust the rotation handle specifically so it doesn't overlap due to new size */
  .selection-overlay .handle.rot {
    top: -34px; 
    width: 24px; 
    height: 24px;
  }
}
