/* ============================================
   CSS Variables & Reset
   ============================================ */
:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #2a2a3e;
  --bg-tertiary: #1e1e30;
  --tile-color: #d4a843;
  --tile-text: #1a1a2e;
  --tile-new-glow: #e8c84a;
  --tw-color: #c0504d;
  --dw-color: #9e8daa;
  --tl-color: #5897b0;
  --dl-color: #7aaab8;
  --accent: #1abc9c;
  --text-primary: #ffffff;
  --text-muted: #8888aa;
  --success: #2ecc71;
  --warning: #f39c12;
  --danger: #e74c3c;
  --cell-bg: #363650;
  --cell-border: #252540;
  --font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
}

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

body {
  font-family: var(--font-family);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  overflow-x: hidden;
}

/* ============================================
   Screens
   ============================================ */
.screen {
  display: none;
  width: 100%;
  min-height: 100vh;
}
.screen.active {
  display: flex;
}

/* ============================================
   Lobby Screen
   ============================================ */
.lobby-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 100vh;
  padding: 2rem;
}

.lobby-title {
  font-size: 3rem;
  font-weight: 800;
  margin-bottom: 0.5rem;
  background: linear-gradient(135deg, var(--tile-color), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.lobby-subtitle {
  color: var(--text-muted);
  margin-bottom: 1rem;
  font-size: 1.1rem;
}

/* ============================================
   Auth inline row (username field + sign-in/out)
   ============================================ */
.username-row {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
}
.username-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin-top: 0.25rem;
}
.username-error.hidden {
  display: none;
}
#username-input.input-error {
  border-color: var(--danger);
  animation: shake 0.3s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-4px); }
  40%, 80% { transform: translateX(4px); }
}
.username-row-auth {
  display: flex;
  flex-direction: column;
}
.username-row-auth label {
  display: block;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 0.4rem;
  font-weight: 600;
}
.username-row-auth .btn {
  white-space: nowrap;
  text-decoration: none;
  /* match the height of the text input */
  padding: 0.7rem 1rem;
  font-size: 0.9rem;
}

.auth-signed-in-row { display: none; }
.auth-signed-in-row.hidden { display: none !important; }
.auth-signed-in-row:not(.hidden) { display: block; }

.auth-signed-in-inner {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--cell-border);
  border-radius: 8px;
  padding: 0.6rem 1rem;
}
.auth-avatar { font-size: 1.1rem; }
.auth-username {
  flex: 1;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.auth-signout-btn { text-decoration: none; }

.lobby-form {
  background: var(--bg-secondary);
  padding: 2rem;
  border-radius: 12px;
  width: 100%;
  max-width: 400px;
}

.form-group {
  margin-bottom: 1.2rem;
}

.form-group label {
  display: block;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 0.4rem;
  font-weight: 600;
}

.checkbox-label {
  display: flex !important;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.checkbox-label:has(input[type="checkbox"]:disabled) {
  cursor: not-allowed;
  opacity: 0.6;
}

.checkbox-label:has(input[type="checkbox"]:disabled) input[type="checkbox"] {
  cursor: not-allowed;
}

input[type="text"] {
  width: 100%;
  padding: 0.7rem 1rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--cell-border);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 1rem;
  font-family: var(--font-family);
  outline: none;
  transition: border-color 0.2s;
}
input[type="text"]:focus {
  border-color: var(--accent);
}

.lobby-actions {
  display: flex;
  gap: 1rem;
}
.lobby-actions-vertical {
  flex-direction: column;
}
.btn-large {
  padding: 1rem 1.5rem;
  font-size: 1.1rem;
}

/* ============================================
   Buttons
   ============================================ */
.btn {
  padding: 0.7rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  font-family: var(--font-family);
  transition: all 0.15s ease;
  white-space: nowrap;
}
.btn:hover { opacity: 0.9; transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.formal-btn-wrapper.guest-disabled .btn:disabled {
  opacity: 0.75;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}
.time-btn-wrapper.guest-disabled .btn:disabled {
  opacity: 0.75;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

.btn-accent {
  background: var(--accent);
  color: var(--bg-primary);
}
.btn-success {
  background: var(--success);
  color: white;
}
.btn-outlined {
  background: transparent;
  color: var(--text-primary);
  border: 1px solid var(--text-muted);
}
.btn-outlined:hover { border-color: var(--text-primary); }
.btn-warning-outlined {
  background: transparent;
  color: var(--warning);
  border: 1px solid var(--warning);
}
.btn-danger {
  background: var(--danger);
  color: white;
}
.btn-danger-outlined {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}
.btn-danger-outlined:hover {
  background: var(--danger);
  color: white;
}
.btn-sm {
  padding: 0.4rem 0.8rem;
  font-size: 0.8rem;
}
.btn-group {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.btn-group .btn.active {
  background: var(--accent);
  color: var(--bg-primary);
  border-color: var(--accent);
}
.recorded-legend {
  font-size: 0.7em;
  opacity: 0.7;
  font-weight: 400;
}
.formal-btn-wrapper {
  position: relative;
}
.formal-btn-wrapper[data-tooltip].guest-disabled::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}
.formal-btn-wrapper.guest-disabled:hover::after {
  opacity: 1;
}

/* AI Hint tooltip (same pattern as formal button) */
.hint-checkbox-wrapper {
  position: relative;
  display: block;
}

.hint-checkbox-wrapper[data-tooltip].guest-disabled::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}

.hint-checkbox-wrapper.guest-disabled:hover::after {
  opacity: 1;
}

/* Time button tooltip (same pattern) */
.time-btn-wrapper {
  position: relative;
  display: block;
}

.time-btn-wrapper[data-tooltip].guest-disabled::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}

.time-btn-wrapper.guest-disabled:has(.btn:disabled:hover)::after {
  opacity: 1;
}

/* AI Difficulty tooltip */
.ai-diff-wrapper,
.ai-char-wrapper {
  position: relative;
  display: block;
}

.ai-diff-wrapper[data-tooltip].guest-disabled::after,
.ai-char-wrapper[data-tooltip].guest-disabled::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}

.ai-diff-wrapper.guest-disabled:has(.btn:disabled:hover)::after,
.ai-char-wrapper.guest-disabled:has(.btn:disabled:hover)::after {
  opacity: 1;
}

.ai-diff-wrapper.guest-disabled .btn:disabled,
.ai-char-wrapper.guest-disabled .btn:disabled {
  opacity: 0.75;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

/* AI Character grid */
.ai-char-grid {
  flex-wrap: wrap;
}

.ai-char-btn {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: center;
}

.char-emoji {
  font-size: 1rem;
}

.char-count-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 400;
}

/* Add AI bot tooltip */
.add-ai-wrapper {
  position: relative;
  display: block;
}

.add-ai-wrapper[data-tooltip].guest-disabled::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}

.add-ai-wrapper.guest-disabled:has(.btn:disabled:hover)::after {
  opacity: 1;
}

.add-ai-wrapper.guest-disabled .btn:disabled,
.add-ai-btn:disabled {
  opacity: 0.75;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  cursor: not-allowed;
}

