/* Электронный учебник: анализ эффективности алгоритмов */

:root {
  --bg: #fbfbfa;
  --surface: #ffffff;
  --fg: #1c1c1e;
  --muted: #66666d;
  --line: #e4e4e0;
  --accent: #2f5bd3;
  --accent-ink: #ffffff;
  --accent-soft: rgba(47, 91, 211, 0.09);
  --accent-mid: rgba(47, 91, 211, 0.35);
  --code-bg: #f3f3f0;
  --ok: #2e7d4f;
  --bad: #b3412f;
  --ok-soft: rgba(46, 125, 79, 0.10);
  --bad-soft: rgba(179, 65, 47, 0.09);
  --mark: rgba(47, 91, 211, 0.14);
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "JetBrains Mono", "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
  --measure: 70ch;
  --header-h: 52px;
  --radius: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141416;
    --surface: #1b1b1e;
    --fg: #e6e6e9;
    --muted: #9b9ba3;
    --line: #2c2c31;
    --accent: #8aa6ff;
    --accent-ink: #10131c;
    --accent-soft: rgba(138, 166, 255, 0.12);
    --accent-mid: rgba(138, 166, 255, 0.40);
    --code-bg: #1e1e22;
    --ok: #6cc28f;
    --bad: #f08a78;
    --ok-soft: rgba(108, 194, 143, 0.12);
    --bad-soft: rgba(240, 138, 120, 0.12);
    --mark: rgba(138, 166, 255, 0.20);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141416;
  --surface: #1b1b1e;
  --fg: #e6e6e9;
  --muted: #9b9ba3;
  --line: #2c2c31;
  --accent: #8aa6ff;
  --accent-ink: #10131c;
  --accent-soft: rgba(138, 166, 255, 0.12);
  --accent-mid: rgba(138, 166, 255, 0.40);
  --code-bg: #1e1e22;
  --ok: #6cc28f;
  --bad: #f08a78;
  --ok-soft: rgba(108, 194, 143, 0.12);
  --bad-soft: rgba(240, 138, 120, 0.12);
  --mark: rgba(138, 166, 255, 0.20);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html { scroll-padding-top: calc(var(--header-h) + 16px); -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.65;
  overflow-x: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }

h1, h2, h3, h4 { line-height: 1.25; font-weight: 650; letter-spacing: -0.01em; position: relative; }
h1 { font-size: 2rem; margin: 0 0 0.5rem; }
h2 { font-size: 1.45rem; margin: 3.2rem 0 0.9rem; padding-top: 0.2rem; }
h3 { font-size: 1.15rem; margin: 2.2rem 0 0.6rem; }
h4 { font-size: 1rem; margin: 1.6rem 0 0.4rem; }
p, ul, ol { margin: 0 0 1rem; }
li { margin: 0.2rem 0; }
strong { font-weight: 620; }
hr { border: 0; border-top: 1px solid var(--line); margin: 2.5rem 0; }

.anchor {
  margin-left: 0.4em;
  color: var(--muted);
  opacity: 0;
  font-weight: 400;
  text-decoration: none;
  transition: opacity 0.15s;
}
h2:hover .anchor, h3:hover .anchor, h4:hover .anchor, .anchor:focus { opacity: 1; }
@media (hover: none) { .anchor { opacity: 0.45; } }

/* ---------- шапка ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--header-h);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  -webkit-backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--line);
}
.site-header .inner {
  max-width: 1180px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 1.2rem;
  padding: 0 16px;
}
.brand { font-weight: 650; color: var(--fg); white-space: nowrap; font-size: 0.95rem; }
.brand:hover { text-decoration: none; color: var(--accent); }
.tabs {
  display: flex;
  gap: 0.15rem;
  overflow-x: auto;
  scrollbar-width: none;
  margin-left: auto;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  color: var(--muted);
  padding: 0.35rem 0.7rem;
  border-radius: 6px;
  font-size: 0.92rem;
  white-space: nowrap;
}
.tabs a:hover { color: var(--fg); background: var(--accent-soft); text-decoration: none; }
.lang-switch {
  flex: none;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.2rem 0.45rem;
}
.lang-switch:hover { color: var(--accent); border-color: var(--accent-mid); text-decoration: none; }
.tabs a[aria-current="page"] { color: var(--accent); background: var(--accent-soft); font-weight: 600; }

@media (max-width: 720px) {
  .brand { display: none; }
  .tabs { margin-left: 0; width: 100%; justify-content: space-between; }
  .tabs a { padding: 0.35rem 0.45rem; font-size: 0.86rem; }
}
@media (max-width: 420px) {
  .site-header .inner { padding: 0 8px; }
  .tabs { gap: 0; }
  .tabs a { padding: 0.3rem 0.32rem; font-size: 0.78rem; }
}

/* ---------- раскладка ---------- */
.layout {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 16px;
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr);
  gap: 3rem;
}
.layout.single { grid-template-columns: minmax(0, 1fr); }
main { min-width: 0; width: 100%; max-width: var(--measure); padding: 2.2rem 0 4rem; }
.layout.single main { margin: 0 auto; }
main.wide { max-width: 860px; }

