:root {
  --primary: #0088cc;
  --primary-dark: #0077b3;
  --danger: #dc3545;
  --success: #28a745;
  --warning: #ffc107;
  --bg: #f5f7fa;
  --card: #ffffff;
  --text: #333333;
  --text-muted: #666666;
  --border: #e0e0e0;
  --sidebar: #1a1d29;
  --sidebar-text: #a0a8c0;
  --msg-in-bg: #f6f9fc;
  --msg-out-bg: #eaf4fd;
  --chip-bg: #ffffff;
  --chart-grid: #e8ecf1;
}

[data-theme="dark"] {
  --bg: #11141c;
  --card: #1c2130;
  --text: #e6e9f0;
  --text-muted: #9aa3b8;
  --border: #313a4f;
  --sidebar: #14171f;
  --msg-in-bg: #1a2233;
  --msg-out-bg: #16283a;
  --chip-bg: #252c3f;
  --chart-grid: #2a3347;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

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

.sidebar {
  width: 240px;
  background: var(--sidebar);
  color: var(--sidebar-text);
  padding: 20px 0;
  position: fixed;
  height: 100vh;
  overflow-y: auto;
}

.logo {
  padding: 0 20px 20px;
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  margin-bottom: 10px;
}

nav {
  display: flex;
  flex-direction: column;
}

.nav-link {
  padding: 14px 20px;
  color: var(--sidebar-text);
  text-decoration: none;
  transition: all 0.2s;
  border-left: 3px solid transparent;
}

.nav-link:hover,
.nav-link.active {
  background: rgba(255,255,255,0.05);
  color: #fff;
  border-left-color: var(--primary);
}

.main {
  flex: 1;
  margin-left: 240px;
  display: flex;
  flex-direction: column;
}

.topbar {
  background: var(--card);
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar h1 {
  margin: 0;
  font-size: 22px;
}

.status {
  font-size: 14px;
  color: var(--success);
}

.content {
  padding: 24px;
  flex: 1;
}

.card {
  background: var(--card);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.card h2 {
  margin: 0;
  font-size: 16px;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

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

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

.btn-success {
  background: var(--success);
  color: #fff;
}

.btn-small {
  padding: 4px 10px;
  font-size: 12px;
}

input, select, textarea {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}

textarea {
  min-height: 80px;
  resize: vertical;
}

.form-row {
  display: flex;
  gap: 12px;
  margin-bottom: 12px;
  align-items: flex-end;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.form-group label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

th, td {
  text-align: left;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
}

.badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
}

.badge-active { background: #d4edda; color: #155724; }
.badge-pending { background: #fff3cd; color: #856404; }
.badge-error { background: #f8d7da; color: #721c24; }
.badge-paused { background: #e2e3e5; color: #383d41; }
.badge-running { background: #cce5ff; color: #004085; }
.badge-banned { background: #f8d7da; color: #721c24; }
.badge-completed { background: #d4edda; color: #155724; }

.empty {
  color: var(--text-muted);
  font-style: italic;
  text-align: center;
  padding: 40px;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background: var(--card);
  border-radius: 12px;
  padding: 24px;
  width: 500px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
}

.modal h2 {
  margin-top: 0;
}

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

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--card);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.stat-value {
  font-size: 32px;
  font-weight: 700;
  color: var(--primary);
}

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

#toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 16px;
  background: #333;
  color: #fff;
  border-radius: 8px;
  min-width: 250px;
}

.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

.hidden { display: none !important; }

.message-item {
  display: flex;
  gap: 12px;
  padding: 14px;
  border-radius: 10px;
  margin-bottom: 8px;
  border-left: 3px solid transparent;
}

.message-in { background: var(--msg-in-bg); border-left-color: var(--success); }
.message-out { background: var(--msg-out-bg); border-left-color: var(--primary); }

.msg-avatar {
  flex: 0 0 38px;
  height: 38px;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
}

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

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

.msg-direction {
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.3px;
  text-transform: uppercase;
}

.msg-dir-in { background: #d4edda; color: #155724; }
.msg-dir-out { background: #cce5ff; color: #004085; }

.msg-sender { font-weight: 600; color: var(--text); }
.msg-arrow { color: #999; }

.msg-account {
  background: var(--chip-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 12px;
  color: var(--text);
}

.msg-phone { color: var(--text-muted); }

.msg-time {
  margin-left: auto;
  color: var(--text-muted);
  font-size: 12px;
  white-space: nowrap;
  text-decoration: underline dotted;
  cursor: help;
}

.msg-submeta {
  margin-top: 5px;
  font-size: 11px;
  color: var(--text-muted);
}

.msg-dot { margin: 0 4px; }

.msg-text {
  margin-top: 8px;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.date-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 18px 0 10px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
}

.date-divider::before,
.date-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.date-divider:first-child { margin-top: 4px; }

.msg-filters { flex-wrap: wrap; align-items: center; }
.msg-filters select { min-width: 190px; }
.msg-filters input[type="search"] { flex: 1; min-width: 220px; }
.msg-summary { margin-left: 8px; font-size: 12px; font-weight: 400; color: var(--text-muted); }
.msg-hint { margin: 0 0 14px; font-size: 12px; color: var(--text-muted); }
.messages-list { max-height: 640px; overflow-y: auto; padding-right: 4px; }

.chat-filters input[type="search"] { flex: 1; min-width: 240px; }
.chat-filters select { min-width: 190px; }
.chat-title-cell { font-weight: 600; }

.chat-accounts { display: flex; align-items: center; gap: 10px; }
.chat-avatars { display: flex; }

.chat-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid var(--card);
  margin-left: -6px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
}

.chat-avatar:first-child { margin-left: 0; }
.chat-avatar-more { background: #e0e0e0; color: #555; }
.chat-account-count { white-space: nowrap; font-size: 13px; color: var(--text-muted); }
.chat-no-accounts { color: #999; font-size: 12px; font-style: italic; }

.restriction-cell { display: flex; flex-direction: column; gap: 4px; }

.restriction-badge {
  display: inline-block;
  border: none;
  border-radius: 12px;
  padding: 4px 9px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

button.restriction-badge { cursor: pointer; }

.restriction-active { background: #f8d7da; color: #721c24; }
.restriction-banned { background: #f5c6cb; color: #721c24; }
.restriction-warning { background: #fff3cd; color: #856404; }
.restriction-ok { background: #d4edda; color: #155724; }
.restriction-until { color: var(--text-muted); font-size: 11px; }
.account-filters { align-items: center; }
.account-filters select { min-width: 220px; }

.checkbox-label { display: flex; align-items: center; gap: 8px; text-transform: none; letter-spacing: 0; font-size: 14px; }
.checkbox-label input { width: auto; }
.settings-hint { display: block; margin-top: 4px; font-size: 11px; color: var(--text-muted); }
.settings-build { margin-top: 8px; }

.filters {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}

.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.tab {
  padding: 10px 16px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.tab.active {
  border-bottom-color: var(--primary);
  color: var(--primary);
  font-weight: 600;
}

/* ===== Тёмная тема: дополнительные подстройки ===== */
[data-theme="dark"] .card,
[data-theme="dark"] .stat-card {
  box-shadow: 0 1px 3px rgba(0,0,0,0.35);
}
[data-theme="dark"] .msg-dir-in { background: #1d3a2a; color: #7ed69a; }
[data-theme="dark"] .msg-dir-out { background: #17364d; color: #6fb7e8; }
[data-theme="dark"] .badge-active,
[data-theme="dark"] .badge-completed { background: #1d3a2a; color: #7ed69a; }
[data-theme="dark"] .badge-pending { background: #3d3419; color: #e5c878; }
[data-theme="dark"] .badge-error,
[data-theme="dark"] .badge-banned { background: #432028; color: #f1a3ae; }
[data-theme="dark"] .badge-paused { background: #2c3342; color: #aab3c5; }
[data-theme="dark"] .badge-running { background: #17364d; color: #6fb7e8; }
[data-theme="dark"] .restriction-active,
[data-theme="dark"] .restriction-banned { background: #432028; color: #f1a3ae; }
[data-theme="dark"] .restriction-warning { background: #3d3419; color: #e5c878; }
[data-theme="dark"] .restriction-ok { background: #1d3a2a; color: #7ed69a; }
[data-theme="dark"] .chat-avatar-more { background: #313a4f; color: #c4ccdd; }
[data-theme="dark"] .toast { background: #2a3245; }
[data-theme="dark"] input,
[data-theme="dark"] select,
[data-theme="dark"] textarea { background: #141925; color: var(--text); }

/* ===== Кнопки и иконки ===== */
.btn:not(.btn-primary):not(.btn-danger):not(.btn-success) { background: var(--border); color: var(--text); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-outline { background: transparent; border: 1px solid var(--border); }

.icon-btn {
  background: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 8px;
  color: var(--text);
  line-height: 1;
}
.icon-btn:hover { background: rgba(128,128,128,0.15); }

.burger { display: none; }

.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-right { display: flex; align-items: center; gap: 8px; }

/* ===== Статус связи ===== */
.status-live { color: var(--success); }
.status-online { color: var(--success); }
.status-offline { color: var(--danger); }

/* ===== Экран входа ===== */
.login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 3000;
}
.login-card {
  background: var(--card);
  border-radius: 16px;
  padding: 36px;
  width: 360px;
  max-width: 92vw;
  box-shadow: 0 10px 40px rgba(0,0,0,0.12);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-logo { font-size: 26px; font-weight: 700; text-align: center; }
.login-subtitle { margin: 0; text-align: center; color: var(--text-muted); font-size: 14px; }
.login-btn { width: 100%; padding: 12px; font-size: 15px; }
.login-error {
  background: rgba(220,53,69,0.12);
  color: var(--danger);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
}

/* ===== Бейдж непрочитанных в меню ===== */
.nav-badge {
  background: var(--danger);
  color: #fff;
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 11px;
  margin-left: 6px;
}

/* ===== Загрузка ===== */
.loading-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 70px 20px;
  gap: 14px;
}
.spinner {
  width: 38px;
  height: 38px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--text-muted); margin: 0; }

/* ===== Тосты ===== */
.toast { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; max-width: 420px; }
.toast-close {
  background: transparent;
  border: none;
  color: inherit;
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
  padding: 0 0 0 8px;
  opacity: 0.8;
}
.toast-close:hover { opacity: 1; }

/* ===== Модалки ===== */
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-header h2 { margin: 0; }
.modal-close { font-size: 24px; padding: 0 6px; }
.modal-body { max-height: 65vh; overflow-y: auto; }
.modal-body .form-group { margin-bottom: 12px; }
.modal-body input, .modal-body select, .modal-body textarea { width: 100%; }

/* ===== Таблицы ===== */
.table-wrap { overflow-x: auto; }
.table-wrap table { min-width: 640px; }
td .btn-small { margin: 2px 2px; white-space: nowrap; }

/* ===== Пагинация ===== */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-top: 16px;
  flex-wrap: wrap;
}
.pagination-info { color: var(--text-muted); font-size: 13px; }

/* ===== Прогресс-бар ===== */
.progress {
  background: var(--border);
  border-radius: 8px;
  height: 14px;
  overflow: hidden;
  min-width: 120px;
}
.progress-bar {
  background: var(--primary);
  height: 100%;
  border-radius: 8px;
  transition: width 0.4s ease;
}
.progress-bar.progress-success { background: var(--success); }
.progress-text { font-size: 12px; color: var(--text-muted); margin-top: 4px; display: block; }

/* ===== Массовые действия ===== */
.bulk-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--msg-out-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 14px;
}
.bulk-count { font-size: 13px; font-weight: 600; }

/* ===== Диалоги и переписка ===== */
.dialogs-layout {
  display: grid;
  grid-template-columns: 290px 1fr;
  gap: 20px;
  align-items: start;
}
.dialogs-panel {
  background: var(--card);
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  max-height: 640px;
  overflow-y: auto;
}
.dialog-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  color: var(--text);
}
.dialog-item:hover { background: var(--msg-in-bg); }
.dialog-item.active { background: var(--msg-out-bg); }
.dialog-title { font-weight: 600; font-size: 14px; display: flex; justify-content: space-between; gap: 8px; }
.dialog-count { color: var(--text-muted); font-weight: 400; font-size: 12px; }
.dialog-preview {
  color: var(--text-muted);
  font-size: 12px;
  margin-top: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dialog-time { color: var(--text-muted); font-size: 11px; margin-top: 2px; }
.conversation-note { color: var(--text-muted); font-size: 12px; margin-bottom: 12px; }
.load-more-wrap { text-align: center; margin: 14px 0; }

/* ===== Графики ===== */
.chart-container { width: 100%; overflow-x: auto; }
.chart-svg { width: 100%; min-width: 480px; height: auto; display: block; }
.chart-grid { stroke: var(--chart-grid); stroke-width: 1; }
.chart-axis { fill: var(--text-muted); font-size: 11px; }
.chart-legend { display: flex; gap: 18px; margin-bottom: 8px; flex-wrap: wrap; }
.chart-legend-item { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); }
.chart-legend-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

.hbars { display: flex; flex-direction: column; gap: 10px; }
.hbar-row { display: grid; grid-template-columns: 130px 1fr 50px; align-items: center; gap: 12px; }
.hbar-label { font-size: 13px; color: var(--text-muted); text-transform: capitalize; }
.hbar-track { background: var(--border); border-radius: 8px; height: 16px; overflow: hidden; }
.hbar-fill { height: 100%; border-radius: 8px; }
.hbar-value { font-size: 13px; font-weight: 600; text-align: right; }

/* ===== Прочее ===== */
.event-list { display: flex; flex-direction: column; gap: 10px; }
.event-item { display: flex; gap: 10px; align-items: baseline; font-size: 14px; }
.event-time { color: var(--text-muted); font-size: 12px; white-space: nowrap; margin-left: auto; }
.period-selector { display: flex; gap: 8px; align-items: center; }
.help-text { font-size: 12px; color: var(--text-muted); }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.mt-16 { margin-top: 16px; }
.sidebar-backdrop { display: none; }

/* ===== Адаптив ===== */
@media (max-width: 1100px) {
  .dialogs-layout { grid-template-columns: 240px 1fr; }
}

@media (max-width: 900px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 1500;
  }
  .sidebar.open { transform: none; }
  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 1400;
  }
  .main { margin-left: 0; }
  .burger { display: inline-flex; }
  .content { padding: 14px; }
  .form-row { flex-direction: column; align-items: stretch; }
  .form-row .btn { align-self: flex-start; }
  .dialogs-layout { grid-template-columns: 1fr; }
  .dialogs-panel { max-height: 300px; }
  .topbar { padding: 12px 14px; flex-wrap: wrap; gap: 8px; }
  .topbar h1 { font-size: 18px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .msg-time { margin-left: 0; }
  .hbar-row { grid-template-columns: 90px 1fr 40px; }
}
