/* Infex web — дизайн-токены из /api/theme (c1/c2/line подставляет app.js). */
:root {
  --c1: #ffffff;
  --c2: #000000;
  --line: 4px;
  --cut: 8px;           /* cornerCut = line*2 */
  --inset: 2px;         /* sideInset = line/2 */
  --text-xs: 12px;
  --text-sm: 16px;
  --text-md: 20px;
  --mono: "Noto Sans Mono", ui-monospace, monospace;
  --anim-fast: 200ms;
  --anim-base: 250ms;
  --anim-slow: 450ms;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--c2);
  color: var(--c1);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  overscroll-behavior: none;
  /* offscreen-панели не должны создавать горизонтальный скролл:
     clip запрещает и программный scrollLeft (фокус на их полях) */
  overflow-x: clip;
  max-width: 100vw;
}

#app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

button {
  font: inherit;
  color: var(--c1);
  background: none;
  border: none;
  cursor: pointer;
}
button:disabled { opacity: 0.4; cursor: default; }

input, textarea {
  font: inherit;
  color: var(--c1);
  background: none;
  border: none;
  outline: none;
  caret-color: var(--c1);
}
::placeholder { color: color-mix(in srgb, var(--c1) 35%, transparent); }
::selection { background: var(--c1); color: var(--c2); }

.hidden { display: none !important; }
.hiddenfile { display: none; }

/* === октагон-рамки: внешний слой = обводка c1, внутренний = фон c2 ===
 * толщина обводки ОДИНАКОВА на прямых и на срезах: внутренний срез
 * меньше на line·(√2−2) — параллельный офсентур из InfexBox
 * (√2−2 ≈ −0.586) */
.oct {
  --in: var(--cut);
  --cin: calc(var(--cut) - 0.586 * var(--line));
  clip-path: polygon(
    var(--in) 0, calc(100% - var(--in)) 0,
    100% var(--in), 100% calc(100% - var(--in)),
    calc(100% - var(--in)) 100%, var(--in) 100%,
    0 calc(100% - var(--in)), 0 var(--in));
  background: var(--c1);
  padding: var(--line);
}
.oct-in {
  clip-path: polygon(
    var(--cin) 0, calc(100% - var(--cin)) 0,
    100% var(--cin), 100% calc(100% - var(--cin)),
    calc(100% - var(--cin)) 100%, var(--cin) 100%,
    0 calc(100% - var(--cin)), 0 var(--cin));
  background: var(--c2);
  height: 100%;
}

/* шестиугольные строки чатов (Notch/Notch) */
.hexa {
  --nh: 14px;
  clip-path: polygon(
    var(--in) 0, calc(100% - var(--in)) 0,
    100% var(--in), calc(100% - var(--in) - var(--nh)) 50%,
    100% calc(100% - var(--in)),
    calc(100% - var(--in)) 100%, var(--in) 100%,
    0 calc(100% - var(--in)), calc(var(--in) + var(--nh)) 50%,
    0 var(--in));
}

/* === верхняя строка === */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(var(--line) * 2) 12px;
  position: absolute;
  top: 0; left: 0; right: 0;
  z-index: 10;
  pointer-events: none;
  /* подложка: прокрученные сообщения не видны сквозь панель */
  background: var(--c2);
  border-bottom: 1px solid color-mix(in srgb, var(--c1) 14%, transparent);
}
#topbar .tb-side { display: flex; gap: 8px; pointer-events: auto; }
.tb-right { margin-left: auto; }
#chatTitle {
  font-size: var(--text-sm);
  font-weight: bold;
  opacity: 0.9;
  pointer-events: auto;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tile {
  width: 40px; height: 40px;
  display: grid; place-items: center;
}
.icon {
  width: 55%; height: 55%;
  fill: none;
  stroke: var(--c1);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.iconbtn {
  width: 24px; height: 24px;
  display: grid; place-items: center;
  flex: none;
}
.iconbtn .icon { width: 16px; height: 16px; }

/* === лента сообщений === */
#chatScroll {
  flex: 1;
  overflow-y: auto;
  padding-top: 64px;
  padding-bottom: 130px;
  padding-left: 12px;
  padding-right: 12px;
  display: flex;
  justify-content: center;
}
#chatCol {
  width: 100%;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg-row { display: flex; }
