/* ============================================
   LEVEL 2 — CRYSTAL CAVE puzzle styles
   Styles the Balance Scale (#screen-balance-scale) and
   Crystal Lock (#screen-crystal-lock) screens used by
   level-2.js. (Filename is historical: the old
   #screen-gem-math mini-game it was named for has been
   removed; these are Level 2's actual puzzle screens.)
   ============================================ */

/* ============================================
   SCALE IN BACKGROUND — shown during dialogue
   ============================================ */

#scale-bg-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.6s ease;
}

#scale-bg-overlay.visible {
  opacity: 1;
}

.scale-bg-image {
  width: clamp(180px, 35vw, 300px);
  height: auto;
  image-rendering: pixelated;
  filter: drop-shadow(0 0 20px rgba(100, 200, 255, 0.4));
  animation: scale-bg-pulse 2s ease-in-out infinite;
  margin-bottom: 10%;
}

@keyframes scale-bg-pulse {
  0%, 100% { filter: drop-shadow(0 0 20px rgba(100, 200, 255, 0.4)); transform: scale(1); }
  50% { filter: drop-shadow(0 0 35px rgba(100, 200, 255, 0.7)); transform: scale(1.03); }
}

/* ============================================
   CRYSTAL LOCK REVEAL — dramatic close-up
   ============================================ */

#lock-reveal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.5s ease;
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.3) 0%, rgba(0, 0, 0, 0.7) 100%);
}

#lock-reveal-overlay.visible {
  opacity: 1;
  animation: lock-reveal-in 1.2s ease-out;
}

.lock-reveal-image {
  width: clamp(240px, 55vw, 500px);
  height: auto;
  image-rendering: pixelated;
  filter: drop-shadow(0 0 30px rgba(100, 180, 255, 0.5));
  animation: lock-shimmer 3s ease-in-out infinite;
}

@keyframes lock-reveal-in {
  0% { transform: scale(0.5); opacity: 0; filter: brightness(2); }
  60% { transform: scale(1.05); opacity: 1; }
  100% { transform: scale(1); opacity: 1; filter: brightness(1); }
}

@keyframes lock-shimmer {
  0%, 100% { filter: drop-shadow(0 0 30px rgba(100, 180, 255, 0.5)); }
  50% { filter: drop-shadow(0 0 45px rgba(150, 200, 255, 0.8)); }
}


/* ============================================
   BALANCE SCALE — Level 2 gem weighing
   ============================================ */

#screen-balance-scale .scene-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
}

.scale-content {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 0.5rem 1.5rem;
  gap: clamp(8px, 2vw, 16px);
}

/* Value reference display at top */
.scale-value-display {
  text-align: center;
  min-height: 1.5em;
}

.value-ref {
  font-family: var(--font-pixel);
  font-size: clamp(0.4rem, 1.2vw, 0.55rem);
  color: var(--gold-bright);
  background: rgba(12, 16, 22, 0.88);
  padding: 6px 16px;
  border: 2px solid #c5952e;
  box-shadow:
    inset 0 0 0 1px rgba(8, 10, 16, 0.8),
    inset 0 0 0 2px #8a6518,
    0 0 0 1px rgba(0, 0, 0, 0.6),
    0 0 6px rgba(160, 120, 40, 0.1);
  letter-spacing: 0.05em;
  display: inline-block;
}

/* Scale visual area: pans + scale image */
.scale-visual {
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  width: 100%;
  max-width: 600px;
  height: clamp(180px, 40vh, 300px);
}

.scale-image {
  width: clamp(240px, 50vw, 400px);
  height: auto;
  image-rendering: pixelated;
  filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.5));
  z-index: 2;
  transition: opacity 0.3s ease, transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Tilt states — applied via JS alongside image swap for smooth feel */
.scale-image.tilt-left {
  transform: rotate(-2deg);
}

.scale-image.tilt-right {
  transform: rotate(2deg);
}

