html {
  box-sizing: border-box;
}

:root {
  --primary-color: #3e3b4e;
  --primary-color-hover: #645f79;
  --navbar-color: #46425b;
  --bg-color: #23212c;
  --bg-color-hover: #282532;
  --text-color: #fff;
  --score-color: #333;
  --x-color: #20b2aa;
  --o-color: #e2c344;
  --draws-color: #f0e4d6;
  --body-font-family: Nunito, sans-serif;
  --game-font-family: Fredoka, Raleway, Montserrat, sans-serif;
}

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

* {
  -webkit-tap-highlight-color: transparent;
}

body {
  max-width: 100%;
  min-height: 100vh;
  background-color: var(--bg-color);
  color: var(--text-color);

  font-family: var(--body-font-family);
  text-transform: uppercase;
  text-align: center;

  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
  position: relative;
}

body:has(dialog[open]) {
  overflow: hidden;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  border: 0;

  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.tooltip {
  position: absolute;
  top: 110%;
  right: 0;

  background-color: rgba(0, 0, 0, 0.8);
  color: var(--text-color);
  outline: 2px solid var(--text-color);
  box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);

  padding: 0.3rem 0.5rem;
  font-family: var(--body-font-family);
  font-size: 0.92rem;
  font-weight: 400;
  text-align: center;
  white-space: nowrap;

  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  transition:
    visibility 0s linear 600ms,
    opacity 0.3s ease;
}

header {
  width: 100%;
  height: 10vh;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

h1#main-title {
  font-family: var(--game-font-family);
  font-weight: 700;
  font-size: 1.4rem;
  letter-spacing: 3px;
  color: var(--x-color);
  text-align: center;
  flex-grow: 1;
  margin-left: 2rem;
}

/* Navbar styles */
.hamburger-menu {
  background-color: transparent;
  border: none;
  margin-right: 1rem;
  padding: 0.2rem;
}

@media (hover: hover) {
  .hamburger-menu:hover {
    cursor: pointer;
  }
}

.bar1,
.bar2,
.bar3 {
  background-color: var(--x-color);
  display: block;
  height: 4px;
  width: 36px;
  margin: 3px;
  border-radius: 0.6rem;
}

.navbar {
  width: 200px;
  height: 90vh;
  background-color: var(--navbar-color);
  padding: 2rem 1rem 0;

  display: flex;
  flex-direction: column;
  align-items: flex-start;

  position: fixed;
  z-index: 1;
  top: 10vh;
  right: -200px;
  transition: transform 0.3s;
}

.navbar.open-navbar {
  transform: translateX(-200px);
}

.navbar .nav-button,
.navbar a {
  background-color: transparent;
  color: var(--text-color);
  border: none;
  text-decoration: none;
  font-family: var(--body-font-family);
  font-size: 1.2rem;
  text-transform: uppercase;
  padding: 1rem;
  cursor: pointer;
}

@media (hover: hover) {
  .open-navbar .nav-button:hover,
  .open-navbar a:hover {
    color: var(--x-color);
  }
}

.hamburger-menu:focus-visible,
.open-navbar .nav-button:focus-visible,
.open-navbar a:focus-visible {
  color: var(--x-color);
  outline: 2px solid var(--o-color);
  outline-offset: 2px;
  border-radius: 0.3rem;
}

.open-navbar .nav-button:active,
.open-navbar a:active {
  color: var(--o-color);
}

dialog {
  max-width: 480px;
  background-color: var(--primary-color);
  color: var(--text-color);
  border: none;
  border-radius: 0.5rem;
  padding: 1.2rem;
  margin: auto 0.5rem;
  scrollbar-width: thin;
  box-shadow: 0 0.5rem 1.8rem rgba(0, 0, 0, 0.4);

  opacity: 0;
  transform: translateY(10px) scale(0.9);
  transition:
    opacity 0.3s ease-out,
    transform 0.3s ease-out,
    display 0.3s allow-discrete,
    overlay 0.3s allow-discrete;

  display: none;
}

dialog:open {
  opacity: 1;
  transform: scale(1);
  display: block;
}

@starting-style {
  dialog[open] {
    opacity: 0;
    transform: scale(0.9);
  }
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(2px);
}

dialog h1,
dialog h2 {
  font-family: var(--game-font-family);
  font-size: 1.8rem;
  letter-spacing: 2px;
  text-align: center;
  color: var(--x-color);
  margin: 0 0 1rem;
}

fieldset {
  border: none;
}

.mode-card {
  display: block;
  background-color: var(--bg-color);
  border: 2px solid var(--primary-color-hover);
  border-radius: 0.6rem;
  padding: 1rem 0.8rem;
  margin: 1rem 0;
  cursor: pointer;
  transition:
    border-color 0.2s ease,
    background 0.2s ease,
    transform 0.05s ease;
}

.mode-card h2 {
  font-size: 1.5rem;
  margin: 0.5 0 0.8rem;
}

.mode-card p {
  font-size: 1rem;
  opacity: 0.85;
}

.mode-card p:first-of-type {
  margin-bottom: 0.8rem;
}

