:root {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --raised: #242422;
  --line: #343431;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #999791;
  --good: #3987e5;
  --neutral: #6b6a66;
  --bad: #e66767;
  --warning: #fab219;
  /* SVG, а не conic-gradient: тот в WebKit перекашивает клетки */
  --checker: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8' shape-rendering='crispEdges'%3E%3Cpath fill='%23d8d3c4' d='M0 0h8v8H0z'/%3E%3Cpath fill='%2370869b' d='M1 0h1v1h-1zM3 0h1v1h-1zM5 0h1v1h-1zM7 0h1v1h-1zM0 1h1v1h-1zM2 1h1v1h-1zM4 1h1v1h-1zM6 1h1v1h-1zM1 2h1v1h-1zM3 2h1v1h-1zM5 2h1v1h-1zM7 2h1v1h-1zM0 3h1v1h-1zM2 3h1v1h-1zM4 3h1v1h-1zM6 3h1v1h-1zM1 4h1v1h-1zM3 4h1v1h-1zM5 4h1v1h-1zM7 4h1v1h-1zM0 5h1v1h-1zM2 5h1v1h-1zM4 5h1v1h-1zM6 5h1v1h-1zM1 6h1v1h-1zM3 6h1v1h-1zM5 6h1v1h-1zM7 6h1v1h-1zM0 7h1v1h-1zM2 7h1v1h-1zM4 7h1v1h-1zM6 7h1v1h-1z'/%3E%3C/svg%3E") 0 0 / 100% 100%;
  --board-size: 416px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-variant-numeric: tabular-nums;
}

h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button, select, input {
  min-height: 32px;
  padding: 4px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--raised);
}
button, summary, [data-line] { cursor: pointer; }
button:hover { border-color: var(--muted); }
:focus-visible { outline: 2px solid var(--good); outline-offset: 1px; }
a { color: var(--ink-2); }