.scale-image.tilt-balanced {
  transform: rotate(0deg);
  animation: scale-settle 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes scale-settle {
  0% { transform: rotate(-1deg); }
  30% { transform: rotate(0.5deg); }
  60% { transform: rotate(-0.3deg); }
  100% { transform: rotate(0deg); }
}

/* Gem pans positioned over the scale pans */
.scale-pan {
  position: absolute;
  bottom: clamp(45%, 48%, 52%);
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-end;
  gap: 4px;
  z-index: 3;
  min-width: 80px;
  padding: 4px;
}

.scale-pan-left {
  left: clamp(5%, 10%, 15%);
}

.scale-pan-right {
  right: clamp(5%, 10%, 15%);
}

/* Individual gems on the scale */
.scale-gem {
  width: clamp(28px, 6vw, 44px);
  height: auto;
  image-rendering: pixelated;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.4));
}

.scale-gem-pop {
  animation: gem-pop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes gem-pop {
  0% { transform: scale(0) rotate(-15deg); opacity: 0; }
  60% { transform: scale(1.2) rotate(5deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

.scale-gem-reject {
  animation: gem-reject 0.5s ease forwards;
}

@keyframes gem-reject {
  0% { transform: scale(1); opacity: 1; }
  30% { transform: scale(1.2); opacity: 1; filter: drop-shadow(0 0 8px rgba(255, 60, 60, 0.8)); }
  100% { transform: scale(0) translateY(-20px); opacity: 0; }
}

/* Add buttons below the scale */
.scale-add-buttons {
  display: flex;
  gap: clamp(12px, 3vw, 24px);
  justify-content: center;
  flex-wrap: wrap;
}

/* Crystal buttons — icy translucent keys carved from cave crystal.
   Press physics (translateY + scale + brightness) comes from the global
   .game-btn class; here we only transition colour/border/shadow. */
.scale-add-btn {
  font-family: var(--font-pixel);
  font-size: clamp(0.45rem, 1.3vw, 0.6rem);
  color: #eaf4ff;
  text-shadow: 0 1px 2px rgba(8, 16, 40, 0.8);
  background: linear-gradient(180deg, rgba(90, 138, 210, 0.92) 0%, rgba(42, 74, 138, 0.92) 100%);
  border: 2px solid #7fb2e8;
  padding: clamp(10px, 2.5vw, 16px) clamp(16px, 4vw, 28px);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    inset 0 0 0 1px rgba(10, 20, 48, 0.5),
    0 3px 0 #1a2a52,
    0 5px 8px rgba(0, 0, 0, 0.35);
  transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.scale-add-btn:hover:not(:disabled) {
  background: linear-gradient(180deg, rgba(110, 160, 230, 0.95) 0%, rgba(52, 88, 158, 0.95) 100%);
  border-color: #a8d4ff;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.45),
    inset 0 0 0 1px rgba(10, 20, 48, 0.5),
    0 3px 0 #1a2a52,
    0 0 14px rgba(120, 210, 255, 0.45),
    0 5px 10px rgba(0, 0, 0, 0.35);
}

.scale-add-btn:active:not(:disabled) {
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.25),
    inset 0 0 0 1px rgba(10, 20, 48, 0.5),
    0 1px 0 #1a2a52,
    0 2px 4px rgba(0, 0, 0, 0.3);
}

.scale-add-btn:disabled {
  opacity: 0.45;
  filter: saturate(0.3) brightness(0.8);
  cursor: default;
}

.btn-gem-icon {
  width: clamp(24px, 5vw, 36px);
  height: auto;
  image-rendering: pixelated;
}

/* Success flash */
.scale-success .scale-image {
  animation: scale-flash 1.4s ease-out;
}

@keyframes scale-flash {
  0% { filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.5)); }
  15% { filter: drop-shadow(0 0 40px rgba(100, 220, 255, 0.9)) brightness(1.4); }
  40% { filter: drop-shadow(0 0 25px rgba(100, 220, 255, 0.6)) brightness(1.2); }
  60% { filter: drop-shadow(0 0 35px rgba(255, 232, 136, 0.7)) brightness(1.3); }
  100% { filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.5)); }
}

