@charset "UTF-8";
/* ===================================================================
   SMILE UP MANAGER — “Attio / Height” design system
   Một file duy nhất, thay thế hoàn toàn styles.css.
   Không @import, không phụ thuộc thư viện. Inter được nạp qua <link>.

   NGUYÊN TẮC
   1. Dày đặc nhưng thở được: hàng danh sách 44–52px, badge 20px, chữ 13.5px
      trên desktop / 16px cho ô nhập trên mobile (chống iOS auto-zoom).
   2. Mọi màu đi qua token. Dark mode chỉ ghi đè token, không viết lại layout.
   3. Không bao giờ tràn ngang: mọi flex/grid item đều min-width:0,
      bảng rộng cuộn trong khung riêng, không đẩy scroll ra <body>.
   4. Tiếng Việt có dấu: line-height >= 1.35 ở mọi chỗ có chữ hoa,
      không dùng text-transform:uppercase cho nhãn tiếng Việt.
   5. CÁC RULE .rp-* ĐƯỢC BỌC BỞI TỔ TIÊN .rp-main (độ đặc hiệu 0,2,x)
      để thắng khối <style> inline còn sót trong report.html (0,1,x).
      Khi khối inline đó bị xoá, các rule này vẫn đúng nguyên.
   =================================================================== */

/* ===================================================================
   1. TOKENS
   =================================================================== */

:root {
  /* --- Thương hiệu (giữ nguyên tên biến vì JS/inline style đang dùng) --- */
  --brand-navy: #132573;
  --brand-blue: #0086B2;            /* nền / viền / thanh bar */
  --brand-ink: #01678A;             /* chữ brand trên nền sáng — 6.3:1 */
  --brand-dark: #2A3D53;
  --brand-tint: #E6F5FA;            /* nền nhạt cho trạng thái chọn */
  --brand-gradient: linear-gradient(135deg, #0086B2 0%, #12B0C9 100%);
  --brand-gradient-hover: linear-gradient(135deg, #007399 0%, #0E9BB2 100%);
  --brand-gradient-soft: linear-gradient(135deg, #EAF7FB 0%, #EDF3FB 100%);

  /* --- Màu chức năng: 3 lớp (đặc / nền / chữ) ---------------------------
     Lớp "đặc" dùng làm nền thanh bar (report.js gán inline background).
     Lớp "text" luôn >= 4.5:1 trên lớp "bg" tương ứng.                   */
  --success: #0E9F6E;
  --success-bg: #DCF5EA;
  --success-text: #05603A;

  --warning: #C77700;
  --warning-bg: #FDF1DC;
  --warning-text: #7A4A00;

  --danger: #DC2B2B;
  --danger-bg: #FDE4E4;
  --danger-text: #93140F;

  --info: #0086B2;
  --info-bg: #E1F3F9;
  --info-text: #0A5673;

  --neutral: #64748B;
  --neutral-bg: #EEF1F5;
  --neutral-text: #38424F;

  /* --- Bề mặt --- */
  --bg: #F6F7F9;
  --surface: #FFFFFF;
  --surface-hover: #F2F4F7;
  --surface-sunken: #FAFBFC;
  --border: #E1E5EB;
  --border-light: #EDF0F4;
  --border-strong: #CFD5DE;

  /* --- Chữ --- */
  --text: #101828;                  /* 16.9:1 trên surface */
  --text-secondary: #414B5A;        /* 9.4:1 */
  --text-muted: #5B6676;            /* 5.7:1 trên surface, 5.4:1 trên bg */
  --text-on-accent: #FFFFFF;

  /* --- Focus --- */
  --focus: #0086B2;
  --focus-ring: 0 0 0 3px rgba(0, 134, 178, .18);

  /* --- Bóng: rất nhẹ, kiểu “viền có chiều sâu” --- */
  --shadow-sm: 0 1px 1px rgba(16, 24, 40, .04);
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .04);
  --shadow-md: 0 2px 4px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .07);
  --shadow-lg: 0 8px 16px rgba(16, 24, 40, .08), 0 24px 48px rgba(16, 24, 40, .12);
  --shadow-glow: 0 0 0 4px rgba(0, 134, 178, .12);
  --shadow-overlay: rgba(16, 24, 40, .45);

  /* --- Bo góc: 6–10px là “xương sống”, 12–16 chỉ cho tấm lớn --- */
  --radius-sm: 6px;
  --radius: 8px;
  --radius-md: 10px;                /* KHAI BÁO BẮT BUỘC: lightbox ảnh dùng */
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-full: 999px;

  /* --- Nhịp khoảng cách 4px --- */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;

  /* --- Thang chữ --- */
  --fs-micro: 10.5px;
  --fs-xs: 11.5px;
  --fs-sm: 12.5px;
  --fs-base: 13.5px;
  --fs-md: 15px;
  --fs-lg: 17px;
  --fs-xl: 20px;
  --fs-2xl: 25px;
  --lh-tight: 1.3;
  --lh-base: 1.5;

  /* Cỡ chữ ô nhập: 16px trên mobile để iOS không tự phóng to.
     Chỉ hạ xuống 13.5px từ 768px trở lên (không còn nguy cơ zoom). */
  --input-fs: 16px;
  --control-h: 44px;                /* vùng chạm tối thiểu */

  /* --- Chuyển động (JS gán 'toastOut 0.3s var(--ease) forwards') --- */
  --ease: cubic-bezier(.4, 0, .2, 1);
  --ease-out: cubic-bezier(.16, 1, .3, 1);
  --duration: .18s;
  --duration-lg: .26s;

  /* --- Layout --- */
  --topbar-h: 56px;
  --page-max: 1360px;
  --gutter: 16px;

  color-scheme: light dark;
}

/* ===================================================================
   2. DARK MODE — chỉ ghi đè token
   =================================================================== */

@media (prefers-color-scheme: dark) {
  :root {
    --brand-blue: #1FA8D4;
    --brand-ink: #63CDEE;           /* 8.1:1 trên --surface tối */
    --brand-tint: #10303C;
    --brand-gradient: linear-gradient(135deg, #0F87B4 0%, #17ACC4 100%);
    --brand-gradient-hover: linear-gradient(135deg, #1499CB 0%, #23C0D8 100%);
    --brand-gradient-soft: linear-gradient(135deg, #122A34 0%, #16202E 100%);

    --success: #2DBE86;
    --success-bg: #10312A;
    --success-text: #6EE7B7;

    --warning: #E39A18;
    --warning-bg: #38290E;
    --warning-text: #FBC969;

    --danger: #F0605F;
    --danger-bg: #3A1717;
    --danger-text: #FCA5A5;

    --info: #1FA8D4;
    --info-bg: #10303C;
    --info-text: #7DD3F0;

    --neutral: #7B8798;
    --neutral-bg: #212630;
    --neutral-text: #C3CAD5;

    --bg: #0B0D12;
    --surface: #14171D;
    --surface-hover: #1B1F27;
    --surface-sunken: #101319;
    --border: #262B35;
    --border-light: #1E222B;
    --border-strong: #39404D;

    --text: #E9ECF1;                /* 14.2:1 trên surface */
    --text-secondary: #B4BCC9;      /* 8.9:1 */
    --text-muted: #8A93A3;          /* 5.9:1 */
    --text-on-accent: #04121A;

    --focus: #3FBBE3;
    --focus-ring: 0 0 0 3px rgba(63, 187, 227, .28);

    --shadow-sm: 0 1px 1px rgba(0, 0, 0, .4);
    --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 1px 3px rgba(0, 0, 0, .3);
    --shadow-md: 0 2px 4px rgba(0, 0, 0, .5), 0 4px 12px rgba(0, 0, 0, .4);
    --shadow-lg: 0 8px 16px rgba(0, 0, 0, .5), 0 24px 48px rgba(0, 0, 0, .55);
    --shadow-glow: 0 0 0 4px rgba(63, 187, 227, .2);
    --shadow-overlay: rgba(0, 0, 0, .68);
  }
}

/* theme.css được <link> trong cả index.html lẫn report.html nhưng KHÔNG tồn
   tại trong src/ (404). File này đã tự đủ; thẻ link kia nên được gỡ khi triển
   khai. Rule dưới đây chỉ để khẳng định link không được phép chiếm chỗ. */
link[href="theme.css"] { display: none; }

/* ===================================================================
   3. RESET & BASE
   =================================================================== */

*, *::before, *::after { box-sizing: border-box; }

* { margin: 0; padding: 0; }

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scrollbar-gutter: stable;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', system-ui, sans-serif;
  font-size: var(--fs-base);
  line-height: var(--lh-base);
  background: var(--bg);
  color: var(--text);
  min-height: 100svh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  font-feature-settings: 'cv05' 1, 'ss01' 1;
  overflow-x: hidden;               /* chốt chặn cuối: không bao giờ trượt ngang */
}

/* JS đặt body.style.overflow='hidden' khi mở modal — không ghi đè overflow ở
   đây, chỉ chặn rubber-band của iOS trong lúc khoá. */
body[style*="overflow: hidden"] { overscroll-behavior: none; }

img, svg, video { display: block; max-width: 100%; }
svg { flex: 0 0 auto; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
  font-family: inherit;
}

button { background: none; border: 0; cursor: pointer; }

a { color: var(--brand-ink); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { font-weight: 650; line-height: var(--lh-tight); }

strong, b { font-weight: 650; }

::selection { background: rgba(0, 134, 178, .22); color: var(--text); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border: 3px solid transparent;
  background-clip: content-box;
  border-radius: var(--radius-full);
}
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); background-clip: content-box; }
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }

/* Mọi số tiền / ngày dùng bảng số đều rộng */
.stat-value, .visit-date, .visit-phone, .visit-code,
.detail-value, .inline-money, .date-tab-date, .date-tab-count,
.rp-kpi-val, .rp-kpi-sub, .rp-fval, .input-money,
input[type="date"], input[type="time"] {
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum' 1;
}

/* ===================================================================
   4. TOPBAR — thanh công cụ dính, kiểu app gốc
   =================================================================== */

.topbar {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: var(--topbar-h);
  padding: 6px max(var(--gutter), env(safe-area-inset-right))
           6px max(var(--gutter), env(safe-area-inset-left));
  padding-top: max(6px, env(safe-area-inset-top));
  background: transparent;
  border-bottom: 1px solid var(--border);
  isolation: isolate;
}

/* Lớp kính mờ nằm ở PSEUDO-ELEMENT chứ không phải trên .topbar.
   Lý do: backdrop-filter trên .topbar sẽ biến nó thành containing block của
   mọi con position:fixed — #btnAddVisit (FAB ở ≤480px) sẽ bị neo vào topbar
   thay vì vào viewport. Đây là lỗi thật đã phát hiện khi kiểm tra trên máy. */
.topbar::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
}

