:root {
  --bar: #229ed9;
  --bar-dark: #1b8ec4;
  --bg: #eef2f5;
  --panel: #ffffff;
  --line: #d7e0e7;
  --text: #1f2d3a;
  --muted: #7a8b99;
  --in: #ffffff;
  --out: #dcf8c6;
  --unread: #e53935;
  --shadow: 0 8px 24px rgba(31, 45, 58, 0.08);
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  height: 100dvh;
  margin: 0;
}
body {
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.login-card {
  width: min(360px, 100%);
  background: var(--panel);
  padding: 32px 28px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-card h1 { margin: 0 0 8px; font-size: 22px; text-align: center; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.login-card input, .composer input {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 16px;
}
.login-card button, .composer button, .topbar button {
  border: 0;
  background: var(--bar);
  color: #fff;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 15px;
  cursor: pointer;
}
.login-card button:hover, .composer button:hover { background: var(--bar-dark); }
.error { color: var(--unread); margin: 0; font-size: 13px; }

.app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
.topbar {
  min-height: 56px;
  background: var(--bar);
  color: #fff;
  display: grid;
  grid-template-columns: 180px 1fr auto;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
}
.brand { font-weight: 700; letter-spacing: 0.04em; }
.account-status { text-align: center; font-size: 14px; }
.top-right { display: flex; align-items: center; gap: 10px; }
.topbar button { background: rgba(255,255,255,0.16); padding: 6px 12px; }
.topbar button:hover { background: rgba(255,255,255,0.28); }

.main { flex: 1; min-height: 0; display: grid; grid-template-columns: 320px 1fr; }
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.sidebar-title {
  padding: 14px 16px;
  font-weight: 600;
  border-bottom: 1px solid var(--line);
}
.customer-list { overflow: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.customer-row { border-bottom: 1px solid var(--line); }
.customer {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 12px 16px;
  cursor: pointer;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 8px;
  -webkit-touch-callout: none;
  user-select: none;
}
.customer:hover, .customer.active { background: #f4f8fb; }
.customer .name { font-weight: 600; }
.customer .preview { grid-column: 1; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.customer .time { color: var(--muted); font-size: 12px; }
.delete-chat {
  display: none;
  width: calc(100% - 24px);
  margin: 0 12px 10px;
  border: 0;
  background: #e53935;
  color: #fff;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  cursor: pointer;
}
.customer-row.ready-delete .delete-chat { display: block; }
.customer:hover, .customer.active { background: #f4f8fb; }
.customer .name { font-weight: 600; }
.customer .preview { grid-column: 1; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.customer .time { color: var(--muted); font-size: 12px; }
.badge {
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  background: var(--unread);
  color: #fff;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
}
.badge.hidden { display: none; }

.chat { min-width: 0; display: flex; flex-direction: column; background: #e8eef3; }
.chat-empty { margin: auto; color: var(--muted); }
.chat-panel { height: 100%; display: flex; flex-direction: column; }
.chat-header {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 12px 18px;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.back-btn {
  display: none;
  border: 0;
  background: transparent;
  color: var(--bar);
  font-size: 16px;
  padding: 8px 4px 8px 0;
  cursor: pointer;
  flex-shrink: 0;
}
.chat-head-text { min-width: 0; flex: 1; }
.chat-name { font-weight: 700; }
.chat-meta {
  color: var(--muted);
  font-size: 13px;
  margin-top: 2px;
}
.messages {
  flex: 1;
  overflow: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  -webkit-overflow-scrolling: touch;
}
.bubble-wrap { display: flex; }
.bubble-wrap.in { justify-content: flex-start; }
.bubble-wrap.out { justify-content: flex-end; }
.bubble {
  max-width: min(62%, 420px);
  padding: 5px 9px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble-wrap.in .bubble { background: var(--in); }
.bubble-wrap.out .bubble { background: var(--out); }
.composer {
  display: flex;
  gap: 8px;
  padding: 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.composer input { flex: 1; min-width: 0; }

.empty-list { color: var(--muted); padding: 24px 16px; font-size: 14px; }

@media (max-width: 768px) {
  .login-card { padding: 24px 18px; }
  .topbar {
    grid-template-columns: 1fr auto;
    padding: 8px 12px;
  }
  .account-status,
  .top-right span {
    display: none;
  }
  .brand { font-size: 16px; }
  .main {
    display: block;
    position: relative;
  }
  .sidebar,
  .chat {
    position: absolute;
    inset: 0;
    width: 100%;
  }
  .sidebar { z-index: 1; }
  .chat {
    z-index: 2;
    display: none;
  }
  .app.show-chat .chat { display: flex; }
  .app.show-chat .sidebar { display: none; }
  .chat-empty { display: none; }
  .back-btn { display: inline-flex; align-items: center; }
  .chat-header { padding: 10px 12px; }
  .chat-meta {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .bubble { max-width: 78%; padding: 5px 8px; }
  .messages { padding: 10px; gap: 5px; }
  .composer {
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  }
  .composer button {
    min-width: 64px;
    padding: 10px 14px;
  }
  .customer { padding: 14px 16px; min-height: 64px; }
}
