*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg: #130f24;
  --bg-card: #1c1735;
  --bg-cell: #1e1740;
  --border: #2a2450;
  --text: #e8e4f0;
  --text-dim: #7a7394;
  --green: #2ecc71;
  --purple: #c850c0;
  --orange: #e8891d;
  --tw-color: #d63031;
  --dw-color: #e17055;
  --tl-color: #00b894;
  --dl-color: #0984e3;
  --star-color: #6c5ce7;
  --tile-bg: #e8891d;
  --tile-text: #ffffff;
  --tile-selected: #2ecc71;
  --tile-bot: #a29bfe;
  --cell-size: min(calc((100vw - 40px) / 11), 42px);
  --rack-gap: 6px;
  --rack-tile-size: min(calc((100vw - 64px) / 7), calc(var(--cell-size) * 1.18));
}

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
}

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  min-height: 100dvh;
  overflow: hidden;
}

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

/* ---------- HEADER ---------- */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 4px;
  flex-shrink: 0;
}

.game-title {
  font-family: 'Outfit', sans-serif;
  font-size: 22px;
  font-weight: 800;
  background: linear-gradient(135deg, #2ecc71, #00b894);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  line-height: 1.2;
}

.game-tagline {
  font-family: 'Outfit', sans-serif;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 2px;
  color: #e17055;
  margin-top: 1px;
}

.new-btn {
  font-family: 'Outfit', sans-serif;
  font-size: 14px;
  font-weight: 700;
  padding: 8px 20px;
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
}

.new-btn:active {
  transform: scale(0.95);
  background: var(--border);
}

/* ---------- SCOREBOARD ---------- */
#scoreboard {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px 6px;
  flex-shrink: 0;
}

.score-col {
  text-align: center;
  flex: 1;
}

.score-label {
  font-family: 'Outfit', sans-serif;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1.5px;
  margin-bottom: 2px;
}

.player-col .score-label { color: var(--green); }
.tiles-col .score-label { color: var(--text-dim); }
.bot-col .score-label { color: var(--purple); }

.score-value {
  font-family: 'Outfit', sans-serif;
  font-size: 32px;
  font-weight: 900;
  line-height: 1;
}

.player-col .score-value { color: var(--text); }
.tiles-col .score-value { color: var(--text); }
.bot-col .score-value { color: var(--text); }

.turn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin: 4px auto 0;
  opacity: 0;
  transition: opacity 0.3s;
}

.turn-dot.active {
  opacity: 1;
}

#player-dot { background: var(--green); box-shadow: 0 0 8px var(--green); }
#bot-dot { background: var(--purple); box-shadow: 0 0 8px var(--purple); }

/* ---------- BOARD ---------- */
#board-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 8px;
  overflow: hidden;
}

#board {
  display: grid;
  grid-template-columns: repeat(11, var(--cell-size));
  grid-template-rows: repeat(11, var(--cell-size));
  gap: 2px;
  border-radius: 8px;
  overflow: hidden;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--bg-cell);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
  user-select: none;
  border-radius: 4px;
}

.cell:active {
  transform: scale(0.95);
}

.cell .premium-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.1;
  font-family: 'Outfit', sans-serif;
  font-weight: 700;
  pointer-events: none;
}

.cell .premium-label .mult {
  font-size: calc(var(--cell-size) * 0.3);
}

.cell .premium-label .type {
  font-size: calc(var(--cell-size) * 0.22);
  text-transform: uppercase;
}

.cell.tw {
  background: var(--tw-color);
  color: white;
}

.cell.dw {
  background: var(--dw-color);
  color: white;
}

.cell.tl {
  background: var(--tl-color);
  color: white;
}

.cell.dl {
  background: var(--dl-color);
  color: white;
}

.cell.star {
  background: var(--star-color);
  color: white;
  font-size: calc(var(--cell-size) * 0.45);
}

