/* ═══════════════════════════════════════════════════════════════════════════
   AI Граф — тёплый минимализм в духе Claude.
   Темы задаются CSS-переменными на html[data-theme].
   ═══════════════════════════════════════════════════════════════════════════ */

:root, html[data-theme="dark"] {
  --bg: #262624;
  --sidebar: #1F1E1D;
  --panel: #2B2A27;
  --card: #343230;
  --input: #30302E;
  --border: #3E3C38;
  --border-lt: #4C4A44;
  --text: #F0EEE6;
  --muted: #A6A39B;
  --dim: #75726A;
  --accent: #D97757;
  --accent-h: #C46647;
  --accent-tx: #FFFFFF;
  --green: #8FB573;
  --yellow: #D4A054;
  --red: #CF5C5C;
  --blue: #8AA9CE;
  --concl: #E3B65C;   /* выводы: узлы и линии к ним */
  --bubble-user: #3B3936;
  --ok-bg: #2D3428;
  --err-bg: #3B2B27;
  --danger-h: #4A322C;
  --hover: #2B2A27;
  --active: #343330;
  --shadow: 0 12px 40px rgba(0, 0, 0, .45);
  --grid-line: rgba(240, 238, 230, .08);
}

html[data-theme="light"] {
  --bg: #FAF9F5;
  --sidebar: #F4F2EC;
  --panel: #EFEDE5;
  --card: #E7E4DA;
  --input: #FFFFFF;
  --border: #D9D5C9;
  --border-lt: #CCC8BB;
  --text: #262624;
  --muted: #6E6B63;
  --dim: #9A968C;
  --accent: #D97757;
  --accent-h: #C46647;
  --accent-tx: #FFFFFF;
  --green: #5F8747;
  --yellow: #B08430;
  --red: #C0504A;
  --blue: #5B82B0;
  --concl: #B5832A;
  --bubble-user: #E9E6DC;
  --ok-bg: #E7EDDC;
  --err-bg: #F4E0D9;
  --danger-h: #EFD8CE;
  --hover: #E4E1D6;
  --active: #DAD6C9;
  --shadow: 0 12px 40px rgba(60, 50, 40, .18);
  --grid-line: rgba(38, 38, 36, .09);
}

/* ── База ─────────────────────────────────────────────────────────────────── */

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

/* Атрибут hidden сильнее оформления: любое наше display:flex/grid иначе
   перебивает встроенное правило браузера, и скрытый из JS элемент
   остаётся на экране. Ниже по файлу есть точечные правила для отдельных
   блоков — это общий случай для всех остальных. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

svg { width: 18px; height: 18px; flex-shrink: 0; }
/* Правило выше — для значков. Слои plotly тоже <svg>, и оно ужимало их
   до 18×18: сама 3D-сцена рисуется на WebGL и не страдала, а легенда,
   заголовок и подсказки при наведении, которые plotly кладёт в эти слои,
   обрезались целиком (08.10). Размер у слоёв свой — из атрибутов. */
.js-plotly-plot svg.main-svg { width: auto; height: auto; }

.caption { font-size: 12px; color: var(--muted); line-height: 1.45; }
.web-privacy a { color: var(--accent); text-underline-offset: 3px; }
.web-privacy p { font-size: 13px; line-height: 1.7; }
.ok-text { color: var(--green); }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-lt); }
::selection { background: rgba(217, 119, 87, .35); }

#app { display: flex; height: 100vh; }

/* Установленное на телефон приложение рисуется под вырезом камеры и
   жестовой полосой. В браузере и в окне эти отступы нулевые */
#app {
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ── Сайдбар ──────────────────────────────────────────────────────────────── */

/* Шторка проектов (выдвигается поверх полотна) */
#scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .4);
  z-index: 40;
  animation: fade .15s ease-out;
}
#scrim[hidden] { display: none; }

#drawer {
  position: fixed;
  left: 0; top: 0; bottom: 0;
  width: 272px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: calc(12px + env(safe-area-inset-top)) 10px
           calc(10px + env(safe-area-inset-bottom))
           calc(10px + env(safe-area-inset-left));
  z-index: 45;
  transform: translateX(-100%);
  transition: transform .22s cubic-bezier(.4, 0, .2, 1);
}
body.drawer-open #drawer { transform: none; box-shadow: var(--shadow); }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 12px;
}

.logo-row {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 4px 8px;
}

.logo-mark { width: 20px; height: 20px; color: var(--accent); }

.logo-text {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: .2px;
}

.proj-add {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 9px 12px;
  margin-top: 4px;
  border: 1px dashed var(--border-lt);
  border-radius: 10px;
  color: var(--muted);
  font-size: 13px;
  transition: color .12s, border-color .12s, background .12s;
}
.proj-add svg { width: 15px; height: 15px; }
.proj-add:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--hover);
}

#project-list { flex: 1; overflow-y: auto; padding: 0 2px; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  text-align: left;
  color: var(--text);
  transition: background .12s;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); }

.chat-item > svg { width: 15px; height: 15px; color: var(--dim); }
.chat-item.active > svg { color: var(--accent); }

.chat-item .title {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-item .meta { font-size: 11px; color: var(--dim); }

.chat-item .del {
  width: 26px; height: 26px;
  border-radius: 7px;
  color: var(--dim);
  display: none;
  align-items: center;
  justify-content: center;
}
.chat-item .del svg { width: 14px; height: 14px; }
.chat-item:hover .del { display: flex; }
.chat-item .del:hover { background: var(--danger-h); color: var(--red); }
.chat-item .del.armed {
  display: flex;
  background: var(--danger-h);
  color: var(--red);
  animation: blink 1.2s infinite;
}

.chat-empty { padding: 10px 12px; }

.drawer-footer {
  border-top: 1px solid var(--border);
  padding-top: 8px;
  margin-top: 8px;
}

.footer-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  color: var(--muted);
  transition: background .12s, color .12s;
}
.footer-row:hover { background: var(--hover); color: var(--text); }
.footer-row svg { width: 17px; height: 17px; }

.status-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 4px;
}

.dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--dim);
  flex-shrink: 0;
}
.dot.ok   { background: var(--green); }
.dot.warn { background: var(--yellow); }
.dot.err  { background: var(--red); }
.dot.ext  { background: var(--blue); }

.status-text { display: flex; flex-direction: column; min-width: 0; }
.status-text > span:first-child {
  font-size: 12px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ── Основная область и виды ──────────────────────────────────────────────── */

#main { flex: 1; min-width: 0; position: relative; }

.view {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
}
.view[hidden] { display: none; }

/* Гамбургер открытия шторки */
.btn-drawer { flex-shrink: 0; }
#view-empty .view-menu {
  position: absolute;
  top: 14px; left: 14px;
}

/* ── Пустой экран ─────────────────────────────────────────────────────────── */

#view-empty, #view-loading { align-items: center; justify-content: center; }

.hero { text-align: center; max-width: 460px; padding: 24px; }
.loading-pulse { animation: loading-pulse 1.6s ease-in-out infinite; }
@keyframes loading-pulse {
  0%, 100% { opacity: .35; transform: scale(.94); }
  50%      { opacity: 1;   transform: scale(1); }
}
.hero-icon {
  width: 52px; height: 52px;
  color: var(--accent);
  margin-bottom: 18px;
}
.hero h1 {
  font-family: Georgia, serif;
  font-size: 26px;
  margin-bottom: 10px;
}
.hero p { color: var(--muted); line-height: 1.6; margin-bottom: 22px; }

/* ── Древо проекта ────────────────────────────────────────────────────────── */

.tree-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 13px 24px 12px 14px;
  position: relative;
}
.tree-header .spacer { flex: 1; }

/* Плашка классов проекта */
#class-panel {
  position: absolute;
  top: 54px;
  left: 56px;
  z-index: 35;
  width: 340px;
  max-height: 62vh;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 12px;
  animation: pop .15s ease-out;
}
#class-panel[hidden] { display: none; }
.cp-head input {
  font-family: Georgia, serif;
  font-size: 16px;
  font-weight: 700;
}
.cp-caption { padding: 8px 2px 6px; }

.cp-class { margin-bottom: 4px; }
.cp-class-row {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 9px;
  border-radius: 9px;
  cursor: pointer;
  transition: background .1s;
}
.cp-class-row:hover { background: var(--hover); }
.cp-class-row .cdot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.cp-class-row .cname { flex: 1; font-size: 13px; font-weight: 600; }
.cp-class-row .ccount { font-size: 11px; color: var(--dim); }
.cp-class-row .cgear, .cp-class-row .cdel {
  width: 24px; height: 24px;
  border-radius: 7px;
  color: var(--dim);
  display: none;
  align-items: center; justify-content: center;
}
.cp-class-row:hover .cgear,
.cp-class-row:hover .cdel { display: flex; }
.cp-class-row .cgear svg, .cp-class-row .cdel svg { width: 13px; height: 13px; }
.cp-class-row .cgear:hover { background: var(--active); color: var(--text); }
.cp-class-row .cdel:hover { background: var(--danger-h); color: var(--red); }

