* { box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --bg: #1e1f22;
  --panel: #2b2d31;
  --main: #313338;
  --hover: #3a3c42;
  --text: #dbdee1;
  --muted: #949ba4;
  --accent: #5865f2;
  --danger: #f23f43;
  --green: #23a55a;
}

body {
  margin: 0;
  height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button { font: inherit; cursor: pointer; }
input {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid #111;
  border-radius: 6px;
  padding: 10px 12px;
  width: 100%;
}
input:focus { outline: 2px solid var(--accent); border-color: transparent; }

/* ---------- Auth ---------- */
#auth { height: 100%; display: grid; place-items: center; padding: 16px; }
.card {
  background: var(--main);
  padding: 32px;
  border-radius: 10px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .4);
}
.card h1 { margin: 0; text-align: center; }
.subtitle { margin: -8px 0 4px; text-align: center; color: var(--muted); }
.card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); text-transform: uppercase; font-weight: 600; }
.tabs { display: flex; background: var(--bg); border-radius: 6px; padding: 4px; }
.tabs button { flex: 1; border: 0; background: none; color: var(--muted); padding: 8px; border-radius: 4px; }
.tabs button.active { background: var(--hover); color: var(--text); }
.primary { background: var(--accent); color: white; border: 0; padding: 11px; border-radius: 6px; font-weight: 600; }
.primary:hover { filter: brightness(1.1); }
.error { color: var(--danger); margin: 0; min-height: 1em; font-size: 14px; }

/* ---------- Chat layout ---------- */
#chat { display: flex; height: 100%; }
#sidebar, #members { background: var(--panel); width: 240px; flex-shrink: 0; display: flex; flex-direction: column; }
main { flex: 1; background: var(--main); display: flex; flex-direction: column; min-width: 0; }

.server-name, .channel-header {
  height: 48px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  font-weight: 700;
  border-bottom: 1px solid var(--bg);
  flex-shrink: 0;
}
.channel-header { color: var(--muted); }
.channel-header span { color: var(--text); margin-left: 4px; }

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 16px 6px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--muted);
}
.section-title button { background: none; border: 0; color: var(--muted); font-size: 18px; line-height: 1; }
.section-title button:hover { color: var(--text); }

ul { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; }
#channel-nav { flex: 1; overflow-y: auto; padding-bottom: 8px; }
#channel-list li, #voice-list > li, #dm-list li {
  padding: 6px 10px;
  border-radius: 4px;
  color: var(--muted);
  cursor: pointer;
  margin-bottom: 2px;
}
#channel-list li::before { content: "# "; opacity: .6; }
#channel-list li:hover, #voice-list > li:hover, #dm-list li:hover { background: var(--hover); color: var(--text); }
#channel-list li.active, #voice-list > li.active, #dm-list li.active { background: var(--hover); color: white; }

.me { display: flex; align-items: center; gap: 8px; padding: 10px 12px; background: #232428; }
.me span#me { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.me button { background: none; border: 0; color: var(--muted); font-size: 16px; }
.me button:hover { color: var(--danger); }

.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 700; color: white;
  flex-shrink: 0;
}

#member-list li { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 4px; }
#member-list li:hover { background: var(--hover); }
#member-list li.offline { opacity: .45; }
#member-list .avatar { position: relative; }
#member-list .avatar.is-online::after {
  content: ""; position: absolute; right: -2px; bottom: -2px;
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--green); border: 3px solid var(--panel);
}

