:root {
  --bg-dark: #12312f;
  --panel: rgba(14, 34, 32, 0.78);
  --panel-border: rgba(255, 255, 255, 0.08);
  --text-main: #f6f1df;
  --text-soft: #c8d8cf;
  --accent: #43a047;
  --accent-hover: #2e7d32;
  --accent-alt: #e0a72f;
  --accent-alt-hover: #c68c18;
  --board-bg: #2e5d58;
  --cell-base: #d7ccc8;
  --cell-border: #6d4c41;
  --tile-bg: #f4d03f;
  --tile-shadow: rgba(0, 0, 0, 0.25);
  --cell-size: min(40px, calc((100vh - 250px) / 15));
  --rack-tile-size: min(46px, calc((100vh - 690px) * 1.45));
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Trebuchet MS", Arial, sans-serif;
  color: var(--text-main);
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.1), transparent 32%),
    linear-gradient(180deg, #204845 0%, var(--bg-dark) 100%);
  overflow: hidden;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%) translateY(18px);
  min-width: 280px;
  max-width: min(560px, calc(100vw - 32px));
  padding: 12px 14px 14px;
  border-radius: 14px;
  background:
    linear-gradient(180deg, rgba(188, 51, 51, 0.98) 0%, rgba(135, 30, 30, 0.98) 100%);
  color: #fff7f4;
  border: 1px solid rgba(255, 217, 217, 0.22);
  box-shadow:
    0 16px 28px rgba(0, 0, 0, 0.28),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
  font-size: 0.95rem;
  line-height: 1.35;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
  z-index: 1000;
  display: grid;
  gap: 10px;
  justify-items: center;
}

.toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}

.toast-message {
  text-align: center;
}

.toast-ok {
  min-width: 88px;
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid rgba(255, 240, 240, 0.25);
  background: rgba(255, 255, 255, 0.14);
  color: #fffaf7;
  font-size: 0.9rem;
  font-weight: bold;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.16);
}

.toast-ok:hover {
  background: rgba(255, 255, 255, 0.22);
}

.app-shell {
  max-width: 1400px;
  margin: 0 auto;
  min-height: 100vh;
  padding: 10px 16px 14px;
  zoom: 0.9;
}

.hero {
  text-align: center;
  margin-bottom: 8px;
  width: calc(100% - 354px);
  margin-left: 0;
  margin-right: auto;
}

h1 {
  margin: 0;
  font-size: clamp(1.8rem, 4vw, 3rem);
  letter-spacing: 0.12em;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 14px;
  align-items: start;
}

.play-area,
.sidebar,
.panel {
  min-width: 0;
}

.board-stack {
  display: grid;
  gap: 8px;
  justify-items: center;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  backdrop-filter: blur(6px);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.18);
}

.panel h2,
.rack-caption h2 {
  margin: 0 0 10px;
  font-size: 0.98rem;
  letter-spacing: 0.04em;
}

#board-container {
  display: inline-block;
  width: fit-content;
  max-width: 96%;
  overflow-x: auto;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), transparent 18%),
    linear-gradient(180deg, #376b66 0%, var(--board-bg) 100%);
  padding: 12px;
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    0 20px 40px rgba(0, 0, 0, 0.16);
}

.board-row {
  display: flex;
}

#top-coords,
#left-coords {
  display: grid;
  gap: 2px;
}

#top-coords {
  grid-template-columns: repeat(15, var(--cell-size));
  margin-left: var(--cell-size);
  margin-bottom: 4px;
}

#left-coords {
  grid-template-rows: repeat(15, var(--cell-size));
  margin-right: 4px;
}

.coord {
  width: var(--cell-size);
  height: var(--cell-size);
  font-size: clamp(10px, calc(var(--cell-size) * 0.28), 12px);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #d6e2dd;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
}

#board {
  display: grid;
  grid-template-columns: repeat(15, var(--cell-size));
  gap: 2px;
  padding: 4px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.14);
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--cell-base);
  border: 1px solid var(--cell-border);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2px;
  font-size: clamp(7px, calc(var(--cell-size) * 0.22), 9px);
  font-weight: bold;
  color: #3e2723;
  position: relative;
  transition: transform 0.15s ease, box-shadow 0.15s ease, filter 0.15s ease;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
  overflow: visible;
}

.cell.drop-target {
  transform: translateY(-1px);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.55);
  filter: brightness(1.06);
}

