:root {
  --bg: #1f2120;
  --panel: #181c29;
  --text: #fbf3e4;
  --muted: #d9cfbd;
  --violet: #7c47f5;
  --cyan: #27d6c1;
  --yellow: #facc15;
  --pink: #fb7185;
  --lime: #a3e635;
  --orange: #fb923c;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  background:
    radial-gradient(100% 70% at 0% 0%, rgb(251 146 60 / 0.18), transparent 60%),
    radial-gradient(90% 70% at 100% 100%, rgb(251 113 133 / 0.18), transparent 60%),
    var(--bg);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.hidden {
  display: none !important;
}

/* ---------- Layout: portrait = column; landscape = board + side column ---------- */

.app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto auto;
  align-content: center;
  grid-template-areas:
    "hud"
    "stage"
    "palette";
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hud {
  grid-area: hud;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 12px 4px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 74px;
  padding: 4px 12px 5px;
  border-radius: 14px;
  background: rgb(255 255 255 / 0.07);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 0.06);
}

.label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat strong {
  font-size: 20px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.stat-moves {
  min-width: 90px;
}

.meter {
  display: block;
  width: 100%;
  height: 4px;
  margin-top: 3px;
  border-radius: 999px;
  background: rgb(255 255 255 / 0.12);
  overflow: hidden;
}

.meter span {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: var(--lime);
  transform-origin: left center;
  transition:
    transform 0.25s ease,
    background-color 0.25s ease;
}

.meter span.low {
  background: var(--pink);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: rgb(255 255 255 / 0.1);
  color: var(--text);
  cursor: pointer;
}

.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

.icon-btn:hover {
  background: rgb(255 255 255 / 0.16);
}

.stage {
  grid-area: stage;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  padding: 8px 12px;
  touch-action: none;
}

#board {
  display: block;
  width: 300px;
  height: 300px;
  border-radius: 18px;
  box-shadow: 0 18px 40px rgb(0 0 0 / 0.4);
  cursor: pointer;
  touch-action: none;
}

#board.enter {
  animation: board-in 0.45s cubic-bezier(0.3, 1.4, 0.5, 1);
}

@keyframes board-in {
  from {
    opacity: 0;
    transform: scale(0.85) rotate(-3deg);
  }
}

#board.shake {
  animation: shake 0.3s ease;
}

@keyframes shake {
  25% {
    transform: translateX(-5px);
  }
  50% {
    transform: translateX(4px);
  }
  75% {
    transform: translateX(-2px);
  }
}

.palette {
  grid-area: palette;
  display: grid;
  grid-template-columns: repeat(6, minmax(48px, 72px));
  justify-content: center;
  gap: 8px;
  padding: 6px 12px 16px;
}

.swatch {
  position: relative;
  display: grid;
  place-items: center;
  height: 58px;
  padding: 0;
  border: 0;
  border-radius: 18px;
  background: var(--c);
  color: #1f2120;
  cursor: pointer;
  box-shadow:
    inset 0 -5px 0 rgb(0 0 0 / 0.2),
    0 4px 0 rgb(0 0 0 / 0.35);
  transition:
    transform 0.1s ease,
    opacity 0.2s ease,
    box-shadow 0.1s ease;
}

.swatch svg {
  width: 52%;
  max-width: 30px;
  height: auto;
  fill: rgb(31 33 32 / 0.62);
}

.swatch .key {
  position: absolute;
  top: 4px;
  left: 7px;
  font-size: 11px;
  font-weight: 900;
  color: rgb(31 33 32 / 0.6);
}

.swatch:hover {
  transform: translateY(-2px);
}

.swatch:active {
  transform: translateY(2px);
  box-shadow:
    inset 0 -3px 0 rgb(0 0 0 / 0.2),
    0 1px 0 rgb(0 0 0 / 0.35);
}

.swatch.is-current {
  opacity: 0.4;
  cursor: default;
  transform: none;
  box-shadow:
    inset 0 0 0 3px rgb(255 255 255 / 0.85),
    0 0 0 rgb(0 0 0 / 0);
}

.swatch.nope {
  animation: shake 0.3s ease;
}

.swatch:focus {
  outline: none;
}

.swatch:focus-visible {
  outline: 3px solid #fff;
  outline-offset: 3px;
}

@media (max-width: 400px) and (max-aspect-ratio: 5/4) {
  .palette {
    grid-template-columns: repeat(6, minmax(48px, 1fr));
    gap: 6px;
    padding: 6px 8px 14px;
  }

  .hud {
    gap: 6px;
    padding: 10px 8px 4px;
  }

  .stat {
    min-width: 0;
    padding: 4px 9px 5px;
  }
}

/* Too narrow for six 48px swatches in a row: use 3 × 2. */
@media (max-width: 339px) and (max-aspect-ratio: 5/4) {
  .palette {
    grid-template-columns: repeat(3, minmax(48px, 1fr));
    gap: 8px;
    padding: 6px 12px 12px;
  }

  .swatch {
    height: 52px;
  }
}

