/* AfriMedlink Admin Console — design tokens
   Inherits the pharmacy dashboard's palette family (Plus Jakarta Sans, green
   accent) but tuned for a dense "product register" (Linear/Stripe/Notion),
   not the marketing-toned pharmacy dashboard: smaller radii, tighter
   spacing, charcoal/ash ink instead of pure black, no decorative glow. */
:root {
  --bg: #f4f5f2;
  --bg2: #eceee8;
  --card: #ffffff;
  --card2: #f4f5f1;
  --ink: #182018;
  --ink2: #4c584c;
  --ink3: #869187;
  --border: rgba(20, 28, 20, 0.09);
  --border2: rgba(45, 158, 122, 0.28);
  --accent: #2d9e7a;
  --accent-ink: #176b4d;
  --accent-soft: rgba(45, 158, 122, 0.11);
  --amber: #d99a1b;
  --amber-soft: rgba(217, 154, 27, 0.14);
  --orange: #e2652b;
  --orange-soft: rgba(226, 101, 43, 0.14);
  --red: #d64545;
  --red-soft: rgba(214, 69, 69, 0.13);
  --ash: #6d7970;
  --ash-soft: rgba(109, 121, 112, 0.12);

  --radius: 10px;
  --radius-sm: 7px;
  --sidebar-w: 224px;
  --topbar-h: 52px;

  --font: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --mono: ui-monospace, 'SF Mono', 'Menlo', monospace;

  --sh-sm: 0 1px 2px rgba(20, 30, 20, 0.05), 0 1px 6px rgba(20, 30, 20, 0.04);
  --sh-md: 0 4px 16px rgba(20, 30, 20, 0.08);

  color-scheme: light;
}

* , *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 13.5px;
  line-height: 1.45;
}
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--ink3); border-radius: 99px; }

a { color: inherit; text-decoration: none; }
button { font-family: inherit; font-size: inherit; cursor: pointer; }

/* ── Login gate ────────────────────────────────────────────────────────── */
#login-gate {
  position: fixed; inset: 0; z-index: 500;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.login-card {
  width: 340px; background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px; box-shadow: var(--sh-md);
}
.login-brand {
  display: flex; align-items: center; gap: 9px; margin-bottom: 4px;
  font-weight: 800; font-size: 1.05rem; letter-spacing: -0.02em;
}
.login-brand img { width: 26px; height: 26px; border-radius: 50%; object-fit: cover; }
.login-sub { color: var(--ink2); font-size: 0.8rem; margin-bottom: 20px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 0.72rem; font-weight: 600; color: var(--ink2); margin-bottom: 5px; text-transform: uppercase; letter-spacing: 0.04em; }
.field input, .field select {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-family: inherit; font-size: 0.85rem; background: var(--card2); color: var(--ink);
}
.field input:disabled, .field select:disabled { opacity: 0.6; cursor: not-allowed; }
.field .hint { font-size: 0.7rem; color: var(--ink3); margin-top: 4px; }
.wa-country-title { font-size: 0.9rem; font-weight: 700; margin-bottom: 10px; }
.field input:focus, .field select:focus { outline: none; border-color: var(--border2); background: var(--card); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 14px; border-radius: var(--radius-sm); border: 1px solid transparent;
  font-weight: 600; font-size: 0.82rem; transition: background .15s, border-color .15s, opacity .15s;
}
.btn-primary { background: var(--ink); color: #fff; width: 100%; margin-top: 6px; }
.btn-primary:hover { background: #0f1610; }
.btn-primary:disabled { opacity: .55; cursor: default; }
.login-error { color: var(--red); font-size: 0.78rem; margin-top: 10px; min-height: 1em; }

/* ── App shell ─────────────────────────────────────────────────────────── */
#app { display: none; min-height: 100vh; }
#app.ready { display: flex; }

.sidebar {
  width: var(--sidebar-w); flex-shrink: 0; position: fixed; top: 0; bottom: 0; left: 0;
  background: var(--card); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 14px 10px;
  z-index: 50;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px 16px;
  font-weight: 800; font-size: 0.94rem; letter-spacing: -0.02em;
}
.sidebar-brand img { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; }
.sidebar-brand span.tag {
  font-size: 0.58rem; font-weight: 700; color: var(--accent-ink); background: var(--accent-soft);
  padding: 2px 6px; border-radius: 5px; letter-spacing: 0.04em; margin-left: auto;
}
.nav-group-label {
  font-size: 0.62rem; font-weight: 700; color: var(--ink3); text-transform: uppercase;
  letter-spacing: 0.06em; padding: 12px 10px 4px;
}
.nav-link {
  display: flex; align-items: center; gap: 9px; padding: 7px 10px; border-radius: var(--radius-sm);
  color: var(--ink2); font-size: 0.82rem; font-weight: 500; margin-bottom: 1px;
}
.nav-link svg.ic { width: 16px; height: 16px; flex-shrink: 0; opacity: .8; }
.nav-link:hover svg.ic, .nav-link.active svg.ic { opacity: 1; }
.nav-link:hover { background: var(--card2); color: var(--ink); }
.nav-link.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 700; }
.sidebar-foot {
  margin-top: auto; padding: 10px 8px 4px; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
}
.avatar {
  width: 26px; height: 26px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-ink);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.7rem; flex-shrink: 0;
}
.sidebar-foot .who { font-size: 0.76rem; overflow: hidden; }
.sidebar-foot .who .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-foot .who .role { color: var(--ink3); font-size: 0.68rem; text-transform: capitalize; }
.sidebar-foot .logout { margin-left: auto; color: var(--ink3); font-size: 0.9rem; padding: 4px; border-radius: 6px; }
.sidebar-foot .logout:hover { color: var(--red); background: var(--red-soft); }

