/* =======================================================
   dashboard.css (CONSOLIDATED)
   - Combines dashboard-grid.css + your extra dropzone/widget rules
   - Removes conflicting duplicates
   - Prevents drag/resize jank + flex overflow issues
   ======================================================= */

/* =======================================================
   Dashboard Layout
   ======================================================= */

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-auto-rows: 100px;
  gap: 16px;
  padding: 20px;
  min-height: 100vh;
  background-color: #f8fafc;
}

.dashboard-version {
  position: relative;
  min-height: 100vh;
  height: auto;
  width: 100%;
  background-color: #f8fafc;
  padding: 20px;

  /* Drop zone feedback */
  transition: background-color 0.2s ease, border 0.2s ease;
}

/* =======================================================
   Panzoom states
   ======================================================= */

.dashboard-version.panzoom-enabled {
  cursor: grab;
}

.dashboard-version.panzoom-enabled:active {
  cursor: grabbing;
}

/* =======================================================
   Pointer-events hierarchy
   ======================================================= */

.widget {
  pointer-events: auto;
}

/* When panzoom is enabled, disable widget interactions */
.dashboard-version.panzoom-enabled .widget {
  pointer-events: none !important;
}

/* In edit mode with panzoom disabled, ensure widgets are interactive */
.dashboard-edit-mode .dashboard-version:not(.panzoom-enabled) .widget {
  pointer-events: auto !important;
}

/* =======================================================
   Widget Base Styles (single authoritative definition)
   ======================================================= */

.widget {
  position: absolute;
  background: white;
  border-radius: 8px;

  box-shadow:
    0 4px 6px -1px rgba(0, 0, 0, 0.1),
    0 2px 4px -1px rgba(0, 0, 0, 0.06);

  border: 4px solid transparent;

  /* IMPORTANT:
     Do NOT transition transform/width/height globally — interact.js
     updates those during drag/resize and it causes jank */
  transition: box-shadow 0.2s ease, border-color 0.2s ease,
    background-color 0.2s ease, opacity 0.2s ease;

  cursor: default;
  overflow: visible;
  display: flex;
  flex-direction: column;

  /* Prevent flex from overriding explicit sizes */
  flex-shrink: 0;
  flex-grow: 0;

  /* Ensure explicit dimensions are respected */
  box-sizing: border-box;

  /* Prevent content from forcing expansion */
  min-width: 0;
  min-height: 0;
}

/* ONLY suppress transition for newly dropped widgets */
.widget.widget-placing,
.widget.widget-just-dropped {
  transition: none !important;
}

/* Widget hover state */
.widget:hover {
  box-shadow:
    0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

/* =======================================================
   Edit mode styles
   ======================================================= */

.dashboard-edit-mode .widget {
  border-color: #e0e7ff;
  cursor: move;
}

.dashboard-edit-mode .widget:hover {
  border-color: #c7d2fe;
}

.dashboard-edit-mode .widget.selected {
  border-color: #6366f1;
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

/* =======================================================
   Dragging / Resizing states (critical transition fix)
   ======================================================= */

.widget.dragging,
.widget.resizing {
  transition: none !important;
}

.widget.dragging {
  opacity: 0.8;
  cursor: grabbing !important;
  z-index: 1000;
}

.widget.dragging * {
  pointer-events: none;
}

.widget.resizing {
  opacity: 0.9;
  z-index: 1000;
}

/* If you use interact.js default dragging class */
.drag-dragging {
  opacity: 0.5;
  z-index: 10;
}

/* =======================================================
   Widget content structure
   ======================================================= */

.widget-header {
  padding: 16px;
  border-bottom: 1px solid #e5e7eb;
  background: #f9fafb;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: inherit;
  flex-shrink: 0;
}

.widget-title {
  font-weight: 600;
  color: #374151;
  margin: 0;
  pointer-events: none;
}

.widget-content {
  flex: 1;
  padding: 16px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  cursor: inherit;
  position: relative;
  z-index: 1;

  /* CRITICAL for flex children:
     Allows scrolling inside without forcing widget growth */
  min-height: 0;
  min-width: 0;
}

/* =======================================================
   Widget action buttons (edit mode)
   ======================================================= */

.widget-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s ease;
  z-index: 10;
}

.dashboard-edit-mode .widget:hover .widget-actions {
  opacity: 1;
}

.widget-action-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s ease;
  pointer-events: auto;
}

