:root {
    --bg: #f4efe7;
    --bg-deep: #dfd3c0;
    --panel: rgba(255, 251, 245, 0.86);
    --panel-strong: #fffdf9;
    --ink: #1f2833;
    --muted: #67717d;
    --line: rgba(31, 40, 51, 0.1);
    --accent: #1c5c78;
    --accent-strong: #123b4d;
    --accent-soft: #d9edf6;
    --user: #1c5c78;
    --assistant: #f7efe3;
    --success: #3d7f55;
    --warm: #bc6f3c;
    --shadow: 0 24px 70px rgba(56, 34, 18, 0.14);
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
    color: var(--ink);
    font-family: "Segoe UI Variable Text", "Segoe UI", sans-serif;
    background:
        radial-gradient(circle at top left, rgba(188, 111, 60, 0.18), transparent 28%),
        radial-gradient(circle at right, rgba(28, 92, 120, 0.14), transparent 30%),
        linear-gradient(145deg, var(--bg), var(--bg-deep));
}

body {
    position: relative;
}

a {
    color: inherit;
    text-decoration: none;
}

img {
    display: block;
    max-width: 100%;
}

button,
input,
select,
textarea {
    font: inherit;
}

.page-backdrop {
    position: fixed;
    inset: 0;
    pointer-events: none;
    background-image:
        linear-gradient(rgba(255, 255, 255, 0.08) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 255, 255, 0.08) 1px, transparent 1px);
    background-size: 32px 32px;
    mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.65), transparent 82%);
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 1rem 1.35rem;
    backdrop-filter: blur(16px);
    background: rgba(244, 239, 231, 0.72);
    border-bottom: 1px solid rgba(18, 59, 77, 0.08);
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.9rem;
}

.brand strong,
h1,
h2,
h3 {
    font-family: Georgia, "Times New Roman", serif;
}

.brand strong {
    display: block;
    font-size: 1.06rem;
}

.brand small,
.subtle,
.card-heading p,
.artifact-card p,
.empty-card p,
.download-card span,
.file-chip span,
.message__meta span {
    color: var(--muted);
}

.brand-mark {
    display: grid;
    place-items: center;
    width: 2.8rem;
    height: 2.8rem;
    border-radius: 18px;
    color: white;
    font-weight: 700;
    letter-spacing: 0.08em;
    background: linear-gradient(135deg, var(--accent), var(--warm));
    box-shadow: var(--shadow);
}

.topnav a,
.ghost-btn,
.secondary-btn,
.primary-btn,
.attach-btn,
.file-chip,
.download-card {
    transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}

.topnav a {
    padding: 0.72rem 1rem;
    border-radius: 999px;
}

.page-frame {
    width: min(1600px, calc(100% - 0.75rem));
    margin: 0 auto;
    padding: 1.4rem 0 2rem;
}

.panel {
    background: var(--panel);
    border: 1px solid rgba(255, 255, 255, 0.46);
    border-radius: 22px;
    box-shadow: var(--shadow);
    backdrop-filter: blur(10px);
}

.chat-layout {
    display: grid;
    grid-template-columns: minmax(0, 1.6fr) minmax(320px, 0.84fr);
    gap: 1.2rem;
    align-items: start;
}

.chat-stage {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    min-height: calc(100vh - 8.8rem);
    padding: 0.85rem;
}

.chat-stage__header,
.card-heading,
.context-card__title,
.message__meta,
.artifact-card,
.artifact-card__actions,
.composer__bottom,
.composer__actions,
.download-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.8rem;
}

.chat-stage__header,
.card-heading {
    align-items: flex-start;
}

.eyebrow {
    margin: 0 0 0.25rem;
    text-transform: uppercase;
    letter-spacing: 0.18em;
    font-size: 0.68rem;
    color: var(--accent);
    font-weight: 700;
}

h1,
h2,
h3 {
    margin: 0;
}

