/* ==========================================================================
   55GMS Chat
   Every color derives from the global --site-* theme tokens (style.css), so
   the chat follows whichever site theme is active.
   ========================================================================== */

:root {
  /* Typography */
  --chat-font:
    "Geist", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  --chat-font-mono:
    "Geist Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  /* Site tokens */
  --chat-bg: var(--site-bg);
  --chat-surface: var(--site-surface);
  --chat-surface-2: var(--site-surface-2);
  --chat-surface-3: var(--site-surface-3);
  --chat-border: var(--site-border);
  --chat-text: var(--site-text);
  --chat-muted: var(--site-muted);
  --chat-button: var(--site-button);
  --chat-button-hover: var(--site-button-hover);
  --chat-accent: var(--site-accent);
  --chat-accent-hover: var(--site-accent-hover);
  --chat-accent-text: var(--site-accent-text);
  --chat-input-bg: var(--site-input-bg);
  --chat-input-text: var(--site-input-text);
  --chat-shadow: var(--site-shadow);
  --chat-control-border: var(--site-control-border);
  --chat-control-border-hover: var(--site-control-border-hover);

  /* Derived surfaces */
  --chat-sidebar-bg: color-mix(
    in srgb,
    var(--site-surface-3) 70%,
    var(--site-bg)
  );
  --chat-popover-bg: color-mix(
    in srgb,
    var(--site-text) 4%,
    var(--site-surface-2)
  );
  --chat-line: color-mix(in srgb, var(--site-border) 60%, transparent);
  --chat-hover-bg: color-mix(in srgb, var(--site-text) 7%, transparent);
  --chat-selected-bg: color-mix(in srgb, var(--site-text) 11%, transparent);
  --chat-pressed-bg: color-mix(in srgb, var(--site-text) 15%, transparent);

  /* Derived text */
  --chat-secondary-text: color-mix(
    in srgb,
    var(--site-muted) 78%,
    var(--site-text)
  );
  --chat-tertiary-text: var(--site-muted);
  --chat-placeholder: color-mix(
    in srgb,
    var(--site-placeholder) 58%,
    transparent
  );

  /* Messages */
  --chat-bubble-bg: color-mix(in srgb, var(--site-text) 9%, var(--site-bg));
  --chat-bubble-text: var(--site-text);
  --chat-bubble-border: transparent;
  --chat-own-bg: var(--site-accent);
  --chat-own-text: var(--site-accent-text);
  --chat-avatar-bg: color-mix(in srgb, var(--site-text) 14%, var(--site-bg));

  /* Status */
  --chat-success: #34c77b;
  --chat-danger: #ef4444;
  --chat-danger-hover: #dc2626;
  --chat-warning: #f5a524;
  --chat-danger-text: color-mix(
    in srgb,
    var(--chat-danger) 58%,
    var(--site-text)
  );
  --chat-warning-text: color-mix(
    in srgb,
    var(--chat-warning) 70%,
    var(--site-text)
  );
  --chat-offline: color-mix(in srgb, var(--site-muted) 62%, var(--site-bg));

  /* Focus */
  --chat-focus: var(--site-accent);
  --chat-ring: var(--site-control-ring);

  /* Shape */
  --chat-radius-sm: 6px;
  --chat-radius-md: 8px;
  --chat-radius-lg: 12px;
  --chat-radius-bubble: 16px;
  --chat-radius-bubble-joined: 5px;

  /* Sizing */
  --chat-control-h: 32px;
  --chat-sidebar-w: 320px;
  --chat-bar-h: 56px;
  --chat-gutter: 16px;

  /* Motion */
  --chat-fast: 110ms;
  --chat-normal: 160ms;
  --chat-ease: cubic-bezier(0.2, 0, 0, 1);

  /* Elevation */
  --chat-shadow-pop:
    0 1px 2px color-mix(in srgb, black 14%, transparent),
    0 12px 32px -8px var(--site-shadow);
}

/* The default 55GMS look: slate-blue brand surfaces, light own bubbles. */
:root:not([data-theme]),
:root[data-theme="legacy"] {
  --chat-line: color-mix(in srgb, var(--site-border) 42%, transparent);
  --chat-secondary-text: color-mix(
    in srgb,
    var(--site-muted) 55%,
    var(--site-text)
  );
  --chat-tertiary-text: color-mix(
    in srgb,
    var(--site-muted) 82%,
    var(--site-text)
  );
  --chat-placeholder: color-mix(in srgb, var(--site-muted) 86%, transparent);
  --chat-input-bg: color-mix(
    in srgb,
    var(--site-surface-3) 86%,
    var(--site-bg)
  );
  --chat-popover-bg: color-mix(
    in srgb,
    var(--site-surface-3) 88%,
    var(--site-bg)
  );
  --chat-bubble-bg: color-mix(in srgb, var(--site-surface-3) 84%, black);
  --chat-own-bg: color-mix(in srgb, var(--site-text) 93%, var(--site-bg));
  --chat-own-text: color-mix(in srgb, var(--site-surface-3) 45%, black);
  --chat-avatar-bg: color-mix(in srgb, var(--site-surface-3) 70%, black);
  --chat-focus: color-mix(in srgb, var(--site-text) 80%, var(--site-bg));
}

