:root {
  --ink: #1c2433;
  --ink-soft: #2a3448;
  --indigo: #3d4fa3;
  --indigo-deep: #2f3d86;
  --indigo-soft: #e4e8f6;
  --terracotta: #c46b4a;
  --terracotta-soft: #f3e4db;
  --teal: #2f9e8a;
  --paper: #f3eee4;
  --paper-deep: #e8e0d2;
  --panel: #fffdf8;
  --text: #1c2433;
  --text-secondary: #6b6458;
  --border: #ddd4c6;
  --danger: #c44b3c;
  --header: #1c2433;
  --sidebar: #f7f3eb;
  --unread: #c44b3c;
  --online: #2f9e8a;
  --offline: #b7b1a6;
  --bubble-self: #e7ebf8;
  --bubble-self-edge: #3d4fa3;
  --bubble-other: #fffdf8;
  --bubble-other-edge: #c46b4a;
  --accent: #3d4fa3;
  --accent-dark: #2f3d86;
  --shadow: 0 10px 28px rgba(28, 36, 51, 0.1);
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: "Source Han Sans SC", "Noto Sans SC", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
}

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

html, body {
  height: 100%;
}

body {
  font-family: var(--font);
  color: var(--text);
  background: var(--paper);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea {
  font-family: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
}

a {
  color: var(--accent);
  text-decoration: none;
}

.auth-page {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background:
    radial-gradient(900px 420px at 12% -8%, rgba(61, 79, 163, 0.16), transparent 58%),
    radial-gradient(700px 380px at 100% 110%, rgba(196, 107, 74, 0.12), transparent 55%),
    var(--paper);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 36px 28px 28px;
  border: 1px solid var(--border);
}

.brand {
  text-align: center;
  margin-bottom: 28px;
}

.brand-logo {
  width: 56px;
  height: 56px;
  margin: 0 auto 12px;
  border-radius: 16px;
  background: var(--indigo);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: 700;
}

.brand h1 {
  font-size: 22px;
  font-weight: 650;
}

.brand p {
  margin-top: 6px;
  color: var(--text-secondary);
  font-size: 14px;
}

.tabs {
  display: flex;
  background: #f2f2f2;
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 20px;
}

.tabs button {
  flex: 1;
  padding: 8px 0;
  border-radius: 8px;
  color: #666;
  font-size: 14px;
}

.tabs button.active {
  background: #fff;
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.form-group {
  margin-bottom: 14px;
}

.form-group label {
  display: block;
  font-size: 13px;
  color: #555;
  margin-bottom: 6px;
}

.form-group input {
  width: 100%;
  height: 42px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0 12px;
  font-size: 15px;
  background: #fff;
  outline: none;
}

.form-group input:focus {
  border-color: var(--indigo);
  box-shadow: 0 0 0 3px rgba(61, 79, 163, 0.16);
}

.btn-primary {
  width: 100%;
  height: 44px;
  border-radius: 10px;
  background: var(--indigo);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  margin-top: 8px;
}

.btn-primary:hover {
  background: var(--indigo-deep);
}

.btn-primary:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.error-text {
  min-height: 20px;
  color: var(--danger);
  font-size: 13px;
  margin-top: 8px;
}

.hint {
  margin-top: 16px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 13px;
}

.app-shell {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--paper);
}

.app-header {
  flex: 0 0 auto;
  min-height: 58px;
  height: auto;
  background: var(--header);
  color: #f6f1e8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  position: relative;
  gap: 12px;
  border-bottom: 3px solid var(--indigo);
}

.app-header h1 {
  font-size: 16px;
  font-weight: 650;
  letter-spacing: 0.04em;
  position: static;
  left: auto;
  transform: none;
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  justify-content: flex-start;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.header-mark-logo {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--indigo);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  flex-shrink: 0;
}

.header-side {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  z-index: 1;
  max-width: 40%;
}

.header-side .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0.9;
}

