:root {
  --bg: #080b16;
  --bg-top: #1b2355;
  --panel: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.15);
  --text: #f2f5ff;
  --muted: #bcc5e2;
  --primary: #7f84ff;
  --primary-strong: #6268ff;
  --accent: #35d4bf;
  --danger: #ff5f8a;
  --cell-bg: rgba(255, 255, 255, 0.06);
  --cell-fixed: rgba(127, 132, 255, 0.28);
  --shadow: 0 15px 35px rgba(0, 0, 0, 0.35);
  --grid-gap: 8px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Inter", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: radial-gradient(circle at 20% 20%, var(--bg-top), var(--bg));
  color: var(--text);
  overflow-x: hidden;
}

body.theme-winx {
  --bg: #26103f;
  --bg-top: #e86aa8;
  --panel: rgba(247, 206, 234, 0.2);
  --border: rgba(255, 238, 248, 0.58);
  --text: #fff8ff;
  --muted: #f7e8ff;
  --primary: #f05ab3;
  --primary-strong: #8f67ff;
  --accent: #7ef6f6;
  --danger: #ff7398;
  --cell-bg: rgba(255, 239, 251, 0.2);
  --cell-fixed: rgba(238, 116, 196, 0.42);
  --shadow: 0 18px 44px rgba(45, 11, 53, 0.45);
}

body.theme-winx .orb-1 {
  background: #f36abc;
}

body.theme-winx .orb-2 {
  background: #66efff;
}

body.theme-winx .orb-3 {
  background: #b673ff;
}

body.theme-winx .winx-layer {
  opacity: 1;
}

body.theme-winx .winx-wing,
body.theme-winx .winx-charm,
body.theme-winx .winx-sprite {
  opacity: 1;
}

body.theme-winx .winx-banner {
  display: inline-flex;
}

.bg-orb {
  position: fixed;
  border-radius: 999px;
  filter: blur(60px);
  opacity: 0.45;
  z-index: -1;
}

.orb-1 {
  width: 320px;
  height: 320px;
  background: #8d4dff;
  top: -80px;
  left: -80px;
}

.orb-2 {
  width: 280px;
  height: 280px;
  background: #36d4bf;
  right: 0;
  top: 25%;
}

.orb-3 {
  width: 260px;
  height: 260px;
  background: #5560ff;
  bottom: -80px;
  left: 20%;
}

.winx-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: -1;
  opacity: 0;
  transition: opacity 0.35s ease;
}

.winx-stars {
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(circle at 14% 18%, rgba(255, 255, 255, 0.75) 0 2px, transparent 2.6px),
    radial-gradient(circle at 76% 24%, rgba(182, 247, 255, 0.85) 0 2px, transparent 2.6px),
    radial-gradient(circle at 68% 72%, rgba(255, 222, 247, 0.8) 0 2px, transparent 2.6px),
    radial-gradient(circle at 22% 82%, rgba(255, 255, 255, 0.75) 0 2px, transparent 2.6px);
  background-size: 220px 220px, 280px 280px, 260px 260px, 300px 300px;
  animation: winx-stars 8s linear infinite;
}

.winx-wing {
  position: absolute;
  width: min(30vw, 320px);
  height: min(36vh, 300px);
  opacity: 0;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  filter: drop-shadow(0 10px 24px rgba(255, 111, 199, 0.35));
}

.wing-left {
  left: -18px;
  top: 22%;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 260 220'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0' y1='0' x2='1' y2='1'%3E%3Cstop offset='0' stop-color='%23fff7ff'/%3E%3Cstop offset='1' stop-color='%2384f7ff'/%3E%3C/linearGradient%3E%3C/defs%3E%3Cpath d='M24 116C52 36 142 6 232 68C164 68 122 98 98 150C66 176 40 162 24 116Z' fill='url(%23g)' fill-opacity='.84'/%3E%3Cpath d='M36 108C74 56 140 44 212 84C144 82 106 106 88 142C60 158 44 144 36 108Z' fill='%23ffffff' fill-opacity='.38'/%3E%3C/svg%3E");
  animation: wing-float-left 3.2s ease-in-out infinite;
}

