* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    padding: 20px;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    background: white;
    border-radius: 20px;
    padding: 10px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.1);
}

.header {
    text-align: left;
    margin-bottom: 20px;
}

.header h1 {
    color: #333;
    font-size: 2rem;
    margin-bottom: 5px;
}

.image-groups-container {
    margin-bottom: 20px;
}

.image-group {
    border: 2px solid #e9ecef;
    border-radius: 15px;
    padding: 5px;
    margin-bottom: 5px;
    background: #f8f9fa;
    position: relative;
    transition: all 0.3s ease;
}

/* Active group styling - DISABLED per user request */
.image-group.active-group {
    /* border-left: 4px solid #007bff; */
    /* background: rgba(0, 123, 255, 0.02); */
    /* box-shadow: 0 2px 8px rgba(0, 123, 255, 0.1); */
}

/* 3-Column Layout for each group */
.group-content {
    display: grid;
    grid-template-columns: 1fr 1.5fr 1fr;
    gap: 5px;
    margin-top: 5px;
}

.group-column {
    background: white;
    border-radius: 12px;
    padding: 5px;
    margin: 5px;
    border: 1px solid #dee2e6;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.group-column h4 {
    margin: 0 0 5px 0;
    color: #495057;
    font-size: 1rem;
    font-weight: 600;
    padding-bottom: 5px;
    border-bottom: 2px solid #e9ecef;
}

/* Column 1: Grid Selector */
.grid-column .grid-selector {
    margin: 0;
    padding: 10px;
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 8px;
}

.grid-column .grid-container {
    max-width: 200px;
    height: 200px;
    margin: 0 auto 10px;
}

/* Column 1: Settings Panel - HỒNG NHẠT */
.settings-column {
    background: #ffebee;
    border-radius: 8px;
    padding: 5px;
    border: none;
}

.settings-column h4 {
    font-size: 1rem;
    margin: 0 0 5px 0;
    padding-bottom: 5px;
}

.group-settings .setting-group {
    margin-bottom: 16px;
}

.group-settings .setting-row {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 7px;
}

.group-settings .setting-label {
    margin-bottom: 0;
    font-size: 0.85rem;
    flex: 0 0 auto;
    min-width: 80px;
}

.group-settings .setting-select {
    font-size: 0.85rem;
    padding: 6px 8px;
    flex: 1;
    max-width: calc(100% - 90px);
    border: 1px solid #dee2e6;
    border-radius: 4px;
    background: #f8f9fa;
}

.group-settings .custom-size-inputs {
    font-size: 0.85rem;
}

.group-settings .custom-size-row {
    margin-bottom: 8px;
}

.group-settings .custom-label {
    font-size: 0.8rem;
    min-width: 70px;
}

.group-settings .custom-input {
    font-size: 0.85rem;
    padding: 5px 7px;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    background: #f8f9fa;
}

.group-settings .color-presets {
    gap: 7px;
}

.group-settings .color-preset {
    width: 32px;
    height: 32px;
}

.group-settings .color-picker {
    width: 38px;
    height: 38px;
    margin-left: auto;
    border: none;
    border-radius: 4px;
}

.group-settings .quality-value {
    font-size: 0.85rem;
    color: #e91e63;
    font-weight: 600;
    margin-left: auto;
}

.group-settings .quality-slider-container {
    margin-top: 5px;
}

.group-settings .scale-slider-container {
    margin-top: 5px;
    overflow: hidden; /* Ngăn scroll bar khi thumb scale 1.1x */
}

/* Export Scale Slider - giống style-slider */
.group-settings .export-scale-slider {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: #e9ecef;
    outline: none;
    -webkit-appearance: none;
    transition: all 0.2s ease;
}

.group-settings .export-scale-slider::-webkit-slider-thumb {
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #007bff;
    cursor: pointer;
    border: 2px solid #ffffff;
    box-shadow: 0 2px 4px rgba(0,123,255,0.3);
    transition: all 0.2s ease;
}

.group-settings .export-scale-slider::-webkit-slider-thumb:hover {
    background: #0056b3;
    transform: scale(1.1);
    box-shadow: 0 3px 6px rgba(0,123,255,0.4);
}

.group-settings .export-scale-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #007bff;
    cursor: pointer;
    border: 2px solid #ffffff;
    box-shadow: 0 2px 4px rgba(0,123,255,0.3);
    transition: all 0.2s ease;
}

.group-settings .scale-options {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    padding: 0 2px;
}

.group-settings .scale-label {
    font-size: 0.75rem;
    color: #666;
    font-weight: 500;
}

.group-settings .scale-value {
    background: #f8f9fa;
    color: #333;
    font-weight: 600;
    margin-left: auto;
    font-size: 0.85rem;
    padding: 2px 8px;
    border-radius: 4px;
    border: 1px solid #dee2e6;
}