.toc {
  position: sticky;
  top: calc(var(--header-h) + 1.5rem);
  align-self: start;
  max-height: calc(100vh - var(--header-h) - 3rem);
  overflow-y: auto;
  padding: 2.2rem 0 2rem;
  font-size: 0.88rem;
}
.toc-title { color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 0.6rem; }
.toc ol { list-style: none; margin: 0; padding: 0; border-left: 1px solid var(--line); }
.toc li { margin: 0; }
.toc a {
  display: block;
  color: var(--muted);
  padding: 0.28rem 0 0.28rem 0.9rem;
  margin-left: -1px;
  border-left: 2px solid transparent;
  line-height: 1.35;
}
.toc a:hover { color: var(--fg); text-decoration: none; }
.toc a.active { color: var(--accent); border-left-color: var(--accent); font-weight: 600; }
.toc-toggle { display: none; }

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); gap: 0; }
  main { margin: 0 auto; padding-top: 1.2rem; }
  .toc {
    position: sticky;
    top: var(--header-h);
    z-index: 15;
    max-height: none;
    overflow: visible;
    padding: 0;
    margin: 0 -16px;
    background: var(--bg);
    border-bottom: 1px solid var(--line);
  }
  .toc-title { display: none; }
  .toc-toggle {
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.6rem 16px;
    background: none;
    border: 0;
    color: var(--fg);
    font: inherit;
    font-size: 0.92rem;
    cursor: pointer;
  }
  .toc-toggle > span:first-child { display: flex; gap: 0.35rem; min-width: 0; white-space: nowrap; text-align: left; }
  .toc-toggle .current { color: var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
  .toc-toggle .chev { color: var(--muted); flex: none; }
  .toc ol {
    display: none;
    max-height: 60vh;
    overflow-y: auto;
    border-left: 0;
    border-top: 1px solid var(--line);
    padding: 0.4rem 0;
  }
  .toc.open ol { display: block; }
  .toc a { padding: 0.45rem 16px; border-left: 0; }
}

/* ---------- текстовые элементы ---------- */
.lead { font-size: 1.1rem; color: var(--muted); }
.kicker { color: var(--muted); font-size: 0.85rem; letter-spacing: 0.04em; text-transform: uppercase; margin-bottom: 0.3rem; }
.muted { color: var(--muted); }
.small { font-size: 0.9rem; }

.term { color: inherit; border-bottom: 1px dotted var(--accent-mid); }
.term:hover { color: var(--accent); text-decoration: none; border-bottom-color: var(--accent); }
.tip {
  position: fixed;
  z-index: 50;
  max-width: 300px;
  padding: 0.55rem 0.75rem;
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  font-size: 0.86rem;
  line-height: 1.45;
  pointer-events: none;
}
.tip b { display: block; margin-bottom: 0.15rem; }