/* Шапка и фильтры */
.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 20px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.top h1 { font-size: 16px; }
#load-form { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
#load-form label { display: flex; align-items: center; gap: 6px; color: var(--ink-2); }
#load-form input { width: 150px; background: var(--page); color: var(--ink); }
.primary { background: #256abf; border-color: #256abf; color: #fff; }
#status { color: var(--ink-2); }
#status.error { color: var(--bad); }

.intro { max-width: 620px; margin: 16vh auto 0; padding: 0 20px; color: var(--ink-2); font-size: 16px; }
.intro h2 { margin-bottom: 12px; color: var(--ink); font-size: 30px; }

.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px; padding: 12px 20px 0; }
.segmented { display: flex; gap: 4px; }
.filters button.on, .chips button.on { border-color: var(--good); background: #183654; }
.filters label { display: flex; align-items: center; gap: 6px; color: var(--ink-2); }
.filters .note { color: var(--muted); }

/* Раскладка */
.layout {
  display: grid;
  grid-template-columns: calc(var(--board-size) + 16px) minmax(0, 1fr);
  align-items: start;
  gap: 20px;
  padding: 14px 20px 40px;
}
.board-panel {
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
  overscroll-behavior: contain;
}
main { min-width: 0; }

/* Доска */
.board { width: var(--board-size); height: var(--board-size); }
cg-board {
  background: var(--checker);
}
cg-board square.last-move { background: rgb(242 204 92 / 38%); }
cg-board square.selected { background: rgb(57 135 229 / 50%); }
cg-board square.move-dest { background: radial-gradient(rgb(0 0 0 / 28%) 19%, transparent 20%); }
cg-board square.oc.move-dest { background: radial-gradient(transparent 0 78%, rgb(0 0 0 / 28%) 80%); }
cg-board square.check { background: radial-gradient(#e24a4a, transparent 75%); }
.cg-wrap piece.anim { z-index: 8; }
.cg-wrap piece.dragging { cursor: grabbing; z-index: 11; }
.cg-wrap piece.fading { opacity: .4; }
.cg-wrap piece.ghost { opacity: .3; }
.cg-wrap coords { color: #20262b; font-size: 10px; opacity: .8; }
.cg-wrap coords.files { left: 0; bottom: 0; height: 14px; text-align: right; }
.cg-wrap coords.files coord { flex: 1 1 0; padding-right: 3px; }
.cg-wrap coords.ranks { top: 0; left: 3px; }
.cg-wrap coords.ranks coord { flex: 1 1 0; padding-top: 1px; transform: none; }

.board-nav { display: flex; align-items: center; gap: 4px; width: var(--board-size); margin: 8px 0; }
.board-nav .eval { margin-left: auto; white-space: nowrap; }
.eval small { margin-right: 8px; color: var(--muted); font-size: 12px; }
.eval b { font-size: 16px; font-weight: 600; }

.explorer { width: var(--board-size); padding-bottom: 16px; }
.explorer table { font-size: 12px; }
.explorer th, .explorer td { padding: 6px 4px; }
.explorer td:nth-child(5) { white-space: normal; }
.crumbs { display: flex; flex-wrap: wrap; gap: 2px; margin-bottom: 8px; }
.crumbs button { min-height: 24px; padding: 1px 5px; border: 0; background: none; color: var(--ink-2); }
.crumbs button.current { background: var(--raised); color: var(--ink); }
.crumbs button.future { color: var(--muted); }
.position { margin-bottom: 8px; color: var(--ink-2); }

.advice {
  margin-bottom: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink-2);
}
.advice .move { font-size: 22px; }

/* Вкладки и панели */
.tabs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 12px; border-bottom: 1px solid var(--line); }
.tabs button { border: 0; border-radius: 0; background: none; color: var(--ink-2); }
.tabs button.on { color: var(--ink); box-shadow: inset 0 -2px var(--good); }

.panel { margin-bottom: 12px; padding: 14px; border-radius: 10px; background: var(--surface); }
.panel h3 { margin-bottom: 8px; font-size: 15px; }
.panel > .hint { margin: -4px 0 8px; }
.hint, .sub, .empty { color: var(--muted); font-size: 12.5px; }
.empty { padding: 12px 0; }
.two { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 0 12px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 12px; }
.tile { padding: 12px 14px; border-radius: 10px; background: var(--surface); }
.tile b { display: block; font-size: 24px; font-weight: 600; }
.tile span { color: var(--ink-2); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { color: var(--muted); font-size: 12px; font-weight: 500; text-align: left; white-space: nowrap; }
th, td { padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child > td { border-bottom: 0; }
td.num { white-space: nowrap; }
tr[data-line]:hover, [data-line].row:hover { background: var(--raised); }
tr.variant td:first-child { padding-left: 34px; color: var(--ink-2); }
.expand, .expand-space { display: inline-block; width: 24px; margin-right: 2px; }
.expand { min-height: 22px; padding: 0; border: 0; background: none; color: var(--muted); }

.move {
  display: inline-block;
  min-height: 0;
  padding: 1px 5px;
  border: 0;
  border-radius: 4px;
  background: var(--raised);
  color: var(--ink);
  font-weight: 600;
  white-space: nowrap;
}

.row {
  display: flex;
  justify-content: space-between;
  gap: 4px 12px;
  padding: 7px 4px;
  border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }

/* Метки: цвет всегда вместе со значком и словом */
.mark { display: inline-flex; align-items: center; gap: 4px; color: var(--ink-2); white-space: nowrap; }
.mark i { color: var(--neutral); font-style: normal; font-weight: 700; }
.mark.good i, .mark.keep i, .mark.better i { color: var(--good); }
.mark.bad i, .mark.fix i, .mark.blunder i, .mark.mistake i { color: var(--bad); }
.mark.lean-good i { color: var(--good); opacity: .7; }
.mark.lean-bad i { color: var(--bad); opacity: .7; }
.mark.inaccuracy i, .mark.risky i { color: var(--warning); }

.wdl { display: flex; gap: 2px; width: 100px; height: 8px; }
.wdl span { height: 100%; min-width: 2px; }
.wdl .w { background: var(--good); }
.wdl .d { background: var(--neutral); }
.wdl .l { background: var(--bad); }
.wdl > :first-child { border-radius: 4px 0 0 4px; }
.wdl > :last-child { border-radius: 0 4px 4px 0; }
.wdl > :only-child { border-radius: 4px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-bottom: 8px; color: var(--muted); font-size: 12px; }
.legend i { display: inline-block; width: 9px; height: 9px; margin-right: 5px; border-radius: 2px; }
.legend .w { background: var(--good); }
.legend .d { background: var(--neutral); }
.legend .l { background: var(--bad); }

.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 10px; }

/* Репертуар */
.tree details > .children { margin-left: 10px; padding-left: 12px; border-left: 1px solid var(--line); }
.tree summary, .tree .leaf {
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr) auto;
  align-items: center;
  gap: 2px 6px;
  padding: 6px 4px;
  list-style: none;
}
.tree summary::-webkit-details-marker { display: none; }
.tree summary::before { content: "▸"; color: var(--muted); }
.tree details[open] > summary::before { content: "▾"; }
.tree .leaf::before { content: ""; }
.tree summary:hover, .tree .leaf:hover { background: var(--raised); }
.pair { display: flex; flex-wrap: wrap; align-items: center; gap: 3px 8px; }
.pair .arrow { color: var(--muted); }
.tree .sub { grid-column: 2 / -1; }
.figures { color: var(--ink-2); font-size: 12.5px; white-space: nowrap; }

/* Проблемы */
.problem { padding: 10px 4px; border-bottom: 1px solid var(--line); }
.problem:last-child { border-bottom: 0; }
.problem:hover { background: var(--raised); }
.problem .head { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; }
.problem .before { color: var(--ink-2); }
.problem.inactive { opacity: .65; }

#tip {
  position: fixed;
  z-index: 100;
  max-width: 260px;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--raised);
  font-size: 12px;
  pointer-events: none;
}