/* Gem settle bounce — plays when a gem lands on the pan */
.scale-gem-settle {
  animation: gem-settle 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes gem-settle {
  0% { transform: translateY(-8px) scale(1.1); }
  60% { transform: translateY(2px) scale(0.95); }
  100% { transform: translateY(0) scale(1); }
}


/* ============================================
   CRYSTAL LOCK — Level 2 lock cracking
   ============================================ */

#screen-crystal-lock .scene-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
}

.lock-content {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 1rem 1.5rem;
}

/* Whole lock area sits on an ornate gold double-frame panel,
   echoing the dialogue box frame language */
.lock-slot-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 3vw, 24px);
  max-width: 500px;
  width: 100%;
  padding: clamp(14px, 3vw, 22px);
  background: rgba(12, 16, 22, 0.88);
  border: 3px solid #c5952e;
  box-shadow:
    inset 0 0 0 1px rgba(8, 10, 16, 0.8),
    inset 0 0 0 2px #8a6518,
    inset 0 0 16px rgba(160, 120, 40, 0.06),
    0 0 0 2px rgba(0, 0, 0, 0.8),
    0 0 10px rgba(160, 120, 40, 0.12),
    0 0 24px rgba(0, 0, 0, 0.5);
}

/* Slot header — engraved gold plaque text */
.lock-slot-header {
  font-family: var(--font-pixel);
  font-size: clamp(0.4rem, 1.2vw, 0.55rem);
  color: #daba6a;
  text-shadow: 0 0 8px rgba(218, 186, 106, 0.35), 0 1px 0 rgba(0, 0, 0, 0.8);
  text-transform: uppercase;
  letter-spacing: 0.15em;
}

/* Gems row: a recessed crystal channel inside the gold panel */
.lock-gems-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 2vw, 16px);
  padding: clamp(12px, 3vw, 20px);
  background: linear-gradient(180deg, rgba(6, 10, 20, 0.75) 0%, rgba(14, 22, 40, 0.75) 100%);
  border: 2px solid rgba(100, 180, 255, 0.25);
  border-radius: 8px;
  box-shadow:
    inset 0 3px 8px rgba(0, 0, 0, 0.6),
    inset 0 0 14px rgba(60, 120, 220, 0.12);
  width: 100%;
}

.lock-gem-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  /* Fixed width prevents layout shift from different gem image sizes */
  width: clamp(48px, 10vw, 72px);
}

.lock-gem-icon {
  width: clamp(40px, 9vw, 64px);
  /* Fixed height ensures all gems align regardless of source image aspect ratio */
  height: clamp(32px, 7vw, 50px);
  object-fit: contain;
  image-rendering: pixelated;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.5));
}

.lock-gem-value {
  font-family: var(--font-pixel);
  font-size: clamp(0.5rem, 1.5vw, 0.7rem);
  color: rgba(255, 255, 255, 0.6);
  text-align: center;
}

.lock-operator {
  font-family: var(--font-pixel);
  font-size: clamp(0.7rem, 2vw, 1rem);
  color: rgba(100, 180, 255, 0.8);
}

/* "= ?" reveal — an empty crystal socket cut into the lock */
.lock-equals {
  font-size: clamp(0.8rem, 2.5vw, 1.2rem);
  color: var(--gold-bright);
  min-width: 3em;
  text-align: center;
  padding: 0.3em 0.2em;
  background: radial-gradient(ellipse at center, rgba(20, 36, 70, 0.9) 0%, rgba(8, 14, 28, 0.95) 100%);
  border: 2px solid rgba(100, 180, 255, 0.35);
  border-radius: 6px;
  box-shadow:
    inset 0 2px 6px rgba(0, 0, 0, 0.7),
    inset 0 0 10px rgba(60, 120, 220, 0.2);
  transition: color 0.3s ease, text-shadow 0.3s ease, border-color 0.3s ease,
              box-shadow 0.3s ease, transform 0.3s ease;
}

/* Socket lights up from within when the answer lands */
.lock-answer-reveal {
  color: #58ff58;
  text-shadow: 0 0 12px rgba(88, 255, 88, 0.6);
  border-color: rgba(120, 255, 140, 0.6);
  box-shadow:
    inset 0 2px 6px rgba(0, 0, 0, 0.45),
    inset 0 0 16px rgba(88, 255, 88, 0.45),
    0 0 14px rgba(88, 255, 88, 0.35);
  transform: scale(1.2);
}

