:root {
    color-scheme: light dark;
    --bg: #f6f7fb;
    --sidebar-bg: #ffffff;
    --sidebar-text: #0f172a;
    --accent: #2563eb;
    --accent-light: rgba(37, 99, 235, 0.12);
    --border: rgba(15, 23, 42, 0.1);
    --sidebar-border: rgba(15, 23, 42, 0.08);
    --muted: #64748b;
    font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
}

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

.app-shell {
    display: grid;
    grid-template-columns: 320px 1fr;
    height: 100vh;
    overflow: hidden;
}

/* ---- Sidebar ---- */
aside.sidebar {
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    padding: 1.5rem 1.25rem;
    gap: 1.5rem;
    overflow-y: auto;
    border-right: 1px solid var(--sidebar-border);
}

.sidebar-logo {
    display: flex;
    justify-content: center;
    align-items: center;
    padding-bottom: 0.25rem;
}

.sidebar-logo img {
    height: 56px;
    width: auto;
    object-fit: contain;
}

.sidebar-spacer { flex: 1; }

.sidebar header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar h1 {
    font-size: 1.2rem;
    margin: 0;
}

.user-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.6rem 0.9rem;
    background: #f8fafc;
    border: 1px solid var(--sidebar-border);
    border-radius: 0.9rem;
    font-size: 0.85rem;
}

