:root {
  --bg-dark: #0f172a;
  --bg-sidebar: #1e293b;
  --bg-card: #1e293b;
  --bg-card-hover: #334155;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --primary: #10b981;
  --primary-hover: #059669;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --danger: #ef4444;
  --warning: #f59e0b;
  --border-color: #334155;
  --chat-bg: #0b1329;
  --chat-bubble-client: #1e293b;
  --chat-bubble-agent: #059669;
  --chat-bubble-bot: #4f46e5;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --font-family: 'Inter', system-ui, -apple-system, sans-serif;
}

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

body {
  font-family: var(--font-family);
  background-color: var(--bg-dark);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
}

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

/* Sidebar */
.sidebar {
  width: 260px;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  padding: 20px 15px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 25px;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 20px;
}

.brand-icon {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: bold;
}

.brand-name {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.nav-menu {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 15px;
  color: var(--text-muted);
  text-decoration: none;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  font-weight: 500;
  transition: all 0.2s ease;
}

.nav-link:hover, .nav-link.active {
  background-color: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
}

.nav-link.active {
  background-color: var(--accent);
  color: white;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-top: 15px;
  border-top: 1px solid var(--border-color);
}

.user-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #475569;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

.user-info {
  flex: 1;
  overflow: hidden;
}

.user-name {
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.logout-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 5px;
  transition: color 0.2s;
}

.logout-btn:hover {
  color: var(--danger);
}

/* Main Content */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  background-color: var(--bg-dark);
}

.header {
  padding: 20px 30px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: var(--bg-sidebar);
}

.header-title h1 {
  font-size: 1.5rem;
  font-weight: 700;
}

.header-title p {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.content-body {
  padding: 30px;
  flex: 1;
}

/* Cards & Stats */
.grid-4 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  background-color: var(--bg-card);
  padding: 20px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 15px;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
}

.stat-icon.green { background: rgba(16, 185, 129, 0.15); color: var(--primary); }
.stat-icon.blue { background: rgba(99, 102, 241, 0.15); color: var(--accent); }
.stat-icon.amber { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
.stat-icon.purple { background: rgba(168, 85, 247, 0.15); color: #a855f7; }

.stat-val {
  font-size: 1.8rem;
  font-weight: 700;
}

.stat-label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Buttons */
.btn {
  padding: 10px 18px;
  border-radius: var(--radius-md);
  border: none;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: background-color 0.2s ease, transform 0.1s ease;
}

.btn-primary {
  background-color: var(--primary);
  color: white;
}
.btn-primary:hover { background-color: var(--primary-hover); }

.btn-accent {
  background-color: var(--accent);
  color: white;
}
.btn-accent:hover { background-color: var(--accent-hover); }

.btn-secondary {
  background-color: #334155;
  color: var(--text-main);
}
.btn-secondary:hover { background-color: #475569; }

.btn-danger {
  background-color: var(--danger);
  color: white;
}

/* Tables */
.table-container {
  background-color: var(--bg-card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  overflow: hidden;
}

table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

th, td {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border-color);
}

th {
  background-color: rgba(255, 255, 255, 0.03);
  font-size: 0.8rem;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 600;
}

td {
  font-size: 0.9rem;
}

tr:last-child td { border-bottom: none; }

/* Forms */
.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-main);
}

.form-control {
  width: 100%;
  padding: 12px 15px;
  border-radius: var(--radius-md);
  background-color: #0f172a;
  border: 1px solid var(--border-color);
  color: var(--text-main);
  font-size: 0.95rem;
  font-family: inherit;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary);
}

.channel-toggle-card {
  background: #0f172a;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 12px;
}

.channel-toggle-card .form-label {
  margin-bottom: 6px;
}

.channel-toggle-card .form-control {
  padding: 8px 10px;
  font-size: 0.85rem;
}

/* Inbox / Live Chat Specifics */
.inbox-container {
  display: flex;
  height: calc(100vh - 81px);
  overflow: hidden;
}

