/* Animated answer guides for the IA2 Mock Builder (pages/ia2-guide.php,
   js/ia2-guide.js; figures compiled by tools/ia2-guides/build.py).

   A guide reveals its working a step at a time, when the student asks, and
   the figure builds alongside: curves draw themselves, points pop on, areas
   and guide lines fade in. That is motion answering the reader, not a
   moment that plays on its own. Without the script everything shows at
   once; with reduced motion the steps still reveal, but nothing moves.

   One orange per frame: in the figure it is the "key" layer (the answer or
   the tangent); in the working it is the answer box. */

.ag {
  padding-top: var(--space-16);
}

.ag__head {
  max-width: 75ch;
  margin-bottom: var(--space-8);
}

.ag__topic {
  margin: var(--space-2) 0 0;
  color: var(--muted);
  font-size: var(--text-small-size);
  line-height: var(--text-small-line);
}

.ag-question {
  max-width: 80ch;
}

.ag-question h2,
.ag-work h2 {
  font-size: var(--text-h3-size);
  line-height: var(--text-h3-line);
}

.ag-parts {
  margin: 0;
  padding: 0;
  list-style: none;
}

.ag-parts li {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  column-gap: var(--space-2);
  margin-top: var(--space-2);
}

.ag-parts li > :nth-child(n + 3) {
  grid-column: 2;
}

.ag-parts__label {
  font-weight: 600;
}

.ag-parts__marks {
  color: var(--muted);
  white-space: nowrap;
}

.ag-table {
  overflow-x: auto;
  margin: var(--space-4) 0;
}

.ag-table table {
  border-collapse: collapse;
}

.ag-table th,
.ag-table td {
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--muted);
  font-weight: 400;
  text-align: center;
}

.ag-idea {
  max-width: 75ch;
  margin: var(--space-8) 0;
}

/* ---------- Layout: working on the left, the figure staying in view ---------- */

.ag-body {
  display: grid;
  gap: var(--space-8);
}

.ag-fig {
  position: sticky;
  top: 0;
  z-index: 2;
  margin: 0 calc(-1 * var(--space-4));
  padding: var(--space-2) var(--space-4);
  background: var(--frame);
  border-bottom: 1px solid var(--grid);
}

.ag-fig figcaption {
  margin-top: var(--space-2);
  color: var(--muted);
  font-size: var(--text-small-size);
  line-height: var(--text-small-line);
}

@media (min-width: 1000px) {
  .ag-body {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    column-gap: var(--space-16);
    align-items: start;
  }

  .ag-body--plain {
    grid-template-columns: minmax(0, 75ch);
  }

  .ag-work {
    grid-column: 1;
    grid-row: 1;
  }

  .ag-fig {
    grid-column: 2;
    grid-row: 1;
    top: var(--space-8);
    margin: 0;
    padding: 0;
    border-bottom: 0;
  }
}

/* On a short screen held sideways, don't let the figure fill it. */
@media (max-width: 999px) and (max-height: 520px) {
  .ag-fig {
    position: static;
  }
}

.ag-fig__frame {
  position: relative;
  container-type: inline-size;
  max-width: 640px;
  margin: 0 auto;
  border: 1px solid var(--grid);
  background: var(--frame);
}

.ag-fig__svg {
  display: block;
  width: 100%;
  height: auto;
}

/* ---------- The figure's drawing ---------- */

.ag-grid {
  fill: none;
  stroke: var(--grid);
  stroke-width: var(--stroke-grid);
}

.ag-axis {
  fill: none;
  stroke: var(--muted);
  stroke-width: var(--stroke-axis);
}

.ag-num,
.ag-var,
.ag-readout {
  fill: var(--muted);
  font-family: var(--font-math);
  font-size: 17px;
}

.ag-var {
  font-family: KaTeX_Math, var(--font-math);
  font-style: italic;
  font-size: 20px;
}

.ag-readout {
  fill: var(--ink);
  font-size: 22px;
}

