/* ==========================================================
   Telesale & CSKH - giao diện mobile-first + PWA
   ========================================================== */
:root {
    --c-primary: #0d6efd;
    --c-primary-dark: #0a58ca;
    --c-bg: #f1f4f9;
    --c-card: #ffffff;
    --c-line: #e5e9f0;
    --c-text: #1f2937;
    --c-muted: #6b7280;
    --radius: 14px;
    --appbar-h: 56px;
    --bottom-h: 62px;
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-t: env(safe-area-inset-top, 0px);
}

html { -webkit-text-size-adjust: 100%; }
body {
    background: var(--c-bg);
    color: var(--c-text);
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior-y: contain;
}

/* Khung chung của mọi trang */
body > .container-fluid {
    max-width: 1440px;
    margin: 0 auto;
    padding: 0 12px calc(var(--bottom-h) + var(--safe-b) + 24px) !important;
}
@media (min-width: 992px) {
    body > .container-fluid { padding-bottom: 32px !important; }
}

/* Input đủ lớn, không bị iOS tự zoom */
@media (max-width: 767.98px) {
    input.form-control, select.form-select, textarea.form-control,
    .form-control-sm, .form-select-sm { font-size: 16px !important; min-height: 44px; }
    textarea.form-control { min-height: 0; }
    .btn { min-height: 42px; }
    .btn-sm { min-height: 38px; }
}

