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

:root {
  --tile-size: 52px;
  --tile-font: 24px;
  --tile-bg: #F5DEB3;
  --tile-border: #C4A468;
  --tile-selected: #FFD700;
  --tile-invalid: #FF6B6B;
  --board-bg: #2C5F2D;
  --board-grid: rgba(255,255,255,0.08);
  --btn-primary: #2C5F2D;
  --btn-secondary: #5C4033;
  --text-light: #fff;
  --bg-page: #1a1a2e;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg-page);
  color: var(--text-light);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-text-size-adjust: 100%;
}

/* Prevent double-tap zoom on all interactive elements */
button, .rack-tile, .board-tile, .grid-cell, #boardArea, #tileRack, #bottomPanel {
  touch-action: manipulation;
}

/* ========== LANDING PAGE ========== */

#landing, #lobby {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
  gap: 16px;
}

h1 {
  font-size: 3em;
  color: #FFD700;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}

.subtitle {
  font-size: 1.2em;
  opacity: 0.7;
  margin-bottom: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 320px;
  gap: 6px;
}

.form-group label {
  font-size: 1em;
  opacity: 0.8;
}

.form-group input {
  font-size: 1.3em;
  padding: 14px 16px;
  border: 2px solid #444;
  border-radius: 10px;
  background: #222;
  color: #fff;
  outline: none;
}

.form-group input:focus {
  border-color: #FFD700;
}

.btn {
  font-size: 1.3em;
  padding: 16px 32px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 700;
  min-width: 140px;
  min-height: 56px;
  transition: transform 0.1s, opacity 0.1s;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: var(--btn-primary);
  color: var(--text-light);
}

.btn-secondary {
  background: var(--btn-secondary);
  color: var(--text-light);
}

.btn-large {
  font-size: 1.6em;
  padding: 20px 48px;
  margin-top: 20px;
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.button-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.hidden { display: none !important; }

.error {
  color: #FF6B6B;
  font-size: 1.1em;
  margin-top: 8px;
  text-align: center;
}

.share-hint {
  opacity: 0.6;
  margin-bottom: 12px;
}

#roomCode {
  color: #FFD700;
  font-size: 1.4em;
  letter-spacing: 0.15em;
}

.player-item {
  font-size: 1.2em;
  padding: 8px 20px;
  background: rgba(255,255,255,0.1);
  border-radius: 8px;
  margin: 4px 0;
}

/* ========== GAME PAGE ========== */

#gameContainer {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  background: var(--board-bg);
}

/* Top bar */
#topBar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: rgba(0,0,0,0.4);
  min-height: 44px;
  flex-shrink: 0;
  gap: 8px;
  flex-wrap: wrap;
}

#poolInfo {
  font-size: 1em;
  font-weight: 700;
  white-space: nowrap;
}

#playerScores {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  font-size: 0.9em;
}

.score-badge {
  padding: 3px 10px;
  background: rgba(255,255,255,0.15);
  border-radius: 6px;
  white-space: nowrap;
}

.score-badge.me {
  background: rgba(255,215,0,0.3);
  border: 1px solid #FFD700;
}

#voiceControls {
  display: flex;
  gap: 6px;
}

#muteBtn {
  font-size: 1.4em;
  background: none;
  border: 2px solid rgba(255,255,255,0.3);
  border-radius: 8px;
  color: white;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#muteBtn svg {
  width: 22px;
  height: 22px;
}

#muteBtn .mic-slash {
  stroke: #FF6B6B;
  stroke-width: 2.5;
}

#muteBtn.muted {
  border-color: #FF6B6B;
  opacity: 0.6;
}

/* Board area */
#boardArea {
  flex: 1;
  position: relative;
  overflow: hidden;
  touch-action: none;
}

#boardCanvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#boardGrid {
  position: absolute;
  transform-origin: 0 0;
}

.grid-cell {
  position: absolute;
  width: var(--tile-size);
  height: var(--tile-size);
  border: 1px solid var(--board-grid);
}

.grid-cell.highlight {
  background: rgba(255,255,255,0.12);
  border-color: rgba(255,255,255,0.3);
}

.board-tile {
  position: absolute;
  width: var(--tile-size);
  height: var(--tile-size);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--tile-bg);
  border: 2px solid var(--tile-border);
  border-radius: 6px;
  font-size: var(--tile-font);
  font-weight: 800;
  color: #333;
  cursor: pointer;
  z-index: 2;
  box-shadow: 1px 2px 4px rgba(0,0,0,0.3);
}

.board-tile .points {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 10px;
  font-weight: 600;
  color: #888;
}

.board-tile.invalid {
  border-color: var(--tile-invalid);
  box-shadow: 0 0 8px var(--tile-invalid);
}

/* Bottom panel — tile rack + controls */
#bottomPanel {
  flex-shrink: 0;
  background: rgba(0,0,0,0.5);
  padding: 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
}

#tileRack {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  padding: 6px 4px;
  min-height: 60px;
  align-items: center;
}

.rack-tile {
  width: var(--tile-size);
  height: var(--tile-size);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--tile-bg);
  border: 2px solid var(--tile-border);
  border-radius: 6px;
  font-size: var(--tile-font);
  font-weight: 800;
  color: #333;
  cursor: pointer;
  position: relative;
  box-shadow: 1px 2px 4px rgba(0,0,0,0.3);
  transition: transform 0.1s;
  flex-shrink: 0;
}

.rack-tile .points {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 10px;
  font-weight: 600;
  color: #888;
}

.rack-tile.selected {
  border-color: var(--tile-selected);
  box-shadow: 0 0 12px var(--tile-selected);
  transform: scale(1.1);
}

