/* ===== Design Canvas (Figma-style layout planner) ===== */
.cv-overlay {
  position: fixed;
  inset: 0;
  z-index: 99990;
  display: none;
  flex-direction: column;
  background: var(--bg);
}
.cv-overlay.open { display: flex; }

/* Top toolbar */
.cv-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: 52px;
  padding: 0 14px;
  border-bottom: 0.5px solid var(--border);
  background: var(--bg-sidebar);
  flex-shrink: 0;
}
.cv-tb-left, .cv-tb-right { display: flex; align-items: center; gap: 10px; }
.cv-logo { font-family: 'Inter', sans-serif; font-weight: 700; font-size: 13px; color: var(--text); letter-spacing: 0.3px; }
.cv-tools { display: flex; gap: 2px; background: var(--bg-input); border: 0.5px solid var(--border); border-radius: 10px; padding: 3px; }
.cv-tool {
  width: 30px; height: 30px; display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: none; border-radius: 7px; color: var(--text-muted);
  cursor: pointer; transition: all 0.15s; -webkit-tap-highlight-color: transparent;
}
.cv-tool:hover { background: var(--hover); color: var(--text); }
.cv-tool.active { background: var(--accent); color: #fff; }
.cv-zoom { display: flex; align-items: center; gap: 4px; font-family: 'Inter', sans-serif; font-size: 12px; color: var(--text-muted); }
.cv-zoom button { width: 26px; height: 26px; border: 0.5px solid var(--border); background: transparent; border-radius: 6px; color: var(--text-muted); cursor: pointer; line-height: 1; }
.cv-zoom button:hover { background: var(--hover); color: var(--text); }
.cv-zoom span { min-width: 44px; text-align: center; }
.cv-act {
  height: 32px; padding: 0 14px; border: 0.5px solid var(--border); background: transparent;
  border-radius: 8px; color: var(--text); font-family: 'Inter', sans-serif; font-size: 12px; font-weight: 600;
  cursor: pointer; transition: all 0.15s; -webkit-tap-highlight-color: transparent;
}
.cv-act:hover { background: var(--hover); border-color: var(--border-hover); }
.cv-act.cv-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.cv-act.cv-primary:hover { background: var(--orange); }
.cv-act.cv-close { width: 32px; padding: 0; }

/* Stage + world */
.cv-stage {
  flex: 1; position: relative; overflow: hidden;
  background-color: var(--bg);
  background-image: radial-gradient(rgba(127,127,127,0.18) 1px, transparent 1px);
  background-size: 24px 24px;
}
.cv-stage.panning { cursor: grabbing; }
.cv-world { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; }

/* Elements */
.cv-el { position: absolute; box-sizing: border-box; font-family: ui-sans-serif, system-ui, -apple-system, sans-serif; }
.cv-el.sel { outline: 1.5px solid var(--accent); outline-offset: 1px; }
.cv-el[data-type=frame] { border: 1.5px solid var(--text-neutral, #9a9893); border-radius: 10px; background: rgba(127,127,127,0.05); z-index: 1; }
.cv-el[data-type=frame] .cv-label {
  position: absolute; top: -10px; left: 10px; font-size: 11px; background: var(--bg); padding: 0 6px;
  color: var(--text-muted); font-family: 'Inter', sans-serif; outline: none; border-radius: 4px;
}
.cv-el[data-type=heading] { font-weight: 700; font-size: 24px; color: var(--text); z-index: 2; }
.cv-el[data-type=text] { font-size: 14px; color: var(--text-body, var(--text)); line-height: 1.5; z-index: 2; }
.cv-el[data-type=button] { background: var(--accent); color: #fff; border-radius: 8px; font-weight: 600; font-size: 14px; z-index: 2; }
.cv-el[data-type=image] { border-radius: 8px; color: var(--text-muted); font-size: 12px; z-index: 2; }
.cv-el[data-type=image]:not(:has(.cv-img-preview)) {
  background: rgba(127,127,127,0.12); border: 1px dashed var(--text-neutral, #9a9893);
}
.cv-el[data-type=rect] { background: rgba(174,86,48,0.15); border: 1px solid var(--accent); border-radius: 8px; z-index: 2; }
.cv-content { width: 100%; height: 100%; outline: none; overflow: hidden; padding: 6px 8px; box-sizing: border-box; }
.cv-el[data-type=heading] .cv-content,
.cv-el[data-type=button] .cv-content,
.cv-el[data-type=image] .cv-content { display: flex; align-items: center; justify-content: center; text-align: center; }
.cv-el[data-type=image] .cv-content.cv-img-preview {
  padding: 0;
  border-radius: 7px;
  background-color: #fff;
  background-image:
    linear-gradient(45deg, rgba(0,0,0,0.06) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(0,0,0,0.06) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(0,0,0,0.06) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(0,0,0,0.06) 75%);
  background-size: 12px 12px;
  background-position: 0 0, 0 6px, 6px -6px, -6px 0px;
}
.cv-el[data-type=image] .cv-content.cv-img-preview img {
  width: 100%; height: 100%; object-fit: contain; border-radius: 7px; display: block;
}

/* Resize handles */
.cv-handle { position: absolute; width: 10px; height: 10px; background: #fff; border: 1.5px solid var(--accent); border-radius: 2px; display: none; z-index: 5; }
.cv-el.sel .cv-handle { display: block; }
.cv-handle.nw { left: -5px; top: -5px; cursor: nwse-resize; }
.cv-handle.ne { right: -5px; top: -5px; cursor: nesw-resize; }
.cv-handle.sw { left: -5px; bottom: -5px; cursor: nesw-resize; }
.cv-handle.se { right: -5px; bottom: -5px; cursor: nwse-resize; }

/* Smart alignment guides */
.cv-guides { position: absolute; inset: 0; pointer-events: none; z-index: 4; overflow: hidden; }
.cv-guide { position: absolute; }
.cv-guide-v { top: 0; bottom: 0; width: 0; border-left: 1px dashed var(--accent); }
.cv-guide-h { left: 0; right: 0; height: 0; border-top: 1px dashed var(--accent); }

/* Hint pill */
.cv-hint {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  font-size: 11px; color: var(--text-muted); background: var(--bg-input);
  border: 0.5px solid var(--border); border-radius: 20px; padding: 5px 14px;
  pointer-events: none; z-index: 3; font-family: 'Inter', sans-serif; white-space: nowrap;
}

/* Image import progress overlay */
.cv-import-progress {
  position: absolute; inset: 0; z-index: 20;
  display: none; align-items: center; justify-content: center;
  background: rgba(245, 245, 240, 0.72);
  pointer-events: all;
}
.cv-import-progress.show { display: flex; }
.cv-ip-card {
  width: min(360px, calc(100vw - 48px));
  padding: 20px 22px;
  background: var(--bg-input);
  border: 0.5px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 0.25rem 1.25rem rgba(0,0,0,0.035);
  font-family: 'Inter', sans-serif;
}
.cv-ip-label {
  font-size: 13px; font-weight: 600; color: var(--text);
  margin-bottom: 14px; line-height: 1.4;
}
.cv-ip-track {
  height: 6px; border-radius: 999px;
  background: rgba(0,0,0,0.06); overflow: hidden;
}
.cv-ip-fill {
  height: 100%; width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.35s cubic-bezier(0.165, 0.85, 0.45, 1);
}
.cv-ip-pct {
  margin-top: 10px; font-size: 11px; color: var(--text-muted);
  text-align: right; font-variant-numeric: tabular-nums;
}
.cv-overlay.cv-busy .cv-act[data-act="import"],
.cv-overlay.cv-busy .cv-act[data-act="removebg"] { opacity: 0.45; pointer-events: none; }

@media (prefers-color-scheme: dark) {
  .cv-import-progress { background: rgba(43, 42, 39, 0.78); }
}

@media (max-width: 768px) {
  .cv-logo { display: none; }
  .cv-act { padding: 0 10px; }
  .cv-hint { display: none; }
}

/* Phones: the desktop toolbar (~634px) overflows a phone viewport, which used to
   push the close ✕ (and right-side actions) off-screen — trapping the user in the
   fullscreen canvas with no way back. Fix: let the toolbar scroll horizontally so
   every control is reachable, and PIN the close ✕ to the top-right corner so the
   user can always exit. */
@media (max-width: 600px) {
  .cv-topbar {
    gap: 6px;
    padding: 0 46px 0 6px;            /* reserve space on the right for the pinned ✕ */
    overflow-x: auto;
    overflow-y: hidden;
    justify-content: flex-start;
    -webkit-overflow-scrolling: touch;
  }
  .cv-tb-left, .cv-tb-right { flex: 0 0 auto; }
  .cv-act.cv-close {
    position: fixed;
    top: 9px;
    right: 8px;
    z-index: 100000;                  /* above the overlay (99990) */
    width: 34px;
    height: 34px;
    background: var(--bg-sidebar);
    border: 0.5px solid var(--border-hover);
    border-radius: 8px;
  }
}
