:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --ink: #1d1b18;
  --muted: #6b665d;
  --line: #d9d4c8;
  --line-strong: #1d1b18;
  --x: #d6453d;
  --x-soft: #fbe3e1;
  --o: #2763c9;
  --o-soft: #dfe8f9;
  --play: #f2c94c;
  --play-soft: #fdf3d2;
  --primary: #1d1b18;
  --primary-ink: #ffffff;
  --error: #b42318;
  --radius: 10px;
  /* The big board: a soft frame instead of a solid black one. */
  --frame: #e3ddcf;
  --frame-shadow: 0 1px 2px rgba(60, 50, 30, .08), 0 10px 30px -8px rgba(60, 50, 30, .22);
  --play-cell: #ffffff;
  --play-glow: rgba(242, 201, 76, .55);
  /* A serif in the style of Claude's classic chat font (Tiempos), which is licensed. */
  --font: "Source Serif 4", "Iowan Old Style", Georgia, serif;
  /* Cards (how-to, chat, lobby rows...): a soft shadow instead of a plain outline. */
  --card-border: rgba(60, 50, 30, .08);
  --card-shadow: 0 1px 2px rgba(60, 50, 30, .05), 0 6px 20px -10px rgba(60, 50, 30, .2);
  --ok: #1f8a4c;
  --ok-ink: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15140f;
    --surface: #201e19;
    --ink: #f1ede4;
    --muted: #a39d90;
    --line: #3a372f;
    --line-strong: #f1ede4;
    --x: #ff6b61;
    --x-soft: #3d201d;
    --o: #6b9bff;
    --o-soft: #1c2840;
    --play: #f0c446;
    --play-soft: #2f2816;
    --primary: #f1ede4;
    --primary-ink: #15140f;
    --error: #ff7a6e;
    --frame: #2e2b24;
    --frame-shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 32px -8px rgba(0, 0, 0, .6);
    --play-cell: #3a3218;
    --play-glow: rgba(240, 196, 70, .5);
    --card-border: #34312a;
    --card-shadow: 0 1px 2px rgba(0, 0, 0, .35), 0 8px 24px -12px rgba(0, 0, 0, .6);
    --ok: #4cc67f;
    --ok-ink: #0d1a12;
  }
}

:root[data-theme="dark"] {
  --bg: #15140f;
  --surface: #201e19;
  --ink: #f1ede4;
  --muted: #a39d90;
  --line: #3a372f;
  --line-strong: #f1ede4;
  --x: #ff6b61;
  --x-soft: #3d201d;
  --o: #6b9bff;
  --o-soft: #1c2840;
  --play: #f0c446;
  --play-soft: #2f2816;
  --primary: #f1ede4;
  --primary-ink: #15140f;
  --error: #ff7a6e;
  --frame: #2e2b24;
  --frame-shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 32px -8px rgba(0, 0, 0, .6);
  --play-cell: #3a3218;
  --play-glow: rgba(240, 196, 70, .5);
  --card-border: #34312a;
  --card-shadow: 0 1px 2px rgba(0, 0, 0, .35), 0 8px 24px -12px rgba(0, 0, 0, .6);
  --ok: #4cc67f;
  --ok-ink: #0d1a12;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
}

[hidden] { display: none !important; }

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

.masthead h1 {
  margin: 0 0 20px;
  font-size: 28px;
  letter-spacing: -0.02em;
}

.link-home {
  all: unset;
  cursor: pointer;
}

h2 { margin: 0 0 16px; font-size: 22px; }

