/* chat: P6 chat workspace (top bar, thread, turns, evidence trail, composer, boot shell). Tokens only
   (scripts/ci/check-css-tokens.sh). Blazor sets --blazor-load-percentage(-text) inline on <html> during boot;
   the :root values are fallbacks for when it has not. */

:root {
    --blazor-load-percentage: 0%;
    --blazor-load-percentage-text: "";
    --vv-h: 100dvh;
}

/* ===== Workspace grid: mobile and tablet stack; desktop adds the evidence column (>= 1200 px) ===== */

.chat-workspace {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas:
        "top"
        "banners"
        "thread"
        "composer";
    height: calc(min(100dvh, var(--vv-h)) - var(--topbar-h) - env(safe-area-inset-top));
    min-height: 0;
}

.chat-workspace__banners {
    grid-area: banners;
}

.chat-workspace__banners:empty {
    display: none;
}

.chat-workspace__thread {
    position: relative;
    display: flex;
    flex-direction: column;
    grid-area: thread;
    min-height: 0;
}

.chat-workspace__thread > .thread {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.chat-workspace__composer {
    grid-area: composer;
    padding: var(--s-3) var(--s-4) max(var(--s-3), env(safe-area-inset-bottom));
    background-color: var(--ink-0);
    border-top: 1px solid var(--rule);
}

/* Mobile and tablet: the evidence panel is an overlay, so its wrapper takes no track. */
.chat-workspace__evidence {
    display: contents;
}

@media (min-width: 1200px) {
    .chat-workspace {
        height: 100%;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "top evidence"
            "banners evidence"
            "thread evidence"
            "composer evidence";
    }

    .chat-workspace__evidence {
        display: flex;
        flex-direction: column;
        grid-area: evidence;
        min-height: 0;
    }
}

/* ===== Top bar ===== */

.chat-top {
    grid-area: top;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--s-3);
    min-height: var(--topbar-h);
    padding: var(--s-2) var(--s-4);
    background-color: var(--ink-1);
    border-bottom: 1px solid var(--rule);
}

.chat-top__pickers {
    display: none;
}

.chat-top__spacer {
    flex: 1;
}

.chat-top__new {
    min-width: var(--hit);
    min-height: var(--hit);
}

/* Below 1200 px the chat's actions dock into the shell's single top bar (right side). DOM order puts them above
   the shell bar, which paints at the same z-index. */
@media (max-width: 1199.98px) {
    .chat-top {
        position: fixed;
        top: env(safe-area-inset-top);
        right: env(safe-area-inset-right);
        z-index: var(--z-topbar);
        flex-wrap: nowrap;
        gap: var(--s-1);
        height: var(--topbar-h);
        min-height: 0;
        padding: 0 var(--s-2) 0 0;
        background: none;
        border-bottom: 0;
    }

    .chat-top__spacer {
        display: none;
    }

    .chat-top__new {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: var(--tap);
        min-height: var(--tap);
        padding: 0;
        font: 400 var(--t-lg) / 1 var(--font-mono);
        border-color: transparent;
    }

    .chat-top .conn-pill {
        padding: 0 var(--s-2);
    }
}

/* Phones: Live is a dot; the label stays for assistive tech and returns when the link is not live. */
@media (max-width: 899.98px) {
    .chat-top .conn-pill--live .conn-pill__label {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }
}

@media (min-width: 900px) {
    .chat-top__pickers {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: var(--s-3);
    }
}

@media (min-width: 1200px) {
    .chat-top__new {
        display: none;
    }
}

/* ===== Pickers and connection pill ===== */

.scope-picker,
.model-picker {
    display: inline-block;
}

.scope-picker--up .menu__list,
.model-picker--up .menu__list {
    top: auto;
    bottom: calc(100% + var(--s-1));
}

.scope-chip__ver,
.scope-chip__state,
.menu__ver,
.menu__state {
    font: 400 var(--t-xs) / var(--lh-xs) var(--font-mono);
    color: var(--paper-lo);
}

