:root {
  --bg: #f5f8f7;
  --surface: #ffffff;
  --surface-soft: #eef6f3;
  --text: #243331;
  --muted: #70807c;
  --primary: #2f8f78;
  --primary-hover: #24735f;
  --primary-soft: #dcefe9;
  --border: #d9e5e1;
  --danger: #d65d5d;
  --danger-hover: #b94747;
  --shadow: 0 12px 32px rgba(37, 82, 70, 0.09);
  --header-bg: rgba(255, 255, 255, 0.92);
}

body.dark-theme {
  --bg: #14201e;
  --surface: #1c2b28;
  --surface-soft: #243834;
  --text: #edf7f4;
  --muted: #aabdb7;
  --primary: #54b89f;
  --primary-hover: #70ccb5;
  --primary-soft: #284b43;
  --border: #38514b;
  --danger: #eb7777;
  --danger-hover: #ff9191;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.22);
  --header-bg: rgba(28, 43, 40, 0.93);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  color: var(--text);
  background: var(--bg);
  font-family: Inter, Arial, sans-serif;
  transition: background-color 0.25s ease, color 0.25s ease;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
  backdrop-filter: blur(12px);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  max-width: 1280px;
  min-height: 76px;
  margin: 0 auto;
  padding: 14px 32px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text);
  text-decoration: none;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border-radius: 12px;
  color: #fff;
  background: var(--primary);
  box-shadow: 0 6px 14px rgba(47, 143, 120, 0.24);
  font-size: 22px;
  font-weight: 800;
}

.brand-name { display: block; font-size: 18px; font-weight: 800; letter-spacing: 0.02em; }
.brand-description { display: block; margin-top: 2px; color: var(--muted); font-size: 12px; }

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

.theme-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  background: var(--surface);
  transition: 0.2s ease;
}
.theme-toggle:hover { border-color: var(--primary); color: var(--primary); }
.theme-icon { display: inline-flex; align-items: center; justify-content: center; }
.theme-icon svg { display: block; }

.user-button {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-height: 42px;
  padding: 0 14px;
  border: 0;
  border-radius: 10px;
  color: #fff;
  background: var(--primary);
  font-weight: 700;
  transition: background-color 0.2s ease;
  text-decoration: none;
}
.user-button:hover { background: var(--primary-hover); }

.nav-link {
  display: inline-flex;
  align-items: center;
  min-height: 42px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  background: var(--surface);
  text-decoration: none;
  font-weight: 700;
  transition: 0.2s ease;
}
.nav-link:hover { border-color: var(--primary); color: var(--primary); }

.container { max-width: 1280px; margin: 0 auto; padding: 34px 32px 48px; }

.page-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 26px;
}
.page-heading h1 { margin: 0; font-size: clamp(25px, 3vw, 34px); line-height: 1.2; }
.page-heading p { margin: 8px 0 0; color: var(--muted); font-size: 15px; }

.draft-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  color: var(--primary);
  background: var(--primary-soft);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--primary); }

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

.section-title {
  display: flex;
  align-items: center;
  gap: 11px;
  margin: 0 0 6px;
  font-size: 20px;
}
.section-number {
  display: grid;
  width: 29px;
  height: 29px;
  place-items: center;
  border-radius: 50%;
  color: #fff;
  background: var(--primary);
  font-size: 14px;
  font-weight: 800;
}
.section-note { margin: 0 0 20px 40px; color: var(--muted); font-size: 14px; }

.settings-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.field { margin-bottom: 14px; }
.field label {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.045em;
}
.field select, .field input, .field textarea {
  width: 100%;
  min-height: 45px;
  padding: 10px 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
  color: var(--text);
  background: var(--surface);
}
.field select:focus, .field input:focus, .field textarea:focus, .text-editor:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(47, 143, 120, 0.15);
}

