/* main-ui.css - Thiết kế lại giao diện trang chính (tab "Proxy thường").
 * Nạp SAU styles.css và groups.css nên ghi đè các quy tắc trùng lặp/quá to trong styles.css.
 * Mục tiêu: gọn (mật độ cao), cột "Thao tác" luôn thấy, vừa các màn PC phổ biến (1280-1920) và mobile.
 * Chỉ dùng CSS chuẩn của Chrome (flex, grid, sticky, accent-color, aspect-ratio không cần polyfill). */

/* ========== 1. Header: 1 hàng gọn, sticky ========== */
.header { z-index: 900; box-shadow: 0 1px 6px rgba(15, 23, 42, .08); }
.header .header-content {
    max-width: none;
    min-height: 0;
    padding: 8px 16px;
    flex-wrap: nowrap;
    gap: 12px;
}
.header .logo { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.header .logo i { font-size: 22px; }
.header .logo h1 { font-size: 17px; font-weight: 700; white-space: nowrap; letter-spacing: 0; }
.header .header-info.desktop-only { display: flex; align-items: center; gap: 6px; flex-wrap: nowrap; margin-left: auto; min-width: 0; }
.header .header-info .version { font-size: 11px; padding: 2px 8px; }
.header .header-info .server-status { font-size: 12px; white-space: nowrap; margin-right: 4px; }
.header .header-info .btn {
    height: 30px;
    padding: 0 10px;
    font-size: 12px;
    font-weight: 600;
    gap: 6px;
    margin: 0 !important; /* nút trong HTML có style="margin-right:10px" */
    white-space: nowrap;
    border-radius: 6px;
    box-shadow: none;
    flex: 0 0 auto;
}
.header .header-info .btn i { font-size: 12px; }

/* Màn hình vừa: dùng menu hamburger có sẵn thay vì tràn nhiều hàng */
@media (max-width: 1359px) {
    .header .hamburger-menu { display: flex; align-items: center; justify-content: center; margin-left: auto; }
    .header .header-info.desktop-only { display: none !important; }
}
@media (max-width: 1549px) { .header .header-info .version { display: none; } }
@media (max-width: 1549px) { .header .header-info .server-status { display: none; } }

/* ========== 2. Khung chính, thẻ thống kê ========== */
.main-container { padding: 12px 16px 24px; }

.stats-section { margin-bottom: 12px; }
.stats-section .stats-grid.compact {
    max-width: none;
    margin: 0;
    display: grid;
    grid-template-columns: repeat(4, minmax(150px, 210px));
    gap: 10px;
    justify-content: start;
}
.stats-section .stat-card { height: 56px; padding: 8px 12px; gap: 10px; border-radius: 8px; box-shadow: var(--shadow-sm); }
.stats-section .stat-icon { width: 34px; height: 34px; min-width: 34px; font-size: 15px; border-radius: 8px; }
.stats-section .stat-number { font-size: 20px; line-height: 1.1; }
.stats-section .stat-label { font-size: 10.5px; letter-spacing: .06em; }

/* ========== 3. Khung danh sách + thanh công cụ ========== */
.proxy-list-section .section-card { border-radius: 10px; box-shadow: var(--shadow-sm); }
.proxy-list-section .section-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 14px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border-light);
}
.proxy-list-section .section-header h2 { font-size: 15px; font-weight: 700; margin: 0; white-space: nowrap; gap: 8px; }
.proxy-list-section .section-header h2 i { font-size: 13px; padding: 5px; border-radius: 5px; }
.proxy-list-section .header-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; flex: 1 1 auto; margin: 0; }
.proxy-list-section .header-actions .btn {
    height: 30px;
    padding: 0 10px;
    font-size: 12px;
    font-weight: 600;
    gap: 6px;
    border-radius: 6px;
    box-shadow: none;
    white-space: nowrap;
}
.proxy-list-section .header-actions .btn i { font-size: 12px; }
.proxy-list-section .header-actions .search-box { margin-left: auto; min-width: 220px; flex: 0 1 260px; position: relative; }
.proxy-list-section .search-box input { height: 30px; font-size: 13px; padding: 0 10px 0 30px; border-width: 1px; box-shadow: none; }
.proxy-list-section .search-box i { font-size: 12px; left: 10px; }
.proxy-list-section .message-container:empty { display: none; }