.scope-chip__state {
    color: var(--caution);
}

.menu__state {
    margin-left: auto;
}

.menu__check {
    margin-left: auto;
    color: var(--accent);
}

.menu__item.is-unavailable {
    color: var(--paper-faint);
    cursor: not-allowed;
}

.model-chip {
    display: inline-flex;
    align-items: center;
    gap: var(--s-2);
    padding: var(--s-1) var(--s-3);
    font: 500 var(--t-sm) / var(--lh-sm) var(--font-ui);
    color: var(--paper);
    background-color: transparent;
    border: 1px solid var(--rule-strong);
    border-radius: var(--r-1);
    cursor: pointer;
}

.model-chip:hover {
    background-color: var(--ink-3);
}

.model-struck {
    text-decoration: line-through;
}

.chev {
    font-size: var(--t-xs);
    line-height: 1;
    color: var(--paper-lo);
}

.conn-pill {
    display: inline-flex;
    align-items: center;
    gap: var(--s-2);
    font: 500 var(--t-xs) / var(--lh-xs) var(--font-mono);
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--paper-lo);
}

.conn-pill__dot {
    width: var(--s-2);
    height: var(--s-2);
    background-color: currentColor;
    border-radius: var(--r-pill);
}

.conn-pill--live {
    color: var(--ok);
}

.conn-pill--reconnecting {
    color: var(--caution);
}

.conn-pill--reconnecting .conn-pill__dot {
    animation: conn-pulse 1.4s ease-in-out infinite;
}

.conn-pill--offline {
    color: var(--fault);
}

.conn-pill--offline .conn-pill__dot {
    background-color: transparent;
    border: 1px solid currentColor;
}

@keyframes conn-pulse {
    50% {
        opacity: .3;
    }
}

@media (max-width: 899.98px), (pointer: coarse) {
    .scope-chip,
    .model-chip {
        min-height: var(--hit);
    }

    .menu__item {
        min-height: var(--hit);
    }
}

/* ===== Banners (the shared .banner is fixed; here it sits in the flow) ===== */

.chat-banner {
    position: static;
    padding: var(--s-2) var(--s-4);
}

/* ===== Thread and turns ===== */

/* The thread column shares its edges with the composer box (same gutter, same max width). */
.chat-workspace__thread .thread__col {
    max-width: calc(var(--col-max) + 2 * var(--s-4));
    padding: var(--s-5) var(--s-4) var(--s-6);
}

.turn {
    padding: var(--s-5) 0;
    border-top: 1px solid var(--rule);
}

.turn:first-child {
    border-top: 0;
    padding-top: 0;
}

.turn__q {
    display: flex;
    flex-direction: column;
    gap: var(--s-1);
}

.turn__q-label {
    font: 500 var(--t-xs) / var(--lh-xs) var(--font-mono);
    letter-spacing: .08em;
    color: var(--paper-lo);
}

.turn__q-text {
    font: 500 var(--t-lg) / var(--lh-lg) var(--font-ui);
    color: var(--paper-hi);
    overflow-wrap: anywhere;
}

.turn__a {
    display: flex;
    flex-direction: column;
    gap: var(--s-3);
    margin-top: var(--s-3);
}

.turn__live {
    font: 400 var(--t-sm) / var(--lh-sm) var(--font-mono);
    color: var(--paper-lo);
}

.turn__note {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--s-3);
    margin: 0;
    font: 400 var(--t-sm) / var(--lh-sm) var(--font-ui);
    color: var(--paper);
}

.turn__note--caution {
    color: var(--caution);
}

.turn__note--fault {
    color: var(--fault);
}

.turn__retry {
    min-height: var(--hit);
}

.turn__meta-part {
    white-space: nowrap;
}

.turn__meta {
    margin: 0;
    font: 400 var(--t-xs) / var(--lh-xs) var(--font-mono);
    color: var(--paper-faint);
}

/* ===== Evidence trail ===== */

