/* ==========================================================================
   CHAT
   ========================================================================== */
.chat {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 0;
  margin: 34px 0 80px;
  height: min(72vh, 720px);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: var(--radius);
  overflow: hidden;
}
.chat[hidden] { display: none; }

/* ---------------------------------------------------------------- sidebar */
.chat__side {
  display: flex; flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--glass-line);
  background: rgba(255, 255, 255, 0.015);
}
.chat__sidehead {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--glass-line);
}
.chat__sidetitle {
  font-size: 12px; letter-spacing: .08em; text-transform: uppercase;
  color: var(--fg-faint);
}
.chat__new {
  width: 28px; height: 28px;
  display: grid; place-items: center;
  font: inherit; font-size: 18px; line-height: 1;
  color: var(--fg-dim);
  background: none;
  border: 1px solid var(--glass-line-2);
  border-radius: 50%;
  cursor: pointer;
}
.chat__new:hover { color: var(--fg); border-color: var(--fg-dim); }
.chat__new:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.chat__list { flex: 1; min-height: 0; overflow-y: auto; padding: 6px; }

.chat__item {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 9px 10px;
  font: inherit; text-align: left;
  color: var(--fg-dim);
  background: none; border: 0; border-radius: 10px;
  cursor: pointer;
}
.chat__item:hover { background: var(--glass-2); color: var(--fg); }
.chat__item.is-active { background: var(--glass-2); color: var(--fg); }
.chat__item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.chat__ico {
  flex: 0 0 34px;
  width: 34px; height: 34px;
  display: grid; place-items: center;
  font-size: 15px;
  color: var(--fg-faint);
  background: var(--glass-2);
  border-radius: 50%;
  object-fit: cover;
}
.chat__ico--ph {
  font-weight: 600; font-size: 14px;
  color: var(--fg-dim);
  text-transform: uppercase;
}

