:root {
  --soil: #3b2a1f;
  --soil-2: #4a3628;
  --soil-3: #5c4433;
  --cream: #f6ecd9;
  --cream-2: #e8d9bc;
  --ink: #2a1d14;
  --muted: #b9a68c;
  --worm: #e2877c;
  --leaf: #7fb069;
  --leaf-dark: #5d8c48;
  --danger: #d9534f;
  --radius: 12px;
}

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

body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--cream);
  background:
    radial-gradient(circle at 20% 10%, rgba(255,255,255,.05), transparent 40%),
    var(--soil);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  max-width: 1100px;
  margin: 0 auto;
}
.topbar h1 { margin: 0; font-size: 1.4rem; letter-spacing: .5px; }

.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); }
.conn.on { background: var(--leaf); }

main { max-width: 1100px; margin: 0 auto; padding: 0 16px 32px; }

.card {
  background: var(--soil-2);
  border: 1px solid var(--soil-3);
  border-radius: var(--radius);
  padding: 16px;
}
.narrow { max-width: 440px; margin: 24px auto; display: flex; flex-direction: column; gap: 12px; }
h2 { margin: 0 0 4px; }
h3 { margin: 8px 0 0; font-size: 1rem; }
.muted { color: var(--muted); margin: 0; font-size: .9rem; }

label { display: flex; flex-direction: column; gap: 6px; font-size: .9rem; color: var(--muted); }

input {
  font: inherit;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--soil-3);
  background: var(--soil);
  color: var(--cream);
  min-width: 0;
  flex: 1;
}
.code-input { text-transform: uppercase; letter-spacing: 4px; font-weight: 700; text-align: center; }

button {
  font: inherit;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid var(--soil-3);
  background: var(--soil-3);
  color: var(--cream);
  cursor: pointer;
}
button:hover:not(:disabled) { filter: brightness(1.12); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--leaf); border-color: var(--leaf-dark); color: var(--ink); }
button.ghost { background: transparent; }

.row { display: flex; gap: 8px; }
.divider { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: .85rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--soil-3); }

.rules summary { cursor: pointer; color: var(--muted); }
.rules ul { padding-left: 18px; margin: 8px 0 0; line-height: 1.5; font-size: .9rem; }

.code { font-family: ui-monospace, Consolas, monospace; letter-spacing: 3px; color: var(--worm); }

.players-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.players-list li { padding: 8px 10px; background: var(--soil); border-radius: 8px; display: flex; justify-content: space-between; }

.section-title { font-size: .75rem; text-transform: uppercase; letter-spacing: 1.5px; color: var(--muted); margin-bottom: 8px; }

/* Tiles */
.grill-wrap { margin-bottom: 16px; }
.grill { display: grid; grid-template-columns: repeat(auto-fit, minmax(38px, 1fr)); gap: 6px; }
.grill .tile { width: auto; }

.tile {
  width: 52px;
  height: 84px;
  border-radius: 8px;
  background: var(--cream);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 8px 2px;
  border: 2px solid var(--cream-2);
  box-shadow: 0 2px 0 rgba(0,0,0,.35);
  flex-shrink: 0;
}
.tile .num { font-size: 1.25rem; font-weight: 800; }
.tile .worms { font-size: .7rem; line-height: 1; text-align: center; word-break: break-all; max-width: 40px; }
.tile.gone { background: var(--soil-3); border-color: var(--soil-3); color: transparent; box-shadow: none; }
.tile.gone .worms { visibility: hidden; }
.tile.takeable {
  cursor: pointer;
  border-color: var(--leaf);
  box-shadow: 0 0 0 3px rgba(127,176,105,.45), 0 2px 0 rgba(0,0,0,.35);
  animation: pulse 1.2s ease-in-out infinite;
}
.tile.takeable:hover { transform: translateY(-3px); }
.tile.small { width: 40px; height: 64px; padding: 5px 2px; }
.tile.small .num { font-size: 1rem; }
.tile.small .worms { font-size: .55rem; max-width: 32px; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(127,176,105,.2), 0 2px 0 rgba(0,0,0,.35); } }

/* Layout */
.layout { display: grid; grid-template-columns: 1fr 320px; gap: 16px; }
.side { display: flex; flex-direction: column; gap: 16px; }
@media (max-width: 800px) { .layout { grid-template-columns: 1fr; } }

.status { font-size: 1.15rem; font-weight: 700; margin-bottom: 12px; min-height: 1.5em; }
.status.mine { color: var(--leaf); }

.dice-block { margin-bottom: 14px; }
.dice { display: grid; grid-template-columns: repeat(8, minmax(0, 48px)); gap: 8px; }
.dice.kept { grid-template-columns: repeat(8, minmax(0, 38px)); gap: 6px; }