.msg-row.right { justify-content: flex-end; }
.msg-row.left { justify-content: flex-start; }

.bubble { max-width: min(420px, 92%); min-width: 40px; }
.b-oct { --in: var(--cut); }
.b-in { padding: calc(var(--line) * 1.5); }

.msg-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: var(--text-sm);
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.msg-meta {
  display: flex;
  gap: 4px;
  align-items: center;
  justify-content: flex-end;
  margin-top: 4px;
  min-height: 16px;
}
.msg-row.left .msg-meta { justify-content: flex-start; }
.msg-meta .iconbtn { width: 18px; height: 18px; opacity: 0.55; }
.msg-meta .iconbtn:hover, .msg-meta .iconbtn.hot { opacity: 1; }
.msg-meta .iconbtn .icon { width: 13px; height: 13px; }

.msg-row.hit .bubble { outline: 2px solid var(--c1); outline-offset: 4px; }

/* вложения-плашки */
.atts { display: flex; flex-wrap: wrap; gap: 8px; }
.att-chip {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px;
  font-size: var(--text-xs);
  font-weight: bold;
}
.att-chip img {
  width: 32px; height: 32px;
  object-fit: cover;
}

/* === markdown === */
.msg-body h1, .msg-body h2, .msg-body h3,
.stream-body h1, .stream-body h2, .stream-body h3 {
  font-size: var(--text-md);
  margin: 4px 0 2px;
}
.msg-body h1, .stream-body h1 { font-size: var(--text-lg, 24px); }
.msg-body ul, .msg-body ol, .stream-body ul, .stream-body ol {
  padding-left: 22px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.msg-body code, .stream-body code {
  font-family: var(--mono);
  font-size: var(--text-xs);
  padding: 1px 4px;
}
.inline-code { background: color-mix(in srgb, var(--c1) 12%, transparent); }
.msg-body a, .stream-body a { color: var(--c1); }

/* блоки кода: рамка + шапка язык + копирование */
.codeblock {
  position: relative;
  padding: var(--line);
  margin: 0 calc(var(--line) / 2);
}
.codeblock .oct-in { background: var(--c2); padding: calc(var(--line) * 1.5); }
.code-head {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: var(--text-sm);
  font-weight: bold;
  margin-bottom: 6px;
}
.code-head .icon { width: 12px; height: 12px; }
.code-copy { margin-left: auto; }
.code-copy .icon { width: 12px; height: 12px; }
.code-text {
  font-family: var(--mono);
  font-size: var(--text-xs);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* таблицы в октагон-рамке с полной сеткой */
.tablewrap { padding: var(--line); }
.tablewrap .oct-in { padding: calc(var(--line) * 2); overflow-x: auto; }
table.inftable {
  border-collapse: collapse;
  width: 100%;
  font-size: var(--text-xs);
}
table.inftable td, table.inftable th {
  padding: 4px 8px 4px 14px;
  position: relative;
}
table.inftable th { font-weight: bold; }
table.inftable td + td::before, table.inftable th + th::before {
  content: "";
  position: absolute;
  left: calc(var(--line) / -2 - 1px);
  top: 0; bottom: 0;
  width: var(--line);
  background: var(--c1);
}
table.inftable tr + tr td, table.inftable thead + tbody tr td {
  border-top: var(--line) solid var(--c1);
}
table.inftable thead th { border-bottom: var(--line) solid var(--c1); }

/* горизонтальный разделитель */
.hr-line { height: var(--line); background: var(--c1); }

.msg-body img { max-width: 100%; max-height: 220px; }

/* === секции хода: reasoning + tool-вызовы ===
 * рамка только обводкой: внутренний слой с фоном c2 сливается
 * с фоном пузыря */
.secframe {
  position: relative;
  padding: var(--line);
  margin: 2px 0;
}
.secframe .oct-in { background: var(--c2); padding: calc(var(--line) * 1.5); }
.sec-content { display: flex; flex-direction: column; gap: 3px; }

.fold-head {
  display: flex;
  align-items: center;
  gap: 5px;
  min-height: 16px;
  font-size: var(--text-xs);
  font-weight: bold;
  cursor: pointer;
}
.fold-head .icon { width: 11px; height: 11px; flex: none; }
.fold-chev { margin-left: auto; transition: transform var(--anim-fast) cubic-bezier(0, 0, 0.2, 1); }
.fold-head.open .fold-chev { transform: rotate(180deg); }
.fold-body {
  overflow: hidden;
  font-size: var(--text-xs);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  transition: height var(--anim-fast) cubic-bezier(0, 0, 0.2, 1);
}

.tool-row {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 18px;
  font-size: var(--text-xs);
  font-weight: bold;
  cursor: pointer;
}
.tool-row .icon { width: 12px; height: 12px; flex: none; }
.tool-row .tname {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tool-status {
  width: 6px; height: 6px;
  margin-left: auto;
  flex: none;
  transform: rotate(45deg);
  background: var(--c2);
  border: 1px solid var(--c1);
}
.tool-status.done { background: var(--c1); }
.tool-detail {
  padding: 0 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: hidden;
  transition: height var(--anim-fast) cubic-bezier(0, 0, 0.2, 1);
}

/* ask-карточка */
.ask-q { font-weight: bold; font-size: var(--text-xs); margin-top: 4px; }
.ask-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--text-xs);
  cursor: pointer;
  padding: 3px 0;
  margin-left: 4px;
}
.ask-mark {
  width: 12px; height: 12px;
  flex: none;
  border: 1px solid var(--c1);
  display: grid; place-items: center;
}
.single .ask-mark { border-radius: 50%; }
.ask-mark.on { background: var(--c1); }
.ask-opt.picked span { font-weight: bold; }
.ask-custom {
  margin-left: 18px;
  border-bottom: 1px solid color-mix(in srgb, var(--c1) 40%, transparent);
  font-style: italic;
  font-size: var(--text-xs);
  padding: 2px 0;
  flex: 1;
  min-width: 80px;
}
.ask-btns { display: flex; gap: 14px; margin-top: 6px; }
.txtbtn {
  font-size: var(--text-xs);
  text-decoration: underline;
  text-decoration-color: transparent;
  transition: text-decoration-color var(--anim-fast);
}
.txtbtn:hover { text-decoration-color: var(--c1); }
.txtbtn.bold { font-weight: bold; }

/* «думаю...» */
.thinking-dots { font-size: var(--text-sm); }

/* ошибки */
.err-text { font-size: var(--text-sm); }

/* === стрим (футер) === */
#streamFooter {
  display: flex;
  justify-content: center;
  padding: 0 12px 0;
  position: absolute;
  bottom: 16px; left: 0; right: 0;
  z-index: 9;
  pointer-events: none;
}
#app.streaming #inputArea { display: none; }
#app.streaming #chatScroll {
  padding-bottom: 40px;
}
#streamFooter .msg-row { width: 100%; max-width: 640px; margin: 0 auto; }
#streamFooter .bubble { pointer-events: auto; }