:root[data-theme="contrast"] {
  --chat-sidebar-bg: var(--site-bg);
  --chat-popover-bg: var(--site-bg);
  --chat-line: var(--site-border);
  --chat-control-border: var(--site-border);
  --chat-control-border-hover: var(--site-accent);
  --chat-hover-bg: color-mix(in srgb, var(--site-text) 20%, transparent);
  --chat-selected-bg: color-mix(in srgb, var(--site-text) 28%, transparent);
  --chat-pressed-bg: color-mix(in srgb, var(--site-text) 36%, transparent);
  --chat-secondary-text: var(--site-text);
  --chat-tertiary-text: var(--site-text);
  --chat-placeholder: color-mix(in srgb, var(--site-text) 78%, transparent);
  --chat-bubble-bg: var(--site-bg);
  --chat-bubble-border: var(--site-border);
  --chat-avatar-bg: var(--site-bg);
  --chat-offline: var(--site-bg);
  --chat-danger-text: #ff8a8a;
  --chat-warning-text: #ffd166;
}

/* ==========================================================================
   Base
   ========================================================================== */

.chat-page,
.chat-page *,
.chat-page *::before,
.chat-page *::after {
  box-sizing: border-box;
}

body.chat-page {
  display: flex;
  height: 100dvh;
  flex-direction: column;
  overflow: hidden;
  color: var(--chat-text);
  /* background-color only: keeps the themed grid from style.css behind the nav */
  background-color: var(--chat-bg);
}

