:root {
  --bg: #0d1117;
  --bg-alt: #0a0e13;
  --surface: #161c24;
  --surface-2: #1d2530;
  --surface-hover: #232c38;
  --border: #2a3441;
  --border-soft: #1c2430;
  --text: #e6eaef;
  --text-dim: #aab4c0;
  --muted: #7c8896;

  --accent: #25d366;
  --accent-dark: #1fa855;
  --accent-soft: rgba(37, 211, 102, 0.14);
  --accent-ink: #06210f;

  --warn: #f2ab27;
  --warn-soft: rgba(242, 171, 39, 0.14);

  --danger: #f0524a;
  --danger-strong: #c9372c;
  --danger-soft: rgba(240, 82, 74, 0.14);

  --read-blue: #3bb1f2;

  --font-sans: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "IBM Plex Mono", SFMono-Regular, Menlo, Consolas, monospace;

  --radius: 8px;
  --sidebar-width: 232px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

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

@media (prefers-reduced-motion: no-preference) {
  .card, .sidenav a, button, .btn, input, select, textarea, .stat {
    transition: background-color .15s ease, border-color .15s ease, color .15s ease, box-shadow .15s ease, transform .1s ease;
  }
}

/* ---------- app shell ---------- */

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--bg-alt);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 1rem;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: 0 .2rem;
  margin-bottom: 2rem;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-mark svg { width: 18px; height: 18px; }

.brand-text strong {
  display: block;
  font-size: .95rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.15;
}

.brand-text span {
  display: block;
  font-family: var(--font-mono);
  font-size: .62rem;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: .2rem;
}

.sidenav {
  display: flex;
  flex-direction: column;
  gap: .15rem;
  flex: 1;
}

.sidenav a {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .55rem .7rem;
  border-radius: 7px;
  color: var(--text-dim);
  font-size: .87rem;
  font-weight: 500;
  border-left: 2px solid transparent;
}

.sidenav a:hover {
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
}

.sidenav a.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-left-color: var(--accent);
}

.sidenav svg { width: 16px; height: 16px; flex-shrink: 0; opacity: .9; }

.logout-form {
  border-top: 1px solid var(--border-soft);
  padding-top: .75rem;
  margin-top: .75rem;
}

.main { flex: 1; min-width: 0; }

.container {
  max-width: 1220px;
  margin: 0 auto;
  padding: 2rem 2rem 3.5rem;
}

@media (max-width: 768px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    height: auto;
    position: static;
    flex-direction: row;
    align-items: center;
    padding: .75rem 1rem;
    overflow-x: auto;
    gap: 1rem;
  }
  .brand { margin-bottom: 0; }
  .brand-text span { display: none; }
  .sidenav { flex-direction: row; flex: none; }
  .sidenav a { white-space: nowrap; border-left: none; border-bottom: 2px solid transparent; }
  .sidenav a.active { border-left-color: transparent; border-bottom-color: var(--accent); }
  .logout-form { border-top: none; margin: 0 0 0 auto; padding: 0; flex-shrink: 0; }
  .container { padding: 1.25rem 1rem 2.5rem; }
}

/* ---------- typography ---------- */

h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0 0 .9rem;
}

h2 {
  font-size: 1rem;
  font-weight: 600;
  margin: 0 0 .9rem;
  display: flex;
  align-items: center;
  gap: .55rem;
}

h2::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 2px;
  background: var(--accent);
  flex-shrink: 0;
}

p { margin: 0 0 .9rem; }
p:last-child { margin-bottom: 0; }

code {
  font-family: var(--font-mono);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .1rem .35rem;
  font-size: .85em;
}

.muted, .muted a { color: var(--muted); font-size: .85rem; font-family: var(--font-mono); }
.mono { font-family: var(--font-mono); }

