/* ========== AI 聊天模块样式 ========== */

/* 整体布局 - 适配 .page-wrap 的 padding: 7px 24px 20px */
.ai-chat-layout {
    display: flex;
    height: calc(100vh - 56px);
    margin: -7px -24px -20px;
    overflow: hidden;
    background: var(--bg);
}

/* ========== 左侧对话列表 ========== */
.ai-sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--card-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.ai-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 12px 12px;
    border-bottom: 1px solid var(--border-light);
}

.ai-sidebar-header h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    margin: 0;
}

.ai-new-chat-btn {
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background .15s;
}
.ai-new-chat-btn:hover {
    background: var(--primary-hover);
}

.ai-conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.ai-conv-empty {
    padding: 40px 20px;
    text-align: center;
    color: var(--text-tertiary);
    font-size: 13px;
}

.ai-conv-item {
    display: flex;
    flex-direction: column;
    padding: 10px 12px;
    margin: 0 8px 2px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background .15s;
    position: relative;
}

.ai-conv-item:hover {
    background: var(--bg-hover);
}

.ai-conv-item.active {
    background: var(--primary-light);
}

.ai-conv-item-title {
    font-size: 13px;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
    padding-right: 24px;
}

.ai-conv-item.active .ai-conv-item-title {
    color: var(--primary);
    font-weight: 500;
}

.ai-conv-item-meta {
    font-size: 11px;
    color: var(--text-tertiary);
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-conv-item-del {
    position: absolute;
    top: 8px;
    right: 8px;
    display: none;
    background: none;
    border: none;
    padding: 3px;
    cursor: pointer;
    color: var(--text-tertiary);
    border-radius: 3px;
    line-height: 1;
}

.ai-conv-item:hover .ai-conv-item-del {
    display: flex;
}

.ai-conv-item-del:hover {
    background: rgba(239,68,68,0.1);
    color: var(--error);
}

/* ========== 右侧聊天主区域 ========== */
.ai-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg);
}

.ai-main-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    background: var(--card-bg);
    border-bottom: 1px solid var(--border-light);
    min-height: 48px;
    gap: 12px;
}

.ai-main-title {
    font-size: 15px;
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.ai-main-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.ai-model-select {
    padding: 5px 28px 5px 10px;
    font-size: 13px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--card-bg);
    color: var(--text-secondary);
    outline: none;
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23646A73' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
}

.ai-model-select:focus {
    border-color: var(--primary);
}

.ai-settings-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text-secondary);
    transition: all .15s;
}
.ai-settings-btn:hover {
    background: var(--bg-hover);
    color: var(--text);
}

/* ========== 消息区域 ========== */
#aiMessages,
.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    scroll-behavior: smooth;
}

/* 欢迎空状态 */
.ai-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100%;
    text-align: center;
    color: var(--text-tertiary);
    padding: 60px 20px;
}

.ai-welcome-icon {
    width: 72px;
    height: 72px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 20px;
    margin-bottom: 20px;
}

.ai-welcome-icon svg {
    stroke: #fff;
}

.ai-welcome h3 {
    font-size: 20px;
    font-weight: 600;
    color: var(--text);
    margin: 0 0 6px;
}

.ai-welcome p {
    font-size: 14px;
    color: var(--text-tertiary);
    margin: 0;
    max-width: 360px;
}

/* ========== 消息气泡 ========== */
.ai-msg {
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
    max-width: 80%;
}

.ai-msg.user {
    margin-left: auto;
    flex-direction: row-reverse;
}

