/* Тёплая «бумажная» тема. Все пары цветов проверены на WCAG AA
   (текст ≥ 4.5:1, UI ≥ 3:1), расчёт в истории проекта. */
:root {
  --page-bg: #F6F1E7;
  --surface-bg: #FFFDF8;
  --ink: #26221B;
  --text-muted: #6B6357;
  --accent: #6E4A32;
  --border-strong: #8A8070;   /* функциональные границы: ≥3:1 */
  --border-soft: #D2C5A8;     /* декоративные разделители */
  --rule: #C9BFAE;            /* линия у реплик ассистента, декор */
  --focus-ring: #8A3B2A;
  --danger-rec: #B3261E;
  --pill-bg: #EFE8DA;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  /* градиент только СВЕТЛЕЕ базы: муть-текст на #F6F1E7 = 4.60:1, запаса нет,
     темнить нельзя (проверено contrast-master) */
  background: radial-gradient(125% 120% at 50% -10%, #FCF8F0 0%, #F6F1E7 58%);
  background-attachment: fixed;
  color: var(--ink);
  font: 16px/1.55 Georgia, "Iowan Old Style", "Times New Roman", serif;
  -webkit-font-smoothing: antialiased;
}

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--surface-bg);
  color: var(--accent);
  padding: 10px 16px;
  border-radius: 0 0 10px 0;
  z-index: 10;
}
.skip-link:focus-visible { left: 0; }

.app {
  max-width: 1020px;
  margin: 0 auto;
  padding: 32px 20px 44px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 290px;
  gap: 24px;
}

.app-header { grid-column: 1 / -1; }

.app > main,
.app > .panel { min-width: 0; }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}
.brand-mark { flex: none; }

h1 {
  margin: 0;
  font-size: 1.8rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}
h1::after {
  content: ".";
  color: var(--accent);
}

.tagline {
  margin: 0;
  color: var(--text-muted);
  max-width: 60ch;
}

/* ---- Чат ---- */

.chat {
  background: var(--surface-bg);
  border: 1px solid var(--border-soft);
  border-radius: 16px;
  /* многослойная мягкая тень для глубины (декор; границу держит border) */
  box-shadow: 0 1px 2px rgba(38, 34, 27, 0.05), 0 12px 32px -12px rgba(38, 34, 27, 0.14);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-log {
  height: 56vh;
  height: 56dvh;
  min-height: 320px;
  overflow-y: auto;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(8px) scale(0.99); }
  to { opacity: 1; transform: none; }
}

/* Плавное появление блоков при загрузке (только для тех, кто не просил
   меньше движения; reduced-motion видит всё сразу на полной непрозрачности). */
@keyframes reveal {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: no-preference) {
  .app-header { animation: reveal 500ms cubic-bezier(0.22, 1, 0.36, 1) both; }
  .chat { animation: reveal 560ms cubic-bezier(0.22, 1, 0.36, 1) 80ms both; }
  .panel { animation: reveal 560ms cubic-bezier(0.22, 1, 0.36, 1) 160ms both; }
}

.message {
  display: flex;
  animation: msg-in 260ms cubic-bezier(0.22, 1, 0.36, 1);
}

/* Индикатор «печатает»: три точки, декоративные (aria-hidden), в линию
   ответа ассистента. */
.typing-dots {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 0 6px 14px;
  border-left: 3px solid var(--rule);
  animation: msg-in 200ms cubic-bezier(0.22, 1, 0.36, 1);
}
.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typing-bounce 1.2s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.18s; }
.typing-dots span:nth-child(3) { animation-delay: 0.36s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-5px); opacity: 1; }
}

.message p {
  margin: 0;
  max-width: 88%;
  white-space: pre-line;
  overflow-wrap: anywhere;
}

/* Ассистент: без пузыря, спокойный текст с тонкой линией слева */
.message--assistant p {
  color: var(--ink);
  border-left: 3px solid var(--rule);
  padding: 2px 0 2px 14px;
}

.message--assistant {
  flex-direction: column;
  align-items: flex-start;
}

.message--user { justify-content: flex-end; }

