/* wa_console 样式 —— 手写，零依赖，零构建。
 *
 * 不用 Tailwind CDN：国内访问 cdn.tailwindcss.com 不稳定，前端会变成「没样式」。
 *
 * 改完必须提升 templates/base.html 里的 ?v= 参数，否则浏览器还在用旧文件。
 */

:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #fafbfc;
  --border: #e3e6ea;
  --border-strong: #d3d8de;

  --text: #1a1f27;
  --text-dim: #5b6472;      /* 次要文字：对比度 ≥ 4.5:1 */
  --text-faint: #7c8695;    /* 时间戳等：仍可读，但明显弱于正文 */

  --brand: #2563eb;
  --brand-ink: #1d4ed8;
  --brand-soft: #e6efff;

  --ok: #046c4e;            /* 加深：浅底上的小字要够对比 */
  --ok-soft: #d6f5e6;
  --warn: #92400e;
  --warn-soft: #fdf0d5;
  --danger: #b42318;
  --danger-soft: #fee4e2;

  /* 统一的圆角刻度，避免「有的胶囊有的方」 */
  --r-sm: 8px;
  --r: 10px;
  --r-lg: 14px;

  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 10px 24px rgba(16, 24, 40, .12);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
          "Hiragino Sans GB", "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; font-size: inherit; cursor: pointer; }
a { color: inherit; text-decoration: none; }
textarea, input, select { font-family: inherit; font-size: inherit; }

/* ---------------------------------------------------------------- 布局 --- */

.app { display: flex; flex-direction: column; height: 100%; }

.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 0 16px; height: 52px; flex: 0 0 52px;
  background: var(--panel); border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow); z-index: 20;
  overflow: hidden;           /* 子元素再挤也不许画到栏外 */
}
/* ⚠️ nowrap 必须加：窄屏下「WA 控制台」会被压到 42px 宽而折成三行，
   把 52px 的顶栏撑到 68px。 */
.topbar .logo {
  font-weight: 700; font-size: 15px; letter-spacing: -.2px;
  white-space: nowrap; flex: 0 0 auto;
}
.topbar .spacer { flex: 1 1 auto; min-width: 8px; }

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

.pane-list {
  width: 320px; flex: 0 0 320px; display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--border); min-height: 0;
}
.pane-thread { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }

/* #thread 是 HTMX 的替换目标，自身必须是纵向 flex 容器，
   否则 头部/消息流/输入框 三块不会正确分配高度 */
.thread-root { display: flex; flex-direction: column; flex: 1; min-height: 0; }

@media (max-width: 860px) {
  .pane-list { width: 100%; flex: 1 1 auto; border-right: none; }
  .pane-thread { display: none; }
  body.showing-thread .pane-list { display: none; }
  body.showing-thread .pane-thread { display: flex; }
  #back-btn { display: inline-flex !important; }
  /* 窄屏只留最关键的状态，其余靠横向滚动看 */
  .status-bar .pill.optional { display: none; }
}

/* ------------------------------------------------------------- 状态条 --- */

.status-bar {
  display: flex; align-items: center; gap: 6px;
  flex-wrap: nowrap;          /* 换行会把 52px 的顶栏撑到 56px 以上 */
  overflow-x: auto; min-width: 0;
  scrollbar-width: none;
}
.status-bar::-webkit-scrollbar { display: none; }

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 999px; font-size: 12px;
  font-weight: 600; background: #eef0f3; color: var(--text-dim);
  white-space: nowrap; flex: 0 0 auto;
  line-height: 1.35;
}
.pill.ok     { background: var(--ok-soft);     color: var(--ok); }
.pill.warn   { background: var(--warn-soft);   color: var(--warn); }
.pill.danger { background: var(--danger-soft); color: var(--danger); }
.pill.brand  { background: var(--brand-soft);  color: var(--brand-ink); }
.pill.human  { background: var(--warn-soft);   color: var(--warn); }
.pill.bot    { background: var(--ok-soft);     color: var(--ok); }
.pill.closed { background: #eef0f3;            color: var(--text-faint); }

.dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: 0 0 auto; }
.dot.pulse { animation: pulse 2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .3; } }