/* Number pad */
.lock-numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(8px, 2vw, 12px);
  width: 100%;
  max-width: 300px;
}

/* Crystal keys — translucent icy-blue, 3D depth, lit from above.
   Press physics handled by .game-btn; only colour/shadow transitions here. */
.lock-num-btn {
  font-family: var(--font-pixel);
  font-size: clamp(0.7rem, 2.2vw, 1rem);
  color: #eaf4ff;
  text-shadow: 0 1px 2px rgba(8, 16, 40, 0.8);
  background: linear-gradient(180deg, rgba(90, 138, 210, 0.92) 0%, rgba(42, 74, 138, 0.92) 100%);
  border: 2px solid #7fb2e8;
  padding: clamp(12px, 3vw, 18px);
  cursor: pointer;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    inset 0 0 0 1px rgba(10, 20, 48, 0.5),
    0 3px 0 #1a2a52,
    0 5px 8px rgba(0, 0, 0, 0.35);
  transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  text-align: center;
}

.lock-num-btn:hover:not(:disabled) {
  background: linear-gradient(180deg, rgba(110, 160, 230, 0.95) 0%, rgba(52, 88, 158, 0.95) 100%);
  border-color: #a8d4ff;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.45),
    inset 0 0 0 1px rgba(10, 20, 48, 0.5),
    0 3px 0 #1a2a52,
    0 0 14px rgba(120, 210, 255, 0.45),
    0 5px 10px rgba(0, 0, 0, 0.35);
}

.lock-num-btn:active:not(:disabled) {
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.25),
    inset 0 0 0 1px rgba(10, 20, 48, 0.5),
    0 1px 0 #1a2a52,
    0 2px 4px rgba(0, 0, 0, 0.3);
}

.lock-num-btn:disabled {
  opacity: 0.4;
  filter: saturate(0.25) brightness(0.75);
  cursor: default;
}

.lock-num-btn.lock-correct {
  background: linear-gradient(180deg, rgba(60, 140, 70, 0.92) 0%, rgba(26, 80, 40, 0.92) 100%);
  border-color: #58ff58;
  color: #b8ffb8;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    inset 0 0 12px rgba(88, 255, 88, 0.35),
    0 3px 0 #143a1c,
    0 0 16px rgba(88, 255, 88, 0.5);
  opacity: 1;
  filter: none;
  transform: scale(1.1);
}

.lock-num-btn.lock-wrong {
  background: linear-gradient(180deg, rgba(90, 46, 50, 0.7) 0%, rgba(56, 28, 34, 0.7) 100%);
  border-color: rgba(255, 60, 60, 0.5);
  color: rgba(255, 120, 120, 0.6);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    inset 0 0 0 1px rgba(40, 12, 16, 0.5),
    0 3px 0 #2e1418,
    0 4px 6px rgba(0, 0, 0, 0.3);
  filter: saturate(0.5);
}

/* Shake on wrong answer */
.lock-shake {
  animation: lock-shake 0.4s ease;
}

@keyframes lock-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

/* ============================================
   LOCK-OPENING BEAT — final slot solved (~2s):
   glow build-up → soft shake → panel shatters out.
   Classes driven by level-2.js runLockSlot.
   ============================================ */

/* Stage 1: the lock charges with icy light (filter only — the panel's
   layered box-shadow frame is left untouched) */
.lock-slot-content.lock-opening-glow {
  animation: lock-open-glow 0.8s ease-in forwards;
}

@keyframes lock-open-glow {
  0%   { filter: brightness(1) drop-shadow(0 0 0 rgba(154, 210, 255, 0)); }
  55%  { filter: brightness(1.25) drop-shadow(0 0 18px rgba(154, 210, 255, 0.55)); }
  100% { filter: brightness(1.9) drop-shadow(0 0 36px rgba(205, 243, 255, 0.95)); }
}

/* Stage 2: flash-peak, then the panel swells slightly and dissolves
   (shard divs fly out at the same moment, spawned from JS) */