.trail {
    font: 400 var(--t-xs) / var(--lh-xs) var(--font-mono);
    color: var(--paper-lo);
    background-color: var(--ink-1);
    border: 1px solid var(--rule);
    border-radius: var(--r-1);
}

.trail__sum {
    display: flex;
    align-items: center;
    gap: var(--s-2);
    width: 100%;
    min-height: var(--s-6);
    padding: var(--s-2) var(--s-3);
    font: inherit;
    color: inherit;
    text-align: left;
    background: none;
    border: 0;
    cursor: pointer;
}

.trail__sum:hover {
    color: var(--paper-hi);
}

.trail__title {
    flex: 1;
}

.trail__status {
    color: var(--ok);
}

.trail__steps {
    margin: 0;
    padding: 0 var(--s-3) var(--s-2);
    list-style: none;
}

.trail__step {
    display: grid;
    grid-template-columns: var(--s-5) auto minmax(0, 1fr) auto var(--s-4);
    align-items: baseline;
    gap: var(--s-2);
    padding: var(--s-1) 0;
}

.trail__icon {
    color: var(--paper-faint);
    text-align: center;
}

.trail__tool {
    color: var(--paper);
}

.trail__what {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s-1) var(--s-2);
    min-width: 0;
    overflow-wrap: anywhere;
}

.trail__ref {
    color: var(--accent-ink);
    text-decoration: underline;
    text-underline-offset: .15em;
}

.trail__ms {
    color: var(--paper-lo);
}

.trail__glyph {
    color: var(--ok);
}

.trail__step.is-error .trail__glyph,
.trail__step.is-error .trail__what {
    color: var(--fault);
}

.trail__bar {
    grid-column: 1 / -1;
    position: relative;
    display: block;
    height: .125rem;
    overflow: hidden;
    background-color: var(--ink-3);
}

.trail__bar::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: 30%;
    background-color: var(--accent);
    animation: trail-run 1.2s var(--ease-out) infinite;
}

@keyframes trail-run {
    from {
        transform: translateX(-100%);
    }

    to {
        transform: translateX(340%);
    }
}

.trail__ref:focus-visible,
.trail__sum:focus-visible {
    outline: var(--focus-w) solid var(--accent);
    outline-offset: var(--focus-offset);
}

/* Touch at any width: summary row and reference links reach 44 px; a link's extra area is invisible. */
@media (pointer: coarse) {
    .trail__sum {
        min-height: var(--tap);
    }
}

@media (min-width: 900px) and (pointer: coarse) {
    .trail__ref {
        position: relative;
    }

    .trail__ref::after {
        content: "";
        position: absolute;
        inset: -.875rem -.25rem;
    }
}

@media (max-width: 899.98px) {
    .trail__sum {
        min-height: var(--tap);
    }

    /* Tool and duration on the first line; summary and reference chips wrap below. */
    .trail__step {
        grid-template-columns: var(--s-5) minmax(0, 1fr) auto var(--s-4);
        grid-template-areas:
            "icon tool ms glyph"
            ". what what what"
            "bar bar bar bar";
        row-gap: var(--s-1);
    }

    .trail__icon { grid-area: icon; }
    .trail__tool { grid-area: tool; }
    .trail__what { grid-area: what; }
    .trail__ms { grid-area: ms; text-align: right; }
    .trail__glyph { grid-area: glyph; }
    .trail__bar { grid-area: bar; }

    .trail__text {
        flex: 1 1 100%;
        overflow-wrap: anywhere;
    }

    .trail__ref {
        display: inline-flex;
        align-items: center;
        min-height: var(--tap);
        max-width: 100%;
        padding: 0 var(--s-2);
        overflow-wrap: anywhere;
        text-decoration: none;
        border: 1px solid var(--rule-strong);
        border-radius: var(--r-1);
    }
}

/* ===== Composer ===== */

.composer {
    display: flex;
    flex-direction: column;
    gap: var(--s-2);
    max-width: var(--col-max);
    margin: 0 auto;
    padding: var(--s-3);
    background-color: var(--ink-2);
    border: 1px solid var(--rule-strong);
    border-radius: var(--r-2);
}