.user-info .user-name {
    color: #0f172a;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

button.logout-btn {
    background: transparent;
    border: 1px solid var(--sidebar-border);
    border-radius: 0.65rem;
    color: var(--muted);
    padding: 0.25rem 0.7rem;
    cursor: pointer;
    font-size: 0.8rem;
    font-family: inherit;
    transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease;
    white-space: nowrap;
    flex-shrink: 0;
}

button.logout-btn:hover {
    background: rgba(239, 68, 68, 0.08);
    border-color: rgba(239, 68, 68, 0.3);
    color: #dc2626;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.sidebar-nav-btn {
    background: transparent;
    border: none;
    border-radius: 0.75rem;
    color: var(--muted);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.88rem;
    font-weight: 500;
    padding: 0.55rem 0.85rem;
    text-align: left;
    transition: background 0.15s ease, color 0.15s ease;
}

.sidebar-nav-btn:hover {
    background: var(--accent-light);
    color: var(--accent);
}

.sidebar-nav-btn.active {
    background: var(--accent-light);
    color: var(--accent);
    font-weight: 600;
}

a.sidebar-nav-link {
    display: block;
    text-decoration: none;
}

button.refresh {
    background: transparent;
    border: 1px solid var(--sidebar-border);
    border-radius: 0.75rem;
    color: var(--muted);
    padding: 0.35rem 0.8rem;
    cursor: pointer;
    font-size: 0.85rem;
    transition: background 0.2s ease;
}

button.refresh:hover {
    background: rgba(15, 23, 42, 0.05);
}

/* ---- Agent select (chat header) ---- */
.agent-select {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.agent-select label {
    font-size: 0.82rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
    white-space: nowrap;
}

.agent-select select {
    appearance: none;
    background-color: #ffffff;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    color: #0f172a;
    font-family: inherit;
    font-size: 0.9rem;
    padding: 0.55rem 2.2rem 0.55rem 0.9rem;
    cursor: pointer;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    min-width: 140px;
    max-width: 200px;
}

.agent-select select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.agent-select select:disabled {
    cursor: not-allowed;
    opacity: 0.5;
    background-color: #f8fafc;
}

/* ---- Agent details (hidden, DOM-only) ---- */
.agent-details {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 1rem;
    background: #f8fafc;
    border: 1px solid var(--sidebar-border);
    border-radius: 1rem;
}

.agent-tools-summary {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
    margin-top: 0.35rem;
}

.agent-tools-summary .tool-chip {
    background: rgba(37, 99, 235, 0.18);
    color: #1d4ed8;
}

.agent-tools-summary .no-tools {
    font-size: 0.85rem;
    color: var(--muted);
}

/* ---- Chat panel ---- */
main.chat-panel {
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 0;
    overflow: hidden;
}

#chatView {
    display: flex;
    flex-direction: column;
    flex: 1;
    gap: 0;
    overflow: hidden;
}

.chat-header {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 1rem;
    margin-bottom: 1.5rem;
    flex-shrink: 0;
}

.chat-header-text {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.chat-header h2 {
    margin: 0;
    font-size: 1.5rem;
}

.chat-header span.meta {
    color: var(--muted);
    font-size: 0.9rem;
}

.conversation {
    flex: 1;
    background: white;
    border-radius: 1.25rem;
    border: 1px solid var(--border);
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    overflow-y: auto;
    margin-bottom: 1.5rem;
}

.message {
    max-width: 80%;
    padding: 0.9rem 1.1rem;
    border-radius: 1rem;
    line-height: 1.5;
    box-shadow: 0 6px 16px rgba(15, 23, 42, 0.08);
    animation: fade-in 0.2s ease;
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    white-space: pre-wrap;
}

.message.user {
    align-self: flex-end;
    background: var(--accent);
    color: white;
}

.message.assistant {
    align-self: flex-start;
    background: var(--accent-light);
    color: #0f172a;
}

.message.system {
    align-self: center;
    background: #e2e8f0;
    color: #1f2937;
}

.tool-usage {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem;
    padding-top: 0.4rem;
    border-top: 1px solid rgba(15, 23, 42, 0.1);
    font-size: 0.8rem;
    color: var(--muted);
}

.tool-usage-label {
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
    font-size: 0.75rem;
}

.tool-chip {
    background: rgba(37, 99, 235, 0.12);
    color: #1d4ed8;
    border-radius: 999px;
    padding: 0.25rem 0.65rem;
    font-size: 0.8rem;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}

.tool-chip.tool-chip-none {
    background: rgba(148, 163, 184, 0.24);
    color: #475569;
}

.tool-chip .tool-chip-meta {
    color: inherit;
    font-size: 0.75rem;
    opacity: 0.85;
}

@keyframes fade-in {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

form.chat-input {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.9rem;
    align-items: center;
}

form.chat-input textarea {
    resize: vertical;
    min-height: 70px;
    max-height: 160px;
    padding: 1rem;
    border-radius: 1rem;
    border: 1px solid var(--border);
    font-size: 1rem;
    font-family: inherit;
    background: #ffffff;
    color: #0f172a;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

form.chat-input textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

form.chat-input button {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 1rem;
    padding: 0.9rem 1.6rem;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 12px 24px rgba(37, 99, 235, 0.2);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

form.chat-input button:disabled {
    background: rgba(37, 99, 235, 0.5);
    cursor: not-allowed;
    box-shadow: none;
}

form.chat-input button:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 16px 30px rgba(37, 99, 235, 0.3);
}

/* ---- Users panel ---- */
.users-panel { display: none; flex-direction: column; gap: 1.5rem; }
.users-panel.visible { display: flex; }

.users-panel h2 {
    margin: 0;
    font-size: 1.25rem;
}

.users-table-wrap {
    background: white;
    border: 1px solid var(--border);
    border-radius: 1.25rem;
    overflow: hidden;
}

.users-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.users-table th {
    background: #f8fafc;
    color: var(--muted);
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    padding: 0.75rem 1.25rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.users-table td {
    padding: 0.85rem 1.25rem;
    border-bottom: 1px solid var(--border);
    color: #0f172a;
}

.users-table tr:last-child td { border-bottom: none; }
.users-table tr:hover td { background: #f8fafc; }
.users-table .td-muted { color: var(--muted); font-size: 0.82rem; }

/* ---- Role badges ---- */
.role-badge {
    display: inline-block;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    padding: 0.2rem 0.6rem;
    text-transform: uppercase;
}

.role-badge--admin {
    background: rgba(37, 99, 235, 0.12);
    color: #1d4ed8;
}

.role-badge--user {
    background: rgba(100, 116, 139, 0.12);
    color: #475569;
}

/* ---- Role toggle button ---- */
.btn-role {
    background: none;
    border: 1px solid rgba(37, 99, 235, 0.3);
    border-radius: 0.5rem;
    color: #2563eb;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.8rem;
    padding: 0.25rem 0.7rem;
    transition: background 0.15s ease;
}

.btn-role:hover { background: rgba(37, 99, 235, 0.07); }
.btn-role:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-delete {
    background: none;
    border: 1px solid rgba(239, 68, 68, 0.3);
    border-radius: 0.5rem;
    color: #dc2626;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.8rem;
    padding: 0.25rem 0.7rem;
    transition: background 0.15s ease;
}

.btn-delete:hover { background: rgba(239, 68, 68, 0.07); }

.add-user-card {
    background: white;
    border: 1px solid var(--border);
    border-radius: 1.25rem;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.add-user-card h3 {
    margin: 0;
    font-size: 1rem;
    color: #0f172a;
}

.add-user-form {
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    gap: 0.75rem;
    align-items: end;
}

.add-user-form .form-field {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.add-user-form label {
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
}

.add-user-form input {
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    color: #0f172a;
    font-family: inherit;
    font-size: 0.9rem;
    padding: 0.65rem 0.9rem;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.add-user-form input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.btn-add-user {
    background: var(--accent);
    border: none;
    border-radius: 0.75rem;
    color: white;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    padding: 0.65rem 1.25rem;
    transition: background 0.15s ease;
    white-space: nowrap;
}

.btn-add-user:hover:not(:disabled) { background: #1d4ed8; }
.btn-add-user:disabled { opacity: 0.5; cursor: not-allowed; }

.users-error {
    color: #dc2626;
    font-size: 0.85rem;
    display: none;
}

.users-error.visible { display: block; }

/* ---- Misc ---- */
.empty-state {
    text-align: center;
    color: var(--muted);
    padding: 2rem 1rem;
}

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

.toast {
    position: fixed;
    right: 2rem;
    bottom: 2rem;
    background: #1e293b;
    color: white;
    padding: 1rem 1.4rem;
    border-radius: 0.9rem;
    box-shadow: 0 12px 34px rgba(15, 23, 42, 0.35);
    opacity: 0;
    pointer-events: none;
    transform: translateY(12px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.active {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

/* ---- Responsive ---- */
@media (max-width: 700px) {
    .add-user-form { grid-template-columns: 1fr; }
}

@media (max-width: 980px) {
    .app-shell { grid-template-columns: 1fr; }
    aside.sidebar { gap: 1.1rem; }

    .agent-select {
        width: 100%;
        flex-direction: column;
        align-items: flex-start;
    }

    .agent-select select {
        min-width: unset;
        width: 100%;
    }

    .chat-header {
        flex-direction: column;
        align-items: flex-start;
    }
}
