:root {
    --page-bg: #dfe3e6;
    --window-bg: #ededed;
    --header-bg: #f7f7f7;
    --border: #d8d8d8;
    --text: #1f2328;
    --muted: #7a8188;
    --ai-bubble: #ffffff;
    --user-bubble: #95ec69;
    --input-bg: #ffffff;
    --button-bg: #07c160;
    --button-hover: #06ad56;
    --danger: #b74848;
    --shadow: 0 18px 48px rgba(30, 36, 42, 0.18);
    --header-height: 72px;
    --composer-max: 196px;
    --radius: 14px;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
}

body {
    overflow: hidden;
    background: var(--page-bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}

button,
textarea {
    font: inherit;
}

button {
    -webkit-tap-highlight-color: transparent;
}

.chat-shell {
    width: min(100%, 880px);
    height: min(100dvh, 920px);
    margin: max(0px, calc((100dvh - min(100dvh, 920px)) / 2)) auto 0;
    display: grid;
    grid-template-rows: var(--header-height) minmax(0, 1fr) auto;
    overflow: hidden;
    background: var(--window-bg);
    box-shadow: var(--shadow);
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 10px 18px;
    border-bottom: 1px solid var(--border);
    background: var(--header-bg);
    z-index: 2;
}

.contact {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 12px;
}

.contact-avatar,
.message-avatar {
    flex: 0 0 auto;
    display: block;
    object-fit: cover;
    background: #d8dde0;
}

.contact-avatar {
    width: 46px;
    height: 46px;
    border-radius: 10px;
}

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

.contact-meta h1 {
    margin: 0;
    overflow: hidden;
    font-size: 17px;
    font-weight: 650;
    line-height: 1.35;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.contact-meta p {
    margin: 3px 0 0;
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--muted);
    font-size: 12px;
}

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--button-bg);
}

.header-button,
.retry-button {
    border: 0;
    color: var(--muted);
    background: transparent;
    cursor: pointer;
}

.header-button {
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 14px;
}

.header-button:hover {
    background: rgba(0, 0, 0, 0.05);
}

.header-button:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

.message-list {
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 22px 18px 28px;
    scroll-behavior: smooth;
}

.message-row {
    width: 100%;
    display: flex;
    align-items: flex-start;
    gap: 10px;
    margin-bottom: 18px;
}

.message-row.user {
    justify-content: flex-end;
}

.message-avatar {
    width: 40px;
    height: 40px;
    border-radius: 8px;
}

.message-body {
    min-width: 0;
    max-width: min(72%, 620px);
}

.message-row.user .message-body {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.message-bubble {
    position: relative;
    width: fit-content;
    max-width: 100%;
    min-height: 40px;
    padding: 10px 13px;
    border-radius: 6px;
    background: var(--ai-bubble);
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
}

.message-row.user .message-bubble {
    background: var(--user-bubble);
}

.message-bubble::before {
    content: "";
    position: absolute;
    top: 13px;
    left: -6px;
    width: 12px;
    height: 12px;
    background: inherit;
    transform: rotate(45deg);
    border-radius: 1px;
}

.message-row.user .message-bubble::before {
    right: -6px;
    left: auto;
}

.message-text {
    position: relative;
    z-index: 1;
    margin: 0;
    color: var(--text);
    font-size: 15px;
    line-height: 1.58;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.message-meta {
    min-height: 20px;
    margin-top: 4px;
    display: flex;
    align-items: center;
    gap: 9px;
    color: var(--muted);
    font-size: 11px;
}

.message-row.user .message-meta {
    justify-content: flex-end;
}

.message-error {
    outline: 1px solid rgba(183, 72, 72, 0.2);
}

.retry-button {
    padding: 1px 0;
    color: var(--danger);
    font-size: 12px;
}

.retry-button:hover {
    text-decoration: underline;
}

.typing-indicator {
    height: 19px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.typing-indicator i {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #9aa0a6;
    animation: typing-bounce 1.15s infinite ease-in-out;
}

.typing-indicator i:nth-child(2) {
    animation-delay: 0.14s;
}

.typing-indicator i:nth-child(3) {
    animation-delay: 0.28s;
}

@keyframes typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.55; }
    30% { transform: translateY(-4px); opacity: 1; }
}

.composer {
    padding: 10px 14px max(10px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    background: var(--header-bg);
    z-index: 2;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 9px;
}

#message-input {
    width: 100%;
    min-height: 42px;
    max-height: 144px;
    resize: none;
    overflow-y: hidden;
    padding: 10px 12px;
    border: 1px solid transparent;
    border-radius: 7px;
    outline: none;
    color: var(--text);
    background: var(--input-bg);
    line-height: 1.45;
}

#message-input:focus {
    border-color: #c9d0d4;
}

