:root {
  --ink-light: #1a1f24;
  --ink-dark: #e6edf2;
  --paper-light: #f4f7f9;
  --paper-dark: #171d23;
  --panel-light: rgba(255, 255, 255, 0.82);
  --panel-dark: rgba(17, 23, 30, 0.76);
  --accent: #6f9eb8;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.2);
  --cursor-size: 28px;
  --cursor-border: 3px;
  --cursor-label-size: 11px;
  --paper-texture-light:
    linear-gradient(0deg, rgba(255, 255, 255, 0.25), rgba(255, 255, 255, 0.55)),
    repeating-linear-gradient(115deg, rgba(120, 150, 170, 0.08) 0 2px, transparent 2px 8px),
    repeating-linear-gradient(25deg, rgba(120, 150, 170, 0.04) 0 1px, transparent 1px 6px);
  --paper-texture-dark:
    linear-gradient(0deg, rgba(12, 16, 20, 0.4), rgba(24, 30, 36, 0.6)),
    repeating-linear-gradient(115deg, rgba(110, 140, 160, 0.08) 0 2px, transparent 2px 8px),
    repeating-linear-gradient(25deg, rgba(110, 140, 160, 0.04) 0 1px, transparent 1px 6px);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Space Grotesk", "IBM Plex Sans", sans-serif;
  --paper: var(--paper-light);
  --ink: var(--ink-light);
  --panel-bg: var(--panel-light);
  --paper-texture: var(--paper-texture-light);
  --board-line: rgba(15, 30, 40, 0.15);
  --board-fill: rgba(255, 255, 255, 0.5);
  --board-sheen: rgba(255, 255, 255, 0.8);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% 10%, rgba(255, 255, 255, 0.6), transparent 60%),
    radial-gradient(900px 500px at 90% 20%, rgba(120, 165, 190, 0.18), transparent 60%),
    linear-gradient(130deg, #eef2f5, #e3e9ee 60%, #e9eef2);
  overflow: hidden;
}

body.modal-open #viewport,
body.modal-open #hud {
  pointer-events: none;
  filter: blur(1px) saturate(0.9);
}

body[data-theme="light"] {
  --paper: var(--paper-light);
  --ink: var(--ink-light);
  --panel-bg: var(--panel-light);
  --paper-texture: var(--paper-texture-light);
  --board-line: rgba(15, 30, 40, 0.15);
  --board-fill: rgba(255, 255, 255, 0.5);
}

body[data-theme="dark"] {
  --paper: var(--paper-dark);
  --ink: var(--ink-dark);
  --panel-bg: var(--panel-dark);
  --paper-texture: var(--paper-texture-dark);
  --board-line: rgba(230, 240, 245, 0.18);
  --board-fill: rgba(12, 16, 20, 0.6);
  --board-sheen: rgba(255, 255, 255, 0.12);
  background:
    radial-gradient(1200px 600px at 10% 10%, rgba(130, 170, 190, 0.12), transparent 60%),
    radial-gradient(900px 500px at 90% 20%, rgba(90, 120, 150, 0.14), transparent 60%),
    linear-gradient(130deg, #11161b, #181f27 55%, #13181d);
}

#viewport {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  touch-action: none;
}

#stage {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: top left;
  background-color: var(--paper);
  background-image: var(--paper-texture);
  border-radius: 20px;
  box-shadow: var(--shadow);
  overflow: visible;
  touch-action: none;
}

#board {
  position: absolute;
  border: 2px dashed var(--board-line);
  border-radius: 12px;
  background: linear-gradient(0deg, var(--board-fill), var(--board-sheen));
}

.piece {
  position: absolute;
  user-select: none;
  touch-action: none;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.2));
  transition: filter 120ms ease;
  will-change: transform;
}

.piece.dragging {
  filter: drop-shadow(0 12px 20px rgba(0, 0, 0, 0.35));
  z-index: 3;
}

#cursor-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 20000;
}

.cursor {
  position: absolute;
  width: var(--cursor-size);
  height: var(--cursor-size);
  border-radius: 50%;
  border: var(--cursor-border) solid rgba(255, 255, 255, 0.85);
  transform: translate(-50%, -50%);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.22);
}

