/* === NOVEL ENGINE STYLES === */

@import url('https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;500;600;700&family=Noto+Sans+JP:wght@400;700&display=swap');

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

:root {
  font-size: 16px; /* Base font, dynamic sizing moved to containers */
}

html, body {
  width: 100%; height: 100dvh;
  overflow: hidden !important;
  background: #000;
  font-family: 'Noto Sans', 'Noto Sans JP', sans-serif;
  position: fixed;
  inset: 0;
  margin: 0; padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* === GAME CONTAINER LETTERBOXING === */
#loading-screen, #main-menu, #game-container {
  position: absolute !important;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 100vw;
  height: 56.25vw; /* 16:9 ratio */
  max-height: 100dvh;
  max-width: 177.78dvh; /* 16:9 ratio */
  overflow: hidden !important;
  container-type: size;
  font-size: clamp(12px, 2.5cqmin, 24px);
  background: #000;
}

/* Prevent context menu and selection */
body { 
  touch-action: none; 
  -webkit-touch-callout: none; 
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

img {
  -webkit-user-drag: none;
  -khtml-user-drag: none;
  -moz-user-drag: none;
  -o-user-drag: none;
  user-drag: none;
  pointer-events: none; /* Images shouldn't intercept clicks unless they are buttons */
}

button, .btn, .choice-btn, .music-toggle {
  pointer-events: auto; /* Re-enable for interactive elements */
}

/* === LOADING SCREEN === */
#loading-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0a0a1a 0%, #1a0a2e 100%);
  z-index: 1000;
}

.loading-spinner {
  width: 48px; height: 48px;
  border: 3px solid rgba(255,255,255,0.1);
  border-top-color: #8a60ff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.loading-text {
  color: rgba(255,255,255,0.7);
  font-size: 14px;
  margin-top: 20px;
}

.loading-bar {
  width: 200px;
  height: 4px;
  background: rgba(255,255,255,0.1);
  border-radius: 2px;
  margin-top: 16px;
  overflow: hidden;
}

.loading-fill {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, #8a60ff, #00d4aa);
  border-radius: 2px;
  transition: width 0.3s ease;
}

/* === MAIN MENU === */
#main-menu {
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  background: #000;
}

#main-menu-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  filter: blur(8px) brightness(0.6);
  transform: scale(1.05); /* hide blurred edges */
  animation: bgPulse 15s ease-in-out infinite alternate;
}

@keyframes bgPulse {
  0% { transform: scale(1.05); }
  100% { transform: scale(1.15); }
}

.main-menu-content {
  position: relative;
  z-index: 2;
  text-align: center;
  background: rgba(10, 10, 26, 0.6);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  padding: 8% 10%;
  border-radius: 4cqmin;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 4cqmin 10cqmin rgba(0, 0, 0, 0.5);
  animation: slideInDown 0.8s cubic-bezier(0.2, 0.8, 0.2, 1);
  max-width: 90%;
  max-height: 90%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow: hidden !important;
}

@media (max-width: 768px) {
  .main-menu-content {
    padding: 30px 20px;
    padding-bottom: calc(30px + env(safe-area-inset-bottom));
    width: 95vw;
    border-radius: 20px;
  }
}

@keyframes slideInDown {
  from { opacity: 0; transform: translateY(-40px); }
  to { opacity: 1; transform: translateY(0); }
}

#main-title {
  font-size: 8cqmin;
  font-weight: 800;
  margin-bottom: 5cqmin;
  background: linear-gradient(135deg, #FFD700 0%, #ff8a00 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0.5cqmin 3cqmin rgba(255, 138, 0, 0.3);
  letter-spacing: -0.02em;
  line-height: 1.1;
}

@media (max-width: 768px) {
  #main-title {
    font-size: 1.8rem;
    margin-bottom: 20px;
  }
}

