/* ── Reset & Base ──────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  font-size: 14px;
  color: #111b21;
  background: #dfe5d7;
  overflow: hidden;
}

/* ── Reconnect Screen ────────────────────────────────────────── */
#reconnect-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  background: linear-gradient(180deg, #00a884 0%, #00a884 220px, #dfe5d7 220px);
}

#reconnect-screen.hidden { display: none; }

.account-card {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 4px 24px rgba(0,0,0,.12);
  padding: 48px 56px;
  text-align: center;
  max-width: 520px;
  width: 90%;
}

.account-card h1 {
  font-size: 20px;
  font-weight: 400;
  color: #41525d;
  margin-bottom: 8px;
}

.account-card > p {
  color: #667781;
  margin-bottom: 24px;
  font-size: 14px;
}

#account-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 24px;
  max-height: 300px;
  overflow-y: auto;
}

#account-list:empty::after {
  content: 'Nessun account salvato';
  color: #a0a0a0;
  font-size: 13px;
  padding: 16px 0;
  display: block;
}

.account-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  text-align: left;
  transition: background .1s;
}

.account-item:hover { background: #f0f2f5; }

.account-item .account-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #00a884;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.account-item .account-name {
  flex: 1;
  font-size: 15px;
  font-weight: 500;
  color: #111b21;
}

.account-item .btn { font-size: 12px; padding: 5px 12px; }

.btn-danger {
  background: #f0f2f5;
  color: #c0392b;
}

.btn-danger:hover { background: #fde8e8; }

.account-new {
  display: flex;
  gap: 8px;
  align-items: center;
}

.account-new input {
  flex: 1;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
}

.account-new input:focus { border-color: #00a884; }

.account-new .btn-primary {
  white-space: nowrap;
  padding: 10px 18px;
}

/* ── Login Screen ─────────────────────────────────────────────── */
#login-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  background: linear-gradient(180deg, #00a884 0%, #00a884 220px, #dfe5d7 220px);
}

#login-screen .login-card {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 4px 24px rgba(0,0,0,.12);
  padding: 48px 56px;
  text-align: center;
  max-width: 480px;
  width: 90%;
}

#login-screen .login-card h1 {
  font-size: 20px;
  font-weight: 400;
  color: #41525d;
  margin-bottom: 24px;
}

#login-screen .login-card p {
  color: #667781;
  margin-bottom: 16px;
  font-size: 14px;
}

#qr-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 280px;
}

#qr-container img {
  border-radius: 4px;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid #e0e0e0;
  border-top-color: #00a884;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

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

#login-status {
  margin-top: 16px;
  font-size: 13px;
  color: #667781;
}

/* ── Chat Screen (Main Layout) ────────────────────────────────── */
#chat-screen {
  display: none;
  height: 100dvh;
  flex-direction: column;
}

#chat-screen.active {
  display: flex;
}

#login-screen.hidden { display: none; }

/* Top bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #00a884;
  color: #fff;
  padding: 8px 16px;
  font-size: 13px;
  flex-shrink: 0;
}

.top-bar .status-text {
  display: flex;
  align-items: center;
  gap: 8px;
}

.top-bar .status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #25d366;
  display: inline-block;
}

.top-bar .status-dot.offline { background: #f44336; }

.account-badge {
  background: rgba(255,255,255,.2);
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.top-bar .actions { display: flex; gap: 8px; }

.btn {
  border: none;
  border-radius: 4px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
  transition: background .15s;
}

.btn-light {
  background: rgba(255,255,255,.2);
  color: #fff;
}

.btn-light:hover { background: rgba(255,255,255,.35); }

.btn-primary {
  background: #00a884;
  color: #fff;
}

.btn-primary:hover { background: #00957a; }

/* Main content */
.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Sidebar ──────────────────────────────────────────────────── */
.sidebar {
  width: 340px;
  min-width: 260px;
  background: #fff;
  border-right: 1px solid #e0e0e0;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: #f0f2f5;
  border-bottom: 1px solid #e0e0e0;
}

