:root {
  --bg: #1e1f22; --bg-2: #2b2d31; --bg-3: #313338; --bg-4: #383a40;
  --border: #3f4147; --text: #dbdee1; --muted: #949ba4;
  --accent: #5865f2; --accent-hover: #4752c4;
  --green: #23a559; --red: #da373c; --yellow: #f0b232;
  --self: #3b4a9e; --radius: 10px;
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg-3); color: var(--text); font-size: 16px; -webkit-text-size-adjust: 100%; }
a { color: #00a8fc; word-break: break-all; }
.muted { color: var(--muted); font-size: 13px; }
.hidden { display: none !important; }

button, input, select, textarea { font: inherit; color: inherit; }
.btn {
  border: 1px solid transparent; background: var(--bg-4); color: var(--text);
  padding: 10px 14px; border-radius: var(--radius); cursor: pointer; font-size: 15px;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.wide { width: 100%; }
.icon-btn {
  width: 40px; height: 40px; flex: 0 0 40px; border: 0; border-radius: 50%;
  background: var(--bg-4); color: var(--text); font-size: 20px; line-height: 1;
  cursor: pointer; display: grid; place-items: center;
}
.icon-btn:disabled { opacity: .4; cursor: not-allowed; }
.icon-btn.send { background: var(--accent); color: #fff; }
input, select, textarea {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px; font-size: 16px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---- login ---- */
.login-body { display: grid; place-items: center; background: var(--bg); padding: 16px; min-height: 100%; }
.login-card {
  width: 100%; max-width: 380px; background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px 22px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 19px; margin: 0; }
.brand-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px rgba(88,101,242,.25); }
.login-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card label input { width: 100%; }
.alert { margin-top: 14px; background: rgba(218,55,60,.15); border: 1px solid var(--red); color: #f5b4b6; padding: 10px 12px; border-radius: var(--radius); font-size: 14px; }

/* ---- app shell ---- */
.app {
  height: 100dvh; max-width: 760px; margin: 0 auto;
  display: flex; flex-direction: column; background: var(--bg-3);
  padding-top: env(safe-area-inset-top);
}
.top {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--bg-2); border-bottom: 1px solid var(--border);
}
.top select { flex: 1; min-width: 0; padding: 8px 10px; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: 0 0 12px; background: var(--muted); }
.dot.online { background: var(--green); }
.dot.offline { background: var(--red); }
.dot.pending { background: var(--yellow); }
.status-line { padding: 4px 14px; font-size: 12px; color: var(--muted); background: var(--bg-2); min-height: 20px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-line.warn { color: var(--yellow); }
.status-line.err { color: #f5b4b6; }

.menu {
  position: absolute; right: 12px; top: 56px; z-index: 5;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius);
  display: flex; flex-direction: column; min-width: 200px; overflow: hidden;
  box-shadow: 0 10px 30px rgba(0,0,0,.45);
}
.menu button { background: none; border: 0; text-align: left; padding: 12px 16px; cursor: pointer; width: 100%; }
.menu button:hover { background: var(--bg-4); }
.menu form { margin: 0; border-top: 1px solid var(--border); }

/* ---- messages ---- */
.messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; -webkit-overflow-scrolling: touch; }
.empty { margin: auto; color: var(--muted); text-align: center; padding: 20px; }
.msg { display: flex; flex-direction: column; max-width: 88%; align-self: flex-start; }
.msg.self { align-self: flex-end; align-items: flex-end; }
.msg .meta { font-size: 12px; color: var(--muted); margin: 0 6px 2px; display: flex; gap: 6px; align-items: baseline; }
.msg .meta b { color: var(--text); font-weight: 600; }
.msg .tag { font-size: 10px; padding: 0 5px; border-radius: 4px; background: var(--accent); color: #fff; }
.msg .bubble {
  background: var(--bg-4); padding: 9px 12px; border-radius: 14px;
  white-space: pre-wrap; word-break: break-word; line-height: 1.45; cursor: pointer;
}
.msg.self .bubble { background: var(--self); }
.msg .bubble:empty { display: none; }
.msg .reply-ref { font-size: 12px; color: var(--muted); border-left: 2px solid var(--border); padding-left: 6px; margin: 0 6px 3px; }
.msg .images { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.msg .images img { max-width: 100%; max-height: 240px; border-radius: 10px; display: block; background: var(--bg); }
.msg .files { margin-top: 4px; display: flex; flex-direction: column; gap: 3px; font-size: 14px; }
.msg .files a { background: var(--bg-2); padding: 6px 10px; border-radius: 8px; text-decoration: none; }
.msg .actions { display: none; gap: 8px; margin-top: 4px; }
.msg.open .actions { display: flex; }
.msg .actions button { background: var(--bg-2); border: 1px solid var(--border); border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; }
.msg .actions .danger { color: #f5b4b6; }

/* ---- composer ---- */
.reply-bar, .file-list {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 12px; background: var(--bg-2); border-top: 1px solid var(--border); font-size: 13px; color: var(--muted);
}
.reply-bar #reply-preview { color: var(--text); }
.reply-bar span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-bar .icon-btn { width: 30px; height: 30px; flex-basis: 30px; font-size: 14px; }
.file-list { flex-wrap: wrap; justify-content: flex-start; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  background: var(--bg-4); border-radius: 999px; padding: 4px 6px 4px 10px; color: var(--text);
}
.chip img { width: 24px; height: 24px; border-radius: 4px; object-fit: cover; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 180px; }
.chip button { border: 0; background: var(--bg-2); color: var(--text); border-radius: 50%; width: 22px; height: 22px; cursor: pointer; font-size: 12px; }
.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 10px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  background: var(--bg-2); border-top: 1px solid var(--border);
}
.composer textarea { flex: 1; min-width: 0; resize: none; max-height: 140px; min-height: 40px; padding: 9px 12px; border-radius: 20px; }
.composer textarea:disabled { opacity: .5; }

.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: 92vw; background: var(--bg-2); color: var(--text); border: 1px solid var(--border);
  padding: 10px 16px; border-radius: var(--radius); font-size: 14px; z-index: 10; box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
.toast.error { border-color: var(--red); }
.toast.ok { border-color: var(--green); }
