:root {
  --bg: #f7f7f9;
  --panel: #ffffff;
  --sidebar: #f0f0f4;
  --text: #1c1c22;
  --muted: #6b6b78;
  --border: #e3e3ea;
  --accent: #5b5bd6;
  --accent-soft: #ececfb;
  --ai: #0f8f7e;
  --ai-soft: #e3f5f2;
  --danger: #c93a3a;
  --mention: #fff6dc;
  --code-bg: #f1f1f5;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 8px 24px rgba(0, 0, 0, .06);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15151a;
    --panel: #1c1c23;
    --sidebar: #111116;
    --text: #e8e8ee;
    --muted: #9696a5;
    --border: #2c2c36;
    --accent: #8b8bf0;
    --accent-soft: #2a2a48;
    --ai: #3cc7b3;
    --ai-soft: #173430;
    --danger: #f07070;
    --mention: #3a3320;
    --code-bg: #24242d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
code, pre { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .88em; }
.muted { color: var(--muted); }
.small { font-size: .85em; }
.pad { padding: 8px 12px; }
.error { color: var(--danger); }

/* ---------- buttons & inputs */
.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  font: inherit;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.full { width: 100%; text-align: left; }
.btn.danger { color: var(--danger); }
.small-btn { padding: 4px 10px; font-size: .85em; }
.icon-btn {
  border: 0; background: transparent; color: var(--muted);
  width: 30px; height: 30px; border-radius: 6px; cursor: pointer; font-size: 15px;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--text); }
input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.check { display: inline-flex; gap: 6px; align-items: center; }

.badge {
  display: inline-block; font-size: 11px; font-weight: 600; line-height: 1;
  padding: 3px 6px; border-radius: 5px; background: var(--accent-soft); color: var(--accent);
  vertical-align: middle;
}
.badge.ai { background: var(--ai-soft); color: var(--ai); }
.badge.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }

.avatar {
  --h: 240;
  flex: none; display: inline-grid; place-items: center;
  width: 36px; height: 36px; border-radius: 50%;
  background: hsl(var(--h) 55% 88%); color: hsl(var(--h) 45% 30%);
  font-weight: 700; font-size: 14px;
}
.avatar.ai { border-radius: 10px; background: hsl(var(--h) 50% 30%); color: hsl(var(--h) 80% 85%); }
.avatar.sm { width: 24px; height: 24px; font-size: 11px; margin-right: 8px; vertical-align: middle; }
@media (prefers-color-scheme: dark) {
  .avatar { background: hsl(var(--h) 35% 28%); color: hsl(var(--h) 70% 85%); }
  .avatar.ai { background: hsl(var(--h) 45% 22%); color: hsl(var(--h) 80% 75%); }
}

