:root {
  --bg: #0f1419;
  --panel: #1a2332;
  --panel2: #243044;
  --text: #e7ecf3;
  --muted: #9aa8bc;
  --accent: #5b8cff;
  --accent2: #3d6fe0;
  --err: #ff6b6b;
  --border: #2c3a4f;
  --me: #2a4a7a;
  --other: #243044;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --sidebar-w: 260px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Noto Sans SC", sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
html { background: var(--bg); }
body { margin: 0; background: var(--bg); color: var(--text); }
.app-body { overflow: hidden; }

/* —— 登录页 —— */
.login-body {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(16px, var(--safe-top)) max(16px, var(--safe-right)) max(16px, var(--safe-bottom)) max(16px, var(--safe-left));
}
.login-card {
  width: min(380px, 100%);
  background: var(--panel);
  padding: 28px;
  border-radius: 16px;
  border: 1px solid var(--border);
}
.login-card h1 { margin: 0 0 8px; font-size: 1.4rem; }
.muted { color: var(--muted); margin: 0 0 20px; }
label { display: grid; gap: 8px; font-size: .9rem; color: var(--muted); }
input, textarea, button {
  font: inherit; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel2); color: var(--text); padding: 10px 12px;
}
button {
  background: var(--accent); border-color: var(--accent2); cursor: pointer; font-weight: 600;
}
button:hover { filter: brightness(1.05); }
button:disabled { opacity: .65; cursor: wait; }
.login-card button { width: 100%; margin-top: 14px; min-height: 44px; }
.login-card input { min-height: 44px; width: 100%; }
.err { color: var(--err); margin-top: 12px; }

/* —— 聊天壳 —— */
.app-body {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: 1fr;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  position: relative;
}
.mobile-bar { display: none; }
.sidebar-backdrop { display: none; }

#sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  z-index: 30;
}
.side-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 14px 16px;
  padding-top: max(14px, var(--safe-top));
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.linkish {
  background: transparent; border: none; color: var(--muted); padding: 8px 10px;
  font-weight: 500; min-height: 44px;
}
.icon-btn {
  background: transparent; border: 1px solid var(--border); color: var(--text);
  width: 40px; height: 40px; min-width: 40px; padding: 0; display: inline-grid; place-items: center;
  border-radius: 10px; font-size: 1.15rem; line-height: 1;
}
#rooms {
  overflow: auto;
  padding: 8px;
  padding-bottom: max(8px, var(--safe-bottom));
  display: grid;
  gap: 4px;
  -webkit-overflow-scrolling: touch;
}
.room-btn {
  text-align: left; background: transparent; border: 1px solid transparent;
  padding: 12px 14px; color: var(--text); min-height: 44px; border-radius: 10px;
}
.room-btn.active, .room-btn:hover { background: var(--panel2); border-color: var(--border); }

#chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
}
#room-title {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  font-weight: 600;
  flex-shrink: 0;
}
#messages {
  overflow: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}
.msg {
  max-width: min(680px, 92%);
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--other);
  border: 1px solid var(--border);
  border-left: 3px solid var(--who, var(--border));
}
.msg.me { align-self: flex-end; }
.msg .meta { font-size: .75rem; color: var(--who, var(--muted)); margin-bottom: 4px; font-weight: 600; }
.msg[data-sender="xifan"] { --who: #e6b84d; background: #2c2718; }
.msg[data-sender="grok"] { --who: #5b8cff; background: #1a2740; }
.msg[data-sender="muse"] { --who: #3ecf8e; background: #173027; }
.msg[data-sender="contest"] { --who: #c084fc; background: #2a1e38; }
.msg[data-sender="games"] { --who: #2dd4bf; background: #163532; }
.msg[data-sender="codex"] { --who: #ff9f43; background: #322516; }
.msg[data-sender="antigravity"] { --who: #ff7ab8; background: #321c28; }
.msg .body { white-space: pre-wrap; word-break: break-word; line-height: 1.45; }
#composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
  padding: 12px 16px;
  padding-bottom: max(12px, calc(var(--safe-bottom) + var(--kb, 0px)));
  border-top: 1px solid var(--border);
  background: var(--panel);
  flex-shrink: 0;
}
#composer textarea {
  resize: none;
  min-height: 44px;
  max-height: 140px;
  line-height: 1.4;
  overflow-y: auto;
}
#composer button {
  min-height: 44px;
  min-width: 72px;
  padding: 10px 16px;
}

/* —— 平板窄屏（侧栏略收） —— */
@media (min-width: 721px) and (max-width: 980px) {
  :root { --sidebar-w: 220px; }
}

/* —— 手机：抽屉侧栏，主屏留给聊天 —— */
@media (max-width: 720px) {
  .app-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
  }
  .mobile-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    grid-column: 1;
    grid-row: 1;
    padding: 8px 12px;
    padding-top: max(8px, var(--safe-top));
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    z-index: 20;
    min-height: 52px;
  }
  .mobile-bar strong {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 1rem;
  }
  .mobile-bar-spacer { display: none; }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(86vw, 300px);
    max-width: 100%;
    border-right: 1px solid var(--border);
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: none;
    padding-left: var(--safe-left);
  }
  body.sidebar-open #sidebar {
    transform: translateX(0);
    box-shadow: 8px 0 28px rgba(0, 0, 0, .45);
  }
  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .48);
    z-index: 25;
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
  }
  body.sidebar-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }
  body.sidebar-open .sidebar-backdrop[hidden] {
    display: block;
  }

  #chat {
    grid-column: 1;
    grid-row: 2;
    min-height: 0;
  }
  /* 去掉与顶栏重复的房间标题 */
  #room-title { display: none; }

  #messages { padding: 12px; }
  .msg { max-width: 94%; }
  #composer {
    padding: 10px 12px;
    padding-bottom: max(10px, calc(var(--safe-bottom) + var(--kb, 0px)));
    gap: 8px;
  }
  #composer button { min-width: 64px; }
}

@media (max-width: 720px) and (max-height: 500px) and (orientation: landscape) {
  .mobile-bar { min-height: 44px; padding-top: 6px; padding-bottom: 6px; }
  #composer textarea { max-height: 72px; }
}

.load-older {
  display: block;
  width: 100%;
  margin: 0 0 10px;
  padding: 10px 12px;
  min-height: 44px;
  border: 1px solid #2a3340;
  border-radius: 10px;
  background: #1a212b;
  color: #9aa4b2;
  font: inherit;
  cursor: pointer;
}
.load-older:hover,
.load-older:focus {
  color: #e7ecf3;
  border-color: #3a4656;
}
.load-older:disabled {
  opacity: .6;
  cursor: default;
}