/* Add AI button disabled because character already in room */
.add-ai-btn.ai-already-added {
  opacity: 0.5;
  cursor: not-allowed;
  text-decoration: line-through;
}

/* Solo max AI tooltip */
.ai-max-tooltip {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}

.ai-max-tooltip.visible {
  opacity: 1;
}

/* AI Count tooltip */
.ai-count-wrapper {
  position: relative;
  display: block;
}

.ai-count-wrapper[data-tooltip].guest-disabled::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}

.ai-count-wrapper.guest-disabled:hover::after {
  opacity: 1;
}

.ai-count-wrapper.guest-disabled .btn:disabled {
  opacity: 0.75;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

/* Timeout mode tooltip */
.timeout-btn-wrapper {
  position: relative;
  display: block;
}

.timeout-btn-wrapper[data-tooltip].guest-disabled::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}

.timeout-btn-wrapper.guest-disabled:hover::after {
  opacity: 1;
}

.timeout-btn-wrapper.guest-disabled .btn:disabled {
  opacity: 0.75;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

/* Public Room checkbox tooltip */
.public-room-wrapper {
  position: relative;
  display: block;
}

.public-room-wrapper[data-tooltip].guest-disabled::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-tertiary, #333);
  color: var(--text-primary, #fff);
  padding: 0.35rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
}

.public-room-wrapper.guest-disabled:hover::after {
  opacity: 1;
}

/* ============================================
   Modals
   ============================================ */
.modal {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal.hidden { display: none; }

.modal-content {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 2rem;
  width: 90%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-content.modal-wide {
  max-width: 700px;
}
.modal-content h2 {
  margin-bottom: 1rem;
  font-size: 1.3rem;
}
.modal-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 1.5rem;
  flex-wrap: wrap;
}

/* ============================================
   Waiting Room
   ============================================ */
.waiting-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 100vh;
  padding: 2rem;
}
.room-code-display {
  color: var(--text-muted);
  margin-bottom: 2rem;
  font-size: 1.1rem;
}
.room-code-display span {
  color: var(--accent);
  font-weight: 700;
  font-size: 1.3rem;
  letter-spacing: 2px;
}
.waiting-room-settings {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: center;
  margin-bottom: 1rem;
}

.setting-tag {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 500;
}

.waiting-players {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 2rem;
}
.waiting-player-card {
  background: var(--bg-secondary);
  padding: 1.5rem 2rem;
  border-radius: 12px;
  text-align: center;
  min-width: 140px;
}
.waiting-player-card .avatar {
  width: 50px; height: 50px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 700;
  margin: 0 auto 0.6rem;
  color: var(--bg-primary);
}
.waiting-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
}
.ai-add-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
}
.ai-bot-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin-top: 12px;
}
.remove-ai-btn {
  margin-left: auto;
  padding: 2px 8px !important;
  font-size: 0.75rem !important;
  color: var(--danger) !important;
  border-color: var(--danger) !important;
  background: transparent !important;
}
.remove-ai-btn:hover {
  background: var(--danger) !important;
  color: white !important;
}
.waiting-player-card {
  position: relative;
}
.ai-diff-badge {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 400;
}
.ai-difficulty-select {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--cell-border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.8rem;
  font-family: var(--font-family);
  cursor: pointer;
  outline: none;
}
.ai-difficulty-select:focus {
  border-color: var(--accent);
}

/* ============================================
   Room List
   ============================================ */
.room-combobox {
  position: relative;
}

.room-dropdown {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease;
  border-radius: 0 0 8px 8px;
  background: var(--bg-secondary);
}

.room-combobox:focus-within .room-dropdown,
.room-dropdown.has-rooms {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-top: none;
}

.room-dropdown .room-item {
  padding: 0.5rem 0.8rem;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  border-bottom: 1px solid var(--border);
}

.room-dropdown .room-item:last-child {
  border-bottom: none;
}

.room-dropdown .room-item:hover {
  background: var(--bg-tertiary);
}

.room-dropdown .text-muted {
  padding: 0.5rem 0.8rem;
  font-size: 0.8rem;
}

/* ============================================
   Game Layout
   ============================================ */
