:root {
  --bg: #f7f4ee;
  --ink: #1d2a35;
  --muted: #56636d;
  --panel: #ffffff;
  --border: #ddd5c6;
  --sq-light: #f0dcb8;
  --sq-dark: #b88a62;
  --frame: #6b4f36;
  --accent: #2e7d32;
  --on-accent: #ffffff;
  --good: #2e7d32;
  --good-bg: #edf7ee;
  --info: #1d5fa8;
  --soft-ink: #7a4b00;
  --hint-ring: #e8890c;
  --hint-bg: #fdf3e2;
  --answer: #1e88e5;
  --expl-bg: #eef5fc;
  --notice-bg: #f1ede4;
  --chip-bg: #efe9dd;
  --focus: #0d47a1;
  --err-bg: #fdecea;
  --err-border: #f1b8b2;
  --err-ink: #8e1c12;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1e22;
    --ink: #ebe8e2;
    --muted: #a9b3ba;
    --panel: #23292f;
    --border: #3a424a;
    --accent: #66bb6a;
    --on-accent: #0b1f0d;
    --good: #7fcf83;
    --good-bg: #1f3322;
    --info: #8cc4ff;
    --soft-ink: #f2c46b;
    --hint-bg: #33302a;
    --expl-bg: #1f2c3a;
    --notice-bg: #2c3238;
    --chip-bg: #2c3238;
    --focus: #8cc4ff;
    --err-bg: #3a2220;
    --err-border: #6e3a35;
    --err-ink: #ffb4a9;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, sans-serif;
  font-size: 18px;
  line-height: 1.45;
}
.page { max-width: 1040px; margin: 0 auto; padding: 16px; }
.top {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 16px;
  margin-bottom: 12px;
}
.top h1 { font-size: 1.25rem; margin: 0; }
.progress { margin: 0; color: var(--muted); font-weight: 600; }
.loading { color: var(--muted); font-size: 1.1rem; text-align: center; padding: 48px 0; margin: 0; }

/* --- раскладка: доска слева, задача и отклик справа; на узком экране — задача, доска, отклик --- */
.lesson {
  display: grid;
  grid-template-columns: auto minmax(300px, 1fr);
  grid-template-areas: "board head" "board panel";
  grid-template-rows: auto 1fr;
  column-gap: 28px;
  align-items: start;
}
.task-head {
  grid-area: head;
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 14px 14px 0 0;
  padding: 18px 20px 2px;
}
.panel {
  grid-area: panel;
  background: var(--panel);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 14px 14px;
  padding: 2px 20px 20px;
}
/* Доска помещается и в низкое окно ноутбука: сторона — не больше высоты окна без шапки. */
.board-wrap {
  grid-area: board;
  position: relative;
  width: min(520px, calc(100vw - 400px), max(280px, calc(100vh - 90px)));
  width: min(520px, calc(100vw - 400px), max(280px, calc(100dvh - 90px)));
}
@media (max-width: 760px) {
  .lesson {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "head" "board" "panel";
    grid-template-rows: auto;
    row-gap: 12px;
  }
  .board-wrap {
    justify-self: center;
    width: min(100%, max(260px, calc(100vh - 120px)));
    width: min(100%, max(260px, calc(100dvh - 120px)));
  }
  .task-head { background: none; border: none; padding: 0; }
  .panel { border: 1px solid var(--border); border-radius: 14px; padding: 4px 16px 16px; }
}

