/* Colors are NYT Connections' own tokens (--connections-* and friends on nytimes.com). */
:root {
  --page: #fff;
  --text: #121212;
  --text-muted: #8b8b8b;
  --tile: #efefe6;
  --tile-text: #121212;
  --tile-selected: #5a594e;
  --tile-selected-text: #f8f8f8;
  --border: #121212;
  --border-disabled: #979797;
  --pressed: #1212121f;
  --emphasis: #121212;
  --emphasis-text: #fff;
  --toggle-track: #ebebeb;
  --toggle-thumb: #fff;
  --toast: #121212;
  --toast-text: #f8f8f8;
  --font: "Libre Franklin", Arial, sans-serif;
  --gap: 8px;
  /* Four tiles, three gaps, page and board padding, and 2px of rounding slack across a phone. */
  --tile-width: min(150px, calc((100vw - 58px) / 4));
  --tile-height: 80px;
  --tile-font: 20px;
  --letter-size: clamp(46px, 12vw, 62px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #121212;
    --text: #f8f8f8;
    --text-muted: #8b8b8b;
    --tile: #cfcfbf;
    --tile-selected: #656458;
    --border: #f8f8f8;
    --border-disabled: #727272;
    --pressed: #f8f8f81f;
    --emphasis: #f8f8f8;
    --emphasis-text: #121212;
    --toggle-track: #363636;
    --toggle-thumb: #5a5a5a;
    --toast: #f8f8f8;
    --toast-text: #121212;
  }
}

@media (max-width: 720px) {
  :root {
    --tile-height: var(--tile-width);
    --tile-font: 16px;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--page);
  color: var(--text);
  font-family: var(--font);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.table {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
}

.board {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
}

.board-hint {
  position: absolute;
  inset: 0;
  margin: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  font-size: 18px;
  font-weight: 500;
  color: var(--text-muted);
  pointer-events: none;
  transition: opacity 0.3s;
}

.board.has-tiles .board-hint {
  opacity: 0;
}

/* Tiles */

.tile {
  --tilt: 0deg;
  position: absolute;
  top: 0;
  left: 0;
  touch-action: none;
  cursor: grab;
  outline: none;
  will-change: transform;
}

.board.glide .tile {
  transition: transform 0.52s cubic-bezier(0.3, 1.25, 0.45, 1);
}

.tile-body {
  display: grid;
  place-items: center;
  width: var(--letter-size);
  height: var(--letter-size);
  overflow: hidden;
  border-radius: 6px;
  color: var(--tile-text);
  background: var(--tile);
  font-size: calc(var(--letter-size) * 0.5);
  font-weight: 700;
  line-height: 1;
  text-transform: uppercase;
  transition: transform 0.18s ease-out, box-shadow 0.18s ease-out, background-color 0.15s, color 0.15s, opacity 0.2s;
}

.tile.phrase .tile-body {
  width: var(--tile-width);
  height: var(--tile-height);
  padding: 0 3px;
  font-size: var(--tile-font);
  line-height: 1.25;
  text-align: center;
  word-break: keep-all;
}

.tile.selected .tile-body {
  color: var(--tile-selected-text);
  background: var(--tile-selected);
}

.tile:focus-visible .tile-body {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

.tile.dragging {
  cursor: grabbing;
  transition: none !important;
}

.tile.dragging .tile-body {
  transform: rotate(var(--tilt)) scale(1.06);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.2);
}

.tile.entering .tile-body {
  animation: deal 0.3s cubic-bezier(0.2, 1.2, 0.4, 1) backwards;
}

@keyframes deal {
  from {
    opacity: 0;
    transform: scale(0.85);
  }
}

.tile.leaving {
  pointer-events: none;
}

.tile.leaving .tile-body {
  opacity: 0;
  transform: scale(0.85);
}

.tile-remove {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--page);
  background: var(--text);
  opacity: 0;
  transform: scale(0.4);
  pointer-events: none;
  cursor: pointer;
  transition: opacity 0.14s, transform 0.18s cubic-bezier(0.3, 1.6, 0.5, 1);
}

