:root {
    color-scheme: light;
    --bg: #f5f7f8;
    --panel: #ffffff;
    --panel-2: #eef3f2;
    --text: #17212b;
    --muted: #65717d;
    --line: #dce5e3;
    --primary: #0f766e;
    --primary-strong: #115e59;
    --danger: #b42318;
    --shadow: 0 20px 50px rgba(20, 35, 45, 0.12);
}

[data-theme="dark"] {
    color-scheme: dark;
    --bg: #101418;
    --panel: #171d22;
    --panel-2: #20282e;
    --text: #edf3f2;
    --muted: #9aa8b2;
    --line: #2d3941;
    --primary: #2dd4bf;
    --primary-strong: #14b8a6;
    --danger: #f97066;
    --shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

button {
    cursor: pointer;
}

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

.login-panel {
    width: min(100%, 420px);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 28px;
    box-shadow: var(--shadow);
}

.brand-mark {
    width: 42px;
    height: 42px;
    border-radius: 8px;
    display: grid;
    place-items: center;
    background: var(--primary);
    color: #ffffff;
    font-weight: 800;
}

.login-panel h1 {
    margin: 18px 0 6px;
    font-size: 1.6rem;
}

.login-panel p {
    margin: 0 0 22px;
    color: var(--muted);
}

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

.login-form input,
.composer textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--text);
    padding: 12px 14px;
    outline: none;
}

.login-form input:focus,
.composer textarea:focus {
    border-color: var(--primary);
}

.login-form button,
.new-chat,
.composer button {
    border: 0;
    border-radius: 8px;
    background: var(--primary);
    color: #ffffff;
    padding: 12px 14px;
    font-weight: 700;
}

.login-form button:hover,
.new-chat:hover,
.composer button:hover {
    background: var(--primary-strong);
}

.alert {
    margin: 0 0 18px;
    padding: 12px;
    border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
    border-radius: 8px;
    color: var(--danger);
    background: color-mix(in srgb, var(--danger) 8%, transparent);
}

.alert-success {
    border-color: color-mix(in srgb, var(--primary) 35%, transparent);
    color: var(--primary-strong);
    background: color-mix(in srgb, var(--primary) 9%, transparent);
}

.panel-link {
    display: inline-block;
    margin-top: 18px;
    color: var(--muted);
    text-decoration: none;
}

.panel-link:hover {
    color: var(--primary);
}

.admin-shell {
    min-height: 100vh;
    padding: 24px;
}

.admin-panel {
    width: min(100%, 1120px);
    margin: 0 auto;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 24px;
    box-shadow: var(--shadow);
}

.admin-panel-narrow {
    width: min(100%, 620px);
}

.admin-header {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
}

.admin-header h1 {
    margin: 0 0 4px;
    font-size: 1.5rem;
}

.admin-header p {
    margin: 0;
    color: var(--muted);
}

.admin-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.button-primary,
.button-secondary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    border-radius: 8px;
    padding: 10px 14px;
    font-weight: 700;
    text-decoration: none;
}

.button-primary {
    background: var(--primary);
    color: #ffffff;
}

.button-primary:hover {
    background: var(--primary-strong);
}

.button-secondary {
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--text);
}

.button-secondary:hover {
    background: var(--panel-2);
}

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

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

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

.admin-table th {
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 700;
}

.status-pill {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    border-radius: 8px;
    padding: 4px 8px;
    font-size: 0.85rem;
    font-weight: 700;
}

.status-pill.active {
    color: var(--primary-strong);
    background: color-mix(in srgb, var(--primary) 10%, transparent);
}

.status-pill.inactive {
    color: var(--muted);
    background: var(--panel-2);
}

.table-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.table-actions a,
.table-actions button {
    border: 0;
    background: transparent;
    color: var(--primary);
    padding: 0;
    text-decoration: none;
    font-weight: 700;
}

.table-actions button {
    color: var(--danger);
}

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

.admin-form input,
.admin-form select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--text);
    padding: 12px 14px;
    outline: none;
}

.admin-form input:focus,
.admin-form select:focus {
    border-color: var(--primary);
}

.admin-form button {
    border: 0;
    border-radius: 8px;
    background: var(--primary);
    color: #ffffff;
    padding: 12px 14px;
    font-weight: 700;
}

