/*
 * hand.css — the papers the SIDE B handwriting sits on.
 *   .hand-letter   ruled letter paper with a strip of yellow masking tape
 *   .hand-note     a loose-leaf notebook page
 * hand.js sets --hand-lh (the ruled-line pitch, whole pixels) on the paper
 * (option `varsOn`), so the rules and the writing always share one grid.
 */

/* the handwriting block itself */
.sbh {
  position: relative;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.sbh svg {
  display: block;
  overflow: visible;
}
.sbh[data-hand-state="writing"],
.sbh[data-hand-state="paused"] {
  cursor: pointer;
}

/* ------------------------------------------------------------- letter */

.hand-letter {
  --hand-lh: 38px;
  --hand-paper: #f9f2e1;
  --hand-rule: rgba(128, 98, 56, 0.3);
  --hand-ink: #2a2823;
  --hand-pad: clamp(24px, 7.4%, 64px);
  position: relative;
  box-sizing: border-box;
  width: 100%;
  max-width: 620px;
  margin-inline: auto;
  /* everything above the writing is a whole number of rules */
  padding: calc(var(--hand-lh) * 2) var(--hand-pad) calc(var(--hand-lh) * 2);
  color: var(--hand-ink);
  background-color: var(--hand-paper);
  background-image:
    /* two soft creases: the letter was folded in three */
    linear-gradient(to bottom, transparent calc(33.4% - 3px), rgba(255, 255, 255, 0.55) calc(33.4% - 1px), rgba(96, 72, 40, 0.1) 33.4%, transparent calc(33.4% + 5px)),
    linear-gradient(to bottom, transparent calc(66.8% - 3px), rgba(255, 255, 255, 0.55) calc(66.8% - 1px), rgba(96, 72, 40, 0.1) 66.8%, transparent calc(66.8% + 5px)),
    /* light from the upper left, a little age at the edges */
    radial-gradient(120% 90% at 18% 8%, rgba(255, 255, 255, 0.5), transparent 55%),
    radial-gradient(140% 120% at 50% 50%, transparent 62%, rgba(150, 112, 60, 0.14) 100%),
    /* paper fibre */
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.82' numOctaves='3' seed='4' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .42  0 0 0 0 .32  0 0 0 0 .18  0 0 0 .1 0'/%3E%3C/filter%3E%3Crect width='240' height='240' filter='url(%23n)'/%3E%3C/svg%3E");
  border-radius: 2px 3px 2px 4px / 3px 2px 4px 2px;
  transform: rotate(-0.7deg);
  box-shadow:
    0 0 0 0.5px rgba(96, 72, 40, 0.18),
    0 1px 1px rgba(20, 14, 6, 0.16),
    0 10px 18px -10px rgba(20, 14, 6, 0.42),
    0 34px 60px -28px rgba(20, 14, 6, 0.6);
}

/* the printed rules: inset from the edges like real letter paper */
.hand-letter::before {
  content: "";
  position: absolute;
  top: var(--hand-lh);
  bottom: calc(var(--hand-lh) * 0.5);
  left: calc(var(--hand-pad) * 0.5);
  right: calc(var(--hand-pad) * 0.5);
  background-image: linear-gradient(to top, var(--hand-rule) 1px, transparent 1px);
  background-size: 100% var(--hand-lh);
  background-position: left top;
  background-repeat: repeat-y;
  pointer-events: none;
}

/* a small stationery imprint, bottom left */
.hand-letter::after {
  content: "SIDE B  ·  OFFLINE CLUB, OSAKA";
  position: absolute;
  left: calc(var(--hand-pad) * 0.5);
  bottom: calc(var(--hand-lh) * 0.5 - 16px);
  font: italic 600 8.5px/1 "Fraunces", Georgia, serif;
  letter-spacing: 0.2em;
  white-space: pre;
  color: rgba(214, 78, 48, 0.62);
  pointer-events: none;
}

/* masking tape: translucent, hand-torn at both ends */
.hand-letter__tape {
  position: absolute;
  top: -15px;
  left: 50%;
  width: clamp(104px, 22%, 132px);
  height: 31px;
  margin-left: calc(clamp(104px, 22%, 132px) / -2);
  transform: rotate(-2.6deg);
  filter: drop-shadow(0 1px 1px rgba(20, 14, 6, 0.22));
  pointer-events: none;
  z-index: 2;
}
.hand-letter__tape::before {
  content: "";
  position: absolute;
  inset: 0;
  background-color: rgba(255, 198, 41, 0.86);
  background-image:
    linear-gradient(to bottom, rgba(255, 255, 255, 0.34), transparent 22%, transparent 74%, rgba(160, 104, 0, 0.14)),
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.1) 0 1px, transparent 1px 4px),
    repeating-linear-gradient(90deg, rgba(150, 96, 0, 0.05) 0 2px, transparent 2px 9px);
  clip-path: polygon(
    0% 6%, 1.6% 0%, 98.2% 0%, 100% 9%, 98.6% 19%, 100% 31%, 98.4% 44%, 99.8% 57%, 98.2% 70%, 100% 84%, 98.6% 100%,
    1.8% 100%, 0% 90%, 1.6% 78%, 0.2% 66%, 1.8% 52%, 0.2% 40%, 1.6% 27%, 0.2% 16%
  );
}

