:root {
  color-scheme: dark;
  --bg: #111827;
  --ink: #f8fafc;
  --muted: #9fb0c8;
  --panel: rgba(20, 29, 47, 0.92);
  --panel-strong: #1d293d;
  --line: rgba(255, 255, 255, 0.13);
  --accent: #42d6a4;
  --accent-strong: #14b887;
  --warn: #f6b860;
  --tile-radius: 8px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background:
    linear-gradient(130deg, rgba(17, 24, 39, 0.88), rgba(23, 33, 52, 0.78)),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1200' height='800' viewBox='0 0 1200 800'%3E%3Cdefs%3E%3ClinearGradient id='a' x1='0' x2='1' y1='0' y2='1'%3E%3Cstop stop-color='%232d6cdf'/%3E%3Cstop offset='.48' stop-color='%231e8a72'/%3E%3Cstop offset='1' stop-color='%23bd6b46'/%3E%3C/linearGradient%3E%3Cpattern id='p' width='80' height='80' patternUnits='userSpaceOnUse'%3E%3Cpath d='M0 80V0h80' fill='none' stroke='%23ffffff' stroke-opacity='.15'/%3E%3Ccircle cx='40' cy='40' r='10' fill='%23ffffff' fill-opacity='.05'/%3E%3C/pattern%3E%3C/defs%3E%3Crect width='1200' height='800' fill='url(%23a)'/%3E%3Crect width='1200' height='800' fill='url(%23p)'/%3E%3Cpath d='M0 620c150-72 254-23 378-66 150-52 220-168 404-112 156 48 224 160 418 92v266H0z' fill='%230b1220' fill-opacity='.55'/%3E%3C/svg%3E");
  background-size: cover;
  background-position: center;
  color: var(--ink);
}

button,
select {
  font: inherit;
}

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #243149;
  color: var(--ink);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.48rem;
  padding: 0.72rem 0.92rem;
  transition: transform 140ms ease, border-color 140ms ease, background 140ms ease;
}

button:hover {
  transform: translateY(-1px);
  border-color: rgba(255, 255, 255, 0.28);
  background: #2b3b58;
}

button:active {
  transform: translateY(0);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
  transform: none;
}

button svg {
  width: 1.08rem;
  height: 1.08rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: 0 0 auto;
}

.app {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: clamp(1rem, 3vw, 2.5rem);
}

.game-shell {
  width: min(1120px, 100%);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.eyebrow {
  margin: 0 0 0.35rem;
  color: var(--accent);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(2.4rem, 8vw, 5.4rem);
  line-height: 0.9;
}

.top-actions {
  display: flex;
  gap: 0.55rem;
}

.icon-button {
  width: 46px;
  padding: 0;
  border-radius: 50%;
}

.play-area {
  display: grid;
  grid-template-columns: minmax(280px, 1fr) minmax(280px, 350px);
  gap: clamp(1rem, 3vw, 1.4rem);
  align-items: start;
}

.board-wrap {
  position: relative;
}

.board-frame {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(13, 20, 34, 0.72);
  padding: clamp(0.55rem, 1.6vw, 0.8rem);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.34);
}

.board {
  --size: 4;
  width: min(70vh, 100%);
  max-width: 710px;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  grid-template-rows: repeat(var(--size), 1fr);
  gap: clamp(0.34rem, 1vw, 0.62rem);
  padding: clamp(0.34rem, 1vw, 0.62rem);
  border-radius: 8px;
  background: #0b1220;
  outline: none;
  overflow: hidden;
}

.board:focus-visible {
  box-shadow: 0 0 0 4px rgba(66, 214, 164, 0.35);
}

.tile,
.empty-tile {
  min-width: 0;
  min-height: 0;
  border-radius: var(--tile-radius);
  user-select: none;
}

.tile {
  position: relative;
  border: 0;
  padding: 0;
  min-height: auto;
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.22), rgba(255, 255, 255, 0.04)),
    var(--tile-bg, #2669c9);
  background-size: calc(var(--size) * 100%) calc(var(--size) * 100%);
  background-position: var(--tile-x) var(--tile-y);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.34), 0 8px 18px rgba(0, 0, 0, 0.22);
  color: #fff;
  font-weight: 900;
  font-size: clamp(1rem, 4.5vw, 2.6rem);
}

