:root {
  --panel-bg: rgba(0, 0, 0, 0.64);
  --panel-border: rgba(255, 255, 255, 0.2);
  --text: #ffffff;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Trebuchet MS", Verdana, sans-serif;
  color: var(--text);
  background: #000;
}

#game {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-image: url("Background.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  user-select: none;
}

.hidden {
  display: none !important;
}

.ui-layer {
  position: absolute;
  inset: 0;
  z-index: 3;
}

#narrationOverlay {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(14px, 4vw, 40px);
}

.center-panel {
  width: min(860px, 92vw);
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: clamp(20px, 3.4vw, 34px);
  text-align: center;
  backdrop-filter: blur(2px);
}

#narrationText {
  margin: 0;
  font-size: clamp(18px, 2.3vw, 34px);
  line-height: 1.4;
}

#dialoguePanel {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}

.dialogue-wrap {
  position: relative;
  width: min(1118px, 96vw);
  min-height: clamp(190px, 30vh, 350px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  margin: 0;
}

#portrait {
  position: absolute;
  left: clamp(-56px, -6vw, -10px);
  bottom: 0;
  height: min(92vh, 100vh);
  width: auto;
  max-width: min(58vw, 620px);
  display: block;
  object-fit: contain;
  z-index: 1;
  margin: 0;
  pointer-events: none;
  filter: drop-shadow(0 10px 18px rgba(0, 0, 0, 0.45));
}

.dialogue-box {
  position: relative;
  z-index: 2;
  width: 100%;
  min-height: clamp(90px, 15vw, 165px);
  margin: 0 0 clamp(6px, 1vw, 14px) clamp(110px, 17vw, 255px);
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: clamp(14px, 2.3vw, 26px);
  display: flex;
  align-items: center;
  backdrop-filter: blur(2px);
}

#dialogueText {
  margin: 0;
  width: 100%;
  font-size: clamp(17px, 2.2vw, 30px);
  line-height: 1.35;
  text-align: left;
}

#cardGame {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(10px, 2vw, 24px);
}

#anxiousText {
  position: absolute;
  top: 16%;
  left: 50%;
  transform: translateX(-50%);
  width: min(90vw, 980px);
  text-align: center;
  color: rgba(255, 255, 255, 0.95);
  font-size: clamp(18px, 2vw, 30px);
  letter-spacing: 0.02em;
  text-shadow: 0 0 10px rgba(0, 0, 0, 0.82), 0 0 20px rgba(0, 0, 0, 0.56);
  opacity: 0;
  pointer-events: none;
  z-index: 4;
}

#anxiousText.show {
  animation: anxiousFade 1.5s ease forwards;
}

#cardGrid {
  width: min(92vw, 760px);
  display: grid;
  gap: clamp(6px, 1.2vw, 14px);
}

#cardGrid.level-1 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  width: min(56vw, 420px);
}

#cardGrid.level-2 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  width: min(72vw, 560px);
}

#cardGrid.level-3 {
  grid-template-columns: repeat(5, minmax(0, 1fr));
  width: min(92vw, 760px);
}

#cardGrid.locked {
  pointer-events: none;
}

#cardGrid.loss-dim {
  opacity: 0.45;
  filter: brightness(0.6) saturate(0.7);
}

.card {
  position: relative;
  aspect-ratio: 2 / 3;
  border: 0;
  background: transparent;
  padding: 0;
  cursor: pointer;
  transform-origin: center;
  transition: transform 0.16s ease, filter 0.16s ease;
  will-change: transform, filter;
}

.card::after {
  content: "";
  position: absolute;
  inset: 5%;
  border-radius: 10px;
  pointer-events: none;
  opacity: 0;
}

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

.card-symbol {
  opacity: 0;
  transition: opacity 0.2s ease;
}

.card.revealed .card-symbol,
.card.matched .card-symbol {
  opacity: 1;
}

.card.matched {
  cursor: default;
  pointer-events: none;
}

.card.matched-glow::after {
  opacity: 1;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.85), 0 0 26px rgba(132, 227, 255, 0.75);
  animation: glowPulse 0.56s ease-out forwards;
}

.card.matched-glow .card-base,
.card.matched-glow .card-symbol {
  filter: brightness(1.45) saturate(1.45) contrast(1.15)
    drop-shadow(0 0 8px rgba(120, 220, 255, 0.65));
}

.card.matched-dim .card-base,
.card.matched-dim .card-symbol {
  opacity: 0.46;
  filter: saturate(0.35) brightness(0.65) contrast(0.92);
}

.card.matched-dim .card-symbol {
  opacity: 0.46;
}

.card-symbol.glitching {
  animation: glitchOut 0.34s steps(2, end) forwards;
}

.card.interference {
  animation: interferencePulse 0.34s ease-out;
}

.card-symbol.manip-glitch {
  animation: manipulationGlitch 0.42s steps(2, end);
}

#screamer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  object-fit: cover;
  opacity: 0;
  pointer-events: none;
  z-index: 9999;
  transform: scale(1);
  transition: opacity 120ms ease, transform 120ms ease;
}

#screamer.active {
  opacity: 1;
  transform: scale(1.03);
  pointer-events: auto;
}

#screamer.flicker {
  animation: screamerFlicker 820ms steps(1, end) 1 both;
}