.composer:focus-within {
    border-color: var(--accent);
}

/* One focus ring: the container's border. The textarea itself shows none. */
.composer__input:focus,
.composer__input:focus-visible {
    outline: none;
}

.composer__reason {
    margin: 0;
    font: 400 var(--t-xs) / var(--lh-xs) var(--font-mono);
    color: var(--caution);
}

.composer__input {
    width: 100%;
    min-height: var(--s-7);
    max-height: min(40vh, calc(var(--vv-h) * .4));
    padding: 0;
    overflow-y: auto;
    font: 400 var(--t-md) / var(--lh-md) var(--font-ui);
    color: var(--paper-hi);
    background-color: transparent;
    border: 0;
    resize: none;
}

.composer__input::placeholder {
    color: var(--paper-faint);
}

.composer__bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--s-2);
}

.composer__chips {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--s-2);
}

.composer__hint {
    margin-left: auto;
    font: 400 var(--t-xs) / var(--lh-xs) var(--font-mono);
    color: var(--paper-lo);
}

.composer__send {
    min-width: var(--hit);
    min-height: var(--hit);
}

/* Muted, not faded: a half-opacity accent button still reads as the strongest control in dark theme. */
.composer__send:disabled {
    color: var(--paper-lo);
    background-color: var(--ink-3);
    border-color: var(--rule);
    cursor: not-allowed;
}

@media (min-width: 900px) {
    .composer__chips {
        display: none;
    }
}

@media (max-width: 899.98px) {
    .chat-workspace__composer,
    .chat-boot .chat-boot__dock {
        padding: var(--s-2) var(--s-4) max(var(--s-2), env(safe-area-inset-bottom));
    }

    .composer {
        gap: var(--s-1);
        padding: var(--s-2) var(--s-3);
    }

    /* 16 px keeps iOS from zooming the page on focus. */
    .composer__input {
        max-height: min(30vh, calc(var(--vv-h) * .3));
        font-size: 1rem;
    }

    .composer__hint {
        display: none;
    }

    /* Scope, model and Send share one row on phones; the pickers truncate their labels. */
    .composer__bar {
        flex-wrap: nowrap;
    }

    .composer__chips {
        flex: 1 1 auto;
        flex-wrap: nowrap;
        min-width: 0;
    }

    .composer__chips > .menu,
    .composer__chips .scope-chip,
    .composer__chips .model-chip {
        min-width: 0;
    }

    .composer__chips .scope-chip__ver,
    .composer__chips .scope-chip__state {
        display: none;
    }

    .composer__chips .scope-chip__name,
    .composer__chips .model-chip__name {
        overflow: hidden;
        min-width: 0;
        max-width: 7.5rem;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .composer__send {
        flex: none;
    }
}

/* Short viewports (landscape phones, keyboard up): the composer must never outgrow the visual viewport, so Send stays
   reachable. Input and Send share a row, the textarea stops at about three lines, the pickers sit below, and under
   320 px of height they step aside. --vv-h is set from visualViewport by chat-workspace.js. */
@media (max-height: 500px) {
    .composer {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "reason reason"
            "input send"
            "chips chips";
        align-items: end;
        column-gap: var(--s-2);
    }

    .composer__bar {
        display: contents;
    }

    .composer__reason {
        grid-area: reason;
    }

    .composer__input {
        grid-area: input;
        align-self: center;
        min-height: var(--lh-md);
        max-height: min(calc(3 * var(--lh-md)), calc(var(--vv-h) * .3));
    }

    .composer__send {
        grid-area: send;
    }

    .composer__chips {
        grid-area: chips;
    }

    .composer__hint {
        display: none;
    }
}

@media (max-height: 320px) {
    .composer__chips {
        display: none;
    }
}

.composer__send-icon {
    font: 500 var(--t-sm) / var(--lh-sm) var(--font-ui);
}

/* ===== Jump to latest ===== */

.jump-pill {
    position: absolute;
    bottom: var(--s-4);
    left: 50%;
    z-index: var(--z-raised);
    min-height: var(--hit);
    padding: var(--s-2) var(--s-4);
    font: 500 var(--t-xs) / var(--lh-xs) var(--font-mono);
    color: var(--paper-hi);
    background-color: var(--ink-2);
    border: 1px solid var(--rule-strong);
    border-radius: var(--r-pill);
    box-shadow: var(--e-overlay);
    transform: translateX(-50%);
    cursor: pointer;
}

.jump-pill:hover {
    background-color: var(--ink-3);
}

/* ===== Empty state ===== */

.chat-empty__list {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--s-2);
    margin: 0;
    padding: 0;
    list-style: none;
}