.ai-msg-avatar {
    width: 32px;
    height: 32px;
    min-width: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.ai-msg.user .ai-msg-avatar {
    background: var(--primary);
}

.ai-msg-content {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.7;
    word-break: break-word;
    min-width: 0;
}

.ai-msg.user .ai-msg-content {
    background: var(--primary);
    color: #fff;
    border-top-right-radius: 4px;
}

.ai-msg.assistant .ai-msg-content {
    background: var(--card-bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-top-left-radius: 4px;
}

/* ========== Markdown 内容样式 ========== */
.ai-msg-content p { margin: 0 0 8px; }
.ai-msg-content p:last-child { margin-bottom: 0; }
.ai-msg-content ul, .ai-msg-content ol { margin: 4px 0 8px; padding-left: 20px; }
.ai-msg-content li { margin-bottom: 2px; }
.ai-msg-content strong { font-weight: 600; }
.ai-msg-content blockquote {
    border-left: 3px solid var(--border);
    margin: 8px 0;
    padding: 4px 12px;
    color: var(--text-secondary);
}

.ai-msg-content h1, .ai-msg-content h2, .ai-msg-content h3,
.ai-msg-content h4, .ai-msg-content h5, .ai-msg-content h6 {
    margin: 12px 0 6px;
    font-weight: 600;
    line-height: 1.4;
}
.ai-msg-content h1 { font-size: 18px; }
.ai-msg-content h2 { font-size: 16px; }
.ai-msg-content h3 { font-size: 15px; }

.ai-msg-content table { border-collapse: collapse; margin: 8px 0; font-size: 13px; width: 100%; }
.ai-msg-content table th, .ai-msg-content table td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.ai-msg-content table th { background: #f8f9fa; font-weight: 600; }

/* 行内代码 */
.ai-msg-content code {
    background: rgba(0,0,0,0.06);
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 13px;
    font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
}

/* 代码块 */
.ai-code-block {
    margin: 8px 0;
    border-radius: 8px;
    overflow: hidden;
    background: #1e1e2e;
}

.ai-code-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 12px;
    background: #181825;
    font-size: 12px;
    color: #6c7086;
}

.ai-code-copy-btn {
    background: none;
    border: none;
    color: #6c7086;
    cursor: pointer;
    font-size: 12px;
    padding: 2px 6px;
    border-radius: 3px;
}
.ai-code-copy-btn:hover {
    background: rgba(255,255,255,0.1);
    color: #cdd6f4;
}

.ai-msg-content pre {
    background: #1e1e2e;
    margin: 0;
    padding: 0;
    border-radius: 0;
}

.ai-msg-content .ai-code-block pre {
    border-radius: 0;
}

.ai-msg-content pre code {
    display: block;
    padding: 14px 16px;
    background: none;
    color: #cdd6f4;
    font-size: 13px;
    line-height: 1.5;
    overflow-x: auto;
    border-radius: 0;
}

/* 用户消息中的代码 */
.ai-msg.user .ai-msg-content code { background: rgba(255,255,255,0.2); color: #fff; }

/* ========== 流式输出光标 ========== */
.ai-streaming-cursor::after {
    content: '';
    display: inline-block;
    width: 2px;
    height: 16px;
    background: var(--primary);
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: ai-blink 1s infinite;
}

@keyframes ai-blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

.ai-stopped {
    font-size: 12px;
    color: var(--text-tertiary);
    font-style: italic;
}

.ai-error {
    color: var(--error);
    font-size: 13px;
}

/* ========== 输入区域 ========== */
.ai-input-area {
    padding: 12px 20px 16px;
    background: var(--bg);
}

.ai-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 8px 8px 8px 16px;
    transition: border-color .2s, box-shadow .2s;
    max-width: 800px;
    margin: 0 auto;
}

.ai-input-wrapper:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.ai-input {
    flex: 1;
    border: none;
    outline: none;
    resize: none;
    font-size: 14px;
    line-height: 1.5;
    background: transparent;
    color: var(--text);
    font-family: inherit;
    min-height: 21px;
    max-height: 150px;
    padding: 2px 0;
}

.ai-input::placeholder { color: var(--text-tertiary); }
.ai-input:disabled { opacity: 0.6; }

.ai-send-btn,
.ai-stop-btn {
    width: 36px;
    height: 36px;
    min-width: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background .15s;
}

.ai-send-btn {
    background: var(--primary);
    color: #fff;
}
.ai-send-btn:hover { background: var(--primary-hover); }

.ai-stop-btn {
    background: var(--error);
    color: #fff;
}
.ai-stop-btn:hover { background: #dc2626; }

/* ========== 设置弹窗 ========== */
.ai-settings-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ai-settings-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.4);
}

.ai-settings-panel {
    position: relative;
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 480px;
    max-height: 80vh;
    overflow-y: auto;
    padding: 24px;
}

.ai-settings-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}

.ai-settings-header h3 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
}

.ai-settings-header button {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
    line-height: 1;
    padding: 0 4px;
}

.ai-settings-body label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    margin-bottom: 4px;
    margin-top: 14px;
}

.ai-settings-body label:first-child {
    margin-top: 0;
}

