/* ====================================================================
   LexGuard — красно-чёрная тема, максимально простой интерфейс
   ==================================================================== */

:root {
  color-scheme: dark;

  --bg: #0a0a0a;
  --bg-elevated: #111111;
  --card: #1a1a1a;
  --card-2: #222222;
  --separator: rgba(255, 255, 255, 0.08);
  --separator-strong: rgba(255, 255, 255, 0.15);

  --text: #f0f0f0;
  --text-2: #999999;
  --text-3: #666666;

  --accent: #e63946;
  --accent-soft: rgba(230, 57, 70, 0.15);
  --accent-text: #ffffff;

  --success: #2ecc71;
  --danger: #e63946;
  --warning: #f39c12;

  --glass: rgba(17, 17, 17, 0.85);
  --glass-strong: rgba(17, 17, 17, 0.95);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.5);

  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 18px;
  --r-xl: 24px;
  --r-pill: 999px;

  --tab-h: 60px;
  --top-h: 52px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);

  --ease: cubic-bezier(0.32, 0.72, 0, 1);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
}

/* Светлая тема — тоже красно-чёрная, но мягче */
html[data-theme="light"] {
  color-scheme: light;

  --bg: #f5f5f5;
  --bg-elevated: #ffffff;
  --card: #ffffff;
  --card-2: #f0f0f0;
  --separator: rgba(0, 0, 0, 0.08);
  --separator-strong: rgba(0, 0, 0, 0.15);

  --text: #1a1a1a;
  --text-2: #666666;
  --text-3: #999999;

  --accent: #d62839;
  --accent-soft: rgba(214, 40, 57, 0.1);
  --accent-text: #ffffff;

  --success: #27ae60;
  --danger: #d62839;
  --warning: #e67e22;

  --glass: rgba(255, 255, 255, 0.85);
  --glass-strong: rgba(255, 255, 255, 0.95);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

body.sheet-open { overflow: hidden; }

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; padding: 0; }
a { color: var(--accent); text-decoration: none; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

.hidden { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ============================ Шапка ============================ */

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  padding-top: var(--safe-top);
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--separator);
}

.topbar-inner {
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--top-h);
  padding: 0 16px;
  max-width: 600px;
  margin: 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 17px;
  flex: 1;
  min-width: 0;
}

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--accent);
  display: grid;
  place-items: center;
  color: #fff;
  flex: none;
}

.brand-mark svg { width: 15px; height: 15px; }
.brand-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.topbar-title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: var(--r-pill);
  display: grid;
  place-items: center;
  color: var(--text);
  flex: none;
}

.icon-btn:active { opacity: 0.6; }
.icon-btn svg { width: 19px; height: 19px; }

.icon-btn .dot {
  position: absolute;
  top: 4px;
  right: 3px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  border: 2px solid var(--bg);
}

.avatar-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--r-pill);
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  display: grid;
  place-items: center;
  flex: none;
}

/* ============================ Контент ============================ */

.view {
  max-width: 600px;
  margin: 0 auto;
  padding: 12px 16px calc(var(--tab-h) + var(--safe-bottom) + 24px);
}

.view-header { padding: 8px 0 14px; }

.view-title {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.view-sub {
  margin: 4px 0 0;
  color: var(--text-2);
  font-size: 14px;
}

/* ============================ Карточки ============================ */

.card {
  background: var(--card);
  border-radius: var(--r-lg);
  overflow: hidden;
}

.card + .card { margin-top: 12px; }

.list-heading {
  margin: 20px 4px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Услуга */
.service-card {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 14px 16px;
  text-align: left;
  background: var(--card);
  position: relative;
}

.service-card + .service-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 58px;
  right: 0;
  height: 1px;
  background: var(--separator);
}

.service-card:active { background: var(--card-2); }

.service-icon {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--accent-soft);
  display: grid;
  place-items: center;
  font-size: 18px;
  flex: none;
}

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

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

.service-sub {
  color: var(--text-2);
  font-size: 13px;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Стрелка — фиксированный размер */
.chevron {
  width: 16px;
  height: 16px;
  flex: none;
  color: var(--text-3);
}

/* Дело */
.dialog-card {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 13px 16px;
  text-align: left;
  position: relative;
}

.dialog-card + .dialog-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 16px;
  right: 0;
  height: 1px;
  background: var(--separator);
}

.dialog-card:active { background: var(--card-2); }
.dialog-body { flex: 1; min-width: 0; }

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

.dialog-meta {
  color: var(--text-2);
  font-size: 12px;
  margin-top: 2px;
  display: flex;
  gap: 6px;
  overflow: hidden;
}

.dialog-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================ Пустые состояния ============================ */

.empty { text-align: center; padding: 40px 20px; }

.empty-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 14px;
  border-radius: 16px;
  background: var(--card);
  display: grid;
  place-items: center;
  color: var(--text-2);
}

