* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  background: #111b21;
  color: #e9edef;
  height: 100vh;
  overflow: hidden;
}

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Login */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: #111b21;
}

.login-box {
  background: #1f2c33;
  padding: 40px;
  border-radius: 8px;
  width: 400px;
  max-width: 90vw;
}

.login-box h1 {
  font-size: 20px;
  margin-bottom: 8px;
  color: #e9edef;
}

.login-box p {
  font-size: 13px;
  color: #8696a0;
  margin-bottom: 24px;
}

.login-box input {
  width: 100%;
  padding: 12px;
  border: 1px solid #2a3942;
  border-radius: 6px;
  background: #2a3942;
  color: #e9edef;
  font-size: 14px;
  outline: none;
  margin-bottom: 16px;
}

.login-box input:focus {
  border-color: #00a884;
}

.login-box .error {
  color: #ef4444;
  font-size: 13px;
  margin-bottom: 12px;
}

/* Buttons */
.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: opacity 0.2s;
}

.btn:hover {
  opacity: 0.85;
}

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

.btn-primary {
  background: #00a884;
  color: #111b21;
  font-weight: 600;
}

.btn-block {
  width: 100%;
}

.btn-danger {
  background: #ef4444;
  color: #fff;
  font-size: 12px;
  padding: 6px 12px;
}

.btn-secondary {
  background: #2a3942;
  color: #e9edef;
  font-size: 12px;
  padding: 6px 12px;
}

/* Instances */
.instances-screen {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: #202c33;
  border-bottom: 1px solid #2a3942;
}

.topbar h1 {
  font-size: 16px;
  font-weight: 600;
}

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

.instances-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
}

.instance-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: #1f2c33;
  border-radius: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
  gap: 10px;
}

.instance-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.instance-info h3 {
  font-size: 14px;
  font-weight: 500;
}

.instance-info .phone {
  font-size: 12px;
  color: #8696a0;
}