.ai-settings-body input,
.ai-settings-body select,
.ai-settings-body textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    outline: none;
    box-sizing: border-box;
    background: var(--bg);
    color: var(--text);
}

.ai-settings-body input:focus,
.ai-settings-body select:focus,
.ai-settings-body textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
    background: #fff;
}

.ai-settings-body textarea {
    resize: vertical;
    min-height: 70px;
}

.ai-settings-body input[type="range"] {
    padding: 0;
    border: none;
    box-shadow: none;
    background: transparent;
}

.ai-settings-actions {
    margin-top: 24px;
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}

.ai-settings-actions button {
    padding: 8px 20px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    cursor: pointer;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    transition: all .15s;
}

.ai-settings-actions button[type="submit"] {
    background: var(--primary);
    color: #fff;
    border: none;
}
.ai-settings-actions button[type="submit"]:hover {
    background: var(--primary-hover);
}

.ai-settings-loading {
    text-align: center;
    padding: 40px;
    color: var(--text-tertiary);
}

/* ========== 设置面板 - Tab 样式 ========== */
.ai-settings-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
    margin: -4px 0 16px;
    gap: 0;
}

.ai-tab {
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 500;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all .15s;
}

.ai-tab:hover {
    color: var(--text);
}

.ai-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.ai-tab-content {
    display: none;
}

.ai-tab-content.active {
    display: block;
}

/* ========== 服务商管理 ========== */
.ai-providers-toolbar {
    margin-bottom: 12px;
}

.ai-providers-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.ai-provider-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    gap: 12px;
}

.ai-provider-card.inactive {
    opacity: 0.55;
}

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

.ai-provider-name {
    font-size: 14px;
    font-weight: 500;
    color: var(--text);
    margin-bottom: 2px;
}

.ai-provider-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    font-size: 12px;
    color: var(--text-tertiary);
}

.ai-provider-type {
    background: var(--primary-light);
    color: var(--primary);
    padding: 1px 6px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 500;
}

.ai-provider-url {
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ai-provider-key {
    font-family: monospace;
    font-size: 11px;
}

.ai-provider-actions {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

.ai-provider-actions .btn-sm {
    padding: 4px 8px;
    font-size: 12px;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: var(--card-bg);
    color: var(--text-secondary);
    cursor: pointer;
    white-space: nowrap;
}

.ai-provider-actions .btn-sm:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.ai-provider-actions .btn-danger {
    color: var(--error);
    border-color: rgba(239,68,68,0.3);
}

.ai-provider-actions .btn-danger:hover {
    background: rgba(239,68,68,0.08);
    color: var(--error);
}

/* 服务商表单 */
.ai-provider-form {
    margin-top: 16px;
    padding: 16px;
    border: 1px solid var(--primary);
    border-radius: var(--radius-sm);
    background: var(--bg);
}

.ai-provider-form h4 {
    margin: 0 0 12px;
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
}

.ai-provider-form label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    margin-bottom: 4px;
    margin-top: 10px;
}

.ai-provider-form label:first-of-type {
    margin-top: 0;
}

.ai-provider-form input,
.ai-provider-form select {
    width: 100%;
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    outline: none;
    box-sizing: border-box;
    background: var(--card-bg);
    color: var(--text);
}

.ai-provider-form input:focus,
.ai-provider-form select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-light);
}

.ai-provider-form .ai-settings-actions {
    margin-top: 14px;
}

/* 设置面板关闭按钮 */
.ai-settings-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
    line-height: 1;
    padding: 0 4px;
}

.ai-settings-close:hover {
    color: var(--text);
}

/* optgroup 样式 */
.ai-model-select optgroup {
    font-weight: 600;
    font-size: 12px;
    color: var(--text-secondary);
}

.ai-model-select option {
    font-weight: normal;
    font-size: 13px;
    color: var(--text);
}

/* ========== 移动端响应式 ========== */
@media (max-width: 768px) {
    .ai-sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 300;
        transform: translateX(-100%);
        transition: transform .25s ease;
        box-shadow: var(--shadow-lg);
    }
    .ai-sidebar.open { transform: translateX(0); }
    .ai-msg { max-width: 95%; }
    .ai-input-wrapper { max-width: 100%; }
    .ai-settings-panel { max-width: 95vw; margin: 10px; }
    .ai-provider-card { flex-direction: column; align-items: flex-start; }
    .ai-provider-actions { width: 100%; justify-content: flex-end; margin-top: 8px; }
}
