/* Математическая машина — белая лаборатория.
   Цвет здесь кодирует смысл, а не украшает: чернила — ответ, индиго — активное,
   охра — вторичные формы, бордо — отказ, зелёное — сошедшаяся проверка. */

:root {
  --paper:   #f6f4ee;   /* поле протокола */
  --sheet:   #fffdf8;   /* лист поверх поля */
  --pult:    #fbfaf5;   /* пульт */
  --ink:     #14181f;
  --ink-2:   #454b57;
  --muted:   #6c6559;   /* ≥4.5:1 на бумаге, пульте и листе */
  --rule:    #e4dfd3;
  --rule-2:  #efebe1;

  --indigo:  #1f3d99;
  --indigo-soft: #e8ecf8;
  --ochre:   #92580f;
  --wine:    #8a2a3d;
  --green:   #2c6650;

  --grid:    #ebe7dc;

  --serif: "STIX Two Text", "Iowan Old Style", Georgia, "Times New Roman", serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --pult-w: 25.5rem;
  --step: 0.5rem;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scrollbar-color: #d8d2c4 transparent;
  scrollbar-width: thin;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 1rem;
  line-height: 1.55;
  font-kerning: normal;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

::selection { background: #d8def4; color: var(--ink); }

:focus-visible {
  outline: 2px solid var(--indigo);
  outline-offset: 2px;
  border-radius: 2px;
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: #d8d2c4;
  border: 3px solid var(--paper);
  border-radius: 6px;
}
::-webkit-scrollbar-thumb:hover { background: #c4bcaa; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ── рабочий стол ─────────────────────────────────────────────────────── */

.deck {
  display: grid;
  grid-template-columns: var(--pult-w) minmax(0, 1fr);
  min-height: 100vh;
  min-height: 100dvh;
}

/* ── пульт ────────────────────────────────────────────────────────────── */

.pult {
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  border-right: 1px solid var(--rule);
  background: var(--pult);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.pult-inner {
  display: flex;
  flex-direction: column;
  gap: calc(var(--step) * 5);
  min-height: 100%;
  padding: calc(var(--step) * 5) calc(var(--step) * 4.5) calc(var(--step) * 3);
}

.brand h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  line-height: 1.15;
  letter-spacing: -0.015em;
  text-wrap: balance;
}

.brand p {
  margin: calc(var(--step) * 0.75) 0 0;
  color: var(--muted);
  font-size: 0.875rem;
  line-height: 1.4;
}

/* поле ввода */

#console { display: flex; flex-direction: column; gap: calc(var(--step) * 1.75); }

#input {
  width: 100%;
  min-height: 4.25rem;
  padding: calc(var(--step) * 2) calc(var(--step) * 2.25);
  border: 1px solid var(--rule);
  border-radius: 3px;
  background: #fff;
  color: var(--ink);
  font-family: var(--mono);
  font-size: 1.0625rem;
  line-height: 1.45;
  resize: none;
  overflow: hidden;
  caret-color: var(--indigo);
  box-shadow: 0 1px 2px rgba(20, 24, 31, 0.04);
  transition: border-color .16s ease, box-shadow .16s ease;
}

/* плейсхолдер несёт грамматику ввода — он обязан читаться (5.8:1 на белом) */
#input::placeholder { color: var(--muted); opacity: 1; }

#input:focus {
  outline: none;
  border-color: var(--indigo);
  box-shadow: 0 0 0 3px var(--indigo-soft), 0 1px 2px rgba(20, 24, 31, 0.05);
}

/* живое эхо: как машина поняла набранное */

.echo {
  display: flex;
  align-items: flex-start;
  gap: calc(var(--step) * 1.25);
  min-height: 1.75rem;
  padding-left: calc(var(--step) * 0.25);
  color: var(--ink-2);
  font-size: 0.95rem;
  opacity: 0;
  transform: translateY(-2px);
  transition: opacity .2s ease, transform .2s ease;
}

.echo.on { opacity: 1; transform: none; }

.echo::before {
  content: "=";
  flex: none;
  margin-top: 0.05em;
  color: var(--muted);
  font-size: 0.9rem;
}

.echo .math { overflow-x: auto; overflow-y: hidden; padding-bottom: 2px; }
.echo.bad { color: var(--wine); font-size: 0.85rem; }
.echo.bad::before { content: "?"; color: var(--wine); }

/* знаки */

.keys { display: flex; flex-wrap: wrap; gap: 4px; }

.keys button {
  min-width: 2.125rem;
  height: 2.125rem;
  padding: 0 0.5rem;
  border: 1px solid var(--rule);
  border-radius: 3px;
  background: #fff;
  color: var(--ink-2);
  font-family: var(--serif);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: color .14s ease, border-color .14s ease, background .14s ease;
}

.keys button:hover { color: var(--indigo); border-color: #c3cbe4; background: #fbfcff; }
.keys button:active { background: var(--indigo-soft); }

#run {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  padding: calc(var(--step) * 1.6) 1rem;
  border: 1px solid var(--indigo);
  border-radius: 3px;
  background: var(--indigo);
  color: #fff;
  font-family: var(--serif);
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: background .16s ease, border-color .16s ease;
}

#run:hover { background: #1a3383; border-color: #1a3383; }
/* пока считает: заливка гаснет, но белая надпись остаётся читаемой */
#run[disabled] { background: #6b78a8; border-color: #6b78a8; cursor: default; }

#run kbd {
  padding: 0.05em 0.4em;
  border: 1px solid rgba(255, 255, 255, 0.45);
  border-radius: 2px;
  font-family: var(--mono);
  font-size: 0.75rem;
  opacity: 0.9;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.5;
}

