@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Manrope:wght@400;500;600;700;800&display=swap');

:root {
    color-scheme: dark;
    --bg: #101522;
    --panel: #171e2e;
    --panel-light: #1d2639;
    --border: rgba(165, 180, 210, .12);
    --text: #edf0fa;
    --muted: #929db5;
    --accent: #9b8cff;
    --accent-light: #b6aaff;
    --user: #5146a8;
    --radius: 20px;
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    background: var(--bg);
}

body {
    min-height: 100vh;
    margin: 0;
    color: var(--text);
    font-family: 'DM Sans', sans-serif;
    background:
        radial-gradient(ellipse at 50% -20%, rgba(108, 91, 210, .22), transparent 55%),
        var(--bg);
}

button, textarea, input {
    font: inherit;
}

button {
    color: inherit;
}

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

.topbar {
    height: 72px;
    flex-shrink: 0;
    padding: 0 max(5%, calc((100% - 1120px) / 2));
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    background: rgba(16, 21, 34, .78);
    backdrop-filter: blur(18px);
}

.brand {
    display: flex;
    align-items: center;
    gap: 11px;
    color: var(--text);
    font-family: 'Manrope', sans-serif;
    font-size: 19px;
    font-weight: 800;
    text-decoration: none;
    letter-spacing: -.5px;
}

