:root {
  --bg: #e9e3d1;
  --paper: #fbf6e9;
  --paper-shade: rgba(128, 96, 44, 0.12);
  --paper-light: rgba(255, 255, 255, 0.4);
  --ink: #1c3254;
  --ink-soft: #66748a;
  --rule: rgba(28, 50, 84, 0.24);
  --link: #0e58a8;
  --accent: #c8102e;
  --stamp-paper: #fffdf7;
  --shadow: rgba(18, 28, 40, 0.42);
  --tilt: -1.6deg;
  --theme-fade: 1.4s;
}

html.night {
  --bg: #0b101c;
  --paper: #1d2334;
  --paper-shade: rgba(0, 0, 0, 0.28);
  --paper-light: rgba(150, 170, 220, 0.07);
  --ink: #ebe5d3;
  --ink-soft: #9aa5ba;
  --rule: rgba(235, 229, 211, 0.2);
  --link: #a9ccff;
  --accent: #ffa08a;
  --stamp-paper: #d6d0c0;
  --shadow: rgba(0, 0, 0, 0.62);
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  transition: background var(--theme-fade) ease;
}

body {
  min-height: 100dvh;
  overflow-x: hidden;
  font-family: "Crimson Text", Georgia, serif;
  font-size: 1.2rem;
  font-weight: 400;
  color: var(--ink);
}

/* The painting: a persistent canvas, the things that move, and paper grain. */
#scene,
#sprites,
.grain {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  height: 100lvh;
}

#scene {
  z-index: 0;
  cursor: pointer;
}

#sprites,
.grain {
  z-index: 0;
  pointer-events: none;
}

.grain {
  background-image: var(--grain-day);
  background-size: 256px 256px;
}

html.night .grain {
  background-image: var(--grain-night);
}

/* ?grain=overlay: blended, as it was; dearer, the whole window is worked out anew every frame */
html.grain-overlay .grain {
  background-image: var(--grain);
  mix-blend-mode: overlay;
  opacity: 0.5;
}

html.grain-overlay.night .grain {
  opacity: 0.32;
}

main {
  position: relative;
  z-index: 1;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(1.6rem, 5vw, 3.2rem) clamp(0.9rem, 3.4vw, 2.6rem) clamp(3rem, 6vw, 3.6rem);
  box-sizing: border-box;
  pointer-events: none;
}

/* The postcard */
.postcard {
  position: relative;
  box-sizing: border-box;
  width: min(760px, 100%);
  padding: clamp(1.1rem, 2.6vw, 1.7rem) clamp(1.1rem, 3vw, 2rem) clamp(1.2rem, 2.6vw, 1.75rem);
  display: grid;
  grid-template-columns: minmax(0, 1.12fr) minmax(0, 1fr);
  grid-template-areas: "head head" "message address";
  background:
    linear-gradient(128deg, var(--paper-light), transparent 42%),
    radial-gradient(130% 100% at 50% 38%, transparent 58%, var(--paper-shade)),
    var(--paper);
  border-radius: 3px;
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.06),
    0 1px 2px rgba(0, 0, 0, 0.22),
    0 22px 50px var(--shadow),
    0 6px 14px rgba(0, 0, 0, 0.16);
  color: var(--ink);
  transform: rotate(var(--tilt));
  pointer-events: auto;
  transition: background-color var(--theme-fade) ease, color var(--theme-fade) ease, box-shadow var(--theme-fade) ease;
}

.postcard::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background-image: var(--grain);
  background-size: 256px 256px;
  mix-blend-mode: overlay;
  opacity: 0.3;
  pointer-events: none;
}

html.night .postcard::before {
  mix-blend-mode: soft-light;
  opacity: 0.3;
}

.js .postcard {
  opacity: 0;
  transform: translateY(28px) rotate(-5.5deg) scale(1.03);
}

.js .postcard.in {
  opacity: 1;
  transform: rotate(var(--tilt));
  transition:
    opacity 0.7s ease-out,
    transform 1.15s cubic-bezier(0.2, 0.9, 0.25, 1),
    background-color var(--theme-fade) ease,
    color var(--theme-fade) ease,
    box-shadow var(--theme-fade) ease;
}