.sidebar-header input {
  flex: 1;
  border: none;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  background: #fff;
  outline: none;
  font-family: inherit;
}

.sidebar-header input::placeholder { color: #a0a0a0; }

#btn-new-chat {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: #00a884;
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s;
}

#btn-new-chat:hover { background: #00957a; }

.chat-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

.chat-list::-webkit-scrollbar { width: 6px; }
.chat-list::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }

.chat-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid #f0f2f5;
  transition: background .1s;
  gap: 12px;
}

.chat-item:hover { background: #f0f2f5; }

.chat-item.active { background: #e9edef; }

.chat-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #dfe5e7;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: #fff;
  flex-shrink: 0;
  font-weight: 600;
  position: relative;
  overflow: hidden;
}

.chat-avatar.group { background: #00a884; }

.avatar-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
  display: none; /* shown by onload */
}

.avatar-fallback {
  z-index: 0;
}

.chat-info {
  flex: 1;
  min-width: 0;
}

.chat-info-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 2px;
}

.chat-name {
  font-size: 16px;
  font-weight: 400;
  color: #111b21;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-time {
  font-size: 12px;
  color: #667781;
  white-space: nowrap;
  margin-left: 8px;
}

.chat-info-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-preview {
  font-size: 13px;
  color: #667781;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.unread-badge {
  background: #25d366;
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  border-radius: 50%;
  min-width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  margin-left: 8px;
  flex-shrink: 0;
}

/* ── Chat Panel (right side) ──────────────────────────────────── */
.chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #efeae2;
  min-width: 0;
}

/* Empty state */
.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #667781;
  gap: 12px;
}

.chat-empty .icon {
  font-size: 64px;
  opacity: .4;
}

.chat-empty p {
  font-size: 14px;
}

/* Chat header */
.chat-header {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  background: #f0f2f5;
  border-bottom: 1px solid #e0e0e0;
  gap: 12px;
  flex-shrink: 0;
}

.chat-header .chat-avatar { width: 40px; height: 40px; font-size: 15px; }

.chat-header-info { flex: 1; min-width: 0; }

.chat-header-name {
  font-size: 16px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-header-status {
  font-size: 12px;
  color: #667781;
}

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.chat-header-actions .btn-icon {
  font-size: 18px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  transition: background .15s;
}

.chat-header-actions .btn-icon:hover {
  background: #dfe5d7;
}

.chat-header-actions .btn-icon.spinning {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

/* Messages area */
.messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 16px 48px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: #efeae2 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cg fill='%23d6cec2' fill-opacity='.15'%3E%3Ccircle cx='25' cy='25' r='3'/%3E%3Ccircle cx='75' cy='75' r='3'/%3E%3Ccircle cx='125' cy='125' r='3'/%3E%3Ccircle cx='175' cy='175' r='3'/%3E%3Ccircle cx='225' cy='25' r='3'/%3E%3Ccircle cx='275' cy='75' r='3'/%3E%3Ccircle cx='25' cy='125' r='3'/%3E%3Ccircle cx='75' cy='175' r='3'/%3E%3C/g%3E%3C/svg%3E");
}

.messages-area::-webkit-scrollbar { width: 6px; }
.messages-area::-webkit-scrollbar-thumb { background: rgba(0,0,0,.2); border-radius: 3px; }

/* ── Message row (wraps checkbox + bubble) ── */
.message-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}
.message-row.sent    { justify-content: flex-end; }
.message-row.received { justify-content: flex-start; }

.bubble-checkbox {
  flex-shrink: 0;
  display: none;
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: #00a884;
}
.messages-area.selection-mode .bubble-checkbox       { display: block; }
.messages-area.selection-mode .message-row           { justify-content: flex-start; }
.messages-area.selection-mode .message-row.sent .message-bubble { margin-left: auto; }