.cp-members { padding: 2px 0 4px 26px; }
.cp-member {
  display: block;
  width: 100%;
  text-align: left;
  padding: 5px 9px;
  border-radius: 7px;
  font-size: 12.5px;
  color: var(--muted);
}
.cp-member:hover { background: var(--hover); color: var(--text); }

.cp-settings {
  margin: 4px 0 8px 26px;
  padding: 10px;
  background: var(--panel);
  border-radius: 10px;
}
.cp-settings .field { margin-bottom: 10px; }
.cp-settings .field:last-child { margin-bottom: 0; }

/* Свотчи цвета */
.swatches { display: flex; flex-wrap: wrap; gap: 7px; }
.swatch {
  width: 26px; height: 26px;
  border-radius: 8px;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform .1s, border-color .1s;
  position: relative;
}
.swatch:hover { transform: scale(1.12); }
.swatch.on { border-color: #FFFFFF; }
.swatch.auto { background: var(--input) !important; }


/* Предложения связей */
#suggest-bar {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 14px;
  z-index: 26;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 560px;
}
#suggest-bar[hidden] { display: none; }
.suggestion {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 12.5px;
  box-shadow: var(--shadow);
  animation: rise .2s ease-out;
}
.suggestion .s-text { flex: 1; line-height: 1.4; }
.suggestion .s-text b { color: var(--accent); }
.suggestion button { flex-shrink: 0; }

.tree-h {
  font-family: Georgia, serif;
  font-size: 20px;
  font-weight: 700;
  cursor: default;
  padding: 2px 8px;
  border-radius: 8px;
}
.tree-h:hover { background: var(--hover); }

/* Бесконечное поле: без полос прокрутки, панорама перетаскиванием ПКМ */
#tree-scroll {
  flex: 1;
  overflow: hidden;
  position: relative;
}
#tree-scroll.panning { cursor: grabbing; }
#tree-scroll { touch-action: none; }

/* Фоновая сетка рабочего поля (двигается и масштабируется вместе с полем) */
#tree-scroll.grid-on {
  background-image:
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 40px 40px;
}

#tree-canvas {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  will-change: transform;
}

#tree-lines {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: none;
}
#tree-lines path.link-vis {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  opacity: .55;
  stroke-linecap: round;
  transition: opacity .12s, stroke-width .12s;
}
#tree-lines circle.link-dot {
  fill: var(--accent);
  opacity: .7;
}
#tree-lines path.link-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 16;
  pointer-events: stroke;
  cursor: pointer;
}
#tree-lines g[data-link]:hover path.link-vis {
  opacity: 1;
  stroke-width: 2.6;
}
#tree-lines g[data-link]:hover circle.link-dot { opacity: 1; }
/* Линии к выводам: градиент от цвета связи к цвету вывода, искры бегут
   к узлу, а там, где ветка растёт из связи, — пульсирующая почка */
#tree-lines stop.g-from { stop-color: var(--accent); }
#tree-lines stop.g-to { stop-color: var(--concl); }
#tree-lines path.link-vis.concl-line {
  stroke-width: 2.6;
  opacity: .85;
}
#tree-lines g.concl:hover path.link-vis {
  opacity: 1;
  stroke-width: 3.4;
}
#tree-lines circle.flow-dot {
  fill: var(--concl);
  filter: drop-shadow(0 0 3px var(--concl));
}
#tree-lines circle.bud {
  fill: var(--concl);
  stroke: var(--bg);
  stroke-width: 2;
  transform-box: fill-box;
  transform-origin: center;
  animation: bud-pulse 2.4s ease-in-out infinite;
}
@keyframes bud-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.3); }
}
@media (prefers-reduced-motion: reduce) {
  #tree-lines circle.bud { animation: none; }
}

/* Карточка группы — свободно позиционируется и перетаскивается */
.tgroup {
  position: absolute;
  width: 228px;
  background: var(--sidebar);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px;
  transition: border-color .12s, box-shadow .12s;
}
.tgroup:hover { border-color: var(--border-lt); }
.tgroup.dragging {
  z-index: 20;
  box-shadow: var(--shadow);
  border-color: var(--accent);
  opacity: .95;
}
/* Визуал класса: синхронно у всех групп класса */
.tgroup.has-class {
  border-color: color-mix(in srgb, var(--cls-color) 45%, var(--border));
  background: var(--cls-bg, var(--sidebar));
}
.tgroup.has-class:hover {
  border-color: var(--cls-color);
}

/* Категория группы — цветной ярлык сверху карточки */
.tcat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 20px;
  margin-bottom: 8px;
  border: 1px solid var(--cat-color, var(--border));
  color: var(--cat-color, var(--muted));
  background: color-mix(in srgb, var(--cat-color, var(--border)) 14%, transparent);
  cursor: text;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tcat .cat-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--cat-color, var(--muted));
  flex-shrink: 0;
}
.tcat.empty {
  border-style: dashed;
  color: var(--dim);
  background: transparent;
  opacity: 0;
  transition: opacity .12s;
}
.tgroup:hover .tcat.empty,
.tcat.empty:focus-within { opacity: 1; }
.tcat .rename-input { min-width: 80px; }

/* Превью усреднённых кривых на карточке группы */
.tgroup-preview {
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--border);
  min-height: 96px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color .12s;
}
.tgroup-preview:hover { border-color: var(--accent); }
.tgroup-preview img { display: block; width: 100%; height: auto; }
.tgroup-preview .tp-load,
.tgroup-preview .tp-empty { padding: 16px 12px; text-align: center; }
.tgroup-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--muted);
  flex-wrap: wrap;
}
.tgroup-info .gi-item {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 1px 8px; border-radius: 10px;
  border: 1px solid var(--border);
}
/* Группа без сводки: карточка сжимается до заголовка и счётчика */
.tgroup.plain .tgroup-info { margin-top: 10px; cursor: pointer; }
.tgroup-info .gi-sub {
  color: var(--gi-color, var(--muted));
  border-color: color-mix(in srgb, var(--gi-color, var(--border)) 40%, var(--border));
}
.tgroup-info .gi-total { margin-left: auto; color: var(--dim); }

/* ── Детальный экран группы: вкладки, сводка, подгруппы ─────────────────── */
.group-tabs {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 10px 22px 0; border-bottom: 1px solid var(--border);
}

/* Настройки группы: имя, класс, сводка, подгруппы — по шестерёнке */
#group-settings {
  display: flex; align-items: center; gap: 22px; flex-wrap: wrap;
  padding: 14px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.gs-field { display: flex; align-items: center; gap: 8px; }
.gs-field > label {
  font-size: 11.5px; color: var(--muted); text-transform: lowercase;
}
.gs-field input, .gs-field select {
  background: var(--input); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 5px 9px; font-size: 13px; font-family: inherit;
  outline: none; min-width: 150px;
}
.gs-field input:focus, .gs-field select:focus { border-color: var(--accent); }
.gs-dot { width: 10px; height: 10px; border-radius: 50%; }

