/* CSS Variables for consistent theming */
:root {
    --primary-color: #5865F2;
    --primary-hover: #4752C4;
    --primary-light: #7289DA;
    --success-color: #3BA55C;
    --warning-color: #FAA61A;
    --danger-color: #ED4245;
    --bg-primary: #FFFFFF;
    --bg-secondary: #F7F7F8;
    --bg-tertiary: #EFEFEF;
    --sidebar-bg: #1E1F22;
    --sidebar-hover: #2B2D31;
    --text-primary: #0D0D0D;
    --text-secondary: #565869;
    --text-muted: #939393;
    --text-on-dark: #FFFFFF;
    --text-on-dark-muted: #B5BAC1;
    --border-color: #E3E3E3;
    --border-light: #F0F0F0;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 16px rgba(0, 0, 0, 0.08);
    --shadow-xl: 0 12px 24px rgba(0, 0, 0, 0.12);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    
    /* Safe areas for mobile devices */
    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-inset-left: env(safe-area-inset-left, 0px);
    --safe-area-inset-right: env(safe-area-inset-right, 0px);
}

/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    height: 100%;
    overflow: hidden;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Inter', 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    height: 100%;
    overflow: hidden;
    position: fixed;
    width: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    font-feature-settings: 'kern' 1, 'liga' 1;
}

/* Prevent iOS bounce scrolling */
body {
    position: fixed;
    overflow: hidden;
    width: 100%;
    height: 100vh;
    -webkit-overflow-scrolling: touch;
}

/* App Container */
.app-container {
    display: flex;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile browsers */
    width: 100vw;
    overflow: hidden;
    position: relative;
}

/* Mobile keyboard handling */
@supports (height: 100dvh) {
    .app-container {
        height: 100dvh;
    }
}

/* Sidebar Styles */
.sidebar {
    width: 280px;
    background: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform, width;
    position: relative;
    /* Ensure no filters are applied */
    filter: none !important;
    -webkit-filter: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
}

.sidebar.collapsed {
    width: 0;
    overflow: hidden;
}

/* Sidebar toggle button */
.sidebar-toggle {
    position: absolute;
    right: -40px;
    top: 50%;
    transform: translateY(-50%);
    width: 32px;
    height: 64px;
    background: var(--bg-primary);
    border: 1px solid var(--border-light);
    border-left: none;
    border-radius: 0 8px 8px 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 100;
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.sidebar-toggle:hover {
    background: var(--bg-secondary);
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.15);
}

.sidebar-toggle i {
    color: var(--text-secondary);
    transition: transform 0.3s ease;
    font-size: 1.2rem;
}

.sidebar.collapsed .sidebar-toggle i {
    transform: rotate(180deg);
}

.sidebar-header {
    padding: calc(24px + var(--safe-area-inset-top)) 20px 24px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    background: var(--sidebar-bg);
    position: sticky;
    top: 0;
    z-index: 10;
    pointer-events: auto !important;
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--text-on-dark);
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 24px;
    letter-spacing: -0.02em;
}

.logo i {
    font-size: 1.5rem;
    color: var(--primary-light);
}

.new-chat-btn {
    width: 100%;
    padding: 11px 18px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 0.875rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: var(--transition);
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

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

.new-chat-btn:active {
    transform: scale(0.98);
}

/* Sidebar Content */
.sidebar-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px 12px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    pointer-events: auto !important;
}

.section-title {
    color: var(--text-on-dark-muted);
    font-size: 0.6875rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    padding: 0 12px;
    margin-bottom: 12px;
    opacity: 0.7;
}

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

.chat-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
    position: relative;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.chat-item:hover {
    background: var(--sidebar-hover);
}

.chat-item.active {
    background: var(--sidebar-hover);
    border-left: 3px solid var(--primary-color);
    padding-left: 9px;
}

.chat-item-content {
    flex: 1;
    min-width: 0;
}

.chat-item-title {
    color: var(--text-on-dark);
    font-size: 0.875rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    letter-spacing: -0.01em;
}

