* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    min-height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: #d8dee8;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #1f2329;
    -webkit-text-size-adjust: 100%;
}

button,
input,
select {
    font: inherit;
}

.chat-container {
    width: 100%;
    height: 100vh;
    height: 100svh;
    height: var(--app-height, 100dvh);
    min-height: 0;
    background: #f2f3f5;
    display: grid;
    grid-template-columns: 0 0 1fr;
    overflow: hidden;
    position: relative;
}

.app-rail,
.session-list {
    display: none;
}

.conversation-panel {
    min-width: 0;
    min-height: 0;
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background: #f2f3f5;
}

@media (min-width: 900px) {
    body {
        padding: 30px;
    }

    .chat-container {
        width: min(94vw, 1180px);
        height: min(86vh, 760px);
        min-height: 620px;
        grid-template-columns: 64px 292px minmax(0, 1fr);
        border-radius: 2px;
        box-shadow: 0 22px 60px rgba(29, 38, 57, 0.24);
    }

    .app-rail {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 18px;
        padding: 22px 0 18px;
        background: #2f73bd;
        color: rgba(255, 255, 255, 0.86);
    }

    .session-list {
        display: flex;
        flex-direction: column;
        background: #e5e8ed;
        border-right: 1px solid #d6dae2;
    }
}

.rail-avatar {
    width: 36px;
    height: 36px;
    border-radius: 3px;
    overflow: hidden;
    background: #d8dee8;
    margin-bottom: 14px;
}

.rail-avatar img,
.session-avatar img,
.msg-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.rail-icon,
.rail-menu {
    width: 38px;
    height: 38px;
    border: 0;
    background: transparent;
    color: rgba(255, 255, 255, 0.74);
    cursor: pointer;
    border-radius: 4px;
}

.rail-icon {
    font-size: 19px;
}

.rail-icon.active,
.rail-icon:hover,
.rail-menu:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.12);
}

.rail-menu {
    margin-top: auto;
    font-size: 22px;
}

.session-search {
    height: 58px;
    padding: 14px 14px 10px;
    display: grid;
    grid-template-columns: 22px 1fr 32px;
    align-items: center;
    gap: 4px;
}

.session-search span {
    height: 32px;
    display: grid;
    place-items: center;
    background: #d3d7de;
    color: #7d858f;
    border-radius: 2px 0 0 2px;
}

.session-search input {
    height: 32px;
    border: 0;
    outline: 0;
    color: #7d858f;
    background: #d3d7de;
    padding: 0 6px;
    border-radius: 0 2px 2px 0;
}

.session-search button {
    height: 32px;
    border: 0;
    background: transparent;
    color: #6b7280;
    cursor: pointer;
    font-size: 26px;
    line-height: 1;
}

.session-item {
    min-height: 76px;
    display: grid;
    grid-template-columns: 48px 1fr auto;
    gap: 12px;
    align-items: center;
    padding: 10px 12px 10px 16px;
    color: #4e5969;
}

.session-item.active {
    background: #c7cbd2;
}

.session-avatar {
    width: 48px;
    height: 48px;
    border-radius: 3px;
    overflow: hidden;
    background: #3370ff;
    color: #fff;
    display: grid;
    place-items: center;
    font-weight: 700;
}

.session-avatar.doc {
    background: #f6b923;
}

.session-avatar.team {
    background: #39b36b;
}

.session-meta {
    min-width: 0;
}

.session-title {
    font-size: 14px;
    font-weight: 600;
    color: #1f2329;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.session-preview {
    margin-top: 7px;
    font-size: 13px;
    color: #8a919f;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.session-item time {
    align-self: start;
    margin-top: 8px;
    font-size: 12px;
    color: #9aa1ad;
}

.chat-header {
    height: 74px;
    background: #f8f9fb;
    padding: 0 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #e1e4ea;
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    min-width: 0;
}

.header-info {
    min-width: 0;
}

.header-info h1 {
    font-size: 18px;
    font-weight: 700;
    color: #111827;
    white-space: nowrap;
}

.header-info p {
    margin-top: 5px;
    font-size: 13px;
    color: #8a919f;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.header-right label {
    font-size: 14px;
    font-weight: 600;
    color: #4e5969;
    white-space: nowrap;
}

.role-picker {
    width: clamp(150px, 24vw, 220px);
}

.header-right input,
.header-right select {
    height: 36px;
    padding: 0 12px;
    border: 1px solid #d6dae2;
    border-radius: 4px;
    font-size: 15px;
    width: 100%;
    background: #fff;
    outline: none;
    color: #1f2329;
}

.header-right input {
    text-align: center;
}

.header-right select {
    cursor: pointer;
    text-overflow: ellipsis;
}

.header-right input:focus,
.header-right select:focus {
    border-color: #2f73bd;
    box-shadow: 0 0 0 2px rgba(47, 115, 189, 0.12);
}

.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 24px 28px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background-color: #f2f3f5;
    background-position: 0 0;
    background-repeat: repeat;
    background-size: 260px 150px;
}

