:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --secondary: #64748b;
  --accent: #f59e0b;
  --success: #10b981;
  --danger: #ef4444;
  --bg-body: #f1f5f9;
  --bg-card: #ffffff;
  --bg-sidebar: #1e293b;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --shadow:
    0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --radius: 12px;
  --font-main: "Inter", sans-serif;
}

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

body {
  font-family: var(--font-main);
  background-color: var(--bg-body);
  color: var(--text-main);
  font-size: 14px;
  line-height: 1.5;
  height: 100dvh; /* 100dvh решает проблему с адресной строкой на iOS/Android */
  overflow: hidden;
}

/* --- Layout --- */
#app {
  height: 100%;
  width: 100%;
}

.screen {
  display: none;
  height: 100%;
  width: 100%;
}

.screen.active {
  display: flex;
}

/* Login Screen */
#login-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #4f46e5 0%, #8b5cf6 100%);
  padding: 1rem;
}

.login-card {
  background: white;
  padding: 2.5rem;
  border-radius: var(--radius);
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.login-header {
  margin-bottom: 2rem;
}

.logo-icon {
  font-size: 2rem;
  color: var(--primary);
  background: #e0e7ff;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1rem;
}

.login-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: #1e293b;
}

.login-header p {
  color: var(--text-muted);
}

/* Main Screen Layout */
#main-screen {
  flex-direction: row;
}

/* Sidebar */
.sidebar {
  width: 260px;
  background: var(--bg-sidebar);
  color: white;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  height: 100%;
  z-index: 50;
  transition: all 0.3s ease;
}

.sidebar-header {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  font-size: 1.25rem;
  font-weight: 700;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header i.fa-paper-plane {
  color: #818cf8;
}

.mobile-menu-btn {
  display: none;
  background: transparent;
  border: none;
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.25rem;
}

.main-nav {
  flex: 1;
  padding: 1.5rem 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.nav-divider {
  font-size: 0.75rem;
  text-transform: uppercase;
  color: #94a3b8;
  margin: 1.5rem 0 0.5rem 1rem;
  font-weight: 600;
  letter-spacing: 0.05em;
}

.nav-btn {
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: #cbd5e1;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: all 0.2s;
  margin-bottom: 0.25rem;
}

.nav-btn i {
  width: 20px;
  text-align: center;
}

.nav-btn:hover {
  background: rgba(255, 255, 255, 0.05);
  color: white;
}

.nav-btn.active {
  background: var(--primary);
  color: white;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
}

.sidebar-footer {
  padding: 1rem;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  overflow: hidden;
}

.avatar {
  width: 36px;
  height: 36px;
  background: #475569;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #e2e8f0;
  flex-shrink: 0;
}

.user-details {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.user-label {
  font-size: 0.7rem;
  color: #94a3b8;
}

#current-user {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.logout-btn {
  background: transparent;
  border: none;
  color: #ef4444;
  cursor: pointer;
  font-size: 1.1rem;
  padding: 5px;
  opacity: 0.8;
  transition: opacity 0.2s;
  flex-shrink: 0;
}

.logout-btn:hover {
  opacity: 1;
}

/* Main Content */
.main-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1.5rem;
  background: var(--bg-body);
  display: flex;
  flex-direction: column;
  -webkit-overflow-scrolling: touch;
}

.page {
  display: none;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
  flex: 1;
  animation: fadeIn 0.3s ease;
}

.page.active {
  display: flex;
  flex-direction: column;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: 1rem;
  flex-shrink: 0;
}

.page-header h2 {
  font-size: 1.5rem;
  font-weight: 700;
  color: #1e293b;
}

/* Components */
.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  margin-bottom: 1.5rem;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.card-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  flex-wrap: wrap;
  gap: 10px;
}

.card-header h3 {
  font-size: 1.1rem;
  font-weight: 600;
  color: #0f172a;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-header.bg-gradient {
  background: linear-gradient(135deg, #4f46e5 0%, #6366f1 100%);
  border-bottom: none;
}

.text-white {
  color: white !important;
}

.card-toolbar {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #f8fafc;
  flex-wrap: wrap;
  gap: 10px;
}

/* Grids */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1.5rem;
}

.stat-card {
  background: white;
  padding: 1.25rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 1.25rem;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  background: #e0e7ff;
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  flex-shrink: 0;
}

.stat-info h4 {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 500;
  margin-bottom: 0.25rem;
}

.stat-info .value {
  font-size: 1.5rem;
  font-weight: 700;
  color: #0f172a;
  line-height: 1.2;
}

.grid-2-1 {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 1.5rem;
  align-items: start;
}

