/* ==========================================================
   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);
}

/* A panel restored after a navigation (#794) was already open, so it must appear open
   rather than animate in again on every page load. Applied for one frame only. */
.kf-chat-panel--no-transition {
    transition: none;
}

/* ----------------------------------------------------------
   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:not(:disabled) {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

/* Maximise is closed while an answer is being written: a full-page navigation there would
   discard the turn, so the control says so rather than taking the click (#845). */
.kf-chat-btn-icon:disabled {
    opacity: 0.45;
    cursor: default;
}

/* ----------------------------------------------------------
   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; }

/* Tables in an answer.

   The bubble lives in a panel a few hundred pixels wide and a table does not wrap, so a wide
   one used to be drawn straight through the side of it. .kf-chat-table is the scroll box the
   engine wraps every rendered table in: the overflow stops at the bubble's edge, and a table
   too wide to fit is scrolled sideways rather than spilling out of the panel. */
.kf-chat-table {
    max-width: 100%;
    overflow-x: auto;
    margin: 0.4rem 0 0.5rem;
    -webkit-overflow-scrolling: touch;
}

.kf-chat-message--assistant table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82em;
}

.kf-chat-message--assistant th,
.kf-chat-message--assistant td {
    border: 1px solid var(--bs-border-color, #dee2e6);
    padding: 0.25rem 0.45rem;
    text-align: left;
    vertical-align: top;
    /* The floor under a column, and the reason the scroll box has something to scroll. Left to
       itself a table asked to fit 360px divides it by however many columns it was given, and a
       six-column answer comes out one character per line — inside the panel, and unreadable.
       Below this the table stops shrinking and starts overflowing, which is the outcome we can
       do something about. */
    min-width: 6em;
    /* And a name or path with nothing to break on breaks rather than setting the column's
       width on its own. */
    overflow-wrap: break-word;
}

.kf-chat-message--assistant th {
    font-weight: 600;
    background: rgba(0, 0, 0, 0.04);
}

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

/* Scoped to the dots' own wrapper, not to every span under the indicator: the progress label
   beside them is a span too, and a descendant selector turned it into a fourth bouncing dot. */
.kf-chat-typing-dots 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-dots span:nth-child(1) { animation-delay: 0s; }
.kf-chat-typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.kf-chat-typing-dots 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);
    }
}

/* ----------------------------------------------------------
   Divider — a labelled rule between message groups. Used where
   the transcript on screen is not simply one continuous
   exchange: after a stored conversation is restored, and where
   older turns were trimmed to fit the model's context window.
   Both are announced rather than left for the user to infer.
   ---------------------------------------------------------- */