.main-menu-content .btn {
  background: linear-gradient(135deg, #8a60ff, #5c30ff);
  color: #fff;
  border: none;
  padding: 18px 48px;
  border-radius: 100px;
  font-size: 1.25rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 8px 32px rgba(138, 96, 255, 0.4);
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  width: fit-content;
  align-self: center;
}

@media (max-width: 768px) {
  .main-menu-content .btn {
    padding: 12px 32px;
    font-size: 1rem;
  }
}

.main-menu-content .btn:hover {
  transform: translateY(-4px) scale(1.02);
  box-shadow: 0 16px 48px rgba(138, 96, 255, 0.6);
}

/* === GAME CONTAINER === */
#game-container {
  z-index: 5;
}

/* === MUSIC TOGGLE === */
.music-toggle {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 48px;
  height: 48px;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
  z-index: 40;
  cursor: pointer;
  transition: all 0.3s ease;
  user-select: none;
}
.music-toggle:hover {
  background: rgba(138, 96, 255, 0.4);
  transform: scale(1.1);
}
.music-toggle.muted {
  opacity: 0.6;
}

/* === BACKGROUND LAYERS === */
.background-layer {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  opacity: 0;
  transition: opacity 0.8s ease;
  pointer-events: none;
}

.background-layer.active { opacity: 1; }

/* === CHARACTERS LAYER === */
#characters-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.character-sprite {
  position: absolute;
  bottom: 0;
  height: 85%;
  max-width: 35%;
  object-fit: contain;
  object-position: bottom center;
  transition: translate 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), 
              opacity 0.3s ease,
              filter 0.3s ease;
  image-rendering: auto;
}

/* Positions */
.character-sprite[data-pos="left"] { left: 15%; translate: -50% 0; }
.character-sprite[data-pos="center"] { left: 50%; translate: -50% 0; }
.character-sprite[data-pos="right"] { left: 85%; translate: -50% 0; }

/* Speaking / dimmed states */
.character-sprite.speaking { 
  opacity: 1; 
  filter: brightness(1.05);
  z-index: 2;
}
.character-sprite.dimmed { 
  opacity: 0.65; 
  filter: brightness(0.75);
  z-index: 1;
}

/* Mirroring / Facing logic handled independently by scale */
.character-sprite.is-mc { scale: -1 1; }
.character-sprite.mirrored { scale: -1 1; }
.character-sprite.is-mc.mirrored { scale: 1 1; }


/* Enter/exit animations */
.character-sprite.entering-left {
  animation: slideInLeft 0.45s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
.character-sprite.entering-right {
  animation: slideInRight 0.45s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
.character-sprite.entering-center {
  animation: fadeInScale 0.4s ease forwards;
}
.character-sprite.exiting {
  animation: fadeOutDown 0.3s ease forwards;
}

@keyframes slideInLeft {
  from { transform: translateX(-50vw); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
@keyframes slideInRight {
  from { transform: translateX(50vw); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
@keyframes fadeInScale {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes fadeOutDown {
  to { transform: translateY(10vh); opacity: 0; }
}

/* Pose crossfade removed to prevent flickering */

/* === DIALOGUE LAYER === */
#dialogue-layer {
  position: absolute;
  bottom: 4%;
  left: 5%;
  right: 5%;
  background: rgba(0, 0, 0, 0.78);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-radius: 2cqmin;
  padding: 3cqmin 4cqmin;
  min-height: 15%;
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 -0.5cqmin 4cqmin rgba(0, 0, 0, 0.3);
  z-index: 10;
}

@media (max-width: 768px) {
  #dialogue-layer {
    padding: 12px 16px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    min-height: 80px;
    bottom: 5px;
    border-radius: 12px;
    overflow: hidden !important;
  }
}

#speaker-name {
  font-size: 1.05em;
  font-weight: 700;
  color: #FFD700;
  margin-bottom: 8px;
  text-shadow: 0 0 12px rgba(255, 215, 0, 0.3);
  min-height: 1.3em;
  overflow: hidden !important;
}

#dialogue-text {
  font-size: 0.95em;
  color: #f0f0f0;
  line-height: 1.7;
  min-height: 2.5em;
  overflow: hidden !important;
  transition: all 0.3s ease;
}

#dialogue-layer.narrative #speaker-name {
  display: none;
}

#dialogue-layer.narrative #dialogue-text {
  font-style: italic;
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
}

@media (max-width: 768px) {
  #speaker-name {
    font-size: 0.9em;
    margin-bottom: 4px;
  }
  #dialogue-text {
    font-size: 0.8em;
    line-height: 1.4;
  }
}

