/* =================================================================
   ENHANCED COMPLETE STYLESHEET WITH THEME SUPPORT
   ================================================================= */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

/* --- CSS Custom Properties for Dynamic Theming --- */
:root {
    /* Default Light Theme Colors */
    --sfa-primary-color: #ef4444;
    --sfa-secondary-color: #6b7280;
    --sfa-accent-color: #111827;
    --sfa-background-color: #ffffff;
    --sfa-surface-color: #f9fafb;
    --sfa-text-primary: #111827;
    --sfa-text-secondary: #6b7280;
    --sfa-border-color: #e5e7eb;
    --sfa-shadow: rgba(0, 0, 0, 0.1);
    --sfa-overlay-bg: rgba(0, 0, 0, 0.6);
    --sfa-chatbox-opacity: 0.95;
}

/* Dark Theme Variables */
[data-theme="dark"] {
    --sfa-background-color: #1f2937;
    --sfa-surface-color: #374151;
    --sfa-text-primary: #f9fafb;
    --sfa-text-secondary: #d1d5db;
    --sfa-border-color: #4b5563;
    --sfa-shadow: rgba(0, 0, 0, 0.3);
    --sfa-overlay-bg: rgba(0, 0, 0, 0.8);
}

/* Auto Theme (System Preference) */
@media (prefers-color-scheme: dark) {
    [data-theme="auto"] {
        --sfa-background-color: #1f2937;
        --sfa-surface-color: #374151;
        --sfa-text-primary: #f9fafb;
        --sfa-text-secondary: #d1d5db;
        --sfa-border-color: #4b5563;
        --sfa-shadow: rgba(0, 0, 0, 0.3);
        --sfa-overlay-bg: rgba(0, 0, 0, 0.8);
    }
}

/* --- Global & Core --- */
#floating-tray, #chat-overlay {
    font-family: 'Inter', sans-serif;
    box-sizing: border-box;
}
#floating-tray *, #chat-overlay * {
    box-sizing: border-box;
}
.hidden {
    display: none !important;
}

/* --- Floating Tray Placement Classes --- */
#floating-tray {
    position: fixed;
    z-index: 999;
    display: flex;
    gap: 12px;
    background-color: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(12px);
    padding: 12px;
    border-radius: 50px;
    box-shadow: 0 6px 24px var(--sfa-shadow);
    border: 1px solid var(--sfa-border-color);
    transition: all 0.3s ease;
}

/* Dark theme floating tray */
[data-theme="dark"] #floating-tray,
[data-theme="auto"] #floating-tray {
    background-color: rgba(31, 41, 55, 0.9);
}

@media (prefers-color-scheme: dark) {
    [data-theme="auto"] #floating-tray {
        background-color: rgba(31, 41, 55, 0.9);
    }
}

#floating-tray.position-bottom-right { bottom: 20px; right: 20px; }
#floating-tray.position-top-center { top: 20px; left: 50%; transform: translateX(-50%); }
#floating-tray.position-top-left { top: 20px; left: 20px; }

/* --- Enhanced Action Buttons --- */
.action-btn {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background-color: var(--sfa-surface-color);
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    border: none;
    transition: all 0.25s ease;
    position: relative;
    overflow: hidden;
}

.action-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--sfa-primary-color);
    opacity: 0;
    transition: opacity 0.25s ease;
    border-radius: 50%;
}

.action-btn:hover::before {
    opacity: 0.1;
}

.action-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px var(--sfa-shadow);
}

/* --- Dynamic Icon Colors --- */
.action-btn svg {
    width: 33px;
    height: 33px;
    stroke: var(--sfa-primary-color);
    position: relative;
    z-index: 1;
    transition: stroke 0.3s ease;
}

.action-btn img.custom-icon {
    width: 45px;
    height: 45px;
    position: relative;
    z-index: 1;
    border-radius: 50%;
}

/* --- Main Chat Pop-up with Dynamic Opacity --- */
#chat-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--sfa-overlay-bg);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 10px;
    transition: all 0.3s ease;
}

#chat-popup {
    background-color: var(--sfa-background-color);
    opacity: var(--sfa-chatbox-opacity);
    border-radius: 16px;
    width: 100%;
    max-width: 100%;
    box-shadow: 0 10px 40px var(--sfa-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 75vh;
    max-height: 700px;
    border: 1px solid var(--sfa-border-color);
    transition: all 0.3s ease;
}