.send-button,
.stop-button {
    flex: 0 0 auto;
    height: 40px;
    padding: 0 16px;
    border: 0;
    border-radius: 7px;
    cursor: pointer;
}

.send-button {
    color: #ffffff;
    background: var(--button-bg);
}

.send-button:hover:not(:disabled) {
    background: var(--button-hover);
}

.send-button:disabled {
    color: #a3a8ac;
    background: #e1e3e5;
    cursor: not-allowed;
}

.stop-button {
    color: var(--danger);
    background: #f3e8e8;
}

.composer-hint {
    margin: 7px 2px 0;
    color: var(--muted);
    font-size: 11px;
}

@media (max-width: 720px) {
    .chat-shell {
        width: 100%;
        height: 100dvh;
        margin: 0;
        box-shadow: none;
    }

    .chat-header {
        padding-right: 12px;
        padding-left: 12px;
    }

    .message-list {
        padding: 18px 12px 24px;
    }

    .message-body {
        max-width: calc(100% - 60px);
    }

    .message-avatar {
        width: 38px;
        height: 38px;
    }

    .composer {
        padding-right: 10px;
        padding-left: 10px;
    }

    .composer-hint {
        display: none;
    }

    .send-button,
    .stop-button {
        padding: 0 13px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .message-list {
        scroll-behavior: auto;
    }

    .typing-indicator i {
        animation: none;
    }
}

/* Mi Manchi 分条回复与本地黄色小人表情 */
.message-row.reply-part {
    margin-bottom: 11px;
}

.message-bubble.sticker-bubble {
    min-height: 0;
    padding: 0;
    background: transparent;
    box-shadow: none;
}

.message-bubble.sticker-bubble::before {
    display: none;
}

.sticker-message-image {
    width: 116px;
    height: 116px;
    display: block;
    object-fit: contain;
    border-radius: 18px;
}

.composer {
    position: relative;
}

.sticker-button {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    color: #596168;
    background: transparent;
    font-size: 27px;
    line-height: 1;
    cursor: pointer;
}

.sticker-button:hover:not(:disabled),
.sticker-button.active {
    color: var(--button-bg);
    background: rgba(7, 193, 96, 0.08);
}

.sticker-button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.sticker-panel {
    position: absolute;
    right: 10px;
    bottom: calc(100% - 1px);
    left: 10px;
    z-index: 5;
    display: grid;
    grid-template-columns: repeat(6, minmax(58px, 1fr));
    gap: 7px;
    max-height: min(330px, 46vh);
    padding: 12px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 13px 13px 0 0;
    background: rgba(250, 250, 250, 0.98);
    box-shadow: 0 -12px 34px rgba(30, 36, 42, 0.13);
    backdrop-filter: blur(12px);
}

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

.sticker-item {
    min-width: 0;
    aspect-ratio: 1;
    padding: 4px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    cursor: pointer;
}

.sticker-item:hover,
.sticker-item:focus-visible {
    outline: none;
    background: #eceff1;
}

.sticker-item img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: contain;
}

@media (max-width: 720px) {
    .sticker-panel {
        grid-template-columns: repeat(4, minmax(58px, 1fr));
        right: 0;
        left: 0;
        border-right: 0;
        border-left: 0;
        border-radius: 12px 12px 0 0;
    }

    .sticker-message-image {
        width: 104px;
        height: 104px;
    }

    .sticker-button {
        width: 36px;
        height: 40px;
        font-size: 25px;
    }
}