/* ---------- login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%); display: grid; gap: 10px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 32px 28px; box-shadow: var(--shadow); text-align: center;
}
.login-card label { text-align: left; font-weight: 600; font-size: .9em; margin-top: 8px; }
.login-card h1 { margin: 0; }
.login-card p { margin: 0; }
.logo { display: block; margin: 0 auto 4px; width: 64px; height: 64px; }

/* ---------- layout */
.app { display: grid; grid-template-columns: 250px minmax(0, 1fr); height: 100vh; height: 100dvh; }
.sidebar {
  background: var(--sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { display: flex; align-items: center; gap: 8px; padding: 16px; font-weight: 700; font-size: 17px; }
.small-logo { width: 28px; height: 28px; flex: none; }
.sidebar nav { flex: 1; overflow-y: auto; padding: 0 8px; }
.nav-label { text-transform: uppercase; font-size: 11px; letter-spacing: .06em; color: var(--muted); padding: 8px 8px 4px; font-weight: 600; }
.channel-list { list-style: none; margin: 0; padding: 0; }
.channel-link {
  display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 7px;
  color: var(--muted); text-decoration: none;
}
.channel-link:hover { background: var(--accent-soft); color: var(--text); }
.channel-link.active { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.channel-link.unread { color: var(--text); font-weight: 700; }
.channel-link.unread::after { content: ""; margin-left: auto; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.channel-link.archived { opacity: .55; }
.hash { width: 16px; text-align: center; opacity: .7; font-size: .9em; }
.ch-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-foot { padding: 10px; border-top: 1px solid var(--border); display: grid; gap: 4px; }
.me { display: flex; align-items: center; gap: 10px; padding: 8px 4px 0; }
.me .avatar { width: 30px; height: 30px; font-size: 12px; }
.me-name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat, .admin { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--panel); }
.chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 20px; border-bottom: 1px solid var(--border); min-height: 60px;
}
.chat-title { flex: 1; min-width: 0; }
.chat-title h2 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title p { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-btn { display: none; }

.participants {
  border-bottom: 1px solid var(--border); padding: 12px 20px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px;
  max-height: 40vh; overflow-y: auto; background: var(--bg);
}
.participant { display: flex; align-items: flex-start; }

/* ---------- messages */
.messages { flex: 1; overflow-y: auto; padding: 16px 0 8px; }
.empty { text-align: center; padding: 40px 16px; }
.msg { position: relative; display: flex; gap: 12px; padding: 6px 20px; }
.msg:hover { background: var(--bg); }
.msg.grouped { padding-top: 0; }
.msg.grouped .avatar { visibility: hidden; height: 0; }
.msg.grouped header { display: none; }
.msg.mentioned { background: var(--mention); }
.msg.flash { animation: flash 1.5s; }
@keyframes flash { from { background: var(--accent-soft); } }
.msg-body { flex: 1; min-width: 0; }
.msg header { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.msg time, .msg-id { color: var(--muted); font-size: 12px; }
.msg-id { opacity: 0; }
.msg:hover .msg-id { opacity: 1; }
.reply-ref {
  display: block; font-size: 12.5px; color: var(--muted); text-decoration: none;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-bottom: 2px;
}
.reply-ref:hover { color: var(--text); }
.content { overflow-wrap: anywhere; }
.content p { margin: 2px 0 6px; }
.content p:last-child { margin-bottom: 0; }
.content h3, .content h4, .content h5, .content h6 { margin: 10px 0 4px; font-size: 1em; }
.content h3 { font-size: 1.1em; }
.content ul, .content ol { margin: 4px 0 6px; padding-left: 22px; }
.content blockquote { margin: 4px 0; padding: 2px 10px; border-left: 3px solid var(--border); color: var(--muted); }
.content code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }
.content pre { background: var(--code-bg); padding: 10px 12px; border-radius: 8px; overflow-x: auto; margin: 6px 0; }
.content pre code { background: none; padding: 0; }
.content hr { border: 0; border-top: 1px solid var(--border); }
.mention { color: var(--accent); font-weight: 600; cursor: pointer; }
.msg-actions {
  position: absolute; right: 16px; top: -10px; display: none;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); padding: 2px;
}
.msg:hover .msg-actions { display: flex; }

/* ---------- composer */
.composer { padding: 8px 20px 16px; }
.reply-bar {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; color: var(--muted); padding: 2px 4px 6px;
}
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: 42px; max-height: 260px; line-height: 1.45; padding: 10px 12px; overflow-y: hidden; }
.composer .btn { height: 42px; }

/* ---------- admin */
.admin-body { flex: 1; overflow-y: auto; padding: 20px; display: grid; gap: 16px; align-content: start; }
.admin-body > div { display: grid; gap: 16px; }
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 3px; border-radius: 9px; }
.tab { border: 0; background: transparent; color: var(--muted); font: inherit; padding: 5px 14px; border-radius: 7px; cursor: pointer; }
.tab.active { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card h3 { margin: 0; font-size: 15px; }
.card h4 { margin: 14px 0 4px; font-size: 13px; }
.card.highlight { border-color: var(--accent); background: var(--accent-soft); }
.form-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.form-row h3 { width: 100%; }
.form-row input:not([type=checkbox]) { flex: 1 1 160px; }
.new-key-head { display: flex; justify-content: space-between; align-items: center; }
pre.copyable {
  background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; margin: 4px 0; overflow-x: auto; cursor: copy; white-space: pre-wrap; word-break: break-all;
}
pre.copyable:hover { border-color: var(--accent); }
pre.copyable.big { font-size: 1.05em; font-weight: 600; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.table td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tr.revoked { opacity: .5; }
.actions { display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; }
.channel-card { display: grid; gap: 10px; }
.channel-card p { margin: 0; }
.channel-card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; }
.access-grid { display: grid; gap: 6px; border-top: 1px solid var(--border); padding-top: 10px; }
.access-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 13px; padding: 5px 10px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent); color: #fff; }

