/* ============================================================
   AI 助手样式 — 侧边栏推挤布局，使用 design tokens
   ============================================================ */

:root {
    --ai-sidebar-width: 380px;
}

/* ---- 推挤容器 (由 JS 动态注入包裹 body 直接子元素) ---- */
#appPushWrapper {
    min-height: 100vh;
    transition: margin-right 0.3s ease;
}

/* ---- 分栏模式 (≥768px): 内容压缩到左侧，侧边栏并排右侧 ---- */
@media (min-width: 768px) {
    body.ai-sidebar-active #appPushWrapper {
        margin-right: var(--ai-sidebar-width);
    }

    /* position:fixed 的 header/bottomMenu 需单独收窄右边界 */
    body.ai-sidebar-active .appHeader,
    body.ai-sidebar-active .appBottomMenu {
        right: var(--ai-sidebar-width);
    }

    /* appHeader:before 有 width:100% 需显式改为 auto 才能让 right 生效 */
    body.ai-sidebar-active .appHeader:before {
        right: var(--ai-sidebar-width);
        width: auto;
    }

    /* 分栏模式无需遮罩 */
    body.ai-sidebar-active .ai-overlay {
        display: none !important;
    }
}

/* ---- 小屏 (<768px): 遮罩模式，内容不缩窄 ---- */
@media (max-width: 767px) {
    body.ai-sidebar-active .ai-overlay {
        display: block;
    }
}

/* ---- 分栏动画：header/bottomMenu 随侧边栏开关平滑移动 ---- */
.appHeader,
.appHeader:before,
.appBottomMenu {
    transition: right 0.3s ease;
}

/* ---- 拖拽期间禁用所有过渡，避免 resize 延迟 ---- */
body.ai-drag-active * {
    transition: none !important;
}

/* ---- 侧边栏面板 ---- */
.ai-sidebar {
    position: fixed;
    top: 0;
    right: 0;
    width: var(--ai-sidebar-width);
    min-width: 380px;
    max-width: 85vw;
    height: 100vh;
    height: 100dvh;
    background: var(--bg-body);
    z-index: 1060;
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 0.3s ease;
    box-shadow: -2px 0 12px rgba(0, 0, 0, 0.08);
}

/* ---- 拖拽调整手柄 ---- */
.ai-sidebar-resize {
    position: absolute;
    top: 0;
    left: 0;
    width: 12px;
    height: 100%;
    cursor: col-resize;
    z-index: 1061;
    background: transparent;
    transition: background 0.2s;
    touch-action: none;
}

.ai-sidebar-resize:hover,
.ai-sidebar-resize.ai-sidebar-resize-active {
    background: var(--color-primary);
    opacity: 0.3;
}

.ai-sidebar.ai-sidebar-open {
    transform: translateX(0);
}

/* ---- 侧边栏头部 ---- */
.ai-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-sm) var(--space-md);
    background: var(--color-primary);
    color: #fff;
    flex-shrink: 0;
}

.ai-sidebar-header h5 {
    font-size: var(--font-lg);
    margin: 0;
    display: flex;
    align-items: center;
    gap: 6px;
}

.ai-sidebar-header-actions {
    display: flex;
    gap: var(--space-xs);
    align-items: center;
}

.ai-sidebar-header-actions button {
    background: transparent;
    border: none;
    color: #fff;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s;
}

.ai-sidebar-header-actions button:hover {
    background: rgba(255, 255, 255, 0.15);
}

.ai-sidebar-header-actions button ion-icon {
    font-size: 20px;
}

/* ---- 消息区 ---- */
.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-md);
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    -webkit-overflow-scrolling: touch;
}

/* ---- 对话气泡 ---- */
.ai-bubble {
    max-width: 88%;
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-lg);
    font-size: var(--font-base);
    line-height: 1.5;
    word-break: break-word;
}

.ai-bubble-user {
    align-self: flex-end;
    background: var(--color-primary);
    color: #fff;
    border-bottom-right-radius: var(--radius-sm);
}

.ai-bubble-assistant {
    align-self: flex-start;
    background: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--border-light);
    border-bottom-left-radius: var(--radius-sm);
}

