:root {
  --cs: 52px;              /* cell size */
  --paper: #fdfcf9;
  --ink: #1b1a17;
  --line: #b9b4a8;
  --line-strong: #6c6659;
  --def-bg: #e6e3da;
  --arrow: #d63a2f;
  --sel: #ffe08a;
  --word: #fff5d6;
  --bg: #f2efe8;
  --panel: #fff;
  --muted: #6f6a60;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #1d1c1a;
    --ink: #ece8df;
    --line: #4a463f;
    --line-strong: #8a8377;
    --def-bg: #2b2925;
    --arrow: #ff6b5e;
    --sel: #7a5f14;
    --word: #3a3221;
    --bg: #141311;
    --panel: #1d1c1a;
    --muted: #9a938a;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- top bar ---------- */

/* Header + current definition travel together: on a phone the definition bar
   is the only place the clue text is legible, so it must never scroll away. */
.sticky-top {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  flex-wrap: wrap;
}
.brand { font-size: 16px; }
.narrow { display: none; }
.spacer { flex: 1; }
.label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.room { display: flex; align-items: center; gap: 8px; }
.room code {
  background: var(--def-bg);
  padding: 2px 8px;
  border-radius: 5px;
  font-size: 13px;
}

button.ghost, a.ghost {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  text-decoration: none;
}
button.ghost:hover, a.ghost:hover { background: var(--def-bg); }
button.danger:hover { background: #f8d7d5; color: #8a1c14; border-color: #d99; }

.status { display: flex; gap: 6px; }
.pill {
  font-size: 12px;
  padding: 3px 9px;
  border-radius: 99px;
  background: var(--def-bg);
  color: var(--muted);
  white-space: nowrap;
}
.pill.online { background: #d3f0dc; color: #14562f; }
.pill.offline { background: #f4d9d6; color: #7d241c; }
@media (prefers-color-scheme: dark) {
  .pill.online { background: #17402a; color: #8fe0ae; }
  .pill.offline { background: #4a201c; color: #ffab9f; }
}

.peers { display: flex; gap: 6px; flex-wrap: wrap; }
.peer {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  padding: 3px 9px 3px 6px;
  border-radius: 99px;
  background: var(--def-bg);
}
.peer .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.peer.me { outline: 1px solid var(--line-strong); }

/* ---------- layout ---------- */

main {
  padding: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.current {
  min-height: 36px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 14px;
  border-top: 1px solid var(--line);
  background: var(--def-bg);
  font-size: 14px;
}
.current .dir {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  flex: none;
}
.current strong { min-width: 0; }
.current .len { color: var(--muted); font-size: 13px; margin-left: auto; flex: none; }
.muted { color: var(--muted); }

.grid-wrap { overflow: auto; max-width: 100%; padding-bottom: 4px; }

.grid {
  /* Always fit the viewport width, never wider than the desktop cell size.
     --cols is set by the renderer, so this adapts to any grid shape. */
  --cs: min(52px, calc((100vw - 34px) / var(--cols)));
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cs));
  grid-auto-rows: var(--cs);
  width: max-content;
  background: var(--line-strong);
  border: 2px solid var(--line-strong);
  gap: 1px;
  user-select: none;
  touch-action: manipulation;
}

/* ---------- cells ---------- */

.cell {
  position: relative;
  background: var(--paper);
  overflow: visible;
}

.cell.letter {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: calc(var(--cs) * 0.55);
  font-weight: 600;
  text-transform: uppercase;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.cell.letter.in-word { background: var(--word); }
.cell.letter.selected { background: var(--sel); }
.cell.letter .ch { display: block; line-height: 1; }
.cell.letter .ch.fresh { animation: pop .35s ease-out; }
@keyframes pop { from { transform: scale(1.28); } to { transform: scale(1); } }

/* --peer carries the player's colour: the label needs it for its background
   while keeping white text, so currentColor can't do both jobs. */
.cell.letter .peer-cursor {
  position: absolute;
  inset: 0;
  border: 2px solid var(--peer);
  pointer-events: none;
  z-index: 4;
}
.cell.letter .peer-cursor::after {
  content: attr(data-name);
  /* The cell uppercases its letter; the player's name should stay as typed. */
  text-transform: none;
  position: absolute;
  bottom: 100%;
  left: -2px;
  margin-bottom: 1px;
  font-size: 9px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 3px 3px 3px 0;
  background: var(--peer);
  color: #fff;
  white-space: nowrap;
  font-weight: 600;
  max-width: calc(var(--cs) * 3);
  overflow: hidden;
  text-overflow: ellipsis;
}
.cell.letter .peer-cursor.below::after {
  bottom: auto;
  top: 100%;
  margin: 1px 0 0;
  border-radius: 0 3px 3px 3px;
}

.cell.def {
  background: var(--def-bg);
  display: grid;
  padding: 1px;
  gap: 1px;
  cursor: default;
}
.cell.def.rows-2 { grid-template-rows: 1fr 1fr; }
.cell.def .txt {
  /* --fit is set per cell so long definitions still fit the box. */
  font-size: calc(var(--cs) * var(--fit, 0.165));
  line-height: 1.06;
  text-align: center;
  align-self: center;
  text-transform: uppercase;
  font-weight: 600;
  letter-spacing: -0.01em;
  overflow: hidden;
  cursor: pointer;
  hyphens: auto;
  overflow-wrap: anywhere;
}
.cell.def .txt:hover { text-decoration: underline; }

/* Two definitions in one cell: a full-width rule between them. */
.cell.def.rows-2::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 1px;
  background: var(--line-strong);
}

/* Arrows are SVG so every one has the same hairline stroke, and they start
   exactly on the cell border rather than floating inside it. */
.arrow {
  position: absolute;
  width: calc(var(--cs) * 0.42);
  height: calc(var(--cs) * 0.42);
  overflow: visible;
  pointer-events: none;
  z-index: 2;
}
.arrow path {
  fill: none;
  stroke: var(--arrow);
  stroke-width: 1.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.arrow.at-right { left: 100%; transform: translateY(-50%); }
.arrow.at-bottom { top: 100%; transform: translateX(-50%); }

.hint { color: var(--muted); font-size: 12.5px; margin: 12px 0 0; }
kbd {
  font: inherit;
  font-size: 11px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 4px;
  background: var(--panel);
}

.ghost-input {
  position: fixed;
  opacity: 0;
  pointer-events: none;
  width: 1px;
  height: 1px;
  top: 50%;
  left: 50%;
  /* iOS zooms the page when focusing an input under 16px. */
  font-size: 16px;
}

/* ---------- dialog ---------- */

dialog {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--ink);
  padding: 20px 22px;
  max-width: 340px;
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin: 0 0 6px; font-size: 17px; }
dialog p { margin: 0 0 14px; font-size: 13px; color: var(--muted); }
dialog input {
  font: inherit;
  font-size: 16px;
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
}
dialog menu { display: flex; justify-content: flex-end; margin: 14px 0 0; padding: 0; }
dialog button {
  font: inherit;
  padding: 7px 14px;
  border-radius: 6px;
  border: 1px solid var(--line-strong);
  background: var(--ink);
  color: var(--paper);
  cursor: pointer;
}

/* ---------- phones ---------- */

@media (max-width: 720px) {
  .bar { gap: 7px; padding: 6px 9px; }
  .brand strong { display: none; }
  .wide, .room .label { display: none; }
  .narrow { display: inline; }
  .peer { padding: 3px 7px 3px 5px; font-size: 11px; }
  .pill { font-size: 11px; padding: 2px 7px; }
  .current { padding: 6px 10px; font-size: 13px; }
  .current .len { font-size: 11px; }
  /* Connection state shrinks to a dot so the bar stays on one line. */
  #conn {
    font-size: 0;
    width: 10px;
    height: 10px;
    padding: 0;
    flex: none;
  }
  main { padding: 8px 4px; }
  /* No physical keyboard to advertise. */
  .hint { display: none; }
}

/* Name tags on remote cursors would cover neighbouring letters on a small
   grid, so shrink them with the cells. */
@media (max-width: 720px) {
  .cell.letter .peer-cursor::after { font-size: 8px; top: -11px; }
}

/* Touch devices: no hover states, and a slightly heavier selection outline
   since the highlight colour alone is hard to spot on a small screen. */
@media (hover: none) {
  button.ghost:hover, a.ghost:hover, .cell.def .txt:hover { background: none; text-decoration: none; }
  .cell.letter.selected { box-shadow: inset 0 0 0 2px var(--line-strong); }
}