.message--user p {
  background: var(--ink);
  color: var(--surface-bg);
  padding: 10px 14px;
  border-radius: 12px;
  border-bottom-right-radius: 4px;
  box-shadow: 0 1px 4px rgba(38, 34, 27, 0.25);
}

/* Ответы ИИ: абзацы с той же линией */
.summary-body {
  border-left: 3px solid var(--rule);
  padding: 2px 0 2px 14px;
  max-width: 88%;
}
.summary-body p {
  margin: 0;
  white-space: pre-line;
  overflow-wrap: anywhere;
  max-width: none;
}
.summary-body p + p { margin-top: 10px; }

/* ---- Голосовое сообщение ---- */

.voice-message {
  background: var(--ink);
  color: var(--surface-bg);
  border-radius: 12px;
  border-bottom-right-radius: 4px;
  padding: 10px 14px;
  max-width: 88%;
  box-shadow: 0 1px 4px rgba(38, 34, 27, 0.25);
}

.voice-player {
  display: flex;
  align-items: center;
  gap: 10px;
}

.voice-msg__play {
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--surface-bg);
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.voice-msg__play svg { fill: currentColor; }
.voice-msg__play:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 3px;
  box-shadow: 0 0 0 3px #FFFFFF;
}

.voice-waveform { display: block; }

.voice-duration {
  font-size: 0.85rem;
  color: var(--surface-bg);
}

.voice-transcript {
  margin: 8px 0 0;
  padding-top: 8px;
  border-top: 1px solid rgba(255, 253, 248, 0.35);
  font-size: 0.92rem;
  color: var(--surface-bg);
  white-space: pre-line;
  overflow-wrap: anywhere;
}

/* ---- Панель записи голосового ---- */

.recording-panel {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 4px 4px 4px 12px;
  background: var(--surface-bg);
}
.recording-panel[hidden] { display: none; }

@keyframes rec-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.82); }
}

.rec-dot {
  flex: none;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger-rec);
  animation: rec-pulse 1.2s ease-in-out infinite;
}

.rec-timer {
  flex: none;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--danger-rec);
}

.rec-waveform {
  flex: 1;
  min-width: 0;
  height: 36px;
}

/* ---- Меню ---- */

.quick-actions {
  display: flex;
  justify-content: flex-end;
  padding: 10px 20px 4px;
  border-top: 1px solid var(--border-soft);
}

.menu-wrap { position: relative; }

.menu-popover {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  z-index: 5;
  min-width: 240px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px;
  background: var(--surface-bg);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(38, 34, 27, 0.18);
  transform-origin: bottom right;
  animation: pop-in 160ms cubic-bezier(0.22, 1, 0.36, 1);
}
.menu-popover[hidden] { display: none; }
@keyframes pop-in {
  from { opacity: 0; transform: scale(0.94) translateY(6px); }
  to { opacity: 1; transform: none; }
}
.mic-btn { transition: background-color 150ms, transform 120ms ease; }
.mic-btn:hover { background: var(--pill-bg); }
.mic-btn:active { transform: scale(0.94); }
.menu-item { transition: background-color 120ms ease; }

.menu-item {
  min-height: 44px;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover { background: var(--pill-bg); }

.menu-item--confirm {
  padding: 10px 12px;
  font-size: 0.92rem;
}
.menu-item--confirm[hidden] { display: none; }

.confirm-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.btn-danger {
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--danger-rec);
  border-radius: 10px;
  background: var(--surface-bg);
  color: var(--danger-rec);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn-danger:hover { background: #F8ECE9; }

/* ---- Композер ---- */

.chip {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface-bg);
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  transition: background-color 150ms, transform 120ms ease, box-shadow 150ms ease;
}
.chip:hover { background: var(--pill-bg); transform: translateY(-1px); box-shadow: 0 4px 12px -6px rgba(38, 34, 27, 0.3); }
.chip:active { transform: translateY(0) scale(0.97); }

.composer {
  display: flex;
  gap: 8px;
  padding: 14px 20px 20px;
}

#message-input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 0 14px;
  font: inherit;
  color: var(--ink);
  background: var(--surface-bg);
  transition: box-shadow 160ms ease, border-color 160ms ease;
}
#message-input::placeholder { color: var(--text-muted); }
/* добавочный мягкий glow ПОВЕРХ outline (кольцо начинается за 5px, чтобы не
   перекрыть основную обводку :focus-visible - она остаётся индикатором) */
