* {
    box-sizing: border-box;
}

:root {
    --bg: #09090b;
    --panel: #111113;
    --panel-2: #18181b;
    --border: #27272a;
    --text: #f4f4f5;
    --muted: #a1a1aa;
    --muted2: #71717a;
    --accent: #ffffff;
    --accent-text: #09090b;
    --danger: #f87171;
}

html,
body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    font-family:
        Inter,
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        sans-serif;
    background: var(--bg);
    color: var(--text);
}

button,
input,
textarea {
    font: inherit;
}

button {
    cursor: pointer;
}

/* AUTH */

.auth-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background:
        radial-gradient(circle at 50% 0%, #27272a 0%, transparent 45%),
        var(--bg);
}

.auth-container {
    width: 100%;
    max-width: 420px;
}

.auth-card {
    background: rgba(17, 17, 19, .94);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 36px;
    box-shadow: 0 30px 80px rgba(0,0,0,.45);
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 42px;
}

.logo-mark {
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 13px;
    background: #fff;
    color: #000;
    font-size: 24px;
    font-weight: 700;
}

.logo-title {
    font-size: 18px;
    font-weight: 700;
}

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

.auth-heading h1 {
    font-size: 28px;
    margin: 0 0 8px;
    letter-spacing: -.7px;
}

.auth-heading p {
    color: var(--muted);
    margin: 0 0 28px;
    line-height: 1.5;
}

.auth-card label {
    display: block;
    font-size: 13px;
    color: #d4d4d8;
    margin: 18px 0 8px;
}

.auth-card input {
    width: 100%;
    padding: 14px 15px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: #09090b;
    color: var(--text);
    outline: none;
    transition: .2s;
}

.auth-card input:focus {
    border-color: #52525b;
    box-shadow: 0 0 0 3px rgba(255,255,255,.04);
}

.primary-button {
    width: 100%;
    border: 0;
    border-radius: 12px;
    background: #fff;
    color: #000;
    padding: 14px;
    margin-top: 24px;
    font-weight: 650;
    transition: .2s;
}

.primary-button:hover {
    transform: translateY(-1px);
    opacity: .92;
}

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

.auth-switch a {
    color: #fff;
    text-decoration: none;
    font-weight: 600;
}

.error-message {
    background: rgba(248,113,113,.08);
    border: 1px solid rgba(248,113,113,.2);
    color: #fca5a5;
    padding: 12px 14px;
    border-radius: 10px;
    font-size: 14px;
}

/* APP */

.app {
    width: 100%;
    height: 100vh;
    display: flex;
    overflow: hidden;
}

.sidebar {
    width: 280px;
    min-width: 280px;
    height: 100%;
    background: var(--panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.sidebar-top {
    padding: 18px;
}

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

.logo-mark.small {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    font-size: 19px;
}

.brand-title {
    font-weight: 700;
    font-size: 15px;
}

.brand-model {
    font-size: 11px;
    color: var(--muted);
    margin-top: 2px;
}

.new-chat-button {
    width: 100%;
    border: 1px solid var(--border);
    background: #18181b;
    color: var(--text);
    border-radius: 11px;
    padding: 11px 13px;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 9px;
    transition: .2s;
}

.new-chat-button:hover {
    background: #27272a;
}

.new-chat-button span {
    font-size: 20px;
    line-height: 1;
}

.chats-section {
    flex: 1;
    overflow-y: auto;
    padding: 0 10px;
}

.section-title {
    color: var(--muted2);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .08em;
    padding: 8px 9px;
}

.chat-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.chat-item {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 9px;
    background: transparent;
    border: 0;
    color: #d4d4d8;
    padding: 10px 9px;
    border-radius: 9px;
    text-align: left;
}

.chat-item:hover {
    background: #18181b;
}

.chat-item.active {
    background: #27272a;
    color: white;
}

.chat-icon {
    color: var(--muted2);
}

.chat-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 13px;
}

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

.user-card {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px;
}

.avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: #27272a;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 700;
}

.user-info {
    flex: 1;
    min-width: 0;
}

.username {
    font-size: 13px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-status {
    color: var(--muted2);
    font-size: 11px;
    margin-top: 2px;
}

.logout-button {
    color: var(--muted);
    text-decoration: none;
    font-size: 20px;
    padding: 5px;
}

.logout-button:hover {
    color: white;
}

/* MAIN */

.main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
}

.topbar {
    height: 60px;
    min-height: 60px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 22px;
}

.current-chat-title {
    font-size: 14px;
    font-weight: 600;
}

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

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #4ade80;
}

.mobile-menu {
    display: none;
    background: none;
    border: 0;
    color: white;
    font-size: 22px;
    margin-right: 12px;
}

