/* === CSS Variables for Better Maintainability === */
:root {
    --primary-gradient: linear-gradient(135deg, #10b981 0%, #059669 100%);
    --secondary-gradient: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
    --success-gradient: linear-gradient(135deg, #34d399 0%, #10b981 100%);
    --shadow-light: 0 4px 15px rgba(0, 0, 0, 0.1);
    --shadow-medium: 0 8px 25px rgba(0, 0, 0, 0.15);
    --shadow-heavy: 0 15px 35px rgba(0, 0, 0, 0.2);
    --border-radius: 16px;
    --border-radius-small: 12px;
    --transition-smooth: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* === Thinking Dots Animation (Enhanced) === */
#thinking-dots {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 0;
}

#thinking-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--primary-gradient);
    animation: bounce-dot 0.8s infinite ease-in-out;
    display: inline-block;
}

#thinking-dots span:nth-child(1) {
    animation-delay: 0s;
}

#thinking-dots span:nth-child(2) {
    animation-delay: 0.15s;
}

#thinking-dots span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes bounce-dot {
    0%, 60%, 100% {
        opacity: 0.3;
        transform: scale(0.6) translateY(0);
    }
    30% {
        opacity: 1;
        transform: scale(1.1) translateY(-8px);
    }
}
@keyframes pulse-dot {
    0%, 80%, 100% {
        opacity: 0.4;
        transform: scale(0.8);
    }
    40% {
        opacity: 1;
        transform: scale(1.3);
    }
}

/* === Enhanced Chat Float Button === */
#chat-float-btn {
    position: fixed;
    bottom: 110px !important; /* Position above WhatsApp button */
    right: 30px;
    width: 160px;
    height: 56px;
    background: var(--primary-gradient);
    color: white;
    border: none;
    border-radius: 28px;
    box-shadow: var(--shadow-medium);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition-smooth);
    z-index: 1000;
    font-family: var(--font-family);
    font-size: 15px;
    font-weight: 600;
    letter-spacing: 0.025em;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

#chat-float-btn::before {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    background: var(--secondary-gradient);
    border-radius: 30px;
    z-index: -1;
    opacity: 0;
    transition: var(--transition-smooth);
}

#chat-float-btn:hover {
    transform: translateY(-6px) scale(1.05);
    box-shadow: var(--shadow-heavy);
}

#chat-float-btn:hover::before {
    opacity: 1;
}

#chat-float-btn:active {
    transform: translateY(-2px) scale(1.02);
}

.chat-btn-icon {
    margin-right: 8px;
    transition: var(--transition-smooth);
    display: flex;
    align-items: center;
    justify-content: center;
}

#chat-float-btn:hover .chat-btn-icon {
    transform: rotate(12deg) scale(1.1);
}

.chat-btn-icon svg {
    width: 20px;
    height: 20px;
    fill: currentColor;
}

/* === Chat Button Text === */
.chat-btn-text {
    font-weight: 600;
    letter-spacing: 0.025em;
    transition: var(--transition-smooth);
}

/* === Modern Chat Popup - Fixed positioning === */
#chat-float-popup {
    display: none;
    position: fixed;
    bottom: 50px;
    right: 30px;
    width: 380px;
    height: 500px;
    max-height: 75vh;
    background: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-heavy);
    z-index: 10000;
    overflow: hidden;
    border: 1px solid rgba(16, 185, 129, 0.2);
    animation: slideUp 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    margin-bottom: 20px;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

#chat-popup-inner {
    display: flex;
    flex-direction: column;
    height: 100%;
    position: relative;
    overflow: hidden;
}

/* === Enhanced Chat Header - Fixed alignment and cross button visibility === */
.chat-header {
    background: var(--primary-gradient);
    color: white;
    padding: 18px 60px 18px 20px; /* Added right padding for close button space */
    display: flex;
    align-items: center;
    font-size: 16px;
    font-family: var(--font-family);
    font-weight: 600;
    position: relative;
    box-shadow: 0 2px 10px rgba(16, 185, 129, 0.2);
    flex-shrink: 0;
    z-index: 10003;
    min-height: 70px; /* Increased height for better proportion */
    max-height: 70px;
    border-radius: var(--border-radius) var(--border-radius) 0 0;
    margin: 0;
    box-sizing: border-box;
}