.game-layout {
  display: flex;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.left-panel {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 0.8rem;
  gap: 0.6rem;
  overflow: hidden;
}

.right-panel {
  flex: 0 0 340px;
  max-width: 40%;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  padding: 0.8rem;
  padding-left: 0;
  gap: 0.6rem;
  overflow-y: auto;
  max-height: 100vh;
}

/* ============================================
   Board
   ============================================ */
.board-container {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  overflow: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.board-container::-webkit-scrollbar {
  display: none;
}
.board-container.board-pannable {
  cursor: grab;
}
.board-container.board-pan-active {
  cursor: grabbing !important;
  user-select: none;
  -webkit-user-select: none;
}

.board-wrapper {
  width: 100%;
  max-width: min(680px, 100%);
  margin: auto;
  user-select: none;
  -webkit-user-select: none;
}

.board-col-headers {
  display: flex;
  margin-bottom: 2px;
}
.header-spacer {
  width: 22px;
  flex-shrink: 0;
}
.col-header {
  flex: 1;
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  padding: 2px 0;
}

.board-body {
  display: flex;
}

.board-row-headers {
  display: flex;
  flex-direction: column;
  width: 22px;
  flex-shrink: 0;
}
.row-header {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
}

.board-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  gap: 2px;
  background: var(--cell-border);
  border: 2px solid var(--cell-border);
  border-radius: 4px;
  aspect-ratio: 1;
}

.board-cell {
  background: var(--cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
  transition: background-color 0.1s;
  font-size: 10px;
  font-weight: 700;
  user-select: none;
  aspect-ratio: 1;
  border-radius: 2px;
}

.board-cell.premium-tw {
  background: var(--tw-color);
  color: rgba(255,255,255,0.9);
}
.board-cell.premium-dw {
  background: var(--dw-color);
  color: rgba(255,255,255,0.9);
}
.board-cell.premium-tl {
  background: var(--tl-color);
  color: rgba(255,255,255,0.9);
}
.board-cell.premium-dl {
  background: var(--dl-color);
  color: rgba(255,255,255,0.9);
}

.board-cell.center-star {
  font-size: 16px;
}

.board-cell.drop-target {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
.board-cell.invalid-target {
  outline: 2px solid var(--danger);
  outline-offset: -2px;
}

.board-cell .premium-label {
  font-size: 9px;
  font-size: calc(var(--board-cell-size, 18px) * 0.45);
  font-weight: 700;
  line-height: 1;
  text-align: center;
  opacity: 0.9;
}

/* Tiles on board */
.board-tile {
  position: absolute;
  inset: 1px;
  background: var(--tile-color);
  border-radius: calc(var(--board-cell-size, 14px) * 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}
.board-tile .tile-letter {
  font-size: calc(var(--board-cell-size, 14px) * 0.6);
  font-weight: 800;
  color: var(--tile-text);
  line-height: 1;
}
.board-tile .tile-points {
  position: absolute;
  bottom: 0;
  right: 1px;
  font-size: calc(var(--board-cell-size, 14px) * 0.25);
  font-weight: 700;
  color: var(--tile-text);
  opacity: 0.7;
}

.board-tile.new-tile {
  box-shadow: 0 0 6px 2px rgba(232, 200, 74, 0.5);
  border: 1px solid var(--tile-new-glow);
  animation: tilePlace 0.15s ease-out;
}

.board-tile.last-move-tile {
  border: 1.5px solid rgba(255, 255, 255, 0.45);
  box-shadow: 0 0 4px 1px rgba(255, 255, 255, 0.15);
}

.board-tile.blank-tile {
  background: #c9a83a;
}

.board-tile.blank-tile .tile-letter {
  color: #e74c3c;
  font-style: italic;
}

@keyframes tilePlace {
  from { transform: scale(0.9); opacity: 0.7; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes tileShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

.board-tile.shake {
  animation: tileShake 0.3s ease-in-out;
  border: 1px solid var(--danger) !important;
}

/* ============================================
   Tile Rack
   ============================================ */
.rack-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary);
  border-radius: 12px;
  padding: 8px 12px;
  gap: 24px;
  flex-shrink: 0;
  user-select: none;
  -webkit-user-select: none;
}

.rack-utility-btns {
  display: flex;
  flex-direction: row;
  flex: 0 0 auto;
  gap: 8px;
  align-items: center;
  justify-content: center;
}

.rack-btn {
  background: var(--bg-secondary);
  border: none;
  border-radius: 8px;
  color: var(--text-muted);
  cursor: pointer;
  width: 48px;
  height: 48px;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}
@media (hover: hover) and (pointer: fine) {
  .rack-btn:hover {
    color: var(--text-primary);
    background: #353550;
  }
  /* Icon-only action buttons: hover only on pointer devices */
  #game-actions .btn.icon-only:hover {
    color: var(--text-primary);
    background: #353550;
  }
}
.rack-btn:focus {
  outline: none;
}
.rack-btn:focus-visible {
  outline: 2px solid var(--text-muted);
  outline-offset: 2px;
}
.rack-btn:active,
#game-actions .btn.icon-only:active {
  background: #353550;
  color: var(--text-primary);
}
.rack-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
}

.tile-rack {
  display: flex;
  flex: 0 0 auto;
  min-width: 0;
  gap: 6px;
  justify-content: center;
  min-height: 48px;
}

.rack-tile {
  width: 48px;
  height: 48px;
  background: var(--tile-color);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: grab;
  user-select: none;
  transition: transform 0.1s, box-shadow 0.1s;
  container-type: inline-size;
}
.rack-tile:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.rack-tile.selected {
  transform: translateY(-6px);
  box-shadow: 0 6px 16px rgba(26, 188, 156, 0.4);
  border: 2px solid var(--accent);
}
.rack-tile.dragging {
  opacity: 0.5;
  cursor: grabbing;
}
.rack-tile.rack-drag-over {
  border-left: 3px solid var(--accent);
  transform: translateX(3px);
}
.rack-tile .tile-letter {
  font-size: 45cqi;
  font-weight: 800;
  color: var(--tile-text);
}
.rack-tile .tile-points {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 18cqi;
  font-weight: 700;
  color: var(--tile-text);
  opacity: 0.7;
}
.rack-tile.blank-tile {
  background: #c9a83a;
}

.rack-tile.blank-tile .tile-letter {
  color: #e74c3c;
  font-style: italic;
}

.rack-slot-empty {
  width: 48px;
  height: 48px;
  border: 2px dashed rgba(255,255,255,0.1);
  border-radius: 6px;
}

.tile-rack.rack-drop-target {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  background: rgba(26, 188, 156, 0.1);
}

/* ============================================
   Game Actions
   ============================================ */
.game-actions {
  display: flex;
  gap: 0.6rem;
  flex-shrink: 0;
  user-select: none;
  -webkit-user-select: none;
}

/* In landscape / desktop, the wrapper is transparent (children act as
   direct flex children of .left-panel). In portrait, it becomes a real
   sticky element — see the portrait media query below. */
.portrait-bottom-bar {
  display: contents;
}
.game-actions .btn {
  flex: 1;
  text-align: center;
}

.btn-icon {
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
}
.btn .btn-text {
  display: inline;
}
.btn.icon-only .btn-text {
  display: none;
}
.btn.icon-only {
  padding: 6px 10px;
  min-width: unset;
}

/* ============================================
   Game Info Card
   ============================================ */
.game-info-card {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 12px 16px;
}
.info-text {
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.4;
}

/* ============================================
   Player Scoreboard
   ============================================ */
.player-scoreboard {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.player-card {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 12px 16px;
  flex: 1;
  min-width: 140px;
  border: 2px solid transparent;
  transition: border-color 0.3s;
}
.player-card.active {
  border-color: var(--text-primary);
  background: #33334d;
}
.player-card.player-card-self {
  border-left: 3px solid var(--accent);
  background: rgba(99, 102, 241, 0.08);
}

.player-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.player-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--bg-primary);
  flex-shrink: 0;
  overflow: hidden;
}
.player-avatar img {
  width: 100%; height: 100%;
  object-fit: cover;
}
.player-info {
  flex: 1;
  min-width: 0;
}
.player-name {
  font-size: 0.9rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.verified-badge {
  display: inline-block;
  color: #4caf50;
  font-size: 0.75em;
  font-weight: 800;
  vertical-align: middle;
}
.deleted-badge {
  display: inline-block;
  font-size: 0.7em;
  vertical-align: middle;
  opacity: 0.7;
}
.player-elo {
  font-size: 0.7rem;
  color: var(--text-muted);
}
.player-heart {
  color: var(--danger);
  font-size: 0.8rem;
}

.player-card-body {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid rgba(255,255,255,0.1);
  padding-top: 8px;
}
.player-score {
  font-size: 2.2rem;
  font-weight: 800;
  line-height: 1;
}
.player-timer-container {
  display: flex;
  align-items: center;
  gap: 6px;
}
.player-timer {
  font-size: 0.95rem;
  color: var(--text-muted);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.trophy-icon {
  font-size: 1.2rem;
}

@keyframes scoreFlash {
  0% { color: var(--success); transform: scale(1.1); }
  100% { color: var(--text-primary); transform: scale(1); }
}
.score-flash {
  animation: scoreFlash 0.5s ease-out;
}
.score-delta {
  position: absolute;
  top: -8px;
  right: 8px;
  background: var(--success);
  color: white;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 10px;
  animation: scoreDeltaIn 0.3s ease-out;
  transition: opacity 0.5s;
}
.score-delta-fade {
  opacity: 0;
}
@keyframes scoreDeltaIn {
  from { transform: translateY(10px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.player-card-body {
  position: relative;
}

@keyframes activePulse {
  0%, 100% { border-color: var(--text-primary); }
  50% { border-color: var(--accent); }
}
.player-card.active {
  animation: activePulse 2s ease-in-out infinite;
}
.player-card.player-card-self .player-name {
  font-weight: 800;
}

/* Disabled submit button when not player's turn */
#submit-btn.btn-disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}
#hint-btn {
  padding: 0.4rem 0.6rem;
  line-height: 1;
}
@media (max-width: 600px) and (orientation: portrait) {
  #hint-btn .hint-label { display: none; }
  #hint-btn { min-width: 2.5rem; font-size: 1.1rem; }
}
.hint-spinner {
  display: inline-block;
  width: 1em;
  height: 1em;
  border: 2px solid var(--text-muted);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: hint-spin 0.6s linear infinite;
}
@keyframes hint-spin {
  to { transform: rotate(360deg); }
}

/* ============================================
   Tile Bag Info
   ============================================ */
.tile-bag-info {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 10px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.bag-count {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
}
.bag-icon {
  font-size: 1.1rem;
}
.bag-number {
  color: var(--text-primary);
  font-weight: 700;
}
.rack-counts {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  color: var(--text-muted);
}
.rack-count-item {
  display: flex;
  align-items: center;
  gap: 4px;
}
.rack-count-avatar {
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--bg-primary);
}

/* ============================================
   Chat & Turn History
   ============================================ */
.chat-history-panel {
  background: var(--bg-secondary);
  border-radius: 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 300px;
}

.tab-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  gap: 12px;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.tab-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
  font-family: var(--font-family);
  transition: color 0.15s;
}
.tab-btn.active {
  color: var(--text-primary);
}
.tab-btn.chat-notify {
  animation: chat-pulse 0.6s ease-in-out 3;
  color: var(--accent);
}
@keyframes chat-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

.toggle-switch {
  position: relative;
  width: 44px;
  height: 24px;
}
.toggle-input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-label {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--accent);
  border-radius: 12px;
  transition: background 0.3s;
}
.toggle-label::after {
  content: '';
  position: absolute;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: white;
  top: 3px; left: 3px;
  transition: transform 0.3s;
}
.toggle-input:checked + .toggle-label::after {
  transform: translateX(20px);
}

.tab-panel {
  display: none;
  flex: 1;
  overflow: hidden;
  flex-direction: column;
}
.tab-panel.active {
  display: flex;
}

/* Chat */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-message {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}
.chat-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--bg-primary);
  flex-shrink: 0;
}
.chat-body {
  flex: 1;
}
.chat-header {
  display: flex;
  gap: 8px;
  align-items: baseline;
}
.chat-username {
  font-size: 0.8rem;
  font-weight: 700;
}
.chat-time {
  font-size: 0.7rem;
  color: var(--text-muted);
}
.chat-text {
  font-size: 0.85rem;
  color: rgba(255,255,255,0.85);
  margin-top: 2px;
}