.lede { color: var(--muted); margin: 0 0 24px; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: var(--radius);
  border: 1.5px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-big { padding: 18px; font-size: 18px; flex: 1; }
.btn-small { padding: 6px 12px; font-size: 14px; }
.btn-quiet { border-color: var(--line); color: var(--muted); }

.choice { display: flex; gap: 12px; }
.row { display: flex; gap: 12px; justify-content: flex-end; }

/* Rules */
.rules {
  margin-top: 28px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.rules summary { cursor: pointer; font-weight: 600; list-style: none; display: flex; align-items: center; gap: 8px; }
/* Our own arrow instead of the browser's, so it can turn smoothly as the box opens and closes
   (js/app.js animates the box itself, with the same timing). */
.rules summary::-webkit-details-marker { display: none; }
.rules summary::before {
  content: '';
  flex: none;
  width: 0.55em;
  height: 0.65em;
  background: currentColor;
  clip-path: polygon(0 0, 100% 50%, 0 100%);
  transition: transform 0.6s cubic-bezier(0.65, 0, 0.35, 1);
}
.rules[open]:not(.closing) summary::before { transform: rotate(90deg); }
.rules ul { margin: 12px 0 4px; padding-left: 20px; color: var(--muted); }
.rules li { margin-bottom: 6px; }

/* Forms */
.form { display: grid; gap: 16px; }
.form label { display: grid; gap: 6px; font-weight: 600; }
.form input {
  font: inherit;
  font-weight: 400;
  padding: 12px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
}
.form input:focus { outline: none; border-color: var(--line-strong); }
.code-input {
  width: 100%;
  min-width: 0; /* let it shrink to the screen instead of its default 20-character width */
  font-size: 28px !important;
  letter-spacing: 0.3em;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.hint { margin: 0; color: var(--muted); font-size: 14px; }

/* Start options */
.options { margin: 0; padding: 0; border: 0; min-width: 0; }
.options legend { padding: 0; margin-bottom: 6px; font-weight: 600; }
.segmented {
  --n: 2;   /* number of options */
  --i: 0;   /* which one is picked, from 0 */
  position: relative;
  isolation: isolate;
  display: flex;
  padding: 3px;
  gap: 3px;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
}
.segmented:has(> label:nth-child(3)) { --n: 3; }
.segmented:has(> label:nth-child(2) input:checked) { --i: 1; }
.segmented:has(> label:nth-child(3) input:checked) { --i: 2; }
/* The black highlight is one block that slides to the picked option. The options are
   equal widths (flex: 1), so it is one option wide and moves by option + gap. */
.segmented::before {
  content: '';
  position: absolute;
  z-index: -1;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: calc((100% - 6px - (var(--n) - 1) * 3px) / var(--n));
  border-radius: 7px;
  background: var(--primary);
  transform: translateX(calc(var(--i) * (100% + 3px)));
  transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}
.form .segmented label { display: block; flex: 1; min-width: 0; font-weight: 600; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block;
  padding: 8px 10px;
  border-radius: 7px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}
.segmented input:checked + span { color: var(--primary-ink); }
@media (prefers-reduced-motion: reduce) {
  .segmented::before, .segmented span { transition: none; }
}
/* Browsers without :has() can't tell where to slide, so they colour the picked option directly. */
@supports not selector(:has(*)) {
  .segmented::before { display: none; }
  .segmented input:checked + span { background: var(--primary); }
}
.segmented input:focus-visible + span { outline: 2px solid var(--play); outline-offset: 1px; }
.form label.check { display: flex; align-items: flex-start; gap: 10px; font-weight: 600; cursor: pointer; }
.form .check input { width: 20px; height: 20px; margin: 2px 0 0; padding: 0; flex: none; accent-color: var(--primary); }
.check small { font-weight: 400; color: var(--muted); font-size: 13px; }

/* Lobby */
.lobby-heading { margin: 20px 0 8px; font-size: 15px; color: var(--muted); font-weight: 600; }
.lobby-heading:first-of-type { margin-top: 0; }
.lobby-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.lobby-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px 10px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.lobby-item > div { min-width: 0; }
.lobby-names { font-weight: 600; overflow-wrap: anywhere; }
.lobby-names .vs { margin: 0 4px; }
.lobby-item .meta { font-size: 13px; }
.lobby-empty { color: var(--muted); font-size: 14px; padding: 4px 0; }
#screen-lobby .btn { white-space: nowrap; }

/* Join: the game's details */
.join-details {
  display: grid;
  gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
}
.jd-who { font-size: 17px; text-align: center; overflow-wrap: anywhere; }
.jd-sides { display: flex; align-items: center; justify-content: center; gap: 16px; }
.jd-side { display: grid; justify-items: center; gap: 2px; min-width: 0; flex: 1; }
.jd-mark { font-size: 30px; line-height: 1; }
.jd-name { font-weight: 600; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.jd-note { font-size: 12px; color: var(--muted); }
.jd-visibility {
  padding-top: 10px;
  border-top: 1px solid var(--line);
  text-align: center;
  font-size: 14px;
  color: var(--muted);
}
.error { margin: 0; color: var(--error); font-size: 14px; min-height: 1em; }
.error:empty { display: none; }

/* Share code */
.share {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  margin-bottom: 16px;
  background: var(--play-soft);
  border: 1.5px dashed var(--play);
  border-radius: var(--radius);
}
.share-label { font-size: 14px; color: var(--muted); }
.share-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}
.share-code {
  font-size: 30px;
  font-weight: 700;
  letter-spacing: 0.2em;
  font-variant-numeric: tabular-nums;
}

/* Players */
.players {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 8px;
}
.player {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1.5px solid transparent;
  min-width: 0;
}
.player { transition: background .2s, border-color .2s, box-shadow .2s; }
/* Whose turn it is: their pill fills with their colour. */
#player-X.turn { border-color: var(--x); background: var(--x-soft); box-shadow: 0 2px 10px -4px var(--x); }
#player-O.turn { border-color: var(--o); background: var(--o-soft); box-shadow: 0 2px 10px -4px var(--o); }
.player .pname {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 150px;
}
.player .you { font-size: 12px; color: var(--muted); font-weight: 400; }
.vs { color: var(--muted); font-size: 14px; }

.mark { display: inline-flex; vertical-align: -0.15em; }
.mark .mark-svg { width: 1.05em; height: 1.05em; }
.mark-X { color: var(--x); }
.mark-O { color: var(--o); }
.jd-mark .mark-svg { width: 34px; height: 34px; }

.status {
  text-align: center;
  margin: 0 0 16px;
  min-height: 1.5em;
  font-weight: 600;
}

/* Board */
.big-board {
  --pad: 8px;
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: var(--pad);
  padding: var(--pad);
  background: var(--frame);
  border-radius: 14px;
  box-shadow: var(--frame-shadow);
  aspect-ratio: 1;
  width: 100%;
}

.small-board {
  --pad: 4px;
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  min-width: 0;
  min-height: 0;
  gap: 3px;
  padding: var(--pad);
  background: var(--surface);
  border-radius: 8px;
  transition: background 0.2s;
}
/* The board(s) you can play in: yellow outline with a slow glow, so it's easy to find in
   either theme. */
.small-board.playable {
  z-index: 1;
  background: var(--play-soft);
  box-shadow: inset 0 0 0 2.5px var(--play), 0 0 0 0 var(--play-glow);
  animation: play-glow 2.4s ease-in-out infinite;
}
@keyframes play-glow {
  50% { box-shadow: inset 0 0 0 2.5px var(--play), 0 0 16px 2px var(--play-glow); }
}
.small-board.won-line { box-shadow: inset 0 0 0 3px var(--play); }

.cell {
  all: unset;
  position: relative;
  display: grid;
  place-items: center;
  border-radius: 4px;
  background: var(--bg);
  line-height: 1;
  min-width: 0;
  min-height: 0;
  transition: background 0.15s, box-shadow 0.15s;
}
.cell .mark-svg { width: 72%; height: 72%; overflow: visible; }
.small-board.playable .cell:not(.filled) { cursor: pointer; background: var(--play-cell); }
.cell.X { color: var(--x); }
.cell.O { color: var(--o); }
/* The last move: the square tinted in the mover's colour. */
.cell.last { background: color-mix(in srgb, currentColor 20%, var(--surface)); }
/* Selected but not submitted yet: a dashed outline that runs around the square clockwise
   ("marching ants") and a lighter mark. The outline is an SVG rounded rectangle (js/app.js,
   antsSvg) so the dashes follow the square's rounded corners. Its line sits 2px in from the
   edge with a 2px corner radius: the square's own 4px radius minus 2px, so the corners are
   concentric. pathLength="100" on the rectangle makes the dash numbers below fractions of the
   way round: 12 dashes and 12 gaps, whatever the square's size. */
.cell.pending {
  background: color-mix(in srgb, currentColor 12%, var(--play-cell)) !important;
}
.ants {
  position: absolute;
  inset: 2px;
  width: calc(100% - 4px);
  height: calc(100% - 4px);
  overflow: visible;
  pointer-events: none;
}
.ants rect {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-dasharray: 4.1667 4.1667;
  /* Moving the pattern forward along the rectangle, which runs clockwise. One dash and one
     gap per 0.85 s, about 11px a second on a phone. */
  animation: ants 0.85s linear infinite;
}
@keyframes ants { to { stroke-dashoffset: -8.3333; } }
@media (prefers-reduced-motion: reduce) {
  .ants rect { animation: none; }
}
.cell.pending .mark-svg { opacity: 0.6; }
.cell:focus-visible { outline: 2px solid var(--ink); outline-offset: 1px; }

/* A faint preview of your mark on the square under the pointer (or keyboard focus). */
.ghost { opacity: 0; transition: opacity 0.12s; }
.cell:focus-visible .ghost { opacity: 0.3; }
@media (hover: hover) {
  .small-board.playable .cell:not(.filled):hover { box-shadow: inset 0 0 0 2px var(--play); }
  .small-board.playable .cell:not(.filled):hover .ghost { opacity: 0.3; }
}

/* X and O, drawn as strokes. */
.mark-svg .stroke,
.strike .stroke {
  fill: none;
  stroke: currentColor;
  stroke-width: 13;
  stroke-linecap: round;
}

/* A won small board: tinted in the winner's colour, its squares faded, a line through the
   three in a row and the winner's mark on top. */
.small-board.won-X { background: var(--x-soft); }
.small-board.won-O { background: var(--o-soft); }
.small-board.won .cell { opacity: 0.22; }
.strike {
  position: absolute;
  top: var(--pad);
  left: var(--pad);
  width: calc(100% - 2 * var(--pad));
  height: calc(100% - 2 * var(--pad));
  overflow: visible;
  pointer-events: none;
}
.strike-X { color: var(--x); }
.strike-O { color: var(--o); }
.strike-small .stroke { stroke-width: 4; opacity: 0.45; }

.board-owner {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
  line-height: 1;
  pointer-events: none;
  z-index: 3; /* above the winning line across the big board */
}
.board-owner .mark-svg { width: 74%; height: 74%; overflow: visible; }
.board-owner .stroke { stroke-width: 11; }
.board-owner.X { color: var(--x); }
.board-owner.O { color: var(--o); }
.board-owner.D {
  color: var(--muted);
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  font-weight: 700;
  font-size: 18px;
}

/* The winning line across the whole board. */
.strike-big { z-index: 2; filter: drop-shadow(0 2px 6px rgba(0, 0, 0, .25)); }
.strike-big .stroke { stroke-width: 1.7; }
.strike-big .halo { stroke: var(--surface); stroke-width: 3.1; }
.strike-small .halo { display: none; }

/* Drawing things in. --since (set by js/app.js) is how long ago each thing appeared, as a
   negative delay, so a re-render carries on the animation instead of restarting it. */
.animate .stroke,
.animate.strike .stroke {
  stroke-dasharray: 1 2;
  animation: draw 0.3s cubic-bezier(.4, 0, .2, 1) both;
  animation-delay: var(--since);
}
.animate .s2 { animation-delay: calc(var(--since) + 140ms); }
.board-owner.animate { animation: pop 0.4s cubic-bezier(.3, 1.4, .5, 1) both; animation-delay: calc(var(--since) + 300ms); }
.board-owner.animate .stroke { animation-duration: 0.35s; animation-delay: calc(var(--since) + 300ms); }
.board-owner.animate .s2 { animation-delay: calc(var(--since) + 450ms); }
.animate.strike-big .stroke { animation-duration: 0.5s; animation-delay: calc(var(--since) + 600ms); }
@keyframes draw {
  0% { stroke-dashoffset: 1; opacity: 0; }
  1% { opacity: 1; }
  100% { stroke-dashoffset: 0; opacity: 1; }
}
@keyframes pop {
  0% { transform: scale(.6); }
  100% { transform: scale(1); }
}
.strike-small.animate .stroke { opacity: 0.45; }
@keyframes draw-faint {
  0% { stroke-dashoffset: 1; opacity: 0; }
  1% { opacity: 0.45; }
  100% { stroke-dashoffset: 0; opacity: 0.45; }
}
.strike-small.animate .stroke { animation-name: draw-faint; }

@media (prefers-reduced-motion: reduce) {
  .small-board.playable,
  .board-owner.animate,
  .animate .stroke { animation: none !important; }
}

.actions { margin-top: 16px; }

.footer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 24px;
  gap: 12px;
}
.meta { color: var(--muted); font-size: 13px; }

@media (max-width: 420px) {
  .big-board { --pad: 5px; }
  .small-board { --pad: 3px; gap: 2px; }
  .choice { flex-direction: column; }
  .share-code { font-size: 24px; }
}

.theme-toggle {
  position: fixed;
  left: 12px;
  bottom: 12px;
  z-index: 2147483647;
  box-sizing: border-box;
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg);
  box-shadow: 0 2px 8px rgba(0, 0, 0, .35);
  cursor: pointer;
}
/* Same look as the theme button beside it, in both light and dark mode. */
.feedback-fab {
  position: fixed;
  right: 12px;
  bottom: 12px;
  z-index: 2147483647;
  box-sizing: border-box;
  height: 36px;
  padding: 0 14px;
  display: flex;
  align-items: center;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font: 600 14px/1 var(--font);
  text-decoration: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .35);
}
.theme-toggle svg { width: 20px; height: 20px; flex: none; display: none; }
.theme-toggle[data-state="light"] .icon-sun,
.theme-toggle[data-state="dark"] .icon-moon { display: block; }