h1 {
    font-size: clamp(2rem, 4vw, 3.6rem);
    line-height: 0.95;
}

.chat-stage__header h1 {
    font-size: clamp(1.25rem, 2.2vw, 1.8rem);
    line-height: 1.1;
}
.chat-stage__header {
    padding-bottom: 0.45rem;
}
.chat-stage__header .subtle {
    font-size: 0.82rem;
    margin: 0.15rem 0 0;
}
.card-heading h2 {
    font-size: 1rem;
}
.card-heading p {
    font-size: 0.8rem;
    margin-top: 0.15rem;
}

.chat-thread {
    display: grid;
    gap: 0.65rem;
    padding: 0.5rem 0.2rem 0.5rem 0;
    overflow-y: auto;
    min-height: 0;
    scroll-behavior: smooth;
}

.message {
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr);
    gap: 0.6rem;
    align-items: start;
}

.message__avatar {
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    border-radius: 14px;
    font-weight: 700;
    letter-spacing: 0.04em;
}

.message--assistant .message__avatar {
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
    color: white;
}

.message--user .message__avatar {
    background: #f1dfc9;
    color: var(--warm);
}

.message__body {
    display: grid;
    gap: 0.55rem;
    padding: 0.7rem 0.9rem;
    border-radius: 18px;
    border: 1px solid var(--line);
}

.message--assistant .message__body {
    background: var(--assistant);
}

.message--user .message__body {
    background: rgba(28, 92, 120, 0.1);
}

.message-text {
    white-space: pre-line;
    line-height: 1.55;
}

.artifact-stack,
.analysis-card,
.preview-card,
.quick-grid,
.file-list,
.artifact-list,
.insight-list {
    display: grid;
    gap: 0.5rem;
}

.artifact-card,
.empty-card,
.context-card,
.analysis-card,
.preview-card,
.download-card,
.file-chip {
    padding: 0.65rem 0.8rem;
    border-radius: 14px;
    border: 1px solid var(--line);
    background: var(--panel-strong);
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.artifact-card {
    align-items: flex-start;
}

.artifact-card__actions {
    flex-wrap: wrap;
    justify-content: flex-end;
}

.artifact-card__name {
    margin-top: 0.2rem;
    font-weight: 600;
}

.message-preview-image {
    width: 100%;
    max-width: 100%;
    border-radius: 14px;
    border: 1px solid var(--line);
    background: white;
}

.preview-meta,
.stats-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
}

.preview-meta span,
.stats-strip span {
    padding: 0.3rem 0.6rem;
    border-radius: 999px;
    background: rgba(28, 92, 120, 0.08);
    border: 1px solid rgba(28, 92, 120, 0.08);
    font-size: 0.85rem;
}

.table-wrap {
    overflow: auto;
    border-radius: 18px;
    border: 1px solid var(--line);
}

table {
    width: 100%;
    min-width: 540px;
    border-collapse: collapse;
    background: white;
}

th,
td {
    padding: 0.5rem 0.65rem;
    border-bottom: 1px solid rgba(31, 40, 51, 0.08);
    text-align: left;
    font-size: 0.88rem;
}

th {
    position: sticky;
    top: 0;
    background: #f9f6f1;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
}

.insight-item {
    padding: 0.55rem 0.7rem;
    border-radius: 14px;
    background: rgba(28, 92, 120, 0.07);
    border: 1px solid rgba(28, 92, 120, 0.08);
}

.composer {
    display: grid;
    gap: 0.55rem;
    margin-top: 0.55rem;
    padding: 0.7rem;
    border-radius: 20px;
    background: linear-gradient(145deg, rgba(255, 255, 255, 0.92), rgba(250, 244, 236, 0.96));
    border: 1px solid rgba(28, 92, 120, 0.12);
}

.composer__top,
.composer__bottom {
    gap: 0.55rem;
}