.chat-container,
.modal,
.notification-container {
  font-family: var(--chat-font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.chat-page button,
.chat-page input,
.chat-page textarea {
  font-family: inherit;
}

/* style.css scales every button on hover; chat controls stay put. */
.chat-page button:hover,
.chat-page button:active {
  transform: none;
}

.chat-page [hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.icon-sprite {
  position: absolute;
  width: 0;
  height: 0;
}

.icon {
  width: 16px;
  height: 16px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chat-page :focus-visible:not(.navbar *) {
  outline: none;
  box-shadow:
    0 0 0 2px var(--chat-bg),
    0 0 0 4px var(--chat-focus);
}

/* Scrollbars */
.chat-list,
.messages-container,
.modal-body,
.message-input textarea {
  scrollbar-width: thin;
  scrollbar-color: var(--chat-pressed-bg) transparent;
}

.chat-page ::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.chat-page ::-webkit-scrollbar-track {
  background: transparent;
  box-shadow: none;
}

.chat-page ::-webkit-scrollbar-thumb {
  background: var(--chat-pressed-bg);
  border-radius: 8px;
}

.chat-page ::-webkit-scrollbar-thumb:hover {
  background: color-mix(in srgb, var(--chat-text) 26%, transparent);
}

/* ==========================================================================
   Site navigation: left exactly as nav.css styles it on every other page
   ========================================================================== */

.chat-page nav {
  flex: none;
}

/* ==========================================================================
   Shell
   ========================================================================== */

.chat-container {
  display: flex;
  flex: 1;
  min-height: 0;
  border-top: 1px solid var(--chat-line);
  color: var(--chat-text);
  font-size: 14px;
  line-height: 1.4;
}

.chat-sidebar {
  display: flex;
  width: var(--chat-sidebar-w);
  flex: none;
  flex-direction: column;
  min-width: 0;
  background: var(--chat-sidebar-bg);
  border-right: 1px solid var(--chat-line);
}

.chat-main {
  position: relative;
  display: flex;
  flex: 1;
  min-width: 0;
  flex-direction: column;
  background: var(--chat-bg);
}

/* ==========================================================================
   Controls
   ========================================================================== */

.btn,
.icon-btn {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  height: var(--chat-control-h);
  color: var(--chat-text);
  background: transparent;
  border: 1px solid var(--chat-control-border);
  border-radius: var(--chat-radius-sm);
  cursor: pointer;
  user-select: none;
  transition:
    background-color var(--chat-fast) var(--chat-ease),
    border-color var(--chat-fast) var(--chat-ease),
    color var(--chat-fast) var(--chat-ease),
    opacity var(--chat-fast) var(--chat-ease);
}

.btn {
  gap: 6px;
  padding: 0 12px;
  font-size: 13px;
  font-weight: 500;
  line-height: 1;
  white-space: nowrap;
}

.btn:hover,
.icon-btn:hover {
  background: var(--chat-hover-bg);
  border-color: var(--chat-control-border-hover);
}

.chat-page .btn:active:not(:disabled),
.chat-page .icon-btn:active:not(:disabled) {
  background: var(--chat-pressed-bg);
  transform: translateY(1px);
}

.btn:disabled,
.icon-btn:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.btn-primary {
  color: var(--chat-accent-text);
  background: var(--chat-accent);
  border-color: var(--chat-accent);
}

.btn-primary:hover,
.chat-page .btn-primary:active:not(:disabled) {
  color: var(--chat-accent-text);
  background: var(--chat-accent-hover);
  border-color: var(--chat-accent-hover);
}

.btn-danger {
  color: #fff;
  background: var(--chat-danger);
  border-color: var(--chat-danger);
}

.btn-danger:hover,
.chat-page .btn-danger:active:not(:disabled) {
  color: #fff;
  background: var(--chat-danger-hover);
  border-color: var(--chat-danger-hover);
}

.btn-sm {
  height: 28px;
  padding: 0 10px;
  font-size: 12px;
}

.icon-btn.btn-sm {
  width: 28px;
  padding: 0;
}

.icon-btn {
  position: relative;
  width: var(--chat-control-h);
  padding: 0;
  color: var(--chat-secondary-text);
  border-color: transparent;
}

.icon-btn:hover {
  color: var(--chat-text);
  border-color: transparent;
}

.field,
.chat-search input {
  width: 100%;
  min-width: 0;
  height: var(--chat-control-h);
  padding: 0 10px;
  color: var(--chat-input-text);
  background: var(--chat-input-bg);
  border: 1px solid var(--chat-control-border);
  border-radius: var(--chat-radius-sm);
  font-size: 13px;
  outline: none;
  transition:
    border-color var(--chat-fast) var(--chat-ease),
    box-shadow var(--chat-fast) var(--chat-ease);
}

.field::placeholder,
.chat-search input::placeholder,
.token-field input::placeholder,
.message-input textarea::placeholder {
  color: var(--chat-placeholder);
  opacity: 1;
}

.field:hover,
.chat-search input:hover,
.token-field:hover,
.message-input:hover {
  border-color: var(--chat-control-border-hover);
}

.chat-page .field:focus,
.chat-page .chat-search input:focus,
.token-field:focus-within,
.message-input:focus-within {
  border-color: var(--chat-focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--chat-focus) 26%, transparent);
}

/* ==========================================================================
   Avatars and presence
   ========================================================================== */

.chat-avatar {
  position: relative;
  display: inline-flex;
  flex: none;
  /* ring color of the presence dot; set to the surface the avatar sits on */
  --avatar-ring: var(--chat-sidebar-bg);
}

.avatar-circle,
.message-avatar {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  overflow: hidden;
  color: var(--chat-text);
  background: color-mix(
    in oklab,
    hsl(var(--avatar-hue, 212) 62% 56%) 34%,
    var(--chat-avatar-bg)
  );
  border-radius: 50%;
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  user-select: none;
}

.avatar-circle.is-group {
  border-radius: var(--chat-radius-md);
}

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

.avatar-sm .avatar-circle,
.user-avatar,
.message-avatar {
  width: 28px;
  height: 28px;
  font-size: 12px;
}

.online-indicator,
.status-indicator {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 12px;
  height: 12px;
  background: var(--chat-success);
  border: 2px solid var(--avatar-ring);
  border-radius: 50%;
}

.online-indicator.offline,
.status-indicator.offline {
  background: var(--chat-offline);
}

:root[data-theme="contrast"] :is(.avatar-circle, .message-avatar) {
  background: var(--chat-avatar-bg);
  border: 1px solid var(--chat-border);
}

:root[data-theme="contrast"] :is(.online-indicator, .status-indicator) {
  border-color: var(--site-text);
}

/* ==========================================================================
   Sidebar
   ========================================================================== */

.chat-header {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: var(--chat-bar-h);
  padding: 0 12px 0 var(--chat-gutter);
}

.chat-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.chat-actions {
  display: flex;
  gap: 2px;
}

.friends-notification-badge,
.unread-badge,
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  color: var(--chat-accent-text);
  background: var(--chat-accent);
  border-radius: 9px;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.friends-notification-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  font-size: 10px;
  box-shadow: 0 0 0 2px var(--chat-sidebar-bg);
}

.chat-search {
  position: relative;
  flex: none;
  padding: 0 12px 8px;
}

.chat-search .icon {
  position: absolute;
  top: 8px;
  left: 22px;
  color: var(--chat-tertiary-text);
  pointer-events: none;
}

.chat-search input {
  padding-left: 32px;
}

.chat-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1px;
  min-height: 0;
  padding: 4px 8px 8px;
  overflow-x: hidden;
  overflow-y: auto;
}

.chat-item {
  position: relative;
  display: flex;
  flex: none;
  align-items: center;
  gap: 10px;
  padding: 8px;
  color: var(--chat-text);
  border-radius: var(--chat-radius-md);
  cursor: pointer;
  text-decoration: none;
  transition: background-color var(--chat-fast) var(--chat-ease);
}

.chat-item:hover {
  color: var(--chat-text);
  background: var(--chat-hover-bg);
}

.chat-item:active {
  background: var(--chat-pressed-bg);
}

.chat-item.active {
  background: var(--chat-selected-bg);
}

.chat-item.active::before {
  position: absolute;
  top: 50%;
  left: -8px;
  width: 3px;
  height: 20px;
  background: var(--chat-accent);
  border-radius: 0 3px 3px 0;
  content: "";
  transform: translateY(-50%);
}

.chat-item-content {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.chat-item-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chat-item-name,
.chat-item-preview {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-name {
  font-size: 14px;
  font-weight: 500;
}

.chat-item-preview {
  color: var(--chat-secondary-text);
  font-size: 13px;
}

.chat-time {
  flex: none;
  color: var(--chat-tertiary-text);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.chat-item.is-unread .chat-item-name {
  font-weight: 600;
}

.chat-item.is-unread .chat-item-preview {
  color: var(--chat-text);
}

.chat-item.is-unread .chat-time {
  color: var(--chat-text);
}

/* Empty / loading / error states inside lists */
.list-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 32px 16px;
  color: var(--chat-secondary-text);
  font-size: 13px;
  text-align: center;
}

.list-state strong {
  color: var(--chat-text);
  font-size: 13px;
  font-weight: 500;
}

.list-state .btn {
  margin-top: 8px;
}

.chat-skeleton {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
}

.skeleton {
  display: block;
  background: var(--chat-hover-bg);
  border-radius: 4px;
  animation: chat-pulse 1.2s ease-in-out infinite;
}

.skeleton-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
}

.skeleton-lines {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 8px;
}

.skeleton-lines .skeleton {
  height: 10px;
}

.skeleton-lines .skeleton:first-child {
  width: 45%;
}

.skeleton-lines .skeleton:last-child {
  width: 80%;
}

@keyframes chat-pulse {
  50% {
    opacity: 0.45;
  }
}

.user-info-bar {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--chat-line);
}

.user-info {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.user-details {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.user-details span:first-child {
  overflow: hidden;
  font-size: 13px;
  font-weight: 500;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-status,
.chat-details span,
.friend-meta,
.member-status {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--chat-secondary-text);
  font-size: 12px;
}

.user-status::before,
#chatStatus.is-online::before,
#chatStatus.is-offline::before {
  width: 6px;
  height: 6px;
  flex: none;
  background: var(--chat-success);
  border-radius: 50%;
  content: "";
}

.user-status.is-offline::before,
#chatStatus.is-offline::before {
  background: var(--chat-offline);
}

:root[data-theme="contrast"]
  :is(.user-status.is-offline, #chatStatus.is-offline)::before {
  box-shadow: 0 0 0 1px var(--site-text);
}

.copy-btn.is-copied {
  color: var(--chat-success);
}

/* ==========================================================================
   Empty state
   ========================================================================== */

.chat-welcome {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.welcome-content {
  display: flex;
  max-width: 280px;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.welcome-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  margin-bottom: 14px;
  color: var(--chat-secondary-text);
  border: 1px solid var(--chat-control-border);
  border-radius: var(--chat-radius-md);
}

.welcome-icon .icon {
  width: 18px;
  height: 18px;
}

.welcome-content h2 {
  margin: 0 0 4px;
  font-size: 16px;
  font-weight: 600;
}

.welcome-content p {
  margin: 0 0 16px;
  color: var(--chat-secondary-text);
  font-size: 13px;
}

/* ==========================================================================
   Conversation header
   ========================================================================== */

.chat-content {
  display: flex;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

.chat-info {
  display: flex;
  flex: none;
  align-items: center;
  gap: 10px;
  height: var(--chat-bar-h);
  padding: 0 12px 0 var(--chat-gutter);
  border-bottom: 1px solid var(--chat-line);
}

.chat-info .chat-avatar {
  --avatar-ring: var(--chat-bg);
}

.chat-info .avatar-circle {
  width: 32px;
  height: 32px;
  font-size: 13px;
}

.mobile-chat-back {
  display: none;
  margin-left: -6px;
}

.chat-details {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.chat-details h3 {
  margin: 0;
  overflow: hidden;
  font-size: 14px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-menu {
  position: relative;
  flex: none;
}

.menu-btn[aria-expanded="true"] {
  color: var(--chat-text);
  background: var(--chat-selected-bg);
}

.chat-menu-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 50;
  min-width: 208px;
  padding: 4px;
  background: var(--chat-popover-bg);
  border: 1px solid var(--chat-control-border);
  border-radius: 10px;
  box-shadow: var(--chat-shadow-pop);
  transform-origin: top right;
  animation: chat-pop-in var(--chat-fast) var(--chat-ease);
}

.menu-option {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  height: var(--chat-control-h);
  padding: 0 8px;
  color: var(--chat-text);
  background: transparent;
  border: 0;
  border-radius: var(--chat-radius-sm);
  cursor: pointer;
  font-size: 13px;
  text-align: left;
  transition: background-color var(--chat-fast) var(--chat-ease);
}

.menu-option .icon {
  color: var(--chat-secondary-text);
}

.menu-option:hover,
.chat-page .menu-option:focus-visible {
  background: var(--chat-hover-bg);
  box-shadow: none;
}

.menu-option.danger,
.menu-option.danger .icon {
  color: var(--chat-danger-text);
}

.menu-option.danger:hover,
.chat-page .menu-option.danger:focus-visible {
  background: color-mix(in srgb, var(--chat-danger) 18%, transparent);
}

.menu-divider {
  height: 1px;
  margin: 4px -4px;
  background: var(--chat-line);
}

@keyframes chat-pop-in {
  from {
    opacity: 0;
    transform: scale(0.97) translateY(-2px);
  }
}

/* ==========================================================================
   Messages
   ========================================================================== */

.messages-container {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.messages {
  display: flex;
  min-height: 100%;
  flex-direction: column;
  justify-content: flex-end;
  padding: 16px var(--chat-gutter) 4px;
}

.message {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: min(72%, 640px);
  margin-top: 2px;
}

.message.is-first {
  margin-top: 12px;
}

.message.own {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message.is-new {
  animation: chat-message-in var(--chat-normal) var(--chat-ease);
}

@keyframes chat-message-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
}

.message-avatar {
  margin-bottom: 18px;
  visibility: hidden;
}

.message.is-last .message-avatar {
  visibility: visible;
}

.message-content {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
}

.message.own .message-content {
  align-items: flex-end;
}

.message-header {
  display: none;
  margin: 0 0 3px 12px;
  color: var(--chat-secondary-text);
  font-size: 12px;
  font-weight: 500;
}

.message.is-first .message-header {
  display: block;
}

.message-bubble {
  max-width: 100%;
  padding: 7px 12px;
  color: var(--chat-bubble-text);
  background: var(--chat-bubble-bg);
  border: 1px solid var(--chat-bubble-border);
  border-radius: var(--chat-radius-bubble);
  font-size: 14px;
  line-height: 1.45;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  transition: opacity var(--chat-normal) var(--chat-ease);
}

.message:not(.own, .is-first) .message-bubble {
  border-top-left-radius: var(--chat-radius-bubble-joined);
}

.message:not(.own, .is-last) .message-bubble {
  border-bottom-left-radius: var(--chat-radius-bubble-joined);
}

.message.own .message-bubble {
  color: var(--chat-own-text);
  background: var(--chat-own-bg);
  border-color: var(--chat-own-bg);
}

.message.own:not(.is-first) .message-bubble {
  border-top-right-radius: var(--chat-radius-bubble-joined);
}

.message.own:not(.is-last) .message-bubble {
  border-bottom-right-radius: var(--chat-radius-bubble-joined);
}

.message.is-pending .message-bubble {
  opacity: 0.6;
}

.message-time {
  display: none;
  margin: 3px 4px 0;
  color: var(--chat-tertiary-text);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.message.is-last .message-time {
  display: block;
}

/* System messages: inline metadata, not bubbles */
.message.system {
  max-width: 100%;
  align-self: center;
  margin: 12px 0 4px;
}

.system-message {
  display: block;
  color: var(--chat-secondary-text);
  font-size: 12px;
  text-align: center;
  overflow-wrap: anywhere;
}

.system-message .message-time {
  display: inline;
  margin: 0;
  font-size: 12px;
}

.system-message .message-time::before {
  content: " · ";
}

.message-day {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 0 4px;
  color: var(--chat-tertiary-text);
  font-size: 11px;
  font-weight: 500;
}

.message-day::before,
.message-day::after {
  height: 1px;
  flex: 1;
  background: var(--chat-line);
  content: "";
}

.messages-state {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 24px;
  color: var(--chat-secondary-text);
  font-size: 13px;
  text-align: center;
}

.messages-state strong {
  color: var(--chat-text);
  font-weight: 500;
}

.messages-state .btn {
  margin-top: 8px;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--chat-pressed-bg);
  border-top-color: var(--chat-text);
  border-radius: 50%;
  animation: chat-spin 0.7s linear infinite;
}

@keyframes chat-spin {
  to {
    transform: rotate(360deg);
  }
}

/* ==========================================================================
   Composer
   ========================================================================== */

.message-input-container {
  position: relative;
  flex: none;
  padding: 0 var(--chat-gutter);
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}

/* Always reserves its row, so showing it never shifts the layout. */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 22px;
  padding: 0 4px;
  overflow: hidden;
  color: var(--chat-secondary-text);
  font-size: 12px;
  opacity: 0;
  white-space: nowrap;
  transition: opacity var(--chat-normal) var(--chat-ease);
}

.typing-indicator.is-visible {
  opacity: 1;
}

#typingText {
  overflow: hidden;
  text-overflow: ellipsis;
}

.typing-dots {
  display: inline-flex;
  flex: none;
  gap: 3px;
}

.typing-dots i {
  width: 4px;
  height: 4px;
  background: currentColor;
  border-radius: 50%;
  opacity: 0.35;
}

.typing-indicator.is-visible .typing-dots i {
  animation: chat-typing 1.2s ease-in-out infinite;
}

.typing-dots i:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-dots i:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes chat-typing {
  30% {
    opacity: 1;
    transform: translateY(-2px);
  }
  60% {
    opacity: 0.35;
    transform: none;
  }
}

.message-input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 5px 5px 5px 12px;
  background: var(--chat-input-bg);
  border: 1px solid var(--chat-control-border);
  border-radius: 10px;
  transition:
    border-color var(--chat-fast) var(--chat-ease),
    box-shadow var(--chat-fast) var(--chat-ease);
}

.message-input textarea {
  flex: 1;
  min-width: 0;
  max-height: 132px;
  padding: 5px 0;
  color: var(--chat-input-text);
  background: transparent;
  border: 0;
  font-size: 14px;
  line-height: 20px;
  outline: none;
  resize: none;
}

.chat-page .message-input textarea:focus-visible {
  box-shadow: none;
}

.send-btn {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  color: var(--chat-accent-text);
  background: var(--chat-accent);
  border: 0;
  border-radius: 7px;
  cursor: pointer;
  transition:
    background-color var(--chat-fast) var(--chat-ease),
    color var(--chat-fast) var(--chat-ease);
}

.send-btn:hover:not(:disabled) {
  background: var(--chat-accent-hover);
}

.chat-page .send-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.send-btn:disabled {
  color: var(--chat-tertiary-text);
  background: var(--chat-hover-bg);
  cursor: default;
}

.character-counter {
  align-self: center;
  flex: none;
  color: var(--chat-tertiary-text);
  font-family: var(--chat-font-mono);
  font-size: 11px;
}

.character-counter.is-warning,
#memberCount.is-warning {
  color: var(--chat-warning-text);
}

.character-counter.is-danger,
#memberCount.is-danger {
  color: var(--chat-danger-text);
}

