*,
*::before,
*::after {
  box-sizing: border-box;
}

* {
  margin: 0;
}

body {
  --text: light-dark(darkslategray, slategray);
  --accent: light-dark(teal, pink);
  --surface: light-dark(white, #123);

  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  color-scheme: light dark;
  color: var(--text);
  touch-action: none;
  user-select: none;
  background-color: var(--surface);
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;

  @media (min-width: 600px) {
    user-select: text;
  }
}

img,
picture,
video,
canvas,
svg {
  display: block;
  max-width: 100%;
}

input,
button,
textarea,
select {
  font: inherit;
}

p,
h1,
h2,
h3,
h4,
h5,
h6 {
  overflow-wrap: break-word;
}

p {
  text-wrap: pretty;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  text-wrap: balance;
}

body > main {
  isolation: isolate;
}

main {
  --max-width: 67.5rem;
  --inline-padding: 1rem;

  --_side-col: minmax(var(--inline-padding), 1fr);
  --_content-col: [content-start] minmax(0, var(--max-width)) [content-end];

  display: grid;
  grid-template-columns:
    [breakout-start] var(--_side-col) var(--_content-col) var(--_side-col)
    [breakout-end];
  place-items: center;
  text-align: center;
  gap: 2rem 0;
  margin-block-start: 2rem;
  padding-block: 1rem;

  > * {
    grid-column: content;
  }

  > .breakout {
    grid-column: breakout;
  }

  > .wider {
    grid-column: wider;
  }
}

dialog {
  &[open] {
    width: 100%;
    max-height: 20ch;
    max-width: 40ch;
    margin: auto;
    text-align: center;
    border: 0;
    background-color: var(--surface);
    border-radius: 1rem;
    display: grid;
    gap: 0.5rem;

    > form {
      margin-block-start: 1rem;
    }

    &::backdrop {
      background-color: rgb(0 0 0 / 0.5);
    }
  }
}

a {
  color: var(--accent);

  &:focus-visible {
    outline-width: 0.5px;
    outline-offset: 0.2em;
    outline-color: var(--accent);
  }
}

button {
  padding: 0.5em 1.5em;
  border-radius: 100vw;
  font-size: 1.2rem;
  border: 0.1em solid color(from currentColor srgb r g b / 0.4);
  min-width: 7em;
  text-transform: uppercase;
  font-weight: 300;
  color: inherit;
  background-color: transparent;
  backdrop-filter: blur(1px);
  cursor: pointer;
  transition:
    scale ease-in 0.1s,
    background-color ease-out 0.2s;

  &:hover {
    background-color: color(from currentColor srgb r g calc(b + 0.1) / 0.1);
  }

  &:active {
    scale: 0.98;
  }

  &:focus-visible {
    outline-offset: 0.4em;
    outline-color: var(--accent);
  }

  &.compact {
    font-size: 1rem;
    padding: 0.5em 1.25em;
  }
}

footer {
  margin-block-start: 2rem;
  max-width: 40ch;
  margin-inline: auto;
  text-align: center;
  padding-inline: 1rem;
  font-size: 0.75rem;

  @media (min-width: 600px) {
    font-size: 1rem;
  }
}

p {
  text-align: center;
  max-width: 40ch;
}

h1 {
  font-size: 2rem;
  text-transform: lowercase;
  font-weight: 300;
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
}

u {
  text-decoration-color: currentColor;
  text-decoration-thickness: 0.1em;
  text-decoration-style: wavy;
  text-underline-offset: 0.2em;
  text-decoration-skip-ink: none;
}

.tutorial {
  font-size: 0.75rem;

  @media (min-width: 600px) {
    font-size: 1rem;
  }
}

#high-score-info {
  color: color(from currentColor xyz x calc(y + 0.08) z);

  &:has(#high-score:empty) {
    display: none;
  }
}

.scene {
  perspective: 100rem;
}

.cube {
  --grid-size: 16;
  --cell-size: 1rem;
  --z-offset: min(var(--grid-size) * var(--cell-size), 90vw);
  --z-offset-reversed: calc(-1 * var(--z-offset));

  position: relative;
  transform-style: preserve-3d;
  user-select: none;
}

.game-grid {
  --bg-opacity: 0.5;

  list-style: none;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(var(--grid-size), 1fr);
  border: 1px solid currentColor;
  position: relative;
  background-color: color(from var(--surface) srgb r g b / var(--bg-opacity));
  backdrop-filter: blur(4px);

  &:not(:first-of-type) {
    top: 0;
    left: 0;
    position: absolute;
  }

  &:nth-of-type(1) {
    z-index: 100;
    transform: rotateY(0deg);
  }

  &:nth-of-type(2) {
    transform: rotateY(90deg) translateX(100%);
    transform-origin: center right;
  }

  &:nth-of-type(3) {
    transform: rotateX(90deg) translateY(-100%);
    transform-origin: center top;
  }

  &:nth-of-type(4) {
    transform: rotateY(-90deg) translateX(-100%);
    transform-origin: center left;
  }

  &:nth-of-type(5) {
    transform: rotateX(-90deg) translateY(100%);
    transform-origin: center bottom;
  }

  &:nth-of-type(6) {
    transform: rotateY(180deg) translateZ(var(--z-offset));
    transform-origin: center center;
  }

  > li {
    aspect-ratio: 1;
    width: clamp(4px, (100vw - 2rem) / var(--grid-size), var(--cell-size));
    border: 0.5px solid color(from currentColor srgb r g b / 0.2);
    background-color: transparent;
    transition: background-color ease-out 0.5s;

    &[data-snake="head"] {
      --radius: 25%;

      transition: none;
      border-radius: 0;
      position: relative;

      &::before,
      &::after {
        content: "";
        position: absolute;
        background-color: var(--surface);
        aspect-ratio: 1;
        border-radius: 100vw;
        width: 2px;
        height: 2px;
      }

      &[data-direction="top"] {
        border-top-left-radius: var(--radius);
        border-top-right-radius: var(--radius);

        &::before {
          top: 25%;
          left: 25%;
        }

        &::after {
          top: 25%;
          right: 25%;
        }
      }

      &[data-direction="right"] {
        border-top-right-radius: var(--radius);
        border-bottom-right-radius: var(--radius);

        &::before {
          top: 25%;
          right: 25%;
        }

        &::after {
          bottom: 25%;
          right: 25%;
        }
      }

      &[data-direction="bottom"] {
        border-bottom-left-radius: var(--radius);
        border-bottom-right-radius: var(--radius);

        &::before {
          bottom: 25%;
          right: 25%;
        }

        &::after {
          bottom: 25%;
          left: 25%;
        }
      }

      &[data-direction="left"] {
        border-top-left-radius: var(--radius);
        border-bottom-left-radius: var(--radius);

        &::before {
          bottom: 25%;
          left: 25%;
        }

        &::after {
          top: 25%;
          left: 25%;
        }
      }
    }

    &.snake {
      color: light-dark(teal, darkcyan);
      background-color: currentColor;
    }

    &.point {
      color: light-dark(darksalmon, pink);
      background-color: currentColor;
    }
  }
}

#start-button {
  position: absolute;
  inset: 0;
  margin: auto;
  width: max-content;
  height: max-content;
}

@-moz-document url-prefix() {
  .game-grid {
    --bg-opacity: 0.8;
  }
}
