/* Variables */
:root {
  --bg: #0d0e12;
  --surface: #161922;
  --surface-elevated: #1a1d26;
  --board-bg: #111318;
  --text: #e2e8f0;
  --text-muted: #64748b;
  --border: #252830;
  --border-subtle: #2a2d3a;
  --x-color: #ef4444;
  --o-color: #3b82f6;
  --active-board: rgba(59, 130, 246, 0.15);
  --last-move: rgba(59, 130, 246, 0.2);
  --legal-hover: rgba(255, 255, 255, 0.06);
  --focus-ring: #3b82f6;
  --primary-btn: #3b82f6;
}

* {
  box-sizing: border-box;
}

html, body {
  overflow-x: hidden;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

#app {
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
  padding: 1rem;
  padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0));
  overflow-x: hidden;
}

.screen {
  display: block;
}
.hidden {
  display: none !important;
}
.screen.hidden {
  display: none;
}

.header {
  text-align: center;
  margin-bottom: 1.5rem;
}
#app:has(#game-screen:not(.hidden)) > .header {
  display: none;
}
.header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
}
@media (max-width: 400px) {
  .header {
    margin-bottom: 1rem;
  }
  .header h1 {
    font-size: 1.25rem;
  }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem;
  margin-bottom: 1rem;
}
.card h2 {
  margin: 0 0 1rem;
  font-size: 1.1rem;
}

.form-group { margin-bottom: 0.75rem; }
.form-group label { display: block; margin-bottom: 0.25rem; color: var(--text-muted); font-size: 0.9rem; }
#vs-ai-options label[for="strength"] { display: block; margin-top: 0.75rem; margin-bottom: 0.25rem; color: var(--text-muted); font-size: 0.9rem; }
input[type="text"], input[type="password"], select {
  width: 100%;
  padding: 0.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 1rem;
}

/* Radio groups */
.radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}
.radio-group-vertical {
  flex-direction: column;
  gap: 0.5rem;
}
.radio-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  color: var(--text);
  font-size: 0.95rem;
}
.radio-option input[type="radio"] {
  width: auto;
  margin: 0;
  accent-color: var(--primary-btn);
}
.radio-option label {
  margin: 0;
  cursor: pointer;
  color: inherit;
  font-size: inherit;
}
.radio-group-legend {
  display: block;
  margin-bottom: 0.35rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

button {
  padding: 0.5rem 1rem;
  min-height: 44px;
  background: var(--primary-btn);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 0.5rem;
}
button:hover { opacity: 0.9; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.btn-secondary {
  background: var(--surface-elevated);
  color: var(--text);
  border: 1px solid var(--border);
}
.card .form-group + button,
.card .radio-group-wrap + button {
  margin-top: 1rem;
}
.error { color: #f7768e; font-size: 0.9rem; margin-top: 0.5rem; }

/* Game screen */
.game-screen-wrap {
  max-width: 100%;
  padding: 0 0.25rem;
  overflow-x: hidden;
}

.game-app-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  min-height: 48px;
  padding: 0.5rem 0;
  padding-top: max(0.5rem, env(safe-area-inset-top, 0));
  margin-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}
.game-app-bar-title {
  font-size: 1.1rem;
  font-weight: 600;
  flex-shrink: 0;
}
.game-app-bar-code {
  font-size: 0.9rem;
  color: var(--text-muted);
  flex-shrink: 0;
}
.game-app-bar-code.hidden {
  display: none;
}
.game-app-bar-actions {
  display: flex;
  gap: 0.5rem;
  margin-left: auto;
  flex-shrink: 0;
}
.game-app-bar .btn-bar {
  margin: 0;
  min-height: 36px;
  padding: 0.4rem 0.75rem;
  font-size: 0.9rem;
  background: var(--surface-elevated);
  color: var(--text);
  border: 1px solid var(--border);
}
.game-app-bar .btn-bar:hover {
  opacity: 1;
  background: var(--border-subtle);
}

.turn-status {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.75rem;
  margin-bottom: 0.75rem;
  min-height: 1.5em;
}
.turn-status .turn-indicator {
  font-size: clamp(1rem, 2.5vw + 0.5rem, 1.25rem);
  font-weight: 600;
}
.turn-indicator .x { color: var(--x-color); }
.turn-indicator .o { color: var(--o-color); }
.turn-status .status-message {
  font-size: clamp(0.85rem, 2vw + 0.4rem, 1rem);
  color: var(--text-muted);
}
.turn-status .status-message:empty { display: none; }

.board-wrap {
  position: relative;
  overflow: hidden;
  margin-bottom: 1rem;
  padding: 1rem;
  width: 100%;
  max-width: 100%;
  background: var(--board-bg);
  border-radius: 10px;
}
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  z-index: 1;
}
.loading-overlay.hidden { display: none; }
.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--x-color);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.board {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  width: 100%;
  max-width: min(100%, 420px);
  aspect-ratio: 1;
  margin: 0 auto;
  background: var(--border);
  padding: 6px;
  border-radius: 8px;
  position: relative;
  z-index: 0;
}
.sub-board {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 2px;
  background: var(--surface-elevated);
  padding: 4px;
  border-radius: 4px;
  border: 2px solid transparent;
  transition: border-color 0.15s, background 0.15s;
  position: relative;
  min-width: 0;
}
.sub-board.active-board {
  border-color: var(--o-color);
  background: var(--active-board);
}
.sub-board.playable-region {
  border-color: var(--border-subtle);
  background: rgba(59, 130, 246, 0.06);
}
.region-winner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  font-size: clamp(3rem, 14vw, 6.5rem);
  font-weight: 800;
  border-radius: 4px;
  opacity: 1;
}
.region-won-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  pointer-events: none;
  border-radius: 4px;
}
.region-winner-x { color: var(--x-color); }
.region-winner-o { color: var(--o-color); }
/* Gray out only finalized regions; keep the big X/O overlay in the foreground */
.sub-board.region-draw {
  opacity: 0.5;
  filter: grayscale(0.4);
}
.sub-board:has(.region-winner) .cell,
.sub-board:has(.region-winner) .region-won-backdrop {
  opacity: 0.5;
  filter: grayscale(0.4);
}
.sub-board:has(.region-winner) .region-winner {
  opacity: 1;
  filter: none;
}
.cell {
  aspect-ratio: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(14px, 4vw, 22px);
  font-weight: 700;
  background: var(--bg);
  border: 1px solid var(--border-subtle);
  border-radius: 2px;
  cursor: default;
  transition: background 0.1s;
}
@media (min-width: 400px) {
  .cell {
    min-width: 44px;
    min-height: 44px;
  }
}
.cell.cell-x { color: var(--x-color); }
.cell.cell-o { color: var(--o-color); }
.cell.cell-legal {
  cursor: pointer;
}
.cell.cell-legal:hover {
  background: var(--legal-hover);
}
.cell.last-move {
  background: var(--last-move);
  border-color: var(--o-color);
}
.cell:focus {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}
.cell:focus:not(:focus-visible) { outline: none; }
.cell:disabled {
  opacity: 1;
  cursor: not-allowed;
}
