/* ===== CSS Variables ===== */
:root {
    --primary-color: #d32f2f;
    --primary-dark: #b71c1c;
    --secondary-color: #424242;
    --text-dark: #212121;
    --text-light: #757575;
    --bg-light: #f5f5f5;
    --white: #ffffff;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.15);
}

/* ===== Reset & Base ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: transparent;
    /* Prevent any extra space */
    line-height: 1;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ===== Chat Widget Button ===== */
.chat-widget-button {
    position: fixed;
    bottom: 10px;
    right: 10px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: var(--white);
    border: none;
    cursor: pointer;
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s ease, box-shadow 0.3s ease, opacity 0.3s ease;
    z-index: 9999;
    margin: 0;
    padding: 0;
}

.chat-widget-button:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 40px rgba(211, 47, 47, 0.4);
}

.chat-widget-button:hover .widget-car-icon {
    animation: widget-shake 0.5s ease-in-out infinite;
}

@keyframes widget-shake {
    0%, 100% { transform: translateX(0) rotate(0deg); }
    25% { transform: translateX(-3px) rotate(-3deg); }
    75% { transform: translateX(3px) rotate(3deg); }
}

.chat-widget-button:active {
    transform: scale(0.95);
}

.chat-widget-button.hidden {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.8);
}

/* ===== Chat Window ===== */
.chat-window {
    position: fixed;
    bottom: 10px;
    right: 10px;
    width: 400px;
    height: 600px;
    max-width: calc(100vw - 20px);
    max-height: calc(100vh - 20px);
    background: var(--white);
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    z-index: 9998;
    opacity: 0;
    visibility: hidden;
    transform: scale(0.9) translateY(20px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Minimized state - hide chat window */
.chat-window.minimized {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: scale(0.9) translateY(20px);
}

/* Open state - show chat window */
.chat-window:not(.minimized) {
    opacity: 1;
    visibility: visible;
    transform: scale(1) translateY(0);
}

/* ===== Chat Header ===== */
.chat-header {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: var(--white);
    padding: 16px 20px;
    border-radius: 16px 16px 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    min-height: 70px;
    /* Add safe area for mobile notch/status bar */
    padding-top: max(16px, env(safe-area-inset-top));
}

.chat-header-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.chat-header-info h3 {
    font-size: 18px;
    font-weight: 600;
    margin: 0;
}

.chat-status {
    font-size: 12px;
    opacity: 0.9;
    display: flex;
    align-items: center;
    margin: 0;
}

.chat-status::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #4caf50;
    margin-right: 6px;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.chat-close-button {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: var(--white);
    cursor: pointer;
    padding: 8px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease;
    flex-shrink: 0;
}

.chat-close-button:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* ===== Messages Container ===== */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: var(--bg-light);
    min-height: 0; /* Critical for flex scrolling */
}

.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #aaa;
}

/* ===== Messages ===== */
.message {
    display: flex;
    animation: slideIn 0.3s ease;
}

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

.message-content {
    max-width: 75%;
    padding: 12px 16px;
    border-radius: 16px;
    word-wrap: break-word;
}

.welcome-highlight {
    border: 2px solid #f4c430;
    background: #fffdf2;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
}

.bot-message {
    justify-content: flex-start;
    align-items: flex-end;
    gap: 8px;
}

.bot-avatar {
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: var(--shadow);
}

.bot-avatar svg {
    width: 20px;
    height: 20px;
}

.bot-message .message-content {
    background: var(--white);
    color: var(--text-dark);
    box-shadow: var(--shadow);
    border-bottom-left-radius: 4px;
}

.user-message {
    justify-content: flex-end;
}

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

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

/* ===== Typing Indicator ===== */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
    background: var(--white);
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    width: fit-content;
    box-shadow: var(--shadow);
}

.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-light);
    animation: typing 1.4s infinite;
}

.typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.5;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

/* ===== Input Area ===== */
.chat-input-container {
    padding: 16px 20px;
    background: var(--white);
    border-top: 1px solid #e0e0e0;
    border-radius: 0 0 16px 16px;
    flex-shrink: 0;
    min-height: 76px;
}

.chat-form {
    display: flex;
    gap: 8px;
    align-items: center;
}

.chat-input {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid #e0e0e0;
    border-radius: 24px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s ease;
    font-family: inherit;
}

.chat-input:focus {
    border-color: var(--primary-color);
}

.chat-send-button {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--primary-color);
    color: var(--white);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease, transform 0.1s ease;
    flex-shrink: 0;
}

.chat-send-button:hover {
    background: var(--primary-dark);
}

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

.chat-send-button:disabled {
    background: #ccc;
    cursor: not-allowed;
}

/* ===== Responsive Design ===== */
@media (max-width: 768px) {
    /* Remove ALL gaps and positioning on mobile */
    html, body {
        margin: 0 !important;
        padding: 0 !important;
        width: 100% !important;
        height: 100% !important;
    }
    
    .chat-window {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        bottom: 0 !important;
        right: 0 !important;
        width: 100% !important;
        height: 100% !important;
        max-width: 100% !important;
        max-height: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        border-radius: 0 !important;
        /* Slide up from bottom animation */
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    .chat-window:not(.minimized) {
        transform: translateY(0) !important;
        opacity: 1 !important;
        visibility: visible !important;
    }
    
    .chat-window.minimized {
        transform: translateY(100%) !important;
    }

    .chat-header {
        border-radius: 0;
        padding: 20px;
        /* IMPORTANT: Account for notch/status bar on iPhone */
        padding-top: calc(20px + env(safe-area-inset-top));
        min-height: calc(70px + env(safe-area-inset-top));
    }

    .chat-input-container {
        border-radius: 0;
        padding: 20px;
        /* Account for mobile keyboard and home indicator */
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }

    .chat-widget-button {
        bottom: 0px;
        right: 0px;
        width: 60px;
        height: 60px;
        /* Account for mobile home indicators */
        margin-bottom: env(safe-area-inset-bottom);
    }
    
    /* Adjust messages for mobile */
    .chat-messages {
        padding: 16px;
        /* Account for safe areas */
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }
    
    .message-content {
        max-width: 85%;
        font-size: 15px; /* Slightly larger on mobile */
    }
    
    /* Make input larger for mobile */
    .chat-input {
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 14px 18px;
    }
    
    .chat-send-button {
        width: 48px;
        height: 48px;
    }
}