/* Typewriter cursor */
#dialogue-text::after {
  content: '';
  display: inline-block;
  width: 2px;
  height: 1em;
  background: rgba(255,255,255,0.8);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 0.7s step-end infinite;
}
#dialogue-text.complete::after { display: none; }

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

#click-indicator {
  position: absolute;
  bottom: 10px;
  right: 20px;
  color: rgba(255,255,255,0.4);
  font-size: 12px;
  animation: bounce 1.5s ease infinite;
}

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

/* === CHOICES === */
#choices-layer {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 20;
  padding: 20px;
}

@media (max-width: 768px) {
  #choices-layer {
    gap: 8px;
    padding: 10px;
  }
}

.choice-btn {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  padding: 16px 40px;
  border-radius: 14px;
  font-family: inherit;
  font-size: 1em;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  max-width: 80%;
  text-align: center;
  animation: choiceAppear 0.3s ease backwards;
}

@media (max-width: 768px) {
  .choice-btn {
    padding: 10px 20px;
    font-size: 0.85em;
    max-width: 95%;
    border-radius: 10px;
  }
}

.choice-btn:nth-child(1) { animation-delay: 0.05s; }
.choice-btn:nth-child(2) { animation-delay: 0.1s; }
.choice-btn:nth-child(3) { animation-delay: 0.15s; }
.choice-btn:nth-child(4) { animation-delay: 0.2s; }

@keyframes choiceAppear {
  from { opacity: 0; transform: scale(0.9) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.choice-btn:hover {
  background: rgba(138, 96, 255, 0.3);
  border-color: rgba(138, 96, 255, 0.5);
  transform: scale(1.03);
  box-shadow: 0 4px 24px rgba(138, 96, 255, 0.2);
}
.choice-btn:active { transform: scale(0.98); }

/* === ENDING SCREEN === */
#ending-screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(20px);
  z-index: 30;
  animation: fadeIn 1s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

#ending-title {
  font-size: 1.5em;
  font-weight: 700;
  color: #fff;
  text-align: center;
  margin-bottom: 16px;
  max-width: 80%;
}

#ending-badge {
  font-size: 2.5em;
  font-weight: 800;
  background: linear-gradient(135deg, #8a60ff, #00d4aa, #FFD700);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 4px;
  margin-bottom: 40px;
}

#restart-btn {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  padding: 14px 36px;
  border-radius: 12px;
  font-family: inherit;
  font-size: 1em;
  cursor: pointer;
  transition: all 0.2s ease;
}
#restart-btn:hover {
  background: rgba(138, 96, 255, 0.3);
  transform: scale(1.05);
}

/* === RESPONSIVE === */
@media (max-width: 640px) {
  .character-sprite { height: 65vh; max-width: 45vw; }
  #dialogue-layer { 
    bottom: 1vh; left: 1.5vw; right: 1.5vw; 
    padding: 14px 18px; border-radius: 12px;
    min-height: 90px;
  }
  #speaker-name { font-size: 0.9em; }
  #dialogue-text { font-size: 0.85em; }
  .choice-btn { padding: 12px 24px; font-size: 0.9em; }
  #ending-badge { font-size: 1.8em; }
}

@media (max-height: 500px) {
  .character-sprite { height: 75vh; }
  #dialogue-layer { min-height: 70px; padding: 10px 16px; }
}

/* === MINIGAME & INVENTORY === */
#minigame-layer {
  position: absolute;
  inset: 0;
  z-index: 15;
}

.minigame-item {
  position: absolute;
  width: 100px;
  height: 100px;
  object-fit: contain;
  cursor: pointer;
  filter: drop-shadow(0 4px 6px rgba(0,0,0,0.5)) brightness(0.85);
  transition: transform 0.2s, filter 0.2s, opacity 0.4s;
  pointer-events: auto; /* CRITICAL: enable clicking on item images */
}

.minigame-item:hover {
  transform: scale(1.1);
  filter: drop-shadow(0 0 12px rgba(255,255,255,0.4)) brightness(1.1);
}

.minigame-item.collected {
  opacity: 0;
  transform: scale(1.5);
  pointer-events: none;
}

