:root {
  --bg-primary: #070705;
  --bg-secondary: #0e0e0b;
  --bg-card: #141410;
  --bg-card-hover: #1c1c17;
  --bg-hover: #1f1f1a;
  --bg-surface: #1a1a15;
  --text-primary: #f0f4f8;
  --text-secondary: #8896a4;
  --text-muted: #5e6b78;
  --accent: #6ec627;
  --accent-dark: #5aab1a;
  --accent-orange: #f87f06;
  --blue: #3b82f6;
  --blue-soft: #93c5fd;
  --orange-soft: #fdba74;
  --green-soft: #bbf7d0;
  --red-soft: #fca5a5;
  --border: #222218;
  --border-light: #2e2e26;
  --danger: #ef4444;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.5);
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

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

body {
  font-family: 'Ubuntu', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ===================== Header ===================== */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 28px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(12px);
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.header-logo {
  height: 34px;
  width: auto;
  object-fit: contain;
}

header h1 {
  font-family: 'Ubuntu', sans-serif;
  font-size: 1.25rem;
  font-weight: 700;
  background: linear-gradient(135deg, #6ec627, #f87f06);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  white-space: nowrap;
}

.badge-icon {
  font-size: 13px;
  vertical-align: middle;
  margin-right: 2px;
}

.badge {
  font-size: 0.65rem;
  padding: 3px 10px;
  border-radius: 20px;
  background: rgba(239,68,68,0.2);
  color: var(--red-soft);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border: 1px solid rgba(239,68,68,0.3);
}

.badge.connected {
  background: rgba(110,198,39,0.15);
  color: var(--accent);
  border-color: rgba(110,198,39,0.3);
}

/* Search */
.search-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 10px;
  font-size: 16px;
  color: var(--text-muted);
  pointer-events: none;
}

#search-input {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 6px 12px 6px 32px;
  border-radius: var(--radius-sm);
  width: 180px;
  font-size: 0.8rem;
  font-family: inherit;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

#search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(110,198,39,0.1);
  width: 220px;
}

.message-filters #btn-refresh {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.message-filters #btn-refresh .material-icons-outlined {
  font-size: 18px;
}

.message-filters #btn-refresh:hover {
  background: var(--bg-hover);
  color: var(--accent);
  border-color: var(--accent);
}

#search-input::placeholder {
  color: var(--text-muted);
}

/* ===================== Main ===================== */
main {
  padding: 24px 28px;
  max-width: 1440px;
  margin: 0 auto;
}

/* ===================== Stats Grid ===================== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
  position: relative;
  overflow: hidden;
}

.stat-card::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--border-light);
}

.stat-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: rgba(110,198,39,0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.stat-icon .material-icons-outlined {
  font-size: 20px;
  color: var(--accent);
}

.stat-info {
  min-width: 0;
}

.stat-number {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--text-primary);
  line-height: 1;
  margin-bottom: 2px;
}

.stat-label {
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Stat card color variants */
.stat-card.stat-sent::before { background: var(--blue); }
.stat-card.stat-sent .stat-icon { background: rgba(59,130,246,0.1); }
.stat-card.stat-sent .stat-icon .material-icons-outlined { color: var(--blue); }
.stat-card.stat-sent .stat-number { color: var(--blue-soft); }

.stat-card.stat-delivered::before { background: var(--accent-orange); }
.stat-card.stat-delivered .stat-icon { background: rgba(248,127,6,0.1); }
.stat-card.stat-delivered .stat-icon .material-icons-outlined { color: var(--accent-orange); }
.stat-card.stat-delivered .stat-number { color: var(--orange-soft); }

.stat-card.stat-read::before { background: var(--accent); }
.stat-card.stat-read .stat-icon { background: rgba(110,198,39,0.1); }
.stat-card.stat-read .stat-icon .material-icons-outlined { color: var(--accent); }
.stat-card.stat-read .stat-number { color: var(--green-soft); }

.stat-card.stat-failed::before { background: var(--danger); }
.stat-card.stat-failed .stat-icon { background: rgba(239,68,68,0.1); }
.stat-card.stat-failed .stat-icon .material-icons-outlined { color: var(--danger); }
.stat-card.stat-failed .stat-number { color: var(--red-soft); }

