@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

:root {
    /* Color Palette */
    --bg-color: #f3f4f6;
    --sidebar-bg: #ffffff;
    --chat-bg: #f3f4f6;
    --input-bg: rgba(255, 255, 255, 0.8);
    
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --text-tertiary: #9ca3af;
    
    --accent-primary: #4f46e5; /* Indigo 600 */
    --accent-secondary: #7c3aed; /* Violet 600 */
    --accent-gradient: linear-gradient(135deg, #4f46e5, #7c3aed);
    
    --user-msg-bg: #4f46e5;
    --user-msg-text: #ffffff;
    --ai-msg-bg: #ffffff;
    --ai-msg-text: #374151;
    
    --border-light: #e5e7eb;
    --border-focus: #c7d2fe;
    
    /* Shadows */
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --shadow-input: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    
    /* Transitions */
    --transition-fast: 0.15s ease;
    --transition-normal: 0.3s ease;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100vh; /* Fallback */
    height: 100dvh; /* Dynamic viewport height for mobile browsers */
    overflow: hidden;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    caret-color: transparent; /* Hide caret globally to prevent blinking cursor on text click */
}

input, textarea, [contenteditable] {
    caret-color: auto; /* Restore caret for input fields */
}

/* Layout */
.app-container {
    display: flex;
    height: 100%;
    position: relative;
}

/* Sidebar */
.sidebar {
    width: 280px;
    background-color: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    padding: 1.5rem 1rem;
    border-right: 1px solid var(--border-light);
    transition: transform var(--transition-normal);
    z-index: 50;
}

.sidebar-header {
    margin-bottom: 2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 0.5rem;
}

.sidebar-header h2 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.025em;
}

.sidebar-header h2 i {
    -webkit-text-fill-color: initial;
    color: var(--accent-primary);
}

.close-sidebar-btn {
    display: none;
    cursor: pointer;
    padding: 8px;
    color: var(--text-secondary);
    border-radius: 8px;
    transition: background-color var(--transition-fast);
}

.close-sidebar-btn:hover {
    background-color: #f3f4f6;
    color: var(--text-primary);
}

/* Model Selector */
.model-selector-container {
    margin-bottom: 1.5rem;
}

.model-selector-container label {
    display: block;
    margin-bottom: 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-tertiary);
    padding-left: 0.5rem;
}

#model-select {
    width: 100%;
    padding: 0.75rem 1rem;
    background-color: #f9fafb;
    border: 1px solid var(--border-light);
    border-radius: 12px;
    color: var(--text-primary);
    outline: none;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all var(--transition-fast);
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%236b7280'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 1rem center;
    background-size: 1rem;
}

#model-select:hover {
    border-color: #d1d5db;
    background-color: #ffffff;
}

#model-select:focus {
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
    background-color: #ffffff;
}

/* Chat History */
.chat-history-container {
    flex: 1;
    overflow-y: auto;
    margin-bottom: 1.5rem;
    padding-right: 4px;
}

.chat-history-container::-webkit-scrollbar {
    width: 4px;
}

.chat-history-container::-webkit-scrollbar-thumb {
    background-color: #e5e7eb;
    border-radius: 4px;
}

#chat-history-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.chat-history-item {
    padding: 0.75rem 1rem;
    border-radius: 10px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all var(--transition-fast);
    font-size: 0.9rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    border: 1px solid transparent;
}

.chat-item-content {
    display: flex;
    align-items: center;
    gap: 12px;
    overflow: hidden;
    flex: 1;
}

.chat-item-content span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-history-item:hover {
    background-color: #f3f4f6;
    color: var(--text-primary);
}

.chat-history-item.active {
    background-color: #eef2ff;
    color: var(--accent-primary);
    border-color: #e0e7ff;
}

.chat-history-item i {
    font-size: 0.9rem;
    opacity: 0.7;
}

.chat-history-item.active i {
    opacity: 1;
}

.delete-chat-btn {
    display: none;
    color: #ef4444;
    padding: 4px;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
    opacity: 0.7;
}

.delete-chat-btn:hover {
    background-color: #fee2e2;
    opacity: 1;
}

.chat-history-item:hover .delete-chat-btn {
    display: block;
}

/* Sidebar Footer */
.sidebar-footer {
    margin-top: auto;
}