.bot-avatar {
    margin-right: 12px;
    font-size: 22px; /* Slightly larger icon */
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(10px);
    flex-shrink: 0;
}

/* === Header text container === */
.chat-header-content {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 0; /* Allow text to shrink if needed */
}

.chat-header-title {
    font-size: 16px;
    font-weight: 600;
    line-height: 1.2;
    margin: 0;
    padding: 0;
    color: white;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* === Enhanced Close Button - Much more visible === */
#close-float-chat {
    position: absolute;
    top: 50%;
    right: 20px;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.95); /* More opaque background */
    border: 2px solid rgba(255, 255, 255, 0.8); /* Visible border */
    color: #059669;
    width: 36px; /* Larger size */
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-smooth);
    font-size: 20px; /* Larger X */
    font-weight: bold;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2); /* Stronger shadow */
    flex-shrink: 0;
    z-index: 10010; /* Higher z-index */
}

#close-float-chat:hover {
    background: rgba(255, 255, 255, 1);
    color: #dc2626;
    transform: translateY(-50%) scale(1.15);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.25);
    border-color: rgba(220, 38, 38, 0.3);
}

#close-float-chat:hover::before {
    color: #dc2626;
}

#close-float-chat:active {
    transform: translateY(-50%) scale(0.95);
}

/* === Enhanced Chat History === */
#chat-history {
    flex: 1;
    padding: 16px 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: linear-gradient(180deg, #ffffff 0%, #fefefe 100%);
    margin: 0;
    min-height: 0;
    position: relative;
    z-index: 10001;
    box-sizing: border-box;
}

#chat-history::-webkit-scrollbar {
    width: 6px;
}

#chat-history::-webkit-scrollbar-track {
    background: rgba(16, 185, 129, 0.05);
    border-radius: 3px;
}

#chat-history::-webkit-scrollbar-thumb {
    background: var(--primary-gradient);
    border-radius: 3px;
}

/* === Modern Chat Bubbles === */
.chat-message {
    display: flex;
    max-width: 85%;
    animation: fadeInUp 0.3s ease-out;
    margin-bottom: 6px;
}

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

.chat-message.user {
    align-self: flex-end;
    justify-content: flex-end;
    margin-left: auto;
}

.chat-message.bot {
    align-self: flex-start;
    justify-content: flex-start;
    margin-right: auto;
}

.bubble {
    padding: 12px 16px;
    border-radius: 20px;
    font-family: var(--font-family);
    line-height: 1.4;
    font-size: 14px;
    box-shadow: var(--shadow-light);
    position: relative;
    word-wrap: break-word;
}

.chat-message.user .bubble {
    background: var(--success-gradient);
    color: white;
    border-bottom-right-radius: 6px;
}

.chat-message.bot .bubble {
    background: #f8fafc;
    color: #1f2937;
    border-bottom-left-radius: 6px;
    border: 1px solid rgba(16, 185, 129, 0.2);
}

/* === Thinking bubble styling === */
.thinking-dietmaestro {
    display: flex;
    align-self: flex-start;
    justify-content: flex-start;
    max-width: 200px;
    margin-bottom: 6px;
    animation: fadeInUp 0.3s ease-out;
}

.thinking-dietmaestro .bubble {
    background: #f8fafc;
    color: #1f2937;
    border-bottom-left-radius: 6px;
    border: 1px solid rgba(16, 185, 129, 0.2);
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
}

.thinking-text {
    font-size: 13px;
    color: #047857;
    font-style: italic;
}

/* === Enhanced Input Form === */
#chat-form {
    display: flex;
    padding: 20px 20px 40px; /* Increased top padding */
    background: white;
    border-top: 1px solid rgba(16, 185, 129, 0.15);
    gap: 12px;
    align-items: flex-end;
    position: relative;
    z-index: 10004;
    margin: 0;
    background: white !important;
    flex-shrink: 0;
    min-height: 85px; /* Increased min-height */
    box-sizing: border-box;
    border-radius: 0 0 var(--border-radius) var(--border-radius);
}