.chat-item-date {
    color: var(--text-on-dark-muted);
    font-size: 0.6875rem;
    margin-top: 3px;
    opacity: 0.8;
}

.chat-item-delete {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    color: var(--text-on-dark-muted);
    cursor: pointer;
    padding: 8px;
    opacity: 0;
    transition: var(--transition);
}

.chat-item:hover .chat-item-delete {
    opacity: 1;
}

.chat-item-delete:hover {
    color: var(--danger-color);
}

.empty-state {
    color: var(--text-on-dark-muted);
    text-align: center;
    padding: 40px 20px;
    font-size: 0.9rem;
}

/* Sidebar Footer */
.sidebar-footer {
    padding: 20px 20px calc(20px + var(--safe-area-inset-bottom));
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: var(--sidebar-bg);
    pointer-events: auto !important;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    color: var(--text-on-dark-muted);
    text-decoration: none;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    transition: var(--transition);
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.sidebar-link:hover {
    background: var(--sidebar-hover);
    color: var(--text-on-dark);
}

/* Main Chat Container */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    min-width: 0;
    position: relative;
    z-index: 1;
    height: 100%;
    overflow: hidden;
}

/* Ensure proper height calculation on mobile */
@supports (height: 100dvh) {
    .chat-container {
        height: 100dvh;
        max-height: 100dvh;
    }
}

/* Chat Header */
.chat-header {
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-light);
    padding: 14px 24px;
    padding-top: calc(14px + var(--safe-area-inset-top));
    display: flex;
    align-items: center;
    gap: 16px;
    min-height: 56px;
    flex-shrink: 0;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
}

