/* =====================================================================
   KRENDE UI — COMPONENT DÙNG CHUNG
   Nút · nút tròn · chip · ô nhập · công tắc · thẻ · thẻ tool (dạng lưới video) · bảng ·
   nhãn trạng thái · tab · avatar · menu thả · hộp thoại · toast · khung xương · trống ·
   phân trang · ô thống kê · thông báo dải.
   Phản hồi khi bấm là TỨC THÌ (đổi nền ngay, không chờ hiệu ứng) để cảm giác nhanh.
   ===================================================================== */

/* ---------- Nút ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
    height: 36px; padding: 0 var(--sp-4); border-radius: var(--r-pill); border: 0;
    font-weight: 500; font-size: var(--fs-md); white-space: nowrap; user-select: none;
    background: var(--surface-2); color: var(--text);
}
.btn:hover  { background: var(--surface-hover); }
.btn:active { background: var(--surface-press); }
.btn:disabled, .btn.is-loading { opacity: .5; pointer-events: none; }
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover  { background: var(--primary-hover); }
.btn-primary:active { opacity: .85; }
.btn-outline { background: transparent; border: 1px solid var(--border-strong); color: var(--link); }
.btn-outline:hover { background: var(--info-bg); border-color: transparent; }
.btn-ghost  { background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { height: 32px; padding: 0 var(--sp-3); font-size: var(--fs-sm); }
.btn-lg { height: 44px; padding: 0 var(--sp-6); font-size: var(--fs-lg); }
.btn-block { width: 100%; }
.btn.is-loading::after {
    content: ""; width: 14px; height: 14px; border-radius: 50%;
    border: 2px solid currentColor; border-right-color: transparent; animation: k-spin .6s linear infinite;
}
@keyframes k-spin { to { transform: rotate(360deg); } }

/* Nút tròn chỉ có icon */
.icon-btn {
    position: relative; display: inline-grid; place-items: center;
    width: 40px; height: 40px; border-radius: 50%; border: 0;
    background: transparent; font-size: 20px; line-height: 1;
}
.icon-btn:hover  { background: var(--surface-2); }
.icon-btn:active { background: var(--surface-hover); }
.icon-btn .dot {
    position: absolute; top: 4px; right: 2px; min-width: 18px; height: 16px; padding: 0 4px;
    border-radius: var(--r-pill); background: var(--badge); color: #fff;
    font-size: 10px; font-weight: 700; line-height: 16px; text-align: center;
}

/* ---------- Chip (bộ lọc ngang) ---------- */
.chips { display: flex; gap: var(--sp-3); overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
    flex-shrink: 0; height: 32px; padding: 0 var(--sp-3); border-radius: var(--r-sm); border: 0;
    background: var(--surface-2); color: var(--text); font-weight: 500; font-size: var(--fs-md);
}
.chip:hover { background: var(--surface-hover); }
.chip.is-active { background: var(--primary); color: var(--on-primary); }

