/* vCard Theme Selection Enhancements */

/* Z-index hierarchy system */
:root {
    --z-normal: 0;
    --z-hover: 10;
    --z-expanded: 50;
    --z-modal: 100;
}

/* Theme Selection Grid Container - Fixed Layout */
#theme-selection {
    /* Baseline (fallback) flex layout */
    display: flex;
    flex-wrap: wrap;
    /* Neutralize Bootstrap .row gutters for consistent math */
    margin-left: 0 !important;
    margin-right: 0 !important;
    margin-top: -0.5rem;
    margin-bottom: -0.5rem;
    justify-content: flex-start;
    align-content: flex-start;
    gap: 2rem 1rem; /* Fallback gaps for expansion room */
    overflow: visible !important; /* Allow expansion beyond bounds */
    padding: 2rem 1rem !important; /* Fallback padding for expansion room */
}

#theme-selection > div {
    padding: 0.5rem;
    box-sizing: border-box;
    /* Baseline flex sizing (simple, avoids width/flex-basis conflicts) */
    flex: 0 0 25% !important;
    max-width: 25% !important;
    width: auto !important;
    min-width: 0;
}

/* Override all Bootstrap column classes for theme selection */
#theme-selection .col-lg-3,
#theme-selection .col-md-4,
#theme-selection .col-sm-6,
#theme-selection .col-6,
#theme-selection [class*="col-"] {
    width: calc(25% - 0px) !important;
    flex-basis: calc(25% - 0px) !important;
    max-width: calc(25% - 0px) !important;
    flex: 0 0 calc(25% - 0px) !important;
}

/* Force 4-column layout on all screen sizes for theme selection */
@media (max-width: 991px) {
    #theme-selection > div,
    #theme-selection .col-lg-3,
    #theme-selection .col-md-4,
    #theme-selection .col-sm-6,
    #theme-selection .col-6 {
        width: calc(25% - 0px) !important; /* Keep 4 columns even on medium screens */
        flex-basis: calc(25% - 0px) !important;
        max-width: calc(25% - 0px) !important;
    }
}

@media (max-width: 767px) {
    #theme-selection > div,
    #theme-selection .col-lg-3,
    #theme-selection .col-md-4,
    #theme-selection .col-sm-6,
    #theme-selection .col-6 {
        width: calc(50% - 0px) !important; /* 2 columns on small screens */
        flex-basis: calc(50% - 0px) !important;
        max-width: calc(50% - 0px) !important;
    }
}

/* Robust layout: prefer CSS Grid where supported to avoid flex rounding/gutter issues */
@supports (display: grid) {
    #theme-selection {
        display: grid !important;
        margin: 0 !important; /* cancel Bootstrap row negative margins */
        grid-template-columns: repeat(4, minmax(0, 1fr));
        grid-auto-flow: row dense; /* fill any accidental holes */
        gap: 2.5rem 1.25rem !important; /* Larger gaps for expansion room */
        align-items: stretch;
        overflow: visible !important; /* Allow expansion beyond bounds */
        padding: 2.5rem 1.25rem !important; /* Padding for expansion room */
    }

    /* Neutralize Bootstrap grid sizing inside this container */
    #theme-selection > div,
    #theme-selection [class*="col-"] {
        padding: 0 !important;         /* use grid gap, not column padding */
        width: auto !important;
        max-width: none !important;
        flex: initial !important;      /* remove flex sizing */
        flex-basis: auto !important;
    }

    /* Remove mb-3 spacing in favor of uniform grid gap */
    #theme-selection .mb-3 { margin-bottom: 0 !important; }

    /* Responsive columns */
    @media (max-width: 991.98px) {
        #theme-selection { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    }
    @media (max-width: 767.98px) {
        #theme-selection { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    }
}

/* Theme Selection Grid - Force Light Mode */
#theme-selection .theme-option {
    cursor: pointer;
    display: block;
    border-radius: 12px;
    overflow: visible !important; /* Allow image expansion */
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border: 2px solid transparent !important;
    background: #ffffff !important;
    background-color: #ffffff !important;
    color: #333333 !important;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08) !important;
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 180px;
    /* Override Chrome Force Dark Mode */
    color-scheme: light !important;
    forced-color-adjust: none !important;
}

#theme-selection .theme-option:hover {
    transform: translateY(-12px);
    box-shadow: 0 25px 60px rgba(0,0,0,0.2);
    border-color: #ba68c8;
    z-index: var(--z-expanded);
    overflow: visible !important;
}