@supports not (background: color-mix(in srgb, red 50%, blue)) {
  .topbar::before { background: var(--surface); }
}

.topbar-left {
  display: flex;
  align-items: baseline;
  gap: var(--sp-2);
  min-width: 0;
  flex: 1 1 auto;
}

.topbar-logo {
  align-self: center;
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  font-size: 20px;
  line-height: 26px;
  text-align: center;
  border-radius: var(--radius-sm);
  object-fit: contain;
}
.topbar-logo:hover { animation: pulse-glow 1.6s var(--ease) infinite; }

.topbar-title {
  font-size: var(--fs-md);
  font-weight: 680;
  letter-spacing: -.012em;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.topbar-subtitle {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  padding-left: var(--sp-2);
  border-left: 1px solid var(--border);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

/* ===================================================================
   5. BUTTONS
   =================================================================== */

.btn {
  --btn-h: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--btn-h);
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-secondary);
  font-size: var(--fs-base);
  font-weight: 560;
  line-height: 1.35;
  white-space: nowrap;
  cursor: pointer;
  text-decoration: none;
  transition: background-color var(--duration) var(--ease),
              border-color var(--duration) var(--ease),
              color var(--duration) var(--ease),
              box-shadow var(--duration) var(--ease),
              transform var(--duration) var(--ease);
}
.btn:hover { text-decoration: none; }
.btn:focus-visible { outline: none; box-shadow: var(--focus-ring); border-color: var(--focus); }
.btn:active { transform: translateY(1px); }

.btn-label { display: inline; }

/* Primary — gradient thương hiệu, hover trượt vị trí gradient */
.btn.btn-primary {
  background-image: var(--brand-gradient);
  background-size: 160% 100%;
  background-position: 0% 0%;
  color: #FFFFFF;
  border-color: transparent;
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}
.btn.btn-primary:hover {
  background-position: 100% 0%;
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}
.btn.btn-primary:active { transform: translateY(0); box-shadow: var(--shadow-sm); }
.btn.btn-primary svg { stroke: currentColor; }

/* Ghost — viền mảnh, nền surface */
.btn.btn-ghost {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text-secondary);
  box-shadow: var(--shadow-sm);
}
.btn.btn-ghost:hover {
  border-color: var(--brand-blue);
  color: var(--brand-ink);
  background: var(--surface-hover);
}

/* Danger */
.btn-danger {
  background: var(--danger);
  color: #FFFFFF;
  border-color: transparent;
  box-shadow: var(--shadow-sm);
}
.btn-danger:hover { box-shadow: var(--shadow-md); filter: brightness(1.06); }

.btn-sm { --btn-h: 28px; padding: 0 9px; font-size: var(--fs-sm); border-radius: var(--radius-sm); }
.btn-lg { --btn-h: 40px; padding: 0 18px; font-size: var(--fs-md); }

/* Trạng thái vô hiệu — trước đây KHÔNG có, bổ sung cho nút Lưu khi đang gửi */
.btn:disabled,
.btn[aria-disabled="true"],
.btn[disabled] {
  opacity: .5;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
  filter: grayscale(.3);
  pointer-events: none;
}

/* Nút đóng modal */
.btn-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  margin: -4px -4px -4px 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-size: 22px;
  line-height: 1;
  transition: background-color var(--duration) var(--ease), color var(--duration) var(--ease);
}
.btn-close:hover { background: var(--danger-bg); color: var(--danger-text); }
.btn-close:focus-visible { outline: none; box-shadow: var(--focus-ring); }

/* Nút icon-only cần vùng chạm đủ 44px trên mobile */
#btnSettings { width: 34px; padding: 0; }
#btnSettings svg { width: 17px; height: 17px; }

#btnAddVisit { font-weight: 620; }

/* ===================================================================
   6. MAIN CONTENT
   =================================================================== */

.main-content {
  width: 100%;
  max-width: var(--page-max);
  margin: 0 auto;
  padding: var(--sp-4) max(var(--gutter), env(safe-area-inset-right))
           calc(var(--sp-8) + env(safe-area-inset-bottom))
           max(var(--gutter), env(safe-area-inset-left));
}

/* ===================================================================
   7. STATS ROW — dải chỉ số dày đặc
   =================================================================== */

/* 5 ô chỉ số. Chỉ ép đủ 5 cột khi thực sự đủ chỗ (>=1000px); dưới ngưỡng đó
   để lưới tự gấp và cho ô Doanh thu gấp đôi bề ngang, vì '16.900.000.000đ'
   dài gấp ba lần các ô còn lại. */
.stats-row,
#statsRow {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(148px, 1fr));
  gap: var(--sp-2);
  margin-bottom: var(--sp-3);
}
.stat-revenue { grid-column: span 2; }

@media (min-width: 1000px) {
  .stats-row, #statsRow { grid-template-columns: repeat(4, minmax(0, 1fr)) minmax(0, 1.45fr); }
  .stat-revenue { grid-column: auto; }
}

.stat-card {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;                     /* chống tràn */
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  transition: transform var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}

/* gạch màu chạy trên đỉnh thẻ, hiện khi hover */
.stat-card::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 2px;
  background: var(--stat-accent, var(--brand-gradient));
  opacity: 0;
  transition: opacity var(--duration) var(--ease);
}
.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.stat-card:hover::before { opacity: 1; }

.stat-total    { --stat-accent: var(--brand-gradient); }
.stat-arrived  { --stat-accent: var(--info); }
.stat-closed   { --stat-accent: var(--success); }
.stat-fail     { --stat-accent: var(--danger); }
.stat-revenue  { --stat-accent: var(--warning); }

.stat-icon {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  background: var(--neutral-bg);
  font-size: 15px;
  line-height: 1;
}
.stat-total   .stat-icon { background: var(--info-bg); }
.stat-arrived .stat-icon { background: var(--info-bg); }
.stat-closed  .stat-icon { background: var(--success-bg); }
.stat-fail    .stat-icon { background: var(--danger-bg); }
.stat-revenue .stat-icon { background: var(--warning-bg); }

.stat-info {
  display: flex;
  flex-direction: column;
  min-width: 0;                     /* BẮT BUỘC: '16.900.000.000đ' phải co được */
  gap: 1px;
}

.stat-value {
  font-size: var(--fs-lg);
  font-weight: 680;
  letter-spacing: -.02em;
  line-height: 1.2;
  color: var(--text);
  min-width: 0;
  overflow-wrap: anywhere;
}

.stat-label {
  font-size: var(--fs-xs);
  font-weight: 520;
  color: var(--text-muted);
  line-height: 1.35;
  overflow-wrap: anywhere;
}

/* Doanh thu là chuỗi 15 ký tự: hạ cỡ chữ + siết letter-spacing thay vì để tràn */
.stat-revenue .stat-value,
#statRevenue { font-size: clamp(13px, 1.5vw, var(--fs-lg)); letter-spacing: -.035em; }

#statTotal, #statArrived { color: var(--text); }
#statClosed  { color: var(--success-text); }
#statFail    { color: var(--danger-text); }
#statRevenue { color: var(--text); }

/* ===================================================================
   8. FILTER BAR
   =================================================================== */

.filter-bar {
  display: grid;
  grid-template-columns: minmax(200px, 1.4fr) minmax(0, 2fr);
  gap: var(--sp-2);
  align-items: center;
  padding: var(--sp-2);
  margin-bottom: var(--sp-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}

.search-wrapper { position: relative; min-width: 0; }

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--text-muted);
  pointer-events: none;
}

.search-input,
#searchInput {
  width: 100%;
  min-width: 0;
  height: var(--control-h);
  padding: 0 12px 0 32px;
  font-size: var(--input-fs);
  color: var(--text);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease),
              background-color var(--duration) var(--ease);
}
.search-input::placeholder { color: var(--text-muted); }
.search-input:hover { border-color: var(--border-strong); }
.search-input:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--brand-blue);
  box-shadow: var(--focus-ring);
}

.filter-group {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: var(--sp-2);
  min-width: 0;
}