.empty-icon svg { width: 24px; height: 24px; }
.empty h3 { margin: 0 0 6px; font-size: 17px; font-weight: 600; }
.empty p { margin: 0 auto; color: var(--text-2); font-size: 14px; max-width: 30ch; }

/* ============================ Кнопки ============================ */

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 20px;
  border-radius: var(--r-md);
  font-weight: 600;
  font-size: 15px;
  width: 100%;
}

.btn:active { opacity: 0.8; }
.btn:disabled { opacity: 0.4; }

.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-plain { background: var(--card); color: var(--text); }
.btn-ghost { background: var(--separator); color: var(--text); }
.btn-danger { background: var(--accent-soft); color: var(--danger); }
.btn-sm { min-height: 36px; padding: 0 14px; font-size: 14px; width: auto; border-radius: 10px; }
.btn svg { width: 17px; height: 17px; }

/* ============================ Формы ============================ */

.field {
  background: var(--card);
  border-radius: var(--r-md);
  padding: 12px 14px;
  border: 1px solid transparent;
}

.field:focus-within { border-color: var(--accent); }

.field textarea {
  width: 100%;
  border: none;
  background: none;
  resize: none;
  outline: none;
  font-size: 15px;
  line-height: 1.4;
  max-height: 180px;
  min-height: 22px;
}

.field input {
  width: 100%;
  border: none;
  background: none;
  outline: none;
  font-size: 15px;
}

/* ============================ Чат ============================ */

.chat-view {
  display: flex;
  flex-direction: column;
  min-height: calc(100dvh - var(--top-h));
  margin-bottom: calc(-1 * (var(--tab-h) + var(--safe-bottom) + 24px));
}

/* Выбор роли в чате */
.role-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  color: var(--text-2);
  font-size: 13px;
}

.messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px 0 16px;
}

.msg { display: flex; }
.msg-user { justify-content: flex-end; }
.msg-ai { justify-content: flex-start; }

.bubble {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.msg-user .bubble {
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 4px;
}

.msg-ai .bubble {
  background: var(--card);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.msg-file {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  opacity: 0.8;
  margin-bottom: 5px;
}

/* Кнопка скачивания заключения под ответом */
.download-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin: 6px 0 4px;
  padding: 8px 14px;
  border-radius: var(--r-pill);
  background: var(--card);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--separator);
}

.download-btn:active { opacity: 0.7; }
.download-btn svg { width: 15px; height: 15px; }

.typing { display: flex; gap: 4px; padding: 4px 2px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-3);
  animation: typing 1.3s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: 0.16s; }
.typing i:nth-child(3) { animation-delay: 0.32s; }

@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.composer {
  position: sticky;
  bottom: 0;
  padding: 8px 0 calc(var(--safe-bottom) + 76px);
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}

.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer .field { flex: 1; border-radius: var(--r-lg); }
.composer .field textarea { max-height: 120px; }

.send-btn {
  width: 42px;
  height: 42px;
  border-radius: var(--r-pill);
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  flex: none;
}

.send-btn:active { opacity: 0.8; }
.send-btn:disabled { opacity: 0.3; }
.send-btn svg { width: 18px; height: 18px; }

.composer-hint {
  text-align: center;
  color: var(--text-3);
  font-size: 11px;
  margin-top: 6px;
}

/* ============================ Карточка файла ============================ */

.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border-radius: var(--r-lg);
  padding: 10px 12px;
  margin-bottom: 8px;
  animation: file-in 0.3s var(--ease-out);
}

@keyframes file-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.file-card.removing { animation: file-out 0.2s var(--ease) forwards; }
@keyframes file-out { to { opacity: 0; transform: translateY(4px); } }

.file-icon {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--accent-soft);
  display: grid;
  place-items: center;
  color: var(--accent);
  flex: none;
}

.file-icon svg { width: 18px; height: 18px; }
.file-icon.done { background: rgba(46, 204, 113, 0.15); color: var(--success); }
.file-icon.error { background: var(--accent-soft); color: var(--danger); }

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

.file-name {
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-status {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-2);
  font-size: 12px;
  margin-top: 2px;
}

.file-status.ok { color: var(--success); }
.file-status.err { color: var(--danger); }

.file-progress {
  height: 3px;
  border-radius: 2px;
  background: var(--separator);
  overflow: hidden;
  margin-top: 6px;
}

.file-progress > i {
  display: block;
  height: 100%;
  width: 40%;
  border-radius: 2px;
  background: var(--accent);
  animation: file-scan 1.1s var(--ease) infinite;
}

@keyframes file-scan {
  0% { transform: translateX(-110%); }
  100% { transform: translateX(280%); }
}

.file-remove {
  width: 28px;
  height: 28px;
  border-radius: var(--r-pill);
  display: grid;
  place-items: center;
  color: var(--text-3);
  flex: none;
}

.file-remove:active { background: var(--separator); }
.file-remove svg { width: 14px; height: 14px; }

/* ============================ Нижняя панель ============================ */

