:root {
    --bg: #f4f6f8;
    --panel: #ffffff;
    --ink: #1f2a37;
    --muted: #667085;
    --line: #d7dde4;
    --brand: #0f766e;
    --brand-ink: #ffffff;
    --danger: #b42318;
    --shadow: 0 8px 24px rgba(16, 24, 40, 0.08);
    --radius: 14px;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    color: var(--ink);
    background:
        radial-gradient(circle at 10% -10%, #d1fae5 0%, transparent 40%),
        radial-gradient(circle at 100% 0%, #dbeafe 0%, transparent 35%),
        var(--bg);
}

a {
    color: var(--brand);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

.app-shell {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 18px;
    justify-content: space-between;
    padding: 14px 22px;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--line);
}

.brand {
    font-weight: 700;
    letter-spacing: 0.3px;
    font-size: 20px;
}

.topnav {
    display: flex;
    gap: 12px;
}

.topnav a {
    padding: 8px 12px;
    border-radius: 10px;
    color: var(--ink);
    text-decoration: none;
}

.topnav a.active,
.topnav a:hover {
    background: #e6f7f5;
    color: #0b5550;
}

.userbar {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 14px;
}

.content {
    width: min(1200px, 96vw);
    margin: 20px auto;
    display: grid;
    gap: 16px;
}

.card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
}

h1,
h2,
h3 {
    margin: 0 0 12px;
}

p,
label {
    color: var(--muted);
}

.grid-3 {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
}

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

.stat {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 12px;
    background: #f9fbfc;
}

.stat .v {
    display: block;
    font-size: 24px;
    font-weight: 700;
    color: var(--ink);
}

.actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.btn,
button {
    border: none;
    border-radius: 10px;
    background: var(--brand);
    color: var(--brand-ink);
    padding: 10px 14px;
    cursor: pointer;
    font-weight: 600;
}

.btn.secondary,
button.secondary {
    background: #e2e8f0;
    color: var(--ink);
}

.btn:hover,
button:hover {
    filter: brightness(0.96);
}

.form-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: end;
}

.field {
    min-width: 180px;
    flex: 1;
}

input,
select,
textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px;
    font: inherit;
    color: var(--ink);
    background: #fff;
}

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

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 900px;
}

th,
td {
    border-bottom: 1px solid var(--line);
    padding: 10px;
    text-align: left;
    vertical-align: top;
}

th {
    background: #f8fafc;
    font-size: 13px;
    color: var(--muted);
}

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

.mono {
    font-family: "JetBrains Mono", "Fira Code", ui-monospace, monospace;
}

pre.log {
    white-space: pre-wrap;
    word-break: break-word;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #0b1220;
    color: #dbe7ff;
    padding: 12px;
    max-height: 70vh;
    overflow: auto;
}

.audio-player {
    width: 260px;
    max-width: 100%;
}

.badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 999px;
    background: #e6f7f5;
    color: #0b5550;
    font-size: 12px;
}

.badge-ok {
    background: #dcfce7;
    color: #166534;
}

.badge-warn {
    background: #fef3c7;
    color: #92400e;
}

.badge-err {
    background: #fee2e2;
    color: #991b1b;
}

.stage-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 8px;
}

.stage-item {
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 8px 10px;
    display: grid;
    grid-template-columns: 20px 1fr;
    column-gap: 8px;
    row-gap: 2px;
    align-items: center;
}

.stage-item .small {
    grid-column: 2;
}

.stage-item input[type="checkbox"] {
    width: 16px;
    height: 16px;
}

.setting-option-wrap {
    display: grid;
    gap: 6px;
}

.setting-name {
    font-weight: 600;
}

.setting-desc {
    margin-top: 4px;
}

.setting-custom-input.is-hidden {
    display: none;
}

.error {
    border: 1px solid #fecaca;
    background: #fef2f2;
    color: var(--danger);
    border-radius: 10px;
    padding: 10px;
}

.login-shell {
    max-width: 420px;
    margin: 8vh auto;
}

.footer {
    margin-top: auto;
    padding: 14px 20px;
    border-top: 1px solid var(--line);
    color: var(--muted);
    text-align: center;
}

.pagination {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.chat-layout {
    display: grid;
    grid-template-columns: 330px minmax(0, 1fr);
    gap: 16px;
    align-items: start;
}

.chat-sidebar {
    display: grid;
    gap: 12px;
    align-self: start;
}

.chat-thread-list {
    display: grid;
    gap: 8px;
    max-height: 42vh;
    overflow: auto;
    padding-right: 2px;
}

.chat-thread-item {
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #f8fafc;
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    padding: 8px;
}

.chat-thread-item.active {
    border-color: #93c5fd;
    background: #eff6ff;
}

.chat-thread-link {
    display: grid;
    gap: 4px;
    text-decoration: none;
}

.chat-thread-title {
    color: var(--ink);
    font-weight: 600;
    line-height: 1.3;
}

.chat-thread-delete {
    display: flex;
    align-items: start;
}

.chat-thread-delete .btn {
    padding: 4px 9px;
    min-height: 30px;
}

.chat-memory-box {
    border-top: 1px solid var(--line);
    padding-top: 10px;
}

.chat-memory-form textarea {
    min-height: 140px;
}

.chat-main {
    display: grid;
    gap: 12px;
}

.chat-messages {
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fbfdff;
    padding: 12px;
    max-height: 68vh;
    overflow: auto;
    display: grid;
    gap: 10px;
}

.chat-empty {
    color: var(--muted);
    text-align: center;
    padding: 30px 12px;
}

.chat-msg {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 12px;
    background: #fff;
}

.chat-msg.user {
    border-color: #bfdbfe;
    background: #eff6ff;
}

.chat-msg.assistant {
    border-color: #bbf7d0;
    background: #f0fdf4;
}

.chat-msg.system {
    border-color: #fde68a;
    background: #fffbeb;
}

.chat-msg-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 6px;
}

.chat-msg-body {
    white-space: pre-wrap;
    line-height: 1.45;
}

.chat-input-form textarea {
    min-height: 90px;
}

@media (max-width: 900px) {
    .topbar {
        flex-wrap: wrap;
        justify-content: center;
    }

    .content {
        width: min(1200px, 98vw);
    }

    .grid-3 {
        grid-template-columns: 1fr;
    }

    .grid-2 {
        grid-template-columns: 1fr;
    }

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

    .chat-thread-list {
        max-height: 30vh;
    }

    .chat-messages {
        max-height: 52vh;
    }
}