.filter-select,
#filterStatus, #filterResult, #filterSale {
  width: 100%;
  min-width: 0;
  height: var(--control-h);
  padding: 0 32px 0 10px;
  font-size: var(--input-fs);
  color: var(--text);
  background-color: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  -webkit-appearance: none;
  appearance: none;
  text-overflow: ellipsis;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%235B6676' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.filter-select:hover { border-color: var(--border-strong); }
.filter-select:focus {
  outline: none;
  border-color: var(--brand-blue);
  box-shadow: var(--focus-ring);
}
@media (prefers-color-scheme: dark) {
  .filter-select,
  #filterStatus, #filterResult, #filterSale {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238A93A3' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  }
}

/* option do populateSelect sinh — Windows/Linux tôn trọng, macOS bỏ qua */
select > option {
  background: var(--surface);
  color: var(--text);
  padding: 6px 8px;
}

/* ===================================================================
   9. DATE TABS — dải cuộn ngang có gợi ý còn nội dung
   =================================================================== */

.date-tabs-wrapper,
#dateTabsWrapper {
  position: relative;
  margin-bottom: var(--sp-3);
  min-width: 0;
}

.date-tabs,
#dateTabs {
  display: flex;
  align-items: stretch;
  gap: 6px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 24px 6px 2px;
  scroll-snap-type: x proximity;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  /* fade mép phải = tín hiệu “còn tab bên phải” */
  -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 26px), transparent 100%);
  mask-image: linear-gradient(90deg, #000 calc(100% - 26px), transparent 100%);
}
.date-tabs::-webkit-scrollbar { height: 0; }

.date-tab {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-width: 62px;
  min-height: 56px;                 /* vùng chạm iPhone */
  padding: 6px 10px;
  scroll-snap-align: start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  box-shadow: var(--shadow-sm);
  transition: background-color var(--duration) var(--ease), border-color var(--duration) var(--ease),
              color var(--duration) var(--ease), transform var(--duration) var(--ease);
}
.date-tab:hover { background: var(--bg); border-color: var(--border-strong); }
.date-tab:active { transform: scale(.97); }
.date-tab:focus-visible { outline: none; box-shadow: var(--focus-ring); border-color: var(--focus); }

.date-tab[data-date="all"] {
  min-width: 72px;
  margin-right: 2px;
  background: var(--surface-sunken);
}
/* renderDateTabs() in chữ 'Tất cả' HAI lần (.date-tab-day và .date-tab-date).
   Không sửa được JS ⇒ giấu bản trùng, giữ .date-tab-date làm nhãn chính. */
.date-tab[data-date="all"] .date-tab-day { display: none; }

.date-tab-day {
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: .01em;
  color: var(--text-muted);
  line-height: 1.25;
}
.date-tab-date {
  font-size: var(--fs-base);
  font-weight: 660;
  color: var(--text);
  line-height: 1.25;
}
.date-tab-count {
  font-size: var(--fs-micro);
  font-weight: 550;
  color: var(--text-muted);
  line-height: 1.3;
  white-space: nowrap;
}
.date-tab-today {
  font-size: var(--fs-micro);
  font-weight: 650;
  line-height: 1.3;
  color: var(--brand-ink);
  white-space: nowrap;
}

/* tab đang chọn */
.date-tab.active {
  background: var(--brand-gradient);
  border-color: transparent;
  box-shadow: var(--shadow);
}
.date-tab.active .date-tab-day,
.date-tab.active .date-tab-date,
.date-tab.active .date-tab-count,
.date-tab.active .date-tab-today { color: #FFFFFF; }
.date-tab.active .date-tab-day,
.date-tab.active .date-tab-count { color: rgba(255, 255, 255, .82); }

/* nút chọn ngày tuỳ ý */
.date-picker-control {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 2px;
  min-height: 56px;
  padding: 6px 10px;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color var(--duration) var(--ease), background-color var(--duration) var(--ease);
}
.date-picker-control:hover { border-color: var(--brand-blue); background: var(--brand-tint); }
.date-picker-control > span {
  font-size: var(--fs-micro);
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}
.date-picker-control input,
.date-picker-control input[type="date"] {
  width: 108px;
  min-width: 0;
  max-width: 118px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: var(--fs-base);
  font-weight: 600;
  line-height: 1.3;
  -webkit-appearance: none;
  appearance: none;
}
.date-picker-control input[type="date"]::-webkit-date-and-time-value { text-align: left; }
.date-picker-control input[type="date"]::-webkit-calendar-picker-indicator { opacity: .55; padding: 0; }
.date-picker-control input[type="date"]:focus { outline: none; }
.date-picker-control:focus-within { border-color: var(--brand-blue); box-shadow: var(--focus-ring); }

/* ===================================================================
   10. VISIT LIST — hàng dữ liệu kiểu Attio
   =================================================================== */

.visit-list,
#visitList {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.visit-card {
  --accent: var(--border-strong);
  --accent-bg: transparent;
  position: relative;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  transition: box-shadow var(--duration) var(--ease), border-color var(--duration) var(--ease);
}
.visit-card:hover { box-shadow: var(--shadow-md); }

/* 4 biến thể màu theo kết quả tư vấn — dùng custom property nên hai
   modifier cùng lúc (card-chot-fail + has-fail-result) không đá nhau. */
.visit-card.card-da-chot     { --accent: var(--success); --accent-bg: color-mix(in srgb, var(--success) 5%, transparent); }
.visit-card.card-chot-fail   { --accent: var(--danger);  --accent-bg: color-mix(in srgb, var(--danger) 5%, transparent); }
.visit-card.card-tu-choi     { --accent: var(--warning); --accent-bg: color-mix(in srgb, var(--warning) 6%, transparent); }
.visit-card.card-dang-tu-van { --accent: var(--info);    --accent-bg: color-mix(in srgb, var(--info) 5%, transparent); }
.visit-card { background-image: linear-gradient(var(--accent-bg), var(--accent-bg)); }

/* cờ chồng lên: chỉ tăng độ dày dải màu, không đụng tới nền */
.visit-card.has-fail-result { border-left-width: 4px; }

/* trạng thái legacy — toggleVisitDetail() hiện không được gọi nữa */
.visit-card.expanded { box-shadow: var(--shadow-md); border-color: var(--brand-blue); }

.visit-card[data-id] { scroll-margin-top: calc(var(--topbar-h) + 8px); }

/* --- hàng nội dung: grid đặt vị trí tường minh, badge thiếu vẫn giữ cột --- */
.visit-card-header {
  display: grid;
  gap: 3px 10px;
  align-items: center;
  /* chừa chỗ cho nút Chi tiết độc lập; click phần còn lại chọn khách cho AI */
  padding: 9px 52px 9px 12px;
  min-width: 0;
  cursor: pointer;
  transition: background-color var(--duration) var(--ease);
  /* mobile-first: 2 cột, 4 hàng */
  grid-template-columns: minmax(0, 1fr) auto;
}
.visit-card-header:hover { background: var(--surface-hover); }
.visit-card-header:active { background: var(--bg); }
.visit-card-header:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: -3px;
  background: var(--surface-hover);
}

/* Nút chi tiết là hành động riêng, tránh việc modal che panel Trợ lý AI khi
   người dùng chỉ muốn chọn khách.  Không nằm bên trong phần tử role=button. */
.visit-detail-button {
  position: absolute;
  top: 50%;
  right: 10px;
  z-index: 1;
  min-width: 34px;
  min-height: 28px;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  background: var(--surface);
  font: inherit;
  font-size: var(--fs-xs);
  font-weight: 700;
  line-height: 1.2;
  transform: translateY(-50%);
  cursor: pointer;
}
.visit-detail-button:hover { border-color: var(--brand-blue); color: var(--brand-blue); background: var(--info-bg); }
.visit-detail-button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.visit-card-header > * { min-width: 0; }

