:root {
  color-scheme: light dark;
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273548;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22c55e;
  --accent-2: #16a34a;
  --danger: #ef4444;
  --bubble-out: #22c55e;
  --bubble-in: #334155;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
}

.login-card h1 {
  font-size: 1.25rem;
  margin: 0 0 4px;
}

.login-card p.subtitle {
  color: var(--muted);
  margin: 0 0 24px;
  font-size: 0.9rem;
}

label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 6px;
}

input[type="email"],
input[type="password"],
input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 16px;
}

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

button {
  cursor: pointer;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 10px 16px;
}

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #06240f;
}
.btn-primary:hover { background: var(--accent-2); }

.btn-secondary {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-danger { background: var(--danger); color: white; }

.error-text {
  color: var(--danger);
  font-size: 0.85rem;
  min-height: 1.2em;
  margin: -8px 0 12px;
}

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

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  overflow: hidden;
  transition: width 0.15s ease, padding 0.15s ease;
}
.sidebar.collapsed {
  width: 0;
  padding: 0;
  border-right: none;
}

.brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 20px 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
  white-space: nowrap;
}

.brand {
  font-weight: 700;
  font-size: 1.1rem;
}

.sidebar-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  white-space: nowrap;
}

.icon-btn {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
}
.icon-btn:hover { background: var(--border); }

.sidebar-expand-btn {
  position: fixed;
  top: 20px;
  left: 12px;
  z-index: 5;
}

.tab-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  background: transparent;
  color: var(--text);
  padding: 12px 20px;
  font-weight: 500;
  border-radius: 0;
}
.tab-btn:hover { background: var(--panel-2); }
.tab-btn.active { background: var(--panel-2); border-right: 3px solid var(--accent); }

.sidebar-footer {
  margin-top: auto;
  padding: 12px 20px 0;
  border-top: 1px solid var(--border);
}

.sidebar-footer .who {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 10px;
  word-break: break-all;
}

.role-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: #06240f;
  margin-bottom: 8px;
}

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

.panel {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
  padding: 24px;
  overflow: auto;
}
.panel.active { display: flex; }

.panel h2 { margin-top: 0; }

.panel-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.panel-header-row h2 { margin-bottom: 0; }

.panel-header-actions {
  display: flex;
  gap: 8px;
}

/* ---------- QR panel ---------- */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  margin-bottom: 20px;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status-dot.ready { background: var(--accent); }
.status-dot.qr { background: #eab308; }
.status-dot.auth_failure, .status-dot.disconnected { background: var(--danger); }

.qr-box {
  background: white;
  padding: 20px;
  border-radius: 12px;
  display: inline-flex;
  width: fit-content;
}
.qr-box img { width: 260px; height: 260px; display: block; }

/* ---------- Chat panel ---------- */
.chat-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.chat-list-pane {
  width: 300px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

.chat-search {
  padding: 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-search input { margin-bottom: 0; }

.chat-list {
  flex: 1;
  overflow-y: auto;
}

.chat-list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.chat-list-item:hover { background: var(--panel-2); }
.chat-list-item.active { background: var(--panel-2); }

.chat-avatar {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.9rem;
  color: #06240f;
  background: var(--accent);
}

.chat-list-item .item-main {
  flex: 1;
  min-width: 0;
}
.chat-list-item .item-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.chat-list-item .name { font-weight: 600; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list-item .timestamp { color: var(--muted); font-size: 0.72rem; flex-shrink: 0; }
.chat-list-item .preview { color: var(--muted); font-size: 0.8rem; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-list-item .unread-badge {
  flex-shrink: 0;
  background: var(--accent);
  color: #06240f;
  font-size: 0.7rem;
  font-weight: 700;
  border-radius: 999px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-thread {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.thread-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.thread-header-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
#thread-header-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chat-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.tag-pill {
  display: inline-block;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 999px;
  white-space: nowrap;
}

.tag-picker-wrap { position: relative; flex-shrink: 0; }

.tag-picker {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 220px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  z-index: 10;
}

.tag-picker-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
}
.tag-picker-item:hover { background: var(--panel-2); }
.tag-picker-item input { margin: 0; }

.tag-picker-empty {
  padding: 8px;
  color: var(--muted);
  font-size: 0.8rem;
}

.thread-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bubble {
  max-width: 65%;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 0.9rem;
  line-height: 1.35;
}
.bubble.in { align-self: flex-start; background: var(--bubble-in); }
.bubble.out { align-self: flex-end; background: var(--bubble-out); color: #06240f; }

.attachment {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.12);
  color: inherit;
  text-decoration: none;
}
.attachment:hover { background: rgba(0, 0, 0, 0.2); }
.attachment-icon { font-size: 1.2rem; flex-shrink: 0; }
.attachment-name {
  font-size: 0.85rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-caption { margin-top: 6px; font-size: 0.9rem; }

.attachment-image {
  display: block;
  max-width: 260px;
  max-height: 260px;
  width: 100%;
  border-radius: 8px;
  object-fit: cover;
}

.composer-attachment {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--panel-2);
  font-size: 0.85rem;
}

.thread-composer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
}
.thread-composer input { flex: 1; min-width: 0; margin-bottom: 0; }
.thread-composer button { flex: 0 0 auto; width: auto; white-space: nowrap; }

.order-pane {
  width: 320px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-left: 1px solid var(--border);
}

.order-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

.order-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 18px;
}
.order-form .field { min-width: 0; }
.order-form label { margin-bottom: 6px; }
.order-form select,
.order-form input {
  width: 100%;
  margin-bottom: 0;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 0.9rem;
}
.order-form input { text-transform: uppercase; }

.order-title-preview {
  margin: 0 18px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 600;
  word-break: break-word;
}

.order-status {
  padding: 6px 18px 14px;
  font-size: 0.75rem;
  color: var(--muted);
  min-height: 1.2em;
}

/* ---------- Pipeline panel ---------- */
.pipeline-board {
  display: flex;
  gap: 16px;
  flex: 1;
  min-height: 0;
  overflow-x: auto;
  padding-bottom: 8px;
}

.pipeline-column {
  width: 260px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.pipeline-column-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.pipeline-count {
  background: var(--panel-2);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  padding: 1px 8px;
  border-radius: 999px;
}

.pipeline-column-body {
  flex: 1;
  min-height: 120px;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.pipeline-column-body.drag-over { background: var(--panel-2); }

.pipeline-card {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: grab;
}
.pipeline-card:hover { border-color: var(--accent); }
.pipeline-card.dragging { opacity: 0.4; }
.pipeline-card-name { font-weight: 600; font-size: 0.88rem; }
.pipeline-card-phone { color: var(--muted); font-size: 0.78rem; margin-top: 2px; }

/* ---------- Tags panel ---------- */
.tags-board {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.tag-card {
  width: 260px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  align-self: flex-start;
}

.tag-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.tag-card-body {
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
}

.tag-contact { font-size: 0.85rem; padding: 4px 0; cursor: pointer; }
.tag-contact:hover { color: var(--accent); text-decoration: underline; }
.tag-card-empty { color: var(--muted); font-size: 0.8rem; }

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

/* ---------- Users panel ---------- */
.users-form {
  display: flex;
  gap: 10px;
  align-items: end;
  flex-wrap: wrap;
  margin-bottom: 20px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 16px;
  border-radius: 10px;
}
.users-form .field { min-width: 200px; }
.users-form label { margin-bottom: 6px; }
.users-form input, .users-form select {
  margin-bottom: 0;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
}

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
th { color: var(--muted); font-weight: 600; }

.hidden { display: none !important; }
