:root {
  --bg-tertiary: #1e1f22;
  --bg-secondary: #2b2d31;
  --bg-primary: #313338;
  --bg-input: #383a40;
  --bg-hover: #35373c;
  --bg-message-hover: #2e3035;
  --bg-selected: #404249;
  --text-normal: #dbdee1;
  --text-muted: #949ba4;
  --text-header: #f2f3f5;
  --text-link: #00a8fc;
  --accent: #5865f2;
  --danger: #f23f43;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg-tertiary);
  color: var(--text-normal);
  font: 15px/1.375 "gg sans", "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--text-link); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Each list (channels, messages) has its own scrollbar; keep the thumb visible against the dark panels. */
* { scrollbar-width: thin; scrollbar-color: #5c5f66 transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #5c5f66; border-radius: 5px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background-color: #80848e; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Layout ---------- */

#app {
  display: grid;
  grid-template-columns: 72px 240px 1fr;
  grid-template-rows: minmax(0, 1fr); /* columns scroll inside the window instead of growing the page */
  height: 100dvh;
}

/* ---------- Server rail ---------- */

#guilds {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 0;
  overflow-y: auto;
  scrollbar-width: none;
}

.guild {
  position: relative;
  width: 48px;
  height: 48px;
  flex: none;
  border: 0;
  padding: 0;
  border-radius: 50%;
  background: var(--bg-primary) center / cover no-repeat;
  color: var(--text-normal);
  font-weight: 600;
  transition: border-radius .15s, background-color .15s;
}
.guild:hover, .guild.active { border-radius: 16px; background-color: var(--accent); }
.guild::before {
  content: "";
  position: absolute;
  left: -12px;
  top: 50%;
  width: 4px;
  height: 0;
  border-radius: 0 4px 4px 0;
  background: var(--text-header);
  transform: translateY(-50%);
  transition: height .15s;
}
.guild:hover::before { height: 20px; }
.guild.active::before { height: 40px; }

/* ---------- Channel sidebar ---------- */

#sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-secondary);
}

#guild-name {
  height: 48px;
  flex: none;
  display: flex;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid var(--bg-tertiary);
  color: var(--text-header);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#channels { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 8px; }

.channel {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  margin: 1px 0;
  padding: 6px 8px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--text-muted);
  text-align: left;
}
.channel:hover { background: var(--bg-hover); color: var(--text-normal); }
.channel.active { background: var(--bg-selected); color: var(--text-header); }
.channel .icon { width: 20px; flex: none; text-align: center; opacity: .8; }
.channel .name { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.channel.thread { padding-left: 22px; font-size: 14px; }
.channel.deleted .name { text-decoration: line-through; opacity: .7; }
.channel .deleted-label {
  margin-left: auto;
  flex: none;
  padding: 0 5px;
  border-radius: 3px;
  background: var(--bg-tertiary);
  color: var(--text-muted);
  font-size: 10px;
  text-transform: uppercase;
}

.category {
  margin: 16px 0 4px;
  padding: 0 8px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
}

/* ---------- "Downloading now" markers ---------- */

@keyframes pulse { 50% { opacity: .35; } }

.channel .sync-dot {
  margin-left: auto;
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #23a55a;
  animation: pulse 1.2s ease-in-out infinite;
}
.channel.syncing .name { color: var(--text-normal); }

.guild.syncing::after {
  content: "";
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 12px;
  height: 12px;
  border: 3px solid var(--bg-tertiary);
  border-radius: 50%;
  background: #23a55a;
  animation: pulse 1.2s ease-in-out infinite;
}

#sync-panel {
  flex: none;
  max-height: 30%;
  overflow-y: auto;
  padding: 8px 10px;
  border-top: 1px solid var(--bg-tertiary);
  font-size: 12px;
}
#sync-panel h3 {
  margin: 0 0 4px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
}
.sync-item {
  display: block;
  width: 100%;
  padding: 3px 4px;
  border: 0;
  border-radius: 4px;
  background: none;
  text-align: left;
  color: var(--text-normal);
}
.sync-item:hover { background: var(--bg-hover); }
.sync-item .where, .sync-item .progress { color: var(--text-muted); }
.sync-item .progress { display: block; }

#status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: #232428;
  font-size: 12px;
  color: var(--text-muted);
}
#status { flex: 1; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
#logout {
  flex: none;
  border: 0;
  border-radius: 4px;
  padding: 4px 8px;
  background: var(--bg-input);
  font-size: 12px;
}
#logout:hover { background: var(--bg-selected); }

/* ---------- Chat ---------- */

#chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-primary);
}