.jump-to-latest {
  position: absolute;
  top: -22px;
  right: var(--chat-gutter);
  z-index: 5;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px 0 8px;
  color: var(--chat-text);
  background: var(--chat-popover-bg);
  border: 1px solid var(--chat-control-border);
  border-radius: 14px;
  box-shadow: var(--chat-shadow-pop);
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  animation: chat-pop-in var(--chat-fast) var(--chat-ease);
}

.jump-to-latest:hover {
  border-color: var(--chat-control-border-hover);
}

.jump-to-latest.has-new {
  color: var(--chat-accent-text);
  background: var(--chat-accent);
  border-color: var(--chat-accent);
}

/* ==========================================================================
   Dialogs
   ========================================================================== */

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 12vh 16px 16px;
  background: color-mix(in srgb, black 55%, transparent);
  animation: chat-fade-in var(--chat-fast) linear;
}

.modal-content {
  display: flex;
  width: min(100%, 440px);
  max-height: min(620px, 76dvh);
  flex-direction: column;
  overflow: hidden;
  color: var(--chat-text);
  background: var(--chat-popover-bg);
  border: 1px solid var(--chat-control-border);
  border-radius: var(--chat-radius-lg);
  box-shadow: var(--chat-shadow-pop);
  font-size: 13px;
  animation: chat-dialog-in var(--chat-normal) var(--chat-ease);
}

