/* ===== Reset & Base ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background: #1a1a2e;
  color: #e0e0e0;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* ===== Back Link ===== */
.back-link {
  position: absolute;
  top: 1.2rem;
  left: 1.5rem;
  color: #8888aa;
  text-decoration: none;
  font-size: 1rem;
  transition: color 0.2s;
  z-index: 10;
}

.back-link:hover {
  color: #4fc3f7;
}

/* ===== Container ===== */
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 2rem 1rem;
  width: 100%;
  max-width: 700px;
}

/* ===== Title ===== */
.title {
  font-size: 2.5rem;
  font-weight: 700;
  color: #ffffff;
  margin-bottom: 0.5rem;
  letter-spacing: 0.04em;
  text-align: center;
}

/* ===== Mode Selector ===== */
.mode-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-bottom: 1rem;
}

.mode-btn {
  padding: 0.5rem 1.1rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: #b0b0c8;
  background: #16213e;
  border: 2px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, color 0.2s, box-shadow 0.2s;
  letter-spacing: 0.02em;
  touch-action: manipulation;
  min-height: 44px;
}

.mode-btn:hover {
  background: #1b2a50;
  border-color: rgba(79, 195, 247, 0.3);
  color: #e0e0e0;
}

.mode-btn.active {
  background: linear-gradient(135deg, #0f3460, #16213e);
  border-color: #4fc3f7;
  color: #ffffff;
  box-shadow: 0 0 10px rgba(79, 195, 247, 0.25);
}

/* ===== Status ===== */
.status {
  font-size: 1.3rem;
  margin-bottom: 1.5rem;
  min-height: 2rem;
  transition: color 0.3s;
  text-align: center;
}

.status.turn-x {
  color: #4fc3f7;
}

.status.turn-o {
  color: #ef5350;
}

.status.winner-x {
  color: #4fc3f7;
  font-weight: 700;
  animation: statusPop 0.4s ease;
}

.status.winner-o {
  color: #ef5350;
  font-weight: 700;
  animation: statusPop 0.4s ease;
}

.status.draw {
  color: #ffd54f;
  font-weight: 700;
  animation: statusPop 0.4s ease;
}

@keyframes statusPop {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}

/* ===== Grid Size Selector ===== */
.grid-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-bottom: 0.7rem;
}

.grid-btn {
  padding: 0.45rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: #b0b0c8;
  background: #16213e;
  border: 2px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, color 0.2s, box-shadow 0.2s;
  touch-action: manipulation;
  min-height: 44px;
}

.grid-btn:hover {
  background: #1b2a50;
  border-color: rgba(255, 215, 0, 0.3);
  color: #e0e0e0;
}

.grid-btn.active {
  background: linear-gradient(135deg, #0f3460, #16213e);
  border-color: #ffd740;
  color: #ffd740;
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.25);
}

/* ===== Board ===== */
.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
  width: min(85vmin, 500px);
  height: min(85vmin, 500px);
  background: rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  padding: 6px;
  margin-bottom: 1.5rem;
  touch-action: manipulation;
}

/* ===== Cells ===== */
.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #16213e;
  border-radius: 8px;
  font-size: min(14vmin, 80px);
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s, box-shadow 0.2s;
  min-width: 0;
  min-height: 44px;
  aspect-ratio: 1;
  position: relative;
  touch-action: manipulation;
}

.cell:hover:not(.taken):not(.game-over) {
  background: #1b2a50;
  box-shadow: inset 0 0 20px rgba(255, 255, 255, 0.05);
}

.cell.taken {
  cursor: default;
}

.cell.game-over {
  cursor: default;
}

/* Mark colours */
.cell.x {
  color: #4fc3f7;
}

.cell.o {
  color: #ef5350;
}

