:root {
  --primary: #06C755;
  --primary-dark: #05a847;
  --secondary: #1877F2;
  --secondary-dark: #1260cc;
  --bg: #f7f8fa;
  --surface: #ffffff;
  --card: #ffffff;
  --border: #e4e6ea;
  --border-light: #f0f2f5;
  --text: #1c1e21;
  --text-sub: #65676b;
  --muted: #8a8d91;
  --bubble-in: #ffffff;
  --bubble-out: #06C755;
  --bubble-out-text: #ffffff;
  --bubble-ai: #e8f4ff;
  --unread: #e53935;
  --err: #e53935;
  --warn: #f59e0b;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 20px rgba(0,0,0,0.12);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang TC', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
  height: 100vh;
}

/* ─── Layout ─── */
#app {
  display: flex;
  height: 100vh;
}

/* ─── Sidebar (conversation list) ─── */
#sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

#sidebar-header h1 {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

#sidebar-filters {
  display: flex;
  gap: 4px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-light);
  flex-wrap: wrap;
}

.filter-btn {
  padding: 4px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-sub);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}
.filter-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.filter-btn:hover:not(.active) { background: var(--bg); }

#conv-list {
  flex: 1;
  overflow-y: auto;
}

.conv-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border-light);
  transition: background 0.1s;
  position: relative;
}
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: #f0faf5; }

.conv-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  position: relative;
}

.channel-badge {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  font-size: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--surface);
}
.channel-badge.line { background: var(--primary); color: #fff; }
.channel-badge.fb { background: var(--secondary); color: #fff; }

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

.conv-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
}
.conv-time { font-size: 11px; color: var(--muted); }
.unread-badge {
  background: var(--unread);
  color: #fff;
  border-radius: 10px;
  padding: 1px 6px;
  font-size: 11px;
  font-weight: 600;
  min-width: 18px;
  text-align: center;
}

#conv-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 24px;
  text-align: center;
  color: var(--muted);
  gap: 12px;
}
#conv-empty .empty-icon { font-size: 48px; opacity: 0.5; }
#conv-empty p { font-size: 13px; line-height: 1.6; }

/* ─── Main chat area ─── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

#chat-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 60px;
}

#chat-header-info { display: flex; align-items: center; gap: 12px; }
#chat-header-name { font-weight: 600; font-size: 15px; }
#chat-header-sub { font-size: 12px; color: var(--muted); }
#chat-header-actions { display: flex; gap: 8px; }

#messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#chat-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--muted);
}
#chat-placeholder .placeholder-icon { font-size: 64px; opacity: 0.3; }
#chat-placeholder h2 { font-size: 18px; color: var(--text-sub); font-weight: 600; }
#chat-placeholder p { font-size: 13px; text-align: center; max-width: 300px; line-height: 1.6; }

/* ─── Bubbles ─── */
.bubble-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 75%;
}
.bubble-row.inbound { align-self: flex-start; flex-direction: row; }
.bubble-row.outbound { align-self: flex-end; flex-direction: row-reverse; }

.bubble-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}

.bubble {
  padding: 9px 13px;
  border-radius: 16px;
  max-width: 100%;
  word-break: break-word;
  line-height: 1.5;
  font-size: 14px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
.bubble.inbound {
  background: var(--bubble-in);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  color: var(--text);
}
.bubble.outbound {
  background: var(--bubble-out);
  color: var(--bubble-out-text);
  border-bottom-right-radius: 4px;
}
.bubble.outbound.ai {
  background: var(--bubble-ai);
  color: var(--text);
  border: 1px solid #bee3ff;
}
.bubble-time {
  font-size: 10px;
  color: var(--muted);
  margin-top: 3px;
  text-align: right;
}
.bubble-row.inbound .bubble-time { text-align: left; }

.date-divider {
  align-self: center;
  font-size: 11px;
  color: var(--muted);
  background: var(--border-light);
  padding: 3px 12px;
  border-radius: 12px;
  margin: 8px 0;
}

/* ─── Input area ─── */
#input-area {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#msg-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  min-height: 42px;
  max-height: 120px;
  outline: none;
  transition: border-color 0.15s;
  line-height: 1.5;
}
#msg-input:focus { border-color: var(--primary); }
#msg-input::placeholder { color: var(--muted); }

