:root {
  color-scheme: light;
  --bg: #f5f3ff;
  --panel: #ffffff;
  --border: #e4defc;
  --text: #211a3d;
  --muted: #7a7397;
  --accent: #7c3aed;
  --accent2: #ec4899;
  --grad: linear-gradient(135deg, #7c3aed 0%, #ec4899 100%);
  --green-bg: #dcfce7; --green-fg: #15803d;
  --amber-bg: #fef3c7; --amber-fg: #b45309;
  --red-bg: #fee2e2; --red-fg: #dc2626;
  --gray-bg: #f1eefc; --gray-fg: #6b6486;
  --row-hover: #f7f4ff;
  --row-editing: #fff7ed;
  --table-fs: 13px;
  --purple-bg: #ede9fe; --purple-fg: #6d28d9;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15101f;
    --panel: #1e1830;
    --border: #322a4a;
    --text: #ede9fe;
    --muted: #a094c2;
    --accent: #a78bfa;
    --accent2: #f472b6;
    --grad: linear-gradient(135deg, #a78bfa 0%, #f472b6 100%);
    --green-bg: #113322; --green-fg: #4ade80;
    --amber-bg: #3a2a0c; --amber-fg: #fbbf24;
    --red-bg: #3a1414; --red-fg: #f87171;
    --gray-bg: #241c38; --gray-fg: #a094c2;
    --row-hover: #241c38;
    --row-editing: #332614;
    --purple-bg: #2f2350; --purple-fg: #c4b5fd;
  }
}
:root[data-theme="dark"] {
  --bg: #15101f; --panel: #1e1830; --border: #322a4a; --text: #ede9fe; --muted: #a094c2;
  --accent: #a78bfa; --accent2: #f472b6;
  --grad: linear-gradient(135deg, #a78bfa 0%, #f472b6 100%);
  --green-bg: #113322; --green-fg: #4ade80;
  --amber-bg: #3a2a0c; --amber-fg: #fbbf24;
  --red-bg: #3a1414; --red-fg: #f87171;
  --gray-bg: #241c38; --gray-fg: #a094c2;
  --row-hover: #241c38;
  --row-editing: #332614;
  --purple-bg: #2f2350; --purple-fg: #c4b5fd;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  padding: 16px;
  padding-inline: max(16px, env(safe-area-inset-left, 0px)) max(16px, env(safe-area-inset-right, 0px));
}
.topbar {
  background: var(--grad); border-radius: 14px; padding: 16px 20px; margin-bottom: 14px;
  box-shadow: 0 8px 24px -12px rgba(124, 58, 237, 0.5);
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
}
.topbar h1 { margin: 0 0 4px; font-size: 21px; color: #fff; }
.topbar-sub { color: rgba(255,255,255,0.85); font-size: 12px; }
.theme-toggle {
  background: rgba(255,255,255,0.18); border: 1px solid rgba(255,255,255,0.4); color: #fff;
  border-radius: 999px; padding: 8px 16px; flex-shrink: 0;
}
.theme-toggle:hover { background: rgba(255,255,255,0.28); }
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 14px;
  box-shadow: 0 2px 10px -6px rgba(76, 29, 149, 0.15);
}
.panel-sub { padding: 12px 0 18px; border-bottom: 1px dashed var(--border); margin-bottom: 14px; }
.panel-sub:last-child { border-bottom: none; margin-bottom: 0; }
.panel h2 { margin: 0 0 10px; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.linklike { background: none; border: none; color: var(--accent); cursor: pointer; font-size: 12px; padding: 0; font-weight: 600; }
.sync-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
}
.sync-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
input, select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 9px;
  color: var(--text);
  font-size: 13px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.sync-actions { margin-top: 10px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
button {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 999px;
  padding: 7px 14px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: transform 0.06s ease, filter 0.15s ease;
}
button.primary { background: var(--grad); border-color: transparent; color: #fff; }
button:hover { filter: brightness(1.04); }
button:active { transform: scale(0.97); }
.muted { color: var(--muted); font-size: 12px; }
.count-badge {
  font-size: 16px;
  font-weight: 800;
  color: var(--accent);
  background: var(--row-hover);
  padding: 4px 12px;
  border-radius: 999px;
  white-space: nowrap;
}
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px 0 4px;
}
.pager #pageInfo { font-size: 13px; font-weight: 600; color: var(--text); min-width: 160px; text-align: center; }
.pager button:disabled { opacity: 0.4; cursor: default; }
.hint { color: var(--muted); font-size: 11px; margin: 8px 0 0; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }
.filters input[type="text"] { min-width: 220px; flex: 1; }
.spacer { flex: 1; }
.table-wrap { overflow-x: auto; border-radius: 10px; }
table { border-collapse: collapse; width: 100%; min-width: 1150px; font-size: var(--table-fs); }
th, td { border-bottom: 1px solid var(--border); padding: 9px 10px; text-align: left; vertical-align: top; }
th {
  color: #fff; font-weight: 700; font-size: calc(var(--table-fs) - 2px); position: sticky; top: 0;
  background: var(--grad); white-space: nowrap;
}
th:first-child { border-top-left-radius: 8px; }
th:last-child { border-top-right-radius: 8px; }
td { font-size: var(--table-fs); }
tbody tr { transition: background 0.1s ease; }
tbody tr:hover { background: var(--row-hover); }
tbody tr.editing { background: var(--row-editing); }
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.9em; font-weight: 700; white-space: nowrap; }
.wa-green { background: var(--green-bg); color: var(--green-fg); }
.wa-amber { background: var(--amber-bg); color: var(--amber-fg); }
.wa-red { background: var(--red-bg); color: var(--red-fg); }
.wa-gray { background: var(--gray-bg); color: var(--gray-fg); }
.sub { color: var(--muted); font-size: 0.85em; }
.phone-link { color: var(--accent); text-decoration: none; cursor: pointer; font-weight: 700; }
select.status-select { font-size: 12px; padding: 4px 6px; }
textarea.issue-box {
  width: 100%; min-width: 160px; min-height: 40px; resize: vertical;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; color: var(--text);
  font-size: 12px; padding: 5px 7px; font-family: inherit;
}
.row-actions button { font-size: 12px; padding: 5px 9px; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
tr[data-status="Đã hoàn công"], tr[data-status="Đã thoái trả"] { opacity: 0.65; }
[hidden] { display: none !important; }

/* ---- Column sizing / typography ---- */
.col-wide { min-width: 230px; }
.col-tenkh { min-width: 300px; }
.col-magd { min-width: 150px; max-width: 170px; }
.cust-name { font-size: 1.15em; font-weight: 800; }
.cust-addr { color: var(--muted); font-size: 1em; margin-top: 3px; }
.ma-gd { font-size: 1em; }
.ma-tb { font-size: 1.05em; color: var(--muted); margin-top: 6px; font-weight: 400; font-style: italic; }
.survey-tag {
  display: inline-block;
  background: #ff1f3d;
  color: #fff;
  font-weight: 800;
  font-size: 0.78em;
  letter-spacing: 0.02em;
  padding: 2px 8px;
  border-radius: 999px;
  margin-top: 4px;
  white-space: nowrap;
  box-shadow: 0 0 0 1px rgba(255, 31, 61, 0.25);
}
.assignee-name { white-space: nowrap; }

/* ---- Copy-on-click cells ---- */
.copyable { cursor: copy; border-radius: 4px; }
.copyable:hover { background: var(--gray-bg); }

/* ---- Quick stat chips ---- */
.quickstats { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-group + .chip-group { margin-left: 6px; padding-left: 14px; border-left: 2px dashed var(--border); }
.chip {
  border-radius: 999px; padding: 7px 14px; font-size: 12px; font-weight: 700;
  border: 1px solid var(--border); background: var(--gray-bg); color: var(--gray-fg);
  cursor: pointer; display: flex; align-items: center; gap: 6px;
}
.chip b { font-size: 13px; }
.chip.active { background: var(--grad); color: #fff; border-color: transparent; box-shadow: 0 4px 12px -4px rgba(124,58,237,0.5); }
.chip-warn.wa-amber { background: var(--amber-bg); color: var(--amber-fg); border-color: transparent; }
.chip-warn.wa-red { background: var(--red-bg); color: var(--red-fg); border-color: transparent; }
.chip-warn.wa-purple { background: var(--purple-bg); color: var(--purple-fg); border-color: transparent; }
.chip-warn.active { background: var(--grad) !important; color: #fff !important; }
.chip-clear { background: transparent; color: var(--muted); border-style: dashed; }

.inline-label { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.inline-label input[type="date"] { font-size: 12px; padding: 5px 7px; }
.inline-label input[type="range"] { width: 90px; accent-color: var(--accent); }

/* ---- Column picker ---- */
.col-picker {
  display: flex; flex-wrap: wrap; gap: 10px; background: var(--gray-bg); border-radius: 10px;
  padding: 10px 12px; margin-bottom: 10px; font-size: 12px;
}
.col-picker label { display: flex; align-items: center; gap: 5px; cursor: pointer; }

/* ---- Bulk action bar ---- */
.bulkbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  background: var(--amber-bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; margin-bottom: 10px; font-size: 12px;
}
.bulkbar .issue-box { flex: 1; min-width: 200px; min-height: 34px; }
#bulkCount { font-weight: 800; }

/* ---- Row edit mode ---- */
.edit-input {
  width: 100%; font-size: 0.92em; padding: 5px 7px; margin-top: 3px;
  border-radius: 6px;
}
.dvn-pick {
  cursor: pointer; color: var(--accent); font-weight: 600;
  text-decoration: underline dotted; text-underline-offset: 2px;
}

/* ---- Directory picker modal ---- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(33, 26, 61, 0.45); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px;
}
.modal {
  background: var(--panel); border-radius: 16px; border: 1px solid var(--border);
  width: 100%; max-width: 560px; max-height: 80vh; display: flex; flex-direction: column;
  padding: 14px 16px; gap: 10px; box-shadow: 0 20px 60px -20px rgba(33,26,61,0.4);
}
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.dir-results { overflow-y: auto; display: flex; flex-direction: column; gap: 5px; }
.dir-row {
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; cursor: pointer; font-size: 12px;
}
.dir-row:hover { background: var(--row-hover); border-color: var(--accent); }
.dir-row .name { font-weight: 700; }
.dir-row .org { color: var(--muted); }
.role-pill { font-size: 10px; padding: 1px 7px; border-radius: 999px; background: var(--green-bg); color: var(--green-fg); margin-left: 6px; font-weight: 700; }

/* ---- Tabs ---- */
.tabbar { display: flex; gap: 4px; margin-bottom: 10px; }
.tabbtn {
  background: transparent; border: none; border-bottom: 3px solid transparent;
  border-radius: 8px 8px 0 0; padding: 9px 6px; margin-right: 20px; font-size: 14px; font-weight: 700; color: var(--muted);
}
.tabbtn.active { color: var(--accent); border-bottom-color: var(--accent); }
.tabbtn:hover { background: var(--gray-bg); }

/* ---- Danh bạ: dòng nhóm Tỉnh/TP > Trung tâm/Tổ-Ban ---- */
#dirTbl .dir-group-tinh td {
  background: var(--grad); color: #fff; font-weight: 800; padding: 8px 10px;
}
#dirTbl .dir-group-sub td {
  background: var(--gray-bg); color: var(--gray-fg); font-weight: 700; padding: 6px 10px; font-size: 0.92em;
}

/* Huy hiệu cấp bậc lãnh đạo trong Danh bạ — đậm dần theo cấp, từ cao xuống thấp. */
.rank-badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.85em; font-weight: 800; white-space: nowrap; }
.rank-0 { background: #4c1d95; color: #fff; }
.rank-1 { background: #6d28d9; color: #fff; }
.rank-2 { background: #7c3aed; color: #fff; }
.rank-3 { background: #a78bfa; color: #2e1065; }
.rank-4 { background: #c4b5fd; color: #3730a3; }
.rank-5 { background: #ede9fe; color: #6d28d9; }

/* ---- Nhật ký đồng bộ ---- */
.sync-log {
  margin-top: 10px; background: #12101a; color: #d9d4f0; border-radius: 10px;
  padding: 12px 14px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px; line-height: 1.6; white-space: pre-wrap; word-break: break-word;
  max-height: 320px; overflow-y: auto; border: 1px solid #2a2440;
}
.sync-log .ok { color: #4ade80; }
.sync-log .warn { color: #fbbf24; }
.sync-log .bad { color: #f87171; }
.sync-log .dim { color: #8b83ad; }

/* ---- Google Sheet auto-import ---- */
.gsheet-list { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.gsheet-row {
  display: flex; align-items: center; gap: 10px; border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; font-size: 12px;
}
.gsheet-row .name { font-weight: 700; }
.gsheet-row .muted { flex: 1; }

/* ---- Report ---- */
.report-cards { display: flex; flex-wrap: wrap; gap: 10px; }
.report-card {
  flex: 1; min-width: 130px; border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; background: var(--gray-bg);
}
.report-card .n { font-size: 26px; font-weight: 900; background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.report-card .l { font-size: 12px; color: var(--muted); margin-top: 2px; font-weight: 600; }
.progress-bar { background: var(--gray-bg); border-radius: 999px; height: 7px; overflow: hidden; margin-top: 4px; }
.progress-bar > div { background: var(--grad); height: 100%; }

/* ---- Toast ---- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--grad); color: #fff; padding: 9px 18px; border-radius: 999px;
  font-size: 12px; font-weight: 700; z-index: 100; box-shadow: 0 8px 24px -8px rgba(124,58,237,0.6);
}
