:root,
.theme-light {
    --bg-primary: #ffffff;
    --bg-secondary: #f3f4f6;
    --bg-sidebar: #f9fafb;
    --bg-bubble-user: #2563eb;
    --bg-bubble-assistant: #f3f4f6;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --text-on-primary: #ffffff;
    --border-color: #e5e7eb;
    --accent-color: #2563eb;
    --error-color: #dc2626;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.theme-dark {
    --bg-primary: #111827;
    --bg-secondary: #1f2937;
    --bg-sidebar: #0f172a;
    --bg-bubble-user: #2563eb;
    --bg-bubble-assistant: #1f2937;
    --text-primary: #f9fafb;
    --text-secondary: #9ca3af;
    --text-on-primary: #ffffff;
    --border-color: #374151;
    --accent-color: #3b82f6;
    --error-color: #f87171;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/*
 * Telerik/Kendo components derive their colors from design tokens defined at
 * :root by the light theme. Overriding the core tokens on the <body> when dark
 * mode is active flips the AppBar, Splitter, buttons, dropdowns and dialogs to
 * dark. The token overrides must live on an ancestor of every component
 * (including popups appended to <body>), hence the body.theme-dark scope.
 */
body.theme-dark {
    color-scheme: dark;
    --kendo-color-app-surface: #111827;
    --kendo-color-on-app-surface: #f9fafb;
    --kendo-color-surface: #1f2937;
    --kendo-color-surface-alt: #243042;
    --kendo-color-base: #1f2937;
    --kendo-color-base-hover: #2a3647;
    --kendo-color-base-active: #313f54;
    --kendo-color-on-base: #f9fafb;
    --kendo-color-border: rgba(255, 255, 255, 0.12);
    --kendo-color-border-alt: rgba(255, 255, 255, 0.22);
}

*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    /* Lock the outer shell; only the message list scrolls. Prevents the page
       from scrolling the app bar away when the mobile keyboard opens. */
    overflow: hidden;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
}

#app {
    height: 100%;
}

.app-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    gap: 1rem;
    color: var(--text-secondary);
}