/* Column 2: Grid Selector - XANH NHẠT */
.grid-column {
    background: #e3f2fd;
    border-radius: 8px;
    padding: 15px;
    border: none;
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Grid Delete Button */
.grid-delete-btn {
    padding: 6px 12px !important;
    font-size: 0.8rem !important;
    font-weight: 500;
    min-width: auto;
    background: linear-gradient(135deg, #6c757d, #495057) !important;
    border: 1px solid #495057;
    border-radius: 6px;
    box-shadow: 0 2px 4px rgba(108, 117, 125, 0.2);
    transition: all 0.2s ease;
}

.grid-delete-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 3px 6px rgba(108, 117, 125, 0.3);
    background: linear-gradient(135deg, #495057, #343a40) !important;
}

/* Column 2: Preview Column - VÀNG NHẠT */
.preview-column {
    background: #fff8e1;
    display: flex;
    flex-direction: column;
    height: 100%;
    border-radius: 8px;
    padding: 5px;
    border: none;
}

/* Preview Column - Simplified */
.preview-column {
    display: flex;
    flex-direction: column;
}

.preview-column .preview-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* Controls Column */
.controls-column {
    background: #ffebee;
    border: none;
}

.controls-column h4 {
    color: #495057;
    border-bottom: none;
}

/* Style Controls */
.style-controls-container {
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* Tab Navigation */
.tab-navigation {
    display: flex;
    border-bottom: 2px solid #e9ecef;
    margin-bottom: 5px;
}

.tab-button {
    flex: 1;
    padding: 8px 6px;
    border: none;
    background: #f8f9fa;
    color: #6c757d;
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: 8px 8px 0 0;
    transition: all 0.2s ease;
    border-bottom: 2px solid transparent;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tab-button:hover {
    background: #e9ecef;
    color: #495057;
}

.tab-button.active {
    background: #ffffff;
    color: #007bff;
    border-bottom: 2px solid #007bff;
    font-weight: 600;
}

/* Smart Delete Button - Floating Overlay */
.smart-delete-btn {
    position: absolute;
    bottom: -10px; /* Nằm dưới mép của preview container, dưới layout grid */
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 36px;
    padding: 0;
    border: none;
    background: rgba(108, 117, 125, 0.85);
    color: white;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    border-radius: 50%; /* Hình tròn hoàn hảo */
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 3px 12px rgba(0,0,0,0.25);
    backdrop-filter: blur(6px);
    z-index: 100;
    opacity: 0.85;
    border: 2px solid rgba(255, 255, 255, 0.2);
}

.smart-delete-btn:hover {
    background: rgba(108, 117, 125, 1);
    transform: translateX(-50%) translateY(-3px) scale(1.1);
    box-shadow: 0 8px 24px rgba(0,0,0,0.35);
    opacity: 1;
    border: 2px solid rgba(255, 255, 255, 0.4);
}

.smart-delete-btn.has-selection {
    background: rgba(0, 123, 255, 0.9);
    color: white;
    box-shadow: 0 3px 16px rgba(0, 123, 255, 0.4);
    opacity: 0.9;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.smart-delete-btn.has-selection:hover {
    background: rgba(0, 86, 179, 1);
    transform: translateX(-50%) translateY(-3px) scale(1.1);
    box-shadow: 0 8px 28px rgba(0, 123, 255, 0.6);
    opacity: 1;
    border: 2px solid rgba(255, 255, 255, 0.5);
}

/* Tab Content */
.tab-content {
    flex: 1;
    overflow-y: auto;
    background: #ffffff;
    border-radius: 8px;
    padding: 5px;
    margin: 1px;
}

.tab-panel {
    display: none;
    height: 100%;
}

.tab-panel.active {
    display: block;
}

/* Image Edit Panel Specific */
.image-edit-panel {
    padding: 10px 0;
}

.no-image-selected {
    text-align: center;
    color: #6c757d;
    padding: 40px 20px;
    border: 2px dashed #dee2e6;
    border-radius: 8px;
    margin: 20px 0;
}

.no-image-selected .icon {
    font-size: 2rem;
    margin-bottom: 10px;
    display: block;
}

.selected-image-info {
    background: #e3f2fd;
    border: 1px solid #90caf9;
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 15px;
    text-align: center;
}

.selected-image-info .image-name {
    font-weight: 600;
    color: #1565c0;
    font-size: 0.9rem;
}

.selected-image-info .image-position {
    font-size: 0.8rem;
    color: #1976d2;
    margin-top: 4px;
}

/* Quick rotation buttons */
.quick-rotation-buttons .btn {
    font-size: 0.7rem;
    padding: 4px 8px;
    min-width: auto;
}

.style-controls-container h5 {
    margin: 0 0 20px 0;
    color: #495057;
    font-size: 0.9rem;
    font-weight: 600;
    text-align: center;
    padding-bottom: 10px;
    border-bottom: 1px solid #e9ecef;
}

.style-group {
    margin-bottom: 20px;
}

.style-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

/* Desktop: Ẩn slider trong style-row CHỈ cho Display tab, KHÔNG ẩn cho Edit Image tab */
.style-row .style-slider-container {
    display: none;
}

/* EXCEPTION: Hiển thị slider trong tất cả Edit Image tabs */
[id^="tab-image-edit"] .style-row .style-slider-container {
    display: block !important;
}

/* EXCEPTION: Hiển thị slider trong tất cả Display tabs */
[id^="tab-layout-display"] .style-row .style-slider-container {
    display: block !important;
}

/* EXCEPTION: Hiển thị slider trong tất cả Filter tabs */
[id^="tab-filter"] .style-row .style-slider-container {
    display: block !important;
}

/* Đảm bảo filter grid hiển thị với layout grid trong tất cả Filter tabs */
[id^="tab-filter"] .filter-grid-container {
    display: block !important;
}

[id^="tab-filter"] .filter-grid {
    display: grid !important;
    grid-template-columns: repeat(4, 1fr) !important;
    gap: 8px !important;
}

/* EXCEPTION: Hiển thị slider trong tất cả Lighting tabs */
[id^="tab-lighting"] .style-row .style-slider-container {
    display: block !important;
}

/* Đảm bảo lighting grid hiển thị với layout grid trong tất cả Lighting tabs */
[id^="tab-lighting"] .filter-grid-container {
    display: block !important;
}

[id^="tab-lighting"] .filter-grid {
    display: grid !important;
    grid-template-columns: repeat(4, 1fr) !important;
    gap: 8px !important;
}

.style-group > .style-slider-container {
    display: block;
}

.style-label {
    display: flex;
    align-items: center;
    font-size: 0.8rem;
    color: #495057;
    font-weight: 500;
    min-width: 80px; /* Đảm bảo tất cả label có cùng độ rộng, dựa trên "Spacing:" và "Corners:" */
    flex-shrink: 0; /* Không cho phép co lại */
}

.style-icon {
    margin-right: 6px;
    font-size: 0.9rem;
}

.style-value {
    font-size: 0.8rem;
    color: #007bff;
    font-weight: 600;
    min-width: 35px;
    text-align: right;
}

.style-slider-container {
    width: 100%;
    overflow: visible; /* Cho phép thumb hiển thị khi scale 1.1x */
    position: relative;
    z-index: 10;
}

.style-slider {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: #e9ecef;
    outline: none;
    -webkit-appearance: none;
    transition: all 0.2s ease;
}

/* Make zoom slider thumb same size as other sliders */
#scaleSlider-1::-webkit-slider-thumb,
#scaleSlider-2::-webkit-slider-thumb,
#scaleSlider-3::-webkit-slider-thumb,
#scaleSlider-4::-webkit-slider-thumb,
#scaleSlider-5::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #007bff;
    cursor: pointer;
    border: 2px solid #fff;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.style-slider::-webkit-slider-thumb {
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #007bff;
    cursor: pointer;
    border: 2px solid #ffffff;
    box-shadow: 0 2px 4px rgba(0,123,255,0.3);
    transition: all 0.2s ease;
    position: relative;
    z-index: 1000;
}

.style-slider::-webkit-slider-thumb:hover {
    background: #0056b3;
    transform: scale(1.1);
    box-shadow: 0 3px 6px rgba(0,123,255,0.4);
}

.style-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #007bff;
    cursor: pointer;
    border: 2px solid #ffffff;
    box-shadow: 0 2px 4px rgba(0,123,255,0.3);
    position: relative;
    z-index: 1000;
}

/* Fixed square preview area */
.preview-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 440px; /* Tăng chiều cao tối thiểu */
    padding: 15px;
    position: relative; /* Enable absolute positioning for smart delete button */
}

.preview-square {
    width: 100%;
    max-width: 440px; /* Tăng kích thước tối đa */
    /* aspect-ratio: var(--preview-aspect-ratio, 1); - DISABLED to prevent CSS/JS conflict */
    /* background color will be set by JavaScript */
    border: none;
    border-radius: var(--template-image-radius, 0px);
    box-shadow: none;
    position: relative; /* Enable absolute positioning for overlay button */
    overflow: hidden; /* Allow clipping for border radius */
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Group-specific aspect ratios */
#group-1 .preview-square {
    aspect-ratio: var(--group-1-aspect-ratio, var(--preview-aspect-ratio, 1));
}

#group-2 .preview-square {
    aspect-ratio: var(--group-2-aspect-ratio, 1);
}

#group-3 .preview-square {
    aspect-ratio: var(--group-3-aspect-ratio, 1);
}

