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

:root {
  --bg:        #2d5a1b;        /* classic green baize */
  --surface:   #3a7023;
  --border:    #4a8a2e;
  --gold:      #e8c84a;        /* tile yellow */
  --gold-dark: #9a7a10;
  --text:      #f5f0e0;
  --muted:     #a8c890;
  --danger:    #c0392b;
  --success:   #27ae60;
  --wood:      #8b5e2a;        /* wooden rack/border colour */
  --wood-dark: #5a3a10;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ── Screens ── */
.screen { display: none; width: 100%; height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }

/* ── Buttons ── */
.btn {
  padding: .65rem 1.25rem;
  border: none;
  border-radius: 10px;
  font-size: .95rem;
  font-weight: 700;
  cursor: pointer;
  transition: filter .15s, transform .1s;
  white-space: nowrap;
}
.btn:active { transform: scale(.96); }
.btn:disabled { opacity: .35; cursor: default; }
.btn-primary  { background: var(--gold); color: #2a1a00; border: 2px solid var(--gold-dark); }
.btn-primary:not(:disabled):hover  { filter: brightness(1.1); }
.btn-secondary { background: var(--wood); color: #f5f0e0; border: 2px solid var(--wood-dark); }
.btn-secondary:not(:disabled):hover { filter: brightness(1.2); }
.btn-ghost    { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-full     { width: 100%; }
.btn-small    { padding: .35rem .75rem; font-size: .8rem; background: var(--wood); color: #f5f0e0; border: 1px solid var(--wood-dark); border-radius: 8px; }

/* ── HOME ── */
.home-container {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  height: 100%; gap: 2rem; padding: 1.5rem;
  background: radial-gradient(ellipse at center, #3a7023 0%, #1e4010 100%);
}
.logo-wrap { text-align: center; }
.logo {
  font-size: clamp(2.5rem, 10vw, 4rem);
  font-weight: 900; letter-spacing: .35em;
  color: var(--gold);
  text-shadow: 3px 3px 0 var(--gold-dark);
}
.logo-sub { color: var(--muted); font-size: .9rem; margin-top: .25rem; letter-spacing: .1em; }
.logo-dev { color: var(--muted); font-size: .72rem; margin-top: .2rem; opacity: .7; }
.home-card {
  background: rgba(0,0,0,.25); border: 2px solid var(--wood);
  border-radius: 16px; padding: 1.5rem;
  width: 100%; max-width: 320px;
  display: flex; flex-direction: column; gap: 1rem;
  backdrop-filter: blur(4px);
}
.divider {
  display: flex; align-items: center; gap: .75rem;
  color: var(--muted); font-size: .8rem;
}
.divider::before, .divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}
.join-form { display: flex; flex-direction: column; gap: .5rem; }
.join-form input {
  width: 100%;
  padding: .65rem .75rem;
  background: rgba(0,0,0,.3); border: 2px solid var(--wood);
  border-radius: 10px; color: var(--text);
  font-size: 1.1rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .2em; text-align: center;
}
.join-form input:focus { outline: 2px solid var(--gold); border-color: transparent; }
.join-btns { display: flex; gap: .5rem; }
.join-btns .btn { flex: 1; }
.error-msg { color: #ffaaaa; font-size: .85rem; min-height: 1.2rem; text-align: center; }

/* ── WAITING ── */
.waiting-container {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  height: 100%; gap: 1.25rem; padding: 2rem; text-align: center;
  background: radial-gradient(ellipse at center, #3a7023 0%, #1e4010 100%);
}
.waiting-label { color: var(--muted); font-size: .95rem; }
.game-code {
  font-size: clamp(2rem, 12vw, 4rem);
  font-weight: 900; letter-spacing: .4em;
  color: var(--gold);
  background: rgba(0,0,0,.3); border: 2px solid var(--gold-dark);
  padding: .75rem 1.5rem; border-radius: 14px;
}
.waiting-dots { display: flex; gap: .5rem; }
.dot {
  width: 10px; height: 10px;
  background: var(--gold); border-radius: 50%;
  animation: bounce 1.4s ease-in-out infinite;
}
.dot:nth-child(2) { animation-delay: .2s; }
.dot:nth-child(3) { animation-delay: .4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-10px); }
}
.waiting-text { color: var(--muted); }

/* ── GAME HEADER ── */
#game-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .4rem .75rem;
  background: var(--wood-dark);
  border-bottom: 3px solid var(--wood);
  flex-shrink: 0; min-height: 52px;
}
.player-panel {
  display: flex; flex-direction: column; align-items: center;
  padding: .3rem .6rem; border-radius: 8px;
  border: 2px solid transparent;
  transition: border-color .2s, background .2s;
  min-width: 80px;
}
.player-panel.active {
  background: rgba(232,200,74,.15);
  border-color: var(--gold);
}
.pname { font-size: .7rem; color: var(--muted); letter-spacing: .05em; }
.pscore { font-size: 1.4rem; font-weight: 800; color: var(--gold); }
#turn-badge {
  font-size: .78rem; font-weight: 700;
  padding: .3rem .8rem; border-radius: 20px;
  background: rgba(0,0,0,.3); color: var(--muted);
  border: 1px solid var(--border);
  transition: background .2s, color .2s;
}
#screen-game.your-turn #turn-badge {
  background: var(--gold); color: #2a1a00; border-color: var(--gold-dark);
}

/* ── BOARD ── */
#board-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--bg);
  touch-action: none; /* we handle all touch gestures ourselves */
}
#board {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  gap: 2px;
  background: #1a3a0a;
  border: 3px solid var(--wood-dark);
  border-radius: 4px;
  position: absolute;
  transform-origin: 0 0;
  /* size is set once on load and stays fixed */
  width:  min(calc(100vw - 12px), calc(100vh - 210px));
  height: min(calc(100vw - 12px), calc(100vh - 210px));
}
.cell {
  display: flex; align-items: center; justify-content: center;
  background: #2a5a15; cursor: pointer; position: relative;
  transition: filter .1s; overflow: hidden;
}
.cell:hover { filter: brightness(1.25); }