.def {
  border-left: 2px solid var(--accent);
  padding: 0.1rem 0 0.1rem 1rem;
  margin: 1.2rem 0;
}
.def > :last-child { margin-bottom: 0; }

.steps { counter-reset: step; list-style: none; padding: 0; }
.steps > li { counter-increment: step; position: relative; padding-left: 2.2rem; margin: 0.7rem 0; }
.steps > li::before {
  content: counter(step);
  position: absolute;
  left: 0;
  top: 0.05rem;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  border: 1px solid var(--accent-mid);
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 600;
  display: grid;
  place-items: center;
}

/* шаги разбора по плану */
.plan-walk { margin: 1rem 0 1.4rem; border-top: 1px solid var(--line); }
.plan-walk > div {
  display: grid;
  grid-template-columns: 9.5rem minmax(0, 1fr);
  gap: 1rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--line);
}
.plan-walk dt, .plan-walk .k { color: var(--muted); font-size: 0.9rem; padding-top: 0.1rem; }
.plan-walk .v > :last-child { margin-bottom: 0; }
@media (max-width: 560px) {
  .plan-walk > div { grid-template-columns: minmax(0, 1fr); gap: 0.15rem; }
}

table { border-collapse: collapse; width: 100%; margin: 1rem 0 1.4rem; font-size: 0.93rem; }
th, td { text-align: left; padding: 0.45rem 0.6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-weight: 600; color: var(--muted); font-size: 0.85rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; max-width: 100%; }

/* ---------- код ---------- */
pre {
  background: var(--code-bg);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.86rem;
  line-height: 1.55;
  margin: 1rem 0 1.3rem;
  tab-size: 4;
}
code { font-family: var(--mono); font-size: 0.88em; }
:not(pre) > code { background: var(--code-bg); padding: 0.1em 0.35em; border-radius: 4px; }
pre code { background: none; padding: 0; font-size: inherit; }
pre.pseudo { white-space: pre; }
pre.pseudo .kw { font-weight: 700; }
pre.pseudo .cm { color: var(--muted); }

.hljs-keyword, .hljs-built_in, .hljs-literal { color: var(--accent); font-weight: 600; }
.hljs-comment { color: var(--muted); font-style: italic; }
.hljs-string { color: var(--ok); }
.hljs-number { color: var(--bad); }
.hljs-title, .hljs-title.function_ { font-weight: 600; }

/* ---------- формулы ---------- */
.katex { font-size: 1.05em; }
.katex-display { margin: 0.9rem 0 1.1rem; overflow-x: auto; overflow-y: hidden; padding: 0.2rem 0; }

/* ---------- кнопки ---------- */
button, .btn {
  font: inherit;
  font-size: 0.9rem;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.38rem 0.8rem;
  cursor: pointer;
  line-height: 1.3;
}
button:hover:not(:disabled) { border-color: var(--accent-mid); color: var(--accent); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover:not(:disabled) { color: var(--accent-ink); filter: brightness(1.08); }
button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent-mid); color: var(--accent); }

input[type="range"] { accent-color: var(--accent); }
select, input[type="number"] {
  font: inherit;
  font-size: 0.9rem;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.3rem 0.45rem;
}
input[type="number"] { width: 5.5rem; }
label { font-size: 0.9rem; color: var(--muted); }

/* ---------- раскрывающиеся блоки ---------- */
details { border-top: 1px solid var(--line); }
details:last-of-type { border-bottom: 1px solid var(--line); }
details > summary {
  cursor: pointer;
  padding: 0.55rem 0;
  list-style: none;
  color: var(--accent);
  font-size: 0.94rem;
  user-select: none;
}
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "+"; display: inline-block; width: 1.1rem; color: var(--muted); }
details[open] > summary::before { content: "−"; }
details > .body { padding: 0 0 0.9rem 1.1rem; }
details > .body > :last-child { margin-bottom: 0; }