#group-4 .preview-square {
    aspect-ratio: var(--group-4-aspect-ratio, 1);
}

#group-5 .preview-square {
    aspect-ratio: var(--group-5-aspect-ratio, 1);
}

/* Image container with hidden overflow to clip scaled images */
.preview-square .image-container {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden; /* Clip scaled images */
    display: flex;
    align-items: center;
    justify-content: center;
}

.preview-content {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

/* Responsive 3-column layout */
@media (max-width: 1024px) {
    .group-content {
        grid-template-columns: 1fr;
        gap: 15px;
    }

    .group-column {
        margin-bottom: 10px;
    }
}

@media (min-width: 1025px) and (max-width: 1366px) {
    .group-content {
        grid-template-columns: 0.9fr 1.4fr 0.9fr;
        gap: 10px;
    }
}
}

/* Thêm style cho màn hình lớn */
@media (min-width: 1367px) {
    .group-content {
        grid-template-columns: 0.515625fr 1.984375fr; /* Khôi phục lại chính xác tỷ lệ ban đầu */
        gap: 20px;
    }
    
    .preview-container {
        min-height: 520px;
    }
    
    .preview-square {
        max-width: 520px;
    }
}

.group-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.group-title {
    font-size: 1rem;
    font-weight: 600;
    color: #495057;
}

