:root {
  --bg: #1e2430;
  --panel: #2a3242;
  --panel-light: #343e52;
  --text: #e8ecf3;
  --muted: #9aa5b8;
  --accent: #4da3ff;
  --board-bg: #0f6b4f;
  --cell: #12805e;
  --cell-border: #0c5a42;
  --tile: #f3d9a4;
  --tile-edge: #c9a96a;
  --tile-text: #3a2c14;
  --tile-pending: #ffe9b8;
  --dl: #7fb6d9;
  --tl: #3e7cb8;
  --dw: #e8a4a4;
  --tw: #d95f5f;
  --error: #ff7b7b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

button {
  font: inherit;
  padding: 0.5rem 1rem;
  border: 0;
  border-radius: 8px;
  background: var(--panel-light);
  color: var(--text);
  cursor: pointer;
}
button:hover { filter: brightness(1.15); }
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: var(--accent); color: #08243f; font-weight: 600; }

input {
  font: inherit;
  padding: 0.5rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--panel-light);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
input:focus { outline: 2px solid var(--accent); }

.error { color: var(--error); margin: 0.5rem 0 0; min-height: 1.2em; }

/* ---- overlays ---- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 20, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  padding: 2rem 0;
  z-index: 10;
}
.overlay[hidden] { display: none; }

.rejoin-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
  background: var(--accent);
  color: #0a1a2e;
  padding: 0.7rem 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem 1rem;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
}
.rejoin-banner[hidden] { display: none; }
.rejoin-banner-actions { display: flex; gap: 0.5rem; }
.rejoin-banner button { font-weight: 600; }
.rejoin-banner #rejoin-new { background: #fff; color: #0a1a2e; }

.how-it-works {
  text-align: left;
  color: var(--muted);
  margin: 0.75rem 0 1.25rem;
  padding-left: 1.4rem;
  line-height: 1.5;
}

.lobby-card {
  background: var(--panel);
  border-radius: 16px;
  padding: 2rem 2.5rem;
  width: min(420px, 92vw);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  text-align: center;
}
.lobby-card h1 { margin: 0 0 0.25rem; font-family: Georgia, serif; }
.lobby-card h2 { margin: 0 0 0.25rem; font-family: Georgia, serif; font-size: 1.2rem; }
.tagline { color: var(--muted); margin-top: 0; }
.lobby-card label { display: block; text-align: left; margin: 1rem 0; color: var(--muted); }
.lobby-card label input { margin-top: 0.3rem; }

.lobby-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.2rem;
}
.lobby-choice {
  display: flex;
  gap: 1.2rem;
  flex-wrap: wrap;
  justify-content: center;
}
.lobby-option {
  width: min(320px, 92vw);
  display: flex;
  flex-direction: column;
}
.lobby-option button { margin-top: auto; }
#join-btn, .send-btn {
  transition: background-color 0.15s, color 0.15s, box-shadow 0.15s;
}
#join-btn.ready, .send-btn.ready {
  background: var(--accent);
  color: #08243f;
  font-weight: 600;
  box-shadow: 0 0 0 3px rgba(77, 163, 255, 0.35);
}

.dict-options {
  border: 1px solid var(--panel-light);
  border-radius: 10px;
  margin: 0 0 1rem;
  padding: 0.6rem 0.9rem 0.8rem;
  text-align: left;
}
.dict-options legend { color: var(--muted); font-size: 0.85rem; padding: 0 0.4rem; }
.dict-list { display: flex; flex-wrap: wrap; gap: 0.4rem 1.2rem; color: var(--muted); }
.dict-list label {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0;
  color: var(--text);
  cursor: pointer;
}
.dict-list input[type="checkbox"] { width: auto; accent-color: var(--accent); }

.dict-label { color: var(--muted); font-size: 0.8rem; margin: 0.6rem 0 0; }

/* ---- timers ---- */

.timer-options { display: flex; gap: 1rem; }
.timer-select { flex: 1; color: var(--muted); font-size: 0.85rem; }
.timer-select select {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--panel-light);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.turn-timer {
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 1.15rem;
  margin-bottom: 0.5rem;
  color: var(--text);
}
.turn-timer.low { color: var(--error); }

.scoreboard .clock {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.85rem;
  margin-left: auto;
  padding-right: 0.6rem;
}
.scoreboard .clock.low { color: var(--error); font-weight: 700; }
.scoreboard .flagged { color: var(--error); }
#room-input { text-transform: uppercase; letter-spacing: 0.15em; }

.room-code {
  font-size: 2.2rem;
  letter-spacing: 0.35em;
  font-weight: 700;
  background: var(--bg);
  border-radius: 10px;
  padding: 0.6rem 0.4rem 0.6rem 0.75rem;
  margin: 0.8rem 0 1.2rem;
  user-select: all;
}
.waiting-players { list-style: none; padding: 0; margin: 0 0 1.2rem; }
.waiting-players li { padding: 0.35rem; border-bottom: 1px solid var(--panel-light); }

.blank-letters {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0.4rem;
  margin-top: 1rem;
}
.blank-letters button { padding: 0.5rem 0; font-weight: 700; }

/* ---- game layout ---- */

#game {
  display: flex;
  gap: 1.2rem;
  padding: 1.2rem;
  max-width: 1280px;
  margin: 0 auto;
  align-items: flex-start;
}
#game[hidden] { display: none; }

