:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1b1d21;
  --dim: #6b7280;
  --line: #e3e5e9;
  --accent: #3b6cf6;
  --accent-soft: #eaf0ff;
  --mine: #3b6cf6;
  --mine-text: #ffffff;
  --bubble: #ffffff;
  --danger: #d33a3a;
  --ok: #1f9d55;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --panel: #1a1d22;
    --text: #e7e9ee;
    --dim: #8b93a1;
    --line: #2a2e35;
    --accent: #6d8dff;
    --accent-soft: #1e2640;
    --mine: #3657cc;
    --bubble: #1d2026;
    --danger: #ff6b6b;
    --ok: #3ccf7a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.dim { color: var(--dim); }
.err { color: var(--danger); min-height: 1.5em; margin: 0; font-size: 14px; }

input[type="text"], input[type="password"] {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }

button.primary, .row button, .danger-zone button {
  height: 42px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  white-space: nowrap;
}
button.primary { background: var(--accent); border-color: transparent; color: #fff; font-weight: 600; }
button.ghost { border: 0; background: none; color: var(--dim); padding: 6px 10px; border-radius: 8px; }
button.ghost:hover { background: var(--accent-soft); color: var(--accent); }
button:disabled { opacity: .55; cursor: default; }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login form {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 32px 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login img { align-self: center; }
.login h1 { margin: 4px 0 0; font-size: 22px; }
.login p { margin: 0 0 8px; }

/* ---------- layout ---------- */
#app { display: flex; flex-direction: column; height: 100dvh; max-width: 880px; margin: 0 auto; }
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px 16px;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 17px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dim); display: inline-block; }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--danger); }

main { flex: 1; overflow-y: auto; padding: 8px 14px 12px; overscroll-behavior: contain; }
.load-more { display: block; margin: 8px auto; border: 0; background: none; color: var(--accent); font-size: 13px; }
.empty { text-align: center; margin-top: 28vh; }
.empty p { margin: 4px 0; }
.day { text-align: center; color: var(--dim); font-size: 12px; margin: 16px 0 6px; }

/* ---------- messages ---------- */
.msg { display: flex; flex-direction: column; align-items: flex-start; margin: 4px 0 10px; }
.msg.mine { align-items: flex-end; }
.meta { display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--dim); margin: 0 4px 3px; }
.tag { padding: 0 6px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); }
.bubble {
  max-width: min(80%, 580px);
  background: var(--bubble);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px 12px;
  overflow-wrap: anywhere;
}
.bubble.text { white-space: pre-wrap; }
.mine .bubble.text { background: var(--mine); color: var(--mine-text); border-color: transparent; }
.mine .bubble.text a { color: inherit; }
.bubble.text.clamp { max-height: 16.5em; overflow: hidden; -webkit-mask-image: linear-gradient(#000 70%, transparent); mask-image: linear-gradient(#000 70%, transparent); }
.bubble.media { padding: 0; overflow: hidden; display: block; line-height: 0; background: var(--line); }
.bubble.media img, .bubble.media video {
  display: block;
  max-width: 100%;
  max-height: 340px;
  height: auto;
}
.bubble.media img:not([style]) { width: auto; }
.bubble.media video { width: 100%; background: #000; }
.bubble.file { display: flex; gap: 10px; align-items: center; color: inherit; text-decoration: none; min-width: 230px; }
.bubble.file:hover { border-color: var(--accent); }
.ext {
  width: 42px; height: 42px; flex: none;
  border-radius: 10px;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .02em;
  overflow: hidden;
}
.fname { font-weight: 550; word-break: break-all; line-height: 1.35; }
.fsize { font-size: 12px; color: var(--dim); }
.caption { font-size: 12px; color: var(--dim); margin: 3px 4px 0; max-width: min(80%, 580px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
audio { max-width: 100%; }

.actions { display: flex; gap: 0; margin-left: 2px; }
.mine .meta { flex-direction: row; }
.act {
  font-size: 12px; color: var(--dim);
  background: none; border: 0;
  padding: 1px 6px; border-radius: 6px;
  text-decoration: none; line-height: 1.5;
}
.act:hover { background: var(--accent-soft); color: var(--accent); }
.act.danger, .act.danger:hover { color: var(--danger); }
.act.arm { background: var(--danger); color: #fff; }
@media (hover: hover) {
  .actions { opacity: 0; transition: opacity .15s; }
  .msg:hover .actions, .msg:focus-within .actions, .msg.pending .actions { opacity: 1; }
}

/* ---------- pending uploads ---------- */
.pending .bubble { min-width: 240px; }
.progress { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; margin-top: 8px; }
.progress > i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.pending.failed .progress > i { background: var(--danger); }
.pstatus { font-size: 12px; color: var(--dim); margin-top: 4px; }

/* ---------- composer ---------- */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 10px);
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1;
  resize: none;
  min-height: 42px;
  max-height: 40vh;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  outline: none;
  line-height: 1.5;
}
.icon-btn {
  width: 42px; height: 42px; flex: none;
  display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel);
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); }
.send {
  height: 42px; padding: 0 18px; flex: none;
  border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; font-weight: 600;
}

/* ---------- dialog ---------- */
dialog {
  width: min(540px, calc(100vw - 20px));
  max-height: calc(100dvh - 40px);
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--panel);
  color: var(--text);
  padding: 8px 20px 20px;
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: -8px; background: var(--panel); padding-top: 8px; }
.dlg-head h2 { margin: 0; font-size: 18px; }
dialog section { border-top: 1px solid var(--line); padding: 12px 0; }
dialog h3 { margin: 0 0 4px; font-size: 15px; }
dialog p { margin: 0 0 8px; font-size: 13px; }
.row { display: flex; gap: 8px; }
pre {
  background: var(--bg);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 12px;
  overflow-x: auto;
  margin: 10px 0 0;
  white-space: pre;
}
.danger-zone { display: flex; gap: 8px; flex-wrap: wrap; }
.danger-zone .danger { color: var(--danger); }

/* ---------- overlay & toast ---------- */
.drop-overlay {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  border: 3px dashed var(--accent);
  color: var(--accent); font-size: 20px; font-weight: 600;
  pointer-events: none;
}
.drop-overlay div { background: var(--panel); padding: 14px 24px; border-radius: 14px; }
.toast {
  position: fixed; left: 50%; z-index: 60;
  bottom: calc(env(safe-area-inset-bottom) + 84px);
  transform: translateX(-50%);
  background: rgb(24 26 31 / .92); color: #fff;
  padding: 8px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; transition: opacity .2s; pointer-events: none;
  max-width: calc(100vw - 40px);
}
.toast.show { opacity: 1; }
