* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --tw: #ff4444;
  --dw: #ff8888;
  --tl: #2266cc;
  --dl: #66aaee;
  --center: #ff8888;
  --tile-bg: #f5deb3;
  --tile-placed: #e8c880;
  --board-bg: #1a6b3c;
  --cell-bg: #c8e6c0;
  --bg: #1a1a2e;
  --text: #eee;
  --accent: #e94560;
  --btn: #0f3460;
  --btn-secondary: #16213e;
}

html, body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
}

.screen { padding: 1rem; height: 100vh; display: flex; flex-direction: column; }
.hidden { display: none !important; }

/* Join Screen */
#join-screen {
  justify-content: center;
  align-items: center;
  text-align: center;
  gap: 1rem;
}
#join-screen h1 { font-size: 2.5rem; letter-spacing: 0.1em; }
.join-form { display: flex; flex-direction: column; gap: 0.5rem; width: 100%; max-width: 300px; }
.join-form input {
  padding: 0.75rem;
  border-radius: 8px;
  border: none;
  font-size: 1rem;
  text-align: center;
}
#join-status { color: var(--accent); min-height: 1.5em; }

/* Lobby player list */
.lobby-players {
  width: 100%;
  max-width: 300px;
  min-height: 2em;
}
.lobby-player {
  padding: 0.4rem 0.6rem;
  margin: 0.25rem 0;
  background: var(--btn-secondary);
  border-radius: 6px;
  font-size: 0.9rem;
  text-align: center;
}
.lobby-empty {
  font-size: 0.85rem;
  opacity: 0.5;
  text-align: center;
}

/* Danger button */
button.danger { background: #662222; }

/* Buttons */
button {
  padding: 0.6rem 1rem;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: white;
  transition: opacity 0.2s;
}
button:active { opacity: 0.7; }
button.secondary { background: var(--btn-secondary); }

/* Scoreboard */
#scoreboard {
  display: flex;
  gap: 0.5rem;
  padding: 0.3rem 0;
  overflow-x: auto;
  flex-shrink: 0;
}
.player-score {
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  background: var(--btn-secondary);
  font-size: 0.8rem;
  white-space: nowrap;
  flex-shrink: 0;
}
.player-score.current { background: var(--accent); }
.player-score .name { font-weight: 600; }
.player-score .score { margin-left: 0.3rem; opacity: 0.8; }

/* Info bar */
#info-bar {
  display: flex;
  justify-content: space-between;
  padding: 0.3rem 0;
  font-size: 0.8rem;
  opacity: 0.8;
  flex-shrink: 0;
}

/* Board */
#board-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 0.25rem 0;
  min-height: 0;
}

#board {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  gap: 1px;
  background: var(--board-bg);
  border: 2px solid var(--board-bg);
  border-radius: 4px;
  aspect-ratio: 1;
  width: min(100%, 100vh - 14rem);
  max-width: 100%;
}

.cell {
  background: var(--cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: min(2.8vw, 14px);
  font-weight: 700;
  position: relative;
  cursor: pointer;
  user-select: none;
  aspect-ratio: 1;
  min-width: 0;
  min-height: 0;
}

.cell.TW { background: var(--tw); color: white; }
.cell.DW, .cell.center { background: var(--dw); color: white; }
.cell.TL { background: var(--tl); color: white; }
.cell.DL { background: var(--dl); color: white; }

.cell .premium-label {
  font-size: min(1.6vw, 7px);
  text-transform: uppercase;
  opacity: 0.8;
  position: absolute;
  pointer-events: none;
}

.cell .tile-on-board {
  background: var(--tile-bg);
  color: #333;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 2px;
  font-size: min(3vw, 15px);
  position: relative;
}

.cell .tile-on-board.just-placed {
  background: var(--tile-placed);
  box-shadow: 0 0 4px rgba(233, 69, 96, 0.6);
}

.cell .tile-on-board .tile-value {
  position: absolute;
  bottom: 0;
  right: 1px;
  font-size: min(1.6vw, 7px);
  font-weight: 400;
  opacity: 0.6;
}

/* Rack */
#rack-container {
  padding: 0.4rem 0;
  flex-shrink: 0;
}

#rack {
  display: flex;
  gap: 4px;
  justify-content: center;
}

.rack-tile {
  width: clamp(36px, 12vw, 48px);
  height: clamp(36px, 12vw, 48px);
  background: var(--tile-bg);
  color: #333;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(16px, 5vw, 22px);
  font-weight: 700;
  border-radius: 4px;
  cursor: grab;
  user-select: none;
  position: relative;
  transition: transform 0.1s, box-shadow 0.1s;
}