.TW {
  background: linear-gradient(180deg, #d94848 0%, #c62828 100%);
  color: white;
}

.DW {
  background: linear-gradient(180deg, #f8c7c7 0%, #ef9a9a 100%);
}

.TL {
  background: linear-gradient(180deg, #2a7ad8 0%, #1565c0 100%);
  color: white;
}

.DL {
  background: linear-gradient(180deg, #b5dcfb 0%, #90caf9 100%);
}

.center {
  background: linear-gradient(180deg, #f6c7cc 0%, #ef9a9a 100%);
  font-size: clamp(12px, calc(var(--cell-size) * 0.45), 18px);
}

.cell.has-tile {
  background: linear-gradient(180deg, #fae78f 0%, var(--tile-bg) 100%) !important;
  color: #111;
  font-size: clamp(14px, calc(var(--cell-size) * 0.46), 18px);
  box-shadow:
    inset 0 2px 0 rgba(255, 249, 212, 0.85),
    inset 0 -3px 0 rgba(140, 92, 16, 0.28),
    0 2px 0 rgba(102, 60, 19, 0.42),
    0 8px 12px rgba(0, 0, 0, 0.16);
  border-color: rgba(129, 84, 19, 0.45);
}

.cell.locked {
  box-shadow:
    inset 0 2px 0 rgba(255, 249, 212, 0.75),
    inset 0 -3px 0 rgba(140, 92, 16, 0.28),
    0 1px 0 rgba(102, 60, 19, 0.4),
    0 5px 9px rgba(0, 0, 0, 0.14);
}

.cell .letter {
  line-height: 1;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.28);
}

.cell.has-tile::before {
  content: "";
  position: absolute;
  left: 3px;
  right: 3px;
  top: 3px;
  height: 34%;
  border-radius: 5px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.45), rgba(255, 255, 255, 0));
  pointer-events: none;
}

.cell.preview-word {
  z-index: 2;
}

.cell.preview-valid {
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.25),
    0 0 0 2px rgba(67, 160, 71, 0.95);
  border-color: rgba(67, 160, 71, 0.98);
}

.cell.preview-invalid {
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.25),
    0 0 0 2px rgba(198, 40, 40, 0.95);
  border-color: rgba(198, 40, 40, 0.98);
}

.preview-score-badge {
  position: absolute;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 18px;
  padding: 0 6px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: bold;
  line-height: 1;
  color: #fffdf6;
  border: 1px solid rgba(255, 255, 255, 0.16);
  box-shadow: 0 6px 10px rgba(0, 0, 0, 0.18);
  background: rgba(18, 49, 47, 0.96);
  z-index: 5;
  pointer-events: none;
  transform: translateZ(0);
}

.preview-score-badge.preview-valid {
  background: rgba(46, 125, 50, 0.96);
}

.preview-score-badge.preview-invalid {
  background: rgba(198, 40, 40, 0.96);
}

.preview-score-badge.horizontal {
  top: calc(-24px - (var(--badge-index, 0) * 20px));
  left: 50%;
  transform: translateX(-50%);
}

.preview-score-badge.vertical {
  left: calc(-44px - (var(--badge-index, 0) * 38px));
  top: 50%;
  transform: translateY(-50%);
}

.cell .tile-value {
  position: absolute;
  right: 4px;
  bottom: 3px;
  font-size: clamp(7px, calc(var(--cell-size) * 0.2), 9px);
}

.rack-zone {
  width: min(100%, calc((var(--cell-size) * 15) + 18px));
  padding: 0 18px;
}

.rack-caption {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}

.rack-caption p {
  margin: 0;
  color: var(--text-soft);
  font-size: 0.82rem;
}

.rack-shelf {
  position: relative;
  width: 100%;
  padding: 10px 10px 12px;
  border-radius: 20px 20px 18px 18px;
  background:
    linear-gradient(180deg, #92663b 0%, #7b4f29 42%, #5f3818 100%);
  border: 1px solid rgba(46, 22, 8, 0.55);
  box-shadow:
    inset 0 2px 0 rgba(255, 245, 222, 0.22),
    inset 0 -6px 10px rgba(34, 16, 8, 0.35),
    0 12px 24px rgba(0, 0, 0, 0.22);
  min-width: 0;
}

.rack-lip {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 8px;
  height: 10px;
  border-radius: 0 0 10px 10px;
  background: linear-gradient(180deg, #6b3f1f 0%, #4f2a10 100%);
  box-shadow: inset 0 1px 0 rgba(255, 236, 208, 0.15);
}

#rack.rack-drop-target {
  outline: 2px dashed rgba(255, 244, 200, 0.7);
  outline-offset: 4px;
  border-radius: 12px;
}

.sidebar {
  display: grid;
  gap: 12px;
}

.info-panel,
.setup-panel,
.actions-panel,
.history-panel {
  padding: 14px;
}

.score-row,
.stats,
.turn-grid {
  display: grid;
  gap: 8px;
}

.score-row {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.stats,
.turn-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-top: 10px;
}

.stat-card,
.turn-card {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 12px;
  padding: 9px 8px;
}

.score-card {
  background: linear-gradient(180deg, rgba(224, 167, 47, 0.18) 0%, rgba(190, 126, 30, 0.16) 100%);
  border-color: rgba(239, 210, 137, 0.22);
  box-shadow: inset 0 1px 0 rgba(255, 240, 192, 0.14);
}

.stat-label {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: #b5c8c1;
  margin-bottom: 4px;
}

.stat-card strong,
.turn-card strong {
  font-size: 1.2rem;
}

.turn-summary,
.status,
.history-box,
.hint {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  line-height: 1.3;
  font-size: 0.92rem;
}

.turn-summary {
  background: rgba(255, 255, 255, 0.05);
  color: #eef6f2;
}

.status {
  background: rgba(244, 208, 63, 0.12);
  color: #f8e9a9;
}

.history-box {
  background: rgba(255, 255, 255, 0.05);
  color: #edf5f2;
  min-height: 74px;
}

.hint {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-soft);
  font-size: 0.92rem;
}

.actions {
  display: grid;
  gap: 8px;
}

.board-actions-panel {
  width: 100%;
  padding: 10px 12px;
}

.board-actions-panel h2 {
  margin-bottom: 8px;
}

.icon-actions {
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

.icon-button {
  position: relative;
  min-width: 0;
  width: 100%;
  height: 35px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.icon-button::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 50%;
  top: calc(100% + 8px);
  transform: translateX(-50%) translateY(-4px);
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(16, 27, 25, 0.96);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 10px 18px rgba(0, 0, 0, 0.22);
  font-size: 0.8rem;
  line-height: 1;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
  z-index: 20;
}

.icon-button:hover::after,
.icon-button:focus-visible::after {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.icon-glyph {
  line-height: 1;
}

.field {
  display: grid;
  gap: 6px;
}

.select-input {
  width: 100%;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
  font: inherit;
  outline: none;
}

.select-input:focus {
  border-color: rgba(224, 167, 47, 0.7);
  box-shadow: 0 0 0 2px rgba(224, 167, 47, 0.15);
}

.select-input option {
  color: #172724;
}

button {
  border: none;
  padding: 10px 14px;
  background: var(--accent);
  color: white;
  font-size: 13px;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, opacity 0.15s ease;
}

button:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

button.secondary {
  background: rgba(255, 255, 255, 0.09);
  color: var(--text-main);
}

button.secondary:hover {
  background: rgba(255, 255, 255, 0.14);
}

button.accent-alt {
  background: var(--accent-alt);
  color: #1f1a10;
  font-weight: bold;
}

button.accent-alt:hover {
  background: var(--accent-alt-hover);
}

button.is-active {
  background: #d17b1d;
  color: white;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  transform: none;
}

#rack {
  position: relative;
  z-index: 1;
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: flex-end;
  gap: 6px;
  min-height: 42px;
  overflow: hidden;
  max-width: calc((clamp(34px, var(--rack-tile-size), 46px) * 7) + 36px);
  margin: 0 auto;
}

#rack.is-shuffling .tile {
  pointer-events: none;
}

.tile {
  width: clamp(34px, var(--rack-tile-size), 46px);
  height: clamp(34px, var(--rack-tile-size), 46px);
  background:
    linear-gradient(180deg, #fbefb0 0%, #f5d85e 32%, #e3b93c 100%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(16px, calc(var(--rack-tile-size) * 0.45), 22px);
  font-weight: bold;
  border-radius: 8px;
  cursor: grab;
  color: #111;
  position: relative;
  box-shadow:
    inset 0 2px 0 rgba(255, 250, 216, 0.85),
    inset 0 -3px 0 rgba(140, 92, 16, 0.35),
    0 3px 0 rgba(102, 60, 19, 0.55),
    0 10px 18px rgba(0, 0, 0, 0.24);
  border: 1px solid rgba(129, 84, 19, 0.45);
  user-select: none;
  transition: transform 0.15s ease, box-shadow 0.15s ease, outline 0.15s ease;
}

.tile::before {
  content: "";
  position: absolute;
  left: 4px;
  right: 4px;
  top: 4px;
  height: 34%;
  border-radius: 6px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0));
  pointer-events: none;
}