.group-controls {
    display: flex;
    gap: 8px;
}

.btn-small {
    padding: 6px 10px;
    font-size: 0.8rem;
    min-width: auto;
}

.btn-danger {
    background: linear-gradient(135deg, #dc3545, #c82333);
    color: white;
}

.btn-danger:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 16px rgba(220, 53, 69, 0.3);
}

.btn-warning {
    background: linear-gradient(135deg, #ffc107, #e0a800);
    color: #212529;
}

.btn-warning:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 16px rgba(255, 193, 7, 0.3);
}

.upload-section {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 15px;
    align-items: center;
    margin-bottom: 12px;
}

.group-result {
    text-align: center;
    margin-top: 12px;
}

.add-group-section {
    text-align: center;
    margin-bottom: 20px;
}

.btn-add {
    background: linear-gradient(135deg, #28a745, #20c997);
    color: white;
    font-size: 1rem;
}

.btn-add:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 16px rgba(40, 167, 69, 0.3);
}

.upload-box {
    border: 3px dashed #ddd;
    border-radius: 15px;
    padding: 30px 15px;
    text-align: center;
    background: #f9f9f9;
    transition: all 0.3s ease;
    cursor: pointer;
    position: relative;
    min-height: 200px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.upload-box:hover {
    border-color: #667eea;
    background: #f0f4ff;
}

.upload-box.has-image {
    border-color: #4CAF50;
    background: #f0fff0;
}

.upload-box img {
    max-width: 100%;
    max-height: 150px;
    object-fit: contain;
    border-radius: 10px;
}

.upload-text {
    color: #666;
    font-size: 0.9rem;
    margin-top: 8px;
}

.operator {
    font-size: 3rem;
    color: #667eea;
    font-weight: bold;
}

.result-section {
    margin-top: 20px;
    text-align: center;
}

.result-canvas {
    border: 2px solid #ddd;
    border-radius: 15px;
    max-width: 100%;
    margin: 15px auto;
    display: block;
    box-shadow: 0 8px 24px rgba(0,0,0,0.1);
}

.controls {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-top: 15px;
    flex-wrap: wrap;
}

.btn {
    padding: 10px 24px;
    border: none;
    border-radius: 25px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.btn-secondary {
    background: #f8f9fa;
    color: #495057;
    border: 2px solid #dee2e6;
}

.btn-secondary:hover {
    background: #e9ecef;
    transform: translateY(-2px);
}

.btn-success {
    background: linear-gradient(135deg, #4CAF50, #45a049);
    color: white;
}

.btn-success:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(76, 175, 80, 0.3);
}

.hidden {
    display: none;
}

@media (max-width: 768px) {
    .upload-section {
        grid-template-columns: 1fr;
        gap: 15px;
    }
    
    .header h1 {
        font-size: 2rem;
    }
    
    .container {
        padding: 20px;
    }
    
    /* Responsive preview square */
    .preview-square {
        max-width: 250px;
    }
    
    .layout-cell .placeholder {
        font-size: 0.5rem;
        padding: 3px;
    }
    
    .layout-cell .placeholder .icon {
        font-size: 0.8rem;
        margin-bottom: 2px;
    }
}

@media (min-width: 1400px) {
    .preview-square {
        max-width: 500px;
    }
}

/* Loading and spinner styles */
.loading {
    text-align: center;
    color: #667eea;
    font-size: 1rem;
    margin: 15px 0;
}

.spinner {
    border: 3px solid #f3f3f3;
    border-top: 3px solid #667eea;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    animation: spin 1s linear infinite;
    display: inline-block;
    margin-right: 8px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.template-selection {
    margin-bottom: 20px;
    padding: 0;
    background: transparent;
    border-radius: 0;
}

.template-options {
    display: flex;
    gap: 5px;
    justify-content: center;
    flex-wrap: wrap;
}

.template-option {
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 4px;
    padding: 0;
    background: transparent;
    transition: all 0.2s ease;
    display: flex;
    justify-content: center;
    align-items: center;
}

.template-option:hover {
    background: rgba(102, 126, 234, 0.1);
}

.template-option input[type="radio"] {
    display: none;
}

.template-option:has(input[type="radio"]:checked) {
    border-color: #667eea;
    background: rgba(102, 126, 234, 0.1);
}

.layout-frame {
    display: flex;
    gap: 2px;
    align-items: center;
    justify-content: center;
    background: white;
    border-radius: 4px;
    padding: 2px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.layout-frame.vertical {
    flex-direction: column;
}

.layout-frame.grid-2x2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    gap: 2px;
    width: 100px;
    height: 100px;
}

.layout-frame.main-sidebar {
    display: flex;
    gap: 2px;
    width: 120px;
    height: 70px;
}

.layout-frame.left-main-right {
    display: flex;
    gap: 2px;
    width: 140px;
    height: 70px;
}

.layout-frame.main-left-right {
    display: flex;
    gap: 2px;
    width: 120px;
    height: 70px;
}

.layout-frame.center-around {
    position: relative;
    width: 100px;
    height: 100px;
}

.layout-frame.horizontal-5 {
    display: flex;
    gap: 2px;
    width: 140px;
    height: 30px;
}

.frame-box {
    background: #dee2e6;
    border-radius: 2px;
}

.layout-frame:not(.grid-2x2):not(.main-sidebar) .frame-box {
    width: 40px;
    height: 50px;
}

.layout-frame.vertical .frame-box {
    width: 50px;
    height: 25px;
}

.layout-frame.grid-2x2 .frame-box {
    width: 100%;
    height: 100%;
}

.layout-frame.main-sidebar .frame-box.main {
    width: 75px;
    height: 100%;
    background: #adb5bd;
}

.layout-frame.main-sidebar .sidebar {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
}

.layout-frame.main-sidebar .frame-box.small {
    width: 100%;
    flex: 1;
}

.layout-frame.left-main-right .sidebar-left {
    display: flex;
    flex-direction: column;
    gap: 1px;
    width: 20px;
}

.layout-frame.left-main-right .frame-box.main {
    width: 50px;
    height: 100%;
    background: #adb5bd;
}

.layout-frame.left-main-right .sidebar-right {
    display: flex;
    flex-direction: column;
    gap: 2px;
    width: 20px;
}

.layout-frame.left-main-right .frame-box.small {
    width: 100%;
    flex: 1;
}

.layout-frame.main-left-right .frame-box.main {
    width: 75px;
    height: 100%;
    background: #adb5bd;
}

.layout-frame.main-left-right .sidebar {
    display: flex;
    flex-direction: column;
    gap: 1px;
    flex: 1;
}

.layout-frame.main-left-right .frame-box.small {
    width: 100%;
    flex: 1;
}

.layout-frame.center-around .frame-box.center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40px;
    height: 40px;
    background: #adb5bd;
    z-index: 2;
}

.layout-frame.center-around .frame-box.top {
    position: absolute;
    top: 5px;
    left: 50%;
    transform: translateX(-50%);
    width: 30px;
    height: 15px;
}

.layout-frame.center-around .frame-box.bottom {
    position: absolute;
    bottom: 5px;
    left: 50%;
    transform: translateX(-50%);
    width: 30px;
    height: 15px;
}

.layout-frame.center-around .frame-box.left {
    position: absolute;
    left: 5px;
    top: 50%;
    transform: translateY(-50%);
    width: 15px;
    height: 30px;
}

.layout-frame.center-around .frame-box.right {
    position: absolute;
    right: 5px;
    top: 50%;
    transform: translateY(-50%);
    width: 15px;
    height: 30px;
}

.layout-frame.horizontal-5 .frame-box {
    width: 25px;
    height: 100%;
}

.template-option:has(input[type="radio"]:checked) .frame-box {
    background: #667eea;
}

.template-option:has(input[type="radio"]:checked) .frame-box.main {
    background: #495057;
}

.template-option:hover .frame-box {
    background: #adb5bd;
}

.template-option:hover .frame-box.main {
    background: #6c757d;
}

/* Grid Selector Styles */
.grid-selector {
    text-align: center;
    margin-bottom: 20px;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 15px;
    border: 2px solid #e9ecef;
}

.grid-container {
    position: relative;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 4px;
    width: 90%;
    max-width: 300px;
    aspect-ratio: 1;
    margin: 0 auto;
    border: none;
    border-radius: 0;
    padding: 0;
    background: transparent;
    box-shadow: none;
    place-items: center;
}

.grid-cell {
    background: #e1f5fe;
    border: 2px solid #0288d1;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    aspect-ratio: 1;
    width: 100%;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(2, 136, 209, 0.3);
    transition: all 0.2s ease;
}

.grid-cell:hover {
    background: #f8f9fa;
}

.grid-cell.selected {
    background: #007bff;
    border-color: #0056b3;
}

.grid-cell.selected:hover {
    background: #007bff;
}

/* Drag selection styles */
.grid-cell.dragging {
    background: #adb5bd !important;
    border-color: #495057 !important;
}

/* Grid container for merge overlays */
.grid-container {
    position: relative;
}

.grid-cell.merged {
    position: relative;
    color: white;
    font-weight: bold;
    border: 1px solid #dee2e6;
    background: #e9ecef;
}

/* Merge overlay creates seamless regions */
.merge-overlay {
    position: absolute;
    border-radius: 4px;
    border: 2px solid;
    background: rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.8);
    text-shadow: 1px 1px 2px rgba(255, 255, 255, 0.9);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 2px 4px rgba(0, 0, 0, 0.1);
    z-index: 10;
    pointer-events: none;
    transition: all 0.2s ease;
}

.merge-overlay.region-1 {
    background: rgba(76, 175, 80, 0.3);
    border-color: #4CAF50;
    color: #2E7D32;
}

.merge-overlay.region-2 {
    background: rgba(33, 150, 243, 0.3);
    border-color: #2196F3;
    color: #1565C0;
}

.merge-overlay.region-3 {
    background: rgba(244, 67, 54, 0.3);
    border-color: #F44336;
    color: #C62828;
}

.merge-overlay.region-4 {
    background: rgba(255, 152, 0, 0.3);
    border-color: #FF9800;
    color: #E65100;
}

.merge-overlay.region-5 {
    background: rgba(156, 39, 176, 0.3);
    border-color: #9C27B0;
    color: #6A1B9A;
}

.merge-overlay.region-6 {
    background: rgba(0, 188, 212, 0.3);
    border-color: #00BCD4;
    color: #00838F;
}

.merge-overlay.region-7 {
    background: rgba(233, 30, 99, 0.3);
    border-color: #E91E63;
    color: #AD1457;
}

.merge-overlay.region-8 {
    background: rgba(96, 125, 139, 0.3);
    border-color: #607D8B;
    color: #37474F;
}

/* Merge overlays for seamless merged regions */
.grid-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 2;
}