.status-badge {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-badge.connected { background: #00a884; }
.status-badge.connecting, .status-badge.qr_ready { background: #f59e0b; }
.status-badge.disconnected { background: #ef4444; }
.status-badge.error { background: #be123c; animation: status-badge-pulse 1.5s ease-in-out infinite; }

@keyframes status-badge-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.connection-banner {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  background: #202c33;
  color: #f59e0b;
  border: 1px solid #f59e0b;
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  animation: connection-banner-in 0.2s ease-out;
}

.connection-banner::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #f59e0b;
  animation: status-badge-pulse 1.2s ease-in-out infinite;
}

@keyframes connection-banner-in {
  from { opacity: 0; transform: translateX(-50%) translateY(-8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.instance-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

/* New instance modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal {
  background: #1f2c33;
  padding: 24px;
  border-radius: 8px;
  width: 360px;
  max-width: 90vw;
}

.modal h2 {
  font-size: 16px;
  margin-bottom: 16px;
}

.modal input {
  width: 100%;
  padding: 10px;
  border: 1px solid #2a3942;
  border-radius: 6px;
  background: #2a3942;
  color: #e9edef;
  font-size: 14px;
  outline: none;
  margin-bottom: 16px;
}

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

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

/* QR inline */
.qr-container {
  margin-top: 10px;
  text-align: center;
}

.qr-container img {
  max-width: 200px;
  border-radius: 8px;
}

.qr-container p {
  font-size: 12px;
  color: #8696a0;
  margin-top: 6px;
}

/* Chat screen */
.chat-screen {
  display: flex;
  height: 100vh;
}

/* Chat list (left panel) */
.chat-list-panel {
  width: 340px;
  min-width: 280px;
  background: #111b21;
  border-right: 1px solid #2a3942;
  display: flex;
  flex-direction: column;
  transition: transform 0.2s ease;
}

.chat-list-header {
  padding: 12px 16px;
  background: #202c33;
  border-bottom: 1px solid #2a3942;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 56px;
}

.chat-list-header h2 {
  font-size: 15px;
  font-weight: 600;
}

/* Sync names button */
.btn-sync {
  background: none;
  border: none;
  padding: 6px;
  cursor: pointer;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.btn-sync svg {
  fill: #8696a0;
  transition: fill 0.2s;
}

.btn-sync:hover svg {
  fill: #00a884;
}

.btn-sync:disabled {
  cursor: not-allowed;
}

.btn-sync-spinning svg {
  animation: spin 0.8s linear infinite;
}

/* Chat tabs */
.chat-tabs {
  display: flex;
  background: #111b21;
  border-bottom: 1px solid #2a3942;
  padding: 0 8px;
}

.chat-tab {
  flex: 1;
  padding: 10px 0;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: #8696a0;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.chat-tab:hover {
  color: #e9edef;
}

.chat-tab.active {
  color: #00a884;
  border-bottom-color: #00a884;
}

/* Search box in chat list */
.chat-search {
  padding: 8px 12px;
  background: #111b21;
  border-bottom: 1px solid #2a3942;
}

.chat-search input {
  width: 100%;
  padding: 8px 12px;
  border: none;
  border-radius: 6px;
  background: #202c33;
  color: #e9edef;
  font-size: 13px;
  outline: none;
}

.chat-search input::placeholder {
  color: #8696a0;
}

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

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

.chat-item:hover {
  background: #202c33;
}

.chat-item.active {
  background: #2a3942;
}

.chat-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #00a884;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  color: #111b21;
  flex-shrink: 0;
  margin-right: 12px;
  overflow: hidden;
}

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

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

.chat-preview-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.chat-preview h3 {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-preview .time {
  font-size: 11px;
  color: #8696a0;
  flex-shrink: 0;
  margin-left: 8px;
}

.chat-preview .last-msg {
  font-size: 13px;
  color: #8696a0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.unread-badge {
  background: #00a884;
  color: #111b21;
  font-size: 11px;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  margin-left: 8px;
}

/* Mobile back button in messages header (hidden on desktop) */
.msg-back-btn {
  display: none;
  background: none;
  border: none;
  color: #00a884;
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px 4px 0;
  margin-right: 8px;
  flex-shrink: 0;
}

/* Messages panel (right) */
.messages-panel {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: #0b141a;
  overflow: hidden;
}

.messages-panel-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #8696a0;
  font-size: 14px;
  flex-direction: column;
  gap: 8px;
}

.messages-panel-empty svg {
  width: 48px;
  height: 48px;
  fill: #2a3942;
}

.messages-header {
  padding: 12px 16px;
  background: #202c33;
  border-bottom: 1px solid #2a3942;
  display: flex;
  align-items: center;
  min-height: 56px;
}

.header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  margin-right: 10px;
  overflow: hidden;
}

.header-avatar:empty {
  display: none;
}

.header-avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.messages-header-info {
  min-width: 0;
  overflow: hidden;
}

.messages-header h2 {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.messages-header .jid {
  font-size: 12px;
  color: #8696a0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.btn-mark-read {
  margin-left: 10px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 5px;
  background: none;
  border: 1px solid #2a3942;
  border-radius: 6px;
  color: #8696a0;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.chat-toggle {
  margin-left: auto;
  margin-right: 14px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #8696a0;
}

.chat-toggle:last-of-type {
  margin-right: 0;
}

.toggle-switch {
  position: relative;
  width: 34px;
  height: 19px;
  border-radius: 999px;
  background: #4a5a63;
  border: none;
  cursor: pointer;
  padding: 0;
  transition: background 0.2s;
  flex-shrink: 0;
}

.toggle-switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: #e9edef;
  transition: transform 0.2s;
}

.toggle-switch.on {
  background: #00a884;
}

.toggle-switch.on::after {
  transform: translateX(15px);
}

.toggle-switch:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-mark-read svg {
  fill: #8696a0;
  transition: fill 0.2s;
}

.btn-mark-read:hover {
  border-color: #00a884;
  color: #00a884;
}

.btn-mark-read:hover svg {
  fill: #00a884;
}

.btn-mark-read:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-mark-read-done {
  border-color: #00a884;
  color: #00a884;
}

.btn-mark-read-done svg {
  fill: #00a884;
}

.messages-body-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.messages-body {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sticky-date-header {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  background: rgba(32, 44, 51, 0.92);
  color: #e9edef;
  font-size: 12px;
  font-weight: 500;
  padding: 5px 14px;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.sticky-date-header.visible {
  opacity: 1;
}

.date-divider {
  display: flex;
  justify-content: center;
  margin: 8px 0;
}

.date-divider span {
  background: #1f2c33;
  color: #8696a0;
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 8px;
}

.message-bubble {
  max-width: 65%;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  overflow-wrap: break-word;
  word-break: break-word;
  min-width: 0;
  position: relative;
}

.message-bubble.sent {
  background: #005c4b;
  align-self: flex-end;
  border-bottom-right-radius: 2px;
}

.message-bubble.received {
  background: #1f2c33;
  align-self: flex-start;
  border-bottom-left-radius: 2px;
}

.message-bubble .msg-time {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.5);
  text-align: right;
  margin-top: 4px;
}

.message-bubble .msg-sender {
  font-size: 11px;
  color: #00a884;
  margin-bottom: 2px;
  font-weight: 600;
}

.msg-quote {
  border-left: 3px solid #00a884;
  background: rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  padding: 5px 8px;
  margin-bottom: 5px;
  font-size: 12.5px;
  color: rgba(255, 255, 255, 0.65);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.view-once-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.6);
  margin-bottom: 4px;
}

.view-once-badge svg {
  fill: currentColor;
}

/* Media in messages */
.msg-image-wrap {
  margin: -4px -4px 4px -4px;
}

.msg-image {
  display: block;
  max-width: 100%;
  border-radius: 6px;
  cursor: pointer;
  transition: opacity 0.2s;
}

.msg-image:hover {
  opacity: 0.9;
}

.msg-video {
  display: block;
  max-width: 100%;
  border-radius: 6px;
  outline: none;
}

.msg-sticker {
  display: block;
  max-width: 160px;
  max-height: 160px;
}

.msg-caption {
  padding-top: 6px;
  font-size: 14px;
  line-height: 1.4;
}

.msg-media-placeholder {
  background: #1a2a32;
  border-radius: 6px;
  padding: 40px 20px;
  text-align: center;
  color: #8696a0;
  font-size: 12px;
  min-width: 200px;
}

.msg-sticker-placeholder {
  padding: 30px;
  min-width: 100px;
}

.msg-media-error {
  color: #ef4444;
}

/* Lightbox */
.lightbox {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.85);
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}

.lightbox.active {
  display: flex;
}

.lightbox img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: 4px;
  object-fit: contain;
}

/* Compose bar */
.compose-bar {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  background: #202c33;
  gap: 10px;
}

.compose-bar input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border: none;
  border-radius: 8px;
  background: #2a3942;
  color: #e9edef;
  font-size: 14px;
  outline: none;
}

.compose-bar button {
  background: #00a884;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.compose-bar button svg {
  fill: #111b21;
  width: 20px;
  height: 20px;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: #374045;
  border-radius: 3px;
}

/* Loading */
.loading {
  text-align: center;
  padding: 40px;
  color: #8696a0;
}

.empty-state {
  text-align: center;
  padding: 40px;
  color: #8696a0;
  font-size: 14px;
}

/* Load-older indicator */
.load-older {
  text-align: center;
  padding: 12px;
  color: #8696a0;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-shrink: 0;
}

.load-older .spinner {
  width: 16px;
  height: 16px;
  border: 2px solid #2a3942;
  border-top-color: #00a884;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

.load-older-done {
  text-align: center;
  padding: 12px;
  color: #374045;
  font-size: 11px;
  flex-shrink: 0;
}

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

/* ─── Responsive ─── */

/* Tablets / medium screens */
@media (max-width: 768px) {
  .chat-list-panel {
    width: 280px;
    min-width: 240px;
  }

  .message-bubble {
    max-width: 80%;
  }

  .topbar {
    padding: 10px 14px;
  }

  .topbar h1 {
    font-size: 15px;
  }

  .instances-list {
    padding: 12px 14px;
  }

  .instance-card {
    padding: 12px;
  }

  .instance-actions {
    flex-wrap: wrap;
  }
}

/* Mobile */
@media (max-width: 600px) {
  /* Chat screen: toggle between panels */
  .chat-screen {
    position: relative;
  }

  .chat-list-panel {
    width: 100%;
    min-width: unset;
    position: absolute;
    inset: 0;
    z-index: 2;
    border-right: none;
  }

  .messages-panel {
    position: absolute;
    inset: 0;
    z-index: 1;
  }

  /* When a chat is selected, hide chat list and show messages */
  .chat-screen.chat-open .chat-list-panel {
    display: none;
  }

  .chat-screen.chat-open .messages-panel {
    z-index: 3;
  }

  /* When no chat is selected, messages panel shows empty state behind */
  .chat-screen:not(.chat-open) .messages-panel {
    display: none;
  }

  /* Show mobile back button */
  .msg-back-btn {
    display: block;
  }

  /* Mark read: icon only on mobile */
  .btn-mark-read span {
    display: none;
  }

  .btn-mark-read {
    padding: 6px;
  }

  /* Chat toggles: switch only on mobile, hide labels */
  .chat-toggle span {
    display: none;
  }

  .message-bubble {
    max-width: 85%;
  }

  /* Instance cards: stack actions below */
  .instance-card {
    flex-direction: column;
    align-items: flex-start;
  }

  .instance-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .instance-actions .btn {
    flex: 1;
    min-width: 0;
    text-align: center;
  }

  /* Topbar */
  .topbar {
    flex-wrap: wrap;
    gap: 8px;
  }

  .topbar-actions {
    width: 100%;
    display: flex;
  }

  .topbar-actions .btn {
    flex: 1;
  }

  /* Login box full width on small screens */
  .login-box {
    padding: 24px;
    border-radius: 0;
    width: 100%;
    max-width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  /* Compose bar */
  .compose-bar {
    padding: 8px 10px;
    gap: 8px;
  }

  .compose-bar input {
    font-size: 16px; /* Prevent iOS zoom on focus */
  }

  /* Chat items — larger touch targets */
  .chat-item {
    padding: 14px 16px;
  }

  .chat-avatar {
    width: 44px;
    height: 44px;
    font-size: 18px;
  }

  /* Modal */
  .modal {
    width: 100%;
    max-width: 100%;
    border-radius: 0;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .modal input {
    font-size: 16px;
  }
}
