:root {
    --primary-color: #231F20;
    --text-color: #333333;
    --text-muted: #666666;
    --bg-color: #f3f4f6;
    --surface-color: #ffffff;
    --border-color: #e5e7eb;
    --accent-color: #231F20;
    /* RGB for accents (slider glow, labels) — override in themed templates */
    --theme-accent-rgb: 35, 31, 32;
    --radius-md: 12px;
    --radius-lg: 16px;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --sidebar-width: 440px;
    --app-pad-x: clamp(0.75rem, 3vw, 1.25rem);
}

body {
    font-family: 'Inter', sans-serif;
    margin: 0;
    padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
    background-color: var(--bg-color);
    color: var(--text-color);
    min-height: 100vh;
    min-height: 100dvh;
    overflow: hidden;
    overflow-x: clip;
}

/* Layout */
.app-container {
    display: flex;
    height: 100vh;
    height: 100dvh;
    width: 100%;
    max-width: 100%;
    min-width: 0;
}

/* Sidebar */
.sidebar {
    width: min(var(--sidebar-width), 100%);
    max-width: 100%;
    background: var(--surface-color);
    background: linear-gradient(180deg, var(--surface-color) 0%, color-mix(in srgb, var(--surface-color) 96%, var(--bg-color)) 100%);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    z-index: 10;
    box-shadow: 4px 0 24px -8px rgba(0, 0, 0, 0.08), var(--shadow-sm);
    flex-shrink: 0;
    animation: slideInLeft 0.5s cubic-bezier(0.16, 1, 0.3, 1);
}

.sidebar-header {
    padding: clamp(1rem, 3vw, 1.5rem);
    border-bottom: 1px solid var(--border-color);
    background: var(--surface-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
    min-width: 0;
}

.sidebar-logo img {
    height: 75px;
    width: auto;
}

.sidebar-title {
    font-size: clamp(0.85rem, 2.8vw, 1.1rem);
    font-weight: 700;
    margin: 0;
    color: var(--primary-color);
    letter-spacing: -0.025em;
    line-height: 1.2;
    word-break: break-word;
    hyphens: auto;
    min-width: 0;
    flex: 1 1 140px;
}

.pro-badge {
    font-size: 0.7rem;
    background: #000;
    color: #fff;
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--app-pad-x);
    -webkit-overflow-scrolling: touch;
}

.sidebar-footer {
    padding: var(--app-pad-x);
    border-top: 1px solid var(--border-color);
    background: var(--surface-color);
    box-shadow: 0 -0.125rem 0.25rem rgba(0,0,0,.075); /* Match shadow-sm above, like header-content */
}