.hint b {
  color: var(--ink-2);
  font-family: var(--mono);
  font-size: 0.75rem;
  font-weight: 400;
}

/* переменные и история */

.stack h2 {
  margin: 0 0 calc(var(--step) * 1.5);
  color: var(--muted);
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.13em;
  text-transform: uppercase;
}

.var {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  width: 100%;
  margin-bottom: 4px;
  padding: 0.35rem 0.5rem;
  border: 0;
  border-radius: 3px;
  background: transparent;
  color: var(--ink);
  font-family: var(--mono);
  font-size: 0.8125rem;
  text-align: left;
  cursor: pointer;
  transition: background .14s ease;
}

.var:hover { background: #f2efe6; }
.var b { color: var(--indigo); font-weight: 400; }
.var span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.var-drop {
  flex: none;
  margin-left: auto;
  padding: 0 0.25rem;
  border: 0;
  background: none;
  color: var(--muted);   /* это орган управления — он обязан быть виден */
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.var-drop:hover { color: var(--wine); }

#log-list {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: none;
}

#log-list button {
  display: block;
  width: 100%;
  padding: 0.4rem 0.5rem;
  border: 0;
  border-radius: 3px;
  background: none;
  color: var(--ink-2);
  font-family: var(--mono);
  font-size: 0.8125rem;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  transition: background .14s ease, color .14s ease;
}

#log-list button:hover { background: #f2efe6; color: var(--ink); }

.pult-foot {
  margin-top: auto;
  padding-top: calc(var(--step) * 2);
  border-top: 1px solid var(--rule-2);
}

.pult-foot p {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.5;
}

/* ── протокол ─────────────────────────────────────────────────────────── */

.protocol {
  position: relative;
  min-width: 0;
  padding: calc(var(--step) * 6) calc(var(--step) * 7) calc(var(--step) * 12);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
  background-position: -1px -1px;
}

/* лист поверх поля */

.sheet {
  position: relative;
  max-width: 54rem;
  margin: 0 0 3rem;
  padding: calc(var(--step) * 4) calc(var(--step) * 5) calc(var(--step) * 4.5);
  border: 1px solid var(--rule);
  background: var(--sheet);
  box-shadow: 0 1px 1px rgba(20, 24, 31, 0.03), 0 8px 24px -14px rgba(20, 24, 31, 0.18);
}

/* шапка разбора */

.sheet-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1.5rem;
  margin-bottom: calc(var(--step) * 3);
  padding-bottom: calc(var(--step) * 1.5);
  border-bottom: 1px solid var(--rule-2);
}