#message-input:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 5px rgba(138, 59, 42, 0.10), 0 0 12px 4px rgba(138, 59, 42, 0.12);
}

.mic-btn {
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--surface-bg);
  color: var(--ink);
  cursor: pointer;
}
.mic-btn[hidden] { display: none; }
.mic-btn[data-error="true"] { border-color: var(--danger-rec); }

.mic-error {
  margin: 0;
  padding: 0 20px 16px;
  font-size: 0.92rem;
  color: var(--danger-rec);
}

.btn-send {
  min-height: 44px;
  padding: 0 20px;
  border: none;
  border-radius: 10px;
  background: var(--ink);
  color: var(--surface-bg);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 150ms;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn-send { transition: background-color 150ms, transform 120ms ease, box-shadow 150ms ease; }
.btn-send:hover { background: #3A3428; transform: translateY(-1px); box-shadow: 0 6px 16px -8px rgba(38, 34, 27, 0.5); }
.btn-send:active { transform: translateY(0) scale(0.96); }
/* кольцо фокуса на тёмной заливке: белая прокладка */
.btn-send:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  box-shadow: 0 0 0 3px #FFFFFF;
}
.send-icon { display: none; }

.btn-ghost {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--surface-bg);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

/* ---- Память (aside) ---- */

.panel {
  background: var(--surface-bg);
  border: 1px solid var(--border-soft);
  border-radius: 16px;
  box-shadow: 0 1px 2px rgba(38, 34, 27, 0.05), 0 12px 32px -14px rgba(38, 34, 27, 0.12);
  padding: 20px;
  align-self: start;
}

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

.meter-track {
  height: 10px;
  background: var(--pill-bg);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #8A5C3E);
  transition: width 600ms cubic-bezier(0.22, 1, 0.36, 1);
}

.status-line {
  margin: 12px 0 4px;
  font-size: 0.92rem;
}

.counters-line {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* Мини-шкала «опора на память» в ответах ИИ */
.memory-reliance {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0 0 17px; /* в линию с текстом ассистента (линия 3px + отступ 14px) */
  font-size: 0.8rem;
  color: var(--text-muted);
}

.memory-reliance .bar {
  display: inline-block;
  width: 90px;
  height: 6px;
  background: var(--pill-bg);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  overflow: hidden;
}

.memory-reliance .bar-fill {
  display: block;
  height: 100%;
  background: var(--accent);
}

/* ---- Экран входа ---- */

.login {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login[hidden] { display: none; }

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface-bg);
  border: 1px solid var(--border-soft);
  border-radius: 18px;
  box-shadow: 0 1px 2px rgba(38, 34, 27, 0.05), 0 20px 50px -20px rgba(38, 34, 27, 0.28);
  padding: 34px 28px 30px;
  text-align: center;
  animation: reveal 480ms cubic-bezier(0.22, 1, 0.36, 1) both;
}
.login-mark { margin-bottom: 6px; }
.login-card h1 { font-size: 1.5rem; margin: 0; }
.login-card h1::after { content: "."; color: var(--accent); }
.login-sub { margin: 6px 0 22px; color: var(--text-muted); }
.login-form { text-align: left; }
.login-submit { width: 100%; margin-top: 6px; }

.pw-wrap { position: relative; display: flex; }
.pw-wrap input { flex: 1; padding-right: 46px; }
.pw-toggle {
  position: absolute;
  right: 4px; top: 50%;
  transform: translateY(-50%);
  width: 38px; height: 38px;
  display: inline-flex; align-items: center; justify-content: center;
  border: none; border-radius: 8px;
  background: transparent; color: var(--text-muted); cursor: pointer;
}
.pw-toggle:hover { color: var(--ink); background: var(--pill-bg); }
.pw-toggle[aria-pressed="true"] { color: var(--accent); }

/* ---- Шапка: строка с брендом и шестерёнкой ---- */

.app-header__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
}
.app-header__top .brand { margin-bottom: 0; }

.icon-btn {
  min-width: 44px; min-height: 44px;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface-bg);
  color: var(--accent);
  cursor: pointer;
  transition: background-color 150ms, transform 120ms ease;
}
.icon-btn:hover { background: var(--pill-bg); }
.icon-btn:active { transform: scale(0.94); }

