/* 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: var(--chatraw-sidebar-width, 280px);
    min-width: var(--chatraw-sidebar-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-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;
}

.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 {
    padding: 20px;
    border-top: 1px solid var(--border-color);
}

.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-actions-plugin-slot {
    display: contents;
}

.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 {
    position: relative;
    padding: 20px 24px 40px;
    background: linear-gradient(to top, var(--bg-primary) 80%, transparent);
    z-index: 5;
}

/* 一键回到底部按钮：悬浮在输入框正上方居中 */
.scroll-bottom-btn {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-bottom: 12px;
    width: 36px;
    height: 36px;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    background: var(--bg-primary);
    color: var(--text-primary);
    box-shadow: var(--shadow-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    cursor: pointer;
    z-index: 7;
    transition: var(--transition);
}

.scroll-bottom-btn:hover {
    transform: translateX(-50%) scale(1.08);
}

.input-container-inner {
    max-width: 800px;
    margin: 0 auto;
    position: relative;
}

.input-toolbar {
    position: absolute;
    bottom: 16px;
    left: 16px;
    display: flex;
    gap: 8px;
    z-index: 6;
}

.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: calc(100% + 8px);
    left: 0;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 16px;
    min-width: 320px;
    max-width: calc(100vw - 48px);
    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 { width: 100%; }

/* 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; }

/* Plugin loading text */
.plugin-loading-text {
    padding: 20px;
    text-align: center;
    color: var(--text-muted);
}

/* 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);
}

/* Toggle Switch - compact size (36x20) for plugin-style consistency */
.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-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), (max-height: 500px) and (max-width: 900px) and (pointer: coarse) {
    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;
        overflow: hidden;
        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;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        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;
        overflow-y: auto;
        overscroll-behavior: contain;
        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 {
        flex-shrink: 0;
        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 {
        bottom: calc(100% + 8px);
        left: 0;
        right: 0;
        width: auto;
        min-width: 0;
        max-width: calc(100vw - 24px);
    }
    
    .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: min(100%, 100dvh);
        max-height: 100dvh;
        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 and keyboard-height fixes for phones */
@media (max-height: 500px) and (max-width: 900px) and (pointer: coarse) {
    .welcome {
        padding: 12px 16px;
    }

    .welcome-logo {
        margin-bottom: 8px;
    }
    
    .welcome-logo-img {
        width: 40px;
        height: 40px;
    }
    
    .welcome h1 {
        font-size: 1.3rem;
        margin-bottom: 4px;
    }
    
    .welcome-subtitle {
        margin-bottom: 12px;
    }
    
    .welcome-features {
        grid-template-columns: 1fr;
        gap: 8px;
    }
    
    .feature {
        flex-direction: row;
        justify-content: flex-start;
        padding: 10px 12px;
        gap: 8px;
    }

    .input-area {
        padding-top: 8px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }
}

/* 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;
}

/* ============ Plugin System Styles ============ */

/* Plugin Button in Sidebar */
.btn-plugins {
    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);
}

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

.sidebar-plugins-collapsed {
    position: absolute;
    bottom: 56px;
    left: 50%;
    transform: translateX(-50%);
}

/* Plugin Modal - wider for 2-col grid */
.plugin-modal.settings-modal {
    width: 1200px;
}

.plugin-modal .settings-content {
    min-height: 400px;
}

@media (max-width: 768px), (max-height: 500px) and (max-width: 900px) and (pointer: coarse) {
    .plugin-modal.settings-modal {
        width: 100%;
        height: min(100%, 100dvh);
        max-height: 100dvh;
    }

    .plugin-modal .settings-content {
        min-height: 0;
    }
}

/* Plugin Loading */
.plugin-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    gap: 16px;
    color: var(--text-secondary);
}

.plugin-loading .spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border-color);
    border-top-color: var(--text-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Plugin Error */
.plugin-error {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    gap: 16px;
    color: var(--text-muted);
    text-align: center;
}

.plugin-error i {
    opacity: 0.5;
}

.plugin-error .btn-secondary {
    margin-top: 8px;
}

/* Plugin Market Header with Search */
.plugin-market-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 32px;
}

.plugin-market-header .section-header {
    margin-bottom: 0;
}

.plugin-market-search-wrap {
    position: relative;
    flex-shrink: 0;
    width: 200px;
}

.plugin-market-search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 1rem;
    pointer-events: none;
}

.plugin-market-search {
    width: 100%;
    padding: 10px 12px 10px 36px;
}

/* Plugin Grid - 2 columns for market */
.plugin-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
}

/* Plugin List - One per row for installed plugins */
.plugin-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.plugin-list .plugin-card {
    width: 100%;
}

.plugin-list .plugin-desc {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 400px;
}

/* Plugin Card - Compact horizontal layout */
.plugin-card {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 12px 16px;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 12px;
    transition: var(--transition);
}

.plugin-card:hover {
    border-color: var(--text-muted);
}

/* Plugin Card - Market (horizontal row: logo | title+version | buttons) */
.plugin-card-market {
    flex-direction: row;
    align-items: center;
    padding: 10px 14px;
}

.plugin-card-market .plugin-icon-market {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
}

.plugin-card-market-body {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: baseline;
    gap: 8px;
}