.tile:hover {
  transform: translateY(-3px);
  box-shadow:
    inset 0 2px 0 rgba(255, 250, 216, 0.9),
    inset 0 -3px 0 rgba(140, 92, 16, 0.35),
    0 4px 0 rgba(102, 60, 19, 0.55),
    0 14px 20px rgba(0, 0, 0, 0.28);
}

.tile:active {
  cursor: grabbing;
}

.tile .letter {
  position: relative;
  z-index: 1;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}

.tile .tile-value {
  position: absolute;
  right: 6px;
  bottom: 4px;
  font-size: clamp(8px, calc(var(--rack-tile-size) * 0.23), 11px);
  z-index: 1;
}

.tile.is-dragging {
  opacity: 0.55;
}

.tile.is-selected {
  outline: 3px solid rgba(255, 255, 255, 0.75);
  box-shadow:
    inset 0 2px 0 rgba(255, 250, 216, 0.9),
    inset 0 -3px 0 rgba(140, 92, 16, 0.35),
    0 4px 0 rgba(102, 60, 19, 0.55),
    0 0 0 3px rgba(224, 167, 47, 0.35),
    0 14px 22px rgba(0, 0, 0, 0.28);
  transform: translateY(-3px);
}

.tile.rack-insert-before {
  box-shadow:
    inset 0 2px 0 rgba(255, 250, 216, 0.9),
    inset 0 -3px 0 rgba(140, 92, 16, 0.35),
    -4px 0 0 rgba(224, 167, 47, 0.95),
    0 3px 0 rgba(102, 60, 19, 0.55),
    0 10px 18px rgba(0, 0, 0, 0.24);
}