/* Scrollbar styling for sidebar */
.sidebar-content::-webkit-scrollbar {
    width: 6px;
}
.sidebar-content::-webkit-scrollbar-thumb {
    background-color: color-mix(in srgb, rgb(var(--theme-accent-rgb)) 18%, #e0e0e0);
    border-radius: 4px;
}

.sidebar-content::-webkit-scrollbar-thumb:hover {
    background-color: color-mix(in srgb, rgb(var(--theme-accent-rgb)) 28%, #d0d0d0);
}

/* Main Content Area */
.main-content {
    flex: 1;
    position: relative;
    background-color: var(--bg-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background-image:
        radial-gradient(color-mix(in srgb, rgb(var(--theme-accent-rgb)) 28%, #d1d5db) 1px, transparent 1px);
    background-size: 24px 24px;
}

/* Preview Canvas */
.preview-canvas {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: clamp(0.75rem, 3vw, 2rem);
    overflow: hidden;
    overflow-x: hidden;
    position: relative;
    min-width: 0;
    min-height: 0;
}

/* Upload & Room Areas */
.section-label {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-weight: 700;
    color: #6b6b6b;
    color: color-mix(in srgb, rgb(var(--theme-accent-rgb)) 38%, #8e8e8e);
    margin-bottom: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.55rem;
}

.section-label .bi {
    opacity: 0.88;
    font-size: 1.05em;
}

.upload-zone {
    border: 2px dashed #e5e7eb;
    border-radius: var(--radius-lg);
    padding: clamp(1.5rem, 5vw, 3rem) clamp(1rem, 3vw, 1.5rem);
    text-align: center;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background: #fdfdfd;
}

.upload-zone:hover {
    border-color: rgb(var(--theme-accent-rgb));
    background: color-mix(in srgb, rgb(var(--theme-accent-rgb)) 4%, #f8fafc);
    transform: translateY(-2px);
    box-shadow: 0 8px 24px -6px rgba(var(--theme-accent-rgb), 0.12);
}

.upload-icon {
    font-size: 2.5rem;
    color: #cbd5e1;
    margin-bottom: 1rem;
    transition: color 0.3s;
}

.upload-zone:hover .upload-icon {
    color: rgb(var(--theme-accent-rgb));
}

.upload-zone.dragover {
    border-color: rgb(var(--theme-accent-rgb));
    background: color-mix(in srgb, rgb(var(--theme-accent-rgb)) 8%, #f8fafc);
    transform: scale(0.995);
}

.shake-error {
    animation: shake 0.5s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
    border-color: #dc3545 !important;
    background-color: rgba(220, 53, 69, 0.05) !important;
}

@keyframes shake {
    0%,
    100% {
        transform: translateX(0);
    }
    10%,
    30%,
    50%,
    70%,
    90% {
        transform: translateX(-5px);
    }
    20%,
    40%,
    60%,
    80% {
        transform: translateX(5px);
    }
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translate3d(0, 12px, 0);
    }
    to {
        opacity: 1;
        transform: translate3d(0, 0, 0);
    }
}

.fade-in-up {
    animation: fadeInUp 0.5s ease-out forwards;
}

/* Sample room carousel (design-3) */
.room-thumb-slider-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 1rem;
}

.room-thumb-list {
    --room-thumb-w: clamp(4.5rem, 24vw, 6.5rem);
    display: flex;
    gap: 10px;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-behavior: auto;
    padding: 2px 0;
    flex: 1;
    min-width: 0;
    scrollbar-width: none;
    -ms-overflow-style: none;
    -webkit-overflow-scrolling: touch;
}

.room-thumb-list::-webkit-scrollbar {
    display: none;
}

.btn-slider {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: var(--surface-color);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease;
    z-index: 5;
    flex-shrink: 0;
    color: var(--text-color);
}

.btn-slider:hover {
    background: rgb(var(--theme-accent-rgb));
    color: #fff;
    border-color: rgb(var(--theme-accent-rgb));
}

.btn-slider i {
    font-size: 0.8rem;
}

.room-thumb-item {
    /* Fixed width so huge source images cannot expand cells and break scroll math */
    position: relative;
    flex: 0 0 var(--room-thumb-w);
    width: var(--room-thumb-w);
    min-width: var(--room-thumb-w);
    max-width: var(--room-thumb-w);
    aspect-ratio: 4 / 3;
    height: auto;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    contain: layout style;
    background: color-mix(in srgb, var(--border-color) 35%, var(--surface-color));
}

.room-thumb-item:hover {
    border-color: color-mix(in srgb, rgb(var(--theme-accent-rgb)) 35%, var(--border-color));
}

.room-thumb-item.active {
    border-color: rgb(var(--theme-accent-rgb));
    box-shadow: 0 0 0 1px rgba(var(--theme-accent-rgb), 0.25);
}

.room-thumb-loader {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    background: color-mix(in srgb, var(--border-color) 28%, var(--surface-color));
    transition: opacity 0.28s ease, visibility 0.28s ease;
}

.room-thumb-item.is-loaded .room-thumb-loader {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.room-thumb-loader-spin {
    color: rgb(var(--theme-accent-rgb));
    opacity: 0.9;
}

.room-thumb-img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    object-fit: cover;
    object-position: center;
    display: block;
    opacity: 0;
    transition: opacity 0.35s ease;
}

.room-thumb-item.is-loaded .room-thumb-img {
    opacity: 1;
}

/* Tile upload uses .upload-zone + .room-preview-container (see #tilePreviewContainer) */
.tile-upload-slot #tileUploadArea.selection-disabled {
    pointer-events: none;
    opacity: 0.65;
}

.tile-upload-slot.shake-error {
    animation: shake 0.5s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

.tile-upload-slot.shake-error .upload-zone {
    border-color: #dc3545 !important;
    background-color: rgba(220, 53, 69, 0.05) !important;
}

.tile-preview-size {
    height: clamp(140px, 32vw, 200px);
    max-height: 42vh;
    margin-top: 0;
}

.tile-upload-slot .room-preview-container:not(.d-none) {
    margin-top: 0.75rem;
}

/* Floor type custom dropdown (trigger + themed list panel) */
.floor-type-dropdown {
    --floor-select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%236b7280' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3E%3C/svg%3E");
    position: relative;
    width: 100%;
}

.floor-type-trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    width: 100%;
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.4;
    color: var(--text-color);
    background-color: #fdfdfd;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.65rem 0.75rem 0.65rem 0.9rem;
    cursor: pointer;
    text-align: left;
    transition: border-color 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease;
}

.floor-type-trigger-text {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.floor-type-trigger-chevron {
    flex-shrink: 0;
    width: 12px;
    height: 12px;
    background: var(--floor-select-chevron) center / contain no-repeat;
    opacity: 0.85;
    transition: transform 0.2s ease;
}

.floor-type-dropdown.is-open .floor-type-trigger-chevron {
    transform: rotate(180deg);
}

.floor-type-trigger:hover:not(:disabled) {
    border-color: #d1d5db;
    background-color: var(--surface-color);
}

.floor-type-trigger:focus {
    border-color: rgb(var(--theme-accent-rgb));
    box-shadow: 0 0 0 3px rgba(var(--theme-accent-rgb), 0.14);
    outline: none;
}

.floor-type-dropdown.is-open .floor-type-trigger {
    border-color: rgb(var(--theme-accent-rgb));
    box-shadow: 0 0 0 3px rgba(var(--theme-accent-rgb), 0.14);
}

.floor-type-trigger.is-placeholder .floor-type-trigger-text {
    color: var(--text-muted);
    font-weight: 400;
}

.floor-type-trigger:disabled {
    opacity: 0.72;
    cursor: not-allowed;
    background-color: #f3f4f6;
    color: var(--text-muted);
}

.floor-type-panel {
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 6px);
    z-index: 100;
    margin: 0;
    padding: 0.35rem;
    list-style: none;
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    max-height: 220px;
    overflow-y: auto;
}

.floor-type-panel::-webkit-scrollbar {
    width: 6px;
}

.floor-type-panel::-webkit-scrollbar-thumb {
    background-color: #e0e0e0;
    border-radius: 3px;
}

.floor-type-option {
    display: flex;
    align-items: center;
    width: 100%;
    margin: 0;
    padding: 0.55rem 0.7rem;
    border: none;
    border-radius: calc(var(--radius-md) - 4px);
    background: transparent;
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-color);
    text-align: left;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
}

.floor-type-option:hover {
    background-color: #f3f4f6;
}

.floor-type-option[aria-selected="true"] {
    background-color: rgba(var(--theme-accent-rgb), 0.08);
    color: rgb(var(--theme-accent-rgb));
    font-weight: 600;
}

.floor-type-option.floor-type-option-muted {
    color: var(--text-muted);
    font-weight: 400;
    font-size: 0.8125rem;
    cursor: default;
    pointer-events: none;
}

.floor-type-option.floor-type-option-muted:hover {
    background-color: transparent;
}

.room-preview-container {
    border-radius: var(--radius-lg);
    overflow: hidden;
    position: relative;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-color);
    transition: transform 0.3s;
    background: #f8f9fa;
    height: clamp(200px, 42vw, 260px);
    max-height: 50vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.room-preview-container:hover {
    box-shadow: var(--shadow-md);
}

.room-preview-container img {
    max-width: 100%;
    max-height: 100%;
    width: auto; /* Allow width to adjust */
    height: auto;
    object-fit: contain;
    display: block;
    cursor: pointer;
}

.room-actions {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(255, 255, 255, 0.95);
    padding: 0.5rem 0.75rem;
    display: flex;
    justify-content: space-between;
    backdrop-filter: blur(8px);
    border-top: 1px solid rgba(0,0,0,0.05);
    transform: translateY(100%);
    transition: transform 0.3s ease;
}

.room-preview-container:hover .room-actions {
    transform: translateY(0);
}

/* Touch devices: keep room actions usable without hover */
@media (hover: none) and (pointer: coarse) {
    .room-preview-container .room-actions {
        transform: translateY(0);
        padding: 0.6rem 0.75rem;
    }
}

/* Tiles Grid */
.tile-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}

.tile-grid-modal {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

/* Fix for empty grid height */
.tile-grid:empty {
    display: flex;
}

.tile-card {
    border-radius: var(--radius-md);
    overflow: hidden;
    border: 2px solid transparent;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background: white;
    box-shadow: var(--shadow-sm);
    position: relative;
    margin-bottom: 0; /* Remove potential margin */
}

.tile-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
}

.tile-card.selected {
    border-color: rgb(var(--theme-accent-rgb));
    transform: translateY(-2px);
    box-shadow: 0 6px 20px -4px rgba(var(--theme-accent-rgb), 0.28), 0 2px 8px rgba(0, 0, 0, 0.06);
}

/* Disable tile selection during processing */
.tile-grid.selection-disabled .tile-card,
.tile-grid-modal.selection-disabled .tile-card {
    pointer-events: none;
    cursor: not-allowed;
    opacity: 0.85;
}

/* Updated Selection Overlay */
.tile-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.3);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    opacity: 0;
    transition: opacity 0.2s;
}

.tile-card:hover .tile-overlay,
.tile-card.selected .tile-overlay {
    opacity: 1;
}

/* Top Right Actions (Zoom) */
.tile-actions {
    position: absolute;
    top: 6px;
    right: 6px;
    z-index: 10;
}

.tile-zoom-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.94);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(var(--theme-accent-rgb), 0.12);
    color: rgb(var(--theme-accent-rgb));
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease, color 0.2s ease;
    padding: 0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.tile-zoom-btn:hover {
    transform: scale(1.08);
    background: rgb(var(--theme-accent-rgb));
    color: #fff;
    border-color: transparent;
    box-shadow: 0 4px 14px rgba(var(--theme-accent-rgb), 0.35);
}