/* --- Responsive Modal Width for Desktop --- */
@media (min-width: 768px) {
    #chat-overlay {
        padding: 20px;
    }
    #chat-popup {
        max-width: 800px;
        height: 70vh;
        max-height: 620px;
    }
}

#chat-header {
    display: flex;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--sfa-border-color);
    background: var(--sfa-background-color);
    flex-shrink: 0;
}

#agent-avatar { 
    width: 40px; 
    height: 40px; 
    border-radius: 50%; 
    margin-right: 12px; 
    object-fit: cover;
    border: 2px solid var(--sfa-border-color);
}

#header-title-area { flex-grow: 1; }

#chat-title { 
    font-weight: 600; 
    font-size: 1.1em; 
    color: var(--sfa-text-primary);
    transition: color 0.3s ease;
}

#close-chat-btn { 
    background: var(--sfa-surface-color);
    border-radius: 50%; 
    width: 36px; 
    height: 36px; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    border: none; 
    cursor: pointer; 
    color: var(--sfa-text-secondary);
    margin-left: auto;
    transition: all 0.25s ease;
    z-index: 1000;
}

#close-chat-btn:hover { 
    background-color: var(--sfa-primary-color);
    color: white;
    transform: scale(1.1);
}

#close-chat-btn svg {
    stroke: currentColor;
    width: 20px;
    height: 20px;
    transition: stroke 0.3s ease;
}

/* --- Chat Body & Views --- */
#chat-body { 
    flex: 1; 
    min-height: 0; 
    display: flex; 
    flex-direction: column;
    background: var(--sfa-surface-color);
}

#search-view, #chat-view { 
    flex: 1; 
    min-height: 0; 
    display: flex; 
    flex-direction: column; 
}

/* --- Search View & Slide Animation --- */
#search-view {
    position: relative;
    overflow: hidden;
    flex: 1;
}

#search-filters-panel, #search-results-panel {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    transition: transform 0.4s cubic-bezier(0.25, 1, 0.5, 1);
    background-color: var(--sfa-surface-color);
}

#search-results-panel {
    transform: translateX(100%);
    padding: 0;
}

#search-view.show-results #search-filters-panel {
    transform: translateX(-100%);
}
#search-view.show-results #search-results-panel {
    transform: translateX(0);
}

/* --- Enhanced Search Filters --- */
#search-filters-panel {
    background-color: transparent;
    padding: 24px;
    align-items: center;
    gap: 16px;
    justify-content: flex-start;
}

.filter-card {
    background-color: transparent;
    width: 100%;
    max-width: 400px;
}

.filter-label {
    display: block;
    font-weight: 500;
    font-size: 0.9em;
    margin-bottom: 8px;
    color: var(--sfa-text-secondary);
    transition: color 0.3s ease;
}

.filter-select {
    width: 100%;
    padding: 12px 16px;
    border-radius: 8px;
    border: 1px solid var(--sfa-border-color);
    background-color: var(--sfa-background-color);
    color: var(--sfa-text-primary);
    font-size: 1em;
    font-family: 'Inter', sans-serif;
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 0.7rem center;
    background-size: 1.5em 1.5em;
    transition: all 0.3s ease;
}

.filter-select:focus {
    border-color: var(--sfa-primary-color);
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
    outline: none;
}

select:disabled {
    color: var(--sfa-text-secondary);
    cursor: not-allowed;
    opacity: 0.6;
}

#perform-search-btn {
    background-color: var(--sfa-accent-color);
    color: white;
    border: none;
    padding: 12px 20px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1em;
    cursor: pointer;
    transition: all 0.25s ease;
    position: relative;
    overflow: hidden;
}

#perform-search-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
    transition: left 0.5s ease;
}

#perform-search-btn:hover::before {
    left: 100%;
}

#perform-search-btn:hover {
    background-color: var(--sfa-primary-color);
    transform: translateY(-2px);
    box-shadow: 0 8px 25px var(--sfa-shadow);
}

/* --- Enhanced Search Results --- */
#search-results-header {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 12px 20px;
    border-bottom: 1px solid var(--sfa-border-color);
    background: var(--sfa-background-color);
    flex-shrink: 0;
}

