* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  background: #f5f5f5;
  color: #111;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
}

h1 {
  text-align: center;
  font-size: 1.5rem;
  margin: 8px 0 10px;
}

.controls-card,
.status-card,
.top-how-to-play {
  background: white;
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.row label {
  min-width: 95px;
  font-weight: 700;
}

select,
input {
  flex: 1;
  min-width: 0;
  border: 1px solid #cfcfcf;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
  background: #fff;
}

input[readonly] {
  background: #f7f7f7;
  padding-right: 42px;
}

.name-field-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
}

.name-field-wrap input {
  width: 100%;
}

.name-edit-btn {
  position: absolute;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 999px;
  font-size: 14px;
  line-height: 1;
  background: #f1f1f1;
  color: #111;
  border: 1px solid #d8d8d8;
}

.save-name-btn {
  white-space: nowrap;
}

.button-row,
.status-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.status-actions {
  justify-content: center;
  margin-top: 8px;
}

button {
  border: none;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  background: #111;
  color: white;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.hidden {
  display: none !important;
}

.room-code-wrap {
  margin-top: 12px;
  padding: 10px 12px;
  background: #f8f8f8;
  border: 1px solid #e5e5e5;
  border-radius: 12px;
}

.room-code-label {
  font-size: 0.9rem;
  font-weight: 700;
  margin-bottom: 6px;
}

.room-code-row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.room-code-display {
  flex: 1;
  min-width: 140px;
  padding: 12px 14px;
  background: white;
  border: 1px solid #d5d5d5;
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-align: center;
}

#joinRoomCodeInput {
  text-transform: uppercase;
}

.room-info {
  margin-top: 10px;
  font-size: 0.92rem;
  font-weight: 600;
  color: #333;
  min-height: 20px;
}

.top-how-to-play summary {
  cursor: pointer;
  font-weight: 700;
  list-style: none;
}

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

.top-how-to-play summary::after {
  content: " ▾";
}

.top-how-to-play[open] summary::after {
  content: " ▴";
}

.how-to-play-content {
  margin-top: 10px;
  font-size: 0.95rem;
  line-height: 1.45;
  color: #333;
}

.how-to-play-content p {
  margin: 0 0 10px;
}

.matchup {
  text-align: center;
  font-weight: 800;
  font-size: 1.05rem;
  margin-bottom: 8px;
  min-height: 28px;
}

.turn-headline {
  text-align: center;
  font-size: 1.4rem;
  font-weight: 900;
  letter-spacing: 0.03em;
  margin-bottom: 8px;
  min-height: 52px;
}

.turn-headline.x-turn-color {
  color: #0a58ca;
}

.turn-headline.o-turn-color {
  color: #c92a2a;
}

.turn-headline.game-over {
  color: #111;
}

.helper-line {
  text-align: center;
  font-size: 1rem;
  font-weight: 700;
  color: #333;
  min-height: 24px;
}

.legend-row {
  min-height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 12px;
  padding-top: 8px;
  border-top: 1px solid #ececec;
}

.legend {
  display: flex;
  justify-content: center;
  width: 100%;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.86rem;
  color: #444;
}

.legend-swatch {
  width: 16px;
  height: 16px;
  display: inline-block;
  border-radius: 4px;
}

.legend-last-move {
  background: rgba(240, 173, 0, 0.22);
  border: 2px solid rgba(240, 173, 0, 0.95);
}

.symbol-x {
  color: #0a58ca;
  font-weight: 900;
}

.symbol-o {
  color: #c92a2a;
  font-weight: 900;
}

.board-wrap {
  position: relative;
  width: min(92vw, 620px);
  margin: 0 auto;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: white;
  border: 5px solid #111;
  touch-action: manipulation;
  overflow: hidden;
}

.board::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 6;
  background-image:
    linear-gradient(
      to right,
      transparent 0,
      transparent calc(11.111% - 0.5px),
      #c8c8c8 calc(11.111% - 0.5px),
      #c8c8c8 calc(11.111% + 0.5px),
      transparent calc(11.111% + 0.5px),
      transparent calc(22.222% - 0.5px),
      #c8c8c8 calc(22.222% - 0.5px),
      #c8c8c8 calc(22.222% + 0.5px),
      transparent calc(22.222% + 0.5px),
      transparent calc(44.444% - 0.5px),
      #c8c8c8 calc(44.444% - 0.5px),
      #c8c8c8 calc(44.444% + 0.5px),
      transparent calc(44.444% + 0.5px),
      transparent calc(55.555% - 0.5px),
      #c8c8c8 calc(55.555% - 0.5px),
      #c8c8c8 calc(55.555% + 0.5px),
      transparent calc(55.555% + 0.5px),
      transparent calc(77.777% - 0.5px),
      #c8c8c8 calc(77.777% - 0.5px),
      #c8c8c8 calc(77.777% + 0.5px),
      transparent calc(77.777% + 0.5px),
      transparent calc(88.888% - 0.5px),
      #c8c8c8 calc(88.888% - 0.5px),
      #c8c8c8 calc(88.888% + 0.5px),
      transparent calc(88.888% + 0.5px),
      transparent 100%
    ),
    linear-gradient(
      to bottom,
      transparent 0,
      transparent calc(11.111% - 0.5px),
      #c8c8c8 calc(11.111% - 0.5px),
      #c8c8c8 calc(11.111% + 0.5px),
      transparent calc(11.111% + 0.5px),
      transparent calc(22.222% - 0.5px),
      #c8c8c8 calc(22.222% - 0.5px),
      #c8c8c8 calc(22.222% + 0.5px),
      transparent calc(22.222% + 0.5px),
      transparent calc(44.444% - 0.5px),
      #c8c8c8 calc(44.444% - 0.5px),
      #c8c8c8 calc(44.444% + 0.5px),
      transparent calc(44.444% + 0.5px),
      transparent calc(55.555% - 0.5px),
      #c8c8c8 calc(55.555% - 0.5px),
      #c8c8c8 calc(55.555% + 0.5px),
      transparent calc(55.555% + 0.5px),
      transparent calc(77.777% - 0.5px),
      #c8c8c8 calc(77.777% - 0.5px),
      #c8c8c8 calc(77.777% + 0.5px),
      transparent calc(77.777% + 0.5px),
      transparent calc(88.888% - 0.5px),
      #c8c8c8 calc(88.888% - 0.5px),
      #c8c8c8 calc(88.888% + 0.5px),
      transparent calc(88.888% + 0.5px),
      transparent 100%
    );
}

