@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

:root {
  --bg: #0a0a0f;
  --bg-widget: #12121a;
  --bg-hover: #1a1a25;
  --bg-header: #0e0e16;
  --border: #1e1e2e;
  --border-hover: #2e2e44;
  --text: #c8c8d4;
  --text-dim: #666680;
  --text-bright: #e8e8f0;
  --accent: #8b5cf6;
  --accent-dim: #5b3daa;
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
  --orange: #f97316;
  --blue: #3b82f6;

  --tile-bg: #e8dcc8;
  --tile-text: #1a1a1a;
  --tile-border: #c4b59a;
  --tile-selected: #f5e6c8;

  --board-bg: #1a1520;
  --cell-bg: #2a2535;
  --cell-border: #3a3545;

  --tw-bg: #991b1b;
  --tw-text: #fca5a5;
  --dw-bg: #7c2d8e;
  --dw-text: #e9a8f5;
  --tl-bg: #1e3a5f;
  --tl-text: #7db8e0;
  --dl-bg: #1a4a3a;
  --dl-text: #6dc7a8;

  --star-color: #eab308;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

::selection {
  background: rgba(139, 92, 246, 0.3);
  color: var(--text-bright);
}

::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

#app {
  height: 100%;
  width: 100%;
}

.view {
  display: none;
  height: 100%;
  width: 100%;
}

.view.active {
  display: flex;
}

/* BUTTONS */

.btn {
  padding: 10px 20px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  background: var(--bg-widget);
  color: var(--text);
}

.btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-hover);
}

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

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-dim);
  border-color: var(--accent-dim);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-secondary {
  background: transparent;
  border-color: var(--border);
  color: var(--text-dim);
}

.btn-secondary:hover {
  color: var(--text);
  border-color: var(--border-hover);
}

.btn-small {
  padding: 8px 12px;
  font-size: 16px;
  line-height: 1;
}

/* LOBBY */

.lobby-container {
  margin: auto;
  padding: 40px 20px;
  max-width: 480px;
  width: 100%;
  text-align: center;
}

.logo {
  font-size: 48px;
  font-weight: 800;
  letter-spacing: 8px;
  color: var(--text-bright);
  margin-bottom: 4px;
}

.subtitle {
  color: var(--text-dim);
  font-size: 14px;
  margin-bottom: 40px;
  letter-spacing: 4px;
  text-transform: uppercase;
}

.lobby-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 40px;
}

.lobby-form input {
  padding: 12px 16px;
  background: var(--bg-widget);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-bright);
  font-family: inherit;
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}

.lobby-form input:focus {
  border-color: var(--accent);
}

.lobby-form input::placeholder {
  color: var(--text-dim);
}

#room-list-section h3 {
  font-size: 13px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-bottom: 12px;
}

.room-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 200px;
  overflow-y: auto;
  margin-bottom: 24px;
}

.room-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: var(--bg-widget);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
}

.room-item:hover {
  border-color: var(--accent);
  background: var(--bg-hover);
}

.room-item-name {
  font-weight: 600;
  color: var(--text-bright);
}

.room-item-info {
  font-size: 12px;
  color: var(--text-dim);
}

.room-item-status {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
}

.room-item-status.waiting {
  background: rgba(34, 197, 94, 0.15);
  color: var(--green);
}

.room-item-status.playing {
  background: rgba(234, 179, 8, 0.15);
  color: var(--yellow);
}

.lobby-stats {
  margin-top: 12px;
}

/* WAITING ROOM */

.waiting-container {
  margin: auto;
  padding: 40px 20px;
  max-width: 400px;
  width: 100%;
  text-align: center;
}

.waiting-container h2 {
  font-size: 20px;
  color: var(--text-bright);
  margin-bottom: 24px;
}

.waiting-container h2 span {
  color: var(--accent);
}

.waiting-players {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 24px;
}

.waiting-player {
  padding: 12px 16px;
  background: var(--bg-widget);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-weight: 600;
  color: var(--text-bright);
}

.waiting-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-bottom: 16px;
}

.waiting-hint {
  font-size: 13px;
  color: var(--text-dim);
}

/* GAME LAYOUT */

