/* BTSPL WhatsApp Gateway — dashboard
   Tokens first; every component is written against roles, so light/dark swap
   in one place. */

:root {
  color-scheme: light;
  --surface-0: #f4f5f6;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --surface-3: #eef0f2;
  --line: #e0e3e6;
  --line-strong: #c9ced3;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #85898e;
  --accent: #128c7e;
  --accent-ink: #0b6257;
  --accent-soft: #e2f3f0;
  --series-1: #1baf7a;
  --series-2: #2a78d6;
  --series-3: #e34948;
  --good: #167d3f;
  --good-bg: #e3f4e8;
  --warn: #8a5a00;
  --warn-bg: #fdf1d8;
  --bad: #b3261e;
  --bad-bg: #fce8e6;
  --focus: #2a78d6;
  --radius: 10px;
  --radius-sm: 7px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-0: #121313;
    --surface-1: #1a1a19;
    --surface-2: #212221;
    --surface-3: #2a2b2a;
    --line: #2f3130;
    --line-strong: #444745;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8e918d;
    --accent: #25d366;
    --accent-ink: #9ae9bb;
    --accent-soft: #15301f;
    --series-1: #199e70;
    --series-2: #3987e5;
    --series-3: #e66767;
    --good: #6fdc8c;
    --good-bg: #14311f;
    --warn: #f5c451;
    --warn-bg: #352a0c;
    --bad: #f28b82;
    --bad-bg: #3a1a17;
    --focus: #3987e5;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121313;
  --surface-1: #1a1a19;
  --surface-2: #212221;
  --surface-3: #2a2b2a;
  --line: #2f3130;
  --line-strong: #444745;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8e918d;
  --accent: #25d366;
  --accent-ink: #9ae9bb;
  --accent-soft: #15301f;
  --series-1: #199e70;
  --series-2: #3987e5;
  --series-3: #e66767;
  --good: #6fdc8c;
  --good-bg: #14311f;
  --warn: #f5c451;
  --warn-bg: #352a0c;
  --bad: #f28b82;
  --bad-bg: #3a1a17;
  --focus: #3987e5;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 14px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--series-2); }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

.hidden { display: none !important; }

/* ─── auth screens ───────────────────────────────────────────────────────── */
.auth-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 32px;
  box-shadow: var(--shadow);
}

.auth-card h1 { font-size: 19px; margin: 0 0 4px; letter-spacing: -.01em; }
.auth-card .sub { color: var(--text-secondary); font-size: 13px; margin: 0 0 24px; }

/* ─── app shell ──────────────────────────────────────────────────────────── */
.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100dvh; }

.sidebar {
  background: var(--surface-1);
  border-right: 1px solid var(--line);
  padding: 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: sticky;
  top: 0;
  height: 100dvh;
  overflow-y: auto;
}

.brand {
  display: flex; align-items: center; gap: 9px;
  padding: 0 10px 18px;
  font-weight: 650; letter-spacing: -.01em;
}
.brand-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); flex: none; }
.brand small { display: block; font-weight: 400; font-size: 11px; color: var(--text-muted); }

.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .07em;
  color: var(--text-muted); padding: 16px 10px 6px; }

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--text-secondary); text-decoration: none;
  font-size: 13.5px; cursor: pointer; border: 0; background: none;
  width: 100%; text-align: left; font-family: inherit;
}
.nav-item:hover { background: var(--surface-3); color: var(--text-primary); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 550; }
.nav-item .count {
  margin-left: auto; font-size: 11px; font-weight: 600;
  background: var(--bad); color: #fff; border-radius: 99px; padding: 1px 6px;
}

.main { display: flex; flex-direction: column; min-width: 0; }

.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 24px; border-bottom: 1px solid var(--line);
  background: var(--surface-1); position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 16px; margin: 0; font-weight: 600; letter-spacing: -.01em; }
.topbar .spacer { flex: 1; }

.content { padding: 24px; max-width: 1180px; width: 100%; }

/* ─── primitives ─────────────────────────────────────────────────────────── */
.card {
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px;
}
.card h2 {
  font-size: 12px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-muted); margin: 0 0 14px; font-weight: 600;
}
.card-pad-0 { padding: 0; }

.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }

.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row-tight { gap: 6px; }
.stack { display: flex; flex-direction: column; gap: 14px; }

/* stat tile — a number is not a chart */
.stat .label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.stat .value { font-size: 27px; font-weight: 640; letter-spacing: -.02em;
  font-variant-numeric: tabular-nums; line-height: 1.1; }
