/*
 * css/handwritten.css — the "Handwritten / Typed" view of worked solutions
 * (pilot, Nov 2026; IB topic question pages only). js/handwritten.js adds
 * the switch and the classes; scripts/lib/handwritten.js puts both files on
 * the page. Typed is the default and looks exactly as before: everything
 * below applies only under html.hw-view (the viewer chose Handwritten).
 *
 * THE HANDWRITING FONT IS ONE VALUE: --hw-font below. Kalam (default),
 * 'Patrick Hand' and Caveat are all declared here, self-hosted from
 * css/fonts/ (fontsource 5.3.0, SIL Open Font License 1.1: css/fonts/
 * Kalam-LICENSE.txt, PatrickHand-LICENSE.txt, Caveat-LICENSE.txt); a
 * browser downloads only the one --hw-font names, and only once the
 * Handwritten view is on. size-adjust evens out their sizes, so switching
 * the font needs no other change.
 *
 * Paper, ink and red pen are tokens (--hw-*), redefined for dark mode the
 * repo's way (data-theme="dark"; prefers-color-scheme unless
 * data-theme="light"). The OS setting only counts on a page that is itself
 * dark-capable (<meta name="color-scheme" content="light dark">): the IB
 * topic pages are light-only, and a dark sheet on a light page looked wrong.
 * No animation.
 */
:root {
  --hw-font: 'Kalam';                  /* ← the one config value: 'Kalam' | 'Patrick Hand' | 'Caveat' */
  --hw-paper: #fffdf5;
  --hw-rule: #d5e3f5;
  --hw-margin: #e7a3a3;
  --hw-ink: #1d3a9e;                   /* blue ink, 9:1 on the paper */
  --hw-pen: #c62828;                   /* red pen (marks), 5.5:1 */
  --hw-edge: #e6e1cf;
  --hw-lh: 2.05rem;                    /* one ruled line */
  --hw-gutter: 2.9rem;                 /* the margin, left of the red line */
}
@media (prefers-color-scheme: dark) {
  :root:has(meta[name="color-scheme"][content*="dark"]):not([data-theme="light"]) {
    --hw-paper: #172131; --hw-rule: rgba(150, 175, 215, .17); --hw-margin: rgba(240, 120, 120, .55);
    --hw-ink: #a9c8ff; --hw-pen: #ff8a80; --hw-edge: #2b3b5e;
  }
}
:root[data-theme="dark"] {
  --hw-paper: #172131; --hw-rule: rgba(150, 175, 215, .17); --hw-margin: rgba(240, 120, 120, .55);
  --hw-ink: #a9c8ff; --hw-pen: #ff8a80; --hw-edge: #2b3b5e;
}

@font-face { font-family: 'Kalam'; font-style: normal; font-weight: 400; font-display: swap; size-adjust: 100%;
  src: url(/css/fonts/kalam-latin-400-normal.woff2) format('woff2'); }
@font-face { font-family: 'Kalam'; font-style: normal; font-weight: 700; font-display: swap; size-adjust: 100%;
  src: url(/css/fonts/kalam-latin-700-normal.woff2) format('woff2'); }
@font-face { font-family: 'Patrick Hand'; font-style: normal; font-weight: 400; font-display: swap; size-adjust: 112%;
  src: url(/css/fonts/patrick-hand-latin-400-normal.woff2) format('woff2'); }
@font-face { font-family: 'Caveat'; font-style: normal; font-weight: 400 700; font-display: swap; size-adjust: 128%;
  src: url(/css/fonts/caveat-latin-wght-normal.woff2) format('woff2'); }