@media (max-width: 900px) {
  :root { --board-size: min(416px, calc(100vw - 48px)); }
  .layout { grid-template-columns: minmax(0, 1fr); padding-inline: 16px; }
  .board-panel { position: static; max-height: none; }
}

/* Добавлено к первой версии: вывод, табии, линии */
.summary { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; }
.summary p { color: var(--ink-2); }
.summary b { color: var(--ink); }
.summary .next { color: var(--ink); font-size: 16px; }

.split { display: flex; gap: 2px; width: 100px; height: 8px; overflow: hidden; border-radius: 4px; }
.split span { min-width: 2px; }
.split .error, .legend .error { background: var(--bad); }
.split .clean, .legend .clean { background: var(--good); }
.split .unknown, .legend .unknown { background: var(--neutral); }
.mark.worse i, .mark.error i { color: var(--bad); }
.mark.better i { color: var(--good); }
.mark.choice i { color: var(--warning); }

.mini {
  flex: none;
  width: 64px;
  height: 64px;
  overflow: hidden;
  border-radius: 3px;
  background: var(--checker);
}
.mini piece { will-change: auto; }
.tabiya-name { display: flex; align-items: center; gap: 10px; min-width: 260px; }
.tabiya-name .title { display: block; overflow-wrap: anywhere; }
.tabiya-name .sub { display: block; }
tr[data-tabiya], tr[data-select] { cursor: pointer; }
tr[data-tabiya]:hover, tr[data-select]:hover, tr.selected { background: var(--raised); }
tr.selected > td { border-bottom-color: transparent; }
tr.detail > td { padding: 10px 10px 14px; background: var(--raised); }
.detail .conclusion { margin-bottom: 10px; color: var(--ink-2); }
.detail .conclusion b { color: var(--ink); }
.detail h4 { margin: 0 0 4px; font-size: 13px; }
.variants { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px 20px; }
.move-line { display: flex; justify-content: space-between; gap: 10px; padding: 5px 4px; border-bottom: 1px solid var(--line); }
.move-line:hover { background: var(--surface); }
.move-line:last-child { border-bottom: 0; }
.move-line .num { color: var(--muted); white-space: nowrap; }
.problem-actions { display: flex; gap: 6px; margin-top: 6px; }
button.small { min-height: 24px; padding: 1px 8px; color: var(--ink-2); font-size: 12px; }
.to-tabiya { min-height: 0; padding: 0; border: 0; background: none; color: var(--ink-2); text-align: left; text-decoration: underline; text-decoration-color: var(--neutral); text-underline-offset: 3px; }

.engine { margin-bottom: 10px; }
.engine h4 { display: flex; align-items: center; justify-content: space-between; margin: 0 0 4px; color: var(--muted); font-size: 12px; font-weight: 500; }
.engine-line {
  display: grid;
  grid-template-columns: 62px 48px minmax(0, 1fr) auto;
  align-items: baseline;
  gap: 8px;
  width: 100%;
  min-height: 0;
  padding: 5px 4px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: none;
  text-align: left;
  font-size: 12.5px;
}
.engine-line:hover { background: var(--raised); }
.engine-line .rest { overflow: hidden; color: var(--ink-2); text-overflow: ellipsis; white-space: nowrap; }
.engine-line .word { color: var(--muted); }
.engine-line .word.bad { color: var(--bad); }
.masters { margin-top: 6px; color: var(--ink-2); font-size: 12.5px; }
.masters .move { margin: 2px 3px 2px 0; }

