/* ==========================================================================
   Layout — the four-column application shell
   ========================================================================== */

.app {
  display: grid;
  grid-template-columns: var(--rail-w) var(--sidebar-w) minmax(0, 1fr) var(--members-w);
  grid-template-rows: minmax(0, 1fr) var(--userpanel-h);
  grid-template-areas:
    "rail sidebar chat members"
    "rail userpanel userpanel userpanel";
  height: 100vh;
  height: 100dvh;
  background: var(--bg-deep);
}

.app[data-members="closed"] {
  grid-template-columns: var(--rail-w) var(--sidebar-w) minmax(0, 1fr) 0;
}

.app[data-view="friends"] {
  grid-template-columns: var(--rail-w) minmax(0, 1fr) 0;
  grid-template-areas:
    "rail friends-view friends-view"
    "rail userpanel userpanel userpanel";
}

.app[data-view="friends"] .sidebar,
.app[data-view="friends"] .chat,
.app[data-view="friends"] .members {
  display: none;
}

.friends-view {
  grid-area: friends-view;
  background: var(--bg-base);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

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

.rail {
  grid-area: rail;
  background: var(--bg-deep);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 0;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: none;
}
.rail::-webkit-scrollbar {
  display: none;
}

.rail__item {
  position: relative;
  width: 48px;
  height: 48px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  transition: border-radius var(--dur) var(--ease), background-color var(--dur) var(--ease);
}

.rail__item::before {
  content: "";
  position: absolute;
  left: -12px;
  width: 4px;
  border-radius: 0 4px 4px 0;
  background: var(--text-normal);
  height: 0;
  transition: height var(--dur) var(--ease);
}

.rail__item:hover {
  border-radius: 14px;
}
.rail__item:hover::before,
.rail__item[data-active="true"]::before {
  height: 20px;
}
.rail__item[data-active="true"] {
  border-radius: 14px;
}
.rail__item[data-unread="true"]::before {
  height: 8px;
}
.rail__item[data-mention="true"]::before {
  height: 20px;
  background: var(--red);
}

.rail__glyph {
  width: 48px;
  height: 48px;
  border-radius: 16px;
  background: var(--brand);
  color: var(--text-on-brand);
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: var(--fs-md);
  overflow: hidden;
  user-select: none;
}

.rail__item[data-active="true"] .rail__glyph,
.rail__item:hover .rail__glyph {
  border-radius: 14px;
}

.rail__glyph img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.rail__divider {
  width: 32px;
  height: 2px;
  background: var(--border-strong);
  border-radius: 1px;
  flex: 0 0 auto;
}

.rail__badge {
  position: absolute;
  right: -4px;
  bottom: -4px;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--red);
  color: #fff;
  font-size: var(--fs-xs);
  font-weight: 700;
  display: grid;
  place-items: center;
  border: 3px solid var(--bg-deep);
}

/* ------------------------------- Sidebar ------------------------------- */

.sidebar {
  grid-area: sidebar;
  background: var(--bg-base);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.sidebar__header {
  height: var(--header-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 0 16px;
  font-weight: 600;
  font-size: var(--fs-md);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12);
  cursor: pointer;
}
.sidebar__header:hover {
  background: var(--bg-hover);
}
.sidebar__title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar__search {
  flex: 0 0 auto;
  padding: 10px 10px 4px;
}

.sidebar__body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 8px 16px;
}

.cat {
  margin-bottom: 16px;
}

.cat__header {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 0 4px;
  height: 24px;
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-faint);
  cursor: pointer;
  user-select: none;
}
.cat__header:hover {
  color: var(--text-normal);
}

.cat__chevron {
  width: 12px;
  height: 12px;
  flex: 0 0 auto;
  transition: transform var(--dur) var(--ease);
}
.cat[data-collapsed="true"] .cat__chevron {
  transform: rotate(-90deg);
}
.cat[data-collapsed="true"] .cat__list {
  display: none;
}

.cat__list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.chan {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 8px;
  margin: 1px 0;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  font-size: var(--fs-base);
}
.chan:hover {
  background: var(--bg-hover);
  color: var(--text-normal);
}
.chan[data-active="true"] {
  background: var(--bg-selected);
  color: var(--text-normal);
}
.chan[data-unread="true"] {
  color: var(--text-normal);
  font-weight: 600;
}
.chan[data-unread="true"]::before {
  content: "";
  position: absolute;
  left: 0;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-normal);
  margin-left: -8px;
}
.chan[data-mention="true"]::before {
  background: var(--red);
}

.chan__hash {
  flex: 0 0 auto;
  opacity: 0.7;
  font-size: var(--fs-lg);
  line-height: 1;
}
.chan__name {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chan__lock {
  flex: 0 0 auto;
  opacity: 0.6;
}

.chan__badge {
  min-width: 16px;
  height: 16px;
  padding: 0 5px;
  border-radius: 8px;
  background: var(--red);
  color: #fff;
  font-size: var(--fs-xs);
  font-weight: 700;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}

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

.chat {
  grid-area: chat;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--bg-raised);
  position: relative;
}

