/* One sheet of paper holding the whole script. .paper--pad is the yellow legal pad, .paper--typed a Courier manuscript. */
.paper {
  --line: 2rem;
  position: relative;
  margin: 0 auto;
  color: var(--ink);
  box-shadow: 0 1px 0 rgb(0 0 0 / 0.2), 0 18px 40px rgb(0 0 0 / 0.45);
}

/* The pad is measured in --u, a rem at the standard width, which grows with the page width
   picked in the toolbar until the pad fills the desk. */
.paper--pad {
  --u: clamp(1rem, 100cqi / 52, 1rem * var(--paper-scale, 1));
  --line: calc(2 * var(--u));
  --margin: calc(8 * var(--u));
  --header: calc(7 * var(--u));
  max-width: calc(52 * var(--u));
  /* The extra 0.85 drops each line of writing from between two rules onto the lower one. */
  padding: calc(var(--header) + 0.85 * var(--u)) calc(2 * var(--u)) calc(var(--line) * 4) calc(var(--margin) + 1.25 * var(--u));
  border-top: calc(1.4 * var(--u)) solid var(--binding);
  /* A stack of sheets shows at the right and bottom edges. */
  box-shadow: 1px 1px 0 #e6dc97, 2px 2px 0 #c9c081, 3px 3px 0 #ece29c, 4px 4px 0 #c2b97b, 0 18px 40px rgb(0 0 0 / 0.45);
  background-color: var(--pad-yellow);
  background-image:
    linear-gradient(to right,
      transparent calc(var(--margin) - 4px), var(--pad-margin) calc(var(--margin) - 4px),
      var(--pad-margin) calc(var(--margin) - 2px), transparent calc(var(--margin) - 2px),
      transparent calc(var(--margin) + 1px), var(--pad-margin) calc(var(--margin) + 1px),
      var(--pad-margin) calc(var(--margin) + 3px), transparent calc(var(--margin) + 3px)),
    linear-gradient(var(--pad-yellow), var(--pad-yellow)),
    linear-gradient(to bottom, transparent calc(var(--line) - 1px), var(--pad-rule) calc(var(--line) - 1px));
  /* Rules and margin are one-line tiles repeated down the page: a single gradient as tall as a long script blurs or drops lines. */
  background-size: 100% var(--line), 100% calc(var(--header) - var(--line)), 100% var(--line);
  background-repeat: repeat-y, no-repeat, repeat-y;
  background-position: 0 0, 0 0, 0 calc(var(--header) + 0.35 * var(--u));
  font-family: var(--font-hand);
  font-size: calc(1.3 * var(--u));
  line-height: var(--line);
}

/* The gummed binding, lit from above, and the perforations just under it. */
.paper--pad::before {
  content: "";
  position: absolute;
  inset: calc(-1.4 * var(--u)) 0 auto;
  height: calc(1.4 * var(--u) + 5px);
  background:
    linear-gradient(to bottom, rgb(255 255 255 / 0.14), transparent 40%, rgb(0 0 0 / 0.22)) 0 0 / 100% calc(1.4 * var(--u)) no-repeat,
    radial-gradient(circle at center, rgb(70 50 0 / 0.28) 1.1px, transparent 1.5px) 0 100% / 8px 5px repeat-x;
  pointer-events: none;
}

@container (max-width: 40rem) {
  .paper--pad { --u: 1rem; --margin: 2.5rem; padding-right: 1rem; padding-left: calc(var(--margin) + 0.75rem); font-size: 1.15rem; }
}

.paper--typed {
  --line: 1em;
  width: 51em;
  max-width: 100%;
  padding: 6em 6em 6em 9em;
  background: var(--page);
  color: var(--page-ink);
  font-family: var(--font-typed);
  font-size: min(16px * var(--paper-scale, 1), 100cqi / 51);
  line-height: 1;
  /* Final Draft sets characters 7 points apart at 12pt, not Courier's 7.2. */
  letter-spacing: calc(7em / 12 - 0.6em);
}

.paper__header { margin-bottom: var(--line); text-align: center; }

.paper__title, .paper__byline { margin: 0; font: inherit; line-height: var(--line); }

.paper--pad .paper__header { position: absolute; inset: calc(1.5 * var(--u)) var(--u) auto calc(var(--margin) + 1.25 * var(--u)); }
.paper--pad .paper__title { font: 800 calc(1.8 * var(--u))/1.2 var(--font-ui); letter-spacing: -0.01em; color: var(--ink); }
.paper--pad .paper__byline { color: var(--pencil); }
.paper__watermark { margin: 0; color: var(--pencil); font-size: 0.85em; line-height: var(--line); text-align: center; }

