:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --panel: #ffffff;
  --text: #1c2333;
  --muted: #5c647a;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --border: #e2e8f0;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  /* Слой с паттерном лап (body::before): только рисунок, фон страницы — у body */
  --paw-overlay-opacity: 0.14;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  position: relative;
}

/* Подложка «лапы» по странице (прозрачный слой только с паттерном — не гасит рисунок) */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-color: transparent;
  background-image: url("/paw-pattern.svg");
  background-repeat: repeat;
  background-size: 220px;
  opacity: var(--paw-overlay-opacity);
}

/* Админка: без паттерна лап — иначе на части браузеров он «просвечивает» в полях */
body.admin-app::before {
  display: none;
}

.layout {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}

.layout-chat {
  max-width: 1180px;
  position: relative;
  z-index: 1;
}

.header-chat {
  margin-bottom: 12px;
  padding: 14px 16px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

/* Шапка: бренд + филиал в ряд, вход админа — отдельной строкой по центру */
.header.header-chat {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "brand tenant"
    "admin admin";
  align-items: start;
  column-gap: 20px;
  row-gap: 12px;
  margin-bottom: 12px;
}

.header-chat-brand {
  grid-area: brand;
  min-width: 0;
}

.header.header-chat .tenant {
  grid-area: tenant;
  justify-self: end;
}

.header-chat-admin {
  grid-area: admin;
  justify-self: stretch;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.btn-header-logout {
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  font-size: 0.9rem;
  font-family: inherit;
}

.btn-header-logout:hover {
  background: var(--bg);
}

.admin-link-header {
  display: inline-block;
}

.main-chat {
  grid-template-columns: 1fr;
}

.chat-panel-full {
  max-width: 100%;
}

.catalog-status-line {
  margin: 0 0 14px;
  padding: 10px 14px;
  font-size: 0.85rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.04);
}

/* Слева — узкая история, справа — основной чат (форма + ответ) */
.chat-page {
  display: grid;
  grid-template-columns: minmax(200px, 260px) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

.chat-center {
  min-width: 0;
}

.history-sidebar {
  position: sticky;
  top: 16px;
  width: 100%;
  max-width: 260px;
  max-height: calc(100vh - 32px);
  overflow: hidden;
  padding: 14px 12px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-sidebar-scroll {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-sidebar-footer {
  flex-shrink: 0;
  margin-top: auto;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.history-title {
  margin: 0;
  flex-shrink: 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.btn-clear-history {
  font: inherit;
  font-size: 0.75rem;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid #fecaca;
  background: #fef2f2;
  color: #991b1b;
  cursor: pointer;
  width: 100%;
}

.btn-clear-history:hover {
  background: #fee2e2;
}

.history-empty {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.4;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-item {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  width: 100%;
  margin: 0;
  padding: 10px 10px;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.history-item:hover {
  background: #eff6ff;
  border-color: #bfdbfe;
}

.history-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.history-item.history-item-active {
  background: #eff6ff;
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(37, 99, 235, 0.2);
}

.history-item-time {
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.history-item-text {
  font-size: 0.82rem;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.history-pagination {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding-top: 4px;
  border-top: 1px solid var(--border);
  margin-top: 4px;
}

.history-page-info {
  font-size: 0.75rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 5.5rem;
  text-align: center;
}

.btn-history-page {
  font: inherit;
  font-size: 0.75rem;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #f8fafc;
  color: var(--text);
  cursor: pointer;
}

.btn-history-page:hover:not(:disabled) {
  background: #eff6ff;
  border-color: #bfdbfe;
}

.btn-history-page:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

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

h1 {
  margin: 0 0 6px;
  font-size: 1.75rem;
}

h2 {
  margin: 0 0 12px;
  font-size: 1.1rem;
}

.subtitle {
  margin: 0;
  color: var(--muted);
}

.chat-panel-full .chat-title {
  margin-top: 18px;
  margin-bottom: 10px;
  padding: 2px 4px;
}

.input-label-chat {
  display: block;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}

/* Прозрачная оболочка панели: лапы видны между блоками; ввод/вывод — непрозрачные */
.chat-panel-full.panel {
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
  min-height: 0;
}

.chat-panel-full .chat-form {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.06);
}

.chat-view-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 14px 0 0;
  padding: 10px 14px;
  font-size: 0.85rem;
  color: var(--muted);
  background: rgba(255, 251, 235, 0.95);
  border: 1px solid #fde68a;
  border-radius: 12px;
}

.chat-view-banner #btn-chat-current {
  flex-shrink: 0;
}

.chat-title {
  margin: 0;
  font-size: 1.1rem;
}

.admin-link {
  font-size: 0.9rem;
  color: var(--accent);
  text-decoration: none;
  align-self: center;
}

.admin-link:hover {
  text-decoration: underline;
}

.admin-panel {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  background: #fafbfc;
}

.admin-panel summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
}

.admin-panel[open] summary {
  margin-bottom: 10px;
}

.key-line {
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.key-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.key-form label {
  font-size: 0.85rem;
  color: var(--muted);
}

.key-form input[type="password"] {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background-color: #ffffff;
  font-family: ui-monospace, monospace;
  font-size: 0.9rem;
}

.key-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.btn-secondary {
  background: #64748b;
}

.btn-secondary:hover {
  background: #475569;
}

.tenant {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 200px;
}

.tenant label {
  font-size: 0.85rem;
  color: var(--muted);
}

select {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background-color: #ffffff;
  font-size: 1rem;
}

/* Поля ввода: непрозрачный фон, чтобы подложка с лапами не просвечивала */
input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="hidden"]) {
  background-color: #ffffff;
}

.main {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  gap: 18px;
}

.main.main-chat {
  grid-template-columns: 1fr;
}

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

  .header.header-chat {
    grid-template-columns: 1fr;
    grid-template-areas:
      "brand"
      "tenant"
      "admin";
  }

  .header.header-chat .tenant {
    justify-self: stretch;
  }

  .chat-page {
    grid-template-columns: 1fr;
  }

  .history-sidebar {
    position: static;
    max-width: none;
    max-height: 240px;
    order: -1;
  }
}

.panel {
  background-color: #ffffff;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
  box-shadow: var(--shadow);
}

.messages {
  min-height: 200px;
  max-height: clamp(200px, calc(100vh - 400px), 420px);
  overflow-y: auto;
  padding: 12px;
  border-radius: 12px;
  background: #f8fafc;
  border: 1px solid var(--border);
  white-space: pre-wrap;
  flex-shrink: 1;
}

.messages-response {
  margin-bottom: 12px;
}

.msg {
  padding: 10px 12px;
  border-radius: 12px;
  margin-bottom: 10px;
  border: 1px solid transparent;
}

.msg.user {
  background: #e8efff;
  border-color: #c7d7ff;
}

.msg.bot {
  background: #ffffff;
  border-color: var(--border);
}

.msg-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.msg-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 10px;
  margin-bottom: 6px;
}

.msg-time {
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.msg-meta-inline {
  font-size: 0.72rem;
  color: var(--muted);
}

.msg-header .msg-time ~ .msg-meta-inline::before {
  content: "·";
  margin-right: 6px;
  color: #cbd5e1;
}

.msg-body {
  white-space: pre-wrap;
}

@keyframes msgHighlight {
  0% {
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.45);
  }
  100% {
    box-shadow: 0 0 0 0 transparent;
  }
}

.msg.msg-highlight {
  animation: msgHighlight 1.1s ease-out;
}

.chat-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

textarea {
  width: 100%;
  resize: vertical;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background-color: #ffffff;
  font-family: inherit;
  font-size: 1rem;
}

button {
  align-self: flex-start;
  padding: 10px 18px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

button:hover {
  background: var(--accent-dark);
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.side {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.hint {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 0.9rem;
}

.status {
  margin-top: 10px;
  font-size: 0.9rem;
  color: var(--text);
}

pre.status {
  background-color: #f8fafc;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  overflow-x: auto;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
}

table.logs {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

table.logs th,
table.logs td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

table.logs th {
  background: #f1f5f9;
  font-weight: 600;
}

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

.error {
  color: #b91c1c;
}

.ok {
  color: #15803d;
}