.plugin-name-market {
    font-size: 0.92rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.plugin-actions-market {
    flex-shrink: 0;
}

.plugin-card-market .plugin-actions-market .btn-icon-sm {
    width: 32px;
    height: 32px;
    font-size: 0.9rem;
}

/* Plugin info popover */
.plugin-info-wrap {
    position: relative;
}

.plugin-info-popover {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 8px;
    min-width: 240px;
    max-width: 320px;
    padding: 12px 14px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    z-index: 50;
}

.plugin-info-popover-title {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.plugin-info-popover-version {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.plugin-info-popover-desc {
    font-size: 0.85rem;
    color: var(--text-secondary);
    line-height: 1.45;
}

@media (max-width: 640px) {
    .plugin-market-header {
        flex-direction: column;
        gap: 16px;
    }
    
    .plugin-market-search-wrap {
        width: 100%;
    }
    
    .plugin-grid {
        grid-template-columns: 1fr;
    }
}

.plugin-icon {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    object-fit: cover;
    background: var(--bg-hover);
    flex-shrink: 0;
}

.plugin-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.plugin-name {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.plugin-version {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.plugin-desc {
    font-size: 0.8rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.3;
}

.plugin-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.plugin-card.installed .plugin-actions {
    justify-content: flex-end;
}

/* Plugin card settings/delete icons: same size as sidebar settings icon (.btn-icon 36×36) */
.plugin-card .plugin-actions .btn-icon {
    width: 36px;
    height: 36px;
    font-size: 1rem;
}

.btn-sm {
    padding: 6px 16px;
    font-size: 0.85rem;
}

/* Plugin Empty State */
.plugin-empty {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
    grid-column: 1 / -1;
}

/* Plugin Upload Area */
.plugin-upload {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    border: 2px dashed var(--border-color);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
    color: var(--text-secondary);
    gap: 12px;
}

.plugin-upload:hover {
    border-color: var(--text-muted);
    background: var(--bg-hover);
}

.plugin-upload i {
    opacity: 0.5;
}

.plugin-upload-progress {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 20px;
    color: var(--text-secondary);
}

.plugin-upload-progress .spinner {
    width: 20px;
    height: 20px;
    border: 2px solid var(--border-color);
    border-top-color: var(--text-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* Plugin Dev Guide Link */
.plugin-dev-guide {
    margin-top: 24px;
    text-align: center;
}

.plugin-dev-guide a {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--text-muted);
    font-size: 0.85rem;
    text-decoration: none;
    transition: var(--transition);
}

.plugin-dev-guide a:hover {
    color: var(--text-secondary);
}

/* Plugin Settings Modal */
.plugin-settings-modal {
    background: var(--bg-modal);
    border-radius: var(--radius-lg);
    width: 90%;
    max-width: 600px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
}

/* Custom Settings Modal - Larger size for complex UIs */
.plugin-settings-modal.custom-settings-modal {
    max-width: 960px;
    width: 95%;
}

.plugin-settings-header {
    padding: 20px 24px;
    border-bottom: 1px solid var(--border-color);
}

.plugin-settings-header h3 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary);
}

.plugin-settings-content {
    padding: 24px;
    overflow-y: auto;
    flex: 1;
}

.plugin-settings-content.custom-settings-content {
    padding: 0;
    overflow: hidden;  /* Let child handle scrolling */
    min-height: 0;  /* Critical for flex shrinking */
}

/* Plugin Custom Settings Container */
.plugin-custom-settings {
    flex: 1;
    min-height: 0;  /* Critical: allows flex child to shrink below content size */
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.plugin-settings-footer {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 24px;
    border-top: 1px solid var(--border-color);
}

.btn-danger {
    background: var(--error-color);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    cursor: pointer;
    transition: var(--transition);
}

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

.plugin-no-settings {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
}

.plugin-proxy-settings {
    margin-top: 24px;
    padding-top: 24px;
    border-top: 1px solid var(--border-color);
}

.plugin-proxy-settings h4 {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 16px;
}

/* ============ Plugin Toolbar Extension Area ============ */

/* Plugin Toolbar Area Container */
.plugin-toolbar-area {
    display: flex;
    align-items: center;
    gap: 4px;
}

.plugin-toolbar-divider {
    width: 1px;
    height: 20px;
    background: var(--border-color);
    margin: 0 4px;
}

/* Plugin Button Active State */
.btn-tool.plugin-btn.active {
    background: var(--text-primary);
    color: var(--bg-primary);
}

.btn-tool.plugin-btn.active:hover {
    background: var(--accent-hover);
    color: var(--bg-primary);
}

/* Plugin Button Loading State */
.btn-tool.plugin-btn.loading {
    pointer-events: none;
    opacity: 0.7;
}

/* Plugin More Dropdown */
.plugin-more-dropdown {
    position: relative;
}

.plugin-more-menu {
    position: absolute;
    bottom: 100%;
    right: 0;
    margin-bottom: 8px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    min-width: 180px;
    max-height: 280px;
    overflow-y: auto;
    z-index: 100;
}

.plugin-more-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    cursor: pointer;
    transition: background 0.15s;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.plugin-more-item:first-child {
    border-radius: var(--radius-md) var(--radius-md) 0 0;
}

.plugin-more-item:last-child {
    border-radius: 0 0 var(--radius-md) var(--radius-md);
}

.plugin-more-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.plugin-more-item.active {
    color: var(--text-primary);
    background: var(--bg-secondary);
}

.plugin-more-item.loading {
    opacity: 0.7;
    pointer-events: none;
}

.plugin-more-item i {
    font-size: 16px;
    width: 20px;
    text-align: center;
    flex-shrink: 0;
}

.plugin-more-item span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Plugin Fullscreen Modal */
.plugin-fullscreen-modal {
    z-index: 1000;
}

.plugin-fullscreen-content {
    width: 100%;
    max-width: 900px;
    max-height: 90vh;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
}

/* Mobile: Plugin Toolbar */
@media (max-width: 768px), (max-height: 500px) and (max-width: 900px) and (pointer: coarse) {
    .plugin-toolbar-area {
        gap: 2px;
    }
    
    .plugin-toolbar-divider {
        margin: 0 2px;
    }
    
    .plugin-fullscreen-content {
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
        border: none;
    }
    
    .plugin-more-menu {
        right: auto;
        left: 50%;
        transform: translateX(-50%);
        min-width: 200px;
    }

    .plugin-settings-modal {
        width: calc(100vw - 24px);
        max-width: calc(100vw - 24px);
        max-height: calc(100dvh - 24px);
    }

    .plugin-settings-content {
        min-height: 0;
        -webkit-overflow-scrolling: touch;
    }
}

/* Tablet: Plugin Toolbar */
@media (min-width: 769px) and (max-width: 1024px) {
    .btn-tool.plugin-btn {
        min-width: 44px;
        min-height: 44px;
    }
}

/* --- 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);
    }
}

/* CrossBridge Function 1: SME loan matching */
.crossbridge-business-nav {
    padding: 12px 12px 8px;
    border-bottom: 1px solid var(--border-color);
}

.crossbridge-business-label {
    padding: 0 8px 6px;
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.08em;
}

.crossbridge-business-link {
    display: flex;
    align-items: center;
    width: 100%;
    gap: 9px;
    padding: 9px 10px;
    color: var(--text-primary);
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    text-align: left;
}

.crossbridge-business-link:hover,
.crossbridge-business-link.active {
    background: var(--bg-primary);
    border-color: var(--border-color);
}

.crossbridge-business-collapsed {
    align-self: center;
    margin: 10px 0 4px;
}

.loan-matching-cta {
    max-width: 560px;
    margin-top: 14px;
    padding: 14px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
}

.loan-matching-cta-title {
    margin-bottom: 4px;
    font-weight: 700;
}

.loan-matching-cta-text {
    margin-bottom: 12px;
    color: var(--text-secondary);
    font-size: 13px;
}

.loan-matching-cta-actions {
    display: flex;
    gap: 14px;
    align-items: center;
}

.loan-workspace {
    flex: 1;
    overflow-y: auto;
    padding: 26px;
    background: var(--bg-secondary);
}

.loan-workspace-header,
.loan-panel-header,
.loan-form-actions,
.loan-profile-actions,
.loan-match-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
}

.loan-workspace-header {
    max-width: 1240px;
    margin: 0 auto 18px;
}

.loan-back-link {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    margin-bottom: 8px;
    color: var(--text-secondary);
    background: transparent;
    border: 0;
    cursor: pointer;
}

.loan-workspace h1 {
    margin-bottom: 5px;
    font-size: 28px;
}

.loan-workspace p {
    color: var(--text-secondary);
}

.loan-demo-identity {
    display: flex;
    flex-direction: column;
    max-width: 390px;
    padding: 11px 13px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    font-size: 12px;
}

.loan-demo-identity span {
    color: var(--text-secondary);
}

.loan-workspace-grid,
.loan-results {
    max-width: 1240px;
    margin: 0 auto;
}

.loan-workspace-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.25fr) minmax(320px, 0.75fr);
    gap: 16px;
}

.loan-panel {
    padding: 18px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-primary);
    box-shadow: var(--shadow-sm);
}

.loan-panel h2 {
    margin-bottom: 3px;
    font-size: 18px;
}

.loan-panel-header p {
    font-size: 13px;
}

.loan-status-tag,
.loan-demo-tag {
    display: inline-flex;
    flex-shrink: 0;
    padding: 3px 8px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    color: var(--text-secondary);
    font-size: 11px;
    line-height: 1.4;
}

.loan-progress {
    margin: 18px 0;
}

.loan-progress-label {
    display: flex;
    justify-content: space-between;
    margin-bottom: 7px;
    font-size: 13px;
}

.loan-progress-track {
    display: block;
    height: 5px;
    overflow: hidden;
    border-radius: var(--radius-full);
    background: var(--bg-tertiary);
}

.loan-progress-track span {
    display: block;
    height: 100%;
    background: var(--text-primary);
    transition: width 180ms ease;
}

.loan-form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 13px;
}