.chat-input-area {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid rgba(255,255,255,0.05);
}
.chat-input-area input {
  flex: 1;
}

/* Turn History */
.turn-history {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-entry {
  background: var(--bg-tertiary);
  border-radius: 8px;
  padding: 10px 12px;
}
.history-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.history-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--bg-primary);
  flex-shrink: 0;
}
.history-info {
  flex: 1;
}
.history-player {
  font-size: 0.85rem;
  font-weight: 700;
}
.history-detail {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.3;
}
.history-tiles {
  display: flex;
  gap: 3px;
  flex-wrap: wrap;
  margin-top: 6px;
}
.history-tile {
  width: 28px; height: 30px;
  background: var(--tile-color);
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.history-tile-existing {
  background: #6a6a7e;
}
.history-tile-existing .tile-letter {
  color: #ddd;
}
.history-tile-existing .tile-points {
  color: #bbb;
}
.history-tile-premium-tw {
  background: var(--tw-color);
}
.history-tile-premium-tw .tile-letter,
.history-tile-premium-tw .tile-points {
  color: white;
}
.history-tile-premium-dw {
  background: var(--dw-color);
}
.history-tile-premium-dw .tile-letter,
.history-tile-premium-dw .tile-points {
  color: white;
}
.history-tile-premium-tl {
  background: var(--tl-color);
}
.history-tile-premium-tl .tile-letter,
.history-tile-premium-tl .tile-points {
  color: white;
}
.history-tile-premium-dl {
  background: var(--dl-color);
}
.history-tile-premium-dl .tile-letter,
.history-tile-premium-dl .tile-points {
  color: white;
}
.history-tile .tile-letter {
  font-size: 13px;
  font-weight: 800;
  color: var(--tile-text);
}
.history-tile .tile-points {
  position: absolute;
  bottom: 1px;
  right: 2px;
  font-size: 7px;
  font-weight: 700;
  color: var(--tile-text);
  opacity: 0.7;
}

/* ============================================
   Exchange Modal Rack
   ============================================ */
.exchange-rack {
  display: flex;
  gap: 6px;
  justify-content: center;
  flex-wrap: nowrap;
  margin: 1rem 0;
}
.exchange-tile {
  width: 44px; height: 48px;
  background: var(--tile-color);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s, transform 0.1s;
  flex-shrink: 0;
}
.exchange-tile.selected {
  border-color: var(--accent);
  transform: translateY(-4px);
}
.exchange-tile .tile-letter {
  font-size: 22px;
  font-weight: 800;
  color: var(--tile-text);
}
.exchange-tile .tile-points {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 9px;
  font-weight: 700;
  color: var(--tile-text);
  opacity: 0.7;
}

/* ============================================
   Blank Tile Letter Grid
   ============================================ */
.blank-letter-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  margin-top: 1rem;
}
.blank-letter-btn {
  padding: 10px;
  background: var(--bg-tertiary);
  border: 1px solid var(--cell-border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  font-family: var(--font-family);
  transition: background 0.15s, border-color 0.15s;
}
.blank-letter-btn:hover {
  background: var(--accent);
  color: var(--bg-primary);
  border-color: var(--accent);
}

/* ============================================
   Summary Modal
   ============================================ */
.summary-content {
  max-height: 500px;
  overflow-y: auto;
  overflow-x: auto;
}
.summary-table {
  width: 100%;
  border-collapse: collapse;
  text-align: center;
}
.summary-table th,
.summary-table td {
  padding: 8px 10px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
.stat-label-col {
  width: 1%;
  white-space: nowrap;
}
.stat-player-col {
  text-align: center;
  padding-bottom: 12px !important;
}
.stat-player-col .player-avatar {
  margin: 0 auto 4px;
}
.stat-player-col .player-name {
  font-size: 0.85rem;
}
.stat-player-col.winner {
  background: rgba(72, 187, 120, 0.08);
  border-top: 3px solid var(--success);
  border-radius: 8px 8px 0 0;
}
.stat-label-cell {
  text-align: left;
  color: var(--text-muted);
  font-size: 0.78rem;
  white-space: nowrap;
  padding-right: 16px;
}
.stat-value-cell {
  font-weight: 600;
  font-size: 0.82rem;
  text-transform: uppercase;
  color: var(--text-primary);
}
.stat-value-cell.winner {
  background: rgba(72, 187, 120, 0.05);
}
.stat-pts {
  color: var(--accent);
  font-size: 0.72rem;
}
.stat-sub {
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 400;
}
.summary-table .summary-score {
  font-size: 1.6rem;
  font-weight: 800;
}
.summary-player-info {
  display: flex;
  align-items: center;
  gap: 10px;
}
/* Overall Game Summary */
.game-overall-summary {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid rgba(255,255,255,0.1);
}
.game-overall-summary h3 {
  font-size: 0.95rem;
  margin-bottom: 10px;
  color: var(--text-primary);
}
.overall-stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}
.overall-highlights-boxes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}
.highlight-stat {
  border: 1px solid rgba(255,255,255,0.1);
}
.highlight-stat .overall-stat-val {
  font-size: 0.95rem;
  letter-spacing: 1px;
}
.overall-stat-by {
  font-size: 0.6rem;
  color: var(--text-muted);
  font-style: italic;
  margin-top: 2px;
}
.overall-stat {
  background: var(--bg-tertiary);
  border-radius: 6px;
  padding: 8px;
  text-align: center;
  display: flex;
  flex-direction: column;
}
.overall-stat-val {
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--accent);
}
.overall-stat-lbl {
  font-size: 0.65rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.overall-highlights {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.overall-highlight {
  font-size: 0.78rem;
  color: var(--text-secondary);
  padding: 3px 0;
}
/* Score Progression Graph */
.score-graph-section {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid rgba(255,255,255,0.1);
}
.score-graph-section h3 {
  font-size: 0.95rem;
  margin-bottom: 8px;
  color: var(--text-primary);
}
.score-graph-canvas {
  width: 100%;
  max-width: 600px;
  display: block;
  margin: 0 auto;
}
.score-graph-legend {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.75rem;
  color: var(--text-secondary);
}
.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

/* ============================================
   Utility
   ============================================ */
.hidden { display: none !important; }
.text-muted { color: var(--text-muted); }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); }