/* Putting the postcard away, and the small one that brings it back */
.js .postcard.in.closed {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translate(-34vw, 46vh) rotate(-13deg) scale(0.22);
  transition:
    transform 0.75s cubic-bezier(0.55, 0, 0.75, 0.35),
    opacity 0.35s ease 0.4s,
    visibility 0s 0.75s;
}

.card-close {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  width: clamp(1.7rem, 3.4vw, 2.3rem);
  height: clamp(1.7rem, 3.4vw, 2.3rem);
  padding: 0;
  border: 0;
  background: none;
  color: var(--ink-soft);
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.2s ease, color 0.2s ease;
}

.js .card-close {
  display: block;
}

/* a small cross, drawn in the corner of the card with the same ink */
.card-close svg {
  display: block;
  width: 1.05rem;
  height: 1.05rem;
  margin: auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
}

.card-close:hover,
.card-close:focus-visible {
  opacity: 1;
  color: var(--accent);
  outline: none;
}

.mini-card {
  position: fixed;
  left: 0.9rem;
  bottom: 0.8rem;
  z-index: 2;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 0.55rem;
  align-items: center;
  width: 11.5rem;
  padding: 0.5rem 0.65rem 0.55rem 0.5rem;
  border: 0;
  border-radius: 2px;
  background: linear-gradient(128deg, var(--paper-light), transparent 45%), var(--paper);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25), 0 8px 20px var(--shadow);
  font: italic 600 1.05rem/1.1 "Crimson Text", Georgia, serif;
  text-align: left;
  cursor: pointer;
  transform: rotate(-4deg);
  transform-origin: 0 100%;
  animation: mini-in 0.5s cubic-bezier(0.2, 1.3, 0.4, 1) 0.45s both;
  transition: transform 0.25s ease, color var(--theme-fade) ease, background-color var(--theme-fade) ease;
}

.mini-card[hidden] {
  display: none;
}

.mini-card:hover,
.mini-card:focus-visible {
  outline: none;
  transform: rotate(-1.5deg) translateY(-3px) scale(1.04);
}

.mini-card img {
  grid-row: 1 / 3;
  width: 2.3rem;
  height: 2.3rem;
  object-fit: cover;
  border: 2px solid var(--stamp-paper);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15);
  filter: sepia(0.28) saturate(0.9);
}

.mini-name {
  white-space: nowrap;
}

.mini-lines {
  height: 0.5rem;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  margin-top: 0.3rem;
}

@keyframes mini-in {
  from { opacity: 0; transform: translateY(30px) rotate(-12deg) scale(0.7); }
  to { opacity: 1; transform: rotate(-4deg); }
}

/* Name and tagline */
.card-head {
  grid-area: head;
  position: relative;
  padding-right: clamp(100px, 15%, 124px);
  margin-bottom: clamp(0.8rem, 1.8vw, 1.1rem);
  min-height: clamp(80px, 10vw, 94px);
}

.postcard h1 {
  margin: 0 0 0.22em;
  font-weight: 600;
  font-size: clamp(2rem, 1.4rem + 1.6vw, 2.6rem);
  line-height: 0.95;
  color: var(--ink);
  transition: color var(--theme-fade) ease;
}

.tagline {
  margin: 0;
  font-size: clamp(1rem, 0.9rem + 0.3vw, 1.12rem);
  font-style: italic;
  color: var(--ink-soft);
  transition: color var(--theme-fade) ease;
}

/* The stamp */
.stamp-area {
  position: absolute;
  top: clamp(0.9rem, 2vw, 1.3rem);
  right: clamp(0.9rem, 2.3vw, 1.5rem);
  width: clamp(70px, 9vw, 86px);
  transform: rotate(2.4deg);
}

.stamp-shadow {
  filter: drop-shadow(0 1px 1.2px rgba(0, 0, 0, 0.42)) drop-shadow(0 3px 5px rgba(0, 0, 0, 0.12));
}