/* Option A: Transform-based image expansion on hover */
#theme-selection .theme-option:hover .theme-preview-image {
    transform: scale(1.1);
    transform-origin: center top;
    border-radius: 12px;
    box-shadow: 0 20px 50px rgba(0,0,0,0.3);
    z-index: var(--z-expanded);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: auto;
    max-height: none;
    object-fit: contain;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}


#theme-selection .theme-option.active {
    border: 3px solid transparent !important;
    background: linear-gradient(white, white) padding-box,
               linear-gradient(45deg, #9c27b0, #7b1fa2, #ba68c8) border-box !important;
    transform: translateY(-4px) !important;
    box-shadow: 0 10px 30px rgba(156, 39, 176, 0.3) !important;
    animation: fadeInUp 1.8s ease forwards, selectionPulse 1.8s ease-out 1.8s, gradientRotate 9s linear infinite 3.6s !important;
    opacity: 1 !important;
}

#theme-selection .theme-option:hover .theme-overlay {
    opacity: 1;
}

#theme-selection .theme-option.active .theme-check {
    transform: scale(1) rotate(0deg);
}

/* Remove overlay from selected themes completely */
#theme-selection .theme-option.active .theme-overlay {
    display: none !important;
}

/* Only show purple overlay on hover for non-selected themes */
#theme-selection .theme-option:not(.active):hover .theme-overlay {
    opacity: 0.7;
    background: rgba(156, 39, 176, 0.85);
}

/* Checkmark in theme name area */
.theme-checkmark {
    display: none;
    width: 20px;
    height: 20px;
    background: #9c27b0;
    color: white;
    border-radius: 50%;
    font-size: 11px;
    font-weight: bold;
    align-items: center;
    justify-content: center;
    margin-left: 8px;
    box-shadow: 0 2px 8px rgba(156, 39, 176, 0.3);
    flex-shrink: 0;
}

#theme-selection .theme-option.active .theme-checkmark {
    display: inline-flex;
    animation: checkmarkAppear 1.2s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

@keyframes checkmarkAppear {
    0% { 
        transform: scale(0) rotate(180deg);
        opacity: 0;
    }
    100% { 
        transform: scale(1) rotate(0deg);
        opacity: 1;
    }
}

/* Update theme-name to flex for checkmark alignment */
.theme-name {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Theme Preview Card - Force Light Mode */
.theme-preview-card {
    position: relative;
    overflow: visible !important; /* Allow image expansion */
    border-radius: 10px;
    background: #ffffff !important;
    background-color: #ffffff !important;
    height: 180px;
    display: flex;
    flex-direction: column;
    color-scheme: light !important;
    forced-color-adjust: none !important;
}

.theme-preview-image-wrapper {
    position: relative;
    width: 100%;
    height: 120px;
    overflow: visible !important; /* Allow image expansion */
    background: #f8f9fa;
    border-radius: 10px 10px 0 0;
}

.theme-preview-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: top center;
    display: block;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Theme Overlay with Checkmark */
.theme-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,123,255,0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: all 0.3s ease;
    border-radius: 10px 10px 0 0;
}