/* ---------- cards ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.4rem 1.5rem;
  margin-bottom: 1.5rem;
}

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

.table-wrap {
  overflow-x: auto;
  margin: 0 -0.1rem;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .87rem;
  min-width: 540px;
}

th, td {
  text-align: left;
  padding: .55rem .65rem;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: top;
}

th {
  color: var(--muted);
  font-weight: 600;
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  font-family: var(--font-mono);
  background: var(--surface-2);
  white-space: nowrap;
}

tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }

/* ---------- badges / delivery ticks ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: .4em;
  padding: .18rem .55rem;
  border-radius: 4px;
  font: 600 .72rem/1.4 var(--font-mono);
  text-transform: uppercase;
  letter-spacing: .04em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  white-space: nowrap;
}

.badge::before {
  content: "";
  width: .5em;
  height: .5em;
  border-radius: 50%;
  background: currentColor;
  flex: none;
}

.badge.approved, .badge.completed, .badge.connected { color: var(--accent); }
.badge.pending, .badge.processing { color: var(--warn); }
.badge.rejected, .badge.completed_with_errors, .badge.flagged, .badge.restricted, .badge.banned, .badge.opted-out { color: var(--danger); }

.badge.quality-green { color: var(--accent); }
.badge.quality-yellow { color: var(--warn); }
.badge.quality-red { color: var(--danger); }
.badge.quality-unknown { color: var(--muted); }

/* Message delivery status reuses WhatsApp's own tick vocabulary instead of a generic dot,
   since these five states map exactly to what the ticks mean in WhatsApp itself. */
.badge.queued, .badge.sent, .badge.delivered, .badge.read, .badge.failed {
  background: transparent;
  border-color: transparent;
  padding-left: 0;
  color: var(--muted);
}

.badge.queued::before { content: "○"; background: none; width: auto; height: auto; border-radius: 0; font-size: 1em; }
.badge.sent::before { content: "✓"; background: none; width: auto; height: auto; border-radius: 0; font-size: 1em; }
.badge.delivered::before { content: "✓✓"; background: none; width: auto; height: auto; border-radius: 0; font-size: 1em; letter-spacing: -.12em; }
.badge.read::before { content: "✓✓"; background: none; width: auto; height: auto; border-radius: 0; font-size: 1em; letter-spacing: -.12em; }
.badge.failed::before { content: "✕"; background: none; width: auto; height: auto; border-radius: 0; font-size: 1em; }

.badge.read { color: var(--read-blue); }
.badge.failed { color: var(--danger); }

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

form.inline { display: inline; }

label {
  display: block;
  font-size: .78rem;
  font-weight: 500;
  margin-bottom: .3rem;
  color: var(--text-dim);
}

input[type=text], input[type=password], input[type=tel], input[type=datetime-local],
input[type=date], input[type=file], select, textarea {
  width: 100%;
  padding: .55rem .7rem;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface-2);
  color: var(--text);
  font: 400 .88rem var(--font-sans);
  margin-bottom: .9rem;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input[type=file] { padding: .4rem; }

input[type=checkbox], input[type=radio] {
  width: auto;
  margin: 0;
  accent-color: var(--accent);
}

.field-row { display: flex; gap: 1.25rem; }
.field-row > div { flex: 1; }

button, .btn {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  padding: .55rem 1rem;
  border-radius: 7px;
  cursor: pointer;
  font: 600 .85rem var(--font-sans);
  text-decoration: none;
  display: inline-block;
}

button:hover, .btn:hover { background: var(--accent-dark); text-decoration: none; }

button.secondary, .btn.secondary {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
}
button.secondary:hover, .btn.secondary:hover { background: var(--surface-2); color: var(--text); border-color: var(--muted); }

