/* ==========================================================================
   SHINOBU — CHAT MOTION LAYER
   Silent paper-stage motion for the in-app experience.
   Paper / ink / red only. No sound. No data. No stored history.
   Loaded last so it can refine the active paper.css design system.
   ========================================================================== */

/* ---------- 1. SCREEN ENTRIES: a page turns, a red thread pulls ---------- */

.screen.active {
  animation: paperStageIn 380ms var(--ease) both;
}

.screen.active::after {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  z-index: 301;
  pointer-events: none;
  background: var(--red-bright);
  transform: scaleX(0);
  transform-origin: 0 50%;
  animation: redThread 620ms var(--ease) both;
}

@keyframes paperStageIn {
  from { opacity: 0.45; transform: translate3d(0, 10px, 0); }
  to   { opacity: 1; transform: translate3d(0, 0, 0); }
}

@keyframes redThread {
  0%   { transform: scaleX(0); opacity: 1; }
  72%  { transform: scaleX(1); opacity: 1; }
  100% { transform: scaleX(1); opacity: 0; }
}

/* ---------- 2. THE ROOM BREATHES ---------- */

#screen-live-room .messages-container {
  position: relative;
  isolation: isolate;
}

#screen-live-room .messages-container::before {
  content: '';
  position: absolute;
  inset: -20%;
  z-index: 0;
  pointer-events: none;
  opacity: 0.5;
  background:
    radial-gradient(ellipse at 18% 10%, rgba(193, 18, 31, 0.055), transparent 46%),
    radial-gradient(ellipse at 82% 88%, rgba(10, 10, 10, 0.05), transparent 52%);
  animation: inkDrift 26s ease-in-out infinite alternate;
}

#screen-live-room .messages-container > * {
  position: relative;
  z-index: 1;
}

@keyframes inkDrift {
  from { transform: translate3d(-1.5%, -1%, 0) scale(1); }
  to   { transform: translate3d(1.5%, 1.5%, 0) scale(1.06); }
}

/* ---------- 3. MESSAGES: paper unfolds, ink presses ---------- */

.message.received {
  animation: paperUnfold 420ms var(--ease) both;
}

.message.sent {
  animation: inkPress 380ms var(--ease) both;
}

@keyframes paperUnfold {
  from { opacity: 0; transform: translate3d(-16px, 12px, 0) rotate(-1deg); }
  to   { opacity: 1; transform: translate3d(0, 0, 0) rotate(0); }
}

@keyframes inkPress {
  from { opacity: 0; transform: translate3d(14px, 8px, 0) scale(0.965); }
  to   { opacity: 1; transform: translate3d(0, 0, 0) scale(1); }
}

/* A dying message dissolves like ink instead of blinking out. */
.message-fading {
  opacity: 0;
  transform: translate3d(0, -10px, 0) scale(0.98);
  filter: blur(2px);
  transition: opacity 720ms ease, transform 720ms ease, filter 720ms ease;
}

/* ---------- 4. TYPING: a brush stroke in the margin ---------- */

#screen-live-room .presence-dot {
  position: relative;
}

#screen-live-room .presence-dot.typing {
  background: var(--red-bright);
  border-color: var(--red-bright);
  animation: shadowPulse 1.1s ease-in-out infinite;
}

#screen-live-room .presence-dot.typing::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 12px;
  width: 26px;
  height: 1px;
  background: var(--red-bright);
  transform-origin: 0 50%;
  animation: brushStroke 1.1s ease-in-out infinite;
}

@keyframes shadowPulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%      { opacity: 0.45; transform: scale(0.75); }
}

@keyframes brushStroke {
  0%   { transform: scaleX(0); opacity: 0; }
  45%  { transform: scaleX(1); opacity: 0.8; }
  100% { transform: scaleX(1); opacity: 0; }
}

/* ---------- 5. EXPIRY FUSE: the room visibly approaches its end ---------- */

#screen-live-room .expiry-timer {
  position: relative;
}

#screen-live-room .expiry-timer::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: -5px;
  height: 1px;
  background: linear-gradient(90deg, var(--red-bright), transparent);
  transform: scaleX(var(--fuse, 1));
  transform-origin: 0 50%;
  transition: transform 1s linear;
  pointer-events: none;
}

#screen-live-room.fuse-critical .expiry-time {
  animation: fuseCritical 1s ease-in-out infinite;
}

