:root {
  color-scheme: light dark;
  --bg: #f5f2ea;
  --surface: #fffdf8;
  --surface-2: #efe9dc;
  --ink: #1f1d1a;
  --muted: #6b665c;
  --line: #e2dccd;
  --accent: #3b5bdb;
  --accent-ink: #ffffff;
  --warning-bg: #fff1d6;
  --warning-ink: #7a4b00;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(40 30 10 / 0.06), 0 4px 16px rgb(40 30 10 / 0.06);
  --font: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141518;
    --surface: #1d1f24;
    --surface-2: #262931;
    --ink: #ecebe6;
    --muted: #9a9a93;
    --line: #30333b;
    --accent: #8ea2ff;
    --accent-ink: #10121a;
    --warning-bg: #3a2c12;
    --warning-ink: #ffd48a;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 4px 16px rgb(0 0 0 / 0.25);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

#app {
  /* Board column (640) + gap (16) + panel (300) + side padding (2 × 16). */
  max-width: 988px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

h1,
h2,
h3 {
  line-height: 1.15;
  margin: 0;
}

a {
  color: var(--accent);
}

.muted {
  color: var(--muted);
  font-weight: 400;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Brand ---------- */

.brand {
  font-weight: 800;
  font-size: 1.25rem;
  letter-spacing: -0.02em;
  color: var(--ink);
  text-decoration: none;
}

.brand-accent {
  color: var(--accent);
}

.brand-large {
  font-size: clamp(2.4rem, 9vw, 3.6rem);
}

/* ---------- Cards, forms, buttons ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

label {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 6px;
}

input[type="text"] {
  width: 100%;
  font: inherit;
  font-size: 1rem;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
}

.select {
  width: 100%;
  font: inherit;
  font-size: 1rem;
  color: var(--ink);
  background-color: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 36px 10px 12px;
  appearance: none;
  /* Chevron drawn with two gradients so it follows the text color in both themes. */
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 55%, calc(100% - 13px) 55%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
}

.field-gap {
  margin-top: 10px;
}

.rematch-theme {
  text-align: left;
}

input[type="text"]:focus-visible,
.select:focus-visible,
.btn:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 2px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 700;
  font-size: 1rem;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.btn:hover {
  filter: brightness(0.97);
}

.btn:disabled {
  opacity: 0.6;
  cursor: progress;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.btn-small {
  padding: 6px 12px;
  font-size: 0.9rem;
}

.btn .icon {
  flex: none;
}

.btn-danger {
  background: #d1343a;
  border-color: #d1343a;
  color: #ffffff;
}

.btn-exit {
  margin-left: 4px;
}

/* Square icon-only button (tooltip + aria-label carry the meaning). */
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}

.icon-btn:hover {
  color: var(--accent);
}

.icon-btn:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 2px;
}

.form-error {
  color: #d1343a;
  font-size: 0.9rem;
  margin: 8px 0 0;
  min-height: 1.2em;
}

@media (prefers-color-scheme: dark) {
  .form-error {
    color: #ff8a8f;
  }
}

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

.home {
  display: grid;
  gap: 24px;
  max-width: 440px;
  margin: 6vh auto 0;
}

.home-hero {
  text-align: center;
}

.tagline {
  font-size: 1.15rem;
  font-weight: 700;
  margin: 6px 0 0;
}

.intro {
  color: var(--muted);
  margin: 8px 0 0;
}

.home-card {
  display: grid;
  gap: 4px;
}

.home-card .btn-primary {
  margin-top: 12px;
}

.notice {
  background: var(--warning-bg);
  color: var(--warning-ink);
  border-radius: 10px;
  padding: 8px 12px;
  margin: 0 0 12px;
}

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  font-size: 0.85rem;
  margin: 16px 0 12px;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line);
}

.join-row {
  display: flex;
  gap: 8px;
}

/* Turnstile only renders here when Cloudflare needs the user to interact. */
.turnstile-slot {
  display: flex;
  justify-content: center;
}

.turnstile-slot:empty {
  display: none;
}

.join-form .form-error {
  margin-top: 6px;
}

.code-input {
  text-transform: uppercase;
  letter-spacing: 0.2em;
  font-weight: 700;
}

.center-screen {
  display: grid;
  gap: 24px;
  justify-items: center;
  margin-top: 8vh;
  text-align: center;
}

.name-prompt {
  display: grid;
  gap: 8px;
  width: min(100%, 400px);
  text-align: left;
}

.name-prompt p {
  margin: 0 0 8px;
}

.center-card {
  display: grid;
  gap: 16px;
  justify-items: center;
  text-align: center;
  max-width: 420px;
  margin: 8vh auto 0;
}

.center-card p {
  margin: 0;
  font-size: 1.1rem;
}

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

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  margin-bottom: 12px;
}

.room-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.room-code {
  font-size: 1.1rem;
  letter-spacing: 0.12em;
}

/* Floating message: pinned to the top center, slides down from the edge. */
.toast {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  z-index: 50;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 32px);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 12px;
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.16), 0 2px 6px rgb(0 0 0 / 0.08);
  font-weight: 600;
  text-align: center;
}

