.chat-layout {
  display: grid;
  grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
  gap: 14px;
  height: calc(100vh - 130px);
  min-height: 0;
  overflow: hidden;
}

.ai-workspace {
  display: grid;
  grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
  gap: 14px;
  height: calc(100vh - 126px);
  min-height: 0;
  overflow: hidden;
}

.ai-sidebar {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
}

.ai-main {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.ai-main-toolbar {
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding-bottom: 8px;
}

.ai-mobile-chats-toggle {
  display: none;
}

.ai-actions-popover {
  position: relative;
  z-index: 12;
}

.ai-actions-menu {
  position: absolute;
  top: calc(100% + 7px);
  inset-inline-end: 0;
  z-index: 14;
  width: 210px;
  display: grid;
  gap: 6px;
  padding: 8px;
  background: var(--panel-strong);
  border: 1px solid var(--line-soft);
  border-radius: 14px;
  box-shadow: 0 16px 38px var(--shadow-strong);
  backdrop-filter: blur(22px);
}

.ai-actions-menu[hidden] {
  display: none;
}

.ai-actions-menu button {
  width: 100%;
  min-height: 42px;
  justify-content: flex-start;
  box-shadow: none;
}

.compact-toolbar {
  align-items: center;
  margin-bottom: 0;
}

.ai-chat-list {
  min-height: 0;
}

.chat-users {
  background: var(--row-bg);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  overflow: auto;
}

.chat-user {
  width: 100%;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: 0;
  background: var(--row-bg);
  color: var(--text);
  border-bottom: 1px solid var(--line-soft);
  text-align: right;
  box-shadow: none;
}

.chat-user span {
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-user.active {
  background: var(--blue-soft);
  color: var(--blue);
}

.chat-panel {
  min-height: 0;
  background: var(--row-bg);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  overflow: hidden;
}

.messages {
  min-height: 0;
  padding: 14px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bubble {
  max-width: min(76%, 620px);
  padding: 9px 12px;
  border-radius: 8px;
  background: var(--subtle-bg);
  line-height: 1.8;
  box-shadow: 0 8px 18px var(--shadow);
  animation: bubbleIn 0.2s ease both;
}

.bubble.me {
  align-self: flex-start;
  background: var(--blue-soft);
}

.bubble.pending {
  opacity: 0.72;
}

.ai-thinking {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.ai-thinking i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--blue);
  opacity: 0.35;
  animation: thinkingDot 1s ease-in-out infinite;
}

.ai-thinking i:nth-of-type(2) {
  animation-delay: 0.15s;
}

.ai-thinking i:nth-of-type(3) {
  animation-delay: 0.3s;
}

.bubble small {
  display: block;
  color: var(--muted);
  margin-top: 4px;
}

.chat-compose {
  border-top: 1px solid var(--line-soft);
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  padding: 12px;
}

.chat-compose textarea {
  width: 100%;
  min-height: 44px;
  max-height: 180px;
  overflow-y: hidden;
  resize: none;
  line-height: 1.75;
  box-sizing: border-box;
}

.ai-chat-panel {
  min-height: 0;
}

#toast-root {
  position: fixed;
  left: 18px;
  bottom: 82px;
  z-index: 80;
  display: grid;
  gap: 8px;
}

.toast {
  min-width: 260px;
  max-width: 360px;
  background: #101827;
  color: #fff;
  padding: 10px 12px;
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.22);
  animation: toastIn 0.24s ease both;
}

.toast.error {
  background: #991b1b;
}

.hidden {
  display: none !important;
}

/* Organizational messenger */
.chat-messenger {
  height: calc(100dvh - 126px);
  min-height: 520px;
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--line-soft);
  border-radius: 18px;
  box-shadow: 0 16px 40px var(--shadow);
}

.messenger-contacts {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  background: var(--panel-strong);
  border-inline-end: 1px solid var(--line-soft);
}

.contacts-header,
.conversation-header {
  min-height: 72px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line-soft);
}

.contacts-header { justify-content: space-between; }
.contacts-header h3 { margin: 0; font-size: 17px; }
.contacts-header small { color: var(--muted); }
.chat-tools { display: flex; gap: 5px; }

.contact-search { padding: 10px 12px; border-bottom: 1px solid var(--line-soft); }
.contact-search input { width: 100%; min-height: 42px; border-radius: 12px; }
.contact-list { min-height: 0; overflow-y: auto; overscroll-behavior: contain; }

