/* ==========================================================================
   Читалка — панель «Спросить книгу»: ИИ-собеседник только по тексту книги.
   Широкий экран — колонка справа (взаимоисключающая с «Заметками»),
   ≤1279px — выдвижная справа, ≤759px — нижний лист 85vh.
   ========================================================================== */

:root {
  --chat-w: 368px;
}

.chat {
  display: flex;
  outline: none;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  grid-column: 3;
  grid-row: 1;
  overflow: hidden;
  background: var(--paper);
  border-left: 1px solid var(--rule);
}

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

@media (min-width: 1280px) {
  .chat { display: none; }
  .app--chat-open .chat { display: flex; }
  .app--chat-open .main { grid-template-columns: var(--rail-w) minmax(0, 1fr) var(--chat-w); }
  .app--chat-open.app--rail-collapsed .main {
    grid-template-columns: 0 minmax(0, 1fr) var(--chat-w);
  }
}

/* ------------------------------------------------------------- шапка ------ */
.chat__grab { display: none; }

.chat__head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  min-height: 52px;
  padding: 9px 6px 8px 16px;
  border-bottom: 1px solid var(--rule);
}
.chat__headtext {
  flex: 1 1 auto;
  min-width: 0;
}
.chat__title {
  font-family: var(--font-read);
  font-size: 15.5px;
  font-weight: 600;
  line-height: 1.25;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat__sub {
  margin: 2px 0 0;
  font-size: 11.5px;
  line-height: 1.3;
  color: var(--ink-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat__headacts {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 0 0 auto;
}
.chat__new {
  min-height: 32px;
  padding: 5px 2px;
  border: 0;
  border-bottom: 1px solid transparent;
  background: none;
  color: var(--ink-2);
  font-size: 12px;
  font-weight: 500;
  line-height: 1.2;
  white-space: nowrap;
  cursor: pointer;
}
.chat__new:hover { color: var(--accent); border-bottom-color: var(--accent); }
.chat__head .mini { width: 36px; height: 36px; flex: 0 0 auto; }

/* --------------------------------------------------------- дисклеймер ---- */
.chat__disclaimer {
  flex: 0 0 auto;
  margin: 0;
  padding: 9px 16px 10px;
  border-bottom: 1px solid var(--rule);
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--ink-2);
}

/* ------------------------------------------------------------ подсказка -- */
.chat__tip {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 10px 12px 0;
  padding: 9px 8px 9px 12px;
  border-left: 2px solid var(--accent);
  border-radius: 0 4px 4px 0;
  background: var(--accent-2);
  font-size: 12px;
  line-height: 1.45;
  color: var(--ink);
}
.chat__tip span { min-width: 0; }
.chat__tip-x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  margin: -2px 0 -2px auto;
  padding: 0;
  border: 0;
  border-radius: 3px;
  background: none;
  color: var(--ink-2);
  cursor: pointer;
}
.chat__tip-x:hover { background: var(--paper-2); color: var(--ink); }
.chat__tip-x .ic { width: 13px; height: 13px; }

/* ---------------------------------------------------------------- лента -- */
.chat__log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--paper-3) transparent;
}
.chat__log::-webkit-scrollbar { width: 10px; }
.chat__log::-webkit-scrollbar-thumb {
  background: var(--paper-3);
  border-radius: 6px;
  border: 3px solid transparent;
  background-clip: padding-box;
}
.chat__log::-webkit-scrollbar-track { background: transparent; }