/* History Words */
.history-words {
  margin-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.history-word-item {
  background: rgba(255,255,255,0.04);
  border-radius: 4px;
  padding: 4px 8px;
}
.history-word-text {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 1px;
}
.history-word-score {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-left: 8px;
}
.history-word-def {
  font-size: 0.72rem;
  color: rgba(255,255,255,0.5);
  margin-top: 2px;
  line-height: 1.3;
  font-style: italic;
}

.word-def-source {
  font-size: 0.65rem;
  color: rgba(255,255,255,0.3);
  font-style: normal;
}

/* ============================================
   Mobile / Phone Layout (≤ 768px)
   ============================================ */
@media (max-width: 768px) {
  /* Stack panels vertically; constrain total height to viewport */
  .game-layout {
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* dynamic viewport height — accounts for browser UI */
    overflow: hidden;
  }

  .left-panel {
    display: flex;
    flex-direction: column;
    flex: 1 1 0;
    min-height: 0;
    max-width: 100%;
    width: 100%;
    padding: 0.5rem;
    gap: 0.5rem;
    overflow: hidden;
  }

  .right-panel {
    flex-shrink: 0;
    max-width: 100%;
    width: 100%;
    padding: 0.5rem;
    /* Cap height so it never pushes board + rack + buttons off screen */
    max-height: 35vh;
    overflow-y: auto;
  }

  /* Board wrapper — natural height so the board can overflow and be pannable */
  .board-wrapper {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    overflow: visible;
    max-width: 100%;
    margin: auto;
    transform-origin: 0 0;
    will-change: transform;
  }

  /* board-body — natural height (grid determines size via aspect-ratio) */
  .board-body {
    flex: none;
  }

  /* Board container — scrollable when board overflows vertically */
  .board-container {
    flex: 1 1 0;
    min-height: 0;
    min-width: 0;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    overflow: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
  .board-container::-webkit-scrollbar {
    display: none;
  }

  /* Zoom state marker (transform itself is set by board-zoom.js) */
  .board-wrapper.board-zoomed {
    /* transform applied dynamically via inline style */
  }

  /* Rack tiles: fixed square size that adapts to screen width */
  .rack-tile,
  .rack-slot-empty {
    width: min(44px, calc((100vw - 32px) / 7 - 4px));
    height: min(44px, calc((100vw - 32px) / 7 - 4px));
    flex-shrink: 0;
  }
  .rack-tile .tile-letter {
    font-size: 45cqi;
  }

  .tile-rack {
    display: flex;
    flex: 0 0 auto;
    flex-wrap: nowrap;
    gap: 4px;
    justify-content: center;
    overflow: visible;
  }

  /* Board grid — full width, square; overflows container if constrained */
  #board-grid {
    max-width: 100%;
    aspect-ratio: 1;
    height: auto;
  }

  /* Rack must never shrink — let the board shrink instead */
  .rack-container {
    flex-shrink: 0;
    flex-grow: 0;
    width: 100%;
    box-sizing: border-box;
    overflow: hidden;
    align-items: center;
  }
  .game-actions {
    flex-shrink: 0;
    flex-wrap: wrap;
  }
  .game-actions .btn {
    font-size: 1rem;
    padding: 0.55rem 0.75rem;
  }

  /* Cap the chat/history panel height so it doesn't dominate the page */
  .chat-history-panel {
    min-height: 200px;
    max-height: 350px;
  }

  /* When buttons are in the rack row, rack container uses row layout */
  .rack-container {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
  }

  /* Game-actions row — when buttons are added, keep them compact */
  .game-actions {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 6px;
  }

  /* Shuffle/sort buttons inside game-actions get compact styling */
  #game-actions .rack-btn {
    width: 44px;
    height: 44px;
    padding: 10px;
  }

  /* All icon-only buttons in game-actions match rack-btn size */
  #game-actions .btn.icon-only {
    width: 44px;
    height: 44px;
    padding: 10px;
    min-width: unset;
    flex-grow: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
    border: none;
    border-radius: 8px;
    color: var(--text-muted);
    transition: color 0.15s, background 0.15s;
  }
  /* Submit is always largest */
  #submit-btn {
    flex-grow: 1;
    min-width: 60px;
  }

  .rack-utility-btns:empty {
    display: none;
  }

  .player-score {
    font-size: 1.6rem;
  }

}

