:root {
    --bg: #0f1419;
    --surface: #1a2332;
    --surface-2: #243044;
    --border: #2d3a4f;
    --text: #e8edf5;
    --muted: #8b9cb3;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --error: #ef4444;
    --success: #22c55e;
    --radius: 12px;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: Inter, system-ui, -apple-system, sans-serif;
    background: linear-gradient(160deg, #0b1020 0%, #121a2b 45%, #0f1419 100%);
    color: var(--text);
    min-height: 100vh;
}

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

.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 32px;
    box-shadow: var(--shadow);
}

.brand {
    display: flex;
    gap: 16px;
    align-items: center;
    margin-bottom: 28px;
}

.brand.compact h1 {
    font-size: 1.1rem;
}

.brand-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    background: linear-gradient(135deg, var(--primary), #6366f1);
    font-weight: 700;
    font-size: 1.4rem;
}

.brand h1 {
    margin: 0;
    font-size: 1.5rem;
}

.brand p {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: 0.9rem;
}

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

label {
    font-size: 0.85rem;
    color: var(--muted);
    margin-top: 8px;
}

input, textarea {
    width: 100%;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    padding: 12px 14px;
    font: inherit;
}

input:focus, textarea:focus {
    outline: 2px solid rgba(59, 130, 246, 0.35);
    border-color: var(--primary);
}

.btn {
    border: none;
    border-radius: 10px;
    padding: 12px 18px;
    font: inherit;
    cursor: pointer;
    transition: background 0.15s ease;
}