.main {
  margin-left: var(--sidebar-w); flex: 1; min-width: 0;
  display: flex; flex-direction: column;
}
.topbar {
  height: var(--topbar-h); flex-shrink: 0; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; padding: 0 20px;
  background: rgba(244,245,242,0.92); backdrop-filter: blur(10px);
  position: sticky; top: 0; z-index: 40;
}
.topbar h1 { font-size: 0.95rem; font-weight: 700; letter-spacing: -0.01em; }
.topbar .sub { color: var(--ink3); font-size: 0.74rem; font-weight: 500; margin-left: 8px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.pill {
  font-size: 0.68rem; font-weight: 700; padding: 3px 8px; border-radius: 99px;
  display: inline-flex; align-items: center; gap: 4px;
}
.pill-dot { width: 6px; height: 6px; border-radius: 50%; }
.pill-ok { background: var(--accent-soft); color: var(--accent-ink); }
.pill-ok .pill-dot { background: var(--accent); }
.pill-warn { background: var(--amber-soft); color: var(--amber); }
.pill-warn .pill-dot { background: var(--amber); }
.pill-bad { background: var(--red-soft); color: var(--red); }
.pill-bad .pill-dot { background: var(--red); }
.pill-neutral { background: var(--ash-soft); color: var(--ash); }
.pill-neutral .pill-dot { background: var(--ash); }

.view { padding: 20px 24px 60px; max-width: 1400px; }
.view-head { margin-bottom: 16px; }
.view-head h2 { font-size: 1.25rem; font-weight: 800; letter-spacing: -0.02em; }
.view-head p { color: var(--ink2); font-size: 0.82rem; margin-top: 2px; }

/* ── Grid + cards ──────────────────────────────────────────────────────── */
.grid { display: grid; gap: 12px; }
.grid-kpi { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 1100px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--sh-sm);
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 10px; }
.card-head h3 { font-size: 0.82rem; font-weight: 700; }
.card-head .meta { font-size: 0.7rem; color: var(--ink3); }