/* ---------- задачи ---------- */
.task-group { margin-top: 1rem; }
.task { margin: 1.8rem 0 2.2rem; }
.task h4 { margin: 0 0 0.5rem; font-size: 1.02rem; }
.task .tag { color: var(--muted); font-weight: 500; margin-right: 0.35rem; }
.task .statement > :last-child { margin-bottom: 0.8rem; }

/* ---------- квиз ---------- */
.quiz { margin: 1rem 0 2rem; }
.q { margin: 0 0 1.8rem; }
.q-head { font-weight: 560; margin-bottom: 0.6rem; }
.q-head .n { color: var(--muted); font-weight: 500; margin-right: 0.3rem; }
.q-head > p { display: inline; margin: 0; }
.opts { list-style: none; padding: 0; margin: 0; display: grid; gap: 0.4rem; }
.opt {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.55rem 0.8rem;
  border-radius: var(--radius);
  font-size: 0.95rem;
  line-height: 1.5;
}
.opt .why { display: none; font-size: 0.88rem; color: var(--muted); margin-top: 0.3rem; }
.q.done .opt { cursor: default; }
.q.done .opt .why { display: block; }
.q.done .opt.right { border-color: var(--ok); background: var(--ok-soft); color: var(--fg); }
.q.done .opt.chosen.wrong { border-color: var(--bad); background: var(--bad-soft); color: var(--fg); }
.q.done .opt.right::after, .q.done .opt.chosen.wrong::after { display: block; font-size: 0.82rem; margin-top: 0.25rem; font-weight: 600; }
.q.done .opt.right::after { content: "верно"; color: var(--ok); }
.q.done .opt.chosen.wrong::after { content: "ваш ответ — неверно"; color: var(--bad); }
html[lang="en"] .q.done .opt.right::after { content: "correct"; }
html[lang="en"] .q.done .opt.chosen.wrong::after { content: "your answer — wrong"; }
.q.done .opt:hover { color: var(--fg); }
.quiz-foot { display: flex; flex-wrap: wrap; gap: 0.8rem; align-items: center; padding-top: 0.6rem; border-top: 1px solid var(--line); }
.quiz-score { font-weight: 600; }

/* ---------- визуализации ---------- */
.viz { margin: 1.5rem 0 2rem; display: flex; flex-direction: column; }
.viz > figcaption { order: 99; }
.viz-stage {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.8rem;
  overflow: hidden;
}
.viz svg { display: block; width: 100%; height: auto; max-width: 100%; touch-action: manipulation; }
.viz-controls { display: flex; flex-wrap: wrap; gap: 0.45rem; align-items: center; margin-top: 0.7rem; }
.viz-controls .spacer { flex: 1; }
.viz-controls label { display: inline-flex; align-items: center; gap: 0.35rem; }
.viz-status { font-size: 0.9rem; color: var(--muted); margin-top: 0.5rem; font-variant-numeric: tabular-nums; min-height: 1.4em; }
.viz-status b { color: var(--fg); font-weight: 600; }
.viz figcaption, figure figcaption { font-size: 0.88rem; color: var(--muted); margin-top: 0.5rem; }
figure { margin: 1.5rem 0; }
figure img { display: block; max-width: 100%; height: auto; }

svg text { font-family: var(--font); fill: var(--fg); }
svg .muted-text { fill: var(--muted); }

.legend { display: flex; flex-wrap: wrap; gap: 0.4rem 0.9rem; font-size: 0.86rem; color: var(--muted); }
.viz .legend svg { display: inline-block; width: auto; flex: none; }
.legend label { display: inline-flex; align-items: center; gap: 0.3rem; cursor: pointer; white-space: nowrap; }