.message-bubble {
  max-width: 65%;
  padding: 6px 8px 8px;
  border-radius: 8px;
  position: relative;
  word-wrap: break-word;
  box-shadow: 0 1px 1px rgba(0,0,0,.08);
  line-height: 1.4;
}
.message-bubble.has-media { max-width: 80%; }

.message-bubble.sent {
  background: #d9fdd3;
  border-top-right-radius: 0;
}

.message-bubble.received {
  background: #fff;
  border-top-left-radius: 0;
}

.message-author {
  font-size: 12px;
  font-weight: 600;
  color: #00a884;
  margin-bottom: 2px;
}

.message-text {
  font-size: 14px;
  white-space: pre-wrap;
}

.message-meta {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 4px;
  margin-top: 2px;
}

.star-icon {
  font-size: 10px;
}

.message-time {
  font-size: 11px;
  color: #667781;
}

.message-ack {
  font-size: 13px;
  color: #667781;
}

.message-ack.read { color: #53bdeb; }

/* ── Media in messages ─────────────────────────────────────────── */

/* Image preview */
.message-media-img {
  max-width: 100%;
  max-height: 300px;
  border-radius: 6px;
  cursor: pointer;
  display: block;
  margin-bottom: 4px;
  background: #e0e0e0;
}

/* Video player */
.message-media-video {
  max-width: 100%;
  max-height: 300px;
  border-radius: 6px;
  display: block;
  margin-bottom: 4px;
  background: #000;
}

/* Audio player */
.message-audio-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 240px;
  padding: 4px 0;
}

.btn-audio-play {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: #00a884;
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
}

.btn-audio-play:hover { background: #00957a; }
.btn-audio-play:disabled { background: #a0a0a0; cursor: wait; }

.audio-status {
  flex: 1;
  font-size: 12px;
  color: #667781;
  min-width: 0;
}

.audio-player-slot {
  margin-top: 4px;
}

.audio-player-slot audio {
  width: 100%;
  height: 36px;
}

/* Sticker */
.message-media-sticker {
  max-width: 160px;
  max-height: 160px;
  display: block;
  margin-bottom: 4px;
}

/* Document / generic file card */
.message-file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(0,0,0,.04);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 4px;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  min-width: 200px;
  transition: background .15s;
}

.message-file-card:hover { background: rgba(0,0,0,.08); }

.message-file-icon {
  font-size: 28px;
  flex-shrink: 0;
}

.message-file-info {
  flex: 1;
  min-width: 0;
}

.message-file-name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.message-file-type {
  font-size: 11px;
  color: #667781;
  text-transform: uppercase;
}

.message-file-dl {
  font-size: 20px;
  color: #667781;
  flex-shrink: 0;
}

/* Media loading placeholder */
.media-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px;
  color: #667781;
  font-size: 13px;
}

.media-loading .spinner-sm {
  width: 18px;
  height: 18px;
  border: 2px solid #e0e0e0;
  border-top-color: #00a884;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

/* Lightbox for full-size images */
.lightbox-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.85);
  z-index: 300;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  flex-direction: column;
  gap: 12px;
}

.lightbox-overlay.active { display: flex; }

.lightbox-overlay img {
  max-width: 90vw;
  max-height: 85vh;
  border-radius: 4px;
  box-shadow: 0 4px 24px rgba(0,0,0,.5);
}

.lightbox-actions {
  display: flex;
  gap: 16px;
}

.lightbox-actions a {
  color: #fff;
  text-decoration: none;
  font-size: 14px;
  padding: 8px 16px;
  background: rgba(255,255,255,.15);
  border-radius: 6px;
  transition: background .15s;
}

.lightbox-actions a:hover { background: rgba(255,255,255,.3); }

.lightbox-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0,0,0,.45);
  border: none;
  color: #fff;
  font-size: 48px;
  line-height: 1;
  padding: 10px 16px;
  cursor: pointer;
  border-radius: 8px;
  z-index: 10;
  transition: background .15s;
  user-select: none;
}
.lightbox-nav:hover { background: rgba(0,0,0,.75); }
.lightbox-nav[hidden] { display: none; }
#lightbox-prev { left: 20px; }
#lightbox-next { right: 20px; }

