/* Global Reset for iframe compatibility */
html, body {
    margin: 0;
    padding: 0;
    overflow: hidden;
    width: 100%;
    height: 100%;
}

/* ============================================
   IFRAME ISOLATION - Indipendenza Totale
   ============================================ */

/* Reset completo del widget per isolamento dal parent */
.avm-widget {
    all: initial;
    
    /* Isolation e Containment per massima indipendenza */
    isolation: isolate;
    contain: layout style paint;
    
    /* Re-applica stili base necessari dopo reset */
    display: flex;
    flex-direction: column;
    font-family: "Inter", -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-optical-sizing: auto;
    font-size: 15px;
    line-height: 1.6;
    color: #333;
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    width: 500px;
    height: 600px;
    margin: 0 auto;
    position: relative;
    box-sizing: border-box;
}

/* Reset aggressivo per tutti gli elementi interni */
.avm-widget *,
.avm-widget *::before,
.avm-widget *::after {
    all: unset;
    box-sizing: border-box;
}

/* Ripristina proprietà di layout essenziali */
.avm-widget * {
    display: revert;
    position: revert;
}

/* ============================================
   RIPRISTINO CHECKBOX - Importante!
   ============================================ */

/* Ripristina aspetto e funzionalità checkbox native */
.avm-widget input[type="checkbox"] {
    all: revert;
    appearance: auto;
    -webkit-appearance: checkbox;
    -moz-appearance: checkbox;
    display: inline-block !important;
    width: 18px;
    height: 18px;
    margin: 0;
    cursor: pointer;
    flex-shrink: 0;
}

/* Assicura che la checkbox sia visibile e cliccabile */
.avm-widget .checkbox-option {
    display: flex !important;
    align-items: flex-start;
    gap: 8px;
    margin-top: 12px;
}

.avm-widget .checkbox-option input[type="checkbox"] {
    margin-top: 4px;
}

.avm-widget .checkbox-label {
    display: inline !important;
    font-size: 13px;
    color: #4a5568;
    line-height: 1.4;
    cursor: pointer;
    flex: 1;
}

.avm-widget .privacy-link {
    color: #4fd1c5 !important;
    text-decoration: none;
    display: inline !important;
}

.avm-widget .privacy-link:hover {
    text-decoration: underline;
}

/* CSS Reset e Isolation per Widget */
.avm-widget * {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

/* ============================================
   DESKTOP LAYOUT (Default - 500x600px)
   ============================================ */

/* Error and Loading Containers */
#avm-widget-error,
#avm-widget-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    text-align: center;
    height: 100%;
}

#avm-widget-error h2,
#avm-widget-loading h2 {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 12px;
    color: #333;
}

#avm-widget-error p,
#avm-widget-loading p {
    font-size: 16px;
    color: #666;
    line-height: 1.5;
}

/* Widget Container - Grid Layout */
.widget-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    position: relative;
    overflow: hidden;
}

/* Header */
.widget-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px;
    text-align: center;
    color: #ffffff;
    flex-shrink: 0;
}

.widget-header h1 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 5px;
}

.widget-header p {
    font-size: 14px;
    opacity: 0.95;
}

/* Progress Bar */
.progress-container {
    padding: 12px 20px;
    background: #f8f9fa;
    border-bottom: 1px solid #e9ecef;
    flex-shrink: 0;
}

.progress-bar {
    height: 6px;
    background: #e9ecef;
    border-radius: 10px;
    overflow: hidden;
    position: relative;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #4fd1c5, #38b2ac);
    border-radius: 10px;
    transition: width 0.4s ease;
    width: 0%;
}

/* Form Container */
.form-container {
    padding: 15px 20px 70px;
    background: #ffffff;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

/* Form */
.valuation-form {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

/* Form Steps */
.form-step {
    display: none;
    flex-direction: column;
    height: 100%;
}

.form-step.active {
    display: flex;
    animation: fadeIn 0.3s ease;
}

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

/* Step Header */
.step-header {
    margin-bottom: 15px;
}

.step-header h2,
.step-header h3 {
    font-size: 20px;
    font-weight: 600;
    color: #2d3748;
    margin-bottom: 5px;
}

.step-header p {
    font-size: 14px;
    color: #718096;
}

/* Step 5 - Compact layout for user data */
#step5 {
    padding-bottom: 0 !important;
}

#step5 .step-header {
    margin-bottom: 6px;
}

