/* ===================================================================
   AppraiserAi.UI — shared AI panel stylesheet
   Included by all hosts via: _content/AppraiserAi.UI/css/ai-app.css
   Host-specific overrides (e.g. splitter-pane sizing) go in the
   host's own stylesheet loaded AFTER this one.
   =================================================================== */

/* ===== THEME TOKENS =====
   Light defaults live in :root. The dark overrides apply only when the HOST sets
   data-bs-theme="dark" on <html> — Flex does (via ThemeService); ClickForms never
   does, so it always resolves the light values. Defined here (not in Flex's
   AgCF.css) because ClickForms doesn't load AgCF.css. Never hardcode a color in the
   rules below — reference a token. Semantic hues stay; only lightness flips for dark. */
:root {
    --ai-bg: #ffffff;
    --ai-surface: #ffffff;
    --ai-surface-sunken: #f8f9fa;
    --ai-surface-hover: #e9ecef;
    --ai-assistant-bubble: #f1f3f4;

    --ai-text: #212529;
    --ai-text-secondary: #495057;
    --ai-text-muted: #6c757d;
    --ai-text-placeholder: #999999;

    --ai-border: #dee2e6;
    --ai-border-subtle: #e9ecef;

    --ai-accent: #0d6efd;
    --ai-accent-hover: #0b5ed7;
    --ai-on-accent: #ffffff;
    --ai-accent-subtle: #e8f4fd;
    --ai-accent-subtle-border: #86b7fe;
    --ai-focus-ring: rgba(13, 110, 253, 0.15);

    --ai-success: #198754;
    --ai-success-hover: #157347;
    --ai-warning-surface: #fff3cd;
    --ai-warning: #ffc107;
    --ai-on-warning: #000000;
    --ai-danger: #dc3545;

    --ai-template-bg: #e8f4e8;
    --ai-shadow: rgba(0, 0, 0, 0.15);

    /* Toast: an inverse (always-dark) floating element, in both themes. */
    --ai-toast-bg: #343a40;
    --ai-toast-text: #ffffff;
}

[data-bs-theme="dark"] {
    --ai-bg: #1e2227;
    --ai-surface: #2b3035;
    --ai-surface-sunken: #212529;
    --ai-surface-hover: #343a40;
    --ai-assistant-bubble: #343a40;

    --ai-text: #e9ecef;
    --ai-text-secondary: #ced4da;
    --ai-text-muted: #adb5bd;
    --ai-text-placeholder: #6c757d;

    --ai-border: #495057;
    --ai-border-subtle: #343a40;

    --ai-accent: #5b73ff;
    --ai-accent-hover: #6f86ff;
    --ai-on-accent: #ffffff;
    --ai-accent-subtle: #243044;
    --ai-accent-subtle-border: #3d5a99;
    --ai-focus-ring: rgba(91, 115, 255, 0.25);

    --ai-success: #1a9d63;
    --ai-success-hover: #157d50;
    --ai-warning-surface: #332701;
    --ai-warning: #ffda6a;
    --ai-on-warning: #000000;
    --ai-danger: #ea868f;

    --ai-template-bg: #1e3226;
    --ai-shadow: rgba(0, 0, 0, 0.45);

    --ai-toast-bg: #495057;
    --ai-toast-text: #ffffff;
}

/* ===== DxPopup polish for AppraiserAi dialogs ===== */
/* Give custom HeaderContentTemplate content room to breathe and
   ensure FooterContentTemplate flex containers can span the full width. */
.model-setup-popup .dxbl-popup-header,
.sales-prompt-popup .dxbl-popup-header,
.embedding-unavailable-popup .dxbl-popup-header,
.index-directory-popup .dxbl-popup-header,
.disable-indexing-popup .dxbl-popup-header,
.app-dialog-popup .dxbl-popup-header {
    padding-bottom: 0.75rem;
}