.rack-tile:active { transform: scale(1.1); }
.rack-tile.selected, .rack-tile.multi-selected {
  box-shadow: 0 0 0 3px var(--accent);
  transform: scale(1.05);
}
.rack-tile.exchange-selected { box-shadow: 0 0 0 3px #ffaa00; }
.rack-tile .tile-value {
  position: absolute;
  bottom: 1px;
  right: 3px;
  font-size: 9px;
  font-weight: 400;
  opacity: 0.6;
}
.rack-tile .select-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  background: var(--accent);
  color: white;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* Controls */
#controls {
  display: flex;
  gap: 0.4rem;
  padding: 0.3rem 0;
  justify-content: center;
  flex-wrap: wrap;
  flex-shrink: 0;
}
#controls button { flex: 1; min-width: 0; font-size: 0.8rem; padding: 0.5rem 0.3rem; }

/* Challenge bar */
#challenge-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  background: #332200;
  border-radius: 8px;
  flex-shrink: 0;
}
#challenge-bar span { flex: 1; font-size: 0.85rem; }
#challenge-bar button { font-size: 0.8rem; }

/* Exchange bar */
#exchange-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  background: #223322;
  border-radius: 8px;
  flex-shrink: 0;
}
#exchange-bar span { flex: 1; font-size: 0.85rem; }

/* Lookup */
#lookup-bar {
  display: flex;
  gap: 0.3rem;
  padding: 0.3rem 0;
  align-items: center;
  flex-shrink: 0;
}
#lookup-bar input {
  flex: 1;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  border: none;
  font-size: 0.85rem;
}
#lookup-result { font-size: 0.85rem; min-width: 3em; }
#lookup-result.valid { color: #44ff44; }
#lookup-result.invalid { color: #ff4444; }

/* Messages */
#messages {
  flex-shrink: 0;
  max-height: 4rem;
  overflow-y: auto;
  font-size: 0.75rem;
  opacity: 0.7;
  padding: 0.2rem 0;
}
#messages .msg { padding: 0.1rem 0; }

/* Blank tile modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-content {
  background: var(--btn-secondary);
  padding: 1.5rem;
  border-radius: 12px;
  text-align: center;
  max-width: 90vw;
}
#letter-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  margin-top: 1rem;
}
#letter-grid button {
  width: 36px;
  height: 36px;
  font-size: 1rem;
  font-weight: 700;
  padding: 0;
  background: var(--tile-bg);
  color: #333;
  border-radius: 4px;
}

/* Challenge result banner */
.challenge-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 90;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  animation: bannerSlideIn 0.3s ease-out;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}
.challenge-banner.challenge-success {
  background: linear-gradient(135deg, #1a4d1a, #2a6b2a);
  border-bottom: 3px solid #44ff44;
}
.challenge-banner.challenge-fail {
  background: linear-gradient(135deg, #4d1a1a, #6b2a2a);
  border-bottom: 3px solid #ff4444;
}
.challenge-banner-icon { font-size: 2rem; flex-shrink: 0; }
.challenge-banner-text { flex: 1; font-size: 0.9rem; line-height: 1.4; }
.challenge-banner-text strong { font-size: 1.1rem; }
.challenge-invalid-words { color: #ff8888; font-style: italic; }
.challenge-banner-close {
  background: none;
  border: none;
  color: white;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.25rem;
  opacity: 0.7;
  flex-shrink: 0;
}
.challenge-banner-close:active { opacity: 1; }

@keyframes bannerSlideIn {
  from { transform: translateY(-100%); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* Game over overlay */
.game-over-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99;
}
.game-over-content {
  text-align: center;
  padding: 2rem;
}
.game-over-content h2 { font-size: 2rem; margin-bottom: 1rem; }
.game-over-content .final-scores { margin: 1rem 0; font-size: 1.2rem; }

/* Highlight valid placements */
.cell.highlight {
  box-shadow: inset 0 0 0 2px var(--accent);
  animation: highlight-pulse 1.5s ease-in-out infinite;
  cursor: pointer;
}

@keyframes highlight-pulse {
  0%, 100% { box-shadow: inset 0 0 0 2px var(--accent); }
  50% { box-shadow: inset 0 0 0 2px var(--accent), 0 0 6px var(--accent); }
}

.cell.highlight .highlight-arrow {
  font-size: min(2.4vw, 11px);
  color: var(--accent);
  font-weight: bold;
  pointer-events: none;
}

/* Direction choice modal */
.direction-buttons {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
  justify-content: center;
}
.direction-buttons button {
  padding: 0.8rem 1.5rem;
  font-size: 1.2rem;
}
