:root {
  --b24-blue: #2fc6f6;
  --b24-blue-dark: #0fa8d8;
  --b24-bg: #f5f7fa;
  --b24-card: #ffffff;
  --b24-border: #e5e8ec;
  --b24-text: #333;
  --b24-text-muted: #8a9bae;
  --b24-success: #59c74a;
  --b24-danger: #ff5752;
  --b24-warning: #ffa900;
  --tg-blue: #2aabee;
  --tg-dark: #1e88c7;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --radius: 8px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color: var(--b24-text);
  background: var(--b24-bg);
  line-height: 1.5;
}

/* ── Layout ── */
.app-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: 320px;
  min-width: 320px;
  background: var(--b24-card);
  border-right: 1px solid var(--b24-border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.main-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--b24-bg);
  min-height: 0;
  overflow: hidden;
}

#chatView {
  flex: 1;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  display: none;
}
#chatView.visible {
  display: flex;
}

/* ── Sidebar Header ── */
.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--b24-border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-header h2 {
  font-size: 16px;
  font-weight: 600;
  flex: 1;
}

.account-selector {
  padding: 6px 10px;
  border: 1px solid var(--b24-border);
  border-radius: var(--radius);
  font-size: 13px;
  background: var(--b24-bg);
  cursor: pointer;
  max-width: 160px;
}

.btn-icon {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--b24-border);
  border-radius: var(--radius);
  background: var(--b24-card);
  cursor: pointer;
  font-size: 16px;
  transition: background 0.15s;
}
.btn-icon:hover { background: var(--b24-bg); }

/* ── Chat List ── */
.chat-list {
  flex: 1;
  overflow-y: auto;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--b24-border);
  transition: background 0.15s;
}
.chat-item:hover { background: #f0f4f8; }
.chat-item.active { background: #e8f4fd; }

.chat-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--tg-blue);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  flex-shrink: 0;
}

