/* Self-hosted variable fonts (SIL Open Font License, see fonts/LICENSES.txt). */
@font-face { font-family: "Fredoka"; src: url("fonts/fredoka.woff2") format("woff2"); font-weight: 300 700; font-display: swap; }
@font-face { font-family: "Nunito"; src: url("fonts/nunito.woff2") format("woff2"); font-weight: 200 1000; font-display: swap; }

:root {
  color-scheme: dark;
  --text: #f4f1ff;
  --dim: #c3c9ec;
  --pink: #ff9fcf;
  --pink-strong: #ff7fbf;
  --cyan: #8ff0ff;
  --glass: rgb(16 22 58 / 0.55);
  --glass-line: rgb(190 200 255 / 0.22);
  --focus: #ffe08a;
  --display: "Fredoka", "Nunito", system-ui, sans-serif;
  --ui: "Nunito", system-ui, sans-serif;
  --title-size: clamp(2rem, 7vw, 3.2rem);
  --status-size: clamp(1rem, 4vw, 1.6rem);
  color: var(--text);
  background: #0b1230;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100svh;
  display: grid;
  place-items: center;
  overflow-x: hidden;
  font-family: var(--ui);
}

/* The illustration stays anchored to the bottom-left, where the stargazer sits. */
body::before {
  --scrim: radial-gradient(ellipse 60% 55% at 50% 45%, rgb(6 9 30 / 0.45), transparent 75%);
  --focus-x: 12%;
  content: "";
  position: fixed;
  inset: 0;
  /* Fallback for browsers without image-set(). The WebP is lossless. */
  background: var(--scrim), url("background.jpg") var(--focus-x) 100% / cover no-repeat, #0b1230;
  background:
    var(--scrim),
    image-set(url("background.webp") type("image/webp"), url("background.jpg") type("image/jpeg")) var(--focus-x) 100% / cover no-repeat,
    #0b1230;
}

/* Narrow screens crop the sides; keep the stargazer in frame. */
@media (max-width: 900px) {
  body::before { --focus-x: 9%; }
}

#bg-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.game {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(10px, 2.2svh, 20px);
  width: min(100%, 640px);
  padding: clamp(14px, 3.5svh, 36px) 16px clamp(18px, 4svh, 40px);
  text-align: center;
}

/* ---- Title ---- */

h1 {
  margin: 0;
  font-family: var(--display);
  font-size: var(--title-size);
  font-weight: 600;
  letter-spacing: 0.01em;
  line-height: 1.02;
}

.tagline {
  margin: 0.3em 0 0;
  font-size: clamp(0.9rem, 2.4vw, 1.02rem);
}

.game-heading {
  padding: 0.5em 1.7em 0.6em;
  border-radius: 28px;
  background: linear-gradient(180deg, rgb(255 190 222 / 0.95), rgb(242 150 200 / 0.95));
  color: #2a1036;
  box-shadow: 0 6px 0 #c8669f, 0 16px 40px rgb(255 120 200 / 0.3), inset 0 1px 0 rgb(255 255 255 / 0.6);
}

.tagline { font-weight: 700; opacity: 0.75; }

/* ---- Score ---- */

.score {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 0;
  padding: 8px 20px;
  border: 1px solid var(--glass-line);
  border-radius: 999px;
  background: var(--glass);
  backdrop-filter: blur(8px);
  color: var(--dim);
  font-size: 0.95rem;
  font-weight: 500;
}

.score-side { display: inline-flex; align-items: center; gap: 8px; }
.score-value { color: var(--text); font-weight: 800; font-variant-numeric: tabular-nums; min-width: 1ch; }
.score-divider { color: var(--glass-line); font-weight: 700; }
.mark-x { color: var(--pink); text-shadow: 0 0 10px var(--pink); }
.mark-o { color: var(--cyan); text-shadow: 0 0 10px var(--cyan); }

/* ---- Board ---- */

.board-stage {
  position: relative;
  width: min(100%, 460px, 50svh);
  aspect-ratio: 420 / 380;
}