.chat-empty__list > li {
    animation: chat-empty-in var(--d-med) var(--ease-out);
}

@keyframes chat-empty-in {
    from {
        opacity: 0;
    }
}

.chat-empty__ask {
    max-width: 100%;
    min-height: var(--hit);
    text-align: left;
    white-space: normal;
}

.chat-empty__note {
    font: 400 var(--t-xs) / var(--lh-xs) var(--font-mono);
    color: var(--paper-faint);
}

/* ===== Boot shell: static HTML shown until the island takes over ===== */

.chat-boot {
    display: flex;
    flex-direction: column;
    height: calc(min(100dvh, var(--vv-h)) - var(--topbar-h) - env(safe-area-inset-top));
}

@media (min-width: 1200px) {
    .chat-boot {
        height: 100%;
    }

    /* Stands in for the chat's top bar, which is in the flow from here up. */
    .chat-boot::before {
        content: "";
        display: block;
        flex: none;
        height: var(--topbar-h);
        background-color: var(--ink-1);
        border-bottom: 1px solid var(--rule);
    }
}

/* While the static boot shell is up, the island renders out of the flow (still laid out, so it can measure), so its
   arrival moves nothing; removing the shell then drops it into the same place. */
.chat-boot ~ .chat-workspace {
    position: absolute;
    width: 100%;
    visibility: hidden;
}

.chat-boot__thread {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: var(--s-4);
    width: 100%;
    max-width: calc(var(--col-max) + 2 * var(--s-4));
    margin: 0 auto;
    padding: var(--s-5) var(--s-4) var(--s-6);
}

/* Mirrors `h1` (base.css) in `.empty` (empty.css) without a second h1 on the page. */
.chat-boot__title {
    margin: 0;
    font-family: var(--font-display);
    font-size: calc(var(--t-xl) * 1.6);
    font-weight: 800;
    line-height: 1.15;
    letter-spacing: .04em;
    text-transform: uppercase;
    color: var(--paper-hi);
}

.chat-boot__dock {
    position: relative;
    padding: var(--s-3) var(--s-4) max(var(--s-3), env(safe-area-inset-bottom));
    background-color: var(--ink-0);
    border-top: 1px solid var(--rule);
}


.chat-boot__send {
    min-width: var(--hit);
    min-height: var(--hit);
}

.chat-boot__label::after {
    content: var(--blazor-load-percentage-text);
}

.chat-boot__bar {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    height: .125rem;
    overflow: hidden;
    background-color: var(--ink-3);
}

.chat-boot__fill {
    display: block;
    width: var(--blazor-load-percentage);
    height: 100%;
    background-color: var(--accent);
    transition: width var(--d-med) var(--ease-out);
}

.chat-noscript {
    max-width: var(--col-max);
    margin: 0 auto;
    padding: 0 var(--s-5);
    font: 400 var(--t-sm) / var(--lh-sm) var(--font-ui);
    color: var(--paper-lo);
}

@media (prefers-reduced-motion: reduce) {
    .trail__bar::after,
    .conn-pill--reconnecting .conn-pill__dot {
        animation: none;
    }

    .chat-boot__fill {
        transition: none;
    }

    .chat-empty__list > li {
        animation: none;
    }
}
