:root {
  color-scheme: dark;
  --ink: #f8f4ff;
  --muted: #cdbfd8;
  --night: #15091e;
  --night-2: #241033;
  --panel: rgba(49, 25, 67, 0.88);
  --panel-strong: #321a45;
  --lilac: #c7a6df;
  --purple: #7b4da3;
  --gold: #f1c75b;
  --gold-deep: #c89528;
  --teal: #63d5ca;
  --line: rgba(199, 166, 223, 0.28);
  --shadow: 0 22px 60px rgba(8, 2, 14, 0.5);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--night);
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 18% 8%, rgba(123, 77, 163, 0.26), transparent 28rem),
    radial-gradient(circle at 90% 12%, rgba(241, 199, 91, 0.12), transparent 24rem),
    linear-gradient(145deg, #110718 0%, #1d0c29 52%, #0d1520 100%);
}

button,
img {
  -webkit-user-select: none;
  user-select: none;
}

button {
  font: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem clamp(1rem, 3vw, 2.5rem);
  border-bottom: 1px solid rgba(241, 199, 91, 0.35);
  background: rgba(21, 9, 30, 0.92);
  box-shadow: 0 10px 30px rgba(5, 1, 10, 0.3);
  backdrop-filter: blur(16px);
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  min-width: 0;
}

.realm-mark {
  display: grid;
  width: 2.65rem;
  height: 2.65rem;
  flex: 0 0 auto;
  place-items: center;
  color: var(--gold);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.15rem;
  font-weight: 800;
  border: 2px solid var(--gold);
  border-radius: 0.72rem 0.28rem 0.72rem 0.28rem;
  transform: rotate(45deg);
}

.realm-mark > span {
  transform: rotate(-45deg);
}

.eyebrow,
.section-kicker,
.completion-kicker {
  margin: 0;
  color: var(--gold);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.13em;
  line-height: 1.35;
  text-transform: uppercase;
}

.topbar h1 {
  margin: 0.08rem 0 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.2rem, 2.2vw, 1.8rem);
  line-height: 1.1;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.55rem;
}

.control-button,
#play-again-button {
  display: inline-flex;
  min-height: 2.75rem;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.65rem 0.95rem;
  color: var(--ink);
  font-weight: 750;
  border: 1px solid rgba(199, 166, 223, 0.45);
  border-radius: 999px;
  background: rgba(72, 40, 94, 0.72);
  cursor: pointer;
  transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}

.control-button:hover,
.control-button:focus-visible,
#play-again-button:hover,
#play-again-button:focus-visible {
  border-color: var(--gold);
  background: rgba(105, 61, 135, 0.9);
  outline: none;
  transform: translateY(-1px);
}

.control-button[aria-pressed="false"]#sound-toggle {
  color: var(--muted);
  background: rgba(50, 32, 60, 0.74);
}

.control-button[aria-pressed="true"]#hint-button {
  color: #25142f;
  border-color: var(--gold);
  background: var(--gold);
}

main {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: clamp(1rem, 3vw, 2rem);
}

.game-intro {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 390px);
  gap: 1.5rem;
  align-items: end;
  margin-bottom: 0.65rem;
}

.game-intro h2 {
  margin: 0.22rem 0 0.45rem;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.55rem, 3vw, 2.55rem);
  line-height: 1.08;
}

.instructions {
  max-width: 58rem;
  margin: 0;
  color: var(--muted);
  font-size: clamp(1rem, 1.5vw, 1.12rem);
  line-height: 1.55;
}

.progress-card {
  padding: 0.9rem 1rem;
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: rgba(34, 16, 48, 0.78);
}

.progress-copy {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.55rem;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 700;
}

.progress-track {
  height: 0.62rem;
  overflow: hidden;
  border: 1px solid rgba(199, 166, 223, 0.2);
  border-radius: 999px;
  background: rgba(10, 4, 15, 0.68);
}

.progress-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--lilac), var(--gold));
  box-shadow: 0 0 16px rgba(241, 199, 91, 0.45);
  transition: width 360ms ease;
}

.keyboard-note {
  margin: 0.4rem 0 1rem;
  color: #aa9ab7;
  font-size: 0.84rem;
}

.game-shell {
  padding: clamp(0.7rem, 1.7vw, 1.25rem);
  overflow: hidden;
  border: 1px solid rgba(199, 166, 223, 0.28);
  border-radius: clamp(1.2rem, 2vw, 2rem);
  background:
    linear-gradient(rgba(199, 166, 223, 0.038) 1px, transparent 1px),
    linear-gradient(90deg, rgba(199, 166, 223, 0.038) 1px, transparent 1px),
    rgba(20, 9, 29, 0.74);
  background-size: 26px 26px;
  box-shadow: var(--shadow);
}