/* ---------- dialog & toast */
.dialog {
  border: 1px solid var(--border); border-radius: 14px; background: var(--panel); color: var(--text);
  width: min(640px, calc(100% - 32px)); box-shadow: var(--shadow); padding: 20px 22px;
}
.dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog h3 { margin: 0 0 8px; }
.dialog h4 { margin: 14px 0 4px; font-size: 13px; }
.dialog-actions { display: flex; justify-content: flex-end; margin-top: 14px; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 8px; z-index: 100; box-shadow: var(--shadow);
}
.toast.error { background: var(--danger); color: #fff; }

/* ---------- mobile */
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 270px; z-index: 20;
    transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-block; }
  .chat-head { padding: 8px 12px; }
  .msg { padding: 6px 16px; }
  .composer { padding: 8px 16px 12px; }
  .admin-body { padding: 16px; }
  .table thead { display: none; }
  .table tr { display: grid; grid-template-columns: 1fr auto; padding: 8px 0; border-bottom: 1px solid var(--border); }
  .table td { border: 0; padding: 2px 4px; }
  .table td.actions-cell { grid-column: 1 / -1; }
  .table td.actions-cell .actions { justify-content: flex-start; }
  .chat-head .tabs { order: 3; }
  .admin .chat-head { flex-wrap: wrap; }
}

/* ---------- attachments */
.attach-btn { height: 42px; width: 38px; font-size: 18px; flex: none; }
.pending { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 0 0 8px; }
.pending-note { margin-left: 4px; }
.chip {
  position: relative; display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
  height: 56px; padding: 0 28px 0 10px; max-width: 220px;
}
.chip.img { width: 56px; padding: 0; overflow: hidden; }
.chip.img img { width: 100%; height: 100%; object-fit: cover; }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.chip-x {
  position: absolute; top: 2px; right: 2px; width: 20px; height: 20px; border: 0; border-radius: 50%;
  background: rgba(0, 0, 0, .55); color: #fff; font-size: 11px; cursor: pointer; line-height: 20px; padding: 0;
}
.composer.busy { opacity: .6; pointer-events: none; }
.chat.dropping .messages { outline: 2px dashed var(--accent); outline-offset: -10px; background: var(--accent-soft); }

.att-images { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px; margin: 6px 0 2px; max-width: 600px; }
.att-images a { display: block; aspect-ratio: 1; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); background: var(--bg); }
.att-images img { width: 100%; height: 100%; object-fit: cover; display: block; }
.att-images.single { display: block; }
.att-images.single a { aspect-ratio: auto; width: fit-content; max-width: min(100%, 420px); }
.att-images.single img { max-height: 320px; width: auto; max-width: 100%; height: auto; object-fit: contain; }
.att-files { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 2px; }
.att-file {
  display: inline-flex; align-items: center; gap: 8px; max-width: 100%;
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; text-decoration: none; color: var(--text); background: var(--bg);
}
.att-file:hover { border-color: var(--accent); }
.att-file.expired { color: var(--muted); border-style: dashed; }
.att-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }
