/* ==========================================================================
   Мультианализ (templates/tools/multianalysis.html,
   static/chess/multianalysis/). Подключается после main.css и
   chess/board/board.css и берёт их переменные и компоненты: доску (.cb),
   список ходов (.chess-moves), навигацию (.chess-nav), карточки.

   Раскладка - контейнерными запросами: на узком экране всё идёт столбцом,
   на широком - доска со шкалой оценки и ходами слева, вкладки справа.
   ========================================================================== */

.ma {
  container: ma / inline-size;
  --board-max: max(300px, calc(100vh - 230px));
  /* Цвета классов ходов разбора партии. */
  --c-brilliant: #2FC7A8;
  --c-great: #5B9BE6;
  --c-best: #7CC183;
  --c-excellent: #9CC45A;
  --c-good: #A9B595;
  --c-book: #B8946A;
  --c-inaccuracy: #E6B54A;
  --c-mistake: #E58F2A;
  --c-blunder: #E0544A;
  --c-forced: #8C8673;
}
@supports (height: 100svh) {
  .ma { --board-max: max(300px, calc(100svh - 230px)); }
}

.ma-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "board" "nav" "position" "moves" "side";
  gap: 12px;
}
.ma-board { grid-area: board; min-width: 0; display: flex; gap: 8px; justify-content: center; }
.ma-grid > .chess-nav { grid-area: nav; }
.ma-position { grid-area: position; }
.ma-moves { grid-area: moves; }
.ma-side { grid-area: side; min-width: 0; container: ma-side / inline-size; }

.ma-board .cb { flex: 1 1 auto; max-width: min(var(--board-max), 640px); }

@container ma (min-width: 880px) {
  .ma-grid {
    grid-template-columns: minmax(320px, min(calc(var(--board-max) + 26px), 646px)) minmax(360px, 580px);
    grid-template-rows: auto auto auto 1fr;
    grid-template-areas:
      "board side"
      "nav side"
      "position side"
      "moves side";
    justify-content: center;
    align-items: start;
    column-gap: 22px;
  }
  .ma-board .cb { max-width: none; }
  .ma-moves { max-height: 240px; }
}

@media (max-width: 640px) {
  .ma-board { margin-inline: -20px; gap: 0; }
  .ma-board .cb { border-radius: 0; box-shadow: none; }
  .ma-evalbar { border-radius: 0; }
}

/* ---------- Шкала оценки слева от доски ---------- */
.ma-evalbar {
  position: relative;
  flex: 0 0 18px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #1A1814;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}
.ma-evalbar-fill {
  position: absolute;
  inset: auto 0 0 0;
  height: 50%;
  background: #EAE3D2;
  transition: height 0.35s ease;
}
/* Доска перевёрнута - шансы белых растут сверху. */
.ma-evalbar[data-orientation="black"] .ma-evalbar-fill { inset: 0 0 auto 0; }
.ma-evalbar-text {
  position: absolute;
  left: 0;
  right: 0;
  font-family: var(--mono);
  font-size: 9px;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
}
.ma-evalbar-text[data-side="w"] { bottom: 4px; color: #1D1B16; }
.ma-evalbar-text[data-side="b"] { top: 4px; color: #EAE3D2; }
.ma-evalbar[data-orientation="black"] .ma-evalbar-text[data-side="w"] { bottom: auto; top: 4px; }
.ma-evalbar[data-orientation="black"] .ma-evalbar-text[data-side="b"] { top: auto; bottom: 4px; }
@media (prefers-reduced-motion: reduce) {
  .ma-evalbar-fill { transition: none; }
}

/* ---------- Слои поверх доски ---------- */
.cb .ma-arrows { z-index: 4; width: 100%; height: 100%; pointer-events: none; }
.cb .ma-heat { z-index: 0; pointer-events: none; }
.ma-heat-square {
  position: absolute;
  left: 0;
  top: 0;
  width: 12.5%;
  height: 12.5%;
}
.ma-heat-square.white { background: rgb(70, 140, 230); }
.ma-heat-square.black { background: rgb(225, 70, 60); }

/* ---------- Текущий ход, оценка хода, дебют ---------- */
.ma-position {
  display: grid;
  gap: 10px;
  padding: 12px 16px;
}
.ma-turn { display: flex; align-items: center; gap: 12px; min-width: 0; }
.ma-turn strong { display: block; font-size: 1.05rem; line-height: 1.3; }
.ma-turn [data-turn-text] { display: block; color: var(--muted); font-size: 0.88rem; line-height: 1.4; }
.ma-turn-mark {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border);
}
.ma-turn[data-color="w"] .ma-turn-mark { background: #F2EDE0; }
.ma-turn[data-color="b"] .ma-turn-mark { background: #111; border-color: var(--muted); }
.ma-opening { margin: 0; color: var(--hint); font-size: 0.88rem; }
.ma-opening[hidden] { display: none; }

.ma-move-class {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 8px;
  font-size: 0.9rem;
  line-height: 1.45;
}
.ma-move-class[hidden] { display: none; }
.ma-move-class strong { color: var(--class-color, var(--text)); }
.ma-move-class span:last-child { color: var(--muted); }

/* Цвет класса хода: метка, значок в списке ходов, строки итогов. */
[data-class="brilliant"] { --class-color: var(--c-brilliant); }
[data-class="great"] { --class-color: var(--c-great); }
[data-class="best"] { --class-color: var(--c-best); }
[data-class="excellent"] { --class-color: var(--c-excellent); }
[data-class="good"] { --class-color: var(--c-good); }
[data-class="book"] { --class-color: var(--c-book); }
[data-class="inaccuracy"] { --class-color: var(--c-inaccuracy); }
[data-class="mistake"] { --class-color: var(--c-mistake); }
[data-class="blunder"] { --class-color: var(--c-blunder); }
[data-class="forced"] { --class-color: var(--c-forced); }
.ma-class-mark {
  display: inline-block;
  flex: 0 0 auto;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--class-color, var(--hint));
  align-self: center;
}

/* Список ходов: значок разбора после хода. */
.ma-moves .chess-moves-list button[data-class] .ma-glyph {
  margin-left: 1px;
  color: var(--class-color);
  font-weight: 700;
}
.ma-moves .chess-moves-list button[data-class="blunder"],
.ma-moves .chess-moves-list button[data-class="mistake"],
.ma-moves .chess-moves-list button[data-class="inaccuracy"],
.ma-moves .chess-moves-list button[data-class="brilliant"],
.ma-moves .chess-moves-list button[data-class="great"] { color: var(--class-color); }
.ma-moves .chess-moves-list button.active { color: var(--accent-text); }
.ma-moves .chess-moves-list button.active .ma-glyph { color: inherit; }

/* ---------- Вкладки ---------- */
.ma-tabs {
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
  border-bottom: 1px solid var(--border);
}
.ma-tabs::-webkit-scrollbar { display: none; }
.ma-tabs [role="tab"] {
  flex: 0 0 auto;
  padding: 10px 14px;
  border: 0;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.ma-tabs [role="tab"]:hover { color: var(--text); }
.ma-tabs [role="tab"][aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.ma-tabs [role="tab"]:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
@media (max-width: 1024px) {
  .ma-tabs [role="tab"] { padding: 10px; font-size: 0.9rem; }
}
@container ma-side (max-width: 440px) {
  .ma-tabs [role="tab"] { padding: 10px 6px; font-size: 0.8rem; }
}
.ma-panel { padding: 16px 0 0; }
.ma-panel[hidden] { display: none; }
.ma-subtitle { margin: 22px 0 10px; font-size: 1rem; }
.ma-panel > .ma-subtitle:first-child { margin-top: 0; }

.ma .btn .icon { width: 16px; height: 16px; }
.ma .btn:has(.icon) { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.ma input[type="checkbox"] { width: 17px; height: 17px; margin: 0; accent-color: var(--accent); }

/* ---------- Сводка движков ---------- */
.ma-summary {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-block);
}
.ma-summary-head { display: flex; align-items: center; gap: 14px; }
.ma-score {
  flex: 0 0 auto;
  min-width: 88px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  font-family: var(--mono);
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.2;
  text-align: center;
}
.ma-score[data-side="w"] { background: #EAE3D2; border-color: #EAE3D2; color: #1D1B16; }
.ma-score[data-side="b"] { background: #111; border-color: var(--border); color: #F2EDE0; }
.ma-summary-text { min-width: 0; line-height: 1.4; }
.ma-summary-text > strong { display: block; font-size: 1.02rem; }
.ma-summary-text > span { display: block; color: var(--muted); font-size: 0.88rem; }

.ma-wdl { margin-top: 12px; }
.ma-wdl[hidden] { display: none; }
.ma-wdl-bar {
  display: flex;
  height: 10px;
  overflow: hidden;
  border-radius: 5px;
  background: var(--bg-input);
}
.ma-wdl-bar span { display: block; height: 100%; transition: width 0.3s ease; }
.ma-wdl-w { background: #EAE3D2; }
.ma-wdl-d { background: #8C8673; }
.ma-wdl-l { background: #0E0D0A; }
.ma-wdl-text { margin: 6px 0 0; color: var(--hint); font-size: 0.82rem; }

.ma-hint { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-top: 12px; }
.ma-hint-text { color: var(--link-green); font-size: 0.9rem; }

/* ---------- Настройки ---------- */
.ma-settings {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  align-items: end;
  gap: 10px;
  margin-top: 14px;
}
.ma-panel > .ma-settings:first-child { margin-top: 0; }
.ma-settings label span {
  display: block;
  margin-bottom: 4px;
  color: var(--hint);
  font-size: 0.82rem;
  font-weight: 600;
}
.ma-settings select { padding: 8px 10px; }
.ma-settings .btn { padding-block: 9px; }
.ma-toggles { display: flex; flex-wrap: wrap; gap: 8px 18px; margin-top: 12px; }
.ma-check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.9rem;
  cursor: pointer;
  user-select: none;
}

/* ---------- Карточки движков ---------- */
.ma-engines { display: grid; gap: 10px; margin-top: 14px; }
.ma-engine {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--engine-color);
  border-radius: var(--radius-sm);
  background: var(--bg-block);
}
.ma-engine-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; }
.ma-engine-status { color: var(--hint); font-size: 0.82rem; }
.ma-engine[data-state="busy"] .ma-engine-status::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--engine-color);
  vertical-align: 1px;
  animation: ma-pulse 1s ease-in-out infinite alternate;
}
.ma-engine[data-state="error"] .ma-engine-status { color: var(--danger); }
@keyframes ma-pulse { to { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) {
  .ma-engine[data-state="busy"] .ma-engine-status::before { animation: none; }
}

/* Переключатель движка. */
.ma-switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.ma-switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.ma-switch-track {
  position: relative;
  width: 34px;
  height: 20px;
  border-radius: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  transition: background 0.15s;
}
.ma-switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--hint);
  transition: transform 0.15s, background 0.15s;
}
.ma-switch input:checked + .ma-switch-track { background: var(--engine-color); border-color: var(--engine-color); }
.ma-switch input:checked + .ma-switch-track::after { transform: translateX(14px); background: #FFF; }
.ma-switch input:focus-visible + .ma-switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.ma-engine-name { font-weight: 700; color: var(--text); }

.ma-lines { margin: 10px 0 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.ma-lines:empty { display: none; }
.ma-line { display: flex; align-items: flex-start; gap: 8px; }
.ma-line-score {
  flex: 0 0 auto;
  min-width: 58px;
  padding: 2px 6px;
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 0.85rem;
  font-weight: 700;
  text-align: center;
}
.ma-line-score[data-side="w"] { background: #EAE3D2; color: #1D1B16; }
.ma-line-score[data-side="b"] { background: #111; color: #F2EDE0; border: 1px solid var(--border); }
.ma-line-pv { display: flex; flex-wrap: wrap; align-items: center; gap: 1px 2px; min-width: 0; line-height: 1.5; }
.ma-line-pv .chess-move-number { padding-left: 3px; font-size: 0.8rem; }
.ma-line-pv button {
  padding: 0 4px;
  border: 0;
  border-radius: 5px;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}
.ma-line-pv button:hover { background: var(--bg-hover); color: var(--link); }
.ma-line + .ma-line .ma-line-pv button { font-weight: 500; color: var(--muted); }
.ma-pv-more { color: var(--hint); }

/* ---------- Позиция ---------- */
.ma-notes { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.ma-note {
  display: flex;
  gap: 10px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-block);
  font-size: 0.92rem;
  line-height: 1.45;
}
.ma-note-mark {
  flex: 0 0 auto;
  width: 4px;
  border-radius: 2px;
  background: var(--hint);
}
.ma-note[data-tone="good"] .ma-note-mark { background: var(--link-green); }
.ma-note[data-tone="bad"] .ma-note-mark { background: var(--danger); }
.ma-note[data-tone="warn"] .ma-note-mark { background: var(--warning); }
.ma-san { font-weight: 700; white-space: nowrap; }

.ma-facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px 16px;
  margin: 14px 0 0;
}
.ma-facts dt,
.ma-list dt { color: var(--hint); font-size: 0.8rem; font-weight: 600; }
.ma-facts dd,
.ma-list dd { margin: 0; font-weight: 600; }

.ma-material { display: grid; gap: 6px; }
.ma-material-row { display: flex; align-items: center; gap: 8px; min-height: 30px; }
.ma-material-side { flex: 0 0 64px; color: var(--muted); font-size: 0.88rem; }
.ma-material-sum { margin-left: auto; font-family: var(--mono); font-weight: 700; }
.ma-material-piece { position: relative; display: inline-flex; align-items: flex-end; }
.ma-material-piece .cb-piece-image {
  display: inline-block;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background-color: #E9D6B4;
  background-size: 86% 86%;
}
.ma-material-piece small { margin-left: 2px; color: var(--muted); font-size: 0.78rem; font-weight: 700; }
.ma-small { margin: 8px 0 0; color: var(--muted); font-size: 0.88rem; line-height: 1.45; }
.ma-muted { margin: 0; color: var(--hint); font-size: 0.9rem; }

.ma-terms { display: grid; gap: 6px; }
.ma-term { display: grid; grid-template-columns: minmax(120px, 1fr) minmax(90px, 1.3fr) 54px; align-items: center; gap: 10px; }
.ma-term-name { font-size: 0.88rem; color: var(--muted); }
.ma-term-bar { position: relative; height: 10px; border-radius: 5px; background: var(--bg-input); }
.ma-term-bar::before {
  content: "";
  position: absolute;
  left: 50%;
  top: -2px;
  bottom: -2px;
  width: 1px;
  background: var(--border);
}
.ma-term-bar i { position: absolute; top: 0; bottom: 0; border-radius: 5px; }
.ma-term-bar i[data-side="w"] { left: 50%; background: #EAE3D2; }
.ma-term-bar i[data-side="b"] { right: 50%; background: #0E0D0A; box-shadow: inset 0 0 0 1px var(--border); }
.ma-term-value { font-family: var(--mono); font-size: 0.82rem; text-align: right; }

.ma-table-wrap { overflow-x: auto; }
.ma-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.ma-table th,
.ma-table td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: left; }
.ma-table thead th { color: var(--hint); font-size: 0.8rem; font-weight: 600; }
.ma-table tbody th { color: var(--muted); font-weight: 500; }
.ma-table td { font-weight: 600; }
.ma-table th[title] { cursor: help; }
.ma-level[data-level="safe"] { color: var(--link-green); }
.ma-level[data-level="warn"] { color: var(--warning); }
.ma-level[data-level="danger"] { color: var(--danger); }

.ma-sides { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px; }
.ma-side-card {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-block);
}
.ma-side-card h4 { margin: 0 0 8px; font-size: 0.95rem; }
.ma-list { display: grid; gap: 6px; margin: 0; }

.ma-tactics,
.ma-moments { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.ma-tactic,
.ma-moments button {
  display: flex;
  align-items: baseline;
  gap: 10px;
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-block);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  line-height: 1.45;
  text-align: left;
  cursor: pointer;
}
.ma-tactic:hover,
.ma-moments button:hover { background: var(--bg-hover); border-color: var(--accent-soft-border); }
.ma-tactic-move { flex: 0 0 auto; min-width: 54px; font-weight: 700; color: var(--link-green); }
.ma-tactic[data-kind="mate"] .ma-tactic-move { color: var(--c-brilliant); }
.ma-threat { margin: 0; padding: 8px 12px; border-left: 3px solid var(--danger); background: rgba(217, 99, 79, 0.08); }
.ma-plain { margin: 0; padding-left: 20px; color: var(--muted); font-size: 0.92rem; }
.ma-plain li { margin-bottom: 4px; }

/* ---------- Разбор партии ---------- */
.ma-progress { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-top: 14px; }
.ma-progress[hidden] { display: none; }
.ma-progress-bar { flex: 1 1 100%; height: 8px; overflow: hidden; border-radius: 4px; background: var(--bg-input); }
.ma-progress-bar span { display: block; width: 0; height: 100%; background: var(--accent); transition: width 0.2s; }
.ma-progress-text { flex: 1 1 auto; color: var(--muted); font-size: 0.88rem; }
[data-review-result] { margin-top: 14px; }
.ma-review-opening { margin: 0 0 10px; color: var(--muted); font-weight: 600; }
.ma-review-sides { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
.ma-review-card {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-block);
}
.ma-review-name { display: flex; align-items: center; gap: 8px; font-weight: 700; overflow-wrap: anywhere; }
.ma-review-name .cb-piece-image {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background-color: #E9D6B4;
  background-size: 82% 82%;
}
.ma-accuracy { display: flex; align-items: baseline; gap: 8px; margin-top: 8px; }
.ma-accuracy strong { font-size: 1.8rem; line-height: 1.1; }
.ma-accuracy span { color: var(--hint); font-size: 0.85rem; }
.ma-class-list { margin: 10px 0 0; padding: 0; list-style: none; display: grid; gap: 3px; font-size: 0.88rem; }
.ma-class-list li { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.ma-class-list b { margin-left: auto; color: var(--class-color); }
.ma-moment-move { flex: 0 0 auto; font-weight: 700; color: var(--class-color); white-space: nowrap; }
.ma-moment-text { color: var(--muted); }
.ma-moments .ma-class-mark { align-self: center; }

.ma-graph {
  display: block;
  width: 100%;
  height: 170px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: crosshair;
  touch-action: pan-y;
}

/* ---------- Партия ---------- */
.ma-label {
  display: block;
  margin: 14px 0 6px;
  color: var(--hint);
  font-size: 0.82rem;
  font-weight: 600;
}
.ma-panel > .ma-label:first-child { margin-top: 0; }
.ma-inline { display: flex; flex-wrap: wrap; gap: 8px; }
.ma-inline input { flex: 1 1 220px; min-width: 0; }
.ma-inline .btn { flex: 0 0 auto; white-space: nowrap; }
.ma input[data-fen],
.ma input[data-link],
.ma textarea[data-pgn] { font-family: var(--mono); font-size: 0.82rem; }
.ma textarea[data-pgn] { min-height: 170px; }
.ma-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.ma-buttons-split { margin-top: 22px; padding-top: 14px; border-top: 1px solid var(--border); }
.ma-message { margin: 8px 0 0; font-size: 0.9rem; line-height: 1.45; }
.ma-message[hidden] { display: none; }
.ma-message[data-tone="good"] { color: var(--link-green); }
.ma-message[data-tone="bad"] { color: var(--danger); }

/* ---------- Справка ---------- */
.ma-about p { margin: 0 0 12px; color: var(--muted); }
.ma-about ul { margin: 0 0 12px; padding-left: 20px; color: var(--muted); }
.ma-about li { margin-bottom: 6px; }
.ma-about strong { color: var(--text); }