.theme-check {
    width: 40px;
    height: 40px;
    background: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #007bff;
    font-size: 18px;
    font-weight: bold;
    transform: scale(0) rotate(180deg);
    transition: all 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

/* Theme Info - Force Light Mode */
.theme-info {
    padding: 12px 15px;
    background: #ffffff !important;
    background-color: #ffffff !important;
    border-radius: 0 0 10px 10px;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color-scheme: light !important;
    forced-color-adjust: none !important;
}

.theme-name {
    text-align: center;
    font-weight: 600;
    font-size: 14px;
    color: #495057 !important;
    margin: 0;
    line-height: 1.2;
}

.theme-option:hover .theme-name {
    color: #007bff;
}

.theme-option.active .theme-name {
    color: #007bff;
    font-weight: 700;
}

/* Loading States */
.theme-preview-image[data-loading="true"] {
    background: linear-gradient(90deg, #f0f0f0 25%, transparent 37%, #f0f0f0 63%);
    background-size: 400% 100%;
    animation: shimmer 1.5s ease-in-out infinite;
}

@keyframes shimmer {
    0% { background-position: 100% 0; }
    100% { background-position: -100% 0; }
}

/* Responsive Design */
@media (max-width: 576px) {
    .theme-preview-image-wrapper {
        height: 100px;
    }
    
    .theme-name {
        font-size: 13px;
    }
    
    .theme-info {
        padding: 10px 12px;
    }
    
    #theme-selection .theme-option {
        margin-bottom: 15px;
    }
}

@media (max-width: 768px) {
    .theme-preview-image-wrapper {
        height: 110px;
    }
}

/* Focus States for Accessibility */
.theme-option:focus-within {
    outline: 2px solid #007bff;
    outline-offset: 2px;
}

/* Theme Selection Animation - Slower speed */
.theme-option {
    animation: fadeInUp 1.8s ease forwards;
    opacity: 0;
    transform: translateY(20px);
    transition: all 0.9s cubic-bezier(0.4, 0, 0.2, 1);
}

.theme-option:nth-child(1) { animation-delay: 0.1s; }
.theme-option:nth-child(2) { animation-delay: 0.2s; }
.theme-option:nth-child(3) { animation-delay: 0.3s; }
.theme-option:nth-child(4) { animation-delay: 0.4s; }
.theme-option:nth-child(5) { animation-delay: 0.5s; }
.theme-option:nth-child(6) { animation-delay: 0.6s; }
.theme-option:nth-child(7) { animation-delay: 0.7s; }
.theme-option:nth-child(8) { animation-delay: 0.8s; }
.theme-option:nth-child(9) { animation-delay: 0.9s; }
.theme-option:nth-child(10) { animation-delay: 1.0s; }
.theme-option:nth-child(11) { animation-delay: 1.1s; }
.theme-option:nth-child(12) { animation-delay: 1.2s; }
.theme-option:nth-child(13) { animation-delay: 1.3s; }

@keyframes fadeInUp {
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes gradientRotate {
    0% { 
        background: linear-gradient(white, white) padding-box, 
                   linear-gradient(0deg, #9c27b0, #7b1fa2, #ba68c8) border-box;
    }
    100% { 
        background: linear-gradient(white, white) padding-box, 
                   linear-gradient(360deg, #9c27b0, #7b1fa2, #ba68c8) border-box;
    }
}

/* Custom Scrollbar for Theme Selection */
#customizations_container {
    scrollbar-width: thin;
    scrollbar-color: #007bff #f1f1f1;
}

#customizations_container::-webkit-scrollbar {
    width: 6px;
}

#customizations_container::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
}

#customizations_container::-webkit-scrollbar-thumb {
    background: #007bff;
    border-radius: 3px;
}

#customizations_container::-webkit-scrollbar-thumb:hover {
    background: #0056b3;
}

/* Theme Selection Header */
.form-group label[for="theme"] {
    font-weight: 600;
    color: #495057;
    margin-bottom: 8px;
    font-size: 16px;
}

/* Success Animation for Selection */
@keyframes selectionPulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

.theme-option.active {
    animation: fadeInUp 0.6s ease forwards, selectionPulse 0.6s ease-out 0.6s;
    opacity: 1;
}

/* Dark Mode Support */
@media (prefers-color-scheme: dark) {
    #theme-selection .theme-option {
        background: #2c3e50;
        border-color: #34495e;
        color: #ecf0f1;
    }
    
    .theme-info {
        background: #2c3e50;
    }
    
    .theme-name {
        color: #ecf0f1;
    }
    
    .theme-option:hover .theme-name {
        color: #74b9ff;
    }
    
    .theme-option.active .theme-name {
        color: #74b9ff;
    }
}

/* Print Styles */
/* Override Dark Mode - Force Light Theme Always */
@media (prefers-color-scheme: dark) {
    #theme-selection .theme-option {
        background: #ffffff !important;
        background-color: #ffffff !important;
        color: #333333 !important;
        border: 2px solid transparent !important;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08) !important;
        color-scheme: light !important;
        forced-color-adjust: none !important;
    }
    
    .theme-preview-card {
        background: #ffffff !important;
        background-color: #ffffff !important;
        color-scheme: light !important;
        forced-color-adjust: none !important;
    }
    
    .theme-info {
        background: #ffffff !important;
        background-color: #ffffff !important;
        color-scheme: light !important;
        forced-color-adjust: none !important;
    }
    
    .theme-name {
        color: #495057 !important;
    }
    
    .theme-option:hover .theme-name {
        color: #007bff !important;
    }
    
    .theme-option.active .theme-name {
        color: #007bff !important;
    }
}

@media print {
    #theme-selection {
        display: none;
    }
}