/* Premium squares — classic Scrabble colours */
.cell.TW { background: #c0392b; }
.cell.DW { background: #e07080; }
.cell.TL { background: #2471a3; }
.cell.DL { background: #5dade2; }
.cell.star { background: #e07080; }
.cell.star::after {
  content: '★';
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%,-50%);
  font-size: 1.2cqi; opacity: .6; color: #fff;
  pointer-events: none; z-index: 0;
}


.cell-label {
  position: absolute; font-size: .9cqi;
  color: rgba(255,255,255,.85); line-height: 1; text-align: center;
  pointer-events: none; top: 50%; left: 50%;
  transform: translate(-50%,-50%);
  font-weight: 700;
}

/* Board tile — cream/ivory classic look */
.b-tile {
  position: absolute;
  width: 90%; height: 90%;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: #f5eec8;
  color: #1a0a00;
  border-radius: 2px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 2.2cqi;
  box-shadow: 1px 2px 3px rgba(0,0,0,.4), inset 0 1px 0 rgba(255,255,255,.6);
  border: 1px solid #c8a850;
  z-index: 1;
}
.b-tile.blank-tile { background: #e8e4c0; }
.b-tile.pending-tile {
  background: #fffaaa;
  border: 2px solid #cc8800;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(200,150,0,.5);
}
.b-tile .tv {
  position: absolute; bottom: .5%; right: 3%;
  font-size: .9cqi; font-weight: 500; color: #5a3a10;
}

/* ── RACK ── */
#rack-area {
  background: var(--wood-dark);
  border-top: 3px solid var(--wood);
  padding: .5rem; flex-shrink: 0;
}
#rack {
  display: flex; justify-content: center; gap: 5px;
  margin-bottom: .4rem;
}
.r-tile {
  width: 44px; height: 44px;
  background: #f5eec8; color: #1a0a00;
  border-radius: 6px; border: 2px solid transparent;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.25rem; font-weight: 800;
  cursor: pointer; position: relative;
  box-shadow: 2px 3px 5px rgba(0,0,0,.5), inset 0 1px 0 rgba(255,255,255,.6);
  border-color: #c8a850;
  transition: transform .12s, box-shadow .12s, border-color .12s;
  flex-shrink: 0;
}
.r-tile:hover  { transform: translateY(-4px); box-shadow: 2px 6px 10px rgba(0,0,0,.5); }
.r-tile.sel    { border-color: #cc8800; transform: translateY(-8px); box-shadow: 2px 8px 14px rgba(200,130,0,.6); background: #fffaaa; }
.r-tile.exsel  { border-color: #c0392b; transform: translateY(-4px); background: #ffe0e0; }
.r-tile.blank-r { background: #e8e4c0; }
.r-tile.used   { opacity: 0; pointer-events: none; }
.r-tile .tv { position: absolute; bottom: 2px; right: 3px; font-size: .52rem; font-weight: 500; color: #5a3a10; }
.r-empty {
  width: 44px; height: 44px;
  background: rgba(0,0,0,.2); border: 1px dashed rgba(255,255,255,.2);
  border-radius: 6px; flex-shrink: 0;
}
#rack-btns { display: flex; justify-content: center; gap: .5rem; }

/* ── ACTION BAR ── */
#action-bar {
  display: flex; justify-content: center; gap: .5rem;
  padding: .5rem; background: var(--wood-dark);
  border-top: 3px solid var(--wood); flex-shrink: 0;
}
#action-bar .btn { flex: 1; max-width: 120px; }
#btn-play { min-width: 110px; }

/* ── STATUS BAR ── */
#status-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: .2rem .5rem; background: var(--wood-dark);
  font-size: .72rem; color: var(--muted);
  border-top: 1px solid var(--wood); flex-shrink: 0;
}
.status-btns { display: flex; gap: .3rem; }
.status-btns .btn-small { font-size: .65rem; padding: .2rem .5rem; }
#last-move-bar {
  padding: .1rem .75rem; background: var(--wood-dark);
  font-size: .7rem; color: var(--muted); min-height: 1.2rem; flex-shrink: 0;
}
/* ── BOARD COORDINATES (hint mode) ── */
.coord-col {
  position: absolute; top: 1px; left: 50%; transform: translateX(-50%);
  font-size: .75cqi; font-weight: 800; color: rgba(255,255,255,.8);
  pointer-events: none; z-index: 0; text-shadow: 0 1px 2px rgba(0,0,0,.6);
  line-height: 1;
}
.coord-row {
  position: absolute; top: 50%; left: 2px; transform: translateY(-50%);
  font-size: .75cqi; font-weight: 800; color: rgba(255,255,255,.8);
  pointer-events: none; z-index: 0; text-shadow: 0 1px 2px rgba(0,0,0,.6);
  line-height: 1;
}

/* ── HINT BAR ── */
#hint-bar {
  display: flex; align-items: center; gap: .5rem;
  padding: .3rem .6rem; background: rgba(0,0,0,.35);
  border-top: 1px solid rgba(232,200,74,.3); flex-shrink: 0;
  min-height: 2rem; flex-wrap: wrap;
}
#hint-bar.hidden { display: none; }
#hint-bar-label { font-size: .85rem; flex-shrink: 0; }
#hint-bar-list { display: flex; gap: .4rem; flex-wrap: wrap; flex: 1; align-items: center; }
.hint-chip {
  display: inline-flex; align-items: center; gap: .3rem;
  background: rgba(232,200,74,.12); border: 1px solid rgba(232,200,74,.35);
  border-radius: 20px; padding: .2rem .55rem; font-size: .72rem;
}
.hint-chip b { color: var(--gold); letter-spacing: .04em; }
.hint-chip-pos { color: var(--muted); font-size: .68rem; }
.hint-chip-score { color: var(--text); font-weight: 700; font-size: .68rem; }

/* ── CHECKER / HISTORY MODALS ── */
#checker-word-display {
  display: flex; flex-wrap: wrap; gap: 4px; justify-content: center;
  min-height: 52px; background: rgba(0,0,0,.2);
  border: 2px solid var(--wood); border-radius: 10px;
  padding: 6px; margin-bottom: .5rem; align-items: center;
}
.checker-tile {
  width: 40px; height: 46px; background: #f5eec8; color: #1a0a00;
  border-radius: 4px; border: 1px solid #c8a850;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 1.2rem; cursor: pointer; position: relative;
  box-shadow: 1px 2px 3px rgba(0,0,0,.3);
  transition: transform .1s;
}
.checker-tile:hover { transform: translateY(-3px); filter: brightness(.9); }
.checker-tile .tv { position: absolute; bottom: 1px; right: 3px; font-size: .5rem; color: #5a3a10; }
.checker-placeholder { color: var(--muted); font-size: .82rem; text-align: center; padding: .5rem; }
#checker-rack {
  display: flex; flex-wrap: wrap; gap: 5px; justify-content: center;
  margin: .5rem 0; padding: .4rem 0;
  border-top: 1px solid rgba(255,255,255,.1);
}
#checker-rack .r-tile { cursor: pointer; touch-action: none; }
#hint-panel.hidden { display: none; }
/* Spectator mode — hide all interactive controls */
.spectating #rack-area,
.spectating #action-bar,
.spectating #btn-checker,
.spectating #btn-history { display: none; }
/* QR scanner */
#qr-reader { width:100%; max-width:280px; margin:0 auto; border-radius:8px; overflow:hidden; }
#qr-reader video { border-radius:8px; }
#waiting-qr img { box-shadow:0 2px 12px rgba(0,0,0,.4); }
/* drag clone */
.drag-clone {
  position: fixed; pointer-events: none; z-index: 9999;
  transform: translate(-50%,-50%) scale(1.25);
  opacity: .9; box-shadow: 0 8px 20px rgba(0,0,0,.5);
}
#checker-result {
  text-align: center; font-size: 1rem; font-weight: 700; min-height: 1.5rem;
  padding: .25rem 0;
}
#checker-result.valid   { color: #5dfc8b; }
#checker-result.invalid { color: #ff7070; }
#hint-list { display: flex; flex-direction: column; gap: .3rem; max-height: 180px; overflow-y: auto; }
.hint-row {
  display: flex; justify-content: space-between;
  background: rgba(0,0,0,.2); border-radius: 6px; padding: .35rem .6rem;
  font-size: .9rem;
}
.hint-word { font-weight: 700; color: var(--gold); letter-spacing: .05em; }
.hint-pos  { color: var(--text); font-size: .8rem; opacity: .75; }
.hint-score { color: var(--muted); font-size: .8rem; }
#history-list {
  display: flex; flex-direction: column; gap: .35rem;
  max-height: 55vh; overflow-y: auto; margin-top: .5rem;
}
.history-row {
  display: flex; justify-content: space-between; align-items: center;
  background: rgba(0,0,0,.2); border-radius: 6px; padding: .35rem .6rem;
  font-size: .85rem;
}
.history-row .h-who { font-size: .7rem; color: var(--muted); min-width: 50px; }
.history-row .h-words { font-weight: 700; color: var(--text); flex: 1; padding: 0 .5rem; }
.history-row .h-score { color: var(--gold); font-weight: 700; }
.history-empty { text-align: center; color: var(--muted); padding: 1rem; }