#step5 .step-header h2 {
    font-size: 17px;
    margin-bottom: 2px;
}

#step5 .form-row {
    margin-bottom: 6px;
}

#step5 .form-group {
    margin-bottom: 0;
}

#step5 .form-label {
    font-size: 12px;
    margin-bottom: 3px;
}

#step5 .form-control {
    padding: 7px 10px;
    font-size: 13px;
}

#step5 .checkbox-option {
    margin-top: 6px;
    margin-bottom: 0;
    gap: 5px;
}

#step5 .checkbox-option input[type="checkbox"] {
    width: 13px;
    height: 13px;
    margin-top: 1px;
    flex-shrink: 0;
}

#step5 .checkbox-option label,
#step5 .checkbox-label {
    font-size: 10px;
    line-height: 1.25;
}

#step5 .form-actions {
    margin-top: 12px;
    padding-top: 0;
    padding-bottom: 50px;
}

#step5 .btn {
    padding: 9px 18px;
    font-size: 13px;
}

/* Step 8 - Compact layout for interest + timeframe */
#step8 {
    padding-bottom: 0 !important;
}

#step8 .step-header {
    margin-bottom: 10px;
}

#step8 .form-group {
    margin-bottom: 12px;
}

#step8 .form-label {
    font-size: 13px;
    margin-bottom: 6px;
}

#step8 .form-control {
    padding: 10px 12px;
    font-size: 14px;
}

#step8 .button-group {
    margin: 10px 0;
}

#step8 .form-actions {
    margin-top: 15px;
    padding-bottom: 50px;
}

/* Step 10 - Compact layout for final valuation */
#step10 {
    padding-bottom: 0 !important;
}

#step10 .result-header {
    margin-bottom: 8px;
    text-align: center;
}

#step10 .result-header h2 {
    font-size: 18px;
    margin-bottom: 3px;
    font-weight: 600;
}

#step10 .result-header p {
    font-size: 13px;
    color: #718096;
    margin-bottom: 0;
}

#step10 .valuation-result {
    padding: 12px;
    margin: 8px 0;
    background: linear-gradient(135deg, #f0fffe 0%, #e6fffd 100%);
    border-radius: 10px;
}

#step10 .valuation-range {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
}

#step10 .valuation-label {
    font-size: 12px;
    color: #718096;
    font-weight: 500;
}

#step10 .valuation-price {
    font-size: 24px !important;
    font-weight: 700;
    color: #38b2ac;
    white-space: nowrap;
    line-height: 1.2;
}

#step10 .valuation-details {
    margin-top: 4px;
}

#step10 .valuation-details p {
    font-size: 11px;
    color: #718096;
    margin: 0;
}

#step10 .info-box {
    padding: 12px !important;
    background: #f8f9fa;
    border-radius: 8px;
    margin-top: 8px;
}

#step10 .info-text {
    font-size: 11px !important;
    line-height: 1.35;
    color: #4a5568;
    margin-bottom: 8px !important;
}

#step10 .next-steps h3 {
    font-size: 12px !important;
    font-weight: 600 !important;
    margin-bottom: 6px !important;
    margin-top: 0 !important;
}

#step10 .next-steps ul {
    margin: 0 !important;
    padding-left: 0 !important;
    list-style: none;
}

#step10 .next-steps li {
    font-size: 10px !important;
    line-height: 1.3 !important;
    color: #4a5568;
    margin-bottom: 5px !important;
    padding-left: 18px !important;
    position: relative;
}

#step10 .next-steps li:before {
    content: '•';
    position: absolute;
    left: 6px;
    color: #38b2ac;
    font-weight: bold;
}

/* Step Content Wrapper */
.form-step > div:not(.step-header):not(.form-actions) {
    flex: 1;
    min-height: 0;
    overflow: visible;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
}

/* Button Group Styles */
.button-group {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
    margin: 15px 0;
}

.button-group.vertical {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
}

