:root {
  --board-sq: 44px;
  --rack-max: 64px;
  --gap: 2px;

  --page: #f4efe3;
  --ink: #1d1a16;
  --muted: #4a443b;
  --felt: #24543f;
  --felt-dark: #183a2b;
  --square: #ece3cc;
  --tile: #fbefcf;
  --tile-edge: #b8924a;
  --tile-ink: #2a1c0b;
  --blank-ink: #9a3412;

  --tw: #c4541a;
  --dw: #f2c28c;
  --tl: #1f6f78;
  --dl: #a7d8d2;

  --accent: #1f6f78;
  --play: #2f7d32;
  --play-dark: #215a24;
  --recent: #ffd54a;
  --error-bg: #fff0d1;
  --error-edge: #c47f00;
  --good-bg: #e2f2df;
  --good-edge: #2f7d32;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--page);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 18px;
  line-height: 1.35;
  overflow-x: clip;
  -webkit-tap-highlight-color: transparent;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px 20px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--felt);
  color: #fff;
}

.logo {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.7rem;
  letter-spacing: 0.02em;
}

.scores { display: flex; gap: 10px; }

.score {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 14px;
  border-radius: 10px;
  background: var(--felt-dark);
  border: 3px solid transparent;
}

.score .who { font-size: 1rem; }
.score .pts { font-size: 1.5rem; font-weight: 700; min-width: 2ch; text-align: right; }
.score.active { border-color: var(--recent); }
.score.thinking { animation: pulse 1.2s ease-in-out infinite; }

@keyframes pulse {
  50% { background: #2f6b51; }
}

.dots span { animation: blink 1.4s infinite both; }
.dots span:nth-child(2) { animation-delay: 0.2s; }
.dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.15; }
  40% { opacity: 1; }
}

.bag { font-size: 1rem; margin-left: auto; white-space: nowrap; }
.bag-short { display: none; }

/* ---------- Layout ---------- */

.layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 12px 8px 24px;
}

body.landscape .layout {
  flex-direction: row;
  align-items: flex-start;
  justify-content: center;
  gap: 20px;
  padding: 12px 16px;
}

.board-area { display: flex; flex-direction: column; align-items: center; gap: 8px; }

.side {
  width: 100%;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

body.landscape .side {
  width: 380px;
  flex: 0 0 380px;
}

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

.board {
  /* Never wider than the screen, even if the JS sizing is off. */
  --sq: min(var(--board-sq), calc((100vw - 16px - 16 * var(--gap)) / 15));
  display: grid;
  grid-template-columns: repeat(15, var(--sq));
  grid-template-rows: repeat(15, var(--sq));
  gap: var(--gap);
  padding: var(--gap);
  background: var(--felt);
  border-radius: 8px;
  user-select: none;
  -webkit-user-select: none;
}

.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--square);
  border-radius: 3px;
  overflow: hidden;
}

