@layer tokens, base, layout, components, pages;

@layer components {
  /*
   * Sheets and prose: a document shown as a document. The sheet is the white
   * page; prose is the typography of rendered Markdown inside it. The base
   * layer strips margins and list markers for the interface, so everything a
   * document needs is put back here, and only inside .prose.
   */
  .sheet {
    max-height: 42rem;
    padding: var(--space-5) clamp(var(--space-4), 3vw, var(--space-6));
    overflow: auto;
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
  }

  .well--file {
    max-height: 42rem;
  }

  /* On the full-screen page the window scrolls, not the box. Keep this after
     the heights it lifts. */
  .sheet--open,
  .well--open {
    max-height: none;
  }

  /*
   * Documents: a workbook as a grid of cells, one sheet at a time, and a PDF
   * in the browser's own viewer. Each is a box of the height a sheet has, and
   * fills the window on the full-screen page.
   */
  .workbook {
    display: grid;
    gap: var(--space-3);
    min-width: 0;
  }

  .grid-scroll {
    max-height: 42rem;
    overflow: auto;
  }

  .grid-scroll--open {
    max-height: calc(100vh - 12rem);
  }

  .grid {
    width: max-content;
    min-width: 100%;
  }

  .grid th,
  .grid td {
    max-width: 26rem;
    padding: 0.4rem 0.75rem;
    border-right: 1px solid var(--rule-soft);
    vertical-align: top;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
  }

  .grid :is(th, td):last-child {
    border-right: 0;
  }

  /* The headings stay in view as the rows scroll under them. */
  .grid thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    box-shadow: 0 1px 0 var(--rule);
    white-space: nowrap;
  }

  .grid tbody .numeric {
    font-weight: 400;
  }

  .grid__formula {
    color: var(--ink-soft);
    font-family: var(--font-data);
    font-size: 0.9375em;
  }

  .pdf-frame {
    display: block;
    width: 100%;
    height: 42rem;
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    background: var(--well);
    box-shadow: var(--shadow);
  }

  .pdf-frame--open {
    height: calc(100vh - 9rem);
    min-height: 24rem;
  }

  .prose {
    font-size: 1rem;
    line-height: 1.65;
    overflow-wrap: anywhere;
  }

  .prose > * + * {
    margin-top: var(--space-4);
  }

  .prose h1,
  .prose h2,
  .prose h3,
  .prose h4,
  .prose h5,
  .prose h6 {
    margin: 0;
    font-stretch: 100%;
    letter-spacing: -0.01em;
    line-height: 1.25;
  }

  .prose > * + :is(h1, h2, h3, h4, h5, h6) {
    margin-top: var(--space-6);
  }

  .prose h1 {
    font-size: 1.75rem;
    font-weight: 720;
  }

  .prose h2 {
    padding-bottom: var(--space-2);
    border-bottom: 1px solid var(--rule-soft);
    font-size: 1.3125rem;
    font-weight: 680;
  }

  .prose h3 {
    font-size: 1.0625rem;
    font-weight: 680;
  }

  .prose h4,
  .prose h5,
  .prose h6 {
    font-size: 1rem;
    font-weight: 650;
  }

  .prose h5,
  .prose h6 {
    color: var(--ink-soft);
  }

  .prose a {
    text-decoration-color: var(--signal);
  }

  .prose ul,
  .prose ol {
    padding-left: 1.6rem;
  }

  .prose ul {
    list-style: disc;
  }

  .prose ul ul {
    list-style: circle;
  }

  .prose ol {
    list-style: decimal;
  }

  .prose li + li,
  .prose li > ul,
  .prose li > ol {
    margin-top: var(--space-1);
  }

  .prose li > p + p {
    margin-top: var(--space-2);
  }

  /* Task lists: the tick box stands in for the bullet. */
  .prose li:has(> input[type="checkbox"]) {
    margin-left: -1.6rem;
    list-style: none;
  }

  .prose li > input[type="checkbox"] {
    width: 1rem;
    height: 1rem;
    margin: 0 var(--space-2) 0 0;
    vertical-align: -0.15em;
    accent-color: var(--ink);
  }

  .prose dt {
    font-weight: 650;
  }

  .prose dd {
    margin: 0 0 var(--space-2) 1.6rem;
  }

  .prose blockquote {
    margin-inline: 0;
    padding: var(--space-1) 0 var(--space-1) var(--space-4);
    border-left: 3px solid var(--rule);
    color: var(--ink-soft);
  }

  .prose blockquote > * + * {
    margin-top: var(--space-3);
  }

  .prose code {
    padding: 0.1em 0.35em;
    border: 1px solid var(--rule-soft);
    border-radius: 4px;
    background: var(--well);
    font-size: 0.875em;
  }

  .prose pre {
    padding: var(--space-3) var(--space-4);
    overflow-x: auto;
    border: 1px solid var(--rule);
    border-radius: var(--radius-control);
    background: var(--well);
    line-height: 1.55;
    overflow-wrap: normal;
  }

  .prose pre code {
    padding: 0;
    border: 0;
    background: none;
    font-size: var(--step--1);
  }

  /* A wide table scrolls sideways within the sheet rather than stretching it. */
  .prose table {
    display: block;
    width: max-content;
    max-width: 100%;
    overflow-x: auto;
    border-collapse: collapse;
    font-size: var(--step-0);
    overflow-wrap: normal;
  }

  .prose th,
  .prose td {
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--rule);
    text-align: left;
    vertical-align: top;
  }

  .prose th {
    background: var(--well);
    font-weight: 650;
  }

  .prose :is(th, td)[align="right"] {
    text-align: right;
    font-variant-numeric: tabular-nums;
  }

  .prose :is(th, td)[align="center"] {
    text-align: center;
  }

  .prose hr {
    margin: var(--space-5) 0;
  }

  /*
   * The reader: one file filling the window, opened in its own tab. A bar that
   * stays put says what is being read and how it is shown; under it, the file.
   */
  .reader {
    min-height: 100vh;
  }

  .reader__bar {
    position: sticky;
    top: 0;
    z-index: 2;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3) var(--space-5);
    padding: var(--space-3) clamp(var(--space-4), 4vw, var(--space-7));
    border-bottom: 1px solid var(--rule);
    background: var(--surface);
    box-shadow: var(--shadow);
  }

  .reader__title {
    min-width: 0;
  }

  .reader__name {
    font-family: var(--font-data);
    font-size: var(--step-0);
    font-stretch: 100%;
    font-weight: 500;
    letter-spacing: 0;
    line-height: 1.4;
  }

  .reader__actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2) var(--space-4);
  }

  .reader__body {
    padding: var(--space-5) clamp(var(--space-4), 4vw, var(--space-7)) var(--space-7);
  }

  /* A rendered document keeps a readable measure however wide the window. */
  .reader__body--sheet {
    max-width: 62rem;
    margin-inline: auto;
  }

  .reader > .notice {
    margin: var(--space-4) clamp(var(--space-4), 4vw, var(--space-7)) 0;
  }
}
