:root {
  --bg: #14161a;
  --bg-elev: #1b1e24;
  --bg-panel: #20232a;
  --bg-panel-2: #262a32;
  --bg-hover: #2c313b;
  --border: #2c313b;
  --text: #e8eaf0;
  --text-dim: #9aa1ad;
  --text-faint: #6b7280;
  --accent: #ff8a3d;
  --accent-2: #ff5f6d;
  --accent-grad: linear-gradient(135deg, #ff8a3d, #ff5f6d);
  --green: #3ba55d;
  --yellow: #f0b232;
  --red: #ed4245;
  --blue: #4f8cf7;
  --radius: 10px;
  --radius-sm: 7px;
  --rail-w: 68px;
  --sidebar-w: 250px;
  --members-w: 220px;
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --mono: "Cascadia Code", Consolas, "Courier New", monospace;
  scrollbar-color: #3a3f4a var(--bg);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  font-size: 14px;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea { font-family: inherit; }
.hidden { display: none !important; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #3a3f4a; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Buttons / inputs ---------- */
.btn {
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  background: var(--bg-panel-2);
  color: var(--text);
  transition: filter .15s, background .15s;
}
.btn:hover { background: var(--bg-hover); }
.btn-primary { background: var(--accent-grad); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-accent { background: var(--accent-grad); color: #fff; }
.btn-accent:hover { filter: brightness(1.08); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover { filter: brightness(1.1); }
.btn-danger-ghost { color: var(--red); }
.btn-block { width: 100%; }
.icon-btn {
  width: 30px; height: 30px;
  border-radius: var(--radius-sm);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 17px; color: var(--text-dim);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.mini-btn { color: var(--text-dim); font-size: 13px; padding: 2px 6px; border-radius: 5px; }
.mini-btn:hover { background: var(--bg-hover); color: var(--text); }
.badge {
  background: var(--accent); color: #fff; font-size: 10px; font-weight: 700;
  border-radius: 9px; padding: 1px 6px; margin-left: 4px;
}

/* ---------- Auth ---------- */
.auth-screen {
  height: 100vh; width: 100vw;
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(255,138,61,.12), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(255,95,109,.10), transparent 60%),
    var(--bg);
}
.auth-card {
  width: 380px; max-width: 92vw;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 30px 30px 26px;
  box-shadow: 0 24px 60px rgba(0,0,0,.5);
}
.auth-brand { text-align: center; margin-bottom: 20px; }
.auth-logo {
  width: 58px; height: 58px; margin: 0 auto 12px;
  border-radius: 16px;
  background: var(--accent-grad);
  display: flex; align-items: center; justify-content: center;
  font-size: 32px; font-weight: 800; color: #fff;
}
.auth-brand h1 { font-size: 26px; letter-spacing: .5px; }
.auth-brand p { color: var(--text-dim); font-size: 13px; margin-top: 4px; }
.auth-tabs { display: flex; background: var(--bg-panel); border-radius: 9px; padding: 4px; margin-bottom: 18px; }
.auth-tab { flex: 1; padding: 8px; border-radius: 6px; font-weight: 600; color: var(--text-dim); }
.auth-tab.active { background: var(--bg-panel-2); color: var(--text); }
.auth-card label { display: block; font-size: 12px; color: var(--text-dim); margin: 14px 0 6px; }
.auth-card input[type="text"], .auth-card input[type="password"] {
  width: 100%; padding: 10px 12px;
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 14px;
}
.auth-card input:focus { outline: none; border-color: var(--accent); }
.auth-row { display: flex; align-items: center; margin-top: 12px; }
.checkbox { display: flex; align-items: center; gap: 8px; font-size: 13px !important; color: var(--text-dim) !important; margin: 0 !important; cursor: pointer; }
.checkbox input { accent-color: var(--accent); }
.auth-error { color: var(--red); font-size: 13px; min-height: 18px; margin-top: 12px; }
#auth-submit { margin-top: 8px; }

/* ---------- App shell ---------- */
.app { display: flex; height: 100vh; width: 100vw; }

/* Server rail */
.server-rail {
  width: var(--rail-w);
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; align-items: center;
  padding: 10px 0; gap: 8px;
}
.rail-item {
  width: 46px; height: 46px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 17px; font-weight: 700;
  background: var(--bg-panel); color: var(--text-dim);
  transition: border-radius .15s, background .15s, color .15s;
  position: relative;
}
.rail-item:hover { border-radius: 14px; background: var(--accent-grad); color: #fff; }
.rail-item.active { border-radius: 14px; background: var(--accent-grad); color: #fff; }
.rail-item .rail-badge {
  position: absolute; top: -4px; right: -4px;
  background: var(--red); color: #fff; font-size: 10px; font-weight: 700;
  border-radius: 10px; padding: 0 5px;
}
.rail-home { background: var(--accent-grad); color: #fff; }
.rail-home .rail-logo {
  position: absolute; inset: 0; width: 100%; height: 100%;
  border-radius: inherit; object-fit: cover;
}
.rail-home .rail-icon { position: relative; z-index: 0; }
.rail-servers { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1; padding: 2px; }
.rail-servers .rail-item { background: var(--bg-panel); }
.rail-servers .rail-item .rail-icon { font-size: 15px; }
.rail-servers .rail-item img { width: 100%; height: 100%; border-radius: inherit; object-fit: cover; }
.rail-add { background: var(--bg-panel); font-size: 22px; }
.rail-add:hover { background: var(--green); }

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px; border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.sidebar-title { font-weight: 700; font-size: 15px; }
.sidebar-actions { display: flex; gap: 4px; }
.sidebar-body { flex: 1; overflow-y: auto; padding: 10px 8px; }
.sidebar-group-header {
  display: flex; align-items: center; justify-content: space-between;
  color: var(--text-faint); font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .4px; margin: 8px 6px;
}
.dm-list { display: flex; flex-direction: column; gap: 2px; }
.dm-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  color: var(--text-dim); position: relative;
}
.dm-item:hover { background: var(--bg-hover); color: var(--text); }
.dm-item.active { background: var(--bg-panel-2); color: var(--text); }
.dm-item .avatar { flex: 0 0 auto; }
.dm-item .dm-meta { flex: 1; min-width: 0; }
.dm-item .dm-name { font-weight: 600; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dm-item .dm-preview { font-size: 12px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dm-item .dm-unread {
  flex: 0 0 auto; min-width: 20px; text-align: center;
  background: var(--red); color: #fff; font-size: 11px; font-weight: 700;
  border-radius: 10px; padding: 1px 6px;
}
.dm-item.unread .dm-name { color: var(--text); }

/* Avatars */
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--bg-panel-2);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px;
  position: relative; overflow: hidden;
  flex: 0 0 auto;
  color: var(--text-dim);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-sm { width: 28px; height: 28px; font-size: 12px; }
.avatar-xs { width: 22px; height: 22px; font-size: 10px; }
.presence-dot {
  position: absolute; bottom: -1px; right: -1px;
  width: 11px; height: 11px; border-radius: 50%;
  background: var(--bg); border: 2px solid var(--bg);
}
.presence-dot.online { background: var(--green); }
.presence-dot.afk { background: var(--yellow); }
.presence-dot.dnd { background: var(--red); }
.presence-dot.invisible { background: var(--text-faint); }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg-panel); }
.chat-header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px; border-bottom: 1px solid var(--border);
  flex: 0 0 auto; background: var(--bg-panel);
}
.chat-head-left { display: flex; align-items: center; gap: 8px; }
.chat-hash { color: var(--text-faint); font-size: 20px; }
.chat-header h2 { font-size: 16px; font-weight: 700; }
.chat-subtitle { color: var(--text-dim); font-size: 12.5px; flex: 1; }
.chat-head-actions { display: flex; gap: 4px; }

.chat-scroll { flex: 1; overflow-y: auto; padding: 18px 22px 8px; }
.chat-messages { display: flex; flex-direction: column; }
.message {
  display: flex; gap: 12px; padding: 4px 8px;
  border-radius: 6px; margin-top: 2px;
}
.message:hover { background: rgba(255,255,255,.025); }
.message.compact { margin-top: 0; padding-top: 1px; padding-bottom: 1px; }
.message.compact > .avatar { visibility: hidden; }
.message.compact .msg-meta { display: none; }
.message.compact .msg-actions { top: 4px; position: relative; }
.message .msg-main { flex: 1; min-width: 0; }
.message .msg-meta { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.message .msg-author { font-weight: 700; font-size: 14px; }
.message .msg-time { font-size: 11px; color: var(--text-faint); }
.message .msg-content { font-size: 14.5px; line-height: 1.45; white-space: pre-wrap; word-break: break-word; }
.message .msg-content a { color: var(--accent); text-decoration: none; }
.message .msg-content a:hover { text-decoration: underline; }
.message .msg-edited { color: var(--text-faint); font-size: 11px; margin-left: 6px; }
.message .msg-actions {
  opacity: 0; display: flex; gap: 4px; margin-left: auto; padding-left: 8px;
}
.message:hover .msg-actions { opacity: 1; }
.msg-action { font-size: 12px; color: var(--text-dim); padding: 3px 6px; border-radius: 5px; }
.msg-action:hover { background: var(--bg-hover); color: var(--text); }
.reply-chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--bg-panel-2); border-left: 3px solid var(--accent);
  border-radius: 5px; padding: 4px 10px; margin-bottom: 6px;
  font-size: 12.5px; color: var(--text-dim); max-width: 100%;
  cursor: pointer; overflow: hidden;
}
.reply-chip:hover { background: var(--bg-hover); }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.attachment {
  background: var(--bg-panel-2); border-radius: 8px; overflow: hidden;
  max-width: 340px; border: 1px solid var(--border);
}
.attachment img { max-width: 340px; max-height: 260px; display: block; cursor: zoom-in; }
.attachment video { max-width: 340px; max-height: 260px; display: block; }
.attachment audio { width: 320px; max-width: 90%; padding: 10px; }
.attachment .file-body {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  font-size: 13px; min-width: 200px;
}
.attachment .file-body a { color: var(--text); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment .file-body a:hover { color: var(--accent); }
.attachment .file-ico { font-size: 26px; }
.reactions { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px; }
.reaction {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--bg-panel-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 2px 9px; font-size: 13px; color: var(--text-dim);
}
.reaction:hover { border-color: var(--accent); }
.reaction.me { background: rgba(255,138,61,.18); border-color: var(--accent); color: var(--text); }
.new-messages-divider {
  display: flex; align-items: center; gap: 10px;
  color: var(--accent); font-size: 12px; font-weight: 700;
  margin: 14px 0; text-transform: uppercase; letter-spacing: .4px;
}
.new-messages-divider::after { content: ""; flex: 1; height: 1px; background: var(--accent); opacity: .5; }
.system-note { color: var(--text-faint); font-size: 12px; text-align: center; margin: 12px 0; }
.load-more-btn {
  display: block; margin: 8px auto; padding: 6px 16px;
  background: var(--bg-panel-2); border-radius: 8px; color: var(--text-dim); font-size: 13px;
}
.load-more-btn:hover { color: var(--text); background: var(--bg-hover); }
.typing-indicator { color: var(--text-dim); font-size: 12.5px; padding: 6px 10px; }

/* Composer */
.composer { padding: 6px 18px 16px; background: var(--bg-panel); }
.reply-bar {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg-panel-2); border-radius: 8px 8px 0 0;
  padding: 8px 14px; margin-top: 8px;
}
.reply-meta { font-size: 12px; color: var(--text-faint); }
.reply-meta b { color: var(--text-dim); }
.reply-preview { flex: 1; font-size: 12.5px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-preview { display: flex; gap: 10px; flex-wrap: wrap; padding: 10px 4px 4px; }
.attach-thumb {
  position: relative; width: 84px; height: 84px; border-radius: 8px; overflow: hidden;
  background: var(--bg-panel-2); border: 1px solid var(--border);
}
.attach-thumb img, .attach-thumb video { width: 100%; height: 100%; object-fit: cover; }
.attach-thumb .file { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; font-size: 11px; color: var(--text-dim); height: 100%; }
.attach-thumb .rm { position: absolute; top: 3px; right: 3px; background: rgba(0,0,0,.65); color: #fff; border-radius: 50%; width: 20px; height: 20px; font-size: 13px; }
.composer-row { display: flex; align-items: flex-end; gap: 10px; }
.composer-input-wrap { flex: 1; }
.composer-input-wrap textarea {
  width: 100%; resize: none; overflow-y: auto; max-height: 180px;
  background: var(--bg-panel-2); border: 1px solid var(--border); border-radius: 12px;
  color: var(--text); padding: 11px 14px; font-size: 14.5px; line-height: 1.4;
}
.composer-input-wrap textarea:focus { outline: none; border-color: var(--accent); }
.send-btn { padding: 11px 20px; border-radius: 12px; }
#btn-attach, #btn-emoji { width: 36px; height: 36px; }

/* Members panel */
.members-panel {
  width: var(--members-w);
  background: var(--bg-elev);
  border-left: 1px solid var(--border);
  overflow-y: auto;
}
.members-title {
  padding: 16px 14px 10px; font-size: 12px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .4px; color: var(--text-faint);
}
.members-list { padding: 0 8px 12px; display: flex; flex-direction: column; gap: 2px; }
.member-item {
  display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: 8px;
}
.member-item:hover { background: var(--bg-hover); }
.member-item .member-name { font-size: 13.5px; font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-item .member-role { font-size: 10px; color: var(--accent); font-weight: 700; text-transform: uppercase; }
.member-item .member-status { font-size: 11.5px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 90px; }

/* Modals */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(8,9,12,.72);
  display: flex; align-items: center; justify-content: center; z-index: 50;
  animation: fadeIn .12s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal {
  width: 440px; max-width: 92vw; max-height: 86vh; overflow-y: auto;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; padding: 22px;
  box-shadow: 0 24px 70px rgba(0,0,0,.6);
  animation: popIn .14s ease;
}
@keyframes popIn { from { transform: scale(.96); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal h3 { font-size: 17px; margin-bottom: 16px; }
.modal label { display: block; font-size: 12px; color: var(--text-dim); margin: 12px 0 6px; }
.modal input[type="text"], .modal input[type="password"], .modal textarea, .modal select {
  width: 100%; padding: 9px 11px;
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 14px;
}
.modal input:focus, .modal textarea:focus { outline: none; border-color: var(--accent); }
.modal textarea { resize: vertical; min-height: 60px; }
.modal-row { display: flex; gap: 10px; margin-top: 18px; justify-content: flex-end; }
.modal-list { display: flex; flex-direction: column; gap: 6px; max-height: 260px; overflow-y: auto; }
.modal-list .item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; background: var(--bg-panel); }
.modal-list .item:hover { background: var(--bg-hover); }
.modal .spacer { flex: 1; }
.modal-note { font-size: 12.5px; color: var(--text-dim); margin-top: 8px; }
.danger-zone { border-top: 1px solid var(--border); margin-top: 18px; padding-top: 14px; }
.avatar-preview-lg {
  width: 72px; height: 72px; border-radius: 50%; margin-bottom: 10px;
  background: var(--bg-panel-2); display: flex; align-items: center; justify-content: center;
  font-size: 28px; overflow: hidden; font-weight: 700;
}
.avatar-preview-lg img { width: 100%; height: 100%; object-fit: cover; }
.presence-select { display: flex; gap: 8px; flex-wrap: wrap; }
.presence-select button {
  padding: 7px 12px; border-radius: 8px; background: var(--bg-panel);
  color: var(--text-dim); font-size: 13px; border: 1px solid var(--border);
}
.presence-select button.active { border-color: var(--accent); color: var(--text); background: rgba(255,138,61,.12); }

/* Toasts */
.toast-root { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 90; }
.toast {
  background: var(--bg-panel-2); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 10px; padding: 12px 16px; min-width: 240px; max-width: 360px;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
  animation: slideIn .18s ease;
}
@keyframes slideIn { from { transform: translateX(30px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.toast h4 { font-size: 13.5px; margin-bottom: 2px; }
.toast p { font-size: 12.5px; color: var(--text-dim); }
.toast.error { border-left-color: var(--red); }
.toast.success { border-left-color: var(--green); }

/* Image viewer */
.image-viewer {
  position: fixed; inset: 0; background: rgba(5,6,8,.92); z-index: 80;
  display: flex; align-items: center; justify-content: center;
}
.image-viewer img { max-width: 92vw; max-height: 92vh; border-radius: 8px; }
#image-close { position: absolute; top: 18px; right: 18px; width: 40px; height: 40px; font-size: 22px; background: rgba(255,255,255,.08); color: #fff; border-radius: 50%; }

/* Emoji picker */
.emoji-picker {
  position: fixed; z-index: 70;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px;
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px;
  box-shadow: 0 16px 50px rgba(0,0,0,.55);
  max-height: 300px; overflow-y: auto;
}
.emoji-picker button { font-size: 20px; padding: 4px; border-radius: 6px; }
.emoji-picker button:hover { background: var(--bg-hover); }

/* Dropdown / context menu */
.ctx-menu {
  position: fixed; z-index: 75; min-width: 170px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 10px; padding: 5px; box-shadow: 0 12px 40px rgba(0,0,0,.5);
  animation: popIn .1s ease;
}
.ctx-menu button {
  display: flex; width: 100%; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 7px; font-size: 13.5px; color: var(--text-dim); text-align: left;
}
.ctx-menu button:hover { background: var(--bg-hover); color: var(--text); }
.ctx-menu button.danger { color: var(--red); }
.ctx-menu button.danger:hover { background: rgba(237,66,69,.15); }

/* Misc */
.empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: var(--text-faint); gap: 10px; }
.empty-state .big { font-size: 40px; opacity: .5; }
.search-result-item { display: flex; gap: 12px; padding: 10px; border-radius: 10px; cursor: pointer; }
.search-result-item:hover { background: var(--bg-hover); }
.search-result-item .jump { margin-left: auto; color: var(--accent); font-size: 12px; align-self: center; }

@media (max-width: 900px) {
  .members-panel { display: none; }
  .sidebar { width: 210px; }
}