/* ==========================================================
   AI Chat Widget — KnowledgeFabric
   All classes prefixed kf-chat- to avoid Vuexy/Bootstrap clashes.
   Colors use Bootstrap 5 CSS custom properties so the widget
   automatically inherits whichever Vuexy theme is active.
   z-index 9999: above menu (3000) and navbar (2500),
                 below Vuexy template customizer (99999999).
   ========================================================== */

/* ----------------------------------------------------------
   Bubble toggle button
   ---------------------------------------------------------- */
.kf-chat-bubble {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    z-index: 9999;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: var(--bs-primary);
    color: #fff;
    font-size: 1.4rem;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.kf-chat-bubble:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 22px rgba(0, 0, 0, 0.28);
}

.kf-chat-bubble--active {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2), 0 0 0 4px rgba(var(--bs-primary-rgb, 105, 108, 255), 0.25);
}

/* ----------------------------------------------------------
   Chat panel
   ---------------------------------------------------------- */
.kf-chat-panel {
    position: fixed;
    bottom: 5rem;
    right: 1.5rem;
    z-index: 9999;
    width: 360px;
    max-height: 540px;
    border-radius: 14px;
    background: var(--bs-body-bg);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.16);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Initial state for animation */
    opacity: 0;
    transform: translateY(12px);
    transition: opacity 0.22s ease, transform 0.22s ease;
}

.kf-chat-panel--open {
    opacity: 1;
    transform: translateY(0);
}

/* ----------------------------------------------------------
   Header
   ---------------------------------------------------------- */
.kf-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background: var(--bs-primary);
    color: #fff;
    flex-shrink: 0;
}

.kf-chat-header-title {
    display: flex;
    align-items: center;
    font-weight: 600;
    font-size: 0.95rem;
    gap: 0.25rem;
}

.kf-chat-header-actions {
    display: flex;
    gap: 0.25rem;
}

.kf-chat-btn-icon {
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.85);
    width: 28px;
    height: 28px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1rem;
    transition: background 0.15s ease, color 0.15s ease;
}

.kf-chat-btn-icon:hover {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

/* ----------------------------------------------------------
   Messages area
   ---------------------------------------------------------- */
.kf-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    min-height: 0;
}

/* Welcome state */
.kf-chat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 1.25rem 0.5rem;
    color: var(--bs-body-color);
}