/* Переключатель сводки */
.gs-switch {
  width: 38px; height: 21px; border-radius: 11px; padding: 2px;
  background: var(--input); border: 1px solid var(--border);
  cursor: pointer; display: flex; align-items: center;
  transition: background .12s, border-color .12s;
}
.gs-switch .gs-knob {
  width: 15px; height: 15px; border-radius: 50%;
  background: var(--dim); transition: transform .12s, background .12s;
}
.gs-switch.on { background: var(--accent); border-color: var(--accent); }
.gs-switch.on .gs-knob { transform: translateX(17px); background: #fff; }

.gs-subs { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.gs-sub {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 6px 3px 10px; border-radius: 11px;
  border: 1px solid var(--border); font-size: 12px;
  border-left: 3px solid var(--gi-color, var(--border));
}
.gs-sub-del, .gs-add {
  display: inline-flex; align-items: center; gap: 4px;
  background: none; border: none; color: var(--muted);
  cursor: pointer; border-radius: 8px; padding: 3px 7px; font-size: 12px;
}
.gs-sub-del svg, .gs-add svg { width: 13px; height: 13px; }
.gs-sub-del:hover { color: var(--red); }
.gs-add { border: 1px dashed var(--border); }
.gs-add:hover { color: var(--text); border-color: var(--border-lt); }
#lb-note { padding: 24px; color: var(--muted); }
.gtab {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border: none; background: transparent;
  color: var(--muted); font-size: 13px; font-weight: 600;
  border-bottom: 2px solid transparent; cursor: pointer;
  border-radius: 8px 8px 0 0; transition: color .1s, background .1s;
}
.gtab::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: var(--gt-color); display: none;
}
.gtab[style*="--gt-color"]::before { display: inline-block; }
.gtab:hover { background: var(--hover); }
.gtab.active { color: var(--fg); border-bottom-color: var(--gt-color, var(--accent)); }

.group-content {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 20px 24px; display: flex; flex-direction: column; gap: 16px;
}
/* Сводка: график слева (шире), чат/вывод справа (уже ~30%) */
.gc-summary {
  display: flex; align-items: stretch; gap: 18px;
  flex: 1; min-height: 0;
}
.gsum-graph {
  flex: 1 1 68%; min-width: 0;
  display: flex; align-items: flex-start; justify-content: center;
}
.gsum-side {
  flex: 0 0 30%; min-width: 280px; max-width: 420px;
  display: flex; flex-direction: column; min-height: 0;
  border: 1px solid var(--border); border-radius: 14px;
  background: var(--sidebar); overflow: hidden;
}
.gsum-msgs { flex: 1; min-height: 60px; overflow-y: auto; padding: 10px 12px; }
.gsum-composer { padding: 8px 12px 12px; }
.gsum-composer .composer-bottom {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
/* Иконка действий со сводкой + всплывающее меню */
.gsum-actions { position: relative; }
.gsum-act-btn svg { width: 17px; height: 17px; }
.gsum-menu {
  position: absolute; bottom: calc(100% + 8px); left: 0; z-index: 50;
  min-width: 210px; padding: 5px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow);
}
.gsum-menu[hidden] { display: none; }
.gsum-menu-item {
  display: block; width: 100%; text-align: left;
  padding: 8px 10px; border-radius: 8px; border: none; background: transparent;
  color: var(--fg); font-size: 13px; cursor: pointer;
}
.gsum-menu-item:hover { background: var(--hover); }
.group-graph {
  max-width: 100%; width: 100%;
  border-radius: 12px; border: 1px solid var(--border);
}
@media (max-width: 860px) {
  .gc-summary { flex-direction: column; }
  .gsum-side {
    flex-basis: auto; max-width: none; min-width: 0; min-height: 320px;
  }
}
.ph-block {
  width: 100%; max-width: 900px; margin: 0 auto;
  padding: 40px 20px; text-align: center;
  color: var(--dim); border: 1px dashed var(--border); border-radius: 12px;
}
.sg-head {
  display: flex; align-items: center; gap: 8px; max-width: 980px;
  width: 100%; margin: 0 auto;
}
.sg-head .sg-name {
  font-family: Georgia, serif; font-size: 17px; font-weight: 700;
  cursor: text;
}
.sg-head .sg-hint { margin-left: auto; }
.sg-empty { padding: 20px; text-align: center; grid-column: 1 / -1; }

/* Графики — небольшие прямоугольники подряд, перенос строк, прокрутка вниз */
.sg-grid {
  max-width: 980px; width: 100%; margin: 0 auto;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 12px;
  /* Сетка скроллится сама — чат остаётся прижат к низу экрана */
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
  align-content: start;
}
.sg-cell {
  position: relative;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--sidebar); overflow: hidden; cursor: zoom-in;
  transition: border-color .12s, transform .1s;
}
.sg-cell:hover { border-color: var(--accent); }
.sg-cell:active { transform: scale(.985); }
/* Шапка ячейки: название слева, крестик справа — по центру строки */
.sg-cell-head {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 5px 4px 9px;
}
/* Крестик удаления отдельного графика */
.sg-cell-del {
  flex-shrink: 0;
  width: 20px; height: 20px; padding: 0; border-radius: 6px;
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; opacity: 0;
  transition: opacity .12s, color .1s, background .1s;
}
.sg-cell:hover .sg-cell-del { opacity: 1; }
.sg-cell-del svg { width: 12px; height: 12px; }
.sg-cell-del:hover { color: var(--red); background: var(--danger-h); }
.sg-cell-del.armed { opacity: 1; color: #fff; background: var(--red); }
.sg-cell-title {
  flex: 1; min-width: 0;
  font-size: 12px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sg-cell-graph {
  background: var(--panel); aspect-ratio: 16 / 10;
  display: flex; align-items: center; justify-content: center;
}
.sg-cell-graph img { display: block; width: 100%; height: 100%; object-fit: cover; }

/* Лайтбокс графика на весь экран */
#graph-lightbox {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(0, 0, 0, .72);
  display: flex; align-items: center; justify-content: center; padding: 28px;
}
#graph-lightbox[hidden] { display: none; }
.lb-inner {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow);
  max-width: 1100px; width: 100%; max-height: 92vh;
  display: flex; flex-direction: column; overflow: hidden;
}
.lb-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
.lb-head .lb-title { font-weight: 600; font-family: Georgia, serif; flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-body {
  flex: 1; min-height: 0; overflow: auto; padding: 16px;
  display: flex; align-items: center; justify-content: center;
}
.lb-body img { max-width: 100%; max-height: 78vh; border-radius: 8px; }

.sg-composer {
  max-width: 980px; width: 100%;
  margin: auto auto 0;   /* прижат к низу экрана */
  border-top: 1px solid var(--border); padding-top: 14px;
  flex-shrink: 0;
}
.sg-composer .sg-chips {
  display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px;
}
.sg-composer .sg-chips[hidden] { display: none; }
/* Модель думает / отвечает — над полем ввода подгруппы */
.sg-status {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  margin-bottom: 9px;
  padding: 9px 12px;
  background: var(--sidebar);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
}
.sg-status[hidden] { display: none; }
.sg-status svg { width: 15px; height: 15px; flex: 0 0 auto; color: var(--accent); margin-top: 2px; }
.sg-status.err { border-color: var(--red); }
.sg-status.err svg { color: var(--red); }
.sg-status.thinking { align-items: center; }
.sg-status .dots { display: flex; gap: 5px; }
.sg-status .dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--dim); animation: blink 1.2s infinite;
}
.sg-status .dots span:nth-child(2) { animation-delay: .2s; }
.sg-status .dots span:nth-child(3) { animation-delay: .4s; }
.sg-status-text { flex: 1; min-width: 0; }
.sg-composer .chip-x {
  background: none; border: none; color: var(--dim); cursor: pointer;
  font-size: 14px; padding: 0 0 0 4px;
}

.tgroup-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 2px 9px;
  cursor: grab;
  user-select: none;
}
.tgroup.dragging .tgroup-head { cursor: grabbing; }
.tgroup-title {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  border-radius: 6px;
  padding: 2px 6px;
  margin-left: -6px;
}
.tgroup-title:hover { background: var(--hover); color: var(--text); }

.tcharts { display: flex; flex-direction: column; gap: 8px; }

.tcard {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: border-color .12s, transform .1s, background .12s;
}
.tcard:hover {
  border-color: var(--border-lt);
  background: var(--card);
  transform: translateY(-1px);
}

.tcard .tname {
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.35;
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.tcard .tvars {
  display: block;
  margin-top: 3px;
  font-size: 11.5px;
  color: var(--accent);
  font-family: Georgia, serif;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.tcard .texpr {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  color: var(--muted);
  font-family: Consolas, monospace;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.tcard .tempty {
  display: block;
  margin-top: 3px;
  font-size: 11px;
  color: var(--dim);
}

/* Узел вывода (группы — внутри карточки, проекта — свободный на полотне) */
.tnode {
  margin-top: 12px;
  width: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 12px;
  display: flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  text-align: left;
  transition: border-color .12s, background .12s;
}
.tnode:hover { border-color: var(--accent); }
.tnode svg { width: 15px; height: 15px; color: var(--accent); }
.tnode .tn-label {
  font-family: Georgia, serif;
  font-size: 13px;
  font-weight: 700;
  flex: 1;
}
.tnode .tn-state { font-size: 11px; color: var(--dim); }
.tnode.ready .tn-state { color: var(--green); }

#concl-node {
  position: absolute;
  width: 280px;
  margin-top: 0;
  padding: 13px 16px;
  border-width: 1.5px;
  border-radius: 14px;
  background: var(--sidebar);
}
#concl-node .tn-label { font-size: 15px; cursor: grab; }
#concl-node.dragging {
  z-index: 20;
  box-shadow: var(--shadow);
  border-color: var(--accent);
}

/* Узел вывода (класса, групп, связи). Не карточка, как у группы, а
   светящаяся капсула своего цвета — вывод с группой не спутать */
.tderived {
  position: absolute;
  width: 280px;
  margin-top: 0;
  padding: 10px 16px 10px 10px;
  gap: 11px;
  border: 1.5px solid color-mix(in srgb, var(--concl) 70%, transparent);
  border-radius: 26px;
  background:
    linear-gradient(120deg,
      color-mix(in srgb, var(--concl) 20%, var(--sidebar)) 0%,
      var(--sidebar) 75%);
  box-shadow:
    0 0 0 4px color-mix(in srgb, var(--concl) 10%, transparent),
    0 10px 28px rgba(0, 0, 0, .28);
  transition: border-color .15s, box-shadow .15s;
}
.tderived:hover {
  border-color: var(--concl);
  box-shadow:
    0 0 0 6px color-mix(in srgb, var(--concl) 16%, transparent),
    0 12px 32px rgba(0, 0, 0, .32);
}
.tderived .tn-badge {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--concl);
  box-shadow: 0 0 14px color-mix(in srgb, var(--concl) 55%, transparent);
}
.tderived .tn-badge svg { width: 16px; height: 16px; color: var(--sidebar); }
.tderived .tn-label { font-size: 14px; line-height: 1.3; cursor: grab; }
.tderived .tn-state {
  flex: none;
  padding: 2px 8px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--concl) 14%, transparent);
  color: var(--concl);
}
.tderived.ready .tn-state { color: var(--concl); }
.tderived.dragging {
  z-index: 20;
  box-shadow: var(--shadow);
  border-color: var(--concl);
}

