/* Crisp Chat-Style Enhanced Widget */
:root {
    --gd-primary: #4b9fff;
    --gd-primary-dark: #3a8fff;
    --gd-primary-light: #65b3ff;
    --gd-success: #10b981;
    --gd-warning: #f59e0b;
    --gd-danger: #ef4444;
    --gd-gray-50: #f8fafc;
    --gd-gray-100: #f1f5f9;
    --gd-gray-200: #e2e8f0;
    --gd-gray-300: #cbd5e1;
    --gd-gray-400: #94a3b8;
    --gd-gray-500: #64748b;
    --gd-gray-600: #475569;
    --gd-gray-700: #334155;
    --gd-gray-800: #1e293b;
    --gd-gray-900: #0f172a;
    --gd-white: #ffffff;
    --gd-shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --gd-shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --gd-shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --gd-shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    --gd-shadow-2xl: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

.gd-enhanced-widget {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 999999;
    font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', 'Roboto', sans-serif;
    font-size: 14px;
    line-height: 1.5;
}

.gd-position-bottom-right {
    right: 20px;
}

.gd-position-bottom-left {
    left: 20px;
}

/* Round Chat Bubble */
.gd-enhanced-bubble {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--gd-primary) 0%, var(--gd-accent) 100%);
    box-shadow: 0 8px 32px rgba(79, 70, 229, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    border: none;
    overflow: hidden;
    z-index: 999999;
    border: 3px solid rgba(255, 255, 255, 0.2);
}

.gd-enhanced-bubble:hover {
    transform: scale(1.1) translateY(-2px);
    box-shadow: 0 12px 48px rgba(79, 70, 229, 0.6);
    background: linear-gradient(135deg, color-mix(in srgb, var(--gd-primary) 85%, #ffffff) 0%, color-mix(in srgb, var(--gd-accent) 85%, #ffffff) 100%);
}

.gd-enhanced-bubble.gd-active {
    transform: scale(0.9);
    background: linear-gradient(135deg, color-mix(in srgb, var(--gd-primary) 80%, #000) 0%, color-mix(in srgb, var(--gd-accent) 80%, #000) 100%);
}

.gd-enhanced-bubble::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0.05) 100%);
    opacity: 0;
    transition: opacity 0.2s ease;
}

.gd-enhanced-bubble:hover::before {
    opacity: 1;
}

.gd-agent-avatar-wrapper {
    position: relative;
    width: 48px;
    height: 48px;
}

.gd-agent-avatar {
    width: 48px;
    height: 48px;
    border-radius: 10px;
    object-fit: cover;
    box-shadow: var(--gd-shadow-md);
}

.gd-default-avatar {
    width: 48px;
    height: 48px;
    border-radius: 10px;
    background: var(--gd-gray-200);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--gd-gray-600);
    font-size: 20px;
    box-shadow: var(--gd-shadow-sm);
}

.gd-online-badge {
    position: absolute;
    bottom: -2px;
    right: -2px;
    width: 14px;
    height: 14px;
    background: var(--gd-success);
    border: 2px solid var(--gd-white);
    border-radius: 50%;
    animation: gd-pulse 2s infinite;
    z-index: 2;
}

.gd-online-badge-small {
    position: absolute;
    bottom: -1px;
    right: -1px;
    width: 12px;
    height: 12px;
    background: var(--gd-success);
    border: 2px solid var(--gd-white);
    border-radius: 50%;
    animation: gd-pulse 2s infinite;
}

@keyframes gd-pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
    }
    70% {
        box-shadow: 0 0 0 8px rgba(16, 185, 129, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0);
    }
}

.gd-bubble-text {
    position: absolute;
    bottom: 70px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--gd-gray-900);
    color: var(--gd-white);
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 13px;
    white-space: nowrap;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 1000;
    box-shadow: var(--gd-shadow-lg);
    max-width: 250px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.gd-enhanced-bubble:hover .gd-bubble-text {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(-5px);
}

.gd-bubble-icon {
    color: var(--gd-white);
    font-size: 24px;
    z-index: 1;
}
.gd-bubble-icon svg {
    display: block;
    width: 28px;
    height: 28px;
    fill: #ffffff;
}

/* Enhanced Window */
.gd-enhanced-window {
    width: 380px;
    height: 600px;
    background: #ffffff;
    border-radius: 20px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: fixed;
    bottom: 80px;
    right: 20px;
    z-index: 999999;
    animation: gd-slide-up 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid #e2e8f0;
}

