:root {
  --bg: #fafbfc;
  --card: #ffffff;
  --border: #e5e7eb;
  --border-soft: #f0f1f4;
  --text: #111827;
  --text-soft: #6b7280;
  --text-faint: #9ca3af;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --primary-active: #dbeafe;
  --success: #10b981;
  --danger: #ef4444;
  --bubble-customer: #ffffff;
  --bubble-page: #2563eb;
  --shadow-xs: 0 1px 2px rgba(15, 23, 42, .04);
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, .06), 0 1px 2px rgba(15, 23, 42, .04);
  --shadow: 0 4px 6px -1px rgba(15, 23, 42, .06), 0 2px 4px -2px rgba(15, 23, 42, .04);
  --shadow-lg: 0 10px 15px -3px rgba(15, 23, 42, .08), 0 4px 6px -4px rgba(15, 23, 42, .06);
  --radius: 10px;
  --radius-sm: 6px;
  --radius-lg: 14px;
  --radius-bubble: 18px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", "PingFang SC",
    "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
button { cursor: pointer; font-family: inherit; font-size: 14px; }
input, textarea, button { font-family: inherit; }
.hidden { display: none !important; }
.muted { color: var(--text-soft); font-size: 13px; }
.error {
  background: #fef2f2;
  color: #b91c1c;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  font-size: 13px;
  border: 1px solid #fecaca;
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #9ca3af; }
::-webkit-scrollbar-track { background: transparent; }

/* ========== Login ========== */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #e3e9f0 100%);
  padding: 20px;
}
.login-card {
  background: var(--card);
  border-radius: var(--radius-lg);
  padding: 36px 32px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border-soft);
}
.login-card h1 {
  margin: 0 0 6px 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -.01em;
}
.login-card p { margin: 0 0 24px 0; color: var(--text-soft); font-size: 13px; }
.login-card label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-soft);
  margin-top: 14px;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: .03em;
}
.login-card input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--card);
  color: var(--text);
  transition: border .15s ease, box-shadow .15s ease;
}
.login-card input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.login-card button {
  width: 100%;
  padding: 12px;
  background: var(--primary);
  color: #fff;
  border: 0;
  border-radius: var(--radius-sm);
  margin-top: 22px;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .01em;
  transition: background .15s ease, transform .05s ease;
}
.login-card button:hover { background: var(--primary-hover); }
.login-card button:active { transform: translateY(1px); }

/* ========== App layout ========== */
.app { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border-soft);
  padding: 0 20px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.brand {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: -.005em;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 2px rgba(16, 185, 129, .15);
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.user-tag {
  font-size: 13px;
  color: var(--text-soft);
  padding: 4px 10px;
  background: var(--bg);
  border-radius: 999px;
}
.btn-link {
  color: var(--text-soft);
  text-decoration: none;
  font-size: 13px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  transition: background .15s, color .15s;
}
.btn-link:hover { background: var(--bg); color: var(--text); }
.btn-secondary {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  transition: background .15s, border .15s;
}
.btn-secondary:hover { background: #f3f4f6; border-color: #d1d5db; }
.btn-secondary:disabled { opacity: .5; cursor: not-allowed; }
.btn-icon {
  background: transparent;
  border: 0;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-soft);
  transition: background .15s, color .15s;
  flex-shrink: 0;
}
.btn-icon:hover { background: var(--bg); color: var(--text); }
.btn-icon svg { width: 20px; height: 20px; }

.layout { display: flex; flex: 1; min-height: 0; }

/* ========== Sidebar ========== */
.sidebar {
  width: 320px;
  background: var(--card);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.search {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-soft);
  position: relative;
}
.search input {
  width: 100%;
  padding: 8px 14px 8px 34px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
  background: var(--bg);
  transition: border .15s, background .15s, box-shadow .15s;
}
.search input:focus {
  outline: none;
  background: var(--card);
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.search-icon {
  position: absolute;
  left: 24px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-faint);
  pointer-events: none;
  display: flex;
}
.conv-list { flex: 1; overflow-y: auto; padding: 4px 0; }
.conv-item {
  padding: 10px 14px;
  display: flex;
  gap: 11px;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background .12s ease;
  margin: 1px 0;
  align-items: center;
}
.conv-item:hover { background: var(--bg); }
.conv-item.active {
  background: var(--primary-soft);
  border-left-color: var(--primary);
}
.conv-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
  object-fit: cover;
}
.conv-avatar.initials, .chat-avatar.initials {
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.conv-meta { flex: 1; min-width: 0; }
.conv-name {
  font-weight: 500;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--text);
}
.conv-name > span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-time {
  font-size: 11px;
  color: var(--text-faint);
  font-weight: 400;
  flex-shrink: 0;
  margin-left: 8px;
}
.conv-snippet {
  font-size: 13px;
  color: var(--text-soft);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 3px;
  line-height: 1.35;
}
.conv-side { flex-shrink: 0; }
.conv-unread {
  background: var(--primary);
  color: #fff;
  border-radius: 999px;
  padding: 1px 7px;
  font-size: 10px;
  font-weight: 600;
  min-width: 18px;
  text-align: center;
  line-height: 16px;
  display: inline-block;
}
.empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
  line-height: 1.6;
}