.sheet-query {
  min-width: 0;
  color: var(--ink-2);
  font-family: var(--mono);
  font-size: 0.8125rem;
  word-break: break-word;
}

.sheet-time {
  flex: none;
  color: var(--muted);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}

/* блок = маргинальная метка + содержимое */

.block {
  display: grid;
  grid-template-columns: 7.5rem minmax(0, 1fr);
  gap: 0 calc(var(--step) * 3);
  padding: calc(var(--step) * 2.25) 0;
  border-top: 1px solid var(--rule-2);
}

.block:first-of-type { border-top: 0; padding-top: 0; }

.mark {
  margin: 0;
  padding-top: 0.35rem;
  color: var(--muted);
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  line-height: 1.4;
  text-transform: uppercase;
}

.body { min-width: 0; }

/* inline-block, иначе знак равенства между формулой и результатом падает на свою строку */
.math {
  display: inline-block;
  max-width: 100%;
  vertical-align: middle;
  overflow-x: auto;
  overflow-y: hidden;
}

.body > .math { display: block; }

/* формула шире листа — показываем, что строка прокручивается */
.math.scrolls {
  position: relative;
  -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 2.5rem), transparent);
  mask-image: linear-gradient(90deg, #000 calc(100% - 2.5rem), transparent);
}
.math .katex-display { margin: 0.2em 0; text-align: left; }
.math .katex-display > .katex { text-align: left; }

/* ответ — монументально */

.block-answer { padding: calc(var(--step) * 3) 0; }
.block-answer .mark { color: var(--ink-2); }
.block-answer .math .katex { font-size: 1.72em; }
.block-answer .math .katex-display { margin: 0; }

.block-input .math .katex { font-size: 1.16em; color: var(--ink-2); }

/* список формул */

.rows { margin: 0; padding: 0; list-style: none; }

.rows li {
  padding: 0.45rem 0;
  border-bottom: 1px solid var(--rule-2);
  overflow-x: auto;
}

.rows li:last-child { border-bottom: 0; }
.rows .katex { font-size: 1.05em; }

.text { margin: 0; color: var(--ink-2); }

.note {
  margin: calc(var(--step) * 1.5) 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  font-style: italic;
  line-height: 1.5;
}

.note.verified { color: var(--green); font-style: normal; }

/* вторичные формы — охрой: метка, раскрывашки и их указатели */

.block-detail .mark { color: var(--ochre); }
.block-detail > .body > .fold > summary { color: var(--ochre); }
.block-detail > .body > .fold > summary:hover { color: #7a4a0d; }

.block-detail > .body > .fold {
  padding: 0.3rem 0;
  border-bottom: 1px solid var(--rule-2);
}

.block-detail > .body > .fold:last-child { border-bottom: 0; }

/* свёртка */

.fold { border: 0; padding: 0; }

.fold > summary {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--indigo);
  cursor: pointer;
  font-size: 0.9rem;
  list-style: none;
  user-select: none;
}

.fold > summary::-webkit-details-marker { display: none; }

.fold > summary::before {
  content: "";
  width: 0.4rem; height: 0.4rem;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg) translate(-1px, 1px);
  transition: transform .18s ease;
}

.fold[open] > summary::before { transform: rotate(45deg) translate(-2px, -1px); }
.fold > summary:hover { color: #16307d; }
.fold[open] > summary { margin-bottom: calc(var(--step) * 1.5); }

/* шаги решения */

.steps { margin: 0; }

.step {
  position: relative;
  padding: 0.55rem 0 0.55rem 1rem;
  border-left: 1px solid var(--rule);
}

.step::before {
  content: "";
  position: absolute;
  left: -3px; top: 1.05rem;
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--rule);
}

.step-title { font-size: 0.9375rem; color: var(--ink); }
.step-note { margin-left: 0.5rem; color: var(--muted); font-size: 0.875rem; }

.step-math {
  margin-top: 0.2rem;
  color: var(--ink-2);
  overflow-x: auto;
}

