.chat-toggle {
  position: fixed;
  right: 70px;
  bottom: 14px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
  cursor: pointer;
  z-index: 60;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.18);
}

.chat-toggle:hover {
  filter: brightness(1.06);
}

.chat-panel {
  position: fixed;
  right: 70px;
  bottom: 66px;
  width: min(620px, calc(100vw - 28px));
  max-height: min(84vh, 760px);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.2);
  z-index: 70;
  display: none;
  flex-direction: column;
  overflow: hidden;
}

.chat-panel.open {
  display: flex;
}

.chat-head {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  font-weight: 700;
  background: linear-gradient(135deg, rgba(30, 111, 79, 0.12), rgba(30, 111, 79, 0.08));
}

.chat-body {
  --chat-item-size: 52px;
  --chat-gap: 8px;
  --chat-visible: 10;
  --chat-list-height: calc((var(--chat-item-size) * var(--chat-visible)) + (var(--chat-gap) * (var(--chat-visible) - 1)));
  display: grid;
  grid-template-columns: 88px 1fr;
  height: var(--chat-list-height);
  min-height: 0;
  max-height: inherit;
}

.chat-left {
  border-right: 1px solid var(--line);
  padding: 12px;
  display: grid;
  gap: 10px;
  align-content: start;
  background: rgba(248, 251, 246, 0.7);
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(30, 111, 79, 0.7) rgba(248, 251, 246, 0.7);
}

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

.chat-left::-webkit-scrollbar-track {
  background: rgba(248, 251, 246, 0.7);
  border-radius: 999px;
}

.chat-left::-webkit-scrollbar-thumb {
  background: rgba(30, 111, 79, 0.7);
  border-radius: 999px;
  border: 2px solid transparent;
  background-clip: content-box;
}

body.dark-mode .chat-left {
  background: rgba(27, 45, 37, 0.65);
  scrollbar-color: rgba(30, 111, 79, 0.8) rgba(255, 255, 255, 0.08);
}

body.dark-mode .chat-left::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.08);
}

body.dark-mode .chat-left::-webkit-scrollbar-thumb {
  background: rgba(30, 111, 79, 0.8);
}

.chat-right {
  padding: 12px;
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 10px;
  height: var(--chat-list-height);
  min-height: 0;
}

.chat-list {
  display: grid;
  gap: var(--chat-gap);
  max-height: none;
}

.chat-item {
  border: 1px solid var(--line);
  border-radius: 12px;
  width: var(--chat-item-size);
  height: var(--chat-item-size);
  background: #f8fbf6;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  font-weight: 700;
  position: relative;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

body.dark-mode .chat-item {
  background: #1f3229;
}

.chat-item.active {
  border-color: var(--accent, #2b8f65);
  box-shadow: 0 6px 16px rgba(30, 111, 79, 0.18);
}

.chat-item.empty {
  color: var(--muted);
  background: transparent;
  border-style: dashed;
  font-weight: 600;
  width: auto;
  height: auto;
  padding: 10px;
  text-align: center;
}

.chat-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--line);
}

.chat-item.has-unread::after {
  content: "";
  position: absolute;
  right: 6px;
  bottom: 6px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #d33a32;
  border: 2px solid var(--card);
}

.chat-messages {
  display: grid;
  gap: 4px;
  min-height: 0;
  overflow-y: auto;
  align-content: start;
  padding-right: 4px;
}

.chat-message {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  gap: 8px;
  align-items: start;
  border-radius: 12px;
  padding: 8px 10px;
  background: transparent;
  color: var(--ink);
}

.chat-message:hover {
  background: rgba(0, 0, 0, 0.04);
}

body.dark-mode .chat-message:hover {
  background: rgba(255, 255, 255, 0.05);
}

.chat-message.compact {
  padding-top: 0;
}

.chat-message.compact .avatar,
.chat-message.compact .header {
  display: none;
}

.chat-message.compact .content {
  grid-column: 2;
}

.chat-message.compact .actions {
  grid-column: 3;
  align-self: center;
}

.chat-message .avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--line);
}

.chat-message .content {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.chat-message .header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.chat-message .meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}

.chat-message .username {
  font-weight: 700;
  font-size: 0.9rem;
}

.chat-message .time,
.chat-message .edited {
  font-size: 0.75rem;
  color: var(--muted);
}

.chat-message .text {
  background: #f8fbf6;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 10px;
  width: fit-content;
  max-width: min(100%, 420px);
  color: var(--ink);
  line-height: 1.35;
  display: grid;
  gap: 8px;
}

.chat-message .text.attachment-only {
  min-width: 220px;
}

body.dark-mode .chat-message .text {
  background: #1f3229;
}

.chat-attachment-link,
.chat-attachment-file {
  color: inherit;
  text-decoration: none;
}

.chat-attachment-file {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}

.chat-attachment-media {
  display: block;
  width: 100%;
  max-width: min(100%, 360px);
  max-height: 320px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.05);
  object-fit: cover;
}

.chat-image-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px;
  width: min(320px, 100%);
  border: 0;
  padding: 0;
  background: transparent;
  cursor: pointer;
}

.chat-image-tile {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.06);
}

.chat-image-tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.chat-image-tile.overflow::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}

.chat-image-grid-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 1.5rem;
  font-weight: 700;
  z-index: 1;
}

.chat-message.editing .text {
  width: 100%;
  max-width: 100%;
  background: transparent;
  border: 0;
  padding: 0;
}

