/* ============================================================================
   GRID SYSTEM - Core Layout
   ============================================================================
   
   Architecture:
   - Grid container with 3 fixed columns
   - Each slot is a perfect square (aspect-ratio: 1/1)
   - Slots are the CONSTANT - they never change size or position
   - Blocks cover slots completely when placed
   - Content adapts to block size, scrolls if needed
   
   ============================================================================ */

:root {
  --grid-columns: 3;
  --grid-gap: 16px;
  --grid-slot-border: 2px;
  --grid-slot-radius: 12px;
}

/* ============================================================================
   Grid Container
   ============================================================================ */

.grid-container {
  display: grid;
  grid-template-columns: repeat(var(--grid-columns), 1fr);
  gap: var(--grid-gap);
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--grid-gap);
}

/* ============================================================================
   Grid Slots - The Constant Foundation
   ============================================================================ */

.grid-slot {
  /* Square aspect ratio - this is the CONSTANT */
  aspect-ratio: 1 / 1;
  
  /* CRITICAL: Prevent content from affecting slot size */
  contain: strict;
  overflow: hidden;
  
  /* Visual appearance for empty slots */
  background: rgba(var(--color-bg-card-rgb, 30, 41, 59), 0.3);
  border: var(--grid-slot-border) dashed rgba(var(--color-border-light-rgb, 255, 255, 255), 0.2);
  border-radius: var(--grid-slot-radius);
  
  /* Positioning */
  position: relative;
  
  /* Interaction */
  cursor: pointer;
  transition: all 0.2s ease;
}

/* Empty slot hover state */
.grid-slot:hover {
  background: rgba(var(--color-bg-card-rgb, 30, 41, 59), 0.5);
  border-color: rgba(var(--color-primary-rgb, 99, 102, 241), 0.5);
}

/* Add button inside empty slots */
.grid-slot::before {
  content: '+';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 2rem;
  font-weight: 300;
  color: rgba(var(--color-text-muted-rgb, 148, 163, 184), 0.5);
  transition: all 0.2s ease;
  pointer-events: none;
}

.grid-slot:hover::before {
  color: rgba(var(--color-primary-rgb, 99, 102, 241), 0.8);
  transform: translate(-50%, -50%) scale(1.2);
}

/* Slot position indicator (for debugging, hidden by default) */
.grid-slot::after {
  content: attr(data-row) ',' attr(data-col);
  position: absolute;
  bottom: 8px;
  right: 8px;
  font-size: 0.625rem;
  font-family: monospace;
  color: rgba(var(--color-text-muted-rgb, 148, 163, 184), 0.3);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}

.grid-container.debug .grid-slot::after {
  opacity: 1;
}

/* ============================================================================
   Covered Slots - Hidden when block is placed
   ============================================================================ */

.grid-slot.covered {
  /* Completely hidden - block covers this slot */
  visibility: hidden;
  pointer-events: none;
}

/* ============================================================================
   Grid Blocks - Content that covers slots
   ============================================================================ */

