:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --line: #e3e8ee;
  --text: #1c2430;
  --muted: #6b7684;
  --brand: #1d9e75;
  --brand-dark: #0f6e56;
  --mine: #d9f2ea;
  --radius: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
}

.hidden { display: none !important; }

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  background: var(--bg);
}

/* ---------- gate ---------- */

#gate {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.gate-card {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 32px 24px;
}

.gate-logo {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: var(--brand);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin-bottom: 16px;
  overflow: hidden;
  flex-shrink: 0;
}
.gate-logo img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}

.gate-card h1 { font-size: 20px; font-weight: 600; margin: 0 0 6px; }
.gate-card p.sub { margin: 0 0 20px; color: var(--muted); font-size: 14px; line-height: 1.6; }

/* 信任要点：降低「点开外链=被骗」的疑虑，不依赖机构配置，JS 不会覆盖 */
.trust-points {
  list-style: none;
  margin: 0 0 24px;
  padding: 14px 16px;
  background: #f4f8ff;
  border: 1px solid #e3ecfb;
  border-radius: 12px;
}
.trust-points li {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  font-size: 13px;
  line-height: 1.5;
  color: #3d4a5c;
}
.trust-points li + li { margin-top: 10px; }
.trust-points svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  margin-top: 1px;
  color: var(--brand);
}

label.field { display: block; margin-bottom: 16px; }
label.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }

input[type=text], input[type=tel], input[type=password] {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  background: #fbfcfd;
  color: var(--text);
  outline: none;
}
input:focus { border-color: var(--brand); background: #fff; }

button.primary {
  width: 100%;
  padding: 13px;
  border: none;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  font-size: 16px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
}
button.primary:hover { background: var(--brand-dark); }
button.primary:active { transform: translateY(1px); }

.gate-note { margin: 16px 0 0; font-size: 12px; color: var(--muted); line-height: 1.6; }

/* ---------- chat ---------- */

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

header.top {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 500;
  flex-shrink: 0;
  position: relative;
  overflow: visible;
}

.avatar img {
  width: 100%; height: 100%;
  border-radius: 50%;
  object-fit: cover;
  display: block;
}

/* 可点击换头像 */
.avatar-tap { cursor: pointer; transition: transform .12s ease; }
.avatar-tap:active { transform: scale(.93); }
.avatar-tap::after {
  content: '';
  position: absolute; inset: -3px;
  border-radius: 50%;
  border: 1.5px dashed var(--brand);
  opacity: 0;
  transition: opacity .15s ease;
}
.avatar-tap:hover::after { opacity: .38; }

.avatar-lg { width: 62px; height: 62px; font-size: 24px; }

.avatar-picker {
  display: flex; align-items: center; gap: 12px;
  margin: 4px 0 18px;
}
.avatar-picker-tip {
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.45;
}

/* T1 顾问在线绿点 */
#live-dot {
  position: absolute; right: -1px; bottom: -1px;
  width: 11px; height: 11px; border-radius: 50%;
  background: #b4b2a9; border: 2px solid var(--card);
  transition: background .2s;
}
#live-dot.on { background: #1d9e75; }

.top .who { flex: 1; min-width: 0; }
.top .who b { display: block; font-size: 15px; font-weight: 600; }
.top .who small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn-reset {
  background: none;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
  flex-shrink: 0;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-overflow-scrolling: touch;
}

/* 空状态欢迎卡：客户进来不再是空白，明确告知「已收到、正在等顾问」 */
.welcome { margin: auto 0; display: flex; justify-content: center; }
.welcome-inner {
  text-align: center;
  padding: 22px 20px;
  max-width: 300px;
}
.welcome-title {
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 8px;
}
.welcome-sub {
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
  margin-bottom: 12px;
}
.welcome-brand {
  display: inline-block;
  font-size: 12px;
  color: var(--brand);
  background: var(--mine);
  padding: 4px 12px;
  border-radius: 20px;
}

.row { display: flex; flex-direction: column; max-width: 76%; }
.row.me { align-self: flex-end; align-items: flex-end; }
.row.them { align-self: flex-start; align-items: flex-start; }