.loan-form-grid label,
.loan-clarify-input {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.loan-form-grid label span {
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 600;
}

.loan-form-grid input,
.loan-form-grid select,
.loan-clarify-input textarea {
    width: 100%;
    padding: 9px 10px;
    color: var(--text-primary);
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font: inherit;
}

.loan-form-grid input:focus,
.loan-form-grid select:focus,
.loan-clarify-input textarea:focus {
    border-color: var(--border-focus);
    outline: none;
}

.loan-form-actions,
.loan-profile-actions,
.loan-match-actions {
    justify-content: flex-start;
    flex-wrap: wrap;
    margin-top: 17px;
}

.loan-btn {
    padding: 8px 12px;
    color: var(--text-primary);
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 600;
}

.loan-btn:hover:not(:disabled) {
    background: var(--bg-hover);
}

.loan-btn-primary {
    color: var(--on-accent);
    background: var(--accent-color);
    border-color: var(--accent-color);
}

.loan-btn-primary:hover:not(:disabled) {
    opacity: 0.86;
    background: var(--accent-color);
}

.loan-btn-danger {
    color: var(--error-color);
}

.loan-btn:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

.loan-agent-question,
.loan-workspace-notice,
.loan-workspace-error,
.loan-workspace-loading {
    margin-top: 17px;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-secondary);
}

.loan-agent-question span,
.loan-missing-list strong {
    display: block;
    margin-bottom: 4px;
    color: var(--text-secondary);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.08em;
}

.loan-agent-question p {
    color: var(--text-primary);
    font-weight: 600;
}

.loan-agent-ready {
    border-color: var(--success-color);
}

.loan-workspace-notice,
.loan-workspace-error {
    color: var(--error-color);
    font-size: 13px;
}

.loan-workspace-loading,
.loan-workspace-error {
    max-width: 1240px;
    margin: 0 auto 16px;
}

.loan-missing-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 16px;
}

.loan-missing-list strong {
    width: 100%;
}

.loan-missing-list span {
    padding: 3px 7px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    color: var(--text-secondary);
    font-size: 12px;
}

.loan-clarify-input {
    margin-top: 17px;
}

.loan-clarify-input .loan-btn {
    align-self: flex-start;
}

.loan-match-actions small,
.loan-profile-actions small {
    color: var(--text-secondary);
}

.loan-results {
    margin-top: 16px;
}

.loan-product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 12px;
    margin-top: 16px;
}

.loan-product-card {
    padding: 14px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-secondary);
}

.loan-product-card h3 {
    margin: 10px 0;
    font-size: 17px;
}

.loan-product-description {
    margin-bottom: 10px;
    color: var(--text-secondary);
    font-size: 13px;
}

.loan-product-card dl div {
    display: grid;
    grid-template-columns: 72px minmax(0, 1fr);
    gap: 7px;
    margin-bottom: 6px;
    font-size: 13px;
}

.loan-product-card dt {
    color: var(--text-secondary);
}

.loan-product-card ul {
    margin: 7px 0 0 18px;
    font-size: 13px;
}

.loan-rm-note {
    margin-top: 10px;
    font-size: 12px;
}

.loan-source-meta {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 12px;
    padding-top: 10px;
    border-top: 1px solid var(--border-color);
    color: var(--text-secondary);
    font-size: 12px;
}

.loan-source-meta a {
    color: var(--accent-color);
    font-weight: 600;
}

@media (max-width: 960px) {
    .loan-workspace-grid {
        grid-template-columns: 1fr;
    }

    .loan-workspace-header {
        align-items: flex-start;
        flex-direction: column;
    }
}

@media (max-width: 640px) {
    .loan-workspace {
        padding: 14px;
    }

    .loan-form-grid {
        grid-template-columns: 1fr;
    }
}

/* ============================================================
   Function 1 — chat-embedded loan matching (inline cards)
   Minimal, mono, restrained — harmonises with ChatRaw.
   ============================================================ */