@keyframes chat-fade-in {
  from {
    opacity: 0;
  }
}

@keyframes chat-dialog-in {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.985);
  }
}

.modal-header {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 10px 12px var(--chat-gutter);
}

.modal-header h3 {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
}

.modal-header h3 small {
  min-width: 0;
  overflow: hidden;
  color: var(--chat-secondary-text);
  font-size: 12px;
  font-weight: 400;
  text-overflow: ellipsis;
}

.modal-body {
  display: flex;
  min-height: 0;
  flex-direction: column;
  padding: 0 var(--chat-gutter) var(--chat-gutter);
  overflow-y: auto;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.add-friend-form,
.field-row {
  display: flex;
  gap: 8px;
}

.add-friend-form {
  margin-bottom: 12px;
}

/* Underline tabs (friends) */
.friends-tabs {
  display: flex;
  flex: none;
  gap: 16px;
  margin: 0 calc(var(--chat-gutter) * -1) 4px;
  padding: 0 var(--chat-gutter);
  border-bottom: 1px solid var(--chat-line);
}

.friends-tabs .tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  margin-bottom: -1px;
  padding: 0;
  color: var(--chat-secondary-text);
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition:
    border-color var(--chat-fast) var(--chat-ease),
    color var(--chat-fast) var(--chat-ease);
}