.tile-zoom-btn i {
    font-size: 0.75rem;
}

/* Center Selection Icon */
.tile-select-icon {
    /* Centered by flex parent */
    transform: scale(0.9);
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.tile-card:hover .tile-select-icon {
     transform: scale(1);
}

/* Force override bootstrap fs-4 if needed */
.tile-select-icon i {
    font-size: 1.75rem !important; 
    filter: drop-shadow(0 2px 8px rgba(0,0,0,0.3));
    color: white;
}

.tile-card.selected .tile-select-icon i {
    color: #22c55e;
    font-size: 2.25rem !important; /* Larger when selected */
}

.tile-image-container {
    position: relative;
    aspect-ratio: 1;
    overflow: hidden;
}

.tile-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.5s ease;
}

.tile-card:hover .tile-image {
    transform: scale(1.1);
}

.tile-name {
    padding: 0.6rem;
    font-size: 0.7rem;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    background: #fff;
    border-top: 1px solid #f3f4f6;
    font-weight: 500;
}

/* Preview Gallery (The result) */
.preview-gallery-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.preview-gallery {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    animation: fadeIn 0.8s ease-out;
}

.preview-placeholder {
    text-align: center;
    color: var(--text-muted);
    animation: fadeIn 1s ease-out;
    padding: 0 clamp(0.5rem, 4vw, 1.5rem);
    max-width: 42rem;
    margin-inline: auto;
}

