:root {
  --kraft: #c9a06c;
  --kraft-deep: #b98f5c;
  --ink: #221b38;
  --paper: #fff6e0;
  --marigold: #ffc233;
  --air-red: #c6341f;
  --air-blue: #1d4f8c;
  --display: Rockwell, "Roboto Slab", "DejaVu Serif", Georgia, serif;
  --text: "Gill Sans", "Gill Sans MT", "Avenir Next", "Trebuchet MS", "Segoe UI", sans-serif;
  --step--1: 0.875rem;
  --step-0: 1rem;
  --step-1: 1.25rem;
  --step-2: 1.6rem;
  /* Fibres in the kraft paper. Generated locally, nothing is fetched. */
  --kraft-fibres: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='260' height='260'%3E%3Cfilter id='f'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9 .18' numOctaves='3' seed='7'/%3E%3CfeColorMatrix values='0 0 0 0 .25 0 0 0 0 .15 0 0 0 0 .05 0 0 0 .55 -.14'/%3E%3C/filter%3E%3Crect width='260' height='260' filter='url(%23f)'/%3E%3C/svg%3E");
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  font-family: var(--text);
  font-size: var(--step-0);
  line-height: 1.4;
  background-color: var(--kraft);
  background-image: var(--kraft-fibres);
}

h1, h2, h3 { font-family: var(--display); margin: 0; line-height: 1.05; font-weight: 700; }
p { margin: 0; }
[hidden] { display: none !important; }

:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 2px;
  box-shadow: 0 0 0 2px var(--marigold);
}

.screen { min-height: 100dvh; padding: 16px; }

/* Stamps */
.stamp {
  background: var(--paper);
  padding: 13px;
  position: relative;
}
.stamp-canvas {
  display: block;
  width: 100%;
  aspect-ratio: 3 / 4;
  background: var(--ink);
}

