/* ==========================================================================
   移动端适配层（最后加载，用于覆盖各页面内联样式）
   断点：≤1024 / ≤768 / ≤640 / ≤420
   ========================================================================== */

/* ---------- 基础 ---------- */
html { -webkit-text-size-adjust: 100%; }
body { overflow-x: hidden; }
img, video, svg { max-width: 100%; }
img, video { height: auto; }
pre { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* 密钥/长串不撑破卡片 */
.api-key, .api-key-box code, .key-code, .key-text, .token-text {
    overflow-wrap: anywhere;
    word-break: break-all;
}

/* ---------- 表格：容器横向滚动 ---------- */
.table-wrapper,
.table-card,
.orders-card,
.ak-table-wrap,
.lr-body {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
}
/* 原先 .table-card 为 overflow:hidden，会把宽表格裁掉 */
.table-card { overflow-y: hidden !important; }

/* 表格没有包在滚动容器时，让卡片自己滚动 */
.content-card:has(> .data-table),
.card-body:has(> table),
.card:has(> table),
.card:has(> .table-wrapper) { overflow-x: auto; }

/* ---------- 顶栏 / 标题 / 操作区自动换行 ---------- */
.page-header,
.card-header,
.section-header,
.stats-header,
.services-header,
.api-key-header,
.chart-header,
.logs-header,
.toolbar,
.toolbar-left,
.toolbar-right,
.form-actions,
.update-actions,
.card-actions,
.page-actions,
.pagination,
.pagination-section,
.filters-row {
    flex-wrap: wrap;
}

/* ---------- ≤1024：后台侧边栏已由 layout 处理，这里收敛留白 ---------- */
@media (max-width: 1024px) {
    #app-wrapper { height: 100dvh; }
    #page-content { padding: 20px !important; }
    .page-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
    .page-header h2 { font-size: 20px !important; margin-bottom: 0 !important; }
    .content-card { padding: 18px !important; }
    .chart-card, .info-card, .section-card { padding: 16px !important; }
    .stat-card { padding: 16px !important; }
    .stat-card-value { font-size: 22px !important; }
}

/* ---------- ≤960：多列布局折成单列 ---------- */
@media (max-width: 960px) {
    .form-row,
    .info-grid,
    .cc-form,
    .lr-form-row,
    .dashboard-grid,
    .online-tester-grid,
    .chart-section,
    .top-apis-section,
    .detail-grid,
    #dbSummary {
        grid-template-columns: 1fr !important;
    }
    /* 平板：换行显示，不横向滑动 */
    .nav-tabs { flex-wrap: wrap; overflow-x: visible; }
    .nav-tab { white-space: nowrap; }
    /* 消费概览保持两列 */
    .consumption-overview { grid-template-columns: repeat(2, 1fr) !important; }
    .consumption-card:nth-child(3) { grid-column: 1 / -1; }
}

/* ---------- ≤768：手机 ---------- */
@media (max-width: 768px) {
    /* iOS 聚焦时不放大 */
    input, select, textarea { font-size: 16px !important; }

    #page-content { padding: 14px !important; }
    .page-header h2 { font-size: 18px !important; }
    .page-header p { font-size: 13px; }
    .content-card { padding: 14px !important; border-radius: 12px !important; }
    .card-header { padding: 14px 16px !important; }
    .card-body { padding: 14px !important; }
    .card-title { font-size: 16px !important; }

    /* 表格单元格不折成竖排，交给容器横向滚动 */
    .table-wrapper th, .table-wrapper td,
    .table-card th, .table-card td,
    .orders-card th, .orders-card td,
    .ak-table-wrap th, .ak-table-wrap td,
    .lr-body th, .lr-body td,
    .data-table th, .data-table td,
    .card-body > table th, .card-body > table td,
    .card > table th, .card > table td {
        padding: 10px 12px !important;
        font-size: 13px !important;
        white-space: nowrap;
    }

    /* 搜索框不再强制 300px 宽 */
    .search-box { min-width: 0 !important; flex: 1 1 100% !important; }
    .filters-row { flex-direction: column; align-items: stretch; gap: 10px; }
    .filters-row > * { width: 100%; }

    /* 弹窗铺满可用宽度 */
    .modal { width: calc(100vw - 24px) !important; max-width: 100% !important; max-height: 88dvh; overflow-y: auto; }
    .modal-overlay { padding: 12px; align-items: flex-start; }

    /* 代码块 */
    .detail-json, .code-block, .response-body, .json-block { max-width: 100%; }

    /* 按钮组不溢出 */
    .btn-group, .actions-wrapper { flex-wrap: wrap; }
}