.paper--typed .paper__header { padding: 18em 0 24em; border-bottom: 1px dashed var(--page-rule, #bbb); margin-bottom: 6em; }
.paper--typed .paper__title { text-transform: uppercase; text-decoration: underline; }
.paper--typed .paper__byline { margin-top: 2em; }

.scene { position: relative; }

.scene__heading {
  margin: calc(var(--line) * 2) 0 0;
  font: inherit;
  font-weight: 700;
  line-height: var(--line);
  text-transform: uppercase;
  outline: none;
}

.scene:first-of-type .scene__heading { margin-top: 0; }

/* Locked scene numbers. On the typed page they sit in both margins where Final Draft prints them,
   at 0.75" and ending at 7.375" from the left edge (the text starts at 1.5", 6em an inch). The pad's left margin
   holds the card's notes, so there the number goes at the right end of the heading. */
.scene__number {
  position: absolute;
  top: 0;
  line-height: var(--line);
  font-weight: 400;
  white-space: nowrap;
}
.scene__number:empty { display: none; }
.paper--typed .scene__number--left { left: -4.5em; }
.paper--typed .scene__number--right { right: 0.75em; }
.paper--pad .scene__number--left { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.paper--pad .scene__number--right { right: 0; color: var(--pencil); }
.paper--pad .scene:has(.scene__number:not(:empty)) .scene__heading { padding-right: 3.5em; }

.scene--omitted .scene__heading { font-weight: 400; }

/* Someone else is writing the scene: say who, just above its heading, and leave it read-only.
   The heading's top margin collapses through the scene, so the scene's top is the heading's. */
.scene__lock {
  position: absolute;
  top: 0;
  right: 0;
  margin: 0;
  transform: translateY(-115%);
  pointer-events: none;
}
.scene:not(.scene--locked) > .scene__lock { display: none; }
.scene__locker {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.1rem 0.6rem 0.1rem 0.15rem;
  border-radius: 999px;
  background: var(--avatar-ink, var(--accent));
  color: #fff;
  font: 600 0.75rem/1.4 var(--font-ui);
}
.scene__locker .avatar { box-shadow: 0 0 0 1.5px #fff; }
/* Clicked into while a partner writes there: the caret waits, and the scene opens when they leave it. */
.scene__lock-wait { display: none; }
.scene--waiting .scene__lock-wait { display: inline; }
.scene--locked :is(.scene__heading, .scene__body) { cursor: default; }
.scene--locked::before { content: ""; position: absolute; inset: 0 auto 0 -0.6rem; width: 0.2rem; border-radius: 0.1rem; background: var(--accent); }

/* Writing before the first scene heading, like FADE IN:, has no heading of its own. */
.scene:first-of-type:has(.element:not(:empty)) .scene__heading:empty:not(:focus) { min-height: 0; height: 0; }
.scene:first-of-type:has(.element:not(:empty)) .scene__heading:empty::before { content: none; }
.scene:first-of-type:has(.element:not(:empty)) .scene__heading:empty + .scene__body > .element:first-child { margin-top: 0; }

.scene__notes {
  position: absolute;
  top: 0;
  right: calc(100% + 1.75 * var(--u, 1rem));
  width: calc(var(--margin) - 2.5 * var(--u, 1rem));
  margin: 0;
  padding: 0;
  color: var(--pencil);
  font-size: calc(0.8 * var(--u, 1rem));
  line-height: 1.15;
  white-space: pre-line;
  overflow-wrap: break-word;
  transform: rotate(-1.5deg);
}

@container (max-width: 40rem) {
  .paper--pad .scene__notes { position: static; width: auto; margin-top: 0; transform: none; line-height: var(--line); font-size: 0.95rem; }
  /* No room above the first heading beside the byline: take one ruled line of its own. */
  .paper--pad .scene--locked:first-of-type .scene__lock { position: static; display: flex; justify-content: flex-end; align-items: center; height: var(--line); transform: none; }
}

.paper--typed .scene__notes { display: none; }

.scene__body { outline: none; }

[data-placeholder]:empty::before,
.scene__body > .element:only-child:empty::before {
  content: attr(data-placeholder);
  color: var(--pencil);
  opacity: 0.6;
  font-weight: 400;
}

.scene__body > .element:only-child:empty::before { content: "Start writing…"; }

.element {
  margin: var(--line) 0 0;
  min-height: var(--line);
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

/* A forced page break: on typed paper the page's dashed line shows it; on the pad, a small label. */
.paper--pad :is(.element, .scene__heading)[data-new-page] { position: relative; border-top: 1px dashed var(--pencil); }
.paper--pad :is(.element, .scene__heading)[data-new-page]::after {
  content: "new page";
  position: absolute;
  top: 0;
  right: 0;
  color: var(--pencil);
  font: 0.7rem/1.4 var(--font-ui);
  pointer-events: none;
}

/* Final Draft's default indents, as shares of the 6" action line, which holds 61 characters. */
.element[data-kind="character"] { margin-left: 33.3%; text-transform: uppercase; }
.element[data-kind="parenthetical"] { margin: 0 0 0 25%; max-width: 42.2%; text-indent: -1ch; }
.element[data-kind="dialogue"] { margin: 0 0 0 16.7%; max-width: 57.5%; }
.element[data-kind="transition"] { text-align: right; text-transform: uppercase; }
.paper--typed .element[data-kind="transition"] { margin-left: auto; margin-right: 6.7%; max-width: 26.7%; }
.element[data-kind="shot"] { text-transform: uppercase; }
.element:is([data-kind="new_act"], [data-kind="end_of_act"], [data-kind="sequence"]) { font-weight: 700; text-decoration: underline; text-transform: uppercase; }
.element:is([data-kind="new_act"], [data-kind="end_of_act"]) { text-align: center; }
/* Final Draft centers act breaks between 1" and 7.5", and sets sequences a quarter inch out from the action margin. */
.paper--typed .element:is([data-kind="new_act"], [data-kind="end_of_act"]) { margin-left: -8.3%; }
.paper--typed .element[data-kind="sequence"] { margin-left: -4.2%; }
/* A line or heading a Final Draft file set apart from its kind: its alignment shows; its indents and spacing print. */
:is(.element, .scene__heading)[data-align="center"] { text-align: center; }
:is(.element, .scene__heading)[data-align="right"] { text-align: right; }
.element[data-align="full"] { text-align: justify; }

/* A multi-camera sitcom, laid out as Final Draft's multi-camera template: each scene lettered SCENE A, B, C
   (or its locked number) centered above an underlined heading, who's in it listed under the heading, action
   and parentheticals in capitals, and what's said double-spaced. Other layouts leave the letter and list out. */
.scene__letter, .scene__cast { display: none; margin: 0; line-height: var(--line); }
.paper[data-layout="multi_camera"] {
  counter-reset: scene-letter;

  .scene:has(> .scene__heading:not(:empty)) { counter-increment: scene-letter; }
  .scene:has(> .scene__heading:not(:empty)) > .scene__letter {
    display: block;
    margin-top: calc(var(--line) * 2);
    text-align: center;
    text-decoration: underline;
  }
  .scene:first-of-type > .scene__letter { margin-top: 0; }
  .scene__letter::before { content: "SCENE " counter(scene-letter, upper-alpha); }
  .scene__letter[data-number]::before { content: "SCENE " attr(data-number); }
  .scene__letter + .scene__heading { margin-top: var(--line); }
  .scene__heading { text-decoration: underline; }
  /* Level with the heading, two lines under the letter. */
  .scene:has(> .scene__heading:not(:empty)) > .scene__number { top: calc(var(--line) * 2); }
  .scene__cast:not(:empty) { display: block; text-transform: uppercase; }

  .element:is([data-kind="action"], [data-kind="parenthetical"]) { text-transform: uppercase; }
  .element[data-kind="transition"] { text-decoration: underline; }
  /* Each line takes two, with the words on the first of them, as a page double-spaces them. */
  .element:is([data-kind="dialogue"], [data-kind="parenthetical"]) {
    line-height: calc(var(--line) * 2);
    margin-top: calc(var(--line) * -0.5);
    margin-bottom: calc(var(--line) * -0.5);
  }
  .element:is([data-kind="dialogue"], [data-kind="parenthetical"]) + .element:is([data-kind="dialogue"], [data-kind="parenthetical"]) {
    margin-top: calc(var(--line) * 0.5);
  }
}

/* A stage play, laid out as a play manuscript: scene headings centered and underlined, each name centered
   over dialogue that runs the full width, and stage directions, in action or parentheses, set 2" in. */
.paper[data-layout="stage_play"] {
  .scene__heading { font-weight: 400; text-align: center; text-decoration: underline; }
  .element:is([data-kind="action"], [data-kind="shot"]) { margin-left: 33.3%; }
  .element[data-kind="character"] { margin-left: 0; text-align: center; }
  .element[data-kind="parenthetical"] { margin-left: 33.3%; max-width: 66.7%; }
  .element[data-kind="dialogue"] { margin-left: 0; max-width: none; }
}

/* An audio drama, laid out as a radio script: each name and a colon at the margin with what's said beside it,
   cues and parentheticals in that same column 30% in (1.8" of the 6" line), and each speech and cue numbered
   in the left margin from 1 in each scene. A grid puts each name on the row of the line said after it. */
.paper[data-layout="audio_drama"] {
  .scene__heading { font-weight: 400; text-decoration: underline; }
  .scene { counter-reset: cue; }
  .scene__body { display: grid; grid-template-columns: 30% 70%; }
  .scene__body > .element { grid-column: 1 / -1; margin-left: 0; max-width: none; }
  .element[data-kind="character"] { grid-column: 1; padding-right: 1ch; }
  .element[data-kind="character"]:not(:empty, [data-suggestion])::after { content: ":"; }
  .element:is([data-kind="dialogue"], [data-kind="parenthetical"]) { grid-column: 2; }
  .element[data-kind="character"] + .element:is([data-kind="dialogue"], [data-kind="parenthetical"]) { margin-top: var(--line); }
  .element:is([data-kind="action"], [data-kind="shot"]) { padding-left: 30%; }
  .element:is([data-kind="character"], [data-kind="action"], [data-kind="shot"]):not(:empty) { counter-increment: cue; }
}
/* On the typed page the numbers sit in the left margin. They share ::before with the revision mark, so a revised
   line's spans the page from its number to the asterisk, justified out to either end. */
.paper--typed[data-layout="audio_drama"] .element:is([data-kind="character"], [data-kind="action"], [data-kind="shot"]):not(:empty) {
  &::before { content: counter(cue) "."; position: absolute; left: -3em; font-weight: 400; text-indent: 0; }
  &[data-revised]::before { content: counter(cue) ". *"; right: calc(-1 * var(--revision-mark)); text-align-last: justify; }
}

/* Read as typed, for a moment, by the editor (lib/typed_text.js). */
.paper .element.as-typed { text-transform: none !important; }

.page-break {
  position: absolute;
  left: 0;
  right: 0;
  height: 0;
  border-top: 1px dashed var(--page-rule, #c8c8c8);
  pointer-events: none;
}

.page-break::after {
  content: attr(data-page) ".";
  position: absolute;
  right: 6em;
  top: 0.5em;
  color: var(--page-number, #777);
}

.paper--pad .page-break { display: none; }
/* Locked pages number as the PDF breaks them (23A, 23-24), which the drawn breaks can't follow. */
.paper[data-pages-locked] .page-break::after { content: none; }

/* Revision marks: an asterisk in the right margin beside what changed under the current revision,
   level with the line's first row, where Final Draft prints it at 7.75". Placed against the scene,
   so dialogue's narrower column doesn't pull it in. */
:is(.element, .scene__heading)[data-revised]::before {
  content: "*";
  position: absolute;
  right: calc(-1 * var(--revision-mark, 1.2em));
  font-weight: 400;
  text-indent: 0;
}
.paper--typed { --revision-mark: 2.5em; }
.scene__body > .element[data-revised]:only-child:empty::before { content: "*"; }

:is(.element, .scene__heading)[data-suggestion]::after {
  content: attr(data-suggestion);
  opacity: 0.35;
}

/* The sheet's edge while it turns over: a block of pages, legal pad sheets on one side and typed
   pages on the other, standing at whichever side swings toward you. */
.paper-edge {
  position: fixed;
  z-index: 5;
  pointer-events: none;
  transform-style: preserve-3d;
}

.paper-edge__side {
  position: absolute;
  top: 0;
  bottom: 0;
  width: var(--thickness);
  backface-visibility: hidden;
  background:
    linear-gradient(to bottom, rgb(255 255 255 / 0.2), transparent 30%, rgb(0 0 0 / 0.15)),
    repeating-linear-gradient(to right, transparent 0 1px, rgb(0 0 0 / 0.14) 1px 2px),
    linear-gradient(to right, var(--pad-yellow) 50%, rgb(0 0 0 / 0.3) 50% calc(50% + 1px), var(--typed-paper) calc(50% + 1px));
}

.paper-edge__side--right { left: 100%; transform-origin: left; transform: rotateY(90deg); }
.paper-edge__side--left { right: 100%; transform-origin: right; transform: rotateY(-90deg); }

.paper-edge--board .paper-edge__side {
  background: linear-gradient(to bottom, #8a6038, #5a3b22 55%, #3f2814);
}

@media print {
  .toolbar, .page-break { display: none; }
  body { background: none; }
  .desk { padding: 0; }
  .paper { box-shadow: none; }
}