.visit-name {
  grid-column: 1; grid-row: 1;
  font-size: var(--fs-base);
  font-weight: 650;
  color: var(--text);
  letter-spacing: -.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge-result  { grid-column: 2; grid-row: 1; justify-self: end; }
.visit-phone {
  grid-column: 1; grid-row: 2;
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge-arrival { grid-column: 2; grid-row: 2; justify-self: end; }
.visit-code {
  grid-column: 1; grid-row: 3;
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: .01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.visit-date {
  grid-column: 2; grid-row: 3;
  justify-self: end;
  font-size: var(--fs-xs);
  color: var(--text-muted);
  white-space: nowrap;
}
.visit-sale {
  grid-column: 1 / -1; grid-row: 4;
  font-size: var(--fs-xs);
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* chuỗi rỗng khi chưa gán Sale — hàng 4 biến mất hoàn toàn, không để lại gap */
.visit-sale:empty { display: none; }

/* Khu vực chi tiết gập trong thẻ — trạng thái legacy (toggleVisitDetail).
   Giữ lại để nếu JS bật lại .expanded thì giao diện đã sẵn sàng. */
.visit-detail {
  display: none;
  padding: var(--sp-3) 12px var(--sp-4);
  border-top: 1px solid var(--border-light);
  background: var(--surface-sunken);
  font-size: var(--fs-base);
  color: var(--text-secondary);
}
.visit-card.expanded .visit-detail { display: block; }

.visit-expand-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
  font-size: 10px;
  color: var(--text-muted);
  transition: transform var(--duration) var(--ease);
}
.visit-card.expanded .visit-expand-icon { transform: rotate(180deg); }

/* ===================================================================
   11. BADGES — nhỏ, tinh, cao đều dù có emoji hay không
   =================================================================== */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  max-width: 100%;
  min-height: 20px;
  padding: 1px 7px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: 1.45;                /* emoji không làm badge cao lệch */
  letter-spacing: .002em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: var(--neutral-bg);
  color: var(--neutral-text);
  border-color: color-mix(in srgb, var(--neutral) 22%, transparent);
}

/* hook cấu trúc — không có rule nào trong CSS cũ nhưng JS phụ thuộc vị trí */
.badge.badge-arrival { font-weight: 560; }
.badge.badge-result  { font-weight: 620; }
/* getResultBadgeClass() trả '' cho giá trị lạ → .badge-result trơ vẫn đọc được */

.badge-chua-den   { background: var(--neutral-bg); color: var(--neutral-text); border-color: color-mix(in srgb, var(--neutral) 26%, transparent); }
.badge-da-den     { background: var(--success-bg); color: var(--success-text); border-color: color-mix(in srgb, var(--success) 30%, transparent); }
.badge-khong-den  { background: var(--danger-bg);  color: var(--danger-text);  border-color: color-mix(in srgb, var(--danger) 28%, transparent); }
.badge-hen-lai    { background: var(--warning-bg); color: var(--warning-text); border-color: color-mix(in srgb, var(--warning) 30%, transparent); }
.badge-huy-lich   { background: var(--neutral-bg); color: var(--neutral-text); border-color: color-mix(in srgb, var(--neutral) 26%, transparent); text-decoration: line-through; text-decoration-thickness: 1px; }

.badge-dang-tu-van   { background: var(--info-bg);    color: var(--info-text);    border-color: color-mix(in srgb, var(--info) 28%, transparent); }
.badge-da-chot       { background: var(--success-bg); color: var(--success-text); border-color: color-mix(in srgb, var(--success) 34%, transparent); }
.badge-chot-fail     { background: var(--danger-bg);  color: var(--danger-text);  border-color: color-mix(in srgb, var(--danger) 32%, transparent); }
.badge-bs-pk-tu-choi { background: var(--warning-bg); color: var(--warning-text); border-color: color-mix(in srgb, var(--warning) 34%, transparent); }
.badge-dang-can-nhac { background: var(--brand-tint); color: var(--brand-ink);    border-color: color-mix(in srgb, var(--brand-blue) 30%, transparent); }

/* ===================================================================
   12. EMPTY STATE — JS ép display:block nên KHÔNG dùng flex/grid
   =================================================================== */

.empty-state,
#emptyState {
  max-width: 420px;
  margin: var(--sp-8) auto;
  padding: var(--sp-6) var(--sp-5);
  text-align: center;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
}
.empty-icon {
  font-size: 34px;
  line-height: 1;
  margin: 0 auto 10px;
  opacity: .8;
}
.empty-state h3 {
  font-size: var(--fs-md);
  font-weight: 660;
  color: var(--text);
  margin-bottom: 4px;
}
.empty-state p {
  font-size: var(--fs-base);
  color: var(--text-muted);
  line-height: 1.55;
}
.empty-state .btn.btn-ghost.btn-sm { min-height: 34px; padding: 0 14px; }

/* ===================================================================
   13. MODAL / BOTTOM SHEET
   =================================================================== */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--shadow-overlay);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  padding: var(--sp-5) var(--sp-4);
  overflow: hidden;
}
.modal-overlay.active {
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn var(--duration-lg) var(--ease);
}

.modal {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 720px;
  max-height: min(88svh, 900px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: slideUp var(--duration-lg) var(--ease-out);
}
.modal-settings     { max-width: 560px; }
.modal-confirm      { max-width: 420px; }
.modal.modal-case-detail { max-width: 880px; }

.modal-header {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  flex: 0 0 auto;
  padding: 14px var(--sp-5);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.modal-title,
#modalTitle,
#caseDetailTitle {
  min-width: 0;                     /* nếu không, tiêu đề dài đẩy nút × ra ngoài */
  font-size: var(--fs-md);
  font-weight: 660;
  letter-spacing: -.012em;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.modal-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--sp-5);
}
.modal-body > p { font-size: var(--fs-base); color: var(--text-secondary); margin-bottom: 6px; }
.text-muted { color: var(--text-muted); font-size: var(--fs-sm); }

/* Xếp lớp: 4 overlay thường ở 100, lightbox ảnh phải nằm TRÊN chi tiết ca
   vì nó được mở từ bên trong chi tiết ca (Esc đóng lightbox trước). */
#modalOverlay, #settingsOverlay, #deleteOverlay { z-index: 100; }
#caseDetailOverlay { z-index: 101; }
#failImageLightbox { z-index: 110; }

#visitModal { max-width: 760px; }
#deleteVisitCode { font-weight: 680; color: var(--danger-text); }

/* các nút đóng/huỷ dùng chung một hành vi */
#btnCloseModal, #btnCloseSettings, #btnCloseDelete, #btnCloseCaseDetail { flex: 0 0 auto; }
#btnCancelForm, #btnCancelSettings, #btnCancelDelete { min-width: 84px; }

/* ===================================================================
   14. FORM
   =================================================================== */

#visitForm { display: block; }

.form-section {
  min-width: 0;                     /* fieldset mặc định là min-content → phải ép */
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
  margin-bottom: var(--sp-3);
  overflow: hidden;
}
/* hai fieldset gập được — mỗi nhóm một màu nhấn ở icon legend */
.form-section[data-section] { background: var(--surface-sunken); }
.form-section[data-section="source"]  { --legend-accent: var(--info); }
.form-section[data-section="consult"] { --legend-accent: var(--brand-blue); }
[data-section="source"] .legend-icon,
[data-section="consult"] .legend-icon {
  filter: saturate(1.1);
  border-left: 2px solid var(--legend-accent);
  padding-left: 6px;
}

/* legend: float là cách duy nhất khiến <legend> hành xử như block bình thường
   trên mọi trình duyệt. Nội dung phía sau phải clear. */
.section-legend {
  float: left;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  font-size: var(--fs-base);
  font-weight: 650;
  color: var(--text);
  background: var(--surface);
  border-bottom: 1px solid var(--border-light);
}
.legend-icon { font-size: 14px; line-height: 1; flex: 0 0 auto; }

.section-legend.section-toggle {
  cursor: pointer;
  user-select: none;
  min-height: var(--control-h);
  transition: background-color var(--duration) var(--ease);
}
.section-toggle:hover { background: var(--surface-hover); }
.section-toggle:active { background: var(--bg); }

.toggle-arrow {
  margin-left: auto;
  font-size: 10px;
  color: var(--text-muted);
  transition: transform var(--duration) var(--ease);
}
.form-section-open .toggle-arrow { transform: rotate(180deg); }

/* Vùng gập: dùng grid 0fr→1fr thay cho max-height cố định, nên nội dung
   dài bao nhiêu cũng KHÔNG bị cắt cụt (max-height:1000px trước đây cắt
   mất fieldset ‘Tư vấn, kết quả & Doanh thu’ ở mobile). */
.section-content {
  clear: both;
  display: grid;
  grid-template-rows: 0fr;
  visibility: hidden;               /* loại các ô đang gập khỏi thứ tự Tab */
  transition: grid-template-rows var(--duration-lg) var(--ease),
              visibility 0s linear var(--duration-lg);
}
.section-content > * { overflow: hidden; min-height: 0; }
.form-section-open .section-content {
  grid-template-rows: 1fr;
  visibility: visible;
  transition: grid-template-rows var(--duration-lg) var(--ease), visibility 0s;
}
#sectionSource, #sectionConsult { min-width: 0; }

.form-section > .form-grid { clear: both; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--sp-3);
  padding: var(--sp-4);
  min-width: 0;
}

/* display:block (không flex) — #failReasonGroup bị JS ép display:block,
   nếu dựa vào flex-gap thì ô đó sẽ lệch so với các ô khác. */
.form-group { display: block; min-width: 0; }
.form-group-wide { grid-column: 1 / -1; }

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-secondary);
  line-height: 1.4;
}
.required { color: var(--danger); font-weight: 700; margin-left: 1px; }

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  min-width: 0;
  padding: 0 11px;
  height: var(--control-h);
  font-size: var(--input-fs);
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.form-group textarea {
  height: auto;
  min-height: 76px;
  padding: 9px 11px;
  line-height: 1.5;
  resize: vertical;
}
.form-group select {
  -webkit-appearance: none;
  appearance: none;
  padding-right: 32px;
  cursor: pointer;
  text-overflow: ellipsis;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%235B6676' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}
.form-group input::placeholder,
.form-group textarea::placeholder { color: var(--text-muted); }

.form-group input:hover,
.form-group select:hover,
.form-group textarea:hover { border-color: var(--border-strong); }

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--brand-blue);
  box-shadow: var(--focus-ring);
}

/* validation gốc của trình duyệt (#fldDate, #fldName, #fldPhoneRaw) */
.form-group input:user-invalid,
.form-group select:user-invalid,
.form-group textarea:user-invalid {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 16%, transparent);
}
.form-group input:disabled,
.form-group select:disabled,
.form-group textarea:disabled {
  background: var(--neutral-bg);
  color: var(--text-muted);
  cursor: not-allowed;
  opacity: .75;
}
/* select rỗng (chưa có nhân sự trong danh mục) */
.form-group select:empty { color: var(--text-muted); cursor: not-allowed; }

