/* 啟源資本 客服面板 — Telegram-inspired light theme + gold brand accents */
:root {
  /* Brand */
  --gold:        #C4872A;
  --gold-soft:   #FBF6EC;
  --gold-pale:   #F5E6C8;

  /* Telegram-style neutrals (light) */
  --bg:          #FFFFFF;
  --bg-soft:     #F4F4F5;        /* chat area background */
  --bg-tint:     #FAFAFC;        /* secondary tint */
  --line:        #E6E7EB;
  --divider:     #EDEFF2;

  /* Text */
  --ink:         #0F0F10;
  --ink-2:       #6B7280;
  --ink-3:       #9CA3AF;

  /* Bubbles (Telegram outgoing-style for admin, incoming for user) */
  --bubble-in:    #FFFFFF;       /* user/incoming, white with subtle border */
  --bubble-in-tx: #0F0F10;
  --bubble-out:   #FBF1DD;       /* admin/outgoing, soft gold tint */
  --bubble-out-tx:#3A2510;
  --tick:         #6FAF50;       /* Telegram-style read tick */

  /* Status */
  --red:          #E04F4F;
  --green:        #2D7A6A;

  /* Avatar gradient ramps (Telegram uses 7) */
  --av1: linear-gradient(135deg, #E17076, #FF885E);
  --av2: linear-gradient(135deg, #FAA774, #F18F3F);
  --av3: linear-gradient(135deg, #FFD487, #E6AC23);
  --av4: linear-gradient(135deg, #82CB94, #4DA858);
  --av5: linear-gradient(135deg, #72D5FD, #2A9EF1);
  --av6: linear-gradient(135deg, #82A3F0, #6064DC);
  --av7: linear-gradient(135deg, #E08FE6, #C254D8);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); }
body {
  font-family: -apple-system, "PingFang TC", "Noto Sans TC", "Inter", "Microsoft JhengHei", sans-serif;
  color: var(--ink);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  /* Prevent iOS rubber-band scrolling on body */
  overscroll-behavior-y: none;
}
button, input, textarea, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  outline: none;
}
button { background: transparent; border: none; cursor: pointer; }

/* ── Layout ────────────────────────────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-columns: 340px 1fr;
  /* iOS Telegram WebApp: 100vh includes hidden chrome — use dvh + TG var */
  height: 100vh;
  height: 100dvh;
  height: var(--tg-viewport-stable-height, 100dvh);
  background: var(--bg-soft);
}
@media (max-width: 768px) {
  #app { grid-template-columns: 1fr; }
  #app.detail #user-list { display: none; }
  #app:not(.detail) #conversation { display: none; }
}

/* ── Left: chat list (Telegram-style) ──────────────────────────────────────── */
#user-list {
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.list-head {
  padding: 10px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--divider);
}
.list-head input[type="search"] {
  width: 100%;
  padding: 9px 14px;
  border: none;
  border-radius: 20px;
  background: var(--bg-soft);
  font-size: 14px;
}
.list-head input[type="search"]::placeholder { color: var(--ink-3); }
.filter-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.filter-row label { display: flex; align-items: center; gap: 5px; cursor: pointer; }
.filter-row button {
  font-size: 16px;
  color: var(--ink-2);
  padding: 4px 8px;
  border-radius: 14px;
}
.filter-row button:hover { background: var(--bg-soft); color: var(--gold); }

#users {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  background: var(--bg);
}
#users li {
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  gap: 12px;
  align-items: center;
  transition: background 0.1s;
}
#users li:hover { background: var(--bg-soft); }
#users li.active { background: var(--gold-soft); }
#users li.active .preview { color: var(--ink-2); }

#users .avatar {
  width: 48px; height: 48px;
  border-radius: 50%;
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 600;
  font-size: 18px;
  flex-shrink: 0;
  letter-spacing: 0;
}
#users .avatar.c1 { background: var(--av1); }
#users .avatar.c2 { background: var(--av2); }
#users .avatar.c3 { background: var(--av3); }
#users .avatar.c4 { background: var(--av4); }
#users .avatar.c5 { background: var(--av5); }
#users .avatar.c6 { background: var(--av6); }
#users .avatar.c7 { background: var(--av7); }

#users .meta { flex: 1; min-width: 0; }
#users .top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 2px;
}
#users .name {
  font-weight: 600;
  color: var(--ink);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#users .when { font-size: 11.5px; color: var(--ink-3); flex-shrink: 0; margin-left: 6px; }
#users .preview-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#users .preview {
  font-size: 13px;
  color: var(--ink-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}
#users .badge {
  background: var(--gold);
  color: white;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  padding: 1px 7px;
  margin-left: 6px;
  min-width: 18px;
  text-align: center;
}
#users .sla-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
  margin-left: 6px;
  flex-shrink: 0;
}

