:root {
    color-scheme: light dark;

    --bg: #f7f8fc;
    --bg-elevated: rgba(255, 255, 255, 0.72);
    --card: rgba(255, 255, 255, 0.82);
    --text: #172033;
    --muted: #667089;
    --border: rgba(23, 32, 51, 0.10);
    --primary: #5b7cff;
    --primary-soft: rgba(91, 124, 255, 0.10);
    --accent: #8b6dff;
    --danger: #d94f70;
    --shadow: 0 10px 30px rgba(27, 39, 78, 0.08);
    --maxw: 1680px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b1020;
        --bg-elevated: rgba(255, 255, 255, 0.06);
        --card: rgba(255, 255, 255, 0.05);
        --text: #e8eefc;
        --muted: #a8b2ca;
        --border: rgba(255, 255, 255, 0.10);
        --primary: #8aa2ff;
        --primary-soft: rgba(138, 162, 255, 0.14);
        --accent: #b29cff;
        --danger: #ff7b9b;
        --shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
    }
}

* {
    box-sizing: border-box;
}

html {
    width: 100%;
    min-height: 100%;
    scroll-behavior: smooth;
}

body {
    width: 100%;
    margin: 0;
    min-height: 100vh;
    background:
        radial-gradient(circle at top left, var(--primary-soft), transparent 34rem),
        radial-gradient(circle at bottom right, rgba(139, 109, 255, 0.12), transparent 30rem),
        var(--bg);
    color: var(--text);
    font-family: "Segoe UI", Arial, sans-serif;
    line-height: 1.6;
    overflow-x: hidden;
}

a {
    color: inherit;
}

.nav-wrap {
    position: sticky;
    top: 14px;
    z-index: 100;
    padding: 14px 18px 0;
}

nav {
    max-width: var(--maxw);
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 12px 16px;
    border-radius: 999px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    backdrop-filter: blur(12px);
    box-shadow: var(--shadow);
}

.brand {
    font-weight: 700;
    letter-spacing: 0.04em;
    text-decoration: none;
    white-space: nowrap;
}

.nav-links {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.nav-link {
    display: inline-block;
    padding: 9px 14px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    text-decoration: none;
    font-size: 0.95rem;
    color: var(--text);
    transition: 0.2s ease;
}

.nav-link:hover {
    transform: translateY(-1px);
    background: var(--primary-soft);
    border-color: rgba(91, 124, 255, 0.22);
    color: var(--primary);
}

.container {
    width: min(var(--maxw), calc(100vw - 32px));
    max-width: none;
    margin: 0 auto;
    padding: 18px 0 42px;
}

.workspace {
    height: calc(100vh - 122px);
    min-height: 640px;
    display: grid;
    grid-template-columns: 300px minmax(0, 1fr);
    gap: 22px;
}

.sidebar,
.chat-shell {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 28px;
    box-shadow: var(--shadow);
    backdrop-filter: blur(12px);
    overflow: hidden;
}

.sidebar {
    display: flex;
    flex-direction: column;
    padding: 16px;
    min-width: 0;
}

.sidebar-head {
    padding: 6px 6px 14px;
}

.eyebrow {
    display: inline-flex;
    width: fit-content;
    margin-bottom: 12px;
    padding: 7px 12px;
    border-radius: 999px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--muted);
    font-size: 0.86rem;
}

.sidebar h1 {
    margin: 0;
    font-size: 1.7rem;
    line-height: 1.08;
    letter-spacing: -0.04em;
}

.sidebar p {
    margin: 10px 0 0;
    color: var(--muted);
    font-size: 0.94rem;
}

.sidebar-actions {
    display: grid;
    gap: 10px;
    margin: 12px 0 14px;
}

button {
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 11px 14px;
    background: var(--bg-elevated);
    color: var(--text);
    font: inherit;
    font-weight: 650;
    cursor: pointer;
    transition: 0.2s ease;
}

button:hover {
    transform: translateY(-1px);
    background: var(--primary-soft);
    color: var(--primary);
}

button:disabled {
    opacity: 0.55;
    cursor: not-allowed;
    transform: none;
}

.primary-btn {
    border: 0;
    background: var(--primary);
    color: white;
}

.primary-btn:hover {
    color: white;
    filter: brightness(1.03);
}

.danger-btn:hover {
    color: var(--danger);
    background: rgba(217, 79, 112, 0.10);
}

.sessions {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-right: 2px;
}

.session-item {
    width: 100%;
    text-align: left;
    display: block;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 16px;
    padding: 11px 12px;
    color: var(--text);
    cursor: pointer;
}

.session-item:hover {
    background: var(--primary-soft);
    border-color: var(--border);
}

.session-item.active {
    background: var(--primary-soft);
    border-color: rgba(91, 124, 255, 0.25);
    color: var(--primary);
}

.session-title {
    display: block;
    font-weight: 700;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.session-meta {
    display: block;
    margin-top: 3px;
    font-size: 0.82rem;
    color: var(--muted);
}

.sidebar-foot {
    padding: 12px 6px 0;
    color: var(--muted);
    font-size: 0.84rem;
    border-top: 1px solid var(--border);
    margin-top: 12px;
}

.chat-shell {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 16px 18px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elevated);
}