.kf-chat-welcome p {
    font-size: 0.9rem;
    line-height: 1.5;
    color: var(--bs-secondary-color, #6c757d);
}

/* Quick-action buttons */
.kf-quick-actions {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    width: 100%;
}

.kf-quick-btn {
    background: var(--bs-tertiary-bg, #f8f9fa);
    border: 1px solid var(--bs-border-color, #dee2e6);
    border-radius: 20px;
    padding: 0.4rem 0.85rem;
    font-size: 0.82rem;
    color: var(--bs-body-color);
    cursor: pointer;
    text-align: left;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.kf-quick-btn:hover {
    background: var(--bs-primary);
    border-color: var(--bs-primary);
    color: #fff;
}

/* Message bubbles */
.kf-chat-message {
    max-width: 82%;
    padding: 0.5rem 0.8rem;
    border-radius: 12px;
    font-size: 0.875rem;
    line-height: 1.5;
    word-wrap: break-word;
}

/* User messages are plain text — preserve line breaks typed by the user */
.kf-chat-message--user {
    align-self: flex-end;
    background: var(--bs-primary);
    color: #fff;
    border-bottom-right-radius: 3px;
    white-space: pre-wrap;
}

.kf-chat-message--assistant {
    align-self: flex-start;
    background: var(--bs-tertiary-bg, #f8f9fa);
    color: var(--bs-body-color);
    border-bottom-left-radius: 3px;
    border: 1px solid var(--bs-border-color, #dee2e6);
}

.kf-chat-message--error {
    align-self: flex-start;
    background: #fff3cd;
    color: #856404;
    border: 1px solid #ffc107;
    border-bottom-left-radius: 3px;
    font-size: 0.82rem;
    white-space: pre-wrap;
}

/* ----------------------------------------------------------
   Rendered markdown inside assistant bubbles
   Scoped tightly to avoid leaking into the rest of the page.
   ---------------------------------------------------------- */
.kf-chat-message--assistant p {
    margin: 0 0 0.5rem;
}
.kf-chat-message--assistant p:last-child {
    margin-bottom: 0;
}

.kf-chat-message--assistant ul,
.kf-chat-message--assistant ol {
    margin: 0.25rem 0 0.5rem;
    padding-left: 1.25rem;
}
.kf-chat-message--assistant li {
    margin-bottom: 0.2rem;
}
.kf-chat-message--assistant li:last-child {
    margin-bottom: 0;
}

.kf-chat-message--assistant code {
    font-family: ui-monospace, 'SFMono-Regular', Consolas, 'Liberation Mono', monospace;
    font-size: 0.82em;
    background: rgba(0, 0, 0, 0.06);
    border-radius: 3px;
    padding: 0.1em 0.35em;
}

.kf-chat-message--assistant pre {
    background: rgba(0, 0, 0, 0.06);
    border-radius: 6px;
    padding: 0.6rem 0.75rem;
    overflow-x: auto;
    margin: 0.4rem 0 0.5rem;
    font-size: 0.82em;
}
.kf-chat-message--assistant pre code {
    background: none;
    padding: 0;
    font-size: inherit;
    border-radius: 0;
}

.kf-chat-message--assistant strong { font-weight: 600; }
.kf-chat-message--assistant em     { font-style: italic; }

/* ----------------------------------------------------------
   Typing indicator (three bouncing dots)
   ---------------------------------------------------------- */
.kf-chat-typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0.5rem 1rem 0.25rem;
    flex-shrink: 0;
}

.kf-chat-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--bs-secondary-color, #6c757d);
    display: inline-block;
    animation: kf-chat-bounce 1.2s infinite ease-in-out;
}

.kf-chat-typing span:nth-child(1) { animation-delay: 0s; }
.kf-chat-typing span:nth-child(2) { animation-delay: 0.2s; }
.kf-chat-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes kf-chat-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30%            { transform: translateY(-5px); opacity: 1; }
}

/* ----------------------------------------------------------
   Input area
   ---------------------------------------------------------- */
.kf-chat-input-area {
    display: flex;
    align-items: flex-end;
    gap: 0.4rem;
    padding: 0.6rem 0.75rem;
    border-top: 1px solid var(--bs-border-color, #dee2e6);
    flex-shrink: 0;
    background: var(--bs-body-bg);
}

.kf-chat-input {
    flex: 1;
    border: 1px solid var(--bs-border-color, #dee2e6);
    border-radius: 8px;
    padding: 0.45rem 0.65rem;
    font-size: 0.875rem;
    resize: none;
    outline: none;
    background: var(--bs-body-bg);
    color: var(--bs-body-color);
    line-height: 1.45;
    max-height: 96px;
    overflow-y: auto;
    transition: border-color 0.15s ease;
    font-family: inherit;
}

.kf-chat-input:focus {
    border-color: var(--bs-primary);
    box-shadow: 0 0 0 2px rgba(var(--bs-primary-rgb, 105, 108, 255), 0.15);
}

.kf-chat-send-btn {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: var(--bs-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1rem;
    flex-shrink: 0;
    transition: opacity 0.15s ease;
}

.kf-chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.kf-chat-send-btn:not(:disabled):hover {
    opacity: 0.88;
}

/* ----------------------------------------------------------
   Scrollbar styling (messages area)
   ---------------------------------------------------------- */
.kf-chat-messages::-webkit-scrollbar {
    width: 4px;
}

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

.kf-chat-messages::-webkit-scrollbar-thumb {
    background: var(--bs-border-color, #dee2e6);
    border-radius: 4px;
}

/* ----------------------------------------------------------
   Responsive: on very small screens collapse panel width
   ---------------------------------------------------------- */
@media (max-width: 420px) {
    .kf-chat-panel {
        right: 0.75rem;
        left: 0.75rem;
        width: auto;
        max-height: calc(100dvh - 7rem);
    }
}

/* ----------------------------------------------------------
   Session-restore divider (shown when prior session messages
   are replayed after a page navigation)
   ---------------------------------------------------------- */
.kf-chat-restore-divider {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0.25rem 0;
    color: var(--bs-secondary-color, #6c757d);
    font-size: 0.72rem;
}

.kf-chat-restore-divider::before,
.kf-chat-restore-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--bs-border-color, #dee2e6);
}