/* Карта табий: ветки сверху вниз, табии как станции */
.map-tree, .map-tree ul { margin: 0; padding: 0; list-style: none; }
.map-tree ul { margin-left: 12px; }
.map-tree li { position: relative; padding: 6px 0 0 20px; }
.map-tree li::before { content: ""; position: absolute; top: 0; bottom: 0; left: 0; border-left: 2px solid var(--line); }
.map-tree li:last-child::before { bottom: auto; height: 21px; }
.map-tree li::after { content: ""; position: absolute; top: 20px; left: 0; width: 16px; border-top: 2px solid var(--line); }
.map-tree .segment { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; min-height: 28px; }
.map-tree .segment .count { margin-left: 6px; }
.map-tree li.more { padding-top: 8px; }
.station { max-width: 640px; margin: 4px 0 2px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.station.break { border-left: 3px solid var(--bad); }
.station.selected { border-color: var(--good); }
.station-head { display: flex; align-items: center; gap: 10px; width: 100%; min-height: 0; padding: 6px 8px; border: 0; background: none; text-align: left; }
.station-head:hover { background: var(--raised); }
.station .mini { width: 52px; height: 52px; }
.station-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.station-text .title { overflow-wrap: anywhere; }
.station-figures { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; color: var(--ink-2); font-size: 12.5px; }
.station-detail { padding: 8px 10px 12px; border-top: 1px solid var(--line); }

@media (max-width: 900px) {
  /* Глубокие цепочки на телефоне: отступ маленький, иначе уедут за экран */
  .map-tree ul { margin-left: 0; }
  .map-tree li { padding-left: 8px; }
  .map-tree li::after { width: 6px; }
  .station { min-width: 0; }
  .station .mini { width: 44px; height: 44px; }
}

/* Общая шапка: заставка и разделы «Дебют» и «Эндшпиль» */
.intro ul { padding-left: 20px; }
.intro li { margin-top: 6px; }
.intro .privacy { margin-top: 16px; color: var(--muted); font-size: 13px; }
.phases { display: flex; gap: 4px; padding: 10px 20px 0; border-bottom: 1px solid var(--line); }
.phases button { border: 0; border-radius: 0; background: none; color: var(--ink-2); font-size: 15px; font-weight: 600; }
.phases button.on { color: var(--ink); box-shadow: inset 0 -2px var(--good); }
.phases .note { margin-left: 4px; color: var(--muted); font-size: 12px; font-weight: 400; }

/* Раздел «Эндшпиль»: свои правила анализатора эндшпилей, только внутри раздела */
#endgame .filters .segmented button { min-height: 30px; }

/* Ошибки и их группы */
#endgame .repeat { padding: 8px 0 4px; border-bottom: 1px solid var(--line); }
#endgame .repeat:last-child { border-bottom: 0; }
#endgame .repeat > p { padding: 0 4px; }
#endgame .repeat .problem { border-bottom: 0; }
#endgame .crumbs button.bad { color: var(--bad); font-weight: 600; }
#endgame .mark.tablebase i { color: var(--good); }

/* Типовые позиции */
#endgame .cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 10px; }
#endgame .card { display: flex; align-items: center; gap: 10px; min-width: 0; padding: 8px; text-align: left; background: var(--surface); }
#endgame .card.selected { border-color: var(--good); background: #14273a; }
#endgame .card-text { display: flex; flex: 1; flex-direction: column; gap: 2px; min-width: 0; }
#endgame .card strong { overflow-wrap: anywhere; line-height: 1.25; }
#endgame .mini {
  position: relative;
  flex: none;
  width: 84px;
  height: 84px;
  overflow: hidden;
  border-radius: 3px;
  background: var(--checker); /* SVG: conic-gradient в WebKit перекашивает клетки */
}
#endgame .mini piece { position: absolute; top: 0; left: 0; width: 12.5%; height: 12.5%; background-size: cover; will-change: auto; }

/* Часы */
#endgame .tiles.small { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; margin: 8px 0 12px; }
#endgame .tiles.small .tile { padding: 8px 10px; background: var(--raised); }
#endgame .tiles.small .tile b { font-size: 20px; }
#endgame .tiles.small .tile span { font-size: 12.5px; }
#endgame tr.thin td { color: var(--muted); }
#endgame .panel h3 .sub { font-weight: 400; }

@media (max-width: 900px) {
  #endgame .cards { grid-template-columns: minmax(0, 1fr); }
  #endgame .card .mini { width: 64px; height: 64px; }
}

/* Что чинить: уровни по цене исправления */
#endgame .tier { padding: 6px 0 10px; border-bottom: 1px solid var(--line); }
#endgame .tier:last-child { border-bottom: 0; }
#endgame .tier h4 { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px; margin: 4px 0 2px; font-size: 14px; }
#endgame .tier .tier-n { display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; background: var(--raised); font-size: 12px; }
#endgame .tier .link { min-height: 0; padding: 0; border: 0; background: none; color: var(--ink-2); text-decoration: underline; }
