:root {
  --bg: #F4F5F7;
  --card: #FFFFFF;
  --soft: #F7F8FA;
  --text: #111827;
  --text-2: #374151;
  --muted: #5B6472;
  --border: #E2E5EA;
  --line: #EEF0F3;
  --rev: #2563EB;
  --cost: #F5A25D;
  --pos: #15803D;
  --neg: #DC2626;
  --warn: #B45309;
  --accent: #1D4ED8;
  --accent-soft: #EEF2FF;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; color: inherit; }
a { color: var(--rev); }
:focus-visible { outline: 2px solid var(--rev); outline-offset: 2px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Khung ---------- */
.layout { display: grid; grid-template-columns: 240px minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; height: 100vh;
  background: var(--card); border-right: 1px solid #E7E9ED;
  padding: 24px 16px; display: flex; flex-direction: column; gap: 28px;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 0 8px; }
.brand-logo {
  width: 38px; height: 38px; border-radius: 11px; background: var(--rev); color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 15px; flex: none;
}
.brand-text { display: flex; flex-direction: column; gap: 2px; }
.brand-name { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 12px; color: var(--muted); }

.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
  display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 0 12px;
  border-radius: 12px; color: var(--text-2); font-weight: 500; text-decoration: none; white-space: nowrap;
}
.nav a:hover { background: var(--bg); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.sync-box {
  margin-top: auto; background: var(--bg); border-radius: 14px; padding: 14px;
  display: flex; flex-direction: column; gap: 10px; font-size: 12px;
}
.sync-box h3 { margin: 0; font-size: 12px; font-weight: 600; color: var(--text-2); }
#syncList { display: flex; flex-direction: column; gap: 10px; }
.sync-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; color: var(--muted); }
.sync-row span:last-child { white-space: nowrap; }

.ok { color: var(--pos); font-weight: 600; }
.stale { color: var(--warn); font-weight: 600; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.na { color: var(--muted); }
.strong { font-weight: 600; }

.main { min-width: 0; padding: 28px 32px 56px; display: flex; flex-direction: column; gap: 20px; }
.view { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.foot { font-size: 12px; color: var(--muted); }

/* ---------- Header ---------- */
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 16px; }
.page-title { display: flex; flex-direction: column; gap: 4px; }
.page-head h1 { margin: 0; font-size: 30px; font-weight: 700; letter-spacing: -0.02em; }
.subtitle { font-size: 13px; color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }

.pill {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0 16px;
  border: 1px solid var(--border); background: var(--card); border-radius: 999px; font-size: 13px;
}
.select-pill select {
  appearance: none; -webkit-appearance: none;
  min-height: 44px; max-width: 280px; padding: 0 40px 0 16px;
  border: 1px solid var(--border); border-radius: 999px; font-size: 13px; cursor: pointer;
  text-overflow: ellipsis;
  background: var(--card) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23111827' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") no-repeat right 14px center;
}
.btn-dark {
  display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 18px;
  border: 0; background: var(--text); color: #fff; border-radius: 999px;
  font-size: 13px; font-weight: 600; cursor: pointer;
}
.btn-dark:disabled { opacity: 0.75; cursor: default; }
.spin svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.range-bar {
  display: flex; flex-wrap: wrap; gap: 6px; background: var(--card);
  border-radius: 999px; padding: 6px; align-self: flex-start;
}
.range-bar button {
  min-height: 40px; padding: 0 16px; border: 0; background: transparent; border-radius: 999px;
  font-size: 13px; color: var(--text-2); font-weight: 500; cursor: pointer;
}
.range-bar button:hover { background: var(--bg); }
.range-bar button.on { background: var(--text); color: #fff; font-weight: 600; }
.range-bar button.ghost { color: var(--muted); }

/* ---------- Card chung ---------- */
.card { background: var(--card); border-radius: 22px; padding: 24px 28px; min-width: 0; }
.card h2 { margin: 0; font-size: 18px; font-weight: 700; }
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.card-col { display: flex; flex-direction: column; gap: 16px; }
.note { margin: 0; font-size: 12px; color: var(--muted); line-height: 1.5; }

.btn {
  min-height: 40px; padding: 0 14px; border: 1px solid var(--border); background: var(--card);
  border-radius: 10px; font-size: 13px; cursor: pointer;
}
.btn:hover { background: #F9FAFB; }
.btn-sm { min-height: 36px; padding: 0 12px; }
.btn-lg { min-height: 44px; padding: 0 16px; font-weight: 600; }
.btn-primary {
  min-height: 44px; padding: 0 16px; border: 0; border-radius: 10px;
  font-size: 13px; font-weight: 600; cursor: pointer; background: var(--text); color: #fff;
}
.btn-primary.muted { background: #F1F2F5; color: var(--text-2); }
.btn-primary.compact { min-height: 40px; }

/* ---------- Dashboard ---------- */
.kpis {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 24px; padding: 28px 32px;
}
.kpi { display: flex; flex-direction: column; gap: 8px; }
.kpi-label { font-size: 14px; font-weight: 600; color: var(--text-2); }
.kpi-value { font-size: 38px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.kpi-note { font-size: 12px; color: var(--muted); }

.subkpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.subkpi { background: var(--card); border-radius: 16px; padding: 16px 18px; display: flex; flex-direction: column; gap: 6px; }
.subkpi span { font-size: 12px; color: var(--muted); }
.subkpi strong { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }

.row2 { display: flex; flex-wrap: wrap; gap: 20px; }
.chart-card { flex: 2 1 560px; display: flex; flex-direction: column; gap: 18px; }
.channels-card { flex: 1 1 300px; display: flex; flex-direction: column; gap: 20px; }

.seg { display: flex; gap: 4px; background: var(--bg); border-radius: 999px; padding: 4px; }
.seg button {
  min-height: 36px; padding: 0 14px; border: 0; border-radius: 999px; font-size: 12px;
  background: transparent; color: var(--muted); font-weight: 500; cursor: pointer;
}
.seg button.on { background: var(--card); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(17, 24, 39, 0.08); }

.legend { display: flex; flex-wrap: wrap; gap: 18px; font-size: 12px; color: var(--text-2); }
.legend span { display: flex; align-items: center; gap: 6px; }
.sw { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.sw.line { width: 16px; height: 2px; border-radius: 0; background: var(--text); }
.chart-box { position: relative; height: 280px; }

.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; display: inline-block; }
.channel { display: flex; flex-direction: column; gap: 8px; }
.channel-top, .channel-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.channel-name { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.channel-meta { font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.bar { height: 8px; background: #F0F1F4; border-radius: 999px; overflow: hidden; }
.bar div { height: 100%; border-radius: 999px; }
.callout {
  margin-top: auto; background: #FFF7ED; border-radius: 14px; padding: 12px 14px;
  font-size: 12px; line-height: 1.5; color: #7C2D12;
}

/* ---------- Bảng ---------- */
.scroll-x { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.tbl.apps { min-width: 1040px; }
.tbl.camps { min-width: 1000px; }
.tbl.camps td, .tbl.camps th { padding-left: 8px; padding-right: 8px; }
.tbl.camps .tag { display: inline-block; max-width: 190px; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; }
.th-sort {
  border: 0; background: transparent; padding: 4px 0; font-size: 12px; font-weight: 600;
  color: var(--muted); cursor: pointer; white-space: nowrap;
}
.th-sort:hover, .th-sort.on { color: var(--text); }
.camp-cell { display: flex; flex-direction: column; gap: 4px; }
.camp-sub { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.camp-src { font-size: 12px; color: var(--muted); }
.paused { color: var(--warn); font-weight: 600; }
.tbl th {
  font-size: 12px; font-weight: 600; color: var(--muted); text-align: right;
  padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
.tbl td { padding: 10px 12px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl .l { text-align: left; }
.tbl tr.total td { font-weight: 700; background: #F9FAFB; padding-top: 14px; padding-bottom: 14px; }
.tbl tr.sub td { background: #FAFBFC; font-size: 13px; color: var(--text-2); padding-top: 8px; padding-bottom: 8px; }

.appcell { display: flex; align-items: center; gap: 10px; min-width: 0; max-width: 340px; }
.toggle {
  width: 32px; height: 32px; flex: none; border: 1px solid var(--border); background: var(--card);
  border-radius: 8px; display: flex; align-items: center; justify-content: center; cursor: pointer; color: var(--text-2);
}
.toggle svg { transition: transform 0.15s; }
.toggle.open svg { transform: rotate(90deg); }
.toggle-space { width: 32px; flex: none; }
.app-icon {
  width: 36px; height: 36px; flex: none; border-radius: 10px; color: #fff; font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.app-text { display: flex; flex-direction: column; min-width: 0; }
.app-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.app-acct { font-size: 12px; color: var(--muted); }
.sub-label { display: flex; align-items: center; gap: 10px; padding-left: 84px; }

/* ---------- Campaigns ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--bg); border-radius: 10px; font-size: 13px; }
code, .mono { font-family: ui-monospace, Menlo, Consolas, monospace; }
code { font-weight: 600; }
.camp-name { font-size: 13px; font-weight: 600; }
.rule-form { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.input {
  min-height: 40px; padding: 0 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--card);
}
.input-sm { min-height: 36px; max-width: 240px; }
.tag { display: inline-flex; padding: 6px 10px; background: var(--accent-soft); color: #1E3A8A; border-radius: 8px; font-size: 13px; }
.via { margin-left: 8px; font-size: 12px; color: var(--muted); }
.assign { display: inline-flex; align-items: center; gap: 8px; }
.unassigned { color: var(--warn); font-weight: 600; font-size: 13px; }
.btn-warn {
  min-height: 36px; padding: 0 12px; border: 1px solid var(--cost); background: #FFF7ED; color: #7C2D12;
  border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer;
}
.status { font-size: 13px; font-weight: 600; }

/* ---------- Integrations ---------- */
.int-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 20px; }
.int-card { display: flex; flex-direction: column; gap: 16px; padding: 24px; }
.int-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.int-title { display: flex; align-items: center; gap: 12px; }
.int-title strong { display: block; font-size: 16px; }
.int-title small { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }
.logo {
  width: 44px; height: 44px; border-radius: 12px; flex: none; color: #fff; font-weight: 700; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
}
.badge { padding: 6px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.ok { background: #ECFDF3; color: #166534; }
.badge.warn { background: #FFF7ED; color: #9A3412; }
.badge.soon { background: #F1F2F5; color: var(--text-2); }
.int-desc { margin: 0; font-size: 13px; color: var(--text-2); line-height: 1.5; }
.kvs { display: flex; flex-direction: column; gap: 8px; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 10px 12px; background: var(--soft); border-radius: 10px; font-size: 13px; }
.kv span:first-child { font-weight: 500; }
.kv { align-items: center; }
.kv > span:first-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.kv > span:last-child { color: var(--muted); text-align: right; flex: none; }
.kv-v { display: inline-flex; align-items: center; gap: 8px; }
.kv-v .neg, .kv-v .stale { font-weight: 600; }
.kv-btn {
  min-height: 28px; padding: 0 8px; border: 1px solid var(--border); background: var(--card);
  border-radius: 8px; font-size: 12px; color: var(--text-2); cursor: pointer;
}
.kv-btn:hover { border-color: var(--neg); color: var(--neg); }
.int-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: auto; }
.schedule { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 16px; }
.schedule h2 { font-size: 16px; }
.schedule p { margin: 4px 0 0; font-size: 13px; color: var(--muted); line-height: 1.5; }

p.callout { margin: 0; }

/* ---------- Đăng nhập ---------- */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 400px; background: var(--card); border-radius: 22px; padding: 32px 28px;
  display: flex; flex-direction: column; gap: 18px;
}
.login-card .brand { padding: 0; }
.login-card h1 { margin: 0; font-size: 24px; font-weight: 700; letter-spacing: -0.02em; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; color: var(--text-2); }
.field .input { min-height: 44px; font-size: 14px; color: var(--text); }
.login-error { margin: 0; padding: 10px 12px; background: #FEF2F2; color: #991B1B; border-radius: 10px; font-size: 13px; line-height: 1.5; }
.login-card .btn-primary:disabled { opacity: 0.7; cursor: default; }
.user-box { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 4px; font-size: 12px; color: var(--muted); }
.user-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.badge.off { background: #F1F2F5; color: var(--text-2); }
.fatal { max-width: 560px; margin: 80px auto; padding: 24px; background: var(--card); border-radius: 18px; line-height: 1.6; }

/* ---------- Chọn tài khoản Meta ---------- */
.picker { border: 2px solid var(--accent-soft); }
.picker-body { display: flex; flex-direction: column; gap: 12px; max-height: 60vh; overflow-y: auto; }
.bm-group { margin: 0; padding: 0 0 4px; border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.bm-group legend { display: contents; }
.bm-head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding: 12px 14px; background: var(--soft);
}
.bm-pick { display: flex; align-items: center; gap: 10px; font-weight: 600; cursor: pointer; flex: 1 1 260px; min-width: 0; }
.bm-pick .na { font-weight: 400; font-size: 12px; }
.bm-follow {
  display: inline-flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 999px;
  background: var(--card); border: 1px solid var(--border); font-size: 12px; font-weight: 600; color: var(--text-2); cursor: pointer;
}
.bm-follow:has(input:checked) { background: var(--accent-soft); border-color: #C7D2FE; color: var(--accent); }
.acc-empty { padding: 10px 14px; }
.acc-row {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; align-items: center; gap: 10px;
  padding: 10px 14px 10px 38px; border-top: 1px solid var(--line); cursor: pointer;
}
.acc-row:hover { background: #FAFBFC; }
.picker input[type="checkbox"] { width: 18px; height: 18px; margin: 0; accent-color: var(--rev); cursor: pointer; }
.acc-main { display: flex; flex-direction: column; min-width: 0; }
.acc-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acc-main .mono { font-size: 12px; }
.acc-badges { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.acc-via { font-size: 12px; white-space: nowrap; }
.pill-tag { padding: 3px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; background: #F1F2F5; white-space: nowrap; }
.pill-tag.neg { background: #FEF2F2; }
.pill-tag.stale { background: #FFF7ED; }
.picker-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.picker-actions { display: flex; gap: 8px; }
@media (max-width: 640px) {
  .acc-row { grid-template-columns: auto minmax(0, 1fr); padding-left: 14px; }
  .acc-badges, .acc-via { grid-column: 2; justify-content: flex-start; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 12px);
  max-width: calc(100% - 32px); background: var(--text); color: #fff; padding: 12px 16px;
  border-radius: 12px; font-size: 13px; box-shadow: 0 8px 24px rgba(17, 24, 39, 0.18);
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Màn hình hẹp ---------- */
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: static; height: auto; padding: 12px 16px; gap: 12px;
    flex-direction: row; flex-wrap: wrap; align-items: center;
    border-right: 0; border-bottom: 1px solid #E7E9ED;
  }
  .brand { padding: 0; }
  .nav { flex-direction: row; flex: 1 1 100%; min-width: 0; overflow-x: auto; }
  .nav a { flex: 1 0 auto; justify-content: center; gap: 8px; padding: 0 10px; font-size: 13px; }
  .sync-box { display: none; }
  .main { padding: 20px 16px 40px; gap: 16px; }
  .view { gap: 16px; }
  .page-head h1 { font-size: 24px; }
  .card { padding: 20px; border-radius: 18px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; padding: 20px; }
  .kpi-value { font-size: 28px; }
  .subkpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .range-bar { border-radius: 16px; }
  .chart-card, .channels-card { flex-basis: 100%; }
  .int-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 420px) {
  .kpi-value { font-size: 24px; }
  .select-pill select { max-width: 200px; }
}
