*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --bg: #f5f0e8;
  --bg-warm: #ede7db;
  --surface: #fffdf8;
  --ink: #2c2826;
  --ink-light: #8a8279;
  --ink-muted: #b8b0a4;
  --line: #ddd5c8;
  --accent: #2f5ec4;
  --accent-hover: #264fa8;
  --accent-ink: #fff;
  --grid: #ddd5c8;
  --hover: #f3eee4;
  --x: #d8443a;
  --o: #2f5ec4;
  --ok: #3f9a6b;
  --bad: #c45d3e;
  --radius: 6px;
  --bw: max(250px, min(calc(100vw - 2rem), 420px, calc(100dvh - 15rem)));
  color-scheme: light;
}

html.dark {
  --bg: #1a1714;
  --bg-warm: #222019;
  --surface: #23201c;
  --ink: #e8e2d8;
  --ink-light: #8a8279;
  --ink-muted: #5a544e;
  --line: #38332d;
  --accent: #5a85e0;
  --accent-hover: #6f96ea;
  --accent-ink: #0c1424;
  --grid: #38332d;
  --hover: #2a2621;
  --x: #e0554a;
  --o: #5a85e0;
  --ok: #4fb683;
  --bad: #e0704f;
  color-scheme: dark;
}

html { background: var(--bg); }

body {
  font-family: 'DM Mono', ui-monospace, Menlo, Consolas, monospace;
  background: var(--bg);
  color: var(--ink);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top, 0) max(1rem, env(safe-area-inset-right, 0)) env(safe-area-inset-bottom, 0) max(1rem, env(safe-area-inset-left, 0));
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

button, input { font: inherit; color: inherit; }

button {
  padding: 0.5rem 0.95rem;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  font-size: 0.85rem;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.6; cursor: default; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 500; }
.btn-accent:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.wide { width: 100%; }
button.small { padding: 0.25rem 0.6rem; font-size: 0.75rem; }

input {
  padding: 0.5rem 0.7rem;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 0.9rem;
  min-width: 0;
}

/* ── Top bar ── */
.top-bar {
  width: 100%;
  max-width: 960px;
  padding: 0.85rem 0 0.4rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}
.title { border: 0; background: none; padding: 0; }
.title:hover:not(:disabled) { border: 0; }
h1 {
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 700;
  font-size: 1.45rem;
  letter-spacing: -0.02em;
}
h1 span { color: var(--accent); }
.top-actions { display: flex; align-items: center; gap: 0.75rem; }
.badge { font-size: 0.72rem; color: var(--ink-light); }
.icon-btn { border: 0; background: none; padding: 0.2rem; font-size: 1.05rem; line-height: 1; color: var(--ink-light); }

main { width: 100%; max-width: 960px; flex: 1; }

/* ── Menu ── */
.menu { max-width: 440px; margin: 0 auto; padding-block: 1rem 2rem; display: flex; flex-direction: column; gap: 1rem; }
.tagline { font-family: 'Fraunces', Georgia, serif; font-size: 1.25rem; line-height: 1.3; color: var(--ink); }
.resume { display: flex; flex-direction: column; gap: 0.5rem; }
.resume:empty { display: none; }
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.panel h2 { font-family: 'Fraunces', Georgia, serif; font-size: 1.15rem; display: flex; align-items: center; gap: 0.6rem; }
.live { display: inline-flex; align-items: center; gap: 0.35rem; font-family: 'DM Mono', monospace; font-size: 0.7rem; font-weight: 400; color: var(--ink-light); }
.live i { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }
.row { display: flex; gap: 0.5rem; }
.row > * { flex: 1; }
.seg { display: flex; border: 1.5px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg button { flex: 1; border: 0; border-radius: 0; background: transparent; }
.seg button + button { border-left: 1.5px solid var(--line); }
.seg button.active { background: var(--accent); color: var(--accent-ink); }
.record { font-size: 0.75rem; color: var(--ink-light); }
.err { font-size: 0.8rem; color: var(--bad); }
.rules { font-size: 0.82rem; color: var(--ink-light); line-height: 1.55; }
.rules summary { cursor: pointer; color: var(--ink); }
.rules p { margin-top: 0.6rem; }
kbd { font-family: inherit; font-size: 0.75rem; padding: 0 0.3rem; border: 1px solid var(--line); border-radius: 4px; }

/* ── Game ── */
.game { padding-block: 0.5rem 2rem; display: flex; flex-direction: column; align-items: center; }

.room-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem 0.9rem;
  font-size: 0.8rem;
  color: var(--ink-light);
  margin-bottom: 0.6rem;
}
.room-bar b { color: var(--ink); font-weight: 500; letter-spacing: 0.12em; font-size: 0.95rem; }
.opp-status::before { content: ''; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--ink-muted); margin-right: 0.35rem; }
.opp-status.on::before { background: var(--ok); }

