/* ── SOP assistant chat widget ─────────────────────────────────────────── */

#sop-chat-widget {
    position: fixed;
    right: 1.25rem;
    bottom: 1.25rem;
    z-index: 1080;
}

#sop-chat-toggle {
    width: 3.25rem;
    height: 3.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

#sop-chat-panel {
    position: fixed;
    right: 1.25rem;
    bottom: 5.25rem;
    width: min(22.5rem, calc(100vw - 2rem));
    height: min(30rem, calc(100vh - 7rem));
    display: flex;
    flex-direction: column;
    max-width: 100vw;
}

#sop-chat-form  { min-width: 0; }
#sop-chat-input { min-width: 0; }

#sop-chat-panel .card-header {
    background: #00AEEF;
    color: #fff;
}

#sop-chat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
}

.sop-chat-msg {
    display: flex;
    flex-direction: column;
    margin-bottom: .5rem;
}

.sop-chat-msg-user      { align-items: flex-end; }
.sop-chat-msg-assistant { align-items: flex-start; }

.sop-chat-bubble {
    max-width: 85%;
    padding: .4rem .65rem;
    border-radius: .75rem;
    white-space: pre-wrap;
    word-break: break-word;
    font-size: .875rem;
}

.sop-chat-msg-user .sop-chat-bubble {
    background: #00AEEF;
    color: #fff;
}

.sop-chat-msg-assistant .sop-chat-bubble {
    background: var(--bs-secondary-bg, #e9ecef);
}

.sop-chat-sources {
    font-size: .7rem;
    color: var(--bs-secondary-color, #6c757d);
    margin-top: .15rem;
}

/* ── Markdown inside assistant bubbles (md-block) ─────────────────────── */
.sop-chat-bubble md-block {
    display: block;
    white-space: normal;   /* the bubble is pre-wrap for plain text; Markdown supplies its own breaks */
}

.sop-chat-bubble md-block > :last-child { margin-bottom: 0; }

.sop-chat-bubble md-block p,
.sop-chat-bubble md-block ul,
.sop-chat-bubble md-block ol,
.sop-chat-bubble md-block pre,
.sop-chat-bubble md-block table { margin: 0 0 .45rem; }

.sop-chat-bubble md-block ul,
.sop-chat-bubble md-block ol { padding-left: 1.2rem; }

.sop-chat-bubble md-block li + li { margin-top: .15rem; }

.sop-chat-bubble md-block h1,
.sop-chat-bubble md-block h2,
.sop-chat-bubble md-block h3,
.sop-chat-bubble md-block h4 { font-size: .95rem; font-weight: 600; margin: .25rem 0 .35rem; }

.sop-chat-bubble md-block code {
    font-size: .8rem;
    background: rgba(0, 0, 0, .06);
    padding: .05rem .3rem;
    border-radius: .25rem;
}

.sop-chat-bubble md-block pre {
    white-space: pre-wrap;
    word-break: break-word;
    font-size: .8rem;
    padding: .4rem .5rem;
    background: rgba(0, 0, 0, .06);
    border-radius: .4rem;
}
.sop-chat-bubble md-block pre code { background: transparent; padding: 0; }

.sop-chat-bubble md-block table { font-size: .8rem; border-collapse: collapse; }
.sop-chat-bubble md-block th,
.sop-chat-bubble md-block td { border: 1px solid rgba(0, 0, 0, .12); padding: .15rem .4rem; }

.sop-chat-bubble md-block a { color: #0086b8; }

/* ── Wide result tables: scroll, never crush ───────────────────────────── */
/* The bubble breaks long words, which would shred table cells; the wrapper opts out and
   scrolls in both axes instead, so no row or column is ever hidden. */
.sop-chat-tablewrap {
    overflow: auto;
    max-height: 16rem;
    max-width: 100%;
    margin: 0 0 .45rem;
    border: 1px solid rgba(0, 0, 0, .12);
    border-radius: .4rem;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;   /* scrolling the table does not scroll the message list */
}

.sop-chat-tablewrap:focus-visible { outline: 2px solid #00AEEF; outline-offset: 1px; }

/* The server-built table lives in the message row, not inside the bubble's md-block, so it needs its
   own cell styling; the md-block rules above never reach it. */
.sop-chat-tablewrap table {
    margin: 0;
    border: 0;
    font-size: .8rem;
    border-collapse: collapse;
    width: max-content;
    min-width: 100%;
}

.sop-chat-tablewrap th,
.sop-chat-tablewrap td {
    white-space: nowrap;        /* columns keep their width; the wrapper scrolls */
    word-break: normal;
    padding: .2rem .55rem;
    border: 1px solid rgba(0, 0, 0, .12);
    border-width: 0 1px 1px 0;
}

.sop-chat-tablewrap th:last-child,
.sop-chat-tablewrap td:last-child { border-right: 0; }
.sop-chat-tablewrap tbody tr:last-child td { border-bottom: 0; }
.sop-chat-tablewrap thead th { font-weight: 600; }

.sop-chat-tablewrap thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--bs-body-bg, #fff);
    box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .12);
}

/* A message carrying a table gets the whole panel width — 85% wastes scarce space. The table is a
   sibling of the bubble, not a descendant, so :has() must be anchored on the message row. */
.sop-chat-msg-assistant:has(.sop-chat-tablewrap) .sop-chat-bubble { max-width: 100%; }
.sop-chat-msg-assistant .sop-chat-tablewrap { width: 100%; }

/* ── Data replies by shape (sop-chat.js renderTable) ───────────────────── */
.sop-chat-empty {
    max-width: 85%;
    margin: 0 0 .45rem;
    font-size: .8rem;
    font-style: italic;
    color: var(--bs-secondary-color, #6c757d);
}

.sop-chat-figures {
    display: flex;
    flex-wrap: wrap;
    gap: .5rem;
    max-width: 100%;
    margin: 0 0 .45rem;
}

.sop-chat-figure {
    display: flex;
    flex-direction: column;
    min-width: 5rem;
    padding: .4rem .7rem;
    border: 1px solid rgba(0, 0, 0, .12);
    border-radius: .5rem;
    background: rgba(0, 174, 239, .06);
}

.sop-chat-figure-value {
    font-size: 1.1rem;
    font-weight: 600;
    line-height: 1.25;
    word-break: break-word;
}

.sop-chat-figures-single .sop-chat-figure-value { font-size: 1.6rem; }

.sop-chat-figure-label {
    font-size: .7rem;
    color: var(--bs-secondary-color, #6c757d);
}

/* ── Expanded panel: a readable viewport for wide tables ───────────────── */
#sop-chat-panel.sop-chat-wide {
    width:  min(48rem, calc(100vw - 2rem));
    height: min(38rem, calc(100vh - 7rem));
}

#sop-chat-panel.sop-chat-wide .sop-chat-tablewrap { max-height: 22rem; }

/* ── Small screens: the panel becomes a full-screen sheet ─────────────── */
@media (max-width: 575.98px) {
    #sop-chat-panel {
        inset: 0;
        width: 100vw;
        height: 100vh;
        height: 100dvh;                 /* shrinks with the on-screen keyboard where supported */
        border-radius: 0;
        border: 0;
    }

    #sop-chat-panel .card-header {
        padding-top: max(.75rem, env(safe-area-inset-top));
    }

    #sop-chat-panel .card-footer {
        padding-bottom: max(.75rem, env(safe-area-inset-bottom));
    }

    /* Hide the launcher while the sheet is open so it doesn't float over the input. */
    #sop-chat-widget.sop-chat-open #sop-chat-toggle { display: none; }
}