textarea {
    width: 100%;
    min-height: 44px;
    max-height: 200px;
    resize: vertical;
    padding: 0.65rem 0.85rem;
    border: 1px solid rgba(28, 92, 120, 0.12);
    border-radius: 14px;
    background: white;
    outline: none;
}

textarea:focus,
select:focus,
input:focus {
    border-color: rgba(28, 92, 120, 0.45);
}

.attach-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.7rem;
    padding: 0.6rem 0.9rem;
    border-radius: 14px;
    border: 1px dashed rgba(28, 92, 120, 0.28);
    background: rgba(28, 92, 120, 0.06);
    cursor: pointer;
}

.attach-btn input {
    display: none;
}

.selected-file-pill {
    padding: 0.72rem 0.95rem;
    border-radius: 999px;
    background: rgba(188, 111, 60, 0.12);
    color: var(--warm);
    font-weight: 600;
}

button,
.primary-btn,
.secondary-btn,
.ghost-btn {
    appearance: none;
    border: none;
    cursor: pointer;
    border-radius: 12px;
    padding: 0.6rem 0.9rem;
}

.primary-btn {
    color: white;
    background: linear-gradient(135deg, var(--accent), #26789b);
    box-shadow: 0 14px 24px rgba(28, 92, 120, 0.2);
}

.secondary-btn {
    color: white;
    background: linear-gradient(135deg, var(--success), #5a9a70);
}

.ghost-btn {
    background: rgba(28, 92, 120, 0.08);
    color: var(--ink);
}

.wide-btn {
    width: 100%;
}

.ghost-btn:hover,
.secondary-btn:hover,
.primary-btn:hover,
.attach-btn:hover,
.file-chip:hover,
.download-card:hover,
.topnav a:hover {
    transform: translateY(-1px);
}

.ghost-btn:disabled,
.secondary-btn:disabled,
.primary-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
}

/* Quick-grid chart picker: тоггл + ряд чипов типа графика.
   Чипы — компактные пилюли, не занимают полную ширину. */
.chart-picker {
    display: grid;
    gap: 0.5rem;
}

.chart-picker__toggle::after {
    content: "▾";
    margin-left: 0.4rem;
    font-size: 0.7em;
    transition: transform 0.15s ease;
}

.chart-picker__toggle.is-open::after {
    transform: rotate(180deg);
}

.chart-picker__chips {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
}

.chip-btn {
    appearance: none;
    border: 1px solid rgba(28, 92, 120, 0.25);
    background: rgba(28, 92, 120, 0.06);
    color: var(--ink);
    cursor: pointer;
    border-radius: 999px;
    padding: 0.35rem 0.75rem;
    font-size: 0.82rem;
    font-weight: 500;
    transition: background 0.15s ease, transform 0.15s ease;
}

.chip-btn:hover:not(:disabled) {
    background: rgba(28, 92, 120, 0.14);
    transform: translateY(-1px);
}

.chip-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.side-rail {
    display: grid;
    gap: 0.7rem;
    min-width: 0;
}

.rail-section {
    padding: 0.7rem;
}

.context-card,
.empty-card {
    display: grid;
    gap: 0.5rem;
}

.file-chip-form {
    margin: 0;
}

.file-chip {
    display: grid;
    gap: 0.2rem;
    width: 100%;
    text-align: left;
    cursor: pointer;
}

.file-chip.is-active {
    border-color: rgba(28, 92, 120, 0.4);
    background: rgba(28, 92, 120, 0.08);
}

.download-card {
    align-items: center;
    gap: 0.55rem;
}

.download-card > div {
    display: grid;
    gap: 0.1rem;
    min-width: 0;
}

/* Имя-хэш артефакта — одна строка с обрезкой (хэш — шум, не надо переносить по слогам) */
.download-card > div > strong {
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--ink);
}

