/*
  Mid-1980s Macintosh look, in light grays: flat controls drawn with
  black lines, rounded push buttons, dithered fills. Nothing is bevelled.
  The browser tab is the window, so the page doesn't draw another one.

  Elements are styled by what they are rather than by class:
  main is the page's centered column, fieldset is a group box,
  nav is the menu bar, a label wrapping a file input is the drop area.
*/

@layer reset, tokens, base, layout, controls, pages;

/* ChicagoFLF: Robin Casady's public-domain Chicago (static/fonts/chicagoflf-readme.txt).
   It comes in one weight that's already heavy, so it's declared for every weight
   to stop the browser drawing a thicker fake bold.
   Its outlines sit on a 12-pixel grid, so it's sharp at 12, 18 and 24px. Shrinking it
   to 12/14 puts it there at the 14, 21 and 28px text sizes below. */
@font-face {
  font-family: ChicagoFLF;
  src: url("fonts/chicagoflf.woff2") format("woff2");
  font-weight: 100 900;
  size-adjust: 85.7143%;
}

@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  body, h1, h2, p, ul, menu, fieldset, table {
    margin: 0;
  }

  ul, menu {
    padding: 0;
    list-style: none;
  }

  img {
    display: block;
    max-inline-size: 100%;
  }

  input, button, select {
    font: inherit;
    color: inherit;
  }
}

@layer tokens {
  :root {
    color-scheme: light;

    --ink: #000;
    --paper: #fff;
    --window: #eee;
    --gray-a: #d4d4d4;
    --gray-b: #e2e2e2;
    --muted: #6b6b6b;

    /* A one-pixel checkerboard of two light grays: the old Mac desktop pattern. */
    --dither-light: repeating-conic-gradient(var(--gray-a) 0 25%, var(--gray-b) 0 50%) 0 0 / 2px 2px;
    /* 50% gray made of black and white dots. */
    --dither-ink: repeating-conic-gradient(var(--ink) 0 25%, var(--paper) 0 50%) 0 0 / 2px 2px;

    --shadow: 2px 2px 0 var(--ink);

    /* Geneva for text. It has no bold, and browsers won't fake one, so anything bold
       is set in ChicagoFLF, falling back to Chicago if it's installed, then Verdana Bold. */
    --font: Geneva, Verdana, sans-serif;
    --font-bold: ChicagoFLF, Chicago, Charcoal, Verdana, sans-serif;
    /* Steps of 1, 1.5 and 2 times, so ChicagoFLF draws at 12, 18 and 24px: one font pixel
       per screen pixel, then three and four Retina pixels. Other sizes blur it. */
    --text-small: 0.75rem;
    --text: 0.875rem; /* 14px */
    --text-large: 1.3125rem; /* 21px */
    --text-title: 1.75rem; /* 28px */

    --glyph-popup: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 5'><path d='M0 0h9L4.5 5z'/></svg>");
    --icon-document: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 48 56'><path d='M.5.5h33l14 14v41H.5z' fill='white' stroke='black'/><path d='M33.5.5v14h14' fill='rgb(170,170,170)' stroke='black'/></svg>");
    --icon-photo: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 48 56'><path d='M.5.5h33l14 14v41H.5z' fill='white' stroke='black'/><path d='M33.5.5v14h14' fill='rgb(170,170,170)' stroke='black'/><path d='M8.5 22.5h31v23h-31z' fill='rgb(170,170,170)' stroke='black'/><path d='M9 45l10-12 6 7 4-4 10 9z' fill='rgb(85,85,85)'/><circle cx='31' cy='29' r='3' fill='white'/></svg>");
  }
}

@layer base {
  body {
    min-block-size: 100dvh;
    background: var(--window);
    color: var(--ink);
    font: var(--text) / 1.4 var(--font);
  }

  ::selection {
    background: var(--ink);
    color: var(--paper);
  }

  :focus-visible {
    outline: 2px dotted var(--ink);
    outline-offset: 2px;
  }

  a {
    color: var(--ink);
  }

  :is(h1, h2, b, strong) {
    font-family: var(--font-bold);
    font-weight: bold;
    text-wrap: balance;
  }

  :is(h1, h2) {
    line-height: 1.2;
  }
}

@layer layout {
  /* Content sits in a centered column on the gray page. */
  main {
    --content: 38rem;
    container: column / inline-size;
    display: grid;
    row-gap: 1rem;
    inline-size: min(100% - 2rem, var(--content));
    margin-inline: auto;
    padding-block: 1.5rem 2rem;
  }

  body:has(> nav) main {
    --content: 64rem;
  }

  main:has([type="password"]) {
    --content: 24rem;
  }

  main > header h1 {
    font-size: var(--text-title);
  }
}