.preview-image {
    max-width: 95%; /* Slightly larger */
    max-height: 90vh;
    object-fit: contain;
    border-radius: calc(var(--radius-lg) + 2px);
    box-shadow:
        0 24px 56px -12px rgba(0, 0, 0, 0.22),
        0 8px 24px rgba(var(--theme-accent-rgb), 0.08);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.preview-image:hover {
    transform: scale(1.01);
}

/* Before/after comparison slider (generated result) */
.preview-card {
    width: 100%;
    max-width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    animation: fadeIn 0.55s cubic-bezier(0.22, 1, 0.36, 1);
}

.comparison-slider-container {
    --slider-line-w: 3px;
    position: relative;
    width: 100%;
    max-width: min(100% - 0px, 1200px);
    min-height: min(280px, 55vw);
    max-height: min(85vh, 85dvh);
    aspect-ratio: 16 / 10;
    margin: 0 auto;
    background: linear-gradient(165deg, #141414 0%, #0a0a0a 45%, #121212 100%);
    border-radius: calc(var(--radius-lg) + 4px);
    overflow: hidden;
    border: 1px solid rgba(var(--theme-accent-rgb), 0.22);
    box-shadow:
        0 0 0 1px rgba(255, 255, 255, 0.06) inset,
        0 28px 56px -14px rgba(0, 0, 0, 0.35),
        0 12px 32px -8px rgba(var(--theme-accent-rgb), 0.18);
    touch-action: none;
    cursor: ew-resize;
    user-select: none;
    transition: box-shadow 0.35s ease, border-color 0.35s ease;
}

.comparison-slider-container:hover {
    border-color: rgba(var(--theme-accent-rgb), 0.32);
    box-shadow:
        0 0 0 1px rgba(255, 255, 255, 0.07) inset,
        0 32px 64px -12px rgba(0, 0, 0, 0.38),
        0 16px 40px -6px rgba(var(--theme-accent-rgb), 0.22);
}

.comparison-img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
}

.comparison-img-before {
    z-index: 1;
}

.comparison-after-wrap {
    position: absolute;
    inset: 0;
    z-index: 2;
    clip-path: inset(0 50% 0 0);
    pointer-events: none;
}

.comparison-handle {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50%;
    width: var(--slider-line-w);
    transform: translateX(-50%);
    background: linear-gradient(
        180deg,
        rgba(255, 255, 255, 0.15) 0%,
        #fff 14%,
        #fff 86%,
        rgba(255, 255, 255, 0.15) 100%
    );
    z-index: 3;
    box-shadow:
        0 0 0 1px rgba(var(--theme-accent-rgb), 0.45),
        0 0 18px rgba(var(--theme-accent-rgb), 0.55),
        0 0 40px rgba(var(--theme-accent-rgb), 0.2),
        0 2px 16px rgba(0, 0, 0, 0.5);
    pointer-events: none;
}

.comparison-handle-knob {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: linear-gradient(145deg, #ffffff 0%, #f5f5f5 100%);
    border: 2px solid rgba(var(--theme-accent-rgb), 0.85);
    box-shadow:
        0 0 0 3px rgba(255, 255, 255, 0.35),
        0 6px 22px rgba(0, 0, 0, 0.35),
        0 0 28px rgba(var(--theme-accent-rgb), 0.35);
    pointer-events: auto;
    cursor: ew-resize;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.comparison-slider-container:active .comparison-handle-knob,
.comparison-handle-knob:hover {
    transform: translate(-50%, -50%) scale(1.04);
    box-shadow:
        0 0 0 3px rgba(255, 255, 255, 0.45),
        0 8px 28px rgba(0, 0, 0, 0.4),
        0 0 36px rgba(var(--theme-accent-rgb), 0.45);
}

.comparison-handle-knob::before,
.comparison-handle-knob::after {
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-style: solid;
    opacity: 0.65;
}

.comparison-handle-knob::before {
    left: 10px;
    border-width: 5px 7px 5px 0;
    border-color: transparent rgb(var(--theme-accent-rgb)) transparent transparent;
}

.comparison-handle-knob::after {
    right: 10px;
    border-width: 5px 0 5px 7px;
    border-color: transparent transparent transparent rgb(var(--theme-accent-rgb));
}

.comparison-label {
    position: absolute;
    top: 12px;
    z-index: 4;
    padding: 7px 15px;
    border-radius: 999px;
    font-size: 0.68rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #fff;
    pointer-events: none;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.12);
}

.comparison-label-before {
    left: 12px;
    background: rgba(0, 0, 0, 0.55);
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

.comparison-label-after {
    right: 12px;
    background: linear-gradient(
        135deg,
        rgba(var(--theme-accent-rgb), 0.92) 0%,
        rgba(var(--theme-accent-rgb), 0.72) 100%
    );
    box-shadow:
        0 4px 18px rgba(var(--theme-accent-rgb), 0.45),
        0 0 0 1px rgba(255, 255, 255, 0.15) inset;
}

.comparison-preview-actions {
    position: absolute;
    bottom: 12px;
    right: 12px;
    z-index: 5;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
}

.comparison-download-btn,
.comparison-fullscreen-btn {
    position: relative;
    bottom: auto;
    right: auto;
    border-radius: 50px;
    padding: 0.4rem 0.75rem;
    background: rgba(255, 255, 255, 0.97);
    border: 1px solid rgba(var(--theme-accent-rgb), 0.35);
    box-shadow:
        0 4px 14px rgba(0, 0, 0, 0.12),
        0 0 0 1px rgba(255, 255, 255, 0.5) inset;
    cursor: pointer;
    line-height: 1;
    color: rgb(var(--theme-accent-rgb));
    transition: background 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease, color 0.2s ease;
}

.comparison-download-btn:hover,
.comparison-fullscreen-btn:hover {
    background: rgb(var(--theme-accent-rgb));
    color: #fff;
    border-color: transparent;
    transform: translateY(-1px);
    box-shadow:
        0 8px 22px rgba(var(--theme-accent-rgb), 0.4),
        0 0 0 1px rgba(255, 255, 255, 0.2) inset;
}

.comparison-download-btn i,
.comparison-fullscreen-btn i {
    font-size: 1.05rem;
    vertical-align: -0.1em;
}

@media (max-width: 576px) {
    .comparison-slider-container {
        aspect-ratio: 4 / 3;
        min-height: 200px;
        max-height: min(72vh, 72dvh);
    }

    .comparison-handle-knob {
        width: 40px;
        height: 40px;
    }

    .comparison-label {
        font-size: 0.6rem;
        padding: 5px 10px;
        top: 8px;
    }

    .comparison-label-before {
        left: 8px;
    }

    .comparison-label-after {
        right: 8px;
    }

    .comparison-preview-actions {
        bottom: max(8px, env(safe-area-inset-bottom, 0px));
        right: max(8px, env(safe-area-inset-right, 0px));
    }
}

@media (max-width: 992px) and (orientation: landscape) {
    .comparison-slider-container {
        aspect-ratio: auto;
        min-height: 180px;
        max-height: min(78vh, 78dvh);
    }
}

/* Placeholder Utility */
.tile-grid-placeholder {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    background: #f9fafb;
    border-radius: var(--radius-md);
    border: 2px dashed #e5e7eb;
}

.placeholder-content {
    text-align: center;
    padding: 2rem;
    color: #9ca3af;
}

/* Generate Button */
#generateBtn {
    transition: all 0.3s ease;
    border: none;
    background: linear-gradient(135deg, rgb(var(--theme-accent-rgb)) 0%, color-mix(in srgb, rgb(var(--theme-accent-rgb)) 70%, #000) 100%);
    letter-spacing: 0.02em;
}

#generateBtn:not(:disabled):hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(var(--theme-accent-rgb), 0.28);
}

#generateBtn:disabled {
    background: #e5e7eb;
    color: #9ca3af;
}

#viewAllTilesBtn:not(:disabled):hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

/* Animations */
@keyframes slideInLeft {
    from { transform: translateX(-20px); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Lightbox */
.lightbox-overlay {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    background-color: rgba(0, 0, 0, 0.92);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    backdrop-filter: blur(8px);
    padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
}

.lightbox-overlay.active {
    opacity: 1;
    visibility: visible;
}

.lightbox-content {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    min-height: 0;
}

.lightbox-content img {
    max-width: min(92vw, 100%);
    max-height: min(88vh, 88dvh);
    object-fit: contain;
    transition: transform 0.2s ease-out;
    transform-origin: center center;
    cursor: grab;
    user-select: none;
    touch-action: none;
}

.lightbox-content img.grabbing {
    cursor: grabbing;
    transition: none;
}

.lightbox-controls {
    position: absolute;
    top: max(0.75rem, env(safe-area-inset-top, 0px));
    right: max(0.75rem, env(safe-area-inset-right, 0px));
    z-index: 2001;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
    max-width: calc(100% - 1.5rem);
}

.lightbox-controls .btn {
    flex-shrink: 0;
}

.lightbox-caption {
    position: absolute;
    bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%);
    max-width: calc(100% - 2rem);
    color: white;
    background: rgba(0, 0, 0, 0.58);
    background: linear-gradient(135deg, rgba(var(--theme-accent-rgb), 0.88), rgba(0, 0, 0, 0.72));
    padding: 8px 16px;
    border-radius: 999px;
    font-size: clamp(0.75rem, 2.5vw, 14px);
    pointer-events: none;
    text-align: center;
    line-height: 1.3;
    border: 1px solid rgba(255, 255, 255, 0.14);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

/* “View all tiles” modal width helper */
.modal.max-width-900 .modal-dialog {
    max-width: min(900px, calc(100vw - 2rem));
    margin-inline: auto;
}

/* Responsive layout — laptop / desktop: wider control column */
@media (min-width: 993px) and (max-width: 1599px) {
    .sidebar {
        width: clamp(360px, 36vw, 520px);
    }
}

@media (min-width: 1600px) {
    .sidebar {
        width: clamp(320px, 30vw, 480px);
    }
}

@media (max-width: 992px) {
    body {
        overflow: auto;
        overflow-x: clip;
        -webkit-overflow-scrolling: touch;
    }

    .app-container {
        flex-direction: column;
        height: auto;
        min-height: 100vh;
        min-height: 100dvh;
        overflow-x: clip;
    }

    .sidebar {
        width: 100%;
        max-width: none;
        height: auto;
        flex-shrink: 0;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        box-shadow: var(--shadow-sm);
    }

    .main-content {
        flex: 1 1 auto;
        min-height: min(420px, 65vh);
        min-height: min(420px, 65dvh);
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }

    .preview-canvas {
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        align-items: flex-start;
        padding-top: 1rem;
        padding-bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    }

    .preview-gallery {
        width: 100%;
        padding-bottom: 0.5rem;
    }

    /* Sidebar is full-width: allow more columns when space allows */
    .tile-grid {
        grid-template-columns: repeat(auto-fill, minmax(min(100%, 100px), 1fr));
        gap: 10px;
    }

    .tile-grid-modal {
        grid-template-columns: repeat(auto-fill, minmax(min(100%, 130px), 1fr));
        gap: 10px;
    }
}

@media (max-width: 768px) {
    #generateBtn {
        min-height: 48px;
    }
}

@media (max-width: 480px) {
    .tile-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 9px;
    }
}

@media (max-width: 380px) {
    .tile-name {
        font-size: 0.65rem;
        padding: 0.45rem;
    }

    .tile-grid-modal {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Scanning Animation Overlay */
.scanning-container {
    position: relative;
    overflow: hidden;
}

.scanning-container::before {
    content: '';
    position: absolute;
    top: -50%;
    left: 0;
    width: 100%;
    height: 50%; /* Gradient height */
    background: linear-gradient(
        to bottom,
        transparent 0%,
        rgba(var(--theme-accent-rgb), 0.22) 50%,
        transparent 100%
    );
    z-index: 10;
    animation: scanAnimation 2s linear infinite;
    pointer-events: none;
}

.scanning-container::after {
    content: '';
    position: absolute;
    top: -50%; /* Start above */
    left: 0;
    width: 100%;
    height: 2px;
    background: rgba(var(--theme-accent-rgb), 0.85);
    box-shadow: 0 0 12px rgba(var(--theme-accent-rgb), 0.55);
    z-index: 11;
    animation: scanAnimation 2s linear infinite;
    pointer-events: none;
}

@keyframes scanAnimation {
    0% { transform: translateY(0%); }
    100% { transform: translateY(400%); } /* Move enough to cover height */
}

/* Modern Loader Animation */
.modern-loader {
    width: 48px;
    height: 48px;
    display: inline-block;
    position: relative;
}
.modern-loader::after,
.modern-loader::before {
    content: '';  
    box-sizing: border-box;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: 3px solid var(--text-muted);
    position: absolute;
    left: 0;
    top: 0;
    animation: animloader 2s linear infinite;
}
.modern-loader::after {
    animation-delay: 1s;
}

@keyframes animloader {
    0% { transform: scale(0); opacity: 1; }
    100% { transform: scale(1); opacity: 0; }
}