/* === ввод === */
#inputArea {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  display: flex;
  justify-content: center;
  padding: 0 12px calc(var(--line) * 1.5);
  z-index: 10;
}
#inputArea > .oct {
  width: 100%;
  max-width: 640px;
}
.i-in {
  padding: 4px 6px 6px;
  display: flex;
  flex-direction: column;
}
#inputField {
  padding: 4px 10px;
  font-size: var(--text-sm);
  max-height: 150px;
  resize: none;
}
.i-divider {
  height: var(--line);
  background: var(--c1);
  margin: 4px 0;
}
.i-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 6px;
  min-height: 24px;
}
.chip {
  font-size: var(--text-xs);
  padding: 2px 8px;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chip:hover { text-decoration: underline; }
#modelChip { margin-left: auto; }
#ctxGauge { width: 24px; height: 24px; flex: none; position: relative; }
#ctxGauge:hover .ctx-tip { visibility: visible; }
.gauge { width: 100%; height: 100%; }
.g-track, .g-fill, .g-full {
  fill: none;
  stroke: var(--c1);
}
.g-track { stroke-width: 1.5; }
.g-fill { stroke-width: 2.5; stroke-dasharray: 50.3; transform: rotate(-90deg); transform-origin: center; }
.g-full { stroke-width: 2.5; opacity: 0; }
.ctx-tip {
  visibility: hidden;
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  padding: calc(var(--line) * 1.5) calc(var(--line) * 3);
  font-size: var(--text-xs);
  white-space: nowrap;
}

#attachStrip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 8px;
}
#attachStrip .att-chip { flex: none; position: relative; }
.att-remove {
  position: absolute;
  top: -6px; right: -6px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--c1);
  color: var(--c2);
  font-size: 10px;
  line-height: 16px;
  text-align: center;
  font-weight: bold;
}

