/* The cellar the notebook is read in: the wall behind the page, the desk in front of it, and their lights.
   The drawing is made by src/scene/ and kept by src/ui/room.js; this file only places and animates it. */
:root {
  scroll-padding-bottom: var(--desk); /* whatever is brought into view is brought clear of the desk */
  --desk: 0px; /* the height at the foot of every page that belongs to the desk */
  --quiet-left: 0px; /* how far in from each side the page's part of the wall begins */
}

/* The wall does not move with the page. It is as tall as the window ever gets, so a phone's toolbar hiding uncovers nothing. */
.room { position: fixed; z-index: 0; top: 0; left: 0; width: 100%; height: 100vh; height: 100lvh; overflow: hidden; pointer-events: none; }
.room canvas { position: absolute; top: 0; left: 0; }
/* Lamplight is the dark it leaves, and the dark is never quite still. */
.room .room__dark { width: 100% !important; height: 100% !important; animation: lamplight 11s ease-in-out infinite; }
@keyframes lamplight {
  0%, 100% { opacity: 1; }
  14% { opacity: .95; }
  23% { opacity: .99; }
  41% { opacity: .92; }
  52% { opacity: .975; }
  77% { opacity: .94; }
}

/* The desk stays at the foot of the window, in front of the page: what is written passes behind it. */
.room-desk { position: fixed; z-index: 2; left: 0; bottom: 0; width: 100%; pointer-events: none; }
.room-desk canvas { position: relative; display: block; }
/* Behind the things on the desk the page goes out of focus, so a line never tangles with a candle. */
.room-desk__veil {
  position: absolute;
  inset: auto var(--quiet-left, 0px) 0;
  height: var(--desk);
  -webkit-backdrop-filter: blur(7px) brightness(.86);
  backdrop-filter: blur(7px) brightness(.86);
  -webkit-mask-image: linear-gradient(to bottom, transparent, #000 min(16px, 25%));
  mask-image: linear-gradient(to bottom, transparent, #000 min(16px, 25%));
}

/* The lantern hangs at the head of the page, between the mark and the way to the paths, and is left
   behind with them when the page is read further down. Its light stays. */
.room-lamp { position: absolute; z-index: 2; top: 0; left: 50%; translate: -50%; pointer-events: none; }
.room-lamp canvas { display: block; }

/* A flame stands on its wick: the point it is placed at is the foot of it. */
.flame { position: absolute; width: var(--size); height: calc(var(--size) * 2); margin: calc(var(--size) * -2) 0 0 calc(var(--size) * -.5); }
.flame::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 55%;
  width: calc(var(--size) * 13);
  aspect-ratio: 1;
  translate: -50% -50%;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(240, 238, 228, .26), rgba(240, 238, 228, .1) 28%, rgba(240, 238, 228, .03) 60%, rgba(240, 238, 228, 0));
  opacity: 0;
  transition: opacity .7s ease-out;
}
.flame svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.flame__body { fill: #f1efe6; stroke: var(--ink); stroke-width: .5; stroke-linejoin: round; filter: blur(.3px); opacity: 0; transform: scale(.2); transform-origin: 50% 100%; transition: opacity .35s ease-out, transform .5s cubic-bezier(.3, 1.4, .5, 1); }
.flame.lit::before { opacity: 1; animation: glimmer 4.3s ease-in-out var(--beat, 0s) infinite; }
.flame.lit .flame__body { opacity: 1; transform: none; animation: flame 3.1s ease-in-out var(--beat, 0s) infinite; }
@keyframes flame {
  0%, 100% { transform: none; }
  22% { transform: scale(.94, 1.06) skewX(-3deg); }
  38% { transform: scale(1.03, .95) skewX(2deg); }
  61% { transform: scale(.96, 1.08) skewX(4deg); }
  80% { transform: scale(1.02, .97) skewX(-2deg); }
}
@keyframes glimmer {
  0%, 100% { opacity: 1; }
  30% { opacity: .8; }
  55% { opacity: .95; }
  72% { opacity: .74; }
}
/* Put out: a thread of smoke goes up from the wick and thins. */
.flame .flame__smoke { inset: auto 0 10% -25%; width: 150%; height: calc(var(--size) * 4.5); fill: none; stroke: var(--pencil); stroke-width: .8; stroke-linecap: round; opacity: 0; }
.flame__smoke path { stroke-dasharray: 1; stroke-dashoffset: 1; }
.flame.snuffed .flame__smoke { animation: smoke 2.1s ease-out; }
.flame.snuffed .flame__smoke path { stroke-dashoffset: 0; animation: draw 1.3s ease-out; }
@keyframes smoke {
  0% { opacity: 0; }
  15% { opacity: .7; }
  100% { opacity: 0; translate: 2px -6px; }
}

/* The run of right answers, chalked on the front of the desk under the candles. */
.run { position: absolute; display: inline-flex; align-items: center; gap: 10px; translate: -50% -50%; scale: var(--tally, .8); pointer-events: auto; }
.run__count { font: 22px/1 var(--hand); color: var(--pencil); }
.tally { flex: none; }
.tally .fresh { stroke-dasharray: 1; animation: draw 320ms cubic-bezier(.45, 0, .25, 1) both; animation-delay: calc(var(--i, 0) * 140ms + 250ms); }
.tally .rubbed { animation: rub 480ms ease-out forwards; }

@media (prefers-reduced-motion: reduce) {
  .room .room__dark, .flame.lit::before, .flame.lit .flame__body { animation: none; }
  /* A flame put out simply goes; no smoke rises from it. */
  .flame.snuffed .flame__smoke, .flame.snuffed .flame__smoke path { animation: none; }
}

/* The cellar darkens as path candles are put out (--lit), and the page's ink with it (styles/notebook.css). */
/* Only the paths page changes it, and there the cellar and the page fade together; a page loaded in the dark starts dark. */
:root:has(.paths) { transition: --lit 1.2s ease-out; }
.room::after { content: ""; position: absolute; inset: 0; z-index: 1; background: #020302; opacity: calc((1 - var(--lit)) * .82); pointer-events: none; }
/* The desk dims through its own drawing, so the clear sheet above the desk does not darken the wall twice. */
.room-desk > canvas { filter: brightness(calc(1 - (1 - var(--lit)) * .82)); }
/* The flames themselves stay bright: they are what is left. */
:is(.room, .room-desk) .flame { z-index: 2; }
.paths__scenery { filter: brightness(var(--k)); }