/* ---------- Form ---------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field + .field { margin-top: var(--sp-4); }
.label { font-size: var(--fs-sm); font-weight: 500; }
.hint  { font-size: var(--fs-xs); color: var(--text-2); }
.field.has-error .hint { color: var(--danger); }
.input, .select, .textarea {
    width: 100%; height: 40px; padding: 0 var(--sp-3);
    background: var(--surface-2); border: 1px solid transparent; border-radius: var(--r-sm); outline: none;
}
.textarea { height: auto; min-height: 96px; padding: var(--sp-3); resize: vertical; }
.input:hover, .select:hover, .textarea:hover { border-color: var(--border-strong); }
.input:focus, .select:focus, .textarea:focus { border-color: var(--link); background: var(--bg); }
.field.has-error .input, .field.has-error .textarea { border-color: var(--danger); }
.input::placeholder, .textarea::placeholder { color: var(--text-3); }

.check { display: inline-flex; align-items: center; gap: var(--sp-2); cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }

.switch { position: relative; display: inline-block; width: 36px; height: 20px; flex-shrink: 0; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: var(--r-pill); background: var(--surface-hover); }
.switch span::after {
    content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px;
    border-radius: 50%; background: var(--text);
}
.switch input:checked + span { background: var(--link); }
.switch input:checked + span::after { left: 18px; background: #fff; }

/* ---------- Thẻ ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); padding: var(--sp-4); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); margin-bottom: var(--sp-3); }
.card-title { font-size: var(--fs-lg); font-weight: 700; }
.card-flat { background: transparent; border: 0; padding: 0; }

/* ---------- Thẻ tool: lưới kiểu video YouTube ---------- */
.tool-grid {
    display: grid; gap: var(--sp-10) var(--sp-4);
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
.tool-card { display: flex; flex-direction: column; gap: var(--sp-3); min-width: 0; }
.tool-thumb {
    position: relative; aspect-ratio: 16 / 9; border-radius: var(--r-md); overflow: hidden;
    background: var(--surface-2);
}
.tool-thumb img { width: 100%; height: 100%; object-fit: cover; }
.tool-thumb .tag {
    position: absolute; right: 8px; bottom: 8px; padding: 2px 6px; border-radius: 4px;
    background: rgba(0, 0, 0, .8); color: #fff; font-size: var(--fs-xs); font-weight: 500;
}
.tool-card:hover .tool-thumb { border-radius: 0; }
.tool-meta { display: flex; gap: var(--sp-3); }
.tool-title {
    font-size: var(--fs-lg); font-weight: 500; line-height: 22px; max-height: 44px; overflow: hidden;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.tool-sub { color: var(--text-2); font-size: var(--fs-md); margin-top: 4px; }
.tool-price { font-weight: 700; color: var(--text); }

/* ---------- Avatar ---------- */
.avatar {
    width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0; overflow: hidden;
    display: grid; place-items: center; background: var(--surface-hover); color: var(--text);
    font-weight: 500; font-size: var(--fs-md);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-sm { width: 24px; height: 24px; font-size: 11px; }
.avatar-lg { width: 80px; height: 80px; font-size: 32px; }

/* ---------- Nhãn trạng thái ---------- */
.badge {
    display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 var(--sp-2);
    border-radius: 4px; font-size: var(--fs-xs); font-weight: 500; white-space: nowrap;
    background: var(--surface-2); color: var(--text-2);
}
.badge-success { background: var(--success-bg); color: var(--success); }
.badge-warning { background: var(--warning-bg); color: var(--warning); }
.badge-danger  { background: var(--danger-bg);  color: var(--danger); }
.badge-info    { background: var(--info-bg);    color: var(--info); }

/* ---------- Tab ---------- */
.tabs { display: flex; gap: var(--sp-6); border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: none; }
.tab {
    padding: var(--sp-3) 0; border: 0; background: none; color: var(--text-2);
    font-weight: 500; font-size: var(--fs-lg); white-space: nowrap; border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--text); border-bottom-color: var(--text); }

/* ---------- Bảng ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--r-md); }
.table { width: 100%; border-collapse: collapse; font-size: var(--fs-md); }
.table th {
    text-align: left; padding: var(--sp-3) var(--sp-4); font-weight: 500; font-size: var(--fs-sm);
    color: var(--text-2); border-bottom: 1px solid var(--border); white-space: nowrap;
}
.table td { padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--surface-2); }
/* Màn hẹp: bảng giữ bề ngang tối thiểu và cuộn ngang trong .table-wrap thay vì bóp chữ xuống từng dòng */
@media (max-width: 640px) {
  .table-wrap > .table { min-width: 560px; }
  .table th, .table td { padding: var(--sp-2) var(--sp-3); }
}
.num { text-align: right; font-variant-numeric: tabular-nums; }
.amount-in  { color: var(--success); font-weight: 500; }
.amount-out { color: var(--text); font-weight: 500; }

/* ---------- Danh sách (thông báo, giao dịch...) ---------- */
.list-item { display: flex; align-items: flex-start; gap: var(--sp-3); padding: var(--sp-3); border-radius: var(--r-md); }
.list-item:hover { background: var(--surface-2); }
.list-item.is-unread { background: var(--info-bg); }
.list-body { flex: 1; min-width: 0; }
.list-title { font-weight: 500; }
.list-sub { color: var(--text-2); font-size: var(--fs-sm); }

/* ---------- Menu thả (avatar, ...) ---------- */
.menu-anchor { position: relative; }
.menu {
    position: absolute; top: calc(100% + 4px); right: 0; z-index: 300; min-width: 260px;
    padding: var(--sp-2) 0; border-radius: var(--r-md); background: var(--surface-2);
    box-shadow: var(--shadow-pop); display: none;
}
.menu.is-open { display: block; }
.menu-head { display: flex; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); }
.menu-item {
    display: flex; align-items: center; gap: var(--sp-4); width: 100%; height: 40px;
    padding: 0 var(--sp-4); border: 0; background: none; text-align: left; font-size: var(--fs-md);
}
.menu-item:hover { background: var(--surface-hover); }
.menu-sep { height: 1px; margin: var(--sp-2) 0; background: var(--border-strong); }

/* ---------- Hộp thoại ---------- */
.modal-scrim { position: fixed; inset: 0; z-index: 400; background: var(--overlay); display: grid; place-items: center; padding: var(--sp-4); }
.modal { width: 100%; max-width: 480px; max-height: 90vh; overflow-y: auto; border-radius: var(--r-lg); background: var(--surface-2); padding: var(--sp-6); box-shadow: var(--shadow-pop); }
.modal-title { font-size: var(--fs-xl); font-weight: 700; margin-bottom: var(--sp-3); }
.modal-actions { display: flex; justify-content: flex-end; gap: var(--sp-2); margin-top: var(--sp-6); }

/* ---------- Toast (góc dưới trái như YouTube) ---------- */
.toast-stack { position: fixed; left: var(--sp-6); bottom: var(--sp-6); z-index: 500; display: flex; flex-direction: column; gap: var(--sp-2); }
.toast {
    min-width: 280px; max-width: 420px; padding: var(--sp-3) var(--sp-4); border-radius: var(--r-sm);
    background: var(--text); color: var(--text-invert); box-shadow: var(--shadow-pop); font-size: var(--fs-md);
}
.toast-error { background: var(--danger); color: #fff; }
@media (max-width: 720px) { .toast-stack { left: var(--sp-3); right: var(--sp-3); bottom: calc(var(--bottomnav-h) + var(--sp-3)); } }

/* ---------- Khung xương khi đang tải (thay cho hiệu ứng chuyển trang) ---------- */
.skeleton {
    border-radius: var(--r-sm);
    background: linear-gradient(90deg, var(--skeleton) 0%, var(--skeleton-hi) 50%, var(--skeleton) 100%);
    background-size: 200% 100%; animation: k-shimmer 1.4s ease-in-out infinite;
}
.skeleton-text  { height: 14px; margin: 6px 0; }
.skeleton-title { height: 18px; width: 70%; }
.skeleton-thumb { aspect-ratio: 16 / 9; border-radius: var(--r-md); }
.skeleton-avatar { width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0; }
@keyframes k-shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* ---------- Trạng thái trống ---------- */
.empty { text-align: center; padding: var(--sp-10) var(--sp-4); color: var(--text-2); }
.empty-icon { font-size: 48px; margin-bottom: var(--sp-3); }
.empty-title { color: var(--text); font-size: var(--fs-lg); font-weight: 500; margin-bottom: var(--sp-1); }
.empty .btn { margin-top: var(--sp-4); }

/* ---------- Phân trang ---------- */
.pager { display: flex; justify-content: center; gap: var(--sp-1); margin-top: var(--sp-6); }
.pager button { min-width: 36px; height: 36px; border-radius: var(--r-pill); border: 0; background: transparent; }
.pager button:hover { background: var(--surface-2); }
.pager button.is-active { background: var(--primary); color: var(--on-primary); }

/* ---------- Ô thống kê ---------- */
.stats { display: grid; gap: var(--sp-4); grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); padding: var(--sp-4); }
.stat-label { color: var(--text-2); font-size: var(--fs-sm); }
.stat-value { font-size: var(--fs-2xl); font-weight: 700; margin-top: var(--sp-1); font-variant-numeric: tabular-nums; }
.stat-delta { font-size: var(--fs-sm); margin-top: var(--sp-1); }
.stat-delta.up { color: var(--success); }
.stat-delta.down { color: var(--danger); }
@media (max-width: 480px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-2); }
  .stat { padding: var(--sp-3); }
  .stat-value { font-size: var(--fs-xl); }
}