.list-foot {
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--divider);
  background: var(--bg-tint);
  font-size: 12px;
  color: var(--ink-3);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.list-foot .admin-pill {
  background: var(--gold-soft);
  color: var(--gold);
  padding: 2px 10px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

/* ── Right: conversation ───────────────────────────────────────────────────── */
#conversation {
  display: flex;
  flex-direction: column;
  background: var(--bg-soft);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

.conv-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 11px 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--divider);
  z-index: 1;
}
.conv-head .user-meta { display: flex; align-items: center; gap: 12px; min-width: 0; }
.conv-head .avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 600;
  font-size: 16px;
  flex-shrink: 0;
}
.conv-head .user-text { min-width: 0; }
.conv-head h2 {
  margin: 0;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-head .user-sub {
  font-size: 12px;
  color: var(--ink-3);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.back-btn {
  border: none;
  font-size: 22px;
  color: var(--ink-2);
  padding: 4px 10px;
  margin-right: 4px;
  border-radius: 14px;
  display: none;
}
.back-btn:hover { background: var(--bg-soft); color: var(--gold); }
@media (max-width: 768px) {
  .back-btn { display: inline-flex; align-items: center; justify-content: center; }
}

.conv-head .actions { display: flex; gap: 4px; flex-shrink: 0; }
.conv-head .actions button {
  border: none;
  border-radius: 18px;
  padding: 7px 12px;
  font-size: 13px;
  color: var(--ink-2);
}
.conv-head .actions button:hover { background: var(--bg-soft); color: var(--gold); }

/* Pinned info bar (alias/note quick view) */
.pinned-bar {
  background: var(--gold-soft);
  border-bottom: 1px solid var(--gold-pale);
  padding: 8px 14px;
  font-size: 12.5px;
  color: var(--ink);
  display: none;
}
.pinned-bar.show { display: block; }
.pinned-bar .tag-row { display: inline-flex; gap: 5px; margin-left: 8px; }
.pinned-bar .mini-tag {
  background: var(--gold);
  color: white;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 10.5px;
}

/* Messages area */
#messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 12px 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}
.bubble {
  max-width: 70%;
  padding: 7px 11px 6px;
  border-radius: 16px;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap;
  position: relative;
  font-size: 14.5px;
  box-shadow: 0 1px 1.5px rgba(0,0,0,0.04);
}
.bubble.user {
  align-self: flex-start;
  background: var(--bubble-in);
  color: var(--bubble-in-tx);
  border-bottom-left-radius: 4px;
  border: 1px solid #E0E2E7;
}
.bubble.admin {
  align-self: flex-end;
  background: var(--bubble-out);
  color: var(--bubble-out-tx);
  border-bottom-right-radius: 4px;
}
.bubble .ts {
  font-size: 10.5px;
  color: rgba(0,0,0,0.4);
  margin-left: 6px;
  vertical-align: baseline;
  white-space: nowrap;
}
.bubble.admin .ts { color: rgba(58,37,16,0.5); }
.bubble.admin .tick-sent::after  { content: ' ✓';   color: rgba(58,37,16,0.45); font-weight: 600; }
.bubble.admin .tick-read::after  { content: ' ✓✓'; color: var(--tick); font-weight: 600; }
.bubble .media-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: rgba(0,0,0,0.04);
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 2px;
  cursor: pointer;
}
.bubble .media-badge:hover { background: rgba(0,0,0,0.08); }
.bubble .media-badge.media-lost { color: var(--ink-3); cursor: default; }
.bubble .msg-media {
  display: block;
  max-width: 260px;
  max-height: 340px;
  border-radius: 10px;
  margin: 2px 0 4px;
  background: rgba(0,0,0,0.04);
}
.bubble .msg-img { cursor: zoom-in; object-fit: cover; }
.bubble .msg-video { width: 260px; }
.bubble .msg-audio { width: 240px; display: block; margin: 2px 0 4px; }

.day-divider {
  align-self: center;
  background: rgba(0,0,0,0.12);
  color: white;
  font-size: 11.5px;
  padding: 3px 12px;
  border-radius: 12px;
  margin: 12px 0 6px;
  user-select: none;
}

/* Empty state */
.empty-conv {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--ink-3);
  font-size: 14px;
  gap: 14px;
  padding: 32px;
  text-align: center;
}
.empty-conv .glyph { font-size: 56px; opacity: 0.4; }
.empty-conv h3 { margin: 0; color: var(--ink-2); font-weight: 600; }