.btn {
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--bg); border: 1px solid var(--border); color: var(--text-sub); }
.btn-secondary:hover:not(:disabled) { background: var(--border-light); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ─── Right panel ─── */
#right-panel {
  width: 320px;
  min-width: 320px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#right-panel-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}
.rpanel-tab {
  flex: 1;
  padding: 12px 8px;
  text-align: center;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
}
.rpanel-tab.active { color: var(--primary); border-bottom-color: var(--primary); }

#right-panel-body { flex: 1; overflow-y: auto; }

.rpanel-section { padding: 16px; border-bottom: 1px solid var(--border-light); }
.rpanel-section h3 { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 10px; }

.info-row { display: flex; gap: 8px; margin-bottom: 8px; align-items: flex-start; }
.info-label { font-size: 12px; color: var(--muted); width: 48px; flex-shrink: 0; padding-top: 2px; }
.info-value { font-size: 13px; flex: 1; word-break: break-all; }
.info-value.empty { color: var(--muted); font-style: italic; }

.tag-list { display: flex; flex-wrap: wrap; gap: 4px; }
.tag { padding: 2px 8px; border-radius: 12px; background: var(--border-light); font-size: 11px; color: var(--text-sub); border: 1px solid var(--border); }

.template-item {
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin-bottom: 6px;
  cursor: pointer;
  transition: all 0.1s;
}
.template-item:hover { background: var(--bg); border-color: var(--primary); }
.template-shortcut { font-size: 11px; font-weight: 600; color: var(--primary); font-family: monospace; }
.template-title { font-size: 12px; font-weight: 500; margin-top: 2px; }
.template-preview { font-size: 11px; color: var(--text-sub); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.draft-placeholder {
  padding: 20px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  margin: 16px;
}

/* ─── Topbar ─── */
#topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  flex-shrink: 0;
}
#topbar .logo { font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 8px; }
#topbar .logo span { color: var(--primary); }
#topbar-right { display: flex; align-items: center; gap: 12px; }
#user-info { font-size: 12px; color: var(--muted); }
#logout-btn { font-size: 12px; color: var(--text-sub); background: none; border: 1px solid var(--border); padding: 4px 10px; border-radius: var(--radius-sm); cursor: pointer; }
#logout-btn:hover { background: var(--bg); }

/* ─── Status badge ─── */
.status-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
}
.status-open { background: #e8f5e9; color: #2e7d32; }
.status-closed { background: #f5f5f5; color: #757575; }
.status-pending { background: #fff8e1; color: #f57f17; }

/* ─── Misc ─── */
.text-muted { color: var(--muted); }
.text-sm { font-size: 12px; }
.hidden { display: none !important; }

/* ─── Search wrap (topbar) ─── */
#search-wrap {
  position: relative;
}
#search-input {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  font-size: 12px;
  font-family: inherit;
  outline: none;
  width: 180px;
  background: var(--bg);
  transition: border-color 0.15s;
}
#search-input:focus { border-color: var(--primary); background: #fff; }
#search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  width: 300px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  z-index: 200;
  max-height: 320px;
  overflow-y: auto;
}
.search-result-item:hover { background: var(--bg); }

/* ─── Summary bar ─── */
#summary-bar {
  background: #fffde7;
  border-bottom: 1px solid #f9e400;
  padding: 0;
  font-size: 13px;
}
#summary-toggle {
  padding: 8px 16px;
  cursor: pointer;
  font-weight: 500;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 6px;
}
#summary-toggle:hover { background: #fff9c4; }
#summary-content {
  padding: 8px 16px 12px;
  color: var(--text-sub);
  white-space: pre-wrap;
  line-height: 1.6;
}

/* ─── Input mode bar ─── */
#input-mode-bar {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}
.input-mode-btn {
  padding: 4px 12px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-sub);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}
.input-mode-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

/* ─── Internal note bubble ─── */
.note-row {
  align-self: center;
  max-width: 80%;
  margin: 4px auto;
}
.note-bubble {
  background: #fffde7;
  border: 1px solid #f9e400;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 12px;
  color: #5a4e00;
}
.note-label {
  font-size: 10px;
  font-weight: 600;
  color: #b8970a;
  text-transform: uppercase;
  margin-right: 6px;
}

