:root {
  --bg: #2b2118;
  --panel: #3b2e22;
  --panel2: #4a3a2b;
  --text: #f6efe6;
  --muted: #c7b39c;
  --accent: #f2b544;
  --accent-d: #b9831f;
  --green: #4cc38a;
  --red: #e5534b;
  --wood: #e4bf86;
  --wood-line: #9b6b3a;
  --board: 320px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }
svg { fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

.screen {
  position: fixed;
  inset: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------------------------------------------------------------- Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 10px 20px;
  border-radius: 14px;
  background: var(--panel2);
  font-weight: 700;
  font-size: 16px;
  transition: transform .08s, opacity .15s;
}
.btn svg { width: 22px; height: 22px; flex: none; }
.btn.primary { background: var(--accent); color: #2a1d05; box-shadow: 0 3px 0 var(--accent-d); }
.btn.primary svg path[d^="M8 5.5"] { fill: currentColor; stroke: none; }
.btn:active { transform: translateY(2px); }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.wide { width: 100%; }

/* ---------------------------------------------------------------- Start (browser only) */
.start {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6vmin;
  overflow-y: auto;
  touch-action: pan-y;
  padding-left: 16px;
  padding-right: 16px;
}
@media (orientation: portrait) {
  .start { flex-direction: column; }
}
.start-main { display: flex; flex-direction: column; align-items: center; gap: 2.6vmin; }
.start-logo {
  width: min(24vmin, 150px);
  height: auto;
  border-radius: 24%;
  box-shadow: 0 8px 26px #0008;
}
.start-title { margin: 0; font-size: clamp(30px, 6vmin, 52px); font-weight: 900; letter-spacing: .5px; }
.start-go {
  width: min(30vmin, 200px);
  height: min(30vmin, 200px);
  border-radius: 50%;
  background: var(--accent);
  color: #2a1d05;
  box-shadow: 0 9px 0 var(--accent-d), 0 14px 26px #0006;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2%;
  font-weight: 900;
  font-size: clamp(18px, 3.4vmin, 28px);
  animation: breathe 1.8s ease-in-out infinite;
}
.start-go svg { width: 46%; height: 46%; fill: currentColor; stroke: none; margin-left: 8%; }
.start-go:active { transform: translateY(6px); animation: none; }
@keyframes breathe { 50% { transform: scale(1.05); } }
.start .install-card { width: min(440px, 100%); flex: none; }

/* ---------------------------------------------------------------- Home */
.home { overflow-y: auto; }
.exit-btn {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 12px);
  right: calc(env(safe-area-inset-right) + 12px);
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 6px;
  height: 44px;
  padding: 0 16px 0 12px;
  border-radius: 999px;
  background: #e5383b;
  color: #fff;
  box-shadow: 0 3px 0 #a4161a, 0 4px 10px #0004;
  font-weight: 800;
  font-size: 16px;
}
.exit-btn svg { width: 22px; height: 22px; stroke-width: 2.6; }
.exit-btn:active { transform: scale(.95); }
.home-inner {
  width: min(440px, 100%);
  min-height: 100%;
  margin: 0 auto;
  padding: 68px 16px 28px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 14px;
}
.logo { display: flex; align-items: center; justify-content: center; gap: 14px; margin-bottom: 6px; }
.logo img { width: 72px; height: 72px; border-radius: 18px; box-shadow: 0 4px 14px #0006; }
.logo h1 { margin: 0; font-size: 38px; letter-spacing: .5px; }
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  border-radius: 20px;
  background: var(--panel);
}
.card h2 { margin: 0; font-size: 18px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 4px 8px;
  border-radius: 14px;
  background: var(--panel2);
  font-size: 14px;
  font-weight: 600;
  border: 3px solid transparent;
}
.seg button .disc { width: 36px; height: 36px; }
.seg button[aria-checked="true"] { border-color: var(--accent); background: #4f4127; }
.dice { display: flex; height: 40px; }
.dice { align-items: center; }
.dice .disc { width: 28px; height: 28px; }
.dice .disc + .disc { margin-left: -10px; margin-top: 10px; }

.code-entry { position: relative; display: block; }
.code-entry input {
  position: absolute;
  inset: 0;
  width: 100%;
  opacity: 0;
  font-size: 16px; /* no iOS zoom */
  caret-color: transparent;
  z-index: 1;
}
.digits { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.digits i {
  height: 68px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: var(--bg);
  border: 3px solid var(--panel2);
  font-style: normal;
  font-size: 34px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.digits i.cur { border-color: var(--accent); }
.home-error {
  margin: 0;
  padding: 12px 14px;
  border-radius: 14px;
  background: #4a2626;
  color: #ffd2cf;
  font-weight: 600;
  text-align: center;
}

/* Install card */
.install-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px; height: 32px;
  display: grid; place-items: center;
  color: var(--muted);
}
.install-close svg, .step-icon { width: 20px; height: 20px; }
.install-head { display: flex; align-items: center; gap: 12px; padding-right: 28px; }
.install-head img { width: 48px; height: 48px; border-radius: 12px; flex: none; }
.install-title { font-size: 17px; font-weight: 800; }
.install-desc { font-size: 13px; color: var(--muted); margin-top: 2px; }
.install-steps { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 7px; font-size: 14px; }
.install-steps li { display: flex; align-items: center; gap: 8px; }
.install-steps .num {
  width: 24px; height: 24px; flex: none;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--accent);
  color: #2a1d05;
  font-size: 12px;
  font-weight: 800;
}
.install-steps .step { flex: 1; }
.step-icon { flex: none; color: var(--muted); }
.step-icon.share { color: #4ea1ff; }

/* ---------------------------------------------------------------- Play */
.play { display: flex; align-items: center; justify-content: center; gap: 8px; }
.play.tall { flex-direction: column; }
.play.tall .side { display: contents; }
.play.tall #p-opp { order: 1; }
.play.tall .board-wrap { order: 2; }
.play.tall #p-me { order: 3; }
.play.tall .moves { order: 4; }
.play.tall .tools { order: 5; }
.play.tall .player, .play.tall .moves, .play.tall .tools { width: min(var(--board), 100% - 12px); flex: none; }

.play.wide { flex-direction: row; gap: 16px; }
.play.wide .side {
  width: clamp(230px, 100% - var(--board) - 40px, 360px);
  height: var(--board);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.board-wrap { position: relative; width: var(--board); height: var(--board); flex: none; }
.board {
  position: absolute;
  inset: 0;
  border-radius: 6px;
  overflow: hidden;
  touch-action: none;
  box-shadow: 0 6px 24px #0007;
}
/* The board: one colour, every square used (Turkish dama), with lines between squares. */
.board { background: var(--wood-line); }
.squares {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 1px;
  padding: 1px;
}
.sq { position: relative; background: var(--wood); }
.sq:nth-child(odd) { background: #e8c48d; }
.sq::before, .sq::after { content: ''; position: absolute; inset: 0; pointer-events: none; }
.sq.last::before { background: #ffe03355; }
.sq.path::before { background: #ffe03333; }
.sq.sel::before { background: #4fc77a80; }
.sq.can::before { box-shadow: inset 0 0 0 calc(var(--board) / 110) #4fc77acc; }
.sq.must::before { box-shadow: inset 0 0 0 calc(var(--board) / 90) #ff7a1acc; }
.sq.dest::after { background: radial-gradient(circle, #3b220e66 0 17%, transparent 18%); }
.coord {
  position: absolute;
  font-size: calc(var(--board) / 40);
  font-weight: 700;
  line-height: 1;
  color: #7a5228;
  pointer-events: none;
}
.coord.rank { top: 5%; left: 6%; }
.coord.file { bottom: 5%; right: 7%; }

/* Pieces: flat round discs; a king (dama) wears a crown. */
.disc { display: inline-block; width: 30px; height: 30px; border-radius: 50%; flex: none; position: relative; }
.disc.w, .pc.w, .pc-d.w {
  background: radial-gradient(circle at 50% 50%, #fff 0 52%, #e9e4dc 53% 58%, #fbf8f3 59%);
  box-shadow: 0 2px 3px #0007, inset 0 -2px 0 #0002;
}
.disc.b, .pc.b, .pc-d.b {
  background: radial-gradient(circle at 50% 50%, #3a332e 0 52%, #191513 53% 58%, #2d2723 59%);
  box-shadow: 0 2px 3px #0009, inset 0 -2px 0 #fff1;
}
.crown { width: 58%; height: 58%; fill: #e0a526; stroke: #7a520f; stroke-width: 1.2; stroke-linejoin: round; }
.pieces { position: absolute; inset: 0; pointer-events: none; }
.pc {
  position: absolute;
  left: 0;
  top: 0;
  width: 12.5%;
  height: 12.5%;
  will-change: transform;
}
.pc i {
  position: absolute;
  inset: 9%;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: inherit;
  box-shadow: inherit;
}
.pc { background: none !important; box-shadow: none !important; }
.pc.w i { background: radial-gradient(circle at 50% 50%, #fff 0 52%, #e9e4dc 53% 58%, #fbf8f3 59%); box-shadow: 0 3px 4px #0007, inset 0 -2px 0 #0002; }
.pc.b i { background: radial-gradient(circle at 50% 50%, #3a332e 0 52%, #191513 53% 58%, #2d2723 59%); box-shadow: 0 3px 4px #0009, inset 0 -2px 0 #fff1; }
.pc.slide { transition: transform .2s cubic-bezier(.3, .7, .3, 1); }
.pc.noanim { transition: none !important; }
.pc.gone { transition: opacity .25s, scale .25s; opacity: 0; scale: .6; }
.pc.ghost { opacity: .35; }
.pc.nope i { animation: nope .32s; }
@keyframes nope { 20% { rotate: -10deg; } 50% { rotate: 9deg; } 80% { rotate: -5deg; } }
.pc.crowned i { animation: crown .5s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes crown { 50% { scale: 1.25; } }

/* Cards over the board: waiting, game over, promotion. */
.board-card {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 8px;
  border-radius: 6px;
  background: #140e0999;
}
.bc-box {
  width: min(340px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 20px 18px;
  border-radius: 20px;
  background: var(--panel);
  box-shadow: 0 10px 30px #0008;
  text-align: center;
}
.bc-title { font-size: 22px; font-weight: 800; }
.bc-sub { color: var(--muted); }
.bc-note { color: var(--muted); font-size: 13px; }
.bc-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.big-code {
  font-size: 56px;
  font-weight: 800;
  letter-spacing: 10px;
  padding-left: 10px;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  line-height: 1.1;
}
.over-discs { display: flex; gap: 6px; }
.over-discs .disc { width: 56px; height: 56px; display: grid; place-items: center; animation: pop .5s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes pop { from { transform: scale(.3); opacity: 0; } }

/* Player rows */
.player {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
  padding: 6px 12px;
  border-radius: 14px;
  background: var(--panel);
  transition: box-shadow .2s;
}
.player.turn { box-shadow: inset 0 0 0 3px var(--accent); }
.p-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--green); flex: none; }
.player.offline .p-dot, .player.waiting .p-dot { background: #6b7886; }
.p-disc { width: 30px; height: 30px; }
.p-text { min-width: 0; flex: none; }
.p-name { font-weight: 700; white-space: nowrap; }
.p-status { font-size: 13px; color: var(--muted); min-height: 1em; white-space: nowrap; }
.player.turn .p-status { color: var(--accent); font-weight: 700; }
.p-count { flex: 1; text-align: right; font-size: 14px; font-weight: 700; color: var(--muted); white-space: nowrap; }
.p-count b { font-size: 24px; color: var(--text); font-variant-numeric: tabular-nums; }

/* Move list: a strip on tall screens, a list beside the board on wide ones. */
.moves {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  font-size: 14px;
  min-height: 24px;
  padding: 2px 4px;
  scrollbar-width: none;
  color: var(--muted);
}
.moves::-webkit-scrollbar { display: none; }
.mv { display: flex; gap: 6px; }
.mv-n { opacity: .7; }
.mv-s.cur { color: var(--text); font-weight: 700; }
.play.wide .moves {
  flex: 1;
  min-height: 0;
  flex-direction: column;
  gap: 2px;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 8px 10px;
  border-radius: 14px;
  background: #00000026;
  font-size: 15px;
}
.play.wide .mv { display: grid; grid-template-columns: 2.6em 1fr 1fr; }

/* Tools */
.tools { display: flex; gap: 6px; }
.tool {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 56px;
  padding: 6px 2px;
  border-radius: 14px;
  background: var(--panel);
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}
.tool span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.tool svg { width: 24px; height: 24px; color: var(--text); }
.tool .half { font-size: 22px; line-height: 24px; font-weight: 800; color: var(--text); }
.tool .room-code { font-size: 17px; line-height: 24px; font-weight: 800; color: var(--accent); letter-spacing: 1px; }
.tool:active { background: var(--panel2); }
.tool:disabled { opacity: .4; cursor: default; }
.tool.on { background: #4a3d1c; color: var(--accent); }
.play.wide .tools { flex-wrap: wrap; }
.play.wide .tool { flex: 1 1 28%; }
.tool.on { background: #5a4518; }

/* Connection pill + toast */
.pill, .toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  max-width: calc(100% - 32px);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}
.pill { top: calc(env(safe-area-inset-top) + 8px); background: #5a3b1c; color: #ffd9a8; }
.toast { top: 42%; background: #000c; color: #fff; padding: 12px 20px; font-size: 16px; animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }

/* ---------------------------------------------------------------- Dialogs */
.dialog {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: #0009;
}
.dialog-box {
  width: min(380px, 100%);
  padding: 20px;
  border-radius: 20px;
  background: var(--panel);
  box-shadow: 0 10px 30px #0008;
}
.dialog-text { font-size: 18px; font-weight: 600; text-align: center; margin-bottom: 16px; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------------------------------------------------------------- Rulebook */
.rules {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  user-select: text;
  -webkit-user-select: text;
}
.rules-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--panel);
  box-shadow: 0 2px 10px #0005;
}
.rules-head h2 { flex: 1; margin: 0; font-size: 22px; }
.rules-close { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 50%; background: var(--panel2); }
.rules-close svg { width: 24px; height: 24px; }
.rules-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 32px);
  width: min(720px, 100%);
  margin: 0 auto;
  line-height: 1.5;
}
.rules-body .lead { font-size: 17px; margin: 0 0 8px; }
.rules-body section { margin: 22px 0; }
.rules-body h3 { display: flex; align-items: center; gap: 10px; margin: 0 0 6px; font-size: 19px; }
.rules-body h3 .n {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; flex: none;
  background: var(--accent); color: #2a1d05; font-size: 15px;
}
.rules-body p { margin: 6px 0; }
.rules-body ul { margin: 6px 0; padding-left: 22px; }
.rules-body li { margin: 4px 0; }
.rules-body .tip { color: var(--muted); font-size: 14px; }
.dgm { margin: 12px auto 4px; width: min(300px, 100%); }
.dgm-board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1;
  gap: 2px;
  padding: 4px;
  border-radius: 8px;
  box-shadow: 0 4px 14px #0006;
}
.dgm-cell { position: relative; display: grid; place-items: center; border-radius: 3px; }
.dgm-cell .lbl { position: absolute; font-size: 8px; font-weight: 700; opacity: .55; line-height: 1; }
.dgm-cell .lbl.r { top: 2px; left: 3px; }
.dgm-cell .lbl.c { bottom: 2px; right: 3px; }
.dgm-cell .pc-d { width: 80%; aspect-ratio: 1; border-radius: 50%; display: grid; place-items: center; }
.dgm-cell.x .pc-d { opacity: .45; }
.dgm-cell.x::after { content: '×'; position: absolute; font-size: 26px; font-weight: 900; color: #e5534b; line-height: 1; }
.dgm-cell.dot::after { content: ''; width: 30%; aspect-ratio: 1; border-radius: 50%; background: #2228; }
.dgm-cell .step {
  width: 62%; aspect-ratio: 1; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: #2a1d05; font-size: 12px; font-weight: 800;
}
.dgm-cap { text-align: center; color: var(--muted); font-size: 14px; margin-top: 6px; }
.dgm-board { background: #9b6b3a; }
.dgm-cell { background: #e8c48d; color: #7a5228; }
.dgm-cell .pc-d .crown { width: 62%; height: 62%; }
.dgm-cell .pc-d.soon { opacity: .6; }
