:root {
  --green: #00a884;
  --green-dark: #008069;
  --panel-header: #f0f2f5;
  --panel-bg: #ffffff;
  --chat-bg: #efeae2;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --text: #111b21;
  --text-secondary: #667781;
  --border: #e9edef;
  --hover: #f5f6f6;
  --active: #f0f2f5;
  --unread: #25d366;
  --shadow: 0 1px 0.5px rgba(11,20,26,.13);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: #d1d7db;
  color: var(--text);
  overflow: hidden;
}
.hidden { display: none !important; }
button { cursor: pointer; font-family: inherit; }

/* ---------------- Login ---------------- */
.login-screen {
  position: fixed; inset: 0;
  background: linear-gradient(135deg, #00a884 0%, #008069 100%);
  display: flex; align-items: center; justify-content: center;
}
.login-card {
  background: #fff; border-radius: 14px; padding: 40px 36px;
  width: 360px; max-width: 92vw; text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
.login-logo { margin-bottom: 12px; }
.login-card h1 { font-size: 24px; color: var(--green-dark); }
.login-sub { color: var(--text-secondary); font-size: 14px; margin: 6px 0 24px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.login-card input {
  padding: 12px 14px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 15px; outline: none;
}
.login-card input:focus { border-color: var(--green); }
.login-card button {
  background: var(--green); color: #fff; border: none; padding: 12px;
  border-radius: 8px; font-size: 16px; font-weight: 600; margin-top: 4px;
}
.login-card button:hover { background: var(--green-dark); }
.login-error { color: #e02e2e; font-size: 13px; min-height: 16px; }

/* ---------------- App layout ---------------- */
.app { display: flex; height: 100vh; max-width: 1600px; margin: 0 auto; background: var(--panel-bg); }
.sidebar {
  width: 30%; min-width: 340px; max-width: 460px;
  display: flex; flex-direction: column; border-right: 1px solid var(--border);
}
.sidebar-header {
  background: var(--panel-header); height: 60px; display: flex; align-items: center;
  padding: 0 16px; gap: 12px;
}
.sidebar-title { flex: 1; display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.sidebar-title span { font-weight: 600; font-size: 15px; }
.conn-status { font-size: 11px; color: var(--text-secondary); }
.conn-status.online { color: var(--green); }
.sidebar-actions { display: flex; gap: 4px; }

.icon-btn {
  background: none; border: none; font-size: 18px; color: #54656f;
  width: 38px; height: 38px; border-radius: 50%; display: flex;
  align-items: center; justify-content: center;
}
.icon-btn:hover { background: rgba(0,0,0,.05); }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--green);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 16px; flex-shrink: 0; overflow: hidden; text-transform: uppercase;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.me { background: #6a7175; }

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

.chat-list { flex: 1; overflow-y: auto; }
.chat-item {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px;
  cursor: pointer; border-bottom: 1px solid var(--border);
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); }
.chat-item-body { flex: 1; min-width: 0; }
.chat-item-top { display: flex; justify-content: space-between; align-items: baseline; }
.chat-item-name { font-size: 16px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-time { font-size: 12px; color: var(--text-secondary); flex-shrink: 0; margin-left: 8px; }
.chat-item-bottom { display: flex; justify-content: space-between; align-items: center; margin-top: 2px; }
.chat-item-preview { font-size: 14px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.chat-item-badge {
  background: var(--unread); color: #fff; font-size: 12px; font-weight: 600;
  min-width: 20px; height: 20px; border-radius: 10px; display: flex;
  align-items: center; justify-content: center; padding: 0 6px; margin-left: 6px;
}
.pin-dot { color: var(--text-secondary); font-size: 12px; margin-left: 4px; }

/* ---------------- Conversation ---------------- */
.conversation { flex: 1; display: flex; flex-direction: column; position: relative; }
.empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--panel-header); text-align: center; color: var(--text-secondary); padding: 40px;
  border-bottom: 6px solid var(--green);
}
.empty-state h2 { font-size: 28px; font-weight: 300; color: #41525d; margin: 16px 0 8px; }
.empty-state p { max-width: 420px; font-size: 14px; }

.chat-view { flex: 1; display: flex; flex-direction: column; height: 100%; }
.chat-header {
  background: var(--panel-header); height: 60px; display: flex; align-items: center;
  padding: 0 16px; gap: 12px; border-left: 1px solid var(--border);
}
.chat-header-info { flex: 1; display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.chat-header-info span { font-weight: 600; font-size: 16px; }
.chat-header-info small { font-size: 12px; color: var(--text-secondary); }
.back-btn { display: none; font-size: 26px; }
.ai-toggle { display: flex; align-items: center; gap: 4px; font-size: 13px; color: #54656f; user-select: none; }
.ai-toggle input { accent-color: var(--green); width: 16px; height: 16px; }

.messages {
  flex: 1; overflow-y: auto; padding: 16px 6%;
  background-color: var(--chat-bg);
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23000000' fill-opacity='0.03'%3E%3Cpath d='M30 30m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0'/%3E%3C/g%3E%3C/svg%3E");
  display: flex; flex-direction: column; gap: 2px;
}
.msg-row { display: flex; margin: 1px 0; }
.msg-row.out { justify-content: flex-end; }
.bubble {
  max-width: 65%; padding: 6px 9px 8px; border-radius: 7.5px; position: relative;
  box-shadow: var(--shadow); font-size: 14.2px; line-height: 19px; word-wrap: break-word; white-space: pre-wrap;
}
.msg-row.in .bubble { background: var(--bubble-in); border-top-left-radius: 0; }
.msg-row.out .bubble { background: var(--bubble-out); border-top-right-radius: 0; }
.bubble .sender { font-size: 12.5px; font-weight: 600; color: var(--green-dark); margin-bottom: 2px; }
.bubble .meta { font-size: 11px; color: var(--text-secondary); float: right; margin: 6px 0 -4px 8px; display: flex; align-items: center; gap: 3px; }
.tick { color: var(--text-secondary); font-size: 12px; }
.tick.read { color: #53bdeb; }
.bubble img.media, .bubble video.media { max-width: 100%; border-radius: 6px; display: block; margin-bottom: 4px; }
.bubble a { color: #027eb5; }
.bubble .doc {
  display: flex; align-items: center; gap: 8px; background: rgba(0,0,0,.04);
  padding: 8px; border-radius: 6px;
}
.day-divider { align-self: center; background: #fff; color: var(--text-secondary);
  font-size: 12px; padding: 5px 12px; border-radius: 8px; margin: 12px 0; box-shadow: var(--shadow); }

.composer {
  background: var(--panel-header); padding: 8px 16px; display: flex; align-items: flex-end; gap: 8px;
}
.composer textarea {
  flex: 1; border: none; border-radius: 8px; padding: 10px 14px; font-size: 15px;
  resize: none; outline: none; max-height: 120px; font-family: inherit; line-height: 20px;
}
.send-btn {
  background: var(--green); color: #fff; border: none; width: 44px; height: 44px;
  border-radius: 50%; font-size: 18px; flex-shrink: 0;
}
.send-btn:hover { background: var(--green-dark); }
.attach-btn { font-size: 20px; }

/* ---------------- Modal / toast ---------------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal-card { background: #fff; border-radius: 12px; padding: 24px; width: 380px; max-width: 92vw; display: flex; flex-direction: column; gap: 12px; }
.modal-card h3 { font-size: 18px; }
.modal-card input, .modal-card textarea { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; outline: none; font-family: inherit; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.btn-primary { background: var(--green); color: #fff; border: none; padding: 10px 18px; border-radius: 8px; font-weight: 600; }
.btn-secondary { background: var(--panel-header); border: none; padding: 10px 18px; border-radius: 8px; }
.modal-error { color: #e02e2e; font-size: 13px; min-height: 16px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #333; color: #fff; padding: 10px 20px; border-radius: 8px; font-size: 14px;
  z-index: 200; box-shadow: 0 4px 12px rgba(0,0,0,.3);
}

/* ---------------- Reply / quoted ---------------- */
.chat-view { position: relative; }
.bubble { padding-right: 26px; }
.reply-btn {
  position: absolute; top: 2px; right: 2px; border: none; background: transparent;
  color: var(--text-secondary); font-size: 13px; cursor: pointer; opacity: 0;
  transition: opacity .12s; padding: 2px 5px; border-radius: 4px;
}
.msg-row:hover .reply-btn { opacity: .8; }
.reply-btn:hover { opacity: 1 !important; background: rgba(0,0,0,.06); }
.msg-row { transition: transform .12s ease-out; touch-action: pan-y; }

.quoted {
  display: flex; flex-direction: column; gap: 1px; border-left: 3px solid var(--green);
  background: rgba(0,0,0,.04); border-radius: 5px; padding: 4px 8px; margin-bottom: 4px;
  font-size: 13px; max-width: 100%; overflow: hidden;
}
.msg-row.out .quoted { border-left-color: #06cf9c; background: rgba(0,0,0,.05); }
.quoted-who { color: var(--green-dark); font-weight: 600; font-size: 12.5px; }
.quoted-text { color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.reply-bar { background: var(--panel-header); padding: 0 16px; }
.reply-bar-inner {
  display: flex; align-items: center; gap: 8px; background: rgba(0,0,0,.04);
  border-left: 4px solid var(--green); border-radius: 6px; padding: 6px 10px; margin: 8px 0 0;
}
.reply-bar-text { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.reply-bar-name { color: var(--green-dark); font-weight: 600; font-size: 13px; }
.reply-bar-preview { color: var(--text-secondary); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------------- Slash templates ---------------- */
.slash-menu {
  position: absolute; left: 16px; right: 16px; bottom: 64px; max-height: 260px; overflow-y: auto;
  background: #fff; border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,.18); z-index: 50;
}
.slash-item { display: flex; align-items: baseline; gap: 10px; padding: 9px 14px; cursor: pointer; border-bottom: 1px solid var(--border); }
.slash-item:last-child { border-bottom: none; }
.slash-item.active, .slash-item:hover { background: var(--active); }
.slash-slug { color: var(--green-dark); font-weight: 700; font-size: 13.5px; flex-shrink: 0; }
.slash-title { font-weight: 600; font-size: 13.5px; flex-shrink: 0; }
.slash-body { color: var(--text-secondary); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------------- Responsive (mobile) ---------------- */
@media (max-width: 768px) {
  .sidebar { width: 100%; min-width: 0; max-width: none; }
  .conversation { position: absolute; inset: 0; z-index: 10; transform: translateX(100%); transition: transform .2s; }
  .app.show-chat .conversation { transform: translateX(0); }
  .app.show-chat .sidebar { display: none; }
  .back-btn { display: flex !important; }
  .bubble { max-width: 80%; }
  .messages { padding: 12px 4%; }
}