.tree-empty {
  position: absolute;
  left: 50%; top: 42%;
  transform: translate(-50%, -50%);
  color: var(--dim);
  text-align: center;
  line-height: 1.7;
  pointer-events: none;
}

/* Режим создания связи */
body.linkmode #tree-canvas { cursor: crosshair; }
body.linkmode .tgroup:hover,
body.linkmode #concl-node:hover,
body.linkmode .tderived:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(217, 119, 87, .35);
}
.link-src {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 2px rgba(217, 119, 87, .35);
}

#link-hint {
  position: absolute;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--card);
  border: 1px solid var(--accent);
  border-radius: 20px;
  padding: 7px 18px;
  font-size: 12.5px;
  z-index: 30;
  box-shadow: var(--shadow);
}

/* Контекстное меню */
#ctx-menu {
  position: fixed;
  z-index: 90;
  min-width: 200px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 5px;
  box-shadow: var(--shadow);
  animation: pop .12s ease-out;
}
#ctx-menu .ctx-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 11px;
  border-radius: 8px;
  font-size: 13px;
  text-align: left;
  color: var(--text);
  transition: background .1s;
}
#ctx-menu .ctx-item:hover { background: var(--hover); }
#ctx-menu .ctx-item svg { width: 15px; height: 15px; color: var(--muted); }
#ctx-menu .ctx-item.danger { color: var(--red); }
#ctx-menu .ctx-item.danger svg { color: var(--red); }
#ctx-menu .ctx-sep {
  height: 1px;
  background: var(--border);
  margin: 5px 8px;
}

/* Инлайн-переименование */
.rename-input {
  font: inherit;
  color: var(--text);
  background: var(--input);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 1px 6px;
  outline: none;
  width: 100%;
  min-width: 60px;
}

/* ── Верхняя панель (график / вывод) ─────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
}

.crumbs {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 7px;
  white-space: nowrap;
  overflow: hidden;
}
.crumbs .c-dim { color: var(--dim); font-size: 12.5px; flex-shrink: 1;
  overflow: hidden; text-overflow: ellipsis; }
.crumbs .c-sep { color: var(--dim); font-size: 12px; }
.crumbs .c-cur {
  font-family: Georgia, serif;
  font-size: 15px;
  font-weight: 700;
  overflow: hidden; text-overflow: ellipsis;
  border-radius: 6px;
  padding: 1px 6px;
  margin-left: -6px;
}
.crumbs .c-cur:hover { background: var(--hover); }

.topbar-right { display: flex; align-items: center; gap: 8px; }

.rate-group { display: flex; gap: 2px; }
.rate-group button {
  padding: 4px 9px;
  border-radius: 8px;
  font-size: 11.5px;
  color: var(--dim);
  border: 1px solid transparent;
  transition: background .12s, color .12s;
}
.rate-group button:hover { background: var(--hover); color: var(--text); }
.rate-group button.picked {
  background: var(--card);
  border-color: var(--border-lt);
  color: var(--text);
}

/* ── Полотно графика ──────────────────────────────────────────────────────── */

#graph-body {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  min-height: 0;
  overflow: hidden;
  position: relative;   /* якорь для подсказки к 3D-сцене */
  /* График не растягивается на весь экран: на широком мониторе это
     полотно обоев, а не рисунок. Держим его в размерах иллюстрации. */
  --graph-max-w: 920px;
  --graph-max-h: 580px;
}

#graph-img {
  max-width: min(100%, var(--graph-max-w));
  max-height: min(100%, var(--graph-max-h));
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
}

/* Интерактивная 3D-сцена занимает всё поле графика.
   Абсолютные размеры, а не проценты: plotly меряет контейнер при
   создании и без определённой высоты уходит в дефолтные 700×450. */
#graph-plotly {
  position: absolute;
  /* по бокам — те же рамки, что у картинки: сцена не расползается
     на всю ширину монитора */
  inset: 16px max(16px, calc((100% - var(--graph-max-w)) / 2));
  border-radius: 12px;
  overflow: hidden;
}
#graph-plotly[hidden] { display: none; }
/* Управление анимацией по четвёртой переменной */
#scene-controls {
  position: absolute;
  /* панель по ширине сцены, а не всего экрана */
  left: max(16px, calc((100% - var(--graph-max-w)) / 2));
  right: max(16px, calc((100% - var(--graph-max-w)) / 2));
  bottom: 10px;
  height: 40px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  z-index: 3;
}
#scene-controls[hidden] { display: none; }
/* У статичной сцены в панели остаётся одна шестерёнка — тянуть её
   через всё поле незачем, прижимаем к правому краю по содержимому */
#scene-controls.only-gear { left: auto; padding: 0 6px; }
#scene-controls .icon-btn svg { width: 16px; height: 16px; }
/* Значение переменной и шаг — прямо в панели */
.anim-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  background: var(--input);
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: 12px;
  color: var(--text);
}
.anim-eq, .anim-cap { color: var(--muted); font-size: 11px; }
#anim-var { font-weight: 600; }
#scene-controls .spacer { flex: 1; }
/* «Все сразу» — показать все состояния слоями вместо хода по одному */
.anim-toggle {
  padding: 4px 10px; border-radius: 9px; font-size: 12px;
  border: 1px solid var(--border); background: var(--input);
  color: var(--muted); cursor: pointer; white-space: nowrap;
}
.anim-toggle:hover { color: var(--text); border-color: var(--border-lt); }
.anim-toggle.on {
  background: var(--accent); border-color: var(--accent);
  color: var(--accent-tx);
}
#scene-controls .dimmed { opacity: .4; pointer-events: none; }
/* Текущее значение в панели — надпись, не поле */
#anim-value-txt {
  min-width: 48px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
/* Числовые поля настроек — без стрелок-спиннеров */
#anim-settings input[type="number"]::-webkit-outer-spin-button,
#anim-settings input[type="number"]::-webkit-inner-spin-button {
  appearance: none; margin: 0;
}
#anim-settings input[type="number"] { -moz-appearance: textfield; }

/* Чем и как меняется четвёртая переменная */
#anim-settings {
  position: absolute;
  right: 16px;
  bottom: 58px;
  z-index: 4;
  width: 340px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  /* оба раздела открыты — панель выше окна; пусть листается сама */
  max-height: calc(100% - 74px);
  overflow-y: auto;
}
#anim-settings[hidden] { display: none; }
#anim-settings label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 11px;
  color: var(--muted);
}
#anim-settings label.wide { grid-column: 1 / -1; }
/* Разделы: анимация и само пространство. Заголовок — кнопка: по нему
   раздел сворачивается, а поля раздела стоят в общей сетке панели */
.as-sec {
  grid-column: 1 / -1;
  display: flex; align-items: center; justify-content: space-between;
  width: 100%;
  font: inherit;
  font-size: 11px; letter-spacing: .04em; text-transform: uppercase;
  color: var(--dim); margin-top: 4px;
  padding: 0 0 4px; border: 0; border-bottom: 1px solid var(--border);
  background: none; cursor: pointer; text-align: left;
}
.as-sec:hover { color: var(--muted); }
.as-sec svg {
  width: 14px; height: 14px; flex: none;
  transition: transform .15s ease;
}
.as-sec.collapsed svg { transform: rotate(-90deg); }
.as-sec:first-child { margin-top: 0; }
.as-body { display: contents; }
.as-var { color: var(--text); font-weight: 600; }
/* Имя величины и «от»/«до» — одной строкой */
.as-cap { white-space: nowrap; }
/* Строка с тумблером: подпись слева, переключатель справа */
#anim-settings label.as-row {
  flex-direction: row; align-items: center;
  justify-content: space-between; gap: 10px;
}
#anim-settings input,
#anim-settings select {
  background: var(--input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 8px;
  color: var(--text);
  font-size: 12px;
  outline: none;
}
#anim-settings input:focus,
#anim-settings select:focus { border-color: var(--accent); }
#anim-settings .accent-btn { grid-column: 1 / -1; justify-content: center; }

/* Чат не должен налезать на панель анимации: поднимаем его над ней.
   С настройками переменной чат не соседствует — они открываются
   по очереди (на узком окне рядом им не поместиться). */
#view-chart.has-anim #chart-chat,
#view-chart.has-anim #chart-chat-launcher { bottom: 66px; }

/* Сценой управляет мышь, поэтому панель инструментов plotly не нужна */
#graph-plotly .modebar { display: none !important; }
#graph-plotly { user-select: none; }