.wing-right {
  right: -18px;
  top: 20%;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 260 220'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0' y1='0' x2='1' y2='1'%3E%3Cstop offset='0' stop-color='%23fff7ff'/%3E%3Cstop offset='1' stop-color='%23ff8ed4'/%3E%3C/linearGradient%3E%3C/defs%3E%3Cpath d='M236 116C208 36 118 6 28 68C96 68 138 98 162 150C194 176 220 162 236 116Z' fill='url(%23g)' fill-opacity='.84'/%3E%3Cpath d='M224 108C186 56 120 44 48 84C116 82 154 106 172 142C200 158 216 144 224 108Z' fill='%23ffffff' fill-opacity='.38'/%3E%3C/svg%3E");
  animation: wing-float-right 3.4s ease-in-out infinite;
}

.winx-charm {
  position: absolute;
  font-size: clamp(1.25rem, 1.7vw, 1.8rem);
  opacity: 0;
  text-shadow: 0 8px 20px rgba(255, 111, 199, 0.35);
}

.winx-sprite {
  position: absolute;
  width: 144px;
  height: auto;
  opacity: 0;
  pointer-events: none;
  object-fit: contain;
  left: 0;
  top: 0;
  transform: translate(var(--from-x, -18vw), var(--from-y, 20vh)) rotate(var(--rot-a, 0deg));
  filter: drop-shadow(0 9px 16px rgba(255, 120, 198, 0.22));
  animation-name: winx-sprite-drift, winx-sprite-pulse;
  animation-timing-function: linear, ease-in-out;
  animation-iteration-count: infinite, infinite;
}

.charm-a {
  left: 8%;
  bottom: 20%;
  animation: charm-float 4.6s ease-in-out infinite;
}

.charm-b {
  right: 9%;
  bottom: 24%;
  animation: charm-float 4.2s ease-in-out infinite 0.35s;
}

.charm-c {
  right: 45%;
  top: 10%;
  animation: charm-float 4.8s ease-in-out infinite 0.2s;
}

.app {
  width: min(960px, 94vw);
  margin: 30px auto;
  display: grid;
  gap: 18px;
}

.glass {
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
  border-radius: 18px;
}

.hero {
  padding: 20px;
  display: grid;
  gap: 18px;
  grid-template-columns: 1.5fr 1fr;
}

.eyebrow {
  margin: 0;
  color: var(--accent);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.75rem;
}

h1 {
  margin: 6px 0;
  font-size: clamp(1.7rem, 4vw, 2.4rem);
}

.subtitle {
  margin: 0;
  color: var(--muted);
  max-width: 55ch;
}

.winx-banner {
  margin-top: 12px;
  width: fit-content;
  display: none;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  background: linear-gradient(120deg, rgba(236, 109, 183, 0.28), rgba(143, 103, 255, 0.25));
  box-shadow: 0 10px 28px rgba(236, 109, 183, 0.22);
  color: #fff8ff;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  gap: 8px;
  align-items: center;
}

.winx-banner strong {
  color: #ffe6fa;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(70px, 1fr));
  gap: 10px;
}

.stat-card {
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 12px;
  padding: 10px;
  text-align: center;
}

.stat-card span {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
}

.stat-card strong {
  font-size: 1.25rem;
}

.panel {
  padding: 16px;
  display: grid;
  gap: 12px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

label {
  font-size: 0.9rem;
  color: var(--muted);
}

select,
.btn {
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
  padding: 10px 12px;
  font-weight: 600;
}

select {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background: rgba(10, 14, 34, 0.8);
  border-color: rgba(255, 255, 255, 0.34);
  color: #ffffff;
  font-weight: 700;
  padding-right: 38px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), 0 6px 16px rgba(0, 0, 0, 0.25);
  background-image:
    linear-gradient(45deg, transparent 50%, rgba(255, 255, 255, 0.92) 50%),
    linear-gradient(135deg, rgba(255, 255, 255, 0.92) 50%, transparent 50%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0));
  background-position: calc(100% - 16px) calc(50% - 2px), calc(100% - 10px) calc(50% - 2px),
    center;
  background-size: 6px 6px, 6px 6px, 100% 100%;
  background-repeat: no-repeat;
  transition: border-color 0.22s ease, box-shadow 0.22s ease, transform 0.12s ease;
}