/* The credit sits at the very end of the page (pushed to the bottom of the screen on
   short pages), not over the board. Once you scroll to the end it lines up in the same
   row as the fixed theme and feedback buttons, between them. */
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  /* On phones, 100vh is the height with the browser's address bar hidden, which is taller than
     what's on screen when the page loads, so a page that fits would still scroll. svh is the
     height you actually see with the bars showing. */
  min-height: 100svh;
  padding-bottom: 12px; /* the buttons sit 12px from the bottom */
}
.app { width: 100%; }
.credit {
  margin: auto 0 0;
  padding: 8px 16px 0;
  font-size: 11px;
  line-height: 36px; /* the buttons' height, so the text is centred on their row */
  text-align: center;
  color: var(--muted);
}
/* Too narrow to fit between the buttons: sit just above them instead. */
@media (max-width: 359px) {
  body { padding-bottom: 56px; }
  .credit { line-height: 1.5; }
}
@media (max-width: 400px) {
  .credit { font-size: 10px; }
}

/* ---------- Game layout with chat ---------- */

/* The game screen is wider so the chat can sit beside the board. The board column
   keeps the same width the board had before. */
.app.in-game { max-width: 960px; }

.game-layout { display: block; }
.game-main { max-width: 528px; margin: 0 auto; }