#graph-placeholder, #concl-placeholder { text-align: center; color: var(--dim); }
.ph-icon {
  width: 46px; height: 46px;
  color: var(--border-lt);
  margin-bottom: 12px;
}
#graph-placeholder p, #concl-placeholder p {
  font-family: Georgia, serif;
  font-size: 16px;
  color: var(--muted);
  margin-bottom: 6px;
}
#graph-placeholder .caption, #concl-placeholder .caption {
  display: block; max-width: 380px; margin: 0 auto;
}

/* ── Вывод: документ ──────────────────────────────────────────────────────── */

#concl-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 18px;
  padding: 26px 24px;
}
#concl-body:has(#concl-placeholder:not([hidden])) { justify-content: center; }

.concl-doc {
  max-width: 720px;
  width: 100%;
  font-family: Georgia, serif;
  font-size: 16px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
}

.concl-graph {
  max-width: 720px;
  width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border);
}

/* ── Нижний чат ───────────────────────────────────────────────────────────── */

.bottom-chat {
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  max-height: 44%;
  flex-shrink: 0;
}

.bottom-chat .messages,
.floating-chat .messages {
  overflow-y: auto;
  padding: 14px 18px 4px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.bottom-chat .messages:empty,
.floating-chat .messages:empty { padding: 4px 0 0; }

/* ── Плавающий командный чат (правый нижний угол) ─────────────────────────── */

#tree-chat-launcher,
#chart-chat-launcher {
  position: absolute;
  right: 18px;
  bottom: 16px;
  z-index: 28;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px 10px 13px;
  border-radius: 22px;
  background: var(--accent);
  color: var(--accent-tx);
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow);
  transition: background .12s, transform .08s;
}
#tree-chat-launcher:hover,
#chart-chat-launcher:hover { background: var(--accent-h); }
#tree-chat-launcher:active,
#chart-chat-launcher:active { transform: scale(.96); }
#tree-chat-launcher svg,
#chart-chat-launcher svg { width: 16px; height: 16px; }
#tree-chat-launcher[hidden],
#chart-chat-launcher[hidden] { display: none; }

.floating-chat {
  position: absolute;
  right: 18px;
  bottom: 16px;
  z-index: 28;
  width: 380px;
  max-width: calc(100% - 36px);
  max-height: 60%;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  animation: rise .16s ease-out;
  overflow: hidden;
}
.floating-chat[hidden] { display: none; }

.fc-head {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 11px 10px 11px 14px;
  border-bottom: 1px solid var(--border);
}
.fc-icon { width: 16px; height: 16px; color: var(--accent); }
.fc-title {
  flex: 1;
  font-family: Georgia, serif;
  font-size: 14px;
  font-weight: 700;
}
.floating-chat .messages { max-height: 46vh; }
.floating-chat .composer { padding: 8px 12px 12px; }

.composer { padding: 10px 18px 14px; position: relative; }

/* Автоподсказки классов/объектов в командном вводе.
   position: fixed + координаты из JS — чтобы не обрезалось overflow:hidden
   плавающего чата. */
.cmd-suggest {
  position: fixed;
  max-height: 46vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 5px;
  z-index: 60;
}
.cmd-suggest[hidden] { display: none; }
.cs-head {
  font-size: 10.5px;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 5px 8px 3px;
}
.cs-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.cs-item:hover, .cs-item.active { background: var(--hover); }
.cs-item .cs-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--cs-color, var(--dim)); flex-shrink: 0;
}
.cs-item .cs-name { flex: 1; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cs-item .cs-kind {
  font-size: 10.5px; color: var(--dim);
  border: 1px solid var(--border); border-radius: 6px; padding: 1px 6px;
}
.cs-item .cs-hint { font-size: 11px; color: var(--dim); }

/* Имя объекта/класса, упомянутого в сообщении чата — в цвете с полотна */
.mention { color: var(--m-color, var(--accent)); font-weight: 600; }

/* Формулы: LaTeX-подмножество → HTML, набор «как в Word» */
.math {
  font-family: "Cambria Math", "Times New Roman", Georgia, serif;
  white-space: nowrap;
}
.math i { font-style: italic; font-family: inherit; }
.math .mup { font-style: normal; }
.math sup, .math sub { font-size: 0.72em; }
.math-block {
  display: block; text-align: center; font-size: 1.15em; margin: 10px 0;
}
.math .frac {
  display: inline-flex; flex-direction: column; align-items: center;
  vertical-align: middle; margin: 0 0.18em; text-align: center;
}
.math .frac .fnum { border-bottom: 1px solid currentColor; padding: 0 0.35em; }
.math .frac .fden { padding: 0 0.35em; }
.math .sqrt { display: inline-flex; align-items: flex-start; }
.math .sqrt::before { content: "√"; }
.math .sqrt .rad { border-top: 1px solid currentColor; padding: 0 0.15em; }

/* Богатый текст вывода: заголовки, списки, разделители */
.rich-h { font-weight: 700; font-family: Georgia, serif; line-height: 1.35;
  margin: 12px 0 4px; }
.rich-h1 { font-size: 1.35em; }
.rich-h2 { font-size: 1.22em; }
.rich-h3 { font-size: 1.1em; }
.rich-h4 { font-size: 1.02em; color: var(--muted); }
.rich-p { margin: 3px 0; line-height: 1.6; }
.rich-ul { margin: 4px 0 8px; padding-left: 22px; }
.rich-li { margin: 3px 0; line-height: 1.55; }
.rich-li::marker { color: var(--accent); }
.rich-hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.rich-gap { height: 7px; }
.rich-p strong, .rich-li strong, .rich-h strong { font-weight: 700; }
.rich-p em, .rich-li em { font-style: italic; }

/* Кликабельный чип класса на карточке группы (вставка в чат) */
.tcat.clickable { cursor: pointer; }
.tcat.clickable:hover {
  background: color-mix(in srgb, var(--cat-color, var(--border)) 26%, transparent);
}

.msg { max-width: 100%; animation: rise .18s ease-out; }

@keyframes rise {
  from { opacity: 0; transform: translateY(5px); }
  to   { opacity: 1; transform: none; }
}

.msg-user {
  align-self: flex-end;
  max-width: 82%;
  background: var(--bubble-user);
  border-radius: 14px 14px 4px 14px;
  padding: 8px 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 13.5px;
}

.msg-user .u-attach {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin-bottom: 6px;
}
.msg-user .u-attach img {
  max-width: 130px; max-height: 100px;
  border-radius: 9px; display: block;
}
.msg-user .u-attach .file-chip {
  display: flex; align-items: center; gap: 6px;
  background: var(--card);
  border-radius: 8px;
  padding: 4px 9px;
  font-size: 12px;
  color: var(--muted);
}
.msg-user .u-attach .file-chip svg { width: 13px; height: 13px; }

.msg-ai { display: flex; gap: 10px; }

.ai-mark {
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
}
.ai-mark svg { width: 12px; height: 12px; }

.msg-ai .body {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 14.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  padding-top: 2px;
  min-width: 0;
}

/* Расход обращения к модели — приглушённая строка под ответом */
.msg-cost {
  margin-top: 6px;
  font-family: "Segoe UI", system-ui, sans-serif;
  font-size: 11.5px;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.msg-note {
  align-self: stretch;
  margin-left: 32px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border-left: 3px solid var(--border-lt);
  border-radius: 8px;
  padding: 7px 11px;
  line-height: 1.5;
  white-space: pre-wrap;
}
.msg-note.ok  { border-left-color: var(--green); }
.msg-note.err { border-left-color: var(--red); }

.rate-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: -4px 0 0 32px;
}
.rate-row .q { font-size: 12px; color: var(--dim); margin-right: 2px; }
.rate-row button {
  width: 26px; height: 26px;
  border-radius: 8px;
  color: var(--dim);
  display: flex; align-items: center; justify-content: center;
  transition: background .12s, color .12s;
}
.rate-row button svg { width: 14px; height: 14px; }
.rate-row button:hover { background: var(--hover); color: var(--text); }
.rate-row button.chosen-up   { color: var(--green); }
.rate-row button.chosen-down { color: var(--red); }
.rate-row button:disabled { pointer-events: none; opacity: .9; }
.rate-row .rate-more {
  width: auto; height: 26px;
  padding: 0 9px;
  font-size: 12px;
  color: var(--dim);
}
.rate-row .rate-more:hover { background: var(--hover); color: var(--text); }

/* Комментарий к оценке: чем именно ответ не подошёл */
.rate-note {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin: 4px 0 6px 32px;
}
.rate-note textarea {
  flex: 1;
  max-width: 420px;
  padding: 7px 10px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--input);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  resize: vertical;
}
.rate-note textarea:focus { outline: none; border-color: var(--accent); }
.rate-note .rate-save {
  height: 32px;
  padding: 0 12px;
  border-radius: 9px;
  font-size: 12px;
  color: var(--dim);
  background: var(--hover);
}
.rate-note .rate-save:hover { color: var(--text); }
.rate-note .rate-save:disabled { opacity: .6; }

.typing { display: flex; gap: 10px; align-items: center; }
.typing .dots { display: flex; gap: 5px; padding: 6px 2px; }
.typing .dots span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--dim);
  animation: blink 1.2s infinite;
}
.typing .dots span:nth-child(2) { animation-delay: .2s; }
.typing .dots span:nth-child(3) { animation-delay: .4s; }

