* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --secondary-color: #6b7280;
    --background: #f9fafb;
    --surface: #ffffff;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --border: #e5e7eb;
    --success: #10b981;
    --error: #ef4444;
    --warning: #f59e0b;
    --user-message: #4f46e5;
    --ai-message: #f3f4f6;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
}

/* Explicit light theme (overrides system preference) */
[data-theme="light"] {
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --secondary-color: #6b7280;
    --background: #f9fafb;
    --surface: #ffffff;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --border: #e5e7eb;
    --success: #10b981;
    --error: #ef4444;
    --warning: #f59e0b;
    --user-message: #4f46e5;
    --ai-message: #f3f4f6;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
}

/* Dark theme variables */
[data-theme="dark"] {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --secondary-color: #9ca3af;
    --background: #111827;
    --surface: #1f2937;
    --text-primary: #f9fafb;
    --text-secondary: #9ca3af;
    --border: #374151;
    --success: #34d399;
    --error: #f87171;
    --warning: #fbbf24;
    --user-message: #6366f1;
    --ai-message: #374151;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.3), 0 1px 2px -1px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -4px rgba(0, 0, 0, 0.5);
}

/* Auto-detect system preference if no manual choice */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme]) {
        --primary-color: #6366f1;
        --primary-hover: #4f46e5;
        --secondary-color: #9ca3af;
        --background: #111827;
        --surface: #1f2937;
        --text-primary: #f9fafb;
        --text-secondary: #9ca3af;
        --border: #374151;
        --success: #34d399;
        --error: #f87171;
        --warning: #fbbf24;
        --user-message: #6366f1;
        --ai-message: #374151;
        --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.3), 0 1px 2px -1px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -4px rgba(0, 0, 0, 0.5);
    }
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--background);
    color: var(--text-primary);
    line-height: 1.5;
    /* Prevent bounce scrolling on iOS */
    overscroll-behavior: none;
    /* Prevent text size adjustment on orientation change */
    -webkit-text-size-adjust: 100%;
    /* Enable momentum scrolling on iOS */
    -webkit-overflow-scrolling: touch;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    /* Use dvh (dynamic viewport height) for better mobile support */
    height: 100dvh;
    /* Fallback for browsers without dvh support */
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    box-shadow: var(--shadow-lg);
    /* Prevent pull-to-refresh on mobile */
    overscroll-behavior-y: contain;
}

/* Header */
.chat-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 1rem 1.5rem;
    box-shadow: var(--shadow);
    z-index: 10;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-header h1 {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary);
}

.settings-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 0.5rem;
    color: var(--text-secondary);
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.settings-btn:hover {
    background: var(--background);
    color: var(--primary-color);
}

.theme-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 0.5rem;
    color: var(--text-secondary);
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 0.5rem;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.theme-btn:hover {
    background: var(--background);
    color: var(--primary-color);
}

.theme-btn svg {
    width: 20px;
    height: 20px;
}

/* Main Chat Area */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 2rem 1.5rem;
    /* Use auto instead of smooth for better mobile performance */
    scroll-behavior: auto;
    /* Enable momentum scrolling on iOS */
    -webkit-overflow-scrolling: touch;
    /* Optimize scrolling performance */
    will-change: scroll-position;
    /* Prevent rubber-band effect */
    overscroll-behavior: contain;
}

.welcome-message {
    text-align: center;
    padding: 3rem 2rem;
    max-width: 600px;
    margin: 0 auto;
}

.welcome-message h2 {
    font-size: 1.875rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.welcome-message p {
    color: var(--text-secondary);
    font-size: 1.125rem;
    margin-bottom: 0.5rem;
}

.setup-notice {
    background: #fef3c7;
    color: #92400e;
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    margin-top: 1.5rem;
    font-size: 0.875rem !important;
}

[data-theme="dark"] .setup-notice {
    background: #78350f;
    color: #fef3c7;
}

.setup-notice.hidden {
    display: none;
}

/* Messages */
.message {
    display: flex;
    margin-bottom: 1.5rem;
    animation: slideIn 0.3s ease-out;
}

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

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

.message-content {
    max-width: 70%;
    padding: 0.875rem 1.125rem;
    border-radius: 1rem;
    word-wrap: break-word;
    white-space: pre-wrap;
    line-height: 1.6;
}

.message-content strong {
    font-weight: 600;
}

.message.user .message-content {
    background: var(--user-message);
    color: white;
    border-bottom-right-radius: 0.25rem;
}

.message.ai .message-content {
    background: var(--ai-message);
    color: var(--text-primary);
    border-bottom-left-radius: 0.25rem;
}

.message-time {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
    padding: 0 0.25rem;
}

/* Message Images */
.message.with-image .message-content {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.message-image {
    max-width: 300px;
    width: 100%;
    height: auto;
    border-radius: 0.75rem;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: all 0.2s;
    display: block;
    touch-action: manipulation;
}

/* Desktop hover effect */
@media (hover: hover) and (pointer: fine) {
    .message-image:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        transform: scale(1.02);
    }
}

/* Mobile active state */
@media (hover: none) {
    .message-image:active {
        opacity: 0.9;
    }
}

/* Loading Indicator */
.loading-message {
    display: flex;
    margin-bottom: 1.5rem;
}

.loading-content {
    background: var(--ai-message);
    padding: 0.875rem 1.125rem;
    border-radius: 1rem;
    border-bottom-left-radius: 0.25rem;
    display: flex;
    gap: 0.5rem;
}

.loading-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-secondary);
    animation: bounce 1.4s infinite ease-in-out;
}