#new-chat-btn {
    width: 100%;
    padding: 0.875rem;
    background: var(--accent-gradient);
    border: none;
    border-radius: 12px;
    color: white;
    cursor: pointer;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-weight: 600;
    font-size: 0.95rem;
    box-shadow: 0 4px 6px -1px rgba(79, 70, 229, 0.2);
}

#new-chat-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 8px -1px rgba(79, 70, 229, 0.3);
}

#new-chat-btn:active {
    transform: translateY(0);
}

/* Main Chat Area */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: var(--chat-bg);
    position: relative;
    overflow: hidden;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 2rem 1rem 180px; /* Bottom padding for input area */
    padding-top: calc(2rem + env(safe-area-inset-top)); /* Handle notch/status bar */
    scroll-behavior: smooth;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
}

/* Welcome Message */
.welcome-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    color: var(--text-primary);
    padding: 2rem;
    animation: fadeIn 0.5s ease-out;
    cursor: default; /* Show arrow cursor but allow selection */
}

.welcome-message h1 {
    margin-bottom: 1rem;
    font-size: 3rem;
    font-weight: 800;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.03em;
}

.welcome-message h1 .small-text {
    font-size: 1em;
    white-space: nowrap;
    display: inline;
    vertical-align: baseline;
}

.welcome-message p {
    color: var(--text-secondary);
    font-size: 1.125rem;
    max-width: 500px;
    line-height: 1.6;
}

/* Messages */
.message {
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    display: flex;
    animation: slideUp 0.3s ease-out;
}

.message.user {
    justify-content: flex-end;
}

.message.ai {
    justify-content: flex-start;
}

.message-content {
    max-width: 85%;
    display: flex;
    gap: 1rem;
    align-items: flex-start;
}

.message.user .message-content {
    flex-direction: row-reverse;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 1.25rem;
    box-shadow: var(--shadow-sm);
}

.user-avatar {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    color: white;
}

.ai-avatar {
    background: #ffffff;
    color: var(--accent-primary);
    border: 1px solid var(--border-light);
}

.text {
    padding: 1rem 1.25rem;
    border-radius: 16px;
    font-size: 1rem;
    line-height: 1.7;
    box-shadow: var(--shadow-sm);
    position: relative;
    overflow-wrap: break-word;
}

.message.user .text {
    background: var(--accent-gradient);
    color: var(--user-msg-text);
    border-bottom-right-radius: 4px;
    box-shadow: 0 4px 6px -1px rgba(79, 70, 229, 0.2);
}

.message.ai .text {
    background-color: var(--ai-msg-bg);
    color: var(--ai-msg-text);
    border: 1px solid var(--border-light);
    border-bottom-left-radius: 4px;
}

/* Markdown Styles */
.text p {
    margin-bottom: 0.75em;
}

.text p:last-child {
    margin-bottom: 0;
}

.text h1, .text h2, .text h3, .text h4 {
    margin-top: 1em;
    margin-bottom: 0.5em;
    font-weight: 700;
    line-height: 1.3;
}

.text ul, .text ol {
    margin-left: 1.5em;
    margin-bottom: 0.75em;
}

.text li {
    margin-bottom: 0.25em;
}

.text pre {
    background-color: #1e293b;
    padding: 1rem;
    border-radius: 12px;
    overflow-x: auto;
    margin: 1rem 0;
    color: #e2e8f0;
    font-size: 0.9em;
    border: 1px solid #334155;
}

.text code {
    font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
    font-size: 0.9em;
}

.text :not(pre) > code {
    background-color: rgba(0, 0, 0, 0.06);
    padding: 0.2em 0.4em;
    border-radius: 6px;
    color: #ef4444;
    font-weight: 500;
}

.message.user .text :not(pre) > code {
    background-color: rgba(255, 255, 255, 0.2);
    color: #ffffff;
}

.text a {
    color: var(--accent-primary);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s;
}

.text a:hover {
    border-bottom-color: currentColor;
}

.message.user .text a {
    color: white;
    text-decoration: underline;
}

/* Image Styles in Chat */
.text img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    margin: 0.5rem 0;
    cursor: pointer;
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-sm);
}

.text img:hover {
    transform: scale(1.02);
    box-shadow: var(--shadow-md);
}