/* Mode banner */
.loan-mode-banner {
    position: sticky;
    top: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 8px;
    align-self: center;
    margin: 8px auto 0;
    padding: 5px 6px 5px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    background: var(--bg-primary);
    color: var(--text-secondary);
    font-size: 12px;
    line-height: 1.4;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
.loan-mode-dot {
    width: 7px;
    height: 7px;
    border-radius: var(--radius-full);
    background: var(--accent-color);
    animation: loan-pulse 1.6s ease-in-out infinite;
}
@keyframes loan-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
.loan-mode-text { font-weight: 500; color: var(--text-primary); }
.loan-mode-exit {
    margin-left: 2px;
    padding: 3px 12px;
    border: none;
    border-radius: var(--radius-full);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: 12px;
    cursor: pointer;
}
.loan-mode-exit:hover { background: var(--bg-hover); }

/* Clarify card (the AI question + chips) */
.loan-clarify {
    margin-top: 6px;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
}
.loan-clarify-top { margin-bottom: 8px; }
.loan-progress-pill {
    display: inline-flex;
    padding: 2px 10px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    background: var(--bg-primary);
    color: var(--text-secondary);
    font-size: 11px;
    letter-spacing: 0.02em;
}
.loan-clarify-q {
    color: var(--text-primary);
    font-size: var(--text-base);
    font-weight: 500;
    line-height: 1.5;
}
.loan-chip-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
}
.loan-chip {
    padding: 7px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: var(--text-sm);
    cursor: pointer;
    transition: background 140ms ease, color 140ms ease, border-color 140ms ease;
}
.loan-chip:hover:not(:disabled) {
    background: var(--accent-color);
    border-color: var(--accent-color);
    color: var(--on-accent);
}
.loan-chip:disabled { opacity: 0.45; cursor: default; }
.loan-clarify-hint {
    margin-top: 10px;
    color: var(--text-muted);
    font-size: var(--text-xs);
    line-height: 1.5;
}
.loan-clarify-warn {
    margin-top: 8px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
}

/* Inline draft card + inline results container */
.loan-draft-inline,
.loan-results-inline {
    margin-top: 6px;
    padding: 16px 18px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
}
.loan-draft-note {
    display: block;
    margin-top: 10px;
    color: var(--text-muted);
    font-size: var(--text-xs);
}
/* Restored draft snapshot (shown until the live F1 session is resumed on click) */
.loan-draft-restored {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.loan-draft-restored-row {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 10px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    font-size: var(--text-sm);
}
.loan-draft-restored-label { color: var(--text-muted); }
.loan-draft-restored-value { font-weight: 600; text-align: right; }

/* Two-per-row product grid inside the chat column */
.loan-product-grid.loan-inline-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (max-width: 720px) {
    .loan-product-grid.loan-inline-grid {
        grid-template-columns: 1fr;
    }
}

/* F1 anchored clarification panel (sits above the composer, updates in place) */
.loan-ask {
    margin: 0 auto 10px;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
    animation: loan-ask-in 200ms ease;
}
@keyframes loan-ask-in {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}
.loan-ask-top {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
}
.loan-ask-dots { display: inline-flex; gap: 5px; }
.loan-ask-dot {
    width: 7px;
    height: 7px;
    border-radius: var(--radius-full);
    background: var(--bg-tertiary);
    box-shadow: inset 0 0 0 1px var(--border-color);
    transition: background 160ms ease;
}
.loan-ask-dot.done {
    background: var(--accent-color);
    box-shadow: none;
}
.loan-ask-count {
    color: var(--text-secondary);
    font-size: 11px;
    letter-spacing: 0.02em;
}
.loan-ask-q {
    color: var(--text-primary);
    font-size: var(--text-base);
    font-weight: 500;
    line-height: 1.5;
}
.loan-ask-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
}
.loan-ask-hint {
    margin-top: 10px;
    color: var(--text-muted);
    font-size: var(--text-xs);
    line-height: 1.5;
}
.loan-ask-warn {
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
}

/* Two AI modes segmented switch (composer) */
.ai-mode-switch {
    display: inline-flex;
    gap: 2px;
    margin: 0 auto 10px;
    padding: 3px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-full);
    background: var(--bg-tertiary);
}
.ai-mode-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 16px;
    border: none;
    border-radius: var(--radius-full);
    background: transparent;
    color: var(--text-secondary);
    font-size: var(--text-sm);
    font-weight: 500;
    cursor: pointer;
    transition: background 140ms ease, color 140ms ease;
}
.ai-mode-btn i { font-size: 15px; }
.ai-mode-btn:hover:not(.active) { color: var(--text-primary); }
.ai-mode-btn.active {
    background: var(--accent-color);
    color: var(--on-accent);
}

/* Compact bottom composer: preserve the transcript and F1 product-card viewport. */
.input-area {
    padding: 4px 24px 8px;
}

.input-toolbar {
    bottom: 6px;
    left: 9px;
}

.input-wrapper {
    padding: 8px 10px 34px;
}

.input-wrapper textarea {
    min-height: 20px;
    max-height: 180px;
    line-height: 1.4;
}

.btn-send,
.btn-stop {
    right: 6px;
    bottom: 6px;
    width: 28px;
    height: 28px;
    font-size: 15px;
}

.loan-ask {
    margin-bottom: 4px;
    padding: 9px 11px;
}

.loan-ask-top {
    margin-bottom: 4px;
}

.loan-ask-q {
    line-height: 1.35;
}

.loan-ask-chips {
    gap: 6px;
    margin-top: 7px;
}

.ai-mode-switch {
    margin: 0 auto 4px;
    padding: 2px;
}

.ai-mode-btn {
    gap: 4px;
    padding: 3px 10px;
    font-size: var(--text-xs);
}

.ai-mode-btn i {
    font-size: 12px;
}

@media (max-width: 768px) {
    .input-area {
        padding: 4px 12px max(12px, env(safe-area-inset-bottom) + 6px);
    }
}

/* ============================================================
   Function 2 — document preparation right-side workbench
   ============================================================ */

body {
    --chatraw-doc-panel-layout-width: clamp(
        320px,
        var(--chatraw-doc-panel-width, 460px),
        calc(100vw - var(--chatraw-active-sidebar-width, 280px) - 280px)
    );
}

.main-content {
    transition: margin-right 180ms ease;
}

.main-content.doc-panel-open {
    margin-right: var(--chatraw-doc-panel-layout-width);
}

.crossbridge-doc-prep-collapsed {
    margin-top: 0;
}

.loan-doc-prep-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: 14px;
}

.doc-overview-bubble {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 7px;
    max-width: 560px;
    margin-top: 8px;
    padding: 14px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
}

.doc-overview-bubble small,
.doc-prep-muted {
    color: var(--text-secondary);
    font-size: 12px;
    line-height: 1.5;
}

