/* ============ 后台管理系统样式 ============ */
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
    background: #f0f2f5;
    color: #1f2937;
    min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
input, textarea, select, button {
    font-family: inherit;
    font-size: 14px;
}

/* ============ 登录页 ============ */
.admin-login {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #2b7cff 0%, #1a5bd9 100%);
}
.login-card {
    background: #fff;
    padding: 48px 40px;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.2);
    width: 380px;
    max-width: 92vw;
}
.login-card h1 {
    text-align: center;
    margin-bottom: 8px;
    font-size: 22px;
    font-weight: 700;
    color: #1f2937;
}
.login-card .sub {
    text-align: center;
    color: #9ca3af;
    font-size: 13px;
    margin-bottom: 32px;
}
.form-group { margin-bottom: 20px; }
.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: #374151;
    margin-bottom: 8px;
}
.form-group input,
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    outline: none;
    transition: border-color 0.2s;
    background: #fff;
}
.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    border-color: #2b7cff;
    box-shadow: 0 0 0 3px rgba(43,124,255,0.1);
}
.form-group textarea { resize: vertical; min-height: 80px; }

.btn {
    padding: 10px 20px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}
.btn-primary {
    background: linear-gradient(135deg, #2b7cff 0%, #1a5bd9 100%);
    color: #fff;
    width: 100%;
    padding: 12px;
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(43,124,255,0.4); }
.btn-secondary {
    background: #f3f4f6;
    color: #374151;
}
.btn-secondary:hover { background: #e5e7eb; }
.btn-danger { background: #fee2e2; color: #dc2626; }
.btn-danger:hover { background: #fecaca; }
.btn-success { background: #d1fae5; color: #059669; }
.btn-small { padding: 6px 12px; font-size: 13px; }

/* ============ 主布局 ============ */
.admin-layout {
    display: flex;
    min-height: 100vh;
}
.admin-sidebar {
    width: 220px;
    background: #1a2236;
    color: #cbd5e1;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}
.sidebar-logo {
    padding: 24px 20px;
    font-size: 18px;
    font-weight: 700;
    color: #fff;
    border-bottom: 1px solid rgba(255,255,255,0.08);
}
.sidebar-logo span { color: #2b7cff; }
.sidebar-menu { flex: 1; padding: 16px 0; }
.sidebar-menu a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 20px;
    color: #cbd5e1;
    font-size: 14px;
    border-left: 3px solid transparent;
    transition: all 0.2s;
}
.sidebar-menu a:hover { background: rgba(255,255,255,0.05); color: #fff; }
.sidebar-menu a.active {
    background: rgba(43,124,255,0.15);
    color: #fff;
    border-left-color: #2b7cff;
}
.sidebar-menu a .icon {
    width: 20px;
    text-align: center;
}
.sidebar-footer {
    padding: 16px 20px;
    font-size: 12px;
    color: #64748b;
    border-top: 1px solid rgba(255,255,255,0.08);
}

.admin-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.admin-topbar {
    background: #fff;
    padding: 14px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #f0f0f0;
}
.topbar-title { font-size: 18px; font-weight: 600; }
.topbar-right { display: flex; align-items: center; gap: 16px; font-size: 14px; color: #6b7280; }
.topbar-right .user-info { color: #1f2937; font-weight: 500; }
.topbar-right a { color: #2b7cff; cursor: pointer; }

.admin-content {
    flex: 1;
    padding: 24px;
    overflow-x: auto;
}

/* ============ 卡片 ============ */
.card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    margin-bottom: 20px;
}
.card-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

/* ============ 统计卡片 ============ */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}
.stat-card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    position: relative;
    overflow: hidden;
}
.stat-card .label { font-size: 13px; color: #6b7280; margin-bottom: 8px; }
.stat-card .value { font-size: 28px; font-weight: 700; color: #1f2937; }
.stat-card .icon-big {
    position: absolute;
    right: 16px;
    top: 16px;
    width: 44px; height: 44px;
    border-radius: 12px;
    background: #e1ecff;
    color: #2b7cff;
    display: flex; align-items: center; justify-content: center;
    font-size: 22px;
}
.stat-card.blue .icon-big { background: #e1ecff; color: #2b7cff; }
.stat-card.green .icon-big { background: #d1fae5; color: #059669; }
.stat-card.orange .icon-big { background: #fed7aa; color: #ea580c; }
.stat-card.purple .icon-big { background: #ede9fe; color: #7c3aed; }

/* ============ 表格 ============ */
table.admin-table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    border-radius: 12px;
    overflow: hidden;
}
table.admin-table th {
    background: #f9fafb;
    text-align: left;
    padding: 12px 16px;
    font-size: 13px;
    font-weight: 600;
    color: #374151;
    border-bottom: 1px solid #f0f0f0;
}
table.admin-table td {
    padding: 12px 16px;
    border-bottom: 1px solid #f9fafb;
    font-size: 14px;
    color: #4b5563;
}
table.admin-table tr:hover td { background: #f9fafb; }
.badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}
.badge-green { background: #d1fae5; color: #059669; }
.badge-gray { background: #f3f4f6; color: #6b7280; }
.badge-blue { background: #dbeafe; color: #2563eb; }
.badge-red { background: #fee2e2; color: #dc2626; }

/* ============ 模态框 ============ */
.modal-mask {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}
.modal {
    background: #fff;
    border-radius: 16px;
    width: 520px;
    max-width: 92vw;
    max-height: 85vh;
    overflow-y: auto;
}
.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.modal-header h3 { font-size: 16px; font-weight: 600; }
.modal-close {
    width: 28px; height: 28px;
    border-radius: 50%;
    border: none;
    background: #f3f4f6;
    cursor: pointer;
    font-size: 16px;
}
.modal-body { padding: 20px; }
.modal-footer {
    padding: 14px 20px;
    border-top: 1px solid #f0f0f0;
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

/* ============ 聊天管理 ============ */
.conv-layout {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 16px;
    height: calc(100vh - 200px);
}
.conv-list {
    background: #fff;
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.conv-list-head {
    padding: 14px 16px;
    border-bottom: 1px solid #f0f0f0;
    font-weight: 600;
    background: #f9fafb;
}
.conv-list-search {
    padding: 10px 12px;
    border-bottom: 1px solid #f0f0f0;
}
.conv-list-search input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    outline: none;
}
.conv-list-search input:focus { border-color: #2b7cff; }
.conv-items { flex: 1; overflow-y: auto; }
.conv-item {
    padding: 12px 16px;
    border-bottom: 1px solid #f9fafb;
    cursor: pointer;
    display: flex;
    gap: 12px;
    transition: background 0.2s;
}
.conv-item:hover { background: #f9fafb; }
.conv-item.active { background: #e1ecff; border-left: 3px solid #2b7cff; padding-left: 13px; }
.conv-item .c-avatar {
    width: 42px; height: 42px;
    border-radius: 50%;
    background: #e1ecff;
    background-size: cover;
    background-position: center;
    flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    color: #2b7cff; font-weight: 600;
    font-size: 16px;
}
.conv-item .c-info { flex: 1; min-width: 0; }
.conv-item .c-name {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 2px;
}
.conv-item .c-name span { font-weight: 600; font-size: 14px; }
.conv-item .c-time { font-size: 11px; color: #9ca3af; }
.conv-item .c-last {
    font-size: 13px; color: #6b7280;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item .c-unread {
    background: #ff4d4f;
    color: #fff;
    min-width: 18px; height: 18px;
    border-radius: 9px;
    font-size: 11px;
    padding: 0 5px;
    display: flex; align-items: center; justify-content: center;
    margin-top: 4px;
}

/* 聊天区 */
.chat-area {
    background: #fff;
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.chat-area-head {
    padding: 14px 20px;
    border-bottom: 1px solid #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #f9fafb;
}
.chat-area-head .agent-name { font-weight: 600; }
.chat-area-head .conv-actions { display: flex; gap: 10px; }

.chat-msg-area {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    background: #f7f9fc;
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.chat-msg {
    display: flex; gap: 10px; max-width: 75%;
}
.chat-msg.visitor { align-self: flex-end; flex-direction: row-reverse; }
.chat-msg.system { align-self: center; max-width: 90%; }
.chat-msg .m-avatar {
    width: 36px; height: 36px;
    border-radius: 50%;
    background: #2b7cff;
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 13px; font-weight: 600;
    flex-shrink: 0;
    background-size: cover; background-position: center;
}
.chat-msg.visitor .m-avatar { background: #10b981; }
.chat-msg .m-content {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.5;
    box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}
.chat-msg.agent .m-content { background: #fff; border-top-left-radius: 4px; }
.chat-msg.visitor .m-content { background: #2b7cff; color: #fff; border-top-right-radius: 4px; }
.chat-msg.system .m-content {
    background: rgba(0,0,0,0.05);
    color: #9ca3af;
    font-size: 12px;
    padding: 6px 12px;
    border-radius: 10px;
    box-shadow: none;
}
.chat-input-area {
    padding: 14px 20px;
    border-top: 1px solid #f0f0f0;
    display: flex;
    gap: 10px;
    align-items: flex-end;
}
.chat-input-area textarea {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    resize: none;
    min-height: 44px;
    max-height: 150px;
    outline: none;
}
.chat-input-area textarea:focus { border-color: #2b7cff; box-shadow: 0 0 0 3px rgba(43,124,255,0.1); }
.chat-input-area .send-btn {
    padding: 12px 28px;
    background: linear-gradient(135deg, #2b7cff 0%, #1a5bd9 100%);
    color: #fff;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 600;
}
.chat-input-area .send-btn:hover { transform: scale(1.03); }

/* 空状态 */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: #9ca3af;
}
.empty-state .icon { font-size: 48px; margin-bottom: 16px; }

/* ============ 图表占位 ============ */
.chart-placeholder {
    width: 100%;
    height: 220px;
    background: #f9fafb;
    border-radius: 8px;
    display: flex;
    align-items: flex-end;
    padding: 12px;
    gap: 8px;
}
.chart-placeholder .bar {
    flex: 1;
    background: linear-gradient(180deg, #2b7cff 0%, #1a5bd9 100%);
    border-radius: 4px 4px 0 0;
    position: relative;
}
.chart-placeholder .bar::after {
    content: attr(data-h);
    position: absolute;
    top: -20px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 11px;
    color: #6b7280;
}

/* 移动端抽屉菜单（桌面隐藏） */
.mobile-nav-btn { display: none; }
.mobile-nav-mask { display: none; }
.mobile-back-btn { display: none; }

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

/* ============ 响应式 ============ */
@media (max-width: 900px) {
    .conv-layout { grid-template-columns: 1fr; height: auto; }
    .conv-list { height: 320px; }
}

@media (max-width: 768px) {
    html { -webkit-text-size-adjust: 100%; }

    /* 侧栏改为抽屉，点击左上角菜单按钮展开 */
    .admin-sidebar {
        display: flex;
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: 250px;
        max-width: 82vw;
        z-index: 900;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        overflow-y: auto;
    }
    .admin-layout.nav-open .admin-sidebar {
        transform: translateX(0);
        box-shadow: 0 0 30px rgba(15,23,42,0.35);
    }
    .admin-layout.nav-open .mobile-nav-mask {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(15,23,42,0.5);
        z-index: 800;
    }
    .mobile-nav-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 38px;
        height: 38px;
        flex: none;
        border: none;
        border-radius: 10px;
        background: #f1f5f9;
        color: #1f2937;
        font-size: 18px;
        line-height: 1;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    /* 顶栏：标题一行，操作按钮一行，避免被挤压 */
    .admin-topbar {
        position: sticky;
        top: 0;
        z-index: 60;
        padding: 10px 12px;
        gap: 10px;
        flex-wrap: wrap;
        align-items: center;
    }
    .topbar-title {
        flex: 1;
        min-width: 0;
        font-size: 16px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .topbar-right {
        width: 100%;
        justify-content: flex-end;
        gap: 10px;
        font-size: 13px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 2px;
    }
    .topbar-right > span:first-child { display: none; } /* 隐藏问候语，给安装/推送按钮留空间 */
    .topbar-right > * { flex: none; }

    .admin-content {
        padding: 12px;
        overflow-x: hidden;
    }
    .card { padding: 14px; border-radius: 10px; margin-bottom: 12px; }
    .card-title {
        font-size: 15px;
        margin-bottom: 12px;
        gap: 8px;
        flex-wrap: wrap;
    }
    .form-row { grid-template-columns: 1fr; gap: 0; }
    .form-group { margin-bottom: 16px; }
    .form-group input,
    .form-group textarea,
    .form-group select,
    .conv-list-search input,
    select,
    input[type="text"],
    input[type="number"] {
        font-size: 16px; /* 防止 iOS 聚焦输入框时页面放大 */
    }
    .btn { padding: 9px 16px; }

    .stat-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
        margin-bottom: 12px;
    }
    .stat-card { padding: 14px; }
    .stat-card .label { font-size: 12px; margin-bottom: 6px; }
    .stat-card .value { font-size: 22px; }
    .stat-card .icon-big {
        width: 34px;
        height: 34px;
        font-size: 17px;
        right: 10px;
        top: 12px;
    }

    /* 表格改为横向滚动，不再压缩列宽 */
    table.admin-table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        font-size: 13px;
    }
    table.admin-table th,
    table.admin-table td { padding: 10px 12px; }

    /* 会话：先显示列表，选中后显示聊天，左上角返回 */
    .conv-layout { display: block; height: auto; }
    .conv-list { height: calc(100vh - 170px); max-height: none; }
    .conv-layout .chat-area { display: none; }
    .conv-layout.mobile-chat-open .conv-list { display: none; }
    .conv-layout.mobile-chat-open .chat-area {
        display: flex;
        height: calc(100vh - 150px);
    }
    .chat-area-head { padding: 10px 12px; gap: 8px; flex-wrap: wrap; }
    .chat-msg-area { padding: 12px; }
    .chat-msg { max-width: 92%; }
    .chat-input-area { padding: 10px 12px; }
    .chat-input-area .send-btn { padding: 12px 16px; }
    .mobile-back-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 30px;
        height: 30px;
        flex: none;
        border: none;
        border-radius: 8px;
        background: #f1f5f9;
        color: #374151;
        font-size: 18px;
        line-height: 1;
        cursor: pointer;
    }

    .modal { max-width: 94vw; max-height: 88vh; }
    .modal-header,
    .modal-body,
    .modal-footer { padding: 14px; }

    /* 总后台会话弹层 */
    #convModal > div { height: 90vh !important; }
    #convHeader { padding: 10px 12px !important; font-size: 13px; line-height: 1.6; }
    #convMessages { padding: 12px !important; }
}

@media (max-width: 380px) {
    .stat-grid { grid-template-columns: 1fr; }
}
.alert {
    padding: 10px 14px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 14px;
}
.alert-success { background: #d1fae5; color: #065f46; }
.alert-error { background: #fee2e2; color: #991b1b; }