.brand-icon, .message-avatar {
    display: grid;
    place-items: center;
    flex-shrink: 0;
    color: white;
    font-family: 'Manrope', sans-serif;
    font-weight: 800;
    background: linear-gradient(145deg, #a18dff, #6454c8);
    box-shadow: 0 4px 18px rgba(125, 102, 235, .25);
}

.brand-icon {
    width: 35px;
    height: 35px;
    border-radius: 12px;
}

.brand-accent {
    color: var(--accent);
}

.topbar-caption {
    display: flex;
    align-items: center;
    gap: 9px;
    color: var(--muted);
    font-size: 13px;
}

.status-dot {
    width: 7px;
    height: 7px;
    display: inline-block;
    flex-shrink: 0;
    border-radius: 50%;
    background: #64d6a0;
    box-shadow: 0 0 10px rgba(100, 214, 160, .5);
}

.main-content {
    width: min(100% - 40px, 850px);
    margin: 0 auto;
    padding: 48px 0 22px;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.welcome {
    margin-bottom: 30px;
    text-align: center;
}

.welcome-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 7px 12px;
    border: 1px solid rgba(155, 140, 255, .2);
    border-radius: 30px;
    color: var(--accent-light);
    background: rgba(155, 140, 255, .07);
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 1.2px;
}

.welcome-sparkle {
    font-size: 14px;
}

.welcome h1 {
    margin: 19px 0 0;
    font-family: 'Manrope', sans-serif;
    font-size: clamp(29px, 5vw, 43px);
    line-height: 1.2;
    letter-spacing: -1.6px;
    font-weight: 800;
}

.welcome h1 span {
    color: var(--accent-light);
}

.chat-container {
    min-height: 480px;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: rgba(23, 30, 46, .88);
    box-shadow: 0 24px 80px rgba(0, 0, 0, .22);
}

.chat-heading {
    min-height: 75px;
    padding: 15px 22px;
    display: flex;
    align-items: center;
    gap: 13px;
    border-bottom: 1px solid var(--border);
}

.chat-heading-icon {
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
    border: 1px solid rgba(155, 140, 255, .2);
    border-radius: 13px;
    color: var(--accent-light);
    background: rgba(155, 140, 255, .1);
    font-size: 20px;
}

.chat-heading h2 {
    margin: 0 0 3px;
    font-family: 'Manrope', sans-serif;
    font-size: 14px;
}

.chat-heading p {
    margin: 0;
    color: var(--muted);
    font-size: 12px;
}

.chat-online {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 7px;
    color: var(--muted);
    font-size: 11px;
}

.messages {
    min-height: 300px;
    max-height: 58vh;
    flex: 1;
    overflow-y: auto;
    padding: 25px 24px;
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: #39435c transparent;
}

.message {
    width: fit-content;
    max-width: min(88%, 680px);
    margin: 0 0 22px;
    display: flex;
    align-items: flex-start;
    gap: 12px;
}

.message-avatar {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    font-size: 13px;
}

.message-content {
    min-width: 0;
    padding-top: 2px;
}

.message-author {
    margin-bottom: 8px;
    color: var(--accent-light);
    font-size: 12px;
    font-weight: 700;
}

.message-body {
    color: #dce2f1;
    font-size: 14px;
    line-height: 1.8;
    overflow-wrap: anywhere;
}

.user-message {
    margin-left: auto;
    padding: 12px 17px;
    border: 1px solid rgba(182, 170, 255, .14);
    border-radius: 17px 17px 4px 17px;
    color: #fff;
    background: linear-gradient(135deg, #554aab, #403a82);
    line-height: 1.65;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.assistant-message {
    width: 100%;
}

.message-body p {
    margin: 0 0 13px;
}

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

.message-body h1,
.message-body h2,
.message-body h3,
.message-body h4 {
    margin: 23px 0 10px;
    color: #f0edff;
    font-family: 'Manrope', sans-serif;
    line-height: 1.35;
}

.message-body h1 { font-size: 21px; }
.message-body h2 { font-size: 19px; }
.message-body h3 { font-size: 16px; }
.message-body h4 { font-size: 14px; }

.message-body ul,
.message-body ol {
    margin: 10px 0 17px;
    padding-left: 25px;
}

.message-body li {
    margin: 6px 0;
    padding-left: 4px;
}

.message-body li::marker {
    color: var(--accent);
}

.message-body strong {
    color: #fff;
    font-weight: 700;
}

.message-body em {
    color: #c9c1ff;
}

.message-body blockquote {
    margin: 14px 0;
    padding: 10px 15px;
    border-left: 3px solid var(--accent);
    border-radius: 0 9px 9px 0;
    color: #b9c3d8;
    background: rgba(155, 140, 255, .07);
}

.message-body code {
    padding: 2px 6px;
    border: 1px solid rgba(155, 140, 255, .13);
    border-radius: 5px;
    color: #d4caff;
    background: rgba(155, 140, 255, .1);
    font-family: 'Consolas', monospace;
    font-size: .9em;
}

.message-body pre {
    max-width: 100%;
    margin: 14px 0;
    padding: 16px;
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: #0c111d;
    scrollbar-width: thin;
}

.message-body pre code {
    padding: 0;
    border: 0;
    color: #d9dff0;
    background: transparent;
    font-size: 13px;
    line-height: 1.7;
}

.message-body a {
    color: var(--accent-light);
    text-decoration: underline;
    text-underline-offset: 3px;
}

.message-body hr {
    border: 0;
    border-top: 1px solid var(--border);
    margin: 20px 0;
}

.sources {
    margin-top: 19px;
    padding: 13px 15px;
    border: 1px solid rgba(155, 140, 255, .14);
    border-radius: 12px;
    background: rgba(155, 140, 255, .045);
}

.sources-title {
    margin-bottom: 9px;
    display: flex;
    align-items: center;
    gap: 7px;
    color: #c7bcff;
    font-size: 12px;
    font-weight: 700;
}

.sources ul {
    margin: 0;
    padding-left: 20px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.7;
}

.sources li {
    margin: 3px 0;
    overflow-wrap: anywhere;
}

.sources li::marker {
    color: var(--accent);
}

.chat-footer {
    position: relative;
    padding: 16px 22px 13px;
    border-top: 1px solid var(--border);
    background: rgba(19, 25, 39, .75);
}

.input-box {
    min-height: 55px;
    padding: 7px 8px 7px 11px;
    display: flex;
    align-items: flex-end;
    gap: 9px;
    border: 1px solid rgba(165, 180, 210, .2);
    border-radius: 16px;
    background: #20283a;
    transition: border-color .2s, box-shadow .2s;
}

.input-box:focus-within {
    border-color: rgba(155, 140, 255, .65);
    box-shadow: 0 0 0 3px rgba(155, 140, 255, .08);
}

.icon-button {
    width: 37px;
    height: 37px;
    flex-shrink: 0;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 11px;
    color: #c5cce0;
    background: transparent;
    cursor: pointer;
}

.upload-toggle {
    font-size: 25px;
    color: var(--accent-light);
    transition: background .2s, transform .2s;
}

.upload-toggle:hover {
    background: rgba(155, 140, 255, .13);
}

.upload-toggle.active {
    transform: rotate(45deg);
}

.input-box textarea {
    min-width: 0;
    min-height: 38px;
    max-height: 150px;
    flex: 1;
    padding: 9px 3px;
    resize: none;
    outline: none;
    border: 0;
    color: var(--text);
    background: transparent;
    font-size: 14px;
    line-height: 1.5;
}

.input-box textarea::placeholder {
    color: #77839c;
}

.send-button {
    width: 39px;
    height: 39px;
    flex-shrink: 0;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 12px;
    color: white;
    background: linear-gradient(145deg, #a18dff, #6858ce);
    box-shadow: 0 4px 13px rgba(125, 102, 235, .22);
    cursor: pointer;
    transition: transform .15s, opacity .15s;
}

.send-button span {
    font-size: 23px;
    line-height: 1;
}

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

.send-button:disabled,
.upload-button:disabled {
    opacity: .5;
    cursor: not-allowed;
}

.input-hint {
    margin: 9px 0 0;
    color: #748099;
    text-align: center;
    font-size: 10px;
}

.upload-panel {
    margin-bottom: 13px;
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: #20283a;
}

.upload-panel[hidden] {
    display: none;
}

.upload-panel-heading {
    margin-bottom: 13px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
}

.upload-panel-heading strong {
    font-size: 13px;
}

.upload-panel-heading p {
    margin: 5px 0 0;
    color: var(--muted);
    font-size: 11px;
}

.close-upload {
    font-size: 22px;
}

#upload-form {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 9px;
}

#document-file {
    display: none;
}

.file-picker {
    min-width: 0;
    min-height: 40px;
    flex: 1;
    padding: 8px 12px;
    display: flex;
    align-items: center;
    gap: 9px;
    overflow: hidden;
    border: 1px dashed rgba(165, 180, 210, .25);
    border-radius: 9px;
    color: #bdc6da;
    font-size: 12px;
    cursor: pointer;
}

.file-picker span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-picker-icon {
    color: var(--accent-light);
    font-size: 18px;
}

.upload-button {
    min-height: 40px;
    padding: 0 16px;
    border: 0;
    border-radius: 9px;
    color: white;
    background: #6558b8;
    font-size: 12px;
    cursor: pointer;
}

#upload-status {
    margin-top: 9px;
    font-size: 12px;
    line-height: 1.5;
}

#upload-status:empty {
    display: none;
}

