:root {
    --bg-primary: #000000;
    --bg-secondary: #000000;
    --bg-sidebar: #000000;
    --bg-card: #0f0f0f;
    --bg-input: #141414;
    --bg-hover: #1c1c1c;
    --text-primary: #ffffff;
    --text-secondary: #c2c2c2;
    --text-muted: #8a8a8a;
    --accent: #ffffff;
    --accent-hover: #e5e5e5;
    --accent-glow: rgba(255, 255, 255, 0.08);
    --on-accent: #000000;
    --model-tag: #c4c4c4;
    --border: #1f1f1f;
    --border-light: #2e2e2e;
    --success: #22c55e;
    --warning: #f59e0b;
    --danger: #ef4444;
    --sidebar-width: 270px;
    --header-height: 56px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    overflow: hidden;
    box-sizing: border-box;
    display: flex;
}

/* ===== Layout ===== */
.app-root {
    display: flex;
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    box-sizing: border-box;
    position: relative;
}

/* ===== Sidebar (Drawer on Mobile, Static on Desktop) ===== */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.72);
    z-index: 900;
    opacity: 0;
    transition: opacity 0.25s ease;
}

.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    height: 100%;
    height: 100dvh;
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    padding: 12px 10px;
    border-right: 1px solid var(--border);
    z-index: 950;
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1), margin-left 0.28s cubic-bezier(0.16, 1, 0.3, 1);
}

@media (min-width: 769px) {
    .sidebar.collapsed {
        margin-left: calc(-1 * var(--sidebar-width));
        pointer-events: none;
    }
}

.sidebar-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 8px 12px;
}

.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--text-primary);
    text-decoration: none;
}

.sidebar-close-btn {
    display: none;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1.25rem;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    cursor: pointer;
    align-items: center;
    justify-content: center;
}
.sidebar-close-btn:hover { background: var(--bg-hover); color: #fff; }

.new-chat-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 11px 14px;
    border-radius: 10px;
    background: transparent;
    border: 1px solid var(--border-light);
    color: var(--text-primary);
    font-size: 0.92rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
    margin-bottom: 14px;
    min-height: 44px;
}
.new-chat-btn:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
}

.sidebar-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 8px 6px;
}

.sidebar-section-label {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.clear-history-btn {
    display: none;
    align-items: center;
    gap: 4px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 0.72rem;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 5px;
    transition: all 0.15s ease;
}
.clear-history-btn:hover {
    color: var(--danger);
    background: rgba(239, 68, 68, 0.1);
}

.sidebar-history {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding-right: 2px;
}

.sidebar-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 9px;
    color: var(--text-secondary);
    font-size: 0.88rem;
    cursor: pointer;
    transition: all 0.15s ease;
    text-decoration: none;
    min-height: 40px;
    position: relative;
    user-select: none;
}
.sidebar-item svg.item-icon { flex-shrink: 0; opacity: 0.8; }
.sidebar-item:hover, .sidebar-item.active {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.chat-item-text {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.86rem;
}

.chat-delete-btn {
    opacity: 0;
    background: transparent;
    border: none;
    color: var(--text-muted);
    width: 26px;
    height: 26px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.15s ease;
}
.sidebar-item:hover .chat-delete-btn,
.sidebar-item.active .chat-delete-btn {
    opacity: 1;
}
.chat-delete-btn:hover {
    background: rgba(239, 68, 68, 0.18);
    color: var(--danger);
}

@media (max-width: 768px) {
    .chat-delete-btn {
        opacity: 0.85;
    }
}

.sidebar-item.history-empty {
    color: var(--text-muted);
    font-size: 0.82rem;
    cursor: default;
    padding: 12px 10px;
    white-space: normal;
    line-height: 1.4;
}
.sidebar-item.history-empty:hover { background: transparent; }

.sidebar-bottom {
    border-top: 1px solid var(--border);
    padding-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

/* ===== Google Auth UI ===== */
.auth-google-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    background: transparent;
    border: 1px solid var(--border-light);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.86rem;
    cursor: pointer;
    text-align: left;
    transition: background 0.15s ease;
    min-height: 44px;
}
.auth-google-btn:hover { background: var(--bg-hover); }
.google-g-icon { width: 18px; height: 18px; flex-shrink: 0; }

.auth-user-row {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 8px 10px;
    border-radius: 10px;
    cursor: pointer;
    position: relative;
    transition: background 0.15s ease;
    min-height: 44px;
}
.auth-user-row:hover { background: var(--bg-hover); }
.auth-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    flex-shrink: 0;
    object-fit: cover;
}
.auth-user-name {
    font-size: 0.86rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}
