/* ==========================================================================
   CONTACT — a letter on a manual typewriter
   The machine is a photograph (assets/images/typewriter, made by build-layers.py), cut
   into a body that stays still and a carriage that travels like a real one: it steps a
   letter-width per keystroke and glides back on a return, carrying the live sheet, platen,
   bail, knobs and lever. The knobs are painted live from the photo's ridges so they roll.
   Every part sits in the render's own pixels (1536 × 1392) times --s; scripts/contact.js
   sets --s, --mx, --my (fit) and --cx (carriage travel). The line being typed sits just
   above the ribbon; the sheet feeds one ruled line at a time.

   Light mode: a charcoal machine on the warm paper background.
   Dark mode: an ivory machine on deep charcoal. The sheet stays warm ivory with dark
   ink in both, like a real page under different light.
   ========================================================================== */

.letter-page { position: relative; }
/* the site grain doubles in dark mode; here it would grey the ivory sheet, so it stays light */
:root:has(.tw) { --grain-opacity: 0.25; }

.tw {
  /* the line grid and type (independent of the photo's scale) */
  --L: 32px;
  --fs: 16px;
  --margin-x: 72px;
  /* the photo's placement (set by contact.js) */
  --s: 0.7;
  --mx: 0px;
  --my: 0px;
  --cx: 0px;               /* the carriage's travel (contact.js) */
  --scene-h: var(--vvh, max(600px, 100svh));
  --feed-ms: 240ms;
  --feed-ease: cubic-bezier(0.3, 0.75, 0.2, 1); /* a ratchet: quick start, firm stop */

  /* the sheet — same in both themes, matched to the photo's paper */
  --sheet: #efe6da;
  --ink: #1d1b18;
  --ink-soft: rgb(29 27 24 / 0.42);
  --rule: rgb(29 27 24 / 0.2);
  --ink-red: #9b2f22;

  /* the machine: charcoal in light mode */
  --tw-carriage: url("/assets/images/typewriter/carriage-charcoal.webp");
  --tw-bail: url("/assets/images/typewriter/bail-charcoal.webp");
  --tw-body: url("/assets/images/typewriter/body-charcoal.webp");
  --m-plate-text: #d8d2c3;
  --m-plate-line: rgb(216 210 195 / 0.35);

  position: relative;
  height: var(--scene-h);
  overflow: hidden;
  overflow: clip;          /* not a scroll container: focusing a field can't shift the scene */
  isolation: isolate;
}
/* Dark mode: the ivory machine */
:root[data-theme="dark"] .tw {
  --tw-carriage: url("/assets/images/typewriter/carriage-ivory.webp");
  --tw-bail: url("/assets/images/typewriter/bail-ivory.webp");
  --tw-body: url("/assets/images/typewriter/body-ivory.webp");
  --m-plate-text: #4a463e;
  --m-plate-line: rgb(74 70 62 / 0.35);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tw {
    --tw-carriage: url("/assets/images/typewriter/carriage-ivory.webp");
    --tw-bail: url("/assets/images/typewriter/bail-ivory.webp");
    --tw-body: url("/assets/images/typewriter/body-ivory.webp");
    --m-plate-text: #4a463e;
    --m-plate-line: rgb(74 70 62 / 0.35);
  }
}
/* Phones: smaller type; contact.js scales the machine so the whole sheet and the right
   knob stay in view (the left knob and lever go off the edge first) */
@media (max-width: 40rem) {
  .tw { --L: 24px; --fs: 13px; --margin-x: 18px; }
}

/* Photo coordinates → page: x = --mx + px × --s, y = --my + px × --s.
   Back to front: rail, carriage (photo, knobs, lever, sheet, bail), body, pressed keys. */
.tw__rail,
.tw__carriage-img,
.tw__body,
.tw__keys,
.tw__bail,
.tw__knob,
.tw__lever { position: absolute; pointer-events: none; }

