:root {
  --bg: #1c1d25;
  --panel: #262731;
  --panel-2: #30313d;
  --panel-3: #3a3b48;
  --line: #3d3e4b;
  --text: #ecedf3;
  --muted: #9c9db0;
  --cell-bg: #c6c7d1;
  --tw: #c64f4e;
  --dw: #e5a3a2;
  --tl: #135f90;
  --dl: #6aa4c5;
  --tile: #fde1ab;
  --tile-edge: #e9c27e;
  --tile-text: #1d1d24;
  --accent: #58b9c1;
  --accent-dark: #2f8e96;
  --valid: #2b50e0;
  --invalid: #e23b3b;
  --danger: #e06a6a;

  --gap: 3px;
  --cell: clamp(24px, min(calc((100vh - 285px) / 16.2), calc((100vw - 470px) / 16.6)), 58px);
  --rack-tile: calc(var(--cell) * 1.18);
  --radius: 6px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-size: 14px; }
button { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.layout {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 24px;
  padding: 16px;
  min-height: 100%;
}

/* ---------- board ---------- */

.board-wrap {
  display: grid;
  grid-template-columns: 24px auto;
  grid-template-rows: 24px auto;
}
.coords { display: grid; gap: var(--gap); color: var(--muted); font-weight: 600; font-size: 13px; }
.coords-top { grid-template-columns: repeat(15, var(--cell)); }
.coords-left { grid-template-rows: repeat(15, var(--cell)); }
.coords > div { display: grid; place-items: center; }

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(15, var(--cell));
  grid-template-rows: repeat(15, var(--cell));
  gap: var(--gap);
}
.cell {
  position: relative;
  background: var(--cell-bg);
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: calc(var(--cell) * 0.34);
  color: #fff;
  user-select: none;
  cursor: pointer;
}
.cell.TW { background: var(--tw); }
.cell.DW, .cell.ST { background: var(--dw); }
.cell.TL { background: var(--tl); }
.cell.DL { background: var(--dl); }
.cell.ST::before { content: "★"; font-size: calc(var(--cell) * 0.5); color: #fff; }
.cell.filled::before { content: none; }
.cell.drop-hover { outline: 3px solid var(--accent); outline-offset: -3px; }
.cell .cursor {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: #2d2e39;
  border-radius: var(--radius);
  color: #fff;
}
.cell .cursor svg { width: 55%; height: 55%; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- tiles ---------- */

.tile {
  position: relative;
  width: var(--cell);
  height: var(--cell);
  border-radius: var(--radius);
  background: var(--tile);
  box-shadow: inset 0 -3px 0 var(--tile-edge);
  color: var(--tile-text);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.56);
  line-height: 1;
  user-select: none;
  touch-action: none;
}
.tile .v {
  position: absolute;
  right: 9%;
  bottom: 8%;
  font-size: calc(var(--cell) * 0.24);
  font-weight: 700;
}
.tile.blank .l { color: #b0402f; }
.tile.blank-unset .l { color: #a08a62; }
.tile.last { box-shadow: inset 0 -3px 0 var(--tile-edge), 0 0 0 2px #f2b84b; }
.tile.draft { cursor: grab; background: #fff3d9; }
.tile.dragging { opacity: 0.25; }
.tile.ghost {
  position: fixed;
  z-index: 100;
  pointer-events: none;
  transform: scale(1.08);
  box-shadow: inset 0 -3px 0 var(--tile-edge), 0 10px 24px rgba(0, 0, 0, 0.45);
}

/* word outlines + score badge from the live preview */
.overlay { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
.outline {
  position: absolute;
  border: 3px solid var(--valid);
  border-radius: calc(var(--radius) + 2px);
  margin: -3px;
}
.outline.bad { border-color: var(--invalid); }
.outline.cross { border-style: dashed; border-width: 2px; }
.badge {
  position: absolute;
  transform: translate(-45%, -45%);
  min-width: calc(var(--cell) * 0.6);
  height: calc(var(--cell) * 0.6);
  padding: 0 6px;
  border-radius: 999px;
  background: var(--valid);
  color: #fff;
  font-weight: 700;
  font-size: calc(var(--cell) * 0.3);
  display: grid;
  place-items: center;
  border: 2px solid #fff;
}
.badge.bad { background: var(--invalid); }

/* ---------- rack + actions ---------- */

.rack-bar {
  margin-top: 16px;
  margin-left: 24px;
  background: var(--panel);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.rack {
  flex: 1;
  min-height: calc(var(--rack-tile) + 20px);
  background: var(--panel-2);
  border-radius: 12px;
  padding: 10px;
  display: flex;
  justify-content: center;
  gap: 10px;
}
.rack .tile {
  --cell: var(--rack-tile);
  cursor: grab;
}
.rack .tile.selected { transform: translateY(-8px); box-shadow: inset 0 -3px 0 var(--tile-edge), 0 0 0 3px var(--accent); }
.rack .insert-marker { width: 4px; border-radius: 2px; background: var(--accent); margin: 0 -7px; }
.rack.exchange .tile { cursor: pointer; }
.rack-empty { color: var(--muted); align-self: center; }

.icon-btn {
  width: calc(var(--rack-tile) + 20px);
  height: calc(var(--rack-tile) + 20px);
  background: var(--panel-3);
  border: 0;
  border-radius: 12px;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: #454656; }
.icon-btn svg { width: 50%; height: 50%; fill: none; stroke: #fff; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

.status { margin: 10px 0 0 24px; min-height: 20px; color: var(--muted); text-align: center; }
.status.error { color: var(--danger); }

.actions {
  margin: 10px 0 0 24px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}
.btn {
  background: var(--panel-3);
  border: 0;
  border-radius: 10px;
  padding: 16px 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  display: inline-block;
  text-align: center;
  text-decoration: none;
}
.btn:hover:not(:disabled) { background: #454656; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn:disabled { opacity: 0.4; cursor: default; }
.actions .btn.active { outline: 2px solid var(--accent); }

/* ---------- side panel ---------- */

.side {
  width: 380px;
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 12px;
  height: calc(100vh - 32px);
  position: sticky;
  top: 16px;
}
.card { background: var(--panel); border-radius: 10px; padding: 12px 14px; }
.card header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }

.info { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; }
.links { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; text-decoration: none; font-size: 12px; }
.link:hover { text-decoration: underline; }

.players { display: grid; grid-template-columns: 1fr 1fr; background: var(--panel); border-radius: 10px; padding: 6px; gap: 6px; }
.player { border-radius: 8px; padding: 10px 12px; border: 2px solid transparent; }
.player.on-turn { border-color: #fff; background: var(--panel-2); }
.player .name { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.player .avatar {
  width: 28px; height: 28px; border-radius: 6px; display: grid; place-items: center;
  background: var(--panel-3); font-weight: 700; flex: none;
}
.player .sub { font-size: 11px; color: var(--muted); margin-top: 2px; }
.player .bottom { display: flex; justify-content: space-between; align-items: baseline; border-top: 1px solid var(--line); margin-top: 8px; padding-top: 6px; }
.player .score { font-size: 26px; font-weight: 300; }
.player .clock { font-variant-numeric: tabular-nums; color: var(--muted); }
.player.on-turn .clock { color: var(--text); }
.player .clock.over { color: var(--danger); }

.result h3 { margin: 0 0 4px; }
.result .btn { margin-top: 10px; width: 100%; padding: 10px; font-size: 15px; }

.bag-letters {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 3px;
  line-height: 1.8;
  display: flex;
  flex-wrap: wrap;
  column-gap: 14px;
  font-size: 13px;
}

.history { flex: 1; min-height: 120px; display: flex; flex-direction: column; }
.history header { border-bottom: 1px solid var(--accent); padding-bottom: 8px; }
.history-list { overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 8px; }
.turn { background: var(--panel-2); border-radius: 8px; padding: 10px 12px; }
.turn .who { font-weight: 600; }
.turn .meta { font-size: 11px; color: var(--muted); margin: 2px 0 6px; }
.turn .words { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.turn .mini { display: inline-flex; gap: 2px; }
.turn .mini .tile { --cell: 22px; }
.turn .extra { font-size: 12px; color: var(--muted); }
.turn .pts { margin-left: auto; font-weight: 700; }

/* ---------- dialogs, toast, empty state ---------- */

dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px 22px;
  min-width: 340px;
}
dialog::backdrop { background: rgba(10, 10, 15, 0.6); }
dialog h3 { margin: 0 0 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.dialog-actions .btn { font-size: 15px; padding: 10px 18px; }
label.row { display: flex; align-items: center; gap: 10px; margin: 12px 0 4px; }
.indent { margin-left: 26px !important; }
input[type=number], select, input[type=text], input[type=password], textarea {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  font: inherit;
}
input[type=number] { width: 80px; }
input:disabled { opacity: 0.45; }

.blank-grid { display: grid; grid-template-columns: repeat(7, 40px); gap: 6px; }
.blank-grid .tile { --cell: 40px; cursor: pointer; }
.blank-grid .tile:hover { background: #fff3d9; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: #44253a;
  border: 1px solid var(--danger);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.info { background: #213b3e; border-color: var(--accent); }

.empty-state { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(15, 15, 20, 0.7); z-index: 50; }
.empty-state .card { padding: 28px 32px; text-align: center; max-width: 360px; }
.empty-state h2 { margin-top: 0; }

/* ---------- login / dictionary pages ---------- */

.page { max-width: 760px; margin: 40px auto; padding: 0 16px; }
.page h1 { font-size: 22px; }
.page .card { margin-bottom: 14px; }
.page textarea { width: 100%; min-height: 90px; resize: vertical; }
.page .row-flex { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.page .btn { font-size: 14px; padding: 8px 14px; }
.word-list {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 10px; max-height: 260px; overflow-y: auto;
}
.ok { color: #7fd38a; }
.bad { color: var(--danger); }

.login { max-width: 340px; margin: 14vh auto; }
.login form { display: flex; flex-direction: column; gap: 10px; }
.login input { padding: 10px 12px; }
.login .btn { margin-top: 6px; }

@media (max-width: 900px) {
  :root { --cell: calc((100vw - 60px) / 16.4); }
  .layout { flex-direction: column; align-items: center; }
  .side { width: 100%; max-width: 560px; height: auto; position: static; }
  .history-list { max-height: 400px; }
  .btn { font-size: 15px; padding: 12px 8px; }
}