.btn-logout {
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  padding: 4px 10px;
  font-size: 12px;
}

.btn-logout:hover {
  background: rgba(255, 255, 255, 0.2);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--offline);
  display: inline-block;
}

.status-dot.online {
  background: var(--online);
  box-shadow: 0 0 0 3px rgba(47, 158, 138, 0.28);
}

.chat-main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.message-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 12px 8px;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  height: 100%;
  min-height: 180px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  text-align: center;
  padding: 24px;
  font-size: 14px;
}

.empty-state .icon {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  background: var(--indigo-soft);
  color: var(--indigo);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  margin-bottom: 12px;
}

.day-sep {
  text-align: center;
  margin: 10px 0 14px;
}

.day-sep span {
  display: inline-block;
  background: var(--panel);
  color: var(--text-secondary);
  font-size: 11px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.msg-row {
  display: flex;
  margin-bottom: 12px;
  align-items: flex-start;
  gap: 8px;
  -webkit-touch-callout: none;
}

.msg-row.self {
  flex-direction: row-reverse;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  flex: 0 0 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  user-select: none;
  letter-spacing: 0;
}

.avatar.agent {
  background: var(--indigo);
  color: #fff;
}

.avatar.visitor {
  background: var(--terracotta);
  color: #fffdf8;
}

.bubble-wrap {
  max-width: min(76%, 560px);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.msg-row.self .bubble-wrap {
  align-items: flex-end;
}

.bubble-line {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  max-width: 100%;
}

.bubble-line .bubble {
  min-width: 0;
}

.bubble {
  position: relative;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 15px;
  line-height: 1.55;
  word-break: break-word;
  white-space: pre-wrap;
  box-shadow: 0 1px 2px rgba(28, 36, 51, 0.08);
}

.msg-row.other .bubble {
  background: var(--bubble-other);
  border-left: 3px solid var(--bubble-other-edge);
  border-radius: 14px 14px 14px 4px;
  color: var(--ink);
}

.msg-row.self .bubble {
  background: var(--bubble-self);
  border-right: 3px solid var(--bubble-self-edge);
  border-radius: 14px 14px 4px 14px;
  color: var(--ink);
}

.msg-time {
  font-size: 11px;
  color: var(--text-secondary);
}

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px 14px calc(14px + var(--safe-bottom));
  background: var(--paper-deep);
  border-top: 1px solid var(--border);
}

.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  min-height: 44px;
  max-height: 120px;
  padding: 10px 12px;
  font-size: 15px;
  outline: none;
  background: var(--panel);
  line-height: 1.45;
  color: var(--ink);
}

.composer textarea:focus {
  border-color: var(--indigo);
  box-shadow: 0 0 0 3px rgba(61, 79, 163, 0.16);
}

.btn-send {
  height: 44px;
  min-width: 72px;
  padding: 0 16px;
  border-radius: 10px;
  background: var(--indigo);
  color: #fff;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: 0.06em;
}

.btn-send:hover:not(:disabled) {
  background: var(--indigo-deep);
}

.btn-send:disabled {
  background: #c9c3b8;
  color: #f7f3ec;
  cursor: not-allowed;
}

/* Agent layout */
.desk {
  height: 100%;
  display: flex;
  background: var(--panel);
}

.sidebar {
  width: 300px;
  flex: 0 0 300px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--sidebar);
  min-width: 0;
}

.sidebar-header {
  height: 58px;
  background: var(--header);
  color: #f6f1e8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: 0.04em;
  border-bottom: 3px solid var(--indigo);
}

.search-box {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.search-box input {
  width: 100%;
  height: 34px;
  border: none;
  background: var(--paper);
  border-radius: 10px;
  padding: 0 10px;
  font-size: 13px;
  outline: none;
}

.contact-list {
  flex: 1;
  overflow-y: auto;
  background: var(--panel);
}

.contact {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--paper-deep);
}

.contact:hover {
  background: var(--paper);
}

