/* Pharmalink CRM demo: token trước, component sau. */
:root {
  --bg: #f3f6f4; --surface: #ffffff; --surface-2: #e9efec; --line: #d3dcd7; --line-strong: #b7c5bf;
  --ink: #16282b; --muted: #5f716e; --faint: #8b9c98;
  --accent: #0f6e5d; --accent-ink: #ffffff; --accent-soft: #dcefe8; --accent-line: #9fd0c1;
  --amber: #c8841a; --amber-soft: #fbefd8; --amber-ink: #6b4400;
  --good: #2f8f5b; --good-soft: #def3e6; --warn: #b8730d; --warn-soft: #fdf1da; --bad: #c0392b; --bad-soft: #f9e0dc;
  --zalo: #0068ff; --zalo-soft: #e6efff; --sms: #3d7f48; --sms-soft: #e4f2e5;
  --shadow: 0 1px 2px rgba(22, 40, 43, .06), 0 6px 20px rgba(22, 40, 43, .06);
  --radius: 10px; --radius-sm: 6px;
  --font: "Be Vietnam Pro", "Segoe UI", Roboto, system-ui, sans-serif;
  --mono: "IBM Plex Mono", "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1716; --surface: #172120; --surface-2: #1e2b29; --line: #2a3a37; --line-strong: #3b4f4b;
    --ink: #e6eeeb; --muted: #97a9a4; --faint: #6f817d;
    --accent: #4fc1a6; --accent-ink: #0f1716; --accent-soft: #1b3a33; --accent-line: #2f6b5d;
    --amber: #f0b25a; --amber-soft: #3a2c15; --amber-ink: #ffd9a0;
    --good: #5fcf8c; --good-soft: #1b3527; --warn: #f0b25a; --warn-soft: #3a2c15; --bad: #ff7b6b; --bad-soft: #3d1f1b;
    --zalo: #5b9bff; --zalo-soft: #16263d; --sms: #6bbf77; --sms-soft: #16301c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1716; --surface: #172120; --surface-2: #1e2b29; --line: #2a3a37; --line-strong: #3b4f4b;
  --ink: #e6eeeb; --muted: #97a9a4; --faint: #6f817d;
  --accent: #4fc1a6; --accent-ink: #0f1716; --accent-soft: #1b3a33; --accent-line: #2f6b5d;
  --amber: #f0b25a; --amber-soft: #3a2c15; --amber-ink: #ffd9a0;
  --good: #5fcf8c; --good-soft: #1b3527; --warn: #f0b25a; --warn-soft: #3a2c15; --bad: #ff7b6b; --bad-soft: #3d1f1b;
  --zalo: #5b9bff; --zalo-soft: #16263d; --sms: #6bbf77; --sms-soft: #16301c;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.5 var(--font); -webkit-font-smoothing: antialiased; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; text-wrap: balance; }
h1 { font-size: 20px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }
p { margin: 0; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.eyebrow { font-size: 11px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }

/* ---------- khung ---------- */
.app { display: grid; grid-template-columns: 224px minmax(0, 1fr) 360px; grid-template-rows: auto minmax(0, 1fr); height: 100%; }
.topbar { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.brand .mark { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-family: var(--mono); font-size: 13px; }
.brand .sub { font-weight: 500; color: var(--muted); font-size: 12px; }
.clock { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-left: auto; }
.clock .time { font-family: var(--mono); font-size: 13px; padding: 6px 10px; border: 1px dashed var(--amber); background: var(--amber-soft); color: var(--amber-ink); border-radius: var(--radius-sm); }
.sidebar { background: var(--surface); border-right: 1px solid var(--line); padding: 12px 10px; overflow: auto; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav button { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; padding: 8px 10px; border: 0; background: transparent; border-radius: var(--radius-sm); color: var(--ink); }
.nav button:hover { background: var(--surface-2); }
.nav button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line-strong); flex: none; }
.nav button.active .dot { background: var(--accent); }
.nav .badge { margin-left: auto; }
.nav .group { padding: 12px 10px 4px; }
.main { overflow: auto; padding: 20px 24px 40px; }
.phone-panel { background: var(--surface); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }

/* ---------- component ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink); font-weight: 500; line-height: 1.2; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.amber { background: var(--amber-soft); border-color: var(--amber); color: var(--amber-ink); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 8px; font-size: 12px; }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 999px; font-size: 11px; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.badge.amber { background: var(--amber-soft); color: var(--amber-ink); }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.zalo { background: var(--zalo-soft); color: var(--zalo); }
.badge.sms { background: var(--sms-soft); color: var(--sms); }
.tag { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 12px; background: var(--accent-soft); color: var(--accent); border: 1px solid transparent; }
.tag.sys { background: var(--surface-2); color: var(--muted); }
.tag.risk { background: var(--warn-soft); color: var(--warn); }
.tag.task { background: var(--bad-soft); color: var(--bad); }
.tag.voucher { background: var(--amber-soft); color: var(--amber-ink); }
.tag button { border: 0; background: transparent; color: inherit; padding: 0 0 0 2px; line-height: 1; font-size: 13px; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card.flat { box-shadow: none; }
.card + .card { margin-top: 16px; }
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid.three { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.kpi .label { font-size: 12px; color: var(--muted); }
.kpi .value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; margin-top: 4px; }
.kpi .value small { font-size: 13px; font-weight: 500; color: var(--muted); }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.kpi.hot { border-color: var(--amber); background: linear-gradient(180deg, var(--amber-soft), var(--surface)); }
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.page-head p { color: var(--muted); max-width: 64ch; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 11px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); font-weight: 600; white-space: nowrap; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { padding: 24px; text-align: center; color: var(--muted); border: 1px dashed var(--line-strong); border-radius: var(--radius); }

.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; font-weight: 600; color: var(--muted); }
.input, select, textarea { width: 100%; padding: 7px 10px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: var(--surface); }
textarea { min-height: 64px; resize: vertical; line-height: 1.45; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: auto; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }

.funnel { display: flex; flex-direction: column; gap: 6px; }
.funnel .frow { display: grid; grid-template-columns: 150px 1fr 40px; align-items: center; gap: 10px; font-size: 13px; }
.funnel .bar { height: 18px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.funnel .bar i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.funnel .frow:nth-child(5) .bar i, .funnel .frow:nth-child(6) .bar i { background: var(--amber); }

.feed { display: flex; flex-direction: column; gap: 0; }
.feed .item { display: grid; grid-template-columns: 76px 1fr; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.feed .item:last-child { border-bottom: 0; }
.feed .when { font-family: var(--mono); font-size: 11.5px; color: var(--muted); padding-top: 2px; }
.feed .who { font-weight: 600; }
.stripe { border-left: 3px solid var(--line-strong); padding-left: 10px; }
.stripe.message { border-color: var(--zalo); }
.stripe.invoice { border-color: var(--accent); }
.stripe.tag { border-color: var(--accent-line); }
.stripe.automation { border-color: var(--amber); }
.stripe.survey { border-color: var(--amber); }
.stripe.voucher { border-color: var(--amber); }
.stripe.task { border-color: var(--bad); }
.stripe.treatment { border-color: var(--good); }

.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--accent); }
.status-line { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.status-line .led { width: 10px; height: 10px; border-radius: 50%; background: var(--bad); }
.status-line .led.on { background: var(--good); box-shadow: 0 0 0 3px var(--good-soft); }

/* ---------- điện thoại ---------- */
.phone-head { padding: 12px 14px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.phone { margin: 14px; flex: 1; min-height: 0; display: flex; flex-direction: column; border: 6px solid var(--ink); border-radius: 26px; background: var(--surface-2); overflow: hidden; }
.phone .bar { padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; }
.phone .bar .avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 11px; font-weight: 700; }
.chat { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.bubble { max-width: 92%; background: var(--surface); border-radius: 12px; padding: 8px 10px; font-size: 13px; box-shadow: var(--shadow); border-top: 3px solid var(--zalo); }
.bubble.sms { border-top-color: var(--sms); }
.bubble.out { align-self: flex-end; background: var(--accent); color: var(--accent-ink); border-top: 0; }
.bubble .from { display: flex; justify-content: space-between; gap: 8px; font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.bubble .from b { color: var(--zalo); }
.bubble.sms .from b { color: var(--sms); }
.bubble .opts { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.bubble .opts button { text-align: left; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--accent-line); background: var(--accent-soft); color: var(--accent); font-weight: 500; }
.bubble .opts button:hover { background: var(--accent); color: var(--accent-ink); }
.vcode { margin-top: 8px; display: flex; gap: 10px; align-items: center; padding: 8px; border: 1px dashed var(--amber); border-radius: 8px; background: var(--amber-soft); color: var(--amber-ink); }
.vcode .qr { width: 44px; height: 44px; display: grid; grid-template-columns: repeat(7, 1fr); gap: 1px; background: var(--surface); padding: 3px; border-radius: 4px; flex: none; }
.vcode .qr i { background: var(--ink); }
.vcode .qr i.off { background: transparent; }
.vcode b { font-family: var(--mono); font-size: 14px; letter-spacing: .04em; }
.next-msg { padding: 8px 14px 12px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); }
.phone-fab { display: none; }

/* ---------- toast & modal ---------- */
.toasts { position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom, 0px)); display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: var(--radius-sm); box-shadow: var(--shadow); font-size: 13px; max-width: 360px; }
.toast.err { background: var(--bad); color: #fff; }
.modal-bg { position: fixed; inset: 0; background: rgba(10, 20, 20, .45); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal { background: var(--surface); border-radius: var(--radius); max-width: 640px; width: 100%; max-height: 90vh; overflow: auto; padding: 20px; box-shadow: var(--shadow); }
.modal ol { padding-left: 20px; margin: 10px 0 0; display: flex; flex-direction: column; gap: 8px; }
.steps { display: grid; gap: 8px; }
.step { display: grid; grid-template-columns: 110px 70px 1fr; gap: 10px; padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--radius-sm); align-items: start; }
.step .when { font-family: var(--mono); font-size: 12px; }
.step textarea { min-height: 48px; font-size: 13px; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 4px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.cart-line { display: grid; grid-template-columns: 1fr 70px 100px 30px; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.warnbox { padding: 10px 12px; border-radius: var(--radius-sm); background: var(--warn-soft); color: var(--warn); font-size: 13px; }
.okbox { padding: 10px 12px; border-radius: var(--radius-sm); background: var(--good-soft); color: var(--good); font-size: 13px; }
.infobox { padding: 10px 12px; border-radius: var(--radius-sm); background: var(--accent-soft); color: var(--accent); font-size: 13px; }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .app { grid-template-columns: 200px minmax(0, 1fr); }
  .phone-panel { position: fixed; right: 0; top: 0; bottom: 0; width: min(380px, 100%); z-index: 30; box-shadow: var(--shadow); transform: translateX(105%); transition: transform .2s; padding-top: env(safe-area-inset-top, 0px); }
  .phone-panel.open { transform: none; }
  .phone-fab { display: inline-flex; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr); }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--line); padding: 6px 8px; }
  .nav { flex-direction: row; overflow-x: auto; }
  .nav button { white-space: nowrap; width: auto; }
  .nav .group, .nav .badge { display: none; }
  .main { padding: 16px 16px 40px; }
  .funnel .frow { grid-template-columns: 110px 1fr 36px; }
  .step { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) { .phone-panel { transition: none; } }

/* Bổ sung cho bản chạy máy chủ */
body.busy { cursor: progress; }
body.busy .btn { pointer-events: none; opacity: .65; }
.warn-text { color: var(--warn); }
.bad { color: var(--bad); }
.status-line + .status-line { margin-top: 2px; }

/* Mẫu tin ZNS */
tr.sel td { background: var(--accent-soft); }
tr.sel td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
.zns-map { display: grid; gap: 4px; }
.zns-map-row { display: grid; grid-template-columns: 130px 1fr; gap: 8px; align-items: center; }
@media (max-width: 760px) { .zns-map-row { grid-template-columns: 1fr; } }
.tpl-list { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; max-height: 560px; overflow-y: auto; }
.tpl { display: block; width: 100%; text-align: left; padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface); color: var(--ink); }
.tpl:hover { background: var(--surface-2); }
.tpl.sel { border-color: var(--accent); background: var(--accent-soft); }
.tpl-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.tpl-top b { font-size: 13px; line-height: 1.35; }
.tpl-sub { font-size: 11.5px; color: var(--muted); margin-top: 3px; word-break: break-word; }

/* Marketing: trình soạn điều kiện tệp khách hàng */
.cond-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px 8px;
  border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface-2); }
.cond-row select, .cond-row input { font-size: 12.5px; padding: 4px 8px; }
.cond-row select { width: auto; min-width: 120px; max-width: 220px; }
@media (max-width: 760px) { .cond-row select { max-width: 100%; } }