.auth-user-menu {
    position: absolute;
    bottom: calc(100% + 6px);
    left: 0;
    width: 100%;
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: 10px;
    padding: 6px;
    display: none;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    z-index: 60;
}
.auth-user-menu.open { display: block; }
.auth-user-menu button {
    width: 100%;
    padding: 10px 12px;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.86rem;
    text-align: left;
    border-radius: 8px;
    cursor: pointer;
    min-height: 40px;
}
.auth-user-menu button:hover { background: var(--bg-hover); }

/* ===== Main Application Area ===== */
.app-main {
    flex: 1;
    width: 100%;
    height: 100%;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
    background: var(--bg-primary);
}

/* ===== Top Header Bar ===== */
header {
    height: var(--header-height);
    min-height: var(--header-height);
    width: 100%;
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    background: var(--bg-primary);
    z-index: 100;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.sidebar-toggle-btn {
    display: flex;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}
.sidebar-toggle-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--border-light);
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--text-primary);
    text-decoration: none;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-icon-btn {
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}
.header-icon-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--border-light);
}
.header-icon-btn.api-configured {
    background: rgba(34, 197, 94, 0.12);
    border-color: rgba(34, 197, 94, 0.4);
    color: #4ade80;
}

/* ===== Main Scrollable View ===== */
.chat-container {
    flex: 1;
    width: 100%;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    position: relative;
    padding: 16px 20px 8px;
}

/* Welcome view */
.welcome-view {
    margin: auto;
    width: 100%;
    max-width: 720px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px 12px;
    text-align: center;
}

/* ===== Workspace / Messages View ===== */
.workspace {
    width: 100%;
    max-width: 960px;
    margin: 0 auto;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.workspace-body {
    flex: 1;
    padding: 8px 0 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.msg {
    display: flex;
    flex-direction: column;
    gap: 4px;
    animation: fadeIn 0.25s ease-out;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.msg-role {
    font-size: 0.76rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    padding-left: 2px;
}
.msg-user .msg-role { color: var(--accent-hover); }

.msg-bubble {
    font-size: 0.95rem;
    line-height: 1.6;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    padding: 12px 14px;
    border-radius: 12px;
}

.msg-user .msg-bubble {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px 12px 2px 12px;
    align-self: flex-start;
    max-width: 94%;
}

.msg-ai .msg-bubble {
    background: transparent;
    padding-left: 2px;
    padding-right: 2px;
    max-width: 100%;
}

.msg-bubble img.chat-attached-img {
    max-width: 100%;
    max-height: 280px;
    border-radius: 10px;
    margin-top: 8px;
    border: 1px solid var(--border);
    cursor: pointer;
    transition: transform 0.2s;
}
.msg-bubble img.chat-attached-img:hover {
    transform: scale(1.01);
}

.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 10px 0;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.msg-bubble table {
    border-collapse: collapse;
    width: 100%;
    font-size: 0.85rem;
    min-width: 320px;
}

.msg-bubble th, .msg-bubble td {
    border: 1px solid var(--border);
    padding: 8px 10px;
    text-align: left;
}
.msg-bubble th {
    background: var(--bg-card);
    color: var(--text-primary);
}

pre {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px 14px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.84rem;
    line-height: 1.5;
    margin: 8px 0;
}

code {
    font-family: 'JetBrains Mono', monospace;
    background: var(--bg-input);
    padding: 2px 5px;
    border-radius: 4px;
    font-size: 0.84rem;
}

.code-block-wrapper {
    position: relative;
    margin: 10px 0;
    border-radius: 10px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.code-block-header {
    background: #0f0f0f;
    color: var(--text-secondary);
    font-size: 0.75rem;
    padding: 6px 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
}

.code-action-btn {
    background: var(--accent);
    border: none;
    color: #fff;
    padding: 4px 9px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.74rem;
    font-weight: 500;
}
.code-action-btn:hover { background: var(--accent-hover); }

.ws-download {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    padding: 8px 14px;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    text-decoration: none;
    font-size: 0.84rem;
    font-weight: 500;
    cursor: pointer;
    border: none;
    min-height: 38px;
}
.ws-download:hover { background: var(--accent-hover); }

.ws-loading {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-secondary);
    font-size: 0.9rem;
    padding: 6px 0;
    flex-wrap: wrap;
}

.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border-light);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Speed & Generation Stats Indicator ===== */
.gen-speed-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 9px;
    border-radius: 20px;
    font-size: 0.73rem;
    font-family: 'JetBrains Mono', monospace;
    background: rgba(99, 102, 241, 0.12);
    color: var(--accent);
    border: 1px solid rgba(99, 102, 241, 0.28);
    margin-top: 6px;
    user-select: none;
    transition: all 0.2s ease;
}
.gen-speed-badge.live {
    background: rgba(16, 185, 129, 0.12);
    color: #10B981;
    border-color: rgba(16, 185, 129, 0.3);
}
.gen-speed-badge.done {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-muted);
    border-color: var(--border);
}
.gen-speed-pulse {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    display: inline-block;
    box-shadow: 0 0 8px currentColor;
    animation: pulseSpeed 1.2s ease-in-out infinite;
}
@keyframes pulseSpeed {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.35; transform: scale(0.75); }
}