.mobile-menu-btn {
    display: none;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 1.25rem;
    cursor: pointer;
    padding: 8px;
    margin-left: -8px;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.chat-header-content {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.header-logo i {
    color: var(--primary-color);
    font-size: 1.25rem;
}

.icon-btn {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-md);
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.icon-btn:hover {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-color: var(--text-secondary);
}

.icon-btn:active {
    transform: scale(0.95);
}

/* Chat Content Wrapper for centering */
.chat-content-wrapper {
    display: flex;
    flex-direction: column;
    flex: 1;
    width: 100%;
    padding-top: 60px; /* Space for header */
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    height: calc(100vh - 60px);
}

/* During active chat, ensure proper layout */
.chat-content-wrapper:not(.welcome-state) {
    justify-content: flex-start;
}

/* Welcome state - golden ratio positioning */
.chat-content-wrapper.welcome-state {
    justify-content: flex-start;
    align-items: center;
    padding-top: 38.2vh; /* Golden ratio from top */
}

.chat-content-wrapper.welcome-state .chat-messages {
    max-height: none;
    overflow: visible;
    padding: 0;
    flex: 0 0 auto;
    margin-bottom: 40px;
}

/* In welcome state, input is positioned with the content */
.chat-content-wrapper.welcome-state .chat-input-container {
    position: static;
    background: transparent;
    border-top: none;
    box-shadow: none;
    padding: 20px 40px;
    margin-top: 0;
}

/* Chat Messages Area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 32px 24px;
    padding-bottom: 180px; /* More space for fixed input container */
    display: flex;
    flex-direction: column;
    gap: 20px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    scroll-behavior: smooth;
    max-width: 1600px;
    margin: 0 auto;
    width: 90%;
    position: relative;
    min-height: 0; /* Important for flex children */
}

/* Welcome Message */
.welcome-message {
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.5s ease;
    padding: 0 20px;
    margin-bottom: 0;
}

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

.welcome-content {
    text-align: center;
    max-width: 500px;
    margin: 0 auto;
}

.welcome-icon {
    width: 72px;
    height: 72px;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px;
    box-shadow: 0 8px 24px rgba(88, 101, 242, 0.2);
}

.welcome-icon i {
    font-size: 2.25rem;
    color: white;
}

.welcome-content h2 {
    font-size: 1.875rem;
    font-weight: 700;
    margin-bottom: 8px;
    color: var(--text-primary);
    letter-spacing: -0.03em;
}

.welcome-content p {
    font-size: 0.9375rem;
    color: var(--text-secondary);
    line-height: 1.5;
    letter-spacing: -0.01em;
}

/* Messages */
.message {
    display: flex;
    gap: 12px;
    animation: messageSlide 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    max-width: 100%;
    width: 100%;
}

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

.user-message {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
}

.user-message .message-avatar {
    background: linear-gradient(135deg, #5865F2, #6B73F5);
}

.assistant-message .message-avatar {
    background: linear-gradient(135deg, #10A37F, #13B886);
}

.message-avatar i {
    color: white;
    font-size: 0.875rem;
}

.message-content {
    background: var(--bg-secondary);
    padding: 11px 16px;
    border-radius: 16px;
    max-width: 65%;
    word-wrap: break-word;
    overflow-wrap: break-word;
    font-size: 0.9375rem;
    line-height: 1.55;
    letter-spacing: -0.008em;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
    border: 1px solid rgba(0, 0, 0, 0.04);
}

.user-message .message-content {
    background: linear-gradient(135deg, #5865F2, #6570F7);
    color: white;
    margin-left: auto;
    box-shadow: 0 2px 4px rgba(88, 101, 242, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.message-content p {
    margin: 0 0 10px;
    line-height: 1.6;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content h1,
.message-content h2,
.message-content h3 {
    margin: 16px 0 12px;
    font-weight: 600;
}

.message-content h1:first-child,
.message-content h2:first-child,
.message-content h3:first-child {
    margin-top: 0;
}

.message-content ul,
.message-content ol {
    margin: 12px 0;
    padding-left: 24px;
}

.message-content li {
    margin: 6px 0;
}

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

.message-content em {
    font-style: italic;
}

/* Error Message Styles */
.error-message .message-avatar {
    background: var(--warning-color);
}

.error-message .message-content {
    background: #FFF3CD;
    border-left: 4px solid var(--warning-color);
    color: #856404;
}

.retry-btn {
    margin-top: 12px;
    padding: 8px 16px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: var(--transition);
}

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

.retry-btn:active {
    transform: scale(0.98);
}

/* Typing Indicator */
.typing-indicator .message-content {
    padding: 16px;
    background: var(--bg-secondary);
}

.typing-dots {
    display: flex;
    gap: 4px;
}

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

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

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

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

/* Chat Input Container - Fixed at bottom */
.chat-input-container {
    padding: 16px 40px;
    padding-bottom: calc(16px + var(--safe-area-inset-bottom));
    background: var(--bg-primary);
    /* Removed backdrop filter to prevent blur issues */
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 500;
    display: flex;
    justify-content: center;
    width: 100%;
    border-top: 1px solid rgba(0, 0, 0, 0.06);
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.04);
}

/* Mobile-specific input container adjustments */
@supports (-webkit-touch-callout: none) {
    .chat-input-container {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-primary);
        /* Force GPU acceleration for smooth keyboard transitions */
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
    }
}

.chat-form {
    max-width: none;
    width: 85%;
    margin: 0;
}

@media (min-width: 1400px) {
    .chat-form {
        width: 75%;
    }
}

@media (min-width: 1800px) {
    .chat-form {
        width: 70%;
    }
}

.input-wrapper {
    display: flex;
    align-items: center;
    gap: 10px;
    background: white;
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 20px;
    padding: 8px 12px 8px 16px;
    transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

.input-wrapper:focus-within {
    border-color: rgba(88, 101, 242, 0.5);
    background: white;
    box-shadow: 0 0 0 3px rgba(88, 101, 242, 0.06), 0 4px 12px rgba(0, 0, 0, 0.08);
    transform: translateY(-1px);
}

.message-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    font-size: 15px;
    font-family: inherit;
    resize: none;
    min-height: 24px;
    max-height: 100px;
    line-height: 1.45;
    color: var(--text-primary);
    width: 100%;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    display: flex;
    align-items: center;
    letter-spacing: -0.006em;
    padding: 2px 0;
}

.message-input::placeholder {
    color: var(--text-muted);
}

/* Prevent iOS from zooming when focusing input */
@supports (-webkit-touch-callout: none) {
    .message-input {
        font-size: 16px !important;
    }
}

.send-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--primary-color);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    flex-shrink: 0;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.15);
}

.send-btn i {
    font-size: 0.875rem;
    transform: translateX(-1px);
}

.send-btn:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: scale(1.08);
    box-shadow: 0 2px 8px rgba(88, 101, 242, 0.35);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.95);
}

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