/* ===================== Delivery Funnel ===================== */
.funnel-section {
  margin-bottom: 20px;
}

.funnel-bar {
  display: flex;
  height: 28px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--bg-card);
  border: 1px solid var(--border);
}

.funnel-segment {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  transition: width 0.8s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.funnel-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  white-space: nowrap;
  opacity: 0.9;
}

.funnel-sent { background: rgba(59,130,246,0.3); color: var(--blue-soft); }
.funnel-delivered { background: rgba(248,127,6,0.3); color: var(--orange-soft); }
.funnel-read { background: rgba(110,198,39,0.3); color: var(--green-soft); }
.funnel-failed { background: rgba(239,68,68,0.3); color: var(--red-soft); }

/* ===================== Content Grid ===================== */
.content-grid {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 16px;
  margin-bottom: 20px;
}

/* ===================== Recipients Panel ===================== */
.conversations-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  max-height: 640px;
  display: flex;
  flex-direction: column;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.panel-header h2 {
  font-size: 0.9rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.panel-header h2 .material-icons-outlined {
  font-size: 18px;
  color: var(--text-muted);
}

.panel-count {
  font-size: 0.7rem;
  background: var(--bg-surface);
  color: var(--text-muted);
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
}

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

.conversation-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background var(--transition);
}

.conversation-item:hover {
  background: var(--bg-card-hover);
}

.conversation-item.active {
  background: var(--bg-hover);
  border-left: 3px solid var(--accent);
}

.conversation-item .conv-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.conversation-item .conv-name {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 4px;
}

.conversation-item .conv-count {
  font-size: 0.65rem;
  background: rgba(110,198,39,0.15);
  color: var(--accent);
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 700;
}

/* Recipient stats mini-bars */
.conv-stats {
  display: flex;
  gap: 4px;
  margin-top: 4px;
}

.conv-stats .cs {
  font-size: 0.62rem;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
  letter-spacing: 0.2px;
}

.cs.sent { background: rgba(59,130,246,0.12); color: var(--blue-soft); }
.cs.delivered { background: rgba(248,127,6,0.12); color: var(--orange-soft); }
.cs.read { background: rgba(110,198,39,0.12); color: var(--green-soft); }
.cs.failed { background: rgba(239,68,68,0.12); color: var(--red-soft); }

/* ===================== Messages Panel ===================== */
.messages-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: 640px;
}

.messages-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 8px 12px;
}

.messages-header h2 {
  font-size: 0.95rem;
  font-weight: 600;
  white-space: nowrap;
}

.message-filters {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  flex: 1 1 280px;
  min-width: 0;
}

.message-filters select {
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-family: inherit;
  outline: none;
  cursor: pointer;
  transition: border-color var(--transition);
}

.message-filters select:focus {
  border-color: var(--accent);
}

.messages-list {
  overflow-y: auto;
  flex: 1;
  padding: 6px;
}

/* ===================== Message Item ===================== */
.message-item {
  display: flex;
  gap: 12px;
  padding: 14px 12px;
  border-radius: var(--radius-sm);
  margin-bottom: 2px;
  transition: background var(--transition);
  border: 1px solid transparent;
}

.message-item:hover {
  background: var(--bg-card-hover);
  border-color: var(--border);
}

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

.msg-status-icon {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1.1rem;
}

.msg-status-icon.sent { background: rgba(59,130,246,0.12); color: var(--blue); }
.msg-status-icon.delivered { background: rgba(248,127,6,0.12); color: var(--accent-orange); }
.msg-status-icon.read { background: rgba(110,198,39,0.12); color: var(--accent); }
.msg-status-icon.failed { background: rgba(239,68,68,0.12); color: var(--danger); }

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

.msg-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2px;
}

.msg-sender {
  font-weight: 600;
  font-size: 0.88rem;
}

