:root {
  --paper: #f3f0e8;
  --paper-raised: #fbf9f3;
  --ink: #1c2a3a;
  --ink-soft: #4a5568;
  --rule: #d8d2c2;
  --gold: #a97a1f;
  --gold-deep: #8a6318;
  --clay: #963c2e;
  --green: #3c5c47;

  --board-bg: #2f5a45;
  --square: #e9e2cf;
  --tw: #b4472f;
  --dw: #e2a99a;
  --tl: #2f6f93;
  --dl: #a8cfe0;
  --tile: #f6e7bd;
  --tile-edge: #c9a45a;
  --tile-new: #fff2c4;

  --cell: min(calc((100vw - 32px - 6px) / 15), 40px);
  --radius: 4px;
  font-size: 16px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: "Source Sans 3", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.top { border-bottom: 1px solid var(--rule); background: var(--paper-raised); }
.top-inner { max-width: 720px; margin: 0 auto; padding: 12px 16px 14px; }
.back { margin: 0 0 6px; font-size: 0.9rem; }
.back a { color: var(--gold-deep); text-decoration: none; }
.back a:hover { text-decoration: underline; }
.title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
h1 { font-family: "Fraunces", Georgia, serif; font-size: 1.6rem; margin: 0; line-height: 1.15; }
.top-actions { display: flex; gap: 8px; }
.subhead { margin: 6px 0 0; color: var(--ink-soft); font-size: 0.95rem; }

.game { max-width: 720px; margin: 0 auto; padding: 14px 16px 40px; }

/* Buttons */
.btn {
  font: inherit; font-weight: 600; font-size: 0.95rem;
  padding: 9px 14px; border-radius: var(--radius);
  border: 1px solid var(--rule); background: var(--paper-raised); color: var(--ink);
  cursor: pointer; min-height: 40px;
}
.btn:hover:not(:disabled) { border-color: var(--gold); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn-primary:hover:not(:disabled) { background: #2f4b39; border-color: #2f4b39; }
.btn-ghost { background: transparent; }
.btn:focus-visible, .cell:focus-visible, .rack-tile:focus-visible, .letter-grid button:focus-visible {
  outline: 3px solid var(--gold); outline-offset: 1px;
}

/* Scoreboard */
.scoreboard { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; align-items: stretch; margin-bottom: 8px; }
.player-card {
  display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
  padding: 8px 12px; border: 2px solid var(--rule); border-radius: var(--radius);
  background: var(--paper-raised); min-width: 0;
}
.player-card.is-active { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(169, 122, 31, 0.2); }
.player-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.player-score { font-family: "Fraunces", Georgia, serif; font-size: 1.5rem; font-weight: 700; }
.bag-count { grid-column: 1 / -1; grid-row: 2; font-size: 0.85rem; color: var(--ink-soft); text-align: center; }

.status { min-height: 1.5em; margin: 6px 0 10px; font-weight: 600; }
.status.is-error { color: var(--clay); }

/* Board */
.board-wrap { display: flex; justify-content: center; }
.board {
  display: grid;
  grid-template-columns: repeat(15, var(--cell));
  grid-template-rows: repeat(15, var(--cell));
  gap: 1px; padding: 2px;
  background: var(--board-bg); border-radius: 6px;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.cell {
  position: relative; display: flex; align-items: center; justify-content: center;
  padding: 0; margin: 0; border: 0; border-radius: 2px;
  background: var(--square); color: var(--ink-soft);
  font: 700 calc(var(--cell) * 0.3) "Source Sans 3", sans-serif;
  cursor: pointer; min-width: 0; overflow: hidden;
}
.cell.p-TW { background: var(--tw); color: #fff; }
.cell.p-DW { background: var(--dw); color: #5a2418; }
.cell.p-TL { background: var(--tl); color: #fff; }
.cell.p-DL { background: var(--dl); color: #123a50; }
.cell .star { font-size: calc(var(--cell) * 0.6); line-height: 1; }
.cell.is-drop { outline: 3px solid var(--gold); outline-offset: -3px; }

.tile {
  position: relative; display: flex; align-items: center; justify-content: center;
  width: 100%; height: 100%;
  background: var(--tile); color: var(--ink);
  border: 1px solid var(--tile-edge); border-radius: 3px;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12);
  font: 700 calc(var(--cell) * 0.58) "Fraunces", Georgia, serif;
  text-transform: uppercase; line-height: 1;
}
.tile .pts {
  position: absolute; right: 8%; bottom: 4%;
  font: 600 calc(var(--cell) * 0.24) "Source Sans 3", sans-serif;
}
.tile.is-blank { color: var(--clay); }
.tile.is-blank .pts { display: none; }
.tile.is-pending { background: var(--tile-new); border-color: var(--gold); box-shadow: 0 0 0 2px var(--gold); }
.tile.is-last { box-shadow: 0 0 0 2px var(--green); }
.tile.is-hint { opacity: 0.55; border-style: dashed; border-color: var(--green); background: #e6f1e4; }

.preview { min-height: 1.4em; margin: 10px 0 0; font-size: 0.95rem; color: var(--ink-soft); text-align: center; }
.preview.is-error { color: var(--clay); }

/* Rack */
.rack-area { margin-top: 10px; padding: 12px; border: 1px solid var(--rule); border-radius: var(--radius); background: var(--paper-raised); }
.rack {
  display: flex; justify-content: center; gap: 6px; min-height: 52px;
  padding: 8px; border-radius: var(--radius); background: #8a6a3c;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.35);
}
.rack.is-drop { outline: 3px solid var(--gold); }
.rack-tile {
  position: relative; width: min(46px, 11.5vw); height: min(46px, 11.5vw);
  padding: 0; cursor: grab; touch-action: none;
  background: var(--tile); border: 1px solid var(--tile-edge); border-radius: 4px;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
  font: 700 min(1.6rem, 6.2vw) "Fraunces", Georgia, serif; color: var(--ink);
  text-transform: uppercase;
}
.rack-tile .pts { position: absolute; right: 7%; bottom: 3%; font: 600 0.62rem "Source Sans 3", sans-serif; }
.rack-tile.is-blank { color: var(--clay); }
.rack-tile.is-blank .pts { display: none; }
.rack-tile.is-selected { transform: translateY(-6px); box-shadow: 0 0 0 3px var(--gold), 0 6px 0 rgba(0, 0, 0, 0.25); }
.rack-tile.is-exchange { background: #f3d2c9; border-color: var(--clay); }
.rack.is-hidden .rack-tile { color: transparent; background: #cdb98a; }
.rack.is-hidden .rack-tile .pts { display: none; }

.drag-ghost {
  position: fixed; z-index: 1000; pointer-events: none;
  width: 44px; height: 44px; margin: -22px 0 0 -22px; opacity: 0.9;
}
.drag-ghost .tile { font-size: 1.5rem; box-shadow: 0 6px 14px rgba(0, 0, 0, 0.4); }

.actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 12px; }

/* Log */
.log-area { margin-top: 18px; }
.log-area h2 { font-family: "Fraunces", Georgia, serif; font-size: 1.1rem; margin: 0 0 6px; }
.log { margin: 0; padding: 0; list-style: none; max-height: 220px; overflow: auto; border-top: 1px solid var(--rule); }
.log li { padding: 6px 2px; border-bottom: 1px solid var(--rule); font-size: 0.95rem; }
.log li a { color: var(--gold-deep); font-weight: 600; text-decoration: none; }
.log li a:hover { text-decoration: underline; }
.log li.bingo { color: var(--green); font-weight: 600; }

.legend { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px 14px; list-style: none; margin: 16px 0 0; padding: 0; font-size: 0.82rem; color: var(--ink-soft); }
.legend li { display: flex; align-items: center; gap: 6px; }
.legend .swatch { display: inline-block; width: 18px; height: 18px; border-radius: 2px; }

/* Dialogs */
dialog {
  border: 1px solid var(--rule); border-radius: 8px; padding: 0;
  background: var(--paper-raised); color: var(--ink);
  width: min(460px, calc(100vw - 24px)); max-height: calc(100vh - 24px);
  box-shadow: 0 20px 50px rgba(28, 42, 58, 0.35);
}
dialog::backdrop { background: rgba(28, 42, 58, 0.55); }
.dlg-body { padding: 18px 18px 16px; overflow: auto; max-height: calc(100vh - 28px); }
.dlg-body.center { text-align: center; }
.dlg-body h2 { font-family: "Fraunces", Georgia, serif; font-size: 1.3rem; margin: 0 0 10px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.dlg-body.center .dlg-actions { justify-content: center; }
.help { color: var(--ink-soft); margin: 0 0 10px; font-size: 0.95rem; }
fieldset { border: 1px solid var(--rule); border-radius: var(--radius); margin: 0 0 12px; padding: 6px 12px 8px; }
legend { font-weight: 700; padding: 0 6px; }
.choice { display: flex; align-items: center; gap: 8px; padding: 6px 0; cursor: pointer; }
.choice input { width: 18px; height: 18px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-weight: 600; }
.field input { font: inherit; padding: 9px 10px; border: 1px solid var(--rule); border-radius: var(--radius); background: #fff; }
.letter-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; }
.letter-grid button {
  font: 700 1.25rem "Fraunces", Georgia, serif; text-transform: uppercase;
  padding: 8px 0; border: 1px solid var(--tile-edge); background: var(--tile); border-radius: 4px; cursor: pointer; color: var(--ink);
}
.letter-grid button:hover { background: var(--tile-new); }
.rack-small { background: transparent; box-shadow: none; flex-wrap: wrap; }
.rules { margin: 0; padding-left: 1.2em; }
.rules li { margin-bottom: 8px; }
.winner { font-family: "Fraunces", Georgia, serif; font-size: 1.3rem; font-weight: 700; margin: 4px 0 10px; color: var(--green); }
.final { width: 100%; border-collapse: collapse; margin: 6px 0 0; }
.final th, .final td { padding: 6px 8px; border-bottom: 1px solid var(--rule); text-align: right; }
.final th:first-child, .final td:first-child { text-align: left; }

@media (max-width: 480px) {
  h1 { font-size: 1.35rem; }
  .btn { padding: 8px 11px; font-size: 0.9rem; }
  .player-score { font-size: 1.25rem; }
}
@media (prefers-reduced-motion: no-preference) {
  .rack-tile { transition: transform 0.12s ease; }
}

.tile.is-pending { touch-action: none; cursor: grab; }
.legend .swatch.cell { cursor: default; font-size: 0; }

/* Remote play */
.net-note { margin: -4px 0 8px; padding: 6px 10px; border-radius: var(--radius); background: #f6e2dc; color: var(--clay); font-size: 0.9rem; font-weight: 600; }
.player-card.is-away { opacity: 0.6; }
.form-error { margin: 0 0 8px; color: var(--clay); font-weight: 600; }
.code-input { text-transform: uppercase; letter-spacing: 0.25em; font-weight: 700; text-align: center; }
.big-code { font-family: "Fraunces", Georgia, serif; font-size: 2.4rem; font-weight: 700; letter-spacing: 0.2em; margin: 6px 0 12px; color: var(--green); }
.share-row { display: flex; gap: 8px; }
.share-row input { flex: 1; min-width: 0; font: inherit; font-size: 0.85rem; padding: 8px 10px; border: 1px solid var(--rule); border-radius: var(--radius); background: #fff; }