.message-row {
    display: flex;
    align-items: flex-start;
    margin-bottom: 8px;
    width: 100%;
    animation: fadeIn 0.25s ease;
}

.message-row.user-row {
    flex-direction: row-reverse;
}

.msg-avatar {
    width: 36px;
    height: 36px;
    border-radius: 3px;
    flex-shrink: 0;
    background: #2f73bd;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
    font-weight: 700;
    font-size: 15px;
    overflow: hidden;
}

.user-row .msg-avatar {
    background: #2f73bd;
    margin-left: 12px;
}

.ai-row .msg-avatar {
    background: #2f73bd;
    margin-right: 12px;
}

.msg-content-wrapper {
    display: flex;
    flex-direction: column;
    max-width: min(70%, 560px);
}

.user-row .msg-content-wrapper {
    align-items: flex-end;
}

.ai-row .msg-content-wrapper {
    align-items: flex-start;
}

.msg-name {
    font-size: 12px;
    color: #8a919f;
    margin-bottom: 5px;
}

.message-bubble {
    padding: 9px 12px;
    border-radius: 3px;
    font-size: 14px;
    line-height: 1.58;
    position: relative;
    max-width: 100%;
    overflow-wrap: break-word;
    word-break: normal;
    box-shadow: 0 1px 2px rgba(31, 35, 41, 0.06);
}

.message-bubble::before {
    content: '';
    position: absolute;
    top: 13px;
    width: 0;
    height: 0;
    border-style: solid;
}

.ai-row .message-bubble::before {
    left: -7px;
    border-width: 5px 7px 5px 0;
    border-color: transparent #fff transparent transparent;
}

.user-row .message-bubble::before {
    right: -7px;
    border-width: 5px 0 5px 7px;
    border-color: transparent transparent transparent #95ec69;
}

.ai-row .message-bubble {
    background: #fff;
    color: #111827;
}

.user-row .message-bubble {
    background: #95ec69;
    color: #111827;
}

.message-bubble p {
    margin-bottom: 8px;
}

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

.message-bubble pre,
.message-bubble code {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.copy-btn {
    margin-left: 8px;
    background: none;
    border: none;
    cursor: pointer;
    color: #a8abb2;
    padding: 5px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.copy-btn:hover {
    color: #2f73bd;
    background: rgba(47, 115, 189, 0.08);
}

.copy-btn svg {
    width: 16px;
    height: 16px;
}

.system-message {
    align-self: center;
    background: rgba(31, 35, 41, 0.06);
    color: #8a919f;
    font-size: 13px;
    padding: 6px 12px;
    border-radius: 3px;
    margin-bottom: 12px;
}

.chat-input-area {
    min-height: 104px;
    max-height: 148px;
    padding: 10px 22px 14px;
    background: #fff;
    border-top: 1px solid #e1e4ea;
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex-shrink: 0;
}

.chat-tools {
    height: 26px;
    display: flex;
    align-items: center;
    gap: 18px;
    color: #7d858f;
    font-size: 18px;
    line-height: 1;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    min-height: 38px;
}

.message-input {
    flex: 1;
    height: 38px;
    max-height: 76px;
    border: 0;
    border-radius: 3px;
    padding: 8px;
    font-family: inherit;
    font-size: 15px;
    line-height: 22px;
    outline: none;
    resize: none;
    background: #fff;
    color: #1f2329;
    min-width: 0;
    overflow-y: auto;
}

.message-input:focus {
    box-shadow: none;
}

#sendBtn {
    width: 92px;
    height: 36px;
    border-radius: 3px;
    background: #2f73bd;
    border: none;
    color: white;
    font-size: 15px;
    cursor: pointer;
    flex-shrink: 0;
}

#sendBtn:hover {
    background: #2463a8;
}

@media (max-width: 899px) {
    body {
        display: block;
    }

    .chat-container {
        grid-template-columns: minmax(0, 1fr);
    }

    .chat-header {
        height: 66px;
        padding: 0 16px;
    }

    .header-info h1 {
        font-size: 17px;
    }

    .header-info p {
        max-width: 180px;
    }

    .header-right {
        gap: 6px;
    }

    .header-right input {
        width: 88px;
        font-size: 16px;
    }

    .chat-messages {
        padding: 18px 16px;
    }

    .msg-content-wrapper {
        max-width: 74%;
    }

    .chat-input-area {
        min-height: 88px;
        max-height: 126px;
        padding: 8px 12px max(10px, env(safe-area-inset-bottom));
    }

    .chat-tools {
        display: none;
    }

    .message-input {
        height: 40px;
        max-height: 84px;
        border: 1px solid #e1e4ea;
        padding: 8px 12px;
        background: #f8f9fb;
        font-size: 16px;
    }

    .message-input:focus {
        box-shadow: none;
    }

    #sendBtn {
        width: 72px;
        height: 40px;
    }
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.typing-indicator {
    padding: 9px 12px;
    background: #fff;
    border-radius: 3px;
    display: inline-block;
}

.typing-indicator span {
    display: inline-block;
    width: 6px;
    height: 6px;
    background: #90949c;
    border-radius: 50%;
    margin: 0 2px;
    animation: bounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}