/* ── GAME OVER ── */
.gameover-wrap {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  height: 100%; gap: 1.5rem; padding: 2rem;
  background: radial-gradient(ellipse at center, #3a7023 0%, #1e4010 100%);
}
#go-title { font-size: 2rem; color: var(--gold); font-weight: 900; }
#go-scores { display: flex; flex-direction: column; gap: .5rem; width: 100%; max-width: 280px; }
.score-row {
  display: flex; justify-content: space-between; align-items: center;
  background: rgba(0,0,0,.25); border: 2px solid var(--wood);
  border-radius: 10px; padding: .75rem 1rem; font-size: 1.05rem;
}
.score-row.winner { background: rgba(232,200,74,.15); border-color: var(--gold); color: var(--gold); font-weight: 700; }

/* ── MODAL ── */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.75);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-box {
  background: #3a7023; border: 2px solid var(--wood);
  border-radius: 16px; padding: 1.5rem;
  width: min(320px, 90vw);
}
.modal-box p { text-align: center; margin-bottom: 1rem; font-size: .95rem; color: var(--text); }
#letter-picker {
  display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px;
}
.lbtn {
  aspect-ratio: 1; background: #f5eec8; color: #1a0a00;
  border: 1px solid #c8a850; border-radius: 4px;
  font-weight: 800; font-size: .9rem; cursor: pointer;
  transition: transform .1s, filter .1s;
}
.lbtn:hover { transform: scale(1.1); filter: brightness(1.1); background: #fffaaa; }
.lbtn:active { transform: scale(.96); }

/* ── TOAST ── */
.toast {
  position: fixed; bottom: 5rem; left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,.85); color: #fff;
  padding: .6rem 1.25rem; border-radius: 10px;
  font-size: .88rem; font-weight: 600;
  z-index: 200; max-width: 85vw; text-align: center;
  animation: fadeIn .2s;
}
.toast.hidden { display: none; }
.toast.err  { background: var(--danger); }
.toast.ok   { background: var(--success); }
@keyframes fadeIn { from { opacity: 0; transform: translateX(-50%) translateY(8px); } }

