/* Bryan's Scary Stories — Ambient Darkness Layer, Light Falloff Engine,
   Paper Stack Reader. CSS carries almost all of the atmosphere; JS only
   supplies timing, state, and the numbers CSS can't compute on its own. */

:root {
  --light-x: 50%;
  --light-y: 32%;
  --room-light: 0;
  --scroll-dark: 0;

  --ink: #241f19;
  --ink-soft: #4a4136;
  --nav-text: rgba(224, 218, 205, 0.5);
  --nav-text-active: #efe9db;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: #060607;
  color: var(--nav-text-active);
  min-height: 100%;
}

body {
  font-family: -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  overflow-x: hidden;
}

.room {
  position: relative;
  min-height: 100vh;
  isolation: isolate;
}

/* --- Light Falloff Engine --- */
/* Three layers: the pool (the fixture itself), the spill (the wall
   behind it, for depth), and the room (the base darkness). Range
   matters more than shape here — a wide flat gradient reads as a
   background color; a tight bright one reads as a light. */

.ambient-layer {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 56% 44% at var(--light-x) var(--light-y),
      rgba(255, 246, 226, calc(0.40 * var(--room-light))) 0%,
      rgba(255, 243, 219, calc(0.15 * var(--room-light))) 26%,
      rgba(146, 140, 126, calc(0.05 * var(--room-light))) 50%,
      rgba(6, 6, 7, 0) 72%),
    radial-gradient(ellipse 120% 70% at var(--light-x) -14%,
      rgba(190, 182, 164, calc(0.07 * var(--room-light))) 0%,
      rgba(6, 6, 7, 0) 64%),
    radial-gradient(ellipse 140% 110% at 50% -10%,
      #121216 0%, #08080a 48%, #030304 100%);
}

.ambient-layer::after {
  content: "";
  position: absolute;
  inset: 0;
  background: #000;
  opacity: calc(0.22 * var(--scroll-dark));
  transition: opacity 220ms linear;
}

.grain {
  position: fixed;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  opacity: 0.045;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* --- Tab-return background states --- */
/* These classes live only on their own elements (fx-*), never on
   #room, which carries a differently-named state-* class. A shared
   bare selector between a parent and its own child is how this used
   to blank the whole page. */

.fx-silhouette,
.fx-hallway,
.fx-headlights {
  position: fixed;
  z-index: 1;
  pointer-events: none;
  opacity: 0;
  transition: opacity 900ms ease;
}

.fx-silhouette {
  right: 8%;
  bottom: 6%;
  width: 90px;
  height: 150px;
  filter: blur(5px);
}

.fx-silhouette::before,
.fx-silhouette::after {
  content: "";
  position: absolute;
  left: 50%;
  background: #1c1c1f;
  transform: translateX(-50%);
}

.fx-silhouette::before {
  top: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
}

.fx-silhouette::after {
  top: 30px;
  width: 70px;
  height: 110px;
  border-radius: 40% 40% 20% 20%;
}

.room.state-silhouette .fx-silhouette {
  opacity: 0.11;
}

.fx-hallway {
  left: 0;
  top: 0;
  width: 22%;
  height: 100%;
  background: linear-gradient(
    100deg,
    rgba(30, 30, 34, 0.6) 0%,
    rgba(20, 20, 23, 0.3) 40%,
    rgba(180, 170, 150, 0.05) 70%,
    transparent 100%
  );
}

.room.state-hallway .fx-hallway {
  opacity: 0.16;
}

.fx-headlights span {
  position: absolute;
  bottom: 10%;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 244, 214, 0.9) 0%, rgba(255, 244, 214, 0) 70%);
  box-shadow: 0 0 14px 4px rgba(255, 244, 214, 0.5);
}

.fx-headlights span:first-child { left: 32%; }
.fx-headlights span:last-child { left: 36%; }

.room.state-headlights .fx-headlights {
  opacity: 0.18;
}

/* --- Hidden margin trigger --- */

.margin-secret {
  position: absolute;
  z-index: 1;
  top: 14%;
  left: 1rem;
  width: 120px;
  height: 200px;
  display: block;
}