/* ── Avatars (generic) ── */
.avatar {
  position: relative;
  overflow: hidden;
}
.avatar .avatar-fallback {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.avatar .avatar-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

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

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

.chat-last-msg {
  font-size: 13px;
  color: var(--b24-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-meta {
  text-align: right;
  flex-shrink: 0;
}

.chat-time {
  font-size: 11px;
  color: var(--b24-text-muted);
}

.chat-unread {
  display: inline-block;
  min-width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  background: var(--tg-blue);
  color: #fff;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  margin-top: 4px;
}

/* ── Messages Area ── */
.chat-header {
  padding: 14px 20px;
  background: var(--b24-card);
  border-bottom: 1px solid var(--b24-border);
  display: flex;
  align-items: center;
  gap: 12px;
}

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

.chat-header-status {
  font-size: 12px;
  color: var(--b24-text-muted);
}

.messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
}

.message-row {
  display: flex;
  max-width: 70%;
}
.message-row.outgoing {
  align-self: flex-end;
}
.message-row.incoming {
  align-self: flex-start;
}

.message-bubble {
  padding: 8px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
  position: relative;
}

.message-row.outgoing .message-bubble {
  background: var(--tg-blue);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message-row.incoming .message-bubble {
  background: var(--b24-card);
  border: 1px solid var(--b24-border);
  border-bottom-left-radius: 4px;
}

.message-sender {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 2px;
  line-height: 1.2;
}

.message-time {
  font-size: 11px;
  margin-top: 4px;
  opacity: 0.7;
  text-align: right;
}

.message-row.incoming .message-time {
  color: var(--b24-text-muted);
}

/* ── Message Input ── */
.message-input-area {
  padding: 12px 20px;
  background: var(--b24-card);
  border-top: 1px solid var(--b24-border);
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.message-input-area textarea {
  flex: 1;
  border: 1px solid var(--b24-border);
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  max-height: 120px;
  outline: none;
  transition: border-color 0.15s;
}
.message-input-area textarea:focus {
  border-color: var(--tg-blue);
}

.btn-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--tg-blue);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: background 0.15s;
  flex-shrink: 0;
}
.btn-send:hover { background: var(--tg-dark); }
.btn-send:disabled { background: #ccc; cursor: default; }

/* ── Empty State ── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--b24-text-muted);
  gap: 12px;
}

.empty-state-icon {
  font-size: 48px;
  opacity: 0.3;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 18px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-primary {
  background: var(--tg-blue);
  color: #fff;
}
.btn-primary:hover { background: var(--tg-dark); }

.btn-danger {
  background: var(--b24-danger);
  color: #fff;
}
.btn-danger:hover { background: #e0443f; }

.btn-outline {
  background: transparent;
  border: 1px solid var(--b24-border);
  color: var(--b24-text);
}
.btn-outline:hover { background: var(--b24-bg); }

.btn-sm {
  padding: 5px 12px;
  font-size: 13px;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.modal-overlay.active {
  opacity: 1;
  pointer-events: all;
}

.modal {
  background: var(--b24-card);
  border-radius: 12px;
  box-shadow: var(--shadow-md);
  padding: 24px;
  min-width: 360px;
  max-width: 90vw;
}

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

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

/* ── QR Code ── */
.qr-container {
  text-align: center;
  padding: 20px;
}

.qr-container img {
  width: 220px;
  height: 220px;
  border-radius: 8px;
}

.qr-hint {
  margin-top: 12px;
  font-size: 13px;
  color: var(--b24-text-muted);
}

/* ── Settings ── */
.settings-page {
  max-width: 640px;
  margin: 0 auto;
  padding: 24px;
}

.account-card {
  background: var(--b24-card);
  border-radius: var(--radius);
  border: 1px solid var(--b24-border);
  padding: 16px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 14px;
}

.account-card .account-info { flex: 1; }
.account-card .account-name { font-weight: 600; }
.account-card .account-phone { font-size: 13px; color: var(--b24-text-muted); }

.status-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}
.status-badge.active { background: #e6f9e6; color: #2d8a2d; }
.status-badge.disconnected { background: #fff3e0; color: #d17a00; }
.status-badge.auth_required { background: #ffebee; color: #c62828; }

/* ── CRM Tab ── */
.crm-tab {
  padding: 16px;
  max-width: 480px;
}

.crm-field-status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-size: 13px;
}
.crm-field-status.ok { background: #e6f9e6; color: #2d8a2d; }
.crm-field-status.empty { background: #fff3e0; color: #d17a00; }
.crm-field-status.error { background: #ffebee; color: #c62828; }

.crm-compose {
  margin-top: 16px;
}

.crm-compose textarea {
  width: 100%;
  border: 1px solid var(--b24-border);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  min-height: 80px;
  outline: none;
}
.crm-compose textarea:focus { border-color: var(--tg-blue); }

.crm-chat-preview {
  background: var(--b24-card);
  border: 1px solid var(--b24-border);
  border-radius: var(--radius);
  padding: 14px;
  margin-top: 12px;
}

.crm-chat-preview .chat-link {
  color: var(--tg-blue);
  text-decoration: none;
  font-weight: 500;
}
.crm-chat-preview .chat-link:hover { text-decoration: underline; }

/* ── Navigation ── */
.nav-tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--b24-border);
  padding: 0 16px;
  background: var(--b24-card);
}

.nav-tab {
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 500;
  color: var(--b24-text-muted);
  cursor: pointer;
  border: none;
  background: none;
  position: relative;
}
.nav-tab:hover { color: var(--b24-text); }
.nav-tab.active {
  color: var(--tg-blue);
}
.nav-tab.active::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--tg-blue);
}

/* ── Media in messages ── */
.message-media { margin-bottom: 4px; }
.message-media img {
  max-width: 280px;
  max-height: 280px;
  border-radius: 8px;
  cursor: pointer;
  display: block;
}
/* ── Voice player ── */
.voice-player {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 200px;
  max-width: 260px;
  padding: 4px 0;
}
.vp-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.08);
  color: var(--b24-text);
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}
.message-row.outgoing .vp-btn { background: rgba(255,255,255,0.2); color: #fff; }
.vp-btn:hover { background: rgba(0,0,0,0.15); }
.message-row.outgoing .vp-btn:hover { background: rgba(255,255,255,0.35); }
.vp-bar {
  flex: 1;
  height: 4px;
  background: rgba(0,0,0,0.1);
  border-radius: 2px;
  cursor: pointer;
  position: relative;
}
.message-row.outgoing .vp-bar { background: rgba(255,255,255,0.25); }
.vp-progress {
  height: 100%;
  background: var(--tg-blue);
  border-radius: 2px;
  width: 0%;
  transition: width 0.1s linear;
}
.message-row.outgoing .vp-progress { background: #fff; }
.vp-time {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  min-width: 32px;
  text-align: right;
  opacity: 0.7;
}
.message-media video {
  max-width: 280px;
  max-height: 280px;
  border-radius: 8px;
  display: block;
}
.message-media .file-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: rgba(0,0,0,0.05);
  border-radius: 6px;
  font-size: 13px;
  text-decoration: none;
  color: inherit;
  word-break: break-all;
}
.message-row.outgoing .file-link { background: rgba(255,255,255,0.15); color: #fff; }
.file-link:hover { text-decoration: underline; }
.file-icon { font-size: 18px; flex-shrink: 0; }

/* ── Media loading placeholder ── */
.media-loading {
  padding: 12px 16px;
  background: rgba(0,0,0,0.04);
  border-radius: 8px;
  font-size: 13px;
  color: var(--b24-text-muted);
  text-align: center;
  min-width: 120px;
}
.message-row.outgoing .media-loading { background: rgba(255,255,255,0.15); color: rgba(255,255,255,0.7); }

/* ── Attach & Voice buttons ── */
.btn-attach {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--b24-text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.15s;
}
.btn-attach:hover { color: var(--tg-blue); background: var(--b24-bg); }
.btn-attach.recording { color: var(--b24-danger); background: #ffebee; animation: pulse 1s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }

/* ── Attachment preview ── */
.attachment-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--b24-bg);
  border-top: 1px solid var(--b24-border);
  font-size: 13px;
}
.attachment-preview .file-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.attachment-preview .btn-remove {
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  color: var(--b24-danger);
  cursor: pointer;
  font-size: 16px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.attachment-preview .btn-remove:hover { background: #ffebee; }

/* ── Image modal ── */
.image-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  cursor: pointer;
}
.image-modal img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: 4px;
}

/* ── Loading ── */
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--b24-border);
  border-top-color: var(--tg-blue);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-center {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