#upload-status.success {
    color: #76d9a8;
}

#upload-status.error {
    color: #ff929c;
}

.page-footer {
    padding: 18px 0 0;
    color: #68748d;
    text-align: center;
    font-size: 11px;
}

.page-footer span {
    padding: 0 5px;
    color: var(--accent);
}

@media (min-width: 900px) {
    .messages {
        padding: 30px 32px;
    }

    .chat-container {
        min-height: 540px;
    }
}

@media (max-width: 600px) {
    .topbar {
        height: 60px;
        padding: 0 17px;
    }

    .brand {
        font-size: 17px;
    }

    .brand-icon {
        width: 32px;
        height: 32px;
    }

    .topbar-caption {
        font-size: 11px;
    }

    .main-content {
        width: 100%;
        min-height: calc(100dvh - 60px);
        padding: 25px 12px 10px;
    }

    .welcome {
        margin-bottom: 20px;
    }

    .welcome-badge {
        font-size: 9px;
        letter-spacing: .8px;
    }

    .welcome h1 {
        margin-top: 14px;
        font-size: clamp(26px, 7vw, 34px);
        letter-spacing: -1px;
    }

    .chat-container {
        min-height: 0;
        flex: 1;
        border-radius: 17px;
    }

    .chat-heading {
        min-height: 65px;
        padding: 11px 14px;
        gap: 10px;
    }

    .chat-heading-icon {
        width: 36px;
        height: 36px;
    }

    .chat-heading p {
        font-size: 10px;
    }

    .chat-online {
        font-size: 10px;
    }

    .messages {
        min-height: 250px;
        max-height: none;
        padding: 19px 13px;
    }

    .message {
        max-width: 94%;
        gap: 9px;
        margin-bottom: 18px;
    }

    .message-body {
        font-size: 13px;
        line-height: 1.75;
    }

    .message-avatar {
        width: 27px;
        height: 27px;
        border-radius: 9px;
    }

    .message-body h1 { font-size: 19px; }
    .message-body h2 { font-size: 17px; }
    .message-body h3 { font-size: 15px; }

    .message-body pre {
        padding: 12px;
    }

    .chat-footer {
        padding: 12px 11px 10px;
    }

    .input-box {
        min-height: 51px;
        gap: 5px;
    }

    .input-box textarea {
        font-size: 16px;
    }

    .input-hint {
        font-size: 9px;
    }

    #upload-form {
        align-items: stretch;
    }

    .file-picker {
        flex-basis: 100%;
    }

    .upload-button {
        width: 100%;
    }

    .page-footer {
        padding-top: 12px;
    }
}

@media (max-width: 360px) {
    .topbar-caption {
        font-size: 10px;
    }

    .chat-online {
        font-size: 0;
    }

    .chat-online .status-dot {
        width: 8px;
        height: 8px;
    }
}

/* Скрываем приветствие после первого сообщения */
.welcome.is-hidden {
    display: none;
}

/* Не растягиваем карточку чата на всю свободную высоту */
.chat-container {
    flex: 0 0 auto;
}

/* Область сообщений сохраняет комфортную высоту,
   но не заполняет всё свободное пространство */
.messages {
    flex: 0 1 auto;
    min-height: 300px;
    max-height: 58vh;
}

/* Карточка чата подстраивается под содержимое */
.main-content {
    flex: 0 0 auto;
}

.chat-container {
    flex: 0 0 auto;
    min-height: 0;
}

.messages {
    flex: 0 1 auto;
    min-height: 300px;
    max-height: 58vh;
}

/* На телефонах тоже не растягиваем карточку */
@media (max-width: 600px) {
    .chat-container {
        flex: 0 0 auto;
        min-height: 0;
    }

    .messages {
        flex: 0 1 auto;
        min-height: 250px;
        max-height: 55vh;
    }
}