/* ---- Модалка настроек ---- */

.settings-modal {
  padding: 0;
  border: none;
  border-radius: 18px;
  max-width: 460px;
  width: calc(100% - 32px);
  background: var(--surface-bg);
  color: var(--ink);
  box-shadow: 0 24px 60px -20px rgba(38, 34, 27, 0.5);
}
.settings-modal::backdrop { background: rgba(38, 34, 27, 0.45); }
.settings-modal[open] { animation: pop-in 180ms cubic-bezier(0.22, 1, 0.36, 1); }
.settings-modal__panel { padding: 22px; max-height: 86vh; overflow-y: auto; }
.settings-modal__head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 18px;
}
.settings-modal__head h2 { margin: 0; font-size: 1.25rem; }
.settings-modal__foot { margin-top: 18px; }

/* ---- Поля форм ---- */

.field { margin-bottom: 16px; }
.field > label { display: block; margin-bottom: 6px; font-weight: 600; }
.field input[type="text"],
.field input[type="password"],
.field select {
  width: 100%; min-height: 44px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 0 14px;
  font: inherit; color: var(--ink); background: var(--surface-bg);
  transition: box-shadow 160ms ease, border-color 160ms ease;
}
.field select:disabled { color: var(--text-muted); background: var(--pill-bg); cursor: not-allowed; }
.field input:focus-visible,
.field select:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 5px rgba(138, 59, 42, 0.10), 0 0 12px 4px rgba(138, 59, 42, 0.12);
}
.field input[aria-invalid="true"] { border-color: var(--danger-rec); }
.field-hint { margin: 6px 0 0; font-size: 0.9rem; color: var(--text-muted); }
.field-error { margin: 6px 0 0; font-size: 0.92rem; color: var(--danger-rec); }
.field-error[hidden] { display: none; }
.field-status { margin: 10px 0 0; font-size: 0.92rem; color: var(--accent); }
.field-group { border: 1px solid var(--border-soft); border-radius: 12px; padding: 14px 16px 4px; margin: 0 0 16px; }
.field-group legend { padding: 0 6px; font-weight: 600; }

/* switch-переключатель на базе нативного checkbox */
.field--switch { display: flex; align-items: center; gap: 12px; }
.field--switch > label { margin-bottom: 0; }
.switch-input {
  appearance: none; -webkit-appearance: none;
  width: 44px; height: 26px; min-width: 44px;
  border: 1px solid var(--border-strong); border-radius: 999px;
  background: var(--pill-bg); position: relative; cursor: pointer; flex: none;
  transition: background-color 150ms;
}
.switch-input::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--surface-bg); border: 1px solid var(--border-strong);
  transition: transform 150ms;
}
.switch-input:checked { background: var(--accent); }
.switch-input:checked::after { transform: translateX(18px); }
.switch-input:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 2px; }

/* ---- Мобильная адаптация ---- */

@media (max-width: 880px) {
  .app { grid-template-columns: 1fr; }
  .chat-log {
    height: 50vh;
    height: 50dvh;
  }
}

@media (max-width: 560px) {
  .app {
    padding: 16px 12px 24px;
    gap: 16px;
  }
  h1 { font-size: 1.45rem; }
  .brand-mark { width: 26px; height: 26px; }
  .tagline { font-size: 0.95rem; }
  .chat-log {
    height: 48vh;
    height: 48dvh;
    min-height: 260px;
    padding: 14px;
  }
  .quick-actions { padding: 8px 14px 4px; }
  .composer { padding: 10px 14px 16px; }
  .btn-send {
    width: 44px;
    min-width: 44px;
    padding: 0;
    border-radius: 50%;
  }
  .send-text { display: none; }
  .send-icon { display: block; }
  .recording-panel .btn-send {
    width: auto;
    padding: 0 16px;
    border-radius: 10px;
  }
  .menu-popover {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: 96px;
    min-width: 0;
  }
  .panel { padding: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .rec-dot { animation: none; opacity: 1; }
  .typing-dots span { animation: none; opacity: 0.7; } /* чистый покой, как у .rec-dot */
}
