:root {
    --bg: #081120;
    --bg-2: #0d1728;
    --panel: rgba(15, 23, 42, 0.78);
    --panel-strong: rgba(17, 24, 39, 0.92);
    --panel-soft: rgba(255, 255, 255, 0.04);
    --border: rgba(255, 255, 255, 0.08);
    --text: #f8fafc;
    --muted: #94a3b8;
    --accent: #38bdf8;
    --accent-2: #6366f1;
    --danger: #ef4444;
    --danger-dark: #dc2626;
    --success: #22c55e;
    --shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
    --radius: 20px;
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    font-family: Inter, "Segoe UI", Arial, sans-serif;
    color: var(--text);
    background:
        radial-gradient(circle at top left, rgba(56, 189, 248, 0.12), transparent 28%),
        radial-gradient(circle at bottom right, rgba(99, 102, 241, 0.12), transparent 26%),
        linear-gradient(180deg, var(--bg), var(--bg-2));
}

body.theme-app {
    height: 100vh;
    overflow: hidden;
}

body.theme-auth {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

a {
    color: inherit;
}

.panel-card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    backdrop-filter: blur(12px);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 46px;
    padding: 12px 14px;
    border: none;
    border-radius: 14px;
    color: #fff;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease, opacity 0.2s ease, border-color 0.2s ease, background 0.2s ease;
    text-decoration: none;
}

.btn:hover {
    transform: translateY(-2px);
}

.btn:disabled {
    opacity: 0.7;
    cursor: not-allowed;
    transform: none;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent), #2563eb);
    box-shadow: 0 12px 28px rgba(37, 99, 235, 0.25);
    margin-bottom: 10px;
}

.btn-danger {
    background: linear-gradient(135deg, var(--danger), var(--danger-dark));
    box-shadow: 0 12px 28px rgba(220, 38, 38, 0.22);
    margin-bottom: 10px;
}

.btn-ghost {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: none;
}

.field-label {
    display: block;
    margin-bottom: 6px;
    font-weight: 700;
    color: #dbeafe;
}

.form-control {
    width: 100%;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 14px;
    padding: 12px;
    font: inherit;
    font-size: 14px;
    background: rgba(255, 255, 255, 0.03);
    color: var(--text);
}

.form-control:focus {
    outline: none;
    border-color: rgba(56, 189, 248, 0.5);
}

select.form-control {
    background: rgba(15, 23, 42, 0.95);
}

select.form-control option {
    background: #0f172a;
    color: #f8fafc;
}

.alert {
    padding: 12px 14px;
    border-radius: 14px;
    overflow-wrap: anywhere;
    border: 1px solid transparent;
}

.alert-error {
    color: #fecaca;
    background: rgba(185, 28, 28, 0.18);
    border-color: rgba(185, 28, 28, 0.3);
}

.alert-success {
    color: #dcfce7;
    background: rgba(22, 101, 52, 0.22);
    border-color: rgba(34, 197, 94, 0.28);
}

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

.empty {
    color: var(--muted);
    overflow-wrap: anywhere;
}

.layout {
    height: 100vh;
    display: flex;
    align-items: stretch;
    position: relative;
    overflow: hidden;
}

.sidebar {
    width: 320px;
    height: 100vh;
    background: rgba(8, 17, 32, 0.72);
    backdrop-filter: blur(16px);
    border-right: 1px solid rgba(255, 255, 255, 0.06);
    padding: 22px;
    overflow-y: auto;
    overflow-x: hidden;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 18px;
}

.sidebar-logo {
    width: 42px;
    height: 42px;
    border-radius: 14px;
    object-fit: cover;
    box-shadow: 0 10px 24px rgba(56, 189, 248, 0.18);
}

.sidebar-title-wrap h1,
.sidebar-title-wrap h2 {
    margin: 0;
    font-size: 22px;
    letter-spacing: -0.02em;
}

.sidebar-subtitle {
    margin-top: 2px;
    font-size: 12px;
    color: var(--muted);
}