/* ---- the switch (both views) ---- */
.hw-bar { display: flex; justify-content: flex-end; margin: 0 0 6px; }
.hw-sw { display: inline-flex; border: 1px solid #c9d5ea; border-radius: 999px; padding: 2px; background: #f4f7fc; font-size: .82rem; line-height: 1; }
.hw-sw button { min-height: 32px; padding: 6px 12px; border: 0; border-radius: 999px; background: transparent; color: #3a4a68; font: inherit; font-weight: 600; cursor: pointer; }
.hw-sw button[aria-pressed="true"] { background: #0d3d8f; color: #fff; }
.hw-sw button:focus-visible { outline: 3px solid #0d3d8f; outline-offset: 2px; }
.hw-sw .hw-pen { font-family: var(--hw-font), 'Comic Sans MS', cursive; font-weight: 400; font-size: 1.05em; }
@media (prefers-color-scheme: dark) {
  :root:has(meta[name="color-scheme"][content*="dark"]):not([data-theme="light"]) .hw-sw { background: #101a2e; border-color: #2b3b5e; }
  :root:has(meta[name="color-scheme"][content*="dark"]):not([data-theme="light"]) .hw-sw button { color: #b5c2dc; }
  :root:has(meta[name="color-scheme"][content*="dark"]):not([data-theme="light"]) .hw-sw button[aria-pressed="true"] { background: #5ec8ff; color: #06122b; }
}
:root[data-theme="dark"] .hw-sw { background: #101a2e; border-color: #2b3b5e; }
:root[data-theme="dark"] .hw-sw button { color: #b5c2dc; }
:root[data-theme="dark"] .hw-sw button[aria-pressed="true"] { background: #5ec8ff; color: #06122b; }

/* ---- both views: punctuation stays with its formula ----
   js/handwritten.js puts an inline formula and the "." or "," after it in
   one span.hw-tail. In the Typed view a long formula scrolls in its own box
   (the site's mjx-container[data-mjx-wide]); here that box leaves room
   beside it for the punctuation instead of filling the line. */
.hw-paper .hw-tail { white-space: nowrap; }
.hw-paper .hw-tail > mjx-container[data-mjx-wide]:not([display="true"]) { max-width: calc(100% - 1em); margin-right: .15em; }
html.hw-view .hw-paper .hw-tail { white-space: normal; }
/* A cut-off scroll box beside a full stop read as a finished line
   ("… = 3x² − 1."): the box's right edge fades while there is more to
   scroll, and the fade is gone once scrolled to the end. Only where the
   browser can tell (scroll-driven animations); elsewhere, no fade. */
@property --hw-fade { syntax: '<length>'; inherits: false; initial-value: 0px; }
@keyframes hw-fade { from { --hw-fade: 1.6em; } to { --hw-fade: 0px; } }
@supports (animation-timeline: scroll()) {
  .hw-paper mjx-container[data-mjx-wide]:not([display="true"]) {
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - var(--hw-fade)), transparent);
    mask-image: linear-gradient(to right, #000 calc(100% - var(--hw-fade)), transparent);
    animation: hw-fade linear both;
    animation-timeline: scroll(self inline);
  }
}

/* ---- the Handwritten view: lined paper, blue ink ---- */
html.hw-view .hw-paper {
  position: relative;
  margin: 4px 0 8px;
  padding: calc(var(--hw-lh) * .35) 14px calc(var(--hw-lh) * .5) var(--hw-gutter);
  border: 1px solid var(--hw-edge);
  border-radius: 4px;
  background-color: var(--hw-paper);
  background-image:
    linear-gradient(to right, transparent calc(var(--hw-gutter) - 10px), var(--hw-margin) calc(var(--hw-gutter) - 10px), var(--hw-margin) calc(var(--hw-gutter) - 8.5px), transparent calc(var(--hw-gutter) - 8.5px)),
    repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--hw-lh) - 1px), var(--hw-rule) calc(var(--hw-lh) - 1px), var(--hw-rule) var(--hw-lh));
  background-position: 0 0, 0 calc(var(--hw-lh) * .35);
  background-attachment: local;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .06);
  color: var(--hw-ink);
  font-family: var(--hw-font), 'Comic Sans MS', 'Segoe Print', cursive;
  font-size: 1.12rem;
  line-height: var(--hw-lh);
  overflow-x: auto;
}
html.hw-view .hw-paper p,
html.hw-view .hw-paper li { margin: 0; }
html.hw-view .hw-paper ol,
html.hw-view .hw-paper ul { margin: 0; padding-left: 1.4em; }
html.hw-view .hw-paper b,
html.hw-view .hw-paper strong { font-weight: 700; color: inherit; }
/* A solution's own leading line break (bank schemes start "<p><br />"). */
html.hw-view .hw-paper > p:first-child > br:first-child { display: none; }
/* The worked example's blue "Solution" rule gives way to the paper. */
html.hw-view .hw-host { border-left-color: transparent; padding-left: 0; }
/* The step-by-step reveal: no paper until the first step shows. */
html.hw-view .ws.hw-paper:not(:has([data-ws-s]:not([hidden]))) { padding: 0; border: 0; background: none; box-shadow: none; }
/* The fill-in box and its buttons keep the site's own type. */
html.hw-view .hw-paper .ws-blank,
html.hw-view .hw-paper input,
html.hw-view .hw-paper button { font-family: inherit; }
html.hw-view .hw-paper .ws-in { font-family: -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.3; }

/* Maths: same MathJax layout (fractions, powers, roots, brackets untouched),
   in the ink colour, with letters and digits written in the hand. */
html.hw-view .hw-paper mjx-container { color: var(--hw-ink); }
/* A little larger than the typed view, so small inline fractions (π/3) and
   powers stay readable on a phone. */
html.hw-view .hw-paper mjx-container[jax="CHTML"] { font-size: 112% !important; }
html.hw-view .hw-paper mjx-container[jax="CHTML"] mjx-mi mjx-c,
html.hw-view .hw-paper mjx-container[jax="CHTML"] mjx-mn mjx-c {
  font-family: var(--hw-font), MJXZERO, MJXTEX-I, MJXTEX;
  font-style: normal;
}
html.hw-view .hw-paper mjx-container[jax="CHTML"] mjx-mi mjx-c::before,
html.hw-view .hw-paper mjx-container[jax="CHTML"] mjx-mn mjx-c::before { font-family: var(--hw-font), MJXZERO, MJXTEX-I, MJXTEX; }
/* Long inline maths wraps onto the next ruled line instead of hiding in its
   own sideways-scrolling box (js/handwritten.js groups each formula into
   <mjx-hw-chunk>s that end after =, +, − … outside brackets). The line may
   break only between chunks; a chunk too wide on its own scrolls with the
   paper. Beats the site's mjx-container[data-mjx-wide] rule. */
html.hw-view .hw-paper mjx-container[data-hw-wrap]:not([display="true"]) {
  display: inline;
  max-width: none;
  overflow: visible;
  white-space: normal;
}
html.hw-view .hw-paper mjx-container[data-hw-wrap] [data-hw-flow] { display: inline; white-space: normal; }
html.hw-view .hw-paper mjx-hw-chunk { display: inline-block; white-space: nowrap; }
html.hw-view .hw-paper mjx-container svg { color: var(--hw-ink); }
html.hw-view .hw-paper mjx-container svg path { stroke: currentColor; stroke-width: 14; stroke-linejoin: round; }

/* Marks (M1, A1, R1 …) already in the solution: red pen in the margin. */
html.hw-view .hw-paper .hw-mk {
  position: absolute;
  left: 4px;
  width: calc(var(--hw-gutter) - 14px);
  margin-top: calc(.35em + var(--hw-mk-i, 0) * 1.05em);
  color: var(--hw-pen);
  font-family: var(--hw-font), 'Comic Sans MS', cursive;
  font-weight: 700;
  font-size: .86em;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  transform: rotate(-6deg);
}
@media (max-width: 480px) {
  :root { --hw-gutter: 2.5rem; }
  html.hw-view .hw-paper { font-size: 1.06rem; padding-right: 10px; }
}
@media print {
  html.hw-view .hw-paper { box-shadow: none; }
  .hw-bar { display: none; }
}
