body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    /* Notched phones: keep the chat input above the home-indicator area */
    padding-bottom: env(safe-area-inset-bottom);
}

/* --nav-h is measured by App._trackNavHeight (fallback for the first paint);
   #app carries mt-3 = 1rem, so it is subtracted too or the page overflows. */
:root { --nav-h: 57px; }
#app {
    min-height: calc(100vh - var(--nav-h) - 1rem);
}

/* ---- Home dashboard ------------------------------------------------------ */

.home-wrap {
    max-width: 960px;
}

/* Room to breathe around the primary action: this is the button the page
   exists for, so it must not sit flush against the subtitle or the stats. */
.home-chat-cta {
    margin-top: 0.25rem;
    margin-bottom: 0.5rem;
}

.home-stat {
    transition: transform 0.1s;
}

.home-stat:hover {
    transform: translateY(-2px);
}

.home-stat-icon {
    font-size: 1.4rem;
    color: var(--bs-primary);
    line-height: 1;
}

.home-stat-sub {
    color: var(--bs-success-text-emphasis);
}

/* Chat page wrapper. 100dvh (dynamic viewport) tracks the mobile URL bar and
   virtual keyboard so the input bar stays visible; 100vh is the fallback. The
   safe-area inset is already added to the body as padding-bottom, so subtract
   it here too — otherwise the two stack and the page overflows by that inset
   on notched phones. Same --nav-h + 1rem (#app's mt-3) budget as #app, plus
   a little slack so no rounding ever yields a page scrollbar. */
.chat-wrap {
    height: calc(100vh - var(--nav-h) - 1rem - 6px);
    height: calc(100dvh - var(--nav-h) - 1rem - 6px - env(safe-area-inset-bottom));
    max-width: 900px;
}

#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.msg {
    margin-bottom: 0.75rem;
    padding: 0.5rem 0.75rem;
    border-radius: 0.5rem;
    max-width: 85%;
    /* Long unbroken tokens (URLs, paths, base64) must wrap, not force the
       whole chat to scroll horizontally on narrow screens. */
    overflow-wrap: anywhere;
    word-break: break-word;
    min-width: 0;
}

.msg-user {
    background: var(--bs-primary);
    color: white;
    margin-left: auto;
}

.msg-assistant {
    /* Theme-adaptive: secondary-bg is a light grey in light mode and a dark
       grey in dark mode, so the bubble stays readable in both themes. */
    background: var(--bs-secondary-bg);
    color: var(--bs-body-color);
    border: 1px solid var(--bs-border-color);
}

.msg-error {
    background: var(--bs-danger-bg-subtle);
    color: var(--bs-danger-text-emphasis);
}

/* "Answering with X because your default model is down." Not a failure — the
   turn succeeded — so it is warning-coloured, not danger, and full width:
   it explains the answer below it rather than being part of the conversation. */
.msg-notice {
    background: var(--bs-warning-bg-subtle);
    color: var(--bs-warning-text-emphasis);
    max-width: 100%;
    font-size: 0.9em;
}

/* How full the model's context got this turn. Deliberately the quietest thing in
   the bubble — it is a fact about the machine, not about the answer — until it
   matters: amber when the turn came close to the window, red when the executor
   actually had to compress the oldest tool results to fit. */
