/**
 * chat.css — 对话助手(ChatGPT 式)面板。
 *
 * 左侧会话历史 + 右侧消息流 + 底部输入区。
 * 颜色 / 圆角 / 动效全部走 style.css 里已有的语义变量(--bg-* / --text-* / --border-color /
 * --radius-* / --motion-*),它们在 [data-theme="dark"] 下已自动换值,本文件不再另建调色板。
 */

/* 主工具栏「对话」入口按钮 */
.btn-chat {
    font-weight: 600;
}

/* ── 外壳 ─────────────────────────────────────────────────────── */
.chat-modal .chat-shell {
    display: flex;
    position: relative;
    width: min(1160px, calc(100vw - 48px));
    height: min(880px, calc(100vh - 48px));
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    overflow: hidden;
    box-shadow: var(--shadow-lg, 0 20px 60px rgba(0, 0, 0, 0.24));
    transform: scale(0.985);
    transition: transform var(--motion-spring);
}

.chat-modal[style*="display: flex"] .chat-shell,
.chat-modal[style*="display:flex"] .chat-shell {
    transform: scale(1);
}

/* ── 会话侧栏 ─────────────────────────────────────────────────── */
.chat-sidebar {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    width: 264px;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
}

.chat-sidebar-head,
.chat-sidebar-foot {
    padding: 12px;
}

.chat-sidebar-foot {
    border-top: 1px solid var(--border-color);
}

.chat-new-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: 100%;
    min-height: 38px;
    padding: 0 12px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md, 8px);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--motion-fast), border-color var(--motion-fast);
}

.chat-new-btn:hover {
    background: var(--fill-hover);
    border-color: var(--brand-primary);
}

.chat-clear-all-btn {
    width: 100%;
    min-height: 32px;
    background: transparent;
    color: var(--text-secondary);
    border: none;
    border-radius: var(--radius-sm, 6px);
    font-size: 12px;
    cursor: pointer;
    transition: background var(--motion-fast), color var(--motion-fast);
}

.chat-clear-all-btn:hover {
    background: var(--color-danger-bg);
    color: var(--color-danger);
}

.chat-session-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 0 8px 8px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.chat-session-hint {
    padding: 16px 10px;
    color: var(--text-secondary);
    font-size: 12px;
    line-height: 1.6;
}

.chat-session-hint.is-error {
    color: var(--color-danger);
}

.chat-session-item {
    display: flex;
    align-items: center;
    gap: 2px;
    border-radius: var(--radius-md, 8px);
    transition: background var(--motion-fast);
}

.chat-session-item:hover {
    background: var(--sidebar-hover);
}

.chat-session-item.is-active {
    background: var(--sidebar-active-bg);
}

.chat-session-open {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: baseline;
    gap: 8px;
    padding: 9px 10px;
    background: transparent;
    border: none;
    text-align: left;
    cursor: pointer;
    color: inherit;
}

.chat-session-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13px;
    color: var(--text-primary);
}

.chat-session-item.is-active .chat-session-title {
    font-weight: 600;
}

.chat-session-time {
    flex-shrink: 0;
    font-size: 11px;
    color: var(--text-muted);
}

.chat-session-tools {
    display: flex;
    align-items: center;
    gap: 2px;
    padding-right: 6px;
    opacity: 0;
    transition: opacity var(--motion-fast);
}

.chat-session-item:hover .chat-session-tools,
.chat-session-item:focus-within .chat-session-tools {
    opacity: 1;
}

.chat-session-tool {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm, 6px);
    color: var(--text-secondary);
    cursor: pointer;
}

.chat-session-tool:hover {
    background: var(--fill-hover);
    color: var(--text-primary);
}

.chat-session-tool.is-danger:hover {
    background: var(--color-danger-bg);
    color: var(--color-danger);
}

.chat-session-item.is-editing {
    padding: 4px 6px;
}