/* ── LOADING ── */
.loading {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 150;
}
.loading.hidden { display: none; }
.spinner {
  width: 48px; height: 48px;
  border: 4px solid rgba(255,255,255,.2);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin .75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Exchange mode ── */
#btn-exchange.exchange-active { background: #8b2020; color: #fff; border-color: #c0392b; }

/* ── Pick tiles modal ── */
#modal-pick .modal-box {
  max-height: 80vh; display: flex; flex-direction: column;
}
#pick-grid {
  display: flex; justify-content: center; gap: 6px; flex-wrap: wrap;
  overflow-y: auto; padding: .5rem 0; flex: 1;
}
.pick-card {
  width: 42px; height: 50px; min-width: 42px; flex-shrink: 0;
  background: var(--wood-dark); border: 2px solid var(--gold);
  border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem; color: var(--gold);
  transition: transform .15s, background .25s, border-color .25s;
  position: relative;
}
.pick-card:hover:not(.flipped) { transform: translateY(-6px); filter: brightness(1.3); }
.pick-card.flipped {
  background: #f5eec8; border-color: #c8a850; cursor: default;
  font-size: 1.4rem; font-weight: 800; color: #1a0a00;
  animation: flipIn .25s ease;
}
.pick-card.flipped .pick-val {
  position: absolute; bottom: 2px; right: 4px;
  font-size: .5rem; font-weight: 500; color: #5a3a10;
}
@keyframes flipIn { from { transform: scaleX(0); } to { transform: scaleX(1); } }
#pick-count { text-align: center; color: var(--muted); font-size: .85rem; }

/* ── AI thinking ── */
#ai-thinking {
  position: fixed; bottom: 5rem; left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,.8); color: #f5f0e0;
  padding: .5rem 1.2rem; border-radius: 20px;
  font-size: .85rem; display: flex; align-items: center; gap: .5rem;
  z-index: 200;
}
#ai-thinking.hidden { display: none; }
.ai-dot { width: 6px; height: 6px; background: var(--gold); border-radius: 50%; animation: bounce 1s infinite; }
.ai-dot:nth-child(2) { animation-delay: .15s; }
.ai-dot:nth-child(3) { animation-delay: .3s; }
