:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --panel-2: #f7f9fa;
  --border: #e5e8eb;
  --text: #111b21;
  --muted: #667781;
  --accent: #25d366;
  --accent-ink: #0b3d2e;
  --in-bubble: #ffffff;
  --out-bubble: #d9fdd3;
  --header: #f0f2f5;
  --shadow: 0 1px 2px rgba(0,0,0,.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --panel: #111b21;
    --panel-2: #202c33;
    --border: #222d34;
    --text: #e9edef;
    --muted: #8696a0;
    --accent: #25d366;
    --in-bubble: #202c33;
    --out-bubble: #005c4b;
    --header: #202c33;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; } /* precisa vencer .preview/.conversation/.empty-state */
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* ---------- Login ---------- */
.login-body { display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  padding: 40px 32px; width: 100%; max-width: 360px; text-align: center; box-shadow: var(--shadow);
}
.login-logo { font-size: 44px; }
.login-card h1 { font-size: 22px; margin: 8px 0 2px; }
.login-sub { color: var(--muted); margin: 0 0 24px; font-size: 14px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.login-card input, .login-card button { padding: 12px 14px; border-radius: 10px; font-size: 15px; }
.login-card input { border: 1px solid var(--border); background: var(--panel-2); color: var(--text); }
.login-card button { border: none; background: var(--accent); color: #06281c; font-weight: 600; cursor: pointer; }
.login-card button:hover { filter: brightness(.96); }
.login-error { color: #e5484d; font-size: 13px; margin: 4px 0 0; }

/* ---------- App layout ---------- */
.app { display: grid; grid-template-columns: 360px 1fr; height: 100dvh; }
.sidebar { display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--border); min-height: 0; }
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--header); border-bottom: 1px solid var(--border);
}
.sidebar-head .title { font-weight: 600; font-size: 17px; }
.head-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  border: none; background: transparent; color: var(--text); cursor: pointer;
  width: 36px; height: 36px; border-radius: 50%; font-size: 18px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--panel-2); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #b0b8bd; margin-right: 4px; }
.dot.on { background: var(--accent); }
.dot.off { background: #e5484d; }

.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-item { display: flex; gap: 12px; padding: 12px 16px; cursor: pointer; border-bottom: 1px solid var(--border); }
.chat-item:hover, .chat-item.active { background: var(--panel-2); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%; background: var(--panel-2);
  display: flex; align-items: center; justify-content: center; font-size: 18px; flex: 0 0 auto;
  color: var(--muted); border: 1px solid var(--border);
}
.chat-meta { min-width: 0; flex: 1; }
.chat-row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.chat-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { font-size: 11px; color: var(--muted); flex: 0 0 auto; }
.chat-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Conversation ---------- */
.chat { min-height: 0; display: flex; flex-direction: column; background:
  linear-gradient(var(--bg), var(--bg)); }
.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); gap: 8px; }
.empty-emoji { font-size: 56px; opacity: .6; }
.conversation { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.chat-head { display: flex; align-items: center; gap: 8px; padding: 10px 16px; background: var(--header); border-bottom: 1px solid var(--border); }
.chat-head-info { display: flex; flex-direction: column; min-width: 0; }
.chat-title { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { font-size: 12px; color: var(--muted); }
.back-btn { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 4px; }
.bubble {
  max-width: min(75%, 560px); padding: 7px 10px 5px; border-radius: 10px; font-size: 14.5px;
  line-height: 1.35; box-shadow: var(--shadow); word-wrap: break-word; white-space: pre-wrap;
}
.bubble.in { background: var(--in-bubble); align-self: flex-start; border-top-left-radius: 3px; }
.bubble.out { background: var(--out-bubble); align-self: flex-end; border-top-right-radius: 3px; }
.bubble img { max-width: 100%; border-radius: 8px; display: block; margin-bottom: 4px; }
.bubble .time { font-size: 10.5px; color: var(--muted); float: right; margin: 4px 0 0 10px; }
.bubble a { color: inherit; }

/* ---------- Composer ---------- */
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px; background: var(--header); border-top: 1px solid var(--border); }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); background: var(--panel);
  color: var(--text); border-radius: 20px; padding: 10px 14px; font: inherit; font-size: 14.5px;
  max-height: 120px; overflow-y: auto;
}
.send-btn {
  border: none; background: var(--accent); color: #06281c; width: 44px; height: 44px;
  border-radius: 50%; font-size: 18px; cursor: pointer; flex: 0 0 auto;
}
.send-btn:hover { filter: brightness(.96); }

/* ---------- Preview de imagem ---------- */
.preview {
  position: absolute; inset: 0; background: rgba(0,0,0,.85); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 16px; padding: 24px; z-index: 20;
}
.preview img { max-width: 90%; max-height: 65%; border-radius: 8px; object-fit: contain; }
.preview-actions { display: flex; gap: 8px; width: 100%; max-width: 520px; }
.preview-actions input { flex: 1; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel); color: var(--text); }
.btn-primary { padding: 12px 20px; border: none; border-radius: 10px; background: var(--accent); color: #06281c; font-weight: 600; cursor: pointer; }
.btn-ghost { padding: 12px 16px; border: 1px solid var(--border); border-radius: 10px; background: transparent; color: #fff; cursor: pointer; }

.chat { position: relative; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #e5484d; color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 14px;
  z-index: 50; box-shadow: var(--shadow);
}

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: none; }
  .chat { display: none; }
  body.viewing-chat .sidebar { display: none; }
  body.viewing-chat .chat { display: flex; }
  .back-btn { display: inline-flex; }
}