@layer controls {
  form {
    display: grid;
    gap: 1rem;
  }

  form > footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 1rem;
  }

  fieldset {
    min-inline-size: 0;
    padding: 0.75rem 1rem 1rem;
    border: 1px solid var(--ink);
  }

  legend {
    padding-inline: 0.25rem;
  }

  /* Labels and their controls line up in two columns, labels flush right. */
  :has(> label > :is(input:not([type="file"]), select)) {
    display: grid;
    grid-template-columns: max-content minmax(0, 1fr);
    gap: 0.5rem 0.75rem;
    align-items: center;

    & > * {
      grid-column: 1 / -1;
    }

    & > label {
      display: grid;
      grid-template-columns: subgrid;
      align-items: center;
      text-align: end;
    }

    & > label > * {
      justify-self: start;
      text-align: start;
    }
  }

  /* In a narrow column, labels go above their controls. */
  @container column (width < 20rem) {
    :has(> label > :is(input:not([type="file"]), select)) {
      grid-template-columns: minmax(0, 1fr);

      & > label {
        row-gap: 0.25rem;
        text-align: start;
      }
    }
  }

  label:has(> :user-invalid)::after {
    content: "Required";
    grid-column: -2;
    font-size: var(--text-small);
    text-align: start;
  }

  input:is([type="text"], [type="password"]) {
    inline-size: 100%;
    max-inline-size: 20rem;
    padding: 3px 5px;
    background: var(--paper);
    border: 1px solid var(--ink);
    border-radius: 0;
  }

  /* Pop-up menus: a shadowed box with a small triangle. */
  select {
    appearance: none;
    min-inline-size: 7rem;
    padding: 3px 26px 3px 8px;
    background: var(--paper) var(--glyph-popup) no-repeat right 8px center / 9px 5px;
    border: 1px solid var(--ink);
    border-radius: 0;
    box-shadow: var(--shadow);
  }

  /* Push buttons: rounded rectangles that invert while pressed. */
  button,
  a[download],
  ::file-selector-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-block-size: 24px;
    padding: 2px 16px;
    background: var(--paper);
    color: var(--ink);
    font: var(--text) / 1.3 var(--font);
    text-decoration: none;
    border: 1px solid var(--ink);
    border-radius: 8px;
  }

  ::file-selector-button {
    margin-inline-end: 0.5rem;
  }

  :is(button, a[download]):active,
  input[type="file"]:active::file-selector-button {
    background: var(--ink);
    color: var(--paper);
  }

  button:disabled {
    color: var(--muted);
    border-color: var(--muted);
  }

  form:has(button:disabled) {
    cursor: progress;
  }

  /* The default button wears a thick rounded ring, so Return clearly means "this one". */
  form > footer > button[type="submit"] {
    margin: 4px;
    box-shadow: 0 0 0 2px var(--window), 0 0 0 5px var(--ink);

    /* The ring covers the usual focus outline, so draw it outside the ring instead. */
    &:focus-visible {
      outline-offset: 7px;
    }
  }

  progress {
    appearance: none;
    inline-size: 100%;
    block-size: 14px;
    background: var(--paper);
    border: 1px solid var(--ink);

    &::-webkit-progress-bar {
      background: transparent;
    }

    &::-webkit-progress-value {
      background: var(--dither-ink);
    }

    &::-moz-progress-bar {
      background: var(--dither-ink);
    }
  }

  table {
    inline-size: 100%;
    border-spacing: 0;
    background: var(--paper);
    border: 1px solid var(--ink);
    font-variant-numeric: tabular-nums;
  }

  th {
    padding: 4px 8px;
    border-block-end: 1px solid var(--ink);
    font-weight: normal;
    text-align: start;
  }

  td {
    padding: 6px 8px;
    vertical-align: middle;
  }

  tr + tr > td {
    border-block-start: 1px dotted var(--muted);
  }

  :is(th, td):not(:first-child) {
    text-align: end;
  }

  @container column (width < 30rem) {
    :is(th, td):is(:nth-child(2), :nth-child(4)) {
      display: none;
    }
  }
}