.board-stage::before {
  content: "";
  position: absolute;
  inset: 12% 10%;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgb(140 170 255 / 0.22), transparent);
  filter: blur(18px);
  pointer-events: none;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 22px 26px rgb(3 6 25 / 0.65));
}

.board-grid {
  position: absolute;
  inset: 0 auto auto 0;
  width: var(--board-size);
  height: var(--board-size);
  transform-origin: 0 0;
}

.board-grid.rotating { pointer-events: none; }

.board-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: calc(100% / 3);
}

.square {
  display: block;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.square[aria-disabled="false"] { cursor: pointer; }

@media (hover: hover) {
  .square[aria-disabled="false"]:hover { background: rgb(190 170 255 / 0.16); }
}

.square:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: -6px;
}

/* ---- Controls: status and the main button share one centred column ---- */

.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#status-text {
  margin: 0;
  min-height: 1.3em;
  font-family: var(--display);
  font-size: var(--status-size);
  font-weight: 600;
  line-height: 1.3;
  text-shadow: 0 2px 12px rgb(0 0 0 / 0.6);
}


#status-text[data-result="X"] { color: #b9ffd8; }
#status-text[data-result="O"] { color: var(--pink); }
#status-text[data-result="draw"] { color: #ffe0a6; }

#board-help {
  margin: 0;
  color: var(--dim);
  font-size: 0.8rem;
  text-shadow: 0 1px 8px rgb(0 0 0 / 0.7);
}

button { font: inherit; }

.primary-button {
  min-height: 48px;
  min-width: 176px;
  padding: 0.6rem 1.8rem;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(180deg, var(--pink), var(--pink-strong));
  color: #2a1036;
  font-family: var(--display);
  font-weight: 600;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
  cursor: pointer;
  box-shadow: 0 5px 0 #c8669f, 0 12px 28px rgb(255 120 200 / 0.35);
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.primary-button:hover { box-shadow: 0 5px 0 #c8669f, 0 14px 34px rgb(255 120 200 / 0.5); }

.primary-button:active { transform: translateY(3px); box-shadow: 0 2px 0 #c8669f; }

.primary-button:focus-visible,
.icon-button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 4px;
}

/* ---- Sound toggle, top-right corner ---- */

.icon-button {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  z-index: 1;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--glass-line);
  border-radius: 50%;
  background: var(--glass);
  backdrop-filter: blur(8px);
  color: var(--dim);
  cursor: pointer;
}

.icon-button:hover { color: var(--text); }
.icon-button svg { width: 22px; height: 22px; }
.icon-button .wave { display: none; }
.icon-button[aria-pressed="true"] { color: var(--cyan); border-color: rgb(143 240 255 / 0.5); }
.icon-button[aria-pressed="true"] .wave { display: inline; }
.icon-button[aria-pressed="true"] .mute { display: none; }

/* ---- Small screens ---- */

@media (max-height: 700px) {
  .game { gap: 8px; padding-block: 10px; }
  .tagline { display: none; }
  .game-heading { padding: 0.35em 1.4em 0.42em !important; }
  h1 { font-size: calc(var(--title-size) * 0.7); }
  .score { padding: 4px 14px; font-size: 0.85rem; }
  .board-stage { width: min(100%, 42svh); }
  .controls { gap: 8px; }
  .primary-button { min-height: 42px; padding-block: 0.4rem; }
  #status-text { font-size: calc(var(--status-size) * 0.8); }
  #board-help { font-size: 0.72rem; }
}

@media (max-height: 480px) {
  .board-stage { width: min(100%, 34svh); }
  .controls { gap: 6px; }
}

/* Landscape phones: board on the right, everything else on the left. */
@media (max-height: 520px) and (min-width: 600px) {
  .game {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas: "heading board" "score board" "controls board";
    width: min(100%, 900px);
    column-gap: 28px;
    align-items: center;
  }
  .game-heading { grid-area: heading; justify-self: center; }
  .score { grid-area: score; justify-self: center; }
  .controls { grid-area: controls; }
  .board-stage { grid-area: board; width: min(46vw, 82svh); }
}

/* Keyboard hints are noise on touch-only devices. */
@media (hover: none) and (pointer: coarse) {
  #board-help { display: none; }
}
