/* "Special content box" family: math callouts (problem/solution/example),
 * statement environments (theorem/definition/axiom/proposition/lemma/
 * corollary), and admonitions (note/info/warning/danger/attention). Grouped
 * together because they share the same visual pattern (a labeled box with
 * a pill title) and several combined selectors. */

/* Mathematical callouts */
div.problem,
div.math-solution,
div.eg {
  --callout-accent: #6d28d9;
  --callout-accent-dark: #4c1d95;
  --callout-background: #f7f3ff;

  position: relative;
  box-sizing: border-box;
  margin: 1.75rem 0;
  padding: 1.5rem 1.25rem 1.1rem;

  color: inherit;
  background: var(--callout-background);
  border: 1px solid #d8dbe3;
  border: 1px solid color-mix(in srgb, var(--callout-accent) 28%, white);
  border-left: 4px solid var(--callout-accent);
  border-radius: 0.65rem;
  box-shadow: 0 2px 8px rgba(31, 41, 55, 0.06);
}

div.problem {
  --callout-accent: #2563eb;
  --callout-accent-dark: #1e40af;
  --callout-background: #eff6ff;
}

div.math-solution {
  --callout-accent: #16803a;
  --callout-accent-dark: #166534;
  --callout-background: #f0fdf4;
}

div.eg {
  --callout-accent: #64748b;
  --callout-accent-dark: #475569;
  --callout-background: #f8fafc;

  margin-block: 1.4rem;
  border-left-width: 3px;
  box-shadow: none;
}

div.problem:not(.math-callout)::before,
div.math-solution:not(.math-callout)::before,
div.eg:not(.math-callout)::before,
.math-callout-toggle {
  position: absolute;
  top: -0.8rem;
  left: 1rem;
  z-index: 1;

  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  min-height: 1.6rem;
  box-sizing: border-box;
  padding: 0.2rem 0.7rem;

  color: #fff;
  background: var(--callout-accent);
  border: 0;
  border-radius: 999px;
  box-shadow: 0 2px 5px rgba(31, 41, 55, 0.14);

  font: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0.025em;
}

div.problem:not(.math-callout)::before {
  content: "Problem";
}

div.math-solution:not(.math-callout)::before {
  content: "Solution";
}

div.eg:not(.math-callout)::before {
  content: "Example";
}

.math-callout-toggle {
  cursor: pointer;
}

.math-callout-toggle::after {
  content: "⌄";
  display: inline-block;
  font-size: 0.95rem;
  line-height: 1;
  transform: rotate(0deg);
  transition: transform 0.22s ease;
}

.math-callout-toggle[aria-expanded="false"]::after {
  transform: rotate(-90deg);
}

.math-callout-toggle:hover {
  background: var(--callout-accent-dark);
}

.math-callout-toggle:focus-visible {
  outline: 3px solid #bfdbfe;
  outline: 3px solid color-mix(in srgb, var(--callout-accent) 35%, white);
  outline-offset: 3px;
}

.math-callout-content {
  display: grid;
  grid-template-rows: 1fr;
  opacity: 1;
  visibility: visible;
  transition:
    grid-template-rows 0.25s ease,
    opacity 0.18s ease,
    visibility 0s linear;
}

.math-callout-content-inner {
  min-height: 0;
  overflow: hidden;
}

.math-callout.is-collapsed .math-callout-content {
  grid-template-rows: 0fr;
  opacity: 0;
  visibility: hidden;
  transition:
    grid-template-rows 0.25s ease,
    opacity 0.18s ease,
    visibility 0s linear 0.25s;
}

div.problem.is-collapsed,
div.math-solution.is-collapsed,
div.eg.is-collapsed {
  padding-bottom: 0.85rem;
}

div.problem .math-callout-content-inner > :first-child,
div.math-solution .math-callout-content-inner > :first-child,
div.eg .math-callout-content-inner > :first-child,
div.definition > :first-child,
div.axiom > :first-child,
div.proposition > :first-child,
div.theorem > :first-child,
div.lemma > :first-child,
div.corollary > :first-child {
  margin-top: 0;
}