.widget-delete-btn {
  background: #ef4444;
  color: white;
}

.widget-delete-btn:hover {
  background: #dc2626;
}

/* =======================================================
   Resize handles
   ======================================================= */

.widget-resize-handle {
  position: absolute;
  background: #6366f1;
  opacity: 0;
  transition: opacity 0.2s ease, background 0.2s ease;
  pointer-events: none;
  z-index: 100;
}

.dashboard-edit-mode .widget-resize-handle {
  pointer-events: auto !important;
  opacity: 0;
}

.dashboard-edit-mode .widget:hover .widget-resize-handle {
  opacity: 0.7;
  background: rgba(99, 102, 241, 0.6);
}

.dashboard-edit-mode .widget-resize-handle:hover {
  opacity: 1 !important;
  background: #6366f1 !important;
}

.dashboard-version.panzoom-enabled .widget-resize-handle {
  pointer-events: none !important;
  opacity: 0 !important;
}

/* Handle positioning */
.widget-resize-handle.se {
  bottom: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  cursor: se-resize;
  border-radius: 0 0 8px 0;
}

.widget-resize-handle.sw {
  bottom: -8px;
  left: -8px;
  width: 24px;
  height: 24px;
  cursor: sw-resize;
  border-radius: 0 0 0 8px;
}

.widget-resize-handle.nw {
  top: -8px;
  left: -8px;
  width: 24px;
  height: 24px;
  cursor: nw-resize;
  border-radius: 8px 0 0 0;
}

.widget-resize-handle.ne {
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  cursor: ne-resize;
  border-radius: 0 8px 0 0;
}

.widget-resize-handle.n {
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 8px;
  cursor: n-resize;
  border-radius: 4px;
}

.widget-resize-handle.s {
  bottom: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 8px;
  cursor: s-resize;
  border-radius: 4px;
}

.widget-resize-handle.e {
  right: -4px;
  top: 50%;
  transform: translateY(-50%);
  width: 8px;
  height: 60px;
  cursor: e-resize;
  border-radius: 4px;
}

.widget-resize-handle.w {
  left: -4px;
  top: 50%;
  transform: translateY(-50%);
  width: 8px;
  height: 60px;
  cursor: w-resize;
  border-radius: 4px;
}

.dashboard-edit-mode .widget {
  z-index: 1;
}

.dashboard-edit-mode .widget:hover {
  z-index: 10;
}

.dashboard-edit-mode .widget-resize-handle {
  z-index: 100 !important;
}

/* =======================================================
   Drop zone feedback (dashboard container)
   ======================================================= */

.dashboard-version.drag-over {
  background: rgba(99, 102, 241, 0.1) !important;
  border: 2px dashed #6366f1;
  transition: all 0.2s ease;
}

.dashboard-version.drop-invalid {
  background: rgba(239, 68, 68, 0.1) !important;
  border: 2px dashed #ef4444;
}

/* Generic dropzone class (used elsewhere) */
.dropzone {
  transition: background-color 0.3s ease;
}

/* Generic interact drop states */
.drop-active {
  border-color: #4f46e5;
}

.drop-target {
  background-color: #e0e7ff;
  border-color: #818cf8;
}

/* =======================================================
   Canvas + grid
   ======================================================= */

.canvas-container {
  position: relative;
  overflow: hidden;

  /* Used for pan/zoom: prevents default touch gestures */
  touch-action: none;
}

.grid-canvas {
  pointer-events: none !important;
  z-index: -1 !important;
}

/* =======================================================
   Widget-not-configured UI
   ======================================================= */

.widget-not-configured {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #6b7280;
  font-style: italic;
}

.widget-not-configured-table {
  border: 2px dashed #9ca3af;
  font-family: monospace;
  font-size: 14px;
}

.widget-not-configured-table th {
  background: #f3f4f6;
  padding: 8px 16px;
  text-align: center;
  font-weight: 600;
  color: #374151;
}

.widget-not-configured-table td {
  padding: 8px 16px;
  color: #6b7280;
  text-align: center;
}

.widget-configure-hint {
  font-size: 12px;
  color: #6366f1;
  font-weight: 600;
  margin-top: 8px;
}

