:root {
    color-scheme: light dark;
    --bg: #f7f7f5;
    --panel: #ffffff;
    --panel-soft: #eeeeeb;
    --text: #1d1d1b;
    --muted: #666b70;
    --line: #d8d8d2;
    --accent: #226f54;
    --accent-dark: #17513d;
    --danger-bg: #fff0ef;
    --danger-text: #8a1f16;
    --radius: 8px;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #141513;
        --panel: #1f211f;
        --panel-soft: #282b28;
        --text: #f1f0ec;
        --muted: #aaa79f;
        --line: #3d403a;
        --accent: #65bd91;
        --accent-dark: #4b9e76;
        --danger-bg: #351c1a;
        --danger-text: #ffaaa1;
    }
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
}

a {
    color: inherit;
}

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

input,
textarea,
select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel);
    color: var(--text);
    padding: 12px 13px;
}

textarea {
    resize: vertical;
    min-height: 58px;
    max-height: 180px;
}

button,
.primary,
.secondary,
.new-chat {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    border-radius: var(--radius);
    border: 1px solid transparent;
    padding: 0 16px;
    text-decoration: none;
    cursor: pointer;
}

.primary,
.new-chat {
    background: var(--accent);
    color: white;
}

.primary:hover,
.new-chat:hover {
    background: var(--accent-dark);
}

.secondary,
.ghost {
    background: var(--panel);
    border-color: var(--line);
    color: var(--text);
}

.ghost {
    min-height: 34px;
    padding: 0 10px;
}

.muted,
.empty {
    color: var(--muted);
}

.eyebrow {
    margin: 0 0 8px;
    color: var(--accent);
    font-weight: 700;
    text-transform: uppercase;
    font-size: 0.76rem;
}

.alert {
    background: var(--danger-bg);
    color: var(--danger-text);
    border: 1px solid color-mix(in srgb, var(--danger-text), transparent 65%);
    border-radius: var(--radius);
    padding: 12px 14px;
}

.notice {
    background: color-mix(in srgb, var(--accent), transparent 84%);
    color: var(--text);
    border: 1px solid color-mix(in srgb, var(--accent), transparent 55%);
    border-radius: var(--radius);
    padding: 12px 14px;
}

.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}

.login-shell {
    width: min(100%, 430px);
}

.login-panel,
.admin-form,
.table-panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 24px;
}

.login-panel h1,
.page-header h1,
.chat-header h1 {
    margin: 0;
    line-height: 1.1;
}

.stack,
.admin-form {
    display: grid;
    gap: 16px;
}

.app-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 310px 1fr;
}

.sidebar {
    background: var(--panel);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    padding: 18px;
    gap: 16px;
}

.brand {
    display: grid;
    gap: 4px;
}

.brand span,
.conversation-list small {
    color: var(--muted);
    font-size: 0.82rem;
}

.conversation-list {
    display: grid;
    gap: 6px;
    overflow-y: auto;
}

.conversation-item {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 6px;
    border-radius: var(--radius);
}

.conversation-item:hover,
.conversation-item.active {
    background: var(--panel-soft);
}

.conversation-item a {
    display: grid;
    gap: 4px;
    min-width: 0;
    text-decoration: none;
    padding: 10px 4px 10px 10px;
}

.conversation-list span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.icon-danger {
    width: 32px;
    min-height: 32px;
    padding: 0;
    margin-right: 5px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    background: transparent;
    color: var(--muted);
    font-size: 1.4rem;
    line-height: 1;
}

.icon-danger:hover,
.danger:hover {
    color: var(--danger-text);
    border-color: color-mix(in srgb, var(--danger-text), transparent 60%);
    background: var(--danger-bg);
}

.sidebar-footer {
    margin-top: auto;
    display: grid;
    gap: 10px;
}

.chat-main {
    min-height: 100vh;
    display: grid;
    grid-template-rows: auto 1fr auto;
}

.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 20px 28px;
    border-bottom: 1px solid var(--line);
}

.chat-header p {
    margin: 8px 0 0;
    color: var(--muted);
}

.messages {
    padding: 28px;
    overflow-y: auto;
}

.message {
    display: flex;
    margin: 0 auto 18px;
    max-width: 860px;
}

.message.user {
    justify-content: flex-end;
}

.bubble {
    max-width: 78%;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 13px 15px;
    background: var(--panel);
    line-height: 1.55;
}

.message.user .bubble {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
}

.bubble p:first-child {
    margin-top: 0;
}

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

.bubble pre {
    overflow-x: auto;
    background: var(--panel-soft);
    border-radius: var(--radius);
    padding: 12px;
}

.welcome {
    min-height: 52vh;
    display: grid;
    place-content: center;
    text-align: center;
    gap: 8px;
}

.welcome h2 {
    font-size: clamp(2rem, 6vw, 3.4rem);
    margin: 0;
}

.composer {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    padding: 18px 28px 24px;
    border-top: 1px solid var(--line);
    background: color-mix(in srgb, var(--bg), var(--panel) 35%);
}

.composer-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
}

.model-select {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: min(100%, 310px);
}

.model-select span,
.thinking-status {
    color: var(--muted);
    font-size: 0.9rem;
}

.model-select select {
    min-height: 42px;
    padding: 8px 10px;
}

.thinking-status {
    min-width: 78px;
}

button:disabled {
    cursor: wait;
    opacity: 0.68;
}

.page {
    width: min(1180px, calc(100% - 32px));
    margin: 0 auto;
    padding: 32px 0;
    display: grid;
    gap: 22px;
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
}

.admin-grid {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 22px;
    align-items: start;
}

table {
    width: 100%;
    border-collapse: collapse;
}

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

th {
    color: var(--muted);
    font-weight: 700;
    font-size: 0.86rem;
}

.actions,
.inline-reset {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.inline-reset input {
    width: 160px;
    min-height: 34px;
    padding: 6px 8px;
}

.memory-panel {
    overflow-x: auto;
}

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

    .sidebar {
        min-height: auto;
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }

    .conversation-list {
        grid-auto-flow: column;
        grid-auto-columns: minmax(180px, 240px);
        overflow-x: auto;
    }

    .chat-header,
    .page-header {
        align-items: stretch;
        flex-direction: column;
    }

    .composer-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .model-select {
        align-items: stretch;
        flex-direction: column;
        min-width: 0;
    }

    .bubble {
        max-width: 92%;
    }

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

    .table-panel {
        overflow-x: auto;
    }
}