.mode-card input[type='radio'] {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}

.mode-card input:focus-visible {
  outline: none;
}

.mode-card:has(input:checked) {
  border-color: var(--x-color);
  background: color-mix(in srgb, var(--x-color) 7%, var(--bg-color));
}

@media (hover: hover) {
  .mode-card:hover {
    border-color: var(--x-color);
  }
}

.mode-card:focus-within {
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--o-color) 60%, transparent);
}

.mode-card:active {
  transform: scale(0.99);
}

.dialog-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.dialog-actions button {
  font-family: var(--body-font-family);
  font-size: 1.1rem;
  font-weight: 500;
  letter-spacing: 2px;
  text-transform: uppercase;
  background-color: var(--bg-color);
  color: var(--text-color);
  border: none;
  border-radius: 0.3rem;
  padding: 0.8rem;
  cursor: pointer;
  flex-grow: 1;
  transition: background-color 0.2s ease-in-out;
}

@media (hover: hover) {
  .dialog-actions button:hover {
    background-color: var(--bg-color-hover);
    color: var(--x-color);
  }
}

.dialog-actions button:focus-visible {
  color: var(--x-color);
  outline: 2px solid var(--o-color);
  outline-offset: 2px;
}

.dialog-actions button:active {
  background-color: var(--bg-color-hover);
  color: var(--o-color);
}

main {
  width: 100%;
  height: 100%;
  margin: 1rem auto;
  padding: 1rem;

  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

/* Title for desktop screens */
#game-title {
  font-family: var(--game-font-family);
  font-size: 9rem;
  font-weight: 700;
  line-height: 1;
  text-align: left;
  width: 10%;
  height: 100px;
  position: absolute;
  top: 43%;
  left: 20px;

  display: none;
}

#game-title .tic {
  color: var(--x-color);
  position: absolute;
}

#game-title .tac {
  color: var(--o-color);
  position: absolute;
  top: 73%;
}

#game-title .toe {
  color: var(--x-color);
  position: absolute;
  top: 160%;
}

/* Game status and controls */
#game-container {
  width: 200px;
  max-width: 250px;
  border-radius: 0.6rem;
  margin: 0 auto;

  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}

.is-inactive {
  pointer-events: none;
}

#game-status {
  width: 230px;
  max-width: 280px;
  height: 52px;
  font-size: 1.1rem;
  font-weight: 700;
  margin: 0.2rem auto;
  color: var(--text-color);
}

.btn-container,
.mode-badge-container {
  width: 220px;
  max-width: 270px;
  margin-bottom: 1rem;
}

.btn {
  letter-spacing: 2px;
  font-weight: 700;
  text-transform: uppercase;
  background-color: var(--primary-color);
  color: var(--text-color);
  border: none;
  border-radius: 0.3rem;
  cursor: pointer;
  transition: background-color 0.2s ease-in-out;
}

.btn:active {
  background-color: var(--primary-color-hover);
}

.btn:focus-visible {
  color: var(--x-color);
  outline: 2px solid var(--o-color);
  outline-offset: 2px;
}

#restart-btn {
  padding: 0.5rem;
  font-size: 1.4rem;
  float: right;
  position: relative;
}

@media (hover: hover) {
  .btn:hover {
    background-color: var(--primary-color-hover);
  }

  #restart-btn:hover .tooltip {
    visibility: visible;
    opacity: 1;
    transition-delay: 600ms;
  }

  #restart-btn:not(:hover) .tooltip {
    transition-delay: 0s;
  }
}

#next-round-btn {
  width: 100%;
  padding: 1rem;
  margin-top: 2rem;
  font-size: 1.1rem;
}

#next-round-btn:disabled {
  cursor: not-allowed;

  &:hover {
    background-color: var(--primary-color);
  }
}

.mode-badge-container {
  margin-bottom: 0.7rem;
}

.mode-badge {
  font-size: 1rem;
  font-weight: 500;
  height: 2rem;
  padding: 0.4rem 0.5rem;
  border-radius: 0.3rem;
  background: var(--primary-color);
  color: var(--text-color);
}

/* Score display */
#score-board {
  width: 100%;
  display: grid;
  justify-content: center;
  grid-template-columns: repeat(3, calc(100% / 3));
  gap: 10px;
  margin: auto;
}

.game-score {
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.5rem 0;
  background-color: var(--primary-color);
  color: var(--score-color);
  border-radius: 0.3rem;

  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.x {
  background-color: var(--x-color);
  font-weight: 800;
}

.o {
  background-color: var(--o-color);
  font-weight: 800;
}

.draws {
  background-color: var(--draws-color);
  font-weight: 800;
}

/* Main game */
#game-board {
  width: 200px;
  max-width: 250px;
  height: 200px;
  max-height: 250px;
  margin-top: 2rem;

  display: grid;
  justify-content: center;
  align-content: center;
  grid-template-columns: repeat(3, calc(100% / 3));
  grid-template-rows: repeat(3, calc(100% / 3));
  gap: 10px;
}