.messenger-contact {
  width: 100%;
  min-height: 76px;
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  color: var(--text);
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line-soft);
  border-radius: 0;
  box-shadow: none;
  text-align: right;
  white-space: normal;
}

.messenger-contact:hover,
.messenger-contact.active {
  color: var(--text);
  background: var(--emerald-soft);
  transform: none;
}

.chat-avatar {
  position: relative;
  width: 46px;
  height: 46px;
  flex: 0 0 46px;
  display: grid;
  place-items: center;
  overflow: visible;
  color: #fff;
  background: var(--emerald-dark);
  border-radius: 50%;
  font-size: 12px;
  font-weight: 800;
}

.chat-avatar img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
  background: var(--panel-strong);
}

.presence-dot {
  position: absolute;
  inset: auto auto -1px -1px;
  z-index: 2;
  width: 12px;
  height: 12px;
  background: var(--muted);
  border: 2px solid var(--panel-strong);
  border-radius: 50%;
}

.presence-dot.online { background: var(--emerald); }
.contact-copy { min-width: 0; display: grid; gap: 3px; }
.contact-copy strong,
.contact-copy small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-copy strong { font-size: 14px; }
.contact-copy small { color: var(--muted); font-size: 11px; }
.contact-meta { align-self: stretch; display: flex; flex-direction: column; align-items: flex-end; justify-content: space-around; gap: 4px; }
.contact-meta time { color: var(--muted); font-size: 10px; direction: ltr; }
.contact-meta b { min-width: 21px; height: 21px; display: grid; place-items: center; padding: 0 5px; color: #fff; background: var(--emerald-dark); border-radius: 999px; font-size: 10px; }

.messenger-conversation {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto auto auto;
  background: var(--row-bg);
  overflow: hidden;
}

.header-avatar { width: 44px; height: 44px; flex-basis: 44px; }
.conversation-person { min-width: 0; display: grid; }
.conversation-person strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-person small { color: var(--muted); font-size: 11px; }
.live-state { margin-inline-start: auto; color: var(--muted); font-size: 10px; }
#chat-mobile-back { display: none; }

.messenger-messages {
  min-height: 0;
  padding: 18px;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 7px;
  background:
    linear-gradient(135deg, rgba(var(--accent-rgb), 0.035), transparent 42%),
    var(--row-bg);
}

.message-bubble {
  width: fit-content;
  max-width: min(72%, 640px);
  padding: 8px 10px 5px;
  align-self: flex-start;
  background: var(--subtle-bg);
  border: 1px solid var(--line-soft);
  border-radius: 8px 8px 8px 2px;
  box-shadow: 0 7px 18px var(--shadow);
  animation: bubbleIn .18s ease both;
}

.message-bubble.mine {
  align-self: flex-end;
  background: var(--emerald-soft);
  border-color: rgba(var(--accent-rgb), .2);
  border-radius: 8px 8px 2px 8px;
}

.message-bubble p { margin: 2px 1px; line-height: 1.8; overflow-wrap: anywhere; }
.message-bubble footer { min-height: 17px; display: flex; align-items: center; justify-content: flex-end; gap: 5px; color: var(--muted); font-size: 9px; direction: ltr; }
.message-read-state { color: var(--muted); font-weight: 800; letter-spacing: 0; }
.message-read-state.read { color: var(--emerald-dark); }
.delete-message { width: 20px; height: 20px; min-height: 20px; padding: 0; color: var(--muted); background: transparent; border-radius: 50%; box-shadow: none; font-size: 14px; }
.reply-message { width: 20px; height: 20px; min-height: 20px; padding: 0; color: var(--muted); background: transparent; border-radius: 50%; box-shadow: none; font-size: 13px; }
.message-bubble.deleted { color: var(--muted); background: transparent; border-style: dashed; box-shadow: none; font-style: italic; }
.message-reply-quote { width: 100%; display: grid; gap: 2px; margin-bottom: 5px; padding: 6px 8px; color: var(--text); background: rgba(var(--accent-rgb), .08); border: 0; border-inline-start: 3px solid var(--emerald); border-radius: 5px; box-shadow: none; text-align: start; }
.message-reply-quote strong { color: var(--emerald); font-size: 10px; }
.message-reply-quote span { overflow: hidden; color: var(--muted); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.message-highlight { animation: messageHighlight 1.1s ease; }

@keyframes messageHighlight {
  35% { box-shadow: 0 0 0 4px rgba(var(--accent-rgb), .24); }
}

.message-media { display: block; max-width: 420px; overflow: hidden; border-radius: 7px; }
.message-media img,
.message-media video { display: block; width: 100%; max-height: 360px; object-fit: contain; background: #050807; border-radius: 7px; }
.message-audio { display: block; width: min(320px, 62vw); max-width: 100%; height: 42px; margin: 3px 0; }
.message-file { min-width: min(300px, 60vw); display: flex; align-items: center; gap: 10px; padding: 8px; color: var(--text); background: rgba(var(--accent-rgb), .08); border: 1px solid rgba(var(--accent-rgb), .16); border-radius: 7px; text-decoration: none; }
.message-file > span:first-child { width: 38px; height: 38px; display: grid; place-items: center; color: #fff; background: var(--emerald-dark); border-radius: 50%; font-size: 20px; }
.message-file > span:last-child { min-width: 0; display: grid; }
.message-file strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-file small { color: var(--muted); }

.typing-state { padding: 5px 16px; color: var(--emerald); background: var(--row-bg); font-size: 11px; }
.chat-reply-preview,
.chat-file-preview { min-height: 58px; display: grid; grid-template-columns: 28px minmax(0, 1fr) auto; align-items: center; gap: 8px; padding: 7px 12px; background: var(--panel-strong); border-top: 1px solid var(--line-soft); }
.chat-reply-preview { grid-template-columns: minmax(0, 1fr) auto; min-height: 52px; border-inline-start: 3px solid var(--emerald); }
.chat-reply-preview > span { min-width: 0; display: grid; }
.chat-reply-preview strong { color: var(--emerald); font-size: 11px; }
.chat-reply-preview small { overflow: hidden; color: var(--muted); text-overflow: ellipsis; white-space: nowrap; }
.chat-file-preview > span:nth-child(2) { min-width: 0; display: grid; }
.chat-file-preview strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-file-preview small { color: var(--muted); }

.messenger-compose {
  min-height: 66px;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 46px;
  align-items: end;
  gap: 7px;
  padding: 10px 12px;
  background: var(--panel-strong);
  border-top: 1px solid var(--line-soft);
}

.messenger-compose textarea { width: 100%; min-height: 44px; max-height: 180px; overflow-y: hidden; resize: none; border-radius: 14px; line-height: 1.75; box-sizing: border-box; }
.compose-icon,
.send-message { width: 44px; height: 44px; min-height: 44px; display: grid; place-items: center; padding: 0; border-radius: 50%; cursor: pointer; }
.compose-icon { color: var(--emerald); background: var(--subtle-bg); border: 1px solid var(--line-soft); font-size: 25px; }
.send-message { width: 46px; color: #fff; background: var(--emerald-dark); box-shadow: 0 8px 18px rgba(var(--accent-rgb), .2); transform: rotate(180deg); }
.messenger-empty,
.conversation-empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 5px; }
.empty-chat-icon { width: 64px; height: 64px; display: grid; place-items: center; margin-bottom: 8px; color: var(--emerald); background: var(--emerald-soft); border-radius: 50%; font-size: 34px; }


@media (max-width: 760px) {
  .chat-messenger { height: calc(100dvh - 212px); min-height: 480px; grid-template-columns: 1fr; border-radius: 12px; }
  .messenger-contacts,
  .messenger-conversation { grid-column: 1; grid-row: 1; }
  .chat-messenger:not(.has-conversation) .messenger-conversation { display: none; }
  .chat-messenger.has-conversation .messenger-contacts { display: none; }
  #chat-mobile-back { display: inline-grid; }
  .message-bubble { max-width: 88%; }
  .message-media { max-width: min(100%, 340px); }
  .messenger-messages { padding: 10px; }
}

@media (max-width: 390px) {
  .chat-messenger { height: calc(100dvh - 225px); min-height: 430px; }
  .contacts-header,
  .conversation-header { min-height: 64px; padding: 9px 10px; }
  .messenger-compose { grid-template-columns: 42px minmax(0, 1fr) 44px; padding: 8px; }
  .compose-icon { width: 42px; height: 42px; min-height: 42px; }
  .send-message { width: 44px; height: 44px; }
}