select:hover {
  border-color: rgba(255, 255, 255, 0.56);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.16), 0 8px 18px rgba(0, 0, 0, 0.3);
}

select:focus {
  transform: translateY(-1px);
  border-color: rgba(126, 246, 246, 0.9);
  box-shadow: 0 0 0 3px rgba(126, 246, 246, 0.22), 0 10px 22px rgba(0, 0, 0, 0.33);
}

select option {
  background: #161b38;
  color: #ffffff;
}

body.theme-winx select {
  background: rgba(56, 24, 78, 0.95);
  border-color: rgba(255, 230, 245, 0.55);
  color: #fff8ff;
  box-shadow: 0 4px 12px rgba(36, 10, 53, 0.35);
  background-image:
    linear-gradient(45deg, transparent 50%, rgba(255, 240, 250, 0.95) 50%),
    linear-gradient(135deg, rgba(255, 240, 250, 0.95) 50%, transparent 50%);
  background-size: 6px 6px, 6px 6px;
  background-position: calc(100% - 16px) calc(50% - 2px), calc(100% - 10px) calc(50% - 2px);
  background-repeat: no-repeat;
}

body.theme-winx select:hover {
  border-color: rgba(255, 236, 248, 0.75);
  box-shadow: 0 6px 14px rgba(36, 10, 53, 0.42);
}

body.theme-winx select:focus {
  border-color: rgba(161, 250, 250, 0.9);
  box-shadow: 0 0 0 3px rgba(161, 250, 250, 0.2), 0 8px 18px rgba(36, 10, 53, 0.45);
}

body.theme-winx select option {
  background: #4c2467;
  color: #fff6ff;
}

select:focus,
.btn:focus,
.cell:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn {
  cursor: pointer;
  transition: transform 0.1s ease, background-color 0.2s ease;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn.primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-strong));
  border: none;
}

.btn.danger {
  background: rgba(255, 95, 138, 0.2);
  border-color: rgba(255, 95, 138, 0.4);
}

.theme-btn {
  padding: 6px 10px;
  min-width: 56px;
}

.theme-btn-image {
  display: block;
  height: 30px;
  width: auto;
  object-fit: contain;
}

.message {
  min-height: 22px;
  color: var(--muted);
  font-weight: 500;
}

.message.success {
  color: var(--accent);
}

.message.error {
  color: #ff9db8;
}

.board-wrap {
  overflow: auto;
  padding: 4px;
}

.board {
  display: grid;
  gap: var(--grid-gap);
  justify-content: center;
}

.cell {
  width: clamp(36px, 6.2vw, 52px);
  aspect-ratio: 1;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 10px;
  background: var(--cell-bg);
  color: var(--text);
  font-size: clamp(1.05rem, 2.2vw, 1.4rem);
  font-weight: 800;
  display: grid;
  place-items: center;
  user-select: none;
  transition: transform 0.08s ease, border-color 0.2s ease, background-color 0.2s ease;
}

.cell:hover {
  transform: translateY(-1px);
  border-color: rgba(255, 255, 255, 0.32);
}

.cell.fixed {
  background: var(--cell-fixed);
  border-color: rgba(127, 132, 255, 0.55);
}

.cell.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(53, 212, 191, 0.3);
}

.cell.invalid {
  background: rgba(255, 95, 138, 0.18);
  border-color: rgba(255, 95, 138, 0.65);
}

body.theme-winx .cell[data-value="0"],
body.theme-winx .cell[data-value="1"] {
  color: transparent;
  text-shadow: none;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 76% 76%;
}

body.theme-winx .cell[data-value="0"] {
  background-image: url("wings/0.png");
}

body.theme-winx .cell[data-value="1"] {
  background-image: url("wings/1.png");
}

body.theme-winx .cell[data-value="-1"] {
  background-image: none;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  color: var(--muted);
  font-size: 0.9rem;
}

