:root {
  /* Neutrals lean towards the taffy pink so nothing reads as flat grey. */
  --paper: #f4f0f1;
  --sheet: #fffdfd;
  --ink: #291f26;
  --quiet: #6e6169;
  --rule: #e3dadf;
  --edge: #cdc0c7;

  /* Pink means the text gets shorter, mint means it gets longer. Pink is also the handle. */
  --taffy: #e5568b;
  --taffy-deep: #b83a6c;
  --taffy-soft: rgb(229 86 139 / 0.2);
  --taffy-ink: #a8285e;
  --mint: #2aa580;
  --mint-soft: rgb(42 165 128 / 0.22);
  --mint-ink: #157256;
  --on-taffy: #fffdfd;

  /* Accent colours used on the dark readout. */
  --taffy-on-ink: #ff9cc0;
  --mint-on-ink: #7fe6c5;

  /* Literata for everything you read; Anybody only for the wordmark, because its width axis
     lets the name really stretch. */
  --font-text: "Literata", ui-serif, Georgia, serif;
  --font-mark: "Anybody", ui-sans-serif, system-ui, sans-serif;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #1a1518;
    --sheet: #231c20;
    --ink: #f1e9ed;
    --quiet: #a9999f;
    --rule: #3b3036;
    --edge: #54464d;
    --taffy-ink: #f590b6;
    --mint-ink: #6fd8b5;
    --taffy-on-ink: #a8285e;
    --mint-on-ink: #157256;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  overflow-x: clip;
}

/* The page grows as the box does; keep it from jumping sideways when the scrollbar appears. */
html { scrollbar-gutter: stable; }

body {
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
  font: 400 15px/1.5 var(--font-text);
  -webkit-font-smoothing: antialiased;
}

/* Header */

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 24px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 32px clamp(20px, 5vw, 64px) 0;
}

/* wordmark.js draws the letters as an SVG on top of this text and pulls them about like taffy.
   The width is a keyword value (semi-expanded) because that's all a canvas can measure with. */
.wordmark {
  position: relative;
  margin: 0;
  font-family: var(--font-mark);
  font-size: 44px;
  font-stretch: 112.5%;
  font-weight: 820;
  line-height: 1;
  letter-spacing: -0.02em;
  transition: color 0.35s;
}

.wordmark[data-tone="up"] { color: var(--mint); }
.wordmark[data-tone="down"] { color: var(--taffy); }

.wordmark svg {
  position: absolute;
  top: 0;
  left: 0;
  overflow: visible;
  color: var(--ink);
  fill: currentColor;
  letter-spacing: 0;
  pointer-events: none;
  transition: color 0.35s;
}

.wordmark[data-tone="up"] svg { color: var(--mint); }
.wordmark[data-tone="down"] svg { color: var(--taffy); }

/* Once the SVG is up, the real text only holds the space (and stays selectable). */
.wordmark.is-drawn { color: transparent; }

.wordmark-probe {
  display: inline-block;
  width: 0;
  height: 0;
}

/* Only shown when Taffy can't reach Claude. */
.engine {
  margin: 0;
  color: var(--taffy-ink);
  font-size: 13px;
}

/* Stage */

.stage {
  --accent: var(--ink);
  --accent-soft: transparent;
  --accent-on-ink: var(--paper);
  max-width: 1180px;
  margin: 0 auto;
  padding: 16px clamp(20px, 5vw, 64px) 96px;
}

.stage[data-dir="up"] {
  --accent: var(--mint);
  --accent-soft: var(--mint-soft);
  --accent-on-ink: var(--mint-on-ink);
}

.stage[data-dir="down"] {
  --accent: var(--taffy);
  --accent-soft: var(--taffy-soft);
  --accent-on-ink: var(--taffy-on-ink);
}

.lede {
  max-width: 34em;
  margin: 0 0 40px;
  color: var(--quiet);
  font-size: 16px;
}

.desk {
  position: relative;
  width: 100%;
}

/* The box: a flat sheet, not a floating card. */

/* app.js springs the height into place after a rewrite, and wobbles the box from its top left
   corner (a scale transform, so the text doesn't rewrap) when you let go of it. */