/* Template selection header */
.template-selection {
    margin-bottom: 20px;
}

.template-selection h3 {
    text-align: center;
    color: #495057;
    margin-bottom: 15px;
    font-size: 1.2rem;
}

/* Upload box styles */
.upload-input {
    display: none;
}

.upload-overlay {
    position: absolute;
    top: 5px;
    right: 5px;
    background: rgba(0,0,0,0.7);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
}

/* Visual Layout Preview Styles */
.layout-preview {
    display: block; /* Default to block when empty */
    width: 100%;
    height: 100%;
    margin: 0;
    /* Ensure it shows parent background color */
    background: transparent;
    border-radius: var(--template-image-radius, 0px);
    overflow: hidden;
    position: relative; /* Add positioning context */
}

/* When layout preview has template content, switch to grid */
.layout-preview.has-template {
    display: block; /* Keep as block, let child handle grid */
    width: 100%;       /* Fill available space */
    height: auto;      /* Auto height for aspect ratio */
    margin: 0 auto;    /* Center when has template */
    border-radius: var(--template-image-radius, 0px);
    overflow: hidden;
    position: relative; /* Ensure proper containment */
    aspect-ratio: var(--preview-aspect-ratio, 1); /* Dynamic aspect ratio */
}

/* Group-specific layout preview aspect ratios */
#group-1 .layout-preview.has-template {
    aspect-ratio: var(--group-1-aspect-ratio, var(--preview-aspect-ratio, 1));
}