.model-setup-popup .dxbl-popup-footer > *,
.sales-prompt-popup .dxbl-popup-footer > *,
.embedding-unavailable-popup .dxbl-popup-footer > *,
.index-directory-popup .dxbl-popup-footer > *,
.disable-indexing-popup .dxbl-popup-footer > *,
.app-dialog-popup .dxbl-popup-footer > * {
    width: 100%;
}

/* ===== MAIN APP LAYOUT ===== */

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
    position: relative; /* anchor for the settings overlay so it fills the AI area, not the host header */
}

.chat-area {
    flex: 1;
    overflow: hidden;
    min-height: 0; /* Important for flex children to scroll */
    display: flex;
    flex-direction: column;
}

.warning-banners {
    flex-shrink: 0;
}

.warning-banners .alert {
    display: flex;
    justify-content: center;
}

.banner-content {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    max-width: 860px;
}

.chat-area-content {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    justify-content: center;
}

/* Transient inline notice shown after an insert (e.g. clipboard fallback). */
.ai-insert-notice {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 4px 10px;
    padding: 6px 10px;
    font-size: 0.85rem;
    color: var(--ai-text-secondary);
    background: var(--ai-accent-subtle);
    border: 1px solid var(--ai-accent-subtle-border);
    border-radius: 4px;
}

/* ===== HEADER ===== */
.header-bar {
    padding: 6px 10px;
    border-bottom: 1px solid var(--ai-border);
    background: var(--ai-surface-sunken);
    flex-shrink: 0;
    position: relative; /* positioning context for the search results dropdown */
}

.header-inner {
    display: flex;
    align-items: center;
    gap: clamp(8px, 6%, 32px);
    width: 100%;
    max-width: 860px;
    margin: 0 auto;
    /* Lock the row to the DevExpress Medium control height (32px) so the header doesn't shrink when
       the shorter search bar replaces the chat-selector combo — identical height in both states. */
    min-height: 32px;
}

.header-left {
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.header-center {
    display: flex;
    align-items: center;
    gap: 4px;
    flex: 1;
    min-width: 0;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

/* While the message search is expanded, it takes over the header row in place: the chat selector
   and action buttons hide and the search field grows to fill the same bar (no overlay, no shift). */
.header-inner.searching .header-center,
.header-inner.searching .header-right {
    display: none;
}

.header-inner.searching .header-left {
    flex: 1;
    min-width: 0;
}

/* ===== HEADER ICON BUTTONS ===== */
.header-icon-btn {
    width: 28px;
    height: 28px;
    min-width: 28px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-icon-btn .bi {
    font-size: 1.1rem;
}

.index-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    cursor: help;
}

.index-indicator .bi {
    font-size: 1.1rem;
}

/* ===== SESSION SELECTOR ===== */
.session-selector {
    flex: 1;
    min-width: 0;
    font-size: 0.85rem;
}

.session-selector .dxbl-combobox {
    height: 28px !important;
    min-height: 28px !important;
    border: 1px solid transparent !important;
    background: transparent !important;
    font-weight: 500;
}

.session-selector .dxbl-combobox:hover,
.session-selector .dxbl-combobox:focus-within {
    background: var(--ai-surface-hover) !important;
    border-color: var(--ai-border) !important;
    border-radius: 6px !important;
}

.session-selector .dxbl-combobox input {
    font-weight: 500;
}

/* ===== CHAT CONTAINER ===== */
.chat-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 860px;
    overflow: hidden;
    height: 100%;
}

.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* ===== CELL PREVIEW ===== */
.cell-preview {
    background: var(--ai-surface-sunken);
    border: 1px solid var(--ai-border-subtle);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 12px;
    flex-shrink: 0;
}

.cell-preview-header {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--ai-text-muted);
    margin-bottom: 8px;
}

.cell-preview-header i {
    font-size: 0.9rem;
}

.cell-preview-body {
    font-size: 0.9rem;
    color: var(--ai-text);
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 150px;
    overflow-y: auto;
}

