:root {
  color-scheme: dark;
  --bg: #101214;
  --panel: #171a1f;
  --panel-2: #1f242b;
  --panel-3: #12161b;
  --border: #333a44;
  --text: #eef2f5;
  --muted: #9aa6b2;
  --ok: #33c481;
  --warn: #f2bd4d;
  --bad: #ef6b73;
  --info: #65a7ff;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--border);
  background: #20262e;
  color: var(--text);
  border-radius: 6px;
  padding: 7px 9px;
  cursor: pointer;
}

button:hover {
  border-color: var(--info);
}

input,
textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #101419;
  color: var(--text);
  padding: 9px 11px;
}

textarea {
  resize: vertical;
}

code {
  color: #c7d7ff;
}

.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  background: #13161a;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.home-link {
  width: 34px;
  height: 34px;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #20262e;
  color: var(--warn);
  text-decoration: none;
}

.home-link:hover {
  border-color: var(--info);
}

.home-link svg {
  width: 19px;
  height: 19px;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
}

.topbar strong {
  margin-right: 10px;
  color: var(--warn);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.login-panel {
  position: fixed;
  inset: 56px 0 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(16, 18, 20, 0.94);
  z-index: 10;
}

.login-panel[hidden] {
  display: none;
}

.login-box {
  width: min(360px, 100%);
  display: grid;
  gap: 12px;
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
}

.login-box h1 {
  margin: 0 0 2px;
  font-size: 18px;
  line-height: 1.2;
}

.login-box label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.layout {
  height: calc(100vh - 56px);
  display: grid;
  grid-template-columns: minmax(250px, 280px) minmax(300px, 420px) minmax(420px, 1fr) minmax(320px, 360px);
}

.gateways,
.threads,
.chat,
.diagnostics {
  min-width: 0;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--panel);
}

.gateways {
  display: grid;
  grid-template-rows: 48px auto auto minmax(0, 1fr);
}

.diagnostics {
  border-right: 0;
  display: grid;
  grid-template-rows: 48px minmax(0, 1fr) auto;
  overflow: hidden;
}

.diagnostics-scroll {
  min-height: 0;
  overflow: auto;
}

.qr-dock {
  border-top: 1px solid var(--border);
  background: var(--panel);
}

.panel-title {
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  font-weight: 650;
}

.scroll-pane {
  overflow: auto;
}

#gateway-list {
  height: auto;
  min-height: 0;
}

#thread-list {
  height: calc(100% - 106px);
}

.gateway-card,
.thread-card,
.event-row,
.command-row {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.gateway-card,
.thread-card {
  cursor: pointer;
}

.gateway-card.active,
.thread-card.active {
  background: #222832;
}

.row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
}

.row.wrap {
  flex-wrap: wrap;
}

.muted {
  color: var(--muted);
  font-size: 12px;
}

.mono {
  font-family: Consolas, "SFMono-Regular", monospace;
}

.pill {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  padding: 4px 7px;
  border-radius: 999px;
  background: #242b34;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.pill.ready,
.pill.running,
.pill.succeeded,
.pill.sent,
.pill.delivered,
.pill.read,
.pill.processed {
  color: var(--ok);
}

.pill.qr_pending,
.pill.authenticating,
.pill.pending,
.pill.running-command,
.pill.queued {
  color: var(--warn);
}

.pill.disconnected,
.pill.auth_failure,
.pill.phone_mismatch,
.pill.failed,
.pill.error,
.pill.crashed,
.pill.unhealthy {
  color: var(--bad);
}

.gateway-validation {
  border: 1px solid #92400e;
  background: #2b1d0b;
  color: #fde68a;
  padding: 8px;
  margin: 8px 0;
  border-radius: 6px;
  font-size: 12px;
}

.gateway-validation strong {
  display: block;
  color: #fbbf24;
  margin-bottom: 4px;
}

.actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin-top: 9px;
}

.actions button {
  padding: 6px 4px;
  font-size: 12px;
}

.gateway-management {
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.gateway-management-actions,
.gateway-form {
  display: grid;
  gap: 8px;
  padding: 10px;
}

.gateway-management-actions {
  grid-template-columns: 1fr;
}

.gateway-management-message {
  padding: 0 10px 10px;
  color: var(--info);
  font-size: 12px;
}

.gateway-filter-row {
  padding: 10px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.gateway-form {
  border-top: 1px solid var(--border);
}

.gateway-card.editing {
  cursor: default;
}

.gateway-form label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
}

.gateway-form-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #101419;
  color: var(--text);
  padding: 9px 11px;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 10px;
  border-bottom: 1px solid var(--border);
}

