/* CouchWand time machine: arrival cover, peel, trip overlay and HUD, CSS fallback. Owner: Agent C.
   The only place cream1958 (#EFE0BD) and sepia1958 (#A8763E) appear. */

/* ------------------------------------------------------------------------------------------------
   1. Arrival cover. html.arriving is added by the inline head script when sessionStorage
   'cw-arrive' is '1958', so this full-screen cream frame paints before any module runs. The time
   machine removes the class when its own overlay takes over. Failsafe: if no script ever removes
   it, the cover fades away by itself after 6 s, so the page is never left covered.
   ------------------------------------------------------------------------------------------------ */
html.arriving::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 2147483647;
  background: #EFE0BD;
  pointer-events: auto;
  animation: tm-cover-failsafe 0.3s linear 6s forwards;
}

@keyframes tm-cover-failsafe {
  to { opacity: 0; visibility: hidden; }
}

/* ------------------------------------------------------------------------------------------------
   2. The peel (bottom-right page corner). Geometry comes from custom properties: JS overrides them
   inline while dragging; at rest they derive from --tm-rest, the hover lift and the breathing.
   Hit testing is limited to the two clipped triangles (the anchor itself has pointer-events: none),
   so the peel never blocks other controls.
   ------------------------------------------------------------------------------------------------ */
@property --tm-breath {
  syntax: "<number>";
  inherits: true;
  initial-value: 0;
}

@property --tm-lift {
  syntax: "<length>";
  inherits: true;
  initial-value: 0px;
}

.tm-peel {
  --tm-rest: 56px;
  --tm-reach: min(80vmin, 680px);
  --tm-s: calc(var(--tm-rest) + var(--tm-lift) + var(--tm-breath) * 5px);
  --tm-a: var(--tm-s);
  --tm-b: var(--tm-s);
  --tm-pu: var(--tm-s);
  --tm-pv: var(--tm-s);
  --tm-d: calc(var(--tm-s) * 0.70711);
  --tm-ang: -45deg;
  --tm-stamp: 0.22;

  position: fixed;
  right: 0;
  bottom: 0;
  z-index: 80;
  display: block;
  width: calc(var(--tm-rest) + 16px);
  height: calc(var(--tm-rest) + 16px);
  pointer-events: none;
  color: inherit;
  text-decoration: none;
  outline: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  animation: tm-breathe 4.2s ease-in-out infinite;
  transition: --tm-lift 0.35s cubic-bezier(0.2, 0.8, 0.2, 1);
}

@keyframes tm-breathe {
  0%, 100% { --tm-breath: 0; }
  50% { --tm-breath: 1; }
}

.tm-peel:hover,
.tm-peel:focus-visible {
  --tm-lift: 12px;
}

.tm-peel.is-dragging,
.tm-peel.is-committed {
  animation: none;
}

/* Focus ring: the standard 2px ion ring, drawn inside the viewport around the corner (a ring offset
   outward would fall off-screen at the right and bottom edges). */
.tm-peel:focus,
.tm-peel:focus-visible {
  outline: none;
}

.tm-peel:focus-visible::after {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 3px;
  outline: 2px solid #E3A92F;
  outline-offset: 0;
  pointer-events: none;
}

.tm-peel__under,
.tm-peel__flapwrap,
.tm-peel__flap {
  position: absolute;
  right: 0;
  bottom: 0;
  display: block;
  width: var(--tm-reach);
  height: var(--tm-reach);
}

/* The old page showing through: the 1958 card, or cream with burst rays if the image fails. */
.tm-peel__under {
  overflow: hidden;
  pointer-events: auto;
  touch-action: none;
  cursor: grab;
  clip-path: polygon(100% 100%, calc(100% - var(--tm-a)) 100%, 100% calc(100% - var(--tm-b)));
  background-color: #EFE0BD;
  background-image: repeating-conic-gradient(from 0deg at 100% 100%,
    rgba(227, 169, 47, 0.30) 0deg 3deg,
    rgba(239, 224, 189, 0) 3deg 7deg,
    rgba(168, 118, 62, 0.2) 7deg 10deg,
    rgba(239, 224, 189, 0) 10deg 14deg);
}

.tm-peel__under img {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 100% 100%;
  pointer-events: none;
}