.doc-prep-panel {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 24;
    display: flex;
    width: var(--chatraw-doc-panel-layout-width);
    height: 100dvh;
    flex-direction: column;
    border-left: 1px solid var(--border-color);
    background: var(--bg-secondary);
    box-shadow: -12px 0 32px rgba(0, 0, 0, 0.08);
    transition: width 180ms ease;
}

.layout-resizer {
    position: absolute;
    top: 0;
    z-index: 30;
    width: 9px;
    height: 100%;
    cursor: col-resize;
    touch-action: none;
}

.layout-resizer::before {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 4px;
    width: 1px;
    background: transparent;
    content: '';
    transition: background-color 120ms ease, box-shadow 120ms ease;
}

.layout-resizer:hover::before,
.layout-resizer:focus-visible::before,
.layout-resizing .layout-resizer::before {
    background: var(--text-secondary);
    box-shadow: 0 0 0 1px var(--bg-primary);
}

.sidebar-layout-resizer {
    right: -5px;
}

.doc-panel-layout-resizer {
    left: -5px;
}

body.layout-resizing {
    cursor: col-resize;
    user-select: none;
}

body.layout-resizing .sidebar,
body.layout-resizing .main-content,
body.layout-resizing .doc-prep-panel {
    transition: none;
}

.doc-prep-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding: 18px 18px 14px;
    border-bottom: 1px solid var(--border-color);
}

.doc-prep-header h2 {
    font-size: 17px;
}

.doc-prep-header p {
    margin-top: 5px;
    color: var(--text-secondary);
    font-size: 11px;
    line-height: 1.5;
}

.doc-prep-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 14px;
}

.doc-prep-loading,
.doc-prep-error {
    margin-bottom: 10px;
    padding: 9px 11px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-secondary);
    font-size: 12px;
}

.doc-prep-error {
    color: #b42318;
}

.doc-prep-card {
    margin-bottom: 12px;
    padding: 13px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-primary);
}

.doc-prep-card h3,
.doc-template-section > h3 {
    margin-bottom: 9px;
    color: var(--text-primary);
    font-size: 13px;
}

.doc-scenario-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 9px;
}

.doc-scenario-grid button {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-secondary);
    color: var(--text-primary);
    cursor: pointer;
    text-align: left;
}

.doc-scenario-grid button.recommended {
    border-style: dashed;
}

.doc-scenario-grid button.active {
    border-color: var(--text-primary);
    background: var(--bg-tertiary);
}

.doc-scenario-grid small {
    color: var(--text-secondary);
    font-size: 10px;
}

.doc-prep-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 9px;
    color: var(--text-secondary);
    font-size: 12px;
}

.doc-prep-field input,
.doc-prep-field select,
.doc-prep-field textarea {
    width: 100%;
    padding: 8px 9px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-secondary);
    color: var(--text-primary);
    font: inherit;
}

.doc-prep-field textarea {
    resize: vertical;
}

.doc-overlay-card strong {
    display: block;
    font-size: 13px;
}

.doc-overlay-card p {
    margin-top: 6px;
    color: var(--text-secondary);
    font-size: 12px;
    line-height: 1.55;
}

.doc-overlay-card a {
    display: inline-block;
    margin-top: 8px;
    color: var(--accent-color);
    font-size: 12px;
    font-weight: 600;
}

.doc-check-item {
    display: grid;
    grid-template-columns: 16px 1fr;
    column-gap: 7px;
    align-items: start;
    padding: 5px 0;
    color: var(--text-primary);
    font-size: 12px;
}

.doc-check-item input {
    margin-top: 3px;
}

.doc-check-item small {
    grid-column: 2;
    color: var(--text-muted);
    font-size: 10px;
}

.doc-tier-public_source_hint {
    border-left: 3px solid #3b82f6;
}

.doc-tier-suggested_preparation {
    border-left: 3px solid #f59e0b;
}

.doc-tier-bank_to_confirm,
.doc-tier-product {
    border-left: 3px solid #8b5cf6;
}

.doc-validation {
    border-color: #f79009;
    background: #fffaeb;
    color: #93370d;
}

[data-theme="dark"] .doc-validation {
    background: #241a04;
    color: #fedf89;
}

.doc-validation li {
    margin-left: 18px;
    font-size: 12px;
}

.doc-official-section {
    margin-top: 16px;
}

.doc-form-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin: 10px 0;
}

.doc-form-chip {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    width: 100%;
    padding: 10px 12px;
    text-align: left;
    color: var(--text-primary);
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
}

.doc-form-chip:hover {
    background: var(--bg-hover);
}

.doc-form-chip.active {
    border-color: var(--accent-color);
    box-shadow: inset 0 0 0 1px var(--accent-color);
}

.doc-form-chip-name {
    font-weight: 600;
    font-size: 13px;
}

.doc-form-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    justify-content: flex-end;
}

.doc-badge {
    font-size: 10px;
    line-height: 1.7;
    padding: 0 6px;
    border-radius: 999px;
    border: 1px solid var(--border-color);
    color: var(--text-muted);
    white-space: nowrap;
}

.doc-badge-match {
    color: var(--on-accent);
    background: var(--accent-color);
    border-color: var(--accent-color);
}

.doc-badge-missing {
    color: #92400e;
    background: #fef3c7;
    border-color: #fcd34d;
}

.doc-badge-draft {
    color: #1d4ed8;
    background: #dbeafe;
    border-color: #bfdbfe;
}

.doc-form-cache-missing {
    margin: 8px 0;
    padding: 12px;
    border: 1px solid #fcd34d;
    border-radius: var(--radius-sm);
    background: #fffbeb;
    font-size: 12px;
}

.doc-form-cache-missing a {
    display: inline-block;
    margin-top: 6px;
    font-weight: 600;
}

.doc-viewer-wrap {
    margin-top: 10px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: #f3f4f6;
}

.doc-viewer-frame {
    display: block;
    width: 100%;
    height: 70vh;
    min-height: 420px;
    border: 0;
}

.doc-terms-overlay {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(0, 0, 0, 0.4);
}

.doc-terms-card {
    width: 100%;
    max-width: 420px;
    padding: 18px;
    border-radius: var(--radius-md);
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.doc-terms-card h4 {
    margin: 0 0 8px;
}

.doc-terms-card p {
    margin: 0 0 8px;
    font-size: 13px;
    color: var(--text-muted);
}

.doc-terms-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 14px;
}