/* Lightbox Styles */
.lightbox {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.lightbox.active {
    display: flex;
    opacity: 1;
}

.lightbox-content {
    max-width: 90%;
    max-height: 90%;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    transform: scale(0.9);
    transition: transform 0.3s ease;
}

.lightbox.active .lightbox-content {
    transform: scale(1);
}

.lightbox-close {
    position: absolute;
    top: 20px;
    right: 30px;
    color: white;
    font-size: 40px;
    font-weight: bold;
    cursor: pointer;
    transition: color 0.2s ease;
    z-index: 1001;
    line-height: 1;
}

.lightbox-close:hover {
    color: var(--accent-primary);
}

/* Input Area */
.input-area {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 0 1rem 2rem;
    background: linear-gradient(to top, var(--bg-color) 60%, transparent);
    z-index: 10;
}

.input-wrapper {
    max-width: 900px;
    margin: 0 auto;
    background-color: var(--input-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 20px;
    box-shadow: var(--shadow-input);
    border: 1px solid rgba(255, 255, 255, 0.5);
    display: flex;
    align-items: flex-end;
    padding: 0.75rem;
    transition: all var(--transition-normal);
    cursor: text; /* Indicate that the whole area is clickable for input */
}

.input-wrapper:focus-within {
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.1), var(--shadow-input);
    background-color: #ffffff;
}

#attach-btn {
    background-color: transparent;
    border: none;
    color: var(--text-tertiary);
    padding: 0.5rem;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    cursor: pointer;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    margin-right: 0.5rem;
}

#attach-btn:hover {
    background-color: #f3f4f6;
    color: var(--text-primary);
}

.textarea-container {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    padding: 0.5rem 0;
}

#user-input {
    width: 100%;
    background-color: transparent;
    border: none;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 1rem;
    resize: none;
    max-height: 200px;
    outline: none;
    padding: 0;
    line-height: 1.5;
    cursor: text;
    z-index: 1;
}

#custom-placeholder {
    position: absolute;
    top: 0.5rem;
    left: 0;
    color: var(--text-tertiary);
    pointer-events: none;
    user-select: none;
    font-size: 1rem;
    line-height: 1.5;
    z-index: 0;
}

#send-btn {
    background: var(--accent-gradient);
    border: none;
    color: white;
    width: 40px;
    height: 40px;
    border-radius: 12px;
    cursor: pointer;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 0.5rem;
    box-shadow: 0 2px 4px rgba(79, 70, 229, 0.2);
}

#send-btn:hover:not(:disabled) {
    transform: scale(1.05);
    box-shadow: 0 4px 6px rgba(79, 70, 229, 0.3);
}

#send-btn:disabled {
    background: #e5e7eb;
    color: #9ca3af;
    cursor: not-allowed;
    box-shadow: none;
}

.file-preview-container {
    max-width: 900px;
    margin: 0 auto 0.5rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0 0.5rem;
}

.file-preview-item {
    background-color: white;
    border: 1px solid var(--border-light);
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    box-shadow: var(--shadow-sm);
    color: var(--text-secondary);
}

.file-preview-item img {
    width: 24px;
    height: 24px;
    object-fit: cover;
    border-radius: 4px;
}

.footer-text {
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-tertiary);
    margin-top: 0.75rem;
    font-weight: 500;
}

/* Mobile Menu */
.mobile-menu-btn {
    display: none;
    position: absolute;
    top: 1rem;
    left: 1rem;
    z-index: 100;
    padding: 0.5rem;
    cursor: pointer;
    color: var(--text-primary);
    background-color: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border-light);
    border-radius: 10px;
    box-shadow: var(--shadow-sm);
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    z-index: 40;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.sidebar-overlay.open {
    display: block;
    opacity: 1;
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 0;
}

.typing-indicator span {
    display: inline-block;
    width: 6px;
    height: 6px;
    background-color: var(--text-tertiary);
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes typing {
    0%, 80%, 100% { transform: scale(0); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        transform: translateX(-100%);
        box-shadow: var(--shadow-lg);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .mobile-menu-btn {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .close-sidebar-btn {
        display: block;
    }
    
    .chat-messages {
        padding: 4rem 1rem 160px;
    }
    
    .welcome-message h1 {
        font-size: 2rem;
    }
    
    .message {
        max-width: 100%;
    }
    
    .message-content {
        max-width: 95%;
    }
    
    .input-area {
        padding: 0 0.5rem 1rem;
    }
    
    .input-wrapper {
        padding: 0.5rem;
    }
}
