:root {
  --bg: #1a0e08;
  --surface: #2a1a10;
  --surface2: #3a2818;
  --primary: #c0913e;
  --primary-glow: rgba(192,145,62,0.3);
  --text: #f0e6d6;
  --text2: #a0907a;
  --danger: #e74c3c;
  --radius: 14px;
  --tw: #e74c3c;
  --dw: #e8a0c0;
  --tl: #2980b9;
  --dl: #85c1e9;
  --center: #c0913e;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  touch-action: manipulation;
}

#app { height: 100%; display: flex; flex-direction: column; }
.screen { display: none; flex-direction: column; align-items: center; padding: 16px; height: 100%; overflow-y: auto; }
.screen.active { display: flex; }

/* HOME */
.logo { text-align: center; margin: 24px 0 16px; }
.logo-icon { font-size: 56px; margin-bottom: 6px; }
.logo h1 { font-size: 28px; color: var(--primary); letter-spacing: 3px; }
.subtitle { color: var(--text2); font-size: 13px; margin-top: 4px; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px;
  width: 100%;
  max-width: 360px;
  border: 1px solid rgba(255,255,255,0.05);
}

input[type="text"] {
  width: 100%;
  padding: 11px 14px;
  background: var(--surface2);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 10px;
  color: var(--text);
  font-size: 16px;
  outline: none;
  margin-bottom: 10px;
}
input:focus { border-color: var(--primary); }

.btn {
  width: 100%;
  padding: 11px;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #1a0e08; }
.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid rgba(255,255,255,0.1); }
.btn-large { padding: 14px; font-size: 17px; max-width: 360px; width: 100%; margin-top: 10px; }
.btn-icon { background: none; border: none; font-size: 18px; cursor: pointer; padding: 4px 8px; }

.divider { text-align: center; margin: 12px 0; color: var(--text2); font-size: 13px; position: relative; }
.divider::before, .divider::after { content: ''; position: absolute; top: 50%; width: 40%; height: 1px; background: rgba(255,255,255,0.1); }
.divider::before { left: 0; }
.divider::after { right: 0; }

.error { color: var(--danger); font-size: 13px; margin-top: 8px; text-align: center; min-height: 18px; }
.hint { color: var(--text2); font-size: 14px; margin-top: 10px; text-align: center; }