@keyframes gd-slide-up {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.gd-enhanced-header {
    background: linear-gradient(135deg, var(--gd-primary) 0%, var(--gd-accent) 100%);
    color: var(--gd-white);
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
}

.gd-enhanced-header::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
}

.gd-agent-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.gd-agent-avatar-small {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.gd-agent-details h4 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    line-height: 1.2;
}

.gd-status-info {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 2px;
}

.gd-status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--gd-success);
    animation: gd-pulse 2s infinite;
}

.gd-status-dot.gd-status-away {
    background: var(--gd-warning);
}

.gd-status-dot.gd-status-busy {
    background: var(--gd-danger);
}

.gd-status-dot.gd-status-offline {
    background: var(--gd-gray-400);
    animation: none;
}

.gd-status-text {
    font-size: 12px;
    opacity: 0.9;
    font-weight: 500;
}

.gd-header-actions {
    display: flex;
    gap: 8px;
}

.gd-action-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: var(--gd-white);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 14px;
}

.gd-action-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
}

.gd-chat-close {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: var(--gd-white);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 18px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.gd-chat-close:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
}

/* Messages Area */
.gd-enhanced-messages {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    background: var(--gd-gray-50);
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.gd-enhanced-messages::-webkit-scrollbar {
    width: 4px;
}

.gd-enhanced-messages::-webkit-scrollbar-track {
    background: transparent;
}

.gd-enhanced-messages::-webkit-scrollbar-thumb {
    background: var(--gd-gray-300);
    border-radius: 2px;
}

.gd-enhanced-messages::-webkit-scrollbar-thumb:hover {
    background: var(--gd-gray-400);
}

.gd-message {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    animation: gd-message-slide 0.3s ease-out;
}

@keyframes gd-message-slide {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.gd-user-message {
    justify-content: flex-end;
    flex-direction: row-reverse;
}

.gd-message-content {
    max-width: 80%;
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.4;
    word-wrap: break-word;
    position: relative;
}

.gd-agent-message .gd-message-content {
    background: var(--gd-white);
    color: var(--gd-gray-800);
    border: 1px solid var(--gd-gray-200);
    box-shadow: var(--gd-shadow-sm);
}

.gd-user-message .gd-message-content {
    background: linear-gradient(135deg, var(--gd-primary) 0%, var(--gd-accent) 100%);
    color: var(--gd-white);
    box-shadow: var(--gd-shadow-md);
}

.gd-message-time {
    font-size: 11px;
    opacity: 0.7;
    margin-top: 4px;
    text-align: right;
}

.gd-user-message .gd-message-time {
    text-align: left;
}

/* File Upload Styles */
.gd-file-preview {
    background: var(--gd-gray-100);
    border: 1px solid var(--gd-gray-300);
    border-radius: 8px;
    padding: 8px 12px;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.gd-file-name {
    font-size: 12px;
    color: var(--gd-gray-700);
    font-weight: 500;
}

.gd-remove-file {
    background: none;
    border: none;
    color: var(--gd-gray-500);
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    padding: 0 4px;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.gd-remove-file:hover {
    color: var(--gd-danger);
    background: var(--gd-gray-200);
}

.gd-upload-progress {
    height: 4px;
    background: var(--gd-gray-200);
    border-radius: 2px;
    overflow: hidden;
    margin-bottom: 8px;
}

.gd-progress-bar {
    height: 100%;
    background: linear-gradient(90deg, var(--gd-primary), var(--gd-accent));
    width: 0;
    transition: width 0.3s ease;
    border-radius: 2px;
}

/* Lead Form Styles */
.gd-lead-form {
    background: var(--gd-white);
    border-top: 1px solid var(--gd-gray-200);
    padding: 20px;
    animation: gd-slide-up 0.3s ease-out;
}

.gd-form-header h3 {
    margin: 0 0 8px 0;
    color: var(--gd-gray-800);
    font-size: 18px;
    font-weight: 600;
}

.gd-form-header p {
    margin: 0 0 20px 0;
    color: var(--gd-gray-600);
    font-size: 14px;
}

.gd-form-field {
    margin-bottom: 16px;
}

.gd-form-field input,
.gd-form-field textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--gd-gray-300);
    border-radius: 8px;
    font-size: 14px;
    transition: all 0.2s ease;
    background: var(--gd-white);
}

.gd-form-field input:focus,
.gd-form-field textarea:focus {
    outline: none;
    border-color: var(--gd-primary);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.gd-form-actions {
    display: flex;
    gap: 12px;
    margin-top: 20px;
}

.gd-submit-btn {
    flex: 1;
    background: linear-gradient(135deg, var(--gd-primary) 0%, var(--gd-accent) 100%);
    color: var(--gd-white);
    border: none;
    padding: 12px 20px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.gd-submit-btn:hover {
    transform: translateY(-1px);
    box-shadow: var(--gd-shadow-md);
}

.gd-skip-btn {
    flex: 1;
    background: var(--gd-gray-200);
    color: var(--gd-gray-700);
    border: none;
    padding: 12px 20px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.gd-skip-btn:hover {
    background: var(--gd-gray-300);
}

/* Input Area */
.gd-enhanced-input-area {
    background: var(--gd-white);
    border-top: 1px solid var(--gd-gray-200);
    padding: 20px;
    position: relative;
}

.gd-input-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.gd-file-upload-area {
    padding: 8px 0;
}

.gd-input-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--gd-gray-100);
    border: 1px solid var(--gd-gray-300);
    border-radius: 20px;
    padding: 8px 12px;
    transition: all 0.2s ease;
}

.gd-input-wrapper:focus-within {
    border-color: var(--gd-primary);
    background: var(--gd-white);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.gd-enhanced-input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    font-size: 14px;
    padding: 8px 0;
    color: var(--gd-gray-800);
}

.gd-enhanced-input::placeholder {
    color: var(--gd-gray-500);
}

.gd-input-tools {
    display: flex;
    align-items: center;
    gap: 8px;
}

.gd-char-count {
    font-size: 12px;
    color: var(--gd-gray-500);
    min-width: 60px;
    text-align: right;
}

.gd-attachment-btn {
    background: none;
    border: none;
    color: var(--gd-gray-500);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.gd-attachment-btn:hover {
    color: var(--gd-primary);
    background: var(--gd-gray-200);
}

.gd-send-btn {
    background: linear-gradient(135deg, var(--gd-primary) 0%, var(--gd-accent) 100%);
    color: var(--gd-white);
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    opacity: 0.6;
}

.gd-send-btn:not(:disabled):hover {
    transform: scale(1.05);
    box-shadow: var(--gd-shadow-md);
}

.gd-send-btn:disabled {
    cursor: not-allowed;
    opacity: 0.4;
}

.gd-send-btn:not(:disabled) {
    opacity: 1;
}

/* Typing Indicator */
.gd-typing-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: var(--gd-white);
    border-radius: 18px;
    margin: 8px 0;
    animation: gd-fade-in 0.3s ease-out;
    max-width: fit-content;
    border: 1px solid var(--gd-gray-200);
}

@keyframes gd-fade-in {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.gd-typing-dots {
    display: flex;
    gap: 3px;
}

.gd-typing-dots span {
    width: 6px;
    height: 6px;
    background: var(--gd-gray-400);
    border-radius: 50%;
    animation: gd-typing-bounce 1.4s infinite ease-in-out;
}

.gd-typing-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.gd-typing-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes gd-typing-bounce {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

.gd-typing-text {
    font-size: 12px;
    color: var(--gd-gray-500);
    font-style: italic;
}

/* Responsive Design */
@media (max-width: 480px) {
    .gd-enhanced-window {
        width: calc(100vw - 40px);
        height: calc(100vh - 40px);
        bottom: 20px;
        right: 20px;
        max-width: 350px;
    }
    
    .gd-position-left .gd-enhanced-window,
    .gd-position-right .gd-enhanced-window {
        right: auto;
        left: 20px;
    }
}

@media (max-width: 360px) {
    .gd-enhanced-window {
        width: calc(100vw - 20px);
        height: calc(100vh - 20px);
        bottom: 10px;
        right: 10px;
        left: 10px;
        border-radius: 16px;
    }
}

/* Loading States */
.gd-loading {
    position: relative;
}

.gd-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    border: 2px solid transparent;
    border-top: 2px solid currentColor;
    border-radius: 50%;
    animation: gd-spin 1s linear infinite;
}

@keyframes gd-spin {
    to {
        transform: rotate(360deg);
    }
}

/* Error States */
.gd-error {
    color: var(--gd-danger);
    background: rgba(255, 107, 107, 0.1);
    border: 1px solid rgba(255, 107, 107, 0.2);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 13px;
    margin: 8px 0;
}

/* Success States */
.gd-success {
    color: var(--gd-success);
    background: rgba(0, 242, 254, 0.1);
    border: 1px solid rgba(0, 242, 254, 0.2);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 13px;
    margin: 8px 0;
}

/* Animations */
.gd-bounce-in {
    animation: gd-bounce-in 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

@keyframes gd-bounce-in {
    0% {
        opacity: 0;
        transform: scale(0.3);
    }
    50% {
        opacity: 1;
        transform: scale(1.05);
    }
    70% {
        transform: scale(0.9);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

.gd-slide-out {
    animation: gd-slide-out 0.3s ease-in forwards;
}

@keyframes gd-slide-out {
    to {
        opacity: 0;
        transform: translateX(100%);
    }
}

/* Free-tier branding (Crisp-like "We run on" style) */
.gd-branding {
    border-top: 1px solid var(--gd-gray-200);
    background: var(--gd-gray-50);
    color: var(--gd-gray-500);
    font-size: 12px;
    padding: 10px 14px;
    text-align: center;
}
.gd-branding a {
    color: var(--gd-primary);
    text-decoration: none;
    font-weight: 600;
}
.gd-branding a:hover {
    text-decoration: underline;
}
/* === Enhancements: viewport safety, tabs UI, Font Awesome bubble icon, left-position fixes === */

/* Prefer FA icon when available; keep SVG as fallback */
.gd-bubble-icon .fa-solid { display: none; }
html.gd-fa .gd-bubble-icon .fa-solid {
    display: block;
    font-size: 24px;
    line-height: 1;
    color: #fff;
}
html.gd-fa .gd-bubble-icon svg { display: none; }

/* Respect safe-area (iOS notch) and keep widget/panel in-bounds */
.gd-enhanced-widget {
    bottom: calc(20px + env(safe-area-inset-bottom));
    right: calc(20px + env(safe-area-inset-right));
}
.gd-position-bottom-left.gd-enhanced-widget {
    left: calc(20px + env(safe-area-inset-left));
    right: auto;
}
.gd-enhanced-window {
    bottom: calc(80px + env(safe-area-inset-bottom));
    right: calc(20px + env(safe-area-inset-right));
    max-height: calc(100vh - 120px);
    max-width: min(92vw, 420px);
}
.gd-position-bottom-left .gd-enhanced-window {
    left: calc(20px + env(safe-area-inset-left));
    right: auto;
}

/* Header tabs for Chat / Helpdesk */
.gd-header-tabs {
    display: flex;
    gap: 8px;
    padding: 10px 12px;
    background: var(--gd-white);
    border-bottom: 1px solid var(--gd-gray-200);
}
.gd-tab {
    border: 1px solid var(--gd-gray-300);
    background: var(--gd-gray-100);
    color: var(--gd-gray-700);
    padding: 8px 12px;
    border-radius: 16px;
    cursor: pointer;
    font-size: 13px;
    transition: all 0.2s ease;
}
.gd-tab:hover {
    background: var(--gd-gray-200);
}
.gd-tab.active {
    background: linear-gradient(135deg, var(--gd-primary) 0%, var(--gd-accent) 100%);
    border-color: transparent;
    color: var(--gd-white);
    box-shadow: var(--gd-shadow-sm);
}
.gd-tab-content { display: none; }
.gd-tab-chat { display: block; } /* default shown */

/* Knowledge Base results styling */
.gd-knowledge-results-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.gd-kb-result {
    padding: 12px;
    border: 1px solid var(--gd-gray-200);
    border-radius: 10px;
    background: var(--gd-white);
    box-shadow: var(--gd-shadow-sm);
}
.gd-kb-title {
    display: inline-block;
    font-weight: 600;
    color: var(--gd-primary);
    text-decoration: none;
}
.gd-kb-title:hover { text-decoration: underline; }
.gd-kb-excerpt {
    margin-top: 6px;
    color: var(--gd-gray-600);
    font-size: 13px;
}

/* Mobile tuning */
@media (max-width: 480px) {
    .gd-enhanced-window {
        inset: auto auto;
        right: 10px;
        left: 10px;
        bottom: calc(10px + env(safe-area-inset-bottom));
        width: auto;
        max-width: 100%;
        height: calc(100vh - (20px + env(safe-area-inset-bottom)));
        max-height: calc(100vh - (20px + env(safe-area-inset-bottom)));
        border-radius: 16px;
    }
    .gd-header-tabs {
        position: sticky;
        top: 0;
        z-index: 2;
    }
}

/* Ensure messages area never overflows vertically if custom styles alter heights */
.gd-enhanced-messages { overscroll-behavior: contain; }