:root {
  --bg: #f4f1ea;
  --card: #fffdf8;
  --ink: #2e294e;
  --muted: #7a768c;
  --line: #ddd8cb;
  --cell: #e9e5d9;
  --accent: #1b998b;
  --danger: #c8553d;
  --DL: #a9d3ea;
  --TL: #3fb8af;
  --DW: #f3c05a;
  --TW: #e8704f;
  --Lx: #2c7fb8;  /* letter multipliers above 3 */
  --Wx: #b5345f;  /* word multipliers above 3 */
  --tile: #453d78;
  --tile-ink: #fdfaf2;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 1.4rem; margin: 0 0 .8rem; }
h2 { font-size: 1rem; margin: 0 0 .5rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.muted { color: var(--muted); font-size: .9rem; }
.center { text-align: center; }
[hidden] { display: none !important; }

.topbar {
  display: flex; flex-wrap: wrap; gap: .5rem; justify-content: space-between; align-items: center;
  padding: .7rem 1rem; background: var(--ink); color: var(--tile-ink);
}
.brand { color: inherit; text-decoration: none; font-weight: 800; font-size: 1.15rem; letter-spacing: .02em; }
.brand span { color: var(--DW); font-weight: 400; font-style: italic; }
.who { display: flex; gap: .8rem; align-items: center; margin: 0; font-size: .9rem; }
.topbar a.button {
  padding: .35rem .8rem; border-radius: 8px; white-space: nowrap;
  background: var(--card); color: var(--ink); font-weight: 600; text-decoration: none;
}
.topbar a.button:hover { background: #fff; }

main { max-width: 1000px; margin: 0 auto; padding: 1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 1.2rem; margin-bottom: 1rem; }
.narrow { max-width: 420px; margin: 2rem auto; }
.flash { background: #fdf0c8; border: 1px solid var(--DW); border-radius: 8px; padding: .6rem .9rem; }

button {
  font: inherit; font-weight: 600; cursor: pointer;
  padding: .55rem .9rem; border-radius: 8px;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
}
button:hover:not(:disabled) { border-color: var(--ink); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--danger); }
button.link { background: none; border: 0; padding: 0; color: var(--DW); text-decoration: underline; }

input[type=email], input[type=password], input[type=text] {
  font: inherit; width: 100%; padding: .55rem .7rem;
  border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink);
}
.stack > div { margin-bottom: .9rem; }
.stack label, .challenge label { display: block; font-weight: 600; margin-bottom: .25rem; }
.stack .helptext, .stack ul { color: var(--muted); font-size: .8rem; margin: .3rem 0 0; padding-left: 1.1rem; }
.errorlist { color: var(--danger); list-style: none; padding: 0; margin: .3rem 0; font-size: .9rem; }
.challenge .row { display: flex; gap: .5rem; }
.challenge .row button { white-space: nowrap; }

.games { list-style: none; margin: 0; padding: 0; }
.games a {
  display: flex; gap: 1rem; align-items: center; padding: .65rem .4rem;
  border-top: 1px solid var(--line); text-decoration: none; color: var(--ink);
}
.games li:first-child a { border-top: 0; }
.games a:hover { background: var(--bg); }
.games .opp { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.games .pts { font-weight: 400; color: var(--muted); }
.games .score { font-variant-numeric: tabular-nums; }
.result { font-size: .8rem; font-weight: 700; padding: .1rem .5rem; border-radius: 99px; background: var(--cell); }
.result.won { background: var(--TL); color: #fff; }
.result.lost { background: var(--TW); color: #fff; }
.result.tie { background: var(--DW); }

/* ---- game ---- */
.game { display: grid; grid-template-columns: minmax(0, 620px) minmax(180px, 1fr); gap: 1.2rem; align-items: start; }
@media (max-width: 820px) { .game { grid-template-columns: 1fr; } main { padding: .5rem; } }

.scores { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .5rem; }
.player {
  min-width: 0; display: flex; justify-content: space-between; align-items: center; gap: .5rem;
  padding: .4rem .7rem; border-radius: 10px; background: var(--card); border: 2px solid var(--line);
}
.player.active { border-color: var(--accent); }
.player.resigned { opacity: .55; }
.player .who { min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 0; font-size: 1rem; }
.player .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.player .dot { display: inline-block; width: .7em; height: .7em; border-radius: 2px; margin-right: .35em; }
.player .note { font-size: .75rem; color: var(--muted); }
.player .points { font-size: 1.4rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.status { margin: .6rem 0; font-weight: 600; }
.status.over { color: var(--danger); }

.board {
  display: grid; grid-template-columns: repeat(var(--cols, 15), 1fr); gap: 2px;
  padding: 4px; background: var(--ink); border-radius: 10px;
  container-type: inline-size;
}
.cell {
  position: relative; aspect-ratio: 1; background: var(--cell); border-radius: 3px;
  display: grid; place-items: center; user-select: none;
}
.cell.L2 { background: var(--DL); }
.cell.L3 { background: var(--TL); }
.cell.W2 { background: var(--DW); }
.cell.W3 { background: var(--TW); }
.cell.Lx { background: var(--Lx); } .cell.Wx { background: var(--Wx); }
.cell.Lx .label, .cell.Wx .label { color: #fff; }
.cell.center { background: var(--ink); outline: 2px solid var(--DW); outline-offset: -2px; }
.cell.center .label { color: var(--DW); font-size: calc(51cqw / var(--cols, 15)); }
.cell .label { font-size: calc(31.5cqw / var(--cols, 15)); font-weight: 700; color: rgba(46, 41, 78, .75); }

.tile {
  position: relative; display: grid; place-items: center;
  background: var(--tile); color: var(--tile-ink); border-radius: 4px; /* --tile is set per player */
  font-weight: 800; user-select: none;
}
.cell .tile { position: absolute; inset: 0; font-size: calc(54cqw / var(--cols, 15)); border-radius: 3px; }
.tile .value { position: absolute; right: 8%; bottom: 2%; font-size: .5em; font-weight: 600; opacity: .85; }
.tile.blank .letter { color: var(--DW); }
.tile.last { box-shadow: inset 0 0 0 2px var(--DW); }
.tile.tentative { cursor: pointer; box-shadow: inset 0 0 0 2px var(--tile-ink); }
/* Tiles that can be dragged: stop the page scrolling or selecting text under a finger. */
.rack .tile, .tile.tentative { touch-action: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.tile.dragging { opacity: .3; }
.tile.ghost {
  position: fixed; inset: auto; z-index: 100; pointer-events: none; margin: 0;
  transform: scale(1.15); box-shadow: 0 6px 14px rgba(0, 0, 0, .35);
}

.rack {
  display: flex; justify-content: center; gap: 6px; min-height: 58px;
  margin: .8rem 0 .3rem; padding: 6px; background: var(--line); border-radius: 10px;
}
.rack .tile { width: 46px; height: 46px; font-size: 1.5rem; cursor: pointer; transition: transform .1s; }
.rack .tile.selected { transform: translateY(-6px); box-shadow: 0 0 0 3px var(--DW); }
.rack .tile.swapping { transform: translateY(-6px); box-shadow: 0 0 0 3px var(--danger); opacity: .6; }
@media (max-width: 420px) { .rack .tile { width: 40px; height: 40px; font-size: 1.3rem; } .rack { gap: 4px; } }

.message { min-height: 1.4em; margin: .3rem 0; text-align: center; font-size: .95rem; color: var(--muted); }
.message.error { color: var(--danger); font-weight: 600; }
.controls { display: flex; flex-wrap: wrap; gap: .4rem; justify-content: center; }

.side { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 1rem; }
.videos { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }
.videos figure { margin: 0; position: relative; background: var(--ink); border-radius: 8px; overflow: hidden; aspect-ratio: 4 / 3; }
.videos video { width: 100%; height: 100%; object-fit: cover; display: block; }
.videos figcaption {
  position: absolute; left: 0; bottom: 0; padding: 1px 6px; font-size: .75rem;
  color: #fff; background: rgba(0, 0, 0, .55); border-top-right-radius: 6px;
}
.colors { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 1.2rem; }
.colors button { width: 30px; height: 30px; padding: 0; border-radius: 6px; border: 2px solid transparent; }
.colors button.current { border-color: var(--card); box-shadow: 0 0 0 2px var(--ink); }
#call-info { margin: .5rem 0; }
#btn-call { margin-bottom: 1.2rem; }
.chat-log {
  list-style: none; margin: 0 0 .5rem; padding: .4rem .6rem; max-height: 220px; overflow-y: auto;
  border: 1px solid var(--line); border-radius: 8px; background: #fff; font-size: .9rem;
}
.chat-log li { padding: .15rem 0; overflow-wrap: anywhere; }
.chat-log li.muted { font-size: .85rem; }
.chat-log b { margin-right: .35rem; }
.chat-log time { color: var(--muted); font-size: .75rem; margin-left: .4rem; white-space: nowrap; }
.chat-form { display: flex; gap: .4rem; margin: 0; }
.chat-form input { min-width: 0; }
#chat-error { margin: .3rem 0 1.2rem; min-height: 0; }
.history { margin: 0 0 1.2rem; padding-left: 0; list-style: none; font-size: .9rem; }
.history li { padding: .3rem 0; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
.legend { list-style: none; padding: 0; margin: 0 0 1rem; font-size: .85rem; }
.legend i { display: inline-block; width: .9em; height: .9em; border-radius: 2px; vertical-align: -1px; margin-right: .3rem; }
.legend .L2 { background: var(--DL); } .legend .L3 { background: var(--TL); } .legend .Lx { background: var(--Lx); }
.legend .W2 { background: var(--DW); } .legend .W3 { background: var(--TW); } .legend .Wx { background: var(--Wx); }
.rules { margin: 0 0 1rem; padding-left: 1.1rem; font-size: .85rem; color: var(--muted); }
.rules li { margin-bottom: .3rem; }
.rules-choice { margin-top: .8rem; }
.rules-choice select { font: inherit; padding: .45rem .6rem; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); }
.games .variation { font-size: .8rem; color: var(--muted); }
.who a { color: var(--DW); }
.tile.takeable { cursor: pointer; animation: takeable 1.2s ease-in-out infinite alternate; }
@keyframes takeable { from { box-shadow: inset 0 0 0 2px var(--DW); } to { box-shadow: inset 0 0 0 2px var(--tile-ink), 0 0 6px 2px var(--DW); } }

dialog { border: 0; border-radius: 12px; padding: 1.2rem; max-width: 340px; color: var(--ink); }
dialog::backdrop { background: rgba(46, 41, 78, .5); }
.picker-letters { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: .8rem; }
.picker-letters button { padding: .4rem 0; }