.cell.highlight {
  box-shadow: inset 0 0 0 2px var(--green), 0 0 8px rgba(46,204,113,0.3);
}

/* Tiles on board */
.cell .tile {
  position: absolute;
  inset: 1px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Outfit', sans-serif;
  font-size: calc(var(--cell-size) * 0.5);
  font-weight: 800;
  z-index: 2;
}

.cell .tile.player-tile,
.cell .tile.locked-tile {
  background: var(--tile-bg);
  color: var(--tile-text);
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.cell .tile.placed-tile {
  background: var(--tile-bg);
  color: var(--tile-text);
  border: 2px solid var(--green);
  box-shadow: 0 0 10px rgba(46,204,113,0.4);
  cursor: pointer;
}

.cell .tile.bot-locked {
  background: var(--tile-bot);
  color: #1a1a2e;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.cell .tile .points {
  position: absolute;
  bottom: 1px;
  right: 2px;
  font-size: calc(var(--cell-size) * 0.2);
  font-weight: 600;
  opacity: 0.8;
}

/* ---------- RACK ---------- */
#rack-container {
  padding: 8px 12px 4px;
  flex-shrink: 0;
}

#rack {
  display: flex;
  justify-content: center;
  gap: var(--rack-gap);
  width: 100%;
  max-width: calc(var(--rack-tile-size) * 7 + 36px);
  margin: 0 auto;
}

.rack-tile {
  width: var(--rack-tile-size);
  height: var(--rack-tile-size);
  background: var(--tile-bg);
  color: var(--tile-text);
  border: none;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-family: 'Outfit', sans-serif;
  cursor: pointer;
  position: relative;
  transition: transform 0.15s, box-shadow 0.15s;
  user-select: none;
  box-shadow: 0 3px 8px rgba(0,0,0,0.4);
}

.rack-tile .letter {
  font-size: calc(var(--rack-tile-size) * 0.5);
  font-weight: 800;
  line-height: 1;
}

.rack-tile .points {
  font-size: calc(var(--rack-tile-size) * 0.2);
  font-weight: 600;
  opacity: 0.8;
  margin-top: 1px;
}

.rack-tile:active {
  transform: scale(0.93);
}

.rack-tile.selected {
  box-shadow: 0 0 0 3px var(--green), 0 3px 12px rgba(46,204,113,0.4);
  transform: translateY(-4px);
}

.rack-tile.empty {
  background: var(--bg-cell);
  box-shadow: none;
  cursor: default;
}

/* ---------- CONTROLS ---------- */
#controls {
  display: flex;
  gap: 5px;
  padding: 4px 12px calc(10px + env(safe-area-inset-bottom));
  justify-content: center;
  flex-shrink: 0;
}

.ctrl-btn {
  font-family: 'Outfit', sans-serif;
  font-size: clamp(10px, 2.6vw, 12px);
  font-weight: 700;
  letter-spacing: 0.5px;
  padding: 10px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.15s;
  flex: 1;
  max-width: none;
  min-width: 0;
}

.ctrl-btn:active {
  transform: scale(0.95);
}

.ctrl-btn.primary {
  background: var(--green);
  color: #000;
  border-color: var(--green);
  font-weight: 800;
}

.ctrl-btn.primary:disabled {
  background: var(--bg-card);
  color: var(--text-dim);
  border-color: var(--border);
  opacity: 0.4;
  cursor: default;
}

.ctrl-btn:not(:disabled):hover {
  border-color: var(--text-dim);
  color: var(--text);
}

/* ---------- MESSAGE BAR ---------- */
#message-bar {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--bg-card);
  border: 1px solid var(--green);
  color: var(--green);
  font-family: 'Outfit', sans-serif;
  font-size: 14px;
  font-weight: 700;
  padding: 14px 28px;
  border-radius: 12px;
  box-shadow: 0 0 30px rgba(46,204,113,0.2);
  z-index: 100;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
  text-align: center;
  max-width: calc(100vw - 24px);
}