#group-2 .layout-preview.has-template {
    aspect-ratio: var(--group-2-aspect-ratio, 1);
}

#group-3 .layout-preview.has-template {
    aspect-ratio: var(--group-3-aspect-ratio, 1);
}

#group-4 .layout-preview.has-template {
    aspect-ratio: var(--group-4-aspect-ratio, 1);
}

#group-5 .layout-preview.has-template {
    aspect-ratio: var(--group-5-aspect-ratio, 1);
}

/* When no template is applied, ensure preview content shows parent background */
.layout-preview:not(.has-template):empty {
    display: block;
    width: 100%;
    height: 100%;
    background: inherit; /* Inherit background from parent */
}

.layout-cell {
    position: relative;
    background: #ffecb3;
    border: var(--cell-border, 2px) dashed #ff8f00;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    overflow: hidden;
    box-shadow: inset 0 1px 3px rgba(255, 143, 0, 0.2);
    /* Size will be set dynamically by JavaScript */
}

.layout-cell.single-cell {
    /* Ô vuông đơn lẻ */
    background: #fff;
}

.layout-cell.merged-cell {
    /* Ô đã merge */
    background: #f8f9ff;
}

.layout-cell:hover {
    border-color: #667eea;
    background: #f0f4ff;
    transform: scale(1.02);
}