.bubble {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 4px var(--radius) var(--radius) var(--radius);
  padding: 10px 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
.row.me .bubble {
  background: var(--mine);
  border-color: #c3e7db;
  border-radius: var(--radius) 4px var(--radius) var(--radius);
}

.bubble-img {
  max-width: 100%;
  max-height: 320px;
  border-radius: var(--radius);
  display: block;
  cursor: zoom-in;
  border: 1px solid var(--line);
}

.meta { font-size: 11px; color: var(--muted); margin-top: 5px; }
.meta.read { color: var(--brand); }

/* T1 已读回执：自己发的消息最后一条带绿字"已读" */
/* T4 发送失败的气泡可点击重试 */
.row.me.failed .bubble { background: #fceceb; }
.row.me.failed { cursor: pointer; }
.row.me.failed .meta { color: #a32d2d; font-weight: 500; }

/* T6 上传进度条 */
#prog-wrap {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 16px 8px;
  font-size: 12px; color: var(--muted);
}
#prog-track {
  flex: 1; height: 4px; border-radius: 2px;
  background: #e3e8ee; overflow: hidden;
}
#prog-bar {
  height: 100%; width: 0;
  background: var(--brand);
  transition: width .2s;
}
#prog-wrap.failed #prog-bar { background: #e24b4a; }
#prog-wrap.failed #prog-text { color: #a32d2d; }
#prog-text { flex-shrink: 0; }
#prog-retry {
  flex-shrink: 0; padding: 3px 10px; font-size: 12px;
  border: 1px solid #e24b4a; border-radius: 12px;
  background: #fff; color: #a32d2d; cursor: pointer;
}

footer.bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}

footer.bar textarea {
  flex: 1;
  min-height: 40px;
  max-height: 110px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  background: #fbfcfd;
  color: var(--text);
  outline: none;
  line-height: 1.4;
}
footer.bar textarea:focus { border-color: var(--brand); background: #fff; }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fbfcfd;
  color: var(--muted);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.icon-btn:hover { color: var(--brand); border-color: var(--brand); }

.send-btn {
  width: 40px; height: 40px;
  border-radius: 10px;
  border: none;
  background: var(--brand);
  color: #fff;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.send-btn:hover { background: var(--brand-dark); }

#tip {
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  padding: 0 16px;
  min-height: 0;
}
#tip:not(:empty) { padding: 6px 16px; }

/* ---------- viewer ---------- */

#viewer {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99;
  padding: 20px;
}
#viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* ---------- T7 移动端适配 ---------- */

@media (max-width: 560px) {
  #app { max-width: 100%; }
  .row { max-width: 86%; }
  .bubble { font-size: 15px; }
  #messages { padding: 14px 12px 6px; gap: 12px; }
  .gate-card { padding: 28px 20px; }
  .btn-reset { padding: 5px 8px; font-size: 11px; }
  .avatar { width: 34px; height: 34px; font-size: 14px; }
}

/* iOS Safari 地址栏收起/展开时 100vh 会跳动，用 dvh 修正 */
@supports (height: 100dvh) {
  html, body, #app { height: 100dvh; }
}

/* textarea 自适应高度（app.js autoGrow 控制 max-height 120px） */
#input { max-height: 120px; overflow-y: auto; }
#input::placeholder { color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  #prog-bar, #live-dot { transition: none; }
}

/* ---------- 工作台会话列表头像（含客户自定义头像） ---------- */
.avatar-sm {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; font-weight: 500;
  flex-shrink: 0;
  position: relative;
  overflow: visible;
}
.avatar-sm img {
  width: 100%; height: 100%;
  border-radius: 50%;
  object-fit: cover;
  display: block;
}
/* 客户在线绿点：图片头像上也能显示，压在右下角 */
.avatar-sm .dot-live {
  position: absolute; right: -1px; bottom: -1px;
  width: 11px; height: 11px; border-radius: 50%;
  background: #16a34a;
  border: 2px solid var(--bg, #fff);
}
