:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #334155;
  --fg: #e2e8f0;
  --muted: #94a3b8;
  --x: #2563eb;
  --x-soft: rgba(37, 99, 235, 0.18);
  --x-strong: rgba(37, 99, 235, 0.55);
  --o: #dc2626;
  --o-soft: rgba(220, 38, 38, 0.18);
  --o-strong: rgba(220, 38, 38, 0.55);
  --highlight: #fbbf24;
  --highlight-soft: rgba(251, 191, 36, 0.25);
  --cell: #f8fafc;
  --cell-disabled: #e2e8f0;
  --draw: #64748b;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--fg);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

#app {
  width: 100%;
  max-width: 560px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  flex: 1;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input {
  font-family: inherit;
  border: 1px solid var(--panel-2);
  background: var(--panel);
  color: var(--fg);
  border-radius: 10px;
  padding: 14px;
  font-size: 1.05rem;
}

/* ===== Language toggle ===== */
.lang-toggle {
  display: inline-flex;
  gap: 2px;
  background: var(--panel);
  padding: 3px;
  border-radius: 8px;
  font-size: 0.78rem;
}
.lang-toggle .lang {
  padding: 5px 10px;
  border-radius: 6px;
  color: var(--muted);
  font-weight: 700;
  letter-spacing: 0.05em;
}
.lang-toggle .lang.active {
  background: var(--panel-2);
  color: var(--fg);
}

.top-bar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 4px;
}

/* ===== Lobby ===== */
.lobby {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 8px 8px 24px;
}
.lobby h1 {
  text-align: center;
  margin: 16px 0 24px;
  font-size: 1.6rem;
  letter-spacing: 0.02em;
}
.lobby h1 .x { color: var(--x); }
.lobby h1 .o { color: var(--o); }
.lobby .btn {
  font-size: 1.05rem;
  padding: 16px;
  border-radius: 12px;
  font-weight: 600;
  color: white;
  transition: transform 0.05s ease, filter 0.15s ease;
}
.lobby .btn:active { transform: scale(0.98); }
.lobby .btn.primary { background: var(--x); }
.lobby .btn.danger { background: var(--o); }
.lobby .btn.neutral { background: var(--panel-2); }
.lobby .join-row {
  display: flex;
  gap: 8px;
}
.lobby .join-row input {
  flex: 1;
  text-transform: uppercase;
  letter-spacing: 0.25em;
  text-align: center;
  font-weight: 600;
  font-size: 1.2rem;
}
.lobby .join-row .btn { padding: 14px 18px; }
.lobby .divider {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin: 4px 0;
  position: relative;
}
.lobby .divider::before, .lobby .divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 35%;
  height: 1px;
  background: var(--panel-2);
}
.lobby .divider::before { left: 0; }
.lobby .divider::after { right: 0; }
.lobby .hint {
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  margin-top: 8px;
  line-height: 1.5;
}
.lobby .error {
  background: var(--o-soft);
  color: var(--o);
  padding: 10px;
  border-radius: 8px;
  text-align: center;
  font-size: 0.95rem;
}

/* ===== Game ===== */
.game {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--muted);
}
.game-header .role {
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 6px;
  background: var(--panel);
}
.game-header .role.X { color: var(--x); }
.game-header .role.O { color: var(--o); }
.game-header .code {
  font-family: ui-monospace, "SF Mono", monospace;
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: 0.2em;
  color: var(--fg);
  background: var(--panel);
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
}
.game-header .leave {
  background: var(--panel);
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 0.85rem;
}

.sub-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}
.score {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
  background: var(--panel);
  padding: 6px 12px;
  border-radius: 8px;
  color: var(--muted);
}
.score b {
  color: var(--fg);
  font-weight: 700;
  margin-left: 2px;
}
.score .sx { color: var(--x); }
.score .sx b { color: var(--x); }
.score .so { color: var(--o); }
.score .so b { color: var(--o); }
.score .dot { color: var(--panel-2); }
.score .reset-score {
  margin-left: 4px;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 1rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.score .reset-score:active { transform: scale(0.95); }

.status {
  text-align: center;
  font-weight: 600;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--panel);
  border: 2px solid var(--panel-2);
  font-size: 1rem;
}
.status.turn-X { background: var(--x-soft); border-color: var(--x); color: var(--x); }
.status.turn-O { background: var(--o-soft); border-color: var(--o); color: var(--o); }
.status.win-X { background: var(--x); color: white; border-color: var(--x); }
.status.win-O { background: var(--o); color: white; border-color: var(--o); }
.status.draw { background: var(--draw); color: white; border-color: var(--draw); }

.error-toast {
  background: var(--o-soft);
  color: var(--o);
  padding: 8px;
  border-radius: 8px;
  text-align: center;
  font-size: 0.9rem;
}

.big-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
  padding: 6px;
  background: var(--panel-2);
  border-radius: 14px;
  aspect-ratio: 1 / 1;
  width: 100%;
  max-width: min(95vw, 540px);
  margin: 0 auto;
}

.small-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
  padding: 4px;
  background: var(--panel);
  border-radius: 8px;
  position: relative;
  transition: box-shadow 0.15s ease, background 0.15s ease;
}
.small-board.active-X {
  box-shadow: 0 0 0 3px var(--x), 0 0 16px var(--x-soft);
  background: var(--x-soft);
}
.small-board.active-O {
  box-shadow: 0 0 0 3px var(--o), 0 0 16px var(--o-soft);
  background: var(--o-soft);
}
.small-board.won-X { background: var(--x); }
.small-board.won-O { background: var(--o); }
.small-board.won-draw { background: var(--draw); }

.cell {
  background: var(--cell);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(0.9rem, 4vw, 1.5rem);
  padding: 0;
  color: var(--bg);
  transition: background 0.1s ease;
  min-width: 0;
  min-height: 0;
}
.cell.X { color: var(--x); }
.cell.O { color: var(--o); }
.cell.last {
  background: var(--highlight);
  box-shadow: inset 0 0 0 2px #b45309;
}
.cell:disabled { cursor: default; }
.cell.empty:not(:disabled):active { background: var(--cell-disabled); }

.small-board.won-X .cell, .small-board.won-O .cell, .small-board.won-draw .cell {
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.65);
  pointer-events: none;
}
.small-board.won-X .cell.last, .small-board.won-O .cell.last {
  background: rgba(251, 191, 36, 0.7);
  box-shadow: none;
  color: rgba(0, 0, 0, 0.7);
}

.winner-mark {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(2.2rem, 14vw, 5.5rem);
  font-weight: 900;
  color: white;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  letter-spacing: -0.05em;
}

.actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 6px;
}
.actions button {
  padding: 10px 18px;
  border-radius: 8px;
  background: var(--panel);
  color: var(--fg);
  font-weight: 600;
  font-size: 0.95rem;
  border: 1px solid var(--panel-2);
}
.actions button:active { transform: scale(0.98); }
.actions .primary {
  background: var(--x);
  border-color: var(--x);
  color: white;
}

.copy-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--panel-2);
  color: var(--fg);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 100;
}
.copy-toast.visible { opacity: 1; }

@media (min-width: 480px) {
  .cell { font-size: 1.5rem; }
  .winner-mark { font-size: 4.5rem; }
}
