:root {
    --bg: #f5f7fb;
    --panel: #ffffff;
    --line: #d9dfeb;
    --text: #17212f;
    --muted: #5a697f;
    --primary: #1257c4;
    --danger: #b53939;
    --ok: #1f7a3f;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Segoe UI", Tahoma, sans-serif;
    color: var(--text);
    background: linear-gradient(180deg, #f8fbff 0%, #eef2f8 100%);
}

.chat-page {
    height: 100vh;
    overflow: hidden;
}

.app-shell {
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    height: 100vh;
    min-height: 100vh;
    overflow: hidden;
}

.app-shell.sidebar-collapsed {
    grid-template-columns: 84px minmax(0, 1fr);
}

.sidebar {
    border-right: 1px solid var(--line);
    background: var(--panel);
    padding: 14px;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 14px;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
    position: sticky;
    top: 0;
    align-self: start;
}

.sidebar-top {
    grid-row: 1;
    display: flex;
    gap: 8px;
}

.sidebar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.sidebar-btn .btn-icon {
    width: 16px;
    height: 16px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 16px;
}

.sidebar-btn .btn-icon svg {
    width: 16px;
    height: 16px;
    display: block;
}

.sidebar-nav-link {
    font-size: 15px;
    line-height: 1.2;
    justify-content: flex-start;
}

.sidebar-nav-link .btn-icon {
    width: 1.15em;
    height: 1.15em;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 1.15em;
    line-height: 0;
}

.sidebar-nav-link .btn-icon svg {
    width: 1.15em;
    height: 1.15em;
    display: block;
    overflow: visible;
}

.sidebar-nav-link .btn-label {
    line-height: 1.1;
    white-space: nowrap;
}

.sidebar-icon-only {
    width: 40px;
    min-width: 40px;
    padding: 9px;
}

.app-shell.sidebar-collapsed .sidebar-top {
    flex-direction: column;
}

.app-shell.sidebar-collapsed #new-chat {
    padding: 9px;
}

.app-shell.sidebar-collapsed .sidebar-btn {
    width: 100%;
    min-width: 0;
    padding: 9px;
}

.app-shell.sidebar-collapsed .sidebar-btn .btn-label,
.app-shell.sidebar-collapsed .sidebar-nav-link .btn-label {
    display: none;
}

.app-shell.sidebar-collapsed .sidebar-nav-link {
    justify-content: center;
    padding: 9px;
}

.chat-list-wrap h3 {
    margin: 6px 0 10px;
    font-size: 14px;
    color: var(--muted);
}

.chat-list-wrap {
    grid-row: 2;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
}

.chat-item-row {
    position: relative;
}

.chat-list-link {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 8px;
    background: #fbfdff;
    min-width: 0;
    cursor: pointer;
}

.chat-list-link.active {
    border-color: var(--primary);
    background: #e8f0ff;
}

.chat-list-link:focus-visible {
    outline: 2px solid #99b8ff;
    outline-offset: 1px;
}