.playfield {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(330px, 31vw, 430px);
  gap: clamp(0.8rem, 2vw, 1.45rem);
  align-items: stretch;
}

.board-column {
  min-width: 0;
}

.board-frame {
  position: relative;
  padding: clamp(0.35rem, 0.8vw, 0.65rem);
  border: 1px solid rgba(241, 199, 91, 0.52);
  border-radius: 1.2rem;
  background: linear-gradient(135deg, rgba(241, 199, 91, 0.2), rgba(123, 77, 163, 0.22));
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  border-radius: 0.85rem;
  background:
    radial-gradient(circle at 50% 45%, rgba(199, 166, 223, 0.12), transparent 56%),
    #0d1018;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.board-guide {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
  opacity: 0.105;
  filter: grayscale(0.12) contrast(0.9);
  transition: opacity 220ms ease, filter 220ms ease;
  pointer-events: none;
}

.board.hinting .board-guide {
  opacity: 0.6;
  filter: grayscale(0) contrast(1);
}

.board-shimmer {
  position: absolute;
  inset: 0;
  border: 2px solid transparent;
  border-radius: inherit;
  pointer-events: none;
}

.board.connection-flash .board-shimmer {
  animation: board-flash 720ms ease-out;
}

.board-label {
  margin: 0.7rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
  text-align: center;
}

.piece-tray {
  position: relative;
  min-height: 100%;
  overflow: hidden;
  border: 1px dashed rgba(199, 166, 223, 0.42);
  border-radius: 1.2rem;
  background:
    radial-gradient(circle at 75% 15%, rgba(241, 199, 91, 0.15), transparent 12rem),
    linear-gradient(155deg, rgba(69, 37, 90, 0.72), rgba(29, 15, 40, 0.88));
}

.tray-heading {
  position: relative;
  z-index: 1;
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  padding: 1rem;
  pointer-events: none;
}

.tray-heading h2 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.02rem;
}

.tray-heading p {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.4;
}

.tray-star {
  color: var(--gold);
  font-size: 1.3rem;
}

.tray-pattern {
  position: absolute;
  inset: 0;
  opacity: 0.2;
  background-image: radial-gradient(circle, var(--lilac) 1px, transparent 1.5px);
  background-size: 19px 19px;
  mask-image: linear-gradient(to bottom, transparent, black 18%, black 90%, transparent);
}

.pieces-layer {
  position: absolute;
  inset: 0;
  z-index: 20;
  pointer-events: none;
}

.puzzle-piece {
  position: absolute;
  display: block;
  max-width: none;
  cursor: grab;
  pointer-events: auto;
  touch-action: none;
  filter:
    drop-shadow(0 8px 6px rgba(0, 0, 0, 0.46))
    drop-shadow(0 0 1px rgba(255, 255, 255, 0.76));
  transition: filter 160ms ease, opacity 160ms ease;
  will-change: left, top;
}

.puzzle-piece:hover,
.puzzle-piece:focus-visible {
  filter:
    drop-shadow(0 10px 8px rgba(0, 0, 0, 0.52))
    drop-shadow(0 0 6px rgba(199, 166, 223, 0.88));
  outline: none;
}

.puzzle-piece:focus-visible {
  border-radius: 0.4rem;
  box-shadow: 0 0 0 3px var(--gold);
}

.puzzle-piece.dragging {
  cursor: grabbing;
  filter:
    drop-shadow(0 15px 10px rgba(0, 0, 0, 0.58))
    drop-shadow(0 0 9px rgba(241, 199, 91, 0.75));
}

.puzzle-piece.near-target {
  filter:
    drop-shadow(0 10px 8px rgba(0, 0, 0, 0.5))
    drop-shadow(0 0 10px rgba(99, 213, 202, 0.92));
}

.puzzle-piece.placed {
  cursor: default;
  pointer-events: none;
  filter: none;
}

.puzzle-piece.snap-glow {
  animation: piece-connect 800ms ease-out;
}

.spark-ring {
  position: absolute;
  z-index: 999;
  width: 2.2rem;
  height: 2.2rem;
  border: 3px solid var(--gold);
  border-radius: 50%;
  box-shadow:
    0 0 0 4px rgba(199, 166, 223, 0.6),
    0 0 28px 12px rgba(241, 199, 91, 0.62);
  pointer-events: none;
  animation: spark-ring 720ms ease-out forwards;
}