.header-text-wrapper {
    display: flex;
    flex-direction: column;
}

#search-results-title {
    margin: 0;
    font-size: 1.1em;
    font-weight: 600;
    color: var(--sfa-text-primary);
}

#search-criteria-description {
    margin: 2px 0 0 0;
    font-size: 0.85em;
    color: var(--sfa-text-secondary);
}

/* --- Keyframe animation for the glowing effect --- */
@keyframes red-glow {
  0%, 100% {
    filter: drop-shadow(0 0 5px rgba(127, 29, 29, 0.8));
    stroke: #991b1b;
  }
  50% {
    filter: drop-shadow(0 0 16px rgba(127, 29, 29, 10));
    stroke: #b91c1c; /* A lighter red for the peak of the glow */
  }
}

/* --- Style for the glowing back button icon --- */
#back-to-filters-btn {
    background: transparent;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

#back-to-filters-btn svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke-width: 2.5;
    /* Apply the animation */
    animation: red-glow 2.5s ease-in-out infinite;
}

/* --- Add a hover effect to the glowing icon --- */
#back-to-filters-btn:hover {
    /* Optional: Add a subtle background to the button on hover */
    background-color: rgba(239, 68, 68, 0.7);
    border-radius: 50%; /* Ensure the background is circular */
}

#back-to-filters-btn:hover svg {
    /* Pause the animation on hover */
    animation-play-state: paused;
    /* Set a strong, static glow on hover */
    filter: drop-shadow(0 0 16px rgba(255, 20, 20, 1));
    stroke: #ffaaaa;
}





#search-results-list {
    flex-grow: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--sfa-surface-color);
}

.listing-card-search {
    background: var(--sfa-background-color);
    border: 1px solid var(--sfa-border-color);
    border-radius: 12px;
    text-decoration: none;
    color: inherit;
    display: flex;
    flex-direction: row;
    overflow: hidden;
    transition: all 0.2s ease;
    width: 100%;
    height: 140px;
    flex-shrink: 0;
    outline: 2px solid transparent;
    outline-offset: -2px;
}

.listing-card-search:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 20px var(--sfa-shadow);
    background-color: var(--sfa-surface-color);
    border-color: var(--sfa-primary-color);
}

/* --- Style for the selected card --- */
.listing-card-search.selected {
    border-color: var(--sfa-primary-color) !important;
    box-shadow: 0 0 0 2px var(--sfa-primary-color), 0 8px 25px var(--sfa-shadow);
    transform: translateY(-2px);
}

.listing-card-search-image {
    width: 140px;
    height: 140px;
    flex-shrink: 0;
}

.listing-card-search-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.listing-card-search-info {
    padding: 16px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex-grow: 1;
    min-width: 0;
}

.listing-card-search-main-info {
    min-width: 0;
}

.listing-card-search-title {
    font-weight: 600;
    font-size: 1em;
    color: var(--sfa-text-primary);
    margin: 0 0 4px 0;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: color 0.3s ease;
}

.listing-card-search-id {
    font-size: 0.85em;
    font-weight: 500;
    color: var(--sfa-text-secondary);
    transition: color 0.3s ease;
}

.listing-card-search-meta {
    display: flex;
    flex-wrap: nowrap;
    gap: 20px;
    padding-top: 12px;
    font-size: 0.9em;
    border-top: 1px solid var(--sfa-border-color);
}

.meta-item {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--sfa-text-primary);
    transition: color 0.3s ease;
}

.meta-item > span {
    white-space: nowrap;
}

.meta-item svg {
    color: var(--sfa-primary-color);
    stroke: var(--sfa-primary-color);
    fill: none;
    width: 18px;
    height: 18px;
    transition: all 0.3s ease;
}

#search-results-list .placeholder {
    text-align: center;
    padding: 40px;
    color: var(--sfa-text-secondary);
}

/* --- Chat View --- */
#chat-view { 
    flex: 1; 
    min-height: 0; 
    display: flex; 
    flex-direction: column; 
}

#chat-messages { 
    flex: 1; 
    overflow-y: auto; 
    padding: 20px;
    background: var(--sfa-surface-color);
}

#chat-input-container { 
    padding: 8px 16px 16px 16px; 
    border-top: 1px solid var(--sfa-border-color);
    background: var(--sfa-background-color);
}