.chat-item-content {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.chat-item-actions {
    position: relative;
    display: flex;
    width: 34px;
    height: 34px;
    align-items: stretch;
    justify-content: center;
    flex: 0 0 34px;
}

.chat-menu-toggle {
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 10px;
    width: 100%;
    height: 100%;
    padding: 0;
    cursor: pointer;
    color: var(--muted);
    font-size: 16px;
    line-height: 1;
}

.chat-menu {
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    z-index: 30;
    min-width: 120px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 8px 18px rgba(12, 28, 62, 0.12);
    padding: 4px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.chat-menu[hidden] {
    display: none !important;
}

.chat-menu-item {
    border: 1px solid transparent;
    border-radius: 8px;
    background: #fff;
    text-align: left;
    padding: 5px 8px;
    cursor: pointer;
    color: var(--text);
    font-size: 13px;
    line-height: 1.25;
}

.chat-menu-item:hover {
    background: #f4f8ff;
    border-color: #d5e2fb;
}

.chat-menu-item.danger {
    color: #8f2f2f;
}

.app-shell.sidebar-collapsed .chat-list-wrap {
    display: none;
}

.chat-snippet {
    display: block;
    font-size: 13px;
}

.chat-title {
    display: block;
    font-size: 13px;
    font-weight: 600;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-question {
    display: block;
    font-size: 12px;
    color: #46556f;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-meta {
    color: var(--muted);
    font-size: 12px;
}

.sidebar-bottom {
    grid-row: 3;
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-self: end;
    justify-self: stretch;
}

.profile-link {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border: 1px solid var(--line);
    border-radius: 10px;
    text-decoration: none;
    color: var(--text);
    background: #fdfefe;
}

.profile-link.active {
    border-color: var(--primary);
    background: #e8f0ff;
}

.chat-main {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto auto;
    height: 100vh;
    min-height: 0;
    padding: 18px;
    gap: 12px;
    min-width: 0;
    overflow: hidden;
}

.chat-header {
    position: sticky;
    top: 0;
    z-index: 5;
    background: linear-gradient(180deg, #f8fbff 0%, #eef2f8 100%);
    padding-bottom: 4px;
}

.chat-header h1 {
    margin: 0;
    font-size: 20px;
}

.progress-label {
    margin-top: 4px;
    color: var(--muted);
    font-size: 14px;
}

.header-question-wrap {
    margin-top: 8px;
    border: 1px solid #c5d6fa;
    border-radius: 12px;
    background: linear-gradient(180deg, #eef4ff 0%, #e6efff 100%);
    padding: 10px 12px;
}

.header-question-label {
    margin: 0 0 4px;
    font-size: 11px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #4d6286;
}

.header-question {
    color: #182846;
    font-size: 19px;
    font-weight: 600;
    line-height: 1.25;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-question-meta {
    margin-top: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
}

.header-mode-badge {
    display: inline-flex;
    align-items: center;
    border: 1px solid #c0d2f8;
    border-radius: 999px;
    background: #f7faff;
    color: #294979;
    font-size: 12px;
    padding: 4px 10px;
    white-space: nowrap;
}

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-header-actions .btn-secondary,
.chat-header-actions .btn-danger {
    padding: 6px 10px;
    font-size: 12px;
}

.status-panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 8px 12px;
}

.status-panel-title {
    font-size: 12px;
    text-transform: uppercase;
    color: var(--muted);
}

.status-panel-body {
    margin-top: 6px;
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    font-size: 13px;
}

.status-panel-error {
    width: 100%;
    color: var(--danger);
}

.chat-feed {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 12px;
    overflow: auto;
    min-height: 0;
}

.message {
    margin: 0 0 12px;
    padding: 0;
    border-radius: 12px;
    border: 1px solid var(--line);
    background: #fbfdff;
    overflow: hidden;
}

.message.user {
    background: #e8f0ff;
    border-color: #c9dafd;
}

.message-title {
    font-size: 12px;
    text-transform: uppercase;
    color: var(--muted);
}

.message-static-title {
    margin: 0;
    padding: 10px 12px 0;
}

.message-panel {
    display: block;
}

.message-panel > summary {
    list-style: none;
    cursor: pointer;
    padding: 10px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.message-panel > summary::-webkit-details-marker {
    display: none;
}

.message-panel > summary::after {
    content: "+";
    font-size: 15px;
    line-height: 1;
    color: var(--muted);
}

.message-panel[open] > summary::after {
    content: "-";
}

.message-body {
    border-top: 1px solid var(--line);
    padding: 10px 12px 12px;
}

.message-body-static {
    border-top: 0;
    padding-top: 8px;
}

.message-body > :first-child {
    margin-top: 0;
}

.message-body > :last-child {
    margin-bottom: 0;
}

.message-actions {
    margin-top: 10px;
    padding-top: 8px;
    border-top: 1px dashed var(--line);
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.message-actions .btn-secondary {
    padding: 6px 10px;
    font-size: 12px;
}

.message-actions .btn-danger {
    padding: 6px 10px;
    font-size: 12px;
}

.message-body ul,
.message-body ol {
    margin: 6px 0 0 18px;
    padding: 0;
}

.message-body pre {
    margin: 8px 0 0;
    max-height: 260px;
    overflow: auto;
    font-size: 12px;
}

.final-answer-content p {
    margin: 0 0 8px;
}

.final-answer-content ul,
.final-answer-content ol {
    margin: 6px 0 8px 18px;
    padding: 0;
}

.final-answer-content > :last-child {
    margin-bottom: 0;
}

.stage-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    font-size: 12px;
    color: var(--muted);
}

.stage-block {
    margin-top: 8px;
}

.stage-error {
    margin-top: 8px;
    color: var(--danger);
}

.logs-panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 8px 12px;
}

.logs-actions {
    margin-top: 8px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}

.logs-actions .btn-secondary {
    padding: 6px 10px;
    font-size: 12px;
}

.logs-mode-label {
    font-size: 12px;
    color: var(--muted);
}

.logs-mode-select {
    height: 30px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    padding: 0 8px;
    font: inherit;
    font-size: 12px;
    color: var(--text);
}

.logs-panel pre {
    margin: 8px 0 0;
    max-height: 220px;
    overflow: auto;
    font-size: 12px;
}

.chat-form {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 10px;
}

.chat-input-stack {
    display: grid;
    gap: 8px;
    min-width: 0;
}

.chat-form textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 12px;
    resize: vertical;
    font: inherit;
}

.startup-prompts-panel {
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #f8fbff;
    padding: 8px 10px;
}

.startup-prompts-panel > summary {
    cursor: pointer;
    font-size: 12px;
    color: var(--muted);
    user-select: none;
}

.startup-prompts-grid {
    margin-top: 8px;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.startup-prompts-grid label {
    display: grid;
    gap: 4px;
    font-size: 12px;
    color: var(--muted);
}

.startup-prompts-grid textarea {
    min-height: 72px;
}

.debate-mode-toggle {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--muted);
    user-select: none;
    cursor: pointer;
    width: fit-content;
}

.debate-mode-toggle input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    pointer-events: none;
}

.debate-mode-toggle-slider {
    width: 34px;
    height: 20px;
    border-radius: 999px;
    background: #d4dce9;
    border: 1px solid #bdc8dd;
    position: relative;
    transition: background-color 0.2s ease;
}

.debate-mode-toggle-slider::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 2px rgba(16, 28, 52, 0.25);
    transition: transform 0.2s ease;
}

.debate-mode-toggle input:checked + .debate-mode-toggle-slider {
    background: #2f6fe8;
    border-color: #2f6fe8;
}

.debate-mode-toggle input:checked + .debate-mode-toggle-slider::after {
    transform: translateX(14px);
}

.debate-mode-toggle input:focus-visible + .debate-mode-toggle-slider {
    outline: 2px solid #2f6fe8;
    outline-offset: 1px;
}

button,
.btn-primary,
.btn-secondary,
.btn-danger {
    font: inherit;
    border-radius: 10px;
    border: 1px solid transparent;
    padding: 9px 12px;
    cursor: pointer;
}

.btn-primary,
button[type="submit"] {
    background: var(--primary);
    color: #fff;
}

.btn-secondary {
    background: #edf2fd;
    color: #123262;
    border-color: #c9d6f3;
}

.btn-danger {
    background: #ffecec;
    color: #892424;
    border-color: #f0b0b0;
}

.admin-page .app-shell {
    height: auto;
    min-height: 100vh;
    overflow: visible;
}

.admin-main {
    display: block;
    height: auto;
    min-height: 100vh;
    overflow: visible;
}

.admin-page .admin-wrap {
    max-width: 1400px;
    margin: 0 auto;
    padding: 4px;
}

.admin-header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
    margin-bottom: 14px;
}

.admin-header h1 {
    margin: 0;
    font-size: 22px;
}

.admin-header nav {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.admin-header nav a {
    text-decoration: none;
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 6px 10px;
    background: #fff;
}

.admin-header nav a.active {
    border-color: var(--primary);
    background: #e8f0ff;
    color: #0f3f8f;
    font-weight: 600;
}

.card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 14px;
    margin-bottom: 14px;
}

.card-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

#add-agent-panel {
    max-width: 800px;
    margin: 0 auto 50px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #f8fbff;
}