/* Score Hint */
.score-hint {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 20px;
  height: 18px;
  border-radius: 9px;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 0 4px;
  line-height: 1;
  pointer-events: none;
  animation: hintPop 0.2s ease-out;
  transform-origin: top right;
  scale: calc(1 / var(--zoom-scale, 1));
}
.score-hint-valid {
  background: var(--success);
  color: white;
}
.score-hint-invalid {
  background: var(--danger);
  color: white;
}
@keyframes hintPop {
  from { transform: scale(0.5); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
.board-score-indicator {
  position: absolute;
  top: -12px;
  right: -8px;
  background: var(--success);
  color: white;
  font-size: 0.7rem;
  font-weight: 800;
  padding: 2px 6px;
  border-radius: 8px;
  z-index: 20;
  pointer-events: none;
  animation: scoreDeltaIn 0.3s ease-out;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
  transition: opacity 0.5s;
}
.board-score-indicator.fade-out {
  opacity: 0;
}

/* (Generic :fullscreen layout overrides removed — media queries handle all cases.) */

/* ============================================
   Portrait layout for wider viewports (tablets, desktop windows)
   Triggers when the viewport height >= width and width > 768px.
   The max-width: 768px query above handles narrow phones.
   ============================================ */
@media (orientation: portrait) and (min-width: 769px) {
  .game-layout {
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
  }

  .left-panel {
    display: flex;
    flex-direction: column;
    max-width: 100%;
    width: 100%;
  }

  .right-panel {
    flex-shrink: 0;
    max-width: 100%;
    width: 100%;
    overflow-y: auto;
  }

  .board-container {
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .board-container::-webkit-scrollbar {
    display: none;
  }

  .rack-container {
    flex-shrink: 0;
  }

  .game-actions {
    flex-shrink: 0;
  }
}

/* ============================================
   Portrait: full-width board with sticky rack/buttons overlay
   Applies to ALL portrait viewports (phones + tablets + desktop).
   Board always renders at 100% container width (square via
   aspect-ratio on .board-grid). When the viewport is too short
   for board + rack + buttons, the rack/buttons stick to the bottom
   and overlay the board, which is pannable underneath.
   ============================================ */
@media (orientation: portrait) {
  /* Make the game-layout scrollable so the board can be panned
     by scrolling, while the portrait-bottom-bar sticks at the bottom. */
  .game-layout {
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
  .game-layout::-webkit-scrollbar {
    display: none;
  }

  /* Left panel: natural height (no flex-shrink/grow), let the board
     determine its height. No overflow clipping. */
  .left-panel {
    flex: none;
    overflow: visible;
  }

  /* Board container: natural height, no internal scrolling.
     Scrolling/panning now happens on .game-layout. */
  .board-container {
    flex: none;
    overflow: visible;
  }

  /* Remove the 680px cap so the board is truly full-width */
  .board-wrapper {
    max-width: 100%;
  }

  /* Sticky wrapper: rack + buttons stick at the viewport bottom
     when the board pushes them out of view. */
  .portrait-bottom-bar {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    position: sticky;
    bottom: 0;
    z-index: 100;
    background: var(--bg-primary);
    padding-top: 0.4rem;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.45);
  }

  /* Right panel: remove height cap; it lives below the fold. */
  .right-panel {
    max-height: none;
  }
}

/* Reconnecting overlay */
.reconnect-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.reconnect-message {
  background: var(--surface);
  color: var(--text-primary);
  padding: 20px 32px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 1rem;
  font-weight: 600;
  box-shadow: 0 4px 24px rgba(0,0,0,0.3);
}
.reconnect-spinner {
  width: 22px; height: 22px;
  border: 3px solid var(--text-muted);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ============================================
   Stats Screen
   ============================================ */
.stats-container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
  min-height: 100vh;
}

.stats-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 2rem;
}

.stats-title {
  font-size: 1.8rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--tile-color), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.stats-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 4rem 0;
  color: var(--text-muted);
}

.stats-section {
  margin-bottom: 2rem;
}

.stats-section-title {
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 0.8rem;
  color: var(--text-primary);
}

.stats-summary-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.8rem;
}

.stats-stat-card {
  background: var(--bg-secondary);
  border-radius: 10px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
}

.stats-stat-val {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--text-primary);
}

.stats-stat-lbl {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 600;
  text-align: center;
}

.stats-val-win { color: var(--success); }
.stats-val-loss { color: var(--danger); }

/* Stats tables */
.stats-table-wrap {
  overflow-x: auto;
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-secondary);
  border-radius: 10px;
  overflow: hidden;
}

.stats-table th,
.stats-table td {
  padding: 0.6rem 0.8rem;
  text-align: left;
  font-size: 0.88rem;
}

.stats-table thead th {
  background: var(--bg-tertiary);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.stats-table tbody tr {
  border-top: 1px solid var(--cell-border);
}

.stats-game-row {
  cursor: pointer;
  transition: background 0.15s;
}

.stats-game-row:hover {
  background: var(--bg-tertiary);
}
.stats-reason-cell {
  position: relative;
}
.stats-reason-cell .delete-game-btn {
  margin-left: 0.5rem;
  padding: 0.1rem 0.4rem;
  font-size: 0.75rem;
  line-height: 1;
  vertical-align: middle;
  opacity: 0;
  transition: opacity 0.15s;
}
.stats-game-row:hover .delete-game-btn {
  opacity: 1;
}

.stats-table-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem 1rem !important;
}

.stats-result-win {
  color: var(--success);
  font-weight: 700;
}

.stats-result-loss {
  color: var(--danger);
  font-weight: 700;
}

.stats-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  margin-top: 0.8rem;
}

.stats-page-info {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.stats-error {
  color: var(--danger);
  text-align: center;
  padding: 1rem;
}

/* Game Detail Modal */
.stats-detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.stats-detail-date {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.stats-detail-reason {
  font-size: 0.8rem;
  color: var(--text-muted);
  background: var(--bg-tertiary);
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
}

.stats-detail-players {
  display: flex;
  gap: 0.8rem;
  margin-bottom: 1.2rem;
  flex-wrap: wrap;
}

.stats-detail-player {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--bg-tertiary);
  padding: 0.5rem 0.8rem;
  border-radius: 8px;
  flex: 1;
  min-width: 120px;
}

.stats-detail-player.winner {
  border: 1px solid var(--accent);
}

.stats-detail-player-name {
  font-size: 0.9rem;
  font-weight: 600;
  flex: 1;
}

.stats-detail-player-score {
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--accent);
}

.stats-detail-table {
  margin-bottom: 1rem;
}

.stats-detail-table th,
.stats-detail-table td {
  text-align: center;
}

.stats-detail-table td:first-child,
.stats-detail-table th:first-child {
  text-align: left;
}

.stats-detail-summary {
  display: flex;
  gap: 1.5rem;
  color: var(--text-muted);
  font-size: 0.85rem;
}

@media (max-width: 600px) {
  /* Board tile font — phones only (tablets keep the larger global size) */
  .board-tile .tile-letter {
    font-size: 14px;
  }
  .board-tile .tile-points {
    font-size: 6px;
  }

  .stats-container {
    padding: 1rem;
  }
  .stats-summary-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  .stats-title {
    font-size: 1.3rem;
  }
}

/* ============================================
   Game Detail Modal — Tabs & Board
   ============================================ */
.modal-content.modal-extra-wide {
  max-width: 860px;
}

.detail-loading-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 3rem 0;
  color: var(--text-muted);
}
.detail-loading-spinner .spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--text-muted);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.detail-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.detail-modal-header h2 {
  margin-bottom: 0;
}

.detail-tabs {
  display: flex;
  gap: 0.4rem;
}