/* ========== Chat pane ========== */
.chat-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
  position: relative;
}
.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  font-size: 13px;
  flex-direction: column;
  gap: 8px;
}
.chat-empty svg { width: 48px; height: 48px; opacity: .5; }
.chat-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-header {
  background: var(--card);
  border-bottom: 1px solid var(--border-soft);
  padding: 12px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}
.chat-header img.chat-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--border); object-fit: cover; }
.chat-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--border); flex-shrink: 0; }
.chat-avatar.initials { font-size: 13px; }
.chat-avatar-wrap { display: inline-flex; }
.chat-header-info { flex: 1; min-width: 0; }
#chat-name { font-weight: 600; font-size: 14px; color: var(--text); letter-spacing: -.005em; }
#chat-status { font-size: 12px; color: var(--text-soft); margin-top: 2px; }
.chat-header-actions { display: flex; gap: 4px; }

/* Messages area */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.msg-time-divider {
  text-align: center;
  font-size: 11px;
  color: var(--text-faint);
  margin: 18px 0 10px;
  font-weight: 500;
  letter-spacing: .02em;
}

.msg-row {
  display: flex;
  flex-direction: column;
  margin: 1px 0;
  max-width: 70%;
}
.msg-row.from-customer { align-self: flex-start; }
.msg-row.from-page { align-self: flex-end; align-items: flex-end; }
.msg-sender {
  font-size: 11px;
  color: var(--text-soft);
  margin: 6px 14px 3px;
  font-weight: 500;
}

/* Bubble */
.msg {
  position: relative;
  padding: 8px 60px 8px 14px;
  border-radius: var(--radius-bubble);
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: var(--shadow-xs);
  width: fit-content;
  max-width: 100%;
}
.msg.from-customer {
  background: var(--bubble-customer);
  color: var(--text);
  border: 1px solid var(--border-soft);
  border-bottom-left-radius: 6px;
}
.msg.from-page {
  background: var(--bubble-page);
  color: #fff;
  border-bottom-right-radius: 6px;
}
.msg-text { white-space: pre-wrap; word-break: break-word; }

/* Time inside bubble (iMessage style) */
.msg-time {
  position: absolute;
  bottom: 5px;
  right: 10px;
  font-size: 10px;
  font-weight: 400;
  letter-spacing: .02em;
  pointer-events: none;
  user-select: none;
}
.msg.from-customer .msg-time { color: var(--text-faint); }
.msg.from-page .msg-time { color: rgba(255, 255, 255, .65); }

/* Image-only bubble: no padding, time overlays bottom-right */
.msg.msg-image-only {
  padding: 0;
  background: transparent;
  box-shadow: none;
  border: 0;
  border-radius: 14px;
  overflow: hidden;
}
.msg.msg-image-only .msg-time {
  background: rgba(0, 0, 0, .5);
  color: #fff;
  padding: 2px 6px;
  border-radius: 8px;
  bottom: 6px;
  right: 6px;
}
.msg.msg-image-only .msg-attachments { margin: 0; }
.msg.msg-image-only .att-image { display: block; max-width: 320px; max-height: 400px; border-radius: 14px; }

