/* ============================================================================
   ProLayout - Core Styles
   Enterprise-grade layout component with strict bounds enforcement
   ============================================================================ */

/* CSS Custom Properties (Theme Integration) */
:root {
    /* Container */
    --pro-layout-bg: var(--pc-color-background, #ffffff);
    --pro-layout-border: var(--pc-color-border, #e0e0e0);

    /* Item chrome */
    --pro-layout-item-bg: var(--pc-color-surface, #ffffff);
    --pro-layout-item-header-bg: var(--pc-color-primary, #1976d2);
    --pro-layout-item-header-text: #ffffff;
    --pro-layout-item-border: var(--pc-color-border, #e0e0e0);
    --pro-layout-item-shadow: var(--pc-shadow-md, 0 2px 4px rgba(0,0,0,0.1));

    /* Drag/resize states */
    --pro-layout-item-dragging-opacity: 0.8;
    --pro-layout-item-dragging-shadow: var(--pc-shadow-lg, 0 8px 16px rgba(0,0,0,0.2));
    --pro-layout-resize-handle-size: 8px;
    --pro-layout-resize-handle-color: var(--pc-color-primary, #1976d2);

    /* Grid overlay */
    --pro-layout-grid-color: rgba(25, 118, 210, 0.1);
    --pro-layout-grid-line-width: 1px;

    /* Minimized tray */
    --pro-layout-tray-bg: var(--pc-color-surface, #f5f5f5);
    --pro-layout-tray-height: 48px;

    /* Animation */
    --pro-layout-animation-duration: var(--pc-transition-duration, 200ms);
    --pro-layout-animation-easing: var(--pc-transition-easing, cubic-bezier(0.4, 0.0, 0.2, 1));
}

/* ============================================================================
   ProLayoutContainer - The Unescapable Canvas
   ============================================================================ */

.pro-layout-container {
    position: relative;
    min-width: 100%;
    min-height: 100%;
    background-color: var(--pro-layout-bg);
    border: 1px solid var(--pro-layout-border);
    box-sizing: border-box;
    /* Container grows with content, parent provides scroll */
    
    /* Custom Scrollbars - Thin, theme-aware (for when container has overflow) */
    /* Firefox */
    scrollbar-width: thin;
    scrollbar-color: var(--pc-color-border, #e0e0e0) transparent;
}

/* Webkit browsers (Chrome, Safari, Edge) - Container scrollbars */
.pro-layout-container::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

.pro-layout-container::-webkit-scrollbar-track {
    background: transparent;
}

.pro-layout-container::-webkit-scrollbar-thumb {
    background-color: var(--pc-color-border, #e0e0e0);
    border-radius: 4px;
    transition: background-color 0.2s ease;
}

.pro-layout-container::-webkit-scrollbar-thumb:hover {
    background-color: var(--pc-color-text-secondary, #757575);
}

.pro-layout-container::-webkit-scrollbar-thumb:active {
    background-color: var(--pc-color-primary, #1976d2);
}

.pro-layout-container::-webkit-scrollbar-corner {
    background: transparent;
}

/* Scrollbar styles for the container's parent wrapper */
.pro-layout-wrapper,
.pro-layout-container-wrapper,
div:has(> .pro-layout-container) {
    /* Firefox */
    scrollbar-width: thin;
    scrollbar-color: var(--pc-color-border, #e0e0e0) transparent;
}

.pro-layout-wrapper::-webkit-scrollbar,
.pro-layout-container-wrapper::-webkit-scrollbar,
div:has(> .pro-layout-container)::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

.pro-layout-wrapper::-webkit-scrollbar-track,
.pro-layout-container-wrapper::-webkit-scrollbar-track,
div:has(> .pro-layout-container)::-webkit-scrollbar-track {
    background: transparent;
}

.pro-layout-wrapper::-webkit-scrollbar-thumb,
.pro-layout-container-wrapper::-webkit-scrollbar-thumb,
div:has(> .pro-layout-container)::-webkit-scrollbar-thumb {
    background-color: var(--pc-color-border, #e0e0e0);
    border-radius: 4px;
    transition: background-color 0.2s ease;
}

.pro-layout-wrapper::-webkit-scrollbar-thumb:hover,
.pro-layout-container-wrapper::-webkit-scrollbar-thumb:hover,
div:has(> .pro-layout-container)::-webkit-scrollbar-thumb:hover {
    background-color: var(--pc-color-text-secondary, #757575);
}

.pro-layout-wrapper::-webkit-scrollbar-thumb:active,
.pro-layout-container-wrapper::-webkit-scrollbar-thumb:active,
div:has(> .pro-layout-container)::-webkit-scrollbar-thumb:active {
    background-color: var(--pc-color-primary, #1976d2);
}

.pro-layout-wrapper::-webkit-scrollbar-corner,
.pro-layout-container-wrapper::-webkit-scrollbar-corner,
div:has(> .pro-layout-container)::-webkit-scrollbar-corner {
    background: transparent;
}

/* Grid overlay (shown during drag/resize) */
.pro-layout-container__grid {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 9998; /* Below dragging items but above normal items */
}

/* Minimized items tray - fixed to viewport bottom */
.pro-layout-container__tray {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--pro-layout-tray-height);
    background-color: var(--pro-layout-tray-bg);
    border-top: 1px solid var(--pro-layout-border);
    display: flex;
    align-items: center;
    padding: 0 8px;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    z-index: 9997; /* Above grid but below items */
    box-sizing: border-box;
}

.pro-layout-container__tray-item {
    flex: 0 0 auto;
    height: 32px;
    padding: 0 16px;
    background-color: var(--pro-layout-item-bg);
    border: 1px solid var(--pro-layout-border);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--pc-color-text, #000000);
}

.pro-layout-container__tray-item:hover {
    background-color: var(--pc-color-hover, #f0f0f0);
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.pro-layout-container__tray-restore-all {
    flex: 0 0 auto;
    height: 32px;
    padding: 0 16px;
    background-color: var(--pc-color-primary, #1976d2);
    color: #ffffff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 500;
    margin-right: 12px;
}

.pro-layout-container__tray-restore-all:hover {
    background-color: var(--pc-color-primary-dark, #1565c0);
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

.pro-layout-container__tray-restore-all i {
    font-size: 16px;
}

.pro-layout-container__tray-item--removed {
    opacity: 0.6;
    border-style: dashed;
}

.pro-layout-container__tray-item--removed:hover {
    opacity: 1;
}

.pro-layout-container__tray-separator {
    width: 2px;
    height: 24px;
    background-color: var(--pc-color-border);
    margin: 0 8px;
    flex-shrink: 0;
}

.pro-layout-container__tray-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
}

/* ============================================================================
   ProLayoutItem - The Strict Container
   ============================================================================ */

.pro-layout-item {
    position: absolute;
    background-color: var(--pro-layout-item-bg);
    border: 1px solid var(--pro-layout-item-border);
    border-radius: 4px;
    box-shadow: var(--pro-layout-item-shadow);
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    overflow: hidden; /* CRITICAL: No content leakage */
    transition: box-shadow var(--pro-layout-animation-duration) var(--pro-layout-animation-easing);
}

/* Normal state positioning handled by inline styles */
.pro-layout-item--normal {
    /* Position set via inline styles */
}

/* Minimized state (hidden from canvas) */
.pro-layout-item--minimized {
    display: none;
}

/* Removed state (hidden from canvas) */
.pro-layout-item--removed {
    display: none;
}

/* Maximized state (fills container respecting padding) */
.pro-layout-item--maximized {
    left: var(--container-padding, 5px) !important;
    top: var(--container-padding, 5px) !important;
    right: var(--container-padding, 5px) !important;
    bottom: calc(var(--container-padding, 5px) + var(--pro-layout-tray-height, 48px)) !important;
    width: auto !important;
    height: auto !important;
}

/* Draggable capability */
.pro-layout-item--draggable {
    /* Cursor handled by header only */
}

.pro-layout-item--draggable:hover {
    box-shadow: var(--pro-layout-item-dragging-shadow);
}

/* Dragging state */
.pro-layout-item--dragging {
    opacity: var(--pro-layout-item-dragging-opacity);
    box-shadow: var(--pro-layout-item-dragging-shadow);
    cursor: grabbing !important;
    z-index: 9999 !important; /* Top of everything during drag */
    will-change: transform; /* GPU acceleration hint */
}

/* Resizing state */
.pro-layout-item--resizing {
    user-select: none;
    will-change: width, height, transform;
}

/* ============================================================================
   ProLayoutItem - Header
   ============================================================================ */

/* When border radius is applied via inline style, ensure header corners match */
.pro-layout-item__header {
    flex: 0 0 auto;
    height: 40px;
    background-color: var(--pro-layout-item-header-bg);
    color: var(--pro-layout-item-header-text);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 12px;
    cursor: move;
    user-select: none;
    box-sizing: border-box;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 10;
    /* Header inherits border-radius from parent for top corners */
    border-radius: inherit;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

/* Blended header style - matches card background (Grafana-style) */
.pro-layout-item--header-blended .pro-layout-item__header {
    background-color: var(--pro-layout-item-bg);
    color: var(--pc-color-text);
    border-bottom: 1px solid var(--pc-color-border);
}

/* Subtle header style - slightly different from card */
.pro-layout-item--header-subtle .pro-layout-item__header {
    background-color: var(--pc-color-surface);
    color: var(--pc-color-text);
    border-bottom: 1px solid var(--pc-color-border);
    opacity: 0.95;
}

.pro-layout-item__title {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    font-size: 14px;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: calc(100% - 120px);
    pointer-events: none;
}

.pro-layout-item__controls {
    flex: 0 0 auto;
    display: flex;
    gap: 4px;
    position: relative;
}

.pro-layout-item__controls--left {
    margin-right: auto;
}

.pro-layout-item__controls--right {
    margin-left: auto;
}

.pro-layout-item__control-btn {
    width: 24px;
    height: 24px;
    background: transparent;
    border: none;
    color: var(--pro-layout-item-header-text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 2px;
    transition: background-color 0.15s ease;
}

/* Hide hover-only buttons by default */
.pro-layout-item__control-btn--hover {
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease, background-color 0.15s ease;
}

/* Show hover-only buttons when hovering the header */
.pro-layout-item__header:hover .pro-layout-item__control-btn--hover {
    opacity: 1;
    visibility: visible;
}

.pro-layout-item__control-btn:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

.pro-layout-item__control-btn:active {
    background-color: rgba(255, 255, 255, 0.2);
}

/* Blended/Subtle header control buttons */
.pro-layout-item--header-blended .pro-layout-item__control-btn,
.pro-layout-item--header-subtle .pro-layout-item__control-btn {
    color: var(--pc-color-text);
}

.pro-layout-item--header-blended .pro-layout-item__control-btn:hover,
.pro-layout-item--header-subtle .pro-layout-item__control-btn:hover {
    background-color: var(--pc-color-hover);
}

.pro-layout-item--header-blended .pro-layout-item__control-btn:active,
.pro-layout-item--header-subtle .pro-layout-item__control-btn:active {
    background-color: var(--pc-color-active);
}

.pro-layout-item__control-icon {
    font-size: 16px;
    line-height: 1;
}

/* Context Menu - Uses fixed positioning to escape overflow clipping */
.pro-layout-item__context-menu {
    position: fixed;
    background: var(--pc-color-surface, #ffffff);
    border: 1px solid var(--pc-color-border, #e0e0e0);
    border-radius: 4px;
    box-shadow: var(--pc-shadow-lg, 0 4px 12px rgba(0,0,0,0.15));
    min-width: 180px;
    z-index: 100000;
    overflow: hidden;
}

/* Portal context menu - rendered outside all widgets at root level */
.pro-layout-item__context-menu--portal {
    z-index: 2147483647;
    background: var(--pc-color-surface, #ffffff);
}

/* Invisible backdrop to catch clicks outside the context menu */
.pro-layout-context-menu-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2147483646; /* Just below the menu */
    background: transparent;
}

/* Context Menu Header */
.pro-layout-item__context-menu-header {
    padding: 8px 12px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--pc-color-text-muted, #9e9e9e);
    background: var(--pc-color-surface, var(--pc-color-surface-hover, #f5f5f5));
    border-bottom: 1px solid var(--pc-color-border, #e0e0e0);
}

.pro-layout-item__context-menu-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 10px 16px;
    border: none;
    background: transparent;
    color: var(--pc-color-text, #212121);
    font-size: 14px;
    text-align: left;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.pro-layout-item__context-menu-item:hover {
    background: var(--pc-color-hover, #f5f5f5);
}

.pro-layout-item__context-menu-item i {
    width: 16px;
    text-align: center;
    font-size: 14px;
    color: var(--pc-color-text-secondary, #757575);
}

.pro-layout-item__context-menu-separator {
    height: 1px;
    background: var(--pc-color-border, #e0e0e0);
    margin: 4px 0;
}

/* ============================================================================
   ProLayoutItem - Content Area (Strict Overflow Control)
   ============================================================================ */

.pro-layout-item__content {
    flex: 1;
    width: 100%;
    padding: 12px;
    box-sizing: border-box;
    overflow: hidden; /* DEFAULT: No leakage */
}

/* Content overflow modes */
.pro-layout-item__content--hidden {
    overflow: hidden;
}

.pro-layout-item__content--scroll {
    overflow: scroll;
}

.pro-layout-item__content--auto-scroll {
    overflow: auto;
}

/* Custom Scrollbars - Thin, theme-aware */
.pro-layout-item__content--scroll,
.pro-layout-item__content--auto-scroll {
    /* Firefox */
    scrollbar-width: thin;
    scrollbar-color: var(--pc-color-border, #e0e0e0) transparent;
}

/* Webkit browsers (Chrome, Safari, Edge) */
.pro-layout-item__content--scroll::-webkit-scrollbar,
.pro-layout-item__content--auto-scroll::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.pro-layout-item__content--scroll::-webkit-scrollbar-track,
.pro-layout-item__content--auto-scroll::-webkit-scrollbar-track {
    background: transparent;
}

.pro-layout-item__content--scroll::-webkit-scrollbar-thumb,
.pro-layout-item__content--auto-scroll::-webkit-scrollbar-thumb {
    background-color: var(--pc-color-border, #e0e0e0);
    border-radius: 3px;
    transition: background-color 0.2s ease;
}

.pro-layout-item__content--scroll::-webkit-scrollbar-thumb:hover,
.pro-layout-item__content--auto-scroll::-webkit-scrollbar-thumb:hover {
    background-color: var(--pc-color-text-secondary, #757575);
}

.pro-layout-item__content--scroll::-webkit-scrollbar-thumb:active,
.pro-layout-item__content--auto-scroll::-webkit-scrollbar-thumb:active {
    background-color: var(--pc-color-primary, #1976d2);
}

.pro-layout-item__content--scroll::-webkit-scrollbar-corner,
.pro-layout-item__content--auto-scroll::-webkit-scrollbar-corner {
    background: transparent;
}

/* Loading overlay */
.pro-layout-item__loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--pc-color-surface, rgba(255, 255, 255, 0.9));
    backdrop-filter: blur(2px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    z-index: 100;
}

.pro-layout-item__loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--pc-color-border, #e0e0e0);
    border-top-color: var(--pc-color-primary, #1976d2);
    border-radius: 50%;
    animation: pro-layout-spin 1s linear infinite;
}

.pro-layout-item__loading-text {
    font-size: 14px;
    color: var(--pc-color-text, #000000);
    font-weight: 500;
}

@keyframes pro-layout-spin {
    to { transform: rotate(360deg); }
}

/* ============================================================================
   ProLayoutItem - Resize Handles (8-point)
   ============================================================================ */

.pro-layout-item__resize-handle {
    position: absolute;
    background-color: transparent;
    z-index: 10;
}

/* Edge handles */
.pro-layout-item__resize-n {
    top: 0;
    left: 0;
    right: 0;
    height: var(--pro-layout-resize-handle-size);
    cursor: ns-resize;
}

.pro-layout-item__resize-e {
    top: 0;
    right: 0;
    bottom: 0;
    width: var(--pro-layout-resize-handle-size);
    cursor: ew-resize;
}

.pro-layout-item__resize-s {
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--pro-layout-resize-handle-size);
    cursor: ns-resize;
}

.pro-layout-item__resize-w {
    top: 0;
    left: 0;
    bottom: 0;
    width: var(--pro-layout-resize-handle-size);
    cursor: ew-resize;
}

/* Corner handles - larger for easier grabbing */
.pro-layout-item__resize-ne {
    top: 0;
    right: 0;
    width: 16px;
    height: 16px;
    cursor: nesw-resize;
}

.pro-layout-item__resize-se {
    bottom: 0;
    right: 0;
    width: 16px;
    height: 16px;
    cursor: nwse-resize;
}

.pro-layout-item__resize-sw {
    bottom: 0;
    left: 0;
    width: 16px;
    height: 16px;
    cursor: nesw-resize;
}

.pro-layout-item__resize-nw {
    top: 0;
    left: 0;
    width: 16px;
    height: 16px;
    cursor: nwse-resize;
}

/* Visual feedback on hover */
.pro-layout-item--resizable:hover .pro-layout-item__resize-handle::after {
    content: '';
    position: absolute;
    background-color: var(--pro-layout-resize-handle-color);
    opacity: 0.3;
}

.pro-layout-item__resize-n:hover::after,
.pro-layout-item__resize-s:hover::after {
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 2px;
    top: 50%;
    margin-top: -1px;
}

.pro-layout-item__resize-e:hover::after,
.pro-layout-item__resize-w:hover::after {
    top: 50%;
    transform: translateY(-50%);
    height: 40px;
    width: 2px;
    left: 50%;
    margin-left: -1px;
}

/* Corner handles - no visual indicator, just larger hit area on hover */
.pro-layout-item__resize-ne:hover::after,
.pro-layout-item__resize-se:hover::after,
.pro-layout-item__resize-sw:hover::after,
.pro-layout-item__resize-nw:hover::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background-color: var(--pro-layout-resize-handle-color);
    opacity: 0.15;
}

/* ============================================================================
   Accessibility & Focus States
   ============================================================================ */

.pro-layout-item:focus {
    outline: 2px solid var(--pc-color-primary, #1976d2);
    outline-offset: 2px;
}

.pro-layout-item__control-btn:focus {
    outline: 2px solid var(--pro-layout-item-header-text);
    outline-offset: 1px;
}

/* ============================================================================
   Animations
   ============================================================================ */

.pro-layout-item--animating {
    transition:
            left var(--pro-layout-animation-duration) var(--pro-layout-animation-easing),
            top var(--pro-layout-animation-duration) var(--pro-layout-animation-easing),
            width var(--pro-layout-animation-duration) var(--pro-layout-animation-easing),
            height var(--pro-layout-animation-duration) var(--pro-layout-animation-easing);
}

/* ============================================================================
   Performance Optimizations
   ============================================================================ */

/* Note: We intentionally avoid using transform: translateZ(0) here because it
   creates a new stacking context, which causes guide canvases to render below
   the dragged widget. The browser handles drag performance adequately without it. */
.pro-layout-item--dragging,
.pro-layout-item--resizing {
    user-select: none;
}

/* ============================================================================
   Reduced Motion Support
   ============================================================================ */

@media (prefers-reduced-motion: reduce) {
    .pro-layout-item--animating {
        transition: none;
    }

    .pro-layout-container__tray-item {
        transition: none;
    }
}

/* ============================================================================
   Locked Widget Styling
   ============================================================================ */

/* Only show not-allowed cursor on draggable areas, not the entire widget */
.pro-layout-item--locked .pro-layout-item__header[data-drag-handle] {
    cursor: not-allowed !important;
}

.pro-layout-item--locked .pro-layout-item__resize-handle {
    display: none !important;
}

.pro-layout-item__lock-icon {
    font-size: 10px;
    margin-right: 8px;
    opacity: 0.5;
    color: var(--pc-color-text-secondary, #666);
    transition: opacity 0.2s ease;
}

.pro-layout-item__lock-icon--hidden {
    opacity: 0;
    pointer-events: none;
}

.pro-layout-item__lock-icon--spacer {
    opacity: 0;
    pointer-events: none;
    margin-right: 0;
    margin-left: 8px;
}

/* ============================================================================
   Multi-Select Styling
   ============================================================================ */

.pro-layout-item--selected {
    outline: 2px solid var(--pc-color-primary, #1976d2);
    outline-offset: -2px;
    z-index: 999 !important;
}

/* Primary selected item (first in selection) - green outline */
.pro-layout-item--primary-selected {
    outline: 2px solid #4caf50 !important;
    box-shadow: 0 0 0 1px #4caf50;
}

/* Group drag visual feedback - semi-transparent highlight */
.pro-layout-item--group-dragging {
    opacity: 0.8;
    outline: 2px solid rgba(25, 118, 210, 0.5);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    transition: opacity 0.1s ease;
}

.pro-layout-container__selection-box {
    position: absolute;
    border: 2px dashed var(--pc-color-primary, #1976d2);
    background-color: rgba(25, 118, 210, 0.1);
    pointer-events: none;
    z-index: 10000;
    box-sizing: border-box;
}