.kf-chat-divider {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0.25rem 0;
    color: var(--bs-secondary-color, #6c757d);
    font-size: 0.72rem;
    text-align: center;
}

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

/* ----------------------------------------------------------
   Proposal card (#777) — a change the assistant has proposed
   and nobody has agreed to yet. Deliberately not styled like a
   message: it is the only thing in the transcript that does
   something when clicked, and it has to read as a form rather
   than as more prose from the assistant.
   ---------------------------------------------------------- */
.kf-chat-proposal {
    align-self: stretch;
    margin: 0.25rem 0;
    padding: 0.75rem;
    border: 1px solid var(--bs-border-color, #dee2e6);
    border-left: 3px solid var(--bs-primary, #2c7be5);
    border-radius: 0.5rem;
    background: var(--bs-body-bg, #fff);
    font-size: 0.8rem;
}

.kf-chat-proposal-head {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    margin-bottom: 0.35rem;
    color: var(--bs-primary, #2c7be5);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    font-size: 0.68rem;
}

.kf-chat-proposal-summary {
    margin: 0 0 0.5rem;
    font-weight: 500;
}

.kf-chat-proposal-facts {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.15rem 0.5rem;
    margin: 0 0 0.5rem;
}

.kf-chat-proposal-facts dt {
    color: var(--bs-secondary-color, #6c757d);
    font-weight: 400;
}

.kf-chat-proposal-facts dd {
    margin: 0;
    word-break: break-word;
}

/* The conditions themselves, and — where a signal is being replaced — the ones
   it currently has. Both are rendered; a card that showed only the new half
   would be asking the user to approve a change they cannot see the shape of. */
.kf-chat-proposal-conditions {
    margin: 0 0 0.5rem;
}

.kf-chat-proposal-conditions-label {
    color: var(--bs-secondary-color, #6c757d);
    margin-bottom: 0.2rem;
}

.kf-chat-proposal-condition {
    padding: 0.15rem 0.4rem;
    border-radius: 0.25rem;
    background: var(--bs-tertiary-bg, #f8f9fa);
    margin-bottom: 0.15rem;
    font-family: var(--bs-font-monospace, monospace);
    font-size: 0.72rem;
    word-break: break-word;
}

.kf-chat-proposal--replaced .kf-chat-proposal-condition {
    text-decoration: line-through;
    opacity: 0.7;
}

.kf-chat-proposal-join {
    color: var(--bs-secondary-color, #6c757d);
    font-size: 0.7rem;
    margin: 0.1rem 0;
}

.kf-chat-proposal-actions {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.5rem;
}

.kf-chat-proposal-outcome {
    margin-top: 0.5rem;
    color: var(--bs-secondary-color, #6c757d);
}

.kf-chat-proposal-outcome--error {
    color: var(--bs-danger, #ea5455);
}

/* The analysis card's reasons-and-cost list (#778). Bulleted rather than run
   into a paragraph because each line is a separate fact the server checked,
   and the last of them is the cost — which has to be as readable as the
   reasons for saying yes, not buried behind them. */
.kf-chat-proposal-notes {
    margin: 0 0 0.5rem;
    padding-left: 1rem;
    color: var(--bs-secondary-color, #6c757d);
}

.kf-chat-proposal-notes li {
    margin-bottom: 0.1rem;
}

/* The "watch it run" link, left in the transcript after a run is started so
   somebody who navigates away still has the way back to it. */
.kf-chat-proposal-link {
    align-self: stretch;
    margin: 0.25rem 0;
    font-size: 0.8rem;
}

/* ----------------------------------------------------------
   Progress label (#780) — what the assistant is doing while
   it is doing it. The dots say "something is happening"; the
   label says what, which is the half that makes a grounded
   answer's multi-second wait explicable rather than broken.
   ---------------------------------------------------------- */
.kf-chat-typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.kf-chat-progress-label {
    font-size: 0.75rem;
    color: var(--bs-secondary-color, #6c757d);
    line-height: 1.2;
}

.kf-chat-progress-label:empty {
    display: none;
}

/* ----------------------------------------------------------
   Grounding notice (#780/#810) — stated once, at the top of
   the transcript, when the assistant cannot read this
   installation's data. Styled as a standing condition rather
   than as an error: nothing has gone wrong, and it is not
   going to change while the user reads it.
   ---------------------------------------------------------- */
.kf-chat-notice {
    align-self: stretch;
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    padding: 0.55rem 0.7rem;
    margin-bottom: 0.25rem;
    border: 1px solid var(--bs-border-color, #dee2e6);
    border-left: 3px solid var(--bs-warning, #ffc107);
    border-radius: 8px;
    background: var(--bs-tertiary-bg, #f8f9fa);
    color: var(--bs-body-color);
    font-size: 0.78rem;
    line-height: 1.45;
}

.kf-chat-notice > i {
    color: var(--bs-warning, #ffc107);
    margin-top: 0.1rem;
    flex-shrink: 0;
}

.kf-chat-notice-body {
    /* The prose and the model name stack; only the icon sits beside them. */
    min-width: 0;
}

.kf-chat-notice-model {
    display: block;
    margin-top: 0.2rem;
    font-family: ui-monospace, 'SFMono-Regular', Consolas, 'Liberation Mono', monospace;
    font-size: 0.72rem;
    color: var(--bs-secondary-color, #6c757d);
}

/* ----------------------------------------------------------
   Entity chips (#780) — a link in a grounded answer that
   points at something real in this installation. Rendered as
   a chip rather than as underlined prose because it is a
   different kind of thing from a word: it is the object the
   sentence is about, and one click away.

   Only links whose path matches a shape the deep-link builder
   produces get this treatment, so a chip is always a promise
   the product can keep. Everything else stays an ordinary link.
   ---------------------------------------------------------- */
.kf-chat-message--assistant a {
    color: var(--bs-primary);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.kf-chat-entity {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.05rem 0.45rem 0.05rem 0.35rem;
    border: 1px solid var(--bs-border-color, #dee2e6);
    border-radius: 999px;
    background: var(--bs-body-bg, #fff);
    /* Overrides the underline above: the chip's own shape is the affordance. */
    text-decoration: none;
    font-size: 0.94em;
    line-height: 1.5;
    white-space: nowrap;
    /* Long asset names must still wrap out of a narrow panel rather than widen it. */
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    vertical-align: baseline;
}

.kf-chat-entity:hover {
    background: var(--bs-tertiary-bg, #f8f9fa);
    text-decoration: none;
}

.kf-chat-entity > i {
    font-size: 0.9em;
    opacity: 0.75;
    flex-shrink: 0;
}

/* Kind carries the colour, so a person and an asset are distinguishable at a glance
   without reading either. */
.kf-chat-entity--holder   { color: var(--bs-primary); }
.kf-chat-entity--asset    { color: var(--bs-info, #03c3ec); }
.kf-chat-entity--signal   { color: var(--bs-success, #71dd37); }
.kf-chat-entity--source   { color: var(--bs-secondary, #8592a3); }
.kf-chat-entity--manifest { color: var(--bs-secondary, #8592a3); }
.kf-chat-entity--report   { color: var(--bs-primary); }

/* ----------------------------------------------------------
   Full-screen chat page (#780) — the same engine and the same
   message vocabulary as the panel, laid out for a page that is
   always open and as tall as the viewport allows.
   ---------------------------------------------------------- */
.kf-chat-page {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 14rem);
    min-height: 24rem;
    overflow: hidden;
}

.kf-chat-page-messages {
    flex: 1;
}

/* The panel is narrow and its bubbles are capped so they read as speech. A full page is not,
   and the same cap would leave an answer as a thin column down the left. */
.kf-chat-page .kf-chat-message {
    max-width: 68ch;
}

.kf-chat-page .kf-chat-input {
    max-height: 160px;
}

/* ----------------------------------------------------------
   Conversation picker (#787)

   Takes the transcript's place while it is up rather than
   floating over it: the panel is 360px wide, and a drawer with
   a scrim in that space is more chrome than content. The page
   uses the same markup and the same rules — one picker, drawn
   by the engine both surfaces share.
   ---------------------------------------------------------- */
.kf-chat-threads {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    background: var(--bs-body-bg);
}

.kf-chat-threads-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    padding: 0.6rem 1rem;
    font-size: 0.85rem;
    font-weight: 600;
    border-bottom: 1px solid var(--bs-border-color, #dee2e6);
}

.kf-chat-threads-close {
    background: transparent;
    border: none;
    color: var(--bs-secondary-color, #6c757d);
    width: 24px;
    height: 24px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s ease;
}

.kf-chat-threads-close:hover {
    background: var(--bs-tertiary-bg, #f8f9fa);
}

.kf-chat-threads-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.35rem;
    /* The floating panel is sized by whatever is in its body, so without a floor a picker
       holding two conversations shrinks the whole panel to the height of two rows and grows it
       back on the way out. The page's card has a height of its own and is unaffected. */
    min-height: 12rem;
}

/* Nothing to list, or a list that could not be read. Said in the space the rows would have
   filled, because an empty box reads as "you have never chatted". */
.kf-chat-threads-empty {
    padding: 1.5rem 1rem;
    text-align: center;
    font-size: 0.85rem;
    color: var(--bs-secondary-color, #6c757d);
}

/* One conversation: the row that opens it, and the button that ends it. */
.kf-chat-thread {
    display: flex;
    align-items: stretch;
    border-radius: 8px;
}

.kf-chat-thread:hover {
    background: var(--bs-tertiary-bg, #f8f9fa);
}

.kf-chat-thread--current {
    background: rgba(var(--bs-primary-rgb, 105, 108, 255), 0.12);
}

.kf-chat-thread-open {
    flex: 1;
    /* Without this a long title widens the row instead of ellipsising inside it. */
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    background: transparent;
    border: none;
    color: inherit;
    text-align: start;
    padding: 0.5rem 0.6rem;
    cursor: pointer;
}

.kf-chat-thread-title {
    font-size: 0.85rem;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.kf-chat-thread--current .kf-chat-thread-title {
    font-weight: 600;
}

.kf-chat-thread-time {
    font-size: 0.72rem;
    color: var(--bs-secondary-color, #6c757d);
}

.kf-chat-thread-delete {
    flex-shrink: 0;
    width: 34px;
    background: transparent;
    border: none;
    border-radius: 8px;
    color: var(--bs-secondary-color, #6c757d);
    cursor: pointer;
    transition: color 0.15s ease, background 0.15s ease;
}

.kf-chat-thread-delete:hover {
    color: var(--bs-danger, #ff3e1d);
    background: var(--bs-tertiary-bg, #f8f9fa);
}