/* Mobile Overlay - MUST be behind sidebar */
.mobile-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000; /* Base z-index for overlay */
    opacity: 0;
    transition: opacity 0.3s ease;
}

.mobile-overlay.active {
    display: block;
    opacity: 1;
}

/* Scrollbar Styles */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

.sidebar ::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
}

.sidebar ::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Mobile Responsive Design */
@media (max-width: 768px) {
    /* Use visual viewport for better mobile keyboard handling */
    .app-container {
        height: 100vh;
        height: -webkit-fill-available;
        height: 100dvh;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
    
    .chat-container {
        height: 100%;
        display: flex;
        flex-direction: column;
        position: relative;
        overflow: hidden;
    }
    
    /* Chat content wrapper for mobile */
    .chat-content-wrapper {
        flex: 1;
        display: flex;
        flex-direction: column;
        padding-top: 56px; /* Smaller header on mobile */
        overflow: hidden;
        position: relative;
    }
    
    .chat-content-wrapper.welcome-state {
        padding-top: 25vh; /* Better mobile ergonomics */
        justify-content: flex-start;
    }
    
    /* In welcome state on mobile, input still stays at bottom for consistency */
    .chat-content-wrapper.welcome-state .chat-input-container {
        position: fixed !important;
        bottom: 0;
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
        background: var(--bg-primary);
        border-top: 1px solid var(--border-light);
    }
    
    /* Make messages area flexible to accommodate keyboard */
    .chat-messages {
        flex: 1;
        min-height: 0;
        padding: 12px;
        padding-bottom: 100px; /* Space for fixed input on mobile */
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
        width: 100%;
        overscroll-behavior: contain;
    }
    
    .chat-form {
        max-width: 100%;
        width: 100%;
    }
    
    /* Keep input ALWAYS at bottom on mobile, above keyboard */
    .chat-input-container {
        position: fixed !important;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
        background: var(--bg-primary);
        border-top: 1px solid var(--border-light);
        z-index: 1000;
        margin-top: 0;
        width: 100%;
        /* Ensure it stays above virtual keyboard */
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
    }
    
    /* During active chat, ensure input stays fixed */
    .chat-content-wrapper:not(.welcome-state) .chat-input-container {
        position: fixed !important;
        bottom: 0 !important;
    }
    
    /* Hide sidebar toggle on mobile */
    .sidebar-toggle {
        display: none;
    }
    
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        height: 100vh;
        width: 280px;
        max-width: 85vw;
        background: var(--sidebar-bg);
        z-index: 1001; /* Higher than overlay */
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.3);
        display: flex;
        flex-direction: column;
        overflow-y: auto;
    }
    
    .sidebar.active {
        transform: translateX(0);
        z-index: 1001; /* Keep higher than overlay */
    }
    
    /* Ensure sidebar content is interactive */
    .sidebar button,
    .sidebar a,
    .sidebar .chat-item {
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }
    
    .mobile-menu-btn {
        display: flex;
        z-index: 201; /* Above header */
    }
    
    /* Force sidebar to be on top of everything on mobile */
    body:has(.sidebar.active) .mobile-overlay {
        z-index: 998 !important;
    }
    
    body:has(.sidebar.active) .sidebar {
        z-index: 99999 !important;
    }
    
    .chat-header {
        padding: 12px 16px;
        padding-top: calc(12px + var(--safe-area-inset-top));
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        background: var(--bg-primary);
        z-index: 200;
    }
    
    .message {
        max-width: 100%;
    }
    
    .message-content {
        max-width: calc(100vw - 80px);
    }
    
    .user-message .message-content {
        max-width: calc(100vw - 80px);
    }
    
    /* Adjust input wrapper for mobile */
    .input-wrapper {
        padding: 8px 12px 8px 14px;
        border-radius: 18px;
    }
    
    /* Ensure proper font size to prevent zoom */
    .message-input {
        font-size: 16px !important;
        -webkit-text-size-adjust: 100%;
    }
}