/* Ханой */
.hanoi-grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 1rem; }
@media (max-width: 760px) { .hanoi-grid { grid-template-columns: minmax(0, 1fr); } }
.panel-title { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin: 0 0 0.4rem; }
.stack-panel { font-family: var(--mono); font-size: 0.84rem; display: flex; flex-direction: column-reverse; gap: 3px; min-height: 2rem; }
.stack-frame { padding: 0.25rem 0.5rem; border: 1px solid var(--line); border-radius: 5px; background: var(--bg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stack-frame.top { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.hanoi-big { font-size: 1.3rem; font-weight: 650; color: var(--fg); font-variant-numeric: tabular-nums; }
.hanoi-modes { display: flex; gap: 0.4rem; margin-bottom: 0.8rem; flex-wrap: wrap; }
.msg-ok { color: var(--ok); }
.msg-bad { color: var(--bad); }

.reveal-lines { list-style: none; padding: 0; margin: 1rem 0; }
.reveal-lines li { border-bottom: 1px solid var(--line); padding: 0.35rem 0; }
.reveal-lines li.hidden { display: none; }
.reveal-lines .why { color: var(--muted); font-size: 0.88rem; }

table.mtable td, table.mtable th { text-align: right; font-variant-numeric: tabular-nums; }
table.mtable td:first-child, table.mtable th:first-child { text-align: left; }
table.mtable tr.pending td { color: transparent; }
table.mtable tr.pending td:first-child { color: var(--muted); }
table.mtable tr.current td { background: var(--accent-soft); }

/* ---------- обложка ---------- */
.cover { padding: 3rem 0 1rem; }
.cover h1 { font-size: 2.4rem; max-width: 18ch; }
.cover .meta { color: var(--muted); margin-top: 1.4rem; font-size: 0.95rem; }
.toc-big { list-style: none; padding: 0; margin: 1.5rem 0; }
.toc-big > li { padding: 1.1rem 0; border-top: 1px solid var(--line); }
.toc-big > li:last-child { border-bottom: 1px solid var(--line); }
.toc-big .t { font-weight: 620; font-size: 1.08rem; }
.toc-big .d { color: var(--muted); font-size: 0.93rem; margin: 0.15rem 0 0.4rem; }
.toc-big ul { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 0.15rem 1rem; font-size: 0.9rem; }
.stats { display: flex; gap: 2rem; flex-wrap: wrap; margin: 1.5rem 0; }
.stats div { font-size: 0.9rem; color: var(--muted); }
.stats b { display: block; font-size: 1.5rem; color: var(--fg); font-weight: 650; }

/* ---------- глоссарий ---------- */
.glossary dt { font-weight: 620; margin-top: 1.3rem; }
.glossary dd { margin: 0.2rem 0 0; }
.glossary dd .see { font-size: 0.88rem; }
.glossary dt:target { color: var(--accent); }
.letter-nav { display: flex; flex-wrap: wrap; gap: 0.3rem 0.7rem; margin: 1rem 0; font-size: 0.95rem; }

/* ---------- шпаргалка ---------- */
.cheat { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 0 1.5rem; }
.cheat > div { border-top: 1px solid var(--line); padding: 0.7rem 0; }
.cheat .k { color: var(--muted); font-size: 0.85rem; }

/* ---------- подвал ---------- */
.pager {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 4rem;
  padding-top: 1.2rem;
  border-top: 1px solid var(--line);
  font-size: 0.95rem;
}
.pager a { display: flex; flex-direction: column; max-width: 48%; }
.pager a.next { text-align: right; margin-left: auto; }
.pager small { color: var(--muted); font-size: 0.8rem; }
.site-foot { color: var(--muted); font-size: 0.82rem; padding: 1.5rem 0 2.5rem; }

@media (max-width: 560px) {
  body { font-size: 16px; }
  h1 { font-size: 1.65rem; }
  h2 { font-size: 1.3rem; margin-top: 2.6rem; }
  .cover h1 { font-size: 1.9rem; }
  pre { font-size: 0.8rem; padding: 0.75rem; }
}

@media print {
  .site-header, .toc, .viz-controls, .pager { display: none; }
  .layout { display: block; }
  details { border: 0; }
}
