/* ── Reset & root ─────────────────────────────────────────────────────────── */

:root {
  --font: system-ui, -apple-system, 'Helvetica Neue', Arial, sans-serif;
  /* fluid: fits 10 tiles in a row on screens ≥ 480px; floors at 38px on narrow phones */
  --tile-sz: clamp(38px, calc((min(100vw, 480px) - 32px - 45px) / 10), 52px);
  --tile-gap: 5px;
  --col-0: #ffffff;
  --col-1: #f6f7f8;
  --col-2: #d3d6da;
  --col-3: #878a8c;
  --col-dark: #121213;
  --col-claimed: #121213;
  --col-from-word: #b59f3b;
  --col-error: #c9414b;
  --col-success: #538d4e;
  --radius: 5px;
  --bottom-safe: env(safe-area-inset-bottom, 0px);
}

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

[hidden] { display: none !important; }

html, body { height: 100%; overflow: hidden; }

body {
  font-family: var(--font);
  background: var(--col-0);
  color: var(--col-dark);
  display: flex;
  justify-content: center;
}

#app {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Start screen ─────────────────────────────────────────────────────────── */

#start-screen {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 48px 16px;
  gap: 24px;
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

.start-top {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  position: relative;
}

.start-title { font-size: 2.2rem; font-weight: 800; letter-spacing: .25em; }

.start-sub-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.start-sub { color: var(--col-3); font-size: .95rem; }

#lb-icon-btn {
  background: transparent;
  color: var(--col-3);
  padding: 4px;
  border-radius: 6px;
  transition: color 80ms;
  flex-shrink: 0;
}
#lb-icon-btn:hover { color: var(--col-dark); }

.mode-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  border: none;
}

.mode-group legend {
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .12em;
  color: var(--col-3);
  margin-bottom: 10px;
}

.mode-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  border: 2px solid var(--col-2);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 80ms;
}

.mode-card:has(input:checked) { border-color: var(--col-dark); }
.mode-card input[type="radio"] { accent-color: var(--col-dark); width: 18px; height: 18px; }
.mode-card-name { font-weight: 700; font-size: .95rem; }
.mode-card-desc { color: var(--col-3); font-size: .8rem; margin-left: auto; }

/* ── Shared buttons ───────────────────────────────────────────────────────── */

button {
  font-family: var(--font);
  font-weight: 700;
  letter-spacing: .06em;
  border: none;
  cursor: pointer;
  border-radius: var(--radius);
  transition: opacity 80ms, transform 60ms;
}

button:active:not(:disabled) { transform: scale(.97); }
button:disabled { opacity: .38; cursor: not-allowed; }

.btn-primary {
  background: var(--col-dark);
  color: #fff;
  padding: 15px 40px;
  font-size: 1rem;
  border-radius: 8px;
  width: 100%;
}

.btn-secondary {
  background: transparent;
  color: var(--col-dark);
  border: 2px solid var(--col-dark);
  padding: 13px 40px;
  font-size: 1rem;
  border-radius: 8px;
  width: 100%;
}

/* ── Game screen ──────────────────────────────────────────────────────────── */

#game-screen {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

/* ── Header ───────────────────────────────────────────────────────────────── */

bg-game-header {
  display: block;
  flex-shrink: 0;
  padding: 10px 16px 8px;
  border-bottom: 1px solid var(--col-2);
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

.header-top { text-align: center; margin-bottom: 6px; }

.header-title { font-size: 1.2rem; font-weight: 800; letter-spacing: .2em; }

.header-stats { display: flex; justify-content: center; gap: 32px; }

.stat { display: flex; flex-direction: column; align-items: center; gap: 1px; }
.stat-val { font-size: 1.1rem; font-weight: 700; line-height: 1; }
.stat-lbl { font-size: .58rem; font-weight: 600; text-transform: uppercase; letter-spacing: .1em; color: var(--col-3); }
.stat.urgent .stat-val { color: var(--col-error); }

/* ── Scrollable word board ────────────────────────────────────────────────── */

#game-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 8px 8px;
  display: flex;
  flex-direction: column;
}

bg-word-board {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  align-content: flex-start;
}

/* When the board is empty, centre the message both axes */
bg-word-board:has(.board-empty) {
  align-content: center;
}