/* ---------- Thông báo dải ---------- */
.alert { display: flex; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); border-radius: var(--r-md); background: var(--surface-2); }
.alert-info    { background: var(--info-bg); }
.alert-success { background: var(--success-bg); }
.alert-warning { background: var(--warning-bg); }
.alert-danger  { background: var(--danger-bg); }

/* ---------- Tiện ích bố cục ---------- */
.stack   { display: flex; flex-direction: column; gap: var(--sp-4); }
.row     { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }
.between { justify-content: space-between; }
.grid-2  { display: grid; gap: var(--sp-4); grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.section + .section { margin-top: var(--sp-10); }
.section-title { font-size: var(--fs-xl); font-weight: 700; margin-bottom: var(--sp-4); }

/* ---------- Bổ sung cho trang render ở server (link thay cho nút) ---------- */
a.chip { display: inline-flex; align-items: center; }
.pager-link {
    display: inline-grid; place-items: center; min-width: 36px; height: 36px; padding: 0 var(--sp-2);
    border-radius: var(--r-pill); color: var(--text);
}
.pager-link:hover { background: var(--surface-2); }
.pager-link.is-active { background: var(--primary); color: var(--on-primary); font-weight: 500; }
.toolbar { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; margin-bottom: var(--sp-6); }
.toolbar .chips { flex: 1; min-width: 0; }
.select-sm { height: 32px; width: auto; padding: 0 var(--sp-3); }
.result-meta { color: var(--text-2); margin-bottom: var(--sp-4); }
.see-all { color: var(--link); font-weight: 500; font-size: var(--fs-md); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); margin-bottom: var(--sp-4); }
.section-head .section-title { margin: 0; }

