* { box-sizing: border-box; touch-action: manipulation; }

:root {
  --paper: #f6f1e7;
  --ink: #2b2416;
  --faint: #8a7d63;
  --board-gap: #c6b795;
  --cell: #ece3cd;
  --dl: #bcd6e4;
  --tl: #79aac4;
  --dw: #e8b9ad;
  --tw: #d97a5e;
  --tile-bg: #fbf0d2;
  --tile-edge: #d9c79b;
  --accent: #3d6b4f;
  --select: #3579c8;
  --warn: #a33f2c;
  --board-w: min(94vw, 540px);
  --sq: calc(var(--board-w) / 15);
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Top bar */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  max-width: 980px; margin: 0 auto; padding: 10px 16px 0;
}
.brand-small { font-family: Georgia, serif; font-weight: 700; font-size: 1.15rem; color: var(--ink); text-decoration: none; letter-spacing: 0.5px; }
#name-input {
  font-size: 0.95rem; padding: 6px 10px; width: 150px; text-align: right;
  border: 1.5px solid var(--tile-edge); border-radius: 8px; background: #fff; color: var(--ink);
}

#app { max-width: 980px; margin: 0 auto; padding: 8px 12px 48px; }
.screen { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.hidden { display: none !important; }

.brand { font-family: Georgia, "Times New Roman", serif; font-size: 2.2rem; margin: 18px 0 0; letter-spacing: 1px; }
.tagline { color: var(--faint); margin: -6px 0 8px; }

input, select {
  font-size: 1rem; padding: 9px 12px;
  border: 1.5px solid var(--tile-edge); border-radius: 8px;
  background: #fff; color: var(--ink);
}
input:focus, select:focus { outline: 2px solid var(--select); border-color: transparent; }
#code-input { width: 110px; text-transform: uppercase; letter-spacing: 3px; font-weight: 600; text-align: center; }

.btn {
  font-size: 0.98rem; padding: 9px 18px; border-radius: 8px;
  border: 1.5px solid var(--tile-edge); background: #fff; color: var(--ink); cursor: pointer;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.subtle { background: none; border-color: transparent; color: var(--faint); text-decoration: underline; }
.linklike { background: none; border: none; padding: 0; color: var(--faint); text-decoration: underline; cursor: pointer; font-size: inherit; }
.msg { min-height: 1.2em; color: var(--warn); font-size: 0.95rem; text-align: center; margin: 0; }
.hint { color: var(--faint); font-size: 0.9rem; margin: 0; text-align: center; }
.pill { font-size: 0.75rem; color: var(--accent); border: 1px solid var(--accent); border-radius: 999px; padding: 1px 8px; }
.pill.off { color: var(--faint); border-color: var(--tile-edge); }

/* Cards + columns (setwithforks-style matchmaking) */
.cols { display: grid; grid-template-columns: 3fr 2fr; gap: 14px; width: 100%; align-items: start; }
.card {
  background: #fffdf7; border: 1.5px solid var(--tile-edge); border-radius: 12px;
  padding: 14px 16px; width: 100%;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 8px; }
.card-head h2 { font-family: Georgia, serif; font-size: 1.15rem; margin: 0; }
.center-card { display: flex; flex-direction: column; align-items: center; gap: 10px; }

.games-table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.games-table th {
  text-align: left; color: var(--faint); font-weight: 600; font-size: 0.8rem;
  text-transform: uppercase; letter-spacing: 0.5px; padding: 4px 6px; border-bottom: 1.5px solid var(--tile-edge);
}
.games-table td { padding: 7px 6px; border-bottom: 1px solid #efe7d4; }
.games-table tr.g-row { cursor: pointer; }
.games-table tr.g-row:hover { background: #f7f0df; }
.games-table .btn { padding: 4px 12px; font-size: 0.85rem; }
.status-waiting { color: var(--accent); font-weight: 600; }
.status-playing { color: var(--faint); }
#games-empty { padding: 14px 0; }
.row-buttons { display: flex; gap: 10px; justify-content: center; margin: 12px 0 4px; flex-wrap: wrap; }
.join-row { display: flex; gap: 8px; justify-content: center; margin-top: 6px; }
#your-games { margin-top: 10px; font-size: 0.9rem; color: var(--faint); text-align: center; }
#your-games .linklike { margin: 0 4px; }

/* Chat */
.chat-card { display: flex; flex-direction: column; }
.chat-scroll {
  height: 260px; overflow-y: auto; font-size: 0.9rem; line-height: 1.45;
  background: #fff; border: 1px solid #efe7d4; border-radius: 8px; padding: 8px 10px;
}
.chat-scroll .c-line { margin: 2px 0; overflow-wrap: anywhere; }
.chat-scroll .c-name { font-weight: 700; }
.chat-scroll .c-time { color: var(--faint); font-size: 0.75rem; margin-right: 4px; }
.chat-row { display: flex; gap: 6px; margin-top: 8px; }
.chat-row input { flex: 1; min-width: 0; padding: 7px 10px; font-size: 0.92rem; }

/* Room lobby */
.room-line { color: var(--faint); margin: 4px 0 -8px; }
.big-code { font-size: 2rem; font-weight: 700; letter-spacing: 8px; font-family: Georgia, serif; }
.player-list { list-style: none; padding: 0; margin: 4px 0; width: 100%; max-width: 280px; }
.player-list li {
  background: #fff; border: 1.5px solid var(--tile-edge); border-radius: 8px;
  padding: 8px 12px; margin-bottom: 6px; display: flex; align-items: center; gap: 8px;
}
.player-list .host-star { color: #c98f2d; }
.kick-btn { margin-left: 4px; background: none; border: none; color: var(--warn); cursor: pointer; font-size: 1rem; padding: 0 4px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #b9b0a0; margin-left: auto; flex: none; }
.dot.on { background: #4ca36a; }

.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 8px 0; }
.setting-row label { font-size: 0.92rem; color: var(--faint); flex: none; }
.setting-row select { font-size: 0.9rem; padding: 6px 8px; max-width: 230px; }
#chat-slot-lobby { margin-top: 14px; }
#chat-slot-lobby .card, #chat-slot-game .card { padding: 10px 12px; }
#chat-slot-lobby .chat-scroll { height: 180px; }

/* Game layout */
.game-cols { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; width: 100%; align-items: start; }
.game-main { display: flex; flex-direction: column; align-items: center; gap: 12px; min-width: 0; }
.game-side { display: flex; flex-direction: column; gap: 12px; }
.game-side .chat-scroll { height: 220px; }

#scorebar { display: flex; gap: 6px; width: var(--board-w); justify-content: center; flex-wrap: wrap; }
.pl {
  background: #fff; border: 1.5px solid var(--tile-edge); border-radius: 8px;
  padding: 5px 10px; font-size: 0.85rem; display: flex; align-items: center; gap: 6px; min-width: 0;
}
.pl.turn { border-color: var(--accent); box-shadow: 0 0 0 1.5px var(--accent); }
.pl.me .pl-name { font-weight: 700; }
.pl-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 90px; }
.pl-score { font-weight: 700; font-variant-numeric: tabular-nums; }
.pl .dot { margin-left: 2px; }
.timer { font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 700; font-size: 0.8rem; }
.timer.low { color: var(--warn); }

#status-line { margin: 0; color: var(--faint); font-size: 0.95rem; min-height: 1.2em; }
#status-line.mine { color: var(--accent); font-weight: 700; }
#preview-line { min-height: 1.1em; font-size: 0.88rem; }
#preview-line.bad { color: var(--warn); }

/* Board */
#board {
  display: grid; grid-template-columns: repeat(15, 1fr); gap: 1.5px;
  background: var(--board-gap); border: 3px solid var(--board-gap); border-radius: 6px;
  width: var(--board-w);
}
.sq {
  aspect-ratio: 1; background: var(--cell); position: relative;
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--sq) * 0.28); color: rgba(43, 36, 22, 0.55);
  user-select: none; -webkit-user-select: none;
}
.sq.p-d { background: var(--dl); }
.sq.p-t { background: var(--tl); color: rgba(255,255,255,0.85); }
.sq.p-D { background: var(--dw); }
.sq.p-T { background: var(--tw); color: rgba(255,255,255,0.9); }
.sq.center { font-size: calc(var(--sq) * 0.5); }
.sq.drop-hint { outline: 3px dashed var(--select); outline-offset: -2px; z-index: 3; }

.tile {
  position: absolute; inset: 4%;
  background: var(--tile-bg); border: 1px solid var(--tile-edge); border-bottom-width: 2.5px;
  border-radius: 12%; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: calc(var(--sq) * 0.52); color: var(--ink); font-family: Georgia, serif;
}
.tile .pts {
  position: absolute; right: 6%; bottom: 2%; font-size: calc(var(--sq) * 0.22);
  font-family: -apple-system, sans-serif; font-weight: 600; opacity: 0.65;
}
.tile.blank-tile { color: #7a6b8f; }
.tile.pending { outline: 2.5px solid var(--select); outline-offset: -1px; z-index: 2; cursor: grab; touch-action: none; }
.sq.last .tile { outline: 2.5px solid #c98f2d; outline-offset: -1px; }

/* Rack */
#rack {
  display: flex; gap: 6px; justify-content: center;
  min-height: calc(var(--sq) * 1.15); margin-top: 4px;
}
.rtile {
  width: calc(var(--sq) * 1.05); height: calc(var(--sq) * 1.15);
  background: var(--tile-bg); border: 1px solid var(--tile-edge); border-bottom-width: 3px;
  border-radius: 10%; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: calc(var(--sq) * 0.58); font-family: Georgia, serif;
  position: relative; cursor: grab; user-select: none; -webkit-user-select: none; touch-action: none;
}
.rtile .pts { position: absolute; right: 5%; bottom: 2%; font-size: calc(var(--sq) * 0.24); font-family: -apple-system, sans-serif; font-weight: 600; opacity: 0.65; }
.rtile.selected { outline: 3px solid var(--select); outline-offset: -1px; transform: translateY(-4px); }
.rtile.swapsel { outline: 3px solid #c98f2d; outline-offset: -1px; transform: translateY(-4px); }
.rtile.empty { background: rgba(0,0,0,0.06); border-color: transparent; cursor: default; touch-action: auto; }
.rtile.drag-src, .tile.drag-src { opacity: 0.35; }
#rack.drop-hint { outline: 3px dashed var(--select); outline-offset: 3px; border-radius: 8px; }

.drag-ghost {
  position: fixed; z-index: 60; pointer-events: none;
  width: calc(var(--sq) * 1.05); height: calc(var(--sq) * 1.15);
  background: var(--tile-bg); border: 1px solid var(--tile-edge); border-bottom-width: 3px;
  border-radius: 10%; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: calc(var(--sq) * 0.58); font-family: Georgia, serif;
  transform: translate(-50%, -60%) scale(1.06); box-shadow: 0 6px 14px rgba(43,36,22,0.25);
  color: var(--ink);
}
.drag-ghost.blank-tile { color: #7a6b8f; }

#controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
#controls .btn { padding: 9px 16px; }
#skip-btn { border-color: var(--warn); color: var(--warn); }

#log-wrap, #unseen-wrap { color: var(--faint); font-size: 0.85rem; }
#log-wrap summary, #unseen-wrap summary { cursor: pointer; padding: 4px 0; }
#log { list-style: none; padding: 4px 0 0 8px; margin: 0; }
#log li { padding: 1px 0; }
#unseen { display: grid; grid-template-columns: repeat(9, 1fr); gap: 3px; padding: 6px 0; font-variant-numeric: tabular-nums; }
#unseen span { background: #fff; border: 1px solid #efe7d4; border-radius: 5px; text-align: center; padding: 2px 0; font-size: 0.8rem; }
#unseen span.gone { opacity: 0.35; }
.foot { color: var(--faint); font-size: 0.8rem; margin: 2px 0 0; }

/* Game over */
#game-over { text-align: center; }
#final-scores { list-style: none; padding: 0; margin: 0 0 10px; font-size: 1.1rem; }
#final-scores .crown { color: #c98f2d; }

/* Blank picker modal */
#blank-modal {
  position: fixed; inset: 0; background: rgba(43,36,22,0.5);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal-box { background: var(--paper); border-radius: 12px; padding: 18px; max-width: 320px; text-align: center; }
.modal-box p { margin: 0 0 12px; font-weight: 600; }
#blank-letters { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-bottom: 10px; }
#blank-letters button {
  aspect-ratio: 1; font-size: 1.1rem; font-weight: 700; font-family: Georgia, serif;
  background: var(--tile-bg); border: 1px solid var(--tile-edge); border-radius: 8px; cursor: pointer;
}

@media (max-width: 900px) {
  .cols { grid-template-columns: 1fr; }
  .game-cols { grid-template-columns: 1fr; }
  .game-side { width: 100%; }
  .chat-scroll { height: 180px; }
  #topbar { padding-top: 8px; }
  .brand { font-size: 1.8rem; margin-top: 6px; }
}
