/* Self-hosted fonts (Cabin Sketch, Kalam; SIL Open Font License): no requests to third parties. */
@font-face { font-family: 'Cabin Sketch'; font-style: normal; font-weight: 700; font-display: swap; src: url('/fonts/cabin-sketch-700.woff2') format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }
@font-face { font-family: 'Kalam'; font-style: normal; font-weight: 300; font-display: swap; src: url('/fonts/kalam-300.woff2') format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }
@font-face { font-family: 'Kalam'; font-style: normal; font-weight: 400; font-display: swap; src: url('/fonts/kalam-400.woff2') format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }

  /* Layout: a lecture-hall chalkboard. One column, written in chalk. Deliberately single-theme. */
  :root {
    --board: #263a30;
    --board-deep: #1d2e25;
    --chalk: #f1efe6;
    --chalk-faint: rgba(241, 239, 230, 0.5);
    --chalk-ghost: rgba(241, 239, 230, 0.22);
    --chalk-yellow: #f3dc7c;
    --chalk-pink: #f2a093;
    --font-title: "Cabin Sketch", "Chalkboard SE", "Comic Sans MS", cursive;
    --font-hand: "Kalam", "Chalkboard SE", "Comic Sans MS", cursive;
    color-scheme: dark;
  }

  html { height: 100%; overflow-x: clip; }
  body {
    min-height: 100%;
    margin: 0;
    color: var(--chalk);
    font-family: var(--font-hand);
    font-weight: 300;
    font-size: 20px;
    background-color: var(--board);
    background-image:
      url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='220' height='220'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 0.07 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>"),
      radial-gradient(ellipse 55% 30% at 25% 20%, rgba(255,255,255,0.05), transparent 70%),
      radial-gradient(ellipse 45% 25% at 78% 70%, rgba(255,255,255,0.04), transparent 70%),
      radial-gradient(ellipse at center, var(--board) 40%, var(--board-deep) 100%);
    text-shadow: 0 0 1px rgba(241, 239, 230, 0.35);
    display: grid;
    place-items: center;
    padding-block: 56px;
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
    box-sizing: border-box;
    overflow-x: clip;
    position: relative;
  }
  main {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 40px;
    width: 100%;
    max-width: 38rem;
    text-align: center;
  }

  .title-block { display: flex; flex-direction: column; gap: 22px; width: fit-content; max-width: 100%; }
  h1 {
    margin: 0;
    font-family: var(--font-title);
    font-weight: 700;
    font-size: clamp(3.2rem, 20vw, 7rem);
    line-height: 1;
    white-space: nowrap;
  }
  h1 em { font-family: var(--font-hand); font-weight: 300; font-style: normal; font-size: 0.42em; color: var(--chalk-faint); padding: 0 0.2em; }

  button { font-family: var(--font-hand); color: inherit; cursor: pointer; text-shadow: inherit; }
  button:focus-visible { outline: 2px dashed var(--chalk-yellow); outline-offset: 4px; }

  /* No buttons on a blackboard: the control is just words written in chalk, with a chalk line under them. */
  #check {
    width: 100%;
    background: transparent no-repeat center bottom / 100% 12px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 400 12' preserveAspectRatio='none'><path d='M3 7 C 70 4, 140 9, 210 6 S 340 4, 397 7' fill='none' stroke='%23f1efe6' stroke-width='2.4' stroke-linecap='round' opacity='0.85'/></svg>");
    border: 0;
    padding: 2px 0 14px;
    font-size: 24px;
    font-weight: 400;
    transition: color 150ms ease;
  }
  #check:disabled { cursor: progress; color: var(--chalk-faint); }

  /* The board eraser that wipes the old answer before the next check. */
  .eraser {
    position: absolute; left: 0; top: 0; z-index: 2;
    width: 104px; height: 38px;
    border-radius: 5px;
    background: linear-gradient(180deg, #a5703f 0 56%, #6b4526 56% 62%, #3d3b38 62% 100%);
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.4);
    pointer-events: none;
  }

  .verdict:empty { display: none; }
  .verdict { margin: 0; font-size: clamp(1.4rem, 4.5vw, 1.8rem); line-height: 1.35; }
  .verdict b { font-family: var(--font-title); font-weight: 700; color: var(--chalk-pink); font-size: 1.15em; }
  /* Handwriting (verdict and puzzle result): every letter is wiped in from left to right, one after another. */
  .chalkwrite { position: relative; }
  .chalkwrite .line { display: block; }
  .chalkwrite .word { display: inline-block; white-space: nowrap; }
  /* Chalk grain: a speckled noise mask leaves tiny gaps in every stroke. */
  .chalkwrite .line {
    -webkit-mask-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='120' height='120'><filter id='g'><feTurbulence type='fractalNoise' baseFrequency='1.3' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  -2.4 0 0 0 2.05'/></filter><rect width='100%' height='100%' filter='url(%23g)'/></svg>");
    mask-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='120' height='120'><filter id='g'><feTurbulence type='fractalNoise' baseFrequency='1.3' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  -2.4 0 0 0 2.05'/></filter><rect width='100%' height='100%' filter='url(%23g)'/></svg>");
  }
  /* Each letter stays invisible until the chalk reaches it, then is wiped in left to right. */
  .chalkwrite .ch { display: inline-block; opacity: 0; }
  .chalkwrite .ch.on { animation: chalk 90ms ease-out both; }
  @keyframes chalk {
    0%   { clip-path: inset(-0.6em 100% -0.6em -0.6em); opacity: 1; }
    100% { clip-path: inset(-0.6em -0.6em -0.6em -0.6em); opacity: 1; }
  }
  /* Quick chalk writing for short things (numbers, totals): same stroke, timed with CSS delays. */
  .ink .ch { display: inline-block; animation: inked 80ms ease-out both; animation-delay: var(--d, 0ms); }
  @keyframes inked {
    0%   { opacity: 0; clip-path: inset(-0.6em 100% -0.6em -0.6em); }
    1%   { opacity: 1; }
    100% { opacity: 1; clip-path: inset(-0.6em -0.6em -0.6em -0.6em); }
  }
  /* The piece of chalk that follows the writing, and the dust it drops. */
  .stick {
    position: absolute; left: 0; top: 0;
    width: 30px; height: 9px;
    margin: -4px 0 0 -2px;
    border-radius: 2px 3px 3px 2px;
    background: linear-gradient(180deg, #ffffff, #dedad0);
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.35);
    transform-origin: 2px 4px;
    transition: transform 60ms linear, opacity 300ms ease;
    pointer-events: none;
  }
  .stick.pink { background: linear-gradient(180deg, #f8b7ac, #e0897b); }
  .dust.pink { background: var(--chalk-pink); }
  .stick.yellow { background: linear-gradient(180deg, #fbeaa6, #e0c357); }
  .dust.yellow { background: var(--chalk-yellow); }
  .dust {
    position: absolute;
    width: 3px; height: 3px;
    border-radius: 50%;
    background: var(--chalk);
    opacity: 0.8;
    pointer-events: none;
    animation: dust 650ms ease-in forwards;
  }
  @keyframes dust { to { transform: translate(var(--dx), var(--dy)); opacity: 0; } }
  .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

  /* The puzzle: subset sum, worked out as written column addition.
     Left: the numbers you can use. Right: the sum you are building. */
  .game { display: flex; flex-direction: column; align-items: center; gap: 22px; width: 100%; }
  .game > p { margin: 0; line-height: 1.45; }
  .game > p span { color: var(--chalk-faint); }
  .workspace {
    position: relative;
    display: grid;
    grid-template-columns: auto auto;
    justify-content: center;
    align-items: start;
    gap: 56px;
  }
  @media (max-width: 560px) { .workspace { grid-template-columns: 1fr; gap: 28px; justify-items: center; } }

  .pool { display: grid; grid-template-columns: repeat(2, auto); gap: 0 28px; }
  .pool button {
    position: relative;
    background: none;
    border: 0;
    padding: 2px 4px;
    font-size: 23px;
    font-weight: 400;
    text-align: right;
    transition: color 250ms ease;
  }
  .pool button[aria-pressed="true"] { color: var(--chalk-ghost); }
  /* Crossing out: a chalk line drawn left to right through the number. */
  .pool button::after {
    content: "";
    position: absolute;
    left: -3px; right: -3px; top: 54%;
    height: 2px;
    border-radius: 2px;
    background: var(--chalk);
    opacity: 0.8;
    transform-origin: left center;
    transform: rotate(-4deg) scaleX(0);
    transition: transform 220ms ease-out;
  }
  .pool button[aria-pressed="true"]::after { transform: rotate(-4deg) scaleX(1); }

  .column { min-width: 9em; font-size: 23px; font-weight: 400; }
  .row { display: grid; grid-template-columns: 1.2em 1fr; align-items: baseline; }
  .row .sign { text-align: left; color: var(--chalk-faint); }
  .row button { background: none; border: 0; padding: 2px 0; font-size: inherit; font-weight: inherit; text-align: right; }
  .row.empty { color: var(--chalk-ghost); }
  .row.empty .sign { color: var(--chalk-ghost); }
  .row .slot { text-align: right; padding: 2px 0; }
  .rule { height: 0; border-top: 2px solid var(--chalk); margin: 6px 0 4px; transform: rotate(-0.8deg); opacity: 0.85; }
  .total { text-align: right; font-family: var(--font-title); font-weight: 700; font-size: 30px; }
  .total-num { position: relative; display: inline-block; }
  #total.over { color: var(--chalk-pink); }
  #total.hit { color: var(--chalk-yellow); }
  /* Solved: the total gets circled in yellow chalk, drawn as one stroke. */
  .ring {
    position: absolute;
    left: -18px; top: -12px;
    width: calc(100% + 36px); height: calc(100% + 24px);
    overflow: visible;
    pointer-events: none;
  }
  .ring path {
    fill: none;
    stroke: var(--chalk-yellow);
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    opacity: 0;
  }
  .ring.drawn path { opacity: 0.9; animation: ring 650ms ease-out forwards; }
  @keyframes ring { to { stroke-dashoffset: 0; } }
  @keyframes shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-5px) rotate(-1deg); }
    40% { transform: translateX(4px) rotate(1deg); }
    60% { transform: translateX(-3px); }
    80% { transform: translateX(2px); }
  }
  .total-num.shake { animation: shake 380ms ease-out; }

  .reroll { background: none; border: 0; padding: 0; font-size: 18px; color: var(--chalk-faint); text-decoration: underline wavy; text-underline-offset: 5px; text-decoration-thickness: 1px; }
  /* Hover colours only where there is a real pointer, so a tap on a phone does not leave them stuck on. */
  @media (hover: hover) {
    #check:hover { color: var(--chalk-yellow); }
    .pool button:hover { color: var(--chalk-yellow); }
    .row button:hover { color: var(--chalk-pink); text-decoration: line-through; }
    .reroll:hover { color: var(--chalk); }
  }
  button { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
  /* Phones: bigger tap targets in the puzzle. */
  @media (max-width: 560px) {
    .pool { gap: 0 22px; }
    .pool button { padding: 7px 4px; }
    .row button { padding: 6px 0; }
    .reroll { padding: 8px 4px; }
  }
  .result:empty { display: none; }
  .result { margin: 0; max-width: 30rem; line-height: 1.45; }

  @media (prefers-reduced-motion: reduce) {
    .chalkwrite .ch { opacity: 1; }
    .chalkwrite .ch.on, .ink .ch, .total-num.shake { animation: none; }
    .ring.drawn path { animation: none; stroke-dashoffset: 0; }
    #check, .pool button, .pool button::after { transition: none; }
  }

  /* Footer and the plain text pages (privacy). */
  /* Privacy link: tucked into the bottom-left corner, underlined with the same chalk line as "check". */
  .site-foot {
    position: absolute;
    left: max(16px, env(safe-area-inset-left, 0px));
    bottom: max(12px, env(safe-area-inset-bottom, 0px));
    font-size: 15px;
    line-height: 1;
  }
  .site-foot a {
    display: inline-block;
    padding: 2px 0 8px;
    color: var(--chalk);
    opacity: 0.6;
    text-decoration: none;
    background: transparent no-repeat center bottom / 100% 7px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 400 12' preserveAspectRatio='none'><path d='M3 7 C 70 4, 140 9, 210 6 S 340 4, 397 7' fill='none' stroke='%23f1efe6' stroke-width='2.4' stroke-linecap='round' opacity='0.85'/></svg>");
    transition: opacity 150ms ease;
  }
  .site-foot a:focus-visible { outline: 2px dashed var(--chalk-yellow); outline-offset: 4px; opacity: 1; }
  @media (hover: hover) { .site-foot a:hover { opacity: 1; } }
  main.prose { display: block; text-align: left; max-width: 40rem; }
  .prose h1 { font-size: clamp(2.6rem, 12vw, 4.2rem); white-space: normal; margin: 18px 0 26px; }
  .prose h2 { font-family: var(--font-title); font-weight: 700; font-size: 1.45rem; color: var(--chalk-yellow); margin: 30px 0 6px; }
  .prose p { margin: 0 0 12px; line-height: 1.5; font-size: 19px; }
  .prose .back { color: var(--chalk-faint); font-size: 18px; text-decoration: underline wavy; text-underline-offset: 5px; text-decoration-thickness: 1px; }