/* Описание — компактно, до 2 строк */
.download-card > div > span {
    font-size: 0.76rem;
    color: var(--muted);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* "Скачать" — всегда одно слово, не сжимается, прижато справа по центру */
.download-card > span {
    white-space: nowrap;
    flex-shrink: 0;
    align-self: center;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--accent);
}

.alert {
    padding: 0.9rem 1rem;
    border-radius: 18px;
}

.global-loader {
    position: fixed;
    right: 1.5rem;
    bottom: 1.5rem;
    z-index: 40;
    display: none;
    align-items: center;
    gap: 0.75rem;
    padding: 0.85rem 1rem;
    border-radius: 999px;
    background: rgba(31, 40, 51, 0.92);
    color: white;
    box-shadow: var(--shadow);
}

.loader-ring {
    width: 1rem;
    height: 1rem;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-top-color: white;
    animation: spin 0.8s linear infinite;
}

.htmx-request .global-loader,
.htmx-request.global-loader,
.htmx-request ~ .global-loader {
    display: inline-flex;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* Лоадер чуть заметнее + таймер внутри */
.global-loader {
    font-size: 0.85rem;
    font-weight: 500;
    z-index: 60;
}
#loader-seconds {
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    margin-left: 0.1rem;
}

/* Инлайн «AI печатает» — три точки в бабле ассистента */
.is-typing .message__body {
    padding: 0.7rem 0.9rem;
}
.typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    height: 1rem;
}
.typing-dots span {
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: var(--muted);
    animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing-dots span:nth-child(2) {
    animation-delay: 0.18s;
}
.typing-dots span:nth-child(3) {
    animation-delay: 0.36s;
}
@keyframes typing-bounce {
    0%, 80%, 100% {
        opacity: 0.3;
        transform: translateY(0);
    }
    40% {
        opacity: 1;
        transform: translateY(-3px);
    }
}

/* Блокировка ввода на время запроса: затемняем управляющие зоны,
   скролл чата остаётся доступным */
#page-shell.is-busy .composer,
#page-shell.is-busy .quick-grid,
#page-shell.is-busy .file-list {
    opacity: 0.5;
    transition: opacity 0.15s ease;
}
#page-shell.is-busy .composer {
    pointer-events: none;
}

@media (max-width: 1180px) {
    .chat-layout {
        grid-template-columns: 1fr;
    }

    .chat-stage {
        min-height: auto;
    }
}

@media (max-width: 760px) {
    .page-frame {
        width: min(100% - 1rem, 100%);
        padding-top: 1rem;
    }

    .topbar,
    .chat-stage__header,
    .composer__bottom,
    .composer__actions,
    .artifact-card,
    .download-card,
    .message {
        grid-template-columns: 1fr;
        flex-direction: column;
        align-items: stretch;
    }

    .message {
        display: grid;
        grid-template-columns: 1fr;
    }

    .message__avatar {
        width: 44px;
        height: 44px;
    }

    .panel,
    .chat-stage,
    .rail-section {
        border-radius: 22px;
    }

    .chat-stage {
        padding: 0.95rem;
    }

    table {
        min-width: 460px;
    }
}

/* ===== Admin panel (вариант 3) ===== */

.admin-shell {
    /* Заполняем всю ширину page-frame (кап 1600px задаётся в .page-frame).
       Раньше было max-width: 980px — на широком экране оставляло ~50% пустых
       полей. Теперь боковые отступы минимальны. */
    max-width: none;
    margin: 0;
    padding: 0.5rem 0.5rem 1rem;
    display: flex;
    flex-direction: column;
    /* ОПРЕДЕЛЁННАЯ высота (не min-height) — критично для flex-цепочки:
       flex-контейнер с height:auto + min-height НЕ растит flex-children до
       пола min-height (классическая ловушка), и textarea промпта коллапсирует
       в ~1/3 панели в режимах <1181px, где page-frame — обычный блок. С
       определённым height flex:1 form → layout → card → textarea транслирует
       высоту корректно во ВСЕХ режимах. В ≥1181px media flex:1 1 auto всё
       равно растит оболочку до page-frame (height служит flex-basis). */
    height: calc(100vh - 8rem);
}

