/* ==========================================================================
   Chat tab — the full-size "Ask the tutor" surface from mockup 04.
   Tokens only. Every rule is namespaced .st-chat-*.
   ========================================================================== */

.st-chat {
    display: flex;
    min-width: 0;
}

/* The panel owns the height so the thread — not the page — is what scrolls;
   the composer then stays put while history moves behind it. */
.st-chat__panel {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: clamp(420px, calc(100vh - 330px), 860px);
}
@supports (height: 100dvh) {
    .st-chat__panel { height: clamp(420px, calc(100dvh - 330px), 860px); }
}

/* Head ------------------------------------------------------------------ */

.st-chat__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 20px;
    border-bottom: 1px solid var(--st-border-soft);
    background: var(--st-surface);
}
.st-chat__head-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.st-chat__title { font-size: 15px; font-weight: 700; }

/* Thread ---------------------------------------------------------------- */

.st-chat__body { position: relative; flex: 1 1 auto; min-height: 0; }
.st-chat__thread {
    height: 100%;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 20px;
}
.st-chat__thread:focus-visible { outline-offset: -2px; }
.st-chat__list { display: flex; flex-direction: column; gap: 14px; min-height: 100%; }

.st-chat__jump {
    position: absolute;
    left: 50%;
    bottom: 12px;
    transform: translateX(-50%);
    z-index: 2;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 40px;
    padding: 8px 16px;
    border: 0;
    border-radius: 999px;
    background: var(--st-ink);
    color: var(--st-on-ink);
    font-size: 12px;
    font-weight: 700;
    box-shadow: 0 6px 18px var(--st-shadow-tint);
}
.st-chat__jump[hidden] { display: none; }
.st-chat__jump svg { transform: translateY(1px); }
.st-chat__jump-text { white-space: nowrap; }

/* Messages -------------------------------------------------------------- */

.st-chat-msg {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-width: 75%;
    min-width: 0;
}
.st-chat-msg--user { align-self: flex-end; align-items: flex-end; }
.st-chat-msg--bot { align-self: flex-start; align-items: flex-start; }

.st-chat-msg__bubble {
    padding: 11px 14px;
    font-size: 14px;
    line-height: 1.5;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}
.st-chat-msg--user .st-chat-msg__bubble {
    background: var(--st-accent);
    color: var(--st-on-accent);
    border-radius: 12px 12px 2px 12px;
}
.st-chat-msg--bot .st-chat-msg__bubble {
    background: var(--st-bg);
    color: var(--st-ink);
    border-radius: 12px 12px 12px 2px;
}
.st-chat-msg__bubble strong { font-weight: 700; }
.st-chat-msg__bubble code {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 0.92em;
    padding: 1px 5px;
    border-radius: 5px;
    background: var(--st-fill);
}

.st-chat-msg__cites { display: flex; flex-wrap: wrap; gap: 6px; }
.st-chat-cite {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 10px;
    border-radius: 999px;
    border: 1px solid var(--st-border-soft);
    background: var(--st-surface-2);
    color: var(--st-accent);
    font-size: 12px;
    font-weight: 700;
    line-height: 1.3;
}
.st-chat-cite:hover { background: var(--st-fill); color: var(--st-accent-hover); }

.st-chat-msg__foot {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 30px;
}
.st-chat-msg--user .st-chat-msg__foot { flex-direction: row-reverse; }
.st-chat-msg__time { font-size: 12px; color: var(--st-ink-4); }

/* A reply that was stopped, or that the connection cut short. Said on the
   message itself rather than in a toast, because a reload still shows it. */
.st-chat-msg__flag {
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--st-warm-soft);
    color: var(--st-warm-ink);
    font-size: 11px;
    font-weight: 700;
    white-space: nowrap;
}

/* Actions stay in the tab order — they fade rather than disappear, so a
   keyboard user reveals them by focusing them. */
.st-chat-msg__actions {
    display: flex;
    gap: 2px;
    opacity: 0;
    transition: opacity 0.14s ease;
}
.st-chat-msg:hover .st-chat-msg__actions,
.st-chat-msg__actions:focus-within { opacity: 1; }
@media (hover: none) {
    .st-chat-msg__actions { opacity: 1; }
}
.st-chat-msg__action {
    width: 30px;
    height: 30px;
    min-height: 30px;
    padding: 0;
    border-radius: 8px;
    color: var(--st-ink-3);
}
.st-chat-msg__action.is-on { background: var(--st-accent-soft); color: var(--st-accent); }

/* Streaming reply -------------------------------------------------------- */

.st-chat-thinking { display: inline-flex; align-items: center; gap: 5px; padding: 2px 0; }
.st-chat-thinking__dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--st-ink-4);
    animation: st-chat-dot 1.1s ease-in-out infinite;
}
.st-chat-thinking__dot:nth-child(2) { animation-delay: 0.15s; }
.st-chat-thinking__dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes st-chat-dot {
    0%, 80%, 100% { opacity: 0.35; transform: translateY(0); }
    40% { opacity: 1; transform: translateY(-3px); }
}

.st-chat-stream__bubble { min-width: 52px; }
.st-chat-stream__status { font-weight: 600; }
/* Sits on the last line of the streamed text, so the answer reads as still
   being written rather than as having finished short. */
.st-chat-stream__caret {
    display: inline-block;
    width: 2px;
    height: 1em;
    margin-left: 2px;
    vertical-align: text-bottom;
    background: var(--st-accent);
    animation: st-chat-caret 1s steps(2, start) infinite;
}
@keyframes st-chat-caret {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
    .st-chat-thinking__dot, .st-chat-stream__caret { animation: none; }
}