.sheet {
  position: relative;
  width: 640px;
  height: 200px;
  border: 1px solid var(--edge);
  border-radius: 6px;
  background: var(--sheet);
  transform-origin: 0 0;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.sheet:focus-within { border-color: var(--quiet); }

.sheet.is-dragging {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Taffy stripes run round the border while the text is being rewritten. */
.sheet::before {
  content: "";
  position: absolute;
  inset: -1px;
  padding: 3px;
  border-radius: 6px;
  background: repeating-linear-gradient(-45deg, var(--accent) 0 7px, transparent 7px 14px);
  background-size: 19.8px 19.8px;
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  opacity: 0;
  pointer-events: none;
  animation: candy 0.9s linear infinite paused;
}

/* Fades in, but goes off at once: by then the accent has already switched back to ink. */
.sheet.is-busy::before {
  opacity: 1;
  transition: opacity 0.25s;
  animation-play-state: running;
}

@keyframes candy {
  to { background-position: 19.8px 0; }
}

/* Text */

.prose {
  padding: 28px 32px;
  color: var(--ink);
  font-family: var(--font-text);
  font-size: 18px;
  font-weight: 400;
  font-optical-sizing: auto;
  line-height: 1.65;
  letter-spacing: 0;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  tab-size: 4;
}

textarea.prose {
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  outline: none;
  resize: none;
  overflow: auto;
  transition: opacity 0.25s ease;
}

textarea.prose::placeholder {
  color: var(--quiet);
  font-style: italic;
}

.sheet:is(.is-dragging, .is-settling) textarea { overflow: hidden; }
.sheet.is-waiting textarea { opacity: 0.28; }
.sheet.is-streaming textarea { opacity: 0; transition-duration: 0.1s; }

.stream {
  position: absolute;
  inset: 0;
  margin: 0;
  /* The box grows to fit, so a scrollbar would only flash up while it catches up. */
  overflow: hidden;
  border-radius: 5px;
  pointer-events: none;
}

.stream .chunk { animation: ink-in 0.4s ease-out both; }
textarea.swap { animation: ink-in 0.3s ease-out; }

@keyframes ink-in {
  from { opacity: 0; }
}

.mirror {
  position: absolute;
  top: 0;
  left: -10000px;
  height: auto;
  visibility: hidden;
}

/* Resize handles */

.pull {
  position: absolute;
  right: -15px;
  bottom: -15px;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--taffy);
  color: var(--on-taffy);
  box-shadow: 0 2px 0 var(--taffy-deep);
  cursor: nwse-resize;
  touch-action: none;
  transition: background-color 0.15s;
}

.pull svg { width: 14px; height: 14px; }
.pull:hover,
.sheet.is-dragging .pull { background: var(--taffy-deep); }

.pull:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 3px;
}

.edge {
  position: absolute;
  z-index: 1;
  touch-action: none;
}

.edge-x { top: 20px; right: -7px; bottom: 20px; width: 14px; cursor: ew-resize; }
.edge-y { right: 20px; bottom: -7px; left: 20px; height: 14px; cursor: ns-resize; }

.edge::after {
  content: "";
  position: absolute;
  border-radius: 2px;
  background: var(--quiet);
  opacity: 0;
  transition: opacity 0.15s;
}

.edge-x::after { top: 50%; left: 6px; width: 3px; height: 40px; margin-top: -20px; }
.edge-y::after { top: 6px; left: 50%; width: 40px; height: 3px; margin-left: -20px; }
.edge:hover::after { opacity: 1; }

/* Live length readout next to the handle while dragging */

.readout {
  position: absolute;
  bottom: -13px;
  left: calc(100% + 28px);
  z-index: 3;
  display: none;
  align-items: baseline;
  gap: 10px;
  padding: 6px 10px;
  border-radius: 4px;
  background: var(--ink);
  color: var(--paper);
  font: 500 13px/1.2 var(--font-text);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}

/* No room on the right: tuck it under the handle instead. */
.readout.is-below {
  top: calc(100% + 24px);
  right: -14px;
  bottom: auto;
  left: auto;
}

/* Only in the layout while dragging, so it never widens the page. */
.sheet.is-dragging .readout.is-live {
  display: flex;
  @starting-style {
    opacity: 0;
    transform: translateY(-4px);
  }
}

.readout-delta { color: var(--accent-on-ink); font-weight: 600; }
.stage:not([data-dir="up"]):not([data-dir="down"]) .readout-delta { opacity: 0.7; font-weight: 500; }

/* Status and actions */

.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 24px;
  margin-top: 28px;
}

.status {
  margin: 0;
  color: var(--quiet);
  font-variant-numeric: tabular-nums;
}

.status:empty { display: none; }
.status[data-tone="busy"] { color: var(--ink); }
.status[data-tone="error"] { max-width: 40em; color: var(--taffy-ink); }

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0 20px;
}