.chat__itemtext {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; gap: 1px;
}
.chat__itemtext strong {
  font-weight: 500; font-size: 13.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat__itemwhen { font-size: 11px; color: var(--fg-faint); }

.chat__badge {
  flex: 0 0 auto;
  min-width: 20px;
  padding: 2px 6px;
  font-size: 11px; font-weight: 600; text-align: center;
  color: #fff;
  background: var(--accent);
  border-radius: 999px;
}

.chat__emptylist {
  padding: 18px 14px;
  font-size: 13px;
  color: var(--fg-faint);
}

/* ------------------------------------------------------------------- main */
.chat__main {
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0;
}

.chat__head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--glass-line);
}
.chat__peer {
  width: 34px; height: 34px;
  border-radius: 50%; object-fit: cover;
  border: 1px solid var(--glass-line-2);
}
.chat__peer[hidden] { display: none; }
.chat__headtext { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.chat__headtext strong {
  font-size: 14px; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat__sub { font-size: 11.5px; color: var(--fg-faint); }
.chat__back {
  display: none;
  padding: 0 8px 0 0;
  font: inherit; font-size: 22px; line-height: 1;
  color: var(--fg-dim);
  background: none; border: 0; cursor: pointer;
}

.chat__scroll {
  flex: 1; min-height: 0;
  overflow-y: auto;
  padding: 16px;
  display: flex; flex-direction: column;
  overscroll-behavior: contain;
}
.chat__more {
  align-self: center;
  margin-bottom: 14px;
  padding: 6px 14px;
  font: inherit; font-size: 12px;
  color: var(--fg-dim);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: 999px;
  cursor: pointer;
}
.chat__more[hidden] { display: none; }

.chat__stream { display: flex; flex-direction: column; gap: 10px; margin-top: auto; }
.chat__emptystream {
  margin: auto;
  font-size: 13px; color: var(--fg-faint);
}

/* ------------------------------------------------------------ a message */
.chat__msg {
  display: flex; align-items: flex-end; gap: 8px;
  max-width: 74%;
}
.chat__msg.is-grouped { margin-top: -2px; }

.chat__msgav {
  flex: 0 0 30px;
  width: 30px; height: 30px;
  border-radius: 50%; object-fit: cover;
  border: 1px solid var(--glass-line-2);
}
.chat__msgav--ph {
  display: grid; place-items: center;
  font-size: 12px; font-weight: 600;
  color: var(--fg-dim);
  background: var(--glass-2);
  text-transform: uppercase;
}
/* A grouped message keeps the indent so its bubble lines up under the first
   one's, since only that one carries the avatar.
   The tightening is small (-2px, was -6px): every message now has its own
   username line, so the run is already visually separated and a deeper negative
   margin crammed the names together. */
.chat__msg.is-grouped .chat__bubble { margin-left: 38px; }

.chat__bubble {
  position: relative;
  padding: 8px 12px 10px;
  background: var(--glass-2);
  border: 1px solid var(--glass-line);
  border-radius: 14px 14px 14px 4px;
}
.chat__who {
  display: block;
  margin-bottom: 2px;
  font-size: 11.5px; font-weight: 600;
  color: var(--fg-dim);
}
/* Own messages sit on the accent fill, where --fg-dim is too quiet to read.
   The name is the whole point of it being there, so it gets the contrast. */
.chat__msg.is-mine .chat__who { color: var(--fg); opacity: .75; }
.chat__text {
  margin: 0;
  font-size: 14px; line-height: 1.5;
  color: var(--fg);
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.chat__meta {
  display: block;
  margin-top: 4px;
  font-size: 10.5px;
  color: var(--fg-faint);
}

/* own messages, to the right */
.chat__msg.is-mine {
  margin-left: auto;
  flex-direction: row-reverse;
}
.chat__msg.is-mine .chat__bubble {
  background: var(--accent-soft);
  border-color: var(--accent-line);
  border-radius: 14px 14px 4px 14px;
  margin-left: 0;
}
.chat__msg.is-mine .chat__meta { text-align: right; }

/* edit and delete, revealed on hover or focus */
.chat__msgacts {
  position: absolute;
  top: -10px;
  display: flex; gap: 8px;
  padding: 3px 8px;
  background: var(--bg-raise);
  border: 1px solid var(--glass-line-2);
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s ease;
}
.chat__msg .chat__msgacts { right: 8px; }
.chat__msg.is-mine .chat__msgacts { right: auto; left: 8px; }
.chat__msg:hover .chat__msgacts,
.chat__msg:focus-within .chat__msgacts {
  opacity: 1;
  pointer-events: auto;
}
.chat__msgacts button {
  padding: 0;
  font: inherit; font-size: 11px;
  color: var(--fg-dim);
  background: none; border: 0; cursor: pointer;
}
.chat__msgacts button:hover { color: var(--fg); }

/* On a touch screen there is no hover, so the controls are always visible. */
@media (hover: none) {
  .chat__msgacts { opacity: 1; pointer-events: auto; }
}

.chat__gone {
  margin: 0;
  padding: 6px 12px;
  font-size: 12.5px; font-style: italic;
  color: var(--fg-faint);
  border: 1px dashed var(--glass-line-2);
  border-radius: 14px;
}

.chat__edit { margin-top: 6px; }
.chat__edit textarea {
  width: 100%;
  padding: 8px 10px;
  font: inherit; font-size: 14px; line-height: 1.5;
  color: var(--fg);
  background: var(--bg-soft);
  border: 1px solid var(--glass-line-2);
  border-radius: 10px;
  resize: vertical;
}
.chat__edit textarea:focus { outline: none; border-color: var(--accent); }
.chat__editrow { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.chat__editerr {
  margin: 4px 0 0;
  font-size: 11.5px;
  color: var(--red-bright);
}

/* -------------------------------------------------------------- composer */
.chat__compose {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--glass-line);
}
.chat__compose textarea {
  flex: 1;
  min-height: 40px; max-height: 160px;
  padding: 10px 12px;
  font: inherit; font-size: 14px; line-height: 1.5;
  color: var(--fg);
  background: var(--glass);
  border: 1px solid var(--glass-line-2);
  border-radius: 12px;
  resize: none;
  overflow-y: auto;
}
.chat__compose textarea:focus { outline: none; border-color: var(--accent); }
.chat__send { flex: 0 0 auto; }
.chat__hint {
  margin: 0;
  padding: 0 16px 10px;
  font-size: 12px;
  color: var(--fg-faint);
  min-height: 0;
}

/* ------------------------------------------------------------- contacts */
.chat__picker {
  position: fixed; inset: 0; z-index: 60;
  display: grid; place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, .6);
  backdrop-filter: blur(3px);
}
.chat__picker[hidden] { display: none; }
.chat__pickerbox {
  width: min(440px, 100%);
  max-height: 70vh;
  display: flex; flex-direction: column; gap: 14px;
  padding: 22px;
  background: var(--bg-raise);
  border: 1px solid var(--glass-line-2);
  border-radius: var(--radius);
}
.chat__pickerbox h3 { margin: 0; font-size: 15px; }
.chat__contacts {
  flex: 1; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
}
.chat__contact {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px;
  font: inherit; font-size: 13.5px; text-align: left;
  color: var(--fg-dim);
  background: none; border: 0; border-radius: 10px;
  cursor: pointer;
}
.chat__contact:hover { background: var(--glass-2); color: var(--fg); }

/* ----------------------------------------------------------------- gate */
.chat__gate {
  margin: 34px 0 80px;
  padding: 40px 28px;
  text-align: center;
  background: var(--glass);
  border: 1px dashed var(--glass-line-2);
  border-radius: var(--radius);
}
.chat__gate[hidden] { display: none; }
.chat__gate h2 { margin: 0 0 8px; font-size: 1.3rem; }
.chat__gate p { margin: 0 0 20px; color: var(--fg-dim); font-size: 14px; }
.chat__gateactions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* --------------------------------------------------------------- mobile */
@media (max-width: 720px) {
  .chat {
    grid-template-columns: 1fr;
    height: min(78vh, 620px);
  }
  /* One pane at a time: the list, or the conversation. Showing both on a phone
     leaves neither usable. */
  .chat__main { display: none; }
  .chat__side.is-open { display: none; }
  .chat__side.is-open + .chat__main { display: flex; }
  .chat__back { display: block; }
  .chat__msg { max-width: 88%; }
}
