:root {
  --bg: #ffffff;
  --bg-soft: #f6f5f2;
  --bg-hover: #f1efe9;
  --text: #1d2321;
  --muted: #69706d;
  --border: #e3e0d8;
  --side-bg: #1f3a38;
  --side-text: #c9d6d3;
  --side-strong: #ffffff;
  --side-hover: #2a4b48;
  --side-active: #d4a24c;
  --side-active-text: #1d2321;
  --top-bg: #18302e;
  --accent: #a8742a;
  --accent-strong: #8c5f1f;
  --accent-soft: #f6ead3;
  --mention-bg: #fbefd5;
  --mention-text: #7a5216;
  --danger: #b4523f;
  --online: #3f9d5a;
  --badge: #c2412d;
  --code-bg: #f1efe9;
  --shadow: 0 8px 28px rgba(20, 30, 28, .16);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1d1c; --bg-soft: #212524; --bg-hover: #262b29; --text: #e4e6e4; --muted: #9aa29f;
    --border: #333836; --side-bg: #132523; --side-hover: #1d3532; --top-bg: #0e1c1b;
    --accent: #d4a24c; --accent-strong: #e2b766; --accent-soft: #3a2f1c; --mention-bg: #3d3120; --mention-text: #f0cf8c;
    --code-bg: #262b29; --shadow: 0 8px 28px rgba(0, 0, 0, .5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1a1d1c; --bg-soft: #212524; --bg-hover: #262b29; --text: #e4e6e4; --muted: #9aa29f;
  --border: #333836; --side-bg: #132523; --side-hover: #1d3532; --top-bg: #0e1c1b;
  --accent: #d4a24c; --accent-strong: #e2b766; --accent-soft: #3a2f1c; --mention-bg: #3d3120; --mention-text: #f0cf8c;
  --code-bg: #262b29; --shadow: 0 8px 28px rgba(0, 0, 0, .5); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
a { color: var(--accent-strong); }

/* login */
.login { min-height: 100%; display: grid; place-items: center; background: var(--side-bg); padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--bg); border-radius: 14px; padding: 32px 28px; display: flex; flex-direction: column; gap: 12px; box-shadow: var(--shadow); }
.login-card h1 { margin: 4px 0 0; font-size: 26px; }
.login-card p { margin: 0 0 8px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); font-weight: 400; font-size: 15px; }
.error { color: var(--danger); min-height: 1em; font-size: 13px; margin: 0; }
.info { color: var(--online); font-size: 13px; margin: 0; }
.info:empty { display: none; }
.link.muted { color: var(--muted); font-size: 13px; }
.link { color: var(--accent-strong); font-size: 14px; }

.btn { padding: 8px 14px; border-radius: 8px; font-weight: 600; font-size: 14px; border: 1px solid var(--border); background: var(--bg); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; }
.btn.ghost:hover { background: var(--bg-hover); }
.btn.small { padding: 4px 8px; font-size: 13px; }
.icon-btn { width: 30px; height: 30px; border-radius: 6px; display: inline-grid; place-items: center; color: var(--muted); }
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