.detail-tab-btn {
  background: var(--bg-tertiary);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0.35rem 0.9rem;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  font-family: var(--font-family);
}

.detail-tab-btn.active {
  background: var(--accent);
  color: var(--bg-primary);
  border-color: var(--accent);
}

.detail-tab-btn:hover:not(.active) {
  background: rgba(255,255,255,0.08);
  color: var(--text-primary);
}

/* Detail summary panel reuses existing summary-table, game-overall-summary,
   score-graph-section classes from the in-game modal */
.detail-summary-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.detail-summary-date {
  color: var(--text-muted);
  font-size: 0.88rem;
}

.detail-summary-reason {
  font-size: 0.78rem;
  color: var(--text-muted);
  background: var(--bg-tertiary);
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
}

.detail-summary-content {
  max-height: 65vh;
  overflow-y: auto;
  overflow-x: auto;
}

/* Board replay panel */
.detail-board-panel-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.8rem;
}

.detail-board-wrap {
  width: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.detail-board-with-headers {
  display: inline-flex;
  flex-direction: column;
  min-width: min(560px, 100%);
  width: min(560px, 100%);
  margin: 0 auto;
}

.detail-board-col-headers {
  display: flex;
  margin-bottom: 2px;
  padding-left: 18px;
}

.detail-board-col-header {
  flex: 1;
  text-align: center;
  font-size: 9px;
  font-weight: 700;
  color: var(--text-muted);
}

.detail-board-body {
  display: flex;
}

.detail-board-row-headers {
  display: flex;
  flex-direction: column;
  width: 18px;
  flex-shrink: 0;
}

.detail-board-row-header {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 700;
  color: var(--text-muted);
  aspect-ratio: 1;
}

.detail-board-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  gap: 2px;
  background: var(--cell-border);
  border: 2px solid var(--cell-border);
  border-radius: 4px;
  aspect-ratio: 1;
}

.detail-board-cell {
  background: var(--cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  font-size: 8px;
  font-weight: 700;
  user-select: none;
  aspect-ratio: 1;
  border-radius: 2px;
}

.detail-board-cell.premium-tw { background: var(--tw-color); color: rgba(255,255,255,0.9); }
.detail-board-cell.premium-dw { background: var(--dw-color); color: rgba(255,255,255,0.9); }
.detail-board-cell.premium-tl { background: var(--tl-color); color: rgba(255,255,255,0.9); }
.detail-board-cell.premium-dl { background: var(--dl-color); color: rgba(255,255,255,0.9); }
.detail-board-cell.center-star { font-size: 13px; }

.detail-board-cell .detail-premium-label {
  font-size: 6px;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  opacity: 0.9;
}

.detail-board-tile {
  position: absolute;
  inset: 1px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}

.detail-board-tile .tile-letter {
  font-size: 13px;
  font-weight: 800;
  color: rgba(0,0,0,0.85);
  line-height: 1;
}

.detail-board-tile .tile-points {
  position: absolute;
  bottom: 1px;
  right: 2px;
  font-size: 5px;
  font-weight: 700;
  color: rgba(0,0,0,0.65);
}

.detail-board-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.detail-board-legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.76rem;
  color: var(--text-secondary);
}

.detail-board-legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
  flex-shrink: 0;
}

/* Player chips for click-to-highlight on detail board */
.detail-player-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-top: 0.75rem;
}

.detail-player-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 5px 12px 5px 6px;
  border-radius: 999px;
  border: 2px solid var(--chip-color, rgba(255,255,255,0.2));
  background: rgba(255,255,255,0.05);
  color: var(--text-secondary);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
}

.detail-player-chip:hover {
  background: rgba(255,255,255,0.1);
  color: var(--text-primary);
}