/* MESSAGES */

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 35px 20px;
}

.welcome {
    max-width: 760px;
    margin: 8vh auto 0;
    text-align: center;
}

.welcome-icon {
    width: 60px;
    height: 60px;
    margin: 0 auto 20px;
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    color: #000;
    font-size: 30px;
}

.welcome h1 {
    font-size: 30px;
    letter-spacing: -1px;
    margin: 0 0 12px;
}

.welcome p {
    max-width: 560px;
    margin: auto;
    color: var(--muted);
    line-height: 1.6;
}

.suggestions {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-top: 30px;
}

.suggestions button {
    border: 1px solid var(--border);
    background: #111113;
    color: #d4d4d8;
    padding: 15px;
    border-radius: 13px;
    text-align: left;
    transition: .2s;
}

.suggestions button:hover {
    background: #18181b;
    border-color: #3f3f46;
}

.suggestions span {
    display: block;
    margin-bottom: 10px;
    font-size: 18px;
}

/* MESSAGE BUBBLES */

.message {
    max-width: 820px;
    margin: 0 auto 25px;
    display: flex;
    gap: 13px;
}

.message-avatar {
    width: 30px;
    height: 30px;
    min-width: 30px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #27272a;
    font-size: 14px;
}

.message.user .message-avatar {
    background: #fff;
    color: #000;
}

.message-body {
    min-width: 0;
    flex: 1;
}

.message-role {
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 5px;
}

.message-content {
    line-height: 1.65;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    color: #e4e4e7;
}

.message.user .message-content {
    color: #fff;
}

/* COMPOSER */

.composer-wrapper {
    padding: 10px 20px 20px;
}

.composer {
    max-width: 820px;
    margin: 0 auto;
    min-height: 58px;
    border: 1px solid #3f3f46;
    background: #18181b;
    border-radius: 17px;
    display: flex;
    align-items: flex-end;
    padding: 8px;
    box-shadow: 0 15px 40px rgba(0,0,0,.2);
}

.composer textarea {
    flex: 1;
    resize: none;
    max-height: 180px;
    border: 0;
    outline: 0;
    background: transparent;
    color: white;
    padding: 10px 12px;
    line-height: 1.5;
}

.composer textarea::placeholder {
    color: #71717a;
}

.send-button {
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 11px;
    background: #fff;
    color: #000;
    font-size: 22px;
    font-weight: 600;
}

.send-button:disabled {
    opacity: .35;
    cursor: not-allowed;
}

.composer-hint {
    text-align: center;
    color: #52525b;
    font-size: 10px;
    margin-top: 8px;
}

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

.overlay {
    display: none;
}

/* MOBILE */

@media (max-width: 700px) {

    .sidebar {
        position: fixed;
        z-index: 20;
        left: -290px;
        top: 0;
        bottom: 0;
        transition: left .25s ease;
    }

    .sidebar.open {
        left: 0;
    }

    .mobile-menu {
        display: block;
    }

    .topbar {
        padding: 0 15px;
    }

    .topbar-right {
        display: none;
    }

    .messages {
        padding: 20px 14px;
    }

    .welcome {
        margin-top: 7vh;
    }

    .welcome h1 {
        font-size: 25px;
    }

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

    .composer-wrapper {
        padding: 7px 10px 12px;
    }

    .auth-card {
        padding: 27px 22px;
    }

    .overlay {
        position: fixed;
        inset: 0;
        z-index: 10;
        background: rgba(0,0,0,.55);
    }

    .overlay.active {
        display: block;
    }
}
EOFcat > /opt/ai-chat/static/app.js <<'EOF'
const messages = document.getElementById("messages");
const messageForm = document.getElementById("messageForm");
const messageInput = document.getElementById("messageInput");
const sendButton = document.getElementById("sendButton");
const chatList = document.getElementById("chatList");
const currentTitle = document.getElementById("currentTitle");

let currentChatId = null;
let sending = false;

function scrollBottom() {
    messages.scrollTop = messages.scrollHeight;
}

function removeWelcome() {
    const welcome = document.getElementById("welcome");
    if (welcome) welcome.remove();
}

function addMessage(role, content) {
    removeWelcome();

    const message = document.createElement("div");
    message.className = `message ${role}`;

    const avatar = document.createElement("div");
    avatar.className = "message-avatar";
    avatar.textContent = role === "user" ? "Вы" : "✦";

    const body = document.createElement("div");
    body.className = "message-body";

    const roleName = document.createElement("div");
    roleName.className = "message-role";
    roleName.textContent = role === "user" ? "Вы" : "Kimi K3";

    const text = document.createElement("div");
    text.className = "message-content";
    text.textContent = content;

    body.appendChild(roleName);
    body.appendChild(text);

    message.appendChild(avatar);
    message.appendChild(body);

    messages.appendChild(message);

    scrollBottom();
}

