/*
 * Shared Ask AI chat styling.
 *
 * One stylesheet for both surfaces that render an Ask AI conversation:
 *   - ask-ai.php, the full page (container: .ask-ai-container.sm-ai-chat)
 *   - common/ask_ai_drawer.php, the site-wide left drawer (.sm-ai-drawer.sm-ai-chat)
 *
 * Every rule is scoped under .sm-ai-chat so the generic class names the page
 * has always used (.message-content, .input-wrapper, ...) cannot leak into the
 * rest of the app. The base values are the page's original values, verbatim;
 * the .sm-ai-drawer block at the bottom only tightens them for a 420px panel.
 * Loaded by include/header.php on every page.
 */
.sm-ai-chat {
    --ai-primary: #6366f1;
    --ai-primary-light: #818cf8;
    --ai-primary-dark: #4f46e5;
    --ai-gradient: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
    --ai-user-bg: #f1f5f9;
    --ai-assistant-bg: #ffffff;
    --ai-border: #e2e8f0;
    --ai-text-muted: #64748b;
}

.sm-ai-chat .chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem 0;
    scroll-behavior: smooth;
}

/* Welcome Screen */
.sm-ai-chat .welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 2rem;
}

.sm-ai-chat .welcome-icon {
    width: 80px;
    height: 80px;
    background: var(--ai-gradient);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 40px rgba(99, 102, 241, 0.3);
}

.sm-ai-chat .welcome-icon i {
    font-size: 2rem;
    color: white;
}

.sm-ai-chat .welcome-title {
    font-size: 1.75rem;
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 0.5rem;
}

.sm-ai-chat .welcome-subtitle {
    color: var(--ai-text-muted);
    font-size: 1rem;
    margin-bottom: 2rem;
}

.sm-ai-chat .suggestion-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    width: 100%;
    max-width: 600px;
}

.sm-ai-chat .suggestion-card {
    background: white;
    border: 1px solid var(--ai-border);
    border-radius: 12px;
    padding: 1rem;
    text-align: left;
    cursor: pointer;
    transition: all 0.2s ease;
}

.sm-ai-chat .suggestion-card:hover {
    border-color: var(--ai-primary);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.15);
    transform: translateY(-2px);
}

.sm-ai-chat .suggestion-card i {
    color: var(--ai-primary);
    margin-bottom: 0.5rem;
}

.sm-ai-chat .suggestion-card .title {
    font-weight: 600;
    font-size: 0.9rem;
    color: #1e293b;
}

.sm-ai-chat .suggestion-card .desc {
    font-size: 0.8rem;
    color: var(--ai-text-muted);
    margin-top: 0.25rem;
}

/* Message Styles */
.sm-ai-chat .message-wrapper {
    display: flex;
    margin-bottom: 1.5rem;
    animation: smAiFadeIn 0.3s ease;
}

@keyframes smAiFadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.sm-ai-chat .message-wrapper.user {
    justify-content: flex-end;
}

.sm-ai-chat .message-wrapper.assistant {
    justify-content: flex-start;
}

.sm-ai-chat .message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.sm-ai-chat .message-wrapper.user .message-avatar {
    background: var(--ai-user-bg);
    margin-left: 0.75rem;
    order: 2;
}

.sm-ai-chat .message-wrapper.assistant .message-avatar {
    background: var(--ai-gradient);
    margin-right: 0.75rem;
}

.sm-ai-chat .message-wrapper.assistant .message-avatar i {
    color: white;
    font-size: 0.9rem;
}

.sm-ai-chat .message-wrapper.user .message-avatar i {
    color: var(--ai-text-muted);
    font-size: 0.9rem;
}

.sm-ai-chat .message-content {
    max-width: 75%;
    padding: 1rem 1.25rem;
    border-radius: 16px;
    line-height: 1.6;
}

.sm-ai-chat .message-wrapper.user .message-content {
    background: var(--ai-gradient);
    color: white;
    border-bottom-right-radius: 4px;
}

.sm-ai-chat .message-wrapper.assistant .message-content {
    background: var(--ai-assistant-bg);
    color: #1e293b;
    border: 1px solid var(--ai-border);
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}

/* Typing Indicator */
.sm-ai-chat .typing-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--ai-text-muted);
    font-size: 0.9rem;
}

.sm-ai-chat .typing-step-wrapper {
    min-width: 160px;
}

.sm-ai-chat .typing-step-text {
    display: inline-block;
    font-weight: 500;
    color: var(--ai-primary);
}

.sm-ai-chat .typing-elapsed {
    font-size: 0.75rem;
    color: var(--ai-text-muted);
    margin-top: 0.5rem;
    min-height: 1rem;
}

