:root {
  --bg: #0f1222;
  --bg-2: #171b33;
  --card: #1c2140;
  --line: #2c3361;
  --text: #e9ecff;
  --muted: #9aa3cf;
  --accent: #6c7cff;
  --accent-2: #8b98ff;
  --x: #ff6b8b;
  --o: #4dd8c0;
  --good: #5fdc8a;
  --bad: #ff7a7a;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(1200px 600px at 50% -200px, #252c5c 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; justify-content: center; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 820px;
  margin: 0 auto;
  padding: 18px 16px;
}
.logo { margin: 0; font-size: 1.5rem; letter-spacing: 0.5px; margin-right: auto; }
.logo span { color: var(--accent-2); font-weight: 400; }
.who {
  background: var(--bg-2);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 6px 12px;
  font-size: 0.9rem;
}
.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--bad); flex: none; }
.conn.ok { background: var(--good); }

main { max-width: 820px; margin: 0 auto; padding: 0 16px 48px; }

/* Cards & layout */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 6px; font-size: 1.15rem; }
.card p { margin: 0 0 14px; }
.narrow { max-width: 440px; margin-left: auto; margin-right: auto; margin-top: 6vh; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.grid .card { margin-bottom: 0; }
.grid + .card { margin-top: 16px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row input { flex: 1; min-width: 0; }
.stack { display: grid; gap: 10px; }

/* Controls */
button {
  font: inherit;
  font-weight: 600;
  border: 0;
  border-radius: 10px;
  padding: 11px 18px;
  cursor: pointer;
  background: var(--accent);
  color: white;
  transition: background 0.15s, transform 0.05s, opacity 0.15s;
}
button:hover:not(:disabled) { background: var(--accent-2); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.55; cursor: default; }
button.secondary { background: var(--bg-2); border: 1px solid var(--line); color: var(--text); }
button.secondary:hover:not(:disabled) { background: #232a52; }
button.ghost { background: transparent; color: var(--muted); }
button.ghost:hover:not(:disabled) { background: var(--bg-2); color: var(--text); }

input {
  font: inherit;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  outline: none;
}
input:focus { border-color: var(--accent); }
.code-input { text-transform: uppercase; letter-spacing: 4px; font-family: ui-monospace, Consolas, monospace; }

/* Lobby list */
.list { list-style: none; margin: 0 0 4px; padding: 0; }
.list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.list li:last-child { border-bottom: 0; }
.list .host { font-weight: 600; margin-right: 10px; }
.list button { padding: 8px 16px; }

.rules summary { cursor: pointer; font-weight: 600; }
.rules ol { margin: 12px 0 0; padding-left: 20px; line-height: 1.7; color: var(--muted); }
.rules b { color: var(--text); }

/* Waiting */
.big-code {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 2.6rem;
  letter-spacing: 10px;
  font-weight: 700;
  margin: 8px 0 18px;
  padding: 14px;
  background: var(--bg);
  border: 1px dashed var(--line);
  border-radius: 12px;
  user-select: all;
}
#screen-wait .row { margin-bottom: 14px; }
.spinner {
  width: 34px;
  height: 34px;
  margin: 4px auto 14px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Game */
.players {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin: 6px 0 14px;
}
.player {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  max-width: 260px;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--card);
  border: 2px solid transparent;
  transition: border-color 0.2s;
  min-width: 0;
}
.player .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player .sub { font-size: 0.8rem; color: var(--muted); }
.player > div { min-width: 0; }
.player.active { border-color: var(--accent); }
.player.winner { border-color: var(--good); }
.vs { color: var(--muted); font-size: 0.9rem; }
.mark { font-weight: 800; font-size: 1.4rem; width: 28px; text-align: center; flex: none; }
.mark.x, .cell.x { color: var(--x); }
.mark.o, .cell.o { color: var(--o); }

.status { text-align: center; font-size: 1.3rem; font-weight: 700; margin: 4px 0 14px; min-height: 1.6em; }
.status.mine { color: var(--accent-2); }
.status.win { color: var(--good); }
.status.lose { color: var(--bad); }

.timer {
  width: min(100%, 380px);
  margin: -4px auto 16px;
  text-align: center;
}
.timer-track {
  height: 8px;
  border-radius: 99px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  overflow: hidden;
}
.timer-fill {
  height: 100%;
  width: 100%;
  background: var(--muted);
  border-radius: 99px;
  transition: width 0.1s linear;
}
.timer.mine .timer-fill { background: var(--accent); }
.timer.low .timer-fill { background: var(--bad); }
.timer-text { display: block; margin-top: 6px; font-size: 0.85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.timer.mine.low .timer-text { color: var(--bad); font-weight: 600; }

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: min(100%, 380px);
  aspect-ratio: 1;
  margin: 0 auto;
}
.cell {
  position: relative;
  padding: 0;
  border-radius: 16px;
  background: var(--card);
  border: 2px solid var(--line);
  font-size: clamp(2.4rem, 13vw, 4rem);
  font-weight: 800;
  line-height: 1;
  color: var(--text);
}
.cell:disabled { opacity: 1; cursor: default; }
.cell:hover:not(:disabled) { background: #252b52; }
.cell.target { border-style: dashed; border-color: var(--accent); }
.cell.target:empty:hover::after { content: ""; position: absolute; inset: 35%; border-radius: 50%; background: var(--accent); opacity: 0.35; }
.cell.pickable { border-color: var(--accent-2); }
.cell.selected { background: #2e3670; border-color: white; box-shadow: 0 0 0 3px var(--accent); }
.cell.locked::after {
  content: "🔒";
  position: absolute;
  top: 6px;
  right: 8px;
  font-size: 1rem;
  opacity: 0.85;
}
.cell.locked { opacity: 0.8; }
.cell.win { background: #1f3b33; border-color: var(--good); animation: pop 0.4s ease-out; }
@keyframes pop { 50% { transform: scale(1.06); } }

.hint { text-align: center; color: var(--muted); min-height: 1.5em; margin: 16px 0; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: #2b3160;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 480px) {
  .players { gap: 8px; }
  .player { padding: 8px 10px; }
  .vs { display: none; }
  .big-code { font-size: 2rem; letter-spacing: 6px; }
}
