* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f0f2f5;
  --sidebar-bg: #fff;
  --chat-bg: #efeae2;
  --header-bg: #f0f2f5;
  --green: #25d366;
  --green-dark: #075e54;
  --blue: #34b7f1;
  --bubble-out: #d9fdd3;
  --bubble-in: #fff;
  --text: #111b21;
  --text-secondary: #667781;
  --border: #e9edef;
  --danger: #e74c3c;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

#app {
  display: flex;
  height: 100vh;
  max-width: 1600px;
  margin: 0 auto;
  box-shadow: 0 0 20px rgba(0,0,0,0.05);
}

/* --- Sidebar --- */
#sidebar {
  width: 380px;
  min-width: 300px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
}

.sidebar-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-title h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

.brand-dot {
  width: 36px; height: 36px;
  background: var(--green);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 16px;
}

.sidebar-actions button {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
}
.sidebar-actions button:hover { background: var(--border); }

.search-box {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.search-box input {
  width: 100%;
  padding: 8px 14px;
  border: none;
  border-radius: 8px;
  background: var(--bg);
  font-size: 14px;
  outline: none;
}

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

.conv-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.15s;
  border-bottom: 1px solid var(--border);
}
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: #f0f9f0; }

.conv-item .avatar {
  width: 48px; height: 48px;
  border-radius: 50%;
  background: #dfe5e7;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 18px;
  color: #fff;
  flex-shrink: 0;
  margin-right: 12px;
}

.conv-info { flex: 1; min-width: 0; }
.conv-info .name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-info .preview {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.conv-meta {
  text-align: right;
  flex-shrink: 0;
  margin-left: 8px;
}
.conv-meta .time {
  font-size: 12px;
  color: var(--text-secondary);
}
.conv-meta .badge {
  background: var(--green);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  border-radius: 50%;
  width: 20px; height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 4px;
}

/* --- Chat panel --- */
#chatPanel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
  position: relative;
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
}
.empty-state h3 { font-size: 24px; color: var(--text); margin-top: 16px; }
.empty-state p { margin-top: 8px; }

.chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.chat-header {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
  gap: 8px;
}

.btn-back {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 4px;
}

.chat-contact {
  display: flex;
  align-items: center;
  gap: 12px;
}
.chat-contact .avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: #dfe5e7;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  color: #fff;
}
.chat-name { font-weight: 600; font-size: 15px; }
.chat-phone { font-size: 12px; color: var(--text-secondary); }

/* Message list */
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px 48px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg-bubble {
  max-width: 65%;
  padding: 8px 12px;
  border-radius: 8px;
  position: relative;
  word-wrap: break-word;
  font-size: 14.5px;
  line-height: 1.4;
  box-shadow: 0 1px 1px rgba(0,0,0,0.06);
}

.msg-in {
  background: var(--bubble-in);
  align-self: flex-start;
  border-top-left-radius: 0;
}
.msg-out {
  background: var(--bubble-out);
  align-self: flex-end;
  border-top-right-radius: 0;
}

.msg-media img, .msg-media video {
  max-width: 100%;
  max-height: 300px;
  border-radius: 6px;
  display: block;
  margin-bottom: 4px;
}
.msg-media audio {
  width: 100%;
  margin-bottom: 4px;
}

.msg-document {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: rgba(0,0,0,0.04);
  border-radius: 6px;
  margin-bottom: 4px;
  text-decoration: none;
  color: var(--text);
}
.msg-document svg { flex-shrink: 0; }
.msg-document .doc-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.msg-location {
  display: block;
  padding: 8px;
  background: rgba(0,0,0,0.04);
  border-radius: 6px;
  margin-bottom: 4px;
  text-decoration: none;
  color: var(--text);
  font-size: 13px;
}
.msg-location svg { vertical-align: middle; margin-right: 4px; }

.msg-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 2px;
}
.msg-time {
  font-size: 11px;
  color: var(--text-secondary);
}
.msg-status { font-size: 14px; }
.msg-status.sent { color: var(--text-secondary); }
.msg-status.delivered { color: var(--text-secondary); }
.msg-status.read { color: var(--blue); }
.msg-status.failed { color: var(--danger); }

.msg-caption {
  margin-top: 4px;
  font-size: 14px;
}

/* Date separator */
.date-sep {
  text-align: center;
  margin: 8px 0;
}
.date-sep span {
  background: #e1f2fb;
  padding: 4px 12px;
  border-radius: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  box-shadow: 0 1px 1px rgba(0,0,0,0.06);
}

/* Composer */
.composer {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: var(--header-bg);
  border-top: 1px solid var(--border);
  gap: 8px;
  position: relative;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  display: flex;
  align-items: center;
}
.btn-icon:hover { background: var(--border); }

#messageInput {
  flex: 1;
  padding: 10px 14px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  outline: none;
  background: #fff;
}

.btn-send {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--green);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-send:hover { background: #1ebe57; }

.attach-menu {
  position: absolute;
  bottom: 60px;
  left: 16px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  padding: 8px 0;
  z-index: 10;
}
.attach-menu button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 20px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  white-space: nowrap;
}
.attach-menu button:hover { background: var(--bg); }

/* Dialog */
.dialog-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.dialog {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  width: 90%;
  max-width: 400px;
}
.dialog h3 { margin-bottom: 16px; }
.dialog .field { margin-bottom: 12px; }
.dialog .field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; color: #444; }
.dialog .field input { width: 100%; padding: 8px 12px; border: 1.5px solid #ddd; border-radius: 6px; font-size: 14px; outline: none; }
.dialog .field input:focus { border-color: var(--green); }
.dialog-btns { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.btn-cancel { padding: 8px 16px; border: 1px solid #ddd; background: #fff; border-radius: 6px; cursor: pointer; }
.btn-ok { padding: 8px 16px; border: none; background: var(--green); color: #fff; border-radius: 6px; cursor: pointer; font-weight: 600; }

/* Uploading indicator */
.uploading {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: #fff8e1;
  font-size: 13px;
  color: #666;
  border-top: 1px solid var(--border);
}
.spinner {
  width: 16px; height: 16px;
  border: 2px solid #ddd;
  border-top: 2px solid var(--green);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Responsive */
@media (max-width: 768px) {
  #sidebar { width: 100%; position: absolute; z-index: 10; }
  #chatPanel { width: 100%; }
  #app.chat-open #sidebar { display: none; }
  #app.chat-open #chatPanel { display: flex; }
  #app:not(.chat-open) #chatPanel { display: none; }
  #app:not(.chat-open) #sidebar { display: flex; }
  .btn-back { display: block !important; }
  .message-list { padding: 12px 16px; }
  .msg-bubble { max-width: 85%; }
}
