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

:root {
  --green: #6aaa64;
  --yellow: #c9b458;
  --gray: #787c7e;
  --dark-gray: #3a3a3c;
  --light-gray: #d3d6da;
  --bg: #121213;
  --tile-bg: #3a3a3c;
  --text: #ffffff;
  --key-bg: #818384;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

header {
  width: 100%;
  max-width: 600px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--dark-gray);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: 2px;
}

.score-display {
  font-size: 0.9rem;
  color: var(--light-gray);
}

.score-display .score-value {
  font-weight: 700;
  color: var(--green);
  font-size: 1.1rem;
}

/* --- Screens --- */
.screen {
  display: none;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 600px;
  padding: 20px;
}

.screen.active {
  display: flex;
}

/* Lobby */
#lobby {
  gap: 20px;
  text-align: center;
}

#lobby h2 {
  font-size: 1.3rem;
  margin-bottom: 10px;
}

.lobby-section {
  background: #1a1a1b;
  border: 1px solid var(--dark-gray);
  border-radius: 8px;
  padding: 20px;
  width: 100%;
}

.lobby-section h3 {
  margin-bottom: 12px;
  color: var(--green);
}

.btn {
  background: var(--green);
  color: white;
  border: none;
  border-radius: 6px;
  padding: 12px 24px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

.btn:hover {
  opacity: 0.85;
}

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

.btn-secondary {
  background: var(--gray);
}

input[type="text"] {
  background: var(--dark-gray);
  border: 1px solid var(--gray);
  border-radius: 6px;
  padding: 10px 14px;
  color: white;
  font-size: 1rem;
  width: 100%;
  max-width: 300px;
  text-align: center;
  font-family: monospace;
}

input[type="text"]::placeholder {
  color: #666;
}

.peer-id-display {
  font-family: monospace;
  font-size: 1.2rem;
  background: var(--dark-gray);
  padding: 10px 16px;
  border-radius: 6px;
  user-select: all;
  cursor: pointer;
  letter-spacing: 1px;
  margin: 8px 0;
  word-break: break-all;
}

.status-msg {
  font-size: 0.85rem;
  color: var(--yellow);
  min-height: 1.2em;
}

/* --- Game Area --- */
#game-screen {
  gap: 8px;
  max-width: 800px;
}

.game-columns {
  display: flex;
  gap: 24px;
  width: 100%;
  justify-content: center;
}

.player-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.player-column h3 {
  font-size: 0.85rem;
  color: var(--light-gray);
  margin-bottom: 4px;
}

/* Board */
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 5px;
}

.board-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
}

.tile {
  width: 52px;
  height: 52px;
  border: 2px solid var(--dark-gray);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 700;
  text-transform: uppercase;
  transition: transform 0.1s;
  border-radius: 2px;
}

.tile.filled {
  border-color: var(--gray);
  animation: pop 0.1s ease;
}

.tile.green {
  background: var(--green);
  border-color: var(--green);
}

.tile.yellow {
  background: var(--yellow);
  border-color: var(--yellow);
}

.tile.gray {
  background: var(--gray);
  border-color: var(--gray);
}

.tile.reveal {
  animation: flip 0.5s ease forwards;
}

@keyframes pop {
  50% { transform: scale(1.1); }
}

@keyframes flip {
  0% { transform: rotateX(0); }
  50% { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}

.board-row.shake {
  animation: shake 0.4s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

/* Opponent mini-board */
.opponent-board .tile {
  width: 28px;
  height: 28px;
  font-size: 0;
}

/* Timer */
.timer-bar {
  width: 100%;
  max-width: 360px;
  height: 6px;
  background: var(--dark-gray);
  border-radius: 3px;
  overflow: hidden;
  margin: 6px 0;
}

.timer-fill {
  height: 100%;
  background: var(--green);
  transition: width 1s linear, background-color 0.5s;
  border-radius: 3px;
}

.timer-fill.warning {
  background: var(--yellow);
}

.timer-fill.danger {
  background: #e74c3c;
}

.timer-text {
  font-size: 0.85rem;
  color: var(--light-gray);
}

/* Keyboard */
.keyboard {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}

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

.key {
  min-width: 36px;
  height: 50px;
  border: none;
  border-radius: 4px;
  background: var(--key-bg);
  color: white;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  text-transform: uppercase;
  transition: background 0.15s;
  padding: 0 8px;
}

.key:hover {
  opacity: 0.85;
}

.key.wide {
  min-width: 60px;
  font-size: 0.75rem;
}

.key.green { background: var(--green); }
.key.yellow { background: var(--yellow); }
.key.gray { background: var(--dark-gray); }

/* Game area with scoreboard */
.game-area {
  display: flex;
  gap: 20px;
  width: 100%;
  justify-content: center;
  align-items: flex-start;
}

/* Session Scoreboard */
.scoreboard {
  background: #1a1a1b;
  border: 1px solid var(--dark-gray);
  border-radius: 8px;
  padding: 10px;
  min-width: 160px;
  flex-shrink: 0;
}

.scoreboard h3 {
  font-size: 0.8rem;
  color: var(--light-gray);
  text-align: center;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.scoreboard table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.scoreboard th {
  color: var(--gray);
  font-weight: 600;
  padding: 3px 6px;
  border-bottom: 1px solid var(--dark-gray);
  text-align: center;
}

.scoreboard td {
  padding: 4px 6px;
  text-align: center;
  color: var(--light-gray);
}

.scoreboard .sb-word {
  text-transform: uppercase;
  font-family: monospace;
  letter-spacing: 1px;
  font-size: 0.72rem;
  color: var(--gray);
}

.scoreboard .sb-winner {
  color: var(--green);
  font-weight: 700;
}

.scoreboard .sb-totals {
  border-top: 1px solid var(--gray);
}

.scoreboard .sb-totals td {
  font-weight: 700;
  color: white;
  padding-top: 6px;
}

/* Itemized score popup */
.score-popup-container {
  position: fixed;
  top: 30%;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  pointer-events: none;
  z-index: 100;
}

.score-popup-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(30, 30, 30, 0.92);
  border-left: 3px solid var(--green);
  border-radius: 4px;
  padding: 6px 12px;
  opacity: 0;
  transform: translateX(30px);
  transition: opacity 0.3s ease-out, transform 0.3s ease-out;
}

.score-popup-item.visible {
  opacity: 1;
  transform: translateX(0);
}

.score-popup-item.hiding {
  opacity: 0;
  transform: translateX(30px);
}

.score-popup-item.negative {
  border-left-color: #e74c3c;
}

.score-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--light-gray);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.score-pts {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--green);
  margin-left: auto;
}

