:root {
  --desk: #dfe5ec;
  --sheet: #fdfdfb;
  --ink: #1c2a4f;
  --ink-soft: #56617c;
  --proof: #c2331f;
  --air-red: #c8362a;
  --air-blue: #26479b;
  --rule: #c7d0dc;
  --drawer: #f3f5f8;
  --mark: #cfe0fb;
  --mark-strong: #8fb4f2;
  --stamp: #26479b;
  --serif: "Spectral", Georgia, "Times New Roman", serif;
  --sans: "Schibsted Grotesk", "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --desk: #121722;
    --sheet: #1a2131;
    --ink: #e4e8f2;
    --ink-soft: #9ba6bf;
    --proof: #ff7d68;
    --air-red: #d9493c;
    --air-blue: #5577d6;
    --rule: #2f3a52;
    --drawer: #202839;
    --mark: #2b3e6c;
    --mark-strong: #4466b4;
    --stamp: #8fa9ff;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--desk);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
}

a {
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--air-blue);
  outline-offset: 2px;
}

/* Masthead */

.masthead {
  max-width: 1160px;
  margin: 0 auto;
  padding: 28px 24px 8px;
}

.home {
  font-size: 14px;
  color: var(--ink-soft);
  text-decoration: none;
  border-bottom: 1px solid var(--rule);
}

.home:hover {
  color: var(--ink);
  border-color: currentColor;
}

.masthead h1 {
  margin: 28px 0 10px;
  font-family: var(--serif);
  font-weight: 500;
  font-style: italic;
  font-size: clamp(34px, 5vw, 52px);
  line-height: 1.05;
  letter-spacing: -0.015em;
}

.dek {
  max-width: 62ch;
  margin: 0;
  font-size: 16px;
  color: var(--ink-soft);
}

/* Layout */

.desk {
  max-width: 1160px;
  margin: 0 auto;
  padding: 28px 24px 96px;
  display: grid;
  grid-template-columns: minmax(0, 700px) minmax(300px, 380px);
  grid-template-areas:
    "letter machine"
    "notes machine";
  column-gap: 48px;
  align-items: start;
}

.letter {
  grid-area: letter;
}

.machine {
  grid-area: machine;
  position: sticky;
  top: 20px;
}

.notes {
  grid-area: notes;
}

/* The letter */

.sheet {
  position: relative;
  background: var(--sheet);
  border: 11px solid transparent;
  border-image: repeating-linear-gradient(
      -45deg,
      var(--air-red) 0 13px,
      var(--sheet) 13px 22px,
      var(--air-blue) 22px 35px,
      var(--sheet) 35px 44px
    )
    11;
  box-shadow: 0 1px 0 rgba(28, 42, 79, 0.06), 0 18px 40px -24px rgba(28, 42, 79, 0.45);
  padding: 44px 52px 40px;
  font-family: var(--serif);
  font-size: 19px;
  line-height: 1.68;
}

.letter-body {
  outline: none;
  border-radius: 2px;
  caret-color: var(--proof);
}

.letter-body:focus-visible {
  box-shadow: 0 0 0 6px var(--sheet), 0 0 0 7px var(--rule);
}

.letter-body p {
  margin: 0 0 1em;
}

.letter-body p:first-child {
  margin-bottom: 1.1em;
}

.signoff {
  margin-top: 1.4em;
}

.ps {
  margin: 1.6em 0 0;
  padding-top: 1.1em;
  border-top: 1px solid var(--rule);
  font-size: 17px;
  line-height: 1.7;
  min-height: 9em;
}

.ps .item {
  border-radius: 2px;
  transition: color 0.2s;
  cursor: pointer;
}

.ps .num {
  font-style: italic;
}

.ps.unsettled .item.off .num {
  color: var(--proof);
  text-decoration: underline wavy color-mix(in srgb, var(--proof) 55%, transparent);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.ps .item:hover .num,
.ps .item.peek .num {
  text-decoration: underline;
  text-decoration-thickness: 1.5px;
  text-underline-offset: 3px;
}

::highlight(peek) {
  background-color: var(--mark);
  color: var(--ink);
}

::highlight(tally) {
  background-color: var(--mark);
  color: var(--ink);
}

::highlight(tally-head) {
  background-color: var(--mark-strong);
  color: var(--ink);
}

/* Postmark: made only of numbers and ticks so it never enters the count. */

.postmark {
  position: absolute;
  top: -16px;
  right: 2px;
  width: 188px;
  color: var(--stamp);
  opacity: 0;
  transform: rotate(-9deg) scale(1.25);
  pointer-events: none;
  mix-blend-mode: multiply;
}

@media (prefers-color-scheme: dark) {
  .postmark {
    mix-blend-mode: screen;
  }
}

.postmark.on {
  opacity: 0.82;
  transform: rotate(-9deg) scale(1);
  transition: opacity 0.12s ease-out, transform 0.22s cubic-bezier(0.2, 0.9, 0.25, 1.15);
}

.postmark.fade {
  opacity: 0;
  transform: rotate(-9deg) scale(1);
  transition: opacity 0.3s;
}

.postmark svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
}