.contact.active {
  background: var(--indigo-soft);
}

.contact-body {
  flex: 1;
  min-width: 0;
}

.contact-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.contact-name {
  font-size: 15px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.contact-time {
  font-size: 11px;
  color: #b2b2b2;
  flex-shrink: 0;
}

.contact-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}

.contact-preview {
  font-size: 12px;
  color: #999;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.badge {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--unread);
  color: #fff;
  font-size: 11px;
  line-height: 18px;
  text-align: center;
  flex-shrink: 0;
}

.contact-avatar-wrap {
  position: relative;
  flex-shrink: 0;
}

.contact-avatar-wrap .status-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 9px;
  height: 9px;
  border: 2px solid var(--panel);
}

.desk-chat {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--paper);
}

.hidden {
  display: none !important;
}

@media (max-width: 760px) {
  .sidebar {
    width: 100%;
    flex-basis: 100%;
  }

  .desk.chat-open .sidebar {
    display: none;
  }

  .desk:not(.chat-open) .desk-chat {
    display: none;
  }

  .back-btn {
    display: inline-flex !important;
  }
}

.back-btn {
  display: none;
  color: #fff;
  font-size: 15px;
  align-items: center;
  gap: 4px;
  z-index: 1;
}

.reconnect-bar {
  display: none;
  text-align: center;
  background: #fff3cd;
  color: #8a6d3b;
  font-size: 12px;
  padding: 4px 8px;
}

.reconnect-bar.show {
  display: block;
}

.btn-icon {
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  width: 28px;
  height: 28px;
  font-size: 14px;
  line-height: 28px;
  padding: 0;
}

.btn-icon:hover {
  background: rgba(255, 255, 255, 0.22);
}

.btn-icon.muted {
  opacity: 0.65;
}

.btn-icon.light {
  background: rgba(255, 255, 255, 0.12);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.msg-meta {
  margin-top: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-secondary);
}

.msg-row.self .msg-meta {
  justify-content: flex-end;
}

.msg-receipt {
  flex: 0 0 auto;
  font-size: 12px;
  line-height: 1.2;
  color: #7a7a7a;
  font-weight: 600;
  padding-bottom: 2px;
  user-select: none;
  white-space: nowrap;
  letter-spacing: 0.02em;
}

.msg-receipt.is-unread {
  color: #8a8a8a;
}

.msg-receipt.is-read {
  color: var(--indigo);
}

.msg-recall-btn {
  border: none;
  background: none;
  color: var(--indigo);
  font-size: 11px;
  padding: 0;
  cursor: pointer;
  line-height: 1;
}

.msg-recall-btn:hover {
  text-decoration: underline;
}

.msg-system {
  text-align: center;
  margin: 8px 0 14px;
}

.msg-system span {
  display: inline-block;
  background: var(--paper-deep);
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 999px;
}

.msg-action-menu {
  position: fixed;
  z-index: 80;
  background: rgba(47, 47, 47, 0.96);
  color: #fff;
  border-radius: 6px;
  padding: 2px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.22);
}

.msg-action-menu button {
  display: block;
  min-width: 76px;
  border: none;
  background: none;
  color: #fff;
  font-size: 14px;
  padding: 8px 16px;
  cursor: pointer;
}

.msg-action-menu button:hover {
  background: rgba(255, 255, 255, 0.12);
  border-radius: 4px;
}

.contact {
  position: relative;
}

.drag-handle {
  flex: 0 0 14px;
  color: #c0c0c0;
  font-size: 12px;
  letter-spacing: -2px;
  cursor: grab;
  user-select: none;
  line-height: 1;
}

.contact.dragging {
  opacity: 0.5;
}

.contact.drag-over {
  box-shadow: inset 0 2px 0 var(--indigo);
}

.contact.blocked {
  opacity: 0.78;
  background: var(--paper);
}

.contact-name-wrap {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
  flex: 1;
  flex-wrap: wrap;
}