.board-pane { flex: 0 1 auto; }
.side-pane {
  flex: 1 1 260px;
  min-width: 240px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-self: stretch;
}

/* ---- board ---- */

.board {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  gap: 2px;
  background: var(--board-bg);
  border: 6px solid var(--board-bg);
  border-radius: 10px;
  width: min(78vh, 92vw, 640px);
  aspect-ratio: 1;
  touch-action: manipulation;
}

.cell {
  background: var(--cell);
  border-radius: 3px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(0.4rem, 1.1vw, 0.62rem);
  color: rgba(255, 255, 255, 0.85);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.cell.DL { background: var(--dl); color: #123; }
.cell.TL { background: var(--tl); }
.cell.DW { background: var(--dw); color: #401818; }
.cell.TW { background: var(--tw); }
.cell.center::before { content: "★"; font-size: 1.2em; }
.cell.drag-over { outline: 3px solid var(--accent); outline-offset: -2px; z-index: 2; }

/* ---- tiles ---- */

.tile {
  width: 100%;
  height: 100%;
  background: linear-gradient(160deg, var(--tile), var(--tile-edge));
  color: var(--tile-text);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: clamp(0.7rem, 2.4vw, 1.3rem);
  font-family: Georgia, serif;
  position: relative;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}
.tile .pts {
  position: absolute;
  bottom: 4%;
  right: 8%;
  font-size: 0.45em;
  font-family: "Segoe UI", sans-serif;
}
.tile.blank { color: #7a5c9e; }
.tile.pending {
  background: linear-gradient(160deg, var(--tile-pending), var(--tile-edge));
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  cursor: grab;
}
.tile.last-move { outline: 2px solid #ffd76a; outline-offset: -2px; }
.tile.pending.selected { outline: 3px solid #ffd76a; outline-offset: -2px; }

/* ---- rack ---- */

.rack-area { margin-top: 1rem; }

.rack {
  display: flex;
  gap: 6px;
  justify-content: center;
  background: var(--panel);
  padding: 10px;
  border-radius: 12px;
  min-height: 66px;
}
.rack .tile {
  width: 48px;
  height: 48px;
  font-size: 1.5rem;
  cursor: grab;
  flex: 0 0 auto;
}
.rack .tile.selected {
  outline: 3px solid var(--accent);
  outline-offset: -2px;
  transform: translateY(-6px);
}
.rack .tile.exchange-selected {
  outline: 3px solid var(--error);
  outline-offset: -2px;
  transform: translateY(-6px);
}
.rack .tile.dragging { opacity: 0.35; }
.rack .tile.insert-before, .rack .tile.insert-after {
  position: relative;
}
.rack .tile.insert-before::before, .rack .tile.insert-after::after {
  content: "";
  position: absolute;
  top: -4px;
  bottom: -4px;
  width: 3px;
  border-radius: 2px;
  background: var(--accent);
}
.rack .tile.insert-before::before { left: -5px; }
.rack .tile.insert-after::after { right: -5px; }

.controls {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  margin-top: 0.8rem;
  flex-wrap: wrap;
}
.controls .exchange-armed { background: var(--error); color: #3d0d0d; font-weight: 600; }

/* ---- side panels ---- */

.panel {
  background: var(--panel);
  border-radius: 12px;
  padding: 0.9rem 1rem;
}
.panel.grow { flex: 1; display: flex; flex-direction: column; min-height: 170px; }
.panel h2 {
  margin: 0 0 0.6rem;
  font-size: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.bag-count { font-weight: 400; text-transform: none; letter-spacing: 0; }

.scoreboard { list-style: none; margin: 0; padding: 0; }
.scoreboard li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  gap: 0.5rem;
}
.scoreboard li.turn { background: var(--panel-light); box-shadow: inset 3px 0 0 var(--accent); }
.scoreboard .who { display: flex; align-items: center; gap: 0.45rem; min-width: 0; }
.scoreboard .who span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scoreboard .dot { width: 8px; height: 8px; border-radius: 50%; background: #5f6b80; flex: 0 0 auto; }
.scoreboard .dot.online { background: #58d68d; }
.scoreboard .score { font-weight: 700; font-variant-numeric: tabular-nums; }

.log {
  flex: 1;
  overflow-y: auto;
  font-size: 0.88rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  min-height: 0;
  max-height: 28vh;
}
.log .entry { color: var(--muted); }
.log .entry b { color: var(--text); }

.chat-form { display: flex; gap: 0.5rem; margin-top: 0.6rem; }

/* ---- chat ---- */

.chat-list {
  overflow-y: auto;
  font-size: 0.88rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  min-height: 0;
}
.chat-list.grow-list { flex: 1; max-height: 34vh; }
.chat-list .msg { color: var(--text); overflow-wrap: anywhere; }
.chat-list .msg b { color: var(--accent); }
.chat-list .msg.mine b { color: #58d68d; }
.chat-list .msg .time { color: var(--muted); font-size: 0.75em; margin-right: 0.4em; }
.chat-list .empty { color: var(--muted); font-style: italic; }

.waiting-chat {
  margin-top: 1.2rem;
  border-top: 1px solid var(--panel-light);
  padding-top: 0.9rem;
  text-align: left;
}
.waiting-chat .chat-list { max-height: 22vh; min-height: 3.5rem; }

#final-scores { text-align: left; margin: 1rem 0 1.4rem; }
#final-scores .row {
  display: flex;
  justify-content: space-between;
  padding: 0.4rem 0.2rem;
  border-bottom: 1px solid var(--panel-light);
}
#final-scores .winner { color: #ffd76a; font-weight: 700; }

/* ---- fun ---- */

.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 100;
}

.splash {
  position: fixed;
  top: 38%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: Georgia, serif;
  font-size: clamp(2rem, 7vw, 4rem);
  font-weight: 800;
  color: #ffd76a;
  text-shadow: 0 3px 14px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  z-index: 101;
  animation: splash-pop 1.8s ease-out forwards;
  white-space: nowrap;
}
@keyframes splash-pop {
  0%   { transform: translate(-50%, -50%) scale(0.3); opacity: 0; }
  18%  { transform: translate(-50%, -50%) scale(1.15); opacity: 1; }
  30%  { transform: translate(-50%, -50%) scale(1); }
  75%  { opacity: 1; }
  100% { transform: translate(-50%, -58%) scale(1); opacity: 0; }
}

.emoji-bar { display: flex; gap: 0.3rem; margin-top: 0.5rem; }
.emoji-bar button {
  padding: 0.15rem 0.4rem;
  font-size: 1.05rem;
  background: transparent;
  border-radius: 6px;
}
.emoji-bar button:hover { background: var(--panel-light); }

.fun-facts {
  text-align: left;
  background: var(--bg);
  border-radius: 10px;
  padding: 0.7rem 0.9rem;
  margin: 0 0 1.2rem;
  font-size: 0.92rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.fun-facts b { color: #ffd76a; }

@media (max-width: 900px) {
  #game { flex-direction: column; align-items: center; }
  .side-pane { width: 100%; max-width: 640px; }
}
