/**
 * The curtain over a space while it comes up.
 *
 * Opaque, in the canvas's own `--background`, so lifting it reads as the board arriving rather than
 * as one surface swapped for another. Above everything the space draws, the fullscreen display and
 * the save indicator included: nothing under it is in its final place yet.
 *
 * Shown by default — the server renders it, and an attribute the view has not set yet must mean
 * "still loading". `[done]` fades it, then takes it out of hit-testing and the accessibility tree
 * with `visibility`, delayed until the fade has finished.
 */
atomics-space-loader {
  --loader-ink: var(--ds-ink, var(--text));
  --loader-mute: var(--ds-mute, color-mix(in srgb, var(--text) 58%, var(--background)));
  --loader-track: var(--ds-line, color-mix(in srgb, var(--text) 10%, transparent));
  --loader-accent: var(--ds-accent, #2f6bff);

  /*
    The screen, not the fixed-position viewport. On a phone those are different boxes whenever
    anything on the page is wider than the screen: the browser could zoom out to show it, so it
    stretches the box `inset: 0` fills to the zoomed-out size, and the curtain's middle lands well
    below the middle of the screen — Keith's phone, 2026-10-24, where it sat three quarters of the way
    down and jumped to the centre just before lifting, the moment the space locked zooming. Viewport
    units are measured from the screen, so the curtain covers that and centres on it either way.
  */
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  z-index: 2147483000;
  background: var(--background, #f0f0f0);
  color: var(--loader-ink);
  font-family: var(--ds-font, inherit);
  opacity: 1;
  visibility: visible;
  user-select: none;
  touch-action: none;
  transition: none;
}

/*
  A short hold before a quick fade, so a space that opens in a blink does not flash the curtain, and
  the board is not seen through a half-faded curtain for long — which read as cards drawn on top of
  it (Keith, 2026-10-24).
*/
atomics-space-loader[done] {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition:
    opacity 180ms ease 60ms,
    visibility 0s linear 240ms;
}

/**
 * The part of the curtain that is actually on screen, which is not always all of it.
 *
 * On a phone the fixed box can be larger than what is visible while the page comes up: iOS sizes it
 * against a layout viewport that the restored scroll position and the not-yet-mounted canvas have
 * not settled, so centring in the box put the spinner near the bottom of the screen until the board
 * mounted and it jumped up. The logic measures the visual viewport against the box each frame while
 * the curtain is up and writes it here; without those numbers this is simply the whole box.
 */
atomics-space-loader [veil] {
  position: absolute;
  left: var(--loader-x, 0px);
  top: var(--loader-y, 0px);
  width: var(--loader-w, 100%);
  height: var(--loader-h, 100%);
  display: grid;
  place-items: center;
  padding: 1rem;
  box-sizing: border-box;
}

atomics-space-loader [stage] {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  max-width: min(22rem, 100%);
  text-align: center;
}

atomics-space-loader [indicator] {
  position: relative;
  display: grid;
  place-items: center;
  width: 4rem;
  height: 4rem;
  margin-bottom: 0.25rem;
}

/* Opening a disk: one ring turning around a still mark, the quietest thing that still says "working". */
atomics-space-loader [ring] {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2.5px solid var(--loader-track);
  border-top-color: var(--loader-ink);
  will-change: transform;
  animation: atomics-loader-spin 1000ms cubic-bezier(0.5, 0.15, 0.5, 0.85) infinite;
}

atomics-space-loader [mark] {
  position: relative;
  display: grid;
  width: 1.75rem;
  height: 1.75rem;
  color: var(--loader-ink);
}

atomics-space-loader [mark] svg {
  width: 100%;
  height: 100%;
}

/*
  The first boot: the Atomics mark alone, colourless, with no words and nothing glowing or stacked
  around it (Keith, 2026-11-29), over a row of dots that pulse one after another.
*/
atomics-space-loader[reason='boot'] [indicator] {
  width: 4.5rem;
  height: 4.5rem;
  margin-bottom: 0.5rem;
}

atomics-space-loader[reason='boot'] [ring] {
  display: none;
}

atomics-space-loader[reason='boot'] [mark] {
  position: absolute;
  inset: 0;
  width: auto;
  height: auto;
  place-items: center;
  border-radius: 50%;
  background: var(--background, #f0f0f0);
  color: var(--loader-ink);
}

/* No words on the first boot: the heading is still announced, through `role="status"`, but not drawn. */
atomics-space-loader[reason='boot'] [heading] {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

atomics-space-loader [dots] {
  display: none;
}

atomics-space-loader[reason='boot'] [quip] {
  display: none;
}

atomics-space-loader[reason='boot'] [dots] {
  display: flex;
  gap: 0.375rem;
  height: 1.4em;
  align-items: center;
}

atomics-space-loader[reason='boot'] [dots] i {
  width: 0.375rem;
  height: 0.375rem;
  border-radius: 50%;
  background: var(--loader-ink);
  opacity: 0.25;
  will-change: opacity;
  animation: atomics-loader-pulse 1050ms ease-in-out infinite;
}

atomics-space-loader[reason='boot'] [dots] i:nth-child(2) {
  animation-delay: 175ms;
}

atomics-space-loader[reason='boot'] [dots] i:nth-child(3) {
  animation-delay: 350ms;
}

/*
  Still once it has gone, not as it goes: an animation under `visibility: hidden` still ticks, but
  stopping it on `[done]` froze the spinner for the length of the fade. `[rested]` is set by the
  logic once the fade is over.
*/
atomics-space-loader[done][rested] [ring],
atomics-space-loader[done][rested] [dots] i {
  animation: none;
}

@keyframes atomics-loader-spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes atomics-loader-pulse {
  40% {
    opacity: 1;
  }
}

atomics-space-loader [heading] {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.3;
}

atomics-space-loader [heading] > span {
  display: none;
}

atomics-space-loader:not([reason]):not([named]) [heading] > [for='open'],
atomics-space-loader[reason='open']:not([named]) [heading] > [for='open'],
atomics-space-loader:not([reason])[named] [heading] > [for='named'],
atomics-space-loader[reason='open'][named] [heading] > [for='named'],
atomics-space-loader[reason='switch'] [heading] > [for='switch'],
atomics-space-loader[reason='reload'] [heading] > [for='reload'],
atomics-space-loader[reason='boot'] [heading] > [for='boot'] {
  display: inline;
}

atomics-space-loader [disk-name] {
  font-weight: inherit;
}

/*
  The installed app's own splash, in the disks page: the OS splash is only the icon on the
  manifest's background colour, so the page draws the badge until the disks have listed. In an
  ordinary tab the page is up at once and there is nothing to cover.
*/
atomics-space-loader[splash] {
  display: none;
}

@media (display-mode: standalone) {
  atomics-space-loader[splash] {
    display: block;
  }
}

atomics-space-loader [quip] {
  margin: 0;
  min-height: 1.4em;
  font-size: 0.875rem;
  line-height: 1.4;
  color: var(--loader-mute);
  transition:
    opacity 220ms ease,
    translate 220ms ease;
}

atomics-space-loader [quip][leaving] {
  opacity: 0;
  translate: 0 -0.25rem;
}

atomics-space-loader [slow] {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

atomics-space-loader [slow][hidden] {
  display: none;
}

atomics-space-loader [slow] p {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--loader-mute);
}

atomics-space-loader [reload] {
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 0.5rem 1rem;
  min-height: 2.25rem;
  border: 0;
  border-radius: 100vmax;
  background: var(--loader-accent);
  color: var(--ds-on-accent, #fff);
  cursor: pointer;
}

atomics-space-loader [reload]:hover {
  background: var(--ds-accent-hover, var(--loader-accent));
}

atomics-space-loader [reload]:focus-visible {
  outline: 2px solid var(--loader-accent);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  atomics-space-loader [ring] {
    animation-duration: 3000ms;
  }

  atomics-space-loader[reason='boot'] [dots] i {
    animation-duration: 3000ms;
  }

  atomics-space-loader [quip],
  atomics-space-loader[done] {
    transition: none;
  }
}
