/* ===== 基础与变量 ===== */
:root {
    --bg-chat: #f5f5f5;
    --user-bubble: #95ec69;
    --ai-bubble: #ffffff;
    --primary: #1890ff;
    --primary-hover: #40a9ff;
    --danger: #ff4d4f;
    --success: #52c41a;
    --warning: #faad14;
    --sidebar-width: 280px;
    --topbar-height: 56px;
    --text-color: #333;
    --text-secondary: #666;
    --text-muted: #999;
    --border-color: #e5e5e5;
    --border-light: #f0f0f0;
    --radius: 8px;
    --radius-sm: 4px;
    --radius-lg: 12px;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.08);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
    --shadow-overlay: rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    color: var(--text-color);
}

/* ===== 动画 ===== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}
@keyframes scaleIn {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
}
@keyframes dotPulse {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}
@keyframes pulseRed {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}
@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
}

.animate-fade-in { animation: fadeIn 0.25s ease-out both; }
.animate-scale-in { animation: scaleIn 0.25s ease-out both; }

.loading-dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #999;
    animation: dotPulse 1.4s infinite ease-in-out both;
}
.loading-dot:nth-child(1) { animation-delay: -0.32s; }
.loading-dot:nth-child(2) { animation-delay: -0.16s; }

/* ===== 布局 ===== */
.app-container {
    display: flex;
    height: 100vh;
    flex-direction: column;
}
.main-area {
    display: flex;
    flex: 1;
    overflow: hidden;
    position: relative;
}
.sidebar {
    width: var(--sidebar-width);
    background: #fff;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: transform 0.3s ease;
}
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-chat);
    position: relative;
}

/* ================================================================
   移动端适配 (≤767px)
   ================================================================ */