#chat-input {
    flex-grow: 1;
    border: 1px solid #d1fae5;
    padding: 12px 18px;
    border-radius: 24px;
    font-size: 15px;
    font-family: var(--font-family);
    outline: none;
    color: #065f46;
    background: #f9fafb;
    transition: var(--transition-smooth);
    resize: none;
    min-height: 42px;
    max-height: 100px;
    line-height: 1.4;
    position: relative;
    z-index: 10005;
    box-sizing: border-box;
}

#chat-input:focus {
    border-color: #10b981;
    background: white;
    box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.1);
    outline: none;
}

#chat-input::placeholder {
    color: #6b7280;
}

/* === Enhanced Send Button === */
#chat-send {
    background: var(--primary-gradient);
    color: white;
    border: none;
    border-radius: 50%;
    width: 46px;
    height: 46px;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-light);
    transition: var(--transition-smooth);
    flex-shrink: 0;
    position: relative;
    z-index: 10005;
}

#chat-send:hover {
    transform: scale(1.1);
    box-shadow: var(--shadow-medium);
}

#chat-send:active {
    transform: scale(0.95);
}

#chat-send:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

/* === Enhanced Responsiveness === */
@media (max-width: 768px) {
    #chat-float-popup {
        width: calc(100vw - 30px);
        height: calc(100vh - 140px);
        bottom: 30px;
        right: 15px;
        left: 15px;
        max-height: none;
        z-index: 10000;
        margin-bottom: 30px;
    }

    #chat-float-btn {
        bottom: 110px !important;
        right: 20px;
        width: 150px;
        height: 52px;
        font-size: 14px;
    }

    .chat-btn-icon {
        margin-right: 6px;
    }

    .chat-btn-icon svg {
        width: 18px;
        height: 18px;
    }

    .chat-header {
        padding: 16px 55px 16px 18px;
        min-height: 65px;
        max-height: 65px;
        font-size: 15px;
    }

    .bot-avatar {
        width: 36px;
        height: 36px;
        font-size: 20px;
        margin-right: 10px;
    }

    #close-float-chat {
        width: 34px;
        height: 34px;
        font-size: 18px;
        right: 18px;
    }

    #chat-form {
        padding: 18px 18px 45px; /* Increased top padding for mobile */
        min-height: 90px; /* Increased min-height */
    }

    #chat-history {
        padding: 14px 18px;
        gap: 10px;
    }
}

@media (max-width: 640px) {
    #chat-float-btn {
        bottom: 170px !important;
        width: 140px;
        height: 50px;
        font-size: 13px;
    }

    .chat-btn-text {
        font-size: 13px;
    }

    #chat-form {
        padding: 20px 16px 50px; /* Further increased top padding */
        min-height: 95px;
    }
}

@media (max-width: 480px) {
    #chat-float-popup {
        width: 100vw;
        height: calc(100vh - 60px);
        bottom: 0;
        right: 0;
        left: 0;
        top: 60px;
        border-radius: 0;
        max-height: none;
        margin-bottom: 0;
    }

    #chat-float-btn {
        width: 130px;
        height: 48px;
        bottom: 110px !important;
        right: 15px;
        font-size: 12px;
        padding: 0 12px;
    }

    .chat-btn-icon {
        margin-right: 4px;
        flex-shrink: 0;
    }

    .chat-btn-icon svg {
        width: 16px;
        height: 16px;
    }

    .chat-btn-text {
        font-size: 12px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
        min-width: 0;
    }

    .chat-header {
        padding: 14px 50px 14px 16px;
        min-height: 60px;
        max-height: 60px;
        font-size: 14px;
    }

    .bot-avatar {
        width: 32px;
        height: 32px;
        font-size: 18px;
        margin-right: 8px;
    }

    #close-float-chat {
        width: 32px;
        height: 32px;
        font-size: 16px;
        right: 16px;
    }

    #chat-history {
        padding: 12px 16px;
        gap: 8px;
    }

    #chat-form {
        padding: 22px 16px 55px; /* Significantly increased top padding */
        padding-bottom: max(55px, env(safe-area-inset-bottom, 55px));
        min-height: 100px; /* Increased min-height */
    }

    .chat-message {
        max-width: 90%;
    }

    .bubble {
        padding: 10px 14px;
        font-size: 14px;
    }

    .thinking-dietmaestro {
        max-width: 180px;
    }

    .thinking-dietmaestro .thinking-text {
        font-size: 12px;
    }
}