@keyframes blink {
  0%, 60%, 100% { opacity: .25; transform: none; }
  30% { opacity: 1; transform: translateY(-2px); }
}

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

#chips {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin-bottom: 8px;
}
#chips:empty { display: none; }

.chip {
  display: flex; align-items: center; gap: 7px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px 6px 4px 10px;
  font-size: 12px;
  max-width: 220px;
}
.chip img {
  width: 26px; height: 26px;
  object-fit: cover; border-radius: 6px;
}
.chip .name {
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.chip button {
  width: 20px; height: 20px;
  border-radius: 6px;
  color: var(--dim);
  display: flex; align-items: center; justify-content: center;
}
.chip button svg { width: 12px; height: 12px; }
.chip button:hover { background: var(--danger-h); color: var(--red); }

.composer-box {
  background: var(--input);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 10px 12px 7px;
  transition: border-color .15s, box-shadow .15s;
}
.composer-box:focus-within {
  border-color: var(--border-lt);
  box-shadow: 0 4px 18px rgba(0, 0, 0, .12);
}

.composer-box textarea {
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  max-height: 160px;
  padding: 0 4px;
}
.composer-box textarea::placeholder { color: var(--dim); }

.composer-bottom {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
}
.composer-bottom .hint {
  flex: 1;
  font-size: 11px;
  color: var(--dim);
  text-align: right;
}

.icon-btn {
  width: 32px; height: 32px;
  border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  color: var(--muted);
  transition: background .12s, color .12s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn svg { width: 17px; height: 17px; }

.send-btn {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-tx);
  display: flex; align-items: center; justify-content: center;
  transition: background .12s, opacity .12s, transform .08s;
  flex-shrink: 0;
}
.send-btn svg { width: 15px; height: 15px; }
.send-btn:hover:not(:disabled) { background: var(--accent-h); }
.send-btn:active:not(:disabled) { transform: scale(.94); }
.send-btn:disabled { opacity: .35; cursor: default; }

.tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 13px;
  border-radius: 9px;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 12.5px;
  transition: background .12s, border-color .12s;
  white-space: nowrap;
}
.tool-btn svg { width: 14px; height: 14px; }
.tool-btn:hover:not(:disabled) { background: var(--hover); border-color: var(--border-lt); }
.tool-btn:disabled { opacity: .45; cursor: default; }

.accent-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 20px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-tx);
  font-weight: 600;
  transition: background .12s, opacity .12s;
}
.accent-btn svg { width: 15px; height: 15px; }
.accent-btn:hover:not(:disabled) { background: var(--accent-h); }
.accent-btn:disabled { opacity: .5; cursor: default; }
.accent-btn.sm { padding: 7px 14px; font-size: 12.5px; border-radius: 9px; }

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

#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: fade .15s ease-out;
}
#modal-overlay[hidden] { display: none; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

#settings-modal {
  width: min(560px, calc(100vw - 48px));
  max-height: calc(100vh - 64px);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: pop .18s ease-out;
}

@keyframes pop {
  from { opacity: 0; transform: scale(.97) translateY(6px); }
  to   { opacity: 1; transform: none; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 10px;
}

.settings-tabs {
  display: flex;
  gap: 4px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
}
.stab {
  padding: 8px 15px;
  border-radius: 9px 9px 0 0;
  color: var(--muted);
  font-size: 13px;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color .12s, border-color .12s;
}
.stab:hover { color: var(--text); }
.stab.active {
  color: var(--text);
  font-weight: 600;
  border-bottom-color: var(--accent);
}


.modal-title {
  font-family: Georgia, serif;
  font-size: 17px;
  font-weight: 700;
}

.modal-body { flex: 1; display: flex; min-height: 0; }

.modal-content {
  flex: 1;
  overflow-y: auto;
  padding: 18px 22px;
  min-width: 0;
}

#block-local, #block-external { margin-top: 4px; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

.field { margin-bottom: 14px; }
.field > label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
}
.field .caption { display: block; margin-top: 6px; }

input, select {
  width: 100%;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--input);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  outline: none;
  transition: border-color .12s;
}
input:focus, select:focus { border-color: var(--border-lt); }

.row { display: flex; gap: 8px; align-items: center; }
.row > input, .row > select { flex: 1; }

hr.sep { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

/* Нейросеть для каждой задачи: своя AI Graff или платформа */
.role-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px 12px;
  margin-bottom: 12px;
}
.role-row .role-name { font-size: 13px; }
.role-row .role-model { grid-column: 1 / -1; }

.segment {
  display: inline-flex;
  background: var(--input);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 3px;
  gap: 2px;
}
.segment button {
  padding: 6px 16px;
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--muted);
  transition: background .12s, color .12s;
}
.segment button:hover { color: var(--text); }
.segment button.on {
  background: var(--card);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .15);
}

.switch-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  padding: 7px 0;
  cursor: pointer;
  user-select: none;
}
.switch-row input { display: none; }
.switch {
  width: 36px; height: 20px;
  border-radius: 10px;
  background: var(--border);
  position: relative;
  transition: background .15s;
  flex-shrink: 0;
}
.switch::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--bg);
  transition: transform .15s;
}
.switch-row input:checked + .switch { background: var(--accent); }
.switch-row input:checked + .switch::after { transform: translateX(16px); }

.key-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--card);
  border-radius: 10px;
  padding: 7px 10px;
  margin-bottom: 6px;
  font-size: 13px;
}
.key-row .radio {
  width: 15px; height: 15px;
  border-radius: 50%;
  border: 2px solid var(--dim);
  flex-shrink: 0;
  cursor: pointer;
}
.key-row .radio.on {
  border-color: var(--green);
  background: radial-gradient(circle, var(--green) 45%, transparent 50%);
}
.key-row .kname { font-weight: 600; }
.key-row .kmask { color: var(--dim); flex: 1; }
.key-row .kdel {
  width: 24px; height: 24px;
  border-radius: 7px;
  color: var(--dim);
  display: flex; align-items: center; justify-content: center;
}
.key-row .kdel svg { width: 13px; height: 13px; }
.key-row .kdel:hover { background: var(--danger-h); color: var(--red); }

/* ── Тост ─────────────────────────────────────────────────────────────────── */

#toast {
  position: fixed;
  bottom: 22px;
  right: 22px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 18px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 100;
  animation: rise .2s ease-out;
  max-width: 380px;
}
#toast.err { border-color: var(--red); }

/* ── Живой стриминг ответа модели ────────────────────────────────────────── */

.stream-body {
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-ai.streaming .stream-body::after {
  content: "▍";
  opacity: .55;
  animation: caret-blink 1s steps(1) infinite;
}
@keyframes caret-blink { 50% { opacity: 0; } }

/* ── Тост с кнопкой «Отменить» (undo удаления) ───────────────────────────── */

.toast-undo {
  margin-left: 12px;
  padding: 3px 10px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: transparent;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}
.toast-undo:hover { background: var(--accent); color: #fff; }

/* ── Onboarding: «Подключи ИИ» ───────────────────────────────────────────── */

#onboard {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--bg) 65%, transparent);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 90;
}
#onboard[hidden] { display: none; }
.ob-card {
  width: min(430px, 92vw);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 30px 28px 20px;
  text-align: center;
}
.ob-card .ph-icon { width: 34px; height: 34px; color: var(--accent); }
.ob-card h2 { margin: 10px 0 8px; font-size: 19px; }
.ob-card p { margin-bottom: 18px; line-height: 1.5; }
.ob-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-bottom: 10px;
}
.ob-later {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  padding: 6px 10px;
}
.ob-later:hover { color: var(--text); }

/* Модель думает, а чат свёрнут: иконка на кнопке-лаунчере пульсирует */
#tree-chat-launcher.busy svg {
  animation: spark-pulse 1.1s ease-in-out infinite;
}
@keyframes spark-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50%      { transform: scale(1.3); opacity: .55; }
}


/* ── Телефон ──────────────────────────────────────────────────────────────
   Бета открывается в браузере, и часть людей заходит с телефона. Шторка
   проектов и диалоги и так всплывают поверх, а вот что ломалось:
   плашки с жёсткой шириной уезжали за правый край, низ страницы прятался
   под адресной строкой браузера, и в поля было не попасть пальцем.      */
