/* Midnight Journal — load after experience.css. */
:root {
  --journal-grain: .07;
  --journal-doodle-opacity: .37;
  --journal-dust-opacity: 1;
}

/* Keep navigation.js's existing scene colour system intact. */
.has-ambient { isolation: isolate; }
.archive-background.midnight-journal {
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 74% 19%, var(--scene-light), transparent 62%),
    radial-gradient(ellipse at 8% 87%, #68776b1c, transparent 57%),
    #1c1816;
  transition: --scene-light 1.4s ease;
}

/* Fine, static paper grain: no external image or continuous repaint. */
.midnight-journal::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.78' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.85'/%3E%3C/svg%3E");
  background-size: 180px 180px;
  opacity: var(--journal-grain);
  mix-blend-mode: soft-light;
}

/* Vignette, with a gentle pool of warmth near the top. */
.midnight-journal::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at 50% 42%, transparent 38%, #0d0b0a60 100%);
}

.journal-decor, .journal-dust {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.journal-decor {
  transform: translate3d(var(--journal-x, 0px), var(--journal-y, 0px), 0);
  transition: transform 550ms cubic-bezier(.2,.75,.22,1);
  opacity: var(--journal-doodle-opacity);
}
.journal-doodle {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--size);
  height: var(--size);
  color: #cfad86;
  opacity: calc(var(--alpha) + var(--near, 0) * .32);
  transition: opacity 400ms ease, transform 450ms ease;
  transform: translate(-50%, -50%) rotate(var(--angle)) scale(calc(1 + var(--near, 0) * .16));
}
.journal-doodle svg {
  display: block; width: 100%; height: 100%;
  animation: journal-sway 9s ease-in-out var(--phase) infinite;
}
.journal-doodle.is-near { color: #f5cfa5; }
@keyframes journal-sway {
  0%, 100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-5px) rotate(var(--sway)); }
}
.journal-dust {
  opacity: var(--journal-dust-opacity);
  transform: translate3d(var(--dust-x, 0px), var(--dust-y, 0px), 0);
  transition: transform 800ms cubic-bezier(.2,.75,.22,1);
}
.journal-click-spark {
  position: absolute;
  width: 10px;
  height: 10px;
  background: #efd0a5;
  clip-path: polygon(50% 0, 61% 39%, 100% 50%, 61% 61%, 50% 100%, 39% 61%, 0 50%, 39% 39%);
  animation: journal-click 600ms ease-out both;
}
@keyframes journal-click {
  from { opacity: .65; transform: translate(-50%, -50%) scale(.35) rotate(-12deg); }
  35% { opacity: .6; transform: translate(-50%, -70%) scale(1) rotate(8deg); }
  to { opacity: 0; transform: translate(-50%, -160%) scale(.4) rotate(20deg); }
}
.journal-speck {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  background: #eed0a4;
  opacity: var(--alpha);
  animation: journal-float var(--duration) ease-in-out var(--delay) infinite;
}
.journal-speck.is-glow { box-shadow: 0 0 6px 1px #e9b77d65; }
@keyframes journal-float {
  0%, 100% { transform: translate3d(0, 0, 0); opacity: calc(var(--alpha) * .55); }
  45% { transform: translate3d(var(--dx), var(--dy), 0); opacity: var(--alpha); }
  75% { transform: translate3d(calc(var(--dx) * .45), calc(var(--dy) * .7), 0); opacity: calc(var(--alpha) * .7); }
}

/* Keep the existing sticky navigation legible, but blend it into the paper. */
.has-ambient .masthead {
  background: rgb(25 22 19 / 96%);
  border-block-end-color: rgb(216 179 142 / 12%);
}

/* Fewer marks on small screens, where there are no generous margins. */
@media (max-width: 54rem) {
  .midnight-journal { --journal-doodle-opacity: .16; --journal-dust-opacity: .45; }
  .journal-doodle.is-desktop, .journal-speck.is-desktop { display: none; }
  .journal-decor, .journal-dust { transform: none; }
  .journal-doodle svg { animation: none; }
  .journal-click-spark { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .archive-background.midnight-journal, .journal-decor, .journal-dust, .journal-doodle { transition: none; }
  .journal-decor, .journal-dust { transform: none; }
  .journal-speck, .journal-doodle svg { animation: none; }
  .journal-click-spark { display: none; }
}
.midnight-journal.is-paused :is(.journal-speck, .journal-doodle svg, .journal-click-spark) { animation-play-state: paused; }

.journal-doodle svg {
  animation-duration: 6s;
}

@keyframes journal-sway {
  0%, 100% {
    transform: translateY(0) rotate(-4deg);
  }

  50% {
    transform: translateY(-14px) rotate(5deg);
  }
}

.journal-doodle {
  opacity: calc(var(--alpha) + var(--near, 0) * .55);

  transform:
    translate(-50%, -50%)
    rotate(var(--angle))
    scale(calc(1 + var(--near, 0) * .35));
}

@media (prefers-reduced-motion: reduce) {
  .journal-doodle svg {
    animation: none;
  }
}

/* ==============================
   Responsive layout corrections
   ============================== */

/* Desktop navigation. */
.masthead .masthead-inner {
  grid-template-columns: auto minmax(0, 1fr) auto;
  grid-template-areas: "name index search";
  column-gap: clamp(.75rem, 2vw, 2rem);
}

/* Prevent the archive name from wrapping vertically. */
.masthead .archive-name {
  min-inline-size: 0;
  max-inline-size: min(25vw, 23rem);
  white-space: nowrap;
  align-self: center;
}

.masthead .archive-name span {
  min-inline-size: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Navigation scrolls horizontally instead of wrapping. */
.masthead .section-index {
  min-inline-size: 0;
  inline-size: 100%;
  justify-content: safe center;
  overflow-x: auto;
  scrollbar-width: none;
}

.masthead .section-index::-webkit-scrollbar {
  display: none;
}

.masthead .section-index a {
  white-space: nowrap;
  flex-shrink: 0;
}

/* Keep search and share controls together. */
.masthead .nav-actions {
  flex-wrap: nowrap;
  white-space: nowrap;
}

.masthead .search-trigger,
.masthead .share-trigger {
  white-space: nowrap;
  flex-shrink: 0;
}

/* Tablet and narrow desktop windows. */
@media (max-width: 68rem) {
  .masthead .masthead-inner {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "name search"
      "index index";

    gap: .25rem .75rem;
    padding-block-start: .5rem;
  }

  .masthead .archive-name {
    max-inline-size: 100%;
  }

  .masthead .section-index {
    justify-self: stretch;
  }

  .masthead .section-index a {
    min-block-size: 3rem;
  }
}

/* Prevent the main heading from being squeezed. */
.subject h1 {
  overflow-wrap: break-word;
  text-wrap: balance;
}

@media (max-width: 68rem) {
  .subject h1 {
    font-size: clamp(2.2rem, 4.5vw, 3.55rem);
  }
}

/* Stack the introduction before it gets too narrow. */
@media (max-width: 60rem) {
  .subject,
  .subject:not(:has(.subject-photo)) {
    grid-template-columns: minmax(0, 1fr);
    row-gap: 2rem;
  }

  .subject-photo {
    inline-size: min(100%, 32rem);
    justify-self: start;
  }

  .subject:not(:has(.subject-photo))::after {
    inline-size: min(40%, 12rem);
  }
}

@media (max-width: 45rem) {
  .masthead .section-index a {
    min-block-size: 2.75rem;
  }

  .subject h1 {
    font-size: clamp(2rem, 7vw, 2.8rem);
  }

  .subject-photo {
    inline-size: 100%;
  }
}