.loading-dot:nth-child(1) {
    animation-delay: -0.32s;
}

.loading-dot:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Error Message */
.error-message {
    background: #fee2e2;
    color: #991b1b;
    padding: 0.875rem 1.125rem;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

[data-theme="dark"] .error-message {
    background: #7f1d1d;
    color: #fecaca;
}

/* Input Container */
.input-container {
    border-top: 1px solid var(--border);
    padding: 1.5rem;
    background: var(--surface);
}

.input-wrapper {
    display: flex;
    gap: 0.75rem;
    align-items: flex-end;
    flex-wrap: nowrap;
}

#messageInput {
    flex: 1;
    padding: 0.875rem 1rem;
    border: 2px solid var(--border);
    border-radius: 0.75rem;
    font-size: 1rem;
    font-family: inherit;
    resize: none;
    max-height: 150px;
    transition: border-color 0.2s;
    /* Prevent zoom on iOS when focusing */
    touch-action: manipulation;
    /* Better mobile rendering */
    -webkit-appearance: none;
}

#messageInput:focus {
    outline: none;
    border-color: var(--primary-color);
}

#messageInput:disabled {
    background: var(--background);
    cursor: not-allowed;
    opacity: 0.6;
}

/* Image Upload Controls */
.image-preview-container {
    display: none;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    background: var(--background);
    border-radius: 0.75rem;
    margin-bottom: 0.75rem;
}

#imagePreview {
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 0.5rem;
    box-shadow: var(--shadow);
    background: var(--surface);
}