.step-math .katex { font-size: 1em; }
.step-eq { padding: 0 0.45rem; color: var(--muted); }

/* график */

.figure { position: relative; margin: 0; }
.figure > svg { display: block; width: 100%; height: auto; }

/* На узком экране кадр 700px сжимается вдвое — подписи и штрих
   компенсируем в системе координат самого SVG. */
@media (max-width: 900px) {
  .figure > svg { font-size: 18px; }
  .figure > svg path { stroke-width: 2.8; }
}

.probe {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity .15s ease;
}

.figure.live .probe { opacity: 1; }

.probe-line {
  position: absolute;
  top: 4%; bottom: 8%;
  width: 1px;
  background: var(--indigo);
  opacity: 0.35;
}

.probe-dot {
  position: absolute;
  width: 7px; height: 7px;
  margin: -3.5px 0 0 -3.5px;
  border: 1.5px solid var(--indigo);
  border-radius: 50%;
  background: #fff;
}

.probe-tag {
  position: absolute;
  transform: translate(-50%, -2rem);
  padding: 0.15rem 0.4rem;
  border-radius: 2px;
  background: var(--ink);
  color: #fff;
  font-family: var(--mono);
  font-size: 0.6875rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.probe-tag.below { transform: translate(-50%, 0.9rem); }

/* состояния */

.pending {
  position: relative;
  max-width: 54rem;
  margin-bottom: 3rem;
  padding: calc(var(--step) * 3) calc(var(--step) * 5);
  border: 1px solid var(--rule);
  background: var(--sheet);
  color: var(--muted);
  overflow: hidden;
}

.pending-label { display: flex; align-items: baseline; gap: 0.75rem; }
.pending-label span { font-family: var(--mono); font-size: 0.8125rem; }

.scan {
  position: absolute;
  left: 0; right: 0; top: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--indigo), transparent);
  opacity: 0.5;
  animation: scan 1.6s cubic-bezier(.5, 0, .5, 1) infinite;
}

@keyframes scan {
  0%   { transform: translateY(0); opacity: 0; }
  15%  { opacity: 0.55; }
  85%  { opacity: 0.55; }
  100% { transform: translateY(5.5rem); opacity: 0; }
}

/* отказ несут метка, цвет текста и верхняя линейка — не цветная полоса сбоку */
.failure { border-top: 2px solid var(--wine); }
.failure .mark { color: var(--wine); }
.failure .text { color: #6f2231; }

/* появление разбора */

.sheet, .pending { animation: rise .34s cubic-bezier(.16, 1, .3, 1) both; }
.block { animation: rise .3s cubic-bezier(.16, 1, .3, 1) both; }

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

/* ── знакомство ───────────────────────────────────────────────────────── */

.intro { max-width: 54rem; }

.intro-head {
  display: grid;
  grid-template-columns: 7.5rem minmax(0, 1fr);
  gap: 0 calc(var(--step) * 3);
  margin-bottom: calc(var(--step) * 5);
}

.intro-lede {
  margin: 0;
  max-width: 40rem;
  font-size: 1.1875rem;
  line-height: 1.5;
  text-wrap: pretty;
}

.samples {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(13.5rem, 1fr));
  gap: 1px;
  margin-bottom: calc(var(--step) * 6);
  border: 1px solid var(--rule);
  background: var(--rule);
}

.sample {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 6.5rem;
  padding: calc(var(--step) * 2);
  border: 0;
  background: var(--sheet);
  color: var(--ink);
  cursor: pointer;
  transition: background .16s ease, color .16s ease;
}

.sample:hover { background: #fff; color: var(--indigo); }
.sample:active { background: var(--indigo-soft); }
.sample .katex { font-size: 1.15em; }

.branches {
  display: grid;
  grid-template-columns: 7.5rem minmax(0, 1fr);
  gap: 0 calc(var(--step) * 3);
  margin-bottom: calc(var(--step) * 5);
}

.branches p:last-child {
  margin: 0;
  max-width: 44rem;
  color: var(--ink-2);
  line-height: 1.7;
}

.howto { margin: 0; padding: 0; list-style: none; }

.howto li {
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--rule-2);
  line-height: 1.45;
}