#chat-header {
  height: 48px;
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--bg-tertiary);
}
#menu-btn { display: none; border: 0; background: none; font-size: 20px; padding: 0 4px; }
.titles { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 12px; white-space: nowrap; overflow: hidden; }
#chat-title { color: var(--text-header); font-weight: 600; }
#chat-topic { color: var(--text-muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; }

#search {
  width: 220px;
  height: 28px;
  border: 0;
  border-radius: 4px;
  padding: 0 8px;
  background: var(--bg-tertiary);
  font-size: 13px;
}
#search:focus { outline: none; width: 260px; }

#close-search {
  border: 0;
  border-radius: 4px;
  padding: 4px 10px;
  background: var(--bg-input);
  white-space: nowrap;
}

#messages { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding-bottom: 16px; }

#load-older {
  display: block;
  margin: 12px auto;
  border: 0;
  border-radius: 4px;
  padding: 6px 14px;
  background: var(--bg-input);
}
#load-older:hover { background: var(--bg-selected); }
#load-older[hidden] { display: none; }

#empty { padding: 32px 16px; color: var(--text-muted); text-align: center; }

.msg {
  position: relative;
  display: grid;
  grid-template-columns: 72px 1fr;
  padding: 2px 24px 2px 0;
}
.msg.first { margin-top: 16px; }
.msg:hover { background: var(--bg-message-hover); }

.avatar {
  grid-row: span 2;
  justify-self: center;
  width: 40px;
  height: 40px;
  margin-top: 2px;
  border-radius: 50%;
  background: var(--accent);
  object-fit: cover;
}
.hover-time {
  justify-self: center;
  align-self: start;
  padding-top: 3px;
  font-size: 11px;
  color: var(--text-muted);
  visibility: hidden;
}
.msg:hover .hover-time { visibility: visible; }

.meta { display: flex; align-items: baseline; flex-wrap: wrap; gap: 0 8px; }
.author { color: var(--text-header); font-weight: 600; }
.bot-tag {
  align-self: center;
  border-radius: 3px;
  padding: 0 4px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 600;
}
.where, .time { color: var(--text-muted); font-size: 12px; }

.content { white-space: pre-wrap; overflow-wrap: anywhere; }
.edited { color: var(--text-muted); font-size: 11px; margin-left: 4px; }
.deleted-tag { color: var(--danger); font-size: 11px; }

.embed {
  max-width: 520px;
  margin-top: 4px;
  border-left: 4px solid var(--bg-tertiary);
  border-radius: 4px;
  padding: 8px 12px;
  background: var(--bg-secondary);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 14px;
}

.attachments { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; margin-top: 4px; }
.attachments img, .attachments video {
  display: block;
  max-width: min(420px, 100%);
  max-height: 300px;
  border-radius: 6px;
  background: var(--bg-secondary);
}
.file {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--bg-tertiary);
  border-radius: 6px;
  padding: 10px 12px;
  background: var(--bg-secondary);
}
.file small { display: block; color: var(--text-muted); font-size: 11px; }

/* ---------- Composer ---------- */

#composer { padding: 0 16px 20px; flex: none; }
#composer[hidden] { display: none; }
#draft {
  display: block;
  width: 100%;
  max-height: 200px;
  min-height: 44px;
  resize: none;
  border: 0;
  border-radius: 8px;
  padding: 11px 16px;
  background: var(--bg-input);
}
#draft:focus { outline: none; }
#draft:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Login ---------- */