/* the carriage rail along the back, seen where the carriage has travelled away */
.tw__rail {
  z-index: 1;
  left: calc(var(--mx) + 150px * var(--s));
  width: calc(1236px * var(--s));
  top: calc(var(--my) + 612px * var(--s));
  height: calc(46px * var(--s));
  border-radius: calc(10px * var(--s));
  background: linear-gradient(to bottom, #6d6b66 0, #2a2826 8%, #141312 45%, #0b0a09 100%);
}

/* The carriage travels as one: one letter-width per keystroke, back on a return */
.tw__carriage {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  transform: translate3d(var(--cx), 0, 0);
  transition: transform var(--carriage-ms, 90ms) var(--carriage-ease, cubic-bezier(0.34, 1.5, 0.64, 1));
  will-change: transform;
}
.tw__carriage-img {
  z-index: 1;
  left: var(--mx);
  top: calc(var(--my) + 395px * var(--s));
  width: calc(1536px * var(--s));
  height: calc(285px * var(--s));
  background: var(--tw-carriage) 0 0 / 100% 100% no-repeat;
}
/* knobs: painted live (contact.js) from the photo's own ridges, so they roll */
.tw__knob { z-index: 3; top: calc(var(--my) + 536px * var(--s)); height: calc(140px * var(--s)); }
.tw__knob[data-knob="l"] { left: calc(var(--mx) + 98px * var(--s)); width: calc(78px * var(--s)); }
.tw__knob[data-knob="r"] { left: calc(var(--mx) + 1392px * var(--s)); width: calc(76px * var(--s)); }
/* the bail sits in front of the sheet */
.tw__bail {
  z-index: 4;
  left: calc(var(--mx) + 239px * var(--s));
  top: calc(var(--my) + 550px * var(--s));
  width: calc(1076px * var(--s));
  height: calc(65px * var(--s));
  background: var(--tw-bail) 0 0 / 100% 100% no-repeat;
}
/* Return lever: its own layer, so it can flick about its pivot */
.tw__lever {
  z-index: 4;
  left: calc(var(--mx) + 20px * var(--s));
  top: calc(var(--my) + 398px * var(--s));
  width: calc(208px * var(--s));
  height: calc(218px * var(--s));
  max-width: none;
  transform-origin: 88.9% 19.3%;
}

/* The body: type basket, guide, ribbon, keyboard. Stays still, in front of the carriage. */
.tw__body {
  z-index: 3;
  left: var(--mx);
  top: calc(var(--my) + 590px * var(--s));
  width: calc(1536px * var(--s));
  height: calc(802px * var(--s));
  background: var(--tw-body) 0 0 / 100% 100% no-repeat;
}
.tw__keys {
  z-index: 5;
  left: var(--mx);
  top: var(--my);
  width: calc(1536px * var(--s));
  height: calc(1392px * var(--s));
}
/* Keys: a button over each photographed cap. Pressed, the cap (cut from the same photo)
   sinks into a dark well and springs back. */
.tw__key {
  position: absolute;
  padding: 0;
  border: 0;
  border-radius: 22%;
  background: #141210;
  overflow: hidden;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.tw__key > span {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background-image: var(--tw-body);
  background-repeat: no-repeat;
  transition: transform 160ms cubic-bezier(0.34, 1.6, 0.64, 1), filter 160ms ease-out;
}
.tw__key:hover > span { filter: brightness(1.04); }
.tw__key.is-down > span {
  transform: translateY(var(--key-drop, 4px)) scale(0.97);
  filter: brightness(0.86);
  transition-duration: 50ms;
  transition-timing-function: ease-out;
}
/* A typebar swinging up from the basket to the print point */
.tw__typebar {
  position: absolute;
  left: 0;
  top: 0;
  width: max(3px, calc(9px * var(--s)));
  border-radius: 2px;
  background: linear-gradient(to right, #24221f, #e4dfd3 40%, #8d877c 70%, #24221f);
  transform-origin: 50% 100%;
  pointer-events: none;
}
.tw__typebar::before { /* the type slug at its tip */
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  width: calc(18px * var(--s));
  height: calc(14px * var(--s));
  translate: -50% -40%;
  border-radius: 2px;
  background: linear-gradient(#cfc9bc, #6a655d);
}

/* ---------- The sheet ---------- */
.tw__window {
  position: absolute;
  z-index: 2;
  pointer-events: auto;
  top: 0;
  left: calc(var(--mx) + 247px * var(--s));
  width: calc(1060px * var(--s));
  height: calc(var(--my) + 654px * var(--s)); /* below this the sheet is round the platen */
  overflow: hidden;
  overflow: clip;
}
.tw__paper {
  position: absolute;
  inset: 0 0 auto;
  min-height: calc(1060px * var(--s) * 1.3);
  padding: calc(var(--L) * 3) var(--margin-x) calc(var(--L) * 4);
  background: var(--sheet);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: var(--fs);
  line-height: var(--L);
  font-weight: 400;
  will-change: transform;
  transition: transform var(--feed-ms) var(--feed-ease);
}
.tw__paper.is-out { pointer-events: none; }

.letter .row {
  display: block;
  height: var(--L);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.letter .row--gap { margin-top: var(--L); }
.letter .row-block { position: relative; display: block; margin: 0; }

/* Fields type straight onto the sheet; the ruled line sits on the text's baseline (--base) */
.field {
  display: block;
  width: 100%;
  height: var(--L);
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  outline: none;
  background: transparent;
  color: var(--ink);
  caret-color: var(--ink);
  font: inherit;
  line-height: var(--L);
  letter-spacing: 0;
  -webkit-appearance: none;
  appearance: none;
}
.field::placeholder { color: var(--ink-soft); opacity: 1; }
.field::selection, .tw__paper ::selection { background: rgb(29 27 24 / 0.16); color: var(--ink); }
/* name and email: a short ruled line each */
.field:not(.field--body) {
  width: min(100%, 30ch);
  background: linear-gradient(var(--rule), var(--rule)) 0 calc(var(--base, 23px) + 2px) / 100% 1px no-repeat;
}
/* the writing lines: six or more, one continuous field */
.field--body {
  height: calc(var(--L) * 6);
  resize: none;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0 calc(var(--base, 23px) + 2px),
    var(--rule) calc(var(--base, 23px) + 2px) calc(var(--base, 23px) + 3px),
    transparent calc(var(--base, 23px) + 3px) var(--L)
  );
}
/* the signature line runs a little longer and darker */
.field--sign:not(.x) {
  width: min(100%, 34ch);
  background-image: linear-gradient(rgb(29 27 24 / 0.5), rgb(29 27 24 / 0.5));
}
.field.is-invalid:not(.x) {
  background-image: linear-gradient(var(--ink-red), var(--ink-red));
}
.field--body.is-invalid {
  background: repeating-linear-gradient(
    to bottom,
    transparent 0 calc(var(--base, 23px) + 2px),
    rgb(155 47 34 / 0.55) calc(var(--base, 23px) + 2px) calc(var(--base, 23px) + 3px),
    transparent calc(var(--base, 23px) + 3px) var(--L)
  );
}

/* Hidden copy of the letter, used to find which line the caret is on and where each
   typed character landed. Same width, font and wrapping as the textarea. */
.tw__mirror {
  position: absolute;
  inset: 0 auto auto 0;
  visibility: hidden;
  pointer-events: none;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  line-height: var(--L);
}
.tw__probe {
  position: absolute;
  top: 0;
  left: 0;
  line-height: var(--L);
  visibility: hidden;
}
.tw__probe > span { display: inline-block; width: 0; height: 0; vertical-align: baseline; }

/* Ink strike: a heavier copy of the new character that lands and fades into it */
.tw__strike {
  position: absolute;
  height: var(--L);
  line-height: var(--L);
  white-space: pre;
  color: var(--ink);
  pointer-events: none;
  text-shadow: 0 0 0.6px var(--ink), 0.4px 0 0 var(--ink);
}

/* Send */
.row--send { overflow: visible; display: flex !important; align-items: center; }
.tw__send {
  height: calc(var(--L) - 6px);
  padding: 0 14px;
  border: 1px solid var(--ink);
  border-radius: 4px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  line-height: 1;
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--ease-out), color var(--duration-fast) var(--ease-out);
}
.tw__send:hover, .tw__send:focus-visible { background: var(--ink); color: var(--sheet); outline: none; }
.tw__send:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.tw__send:disabled { cursor: progress; opacity: 0.6; }
.tw__status { min-height: var(--L); color: var(--ink-soft); }
.tw[data-state="error"] .tw__status { color: var(--ink-red); }
.tw__trap { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }

/* After sending */
.tw__sent {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(var(--L) * 2.5);
  padding-inline: var(--margin-x);
  display: grid;
  justify-items: start;
  gap: var(--space-8);
  font-family: var(--font-mono);
  font-size: var(--fs);
  line-height: 1.6;
  color: var(--color-text);
}
.tw__sent[hidden] { display: none; }

/* ---------- Done: the finished sheet, out of the machine, to read over ----------
   contact.js places the page (translate + scale, so it is the sheet at its own size) and
   the bar under it. Clicking the page sends it back into the machine. */
.tw__review {
  position: absolute;
  inset: 0;
  z-index: 30;
  background: color-mix(in srgb, var(--color-bg) 78%, transparent);
}
.tw__review[hidden] { display: none; }
.tw__page {
  position: absolute;
  left: 0;
  top: 0;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
  transform-origin: 0 0;
  cursor: pointer;
  will-change: transform;
}
.tw__page-sheet {
  display: block;
  min-height: calc(1060px * var(--s) * 1.3);
  padding: calc(var(--L) * 3) var(--margin-x) calc(var(--L) * 4);
  background: var(--sheet);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: var(--fs);
  line-height: var(--L);
  text-shadow: 0 0 0.6px var(--ink), 0.4px 0 0 var(--ink);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.08), 0 18px 40px -18px rgb(0 0 0 / 0.35);
  transition: box-shadow var(--duration-fast) var(--ease-out);
}
@media (hover: hover) and (pointer: fine) {
  .tw__page:hover .tw__page-sheet { box-shadow: 0 1px 2px rgb(0 0 0 / 0.1), 0 24px 48px -18px rgb(0 0 0 / 0.42); }
}
.tw__page:focus-visible { outline: none; }
.tw__page:focus-visible .tw__page-sheet { outline: 2px solid var(--color-text); outline-offset: 4px; }
.tw__page-body { white-space: pre-wrap; overflow-wrap: anywhere; min-height: calc(var(--L) * 6); }
.tw__review-bar {
  position: absolute;
  left: 0;
  right: 0;
  display: grid;
  justify-items: center;
  gap: var(--space-8);
  text-align: center;
}
.tw__review-hint { color: var(--color-text-muted); margin: 0; }
.tw__review-status { color: var(--color-text); margin: 0; min-height: 1lh; }
.tw[data-state="error"] .tw__review-status { color: #c2412d; }
.tw__sent-title { font-size: calc(var(--fs) * 1.25); color: var(--color-text-strong); }
.tw__sent-text { color: var(--color-text-muted); max-width: 46ch; margin-bottom: var(--space-8); }
.tw__sent .tw__send { border-color: var(--color-text); color: var(--color-text); height: 36px; }
.tw__sent .tw__send:hover, .tw__sent .tw__send:focus-visible { background: var(--color-text); color: var(--color-bg); }

/* ---------- In front of the sheet ---------- */
/* the sheet bending away round the platen: the photo's own shading, as a multiply */
.tw__curve {
  position: absolute;
  z-index: 2;
  left: calc(var(--mx) + 247px * var(--s));
  width: calc(1060px * var(--s));
  top: calc(var(--my) + 594px * var(--s));
  height: calc(60px * var(--s));
  pointer-events: none;
  mix-blend-mode: multiply;
  background: linear-gradient(to bottom,
    rgb(219 219 219) 0%, rgb(243 243 243) 13%, rgb(240 240 240) 27%,
    rgb(225 225 225) 47%, rgb(196 196 196) 67%, rgb(162 162 162) 80%,
    rgb(140 140 140) 87%, rgb(110 110 110) 100%);
}
/* the nameplate on the body, under the type basket: moves between letter and signature */
.tw__plate {
  position: absolute;
  z-index: 6;
  left: calc(var(--mx) + 768px * var(--s));
  top: calc(var(--my) + 859px * var(--s));
  translate: -50% -50%;
}
.tw__jump {
  display: block;
  padding: 2px 8px;
  border: 1px solid var(--m-plate-line);
  border-radius: 5px;
  background: transparent;
  color: var(--m-plate-text);
  font-family: var(--font-mono);
  font-size: 10px;
  line-height: 14px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
  cursor: pointer;
  transition: border-color var(--duration-fast) var(--ease-out);
}
.tw__jump:hover { border-color: var(--m-plate-text); }
.tw__jump:focus-visible { outline: 2px solid var(--m-plate-text); outline-offset: 2px; }

/* Reduced motion: the sheet goes straight to the next line */
@media (prefers-reduced-motion: reduce) {
  .tw__paper, .tw__carriage { transition: none; }
}

/* ==========================================================================
   MODE SWITCH — Template / My letter, under the header
   ========================================================================== */
.letter-mode {
  position: absolute;
  z-index: 20;
  top: calc(var(--header-h) - var(--space-12));
  left: 50%;
  translate: -50% 0;
  flex-wrap: nowrap;
  gap: 2px;
  padding: 3px;
  border-radius: calc(var(--radius-md) + 3px);
  background: var(--color-bg);   /* reads over the sheet in either theme */
}
.letter-mode .filter { cursor: pointer; }
[data-mode-panel][hidden] { display: none !important; }

/* ==========================================================================
   TEMPLATE MODE — Figma: Contact (33:1099), the fill-in-the-blank message
   (scripts/contact-template.js)
   ========================================================================== */
.contact {
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding-top: calc(var(--header-h) + var(--space-10)); /* fixed header + Figma's 10px gap */
  padding-bottom: var(--section-y);
}

.contact__form {
  width: min(100%, 29.375rem);       /* 470 */
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-40);
}

.contact__message {
  font-size: var(--text-lg);         /* 24 at 1440 */
  line-height: var(--leading-lg);    /* same as the About lead */
  font-weight: var(--weight-regular);
  color: var(--color-text);          /* #333 */
}

/* Blanks: type straight into the sentence; the bracket text is the placeholder */
.blank {
  color: var(--color-text);
  outline: none;
  border-radius: 4px;
  caret-color: var(--color-text);
  overflow-wrap: anywhere;
  cursor: text;
  /* Typing indicator: a soft highlight wraps the blank (every line of it) — faint on
     hover, firmer once it has focus — so it's clear where the words will go. */
  padding-inline: 2px;
  margin-inline: -2px;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
  background-color: transparent;
  transition: background-color var(--duration-fast) var(--ease-out);
}
/* tints of the text colour, so it reads on the light and the dark paper alike */
.blank:hover { background-color: color-mix(in srgb, var(--color-text) 5%, transparent); }
.blank:focus { background-color: color-mix(in srgb, var(--color-text) 10%, transparent); }
.blank:empty::before {
  content: attr(data-placeholder);
  color: var(--color-text-muted);    /* #767676 */
  transition: opacity var(--duration-fast) var(--ease-out);
}
/* Focused and still empty: the prompt steps back so the caret reads as "type here" */
.blank:focus:empty::before { opacity: 0.55; }
/* Only a missing/invalid field is underlined; typing shows no decoration */
.blank.is-invalid {
  text-decoration: underline 1px var(--color-text-muted);
  text-underline-offset: 4px;
}

/* Sending — button label swaps; a short status line sits under it */
.contact__actions {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-16);
}
.contact__status {
  min-height: var(--leading-sm);
  color: var(--color-text-muted);
}
.contact__status:empty { min-height: 0; }
.contact__form[data-state="error"] .contact__status { color: var(--color-text); }
.contact__form[data-state="sending"] .btn { cursor: progress; }
.contact__form[data-state="sending"] .btn .icon--arrow { opacity: 0.4; }
.contact__form[data-state="sent"] .btn { cursor: default; }
.contact__form .btn:disabled:active { transform: none; }

/* spam trap: off-screen, not focusable by people */
.contact__trap {
  position: absolute;
  left: -9999px;
  width: 1px;
  height: 1px;
  opacity: 0;
}