.sidebar-section {
    margin-bottom: 18px;
}

.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 20px;
    min-width: 0;
    min-height: 0;
    gap: 18px;
    overflow: hidden;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 16px 18px;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}

.topbar-avatar {
    width: 42px;
    height: 42px;
    border-radius: 14px;
    object-fit: cover;
    box-shadow: 0 10px 24px rgba(56, 189, 248, 0.18);
}

.topbar-title {
    margin: 0;
    font-size: 18px;
    letter-spacing: -0.02em;
}

.topbar-user {
    min-width: 0;
    word-break: break-word;
    color: var(--muted);
    font-size: 14px;
}

.topbar-user strong {
    color: var(--text);
}

.topbar-link {
    text-decoration: none;
    color: #fca5a5;
    font-weight: 700;
    white-space: nowrap;
    transition: color 0.2s ease, transform 0.2s ease;
}

.topbar-link:hover {
    color: #fecaca;
    transform: translateY(-1px);
}

.conversation-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.conversation-link {
    display: block;
    padding: 14px;
    text-decoration: none;
    color: var(--text);
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 14px;
    overflow: hidden;
    transition: transform 0.2s ease, border-color 0.2s ease, background 0.2s ease;
}

.conversation-link:hover {
    transform: translateY(-2px);
    border-color: rgba(56, 189, 248, 0.22);
    background: rgba(56, 189, 248, 0.06);
}

.conversation-link.active {
    background: rgba(56, 189, 248, 0.12);
    border-color: rgba(56, 189, 248, 0.38);
}

.conversation-title {
    font-weight: 700;
    overflow-wrap: anywhere;
}

.small {
    margin-top: 6px;
    font-size: 12px;
    color: var(--muted);
    overflow-wrap: anywhere;
}

.chat-stack {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: 18px;
    min-height: 0;
    flex: 1;
}

.messages {
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 18px;
}

.message {
    margin-bottom: 16px;
    padding: 16px;
    border-radius: 18px;
    max-width: 920px;
    overflow-wrap: anywhere;
    word-break: break-word;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.message-mode {
    margin-top: 8px;
    font-size: 12px;
    color: var(--muted);
    display: flex;
    align-items: center;
    gap: 6px;
}

.message-mode .icon {
    font-size: 14px;
}


.message:last-child {
    margin-bottom: 0;
}

.message.user {
    background: rgba(56, 189, 248, 0.12);
    border-color: rgba(56, 189, 248, 0.2);
    margin-left: auto;
}

.message.assistant {
    background: rgba(255, 255, 255, 0.04);
}

.message-content {
    line-height: 1.7;
}

.message.user .message-content {
    white-space: pre-wrap;
}

.message-header {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    margin-bottom: 10px;
    color: #dbeafe;
}

.message-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 30px;
    height: 30px;
    padding: 0 10px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.06);
    font-size: 12px;
    color: #e2e8f0;
}

.message.assistant .message-header {
    color: #cbd5e1;
}

.message-meta {
    margin-top: 12px;
    font-size: 12px;
    color: var(--muted);
    overflow-wrap: anywhere;
}