.hand-letter__body {
  position: relative;
  z-index: 1;
}

/* --------------------------------------------------------------- note */

.hand-note {
  --hand-lh: 34px;
  --hand-paper: #fbf9f1;
  --hand-rule: rgba(64, 108, 140, 0.3);
  --hand-margin: rgba(255, 90, 60, 0.5);
  --hand-ink: #2a2823;
  --hand-hole: 30px; /* width of the punched strip */
  position: relative;
  width: 100%;
  max-width: 360px;
  margin-inline: auto;
  transform: rotate(1.1deg);
  /* the page is masked (real holes), so the shadow comes from the wrapper */
  filter: drop-shadow(0 1px 1px rgba(20, 14, 6, 0.2)) drop-shadow(0 16px 20px rgba(20, 14, 6, 0.36));
}
.hand-note__page {
  position: relative;
  box-sizing: border-box;
  padding: calc(var(--hand-lh) * 2) 22px calc(var(--hand-lh) * 2) calc(var(--hand-hole) + 30px);
  color: var(--hand-ink);
  border-radius: 3px 7px 7px 3px;
  background-color: var(--hand-paper);
  background-image:
    /* red margin line */
    linear-gradient(to right, transparent calc(var(--hand-hole) + 17px), var(--hand-margin) calc(var(--hand-hole) + 17px), var(--hand-margin) calc(var(--hand-hole) + 18px), transparent calc(var(--hand-hole) + 18px)),
    /* the heavier rule under the title row */
    linear-gradient(to top, rgba(64, 108, 140, 0.36) 1px, transparent 1px),
    /* rules */
    linear-gradient(to top, var(--hand-rule) 1px, transparent 1px),
    radial-gradient(130% 100% at 20% 0%, rgba(255, 255, 255, 0.6), transparent 60%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' seed='9' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .3  0 0 0 0 .34  0 0 0 0 .36  0 0 0 .07 0'/%3E%3C/filter%3E%3Crect width='240' height='240' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size:
    100% 100%,
    100% calc(var(--hand-lh) * 3 + 2px),
    100% var(--hand-lh),
    100% 100%,
    240px 240px;
  background-position:
    0 0,
    0 0,
    0 var(--hand-lh),
    0 0,
    0 0;
  background-repeat: no-repeat, no-repeat, repeat-y, no-repeat, repeat;
  /* punched holes, one every two rules */
  -webkit-mask-image: radial-gradient(circle at calc(var(--hand-hole) / 2 + 2px) 50%, transparent 5.2px, #000 5.9px);
  mask-image: radial-gradient(circle at calc(var(--hand-hole) / 2 + 2px) 50%, transparent 5.2px, #000 5.9px);
  -webkit-mask-size: 100% calc(var(--hand-lh) * 2);
  mask-size: 100% calc(var(--hand-lh) * 2);
  -webkit-mask-position: 0 calc(var(--hand-lh) * 0.5);
  mask-position: 0 calc(var(--hand-lh) * 0.5);
}

@media (prefers-reduced-motion: reduce) {
  .sbh[data-hand-state] {
    cursor: auto;
  }
}