.remove-image-btn {
    background: var(--error);
    color: white;
    border: none;
    padding: 0.5rem;
    border-radius: 0.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.remove-image-btn:hover {
    background: #dc2626;
    transform: scale(1.05);
}

.image-btn {
    background: var(--surface);
    color: var(--text-secondary);
    border: 2px solid var(--border);
    padding: 0.875rem;
    border-radius: 0.75rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    min-width: 48px;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.image-btn:hover:not(:disabled) {
    background: var(--background);
    color: var(--primary-color);
    border-color: var(--primary-color);
}

.image-btn:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

.voice-btn {
    background: var(--surface);
    color: var(--text-secondary);
    border: 2px solid var(--border);
    padding: 0.875rem;
    border-radius: 0.75rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    min-width: 48px;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.voice-btn:hover:not(:disabled) {
    background: var(--background);
    color: var(--primary-color);
    border-color: var(--primary-color);
}

.voice-btn:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

.voice-btn.recording {
    background: var(--error);
    color: white;
    border-color: var(--error);
    animation: pulse 1.5s infinite;
}

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

.send-btn {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 0.875rem;
    border-radius: 0.75rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    min-width: 48px;
    /* Prevent double-tap zoom on iOS */
    touch-action: manipulation;
    /* Better tap targets for mobile */
    -webkit-tap-highlight-color: transparent;
}

.send-btn:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: translate3d(0, -1px, 0);
}

.send-btn:disabled {
    background: var(--secondary-color);
    cursor: not-allowed;
    opacity: 0.5;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.2s;
}

.modal.active {
    display: flex;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.modal-content {
    background: var(--surface);
    border-radius: 1rem;
    width: 90%;
    max-width: 500px;
    box-shadow: var(--shadow-lg);
    animation: slideUp 0.3s;
}

@keyframes slideUp {
    from {
        transform: translate3d(0, 20px, 0);
        opacity: 0;
    }
    to {
        transform: translate3d(0, 0, 0);
        opacity: 1;
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

.close-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 0.5rem;
    color: var(--text-secondary);
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.close-btn:hover {
    background: var(--background);
    color: var(--text-primary);
}

.modal-body {
    padding: 1.5rem;
    max-height: 60vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    color: var(--text-primary);
}

.form-group input,
.form-group textarea {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid var(--border);
    border-radius: 0.5rem;
    font-size: 1rem;
    transition: border-color 0.2s;
    touch-action: manipulation;
    -webkit-appearance: none;
}

.form-group input:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary-color);
}

.help-text {
    display: block;
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.status-message {
    margin-top: 1rem;
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    display: none;
}

.status-message.success {
    display: block;
    background: #d1fae5;
    color: #065f46;
}

.status-message.error {
    display: block;
    background: #fee2e2;
    color: #991b1b;
}

[data-theme="dark"] .status-message.success {
    background: #064e3b;
    color: #d1fae5;
}

[data-theme="dark"] .status-message.error {
    background: #7f1d1d;
    color: #fecaca;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    padding: 1.5rem;
    border-top: 1px solid var(--border);
}

.btn {
    padding: 0.625rem 1.25rem;
    border: none;
    border-radius: 0.5rem;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--background);
    color: var(--text-primary);
}

.btn-secondary:hover {
    background: var(--border);
}

.btn-danger {
    background: var(--error);
    color: white;
    width: 100%;
}

.btn-danger:hover {
    background: #dc2626;
}

/* Scrollbar Styling */
.messages-container::-webkit-scrollbar {
    width: 8px;
}

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

.messages-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
    background: var(--secondary-color);
}

/* Responsive Design */
@media (max-width: 768px) {
    .container {
        max-width: 100%;
    }

    .message-content {
        max-width: 85%;
    }

    .chat-header {
        padding: 0.875rem 1rem;
    }

    .chat-header h1 {
        font-size: 1.25rem;
    }

    .welcome-message {
        padding: 2rem 1rem;
    }

    .welcome-message h2 {
        font-size: 1.5rem;
    }

    .modal-content {
        width: 95%;
        margin: 1rem;
    }

    #imagePreview {
        width: 60px;
        height: 60px;
    }

    .message-image {
        max-width: 240px;
    }

    .input-wrapper {
        gap: 0.5rem;
        flex-wrap: nowrap;
    }

    #messageInput {
        min-width: 0;
        flex: 1;
    }

    .input-container {
        padding: 1rem;
    }

    .messages-container {
        padding: 1.5rem 1rem;
    }

    /* Larger touch targets for mobile */
    .send-btn,
    .voice-btn,
    .image-btn {
        min-width: 42px;
        min-height: 42px;
        padding: 0.625rem;
        flex-shrink: 0;
    }

    .settings-btn,
    .theme-btn {
        min-width: 44px;
        min-height: 44px;
    }

    /* Optimize button transitions for mobile */
    .send-btn,
    .voice-btn,
    .image-btn,
    .settings-btn,
    .theme-btn {
        transition: background-color 0.15s ease;
    }

    /* Remove transform on hover for mobile (use active instead) */
    .send-btn:active:not(:disabled) {
        transform: scale(0.95);
        background: var(--primary-hover);
    }

    .image-btn:active:not(:disabled),
    .voice-btn:active:not(:disabled) {
        transform: scale(0.95);
    }
}

/* Optimize hover states for desktop (fine pointer) vs mobile (touch) */
@media (hover: hover) and (pointer: fine) {
    /* Desktop hover effects - keep existing hover styles */
    .settings-btn:hover,
    .theme-btn:hover,
    .close-btn:hover {
        background: var(--background);
    }
    
    .settings-btn:hover,
    .close-btn:hover {
        color: var(--primary-color);
    }
    
    .theme-btn:hover {
        color: var(--primary-color);
    }
    
    .image-btn:hover:not(:disabled),
    .voice-btn:hover:not(:disabled) {
        background: var(--background);
        color: var(--primary-color);
        border-color: var(--primary-color);
    }
    
    .send-btn:hover:not(:disabled) {
        background: var(--primary-hover);
        transform: translate3d(0, -1px, 0);
    }
    
    .btn-primary:hover {
        background: var(--primary-hover);
    }
    
    .btn-secondary:hover {
        background: var(--border);
    }
    
    .btn-danger:hover {
        background: #dc2626;
    }
    
    .remove-image-btn:hover {
        background: #dc2626;
        transform: scale(1.05);
    }
}

/* Mobile/touch device active states */
@media (hover: none) {
    .settings-btn:active,
    .theme-btn:active,
    .close-btn:active {
        background: var(--background);
    }
    
    .settings-btn:active {
        color: var(--primary-color);
    }
    
    .theme-btn:active {
        color: var(--primary-color);
    }
    
    .close-btn:active {
        color: var(--text-primary);
    }
    
    .image-btn:active:not(:disabled),
    .voice-btn:active:not(:disabled) {
        background: var(--background);
        color: var(--primary-color);
        border-color: var(--primary-color);
    }
    
    .send-btn:active:not(:disabled) {
        background: var(--primary-hover);
        transform: scale(0.95);
    }
    
    .btn-primary:active {
        background: var(--primary-hover);
    }
    
    .btn-secondary:active {
        background: var(--border);
    }
    
    .btn-danger:active {
        background: #dc2626;
    }
    
    .remove-image-btn:active {
        background: #dc2626;
        transform: scale(0.95);
    }
}
