/* Grok-style Minimalist Design System */
:root {
    /* HSL Color Tokens (shadcn/ui compatible) */
    --background: 0 0% 100%;
    --foreground: 0 0% 0%;
    --card: 0 0% 97.6%;
    --card-foreground: 0 0% 0%;
    --popover: 0 0% 100%;
    --popover-foreground: 0 0% 0%;
    --muted: 0 0% 94.1%;
    --muted-foreground: 0 0% 40%;
    --accent: 0 0% 95.3%;
    --accent-foreground: 0 0% 0%;
    --destructive: 0 84.2% 60.2%;
    --destructive-foreground: 0 0% 100%;
    --success: 142.1 76.2% 36.3%;
    --success-foreground: 0 0% 100%;
    --border: 0 0% 89.8%;
    --input: 0 0% 89.8%;
    --ring: 0 0% 0%;
    
    /* Light Theme - Clean White (aliased from HSL tokens) */
    --bg-primary: hsl(var(--background));
    --bg-secondary: #f9f9f9;
    --bg-tertiary: hsl(var(--muted));
    --bg-hover: hsl(var(--accent));
    --bg-input: hsl(var(--background));
    --bg-modal: hsl(var(--background));
    
    --text-primary: hsl(var(--foreground));
    --text-secondary: hsl(var(--muted-foreground));
    --text-muted: #6b6b6b;
    
    --border-color: hsl(var(--border));
    --border-focus: hsl(var(--foreground));
    
    --accent-color: hsl(var(--foreground));
    --accent-hover: #333333;
    --on-accent: hsl(var(--background));
    
    --success-color: hsl(var(--success));
    --error-color: hsl(var(--destructive));
    --error-foreground: hsl(var(--destructive-foreground));
    --success-foreground: hsl(var(--success-foreground));
    
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 20px 40px -10px rgba(0, 0, 0, 0.1);
    
    --radius-sm: 6px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-full: 9999px;
    
    /* Spacing Tokens */
    --spacing-1: 4px;
    --spacing-2: 8px;
    --spacing-3: 12px;
    --spacing-4: 16px;
    --spacing-5: 20px;
    --spacing-6: 24px;
    --spacing-8: 32px;
    --spacing-10: 40px;
    --spacing-12: 48px;
    
    /* Typography Tokens */
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.25rem;
    --text-2xl: 1.5rem;
    --text-3xl: 1.875rem;
    
    /* System font stack - no external requests */
    --font-main: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji';
    --font-mono: ui-monospace, 'SF Mono', 'Cascadia Code', 'Roboto Mono', Menlo, Monaco, 'Courier New', monospace;
    
    /* Animation Tokens */
    --ease: cubic-bezier(0.16, 1, 0.3, 1);
    --duration-fast: 150ms;
    --duration-normal: 200ms;
    --duration-slow: 300ms;
    --transition-colors: color var(--duration-fast) var(--ease),
                         background-color var(--duration-fast) var(--ease),
                         border-color var(--duration-fast) var(--ease);
    --transition-transform: transform var(--duration-normal) var(--ease);
    --transition: all var(--duration-normal) var(--ease);
}

[data-theme="dark"] {
    /* Dark Theme HSL Tokens */
    --background: 0 0% 0%;
    --foreground: 0 0% 100%;
    --card: 0 0% 3.9%;
    --card-foreground: 0 0% 100%;
    --popover: 0 0% 3.9%;
    --popover-foreground: 0 0% 100%;
    --muted: 0 0% 8.6%;
    --muted-foreground: 0 0% 62.7%;
    --accent: 0 0% 10.2%;
    --accent-foreground: 0 0% 100%;
    --border: 0 0% 15.7%;
    --input: 0 0% 15.7%;
    --ring: 0 0% 100%;
    
    /* Dark Theme - Pure Black (Grok Style) */
    --bg-primary: hsl(var(--background));
    --bg-secondary: #0a0a0a;
    --bg-tertiary: hsl(var(--muted));
    --bg-hover: hsl(var(--accent));
    --bg-input: #0a0a0a;
    --bg-modal: #0a0a0a;
    
    --text-primary: hsl(var(--foreground));
    --text-secondary: hsl(var(--muted-foreground));
    --text-muted: #737373;
    
    --border-color: hsl(var(--border));
    --border-focus: hsl(var(--foreground));
    
    --accent-color: hsl(var(--foreground));
    --accent-hover: #e0e0e0;
    --on-accent: hsl(var(--background));
    
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 20px 40px -10px rgba(0, 0, 0, 0.7);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
}