#screen-live-room.fuse-critical .expiry-timer::after {
  background: var(--red-bright);
}

@keyframes fuseCritical {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.35; }
}

/* ---------- 6. SAFETY RAIL: always legible, never buried ---------- */

.room-safety-rail {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 0.5rem;
  flex-shrink: 0;
  padding: 0.4rem clamp(16px, 3vw, 40px);
  background: var(--paper-deep);
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 0.5rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.rail-note {
  margin-right: auto;
  opacity: 0.8;
}

#screen-live-room .rail-capture {
  color: var(--red);
  font-weight: 700;
}

#screen-live-room .rail-capture.detected {
  padding: 0 3px;
  background: var(--red);
  color: var(--paper);
}

.rail-button {
  padding: 0.24rem 0.6rem;
  background: transparent;
  border: 1px solid var(--line-strong);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 0.5rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 180ms var(--ease), color 180ms var(--ease), border-color 180ms var(--ease);
}

.rail-button:hover,
.rail-button:focus-visible {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

.rail-report {
  border-color: var(--red);
  color: var(--red);
}

.rail-report:hover,
.rail-report:focus-visible {
  background: var(--red);
  border-color: var(--red);
  color: var(--paper);
}

/* ---------- 7. PAPER ERROR CARD: no more browser alerts ---------- */

.paper-error-card {
  position: fixed;
  inset: 0;
  z-index: 9500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(10, 10, 10, 0.62);
  opacity: 0;
  transition: opacity 240ms var(--ease);
}

.paper-error-card.visible {
  opacity: 1;
}

.paper-error-inner {
  width: min(420px, 100%);
  padding: 28px;
  background: var(--paper);
  border: 1px solid var(--ink);
  border-top: 4px solid var(--red);
  box-shadow: 12px 12px 0 rgba(10, 10, 10, 0.35);
  transform: translate3d(0, 14px, 0);
  transition: transform 320ms var(--ease);
}

.paper-error-card.visible .paper-error-inner {
  transform: translate3d(0, 0, 0);
}

.paper-error-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  margin-bottom: 14px;
  background: var(--red);
  color: var(--paper);
  font-family: var(--font-display);
  font-size: 0.9rem;
  line-height: 1;
}

.paper-error-text {
  margin: 0 0 22px;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 0.95rem;
  line-height: 1.6;
}

.paper-error-btn {
  padding: 0.55rem 1.1rem;
  background: var(--ink);
  border: 1px solid var(--ink);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 180ms var(--ease), color 180ms var(--ease);
}

.paper-error-btn:hover,
.paper-error-btn:focus-visible {
  background: var(--red);
  border-color: var(--red);
}

/* ---------- 8. IDENTITY PANELS: opened dossier files ---------- */

#screen-identity-home .id-panel {
  animation: dossierOpen 520ms var(--ease) both;
}

#screen-identity-home .id-panel:nth-child(2) { animation-delay: 90ms; }
#screen-identity-home .id-panel:nth-child(3) { animation-delay: 180ms; }

#screen-identity-home .id-panel-code .identity-code-value {
  position: relative;
  display: inline-block;
}

#screen-identity-home .id-panel-code .identity-code-value::after {
  content: 'FILED';
  position: absolute;
  right: -34px;
  top: 50%;
  padding: 2px 5px;
  border: 2px solid var(--red);
  color: var(--red);
  font-family: var(--font-mono);
  font-size: 0.42rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  transform: translateY(-50%) rotate(-9deg);
  opacity: 0.75;
  pointer-events: none;
}

@keyframes dossierOpen {
  from { opacity: 0; transform: translate3d(0, 18px, 0) rotateX(4deg); }
  to   { opacity: 1; transform: translate3d(0, 0, 0) rotateX(0); }
}

/* ---------- 9. REDUCED MOTION: calm, still, fully usable ---------- */

@media (prefers-reduced-motion: reduce) {
  .screen.active,
  .screen.active::after,
  #screen-live-room .messages-container::before,
  .message.received,
  .message.sent,
  #screen-live-room .presence-dot.typing,
  #screen-live-room .presence-dot.typing::after,
  #screen-live-room.fuse-critical .expiry-time,
  #screen-identity-home .id-panel {
    animation: none !important;
  }

  .screen.active::after { display: none; }

  .message-fading {
    filter: none;
    transition: opacity 200ms linear;
  }

  * {
    scroll-behavior: auto !important;
  }
}
