/*
 * The project assistant's floating panel (`_components/chat_panel.html`) and the conversation
 * fragments loaded into it (`project/_chat_*.html`). A file of its own because the panel is one
 * self-contained piece of the shell, and components.css is long past the point where adding to
 * it helps anyone find anything.
 *
 * Non-modal: no backdrop, nothing behind it is blocked, and it floats over the content's corner
 * rather than pushing the page aside, so opening it never reflows what the reader was looking at.
 * Under the toasts (1000), over everything that scrolls with the page.
 */

.chat-panel:not([hidden]) {
    position: fixed;
    z-index: 900;
    right: var(--space-4);
    bottom: var(--space-4);
    display: flex;
    flex-direction: column;
    width: min(28rem, calc(100vw - 2 * var(--space-4)));
    height: min(42rem, calc(100vh - var(--topbar-height) - 2 * var(--space-4)));
    /* Resizable from its corner, and movable by its header (`chat-panel.js`). The limits keep a
       resized panel big enough to hold the composer and small enough to stay on screen. */
    min-width: min(18rem, 100vw);
    min-height: min(20rem, 100vh);
    max-width: 100vw;
    max-height: 100vh;
    resize: both;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    box-shadow: var(--shadow-md);
    overflow: hidden;
}

/* The drag handle. `touch-action: none` hands a touch drag to the script instead of scrolling
   the page underneath. */
.chat-panel__header {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
    border-bottom: 1px solid var(--color-border);
    cursor: grab;
    touch-action: none;
    user-select: none;
}

.chat-panel__header:active {
    cursor: grabbing;
}

.chat-panel__title {
    flex: 1 1 auto;
    margin: 0;
    font-size: var(--font-size-md);
}

.chat-panel__close {
    flex: none;
}

/* What a question will be asked from. Small and muted: it is a caption on the composer, not a
   heading, but it has to be readable before the question is sent rather than only after. */
.chat-panel__page {
    padding: var(--space-2) var(--space-4);
    border-bottom: 1px solid var(--color-border-subtle);
    background: var(--color-bg-subtle);
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}

.chat-panel__page p {
    margin: 0;
}

.chat-panel__notice,
.chat-message__notice {
    margin-top: var(--space-1);
    padding: var(--space-1) var(--space-2);
    border: 1px solid var(--color-warning-border);
    border-radius: var(--radius-sm);
    background: var(--color-warning-bg);
    color: var(--color-warning);
    font-size: var(--font-size-sm);
}

/* The loaded body fills the rest of the panel; only the conversation scrolls, so Clear and the
   composer stay where they are however long the history grows. */
.chat-panel__content,
.chat-panel-body,
.chat-thread {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-height: 0;
}

.chat-loading {
    margin: 0;
    padding: var(--space-4);
    color: var(--color-text-muted);
}

.chat-clear {
    display: flex;
    justify-content: flex-end;
    padding: var(--space-2) var(--space-4) 0;
}

.chat-clear .pl-btn {
    height: var(--control-height-sm);
    font-size: var(--font-size-sm);
}

.chat-conversation {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: var(--space-3) var(--space-4);
    overscroll-behavior: contain;
}

.chat-empty {
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}

.chat-exchange + .chat-exchange {
    margin-top: var(--space-4);
}

.chat-message {
    overflow-wrap: anywhere;
}

/* The reader's own words, set apart from the answers by a tint rather than by alignment alone,
   so the distinction survives a narrow panel where both run full width. */
.chat-message--user {
    margin-left: var(--space-8);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    background: var(--color-primary-bg);
}

.chat-message--user p {
    margin: 0;
}

.chat-message__text {
    white-space: pre-wrap;
}

.chat-message--user .chat-message__context {
    margin-bottom: var(--space-1);
    color: var(--color-text-muted);
    font-size: var(--font-size-xs);
}

.chat-message--assistant {
    margin-top: var(--space-2);
}

.chat-message--failed {
    margin: var(--space-2) 0 0;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-danger-border);
    border-radius: var(--radius-md);
    background: var(--color-danger-bg);
    color: var(--color-danger);
    font-size: var(--font-size-sm);
}

.chat-activity {
    margin-top: var(--space-3);
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
    font-style: italic;
}

/* A poll replaces the activity every two seconds; `htmx-loading.js` would dim it for each one
   and make the line flicker while nothing about it changed. */
.chat-activity.is-loading {
    opacity: 1;
}

.chat-conversation .form-error {
    margin: var(--space-3) 0 0;
}

.chat-composer {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: var(--space-2);
    align-items: end;
    padding: var(--space-3) var(--space-4);
    border-top: 1px solid var(--color-border);
}

.chat-composer label {
    grid-column: 1 / -1;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
}

.chat-composer textarea {
    width: 100%;
    min-height: 4.5rem;
    max-height: 12rem;
    resize: vertical;
}

/* On a phone the panel takes the width and most of the height: a 28rem card in the corner would
   leave a strip of page beside it too narrow to use, and a conversation too short to read. */
@media (max-width: 48rem) {
    .chat-panel:not([hidden]) {
        right: var(--space-2);
        bottom: var(--space-2);
        left: var(--space-2);
        width: auto;
        height: min(80vh, calc(100vh - 2 * var(--space-2)));
        min-width: 0;
        resize: none;
    }

    .chat-panel__header {
        cursor: auto;
        touch-action: auto;
    }

    .chat-message--user {
        margin-left: var(--space-4);
    }
}

.chat-error {
    margin: 0 var(--space-4) var(--space-2);
}