body {
    height: 100%;
    height: -webkit-fill-available;
    height: 100dvh;
    overflow: hidden;
    font-family: var(--font-main);
    background: var(--bg-primary);
    color: var(--text-primary);
    display: flex;
    line-height: 1.6;
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
    /* iOS safe area support */
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

[x-cloak] { display: none !important; }

/* Focus visible styles for keyboard navigation */
:focus-visible {
    outline: 2px solid var(--border-focus);
    outline-offset: 2px;
}

button:focus:not(:focus-visible),
input:focus:not(:focus-visible),
textarea:focus:not(:focus-visible),
select:focus:not(:focus-visible),
[role="switch"]:focus:not(:focus-visible) {
    outline: none;
}

/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* Sidebar */
.sidebar {
    width: 280px;
    min-width: 280px;
    height: 100%;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: width 0.3s cubic-bezier(0.16, 1, 0.3, 1), transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    z-index: 10;
    flex-shrink: 0;
    position: relative;
}

.sidebar.collapsed {
    width: 72px;
    min-width: 72px;
}

/* Sidebar Overlay (mobile only) */
.sidebar-overlay {
    display: none;
}

.sidebar-header {
    padding: 20px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: nowrap;
    min-width: 0;
}

.logo-img {
    width: 32px;
    height: 32px;
    min-width: 32px;
    border-radius: var(--radius-sm);
    object-fit: cover;
    background: var(--bg-tertiary);
    flex-shrink: 0;
}

.logo-text {
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-icon {
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-colors);
}

.btn-icon:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn-icon-danger:hover {
    background: rgba(239, 68, 68, 0.1);
    color: var(--error-color);
}

/* New Chat Button */
.btn-new-chat {
    margin: 0 20px 20px;
    padding: 12px;
    border: 1px solid var(--border-color);
    background: var(--bg-primary);
    color: var(--text-primary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    font-size: 0.95rem;
    font-weight: 600;
    transition: var(--transition);
    box-shadow: var(--shadow-sm);
}

.btn-new-chat:hover {
    border-color: var(--border-focus);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-clear-all {
    margin: 10px 20px;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 0.85rem;
    transition: var(--transition);
}

.btn-clear-all:hover {
    border-color: var(--error-color);
    color: var(--error-color);
    background: rgba(239, 68, 68, 0.1);
}

.btn-new-chat:active {
    transform: translateY(0);
}

.btn-new-chat-collapsed {
    margin: 0 auto 20px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: var(--bg-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-primary);
    transition: var(--transition);
}
.btn-new-chat-collapsed:hover {
    border-color: var(--text-primary);
}

/* Chat List */
.chat-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0 12px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    min-height: 0;
}

.chat-list::-webkit-scrollbar { width: 6px; }
.chat-list::-webkit-scrollbar-thumb { background: transparent; border-radius: 6px; }
.chat-list:hover::-webkit-scrollbar-thumb { background: var(--border-color); }

.chat-records-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 20px 6px;
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 600;
}

.records-title {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.project-records-header {
    margin-top: 4px;
}

.btn-inline-icon {
    width: 24px;
    height: 24px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-colors);
}

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

.project-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0 12px 8px;
    margin-bottom: 6px;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.project-list::-webkit-scrollbar { width: 6px; }
.project-list::-webkit-scrollbar-thumb { background: transparent; border-radius: 6px; }
.project-list:hover::-webkit-scrollbar-thumb { background: var(--border-color); }

.project-group {
    margin-bottom: 4px;
}

.project-create-row {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 0 8px;
}

.project-create-input {
    flex: 1;
    min-width: 0;
    height: 30px;
    border: 1px solid var(--border-focus);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    padding: 0 8px;
    font: inherit;
    font-size: 0.84rem;
}

.project-create-input:focus {
    outline: none;
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--text-primary) 12%, transparent);
}

.project-item {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    margin-bottom: 3px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    text-align: left;
    font: inherit;
    transition: var(--transition-colors);
}

.project-item:hover,
.project-item.active {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--border-color);
}

.project-item span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.project-item span:not(.project-count) {
    flex: 1;
}

.project-count {
    flex-shrink: 0;
    min-width: 22px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--bg-tertiary);
    color: var(--text-muted);
    font-size: 0.72rem;
    line-height: 20px;
    text-align: center;
}

.project-chat-list {
    padding-left: 18px;
    margin: 2px 0 8px;
}

.child-chat-item {
    padding: 10px 10px 10px 12px;
    margin-bottom: 3px;
}

.chat-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    margin-bottom: 4px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition-colors);
    color: var(--text-secondary);
    position: relative;
    border: 1px solid transparent;
}

.chat-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.chat-item.active {
    background: var(--bg-hover);
    color: var(--text-primary);
    font-weight: 500;
    border-color: var(--border-color);
}

.chat-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.9rem;
}

.chat-rename-input {
    flex: 1;
    min-width: 0;
    height: 28px;
    border: 1px solid var(--border-focus);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    padding: 0 8px;
    font: inherit;
    font-size: 0.9rem;
}

.chat-rename-input:focus {
    outline: none;
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--text-primary) 12%, transparent);
}

.btn-delete {
    opacity: 0;
    border: none;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
}

.chat-item:hover .btn-delete { opacity: 1; }
.btn-delete:hover { color: var(--error-color); background: rgba(239, 68, 68, 0.1); }

/* Sidebar Footer */
.sidebar-footer {
    margin-top: auto;
    padding: 20px;
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.btn-settings {
    width: 100%;
    padding: 12px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 0.95rem;
    font-weight: 500;
    transition: var(--transition-colors);
}

.btn-settings:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.sidebar-settings-collapsed {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    background: var(--bg-primary);
    position: relative;
    overflow: hidden;
    width: 100%;
}

.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
    position: relative;
    overflow: hidden;
    min-height: 0;
    contain: layout style;
}