/* Dice */
.die {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 10px;
  background: var(--cream);
  border: 2px solid var(--cream-2);
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  padding: 6px;
  box-shadow: 0 2px 0 rgba(0,0,0,.35);
  position: relative;
}
.die .pip { width: 8px; height: 8px; background: var(--ink); border-radius: 50%; place-self: center; }
.die.worm { display: flex; align-items: center; justify-content: center; font-size: 26px; background: #fbe3df; }
.die.pickable { cursor: pointer; border-color: var(--leaf); }
.die.pickable:hover, .die.hover { transform: translateY(-3px); box-shadow: 0 0 0 3px rgba(127,176,105,.6), 0 2px 0 rgba(0,0,0,.35); }
.die.selected {
  border-color: var(--leaf);
  background: #e4f1dc;
  transform: translateY(-4px);
  box-shadow: 0 0 0 3px var(--leaf), 0 4px 0 rgba(0,0,0,.35);
}
.die.used { opacity: .35; }
.die.rolling { animation: shake .45s ease; }
.die.slot { background: transparent; border: 2px dashed var(--soil-3); box-shadow: none; }
.kept .die { padding: 5px; border-radius: 8px; }
.kept .die .pip { width: 6px; height: 6px; }
.kept .die.worm { font-size: 20px; }
@keyframes shake {
  0% { transform: rotate(0) translateY(-10px); opacity: .2; }
  40% { transform: rotate(18deg); }
  70% { transform: rotate(-10deg); }
  100% { transform: rotate(0); opacity: 1; }
}

.sum { color: var(--cream); font-weight: 800; letter-spacing: 0; text-transform: none; font-size: .95rem; margin-left: 6px; }
.sum .noworm { color: var(--worm); font-weight: 600; }

.hint { margin: 0 0 10px; min-height: 1.3em; }
button.stop { width: 100%; margin-top: 10px; padding: 8px; font-size: .85rem; color: var(--muted); }
button.take { background: var(--worm); border-color: #c86a5f; color: var(--ink); }
.actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* Touch: no double-tap zoom delay, tap flash or accidental text selection on game controls. */
button, .die, .tile { touch-action: manipulation; -webkit-tap-highlight-color: transparent; user-select: none; -webkit-user-select: none; }

/* Players */
.players { display: flex; flex-direction: column; gap: 8px; }
.player {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--soil);
  border: 2px solid transparent;
}
.player.current { border-color: var(--leaf); }
.player .info { flex: 1; min-width: 0; }
.player .name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player .meta { font-size: .8rem; color: var(--muted); }
.player .offline { color: var(--danger); }
.player .empty-slot { width: 40px; height: 64px; border: 2px dashed var(--soil-3); border-radius: 8px; }

.log { margin: 0; padding-left: 18px; max-height: 220px; overflow-y: auto; font-size: .85rem; line-height: 1.45; color: var(--cream-2); }

.over { margin-bottom: 16px; border-color: var(--leaf); text-align: center; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.over .row { justify-content: center; }

/* Phones: fit the grill in two rows and keep dice and players compact
   so the whole turn is visible with little scrolling. */
@media (max-width: 600px) {
  main { padding: 0 10px 24px; }
  .topbar { padding: 10px 12px; }
  .topbar h1 { font-size: 1.2rem; }
  .card { padding: 12px; }
  .grill-wrap { margin-bottom: 10px; }
  .layout, .side { gap: 10px; }
  .grill { grid-template-columns: repeat(8, 1fr); gap: 4px; }
  .grill .tile { height: 58px; padding: 5px 1px; border-radius: 6px; }
  .grill .tile .num { font-size: .95rem; }
  .grill .tile .worms { font-size: .6rem; max-width: 28px; }
  .tile.takeable { background: #e4f1dc; border-color: var(--leaf); box-shadow: 0 0 0 2px var(--leaf); animation: none; }
  .status { font-size: 1.05rem; margin-bottom: 8px; }
  .dice-block { margin-bottom: 10px; }
  .dice { grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 5px; }
  .dice.kept { grid-template-columns: repeat(8, minmax(0, 30px)); gap: 4px; }
  .die { padding: 4px; border-radius: 8px; }
  .die .pip { width: 6px; height: 6px; }
  .die.worm { font-size: 20px; }
  .kept .die { padding: 3px; border-radius: 6px; }
  .kept .die .pip { width: 5px; height: 5px; }
  .kept .die.worm { font-size: 15px; }
  .players { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
  .player { padding: 6px 8px; gap: 6px; }
  .player .meta { font-size: .75rem; }
  .actions { position: sticky; bottom: 0; background: var(--soil-2); padding: 8px 0 2px; }
  .actions button { flex: 1; padding: 12px; }
  .log { max-height: 140px; }
}

/* Lift effects only where there is a real hover, so taps don't leave dice stuck "raised". */
@media (hover: none) {
  .die.pickable:hover, .tile.takeable:hover { transform: none; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--cream);
  color: var(--ink);
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 600;
  box-shadow: 0 6px 24px rgba(0,0,0,.4);
  max-width: calc(100vw - 32px);
  z-index: 10;
}
.toast.bad { background: var(--worm); }

/* At the top on phones so it never covers the buttons or stealable tiles. */
@media (max-width: 600px) {
  .toast { top: 8px; bottom: auto; font-size: .9rem; }
}
@media (max-width: 400px) {
  .player .tile-count { display: none; }
}

/* Turned-over tiles are out of the game: show them face down, unlike taken (empty) slots. */
.tile.flipped {
  background: repeating-linear-gradient(45deg, #6b4f3a 0 6px, #5c4433 6px 12px);
  border-color: #7a5c44;
}