/* ---------- App bar ---------- */
.appbar {
    position: sticky; top: 0; z-index: 1030;
    margin: 0 -12px 10px;
    padding: calc(8px + var(--safe-t)) 12px 8px;
    min-height: var(--appbar-h);
    display: flex; align-items: center; gap: 10px;
    background: linear-gradient(135deg, #0d6efd, #0a4fc4);
    color: #fff;
    box-shadow: 0 2px 10px rgba(13, 110, 253, .25);
}
.appbar-brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; min-width: 0; flex: 1 1 auto; }
.appbar-brand .logo {
    width: 38px; height: 38px; border-radius: 11px; flex: 0 0 auto;
    background: rgba(255,255,255,.18); display: grid; place-items: center; font-size: 1.1rem;
}
.appbar-brand b { display: block; font-size: 1rem; line-height: 1.15; }
.appbar-brand small { display: block; opacity: .85; font-size: .75rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar-actions { display: flex; gap: 6px; flex: 0 0 auto; }
.appbar-actions .btn { background: rgba(255,255,255,.16); border: 0; color: #fff; font-weight: 600; font-size: .85rem; min-height: 38px; }
.appbar-actions .btn:hover { background: rgba(255,255,255,.28); color: #fff; }
.appbar-btn {
    flex: 0 0 auto; width: 42px; height: 42px; border-radius: 12px; border: 0;
    background: rgba(255,255,255,.16); color: #fff; font-size: 1.15rem;
}
.appbar-btn:active { background: rgba(255,255,255,.3); }

/* ---------- Tìm kiếm ---------- */
.searchbar { margin-bottom: 10px; }
.searchbar .input-group { box-shadow: 0 2px 8px rgba(0,0,0,.05); border-radius: 12px; }
.searchbar .form-control, .searchbar .btn { border-color: #cfe0ff; }
.searchbar .form-control { border-radius: 12px 0 0 12px; background: #fff; }
.searchbar .btn:last-child { border-radius: 0 12px 12px 0; }

/* ---------- Tabs cuộn ngang ---------- */
.tab-scroll {
    display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 10px; margin: 0 -12px 4px; padding-left: 12px; padding-right: 12px;
    scrollbar-width: none; -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity;
}
.tab-scroll::-webkit-scrollbar { display: none; }
.tab-pill {
    flex: 0 0 auto; scroll-snap-align: center;
    display: inline-flex; align-items: center; gap: 6px;
    padding: 8px 14px; border-radius: 999px;
    background: #fff; color: var(--c-text); border: 1px solid var(--c-line);
    font-size: .9rem; font-weight: 600; text-decoration: none; white-space: nowrap;
}
.tab-pill i { color: var(--c-muted); }
.tab-pill.active { background: var(--c-primary); border-color: var(--c-primary); color: #fff; box-shadow: 0 3px 10px rgba(13,110,253,.3); }
.tab-pill.active i { color: #fff; }

/* ---------- Menu dưới (mobile) ---------- */
.bottom-nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 1030;
    height: calc(var(--bottom-h) + var(--safe-b)); padding-bottom: var(--safe-b);
    display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
    background: #fff; border-top: 1px solid var(--c-line);
    box-shadow: 0 -4px 16px rgba(0,0,0,.06);
}
.bottom-nav a, .bottom-nav button {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    border: 0; background: none; color: var(--c-muted); font-size: .7rem; font-weight: 600; text-decoration: none;
}
.bottom-nav i { font-size: 1.15rem; }
.bottom-nav .active { color: var(--c-primary); }
.bottom-nav .active i { transform: translateY(-1px); }

/* ---------- Offcanvas menu ---------- */
.menu-user { display: flex; align-items: center; gap: 12px; padding: 4px 0 12px; }
.menu-user .avatar {
    width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center;
    background: var(--c-primary); color: #fff; font-weight: 700; font-size: 1.1rem;
}
.menu-list { display: grid; gap: 4px; }
.menu-list a, .menu-list button {
    display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
    padding: 12px 12px; border-radius: 12px; border: 0; background: transparent;
    color: var(--c-text); font-weight: 600; text-decoration: none; font-size: .98rem;
}
.menu-list a i, .menu-list button i { width: 22px; text-align: center; color: var(--c-primary); }
.menu-list a.active { background: #e7f1ff; color: var(--c-primary); }
.menu-list a:active, .menu-list button:active { background: #eef2f7; }
.menu-list .danger, .menu-list .danger i { color: #dc3545; }
.menu-sep { height: 1px; background: var(--c-line); margin: 8px 0; }

/* ---------- Banner offline + toast ---------- */
.offline-banner {
    display: none; position: sticky; top: var(--appbar-h); z-index: 1029;
    margin: 0 -12px 10px; padding: 7px 12px; background: #b45309; color: #fff; font-size: .85rem; font-weight: 600; text-align: center;
}
body.is-offline .offline-banner { display: block; }
.app-toast {
    position: fixed; left: 50%; bottom: calc(var(--bottom-h) + var(--safe-b) + 16px); transform: translate(-50%, 20px);
    max-width: 90vw; padding: 10px 16px; border-radius: 12px; background: #111827; color: #fff; font-size: .9rem;
    opacity: 0; pointer-events: none; transition: .25s; z-index: 2000;
}
.app-toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 992px) { .app-toast { bottom: 24px; } }

/* ---------- Thanh công cụ danh sách ---------- */
.list-toolbar {
    display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 10px;
}
.list-toolbar .quick-filter { flex: 1 1 220px; position: relative; }
.list-toolbar .quick-filter i { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--c-muted); }
.list-toolbar .quick-filter input { padding-left: 36px; border-radius: 12px; }
.list-count { font-size: .85rem; color: var(--c-muted); font-weight: 600; white-space: nowrap; }
.select-all { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: .9rem; }
.select-all input { width: 1.25rem; height: 1.25rem; }

/* ---------- Thẻ khách hàng ---------- */
.cust-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 768px) { .cust-grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1200px) { .cust-grid { grid-template-columns: repeat(3, 1fr); } }

.cust-card {
    position: relative; background: var(--c-card); border-radius: var(--radius);
    border: 1px solid var(--c-line); border-left: 5px solid #9ca3af;
    box-shadow: 0 2px 8px rgba(17, 24, 39, .05);
    padding: 12px; display: flex; flex-direction: column; gap: 10px; min-width: 0;
}
.cust-card.st-chua-goi { border-left-color: #9ca3af; }
.cust-card.st-chua-nghe { border-left-color: #f59e0b; }
.cust-card.st-dang-lam { border-left-color: #0dcaf0; }
.cust-card.st-khao-sat { border-left-color: #0d6efd; }
.cust-card.st-ky-hd, .cust-card.st-hoan-thanh { border-left-color: #198754; }
.cust-card.st-huy { border-left-color: #dc3545; opacity: .8; }
.cust-card.is-due { box-shadow: 0 0 0 2px rgba(220,53,69,.35), 0 2px 8px rgba(17,24,39,.05); }
.cust-card.is-highlight { background: #fff8e1; box-shadow: 0 0 0 2px #ffc107; }

.cc-head { display: flex; align-items: flex-start; gap: 10px; }
.cc-head .form-check-input { width: 1.4rem; height: 1.4rem; margin-top: 2px; flex: 0 0 auto; }
.cc-title { flex: 1 1 auto; min-width: 0; }
.cc-name { font-weight: 700; font-size: 1.05rem; line-height: 1.25; word-break: break-word; }
.cc-sub { font-size: .78rem; color: var(--c-muted); }
.cc-badges { display: flex; flex-wrap: wrap; gap: 6px; }
.cc-badges .badge { font-weight: 600; white-space: normal; text-align: left; }

.cc-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.cc-actions .btn { display: flex; align-items: center; justify-content: center; gap: 6px; font-weight: 700; border-radius: 12px; min-height: 46px; }
.cc-actions .btn-call { grid-column: 1 / -1; background: #198754; border-color: #198754; color: #fff; font-size: 1.05rem; }
.cc-actions .btn-call:active { background: #146c43; }
.btn-zalo { background-color: #0068ff; border-color: #0068ff; color: #fff; font-weight: 600; }
.btn-zalo:hover, .btn-zalo:active { background-color: #0052cc; border-color: #0052cc; color: #fff; }

.cc-meta { display: grid; gap: 4px; font-size: .88rem; }
.cc-meta div { display: flex; gap: 8px; align-items: flex-start; }
.cc-meta i { width: 16px; margin-top: 3px; color: var(--c-muted); text-align: center; flex: 0 0 auto; }
.cc-meta span { min-width: 0; word-break: break-word; }

.cc-log { background: #f8fafc; border: 1px solid var(--c-line); border-radius: 12px; padding: 8px 10px; font-size: .88rem; }
.cc-log .last { line-height: 1.4; word-break: break-word; }
.cc-log .when { font-weight: 700; color: var(--c-primary); margin-right: 4px; }
.cc-log details summary { cursor: pointer; color: var(--c-primary); font-weight: 600; font-size: .82rem; margin-top: 6px; list-style: none; }
.cc-log details summary::-webkit-details-marker { display: none; }
.call-history-item { padding: 6px 0; border-top: 1px dashed var(--c-line); line-height: 1.4; word-break: break-word; }
.call-history-item:first-child { border-top: 0; }

.cc-foot { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.select-status { font-weight: 700; border-radius: 10px; cursor: pointer; }
.cc-foot .btn-edit { font-weight: 700; border-radius: 12px; min-height: 44px; padding-left: 16px; padding-right: 16px; }

/* Thanh chia data hàng loạt */
.bulk-bar {
    display: none; position: fixed; z-index: 1035; left: 10px; right: 10px;
    bottom: calc(var(--bottom-h) + var(--safe-b) + 10px);
    background: #fff; border: 1px solid #b6d4fe; border-radius: 16px; padding: 10px 12px;
    box-shadow: 0 8px 28px rgba(13,110,253,.25);
}
.bulk-bar.show { display: block; }
.bulk-bar .row-inner { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.bulk-bar select { flex: 1 1 160px; }
@media (min-width: 992px) { .bulk-bar { left: 50%; right: auto; transform: translateX(-50%); bottom: 20px; width: min(720px, 90vw); } }

.empty-state { text-align: center; padding: 48px 16px; color: var(--c-muted); }
.empty-state i { font-size: 2.2rem; display: block; margin-bottom: 10px; opacity: .6; }

/* ---------- Modal ---------- */
.modal-content { border: 0; border-radius: 18px; }
@media (max-width: 767.98px) {
    .modal-fullscreen-md-down .modal-content { border-radius: 0; }
}
.modal-header { position: sticky; top: 0; background: #fff; z-index: 2; }
.modal-footer { position: sticky; bottom: 0; background: #fff; z-index: 2; padding-bottom: calc(.75rem + var(--safe-b)); }
.modal-footer .btn { flex: 1 1 0; }

.info-box { border: 1px solid var(--c-line); border-radius: 12px; background: #f8fafc; margin-bottom: 12px; }
.info-box > summary { padding: 12px; font-weight: 700; cursor: pointer; list-style: none; display: flex; justify-content: space-between; align-items: center; }
.info-box > summary::-webkit-details-marker { display: none; }
.info-box[open] > summary { border-bottom: 1px solid var(--c-line); }
.info-box .inner { padding: 12px; }

.call-step { background: #f8fafc; border: 1px solid var(--c-line); border-left: 4px solid var(--c-primary); border-radius: 12px; margin-bottom: 10px; }
.call-step .call-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 10px 12px; cursor: pointer; }
.call-step .call-head h6 { margin: 0; font-weight: 700; color: var(--c-primary); }
.call-step .call-head .tag { font-size: .75rem; color: var(--c-muted); font-weight: 600; }
.call-step .call-body { padding: 0 12px 12px; }
.call-step.is-collapsed .call-body { display: none; }
.call-step.is-collapsed { border-left-color: #cbd5e1; }
.call-step.is-collapsed .call-head h6 { color: var(--c-muted); }
.call-step.has-data:not(.is-collapsed) { border-left-color: #198754; }

.btn-recording { background-color: #dc3545 !important; border-color: #dc3545 !important; color: #fff !important; animation: pulse-red 1.2s infinite; }
@keyframes pulse-red { 0% { opacity: 1; } 50% { opacity: .55; } 100% { opacity: 1; } }

/* ---------- Các trang còn lại: bảng -> thẻ trên mobile ---------- */
.kpi-card { border-radius: var(--radius) !important; }
.card-custom, .main-card { border-radius: var(--radius) !important; }

@media (max-width: 767.98px) {
    .card-custom, .main-card { padding: 12px !important; }

    table.table:not(.no-cards) { display: block; border: 0; }
    table.table:not(.no-cards) thead { display: none; }
    table.table:not(.no-cards) tbody { display: block; }
    table.table:not(.no-cards) tr {
        display: block; margin-bottom: 10px; padding: 6px 12px;
        background: #fff; border: 1px solid var(--c-line); border-radius: 12px;
        box-shadow: 0 1px 4px rgba(17,24,39,.04);
    }
    table.table:not(.no-cards) td {
        display: flex; justify-content: space-between; align-items: center; gap: 12px;
        width: auto; padding: 7px 0 !important; border: 0 !important; border-bottom: 1px dashed var(--c-line) !important;
        text-align: right !important; white-space: normal !important; background: transparent !important; box-shadow: none !important;
        min-width: 0 !important; max-width: none !important;
    }
    table.table:not(.no-cards) td:last-child { border-bottom: 0 !important; }
    table.table:not(.no-cards) td[data-label]::before {
        content: attr(data-label); flex: 0 0 38%; max-width: 45%; text-align: left;
        font-size: .78rem; font-weight: 700; color: var(--c-muted);
    }
    table.table:not(.no-cards) td[colspan] { display: block; text-align: center !important; }
    .table-responsive { overflow: visible; }
}

/* ---------- Đăng nhập ---------- */
.login-page {
    min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center;
    padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b));
    background: linear-gradient(160deg, #0b3d91, #0d6efd 55%, #4aa3ff);
}
.login-card { width: 100%; max-width: 400px; background: #fff; border-radius: 22px; padding: 28px 22px; box-shadow: 0 18px 50px rgba(0,0,0,.28); }
.login-logo {
    width: 68px; height: 68px; border-radius: 20px; margin: 0 auto 10px; display: grid; place-items: center;
    background: linear-gradient(135deg, #0d6efd, #0a4fc4); color: #fff; font-size: 1.8rem; box-shadow: 0 8px 20px rgba(13,110,253,.4);
}
.login-card .form-control { min-height: 50px; font-size: 16px; }
.login-card .btn-primary { min-height: 50px; border-radius: 12px; font-weight: 700; }
.login-card .input-group-text { border-radius: 10px 0 0 10px; }
.login-card .form-control { border-radius: 0 10px 10px 0; }

/* Chế độ standalone (đã cài PWA) */
@media (display-mode: standalone) {
    .hide-in-app { display: none !important; }
}

/* ---------- Thanh nhắc lịch gọi ---------- */
.due-bar {
    position: sticky; top: calc(var(--appbar-h) + var(--safe-t)); z-index: 1028;
    margin: 0 0 10px; border-radius: 14px; overflow: hidden;
    background: #fff; border: 1px solid #f5c2c7; box-shadow: 0 4px 14px rgba(220, 53, 69, .15);
}
.due-bar.only-today { border-color: #ffe69c; box-shadow: 0 4px 14px rgba(255, 193, 7, .2); }
.due-bar > summary {
    display: flex; align-items: center; gap: 10px; padding: 10px 12px; cursor: pointer; list-style: none;
    background: #dc3545; color: #fff; font-size: .92rem;
}
.due-bar.only-today > summary { background: #f59e0b; color: #1f2937; }
.due-bar > summary::-webkit-details-marker { display: none; }
.due-ic { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: rgba(255,255,255,.22); flex: 0 0 auto; animation: due-ring 2.4s ease-in-out infinite; transform-origin: 50% 10%; }
.due-text { flex: 1 1 auto; min-width: 0; }
.due-chev { transition: transform .2s; }
.due-bar[open] .due-chev { transform: rotate(180deg); }
.due-list { max-height: 48vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.due-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px 8px 12px; border-top: 1px solid var(--c-line); }
.due-main { flex: 1 1 auto; min-width: 0; color: var(--c-text); text-decoration: none; display: block; }
.due-name { display: block; font-weight: 700; line-height: 1.25; word-break: break-word; }
.due-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: .78rem; margin-top: 2px; }
.due-call { flex: 0 0 auto; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; background: #198754; color: #fff; text-decoration: none; }
.due-call:active { background: #146c43; }
.due-more { padding: 8px 12px; font-size: .78rem; color: var(--c-muted); border-top: 1px solid var(--c-line); text-align: center; }
@keyframes due-ring { 0%, 70%, 100% { transform: rotate(0); } 75% { transform: rotate(14deg); } 80% { transform: rotate(-12deg); } 85% { transform: rotate(8deg); } 90% { transform: rotate(-6deg); } }
@media (prefers-reduced-motion: reduce) { .due-ic { animation: none; } }
@media (min-width: 992px) { .due-bar { max-width: 760px; } }