@media (min-aspect-ratio: 5/4) {
  .app {
    grid-template-columns: auto clamp(210px, 26vw, 300px);
    grid-template-rows: minmax(0, 1fr) auto auto minmax(0, 1fr);
    grid-template-areas:
      "stage ."
      "stage hud"
      "stage palette"
      "stage .";
    justify-content: center;
    align-content: stretch;
    column-gap: 8px;
  }

  .stage {
    padding: 12px;
  }

  .hud {
    flex-wrap: wrap;
    padding: 0 12px 6px 0;
  }

  .stat {
    min-width: 0;
    flex: 1 1 30%;
    padding: 4px 6px 5px;
  }

  .palette {
    grid-template-columns: repeat(3, minmax(48px, 1fr));
    padding: 6px 12px 0 0;
  }

  .swatch {
    height: clamp(48px, 10vh, 70px);
  }
}

/* ---------- Overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  padding: 16px;
  overflow-y: auto;
  background: rgb(31 33 32 / 0.82);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
}

.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: min(440px, 100%);
  margin: auto;
  padding: clamp(18px, 4vmin, 30px);
  border-radius: 26px;
  text-align: center;
  background: linear-gradient(180deg, #1f2436, var(--panel));
  box-shadow:
    inset 0 0 0 1px rgb(255 255 255 / 0.08),
    0 24px 60px rgb(0 0 0 / 0.45);
}

.panel h1,
.panel h2 {
  margin: 0;
  font-size: clamp(28px, 7vmin, 46px);
  line-height: 1.05;
}

.panel h1 {
  background: linear-gradient(90deg, var(--pink), var(--orange) 30%, var(--yellow) 50%, var(--lime) 70%, var(--cyan));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.panel p {
  margin: 0;
}

.goal {
  color: #d5dae4;
  font-size: 17px;
  line-height: 1.35;
}

.hint {
  color: var(--muted);
  font-size: 14px;
}

.best-line {
  color: var(--muted);
  font-size: 15px;
}

.best-line strong {
  color: var(--text);
}

.stars {
  display: flex;
  gap: 6px;
  margin: 0;
  font-size: 34px;
  line-height: 1;
}

.stars span {
  color: var(--yellow);
}

.stars span.off {
  color: #5b6275;
}

.new-best {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--yellow);
  color: #1a1408;
  font-size: 13px;
  font-weight: 800;
}

.near-miss {
  margin: 4px 0 2px;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(251, 146, 60, 0.2);
  border: 1px solid rgba(251, 146, 60, 0.45);
  color: #fed7aa;
  font-size: 14px;
  font-weight: 700;
  animation: pop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.swatches {
  display: grid;
  grid-template-columns: repeat(6, 34px);
  gap: 6px;
}

.swatches span {
  display: grid;
  place-items: center;
  height: 34px;
  border-radius: 10px;
  background: var(--c);
  animation: pop 0.5s cubic-bezier(0.3, 1.6, 0.5, 1) backwards;
}

.swatches span:nth-child(2) { animation-delay: 0.05s; }
.swatches span:nth-child(3) { animation-delay: 0.1s; }
.swatches span:nth-child(4) { animation-delay: 0.15s; }
.swatches span:nth-child(5) { animation-delay: 0.2s; }
.swatches span:nth-child(6) { animation-delay: 0.25s; }

.swatches svg {
  width: 18px;
  height: 18px;
  fill: rgb(31 33 32 / 0.6);
}

@keyframes pop {
  from {
    opacity: 0;
    transform: scale(0.3);
  }
}

.summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  width: 100%;
}

.summary-2 {
  grid-template-columns: 1fr 1fr;
}

.summary > div {
  display: flex;
  flex-direction: column;
  padding: 8px 4px;
  border-radius: 14px;
  background: rgb(255 255 255 / 0.06);
}

.summary strong {
  font-size: 24px;
  font-variant-numeric: tabular-nums;
}

.btn {
  min-width: 160px;
  min-height: 52px;
  padding: 10px 26px;
  border: 0;
  border-radius: 999px;
  background: #ee5e24;
  color: #15130f;
  font: inherit;
  font-size: 20px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 5px 0 #b8400f;
  transition:
    transform 0.08s ease,
    box-shadow 0.08s ease,
    filter 0.15s ease;
}

.btn:hover {
  filter: brightness(1.1);
}

.btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #b8400f;
}

.btn:focus,
.icon-btn:focus {
  outline: none;
}

.btn:focus-visible,
.icon-btn:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 3px;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  width: 100%;
}

.btn-row .btn {
  flex: 1 1 150px;
  min-width: 0;
  padding-left: 16px;
  padding-right: 16px;
}

.btn.btn-alt {
  background: #363836;
  box-shadow: 0 5px 0 #46443e, inset 0 0 0 1px #3a4250;
}

.btn.btn-alt:active {
  box-shadow: 0 2px 0 #46443e, inset 0 0 0 1px #3a4250;
}

@media (max-height: 460px) {
  .panel {
    gap: 8px;
    padding: 14px 22px;
  }

  .swatches {
    display: none;
  }

  .goal {
    font-size: 15px;
  }

  .summary strong {
    font-size: 20px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #board.enter,
  #board.shake,
  .swatch.nope,
  .swatches span {
    animation: none;
  }
}