/* Buttons look like rubber-stamped docket labels, not app chrome. */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 8px 16px;
  font: 700 var(--step-0)/1.1 var(--display);
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 3px;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:hover { background: #fff; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--ink); color: var(--paper); box-shadow: 0 0 0 2px var(--ink), inset 0 0 0 2px var(--paper); }
.btn-primary:hover { background: #3a2e5c; }
.btn:disabled, .btn-small:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-small {
  min-width: 44px;
  min-height: 44px;
  font: 700 var(--step-1)/1 var(--display);
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 3px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.btn-small:active { background: var(--marigold); }
.btn-wide { padding: 0 14px; font-size: var(--step-0); }

.toggle { display: inline-flex; gap: 8px; align-items: center; min-height: 44px; font-weight: 600; cursor: pointer; }
.toggle input { width: 20px; height: 20px; accent-color: var(--ink); }

.actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

.stepper { display: inline-flex; align-items: center; gap: 8px; }
.stepper output { min-width: 2.2em; text-align: center; font: 700 var(--step-1)/1 var(--display); }

.fine { font-size: var(--step--1); }

/* Title */
.title-screen {
  display: grid;
  gap: 12px 14px;
  grid-template-columns: minmax(0, clamp(112px, min(38vw, 18dvh), 170px)) minmax(0, 1fr);
  align-content: start;
  padding-bottom: 0;
}
.title-copy { display: contents; }
.title-screen h1 {
  grid-column: 1 / -1;
  order: 1;
  font-size: clamp(2.2rem, min(11.8vw, 17vh), 5.6rem);
  letter-spacing: -0.02em;
  white-space: nowrap;
}
.title-stamp-wrap { order: 2; filter: drop-shadow(2px 3px 0 rgb(60 35 10 / .35)); }
.title-stamp-wrap .stamp { padding: 7px; }
.lede { order: 3; align-self: center; font-size: var(--step-1); line-height: 1.25; }
.rules { list-style: none; margin: 0; padding: 0; display: grid; gap: 7px; max-width: 62ch; }
.rules li { line-height: 1.3; }
.rules strong { font-family: var(--display); }
.title-screen .rules { order: 4; grid-column: 1 / -1; font-size: 0.95rem; }
.keys { font-size: var(--step--1); }
.title-screen .keys { order: 5; grid-column: 1 / -1; }
.route-row { order: 6; grid-column: 1 / -1; display: grid; grid-template-columns: auto auto minmax(0, 1fr); gap: 4px 10px; align-items: center; max-width: 62ch; }
.route-label { font: 700 var(--step-1)/1 var(--display); }
.route-note { font-size: var(--step--1); line-height: 1.2; }
.title-screen .fine { order: 7; grid-column: 1 / -1; }
/* Start stays reachable on the shortest phones and iframes. */
.title-screen .actions {
  order: 8;
  grid-column: 1 / -1;
  position: sticky;
  bottom: 0;
  margin-inline: -16px;
  padding: 10px 16px 14px;
  background: var(--kraft) var(--kraft-fibres);
  gap: 8px;
}
.title-screen .actions .btn { padding-inline: 12px; }
@media (hover: none) { .title-screen .keys { display: none; } }

@media (min-width: 860px) {
  .title-screen {
    grid-template-columns: minmax(0, auto) minmax(0, 62ch);
    justify-content: center;
    align-content: center;
    column-gap: clamp(32px, 5vw, 72px);
    padding-bottom: 16px;
  }
  .title-stamp-wrap { order: 0; width: min(36vw, calc((100dvh - 80px) * .75)); max-width: 500px; grid-column: 1; grid-row: 1 / span 8; align-self: center; }
  .title-stamp-wrap .stamp { padding: 15px; }
  .title-screen h1, .lede, .title-screen .rules, .title-screen .keys, .route-row, .title-screen .fine, .title-screen .actions { grid-column: 2; }
  .title-screen h1 { font-size: clamp(3.6rem, 5.4vw, 5.2rem); white-space: normal; }
  .title-screen .actions { position: static; margin: 0; padding: 0; background: none; gap: 10px; }
  .title-screen .actions .btn { padding-inline: 16px; }
  .title-screen .rules { font-size: var(--step-0); }
}

/* Play */
.play-screen {
  display: grid;
  height: 100dvh;
  min-height: 0;
  padding: 8px;
  gap: 6px;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: "head" "field" "docket";
}
.play-head { grid-area: head; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.play-head h2 { font-size: var(--step-1); }
.play-head p { font-size: var(--step--1); line-height: 1.2; }
.field-area { grid-area: field; display: flex; justify-content: center; align-items: center; min-height: 0; container-type: size; }
.field-stamp { padding: 8px; width: min(100cqw, 75cqh); }
.field-inner { position: relative; }
#field { touch-action: none; cursor: grab; }
#field:active { cursor: grabbing; }
#field:focus-visible { outline: 3px solid var(--marigold); outline-offset: -3px; box-shadow: none; }

.drag-hint {
  position: absolute;
  inset: 10px 10px auto;
  padding: 6px 10px;
  text-align: center;
  background: var(--paper);
  color: var(--ink);
  font: 700 var(--step-0)/1.2 var(--display);
  border-radius: 2px;
  pointer-events: none;
}

.docket { grid-area: docket; display: grid; gap: 4px; }
.tip { display: none; }
.postage { display: flex; align-items: center; gap: 10px; justify-content: space-between; }
.postage-tiles { display: flex; flex-wrap: wrap; gap: 2px; }
.tile { width: 12px; height: 15px; background: var(--paper); border: 1.5px dashed var(--ink); border-radius: 1px; }
.tile.spent { background: transparent; border-color: rgb(34 27 56 / .35); }
.postage-text { font: 700 var(--step-0)/1 var(--display); white-space: nowrap; }
.message { min-height: 1.3em; font-size: var(--step--1); font-weight: 600; line-height: 1.2; }
.controls { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: end; }
.dial-label { display: block; font-size: var(--step--1); font-weight: 600; line-height: 1.1; }
.dial-row { display: flex; align-items: center; justify-content: space-between; gap: 2px; }
.dial-row output { flex: 1; text-align: center; font: 700 var(--step-0)/1 var(--display); min-width: 2.9em; }
.dial-row .btn-small { min-width: 40px; }
.launch { min-height: 44px; padding-inline: 14px; }
.keys-play { display: none; }

@media (max-width: 370px) {
  .play-head p { display: none; }
  .controls { grid-template-columns: 1fr 1fr; }
  .launch { grid-column: 1 / -1; }
}

/* Side by side on laptops and on phones held sideways. */
@media (min-width: 860px), (min-width: 560px) and (orientation: landscape) {
  .play-screen {
    padding: 16px;
    gap: 12px 28px;
    grid-template-columns: minmax(0, 1fr) minmax(260px, 340px);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "field head" "field docket";
    max-width: 1100px;
    margin-inline: auto;
  }
  .field-stamp { padding: 13px; }
  .play-head { flex-direction: column; align-items: flex-start; }
  .play-head h2 { font-size: var(--step-2); }
  .play-head p { font-size: var(--step-0); }
  .docket { align-content: start; gap: 14px; }
  .tip { display: block; }
  .postage { flex-direction: column; align-items: flex-start; }
  .tile { width: 15px; height: 19px; }
  .message { min-height: 2.6em; font-size: var(--step-0); }
  .controls { grid-template-columns: 1fr; }
  .dial-row .btn-small { min-width: 56px; }
  .launch { min-height: 54px; font-size: var(--step-1); }
  .keys-play { display: block; }
}

/* Tall phones have room for the course tip under the stamp. */
@media (max-width: 859px) and (min-height: 780px) and (orientation: portrait) {
  .tip { display: block; font-size: var(--step--1); line-height: 1.25; }
}

/* A short iframe keeps the stamp and the throw button, and sheds everything else. */
@media (max-height: 560px), (max-width: 370px) and (max-height: 600px) {
  .play-head p { display: none; }
  .postage-tiles { display: none; }
  /* Off screen, but still read out by screen readers. */
  .message { position: absolute; width: 1px; height: 1px; min-height: 0; overflow: hidden; clip-path: inset(50%); }
}

@media (max-height: 560px) and (min-width: 560px) and (orientation: landscape) {
  .play-screen { padding: 8px; gap: 6px 16px; }
  .field-stamp { padding: 8px; }
  .play-head { flex-direction: row; align-items: center; }
  .play-head h2 { font-size: var(--step-1); }
  .docket { gap: 6px; }
  .tip, .keys-play { display: none; }
  .controls { grid-template-columns: 1fr 1fr; }
  .launch { grid-column: 1 / -1; min-height: 46px; font-size: var(--step-0); }
  .dial-row .btn-small { min-width: 44px; }
}

/* Course cleared */
.cleared {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px;
}
.cleared.mark-top .postmark { margin-bottom: auto; margin-top: 7%; }
.postmark { width: min(82%, 300px); color: var(--paper); transform-origin: 50% 60%; animation: slam 260ms cubic-bezier(.2, 1.5, .4, 1) both; rotate: -8deg; }
.postmark-mark { display: block; width: 100%; height: auto; }
@keyframes slam {
  from { transform: scale(2.1); opacity: 0; }
  60% { opacity: 1; }
  to { transform: scale(1); opacity: 1; }
}
.cleared-card { width: 100%; background: var(--paper); color: var(--ink); padding: 12px; border: 2px solid var(--ink); }
.cleared-card h3 { font-size: var(--step-1); }
.cleared-card p { font-size: var(--step--1); margin: 4px 0 10px; }
.cleared-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.cleared-actions .btn { flex: 1 1 auto; min-height: 44px; padding: 6px 10px; }

/* Result */
.result-screen {
  display: grid;
  gap: 14px;
  align-content: start;
  max-width: 1000px;
  margin-inline: auto;
}
.result-head h2 { font-size: clamp(2rem, 8vw, 3.4rem); letter-spacing: -0.01em; }
.result-score { font: 700 clamp(1.5rem, 5vw, 2.2rem)/1.1 var(--display); margin-top: 6px; }
.result-summary { max-width: 62ch; margin-top: 4px; }
.sheet {
  list-style: none;
  margin: 0;
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  background: #e8d9b5;
  /* The airmail edge marks this as the thing you could send to someone. */
  border: 8px solid transparent;
  border-image: repeating-linear-gradient(-45deg, var(--air-red) 0 12px, var(--paper) 12px 20px, var(--air-blue) 20px 32px, var(--paper) 32px 40px) 8;
}
@media (min-width: 620px) { .sheet { grid-template-columns: repeat(5, minmax(0, 1fr)); } }
.sheet-item { display: grid; gap: 4px; align-content: start; }
.sheet-btn {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}
.sheet-btn:disabled { cursor: default; }
.mini { position: relative; background: var(--paper); padding: 5px; filter: drop-shadow(1px 2px 0 rgb(60 35 10 / .4)); }
.mini-wrap { position: relative; }
.mini-mark { position: absolute; left: 3%; width: 58%; bottom: 7%; color: var(--paper); rotate: -8deg; opacity: .92; }
.mini.undelivered canvas { filter: grayscale(.85) contrast(.8) brightness(1.15); }
.sheet-caption { font-size: var(--step--1); line-height: 1.2; }
.sheet-caption strong { font-family: var(--display); display: block; }
.sheet-hint { font-size: var(--step--1); }
.tally { display: grid; grid-template-columns: auto auto; gap: 2px 18px; margin: 0; justify-content: start; }
.tally dt { margin: 0; }
.tally dd { margin: 0; text-align: right; font-family: var(--display); font-weight: 700; }
.tally .total { border-top: 2px solid var(--ink); padding-top: 3px; }
.best { font-weight: 600; }

/* Dialogs */
.sheet-dialog {
  max-width: min(92vw, 560px);
  max-height: 92dvh;
  overflow: auto;
  padding: 20px;
  color: var(--ink);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 3px;
}
.sheet-dialog::backdrop { background: rgb(34 27 56 / .68); }
.sheet-dialog h2 { font-size: var(--step-2); margin-bottom: 8px; }
.sheet-dialog p { margin-bottom: 10px; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 12px; }
.replay-dialog { padding: 14px; }
.replay-stamp { width: min(100%, calc((92dvh - 210px) * .75)); min-width: 180px; margin-inline: auto; padding: 8px; background: var(--kraft); }

@media (prefers-reduced-motion: reduce) {
  .postmark { animation: none; }
  .btn:active { transform: none; }
}
