:root {
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --text: #22201c;
  --muted: #6f695e;
  --line: #e2dccf;
  --tile: #fffaf0;
  --tile-edge: #d9cfbb;
  --tile-text: #1f1c17;
  --accent: #2f7d5b;
  --accent-text: #ffffff;
  --typed: #d9a520;
  --bad: #b3412f;
  --info: #8a6a12;
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --surface: #202226;
    --text: #ece9e2;
    --muted: #9c978d;
    --line: #33363b;
    --tile: #2b2e33;
    --tile-edge: #15171a;
    --tile-text: #f5f1e8;
    --accent: #4fbf8b;
    --accent-text: #0d1f17;
    --typed: #e8b93a;
    --bad: #ef7d6a;
    --info: #e0c065;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  padding: env(safe-area-inset-top, 0px) 16px env(safe-area-inset-bottom, 0px);
}

#loading {
  text-align: center;
  margin-top: 30vh;
  color: var(--muted);
}

.app {
  max-width: 64rem;
  margin: 0 auto;
  padding-block: 1.25rem 2rem;
}

header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

h1 {
  margin: 0;
  font-size: 1.6rem;
  letter-spacing: 0.02em;
}

.stats {
  display: flex;
  gap: 1rem;
  align-items: baseline;
  font-variant-numeric: tabular-nums;
}

.points {
  color: var(--muted);
}

.clock {
  font-size: 1.6rem;
  font-weight: 700;
  min-width: 4.5ch;
  text-align: right;
}

.clock.low {
  color: var(--bad);
}

.play {
  display: grid;
  grid-template-columns: minmax(0, 30rem) minmax(0, 1fr);
  gap: 2rem;
  align-items: start;
}

@media (max-width: 52rem) {
  .play {
    grid-template-columns: minmax(0, 1fr);
    gap: 1.25rem;
  }
  .board {
    max-width: 30rem;
    margin-inline: auto;
    width: 100%;
  }
}

/* --- Board ------------------------------------------------------------- */

/* Half a gap of padding on every side puts each tile centre at
   (col + 0.5) / cols of the box, so the SVG overlay can use cell units. */
.board {
  --gap: clamp(6px, 2vw, 12px);
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: var(--gap);
  padding: calc(var(--gap) / 2);
  user-select: none;
}

.board.paused {
  aspect-ratio: 1;
  place-items: center;
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 1.25rem;
}

.tile {
  position: relative;
  z-index: 1;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  background: var(--tile);
  color: var(--tile-text);
  border-radius: var(--radius);
  box-shadow: 0 3px 0 var(--tile-edge), 0 1px 2px rgba(0, 0, 0, 0.08);
  font-size: clamp(1.8rem, 9vw, 3.2rem);
  font-weight: 800;
  line-height: 1;
}

.tile .step {
  position: absolute;
  top: 0.35em;
  left: 0.5em;
  font-size: clamp(0.65rem, 2.2vw, 0.85rem);
  font-weight: 700;
  opacity: 0;
  animation: appear 0.2s ease-out both;
  animation-delay: calc(var(--step) * 140ms);
}

@keyframes appear {
  to {
    opacity: 0.85;
  }
}

.tile.typed {
  box-shadow: 0 3px 0 var(--tile-edge), inset 0 0 0 3px var(--typed);
}

.tile.traced {
  animation: light 0.25s ease-out both;
  animation-delay: calc(var(--step) * 140ms);
}

@keyframes light {
  from {
    background: var(--tile);
    color: var(--tile-text);
    transform: scale(1);
  }
  60% {
    transform: scale(1.06);
  }
  to {
    background: var(--accent);
    color: var(--accent-text);
    transform: scale(1);
  }
}

.trace {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}

.trace polyline {
  fill: none;
  stroke: var(--accent);
  stroke-width: 0.16;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: draw var(--draw) linear forwards;
}

@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}

/* The word being spelled by touch: a steady line in the typing colour. */
.trace.spelling polyline {
  stroke: var(--typed);
  stroke-width: 0.12;
  stroke-dasharray: none;
  stroke-dashoffset: 0;
  animation: none;
}

/* Touches on the board spell words instead of scrolling or zooming the page,
   and a long press does not open the browser's callout. */
.board-input {
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.board-input .tile {
  cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
  .tile.traced {
    animation-duration: 0s;
    animation-delay: 0s;
  }
  .trace polyline,
  .tile .step {
    animation-duration: 0s;
    animation-delay: 0s;
  }
}

/* --- Entry and controls ------------------------------------------------ */

.side {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  min-width: 0;
}

.entry {
  display: flex;
  gap: 0.5rem;
}

.entry input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 1.4rem;
  padding: 0.6rem 0.8rem;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  text-transform: lowercase;
}

.entry input:focus {
  outline: none;
  border-color: var(--accent);
}

button {
  font: inherit;
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--muted);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  padding-inline: 1.2rem;
}