#screamer.fade-out {
  opacity: 0;
  transform: scale(1);
  transition: opacity 600ms ease, transform 600ms ease;
}

#purpleOverlay {
  position: absolute;
  inset: 0;
  z-index: 25;
  background: #07051b;
  opacity: 0;
  pointer-events: none;
  transition: opacity 1600ms ease;
}

#purpleOverlay.show {
  opacity: 0.9;
}

#farewellText {
  position: absolute;
  inset: 0;
  z-index: 26;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  color: #f5ebff;
  text-shadow: 0 0 20px rgba(10, 0, 25, 0.8);
  font-size: clamp(34px, 7vw, 86px);
  letter-spacing: 0.08em;
  opacity: 0;
  transition: opacity 1300ms ease;
}

#farewellText.show {
  opacity: 1;
}

@keyframes anxiousFade {
  0% {
    opacity: 0;
    transform: translateX(-50%) translateY(-6px);
  }
  18% {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
  80% {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
  100% {
    opacity: 0;
    transform: translateX(-50%) translateY(-4px);
  }
}

@keyframes glowPulse {
  0% {
    opacity: 0.25;
    transform: scale(0.98);
  }
  45% {
    opacity: 1;
    transform: scale(1.03);
  }
  100% {
    opacity: 0;
    transform: scale(1);
  }
}

@keyframes glitchOut {
  0% {
    opacity: 1;
    transform: translate(0, 0);
    filter: hue-rotate(0deg) contrast(1);
  }
  20% {
    opacity: 0.5;
    transform: translate(-1px, 1px);
    filter: hue-rotate(12deg) contrast(1.2);
  }
  45% {
    opacity: 0.9;
    transform: translate(1px, -1px);
    filter: hue-rotate(-9deg) contrast(1.25);
  }
  70% {
    opacity: 0.35;
    transform: translate(-1px, 0);
    filter: hue-rotate(15deg) contrast(1.3);
  }
  100% {
    opacity: 0;
    transform: translate(0, 0);
    filter: hue-rotate(0deg) contrast(1);
  }
}

@keyframes interferencePulse {
  0% {
    filter: brightness(1) hue-rotate(0deg);
    transform: translate(0, 0);
  }
  25% {
    filter: brightness(1.25) hue-rotate(35deg);
    transform: translate(-1px, 1px);
  }
  50% {
    filter: brightness(0.9) hue-rotate(-28deg);
    transform: translate(1px, -1px);
  }
  100% {
    filter: brightness(1) hue-rotate(0deg);
    transform: translate(0, 0);
  }
}

@keyframes manipulationGlitch {
  0% {
    opacity: 1;
    transform: translate(0, 0) scale(1);
    filter: hue-rotate(0deg) contrast(1) brightness(1);
  }
  18% {
    opacity: 0.35;
    transform: translate(-3px, 2px) scale(1.03);
    filter: hue-rotate(95deg) contrast(2.1) brightness(1.35);
  }
  38% {
    opacity: 0.95;
    transform: translate(3px, -3px) scale(0.98);
    filter: hue-rotate(-110deg) contrast(2.2) brightness(0.9);
  }
  58% {
    opacity: 0.2;
    transform: translate(-4px, 1px) scale(1.02);
    filter: hue-rotate(140deg) contrast(2.4) brightness(1.4);
  }
  76% {
    opacity: 0.85;
    transform: translate(2px, 2px) scale(1);
    filter: hue-rotate(-70deg) contrast(1.9) brightness(1.1);
  }
  100% {
    opacity: 1;
    transform: translate(0, 0) scale(1);
    filter: hue-rotate(0deg) contrast(1) brightness(1);
  }
}

@keyframes screamerFlicker {
  0% {
    opacity: 0;
    filter: brightness(2) contrast(1.8);
  }
  14% {
    opacity: 1;
  }
  24% {
    opacity: 0.2;
  }
  36% {
    opacity: 1;
  }
  48% {
    opacity: 0.28;
  }
  62% {
    opacity: 1;
  }
  78% {
    opacity: 0.45;
  }
  100% {
    opacity: 1;
    filter: brightness(1) contrast(1);
  }
}

@media (hover: hover) and (pointer: fine) {
  .card:not(.matched):hover {
    transform: scale(1.035);
    filter: brightness(1.08);
  }
}

.card:not(.matched):active,
.card.pressed {
  transform: scale(0.97);
  filter: brightness(0.9);
}

@media (max-width: 900px) {
  #portrait {
    max-width: min(66vw, 560px);
    height: min(84vh, 100vh);
    left: clamp(-52px, -7vw, -8px);
  }

  .dialogue-box {
    margin-left: clamp(72px, 14vw, 190px);
  }
}

@media (max-width: 720px) {
  #dialogueText {
    text-align: center;
  }

  .dialogue-wrap {
    min-height: clamp(145px, 23vh, 230px);
  }

  #portrait {
    max-width: min(72vw, 390px);
    height: min(70vh, 100vh);
    left: clamp(-36px, -6vw, -6px);
    bottom: 0;
  }

  .dialogue-box {
    margin-left: clamp(30px, 8vw, 90px);
    margin-bottom: clamp(4px, 0.9vw, 10px);
  }
}