.game-layout {
  display: flex;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.game-sidebar {
  width: 220px;
  min-width: 220px;
  background: var(--bg-widget);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.game-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* SCORE PANEL */

.score-panel {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.score-player {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 10px;
  border-radius: 4px;
  margin-bottom: 4px;
  font-size: 13px;
}

.score-player.active {
  background: rgba(139, 92, 246, 0.15);
  border-left: 3px solid var(--accent);
}

.score-player.disconnected {
  opacity: 0.4;
}

.score-name {
  font-weight: 600;
  color: var(--text-bright);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

.score-name.me {
  color: var(--accent);
}

.score-value {
  font-weight: 700;
  font-size: 15px;
  color: var(--text-bright);
}

.score-tiles {
  font-size: 11px;
  color: var(--text-dim);
  margin-left: 4px;
}

/* GAME INFO */

.game-info {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-dim);
}

.bag-count {
  font-weight: 600;
}

/* MOVE LOG */

.move-log {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  font-size: 12px;
}

.log-entry {
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
}

.log-entry .player {
  font-weight: 600;
  color: var(--text);
}

.log-entry .words {
  color: var(--accent);
}

.log-entry .score {
  color: var(--green);
  font-weight: 600;
}

.log-entry .bingo {
  color: var(--yellow);
}

/* TURN BANNER */

.turn-banner {
  padding: 8px 16px;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  background: var(--bg-header);
  border-bottom: 1px solid var(--border);
  min-height: 38px;
}

.turn-banner.my-turn {
  background: rgba(139, 92, 246, 0.1);
  color: var(--accent);
}

/* BOARD */

.board-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  overflow: auto;
  min-height: 0;
  touch-action: pan-x pan-y pinch-zoom;
}

.board-zoom {
  transform-origin: center center;
  will-change: transform;
}

.board {
  --board-size: min(calc(100vh - 200px), calc(100vw - 260px));
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  gap: 1px;
  background: var(--board-bg);
  border: 2px solid var(--border);
  border-radius: 4px;
  aspect-ratio: 1;
  max-width: 100%;
  max-height: 100%;
  width: var(--board-size);
  height: var(--board-size);
}

.cell {
  background: var(--cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
  user-select: none;
  font-size: clamp(6px, 1.8vmin, 13px);
  transition: background 0.1s;
  overflow: hidden;
}

.cell:hover {
  background: var(--bg-hover);
}

.cell.tw { background: var(--tw-bg); }
.cell.dw { background: var(--dw-bg); }
.cell.tl { background: var(--tl-bg); }
.cell.dl { background: var(--dl-bg); }

.cell-label {
  font-size: clamp(4px, 1.2vmin, 9px);
  font-weight: 700;
  text-transform: uppercase;
  opacity: 0.6;
  pointer-events: none;
  letter-spacing: 0.5px;
}

.cell.tw .cell-label { color: var(--tw-text); }
.cell.dw .cell-label { color: var(--dw-text); }
.cell.tl .cell-label { color: var(--tl-text); }
.cell.dl .cell-label { color: var(--dl-text); }

.cell.center .cell-label {
  font-size: clamp(8px, 2.5vmin, 20px);
  color: var(--star-color);
  opacity: 1;
}

.cell .tile {
  position: absolute;
  inset: 0;
  background: var(--tile-bg);
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--tile-text);
  font-weight: 800;
  font-size: clamp(13px, calc(var(--board-size) / 22), 28px);
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
  z-index: 1;
  line-height: 1;
}

.cell .tile.new-tile {
  box-shadow: 0 0 0 2px var(--accent), 0 1px 2px rgba(0,0,0,0.3);
  cursor: grab;
}

.cell .tile.blank-tile {
  font-style: italic;
  opacity: 0.85;
}

.cell .tile .tile-value {
  position: absolute;
  bottom: 0;
  right: 1px;
  font-size: clamp(6px, 1.5vmin, 10px);
  font-weight: 700;
  opacity: 0.6;
}

/* DRAG AND DROP */

.drag-ghost {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  background: var(--tile-bg);
  border: 2px solid var(--accent);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--tile-text);
  font-weight: 800;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  opacity: 0.95;
  transform: scale(1.1);
}

.cell.drop-target {
  background: rgba(139, 92, 246, 0.3) !important;
  box-shadow: inset 0 0 0 2px var(--accent);
}

.cell .tile.dragging,
.rack-tile.dragging {
  opacity: 0.25;
}

.rack-tile.drop-indicator {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

body.is-dragging {
  cursor: grabbing !important;
  user-select: none;
}

body.is-dragging * {
  cursor: grabbing !important;
}

.board {
  touch-action: none;
}

.rack {
  touch-action: none;
}

.turn-banner.waiting {
  background: rgba(234, 179, 8, 0.1);
  color: var(--yellow);
}

/* RACK */

.rack-area {
  padding: 8px 16px;
  background: var(--bg-header);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

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

.rack-tile {
  width: clamp(40px, 8vw, 54px);
  height: clamp(40px, 8vw, 54px);
  background: var(--tile-bg);
  border: 2px solid var(--tile-border);
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--tile-text);
  font-weight: 800;
  font-size: clamp(18px, 4vw, 26px);
  cursor: grab;
  user-select: none;
  position: relative;
  transition: all 0.15s;
  touch-action: none;
  line-height: 1;
}

.rack-tile:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}

.rack-tile:active {
  cursor: grabbing;
}

.rack-tile.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
  transform: translateY(-4px);
  background: var(--tile-selected);
}