/* LOBBY */
.header { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.header h2 { font-size: 17px; }
.code-badge { background: var(--primary); color: #1a0e08; padding: 2px 10px; border-radius: 8px; font-weight: 700; letter-spacing: 2px; font-size: 15px; }

.player-list { list-style: none; }
.player-list li {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 0;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.player-list li:last-child { border: none; }
.player-list img { width: 34px; height: 34px; border-radius: 50%; background: var(--surface2); }
.player-list .host-badge { font-size: 11px; background: var(--primary); color: #1a0e08; padding: 1px 6px; border-radius: 6px; font-weight: 600; }

/* GAME */
#screen-game { padding: 6px; gap: 3px; justify-content: flex-start; overflow: hidden; }

.scores-bar {
  display: flex;
  gap: 6px;
  width: 100%;
  overflow-x: auto;
  flex-shrink: 0;
  padding: 2px 0;
}
.score-item {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--surface);
  padding: 4px 8px;
  border-radius: 8px;
  font-size: 12px;
  white-space: nowrap;
  border: 2px solid transparent;
  flex-shrink: 0;
}
.score-item.active { border-color: var(--primary); }
.score-item.is-me { background: var(--surface2); }
.score-item img { width: 22px; height: 22px; border-radius: 50%; }
.score-item .score-pts { color: var(--primary); font-weight: 700; }

.turn-banner {
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  padding: 4px;
  border-radius: 6px;
  width: 100%;
  flex-shrink: 0;
}
.turn-banner.my-turn { background: rgba(192,145,62,0.15); color: var(--primary); }
.turn-banner.other-turn { background: rgba(255,255,255,0.05); color: var(--text2); }

/* Board */
.board-wrapper {
  flex: 1;
  min-height: 0;
  overflow: auto;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  -webkit-overflow-scrolling: touch;
}

.board {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  gap: 1px;
  width: min(100%, 400px);
  aspect-ratio: 1;
  background: #0a0604;
  border-radius: 4px;
  padding: 1px;
  flex-shrink: 0;
}

.cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #3a2a18;
  border-radius: 2px;
  cursor: pointer;
  position: relative;
  font-size: 10px;
  color: rgba(255,255,255,0.35);
  font-weight: 600;
  transition: background 0.15s;
  user-select: none;
  -webkit-user-select: none;
}
.cell:active { opacity: 0.8; }
.cell.tw { background: var(--tw); color: rgba(255,255,255,0.6); }
.cell.dw { background: var(--dw); color: rgba(255,255,255,0.5); }
.cell.tl { background: var(--tl); color: rgba(255,255,255,0.6); }
.cell.dl { background: var(--dl); color: rgba(100,60,20,0.6); }
.cell.center-star { background: var(--center); color: rgba(255,255,255,0.7); }

/* Placed tile on board */
.cell.has-tile {
  background: #e8d5a0;
  color: #2a1a08;
  font-size: clamp(10px, 2.2vw, 16px);
  font-weight: 700;
  cursor: default;
}
.cell.has-tile .tile-pts {
  position: absolute;
  bottom: 0;
  right: 1px;
  font-size: clamp(5px, 1vw, 8px);
  font-weight: 600;
  color: #6a5a3a;
}
/* Preview tile (not yet confirmed) */
.cell.preview-tile {
  background: #f0c860;
  color: #2a1a08;
  font-size: clamp(10px, 2.2vw, 16px);
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 0 6px rgba(240,200,96,0.5);
}
.cell.preview-tile .tile-pts {
  position: absolute;
  bottom: 0;
  right: 1px;
  font-size: clamp(5px, 1vw, 8px);
  font-weight: 600;
  color: #8a6a2a;
}

.cell.highlight {
  box-shadow: inset 0 0 0 2px var(--primary);
}

/* Last word */
.last-word {
  text-align: center;
  font-size: 12px;
  color: var(--text2);
  padding: 2px;
  flex-shrink: 0;
}

/* Rack */
.rack-area {
  width: 100%;
  flex-shrink: 0;
  padding: 2px 0;
}
.rack-label { font-size: 11px; color: var(--text2); margin-bottom: 3px; text-align: center; }
.rack {
  display: flex;
  gap: 5px;
  justify-content: center;
}
.rack-tile {
  width: 38px;
  height: 42px;
  background: #e8d5a0;
  color: #2a1a08;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  border: 2px solid #c8b580;
  transition: transform 0.15s, border-color 0.15s;
  position: relative;
  user-select: none;
  -webkit-user-select: none;
}
.rack-tile:active { transform: scale(0.95); }
.rack-tile.selected { border-color: var(--primary); transform: translateY(-4px); box-shadow: 0 4px 12px var(--primary-glow); }
.rack-tile.exchange-selected { border-color: var(--danger); opacity: 0.6; }
.rack-tile .tile-val {
  font-size: 8px;
  position: absolute;
  bottom: 1px;
  right: 3px;
  color: #7a6a4a;
  font-weight: 600;
}
.rack-tile.blank-tile { color: #8a7a5a; }

/* Actions */
.action-bar {
  display: flex;
  gap: 5px;
  width: 100%;
  flex-shrink: 0;
  padding: 2px 0;
}
.action-bar .btn { font-size: 12px; padding: 8px 4px; }

.bag-count {
  text-align: center;
  font-size: 11px;
  color: var(--text2);
  flex-shrink: 0;
}

/* Overlay */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.overlay.active { display: flex; }
.overlay h2 { font-size: 28px; margin-bottom: 8px; }
.overlay .winner-name { font-size: 22px; color: var(--primary); font-weight: 700; margin-bottom: 12px; }
.final-scores { margin-bottom: 16px; text-align: center; font-size: 14px; line-height: 1.8; }
.overlay .btn { max-width: 300px; margin-top: 6px; }

/* Blank letter picker */
.blank-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.blank-grid .bletter {
  padding: 10px;
  background: #e8d5a0;
  color: #2a1a08;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
}
.blank-grid .bletter:active { transform: scale(0.93); }

/* Toast */
.toast {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--surface2);
  color: var(--text);
  padding: 8px 18px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 200;
  transition: transform 0.3s;
  pointer-events: none;
  border: 1px solid rgba(255,255,255,0.1);
}
.toast.show { transform: translateX(-50%) translateY(0); }

::-webkit-scrollbar { width: 3px; height: 3px; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 3px; }