/* =======================================================
   Chart canvas styles
   ======================================================= */

.widget-content canvas {
  width: 100% !important;
  height: 100% !important;
}

/* =======================================================
   Text widget styles
   ======================================================= */

.text-widget-content {
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.5;
}

.text-widget-editor {
  width: 100%;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  padding: 8px;
  resize: none;
  font-family: inherit;
  font-size: inherit;
}

.text-widget-editor:focus {
  outline: none;
  border-color: #6366f1;
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.text-widget-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.text-widget-btn {
  padding: 4px 12px;
  font-size: 14px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.text-widget-btn-cancel {
  background: #e5e7eb;
  color: #374151;
}

.text-widget-btn-cancel:hover {
  background: #d1d5db;
}

.text-widget-btn-save {
  background: #6366f1;
  color: white;
}

.text-widget-btn-save:hover {
  background: #5048e5;
}

/* =======================================================
   Rich text widget styles
   ======================================================= */

.rich-text-widget .ql-editor {
  font-size: 14px;
  line-height: 1.5;
}

.rich-text-widget .ql-toolbar {
  border-top: none;
  border-left: none;
  border-right: none;
}

/* =======================================================
   Filter overlay sizing safety (placeholder)
   ======================================================= */

.widget-filter-dropzone {
  /* Positioned by JS, but keeping selector here avoids other CSS
     accidentally giving it layout-affecting rules. */
}

/* =======================================================
   Interaction Mode (Link Mode)
   ======================================================= */

/* Visual emphasis on widgets */
body.interaction-mode-active .dashboard-version .widget {
  border-color: #d97706 !important;
  border-width: 4px !important;
  box-shadow: 0 0 0 4px rgba(217, 119, 6, 0.2) !important;
}

/* Disable resize handles in link mode */
body.interaction-mode-active .dashboard-version .widget-resize-handle {
  display: none !important;
  pointer-events: none !important;
}

/* Handle styling */
.widget .interaction-handle-btn {
  position: relative;
  z-index: 20;
  pointer-events: auto !important;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-drag: element;
}

/* CSS backup to show handle when mode active */
body.interaction-mode-active .interaction-handle-btn {
  display: inline-flex !important;
}

/* Highlights */
body.interaction-mode-active .widget.ring-blue-400 {
  box-shadow: 0 0 0 3px #3b82f6 !important;
  z-index: 100 !important;
}

body.interaction-mode-active .widget.ring-green-400 {
  box-shadow: 0 0 0 3px #10b981 !important;
  z-index: 80 !important;
  background: rgba(16, 185, 129, 0.05) !important;
}

/* SVG + knot layers */
#interaction-svg-layer {
  pointer-events: none !important;
  z-index: 30;
}

#interaction-knots-layer {
  pointer-events: none !important;
  z-index: 35;
}

#interaction-knots-layer .interaction-knot {
  pointer-events: auto !important;
}

/* Knot icon hover */
.knot-icon {
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.knot-icon:hover {
  transform: scale(1.15);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
}

/* Expanded knot panel */
.knot-panel,
.knot-panel * {
  pointer-events: auto !important;
}

.knot-panel {
  cursor: move;
}

.knot-panel:active {
  cursor: grabbing;
}

/* Knot appear animation */
@keyframes knot-appear {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.5);
  }
  50% {
    opacity: 0.8;
    transform: translate(-50%, -50%) scale(1.05);
  }
  100% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

.interaction-knot {
  animation: knot-appear 0.25s ease-out;
}

/* =======================================================
   Animations
   ======================================================= */

.widget-fade-in {
  animation: fadeIn 0.3s ease-in;
}

.widget-fade-out {
  animation: fadeOut 0.3s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
    transform: scale(1);
  }
  to {
    opacity: 0;
    transform: scale(0.95);
  }
}

/* =======================================================
   Responsive adjustments
   ======================================================= */

@media (max-width: 768px) {
  .dashboard-grid {
    grid-template-columns: repeat(6, 1fr);
    gap: 12px;
    padding: 12px;
  }

  .widget {
    border-width: 2px;
  }

  .widget-header {
    padding: 12px;
  }

  .widget-content {
    padding: 12px;
  }
}

@media (max-width: 480px) {
  .dashboard-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    padding: 8px;
  }
}