.chat__empty {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding-top: 2px;
}
.chat__hello {
  margin: 0;
  font-family: var(--font-read);
  font-size: 15.5px;
  line-height: 1.55;
  color: var(--ink);
}
.chat__sugs {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.chat__sug {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: transparent;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 12.5px;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
  transition: background-color .12s ease, color .12s ease, border-color .12s ease;
}
.chat__sug:hover {
  background: var(--paper-2);
  border-color: var(--paper-3);
  color: var(--ink);
}

/* ------------------------------------------------------------ сообщения -- */
.cmsg { display: flex; flex-direction: column; min-width: 0; }

.cmsg--user {
  align-self: flex-end;
  max-width: 88%;
}
.cmsg__question {
  margin: 0;
  padding: 8px 11px;
  border-radius: 6px;
  background: var(--paper-2);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 13.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.cmsg--assistant { align-self: stretch; }
.cmsg__answer {
  font-family: var(--font-read);
  font-size: 16px;
  line-height: 1.62;
  color: var(--ink);
  overflow-wrap: anywhere;
}
.cmsg__answer p { margin: 0 0 .72em; }
.cmsg__answer p:last-child { margin-bottom: 0; }
.cmsg__answer ul,
.cmsg__answer ol {
  margin: 0 0 .72em;
  padding-left: 1.25em;
}
.cmsg__answer ul { list-style: disc; }
.cmsg__answer ol { list-style: decimal; }
.cmsg__answer li { margin: 0 0 .32em; }
.cmsg__answer li:last-child { margin-bottom: 0; }
.cmsg__answer strong { font-weight: 600; }
.cmsg__answer--none { color: var(--ink-2); }
.cmsg__verdict {
  margin: 0 0 .6em;
  font-family: var(--font-read);
  font-size: 16px;
  line-height: 1.62;
  color: var(--ink);
}

/* -------------------------------------------------------------- цитаты -- */
.cmsg__quotes {
  margin-top: 14px;
  padding-top: 11px;
  border-top: 1px solid var(--rule);
}
.cmsg__quotes .cmsg__label { margin-bottom: 8px; }
.cmsg__quote {
  margin: 0;
  padding: 1px 0 1px 12px;
  border-left: 2px solid var(--rule);
  font-family: var(--font-read);
  font-style: italic;
  font-size: 14px;
  line-height: 1.55;
  color: var(--ink-2);
}
.cmsg__quote-text {
  margin: 0;
  overflow-wrap: anywhere;
}
.cmsg__quote cite {
  display: block;
  margin-top: 5px;
  font-family: var(--font-ui);
  font-style: normal;
  font-size: 11px;
  line-height: 1.35;
  color: var(--ink-3);
}

/* вторая цитата — свёрнута, первая всегда видна */
.cmsg__quote-more { margin-top: 9px; }
.cmsg__quote-more > summary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 26px;
  padding: 2px 0;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 11.5px;
  font-weight: 500;
  line-height: 1.2;
  list-style: none;
  cursor: pointer;
}
.cmsg__quote-more > summary::-webkit-details-marker { display: none; }
.cmsg__quote-more > summary::before {
  content: "";
  width: 0;
  height: 0;
  border-top: 4px solid currentColor;
  border-left: 3.5px solid transparent;
  border-right: 3.5px solid transparent;
  transform: rotate(-90deg);
  transition: transform .12s ease;
}
.cmsg__quote-more[open] > summary::before { transform: none; }
.cmsg__quote-more > summary:hover { color: var(--accent); }
.cmsg__quote-more .cmsg__quote { margin-top: 2px; }

/* ------------------------------------------------------------ источники -- */
.cmsg__sources {
  margin-top: 14px;
  padding-top: 11px;
  border-top: 1px solid var(--rule);
}
.cmsg__label {
  margin: 0 0 7px;
  font-family: var(--font-ui);
  font-size: 11.5px;
  font-weight: 500;
  letter-spacing: .01em;
  color: var(--ink-3);
}
.cmsg__chips,
.cmsg__related {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}
.cmsg__related { margin-top: 10px; }
.cmsg__related[hidden] { display: none; }

.srcchip {
  display: inline-flex;
  flex-wrap: nowrap;
  align-items: baseline;
  gap: 5px;
  max-width: 100%;
  min-height: 28px;
  padding: 4px 10px;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: transparent;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 11.5px;
  font-weight: 500;
  line-height: 1.25;
  text-align: left;
  cursor: pointer;
  transition: background-color .12s ease, color .12s ease, border-color .12s ease;
}
.srcchip:hover,
.srcchip:focus-visible {
  background: var(--accent-2);
  border-color: transparent;
  color: var(--accent);
}
.srcchip__ch { flex: 0 0 auto; color: inherit; }
.srcchip__sep { flex: 0 0 auto; color: var(--ink-3); }
.srcchip__title {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.srcchip__p { flex: 0 0 auto; color: inherit; }

/* --------------------------------------------------------- кнопки ответа -- */
.cmsg__acts {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 16px;
  margin-top: 12px;
}
.cmsg__act {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  padding: 5px 1px;
  border: 0;
  border-bottom: 1px solid transparent;
  background: none;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 11.5px;
  font-weight: 500;
  line-height: 1.2;
  cursor: pointer;
}
.cmsg__act:hover { color: var(--accent); border-bottom-color: var(--accent); }
.cmsg__act .ic,
.cmsg__act svg { width: 14px; height: 14px; flex: 0 0 auto; }

/* ------------------------------------------------------ followups / демо -- */
.cmsg__followups {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  margin-top: 12px;
}
.fuchip {
  display: inline-flex;
  max-width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: transparent;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 12px;
  line-height: 1.35;
  text-align: left;
  cursor: pointer;
  transition: background-color .12s ease, color .12s ease, border-color .12s ease;
}
.fuchip:hover {
  background: var(--accent-2);
  border-color: transparent;
  color: var(--accent);
}
.cmsg__demo {
  margin: 10px 0 0;
  font-family: var(--font-ui);
  font-size: 11px;
  font-style: italic;
  line-height: 1.4;
  color: var(--ink-3);
}

/* ------------------------------------------------- ожидание и ошибки ----- */
.cmsg__thinking {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-family: var(--font-ui);
  font-size: 12.5px;
  line-height: 1.4;
  color: var(--ink-2);
}
.cmsg__dots { display: inline-flex; gap: 3px; flex: 0 0 auto; }
.cmsg__dots i {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--ink-3);
  animation: chatDot 1.15s ease-in-out infinite;
}
.cmsg__dots i:nth-child(2) { animation-delay: .15s; }
.cmsg__dots i:nth-child(3) { animation-delay: .3s; }
@keyframes chatDot {
  0%, 100% { opacity: .35; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(-1px); }
}

.cmsg--error .cmsg__errtext {
  margin: 0;
  padding: 8px 0 8px 12px;
  border-left: 2px solid var(--danger);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 12.5px;
  line-height: 1.5;
}
.chat__log[aria-busy="true"] .cmsg--pending { opacity: .95; }

/* ---------------------------------------------------------------- ввод --- */
.chat__compose {
  flex: 0 0 auto;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--rule);
  background: var(--paper);
}
.chat__input {
  display: block;
  width: 100%;
  min-height: 46px;
  max-height: 128px;
  padding: 9px 10px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: var(--paper-2);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 13.5px;
  line-height: 1.45;
  resize: none;
  outline: none;
  overflow-y: auto;
}
.chat__input:focus { border-color: var(--accent); }
.chat__input::placeholder { color: var(--ink-2); }
.chat__input[readonly] { opacity: .7; }
.chat__foot {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
}
.chat__counter {
  flex: 0 0 auto;
  font-family: var(--font-ui);
  font-size: 11px;
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}
.chat__counter.is-warn { color: var(--danger); }
.chat__keyhint {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-ui);
  font-size: 10.5px;
  color: var(--ink-3);
  text-align: right;
}
.chat__send {
  flex: 0 0 auto;
  min-height: 34px;
  padding: 0 14px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  background: var(--accent);
  color: #FFF;
  font-family: var(--font-ui);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color .12s ease, opacity .12s ease;
}
:root[data-theme="night"] .chat__send { color: #10201A; }
.chat__send:hover:not(:disabled) { filter: brightness(1.06); }
.chat__send:disabled { opacity: .45; cursor: default; }

/* ----------------------------------------------------------- планшет ----- */
@media (max-width: 1279px) {
  .chat {
    position: fixed;
    top: calc(var(--topbar-h) + 2px);
    right: 0;
    bottom: 0;
    z-index: 60;
    width: min(400px, 92vw);
    transform: translateX(102%);
    transition: transform .22s cubic-bezier(.2, .7, .3, 1);
    border-left: 1px solid var(--rule);
    box-shadow: var(--shadow-sheet);
    grid-column: auto;
    grid-row: auto;
  }
  .chat.is-open { transform: none; }
}

/* ----------------------------------------------------------- телефон ----- */
@media (max-width: 759px) {
  .chat {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 85vh;
    max-height: 85vh;
    border-left: 0;
    border-top: 1px solid var(--rule);
    border-radius: 12px 12px 0 0;
    transform: translateY(102%);
    transition: transform .24s cubic-bezier(.2, .7, .3, 1);
    box-shadow: 0 -12px 44px rgba(15, 22, 18, .22);
  }
  .chat.is-open { transform: none; }
  .chat__grab {
    display: block;
    flex: 0 0 auto;
    width: 38px;
    height: 4px;
    margin: 8px auto 0;
    border-radius: 2px;
    background: var(--rule);
  }
  .chat__head {
    min-height: 48px;
    padding: 6px 4px 6px 14px;
  }
  .chat__title { font-size: 15px; }
  .chat__head .mini { width: 44px; height: 44px; }
  .chat__new { min-height: 40px; padding: 8px 2px; font-size: 12.5px; }
  .chat__disclaimer { padding: 8px 14px 9px; }
  .chat__tip { margin: 10px 12px 0; }
  .chat__log { padding: 14px 14px 16px; }
  .chat__empty { gap: 12px; }
  .chat__sug {
    min-height: 44px;
    padding: 10px 12px;
    font-size: 13px;
  }
  .cmsg__answer,
  .cmsg__verdict { font-size: 16.5px; }
  .cmsg__quote { font-size: 14.5px; }
  .cmsg__act {
    min-height: 40px;
    padding: 8px 1px;
    font-size: 12px;
  }
  .srcchip,
  .cmsg__related .srcchip {
    min-height: 40px;
    padding: 9px 12px;
    font-size: 12px;
  }
  .fuchip {
    min-height: 40px;
    padding: 9px 12px;
    font-size: 12.5px;
  }
  .chat__input {
    font-size: 16px;
    min-height: 52px;
  }
  .chat__send { min-height: 40px; padding: 0 16px; font-size: 13px; }
  .chat__counter { font-size: 11.5px; }
}

@media (max-width: 380px) {
  .chat__keyhint { display: none; }
  .chat__counter { margin-right: auto; }
  .chat__head { padding-left: 12px; }
  .chat__disclaimer { padding-left: 12px; padding-right: 12px; }
  .chat__log { padding-left: 12px; padding-right: 12px; }
  .chat__compose { padding-left: 10px; padding-right: 10px; }
}