.sq.tw { background: var(--tw); color: #fff; }
.sq.dw { background: var(--dw); color: var(--ink); }
.sq.tl { background: var(--tl); color: #fff; }
.sq.dl { background: var(--dl); color: var(--ink); }

.sq.can-drop { box-shadow: inset 0 0 0 3px var(--accent); }
.sq.drop-hover { box-shadow: inset 0 0 0 4px var(--ink); }

.prem {
  font-size: calc(var(--sq) * 0.2);
  font-weight: 700;
  line-height: 1.05;
  text-align: center;
  pointer-events: none;
}

.star { font-size: calc(var(--sq) * 0.6); line-height: 1; pointer-events: none; }

/* ---------- Tiles ---------- */

.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: var(--tile);
  color: var(--tile-ink);
  border: 1px solid var(--tile-edge);
  border-radius: 4px;
  box-shadow: 0 2px 0 var(--tile-edge);
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  padding: 0;
}

.sq .tile { font-size: calc(var(--sq) * 0.62); }

.tile .val {
  position: absolute;
  right: 6%;
  bottom: 2%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 0.4em;
  font-weight: 700;
}

.tile.blank .letter { color: var(--blank-ink); text-decoration: underline; text-underline-offset: 0.08em; }
.tile.blank .val { display: none; }

.sq .tile.pending {
  background: #fff;
  border: 3px solid var(--accent);
  box-shadow: none;
  cursor: grab;
  touch-action: none;
}

.sq .tile.recent { background: var(--recent); border-color: #8a6d00; }

/* ---------- Rack ---------- */

.rack-area {
  background: var(--felt);
  border-radius: 12px;
  padding: 10px;
}

.rack {
  --rack-tile: min(var(--rack-max), calc((100vw - 16px - 20px - 36px) / 7));
  display: flex;
  justify-content: center;
  gap: 6px;
  min-height: var(--rack-tile);
}

.rack .tile {
  width: var(--rack-tile);
  height: var(--rack-tile);
  flex: 0 0 var(--rack-tile);
  font-size: calc(var(--rack-tile) * 0.6);
  cursor: pointer;
  touch-action: none;
  border-width: 2px;
  box-shadow: 0 3px 0 var(--tile-edge);
  transition: transform 0.1s;
}

.rack .tile.selected {
  transform: translateY(-6px);
  outline: 4px solid var(--recent);
  outline-offset: 1px;
}

.rack .tile.swap-chosen {
  background: #fff;
  outline: 4px solid var(--tw);
  outline-offset: 1px;
}

.rack .tile.blank .letter { color: var(--blank-ink); }

.tile.dragging { opacity: 0.35; }

.drag-ghost {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(1.1);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.35);
}

/* ---------- Status ---------- */

.status {
  min-height: 3.2em;
  padding: 12px 14px;
  border-radius: 10px;
  background: #fff;
  border: 2px solid #d8cfba;
  font-size: 1.15rem;
}

.status.error { background: var(--error-bg); border-color: var(--error-edge); }
.status.good { background: var(--good-bg); border-color: var(--good-edge); }
.status strong { letter-spacing: 0.03em; }

/* ---------- Buttons ---------- */

.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.controls[hidden] { display: none; }

/* Portrait: two compact rows so the board can be as big as possible. */
body:not(.landscape) .controls:not(.swap-controls) { grid-template-columns: repeat(3, 1fr); }

@media (min-width: 600px) {
  body:not(.landscape) .controls:not(.swap-controls) { grid-template-columns: repeat(5, 1fr); }
}

.btn {
  min-height: 56px;
  padding: 10px 14px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 1.15rem;
  font-weight: 600;
  cursor: pointer;
}

.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn:focus-visible { outline: 4px solid var(--recent); outline-offset: 2px; }

.btn-play {
  grid-column: 1 / -1;
  min-height: 64px;
  background: var(--play);
  border-color: var(--play-dark);
  color: #fff;
  font-size: 1.3rem;
}

.btn-small {
  min-height: 44px;
  font-size: 1rem;
  border-color: #fff;
  background: transparent;
  color: #fff;
}

/* ---------- Legend ---------- */

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 16px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 0.95rem;
  color: var(--muted);
}

.legend li { display: flex; align-items: center; gap: 6px; }
.swatch { width: 18px; height: 18px; border-radius: 3px; border: 1px solid rgba(0, 0, 0, 0.3); }
.swatch.tw { background: var(--tw); }
.swatch.dw { background: var(--dw); }
.swatch.tl { background: var(--tl); }
.swatch.dl { background: var(--dl); }

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

.history {
  background: #fff;
  border: 2px solid #d8cfba;
  border-radius: 10px;
  padding: 10px 14px;
}

.history h2 { margin: 0 0 6px; font-size: 1.1rem; }

.history ol {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 260px;
  overflow-y: auto;
}

body.landscape .history ol { max-height: none; }

.history li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 0;
  border-top: 1px solid #ece5d4;
}

.history li:first-child { border-top: 0; }
.history .h-who { font-weight: 700; min-width: 5.5em; }
.history .h-what { flex: 1; overflow-wrap: anywhere; }
.history .h-pts { font-weight: 700; }
.history .empty { color: var(--muted); }

.debug {
  padding: 4px 16px 12px;
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  opacity: 0.6;
}

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

.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(20, 18, 14, 0.55);
}

.overlay[hidden] { display: none; }

.dialog {
  width: 100%;
  max-width: 520px;
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 22px;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
}

.dialog h2 { margin: 0 0 10px; font-size: 1.5rem; }
.dialog p { margin: 0 0 12px; font-size: 1.15rem; }

.dialog-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
}

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

.level-buttons { display: grid; gap: 10px; }
.level-buttons .btn { text-align: left; display: flex; flex-direction: column; align-items: flex-start; }
.level-buttons .btn small { font-weight: 400; font-size: 0.95rem; color: var(--muted); }
.level-buttons .btn.current { border-width: 4px; border-color: var(--play); }

.letter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 8px;
}

.letter-grid .btn {
  min-height: 56px;
  padding: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.6rem;
  font-weight: 700;
}

@media (max-width: 560px) {
  body { font-size: 16px; }
  .topbar { padding: 8px; gap: 6px; }
  .score .who { font-size: 0.85rem; }
  .scores { gap: 6px; }
  .logo { display: none; }
  .bag-long { display: none; }
  .bag-short { display: inline; }
  .topbar .btn-small { min-height: 40px; padding: 4px 8px; font-size: 0.95rem; white-space: nowrap; }
  .score { padding: 3px 8px; gap: 5px; }
  .score .pts { font-size: 1.15rem; }
  .bag { font-size: 0.9rem; }
  .btn { min-height: 52px; font-size: 1.05rem; }
}

@media (prefers-reduced-motion: reduce) {
  .rack .tile { transition: none; }
  .score.thinking, .dots span { animation: none; }
}