.option-button {
    flex: 1;
    min-width: 120px;
    padding: 14px 16px;
    background: #ffffff;
    border: 2px solid #e1e5e9;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    font-family: inherit;
    font-size: 15px;
    font-weight: 500;
    color: #2d3748;
}

.button-group.vertical .option-button {
    flex-direction: row;
    justify-content: center;
    padding: 16px 24px;
}

.option-button:hover {
    border-color: #4fd1c5;
    background: #f0fffe;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(79, 209, 197, 0.2);
}

.option-button.selected {
    border-color: #4fd1c5;
    background: linear-gradient(135deg, #f0fffe 0%, #e6fffd 100%);
    box-shadow: 0 4px 12px rgba(79, 209, 197, 0.3);
}

.option-icon {
    font-size: 28px;
}

.button-group.vertical .option-icon {
    font-size: 24px;
    margin-right: 10px;
}

.option-label {
    font-size: 15px;
    font-weight: 500;
    color: #2d3748;
}

/* Form Groups and Fields */
.form-group,
.single-field {
    margin-bottom: 12px;
    position: relative;
}

.form-row {
    display: flex;
    gap: 12px;
    margin-bottom: 12px;
}

.form-row .form-group {
    flex: 1;
    margin-bottom: 0;
}

.form-label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: #2d3748;
    margin-bottom: 6px;
}

.form-control {
    width: 100%;
    padding: 10px 12px;
    font-size: 15px;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    transition: all 0.3s ease;
    font-family: inherit;
    background: #ffffff;
    color: #2d3748;
}

.form-control:focus {
    outline: none;
    border-color: #4fd1c5;
    box-shadow: 0 0 0 3px rgba(79, 209, 197, 0.1);
}

.form-control::placeholder {
    color: #a0aec0;
}

