/* =======================================================================
   FIX POPUP UI/UX (Responsive & Fit Screen)
   ======================================================================= */

/* 1. LOCK BODY SCROLL */
body.coloring-popup-visible {
    overflow: hidden !important;
}

/* 2. MAIN WRAPPER */
#wp-coloring-popup-wrapper {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: 999999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

/* BACKDROP */
.popup-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(5px);
    z-index: 1;
}

/* 3. POPUP CONTAINER */
.popup-container {
    position: relative;
    z-index: 2;
    background: #f5f7fa;
    /* Even smaller for compact view */
    width: 95%; 
    max-width: 1000px; /* Reduced from 1200px */
    height: 80vh;      /* Reduced from 85vh */
    display: flex;
    flex-direction: column;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.4);
    overflow: hidden;
}

/* 4. HEADER */
.popup-header {
    flex-shrink: 0;
    height: 50px;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding: 0 16px;
    background: #fff;
}

.popup-close-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f1f1f1;
    color: #333;
    border-radius: 50%;
    cursor: pointer;
    border: none;
    transition: all 0.2s;
}

.popup-close-btn:hover {
    background: #ff4d4f;
    color: #fff;
}

/* 5. CONTENT AREA */
.popup-content {
    flex-grow: 1;
    overflow: hidden; /* Prevent popup scroll, let internal elements scroll */
    position: relative;
    display: flex;
    flex-direction: column;
}

/* --- ADAPT V2 LAYOUT TO POPUP --- */

/* Ensure V2 Shell fits 100% of popup */
.popup-content .wp-coloring-shell-v2 {
    width: 100% !important;
    height: 100% !important;
    max-width: none !important;
    padding: 0 !important;
    margin: 0 !important;
    border-radius: 0 !important;
    background: transparent !important;
    display: flex;
    flex-direction: column;
}

/* Ensure Content Wrapper fills available space */
.popup-content .wp-coloring-content-wrapper {
    flex: 1;
    height: 100% !important;
    overflow: hidden;
    margin: 0 !important;
}

/* Adjust Sidebar in Popup - COMPACT MODE */
.popup-content .wp-coloring-sidebar-v2 {
    /* Reduce width from 320px to 200px for more canvas space */
    width: 320px !important;
    min-width: 200px !important;
    flex-shrink: 0 !important;
    
    border-radius: 0 !important;
    border-right: 1px solid #e0e0e0;
}

/* Make scrollable area more compact */
.popup-content .sidebar-scrollable {
    padding: 12px !important;
}

/* === SIDEBAR SECTIONS === */

/* Compact section spacing */
.popup-content .sidebar-section {
    margin-bottom: 2px !important;
}

.popup-content .section-title {
    font-size: 11px !important;
    margin-bottom: 6px !important;
}

/* Color grid - compact for popup */
.popup-content .color-grid {
    grid-template-columns: repeat(4, 1fr) !important;
    gap: 6px !important;
}

.popup-content .color-grid .color-btn {
    width: 32px !important;
    height: 32px !important;
    min-width: 32px !important;
    min-height: 32px !important;
}

/* Hide skin tones to save space */
.popup-content .sidebar-section:has(.skin-tones-grid) {
    display: none !important;
}

/* Show patterns in popup (sidebar is now 300px wide) */

/* === REDESIGNED TOOLS SECTION === */
.popup-content .tools-section {
    margin-bottom: 10px !important;
}

.popup-content .tools-section .section-title {
    font-size: 10px !important;
    font-weight: 700 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
    color: #555 !important;
    margin-bottom: 8px !important;
}

/* Tools Grid - Clean 2x2 */
.popup-content .tools-grid {
    display: grid !important;
    grid-template-columns: repeat(4, 1fr) !important;
    gap: 6px !important;
}

.popup-content .tools-grid .tool-btn {
    min-height: 48px !important;
    padding: 8px 4px !important;
    background: #f8f9fa !important;
    border: 1px solid #e0e0e0 !important;
    border-radius: 8px !important;
    transition: all 0.2s ease !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 4px !important;
}

.popup-content .tools-grid .tool-btn:hover {
    background: #fff !important;
    border-color: #FF9500 !important;
    transform: translateY(-1px) !important;
    box-shadow: 0 2px 8px rgba(255, 149, 0, 0.15) !important;
}