.doc-prep-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-primary);
}

@media (max-width: 900px) {
    .main-content.doc-panel-open {
        margin-right: 0;
    }

    .doc-prep-panel {
        width: 100%;
    }

    .layout-resizer {
        display: none !important;
    }
}

/* The old "print the workbench HTML" path is retired: official forms are printed by
   the embedded PDF.js viewer (saveDocument() -> hidden iframe -> print), so the genuine
   bank PDF is what reaches the printer — not a CSS rendering of the panel. */

/* ---- Function 1: candidate selection toggle ----
   "Select" is a SECONDARY outlined button: same height/radius/padding/font as the primary
   "Prepare Documents" so the pair is aligned and balanced, but visually one tier lighter. */
.loan-card-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: stretch;   /* both buttons take the same height */
    margin-top: 12px;
}
/* Neutralize stray margins (e.g. .loan-doc-prep-btn's margin-top:14px) so the row is aligned. */
.loan-card-actions > .loan-btn {
    margin: 0;
}
.loan-btn-select {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    color: var(--text-secondary);
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    transition: color .12s ease, background .12s ease, border-color .12s ease;
}
.loan-btn-select i {
    font-size: 1.05em;
    line-height: 1;
}
.loan-btn-select:hover:not(:disabled) {
    color: var(--text-primary);
    border-color: var(--accent-color);
    background: var(--bg-hover);
}
.loan-btn-select.is-selected {
    color: var(--accent-color);
    border-color: var(--accent-color);
    /* subtle accent wash; falls back to bg-hover where color-mix is unsupported */
    background: var(--bg-hover);
    background: color-mix(in srgb, var(--accent-color) 10%, var(--bg-primary));
    font-weight: 600;
}

/* ---- Function 1: saved-drafts list modal ---- */
.saved-drafts-modal {
    display: flex;
    flex-direction: column;
    width: min(760px, 92vw);
    max-height: 84vh;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md, 12px);
    overflow: hidden;
}
.saved-drafts-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding: 18px 20px;
    border-bottom: 1px solid var(--border-color);
}
.saved-drafts-header h2 {
    margin: 0;
    font-size: 1.1rem;
}
.saved-drafts-header p {
    margin: 4px 0 0;
    font-size: 0.85rem;
    color: var(--text-secondary);
}
.saved-drafts-toolbar {
    padding: 14px 20px 0;
}
.saved-drafts-body {
    padding: 16px 20px 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.saved-drafts-empty {
    color: var(--text-secondary);
    font-size: 0.9rem;
    text-align: center;
    padding: 28px 12px;
}
.saved-drafts-loading {
    display: flex;
    justify-content: center;
    padding: 24px;
}
.saved-draft-card {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-secondary, var(--bg-primary));
}
.saved-draft-main { min-width: 0; flex: 1; }
.saved-draft-main h3 {
    margin: 0 0 8px;
    font-size: 0.98rem;
}
.saved-draft-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    font-size: 0.82rem;
    color: var(--text-secondary);
}
.saved-draft-tag {
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--bg-hover);
    color: var(--text-primary);
}
.saved-draft-products {
    margin-top: 10px;
    font-size: 0.82rem;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
}
.saved-draft-products strong { color: var(--text-secondary); font-weight: 600; }
.saved-draft-none { color: var(--text-secondary); }
.saved-draft-chip {
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--accent-color);
    color: var(--on-accent);
}
.saved-draft-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex-shrink: 0;
}

/* ---- Function 7: personal SME financing dashboard ---- */
.dashboard-modal {
    display: flex;
    flex-direction: column;
    width: min(1080px, 94vw);
    max-height: 88vh;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md, 12px);
    overflow: hidden;
}
.dashboard-header,
.dashboard-toolbar,
.dashboard-section-head,
.dashboard-item-row,
.dashboard-policy-card {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
}
.dashboard-header {
    padding: 18px 20px;
    border-bottom: 1px solid var(--border-color);
}
.dashboard-header h2 {
    margin: 0;
    font-size: 1.15rem;
}
.dashboard-header p {
    margin: 4px 0 0;
    font-size: 0.86rem;
    color: var(--text-secondary);
}
.dashboard-toolbar {
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
}
.dashboard-body {
    overflow-y: auto;
    padding: 18px 20px 22px;
}
.dashboard-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
}
.dashboard-disclaimer,
.dashboard-section {
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
}
.dashboard-disclaimer {
    padding: 12px 14px;
    border-left: 3px solid var(--accent-color);
}
.dashboard-disclaimer strong {
    display: block;
    margin-bottom: 5px;
    font-size: 0.88rem;
    color: var(--text-primary);
}
.dashboard-disclaimer p {
    margin: 0;
    font-size: 0.82rem;
    line-height: 1.5;
    color: var(--text-secondary);
}
.dashboard-section {
    padding: 14px;
}
.dashboard-section-head {
    align-items: baseline;
    margin-bottom: 12px;
}
.dashboard-section-head h3 {
    margin: 0;
    font-size: 0.98rem;
}
.dashboard-section-head span {
    color: var(--text-muted);
    font-size: 0.78rem;
}
.dashboard-summary-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(120px, 1fr));
    gap: 10px;
}
.dashboard-summary-grid article {
    min-width: 0;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-secondary);
}
.dashboard-summary-grid span {
    display: block;
    min-height: 32px;
    font-size: 0.78rem;
    line-height: 1.35;
    color: var(--text-secondary);
}
.dashboard-summary-grid strong {
    display: block;
    margin-top: 8px;
    font-size: 1.35rem;
    line-height: 1;
    color: var(--text-primary);
}
.dashboard-issues {
    display: grid;
    gap: 8px;
}
.dashboard-issues p {
    display: flex;
    gap: 8px;
    margin: 0;
    padding: 9px 10px;
    border-radius: var(--radius-sm);
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-size: 0.84rem;
}
.dashboard-issues strong {
    color: var(--text-primary);
}
.dashboard-card-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}
.dashboard-card-list-compact {
    grid-template-columns: 1fr;
}
.dashboard-item-card {
    min-width: 0;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-secondary);
}
.dashboard-item-card h4 {
    margin: 0 0 8px;
    font-size: 0.94rem;
    line-height: 1.35;
    color: var(--text-primary);
}
.dashboard-item-card a {
    color: inherit;
    text-decoration: none;
}
.dashboard-item-card a:hover {
    text-decoration: underline;
}
.dashboard-item-card p {
    margin: 0 0 8px;
    color: var(--text-secondary);
    font-size: 0.84rem;
    line-height: 1.45;
}
.dashboard-pill {
    flex-shrink: 0;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--bg-hover);
    color: var(--text-secondary);
    font-size: 0.76rem;
    white-space: nowrap;
}
.dashboard-pill.ready {
    color: var(--on-accent);
    background: var(--accent-color);
}
.dashboard-policy-form {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
    margin-bottom: 12px;
}
.dashboard-policy-form input {
    width: 100%;
    min-width: 0;
    padding: 9px 10px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    background: var(--bg-primary);
}
.dashboard-policy-form button {
    justify-content: center;
}
.dashboard-policy-card {
    align-items: center;
}
.dashboard-policy-card > div {
    min-width: 0;
}