.kpi-card { position: relative; overflow: hidden; }
.kpi-label { font-size: 0.7rem; font-weight: 600; color: var(--ink2); text-transform: uppercase; letter-spacing: 0.03em; }
.kpi-value { font-family: var(--mono); font-size: 1.65rem; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.kpi-delta { font-size: 0.72rem; font-weight: 600; margin-top: 4px; }
.kpi-delta.up { color: var(--accent-ink); }
.kpi-delta.down { color: var(--red); }
.kpi-spark { position: absolute; right: 0; bottom: 0; left: 0; height: 34px; opacity: 0.9; }

/* ── Tables ────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table.admin-table { width: 100%; border-collapse: collapse; font-size: 0.8rem; }
table.admin-table th {
  text-align: left; font-size: 0.66rem; font-weight: 700; color: var(--ink3);
  text-transform: uppercase; letter-spacing: 0.04em; padding: 8px 10px; border-bottom: 1px solid var(--border);
  white-space: nowrap; cursor: pointer; user-select: none;
}
table.admin-table th:hover { color: var(--ink); }
table.admin-table th.num { text-align: right; }
table.admin-table td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.admin-table tbody tr { transition: background .12s; cursor: pointer; }
table.admin-table tbody tr:hover { background: var(--card2); }
.mono { font-family: var(--mono); }
.num { font-family: var(--mono); text-align: right; }
.text-ink3 { color: var(--ink3); }
.text-red { color: var(--red); font-weight: 700; }

.status-tag {
  display: inline-flex; align-items: center; gap: 5px; font-size: 0.68rem; font-weight: 700;
  padding: 3px 8px; border-radius: 99px; text-transform: capitalize;
}
.status-pilot { background: var(--amber-soft); color: var(--amber); }
.status-active { background: var(--accent-soft); color: var(--accent-ink); }
.status-past_due { background: var(--orange-soft); color: var(--orange); }
.status-suspended { background: var(--red-soft); color: var(--red); }
.status-churned { background: var(--ash-soft); color: var(--ash); }

.filter-bar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.filter-bar select, .filter-bar input {
  padding: 6px 9px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-family: inherit; font-size: 0.78rem; background: var(--card);
}
.filter-bar input { min-width: 220px; }

/* ── Empty / loading states ────────────────────────────────────────────── */
.empty-state { text-align: center; padding: 40px 20px; color: var(--ink3); }
.empty-state .ic { font-size: 1.6rem; margin-bottom: 8px; opacity: .6; }
.empty-state p { font-size: 0.8rem; }
.skeleton {
  background: linear-gradient(90deg, var(--card2) 25%, var(--bg2) 37%, var(--card2) 63%);
  background-size: 400% 100%; animation: shimmer 1.4s ease infinite; border-radius: var(--radius-sm);
}
@keyframes shimmer { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }

/* ── Panels / detail drawer ────────────────────────────────────────────── */
.drawer-overlay {
  position: fixed; inset: 0; background: rgba(20,28,20,0.28); z-index: 200; opacity: 0;
  pointer-events: none; transition: opacity .18s;
}
.drawer-overlay.open { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: 460px; max-width: 92vw;
  background: var(--card); border-left: 1px solid var(--border); z-index: 201;
  transform: translateX(100%); transition: transform .22s ease; overflow-y: auto;
  box-shadow: -8px 0 24px rgba(20,30,20,0.12);
}
.drawer.open { transform: translateX(0); }
.drawer-head { padding: 18px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: flex-start; justify-content: space-between; }
.drawer-head h3 { font-size: 1.05rem; font-weight: 800; }
.drawer-head .close { font-size: 1.1rem; color: var(--ink3); padding: 4px; }
.drawer-body { padding: 16px 20px 40px; }
.drawer-section { margin-bottom: 18px; }
.drawer-section h4 { font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink3); margin-bottom: 8px; }
.kv-row { display: flex; justify-content: space-between; padding: 5px 0; font-size: 0.8rem; border-bottom: 1px dashed var(--border); }
.kv-row .k { color: var(--ink2); }
.kv-row .v { font-weight: 600; }
.action-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.btn-sm { padding: 6px 10px; border-radius: var(--radius-sm); font-size: 0.74rem; font-weight: 700; border: 1px solid var(--border); background: var(--card2); }
.btn-sm:hover { background: var(--bg2); }
.btn-sm.danger { color: var(--red); border-color: var(--red-soft); }
.btn-sm.danger:hover { background: var(--red-soft); }
.btn-sm.primary { color: var(--accent-ink); border-color: var(--border2); background: var(--accent-soft); }

