:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #b8322a;
  --accent-dark: #96281f;
  --primary: #2f5d8a;
  --primary-dark: #24486b;
  --border: #e3e6ea;
  --radius: 14px;
}

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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
}

.page {
  width: 100%;
  max-width: 860px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.header h1 { font-size: 22px; font-weight: 700; }
.subtitle { color: var(--muted); font-size: 14px; margin-top: 4px; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 15px;
  cursor: pointer;
  transition: filter 0.15s ease;
}
.btn:hover { filter: brightness(0.96); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-accent:hover { background: var(--accent-dark); }
.btn-accent.pulse { animation: pulse 1.6s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(184, 50, 42, 0.45); }
  50% { box-shadow: 0 0 0 10px rgba(184, 50, 42, 0); }
}

.chat {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 320px;
  max-height: calc(100vh - 260px);
}

.msg {
  max-width: 82%;
  padding: 12px 14px;
  border-radius: 12px;
  line-height: 1.5;
  font-size: 15px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.bot {
  align-self: flex-start;
  background: #eef1f5;
  border-bottom-left-radius: 4px;
}

.msg.bot.escalated { background: #fdf1f0; border: 1px solid #f2c9c5; }
.msg.bot.crisis { background: #fff7e6; border: 1px solid #f0d9a8; }

.sources {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  font-size: 12.5px;
  color: var(--muted);
}

.typing {
  align-self: flex-start;
  color: var(--muted);
  font-size: 14px;
  padding: 8px 4px;
}

.composer {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.composer input {
  flex: 1;
  padding: 13px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  background: var(--panel);
}
.composer input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.footer {
  margin-top: 12px;
  text-align: center;
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.4;
}

/* Модальное окно */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 30, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
}

/* ВАЖНО: display:flex выше перекрывает атрибут hidden — возвращаем скрытие */
.modal-overlay[hidden] { display: none; }

.modal { position: relative; }

.modal-close {
  position: absolute;
  top: 10px;
  right: 12px;
  border: none;
  background: none;
  font-size: 26px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }

.modal {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 440px;
}

.modal h2 { font-size: 19px; margin-bottom: 6px; }
.modal-hint { color: var(--muted); font-size: 14px; margin-bottom: 16px; }

.modal label {
  display: block;
  font-size: 14px;
  margin-bottom: 12px;
  font-weight: 600;
}

.modal input, .modal textarea {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14.5px;
  font-family: inherit;
  font-weight: 400;
  resize: vertical;
}

.req { color: var(--accent); }

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

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1f2937;
  color: #fff;
  padding: 12px 20px;
  border-radius: 10px;
  font-size: 14.5px;
  z-index: 20;
  animation: fadein 0.25s ease;
}

@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

/* Шапка: действия и меню пользователя */
.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 8px;
}
.user-menu[hidden] { display: none; }

.user-name { font-weight: 600; font-size: 14.5px; }

.role-badge {
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  padding: 3px 9px;
  border-radius: 999px;
}

.btn-small { padding: 6px 12px; font-size: 13.5px; }
.btn-danger { color: var(--accent); border-color: #f2c9c5; }
.btn-danger:hover { background: #fdf1f0; }

/* Вкладки */
.tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
}

.tab {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--muted);
  padding: 9px 18px;
  border-radius: 10px;
  font-size: 14.5px;
  cursor: pointer;
}
.tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  font-weight: 600;
}

.view[hidden] { display: none; }
.view-title { font-size: 19px; margin-bottom: 12px; }
.view-subtitle { font-size: 16px; margin: 18px 0 10px; }
.muted { color: var(--muted); font-size: 14px; }
.login-hint { display: block; margin-top: 4px; }
.login-hint[hidden] { display: none; }

/* Карточки (история, заявки) */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
}
.card-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.card-answer { white-space: pre-wrap; font-size: 14.5px; line-height: 1.5; }
.status {
  background: #fff3d6;
  color: #8a6d1a;
  font-size: 12.5px;
  padding: 2px 10px;
  border-radius: 999px;
  align-self: center;
}
.status-done { background: #e2f5e3; color: #1e7a2e; }

/* Карточка заявки в панели консультанта */
.esc-card { border-left: 4px solid #e8b53a; }
.esc-card.esc-answered { border-left-color: #2e9e46; }

.summary-block {
  background: #eef4fb;
  border: 1px solid #d3e1f0;
  border-radius: 9px;
  padding: 10px 12px;
  margin-top: 8px;
  font-size: 14px;
  white-space: pre-wrap;
}

.dialog-details { margin-top: 8px; font-size: 14px; }
.dialog-details summary { cursor: pointer; color: var(--primary); }
.dialog-log {
  margin-top: 8px;
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px;
  background: #fafbfc;
}
.dialog-line { margin-bottom: 6px; font-size: 13.5px; line-height: 1.45; }
.dialog-line.user b { color: var(--primary); }
.dialog-line.bot b { color: var(--muted); }

.reply-form {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  align-items: flex-start;
}
.reply-form textarea {
  flex: 1;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
}

.reply-block {
  background: #f0f9f1;
  border: 1px solid #cde8d0;
  border-radius: 9px;
  padding: 10px 12px;
  margin-top: 10px;
  font-size: 14px;
  white-space: pre-wrap;
}

/* Панель консультанта */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
}
.stat-num { font-size: 24px; font-weight: 700; color: var(--primary); }
.stat-label { color: var(--muted); font-size: 13px; margin-top: 4px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}
.panel h3 { font-size: 16px; margin-bottom: 8px; }
.panel-hint { color: var(--muted); font-size: 13.5px; margin-bottom: 12px; }

.upload-form { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.upload-status { font-size: 13.5px; color: var(--muted); margin: 10px 0; min-height: 18px; }

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  margin-top: 8px;
}
.table th, .table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.table th { color: var(--muted); font-weight: 600; font-size: 13px; }

/* Вкладки входа/регистрации */
.auth-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 16px;
}
.auth-tab {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 9px;
  border-radius: 9px;
  font-size: 14.5px;
  cursor: pointer;
  color: var(--muted);
}
.auth-tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  font-weight: 600;
}

@media (max-width: 560px) {
  .header { flex-direction: column; }
  .msg { max-width: 94%; }
  .chat { max-height: calc(100vh - 300px); }
}