.rack-tile:active {
  transform: scale(0.95);
}

#controls {
  display: flex;
  gap: 8px;
  justify-content: center;
  padding-top: 8px;
}

#takeTwoBtn {
  font-size: 1.4em;
  padding: 14px 36px;
  background: #FFD700;
  color: #333;
  border: none;
  border-radius: 12px;
  font-weight: 800;
  cursor: pointer;
  min-height: 52px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.4);
  transition: transform 0.1s;
}

#takeTwoBtn:active:not(:disabled) {
  transform: scale(0.96);
}

#takeTwoBtn:disabled {
  opacity: 0.3;
  cursor: default;
}

#stuckBtn {
  font-size: 1em;
  padding: 10px 16px;
  background: #8B4513;
  color: white;
  border: 1px solid #A0522D;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
}

#stuckBtn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Lobby settings */
#lobbySettings {
  width: 100%;
  max-width: 320px;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 8px 0;
  gap: 12px;
}

.setting-row label {
  font-size: 1em;
  opacity: 0.8;
}

.setting-row select {
  font-size: 1em;
  padding: 8px 12px;
  border: 2px solid #444;
  border-radius: 8px;
  background: #222;
  color: #fff;
}

#readyBtn {
  margin-top: 12px;
}

.player-item.ready {
  background: rgba(42, 119, 42, 0.4);
  border: 1px solid #2a7;
}

/* Rack row with shuffle */
#rackRow {
  display: flex;
  align-items: center;
  gap: 6px;
}

#rackRow #tileRack {
  flex: 1;
}

#shuffleBtn {
  width: 44px;
  height: 44px;
  font-size: 1.3em;
  background: rgba(255,255,255,0.15);
  color: white;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 8px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Zoom controls */
#zoomControls {
  position: absolute;
  bottom: 8px;
  right: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 10;
}

#zoomInBtn, #zoomOutBtn {
  width: 40px;
  height: 40px;
  font-size: 1.5em;
  font-weight: 800;
  background: rgba(0,0,0,0.6);
  color: white;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Selected board tile */
.board-tile.selected {
  border-color: var(--tile-selected);
  box-shadow: 0 0 12px var(--tile-selected);
}

#clearBoardBtn, #recenterBtn {
  font-size: 1em;
  padding: 10px 16px;
  background: rgba(255,255,255,0.15);
  color: white;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 8px;
  cursor: pointer;
}

/* Toast notification */
#toast {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 24px;
  background: rgba(0,0,0,0.85);
  color: white;
  border-radius: 10px;
  font-size: 1.1em;
  font-weight: 600;
  z-index: 100;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
}

#toast.show {
  opacity: 1;
}

/* Opponent mini-boards */
#opponentPanels {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 10;
  max-height: calc(100% - 16px);
  overflow-y: auto;
}

.opponent-panel {
  background: rgba(0,0,0,0.7);
  border-radius: 10px;
  padding: 4px 10px;
  min-width: 100px;
  border: 1px solid rgba(255,255,255,0.15);
  cursor: pointer;
}

.opponent-panel .opp-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.opponent-panel .opp-name {
  font-size: 0.75em;
  font-weight: 600;
  opacity: 0.9;
  white-space: nowrap;
}

.opponent-panel .opp-toggle {
  font-size: 0.6em;
  opacity: 0.5;
  transition: transform 0.2s;
}

.opponent-panel.expanded .opp-toggle {
  transform: rotate(180deg);
}

.opponent-panel .opp-mini {
  position: relative;
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.25s ease, margin 0.25s ease;
  margin-top: 0;
}

.opponent-panel.expanded .opp-mini {
  max-height: 300px;
  margin-top: 4px;
}

.opp-mini-tile {
  position: absolute;
  background: var(--tile-bg);
  border: 1px solid var(--tile-border);
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  color: #333;
  line-height: 1;
}

/* Winner board display */
#winnerBoard {
  position: relative;
  margin: 12px auto;
  max-width: 90vw;
  max-height: 40vh;
  overflow: auto;
}

#winnerBoard .wb-label {
  font-size: 1em;
  opacity: 0.7;
  margin-bottom: 8px;
  text-align: center;
}

#winnerBoard .wb-grid {
  position: relative;
  margin: 0 auto;
}

#winnerBoard .wb-tile {
  position: absolute;
  width: 36px;
  height: 36px;
  background: var(--tile-bg);
  border: 2px solid var(--tile-border);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 800;
  color: #333;
}

/* Game over overlay */
#gameOver {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
  gap: 16px;
}

#gameOver h2 {
  font-size: 2.5em;
  color: #FFD700;
}

#gameOver .final-scores {
  font-size: 1.3em;
  text-align: center;
  line-height: 2;
}

/* Blank tile modal */
#blankModal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 150;
}

#blankModal .modal-content {
  background: #2a2a3e;
  border-radius: 16px;
  padding: 24px;
  text-align: center;
  max-width: 340px;
  width: 90%;
}

#blankModal h3 {
  margin-bottom: 16px;
  font-size: 1.3em;
}

#blankModal .letter-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}

#blankModal .letter-choice {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--tile-bg);
  color: #333;
  font-size: 1.2em;
  font-weight: 800;
  border: 2px solid var(--tile-border);
  border-radius: 6px;
  cursor: pointer;
}

#blankModal .letter-choice:active {
  transform: scale(0.9);
}

/* Responsive */
@media (max-width: 400px) {
  :root {
    --tile-size: 46px;
    --tile-font: 20px;
  }
}

@media (min-width: 768px) {
  :root {
    --tile-size: 56px;
    --tile-font: 26px;
  }
}