@media (max-width: 390px) {
    #chat-float-btn {
        width: 120px;
        height: 44px;
        font-size: 11px;
        bottom: 110px !important;
        right: 12px;
    }

    .chat-btn-text {
        font-size: 11px;
    }

    .chat-btn-icon svg {
        width: 14px;
        height: 14px;
    }

    #chat-form {
        padding: 25px 14px 60px; /* Even more top padding for very small screens */
        min-height: 105px;
    }
}

@media (max-width: 360px) {
    #chat-float-btn {
        width: 110px;
        height: 42px;
        font-size: 10px;
        bottom: 110px !important;
        right: 10px;
        padding: 0 8px;
    }

    .chat-btn-text {
        font-size: 10px;
    }

    .chat-btn-icon {
        margin-right: 3px;
    }

    .chat-btn-icon svg {
        width: 12px;
        height: 12px;
    }

    .chat-header {
        padding: 12px 45px 12px 14px;
        min-height: 55px;
        max-height: 55px;
        font-size: 13px;
    }

    .bot-avatar {
        width: 28px;
        height: 28px;
        font-size: 16px;
        margin-right: 6px;
    }

    #close-float-chat {
        width: 30px;
        height: 30px;
        font-size: 14px;
        right: 14px;
    }

    #chat-history {
        padding: 10px 14px;
        gap: 6px;
    }

    #chat-form {
        padding: 28px 14px 65px; /* Maximum top padding for smallest screens */
        gap: 8px;
        min-height: 110px;
    }

    #chat-input {
        padding: 12px 16px; /* Slightly increased padding */
        font-size: 14px;
        min-height: 42px; /* Slightly increased height */
        margin-bottom: 2px; /* Small bottom margin to prevent overlap */
    }

    #chat-send {
        width: 42px;
        height: 42px;
        font-size: 14px;
        margin-bottom: 2px; /* Align with input */
    }

    .thinking-dietmaestro {
        max-width: 160px;
    }

    .thinking-dietmaestro .thinking-text {
        font-size: 11px;
    }
}

/* === Landscape orientation adjustments === */
@media (max-width: 768px) and (orientation: landscape) {
    #chat-float-btn {
        bottom: 110px !important;
        width: 120px;
        height: 40px;
        font-size: 11px;
    }

    .chat-btn-text {
        font-size: 11px;
    }

    .chat-btn-icon svg {
        width: 14px;
        height: 14px;
    }

    #chat-float-popup {
        height: calc(100vh - 100px);
        bottom: 20px;
        margin-bottom: 20px;
    }

    .chat-header {
        padding: 12px 50px 12px 16px;
        min-height: 50px;
        max-height: 50px;
    }

    #chat-form {
        padding: 18px 16px 35px; /* Increased top padding for landscape */
        min-height: 75px;
    }
}

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

@media (prefers-color-scheme: dark) {
    :root {
        --primary-gradient: linear-gradient(135deg, #059669 0%, #047857 100%);
    }

    #chat-float-popup {
        background: rgba(255, 255, 255, 0.95);
        border: 1px solid rgba(16, 185, 129, 0.3);
    }

    #chat-history {
        background: linear-gradient(180deg, #f9fafb 0%, #f3f4f6 100%);
    }

    .chat-message.bot .bubble,
    .thinking-dietmaestro .bubble {
        background: #f1f5f9;
        color: #1e293b;
        border: 1px solid rgba(16, 185, 129, 0.2);
    }

    .thinking-dietmaestro .thinking-text {
        color: #047857;
    }

    #chat-input {
        background: #f9fafb;
        border-color: #d1fae5;
        color: #065f46;
    }

    #chat-input:focus {
        border-color: #10b981;
        background: white;
    }

    #chat-form {
        background: white;
        border-top: 1px solid rgba(16, 185, 129, 0.2);
    }

    #close-float-chat:hover {
        color: #047857;
    }
}