.tile::before {
  content: attr(data-number);
  position: absolute;
  inset: auto 0.42rem 0.32rem auto;
  min-width: 1.65rem;
  height: 1.65rem;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.56);
  color: #fff;
  font-size: clamp(0.72rem, 2.2vw, 0.92rem);
  line-height: 1;
}

.tile.correct {
  outline: 2px solid rgba(66, 214, 164, 0.8);
  outline-offset: -2px;
}

.tile.hint {
  animation: pulse 780ms ease-in-out 2;
  outline: 3px solid var(--warn);
  outline-offset: -3px;
}

.empty-tile {
  border: 1px dashed rgba(255, 255, 255, 0.2);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.08), transparent),
    rgba(255, 255, 255, 0.03);
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 1rem;
  box-shadow: 0 18px 56px rgba(0, 0, 0, 0.3);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.7rem;
}

.stat {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.045);
  padding: 0.82rem;
}

.stat span,
.progress span,
.control-group label {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stat strong {
  display: block;
  margin-top: 0.22rem;
  font-size: 1.5rem;
  line-height: 1.1;
}

.control-group {
  margin-top: 1rem;
}

select {
  width: 100%;
  min-height: 44px;
  margin-top: 0.42rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--ink);
  padding: 0 0.8rem;
}

.button-row {
  display: grid;
  grid-template-columns: 1.12fr 1fr 1fr;
  gap: 0.56rem;
  margin-top: 1rem;
}

.primary-button {
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #062016;
  border-color: transparent;
  font-weight: 900;
}

.primary-button:hover {
  background: linear-gradient(135deg, #6ae7bd, var(--accent));
}

.hint-box {
  min-height: 74px;
  margin-top: 1rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(6, 12, 24, 0.48);
  padding: 0.86rem;
  color: #d8e2f1;
  line-height: 1.45;
}

.progress {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.45rem 0.8rem;
  align-items: center;
  margin-top: 1rem;
}

.progress-track {
  grid-column: 1 / -1;
  height: 10px;
  border-radius: 999px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.1);
}

#progress-bar {
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--warn));
  border-radius: inherit;
  transition: width 180ms ease;
}

.complete-badge {
  position: absolute;
  inset: 50% auto auto 50%;
  transform: translate(-50%, -50%);
  width: min(320px, 80%);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  background: rgba(11, 18, 32, 0.92);
  padding: 1rem;
  text-align: center;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.4);
}

.complete-badge span {
  color: var(--accent);
  display: block;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.complete-badge strong {
  display: block;
  margin-top: 0.36rem;
  font-size: 1.1rem;
}

@keyframes pulse {
  50% {
    transform: scale(0.94);
    filter: brightness(1.25);
  }
}

body[data-theme="aurora"] .tile {
  --tile-bg:
    radial-gradient(circle at 18% 20%, #f8da7e 0 9%, transparent 10%),
    linear-gradient(135deg, #3178d9 0%, #1aa385 44%, #dd704b 100%);
}

body[data-theme="reef"] .tile {
  --tile-bg:
    radial-gradient(circle at 76% 22%, #ffe08a 0 7%, transparent 8%),
    linear-gradient(135deg, #0f766e 0%, #22b8cf 48%, #7367f0 100%);
}

body[data-theme="ember"] .tile {
  --tile-bg:
    radial-gradient(circle at 22% 78%, #ffd166 0 8%, transparent 9%),
    linear-gradient(135deg, #7c3aed 0%, #e11d48 43%, #f97316 100%);
}

body[data-muted="true"] .sound-wave {
  display: none;
}

@media (max-width: 860px) {
  .app {
    place-items: start center;
  }

  .topbar {
    align-items: flex-start;
  }

  .play-area {
    grid-template-columns: 1fr;
  }

  .board {
    width: 100%;
  }
}

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

  .topbar {
    gap: 0.75rem;
  }

  .button-row {
    grid-template-columns: 1fr;
  }

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

  .tile::before {
    inset: auto 0.24rem 0.22rem auto;
    min-width: 1.35rem;
    height: 1.35rem;
  }
}
