:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --ink: #16202b;
  --muted: #6c7a89;
  --line: #e2e8ef;
  --accent: #14508c;
  --accent-soft: #e7effa;
  --warn: #b26a00;
  --danger: #b3261e;
}

* {
  box-sizing: border-box;
}

/* .start и .chat — flex-контейнеры, поэтому атрибут hidden нужно усилить: иначе он не скрывает секции. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  display: flex;
  flex-direction: column;
}

/* --- шапка --- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 18px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.logo {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  display: grid;
  place-items: center;
  flex: none;
}

.brandText {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brandText strong {
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sub {
  color: var(--muted);
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topActions {
  display: flex;
  gap: 8px;
  flex: none;
}

.ghost {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--accent);
  border-radius: 8px;
  padding: 7px 12px;
  font: inherit;
  font-size: 13.5px;
  cursor: pointer;
}

.ghost:hover {
  background: var(--accent-soft);
}

.ghost.active {
  background: var(--accent-soft);
  border-color: #cfe0f5;
}

.ghost.small {
  padding: 5px 9px;
  font-size: 12.5px;
}

/* На узких экранах длинная подпись кнопки заменяется на «?», чтобы шапка не занимала строку. */
.labelShort {
  display: none;
}

.iconBtn {
  display: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: 8px;
  width: 34px;
  height: 34px;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  flex: none;
}

/* --- левая колонка: список чатов --- */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 12px;
  background: var(--card);
  border-right: 1px solid var(--line);
}

.newChat {
  border: 0;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  font-size: 13.5px;
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  flex: none;
}

.newChat:hover {
  background: #103f6f;
}

.chatSearch {
  border: 1px solid var(--line);
  background: #fbfcfe;
  border-radius: 9px;
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
  flex: none;
}

.chatSearch:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}

.chatList {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
  min-height: 0;
  flex: 1;
}

.chatItem {
  position: relative;
  display: flex;
  border-radius: 10px;
}

.chatItem:hover {
  background: var(--accent-soft);
}

.chatItem.active {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1.5px rgba(20, 80, 140, 0.35);
}

.chatOpen {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 8px 30px 9px 10px;
  border: 0;
  border-radius: 10px;
  background: none;
  text-align: left;
  font: inherit;
  cursor: pointer;
  color: inherit;
}

.chatOpen:focus-visible {
  outline: 2px solid rgba(20, 80, 140, 0.35);
}