/* Scale-in animation for placed marks */
.cell.pop .mark {
  animation: markPop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.mark {
  display: inline-block;
  transform: scale(0);
  line-height: 1;
}

@keyframes markPop {
  0%   { transform: scale(0); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

/* Winning cell glow / pulse */
.cell.win-cell {
  animation: winPulse 1s ease-in-out infinite alternate;
}

.cell.win-cell.x {
  background: rgba(79, 195, 247, 0.15);
  box-shadow: 0 0 18px rgba(79, 195, 247, 0.4);
}

.cell.win-cell.o {
  background: rgba(239, 83, 80, 0.15);
  box-shadow: 0 0 18px rgba(239, 83, 80, 0.4);
}

@keyframes winPulse {
  0%   { filter: brightness(1); }
  100% { filter: brightness(1.3); }
}

/* ===== Scoreboard ===== */
.scoreboard {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  font-size: 1.1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

.score-item {
  white-space: nowrap;
}

.score-x strong {
  color: #4fc3f7;
}

.score-o strong {
  color: #ef5350;
}

.score-d strong {
  color: #ffd54f;
}

.score-divider {
  color: #555;
}

/* ===== New Game Button ===== */
.new-game-btn {
  padding: 0.75rem 2.5rem;
  font-size: 1.1rem;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(135deg, #0f3460, #16213e);
  border: 2px solid rgba(79, 195, 247, 0.3);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.25s, border-color 0.25s, transform 0.15s, box-shadow 0.25s;
  letter-spacing: 0.03em;
  touch-action: manipulation;
  min-height: 44px;
}

.new-game-btn:hover {
  background: linear-gradient(135deg, #153e75, #1b2a50);
  border-color: #4fc3f7;
  box-shadow: 0 0 14px rgba(79, 195, 247, 0.25);
}

.new-game-btn:active {
  transform: scale(0.96);
}

/* ===== How to Play ===== */
.how-to-play {
  margin-top: 2rem;
  width: 100%;
  max-width: 500px;
  background: #16213e;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  overflow: hidden;
}

.how-to-play summary {
  padding: 0.9rem 1.2rem;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  color: #c0c0d8;
  transition: color 0.2s, background 0.2s;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  touch-action: manipulation;
  min-height: 44px;
}

.how-to-play summary::-webkit-details-marker {
  display: none;
}

.how-to-play summary::before {
  content: '\25B6';
  font-size: 0.7rem;
  transition: transform 0.25s;
  display: inline-block;
}

.how-to-play[open] summary::before {
  transform: rotate(90deg);
}

.how-to-play summary:hover {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.03);
}

.how-to-play-content {
  padding: 0.5rem 1.4rem 1.2rem;
  border-top: 1px solid rgba(255, 255, 255, 0.07);
}

.how-to-play-content ul {
  list-style: disc;
  padding-left: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  color: #b0b0c0;
  font-size: 0.95rem;
  line-height: 1.5;
}

.how-to-play-content li strong {
  color: #e0e0e0;
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .title {
    font-size: 1.8rem;
  }

  .status {
    font-size: 1.1rem;
  }

  .board {
    width: min(90vw, 90vmin);
    height: min(90vw, 90vmin);
    gap: 4px;
    padding: 4px;
  }

  .cell {
    min-width: 0;
    min-height: 36px;
    font-size: min(16vmin, 56px);
    border-radius: 6px;
  }

  .mode-selector {
    gap: 0.35rem;
  }

  .mode-btn {
    padding: 0.45rem 0.75rem;
    font-size: 0.82rem;
    min-height: 44px;
  }

  .grid-selector {
    gap: 0.35rem;
  }

  .grid-btn {
    padding: 0.4rem 0.7rem;
    font-size: 0.82rem;
    min-height: 44px;
  }

  .scoreboard {
    font-size: 0.95rem;
    gap: 0.5rem;
  }

  .new-game-btn {
    padding: 0.65rem 2rem;
    font-size: 1rem;
  }

  .back-link {
    top: 0.8rem;
    left: 1rem;
    font-size: 0.9rem;
  }

  .how-to-play {
    max-width: 100%;
  }
}

/* Landscape phones */
@media (max-height: 500px) and (orientation: landscape) {
  .container {
    padding: 0.5rem 1rem;
  }

  .title {
    font-size: 1.4rem;
    margin-bottom: 0.3rem;
  }

  .status {
    font-size: 1rem;
    margin-bottom: 0.5rem;
  }

  .board {
    width: min(60vmin, 320px);
    height: min(60vmin, 320px);
  }

  .cell {
    min-width: 0;
    min-height: 36px;
  }

  .scoreboard {
    margin-bottom: 0.5rem;
  }

  .mode-selector {
    margin-bottom: 0.4rem;
  }

  .mode-btn {
    padding: 0.3rem 0.7rem;
    font-size: 0.82rem;
  }

  .grid-btn {
    padding: 0.3rem 0.6rem;
    font-size: 0.82rem;
  }
}