.tabs button.active {
  border-color: var(--info);
  background: #20314a;
}

.search-row {
  padding: 0 10px 10px;
  border-bottom: 1px solid var(--border);
}

.contact-filters {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 6px;
  padding: 0 10px 10px;
  border-bottom: 1px solid var(--border);
}

.contact-filters[hidden] {
  display: none;
}

.chat {
  display: grid;
  grid-template-rows: 48px 1fr 72px;
}

.messages {
  padding: 16px;
  overflow: auto;
  background: var(--panel-3);
}

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

.message {
  max-width: 78%;
  padding: 9px 11px;
  border-radius: 8px;
  margin-bottom: 10px;
  background: #20262e;
  border: 1px solid var(--border);
  overflow-wrap: anywhere;
}

.message.outbound {
  margin-left: auto;
  background: #173829;
  border-color: #275d42;
}

.message .body {
  white-space: pre-wrap;
}

.status-publish {
  display: grid;
  gap: 8px;
  max-width: 78%;
  margin-bottom: 12px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #181e25;
}

.status-publish textarea {
  min-height: 86px;
}

.status-publish-actions {
  display: grid;
  grid-template-columns: minmax(90px, 120px) auto;
  gap: 8px;
  justify-content: start;
}

.message-media {
  display: grid;
  gap: 8px;
  margin: 8px 0;
}

.media-preview,
.media-chip,
.media-pending {
  display: block;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #11161c;
  color: var(--text);
  text-decoration: none;
  overflow: hidden;
}

.media-preview img,
.media-preview video {
  display: block;
  width: 100%;
  max-height: 360px;
  object-fit: contain;
  background: #05070a;
}

.media-preview audio {
  display: block;
  width: 100%;
  padding: 8px;
}

.media-chip,
.media-pending {
  padding: 9px 10px;
}

.media-chip:hover {
  border-color: var(--info);
}

.media-chip.unavailable {
  color: var(--muted);
}

.media-actions {
  display: flex;
  justify-content: flex-end;
  padding: 0 8px 8px;
}

.media-actions button {
  padding: 5px 8px;
  font-size: 12px;
}

.media-meta {
  padding: 6px 8px;
  color: var(--muted);
  font-size: 12px;
}

.media-transcription {
  width: calc(100% - 16px);
  margin: 0 8px 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: rgba(6, 12, 22, 0.42);
  color: var(--text);
  font-size: 12px;
  line-height: 1.35;
}

.media-transcription span {
  display: block;
  margin-bottom: 4px;
  color: var(--accent);
  font-weight: 700;
  text-transform: uppercase;
}

.media-transcription p {
  margin: 0;
}

.media-transcription.pending {
  color: var(--muted);
}

.media-transcription.failed {
  border-color: rgba(255, 91, 110, 0.4);
  color: #ff9aa8;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
  background: var(--panel-2);
}

.composer[hidden] {
  display: none;
}

.contact-detail {
  display: grid;
  gap: 12px;
  max-width: 980px;
}

.contact-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.contact-field {
  display: grid;
  gap: 4px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #181e25;
  overflow-wrap: anywhere;
}

.json-block {
  margin: 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #0c1117;
  color: #d7e2f1;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.section-head {
  padding: 12px 14px 6px;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
}

.qr {
  margin: 8px 14px 14px;
  min-height: 0;
  max-height: none;
  display: grid;
  place-items: center;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--muted);
  background: #11161c;
  overflow-wrap: anywhere;
  padding: 10px;
}

.qr.available {
  color: var(--text);
  gap: 6px;
  align-content: center;
  justify-items: center;
}

.qr .row {
  width: 100%;
}

.qr button {
  padding: 5px 8px;
  font-size: 12px;
}

.qr-image {
  width: 176px;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  padding: 12px;
  border-radius: 4px;
  background: #fff;
}

.qr-image svg {
  width: 100%;
  height: 100%;
  display: block;
}

.qr details {
  width: 100%;
}

.qr summary {
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
}

.qr textarea {
  width: 100%;
  min-height: 58px;
  margin-top: 8px;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #0e1217;
  color: var(--text);
  padding: 8px;
}

@media (max-width: 1180px) {
  .layout {
    grid-template-columns: minmax(220px, 260px) minmax(260px, 340px) minmax(360px, 1fr);
  }

  .diagnostics {
    display: none;
  }
}

@media (max-width: 860px) {
  .layout {
    height: auto;
    min-height: calc(100vh - 56px);
    grid-template-columns: 1fr;
  }

  .gateways,
  .threads,
  .chat {
    min-height: 320px;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  #gateway-list,
  #thread-list {
    height: auto;
    max-height: 360px;
  }
}
