/**
 * CLS (Cumulative Layout Shift) fixes for mobile optimization
 */

/* Fix image aspect ratios */
img {
    max-width: 100%;
    height: auto;
    width: auto;
    aspect-ratio: attr(width) / attr(height);
}

/* Prevent autocomplete dropdown from causing layout shifts */
.ui-autocomplete {
    max-height: 300px;
    overflow-y: auto;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    z-index: 9999;
}

/* Set fixed dimensions for search inputs to prevent shifts */
input[type="text"],
.search-input {
    width: 100%;
    height: 44px;
    margin-bottom: 10px;
    box-sizing: border-box;
    font-size: 16px; /* Prevents iOS zoom on focus */
}

/* Reserve space for dynamic content */
.autoc-container {
    min-height: 44px;
    position: relative;
    margin-bottom: 10px;
}

/* Ensure content areas have minimum height */
.content-area {
    min-height: 100px;
}

/* Fix CLS for section and main containers */
section {
    min-height: 300px; /* Reserve minimum space */
    display: block;
}

.colored-stripe {
    min-height: 200px; /* Minimum height for colored sections */
    padding: 1.5rem 0;
    display: block;
}

.grid-container {
    min-height: 150px;
    display: block;
}

/* Fix for font icons to prevent layout shifts */
[class^="icon-"],
[class*=" icon-"] {
    display: inline-block;
    width: 1em;
    height: 1em;
    font-size: inherit;
    vertical-align: middle;
}

/* Container for images with fixed aspect ratio */
.image-container {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 56.25%; /* 16:9 aspect ratio */
    overflow: hidden;
}

.image-container img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* Ensure city and district listings don't shift */
.grid-25 {
    min-height: 120px;
}

/* Footer CLS fixes */
footer {
    min-height: 200px;
}

.footer-container {
    min-height: 150px;
    padding: 2rem 1rem;
}

.footer-links {
    min-height: 2rem;
    line-height: 2rem;
}

.footer-links a {
    display: inline-block;
    min-width: 44px;
    min-height: 44px;
    padding: 0.5rem 1rem;
    margin: 0.25rem;
}

.footer-copyright {
    min-height: 1.5rem;
    line-height: 1.5rem;
    margin: 0.5rem 0;
}

/* Back to top button - fixed positioning to prevent CLS */
.back-to-top {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--primary-color, #3498db);
    color: white;
    border-radius: 50%;
    text-decoration: none;
    opacity: 0;
    transition: opacity 0.3s ease;
    z-index: 1000;
}

.back-to-top.visible {
    opacity: 1;
}

/* Responsive iframe container for Google Maps to prevent CLS */
.iframe-rwd {
    position: relative;
    overflow: hidden;
    width: 100%;
    /* Maintain a 1:1 aspect ratio (400/400 = 1). Adjust if other aspect ratios are used. */
    /* For a 4:3 aspect ratio, use padding-bottom: 75%; */
    /* For a 16:9 aspect ratio, use padding-bottom: 56.25%; */
    padding-bottom: 100%; /* Defaulting to 1:1 as iframe is 400x400 */
    height: 0; /* Collapse the div height, padding-bottom will create the space */
}

.iframe-rwd iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: 0; /* Ensure border doesn't add to dimensions */
}

/* H1 and text CLS prevention */
h1 {
    min-height: 2em;
    line-height: 1.3;
    margin: 0 0 1rem 0;
    font-size: 1.75rem;
}

h1 strong {
    font-weight: 700;
}

/* Improve contrast for dark backgrounds */
.colored-stripe.darkblue h1 {
    color: white;
}

.colored-stripe.darkblue .paragraph-title,
.colored-stripe.darkblue a.paragraph-title,
.colored-stripe.darkblue p a {
    color: white !important;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

/* Postal code and street info CLS fixes */
.postal-code {
    display: inline-block;
    min-width: 60px; /* Reserve minimum space */
    min-height: 1.5em; /* Reserve height based on line height */
    font-family: monospace;
    background: rgba(52, 152, 219, 0.1);
    color: var(--primary-color);
    padding: 0.25rem 0.5rem;
    border-radius: 0.25rem;
    vertical-align: baseline;
}

.street-info {
    min-height: 1.5em; /* Reserve minimum height */
    padding: 15px;
    margin: 15px 0;
    background-color: var(--item-bg-light);
    border-radius: 4px;
    border: 1px solid var(--border-light);
    font-size: 1.1em;
    line-height: 1.5;
}

/* Specific CLS fixes for bucuresti_multicode template */
.street-details-container {
    min-height: 200px;
    display: block;
    margin-top: 20px;
}

.multicode-details-list {
    min-height: 100px;
    margin: 15px 0;
}

.multicode-details-item {
    min-height: 1.5em;
    line-height: 1.5;
}

/* List and paragraph CLS prevention */
.back-link {
    min-height: 2.5em;
    margin-bottom: 20px;
}

p {
    min-height: 1.5em;
    line-height: 1.5;
}

ul, ol {
    min-height: 1em;
}

li {
    min-height: 1.5em;
    line-height: 1.5;
}

/* Prevent text shifting during font loading */
.street-info, .postal-code, body, h1, h2, h3, h4, h5, h6, p, li {
    font-display: swap;
}

/* Preload critical fonts */
@font-face {
    font-family: 'Open Sans';
    src: local('Open Sans'), local('OpenSans');
    font-display: swap;
}

@media screen and (max-width: 767px) {
    /* Adjust for smaller screens */
    .grid-25 {
        min-height: 90px;
    }
    
    /* Mobile-specific postal code and street info fixes */
    .postal-code {
        min-width: 50px; /* Slightly smaller on mobile */
        font-size: 0.95em;
        padding: 0.2rem 0.4rem;
    }
    
    .street-info {
        padding: 12px;
        margin: 10px 0;
        font-size: 1em;
        min-height: 1.5em;
    }
    
    /* Ensure paragraphs have consistent height */
    .paragraph-title {
        min-height: 2.5em;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Prevent text size adjustments causing layout shifts */
    html {
        -webkit-text-size-adjust: 100%;
    }
    
    /* Improve contrast on mobile */
    .colored-stripe.darkblue h1 {
        color: white;
        font-size: 18px;
        font-weight: 700;
    }
    
    .colored-stripe.darkblue .paragraph-title,
    .colored-stripe.darkblue a.paragraph-title,
    .colored-stripe.darkblue p a {
        color: white !important;
        font-weight: 600;
        font-size: 16px;
    }
}