.friends-tabs .tab-btn:hover {
  color: var(--chat-text);
}

.friends-tabs .tab-btn.active {
  color: var(--chat-text);
  border-bottom-color: var(--chat-text);
}

.tab-count {
  color: var(--chat-tertiary-text);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  font-weight: 400;
}

.badge {
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  font-size: 10px;
}

/* Segmented control (new conversation) */
.chat-type-tabs {
  display: grid;
  flex: none;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  gap: 2px;
  margin-bottom: 14px;
  padding: 2px;
  background: var(--chat-hover-bg);
  border: 1px solid var(--chat-line);
  border-radius: var(--chat-radius-md);
}

.chat-type-tabs .tab-btn {
  height: 28px;
  color: var(--chat-secondary-text);
  background: transparent;
  border: 0;
  border-radius: 5px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition:
    background-color var(--chat-fast) var(--chat-ease),
    color var(--chat-fast) var(--chat-ease);
}

.chat-type-tabs .tab-btn:hover {
  color: var(--chat-text);
}

.chat-type-tabs .tab-btn.active {
  color: var(--chat-text);
  background: var(--chat-selected-bg);
  box-shadow: inset 0 0 0 1px var(--chat-line);
}

:root[data-theme="contrast"] .chat-type-tabs .tab-btn.active {
  color: var(--chat-accent-text);
  background: var(--chat-accent);
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

#friendsModal .tab-content {
  min-height: 220px;
}