.detail-player-chip.active {
  background: color-mix(in srgb, var(--chip-color, #fff) 20%, transparent);
  color: var(--text-primary);
  box-shadow: 0 0 0 1px var(--chip-color, rgba(255,255,255,0.4));
}

.detail-player-chip-avatar {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 800;
  color: rgba(0,0,0,0.85);
  flex-shrink: 0;
}

.detail-player-chip-score {
  color: var(--text-muted);
  font-weight: 400;
}

/* Tile highlight on detail board */
.detail-board-tile.detail-tile-highlighted {
  box-shadow: 0 0 0 2px var(--tile-new-glow), 0 0 8px 2px rgba(232,200,74,0.6);
  filter: brightness(1.15);
  z-index: 3;
}

.detail-board-tile.detail-tile-dimmed {
  opacity: 0.35;
}

/* Blank tile styling in detail board */
.detail-board-tile.detail-blank-tile .tile-letter {
  color: #e74c3c !important;
  font-style: italic;
}

/* Detail board: sidebar + board row layout */
.detail-board-with-sidebar {
  display: flex;
  flex-direction: row;
  gap: 0.75rem;
  width: 100%;
  align-items: flex-start;
}

/* Turn history sidebar */
.detail-turn-history {
  width: 180px;
  flex-shrink: 0;
  max-height: 520px;
  overflow-y: auto;
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 6px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.15) transparent;
}

.detail-turn-history-title {
  padding: 6px 10px 5px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  border-bottom: 1px solid rgba(255,255,255,0.08);
  position: sticky;
  top: 0;
  background: var(--modal-bg, #1a1a2e);
  z-index: 1;
}

.detail-turn-history-empty {
  padding: 10px;
  font-size: 0.72rem;
  color: var(--text-muted);
  text-align: center;
}

.detail-turn-history-item {
  padding: 5px 8px;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.detail-turn-history-item:last-child { border-bottom: none; }

.detail-turn-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 4px;
  margin-bottom: 3px;
}

.detail-turn-num {
  font-size: 0.62rem;
  color: var(--text-muted);
  font-weight: 700;
  flex-shrink: 0;
}

.detail-turn-player {
  font-weight: 700;
  font-size: 0.68rem;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-turn-score {
  font-weight: 700;
  font-size: 0.68rem;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.detail-turn-words {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
}

.detail-turn-word {
  background: rgba(255,255,255,0.07);
  border-radius: 3px;
  padding: 1px 4px;
  font-size: 0.62rem;
  color: var(--text-primary);
}

.detail-turn-word-pts {
  color: #e8c84a;
  font-weight: 700;
}

.detail-turn-action-pass,
.detail-turn-action-exchange {
  font-size: 0.63rem;
  color: var(--text-muted);
  font-style: italic;
}

/* Highlighted turn history entry when player chip is active */
.detail-turn-history-item.turn-selected {
  border-left: 3px solid var(--turn-selected-color, rgba(255,255,255,0.4));
  padding-left: 5px;
  background: color-mix(in srgb, var(--turn-selected-color, #fff) 10%, transparent);
}

/* ============================================
   Past Game Detail Modal — Mobile layout
   ============================================ */
@media (max-width: 768px) {
  /* Make the modal full-width on phones */
  .modal-content.modal-extra-wide {
    max-width: 100%;
    width: 100%;
    min-width: 0;
    margin: 0;
    border-radius: 0;
  }

  /* Stack board + sidebar vertically */
  .detail-board-with-sidebar {
    flex-direction: column;
    align-items: stretch;
  }

  /* Board takes full container width */
  .detail-board-wrap {
    width: 100%;
  }

  /* Board scales to fit phone width (~15 cols × cell + gap + 18px header) */
  .detail-board-with-headers {
    min-width: 0;
    width: 100%;
  }

  /* Move turn history below the board with a capped height */
  .detail-turn-history {
    width: 100%;
    max-height: 320px;
    flex-shrink: unset;
  }
}

/* ============================================
   Fullscreen Toggle Button
   ============================================ */
#game-screen {
  position: relative;
}

.fullscreen-btn {
  position: absolute;
  top: 10px;
  right: 12px;
  z-index: 100;
  width: 34px;
  height: 34px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  color: var(--text-primary);
  cursor: pointer;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: background 0.15s;
}

.fullscreen-btn:hover {
  background: rgba(255, 255, 255, 0.22);
}

.fullscreen-btn svg {
  width: 18px;
  height: 18px;
  pointer-events: none;
}

/* ============================================
   Fullscreen Layout Fix
   Only constrains height/overflow — does NOT change flex-direction.
   The orientation / width media queries above handle portrait vs
   landscape identically in fullscreen and normal modes.
   ============================================ */
#game-screen:fullscreen,
#game-screen:-webkit-full-screen,
#game-screen:-moz-full-screen {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background: var(--bg-primary);
}

#game-screen:fullscreen .game-layout,
#game-screen:-webkit-full-screen .game-layout,
#game-screen:-moz-full-screen .game-layout {
  height: 100vh;
  min-height: unset;
}

#game-screen:fullscreen .board-container,
#game-screen:-webkit-full-screen .board-container,
#game-screen:-moz-full-screen .board-container {
  min-height: 0;
}

#game-screen:fullscreen .right-panel,
#game-screen:-webkit-full-screen .right-panel,
#game-screen:-moz-full-screen .right-panel {
  max-height: 100vh;
}

/* ============================================
   Landscape phone layout
   Targets phones rotated sideways: tall-but-narrow
   browsers where height < 600px in landscape mode.
   ============================================ */
@media (orientation: landscape) and (max-height: 600px) {

  /* Two-column layout: board column on left, info/scoreboard on right */
  .game-layout {
    flex-direction: row;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  /* Left panel: board + rack + buttons stacked vertically.
     flex: 0 0 auto — width is driven by the square board's intrinsic size. */
  .left-panel {
    flex: 0 0 auto;
    width: auto;
    max-width: none;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    padding: 0.4rem;
    gap: 0.4rem;
    overflow: hidden;
  }

  /* Board container: grows to fill available height after rack + buttons */
  .board-container {
    flex: 1 1 0;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Board wrapper: fit-content width keeps legends aligned with the grid
     when height constrains the grid to be narrower than available width. */
  .board-wrapper {
    width: fit-content;
    max-width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transform-origin: 0 0;
    will-change: transform;
  }

  /* board-body: fills the space below the column-header row */
  .board-body {
    flex: 1;
    min-height: 0;
    display: flex;
  }

  /* board-grid: square that fits within the container.
     flex: none + height: 100% + aspect-ratio: 1 → width = height,
     so the grid is sized by the available height. */
  #board-grid {
    flex: none;
    max-width: 100%;
    height: 100%;
    aspect-ratio: 1;
    width: auto;
  }

  /* Row headers: match the grid height exactly */
  .board-row-headers {
    height: 100%;
  }

  /* Rack: never shrink, always visible */
  .rack-container {
    flex-shrink: 0;
    width: 100%;
    box-sizing: border-box;
    overflow: hidden;
  }

  .tile-rack {
    flex-wrap: nowrap;
    overflow: hidden;
    gap: 3px;
  }

  /* Rack tiles: fixed square size, adapted to screen */
  .rack-tile,
  .rack-slot-empty {
    width: min(44px, calc((100vw - 32px) / 7 - 4px));
    height: min(44px, calc((100vw - 32px) / 7 - 4px));
    flex-shrink: 0;
  }

  .rack-tile .tile-letter {
    font-size: 45cqi;
  }

  .rack-utility-btns {
    flex-direction: row;
    gap: 6px;
  }

  .rack-utility-btns .rack-btn {
    width: 30px;
    height: 30px;
    padding: 4px;
  }

  /* Action buttons: never shrink, compact padding */
  .game-actions {
    flex-shrink: 0;
    flex-wrap: nowrap;
  }

  .game-actions .btn {
    font-size: 0.72rem;
    padding: 0.35rem 0.4rem;
  }

  /* Right panel: fills remaining viewport width, full height, scrollable */
  .right-panel {
    flex: 1 1 0;
    min-width: 0;
    height: 100vh;
    height: 100dvh;
    max-height: none;
    padding: 0.4rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }

  /* Chat / turn-history panel: fills remaining vertical space in right panel */
  .chat-history-panel {
    flex: 1 1 0;
    min-height: 100px;
    max-height: none;
  }
}

/* ============================================
   Advanced Settings / Danger Zone
   ============================================ */
.btn-advanced-settings {
  display: inline-block;
  margin-top: 2rem;
  padding: 0.4rem 0.9rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid var(--text-muted);
  border-radius: 6px;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.2s;
}
.btn-advanced-settings:hover {
  opacity: 1;
}
.danger-zone {
  border: 1px solid var(--danger);
  border-radius: 10px;
  padding: 1.5rem;
  margin-top: 2rem;
}
.danger-zone-title {
  color: var(--danger) !important;
}
.danger-zone-items {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.danger-zone-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem;
  background: rgba(231, 76, 60, 0.06);
  border-radius: 8px;
  border: 1px solid rgba(231, 76, 60, 0.15);
}
.danger-zone-item-info {
  flex: 1;
}
.danger-zone-item-info strong {
  display: block;
  margin-bottom: 0.25rem;
  font-size: 1rem;
}
.danger-zone-item-info p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

/* Danger Confirmation Modal */
.danger-modal-content {
  border: 1px solid var(--danger);
}
.danger-modal-content h2 {
  color: var(--danger);
}
.danger-confirm-msg {
  color: var(--text-muted);
  margin-bottom: 1rem;
  line-height: 1.5;
}
.danger-confirm-input-wrap {
  margin-bottom: 0.5rem;
}
.danger-confirm-input-wrap label {
  display: block;
  margin-bottom: 0.4rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}
.danger-confirm-input {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--text-muted);
  border-radius: 6px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.95rem;
  font-family: var(--font-family);
  box-sizing: border-box;
}
.danger-confirm-input:focus {
  outline: none;
  border-color: var(--danger);
}
.danger-confirm-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin-top: 0.25rem;
}
.danger-confirm-error.hidden {
  display: none;
}

@media (max-width: 600px) {
  .danger-zone-item {
    flex-direction: column;
    align-items: stretch;
  }
  .danger-zone-item .btn {
    align-self: flex-end;
  }
}