.cursor-label {
  position: absolute;
  left: 50%;
  top: -8px;
  transform: translate(-50%, -100%);
  padding: 4px 8px;
  border-radius: 999px;
  font-size: var(--cursor-label-size);
  letter-spacing: 0.03em;
  color: var(--ink);
  background: var(--panel-bg);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.18);
  white-space: nowrap;
  pointer-events: none;
}

#status {
  position: fixed;
  left: 20px;
  top: 20px;
  font-size: 14px;
  letter-spacing: 0.02em;
  color: rgba(0, 0, 0, 0.6);
  background: var(--panel-bg);
  padding: 6px 10px;
  border-radius: 999px;
  backdrop-filter: blur(6px);
}

body[data-theme="dark"] #status {
  color: rgba(230, 240, 245, 0.72);
}

#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
}

#view-controls {
  position: absolute;
  left: 20px;
  top: 60px;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  max-width: calc(100vw - 40px);
  align-items: flex-start;
  pointer-events: auto;
  z-index: 40;
}

#view-controls button {
  border: 0;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  letter-spacing: 0.02em;
  background: var(--panel-bg);
  color: var(--ink);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

#view-controls button:hover {
  transform: translateY(-1px);
  box-shadow: 0 14px 24px rgba(0, 0, 0, 0.2);
}

.panel {
  position: absolute;
  background: var(--panel-bg);
  border-radius: 14px;
  padding: 12px;
  box-shadow: var(--shadow);
  pointer-events: auto;
  backdrop-filter: blur(8px);
}

.panel.collapsed {
  display: none;
}

.panel-title {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: rgba(0, 0, 0, 0.55);
  margin-bottom: 8px;
}

body[data-theme="dark"] .panel-title {
  color: rgba(230, 240, 245, 0.55);
}

#reference-panel {
  top: 20px;
  right: 20px;
  width: min(320px, 36vw);
  max-height: 45vh;
  overflow: hidden;
  z-index: 20;
}

#nickname-panel {
  left: 20px;
  top: 120px;
  width: min(240px, 36vw);
}

.nickname-row {
  display: flex;
  gap: 8px;
}

.nickname-row input {
  flex: 1;
  border: 0;
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 13px;
  background: rgba(255, 255, 255, 0.7);
  color: var(--ink);
  outline: none;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

body[data-theme="dark"] .nickname-row input {
  background: rgba(20, 26, 32, 0.8);
  box-shadow: inset 0 0 0 1px rgba(230, 240, 245, 0.12);
}

.nickname-row button {
  border: 0;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 12px;
  background: var(--panel-bg);
  color: var(--ink);
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
  box-shadow: 0 10px 18px rgba(0, 0, 0, 0.16);
}

.nickname-row button:hover {
  transform: translateY(-1px);
}

#reference-image {
  width: 100%;
  height: auto;
  display: block;
  max-height: calc(45vh - 36px);
  object-fit: contain;
  border-radius: 10px;
  background: var(--paper);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
  cursor: zoom-in;
}

#lofi-shell {
  right: 20px;
  bottom: 20px;
  width: min(220px, 40vw);
  max-width: 240px;
  overflow: hidden;
  display: none;
}

#lofi-shell.active {
  display: block;
}

#lofi-player {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 10px;
  overflow: hidden;
  background: var(--paper);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

#lofi-player iframe,
#lofi-player object,
#lofi-player embed {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

body.lofi-audio-only #lofi-shell {
  display: block;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
}

body.lofi-audio-only #lofi-player {
  width: 1px;
  height: 1px;
  aspect-ratio: auto;
}

#minimap-panel {
  left: 20px;
  bottom: 20px;
  width: auto;
}

#reference-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  z-index: 1000;
  transition: opacity 220ms ease;
}

#reference-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

#reference-overlay .reference-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(12, 16, 20, 0.75);
  backdrop-filter: blur(4px);
}

#reference-overlay-image {
  position: relative;
  max-width: min(92vw, 1400px);
  max-height: 92vh;
  border-radius: 14px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.4);
  transform: scale(0.94);
  opacity: 0;
  transition: transform 240ms ease, opacity 240ms ease;
}

#reference-overlay.show #reference-overlay-image {
  transform: scale(1);
  opacity: 1;
}

#minimap {
  display: block;
  border-radius: 10px;
  background: var(--paper);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