.layout-cell.has-image {
    border-style: solid;
    border-color: #28a745;
    padding: 0;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.layout-cell.has-image:hover {
    box-shadow: 0 6px 12px rgba(0,0,0,0.15);
    transform: scale(1.03);
}

.layout-cell .placeholder {
    text-align: center;
    color: #6c757d;
    font-size: 0.6rem;
    padding: 5px;
    line-height: 1.2;
}

.layout-cell .placeholder .icon {
    font-size: 1rem;
    margin-bottom: 3px;
    display: block;
}

.layout-cell .placeholder small {
    display: block;
    margin-top: 2px;
    font-size: 0.5rem;
    color: #999;
}

.cell-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: move;
    transition: transform 0.2s ease;
}

.cell-image:hover {
    transform: scale(1.05);
}

.image-controls {
    position: absolute;
    top: 5px;
    right: 5px;
    display: flex;
    gap: 5px;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.layout-cell:hover .image-controls {
    opacity: 1;
}

.control-btn {
    background: rgba(0,0,0,0.7);
    color: white;
    border: none;
    border-radius: 4px;
    width: 24px;
    height: 24px;
    font-size: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.control-btn:hover {
    background: rgba(0,0,0,0.9);
}

/* Image adjustment modal */
.image-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.8);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    overflow: hidden;
}

.modal-content {
    background: white;
    border-radius: 10px;
    padding: 20px;
    max-width: 90%;
    max-height: 90%;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.image-editor {
    position: relative;
    overflow: hidden;
    border: 2px solid #dee2e6;
    border-radius: 8px;
    background: #f8f9fa;
    margin-bottom: 15px;
}

.editable-image {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    cursor: move;
    transition: transform 0.1s ease;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
}

.editor-controls {
    margin-top: 15px;
    display: flex;
    gap: 10px;
    align-items: center;
    justify-content: center;
}

.zoom-control {
    display: flex;
    align-items: center;
    gap: 10px;
}

.zoom-slider {
    width: 150px;
}

/* Enlarged Image Modal */
.enlarged-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    cursor: pointer;
    backdrop-filter: blur(5px);
}

.enlarged-modal .image-container {
    max-width: 90%;
    max-height: 90%;
    background: white;
    border-radius: 12px;
    padding: 20px;
    position: relative;
    overflow: hidden;
    box-shadow: 0 20px 40px rgba(0,0,0,0.3);
}

.enlarged-modal .close-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border: none;
    border-radius: 50%;
    width: 30px;
    height: 30px;
    cursor: pointer;
    font-size: 16px;
    z-index: 1;
    transition: background 0.3s ease;
}

.enlarged-modal .close-btn:hover {
    background: rgba(0, 0, 0, 0.9);
}

/* Enhanced Image Controls for better visibility */

/* Template Mode Section */
.template-mode-section {
    margin: 15px 0;
    padding: 12px;
    background: #f8f9fa;
    border-radius: 8px;
    border: 1px solid #dee2e6;
}

.template-mode-label {
    display: block;
    margin: 8px 0;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 6px;
    transition: all 0.2s ease;
    background: white;
    border: 2px solid transparent;
}

.template-mode-label:hover {
    background: #e3f2fd;
}

.template-mode-label input[type="radio"] {
    margin-right: 8px;
    transform: scale(1.2);
}

.template-mode-label input[type="radio"]:checked + span {
    font-weight: bold;
    color: #667eea;
}

.template-mode-label:has(input[type="radio"]:checked) {
    background: #e3f2fd;
    border-color: #667eea;
}

.no-layout {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #999;
    text-align: center;
    border: 2px dashed #ddd;
    border-radius: 10px;
    background: #fafafa;
}

/* Template image drop grid container */
.image-drop-grid {
    display: grid;
    width: 100%;
    height: 100%;
    gap: var(--template-gap, 0px);
    background: transparent; /* Background color will be set by parent */
    border-radius: var(--template-radius, 0px);
    border: var(--template-border, 0px) solid #ffffff;
    box-sizing: border-box;
    overflow: hidden;
    position: relative; /* Changed from absolute to relative for proper containment */
    top: 0;
    left: 0;
    contain: layout style; /* Optimization for clipping performance */
}