/* ========== 4. Bảng proxy (PC) ========== */
.proxy-list-section .table-container {
    max-height: calc(100vh - 250px); /* bảng tự cuộn trong khung: tiêu đề và cột Thao tác luôn thấy */
    min-height: 320px;
    overflow: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
}
.proxy-table { border-collapse: separate; border-spacing: 0; width: 100%; }
.proxy-table th,
.proxy-table td {
    padding: 8px 10px;
    font-size: 12.5px;
    line-height: 1.35;
    text-align: left;
    vertical-align: middle;
    border-right: 0;
    border-bottom: 1px solid var(--border-light);
}
.proxy-table thead th {
    position: sticky;
    top: 0;
    z-index: 5;
    padding: 9px 10px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .05em;
    text-transform: uppercase;
    color: #334155;
    background: #eef2f7;
    border-bottom: 2px solid var(--primary-color);
    white-space: nowrap;
}
[data-theme="dark"] .proxy-table thead th { background: #2d3748; color: #cbd5e1; }
.proxy-table tbody tr { background: var(--bg-card); border-left: 0; box-shadow: none; transition: background-color .12s ease; }
.proxy-table tbody tr:nth-child(even) { background: #fafbfd; }
[data-theme="dark"] .proxy-table tbody tr:nth-child(even) { background: #33383d; }
.proxy-table tbody tr:hover { background: #f1f7ff; box-shadow: none; border-left: 0; }
.proxy-table tbody tr:hover td:first-child { box-shadow: inset 3px 0 0 var(--primary-color); }
.proxy-table tbody tr:last-child td { border-bottom: 0; }

/* cột chọn */
.proxy-table td:first-child,
.proxy-table th:first-child { width: 36px; padding: 8px 6px; text-align: center; }
.proxy-checkbox,
#selectAllCheckbox { width: 16px !important; height: 16px !important; transform: none; margin: 0; cursor: pointer; accent-color: var(--primary-color); }

/* cột IP, loại */
.proxy-table .proxy-ip { font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.proxy-table td.proxy-type-badge { background: none; box-shadow: none; border: 0; border-radius: 0; padding: 8px 10px; white-space: nowrap; }
.proxy-table .badge { font-size: 10.5px; font-weight: 700; padding: 2px 7px; border-radius: 4px; letter-spacing: .02em; }
.proxy-table .badge-converter { padding: 2px 5px; }

/* cột PROXY VT: cho phép xuống dòng thay vì làm bảng rộng ra */
.proxy-table td:nth-child(4) { min-width: 230px; max-width: 330px; }
.proxy-endpoint-full { display: flex; align-items: center; gap: 4px; }
.proxy-endpoint-full .endpoint-text {
    font-size: 12px;
    padding: 3px 8px;
    border-width: 1px;
    border-radius: 6px;
    letter-spacing: 0;
    box-shadow: none;
    line-height: 1.35;
    overflow-wrap: anywhere;
    word-break: break-all;
}
.proxy-endpoint-full .btn-copy { flex: 0 0 auto; padding: 3px 6px; font-size: 13px; border-radius: 5px; }

/* cột ghi chú: các huy hiệu nhỏ, xuống dòng gọn */
.proxy-table td:nth-child(5) { min-width: 104px; max-width: 150px; line-height: 1.9; }
.proxy-table .rate-limit-indicator,
.proxy-table .bandwidth-limit-indicator,
.proxy-table .auto-replace-indicator { font-size: 10.5px; padding: 1px 6px; border-radius: 4px; white-space: nowrap; }

/* cột trạng thái số liệu */
.proxy-table td:nth-child(n+6):nth-child(-n+11) { white-space: nowrap; }
.proxy-table .expiry-badge,
.proxy-table .data-quota-badge { font-size: 11.5px; padding: 3px 8px; border-radius: 999px; }
.proxy-table .status { font-size: 11.5px; font-weight: 700; padding: 3px 9px; border-radius: 999px; gap: 5px; }
.proxy-table .status i { font-size: 7px; }
.proxy-table .ping { font-size: 11.5px; padding: 3px 9px; border-radius: 999px; white-space: nowrap; }
.proxy-table .bandwidth-usage { font-size: 12px; padding: 3px 8px; border-radius: 999px; }
.proxy-table td.timestamp { font-size: 11px; line-height: 1.45; color: var(--text-secondary); }
.proxy-table td.timestamp .ts-upd { color: var(--text-tertiary); }

/* cột thời gian tạo/cập nhật: ẩn khi màn hình hẹp để nhường chỗ (xem tooltip ở ô IP) */
@media (max-width: 1499px) {
    .proxy-table th:nth-child(11),
    .proxy-table td:nth-child(11) { display: none; }
}

/* cột THAO TÁC: luôn dính mép phải, không bao giờ bị che khi cuộn ngang */
.proxy-table th:last-child,
.proxy-table td:last-child {
    position: sticky;
    right: 0;
    width: 1%;
    padding: 6px 10px;
    white-space: nowrap;
    background: inherit;
    box-shadow: -10px 0 10px -10px rgba(15, 23, 42, .25);
}
.proxy-table thead th:last-child { z-index: 7; background: #eef2f7; }
[data-theme="dark"] .proxy-table thead th:last-child { background: #2d3748; }
.proxy-table tbody td:last-child { z-index: 3; }
.proxy-table .action-buttons { display: flex; align-items: center; gap: 4px; flex-wrap: nowrap; }
.proxy-table .btn-action { width: 28px; height: 28px; padding: 0; font-size: 12px; border-radius: 6px; flex: 0 0 auto; }
.proxy-table .btn-action.btn-toggle-type { width: 28px; height: 28px; padding: 0 !important; min-width: 0; }

/* ========== 5. Tablet ========== */
@media (max-width: 1023px) {
    .stats-section .stats-grid.compact { grid-template-columns: repeat(4, minmax(120px, 1fr)); }
}

/* ========== 6. Mobile (bảng -> thẻ) ========== */
@media (max-width: 768px) {
    .header .header-content { padding: 6px 12px; }
    .header .logo h1 { font-size: 15px; }
    .header .logo i { font-size: 20px; }
    .main-container { padding: 10px 10px 20px; }

    .stats-section .stats-grid.compact { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
    .stats-section .stat-card { height: 52px; padding: 6px 10px; gap: 8px; }
    .stats-section .stat-icon { width: 30px; height: 30px; min-width: 30px; font-size: 13px; }
    .stats-section .stat-number { font-size: 17px; }

    .proxy-list-section .section-header { flex-direction: column; align-items: stretch; padding: 10px; gap: 8px; }
    .proxy-list-section .section-header h2 { font-size: 14px; }
    .proxy-list-section .header-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
    .proxy-list-section .header-actions .btn { height: 40px; width: 100%; justify-content: center; font-size: 12.5px; padding: 0 8px; }
    .proxy-list-section .header-actions .search-box { grid-column: 1 / -1; order: -1; margin: 0; min-width: 0; flex: none; width: 100%; }
    .proxy-list-section .search-box input { height: 40px; font-size: 16px; padding: 0 12px 0 34px; } /* >=16px: Chrome Android không tự phóng to */
    .proxy-list-section .search-box i { left: 12px; font-size: 13px; }

    #mobileProxyCards { padding: 8px; }
    .mobile-proxy-card { margin: 0 0 8px; padding: 10px 12px; border-radius: 10px; box-shadow: var(--shadow-sm); }
    .mobile-card-header { padding-bottom: 8px; margin-bottom: 8px; }
    .mobile-card-port { font-size: 16px; }
    .mobile-card-type { font-size: 10.5px; padding: 3px 10px; letter-spacing: .04em; }
    .mobile-card-checkbox { gap: 8px; }
    .mobile-card-checkbox input[type="checkbox"] { width: 20px; height: 20px; }

    /* Thân thẻ: lưới 2 cột, nhãn nhỏ phía trên giá trị -> thẻ ngắn hơn ~50% */
    .mobile-card-body { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; margin-bottom: 10px; }
    .mobile-card-row { display: flex; flex-direction: column; align-items: stretch; gap: 2px; margin: 0; min-width: 0; }
    .mobile-card-row:nth-child(-n+3) { grid-column: 1 / -1; } /* Proxy gốc, Domain, Endpoint: cả hàng */
    .mobile-card-label { min-width: 0; gap: 4px; font-size: 10.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
    .mobile-card-label i { font-size: 10px; width: auto; }
    .mobile-card-value { font-size: 12.5px; min-width: 0; }
    .mobile-card-value.proxy-original { font-size: 12px; padding: 4px 8px; border-radius: 6px; }
    .mobile-card-value.endpoint { font-size: 12px; padding: 5px 8px; border-width: 1px; border-radius: 8px; letter-spacing: 0; gap: 6px; justify-content: space-between; }
    .mobile-card-value.endpoint span { min-width: 0; overflow-wrap: anywhere; }
    .mobile-card-value .mobile-copy-btn { flex: 0 0 auto; width: 32px; height: 32px; padding: 0; font-size: 14px; }
    .mobile-card-value .status { padding: 3px 10px; font-size: 12px; }
    .mobile-card-value .bandwidth-usage { font-size: 12.5px; padding: 3px 10px; }
    .mobile-card-value.ping-good,
    .mobile-card-value.ping-medium,
    .mobile-card-value.ping-bad,
    .mobile-card-value.ping-timeout { font-size: 12.5px; padding: 3px 10px; align-self: flex-start; }

    /* Nút thao tác: to vừa ngón tay (>=40px), luôn nằm cuối thẻ */
    .mobile-card-footer { gap: 6px; padding-top: 10px; }
    .mobile-card-footer .btn-action-mobile { min-height: 40px; padding: 0 6px; font-size: 13px; border-width: 1px; }
    .mobile-card-footer .btn-action-mobile i { font-size: 13px; }
}

/* ========== 7. Ô "Loại" trong bảng: căn giữa theo chiều dọc ========== */
.proxy-table td.proxy-type-badge { display: table-cell; vertical-align: middle; text-transform: none; }

/* ========== 8. Modal: gọn hơn, tiêu đề + nút Lưu/Hủy luôn thấy (không phải cuộn mới thấy) ========== */
.modal .modal-content { max-height: 92vh; max-height: 92dvh; overscroll-behavior: contain; scrollbar-width: thin; }
.modal .modal-content > .modal-header { position: sticky; top: 0; z-index: 3; background: var(--bg-card); padding: 14px 20px 12px; }
.modal .modal-header h3 { font-size: 17px; }
.modal .modal-content .modal-actions {
    position: sticky;
    bottom: 0;
    z-index: 3;
    background: var(--bg-card);
    padding: 12px 20px;
    box-shadow: 0 -6px 10px -8px rgba(15, 23, 42, .25);
}
.modal .modal-content .modal-actions .btn { min-width: 120px; padding: 10px 16px; }
#editModal .proxy-form { padding: 14px 20px 0; }
#editModal .form-group { margin-bottom: 12px; }
#editModal .form-group label { font-size: 13px; margin-bottom: 4px; }
#editModal .form-group input,
#editModal .form-group select { height: 38px; padding: 0 12px; font-size: 14px; }
#editModal .modal-actions { margin: 0 -20px; }
@media (max-width: 768px) {
    .modal .modal-content { width: 94%; max-height: 94vh; max-height: 94dvh; }
    .modal .modal-content .modal-actions { padding: 10px 14px; }
    .modal .modal-content .modal-actions { flex-direction: row; flex-wrap: wrap; gap: 8px; }
    .modal .modal-content .modal-actions .btn { flex: 1 1 30%; min-width: 0; min-height: 42px; padding: 0 8px; font-size: 13px; white-space: nowrap; }
    #editModal .proxy-form { padding: 12px 14px 0; }
    #editModal .modal-actions { margin: 0 -14px; }
    #editModal .form-group input,
    #editModal .form-group select { font-size: 16px; } /* tránh Chrome Android tự phóng to khi focus */
}

/* ========== 9. Căn giữa các cột dữ liệu số (bảng Proxy thường) ========== */
#proxyTableContainer .proxy-table th:nth-child(n+6):nth-child(-n+11),
#proxyTableContainer .proxy-table td:nth-child(n+6):nth-child(-n+11) { text-align: center; }
#proxyTableContainer .proxy-table td:nth-child(n+6):nth-child(-n+11) .ping,
#proxyTableContainer .proxy-table td:nth-child(n+6):nth-child(-n+11) .status { margin-inline: auto; }

/* ========== 10. Thẻ thống kê: số và nhãn căn giữa ========== */
.stats-section .stat-card { align-items: center; }
.stats-section .stat-content { flex: 1; min-width: 0; text-align: center; }
.stats-section .stat-number,
.stats-section .stat-label { text-align: center; }

/* ========== 11. 4 thẻ thống kê chia đều toàn chiều ngang, nội dung mỗi thẻ căn giữa ========== */
.stats-section .stats-grid.compact { grid-template-columns: repeat(4, minmax(0, 1fr)); justify-content: stretch; }
.stats-section .stat-card { justify-content: center; }
.stats-section .stat-content { flex: 0 0 auto; min-width: 84px; }
@media (max-width: 768px) {
    .stats-section .stats-grid.compact { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