/* ===== Bottom Input Area ===== */
.bottom-bar {
    width: 100%;
    background: var(--bg-primary);
    border-top: 1px solid var(--border);
    padding: 10px 14px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    align-items: center;
}

.input-container {
    width: 100%;
    max-width: 960px;
}

.image-preview-bar {
    display: none;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: 8px;
}
.image-preview-thumb {
    width: 44px;
    height: 44px;
    border-radius: 7px;
    object-fit: cover;
    border: 1px solid var(--border);
}
.file-preview-icon {
    width: 44px;
    height: 44px;
    border-radius: 7px;
    background: var(--accent, #2563eb);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.image-preview-info { flex: 1; min-width: 0; }
.image-preview-name {
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.image-preview-hint { font-size: 0.74rem; color: var(--text-muted); }
.image-preview-remove {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    font-size: 1.1rem;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    min-height: 32px;
}
.image-preview-remove:hover { color: var(--danger); }

.input-wrapper {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: 16px;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    transition: border-color 0.2s, box-shadow 0.2s;
}
.input-wrapper:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

.input-textarea {
    width: 100%;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-size: 1rem;
    font-family: inherit;
    resize: none;
    min-height: 48px;
    max-height: 150px;
    line-height: 1.45;
}
.input-textarea::placeholder { color: var(--text-muted); font-size: 0.95rem; }

.input-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
}

.input-tools {
    display: flex;
    align-items: center;
    gap: 4px;
}

.tool-btn {
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}
.tool-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
.tool-btn:active { transform: scale(0.95); }

.input-right-tools {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Model dropdown badge */
.model-select-wrapper {
    position: relative;
}

.model-btn-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    border-radius: 8px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    font-size: 0.78rem;
    cursor: pointer;
    transition: all 0.15s;
    max-width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-height: 34px;
}
.model-btn-badge:hover { background: var(--bg-hover); color: var(--text-primary); }

.model-dropdown-menu {
    display: none;
    position: absolute;
    bottom: calc(100% + 8px);
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: 12px;
    padding: 6px;
    width: min(300px, 92vw);
    max-height: 340px;
    overflow-y: auto;
    box-shadow: 0 12px 36px rgba(0,0,0,0.6);
    z-index: 200;
}

.dropdown-item {
    padding: 9px 12px;
    font-size: 0.84rem;
    color: var(--text-secondary);
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s;
    text-align: left;
    min-height: 38px;
    display: flex;
    align-items: center;
    gap: 10px;
}
.dropdown-item:hover, .dropdown-item.active {
    background: var(--bg-hover);
}

.model-brand-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
    box-shadow: 0 0 6px currentColor;
}

.model-item-name {
    font-weight: 600;
    letter-spacing: 0.01em;
}

.model-item-tag {
    font-size: 0.74rem;
    color: var(--text-muted);
    margin-left: auto;
}

/* Send Button */
.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--accent);
    border: none;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.18s cubic-bezier(0.16, 1, 0.3, 1);
    flex-shrink: 0;
}
.send-btn:hover { background: var(--accent-hover); transform: scale(1.05); }
.send-btn:active { transform: scale(0.95); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

/* ===== Modals ===== */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(4px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 14px;
}
.modal-overlay.open { display: flex; }

.modal-box {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: 16px;
    width: 100%;
    max-width: 460px;
    max-height: 90vh;
    overflow-y: auto;
    padding: 20px;
    box-shadow: 0 24px 60px rgba(0,0,0,0.6);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 14px;
}
.modal-header h2 { font-size: 1.1rem; font-weight: 600; }

.modal-close-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.modal-close-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

.modal-presets {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 14px;
}
.preset-btn {
    padding: 6px 10px;
    border-radius: 7px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    font-size: 0.76rem;
    cursor: pointer;
}
.preset-btn:hover { background: var(--bg-hover); color: #fff; }

.modal-field { margin-bottom: 14px; }
.modal-field label {
    display: block;
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-bottom: 6px;
    font-weight: 500;
}
.modal-field input {
    width: 100%;
    padding: 11px 12px;
    border-radius: 10px;
    border: 1px solid var(--border-light);
    background: var(--bg-input);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
}
.modal-field input:focus {
    outline: none;
    border-color: var(--accent);
}
.modal-hint {
    font-size: 0.74rem;
    color: var(--text-muted);
    margin-top: 5px;
    line-height: 1.4;
}

.modal-actions {
    display: flex;
    gap: 10px;
    margin-top: 18px;
}
.modal-btn {
    flex: 1;
    padding: 12px 14px;
    border-radius: 10px;
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 500;
    min-height: 44px;
}
.modal-btn-primary { background: var(--accent); color: #fff; }
.modal-btn-primary:hover { background: var(--accent-hover); }
.modal-btn-danger { background: var(--danger); color: #fff; }
.modal-btn-danger:hover { opacity: 0.9; }
.modal-btn-secondary { background: transparent; border: 1px solid var(--border-light); color: var(--text-secondary); }
.modal-btn-secondary:hover { background: var(--bg-hover); }

/* Toast notifications */
.toast {
    position: fixed;
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 70px);
    left: 50%;
    transform: translateX(-50%) translateY(30px);
    padding: 10px 18px;
    background: var(--bg-hover);
    border: 1px solid var(--border-light);
    border-radius: 20px;
    color: var(--text-primary);
    font-size: 0.86rem;
    z-index: 1100;
    opacity: 0;
    pointer-events: none;
    transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: 0 10px 25px rgba(0,0,0,0.5);
    max-width: 90vw;
    text-align: center;
}
.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

/* ===== Scroll To Bottom Button ===== */
.scroll-bottom-btn {
    position: fixed;
    bottom: 92px;
    right: 24px;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    color: var(--text-primary);
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 90;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
.scroll-bottom-btn.show { display: flex; animation: fadeIn 0.2s ease; }
.scroll-bottom-btn:hover {
    background: var(--bg-hover);
    transform: translateY(-2px);
    border-color: var(--accent);
}

/* ===== Generated Image Cards ===== */
.generated-img-card {
    width: 100%;
    max-width: 520px;
    margin: 6px 0;
    border-radius: 14px;
    border: 1px solid var(--border);
    background: var(--bg-card);
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.generated-img-card:hover {
    border-color: var(--border-light);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
}
.generated-img-view {
    position: relative;
    cursor: zoom-in;
    background: #111;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    min-height: 260px;
}
.generated-img-element {
    width: 100%;
    height: auto;
    max-height: 520px;
    object-fit: contain;
    display: block;
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
.generated-img-view:hover .generated-img-element {
    transform: scale(1.02);
}
.generated-img-overlay {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.72);
    backdrop-filter: blur(4px);
    color: #fff;
    padding: 5px 10px;
    border-radius: 8px;
    font-size: 0.76rem;
    display: flex;
    align-items: center;
    gap: 5px;
    opacity: 0;
    transition: opacity 0.2s ease;
    pointer-events: none;
}
.generated-img-view:hover .generated-img-overlay {
    opacity: 1;
}
.generated-img-footer {
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    border-top: 1px solid var(--border);
    background: var(--bg-card);
}
.generated-prompt-badge {
    font-size: 0.8rem;
    color: var(--text-secondary);
    line-height: 1.4;
    display: flex;
    align-items: center;
    gap: 6px;
}
.generated-img-btns {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}
.img-action-btn {
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 0.82rem;
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid var(--border);
    background: var(--bg-input);
    color: var(--text-primary);
    transition: all 0.15s ease;
    min-height: 36px;
    text-decoration: none;
}
.img-action-btn:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
    color: #fff;
}
.img-action-btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}
.img-action-btn-primary:hover {
    background: var(--accent-hover);
}

/* ===== Lightbox Modal ===== */
.lightbox-modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.88);
    backdrop-filter: blur(8px);
    z-index: 2000;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.lightbox-modal.open { display: flex; animation: fadeIn 0.2s ease-out; }
.lightbox-box {
    position: relative;
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}
.lightbox-img {
    max-width: 90vw;
    max-height: 78vh;
    object-fit: contain;
    border-radius: 12px;
    border: 1px solid var(--border-light);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.8);
}
.lightbox-close-btn {
    position: absolute;
    top: -16px;
    right: -16px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #111;
    border: 1px solid #333;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    transition: background 0.15s ease, transform 0.15s ease;
    box-shadow: 0 4px 12px rgba(0,0,0,0.5);
    z-index: 2010;
}
.lightbox-close-btn:hover { background: var(--accent); transform: scale(1.08); }

/* ===== Responsive Breakpoints ===== */
@media (max-width: 768px) {
    .sidebar-close-btn { display: flex; }
    .sidebar-overlay { display: block; }
    .sidebar-overlay.open { opacity: 1; pointer-events: auto; }
    .sidebar-overlay:not(.open) { pointer-events: none; }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        transform: translateX(-100%);
        box-shadow: none;
        width: min(300px, 84vw);
        min-width: unset;
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 10px 0 30px rgba(0,0,0,0.5);
    }

    .chat-container { padding: 12px 12px 6px; }

    .model-btn-badge {
        max-width: 130px;
        padding: 5px 8px;
        font-size: 0.74rem;
    }

    .bottom-bar { padding: 8px 10px; }
    .input-wrapper { padding: 8px 10px; border-radius: 14px; }
    .input-textarea { min-height: 44px; }

    .modal-box {
        padding: 16px;
        border-radius: 14px;
    }
}

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 3px; }