/* === панель чатов / настроек === */
#chatsPanel, #settingsPanel {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: min(340px, 100vw);
  z-index: 20;
  transition: transform var(--anim-slow) cubic-bezier(0, 0.215, 0.61, 0.355);
}
#chatsPanel.offscreen, #settingsPanel.offscreen {
  transform: translateX(calc(100% + 20px));
  /* спрятанные панели не ловят фокус и не скроллят документ */
  visibility: hidden;
  transition: transform var(--anim-slow) cubic-bezier(0, 0.215, 0.61, 0.355),
    visibility 0s var(--anim-slow);
}
#chatsPanel:not(.offscreen), #settingsPanel:not(.offscreen) {
  visibility: visible;
  transition: transform var(--anim-slow) cubic-bezier(0, 0.215, 0.61, 0.355);
}
#chatsPanel .c-oct, #settingsPanel .c-oct {
  height: 100%;
  --in: var(--cut);
  --cin: calc(var(--cut) - 0.586 * var(--line));
}
#chatsPanel .c-oct, #settingsPanel .c-oct {
  /* правая кромка заподлицо с краем экрана, углы слева срезаны */
  clip-path: polygon(
    var(--in) 0, 100% 0, 100% 100%,
    var(--in) 100%, 0 calc(100% - var(--in)), 0 var(--in));
}
#chatsPanel .oct-in, #settingsPanel .oct-in {
  clip-path: polygon(
    var(--cin) 0, 100% 0, 100% 100%,
    var(--cin) 100%, 0 calc(100% - var(--cin)), 0 var(--cin));
  padding: calc(var(--line) * 2);
  display: flex;
  flex-direction: column;
}
.chats-head {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  min-height: 40px;
}
.chats-title { font-weight: bold; }
.chats-head .tile { position: absolute; right: 0; }
#chatsList, #settingsBody {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 0;
}
#settingsBody { gap: calc(var(--line) * 2); }

.chat-row {
  position: relative;
  height: 40px;
  cursor: pointer;
}
.chat-row .hexa { position: absolute; inset: 0; background: var(--c1); }
.chat-row .hexa-in {
  position: absolute;
  inset: var(--line);
  background: var(--c2);
}
.chat-row.active .hexa-in { background: var(--c1); }
.chat-row-title {
  position: absolute;
  left: 24px; right: 46px;
  top: 0; bottom: 0;
  display: flex;
  align-items: center;
  font-size: var(--text-xs);
  font-weight: bold;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.chat-row.active .chat-row-title { color: var(--c2); }
.chat-row input.chat-rename {
  position: absolute;
  left: 24px; right: 46px;
  top: 0; bottom: 0;
  font-size: var(--text-xs);
  font-weight: bold;
  color: var(--c2);
}
.chat-dots {
  position: absolute;
  right: 10px;
  top: 0; bottom: 0;
  width: 24px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 3px;
}
.chat-dots i {
  width: 3px; height: 3px;
  border-radius: 1.5px;
  background: var(--c1);
}
.chat-row.active .chat-dots i { background: var(--c2); }

/* попапы (действия чата/сообщения, выбор модель/ризонинг) */
#chatActions, #msgActions, #selectPopup {
  position: absolute;
  z-index: 30;
}
.pop-oct { width: 100%; height: 100%; }
#chatActions .oct, #msgActions .oct {
  --in: var(--cut);
}
.a-in, .p-in {
  display: flex;
  flex-direction: column;
  padding: 4px;
}
.a-in button, .p-in button {
  text-align: left;
  font-size: var(--text-xs);
  padding: 5px 8px;
  min-height: 24px;
}
.a-in button:hover, .p-in button:hover { font-weight: bold; }
#selectRows {
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  max-height: 240px;
  padding: 4px;
}
#selectRows button {
  text-align: left;
  font-size: var(--text-xs);
  padding: 5px 10px;
  min-height: 24px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#selectRows button.cur { font-weight: bold; }