.popup-content .tools-grid .tool-btn.active {
    background: #FF9500 !important;
    border-color: #FF9500 !important;
    color: #fff !important;
}

.popup-content .tools-grid .tool-btn.active .tool-label {
    color: #fff !important;
}

/* Tool Icons */
.popup-content .tools-grid .tool-btn svg {
    width: 18px !important;
    height: 18px !important;
    flex-shrink: 0 !important;
}

/* Tool Labels */
.popup-content .tool-label {
    font-size: 9px !important;
    font-weight: 600 !important;
    line-height: 1 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.3px !important;
}

/* === REDESIGNED ACTIONS SECTION === */
.popup-content .actions-section {
    margin-bottom: 0 !important;
}

.popup-content .actions-section .section-title {
    font-size: 10px !important;
    font-weight: 700 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
    color: #555 !important;
    margin-bottom: 8px !important;
}

/* Actions Grid - Compact 3 columns */
.popup-content .actions-grid {
    display: grid !important;
    grid-template-columns: repeat(5, 1fr) !important;
    gap: 6px !important;
}

.popup-content .actions-grid .bottom-tool-btn {
    min-height: 36px !important;
    padding: 5px 3px !important;
    background: #f8f9fa !important;
    border: 1px solid #e0e0e0 !important;
    border-radius: 6px !important;
    transition: all 0.2s ease !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 2px !important;
}

.popup-content .actions-grid .bottom-tool-btn:hover {
    background: #fff !important;
    border-color: #666 !important;
    transform: translateY(-1px) !important;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1) !important;
}

/* Action Icons */
.popup-content .actions-grid .bottom-tool-btn svg {
    width: 16px !important;
    height: 16px !important;
    flex-shrink: 0 !important;
}

/* Action Labels */
.popup-content .action-label {
    font-size: 8px !important;
    font-weight: 600 !important;
    line-height: 1.1 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.3px !important;
    text-align: center !important;
    color: #666 !important;
}

/* Compact brush size section */
.popup-content .brush-size-section {
    padding-top: 8px !important;
}

/* Compact fixed bottom */
.popup-content .sidebar-fixed-bottom {
    padding: 8px 12px !important;
}

/* Adjust Main Canvas Area in Popup */
.popup-content .wp-coloring-main-v2 {
    height: 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 4px; /* Reduced to almost zero */
    background: #e5e5f7;
}

/* === CANVAS SIZING STRATEGY ===
 * 1. Canvas has min-size (400x533 desktop, 300x400 mobile)
 * 2. fit-content wraps outer tightly around canvas
 * 3. JavaScript applies CSS transform: scale() for visual enlargement
 * 4. Coordinates calculated on original size (accurate!)
 * 5. image-rendering: crisp-edges prevents blur
 */
.popup-content .wp-coloring-canvas-outer {
    /* Adjusted sizing to reduce whitespace */
    min-width: 300px !important;
    min-height: 400px !important;
    width: fit-content !important;
    height: fit-content !important;
    max-width: 95% !important;
    max-height: 95% !important;
    
    /* JS will scale via transform for visibility */
    transform-origin: center center;
    
    border-radius: 4px !important;
    border: 1px solid rgba(0,0,0,0.05) !important;
    
    /* Reduced shadow */
    box-shadow: 0 2px 10px rgba(0,0,0,0.08) !important;
    margin: auto !important;
    position: relative !important;
}

/* Canvas keeps its natural size */
.popup-content .wp-coloring-canvas-outer canvas {
    display: block !important;
}

/* Hide Top Toolbar in Popup if needed or style it */
.popup-content .wp-coloring-toolbar {
    border-bottom: 1px solid #e0e0e0;
    padding: 8px 16px;
    background: #fff;
}