kbd {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 6px;
  padding: 2px 7px;
}

.loading {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  gap: 10px;
  background: rgba(8, 11, 22, 0.8);
  backdrop-filter: blur(4px);
}

.celebration {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(8, 11, 22, 0.5);
  backdrop-filter: blur(3px);
  overflow: hidden;
}

.celebration.hidden {
  display: none;
}

.celebration-card {
  width: min(88vw, 460px);
  padding: 28px 24px;
  text-align: center;
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: linear-gradient(150deg, rgba(127, 132, 255, 0.34), rgba(53, 212, 191, 0.24));
  box-shadow: 0 20px 55px rgba(0, 0, 0, 0.45), 0 0 45px rgba(127, 132, 255, 0.25);
  animation: celebration-pop 550ms cubic-bezier(0.18, 0.9, 0.22, 1.05);
}

.celebration-badge {
  width: 62px;
  height: 62px;
  margin: 0 auto 14px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  font-size: 1.8rem;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.4);
  animation: badge-float 1.2s ease-in-out infinite;
}

.celebration-card h2 {
  margin: 0;
  font-size: clamp(1.5rem, 3.6vw, 2.1rem);
}

.celebration-card p {
  margin: 10px 0 0;
  color: #e4ecff;
  font-size: 1rem;
}

.burst {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 999px;
  background: radial-gradient(circle at 30% 30%, #ffffff, var(--accent));
  opacity: 0;
  pointer-events: none;
}

.burst-a {
  left: 50%;
  top: 50%;
  animation: burst-a 900ms ease-out forwards;
}

.burst-b {
  left: 50%;
  top: 50%;
  animation: burst-b 900ms ease-out forwards;
}

.burst-c {
  left: 50%;
  top: 50%;
  animation: burst-c 900ms ease-out forwards;
}

.burst-d {
  left: 50%;
  top: 50%;
  animation: burst-d 900ms ease-out forwards;
}

.loading.hidden {
  display: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes winx-stars {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-30px);
  }
}

@keyframes wing-float-left {
  0%,
  100% {
    transform: translateY(0) rotate(-3deg);
  }
  50% {
    transform: translateY(-10px) rotate(-1deg);
  }
}

@keyframes wing-float-right {
  0%,
  100% {
    transform: translateY(0) rotate(3deg);
  }
  50% {
    transform: translateY(-10px) rotate(1deg);
  }
}

@keyframes charm-float {
  0%,
  100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-9px) scale(1.08);
  }
}

@keyframes winx-sprite-drift {
  0% {
    transform: translate(var(--from-x, -18vw), var(--from-y, 20vh)) rotate(var(--rot-a, 0deg));
  }
  50% {
    transform: translate(
        calc((var(--from-x, -18vw) + var(--to-x, 118vw)) / 2),
        calc((var(--from-y, 20vh) + var(--to-y, 30vh)) / 2)
      )
      rotate(var(--rot-b, 10deg));
  }
  100% {
    transform: translate(var(--to-x, 118vw), var(--to-y, 30vh)) rotate(var(--rot-c, 0deg));
  }
}

@keyframes winx-sprite-pulse {
  0%,
  100% {
    opacity: 0.22;
  }
  50% {
    opacity: 0.44;
  }
}

@keyframes celebration-pop {
  0% {
    transform: translateY(20px) scale(0.86);
    opacity: 0;
  }
  70% {
    transform: translateY(-4px) scale(1.03);
    opacity: 1;
  }
  100% {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
}

@keyframes badge-float {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

@keyframes burst-a {
  0% {
    opacity: 0.95;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-180px, -130px) scale(0.3);
  }
}

@keyframes burst-b {
  0% {
    opacity: 0.95;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(170px, -110px) scale(0.25);
  }
}

@keyframes burst-c {
  0% {
    opacity: 0.95;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-150px, 120px) scale(0.25);
  }
}

@keyframes burst-d {
  0% {
    opacity: 0.95;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(180px, 130px) scale(0.25);
  }
}

@media (max-width: 780px) {
  .hero {
    grid-template-columns: 1fr;
  }
}