/* ---------- Chat hỗ trợ: nút 💬 góc phải + khung chat (assets/js/ui/support-chat.js) ---------- */
.k-sup-fab {
    position: fixed; right: 20px; bottom: 20px; z-index: 150;
    width: 52px; height: 52px; border-radius: 50%; border: 0; cursor: pointer;
    display: grid; place-items: center; font-size: 24px; line-height: 1;
    background: var(--primary); color: var(--on-primary); box-shadow: var(--shadow-pop); transition: transform .15s;
}
.k-sup-fab:hover { transform: scale(1.06); }
.k-sup-fab[hidden] { display: none; }
.k-sup-badge {
    position: absolute; top: -2px; right: -2px; min-width: 20px; height: 20px; padding: 0 5px;
    display: grid; place-items: center; border-radius: var(--r-pill); border: 2px solid var(--bg);
    background: var(--danger); color: #fff; font-size: 11px; font-weight: 700;
}
.k-sup-badge[hidden] { display: none; }
.k-sup-panel {
    position: fixed; right: 20px; bottom: 84px; z-index: 150;
    width: 360px; height: min(520px, calc(100vh - 120px));
    display: flex; flex-direction: column; overflow: hidden;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); box-shadow: var(--shadow-pop);
}
.k-sup-panel[hidden] { display: none; }
.k-sup-head { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border); }
.k-sup-avatar {
    width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0; display: grid; place-items: center;
    background: var(--primary); color: var(--on-primary); font-weight: 700;
}
.k-sup-title { flex: 1; min-width: 0; display: grid; }
.k-sup-title small { color: var(--text-2); font-size: var(--fs-xs); }
.k-sup-body { flex: 1; overflow-y: auto; padding: var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-3); }
.k-sup-msg { max-width: 85%; align-self: flex-start; }
.k-sup-msg.is-me { align-self: flex-end; text-align: right; }
.k-sup-bubble {
    display: inline-block; text-align: left; padding: var(--sp-2) var(--sp-3); border-radius: 16px;
    background: var(--surface-2); font-size: var(--fs-sm); white-space: pre-wrap; overflow-wrap: anywhere;
}
.k-sup-msg.is-me .k-sup-bubble { background: var(--primary); color: var(--on-primary); }
.k-sup-time { margin-top: 2px; font-size: 11px; color: var(--text-3); }
.k-sup-note { margin: auto; padding: var(--sp-4); display: grid; gap: var(--sp-3); justify-items: center; text-align: center; color: var(--text-2); }
.k-sup-form { display: flex; gap: var(--sp-2); align-items: flex-end; padding: var(--sp-3); border-top: 1px solid var(--border); }
.k-sup-form[hidden] { display: none; }
.k-sup-form textarea {
    flex: 1; min-height: 36px; max-height: 120px; padding: 8px var(--sp-3); resize: none; outline: none; overflow-y: hidden;
    border: 1px solid transparent; border-radius: 18px; background: var(--surface-2); color: inherit; font: inherit;
}
.k-sup-form textarea:focus { border-color: var(--link); }
@media (max-width: 720px) {
    .k-sup-fab { right: 16px; bottom: calc(var(--bottomnav-h) + 16px); width: 48px; height: 48px; font-size: 22px; }
    .k-sup-panel {
        inset: var(--topbar-h) 0 0 0; width: auto; height: auto;
        border-width: 1px 0 0; border-radius: var(--r-lg) var(--r-lg) 0 0;
    }
}
