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);
}

#app {
    min-height: calc(100vh - 70px);
}

/* 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. */
.chat-wrap {
    height: calc(100vh - 80px);
    height: calc(100dvh - 80px - 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);
}

/* 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;
}

.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);
}

/* 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;
}

/* 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;
}

.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);
}

/* ---- 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";  /* ▾ */
}