#chat-input { 
    width: 100%; 
    padding: 12px 16px; 
    border-radius: 25px; 
    border: 1px solid var(--sfa-border-color);
    background: var(--sfa-background-color);
    color: var(--sfa-text-primary);
    font-family: 'Inter', sans-serif;
    transition: all 0.3s ease;
}

#chat-input:focus {
    border-color: var(--sfa-primary-color);
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
    outline: none;
}

/* --- MOBILE-SPECIFIC STYLES --- */
@media (max-width: 767px) {
    .listing-card-search {
        height: 120px;
    }
    .listing-card-search-image {
        width: 120px;
        height: 120px;
    }
    .listing-card-search-info {
        padding: 12px;
    }
    .listing-card-search-title {
        font-size: 0.9em;
    }
    .listing-card-search-id {
        font-size: 0.8em;
    }
    .listing-card-search-meta {
        gap: 12px;
        font-size: 0.85em;
    }
    .meta-item svg {
        width: 16px;
        height: 16px;
    }
}

/* --- Keyframe animation for the close button pulse --- */
@keyframes subtle-pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(107, 114, 128, 0.4);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(107, 114, 128, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(107, 114, 128, 0);
  }
}

/* --- *** MODAL CLOSE BUTTON STYLES (ULTRA-ROBUST FIX) *** --- */
#close-chat-btn {
    /* Positioning and Sizing */
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 36px !important;
    height: 36px !important;
    margin-left: auto !important;
    padding: 0 !important;

    /* Appearance */
    background-color: #f0f0f0 !important; /* A light grey, visible on most backgrounds */
    border: 1px solid #ccc !important; /* A visible border */
    border-radius: 50% !important;
    cursor: pointer !important;
    
    /* Stacking */
    z-index: 9999 !important; /* Extremely high z-index */
    
    /* Transitions */
    transition: background-color 0.2s ease, transform 0.2s ease !important;
}

#close-chat-btn svg {
    /* Ensure the SVG itself is visible */
    display: block !important;
    width: 20px !important;
    height: 20px !important;
    
    /* Explicitly set the icon's appearance */
    stroke: #555 !important; /* A dark grey, visible on the light grey button */
    stroke-width: 2.5px !important;
    fill: none !important;
}

#close-chat-btn:hover {
    background-color: var(--sfa-primary-color) !important;
    transform: rotate(90deg);
}

#close-chat-btn:hover svg {
    stroke: #ffffff !important; /* White stroke on red background */
}

/* --- Additional Property Styling --- */
.property-price,
.property-status,
.property-type {
    font-size: 0.9em;
    font-weight: 600;
    color: var(--sfa-primary-color);
    white-space: nowrap;
    transition: color 0.3s ease;
}

/* --- Loading States --- */
.sfa-loading {
    opacity: 0.6;
    pointer-events: none;
    position: relative;
}

.sfa-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid var(--sfa-primary-color);
    border-radius: 50%;
    border-top-color: transparent;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* --- Smooth Transitions for Theme Changes --- */
* {
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

/* --- Accessibility Improvements --- */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* --- Focus Indicators --- */
.action-btn:focus,
#close-chat-btn:focus,
#back-to-filters-btn:focus,
.filter-select:focus,
#chat-input:focus,
#perform-search-btn:focus {
    outline: 2px solid var(--sfa-primary-color);
    outline-offset: 2px;
}

/* --- High Contrast Mode Support --- */
@media (prefers-contrast: high) {
    :root {
        --sfa-border-color: #000000;
        --sfa-shadow: rgba(0, 0, 0, 0.5);
    }
    
    [data-theme="dark"] {
        --sfa-border-color: #ffffff;
        --sfa-shadow: rgba(255, 255, 255, 0.3);
    }
}



/* --- Additional Icon and Z-Index Fixes --- */
#back-to-filters-btn {
    z-index: 1000;
    position: relative;
}

#close-chat-btn {
    z-index: 1000;
    position: relative;
}

/* Ensure all SVG icons are properly visible */
.action-btn svg {
    stroke: var(--sfa-primary-color) !important;
    fill: none !important;
    width: 33px !important;
    height: 33px !important;
    display: block !important;
    pointer-events: none !important;
}