.form-group {
  margin-bottom: 14px;
}

.form-group label,
.picker-label {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 6px;
  color: var(--chat-secondary-text);
  font-size: 12px;
  font-weight: 500;
}

#memberCount {
  font-family: var(--chat-font-mono);
  font-size: 11px;
  font-weight: 400;
}

.field-hint {
  margin-top: 6px;
  color: var(--chat-tertiary-text);
  font-size: 12px;
}

.token-field {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  min-height: var(--chat-control-h);
  padding: 3px;
  background: var(--chat-input-bg);
  border: 1px solid var(--chat-control-border);
  border-radius: var(--chat-radius-sm);
  cursor: text;
  transition:
    border-color var(--chat-fast) var(--chat-ease),
    box-shadow var(--chat-fast) var(--chat-ease);
}

#membersList {
  display: contents;
}

.token-field input {
  flex: 1;
  min-width: 120px;
  height: 24px;
  padding: 0 6px;
  color: var(--chat-input-text);
  background: transparent;
  border: 0;
  font-size: 13px;
  outline: none;
}

.chat-page .token-field input:focus-visible {
  box-shadow: none;
}

.member-tag {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  max-width: 100%;
  height: 24px;
  padding: 0 2px 0 8px;
  color: var(--chat-text);
  background: var(--chat-selected-bg);
  border: 1px solid var(--chat-line);
  border-radius: 5px;
  font-size: 12px;
  font-weight: 500;
}

.member-tag-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.member-tag .remove {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  padding: 0;
  color: var(--chat-secondary-text);
  background: transparent;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
}

.member-tag .remove .icon {
  width: 12px;
  height: 12px;
}

.member-tag .remove:hover {
  color: var(--chat-text);
  background: var(--chat-hover-bg);
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 8px;
}

.suggestion {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  max-width: 100%;
  height: 24px;
  padding: 0 8px 0 6px;
  overflow: hidden;
  color: var(--chat-secondary-text);
  background: transparent;
  border: 1px dashed var(--chat-control-border);
  border-radius: 5px;
  cursor: pointer;
  font-size: 12px;
  transition:
    background-color var(--chat-fast) var(--chat-ease),
    color var(--chat-fast) var(--chat-ease);
}

.suggestion .icon {
  width: 12px;
  height: 12px;
}

.suggestion:hover {
  color: var(--chat-text);
  background: var(--chat-hover-bg);
}

.picker {
  margin: 0 -8px;
}

.picker-label {
  margin: 4px 8px 4px;
}

/* Rows: friends, requests, blocked users, members, friend picker */
.row-list {
  margin: 0 -8px;
}

.friend-item,
.request-item,
.group-member-item,
.picker-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 48px;
  padding: 6px 8px;
  color: var(--chat-text);
  border-radius: var(--chat-radius-md);
  transition: background-color var(--chat-fast) var(--chat-ease);
}

.friend-item:hover,
.request-item:hover,
.group-member-item:hover,
.picker-item:hover {
  background: var(--chat-hover-bg);
}

.picker-item {
  background: transparent;
  border: 0;
  cursor: pointer;
  font-size: 13px;
  text-align: left;
}

.modal .chat-avatar {
  --avatar-ring: var(--chat-popover-bg);
}

.modal .avatar-circle {
  width: 32px;
  height: 32px;
  font-size: 13px;
}