.cell-preview-empty {
    color: var(--ai-text-placeholder);
    font-style: italic;
}

/* ===== CHAT MESSAGES ===== */
.chat-message {
    display: flex;
    flex-direction: column;
    margin-bottom: 1rem;
    max-width: min(85%, 720px);
}

.chat-message.user {
    align-self: flex-end;
    align-items: flex-end;
}

.chat-message.assistant {
    align-self: flex-start;
    /* Don't set align-items here - let children determine their own width */
}

.message-bubble {
    padding: 0.75rem 1rem;
    border-radius: 1rem;
    width: fit-content;      /* Shrink to content */
    max-width: 100%;
    word-wrap: break-word;
}

.chat-message.user .message-bubble {
    background-color: var(--ai-accent);
    color: var(--ai-on-accent);
    border-bottom-right-radius: 0.25rem;
}

.chat-message.assistant .message-bubble {
    background-color: var(--ai-assistant-bubble);
    color: var(--ai-text);
    border-bottom-left-radius: 0.25rem;
}

.message-bubble pre {
    margin: 0;
    white-space: pre-wrap;
    font-family: inherit;
    font-size: inherit;
}

/* ===== MESSAGE ACTIONS ===== */
.message-actions {
    display: flex;
    gap: 0.25rem;
    margin-top: 0.25rem;
    opacity: 0;
    transition: opacity 0.15s ease;
    justify-content: flex-end;  /* Right-align buttons */
}

.chat-message:hover .message-actions {
    opacity: 1;
}

.message-actions-user {
    justify-content: flex-end;
    width: 80%;
    align-self: flex-end;
}

.action-btn {
    width: 28px;
    height: 28px;
    padding: 0;
    border: 0px;
    border-radius: 4px;
    background: var(--ai-surface);
    color: var(--ai-text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}

.action-btn:hover {
    background: var(--ai-surface-hover);
    color: var(--ai-text-secondary);
}

.action-btn-success {
    background: var(--ai-success);
    border-color: var(--ai-success);
    color: var(--ai-on-accent);
}

.action-btn-success:hover {
    background: var(--ai-success-hover);
    border-color: var(--ai-success-hover);
    color: var(--ai-on-accent);
}

.action-btn .bi {
    font-size: 0.9rem;
}

.action-btn .bi-arrow-clockwise {
    font-size: 1.1rem;
}

/* ===== TYPING INDICATOR ===== */
.typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 0;
}

.typing-indicator .dot {
    width: 6px;
    height: 6px;
    background-color: var(--ai-text-muted);
    border-radius: 50%;
    opacity: 0.3;
    animation: wave 1.4s ease-in-out infinite;
}

.typing-indicator .dot:nth-child(1) {
    animation-delay: 0s;
}

.typing-indicator .dot:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator .dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes wave {
    0%, 60%, 100% {
        opacity: 0.3;
        transform: translateY(0);
    }
    30% {
        opacity: 1;
        transform: translateY(-4px);
    }
}

/* ===== EMPTY STATE ===== */
.empty-state {
    text-align: center;
    color: var(--ai-text-muted);
    padding: 20px;
    margin: auto 0; /* Center vertically in flex container */
}

.empty-state > i {
    font-size: 2.5rem;
    display: block;
    margin-bottom: 10px;
    opacity: 0.6;
}

.empty-state p {
    font-size: 0.85rem;
    margin: 0;
}

/* ===== SETTINGS OVERLAY ===== */
/* position: absolute so the overlay fills whatever positioned container the
   host provides. A full-screen MAUI window (absolute = fixed behaviour) and
   a DxSplitterPane (needs absolute to stay within the pane) both work. */
.settings-overlay {
    position: absolute;
    inset: 0;
    width: auto;
    height: auto;
    background: var(--ai-surface);
    z-index: 1000;
    display: flex;
    flex-direction: column;
}