.ag-line {
  fill: none;
  stroke-width: var(--stroke-curve);
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ag-ink {
  stroke: var(--ink);
}

.ag-alt {
  stroke: var(--curve-alt);
}

.ag-key {
  stroke: var(--tangent);
  stroke-width: var(--stroke-tangent);
}

.ag-guide {
  stroke: var(--muted);
  stroke-width: 2;
  stroke-dasharray: 7 7;
  stroke-linecap: butt;
}

.ag-dot {
  stroke: var(--frame);
  stroke-width: 2;
}

.ag-dot--ink {
  fill: var(--ink);
}

.ag-dot--alt {
  fill: var(--curve-alt);
}

.ag-dot--key {
  fill: var(--tangent);
}

.ag-dot--guide {
  fill: var(--muted);
}

.ag-dot--open {
  fill: var(--frame);
  stroke: var(--ink);
  stroke-width: 3;
}

.ag-area {
  stroke: none;
  fill-opacity: 0.22;
}

.ag-area--ink {
  fill: var(--muted);
}

.ag-area--alt {
  fill: var(--curve-alt);
}

.ag-area--key {
  fill: var(--tangent);
  fill-opacity: 0.3;
}

.ag-bar {
  fill: var(--frame);
  stroke: var(--ink);
  stroke-width: 2;
}

.ag-bar--ink {
  fill: var(--ink);
}

.ag-bar--alt {
  fill: var(--curve-alt);
  stroke: var(--curve-alt);
}

.ag-bar--key {
  fill: var(--tangent);
  stroke: var(--tangent);
}

/* Labels are HTML over the drawing, so KaTeX can typeset them. The script
   places each one from its data-x and data-y (percentages of the frame). */
.ag-label {
  position: absolute;
  z-index: 1;
  padding: 0 4px;
  background: var(--frame);
  color: var(--ink);
  /* Labels shrink with the figure (17px when it is 640px wide), so they keep
     their places on a phone. */
  font-size: 13px;
  font-size: clamp(11px, 2.66cqw, 17px);
  line-height: 1.5;
  white-space: nowrap;
  pointer-events: none;
}

.ag-label--above { transform: translate(-50%, calc(-100% - 10px)); }
.ag-label--below { transform: translate(-50%, 10px); }
.ag-label--left { transform: translate(calc(-100% - 12px), -50%); }
.ag-label--right { transform: translate(12px, -50%); }
.ag-label--above-left { transform: translate(calc(-100% - 8px), calc(-100% - 8px)); }
.ag-label--above-right { transform: translate(8px, calc(-100% - 8px)); }
.ag-label--below-left { transform: translate(calc(-100% - 8px), 8px); }
.ag-label--below-right { transform: translate(8px, 8px); }
.ag-label--centre { transform: translate(-50%, -50%); background: transparent; }

/* ---------- The working ---------- */

.ag-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-4);
  margin: var(--space-4) 0;
}

.ag-controls[hidden],
.ag-controls [hidden] {
  display: none;
}

.ag-controls__count {
  color: var(--muted);
  font-size: var(--text-small-size);
}

.ag-steps {
  max-width: 75ch;
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: ag-step;
}

.ag-step {
  position: relative;
  padding: var(--space-4) 0 var(--space-4) 48px;
  border-top: 1px solid var(--grid);
  counter-increment: ag-step;
}

.ag-step::before {
  content: counter(ag-step);
  position: absolute;
  top: var(--space-4);
  left: 0;
  width: 32px;
  height: 32px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  font-weight: 600;
  line-height: 28px;
  text-align: center;
}

.ag-step p {
  margin: 0;
}

.ag-step__part {
  color: var(--muted);
  font-size: var(--text-small-size);
  font-weight: 600;
}