.cell {
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  position: relative;
  background-color: var(--primary-color);
  border-radius: 0.6rem;
  border: none;
}

.cell:focus-visible {
  outline: 2px solid var(--o-color);
  outline-offset: 2px;
}

.cell:active {
  background-color: var(--primary-color-hover);
}

.cell.x,
.cell.o {
  cursor: not-allowed;
}

@media (hover: hover) {
  .cell:not(.x):hover,
  .cell:not(.o):hover {
    background-color: var(--primary-color-hover);
  }

  .cell.x:hover,
  .cell.o:hover {
    background-color: var(--primary-color);
  }
}

/* X and O marks */
.cell.x::after {
  content: 'X';
  color: var(--x-color);
}

.cell.o::after {
  content: 'O';
  color: var(--o-color);
}

.cell.x::after,
.cell.o::after {
  position: absolute;
  font-family: var(--game-font-family);
  font-size: 3rem;
}

/* Highlights for winning cells */
.cell.won.x {
  background-color: var(--x-color);
}

.cell.won.o {
  background-color: var(--o-color);
}

.cell.won.x::after,
.cell.won.o::after {
  color: var(--primary-color);
}

/* Win/draw styles */
.game-over .cell {
  cursor: default;

  &:not(.won):hover {
    background-color: var(--primary-color) !important;
  }

  &.x.won:hover {
    background-color: var(--x-color) !important;
    color: var(--primary-color);
  }

  &.o.won:hover {
    background-color: var(--o-color) !important;
    color: var(--primary-color);
  }
}

/* Footer styles */
footer {
  width: 100%;
  height: 10vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

footer small {
  font-size: 1.1rem;
  text-transform: none;
  padding: 0.3rem;
  color: var(--text-color);
}

/* Responsive breakpoints */
@media screen and (min-width: 350px) {
  h1#main-title {
    margin-left: 3rem;
    font-size: 1.5rem;
  }

  #game-container {
    width: 210px;
  }

  #game-status {
    width: 240px;
  }

  .btn-container,
  .mode-badge-container {
    width: 230px;
  }

  #game-board {
    width: 210px;
    height: 210px;
  }
}

@media screen and (min-width: 460px) {
  h1#main-title {
    font-size: 1.8rem;
    margin-left: 4rem;
  }

  .dialog-actions {
    flex-direction: row;
  }

  .game-score {
    font-size: 1rem;
  }

  #game-container {
    width: 220px;
  }

  #game-status {
    width: 250px;
  }

  .btn-container,
  .mode-badge-container {
    width: 240px;
  }

  #game-board {
    width: 220px;
    height: 220px;
  }
}

@media screen and (min-width: 490px) {
  dialog {
    margin: auto;
  }
}

@media screen and (min-width: 568px) {
  h1#main-title {
    font-size: 2rem;
  }

  main {
    margin: 0 auto 1rem;
    padding: 0 1rem 1rem;
  }

  #game-container {
    width: 230px;
  }

  #game-status {
    width: 260px;

    display: flex;
    justify-content: center;
    align-items: center;
  }

  .btn-container,
  .mode-badge-container {
    width: 250px;
  }

  #game-board {
    width: 230px;
    height: 230px;
  }
}

@media screen and (min-width: 640px) {
  #game-container {
    width: 240px;
  }

  #game-status {
    width: 270px;
  }

  .btn-container,
  .mode-badge-container {
    width: 260px;
  }

  #game-board {
    width: 240px;
    height: 240px;
  }
}

@media screen and (min-width: 768px) {
  #game-container {
    width: 250px;
  }

  #game-status {
    width: 280px;
  }

  .btn-container,
  .mode-badge-container {
    width: 270px;
  }

  #game-board {
    width: 250px;
    height: 250px;
  }
}

@media screen and (min-width: 1024px) and (orientation: landscape) {
  body {
    justify-content: space-between;
  }

  #game-title {
    display: block;
  }

  h1#main-title {
    display: none;
  }

  .hamburger-menu {
    margin-left: auto;
  }

  #game-status {
    display: block;
  }
}

/* Navbar menu button animations */
.animateBar1 {
  animation: flipBar1 0.5s;
  animation-fill-mode: forwards;
}

.animateBar2 {
  animation: fadeBar2 0.5s;
  animation-fill-mode: forwards;
}

.animateBar3 {
  animation: flipBar3 0.5s;
  animation-fill-mode: forwards;
}

@keyframes flipBar1 {
  from {
    transform: rotate(0);
  }

  to {
    transform: rotate(45deg) translate(4px, 6px);
  }
}

@keyframes fadeBar2 {
  from {
    transform: scaleX(1);
    opacity: 1;
  }

  to {
    transform: scaleX(0);
    opacity: 0;
  }
}

@keyframes flipBar3 {
  from {
    transform: rotate(0);
  }

  to {
    transform: rotate(-45deg) translate(4px, -6px);
  }
}

/* Styles for users who prefer reduced motion */
@media (prefers-reduced-motion: reduce) {
  * {
    transition: none;
    animation: none;
  }
}
