/* =============================================
   RSW — Roleplay Start Widget
   Fully isolated CSS — scoped to #rsw-root
   ============================================= */

/* --- Reset & Base --- */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --- Root Container --- */
#rsw-root {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
}

/* --- Click Ripple Effect --- */
.rsw-ripple {
  position: absolute;
  z-index: 60;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(167, 139, 250, 0.35) 0%, transparent 70%);
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0);
  animation: rsw-ripple-expand 0.5s ease-out forwards;
}

@keyframes rsw-ripple-expand {
  to { transform: translate(-50%, -50%) scale(3); opacity: 0; }
}

/* --- Loading Screen --- */
#rsw-loader {
  position: absolute;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
  transition: opacity 0.8s ease, visibility 0.8s ease;
}

#rsw-loader.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.rsw-loader-spinner {
  width: clamp(28px, 6vw, 48px);
  height: clamp(28px, 6vw, 48px);
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-top-color: #a78bfa;
  border-right-color: #c084fc;
  border-radius: 50%;
  animation: rsw-spin 0.7s linear infinite;
}

.rsw-loader-text {
  margin-top: clamp(8px, 2vw, 16px);
  color: rgba(255, 255, 255, 0.6);
  font-size: clamp(10px, 2.2vw, 14px);
  font-weight: 500;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.rsw-loader-bar-track {
  margin-top: clamp(6px, 1.5vw, 12px);
  width: clamp(100px, 40vw, 200px);
  height: 4px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 2px;
  overflow: hidden;
  position: relative;
}

.rsw-loader-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #a78bfa, #c084fc, #e879f9);
  border-radius: 2px;
  transition: width 0.3s ease;
  position: relative;
}

/* Shimmer on loading bar */
.rsw-loader-bar-fill::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  animation: rsw-shimmer 1.2s ease-in-out infinite;
}

@keyframes rsw-shimmer {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

@keyframes rsw-spin {
  to { transform: rotate(360deg); }
}

/* --- Scene Layer (background) --- */
#rsw-scene {
  position: absolute;
  inset: 0;
  z-index: 1;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  transition: opacity 0.6s ease;
  animation: rsw-scene-breathe 12s ease-in-out infinite alternate;
}

/* Subtle slow scale to make the background feel alive */
@keyframes rsw-scene-breathe {
  from { transform: scale(1.0); }
  to   { transform: scale(1.03); }
}

/* Vignette + gradient overlay for cinematic depth */
#rsw-scene::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at center, transparent 40%, rgba(0,0,0,0.5) 100%),
    linear-gradient(to top, rgba(0,0,0,0.35) 0%, transparent 40%);
  pointer-events: none;
}

/* --- Sprite Layer --- */
#rsw-sprites {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: none;
}

.rsw-sprite {
  position: absolute;
  bottom: 0;
  height: 95vh;
  max-height: 95%;
  max-width: 45%;
  object-fit: contain;
  object-position: bottom;
  transition: opacity 0.4s ease, transform 0.4s ease, filter 0.4s ease;
  filter: drop-shadow(0 4px 30px rgba(0, 0, 0, 0.6));
  image-rendering: auto;
}

.rsw-sprite.rsw-sprite-left {
  left: 2%;
}

.rsw-sprite.rsw-sprite-right {
  right: 2%;
}

/* Entrance animations */
.rsw-sprite.rsw-entering-left {
  animation: rsw-slide-in-left 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.rsw-sprite.rsw-entering-right {
  animation: rsw-slide-in-right 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

/* Dim non-speaking sprite */
.rsw-sprite.rsw-inactive {
  filter: brightness(0.45) saturate(0.6) drop-shadow(0 4px 20px rgba(0, 0, 0, 0.5));
  transform: scale(0.97);
}

.rsw-sprite.rsw-active {
  filter: brightness(1.05) saturate(1.1) drop-shadow(0 4px 30px rgba(0, 0, 0, 0.6));
  transform: scale(1);
}

@keyframes rsw-slide-in-left {
  from { transform: translateX(-50%) scale(0.9); opacity: 0; }
  to   { transform: translateX(0) scale(1);      opacity: 1; }
}

@keyframes rsw-slide-in-right {
  from { transform: translateX(50%) scale(0.9); opacity: 0; }
  to   { transform: translateX(0) scale(1);     opacity: 1; }
}

/* --- Dialogue Layer --- */
#rsw-dialogue {
  position: absolute;
  bottom: clamp(4px, 2vh, 20px);
  left: clamp(4px, 2vw, 20px);
  right: clamp(4px, 2vw, 20px);
  z-index: 20;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.35s ease, transform 0.35s cubic-bezier(0.16, 1, 0.3, 1);
  pointer-events: none;
}

#rsw-dialogue.rsw-visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.rsw-dialogue-box {
  position: relative;
  background: rgba(8, 8, 18, 0.9);
  backdrop-filter: blur(16px) saturate(1.3);
  -webkit-backdrop-filter: blur(16px) saturate(1.3);
  border: 1px solid rgba(167, 139, 250, 0.2);
  border-radius: clamp(6px, 1.5vw, 14px);
  padding: clamp(8px, 2vw, 20px);
  padding-top: clamp(6px, 1.5vw, 16px);
  box-shadow:
    0 8px 40px rgba(0, 0, 0, 0.5),
    0 0 1px rgba(167, 139, 250, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

/* Ambient glow on the dialogue box bottom edge */
.rsw-dialogue-box::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 20%;
  right: 20%;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(167, 139, 250, 0.4), transparent);
}