/* Attachments inside message */
.msg-attachments { margin-top: 6px; display: flex; flex-direction: column; gap: 6px; }
.msg-attachments:first-child { margin-top: 0; }
.att-image {
  max-width: 280px;
  max-height: 360px;
  border-radius: 12px;
  display: block;
  cursor: zoom-in;
  background: rgba(0, 0, 0, .03);
  transition: transform .15s ease, box-shadow .15s ease;
}
.att-image:hover { transform: translateY(-1px); box-shadow: var(--shadow); }
.att-video, .att-audio { max-width: 280px; border-radius: 12px; display: block; }
.att-audio { width: 280px; }
.att-link { color: inherit; text-decoration: underline; }
.att-file {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: rgba(0, 0, 0, .04);
  border-radius: 10px;
  text-decoration: none;
  color: inherit;
  min-width: 220px;
  max-width: 320px;
  transition: background .15s;
}
.msg.from-page .att-file { background: rgba(255, 255, 255, .15); color: #fff; }
.msg.from-page .att-file:hover { background: rgba(255, 255, 255, .22); }
.msg.from-customer .att-file:hover { background: rgba(0, 0, 0, .07); }
.att-icon { width: 36px; height: 36px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
.att-icon svg { width: 28px; height: 28px; opacity: .8; }
.att-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.att-name { font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-meta { font-size: 11px; opacity: .7; }

/* ========== Composer ========== */
.composer {
  background: var(--card);
  border-top: 1px solid var(--border-soft);
  padding: 12px 16px 14px;
  flex-shrink: 0;
  position: relative;
}
.composer-box {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  display: flex;
  flex-direction: column;
  transition: border .15s ease, box-shadow .15s ease;
  overflow: hidden;
}
.composer-box:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.composer textarea {
  width: 100%;
  border: 0;
  padding: 12px 14px 4px;
  font-size: 14px;
  resize: none;
  outline: none;
  font-family: inherit;
  background: transparent;
  color: var(--text);
  line-height: 1.5;
  max-height: 200px;
  min-height: 40px;
}
.composer textarea::placeholder { color: var(--text-faint); }
.composer-toolbar {
  display: flex;
  align-items: center;
  padding: 4px 6px 6px 4px;
  gap: 4px;
}
.composer-attach-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 10px 4px;
  border-top: 1px dashed var(--border-soft);
}
.composer-attach-list:empty { display: none; }
.composer-attach-item {
  position: relative;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  display: flex;
  align-items: center;
}
.composer-attach-item.is-image { width: 64px; height: 64px; overflow: hidden; padding: 0; }
.composer-attach-item.is-image img { width: 100%; height: 100%; object-fit: cover; display: block; }
.composer-attach-item.is-file {
  padding: 8px 28px 8px 8px;
  gap: 8px;
  max-width: 240px;
  height: 56px;
}
.att-thumb-icon {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  background: var(--card);
  border-radius: 6px;
  color: var(--text-soft);
  flex-shrink: 0;
}
.att-thumb-icon svg { width: 20px; height: 20px; }
.att-thumb-info { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.att-thumb-name { font-size: 12px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 130px; }
.att-thumb-meta { font-size: 10px; color: var(--text-soft); }
.composer-attach-item button {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(0, 0, 0, .55);
  color: #fff;
  border: 0;
  font-size: 12px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.composer-attach-item button:hover { background: rgba(0, 0, 0, .8); }
.composer-spacer { flex: 1; }
.composer-status {
  font-size: 11px;
  color: var(--text-soft);
  padding: 0 8px;
}
.composer-send {
  background: var(--primary);
  color: #fff;
  border: 0;
  padding: 7px 18px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .005em;
  transition: background .15s ease, transform .05s ease, opacity .15s ease;
}
.composer-send:hover:not(:disabled) { background: var(--primary-hover); }
.composer-send:active:not(:disabled) { transform: translateY(1px); }
.composer-send:disabled {
  background: var(--border);
  color: var(--text-faint);
  cursor: not-allowed;
}
.composer-hint {
  font-size: 11px;
  color: var(--text-faint);
  padding: 6px 4px 0;
  text-align: right;
}

/* ========== Drag overlay (whole chat pane) ========== */
.chat-drop-overlay {
  position: absolute;
  inset: 12px;
  background: rgba(37, 99, 235, .08);
  border: 2px dashed var(--primary);
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity .15s;
  z-index: 5;
  backdrop-filter: blur(2px);
}
.chat-pane.dragging .chat-drop-overlay { opacity: 1; }
.chat-drop-inner {
  background: var(--card);
  padding: 28px 36px;
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--primary);
  font-weight: 600;
  font-size: 14px;
}
.chat-drop-inner svg { width: 40px; height: 40px; }

/* Image lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .85);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  padding: 24px;
  animation: lb-fade .15s ease-out;
}
@keyframes lb-fade { from { opacity: 0; } to { opacity: 1; } }
.lightbox img {
  max-width: 95vw;
  max-height: 95vh;
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
}

@media (max-width: 720px) {
  .sidebar { width: 100%; }
  .layout.has-active .sidebar { display: none; }
  .layout:not(.has-active) .chat-pane { display: none; }
  .msg-row { max-width: 85%; }
}