button.danger, .btn.danger { background: var(--danger-strong); color: #fff; }
button.danger:hover, .btn.danger:hover { background: #a92c24; }

button.linklike {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font: 500 .85rem var(--font-sans);
  padding: .4rem .5rem;
}
button.linklike:hover { color: var(--text); text-decoration: underline; }

/* ---------- banners ---------- */

.error {
  background: var(--danger-soft);
  color: #ff8f88;
  border: 1px solid rgba(240, 82, 74, .35);
  padding: .7rem 1rem;
  border-radius: 7px;
  margin-bottom: 1rem;
}

.success {
  background: var(--accent-soft);
  color: #74e29c;
  border: 1px solid rgba(37, 211, 102, .35);
  padding: .7rem 1rem;
  border-radius: 7px;
  margin-bottom: 1rem;
}

/* ---------- stat tiles ---------- */

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

.stat {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--muted);
  border-radius: 8px;
  padding: 1rem;
  text-align: center;
}

.stat .value { font-size: 1.7rem; font-weight: 700; font-family: var(--font-mono); }
.stat .label { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-top: .2rem; }

.stat-read { border-left-color: var(--read-blue); }
.stat-read .value { color: var(--read-blue); }
.stat-failed { border-left-color: var(--danger); }
.stat-failed .value { color: var(--danger); }
.stat-queued { border-left-color: var(--warn); }
.stat-queued .value { color: var(--warn); }

/* ---------- filters / pickers ---------- */

.filters { display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; }
.filters > div { min-width: 160px; }

.variable-fields {
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  padding: 1rem 1rem .1rem;
  border-radius: 0 8px 8px 0;
  margin-bottom: 1rem;
}
.variable-fields label { font-family: var(--font-mono); }

.recipient-picker {
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: .6rem .7rem;
  background: var(--surface-2);
}
.recipient-picker label {
  display: flex;
  align-items: center;
  gap: .5rem;
  font-size: .85rem;
  font-weight: 400;
  color: var(--text);
  margin-bottom: .35rem;
}
.recipient-picker label:last-child { margin-bottom: 0; }

.tabs { display: flex; gap: .5rem; margin-bottom: 1rem; }
.tabs button { background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }
.tabs button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ---------- inbox: two-pane / master-detail chat ---------- */

.inbox-shell {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 1rem;
  height: calc(100vh - 4rem);
  min-height: 480px;
}

.inbox-list {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-y: auto;
}

.thread-row {
  display: block;
  padding: .8rem 1rem;
  border-bottom: 1px solid var(--border-soft);
  color: var(--text);
  border-left: 3px solid transparent;
}
.thread-row:hover { background: var(--surface-2); text-decoration: none; }
.thread-row.active { background: var(--accent-soft); border-left-color: var(--accent); }

.thread-row-top { display: flex; justify-content: space-between; gap: .5rem; }
.thread-name { font-weight: 600; font-size: .9rem; }
.thread-time { font-size: .68rem; white-space: nowrap; }
.thread-preview {
  font-size: .82rem;
  margin-top: .25rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inbox-chat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-empty { margin: auto; padding: 2rem; text-align: center; }

.chat-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: baseline;
  gap: .75rem;
  flex-wrap: wrap;
}
.chat-header h2 { margin: 0; }
.back-link { display: none; font-size: .82rem; color: var(--text-dim); }

.chat-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.chat-bubble {
  max-width: 72%;
  padding: .55rem .8rem;
  border-radius: 10px;
  font-size: .88rem;
}
.chat-bubble p { margin: 0 0 .2rem; white-space: pre-wrap; word-break: break-word; }
.chat-ts { font-size: .68rem; }

.chat-bubble.in {
  align-self: flex-start;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-left-radius: 2px;
}
.chat-bubble.out {
  align-self: flex-end;
  background: var(--accent-soft);
  border: 1px solid rgba(37, 211, 102, .35);
  border-bottom-right-radius: 2px;
}

.chat-system { align-self: center; margin: .3rem 0; }

.session-banner {
  margin: 0 1.25rem 1rem;
  padding: .5rem .8rem;
  border-radius: 7px;
  font: 600 .8rem var(--font-mono);
  text-align: center;
}
.session-banner.ok { background: var(--accent-soft); color: #74e29c; border: 1px solid rgba(37, 211, 102, .35); }
.session-banner.warn { background: var(--warn-soft); color: var(--warn); border: 1px solid rgba(242, 171, 39, .35); }

.reply-box {
  display: flex;
  gap: .6rem;
  align-items: flex-end;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border-soft);
  margin: 0;
}
.reply-box textarea { margin-bottom: 0; resize: vertical; }
.reply-box button { flex-shrink: 0; }

@media (max-width: 768px) {
  .inbox-shell { display: block; height: auto; }
  .inbox-list { margin-bottom: 1rem; }
  .inbox-chat { display: none; height: calc(100vh - 8rem); }
  .back-link { display: inline-block; }

  .inbox-shell.has-selection .inbox-list { display: none; }
  .inbox-shell.has-selection .inbox-chat { display: flex; }
}