.recording-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 17px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: var(--surface-soft);
}
.recording-info { display: flex; align-items: center; gap: 12px; }
.microphone {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border-radius: 10px;
  color: var(--primary);
  background: var(--surface);
  font-size: 19px;
}
.recording-info strong, .recording-info span { display: block; }
.recording-info strong { font-size: 14px; }
.recording-info span { margin-top: 3px; color: var(--muted); font-size: 13px; }
.recording-actions { display: flex; align-items: center; gap: 10px; }
.timer {
  min-width: 58px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  font-weight: 700;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 700;
  transition: 0.2s ease;
  text-decoration: none;
}
.btn:disabled { opacity: 0.6; cursor: default; }
.btn-primary { color: #fff; background: var(--primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-outline { border-color: var(--border); color: var(--text); background: var(--surface); }
.btn-outline:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.btn-danger { color: #fff; background: var(--danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 13px; }

.text-editor {
  width: 100%;
  min-height: 230px;
  resize: vertical;
  padding: 17px;
  border: 1px solid var(--border);
  border-radius: 13px;
  outline: none;
  color: var(--text);
  background: var(--surface);
  line-height: 1.58;
  transition: 0.2s ease;
}

.editor-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-top: 12px;
}
.editor-hint { color: var(--muted); font-size: 13px; }

.generation-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-bottom: 19px;
}
.generation-status { display: none; align-items: center; gap: 9px; color: var(--primary); font-size: 14px; font-weight: 700; }
.generation-status.active { display: inline-flex; }
.spinner {
  width: 17px;
  height: 17px;
  border: 2px solid var(--primary-soft);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.result-editor { min-height: 310px; }
.result-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 14px;
}