.admin-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.6rem;
}

.admin-head h1 {
    margin: 0;
    font-size: 1.15rem;
    line-height: 1.2;
}

.admin-sub {
    margin: 0;
    color: var(--muted);
    font-size: 0.82rem;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.admin-badge {
    flex: 0 0 auto;
    padding: 0.4rem 0.8rem;
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--accent-strong);
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.admin-head__actions {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex: 0 0 auto;
}

.admin-save-btn {
    padding: 0.45rem 1rem;
    font-size: 0.86rem;
    font-weight: 600;
}

/* «Тест провайдера» — той же высоты, что «Сохранить настройки», чтобы не
   вытягивать правую колонку (base .secondary-btn = 0.6rem 0.9rem / 16px — выше
   на ~8px). */
.admin-test-btn {
    padding: 0.45rem 1rem;
    font-size: 0.86rem;
    font-weight: 600;
}

.admin-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 0.6rem;
}

.admin-card {
    background: var(--panel-strong);
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 0.7rem 0.85rem 0.7rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.admin-card__head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.5rem;
}

.admin-card__head h2 {
    margin: 0;
    font-size: 0.95rem;
}

.admin-card__key {
    font-size: 0.7rem;
    color: var(--muted);
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
    background: rgba(31, 40, 51, 0.05);
    padding: 0.1rem 0.4rem;
    border-radius: 5px;
}

.admin-card__hint {
    margin: 0;
    font-size: 0.8rem;
    color: var(--muted);
    line-height: 1.4;
}

.admin-form {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 0.1rem;
}

.admin-input {
    width: 100%;
    padding: 0.45rem 0.6rem;
    border: 1px solid rgba(28, 92, 120, 0.16);
    border-radius: 10px;
    background: white;
    outline: none;
    font-size: 0.86rem;
}

.admin-input:focus {
    border-color: rgba(28, 92, 120, 0.45);
}

/* Toast-уведомление статуса: фиксированная позиция, вне потока —
   не крадёт вертикальное место в админке. */
#admin-status {
    position: fixed;
    right: 1rem;
    bottom: 1rem;
    z-index: 1100;
    max-width: min(460px, 92vw);
    pointer-events: none;
}

#admin-status .admin-status {
    pointer-events: auto;
}

.admin-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.55rem 0.85rem;
    border-radius: 12px;
    margin: 0;
    font-size: 0.84rem;
    line-height: 1.35;
    box-shadow: 0 8px 24px rgba(31, 40, 51, 0.22);
}

.admin-status--ok {
    background: rgba(61, 127, 85, 0.12);
    color: var(--success);
    border: 1px solid rgba(61, 127, 85, 0.25);
}

.admin-status--error {
    background: rgba(188, 60, 60, 0.12);
    color: #b43c3c;
    border: 1px solid rgba(188, 60, 60, 0.25);
}

.admin-status__icon {
    font-weight: 700;
    font-size: 0.95rem;
}

/* Textarea-инпут для системного промпта — растягивается в высоту колонки. */
.admin-input--textarea {
    /* Шрифт промпт-редактора — тот же, что у редактируемых значений в правых
       панелях (.admin-card--param .admin-input): наследует body (sans-serif),
       0.84rem. */
    font-family: inherit;
    font-size: 0.84rem;
    line-height: 1.45;
    resize: vertical;
    min-height: 240px;
}

/* Dashboard статистики использования */
.admin-usage {
    border: 1px solid rgba(28, 92, 120, 0.16);
    border-radius: 14px;
    padding: 0.55rem 0.8rem 0.6rem;
    margin-bottom: 0.7rem;
    background: rgba(28, 92, 120, 0.04);
}

.admin-usage__head {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.45rem;
}