/* =====================================================================
   QUANTA OLED LAYER — true black, monochrome, mobile-first touch sizes
   ===================================================================== */
:root { color-scheme: dark; }
html, body, .app-main, header, .bottom-bar, .sidebar { background: #000; }
:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
button, .dropdown-item, .sidebar-item { -webkit-user-select: none; user-select: none; }

/* Header / bottom bar: no hard dividers, safe-area aware */
header {
    border-bottom: 0;
    height: calc(var(--header-height) + env(safe-area-inset-top, 0px));
    min-height: calc(var(--header-height) + env(safe-area-inset-top, 0px));
    padding: env(safe-area-inset-top, 0px) max(12px, env(safe-area-inset-right, 0px)) 0 max(12px, env(safe-area-inset-left, 0px));
}
.bottom-bar {
    border-top: 0;
    padding: 8px max(12px, env(safe-area-inset-right, 0px)) max(12px, env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));
}
.chat-container { overscroll-behavior: contain; }

/* Logo */
.brand-mark { display: block; height: 26px; width: auto; flex-shrink: 0; pointer-events: none; -webkit-user-drag: none; }
.header-brand { min-height: 38px; }
.welcome-view { margin: 0 auto; padding: 0 16px 20px; max-width: 720px; }
.welcome-logo {
    display: block;
    width: 172px;
    height: auto;
    aspect-ratio: 656 / 536;
    pointer-events: none;
    -webkit-user-drag: none;
}
@media (min-width: 769px) { .welcome-logo { width: 200px; } }