/* layout */
.app { height: 100%; display: grid; grid-template-columns: 260px 1fr auto; grid-template-rows: 44px 1fr; grid-template-areas: "top top top" "side main panel"; }
.topbar { grid-area: top; background: var(--top-bg); display: grid; grid-template-columns: 1fr minmax(200px, 640px) 1fr; align-items: center; padding: 0 12px; gap: 12px; }
.search input { width: 100%; padding: 6px 12px; border-radius: 6px; border: 1px solid rgba(255,255,255,.15); background: rgba(255,255,255,.1); color: #fff; }
.search input::placeholder { color: rgba(255,255,255,.6); }
.topbar-right { justify-self: end; }
.avatar-btn { padding: 0; border-radius: 6px; }

.sidebar { grid-area: side; background: var(--side-bg); color: var(--side-text); overflow-y: auto; padding-bottom: 16px; }
.ws-name { color: var(--side-strong); font-weight: 800; font-size: 17px; padding: 14px 16px; border-bottom: 1px solid rgba(255,255,255,.08); }
.side-nav { padding: 8px 0; }
.side-section { padding: 6px 0; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 16px; font-size: 13px; font-weight: 600; }
.side-actions button { color: var(--side-text); width: 24px; height: 24px; border-radius: 5px; font-size: 16px; }
.side-actions button:hover { background: var(--side-hover); color: #fff; }
.side-item { display: flex; align-items: center; gap: 8px; width: calc(100% - 16px); margin: 0 8px; padding: 4px 10px; border-radius: 6px; text-align: left; color: var(--side-text); font-size: 15px; min-height: 30px; }
.side-item:hover { background: var(--side-hover); }
.side-item.active { background: var(--side-active); color: var(--side-active-text); }
.side-item.unread { color: var(--side-strong); font-weight: 700; }
.side-item .ic { width: 18px; text-align: center; opacity: .85; flex: none; }
.side-item .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--badge); color: #fff; font-size: 12px; font-weight: 700; border-radius: 9px; padding: 0 6px; min-width: 18px; text-align: center; line-height: 18px; }
.dot { width: 9px; height: 9px; border-radius: 50%; border: 1.5px solid currentColor; flex: none; margin: 0 4px; }
.dot.on { background: var(--online); border-color: var(--online); }

.main { grid-area: main; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.conv-header { display: flex; align-items: center; gap: 8px; padding: 8px 16px; border-bottom: 1px solid var(--border); min-height: 54px; }
.menu-btn { display: none; font-size: 20px; }
.conv-title-wrap { flex: 1; min-width: 0; }
.conv-title { font-weight: 800; font-size: 17px; }
.conv-topic { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.conv-actions { display: flex; gap: 4px; }

.messages { flex: 1; overflow-y: auto; padding: 12px 0 8px; }
.day-sep { display: flex; align-items: center; gap: 12px; margin: 16px 20px 6px; color: var(--muted); font-size: 13px; font-weight: 600; }
.day-sep::before, .day-sep::after { content: ""; flex: 1; border-top: 1px solid var(--border); }
.new-sep { display: flex; align-items: center; gap: 8px; margin: 6px 20px; color: var(--badge); font-size: 12px; font-weight: 700; }
.new-sep::before { content: ""; flex: 1; border-top: 1px solid var(--badge); }
.empty { padding: 40px 24px; color: var(--muted); }
.empty h2 { color: var(--text); margin: 0 0 6px; font-size: 22px; }

.msg { position: relative; display: grid; grid-template-columns: 40px 1fr; gap: 0 10px; padding: 6px 20px; }
.msg.compact { padding-top: 1px; padding-bottom: 1px; }
.msg:hover, .msg.menu-open { background: var(--bg-soft); }
.msg.highlight { background: var(--accent-soft); transition: background 2s; }
.msg.pinned-msg { background: color-mix(in srgb, var(--accent-soft) 55%, transparent); }
.msg .time-side { font-size: 11px; color: var(--muted); text-align: right; padding-top: 3px; visibility: hidden; }
.msg.compact:hover .time-side { visibility: visible; }
.avatar { width: 36px; height: 36px; border-radius: 8px; color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 15px; flex: none; position: relative; }
.avatar.sm { width: 22px; height: 22px; border-radius: 5px; font-size: 11px; }
.avatar.xs { width: 20px; height: 20px; border-radius: 4px; font-size: 10px; }
.msg-head { display: flex; align-items: baseline; gap: 8px; }
.msg-author { font-weight: 800; cursor: pointer; }
.msg-author:hover { text-decoration: underline; }
.msg-time { font-size: 12px; color: var(--muted); }
.msg-text { word-wrap: break-word; overflow-wrap: anywhere; }
.msg-text p { margin: 0; }
.msg-text code { background: var(--code-bg); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; font-size: 13px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--danger); }
.msg-text pre { background: var(--code-bg); border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; margin: 4px 0; overflow-x: auto; font-size: 13px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; }
.msg-text blockquote { margin: 2px 0; padding-left: 10px; border-left: 4px solid var(--border); color: var(--muted); }
.mention { background: var(--mention-bg); color: var(--mention-text); border-radius: 4px; padding: 0 2px; font-weight: 600; }
.mention.me { background: var(--accent); color: #fff; }
.edited { color: var(--muted); font-size: 12px; }
.deleted-text { color: var(--muted); font-style: italic; }
.pin-label { font-size: 12px; color: var(--accent-strong); font-weight: 600; grid-column: 2; }

.files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.file-img { max-width: min(360px, 100%); max-height: 260px; border-radius: 8px; border: 1px solid var(--border); display: block; cursor: zoom-in; }
.file-card { display: flex; align-items: center; gap: 10px; border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; text-decoration: none; color: var(--text); max-width: 320px; background: var(--bg); }
.file-card .fi { width: 34px; height: 34px; border-radius: 6px; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 11px; font-weight: 700; flex: none; }
.file-card .fn { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-card .fs { font-size: 12px; color: var(--muted); }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction { display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--border); background: var(--bg-soft); border-radius: 12px; padding: 1px 8px; font-size: 13px; }
.reaction:hover { border-color: var(--muted); }
.reaction.mine { background: var(--accent-soft); border-color: var(--accent); }
.reaction .n { font-weight: 600; font-size: 12px; }
.add-react { color: var(--muted); }

.thread-link { display: flex; align-items: center; gap: 6px; margin-top: 4px; padding: 3px 6px; border-radius: 6px; border: 1px solid transparent; width: fit-content; font-size: 13px; }
.thread-link:hover { background: var(--bg); border-color: var(--border); }
.thread-link b { color: var(--accent-strong); }
.thread-link .muted { font-size: 12px; }

.msg-tools { position: absolute; top: -14px; right: 20px; display: none; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,.08); padding: 2px; z-index: 2; }
.msg:hover .msg-tools, .msg.menu-open .msg-tools { display: flex; }
.msg-tools button { width: 30px; height: 28px; border-radius: 6px; font-size: 15px; }
.msg-tools button:hover { background: var(--bg-hover); }

.edit-box textarea { width: 100%; border: 1px solid var(--accent); border-radius: 8px; padding: 8px; resize: vertical; min-height: 60px; background: var(--bg); }
.edit-box .row { display: flex; gap: 6px; justify-content: flex-end; margin-top: 4px; }

.typing { height: 20px; padding: 0 20px; font-size: 12px; color: var(--muted); }

.join-bar { margin: 0 20px 16px; padding: 14px; border: 1px solid var(--border); border-radius: 10px; display: flex; align-items: center; justify-content: space-between; gap: 12px; background: var(--bg-soft); }

/* composer */
.composer { margin: 0 20px 16px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); }
.composer:focus-within { border-color: var(--muted); box-shadow: 0 0 0 1px var(--muted); }
.panel .composer { margin: 0 14px 14px; }
.composer-tools { display: flex; gap: 2px; padding: 4px 6px 0; }
.composer-tools button { width: 28px; height: 26px; border-radius: 5px; color: var(--muted); font-size: 14px; }
.composer-tools button:hover { background: var(--bg-hover); color: var(--text); }
.composer textarea { display: block; width: 100%; border: 0; outline: 0; resize: none; padding: 8px 12px; max-height: 240px; min-height: 40px; background: transparent; }
.composer-bottom { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 6px; }
.composer-bottom .left { display: flex; gap: 2px; }
.composer-bottom .left button { width: 30px; height: 28px; border-radius: 6px; color: var(--muted); font-size: 16px; }
.composer-bottom .left button:hover { background: var(--bg-hover); color: var(--text); }
.send-btn { background: var(--accent); color: #fff; border-radius: 6px; width: 34px; height: 28px; font-size: 15px; }
.send-btn:disabled { background: var(--bg-hover); color: var(--muted); cursor: default; }
.pending-files { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 10px 0; }
.pending-file { display: flex; align-items: center; gap: 6px; border: 1px solid var(--border); border-radius: 8px; padding: 4px 8px; font-size: 13px; background: var(--bg-soft); }
.pending-file img { width: 34px; height: 34px; object-fit: cover; border-radius: 4px; }
.pending-file button { color: var(--muted); }
.composer.drag { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }

/* panel */
.panel { grid-area: panel; width: 400px; border-left: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); font-weight: 800; min-height: 54px; }
.panel-head .sub { font-weight: 400; color: var(--muted); font-size: 13px; }
.panel-body { flex: 1; overflow-y: auto; padding: 8px 0; }
.panel .msg { padding-left: 14px; padding-right: 14px; }
.panel .msg-tools { right: 14px; }
.reply-count { display: flex; align-items: center; gap: 8px; margin: 6px 14px; color: var(--muted); font-size: 13px; }
.reply-count::after { content: ""; flex: 1; border-top: 1px solid var(--border); }
.result-ctx { padding: 8px 14px 0; font-size: 12px; color: var(--muted); font-weight: 600; }
.result { cursor: pointer; border-bottom: 1px solid var(--border); }
.result:hover { background: var(--bg-soft); }

/* modal & popover */
.modal-back { position: fixed; inset: 0; background: rgba(10, 16, 15, .5); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--bg); border-radius: 12px; width: 100%; max-width: 520px; max-height: 85vh; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 22px 6px; }
.modal-head h2 { margin: 0; font-size: 21px; }
.modal-body { padding: 10px 22px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.modal-foot { padding: 12px 22px 18px; display: flex; justify-content: flex-end; gap: 8px; }
.field { display: flex; flex-direction: column; gap: 5px; font-weight: 600; font-size: 14px; }
.field input, .field textarea, .field select { padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); font-weight: 400; }
.field .hint { font-weight: 400; color: var(--muted); font-size: 13px; }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 14px; }
.list { display: flex; flex-direction: column; }
.list-row { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-radius: 8px; text-align: left; width: 100%; }
.list-row:hover { background: var(--bg-hover); }
.list-row .grow { flex: 1; min-width: 0; }
.list-row .sub { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; gap: 6px; align-items: center; background: var(--accent-soft); border-radius: 6px; padding: 2px 8px; font-size: 14px; }