.msg-time {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.msg-id {
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 8px;
  font-family: 'Ubuntu Mono', 'Consolas', monospace;
  opacity: 0.7;
}

/* Status Timeline / Progress */
.msg-timeline {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px 0;
  margin-bottom: 8px;
  background: var(--bg-surface);
  border-radius: 6px;
  padding: 6px 10px;
}

.tl-step {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 0.7rem;
  color: var(--text-muted);
  opacity: 0.35;
  white-space: nowrap;
}

.tl-step .material-icons-outlined {
  font-size: 14px;
}

.tl-step.active {
  opacity: 1;
  color: var(--accent);
}

.tl-step.active.read {
  color: var(--blue);
}

.tl-step.active.failed {
  color: var(--danger);
}

.tl-arrow {
  color: var(--text-muted);
  opacity: 0.2;
  font-size: 0.65rem;
  margin: 0 4px;
}

.msg-meta {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}

.msg-status {
  font-size: 0.65rem;
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.msg-status.sent { background: rgba(59,130,246,0.12); color: var(--blue-soft); }
.msg-status.delivered { background: rgba(248,127,6,0.12); color: var(--orange-soft); }
.msg-status.read { background: rgba(110,198,39,0.12); color: var(--green-soft); }
.msg-status.failed { background: rgba(239,68,68,0.12); color: var(--red-soft); }

.msg-status .si {
  font-size: 12px;
  vertical-align: middle;
  margin-right: 2px;
}

/* ===================== Pagination ===================== */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.pagination button {
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  padding: 6px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.82rem;
  font-family: inherit;
  transition: all var(--transition);
}

.pagination button:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--accent);
  color: var(--accent);
}

.pagination button:disabled {
  opacity: 0.3;
  cursor: default;
}

.pagination span {
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* ===================== Chart ===================== */
.chart-section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.chart-section h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
}

.chart-container {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 160px;
  padding: 0 4px;
}

.chart-bar-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  justify-content: flex-end;
}

.chart-bar {
  width: 100%;
  max-width: 36px;
  background: linear-gradient(to top, var(--accent-dark), var(--accent));
  border-radius: 4px 4px 0 0;
  min-height: 2px;
  transition: height 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  cursor: pointer;
}

.chart-bar:hover {
  filter: brightness(1.2);
}

.chart-bar .bar-tooltip {
  display: none;
  position: absolute;
  top: -30px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-secondary);
  color: var(--text-primary);
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 0.72rem;
  white-space: nowrap;
  border: 1px solid var(--border-light);
  box-shadow: var(--shadow-sm);
  font-weight: 600;
}

.chart-bar:hover .bar-tooltip {
  display: block;
}

.chart-label {
  font-size: 0.6rem;
  color: var(--text-muted);
  margin-top: 6px;
}

/* ===================== Loading & Empty ===================== */
.loading, .empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 48px 20px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

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

.spin {
  animation: spin 1.5s linear infinite;
}

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

.toast {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 12px 18px;
  color: var(--text-primary);
  font-size: 0.82rem;
  animation: toastIn 0.3s ease;
  box-shadow: var(--shadow-lg);
  max-width: 350px;
  backdrop-filter: blur(8px);
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(40px); }
  to { opacity: 1; transform: translateX(0); }
}

.toast .toast-title {
  font-weight: 700;
  margin-bottom: 2px;
  font-size: 0.82rem;
}

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

/* ===================== Responsive ===================== */
@media (max-width: 1200px) {
  .stats-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 900px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .content-grid {
    grid-template-columns: 1fr;
  }

  .conversations-panel {
    max-height: 280px;
  }

  header {
    padding: 10px 16px;
    gap: 6px;
  }

  header h1 {
    font-size: 1.05rem;
  }

  main {
    padding: 16px 12px;
  }

  .chart-container {
    height: 160px;
    overflow-x: auto;
    min-width: 0;
    padding-bottom: 20px;
  }

  .chart-bar-wrapper {
    min-width: 24px;
    flex-shrink: 0;
  }

  .chart-label {
    position: sticky;
    bottom: 0;
  }

  .funnel-section {
    display: none;
  }

  .message-filters {
    width: 100%;
    justify-content: stretch;
  }

  .search-wrapper {
    flex: 1;
    min-width: 0;
  }

  #search-input {
    width: 100%;
  }

  #search-input:focus {
    width: 100%;
  }

  .message-filters select {
    flex: 1;
    min-width: 0;
  }
}

@media (max-width: 550px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .chart-label:nth-child(6n) {
    display: block;
  }
}

/* ===================== Scrollbar ===================== */
::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}

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

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}