/* Composer (Telegram-style rounded) */
.composer {
  display: flex;
  gap: 6px;
  padding: 8px 12px calc(12px + env(safe-area-inset-bottom, 0px));
  background: transparent;
  align-items: flex-end;
}
.attach-btn {
  background: var(--bg);
  border-radius: 50%;
  width: 38px; height: 38px;
  font-size: 17px;
  color: var(--ink-2);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06), 0 0 0 1px var(--line);
  flex-shrink: 0;
}
.attach-btn:hover { color: var(--gold); }
.attach-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: -4px 12px 8px;
  padding: 6px 10px;
  background: var(--gold-soft);
  border: 1px solid var(--gold-pale);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.attach-preview button {
  margin-left: auto;
  color: var(--red);
  font-weight: 600;
}
.composer .input-wrap {
  flex: 1;
  background: var(--bg);
  border-radius: 18px;
  padding: 5px 10px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06), 0 0 0 1px var(--line);
}
.composer textarea {
  width: 100%;
  border: none;
  padding: 6px 4px;
  resize: none;
  background: transparent;
  font-size: 14.5px;
  max-height: 120px;
  line-height: 1.4;
}
.composer button {
  background: var(--gold);
  color: white;
  border-radius: 50%;
  width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
  transition: transform 0.1s;
  font-size: 16px;
}
.composer button:hover:not(:disabled) { transform: scale(1.05); }
.composer button:disabled { background: var(--ink-3); cursor: not-allowed; }

/* ── Dialog (Telegram-style modals) ───────────────────────────────────────── */
dialog {
  border: none;
  border-radius: 12px;
  padding: 18px 22px;
  max-width: 420px;
  font-family: inherit;
  color: var(--ink);
  box-shadow: 0 12px 36px rgba(0,0,0,0.18);
}
dialog::backdrop { background: rgba(15, 15, 16, 0.42); }
dialog h3 { margin: 0 0 12px; font-size: 17px; }
dialog label { display: block; margin: 10px 0; font-size: 13px; color: var(--ink-2); }
dialog input, dialog textarea, dialog select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-top: 4px;
  background: var(--bg);
  font-size: 14px;
}
dialog input:focus, dialog textarea:focus, dialog select:focus { border-color: var(--gold); }
dialog menu { display: flex; gap: 8px; justify-content: flex-end; margin: 16px 0 0; padding: 0; }
dialog menu button {
  padding: 8px 18px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  font-weight: 500;
}
dialog menu button[value="default"], dialog menu button[id$="-save"], dialog menu button[id$="-send"] {
  background: var(--gold);
  color: white;
  border-color: var(--gold);
}

/* ── Toast ─────────────────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(15, 15, 16, 0.88);
  color: white;
  padding: 9px 18px;
  border-radius: 22px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  z-index: 99;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Stats / tags (same as before, re-tuned colors) ───────────────────────── */
#stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 8px 0 14px;
}
#stats-grid .stat-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px 12px;
  text-align: center;
  background: var(--bg-tint);
}
#stats-grid .stat-card .num  { font-size: 28px; font-weight: 700; color: var(--gold); line-height: 1.1; }
#stats-grid .stat-card .lbl  { font-size: 12px; color: var(--ink-2); margin-top: 6px; }

#tags-list { display: flex; flex-wrap: wrap; gap: 6px; max-width: 380px; min-height: 32px; }
.tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--gold-soft);
  color: var(--gold);
  border: 1px solid var(--gold-pale);
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}
.tag-chip.empty { color: var(--ink-3); background: var(--bg-soft); border-color: var(--line); cursor: default; }
.tag-chip .ct { background: var(--gold); color: white; border-radius: 999px; padding: 0 7px; font-size: 11px; font-weight: 600; }
.tag-chip.active { background: var(--gold); color: white; }
.tag-chip.active .ct { background: rgba(255,255,255,0.3); color: white; }

/* ── Broadcast history list ────────────────────────────────────────────────── */
#bc-history-list {
  max-height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}
.bc-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--bg-tint);
  font-size: 12.5px;
}
.bc-row .bc-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 4px;
}
.bc-row .bc-status {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
}
.bc-row .bc-status.done    { background: #DEF3E7; color: var(--green); }
.bc-row .bc-status.running { background: var(--gold-soft); color: var(--gold); }
.bc-row .bc-status.failed  { background: #FCE4E4; color: var(--red); }
.bc-row .bc-body {
  color: var(--ink-2);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.bc-row .bc-stats {
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--ink-3);
}