.popover { position: fixed; z-index: 60; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 34px); gap: 2px; padding: 8px; }
.emoji-grid button { height: 34px; border-radius: 6px; font-size: 20px; }
.emoji-grid button:hover { background: var(--bg-hover); }
.menu { padding: 6px 0; min-width: 200px; }
.menu button { display: block; width: 100%; text-align: left; padding: 7px 16px; font-size: 14px; }
.menu button:hover { background: var(--accent); color: #fff; }
.menu button.danger { color: var(--danger); }
.menu button.danger:hover { background: var(--danger); color: #fff; }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 6px 0; }
.autocomplete { max-height: 240px; overflow-y: auto; min-width: 260px; padding: 4px; }
.ac-row { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; width: 100%; text-align: left; font-size: 14px; }
.ac-row.sel { background: var(--accent); color: #fff; }
.ac-row .muted { font-size: 13px; }
.ac-row.sel .muted { color: rgba(255,255,255,.8); }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 70; box-shadow: var(--shadow); }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 80; cursor: zoom-out; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 6px; }
.offline-bar { background: var(--danger); color: #fff; text-align: center; font-size: 13px; padding: 4px; }

@media (max-width: 1100px) {
  .panel { position: fixed; right: 0; top: 44px; bottom: 0; z-index: 30; box-shadow: var(--shadow); max-width: 100%; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "main"; }
  .topbar { grid-template-columns: 1fr auto; }
  .topbar-left { display: none; }
  .sidebar { position: fixed; top: 44px; bottom: 0; left: 0; width: 280px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; box-shadow: var(--shadow); }
  .menu-btn { display: block; }
  .panel { width: 100%; }
  .msg { padding-left: 16px; padding-right: 16px; }
  .composer { margin: 0 12px 12px; }
  .msg-tools { right: 12px; }
}