.flash {
    margin-bottom: 12px;
    padding: 10px 12px;
    border-radius: 10px;
}

.flash.success {
    background: #e6f6ec;
    color: var(--ok);
    border: 1px solid #b9e2c7;
}

.flash.error {
    background: #ffecec;
    color: var(--danger);
    border: 1px solid #f2c3c3;
}

.admin-form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.admin-form-grid label,
.inline-form label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13px;
}

.admin-form-grid .full-width {
    grid-column: 1 / -1;
}

input,
select,
textarea {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px;
    font: inherit;
}

.table-scroll {
    overflow: auto;
}

.admin-table {
    border-collapse: collapse;
    width: 100%;
    min-width: 800px;
}

.agents-table {
    width: 100%;
    table-layout: fixed;
    min-width: 100%;
}

.agents-table .col-id { width: 6%; }
.agents-table .col-label { width: 17%; }
.agents-table .col-provider { width: 14%; }
.agents-table .col-api-key { width: 20%; }
.agents-table .col-enabled { width: 10%; }
.agents-table .col-notes { width: 21%; }
.agents-table .col-actions { width: 12%; }

.admin-table th,
.admin-table td {
    border-bottom: 1px solid var(--line);
    padding: 8px;
    text-align: left;
    vertical-align: top;
    font-size: 13px;
}