.rsw-speaker-name {
  display: inline-block;
  font-size: clamp(8px, 1.8vw, 13px);
  font-weight: 700;
  color: #c4b5fd;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: clamp(2px, 0.6vw, 6px);
  padding: 2px 8px;
  background: linear-gradient(135deg, rgba(167, 139, 250, 0.15), rgba(192, 132, 252, 0.1));
  border-radius: 4px;
  border: 1px solid rgba(167, 139, 250, 0.1);
}

.rsw-dialogue-text {
  font-size: clamp(10px, 2.4vw, 17px);
  font-weight: 400;
  line-height: 1.55;
  color: rgba(255, 255, 255, 0.93);
  min-height: clamp(14px, 3.5vw, 28px);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* Blinking cursor during typing */
.rsw-dialogue-text .rsw-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: #c4b5fd;
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: rsw-blink 0.55s step-end infinite;
  border-radius: 1px;
}

@keyframes rsw-blink {
  50% { opacity: 0; }
}

/* --- Next Indicator (bouncing arrow) --- */
.rsw-next-indicator {
  position: absolute;
  bottom: clamp(4px, 1vw, 10px);
  right: clamp(8px, 1.5vw, 16px);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.rsw-next-indicator.rsw-visible {
  opacity: 1;
}

.rsw-next-indicator-arrow {
  display: block;
  width: 0;
  height: 0;
  border-left: clamp(4px, 0.8vw, 7px) solid transparent;
  border-right: clamp(4px, 0.8vw, 7px) solid transparent;
  border-top: clamp(5px, 1vw, 9px) solid #c4b5fd;
  animation: rsw-bounce 0.8s ease-in-out infinite;
  filter: drop-shadow(0 0 4px rgba(167, 139, 250, 0.4));
}

@keyframes rsw-bounce {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(4px); }
}

/* --- Title Overlay --- */
#rsw-title {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, rgba(0,0,0,0.2) 0%, rgba(0,0,0,0.65) 100%);
  transition: opacity 0.8s ease, visibility 0.8s ease;
}

#rsw-title.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

/* Staggered cinematic fade-in */
#rsw-title .rsw-title-label {
  animation: rsw-fade-up 1s ease 0.2s both;
}
#rsw-title .rsw-title-subtitle {
  animation: rsw-fade-up 1s ease 0.5s both;
}
#rsw-title .rsw-title-prompt {
  animation: rsw-fade-up 1s ease 0.8s both;
}

@keyframes rsw-fade-up {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}

.rsw-title-label {
  font-size: clamp(14px, 5vw, 40px);
  font-weight: 700;
  color: #fff;
  text-align: center;
  text-shadow: 0 2px 20px rgba(0, 0, 0, 0.7), 0 0 40px rgba(167, 139, 250, 0.15);
  padding: 0 clamp(8px, 3vw, 24px);
  line-height: 1.3;
}

.rsw-title-subtitle {
  margin-top: clamp(4px, 1.2vw, 12px);
  font-size: clamp(9px, 2vw, 15px);
  font-weight: 500;
  color: rgba(255, 255, 255, 0.55);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.rsw-title-prompt {
  margin-top: clamp(12px, 3vw, 32px);
  font-size: clamp(8px, 1.8vw, 13px);
  font-weight: 500;
  color: rgba(255, 255, 255, 0.4);
  animation: rsw-pulse 2.2s ease-in-out infinite !important;
}

/* Divider line between title and prompt */
.rsw-title-divider {
  margin-top: clamp(8px, 2vw, 20px);
  width: clamp(30px, 12vw, 80px);
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(167, 139, 250, 0.5), transparent);
  animation: rsw-fade-up 1s ease 0.65s both;
}

@keyframes rsw-pulse {
  0%, 100% { opacity: 0.4; }
  50%      { opacity: 0.85; }
}

/* --- End Overlay --- */
#rsw-end {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0);
  opacity: 0;
  visibility: hidden;
  transition: opacity 1s ease, visibility 1s ease, background 1.2s ease;
  pointer-events: none;
}

#rsw-end.rsw-visible {
  opacity: 1;
  visibility: visible;
  background: rgba(0, 0, 0, 0.8);
  pointer-events: auto;
  cursor: default;
}

#rsw-end.rsw-visible .rsw-end-message {
  animation: rsw-end-reveal 1.2s cubic-bezier(0.16, 1, 0.3, 1) 0.3s both;
}

#rsw-end.rsw-visible .rsw-end-flair {
  animation: rsw-end-reveal 1s ease 0.8s both;
}

@keyframes rsw-end-reveal {
  from { opacity: 0; transform: translateY(20px) scale(0.95); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.rsw-end-message {
  font-size: clamp(12px, 3.2vw, 24px);
  font-weight: 600;
  color: rgba(255, 255, 255, 0.92);
  text-align: center;
  padding: 0 clamp(10px, 4vw, 28px);
  line-height: 1.55;
  text-shadow: 0 2px 16px rgba(0, 0, 0, 0.5);
  font-style: italic;
}

.rsw-end-flair {
  margin-top: clamp(8px, 2vw, 20px);
  width: clamp(30px, 12vw, 70px);
  height: 2px;
  background: linear-gradient(90deg, transparent, #a78bfa, #c084fc, transparent);
  border-radius: 1px;
}

/* --- Accessibility: Reduced Motion --- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