select.form-control {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%232d3748' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

/* Autocomplete */
.autocomplete-results {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: white;
    border: 2px solid #e2e8f0;
    border-top: none;
    border-radius: 0 0 8px 8px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 1000;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    margin-top: 0;
}

.autocomplete-item {
    padding: 10px 12px;
    cursor: pointer;
    font-size: 14px;
    border-bottom: 1px solid #f7fafc;
}

.autocomplete-item:hover {
    background: #f7fafc;
}

.autocomplete-item:last-child {
    border-bottom: none;
}

/* Checkbox */
.checkbox-option {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    margin-top: 12px;
}

.checkbox-option input[type="checkbox"] {
    margin-top: 4px;
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.checkbox-option label {
    font-size: 13px;
    color: #4a5568;
    line-height: 1.4;
    cursor: pointer;
    flex: 1;
}

.checkbox-option a {
    color: #4fd1c5;
    text-decoration: none;
}

.checkbox-option a:hover {
    text-decoration: underline;
}

/* Form Actions (Buttons) */
.form-actions {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: auto;
    padding-top: 15px;
}

.btn {
    padding: 12px 24px;
    font-size: 15px;
    font-weight: 600;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: none;
    font-family: inherit;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.btn-primary {
    background: linear-gradient(135deg, #4fd1c5 0%, #38b2ac 100%);
    color: #ffffff;
    box-shadow: 0 4px 12px rgba(79, 209, 197, 0.3);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(79, 209, 197, 0.4);
}

.btn-secondary {
    background: #e2e8f0;
    color: #2d3748;
}

.btn-secondary:hover {
    background: #cbd5e0;
    transform: translateY(-2px);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none !important;
}

/* Valuation Result */
.valuation-result {
    text-align: center;
    padding: 20px;
    background: linear-gradient(135deg, #f0fffe 0%, #e6fffd 100%);
    border-radius: 12px;
    margin: 15px 0;
}

.valuation-result h4 {
    font-size: 16px;
    font-weight: 600;
    color: #2d3748;
    margin-bottom: 10px;
}

.valuation-price {
    font-size: 32px;
    font-weight: 700;
    color: #38b2ac;
    margin: 10px 0;
}

.valuation-range {
    font-size: 14px;
    color: #718096;
    margin-top: 8px;
}

/* Footer */
.widget-footer {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: #f8f9fa;
    padding: 10px 15px;
    text-align: center;
    border-top: 1px solid #e9ecef;
    width: 100%;
    box-sizing: border-box;
    z-index: 100;
}

.widget-footer p {
    font-size: 12px;
    color: #718096;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.footer-logo {
    height: 14px;
    width: auto;
    vertical-align: middle;
}

.widget-footer a {
    color: #4fd1c5;
    text-decoration: none;
    font-weight: 500;
}

.widget-footer a:hover {
    text-decoration: underline;
}

/* Validation States - Enhanced */
.form-control.error,
select.error,
input.error {
    border: 2px solid #e74c3c !important;
    background-color: #fff5f5 !important;
    animation: shake 0.3s ease-in-out;
}

.form-control.error:focus,
select.error:focus,
input.error:focus {
    border-color: #e74c3c !important;
    box-shadow: 0 0 0 4px rgba(231, 76, 60, 0.15) !important;
    outline: none !important;
}

.form-control.valid {
    border-color: #68d391;
}

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

/* Error message field-specific */
.field-error-message {
    color: #e74c3c;
    font-size: 12px;
    margin-top: 5px;
    display: block;
    font-weight: 500;
    animation: fadeInError 0.3s ease-in;
}

@keyframes fadeInError {
    from { 
        opacity: 0; 
        transform: translateY(-5px); 
    }
    to { 
        opacity: 1; 
        transform: translateY(0); 
    }
}

/* Hidden Elements */
.hidden {
    display: none !important;
}

/* Error Messages */
.error-messages {
    position: relative;
    background: #fff5f5;
    border: 2px solid #fc8181;
    border-radius: 8px;
    padding: 12px 16px;
    margin: 0 0 15px 0;
    display: none;
    z-index: 100;
    box-shadow: 0 2px 8px rgba(252, 129, 129, 0.15);
}

.error-messages.active {
    display: block;
}

.error-messages p,
.error-messages .error-message {
    color: #c53030;
    font-size: 13px;
    line-height: 1.4;
    margin: 0;
    font-weight: 500;
    text-align: center;
}

/* Loading Overlay */
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.95);
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.loading-overlay.active {
    display: flex;
}

.loading-overlay p {
    margin-top: 15px;
    font-size: 14px;
    color: #4a5568;
    font-weight: 500;
}

/* Loading Spinner */
.loading-spinner {
    border: 4px solid #f3f4f6;
    border-top: 4px solid #4fd1c5;
    border-radius: 50%;
    width: 48px;
    height: 48px;
    animation: spin 0.8s linear infinite;
}

.spinner {
    border: 3px solid #f3f4f6;
    border-top: 3px solid #4fd1c5;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
    margin: 20px auto;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* ============================================
   MOBILE LAYOUT (max-width: 768px)
   ============================================ */

@media (max-width: 768px) {
    html, body {
        height: 100% !important;
        min-height: 100%;
        max-height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background: #f8f9fa;  /* Background matching per iframe */
    }
    
    .avm-widget {
        width: 100%;
        height: 100%;
        min-height: 100%;
        max-height: 100%;
        min-width: 100%;
        max-width: 100%;
        border-radius: 0;
        box-shadow: none;
        margin: 0;
        overflow: hidden;
    }

    /* Widget Container - Mobile */
    .widget-container {
        height: 100%;
        width: 100%;
    }

    /* Header - Compatto */
    .widget-header {
        padding: 6px 15px;
    }

    .widget-header h1 {
        font-size: 14px;
        margin-bottom: 1px;
    }

    .widget-header p {
        font-size: 9px;
    }

    /* Progress Bar - Compatto */
    .progress-container {
        padding: 4px 15px;
    }

    .progress-bar {
        height: 2px;
    }

    /* Form Container - Ottimizzato */
    .form-container {
        padding: 6px 15px 8px;
        overflow: hidden;
    }

    /* Step Header - Compatto */
    .step-header {
        margin-bottom: 10px;
    }

    .step-header h3 {
        font-size: 16px;
        margin-bottom: 3px;
    }

    .step-header p {
        font-size: 12px;
    }

    /* Button Group - Compatto */
    .button-group {
        margin: 10px 0;
        gap: 8px;
    }

    .button-group.vertical {
        gap: 8px;
    }

    .option-button {
        padding: 10px 12px;
        font-size: 13px;
        min-width: 100px;
    }

    .button-group.vertical .option-button {
        padding: 12px 18px;
    }

    .option-icon {
        font-size: 22px;
    }

    .button-group.vertical .option-icon {
        font-size: 20px;
        margin-right: 8px;
    }

    .option-label {
        font-size: 13px;
    }

    /* Form Groups - Compatto */
    .form-group,
    .single-field {
        margin-bottom: 8px;
    }

    .form-row {
        gap: 8px;
        margin-bottom: 8px;
    }

    .form-label {
        font-size: 12px;
        margin-bottom: 4px;
    }

    .form-control {
        padding: 8px 10px;
        font-size: 14px;
        border-width: 1px;
    }

    select.form-control {
        background-position: right 10px center;
        padding-right: 32px;
    }

    /* Autocomplete - Compatto */
    .autocomplete-results {
        max-height: 150px;
        border-width: 1px;
    }

    .autocomplete-item {
        padding: 8px 10px;
        font-size: 13px;
    }

    /* Checkbox - Compatto */
    .checkbox-option {
        gap: 6px;
        margin-top: 8px;
    }

    .checkbox-option input[type="checkbox"] {
        width: 16px;
        height: 16px;
        margin-top: 2px;
    }

    .checkbox-option label {
        font-size: 11px;
    }

    /* Form Actions - Compatto */
    .form-actions {
        gap: 8px;
        padding-top: 10px;
        margin-top: auto;
    }

    .btn {
        padding: 10px 18px;
        font-size: 13px;
        gap: 4px;
    }

    /* Valuation Result - Compatto */
    .valuation-result {
        padding: 12px;
        margin: 10px 0;
    }

    .valuation-result h4 {
        font-size: 13px;
        margin-bottom: 6px;
    }

    .valuation-price {
        font-size: 24px;
        margin: 6px 0;
    }

    .valuation-range {
        font-size: 11px;
        margin-top: 4px;
    }

    /* Footer - Compatto */
    .widget-footer {
        padding: 5px 12px;
    }

    .widget-footer p {
        font-size: 8px;
    }

    .footer-logo {
        height: 10px;
    }

    /* Error/Loading - Compatto */
    #avm-widget-error,
    #avm-widget-loading {
        padding: 20px;
    }

    #avm-widget-error h2,
    #avm-widget-loading h2 {
        font-size: 18px;
        margin-bottom: 8px;
    }

    #avm-widget-error p,
    #avm-widget-loading p {
        font-size: 13px;
    }

    /* Spinner - Compatto */
    .spinner {
        width: 32px;
        height: 32px;
        border-width: 2px;
        margin: 15px auto;
    }

    /* Forza font-size 14px per evitare zoom su iOS */
    .form-control,
    input.form-control,
    select.form-control {
        font-size: 14px !important;
    }
}

/* ============================================
   TABLET LAYOUT (769px - 1024px)
   ============================================ */

@media (min-width: 769px) and (max-width: 1024px) {
    .avm-widget {
        width: 90vw;
        max-width: 600px;
    }

    .widget-header h1 {
        font-size: 20px;
    }

    .widget-header p {
        font-size: 13px;
    }

    .form-container {
        padding: 12px 18px;
    }
}

/* ============================================
   CONDITIONAL FIELDS
   ============================================ */

#elevatorGroup,
#totalFloorsGroup {
    display: none;
}

.avm-widget input[name="propertyType"][value="appartamento"]:checked ~ #elevatorGroup {
    display: block;
}

.avm-widget input[name="propertyType"][value="villa"]:checked ~ #totalFloorsGroup,
.avm-widget input[name="propertyType"][value="villa a schiera"]:checked ~ #totalFloorsGroup {
    display: block;
}

/* ============================================
   UTILITY CLASSES
   ============================================ */

.text-center {
    text-align: center;
}

.mt-auto {
    margin-top: auto;
}

.mb-0 {
    margin-bottom: 0;
}

/* Ensure no scrollbars anywhere */
body, html {
    overflow: hidden;
}

.avm-widget,
.widget-container,
.form-container,
.valuation-form,
.form-step,
.form-step.active {
    overflow: hidden !important;
}