div.problem .math-callout-content-inner > :last-child,
div.math-solution .math-callout-content-inner > :last-child,
div.eg .math-callout-content-inner > :last-child,
div.definition > :last-child,
div.axiom > :last-child,
div.proposition > :last-child,
div.theorem > :last-child,
div.lemma > :last-child,
div.corollary > :last-child {
  margin-bottom: 0;
}

div.problem .katex-display,
div.math-solution .katex-display,
div.eg .katex-display,
div.definition .katex-display,
div.axiom .katex-display,
div.proposition .katex-display,
div.theorem .katex-display,
div.lemma .katex-display,
div.corollary .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding-block: 0.15rem;
}

@media (max-width: 600px) {
  div.problem,
  div.math-solution,
  div.eg {
    padding-right: 0.9rem;
    padding-left: 0.9rem;
  }

  div.problem:not(.math-callout)::before,
  div.math-solution:not(.math-callout)::before,
  div.eg:not(.math-callout)::before,
  .math-callout-toggle {
    left: 0.75rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .math-callout-toggle::after,
  .math-callout-content {
    transition: none;
  }
}

/* Statement environments (definition/axiom/proposition/theorem/lemma/
 * corollary): quiet by default, styled like a print math text rather than
 * a colored alert box. The label ("Theorem.", "Lemma.", ...) is plain
 * text, not auto-numbered — every instance of a given type shows the same
 * word unless it's named. It sits inline above the statement on narrow
 * screens, and moves into the left margin once there's room for it —
 * mirroring the footnote sidenote's right margin (.org-sidenote in
 * index.css).
 *
 * Name a statement with a bold-only first paragraph, e.g. `*Lagrange's
 * Theorem*` as the first line of a #+begin_theorem block, to replace the
 * generic label with that name instead — see further down for the
 * `:has()` rule that wires this up.
 *
 * Theorems are the one environment that's auto-numbered, via a CSS counter
 * reset on `body` — so each exported page starts its own "Theorem 1" fresh,
 * independently of every other page. A named theorem still increments the
 * counter (so IDs stay in sequence around it) but shows its name instead of
 * the number, per the `:has()` rule below.
 *
 * To make a specific statement stand out (e.g. citing a theorem in a
 * solution), add `#+attr_html: :class highlight` above the block in Org;
 * see the `.highlight` rules below for the emphasized, boxed treatment.
 */
body {
  counter-reset: theorem;
}

div.definition,
div.axiom,
div.proposition,
div.theorem,
div.lemma,
div.corollary {
  --statement-accent: #52606d;

  position: relative;
  margin: 1.5rem 0;
  padding-left: 1rem;

  border-left: 3px solid var(--statement-accent);
}

div.theorem {
  counter-increment: theorem;
}

div.definition::before,
div.axiom::before,
div.proposition::before,
div.theorem::before,
div.lemma::before,
div.corollary::before,
:is(div.definition, div.axiom, div.proposition, div.theorem, div.lemma, div.corollary):has(> p:first-child > :is(b, strong):only-child) > p:first-child {
  display: block;
  margin-bottom: 0.3rem;

  color: var(--statement-accent);
  font-weight: 700;
}

div.definition::before {
  content: "Definition.";
}

div.axiom::before {
  content: "Axiom.";
}

div.proposition::before {
  content: "Proposition.";
}

div.theorem::before {
  content: "Theorem " counter(theorem) ".";
}

div.lemma::before {
  content: "Lemma.";
}

div.corollary::before {
  content: "Corollary.";
}

/* A bold-only first paragraph names the statement, replacing the generic
 * label entirely — e.g. `*Lagrange's Theorem*` as the first line of a
 * #+begin_theorem block renders as "Lagrange's Theorem." instead of
 * "Theorem." — the same convention the admonitions below use. */
:is(div.definition, div.axiom, div.proposition, div.theorem, div.lemma, div.corollary):has(> p:first-child > :is(b, strong):only-child)::before {
  content: none;
}

@media (min-width: 1280px) {
  div.definition,
  div.axiom,
  div.proposition,
  div.theorem,
  div.lemma,
  div.corollary {
    padding-left: 0;
    border-left: 0;
  }

  div.definition::before,
  div.axiom::before,
  div.proposition::before,
  div.theorem::before,
  div.lemma::before,
  div.corollary::before,
  :is(div.definition, div.axiom, div.proposition, div.theorem, div.lemma, div.corollary):has(> p:first-child > :is(b, strong):only-child) > p:first-child {
    position: absolute;
    top: 0.15em;
    right: calc(100% + 1.35rem);
    width: clamp(6rem, calc((100vw - 900px) / 2 - 2rem), 10rem);

    margin-bottom: 0;

    text-align: right;
  }
}

/* Proof blocks (#+begin_proof): paired with the theorem/definition family
 * above rather than the colored callouts below — a proof directly follows
 * a theorem, so it keeps the same quiet border-left treatment instead of
 * competing for attention with a colored box. Unlike the plain statement
 * labels, "Proof" is a real button: proofs are often skippable, so the
 * block starts collapsed and expands on click, reusing the same
 * grid-template-rows collapse mechanics as admonitions/math callouts. A
 * QED mark (∎) is appended after the last line once expanded — see
 * enhanceProofs() in article.js, which builds this structure. */
div.proof {
  --proof-accent: var(--site-accent);

  position: relative;
  margin: 1.1rem 0 1.75rem;
  padding-left: 1rem;

  border-left: 3px solid var(--proof-accent);
}

.proof-toggle {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  width: 100%;
  margin: 0;
  padding: 0;

  color: var(--proof-accent);
  background: none;
  border: 0;

  font: inherit;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}

.proof-toggle::after {
  content: "⌄";
  display: inline-block;
  font-size: 0.9em;
  line-height: 1;
  transition: transform 0.22s ease;
}

.proof-toggle[aria-expanded="false"]::after {
  transform: rotate(-90deg);
}

.proof-toggle:hover {
  color: color-mix(in srgb, var(--proof-accent) 75%, black);
}

.proof-toggle:focus-visible {
  outline: 3px solid #bfdbfe;
  outline: 3px solid color-mix(in srgb, var(--proof-accent) 35%, white);
  outline-offset: 3px;
}

.proof-content {
  display: grid;
  grid-template-rows: 1fr;
  opacity: 1;
  visibility: visible;
  transition:
    grid-template-rows 0.25s ease,
    opacity 0.18s ease,
    visibility 0s linear;
}

div.proof.is-collapsed .proof-content {
  grid-template-rows: 0fr;
  opacity: 0;
  visibility: hidden;
  transition:
    grid-template-rows 0.25s ease,
    opacity 0.18s ease,
    visibility 0s linear 0.25s;
}

.proof-content-inner {
  min-height: 0;
  overflow: hidden;
  padding-top: 0.5rem;
}

.proof-content-inner > :first-child {
  margin-top: 0;
}

.proof-content-inner > :last-child {
  margin-bottom: 0;
}

.proof-qed {
  display: block;
  margin-top: 0.4rem;
  color: var(--proof-accent);
  font-size: 1.15em;
  text-align: right;
}

.proof .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding-block: 0.15rem;
}