/* iOS Safari: date/time có bề rộng nội tại rất lớn → phải ép co lại */
input[type="date"], input[type="time"] {
  -webkit-appearance: none;
  appearance: none;
  min-width: 0;
  max-width: 100%;
}
input[type="date"]::-webkit-date-and-time-value,
input[type="time"]::-webkit-date-and-time-value { text-align: left; margin: 0; }

/* readonly — #fldPhoneNorm */
input.input-readonly,
#fldPhoneNorm {
  background: var(--neutral-bg);
  color: var(--text-muted);
  border-color: var(--border-light);
  cursor: not-allowed;
}
input.input-readonly:hover { border-color: var(--border-light); }
input.input-readonly:focus { box-shadow: none; border-color: var(--border-light); }

.input-money,
#fldTotalAmount, #fldPaidAmount, #fldExpectedAmount {
  text-align: right;
  font-weight: 620;
  letter-spacing: .01em;
}

/* nhóm lý do fail: JS bật display:block → chỉ dùng margin, không dùng gap */
#failReasonGroup { grid-column: 1 / -1; }
#failReasonGroup label { margin-bottom: 5px; }
#fldFailReason { border-color: color-mix(in srgb, var(--danger) 35%, var(--border)); }

/* id còn lại trong form — kế thừa .form-group input/select/textarea */
#fldVisitCode, #fldDate, #fldApptTime, #fldCheckinTime, #fldName,
#fldPhoneRaw, #fldBranch, #fldArrivalStatus, #fldSourceGroup, #fldSourceDetail,
#fldMkt, #fldTelesale, #fldSale, #fldDoctor, #fldServiceInterest,
#fldConsultContent, #fldExamContent, #fldConsultResult, #fldServiceClosed,
#fldExpectedDate { min-width: 0; }
#fldConsultContent, #fldExamContent { min-height: 84px; }

.form-actions {
  position: sticky;
  bottom: 0;
  z-index: 2;
  display: flex;
  justify-content: flex-end;
  gap: var(--sp-2);
  padding: var(--sp-3) 0 0;
  margin-top: var(--sp-4);
  background: var(--surface);
  border-top: 1px solid var(--border-light);
}
#btnSaveVisit { min-width: 150px; }

/* ===================================================================
   15. SETTINGS MODAL
   =================================================================== */

.settings-body { padding-top: var(--sp-4); }

.settings-group { margin-bottom: var(--sp-4); min-width: 0; }
.settings-group label {
  display: block;
  margin-bottom: 5px;
  font-size: var(--fs-sm);
  font-weight: 640;
  color: var(--text-secondary);
  line-height: 1.4;
}
.settings-group label small {
  display: inline;
  font-size: var(--fs-xs);
  font-weight: 500;
  color: var(--text-muted);
  white-space: nowrap;
}
.settings-group textarea,
#settMkt, #settTelesale, #settSale, #settDoctors {
  width: 100%;
  min-width: 0;
  padding: 9px 11px;
  font-size: var(--input-fs);
  line-height: 1.55;
  color: var(--text);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: vertical;
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.settings-group textarea:hover { border-color: var(--border-strong); }
.settings-group textarea:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--brand-blue);
  box-shadow: var(--focus-ring);
}
#btnSaveSettings { min-width: 140px; }

/* ===================================================================
   16. DELETE CONFIRM
   =================================================================== */

.modal-confirm .modal-body { padding-bottom: var(--sp-4); }
.modal-confirm .form-actions { position: static; margin-top: var(--sp-5); }
#btnConfirmDelete { min-width: 96px; }

/* ===================================================================
   17. CASE DETAIL + INLINE EDIT
   =================================================================== */

.case-detail-body,
#caseDetailBody { padding: var(--sp-4) var(--sp-5) var(--sp-5); min-width: 0; }

/* app.js không đóng thẻ .detail-content nên các .case-detail-section nằm BÊN
   TRONG nó — layout ở đây không giả định quan hệ anh em. */
.detail-content { min-width: 0; }

.inline-edit-hint,
.case-detail-edit-hint {
  display: block;
  padding: 7px 10px;
  margin-bottom: var(--sp-4);
  font-size: var(--fs-sm);
  line-height: 1.45;
  color: var(--info-text);
  background: var(--info-bg);
  border: 1px solid color-mix(in srgb, var(--info) 24%, transparent);
  border-radius: var(--radius-sm);
}

.detail-section,
.case-detail-section {
  min-width: 0;
  padding: var(--sp-4) 0 0;
  margin-bottom: var(--sp-4);
  border-top: 1px solid var(--border-light);
}
.detail-section:first-of-type { border-top: 0; padding-top: 0; }

.detail-section-title,
.case-detail-section h3,
.case-detail-section > h3 {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 10px;
  font-size: var(--fs-sm);
  font-weight: 700;
  line-height: 1.4;                 /* dấu trên chữ hoa tiếng Việt không bị cắt */
  letter-spacing: .01em;
  color: var(--text-secondary);
  text-transform: none;             /* CỐ Ý: chữ hoa toàn phần làm mất dấu */
}

.detail-grid,
.case-detail-grid {                 /* .case-detail-grid: hook cũ, giữ đồng bộ */
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--sp-3);
  min-width: 0;
}
@media (min-width: 560px) {
  .detail-grid,
  .case-detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.detail-item,
.case-detail-item {
  display: block;
  min-width: 0;
}
.detail-item.wide,
.case-detail-long { grid-column: 1 / -1; }

.detail-label {
  display: block;
  margin-bottom: 4px;
  font-size: var(--fs-xs);
  font-weight: 620;
  line-height: 1.4;
  letter-spacing: .01em;
  color: var(--text-muted);
  text-transform: none;
}

.detail-value {
  display: block;
  font-size: var(--fs-base);
  line-height: 1.5;
  color: var(--text);
  overflow-wrap: anywhere;          /* 'Viện Nha khoa Quốc tế & SmileUp CNC (…)' */
  word-break: break-word;
}
.detail-value.money { font-weight: 660; text-align: right; }
.detail-value.text-long { white-space: pre-wrap; }
.detail-value.empty { color: var(--text-muted); font-style: italic; }

/* --- ô sửa trực tiếp --- */
.inline-editor {
  width: 100%;
  min-width: 0;
  height: 38px;
  padding: 0 10px;
  font-size: var(--input-fs);
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease),
              background-color var(--duration) var(--ease);
}
.inline-editor:hover { border-color: #B7C8DC; background: var(--surface-hover); }
.inline-editor:focus,
.inline-editor:focus-visible {
  outline: none;
  background: var(--surface);
  border-color: var(--brand-blue);
  box-shadow: var(--focus-ring);
}
.inline-editor:user-invalid {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 16%, transparent);
}
.inline-editor:disabled { background: var(--neutral-bg); color: var(--text-muted); cursor: not-allowed; }

input.inline-editor[type="date"],
input.inline-editor[type="tel"] { min-width: 0; max-width: 100%; }

.inline-editor-textarea,
textarea.inline-editor.inline-editor-textarea {
  height: auto;
  min-height: 82px;
  padding: 9px 10px;
  line-height: 1.55;
  resize: vertical;
}

.inline-money,
input.inline-editor.inline-money {
  text-align: right;
  font-weight: 640;
  letter-spacing: .01em;
}

select.inline-editor {
  -webkit-appearance: none;
  appearance: none;
  padding-right: 30px;
  cursor: pointer;
  text-overflow: ellipsis;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%235B6676' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
}
select.inline-editor > option { background: var(--surface); color: var(--text); }
/* option đầu tiên có nhãn RỖNG: đánh dấu ô “chưa chọn” bằng viền đứt để lễ tân
   nhận ra đây là trường trống chứ không phải lỗi hiển thị. */
select.inline-editor:has(> option[value=""]:checked) {
  border-style: dashed;
  background-color: var(--surface-sunken);
}

/* --- khối gập trong chi tiết ca (mặc định ĐÓNG) --- */
.case-detail-collapsible,
.case-detail-section.case-detail-collapsible {
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
  overflow: hidden;
}
.case-detail-collapsible > * { display: none; }
.case-detail-collapsible > .case-section-toggle { display: flex; }

.case-section-toggle,
h3.case-section-toggle {
  align-items: center;
  gap: 8px;
  min-height: var(--control-h);
  margin: 0;
  padding: 10px 14px;
  font-size: var(--fs-base);
  font-weight: 650;
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
  user-select: none;
  transition: background-color var(--duration) var(--ease);
}
.case-section-toggle::after {
  content: "▼";
  margin-left: auto;
  font-size: 9px;
  color: var(--text-muted);
  transition: transform var(--duration) var(--ease);
}
h3.case-section-toggle:hover { background: var(--surface-hover); }
h3.case-section-toggle:active { background: var(--bg); }

.case-detail-collapsible.is-open > * { display: block; }
.case-detail-collapsible.is-open > .case-section-toggle { display: flex; border-bottom: 1px solid var(--border-light); }
.case-detail-collapsible.is-open > .detail-grid { display: grid; padding: var(--sp-4); }
.case-detail-collapsible.is-open .case-section-toggle::after { transform: rotate(180deg); }

/* --- gallery ảnh ca fail --- */
.case-image-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  min-width: 0;
  margin-bottom: 10px;
}
.case-image-gallery > div {
  border-radius: var(--radius);
  overflow: visible;
  line-height: 0;
}
.case-image-gallery img {
  width: 92px;
  height: 92px;
  max-width: 100%;
  object-fit: cover;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--neutral-bg);
  cursor: zoom-in;
  transition: transform var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.case-image-gallery img:hover { transform: scale(1.02); box-shadow: var(--shadow-md); }