.settings-header {
    border-bottom: 1px solid var(--ai-border);
    background: var(--ai-surface-sunken);
    flex-shrink: 0;
    padding: 6px 10px;
}

/* Match the main header's control-row height so the two headers line up consistently. */
.settings-header .settings-inner {
    min-height: 32px;
}

.settings-inner {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    width: 100%;
    max-width: 860px;
    margin: 0 auto;
}

.settings-content {
    flex: 1;
    overflow-y: auto;
}

/* In the content area, settings-inner is a block column, not a flex row */
.settings-content > .settings-inner {
    display: block;
}

/* ===== SETTINGS PANEL ===== */
.settings-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.settings-tabs {
    display: flex;
    border-bottom: 1px solid var(--ai-border);
    background: var(--ai-surface-sunken);
    flex-shrink: 0;
}

.tab-btn {
    flex: 1;
    padding: 10px;
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 0.9rem;
}

.tab-btn.active {
    background: var(--ai-surface);
    border-bottom: 2px solid var(--ai-accent);
    font-weight: 500;
}

.tab-btn:hover:not(.active) {
    background: var(--ai-surface-hover);
}

.settings-body {
    flex: 1;
    overflow-y: auto;
}

/* ===== UNIFIED INPUT BOX ===== */
.input-box {
    border: 1px solid var(--ai-border);
    border-radius: 12px;
    margin: 8px 10px;
    background: var(--ai-surface);
    overflow: hidden;
    flex-shrink: 0;
}

.input-box-top {
    padding: 4px 8px 2px 8px;
}

.input-box-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 8px 8px 8px;
    gap: 8px;
}

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

.context-toggle-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.78rem;
    color: var(--ai-text-muted);
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
    flex-shrink: 1;
    min-width: 0;
    overflow: hidden;
}