@media (max-width: 820px) {
    .dashboard-modal {
        width: 96vw;
        max-height: 90vh;
    }
    .dashboard-header,
    .dashboard-toolbar {
        padding-left: 14px;
        padding-right: 14px;
    }
    .dashboard-body {
        padding: 14px;
    }
    .dashboard-summary-grid,
    .dashboard-card-list,
    .dashboard-policy-form {
        grid-template-columns: 1fr;
    }
    .dashboard-policy-card,
    .dashboard-item-row {
        flex-direction: column;
        align-items: flex-start;
    }
}

/* ===== Function 3 — application progress timeline =====
   Native to ChatRaw's minimal monochrome system: system font, white/grey/black tokens,
   reuses the F2 panel/header/card chrome (.doc-prep-*) so it reads as the same app.
   Status is restrained (ink + grey; ChatRaw's --destructive only for rejected) — no
   rainbow badges. The only F3-local token is a faint theme-aware focus ring. */
:root { --timeline-ring: rgba(0, 0, 0, 0.07); }
[data-theme="dark"] { --timeline-ring: rgba(255, 255, 255, 0.09); }

.timeline-empty { padding: 40px 8px; text-align: center; }

/* status — quiet text + a small dot, single accent only where it carries meaning */
.timeline-status {
    font-size: 12px; font-weight: 500; color: var(--text-secondary);
    white-space: nowrap; display: inline-flex; align-items: center; gap: 6px;
}
.timeline-status::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.timeline-status.tl-in_progress { color: var(--text-primary); }
.timeline-status.tl-completed { color: var(--text-muted); }
.timeline-status.tl-rejected { color: var(--error-color); }
.timeline-status.tl-supplement_required { color: var(--text-secondary); }

/* head card (sits inside a .doc-prep-card): product + id + progress line */
.timeline-head { display: flex; flex-direction: column; gap: 9px; }
.timeline-head-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.timeline-product { margin: 0; font-size: 16px; font-weight: 600; line-height: 1.3; color: var(--text-primary); }
.timeline-sub { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--text-muted); }
.timeline-id, .timeline-step { font-variant-numeric: tabular-nums; letter-spacing: .02em; }
.timeline-progress { height: 3px; border-radius: 999px; background: var(--bg-tertiary); overflow: hidden; }
.timeline-progress-fill { display: block; height: 100%; background: var(--accent-color); border-radius: 999px; transition: width .5s cubic-bezier(.2,.7,.2,1); }
.timeline-progress-fill.tl-rejected { background: var(--error-color); }

.timeline-switcher { margin-bottom: 14px; }

/* ---- stepper ---- */
.timeline-nodes { list-style: none; margin: 0; padding: 0; }
.timeline-node { position: relative; display: grid; grid-template-columns: 16px 1fr; gap: 0 13px; padding: 0 0 18px; }
.timeline-node:last-child { padding-bottom: 0; }
.timeline-node::before { content: ''; position: absolute; left: 8px; top: 17px; bottom: 0; width: 1.5px; background: var(--border-color); transform: translateX(-50%); transition: background .3s ease; }
.timeline-node:last-child::before { display: none; }
/* completed steps fill the connector spine in ink — the journey travelled so far reads at a glance */
.timeline-node.tl-completed::before { background: var(--accent-color); }
.timeline-node-marker {
    position: relative; z-index: 1; margin-top: 3px; width: 16px; height: 16px; border-radius: 50%;
    background: var(--bg-primary); border: 1.5px solid var(--border-color);
    box-shadow: 0 0 0 3px var(--bg-primary);
    display: grid; place-items: center;
    transition: background .3s ease, border-color .3s ease, box-shadow .3s ease;
}
.timeline-node.tl-completed .timeline-node-marker { background: var(--accent-color); border-color: var(--accent-color); }
/* tick is grid-centered in the dot; tiny upward shift optically centers it (its vertex hangs low) */
.timeline-node.tl-completed .timeline-node-marker::after {
    content: ''; width: 5px; height: 2.5px; position: relative; top: -0.5px;
    border-left: 1.5px solid var(--on-accent); border-bottom: 1.5px solid var(--on-accent); transform: rotate(-45deg);
}
.timeline-node.is-current .timeline-node-marker {
    background: var(--bg-primary); border: 2px solid var(--accent-color);
    box-shadow: 0 0 0 3px var(--bg-primary), 0 0 0 4px var(--timeline-ring);
    animation: timeline-pulse 2.4s ease-in-out infinite;
}
.timeline-node.tl-rejected .timeline-node-marker { background: var(--error-color); border-color: var(--error-color); }
.timeline-node.tl-supplement_required .timeline-node-marker { background: var(--bg-primary); border: 2px dashed var(--text-secondary); }
.timeline-node-body { min-width: 0; padding-top: 1px; }
.timeline-node-row { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.timeline-node-title { font-size: 14px; font-weight: 600; color: var(--text-primary); }
.timeline-node.tl-pending .timeline-node-title { color: var(--text-muted); font-weight: 500; }
.timeline-node-state { font-size: 11.5px; color: var(--text-muted); white-space: nowrap; }
.timeline-node.is-current .timeline-node-state { color: var(--text-secondary); }
.timeline-node.tl-rejected .timeline-node-state { color: var(--error-color); }
.timeline-node-note { margin: 6px 0 0; font-size: 13px; line-height: 1.55; color: var(--text-secondary); }
.timeline-node-reminder { margin: 5px 0 0; font-size: 12px; color: var(--text-muted); }

/* ---- chat overview card (matches .doc-prep-card chrome) ---- */
.timeline-card { margin-top: 10px; max-width: 440px; }
.timeline-card-inner {
    border: 1px solid var(--border-color); border-radius: var(--radius-md);
    background: var(--bg-primary); box-shadow: var(--shadow-sm); padding: 15px 16px;
}
.timeline-card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.timeline-card-eyebrow { font-size: 11px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--text-muted); }
.timeline-card-product { margin-top: 7px; font-size: 16px; font-weight: 600; line-height: 1.3; color: var(--text-primary); }
.timeline-card-sub { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 4px 0 11px; font-size: 13px; color: var(--text-secondary); }
.timeline-card-step { font-variant-numeric: tabular-nums; color: var(--text-muted); }
.timeline-card .timeline-progress { margin-bottom: 13px; }
.timeline-card-btn {
    appearance: none; cursor: pointer; width: 100%; font-size: 13px; font-weight: 500;
    color: var(--text-primary); background: var(--bg-secondary);
    border: 1px solid var(--border-color); border-radius: var(--radius-sm);
    padding: 8px 12px; transition: background .15s ease, border-color .15s ease;
}
.timeline-card-btn:hover { background: var(--bg-hover); border-color: var(--text-muted); }