/* Статус отправки в 1С */
.onec-status {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--surface-2, rgba(127,127,127,0.08));
  font-size: 14px;
  flex-wrap: wrap;
}
.onec-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-weight: 700;
  font-size: 13px;
  color: #fff;
  flex-shrink: 0;
}
.onec-badge.onec-sent { background: #16a34a; }
.onec-badge.onec-pending { background: #d97706; }
.onec-badge.onec-error { background: #dc2626; }
.onec-text { color: var(--text); flex: 1 1 auto; min-width: 0; word-break: break-word; }

/* Переключатель (checkbox + подпись) */
.switch-row {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
  font-weight: 500;
}
.switch-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  cursor: pointer;
}

.footer-note { margin: 2px 0 0; color: var(--muted); font-size: 12px; text-align: center; }

/* Таблицы (админка) */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
  padding: 11px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.table th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.table td .btn { margin-right: 6px; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  background: var(--primary-soft);
  color: var(--primary);
}
.badge-muted { background: var(--surface-soft); color: var(--muted); }

.tabs { display: flex; gap: 8px; margin-bottom: 22px; flex-wrap: wrap; }
.tab {
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
}
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Модальное окно */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-overlay.active { display: flex; }
.modal {
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 26px;
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.modal h3 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

.checkbox-list { display: flex; flex-direction: column; gap: 8px; max-height: 200px; overflow-y: auto; }
.checkbox-list label {
  display: flex; align-items: center; gap: 9px;
  text-transform: none; letter-spacing: 0; font-weight: 500; font-size: 14px; color: var(--text);
  margin: 0;
}
.checkbox-list input { width: auto; min-height: auto; }

/* Страница входа */
.auth-wrapper {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}
.auth-card {
  width: 100%;
  max-width: 400px;
  padding: 34px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.auth-card .brand { justify-content: center; margin-bottom: 8px; }
.auth-title { text-align: center; margin: 4px 0 22px; font-size: 22px; }

.alert {
  display: none;
  margin: 12px 0;
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 14px;
}
.alert.active { display: block; }
.alert-error { background: rgba(214, 93, 93, 0.12); color: var(--danger); }
.alert-success { background: var(--primary-soft); color: var(--primary); }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 20px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-weight: 700;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.25s ease;
  z-index: 200;
  pointer-events: none;
}
.toast.show { opacity: 1; }

/* ===== Планшеты и небольшие ноутбуки ===== */
@media (max-width: 900px) {
  .header-inner, .container { padding-right: 20px; padding-left: 20px; }
  .container { padding-top: 26px; padding-bottom: 36px; }
  .settings-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ===== Телефоны ===== */
@media (max-width: 640px) {
  .header-inner {
    flex-wrap: wrap;
    gap: 10px 12px;
    min-height: 60px;
    padding: 12px 16px;
  }
  .container { padding: 20px 16px 34px; }

  .brand-mark { width: 38px; height: 38px; font-size: 20px; }
  .brand-name { font-size: 16px; }
  .brand-description { display: none; }

  /* Действия в шапке — компактно, с переносом и прокруткой при нужде */
  .header-actions {
    order: 3;
    width: 100%;
    flex-wrap: wrap;
    gap: 8px;
  }
  /* Скрываем только подпись, иконка остаётся видимой */
  #themeText { display: none; }
  .theme-toggle { padding: 0 12px; }
  .theme-toggle, .nav-link, .user-button { min-height: 40px; }
  #userName { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Заголовок страницы */
  .page-heading { align-items: flex-start; flex-direction: column; gap: 10px; margin-bottom: 20px; }
  .draft-status { align-self: flex-start; }

  /* Настройки в одну колонку */
  .settings-grid { grid-template-columns: 1fr; gap: 12px; }

  /* Карточки компактнее */
  .card { padding: 16px; border-radius: 14px; margin-bottom: 16px; }
  .section-title { font-size: 18px; }
  .section-note { margin-left: 0; }

  /* Панель записи — вертикально, кнопки во всю ширину */
  .recording-panel { align-items: stretch; flex-direction: column; gap: 12px; }
  .recording-actions { width: 100%; }
  .recording-actions .btn { flex: 1; }
  .timer { min-width: auto; text-align: center; }

  /* Редакторы и футеры */
  .text-editor { min-height: 200px; padding: 14px; }
  .result-editor { min-height: 240px; }
  .editor-footer { flex-direction: column; align-items: stretch; gap: 10px; }
  .editor-footer .btn { width: 100%; }
  .generation-row { flex-direction: column; align-items: stretch; }
  .generation-row .btn { width: 100%; }
  .result-actions { justify-content: stretch; }
  .result-actions .btn { flex: 1 1 auto; }

  /* Вкладки админки — горизонтальная прокрутка без переноса */
  .tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { white-space: nowrap; flex: 0 0 auto; }

  /* Таблицы админки -> карточки */
  .table, .table thead, .table tbody, .table tr, .table th, .table td { display: block; }
  .table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .table tr {
    margin-bottom: 14px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface-soft);
  }
  .table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
    text-align: right;
  }
  .table td:last-child { border-bottom: 0; padding-bottom: 0; }
  .table td::before {
    content: attr(data-label);
    flex: 0 0 40%;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    text-align: left;
  }
  /* Ячейка с кнопками действий — в столбец на всю ширину.
     Правило работает и по классу actions-cell, и по любой ячейке с несколькими кнопками */
  .table td.actions-cell,
  .table td:has(.btn) {
    flex-direction: column;
    align-items: stretch;
    white-space: normal;
  }
  .table td.actions-cell::before,
  .table td:has(.btn)::before { margin-bottom: 6px; }
  .table td.actions-cell .btn,
  .table td:has(.btn) .btn {
    width: 100%;
    margin: 0 0 6px;
  }
  .table td.actions-cell .btn:last-child,
  .table td:has(.btn) .btn:last-child { margin-bottom: 0; }

  /* Модалки */
  .modal { padding: 20px; border-radius: 14px; max-height: 92vh; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions .btn { width: 100%; }

  /* Форма входа */
  .auth-card { padding: 26px 20px; }

  .toast { left: 16px; right: 16px; transform: none; text-align: center; }
  .toast.show { transform: none; }
}

/* ===== Узкие экраны ===== */
@media (max-width: 380px) {
  .brand-name { font-size: 15px; }
  .nav-link, .user-button { padding: 0 11px; font-size: 13px; }
  .table td::before { flex-basis: 45%; }
}