.ag-step__lines {
  margin: var(--space-2) 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.ag-line .katex-display {
  margin: 6px 0;
  text-align: left;
}

.ag-line .katex-display > .katex {
  text-align: left;
}

.ag-step__mark {
  color: var(--muted);
  font-size: var(--text-small-size);
  line-height: var(--text-small-line);
}

/* The answer box: square corners, the working's one orange. */
.ag-answer {
  max-width: 75ch;
  margin-top: var(--space-4);
  padding: var(--space-4);
  border: var(--stroke-tangent) solid var(--tangent);
  border-radius: var(--radius-none);
  background: var(--frame);
  overflow-x: auto;
}

.ag-answer h3 {
  margin: 0 0 var(--space-2);
  font-size: var(--text-body-size);
  line-height: var(--text-body-line);
}

.ag-answer__row {
  display: flex;
  gap: var(--space-4);
  align-items: baseline;
  margin-top: var(--space-2);
}

.ag-answer__part {
  font-weight: 600;
}

.ag-answer__lines {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.ag-watch {
  max-width: 75ch;
  margin: var(--space-8) 0 0;
  padding: var(--space-4);
  border-left: 4px solid var(--ink);
  background: var(--surface);
}

.ag__foot {
  max-width: 75ch;
  margin-top: var(--space-16);
  padding-top: var(--space-8);
  border-top: 1px solid var(--grid);
}

.ag__credit {
  color: var(--muted);
  font-size: 16px;
  line-height: 24px;
}

/* ---------- Stepping (only once the script is running) ----------
   .ag--js is set by js/ia2-guide.js. A step, the answer and each figure
   layer wait hidden until their turn (.is-on). */

.ag--js .ag-step:not(.is-on),
.ag--js .ag-answer:not(.is-on),
.ag--js .ag-watch:not(.is-on) {
  display: none;
}

.ag--js [data-anim]:not(.is-on),
.ag--js .ag-label:not([data-step="-1"]):not(.is-on) {
  visibility: hidden;
}

@media (prefers-reduced-motion: no-preference) {
  /* A new step rises into place, and its lines of working follow one by one
     (the script sets each line's delay). */
  .ag--js .ag-step.is-on {
    animation: ag-rise var(--dur-standard) var(--ease-out) both;
  }

  .ag--js .ag-step.is-on .ag-line,
  .ag--js .ag-step.is-on .ag-step__mark {
    animation: ag-rise var(--dur-standard) var(--ease-out) both;
  }

  /* The answer box is a panel, so it rises like a step rather than popping
     like a point. */
  .ag--js .ag-answer.is-on {
    animation: ag-rise var(--dur-standard) var(--ease-out) both;
  }

  .ag--js .ag-watch.is-on {
    animation: ag-rise var(--dur-standard) var(--ease-out) 0.3s both;
  }

  /* Figure layers. Lines draw themselves; a tangent's two halves grow out of
     its point; points pop; areas, guide lines and labels fade in. */
  .ag--js [data-anim="draw"] {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
  }

  .ag--js [data-anim="draw"].is-on {
    animation: ag-draw 0.9s var(--ease-in-out) forwards;
  }

  .ag--js [data-anim="fade"].is-on {
    animation: ag-fade var(--dur-slow) var(--ease-out) both;
  }

  .ag--js [data-anim="pop"],
  .ag--js [data-anim="run"],
  .ag--js [data-anim="move"] {
    transform-box: fill-box;
    transform-origin: center;
  }

  .ag--js [data-anim="pop"].is-on,
  .ag--js [data-anim="run"].is-on,
  .ag--js [data-anim="move"].is-on {
    animation: ag-pop var(--dur-slow) var(--ease-spring) both;
  }

  .ag--js [data-anim="grow"] {
    transform-box: fill-box;
    transform-origin: center bottom;
  }

  .ag--js [data-anim="grow"].is-on {
    animation: ag-grow 0.5s var(--ease-out) both;
  }

  .ag--js .ag-label.is-on {
    animation: ag-fade var(--dur-slow) var(--ease-out) 0.5s both;
  }
}

@keyframes ag-rise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}

@keyframes ag-fade {
  from {
    opacity: 0;
  }
}

@keyframes ag-pop {
  from {
    opacity: 0;
    transform: scale(0.3);
  }

  35% {
    opacity: 1;
  }
}

@keyframes ag-draw {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes ag-grow {
  from {
    transform: scaleY(0);
  }
}

/* Printing shows the whole answer, whatever has been revealed on screen. */
@media print {
  .ag-controls {
    display: none;
  }

  .ag-fig {
    position: static;
  }

  .ag--js .ag-step,
  .ag--js .ag-answer,
  .ag--js .ag-watch {
    display: block !important;
    animation: none !important;
  }

  .ag--js [data-anim],
  .ag--js .ag-label {
    visibility: visible !important;
    animation: none !important;
    stroke-dashoffset: 0 !important;
  }
}

/* ---------- The index of answers (pages/ia2-guides.php) ---------- */

.ag-index {
  max-width: 80ch;
  margin-top: var(--space-16);
}

.ag-index__count {
  margin: 0 0 var(--space-4);
  color: var(--muted);
  font-size: var(--text-small-size);
}

.ag-index__list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.ag-index__list li {
  display: flex;
  flex-direction: column;
  padding: var(--space-2) 0;
  border-top: 1px solid var(--grid);
}

.ag-index__list a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  font-weight: 600;
}

.ag-index__meta {
  color: var(--muted);
  font-size: var(--text-small-size);
  line-height: var(--text-small-line);
}

/* On a phone the working gets the full width: the step number sits above its
   step, not beside it, and the maths is set at the size of the words. A
   longer line still scrolls sideways inside its own step. */
@media (max-width: 639px) {
  .ag-step {
    padding-left: 0;
  }

  .ag-step::before {
    position: static;
    display: block;
    margin-bottom: var(--space-2);
  }

  .ag-line .katex,
  .ag-answer .katex {
    font-size: 1em;
  }
}