.ciTitle,
.ciPrev {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ciTitle {
  font-size: 13.5px;
  font-weight: 600;
}

.ciMeta {
  font-size: 11.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ciPrev {
  font-size: 12px;
  color: #4a5a6a;
}

.chatDel {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s ease;
}

.chatItem:hover .chatDel,
.chatDel:focus-visible {
  opacity: 1;
}

.chatDel:hover {
  background: #fbe9e7;
  color: var(--danger);
}

.chatEmpty {
  padding: 14px 10px;
  color: var(--muted);
  font-size: 12.5px;
}

.sideFoot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex: none;
  padding-top: 6px;
  border-top: 1px solid var(--line);
}

/* --- правая колонка: инструкция --- */

.guide {
  display: none;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 16px 28px;
  background: var(--card);
  border-left: 1px solid var(--line);
  font-size: 13px;
  color: #33475b;
}

.guideHead {
  font-size: 15px;
  margin: 0 0 12px;
  color: var(--ink);
}

.gSection {
  margin-bottom: 16px;
}

.gTitle {
  font-size: 12px;
  margin: 0 0 5px;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.gText {
  margin: 0 0 8px;
  line-height: 1.55;
}

.gList {
  margin: 0 0 8px;
  padding-left: 18px;
  line-height: 1.55;
}

.gList li {
  margin-bottom: 5px;
}

.gPill {
  display: inline-block;
  background: #fff6e5;
  border: 1px solid #f0d7ab;
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 11.5px;
  color: var(--warn);
  white-space: nowrap;
}

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(16, 32, 48, 0.35);
  z-index: 50;
}

.app {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 276px minmax(0, 1fr);
}

.main {
  min-width: 0;
  min-height: 0;
  padding: 16px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.main > .start,
.main > .chat {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
}

/* --- выбор объекта --- */

.start {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.start h1 {
  font-size: 20px;
  margin: 0 0 6px;
}

.lead {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 13.5px;
  max-width: 68ch;
}

.picker {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

#objectSearch {
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font: inherit;
  background: #fbfcfe;
}

#objectSearch:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}

.objectList {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow-y: auto;
  max-height: 52vh;
  background: #fff;
}

.objectItem {
  width: 100%;
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 9px 12px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: none;
  text-align: left;
  font: inherit;
  cursor: pointer;
}

.objectList li:last-child .objectItem {
  border-bottom: 0;
}

.objectItem:hover,
.objectItem:focus-visible {
  background: var(--accent-soft);
  outline: none;
}

.oiPhoto {
  width: 56px;
  height: 44px;
  object-fit: cover;
  border-radius: 7px;
  background: var(--accent-soft);
}

.oiPhoto.noPhoto {
  display: block;
}

.oiMain {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.oiTitle {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.oiAddr {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.oiPrice {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
}

.objectEmpty {
  padding: 16px;
  color: var(--muted);
  font-size: 13.5px;
}

.startFooter {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
}

.meta {
  color: var(--muted);
  font-size: 12.5px;
}

/* --- чат по объекту --- */

.chat {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  min-height: 0;
}

/* Карточка объявления: строка с названием и ценой кликабельна, характеристики — под ней.
   На телефоне карточка по умолчанию свёрнута, чтобы переписка занимала экран. */
.objectCard {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  flex: none;
}

.objectToggle {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.objectToggle:focus-visible {
  outline: 2px solid var(--accent-soft);
  outline-offset: 4px;
  border-radius: 10px;
}

.objectPhoto {
  width: 108px;
  height: 82px;
  object-fit: cover;
  border-radius: 10px;
  flex: none;
  background: var(--accent-soft);
}

.objectBody {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 0;
}

.objectTitle {
  font-size: 15.5px;
  font-weight: 700;
  line-height: 1.3;
}

.objectChevron {
  flex: none;
  align-self: center;
  color: var(--muted);
  font-size: 13px;
  transition: transform 0.15s ease;
}

.objectCard.collapsed .objectChevron {
  transform: rotate(-90deg);
}

.objectDetails {
  min-width: 0;
}

.objectCard.collapsed .objectDetails {
  display: none;
}

.objectCard.collapsed .objectTitle,
.objectCard.collapsed .address {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.objectCard.collapsed .objectLine {
  flex-wrap: nowrap;
  margin-bottom: 0;
  overflow: hidden;
}

.objectCard.collapsed .price {
  white-space: nowrap;
}

.objectLine {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 6px;
}

.price {
  font-weight: 700;
  color: var(--accent);
}

.address {
  color: var(--muted);
  font-size: 13px;
}

.facts {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  margin: 0;
  padding: 0;
  font-size: 12.5px;
  color: #33475b;
}

.factLabel {
  color: var(--muted);
}

.messages {
  flex: 1;
  min-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 4px 2px;
}

.msg {
  display: flex;
}

.msg-user {
  justify-content: flex-end;
}

.bubble {
  max-width: min(680px, 88%);
  padding: 10px 13px;
  border-radius: 14px;
  background: var(--card);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg-user .bubble {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg-assistant .bubble {
  border-bottom-left-radius: 4px;
}

.bubble.typing .bubbleText {
  color: var(--muted);
  font-style: italic;
}

.bubble.hasTransfer {
  border-color: #f0d7ab;
  background: #fffaf0;
}

.msg-user .bubble.hasTransfer {
  background: var(--accent);
}

.transferNote {
  margin-top: 6px;
  font-size: 12px;
  color: var(--warn);
}

/* Служебное сообщение системы (передача оператору): по центру, пунктирная рамка, приписка про Авито. */
.msg-system {
  justify-content: center;
}

.systemBody {
  max-width: min(680px, 94%);
  padding: 10px 13px;
  border: 1px dashed #c3d2e4;
  border-radius: 12px;
  background: #eef3f9;
  color: var(--ink);
  overflow-wrap: anywhere;
}

.systemHead {
  margin-bottom: 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
}

.systemNote {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px dashed #c3d2e4;
  font-size: 12px;
  color: var(--warn);
}

/* Рассуждения модели: модель думает перед каждым ответом, блок свёрнут до клика. */
.think {
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px dashed var(--line);
}

.thinkSum {
  cursor: pointer;
  font-size: 12px;
  color: var(--muted);
  white-space: normal;
  list-style: none;
}

.thinkSum::-webkit-details-marker {
  display: none;
}

.thinkSum::before {
  content: '▸ ';
}

.think[open] .thinkSum::before {
  content: '▾ ';
}

.thinkBody {
  margin-top: 6px;
  max-height: 240px;
  overflow: auto;
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--muted);
  white-space: pre-wrap;
}

.composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px;
  flex: none;
}

#input {
  flex: 1;
  border: 0;
  resize: none;
  font: inherit;
  padding: 8px 10px;
  max-height: 160px;
  background: none;
  color: inherit;
}

#input:focus {
  outline: none;
}

#btnSend {
  border: 0;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  border-radius: 9px;
  padding: 9px 16px;
  cursor: pointer;
  flex: none;
}

#btnSend:disabled {
  opacity: 0.55;
  cursor: default;
}

.status {
  min-height: 18px;
  font-size: 12.5px;
  color: var(--muted);
  flex: none;
}

.status.error {
  color: var(--danger);
}

@media (min-width: 1240px) {
  .app {
    grid-template-columns: 276px minmax(0, 1fr) 340px;
  }

  .guide {
    display: block;
  }

  body.guideHidden .app {
    grid-template-columns: 276px minmax(0, 1fr);
  }

  body.guideHidden .guide {
    display: none;
  }
}

@media (max-width: 1239px) {
  body.guideOpen .guide {
    display: block;
    position: fixed;
    top: 0;
    right: 0;
    height: var(--appH, 100dvh);
    width: min(370px, 92vw);
    padding-bottom: max(28px, env(safe-area-inset-bottom));
    z-index: 60;
    box-shadow: -10px 0 28px rgba(16, 32, 48, 0.18);
  }
}

/* --- телефон: переписка занимает экран, поле ввода всегда видно --- */

@media (max-width: 860px) {
  /* Высоту окна считаем по visualViewport (--appH выставляет app.js): иначе панель инструментов
     мобильного браузера (Яндекс, Chrome) перекрывает низ экрана вместе с полем ввода. */
  html,
  body {
    height: 100%;
    height: 100dvh;
    height: var(--appH, 100dvh);
    overflow: hidden;
    overscroll-behavior: none;
  }

  .app {
    grid-template-columns: minmax(0, 1fr);
  }

  .topbar {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: 8px;
  }

  .iconBtn {
    display: inline-grid;
    place-items: center;
  }

  .main {
    padding: 8px 8px 0;
  }

  .chat {
    gap: 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: var(--appH, 100dvh);
    width: 282px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    z-index: 60;
    transform: translateX(-100%);
    transition: transform 0.16s ease;
    box-shadow: 10px 0 28px rgba(16, 32, 48, 0.18);
  }

  body.sidebarOpen .sidebar {
    transform: none;
  }

  .objectCard {
    border-radius: 12px;
    padding: 8px 10px;
  }

  .objectPhoto {
    width: 46px;
    height: 40px;
    border-radius: 8px;
  }

  .objectTitle {
    font-size: 13.5px;
  }

  .objectLine {
    gap: 8px;
    margin-bottom: 0;
  }

  .facts {
    font-size: 12px;
  }

  /* Раньше min-height: 220px выдавливал композер за границу экрана. */
  .messages {
    min-height: 0;
  }

  .bubble {
    max-width: 92%;
  }

  .composer {
    gap: 8px;
    border-radius: 12px;
    padding: 6px 6px 6px 10px;
  }

  /* 16px: при меньшем размере мобильные браузеры увеличивают страницу на фокусе. */
  #input {
    font-size: 16px;
    padding: 6px 0;
    max-height: 120px;
  }

  #btnSend {
    padding: 9px 13px;
  }

  .status:empty {
    display: none;
  }

  .start {
    padding: 14px;
    margin-bottom: 8px;
  }

  .picker {
    flex: 1;
  }

  .objectList {
    flex: 1;
    max-height: none;
    min-height: 160px;
  }
}

@media (max-width: 620px) {
  .main {
    padding: 6px 6px 0;
  }

  .topbar {
    gap: 8px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .logo {
    width: 30px;
    height: 30px;
    font-size: 12px;
  }

  /* Подзаголовок дублирует свёрнутую карточку объекта и на телефоне только занимает строку. */
  .sub {
    display: none;
  }

  .brandText strong {
    font-size: 13.5px;
    line-height: 1.2;
  }

  /* Длинная подпись кнопки на телефоне заменяется на «?»: шапка остаётся в одну строку. */
  .labelFull {
    display: none;
  }

  .labelShort {
    display: inline;
  }

  .topActions .ghost {
    min-width: 34px;
    padding: 7px 9px;
    font-size: 13.5px;
    text-align: center;
  }

  .objectPhoto {
    width: 42px;
    height: 38px;
  }

  .oiPhoto {
    width: 46px;
    height: 38px;
  }

  .objectItem {
    grid-template-columns: 46px minmax(0, 1fr) auto;
    gap: 9px;
  }

  .bubble {
    max-width: 94%;
    padding: 9px 11px;
  }

  .start h1 {
    font-size: 17px;
  }

  .lead {
    font-size: 13px;
    margin-bottom: 10px;
  }
}