.btn-primary {
    background: var(--primary);
    color: white;
    margin-top: 12px;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.btn-ghost {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
}

.chat-layout {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

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

.sidebar {
    width: 280px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    min-height: 100vh;
    height: 100vh;
    position: sticky;
    top: 0;
}

.sidebar-body {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.sidebar-section {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.sidebar-section-toggle {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 16px;
    background: transparent;
    border: none;
    color: var(--muted);
    font: inherit;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    cursor: pointer;
    flex-shrink: 0;
}

.sidebar-section-toggle:hover {
    color: var(--text);
    background: rgba(255, 255, 255, 0.04);
}

.sidebar-section-sign {
    font-size: 1.1rem;
    line-height: 1;
    font-weight: 400;
    width: 1.25rem;
    text-align: center;
    flex-shrink: 0;
}

.sidebar-section.collapsed .sidebar-section-panel {
    display: none;
}

.global-queries-section {
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.global-queries-section .sidebar-section-label {
    color: #fde047;
}

.global-queries-section .sidebar-section-toggle:hover .sidebar-section-label {
    color: #fef08a;
}

.favorite-queries-section {
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.favorite-queries-section .sidebar-section-label {
    color: #f9a8d4;
}

.favorite-queries-section .sidebar-section-toggle:hover .sidebar-section-label {
    color: #fbcfe8;
}

.global-queries-section.limited .global-query-item:nth-child(n+4) {
    display: none;
}

.global-queries-section:not(.limited) .global-query-list {
    max-height: 35vh;
    overflow-y: auto;
}

.favorite-queries-section.limited .favorite-query-item:nth-child(n+4) {
    display: none;
}

.favorite-queries-section:not(.limited) .favorite-query-list {
    max-height: 35vh;
    overflow-y: auto;
}

.favorite-query-list {
    padding: 4px 8px 8px;
}

.favorite-query-item {
    display: flex;
    align-items: center;
    gap: 4px;
    border-radius: 8px;
    margin-bottom: 4px;
}

.favorite-query-item:hover {
    background: rgba(59, 130, 246, 0.08);
}

.favorite-query-link {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    background: transparent;
    border: none;
    color: var(--text);
    cursor: pointer;
    text-align: left;
    font: inherit;
    font-size: 0.85rem;
}

.favorite-query-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-item-icon {
    display: inline-flex;
    flex-shrink: 0;
    color: var(--muted);
}

.sidebar-item-icon .icon {
    width: 1rem;
    height: 1rem;
}

.icon {
    width: 1.125rem;
    height: 1.125rem;
    display: block;
    flex-shrink: 0;
}

.btn-new-chat-icon {
    display: inline-flex;
    align-items: center;
    color: var(--muted);
}

.btn-new-chat-icon .icon {
    width: 1rem;
    height: 1rem;
}

.conversations-section {
    flex: 1;
    min-height: 0;
}

.conversations-section.collapsed {
    flex: 0 0 auto;
}

.conversations-section .conversation-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border);
}

.btn-new-chat {
    width: 100%;
    margin-top: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 14px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.btn-new-chat:hover {
    background: rgba(255, 255, 255, 0.04);
    border-color: rgba(59, 130, 246, 0.35);
    color: var(--text);
}

.conversation-list {
    padding: 4px 8px 8px;
}

.global-query-list {
    padding: 4px 8px 8px;
}

.global-query-item {
    display: flex;
    align-items: center;
    gap: 4px;
    border-radius: 8px;
    margin-bottom: 4px;
}

.global-query-item:hover {
    background: rgba(59, 130, 246, 0.08);
}

.global-query-link {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    background: transparent;
    border: none;
    color: var(--text);
    cursor: pointer;
    text-align: left;
    font: inherit;
    font-size: 0.85rem;
}

.global-query-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.globe-icon {
    flex-shrink: 0;
}

.global-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 0.8rem;
    border: 1px solid var(--border);
    color: var(--muted);
}

.global-tag-active {
    background: rgba(59, 130, 246, 0.12);
    color: #93c5fd;
    border-color: rgba(59, 130, 246, 0.3);
}

.conversation-item {
    display: flex;
    align-items: center;
    gap: 4px;
    border-radius: 8px;
    margin-bottom: 4px;
}

.conversation-item.active {
    background: rgba(59, 130, 246, 0.15);
}

.conversation-link {
    flex: 1;
    padding: 10px 12px;
    color: var(--text);
    text-decoration: none;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-menu {
    display: none;
    gap: 2px;
    padding-right: 4px;
}

.conversation-item:hover .conversation-menu {
    display: flex;
}

.btn-icon {
    background: transparent;
    border: none;
    color: var(--muted);
    cursor: pointer;
    padding: 4px 6px;
    border-radius: 4px;
    font-size: 0.85rem;
}

.btn-icon:hover {
    background: var(--surface-2);
    color: var(--text);
}

.sidebar-footer {
    margin-top: auto;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex-shrink: 0;
    background: var(--surface);
}

.sidebar-footer-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.sidebar-admin-link {
    display: block;
    padding: 8px 10px;
    border-radius: 8px;
    color: var(--muted);
    text-decoration: none;
    font-size: 0.85rem;
    transition: color 0.15s, background 0.15s;
}

.sidebar-admin-link:hover,
.sidebar-admin-link.is-active {
    color: var(--text);
    background: rgba(255, 255, 255, 0.04);
}

.users-shell .sidebar-compact {
    width: 240px;
}

.users-layout {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.users-main {
    flex: 1;
    padding: 24px;
}

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

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

.users-table th,
.users-table td {
    padding: 14px 16px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.users-table th {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    font-weight: 600;
}

.users-table tbody tr:last-child td {
    border-bottom: none;
}

.users-actions-col {
    width: 180px;
}

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

.users-self-tag {
    color: var(--muted);
    font-size: 0.85rem;
}

.btn-link {
    background: none;
    border: none;
    padding: 0;
    color: var(--muted);
    font: inherit;
    font-size: 0.85rem;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.btn-link:hover {
    color: var(--text);
}

.btn-link-danger {
    color: #fca5a5;
}

.btn-link-danger:hover {
    color: #fecaca;
}

.btn-danger {
    background: rgba(239, 68, 68, 0.85);
}

.btn-danger:hover:not(:disabled) {
    background: rgba(220, 38, 38, 0.95);
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 200;
}

.modal-overlay.hidden {
    display: none;
}

.modal-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
}

.modal-title {
    margin: 0 0 12px;
    font-size: 1.05rem;
}

.modal-message {
    margin: 0 0 16px;
    color: var(--muted);
    font-size: 0.9rem;
}

.modal-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.modal-label {
    font-size: 0.8rem;
    color: var(--muted);
}

.modal-form input[type="text"],
.modal-form input[type="password"] {
    width: 100%;
    box-sizing: border-box;
}

.modal-check {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    color: var(--text);
}

.modal-error {
    margin: 0;
    color: #fecaca;
    font-size: 0.85rem;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 8px;
}

.hidden {
    display: none !important;
}

.sidebar-empty {
    padding: 16px;
    font-size: 0.85rem;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.85rem;
}

.chat-title {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
}

.sidebar-toggle {
    display: none;
}

.message-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 12px 0;
}

.message-actions .btn-primary,
.message-actions .btn-ghost {
    margin-top: 0;
}

.chat-layout-old {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border);
    background: rgba(26, 35, 50, 0.85);
    backdrop-filter: blur(8px);
    gap: 12px;
}

.chat-header .chat-title {
    flex: 1;
}

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

.user-badge {
    color: var(--muted);
    font-size: 0.9rem;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: 960px;
    width: 100%;
    margin: 0 auto;
    padding: 24px;
}

.messages {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 24px;
    padding-bottom: 24px;
}

.welcome-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
}

.welcome-card h2 {
    margin-top: 0;
}

.welcome-card ul {
    color: var(--muted);
    padding-left: 20px;
}

.message-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.message {
    border-radius: var(--radius);
    padding: 16px;
}

.user-message {
    background: rgba(59, 130, 246, 0.12);
    border: 1px solid rgba(59, 130, 246, 0.25);
    align-self: flex-end;
    max-width: 85%;
}

.assistant-message {
    background: var(--surface);
    border: 1px solid var(--border);
    align-self: stretch;
    position: relative;
}

.assistant-message .message-content {
    padding-right: 104px;
}

.message-toolbar {
    position: absolute;
    top: 12px;
    right: 12px;
    display: flex;
    align-items: center;
    gap: 4px;
    z-index: 1;
}

.btn-msg-action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
}

.btn-msg-action:hover {
    color: var(--text);
    background: rgba(255, 255, 255, 0.06);
}

.btn-msg-action-static {
    cursor: default;
}

.btn-msg-action-static.is-active {
    color: var(--text);
    cursor: default;
}

.btn-msg-action-static.is-active:hover {
    background: transparent;
}

.btn-msg-action .icon {
    width: 1.2rem;
    height: 1.2rem;
}

.btn-msg-action.btn-refresh .icon {
    width: 1.35rem;
    height: 1.35rem;
}

.btn-download-minimal {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin: 8px 0 0;
    padding: 0;
    border: none;
    background: none;
    color: var(--muted);
    font-size: 0.85rem;
    text-decoration: none;
    cursor: pointer;
    transition: color 0.15s;
}

.btn-download-minimal:hover {
    color: var(--text);
}

.btn-download-icon {
    font-size: 0.95rem;
    line-height: 1;
}

.message-label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin-bottom: 8px;
}

.message-content p {
    margin-top: 0;
}

.chat-input-bar {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 12px;
    align-items: stretch;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.chat-input-bar textarea {
    resize: none;
    height: 56px;
    min-height: 56px;
    max-height: 56px;
    overflow-y: auto;
    box-sizing: border-box;
}

.chat-input-bar .btn-primary {
    margin-top: 0;
    height: 56px;
    align-self: stretch;
    padding: 0 20px;
    white-space: nowrap;
}

.alert {
    padding: 12px 14px;
    border-radius: 10px;
    font-size: 0.95rem;
}

.alert-error {
    background: rgba(239, 68, 68, 0.12);
    border: 1px solid rgba(239, 68, 68, 0.35);
    color: #fecaca;
}

.sql-block {
    margin-top: 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
}

.sql-block-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    background: var(--surface-2);
    color: var(--muted);
    font-size: 0.85rem;
}

.sql-block pre {
    background: #0b1220;
    margin: 0;
    padding: 14px;
    overflow-x: auto;
}

.btn-copy {
    padding: 6px 12px;
    font-size: 0.8rem;
}

.status-badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    margin-bottom: 12px;
}

.status-generating,
.status-queued,
.status-sent {
    background: rgba(59, 130, 246, 0.15);
    color: #93c5fd;
}

.status-completed {
    background: rgba(34, 197, 94, 0.15);
    color: #86efac;
}

.status-failed {
    background: rgba(239, 68, 68, 0.15);
    color: #fecaca;
}

.saved-query-heading {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin: 0 0 12px;
}

.saved-query-tag {
    display: inline-block;
    padding: 3px 8px;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    border: 1px solid var(--border);
    color: var(--muted);
    background: transparent;
}

.saved-query-tag-global {
    border-color: rgba(234, 179, 8, 0.45);
    color: #fde047;
}

.saved-query-tag-favorite {
    border-color: rgba(244, 114, 182, 0.35);
    color: #f9a8d4;
}

.saved-query-title {
    font-size: 1rem;
    font-weight: 400;
    color: var(--text);
}

.saved-query-owner {
    font-size: 0.9rem;
    color: var(--muted);
}

.summary-block {
    margin: 16px 0;
}

.summary-block h3 {
    margin: 0 0 8px;
    font-size: 1rem;
}

.btn-download {
    display: inline-block;
    margin: 12px 0;
    text-decoration: none;
}

.sql-debug {
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.sql-debug summary {
    cursor: pointer;
    color: var(--muted);
    margin-bottom: 8px;
}

.sql-debug pre {
    background: #0b1220;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px;
    overflow-x: auto;
    margin: 0;
}

.muted, .waiting-text {
    color: var(--muted);
    font-size: 0.9rem;
}

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -280px;
        top: 0;
        bottom: 0;
        z-index: 100;
        transition: left 0.2s ease;
    }

    .sidebar.open {
        left: 0;
    }

    .sidebar-toggle {
        display: inline-block;
    }
}

@media (max-width: 640px) {
    .chat-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }

    .user-message {
        max-width: 100%;
    }

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