.grid-block {
  /* Use CSS Grid positioning - matches slot sizes exactly */
  /* grid-column and grid-row set via inline styles */
  
  /* CRITICAL: Constrain block to grid cell dimensions */
  /* aspect-ratio based on spans (JS sets this or default 1/1) */
  aspect-ratio: var(--block-aspect-ratio, 1 / 1);
  contain: layout style; /* Prevent content from affecting size */
  
  /* Visual appearance - BRIGHT Glowing blue outline style */
  background: rgba(var(--color-bg-card-rgb, 30, 41, 59), 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(var(--color-primary-rgb, 99, 102, 241), 0.6);
  border-radius: var(--grid-slot-radius);
  box-shadow: 
    0 0 15px rgba(var(--color-primary-rgb, 99, 102, 241), 0.3),
    0 0 30px rgba(var(--color-primary-rgb, 99, 102, 241), 0.15),
    inset 0 0 20px rgba(var(--color-primary-rgb, 99, 102, 241), 0.05),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
  
  /* Layout - position relative for overlay header */
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden; /* Prevent content overflow */
  
  /* Layering */
  z-index: 10;
  
  /* Transitions */
  transition: all 0.2s ease;
}

.grid-block:hover {
  border-color: rgba(var(--color-primary-rgb, 99, 102, 241), 0.9);
  box-shadow: 
    0 0 20px rgba(var(--color-primary-rgb, 99, 102, 241), 0.5),
    0 0 40px rgba(var(--color-primary-rgb, 99, 102, 241), 0.3),
    0 8px 32px rgba(0, 0, 0, 0.3),
    inset 0 0 30px rgba(var(--color-primary-rgb, 99, 102, 241), 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.15);
}

/* Block selected state */
.grid-block.selected {
  border-color: var(--color-primary, #6366f1);
  box-shadow: 
    0 0 40px rgba(var(--color-primary-rgb, 99, 102, 241), 0.4),
    0 0 0 2px rgba(var(--color-primary-rgb, 99, 102, 241), 0.3);
}

/* Block dragging state */
.grid-block.dragging {
  opacity: 0.8;
  cursor: grabbing;
  z-index: 1000;
  box-shadow: 
    0 0 50px rgba(var(--color-primary-rgb, 99, 102, 241), 0.5),
    0 20px 60px rgba(0, 0, 0, 0.5);
}

/* Block resizing state */
.grid-block.resizing {
  z-index: 1000;
  border-color: var(--color-primary, #6366f1);
  box-shadow: 
    0 0 40px rgba(var(--color-primary-rgb, 99, 102, 241), 0.5),
    0 0 0 2px rgba(var(--color-primary-rgb, 99, 102, 241), 0.5);
}

/* ============================================================================
   Block Content Area - Scrollable
   ============================================================================ */

.grid-block-content {
  flex: 1 1 0; /* Grow, shrink, and start with 0 basis */
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0; /* CRITICAL: Allow shrinking in flex container */
  max-height: 100%; /* Prevent expansion beyond parent */
  padding: 12px;
  color: var(--color-text-primary, #f1f5f9);
}

/* Custom scrollbar for block content */
.grid-block-content::-webkit-scrollbar {
  width: 6px;
}

.grid-block-content::-webkit-scrollbar-track {
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.1);
  border-radius: 3px;
}

.grid-block-content::-webkit-scrollbar-thumb {
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.4);
  border-radius: 3px;
}

.grid-block-content::-webkit-scrollbar-thumb:hover {
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.6);
}

/* ============================================================================
   Block Controls - Overlay header (WYSIWYG - doesn't take up content space)
   ============================================================================ */

.grid-block-header {
  /* Position as overlay at top of block */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  
  /* Layout - buttons aligned to the right */
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 8px 10px;
  
  /* Transparent background */
  background: transparent;
  border-bottom: none;
  border-radius: var(--grid-slot-radius) var(--grid-slot-radius) 0 0;
  
  /* Hidden by default - show on hover */
  opacity: 0;
  transform: translateY(-4px);
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

/* Show header on block hover (only when NOT editing) */
.grid-block:hover:not([data-editing="true"]) .grid-block-header {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

/* Hide header completely when editing - buttons move inside block */
.grid-block[data-editing="true"] .grid-block-header {
  display: none;
}

/* ============================================================================
   Edit Mode: Floating buttons inside block corners
   ============================================================================ */

/* Container for edit mode buttons - positioned inside the block (top-right) */
.grid-block-edit-controls {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 100;
  display: none;
  gap: 6px;
}

/* Show edit controls only when editing */
.grid-block[data-editing="true"] .grid-block-edit-controls {
  display: flex;
}

/* Inline edit controls buttons */
.grid-block-edit-controls .btn-action {
  all: unset;
  box-sizing: border-box;
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
  display: flex !important;
  align-items: center;
  justify-content: center;
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.15);
  border: 1px solid rgba(var(--color-primary-rgb, 99, 102, 241), 0.5);
  border-radius: 6px;
  color: #fff;
  cursor: pointer;
  transition: all 0.2s ease;
  padding: 0;
  margin: 0;
}

.grid-block-edit-controls .btn-action:hover {
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.4);
  border-color: var(--color-primary, #6366f1);
  box-shadow: 0 0 20px rgba(var(--color-primary-rgb, 99, 102, 241), 0.6);
}

.grid-block-edit-controls .btn-action svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  display: block;
}

/* Save button - green */
.grid-block-edit-controls .btn-edit-save-inline {
  color: #4ade80;
  border-color: rgba(74, 222, 128, 0.4);
  background: rgba(74, 222, 128, 0.1);
}

.grid-block-edit-controls .btn-edit-save-inline:hover {
  color: #fff;
  border-color: #4ade80;
  background: rgba(74, 222, 128, 0.4);
  box-shadow: 0 0 20px rgba(74, 222, 128, 0.5);
}

/* Delete button - red */
.grid-block-edit-controls .btn-delete-inline {
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.4);
  background: rgba(239, 68, 68, 0.1);
}