/* Welcome Screen */
.welcome {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    text-align: center;
    padding: 56px 40px 24px;
    animation: fadeIn 0.6s ease-out;
    min-height: 0;
    contain: layout style; /* Optimize rendering */
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.welcome-logo {
    margin-bottom: 16px;
}

.welcome-logo-img {
    width: 64px;
    height: 64px;
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
}

.welcome h1 {
    font-size: 2.1rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 6px;
    letter-spacing: -0.03em;
}

.welcome-subtitle {
    color: var(--text-secondary);
    font-size: 1rem;
    margin-bottom: 28px;
    max-width: 600px;
}

.welcome-features {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
    width: 100%;
    max-width: 800px;
}

.feature {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 32px 24px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    transition: var(--transition);
    cursor: default;
}

.feature:hover {
    transform: translateY(-4px);
    border-color: var(--text-primary);
    color: var(--text-primary);
    box-shadow: var(--shadow-md);
}

.feature i {
    font-size: 32px;
    color: var(--text-primary);
}

/* Messages */
.messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 40px 24px 20px;
    scroll-behavior: smooth;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.message {
    display: flex;
    gap: 24px;
    padding: 24px 0;
    max-width: 800px;
    margin: 0 auto;
    animation: slideIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes slideIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.message-avatar {
    width: 36px;
    height: 36px;
    min-width: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.message.assistant .message-avatar {
    background: var(--text-primary);
    color: var(--bg-primary);
    border: none;
}

.message-content {
    flex: 1;
    min-width: 0;
    font-size: 1rem;
    line-height: 1.75;
    color: var(--text-primary);
    overflow-wrap: break-word;
}

/* Markdown Styling */
.message-content p { margin-bottom: 1.2em; }
.message-content p:last-child { margin-bottom: 0; }

.message-content pre {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 20px;
    overflow-x: auto;
    margin: 20px 0;
}

.message-content code {
    font-family: var(--font-mono);
    font-size: 0.9em;
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 4px;
}

.message-content pre code {
    background: transparent;
    padding: 0;
    color: var(--text-primary);
}

/* Markdown Tables */
.message-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    font-size: 0.95em;
}

.message-content table thead {
    background: var(--bg-secondary);
}

.message-content table th,
.message-content table td {
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    color: var(--text-primary);
}

.message-content table th {
    font-weight: 600;
    text-align: left;
    background: var(--bg-secondary);
}

.message-content table tbody tr {
    transition: background 0.15s ease;
}

.message-content table tbody tr:hover {
    background: var(--bg-hover);
}

/* Markdown Lists */
.message-content ul,
.message-content ol {
    margin: 1em 0;
    padding-left: 2em;
}

.message-content li {
    margin-bottom: 0.5em;
}

.message-content li:last-child {
    margin-bottom: 0;
}

/* Markdown Blockquotes */
.message-content blockquote {
    border-left: 3px solid var(--text-muted);
    padding: 12px 16px;
    margin: 1em 0;
    color: var(--text-secondary);
    background: var(--bg-secondary);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

/* Markdown Horizontal Rule */
.message-content hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 2em 0;
}

/* Markdown Links */
.message-content a {
    color: var(--text-primary);
    text-decoration: underline;
    text-underline-offset: 3px;
    text-decoration-color: var(--border-color);
    transition: text-decoration-color 0.2s;
}

.message-content a:hover {
    text-decoration-color: var(--text-primary);
}

/* Markdown Images */
.message-content img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-sm);
    margin: 1em 0;
}

/* Message Actions Bar (copy button etc.) */
.message-actions {
    display: flex;
    gap: 4px;
    margin-top: 8px;
    opacity: 0;
    transition: opacity var(--duration-fast) ease;
}

.message:hover .message-actions {
    opacity: 1;
}

.btn-copy {
    width: 28px;
    height: 28px;
    font-size: 14px;
    color: var(--text-muted);
    border: none;
    background: transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-colors);
}

.btn-copy:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.btn-copy.copied {
    color: var(--success-color);
}