/* Plain text actions, underlined like links: the box is the only thing that looks like an object. */
.actions button {
  padding: 8px 0;
  border: 0;
  background: none;
  color: var(--ink);
  font: 500 15px/1.2 var(--font-text);
  text-decoration: underline 1px var(--edge);
  text-underline-offset: 5px;
  white-space: nowrap;
  cursor: pointer;
  transition: text-decoration-color 0.15s;
}

.actions button:hover { text-decoration-color: currentColor; }
.actions button:active { color: var(--quiet); }
.actions button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; border-radius: 2px; }
.actions #stop { color: var(--taffy-ink); }

/* Length: the target word count on a log scale, with every version so far marked on the rail.
   The rail and its marks are drawn under a native range input that only shows its thumb. */

.length {
  --thumb: 22px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 8.5em;
  align-items: center;
  gap: 0 18px;
  max-width: 640px;
  margin: 0 0 20px;
  transition: opacity 0.25s;
}

.length.is-off { opacity: 0.4; }

.length-caption {
  color: var(--quiet);
  font-size: 13px;
}

.length-track {
  position: relative;
  height: 60px;
}

.length-rail {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.length-rail::before {
  content: "";
  position: absolute;
  top: 29px;
  right: calc(var(--thumb) / 2);
  left: calc(var(--thumb) / 2);
  height: 2px;
  border-radius: 1px;
  background: var(--edge);
}

/* --p is the position along the rail, 0 to 1, matching where the thumb's centre can go. */
.length-mark,
.length-dot {
  position: absolute;
  left: calc(var(--thumb) / 2 + (100% - var(--thumb)) * var(--p));
}

.length-mark {
  top: 43px;
  color: var(--quiet);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  transform: translateX(-50%);
}

.length-mark::before {
  content: "";
  position: absolute;
  top: -8px;
  left: 50%;
  width: 1px;
  height: 5px;
  background: var(--edge);
}

.length-dot {
  top: 30px;
  width: 10px;
  height: 10px;
  margin: -5px 0 0 -5px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--paper);
}

.length-dot.is-source::after {
  content: "original";
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  color: var(--quiet);
  font: 500 11px/1 var(--font-text);
  white-space: nowrap;
  transform: translateX(-50%);
}

.length input {
  position: absolute;
  top: 19px;
  left: 0;
  width: 100%;
  height: var(--thumb);
  margin: 0;
  background: transparent;
  appearance: none;
  cursor: pointer;
}

.length input:disabled { cursor: default; }

.length input::-webkit-slider-runnable-track { height: var(--thumb); background: transparent; }
.length input::-moz-range-track { height: var(--thumb); background: transparent; }

/* The thumb is the same pink as the box's handle: both are things you pull. */
.length input::-webkit-slider-thumb {
  width: var(--thumb);
  height: var(--thumb);
  border: 0;
  border-radius: 50%;
  background: var(--taffy);
  box-shadow: 0 2px 0 var(--taffy-deep);
  appearance: none;
  transition: background-color 0.15s;
}

.length input::-moz-range-thumb {
  width: var(--thumb);
  height: var(--thumb);
  border: 0;
  border-radius: 50%;
  background: var(--taffy);
  box-shadow: 0 2px 0 var(--taffy-deep);
}

.length input:not(:disabled):hover::-webkit-slider-thumb,
.length input:active::-webkit-slider-thumb { background: var(--taffy-deep); }
.length input:disabled::-webkit-slider-thumb { background: var(--edge); box-shadow: none; }
.length input:disabled::-moz-range-thumb { background: var(--edge); box-shadow: none; }

.length input:focus { outline: none; }
.length input:focus-visible::-webkit-slider-thumb { outline: 2px solid var(--ink); outline-offset: 3px; }
.length input:focus-visible::-moz-range-thumb { outline: 2px solid var(--ink); outline-offset: 3px; }

.length-value {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
  transition: color 0.2s;
}

.stage[data-dir="up"] .length-value { color: var(--mint-ink); }
.stage[data-dir="down"] .length-value { color: var(--taffy-ink); }

@media (max-width: 640px) {
  .wordmark { font-size: 36px; }
  .engine { font-size: 12px; }
  .lede { margin-bottom: 28px; font-size: 15px; }
  .prose { padding: 20px 24px; font-size: 17px; }
  .length { grid-template-columns: minmax(0, 1fr) auto; }
  .length-caption { grid-column: 1 / -1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
