/* CSS Custom Properties for theming */
:root {
    --primary-color: #4F46E5;
    --primary-gradient: linear-gradient(135deg, #4F46E5 0%, #7C3AED 100%);
    --secondary-color: #F8FAFC;
    --header-color: #ffffff;
    --brand-image-height: 40px;
    --footer-color: #ffffff;
    --text-color: #1F2937;
    --text-light: #6B7280;
    --border-color: #E5E7EB;
    --success-color: #10B981;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
}

/* --- Container --- */
.chat-widget {
    position: fixed;
    bottom: 24px;
    right: 24px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    z-index: 9999;
}

/* --- Floating Bubble --- */
.chat-bubble {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--primary-gradient);
    color: white;
    border: none;
    cursor: pointer;
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    z-index: 10;
}

/* Hide bubble when window is open */
.chat-window:not(.hidden) ~ .chat-bubble {
    opacity: 0;
    transform: scale(0);
    pointer-events: none;
}

.chat-bubble:hover {
    transform: translateY(-2px) scale(1.05);
    box-shadow: var(--shadow-xl);
}

/* --- Chat Window (Desktop Defaults) --- */
.chat-window {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 380px;
    height: 700px;
    background: white;
    border-radius: 16px;
    box-shadow: var(--shadow-xl);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid var(--border-color);
    transform-origin: bottom right;
    z-index: 20;
}

.chat-window .message-time {
    font-size: 10px;
}

.chat-window.hidden {
    opacity: 0;
    pointer-events: none;
    transform: translateY(20px) scale(0.95);
}

/* --- Header --- */
.chat-header {
    background: var(--header-color);
    color: var(--text-color);
    padding: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    border-bottom: 1px solid var(--border-color);
}

.brand-area {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    pointer-events: none;
}

.brand-logo {
    height: var(--brand-image-height);
    width: auto;
    display: block;
    pointer-events: auto;
}

.debug-area {
    width: 100%;
    order: 3;
    background-color: #FEF3C7;
    border: 1px solid #ccb040;
    font-size: 10px;
    text-align: center;
    border-radius: 10px;
    padding: 5px;
    margin: 5px;
}

.header-actions {
    display: flex;
    align-items: center;
    margin-left: auto;
    flex-shrink: 0;
}

.close-button {
    background: var(--secondary-color);
    border: none;
    color: var(--text-color);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    position: static;
    transform: none;
}

.close-button:hover {
    background: var(--border-color);
}

/* --- Messages Area --- */
.chat-body {
    flex: 1;
    background: var(--secondary-color);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 16px;
}

.message {
    display: flex;
    max-width: 85%;
    animation: slideIn 0.3s ease-out;
}

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

.user-message { align-self: flex-end; }
.bot-message { align-self: flex-start; }

.message-content {
    background: white;
    padding: 12px 16px;
    border-radius: 16px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-color);
}

.user-message .message-content {
    background: var(--primary-gradient);
    color: white;
    border: none;
    border-radius: 16px 16px 4px 16px;
}

.bot-message .message-content {
    border-radius: 16px 16px 16px 4px;
}

.message-content p {
    margin: 0;
    font-size: 14px;
    line-height: 1.5;
}

/* --- Footer & Input --- */
.chat-footer {
    padding: 16px;
    background: var(--footer-color);
    border-top: 1px solid var(--border-color);
}

.input-container {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--secondary-color);
    border: 1px solid var(--border-color);
    border-radius: 24px;
    padding: 4px;
}

#message-input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    padding: 8px 12px;
    font-size: 14px;
}

.send-button {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: var(--primary-gradient);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* --- End Chat button --- */
.end-chat-button {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-color);
    border-radius: 6px;
    padding: 5px 10px;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s ease;
    order: -1;
}

.end-chat-button:hover {
    background: #fee2e2;
    border-color: #fca5a5;
    color: #991b1b;
}

/* --- Start Chat container --- */
.start-chat-container {
    display: flex;
    justify-content: center;
    padding: 8px;
}

.start-chat-container.hidden {
    display: none;
}

.start-chat-button {
    padding: 10px 24px;
    border-radius: 6px;
    border: none;
    background: var(--primary-gradient);
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.2s ease;
}

.start-chat-button:hover {
    opacity: 0.9;
}

/* --- Desktop positioning override --- */
@media (min-width: 481px) {
    .chat-widget {
        position: fixed;
        bottom: 24px;
        right: 24px;
        width: auto;
        height: auto;
    }

    .chat-window {
        position: absolute;
        bottom: 0;
        right: 0;
        width: 380px;
        max-height: calc(100vh - 48px);
    }

    .chat-bubble {
        position: relative;
    }
}

/* --- Mobile Responsiveness (Full Height Only) --- */
@media (max-width: 480px) {
    .chat-widget {
        top: 0 !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
    }

    .chat-window {
        position: fixed !important;
        top: 0 !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        height: 100% !important;
        height: 100dvh !important;
        max-height: none !important;
        border-radius: 0 !important;
        border: none !important;
    }

    .chat-bubble {
        position: fixed !important;
        bottom: 24px !important;
        right: 24px !important;
        width: 64px;
        height: 64px;
    }

    #message-input {
        font-size: 16px; /* Prevent iOS zoom-in */
    }

    .chat-header {
        padding-top: calc(16px + env(safe-area-inset-top));
    }

    .chat-footer {
        padding-bottom: calc(16px + env(safe-area-inset-bottom));
    }
}