/* ---- submit-blocking notice (ChatRaw-styled, restrained) ---- */
.timeline-blocking {
    margin: 0 14px 10px; padding: 10px 12px; font-size: 13px; color: var(--text-secondary);
    background: var(--bg-secondary); border: 1px solid var(--border-color);
    border-left: 2px solid var(--error-color); border-radius: var(--radius-sm);
}
.timeline-blocking-title { margin: 0 0 6px; font-weight: 600; color: var(--text-primary); }
.timeline-blocking ul { margin: 0; padding-left: 16px; }
.timeline-blocking li { margin: 3px 0; display: flex; align-items: baseline; gap: 8px; justify-content: space-between; }
.timeline-blocking li > span { flex: 1; min-width: 0; }
/* per-item "Locate" → jumps to + red-highlights the field in the PDF viewer */
.timeline-blocking-locate {
    flex: none; appearance: none; cursor: pointer; white-space: nowrap;
    font: inherit; font-size: 12px; font-weight: 500;
    color: var(--error-color); background: transparent;
    border: 1px solid var(--error-color); border-radius: var(--radius-sm);
    padding: 1px 9px; transition: background .15s ease;
}
.timeline-blocking-locate:hover { background: rgba(220, 38, 38, 0.08); }

/* published_documents has no PDF field — flash the overlay checklist card when located */
.doc-overlay-card.doc-overlay-flag {
    animation: doc-overlay-flash 1.2s ease-out 2;
    border-color: var(--error-color);
}
@keyframes doc-overlay-flash {
    0% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.4); }
    100% { box-shadow: 0 0 0 7px rgba(220, 38, 38, 0); }
}
@media (prefers-reduced-motion: reduce) {
    .doc-overlay-card.doc-overlay-flag { animation: none; }
}

/* ---- motion (calm) ---- */
@keyframes timeline-pulse {
    0%, 100% { box-shadow: 0 0 0 3px var(--bg-primary), 0 0 0 4px var(--timeline-ring); }
    50%      { box-shadow: 0 0 0 3px var(--bg-primary), 0 0 0 6px transparent; }
}
@media (prefers-reduced-motion: reduce) {
    .timeline-node-marker, .timeline-progress-fill { transition: none !important; }
    .timeline-node.is-current .timeline-node-marker { animation: none !important; }
}

/* ---------- Function 5: Policy & Compliance Q&A modal ---------- */
.policy-qa-modal {
    width: min(760px, 94vw);
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary, #fff);
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: 14px;
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.18);
    overflow: hidden;
}
.policy-qa-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding: 20px 22px 12px;
    border-bottom: 1px solid var(--border-color, #e5e7eb);
}
.policy-qa-heading h3 { margin: 0; font-size: 17px; font-weight: 650; }
.policy-qa-subtitle { margin: 4px 0 0; font-size: 12.5px; color: var(--text-secondary, #6b7280); }
.policy-qa-input-row { display: flex; gap: 10px; padding: 16px 22px 8px; }
.policy-qa-input {
    flex: 1;
    resize: vertical;
    min-height: 44px;
    padding: 10px 12px;
    font: inherit;
    color: var(--text-primary, #111827);
    background: var(--bg-secondary, #f9fafb);
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: 10px;
}
.policy-qa-ask-btn {
    flex: 0 0 auto;
    align-self: stretch;
    padding: 0 18px;
    font-weight: 600;
    color: #fff;
    background: var(--accent-color, #111827);
    border: none;
    border-radius: 10px;
    cursor: pointer;
    white-space: nowrap;
}
.policy-qa-ask-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.policy-qa-body { padding: 8px 22px 22px; overflow-y: auto; }
.policy-qa-empty, .policy-qa-no-sources { color: var(--text-secondary, #6b7280); font-size: 13.5px; padding: 14px 0; }
.policy-qa-error { color: #b91c1c; font-size: 13.5px; padding: 12px 0; }
.policy-qa-meta { display: flex; align-items: center; gap: 10px; margin: 6px 0 14px; }
.policy-qa-engine { font-size: 11.5px; font-weight: 600; padding: 2px 9px; border-radius: 999px; }
.policy-qa-engine.is-full { color: #065f46; background: #d1fae5; }
.policy-qa-engine.is-fallback { color: #92400e; background: #fef3c7; }
.policy-qa-elapsed { font-size: 11.5px; color: var(--text-secondary, #6b7280); }
.policy-qa-section-title { margin: 18px 0 8px; font-size: 12px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-secondary, #6b7280); }
.policy-qa-answer { font-size: 14.5px; line-height: 1.7; color: var(--text-primary, #111827); }
.policy-qa-answer :is(h1, h2, h3, h4) { font-size: 15px; margin: 14px 0 6px; }
.policy-qa-answer p { margin: 8px 0; }
.policy-qa-answer ul, .policy-qa-answer ol { margin: 8px 0; padding-left: 20px; }
.policy-qa-sources { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 8px; }
.policy-qa-source { font-size: 13.5px; }
.policy-qa-source a { color: var(--accent-color, #2563eb); text-decoration: none; word-break: break-word; }
.policy-qa-source a:hover { text-decoration: underline; }
.policy-qa-tier {
    margin-left: 8px;
    font-size: 11px;
    color: var(--text-secondary, #6b7280);
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: 999px;
    padding: 1px 8px;
}
.policy-qa-disclaimer { margin: 20px 0 0; font-size: 12px; color: var(--text-secondary, #6b7280); border-top: 1px solid var(--border-color, #e5e7eb); padding-top: 12px; }