.sm-ai-chat .typing-elapsed i {
    color: #f59e0b;
}

.sm-ai-chat .typing-dots {
    display: flex;
    gap: 4px;
}

.sm-ai-chat .typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--ai-primary-light);
    border-radius: 50%;
    animation: smAiBounce 1.4s infinite ease-in-out;
}

.sm-ai-chat .typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.sm-ai-chat .typing-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes smAiBounce {
    0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

/* Input Area */
.sm-ai-chat .chat-input-area {
    padding: 1rem 0 1.5rem;
    border-top: 1px solid var(--ai-border);
    background: transparent;
}

.sm-ai-chat .input-wrapper {
    display: flex;
    align-items: flex-end;
    background: white;
    border: 2px solid var(--ai-border);
    border-radius: 16px;
    padding: 0.5rem;
    transition: all 0.2s ease;
}

.sm-ai-chat .input-wrapper:focus-within {
    border-color: var(--ai-primary);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

.sm-ai-chat .input-wrapper textarea {
    flex: 1;
    border: none;
    outline: none;
    resize: none;
    padding: 0.75rem 1rem;
    font-size: 1rem;
    line-height: 1.5;
    max-height: 150px;
    background: transparent;
}

.sm-ai-chat .input-wrapper textarea::placeholder {
    color: var(--ai-text-muted);
}

.sm-ai-chat .send-button {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    border: none;
    background: var(--ai-gradient);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.sm-ai-chat .send-button:hover:not(:disabled) {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.sm-ai-chat .send-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.sm-ai-chat .send-button.stop {
    background: #ef4444;
}

.sm-ai-chat .send-button.stop:hover {
    box-shadow: 0 4px 12px rgba(239, 68, 68, 0.4);
}

.sm-ai-chat .disclaimer {
    text-align: center;
    font-size: 0.8rem;
    color: var(--ai-text-muted);
    margin-top: 0.75rem;
}

/* Error/Warning Messages */
.sm-ai-chat .message-error {
    background: #fef2f2 !important;
    border-color: #fecaca !important;
    color: #dc2626 !important;
}

.sm-ai-chat .message-error i {
    margin-right: 0.5rem;
}

/* Responsive */
@media (max-width: 768px) {
    .sm-ai-chat .message-content {
        max-width: 85%;
    }

    .sm-ai-chat .suggestion-cards {
        grid-template-columns: 1fr;
    }

    .sm-ai-chat .welcome-title {
        font-size: 1.5rem;
    }
}

/* Scrollbar Styling */
.sm-ai-chat .chat-messages::-webkit-scrollbar {
    width: 6px;
}

.sm-ai-chat .chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.sm-ai-chat .chat-messages::-webkit-scrollbar-thumb {
    background: var(--ai-border);
    border-radius: 3px;
}

.sm-ai-chat .chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--ai-text-muted);
}

/* ============================================================================
   Site-wide drawer (common/ask_ai_drawer.php). Mirrors the Custom Reports
   "Build with AI" drawer (css/custom-reports.css .cr-ai-drawer) on the right
   edge: same width, header gradient, message background and input styling, so
   the sidebar on the left stays visible while it is open.
   No backdrop on purpose - the page underneath stays fully interactive.
   z-index is a fallback only: js/common.js SmDrawers sets it inline on open so
   the most recently opened drawer is always on top.
   ============================================================================ */
.sm-ai-drawer {
    position: fixed;
    top: 65px; /* below the fixed navbar (.fixed-top height) so its actions stay reachable */
    right: 0;
    width: 420px;
    max-width: 100vw;
    height: calc(100vh - 65px);
    height: calc(100dvh - 65px);
    background: #ffffff;
    box-shadow: -8px 0 24px rgba(0,0,0,.08);
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    visibility: hidden;
    transition: transform .25s ease, visibility 0s linear .25s;
    z-index: 1041;
}
.sm-ai-drawer.open {
    transform: translateX(0);
    visibility: visible;
    transition: transform .25s ease, visibility 0s;
}
/* Re-opened on page load: no slide-in, it was already open on the last page. */
.sm-ai-drawer.sm-ai-no-transition { transition: none !important; }

.sm-ai-drawer-header {
    padding: 14px 16px;
    border-bottom: 1px solid #e6e8ec;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    background: linear-gradient(135deg, rgba(99,102,241,.06) 0%, rgba(139,92,246,.06) 100%);
    flex-shrink: 0;
}
.sm-ai-drawer-header strong { color: #1e293b; }
.sm-ai-drawer-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.sm-ai-drawer-actions .btn { white-space: nowrap; }

.sm-ai-drawer .chat-messages {
    padding: 14px 16px;
    background: #f8fafc;
}
/* The page centres the welcome block in a tall column; in the drawer it holds
   two groups of cards and must scroll rather than clip at the top. */
.sm-ai-drawer .welcome-screen {
    padding: 16px 8px;
    height: auto;
    min-height: 100%;
    justify-content: flex-start;
}
.sm-ai-drawer .sm-ai-group-label {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .04em;
    text-transform: uppercase;
    color: #64748b;
    text-align: left;
    margin-top: 8px;
}
.sm-ai-drawer .welcome-icon {
    width: 56px; height: 56px;
    margin-bottom: 10px;
    box-shadow: 0 10px 30px rgba(99,102,241,.3);
}
.sm-ai-drawer .welcome-icon i { font-size: 22px; }
.sm-ai-drawer .welcome-title { font-size: 1.1rem; }
.sm-ai-drawer .welcome-subtitle { font-size: .85rem; margin-bottom: 1rem; }
.sm-ai-drawer .suggestion-cards { grid-template-columns: 1fr; gap: 6px; }
.sm-ai-drawer .suggestion-card {
    display: flex; align-items: center; gap: 10px;
    padding: 9px 12px;
    border-radius: 10px;
}
.sm-ai-drawer .suggestion-card i { margin-bottom: 0; }
.sm-ai-drawer .suggestion-card .title { font-size: 12.5px; }
.sm-ai-drawer .suggestion-card .desc { font-size: 11.5px; margin-top: 0; }

.sm-ai-drawer .message-wrapper { margin-bottom: 12px; }
.sm-ai-drawer .message-avatar { width: 28px; height: 28px; }
.sm-ai-drawer .message-wrapper.user .message-avatar { margin-left: 8px; }
.sm-ai-drawer .message-wrapper.assistant .message-avatar { margin-right: 8px; }
.sm-ai-drawer .message-avatar i { font-size: .75rem !important; }
.sm-ai-drawer .message-content {
    max-width: 85%;
    padding: 9px 12px;
    border-radius: 12px;
    font-size: 13px;
    line-height: 1.45;
    word-break: break-word;
    overflow-x: auto;
}
.sm-ai-drawer .message-content table { font-size: 12px; }
.sm-ai-drawer .message-content p:last-child { margin-bottom: 0; }
/* Answers are laid out by the model with the Bootstrap grid for a full-width
   page. Breakpoint columns (col-sm-6, col-lg-4) size from the VIEWPORT, so in a
   420px panel a KPI card ends up a third of the bubble wide and its number wraps
   one character per line. Stack everything full width inside the drawer. */
.sm-ai-drawer .message-wrapper.assistant .message-content {
    max-width: calc(100% - 36px);
    word-break: normal;
    overflow-wrap: break-word;
}
.sm-ai-drawer .message-content .container,
.sm-ai-drawer .message-content .container-fluid {
    padding-left: 0;
    padding-right: 0;
    margin: 0 !important;
    max-width: 100%;
    width: 100%;
}
.sm-ai-drawer .message-content .row { --bs-gutter-x: 0.5rem; }
.sm-ai-drawer .message-content [class*="col-"],
.sm-ai-drawer .message-content .col {
    flex: 0 0 100%;
    max-width: 100%;
    width: 100%;
}
.sm-ai-drawer .message-content .card-body { padding: 0.75rem; }
.sm-ai-drawer .message-content h1,
.sm-ai-drawer .message-content h2,
.sm-ai-drawer .message-content h3 { font-size: 1.15rem; }
.sm-ai-drawer .message-content h4,
.sm-ai-drawer .message-content h5 { font-size: 1rem; }
.sm-ai-drawer .typing-indicator { font-size: .8rem; }
.sm-ai-drawer .typing-step-wrapper { min-width: 120px; }

.sm-ai-drawer .chat-input-area {
    padding: 10px 12px 14px;
    background: #fff;
    border-top: 1px solid #e6e8ec;
    flex-shrink: 0;
}
.sm-ai-drawer .input-wrapper {
    border-width: 1.5px;
    border-radius: 14px;
    padding: 6px;
    gap: 6px;
}
.sm-ai-drawer .input-wrapper:focus-within { box-shadow: 0 0 0 3px rgba(99,102,241,.12); }
.sm-ai-drawer .input-wrapper textarea { padding: 6px 8px; font-size: 13.5px; max-height: 120px; }
.sm-ai-drawer .send-button { width: 36px; height: 36px; border-radius: 10px; }
.sm-ai-drawer .disclaimer { font-size: 11px; margin-top: 6px; }

@media (max-width: 575.98px) {
    .sm-ai-drawer { width: 100vw; }
}
/* The header entry that opens the drawer is styled with the rest of the top
   navigation bar in css/style.css (.sm-topnav-ai). */