#close-chat-btn svg {
    stroke: currentColor !important;
    fill: none !important;
    width: 20px !important;
    height: 20px !important;
    display: block !important;
    pointer-events: none !important;
}

#back-to-filters-btn svg {
    stroke: var(--sfa-text-primary) !important;
    fill: none !important;
    width: 20px !important;
    height: 20px !important;
    display: block !important;
    pointer-events: none !important;
}

#back-to-filters-btn:hover svg {
    stroke: white !important;
}

/* Meta item icons */
.meta-item svg {
    stroke: var(--sfa-primary-color) !important;
    fill: none !important;
    width: 18px !important;
    height: 18px !important;
    display: block !important;
    pointer-events: none !important;
    flex-shrink: 0;
}

/* Force visibility for all icons */
svg {
    opacity: 1 !important;
    visibility: visible !important;
}

/* Icon container fixes */
.action-btn,
#close-chat-btn,
#back-to-filters-btn {
    overflow: visible !important;
}

/* Ensure proper stacking order */
#floating-tray {
    z-index: 999;
}

#chat-overlay {
    z-index: 1000;
}

#chat-popup {
    z-index: 1001;
}

#chat-header {
    z-index: 1002;
}

#close-chat-btn {
    z-index: 1003;
}

#search-results-header {
    z-index: 1002;
}

#back-to-filters-btn {
    z-index: 1003;
}

/* Additional fallback for missing icons */
.action-btn:empty::before {
    content: "⚙";
    font-size: 24px;
    color: var(--sfa-primary-color);
}

#close-chat-btn:empty::before {
    content: "✕";
    font-size: 16px;
    color: currentColor;
}

#back-to-filters-btn:empty::before {
    content: "←";
    font-size: 16px;
    color: currentColor;
}






/* --- Add the following styles to your existing style.css file --- */

/* --- Chat View Enhancements --- */
#chat-view {
    position: relative; /* Needed for absolute positioning of children */
}

/* --- AI Start Button (Phone Icon) --- */
#start-ai-btn {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background-color: var(--sfa-primary-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    cursor: pointer;
    z-index: 10;
    transition: transform 0.3s ease, opacity 0.3s ease;
    box-shadow: 0 4px 20px rgba(0,0,0,0.2);
}

#start-ai-btn:hover {
    transform: translate(-50%, -50%) scale(1.1);
}

#start-ai-btn.hidden {
    transform: translate(-50%, -50%) scale(0);
    opacity: 0;
    pointer-events: none;
}

#start-ai-btn svg {
    width: 40px;
    height: 40px;
}

/* --- Voice Visualizer --- */
#voice-visualizer {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: none; /* Hidden by default */
    gap: 8px;
    align-items: center;
    justify-content: center;
    height: 60px;
    z-index: 9;
}

#voice-visualizer.active {
    display: flex;
}

#voice-visualizer span {
    width: 8px;
    height: 10px;
    background-color: var(--sfa-primary-color);
    border-radius: 4px;
    animation: voice-wave 1.2s infinite ease-in-out;
}

#voice-visualizer span:nth-child(2) { animation-delay: 0.2s; }
#voice-visualizer span:nth-child(3) { animation-delay: 0.4s; }
#voice-visualizer span:nth-child(4) { animation-delay: 0.6s; }

@keyframes voice-wave {
    0%, 100% { transform: scaleY(1); }
    50% { transform: scaleY(5); }
}


/* --- Chat View & Messages --- */
#chat-messages {
    z-index: 5;
    /* Add some padding at the bottom to avoid overlapping with the input */
    padding-bottom: 20px;
}

.chat-message {
    max-width: 80%;
    padding: 10px 15px;
    border-radius: 18px;
    margin-bottom: 12px;
    line-height: 1.5;
}

.chat-message.user {
    background-color: var(--sfa-primary-color);
    color: white;
    border-bottom-right-radius: 4px;
    margin-left: auto;
}

.chat-message.ai {
    background-color: var(--sfa-surface-color);
    color: var(--sfa-text-primary);
    border: 1px solid var(--sfa-border-color);
    border-bottom-left-radius: 4px;
    margin-right: auto;
}

#chat-input:disabled {
    background-color: var(--sfa-surface-color);
    cursor: not-allowed;
}