.chat-session-rename-input {
    width: 100%;
    min-height: 30px;
    padding: 0 8px;
    background: var(--input-bg);
    color: var(--text-primary);
    border: 1px solid var(--brand-primary);
    border-radius: var(--radius-sm, 6px);
    font-size: 13px;
    outline: none;
}

.chat-sidebar-scrim {
    display: none;
}

/* ── 右侧主区 ─────────────────────────────────────────────────── */
.chat-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
}

.chat-topbar {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.chat-panel-title {
    flex: 1;
    min-width: 0;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

.chat-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    flex-shrink: 0;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm, 6px);
    color: var(--text-secondary);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    transition: background var(--motion-fast), color var(--motion-fast);
}

.chat-icon-btn:hover {
    background: var(--fill-hover);
    color: var(--text-primary);
}

.chat-sidebar-toggle {
    display: none;
}

/* ── 消息流 ───────────────────────────────────────────────────── */
.chat-thread {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 20px 16px;
}

.chat-thread-inner {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
    max-width: 760px;
    margin: 0 auto;
}

.chat-thread-hint {
    padding: 32px 0;
    text-align: center;
    color: var(--text-secondary);
    font-size: 13px;
}

.chat-msg {
    display: flex;
    gap: 10px;
}

.chat-msg.is-user {
    justify-content: flex-end;
}

.chat-bubble-user {
    max-width: 82%;
    padding: 10px 14px;
    background: var(--brand-primary);
    color: var(--text-on-brand);
    border-radius: 16px 16px 4px 16px;
    font-size: 14px;
    line-height: 1.65;
    white-space: pre-wrap;
    word-break: break-word;
}

.chat-msg-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    margin-top: 2px;
    background: var(--color-ai-bg);
    color: var(--color-ai);
    border-radius: 50%;
}

.chat-msg-body {
    flex: 1;
    min-width: 0;
}

.chat-md {
    font-size: 14px;
    line-height: 1.75;
    color: var(--text-primary);
    word-break: break-word;
}

.chat-md > :first-child { margin-top: 0; }
.chat-md > :last-child { margin-bottom: 0; }

.chat-md pre {
    position: relative;
    overflow-x: auto;
}

.chat-md .chat-table-scroll,
.chat-table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.chat-code-copy-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 26px;
    height: 26px;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm, 6px);
    font-size: 13px;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity var(--motion-fast);
}

.chat-md pre:hover .chat-code-copy-btn,
.chat-code-copy-btn:focus-visible,
.chat-code-copy-btn.is-copied {
    opacity: 1;
}

.chat-msg-actions {
    display: flex;
    gap: 6px;
    margin-top: 8px;
    opacity: 0;
    transition: opacity var(--motion-fast);
}

.chat-msg.is-assistant:hover .chat-msg-actions,
.chat-msg-actions:focus-within {
    opacity: 1;
}

.chat-msg-action {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm, 6px);
    font-size: 12px;
    cursor: pointer;
}

.chat-msg-action:hover {
    background: var(--fill-hover);
    color: var(--text-primary);
}

/* 流式光标 */
.chat-caret {
    display: inline-block;
    width: 7px;
    height: 15px;
    margin-left: 2px;
    vertical-align: text-bottom;
    background: var(--text-primary);
    animation: chat-caret-blink 1s steps(2, start) infinite;
}

@keyframes chat-caret-blink {
    to { visibility: hidden; }
}

/* typing 三点 */
.chat-typing {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 6px 0;
}

.chat-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: chat-typing-bounce 1.2s infinite ease-in-out both;
}

.chat-typing span:nth-child(1) { animation-delay: -0.24s; }
.chat-typing span:nth-child(2) { animation-delay: -0.12s; }

@keyframes chat-typing-bounce {
    0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

/* 失败提示(inline,不弹 alert)*/
.chat-msg-error {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    padding: 10px 12px;
    background: var(--color-danger-bg);
    border: 1px solid var(--color-danger);
    border-radius: var(--radius-md, 8px);
}

.chat-msg-error-text {
    flex: 1;
    min-width: 0;
    font-size: 13px;
    color: var(--color-danger);
}

.chat-retry-btn {
    padding: 4px 12px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm, 6px);
    font-size: 12px;
    cursor: pointer;
}