/* Group-specific CSS variables for image-drop-grid */
#group-1 .image-drop-grid {
    gap: var(--template-gap-group-1, 0px);
    border-radius: var(--template-radius-group-1, 0px);
    border: var(--template-border-group-1, 0px) solid #ffffff;
}

#group-2 .image-drop-grid {
    gap: var(--template-gap-group-2, 0px);
    border-radius: var(--template-radius-group-2, 0px);
    border: var(--template-border-group-2, 0px) solid #ffffff;
}

#group-3 .image-drop-grid {
    gap: var(--template-gap-group-3, 0px);
    border-radius: var(--template-radius-group-3, 0px);
    border: var(--template-border-group-3, 0px) solid #ffffff;
}

#group-4 .image-drop-grid {
    gap: var(--template-gap-group-4, 0px);
    border-radius: var(--template-radius-group-4, 0px);
    border: var(--template-border-group-4, 0px) solid #ffffff;
}

#group-5 .image-drop-grid {
    gap: var(--template-gap-group-5, 0px);
    border-radius: var(--template-radius-group-5, 0px);
    border: var(--template-border-group-5, 0px) solid #ffffff;
}

/* Template image drop cells */
.image-drop-cell {
    background: transparent;
    border: 1px dashed #ced4da;
    border-radius: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    color: #6c757d;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    min-height: 40px;
    overflow: hidden; /* Changed from visible to hidden for proper clipping */
    box-sizing: border-box;
    contain: layout style; /* Optimization for clipping performance */
}

.image-drop-cell:hover {
    border-color: #007bff;
    background: rgba(0,123,255,0.1);
}

.image-drop-cell.dragging {
    border-color: #28a745;
    background: rgba(40,167,69,0.1);
}

/* Template cell images */
.template-cell-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: 0;
    display: block;
    transition: all 0.2s ease;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    touch-action: none; /* Prevent default touch behaviors */
}

.template-cell-image:active {
    cursor: grabbing;
}

.template-cell-image:hover {
    filter: brightness(1.05);
}

/* Selected image container instead of image itself */
.image-drop-cell.selected-cell {
    border: 2px solid #007bff !important;
    box-shadow: 0 0 8px rgba(0, 123, 255, 0.3);
    background: rgba(0, 123, 255, 0.05) !important;
}

/* Keep image itself unaffected by selection */
.template-cell-image {
    border: none !important;
}

/* Styles for disabled sliders and their containers */
.style-group.disabled {
    opacity: 0.5;
    pointer-events: none;
}

.style-slider:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.style-slider:disabled::-webkit-slider-thumb {
    background: #ccc;
    border-color: #aaa;
    box-shadow: none;
    cursor: not-allowed;
}

.style-slider:disabled::-moz-range-thumb {
    background: #ccc;
    border-color: #aaa;
    box-shadow: none;
    cursor: not-allowed;
}

/* ===== DESKTOP SLIDER VISIBILITY FIX ===== */
/* Đảm bảo slider trong Edit Image tab luôn hiển thị trên desktop */
#tab-image-edit .style-slider-container,
#tab-image-edit .style-slider-container input[type="range"] {
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    position: relative !important;
    z-index: 100 !important;
    overflow: visible !important;
}

/* Đảm bảo thumb slider trong Edit Image tab có z-index cao nhất */
#tab-image-edit .style-slider::-webkit-slider-thumb {
    position: relative !important;
    z-index: 1001 !important;
}

#tab-image-edit .style-slider::-moz-range-thumb {
    position: relative !important;
    z-index: 1001 !important;
}

/* Styles for tab system with 3+ tabs */
.tab-navigation.multi-tab .tab-button {
    font-size: 0.7rem;
    padding: 8px 4px;
}

.tab-navigation.multi-tab .tab-button .tab-icon {
    margin-right: 3px;
}

/* Responsive tab adjustments */
@media (max-width: 1200px) {
    .tab-navigation.multi-tab .tab-button {
        font-size: 0.65rem;
        padding: 6px 3px;
    }
}

/* Responsive adjustments for group settings */
@media (max-width: 768px) {
    .group-settings {
        padding: 10px;
    }
    
    .group-settings .setting-row {
        flex-direction: column;
        align-items: flex-start;
    }
    
    .group-settings .setting-select,
    .group-settings .color-picker,
    .group-settings .quality-value {
        margin-left: 0;
        margin-top: 5px;
        max-width: 100%;
    }
    
    .group-settings .color-options {
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
    }
    
    .group-settings .color-presets {
        width: 100%;
        justify-content: flex-start;
    }
}