.grid-block-edit-controls .btn-delete-inline:hover {
  color: #fff;
  border-color: #ef4444;
  background: rgba(239, 68, 68, 0.4);
  box-shadow: 0 0 20px rgba(239, 68, 68, 0.5);
}

.grid-block-header .block-type-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--color-primary, #6366f1);
  text-transform: uppercase;
  letter-spacing: 1px;
  text-shadow: 0 0 15px rgba(var(--color-primary-rgb, 99, 102, 241), 0.8);
}

.grid-block-header .block-actions {
  display: flex;
  gap: 8px;
}

.grid-block-header .btn-action {
  /* Reset Pico CSS button styles */
  all: unset;
  box-sizing: border-box;
  
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
  display: flex !important;
  align-items: center;
  justify-content: center;
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.15);
  border: 1px solid rgba(var(--color-primary-rgb, 99, 102, 241), 0.5);
  border-radius: 6px;
  color: #fff;
  cursor: pointer;
  transition: all 0.2s ease;
  padding: 0;
  margin: 0;
}

.grid-block-header .btn-action:hover {
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.4);
  border-color: var(--color-primary, #6366f1);
  color: #fff;
  box-shadow: 0 0 20px rgba(var(--color-primary-rgb, 99, 102, 241), 0.6);
}

.grid-block-header .btn-action svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  /* Ensure SVG is visible */
  display: block;
}

/* Drag handle */
.grid-block-header .btn-drag {
  cursor: grab;
}

.grid-block-header .btn-drag:active {
  cursor: grabbing;
}

/* Edit/Save button - changes based on state */
.grid-block-header .btn-action.btn-edit-save {
  color: var(--color-primary, #6366f1);
}

.grid-block-header .btn-action.btn-edit-save:hover {
  color: #fff;
}

/* Save state - green */
.grid-block-header .btn-action.btn-edit-save[data-editing="true"] {
  color: #4ade80;
  border-color: rgba(74, 222, 128, 0.4);
  background: rgba(74, 222, 128, 0.1);
}

.grid-block-header .btn-action.btn-edit-save[data-editing="true"]:hover {
  color: #fff;
  border-color: #4ade80;
  background: rgba(74, 222, 128, 0.4);
  box-shadow: 0 0 20px rgba(74, 222, 128, 0.5);
}

/* Delete button - red accent */
.grid-block-header .btn-action.btn-delete {
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.4);
  background: rgba(239, 68, 68, 0.1);
}

.grid-block-header .btn-action.btn-delete:hover {
  color: #fff;
  border-color: #ef4444;
  background: rgba(239, 68, 68, 0.4);
  box-shadow: 0 0 20px rgba(239, 68, 68, 0.5);
}

/* ============================================================================
   Resize Handle
   ============================================================================ */