.apps-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1.5rem;
  padding: 1.5rem;
}

/* Forms */
.clean-form {
  padding: 1.5rem;
}

.input-group {
  margin-bottom: 1.25rem;
  width: 100%;
}

.input-group label {
  display: flex;
  align-items: center;
  font-weight: 500;
  margin-bottom: 0.5rem;
  color: #334155;
  font-size: 0.9rem;
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="time"],
input[type="datetime-local"],
textarea,
select {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.95rem;
  transition:
    border-color 0.2s,
    box-shadow 0.2s;
  font-family: inherit;
  background: #fff;
}

input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

input:disabled {
  background: #f1f5f9;
  color: #94a3b8;
  cursor: not-allowed;
}

.input-wrapper {
  position: relative;
  display: flex;
  width: 100%;
}

.input-wrapper > i {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
  pointer-events: none;
}

.input-wrapper > input {
  padding-left: 36px;
}

.input-wrapper.suffix input {
  padding-left: 10px;
  padding-right: 45px;
}

.input-wrapper.suffix span {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
  font-size: 0.85rem;
  pointer-events: none;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.65rem 1.25rem;
  font-weight: 500;
  border-radius: 6px;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
  gap: 8px;
  font-size: 0.9rem;
  white-space: nowrap;
  font-family: inherit;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-block {
  width: 100%;
}

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

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

.btn-secondary {
  background: white;
  border: 1px solid #cbd5e1;
  color: #475569;
}

.btn-secondary:hover:not(:disabled) {
  background: #f8fafc;
  border-color: #94a3b8;
}

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

.btn-accent:hover:not(:disabled) {
  filter: brightness(0.9);
}

.btn-danger {
  background: #fee2e2;
  color: #ef4444;
}

.btn-danger:hover:not(:disabled) {
  background: #fecaca;
}

.btn-warning {
  background: #fffbeb;
  color: #b45309;
  border: 1px solid #fcd34d;
}

.btn-warning:hover:not(:disabled) {
  background: #fef3c7;
}

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  background: #f1f5f9;
  color: #64748b;
  flex-shrink: 0;
}

.btn-icon:hover:not(:disabled) {
  background: #e2e8f0;
  color: var(--primary);
}

.btn-sm {
  padding: 0.4rem 0.8rem;
  font-size: 0.85rem;
}

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

.btn-outline:hover:not(:disabled) {
  background: #f1f5f9;
  color: var(--primary);
}

/* Lists & Feeds */
.list-feed {
  padding: 0;
  overflow-y: auto;
}

.bulk-item {
  padding: 1.25rem;
  border-bottom: 1px solid var(--border);
  animation: fadeIn 0.3s;
}

.bulk-item:last-child {
  border-bottom: none;
}

.bulk-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
  gap: 10px;
}

.bulk-title {
  font-weight: 600;
  color: #1e293b;
}

.bulk-msg {
  color: #64748b;
  font-size: 0.9rem;
  margin-bottom: 0.75rem;
  line-height: 1.4;
  word-break: break-word;
}

/* Status Badges */
.badge {
  padding: 2px 10px;
  border-radius: 99px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  display: inline-block;
  white-space: nowrap;
}

.badge-success {
  background: #dcfce7;
  color: #166534;
  border: 1px solid #bbf7d0;
}
.badge-error {
  background: #fee2e2;
  color: #991b1b;
  border: 1px solid #fecaca;
}
.badge-process {
  background: #e0f2fe;
  color: #075985;
  border: 1px solid #bae6fd;
}
.badge-wait {
  background: #f3f4f6;
  color: #475569;
  border: 1px solid #e2e8f0;
}

/* Tables */
.table-wrapper {
  overflow-x: auto;
  width: 100%;
  -webkit-overflow-scrolling: touch;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  min-width: 600px; /* Предотвращает сильное сжатие колонок на узких экранах */
}

.data-table th {
  text-align: left;
  padding: 1rem;
  background: #f8fafc;
  color: #64748b;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  color: #334155;
  vertical-align: middle;
}

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

.data-table tr:hover {
  background: #f8fafc;
}

/* Alerts */
.alert {
  padding: 1rem;
  border-radius: 8px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 1rem;
}

.alert-error {
  background: #fef2f2;
  color: #b91c1c;
  border: 1px solid #fecaca;
}

/* Form layouts */
.flex-row {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}
.flex-row > div {
  flex: 1;
  min-width: 120px;
}

.hint {
  color: #64748b;
  margin-top: 0.5rem;
  display: block;
  font-size: 0.8rem;
  line-height: 1.3;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}