.tile-remove::after {
  content: "";
  position: absolute;
  inset: -9px;
}

.tile-remove svg {
  width: 9px;
  height: 9px;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  fill: none;
}

.tile.selected:not(.dragging) .tile-remove {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

@media (hover: hover) {
  .tile:hover:not(.dragging) .tile-remove {
    opacity: 1;
    transform: scale(1);
    pointer-events: auto;
    transition-delay: 0.25s;
  }
}

/* Dock */

.dock {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 12px 16px;
}

.composer {
  flex: 1 1 340px;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.field {
  flex: 1;
  min-width: 0;
  height: 48px;
  padding: 0 20px;
  border: 1px solid var(--border-disabled);
  border-radius: 32px;
  transition: border-color 0.15s;
}

.field:focus-within {
  border-color: var(--border);
}

.field input {
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font: 600 16px var(--font);
  text-transform: uppercase;
  user-select: text;
  -webkit-user-select: text;
}

.field input::placeholder {
  color: var(--text-muted);
  font-weight: 500;
  text-transform: none;
}

.key {
  appearance: none;
  height: 48px;
  min-width: 88px;
  padding: 0 20px;
  border: 1px solid var(--border);
  border-radius: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  background: transparent;
  font: 600 16px var(--font);
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.1s, color 0.2s, border-color 0.2s;
}

.key[hidden] {
  display: none;
}

.key:active:not(:disabled) {
  background: var(--pressed);
}

.key:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

.key:disabled {
  color: var(--text-muted);
  border-color: var(--border-disabled);
  cursor: default;
}

.key-primary {
  color: var(--emphasis-text);
  background: var(--emphasis);
  border-color: var(--emphasis);
}

.key-primary:active:not(:disabled) {
  background: var(--emphasis);
  opacity: 0.8;
}

.mode-switch {
  position: relative;
  flex: none;
  height: 48px;
  padding: 4px;
  border-radius: 32px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--toggle-track);
}

.mode-switch button {
  position: relative;
  z-index: 1;
  padding: 0 14px;
  border: 0;
  background: none;
  color: var(--text-muted);
  font: 600 14px var(--font);
  cursor: pointer;
  touch-action: manipulation;
  transition: color 0.2s;
}

.mode-switch button[aria-checked="true"] {
  color: var(--text);
}

.mode-switch button:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: -2px;
  border-radius: 32px;
}

.mode-thumb {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc(50% - 4px);
  border-radius: 32px;
  background: var(--toggle-thumb);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  transition: transform 0.24s cubic-bezier(0.3, 1.4, 0.5, 1);
}

.mode-switch[data-mode="phrases"] .mode-thumb {
  transform: translateX(100%);
}

/* Toast */

.toast {
  position: absolute;
  left: 50%;
  top: 16px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 6px 6px 6px 16px;
  border-radius: 4px;
  color: var(--toast-text);
  background: var(--toast);
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  opacity: 0;
  transform: translate(-50%, -12px);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.28s cubic-bezier(0.3, 1.3, 0.5, 1);
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
  pointer-events: auto;
}

.toast .key {
  height: 32px;
  min-width: 0;
  padding: 0 14px;
  color: var(--toast-text);
  border-color: var(--toast-text);
  font-size: 14px;
}

.toast:not(.has-action) {
  padding: 12px 16px;
}

.toast:not(.has-action) .key {
  display: none;
}

.table.dropping .board {
  outline: 2px dashed var(--text-muted);
  outline-offset: -2px;
  border-radius: 6px;
}

@media (max-width: 720px) {
  .table {
    gap: 10px;
  }

  .actions {
    flex: 1 1 100%;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }

  .actions .key {
    min-width: 0;
    padding: 0 8px;
  }

  .composer .key {
    min-width: 0;
  }

  .field {
    padding: 0 14px;
  }

  .mode-switch button {
    padding: 0 10px;
    font-size: 13px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .board.glide .tile,
  .tile-body,
  .mode-thumb,
  .toast {
    transition-duration: 0.01s;
  }

  .tile.entering .tile-body {
    animation: none;
  }
}
