/* ─── Variables ─────────────────────────────────────────────────────────── */
:root {
  --bg: #0f0f12;
  --bg-elevated: #18181c;
  --bg-card: #1c1c22;
  --text: #e4e4e7;
  --text-muted: #a1a1aa;
  --border: #27272a;
  --primary: #22c55e;
  --primary-hover: #16a34a;
  --danger: #ef4444;
  --waiting: #eab308;
  --replied: #22c55e;
  --failed: #ef4444;
  --whatsapp: #25d366;
  --instagram: #e4405f;
  --facebook: #1877f2;
  --mail: #6b7280;
  --radius: 12px;
  --sidebar-width: 240px;
  --topbar-height: 56px;
  --font: 'DM Sans', system-ui, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

/* ─── Login ────────────────────────────────────────────────────────────── */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
}

.login-card h1 {
  font-size: 1.5rem;
  margin-bottom: 0.25rem;
}

.login-subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 1.5rem;
}

.login-form label {
  display: block;
  font-size: 0.85rem;
  margin-bottom: 0.35rem;
  color: var(--text-muted);
}

.login-form input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 1rem;
}

.login-form input:focus {
  outline: none;
  border-color: var(--primary);
}

.error-msg {
  color: var(--danger);
  font-size: 0.9rem;
  margin-bottom: 0.75rem;
  min-height: 1.25rem;
}

/* ─── Buttons ───────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem 1.25rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.2s;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  width: 100%;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--border);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger:hover:not(:disabled) {
  background: #b91c1c;
}

.btn-icon {
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-icon:hover {
  background: var(--bg-elevated);
}

.sidebar-close,
.sidebar-toggle {
  display: none;
}

.sidebar-toggle::before { content: '☰'; font-size: 1.25rem; }
.sidebar-close::before { content: '✕'; font-size: 1.25rem; }
.detail-back::before { content: '←'; font-size: 1.25rem; }

/* ─── Panel layout ─────────────────────────────────────────────────────── */
.panel-page .app {
  display: flex;
  min-height: 100vh;
}

/* Channel-specific page accents */
.whatsapp-page {
  background: #e5ddd5;
}

.whatsapp-topbar {
  background: #075e54;
  border-bottom-color: rgba(0, 0, 0, 0.15);
}

.instagram-page {
  background: radial-gradient(circle at 20% 20%, #feda77, #f58529 30%, #dd2a7b 55%, #8134af 75%, #515bd4 100%);
}

.instagram-topbar {
  background: rgba(0, 0, 0, 0.4);
  border-bottom-color: rgba(255, 255, 255, 0.15);
}

.facebook-page {
  background: #f0f2f5;
}

.facebook-topbar {
  background: #1877f2;
  border-bottom-color: rgba(0, 0, 0, 0.25);
}

.mail-page {
  background: #f3f4f6;
}

.mail-topbar {
  background: #020617;
  border-bottom-color: rgba(148, 163, 184, 0.4);
}

.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  padding: 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
  font-size: 1rem;
  font-weight: 600;
}

.channel-filters {
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
}

.channel-btn {
  padding: 0.65rem 1rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 0.95rem;
  text-align: left;
  cursor: pointer;
  transition: background 0.2s;
}

.channel-btn:hover {
  background: var(--bg-card);
}

.channel-btn.active {
  background: var(--primary);
  color: #fff;
}

.sidebar-footer {
  padding: 1rem;
  border-top: 1px solid var(--border);
}

.sidebar-footer .btn {
  width: 100%;
}

.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  height: var(--topbar-height);
  padding: 0 1rem;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 1rem;
  justify-content: space-between;
}

.topbar-stats {
  display: flex;
  gap: 1.25rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.95rem;
  font-weight: 500;
}

.brand-logo {
  width: 24px;
  height: 24px;
  object-fit: contain;
}

/* Brand-specific header contrast tweaks */
.whatsapp-topbar .topbar-brand span,
.whatsapp-topbar .topbar-stats {
  color: #e9f7ec;
}

.instagram-topbar .topbar-brand span,
.instagram-topbar .topbar-stats {
  color: #ffffff;
}

.facebook-topbar .topbar-brand span,
.facebook-topbar .topbar-stats {
  color: #ffffff;
}

.mail-topbar .topbar-brand span,
.mail-topbar .topbar-stats {
  color: #e5e7eb;
}

.topbar-stats strong {
  color: var(--text);
}

.stat-waiting strong {
  color: var(--waiting);
}

.content-wrap {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ─── Message list ──────────────────────────────────────────────────────── */
.message-list-wrap {
  width: 320px;
  min-width: 280px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--bg-elevated);
}

.message-list {
  flex: 1;
  overflow-y: auto;
}

.message-item {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
}

.message-item:hover {
  background: var(--bg-card);
}

.message-item.selected {
  background: var(--bg-card);
  border-left: 3px solid var(--primary);
}

.message-item .channel-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}

.channel-icon.whatsapp { background: rgba(37, 211, 102, 0.2); color: var(--whatsapp); }
.channel-icon.instagram { background: rgba(228, 64, 95, 0.2); color: var(--instagram); }
.channel-icon.facebook { background: rgba(24, 119, 242, 0.2); color: var(--facebook); }
.channel-icon.mail { background: rgba(107, 114, 128, 0.2); color: var(--mail); }