.players {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  width: var(--bw);
}
.player {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.7rem;
  border: 1.5px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.82rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.player span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player#p2 { flex-direction: row-reverse; text-align: right; }
.player.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.player.active span { font-weight: 500; }
.vs { font-size: 0.72rem; color: var(--ink-light); }
.chip { flex: none; font-style: normal; font-weight: 500; font-size: 1rem; line-height: 1; }
.chip.x { color: var(--x); }
.chip.o { color: var(--o); }
.score { flex: none; margin-left: auto; font-weight: 500; font-size: 0.9rem; }
.player#p2 .score { margin: 0 auto 0 0; }
.score:empty { display: none; }

.status {
  min-height: 2.6rem;
  display: grid;
  place-items: center;
  text-align: center;
  font-family: 'Fraunces', Georgia, serif;
  font-size: 1.05rem;
  padding-block: 0.4rem;
}
.status.alert { color: var(--accent); }

/* Board: a 3×3 grid of buttons; the gaps between them are the lines. */
.board-wrap { width: var(--bw); }
.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: calc(var(--bw) * 0.022);
  aspect-ratio: 1;
  background: var(--grid);
  border-radius: 16px;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.cell {
  position: relative;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--surface);
  cursor: default;
  transition: background 0.15s;
}
.cell:hover:not(:disabled) { border: 0; }
.cell:focus-visible { outline: 3px solid var(--accent); outline-offset: -6px; }
.board.can-place .cell:not(.filled) { cursor: pointer; }
.board.can-place .cell:not(.filled):hover { background: var(--hover); }

.cell svg { position: absolute; inset: 0; width: 100%; height: 100%; fill: none; stroke-linecap: round; stroke-width: 10; }
.cell.x svg { stroke: var(--x); }
.cell.o svg { stroke: var(--o); }
.cell.draw svg path, .cell.draw svg circle { stroke-dasharray: 1; animation: draw 0.28s ease-out both; }
.cell.draw svg path + path { animation-delay: 0.14s; }
@keyframes draw { from { stroke-dashoffset: 1; } to { stroke-dashoffset: 0; } }

/* Ghost of the mark you're about to place. */
.board.can-place .cell:not(.filled):hover::before,
.board.can-place .cell:not(.filled):hover::after { content: ''; position: absolute; opacity: 0.22; }
.board.can-place[data-turn="x"] .cell:not(.filled):hover::before,
.board.can-place[data-turn="x"] .cell:not(.filled):hover::after {
  left: 50%; top: 18%; width: 10%; height: 64%; margin-left: -5%;
  border-radius: 999px;
  background: var(--x);
  transform: rotate(45deg);
}
.board.can-place[data-turn="x"] .cell:not(.filled):hover::after { transform: rotate(-45deg); }
.board.can-place[data-turn="o"] .cell:not(.filled):hover::before { inset: 18%; border-radius: 50%; border: calc(var(--bw) / 3 * 0.1) solid var(--o); }
.board.can-place[data-turn="o"] .cell:not(.filled):hover::after { display: none; }

.cell.last { background: var(--hover); }
.board.over .cell:not(.win) svg { opacity: 0.3; }
.cell.win { background: var(--hover); }

.strike { position: absolute; inset: 0; z-index: 1; width: 100%; height: 100%; pointer-events: none; opacity: 0; }
.strike.show { opacity: 1; }
.strike line { stroke-width: 0.085; stroke-linecap: round; stroke-dasharray: 1; }
.strike line.x { stroke: var(--x); }
.strike line.o { stroke: var(--o); }
.strike.show line { animation: draw 0.35s 0.25s ease-out both; }

.actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 0.6rem; margin-top: 1rem; }
.actions:empty { display: none; }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(20, 16, 12, 0.45);
  animation: fade 0.25s ease;
}
.modal-box {
  width: min(340px, 100%);
  background: var(--surface);
  border-radius: 14px;
  padding: 1.6rem 1.4rem 1.3rem;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: rise 0.3s ease;
}
.modal-title { font-family: 'Fraunces', Georgia, serif; font-weight: 700; font-size: 1.8rem; }
.modal-title.won { color: var(--accent); }
.modal-title.lost { color: var(--bad); }
.modal-sub { font-size: 0.8rem; color: var(--ink-light); margin-top: 0.5rem; line-height: 1.6; }
.modal-actions { display: flex; gap: 0.6rem; justify-content: center; margin-top: 1.2rem; }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(12px); opacity: 0; } }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1.2rem + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  max-width: calc(100vw - 2rem);
  padding: 0.55rem 1rem;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.8rem;
  z-index: 30;
  overflow-wrap: anywhere;
}

#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 40; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