.admin-usage__head h2 {
    margin: 0;
    font-size: 0.92rem;
}

.admin-usage__grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
    gap: 0.4rem;
    margin-bottom: 0.45rem;
}

.usage-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: white;
    border-radius: 10px;
    padding: 0.3rem 0.3rem;
    border: 1px solid rgba(28, 92, 120, 0.1);
}

.usage-stat__value {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
}

.usage-stat__label {
    font-size: 0.68rem;
    color: var(--muted, #6b7280);
    margin-top: 0.1rem;
}

/* Метаданные — в одну строку (Последний запрос / Последняя ошибка),
   текст ошибки переносится ниже. */
.admin-usage__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.15rem 1.2rem;
    margin: 0;
    font-size: 0.78rem;
}

.admin-usage__meta div {
    display: flex;
    gap: 0.4rem;
}

.admin-usage__meta dt {
    color: var(--muted, #6b7280);
    min-width: 0;
}

.admin-usage__meta dd {
    margin: 0;
    font-variant-numeric: tabular-nums;
}

.admin-usage__error dd {
    color: #b43c3c;
}

/* ============================================================
   Admin: двухколоночная раскладка + тултипы + пресеты провайдера
   ============================================================ */

/* Две колонки: слева — системный промпт (ядро, во всю высоту),
   справа — управления (макропанель Runtime-конфиг: провайдер + параметры).
   Layout растягивается на всю высоту оболочки — промпт-карточка
   заполняет колонку, textarea тянется до нижней границы экрана. */
.admin-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(460px, 1fr);
    /* Ряд — по высоте контента (правой группы Runtime+Реестры), а не 1fr.
       align-content:start не даёт auto-ряду растягиваться до колонки — иначе
       промпт снова стал бы выше правой группы. Промпт (align-items:stretch)
       тянется до высоты ряда = низ Реестров. Свободное место оказывается снизу
       под обеими колонками (там плавает toast). overflow-y:auto — на коротких
       экранах (контент > колонки) скроллится сам layout, а не только сайд. */
    grid-template-rows: auto;
    align-content: start;
    gap: 0.8rem;
    align-items: stretch;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
}

/* Единая форма настроек растягивается на всю высоту оболочки (админка —
   flex-колонка), чтобы layout (её ребёнок) получил определённую высоту,
   а с ним и 1fr-ряд сетки → промпт-карточка заполняет колонку по высоте. */
#admin-settings-form {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.admin-layout__main {
    min-width: 0;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.admin-layout__side {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    /* Секции стоят вплотную (flex-start, без зазора между Runtime-конфиг и
       Реестрами) — группа контентной высоты прижата к верху. Ряд сетки
       (.admin-layout grid-template-rows:auto + align-content:start) берётся
       по высоте этой группы, промпт тянется до неё же — низ промпта совпадает
       с низом Реестров. Свободное место (когда колонка выше контента) остаётся
       СНИЗУ под обеими колонками — там плавает toast «Тест провайдера», не
       наползая на Реестры. На коротких экранах (контент > колонки) ряд
       перерастает колонку — скроллится сам .admin-layout. */
    min-width: 0;
    min-height: 0;
    overflow-y: auto;
    padding-right: 4px;
}

/* Промпт-карточка занимает левую колонку целиком и тянется в высоту. */
.admin-card--prompt {
    flex: 1 1 auto;
    min-height: 0;
}

.admin-card--prompt .admin-input--textarea {
    /* Растёт flex-ростом до нижней границы карточки. min-height:0 — может
       сжиматься; max-height:none — КРИТИЧНО: глобальное `textarea { max-height:
       200px }` (для чат-композера) иначе капирует промпт-редактор на 200px
       (~1/3 панели) и flex:1 не дорастает. height:100% тоже упиралось в тот
       потолок. Без потолка flex:1 1 auto заполняет карточку целиком. */
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
}