.chat__header {
  height: var(--header-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px 0 16px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12);
  font-weight: 600;
  z-index: 2;
}

.chat__title {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow: hidden;
}

.chat__topic {
  margin-left: 8px;
  padding-left: 8px;
  border-left: 1px solid var(--border-strong);
  font-size: var(--fs-sm);
  font-weight: 400;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat__actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 2px;
}

.chat__scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

.messages {
  --msg-gap: 12px;
  margin-top: auto;
  padding-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: var(--msg-gap);
}

.chat__intro {
  padding: 40px 16px 8px;
}

.chat__intro h2 {
  font-size: var(--fs-2xl);
  margin: 16px 0 8px;
}

.chat__intro p {
  color: var(--text-muted);
  margin: 0;
}

/* Jump-to-latest pill */
.jump {
  position: sticky;
  bottom: 8px;
  margin: 0 auto;
  width: fit-content;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--brand);
  color: var(--text-on-brand);
  font-size: var(--fs-sm);
  font-weight: 600;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 6px;
  z-index: 3;
}

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 16px 0;
  color: var(--text-faint);
  font-size: var(--fs-xs);
}
.divider::before,
.divider::after {
  content: "";
  height: 1px;
  background: var(--border-strong);
  flex: 1;
}

/* Unread divider */
.unread-divider {
  position: relative;
  height: 17px;
  margin: 8px 0;
}
.unread-divider::before {
  content: "";
  position: absolute;
  top: 8px;
  left: 0;
  right: 0;
  height: 1px;
  background: var(--red);
}
.unread-divider span {
  position: relative;
  top: -10px;
  margin-left: 16px;
  padding: 2px 4px;
  background: var(--red);
  color: #fff;
  font-size: var(--fs-xs);
  font-weight: 700;
  border-radius: 0 0 4px 0;
}

/* -------------------------------- Typing ------------------------------- */

.typing {
  height: 24px;
  flex: 0 0 auto;
  padding: 0 20px;
  font-size: var(--fs-sm);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

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

.composer {
  flex: 0 0 auto;
  padding: 0 16px 24px;
}

.composer__box {
  background: var(--bg-input);
  border-radius: var(--radius);
  display: flex;
  align-items: flex-end;
  gap: 4px;
  padding: 0 16px;
}

.composer__input {
  flex: 1 1 auto;
  background: none;
  border: 0;
  resize: none;
  outline: none;
  color: var(--text-normal);
  font-size: var(--fs-md);
  line-height: 1.375;
  padding: 12px 0;
  max-height: 50vh;
  overflow-y: auto;
  font-family: inherit;
}
.composer__input::placeholder {
  color: var(--text-faint);
}

.composer__replies {
  margin-bottom: 8px;
  background: var(--bg-raised);
  border-left: 4px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: var(--fs-sm);
  position: relative;
}

.composer__attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 8px 0;
}

/* ------------------------------ Members -------------------------------- */

.members {
  grid-area: members;
  background: var(--bg-base);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 8px;
  min-height: 0;
}
.app[data-members="closed"] .members {
  display: none;
}

.members__category {
  margin-bottom: 20px;
}

.members__header {
  padding: 0 8px 4px;
  font-size: var(--fs-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--text-faint);
}

.member {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 42px;
  padding: 0 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
}
.member:hover {
  background: var(--bg-hover);
}
.member[data-offline="true"] {
  opacity: 0.4;
}
.member__body {
  min-width: 0;
  flex: 1;
}
.member__name {
  font-size: var(--fs-base);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.member__sub {
  font-size: var(--fs-xs);
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ----------------------------- User panel ------------------------------ */

.userpanel {
  grid-area: userpanel;
  background: var(--bg-deep);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
}

.userpanel__identity {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  min-width: 0;
  height: 36px;
  padding: 0 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.userpanel__identity:hover {
  background: var(--bg-hover);
}

.userpanel__name {
  font-size: var(--fs-sm);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.userpanel__sub {
  font-size: var(--fs-xs);
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.userpanel__controls {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* ------------------------------ Responsive ----------------------------- */

@media (max-width: 1100px) {
  .app {
    grid-template-columns: var(--rail-w) var(--sidebar-w) minmax(0, 1fr) 0;
  }
  .app[data-members="open"] {
    grid-template-columns: var(--rail-w) var(--sidebar-w) minmax(0, 1fr) var(--members-w);
  }
}

@media (max-width: 760px) {
  .app {
    grid-template-columns: 0 0 minmax(0, 1fr) 0;
  }
  .rail,
  .sidebar {
    display: none;
  }
}
