:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efeb;
  --border: #e3e2dc;
  --grid: #e6e5e0;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #6f6e69;
  --brand: #2a78d6;
  --brand-ink: #ffffff;
  --series-1: #2a78d6;
  --good: #0f7b3f;
  --good-bg: #e3f4ea;
  --warn: #8a5a00;
  --warn-bg: #fdf1d6;
  --bad: #b42318;
  --bad-bg: #fde7e5;
  --info-bg: #e7f0fb;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #2f2f2c;
    --grid: #2e2e2c;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #a3a29a;
    --brand: #3987e5;
    --series-1: #3987e5;
    --good: #5cd18b;
    --good-bg: #16301f;
    --warn: #f0b84a;
    --warn-bg: #33270f;
    --bad: #ff8a80;
    --bad-bg: #3a1714;
    --info-bg: #162538;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, Menlo, Consolas, monospace; letter-spacing: .3px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.shell {
  display: grid; grid-template-columns: 220px 1fr; min-height: 100vh;
  /* sidebar colour + border run the full page height, even past the sticky sidebar */
  background: linear-gradient(to right, var(--surface) 219px, var(--border) 219px, var(--border) 220px, var(--bg) 220px);
}
.shell > * { min-width: 0; }
.side {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px;
}
.brand { font-weight: 700; font-size: 15px; padding: 4px 10px 16px; }
.brand span { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
  color: var(--text-2); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--info-bg); color: var(--text); }
.nav .badge { margin-left: auto; }
.side .foot { margin-top: auto; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
main { padding: 24px 28px 64px; max-width: 1240px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.mobile-bar { display: none; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; background: var(--bg); }
  .side { position: static; height: auto; flex-direction: row; overflow-x: auto; padding: 8px; border-right: 0; border-bottom: 1px solid var(--border); }
  .brand, .side .foot { display: none; }
  .nav { display: flex; gap: 4px; }
  .nav a { white-space: nowrap; }
  main { padding: 16px 16px 48px; }
}

/* ---------- surfaces ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 14px; }
.two { grid-template-columns: 2fr 1fr; }
@media (max-width: 1000px) { .two { grid-template-columns: 1fr; } }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .value { font-size: 24px; font-weight: 650; margin: 4px 0 2px; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--text-2); font-size: 12px; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---------- alerts / badges ---------- */
.alert { border-radius: 10px; padding: 10px 12px; display: flex; gap: 8px; align-items: flex-start; font-size: 13px; }
.alert.bad { background: var(--bad-bg); color: var(--bad); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.good { background: var(--good-bg); color: var(--good); }
.alert.info { background: var(--info-bg); color: var(--text); }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.badge.good { background: var(--good-bg); color: var(--good); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.info { background: var(--info-bg); color: var(--text); }

/* ---------- forms ---------- */
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; min-height: 36px;
}
textarea { width: 100%; min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
label { display: block; font-weight: 600; font-size: 12px; color: var(--text-2); margin: 0 0 4px; }
.field { margin-bottom: 12px; }
.field input:not([type=checkbox]), .field select { width: 100%; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; font-size: 13px; color: var(--text); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0 12px; }
button, .btn {
  font: inherit; font-weight: 600; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 7px 12px; min-height: 36px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
button.primary:hover { filter: brightness(1.07); }
button.danger { color: var(--bad); }
button.sm { min-height: 28px; padding: 3px 9px; font-size: 12px; }
button:disabled { opacity: .55; cursor: default; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { text-decoration: none; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 10px; font-size: 12px; font-weight: 600; cursor: pointer; color: var(--text-2); min-height: 0; }
.chip.on { background: var(--info-bg); color: var(--text); border-color: transparent; }
.chip .n { color: var(--muted); font-weight: 500; margin-left: 4px; }
.search { min-width: 240px; }
@media (max-width: 600px) { .search { min-width: 0; width: 100%; } }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -16px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 16px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--surface-2); }
.pager { display: flex; justify-content: space-between; align-items: center; padding-top: 12px; }
.empty { text-align: center; color: var(--muted); padding: 28px 0; }

/* ---------- progress ---------- */
.bar { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; display: flex; gap: 2px; }
.bar > span { display: block; height: 100%; border-radius: 999px; }
.bar .s { background: var(--series-1); }
.bar .f { background: var(--bad); }

/* ---------- chart ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: 220px; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--muted); font-size: 11px; }
.chart .col { fill: var(--series-1); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .col, .chart .col.hover { opacity: .8; }
.tip {
  position: absolute; pointer-events: none; background: var(--text); color: var(--surface);
  padding: 6px 9px; border-radius: 8px; font-size: 12px; white-space: nowrap; transform: translate(-50%, -100%); margin-top: -8px; z-index: 2;
}
.tip b { display: block; }

/* ---------- drawer / modal ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; justify-content: flex-end; z-index: 20; }
.drawer { width: min(520px, 100%); height: 100%; background: var(--surface); padding: 20px; overflow-y: auto; box-shadow: -4px 0 24px rgba(0,0,0,.15); }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; margin: 0 0 16px; }
.kv dt { color: var(--muted); font-size: 12px; }
.kv dd { margin: 0; word-break: break-word; }

/* ---------- sms preview ---------- */
.sms-bubble { background: var(--surface-2); border-radius: 14px 14px 14px 4px; padding: 10px 12px; white-space: pre-wrap; word-break: break-word; margin-bottom: 8px; font-size: 13px; }
.counter { font-size: 12px; color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; margin-top: 6px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(360px, 100%); padding: 24px; }

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--surface);
  padding: 10px 16px; border-radius: 10px; font-weight: 500; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 50; max-width: 90vw;
}
#toast.show { opacity: 1; }
#toast.bad { background: var(--bad); color: #fff; }