/* Макропанель «Runtime-конфиг» — объединяет провайдер и 6 операторских
   параметров в одну группу с общим заголовком и источником (config.json),
   по образцу карточки системного промпта. */
.admin-runtime {
    background: var(--panel-strong);
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 0.4rem 0.6rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.admin-runtime__head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.5rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--line);
}

.admin-runtime__head h2 {
    margin: 0;
    font-size: 0.95rem;
}

.admin-runtime__body {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

/* Внутри макропанели «Реестры» две под-панели (Действия / Типы графиков)
   стоят бок о бок — это экономит ~130px высоты правой колонки. */
.admin-registries .admin-runtime__body {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.4rem;
    align-items: start;
}

/* Read-only макропанель «Реестры агента» — функциональное ядро (действия +
   типы графиков). Переиспользует .admin-runtime (панель + шапка + тело);
   внутри — две под-панели .admin-card--registry. Строка = только видимое
   название + значение из хардкода (напр. «Предпросмотр файла preview»). */
.admin-card--registry {
    padding: 0.4rem 0.6rem 0.45rem;
    gap: 0.25rem;
}

.admin-card--registry .admin-card__head h3 {
    margin: 0;
    font-size: 0.8rem;
    font-family: Georgia, "Times New Roman", serif;
}

.admin-registries__list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.12rem;
}

.admin-registries__list li {
    display: flex;
    align-items: baseline;
    gap: 0.45rem;
    font-size: 0.8rem;
    line-height: 1.2;
    min-width: 0;
}

.admin-reg__label {
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 500;
}

.admin-reg__key {
    flex: 0 0 auto;
    min-width: 0;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
    font-size: 0.7rem;
    color: var(--muted);
    background: rgba(31, 40, 51, 0.05);
    padding: 0.05rem 0.35rem;
    border-radius: 5px;
}

/* Тултип на лейбле параметра (чистый CSS, паттерн AI Curator).
   Наведи мышь на название параметра — появится подробный комментарий. */
.admin-tooltip {
    position: relative;
    display: inline-block;
    cursor: help;
    border-bottom: 1px dotted var(--muted);
}

.admin-tooltip__text {
    /* Текст тултипа хранится в DOM, но рендерится JS-оверлеем (.admin-tooltip-floating),
       чтобы не клиппиться overflow:auto боковой колонки. Сам спрятан. */
    display: none;
}

.admin-tooltip-floating {
    position: fixed;
    z-index: 2000;
    width: max-content;
    max-width: 300px;
    background: var(--ink);
    color: #f4efe7;
    text-align: left;
    border: 1px solid rgba(28, 92, 120, 0.5);
    border-radius: 10px;
    padding: 0.55rem 0.7rem;
    font-size: 0.8rem;
    font-weight: 400;
    line-height: 1.4;
    box-shadow: 0 6px 20px rgba(31, 40, 51, 0.32);
    pointer-events: none;
}

.admin-tooltip__text::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -6px;
    border: 6px solid transparent;
    border-top-color: var(--ink);
}

.admin-tooltip:hover .admin-tooltip__text,
.admin-tooltip:focus-within .admin-tooltip__text {
    visibility: visible;
    opacity: 1;
}

/* Компактные карточки параметров (2 в ряд, без кнопок — общий «Сохранить»). */
.admin-card--param {
    padding: 0.4rem 0.6rem 0.45rem;
    gap: 0.2rem;
    min-width: 0;
}

/* Лейбл и программный ключ — в ОДНОЙ строке (label слева, key справа),
   чтобы сэкономить вертикаль и вместить макропанель + реестры без скролла.
   Длинный ключ (openai_max_history_messages) обрезается многоточием; полный
   ключ доступен через title при наведении. */
.admin-card--param .admin-card__head {
    gap: 0.4rem;
}

