:root {
  --bg: #f7f8fa;
  --panel: #ffffff;
  --ink: #1f2328;
  --ink-soft: #6b7480;
  --line: #e3e6ea;
  --deep: #1a3040;
  --deep-soft: #2c4a5e;
  --her: #f0f2f5;
}

* { box-sizing: border-box; }

/* 侧边栏 */
#menu {
  border: none;
  background: none;
  font: inherit;
  font-size: 18px;
  line-height: 1;
  padding: 4px 6px;
  margin-right: 2px;
  color: var(--deep);
  cursor: pointer;
}
#scrim {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 30, 0.36);
  z-index: 40;
}
#drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: 292px;
  max-width: 86vw;
  padding: 14px 16px calc(18px + env(safe-area-inset-bottom));
  background: var(--panel);
  box-shadow: 0 8px 30px rgba(20, 24, 30, 0.18);
  overflow-y: auto;
  z-index: 41;
}
#drawer.hidden, #scrim.hidden, #confirm.hidden { display: none; }
.dhead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 0 12px;
  border-bottom: 1px solid var(--line);
}
.dhead h2 { margin: 0; font-size: 15px; font-weight: 600; color: var(--deep); }
#drawerClose {
  border: none;
  background: none;
  font: inherit;
  font-size: 22px;
  line-height: 1;
  padding: 6px 10px;
  margin: -6px -10px -6px 0;
  color: var(--ink-soft);
  cursor: pointer;
}
.dbtn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  margin-top: 12px;
  line-height: 1.4;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  cursor: pointer;
}
.dbtn:hover { background: var(--bg); }
.dbtn.danger { color: #b3261e; border-color: #f0d5d2; }
.dhint { margin: 5px 2px 0; font-size: 12px; line-height: 1.6; color: var(--ink-soft); }

/* 说明紧跟它自己那个按钮，下一个按钮留出明显更大的间距，
   这样成组关系不靠感觉判断 */
.dhint + .dbtn { margin-top: 20px; }
.dhint.warn { color: #a4443c; }

/* 退出登录不属于清理动作，用分隔线单独成组 */
#logoutForm {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
}
#logoutForm .dbtn { margin-top: 0; }

/* 清空类操作的确认框 */
#confirm {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 24, 30, 0.42);
  z-index: 50;
}
#confirm .card {
  width: min(360px, 86vw);
  padding: 18px;
  border-radius: 14px;
  background: var(--panel);
}
#confirm h3 { margin: 0 0 8px; font-size: 16px; color: var(--deep); }
#confirm p { margin: 0 0 16px; font-size: 13px; line-height: 1.7; color: #5b6472; }
.cfrow { display: flex; gap: 10px; justify-content: flex-end; }
.cfrow button {
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--panel);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
#cfYes { color: #b3261e; border-color: #f0d5d2; }


html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans CJK SC",
               "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
  /* dvh 而非 vh：避免移动浏览器地址栏收放导致布局跳动 */
  height: 100dvh;
}

#bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

#who {
  font-weight: 600;
  color: var(--deep);
  letter-spacing: 0.02em;
}

#conn {
  font-size: 12px;
  color: var(--ink-soft);
  margin-left: auto;
}
#conn.on   { color: #2f7a52; }
#conn.off  { color: #b4622c; }

#list {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.timebar {
  align-self: center;
  margin: 16px 0 4px;
  font-size: 11px;
  line-height: 1;
  color: var(--ink-soft);
  user-select: none;
}

.msg {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-areas:
    "av bb"
    ".  mt";
  align-items: end;
  column-gap: 8px;
  max-width: min(82%, 660px);
  animation: pop 0.18s ease-out;
}

@keyframes pop {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: none; }
}

.msg.her { align-self: flex-start; }
.msg.me  {
  align-self: flex-end;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "bb av"
    "mt .";
}

/* 头像占位。现在没有图片，用灰度圆加深灰字。
   换真图时给这里设 background-image，字自然会被盖住。 */
.avatar {
  grid-area: av;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #e3e6ea;
  background-size: cover;
  background-position: center;
  color: #8b94a0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  overflow: hidden;
  user-select: none;
}

/* 同角色连续消息只在末条显示头像。
   用 visibility 而不是 display，保留占位，气泡不向边缘平移。 */
.avatar.hidden { visibility: hidden; }

/* 让气泡与状态文字直接参与 .msg 的网格，避免多包一层影响对齐 */
.body { display: contents; }