.agents-table th,
.agents-table td {
    overflow: hidden;
}

.inline-form {
    display: inline-flex;
    gap: 6px;
    align-items: center;
    flex-wrap: wrap;
}

.inline-check {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--muted);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.inline-danger {
    margin-top: 6px;
}

.agent-row .agent-edit,
.agent-row .agent-actions-edit {
    display: none;
}

.agent-row .agent-view,
.agent-row .agent-edit {
    width: 100%;
}

.agent-row.is-editing .agent-view,
.agent-row.is-editing .agent-actions-view {
    display: none;
}

.agent-row.is-editing .agent-edit {
    display: block;
}

.agent-row.is-editing .agent-actions-edit {
    display: inline-flex;
    gap: 6px;
    align-items: center;
}

.agent-edit input,
.agent-edit select,
.agent-edit textarea {
    width: 100%;
}

.agent-view {
    display: block;
    min-height: 20px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.agent-actions-cell {
    white-space: nowrap;
    text-align: center;
}

.agent-actions-view {
    display: inline-flex;
    gap: 6px;
    align-items: center;
}

.agent-update-form,
.agent-delete-form {
    display: none;
}

.models-toolbar {
    display: inline-flex;
    align-items: center;
    gap: 10px;
}

.models-sync-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.models-sync-btn .sync-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255, 255, 255, 0.45);
    border-top-color: #fff;
    border-radius: 50%;
    display: none;
    animation: spin 0.8s linear infinite;
}

.models-sync-btn.is-loading .sync-spinner {
    display: inline-block;
}

.models-sync-feedback {
    font-size: 13px;
    color: var(--muted);
}

.models-sync-feedback.success {
    color: var(--ok);
}

.models-sync-feedback.error {
    color: var(--danger);
}

.models-sync-feedback a {
    color: inherit;
    text-decoration: underline;
}

.models-table .favorite-cell {
    text-align: center;
    width: 56px;
}

.models-table th[data-sortable="1"] {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}

.models-table th[data-sortable="1"]::after {
    content: " ↕";
    color: #8090a8;
    font-size: 11px;
}

.models-table th[data-sortable="1"][data-sort-direction="asc"]::after {
    content: " ↑";
    color: #123262;
}

.models-table th[data-sortable="1"][data-sort-direction="desc"]::after {
    content: " ↓";
    color: #123262;
}

.models-table th[data-sortable="1"]:focus-visible {
    outline: 2px solid #739fe8;
    outline-offset: -2px;
}

.models-row-hidden {
    display: none;
}

.models-expand-wrap {
    margin-top: 10px;
    display: flex;
    justify-content: center;
}

.models-expand-btn {
    min-width: 140px;
}

.icon-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--text);
}

.icon-btn span,
.icon-btn svg {
    display: block;
}

.icon-edit {
    border-color: #b8c9ee;
    background: #edf2fd;
    color: #123262;
}

.icon-save {
    border-color: #b9e2c7;
    background: #e6f6ec;
    color: var(--ok);
}

.icon-cancel {
    border-color: #d6dbe6;
    background: #f3f5fa;
    color: #44566f;
}

.icon-delete {
    border-color: #f2c3c3;
    background: #ffecec;
    color: var(--danger);
}

.icon-favorite {
    color: #a1adbf;
}

.icon-favorite.is-active {
    color: #d19a00;
    border-color: #f0d48a;
    background: #fff7df;
}

pre {
    background: #f8fbff;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px;
    overflow: auto;
}

.auth-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
}

.auth-wrap {
    width: 100%;
    max-width: 420px;
}

.auth-card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 18px;
    box-shadow: 0 10px 24px rgba(17, 34, 68, 0.08);
}

.auth-card h1 {
    margin: 0 0 6px;
    font-size: 24px;
}

.auth-note {
    margin: 0 0 14px;
    color: var(--muted);
    font-size: 14px;
}

.auth-form {
    display: grid;
    gap: 10px;
}

.auth-form label {
    font-size: 13px;
    color: var(--muted);
}

.auth-form input {
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 9px 10px;
    font: inherit;
}

@media (max-width: 980px) {
    .app-shell,
    .app-shell.sidebar-collapsed {
        grid-template-columns: 1fr;
    }

    .sidebar {
        border-right: none;
        border-bottom: 1px solid var(--line);
    }

    .chat-form {
        grid-template-columns: 1fr;
    }

    .startup-prompts-grid {
        grid-template-columns: 1fr;
    }

    .admin-form-grid {
        grid-template-columns: 1fr;
    }
}

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