/* Touch screen: always show message actions */
@media (hover: none) {
    .message-actions {
        opacity: 0.6;
    }
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    gap: 6px;
    padding: 6px 0;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: var(--text-secondary);
    border-radius: 50%;
    animation: bounce 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 bounce {
    0%, 80%, 100% { transform: scale(0); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

/* Input Area */
.input-area {
    padding: 20px 24px 40px;
    background: linear-gradient(to top, var(--bg-primary) 80%, transparent);
    z-index: 5;
}

.input-container-inner {
    max-width: 800px;
    margin: 0 auto;
    position: relative;
}

.input-toolbar {
    position: absolute;
    bottom: 16px;
    left: 16px;
    right: 64px;
    display: flex;
    gap: 8px;
    z-index: 6;
    align-items: center;
    overflow-x: auto;
}

.chat-model-select {
    height: 32px;
    max-width: 180px;
    min-width: 128px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    font: inherit;
    font-size: 0.8rem;
    padding: 0 8px;
}

.image-mode-badge {
    height: 24px;
    display: inline-flex;
    align-items: center;
    padding: 0 8px;
    border-radius: var(--radius-sm);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-size: 0.75rem;
    white-space: nowrap;
}

.btn-tool {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition);
    border: none;
    background: transparent;
}

.btn-tool i {
    font-size: 18px;
}

.btn-tool:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.btn-tool.active {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

/* RAG Toggle with Slider */
.rag-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text-muted);
    transition: var(--transition);
    z-index: 10;
    position: relative;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

.rag-toggle:hover {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
}

.rag-toggle.active {
    color: var(--text-primary);
}

.rag-slider {
    display: block;
    width: 28px;
    height: 14px;
    min-width: 28px;
    min-height: 14px;
    background: var(--bg-tertiary);
    border-radius: 7px;
    position: relative;
    transition: background 0.2s ease;
    flex-shrink: 0;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

.rag-toggle.active .rag-slider {
    background: var(--text-primary);
}

.rag-slider-handle {
    display: block;
    position: absolute;
    top: 2px;
    left: 2px;
    width: 10px;
    height: 10px;
    min-width: 10px;
    min-height: 10px;
    background: var(--text-muted);
    border-radius: 50%;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

.rag-toggle.active .rag-slider-handle {
    left: 16px;
    background: var(--bg-primary);
}

/* Thinking Mode Toggle (same style as RAG toggle) */
.thinking-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text-muted);
    transition: var(--transition);
    z-index: 10;
    position: relative;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

.thinking-toggle:hover {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
}

.thinking-toggle.active {
    color: var(--text-primary);
}

.thinking-slider {
    display: block;
    width: 28px;
    height: 14px;
    min-width: 28px;
    min-height: 14px;
    background: var(--bg-tertiary);
    border-radius: 7px;
    position: relative;
    transition: background 0.2s ease;
    flex-shrink: 0;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

.thinking-toggle.active .thinking-slider {
    background: var(--text-primary);
}

.thinking-slider-handle {
    display: block;
    position: absolute;
    top: 2px;
    left: 2px;
    width: 10px;
    height: 10px;
    min-width: 10px;
    min-height: 10px;
    background: var(--text-muted);
    border-radius: 50%;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

.thinking-toggle.active .thinking-slider-handle {
    left: 16px;
    background: var(--bg-primary);
}

.input-wrapper {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 16px 16px 56px 16px; /* Extra bottom padding for toolbar */
    transition: var(--transition);
    box-shadow: var(--shadow-sm);
    position: relative;
}

.input-wrapper:focus-within {
    border-color: var(--border-focus);
    background: var(--bg-primary);
    box-shadow: var(--shadow-md);
}

.input-wrapper textarea {
    width: 100%;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 1rem;
    font-family: inherit;
    resize: none;
    outline: none;
    max-height: 300px;
    line-height: 1.6;
    min-height: 24px;
}

.btn-send {
    position: absolute;
    bottom: 12px;
    right: 12px;
    width: 40px;
    height: 40px;
    border: none;
    background: var(--text-primary);
    color: var(--bg-primary);
    font-size: 20px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.btn-send:hover:not(:disabled) {
    transform: scale(1.05);
}

.btn-send:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    background: var(--bg-tertiary);
    color: var(--text-muted);
}

/* Stop Button */
.btn-stop {
    position: absolute;
    bottom: 12px;
    right: 12px;
    width: 40px;
    height: 40px;
    border: none;
    background: var(--error-color);
    color: white;
    font-size: 20px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.btn-stop:hover {
    background: #dc2626;
    transform: scale(1.05);
}

/* Image Preview */
.uploaded-image-preview {
    position: absolute;
    bottom: 60px;
    left: 0;
    padding: 8px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    animation: fadeIn 0.2s;
}

.uploaded-image-preview img {
    height: 80px;
    border-radius: 4px;
}

/* URL Input Popup */
.url-input-popup {
    position: absolute;
    bottom: 44px;
    left: 0;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 16px;
    min-width: 320px;
    box-shadow: var(--shadow-lg);
    z-index: 100;
    animation: fadeIn 0.15s ease-out;
}

.url-input-header {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 12px;
}

.url-input-popup input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 0.95rem;
    outline: none;
    transition: border-color 0.2s;
}

.url-input-popup input:focus {
    border-color: var(--border-focus);
}

.url-input-popup input::placeholder {
    color: var(--text-muted);
}

.url-input-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 12px;
}

.url-input-actions .btn-cancel {
    padding: 8px 16px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    transition: var(--transition);
}

.url-input-actions .btn-cancel:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.url-input-actions .btn-confirm {
    padding: 8px 16px;
    border: none;
    background: var(--text-primary);
    color: var(--bg-primary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-weight: 500;
    transition: var(--transition);
}

.url-input-actions .btn-confirm:hover:not(:disabled) {
    background: var(--accent-hover);
}

.url-input-actions .btn-confirm:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Parsed URL Preview */
.parsed-url-preview {
    position: absolute;
    bottom: 60px;
    left: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    animation: fadeIn 0.2s;
    max-width: 300px;
}

.parsed-url-preview i {
    flex-shrink: 0;
    font-size: 16px;
    color: var(--text-secondary);
}

.parsed-url-title {
    font-size: 0.85rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-remove-url {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    border: none;
    background: var(--error-color);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    line-height: 1;
    transition: var(--transition);
}

.btn-remove-url:hover {
    background: #dc2626;
}

/* Attached Document Preview */
.attached-doc-preview {
    position: absolute;
    bottom: 60px;
    left: 100px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    animation: fadeIn 0.2s;
    max-width: 250px;
}

.attached-doc-preview i {
    flex-shrink: 0;
    font-size: 16px;
    color: var(--text-secondary);
}

.attached-doc-name {
    font-size: 0.85rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-remove-doc {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    border: none;
    background: var(--error-color);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    line-height: 1;
    transition: var(--transition);
}

.btn-remove-doc:hover {
    background: #dc2626;
}

/* Spinning animation for loading */
@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.spin {
    animation: spin 1s linear infinite;
}

/* --- New Settings Modal (Grok Style) --- */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 40px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.settings-modal {
    width: 900px;
    height: 700px;
    max-height: 90vh;
    background: var(--bg-modal);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    display: flex;
    overflow: hidden;
    border: 1px solid var(--border-color);
    animation: modalScale 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalScale {
    from { opacity: 0; transform: scale(0.96); }
    to { opacity: 1; transform: scale(1); }
}

/* Settings Left Nav */
.settings-nav {
    width: 240px;
    min-width: 240px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    padding: 24px 12px;
    display: flex;
    flex-direction: column;
}

.settings-nav::-webkit-scrollbar {
    display: none;
}

.settings-nav {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

.settings-title {
    font-size: 1.2rem;
    font-weight: 700;
    padding: 0 12px 24px;
    color: var(--text-primary);
}

.nav-item {
    padding: 12px 16px;
    margin-bottom: 4px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 0.95rem;
    transition: var(--transition-colors);
    display: flex;
    align-items: center;
    gap: 12px;
}

.nav-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.nav-item.active {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-weight: 600;
}

.settings-footer {
    margin-top: auto;
    padding: 12px;
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
}

/* Settings Content */
.settings-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.settings-scroll-area {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 40px 48px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.section-header {
    margin-bottom: 32px;
}

.section-title {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.section-desc {
    color: var(--text-secondary);
    font-size: 0.95rem;
}

/* Form Styles */
.form-group {
    margin-bottom: 32px;
}

.form-label {
    display: block;
    font-weight: 600;
    font-size: 0.95rem;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.form-sublabel {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

/* --- Utility Classes (extracted from inline styles) --- */

/* Avatar image inside circular container */
.avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Hidden file input (accessible) */
.file-input-hidden {
    display: none;
}

/* Form group with extra bottom spacing (UI settings sections) */
.form-group-lg {
    margin-bottom: 40px;
}

/* Status indicators */
.status-success { color: var(--success-color); }
.status-error { color: var(--error-color); }

/* Full-width button */
.btn-full-width {
    display: block;
    width: 100%;
    margin-top: 10px;
}

/* Model verification message */
.model-verify-msg {
    margin-top: 10px;
    font-size: 0.85rem;
}

/* Checkbox group for model capabilities */
.checkbox-group {
    display: flex;
    gap: 20px;
    margin-top: 8px;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.95rem;
    cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
}

/* Form sublabel inline helper */
.form-sublabel-inline {
    margin-top: 8px;
}

.form-sublabel-block {
    margin-bottom: 12px;
    display: block;
}

/* Upload tool label (z-index for file input) */
.btn-tool-upload {
    z-index: 10;
    position: relative;
}

/* Image delete overlay button */
.btn-delete-overlay {
    opacity: 1;
    position: absolute;
    top: -10px;
    right: -10px;
    background: var(--error-color);
    color: white;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    cursor: pointer;
    transition: var(--transition);
}

.btn-delete-overlay:hover {
    background: #dc2626;
}

/* Logo settings delete button */
.btn-delete-inline {
    opacity: 1;
    width: 32px;
    height: 32px;
    border: 1px solid var(--border-color);
    flex-shrink: 0;
}

/* Upload row layout */
.upload-row {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: nowrap;
}

/* Logo preview image */
.logo-preview-img {
    width: 48px;
    height: 48px;
    min-width: 48px;
    border-radius: var(--radius-sm);
    object-fit: cover;
    border: 1px solid var(--border-color);
    flex-shrink: 0;
}

/* Button with no extra margin */
.btn-compact {
    margin: 0;
    white-space: nowrap;
    flex-shrink: 0;
}

/* Input that fills remaining space */
.input-flex {
    flex: 1;
    min-width: 0;
}

/* Avatars row layout */
.avatars-row {
    display: flex;
    align-items: center;
    gap: 24px;
    flex-wrap: wrap;
}

.avatar-group {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* Avatar preview container */
.avatar-preview-user {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--bg-tertiary);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.avatar-preview-assistant {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--text-primary);
    color: var(--bg-primary);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.avatar-preview-assistant i {
    font-size: 20px;
}

/* Settings footer link */
.settings-footer-link {
    color: inherit;
    text-decoration: none;
}

.settings-footer-link:hover {
    text-decoration: underline;
}

/* Error icon size */
.icon-lg { font-size: 24px; }
.icon-xl { font-size: 48px; }

/* Minimal Input */
.input-minimal {
    width: 100%;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    padding: 10px 12px;
    font-size: 1rem;
    color: var(--text-primary);
    transition: border-color 0.2s;
    border-radius: var(--radius-sm);
}

.input-minimal:focus {
    outline: none;
    border-color: var(--border-focus);
}

.input-minimal::placeholder {
    color: var(--text-muted);
}

.textarea-minimal {
    min-height: 108px;
    resize: vertical;
    line-height: 1.5;
}

/* Toggle Switch - compact size (36x20) */
.toggle-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
}

.toggle-info { flex: 1; padding-right: 20px; }

.toggle-switch {
    position: relative;
    width: 36px;
    height: 20px;
    background: var(--bg-tertiary);
    border: none;
    border-radius: 99px;
    cursor: pointer;
    transition: background 0.3s;
    flex-shrink: 0;
    padding: 0;
    appearance: none;
    -webkit-appearance: none;
}

.toggle-switch.checked {
    background: var(--text-primary);
}

.toggle-handle {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    background: var(--bg-primary);
    border-radius: 50%;
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.toggle-switch.checked .toggle-handle {
    transform: translateX(16px);
}

/* Range Slider */
input[type=range] {
    width: 100%;
    height: 4px;
    background: var(--bg-tertiary);
    border-radius: 2px;
    outline: none;
    -webkit-appearance: none;
}

input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    background: var(--bg-primary);
    border: 2px solid var(--text-primary);
    border-radius: 50%;
    cursor: pointer;
    transition: transform 0.1s;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

input[type=range]::-webkit-slider-thumb:hover {
    transform: scale(1.1);
}

/* Collapsible Section */
.collapsible-section {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    overflow: hidden;
}

.collapsible-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    cursor: pointer;
    transition: var(--transition);
    user-select: none;
}

.collapsible-header:hover {
    background: var(--bg-hover);
}

.collapsible-title {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-primary);
}

.collapsible-title i {
    color: var(--text-secondary);
}

.collapsible-chevron {
    color: var(--text-secondary);
    transition: transform 0.2s ease;
}

.collapsible-chevron.expanded {
    transform: rotate(180deg);
}

.collapsible-content {
    padding: 0 20px 20px;
    border-top: 1px solid var(--border-color);
}

.collapsible-content .form-sublabel {
    margin-top: 16px;
    margin-bottom: 12px;
}

.system-prompt-textarea {
    width: 100%;
    min-height: 100px;
    padding: 12px 14px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.95rem;
    line-height: 1.6;
    resize: vertical;
    transition: border-color 0.2s;
}

.system-prompt-textarea:focus {
    outline: none;
    border-color: var(--border-focus);
}

.system-prompt-textarea::placeholder {
    color: var(--text-muted);
}

/* Model Cards */
.model-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 24px;
    margin-bottom: 20px;
    transition: var(--transition);
}

.model-card:hover {
    border-color: var(--text-muted);
}

.model-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.model-badge {
    padding: 4px 10px;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    font-size: 0.75rem;
    font-weight: 600;
}

.model-badge.active {
    background: var(--text-primary);
    color: var(--bg-primary);
    border-color: transparent;
}

/* Modal Actions */
.modal-actions-bar {
    padding: 20px 48px;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
    gap: 16px;
    background: var(--bg-modal);
}

.btn-secondary {
    padding: 10px 24px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-weight: 500;
    transition: var(--transition-colors);
}

.btn-secondary:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn-danger {
    padding: 10px 24px;
    border: 1px solid color-mix(in srgb, var(--error-color) 45%, transparent);
    background: transparent;
    color: var(--error-color);
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-weight: 600;
    transition: var(--transition-colors);
}

.btn-danger:hover {
    background: rgba(239, 68, 68, 0.1);
    border-color: var(--error-color);
}

.btn-primary {
    padding: 10px 24px;
    border: none;
    background: var(--text-primary);
    color: var(--bg-primary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-weight: 600;
    transition: var(--transition-colors), var(--transition-transform);
}

.btn-primary:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

/* Theme Grid */
.theme-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

.theme-option {
    padding: 20px;
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    cursor: pointer;
    text-align: center;
    transition: var(--transition);
}

.theme-option.active {
    border-color: var(--text-primary);
    background: var(--bg-secondary);
}

/* File Upload */
.upload-area {
    border: 2px dashed var(--border-color);
    border-radius: var(--radius-md);
    padding: 40px;
    text-align: center;
    cursor: pointer;
    transition: var(--transition);
    color: var(--text-secondary);
    background: var(--bg-secondary);
}

.upload-area:hover {
    border-color: var(--text-primary);
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

/* Toast */
.toast {
    position: fixed;
    bottom: 32px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 24px;
    background: var(--text-primary);
    color: var(--bg-primary);
    border-radius: var(--radius-md);
    font-weight: 500;
    font-size: 0.9rem;
    z-index: 200;
    box-shadow: var(--shadow-lg);
    animation: toastPop 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.toast.error { background: var(--error-color); color: white; }

@keyframes toastPop {
    from { opacity: 0; transform: translate(-50%, 20px) scale(0.9); }
    to { opacity: 1; transform: translate(-50%, 0) scale(1); }
}

/* Mobile Responsive */
@media (max-width: 768px) {
    body {
        min-height: 100vh;
        min-height: -webkit-fill-available;
        min-height: 100dvh;
    }
    
    .sidebar-overlay {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 99;
        opacity: 1;
        transition: opacity 0.3s ease;
    }
    
    .sidebar-overlay[style*="display: none"] {
        opacity: 0;
    }
    
    .sidebar {
        position: fixed;
        left: 0; 
        top: 0;
        bottom: 0;
        height: 100%;
        height: -webkit-fill-available;
        height: 100dvh;
        z-index: 100;
        transform: translateX(0);
        box-shadow: 5px 0 15px rgba(0,0,0,0.2);
        padding-top: env(safe-area-inset-top);
    }
    
    .sidebar.collapsed {
        transform: translateX(-100%);
        width: 280px;
        min-width: 280px;
    }
    
    .main-content {
        width: 100%;
        min-width: 0;
        height: 100%;
        height: -webkit-fill-available;
        height: 100dvh;
    }
    
    .chat-container {
        flex: 1;
        min-height: 0;
        max-width: 100%;
        padding: 0;
        contain: layout style;
    }
    
    .mobile-header {
        display: flex !important;
        align-items: center;
        padding: 12px 16px;
        padding-top: max(12px, env(safe-area-inset-top));
        border-bottom: 1px solid var(--border-color);
        background: var(--bg-primary);
        position: sticky;
        top: 0;
        z-index: 10;
        flex-shrink: 0;
    }
    
    .mobile-title {
        font-weight: 700;
        font-size: 1.1rem;
        margin-left: 12px;
    }

    .welcome {
        padding: 24px 16px 12px;
        min-height: 0;
    }
    
    .welcome-logo {
        margin-bottom: 12px;
    }
    
    .welcome-logo-img {
        width: 52px;
        height: 52px;
        border-radius: 14px;
    }
    
    .welcome h1 {
        font-size: 1.45rem;
        margin-bottom: 6px;
    }
    
    .welcome-subtitle {
        font-size: 0.9rem;
        margin-bottom: 18px;
    }
    
    .welcome-features {
        grid-template-columns: 1fr;
        gap: 10px;
        max-width: 100%;
    }
    
    .feature {
        padding: 16px 14px;
        flex-direction: row;
        justify-content: flex-start;
    }
    
    .feature i {
        font-size: 24px;
    }

    .messages {
        padding: 16px 12px;
        -webkit-overflow-scrolling: touch;
        min-height: 0;
        contain: layout style; /* Prevent CLS */
    }
    
    .message {
        gap: 12px;
        padding: 16px 0;
        max-width: 100%;
    }
    
    .message-avatar {
        width: 32px;
        height: 32px;
        min-width: 32px;
    }
    
    .message-content {
        font-size: 0.95rem;
        overflow-wrap: break-word;
        word-break: break-word;
    }
    
    .message-content pre {
        padding: 12px;
        margin: 12px 0;
        font-size: 0.85rem;
        max-width: calc(100vw - 48px);
    }
    
    /* Table responsive on mobile */
    .message-content table {
        font-size: 0.85rem;
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .message-content table thead th,
    .message-content table tbody td {
        padding: 10px 12px;
        white-space: normal;
        min-width: 80px;
    }
    
    .input-area {
        padding: 12px;
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }
    
    .input-container-inner {
        max-width: 100%;
    }
    
    .input-wrapper {
        padding: 12px 12px 48px 12px;
        border-radius: var(--radius-md);
    }
    
    .input-wrapper textarea {
        font-size: 16px !important; /* Prevent iOS zoom */
    }
    
    .input-toolbar {
        bottom: 12px;
        left: 12px;
        right: 52px;
        overflow-x: auto;
        scrollbar-width: none;
        -ms-overflow-style: none;
        flex-wrap: nowrap;
    }
    
    .input-toolbar::-webkit-scrollbar {
        display: none;
    }
    
    .btn-tool {
        min-width: 44px;
        min-height: 44px;
    }
    
    .btn-send,
    .btn-stop {
        width: 44px;
        height: 44px;
        bottom: 10px;
        right: 10px;
        font-size: 18px;
    }
    
    .uploaded-image-preview {
        bottom: 52px;
    }
    
    .url-input-popup {
        min-width: calc(100vw - 48px);
        left: -12px;
        right: -12px;
    }
    
    .parsed-url-preview {
        bottom: 52px;
        max-width: calc(100vw - 100px);
    }
    
    .attached-doc-preview {
        bottom: 52px;
        left: 0;
        max-width: calc(100vw - 100px);
    }
    
    /* Settings Modal Mobile */
    .modal-overlay {
        padding: 0;
    }
    
    .settings-modal {
        flex-direction: column;
        width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
    }
    
    .settings-nav {
        width: 100%;
        min-width: 100%;
        flex-direction: row;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding: 8px;
        padding-top: max(8px, env(safe-area-inset-top));
        border-bottom: 1px solid var(--border-color);
        border-right: none;
        min-height: auto;
        flex-shrink: 0;
        scrollbar-width: none;
        -ms-overflow-style: none;
        -webkit-mask-image: linear-gradient(to right, transparent, black 16px, black calc(100% - 16px), transparent);
        mask-image: linear-gradient(to right, transparent, black 16px, black calc(100% - 16px), transparent);
    }
    
    .settings-nav::-webkit-scrollbar {
        display: none;
    }
    
    .settings-title {
        display: none;
    }
    
    .settings-footer {
        display: none;
    }
    
    .nav-item {
        white-space: nowrap;
        padding: 8px 12px;
        font-size: 0.9rem;
        flex-shrink: 0;
    }
    
    .settings-content {
        flex: 1;
        min-height: 0;
    }
    
    .settings-scroll-area {
        padding: 20px 16px;
        padding-bottom: max(100px, env(safe-area-inset-bottom) + 80px);
        -webkit-overflow-scrolling: touch;
    }
    
    .section-title {
        font-size: 1.25rem;
    }
    
    .modal-actions-bar {
        padding: 16px;
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }
    
    .model-card {
        padding: 16px;
    }
    
    .theme-grid {
        gap: 12px;
    }
    
    .theme-option {
        padding: 16px;
    }
    
    /* Prevent iOS zoom on inputs */
    .input-minimal, 
    .input-wrapper textarea,
    input[type="text"],
    input[type="password"],
    input[type="number"],
    select {
        font-size: 16px !important;
    }
    
    /* Hide new chat button in collapsed sidebar on mobile */
    .btn-new-chat-collapsed {
        display: none;
    }
    
    /* Toast positioning for mobile */
    .toast {
        bottom: max(24px, env(safe-area-inset-bottom) + 16px);
        left: 16px;
        right: 16px;
        transform: none;
        max-width: calc(100% - 32px);
        text-align: center;
    }
}

/* Default state for mobile header */
.mobile-header {
    display: none;
}

/* Safe Area Support for iPhone X+ */
.input-area {
    padding-bottom: max(40px, env(safe-area-inset-bottom) + 20px);
}

.sidebar-footer {
    padding-bottom: max(20px, env(safe-area-inset-bottom) + 20px);
}

/* Tablet optimization (iPad, etc.) */
@media (min-width: 769px) and (max-width: 1024px) {
    .input-container-inner {
        max-width: 100%;
        padding: 0 12px;
    }
    
    .input-toolbar {
        gap: 4px;
    }
    
    /* Ensure touch-friendly size for toggles */
    .thinking-toggle,
    .rag-toggle {
        min-height: 44px;
        padding: 8px 6px;
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
    }
    
    .thinking-slider,
    .rag-slider {
        display: block !important;
        width: 28px;
        height: 14px;
        min-width: 28px;
        min-height: 14px;
        -webkit-transform: translateZ(0);
    }
    
    .thinking-slider-handle,
    .rag-slider-handle {
        display: block !important;
        width: 10px;
        height: 10px;
        min-width: 10px;
        min-height: 10px;
        -webkit-transform: translateZ(0);
    }
    
    .btn-tool {
        min-width: 44px;
        min-height: 44px;
    }
}

/* Extra small screens (iPhone SE, etc.) */
@media (max-width: 375px) {
    .welcome-logo-img {
        width: 56px;
        height: 56px;
    }
    
    .welcome h1 {
        font-size: 1.4rem;
    }
    
    .welcome-subtitle {
        font-size: 0.85rem;
        margin-bottom: 20px;
    }
    
    .feature {
        padding: 14px 12px;
        gap: 10px;
    }
    
    .message {
        gap: 10px;
    }
    
    .message-avatar {
        width: 28px;
        height: 28px;
        min-width: 28px;
    }
    
    .input-wrapper {
        padding: 10px 10px 44px 10px;
    }
    
    .settings-scroll-area {
        padding: 16px 12px;
    }
}

/* Landscape mode fixes for mobile */
@media (max-height: 500px) and (max-width: 900px) {
    .welcome {
        padding: 16px;
    }
    
    .welcome-logo-img {
        width: 48px;
        height: 48px;
    }
    
    .welcome h1 {
        font-size: 1.4rem;
        margin-bottom: 8px;
    }
    
    .welcome-subtitle {
        margin-bottom: 20px;
    }
    
    .welcome-features {
        grid-template-columns: repeat(3, 1fr);
    }
    
    .feature {
        flex-direction: column;
        padding: 12px 8px;
        gap: 8px;
    }
}

/* Thinking Block */
.thinking-block {
    margin-bottom: 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
    overflow: hidden;
}

.thinking-header {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text-secondary);
    padding: 10px 14px;
    background: var(--bg-hover);
    transition: var(--transition);
    user-select: none;
}

.thinking-header:hover {
    background: var(--border-color);
}

.thinking-header i {
    flex-shrink: 0;
    color: var(--text-muted);
}

.thinking-header .chevron {
    margin-left: auto;
    transition: transform 0.2s ease;
}

.thinking-header .chevron.expanded {
    transform: rotate(180deg);
}

.thinking-content {
    padding: 14px;
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.7;
    border-top: 1px solid var(--border-color);
    background: var(--bg-primary);
    max-height: 400px;
    overflow-y: auto;
}

.thinking-content p {
    margin-bottom: 0.8em;
}

.thinking-content p:last-child {
    margin-bottom: 0;
}

/* RAG References Bubble */
.rag-references {
    margin-top: 12px;
    border-top: 1px solid var(--border-color);
    padding-top: 12px;
}

.rag-references-header {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text-secondary);
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    background: var(--bg-hover);
    transition: var(--transition);
    user-select: none;
}

.rag-references-header:hover {
    background: var(--border-color);
}

.rag-references-header i {
    flex-shrink: 0;
}

.rag-references-header .chevron {
    margin-left: auto;
    transition: transform 0.2s ease;
}

.rag-references-header .chevron.expanded {
    transform: rotate(180deg);
}

.rag-references-list {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.rag-reference-item {
    background: var(--bg-hover);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 12px;
    font-size: 0.85rem;
}

.rag-reference-item .ref-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
}

.rag-reference-item .ref-badge {
    background: var(--text-primary);
    color: var(--bg-primary);
    font-size: 0.7rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 10px;
}

.rag-reference-item .ref-score {
    color: var(--text-muted);
    font-size: 0.75rem;
}

.rag-reference-item .ref-content {
    color: var(--text-secondary);
    line-height: 1.5;
    word-break: break-word;
}

/* --- Touch screen: disable sticky hover effects --- */
@media (hover: none) {
    .feature:hover {
        transform: none;
        border-color: var(--border-color);
        box-shadow: none;
    }
    
    .btn-new-chat:hover {
        transform: none;
        box-shadow: var(--shadow-sm);
    }
    
    .btn-primary:hover {
        opacity: 1;
        transform: none;
    }
    
    .btn-send:hover:not(:disabled) {
        transform: none;
    }
    
    .btn-stop:hover {
        transform: none;
    }
    
    .chat-item:hover .btn-delete {
        opacity: 1;
    }
    
    .model-card:hover {
        border-color: var(--border-color);
    }
}