/* Earlier history -------------------------------------------------------- */

.st-chat-older { display: flex; justify-content: center; padding-bottom: 2px; }
.st-chat-older[hidden] { display: none; }

/* Inline notes: what a turn did, or why it didn't ------------------------- */

.st-chat-note {
    display: flex;
    align-items: flex-start;
    gap: 9px;
    padding: 10px 12px;
    border-radius: var(--st-radius-card-sm);
    border: 1px solid var(--st-border-soft);
    background: var(--st-surface-2);
    font-size: 13px;
    line-height: 1.45;
}
.st-chat-note svg { flex: 0 0 auto; margin-top: 2px; color: var(--st-ink-3); }
.st-chat-note__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1 1 auto; }
.st-chat-note__text strong { font-weight: 700; }
.st-chat-note__text span { color: var(--st-ink-2); }
.st-chat-note__actions { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; flex-wrap: wrap; }
.st-chat-note--quiet { color: var(--st-ink-2); align-items: center; }
.st-chat-note--quiet svg { margin-top: 0; }
.st-chat-note--error {
    border-color: var(--st-danger-soft);
    background: var(--st-danger-soft);
}
.st-chat-note--error svg { color: var(--st-danger-ink); }
.st-chat-note--error .st-chat-note__text strong,
.st-chat-note--error .st-chat-note__text span { color: var(--st-danger-ink); }

/* Empty state ----------------------------------------------------------- */

.st-chat-empty {
    margin: auto;
    max-width: 540px;
    padding: 24px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    text-align: center;
}
.st-chat-empty__icon { width: 52px; height: 52px; border-radius: 14px; }
.st-chat-empty__title { font-size: 20px; line-height: 1.25; }
.st-chat-empty__text { font-size: 14px; color: var(--st-ink-2); line-height: 1.5; }
.st-chat-empty__prompts {
    width: 100%;
    margin-top: 6px;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}
.st-chat-empty__prompt {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 52px;
    padding: 12px 14px;
    text-align: left;
    font-size: 13px;
    font-weight: 600;
    color: var(--st-ink);
    transition: transform 0.14s ease, box-shadow 0.14s ease;
}
.st-chat-empty__prompt:hover { transform: var(--st-lift); box-shadow: var(--st-shadow-soft-hover); }
.st-chat-empty__prompt svg { color: var(--st-ink-4); flex: 0 0 auto; }

/* Composer -------------------------------------------------------------- */

.st-chat-composer {
    position: sticky;
    bottom: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px 20px 14px;
    border-top: 1px solid var(--st-border-soft);
    background: var(--st-surface);
}
.st-chat-composer__chips { display: flex; flex-wrap: wrap; gap: 6px; }
.st-chat-composer__row { display: flex; align-items: flex-end; gap: 10px; }
.st-chat-composer__input {
    flex: 1 1 auto;
    min-height: var(--st-tap);
    max-height: 168px;
    padding: 11px 14px;
    resize: none;
    overflow-y: auto;
    line-height: 1.4;
}
.st-chat-composer__send { flex: 0 0 auto; }
.st-chat-composer__stop { flex: 0 0 auto; }
/* `.st-btn` sets display, which outranks the user-agent [hidden] rule — these
   two swap places for the length of a turn, so they have to say it themselves. */
.st-chat-composer__send[hidden],
.st-chat-composer__stop[hidden],
.st-chat-composer__scope-clear[hidden] { display: none; }

.st-chat-composer__foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
}
.st-chat-composer__scope-group { display: flex; align-items: center; gap: 4px; min-width: 0; }
.st-chat-composer__scope { max-width: 100%; }
.st-chat-composer__scope-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.st-chat-composer__scope-clear { color: var(--st-ink-3); }
.st-chat-composer[aria-busy="true"] .st-chat-composer__input { opacity: 0.7; }

/* Focus picker ---------------------------------------------------------- */

.st-chat-scope__list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 44vh;
    overflow-y: auto;
    padding-right: 4px;
}
.st-chat-scope__row { align-items: flex-start; }
.st-chat-scope__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.st-chat-scope__text strong { font-weight: 700; }

.st-chat-focus__loading {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 18px 2px;
    color: var(--st-ink-2);
    font-size: 13px;
}
.st-chat-focus__none {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
    padding: 6px 2px 2px;
    font-size: 13px;
    line-height: 1.5;
    color: var(--st-ink-2);
}

/* Responsive ------------------------------------------------------------ */

@media (max-width: 900px) {
    .st-chat__panel { height: clamp(400px, calc(100vh - 290px), 780px); }
    @supports (height: 100dvh) {
        .st-chat__panel { height: clamp(400px, calc(100dvh - 290px), 780px); }
    }
    .st-chat-msg { max-width: 82%; }
}

@media (max-width: 760px) {
    .st-chat__panel { height: calc(100vh - 220px); border-radius: var(--st-radius-card-sm); }
    @supports (height: 100dvh) {
        .st-chat__panel { height: calc(100dvh - 220px); }
    }
    .st-chat__head { padding: 12px 14px; }
    .st-chat__thread { padding: 14px; }
    .st-chat-msg { max-width: 88%; }
    .st-chat-empty__prompts { grid-template-columns: minmax(0, 1fr); }
    .st-chat-composer {
        padding: 10px 14px calc(12px + env(safe-area-inset-bottom));
    }
    .st-chat-composer__foot .st-help { order: 2; width: 100%; }
}