.edit-textarea {
  width: 100%;
  min-height: 70px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  font: inherit;
}

body.dark-mode .edit-textarea {
  background: #20322a;
  color: var(--ink);
}

.edit-actions {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}

.edit-actions button {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 8px;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  background: #fff;
  color: var(--ink);
}

body.dark-mode .edit-actions button {
  background: #223a30;
  color: var(--ink);
}

.chat-message .actions {
  display: flex;
  gap: 6px;
  opacity: 0;
  pointer-events: none;
}

.chat-message button {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 8px;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  background: #fff;
  color: var(--ink);
}

body.dark-mode .chat-message button {
  background: #223a30;
  color: var(--ink);
}

.chat-message:hover .actions {
  opacity: 1;
  pointer-events: auto;
}

.chat-input {
  position: relative;
  display: grid;
  gap: 8px;
  min-width: 0;
}

.chat-input textarea {
  width: 100%;
  min-height: 56px;
  max-height: 140px;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 15px 52px 15px 52px;
  font: inherit;
  resize: none;
}

body.dark-mode .chat-input textarea {
  background: #20322a;
  color: var(--ink);
}

.chat-input .attach-trigger,
.chat-input .send-btn {
  position: absolute;
  bottom: 9px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.chat-input .attach-trigger {
  left: 10px;
  background: transparent;
  color: var(--muted);
}

.chat-input .send-btn {
  right: 10px;
  background: #2b8f65;
  color: #fff;
}

.chat-attachment-input {
  display: none;
}

.chat-attachment-preview {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #f8fbf6;
  font-size: 0.84rem;
}

.chat-attachment-preview.hidden {
  display: none;
}

.chat-attachment-name {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-attachment-clear {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0;
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

body.dark-mode .chat-attachment-preview {
  background: #1f3229;
}

.chat-lightbox {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 180;
}

.chat-lightbox.hidden {
  display: none;
}

.chat-lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
}

.chat-lightbox-panel {
  position: relative;
  z-index: 1;
  width: min(560px, calc(100vw - 32px));
  max-height: min(84vh, 900px);
  border-radius: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  overflow: hidden;
  box-shadow: 0 24px 50px rgba(0, 0, 0, 0.35);
}

.chat-lightbox-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  z-index: 2;
}

.chat-lightbox-body {
  display: grid;
  gap: 10px;
  max-height: min(84vh, 900px);
  overflow: auto;
  padding: 18px;
}

.chat-lightbox-item img {
  width: 100%;
  display: block;
  border-radius: 12px;
}

@media (max-width: 720px) {
  .chat-panel {
    right: 12px;
    bottom: calc(58px + env(safe-area-inset-bottom));
    width: calc(100vw - 24px);
    height: min(78dvh, 720px);
    max-height: min(78dvh, 720px);
  }

  .chat-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: 100%;
  }

  .chat-left {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 8px 10px;
    overflow-y: hidden;
  }

  .chat-list {
    grid-auto-flow: column;
    grid-auto-columns: var(--chat-item-size);
    max-height: none;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 2px;
  }

  .chat-right {
    padding: 10px;
    min-height: 0;
    height: auto;
  }

  .chat-messages {
    padding-right: 0;
  }

  .chat-message {
    grid-template-columns: 28px 1fr auto;
    padding: 6px 0;
  }

  .chat-message .avatar {
    width: 28px;
    height: 28px;
  }

  .chat-message .text,
  .chat-image-grid {
    max-width: 100%;
    width: 100%;
  }

  .chat-message .text {
    padding: 8px 9px;
  }

  .chat-message .actions {
    opacity: 1;
    pointer-events: auto;
  }

  .chat-input textarea {
    min-height: 52px;
    max-height: 120px;
    padding: 14px 48px 14px 46px;
  }

  .chat-input .attach-trigger,
  .chat-input .send-btn {
    bottom: 8px;
    width: 32px;
    height: 32px;
  }

  .chat-input .attach-trigger {
    left: 8px;
  }

  .chat-input .send-btn {
    right: 8px;
  }

  .chat-attachment-preview {
    width: 100%;
    border-radius: 12px;
  }

  .chat-attachment-name {
    max-width: none;
    flex: 1 1 auto;
  }

  .chat-lightbox-panel {
    width: calc(100vw - 24px);
    max-height: min(86dvh, 900px);
  }

  .chat-lightbox-body {
    padding: 14px;
  }
}

@media (max-width: 480px) {
  .chat-panel {
    right: 10px;
    width: calc(100vw - 20px);
    height: min(80dvh, 760px);
    max-height: min(80dvh, 760px);
  }

  .chat-left {
    padding: 8px;
  }

  .chat-right {
    padding: 8px;
  }

  .chat-body {
    --chat-item-size: 46px;
    --chat-gap: 6px;
  }

  .chat-head {
    padding: 10px 12px;
  }

  .chat-message .meta {
    gap: 4px 6px;
  }

  .chat-message .time,
  .chat-message .edited {
    font-size: 0.7rem;
  }

  .chat-message .text.attachment-only {
    min-width: 0;
  }

  .chat-image-grid {
    gap: 3px;
  }

  .chat-lightbox-panel {
    width: calc(100vw - 20px);
  }

  .chat-lightbox-close {
    top: 8px;
    right: 8px;
  }
}

.chat-left .chat-item > div {
  display: none;
}