#message-bar.show { opacity: 1; }

#message-bar.error {
  border-color: #d63031;
  color: #d63031;
  box-shadow: 0 0 30px rgba(214,48,49,0.2);
}

#message-bar.bot-msg {
  border-color: var(--purple);
  color: var(--purple);
  box-shadow: 0 0 30px rgba(200,80,192,0.2);
}

/* ---------- MODAL ---------- */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(19, 15, 36, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  backdrop-filter: blur(6px);
}

#modal-overlay.hidden { display: none; }

#modal {
  background: var(--bg-card);
  border: 1px solid var(--green);
  border-radius: 16px;
  padding: 36px;
  text-align: center;
  max-width: 320px;
  width: 90%;
  box-shadow: 0 0 50px rgba(46,204,113,0.15);
}

#modal h2 {
  font-family: 'Outfit', sans-serif;
  font-size: 22px;
  font-weight: 900;
  margin-bottom: 12px;
  color: var(--green);
}

#modal p {
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 24px;
  line-height: 1.6;
}

#modal .ctrl-btn {
  max-width: none;
  width: 100%;
  padding: 14px;
  font-size: 14px;
}

/* ---------- BLANK TILE PICKER ---------- */
#blank-picker-overlay {
  position: fixed;
  inset: 0;
  background: rgba(19, 15, 36, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 250;
  backdrop-filter: blur(6px);
}

#blank-picker-overlay.hidden { display: none; }

#blank-picker {
  background: var(--bg-card);
  border: 1px solid var(--green);
  border-radius: 16px;
  padding: 24px;
  text-align: center;
  width: 90%;
  max-width: 320px;
  box-shadow: 0 0 50px rgba(46,204,113,0.15);
}

#blank-picker h3 {
  font-family: 'Outfit', sans-serif;
  font-size: 16px;
  font-weight: 700;
  color: var(--green);
  margin-bottom: 16px;
}

#blank-picker-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

.blank-letter-btn {
  font-family: 'Outfit', sans-serif;
  font-size: 16px;
  font-weight: 700;
  padding: 10px 0;
  background: var(--bg-cell);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.12s;
}

.blank-letter-btn:active {
  transform: scale(0.9);
  background: var(--green);
  color: #000;
}

/* ---------- ANIMATIONS ---------- */
@keyframes popIn {
  0% { transform: scale(0); opacity: 0; }
  70% { transform: scale(1.08); }
  100% { transform: scale(1); opacity: 1; }
}

.tile-animate {
  animation: popIn 0.3s ease-out;
}

.cell .tile.bot-placing {
  animation: popIn 0.4s ease-out;
}

/* ---------- RESPONSIVE ---------- */
@media (max-height: 680px) {
  :root {
    --cell-size: min(calc((100vw - 40px) / 11), 30px);
    --rack-tile-size: min(calc((100vw - 56px) / 7), calc(var(--cell-size) * 1.12));
  }
  #header { padding: 6px 12px 2px; }
  .game-title { font-size: 18px; }
  .game-tagline { font-size: 8px; }
  #scoreboard { padding: 4px 12px; }
  .score-value { font-size: 24px; }
  #controls { padding: 2px 12px 6px; }
  .ctrl-btn { padding: 8px 10px; font-size: 11px; }
  #rack-container { padding: 6px 12px 2px; }
}

@media (min-height: 850px) {
  :root {
    --cell-size: min(calc((100vw - 40px) / 11), 48px);
    --rack-tile-size: min(calc((100vw - 64px) / 7), calc(var(--cell-size) * 1.12));
  }
  .score-value { font-size: 36px; }
}

@media (min-width: 481px) {
  :root {
    --cell-size: min(calc((480px - 40px) / 11), 42px);
    --rack-tile-size: min(calc((480px - 64px) / 7), calc(var(--cell-size) * 1.18));
  }
}
