:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --border: #e4e6eb;
  --text: #111827;
  --text-dim: #667085;
  --brand: #111827;
  --accent: #16a34a;
  --accent-dim: #dcfce7;
  --bubble-in: #ffffff;
  --bubble-out-bot: #e5e7eb;
  --bubble-out-agent: #dcf8c6;
  --danger: #dc2626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --panel: #111b21;
    --border: #202c33;
    --text: #e9edef;
    --text-dim: #8696a0;
    --brand: #202c33;
    --accent: #00a884;
    --accent-dim: #05341f;
    --bubble-in: #202c33;
    --bubble-out-bot: #2a3942;
    --bubble-out-agent: #005c4b;
    --danger: #f87171;
  }
}

* { box-sizing: border-box; }

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

/* ---------- Login ---------- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  padding: 20px;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 28px;
  width: 100%;
  max-width: 340px;
  text-align: center;
}
.login-logo { font-size: 40px; }
.login-card h1 { margin: 8px 0 0; font-size: 20px; }
.login-sub { color: var(--text-dim); margin: 4px 0 20px; font-size: 14px; }
.login-card label {
  display: block;
  text-align: left;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 14px;
}
.login-card input {
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
.login-card button {
  width: 100%;
  padding: 11px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 6px;
}
.login-error {
  background: #fee2e2;
  color: var(--danger);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 14px;
}

/* ---------- App shell ---------- */
.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

.conv-list {
  width: 360px;
  max-width: 100%;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
.conv-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--brand);
  color: #fff;
}
.brand { font-weight: 700; font-size: 15px; }
.header-actions { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.logout-link { color: #fff; text-decoration: none; opacity: .8; font-size: 16px; }

.conv-items { flex: 1; overflow-y: auto; }
.empty-hint { color: var(--text-dim); padding: 20px; font-size: 14px; text-align: center; }

.conv-item {
  display: flex;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  align-items: flex-start;
}
.conv-item:hover, .conv-item.active { background: var(--bg); }
.conv-avatar {
  width: 42px; height: 42px; border-radius: 50%;
  background: var(--accent-dim); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; flex-shrink: 0;
}
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11px; color: var(--text-dim); flex-shrink: 0; }
.conv-preview {
  font-size: 13px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: flex; align-items: center; gap: 4px;
}
.conv-badges { display: flex; gap: 4px; margin-top: 4px; }
.badge {
  font-size: 10px; padding: 2px 6px; border-radius: 10px; font-weight: 600;
}
.badge-unread { background: var(--accent); color: #fff; }
.badge-paused { background: #fde68a; color: #92400e; }
.badge-agent { background: var(--accent-dim); color: var(--accent); }

/* ---------- Thread pane ---------- */
.thread-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.thread-empty {
  flex: 1; display: flex; align-items: center; justify-content: center;
  color: var(--text-dim); font-size: 14px;
}
.thread-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.hidden { display: none !important; }

.thread-header {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.back-btn {
  display: none;
  background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text);
}
.thread-contact { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.thread-contact strong { font-size: 14px; }
.thread-contact span { font-size: 12px; color: var(--text-dim); }
.thread-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.bot-status { font-size: 12px; color: var(--text-dim); }
.btn-secondary {
  font-size: 12px; padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  cursor: pointer;
}
.btn-secondary:hover { background: var(--bg); }

.thread-messages {
  flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px;
}
.msg {
  max-width: 75%; padding: 8px 12px; border-radius: 12px; font-size: 14px;
  line-height: 1.4; white-space: pre-wrap; word-break: break-word; position: relative;
}
.msg-in { align-self: flex-start; background: var(--bubble-in); border: 1px solid var(--border); }
.msg-out.msg-bot { align-self: flex-end; background: var(--bubble-out-bot); }
.msg-out.msg-agent { align-self: flex-end; background: var(--bubble-out-agent); }
.msg-meta { display: block; font-size: 10px; color: var(--text-dim); margin-top: 4px; }

.thread-composer {
  display: flex; gap: 8px; padding: 10px 14px; background: var(--panel); border-top: 1px solid var(--border);
}
.thread-composer textarea {
  flex: 1; resize: none; border-radius: 18px; border: 1px solid var(--border);
  padding: 10px 14px; font-size: 14px; font-family: inherit; background: var(--bg); color: var(--text);
  max-height: 120px;
}
.thread-composer button {
  border: none; background: var(--accent); color: #fff; border-radius: 50%;
  width: 42px; height: 42px; font-size: 13px; cursor: pointer; flex-shrink: 0;
}

/* ---------- Mobile ---------- */
@media (max-width: 820px) {
  .conv-list { width: 100%; border-right: none; }
  .app.thread-open .conv-list { display: none; }
  .thread-pane { display: none; }
  .app.thread-open .thread-pane { display: flex; }
  .back-btn { display: inline-block; }
}