.login-page { display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(400px, 100%);
  padding: 32px;
  border-radius: 8px;
  background: var(--bg-primary);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .3);
}
.login-card h1 { margin: 0 0 4px; color: var(--text-header); font-size: 24px; text-align: center; }
.login-card p { margin: 0 0 20px; color: var(--text-muted); text-align: center; }
.login-card label { display: block; margin-bottom: 8px; font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); }
.login-card input {
  width: 100%;
  height: 40px;
  border: 0;
  border-radius: 4px;
  padding: 0 10px;
  background: var(--bg-tertiary);
}
.login-card input:focus { outline: 2px solid var(--accent); }
#error { min-height: 20px; margin: 8px 0; color: var(--danger); font-size: 14px; }
.login-card button {
  width: 100%;
  height: 44px;
  border: 0;
  border-radius: 4px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.login-card button:disabled { opacity: .6; }
.login-card[hidden] { display: none; }
.login-error { min-height: 20px; margin: 0 0 12px; color: var(--danger); font-size: 14px; text-align: center; }

.ms-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  height: 44px;
  border-radius: 4px;
  background: #fff;
  color: #1b1b1b;
  font-weight: 600;
}
.ms-button:hover { text-decoration: none; background: #f3f3f3; }
.ms-logo { display: grid; grid-template-columns: 9px 9px; gap: 2px; }
.ms-logo i { width: 9px; height: 9px; }
.ms-logo i:nth-child(1) { background: #f25022; }
.ms-logo i:nth-child(2) { background: #7fba00; }
.ms-logo i:nth-child(3) { background: #00a4ef; }
.ms-logo i:nth-child(4) { background: #ffb900; }

/* ---------- Signed-in user / admin link ---------- */

#user-name { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--text-normal); }
#user-bar { display: flex; align-items: center; gap: 8px; padding: 8px 10px 0; background: #232428; font-size: 12px; }
#user-bar[hidden] { display: none; }
#admin-link {
  flex: none;
  border-radius: 4px;
  padding: 4px 8px;
  background: var(--bg-input);
  color: var(--text-normal);
}
#admin-link:hover { background: var(--bg-selected); text-decoration: none; }
/* ---------- Search results → jump to message ---------- */

.msg.jumpable { cursor: pointer; }
.msg.jumpable:hover { background: var(--bg-hover); }
.msg.jumpable:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

@keyframes flash { from { background: rgba(250, 168, 26, .35); } to { background: transparent; } }
.msg.highlight { animation: flash 2.5s ease-out; box-shadow: inset 2px 0 0 #faa81a; }

#newer-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 16px 8px;
  padding: 6px 8px 6px 14px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
}
#newer-bar[hidden] { display: none; }
#newer-bar span { flex: 1; }
#newer-bar button {
  border: 0;
  border-radius: 4px;
  padding: 4px 10px;
  background: rgba(255, 255, 255, .18);
  color: #fff;
}
#newer-bar button:hover { background: rgba(255, 255, 255, .3); }

/* ---------- Admin page ---------- */

.admin-page { height: auto; min-height: 100%; background: var(--bg-primary); }
.admin { max-width: 1000px; margin: 0 auto; padding: 24px 16px 48px; }
.admin-header { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 8px 16px; }
.admin-header h1 { margin: 0; color: var(--text-header); font-size: 22px; }
.admin-help { color: var(--text-muted); max-width: 70ch; }
.admin-add { display: flex; gap: 8px; margin: 20px 0 4px; max-width: 520px; }
.admin-add input {
  flex: 1;
  min-width: 0;
  height: 38px;
  border: 0;
  border-radius: 4px;
  padding: 0 10px;
  background: var(--bg-tertiary);
}
.admin-add input:focus { outline: 2px solid var(--accent); }
.admin-add button, .remove {
  border: 0;
  border-radius: 4px;
  padding: 0 14px;
  height: 38px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.remove { height: 30px; background: var(--bg-input); color: var(--text-normal); font-weight: 400; }
.remove:hover { background: var(--danger); color: #fff; }
.admin-table-wrap { overflow-x: auto; border-radius: 8px; background: var(--bg-secondary); }
#users { width: 100%; border-collapse: collapse; }
#users th, #users td { padding: 10px 12px; border-bottom: 1px solid var(--bg-tertiary); text-align: left; vertical-align: middle; }
#users th { color: var(--text-muted); font-size: 12px; font-weight: 600; text-transform: uppercase; white-space: nowrap; }
#users tr:last-child td { border-bottom: 0; }
#users .center { text-align: center; }
#users input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; }
#users input[type=checkbox]:disabled { cursor: default; opacity: .6; }
.who-name { color: var(--text-header); font-weight: 600; }
.who-email, .who-seen { color: var(--text-muted); font-size: 12px; }
.badge {
  display: inline-block;
  margin-top: 4px;
  padding: 1px 6px;
  border-radius: 3px;
  background: #f0b232;
  color: #1e1f22;
  font-size: 11px;
  font-weight: 700;
}

#no-access { margin: 40px auto; max-width: 420px; padding: 0 16px; color: var(--text-muted); text-align: center; }

/* ---------- Phones / narrow windows ---------- */

#scrim { display: none; }

@media (max-width: 800px) {
  #app { grid-template-columns: 1fr; }
  #guilds, #sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    z-index: 20;
    transform: translateX(-320px);
    transition: transform .2s;
  }
  #guilds { left: 0; width: 72px; background: var(--bg-tertiary); }
  #sidebar { left: 72px; width: min(248px, calc(100vw - 72px)); }
  body.menu-open #guilds, body.menu-open #sidebar { transform: none; }
  body.menu-open #scrim { display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, .5); }
  #menu-btn { display: block; }
  #chat-header { padding: 0 10px; gap: 8px; }
  #chat-topic { display: none; }
  #search, #search:focus { width: 120px; }
  .msg { grid-template-columns: 56px 1fr; padding-right: 10px; }
  .avatar { width: 36px; height: 36px; }
  #composer { padding: 0 10px 12px; }
}