.score-popup-item.negative .score-pts {
  color: #e74c3c;
}

/* Result overlay session totals */
.result-session {
  margin-top: 4px;
  padding-top: 12px;
  border-top: 1px solid var(--dark-gray);
  text-align: center;
}

.result-session-label {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--gray);
  margin-bottom: 2px;
}

.session-pts {
  font-size: 1.6rem !important;
}

/* Game controls (give up) */
.game-controls {
  display: flex;
  justify-content: center;
  margin: 4px 0;
}

.btn-danger {
  background: #e74c3c;
  font-size: 0.85rem;
  padding: 8px 18px;
}

.btn-danger:hover {
  opacity: 0.85;
}

/* Waiting overlay */
.waiting-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.80);
  z-index: 150;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.waiting-overlay.active {
  display: flex;
}

.waiting-content {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.waiting-content h2 {
  font-size: 1.5rem;
}

.waiting-content p {
  color: var(--light-gray);
  font-size: 0.9rem;
}

.waiting-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--dark-gray);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Result overlay */
.result-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  z-index: 200;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px;
  overflow-y: auto;
}

.result-overlay.active {
  display: flex;
}

.result-overlay h2 {
  font-size: 2rem;
}

.result-overlay .result-word {
  font-size: 1.8rem;
  letter-spacing: 6px;
  text-transform: uppercase;
  color: var(--green);
}

/* Word definition */
.result-definition {
  max-width: 400px;
  width: 100%;
  text-align: left;
  font-size: 0.85rem;
  color: var(--light-gray);
  max-height: 180px;
  overflow-y: auto;
  padding: 0 4px;
}

.result-definition.loading {
  text-align: center;
  color: var(--gray);
  font-style: italic;
}

.def-phonetic {
  display: block;
  text-align: center;
  color: var(--gray);
  font-style: italic;
  margin-bottom: 8px;
  font-size: 0.9rem;
}

.def-meaning {
  margin-bottom: 8px;
}

.def-pos {
  display: inline-block;
  font-weight: 700;
  font-style: italic;
  color: var(--yellow);
  font-size: 0.8rem;
  margin-bottom: 2px;
}

.def-text {
  margin: 2px 0 2px 10px;
  line-height: 1.35;
}

.def-example {
  margin: 1px 0 4px 10px;
  color: var(--gray);
  font-style: italic;
  font-size: 0.8rem;
}

.result-scores {
  display: flex;
  gap: 40px;
  margin: 16px 0;
}

.result-player {
  text-align: center;
}

.result-player .label {
  font-size: 0.9rem;
  color: var(--light-gray);
}

.result-player .pts {
  font-size: 2.5rem;
  font-weight: 700;
}

.result-player .pts.winner {
  color: var(--green);
}

.result-buttons {
  display: flex;
  gap: 12px;
  margin-top: 12px;
}

/* Connection status dot */
.connection-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 6px;
}

.connection-dot.connected { background: var(--green); }
.connection-dot.disconnected { background: #e74c3c; }

/* Toast */
.toast {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  color: black;
  padding: 10px 20px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.9rem;
  z-index: 300;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 1.5s forwards;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(-10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

@keyframes toastOut {
  to { opacity: 0; transform: translateX(-50%) translateY(-10px); }
}

/* Responsive */
@media (max-width: 700px) {
  .game-area {
    flex-direction: column;
    align-items: center;
  }
  .scoreboard {
    width: 100%;
    max-width: 320px;
    order: -1;
  }
  .score-popup-container {
    right: 10px;
    top: 15%;
  }
}

@media (max-width: 500px) {
  .tile {
    width: 44px;
    height: 44px;
    font-size: 1.3rem;
  }
  .opponent-board .tile {
    width: 22px;
    height: 22px;
  }
  .game-columns {
    gap: 12px;
  }
  .key {
    min-width: 28px;
    height: 44px;
    font-size: 0.75rem;
  }
}