/* --- Header / title reveal --- */

.site-header {
  position: relative;
  z-index: 2;
  text-align: center;
  padding: 3rem 1.5rem 1.5rem;
}

.site-title {
  margin: 0;
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-style: italic;
  font-weight: 400;
  font-size: clamp(1.6rem, 4vw, 2.4rem);
  letter-spacing: 0.02em;
  color: #ece5d4;
}

.title-char {
  display: inline-block;
  opacity: 0;
  transform: translateY(var(--dy, 4px));
  filter: blur(var(--blur, 1px));
  animation: char-in 1 ease forwards;
}

@keyframes char-in {
  to {
    opacity: 1;
    transform: translateY(0);
    filter: blur(0);
  }
}

/* --- Layout --- */

.layout {
  position: relative;
  z-index: 2;
  display: flex;
  gap: 3.5rem;
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 2rem 4rem;
  align-items: flex-start;
}

.index-toggle {
  display: none;
}

.story-index {
  flex: 0 0 210px;
  padding-top: 0.5rem;
  position: sticky;
  top: 1.5rem;
  max-height: calc(100vh - 3rem);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(224, 218, 205, 0.25) transparent;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, black 20px, black calc(100% - 20px), transparent 100%);
  mask-image: linear-gradient(to bottom, transparent 0, black 20px, black calc(100% - 20px), transparent 100%);
}

.story-index::-webkit-scrollbar {
  width: 6px;
}

.story-index::-webkit-scrollbar-thumb {
  background: rgba(224, 218, 205, 0.2);
  border-radius: 3px;
}

.story-index ul {
  list-style: none;
  margin: 0;
  padding: 20px 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.story-title {
  background: none;
  border: none;
  padding: 0.1rem 0;
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-size: 0.92rem;
  letter-spacing: 0.02em;
  color: var(--nav-text);
  text-align: left;
  cursor: pointer;
  transition: color 200ms ease;
}

.story-title:hover {
  color: var(--nav-text-active);
}

.story-title[aria-current="true"] {
  color: var(--nav-text-active);
}

.story-title.is-read {
  color: rgba(224, 218, 205, 0.28);
}

.story-title.is-read[aria-current="true"] {
  color: var(--nav-text-active);
}

/* --- Paper Stack Reader --- */

.paper-stack {
  position: relative;
  flex: 1;
  min-height: 70vh;
}

.paper-stack::after {
  /* the sheet's bottom edge falls into shadow instead of clipping text */
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 110px;
  pointer-events: none;
  z-index: 3;
  background: linear-gradient(to bottom,
    rgba(8, 7, 6, 0) 0%,
    rgba(8, 7, 6, 0.45) 55%,
    rgba(5, 5, 6, 0.9) 100%);
}

.paper-sheet {
  border-radius: 0;
  transition: transform 380ms cubic-bezier(0.22, 0.61, 0.36, 1), opacity 380ms ease;
}

.paper-sheet--back,
.paper-sheet--mid {
  position: absolute;
  inset: 0;
  background: #d9d3c2;
  box-shadow: 0 12px 26px rgba(0, 0, 0, 0.5);
}

.paper-sheet--back {
  transform: rotate(-0.6deg) translate(6px, 10px);
  background: #cfc9b8;
}

.paper-sheet--mid {
  transform: rotate(0.4deg) translate(3px, 5px);
}

/* Sheet promotion: the top sheet slides off, the mid sheet rises into
   its slot, the back sheet rises into mid's slot. Content is swapped
   once this has settled, so it reads as a new page rather than a
   text crossfade. */

.stack-advancing .paper-sheet--top {
  transform: translate(-14px, 26px) rotate(-1.4deg);
  opacity: 0;
}

.stack-advancing .paper-sheet--mid {
  transform: rotate(0deg) translate(0, 0);
}

.stack-advancing .paper-sheet--back {
  transform: rotate(0.4deg) translate(3px, 5px);
}

.paper-sheet--top {
  --px: 50%;
  --py: -10%;
  position: relative;
  background:
    radial-gradient(ellipse 110% 78% at var(--px) var(--py),
      rgba(255, 252, 243, calc(0.52 * var(--room-light))) 0%,
      rgba(255, 252, 243, calc(0.18 * var(--room-light))) 38%,
      rgba(255, 252, 243, 0) 68%),
    linear-gradient(to bottom,
      rgba(24, 19, 11, 0) 30%,
      rgba(24, 19, 11, 0.13) 72%,
      rgba(24, 19, 11, 0.24) 100%),
    linear-gradient(to bottom, #cfcabb, #c1bbaa);
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.9),
    0 3px 7px rgba(0, 0, 0, 0.6),
    var(--cast-x, 0px) 26px 54px rgba(0, 0, 0, 0.62),
    inset 0 1px 0 rgba(255, 252, 240, 0.1);
  padding: 3rem 3.25rem 4rem;
  max-height: calc(100vh - 13rem);
  overflow-y: auto;
  color: var(--ink);
}

.paper-sheet--top::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.035;
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='f'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23f)'/%3E%3C/svg%3E");
}