.lock-slot-content.lock-opening-burst {
  animation: lock-open-burst 0.9s cubic-bezier(0.2, 0.7, 0.4, 1) forwards;
}

@keyframes lock-open-burst {
  0%   { transform: scale(1);    opacity: 1;   filter: brightness(2.2) drop-shadow(0 0 40px rgba(255, 255, 255, 0.9)); }
  35%  { transform: scale(1.06); opacity: 0.9; filter: brightness(1.8) drop-shadow(0 0 28px rgba(205, 243, 255, 0.8)); }
  100% { transform: scale(1.12); opacity: 0;   filter: brightness(1.4) drop-shadow(0 0 8px rgba(154, 210, 255, 0)); }
}

/* Field for the flying crystal shards (spawned + animated from JS,
   same pattern as level-7's .witch-shatter-field) */
.lock-shard-field {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 51;
  overflow: hidden;
}

.lock-shard {
  position: absolute;
  image-rendering: pixelated;
  box-shadow: 0 0 6px rgba(154, 210, 255, 0.8);
}


/* ============================================
   PROGRESSIVE CRYSTAL CRACKS — Jasper in scene
   crystal-crack-1..4 escalate a gentle brightness
   flicker on the trapped sprite as slots are solved;
   crystal-crack-pulse is a one-shot flash applied the
   moment a new crack level lands. Driven by level-2.js
   setCrystalCrackLevel(). Subtle and friendly — light
   leaking through the loosening crystal, never scary.
   ============================================ */

/* Continuous flicker lives on .char-fx (no filter/animation of its own
   outside materialize); the idle transforms on .char-img are untouched. */
#scene-character.crystal-crack-1 .char-fx {
  animation: crystal-crack-flicker-1 4.2s ease-in-out infinite;
}

#scene-character.crystal-crack-2 .char-fx {
  animation: crystal-crack-flicker-2 3.2s ease-in-out infinite;
}

#scene-character.crystal-crack-3 .char-fx {
  animation: crystal-crack-flicker-3 2.4s ease-in-out infinite;
}

#scene-character.crystal-crack-4 .char-fx {
  animation: crystal-crack-flicker-4 1.7s ease-in-out infinite;
}

@keyframes crystal-crack-flicker-1 {
  0%, 86%, 100% { filter: brightness(1); }
  93%           { filter: brightness(1.1) drop-shadow(0 0 6px rgba(154, 210, 255, 0.3)); }
}

@keyframes crystal-crack-flicker-2 {
  0%, 84%, 100% { filter: brightness(1); }
  92%           { filter: brightness(1.16) drop-shadow(0 0 10px rgba(154, 210, 255, 0.4)); }
}

@keyframes crystal-crack-flicker-3 {
  0%, 82%, 100% { filter: brightness(1); }
  91%           { filter: brightness(1.22) drop-shadow(0 0 14px rgba(154, 210, 255, 0.5)); }
}

@keyframes crystal-crack-flicker-4 {
  0%, 80%, 100% { filter: brightness(1.04) drop-shadow(0 0 4px rgba(154, 210, 255, 0.25)); }
  90%           { filter: brightness(1.3) drop-shadow(0 0 18px rgba(205, 243, 255, 0.6)); }
}

/* One-shot glow pulse when a new crack level lands — on the container so
   it never competes with the .char-fx flicker animation above */
#scene-character.crystal-crack-pulse {
  animation: crystal-crack-pulse 0.65s ease-out;
}

@keyframes crystal-crack-pulse {
  0%   { filter: brightness(1); }
  25%  { filter: brightness(1.45) drop-shadow(0 0 20px rgba(205, 243, 255, 0.7)); }
  100% { filter: brightness(1); }
}


/* Value reference at bottom */
.lock-value-ref {
  font-family: var(--font-pixel);
  font-size: clamp(0.35rem, 1vw, 0.45rem);
  color: rgba(245, 230, 200, 0.6);
  text-align: center;
  padding: 6px 12px;
  background: rgba(12, 16, 22, 0.8);
  border: 1px solid rgba(197, 149, 46, 0.45);
  box-shadow: inset 0 0 0 1px rgba(8, 10, 16, 0.8);
}