.howto li:last-child { border-bottom: 0; }

.howto li span {
  display: block;
  margin-top: 0.15rem;
  color: var(--muted);
  font-size: 0.8125rem;
}

code {
  padding: 0.1em 0.35em;
  border-radius: 2px;
  background: #f0ece1;
  color: var(--ink-2);
  font-family: var(--mono);
  font-size: 0.8125rem;
}

.grammar li > code { background: #eeeadf; color: var(--ink); }

/* ── адаптация ────────────────────────────────────────────────────────── */

@media (max-width: 1180px) {
  :root { --pult-w: 21.5rem; }
  /* колонка сузилась — длинный ответ упирался в край листа */
  .block-answer .math .katex { font-size: 1.38em; }
  .protocol { padding: calc(var(--step) * 5) calc(var(--step) * 4) calc(var(--step) * 10); }
  .block, .intro-head, .branches {
    grid-template-columns: 6rem minmax(0, 1fr);
    gap: 0 calc(var(--step) * 2);
  }
}

@media (max-width: 900px) {
  /* Порядок документа важнее колонок: ввод, разбор, и только потом
     переменные, история и колофон — иначе подвал стоит между вопросом и ответом. */
  .deck { grid-template-columns: 1fr; }
  .pult, .pult-inner { display: contents; }

  .brand, #console, .stack, .pult-foot {
    padding-left: calc(var(--step) * 3);
    padding-right: calc(var(--step) * 3);
  }

  .brand {
    order: 1;
    padding-top: calc(var(--step) * 4);
    padding-bottom: calc(var(--step) * 3);
    background: var(--pult);
  }

  #console {
    order: 2;
    padding-bottom: calc(var(--step) * 4);
    border-bottom: 1px solid var(--rule);
    background: var(--pult);
  }

  .protocol { order: 3; padding: calc(var(--step) * 4) calc(var(--step) * 3) calc(var(--step) * 6); }

  #howto { order: 4; }
  #vars { order: 5; }
  #log { order: 6; }
  .stack { order: 4; padding-top: calc(var(--step) * 3); border-top: 1px solid var(--rule); background: var(--pult); }
  #vars, #log { border-top: 0; padding-top: calc(var(--step) * 1); }

  .pult-foot {
    order: 7;
    margin-top: 0;
    padding-top: calc(var(--step) * 3);
    padding-bottom: calc(var(--step) * 5);
    background: var(--pult);
  }

  .sheet { padding: calc(var(--step) * 3) calc(var(--step) * 3); }

  .block, .intro-head, .branches {
    grid-template-columns: minmax(0, 1fr);
    gap: calc(var(--step) * 0.75) 0;
  }

  /* без колонки полей метка перестаёт быть маргиналией — и не должна
     превращаться в надстрочник: строчными, но плотнее и темнее примечания,
     иначе метка и курсивная сноска сливаются в один ранг */
  .mark {
    padding-top: 0;
    color: var(--ink-2);
    font-size: 0.8125rem;
    font-weight: 600;
    font-style: normal;
    letter-spacing: 0;
    text-transform: none;
  }

  .block-detail .mark { color: var(--ochre); }
  .failure .mark { color: var(--wine); }

  .echo { min-height: 0; }
  .block-answer .math .katex { font-size: 1.5em; }
  .block-input .math .katex { font-size: 0.95em; }
  .intro-lede { font-size: 1.0625rem; }
  .samples { grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr)); }
  .sample { min-height: 4.5rem; }
}

@media (max-width: 480px) {
  .brand, #console, .stack, .pult-foot {
    padding-left: calc(var(--step) * 2.5);
    padding-right: calc(var(--step) * 2.5);
  }

  .protocol { padding: calc(var(--step) * 3) calc(var(--step) * 2) calc(var(--step) * 6); }
  .sheet { padding: calc(var(--step) * 2.5) calc(var(--step) * 2); }
  .brand h1 { font-size: 1.375rem; }
  .block-answer .math .katex { font-size: 1.4em; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .scan { display: none; }
}