#lightbox-video {
  max-width: 90vw;
  max-height: 85vh;
  border-radius: 4px;
  box-shadow: 0 4px 24px rgba(0,0,0,.5);
}

/* Action buttons under media */
.btn-media-action {
  border: none;
  background: rgba(0,0,0,.06);
  color: #00a884;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
  margin-top: 4px;
  transition: background .15s;
}

.btn-media-action:hover { background: rgba(0,0,0,.12); }

.media-img-actions {
  display: flex;
  gap: 6px;
  margin-top: 2px;
}

/* Audio download button */
.btn-audio-dl {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 16px;
  color: #667781;
  padding: 4px;
  flex-shrink: 0;
  border-radius: 50%;
  transition: background .15s;
}

.btn-audio-dl:hover { background: rgba(0,0,0,.08); }

/* PDF embed */
.pdf-preview-container {
  margin-top: 6px;
}

.pdf-embed {
  width: 100%;
  height: 520px;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
}

.btn-pdf-toggle {
  border: none;
  background: rgba(0,0,0,.06);
  color: #00a884;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
  margin-top: 4px;
  display: block;
  transition: background .15s;
}

.btn-pdf-toggle:hover { background: rgba(0,0,0,.12); }

/* File card clickable */
.message-file-card[role="button"] {
  cursor: pointer;
}

.message-media-label {
  font-size: 12px;
  color: #667781;
  font-style: italic;
  margin-bottom: 2px;
}

/* Date divider */
.btn-load-more {
  display: block;
  margin: 10px auto;
  padding: 6px 18px;
  background: rgba(0,0,0,.06);
  border: none;
  border-radius: 16px;
  font-size: 12px;
  color: #667781;
  cursor: pointer;
  font-family: inherit;
  transition: background .15s;
}
.btn-load-more:hover { background: rgba(0,0,0,.12); }
.btn-load-more:disabled { opacity: .5; cursor: default; }

.date-divider {
  align-self: center;
  background: #fff;
  color: #667781;
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 8px;
  margin: 8px 0;
  box-shadow: 0 1px 1px rgba(0,0,0,.08);
}

/* ── Input Bar ────────────────────────────────────────────────── */
.input-bar {
  display: flex;
  align-items: flex-end;
  padding: 8px 16px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  background: #f0f2f5;
  gap: 8px;
  flex-shrink: 0;
}

.input-bar .btn-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #54656f;
  flex-shrink: 0;
  transition: background .15s;
}

.input-bar .btn-icon:hover { background: #e0e0e0; }

.input-bar textarea {
  flex: 1;
  border: none;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  outline: none;
  min-height: 40px;
  max-height: 120px;
  line-height: 1.4;
  background: #fff;
}

.input-bar textarea::placeholder { color: #a0a0a0; }

#btn-send {
  background: #00a884;
  color: #fff;
}

#btn-send:hover { background: #00957a; }

/* ── Modals ───────────────────────────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 100;
  align-items: center;
  justify-content: center;
}

.modal-overlay.active {
  display: flex;
}

.modal {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
  padding: 24px;
  min-width: 360px;
  max-width: 440px;
  width: 90%;
}

.modal h2 {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 16px;
  color: #111b21;
}

.modal input[type="text"] {
  width: 100%;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  margin-bottom: 12px;
}

.modal input[type="text"]:focus { border-color: #00a884; }

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

.modal .result-box {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
}

.result-box.success { background: #d9fdd3; color: #1a6e3a; }
.result-box.failure { background: #fde8e8; color: #c0392b; }

.msg-info-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 8px 0;
  border-bottom: 1px solid #f0f2f5;
  gap: 12px;
}
.msg-info-row:last-child { border-bottom: none; }
.msg-info-label {
  font-size: 12px;
  color: #667781;
  white-space: nowrap;
  padding-top: 1px;
  min-width: 90px;
}
.msg-info-value {
  font-size: 13px;
  color: #111b21;
  text-align: right;
  word-break: break-all;
}
.msg-info-value.mono {
  font-family: monospace;
  font-size: 11px;
  color: #667781;
}

.btn-cancel {
  background: #f0f2f5;
  color: #667781;
}

.btn-cancel:hover { background: #e0e0e0; }

/* Attachment options */
.attach-options {
  display: none;
  position: absolute;
  bottom: 60px;
  left: 16px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,.15);
  padding: 8px 0;
  z-index: 50;
}