.rack-tile.exchange-selected {
  border-color: var(--red);
  box-shadow: 0 0 0 2px var(--red);
  opacity: 0.7;
}

.rack-tile .tile-value {
  position: absolute;
  bottom: 1px;
  right: 3px;
  font-size: clamp(8px, 1.5vw, 10px);
  font-weight: 600;
  opacity: 0.6;
}

.rack-controls {
  display: flex;
  gap: 6px;
  align-items: center;
}

/* MODALS */

.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal.active {
  display: flex;
}

.modal-content {
  background: var(--bg-widget);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px;
  max-width: 500px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
}

.modal-content.modal-small {
  max-width: 360px;
}

.modal-content h2, .modal-content h3 {
  color: var(--text-bright);
  margin-bottom: 16px;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 24px;
  cursor: pointer;
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 16px;
}

/* BLANK LETTER PICKER */

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

.blank-letter-btn {
  padding: 10px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-bright);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.1s;
  font-family: inherit;
}

.blank-letter-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
}

/* LEADERBOARD */

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.leaderboard-table th {
  text-align: left;
  padding: 8px;
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.leaderboard-table td {
  padding: 8px;
  border-bottom: 1px solid var(--border);
}

.leaderboard-table tr:hover td {
  background: var(--bg-hover);
}

.rank-1 { color: var(--yellow); font-weight: 700; }
.rank-2 { color: #c0c0c0; font-weight: 600; }
.rank-3 { color: #cd7f32; font-weight: 600; }

/* FINAL SCORES */

.final-score-entry {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  margin-bottom: 8px;
  background: var(--bg-hover);
  border-radius: 6px;
  border: 1px solid var(--border);
}

.final-score-entry.winner {
  border-color: var(--yellow);
  background: rgba(234, 179, 8, 0.1);
}

.final-score-entry .name {
  font-weight: 700;
  font-size: 16px;
  color: var(--text-bright);
}

.final-score-entry .final-score {
  font-weight: 800;
  font-size: 20px;
  color: var(--accent);
}

.final-score-entry.winner .final-score {
  color: var(--yellow);
}

.winner-badge {
  font-size: 11px;
  padding: 2px 8px;
  background: var(--yellow);
  color: #000;
  border-radius: 10px;
  font-weight: 700;
  margin-left: 8px;
}

/* TOAST */

.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--bg-widget);
  border: 1px solid var(--border);
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  z-index: 2000;
  opacity: 0;
  transition: all 0.3s;
  pointer-events: none;
  max-width: 90vw;
  text-align: center;
}

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

.toast.error {
  border-color: var(--red);
  color: var(--red);
}

.toast.success {
  border-color: var(--green);
  color: var(--green);
}

.toast.info {
  border-color: var(--blue);
  color: var(--blue);
}

/* RECENT GAMES */

.recent-game {
  padding: 10px;
  margin-bottom: 6px;
  background: var(--bg-hover);
  border-radius: 4px;
  font-size: 12px;
}

.recent-game-date {
  color: var(--text-dim);
  font-size: 11px;
  margin-bottom: 4px;
}

.recent-game-scores {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.recent-score {
  color: var(--text);
}

.recent-score.winner {
  color: var(--yellow);
  font-weight: 600;
}

/* RESPONSIVE */

@media (max-width: 768px) {
  .game-layout {
    flex-direction: column;
  }

  .game-sidebar {
    width: 100%;
    min-width: unset;
    flex-direction: row;
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 60px;
    overflow-x: auto;
  }

  .score-panel {
    display: flex;
    gap: 8px;
    border-bottom: none;
    padding: 8px;
    white-space: nowrap;
  }

  .score-player {
    margin-bottom: 0;
    padding: 4px 8px;
    font-size: 12px;
    flex-shrink: 0;
  }

  .game-info {
    display: flex;
    align-items: center;
    padding: 8px;
    border-bottom: none;
    border-left: 1px solid var(--border);
    flex-shrink: 0;
  }

  .move-log {
    display: none;
  }

  .board {
    --board-size: min(calc(100vw - 16px), calc(100vh - 240px));
  }

  .rack-area {
    padding: 6px 8px;
    gap: 8px;
  }

  .rack-controls {
    gap: 4px;
  }

  .btn {
    padding: 8px 14px;
    font-size: 13px;
  }

  .btn-small {
    padding: 6px 10px;
  }

  .turn-banner {
    padding: 4px 12px;
    font-size: 13px;
    min-height: 28px;
  }

  .logo {
    font-size: 36px;
  }
}

@media (max-width: 400px) {
  .rack-tile {
    width: 34px;
    height: 34px;
    font-size: 15px;
  }

  .rack-controls .btn {
    padding: 6px 10px;
    font-size: 12px;
  }
}
