:root {
  --bg-dark: #0d0f1a;
  --bg-panel: #1a1d33;
  --text: #e0e8ff;
  --muted: #a0a8c8;
  --accent-blue: #6fa8ff;
  --accent-pink: #ff7ac2;
  --accent-green: #6fe3a2;
  --accent-yellow: #ffd86b;
  --accent-red: #ff6f6f;
  --accent-purple: #b794f6;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);

  /* Prime colors */
  --prime-2: #ff6b6b;
  --prime-3: #74c0fc;
  --prime-5: #ffd43b;
  --prime-7: #51cf66;
}

* {
  box-sizing: border-box;
}

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

body {
  background: radial-gradient(circle at top, #1e2245, var(--bg-dark) 70%);
  color: var(--text);
  font-family:
    "Lexend",
    system-ui,
    -apple-system,
    Segoe UI,
    Roboto,
    sans-serif;
}

/* Header */
.app-header {
  padding: 16px 24px;
  border-bottom: 1px solid #303560;
}

.title-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.help-btn {
  margin-left: 6px;
  padding: 4px 10px;
  border-radius: 50%;
  border: 2px solid #4a4f80;
  background: var(--bg-panel);
  color: var(--accent-blue);
  font-size: 18px;
  font-weight: 900;
  cursor: pointer;
  transition: all 0.2s;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.help-btn:hover {
  background: var(--accent-blue);
  color: var(--bg-dark);
  border-color: var(--accent-blue);
  transform: scale(1.1);
}

.help-btn:active {
  transform: translateY(1px) scale(1.05);
}

.limit-badge {
  font-size: 32px;
  font-weight: 900;
  color: var(--accent-blue);
  line-height: 1;
  text-shadow: 0 0 6px #1a1d33;
}

.game-title {
  font-size: 36px;
  font-weight: 900;
  color: var(--accent-blue);
  line-height: 1;
  text-shadow: 0 0 10px rgba(111, 168, 255, 0.5);
}

.tagline {
  font-family: "Space Mono", monospace;
  font-size: 16px;
  color: var(--accent-green);
  text-shadow: 0 0 10px var(--accent-green);
  white-space: nowrap;
}

.game-info {
  display: flex;
  gap: 32px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.stat-info {
  font-family: "Space Mono", monospace;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.stat-info .label {
  color: var(--muted);
}

.stat-info .value {
  color: var(--accent-yellow);
  font-weight: 700;
  font-size: 24px;
}

.stat-info.target .value {
  color: var(--accent-pink);
  font-size: 32px;
  text-shadow: 0 0 15px var(--accent-pink);
}

.player-info-display {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-panel);
  padding: 6px 12px;
  border-radius: 999px;
  border: 2px solid #4a4f80;
}

.player-avatar-small {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--accent-blue);
  background: #1a1d33;
}

.player-name-display {
  font-family: "Space Mono", monospace;
  font-size: 14px;
  font-weight: 700;
  color: var(--accent-blue);
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 16px;
  padding: 16px;
  height: calc(100vh - 110px);
}

.game-area {
  background: linear-gradient(135deg, #0a1628 0%, #000 100%);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 1px solid #303560;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.right-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

/* Grid Container */
.grid-container {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 8px;
  max-width: 600px;
  max-height: 600px;
  width: 100%;
  aspect-ratio: 1;
  position: relative;
  z-index: 1;
}

.tile {
  background: var(--bg-panel);
  border: 3px solid #4a4f80;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  font-weight: 900;
  font-family: "Space Mono", monospace;
  color: #fff;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  user-select: none;
  position: relative;
}

.tile:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

.tile[data-value="2"] {
  background: linear-gradient(135deg, var(--prime-2), #c92a2a);
  border-color: #c92a2a;
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.4);
}

.tile[data-value="3"] {
  background: linear-gradient(135deg, var(--prime-3), #339af0);
  border-color: #339af0;
  box-shadow: 0 4px 12px rgba(116, 192, 252, 0.4);
}

.tile[data-value="5"] {
  background: linear-gradient(135deg, var(--prime-5), #fab005);
  border-color: #fab005;
  box-shadow: 0 4px 12px rgba(255, 212, 59, 0.4);
}

.tile[data-value="7"] {
  background: linear-gradient(135deg, var(--prime-7), #2f9e44);
  border-color: #2f9e44;
  box-shadow: 0 4px 12px rgba(81, 207, 102, 0.4);
}

.tile[data-value="11"] {
  background: linear-gradient(135deg, #b794f6, #9775fa);
  border-color: #9775fa;
  box-shadow: 0 4px 12px rgba(183, 148, 246, 0.4);
}

.tile[data-value="13"] {
  background: linear-gradient(135deg, #ff8787, #fa5252);
  border-color: #fa5252;
  box-shadow: 0 4px 12px rgba(255, 135, 135, 0.4);
}

.tile[data-value="17"] {
  background: linear-gradient(135deg, #69db7c, #51cf66);
  border-color: #51cf66;
  box-shadow: 0 4px 12px rgba(105, 219, 124, 0.4);
}

.tile[data-value="19"] {
  background: linear-gradient(135deg, #ffa94d, #ff922b);
  border-color: #ff922b;
  box-shadow: 0 4px 12px rgba(255, 169, 77, 0.4);
}

.tile[data-value="23"] {
  background: linear-gradient(135deg, #8ce99a, #69db7c);
  border-color: #69db7c;
  box-shadow: 0 4px 12px rgba(140, 233, 154, 0.4);
}

.tile[data-value="29"] {
  background: linear-gradient(135deg, #ff6b9d, #e64980);
  border-color: #e64980;
  box-shadow: 0 4px 12px rgba(255, 107, 157, 0.4);
}

.tile[data-value="31"] {
  background: linear-gradient(135deg, #9775fa, #845ef7);
  border-color: #845ef7;
  box-shadow: 0 4px 12px rgba(151, 117, 250, 0.4);
}

.tile[data-value="37"] {
  background: linear-gradient(135deg, #ff8c42, #fd7e14);
  border-color: #fd7e14;
  box-shadow: 0 4px 12px rgba(255, 140, 66, 0.4);
}

.tile.selected {
  transform: scale(1.1);
  z-index: 2;
  box-shadow: 0 0 30px rgba(255, 255, 255, 0.6);
  border-color: #fff;
}

.tile.selected.overshoot {
  border-color: var(--accent-red);
  box-shadow: 0 0 30px var(--accent-red);
}

.tile.selected.exact-match {
  border-color: var(--accent-green);
  box-shadow: 0 0 30px var(--accent-green);
  animation: pulse 0.5s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1.1);
  }
  50% {
    transform: scale(1.15);
  }
}

.tile.popping {
  animation: pop 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) forwards;
  z-index: 10;
}

@keyframes pop {
  0% {
    transform: scale(1) rotate(0deg);
    opacity: 1;
    filter: brightness(1);
  }
  40% {
    transform: scale(1.4) rotate(180deg);
    opacity: 1;
    filter: brightness(1.5) saturate(1.5);
  }
  100% {
    transform: scale(0) rotate(360deg);
    opacity: 0;
    filter: brightness(2);
  }
}

.tile.falling {
  animation: fall 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes fall {
  0% {
    transform: translateY(0) scale(0.9);
    opacity: 0.7;
  }
  50% {
    transform: translateY(0) scale(1.05);
  }
  100% {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
}

/* Star particles */
.star-particle {
  position: absolute;
  pointer-events: none;
  z-index: 100;
  font-size: 20px;
  animation: starBurst 0.6s ease-out forwards;
}

@keyframes starBurst {
  0% {
    transform: translate(0, 0) scale(0) rotate(0deg);
    opacity: 1;
  }
  50% {
    opacity: 1;
  }
  100% {
    transform: translate(var(--tx), var(--ty)) scale(1.5) rotate(360deg);
    opacity: 0;
  }
}

/* Path Overlay */
.path-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

.path-line {
  stroke: rgba(255, 255, 255, 0.6);
  stroke-width: 4;
  stroke-linecap: round;
  fill: none;
}

.path-line.overshoot {
  stroke: var(--accent-red);
}

.path-line.exact-match {
  stroke: var(--accent-green);
}

/* Right Panel Boxes */
.equation-box,
.instructions-box,
.history-box {
  background: var(--bg-panel);
  border: 1px solid #303560;
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.equation-box {
  border: 2px solid var(--accent-blue);
  box-shadow: 0 0 20px rgba(111, 168, 255, 0.2);
}

.equation-label {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
  text-align: center;
}

.equation-display {
  font-family: "Space Mono", monospace;
  font-size: 18px;
  color: var(--text);
  text-align: center;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #141938;
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 8px;
  line-height: 1.4;
  word-break: break-word;
}

.product-display {
  font-family: "Space Mono", monospace;
  font-size: 28px;
  font-weight: 900;
  color: var(--accent-yellow);
  text-align: center;
  text-shadow: 0 0 10px var(--accent-yellow);
}

.product-display.overshoot {
  color: var(--accent-red);
  text-shadow: 0 0 10px var(--accent-red);
}

.product-display.exact-match {
  color: var(--accent-green);
  text-shadow: 0 0 10px var(--accent-green);
}

/* Instructions */
.instructions-box h3 {
  margin: 0 0 12px;
  color: var(--accent-blue);
  font-size: 16px;
}

.instructions-box ul {
  margin: 0 0 16px;
  padding-left: 20px;
  list-style-type: none;
}

.instructions-box li {
  margin: 8px 0;
  padding: 8px;
  background: rgba(0, 162, 255, 0.05);
  border-radius: 6px;
  border-left: 3px solid var(--accent-blue);
  font-size: 13px;
  line-height: 1.5;
}

.instructions-box strong {
  color: var(--accent-pink);
}

.prime-colors {
  margin-top: 12px;
}

.color-guide {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.prime-badge {
  display: inline-block;
  padding: 8px 16px;
  border-radius: 8px;
  font-family: "Space Mono", monospace;
  font-weight: 900;
  font-size: 18px;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

.prime-badge.prime-2 {
  background: linear-gradient(135deg, var(--prime-2), #c92a2a);
}

.prime-badge.prime-3 {
  background: linear-gradient(135deg, var(--prime-3), #339af0);
}

.prime-badge.prime-5 {
  background: linear-gradient(135deg, var(--prime-5), #fab005);
  color: #000;
  text-shadow: 0 1px 3px rgba(255, 255, 255, 0.5);
}

.prime-badge.prime-7 {
  background: linear-gradient(135deg, var(--prime-7), #2f9e44);
}

.prime-badge.prime-11 {
  background: linear-gradient(135deg, #b794f6, #9775fa);
}

.prime-badge.prime-13 {
  background: linear-gradient(135deg, #ff8787, #fa5252);
}

.prime-badge.prime-17 {
  background: linear-gradient(135deg, #69db7c, #51cf66);
}

.prime-badge.prime-19 {
  background: linear-gradient(135deg, #ffa94d, #ff922b);
}

.prime-badge.prime-23 {
  background: linear-gradient(135deg, #8ce99a, #69db7c);
  color: #000;
  text-shadow: 0 1px 3px rgba(255, 255, 255, 0.5);
}

.prime-badge.prime-29 {
  background: linear-gradient(135deg, #ff6b9d, #e64980);
}

.prime-badge.prime-31 {
  background: linear-gradient(135deg, #9775fa, #845ef7);
}

.prime-badge.prime-37 {
  background: linear-gradient(135deg, #ff8c42, #fd7e14);
}

/* History */
.history-box h3 {
  margin: 0 0 8px;
  text-align: center;
  color: var(--accent-blue);
  font-size: 16px;
}

.history-list {
  margin: 0;
  padding-left: 20px;
  max-height: 200px;
  overflow-y: auto;
  overflow-x: hidden;
}

.history-list li {
  margin: 3px 0;
  padding: 4px 0;
  font-family: "Space Mono";
  font-size: 12px;
  line-height: 1.4;
  color: var(--muted);
}

.history-list li.success {
  color: var(--accent-green);
}

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  background: #0d0f1acc;
  backdrop-filter: blur(8px);
  z-index: 100;
}

.overlay.visible {
  display: grid;
}

.overlay .card {
  background: var(--bg-panel);
  border: 2px solid #303560;
  border-radius: 16px;
  padding: 32px;
  box-shadow: var(--shadow);
  color: #fff;
  max-width: 90vw;
  max-height: 85vh;
  overflow-y: auto;
}

.overlay .card h2 {
  margin: 0 0 24px;
  color: var(--accent-blue);
  font-size: 28px;
  text-align: center;
}

/* Instructions overlay */
.instructions {
  width: min(1000px, 90vw);
  min-height: 60vh;
}

.instructions-content {
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 10px;
}

.instructions-content h3 {
  color: var(--accent-pink);
  font-size: 18px;
  margin: 16px 0 8px;
}

.instructions-content h3:first-child {
  margin-top: 0;
}

.instructions-content p,
.instructions-content ul,
.instructions-content ol {
  margin: 8px 0;
  line-height: 1.6;
}

.instructions-content ul,
.instructions-content ol {
  padding-left: 24px;
}

.instructions-content li {
  margin: 6px 0;
}

.instructions-content strong {
  color: var(--accent-blue);
}

.countdown {
  margin-top: 16px;
  text-align: center;
  color: var(--accent-yellow);
  font-family: "Space Mono";
  font-size: 16px;
  font-weight: 700;
}

/* Setup overlay */
.setup {
  width: min(500px, 90vw);
  min-height: 50vh;
}

.setup form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.setup label {
  display: block;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
}

.setup input,
.setup select {
  width: 100%;
  padding: 12px;
  margin-top: 8px;
  border-radius: 8px;
  background: var(--bg-dark);
  color: var(--text);
  border: 1px solid #4a4f80;
  font-size: 14px;
  font-family: "Lexend", sans-serif;
}

.setup input:focus,
.setup select:focus {
  outline: none;
  border-color: var(--accent-blue);
  box-shadow: 0 0 10px rgba(111, 168, 255, 0.3);
}

.avatar-preview {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  border: 3px solid #4a4f80;
  overflow: hidden;
  background: #1a1d33;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 12px auto;
  cursor: pointer;
  transition: all 0.2s ease;
}

.avatar-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-preview:hover {
  border-color: var(--accent-purple);
  transform: scale(1.05);
}

.change-avatar-btn {
  padding: 10px 20px;
  border-radius: 8px;
  border: 2px solid var(--accent-purple);
  background: var(--bg-panel);
  color: var(--accent-purple);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
  display: block;
  margin: 12px auto 0;
  font-family: "Lexend", sans-serif;
}

.change-avatar-btn:hover {
  background: var(--accent-purple);
  color: var(--text);
}

button.primary {
  width: 100%;
  background: linear-gradient(45deg, var(--accent-blue), var(--accent-pink));
  border: none;
  color: #fff;
  padding: 14px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  text-shadow: 0 1px 3px #0005;
  transition: transform 0.2s;
  font-family: "Lexend", sans-serif;
}

button.primary:hover {
  transform: scale(1.02);
}

button.primary:active {
  transform: scale(0.98);
}

/* Avatar selection modal */
.avatar-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
}

.avatar-modal.hidden {
  display: none;
}

.avatar-modal-content {
  background: var(--bg-panel);
  border: 2px solid var(--accent-purple);
  border-radius: 16px;
  padding: 24px;
  max-width: 500px;
  width: 90%;
  box-shadow: var(--shadow);
}

.avatar-modal-content h3 {
  margin: 0 0 16px;
  color: var(--accent-purple);
  text-align: center;
}

.avatar-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 20px;
  max-height: 300px;
  overflow-y: auto;
}

.avatar-option {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 50%;
  border: 2px solid #4a4f80;
  cursor: pointer;
  transition: all 0.2s ease;
  overflow: hidden;
  background: #1a1d33;
  padding: 8px;
  position: relative;
}

.avatar-option img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-option:hover {
  border-color: var(--accent-purple);
  transform: scale(1.05);
}

.avatar-option.selected {
  border-color: var(--accent-green);
  box-shadow: 0 0 15px rgba(111, 255, 177, 0.5);
}

.avatar-option .checkmark {
  position: absolute;
  top: 4px;
  right: 4px;
  background: var(--accent-green);
  color: var(--bg-dark);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.avatar-modal-close {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: 2px solid #4a4f80;
  background: var(--bg-dark);
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: "Lexend", sans-serif;
}

.avatar-modal-close:hover {
  border-color: var(--accent-purple);
  background: var(--bg-panel);
}

/* Help overlay */
.help {
  width: min(900px, 92vw);
  max-height: 85vh;
  min-height: 60vh;
}

.help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.help-header h2 {
  margin: 0;
}

.help-content {
  max-height: 70vh;
  overflow-y: auto;
  padding-right: 10px;
}

.help-content h3 {
  margin: 20px 0 12px;
  color: var(--accent-pink);
  font-size: 18px;
}

.help-content h3:first-child {
  margin-top: 0;
}

.help-content ul {
  margin: 0 0 16px;
  padding-left: 24px;
  list-style-type: none;
}

.help-content li {
  margin: 10px 0;
  padding: 10px;
  background: rgba(0, 162, 255, 0.05);
  border-radius: 6px;
  border-left: 3px solid var(--accent-blue);
  font-size: 14px;
  line-height: 1.5;
}

.help-content strong {
  color: var(--accent-blue);
}

.close-btn {
  background: #2a2d4a;
  border: 2px solid #4a4f80;
  color: #fff;
  border-radius: 8px;
  font-size: 28px;
  width: 40px;
  height: 40px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding: 0;
  font-family: Arial, sans-serif;
}

.close-btn:hover {
  background: #38406c;
  border-color: var(--accent-red);
  color: var(--accent-red);
}

/* Responsive */
@media (max-width: 1200px) {
  .layout {
    grid-template-columns: 1fr 340px;
  }
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  body {
    overflow: auto;
  }

  .right-panel {
    max-height: none;
  }

  .game-area {
    min-height: 500px;
  }
}

/* Scrollbar styling */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: #1a1d33;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: #4a4f80;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #6fa8ff;
}