.status-message {
  min-height: 1.5rem;
  margin: 0.95rem 0 0;
  color: var(--lilac);
  font-weight: 750;
  text-align: center;
}

.completion {
  position: absolute;
  inset: 0;
  z-index: 900;
  display: grid;
  place-content: center;
  padding: 1.5rem;
  color: white;
  text-align: center;
  background: linear-gradient(145deg, rgba(36, 16, 51, 0.9), rgba(63, 31, 82, 0.84));
  backdrop-filter: blur(6px);
  animation: completion-in 600ms ease both;
}

.completion[hidden] {
  display: none;
}

.completion h2 {
  margin: 0.35rem 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2rem, 5vw, 4rem);
}

.completion p:not(.completion-kicker) {
  max-width: 35rem;
  margin: 0 auto 1.2rem;
  color: #eee4f5;
  font-size: clamp(1rem, 2vw, 1.2rem);
  line-height: 1.5;
}

#play-again-button {
  width: fit-content;
  margin: 0 auto;
  color: #241033;
  border-color: var(--gold);
  background: var(--gold);
}

footer {
  padding: 1.3rem 1rem 1.8rem;
  color: var(--muted);
  text-align: center;
}

footer strong {
  color: var(--gold);
}

.print-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  max-width: 70rem;
  margin: 1.15rem auto 0;
  padding: 1rem 1.15rem;
  border: 1px solid rgba(199, 166, 223, 0.42);
  border-radius: 1rem;
  background: rgba(36, 16, 51, 0.72);
}

.print-option p {
  margin: 0;
  color: #eee4f5;
  line-height: 1.45;
}

.print-option .print-kicker {
  margin-bottom: 0.15rem;
  color: var(--gold);
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.print-option a {
  flex: 0 0 auto;
  display: inline-flex;
  min-height: 2.8rem;
  align-items: center;
  justify-content: center;
  padding: 0.7rem 1rem;
  color: #241033;
  border: 2px solid var(--gold);
  border-radius: 0.8rem;
  background: var(--gold);
  font-weight: 800;
  text-decoration: none;
}

.print-option a:hover,
.print-option a:focus-visible {
  background: #ffe08a;
  outline: 3px solid rgba(199, 166, 223, 0.78);
  outline-offset: 3px;
}

@keyframes piece-connect {
  0% {
    filter: drop-shadow(0 0 0 rgba(0, 0, 0, 0));
  }
  35% {
    filter:
      drop-shadow(0 0 12px var(--lilac))
      drop-shadow(0 0 18px var(--gold));
  }
  100% {
    filter: none;
  }
}

@keyframes board-flash {
  0% {
    border-color: transparent;
    box-shadow: none;
  }
  35% {
    border-color: var(--gold);
    box-shadow:
      inset 0 0 35px rgba(199, 166, 223, 0.32),
      0 0 28px rgba(241, 199, 91, 0.4);
  }
  100% {
    border-color: transparent;
    box-shadow: none;
  }
}

@keyframes spark-ring {
  from {
    opacity: 1;
    transform: translate(-50%, -50%) scale(0.25);
  }
  to {
    opacity: 0;
    transform: translate(-50%, -50%) scale(3.1);
  }
}

@keyframes completion-in {
  from {
    opacity: 0;
    transform: scale(0.97);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@media (max-width: 900px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .controls {
    width: 100%;
    justify-content: stretch;
  }

  .control-button {
    flex: 1 1 auto;
  }

  .game-intro {
    grid-template-columns: 1fr;
  }

  .playfield {
    grid-template-columns: 1fr;
  }

  .piece-tray {
    min-height: max(690px, 150vw);
  }

  .keyboard-note {
    display: none;
  }
}

@media (max-width: 520px) {
  main {
    padding: 0.85rem;
  }

  .eyebrow {
    font-size: 0.68rem;
  }

  .controls {
    gap: 0.4rem;
  }

  .control-button {
    min-height: 2.6rem;
    padding: 0.55rem 0.65rem;
    font-size: 0.84rem;
  }

  .game-shell {
    padding: 0.55rem;
    border-radius: 1rem;
  }

  .board-frame,
  .piece-tray {
    border-radius: 0.85rem;
  }

  .piece-tray {
    min-height: 760px;
  }

  .print-option {
    align-items: stretch;
    flex-direction: column;
  }

  .print-option a {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