.tile.rack-insert-after {
  box-shadow:
    inset 0 2px 0 rgba(255, 250, 216, 0.9),
    inset 0 -3px 0 rgba(140, 92, 16, 0.35),
    4px 0 0 rgba(224, 167, 47, 0.95),
    0 3px 0 rgba(102, 60, 19, 0.55),
    0 10px 18px rgba(0, 0, 0, 0.24);
}

.tile.is-exchange-mode {
  cursor: pointer;
}

.tile.is-shuffling {
  animation: rack-shuffle 0.5s ease-in-out;
}

.tile.is-flipping {
  transition: transform 0.95s ease;
}

.tile.is-shuffling:nth-child(1) { animation-delay: 0s; }
.tile.is-shuffling:nth-child(2) { animation-delay: 0.03s; }
.tile.is-shuffling:nth-child(3) { animation-delay: 0.06s; }
.tile.is-shuffling:nth-child(4) { animation-delay: 0.09s; }
.tile.is-shuffling:nth-child(5) { animation-delay: 0.12s; }
.tile.is-shuffling:nth-child(6) { animation-delay: 0.15s; }
.tile.is-shuffling:nth-child(7) { animation-delay: 0.18s; }

@keyframes rack-shuffle {
  0% {
    transform: translateY(0) rotate(0deg) scale(1);
  }
  20% {
    transform: translateY(-10px) rotate(-7deg) scale(1.04);
  }
  45% {
    transform: translateY(4px) rotate(8deg) scale(0.98);
  }
  70% {
    transform: translateY(-6px) rotate(-5deg) scale(1.02);
  }
  100% {
    transform: translateY(0) rotate(0deg) scale(1);
  }
}

.cell.is-dragging-tile {
  opacity: 0.65;
  transform: translateY(-2px);
}

@media (max-width: 1100px) {
  body {
    overflow: auto;
  }

  .hero {
    width: 100%;
  }

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

  .sidebar {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 760px) {
  body {
    overflow: auto;
  }

  .app-shell {
    zoom: 1;
    padding: 20px 10px 28px;
    min-height: auto;
  }

  #board-container {
    padding: 12px;
  }

  .sidebar {
    grid-template-columns: 1fr;
  }

  .score-row,
  .stats,
  .turn-grid {
    grid-template-columns: 1fr;
  }

  .rack-caption {
    flex-direction: column;
    align-items: flex-start;
  }

  #rack {
    flex-wrap: wrap;
    justify-content: center;
  }

  .board-actions-panel {
    width: 100%;
  }

  .icon-actions {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