/* -------------------------------------------------------- 会话列表 --- */

.list-toolbar {
  padding: 10px; border-bottom: 1px solid var(--border);
  display: flex; gap: 8px; align-items: center; flex: 0 0 auto;
}
.list-toolbar input[type="search"] {
  flex: 1; min-width: 0; padding: 7px 10px; border: 1px solid var(--border);
  border-radius: var(--r-sm); outline: none; background: var(--panel-2);
}
.list-toolbar input[type="search"]::placeholder { color: var(--text-faint); }
.list-toolbar input[type="search"]:focus {
  border-color: var(--brand); background: #fff;
  box-shadow: 0 0 0 3px var(--brand-soft);
}
.list-toolbar select {
  padding: 7px 8px; border: 1px solid var(--border); border-radius: var(--r-sm);
  background: var(--panel-2); outline: none; flex: 0 0 auto;
}

.conv-scroll { flex: 1; overflow-y: auto; }

.conv-item {
  display: flex; gap: 10px; padding: 11px 12px;
  border-bottom: 1px solid #f0f1f3; cursor: pointer; position: relative;
  transition: background .12s;
}
.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: var(--brand-soft); }
.conv-item.active::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0;
  width: 3px; background: var(--brand);
}

/* 头像配色由服务端按名字哈希成色相注入 --h，保证同一个人颜色恒定 */
.avatar {
  width: 38px; height: 38px; flex: 0 0 38px; border-radius: 50%;
  background: linear-gradient(135deg,
              hsl(var(--h, 218) 72% 68%), hsl(var(--h, 218) 68% 46%));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px; user-select: none;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .06);
}
.conv-body { flex: 1; min-width: 0; }
.conv-line1 { display: flex; align-items: baseline; gap: 8px; }
.conv-name {
  font-weight: 650; font-size: 14px; color: var(--text);
  flex: 1; min-width: 0; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.conv-time { font-size: 11px; color: var(--text-faint); flex: 0 0 auto; }

.conv-line2 { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.conv-preview {
  flex: 1; min-width: 0; color: var(--text-dim); font-size: 12.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-item.active .conv-preview { color: var(--text); }
.unread {
  background: var(--brand); color: #fff; font-size: 11px; font-weight: 700;
  min-width: 18px; height: 18px; border-radius: 9px; display: flex;
  align-items: center; justify-content: center; padding: 0 5px; flex: 0 0 auto;
}
.flag { font-size: 12px; flex: 0 0 auto; line-height: 1; }

/* ---------------------------------------------------------- 消息流 --- */

.thread-head {
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  background: var(--panel); display: flex; align-items: center; gap: 8px;
  flex-wrap: wrap; flex: 0 0 auto; row-gap: 6px;
}
.thread-head .who { font-weight: 700; font-size: 15px; letter-spacing: -.2px; }
.thread-head .num {
  color: var(--text-dim); font-size: 12px; font-family: var(--mono);
  letter-spacing: -.2px;
}
.thread-head .spacer { flex: 1 1 auto; }

/* 消息流：极淡的纵向渐变代替点阵 —— 点阵在大面积空白上像线框图 */
.thread-scroll {
  flex: 1; overflow-y: auto; padding: 18px 16px 10px;
  background: linear-gradient(180deg, #f7f8fa 0%, var(--bg) 220px);
  display: flex; flex-direction: column;
}

.day-sep {
  align-self: center; margin: 14px 0 10px; font-size: 11px;
  color: var(--text-dim); background: #e9ecf0;
  padding: 3px 12px; border-radius: 999px; font-weight: 600;
}

.msg { display: flex; margin-top: 8px; }
.msg.in  { justify-content: flex-start; }
.msg.out { justify-content: flex-end; }
.msg.sys { justify-content: center; }

.bubble {
  max-width: min(560px, 76%); padding: 8px 12px; border-radius: var(--r-lg);
  background: var(--panel); box-shadow: var(--shadow); position: relative;
  word-wrap: break-word;
  /* ⚠️ 绝不能在这里用 white-space: pre-wrap。
     Jinja 模板为了可读性做了缩进换行，pre-wrap 会把这些**结构性空白**
     当成可见文本渲染 —— 实测一个含图片+一行文字的气泡，子元素加起来
     只有 125px，气泡本身却有 485px，多出的 360px 全是模板缩进。
     消息正文的换行需求由下面的 .bubble .text 单独承担。 */
}
.msg.in  .bubble { border-bottom-left-radius: 5px; }
.msg.out .bubble { border-bottom-right-radius: 5px; }
.msg.out .bubble.bot   { background: #e8f0fe; }
.msg.out .bubble.human { background: var(--ok-soft); }

/* 消息正文需要保留换行（客户/客服会打多行），但只作用于正文本身 */
.bubble .text { white-space: pre-wrap; }

.msg.sys .bubble {
  background: transparent; box-shadow: none; color: var(--text-dim);
  font-size: 12px; font-style: italic; max-width: 80%; text-align: center;
  white-space: pre-wrap;      /* 系统气泡里是裸文本，没有 .text 包裹 */
}

.bubble .meta {
  display: flex; align-items: center; gap: 6px; justify-content: flex-end;
  margin-top: 4px; font-size: 10.5px; color: var(--text-faint);
}
.bubble .who-tag {
  font-weight: 700; color: var(--text-dim); margin-right: auto;
  font-size: 10.5px; letter-spacing: .2px;
}
.tick { font-size: 11px; letter-spacing: -1px; }
.tick.read { color: var(--brand); }

/* ---------------------------------------------------------- 输入区 --- */

.composer {
  border-top: 1px solid var(--border); background: var(--panel);
  padding: 10px 14px 12px; flex: 0 0 auto;
}
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; padding: 9px 12px; border: 1px solid var(--border);
  border-radius: var(--r); outline: none; max-height: 140px; min-height: 40px;
  line-height: 1.45; background: var(--panel-2); color: var(--text);
}
.composer textarea::placeholder { color: var(--text-faint); }
.composer textarea:focus {
  border-color: var(--brand); background: #fff;
  box-shadow: 0 0 0 3px var(--brand-soft);
}
.composer .hint {
  font-size: 11.5px; color: var(--text-dim); margin-top: 7px; line-height: 1.45;
}

.btn {
  padding: 8px 14px; border-radius: var(--r); border: 1px solid var(--border);
  background: var(--panel); color: var(--text); font-weight: 600;
  transition: background .12s, border-color .12s, box-shadow .12s;
  white-space: nowrap; line-height: 1.35;
}
.btn:hover { background: #f1f3f6; border-color: var(--border-strong); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--brand-soft); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-ink); border-color: var(--brand-ink); }
.btn.warn { background: var(--warn); border-color: var(--warn); color: #fff; }
.btn.warn:hover { background: #7a3609; border-color: #7a3609; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.ok:hover { background: #03543c; border-color: #03543c; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.sm { padding: 5px 11px; font-size: 12.5px; border-radius: var(--r-sm); }

/* ------------------------------------------------------------ 媒体 --- */

.media-img {
  max-width: 100%; max-height: 320px; border-radius: var(--r);
  display: block; cursor: zoom-in; background: #f1f3f6;
  /* 极小图（如 24×24 表情/贴纸）不设边框会像页面上的一个杂点。
     给个最小展示尺寸 + 描边，让它读起来就是「一张图」。 */
  min-width: 48px; min-height: 48px;
  object-fit: contain;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .07);
}
.media-video {
  max-width: 100%; max-height: 320px; border-radius: var(--r);
  display: block; background: #000;
}
.media-audio { width: 250px; max-width: 100%; display: block; }

.media-doc {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--r);
  background: var(--panel-2); max-width: 320px; transition: background .12s;
}
.media-doc:hover { background: #fff; border-color: var(--border-strong); }
.media-doc .doc-icon { font-size: 18px; flex: 0 0 auto; line-height: 1; }
.media-doc .doc-name {
  flex: 1; min-width: 0; font-size: 12.5px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.media-doc .doc-size { color: var(--text-faint); font-size: 11px; flex: 0 0 auto; }

/* 附件与文字之间留一点空隙。
   ⚠️ 图片被 <a> 包了一层（点击看原图），所以既有 `.bubble > img` 也有
   `.bubble > a > img` 两种结构 —— 只写直接子代选择器会静默不生效。 */
.bubble > a > .media-img, .bubble > .media-img,
.bubble > .media-video, .bubble > .media-audio, .bubble > .media-doc {
  margin-bottom: 2px;
}
.media-img + .text, .media-video + .text,
.media-audio + .text, .media-doc + .text { margin-top: 7px; }

/* 纯附件消息（气泡里只有一张图/一段视频）收紧内边距，让图更满。
   同样要覆盖 <a> 包裹的情况。 */
.bubble:has(> .media-img),
.bubble:has(> a > .media-img),
.bubble:has(> .media-video),
.bubble:has(> a > .media-video) { padding: 5px; }

.bubble:has(> .media-img) .meta,
.bubble:has(> a > .media-img) .meta,
.bubble:has(> .media-video) .meta,
.bubble:has(> a > .media-video) .meta { padding: 0 4px; }

.composer .btn[title*="附件"] { padding: 8px 10px; }

/* -------------------------------------------------------------- 告警 --- */

.alerts-box {
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  background: #fffbf4; flex: 0 0 auto;
}
.alert-row {
  display: flex; align-items: center; gap: 8px; padding: 7px 0;
  border-bottom: 1px dashed #f0e0c8;
}
.alert-row:last-child { border-bottom: none; padding-bottom: 0; }
.alert-row .rule { font-weight: 700; font-size: 12.5px; }
.alert-row .kw { color: var(--text-dim); font-size: 12px; }

.toast-wrap {
  position: fixed; right: 16px; bottom: 16px; z-index: 100;
  display: flex; flex-direction: column; gap: 8px; max-width: 340px;
}
.toast {
  background: var(--panel); border-left: 4px solid var(--brand);
  border-radius: var(--r-sm); box-shadow: var(--shadow-lg); padding: 10px 12px;
  animation: slidein .2s ease-out;
}
.toast.urgent { border-left-color: var(--danger); }
.toast.high   { border-left-color: var(--warn); }
.toast.error  { border-left-color: var(--danger); }
.toast .t-title { font-weight: 700; font-size: 13px; }
.toast .t-body  {
  font-size: 12.5px; color: var(--text-dim); margin-top: 3px; line-height: 1.45;
  overflow: hidden; text-overflow: ellipsis; display: -webkit-box;
  -webkit-line-clamp: 3; -webkit-box-orient: vertical;
}
@keyframes slidein { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ------------------------------------------------------------ 看板 --- */

.page { max-width: 1100px; margin: 0 auto; padding: 22px 18px 60px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--r); padding: 15px 16px; box-shadow: var(--shadow);
}
.card .k { font-size: 12px; color: var(--text-dim); font-weight: 600; }
.card .v { font-size: 27px; font-weight: 700; margin-top: 5px; letter-spacing: -.8px; line-height: 1.1; }
.card .s { font-size: 11.5px; color: var(--text-faint); margin-top: 4px; }
h2.sec { font-size: 15px; margin: 28px 0 10px; letter-spacing: -.2px; }

table.tbl {
  width: 100%; border-collapse: collapse; background: var(--panel);
  border: 1px solid var(--border); border-radius: var(--r); overflow: hidden;
}
table.tbl th, table.tbl td {
  padding: 9px 12px; text-align: left; font-size: 13px;
  border-bottom: 1px solid var(--border);
}
table.tbl th {
  background: var(--panel-2); font-weight: 600; color: var(--text-dim); font-size: 12px;
}
table.tbl tr:last-child td { border-bottom: none; }

.bar { height: 6px; background: #eceff3; border-radius: 3px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--brand); border-radius: 3px; }

.empty { padding: 40px 20px; text-align: center; color: var(--text-faint); }

.htmx-request .spinner { display: inline-block; }
.spinner {
  display: none; width: 12px; height: 12px; margin-left: 6px;
  border: 2px solid rgba(255, 255, 255, .4); border-top-color: #fff;
  border-radius: 50%; animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