.back-to-index {
  display: none;
}

.paper-sheet--top h2 {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-weight: 600;
  font-size: 1.5rem;
  margin: 0 0 1.5rem;
  color: #1b160f;
  max-width: 60ch;
}

.story-year {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-style: italic;
  font-size: 0.85rem;
  color: var(--ink-soft);
  opacity: 0.6;
  margin: 2rem 0 0;
}

.paper-sheet--top p {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  font-size: 1.08rem;
  line-height: 1.8;
  max-width: 62ch;
  margin: 0 0 1.25rem;
}

/* --- Footer --- */

.author-links {
  position: relative;
  z-index: 2;
  display: flex;
  justify-content: center;
  gap: 1.75rem;
  padding: 1rem 1.5rem 3rem;
  font-size: 0.8rem;
  letter-spacing: 0.02em;
}

.author-links a {
  color: rgba(224, 218, 205, 0.45);
  text-decoration: none;
  transition: color 200ms ease;
}

.author-links a:hover {
  color: var(--nav-text-active);
  text-decoration: underline;
}

/* --- Mobile / tablet: bounded sheet, slide-out index --- */

@media (max-width: 960px) {
  .site-header {
    padding: 2.5rem 1.25rem 1.25rem;
  }

  .layout {
    flex-direction: column;
    padding: 0 1.5rem 3rem;
    gap: 0;
  }

  .paper-stack {
    flex: none;
    position: sticky;
    top: 3.25rem;
    height: calc(100dvh - 6.5rem);
    min-height: 0;
  }

  .paper-sheet--top {
    max-height: 100%;
    height: 100%;
    padding: 1.75rem 1.4rem 3rem;
    overscroll-behavior: contain;
  }

  /* keep one sheet behind so it still reads as a stack */
  .paper-sheet--back {
    display: none;
  }

  .back-to-index {
    display: inline-block;
    background: none;
    border: none;
    font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
    font-style: italic;
    font-size: 0.85rem;
    color: var(--ink-soft);
    opacity: 0.65;
    padding: 0 0 1.25rem;
    cursor: pointer;
  }

  .margin-secret {
    display: none;
  }

  /* The index becomes a slide-out drawer over the darkened room,
     never the landing view. */
  .index-toggle {
    display: block;
    position: fixed;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 84px;
    background: rgba(255, 255, 255, 0.03);
    border: none;
    border-radius: 0 3px 3px 0;
    z-index: 9;
  }

  .story-index {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(78vw, 320px);
    max-height: none;
    padding: 4.5rem 1.5rem 2rem;
    background: linear-gradient(to right, rgba(5, 5, 6, 0.98), rgba(8, 8, 10, 0.95));
    transform: translateX(-102%);
    transition: transform 300ms cubic-bezier(0.22, 0.61, 0.36, 1);
    overflow-y: auto;
    z-index: 8;
    -webkit-mask-image: none;
    mask-image: none;
  }

  body.index-open .story-index {
    transform: translateX(0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .title-char {
    animation: none;
    opacity: 1;
    transform: none;
    filter: none;
  }

  .paper-sheet {
    transition: none;
  }

  .story-index {
    transition: none;
  }
}