/* Small Mobile Devices */
@media (max-width: 480px) {
    .chat-header {
        padding: 10px 12px;
        padding-top: calc(10px + var(--safe-area-inset-top));
        min-height: 50px;
    }
    
    .header-logo {
        font-size: 1rem;
    }
    
    .header-logo i {
        font-size: 1.125rem;
    }
    
    .chat-messages {
        padding: 12px;
        gap: 16px;
    }
    
    .message {
        gap: 8px;
    }
    
    .message-avatar {
        width: 32px;
        height: 32px;
    }
    
    .message-avatar i {
        font-size: 0.875rem;
    }
    
    .message-content {
        padding: 10px 14px;
        font-size: 0.9rem;
        border-radius: var(--radius-md);
    }
    
    .chat-input-container {
        padding: 10px 12px;
        padding-bottom: calc(10px + var(--safe-area-inset-bottom));
    }
    
    .input-wrapper {
        padding: 10px 12px;
        gap: 10px;
        border-radius: 18px;
    }
    
    .message-input {
        font-size: 16px; /* Prevent zoom on iOS */
        min-height: 20px;
    }
    
    .send-btn {
        width: 32px;
        height: 32px;
    }
    
    .send-btn i {
        font-size: 0.8125rem;
    }
    
    .welcome-icon {
        width: 64px;
        height: 64px;
    }
    
    .welcome-icon i {
        font-size: 1.875rem;
    }
    
    .welcome-content h2 {
        font-size: 1.5rem;
    }
    
    .welcome-content p {
        font-size: 0.9rem;
    }
}

/* Very Small Devices */
@media (max-width: 360px) {
    .header-logo span {
        font-size: 0.9rem;
    }
    
    .chat-messages {
        padding: 10px;
    }
    
    .message-content {
        padding: 8px 12px;
        font-size: 0.875rem;
    }
    
    .chat-input-container {
        padding: 8px 10px;
        padding-bottom: calc(8px + var(--safe-area-inset-bottom));
    }
}

/* Landscape Mode Adjustments */
@media (max-height: 500px) and (orientation: landscape) {
    .chat-header {
        padding: 8px 16px;
        min-height: 44px;
    }
    
    .chat-messages {
        padding: 12px 16px;
    }
    
    .chat-input-container {
        padding: 8px 16px;
    }
    
    .welcome-message {
        padding: 0 16px;
    }
    
    .welcome-icon {
        width: 56px;
        height: 56px;
    }
    
    .welcome-icon i {
        font-size: 1.625rem;
    }
    
    .welcome-content h2 {
        font-size: 1.5rem;
        margin-bottom: 6px;
    }
    
    .welcome-content p {
        font-size: 0.9rem;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    :root {
        --bg-primary: #09090B;
        --bg-secondary: #18181B;
        --bg-tertiary: #27272A;
        --text-primary: #FAFAFA;
        --text-secondary: #A1A1AA;
        --text-muted: #71717A;
        --border-color: #27272A;
        --border-light: #1C1C1F;
    }
    
    .chat-input-container {
        background: rgba(9, 9, 11, 0.95);
    }
    
    .input-wrapper {
        background: #18181B;
        border-color: rgba(255, 255, 255, 0.08);
    }
    
    .input-wrapper:focus-within {
        background: #1C1C1F;
        border-color: rgba(88, 101, 242, 0.4);
    }
    
    .message-content {
        background: var(--bg-secondary);
    }
    
    .input-wrapper {
        background: var(--bg-secondary);
    }
    
    .input-wrapper:focus-within {
        background: var(--bg-primary);
    }
}

/* iOS-specific fixes */
@supports (-webkit-touch-callout: none) {
    /* iOS Safari - Use available height */
    .app-container {
        height: 100vh;
        height: -webkit-fill-available;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
    
    .chat-container {
        height: 100%;
        max-height: 100%;
        display: flex;
        flex-direction: column;
        position: relative;
    }
    
    /* Flexible messages area */
    .chat-messages {
        flex: 1 1 auto;
        min-height: 0;
        padding-bottom: 100px; /* Space for fixed input */
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
        scroll-padding-bottom: 100px;
    }
    
    /* Fixed input that stays above keyboard */
    .chat-input-container {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-primary);
        z-index: 1000;
        /* iOS-specific positioning */
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
    }
    
    /* Fix for iOS keyboard */
    .message-input {
        font-size: 16px !important;
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
        /* Prevent iOS zoom on focus */
        -webkit-text-size-adjust: 100%;
    }
    
    /* Ensure input wrapper doesn't get cut off */
    .input-wrapper {
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
    }
}

/* Android-specific fixes */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
    .message-input {
        font-size: 16px !important;
    }
    
    /* Android keyboard handling */
    .chat-input-container {
        position: sticky;
        bottom: 0;
        z-index: 200;
    }
    
    /* Ensure proper viewport handling */
    .app-container {
        height: 100vh;
        height: 100dvh;
    }
}