.message-sources {
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.message-sources-title {
    font-size: 12px;
    font-weight: 700;
    color: var(--muted);
    margin-bottom: 8px;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.message-sources ul {
    margin: 0;
    padding-left: 18px;
}

.message-sources li + li {
    margin-top: 6px;
}

.message-sources a {
    color: #7dd3fc;
    text-decoration: underline;
    overflow-wrap: anywhere;
    word-break: break-word;
}

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

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

.markdown-content p,
.markdown-content ul,
.markdown-content ol,
.markdown-content pre,
.markdown-content blockquote,
.markdown-content table {
    margin: 0 0 12px 0;
}

.markdown-content ul,
.markdown-content ol {
    padding-left: 22px;
}

.markdown-content li + li {
    margin-top: 4px;
}

.markdown-content code {
    font-family: Consolas, Monaco, monospace;
    font-size: 0.95em;
    background: rgba(255, 255, 255, 0.08);
    padding: 2px 6px;
    border-radius: 6px;
}

.markdown-content pre {
    background: #020617;
    color: #f8fafc;
    padding: 14px;
    border-radius: 12px;
    overflow-x: auto;
    white-space: pre;
    border: 1px solid rgba(255, 255, 255, 0.06);
}

.markdown-content pre code {
    background: transparent;
    color: inherit;
    padding: 0;
}

.markdown-content blockquote {
    border-left: 4px solid #60a5fa;
    margin-left: 0;
    padding: 6px 0 6px 12px;
    color: #cbd5e1;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 0 10px 10px 0;
}

.markdown-content a {
    color: #7dd3fc;
    text-decoration: underline;
    overflow-wrap: anywhere;
}

.markdown-content hr {
    border: none;
    border-top: 1px solid rgba(255, 255, 255, 0.12);
    margin: 16px 0;
}

.markdown-content table {
    width: 100%;
    border-collapse: collapse;
    display: block;
    overflow-x: auto;
}

.markdown-content th,
.markdown-content td {
    border: 1px solid rgba(255, 255, 255, 0.12);
    padding: 8px 10px;
    text-align: left;
    vertical-align: top;
}

.markdown-content th {
    background: rgba(255, 255, 255, 0.06);
}

.markdown-content img {
    max-width: 100%;
    height: auto;
    border-radius: 10px;
}

.typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 18px;
}

.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #94a3b8;
    animation: blink 1.2s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.35;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

.composer {
    padding: 16px;
}

.composer form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.composer-grid {
    display: grid;
    grid-template-columns: 220px 1fr;
    gap: 12px;
    align-items: start;
}

.composer-actions {
    display: flex;
    justify-content: flex-end;
}

.composer-help {
    font-size: 12px;
    color: var(--muted);
    margin-top: -2px;
}

.auth-card {
    width: 100%;
    max-width: 440px;
    padding: 32px;
}

.auth-brand {
    display: flex;
    align-items: center;
    gap: 14px;
    justify-content: center;
    margin-bottom: 16px;
}

.auth-logo {
    width: 56px;
    height: 56px;
    border-radius: 18px;
    object-fit: cover;
    box-shadow: 0 14px 28px rgba(56, 189, 248, 0.18);
}

.auth-title {
    margin: 0;
    font-size: 26px;
    text-align: center;
}

.auth-subtitle {
    text-align: center;
    color: var(--muted);
    margin-bottom: 24px;
    font-size: 14px;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.auth-form .field-label {
    margin-bottom: 0;
}

@media (max-width: 900px) {
    html, body {
        height: auto;
    }

    body.theme-app {
        min-height: 100vh;
        height: auto;
        overflow: auto;
    }

    .layout {
        height: auto;
        min-height: 100vh;
        flex-direction: column;
        overflow: visible;
    }

    .sidebar {
        width: 100%;
        height: auto;
        border-right: none;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06);
        max-height: none;
        overflow: visible;
    }

    .main {
        overflow: visible;
        min-height: auto;
    }

    .chat-stack {
        display: block;
    }

    .messages {
        min-height: 260px;
        max-height: none;
        overflow: visible;
        margin-bottom: 18px;
    }

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

@media (max-width: 640px) {
    .sidebar,
    .main {
        padding: 12px;
    }

    .topbar {
        flex-direction: column;
        align-items: flex-start;
        padding: 14px;
    }

    .topbar-link {
        align-self: flex-start;
    }

    .messages,
    .composer,
    .auth-card {
        padding: 12px;
        border-radius: 16px;
    }

    .message {
        padding: 12px;
        border-radius: 14px;
    }

    .conversation-link {
        padding: 12px;
    }

    .sidebar-title-wrap h1,
    .sidebar-title-wrap h2 {
        font-size: 20px;
    }

    .composer textarea.form-control {
        min-height: 110px;
    }

    .auth-title {
        font-size: 22px;
    }
}