.admin-card--param .admin-card__head h2 {
    font-size: 0.88rem;
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.admin-card--param .admin-card__key {
    font-size: 0.68rem;
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: right;
}

.admin-card--param .admin-input {
    padding: 0.4rem 0.6rem;
    font-size: 0.84rem;
}

/* Сетка операторских параметров — ровно 2 в ряд (3 ряда по 2). */
.admin-layout__side .admin-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.4rem;
}

/* Секция «Провайдер». */
.admin-card--provider {
    gap: 0.4rem;
}

/* Поля провайдера — раскладка 2+2 (верх: имя + модель, низ: endpoint +
   structured_output), без кнопок. */
.admin-provider-fields {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.4rem;
}

/* Чипы пресетов провайдера. */
.admin-presets__form {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.preset-chip {
    padding: 0.5rem 0.9rem;
    border-radius: 999px;
    border: 1px solid rgba(28, 92, 120, 0.22);
    background: var(--panel-strong);
    color: var(--ink);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.preset-chip:hover {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.preset-chip--active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.preset-chip--active:hover {
    background: var(--accent-strong);
    border-color: var(--accent-strong);
}

/* «Тест провайдера» убран в шапку карточки (рядом с ключом provider · …),
   чтобы не занимать отдельную строку и не удлинять правую колонку. */
.admin-provider__head-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
}

@media (max-width: 980px) {
    .admin-layout {
        grid-template-columns: 1fr;
        grid-template-rows: auto;
    }
    /* Узкий 1-колоночный режим: высотная цепочка не bounded (auto-ряд сетки),
       flex:1 textarea коллапсирует. Даём floor «почти во весь экран» calc-ом;
       страница скроллится нормально. В 981–1180px и >=1181px flex-цепочка
       bounded (admin-shell: height calc + form flex:1) и floor не нужен. */
    .admin-card--prompt .admin-input--textarea {
        min-height: calc(100vh - 360px);
    }
}

@media (max-width: 560px) {
    .admin-head {
        flex-direction: column;
        align-items: flex-start;
    }
    .admin-head__actions {
        align-self: stretch;
        justify-content: flex-end;
    }
    .admin-sub {
        white-space: normal;
    }
    .admin-provider-fields,
    .admin-layout__side .admin-grid {
        grid-template-columns: 1fr;
    }
    .admin-registries .admin-runtime__body {
        grid-template-columns: 1fr;
    }
}

/* ============================================================
   Wide-screen app shell: шапка чата и правая колонка закреплены,
   скроллится только тело чата (независимо от рейла).
   На узких экранах (≤1180px) всё сворачивается в одну колонку
   с обычным страничным скроллом.
   ============================================================ */
@media (min-width: 1181px) {
    html,
    body {
        height: 100%;
    }
    body {
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }
    .topbar {
        flex: 0 0 auto;
    }
    .page-frame {
        flex: 1 1 auto;
        min-height: 0;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        padding: 0.85rem 0 0.85rem;
    }
    .chat-layout {
        flex: 1 1 auto;
        min-height: 0;
        align-items: stretch;
        grid-template-columns: minmax(0, 1fr) 360px;
        gap: 1rem;
    }
    .chat-stage {
        min-height: 0;
        min-width: 0;
        height: 100%;
        overflow: hidden;
    }
    .chat-thread {
        min-height: 0;
        min-width: 0;
        overflow-y: auto;
    }
    .side-rail {
        min-height: 0;
        min-width: 0;
        height: 100%;
        overflow-y: auto;
        padding-right: 4px;
    }
    /* Админка на широком экране: оболочка заполняет viewport, левая колонка
       (промпт) тянется до нижней границы, правая колонка скроллится
       самостоятельно — параметры не выталкивают промпт за экран. */
    .admin-shell {
        flex: 1 1 auto;
        min-height: 0;
        overflow: hidden;
    }
    .admin-layout__side {
        overflow-y: auto;
        min-height: 0;
        padding-right: 4px;
    }
}
