:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #16181d;
  --muted: #6b7280;
  --line: #e4e6eb;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --danger: #dc2626;
  --warn-bg: #fff7e0;
  --warn-ink: #7a5600;
  --bubble-in: #eceef2;
  --bubble-out: #dbe7ff;
  --bubble-out-ink: #0d2b6b;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1014;
    --panel: #171a21;
    --text: #e8eaee;
    --muted: #9aa1ae;
    --line: #272c36;
    --accent: #4f8cff;
    --accent-ink: #06142e;
    --danger: #f87171;
    --warn-bg: #3a2f10;
    --warn-ink: #f3d98a;
    --bubble-in: #222733;
    --bubble-out: #1f3b72;
    --bubble-out-ink: #dce8ff;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

.icon { width: 20px; height: 20px; display: block; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 10px;
  padding: 8px 14px;
  font-weight: 600;
  min-height: 38px;
}
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }

.icon-btn {
  border: 0;
  background: transparent;
  border-radius: 10px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  position: relative;
}
.icon-btn:hover { background: var(--bg); }
.icon-btn.on::after {
  content: "";
  position: absolute;
  top: 8px;
  right: 8px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #16a34a;
}

/* ---------- login ---------- */

.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login form {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 28px 24px;
  display: grid;
  gap: 14px;
}
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 6px; color: var(--muted); }
.field { display: grid; gap: 6px; font-weight: 600; font-size: 13px; }
.field input {
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 10px;
  padding: 10px 12px;
  font-weight: 400;
  font-size: 16px;
}
.field input:focus, .composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

/* ---------- layout ---------- */

.shell {
  height: 100dvh;
  display: grid;
  grid-template-columns: 380px minmax(0, 1fr);
}

.list-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 17px; margin: 0; flex: 1; }
.team { display: flex; }
.team .avatar { margin-left: -6px; border: 2px solid var(--panel); }
.team .avatar:first-child { margin-left: 0; }
.team .avatar.off { opacity: .35; }

.avatar {
  --c: #6b7280;
  width: 34px;
  height: 34px;
  flex: none;
  border-radius: 50%;
  background: var(--c);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
}
.avatar.small { width: 24px; height: 24px; font-size: 11px; }
.team .avatar.small { width: 26px; height: 26px; }

.banner {
  margin: 10px 12px 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--warn-bg);
  color: var(--warn-ink);
  font-size: 13.5px;
  display: flex;
  gap: 10px;
  align-items: center;
}
.banner span { flex: 1; }
.banner .btn { min-height: 32px; padding: 4px 12px; }

.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 10px 12px 0;
  border-bottom: 1px solid var(--line);
}
.tab {
  border: 0;
  background: transparent;
  padding: 8px 2px 10px;
  color: var(--muted);
  font-weight: 600;
  font-size: 13.5px;
  border-bottom: 2px solid transparent;
  display: flex;
  gap: 6px;
  justify-content: center;
  align-items: center;
}
.tab.active { color: var(--text); border-color: var(--accent); }
.count {
  min-width: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--bg);
  color: var(--muted);
  font-size: 12px;
  line-height: 20px;
  text-align: center;
}
.count.hot { background: var(--danger); color: #fff; }

.list { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.empty { padding: 40px 24px; text-align: center; color: var(--muted); }

.conv {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  text-align: left;
  display: flex;
  gap: 12px;
  padding: 12px;
  align-items: flex-start;
}
.conv:hover { background: var(--bg); }
.conv.active { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.conv .body { flex: 1; min-width: 0; }
.conv .row { display: flex; gap: 8px; align-items: baseline; }
.conv .name { font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .time { color: var(--muted); font-size: 12px; flex: none; }
.conv .preview { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.conv.unread .preview { color: var(--text); font-weight: 600; }
.conv .meta { display: flex; gap: 8px; align-items: center; margin-top: 6px; flex-wrap: wrap; }
.conv .badge {
  margin-left: auto;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 10px;
  min-width: 20px;
  padding: 0 6px;
  font-size: 12px;
  line-height: 20px;
  text-align: center;
  font-weight: 700;
}

.tag {
  --c: #6b7280;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px 2px 6px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--c) 16%, transparent);
  color: var(--text);
}
.tag::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--c); }
.tag.wait { --c: #d97706; }

/* ---------- conversa ---------- */

.chat-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  padding-top: env(safe-area-inset-top);
}
.chat-empty { margin: auto; color: var(--muted); text-align: center; padding: 24px; }

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.chat-head .who { flex: 1; min-width: 140px; }
.chat-head .who b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .who small { color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .actions { display: flex; gap: 8px; }
.chat-head .actions .btn { padding: 6px 12px; min-height: 36px; }
.back { display: none; }

.notice {
  padding: 8px 14px;
  font-size: 13.5px;
  background: var(--warn-bg);
  color: var(--warn-ink);
}

.messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.msg { max-width: min(78%, 560px); display: flex; flex-direction: column; gap: 2px; }
.msg .bubble {
  background: var(--bubble-in);
  padding: 8px 12px;
  border-radius: 14px 14px 14px 4px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg .bubble a { color: inherit; text-decoration: underline; }
.msg .info { font-size: 11.5px; color: var(--muted); padding: 0 4px; }
.msg.agent { align-self: flex-end; align-items: flex-end; }
.msg.agent .bubble { background: var(--bubble-out); color: var(--bubble-out-ink); border-radius: 14px 14px 4px 14px; }
.msg.system { align-self: center; max-width: 90%; }
.msg.system .bubble { background: transparent; color: var(--muted); font-size: 12.5px; padding: 2px 8px; text-align: center; }

.composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 140px;
  overflow-y: hidden;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  padding: 10px 12px;
  font-size: 16px;
  line-height: 1.35;
}
.composer textarea:disabled { opacity: .6; }
.composer .send { width: 44px; height: 44px; padding: 0; display: grid; place-items: center; border-radius: 12px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #1f2937;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  max-width: calc(100vw - 32px);
  z-index: 10;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .3);
}

.offline {
  background: var(--danger);
  color: #fff;
  text-align: center;
  font-size: 13px;
  padding: 4px;
}

/* ---------- celular ---------- */

@media (max-width: 800px) {
  .shell { grid-template-columns: 1fr; }
  .list-pane { border-right: 0; }
  .chat-pane {
    position: fixed;
    inset: 0;
    background: var(--bg);
    transform: translateX(100%);
    transition: transform .18s ease-out;
    z-index: 5;
  }
  body.chat-open .chat-pane { transform: none; }
  .back { display: grid; }
  .chat-head { flex-wrap: nowrap; padding: 8px; gap: 8px; }
  .chat-head .who { min-width: 0; }
  .chat-head > .tag { display: none; } /* o aviso logo abaixo já diz com quem está */
  .msg { max-width: 86%; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-pane { transition: none; }
}