@layer pages {
  /* Upload: the drop area, outlined in marching ants. The whole area is a label,
     so clicking anywhere in it opens the file picker. */
  label:has(> input[type="file"]) {
    display: grid;
    grid-template-columns: minmax(0, 1fr); /* lets the text wrap instead of widening the area */
    justify-items: center;
    gap: 0.25rem;
    padding: 1.25rem 1rem 1rem;
    text-align: center;
    background: var(--paper);
    border: 1px dashed var(--ink);

    &::before {
      content: "";
      inline-size: 48px;
      block-size: 56px;
      margin-block-end: 0.5rem;
      background: var(--icon-photo);
    }

    /* While a file is dragged over it, the area fills with a light dither. */
    &[data-over] {
      background: var(--dither-light);
      border-style: solid;
    }

    & strong {
      font-size: var(--text-large);
    }

    & small {
      font-size: var(--text-small);
    }

    & input {
      max-inline-size: 100%;
      margin-block-start: 0.75rem;
    }
  }

  /* The list of chosen files under the drop area. */
  label:has(> input[type="file"]) + p {
    margin-block-start: 0.5rem;
    font-size: var(--text-small);
    overflow-wrap: anywhere;

    &:empty {
      display: none;
    }
  }

  form > footer progress {
    flex: 1;
  }

  /* The progress bar only shows while an upload is running, which is when htmx disables
     the Upload button. */
  form:not(:has(button:disabled)) > footer progress {
    display: none;
  }

  /* Upload results appear in a white box under the form. */
  #result {
    display: grid;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--paper);
    border: 1px solid var(--ink);
    overflow-wrap: anywhere;

    /* Collapse when empty, but stay rendered: a live region hidden with display: none
       may not announce the result when htmx swaps it in. The negative margin cancels main's row gap. */
    &:empty {
      block-size: 0;
      padding: 0;
      border: 0;
      margin-block-start: -1rem;
    }

    & h2 {
      font-size: var(--text-large);
    }

    & ul {
      padding-inline-start: 1.25rem;
      list-style: square;
    }
  }

  form > [role="alert"] {
    font-family: var(--font-bold);
    font-weight: bold;
  }

  /* Admin: the menu bar, set in Chicago as the Mac's was.
     The current page's item is shown inverted. */
  nav {
    position: sticky;
    inset-block-start: 0;
    z-index: 1;
    display: flex;
    flex-wrap: wrap;
    padding-inline: 0.5rem;
    background: var(--paper);
    border-block-end: 1px solid var(--ink);
    font-family: var(--font-bold);

    & > header {
      padding: 3px 12px;
      font-weight: bold;
    }

    & menu {
      display: flex;
    }

    & :is(a, button) {
      display: block;
      min-block-size: 0;
      padding: 3px 12px;
      background: none;
      color: var(--ink);
      font: inherit;
      text-decoration: none;
      border: 0;
      border-radius: 0;
      outline-offset: -3px;
    }

    & :is(a, button):active,
    & [aria-current="page"] {
      background: var(--ink);
      color: var(--paper);
    }
  }

  /* Admin: a summary line with its download button pushed to the right. */
  main > header + p {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem 1rem;
  }

  main > section {
    display: grid;
    gap: 0.5rem;

    & > header {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 0.25rem 1rem;
    }

    & h2 {
      font-size: var(--text-large);
    }

    & > header p {
      font-size: var(--text-small);
    }

    & > header a[download] {
      margin-inline-start: auto;
    }
  }

  /* Each student's photos as a Finder icon view: picture on top, name underneath. */
  main > section > ul {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(7rem, 1fr));
    gap: 1rem 0.5rem;
    padding: 1rem 0.5rem;
    background: var(--paper);
    border: 1px solid var(--ink);

    & a {
      display: grid;
      grid-template-rows: 96px auto;
      justify-items: center;
      gap: 0.375rem;
      text-decoration: none;
      font-size: var(--text-small);
      text-align: center;
    }

    /* Thumbnails keep their shape inside a 96px square, framed by a black line. */
    & img {
      align-self: center;
      inline-size: auto;
      block-size: auto;
      max-inline-size: 96px;
      max-block-size: 96px;
      border: 1px solid var(--ink);
    }

    /* TIFF and PSD files can't be previewed, so they get the document icon labelled with the type. */
    & a[data-kind]::before {
      content: attr(data-kind);
      inline-size: 96px;
      block-size: 96px;
      display: grid;
      place-items: end center;
      padding-block-end: 22px;
      background: var(--icon-document) center / 56px 66px no-repeat;
      font: bold var(--text) var(--font-bold);
    }

    & span {
      max-inline-size: 100%;
      padding-inline: 3px;
      overflow-wrap: anywhere;
    }

    /* The selected icon's name inverts, as in the Finder. */
    & a:is(:hover, :focus-visible) span {
      background: var(--ink);
      color: var(--paper);
    }
  }
}
