:root {
  --gap: 2px;
  --cell: clamp(26px, calc((100vh - 170px) / 17), 38px);
  --coord: clamp(18px, calc(var(--cell) * 0.68), 26px);
  --page-pad: clamp(10px, 1.6vw, 22px);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Roboto", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #004722;
  color: #f0ebd2;
  display: flex;
  flex-direction: column;
}

header {
  padding: 12px var(--page-pad) 6px;
}

h1 {
  margin: 0 0 3px;
  font-size: clamp(20px, 2.2vw, 30px);
  line-height: 1.1;
  color: #ffb673;
  text-align: center;
}

h2 {
  margin: 0 0 8px;
  font-size: 16px;
}

p { margin: 0.5em; color: #f0ebd2; }

strong{color: #ce2029;}

.app-layout {
  display: grid;
  grid-template-columns: max-content max-content minmax(330px, 1fr);
  gap: clamp(12px, 1.7vw, 24px);
  padding: 10px var(--page-pad) 12px;
  align-items: start;
  flex: 1 1 auto;
}

.board-wrap {
  overflow: auto;
  padding: 8px;
  background: #004722;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgb(0 0 0 / 0.99);
}

.board {
  display: grid;
  grid-template-columns: var(--coord) repeat(15, var(--cell)) var(--coord);
  grid-template-rows: var(--coord) repeat(15, var(--cell)) var(--coord);
  gap: var(--gap);
}

.coord {
  width: var(--coord);
  height: var(--coord);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(10px, calc(var(--cell) * 0.32), 13px);
  font-weight: 400;
  color: #ffffffaa;
}

.cell, .rack-slot {
  width: var(--cell);
  height: var(--cell);
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.cell {
  background: #006e33;
  border: 1px solid #004722;
  font-size: clamp(6px, calc(var(--cell) * 0.23), 9px);
  text-align: center;
  line-height: 1.05;
  color: rgb(0 0 0 / 0.55);
}

.cell.center::after {
  content: " ";
  position: absolute;
  font-size: clamp(12px, calc(var(--cell) * 0.42), 16px);
  color: rgb(0 0 0 / 0.28);
}

.tw { background: #ce2029; color: #ffffffaa; }
.dw { background: #ffb673; color: #00000066; }
.tl { background: #00aeef; color: #ffffffaa; }
.dl { background: #b0e0e6; color: #00000066; }

.racks-panel,
.log-panel {
  min-width: 0;
}

.racks {
  display: grid;
  gap: 7px;
}

.rack {
  display: grid;
  grid-template-columns: 28px max-content 30px;
  align-items: center;
  gap: 7px;
  width: max-content;
  border: 1px solid #004722;
  border-radius: 8px;
  background: #004722;
  padding: 6px;
  box-shadow: 0 2px 5px rgb(0 0 0 / 0.89);
}

.rack-label {
  font-weight: 900;
  text-align: right;
  color: #ffffffaa;
}

.rack-slots {
  display: flex;
  gap: 5px;
  min-height: var(--cell);
}

.rack-slot {
  background: #eee;
  border: 1px dashed #ffffffaa;
}

.tile {
  width: calc(var(--cell) - 4px);
  height: calc(var(--cell) - 4px);
  border-radius: 3px;
  background: #f0ebd2;
  border: 1px solid #e0d6ae;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
  cursor: grab;
  font-weight: 700;
  font-size: clamp(15px, calc(var(--cell) * 0.53), 20px);
  color: #004722;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 2;
  flex: 0 0 calc(var(--cell) - 4px);
}

.cell:has(.tile) {
  font-size: 0;
  color: transparent;
}

.cell:has(.tile)::after {
  display: none;
}

.tile:active { cursor: grabbing; }

.tile .value {
  position: absolute;
  right: 1px;
  bottom: 1px;
  font-size: clamp(7px, calc(var(--cell) * 0.24), 9px);
  font-weight: 400;
}

.tile .turn {
  position: absolute;
  left: 3px;
  top: 1px;
  font-size: clamp(7px, calc(var(--cell) * 0.24), 9px);
  color: #00472200;
  font-weight: 400;
}

.actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  width: max-content;
}

button {
  border: 0;
  border-radius: 7px;
  background: #ce2029;
  color: white;
  padding: 9px 12px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}

button.secondary {
  background: #006e33;
}

.icon-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 999px;
  background: #ffffff66;
  color: #004722;
  border: 1px solid #004722;
  font-size: 16px;
  line-height: 1;
}

.icon-btn:hover {
  background: #ddd;
}

.tile.ghost {
  opacity: 0.35;
  filter: grayscale(1);
  cursor: default;
  box-shadow: none;
}

.log {
  min-height: 260px;
  height: min(620px, calc(100vh - 170px));
  max-height: calc(100vh - 170px);
  overflow: auto;
  white-space: pre;
  background: #004722;
  color: #f0ebd2;
  border-radius: 8px;
  padding: 12px;
  line-height: 1.45;
  tab-size: 4;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  box-shadow: 0 2px 10px rgb(0 0 0 / 0.99);
}

.instructions {
  padding: 0 var(--page-pad) 14px;
  font-size: 14px;
}

@media (max-width: 1180px) {
  .app-layout {
    grid-template-columns: max-content minmax(330px, 1fr);
  }

  .log-panel {
    grid-column: 1 / -1;
  }

  .log {
    height: 260px;
    max-height: 260px;
  }
}

@media (max-width: 760px) {
  :root {
    --cell: clamp(22px, calc((100vw - 54px) / 17), 34px);
  }

  .app-layout {
    grid-template-columns: 1fr;
  }

  .board-wrap,
  .racks,
  .actions,
  .rack {
    max-width: 100%;
  }

  .board-wrap {
    width: max-content;
    max-width: 100%;
  }

  .rack {
    grid-template-columns: 28px minmax(0, max-content) 30px;
  }

  .rack-slots {
    overflow-x: auto;
    max-width: calc(100vw - 100px);
  }
}

.tile.selected {
  outline: 3px solid #e1c532;
  outline-offset: 0px;
  transform: translateY(-1px);
}

.cell:has(.tile.selected),
.rack-slot:has(.tile.selected) {
  box-shadow: inset 0 0 0 2px rgb(0 0 0 / 0.28);
}

.tile .letter {
  display: inline-block;
  line-height: 1;
}

.joker-popup {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgb(0 0 0 / 0.45);
}

.joker-popup.hidden {
  display: none;
}

.joker-dialog {
  width: min(520px, 100%);
  max-height: min(680px, 90vh);
  overflow: auto;
  background: #f0ebd2;
  border: 3px solid #004722;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 12px 30px rgb(0 0 0 / 0.3);
}

.joker-dialog h3 {
  margin: 0 0 12px;
  color: #004722;
  font-size: 18px;
}

.joker-letters {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  gap: 7px;
}

.joker-letter-btn {
  min-height: 38px;
  padding: 6px;
  background: #ffffff;
  color: #004722;
  border: 1px solid #004722;
  border-radius: 7px;
  font-weight: 900;
}

.joker-letter-btn:hover,
.joker-letter-btn:focus-visible {
  background: #ce2029;
  color: #ffffff;
}