.ai-bubble-assistant p {
    margin: 0 0 var(--space-xs);
}

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

.ai-bubble-assistant strong {
    font-weight: 600;
}

/* ---- 思考指示器 ---- */
.ai-bubble-thinking {
    align-self: flex-start;
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-bottom-left-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    color: var(--text-muted);
    font-size: var(--font-sm);
    padding: var(--space-sm) var(--space-md);
}

.ai-typing-dots {
    display: inline-flex;
    gap: 4px;
}

.ai-typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: var(--radius-full);
    background: var(--color-gray-400);
    animation: aiBounce 1.4s infinite ease-in-out;
}

.ai-typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.ai-typing-dots span:nth-child(2) { animation-delay: -0.16s; }
.ai-typing-dots span:nth-child(3) { animation-delay: 0s; }

@keyframes aiBounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* ---- 输入栏 ---- */
.ai-input-bar {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md);
    background: var(--bg-card);
    border-top: 1px solid var(--border-light);
    flex-shrink: 0;
}

.ai-input-bar input {
    flex: 1;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-full);
    padding: var(--space-sm) var(--space-md);
    font-size: var(--font-base);
    outline: none;
    transition: border-color 0.2s;
    min-width: 0;
}

.ai-input-bar input:focus {
    border-color: var(--color-primary);
}

.ai-input-bar button {
    width: 38px;
    height: 38px;
    border-radius: var(--radius-full);
    border: none;
    background: transparent;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
    -webkit-tap-highlight-color: transparent;
    flex-shrink: 0;
}

.ai-input-bar button:hover {
    background: var(--bg-hover);
}

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

.ai-input-bar button.ai-send-btn:hover {
    background: var(--color-primary-light);
}

.ai-input-bar button.ai-voice-active {
    color: var(--color-danger);
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.15); }
}

.ai-input-bar button ion-icon {
    font-size: 20px;
}

/* ---- FAB 悬浮按钮 ---- */
.ai-fab {
    position: fixed;
    bottom: calc(80px + env(safe-area-inset-bottom, 0px));
    right: var(--space-md);
    width: 56px;
    height: 56px;
    border-radius: var(--radius-full);
    border: none;
    background: linear-gradient(135deg, var(--color-primary), var(--color-primary-light));
    color: #fff;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-lg);
    cursor: pointer;
    z-index: 1055;
    transition: transform 0.2s, box-shadow 0.2s, right 0.3s ease;
    -webkit-tap-highlight-color: transparent;
}

.ai-fab:hover,
.ai-fab:active {
    transform: scale(1.1);
    box-shadow: 0 12px 20px -4px rgba(0, 61, 121, 0.3);
}

.ai-fab ion-icon {
    font-size: 26px;
}

/* 侧边栏打开时隐藏 FAB */
body.ai-sidebar-active .ai-fab {
    opacity: 0;
    pointer-events: none;
}

/* ---- 欢迎消息 ---- */
.ai-welcome {
    text-align: center;
    padding: var(--space-lg);
    color: var(--text-muted);
}

.ai-welcome p {
    font-size: var(--font-base);
    margin-bottom: var(--space-md);
}

.ai-welcome .ai-example {
    display: inline-block;
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-full);
    padding: var(--space-xs) var(--space-md);
    margin: var(--space-xs);
    font-size: var(--font-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s;
}

.ai-welcome .ai-example:hover {
    background: var(--bg-hover);
    border-color: var(--color-primary);
    color: var(--color-primary);
}

/* ---- 结果表格 ---- */
.ai-result-table {
    width: 100%;
    font-size: var(--font-sm);
    margin-top: var(--space-sm);
    border-collapse: collapse;
}

.ai-result-table th {
    background: var(--color-gray-100);
    padding: var(--space-xs) var(--space-sm);
    text-align: left;
    font-weight: 600;
    color: var(--text-secondary);
    border: 1px solid var(--border-default, #ccc);
    white-space: nowrap;
}

.ai-result-table td {
    padding: var(--space-xs) var(--space-sm);
    border: 1px solid var(--border-light);
    color: var(--text-primary);
}

/* ---- 状态徽章 ---- */
.ai-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: var(--radius-full);
    font-size: 10px;
    font-weight: 600;
}