/* The lifted flap shades the old page near the fold. */
.tm-peel__under::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(var(--tm-ang),
    rgba(0, 0, 0, 0) 0,
    rgba(0, 0, 0, 0) calc(var(--tm-d) * 0.45),
    rgba(52, 30, 10, 0.34) calc(var(--tm-d) * 0.94),
    rgba(20, 10, 2, 0.62) var(--tm-d));
}

/* The flap casts its shadow onto the page (drop-shadow follows the clipped triangle). */
.tm-peel__flapwrap {
  pointer-events: none;
  filter: drop-shadow(-4px -3px 6px rgba(10, 4, 9, 0.6)) drop-shadow(-1px -1px 1px rgba(10, 4, 9, 0.45)) drop-shadow(0 0 5px rgba(227, 169, 47, 0.38));
}

/* The back of the curled page: cream paper, a rolled highlight at the fold, darker toward the tip. */
.tm-peel__flap {
  pointer-events: auto;
  touch-action: none;
  cursor: grab;
  clip-path: polygon(calc(100% - var(--tm-a)) 100%, 100% calc(100% - var(--tm-b)), calc(100% - var(--tm-pu)) calc(100% - var(--tm-pv)));
  background:
    linear-gradient(var(--tm-ang),
      #b89a68 0,
      #c9ad7c calc(var(--tm-d) * 0.985),
      #fff9e8 calc(var(--tm-d) * 1.03),
      #f7ebd0 calc(var(--tm-d) * 1.12),
      #EFE0BD calc(var(--tm-d) * 1.38),
      #e3cfa0 calc(var(--tm-d) * 1.75),
      #ccb27f calc(var(--tm-d) * 2));
}

.tm-peel.is-dragging .tm-peel__under,
.tm-peel.is-dragging .tm-peel__flap {
  cursor: grabbing;
}

.tm-peel__stamp {
  position: absolute;
  right: calc((var(--tm-a) + var(--tm-pu)) / 3);
  bottom: calc((var(--tm-b) + var(--tm-pv)) / 3);
  transform: translate(50%, 50%) rotate(var(--tm-ang)) scale(var(--tm-stamp));
  padding: 3px 7px 2px;
  border: 1.5px solid currentColor;
  border-radius: 2px;
  color: #EC7A42;
  font: 700 10px/1.25 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  white-space: nowrap;
  opacity: 0.9;
  pointer-events: none;
}

@media (max-width: 640px) {
  .tm-peel { --tm-rest: 36px; }
}

@media (prefers-reduced-motion: reduce) {
  .tm-peel { animation: none; transition: none; }
}

html.calm .tm-peel {
  animation: none;
  transition: none;
}

/* ------------------------------------------------------------------------------------------------
   3. The trip overlay (created only while a trip runs). The canvas is the tunnel's own renderer.
   ------------------------------------------------------------------------------------------------ */
.tm-overlay {
  --tm-ink: #E3A92F;
  --tm-odo-ink: #E3A92F;
  --tm-halo: rgba(30, 14, 27, 0.6);
  --tm-odo: 0;
  --tm-hud: 0;
  --tm-cap: 0;
  position: fixed;
  inset: 0;
  z-index: 2147483646;
  overflow: hidden;
  contain: strict;
  background: transparent;
  opacity: 1;
  cursor: default;
  touch-action: none;
  overscroll-behavior: contain;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

.tm-overlay[data-dir="forward"] {
  --tm-ink: #EFE0BD;
  --tm-odo-ink: #2E2216;
  --tm-halo: rgba(239, 224, 189, 0.8);
  --tm-odo: 0;
  background: #EFE0BD;
}

.tm-overlay.is-out {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

/* Back trip end frame: full cream, then navigation. */
.tm-overlay.is-cream {
  background: #EFE0BD;
  opacity: 1;
}

.tm-overlay.is-cream > * {
  visibility: hidden;
}

.tm-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

.tm-hud {
  position: absolute;
  inset: 0;
  color: var(--tm-ink);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  pointer-events: none;
  will-change: transform;
}

.tm-label {
  position: absolute;
  top: calc(18px + env(safe-area-inset-top, 0px));
  max-width: 44vw;
  margin: 0;
  font-size: 12px;
  line-height: 1.35;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  text-shadow: 0 0 10px rgba(20, 8, 18, 0.65);
  opacity: var(--tm-hud);
}

.tm-label--dir {
  left: calc(20px + env(safe-area-inset-left, 0px));
  font-size: 13px;
  font-weight: 700;
}

.tm-label--dest {
  right: calc(20px + env(safe-area-inset-right, 0px));
  text-align: right;
}

.tm-overlay[data-dir="forward"] .tm-label--dir {
  left: auto;
  right: calc(20px + env(safe-area-inset-right, 0px));
  text-align: right;
}

.tm-overlay[data-dir="forward"] .tm-label--dest {
  right: auto;
  left: calc(20px + env(safe-area-inset-left, 0px));
  text-align: left;
}

.tm-label__arrows {
  display: inline-block;
  letter-spacing: -0.12em;
  animation: tm-blink 0.5s steps(1, end) infinite;
}

@keyframes tm-blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0.25; }
}

@media (max-width: 560px) {
  /* stack both labels on the side of travel */
  .tm-label { max-width: calc(100vw - 40px); }
  .tm-label--dest {
    top: calc(40px + env(safe-area-inset-top, 0px));
    right: auto;
    left: calc(20px + env(safe-area-inset-left, 0px));
    text-align: left;
  }
  .tm-overlay[data-dir="forward"] .tm-label--dest {
    left: auto;
    right: calc(20px + env(safe-area-inset-right, 0px));
    text-align: right;
  }
}

/* The year odometer: four rolling drums, then (forward) a split-flap flip to TOMORROW. */
.tm-odo {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: clamp(72px, 17vmin, 200px);
  font-weight: 800;
  line-height: 1;
  perspective: 600px;
  opacity: var(--tm-odo, 1);
  color: var(--tm-odo-ink);
  filter: drop-shadow(0 0 3px var(--tm-halo)) drop-shadow(0 0 16px var(--tm-halo));
}

.tm-odo__digits {
  display: flex;
  transform-origin: 50% 50%;
  transition: transform 0.18s ease-in, opacity 0.18s ease-in;
}

.tm-odo__col {
  position: relative;
  display: block;
  width: 0.64em;
  height: 1.5em;
  overflow: hidden;
  text-align: center;
  -webkit-mask-image: linear-gradient(transparent, #000 30%, #000 70%, transparent);
  mask-image: linear-gradient(transparent, #000 30%, #000 70%, transparent);
}

.tm-odo__strip {
  display: block;
  will-change: transform;
}

.tm-odo__strip > span {
  display: block;
  height: 1em;
  line-height: 1em;
}

.tm-odo__word {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.5em;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.tm-odo__word > span {
  display: inline-block;
  opacity: 0;
  transform: rotateX(-95deg);
  transform-origin: 50% 50%;
}

.tm-odo.is-word .tm-odo__digits {
  opacity: 0;
  transform: rotateX(90deg);
}

.tm-odo.is-word .tm-odo__word > span {
  opacity: 1;
  transform: none;
  transition:
    transform 0.26s cubic-bezier(0.2, 1.4, 0.4, 1) calc(var(--i) * 28ms),
    opacity 0.08s linear calc(var(--i) * 28ms);
}

/* Forward, while the tunnel is still loading under the cream frame: a quiet leader flicker. */
.tm-wait {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

.tm-overlay.is-waiting .tm-hud {
  visibility: hidden;
}

.tm-overlay.is-waiting .tm-wait {
  opacity: 1;
  animation: tm-wait-flicker 0.42s steps(1, end) infinite;
}

.tm-wait__ring {
  position: absolute;
  width: min(46vmin, 380px);
  height: min(46vmin, 380px);
  border: 2px solid rgba(60, 38, 16, 0.3);
  border-radius: 50%;
  box-shadow: inset 0 0 0 calc(min(46vmin, 380px) * 0.09) rgba(60, 38, 16, 0), 0 0 0 1px rgba(60, 38, 16, 0.08);
}

.tm-wait__ring::before,
.tm-wait__ring::after {
  content: "";
  position: absolute;
  background: rgba(60, 38, 16, 0.22);
}

.tm-wait__ring::before { left: 50%; top: -30%; bottom: -30%; width: 1px; }
.tm-wait__ring::after { top: 50%; left: -30%; right: -30%; height: 1px; }

.tm-wait__n {
  position: relative;
  color: rgba(60, 38, 16, 0.4);
  font: 800 clamp(48px, 11vmin, 120px)/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.02em;
}

@keyframes tm-wait-flicker {
  0% { opacity: 1; transform: translateY(0); }
  30% { opacity: 0.82; transform: translateY(-1px); }
  55% { opacity: 1; transform: translateY(1px); }
  80% { opacity: 0.9; transform: translateY(0); }
}

/* Film-leader countdown marks (back trip: 3, 2). */
.tm-leader {
  --tm-leader-ink: rgba(28, 16, 6, 0.55);
  position: absolute;
  left: 50%;
  top: 50%;
  width: min(70vmin, 560px);
  height: min(70vmin, 560px);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  background: conic-gradient(from 0deg, rgba(28, 16, 6, 0.4) 0deg var(--tm-sweep, 0deg), rgba(28, 16, 6, 0) var(--tm-sweep, 0deg) 360deg);
  box-shadow: inset 0 0 0 3px var(--tm-leader-ink);
}

.tm-leader.is-on {
  opacity: 1;
}

.tm-leader::before {
  content: "";
  position: absolute;
  inset: 9%;
  border: 3px solid var(--tm-leader-ink);
  border-radius: 50%;
}

.tm-leader::after {
  content: "";
  position: absolute;
  inset: -40vmax;
  background:
    linear-gradient(var(--tm-leader-ink), var(--tm-leader-ink)) 50% 0 / 2px 100% no-repeat,
    linear-gradient(var(--tm-leader-ink), var(--tm-leader-ink)) 0 50% / 100% 2px no-repeat;
}

.tm-leader__n {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--tm-leader-ink);
  font: 800 min(44vmin, 350px)/1 system-ui, -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}

/* The caption: the one place a serif italic is allowed. */
.tm-caption {
  position: absolute;
  left: 50%;
  bottom: calc(26px + env(safe-area-inset-bottom, 0px));
  width: min(90vw, 560px);
  margin: 0;
  transform: translateX(-50%);
  color: var(--tm-odo-ink);
  font: italic 400 clamp(15px, 1.7vw, 19px)/1.4 Georgia, "Times New Roman", Times, serif;
  text-align: center;
  text-shadow: 0 0 2px var(--tm-halo), 0 0 10px var(--tm-halo);
  opacity: var(--tm-cap);
}

.tm-caption__by {
  display: block;
  margin-top: 0.3em;
  font-size: 0.82em;
  letter-spacing: 0.04em;
  opacity: 0.8;
}

/* ------------------------------------------------------------------------------------------------
   4. CSS fallback trip (the trip's own WebGL context could not be created). JS drives the wipes by
   setting clip-path insets every frame.
   ------------------------------------------------------------------------------------------------ */
.tm-overlay.tm-overlay--css {
  background: transparent;
}

.tm-css-night,
.tm-css-cream {
  position: absolute;
  inset: 0;
}

.tm-css-night {
  clip-path: inset(0 0 0 100%);
  background:
    radial-gradient(ellipse 60% 12% at 50% 50%, rgba(227, 169, 47, 0.24), rgba(227, 169, 47, 0) 70%),
    radial-gradient(ellipse 90% 70% at 50% 50%, rgba(75, 35, 66, 0.7), rgba(75, 35, 66, 0) 75%),
    linear-gradient(to bottom, transparent calc(50% - 1px), #E3A92F calc(50% - 1px), #E3A92F calc(50% + 1px), transparent calc(50% + 1px)),
    #1E0E1B;
}

.tm-css-cream {
  clip-path: inset(0 0 0 100%);
  background: #EFE0BD;
}

.tm-overlay--css[data-dir="forward"] .tm-css-night,
.tm-overlay--css[data-dir="forward"] .tm-css-cream {
  clip-path: inset(0 0 0 0);
}

/* ------------------------------------------------------------------------------------------------
   5. Utilities.
   ------------------------------------------------------------------------------------------------ */
.tm-sr {
  position: absolute !important;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

@media print {
  .tm-peel,
  .tm-overlay {
    display: none !important;
  }
}

@media (forced-colors: active) {
  .tm-peel__flap { forced-color-adjust: none; }
}