.message-item .item-body {
  flex: 1;
  min-width: 0;
}

.message-item .item-sender {
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 0.2rem;
}

.message-item .item-preview {
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.message-item .item-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.status-badge {
  font-size: 0.7rem;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  font-weight: 500;
}

.status-badge.waiting { background: rgba(234, 179, 8, 0.2); color: var(--waiting); }
.status-badge.replied { background: rgba(34, 197, 94, 0.2); color: var(--replied); }
.status-badge.failed { background: rgba(239, 68, 68, 0.2); color: var(--failed); }

.empty-state,
.loading {
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.loading {
  display: none;
}

.loading.visible {
  display: block;
}

.empty-state.hidden {
  display: none;
}

/* ─── Detail / Chat ─────────────────────────────────────────────────────── */
.detail-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.detail-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 1rem;
}

.detail-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.detail-view.hidden {
  display: none;
}

.detail-placeholder.hidden {
  display: none;
}

.detail-header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--bg-card);
}

.detail-meta {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.detail-sender {
  font-weight: 600;
}

.detail-channel {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 1rem;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding-bottom: 1rem;
}

/* Base bubble */
.bubble {
  max-width: 85%;
  padding: 0.75rem 1rem;
  border-radius: 18px;
  font-size: 0.95rem;
  line-height: 1.45;
  word-break: break-word;
  color: var(--text);
}

.bubble.incoming {
  align-self: flex-start;
}

.bubble.outgoing {
  align-self: flex-end;
}

/* WhatsApp theme */
.chat-container.theme-whatsapp {
  background: #e5ddd5;
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23d4c4b0' fill-opacity='0.4'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.theme-whatsapp .bubble.incoming {
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
  border-top-left-radius: 4px;
  color: #111827;
}

.theme-whatsapp .bubble.outgoing {
  background: #d9fdd3;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
  border-top-right-radius: 4px;
  color: #111827;
}

.theme-whatsapp .bubble-time {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
  text-align: right;
}

/* Instagram theme */
.chat-container.theme-instagram {
  background: var(--bg);
}

.theme-instagram .bubble.incoming {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
  border-bottom-left-radius: 18px;
  color: var(--text);
}

.theme-instagram .bubble.outgoing {
  background: #3797f0;
  color: #fff;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 18px;
}

.theme-instagram .bubble-time {
  font-size: 0.7rem;
  opacity: 0.8;
  margin-top: 0.25rem;
}

/* Mail theme */
.chat-container.theme-mail {
  background: #f8f9fa;
}

.theme-mail .thread-header {
  background: #fff;
  border: 1px solid #dee2e6;
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.theme-mail .thread-header strong {
  color: #495057;
}

.theme-mail .bubble.incoming {
  background: #fff;
  border: 1px solid #dee2e6;
  border-radius: 8px;
  color: #111827;
}

.theme-mail .bubble.outgoing {
  background: #e7f1ff;
  border: 1px solid #c5d9f0;
  border-radius: 8px;
  color: #111827;
}

.theme-mail .bubble-time {
  font-size: 0.75rem;
  color: #6c757d;
  margin-top: 0.25rem;
}

/* Facebook theme */
.chat-container.theme-facebook {
  background: #f0f2f5;
}

.theme-facebook .bubble.incoming {
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
  border-radius: 18px;
  border-top-left-radius: 4px;
  color: #111827;
}

.theme-facebook .bubble.outgoing {
  background: #0084ff;
  color: #fff;
  border-radius: 18px;
  border-top-right-radius: 4px;
}

.theme-facebook .bubble-time {
  font-size: 0.7rem;
  opacity: 0.9;
  margin-top: 0.25rem;
}

.reply-section {
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.reply-section textarea {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  resize: vertical;
  min-height: 80px;
  margin-bottom: 0.5rem;
}

.reply-section textarea:focus {
  outline: none;
  border-color: var(--primary);
}

.reply-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.reply-actions-buttons {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.feedback {
  font-size: 0.85rem;
  min-height: 1.25rem;
}

.feedback.success { color: var(--replied); }
.feedback.error { color: var(--danger); }

/* ─── Responsive ───────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .message-list-wrap {
    width: 280px;
  }
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-close,
  .sidebar-toggle {
    display: flex;
  }

  .content-wrap {
    flex-direction: column;
  }

  .message-list-wrap {
    width: 100%;
    min-width: 0;
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 45vh;
    transition: opacity 0.2s, transform 0.2s;
  }

  .detail-wrap {
    flex: 1;
    min-height: 55vh;
  }

  .detail-view .detail-back {
    display: flex;
  }

  .content-wrap.detail-visible .message-list-wrap {
    display: none;
  }

  .content-wrap.detail-visible .detail-wrap {
    flex: 1;
    min-height: 0;
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 1.5rem;
  }

  .topbar-stats {
    flex-direction: column;
    gap: 0.25rem;
  }

  .reply-actions {
    flex-direction: column;
    align-items: flex-start;
  }

  .reply-actions-buttons {
    width: 100%;
    justify-content: flex-end;
  }

  .reply-actions-buttons .btn {
    width: auto;
  }
}

/* Overlay when sidebar open on mobile */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 99;
}

.sidebar-overlay.visible {
  display: block;
}