.bubble {
  grid-area: bb;
  padding: 9px 13px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* 带图片的气泡：图片在上，文字在下，四周留一点内边距 */
.bubble.has-imgs { padding: 6px; }
.bubble.has-imgs .shot {
  display: block;
  max-width: 100%;
  max-height: 260px;
  border-radius: 10px;
  background: #eef0f3;
  cursor: zoom-in;
}
.bubble.has-imgs .shot { margin-bottom: 6px; }

/* 图片占位。本地没有这张图时显示它，点了才去拉。 */
.bubble.has-imgs .imgload {
  display: block;
  border: none;
  border-radius: 10px;
  background: #e3e6ea;
  color: #6b7480;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  margin-bottom: 6px;
}
.bubble.has-imgs .imgload:disabled { cursor: default; color: #8b94a0; }

.bubble.has-imgs .text {
  display: block;
  padding: 5px 6px 1px;
}

.msg.her .bubble {
  background: var(--her);
  border-top-left-radius: 5px;
}

.msg.me .bubble {
  background: var(--deep);
  color: #fff;
  border-top-right-radius: 5px;
}

.meta {
  grid-area: mt;
  font-size: 11px;
  color: var(--ink-soft);
  margin-top: 3px;
  min-height: 14px;
}

.msg.me .meta { text-align: right; }

#composer {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 16px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}

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

#attach {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  font: inherit;
  font-size: 20px;
  line-height: 1;
  color: var(--ink-soft);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
}
#attach:hover { color: var(--deep); border-color: var(--deep-soft); }

#file { display: none; }

/* 待发送图片的缩略图条 */
#thumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
#thumbs.hidden { display: none; }

.thumb {
  position: relative;
  width: 64px;
  height: 64px;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: #eef0f3;
}
.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.thumb button {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  padding: 0;
  font: inherit;
  font-size: 13px;
  line-height: 1;
  color: #fff;
  background: rgba(26, 48, 64, 0.72);
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

/* 拖入图片时给整页一个提示边框 */
body.dragging #list {
  outline: 2px dashed var(--deep-soft);
  outline-offset: -8px;
}

#input {
  flex: 1 1 auto;
  resize: none;
  max-height: 140px;
  min-height: 44px;
  padding: 10px 12px;
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  outline: none;
}
#input:focus { border-color: var(--deep-soft); }

#send {
  flex: 0 0 auto;
  min-width: 64px;
  min-height: 44px;
  padding: 0 16px;
  font: inherit;
  color: #fff;
  background: var(--deep);
  border: none;
  border-radius: 12px;
  cursor: pointer;
}
#send:disabled { opacity: 0.45; cursor: default; }
#send:active  { background: var(--deep-soft); }

#setup {
  position: fixed;
  inset: 0;
  background: rgba(26, 48, 64, 0.42);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}

#setup.hidden { display: none; }

.card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border-radius: 16px;
  padding: 22px;
  box-shadow: 0 8px 32px rgba(26, 48, 64, 0.18);
}

.card h2 {
  margin: 0 0 6px;
  font-size: 18px;
  color: var(--deep);
}

.card p {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--ink-soft);
}

.card input {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  font: inherit;
  border: 1px solid var(--line);
  border-radius: 12px;
  outline: none;
}
.card input:focus { border-color: var(--deep-soft); }

.card button {
  width: 100%;
  min-height: 44px;
  margin-top: 12px;
  font: inherit;
  color: #fff;
  background: var(--deep);
  border: none;
  border-radius: 12px;
  cursor: pointer;
}

.err {
  margin-top: 8px;
  font-size: 12px;
  color: #b4362c;
  min-height: 16px;
}

/* 她正在生成的提示 */
.msg.typing .bubble {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 14px;
}
.msg.typing .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #9aa3ad;
  animation: blink 1.2s infinite ease-in-out;
}
.msg.typing .dot:nth-child(2) { animation-delay: 0.2s; }
.msg.typing .dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

/* 错误提示行。与气泡视觉上分开，表示这不是她说的。 */
.notice {
  align-self: flex-start;
  max-width: min(88%, 700px);
  margin-left: 42px;
  font-size: 12px;
  color: #b4362c;
  display: flex;
  align-items: center;
  gap: 8px;
}
.notice-retry {
  font: inherit;
  font-size: 12px;
  padding: 2px 8px;
  color: var(--deep);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
}
.notice-retry:hover { border-color: var(--deep-soft); }

/* 登录页 */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  /* 用 min-height 而不是固定高度：手机软键盘弹出时可视区变矮，
     固定高度会让居中的卡片被顶出可视区，输入框可能被键盘遮住。 */
  height: auto;
  min-height: 100dvh;
  overflow: auto;
}
.login-card {
  max-width: 340px;
  margin: 0;
}

/* 顶栏的退出按钮 */
#logoutForm { margin: 0; }
#logout {
  min-height: 32px;
  padding: 0 10px;
  font: inherit;
  font-size: 13px;
  color: var(--ink-soft);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
#logout:hover { color: var(--deep); border-color: var(--deep-soft); }

/* 只有一个断点，不做两套布局 */
@media (max-width: 768px) {
  html, body { font-size: 15px; }
  .msg { max-width: 88%; column-gap: 6px; }
  .notice { margin-left: 34px; }
  .avatar { width: 28px; height: 28px; font-size: 12px; }
  #list { padding: 12px 12px 6px; gap: 8px; }
  #bar, #composer { padding-left: 12px; padding-right: 12px; }
  #who { font-size: 15px; }
}