/* ─── Emotion / Intent / Urgency badges ─── */
.emotion-badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
}
.emotion-positive { background: #e8f5e9; color: #2e7d32; }
.emotion-negative { background: #fff3e0; color: #e65100; }
.emotion-angry    { background: #ffebee; color: #c62828; }

.intent-badge {
  display: inline-flex;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 11px;
  background: #e3f2fd;
  color: #1565c0;
}

.urgency-badge {
  background: var(--err);
  color: #fff;
  border-radius: 8px;
  padding: 1px 6px;
  font-size: 10px;
  font-weight: 600;
}

.urgency-badge-header {
  background: var(--err);
  color: #fff;
  border-radius: 10px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 600;
}

/* ─── Conv tag chips ─── */
.conv-tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  margin-top: 3px;
}
.conv-tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 1px 7px;
  border-radius: 10px;
  background: #e3f2fd;
  color: #1565c0;
  font-size: 10px;
  font-weight: 500;
  border: 1px solid #90caf9;
}
.conv-tag-chip.removable { cursor: default; }

/* ─── Draft cards ─── */
.draft-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 10px;
  transition: border-color 0.15s;
}
.draft-card:hover { border-color: var(--primary); }
.draft-variant {
  font-size: 11px;
  font-weight: 600;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}
.draft-content {
  font-size: 13px;
  color: var(--text);
  line-height: 1.5;
  min-height: 40px;
  white-space: pre-wrap;
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 4px 6px;
  outline: none;
  transition: border-color 0.15s;
}
.draft-content:focus { border-color: var(--primary); background: #fafffe; }
.draft-warnings {
  font-size: 11px;
  color: #f57c00;
  margin-top: 4px;
}
.draft-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  flex-wrap: wrap;
}

/* ─── RWD: 手機版三欄 -> 單欄 tab ─── */
@media (max-width: 768px) {
  #app { flex-direction: column; height: 100vh; }

  #sidebar { width: 100%; min-width: unset; height: auto; border-right: none; border-bottom: 1px solid var(--border); }
  #sidebar.hidden { display: none; }

  #main { flex: 1; }
  #main.hidden { display: none; }

  #right-panel { width: 100%; min-width: unset; border-left: none; border-top: 1px solid var(--border); height: auto; }
  #right-panel.hidden { display: none; }

  /* mobile tab bar */
  #mobile-tabs {
    display: flex;
    background: var(--surface);
    border-top: 1px solid var(--border);
    position: fixed;
    bottom: 0;
    width: 100%;
    z-index: 100;
  }
  .mtab { flex: 1; padding: 10px; text-align: center; font-size: 11px; color: var(--muted); cursor: pointer; border-top: 2px solid transparent; }
  .mtab.active { color: var(--primary); border-top-color: var(--primary); }

  body { padding-bottom: 50px; }
}

/* ─── P3 新增 class ─── */

/* 上線狀態圓點 */
.online-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 4px;
}
.online-dot.online { background: #4caf50; }
.online-dot.away   { background: #ff9800; }
.online-dot.busy   { background: #e53935; }
.online-dot.offline { background: #9e9e9e; }

/* 置頂對話 */
.conv-item.pinned { background: #fff9e6; border-left: 3px solid var(--warn); }
.pin-icon { font-size: 11px; color: var(--warn); }

/* 封存對話 */
.conv-item.archived { opacity: 0.6; }

/* 提及鈴鐺 */
.mention-bell {
  position: relative;
  cursor: pointer;
  font-size: 18px;
  padding: 4px;
}
.mention-badge {
  position: absolute;
  top: 0; right: 0;
  background: var(--unread);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  border-radius: 10px;
  padding: 1px 5px;
  line-height: 1.4;
}

/* 快捷鍵說明 modal */
.shortcut-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}
.shortcut-modal-inner {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: 440px;
  max-width: 95vw;
}
.shortcut-modal-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 16px;
}
.shortcut-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.shortcut-table td { padding: 6px 8px; }
.shortcut-table td:first-child { font-family: monospace; background: var(--bg); border-radius: 4px; text-align: center; font-weight: 700; width: 80px; }

/* 排程按鈕 */
.schedule-btn { font-size: 12px; padding: 4px 8px; background: transparent; border: 1px solid var(--border); border-radius: var(--radius-sm); cursor: pointer; color: var(--text-sub); }
.schedule-btn:hover { background: var(--border-light); }

/* 批次操作浮動工具列 */
.bulk-bar {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  border-radius: var(--radius);
  padding: 10px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  z-index: 50;
  box-shadow: var(--shadow-md);
}
.bulk-bar button {
  background: rgba(255,255,255,0.15);
  border: none;
  color: #fff;
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}
.bulk-bar button:hover { background: rgba(255,255,255,0.25); }

/* 狀態選擇 */
.status-select-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-size: 13px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

@media (min-width: 769px) {
  #mobile-tabs { display: none !important; }
}