.board-empty {
  color: var(--col-3);
  font-size: .875rem;
  font-style: italic;
  text-align: center;
  width: 100%;
}

bg-word {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  padding: 6px;
  border-radius: 8px;
  border: 2px solid transparent;
  cursor: pointer;
  transition: opacity 80ms;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

bg-word:active { opacity: .7; }

.word-pts {
  font-size: .62rem;
  font-weight: 700;
  color: var(--col-3);
  letter-spacing: .05em;
  padding-left: 2px;
}

/* ── Tiles (shared bg-tile) ───────────────────────────────────────────────── */

.tile-row { display: flex; gap: var(--tile-gap); }

bg-tile {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tile-sz);
  height: var(--tile-sz);
  font-size: 1.05rem;
  font-weight: 800;
  text-transform: uppercase;
  border: 2px solid var(--col-2);
  border-radius: var(--radius);
  background: var(--col-0);
  color: var(--col-dark);
  user-select: none;
}

bg-tile::before { content: attr(letter); }

bg-tile[data-state="claimed"] {
  background: var(--col-claimed);
  color: #fff;
  border-color: var(--col-claimed);
}

/* ── Sticky bottom panel ──────────────────────────────────────────────────── */

#bottom-panel {
  flex-shrink: 0;
  background: var(--col-0);
  border-top: 1px solid var(--col-2);
  padding: 10px 16px calc(12px + var(--bottom-safe));
  display: flex;
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
  flex-direction: column;
  gap: 8px;
}

/* ── Unclaimed rack ───────────────────────────────────────────────────────── */

bg-tile-rack { display: block; }

.rack-label {
  font-size: .62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--col-3);
  margin-bottom: 6px;
}

.rack-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--tile-gap);
  min-height: var(--tile-sz);
  align-items: center;
}

.rack-empty { font-size: .82rem; color: var(--col-3); font-style: italic; }

bg-tile.tappable {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform 80ms;
}

bg-tile.tappable:active { transform: scale(.88); }

/* ── Composing tray ───────────────────────────────────────────────────────── */

sg-tray {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  background: var(--col-1);
  border-radius: 10px;
  padding: 8px 10px;
}

.tray-x {
  flex-shrink: 0;
  padding: 0 14px;
  height: var(--tile-sz);
  background: var(--col-2);
  color: var(--col-3);
  font-size: .8rem;
  border-radius: var(--radius);
  white-space: nowrap;
}

.tray-inner {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: var(--tile-gap);
  min-height: var(--tile-sz);
}

.tray-hint {
  font-size: .8rem;
  color: var(--col-3);
  font-style: italic;
  white-space: nowrap;
}

.tray-tile {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tile-sz);
  height: var(--tile-sz);
  font-size: 1.05rem;
  font-weight: 800;
  text-transform: uppercase;
  border-radius: var(--radius);
  background: var(--col-claimed);
  color: #fff;
  cursor: grab;
  user-select: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.tray-tile::before { content: attr(letter); }

/* tiles sourced from a claimed word get a golden tint */
.tray-tile.from-word { background: var(--col-from-word); }

.tray-tile.tray-dragging { visibility: hidden; }

/* ghost follows the pointer during drag */
.tray-ghost {
  border-radius: var(--radius);
  font-size: 1.05rem;
  font-weight: 800;
  text-transform: uppercase;
  background: var(--col-claimed);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grabbing;
  box-shadow: 0 6px 20px rgba(0,0,0,.3);
}

.tray-ghost::before { content: attr(letter); }
.tray-ghost.from-word { background: var(--col-from-word); }

.tray-claim {
  flex-shrink: 0;
  padding: 0 14px;
  height: var(--tile-sz);
  background: var(--col-2);
  color: var(--col-3);
  font-size: .8rem;
  border-radius: var(--radius);
  white-space: nowrap;
  transition: background 120ms, color 120ms;
}

.tray-claim.valid {
  background: var(--col-success);
  color: #fff;
}

/* ── Draw + Done buttons ──────────────────────────────────────────────────── */

#bottom-actions { display: flex; gap: 8px; }

#draw-btn {
  flex: 1;
  padding: 13px;
  background: var(--col-dark);
  color: #fff;
  font-size: .9rem;
  border-radius: 6px;
}