.pm-big {
  font-family: var(--sans);
  font-weight: 700;
  font-size: 30px;
  letter-spacing: 0.02em;
}

.pm-date {
  font-family: var(--sans);
  font-weight: 600;
  font-size: 12.5px;
  letter-spacing: 0.08em;
}

.pm-stars {
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: 0.3em;
}

/* The machine */

.machine h2,
.notes h2 {
  font-family: var(--serif);
  font-weight: 500;
  font-style: italic;
  font-size: 24px;
  line-height: 1.2;
  margin: 0 0 8px;
}

.hint {
  margin: 0 0 16px;
  color: var(--ink-soft);
  font-size: 14px;
}

.case {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 1px;
  background: var(--rule);
  border: 1px solid var(--rule);
  font-variant-numeric: tabular-nums;
}

.drawer {
  position: relative;
  appearance: none;
  border: 0;
  margin: 0;
  padding: 6px 7px 9px;
  min-height: 62px;
  background: var(--drawer);
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: background-color 0.25s;
}

.drawer:hover,
.drawer.peeking {
  background: color-mix(in srgb, var(--mark) 55%, var(--drawer));
}

.drawer .glyph {
  font-family: var(--serif);
  font-style: italic;
  font-size: 24px;
  line-height: 1;
}

.drawer .n {
  align-self: flex-end;
  font-weight: 600;
  font-size: 15px;
  line-height: 1;
}

.drawer .delta {
  position: absolute;
  top: 6px;
  right: 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--proof);
}

.drawer .bar {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  width: calc(var(--f, 0) * 100%);
  background: color-mix(in srgb, var(--ink-soft) 45%, transparent);
}

.drawer.off {
  background: color-mix(in srgb, var(--proof) 13%, var(--drawer));
}

.drawer.off .n {
  color: var(--proof);
}

.drawer.counting {
  background: var(--mark);
}

.drawer.counted .n::after {
  content: " ✓";
  color: var(--air-blue);
}

.tally {
  grid-column: span 4;
  background: var(--drawer);
  padding: 8px 10px;
  font-size: 13px;
  color: var(--ink-soft);
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 1px;
}

.tally-n {
  color: var(--ink);
  font-weight: 600;
}

.status {
  margin: 14px 0 16px;
  min-height: 3em;
  font-size: 14.5px;
}

.status strong {
  font-weight: 600;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.controls button {
  appearance: none;
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  padding: 8px 13px;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--sheet);
  color: var(--ink);
  cursor: pointer;
}

.controls button:hover {
  border-color: var(--ink-soft);
}

.controls button.primary {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--sheet);
}

.controls button.primary:hover {
  background: color-mix(in srgb, var(--ink) 85%, var(--air-blue));
}

.slow {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  padding: 4px 2px;
  cursor: pointer;
  user-select: none;
}

.slow input {
  accent-color: var(--air-blue);
  width: 16px;
  height: 16px;
  margin: 0;
}

/* Notes */

.notes {
  max-width: 62ch;
  margin-top: 64px;
  font-family: var(--serif);
  font-size: 18px;
  line-height: 1.65;
}

.notes h2 {
  margin-top: 40px;
}

.notes h2:first-child {
  margin-top: 0;
}

.notes p {
  margin: 0 0 1em;
}

.notes ol {
  margin: 0 0 1.1em;
  padding-left: 1.4em;
}

.notes li {
  padding-left: 0.3em;
  margin-bottom: 0.25em;
}

.notes li::marker {
  font-family: var(--sans);
  font-size: 0.85em;
  font-weight: 600;
  color: var(--ink-soft);
}

.notes blockquote {
  margin: 0 0 1em;
  padding: 2px 0 2px 18px;
  border-left: 3px solid var(--air-red);
  font-style: italic;
  color: var(--ink-soft);
}

#loop-fact,
#rate {
  color: var(--ink);
}

/* Small screens */

@media (max-width: 960px) {
  .masthead {
    max-width: 748px;
  }

  .desk {
    grid-template-columns: minmax(0, 700px);
    justify-content: center;
    grid-template-areas:
      "letter"
      "machine"
      "notes";
    row-gap: 36px;
  }

  .machine {
    position: static;
  }

  .notes {
    margin-top: 12px;
  }
}

@media (max-width: 600px) {
  .masthead {
    padding: 20px 16px 4px;
  }

  .desk {
    padding: 20px 16px 72px;
  }

  .sheet {
    border-width: 7px;
    border-image-slice: 7;
    padding: 26px 20px 24px;
    font-size: 17.5px;
  }

  .ps {
    font-size: 16px;
  }

  .postmark {
    width: 132px;
    top: -12px;
    right: -4px;
  }

  .drawer {
    min-height: 56px;
  }

  .notes {
    font-size: 17px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .postmark.on,
  .postmark.fade {
    transition: none;
  }

  .drawer,
  .ps .item {
    transition: none;
  }
}
