:root {
  --bg: #0a0a1a;
  --panel: rgba(16, 16, 38, 0.92);
  --panel-strong: #15152e;
  --ink: #f3f0ff;
  --muted: #8f91aa;
  --blue: #57c7ff;
  --red: #ff5b5b;
  --purple: #b765ff;
  --gold: #ffd65c;
  --green: #47e38a;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at top, rgba(106, 57, 188, 0.22), transparent 36rem),
    linear-gradient(180deg, #070711 0%, var(--bg) 60%, #05050c 100%);
  font-family: "Press Start 2P", system-ui, sans-serif;
  letter-spacing: 0;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  color: var(--ink);
  background: #272744;
  cursor: pointer;
  box-shadow: 0 5px 0 #101020;
  transition: transform 120ms ease, box-shadow 120ms ease, filter 120ms ease;
  touch-action: manipulation;
}

button:hover:not(:disabled),
button:focus-visible:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 3px 0 #101020;
  filter: brightness(1.15);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.app-shell {
  width: min(100%, 760px);
  min-height: 100vh;
  margin: 0 auto;
  padding: 24px 14px 36px;
  display: grid;
  place-items: center;
}

.screen {
  width: 100%;
}

.start-screen,
.game-over-screen {
  text-align: center;
}

.game-title {
  margin: 0 0 18px;
  color: var(--purple);
  font-size: clamp(1.8rem, 7vw, 3.6rem);
  line-height: 1.28;
  background: linear-gradient(135deg, #d49fff 0%, #b765ff 40%, #7c3aed 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: none;
  filter: drop-shadow(0 0 18px rgba(183, 101, 255, 0.7));
  animation: titleFloat 2.5s ease-in-out infinite;
}

.subtitle {
  margin: 0 auto 28px;
  max-width: 680px;
  color: #cfd0e8;
  font-size: clamp(0.64rem, 2.5vw, 0.88rem);
  line-height: 2;
}

.version-tag {
  font-size: 0.48rem;
  color: #5a5c7a;
  margin: -16px 0 28px;
  letter-spacing: 0;
}

.start-card {
  display: grid;
  gap: 16px;
  width: min(100%, 560px);
  margin: 0 auto;
  background: rgba(20, 16, 40, 0.7);
  border: 1px solid rgba(183, 101, 255, 0.25);
  border-radius: 2px;
  padding: 28px 24px;
  backdrop-filter: blur(8px);
}

.api-key-input {
  width: 100%;
  min-height: 56px;
  padding: 0 16px;
  color: var(--ink);
  background: rgba(11, 11, 27, 0.9);
  border: 2px solid rgba(183, 101, 255, 0.5);
  outline: none;
}

.api-key-input:focus {
  border-color: var(--purple);
  box-shadow: 0 0 22px rgba(183, 101, 255, 0.25);
}

.primary-button {
  min-height: 56px;
  padding: 0 18px;
  color: #160d20;
  background: var(--gold);
  box-shadow: 0 5px 0 #85691f;
}

.primary-button:hover:not(:disabled),
.primary-button:focus-visible:not(:disabled) {
  box-shadow: 0 3px 0 #85691f;
}

.game-screen {
  display: grid;
  gap: 14px;
}

.run-hud {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  font-size: clamp(0.48rem, 1.7vw, 0.66rem);
  line-height: 1.6;
}

.run-hud span,
.narration-panel,
.combat-log,
.choice-panel,
.final-stats,
.run-chronicle {
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.run-hud span {
  min-height: 42px;
  display: grid;
  place-items: center;
  padding: 6px;
  text-align: center;
  background: rgba(10, 10, 30, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-bottom: 2px solid rgba(183, 101, 255, 0.3);
  font-size: clamp(0.44rem, 1.5vw, 0.6rem);
  transition: border-color 0.3s ease;
}

.canvas-frame {
  position: relative;
  width: min(100%, 604px);
  margin: 0 auto;
}

.canvas-frame::after {
  content: "";
  position: absolute;
  inset: 2px;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0, 0, 0, 0.06) 2px,
    rgba(0, 0, 0, 0.06) 4px
  );
  pointer-events: none;
  z-index: 2;
}

#gameCanvas {
  display: block;
  width: 100%;
  aspect-ratio: 3 / 2;
  background: #050510;
  border: 2px solid var(--blue);
  image-rendering: pixelated;
}

.theme-blue #gameCanvas {
  border-color: var(--blue);
  animation: borderPulseBlue 1.7s ease-in-out infinite;
}