.ai-badge-status-0 { background: var(--color-gray-200); color: var(--color-gray-700); }
.ai-badge-status-1 { background: #cce5ff; color: #004085; }
.ai-badge-status-2 { background: var(--color-warning-light); color: var(--color-warning); }
.ai-badge-status-3 { background: #e2d5f1; color: #563d7c; }
.ai-badge-status-4 { background: #d1ecf1; color: #0c5460; }
.ai-badge-status-5 { background: #fff3cd; color: #856404; }
.ai-badge-status-6 { background: #f5c6cb; color: #721c24; }
.ai-badge-status-7 { background: var(--color-success-light); color: var(--color-success); }
.ai-badge-status-8 { background: #ffeaa7; color: #e17055; }
.ai-badge-status-9 { background: var(--color-success-light); color: var(--color-success); }

/* ---- 遮罩层 ---- */
.ai-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    z-index: 1058;
    transition: opacity 0.3s ease;
}

body.ai-sidebar-active .ai-overlay {
    display: block;
}

/* overlay 模式：所有屏幕尺寸均显示遮罩 */

/* ---- 推理过程折叠块 ---- */
.ai-reasoning-block {
    margin-bottom: var(--space-xs);
    border: 1px dashed var(--border-light);
    border-radius: var(--radius-sm);
    background: var(--color-gray-50, #f8f9fa);
    font-size: var(--font-sm);
}

.ai-reasoning-summary {
    cursor: pointer;
    padding: var(--space-xs) var(--space-sm);
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 4px;
    user-select: none;
}

.ai-reasoning-summary::-webkit-details-marker {
    display: none;
}

.ai-reasoning-content {
    padding: 0 var(--space-sm) var(--space-sm);
    color: var(--text-secondary);
    max-height: 300px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.4;
}

/* ---- Tool 执行状态 ---- */
.ai-tool-status {
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-sm);
    color: var(--text-muted);
    font-size: var(--font-sm);
    font-style: italic;
}

.ai-tool-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid var(--color-gray-300, #ddd);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: aiSpin 0.8s linear infinite;
    flex-shrink: 0;
}

@keyframes aiSpin {
    to { transform: rotate(360deg); }
}

/* ---- 流式打字光标 ---- */
.ai-streaming-active .ai-stream-content::after {
    content: '\25CB';
    animation: aiCursor 1s step-end infinite;
    color: var(--color-primary);
    font-weight: normal;
    margin-left: 1px;
}

@keyframes aiCursor {
    50% { opacity: 0; }
}

/* ---- 引用块 ---- */
.ai-bubble-assistant blockquote {
    margin: var(--space-xs) 0;
    padding: var(--space-xs) var(--space-md);
    border-left: 3px solid var(--color-primary);
    background: var(--color-gray-50, #f8f9fa);
    color: var(--text-secondary);
    font-size: var(--font-sm);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.ai-bubble-assistant blockquote p {
    margin: 0;
}

/* ---- 行内代码 ---- */
.ai-bubble-assistant code {
    background: var(--color-gray-100);
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 0.9em;
    color: #c7254e;
    font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

/* ---- 导航跳转按钮 ---- */
.ai-nav-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 5px 12px;
    background: var(--color-primary, #003d79);
    color: #fff !important;
    border-radius: 20px;
    font-size: var(--font-sm);
    font-weight: 500;
    text-decoration: none !important;
    margin: 6px 0 2px;
    transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
    vertical-align: middle;
    cursor: pointer;
    box-shadow: 0 1px 4px rgba(0,61,121,0.18);
}
.ai-nav-link:hover {
    background: #002a55;
    transform: translateY(-1px);
    box-shadow: 0 3px 8px rgba(0,61,121,0.25);
    color: #fff !important;
    text-decoration: none !important;
}
.ai-nav-link ion-icon {
    font-size: 15px;
    flex-shrink: 0;
}

/* ---- 自动跳转提示 ---- */
.ai-nav-jumping {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    margin-top: 8px;
    font-size: var(--font-sm);
    color: var(--text-secondary);
    animation: ai-nav-blink 0.8s ease-in-out infinite;
}
@keyframes ai-nav-blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0.4; }
}