@media (min-width: 1280px) {
  div.proof {
    padding-left: 0;
    border-left: 0;
  }

  .proof-toggle {
    position: absolute;
    top: 0.15em;
    right: calc(100% + 1.35rem);
    width: clamp(6rem, calc((100vw - 900px) / 2 - 2rem), 10rem);

    margin-bottom: 0;

    justify-content: flex-end;
    text-align: right;
  }
}

@media print {
  div.proof {
    padding-bottom: 0;
  }

  .proof-toggle::after {
    display: none;
  }

  .proof-content {
    grid-template-rows: 1fr !important;
    opacity: 1 !important;
    visibility: visible !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  .proof-toggle::after,
  .proof-content {
    transition: none;
  }
}

/* Opt-in emphasis: recreates the colored callout box + pill title for one
 * statement, at any screen width, when it deserves to stand out. The two
 * classes in these selectors give them higher specificity than the quiet
 * rules above (including its min-width: 1280px override), so they win
 * regardless of source order. */
div.definition.highlight,
div.axiom.highlight,
div.proposition.highlight,
div.theorem.highlight,
div.lemma.highlight,
div.corollary.highlight {
  --callout-accent: #6d28d9;
  --callout-accent-dark: #4c1d95;
  --callout-background: #f7f3ff;

  box-sizing: border-box;
  margin: 1.75rem 0;
  padding: 1.5rem 1.25rem 1.1rem;

  background: var(--callout-background);
  border: 1px solid #d8dbe3;
  border: 1px solid color-mix(in srgb, var(--callout-accent) 28%, white);
  border-left: 4px solid var(--callout-accent);
  border-radius: 0.65rem;
  box-shadow: 0 2px 8px rgba(31, 41, 55, 0.06);
}

div.definition.highlight {
  --callout-accent: #0f766e;
  --callout-accent-dark: #115e59;
  --callout-background: #f0fdfa;
}

div.definition.highlight::before,
div.axiom.highlight::before,
div.proposition.highlight::before,
div.theorem.highlight::before,
div.lemma.highlight::before,
div.corollary.highlight::before,
:is(div.definition.highlight, div.axiom.highlight, div.proposition.highlight, div.theorem.highlight, div.lemma.highlight, div.corollary.highlight):has(> p:first-child > :is(b, strong):only-child) > p:first-child {
  position: absolute;
  top: -0.8rem;
  left: 1rem;
  right: auto;
  z-index: 1;

  display: inline-flex;
  align-items: center;
  width: auto;
  min-height: 1.6rem;
  box-sizing: border-box;
  margin-bottom: 0;
  padding: 0.2rem 0.7rem;

  color: #fff;
  background: var(--callout-accent);
  border-radius: 999px;
  box-shadow: 0 2px 5px rgba(31, 41, 55, 0.14);

  font-weight: 700;
  text-align: left;
}

@media (max-width: 600px) {
  div.definition.highlight,
  div.axiom.highlight,
  div.proposition.highlight,
  div.theorem.highlight,
  div.lemma.highlight,
  div.corollary.highlight {
    padding-right: 0.9rem;
    padding-left: 0.9rem;
  }

  div.definition.highlight::before,
  div.axiom.highlight::before,
  div.proposition.highlight::before,
  div.theorem.highlight::before,
  div.lemma.highlight::before,
  div.corollary.highlight::before,
  :is(div.definition.highlight, div.axiom.highlight, div.proposition.highlight, div.theorem.highlight, div.lemma.highlight, div.corollary.highlight):has(> p:first-child > :is(b, strong):only-child) > p:first-child {
    left: 0.75rem;
  }
}

/* General admonitions
 *
 * Org authoring:
 *   #+begin_warning
 *   *Optional custom title*
 *
 *   Callout body.
 *   #+end_warning
 *
 * A bold-only first paragraph becomes the title. Without it, the block type
 * (Note, Info, Warning, Danger, or Attention) is used as the title.
 */
:is(div.note, div.info, div.warning, div.danger, div.attention) {
  --admonition-accent: #52606d;
  --admonition-accent-dark: #3e4c59;
  --admonition-background: #f5f7f9;

  position: relative;
  box-sizing: border-box;
  margin: 1.75rem 0;
  padding: 1.55rem 1.25rem 1.1rem;

  color: inherit;
  background: var(--admonition-background);
  border: 1px solid #d8dbe3;
  border: 1px solid color-mix(in srgb, var(--admonition-accent) 28%, white);
  border-left: 4px solid var(--admonition-accent);
  border-radius: 0.65rem;
  box-shadow: 0 2px 8px rgba(31, 41, 55, 0.06);
}

div.info {
  --admonition-accent: #2563eb;
  --admonition-accent-dark: #1d4ed8;
  --admonition-background: #eff6ff;
}

div.warning {
  --admonition-accent: #b45309;
  --admonition-accent-dark: #92400e;
  --admonition-background: #fffbeb;
}

div.danger {
  --admonition-accent: #b91c1c;
  --admonition-accent-dark: #991b1b;
  --admonition-background: #fef2f2;
}

div.attention {
  --admonition-accent: #7c3aed;
  --admonition-accent-dark: #6d28d9;
  --admonition-background: #f5f3ff;
}

.admonition-toggle,
:is(div.note, div.info, div.warning, div.danger, div.attention):not(.admonition)::before,
:is(div.note, div.info, div.warning, div.danger, div.attention):not(.admonition):has(> p:first-child > :is(b, strong):only-child) > p:first-child {
  position: absolute;
  top: -0.8rem;
  left: 1rem;
  z-index: 1;

  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  max-width: calc(100% - 2rem);
  min-height: 1.6rem;
  box-sizing: border-box;
  margin: 0;
  padding: 0.2rem 0.7rem;

  color: #fff;
  background: var(--admonition-accent);
  border: 0;
  border-radius: 999px;
  box-shadow: 0 2px 5px rgba(31, 41, 55, 0.14);

  font: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0.025em;
}

div.note:not(.admonition)::before {
  content: "Note";
}

div.info:not(.admonition)::before {
  content: "Info";
}

div.warning:not(.admonition)::before {
  content: "Warning";
}

div.danger:not(.admonition)::before {
  content: "Danger";
}

div.attention:not(.admonition)::before {
  content: "Attention";
}

:is(div.note, div.info, div.warning, div.danger, div.attention):not(.admonition):has(> p:first-child > :is(b, strong):only-child)::before {
  content: none;
}

.admonition-toggle {
  cursor: pointer;
}

.admonition-toggle:hover {
  background: var(--admonition-accent-dark);
}

.admonition-toggle:focus-visible {
  outline: 3px solid #bfdbfe;
  outline: 3px solid color-mix(in srgb, var(--admonition-accent) 35%, white);
  outline-offset: 3px;
}

.admonition-icon {
  width: 1rem;
  height: 1rem;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.admonition-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.admonition-toggle::after {
  content: "⌄";
  display: inline-block;
  flex: 0 0 auto;
  font-size: 0.95rem;
  line-height: 1;
  transform: rotate(0deg);
  transition: transform 0.22s ease;
}

.admonition-toggle[aria-expanded="false"]::after {
  transform: rotate(-90deg);
}

.admonition-content {
  display: grid;
  grid-template-rows: 1fr;
  opacity: 1;
  visibility: visible;
  transition:
    grid-template-rows 0.25s ease,
    opacity 0.18s ease,
    visibility 0s linear;
}

.admonition-content-inner {
  min-height: 0;
  overflow: hidden;
}

.admonition.is-collapsed {
  padding-bottom: 0.85rem;
}

.admonition.is-collapsed .admonition-content {
  grid-template-rows: 0fr;
  opacity: 0;
  visibility: hidden;
  transition:
    grid-template-rows 0.25s ease,
    opacity 0.18s ease,
    visibility 0s linear 0.25s;
}

.admonition-content-inner > :first-child {
  margin-top: 0;
}

.admonition-content-inner > :last-child {
  margin-bottom: 0;
}

.admonition .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding-block: 0.15rem;
}

@media (max-width: 600px) {
  :is(div.note, div.info, div.warning, div.danger, div.attention) {
    padding-right: 0.9rem;
    padding-left: 0.9rem;
  }

  .admonition-toggle,
  :is(div.note, div.info, div.warning, div.danger, div.attention):not(.admonition)::before,
  :is(div.note, div.info, div.warning, div.danger, div.attention):not(.admonition):has(> p:first-child > :is(b, strong):only-child) > p:first-child {
    left: 0.75rem;
    max-width: calc(100% - 1.5rem);
  }
}

@media print {
  .admonition {
    padding-bottom: 1.1rem;
  }

  .admonition-toggle::after {
    display: none;
  }

  .admonition .admonition-content {
    grid-template-rows: 1fr !important;
    opacity: 1 !important;
    visibility: visible !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  .admonition-toggle::after,
  .admonition-content {
    transition: none;
  }
}