.friend-info,
.member-info {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.friend-name,
.member-username {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  font-size: 13px;
  font-weight: 500;
}

.friend-name > span:first-child,
.member-username > span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.role-badge {
  flex: none;
  height: 16px;
  padding: 0 5px;
  color: var(--chat-secondary-text);
  border: 1px solid var(--chat-control-border);
  border-radius: 4px;
  font-size: 10px;
  font-weight: 500;
  line-height: 14px;
}

.role-badge.is-admin {
  color: var(--chat-accent-text);
  background: var(--chat-accent);
  border-color: var(--chat-accent);
}

.friend-actions {
  display: flex;
  flex: none;
  gap: 6px;
}

/* ==========================================================================
   Toasts
   ========================================================================== */

.notification-container {
  position: fixed;
  top: 104px;
  right: 16px;
  z-index: 2000;
  display: flex;
  width: min(320px, calc(100vw - 32px));
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.notification {
  padding: 10px 12px;
  color: var(--chat-text);
  background: var(--chat-popover-bg);
  border: 1px solid var(--chat-control-border);
  border-radius: 10px;
  box-shadow: var(--chat-shadow-pop);
  font-family: var(--chat-font);
  pointer-events: auto;
  animation: chat-toast-in var(--chat-normal) var(--chat-ease);
  transition: border-color var(--chat-fast) var(--chat-ease);
}

.notification.is-clickable {
  cursor: pointer;
}

.notification.is-clickable:hover {
  border-color: var(--chat-control-border-hover);
}

@keyframes chat-toast-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
}

.notification-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.notification-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
}

.notification-title::before {
  width: 6px;
  height: 6px;
  flex: none;
  background: var(--chat-accent);
  border-radius: 50%;
  content: "";
}

.notification.is-success .notification-title::before {
  background: var(--chat-success);
}

.notification.is-warning .notification-title::before {
  background: var(--chat-warning);
}

.notification.is-error .notification-title::before {
  background: var(--chat-danger);
}

.notification-time {
  flex: none;
  color: var(--chat-tertiary-text);
  font-size: 11px;
}

.notification-message {
  display: -webkit-box;
  margin-top: 2px;
  overflow: hidden;
  color: var(--chat-secondary-text);
  font-size: 13px;
  line-height: 1.4;
  overflow-wrap: anywhere;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
}

/* ==========================================================================
   SweetAlert dialogs (confirmations and errors)
   ========================================================================== */

.chat-page .swal2-popup.chat-swal {
  width: min(360px, calc(100vw - 32px));
  padding: 20px;
  color: var(--chat-text);
  background: var(--chat-popover-bg);
  border: 1px solid var(--chat-control-border);
  border-radius: var(--chat-radius-lg);
  box-shadow: var(--chat-shadow-pop);
  font-family: var(--chat-font);
}

.chat-swal .swal2-icon {
  margin: 4px auto 12px;
  font-size: 9px;
}

.chat-swal .swal2-title {
  padding: 0;
  color: var(--chat-text);
  font-size: 16px;
  font-weight: 600;
}

.chat-swal .swal2-html-container {
  margin: 6px 0 0;
  padding: 0;
  color: var(--chat-secondary-text);
  font-size: 13px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.chat-swal .swal2-actions {
  gap: 8px;
  margin-top: 18px;
}

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 1024px) {
  :root {
    --chat-sidebar-w: 288px;
  }
}

@media (max-width: 768px) {
  :root {
    --chat-control-h: 36px;
  }

  /* One pane at a time: list first, conversation after selecting one. */
  .chat-sidebar {
    width: 100%;
    border-right: 0;
  }

  .chat-main {
    display: none;
  }

  body.chat-view-active .chat-sidebar,
  body.chat-view-active nav {
    display: none;
  }

  body.chat-view-active .chat-main {
    display: flex;
  }

  .mobile-chat-back {
    display: inline-flex;
  }

  .chat-info {
    padding-left: 12px;
    padding-top: env(safe-area-inset-top);
    height: calc(var(--chat-bar-h) + env(safe-area-inset-top));
  }

  .chat-item {
    padding: 10px 8px;
  }

  .message {
    max-width: 86%;
  }

  /* 16px keeps iOS from zooming the page when a field is focused. */
  .field,
  .chat-search input,
  .token-field input,
  .message-input textarea {
    font-size: 16px;
  }

  .chat-search .icon {
    top: 10px;
  }

  .send-btn {
    width: 34px;
    height: 34px;
  }

  .btn-sm {
    height: 32px;
  }

  .icon-btn.btn-sm {
    width: 32px;
  }

  .notification-container {
    top: calc(8px + env(safe-area-inset-top));
    right: 8px;
    left: 8px;
    width: auto;
  }

  /* Dialogs become bottom sheets. */
  .modal {
    align-items: flex-end;
    padding: 0;
  }

  .modal-content {
    width: 100%;
    max-height: 88dvh;
    padding-bottom: env(safe-area-inset-bottom);
    border-width: 1px 0 0;
    border-radius: var(--chat-radius-lg) var(--chat-radius-lg) 0 0;
  }

  @keyframes chat-dialog-in {
    from {
      transform: translateY(24px);
      opacity: 0;
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-page *,
  .chat-page *::before,
  .chat-page *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .chat-page .btn:active:not(:disabled),
  .chat-page .icon-btn:active:not(:disabled),
  .chat-page .send-btn:active:not(:disabled) {
    transform: none;
  }
}
