/* --- UI Overlay Layer --- */
.ui-layer {
  position: fixed;
  z-index: 1000;
  padding: 20px;
  pointer-events: none;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

/* Left Control Stack */
.left-stack {
  display: flex;
  flex-direction: column;
  gap: 15px;
  max-width: 320px;
  max-height: 95vh;
}

/* --- Canvas Layer --- */
#wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  cursor: grab;
  z-index: 1;
  background: var(--bg-dark);
  touch-action: none;
}

#wrapper:active {
  cursor: grabbing;
}

#canvas {
  position: absolute;
  width: 3800px;
  height: 2400px;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.02) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
  background-size: 40px 40px;
  transform-origin: 0 0;
}

svg {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 5;
}

/* --- Responsive Layout Tweaks --- */
@media (max-width: 1100px) {
  .ui-layer {
    padding: 12px;
    justify-content: flex-start;
    align-items: stretch;
  }

  .left-stack {
    width: 100%;
    max-width: none;
    max-height: none;
    transition: transform 0.3s ease, opacity 0.3s ease;
    z-index: 1500;
  }

  body.menu-closed .left-stack {
    transform: translateX(-110%);
    opacity: 0;
    pointer-events: none;
  }

  body.menu-closed .ui-layer {
    pointer-events: none;
  }
}

@media (max-width: 720px) {
  body {
    height: 100dvh;
    width: 100dvw;
  }

  .ui-layer {
    gap: 10px;
    height: auto;
    min-height: 100%;
    align-items: stretch;
    padding: 8px; /* Reduce padding on mobile */
  }

  /* Fix: Make the whole stack scrollable, not individual panels */
  .left-stack {
    max-height: calc(100dvh - 16px);
    overflow-y: auto;
    overflow-x: hidden; /* Prevent horizontal overflow */
    pointer-events: auto; /* allow scrolling */
    padding-bottom: 60px; /* space for bottom button if needed */
  }

  .left-stack .panel {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    max-height: none; /* remove individual limits */
    overflow-x: hidden; /* Prevent horizontal overflow */
    overflow-y: visible;
  }

  /* Ensure panel content wraps and doesn't overflow */
  .header-panel,
  .controls-panel {
    word-wrap: break-word;
    overflow-wrap: break-word;
  }
}