/* Start screen: logo + composer centred as one group */
.is-welcome .header-brand { visibility: hidden; }
.is-welcome .chat-container { flex: 0 0 auto; margin-top: auto; overflow: visible; padding-bottom: 0; }
.is-welcome .bottom-bar { margin-bottom: auto; padding-top: 0; padding-bottom: calc(5vh + env(safe-area-inset-bottom, 0px)); }
.is-welcome .input-container { max-width: 720px; }

/* Sidebar */
.sidebar { border-right: 1px solid var(--border); }
.sidebar.collapsed { visibility: hidden; transition: margin-left .28s cubic-bezier(.16,1,.3,1), transform .28s cubic-bezier(.16,1,.3,1), visibility 0s .28s; }
.new-chat-btn:hover { border-color: #444; }
.sidebar-section-label { text-transform: none; letter-spacing: 0; font-size: .78rem; }
.sidebar-logo { min-height: 40px; }

/* Composer */
.input-wrapper { background: var(--bg-card); border: 1px solid var(--border-light); border-radius: 22px; padding: 10px 12px 8px; }
.input-wrapper:focus-within { border-color: #5a5a5a; box-shadow: none; }
.input-textarea { min-height: 44px; }
.input-actions, .input-tools, .input-right-tools { min-width: 0; }
.input-actions { flex-wrap: nowrap; }
.tool-btn { border-radius: 50%; color: var(--text-secondary); }
.tool-btn:hover { color: #fff; }
.send-btn { background: #fff; color: #000; }
.send-btn:hover { background: #e5e5e5; }
.send-btn:disabled { opacity: .3; }

/* Model selector */
.model-select-wrapper { min-width: 0; }
.model-btn-badge {
    background: transparent;
    border: 1px solid var(--border-light);
    border-radius: 999px;
    color: #fff;
    font-size: .8rem;
    font-weight: 500;
    padding: 6px 12px;
    max-width: 190px;
}
.model-btn-badge:hover { background: var(--bg-hover); color: #fff; }
.model-btn-badge #modeLabel { overflow: hidden; text-overflow: ellipsis; min-width: 0; color: #fff; }
.model-btn-badge svg { flex-shrink: 0; }
.model-dropdown-menu {
    background: #0a0a0a;
    border: 1px solid #2b2b2b;
    border-radius: 14px;
    width: min(320px, calc(100vw - 24px));
    max-height: min(340px, 46dvh);
    box-shadow: 0 16px 48px rgba(0,0,0,.9);
    overscroll-behavior: contain;
}
.dropdown-item { color: #fff; min-height: 44px; padding: 10px 12px; }
.dropdown-item:hover { background: #151515; }
.model-item-name { color: #fff; font-weight: 500; letter-spacing: 0; }
.model-item-tag { color: var(--model-tag); font-size: .76rem; font-weight: 400; }
.dropdown-item::after {
    content: "";
    flex-shrink: 0;
    width: 5px;
    height: 10px;
    margin: -2px 4px 0 4px;
    border: solid #fff;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
    opacity: 0;
}
.dropdown-item.active { background: #1c1c1c; box-shadow: inset 2px 0 0 #fff; }
.dropdown-item.active .model-item-name { font-weight: 700; }
.dropdown-item.active::after { opacity: 1; }

/* Monochrome elements */
.msg-user .msg-role { color: var(--text-secondary); }
.code-action-btn, .ws-download, .modal-btn-primary, .img-action-btn-primary { color: var(--on-accent); }
.img-action-btn-primary { border-color: var(--accent); }
.lightbox-close-btn:hover { color: #000; }
.gen-speed-badge { background: rgba(255,255,255,.06); color: var(--text-secondary); border-color: var(--border-light); }
.new-chat-btn:hover, .modal-field input:focus, .img-action-btn:hover { border-color: #5a5a5a; }

/* Touch devices */
@media (pointer: coarse) {
    .sidebar-toggle-btn, .header-icon-btn, .sidebar-close-btn { width: 44px; height: 44px; }
    .tool-btn { width: 40px; height: 40px; }
    .send-btn { width: 44px; height: 44px; }
    .send-btn:hover { transform: none; }
    .model-btn-badge { min-height: 40px; }
    .sidebar-item { min-height: 44px; }
    .chat-delete-btn { width: 36px; height: 36px; }
}
@media (max-width: 768px) {
    .sidebar { padding-top: max(12px, env(safe-area-inset-top, 0px)); padding-bottom: max(12px, env(safe-area-inset-bottom, 0px)); overscroll-behavior: contain; }
    .sidebar:not(.open) { visibility: hidden; transition: transform .28s cubic-bezier(.16,1,.3,1), visibility 0s .28s; }
    .model-btn-badge { max-width: 168px; padding: 5px 10px; font-size: .78rem; }
    .input-wrapper { border-radius: 20px; padding: 8px 10px 6px; }
    .model-select-wrapper { position: static; }
    .input-wrapper { position: relative; }
    .model-dropdown-menu { left: 0; right: 0; width: auto; }
    .modal-field input, .input-textarea { font-size: 16px; }
    .is-welcome .bottom-bar { padding-bottom: calc(8vh + env(safe-area-inset-bottom, 0px)); }
}
@media (max-width: 360px) {
    .header-actions { gap: 4px; }
    .tool-btn { width: 36px; height: 36px; }
}
html, body { overflow-x: hidden; }
img, svg, video { max-width: 100%; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* Fix composer focus rings */
.input-textarea,
.input-textarea:focus,
.input-textarea:focus-visible,
.input-textarea:active {
    outline: none !important;
    box-shadow: none !important;
    border: none !important;
    background: transparent !important;
    -webkit-tap-highlight-color: transparent;
}
.input-wrapper,
.input-wrapper:focus,
.input-wrapper:focus-visible,
.input-wrapper:focus-within {
    outline: none !important;
    box-shadow: none !important;
    border-color: var(--border-light) !important;
}

/* Monochrome inline icons */
.ico { display: inline-block; vertical-align: -0.16em; flex-shrink: 0; }
.code-action-btn, .auth-user-menu button, .modal-header h2 { display: inline-flex; align-items: center; gap: 6px; }
.modal-header h2 { gap: 8px; }
.image-preview-remove, .lightbox-close-btn { display: inline-flex; align-items: center; justify-content: center; }
.generated-prompt-badge > span:first-child { display: inline-flex; align-items: center; }

/* Send <-> Stop button */
.send-btn.is-stop { background: #fff; color: #000; }
.send-btn.is-stop svg { display: block; }
.send-btn.is-stop:hover { background: #e5e5e5; }

/* Typing dots */
.typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 22px;
    padding: 0 2px;
}
.typing-dots i {
    display: block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #fff;
    opacity: .25;
    transform: translateY(0) scale(.8);
    animation: typingWave 1.25s cubic-bezier(.45, 0, .25, 1) infinite;
    will-change: transform, opacity;
}
.typing-dots i:nth-child(2) { animation-delay: .16s; }
.typing-dots i:nth-child(3) { animation-delay: .32s; }

@keyframes typingWave {
    0%, 60%, 100% {
        opacity: .25;
        transform: translateY(0) scale(.8);
        box-shadow: 0 0 0 rgba(255, 255, 255, 0);
    }
    30% {
        opacity: 1;
        transform: translateY(-6px) scale(1.05);
        box-shadow: 0 0 10px rgba(255, 255, 255, .45);
    }
}

@media (prefers-reduced-motion: reduce) {
    .typing-dots i { animation: typingFade 1.4s ease-in-out infinite; }
    .typing-dots i:nth-child(2) { animation-delay: .2s; }
    .typing-dots i:nth-child(3) { animation-delay: .4s; }
    @keyframes typingFade { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }
}

/* ===== Drag and Drop Overlay ===== */
.drag-drop-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(6px);
    z-index: 5000;
    display: none;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    transition: opacity 0.2s ease;
    opacity: 0;
}

.drag-drop-overlay.active {
    display: flex;
    opacity: 1;
    pointer-events: auto;
}

.drag-drop-box {
    border: 2px dashed #ffffff;
    border-radius: 20px;
    padding: 40px 50px;
    background: rgba(20, 20, 20, 0.7);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    text-align: center;
    color: #ffffff;
    animation: pulseBorder 2s infinite ease-in-out;
}

.drag-drop-box svg {
    color: #ffffff;
    filter: drop-shadow(0 0 8px rgba(255, 255, 255, 0.4));
}

.drag-drop-title {
    font-size: 1.25rem;
    font-weight: 600;
}

.drag-drop-sub {
    font-size: 0.86rem;
    color: var(--text-muted);
}

@keyframes pulseBorder {
    0%, 100% { border-color: rgba(255, 255, 255, 0.5); transform: scale(1); }
    50% { border-color: #ffffff; transform: scale(1.02); }
}

/* =====================================================================
   WEB SEARCH & SOURCES (INTEGRATED EXTENSION)
   ===================================================================== */
/* Web Search Toggle Active State */
.tool-btn#webSearchToggle.active,
.tool-btn#webSearchToggleBtn.active,
#webSearchToggleBtn.active {
    background: #ffffff !important;
    color: #000000 !important;
    box-shadow: 0 0 14px rgba(255, 255, 255, 0.35) !important;
}

/* Web Sources Card & Container */
.web-sources-card {
    margin: 12px 0 4px;
    padding: 12px 14px;
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: 12px;
    font-size: 0.86rem;
}

.web-sources-header {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 8px;
    cursor: pointer;
    user-select: none;
}

.web-sources-header svg {
    flex-shrink: 0;
}

.web-sources-toggle {
    margin-left: auto;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.web-sources-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
}

.web-source-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 8px;
    text-decoration: none;
    color: var(--text-primary);
    transition: all 0.15s ease;
}

.web-source-item:hover {
    background: var(--bg-hover);
    border-color: #5a5a5a;
}

.web-source-favicon {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
    border-radius: 3px;
}

.web-source-title {
    flex: 1;
    font-size: 0.84rem;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.web-source-domain {
    font-size: 0.74rem;
    color: var(--text-muted);
}

/* Chip Fallback Layout */
.web-sources-box {
    margin-top: 10px;
    padding-top: 8px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.web-sources-label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
}
.web-source-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px;
    border-radius: 8px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    font-size: 0.76rem;
    color: var(--text-primary);
    text-decoration: none;
    transition: all 0.15s ease;
}
.web-source-chip:hover {
    background: var(--bg-hover);
    border-color: #5a5a5a;
}
/* ===== Форматирование текста сообщений и списков ===== */
.msg-bubble {
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.msg-bubble p {
    margin: 0 0 12px 0;
    line-height: 1.6;
}

.msg-bubble p:last-child {
    margin-bottom: 0;
}

.msg-bubble ul,
.msg-bubble ol {
    margin: 10px 0;
    padding-left: 24px;
    line-height: 1.7;
}

.msg-bubble ul {
    list-style-type: disc;
}

.msg-bubble ol {
    list-style-type: decimal;
}

.msg-bubble li {
    margin: 6px 0;
    padding-left: 4px;
}

.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 {
    margin: 16px 0 10px;
    font-weight: 600;
    line-height: 1.3;
}

.msg-bubble blockquote {
    border-left: 3px solid var(--accent, #6366f1);
    padding-left: 14px;
    margin: 12px 0;
    color: var(--text-secondary, #94a3b8);
    font-style: italic;
}

.msg-bubble code:not(pre code) {
    background: rgba(255, 255, 255, 0.08);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.88em;
}

.msg-bubble pre {
    margin: 10px 0;
}