/* --- RESPONSIVE ADJUSTMENTS --- */
@media (max-width: 900px) {
    .popup-container {
        max-width: 100%;
        height: 100%;
        border-radius: 0;
    }

    .popup-content {
        padding: 10px;
    }

    .popup-header {
        padding: 12px 16px;
    }

    .popup-close-btn {
        width: 36px;
        height: 36px;
        font-size: 20px;
    }

    /* Stack layout on mobile - sidebar below canvas */
    .popup-content .wp-coloring-shell-v2 {
        flex-direction: column !important;
    }
    
    .popup-content .wp-coloring-content-wrapper {
        flex-direction: column !important;
    }

    /* Canvas smaller min-size on mobile */
    .popup-content .wp-coloring-canvas-outer {
        min-width: 300px !important;
        min-height: 400px !important;
    }
    
    /* Canvas first (top) */
    .popup-content .wp-coloring-main-v2 {
        order: 1 !important;
        width: 100% !important;
        padding: 10px !important;
    }
    
    /* Sidebar second (bottom) */
    .popup-content .wp-coloring-sidebar-v2 {
        order: 2 !important;
        width: 100% !important;
        max-width: 100% !important;
        min-width: 100% !important;
        border-right: none !important;
        border-top: 1px solid #e0e0e0 !important;
        display: flex !important;
        flex-direction: column !important;
        height: auto !important;
        padding: 8px !important;
        gap: 8px !important;
    }
    
    /* === ROW 1: SCROLLABLE SECTIONS (Colors, Patterns) - One Horizontal Row === */
    .popup-content .sidebar-scrollable {
        display: flex !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        gap: 12px !important;
        padding: 4px !important;
        -webkit-overflow-scrolling: touch;
    }
    
    .popup-content .sidebar-scrollable .sidebar-section {
        flex-shrink: 0 !important;
    }
    
    /* Hide section titles in scrollable */
    .popup-content .sidebar-scrollable .section-title {
        display: none !important;
    }
    
    /* Colors - horizontal in scrollable row */
    .popup-content .color-grid {
        display: flex !important;
        flex-wrap: nowrap !important;
        gap: 8px !important;
    }
    
    .popup-content .color-grid .color-btn {
        flex-shrink: 0 !important;
        width: 44px !important;
        height: 44px !important;
        min-width: 44px !important;
        min-height: 44px !important;
    }
    
    /* Patterns - horizontal in scrollable row */
    .popup-content .patterns-grid {
        display: flex !important;
        flex-wrap: nowrap !important;
        gap: 8px !important;
    }
    
    .popup-content .patterns-grid .pattern-btn {
        flex-shrink: 0 !important;
        width: 44px !important;
        height: 44px !important;
    }
    
    /* Hide skin tones on mobile */
    .popup-content .sidebar-section:has(.skin-tones-grid) {
        display: none !important;
    }
    
    /* === ROW 2: FIXED BOTTOM (Tools + Actions) - One Horizontal Row === */
    .popup-content .sidebar-fixed-bottom {
        display: flex !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        gap: 12px !important;
        padding: 4px !important;
        background: transparent !important;
        border-top: none !important;
        -webkit-overflow-scrolling: touch;
    }
    
    .popup-content .sidebar-fixed-bottom > * {
        flex-shrink: 0 !important;
    }
    
    /* Hide section titles in fixed bottom */
    .popup-content .sidebar-fixed-bottom .section-title {
        display: none !important;
    }
    
    /* Tools and Actions - horizontal */
    .popup-content .tools-grid,
    .popup-content .actions-grid {
        display: flex !important;
        flex-wrap: nowrap !important;
        gap: 8px !important;
    }
    
    .popup-content .tools-grid .tool-btn {
        flex-shrink: 0 !important;
        min-width: 60px !important;
        padding: 6px !important;
        font-size: 10px !important;
    }
    
    .popup-content .actions-grid .action-btn {
        flex-shrink: 0 !important;
        min-width: 50px !important;
        max-width: 50px !important;
        padding: 6px 4px !important;
        font-size: 9px !important;
    }
    
    .popup-content .action-btn svg {
        width: 18px !important;
        height: 18px !important;
    }
    
    /* Hide brush size label on mobile */
    .popup-content .brush-size-label {
        display: none !important;
    }
    
    /* Compact brush size slider */
    .popup-content .brush-size-control {
        padding: 4px 0 !important;
        min-width: 120px !important;
    }
    
    /* Scrollbar styling */
    .popup-content .sidebar-scrollable::-webkit-scrollbar,
    .popup-content .sidebar-fixed-bottom::-webkit-scrollbar {
        height: 4px;
    }
    
    .popup-content .sidebar-scrollable::-webkit-scrollbar-thumb,
    .popup-content .sidebar-fixed-bottom::-webkit-scrollbar-thumb {
        background: #ccc;
        border-radius: 2px;
    }
}