.modal-content {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  width: 100%;
  max-width: 600px;
  max-height: 90dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: fadeIn 0.2s ease;
}

.modal-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.close-modal {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.2s;
  flex-shrink: 0;
}

.close-modal:hover {
  background: #f1f5f9;
  color: var(--danger);
}

/* Utilities */
.text-truncate {
  max-width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
  vertical-align: bottom;
}

.center-container {
  display: flex;
  justify-content: center;
}
.max-w-600 {
  width: 100%;
  max-width: 600px;
}
.h-fit {
  height: fit-content;
}
.mt-4 {
  margin-top: 1rem;
}
.mb-4 {
  margin-bottom: 1rem;
}
.p-4 {
  padding: 1.5rem;
}

.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: #94a3b8;
  font-style: italic;
  font-size: 0.95rem;
}

.date-preset {
  transition: all 0.15s;
}
.date-preset.active-preset {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.date-preset.active-preset:hover {
  background: var(--primary-hover);
}

.fa-file-csv {
  color: #16a34a;
}

/* ----------------------------------------------------
   ПРИНУДИТЕЛЬНЫЕ ИСПРАВЛЕНИЯ ИНЛАЙН СТИЛЕЙ ИЗ HTML
------------------------------------------------------- */

/* Страница истории уведомлений должна тянуться по всей высоте */
#page-history .card {
  height: auto !important;
  flex: 1 !important;
  min-height: 400px;
}

#notifications-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: auto;
  min-height: 0;
  position: relative;
}

#notifications-list table thead th {
  background: #f8fafc;
  border-bottom: 2px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 2;
}

/* Размеры графиков */
#chart-new-devices,
#chart-funnel,
#chart-success-rate {
  max-height: 250px;
  width: 100% !important;
}

/* ----------------------------------------------------
   МЕДИАЗАПРОСЫ (АДАПТИВНОСТЬ)
------------------------------------------------------- */

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

  /* Отключаем жесткие сетки из HTML на планшетах/мобилках */
  #page-settings > div[style*="grid-template-columns"],
  #page-stats > div[style*="grid-template-columns"],
  #page-settings .clean-form[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }
}

@media (max-width: 768px) {
  #main-screen {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    height: auto;
    flex-direction: column;
  }

  .sidebar-header {
    height: 60px;
  }

  .mobile-menu-btn {
    display: block;
  }

  .main-nav {
    display: none;
    padding: 1rem;
  }

  .sidebar-footer {
    display: none;
  }

  /* Состояния при открытом мобильном меню */
  .sidebar.mobile-open .main-nav {
    display: flex;
  }

  .sidebar.mobile-open .sidebar-footer {
    display: flex;
  }

  .main-content {
    padding: 1rem;
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
    margin-bottom: 1rem;
  }

  .reverse-mobile {
    display: flex;
    flex-direction: column-reverse;
  }

  .card-header,
  .card-toolbar {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
  }

  /* Поля фильтров во всю ширину */
  .filters-group,
  .filter {
    width: 100%;
    flex-wrap: wrap;
  }

  .header-actions {
    width: 100%;
    display: flex;
    gap: 10px;
  }

  .header-actions button {
    flex: 1;
  }

  #scheduler-status-bar {
    padding: 10px !important;
  }

  /* Фиксы для страницы истории на телефоне */
  #page-history > section > div:first-child {
    padding: 1rem !important;
  }

  #page-history select,
  #page-history input {
    width: 100% !important;
    min-width: 100% !important;
  }

  /* Модалка ключей на всю ширину, кнопки действий тянутся */
  .modal-content {
    max-width: 100vw !important;
    width: 100vw !important;
  }
  .key-row td div button {
    flex: 1;
  }
}

/* ----------------------------------------------------
   API-КЛЮЧИ И ДОКУМЕНТАЦИЯ
------------------------------------------------------- */

/* Блок с примером кода (curl / Dart) */
.code-block {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  font-size: 0.8rem;
  overflow-x: auto;
  white-space: pre;
  line-height: 1.5;
  font-family: "Courier New", Courier, monospace;
  color: #334155;
  margin: 0;
}

/* Ограничение ширины префикса ключа в таблице */
.key-row code,
.key-row .text-truncate {
  display: inline-block;
  max-width: 220px;
  vertical-align: middle;
}

/* Цветовые модификаторы бейджей типов ключа (в рамках существующей палитры) */
.key-type-client {
  background: #e0e7ff;
  color: var(--primary);
  border: 1px solid #c7d2fe;
}
.key-type-server {
  background: #fee2e2;
  color: var(--danger);
  border: 1px solid #fecaca;
}