button.danger {
  border-color: var(--bad);
  color: var(--bad);
}

/* Two groups (playing, board) that wrap as whole units on narrow screens. */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
  justify-content: space-between;
}

.controls .group {
  display: flex;
  gap: 0.4rem;
}

.controls button:not(.icon) {
  padding-inline: 0.75rem;
}

/* A button with a small icon button joined to its right edge. */
.split {
  display: inline-flex;
}

.split > button:first-child {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.split > button.icon {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-left-width: 0;
  padding-inline: 0.6rem;
  display: inline-grid;
  place-items: center;
}

.split > button.primary.icon {
  border-left: 1px solid color-mix(in srgb, var(--accent-text) 35%, transparent);
}

.glyph {
  width: 1.05em;
  height: 1.05em;
}

.message {
  margin: 0;
  min-height: 1.4em;
}

.message.good {
  color: var(--accent);
  font-weight: 600;
}

.message.bad {
  color: var(--bad);
}

.message.info {
  color: var(--info);
}

.message.hint {
  color: var(--muted);
}

.confirm {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.final {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.75rem;
  padding: 0.6rem 0.9rem;
  border: 2px solid var(--accent);
  border-radius: 10px;
}

.final strong {
  font-size: 1.4rem;
}

.final .label {
  color: var(--muted);
}

.final .solo {
  margin-left: auto;
  font-size: 0.9rem;
}

/* --- Word list ---------------------------------------------------------- */

.list h2 {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--muted);
  margin: 0.5rem 0 0.6rem;
}

.words {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  max-height: 22rem;
  overflow-y: auto;
}

.word {
  display: inline-flex;
  gap: 0.45rem;
  align-items: baseline;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
}

.word .score {
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.word.mine {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
  font-weight: 600;
}

.word.unlisted {
  border-style: dashed;
  border-color: var(--info);
  font-style: italic;
}

.word.unlisted .score {
  color: var(--info);
}

.list h3 {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  margin: 1rem 0 0.5rem;
}

.word.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.word.selected .score {
  color: inherit;
}

.word.theirs {
  border-style: dotted;
  border-color: var(--muted);
}

.word.cancelled .score,
.word.struck .score {
  color: var(--muted);
  text-decoration: line-through;
}

.word.cancelled {
  opacity: 0.75;
}

.word.struck span:last-child {
  text-decoration: line-through;
  text-decoration-color: var(--bad);
}

.lookup {
  margin: 0 0 0.75rem;
  padding: 0.5rem 0.75rem;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.lookup p {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.8rem;
  margin: 0;
}

.lookup p + p {
  margin-top: 0.4rem;
}

button.small {
  padding: 0.15rem 0.6rem;
  font-size: 0.85rem;
}

/* --- Multiplayer -------------------------------------------------------- */

.versus {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.8rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.versus h2 {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--muted);
  margin: 0;
}

.versus .row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.versus input {
  flex: 1 1 10rem;
  min-width: 0;
  font: inherit;
  padding: 0.45rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}

.versus input:focus {
  outline: none;
  border-color: var(--accent);
}

.versus-note {
  margin: 0;
  color: var(--muted);
}

.mode {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.6rem;
}

.mode .label,
.standings .label {
  color: var(--muted);
}

.segmented {
  display: inline-flex;
}

.segmented button {
  padding: 0.3rem 0.75rem;
  font-size: 0.9rem;
}

.segmented button:first-child {
  border-radius: 10px 0 0 10px;
}

.segmented button:last-child {
  border-radius: 0 10px 10px 0;
  border-left: 0;
}

.segmented button.on {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.standings {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.2rem;
  counter-reset: place;
}

.standings li {
  display: grid;
  grid-template-columns: 1.5rem minmax(0, 1fr) auto auto;
  gap: 0.75rem;
  align-items: baseline;
  padding: 0.3rem 0.5rem;
  border-radius: 8px;
  font-variant-numeric: tabular-nums;
  counter-increment: place;
}

.standings li::before {
  content: counter(place) ".";
  color: var(--muted);
}

.standings .who {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.standings .me {
  background: var(--bg);
  font-weight: 600;
}

.standings .lead .pts {
  color: var(--accent);
  font-weight: 700;
}

.lookup .label {
  color: var(--muted);
}

.lookup a {
  color: var(--accent);
  text-underline-offset: 2px;
}

.empty {
  color: var(--muted);
  margin: 0;
}

/* --- QR code ------------------------------------------------------------ */

.qr {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 0.9rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

/* Always dark on white, with a white margin, so cameras read it in dark mode. */
.qr .code {
  width: min(15rem, 100%);
  padding: 0.6rem;
  background: #ffffff;
  border-radius: 10px;
  line-height: 0;
}

.qr .code svg {
  width: 100%;
  height: auto;
}

.qr figcaption {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  color: var(--muted);
}