/* Timer */
.minigame-timer-container {
  position: absolute;
  top: 15%;
  left: 50%;
  transform: translateX(-50%);
  width: 40%;
  height: 8px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 100px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.2);
  z-index: 20;
}

.minigame-timer-fill {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #00d4aa, #ff8a00, #ff4444);
  border-radius: 100px;
  transition: width 0.1s linear;
}

/* Rapid Click */
.rapid-bar-wrap {
  width: 60%;
  height: 24px;
  background: rgba(0, 0, 0, 0.6);
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5), 0 0 15px rgba(255, 138, 0, 0.3);
  pointer-events: none; /* Let clicks pass through to zone */
  animation: pulseBar 2s infinite alternate;
}

@keyframes pulseBar {
  from { transform: scale(1); box-shadow: 0 4px 12px rgba(0,0,0,0.5), 0 0 15px rgba(255, 138, 0, 0.3); }
  to { transform: scale(1.02); box-shadow: 0 4px 12px rgba(0,0,0,0.5), 0 0 25px rgba(255, 138, 0, 0.6); }
}

.rapid-bar-fill {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, #ff4444, #ff8a00);
  transition: width 0.1s linear, background 0.3s ease;
  position: relative;
  z-index: 1;
}

.rapid-bar-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 0.9em;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 1), 0 0 5px rgba(0, 0, 0, 0.8);
  z-index: 2; /* always above fill */
}

.rapid-pulse {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.6);
  transform: translate(-50%, -50%) scale(0);
  pointer-events: none;
  opacity: 0;
}

.rapid-pulse.pop {
  animation: rapidPop 0.3s ease-out;
}

@keyframes rapidPop {
  0% { transform: translate(-50%, -50%) scale(0); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(2); opacity: 0; }
}

.challenge-container {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  z-index: 20;
}

.challenge-title {
  color: #fff;
  font-size: 1.8em;
  font-weight: 700;
  text-align: center;
  margin-bottom: 20px;
  text-shadow: 0 4px 12px rgba(0,0,0,0.5);
}

#inventory-ui {
  position: absolute;
  top: 20px;
  right: -300px;
  width: 250px;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 16px;
  z-index: 25;
  transition: right 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: -4px 4px 24px rgba(0,0,0,0.4);
}

#inventory-ui.open {
  right: 20px;
}

.inventory-title {
  color: #FFD700;
  font-weight: 700;
  margin-bottom: 12px;
  font-size: 1.1em;
  border-bottom: 1px solid rgba(255,255,255,0.2);
  padding-bottom: 8px;
}

.inventory-item-ui {
  display: flex;
  align-items: center;
  gap: 10px;
  color: rgba(255,255,255,0.8);
  margin-bottom: 8px;
  transition: color 0.3s;
}

.inventory-item-ui.checked {
  color: #00d4aa;
  text-decoration: line-through;
  opacity: 0.7;
  animation: itemCheckPulse 0.4s ease;
}

@keyframes itemCheckPulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); color: #fff; }
  100% { transform: scale(1); }
}

.inv-checkbox {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,0.4);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.inventory-item-ui.checked .inv-checkbox::after {
  content: '✓';
  color: #00d4aa;
  font-weight: bold;
}

/* === IMPACT UI === */
#impact-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 35;
  display: flex;
  align-items: center;
  justify-content: center;
}

.impact-text {
  position: absolute;
  font-size: 2em;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 4px 12px rgba(0,0,0,0.8), 0 0 8px currentColor;
  animation: floatUp 2s cubic-bezier(0.1, 0.8, 0.3, 1) forwards;
}

@keyframes floatUp {
  0% { opacity: 0; transform: translate(var(--rx, 0px), calc(var(--offset, 0px) + 20px)) scale(0.8); }
  20% { opacity: 1; transform: translate(var(--rx, 0px), var(--offset, 0px)) scale(1.1); }
  80% { opacity: 1; transform: translate(var(--rx, 0px), calc(var(--offset, 0px) - 40px)) scale(1); }
  100% { opacity: 0; transform: translate(var(--rx, 0px), calc(var(--offset, 0px) - 60px)) scale(0.9); }
}