/* Additional mobile viewport fixes */
@media (max-width: 768px) and (pointer: coarse) {
    /* Touch devices */
    .chat-container {
        height: 100vh;
        height: 100dvh;
        max-height: 100dvh;
        display: flex;
        flex-direction: column;
    }
    
    .chat-messages {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        /* Add padding to ensure last message is visible */
        padding-bottom: 120px;
    }
    
    .chat-input-container {
        flex-shrink: 0;
        position: sticky;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-primary);
        z-index: 200;
        /* Ensure it's above everything */
        transform: translateZ(1px);
        -webkit-transform: translateZ(1px);
    }
}

/* Viewport meta tag adjustments via CSS */
@viewport {
    width: device-width;
    initial-scale: 1;
    maximum-scale: 1;
    user-scalable: no;
    viewport-fit: cover;
}

/* Fallback for browsers that don't support @viewport */
@-ms-viewport {
    width: device-width;
    initial-scale: 1;
    maximum-scale: 1;
    user-scalable: no;
}

/* Keyboard open state adjustments */
body.keyboard-open .chat-container {
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

body.keyboard-open .chat-messages {
    /* Adjust padding when keyboard is open */
    padding-bottom: 16px;
    scroll-padding-bottom: 16px;
}

body.keyboard-open .chat-input-container {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.08);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Visual viewport API for modern mobile browsers */
@supports (height: 100vh) and (height: 100dvh) {
    body.keyboard-open .chat-input-container {
        /* Use visual viewport to stay above keyboard */
        bottom: 0;
        position: fixed;
    }
}

/* Mobile-specific keyboard adjustments */
@media (max-width: 768px) {
    body.keyboard-open .sidebar {
        display: none;
    }
    
    body.keyboard-open .chat-header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 250;
    }
    
    body.keyboard-open .chat-messages {
        padding-top: 70px; /* Account for fixed header */
        padding-bottom: 10px;
    }
}

/* Visual viewport API support for modern browsers */
@supports (height: 100dvh) {
    .app-container {
        height: 100dvh;
    }
    
    .chat-container {
        height: 100dvh;
    }
    
    @media (max-width: 768px) {
        .chat-input-container {
            /* Use dynamic viewport height to stay above keyboard */
            position: fixed;
            bottom: 0;
            bottom: env(keyboard-inset-height, 0);
        }
    }
}