.repeat-note { color: var(--muted); margin: 0 0 6px; font-size: 0.95rem; }
.prompt { font-size: 1.3rem; line-height: 1.35; margin: 0 0 10px; }
.prompt:focus { outline: none; }
.side {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 12px;
  padding: 4px 14px 4px 6px;
  border-radius: 999px;
  background: var(--chip-bg);
  font-weight: 600;
}
.side-dot { width: 20px; height: 20px; border-radius: 50%; border: 2px solid #2b2b2b; flex: none; }
.side.is-white .side-dot { background: #ffffff; }
.side.is-black .side-dot { background: #222222; border-color: #888888; }

/* --- доска --- */
.board-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 4px solid var(--frame);
  border-radius: 6px;
  overflow: hidden;
  container-type: inline-size;
}
.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
  user-select: none;
  touch-action: manipulation;
}
.board .row { display: contents; }
.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  outline: none;
}
.board:not(.disabled) .sq { cursor: pointer; }
.sq.light { background-color: var(--sq-light); }
.sq.dark { background-color: var(--sq-dark); }
.sq.last { background-color: #d9de78; }
.sq.dark.last { background-color: #b7bf52; }
.sq.selected {
  background-image: linear-gradient(rgba(46, 125, 50, 0.32), rgba(46, 125, 50, 0.32));
  box-shadow: inset 0 0 0 4px #2e7d32;
}
.sq.hint {
  background-image: linear-gradient(rgba(232, 137, 12, 0.35), rgba(232, 137, 12, 0.35));
  box-shadow: inset 0 0 0 5px var(--hint-ring);
  animation: pulse 1.1s ease-in-out 3;
}
.sq.tried { box-shadow: inset 0 0 0 4px rgba(198, 40, 40, 0.75); }
.sq.answer-from, .sq.answer-to {
  background-image: linear-gradient(rgba(30, 136, 229, 0.25), rgba(30, 136, 229, 0.25));
  box-shadow: inset 0 0 0 4px var(--answer);
}
.sq.target::before {
  content: "";
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(27, 94, 32, 0.55);
}
.sq.target-take::before {
  content: "";
  position: absolute;
  inset: 6%;
  border-radius: 50%;
  border: 5px solid rgba(27, 94, 32, 0.6);
}
/* Фокус — тёмное кольцо в белой обводке: заметно и на светлой, и на тёмной клетке. */
.sq:focus-visible::after {
  content: "";
  position: absolute;
  inset: 4px;
  border: 3px solid #0d47a1;
  outline: 2px solid #ffffff;
  border-radius: 3px;
  pointer-events: none;
  z-index: 2;
}
.piece {
  font-family: "Segoe UI Symbol", "DejaVu Sans", sans-serif;
  font-size: min(9vw, 52px);
  font-size: 10cqw;
  line-height: 1;
  z-index: 1;
}
.piece.white { color: #ffffff; text-shadow: 0 0 2px #000, 0 0 1px #000, 1px 1px 0 #000, -1px -1px 0 #000; }
.piece.black { color: #1b1b1b; text-shadow: 0 0 2px rgba(255, 255, 255, 0.7); }
.coord { position: absolute; font-size: 11px; font-size: max(10px, 2.3cqw); font-weight: 600; line-height: 1; }
.sq.light .coord, .sq.dark .coord { color: #3b2a17; }
.coord.rank { top: 3px; left: 4px; }
.coord.file { bottom: 3px; right: 5px; }
.arrow-layer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }
.arrow-line { stroke: var(--answer); stroke-width: 0.2; stroke-linecap: round; opacity: 0.85; }
.arrow-head { fill: var(--answer); opacity: 0.85; }
@keyframes pulse {
  50% { box-shadow: inset 0 0 0 9px var(--hint-ring); }
}

/* --- превращение пешки --- */
.promo {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(92%, 360px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
  padding: 14px;
  text-align: center;
  z-index: 5;
}
.promo-title { font-weight: 700; margin: 0 0 10px; }
.promo-choices { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 8px; }
.promo-choices button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px; }
.glyph { font-family: "Segoe UI Symbol", "DejaVu Sans", sans-serif; font-size: 2.4rem; line-height: 1.15; }
/* Фигура на «клетке»: белая фигура не сливается с белой кнопкой. */
.promo-choices .glyph { background: var(--sq-light); border-radius: 8px; padding: 0 12px; }
.glyph.white { color: #ffffff; text-shadow: 0 0 2px #000, 0 0 1px #000, 1px 1px 0 #000, -1px -1px 0 #000; }
.glyph.black { color: #1b1b1b; text-shadow: 0 0 2px rgba(255, 255, 255, 0.7); }

/* --- отклик, подсказки, объяснение --- */
.feedback { font-size: 1.15rem; font-weight: 700; min-height: 1.5em; margin: 14px 0 10px; }
.feedback.good { color: var(--good); }
.feedback.info { color: var(--info); }
.feedback.soft { color: var(--soft-ink); }
.feedback.pending { color: var(--muted); font-weight: 500; }
.feedback:empty { min-height: 0; margin: 0; }
.notice { background: var(--notice-bg); border-radius: 10px; padding: 8px 12px; margin: 0 0 12px; }
.hints { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; }
.hints li { background: var(--hint-bg); border-radius: 10px; padding: 10px 12px; }
.hints li.new { box-shadow: 0 0 0 2px var(--hint-ring); }
.hint-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.explanation {
  border-left: 5px solid var(--answer);
  background: var(--expl-bg);
  border-radius: 0 10px 10px 0;
  padding: 10px 14px;
  margin: 0 0 4px;
}
.explanation.good { border-left-color: var(--good); background: var(--good-bg); }
.explanation p { margin: 0; }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 16px; }

/* --- кнопки --- */
button {
  font: inherit;
  min-height: 48px;
  padding: 10px 20px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
button:disabled, button[aria-disabled="true"] { opacity: 0.55; cursor: default; }
button:focus-visible, .prompt:focus-visible, .done h2:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); font-weight: 600; }
button.quiet { background: transparent; border-color: transparent; color: var(--muted); text-decoration: underline; padding: 10px 8px; }

.error-box {
  background: var(--err-bg);
  border: 1px solid var(--err-border);
  color: var(--err-ink);
  border-radius: 12px;
  padding: 12px 14px;
  margin: 12px 0;
}
.error-box p { margin: 0 0 10px; }

/* --- конец занятия --- */
.done { text-align: center; padding: 40px 16px; }
.done::before { content: "♞"; display: block; font-size: 4rem; line-height: 1; color: var(--accent); margin-bottom: 12px; }
.done h2 { font-size: 1.6rem; margin: 0 0 10px; }
.done h2:focus { outline: none; }
.done p { font-size: 1.15rem; color: var(--muted); margin: 0; }
.done .actions { justify-content: center; }
/* Mini App: название уже в шапке Telegram — место под доску; подпись ученика — в шапке страницы. */
.in-telegram .top h1 { display: none; }
.miniapp-student .quiet { min-height: 40px; padding: 4px 6px; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* --- экран взрослого --- */
.adult { font-size: 16px; }
.adult .top h1 { font-size: 1.4rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin-bottom: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.card h2 { font-size: 1rem; margin: 0 0 6px; }
.card p, .card ul { margin: 0; }
.card ul { padding-left: 1.1em; }
.totals { margin: 0 0 12px; }
.table-wrap { overflow-x: auto; }
table.summary { border-collapse: collapse; width: 100%; background: var(--panel); }
table.summary th, table.summary td { border: 1px solid var(--border); padding: 6px 8px; text-align: left; vertical-align: top; }
.note { color: var(--muted); font-size: 0.9rem; }
.adult .error { color: var(--err-ink); }