.stat .meta { font-size: 12px; color: var(--text-secondary); margin-top: 5px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 9px; border-radius: 99px; font-size: 12px; font-weight: 560;
  background: var(--surface-3); color: var(--text-secondary);
}
.pill.good { background: var(--good-bg); color: var(--good); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

button, .btn {
  font: inherit; font-weight: 550; font-size: 13.5px;
  padding: 8px 14px; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: var(--accent); color: #fff;
  cursor: pointer; white-space: nowrap;
}
button:hover, .btn:hover { filter: brightness(1.07); }
button:disabled { opacity: .5; cursor: not-allowed; filter: none; }
.btn-ghost { background: var(--surface-2); color: var(--text-primary); border-color: var(--line-strong); }
.btn-ghost:hover { background: var(--surface-3); filter: none; }
.btn-danger { background: var(--bad); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-link { background: none; border: 0; color: var(--series-2); padding: 0; font-weight: 500; }
.btn-link:hover { text-decoration: underline; filter: none; }

input, select, textarea {
  font: inherit; width: 100%;
  padding: 8px 11px; border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm); background: var(--surface-2); color: var(--text-primary);
}
input[type="checkbox"] { width: auto; }
textarea { resize: vertical; min-height: 90px; font-family: var(--mono); font-size: 12.5px; }
label { display: block; font-size: 13px; font-weight: 550; margin-bottom: 5px; }
.field { margin-bottom: 15px; }
.field .help { font-size: 12px; color: var(--text-muted); margin-top: 5px; font-weight: 400; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

.err {
  background: var(--bad-bg); color: var(--bad); border: 1px solid currentColor;
  border-radius: var(--radius-sm); padding: 9px 12px; font-size: 13px; margin-bottom: 14px;
}
.ok-msg {
  background: var(--good-bg); color: var(--good); border: 1px solid currentColor;
  border-radius: var(--radius-sm); padding: 9px 12px; font-size: 13px; margin-bottom: 14px;
}
.note { font-size: 12.5px; color: var(--text-muted); }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--text-muted); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--line);
  background: var(--surface-1); position: sticky; top: 0;
}
td { padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-1); }
.table-wrap { overflow: auto; border-radius: var(--radius); }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.truncate { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty { padding: 44px 20px; text-align: center; color: var(--text-muted); font-size: 13.5px; }

/* ─── QR ─────────────────────────────────────────────────────────────────── */
.qr-box { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 14px; }
.qr-box img {
  width: 260px; height: 260px; background: #fff;
  padding: 10px; border-radius: var(--radius); border: 1px solid var(--line);
}
.qr-steps { text-align: left; color: var(--text-secondary); font-size: 13px; margin: 0; padding-left: 18px; }
.qr-steps li { margin: 5px 0; }

/* ─── inbox ──────────────────────────────────────────────────────────────── */
.inbox { display: grid; grid-template-columns: 320px 1fr; gap: 16px; align-items: start; }
.conv-list { max-height: calc(100dvh - 190px); overflow-y: auto; }
.conv {
  display: block; width: 100%; text-align: left; border: 0; background: none;
  padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; font-family: inherit;
}
.conv:hover { background: var(--surface-1); }
.conv.active { background: var(--accent-soft); }
.conv-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.conv-name { font-weight: 600; font-size: 13.5px; color: var(--text-primary); }
.conv-time { font-size: 11px; color: var(--text-muted); white-space: nowrap; }
.conv-preview {
  font-size: 12.5px; color: var(--text-secondary); margin-top: 3px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv .unread-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--bad); display: inline-block; }

.thread { display: flex; flex-direction: column; height: calc(100dvh - 190px); }
.thread-head {
  padding: 13px 16px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.thread-body { flex: 1; overflow-y: auto; padding: 18px 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 74%; padding: 9px 13px; border-radius: 13px; font-size: 13.5px; word-break: break-word; }
/* pre-wrap belongs on the body text only — on .msg it would render the
   surrounding template's own indentation as whitespace inside the bubble. */
.msg .body { white-space: pre-wrap; display: block; }
.msg.in { align-self: flex-start; background: var(--surface-3); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--accent-soft); color: var(--accent-ink);
  border-bottom-right-radius: 4px; }
.msg .meta { font-size: 10.5px; color: var(--text-muted); margin-top: 5px; display: block; }
.thread-compose { border-top: 1px solid var(--line); padding: 12px 14px; display: flex; gap: 10px; }
.thread-compose textarea { min-height: 42px; max-height: 140px; font-family: var(--sans); font-size: 13.5px; }

/* ─── chart ──────────────────────────────────────────────────────────────── */
.viz-root { position: relative; }
.viz-legend { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 14px; font-size: 12.5px; }
.viz-legend .key { display: inline-flex; align-items: center; gap: 7px; color: var(--text-secondary); }
.viz-legend .swatch { width: 10px; height: 10px; border-radius: 2px; flex: none; }
.viz-legend .n { font-variant-numeric: tabular-nums; font-weight: 640; color: var(--text-primary); }
.viz-tip {
  position: absolute; pointer-events: none; z-index: 10;
  background: var(--surface-2); border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm); padding: 9px 11px; font-size: 12.5px;
  box-shadow: var(--shadow); min-width: 128px;
}
.viz-tip .d { color: var(--text-muted); margin-bottom: 5px; font-size: 11.5px; }
.viz-tip .r { display: flex; align-items: center; gap: 7px; justify-content: space-between; }
.viz-tip .r b { font-variant-numeric: tabular-nums; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; overflow-x: auto; }
  .nav-group { display: none; }
  .inbox { grid-template-columns: 1fr; }
  .thread, .conv-list { height: auto; max-height: 60vh; }
  .content { padding: 16px; }
  .field-row { grid-template-columns: 1fr; }
}