/* Samsung Internet specific fixes */
@media screen and (max-width: 768px) {
    .chat-input-container {
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
        backface-visibility: hidden;
        -webkit-backface-visibility: hidden;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .input-wrapper {
        border: 2px solid var(--border-color);
    }
    
    .message-content {
        border: 1px solid var(--border-color);
    }
}

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

/* Product Recommendation Cards */
.product-recommendations {
    margin: 20px 0;
    animation: fadeIn 0.5s ease;
    width: 100%;
    max-width: 100%;
}

.product-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
    padding: 0 20px;
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-weight: 500;
}

.product-header i {
    color: var(--primary-color);
    font-size: 1rem;
}

/* Horizontal scrollable container */
.product-cards {
    display: flex;
    gap: 12px;
    padding: 0 20px 10px;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    /* Hide scrollbar but keep functionality */
    scrollbar-width: thin;
    scrollbar-color: var(--border-color) transparent;
}

/* Custom scrollbar for webkit browsers */
.product-cards::-webkit-scrollbar {
    height: 6px;
}

.product-cards::-webkit-scrollbar-track {
    background: var(--bg-secondary);
    border-radius: 3px;
}

.product-cards::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

.product-cards::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Compact product card */
.product-card {
    flex: 0 0 auto;
    width: 200px;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    overflow: hidden;
    text-decoration: none;
    color: inherit;
    transition: all 0.2s ease;
    box-shadow: var(--shadow-sm);
    position: relative;
}

.product-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    border-color: var(--primary-color);
}

.product-image-container {
    position: relative;
    width: 100%;
    height: 150px;
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.product-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
    padding: 12px;
    display: block;
    max-width: 100%;
    max-height: 100%;
}

/* Fallback for broken images */
.product-image-placeholder {
    font-size: 3rem;
    color: var(--text-muted);
    opacity: 0.5;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
}

/* When image container has no valid image */
.product-image-container.no-image {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
}

.product-image-container.no-image::before {
    content: "🛍️";
    font-size: 3rem;
    color: var(--text-muted);
    opacity: 0.4;
}


/* Hide broken images */
.product-image[onerror] {
    display: none;
}

/* Force image container to show placeholder when image fails */
.product-image-container:has(.product-image[style*="display: none"]) {
    display: flex;
    align-items: center;
    justify-content: center;
}

.product-image-container:has(.product-image[style*="display: none"])::before {
    content: "🛍️";
    font-size: 3rem;
    color: var(--text-muted);
    opacity: 0.4;
}

.savings-tag {
    position: absolute;
    top: 8px;
    right: 8px;
    background: var(--danger-color);
    color: white;
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    font-size: 0.7rem;
    font-weight: 600;
    box-shadow: var(--shadow-sm);
}