/* поиск */
#searchPanel {
  position: absolute;
  top: 56px;
  right: 12px;
  width: min(340px, calc(100vw - 24px));
  z-index: 15;
}
.s-in {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  min-height: 38px;
}
#searchInput { flex: 1; font-size: var(--text-sm); min-width: 60px; }
#searchCounter { font-size: var(--text-xs); }

/* настройки агента */
.set-label { font-weight: bold; }
.set-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: calc(var(--line) * 2);
}
.set-field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--text-xs);
}
.set-field input {
  width: 74px;
  border-bottom: var(--line) solid color-mix(invar(--c1), transparent);
  border-bottom: var(--line) solid var(--c2);
  padding: 2px 4px;
  text-align: right;
}
.set-sep { height: var(--line); background: var(--c2); }
.prov-row { font-size: var(--text-xs); display: flex; align-items: center; gap: 8px; }
.prov-marker {
  width: 16px; height: 16px;
  flex: none;
  border: 1px solid var(--c1);
  display: grid; place-items: center;
  cursor: pointer;
}
.prov-marker.on { background: var(--c1); }
.prov-name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.prov-edit-form {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  margin: 4px 0;
}
.prov-edit-form .set-field input { flex: 1; width: auto; text-align: left; }
.type-btns { display: flex; gap: 6px; }
.type-btns button {
  flex: 1;
  font-size: var(--text-xs);
  padding: 6px 4px;
}
.type-btns button.on { background: var(--c1); color: var(--c2); font-weight: bold; }
.reason-row { display: flex; align-items: center; gap: 6px; font-size: var(--text-xs); }
.reason-row input { flex: 1; min-width: 40px; border-bottom: var(--line) solid var(--c2); padding: 2px 4px; }
.reason-row .del { font-weight: bold; padding: 0 6px; }
.key-field { margin-bottom: 6px; }
.key-value {
  font-family: var(--mono);
  font-size: var(--text-xs);
  padding: 2px 6px;
  overflow-wrap: anywhere;
}
.set-hint {
  font-size: var(--text-xs);
  opacity: 0.55;
  line-height: 1.4;
}
.wide-btn {
  border: var(--line) solid var(--c1);
  padding: 8px;
  font-size: var(--text-xs);
  font-weight: bold;
  text-align: center;
}

/* редактирование сообщения */
.overlay {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--c2) 70%, transparent);
  display: grid;
  place-items: center;
  z-index: 40;
  padding: 16px;
}
.e-oct { width: min(480px, 100%); --in: var(--cut); }
.e-in { padding: calc(var(--line) * 2); display: flex; flex-direction: column; gap: 8px; }
#editField { font-size: var(--text-sm); width: 100%; }
.edit-btns { display: flex; gap: 16px; justify-content: flex-end; }

/* логин */
.login-in { align-items: center; gap: 12px; padding: 32px; }
.login-title { font-size: var(--text-md); font-weight: bold; }
#loginToken {
  border-bottom: var(--line) solid var(--c1);
  padding: 6px 8px;
  width: 220px;
  text-align: center;
}
#loginErr { font-size: var(--text-xs); min-height: 16px; }

/* === мобильная версия === */
@media (max-width: 700px) {
  :root { --cut: 7px; }
  #chatCol { max-width: 100%; }
  .bubble { max-width: 96%; }
  .msg-meta .iconbtn { width: 26px; height: 26px; }
  .msg-meta .iconbtn .icon { width: 16px; height: 16px; }
  .i-toolbar { min-height: 34px; }
  .iconbtn { width: 30px; height: 30px; }
  #topbar { padding: 6px 6px 6px 10px; }
  #chatTitle { max-width: calc(100vw - 150px); font-size: var(--text-xs); }
  #chatsPanel, #settingsPanel { width: 100vw; }
  .set-grid { grid-template-columns: 1fr; }
  /* таблицы: скролл внутри пузыря, а не страницы */
  .tablewrap .oct-in { max-width: 100%; }
  table.inftable { min-width: max-content; }
  /* поле ввода: iOS не зумит при 16px+ */
  #inputField, #searchInput { font-size: 16px; }
  .code-text { font-size: 11px; }
}