function addTyping() {
    removeWelcome();

    const message = document.createElement("div");
    message.className = "message";
    message.id = "typing";

    const avatar = document.createElement("div");
    avatar.className = "message-avatar";
    avatar.textContent = "✦";

    const body = document.createElement("div");
    body.className = "message-body";

    const role = document.createElement("div");
    role.className = "message-role";
    role.textContent = "Kimi K3";

    const text = document.createElement("div");
    text.className = "message-content typing";
    text.textContent = "Думаю…";

    body.appendChild(role);
    body.appendChild(text);

    message.appendChild(avatar);
    message.appendChild(body);

    messages.appendChild(message);

    scrollBottom();
}

function removeTyping() {
    const typing = document.getElementById("typing");
    if (typing) typing.remove();
}

async function loadChat(chatId) {

    const response = await fetch(`/api/chats/${chatId}`);

    if (!response.ok) {
        return;
    }

    const data = await response.json();

    currentChatId = chatId;
    currentTitle.textContent = data.title;

    messages.innerHTML = "";

    data.messages.forEach(message => {
        addMessage(message.role, message.content);
    });

    if (!data.messages.length) {
        messages.innerHTML = `
            <div class="welcome">
                <div class="welcome-icon">✦</div>
                <h1>Начнём разговор</h1>
                <p>Напишите первое сообщение.</p>
            </div>
        `;
    }

    document.querySelectorAll(".chat-item").forEach(item => {
        item.classList.toggle(
            "active",
            Number(item.dataset.chatId) === Number(chatId)
        );
    });
}

async function createChat() {

    const response = await fetch("/api/chats", {
        method: "POST"
    });

    if (!response.ok) {
        alert("Не удалось создать чат");
        return;
    }

    const data = await response.json();

    const item = document.createElement("button");
    item.className = "chat-item";
    item.dataset.chatId = data.id;

    item.innerHTML = `
        <span class="chat-icon">◌</span>
        <span class="chat-title">Новый чат</span>
    `;

    item.addEventListener("click", () => loadChat(data.id));

    chatList.prepend(item);

    await loadChat(data.id);

    messageInput.focus();
}

async function sendMessage(text) {

    if (!currentChatId) {
        await createChat();
    }

    if (!currentChatId || sending) {
        return;
    }

    text = text.trim();

    if (!text) {
        return;
    }

    sending = true;
    sendButton.disabled = true;

    addMessage("user", text);
    addTyping();

    messageInput.value = "";
    messageInput.style.height = "auto";

    try {

        const response = await fetch(
            `/api/chats/${currentChatId}/message`,
            {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
                    message: text
                })
            }
        );

        const data = await response.json();

        removeTyping();

        if (!response.ok) {
            addMessage("assistant", data.error || "Произошла ошибка.");
            return;
        }

        addMessage("assistant", data.response);

    } catch (error) {

        removeTyping();
        addMessage(
            "assistant",
            "Не удалось связаться с сервером."
        );

    } finally {

        sending = false;
        sendButton.disabled = false;
        messageInput.focus();
    }
}

messageForm.addEventListener("submit", async event => {
    event.preventDefault();
    await sendMessage(messageInput.value);
});

messageInput.addEventListener("input", () => {
    messageInput.style.height = "auto";
    messageInput.style.height =
        Math.min(messageInput.scrollHeight, 180) + "px";
});

messageInput.addEventListener("keydown", event => {

    if (event.key === "Enter" && !event.shiftKey) {
        event.preventDefault();
        messageForm.requestSubmit();
    }

});

document.querySelectorAll(".chat-item").forEach(item => {

    item.addEventListener("click", () => {
        loadChat(item.dataset.chatId);
    });

});

document.getElementById("newChat").addEventListener(
    "click",
    createChat
);

document.querySelectorAll("[data-suggestion]").forEach(button => {

    button.addEventListener("click", () => {

        messageInput.value = button.dataset.suggestion;
        messageInput.focus();
        messageInput.dispatchEvent(new Event("input"));

    });

});

const mobileMenu = document.getElementById("mobileMenu");
const sidebar = document.getElementById("sidebar");
const overlay = document.getElementById("overlay");

mobileMenu.addEventListener("click", () => {
    sidebar.classList.add("open");
    overlay.classList.add("active");
});

overlay.addEventListener("click", () => {
    sidebar.classList.remove("open");
    overlay.classList.remove("active");
});

if (chatList.children.length > 0) {
    loadChat(chatList.children[0].dataset.chatId);
}