body[data-theme="dark"] #minimap,
body[data-theme="dark"] #reference-image {
  box-shadow: inset 0 0 0 1px rgba(230, 240, 245, 0.12);
}

@media (max-width: 900px) {
  #reference-panel {
    width: min(240px, 42vw);
  }

  #view-controls {
    top: 56px;
  }

  #nickname-panel {
    top: 116px;
    width: min(220px, 42vw);
  }
}

@media (max-width: 720px) {
  #reference-panel {
    right: 12px;
    top: auto;
    bottom: 12px;
    width: min(200px, 48vw);
  }

  #lofi-shell {
    right: 12px;
    bottom: 12px;
    width: min(180px, 56vw);
    max-width: 200px;
  }

  #minimap-panel {
    left: 12px;
    bottom: 12px;
  }

  #status {
    left: 12px;
    top: 12px;
  }

  #view-controls {
    left: 12px;
    top: 48px;
    flex-direction: column;
  }

  #view-controls button {
    font-size: 12px;
    padding: 7px 12px;
  }

  #nickname-panel {
    left: 12px;
    top: 128px;
    width: min(210px, 70vw);
  }
}

.noscript {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--paper);
  color: var(--ink);
  font-size: 18px;
}

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 240ms ease;
  z-index: 70000;
}

.modal.show {
  opacity: 1;
  pointer-events: auto;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(600px 300px at 50% 45%, rgba(255, 255, 255, 0.65), transparent 70%),
    rgba(8, 12, 16, 0.25);
  backdrop-filter: blur(6px);
}

.modal-card {
  position: relative;
  width: min(420px, 88vw);
  padding: 26px 28px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.28);
  text-align: center;
  transform: translateY(16px) scale(0.98);
  transition: transform 240ms ease;
}

body[data-theme="dark"] .modal-card {
  background: rgba(17, 23, 30, 0.9);
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.45);
}

.modal.show .modal-card {
  transform: translateY(0) scale(1);
}

.modal-title {
  font-size: clamp(20px, 4vw, 26px);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 6px;
}

.modal-text {
  font-size: 14px;
  color: rgba(0, 0, 0, 0.6);
  margin-bottom: 18px;
}

body[data-theme="dark"] .modal-text {
  color: rgba(230, 240, 245, 0.6);
}

.modal-row {
  display: flex;
  gap: 10px;
}

.modal-row input {
  flex: 1;
  border: 0;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 14px;
  background: rgba(255, 255, 255, 0.85);
  color: var(--ink);
  outline: none;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

body[data-theme="dark"] .modal-row input {
  background: rgba(20, 26, 32, 0.9);
  box-shadow: inset 0 0 0 1px rgba(230, 240, 245, 0.12);
}

.modal-row button {
  border: 0;
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 13px;
  background: var(--panel-bg);
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.18);
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.modal-row button:hover {
  transform: translateY(-1px);
  box-shadow: 0 16px 28px rgba(0, 0, 0, 0.22);
}

.piece.snap {
  animation: pieceSnap 520ms ease-out;
}

@keyframes pieceSnap {
  0% {
    filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.2));
  }
  45% {
    filter:
      drop-shadow(0 14px 26px rgba(111, 158, 184, 0.85))
      drop-shadow(0 0 16px rgba(180, 220, 240, 0.7))
      brightness(1.08)
      saturate(1.15);
  }
  100% {
    filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.2));
  }
}

#completion {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 60000;
  opacity: 0;
  animation: completionFade 6.5s ease forwards;
  background:
    radial-gradient(600px 300px at 50% 50%, rgba(255, 255, 255, 0.6), transparent 70%),
    radial-gradient(900px 500px at 50% 40%, rgba(120, 165, 190, 0.18), transparent 70%);
}

.completion-inner {
  text-align: center;
  padding: 28px 38px;
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(6px);
}

.completion-title {
  font-size: clamp(36px, 7vw, 88px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.completion-sub {
  margin-top: 8px;
  font-size: clamp(14px, 2.2vw, 22px);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: rgba(0, 0, 0, 0.6);
}

@keyframes completionFade {
  0% {
    opacity: 0;
    transform: scale(0.96);
  }
  12% {
    opacity: 1;
    transform: scale(1);
  }
  70% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: scale(1.02);
  }
}