.chat-retry-btn:hover {
    border-color: var(--brand-primary);
}

/* ── 空态 ─────────────────────────────────────────────────────── */
.chat-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 100%;
    padding: 40px 16px;
    text-align: center;
}

.chat-empty-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    margin-bottom: 4px;
    background: var(--color-ai-bg);
    color: var(--color-ai);
    border-radius: 50%;
}

.chat-empty-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
}

.chat-empty-desc {
    font-size: 13px;
    color: var(--text-secondary);
}

.chat-suggest-chips {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    max-width: 560px;
    margin-top: 12px;
}

.chat-suggest-chip {
    padding: 8px 14px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 999px;
    font-size: 13px;
    cursor: pointer;
    transition: background var(--motion-fast), border-color var(--motion-fast);
}

.chat-suggest-chip:hover {
    background: var(--fill-hover);
    border-color: var(--brand-primary);
}

/* ── 输入区 ───────────────────────────────────────────────────── */
.chat-composer {
    flex-shrink: 0;
    padding: 12px 16px 14px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.chat-composer-inner {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    width: 100%;
    max-width: 760px;
    margin: 0 auto;
    padding: 6px 6px 6px 12px;
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    transition: border-color var(--motion-fast);
}

.chat-composer-inner:focus-within {
    border-color: var(--brand-primary);
}

.chat-input {
    flex: 1;
    min-width: 0;
    min-height: 32px;
    max-height: 200px;
    padding: 6px 0;
    background: transparent;
    color: var(--text-primary);
    border: none;
    outline: none;
    resize: none;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.6;
}

.chat-input::placeholder {
    color: var(--text-muted);
}

.chat-send-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 34px;
    height: 34px;
    background: var(--brand-primary);
    color: var(--text-on-brand);
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: background var(--motion-fast), transform var(--motion-fast);
}

.chat-send-btn:hover {
    background: var(--brand-hover);
}

.chat-send-btn:active {
    transform: scale(0.94);
}

.chat-send-btn.is-stopping {
    background: var(--color-danger);
}

.chat-composer-hint {
    max-width: 760px;
    margin: 6px auto 0;
    font-size: 11px;
    color: var(--text-muted);
    text-align: center;
}

/* ── 移动端:全屏 + 侧栏抽屉 ──────────────────────────────────── */
@media (max-width: 768px) {
    .chat-modal {
        padding: 0;
        align-items: stretch;
        justify-content: stretch;
    }

    .chat-modal .chat-shell {
        width: 100%;
        height: 100dvh;
        max-height: 100dvh;
        border: none;
        border-radius: 0;
    }

    .chat-sidebar {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 2;
        width: 82vw;
        max-width: 300px;
        transform: translateX(-102%);
        transition: transform var(--motion-emphasized);
    }

    .chat-shell.sidebar-open .chat-sidebar {
        transform: translateX(0);
    }

    .chat-sidebar-scrim {
        display: block;
        position: absolute;
        inset: 0;
        z-index: 1;
        background: rgba(0, 0, 0, 0.42);
        opacity: 0;
        pointer-events: none;
        transition: opacity var(--motion-fast);
    }

    .chat-shell.sidebar-open .chat-sidebar-scrim {
        opacity: 1;
        pointer-events: auto;
    }

    .chat-sidebar-toggle {
        display: inline-flex;
    }

    .chat-thread {
        padding: 16px 12px;
    }

    .chat-bubble-user {
        max-width: 88%;
    }

    .chat-composer {
        padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    }

    /* 移动端没有 hover,操作条常驻可见 */
    .chat-msg-actions,
    .chat-session-tools,
    .chat-code-copy-btn {
        opacity: 1;
    }

    .chat-input {
        /* iOS Safari 小于 16px 会触发自动放大 */
        font-size: 16px;
    }
}
