:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f5;
  --border: #e3e6ea;
  --text: #16191d;
  --muted: #5f6b76;
  --accent: #0f7b6c;
  --accent-contrast: #ffffff;
  --accent-soft: #e2f3ef;
  --info: #2563eb;
  --info-soft: #e7eefc;
  --warn: #a15c00;
  --warn-soft: #fdf0dc;
  --error: #c0262d;
  --error-soft: #fbe5e6;
  --ok: #18794e;
  --ok-soft: #e1f3e9;
  --bubble-in: #ffffff;
  --bubble-out: #d9f5e5;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1113;
    --surface: #161a1d;
    --surface-2: #1d2226;
    --border: #2a3035;
    --text: #e7eaec;
    --muted: #97a2ab;
    --accent: #2cc4a8;
    --accent-contrast: #062520;
    --accent-soft: #10302a;
    --info: #7aa7ff;
    --info-soft: #172338;
    --warn: #f0b04f;
    --warn-soft: #33270f;
    --error: #ff7b7f;
    --error-soft: #3a1618;
    --ok: #4fd08f;
    --ok-soft: #11301f;
    --bubble-in: #1d2226;
    --bubble-out: #11372b;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent);
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.25;
}
h1 {
  font-size: 20px;
}
h2 {
  font-size: 16px;
}
h3 {
  font-size: 14px;
}

code,
pre,
.mono {
  font-family: var(--mono);
  font-size: 12.5px;
}

pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 10px 12px;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- layout ---------- */

.shell {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
  padding: 4px 8px 16px;
}
.brand img {
  width: 26px;
  height: 26px;
}

.nav a {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}
.nav a:hover {
  background: var(--surface-2);
}
.nav a[aria-current='page'] {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.sidebar .spacer {
  flex: 1;
}
.sidebar .who {
  font-size: 12px;
  color: var(--muted);
  padding: 8px 10px;
  border-top: 1px solid var(--border);
}

.main {
  padding: 24px 28px 48px;
  min-width: 0;
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.page-head p {
  margin: 4px 0 0;
  color: var(--muted);
}

@media (max-width: 820px) {
  .shell {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: static;
    height: auto;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    padding: 8px 12px;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .brand {
    padding: 4px 8px;
  }
  .nav {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
  }
  .sidebar .spacer,
  .sidebar .who {
    display: none;
  }
  .main {
    padding: 16px;
  }
}

/* ---------- surfaces ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card + .card,
.stack > * + * {
  margin-top: 16px;
}
.card-head {
  display: flex;
  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));
}
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.tile .label {
  color: var(--muted);
  font-size: 12px;
}
.tile .value {
  font-size: 24px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}
.tile.bad .value {
  color: var(--error);
}
.tile.warn .value {
  color: var(--warn);
}

/* ---------- tables ---------- */

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
table {
  width: 100%;
  border-collapse: collapse;
}
th,
td {
  text-align: left;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
th {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: var(--surface-2);
  white-space: nowrap;
}
tbody tr:last-child td {
  border-bottom: 0;
}
tr.clickable {
  cursor: pointer;
}
tr.clickable:hover td {
  background: var(--surface-2);
}
tr.details td {
  background: var(--surface-2);
}
td.num {
  font-variant-numeric: tabular-nums;
  text-align: right;
}
td.nowrap,
.nowrap {
  white-space: nowrap;
}
td.summary {
  min-width: 280px;
}
.empty {
  padding: 28px;
  text-align: center;
  color: var(--muted);
}
.new-row td {
  animation: flash 1.6s ease-out;
}
@keyframes flash {
  from {
    background: var(--accent-soft);
  }
}

/* ---------- badges ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 550;
  white-space: nowrap;
  background: var(--surface-2);
  color: var(--muted);
}
.badge.info {
  background: var(--info-soft);
  color: var(--info);
}
.badge.ok,
.badge.open,
.badge.sent {
  background: var(--ok-soft);
  color: var(--ok);
}
.badge.warn,
.badge.pending,
.badge.sending,
.badge.qr,
.badge.connecting,
.badge.starting {
  background: var(--warn-soft);
  color: var(--warn);
}
.badge.error,
.badge.failed,
.badge.closed,
.badge.logged_out {
  background: var(--error-soft);
  color: var(--error);
}
.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.chip {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 6px;
}

/* ---------- forms ---------- */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: end;
  margin-bottom: 12px;
}
label.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 550;
}
label.field > span.hint {
  font-weight: 400;
}
.form {
  display: grid;
  gap: 12px;
}
.form label.field {
  font-size: 13px;
}

input,
select,
textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-height: 36px;
}
textarea {
  min-height: 80px;
  resize: vertical;
}
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
}
.check input {
  min-height: 0;
}
fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 0;
}
legend {
  font-size: 12px;
  color: var(--muted);
  padding: 0 4px;
}