/* Nút × xoá ảnh có INLINE STYLE cho position/padding/line-height → chỉ can
   thiệp được vào kích thước tối thiểu, màu và bo góc. */
.case-image-gallery .btn.btn-sm.btn-danger {
  min-width: 26px;
  min-height: 26px;
  border-radius: var(--radius-full);
  font-size: 13px;
  font-weight: 700;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .35);
}
.case-image-gallery .btn.btn-sm.btn-danger:hover { filter: brightness(1.12); }

.case-detail-empty,
.fail-image-empty {
  display: block;
  width: 100%;
  padding: var(--sp-4);
  font-size: var(--fs-sm);
  color: var(--text-muted);
  text-align: center;
  background: var(--surface-sunken);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
}

/* nút “Thêm ảnh” bọc <input type=file hidden> */
label.btn.btn-sm {
  position: relative;
  border-color: var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  box-shadow: var(--shadow-sm);
}
label.btn.btn-sm:hover { border-color: var(--brand-blue); color: var(--brand-ink); }
label.btn.btn-sm:focus-within { border-color: var(--focus); box-shadow: var(--focus-ring); }
/* input đang mang [hidden] ⇒ không thể tab tới. Author rule thắng UA rule nên
   ta trả nó về display:block rồi giấu bằng kỹ thuật visually-hidden để bàn
   phím vẫn tới được nút Thêm ảnh. */
.fail-image-input,
input.fail-image-input {
  display: block;
  position: absolute;
  inset: 0 auto auto 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* hook legacy (chưa được JS dùng) — giữ đồng bộ hình thức */
.failure-image-block { margin-top: var(--sp-4); }
.fail-image-list { display: flex; flex-wrap: wrap; gap: 10px; }
.fail-image-note { font-size: var(--fs-xs); color: var(--text-muted); margin-top: 6px; }
.fail-image-preview {
  width: 92px; height: 92px; object-fit: cover;
  border: 1px solid var(--border); border-radius: var(--radius);
}
.fail-image-remove {
  min-width: 26px; min-height: 26px;
  border-radius: var(--radius-full);
  background: var(--danger); color: #FFF; font-weight: 700;
}
.fail-image-upload { margin-top: var(--sp-2); }

.detail-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--sp-2);
  margin-top: var(--sp-4);
  padding-top: var(--sp-3);
  border-top: 1px solid var(--border-light);
}

/* ===================================================================
   18. LIGHTBOX ẢNH
   =================================================================== */

#failImageLightbox,
.image-lightbox {
  background: rgba(4, 8, 14, .88);
  padding: var(--sp-4);
  align-items: center;
}
#failImageLightbox.image-lightbox.active { display: flex; }

.image-lightbox-content {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-2);
  max-width: min(100%, 1100px);
  max-height: 100%;
}

.image-lightbox-close,
#btnCloseFailImage {
  position: absolute;
  top: -6px;
  right: -6px;
  z-index: 2;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: rgba(255, 255, 255, .94);
  color: #101828;
  font-size: 24px;
  line-height: 1;
  box-shadow: var(--shadow-md);
}
.image-lightbox-close:hover { background: #FFFFFF; }
.image-lightbox-close:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(255, 255, 255, .6); }

#failImageLightboxImage {
  max-width: 100%;
  max-height: calc(100svh - 120px);
  object-fit: contain;
  border-radius: var(--radius-md);
  background: #000;
}

.image-lightbox-caption,
#failImageLightboxCaption {
  font-size: var(--fs-sm);
  color: rgba(255, 255, 255, .85);
  text-align: center;
}
/* caption luôn rỗng → không để lại khoảng trắng thừa dưới ảnh */
.image-lightbox-caption:empty { display: none; }

/* ===================================================================
   19. TOAST — tên keyframes 'toastOut' và biến --ease bị JS nhúng inline,
        TUYỆT ĐỐI không đổi tên.
   =================================================================== */

.toast-container,
#toastContainer {
  position: fixed;
  z-index: 200;
  right: max(var(--sp-4), env(safe-area-inset-right));
  bottom: calc(var(--sp-4) + env(safe-area-inset-bottom));
  left: max(var(--sp-4), env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  pointer-events: none;
}

.toast {
  max-width: min(380px, 100%);
  padding: 10px 14px;
  font-size: var(--fs-base);
  font-weight: 560;
  line-height: 1.45;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--neutral);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
  animation: toastIn var(--duration-lg) var(--ease-out);
}
.toast-success { border-left-color: var(--success); }
.toast-error   { border-left-color: var(--danger); }
.toast-warning { border-left-color: var(--warning); }

.toast.toast-success::before { content: "✓ "; color: var(--success-text); font-weight: 800; }
.toast.toast-warning::before { content: "! "; color: var(--warning-text); font-weight: 800; }
.toast-error::before         { content: "× "; color: var(--danger-text);  font-weight: 800; }

/* ===================================================================
   20. KEYFRAMES
   =================================================================== */

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(12px) scale(.99); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px) scale(.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toastOut {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to   { opacity: 0; transform: translateY(6px) scale(.98); }
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(0, 134, 178, .35); }
  50%      { box-shadow: 0 0 0 6px rgba(0, 134, 178, 0); }
}

@keyframes rp-pulse {
  0%, 100% { opacity: .35; }
  50%      { opacity: 1; }
}

/* ===================================================================
   21. TRANG BÁO CÁO (.rp-*)
   Mọi rule bọc bởi .rp-main để thắng khối <style> inline trong report.html.
   =================================================================== */

main.rp-main {
  width: 100%;
  max-width: var(--page-max);
  margin: 0 auto;
  padding: var(--sp-4) max(var(--gutter), env(safe-area-inset-right))
           calc(56px + env(safe-area-inset-bottom))
           max(var(--gutter), env(safe-area-inset-left));
}

/* --- thanh điều khiển --- */
.rp-main .rp-controls {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-3);
  align-items: center;
  padding: var(--sp-2) var(--sp-3);
  margin-bottom: var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}

.rp-main .rp-months {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  min-width: 0;
  flex: 1 1 320px;                  /* toggle không bị đẩy rơi khi chip wrap */
}

/* #monthChips CHÍNH LÀ .rp-months. Trước khi buildMonthChips() chạy nó rỗng —
   ẩn đi để .rp-toggle không bị đẩy lệch trong lúc chờ dữ liệu. */
.rp-main #monthChips:empty { display: none; }

.rp-main #reportBody { min-width: 0; }

.rp-main .rp-chip {
  min-height: 34px;
  padding: 0 12px;
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color var(--duration) var(--ease), border-color var(--duration) var(--ease),
              color var(--duration) var(--ease), transform var(--duration) var(--ease);
}
.rp-main .rp-chip:hover { border-color: var(--brand-blue); color: var(--brand-ink); background: var(--brand-tint); }
.rp-main .rp-chip:active { transform: scale(.96); }
.rp-main .rp-chip:focus-visible { outline: none; box-shadow: var(--focus-ring); border-color: var(--focus); }
.rp-main .rp-chip.active {
  background: var(--brand-gradient);
  border-color: transparent;
  color: #FFFFFF;
  box-shadow: var(--shadow-sm);
}
.rp-main .rp-chip.active:hover { color: #FFFFFF; background: var(--brand-gradient-hover); }

/* checkbox “Ẩn dữ liệu mẫu” */
.rp-main .rp-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  min-height: var(--control-h);
  padding: 0 10px 0 8px;
  font-size: var(--fs-sm);
  font-weight: 560;
  color: var(--text-secondary);
  white-space: nowrap;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: var(--radius);
  transition: background-color var(--duration) var(--ease), border-color var(--duration) var(--ease);
}
.rp-main .rp-toggle:hover { background: var(--surface-hover); border-color: var(--border); }
.rp-main .rp-toggle:focus-within { border-color: var(--focus); box-shadow: var(--focus-ring); }

.rp-main #hideSamples {
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  margin: 0;
  -webkit-appearance: none;
  appearance: none;
  background: var(--surface);
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background-color var(--duration) var(--ease), border-color var(--duration) var(--ease);
}
.rp-main #hideSamples:hover { border-color: var(--brand-blue); }
.rp-main #hideSamples:checked {
  background-color: var(--brand-blue);
  border-color: var(--brand-blue);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='3.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 13l4 4L19 7'/%3E%3C/svg%3E");
  background-size: 13px 13px;
  background-position: center;
  background-repeat: no-repeat;
}
.rp-main #hideSamples:focus-visible { outline: none; box-shadow: var(--focus-ring); }

/* --- trạng thái rỗng / đang tải --- */
.rp-main .rp-empty {
  padding: var(--sp-8) var(--sp-4);
  font-size: var(--fs-base);
  color: var(--text-muted);
  text-align: center;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-md);
}
.rp-main .rp-empty::after {
  content: "•••";
  display: block;
  margin-top: 6px;
  letter-spacing: 4px;
  color: var(--border-strong);
  animation: rp-pulse 1.4s var(--ease) infinite;
}

/* --- banner cảnh báo chất lượng dữ liệu --- */
.rp-main .rp-warn {
  padding: 11px 14px;
  margin-bottom: var(--sp-4);
  font-size: var(--fs-base);
  line-height: 1.55;
  color: var(--warning-text);
  background: var(--warning-bg);
  border: 1px solid color-mix(in srgb, var(--warning) 32%, transparent);
  border-left: 3px solid var(--warning);
  border-radius: var(--radius);
  overflow-wrap: anywhere;
}
.rp-main .rp-warn b { font-weight: 720; color: var(--warning-text); }
/* LỖI API: report.js render .rp-warn làm phần tử DUY NHẤT của #reportBody
   → phân biệt được với cảnh báo chất lượng (luôn kèm .rp-kpis phía sau). */