/* ── Charts (D3 renders into these; classes below are the "override the
   library default" surface — colors/labels/tooltips are ours, not D3's) ── */
.chart-tooltip {
  position: fixed; pointer-events: none; z-index: 300; background: var(--ink); color: #fff;
  font-size: 0.72rem; padding: 6px 9px; border-radius: 6px; opacity: 0; transition: opacity .1s;
  font-family: var(--font); box-shadow: var(--sh-md); white-space: nowrap;
}
.chart-tooltip.show { opacity: 1; }
.chart-legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 0.72rem; margin-top: 8px; }
.chart-legend .item { display: flex; align-items: center; gap: 5px; color: var(--ink2); cursor: default; }
.chart-legend .item .sw { width: 8px; height: 8px; border-radius: 2px; }
.chart-legend .item.dim { opacity: .35; }
.axis text { fill: var(--ink3); font-size: 9.5px; font-family: var(--font); }
.axis path, .axis line { stroke: var(--border); }
.ref-line { stroke: var(--red); stroke-width: 1; stroke-dasharray: 3 3; }
.ref-label { fill: var(--red); font-size: 9.5px; font-weight: 700; font-family: var(--font); }

.ranked-bar-row { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; }
.ranked-bar-label { width: 130px; flex-shrink: 0; font-size: 0.74rem; text-align: right; color: var(--ink2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ranked-bar-track { flex: 1; height: 16px; background: var(--card2); border-radius: 4px; overflow: hidden; }
.ranked-bar-fill { height: 100%; background: var(--accent); border-radius: 4px; }
.ranked-bar-value { width: 34px; flex-shrink: 0; font-family: var(--mono); font-size: 0.72rem; color: var(--ink2); }

.funnel-row { margin-bottom: 8px; }
.funnel-label { display: flex; justify-content: space-between; font-size: 0.74rem; margin-bottom: 3px; }
.funnel-label .n { font-family: var(--mono); font-weight: 700; }
.funnel-track { height: 22px; background: var(--card2); border-radius: 5px; overflow: hidden; }
.funnel-fill { height: 100%; background: var(--accent); border-radius: 5px; transition: width .5s ease; }

.pilot-row { padding: 10px 0; border-bottom: 1px solid var(--border); }
.pilot-row:last-child { border-bottom: none; }
.pilot-row-top { display: flex; justify-content: space-between; font-size: 0.8rem; margin-bottom: 6px; }
.pilot-row-top .name { font-weight: 700; }
.pilot-progress { display: flex; gap: 10px; }
.pilot-bar-group { flex: 1; }
.pilot-bar-label { font-size: 0.66rem; color: var(--ink3); margin-bottom: 3px; }
.pilot-bar-track { height: 6px; background: var(--card2); border-radius: 3px; overflow: hidden; }
.pilot-bar-fill { height: 100%; background: var(--accent); border-radius: 3px; }
.pilot-bar-fill.met { background: var(--amber); }

.settings-form { max-width: 560px; }
.settings-form .field { margin-bottom: 14px; }
.settings-form textarea {
  width: 100%; min-height: 100px; padding: 9px 11px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-family: inherit; font-size: 0.82rem; background: var(--card2); resize: vertical;
}
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.toast {
  position: fixed; bottom: 18px; right: 18px; background: var(--ink); color: #fff; padding: 10px 16px;
  border-radius: var(--radius-sm); font-size: 0.8rem; z-index: 400; box-shadow: var(--sh-md);
  opacity: 0; transform: translateY(8px); transition: opacity .18s, transform .18s; pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--red); }

.badge-role { font-size: 0.66rem; font-weight: 700; padding: 2px 7px; border-radius: 99px; background: var(--accent-soft); color: var(--accent-ink); text-transform: capitalize; }