.toast[hidden] {
  display: none;
}

.toast.toast-warning {
  background: var(--warning-bg);
  color: var(--warning-ink);
  border-color: color-mix(in srgb, var(--warning-ink) 25%, transparent);
}

.toast-in {
  animation: toast-in 0.32s cubic-bezier(0.2, 0.9, 0.3, 1.15);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, calc(-100% - 16px));
  }
}

.game {
  display: grid;
  gap: 16px;
  grid-template-areas:
    "theme"
    "board"
    "panel";
}

.board-area {
  grid-area: board;
  display: grid;
  justify-items: center;
  min-width: 0;
}

.theme {
  grid-area: theme;
  font-size: 1.1rem;
  justify-self: start;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  min-height: 36px;
}

/* Theme picker styled as an accent button; picking an option applies it immediately. */
.select-inline {
  width: auto;
  font-size: 0.95rem;
  font-weight: 800;
  color: var(--accent-ink);
  background-color: var(--accent);
  border-color: var(--accent);
  border-radius: 999px;
  padding: 6px 34px 6px 14px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.12);
}

.select-inline:hover {
  filter: brightness(1.06);
}

.select-inline:disabled {
  opacity: 0.7;
  cursor: progress;
}

.select-inline option {
  color: var(--ink);
  background: var(--surface);
  font-weight: 600;
}

.panel {
  grid-area: panel;
  display: grid;
  gap: 16px;
  align-content: start;
}

.panel-title {
  font-size: 1rem;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
}

@media (min-width: 900px) {
  /* The theme line sits above the board only, so the panel starts level with the board. */
  .game {
    grid-template-columns: minmax(0, 640px) 300px;
    grid-template-areas:
      "theme ."
      "board panel";
    justify-content: center;
    align-items: start;
    row-gap: 10px;
  }

  .board-area {
    justify-items: start;
  }

  .board {
    /* Leave room for the top bar and the round/theme heading (messages float above). */
    width: min(100%, calc(100dvh - 150px));
  }
}

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

.board {
  --size: 12;
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  container-type: inline-size;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.board-lines {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* Floating readout of the letters being dragged (the finger hides them on phones). */
.selection-label {
  --sel: var(--accent);
  position: absolute;
  top: 8px;
  left: 50%;
  z-index: 2;
  transform: translateX(-50%);
  max-width: calc(100% - 16px);
  padding: 4px 14px;
  border: 2px solid var(--sel);
  border-radius: 999px;
  background: color-mix(in srgb, var(--sel) 14%, var(--surface));
  box-shadow: var(--shadow);
  font-weight: 800;
  font-size: clamp(1rem, 4.5cqi, 1.5rem);
  letter-spacing: 0.12em;
  white-space: nowrap;
  overflow: hidden;
  pointer-events: none;
}

.selection-label[hidden] {
  display: none;
}

.selection-label-bottom {
  top: auto;
  bottom: 8px;
}

.board-grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  grid-template-rows: repeat(var(--size), 1fr);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}

.board-row {
  display: contents;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: calc(100cqi / var(--size) * 0.54);
  line-height: 1;
}

.line {
  fill: none;
  stroke-width: 0.78;
  stroke-linecap: round;
}

.line-found {
  stroke-opacity: 0.38;
}

.line-selection {
  stroke-opacity: 0.5;
  visibility: hidden;
}

.line-preview {
  stroke-opacity: 0.22;
  stroke-width: 0.62;
  transition: stroke-opacity 0.15s;
}

@media (prefers-color-scheme: dark) {
  .line-found {
    stroke-opacity: 0.5;
  }
  .line-selection {
    stroke-opacity: 0.6;
  }
  .line-preview {
    stroke-opacity: 0.3;
  }
}

.board-disabled .board-grid {
  cursor: not-allowed;
}

.board-disabled .cell {
  opacity: 0.45;
}

/* ---------- Feedback animations ---------- */

/* Own find: the line swells briefly, then settles. */
.line-pop {
  animation: line-pop 0.6s ease-out;
}

@keyframes line-pop {
  0% {
    stroke-width: 0.78;
    stroke-opacity: 0.75;
  }
  35% {
    stroke-width: 1;
    stroke-opacity: 0.75;
  }
  100% {
    stroke-width: 0.78;
  }
}

/* Someone else's find: a quiet fade in. */
.line-appear {
  animation: line-appear 0.45s ease-out;
}

@keyframes line-appear {
  from {
    stroke-opacity: 0;
  }
}

/* Rejected selection: short shake, then fade out. Units are cells (SVG user space). */
.line-miss {
  animation: line-miss 0.45s ease-in-out forwards;
}

@keyframes line-miss {
  0%,
  100% {
    transform: translateX(0);
  }
  15%,
  55% {
    transform: translateX(-0.18px);
  }
  35%,
  75% {
    transform: translateX(0.18px);
  }
  100% {
    stroke-opacity: 0;
  }
}

.word-pop {
  animation: bump 0.6s ease-out;
}

.player-scored .player-score {
  display: inline-block;
  animation: bump 0.6s ease-out;
}

@keyframes bump {
  30% {
    transform: scale(1.18);
  }
}

/* New board: letters flip in along a diagonal wave (~0.7s total). */
.cell-deal {
  animation: cell-deal 0.32s cubic-bezier(0.2, 0.8, 0.3, 1.2) backwards;
  animation-delay: calc(var(--wave, 0) * 18ms);
}

@keyframes cell-deal {
  from {
    opacity: 0;
    transform: scale(0.4) rotate(-12deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .cell-deal,
  .line-pop,
  .line-appear,
  .word-pop,
  .player-scored .player-score {
    animation: none;
  }
  .line-miss {
    animation: line-miss-reduced 0.45s forwards;
  }
  @keyframes line-miss-reduced {
    to {
      stroke-opacity: 0;
    }
  }
}

/* ---------- Words ---------- */

.word-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.word {
  font-weight: 700;
  font-size: 0.9rem;
  letter-spacing: 0.04em;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
}

.word-found {
  color: var(--muted);
  text-decoration: line-through;
  text-decoration-color: var(--by, var(--muted));
  text-decoration-thickness: 3px;
  box-shadow: inset 0 0 0 2px var(--by, transparent);
  background: transparent;
}

/* ---------- Players ---------- */

.player-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.player {
  --player: var(--muted);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
}

.player-offline {
  opacity: 0.55;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 800;
  color: var(--ink);
  background: color-mix(in srgb, var(--player) 18%, var(--surface));
  box-shadow: inset 0 0 0 3px var(--player);
}

.player-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.tag {
  font-size: 0.75rem;
  font-weight: 700;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}

.player-score {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  font-size: 1.1rem;
}

.board-locked .board-grid {
  cursor: default;
}

/* ---------- Status banner action ---------- */


/* ---------- Dialogs ---------- */

.dialog {
  width: min(calc(100% - 32px), 420px);
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
}

.dialog::backdrop {
  background: rgb(20 18 14 / 0.45);
  backdrop-filter: blur(2px);
}

.dialog[open] {
  animation: dialog-in 0.2s ease-out;
}

@keyframes dialog-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
}

.dialog-body {
  display: grid;
  gap: 12px;
  padding: 20px;
}

.dialog-title {
  font-size: 1.35rem;
}

.dialog-body p {
  margin: 0;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}

.dialog-actions .btn {
  flex: 1 1 140px;
}

/* ---------- Results ---------- */

.results .dialog-body {
  text-align: center;
}

.results-headline {
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--accent);
}

.ranking {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
  text-align: left;
}

.ranking-row {
  --player: var(--muted);
  display: grid;
  grid-template-columns: 2.2em auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--surface-2);
}

