// =============================================================================
// Global Maths Styles
// (c) Mathigon
// =============================================================================


// TODO Sums, integrals and n-th roots
// TODO Better munder, mover and accents
// TODO Better element nesting


@mixin svg-operator($path, $width, $height) {
  background: currentColor;
  mask-image: inline-svg('<path d="#{$path}"/>', $width, $height);
  mask-size: 100% 100%;
  position: absolute;
}

@font-face {
  font-family: 'Mathigon Symbol';
  src: url('/fonts/mathigon-symbol.woff2') format('woff2'),
  url('/fonts/mathigon-symbol.woff') format('woff');
  font-weight: normal;
  font-style: normal;
}


// -----------------------------------------------------------------------------
// MathML Fonts and Styles

math, .math { display: inline-block; margin: 0 .1em; font-style: normal; font-weight: normal; }
.nowrap math, .nowrap .math { margin-right: 0; }  // before punctuation

mn, mi, mo, ms, mtext, mrow, mfrac, msqrt, mroot, msub, msup, mfenced {
  display: inline-block;
  font-size: inherit;
  line-height: 1;
  white-space: nowrap;
  font-family: 'Mathigon Symbol', 'Source Sans Pro', sans-serif;
}

mi[mathvariant="normal"] { font-family: 'Source Sans Pro', sans-serif; }
mi { line-height: .9; }
mo { margin: 0 .25em; }
mn + mi, mn + .cursor + mi { margin-left: .1em; }
mi + mi { margin-left: .05em; }
mfrac + mi, mfrac + mtext, mfrac + .coursor + mi, mfrac + .cursor + mtext{ margin-left: .15em; }

mo[value="−"]:first-child { margin: 0 .1em; }
mo[value="="], mo[value="≈"] { margin: 0 .4em; }
mo[value="∡"], mo[value="△"] { margin: 0; }

mo[lspace="0"], mo:first-child, .cursor:first-child + mo, mo + mo,
mo + mo[value], mo + .cursor + mo, mo + .cursor + mo[value],
mn + mo[value="…"], mo[value="°"], mo[value="’"] { margin-left: 0; }
mo[rspace="0"], mo:last-child { margin-right: 0; }

mtext { font-family: 'Source Sans Pro', Helvetica, sans-serif; }
mspace { padding: 0 .2em; }
mphantom { visibility: hidden; }

ms:before, ms:after { content:"\0022"; }
ms[lquote]:before { content: attr(lquote); }
ms[rquote]:after { content: attr(rquote); }


// -----------------------------------------------------------------------------
// Super and Subscripts

msup > *:last-child, msubsup > *:nth-child(3) {
  font-size: 67%;
  margin: -.2em .1em 0 .15em;
  vertical-align: top;
}

msub > *:last-child, msubsup > *:nth-child(2) {
  font-size: 67%;
  margin: 0 .1em -.2em;
  vertical-align: bottom;
}

msup > *:last-child, msub > *:last-child, msubsup > *:nth-child(3) {
  mo { margin: 0 .1em; }
}


// -----------------------------------------------------------------------------
// Fractions, Square Roots and Brackets

mfrac {
  font-size: .6em;
  vertical-align: -0.2em;
  margin: 0 .1em;

  & > * { display: block; text-align: center; }
  & > *:first-child { padding-bottom: .06em; }
  & > *:last-child {
    border-top: 1px solid currentColor;
    float: right;
    padding-top: .06em;
    width: 100%;
  }

  &:after {
    content: "\00a0";
    visibility: hidden;
    width: 0;
  }

  .text-center:not(.inline) &, .display &, table & { font-size: 1em; vertical-align: -0.45em; }
  mfrac, .text-center:not(.inline) & mfrac, .display & mfrac { font-size: 75%; vertical-align: -0.25em; }
}

msqrt, mroot {
  border-top: 1px solid currentColor;
  margin-left: 9px;
  padding: .1em .1em 0 .2em;
  position: relative;

  &:before {
    @include svg-operator('M3,11.72l-1.56.7-.42-1,2.93-1.3,2.55,7.61c.15.46.26.95.37,1.41h.09c.09-.46.18-.95.26-1.41L11.12,1h1.32L7.67,21.13H6.28Z', 13, 22);
    content: "";
    bottom: 0;
    left: -11px;
    top: -2px;
    width: 13px;
  }
}

mroot > *:last-child {
  position: absolute;
  font-size: 67%;
  right: calc(100% + 5px);
  margin-top: -5px;
}

mfenced {
  margin: 0 calc(5px + .1em);
  padding: .05em .1em;
  position: relative;

  &:before, &:after { bottom: -1px; top: -1px; content: ""; width: 5px; }
  &:before { @include svg-operator('M0,11A18.32,18.32,0,0,1,2.91,1L4,1.53A18.32,18.32,0,0,0,1.5,11,18.33,18.33,0,0,0,4,20.47L2.91,21A18.32,18.32,0,0,1,0,11Z', 5, 22); /* rtl:ignore */ left: -4px; }
  &:after { @include svg-operator('M1,20.47A18.33,18.33,0,0,0,3.5,11,18.32,18.32,0,0,0,1,1.53L2.09,1A18.32,18.32,0,0,1,5,11,18.32,18.32,0,0,1,2.09,21Z', 5, 22); /* rtl:ignore */ right: -4px; }
}

mfenced[open="|"] {
  margin: 0 .1em;
  padding: 0 .1em;
  border-left: 1px solid currentColor;
  border-right: 1px solid currentColor;
  &:before, &:after { content: none; }
}

mfenced[open="["]:before { content: '['; }
mfenced[close="]"]:after { content: ']'; }

mfenced[open="{"]:before { content: '{'; }
mfenced[close="}"]:after { content: '}'; }


// -----------------------------------------------------------------------------
// Mover and Munder

mover, munder { position: relative; }

mover > *:nth-child(2) {
  position: absolute;
  left: 0;
  right: 0;
  height: 5px;
  font-size: 0;
}
mover > [value="‾"]:nth-child(2) { top: 0; @include svg-operator('M0,2L21,2L21,3L0,3Z', 21, 5); }
mover > [value="→"]:nth-child(2) { top: -2px; @include svg-operator('M21,2.5L15.7,0.3L16.5,2L0,2L0,3L16.5,3L15.7,4.7L21,2.5Z', 21, 5); }
mover > [value="↔"]:nth-child(2) { top: -2px; @include svg-operator('M21,2.5L15.7,0.3L16.5,2L4.5,2L5.3,0.3L0,2.5L5.3,4.7L4.5,3L16.5,3L15.7,4.7L21,2.5Z', 21, 5); }
mover > [value="⌒"]:nth-child(2) { top: -2px; @include svg-operator('M20.5,4.5a.52.52,0,0,1-.29-.09A17.1,17.1,0,0,0,10.5,1.5,17.1,17.1,0,0,0,.79,4.41a.5.5,0,0,1-.7-.13.5.5,0,0,1,.12-.69A18.22,18.22,0,0,1,10.5.5,18.22,18.22,0,0,1,20.79,3.59a.5.5,0,0,1,.12.69A.5.5,0,0,1,20.5,4.5Z', 21, 5); }