/* ---------- ≤640：更窄 ---------- */
@media (max-width: 640px) {
    .plans-grid,
    .type-cards {
        grid-template-columns: 1fr !important;
    }

    /* 概览：快捷操作 / 我的服务 每行 2 个 */
    .quick-grid,
    .services-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 12px !important;
    }
    .quick-card,
    .service-card {
        padding: 12px !important;
        gap: 10px !important;
    }
    .quick-card-icon,
    .service-card-icon {
        width: 36px !important;
        height: 36px !important;
        border-radius: 10px !important;
    }
    .quick-card-icon svg,
    .service-card-icon svg {
        width: 18px !important;
        height: 18px !important;
    }
    .quick-card-title { font-size: 13px !important; }
    .service-card-name {
        font-size: 13px !important;
        white-space: normal !important;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }
    .service-card-price { font-size: 11px !important; }
    .service-card-arrow { display: none !important; }

    .stat-row { grid-template-columns: repeat(2, 1fr) !important; }
    #dbSummary { grid-template-columns: repeat(2, 1fr) !important; }

    /* 概览统计卡片：2×2，收紧尺寸避免挤压 */
    .stat-card { padding: 13px !important; }
    .stat-card-icon { width: 40px !important; height: 40px !important; margin-bottom: 10px !important; }
    .stat-card-icon svg { width: 20px; height: 20px; }
    .stat-card-value { font-size: 19px !important; }
    .stat-card-label { font-size: 12px; }
    .stat-card-action { top: 12px !important; right: 12px !important; padding: 4px 8px !important; font-size: 11px !important; }

    /* 登录 / 注册 / 找回密码 */
    .auth-box { padding: 24px 18px !important; }
    .auth-card { padding: 26px 18px !important; }
    .auth-wrapper { padding: 14px !important; }
    .login-card, .login-container { padding-left: 16px !important; padding-right: 16px !important; }

    /* 个人中心导航：手机端每行 3 个，不再横向滑动 */
    .nav-tabs {
        display: grid !important;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 8px;
        overflow: visible !important;
    }
    .nav-tab {
        justify-content: center;
        gap: 5px;
        padding: 10px 4px !important;
        font-size: 13px;
        min-width: 0;
    }
    .nav-tab svg { width: 16px; height: 16px; }

    /* 首页卡片 */
    .api-grid { grid-template-columns: 1fr !important; }
    .api-card { padding: 18px !important; }
    .topbar { padding: 10px 14px !important; gap: 12px; }
    .layout { padding: 14px 12px !important; }

    /* 分页 */
    .pagination { justify-content: center; }
    .pagination-info { width: 100%; justify-content: space-between; }

    /* 卡片内边距进一步收敛 */
    .rail-card, .stat-box, .chart-card, .info-card { padding: 14px !important; }
    .stat-box .v { font-size: 20px !important; }
}

/* ---------- ≤420：小屏 ---------- */
@media (max-width: 420px) {
    .stat-row,
    .mini-stats {
        grid-template-columns: 1fr !important;
    }
    /* 统计概览保持两列 */
    .stats-overview { grid-template-columns: repeat(2, 1fr) !important; gap: 10px !important; }
    .page-header h2 { font-size: 17px !important; }
    .card-title { font-size: 15px !important; }
    .btn-submit, .btn-save, .btn-primary { width: 100%; }

    /* 验证码 + 输入框一行放不下时换行 */
    .input-group { flex-wrap: wrap; }
    .input-group .captcha-img { width: 100%; height: 44px; object-fit: contain; }

    /* 表格内边距再压缩 */
    .table-wrapper th, .table-wrapper td,
    .table-card th, .table-card td,
    .orders-card th, .orders-card td {
        padding: 8px 10px !important;
        font-size: 12px !important;
    }
}