@media (max-width: 767px) {
    /* 侧边栏 */
    .sidebar {
        position: absolute;
        top: 0; bottom: 0; left: 0;
        z-index: 50;
        width: 260px;
        transform: translateX(-100%);
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-overlay {
        display: none;
        position: absolute;
        inset: 0;
        background: var(--shadow-overlay);
        z-index: 40;
    }
    .sidebar-overlay.open { display: block; }

    /* 顶部栏 */
    .top-bar {
        padding: 0 8px;
        height: 48px;
    }
    .logo { font-size: 15px; gap: 4px; }
    .export-btn {
        padding: 4px 8px;
        font-size: 12px;
        border-radius: 4px;
    }

    /* 消息列表 */
    .msg-list { padding: 10px 8px; gap: 8px; }
    .msg-row { max-width: 92%; }
    .msg-bubble { font-size: 15px; padding: 8px 12px; }
    .ai-avatar { width: 32px; height: 32px; font-size: 18px; flex-shrink: 0; }

    /* 确认卡片 */
    .confirm-card {
        max-width: calc(100vw - 24px);
        min-width: auto;
        padding: 12px;
    }
    .card-row { font-size: 13px; }
    .card-input { width: 80px; font-size: 13px; }
    .card-btn { padding: 8px 14px; font-size: 14px; min-height: 40px; }

    /* 输入区 */
    .input-area { padding: 8px 8px; }
    .quick-tags { gap: 6px; margin-bottom: 6px; }
    .tag-btn { padding: 6px 12px; font-size: 13px; min-height: 36px; }
    .input-row { gap: 6px; }
    .msg-input {
        font-size: 16px; /* 防止 iOS 自动缩放 */
        padding: 8px 10px;
        min-height: 40px;
    }
    .send-btn { padding: 8px 14px; font-size: 14px; min-height: 40px; }
    .voice-btn { width: 40px; height: 40px; font-size: 18px; }

    /* 弹窗通用 */
    .modal-content {
        max-width: 96vw;
        padding: 16px 12px;
        border-radius: 10px;
    }
    .modal-close {
        width: 36px;
        height: 36px;
        top: 8px;
        right: 8px;
    }

    /* 确认卡片（交易记录） */
    .tx-record {
        max-width: calc(100vw - 24px);
        font-size: 13px;
    }
    .tx-record .tx-actions {
        display: flex;
        top: 4px;
        right: 4px;
    }
    .tx-action-btn {
        width: 32px;
        height: 32px;
    }

    /* 客户列表 */
    .customer-item { padding: 14px 12px; }
    .customer-name { font-size: 15px; }
    .customer-balance { font-size: 13px; }
    .customer-time { font-size: 11px; }
    .sidebar-header { padding: 10px 12px; }
    .search-box { font-size: 16px; padding: 10px 12px; }
    .sidebar-footer { padding: 10px 12px; font-size: 14px; }

    /* 对账单弹窗 */
    #billCanvas {
        width: 100% !important;
        height: auto !important;
    }

    /* ===== 账单总览表格（重点） ===== */
    .bill-table-modal {
        max-width: 98vw;
        width: 98vw;
        padding: 12px 8px;
    }
    .bill-filters {
        gap: 6px;
        margin-bottom: 10px;
    }
    .bill-filter-select {
        padding: 8px 10px;
        font-size: 14px;
        min-width: 0;
        flex: 1 1 calc(50% - 6px);
    }
    .bill-search-input {
        padding: 8px 10px;
        font-size: 14px;
        flex: 1 1 100%;
        min-width: 0;
    }
    .bill-stats {
        font-size: 12px;
        padding: 8px 10px;
        flex-wrap: wrap;
    }
    /* 表格横向滚动 */
    .bill-table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-height: 45vh;
    }
    .bill-table {
        min-width: 620px;
        font-size: 13px;
    }
    .bill-table th { padding: 8px 8px; font-size: 12px; }
    .bill-table td { padding: 8px 8px; font-size: 13px; }
    .bill-time { font-size: 12px; }
    .bill-amount { font-size: 13px; }
    .bill-action-btn {
        width: 32px;
        height: 32px;
        font-size: 14px;
    }
    .bill-edit-input { font-size: 14px; padding: 6px 8px; width: 60px !important; }
    .bill-edit-select { font-size: 14px; padding: 6px 4px; }
    .bill-footer-actions {
        gap: 6px;
        margin-top: 12px;
    }
    .bill-footer-actions .export-btn {
        font-size: 13px;
        padding: 8px 12px;
        min-height: 40px;
    }

    /* 催款话术 */
    .reminder-text { font-size: 14px; padding: 12px; }

    /* AI 设置弹窗 */
    #aiSettingsModal .modal-content { max-width: 96vw !important; padding: 16px 12px; }
    .provider-btn { padding: 8px 14px; font-size: 13px; min-height: 38px; }
    .form-input { font-size: 16px; padding: 10px 12px; }

    /* 提示框 */
    .tip-box { font-size: 12px; padding: 10px; }

    /* 滚动条在移动端更细 */
    ::-webkit-scrollbar { width: 3px; height: 3px; }
}
@media (min-width: 768px) {
    .sidebar-overlay { display: none !important; }
    .mobile-menu-btn { display: none !important; }
}

/* ===== 顶部栏 ===== */
.top-bar {
    height: var(--topbar-height);
    background: #fff;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    z-index: 30;
    flex-shrink: 0;
}
.logo {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-color);
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ===== 消息列表 ===== */
.msg-list {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.msg-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 85%;
}
.msg-row.user { align-self: flex-end; flex-direction: row-reverse; }
.msg-row.ai { align-self: flex-start; }
.msg-row.system { align-self: center; max-width: 90%; }
.msg-row.card { align-self: flex-start; }
.msg-bubble {
    padding: 10px 14px;
    border-radius: var(--radius);
    line-height: 1.5;
    word-break: break-word;
}
.msg-row.user .msg-bubble { background: var(--user-bubble); }
.msg-row.ai .msg-bubble {
    background: var(--ai-bubble);
    box-shadow: var(--shadow-sm);
}
.msg-row.system .msg-bubble {
    background: transparent;
    color: #888;
    font-size: 14px;
    text-align: center;
}
.ai-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #e6f7ff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}