/* The column beside (or, on narrow screens, under) the board: the chat, and under it the
   number of spectators. */
.side {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 528px;
  margin: 24px auto 0;
}

.chat {
  display: flex;
  flex-direction: column;
  height: 340px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

@media (min-width: 900px) {
  .game-layout {
    display: grid;
    grid-template-columns: minmax(0, 528px) minmax(260px, 1fr);
    gap: 24px;
    align-items: start;
  }
  .game-main { margin: 0; }
  .side {
    position: sticky;
    top: 16px;
    max-width: none;
    height: min(620px, calc(100vh - 48px));
    margin: 0;
  }
  .chat { flex: 1; min-height: 0; height: auto; }
}

.spectators {
  flex: none;
  margin: 0;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-align: center;
  font-size: 14px;
  color: var(--muted);
}
.spectators strong { color: var(--ink); font-variant-numeric: tabular-nums; }

.chat-title { margin: 0 0 8px; font-size: 16px; }

.chat-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-empty { margin: auto; color: var(--muted); font-size: 14px; text-align: center; }

.msg { max-width: 85%; align-self: flex-start; }
.msg.mine { align-self: flex-end; }
.msg-name { font-size: 12px; color: var(--muted); margin: 0 6px 2px; }
.msg.mine .msg-name { text-align: right; }
.msg.mine .msg-text { margin-left: auto; }
.msg-text {
  /* As wide as its own text, not as wide as the name above it. */
  width: fit-content;
  max-width: 100%;
  margin: 0;
  padding: 7px 11px;
  border-radius: 14px;
  background: var(--bg);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.from-X .msg-text { background: var(--x-soft); }
.msg.from-O .msg-text { background: var(--o-soft); }
.msg.sending { opacity: 0.55; }

.chat .error { margin-top: 6px; }
.chat-form { display: flex; gap: 8px; margin-top: 8px; }
.chat-form input {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 8px 10px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
}
.chat-form input:focus { outline: none; border-color: var(--line-strong); }
.chat-form input:disabled { opacity: 0.5; }
.chat-note a { color: inherit; }
.chat-note { margin: 6px 0 0; font-size: 11px; line-height: 1.4; color: var(--muted); }

/* ---------- Wide screens: how-to | board | chat, all on one screen ---------- */
@media (min-width: 1000px) {
  .app.in-game {
    max-width: 1400px;
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
    grid-template-areas:
      "head  head"
      "rules game";
    column-gap: 24px;
    align-items: start;
  }
  .app.in-game .masthead { grid-area: head; }
  .app.in-game #screen-game { grid-area: game; }
  .app.in-game .rules {
    grid-area: rules;
    margin-top: 0;
    max-height: calc(100vh - 150px);
    overflow-y: auto;
  }

  .app.in-game .game-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(300px, 400px);
    gap: 24px;
    align-items: start;
  }
  .app.in-game .game-main { max-width: none; margin: 0; }

  /* Size the board by the window height so nothing needs scrolling. */
  .app.in-game { --board: min(100%, max(320px, calc(100vh - 400px))); }
  /* The fixed credit/Feedback row already reserves the bottom of the window. */
  .app.in-game { padding-bottom: 0; }
  /* The "send this code" box takes room until the opponent joins. */
  .app.in-game:has(#share:not([hidden])) { --board: min(100%, max(300px, calc(100vh - 480px))); }
  .app.in-game .masthead h1 { margin-bottom: 12px; }
  .app.in-game .share { padding: 8px 14px; margin-bottom: 10px; }
  .app.in-game .share-code { font-size: 24px; }
  .app.in-game .status { margin-bottom: 10px; }
  .app.in-game .actions { margin-top: 10px; }
  .app.in-game .footer-row { margin-top: 10px; }
  .app.in-game .big-board,
  .app.in-game .share,
  .app.in-game .actions,
  .app.in-game .footer-row {
    width: var(--board);
    margin-left: auto;
    margin-right: auto;
  }

  .app.in-game .side {
    position: sticky;
    top: 16px;
    max-width: none;
    height: calc(100vh - 150px);
    min-height: 360px;
    margin: 0;
  }
  .app.in-game .chat { padding: 16px; }
  .app.in-game .chat-title { font-size: 18px; }
  .app.in-game .chat-form input { padding: 11px 12px; font-size: 15px; }
}

/* Grouped chat messages sit closer together. */
.msg.continued { margin-top: -5px; }

/* Spectators: no chat, just the number of spectators. */
#leave { white-space: nowrap; }
@media (max-width: 999px) {
  /* The board centred, with the count under it. */
  .app.watching .game-layout { display: block; }
  .app.watching .game-main { margin: 0 auto; }
  .app.watching .side { position: static; height: auto; max-width: 528px; margin: 16px auto 0; }
}
@media (min-width: 1000px) {
  /* The right column as wide as the how-to on the left, so the board sits in the middle
     of the page; the count sits at its top, level with the players' names. */
  .app.in-game.watching .game-layout { grid-template-columns: minmax(0, 1fr) 260px; }
  .app.in-game.watching .side { height: auto; min-height: 0; }
}

/* ---------- Typography ---------- */

/* Source Serif 4 (Google Fonts) for the whole site, set on body. Headings use semibold, not
   bold: a serif at 700 reads heavy. */
.masthead h1 { font-size: 31px; font-weight: 600; letter-spacing: -0.015em; }
h2 { font-weight: 600; letter-spacing: -0.01em; }
.status { font-size: 17px; font-weight: 600; }

/* ---------- Home ---------- */

.hero {
  display: grid;
  justify-items: center;
  gap: 18px;
  margin: 4px 0 24px;
}
.hero .lede { margin: 0; max-width: 36ch; text-align: center; }
.hero-board {
  --pad: 5px;
  width: min(210px, 58vw);
  transition: opacity 0.4s;
}
.hero-board.hero-fade { opacity: 0; }
.hero-board .small-board { --pad: 3px; gap: 2px; border-radius: 6px; }
/* Concentric corners: the frame's radius is the mini boards' radius plus the gap around them. */
.hero-board { border-radius: calc(6px + var(--pad)); }
.hero-board .cell { border-radius: 2px; cursor: default !important; box-shadow: none !important; }
.hero-board .mark-svg .stroke { stroke-width: 16; }
.hero-board .strike-small .stroke { stroke-width: 6; }
.hero-board .board-owner .stroke { stroke-width: 13; }
.hero-board .board-owner.D { font-size: 10px; }

/* The three home buttons: an icon, a title and a line about what each does. */
.btn-card {
  display: grid;
  grid-template-areas: "icon" "title" "sub";
  justify-items: start;
  align-content: start;
  gap: 2px;
  padding: 16px;
  text-align: left;
  border-radius: 14px;
  box-shadow: var(--card-shadow);
  transition: transform 0.15s, box-shadow 0.15s;
}
.btn-card:hover:not(:disabled) { box-shadow: 0 10px 24px -10px rgba(0, 0, 0, .45); }
/* The opposite colours to the theme and Feedback buttons: light cards in light mode,
   dark cards in dark mode. */
.btn-card.btn-primary {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--card-border);
}
.btn-card .btn-sub { color: var(--muted); opacity: 1; }
.btn-icon {
  grid-area: icon;
  width: 22px;
  height: 22px;
  padding: 7px;
  margin-bottom: 8px;
  box-sizing: content-box;
  border-radius: 10px;
  fill: none;
  stroke: #ffffff;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
#go-start .btn-icon { background: var(--play); stroke: #1d1b18; }
#go-join .btn-icon { background: var(--x); }
#go-lobby .btn-icon { background: var(--o); }
.btn-title { grid-area: title; font-size: 19px; font-weight: 600; }
.btn-sub {
  grid-area: sub;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.35;
  opacity: 0.72;
}
/* Phones: the buttons are stacked, so the icon sits to the left of the text. */
@media (max-width: 420px) {
  .btn-card {
    grid-template-areas: "icon title" "icon sub";
    grid-template-columns: auto 1fr;
    column-gap: 14px;
    align-items: center;
    padding: 14px 16px;
  }
  .btn-icon { margin-bottom: 0; }
  .hero { gap: 14px; margin-bottom: 20px; }
}

/* ---------- Cards ---------- */

.rules,
.lobby-item,
.join-details,
.chat,
.spectators {
  border: 1px solid var(--card-border);
  box-shadow: var(--card-shadow);
}
.rules, .chat { border-radius: 14px; }

/* ---------- Motion ---------- */

/* Each screen fades in when it's shown. */
.screen:not([hidden]) { animation: screen-in 0.28s ease-out; }
@keyframes screen-in {
  from { opacity: 0; transform: translateY(6px); }
}

/* Copy buttons: "Copied ✓" in green for a moment. */
.btn.copied { background: var(--ok); border-color: var(--ok); color: var(--ok-ink); }

/* The result: the status line turns into a banner in the winner's colour. */
.status.result {
  width: fit-content;
  max-width: 100%;
  margin-left: auto;
  margin-right: auto;
  padding: 6px 18px;
  border-radius: 999px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  font-size: 18px;
  animation: pop 0.4s cubic-bezier(.3, 1.4, .5, 1);
}
.status.result-win-X { background: var(--x-soft); border-color: var(--x); color: var(--x); }
.status.result-win-O { background: var(--o-soft); border-color: var(--o); color: var(--o); }
.status.result-draw { background: var(--play-soft); border-color: var(--play); }
.status[class*="result-lose"] { color: var(--muted); }

.confetti {
  position: fixed;
  inset: 0;
  z-index: 2147483646;
  overflow: hidden;
  pointer-events: none;
}
.confetti i {
  position: absolute;
  top: -16px;
  width: 9px;
  height: 14px;
  border-radius: 2px;
  animation: confetti-fall 2.4s cubic-bezier(.25, .4, .6, 1) both;
}
@keyframes confetti-fall {
  0% { transform: translate(0, 0) rotate(0); opacity: 1; }
  85% { opacity: 1; }
  100% { transform: translate(var(--drift), 105vh) rotate(var(--spin)); opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .screen:not([hidden]),
  .status.result { animation: none; }
  .rules summary::before { transition: none; }
}
