:root {
  color-scheme: dark;
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #263247;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #22c55e;
  --primary-hover: #16a34a;
  --danger: #ef4444;
  --warning: #f59e0b;
  --info: #38bdf8;
  --radius: 12px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.45;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small-text { font-size: 0.82rem; }
.center { text-align: center; }
.error-text { color: #fca5a5; font-size: 0.88rem; margin: 0; }
.ok-text { color: #86efac; }

/* Login */
.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.login-card {
  width: 100%;
  max-width: 390px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.login-card h1 { margin: 0; font-size: 1.4rem; }
.login-card form { display: flex; flex-direction: column; gap: 0.75rem; }

.brand-badge {
  width: 44px;
  height: 44px;
  border-radius: 11px;
  background: linear-gradient(135deg, #22c55e, #0ea5e9);
  color: #052e16;
  font-weight: 800;
  display: grid;
  place-items: center;
  letter-spacing: 0.5px;
}

.brand-badge.small { width: 34px; height: 34px; font-size: 0.85rem; border-radius: 9px; }

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(17, 24, 39, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-title { display: block; font-size: 0.98rem; line-height: 1.2; }

.tabs {
  display: flex;
  gap: 0.35rem;
  background: var(--bg);
  padding: 0.25rem;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.tab-btn {
  background: transparent;
  border: 0;
  color: var(--muted);
  padding: 0.45rem 0.9rem;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  font-size: 0.9rem;
}

.tab-btn.active {
  background: var(--surface-2);
  color: var(--text);
}

/* Status pills */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}

.status-connected { background: rgba(34, 197, 94, 0.16); color: #86efac; }
.status-waiting_qr { background: rgba(245, 158, 11, 0.18); color: #fcd34d; }
.status-disconnected, .status-logged_out, .status-error { background: rgba(239, 68, 68, 0.18); color: #fca5a5; }

/* Switch */
.switch-label {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
}

.switch-label input { display: none; }
.switch-track {
  width: 38px;
  height: 22px;
  border-radius: 999px;
  background: #334155;
  position: relative;
  transition: background 0.2s;
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
}
.switch-label input:checked + .switch-track { background: var(--primary); }
.switch-label input:checked + .switch-track::after { transform: translateX(16px); }

/* Layout */
.content {
  max-width: 1320px;
  margin: 0 auto;
  padding: 1.25rem;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.card-header h2, .card-header h3 { margin: 0; font-size: 1.05rem; }

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

.stats-grid {
  margin-top: 1rem;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
}
.stat-label { display: block; color: var(--muted); font-size: 0.82rem; }
.stat-value { display: block; font-size: 1.6rem; margin-top: 0.2rem; }

/* KV list */
.kv-list {
  display: grid;
  gap: 0.65rem;
  margin: 0 0 1.1rem;
}
.kv-list div {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  border-bottom: 1px solid rgba(148, 163, 184, 0.12);
  padding-bottom: 0.45rem;
}
.kv-list dt { color: var(--muted); font-size: 0.88rem; }
.kv-list dd { margin: 0; font-weight: 500; text-align: right; word-break: break-all; }

/* QR */
.qr-box {
  min-height: 250px;
  display: grid;
  place-items: center;
  background: var(--bg);
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 1rem;
  margin-bottom: 0.65rem;
}
.qr-box img {
  width: 230px;
  height: 230px;
  border-radius: 8px;
  background: #fff;
  padding: 6px;
}

/* Inputs & Buttons */
label { display: block; font-size: 0.85rem; color: #cbd5e1; margin-bottom: 0.3rem; }
input[type="text"], input[type="password"], input[type="url"], input[type="number"], input[type="search"], select, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
  font: inherit;
  font-size: 0.92rem;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(34, 197, 94, 0.45);
  border-color: var(--primary);
}
textarea { resize: vertical; }

.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0.55rem 0.95rem;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
}
.btn-primary { background: var(--primary); color: #052e16; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #293548; }
.btn-danger { background: rgba(239, 68, 68, 0.16); color: #fca5a5; border-color: rgba(239, 68, 68, 0.35); }
.btn-danger:hover { background: rgba(239, 68, 68, 0.28); }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn-ghost:hover { color: var(--text); }
.btn-sm { padding: 0.35rem 0.7rem; font-size: 0.82rem; }
.btn-full { width: 100%; }
.btn-row { display: flex; gap: 0.6rem; flex-wrap: wrap; }
.btn-row.end { justify-content: flex-end; }

/* Conversations */
.chat-layout {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  gap: 1rem;
  height: calc(100vh - 115px);
  min-height: 520px;
}

.chat-sidebar {
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}
.sidebar-search { padding: 0.75rem; border-bottom: 1px solid var(--border); }
.chat-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}
.chat-item {
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid rgba(148, 163, 184, 0.1);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.chat-item:hover { background: rgba(255, 255, 255, 0.03); }
.chat-item.active { background: var(--surface-2); }
.chat-item-top { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; }
.chat-name { font-weight: 600; font-size: 0.92rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-preview { font-size: 0.82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.14rem 0.45rem;
  border-radius: 999px;
  white-space: nowrap;
}
.badge-active { background: rgba(34, 197, 94, 0.16); color: #86efac; }
.badge-paused { background: rgba(245, 158, 11, 0.2); color: #fcd34d; }
.badge-info { background: rgba(56, 189, 248, 0.18); color: #7dd3fc; }
.badge-danger { background: rgba(239, 68, 68, 0.18); color: #fca5a5; }

.chat-main { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.empty-state { flex: 1; display: grid; place-items: center; color: var(--muted); padding: 2rem; text-align: center; }
.chat-thread-wrap { display: flex; flex-direction: column; height: 100%; }
.chat-thread-header {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}
.chat-thread-header h3 { margin: 0; font-size: 1rem; }
.thread-actions { display: flex; align-items: center; gap: 0.6rem; }

.thread-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  background: var(--bg);
}

.msg-bubble {
  max-width: 75%;
  padding: 0.6rem 0.85rem;
  border-radius: 12px;
  font-size: 0.91rem;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg-in { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg-bot { align-self: flex-end; background: #14532d; border-bottom-right-radius: 4px; }
.msg-human { align-self: flex-end; background: #1e3a8a; border-bottom-right-radius: 4px; }
.msg-meta {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.72rem;
  color: rgba(241, 245, 249, 0.68);
  margin-top: 0.3rem;
}

/* Forms & Rules */
.stack-form { display: flex; flex-direction: column; gap: 0.85rem; }
.form-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
.form-row-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.75rem; }
.provider-group { display: flex; flex-direction: column; gap: 0.75rem; padding: 0.8rem; background: rgba(15, 23, 42, 0.6); border: 1px solid var(--border); border-radius: 10px; }
.rules-card { margin-top: 1rem; }
.table-wrap { overflow-x: auto; }
.rules-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.rules-table th, .rules-table td { text-align: left; padding: 0.65rem 0.6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.rules-table th { color: var(--muted); font-weight: 600; font-size: 0.8rem; }

.sim-result {
  margin-top: 1rem;
  padding: 0.85rem;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.88rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

/* Modal */
.modal {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  width: min(560px, 92vw);
}
.modal::backdrop { background: rgba(0, 0, 0, 0.65); }
.checkbox-row { display: inline-flex; align-items: center; gap: 0.5rem; cursor: pointer; }
.checkbox-row input { width: auto; }

/* Toast */
.toast {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0.65rem 1rem;
  border-radius: 10px;
  font-size: 0.88rem;
  z-index: 50;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

@media (max-width: 900px) {
  .grid-2, .stats-grid, .chat-layout, .form-row-2, .form-row-3 {
    grid-template-columns: 1fr;
  }
  .chat-layout { height: auto; }
  .chat-sidebar { max-height: 300px; }
  .chat-main { min-height: 440px; }
}