/* --- Scrollbar --- */
.chat-body::-webkit-scrollbar { width: 4px; }
.chat-body::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 2px;
}

/* --- Connection Status Styles --- */
.connection-status {
    padding: 8px 12px;
    border-radius: 8px;
    text-align: center;
    font-size: 12px;
    margin-bottom: 8px;
    transition: all 0.3s ease;
}

.connection-status.connecting {
    background: #FEF3C7;
    color: #92400E;
}

.connection-status.connected {
    background: transparent;
    color: var(--text-light);
    font-style: italic;
}

.connection-status.disconnected {
    background: #FEE2E2;
    color: #991B1B;
}

.connection-status.typing {
    background: #E0E7FF;
    color: #3730A3;
}

/* --- File Preview Styles --- */
.file-preview {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
    max-height: 160px;
    overflow-y: auto;
}

.file-preview.hidden {
    display: none;
}

.file-item {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 6px 10px;
    background: #f3f4f6;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    font-size: 13px;
    color: #374151;
}

/* --- File item thumbnail (image preview) --- */
.file-item-thumb {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 6px;
    overflow: hidden;
    background: #e5e7eb;
    display: flex;
    align-items: center;
    justify-content: center;
}

.file-item-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.file-item-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.file-item-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-item-size {
    flex-shrink: 0;
    color: #9ca3af;
    font-size: 11px;
}

/* --- Upload progress bar --- */
.file-item-progress {
    height: 4px;
    background: #e5e7eb;
    border-radius: 2px;
    overflow: hidden;
    margin-top: 4px;
}

.file-item-progress.hidden {
    display: none;
}

.file-item-progress-bar {
    height: 100%;
    width: 0%;
    background: var(--primary-gradient);
    border-radius: 2px;
    transition: width 0.15s ease;
}

.remove-file-btn {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: #9ca3af;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
}

.remove-file-btn:hover {
    background-color: #fee2e2;
    color: #dc2626;
}

.remove-file-btn:active {
    background-color: #fecaca;
}

.remove-file-btn svg {
    pointer-events: none;
}

.attach-button {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: var(--secondary-color);
    color: var(--text-color);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.attach-button:hover {
    background: var(--border-color);
}

/* --- Animation override for history messages --- */
.message.no-animation {
    animation: none;
}

/* --- System notice --- */
.system-notice {
    text-align: center;
    font-size: 0.72rem;
    color: #888;
    padding: 4px 12px;
    margin: 6px auto;
    font-style: italic;
}

/* --- Image attachment in sent messages --- */
.file-attachment--image {
    display: block;
    margin-top: 6px;
    border-radius: 10px;
    overflow: hidden;
    text-decoration: none;
    color: inherit;
    max-width: 200px;
}

.file-attachment-thumb {
    width: 100%;
    height: auto;
    display: block;
    border-radius: 10px 10px 0 0;
}

.file-attachment-name {
    display: block;
    font-size: 11px;
    padding: 4px 6px;
    opacity: 0.85;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* --- Form Modal Overlay --- */
.kore-form-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.kore-form-modal-overlay.hidden {
    display: none !important;
}

.kore-form-modal {
    background: white;
    border-radius: 16px;
    width: 90%;
    max-width: 600px;
    height: 85vh;
    max-height: 850px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: var(--shadow-xl);
    border: 1px solid var(--border-color);
}

.kore-form-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
    background: var(--header-color);
    flex-shrink: 0;
}

.kore-form-modal-title {
    font-weight: 600;
    font-size: 15px;
    color: var(--text-color);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding-right: 12px;
}

.kore-form-modal-close {
    background: var(--secondary-color);
    border: none;
    color: var(--text-color);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.kore-form-modal-close:hover {
    background: var(--border-color);
}

.kore-form-modal-body {
    flex: 1;
    overflow: hidden;
    background: var(--secondary-color);
}

.kore-form-modal-iframe {
    width: 100%;
    height: 100%;
    border: none;
    display: block;
}

.form-expired-notice {
    padding: 24px;
    text-align: center;
    color: var(--text-light);
    font-size: 14px;
}

/* --- Mobile: Form modal full-screen --- */
@media (max-width: 480px) {
    .kore-form-modal {
        width: 100%;
        height: 100%;
        max-height: none;
        border-radius: 0;
        border: none;
    }

    .kore-form-modal-header {
        padding-top: calc(16px + env(safe-area-inset-top));
    }
}

/* --- Protect custom widget from JavaScript overrides --- */
.chat-widget {
    position: fixed !important;
    bottom: 24px !important;
    right: 24px !important;
    z-index: 9999 !important;
}

@media (max-width: 480px) {
    .chat-widget {
        top: 0 !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
    }
}

/* Ensure chat window positioning on desktop */
@media (min-width: 481px) {
    .chat-window {
        position: absolute !important;
        bottom: 0 !important;
        right: 0 !important;
    }
}

/* --- Generic hidden utility --- */
.hidden {
    display: none !important;
}

.message.bot-message a {
    color: inherit;
    text-decoration: underline;
    word-break: break-all;
}

.message.bot-message a:hover {
    opacity: 0.8;
}

.chat-phone-number {
    white-space: nowrap;
    display: inline-block;
}

.reconnecting-notice {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #fff8e1;
    color: #7a5c00;
    border: 1px solid #ffe082;
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 0.8rem;
    margin: 6px auto;
    max-width: 85%;
    text-align: center;
    justify-content: center;
}

.reconnecting-spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid #ffe082;
    border-top-color: #7a5c00;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    flex-shrink: 0;
}

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