.grid-block-resize {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 24px;
  height: 24px;
  cursor: nwse-resize;
  z-index: 20;
  
  /* Glowing triangle corner indicator */
  background: linear-gradient(
    -45deg,
    var(--color-primary, #6366f1) 0%,
    var(--color-primary, #6366f1) 50%,
    transparent 50%
  );
  border-radius: 0 0 var(--grid-slot-radius) 0;
  opacity: 0.8;
  
  transition: all 0.2s ease;
}

.grid-block-resize:hover {
  opacity: 1;
  width: 28px;
  height: 28px;
  filter: drop-shadow(0 0 8px rgba(var(--color-primary-rgb, 99, 102, 241), 0.8));
}

/* Show resize handle more prominently when block is hovered */
.grid-block:hover .grid-block-resize {
  opacity: 1;
  filter: drop-shadow(0 0 4px rgba(var(--color-primary-rgb, 99, 102, 241), 0.5));
}

/* ============================================================================
   Resize Preview Overlay
   ============================================================================ */

.grid-resize-preview {
  /* Uses CSS Grid positioning - matches slots exactly */
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.1);
  border: 2px dashed var(--color-primary, #6366f1);
  border-radius: var(--grid-slot-radius);
  pointer-events: none;
  z-index: 999;
}

/* Invalid resize indicator */
.grid-resize-preview.invalid {
  background: rgba(239, 68, 68, 0.1);
  border-color: #ef4444;
}

/* ============================================================================
   Drop Target Indicator
   ============================================================================ */

.grid-drop-target {
  /* Uses CSS Grid positioning - matches slots exactly */
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.15);
  border: 2px solid var(--color-primary, #6366f1);
  border-radius: var(--grid-slot-radius);
  pointer-events: none;
  z-index: 998;
  animation: pulse-border 1s ease-in-out infinite alternate;
}

@keyframes pulse-border {
  from {
    border-color: rgba(var(--color-primary-rgb, 99, 102, 241), 0.5);
  }
  to {
    border-color: var(--color-primary, #6366f1);
  }
}

/* ============================================================================
   Responsive - Maintain square slots at all sizes
   ============================================================================ */

/* Tablet: Keep 3 columns but smaller */
@media (max-width: 1024px) {
  .grid-container {
    max-width: 100%;
    padding: 12px;
  }
  
  :root {
    --grid-gap: 12px;
  }
}

/* Mobile: Single column, still square */
@media (max-width: 640px) {
  .grid-container {
    grid-template-columns: 1fr;
    padding: 8px;
  }
  
  :root {
    --grid-gap: 8px;
  }
  
  /* Limit slot height on mobile so they're not too tall */
  .grid-slot {
    max-height: 300px;
  }
}

/* ============================================================================
   Viewer Mode Styles
   ============================================================================ */

.grid-container.viewer-mode {
  /* Match editor gap for consistency */
  gap: var(--spacing-lg);
}

.grid-container.viewer-mode .grid-slot.viewer-slot {
  /* Hidden slots for maintaining grid structure */
  border: none;
  background: transparent;
  pointer-events: none;
}

.grid-block.viewer-block,
.grid-block.viewer-block:hover,
.grid-block.viewer-block:focus,
.grid-block.viewer-block:active {
  /* Viewer blocks - clean appearance with no glow */
  cursor: default;
  transition: none !important;
  border: none !important;
  box-shadow: none !important;
  background: transparent !important;
  transform: none !important;
}

/* Hide resize handle in viewer */
.grid-block.viewer-block .grid-block-resize {
  display: none !important;
}

.grid-block.viewer-block .grid-block-content {
  /* Use flex sizing - inherited from base .grid-block-content */
  padding: 16px;
}

/* Custom scrollbar for viewer blocks */
.grid-block.viewer-block .grid-block-content::-webkit-scrollbar {
  width: 6px;
}

.grid-block.viewer-block .grid-block-content::-webkit-scrollbar-track {
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.1);
  border-radius: 3px;
}

.grid-block.viewer-block .grid-block-content::-webkit-scrollbar-thumb {
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.4);
  border-radius: 3px;
}

.grid-block.viewer-block .grid-block-content::-webkit-scrollbar-thumb:hover {
  background: rgba(var(--color-primary-rgb, 99, 102, 241), 0.6);
}

/* Text content inside viewer blocks - no extra height constraints */
.grid-block.viewer-block .grid-block-content .text-block-content {
  font-size: 0.9375rem;
  line-height: 1.6;
  color: var(--color-text-primary, #f1f5f9);
}

.grid-block.viewer-block .grid-block-content .text-block-content p {
  margin: 0 0 0.75em 0;
}

.grid-block.viewer-block .grid-block-content .text-block-content p:last-child {
  margin-bottom: 0;
}