.attach-options.active { display: block; }

.attach-options label,
.attach-options button {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
  cursor: pointer;
  font-size: 14px;
  color: #111b21;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
}

.attach-options label:hover,
.attach-options button:hover {
  background: #f0f2f5;
}

/* File preview */
.file-preview {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: #e7f8e9;
  border-radius: 8px 8px 0 0;
  font-size: 13px;
  color: #111b21;
}

.file-preview.active { display: flex; }

.file-preview .file-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.file-preview .btn-remove-file {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 18px;
  color: #667781;
  padding: 0 4px;
}

/* Caption input */
.caption-bar {
  display: none;
  padding: 6px 16px;
  background: #f0f2f5;
}

.caption-bar.active { display: block; }

.caption-bar input {
  width: 100%;
  border: none;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  background: #fff;
}

/* Document checkbox */
.doc-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 16px;
  font-size: 12px;
  color: #667781;
  background: #f0f2f5;
}

.doc-toggle.active { display: flex; }

/* ── Toast / Notification ─────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: #323739;
  color: #fff;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 4px 12px rgba(0,0,0,.3);
  animation: slideIn .3s ease;
}

.toast.error { background: #c0392b; }

@keyframes slideIn {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* ── Context Menu ────────────────────────────────────────────── */
.ctx-menu {
  display: none;
  position: fixed;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,.18);
  padding: 6px 0;
  z-index: 250;
  min-width: 200px;
}

.ctx-menu.active { display: block; }

.ctx-menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  cursor: pointer;
  font-size: 14px;
  color: #111b21;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
  white-space: nowrap;
}