.rp-main #reportBody > .rp-warn:only-child {
  color: var(--danger-text);
  background: var(--danger-bg);
  border-color: color-mix(in srgb, var(--danger) 34%, transparent);
  border-left-color: var(--danger);
  text-align: center;
  padding: var(--sp-5) var(--sp-4);
}
.rp-main #reportBody > .rp-warn:only-child b { color: var(--danger-text); }

/* --- KPI --- */
.rp-main .rp-kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(168px, 1fr));
  gap: var(--sp-2);
  margin-bottom: var(--sp-4);
}

.rp-main .rp-kpi {
  min-width: 0;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--brand-blue);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.rp-main .rp-kpi.good { border-left-color: var(--success); }
.rp-main .rp-kpi.warn { border-left-color: var(--warning); }
.rp-main .rp-kpi.bad  { border-left-color: var(--danger); }

.rp-main .rp-kpi-val {
  display: block;
  font-size: clamp(18px, 2.1vw, var(--fs-2xl));
  font-weight: 700;
  letter-spacing: -.025em;
  line-height: 1.15;
  color: var(--text);
  overflow-wrap: anywhere;
}
.rp-main .rp-kpi-lbl {
  display: block;
  margin-top: 3px;
  font-size: var(--fs-sm);
  font-weight: 600;
  line-height: 1.35;
  color: var(--text-secondary);
  overflow-wrap: anywhere;
}
.rp-main .rp-kpi-sub {
  display: block;
  margin-top: 2px;
  font-size: var(--fs-xs);
  line-height: 1.4;
  color: var(--text-muted);
  overflow-wrap: anywhere;          /* '16.900.000.000đ' không có điểm ngắt tự nhiên */
}
.rp-main .rp-kpi.good .rp-kpi-val { color: var(--success-text); }
.rp-main .rp-kpi.bad  .rp-kpi-val { color: var(--danger-text); }