.app-loading-spinner {
    width: 2.5rem;
    height: 2.5rem;
    border: 3px solid var(--border-color);
    border-top-color: var(--accent-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.app-shell {
    height: 100vh;
}

.chat-page {
    --appbar-height: 56px;
    display: flex;
    flex-direction: column;
    height: 100vh;
    /* Dynamic viewport height keeps the composer reachable above mobile browser chrome. */
    height: 100dvh;
    /* --app-height is driven by the VisualViewport API so the layout shrinks when
       the on-screen keyboard opens (keeps the composer above the keyboard). */
    height: var(--app-height, 100dvh);
    background: var(--bg-primary);
    color: var(--text-primary);
}

.chat-appbar {
    min-height: var(--appbar-height);
}

.chat-appbar-title {
    font-weight: 600;
    font-size: 1.1rem;
    margin-left: 0.5rem;
}

.chat-body {
    display: flex;
    flex: 1;
    min-height: 0;
}

.chat-sidebar {
    width: 280px;
    border-right: 1px solid var(--border-color);
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.sidebar-header {
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.sidebar-empty {
    padding: 1rem;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.sidebar-item {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    border-radius: 0.5rem;
    margin-bottom: 0.25rem;
}

.sidebar-item.active {
    background: var(--bg-secondary);
}

.sidebar-item-button {
    flex: 1;
    text-align: left;
    background: none;
    border: none;
    padding: 0.625rem 0.75rem;
    cursor: pointer;
    color: var(--text-primary);
}

.sidebar-item-title {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-item-meta {
    display: block;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: var(--bg-primary);
}

.chat-error-banner {
    background: color-mix(in srgb, var(--error-color) 15%, transparent);
    color: var(--error-color);
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    border-bottom: 1px solid var(--border-color);
}

.chat-message-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 1.5rem 1rem;
    scroll-behavior: smooth;
}

.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    color: var(--text-secondary);
}

.chat-message {
    display: flex;
    flex-direction: column;
    margin-bottom: 1.25rem;
    max-width: 820px;
    width: 100%;
}

.chat-message.user {
    margin-left: auto;
    align-items: flex-end;
}

.chat-message.assistant,
.chat-message.system {
    margin-right: auto;
    align-items: flex-start;
}

.chat-bubble {
    padding: 0.875rem 1rem;
    border-radius: 1rem;
    box-shadow: var(--shadow-sm);
    line-height: 1.6;
    word-break: break-word;
}

.chat-message.user .chat-bubble {
    background: var(--bg-bubble-user);
    color: var(--text-on-primary);
    border-bottom-right-radius: 0.25rem;
}

.chat-message.assistant .chat-bubble,
.chat-message.system .chat-bubble {
    background: var(--bg-bubble-assistant);
    color: var(--text-primary);
    border-bottom-left-radius: 0.25rem;
}

.chat-bubble.error {
    border: 1px solid var(--error-color);
}

.chat-message-actions {
    display: flex;
    gap: 0.25rem;
    margin-top: 0.25rem;
    opacity: 0;
    transition: opacity 0.15s ease;
}

.chat-message:hover .chat-message-actions {
    opacity: 1;
}

/* Touch devices have no hover, so keep message actions visible. */
@media (hover: none) {
    .chat-message-actions {
        opacity: 1;
    }
}

.markdown-content pre {
    background: #0d1117;
    color: #e6edf3;
    padding: 1rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    margin: 0.75rem 0;
}

.markdown-content code {
    font-family: Consolas, 'Courier New', monospace;
    font-size: 0.875em;
}

.markdown-content p {
    margin: 0 0 0.75rem;
}

.markdown-content p:last-child {
    margin-bottom: 0;
}

.streaming-cursor {
    display: inline-block;
    width: 0.5rem;
    height: 1rem;
    background: var(--accent-color);
    margin-left: 2px;
    animation: blink 1s step-end infinite;
    vertical-align: text-bottom;
}

@keyframes blink {
    50% { opacity: 0; }
}

.typing-indicator {
    display: flex;
    gap: 0.35rem;
    padding: 0.5rem 0;
}

.typing-indicator span {
    width: 0.5rem;
    height: 0.5rem;
    background: var(--text-secondary);
    border-radius: 50%;
    animation: bounce 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

.chat-composer {
    border-top: 1px solid var(--border-color);
    padding: 1rem;
    background: var(--bg-secondary);
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.chat-composer-actions {
    display: flex;
    justify-content: flex-end;
}

.chat-loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

#blazor-error-ui {
    display: none;
    background: #b32121;
    color: white;
    padding: 0.75rem 1rem;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1000;
}

.chat-splitter {
    flex: 1;
    min-height: 0;
    border: none;
}

.chat-sidebar-pane,
.chat-main-pane {
    overflow: hidden;
}

.chat-drawer-backdrop {
    position: fixed;
    inset: var(--appbar-height) 0 0 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 90;
}

.chat-sidebar-mobile {
    position: fixed;
    top: var(--appbar-height);
    left: 0;
    bottom: 0;
    z-index: 100;
    width: 280px;
    max-width: 85vw;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.chat-sidebar-mobile.open {
    transform: translateX(0);
}

.confirm-dialog-message {
    margin: 0;
    color: var(--text-primary);
    line-height: 1.5;
}

.sidebar-new-btn {
    width: 100%;
}

.sidebar-menu-btn {
    flex-shrink: 0;
}

.model-selector {
    min-width: 180px;
}

.model-selector-label {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    opacity: 0.75;
    margin-right: 0.5rem;
}

@media (max-width: 768px) {
    .chat-message {
        max-width: 100%;
    }

    /* Free up app bar space on narrow screens. */
    .chat-appbar-title {
        display: none;
    }

    .model-selector-label {
        display: none;
    }

    /* Override Telerik's inline width so the dropdown fits a phone screen. */
    .model-selector {
        width: 42vw !important;
        min-width: 0 !important;
        max-width: 200px;
    }

    .chat-message-list {
        padding: 1rem 0.75rem;
    }

    /* Respect the iOS home indicator / safe area at the bottom. */
    .chat-composer {
        padding: 0.75rem;
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
    }

    /* 16px input font prevents iOS Safari from auto-zooming on focus. */
    .chat-input textarea,
    .chat-input .k-input-inner {
        font-size: 16px;
    }
}

.pwa-update-banner {
    position: fixed;
    bottom: calc(1rem + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    z-index: 1100;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    max-width: calc(100vw - 2rem);
    padding: 0.6rem 0.75rem 0.6rem 1rem;
    border-radius: 0.75rem;
    background: #1f2937;
    color: #f9fafb;
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
    animation: pwa-update-in 0.25s ease-out;
}

.pwa-update-icon {
    display: inline-flex;
    align-items: center;
    color: #818cf8;
}

.pwa-update-text {
    font-size: 0.9rem;
    white-space: nowrap;
}

.pwa-update-dismiss {
    background: transparent;
    border: none;
    color: inherit;
    opacity: 0.6;
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
    padding: 0 0.25rem;
}

.pwa-update-dismiss:hover {
    opacity: 1;
}

@keyframes pwa-update-in {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(1rem);
    }

    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

@media (max-width: 768px) {
    .pwa-update-text {
        white-space: normal;
    }
}
