/* ════════════════════════════════════════════════════════════════════════
 * magic-layer.css — Canva-style layered editor for zdoc-edit
 * v6.6 — adds a true layer-based design surface on top of the existing
 *        contenteditable doc-editor. Layers: background image + text + shapes
 *        + image layers (signature, logo). Each layer can be moved, resized,
 *        deleted, hidden, reordered. Export flattens to PNG/PDF via html2canvas.
 * ════════════════════════════════════════════════════════════════════════ */

/* ── Canvas: the design surface ─────────────────────────────────────────── */
.ml-canvas-wrap {
  position: relative;
  margin: 16px 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 500px;
  /* checkerboard so transparent PNGs are visible */
  background-image:
    linear-gradient(45deg, rgba(255,255,255,0.04) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255,255,255,0.04) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255,255,255,0.04) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255,255,255,0.04) 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}

.ml-canvas {
  position: relative;
  width: 100%;
  min-height: 600px;
  background: #fff;
  /* paper-like max width + center */
  max-width: 794px;   /* A4 @ 96 DPI */
  margin: 0 auto;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  overflow: hidden;
}

.ml-layer {
  position: absolute;
  cursor: move;
  user-select: none;
  -webkit-user-select: none;
  transition: box-shadow 0.15s;
}
.ml-layer.selected {
  box-shadow: 0 0 0 2px var(--blue), 0 0 0 4px rgba(10,132,255,0.25);
  z-index: 999;
}
.ml-layer.hidden { display: none; }

/* Background layer — non-movable, fills the canvas */
.ml-layer.ml-bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: default;
  z-index: 0;
}
.ml-layer.ml-bg img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

/* Text layer */
.ml-layer.ml-text {
  padding: 4px 8px;
  min-width: 60px;
  min-height: 24px;
  background: transparent;
  color: #1a1a1a;
  font-size: 14px;
  font-family: var(--font);
  line-height: 1.4;
  cursor: text;
  white-space: pre-wrap;
  word-break: break-word;
}
.ml-layer.ml-text[contenteditable="true"] {
  cursor: text;
  outline: none;
}
.ml-layer.ml-text[contenteditable="true"]:focus {
  background: rgba(10,132,255,0.05);
}
.ml-layer.ml-text.field {
  background: rgba(10,132,255,0.08);
  border: 1px dashed rgba(10,132,255,0.4);
  border-radius: 4px;
  color: #0a84ff;
  font-weight: 500;
}
.ml-layer.ml-text.field.filled {
  color: #1a1a1a;
  background: rgba(48,209,88,0.12);
  border-color: rgba(48,209,88,0.4);
}

/* Shape layer (rectangle / line / circle) */
.ml-layer.ml-shape {
  background: rgba(10,132,255,0.12);
  border: 2px solid var(--blue);
  border-radius: 4px;
}
.ml-layer.ml-shape.circle { border-radius: 50%; }
.ml-layer.ml-shape.line {
  height: 2px !important;
  background: #1a1a1a;
  border: none;
  border-radius: 1px;
}

/* Image layer (signature / logo) */
.ml-layer.ml-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  display: block;
}

/* Resize handles (8 handles around selected layer) */
.ml-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: #fff;
  border: 2px solid var(--blue);
  border-radius: 50%;
  z-index: 1000;
  pointer-events: auto;
}
.ml-handle.nw { top: -6px; left: -6px; cursor: nwse-resize; }
.ml-handle.n  { top: -6px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.ml-handle.ne { top: -6px; right: -6px; cursor: nesw-resize; }
.ml-handle.e  { top: 50%; right: -6px; transform: translateY(-50%); cursor: ew-resize; }
.ml-handle.se { bottom: -6px; right: -6px; cursor: nwse-resize; }
.ml-handle.s  { bottom: -6px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.ml-handle.sw { bottom: -6px; left: -6px; cursor: nesw-resize; }
.ml-handle.w  { top: 50%; left: -6px; transform: translateY(-50%); cursor: ew-resize; }

/* Delete button on selected layer */
.ml-delete {
  position: absolute;
  top: -32px;
  right: -8px;
  width: 26px;
  height: 26px;
  background: var(--red);
  color: #fff;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1001;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.ml-delete:hover { transform: scale(1.1); }

/* ── Magic Layer toolbar ───────────────────────────────────────────────── */
.ml-toolbar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  align-items: center;
}
.ml-toolbar .ml-group {
  display: flex;
  gap: 4px;
  padding-right: 8px;
  border-right: 1px solid var(--border);
}
.ml-toolbar .ml-group:last-child { border-right: none; }
.ml-tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text2);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.ml-tool-btn:hover { background: rgba(255,255,255,0.08); color: var(--text); }
.ml-tool-btn:active { transform: scale(0.96); }
.ml-tool-btn.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.ml-tool-btn i { font-size: 13px; }
.ml-tool-btn.magic {
  background: linear-gradient(135deg, var(--purple), var(--blue));
  color: #fff;
  border: none;
}
.ml-tool-btn.magic:hover {
  background: linear-gradient(135deg, #c45cff, #1a90ff);
  box-shadow: 0 4px 16px rgba(191,90,242,0.4);
}

/* ── Layer panel (right sidebar on desktop, bottom sheet on mobile) ────── */
.ml-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-top: 12px;
}
.ml-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.ml-panel-head h4 {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
}
.ml-layer-count {
  font-size: 11px;
  color: var(--text3);
  background: var(--surface2);
  padding: 2px 8px;
  border-radius: 10px;
}
.ml-layer-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 280px;
  overflow-y: auto;
}
.ml-layer-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--surface2);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.15s;
}
.ml-layer-row:hover { background: rgba(255,255,255,0.06); }
.ml-layer-row.active {
  border-color: var(--blue);
  background: rgba(10,132,255,0.1);
}
.ml-layer-row-icon {
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border-radius: 5px;
  font-size: 11px;
  color: var(--text3);
  flex-shrink: 0;
}
.ml-layer-row-name {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text2);
}
.ml-layer-row-actions {
  display: flex;
  gap: 4px;
}
.ml-layer-row-actions button {
  background: none;
  border: none;
  color: var(--text3);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  font-size: 11px;
}
.ml-layer-row-actions button:hover { background: rgba(255,255,255,0.1); color: var(--text); }
.ml-layer-row-actions .del:hover { color: var(--red); }
.ml-layer-empty {
  text-align: center;
  padding: 24px 8px;
  color: var(--text3);
  font-size: 12px;
}

/* ── Toggle between Magic Layer and Classic Editor ─────────────────────── */
.ml-mode-toggle {
  display: flex;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 12px;
  width: fit-content;
}
.ml-mode-toggle button {
  padding: 6px 14px;
  background: none;
  border: none;
  color: var(--text3);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 5px;
  transition: all 0.15s;
}
.ml-mode-toggle button.active {
  background: var(--blue);
  color: #fff;
}

/* ── Mobile adjustments ───────────────────────────────────────────────── */
@media (max-width: 768px) {
  .ml-canvas { min-height: 500px; }
  .ml-toolbar { padding: 8px; }
  .ml-tool-btn { padding: 5px 8px; font-size: 11px; }
  .ml-tool-btn span { display: none; }   /* icons only on mobile */
}