@media (max-width: 640px) {

  /* Адресная строка браузера то появляется, то исчезает, и 100vh больше
     видимой части: поле ввода внизу уезжало под неё */
  #app { height: 100dvh; }

  /* Плашка классов висела в 56px от левого края и была шириной 340px —
     на экране в 375 правый край оказывался за границей */
  #class-panel {
    left: 8px; right: 8px; width: auto; max-height: 70vh;
  }

  /* То же самое, только от правого края */
  #anim-settings {
    left: 8px; right: 8px; width: auto; bottom: 64px;
  }

  /* Чат — главное, чем пользуются: на телефоне ему вся ширина */
  .floating-chat {
    left: 8px; right: 8px; width: auto; max-width: none;
    bottom: 8px; max-height: 76%;
  }

  /* Палец — не курсор: в кнопку 32px он попадает через раз */
  .icon-btn { width: 40px; height: 40px; }
  .tool-btn, .accent-btn, .proj-add { min-height: 40px; }

  /* Шрифт мельче 16px заставляет браузер приближать страницу при наборе,
     и обратно она сама уже не отъезжает */
  input, textarea, select { font-size: 16px; }

  /* Диалог во весь экран: обрезанный по бокам он выглядит сломанным, а
     прокрутка внутри узкой колонки неудобна */
  #settings-modal {
    width: 100vw; max-width: none; height: 100dvh; max-height: none;
    border-radius: 0; border: none;
  }
  .modal-content { padding: 16px; }
  .modal-header { padding: 14px 16px 8px; }
  .settings-tabs { overflow-x: auto; padding: 0 10px; }
  .stab { white-space: nowrap; }

  /* Графики в один столбец: две колонки по 170px — это картинки,
     на которых ничего не разобрать */
  .sg-grid { grid-template-columns: 1fr; }

  /* ── Экран графика ──────────────────────────────────────────────────
     Чат был шторкой поверх картинки, а кнопка «Чат» — кружком в углу:
     на телефоне этот угол занимают то панель сцены, то системная
     полоса, и кнопка пропадала с глаз. Ставим обоих в поток: сверху
     график, снизу чат — ничто ничего не закрывает.                  */
  /* Низ чата не вылезает за экран: лишние пиксели отрезаем здесь,
     а не вычитаем из высоты — иначе она поползёт от шрифта */
  #view-chart { overflow: hidden; }
  #view-chart .floating-chat {
    position: static;
    width: auto; max-width: none;
    max-height: none; min-height: 0;
    /* сжимается, если низ не помещается: иначе кнопка отправки
       уезжает за край экрана */
    flex: 0 1 56%;
    margin: 0; border-radius: 14px 14px 0 0;
    box-shadow: none; border-bottom: none;
  }
  #chart-chat-launcher,
  #view-chart.has-anim #chart-chat-launcher {
    position: static; inset: auto;
    width: 100%; justify-content: center;
    border-radius: 0; padding: 13px;
  }

  /* В шапке три вещи на 360 точек не помещаются, и «Сохранить»
     наезжало на путь. Путь коротким и не нужен: где ты находишься,
     видно по последнему звену */
  .topbar { padding: 8px 10px; gap: 8px; }
  .crumbs .c-dim, .crumbs .c-sep { display: none; }
  .tool-btn { padding: 8px 11px; }

  #toast { max-width: calc(100vw - 24px); }
  .hero h1 { font-size: 25px; }
  .hero p { font-size: 14px; }
}

/* ── Новый проект: «+» в конце списка ───────────────────────────────────── */

.chat-item.proj-plus { color: var(--dim); }
.chat-item.proj-plus > svg { width: 16px; height: 16px; color: inherit; }
.chat-item.proj-plus:hover { color: var(--accent); }

/* ── Кнопка «?» и справка ───────────────────────────────────────────────── */

#view-empty .view-help {
  position: absolute;
  top: 14px; right: 14px;
}

#help-pop {
  position: fixed;
  z-index: 92;
  width: 330px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 14px 14px 12px;
  animation: help-in .16s ease-out;
}
#help-pop[hidden] { display: none; }
@keyframes help-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: none; }
}
.help-head {
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .3px;
  color: var(--dim);
  margin-bottom: 8px;
}
.segment.seg-support { display: flex; margin-bottom: 8px; }
.segment.seg-support button { flex: 1; padding: 6px 4px; font-size: 12.5px; }
#support-text {
  width: 100%;
  resize: vertical;
  min-height: 84px;
  max-height: 260px;
  font: inherit;
  font-size: 13px;
  line-height: 1.45;
}
.support-done { color: var(--green); font-size: 12.5px; margin-top: 6px; }
.help-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}
.help-actions .tool-btn { flex: 1; justify-content: center; }

/* ── Знакомство с приложением ───────────────────────────────────────────── */

#tour {
  position: fixed;
  inset: 0;
  z-index: 95;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  backdrop-filter: blur(4px);
}
#tour[hidden] { display: none; }

.tour-card {
  position: relative;
  width: min(560px, 100%);
  max-height: 100%;
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  animation: tour-in .35s cubic-bezier(.2, .8, .3, 1);
}
@keyframes tour-in {
  from { opacity: 0; transform: translateY(14px) scale(.97); }
  to { opacity: 1; transform: none; }
}
.tour-x { position: absolute; top: 10px; right: 10px; }

.tour-stage {
  aspect-ratio: 480 / 220;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.tour-stage svg { display: block; width: 100%; height: 100%; }

.tour-body { padding: 18px 24px 6px; min-height: 148px; }
.tour-body.enter { animation: tour-text .35s ease-out; }
@keyframes tour-text {
  from { opacity: 0; transform: translateX(10px); }
  to { opacity: 1; transform: none; }
}
.tour-count { font-size: 11.5px; color: var(--dim); letter-spacing: .3px; }
.tour-body h2 {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 20px;
  margin: 3px 0 8px;
}
.tour-body p { color: var(--muted); font-size: 13.5px; line-height: 1.6; }
.tour-body p b { color: var(--text); font-weight: 600; }
.tour-cmd {
  font-family: Consolas, "SF Mono", monospace;
  font-size: 12.5px;
  color: var(--text);
  background: var(--input);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 5px;
  white-space: nowrap;
}

.tour-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px 18px;
}
.tour-dots { flex: 1; display: flex; gap: 5px; }
.tour-dots span {
  width: 7px; height: 7px;
  border-radius: 4px;
  background: var(--border-lt);
  cursor: pointer;
  transition: width .25s, background .25s;
}
.tour-dots span.on { width: 22px; background: var(--accent); }
.tour-skip { color: var(--muted); font-size: 12.5px; padding: 6px 8px; }
.tour-skip:hover { color: var(--text); }

@media (max-width: 640px) {
  .tour-body { padding: 14px 16px 4px; min-height: 0; }
  .tour-foot { padding: 10px 16px 14px; flex-wrap: wrap; }
  .tour-dots { flex-basis: 100%; margin-bottom: 4px; }
  .tour-body h2 { font-size: 18px; }
}