.product-details {
    padding: 10px;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.product-title {
    font-size: 0.8rem;
    font-weight: 500;
    line-height: 1.3;
    color: var(--text-primary);
    margin: 0;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    min-height: 2.6em;
}

.product-rating {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
}

.stars {
    color: #FFA500;
    display: flex;
    gap: 1px;
}

.stars i {
    font-size: 0.7rem;
}

.review-count {
    color: var(--text-muted);
    font-size: 0.7rem;
}

/* Additional mobile input fixes */
@media (max-width: 768px) and (max-height: 700px) {
    /* Landscape mode or keyboard open */
    .chat-header {
        padding: 8px 12px;
        min-height: 44px;
    }
    
    .chat-input-container {
        padding: 8px 12px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }
    
    .input-wrapper {
        padding: 8px 10px;
    }
    
    .message-input {
        min-height: 20px;
        max-height: 60px;
    }
}

/* Ensure input is never hidden behind browser UI */
@supports (padding: max(0px)) {
    .chat-input-container {
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }
}

/* Fix for mobile browsers with bottom navigation bars */
@media (max-width: 768px) {
    .app-container {
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    
    .chat-container {
        padding-bottom: 0;
    }
    
    /* Ensure input container accounts for safe areas */
    .chat-input-container {
        padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
    }
}

.product-price-container {
    display: flex;
    align-items: baseline;
    gap: 6px;
    margin-top: auto;
}

.product-price {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
}

.original-price {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-decoration: line-through;
}

.product-badges {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: 6px;
}

.prime-badge {
    background: linear-gradient(135deg, #FF9900, #FF6600);
    color: white;
    padding: 1px 6px;
    border-radius: var(--radius-sm);
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
}

.stock-badge {
    background: var(--success-color);
    color: white;
    padding: 1px 6px;
    border-radius: var(--radius-sm);
    font-size: 0.65rem;
    font-weight: 500;
}

/* Mobile optimizations for product cards */
@media (max-width: 768px) {
    .product-header {
        padding: 0 16px;
        font-size: 0.8rem;
    }
    
    .product-cards {
        padding: 0 16px 8px;
        gap: 10px;
    }
    
    .product-card {
        width: 160px;
    }
    
    .product-image-container {
        height: 120px;
    }
    
    .product-image {
        padding: 10px;
    }
    
    .product-details {
        padding: 8px;
        gap: 4px;
    }
    
    .product-title {
        font-size: 0.75rem;
    }
    
    .product-price {
        font-size: 0.9rem;
    }
    
    .savings-tag {
        top: 6px;
        right: 6px;
        font-size: 0.65rem;
        padding: 2px 4px;
    }
}

@media (max-width: 480px) {
    .product-recommendations {
        margin: 12px 0;
    }
    
    .product-header {
        padding: 0 12px;
    }
    
    .product-cards {
        padding: 0 12px 6px;
        gap: 8px;
    }
    
    .product-card {
        width: 140px;
    }
    
    .product-image-container {
        height: 100px;
    }
    
    .product-details {
        padding: 6px;
    }
    
    .product-title {
        font-size: 0.7rem;
        -webkit-line-clamp: 2;
    }
    
    .product-price {
        font-size: 0.85rem;
    }
    
    .product-badges {
        gap: 4px;
    }
    
    .prime-badge,
    .stock-badge {
        font-size: 0.6rem;
        padding: 1px 4px;
    }
}

/* Dark mode support for product cards */
@media (prefers-color-scheme: dark) {
    .product-card {
        background: var(--bg-secondary);
        border-color: var(--border-color);
    }
    
    .product-card:hover {
        border-color: var(--primary-light);
    }
    
    .product-image-container {
        background: var(--bg-tertiary);
    }
}

/* Print styles */
@media print {
    .sidebar,
    .chat-header,
    .chat-input-container,
    .mobile-overlay {
        display: none !important;
    }
    
    .chat-messages {
        padding: 0;
        height: auto;
        overflow: visible;
    }
    
    .message {
        page-break-inside: avoid;
    }
}

/* Inline Product Links (within message text) */
.product-inline-link {
    color: #5865F2;
    text-decoration: none;
    font-weight: 600;
    border-bottom: 1.5px solid rgba(88, 101, 242, 0.25);
    transition: all 0.15s ease;
    padding-bottom: 1px;
}

.product-inline-link:hover {
    color: #4752C4;
    border-bottom-color: #5865F2;
    background: rgba(88, 101, 242, 0.06);
    padding: 0 3px 1px 3px;
    border-radius: 4px;
}

/* In user messages (white text on gradient background) */
.user-message .product-inline-link {
    color: white;
    border-bottom-color: rgba(255, 255, 255, 0.5);
    font-weight: 600;
}

.user-message .product-inline-link:hover {
    background: rgba(255, 255, 255, 0.2);
    border-bottom-color: white;
}

/* Dark mode support for inline product links */
@media (prefers-color-scheme: dark) {
    .product-inline-link {
        color: #8b9aff;
        border-bottom-color: rgba(139, 154, 255, 0.3);
    }
    
    .product-inline-link:hover {
        color: #a5b3ff;
        border-bottom-color: #8b9aff;
        background: rgba(139, 154, 255, 0.1);
    }
}

/* Mobile responsive for inline links */
@media (max-width: 768px) {
    .product-inline-link {
        /* Ensure links are easily tappable on mobile */
        padding: 2px 4px;
        margin: 0 1px;
    }
}