.msg-context {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    margin-top: 0.35rem;
    font-size: 0.72rem;
    opacity: 0.55;
}
.msg-context .ctx-bar {
    display: inline-block;
    width: 56px;
    height: 4px;
    border-radius: 2px;
    background: var(--bs-border-color);
    overflow: hidden;
}
.msg-context .ctx-bar i {
    display: block;
    height: 100%;
    width: 0;
    background: currentColor;
}
.msg-context.ctx-warn {
    opacity: 0.8;
    color: var(--bs-warning-text-emphasis, #997404);
}
.msg-context.ctx-cut {
    opacity: 1;
    color: var(--bs-danger-text-emphasis, #b02a37);
}

/* A thinking model's chain-of-thought: collapsed, above the tool calls. Quieter
   than a tool call (dashed, no fill) — it is context, not something that ran. */
.msg-reasoning {
    border: 1px dashed var(--bs-border-color);
    border-radius: 6px;
    font-size: 0.82rem;
    margin-bottom: 0.4rem;
    opacity: 0.85;
}

.msg-reasoning > summary {
    cursor: pointer;
    padding: 0.3rem 0.5rem;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.35rem;
    user-select: none;
}
.msg-reasoning > summary::-webkit-details-marker {
    display: none;
}
.msg-reasoning > summary::before {
    content: "\25B8";  /* ▸ */
    font-size: 0.7rem;
    opacity: 0.6;
}
.msg-reasoning[open] > summary::before {
    content: "\25BE";  /* ▾ */
}
.msg-reasoning.reasoning-live > summary {
    /* Still thinking: the label pulses so a long silent block doesn't read as
       a stall. Honors a reduced-motion preference below. */
    animation: reasoning-pulse 1.4s ease-in-out infinite;
}
@keyframes reasoning-pulse {
    50% { opacity: 0.5; }
}
@media (prefers-reduced-motion: reduce) {
    .msg-reasoning.reasoning-live > summary { animation: none; }
}

.reasoning-body {
    padding: 0.35rem 0.6rem 0.5rem;
    border-top: 1px dashed var(--bs-border-color);
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 0.78rem;
    opacity: 0.9;
    max-height: 300px;
    overflow: auto;
}

/* Interleaved answer flow: .msg-content text segments and .tool-calls groups
   appended in turn order (text → tools → text → …) inside the bubble */
.msg-flow > .tool-calls {
    margin: 0.4rem 0;
}
.msg-flow > .tool-calls:first-child {
    margin-top: 0;
}

/* Inline expandable tool calls (rendered in the chat, nesting recursively) */
.tool-calls {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    margin-bottom: 0.4rem;
}
.tool-calls:empty {
    display: none;
}

.tool-call {
    border: 1px solid var(--bs-border-color);
    border-radius: 6px;
    background: var(--bs-tertiary-bg);
    font-size: 0.82rem;
}

.tool-call > summary {
    cursor: pointer;
    padding: 0.3rem 0.5rem;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.35rem;
    user-select: none;
}
.tool-call > summary::-webkit-details-marker {
    display: none;
}
.tool-call > summary::before {
    content: "\25B8";  /* ▸ */
    font-size: 0.7rem;
    opacity: 0.6;
}
.tool-call[open] > summary::before {
    content: "\25BE";  /* ▾ */
}
.tool-call .tc-name {
    font-weight: 600;
}
.tool-call .tc-arrow {
    opacity: 0.6;
}

.tool-call-body {
    padding: 0.35rem 0.6rem 0.5rem;
    border-top: 1px solid var(--bs-border-color);
}
.tc-args {
    font-family: monospace;
    font-size: 0.75rem;
    opacity: 0.75;
    white-space: pre-wrap;
    word-break: break-word;
    margin-bottom: 0.3rem;
    max-height: 160px;
    overflow: auto;
}
/* Scoped higher than `.msg-assistant pre` so the result box doesn't inherit
   the message code-block border/background/padding (avoids a nested box). */
.tool-call .tc-result {
    white-space: pre-wrap;
    max-height: 260px;
    overflow: auto;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    font-size: 0.78rem;
}

/* Nested sub-agent trace inside a call_agent step */
.sub-agent {
    border-left: 2px solid var(--bs-info-border-subtle, var(--bs-info));
    padding-left: 0.5rem;
    margin-top: 0.35rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}
.sub-agent-head {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--bs-info-text-emphasis, var(--bs-info));
}
.sub-agent-reply {
    font-style: italic;
    opacity: 0.8;
    white-space: pre-wrap;
    font-size: 0.78rem;
}
/* Live delegation: the reply div exists before any token arrives */
.sub-agent-reply:empty { display: none; }
.sub-agent-live > .sub-agent-head::after {
    content: '';
    display: inline-block;
    width: 0.45em;
    height: 0.45em;
    margin-left: 0.35em;
    border-radius: 50%;
    background: currentColor;
    animation: sub-agent-pulse 1.2s ease-in-out infinite;
}
@keyframes sub-agent-pulse {
    0%, 100% { opacity: 0.25; }
    50% { opacity: 1; }
}

/* Files a tool delivered to the chat (resource channel): rendered OUTSIDE the
   collapsed tool <details>, between the tool area and the answer. */
.msg-resources {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    align-items: flex-start;
    margin-bottom: 0.4rem;
}
.msg-resources:empty {
    display: none;
}
.res-thumb img {
    max-height: 200px;
    max-width: 100%;
    border-radius: 6px;
    border: 1px solid var(--bs-border-color);
    display: block;
}
.res-card {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.35rem 0.6rem;
    border: 1px solid var(--bs-border-color);
    border-radius: 6px;
    background: var(--bs-tertiary-bg);
    font-size: 0.82rem;
    text-decoration: none;
    color: inherit;
    max-width: 100%;
}
.res-card:hover {
    border-color: var(--bs-primary);
    color: inherit;
}
.res-card .res-title {
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.res-card .res-hint {
    font-size: 0.72rem;
    opacity: 0.65;
    white-space: nowrap;
}
.res-missing {
    font-size: 0.78rem;
    opacity: 0.6;
    font-style: italic;
    border: 1px dashed var(--bs-border-color);
    border-radius: 6px;
    padding: 0.3rem 0.55rem;
}
/* Inline HTML preview: a collapsible block styled like a tool call, with a
   sandboxed iframe as its body. Takes the full strip width. */
.res-html {
    flex-basis: 100%;
    border: 1px solid var(--bs-border-color);
    border-radius: 6px;
    background: var(--bs-tertiary-bg);
    font-size: 0.82rem;
}
.res-html > summary {
    cursor: pointer;
    padding: 0.3rem 0.5rem;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.35rem;
    user-select: none;
}
.res-html > summary::-webkit-details-marker {
    display: none;
}
.res-html > summary::before {
    content: "\25B8";  /* ▸ */
    font-size: 0.7rem;
    opacity: 0.6;
}
.res-html[open] > summary::before {
    content: "\25BE";  /* ▾ */
}
.res-html .res-open {
    margin-left: auto;
    font-size: 0.78rem;
}
.res-html-body {
    border-top: 1px solid var(--bs-border-color);
    padding: 0.35rem;
}
.res-iframe {
    width: 100%;
    height: 320px;
    border: 0;
    display: block;
    border-radius: 4px;
    background: #fff;  /* a page without its own background stays readable in dark theme */
}
/* Model-embedded inline image (the restricted ![alt](_resources/…) rule) */
.msg-content .md-img {
    max-width: 100%;
    max-height: 320px;
    border-radius: 6px;
    border: 1px solid var(--bs-border-color);
    display: block;
    margin: 0.35rem 0;
}

.system-prompt-textarea {
    font-family: monospace;
    font-size: 0.9rem;
    min-height: 200px;
}

.card-agent {
    transition: transform 0.1s;
    cursor: pointer;
}

.card-agent:hover {
    transform: translateY(-2px);
}

/* The whole agent card is the link to its editor, and a card that navigates
   looks like a card that doesn't: the pencil next to the name is the only
   affordance saying so. Muted at rest (it's a hint, not an action — the real
   actions live in the footer) and lit on hover, where the pointer already is. */
.card-edit-hint {
    margin-left: .4rem;
    font-size: .8em;
    opacity: .35;
    transition: opacity 0.1s, color 0.1s;
}

.card-agent:hover .card-edit-hint {
    opacity: 1;
    color: var(--bs-primary);
}

/* Agent descriptions are prompt-facing text and range from a line to a
   paragraph: clamp them so one verbose agent doesn't stretch a whole row of
   cards. The full text stays in the tooltip. */
.card-desc {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* A native agent missing from the workspace: shown in place so it can be
   restored where it went missing. Only the body is dimmed — an Import button at
   half opacity would read as disabled. */
.card-ghost {
    border-style: dashed;
}

.card-ghost .card-body {
    opacity: 0.5;
}

/* ---- Agent form tabs ---------------------------------------------------- */

/* Five tabs don't fit a phone, and a wrapped .nav-tabs stops reading as a tab
   strip (two rows, only one of them joined to the panel). Scroll instead:
   flex-nowrap + overflow-x is the whole trick, and the scrollbar is hidden
   because the active tab is scrolled into view for you. */
.agent-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;                       /* Firefox */
    -webkit-overflow-scrolling: touch;
    /* A scrolling container clips the tabs' own 1px bottom border against the
       panel; carry the line on the container so it stays continuous. */
    border-bottom: var(--bs-nav-tabs-border-width) solid var(--bs-nav-tabs-border-color);
}

.agent-tabs::-webkit-scrollbar {
    display: none;
}

.agent-tabs .nav-link {
    white-space: nowrap;
}

/* State indicators in a tab label: the pane is hidden, its state is not. */
.tab-state:not(:empty) {
    margin-left: 0.25rem;
}

.tab-dot {
    display: inline-block;
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    vertical-align: middle;
    background: var(--bs-success);
}

/* Amber = saving would widen this agent's grants (see AgentsPage.refreshState). */
.tab-dot.tab-dot-warn {
    background: var(--bs-warning);
}

/* The tab a failed submit points at (see validateAcrossTabs). */
.agent-tabs .nav-link.tab-error,
.agent-tabs .nav-link.tab-error.active {
    color: var(--bs-danger);
}

@media (pointer: coarse) {
    /* Real touch target on the strip; Bootstrap's default row is ~38px tall. */
    .agent-tabs .nav-link {
        padding-block: 0.6rem;
    }
}

/* ---- Explained numeric fields (RangeField) ------------------------------
   Slider + number input pair. Bootstrap already themes .form-range's track with
   var(--bs-secondary-bg); only the thumb is a hardcoded #0d6efd, so it is the one
   colour worth overriding. Everything else here is layout. */
.range-field {
    margin-bottom: 0.25rem;
}

.range-field .rf-head {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.range-field .rf-head .form-label {
    flex: 1 1 auto;
    min-width: 0;
}

.range-field .rf-value {
    flex: 0 0 auto;
    font-variant-numeric: tabular-nums;  /* badge width stops twitching as you drag */
}

.range-field .rf-controls {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.range-field .form-range {
    flex: 1 1 auto;
    min-width: 0;
}

.range-field .rf-num {
    flex: 0 0 5.5rem;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* End-of-range labels, aligned to the track (not to the number box). */
.range-field .rf-ends {
    display: flex;
    justify-content: space-between;
    font-size: 0.72rem;
    color: var(--bs-secondary-color);
    padding-right: 6.1rem;
}

/* Reserve two lines: the sentence changes length on every move and the fields
   below must not jump around while dragging. */
.range-field .rf-desc {
    min-height: 2.4em;
}

.range-field .rf-more > summary {
    cursor: pointer;
    user-select: none;
}

/* Switched off (response_temperature inheriting) vs merely irrelevant
   (memory_threshold while memory is off): both dim, only the first is disabled. */
.range-field.rf-off .rf-controls,
.range-field.rf-off .rf-ends,
.range-field.rf-idle .rf-controls,
.range-field.rf-idle .rf-ends {
    opacity: 0.5;
}

/* Thumb follows the theme's primary, and the track shows how far along the range
   we are (--rf-pct is set inline by RangeField.refresh). */
.range-field .form-range::-webkit-slider-thumb {
    background-color: var(--bs-primary);
}

.range-field .form-range::-moz-range-thumb {
    background-color: var(--bs-primary);
}

.range-field .form-range::-webkit-slider-runnable-track {
    background-image: linear-gradient(to right,
        var(--bs-primary) 0 var(--rf-pct, 0%), transparent var(--rf-pct, 0%));
}

.range-field .form-range::-moz-range-progress {
    height: 0.5rem;
    border-radius: 1rem;
    background-color: var(--bs-primary);
}

@media (pointer: coarse) {
    /* A 16px thumb is not a touch target. Keep it centred on the .5rem track:
       margin-top = -(thumb - track) / 2. */
    .range-field .form-range {
        height: 2.25rem;
    }

    .range-field .form-range::-webkit-slider-thumb {
        width: 1.5rem;
        height: 1.5rem;
        margin-top: -0.5rem;
    }

    .range-field .form-range::-moz-range-thumb {
        width: 1.5rem;
        height: 1.5rem;
    }
}

@media (max-width: 576px) {
    /* iOS Safari auto-zooms inputs under 16px (same reason as
       .system-prompt-textarea above). */
    .range-field .rf-num {
        font-size: 1rem;
        flex-basis: 4.75rem;
    }

    .range-field .rf-ends {
        padding-right: 5.35rem;
    }
}

.spinner-chat {
    display: none;
}

.spinner-chat.active {
    display: inline-block;
}

/* Per-message timestamp */
.msg-time {
    font-size: 0.68rem;
    opacity: 0.55;
    margin-top: 3px;
    text-align: right;
}
.msg-assistant .msg-time {
    text-align: left;
}

/* Auto mode: which agent answered, shown as a header above the answer */
.msg-agent {
    font-size: 0.68rem;
    opacity: 0.55;
    margin-bottom: 4px;
}

/* Per-message actions: copy / markdown source on answers, edit on prompts,
   regenerate on the last answer. */
.msg-actions {
    display: flex;
    gap: 0.15rem;
    margin-top: 0.25rem;
}
.msg-user .msg-actions {
    justify-content: flex-end;
}
.msg-act {
    padding: 0.05rem 0.3rem;
    line-height: 1.15;
    font-size: 0.85rem;
    border: 0;
    background: transparent;
    color: inherit;
    opacity: 0.55;
}
.msg-act:hover,
.msg-act:focus-visible {
    opacity: 1;
    color: inherit;
    background: var(--bs-tertiary-bg);
}
.msg-user .msg-act:hover,
.msg-user .msg-act:focus-visible {
    background: rgba(255, 255, 255, 0.25);
}
/* Desktop: keep the bubbles clean until pointed at. Touch screens have no
   hover, so there the buttons stay visible (the query simply doesn't apply). */
@media (hover: hover) and (pointer: fine) {
    .msg-actions {
        opacity: 0;
        transition: opacity 0.12s ease-in-out;
    }
    .msg:hover .msg-actions,
    .msg-actions:focus-within {
        opacity: 1;
    }
}

/* Raw markdown of an answer, shown in place of the rendered version */
.msg-assistant .md-source {
    white-space: pre-wrap;
    font-size: 0.8rem;
    margin: 0;
}

/* Inline prompt editor (a user bubble turned into a form) */
.msg.editing {
    max-width: 100%;
    width: 100%;
}
.msg-edit {
    font-size: 0.9rem;
}
.msg-edit-note {
    font-size: 0.72rem;
    opacity: 0.85;
    margin-top: 0.25rem;
}

/* "Thinking" indicator (animated dots) shown while awaiting the first token */
.typing-indicator {
    display: inline-flex;
    gap: 5px;
    align-items: center;
    padding: 4px 2px;
}

.typing-indicator span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--bs-secondary-color, #888);
    opacity: 0.4;
    animation: typing-bounce 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* Markdown rendering inside assistant messages */
.msg-assistant .md-table {
    border-collapse: collapse;
    margin: 0.5rem 0;
    width: auto;
    max-width: 100%;
    font-size: 0.9rem;
}

.msg-assistant .md-table th,
.msg-assistant .md-table td {
    border: 1px solid var(--bs-border-color);
    padding: 0.3rem 0.6rem;
    text-align: left;
    vertical-align: top;
}

.msg-assistant .md-table thead th {
    background: var(--bs-tertiary-bg);
    font-weight: 600;
}

.msg-assistant .md-table tbody tr:nth-child(even) {
    background: var(--bs-tertiary-bg);
}

.msg-assistant table {
    display: block;
    overflow-x: auto;
}

/* Math. The vendored KaTeX emits MathML only, so there is no KaTeX stylesheet
   to load and nothing here restyles the formula itself — the browser draws it,
   inheriting the bubble's color. This only handles size and overflow. */
.msg-content .katex {
    /* Math renders in a serif face with a smaller x-height than the UI font,
       so at 1em it reads as fine print next to the prose around it. */
    font-size: 1.15em;
}

.msg-content .math-block {
    /* A wide equation scrolls inside its own box instead of widening the
       bubble; overflow-y stays hidden or the scrollbar clips tall fractions. */
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0.15rem 0;
    margin: 0.4rem 0;
    text-align: center;
}

/* KaTeX ships its own inline color:#cc0000 on parse errors — readable on both
   themes — but the source it echoes must not break the bubble's width. */
.msg-content .katex-error {
    white-space: pre-wrap;
    word-break: break-word;
}

.msg-assistant p:last-child {
    margin-bottom: 0;
}

.msg-assistant h1, .msg-assistant h2, .msg-assistant h3,
.msg-assistant h4, .msg-assistant h5, .msg-assistant h6 {
    margin: 0.6rem 0 0.3rem;
    font-weight: 600;
}

.msg-assistant h1 { font-size: 1.4rem; }
.msg-assistant h2 { font-size: 1.25rem; }
.msg-assistant h3 { font-size: 1.1rem; }
.msg-assistant h4, .msg-assistant h5, .msg-assistant h6 { font-size: 1rem; }

.msg-assistant ul, .msg-assistant ol {
    margin: 0.3rem 0;
    padding-left: 1.5rem;
}

.msg-assistant pre {
    background: var(--bs-tertiary-bg);
    border: 1px solid var(--bs-border-color);
    border-radius: 0.4rem;
    padding: 0.6rem 0.8rem;
    overflow-x: auto;
    margin: 0.5rem 0;
}

.msg-assistant pre code {
    background: none;
    padding: 0;
}

.msg-assistant code {
    background: var(--bs-tertiary-bg);
    padding: 0.1rem 0.35rem;
    border-radius: 0.25rem;
    font-size: 0.88em;
}

.msg-assistant blockquote {
    border-left: 3px solid var(--bs-border-color);
    margin: 0.5rem 0;
    padding: 0.1rem 0.8rem;
    color: var(--bs-secondary-color);
}

.msg-assistant a {
    color: var(--bs-info);
}

.msg-assistant hr {
    border: none;
    border-top: 1px solid var(--bs-border-color);
    margin: 0.6rem 0;
}

/* Toasts must never exceed a phone viewport */
.toast {
    max-width: calc(100vw - 2rem);
}

/* ---- Connector setup form: numbered steps, type cards, channel guide ----- */
/* The number is a CSS counter over .setup-step, so a step hidden with d-none
   (display:none generates no box, hence no increment) leaves no gap: a device
   channel shows 1-2-3, not 1-2-3-_-_. */
.setup-steps {
    counter-reset: setup-step;
}
.setup-step {
    counter-increment: setup-step;
    position: relative;
    padding: 1rem 1rem 0.25rem 3.25rem;
    margin-bottom: 1rem;
    border: 1px solid var(--bs-border-color);
    border-radius: 0.5rem;
}
.setup-step::before {
    content: counter(setup-step);
    position: absolute;
    left: 0.9rem;
    top: 0.95rem;
    width: 1.75rem;
    height: 1.75rem;
    border-radius: 50%;
    background: var(--bs-primary);
    color: #fff;
    font-weight: 600;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
.setup-step-title {
    font-size: 1rem;
    font-weight: 600;
    line-height: 1.75rem;
    margin-bottom: 0.75rem;
}
.type-cards {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.type-card {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    min-width: 8.5rem;
    padding: 0.6rem 1rem;
    border: 1px solid var(--bs-border-color);
    border-radius: 0.5rem;
    color: var(--bs-body-color);
}
.type-card i {
    font-size: 1.4rem;
    line-height: 1;
}
.type-card:hover {
    border-color: var(--bs-primary);
    color: var(--bs-body-color);
}
.type-card.active {
    border-color: var(--bs-primary);
    background: rgba(var(--bs-primary-rgb), 0.1);
    color: var(--bs-primary);
    box-shadow: inset 0 0 0 1px var(--bs-primary);
}
.setup-guide {
    padding: 0.6rem 0.9rem;
    border-left: 3px solid var(--bs-info);
    border-radius: 0.375rem;
    background: rgba(var(--bs-info-rgb), 0.08);
}
.setup-guide summary {
    cursor: pointer;
    font-weight: 600;
}
.setup-guide ol li {
    margin-bottom: 0.4rem;
}
.setup-advanced summary {
    cursor: pointer;
    color: var(--bs-secondary-color);
}

/* ---- Mobile / touch adjustments ---------------------------------------- */

@media (pointer: coarse) {
    /* Expanding tool traces is a primary chat interaction: give the summary
       rows and small table buttons a real touch target. */
    .tool-call > summary {
        padding: 0.55rem 0.6rem;
    }
    .table .btn-sm {
        padding: 0.375rem 0.75rem;
    }
    /* Attachment chip remove button: enlarge the hit box, not the icon */
    .attach-remove {
        padding: 0.55rem;
    }
    /* Address-book chips on the bot form: btn-sm is ~31px, under the 44px a
       finger needs. Outside a table, so .table .btn-sm above doesn't cover it. */
    .chip-toggle {
        min-height: 44px;
    }
}

@media (max-width: 576px) {
    /* iOS Safari auto-zooms inputs below 16px — keep textareas at 1rem */
    .system-prompt-textarea {
        font-size: 1rem;
    }
    .msg {
        max-width: 92%;
    }
    #chat-messages {
        padding: 0.6rem;
    }
    /* Chat toolbar: let the agent select take the remaining row width */
    #agent-select {
        max-width: 100% !important;
        flex: 1 1 40%;
        min-width: 0;
    }
}

/* ---- History window (filterable, date-grouped chat list) --------------- */
.history-list {
    display: flex;
    flex-direction: column;
}

.history-group {
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--bs-secondary-color);
    padding: 0.5rem 0.25rem 0.25rem;
    position: sticky;
    top: 0;
    background: var(--bs-body-bg);
    z-index: 1;
}

.history-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.55rem 0.5rem;
    border-radius: 6px;
    cursor: pointer;
    border: 1px solid transparent;
}
.history-item:hover {
    background: var(--bs-tertiary-bg);
}
.history-item.active {
    border-color: var(--bs-primary);
    background: var(--bs-primary-bg-subtle);
}

.history-item .hi-main {
    flex: 1 1 auto;
    min-width: 0;
}
.history-item .hi-title {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.history-item .hi-meta {
    font-size: 0.76rem;
    color: var(--bs-secondary-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.history-item .hi-when {
    font-size: 0.72rem;
    color: var(--bs-secondary-color);
    white-space: nowrap;
    flex: 0 0 auto;
}
.history-item .hi-del {
    flex: 0 0 auto;
    padding: 0.15rem 0.35rem;
    opacity: 0.55;
    line-height: 1;
}
.history-item .hi-del:hover {
    opacity: 1;
}

/* Agent preview tree (delegation + tools) */
.agent-tree ul {
    list-style: none;
    padding-left: 1.1rem;
    margin: 0.2rem 0;
    border-left: 1px solid var(--bs-border-color);
}
.agent-tree li {
    margin: 0.15rem 0;
}
.agent-tree .agent-node-head {
    padding: 0.2rem 0;
    font-size: 1rem;
}
.agent-tree details > summary {
    cursor: pointer;
    user-select: none;
}
.agent-tree details > summary:hover {
    color: var(--bs-primary);
}

/* Tool description under the name: one line, so every row in the list has the
   same height and the list stays scannable. The full text is in the title. */
.tool-desc {
    display: -webkit-box;
    -webkit-line-clamp: 1;
    line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Collapsible section on the Tools page: the summary IS the card header, so it
   keeps the default triangle out and draws its own (same idiom as .tool-call). */
.tool-section > summary {
    cursor: pointer;
    user-select: none;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.35rem;
}
.tool-section > summary::-webkit-details-marker {
    display: none;
}
.tool-section > summary::after {
    content: "\25B8";  /* ▸ */
    margin-left: auto;
    font-size: 0.75rem;
    opacity: 0.6;
}
.tool-section[open] > summary::after {
    content: "\25BE";  /* ▾ */
}