/* Сцены: цвета — из темы, поэтому знакомство одинаково в обеих */
.tour-svg { animation: tour-scene linear both; }
@keyframes tour-scene {
  0% { opacity: 0; }
  4%, 94% { opacity: 1; }
  100% { opacity: 0; }
}
.tour-stage .s-grid { fill: none; stroke: var(--grid-line); }
.tour-stage .s-axis { fill: none; stroke: var(--border-lt); stroke-width: 1.5; }
.tour-stage .s-card { fill: var(--sidebar); stroke: var(--border-lt); }
.tour-stage .s-panel { fill: var(--panel); stroke: var(--border); }
.tour-stage .s-menu { fill: var(--card); stroke: var(--border-lt); }
.tour-stage .s-input { fill: var(--input); stroke: var(--border); }
.tour-stage .s-bubble { fill: var(--bubble-user); }
.tour-stage .s-cover { fill: var(--input); }
.tour-stage .s-cover-bubble { fill: var(--bubble-user); }
.tour-stage .s-active { fill: var(--active); }
.tour-stage .s-folder { fill: none; stroke: var(--accent); stroke-width: 1.4; }
.tour-stage .s-bar { fill: var(--border-lt); }
.tour-stage .s-dim { fill: var(--dim); }
.tour-stage .s-dim-s { fill: none; stroke: var(--dim); stroke-width: 1.6; stroke-linecap: round; }
.tour-stage .s-plus { fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; }
.tour-stage .s-acc { fill: var(--accent); }
.tour-stage .s-blue { fill: var(--blue); }
.tour-stage .s-gold { fill: var(--concl); }
.tour-stage .s-acc-s, .tour-stage .s-blue-s, .tour-stage .s-gold-s {
  fill: none; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round;
}
.tour-stage .s-acc-s { stroke: var(--accent); }
.tour-stage .s-blue-s { stroke: var(--blue); }
.tour-stage .s-gold-s { stroke: var(--concl); }
.tour-stage .thin { stroke-width: 1.8; }
.tour-stage .s-band-blue { fill: color-mix(in srgb, var(--blue) 15%, transparent); }
.tour-stage .s-band-acc { fill: color-mix(in srgb, var(--accent) 15%, transparent); }
.tour-stage .s-chipbox { fill: var(--panel); stroke: var(--border-lt); }
.tour-stage .s-chip-blue { fill: color-mix(in srgb, var(--blue) 16%, transparent); stroke: var(--blue); }
.tour-stage .s-chip-acc { fill: color-mix(in srgb, var(--accent) 16%, transparent); stroke: var(--accent); }
.tour-stage .s-concl {
  fill: color-mix(in srgb, var(--concl) 18%, var(--sidebar));
  stroke: var(--concl); stroke-width: 1.5;
}
.tour-stage .s-badge { fill: var(--sidebar); }
.tour-stage .s-flow {
  fill: none; stroke: #fff; stroke-width: 3.2; opacity: .85;
  stroke-dasharray: .07 2; animation: s-flow 1.6s linear infinite both;
}
@keyframes s-flow { from { stroke-dashoffset: .08; } to { stroke-dashoffset: -1.01; } }
.tour-stage .s-send { fill: none; stroke: #fff; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tour-stage .s-word { fill: #2B579A; }
.tour-stage .s-word-t { fill: #fff; font: 700 24px Georgia, serif; }
.tour-stage .s-seg {
  fill: color-mix(in srgb, var(--accent) 22%, var(--card));
  stroke: color-mix(in srgb, var(--accent) 60%, transparent);
}
.tour-stage .s-help { fill: var(--panel); stroke: var(--accent); stroke-width: 1.6; }
.tour-stage .s-help-t { fill: var(--accent); font: 700 17px "Segoe UI", system-ui, sans-serif; }
.tour-stage .s-gear { fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; }
.tour-stage .s-ok { fill: var(--green); }
.tour-stage .s-ok-t { fill: var(--green); font-size: 11px; font-weight: 600; }
.tour-stage .s-send-t { fill: #fff; font-size: 11px; font-weight: 600; }
.tour-stage .a-toast {
  animation: s-toast .5s cubic-bezier(.2, .8, .3, 1) both; transform-box: view-box;
}
@keyframes s-toast { from { opacity: 0; transform: translateX(30px); } to { opacity: 1; transform: none; } }
.tour-stage .s-tap { fill: none; stroke: var(--accent); stroke-width: 2; }
.tour-stage .s-cursor path { fill: var(--text); stroke: var(--bg); stroke-width: 1.2; }
.tour-stage .s-finger {
  fill: color-mix(in srgb, var(--text) 35%, transparent);
  stroke: var(--text); stroke-width: 1.5;
}
.tour-stage text { font-family: "Segoe UI", system-ui, sans-serif; }
.tour-stage .s-text { fill: var(--text); font-size: 11px; font-weight: 600; }
.tour-stage .s-text.big { font-size: 13px; font-family: Georgia, serif; }
.tour-stage .s-small { fill: var(--muted); font-size: 10px; }
.tour-stage .s-title { fill: var(--text); font: 700 15px Georgia, serif; }
.tour-stage .s-formula { fill: var(--concl); font: italic 700 17px Georgia, serif; }

/* Движение: всё одноразовое, исходное состояние — скрытое (fill both) */
.tour-stage .a-in, .tour-stage .a-pop, .tour-stage .a-draw, .tour-stage .a-grow,
.tour-stage .a-grow-x, .tour-stage .a-tap, .tour-stage .a-type,
.tour-stage .a-flash, .tour-stage .a-drag, .tour-stage .a-down,
.tour-stage .a-slide {
  animation-fill-mode: both;
  animation-timing-function: cubic-bezier(.2, .8, .3, 1);
  transform-box: fill-box;
}
.tour-stage .a-in { animation-name: s-in; animation-duration: .5s; }
.tour-stage .a-pop {
  animation-name: s-pop; animation-duration: .45s; transform-origin: center;
}
.tour-stage .a-draw {
  stroke-dasharray: 1 2; animation-name: s-draw; animation-duration: 1.2s;
  animation-timing-function: ease-in-out;
}
.tour-stage .a-grow {
  animation-name: s-grow; animation-duration: .6s; transform-origin: bottom;
}
.tour-stage .a-grow-x {
  animation-name: s-grow-x; animation-duration: .5s; transform-origin: left;
}
.tour-stage .a-tap {
  animation-name: s-tap; animation-duration: .6s; transform-origin: center;
  animation-timing-function: ease-out;
}
.tour-stage .a-type { animation-name: s-type; transform-origin: right; }
.tour-stage .a-flash { animation-name: s-flash; animation-duration: 1.2s; }
.tour-stage .a-drag {
  animation-name: s-drag; animation-duration: 1s; transform-box: view-box;
  animation-timing-function: ease-in-out;
}
.tour-stage .a-down { animation-name: s-down; transform-box: view-box; }
.tour-stage .a-slide { animation-name: s-slide; }
.tour-stage .a-wave { animation: s-wave 2.4s linear infinite; }
.tour-stage .a-wave.slow { animation-duration: 3.6s; }
.tour-stage .a-spin {
  animation: s-spin 4s linear infinite; transform-box: fill-box; transform-origin: center;
}
.tour-stage .bud {
  animation: s-pop .45s cubic-bezier(.2, .8, .3, 1) both,
             s-bud 1.6s ease-in-out 3.1s infinite;
  transform-box: fill-box; transform-origin: center;
}
.tour-stage .pulse > circle { animation: s-bud 1.4s ease-in-out 3.7s infinite;
  transform-box: fill-box; transform-origin: center; }
.tour-stage .t-dot { animation: s-dots 1s ease-in-out infinite; }

@keyframes s-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes s-pop { from { opacity: 0; transform: scale(.4); } to { opacity: 1; transform: scale(1); } }
@keyframes s-draw { from { stroke-dashoffset: 1.01; } to { stroke-dashoffset: 0; } }
@keyframes s-grow { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@keyframes s-grow-x { from { transform: scaleX(0); } to { transform: scaleX(1); } }
@keyframes s-tap {
  0% { opacity: 0; transform: scale(.3); }
  20% { opacity: .9; }
  100% { opacity: 0; transform: scale(1.7); }
}
@keyframes s-type { from { transform: scaleX(1); } to { transform: scaleX(0); } }
@keyframes s-flash {
  0% { opacity: 0; transform: translateY(-4px); }
  15%, 80% { opacity: 1; transform: none; }
  100% { opacity: 0; }
}
@keyframes s-drag { from { transform: none; } to { transform: translate(130px, 40px); } }
@keyframes s-down { from { transform: none; } to { transform: translateY(28px); } }
@keyframes s-slide { from { transform: none; } to { transform: translateX(67px); } }
@keyframes s-wave { from { transform: none; } to { transform: translateX(-124px); } }
@keyframes s-spin { to { transform: rotate(360deg); } }
@keyframes s-bud { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.25); } }
@keyframes s-dots { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  /* Сцена сразу в конечном виде и больше не двигается */
  .tour-stage * { animation-duration: .01s !important;
    animation-delay: 0s !important; animation-iteration-count: 1 !important; }
  .tour-svg, .tour-card, .tour-body.enter, #help-pop { animation: none; }
}

/* ── Телефон: то, что появилось после первой телефонной версии ────────── */

/* Наведения у пальца нет. Что открывалось только наведением, на
   телефоне было не достать: крестик у графика в группе и корзину у
   проекта в шторке — касание по строке сразу открывает проект */
@media (hover: none) {
  .sg-cell-del { opacity: 1; }
  .chat-item .del { display: flex; }
}

/* Палец толще курсора: всё, во что надо попасть, — от 36 точек */
@media (pointer: coarse) {
  .segment button { min-height: 36px; }
  .swatches { gap: 10px; }
  .swatch { width: 34px; height: 34px; }
  .sg-cell-del { width: 32px; height: 32px; }
  .chat-item .del { width: 34px; height: 34px; }
  .gs-add, .gs-sub-del { min-height: 34px; padding: 6px 10px; }
  .gs-switch { width: 46px; height: 28px; border-radius: 14px; }
  .gs-switch .gs-knob { width: 22px; height: 22px; }
  .gs-switch.on .gs-knob { transform: translateX(18px); }
  .tour-skip { padding: 10px 12px; }

  /* Долгое нажатие — это меню или переименование, а не выделение текста
     с системной плашкой «Копировать» поверх */
  #tree-scroll, #drawer, .sg-cell, .tname {
    -webkit-user-select: none; user-select: none;
    -webkit-touch-callout: none;
  }
  /* Полям — обратно: в Safari поле внутри user-select: none не даёт
     в себя печатать, а поля переименования живут как раз здесь */
  #tree-scroll :is(input, textarea), #drawer :is(input, textarea),
  .sg-cell :is(input, textarea) {
    -webkit-user-select: text; user-select: text;
  }

  /* Правило «полям 16px» в блоке выше перебивали более точные правила
     новых полей (чат на полотне, настройки группы) — и телефон снова
     приближал страницу при наборе. Здесь важнее, чем где-либо ещё */
  input:not([type=checkbox]):not([type=radio]):not([type=range]),
  textarea, select { font-size: 16px !important; }
}

/* Знакомство: карточки разной высоты, и по центру экрана кнопка «Далее»
   прыгала от карточки к карточке — листать большим пальцем подряд не
   выходило. Прижимаем карточку к низу, а «Далее» — к правому краю */
@media (max-width: 640px) {
  #tour {
    align-items: flex-end;
    padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  }
  .tour-skip { order: 1; }
  #tour-prev { order: 2; }
  #tour-next { order: 3; margin-left: auto; min-width: 116px; justify-content: center; }
}