.tabbar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-bottom) + 10px);
  width: min(calc(100% - 24px), 400px);
  height: var(--tab-h);
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 0 6px;
  border-radius: 26px;
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(24px);
  backdrop-filter: blur(24px);
  box-shadow: var(--shadow-md);
  border: 1px solid var(--separator);
  z-index: 50;
}

.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  height: 44px;
  border-radius: 18px;
  color: var(--text-2);
  position: relative;
}

.tab svg { width: 21px; height: 21px; }
.tab span { font-size: 10px; font-weight: 600; }
.tab.active { color: var(--accent); }

.tab.active::after {
  content: "";
  position: absolute;
  bottom: 2px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--accent);
}

/* ============================ Шторки ============================ */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s var(--ease);
  z-index: 60;
}

.sheet-backdrop.open { opacity: 1; visibility: visible; }

.sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translate(-50%, 102%);
  width: min(100%, 500px);
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg-elevated);
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.4);
  transition: transform 0.4s var(--ease);
  z-index: 61;
  visibility: hidden;
  padding-bottom: var(--safe-bottom);
}

.sheet.open { transform: translate(-50%, 0); visibility: visible; }

.sheet-grip {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--separator-strong);
  margin: 8px auto 4px;
  flex: none;
}

.sheet-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 18px 10px;
  flex: none;
}

.sheet-head h2 {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sheet-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 18px 20px;
}

/* ============================ Тосты ============================ */

.toasts {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--tab-h) + var(--safe-bottom) + 20px);
  z-index: 80;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: min(calc(100% - 32px), 360px);
  pointer-events: none;
}

.toast {
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  border: 1px solid var(--separator);
  color: var(--text);
  padding: 10px 16px;
  border-radius: var(--r-pill);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  text-align: center;
  animation: toast-in 0.3s var(--ease-out);
}

.toast.out { animation: toast-out 0.25s var(--ease) forwards; }
.toast.error { color: var(--danger); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out { to { opacity: 0; transform: translateY(6px); } }

/* ============================ Прочее ============================ */

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  position: relative;
}

.row + .row::before {
  content: "";
  position: absolute;
  top: 0; left: 16px; right: 0;
  height: 1px;
  background: var(--separator);
}

.row-label { flex: 1; }
.row-label b { display: block; font-weight: 600; font-size: 15px; }
.row-label small { color: var(--text-2); font-size: 12px; }
.row-value { color: var(--text-2); font-size: 14px; }

.badge {
  display: inline-flex;
  align-items: center;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: var(--r-pill);
}

.badge-ok { background: rgba(46, 204, 113, 0.15); color: var(--success); }
.badge-off { background: var(--separator); color: var(--text-2); }
.badge-warn { background: rgba(243, 156, 18, 0.15); color: var(--warning); }

.hero-card {
  background: linear-gradient(135deg, var(--accent), #b71c2c);
  color: #fff;
  border-radius: var(--r-lg);
  padding: 18px;
}

.hero-card h3 { margin: 0 0 4px; font-size: 19px; font-weight: 700; }
.hero-card p { margin: 0; opacity: 0.9; font-size: 13px; }

.hero-meta {
  display: flex;
  gap: 16px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.hero-meta div { font-size: 12px; opacity: 0.9; }
.hero-meta b { display: block; font-size: 16px; font-weight: 700; opacity: 1; }

.maintenance { text-align: center; padding: 40px 20px; max-width: 380px; margin: 0 auto; }
.maintenance .empty-icon { color: var(--warning); }
.maintenance h1 { font-size: 22px; font-weight: 700; margin: 0 0 8px; }
.maintenance p { color: var(--text-2); margin: 0 0 18px; font-size: 15px; }

.gate { max-width: 340px; width: 100%; text-align: center; }
.gate .brand-mark { width: 56px; height: 56px; border-radius: 16px; margin: 0 auto 16px; }
.gate .brand-mark svg { width: 28px; height: 28px; }
.gate h1 { font-size: 24px; font-weight: 700; margin: 0 0 8px; }
.gate p { color: var(--text-2); font-size: 14px; margin: 0 0 20px; }

.spinner {
  width: 22px; height: 22px;
  border: 2.5px solid var(--separator-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
  margin: 0 auto;
}

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

.loading-screen {
  display: grid;
  place-items: center;
  gap: 12px;
  min-height: 60dvh;
  color: var(--text-2);
  font-size: 14px;
}

.skeleton {
  background: linear-gradient(90deg, var(--separator) 25%, var(--card-2) 50%, var(--separator) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--r-sm);
}

@keyframes shimmer { to { background-position: -200% 0; } }

.sk-line { height: 14px; margin-bottom: 8px; }
.sk-card { height: 64px; border-radius: var(--r-lg); margin-bottom: 12px; }

.divider { height: 1px; background: var(--separator); margin: 14px 0; }
.muted { color: var(--text-2); font-size: 13px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