.conversations-sidebar {
  width: 340px;
  border-right: 1px solid var(--border-color);
  background-color: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
}

.conversations-header {
  padding: 15px;
  border-bottom: 1px solid var(--border-color);
}

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

.conv-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 15px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  cursor: pointer;
  transition: background-color 0.2s;
}

.conv-item:hover, .conv-item.active {
  background-color: rgba(255, 255, 255, 0.06);
}

.conv-item.active {
  border-left: 4px solid var(--primary);
}

.badge {
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
}

.badge-whatsapp_cloud { background: #075e54; color: #25d366; }
.badge-whatsapp_evolution { background: #128c7e; color: white; }
.badge-webchat { background: #4f46e5; color: white; }
.badge-unread { background: var(--danger); color: white; }

.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: var(--chat-bg);
}

.chat-header {
  padding: 15px 25px;
  background-color: var(--bg-sidebar);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chat-messages {
  flex: 1;
  padding: 25px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg-bubble {
  max-width: 65%;
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  font-size: 0.95rem;
  line-height: 1.4;
  position: relative;
  word-break: break-word;
}

.msg-customer {
  align-self: flex-start;
  background-color: var(--chat-bubble-client);
  border-bottom-left-radius: 2px;
}

.msg-agent {
  align-self: flex-end;
  background-color: var(--chat-bubble-agent);
  color: white;
  border-bottom-right-radius: 2px;
}

.msg-bot {
  align-self: flex-end;
  background-color: var(--chat-bubble-bot);
  color: white;
  border-bottom-right-radius: 2px;
}

.msg-bug {
  align-self: flex-start;
  background-color: #7f1d1d;
  color: #fca5a5;
  border: 1px solid #ef4444;
  border-bottom-left-radius: 2px;
  max-width: 85%;
}

.msg-bug pre,
.msg-bug div {
  color: #fecaca;
}

.msg-image {
  max-width: 240px;
  max-height: 240px;
  border-radius: 8px;
  display: block;
  margin: 6px 0;
  cursor: pointer;
}

.msg-doc {
  display: inline-block;
  margin: 6px 0;
  color: inherit;
  text-decoration: underline;
}

.msg-agent-avatar {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  object-fit: cover;
  vertical-align: middle;
  margin-right: 5px;
}

.msg-time {
  font-size: 0.65rem;
  opacity: 0.7;
  display: block;
  margin-top: 4px;
  text-align: right;
}

.chat-input-area {
  padding: 15px 25px;
  background-color: var(--bg-sidebar);
  border-top: 1px solid var(--border-color);
  display: flex;
  gap: 12px;
}

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

.modal-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 25px;
  width: 90%;
  max-width: 480px;
}

/* ============================================
   Mobile responsive (phones & small tablets)
   ============================================ */

/* Botón hamburguesa — oculto en desktop */
.mobile-menu-btn {
  display: none;
  position: fixed;
  top: 12px;
  right: 16px;
  z-index: 1100;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 1.3rem;
  cursor: pointer;
}

/* Overlay para cuando el sidebar está abierto en móvil */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1050;
}

@media (max-width: 768px) {
  body {
    overflow-x: hidden;
  }

  .app-container {
    flex-direction: column;
  }

  .mobile-menu-btn {
    display: block;
    position: fixed;
    top: 12px;
    right: 16px;
    z-index: 1100;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: -280px;
    width: 270px;
    height: 100vh;
    height: 100dvh;
    z-index: 1080;
    transition: left 0.3s ease;
    overflow-y: auto;
    padding-top: max(20px, env(safe-area-inset-top));
  }

  .sidebar.open {
    left: 0;
  }

  .sidebar-overlay.open {
    display: block;
  }

  .main-content {
    width: 100% !important;
    margin-left: 0 !important;
    padding-top: 50px;
  }

  .header {
    flex-wrap: wrap;
    padding: 12px 16px;
    gap: 10px;
  }

  .header h1 {
    font-size: 1.15rem;
  }

  .header p {
    font-size: 0.8rem;
  }

  /* Inbox: sidebar full-width, chat full-screen on mobile */
  .inbox-container {
    flex-direction: column;
    height: calc(100vh - 50px) !important;
    position: relative;
  }

  .conversations-sidebar {
    width: 100% !important;
    height: 100%;
    border-right: none !important;
  }

  .chat-area {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    background: var(--bg-dark);
    display: flex;
    flex-direction: column;
  }

  .mobile-back-btn {
    display: flex !important;
    width: 36px;
    height: 36px;
    align-items: center;
    justify-content: center;
    padding: 0 !important;
    font-size: 1.2rem;
    border-radius: 50%;
  }

  #btn-back-to-list {
    display: none;
  }

  .chat-header {
    flex-wrap: nowrap !important;
    padding: 12px 14px !important;
    gap: 8px;
  }

  .chat-header h3 {
    font-size: 0.95rem !important;
  }

  .chat-messages {
    flex: 1;
    padding: 12px;
  }

  .chat-input-area {
    padding: 10px 8px !important;
    padding-bottom: max(10px, env(safe-area-inset-bottom)) !important;
    gap: 6px !important;
  }

  .chat-input-area input {
    padding: 14px 12px !important;
    font-size: 1rem !important;
    border-radius: 24px !important;
  }

  .chat-input-area .btn {
    padding: 10px 12px !important;
    font-size: 1.1rem !important;
    min-width: 42px !important;
    height: 42px !important;
    border-radius: 50% !important;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .chat-input-area .btn-accent {
    padding: 10px 14px !important;
    border-radius: 24px !important;
    min-width: auto !important;
  }

  .msg-bubble {
    max-width: 85% !important;
    font-size: 0.95rem !important;
    padding: 12px 16px !important;
  }

  .conv-item {
    padding: 14px 16px !important;
  }

  /* Tablas y cards más compactas */
  .stat-card {
    padding: 16px;
  }

  .stat-card h2 {
    font-size: 1rem;
  }

  .table-container {
    overflow-x: auto;
  }

  table {
    font-size: 0.8rem;
  }

  table th, table td {
    padding: 8px 10px;
  }

  /* Tabs */
  #tab-btn-general, #tab-btn-wa, #tab-btn-ai, #tab-btn-widget, #tab-btn-apikeys,
  [id^="tab-btn-"] {
    font-size: 0.75rem;
    padding: 6px 10px;
  }

  /* Knowledge base */
  .content-body {
    padding: 12px;
  }

  .form-group {
    margin-bottom: 12px;
  }

  .form-control {
    padding: 10px 12px;
    font-size: 0.9rem;
  }

  textarea.form-control {
    font-size: 0.9rem;
  }

  /* Monitoreo */
  .monitor-stats {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px;
  }

  .monitor-stat {
    padding: 12px;
  }

  .monitor-stat .stat-val {
    font-size: 1.5rem;
  }

  .activity-feed {
    max-height: 300px !important;
  }

  /* Sidebar interno: ajustar logo */
  .brand {
    padding: 0 10px;
  }

  .brand-icon {
    font-size: 1.3rem;
  }

  .brand-name {
    font-size: 1rem;
  }

  .nav-link {
    padding: 10px 12px;
    font-size: 0.9rem;
  }

  .user-profile {
    padding: 10px;
  }
}

@media (max-width: 480px) {
  .header {
    padding: 8px 12px;
  }

  .header h1 {
    font-size: 1rem;
  }

  .msg-bubble {
    max-width: 90% !important;
    font-size: 0.85rem !important;
    padding: 10px 14px !important;
  }

  .msg-image {
    max-width: 180px !important;
    max-height: 180px !important;
  }

  .monitor-stats {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  .stat-card {
    padding: 12px;
  }

  [style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }
}