.stamp {
  position: relative;
  display: grid;
  box-sizing: border-box;
  padding: 9px;
  background: var(--stamp-paper);
  -webkit-mask:
    linear-gradient(#000 0 0) center / calc(100% - 9px) calc(100% - 9px) no-repeat,
    radial-gradient(circle, #000 3.3px, transparent 3.8px) 0 0 / 9px 9px round;
  mask:
    linear-gradient(#000 0 0) center / calc(100% - 9px) calc(100% - 9px) no-repeat,
    radial-gradient(circle, #000 3.3px, transparent 3.8px) 0 0 / 9px 9px round;
  transition: background-color var(--theme-fade) ease;
}

.stamp img {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  filter: sepia(0.28) saturate(0.9) contrast(1.04);
  transition: filter var(--theme-fade) ease;
}

html.night .stamp img {
  filter: sepia(0.3) saturate(0.8) brightness(0.82);
}

.stamp img.avatar-photo {
  grid-area: 1 / 1;
  filter: none;
}

/* The message, written on the left, and the address lines on the right */
.message {
  grid-area: message;
  padding-right: clamp(1rem, 2.2vw, 1.5rem);
  font-size: clamp(1.02rem, 0.85rem + 0.45vw, 1.16rem);
}

.message p {
  margin: 0 0 0.7em;
  line-height: 1.45;
}

.message p:last-child {
  margin-bottom: 0;
}

.address {
  grid-area: address;
  list-style: none;
  margin: 0;
  padding: 0 0 0 clamp(1rem, 2.2vw, 1.5rem);
  border-left: 1px solid var(--rule);
  font-size: clamp(1rem, 0.85rem + 0.4vw, 1.1rem);
  transition: border-color var(--theme-fade) ease;
}

.address li {
  padding: 0.24em 0 0.1em;
  border-bottom: 1px solid var(--rule);
  line-height: 1.3;
  transition: border-color var(--theme-fade) ease;
}

.address li:first-child {
  padding-top: 0;
}

.postcard a {
  color: var(--link);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.16em;
  border-radius: 3px;
  transition: color 0.2s ease;
}

.postcard a:hover,
.postcard a:focus-visible {
  color: var(--accent);
  outline: none;
}

.postcard a:focus-visible {
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 45%, transparent);
}

/* Every word is written in, one after the other. */
.js .write .w {
  display: inline-block;
  position: relative;
  opacity: 0;
}

.js .writing .w {
  animation: write-in calc(var(--d, 80ms) * var(--k, 1)) linear calc(var(--t, 0ms) * var(--k, 1) + var(--o, 0ms)) both;
}

.js .written .w {
  opacity: 1;
  animation: none;
}

@keyframes write-in {
  from { opacity: 1; clip-path: inset(-0.3em calc(100% + 0.3em) -0.3em -0.2em); }
  to { opacity: 1; clip-path: inset(-0.3em -0.2em -0.3em -0.2em); }
}

/* Words are inline blocks, which an underline does not reach: links get a pen stroke instead. */
.js .postcard a {
  text-decoration: none;
}

.js .postcard a .w::after {
  content: "";
  position: absolute;
  left: -0.12em;
  right: -0.12em;
  bottom: -0.04em;
  height: 0.3em;
  background: currentColor;
  opacity: 0.6;
  -webkit-mask: var(--stroke) center / 100% 100% no-repeat;
  mask: var(--stroke) center / 100% 100% no-repeat;
}

.postcard {
  --stroke: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 8' preserveAspectRatio='none'%3E%3Cpath d='M1,5 C12,2.6 22,6.4 36,4.3 S62,5.8 76,3.9 S93,5 99,3.5' fill='none' stroke='%23000' stroke-width='1.5' stroke-linecap='round' vector-effect='non-scaling-stroke'/%3E%3C/svg%3E");
}

/* Place, season, weather and theme, pencilled into the corner */
.controls {
  position: fixed;
  right: 0.8rem;
  bottom: 0.7rem;
  z-index: 2;
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
  transform: rotate(var(--tilt));
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.control {
  position: relative;
  display: flex;
  align-items: stretch;
}

.control[hidden] {
  display: none;
}

.control > .value,
.control > .more,
.fan-item {
  font: italic 0.95rem/1.3 "Crimson Text", Georgia, serif;
  color: var(--ink);
  background: var(--paper);
  border: 0;
  border-radius: 2px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25), 0 4px 10px var(--shadow);
  cursor: pointer;
  opacity: 0.86;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  transition: opacity 0.2s ease, color var(--theme-fade) ease, background-color var(--theme-fade) ease;
}

.control > .value {
  box-sizing: border-box;
  padding: 0.08rem 0.6rem 0.14rem;
  white-space: nowrap;
  touch-action: none;
}

.control:hover > .value,
.control:hover > .more,
.control.open > .value,
.control.open > .more,
.control > .value:focus-visible,
.control > .more:focus-visible {
  opacity: 1;
  outline: none;
}

.control > .value:focus-visible,
.control > .more:focus-visible,
.fan-item:focus-visible {
  color: var(--accent);
}

/* the little arrow, where there is a mouse: the other end of the same slip of paper */
.control > .more {
  display: none;
  position: relative;
  padding: 0 0.4rem 0 0.32rem;
}

.control > .more svg {
  display: block;
  width: 0.6rem;
  height: 0.45rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.3;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.7;
  transition: transform 0.2s ease;
}

.control.open > .more svg {
  transform: rotate(180deg);
}

@media (hover: hover) and (pointer: fine) {
  .control > .more {
    display: block;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
  }

  .control > .value {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }

  .control > .more::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0.3rem;
    bottom: 0.3rem;
    border-left: 1px solid var(--rule);
  }
}

/* What a button can be set to, fanned out from it */
.fan {
  position: absolute;
  top: 0;
  right: 0;
  width: 0;
  height: 0;
  z-index: 3;
}

.fan-item {
  position: absolute;
  top: 0;
  right: 0;
  padding: 0.08rem 0.6rem 0.14rem;
  white-space: nowrap;
  opacity: 0;
  touch-action: none;
  transform-origin: 100% 50%;
  transform: translate(0, var(--from, 0)) rotate(0deg) scale(0.6);
  transition:
    transform 0.16s ease-in,
    opacity 0.12s ease-in,
    color 0.15s ease;
}

.fan.up .fan-item {
  --from: -0.5rem;
}

.fan.down .fan-item {
  --from: 0.5rem;
}

.fan.out .fan-item {
  opacity: 0.94;
  transform: translate(calc(var(--x) - var(--push, 0px)), var(--y)) rotate(var(--r)) scale(var(--s, 1));
  transition:
    transform 0.34s cubic-bezier(0.2, 1.25, 0.4, 1) calc(var(--i) * 22ms),
    opacity 0.18s ease-out calc(var(--i) * 22ms),
    color 0.15s ease;
}

.fan-item.current {
  color: var(--link);
}

/* the one a finger is on, or the mouse */
.fan.out .fan-item.hot,
.fan.out .fan-item:focus-visible {
  --push: 0.45rem;
  --s: 1.07;
  opacity: 1;
  outline: none;
  transition-delay: 0s;
}

@media (hover: hover) {
  .fan.out .fan-item:hover {
    --push: 0.45rem;
    --s: 1.07;
    opacity: 1;
    transition-delay: 0s;
  }
}

.fan.in .fan-item.picked {
  opacity: 1;
  transform: translate(0, 0) rotate(0deg) scale(0.9);
}

@media only screen and (max-width: 380px) {
  .controls {
    right: 0.5rem;
    gap: 0.3rem;
  }

  .control > .value,
  .fan-item {
    font-size: 0.9rem;
    padding: 0.08rem 0.45rem 0.14rem;
  }
}

@media only screen and (max-width: 700px) {
  :root {
    --tilt: -1deg;
  }

  .mini-card {
    bottom: 3.1rem;
  }

  /* upright phones: the place has a line of its own under the others, so a long name pushes nothing aside */
  .controls {
    display: grid;
    grid-template-columns: repeat(3, auto);
    justify-content: end;
    justify-items: end;
    row-gap: 0.4rem;
  }

  .control[data-kind="place"] {
    grid-row: 2;
    grid-column: 1 / -1;
  }

  .postcard {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "head" "message" "address";
  }

  .card-head {
    padding-right: clamp(92px, 27vw, 124px);
    min-height: 96px;
  }

  .message {
    padding-right: 0;
    margin-bottom: 1rem;
  }

  .address {
    padding-left: 0;
    border-left: 0;
    border-top: 1px solid var(--rule);
    padding-top: 0.5em;
  }
}

/* Small screens, upright or on their side: a smaller card.  The name goes on one line beside a smaller stamp,
   and the addresses lose their little sentences and go two to a line. */
@media only screen and (max-width: 700px), only screen and (max-height: 500px) {
  .postcard {
    padding: 1rem 1.1rem 1.15rem;
  }

  .card-head {
    padding-right: 70px;
    min-height: 62px;
    margin-bottom: 0.85rem;
  }

  .postcard h1 {
    white-space: nowrap;
    font-size: clamp(1.7rem, 1.1rem + 2.6vw, 2.2rem);
    margin-bottom: 0.14em;
  }

  .tagline {
    font-size: 1rem;
  }

  .stamp-area {
    top: 0.8rem;
    right: 0.85rem;
    width: 64px;
  }

  .stamp {
    padding: 9px;
  }

  .message {
    font-size: 1.05rem;
  }

  .message p {
    line-height: 1.42;
  }

  .address {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    column-gap: 0.9rem;
    font-size: 1.05rem;
  }

  .address li {
    white-space: nowrap;
  }

  .address li:nth-child(2) {
    padding-top: 0;
  }

  .address .lead {
    display: none;
  }

  .address .via {
    margin-left: 0.3em;
    font-size: 0.82em;
    font-style: italic;
    color: var(--ink-soft);
  }

  /* the picture's buttons go to the top, out of the card's way */
  .controls {
    top: 0.6rem;
    bottom: auto;
    align-items: flex-start;
  }

  .mini-card {
    bottom: 0.8rem;
  }

  /* ?card=peek (the default): the picture has the screen, the card is tucked in at its foot and is pulled up
     by scrolling the page, or by a tap on it */
  html.card-peek {
    --peek: 12.5rem;
  }

  html.card-peek main {
    align-items: flex-start;
    padding-top: calc(100vh - var(--peek));
    padding-top: calc(100svh - var(--peek));
    padding-bottom: 1.3rem;
  }

  html.card-peek.card-away,
  html.card-peek.card-away body {
    overflow: hidden;
  }
}

@media only screen and (max-width: 370px) {
  .address {
    font-size: 1rem;
  }
}

@media only screen and (max-height: 500px) {
  html.card-peek {
    --peek: 6.5rem;
  }
}

/* ?card=flip, upright phones: the message on one side, the addresses on the other; the card is turned over */
.card-turn {
  display: none;
}

@media only screen and (max-width: 700px) {
  html.card-flip .postcard {
    grid-template-areas: "head" "body" "turn";
  }

  html.card-flip .message,
  html.card-flip .address {
    grid-area: body;
    align-self: start;
    margin-bottom: 0;
  }

  html.card-flip .address {
    border-top: 0;
    padding-top: 0.15em;
  }

  html.card-flip .postcard:not(.turned) .address,
  html.card-flip .postcard.turned .message {
    visibility: hidden;
  }

  html.js.card-flip .card-turn {
    grid-area: turn;
    justify-self: end;
    display: flex;
    align-items: center;
    gap: 0.35em;
    margin: 0.7rem -0.2rem -0.3rem 0;
    padding: 0.1rem 0.2rem;
    border: 0;
    background: none;
    color: var(--ink-soft);
    font: italic 0.98rem/1.2 "Crimson Text", Georgia, serif;
    cursor: pointer;
    transition: color 0.2s ease;
  }

  .card-turn svg {
    width: 1.25rem;
    height: 0.75rem;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.4;
    stroke-linecap: round;
    stroke-linejoin: round;
  }

  .card-turn:hover,
  .card-turn:focus-visible {
    color: var(--accent);
    outline: none;
  }

  .postcard.turned .turn-to-links,
  .postcard:not(.turned) .turn-to-message {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --theme-fade: 0s;
  }

  .js .postcard.in,
  .js .postcard.in.closed {
    transition: opacity 0.2s ease, visibility 0s;
    transform: rotate(var(--tilt));
  }

  .mini-card {
    animation: none;
  }

  .fan-item,
  .fan.out .fan-item {
    transition: opacity 0.15s ease, color 0.15s ease;
  }
}