/* ---------- Messages ---------- */
#messages { flex: 1; overflow-y: auto; padding: 16px 0; }
.msg { position: relative; padding: 2px 16px; margin-top: 14px; }
.msg.grouped { margin-top: 0; }
.msg:hover, .msg.show-actions { background: #2e3035; }
.msg.flash { animation: flash 1.5s; }
@keyframes flash { 0%, 40% { background: rgba(88, 101, 242, .3); } 100% { background: transparent; } }

.msg-main { display: flex; gap: 14px; }
.msg.grouped .msg-main { padding-left: 54px; }
.msg .avatar { width: 40px; height: 40px; margin-top: 2px; }
.msg-body { min-width: 0; flex: 1; }
.msg-head { display: flex; gap: 8px; align-items: baseline; }
.msg-author { font-weight: 600; }
.msg-time { font-size: 12px; color: var(--muted); }
.msg-text { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.4; }
.msg-text.big-emoji { font-size: 2.6em; line-height: 1.25; }
.msg-text.big-emoji .edited { font-size: 12px; }
.edited { font-size: 11px; color: var(--muted); }
.empty { color: var(--muted); text-align: center; margin-top: 40px; }

.reply-preview {
  margin-left: 20px; padding-left: 34px; margin-bottom: 2px;
  font-size: 13px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: pointer; position: relative;
}
.reply-preview:hover span { color: var(--text); }
.reply-preview b { font-weight: 600; }

/* Hover toolbar on a message */
.msg-actions {
  display: none;
  position: absolute; right: 16px; top: -16px;
  background: var(--panel); border: 1px solid var(--bg); border-radius: 6px;
  padding: 2px; gap: 2px; box-shadow: 0 2px 8px rgba(0,0,0,.3); z-index: 2;
}
.msg:hover .msg-actions, .msg.show-actions .msg-actions { display: flex; }
.msg-actions button {
  background: none; border: 0; border-radius: 4px; padding: 4px 6px;
  font-size: 16px; line-height: 1; color: var(--text);
}
.msg-actions button:hover { background: var(--hover); }
.msg-actions button.danger:hover { background: rgba(242, 63, 67, .25); }

/* Reactions */
.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction {
  display: flex; align-items: center; gap: 6px;
  background: #2b2d31; border: 1px solid transparent; border-radius: 8px;
  padding: 2px 8px; color: var(--text); font-size: 14px;
}
.reaction:hover { border-color: #4e5058; }
.reaction.mine { background: rgba(88, 101, 242, .2); border-color: var(--accent); }
.reaction-count { font-size: 13px; font-weight: 600; }

/* Edit box */
.edit-box input { background: #383a40; border: 0; margin-top: 2px; }
.edit-hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Banner (connection lost / kicked) */
#banner { background: var(--danger); color: white; padding: 8px 16px; font-size: 14px; font-weight: 600; }
#banner.clickable { cursor: pointer; background: var(--accent); }

#typing { height: 20px; padding: 0 16px; font-size: 13px; color: var(--muted); }

#reply-bar {
  display: flex; justify-content: space-between; align-items: center;
  margin: 0 16px; padding: 8px 12px;
  background: #2b2d31; border-radius: 8px 8px 0 0;
  font-size: 13px; color: var(--muted);
}
#reply-bar b { color: var(--text); }
#reply-bar button { background: none; border: 0; color: var(--muted); }
#reply-bar button:hover { color: var(--text); }
#reply-bar:not([hidden]) + #message-form #message-input { border-radius: 0 0 8px 8px; }

#message-form { padding: 0 16px 20px; position: relative; }
#message-input { background: #383a40; border: 0; padding: 12px 48px 12px 16px; border-radius: 8px; }
#emoji-button {
  position: absolute; right: 24px; top: 6px;
  background: none; border: 0; font-size: 22px; line-height: 1; padding: 4px;
  filter: grayscale(1); transition: filter .15s, transform .15s;
}
#emoji-button:hover { filter: none; transform: scale(1.15); }

/* Channels: admin delete button */
#channel-list li, #voice-list > li, #dm-list li { display: flex; align-items: center; }
#channel-list li::before { content: none; }
.channel-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.channel-label::before { content: "# "; opacity: .6; }
.channel-delete { display: none; background: none; border: 0; font-size: 13px; padding: 0 2px; opacity: .7; }
#channel-list li:hover .channel-delete { display: block; }
.channel-delete:hover { opacity: 1; }

/* Members */
.member-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crown { font-size: 13px; }
.member-more {
  background: none; border: 0; color: var(--muted); font-size: 18px; line-height: 1;
  padding: 0 4px; visibility: hidden;
}
#member-list li:hover .member-more { visibility: visible; }
.member-more:hover { color: var(--text); }

/* ---------- Popups ---------- */
#popup {
  position: fixed; z-index: 100;
  background: #111214; border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
}
.menu { display: flex; flex-direction: column; padding: 6px; min-width: 170px; }
.menu button {
  background: none; border: 0; color: var(--text); text-align: left;
  padding: 8px 10px; border-radius: 4px; font-size: 14px;
}
.menu button:hover { background: var(--accent); color: white; }
.menu button.danger { color: var(--danger); }
.menu button.danger:hover { background: var(--danger); color: white; }