.theme-red #gameCanvas {
  border-color: var(--red);
  animation: borderPulseRed 1.7s ease-in-out infinite;
}

.theme-purple #gameCanvas {
  border-color: var(--purple);
  animation: borderPulsePurple 1.7s ease-in-out infinite;
}

#gameCanvas.damage-flash {
  border-color: var(--red);
  box-shadow: 0 0 34px rgba(255, 91, 91, 0.85);
}

.mute-toggle {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 4;
  width: 38px;
  height: 34px;
  color: var(--gold);
  border: 1px solid rgba(255, 214, 92, 0.42);
  background: rgba(5, 5, 14, 0.82);
  box-shadow: 0 3px 0 #101020;
}

.floor-transition {
  position: absolute;
  inset: 2px;
  z-index: 3;
  display: grid;
  place-items: center;
  color: var(--ink);
  background: rgba(0, 0, 0, 0.9);
  opacity: 0;
  transition: opacity 220ms ease;
  pointer-events: none;
}

.floor-transition.is-visible {
  opacity: 1;
}

.floor-transition span {
  color: var(--gold);
  font-size: clamp(1.2rem, 6vw, 2.6rem);
  text-shadow: 0 0 24px rgba(255, 214, 92, 0.5);
}

.narration-panel {
  min-height: 92px;
  border-left: 3px solid var(--purple);
  background: linear-gradient(90deg, rgba(30, 16, 50, 0.95) 0%, rgba(16, 16, 38, 0.92) 100%);
  padding: 16px 18px;
}

.narration-speaker {
  margin-bottom: 10px;
  color: var(--gold);
  font-size: 0.58rem;
  letter-spacing: 0;
  text-shadow: 0 0 10px rgba(255, 214, 92, 0.4);
}

.narration-text {
  min-height: 42px;
  margin: 0;
  color: #e8e9ff;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.88rem;
  line-height: 1.7;
  font-style: italic;
}

.typing::after {
  content: "";
  animation: dots 1s steps(4, end) infinite;
}

.combat-log {
  min-height: 96px;
  padding: 10px 14px;
  display: grid;
  align-content: start;
  gap: 6px;
  border-left: 3px solid rgba(87, 199, 255, 0.3);
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.92rem;
  line-height: 1.3;
}

.log-line {
  color: #8f91aa;
  padding: 3px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
  font-size: 0.82rem;
}

.log-line:last-child {
  color: #ffffff;
  border-bottom: none;
}

.choice-panel {
  border-left: 3px solid var(--gold);
  background: linear-gradient(90deg, rgba(30, 20, 5, 0.9), rgba(16, 16, 38, 0.92));
  padding: 14px;
}

.choice-title {
  margin: 0 0 12px;
  color: var(--gold);
  font-size: 0.68rem;
  line-height: 1.7;
}

.choice-text {
  margin: 0 0 14px;
  color: #d8daf2;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.95rem;
  line-height: 1.45;
}

.choice-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.choice-buttons button {
  min-height: 42px;
  padding: 0 12px;
  font-size: 0.62rem;
  border-bottom: 2px solid rgba(255, 214, 92, 0.4);
  background: rgba(255, 214, 92, 0.08);
  transition: all 150ms ease;
}