.admin-form button:hover {
    background: var(--primary-strong);
}

.field-hint {
    margin-top: -4px;
    color: var(--muted);
    font-size: 0.9rem;
}

.check-row {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    margin: 6px 0;
}

.check-row input {
    width: auto;
}

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

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

.sidebar-header,
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.sidebar-header span,
.chat-header span {
    display: block;
    color: var(--muted);
    font-size: 0.9rem;
    margin-top: 2px;
}

.session-list {
    display: grid;
    gap: 6px;
    overflow: auto;
    min-height: 0;
}

.session-item {
    width: 100%;
    border: 1px solid transparent;
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    display: grid;
    grid-template-columns: minmax(0, 1fr) 32px;
    align-items: center;
    gap: 4px;
}

.session-item:hover,
.session-item.active {
    background: var(--panel-2);
    border-color: var(--line);
}

.session-open,
.session-delete {
    border: 0;
    background: transparent;
    color: inherit;
}

.session-open {
    min-width: 0;
    padding: 11px 4px 11px 12px;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.session-delete {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    color: var(--muted);
}

.session-delete:hover {
    background: color-mix(in srgb, var(--danger) 12%, transparent);
    color: var(--danger);
}

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

.sidebar-footer a {
    color: var(--muted);
    text-decoration: none;
    padding: 10px 2px;
}

.sidebar-footer a:hover {
    color: var(--primary);
}

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

.chat-header {
    min-height: 68px;
    padding: 14px 18px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
}

.chat-header h1 {
    margin: 0;
    font-size: 1.15rem;
}

.icon-button {
    width: 40px;
    height: 40px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--text);
}

.messages {
    overflow-y: auto;
    min-height: 0;
    padding: 18px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.empty-state {
    margin: auto;
    text-align: center;
    color: var(--muted);
    display: grid;
    justify-items: center;
    gap: 10px;
}

.empty-state h2 {
    color: var(--text);
    margin: 0;
    font-size: 1.3rem;
}

.empty-state p {
    margin: 0;
}

.message {
    display: grid;
    grid-template-columns: 34px minmax(0, 720px);
    gap: 10px;
    align-items: start;
}

.message.user {
    grid-template-columns: minmax(0, 720px) 34px;
    justify-content: end;
}

.message.user .avatar {
    order: 2;
}

.avatar {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    display: grid;
    place-items: center;
    background: var(--panel-2);
    color: var(--text);
    font-weight: 800;
}

.bubble {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px 14px;
    background: var(--panel);
    line-height: 1.5;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.message.user .bubble {
    background: var(--primary);
    color: #ffffff;
    border-color: var(--primary);
}

.message.error .bubble {
    border-color: color-mix(in srgb, var(--danger) 40%, var(--line));
    color: var(--danger);
}

.typing {
    padding: 8px 18px;
    color: var(--muted);
    font-size: 0.92rem;
}

.composer {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    padding: 14px 18px 18px;
    background: var(--panel);
    border-top: 1px solid var(--line);
}

.composer textarea {
    min-height: 46px;
    max-height: 180px;
    resize: none;
}

.mobile-only,
.backdrop {
    display: none;
}

@media (max-width: 800px) {
    .admin-shell {
        padding: 12px;
    }

    .admin-panel {
        padding: 18px;
    }

    .admin-header {
        display: grid;
    }

    .admin-actions {
        justify-content: stretch;
    }

    .button-primary,
    .button-secondary {
        width: 100%;
    }

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

    .sidebar {
        position: fixed;
        z-index: 30;
        inset: 0 auto 0 0;
        width: min(86vw, 320px);
        transform: translateX(-105%);
        transition: transform 180ms ease;
        box-shadow: var(--shadow);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .backdrop.open {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.35);
        z-index: 20;
    }

    .mobile-only {
        display: inline-grid;
        place-items: center;
    }

    .chat-header {
        padding: 12px;
    }

    .messages {
        padding: 14px 10px;
    }

    .message,
    .message.user {
        grid-template-columns: 30px minmax(0, 1fr);
        justify-content: stretch;
    }

    .message.user .avatar {
        order: 0;
    }

    .composer {
        grid-template-columns: 1fr;
        padding: 10px;
    }
}