.emoji-picker { width: 340px; max-width: calc(100vw - 16px); display: flex; flex-direction: column; }
.emoji-tabs { display: flex; gap: 2px; padding: 6px; border-bottom: 1px solid var(--panel); }
.emoji-tabs button {
  flex: 1; background: none; border: 0; border-radius: 4px; padding: 6px 0; font-size: 18px;
  filter: grayscale(1); opacity: .7;
}
.emoji-tabs button:hover, .emoji-tabs button.active { filter: none; opacity: 1; background: var(--panel); }
.emoji-title { font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--muted); padding: 8px 10px 4px; }
.emoji-grid {
  display: grid; grid-template-columns: repeat(8, 1fr);
  padding: 0 6px 8px; height: 230px; overflow-y: auto; align-content: start;
}
.emoji-grid button {
  background: none; border: 0; border-radius: 4px; font-size: 24px; padding: 4px 0; line-height: 1.2;
}
.emoji-grid button:hover { background: var(--panel); }

/* ---------- Voice channels + DMs ---------- */
#voice-list > li, #dm-list li { display: flex; align-items: center; gap: 8px; }
.voice-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.voice-label::before { content: "🔊 "; font-size: 13px; opacity: .8; }
#voice-list > li:hover .channel-delete { display: block; }

.voice-people { padding: 0 0 4px 26px; }
.voice-people li { display: flex; align-items: center; gap: 8px; padding: 3px 6px; font-size: 14px; color: var(--muted); }
.voice-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.voice-icon { font-size: 12px; }
.avatar.small { width: 22px; height: 22px; font-size: 11px; }
.voice-people li .avatar { transition: box-shadow .1s; }
.voice-people li.speaking .avatar { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--green); }
.voice-people li.speaking .voice-name { color: var(--text); }

.dm-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dm-call { font-size: 12px; }
.unread-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); flex-shrink: 0; }
#dm-list li:has(.unread-dot) .dm-name { color: white; font-weight: 600; }

/* Panel at the bottom of the sidebar while in a call */
#voice-panel {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; background: #232428; border-bottom: 1px solid var(--panel);
}
.voice-status { flex: 1; min-width: 0; }
.voice-connected { color: var(--green); font-weight: 700; font-size: 13px; white-space: nowrap; }
.voice-where { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.voice-buttons { display: flex; gap: 2px; }
.voice-buttons button {
  background: none; border: 0; border-radius: 4px; padding: 4px 5px; font-size: 16px; line-height: 1;
}
.voice-buttons button:hover { background: var(--hover); }
.voice-buttons button.off { background: rgba(242, 63, 67, .25); }
.voice-buttons button.leave { background: var(--danger); }
.voice-buttons button.leave:hover { filter: brightness(1.15); background: var(--danger); }

/* Call button in a DM header */
.channel-header { gap: 4px; }
#channel-prefix { color: var(--muted); }
#call-button {
  margin-left: auto; background: var(--green); color: white; border: 0;
  border-radius: 6px; padding: 6px 12px; font-weight: 600; font-size: 14px;
}
#call-button:hover { filter: brightness(1.1); }
#call-button.leave { background: var(--danger); }

/* Member list: clickable rows */
#member-list li.clickable { cursor: pointer; }
.menu-sep { height: 1px; background: var(--panel); margin: 4px 6px; }

/* Incoming call popup */
#call-toast {
  position: fixed; top: 20px; left: 50%; transform: translateX(-50%); z-index: 200;
  background: #111214; border-radius: 12px; padding: 16px 20px;
  display: flex; align-items: center; gap: 16px;
  box-shadow: 0 10px 40px rgba(0,0,0,.6); border: 1px solid var(--green);
  max-width: calc(100vw - 24px);
}
.call-ring { font-size: 28px; animation: ring 1s infinite; }
@keyframes ring { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-15deg); } 75% { transform: rotate(15deg); } }
.call-actions { display: flex; gap: 8px; }
.call-actions button { border: 0; border-radius: 6px; padding: 8px 16px; font-weight: 700; color: white; }
.call-actions .accept { background: var(--green); }
.call-actions .decline { background: var(--danger); }

#toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); z-index: 200;
  background: #111214; color: var(--text); padding: 10px 16px; border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,.5); font-size: 14px; max-width: calc(100vw - 24px);
}

/* ---------- Small screens ---------- */
@media (max-width: 800px) {
  #members { display: none; }
  #sidebar { width: 180px; }
}
@media (max-width: 520px) {
  #sidebar { width: 130px; }
  .me #me { display: none; }
  .msg-actions { right: 4px; }
}