.window-dots {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.window-dots span {
    width: 10px;
    height: 10px;
    border-radius: 999px;
    background: var(--muted);
    opacity: 0.42;
}

.chat-title {
    font-weight: 700;
    color: var(--text);
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.top-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.small-btn {
    padding: 8px 11px;
    border-radius: 999px;
    color: var(--muted);
    font-size: 0.88rem;
}

#chat {
    flex: 1;
    width: 100%;
    overflow-y: auto;
    padding: 22px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.msg {
    padding: 14px 16px;
    border-radius: 20px;
    word-break: break-word;
    overflow-wrap: anywhere;
    font-size: 0.98rem;
    line-height: 1.65;
}

.msg.user {
    align-self: flex-end;
    max-width: min(760px, 72%);
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 7px;
    box-shadow: var(--shadow);
    white-space: pre-wrap;
}

.msg.bot {
    align-self: flex-start;
    max-width: min(1120px, 92%);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-bottom-left-radius: 7px;
    white-space: normal;
}

.msg.system {
    align-self: center;
    max-width: 92%;
    background: transparent;
    color: var(--muted);
    border: 1px dashed var(--border);
    font-size: 0.92rem;
    text-align: center;
    white-space: pre-wrap;
}

.msg.markdown-body p {
    margin: 0 0 0.85em;
}

.msg.markdown-body p:last-child {
    margin-bottom: 0;
}

.msg.markdown-body h1,
.msg.markdown-body h2,
.msg.markdown-body h3,
.msg.markdown-body h4 {
    margin: 0.9em 0 0.45em;
    line-height: 1.25;
}

.msg.markdown-body h1:first-child,
.msg.markdown-body h2:first-child,
.msg.markdown-body h3:first-child,
.msg.markdown-body h4:first-child {
    margin-top: 0;
}

.msg.markdown-body ul,
.msg.markdown-body ol {
    margin: 0.6em 0 0.9em 1.4em;
    padding-left: 1em;
}

.msg.markdown-body li + li {
    margin-top: 0.25em;
}

.msg.markdown-body code {
    padding: 0.12em 0.35em;
    border-radius: 6px;
    background: rgba(127, 127, 127, 0.16);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.92em;
}

.msg.markdown-body pre {
    max-width: 100%;
    overflow-x: auto;
    padding: 12px 14px;
    border-radius: 12px;
    background: rgba(127, 127, 127, 0.14);
    white-space: pre;
}

.msg.markdown-body pre code {
    padding: 0;
    background: transparent;
}

.msg.markdown-body blockquote {
    margin: 0.8em 0;
    padding-left: 1em;
    border-left: 3px solid rgba(127, 127, 127, 0.35);
    color: inherit;
    opacity: 0.9;
}

.msg.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.8em 0;
    display: block;
    overflow-x: auto;
}

.msg.markdown-body th,
.msg.markdown-body td {
    border: 1px solid rgba(127, 127, 127, 0.25);
    padding: 8px 10px;
    text-align: left;
}

.msg.markdown-body a {
    color: var(--primary);
    text-decoration: underline;
    text-underline-offset: 3px;
}

.msg.markdown-body hr {
    border: 0;
    border-top: 1px solid var(--border);
    margin: 1em 0;
}

.composer {
    display: flex;
    gap: 12px;
    padding: 18px;
    border-top: 1px solid var(--border);
    background: var(--bg-elevated);
}

textarea {
    flex: 1;
    resize: none;
    min-height: 48px;
    max-height: 160px;
    padding: 13px 14px;
    border-radius: 18px;
    border: 1px solid var(--border);
    outline: none;
    background: var(--card);
    color: var(--text);
    font: inherit;
}

textarea::placeholder {
    color: var(--muted);
}

.send-btn {
    border: 0;
    background: var(--primary);
    color: white;
    padding: 0 20px;
}

.image-button {
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 8px 12px;
    background: transparent;
    color: inherit;
    cursor: pointer;
    flex-shrink: 0;
}

.image-button:hover {
    background: var(--primary-soft);
    color: var(--primary);
}

.image-button.has-image {
    max-width: 210px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    border-color: rgba(91, 124, 255, 0.28);
    background: var(--primary-soft);
    color: var(--primary);
}

.token-modal {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 18px;
    background: rgba(5, 8, 18, 0.56);
    backdrop-filter: blur(10px);
}

.token-modal.visible {
    display: flex;
}

.modal-card {
    width: min(440px, 100%);
    padding: 24px;
    border-radius: 26px;
    background: var(--card);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.modal-card h2 {
    margin: 0 0 8px;
    font-size: 1.45rem;
}

.modal-card p {
    margin: 0 0 16px;
    color: var(--muted);
}

.modal-card input {
    width: 100%;
    padding: 12px 13px;
    border-radius: 15px;
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    color: var(--text);
    font: inherit;
    outline: none;
    margin-bottom: 12px;
}

@media (max-width: 1180px) {
    .workspace {
        grid-template-columns: 280px minmax(0, 1fr);
    }

    .msg.user {
        max-width: min(700px, 78%);
    }

    .msg.bot {
        max-width: 94%;
    }
}

@media (max-width: 860px) {
    nav {
        border-radius: 24px;
        align-items: flex-start;
        flex-direction: column;
    }

    .nav-links {
        justify-content: flex-start;
    }

    .workspace {
        height: auto;
        min-height: 0;
        grid-template-columns: 1fr;
    }

    .sidebar {
        min-height: 320px;
    }

    .chat-shell {
        min-height: 660px;
    }
}

@media (max-width: 640px) {
    .container {
        width: calc(100vw - 20px);
    }

    .nav-wrap {
        padding: 10px 10px 0;
    }

    .sidebar,
    .chat-shell {
        border-radius: 24px;
    }

    #chat {
        padding: 14px;
    }

    .msg.user,
    .msg.bot,
    .msg.system {
        max-width: 96%;
    }

    .composer {
        padding: 12px;
        gap: 8px;
    }

    .send-btn {
        padding: 0 16px;
    }

    .image-button {
        max-width: 92px;
        padding: 8px 10px;
    }

    .image-button.has-image {
        max-width: 118px;
    }
}