.board::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 7;
  background-image:
    linear-gradient(
      to right,
      transparent 0,
      transparent calc(33.333% - 3px),
      #111 calc(33.333% - 3px),
      #111 calc(33.333% + 3px),
      transparent calc(33.333% + 3px),
      transparent calc(66.666% - 3px),
      #111 calc(66.666% - 3px),
      #111 calc(66.666% + 3px),
      transparent calc(66.666% + 3px),
      transparent 100%
    ),
    linear-gradient(
      to bottom,
      transparent 0,
      transparent calc(33.333% - 3px),
      #111 calc(33.333% - 3px),
      #111 calc(33.333% + 3px),
      transparent calc(33.333% + 3px),
      transparent calc(66.666% - 3px),
      #111 calc(66.666% - 3px),
      #111 calc(66.666% + 3px),
      transparent calc(66.666% + 3px),
      transparent 100%
    );
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  user-select: none;
  padding: 0;
  margin: 0;
  border: none;
  width: 100%;
  height: 100%;
  aspect-ratio: 1 / 1;
  appearance: none;
  -webkit-appearance: none;
  overflow: hidden;
  z-index: 1;
}

.cell.playable.x-turn {
  background: rgba(100, 170, 255, 0.08);
  box-shadow: inset 0 0 0 2px rgba(100, 170, 255, 0.28);
}

.cell.playable.o-turn {
  background: rgba(255, 120, 120, 0.12);
  box-shadow: inset 0 0 0 2px rgba(255, 120, 120, 0.28);
}

.cell.dimmed {
  opacity: 0.45;
}

.cell.full-board {
  background: rgba(0, 0, 0, 0.03);
}

.cell.banned-opening {
  background: rgba(0, 0, 0, 0.06);
}

.cell.banned-opening::after {
  content: "";
  position: absolute;
  width: 140%;
  height: 2px;
  background: rgba(0, 0, 0, 0.35);
  transform: rotate(45deg);
  pointer-events: none;
}

.cell.opponent-last-move {
  background: rgba(240, 173, 0, 0.18);
  box-shadow: inset 0 0 0 2px rgba(240, 173, 0, 0.85);
}

.cell.required-board {
  box-shadow: inset 0 0 0 3px rgba(255, 200, 0, 0.45);
}

.cell.required-board.pulse {
  animation: boardPulse 1.25s ease-in-out infinite;
}

@keyframes boardPulse {
  0% { box-shadow: inset 0 0 0 0 rgba(255, 200, 0, 0.0); }
  50% { box-shadow: inset 0 0 0 3px rgba(255, 200, 0, 0.7); }
  100% { box-shadow: inset 0 0 0 0 rgba(255, 200, 0, 0.0); }
}

.won-overlay {
  position: absolute;
  width: 33.3333%;
  height: 33.3333%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(120px, 18vw, 220px);
  font-weight: 900;
  opacity: 0.24;
  pointer-events: none;
  z-index: 2;
  line-height: 1;
}

.won-overlay.x { color: #0a58ca; }
.won-overlay.o { color: #c92a2a; }

.win-line {
  position: absolute;
  height: 8px;
  border-radius: 999px;
  transform-origin: 0 50%;
  z-index: 8;
  pointer-events: none;
  opacity: 0.9;
}

.win-line.x { background: #0a58ca; }
.win-line.o { background: #c92a2a; }

.cell-value {
  position: relative;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: clamp(18px, 3.5vw, 30px);
  font-weight: 800;
}

.cell-value.x { color: #0a58ca; }
.cell-value.o { color: #c92a2a; }

.game-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.48);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 16px;
}

.game-overlay-card {
  background: white;
  border-radius: 18px;
  padding: 22px 18px;
  width: min(86%, 320px);
  text-align: center;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.25);
}

.overlay-title {
  font-size: 1.6rem;
  font-weight: 900;
  margin-bottom: 8px;
}

.overlay-subtitle {
  font-size: 1rem;
  color: #444;
  margin-bottom: 16px;
  min-height: 20px;
}

@media (max-width: 480px) {
  .app { padding: 12px; }
  h1 { font-size: 1.25rem; }
  .row {
    flex-direction: column;
    align-items: stretch;
  }
  .row label { min-width: auto; }
  .board-wrap { width: 96vw; }
  .turn-headline { font-size: 1.2rem; }
  .save-name-btn { width: 100%; }
  .won-overlay {
    font-size: clamp(110px, 22vw, 200px);
    opacity: 0.28;
  }
  .win-line { height: 7px; }
}