/* ─────────────────────────────────────────────────────────────────────────────
   Free Layout Mode — all styles for the canvas-based block positioning system
   ───────────────────────────────────────────────────────────────────────────── */

/* ── Canvas container ─────────────────────────────────────────────────────── */
.blocks-free-layout {
  position: relative;
  width: 100%;
  min-height: 500px;
  /* Extra top padding so blocks at y≈0 don't visually bleed into the toolbar */
  padding-top: 16px;
  box-sizing: border-box;
  /* Let blocks overflow to the right without clipping */
  overflow-x: auto;
  overflow-y: visible;
}

/* Toolbar sits in its own stacking context above the canvas blocks.
 * Since .block-free uses z-index: 1 (and z-index: 100 while dragging),
 * the toolbar needs z-index > 100 to stay visible on top of dragged blocks. */
.free-layout-toolbar {
  position: relative;
  z-index: 200;
  /* Ensure the toolbar background fully covers any overflowing block below it */
  isolation: isolate;
}

/* When free layout is active, expand the page wrap so canvas is full-bleed */
.free-layout-active #editor-blocks,
.free-layout-active .fw-page-blocks {
  max-width: none !important;
  padding: 0 !important;
  margin: 0 !important;
}

/* ── Individual free-positioned block ────────────────────────────────────── */
.block-free {
  position: absolute !important;
  /* Default width — users can resize with the existing block width controls */
  width: 400px;
  max-width: 90vw;
  min-height: 32px;        /* Bug 8 fix: empty blocks are always clickable */
  box-sizing: border-box;
  /* Subtle card look so blocks are visually distinct on the canvas */
  background: var(--bg, #ffffff);
  border: 1.5px solid var(--border, rgba(0,0,0,0.08));
  border-radius: 8px;
  padding: 6px 10px 6px 8px;
  /* Smooth positioning on first render */
  will-change: left, top;
  transition: box-shadow 0.15s ease;
  /* z-index so blocks overlap cleanly while dragging */
  z-index: 1;
}

[data-theme="dark"] .block-free {
  background: var(--bg-secondary, #1a1a1f);
  border-color: rgba(255,255,255,0.09);
}

/* Hover: reveal handles, lift block slightly */
.block-free:hover {
  box-shadow: 0 2px 12px rgba(0,0,0,0.10);
  z-index: 2;
}

[data-theme="dark"] .block-free:hover {
  box-shadow: 0 2px 12px rgba(0,0,0,0.40);
}

/* ── Drag handle (⋮⋮) inline in free mode ─────────────────────────────────── */
.block-free .block-handle {
  display: flex !important;
  align-items: center;
  opacity: 0;
  cursor: grab;
  transition: opacity 0.12s ease;
  padding: 2px 4px;
  border-radius: 4px;
  color: var(--text-muted, #9b9b9b);
  font-size: 13px;
  line-height: 1;
  position: absolute;
  left: -28px;
  top: 50%;
  transform: translateY(-50%);
  user-select: none;
}

.block-free:hover .block-handle {
  opacity: 1;
}

/* ── Dragging state ───────────────────────────────────────────────────────── */
.block-free.is-dragging-free {
  opacity: 0.88;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.10);
  z-index: 100;
  cursor: grabbing !important;
  /* Slightly scale up while dragging for tactile feel */
  transform: scale(1.015);
  transition: box-shadow 0.1s ease, transform 0.1s ease;
}

[data-theme="dark"] .block-free.is-dragging-free {
  box-shadow: 0 8px 32px rgba(0,0,0,0.55);
}

/* Drop snap-back animation */
.block-free.free-drop-anim {
  transition: transform 0.18s cubic-bezier(0.22, 1, 0.36, 1), box-shadow 0.18s ease !important;
  transform: scale(1) !important;
}

/* ── Entrance animation ───────────────────────────────────────────────────── */
@keyframes freeBlockEnter {
  from {
    opacity: 0;
    transform: scale(0.94) translateY(6px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.block-free.free-enter {
  animation: freeBlockEnter 0.22s cubic-bezier(0.22, 1, 0.36, 1) both;
}

/* ── Empty block placeholder ──────────────────────────────────────────────── */
.block-free .editable:empty::before {
  content: attr(data-placeholder);
  color: var(--text-placeholder, #bbb);
  pointer-events: none;
  display: block;        /* ensures it takes vertical space */
  min-height: 1em;
}

/* ── Toolbar (layout toggle + undo/redo/reset) ────────────────────────────── */
.free-layout-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  margin-bottom: 8px;
  border-radius: 8px;
  background: var(--bg-secondary, #f7f7f8);
  border: 1px solid var(--border, rgba(0,0,0,0.08));
  font-size: 12px;
  flex-wrap: wrap;
  user-select: none;
  width: fit-content;
  max-width: 100%;
}

[data-theme="dark"] .free-layout-toolbar {
  background: #1e1e24;
  border-color: rgba(255,255,255,0.08);
}

/* Toolbar buttons: toggle + action buttons */
.free-layout-toggle,
.free-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary, #666);
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
  white-space: nowrap;
}

.free-layout-toggle:hover,
.free-action-btn:hover {
  background: var(--hover-bg, rgba(0,0,0,0.06));
  color: var(--text, #1a1a1a);
}

[data-theme="dark"] .free-layout-toggle:hover,
[data-theme="dark"] .free-action-btn:hover {
  background: rgba(255,255,255,0.07);
  color: #e2e2e2;
}

/* Active / free-mode-on state for the toggle button */
.free-layout-toggle.active {
  background: rgba(99,102,241,0.10);
  border-color: rgba(99,102,241,0.25);
  color: #6366f1;
}

[data-theme="dark"] .free-layout-toggle.active {
  background: rgba(99,102,241,0.15);
  border-color: rgba(99,102,241,0.35);
  color: #a5b4fc;
}

.free-layout-toggle svg,
.free-action-btn svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

/* Flash animation for undo/redo when nothing to do */
.free-action-btn.flash-error {
  background: rgba(239,68,68,0.10) !important;
  color: #ef4444 !important;
  border-color: rgba(239,68,68,0.25) !important;
  transition: none !important;
}

/* Hint text */
.free-mode-hint {
  color: var(--text-muted, #9b9b9b);
  font-size: 11px;
  padding-left: 4px;
  font-style: italic;
  white-space: nowrap;
}

/* ── Right-edge resize handle ─────────────────────────────────────────────── */
.block-free-resize-handle {
  position: absolute;
  right: -4px;
  top: 0;
  bottom: 0;
  width: 8px;
  cursor: ew-resize;
  z-index: 10;
  border-radius: 0 8px 8px 0;
  opacity: 0;
  transition: opacity 0.15s ease, background 0.15s ease;
}
.block-free-resize-handle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 3px;
  height: 28px;
  border-radius: 2px;
  background: var(--primary, #6366f1);
  opacity: 0.6;
}
.block-free:hover .block-free-resize-handle {
  opacity: 1;
}
.block-free-resize-handle:hover {
  background: rgba(99,102,241,0.08);
  opacity: 1;
}

/* ── Responsive: tighten on small screens ────────────────────────────────── */
@media (max-width: 600px) {
  .block-free {
    width: calc(100vw - 48px);
    max-width: calc(100vw - 48px);
  }

  .free-mode-hint {
    display: none;
  }

  .free-layout-toolbar {
    padding: 4px 6px;
    gap: 4px;
  }

  .free-layout-toggle span,
  .free-action-btn span {
    display: none;
  }

  .free-layout-toggle,
  .free-action-btn {
    padding: 4px 6px;
  }
}