/* ===== 确认卡片 ===== */
.confirm-card {
    background: #fff;
    border-left: 4px solid var(--primary);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: 16px;
    min-width: 280px;
    max-width: 380px;
    transition: opacity 0.3s;
}
.confirm-card.readonly {
    border-left-color: var(--success);
    opacity: 0.9;
}
.confirm-card .card-title {
    font-weight: 600;
    margin-bottom: 8px;
    font-size: 15px;
}
.card-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 0;
    font-size: 14px;
}
.card-label { color: var(--text-secondary); }
.card-value { font-weight: 500; }
.card-input {
    border: 1px solid #d9d9d9;
    border-radius: var(--radius-sm);
    padding: 4px 8px;
    font-size: 14px;
    width: 100px;
    text-align: right;
}
.card-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}
.card-actions { display: flex; gap: 8px; margin-top: 12px; }
.card-actions-wrap { min-height: 36px; }

/* 交易记录卡片 (已确认的历史记录) */
.tx-record {
    background: #fff;
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 12px 14px;
    max-width: 380px;
    font-size: 14px;
    position: relative;
    transition: box-shadow 0.2s;
}
.tx-record:hover { box-shadow: var(--shadow-md); }
.tx-record .tx-actions {
    display: none;
    gap: 4px;
    position: absolute;
    top: 8px;
    right: 8px;
}
.tx-record:hover .tx-actions { display: flex; }
@media (hover: none) {
    .tx-record .tx-actions { display: flex; }
}
.tx-action-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid #d9d9d9;
    background: #fff;
    cursor: pointer;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.tx-action-btn.edit:hover { background: #e6f7ff; border-color: var(--primary); }
.tx-action-btn.delete:hover { background: #fff1f0; border-color: var(--danger); color: var(--danger); }

/* ===== 按钮 ===== */
.card-btn {
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    cursor: pointer;
    border: none;
    transition: all 0.2s;
}
.card-btn-primary { background: var(--primary); color: #fff; }
.card-btn-primary:hover { background: var(--primary-hover); }
.card-btn-default { background: #fff; border: 1px solid #d9d9d9; color: var(--text-color); }
.card-btn-default:hover { border-color: var(--primary); color: var(--primary); }
.card-btn-danger { background: #fff; border: 1px solid var(--danger); color: var(--danger); }
.card-btn-danger:hover { background: #fff1f0; }

/* ===== 输入区 ===== */
.input-area {
    background: #f7f7f7;
    border-top: 1px solid var(--border-color);
    padding: 12px 16px;
}
.quick-tags { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.tag-btn {
    padding: 4px 10px;
    border-radius: 12px;
    background: #e8e8e8;
    color: #555;
    font-size: 13px;
    cursor: pointer;
    border: none;
    transition: all 0.2s;
}
.tag-btn:hover { background: #d0d0d0; }
.input-row { display: flex; gap: 8px; align-items: flex-end; }
.msg-input {
    flex: 1;
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    padding: 10px 12px;
    font-size: 15px;
    resize: none;
    max-height: 120px;
    min-height: 44px;
    font-family: inherit;
    line-height: 1.4;
}
.msg-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.15);
}
.send-btn {
    padding: 10px 20px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 15px;
    cursor: pointer;
    transition: background 0.2s;
    white-space: nowrap;
}
.send-btn:hover { background: var(--primary-hover); }
.send-btn:disabled { background: #bfbfbf; cursor: not-allowed; }

/* ===== 语音按钮 ===== */
.voice-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: #fff;
    border: 1px solid #d9d9d9;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    cursor: pointer;
    transition: all 0.2s;
    flex-shrink: 0;
}
.voice-btn:hover { background: #e6f7ff; border-color: var(--primary); }
.voice-btn.recording {
    background: var(--danger);
    color: #fff;
    border-color: var(--danger);
    animation: pulseRed 1s infinite;
}

/* ===== 侧边栏 ===== */
.sidebar-header { padding: 12px 16px; border-bottom: 1px solid var(--border-light); }
.search-box {
    width: 100%;
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    padding: 8px 12px;
    font-size: 14px;
}
.search-box:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.15);
}
.customer-list { flex: 1; overflow-y: auto; }
.customer-item {
    padding: 12px 16px;
    border-bottom: 1px solid #f5f5f5;
    cursor: pointer;
    transition: background 0.15s;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.customer-item:hover, .customer-item.active { background: #e6f7ff; }
.remind-btn:hover { background: #fff1f0 !important; border-color: #ff4d4f !important; }
.customer-name { font-weight: 500; }
.customer-meta { text-align: right; }
.customer-balance { font-size: 14px; font-weight: 600; }
.customer-balance.owed { color: var(--danger); }
.customer-balance.paid { color: var(--success); }
.customer-time { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--border-light);
    font-weight: 600;
    font-size: 15px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.sidebar-summary { display: flex; justify-content: space-between; }
.total-owed { color: var(--danger); }
.backup-btn {
    width: 100%;
    padding: 8px;
    border: 1px dashed #d9d9d9;
    background: #fafafa;
    border-radius: var(--radius-sm);
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
}
.backup-btn:hover { border-color: var(--primary); color: var(--primary); background: #e6f7ff; }

/* ===== 顶部按钮 ===== */
.export-btn {
    padding: 6px 14px;
    border: 1px solid #d9d9d9;
    background: #fff;
    border-radius: var(--radius-sm);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
}
.export-btn:hover { border-color: var(--primary); color: var(--primary); }

/* ===== 空状态 ===== */
.empty-state { text-align: center; color: #aaa; padding: 40px 20px; }
.empty-state-icon { font-size: 48px; margin-bottom: 12px; }

/* ===== 催款标记 ===== */
.overdue-badge {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--danger);
    margin-left: 4px;
    animation: blink 1.5s infinite;
}

/* ===== 弹窗 ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: var(--shadow-overlay);
    z-index: 100;
    display: none;
    align-items: center;
    justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal-content {
    background: #fff;
    border-radius: var(--radius-lg);
    max-width: 90vw;
    max-height: 90vh;
    overflow: auto;
    padding: 24px;
    position: relative;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    animation: scaleIn 0.2s ease-out both;
}
.modal-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: none;
    background: #f5f5f5;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.modal-close:hover { background: #e0e0e0; }
.bill-canvas { border: 1px solid var(--border-color); border-radius: var(--radius); }
.bill-actions, .modal-actions { display: flex; gap: 12px; margin-top: 16px; justify-content: center; }

/* ===== 催款话术 ===== */
.reminder-text {
    background: #f6ffed;
    border: 1px solid #b7eb8f;
    border-radius: var(--radius);
    padding: 16px;
    font-size: 15px;
    line-height: 1.6;
    color: var(--text-color);
}

/* ===== AI 设置表单 ===== */
.form-group { margin-bottom: 12px; }
.form-label { display: block; font-size: 14px; color: var(--text-secondary); margin-bottom: 6px; }
.form-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    font-size: 14px;
    transition: border-color 0.2s;
}
.form-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.15);
}
.provider-btn {
    padding: 6px 12px;
    border-radius: 20px;
    border: 2px solid transparent;
    background: #f0f0f0;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
}
.provider-btn.active { border-color: var(--primary); background: #e6f7ff; color: var(--primary); }

/* ===== 提示信息 ===== */
.tip-box {
    padding: 12px;
    border-radius: 6px;
    font-size: 13px;
    line-height: 1.6;
    margin-bottom: 12px;
}
.tip-warning { background: #fff7e6; color: #666; border: 1px solid #ffe58f; }
.tip-info { background: #e6f7ff; color: var(--text-color); border: 1px solid #91d5ff; }
.tip-success { background: #f6ffed; color: var(--text-color); border: 1px solid #b7eb8f; }

/* ===== 账单总览表格 ===== */
.bill-table-modal { max-width: 95vw; width: 900px; padding: 20px; }
.bill-filters {
    display: flex;
    gap: 8px;
    margin-bottom: 14px;
    flex-wrap: wrap;
    align-items: center;
}
.bill-filter-select {
    padding: 6px 10px;
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    font-size: 13px;
    background: #fff;
    cursor: pointer;
    min-width: 90px;
    transition: border-color 0.2s;
}
.bill-filter-select:focus { outline: none; border-color: var(--primary); }
.bill-search-input {
    padding: 6px 10px;
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    font-size: 13px;
    flex: 1;
    min-width: 140px;
    transition: border-color 0.2s;
}
.bill-search-input:focus { outline: none; border-color: var(--primary); }
.bill-stats {
    padding: 10px 14px;
    background: #fafafa;
    border-radius: 8px;
    margin-bottom: 12px;
    font-size: 14px;
    font-weight: 500;
    color: #333;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    border: 1px solid #f0f0f0;
}
.bill-table-wrap {
    max-height: 55vh;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 8px;
}
.bill-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}
.bill-table thead {
    position: sticky;
    top: 0;
    z-index: 2;
    background: #fafafa;
}
.bill-table th {
    padding: 10px 12px;
    text-align: left;
    font-weight: 600;
    color: #555;
    font-size: 13px;
    border-bottom: 2px solid #e5e5e5;
    white-space: nowrap;
}
.bill-table td {
    padding: 10px 12px;
    border-bottom: 1px solid #f5f5f5;
    vertical-align: middle;
}
.bill-row { transition: background 0.15s; }
.bill-row:hover { background: #fafafa; }
.bill-row.editing { background: #fffbe6; }
.bill-time { color: #888; font-size: 13px; white-space: nowrap; }
.bill-amount { font-weight: 600; white-space: nowrap; }
.bill-tag {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 12px;
    background: #f0f0f0;
    color: #666;
}
.bill-tag-payment { background: #e6f7ff; color: #1890ff; }
.bill-status-owed { color: var(--danger); font-weight: 500; font-size: 13px; }
.bill-status-paid { color: var(--success); font-weight: 500; font-size: 13px; }
.bill-actions { white-space: nowrap; }
.bill-action-btn {
    width: 28px;
    height: 28px;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    background: #fff;
    cursor: pointer;
    font-size: 13px;
    padding: 0;
    transition: all 0.15s;
    vertical-align: middle;
}
.bill-action-btn.edit:hover { background: #e6f7ff; border-color: var(--primary); }
.bill-action-btn.delete:hover { background: #fff1f0; border-color: var(--danger); color: var(--danger); }
.bill-action-btn.save { background: #f6ffed; border-color: #b7eb8f; color: var(--success); }
.bill-action-btn.save:hover { background: #d9f7be; }
.bill-action-btn.cancel { background: #fff2f0; border-color: #ffccc7; color: var(--danger); }
.bill-action-btn.cancel:hover { background: #ffd8d2; }
.bill-edit-input {
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    padding: 4px 6px;
    font-size: 13px;
    transition: border-color 0.2s;
}
.bill-edit-input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px rgba(24,144,255,0.15); }
.bill-edit-select {
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    padding: 4px 6px;
    font-size: 13px;
    background: #fff;
}
.bill-footer-actions {
    display: flex;
    gap: 10px;
    margin-top: 16px;
    justify-content: center;
    flex-wrap: wrap;
}

/* ===== 滚动条 ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #aaa; }