.context-toggle-label span {
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.context-toggle-stack {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 8px;
    margin-left: 6px;
    flex-shrink: 1;
    min-width: 0;
    overflow: hidden;
}

.model-selector-inline .dxbl-combobox {
    border: none !important;
    background: var(--ai-assistant-bubble) !important;
    border-radius: 8px !important;
}

/* Focus state for the entire input box */
.input-box:focus-within {
    border-color: var(--ai-accent-subtle-border);
    box-shadow: 0 0 0 0.2rem var(--ai-focus-ring);
}

/* ===== INPUT BOX SELECTORS ===== */
.input-left {
    display: flex;
    gap: 6px;
    flex-shrink: 1;
    min-width: 0;
    align-items: center;
}

.template-selector-inline,
.model-selector-inline {
    min-width: 90px;
    max-width: 140px;
    font-size: 0.8rem;
}

.template-selector-inline .dxbl-combobox,
.model-selector-inline .dxbl-combobox {
    height: 32px !important;
    min-height: 32px !important;
}

.template-selector-inline .dxbl-combobox {
    border: none !important;
    background: var(--ai-template-bg) !important;
    border-radius: 8px !important;
}

.model-selector-inline .dxbl-combobox {
    border: none !important;
    background: var(--ai-assistant-bubble) !important;
    border-radius: 8px !important;
}

/* ===== INPUT ACTION BUTTON ===== */
.input-action-btn {
    height: 30px !important;
    width: 30px !important;
    min-width: 30px !important;
    padding: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

.input-action-btn .bi-stop-fill {
    font-size: 1.2rem;
}

/* ===== INPUT TEXTAREA ===== */
.input-textarea {
    width: 100%;
    border: none;
    outline: none;
    background: transparent;
    resize: none;
    font-family: inherit;
    font-size: 0.9rem;
    line-height: 1.4;
    padding: 4px 4px;
    min-height: 24px;
    max-height: 120px;
    overflow-y: auto;
    field-sizing: content;
}

.input-textarea::placeholder {
    color: var(--ai-text-placeholder);
}

/* ===== SETTINGS EDITOR ===== */
.settings-editor-container {
    display: flex;
    flex-direction: column;
    padding: 1rem;
    max-width: 860px;
    margin: 0 auto;
}

/* ===== QUICK ACTIONS ===== */
.quick-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
    padding: 6px 10px;
    background: var(--ai-bg);
    flex-shrink: 0;
}

.quick-action-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 4px 10px;
    font-size: 0.8rem;
    color: var(--ai-text-secondary);
    background: var(--ai-surface);
    border: 1px solid var(--ai-border);
    border-radius: 16px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.quick-action-btn:hover {
    background: var(--ai-accent-subtle);
    border-color: var(--ai-accent-subtle-border);
    color: var(--ai-accent);
}

.quick-action-btn .bi {
    font-size: 0.85rem;
}

.empty-state .quick-action-btn {
    margin-top: 14px;
}

/* ===== SESSION USAGE ICON ===== */
.session-usage-icon {
    color: var(--ai-text-muted);
    border-radius: 4px;
    cursor: help;
}

.session-usage-icon:hover {
    background: var(--ai-surface-hover);
    color: var(--ai-text-secondary);
}

/* ===== BUTTON LOADING SPINNER ===== */
/* Make spinner-border appropriately sized when used in buttons */
.dxbl-btn .spinner-border-sm {
    width: 1rem;
    height: 1rem;
    border-width: 0.15em;
    margin-right: 0.5rem;
}

/* For icon-only buttons (no text), center the spinner */
.dxbl-btn .spinner-border-sm:only-child {
    margin: 0;
}

/* Adjust spinner animation speed for better UX */
.spinner-border-sm {
    animation: spinner-border 0.75s linear infinite;
}

/* ===== DARK-MODE OVERRIDES FOR NON-ADAPTIVE BOOTSTRAP/DEVEXPRESS BITS =====
   Scoped to the AI panel so nothing leaks to a host's main UI. Only active when the
   host sets data-bs-theme="dark" (Flex); ClickForms never does, so it stays light. */

/* Bootstrap's .bg-light is a FIXED light color (it does not follow data-bs-theme).
   Remap it to a recessed themed surface so cards/panels stop rendering white. */
[data-bs-theme="dark"] .app-container .bg-light,
[data-bs-theme="dark"] .settings-overlay .bg-light {
    background-color: var(--ai-surface-sunken) !important;
}

/* DevExpress "Light" render-style icon buttons (new chat, delete, settings, search close)
   keep a light fill in the Fluent dark theme; blend them into the dark header instead. */
[data-bs-theme="dark"] .dxbl-btn.header-icon-btn {
    background-color: transparent !important;
    border-color: transparent !important;
    color: var(--ai-text) !important;
}

[data-bs-theme="dark"] .dxbl-btn.header-icon-btn:hover {
    background-color: var(--ai-surface-hover) !important;
}

/* DxPopup content PORTALS to <body>, outside .app-container, so the scoped overrides above never
   reach it (this is why the Maven login MODAL stayed light while the in-panel settings page is
   dark — same component, different DOM home). Theme the AppraiserAi popups + their bg-light cards
   here, scoped to each popup's CssClass so no other DevExpress popups in the host are touched. */
[data-bs-theme="dark"] :is(.model-setup-popup, .app-dialog-popup, .sales-prompt-popup,
    .embedding-unavailable-popup, .index-directory-popup, .disable-indexing-popup)
    :is(.dxbl-popup-header, .dxbl-popup-body, .dxbl-popup-footer) {
    background-color: var(--ai-surface) !important;
    color: var(--ai-text) !important;
}

[data-bs-theme="dark"] :is(.model-setup-popup, .app-dialog-popup, .sales-prompt-popup,
    .embedding-unavailable-popup, .index-directory-popup, .disable-indexing-popup) .bg-light {
    background-color: var(--ai-surface-sunken) !important;
}
