:root {
  --bg: var(--tg-theme-bg-color, #f4f6fa);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --surface-alt: #eef2f8;
  --text: var(--tg-theme-text-color, #111827);
  --muted: var(--tg-theme-hint-color, #6b7280);
  --accent: var(--tg-theme-button-color, #2563eb);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --border: rgba(15, 23, 42, 0.1);
  --success: #059669;
  --success-bg: rgba(5, 150, 105, 0.12);
  --warning: #d97706;
  --warning-bg: rgba(217, 119, 6, 0.12);
  --danger: #dc2626;
  --danger-bg: rgba(220, 38, 38, 0.12);
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.05);
  --radius: 14px;
  --nav-height: 64px;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-alt: #273449;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --accent-text: #ffffff;
  --border: rgba(148, 163, 184, 0.18);
  --shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
}

.hidden { display: none !important; }

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  max-width: 960px;
  margin: 0 auto;
  padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + 12px);
}

/* Шапка */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(10px);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-logo {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.brand-title {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-sub {
  font-size: 11px;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  position: relative;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Контент */
.view-root {
  padding: 14px;
  flex: 1;
  animation: fadeIn 0.18s ease;
}

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

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 16px 0 10px;
}

.section-header:first-child { margin-top: 0; }

.section-title {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
}

/* Карточки и сетки */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

@media (min-width: 640px) {
  .kpi-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
}

.kpi-value {
  font-size: 22px;
  font-weight: 800;
  line-height: 1.15;
  margin-bottom: 4px;
}

.kpi-label {
  font-size: 12px;
  color: var(--muted);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
}

.card-clickable {
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.15s ease;
}

.card-clickable:active {
  transform: scale(0.99);
}

.card-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.card-title {
  font-weight: 700;
  font-size: 14px;
  margin: 0 0 4px;
  word-break: break-word;
}

.card-meta {
  font-size: 12px;
  color: var(--muted);
  word-break: break-word;
}

/* Бейджи статусов */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.pill-success { background: var(--success-bg); color: var(--success); }
.pill-warning { background: var(--warning-bg); color: var(--warning); }
.pill-danger { background: var(--danger-bg); color: var(--danger); }
.pill-neutral { background: var(--surface-alt); color: var(--muted); }
.pill-accent { background: rgba(37, 99, 235, 0.14); color: var(--accent); }

/* Кнопки */
.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: opacity 0.12s ease;
}

.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-secondary { background: var(--surface-alt); color: var(--text); border-color: var(--border); }
.btn-danger { background: var(--danger-bg); color: var(--danger); border-color: rgba(220, 38, 38, 0.25); }
.btn-success { background: var(--success-bg); color: var(--success); border-color: rgba(5, 150, 105, 0.25); }
.btn-sm { padding: 6px 10px; font-size: 12px; border-radius: 8px; }
.btn-block { width: 100%; }

/* Инпуты и фильтры */
.input, .select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 14px;
  outline: none;
}

.input:focus, .select:focus {
  border-color: var(--accent);
}

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

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 5px;
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.chip {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.chip.active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

/* Фрагменты текста и подсветка */
.snippet-box {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface-alt);
  border-left: 3px solid var(--accent);
  font-size: 12px;
  color: var(--text);
  word-break: break-word;
}

mark {
  background: #fde047;
  color: #111827;
  padding: 0 2px;
  border-radius: 3px;
}

/* Тумблер настроек */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.setting-row:last-child { border-bottom: none; }

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute;
  inset: 0;
  background: var(--muted);
  border-radius: 999px;
  transition: 0.2s;
  cursor: pointer;
}
.slider::before {
  content: "";
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(20px); }

/* Нижняя панель навигации */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 40;
  height: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.nav-item {
  flex: 1;
  height: var(--nav-height);
  border: none;
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 10px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 2px;
}

.nav-item.active { color: var(--accent); }

/* Модальное окно */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 12px;
}

@media (min-width: 600px) {
  .modal-backdrop { align-items: center; }
}

.modal-card {
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 16px;
  padding: 18px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.3);
}

/* Полноэкранный PDF-просмотрщик */
.pdf-overlay {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

.pdf-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.pdf-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.pdf-body {
  flex: 1;
  overflow: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.pdf-canvas-wrap {
  background: #fff;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  overflow: hidden;
  max-width: 100%;
}

.pdf-canvas-wrap canvas {
  display: block;
  max-width: 100%;
  height: auto !important;
}

.pdf-matches-panel {
  width: 100%;
  max-width: 820px;
  margin-top: 12px;
}

/* Лоадер, пустое состояние, тост */
.loader-block, .empty-state {
  text-align: center;
  padding: 36px 16px;
  color: var(--muted);
}

.spinner {
  width: 30px;
  height: 30px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 12px;
  animation: spin 0.7s linear infinite;
}

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

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-height) + 18px);
  transform: translateX(-50%);
  z-index: 100;
  background: #111827;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  max-width: 90vw;
  text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}