#done-btn {
  padding: 13px 16px;
  background: transparent;
  color: var(--col-3);
  border: 2px solid var(--col-2);
  font-size: .8rem;
  border-radius: 6px;
}

.status-msg {
  font-size: .8rem;
  font-weight: 600;
  min-height: 1em;
  text-align: center;
}

.status-msg.success { color: var(--col-success); }
.status-msg.error   { color: var(--col-error); }

/* ── Result screen ────────────────────────────────────────────────────────── */

#result-screen {
  flex: 1;
  overflow: hidden;
  padding: 32px 24px 24px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

#result-screen h2 { font-size: 1.6rem; font-weight: 800; letter-spacing: .12em; }
.big-score { font-size: 5rem; font-weight: 800; line-height: 1; }
.score-lbl { font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .12em; color: var(--col-3); }
#final-time { font-size: .85rem; color: var(--col-3); }
#final-words {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  width: 100%;
  text-align: left;
  border-top: 1px solid var(--col-2);
}

.result-word {
  display: flex;
  justify-content: space-between;
  padding: 10px 4px;
  border-bottom: 1px solid var(--col-2);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: .06em;
}

.rw-pts { color: var(--col-success); }
.no-words { color: var(--col-3); font-style: italic; padding: 16px 4px; font-size: .9rem; }
.final-rank { font-size: .9rem; color: var(--col-3); font-weight: 600; min-height: 1.2em; }

/* ── Profile screen ───────────────────────────────────────────────────────── */

#profile-screen {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 32px 24px 24px;
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

.screen-back {
  align-self: flex-start;
  background: transparent;
  color: var(--col-3);
  font-size: 1.2rem;
  padding: 4px 8px 4px 0;
}

.screen-title { font-size: 1.6rem; font-weight: 800; letter-spacing: .06em; }
.screen-sub   { color: var(--col-3); font-size: .875rem; margin-top: -12px; }

.profile-field { display: flex; flex-direction: column; gap: 8px; width: 100%; }

/* flag-field expands to fill remaining space, with its scroll inside */
.flag-field { flex: 1; min-height: 0; }

.flag-grid-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  border: 1px solid var(--col-2);
  border-radius: 8px;
  padding: 8px;
  height: 100%;
}

.field-label {
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--col-3);
}

.field-input {
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 600;
  padding: 12px 14px;
  border: 2px solid var(--col-2);
  border-radius: var(--radius);
  outline: none;
  width: 100%;
  transition: border-color 80ms;
}
.field-input:focus { border-color: var(--col-dark); }

.flag-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
  gap: 6px;
}

.flag-btn {
  font-size: 1.5rem;
  padding: 6px;
  background: transparent;
  border-radius: 8px;
  border: 2px solid transparent;
  transition: border-color 80ms, background 80ms;
  line-height: 1;
}

.flag-btn:hover   { background: var(--col-1); }
.flag-btn.selected { border-color: var(--col-dark); background: var(--col-1); }

/* ── Leaderboard screen ───────────────────────────────────────────────────── */

#leaderboard-screen {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
  padding: 24px 0 0;
}

.lb-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 24px 16px;
}

.lb-tabs {
  display: flex;
  gap: 4px;
  padding: 0 24px 12px;
  flex-shrink: 0;
}

.lb-tab {
  flex: 1;
  padding: 8px 4px;
  background: transparent;
  color: var(--col-dark);
  border: 2px solid var(--col-2);
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .05em;
  border-radius: 4px;
}

.lb-tab.active {
  background: var(--col-dark);
  color: #fff;
  border-color: var(--col-dark);
}

.lb-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 24px 24px;
}

.lb-entry {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--col-2);
  font-size: .95rem;
}

.lb-entry.lb-me { background: var(--col-1); margin: 0 -8px; padding: 10px 8px; border-radius: 6px; }

.lb-rank  { width: 28px; font-size: .85rem; font-weight: 700; color: var(--col-3); flex-shrink: 0; }
.lb-flag  { font-size: 1.3rem; flex-shrink: 0; }
.lb-name  { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { font-weight: 800; flex-shrink: 0; }

.lb-loading, .lb-empty {
  color: var(--col-3);
  font-size: .875rem;
  font-style: italic;
  text-align: center;
  padding: 32px 0;
}

/* ── Animations ───────────────────────────────────────────────────────────── */

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

.shake { animation: shake .38s ease; }