/* === Enhanced Widget Fix === */
.elfsight-widget-container {
    height: 20px;
    overflow: hidden !important;
    margin: 0 !important;
    padding: 0 !important;
}

/* === Loading State === */
.chat-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    color: #6b7280;
    font-style: italic;
}

/* === Smooth Focus Management === */
*:focus {
    outline: 2px solid #10b981;
    outline-offset: 2px;
}

*:focus:not(:focus-visible) {
    outline: none;
    box-shadow: none;
}

/* Remove any default browser input styling that might cause double borders */
input, textarea {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

/* === Additional Mobile Optimizations === */
@media (hover: none) and (pointer: coarse) {
    #chat-float-btn:hover {
        transform: none;
    }

    #chat-float-btn:active {
        transform: scale(0.95);
        transition: transform 0.1s ease;
    }
}

/* === High DPI displays === */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .chat-btn-text {
        font-weight: 500;
    }
}

/* === iOS Safari specific fixes === */
@supports (-webkit-touch-callout: none) {
    #chat-form {
        padding-bottom: calc(40px + env(safe-area-inset-bottom, 0px)); /* Increased base padding */
    }

    @media (max-width: 768px) {
        #chat-form {
            padding-bottom: calc(45px + env(safe-area-inset-bottom, 0px));
        }
    }

    @media (max-width: 480px) {
        #chat-form {
            padding-bottom: calc(55px + env(safe-area-inset-bottom, 0px));
        }
    }

    @media (max-width: 360px) {
        #chat-form {
            padding-bottom: calc(65px + env(safe-area-inset-bottom, 0px));
        }
    }
}

/* === Additional mobile input positioning fix === */
@media (max-width: 480px) {
    #chat-input {
        /* Ensure input doesn't get too close to bottom edge */
        transform: translateY(-2px);
    }

    #chat-send {
        /* Align send button with adjusted input */
        transform: translateY(-2px);
    }
}

/* === Prevent keyboard overlay issues on mobile === */
@media (max-width: 768px) {
    #chat-float-popup.keyboard-open {
        height: 50vh !important;
        bottom: 10px !important;
    }

    #chat-float-popup.keyboard-open #chat-form {
        padding-top: 25px !important;
        padding-bottom: 20px !important;
    }
}

/* === Additional fixes for better spacing and alignment === */
@media (max-height: 600px) {
    #chat-float-popup {
        height: calc(100vh - 80px);
        max-height: calc(100vh - 80px);
        bottom: 40px;
    }

    .chat-header {
        padding: 14px 55px 14px 18px;
        min-height: 55px;
        max-height: 55px;
    }

    #chat-form {
        padding-bottom: 35px;
        min-height: 75px;
    }
}

@media (max-height: 500px) {
    #chat-float-popup {
        height: calc(100vh - 60px);
        max-height: calc(100vh - 60px);
        bottom: 30px;
    }

    .chat-header {
        padding: 12px 50px 12px 16px;
        min-height: 50px;
        max-height: 50px;
    }

    #chat-form {
        padding-bottom: 30px;
        min-height: 70px;
    }
}

/* === Ensure proper stacking and no overlap === */
#chat-popup-inner > * {
    position: relative;
}

/* === Fix for any potential scrollbar issues === */
#chat-history {
    scrollbar-width: thin;
    scrollbar-color: #10b981 rgba(16, 185, 129, 0.05);
}

/* === Prevent any potential content overflow === */
#chat-float-popup * {
    box-sizing: border-box;
}

/* === Enhanced focus states for accessibility === */
#chat-input:focus-visible {
    outline: none;
    border-color: #10b981;
    box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.1);
}

#chat-send:focus-visible {
    outline: 2px solid #10b981;
    outline-offset: 2px;
}

/* === Additional close button styles to ensure visibility === */
#close-float-chat {
    /* Ensure it's always on top */
    position: absolute !important;
    z-index: 10010 !important;

    /* Make it more contrasted */
    background: white !important;
    border: 2px solid rgba(5, 150, 105, 0.3) !important;

    /* Add text shadow for better visibility */
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

#close-float-chat::before {
    /* Use a more visible symbol */
    content: '✕' !important;
    color: #059669 !important;
    font-weight: 900 !important;
    line-height: 0.8 !important;
}