.ctx-menu-item:hover { background: #f0f2f5; }

.ctx-menu-item:first-child { border-radius: 12px 12px 0 0; }
.ctx-menu-item:last-child { border-radius: 0 0 12px 12px; }

.ctx-menu-item.danger { color: #c0392b; }
.ctx-menu-item.danger:hover { background: #fde8e8; }

.ctx-menu-icon { font-size: 16px; width: 20px; text-align: center; flex-shrink: 0; }

.ctx-menu-sep {
  height: 1px;
  background: #e0e0e0;
  margin: 4px 0;
}

.ctx-menu-info {
  padding: 6px 20px;
  font-size: 12px;
  color: #667781;
  white-space: nowrap;
  user-select: none;
}

.emoji-input-picker {
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  width: 300px;
  max-height: 240px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,.15);
  padding: 6px;
  z-index: 200;
  flex-wrap: wrap;
  gap: 2px;
}
.emoji-input-picker.active { display: flex; }

.emoji-input-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 22px;
  padding: 4px 5px;
  border-radius: 6px;
  line-height: 1;
  transition: background .1s;
}
.emoji-input-btn:hover { background: #f0f2f5; }

/* Message dropdown button (hover trigger) */
.msg-menu-btn {
  display: none;
  position: absolute;
  top: 2px;
  right: 2px;
  width: 24px;
  height: 24px;
  border: none;
  background: inherit;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  color: #8696a0;
  align-items: center;
  justify-content: center;
  z-index: 2;
  line-height: 1;
}

.message-bubble:hover .msg-menu-btn { display: flex; }
.msg-menu-btn:hover { background: rgba(0,0,0,.08); }

/* ── Reply / Edit bar ────────────────────────────────────────── */
.reply-bar {
  display: none;
  align-items: center;
  padding: 8px 16px;
  background: #f0f2f5;
  border-left: 4px solid #00a884;
  gap: 10px;
  flex-shrink: 0;
}

.reply-bar.active { display: flex; }
.reply-bar.editing { border-left-color: #e8a838; }

.reply-bar-content { flex: 1; min-width: 0; }

.reply-bar-author {
  font-size: 12px;
  font-weight: 600;
  color: #00a884;
}

.reply-bar.editing .reply-bar-author { color: #e8a838; }

.reply-bar-text {
  font-size: 13px;
  color: #667781;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reply-bar-close {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 18px;
  color: #667781;
  padding: 2px 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

.reply-bar-close:hover { background: rgba(0,0,0,.08); }

/* ── Reaction Picker ─────────────────────────────────────────── */
.reaction-picker {
  display: none;
  position: fixed;
  background: #fff;
  border-radius: 28px;
  box-shadow: 0 4px 18px rgba(0,0,0,.2);
  padding: 4px 8px;
  z-index: 260;
  gap: 2px;
}

.reaction-picker.active { display: flex; }

.reaction-btn {
  border: none;
  background: none;
  font-size: 26px;
  cursor: pointer;
  padding: 6px 6px;
  border-radius: 50%;
  transition: transform .15s, background .15s;
  line-height: 1;
}

.reaction-btn:hover {
  transform: scale(1.35);
  background: #f0f2f5;
}

/* ── Forward Modal chat list ─────────────────────────────────── */
.forward-chat-list {
  max-height: 320px;
  overflow-y: auto;
  margin: 8px -24px;
  padding: 0 24px;
}

.forward-chat-list::-webkit-scrollbar { width: 6px; }
.forward-chat-list::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }

.forward-chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  cursor: pointer;
  border-radius: 8px;
  transition: background .1s;
}

.forward-chat-item:hover { background: #f0f2f5; }

.forward-chat-item .chat-avatar { width: 40px; height: 40px; font-size: 14px; }

.forward-chat-item .chat-name {
  font-size: 14px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Selection Mode ──────────────────────────────────────────── */
.selection-bar {
  display: none;
  align-items: center;
  padding: 10px 16px;
  background: #f0f2f5;
  border-top: 1px solid #e0e0e0;
  gap: 12px;
  flex-shrink: 0;
}

.selection-bar.active { display: flex; }

.selection-count {
  flex: 1;
  font-size: 14px;
  color: #111b21;
  font-weight: 500;
}

.selection-actions { display: flex; gap: 8px; }

/* Bubbles in selection mode */
.messages-area.selection-mode .message-bubble {
  cursor: pointer;
  transition: background .15s;
}

.messages-area.selection-mode .message-row.selected .message-bubble.sent {
  background: #a8e6a3 !important;
}

.messages-area.selection-mode .message-row.selected .message-bubble.received {
  background: #d0e8ff !important;
}

.messages-area.selection-mode .msg-menu-btn { display: none !important; }

/* ── Reaction Badges (under bubbles) ─────────────────────────── */
.reactions-container {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
  margin-bottom: -2px;
}

.reaction-badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  padding: 2px 6px;
  font-size: 14px;
  line-height: 1;
  cursor: default;
  box-shadow: 0 1px 2px rgba(0,0,0,.06);
}

.reaction-badge .reaction-count {
  font-size: 11px;
  color: #667781;
  font-weight: 500;
}

.message-bubble.sent .reaction-badge {
  background: #c5f0bd;
  border-color: #b0e0a8;
}

/* ── Back button (mobile only) ───────────────────────────────── */
#btn-back-chat {
  display: none;
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 22px;
  color: #54656f;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: 50%;
  transition: background .15s;
}
#btn-back-chat:hover { background: rgba(0,0,0,.08); }

/* ── PWA Standalone (nessuna barra browser) ──────────────────── */
@media (display-mode: standalone) {
  .top-bar { padding: 6px 10px; gap: 6px; }
  #btn-verify, #btn-refresh-chats { display: none; }
  .account-badge { display: none; }

  .main-content { position: relative; overflow: hidden; }

  .sidebar {
    width: 100%; min-width: 0;
    position: absolute; inset: 0; z-index: 1;
    transform: translateX(0); transition: transform .25s ease;
  }

  .chat-panel {
    width: 100%; position: absolute; inset: 0; z-index: 2;
    transform: translateX(100%); transition: transform .25s ease;
  }

  #chat-screen.mobile-chat-open .chat-panel { transform: translateX(0); }
  #chat-screen.mobile-chat-open .sidebar   { transform: translateX(-100%); }

  #btn-back-chat { display: flex; }

  .messages-area  { padding: 10px 8px; }
  .message-bubble { max-width: 88%; }
  .input-bar      { padding: 6px 8px; gap: 4px; padding-bottom: env(safe-area-inset-bottom); }

  .modal { min-width: 0; width: 94%; padding: 20px 16px; }
  #login-screen .login-card,
  #reconnect-screen .login-card,
  .account-card { padding: 32px 20px; }

  .selection-bar { flex-wrap: wrap; }
  .selection-actions { flex-wrap: wrap; gap: 4px; }
  .selection-actions .btn { font-size: 12px; padding: 5px 10px; }

  .toast-container { right: 12px; left: 12px; bottom: 16px; }
  .toast { text-align: center; }

  #reconnect-screen { background: linear-gradient(180deg, #00a884 0%, #00a884 160px, #dfe5d7 160px); }
  #login-screen     { background: linear-gradient(180deg, #00a884 0%, #00a884 160px, #dfe5d7 160px); }
}

/* ── Responsive Mobile (≤ 768px) ─────────────────────────────── */
@media (max-width: 768px) {

  /* Top bar: nascondi bottoni secondari, compatta padding */
  .top-bar { padding: 6px 10px; gap: 6px; }
  #btn-verify, #btn-refresh-chats { display: none; }
  .account-badge { display: none; }

  /* Layout principale: sidebar e chat panel sovrapposti a piena larghezza */
  .main-content {
    position: relative;
    overflow: hidden;
  }

  .sidebar {
    width: 100%;
    min-width: 0;
    position: absolute;
    inset: 0;
    z-index: 1;
    transform: translateX(0);
    transition: transform .25s ease;
  }

  .chat-panel {
    width: 100%;
    position: absolute;
    inset: 0;
    z-index: 2;
    transform: translateX(100%);
    transition: transform .25s ease;
  }

  /* Quando una chat è aperta: mostra pannello, nascondi sidebar */
  #chat-screen.mobile-chat-open .chat-panel {
    transform: translateX(0);
  }
  #chat-screen.mobile-chat-open .sidebar {
    transform: translateX(-100%);
  }

  /* Back button visibile su mobile */
  #btn-back-chat { display: flex; }

  /* Messaggi: padding ridotto */
  .messages-area { padding: 10px 8px; }

  /* Bolle più larghe */
  .message-bubble { max-width: 88%; }

  /* Input bar compatta */
  .input-bar { padding: 6px 8px; gap: 4px; }

  /* Modali a piena larghezza */
  .modal { min-width: 0; width: 94%; padding: 20px 16px; }

  /* Login/reconnect card: meno padding */
  #login-screen .login-card,
  #reconnect-screen .login-card,
  .account-card { padding: 32px 20px; }

  /* Selection bar: wrap */
  .selection-bar { flex-wrap: wrap; }
  .selection-actions { flex-wrap: wrap; gap: 4px; }
  .selection-actions .btn { font-size: 12px; padding: 5px 10px; }

  /* Toast centrato */
  .toast-container { right: 12px; left: 12px; bottom: 16px; }
  .toast { text-align: center; }

  /* Reconnect screen: barra verde più bassa */
  #reconnect-screen {
    background: linear-gradient(180deg, #00a884 0%, #00a884 160px, #dfe5d7 160px);
  }
  #login-screen {
    background: linear-gradient(180deg, #00a884 0%, #00a884 160px, #dfe5d7 160px);
  }
}