.choice-buttons button:hover {
  background: rgba(255, 214, 92, 0.18);
  border-bottom-color: var(--gold);
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.action-grid button {
  min-height: 50px;
  padding: 0 8px;
  color: #f6f1ff;
  font-size: clamp(0.52rem, 2vw, 0.66rem);
}

.action-grid button[data-action="attack"] {
  border-bottom: 3px solid #ff5b5b;
  background: rgba(255, 91, 91, 0.12);
}

.action-grid button[data-action="defend"] {
  border-bottom: 3px solid #57c7ff;
  background: rgba(87, 199, 255, 0.1);
}

.action-grid button[data-action="item"] {
  border-bottom: 3px solid #47e38a;
  background: rgba(71, 227, 138, 0.1);
}

.action-grid button[data-action="flee"] {
  border-bottom: 3px solid #ffd65c;
  background: rgba(255, 214, 92, 0.08);
}

.action-grid button:hover:not(:disabled) {
  filter: brightness(1.3);
  transform: translateY(2px);
}

.key-hint {
  text-align: center;
  color: #8f91aa;
  font-size: 0.52rem;
  margin: 4px 0 0;
  letter-spacing: 0;
}

.game-over-screen h2,
.game-over-screen h3 {
  color: var(--gold);
  line-height: 1.6;
}

.final-stats {
  width: min(100%, 580px);
  margin: 0 auto 20px;
  padding: 16px;
  display: grid;
  gap: 12px;
  font-size: 0.7rem;
  line-height: 1.7;
}

.run-chronicle {
  width: min(100%, 660px);
  max-height: 280px;
  margin: 0 auto 24px;
  padding: 18px 18px 18px 42px;
  overflow: auto;
  text-align: left;
  color: #d8daf2;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.92rem;
  line-height: 1.45;
  border-left: 3px solid var(--gold);
  background: linear-gradient(90deg, rgba(30, 20, 5, 0.9), rgba(16, 16, 38, 0.9));
}

.run-chronicle li {
  padding: 4px 0;
  border-bottom: 1px solid rgba(255, 214, 92, 0.08);
  line-height: 1.7;
}

.share-run-button {
  display: block;
  width: min(100%, 280px);
  margin: 0 auto 16px;
  white-space: nowrap;
}

@keyframes titleFloat {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-3px);
  }
}

@keyframes borderPulseBlue {
  0%,
  100% {
    box-shadow: 0 0 10px rgba(87, 199, 255, 0.25), 0 0 26px rgba(87, 199, 255, 0.12);
  }
  50% {
    box-shadow: 0 0 18px rgba(87, 199, 255, 0.5), 0 0 34px rgba(87, 199, 255, 0.22);
  }
}

@keyframes borderPulseRed {
  0%,
  100% {
    box-shadow: 0 0 10px rgba(255, 91, 91, 0.22), 0 0 26px rgba(255, 91, 91, 0.1);
  }
  50% {
    box-shadow: 0 0 18px rgba(255, 91, 91, 0.48), 0 0 34px rgba(255, 91, 91, 0.2);
  }
}

@keyframes borderPulsePurple {
  0%,
  100% {
    box-shadow: 0 0 10px rgba(183, 101, 255, 0.24), 0 0 26px rgba(183, 101, 255, 0.12);
  }
  50% {
    box-shadow: 0 0 18px rgba(183, 101, 255, 0.52), 0 0 34px rgba(183, 101, 255, 0.22);
  }
}

@keyframes dots {
  0% {
    content: "";
  }
  25% {
    content: ".";
  }
  50% {
    content: "..";
  }
  75%,
  100% {
    content: "...";
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding-top: 14px;
  }

  .run-hud {
    grid-template-columns: repeat(2, 1fr);
  }

  .run-hud span:first-child {
    grid-column: 1 / -1;
  }

  .action-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .narration-text,
  .combat-log,
  .choice-text,
  .run-chronicle {
    font-size: 0.84rem;
  }
}
