:root {
  color-scheme: light;
  --paper: #dce8da;
  --ink: #17251d;
  --grid: #32473a;
  --accent: #f0f4e7;
  font-family: "Aldrich", sans-serif;
  font-weight: 400;
}

@property --piece-angle {
  syntax: "<angle>";
  inherits: true;
  initial-value: 0deg;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  min-height: 560px;
  margin: 0;
  overflow: hidden;
}

body {
  display: grid;
  place-items: center;
  background: #101512;
  color: var(--ink);
}

body[data-state="invalid"] {
  --paper: #591b1d;
  --ink: #f2d8d1;
  --grid: #efc5ba;
  --accent: #321012;
}

button {
  font: inherit;
}

.game {
  position: relative;
  isolation: isolate;
  width: min(480px, 100vw);
  height: min(600px, 100vh);
  min-height: 560px;
  overflow: hidden;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  background-color: var(--paper);
  transition: background-color 240ms ease, color 240ms ease;
}

.game::after {
  content: "";
  position: absolute;
  z-index: -1;
  pointer-events: none;
}

.game::after {
  inset: 12px;
  border: 2px solid color-mix(in oklch, var(--ink) 52%, transparent);
}

.level {
  position: absolute;
  top: 47px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 21px;
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.16em;
  white-space: nowrap;
}

.level::before,
.level::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 54px;
  height: 2px;
  background: currentColor;
  opacity: 0.55;
}

.level::before {
  right: calc(100% + 15px);
}

.level::after {
  left: calc(100% + 12px);
}

.playfield {
  position: absolute;
  inset: 108px 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.board-wrap {
  position: relative;
  display: grid;
  place-items: center;
  width: 356px;
  height: 356px;
}

.board {
  --size: 1;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  width: 340px;
  height: 340px;
  border: 3px solid var(--grid);
  background: color-mix(in oklch, var(--paper) 92%, var(--accent));
  box-shadow: 7px 7px 0 color-mix(in oklch, var(--ink) 14%, transparent);
  transition: border-color 240ms ease, background-color 240ms ease, box-shadow 240ms ease;
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  padding: 0;
  appearance: none;
  border: 0;
  border-right: 2px solid var(--grid);
  border-bottom: 2px solid var(--grid);
  border-radius: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.cell.is-last-column {
  border-right: 0;
}

.cell.is-last-row {
  border-bottom: 0;
}

.cell:focus-visible {
  outline: 3px double currentColor;
  outline-offset: -6px;
}

.cell:not(:disabled):hover {
  background: color-mix(in oklch, var(--ink) 7%, transparent);
}

.cell:disabled {
  background: transparent;
  cursor: not-allowed;
}

.cell::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: calc(141.421% - 5px);
  height: clamp(2px, calc(7px - var(--size) * 0.65px), 5px);
  border-radius: 999px;
  background: currentColor;
  opacity: 0;
  transform-origin: center;
  transition: opacity 120ms ease, transform 180ms cubic-bezier(0.22, 1, 0.36, 1);
}

.cell[data-line="1"]::before {
  opacity: 1;
  transform: translate(-50%, -50%) rotate(45deg) scaleX(0.96);
}

.cell[data-line="2"]::before {
  opacity: 1;
  transform: translate(-50%, -50%) rotate(-45deg) scaleX(0.96);
}

.complete {
  position: absolute;
  inset: 8px;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 29px;
  background: color-mix(in oklch, var(--paper) 92%, transparent);
  opacity: 0;
  pointer-events: none;
  transform: scale(0.97);
  transition:
    opacity 320ms ease,
    transform 480ms cubic-bezier(0.16, 1, 0.3, 1);
}

.complete.is-visible {
  opacity: 1;
  pointer-events: auto;
  transform: scale(1);
}

.complete-title {
  font-size: 31px;
  font-weight: 400;
  letter-spacing: 0.09em;
}

.next,
.again {
  position: relative;
  padding: 12px 18px 11px 20px;
  border: 2px solid currentColor;
  border-radius: 0;
  background: transparent;
  color: inherit;
  font-size: 14px;
  font-weight: 400;
  letter-spacing: 0.14em;
  cursor: pointer;
  transition: color 140ms ease, background-color 140ms ease, transform 140ms ease;
}

.next:hover,
.next:focus-visible,
.again:hover,
.again:focus-visible {
  background: var(--ink);
  color: var(--paper);
  outline: none;
}

.next:active,
.again:active {
  transform: translateY(2px);
}

.pieces {
  --piece-angle: 0deg;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  width: 232px;
  min-height: 72px;
  padding: 21px 0 8px;
  animation: piece-clock 2.6s linear infinite;
}

.piece {
  display: block;
  width: 16px;
  height: 2px;
  border-radius: 999px;
  background: currentColor;
  transform: rotate(var(--piece-angle));
  transition: background-color 240ms ease;
}

.win {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  background: var(--paper);
  opacity: 0;
  pointer-events: none;
  transition: opacity 600ms ease;
}

.win.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.win-stage {
  display: grid;
  justify-items: center;
  gap: 48px;
  opacity: 0;
  transform: translateY(12px) scale(0.96);
  transition:
    opacity 500ms cubic-bezier(0.16, 1, 0.3, 1) 160ms,
    transform 500ms cubic-bezier(0.16, 1, 0.3, 1) 160ms;
}

.win.is-visible .win-stage {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.win-title {
  display: flex;
  align-items: baseline;
  justify-content: center;
  font-size: 50px;
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.035em;
}

.win-letter {
  display: inline-block;
  min-width: 0.62em;
  color: var(--ink);
  text-align: center;
  transform-origin: 50% 80%;
  animation: letter-party 0.82s cubic-bezier(0.25, 1, 0.5, 1) infinite;
  animation-delay: calc(var(--distance) * -0.055s);
  animation-play-state: paused;
}

.win.is-visible .win-letter {
  animation-play-state: running;
}

.win-space {
  width: 0.32em;
}

.win-mark {
  transform-origin: 50% 70%;
}

.again {
  min-width: 132px;
  padding-inline: 18px;
  background: transparent;
}

@keyframes piece-clock {
  to {
    --piece-angle: 360deg;
  }
}

@keyframes letter-party {
  0%,
  100% {
    transform: translateY(0) scale(1);
  }

  38% {
    transform: translateY(-10px) scale(1.045);
  }

  68% {
    transform: translateY(1px) scale(0.995);
  }
}

@media (max-width: 390px), (max-height: 580px) {
  .level {
    top: 39px;
  }

  .playfield {
    inset: 90px 0 0;
  }

  .board-wrap {
    width: min(326px, 90vw);
    height: min(326px, 90vw);
  }

  .board {
    width: min(310px, calc(90vw - 16px));
    height: min(310px, calc(90vw - 16px));
  }

  .pieces {
    width: min(232px, 90vw);
    padding-top: 17px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }

  .pieces,
  .win-letter {
    animation: none !important;
  }
}