.contact-username {
  font-size: 11px;
  color: #b0b0b0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mark-tag,
.header-mark,
.dnd-tag,
.blocked-tag {
  display: inline-block;
  font-size: 10px;
  line-height: 16px;
  padding: 0 5px;
  border-radius: 8px;
  flex-shrink: 0;
  font-weight: 600;
}

.mark-tag.mark-star,
.header-mark.mark-star {
  background: #fff4d1;
  color: #c48a00;
}

.mark-tag.mark-important,
.header-mark.mark-important {
  background: #e8f0fe;
  color: #2b6cb0;
}

.mark-tag.mark-urgent,
.header-mark.mark-urgent {
  background: #ffe8e8;
  color: #d43838;
}

.dnd-tag {
  background: #eee;
  color: #777;
}

.blocked-tag {
  background: #333;
  color: #fff;
}

.contact-group {
  padding: 8px 12px 4px;
  font-size: 12px;
  color: #999;
  background: #fafafa;
  border-top: 1px solid #f0f0f0;
}

@keyframes badge-blink {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.9); }
}

.badge.blink {
  background: var(--unread);
  animation: badge-blink 1s ease-in-out infinite;
}

.badge.dnd {
  background: #9e9e9e;
  animation: none;
}

.chat-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px;
  background: var(--paper-deep);
  border-bottom: 1px solid var(--border);
}

.chat-toolbar button {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--ink);
}

.chat-toolbar button:hover {
  background: var(--paper);
}

.chat-toolbar button.active {
  background: var(--indigo-soft);
  border-color: #b7c0e6;
  color: var(--indigo-deep);
}

.chat-toolbar button.danger {
  color: #c0392b;
}

.chat-toolbar button.danger-active {
  background: #333;
  border-color: #333;
  color: #fff;
}

.menu-wrap {
  position: relative;
}

.menu {
  position: absolute;
  top: 110%;
  left: 0;
  background: #fff;
  border: 1px solid #e6e6e6;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  min-width: 120px;
  z-index: 20;
  padding: 4px;
}

.menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  border-radius: 6px;
  padding: 6px 8px;
}

.menu button:hover {
  background: #f5f5f5;
}

.title-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  line-height: 1.2;
}

.chat-sub {
  font-size: 11px;
  font-weight: 400;
  opacity: 0.7;
}

.app-header h1 {
  max-width: none;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}

.modal-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border-radius: 14px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.modal-card h3 {
  font-size: 16px;
  margin-bottom: 6px;
}

.modal-hint {
  font-size: 12px;
  color: #888;
  margin-bottom: 12px;
}

.modal-card input {
  width: 100%;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 10px;
  font-size: 15px;
  outline: none;
}

.modal-card input:focus {
  border-color: var(--indigo);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 14px;
}

.modal-actions button {
  padding: 6px 12px;
  border-radius: 8px;
}

body.is-popout .sidebar {
  display: none !important;
}

body.is-popout .desk {
  display: block;
}

body.is-popout .desk-chat {
  height: 100%;
  display: flex !important;
  flex-direction: column;
}

body.is-popout .back-btn {
  display: none !important;
}


.chat-ip,
.contact-ip {
  font-size: 11px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--indigo-deep);
  letter-spacing: 0;
  font-weight: 600;
}

.chat-ip {
  display: block;
  margin-top: 2px;
  opacity: 0.95;
}

.contact-ip {
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.draft-panel {
  flex: 0 0 auto;
  margin: 0 14px 10px;
  padding: 8px 12px 10px;
  background: var(--panel);
  border: 1px dashed var(--indigo);
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(28, 36, 51, 0.06);
}

.draft-panel-label {
  font-size: 11px;
  font-weight: 650;
  color: var(--indigo);
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}

.draft-panel-text {
  font-size: 14px;
  color: var(--ink);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 88px;
  overflow-y: auto;
  line-height: 1.45;
}