/* --- khối nội dung --- */
.rp-main .rp-section {
  min-width: 0;
  padding: var(--sp-4) var(--sp-4);
  margin-bottom: var(--sp-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}
/* Bảng KHÔNG có wrapper trong HTML → biến chính .rp-section thành vùng cuộn
   riêng, vừa chặn tràn ngang ra <body>, vừa giới hạn chiều cao bảng vài trăm
   dòng (Theo nguồn / Theo dịch vụ). */
.rp-main .rp-section:has(> table.rp) {
  overflow: auto;
  max-height: min(74svh, 760px);
  overscroll-behavior: contain;
}

.rp-main .rp-h2 {
  font-size: var(--fs-md);
  font-weight: 680;
  letter-spacing: -.012em;
  color: var(--text);
  margin-bottom: 2px;
}
.rp-main .rp-note {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  line-height: 1.45;
  margin-bottom: var(--sp-3);
}

.rp-main .rp-grid2 {
  display: grid;
  grid-template-columns: minmax(0, 1fr);   /* 1 cột cho tới 1000px */
  gap: var(--sp-3);
}

/* --- bảng --- */
.rp-main table.rp {
  width: 100%;
  min-width: 480px;                 /* hẹp hơn thì .rp-section cuộn ngang */
  border-collapse: collapse;
  font-size: var(--fs-base);
}
.rp-main table.rp th {
  position: sticky;                 /* dính khi bảng vài chục dòng cuộn trong section */
  top: 0;
  z-index: 1;
  padding: 8px;
  text-align: right;
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: .01em;
  text-transform: none;             /* nhãn tiếng Việt không viết hoa toàn phần */
  color: var(--text-muted);
  white-space: nowrap;
  background: var(--surface);
  /* border-collapse:collapse làm mất viền của ô sticky ⇒ dùng inset shadow */
  box-shadow: inset 0 -1px 0 var(--border);
}
.rp-main table.rp th:first-child { text-align: left; }
/* report.js sinh nhãn cột đầu bằng title.replace(/^Theo /,'') ⇒ 'nguồn',
   'dịch vụ' viết thường. Viết hoa ĐÚNG MỘT chữ cái đầu thay vì uppercase cả
   cụm (uppercase làm hỏng dấu tiếng Việt). */
.rp-main table.rp th:first-child::first-letter { text-transform: uppercase; }

.rp-main table.rp td {
  padding: 8px;
  text-align: right;
  color: var(--text-secondary);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  border-bottom: 1px solid var(--border-light);
}
.rp-main table.rp td:first-child {
  text-align: left;
  font-weight: 600;
  color: var(--text);
  white-space: normal;
  overflow-wrap: anywhere;
  min-width: 140px;
  max-width: 280px;
  line-height: 1.4;
}
.rp-main table.rp tr:last-child td { border-bottom: none; }
.rp-main table.rp tbody tr:hover td { background: var(--surface-hover); }

.rp-main table.rp tr.total-row td {
  position: sticky;                 /* dòng Tổng luôn nhìn thấy khi cuộn bảng dài */
  bottom: 0;
  font-weight: 700;
  color: var(--text);
  background: var(--surface-sunken);
  box-shadow: inset 0 2px 0 var(--border);
}
.rp-main table.rp tr.total-row:hover td { background: var(--surface-sunken); }

.rp-main .rp-muted { color: var(--text-muted); font-weight: 500; font-style: italic; }

/* thanh bar trong bảng */
.rp-main .rp-bar-cell { width: 92px; min-width: 92px; padding-right: 4px; }
.rp-main .rp-bar {
  height: 6px;
  min-width: 0;
  border-radius: var(--radius-full);
  background: var(--neutral-bg);
  overflow: hidden;
}
.rp-main .rp-bar > i {
  display: block;
  height: 100%;
  min-width: 3px;                   /* giá trị 2% vẫn nhìn thấy được */
  border-radius: var(--radius-full);
  background: var(--brand-gradient);
  background-size: 92px 100%;       /* gradient không bị nén khi thanh rất ngắn */
}
.rp-main .rp-bar.g > i { background: var(--success); }
.rp-main .rp-bar.r > i { background: var(--danger); }

/* --- phễu --- */
.rp-main .rp-funnel { display: flex; flex-direction: column; gap: 8px; }
.rp-main .rp-frow {
  display: grid;
  grid-template-columns: minmax(0, 132px) minmax(0, 1fr) minmax(0, 118px);
  gap: 10px;
  align-items: center;
  font-size: var(--fs-base);
}
.rp-main .rp-flabel {
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rp-main .rp-ftrack {
  height: 22px;
  min-width: 0;
  background: var(--neutral-bg);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
/* report.js gán background inline (màu hoặc var(--brand-gradient)) */
.rp-main .rp-ftrack > i {
  display: block;
  height: 100%;
  min-width: 3px;                   /* hàng 0% vẫn để lại vệt nhìn thấy */
  border-radius: var(--radius-sm);
}
.rp-main .rp-fval {
  text-align: right;
  color: var(--text-muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.rp-main .rp-fval b { color: var(--text); font-weight: 680; }

/* ===================================================================
   22. RESPONSIVE
   =================================================================== */

/* --- >= 560px: stats 5 cột vẫn ổn, form vẫn 2 cột --- */

/* --- <= 900px: hàng khách chuyển layout --- */
@media (min-width: 640px) and (max-width: 899.98px) {
  .visit-card-header {
    grid-template-columns: 104px minmax(0, 1fr) 118px auto;
    gap: 4px 10px;
    padding: 10px 52px 10px 14px;
  }
  .visit-code   { grid-column: 1; grid-row: 1; }
  .visit-name   { grid-column: 2; grid-row: 1; }
  .visit-phone  { grid-column: 3; grid-row: 1; }
  .badge-result { grid-column: 4; grid-row: 1; justify-self: end; }
  .visit-date   { grid-column: 1; grid-row: 2; justify-self: start; }
  .visit-sale   { grid-column: 2 / 4; grid-row: 2; }
  .badge-arrival{ grid-column: 4; grid-row: 2; justify-self: end; }
}

/* --- >= 900px: hàng dữ liệu 1 dòng, cột cố định nên các thẻ thẳng hàng --- */
@media (min-width: 900px) {
  .visit-card-header {
    grid-template-columns:
      112px                        /* mã lượt: KH-20260729-001 */
      88px                         /* ngày 29/07/2026 */
      minmax(140px, 1.6fr)         /* tên khách */
      120px                        /* SĐT */
      92px                         /* badge đến */
      140px                        /* badge kết quả (chuỗi dài nhất) */
      minmax(0, 1fr);              /* Sale */
    gap: 0 12px;
    padding: 8px 52px 8px 14px;
    min-height: 44px;
  }
  .visit-code   { grid-column: 1; grid-row: 1; }
  .visit-date   { grid-column: 2; grid-row: 1; justify-self: start; }
  .visit-name   { grid-column: 3; grid-row: 1; }
  .visit-phone  { grid-column: 4; grid-row: 1; }
  .badge-arrival{ grid-column: 5; grid-row: 1; justify-self: start; }
  .badge-result { grid-column: 6; grid-row: 1; justify-self: start; }
  .visit-sale   { grid-column: 7; grid-row: 1; }
  .visit-sale:empty { display: block; }   /* giữ cột để các hàng thẳng nhau */
}

/* --- >= 768px: hạ cỡ chữ control (không còn nguy cơ iOS zoom) --- */
@media (min-width: 768px) {
  :root { --input-fs: 13.5px; --control-h: 38px; --gutter: 24px; }
  .main-content { padding-top: var(--sp-5); }
  .modal-body { padding: var(--sp-5) var(--sp-6); }
  .modal-header { padding: 14px var(--sp-6); }
  .case-detail-body, #caseDetailBody { padding: var(--sp-5) var(--sp-6) var(--sp-6); }
  .inline-editor { height: 34px; }
}

/* --- >= 1000px: báo cáo 2 cột --- */
@media (min-width: 1000px) {
  .rp-main .rp-grid2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .rp-main .rp-section { padding: var(--sp-5); }
  main.rp-main { padding-top: var(--sp-5); }
}

/* --- <= 640px: bottom sheet, dải stats 2 cột, báo cáo 1 cột --- */
@media (max-width: 640px) {
  :root { --gutter: 12px; }

  .topbar { gap: var(--sp-2); }
  .topbar-subtitle { display: none; }

  .filter-bar { grid-template-columns: minmax(0, 1fr); }

  /* modal → bottom sheet */
  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal-overlay.active { align-items: flex-end; }
  .modal {
    max-width: 100%;
    max-height: 92dvh;
    margin-top: auto;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    border-bottom: 0;
    padding-top: 8px;
    animation: slideUp var(--duration-lg) var(--ease-out);
  }
  /* thanh kéo */
  .modal::before {
    content: "";
    position: absolute;
    top: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 38px;
    height: 4px;
    border-radius: var(--radius-full);
    background: var(--border-strong);
    z-index: 3;
  }
  .modal-header { padding: 12px var(--sp-4) 12px; }
  .modal-body { padding: var(--sp-4) var(--sp-4) calc(var(--sp-4) + env(safe-area-inset-bottom)); }
  .case-detail-body, #caseDetailBody { padding: var(--sp-4) var(--sp-4) calc(var(--sp-4) + env(safe-area-inset-bottom)); }
  .form-actions {
    padding: var(--sp-3) 0 calc(var(--sp-2) + env(safe-area-inset-bottom));
    margin-top: var(--sp-3);
  }
  .form-actions .btn { flex: 1 1 auto; }

  /* lightbox không được biến thành sheet */
  #failImageLightbox, .image-lightbox { align-items: center; padding: var(--sp-4); }
  .image-lightbox-close, #btnCloseFailImage { top: 0; right: 0; }

  .form-grid { grid-template-columns: minmax(0, 1fr); padding: var(--sp-3); }

  /* báo cáo */
  main.rp-main { padding: var(--sp-3) max(12px, env(safe-area-inset-right))
                          calc(40px + env(safe-area-inset-bottom))
                          max(12px, env(safe-area-inset-left)); }
  .rp-main .rp-section { padding: var(--sp-3); }
  .rp-main .rp-grid2 { grid-template-columns: minmax(0, 1fr); }
  .rp-main .rp-frow {
    grid-template-columns: minmax(0, 104px) minmax(0, 1fr) minmax(0, 96px);
    gap: 8px;
    font-size: var(--fs-sm);
  }
  .rp-main .rp-fval { font-size: var(--fs-xs); }
  .rp-main .rp-toggle { margin-left: 0; }
  .rp-main .rp-kpis { grid-template-columns: repeat(auto-fit, minmax(148px, 1fr)); }
}

/* --- <= 560px: bỏ cột bar trang trí trong bảng, nhường chỗ cho số --- */
@media (max-width: 560px) {
  .rp-main table.rp th:nth-child(2),
  .rp-main .rp-bar-cell { display: none; }
  .rp-main table.rp { min-width: 420px; }
  .rp-main table.rp td:first-child { min-width: 108px; max-width: 160px; }
  .rp-main table.rp td, .rp-main table.rp th { padding-left: 6px; padding-right: 6px; }
}

/* --- <= 480px: FAB, stats 2 cột --- */
@media (max-width: 480px) {
  .stats-row, #statsRow {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
  }
  /* doanh thu chiếm cả hàng: chuỗi 15 ký tự cần đủ bề ngang */
  .stat-revenue { grid-column: 1 / -1; }
  .stat-revenue .stat-value, #statRevenue { font-size: var(--fs-lg); letter-spacing: -.02em; }

  .topbar { min-height: 52px; }
  .topbar-title { font-size: var(--fs-base); }

  /* nút thêm lượt → FAB tròn cố định */
  #btnAddVisit {
    position: fixed;
    right: max(var(--sp-4), env(safe-area-inset-right));
    bottom: calc(var(--sp-4) + env(safe-area-inset-bottom));
    z-index: 70;
    width: 56px;
    height: 56px;
    min-height: 56px;
    padding: 0;
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-lg);
  }
  #btnAddVisit svg { width: 22px; height: 22px; }
  #btnAddVisit .btn-label { display: none; }
  /* nút quay lại của trang báo cáo chỉ giữ mũi tên; các .btn-label khác
     PHẢI giữ nguyên vì nút sẽ rỗng nếu ẩn (Báo cáo / In / Xuất PDF) */
  .topbar-right a[href="/"] .btn-label { display: none; }

  .main-content { padding-bottom: calc(88px + env(safe-area-inset-bottom)); }

  /* toast phải nằm trên FAB, không che nút */
  .toast-container, #toastContainer {
    bottom: calc(84px + env(safe-area-inset-bottom));
    align-items: stretch;
  }
  .toast { max-width: 100%; }

  /* vùng chạm rộng hơn trên iPhone — .btn-sm giữ nguyên cỡ nhỏ */
  #btnSettings { width: 38px; min-height: 38px; }
  .btn:not(.btn-sm) { --btn-h: 38px; }
}

/* --- <= 360px: ép thêm một nấc --- */
@media (max-width: 360px) {
  .visit-card-header { padding: 8px 52px 8px 10px; }
  .date-tab { min-width: 56px; padding: 6px 8px; }
  .date-picker-control input[type="date"] { width: 96px; }
  .rp-main .rp-kpis { grid-template-columns: minmax(0, 1fr); }
}

/* ===================================================================
   23. PRINT
   =================================================================== */

@media print {
  :root {
    --surface: #FFFFFF;
    --bg: #FFFFFF;
    --text: #000000;
    --text-secondary: #1F2937;
    --text-muted: #4B5563;
    --border: #C9CFD8;
    --border-light: #E3E7EC;
  }

  body { background: #FFFFFF; }

  .topbar,
  .filter-bar,
  .date-tabs-wrapper,
  .toast-container,
  .modal-overlay,
  .rp-main .rp-controls,
  .topbar-right,
  #btnAddVisit { display: none !important; }

  .main-content, main.rp-main { padding: 0; max-width: none; }

  .stat-card,
  .visit-card,
  .rp-main .rp-kpi,
  .rp-main .rp-section {
    box-shadow: none;
    border: 1px solid #C9CFD8;
    break-inside: avoid;
    page-break-inside: avoid;
  }

  /* bảng dài phải được chảy qua nhiều trang, không bị kẹt trong khung cuộn */
  .rp-main .rp-section:has(> table.rp) {
    overflow: visible;
    max-height: none;
    break-inside: auto;
    page-break-inside: auto;
  }
  .rp-main table.rp { min-width: 0; }
  .rp-main table.rp thead { display: table-header-group; }
  .rp-main table.rp tr { break-inside: avoid; page-break-inside: avoid; }
  .rp-main table.rp th { position: static; box-shadow: none; }
  .rp-main table.rp tr.total-row td { position: static; box-shadow: none; }
  .rp-main .rp-bar > i, .rp-main .rp-ftrack > i {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .rp-main .rp-h2 { break-after: avoid; page-break-after: avoid; }
}

/* ===================================================================
   24. REDUCED MOTION
   =================================================================== */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
    scroll-behavior: auto !important;
  }
  .stat-card:hover,
  .btn.btn-primary:hover,
  .date-tab:active,
  .rp-main .rp-chip:active,
  .case-image-gallery img:hover { transform: none; }
  .topbar-logo:hover { animation: none; }
  .rp-main .rp-empty::after { animation: none; opacity: .6; }
  .date-tabs, #dateTabs { scroll-snap-type: none; }
}


/* ===================================================================
   Vai trò hành chính: ẩn toàn bộ doanh thu và lương.
   Server cũng đã lược bỏ các trường này khỏi dữ liệu trả về.
   =================================================================== */
body.an-doanh-thu .stat-revenue,
body.an-doanh-thu #statRevenue,
body.an-doanh-thu .stat-card:has(#statRevenue),
body.an-doanh-thu a[href="/report.html"],
body.an-doanh-thu a[href="report.html"],
body.an-doanh-thu .visit-amount,
body.an-doanh-thu .inline-money,
body.an-doanh-thu [data-field="totalAmount"],
body.an-doanh-thu [data-field="paidAmount"],
body.an-doanh-thu [data-field="expectedAmount"],
body.an-doanh-thu [data-field="expectedDate"] {
  display: none !important;
}
body.an-doanh-thu .detail-item:has([data-field="totalAmount"]),
body.an-doanh-thu .detail-item:has([data-field="paidAmount"]),
body.an-doanh-thu .detail-item:has([data-field="expectedAmount"]),
body.an-doanh-thu .detail-item:has([data-field="expectedDate"]) {
  display: none !important;
}