button,
.btn {
  font: inherit;
  font-weight: 550;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
button:hover {
  background: var(--surface-2);
}
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}
button.primary:hover {
  filter: brightness(1.05);
}
button.danger {
  color: var(--error);
}
button.danger.solid {
  background: var(--error);
  border-color: var(--error);
  color: #fff;
}
button.small {
  min-height: 28px;
  padding: 3px 10px;
  font-size: 12.5px;
}
button.link {
  border: 0;
  background: none;
  padding: 0;
  min-height: 0;
  color: var(--accent);
}
button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.muted {
  color: var(--muted);
}
.error-text {
  color: var(--error);
}
.right {
  margin-left: auto;
}

/* ---------- login ---------- */

.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login .card {
  width: min(380px, 100%);
  padding: 24px;
}
.login .brand {
  padding: 0 0 16px;
  font-size: 16px;
}

/* ---------- modal & toasts ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0;
  width: min(560px, calc(100% - 32px));
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgb(0 0 0 / 25%);
}
dialog::backdrop {
  background: rgb(10 14 18 / 45%);
}
.modal-body {
  padding: 20px;
  display: grid;
  gap: 14px;
  max-height: 80vh;
  overflow: auto;
}
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

.secret {
  display: flex;
  gap: 8px;
  align-items: center;
}
.secret code {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  overflow-wrap: anywhere;
}
.callout {
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--warn-soft);
  color: var(--warn);
}

#toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: grid;
  gap: 8px;
  z-index: 50;
}
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  max-width: 420px;
}
.toast.error {
  background: var(--error);
  color: #fff;
}

/* ---------- WhatsApp session & inbox ---------- */

.qr {
  display: grid;
  justify-items: center;
  gap: 8px;
}
.qr img {
  width: 260px;
  height: 260px;
  border-radius: 8px;
  background: #fff;
  padding: 6px;
}

.inbox {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 16px;
  min-height: 60vh;
}
@media (max-width: 820px) {
  .inbox {
    grid-template-columns: 1fr;
  }
}
.chat-list {
  max-height: 70vh;
  overflow: auto;
  padding: 6px;
}
.chat-list button {
  width: 100%;
  justify-content: space-between;
  border: 0;
  background: none;
  text-align: left;
  padding: 8px 10px;
  min-height: 0;
  font-weight: 400;
}
.chat-list button[aria-current='true'] {
  background: var(--accent-soft);
}
.chat-list .name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-list .sub {
  display: block;
  font-size: 12px;
  color: var(--muted);
}
.messages {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 70vh;
  overflow: auto;
  padding: 12px;
  background: var(--surface-2);
  border-radius: 8px;
}
.bubble {
  max-width: min(560px, 85%);
  background: var(--bubble-in);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 10px;
  align-self: flex-start;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.bubble.me {
  background: var(--bubble-out);
  align-self: flex-end;
}
.bubble .meta {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}
.bubble .from {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
}

/* ---------- spacing utilities (no inline styles: the CSP forbids them) ---------- */

.gap {
  height: 16px;
}
.mt {
  margin-top: 12px;
}
.mb {
  margin-bottom: 12px;
}
.mb0 {
  margin-bottom: 0;
}
.pad {
  padding: 8px;
}
.sub-head {
  margin: 8px 0;
}

[hidden] {
  display: none !important;
}

a.btn {
  text-decoration: none;
}