.ranking-first {
  background: color-mix(in srgb, var(--player) 16%, var(--surface));
  box-shadow: inset 0 0 0 2px var(--player);
}

.ranking-pos {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.ranking-first .ranking-pos {
  color: var(--ink);
}

.ranking .player-score {
  font-size: 1rem;
}

/* ---------- Invite / QR ---------- */

.invite-card {
  display: grid;
  justify-items: center;
  gap: 10px;
  text-align: center;
}

.invite-head {
  justify-self: stretch;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.invite-head .panel-title {
  margin-bottom: 0;
}

.icon-actions {
  display: flex;
  gap: 6px;
}

.invite .dialog-body {
  justify-items: center;
  text-align: center;
}

.invite .dialog-actions {
  width: 100%;
}

.qr-box-large {
  width: min(100%, 280px);
}

.qr-box {
  width: min(100%, 200px);
  padding: 8px;
  background: #ffffff;
  border-radius: 12px;
}

.qr {
  display: block;
  width: 100%;
  height: auto;
}

.invite-url {
  margin: 0;
  font-weight: 700;
  font-size: 0.9rem;
  word-break: break-all;
}

/* ---------- Touch devices ---------- */

/* No double-tap zoom or tap delay on controls (the board already uses touch-action: none). */
button,
a,
select,
input,
label {
  touch-action: manipulation;
}

/* Finger-sized targets (~44px) where the primary input is touch. */
@media (pointer: coarse) {
  .btn,
  .select-inline {
    min-height: 44px;
  }

  .icon-btn {
    width: 44px;
    height: 44px;
  }
}

/* ---------- Small screens ---------- */

.room-info {
  flex-wrap: wrap;
  justify-content: flex-end;
}

@media (max-width: 420px) {
  #app {
    padding: 12px;
  }

  .room-label {
    display: none;
  }

  /* Brand, code and both buttons fit on one row from ~340px up. */
  .topbar {
    flex-wrap: nowrap;
    gap: 8px;
  }

  .topbar .brand {
    font-size: 1.1rem;
  }

  .room-info {
    flex-wrap: nowrap;
    gap: 6px;
  }

  .room-code {
    font-size: 1rem;
    letter-spacing: 0.08em;
  }

  .btn-small {
    padding: 6px 10px;
  }

  .card {
    padding: 14px;
  }
}

@media (max-width: 340px) {
  .topbar,
  .room-info {
    flex-wrap: wrap;
  }
}

@media (prefers-reduced-motion: reduce) {
  .toast-in,
  .dialog[open] {
    animation: none;
  }
}
