:root {
  --bg: #16302a;
  --panel: #21413a;
  --panel2: #2c5249;
  --text: #eef6f2;
  --muted: #a3c2b8;
  --accent: #f2b544;
  --accent-d: #b9831f;
  --green: #4cc38a;
  --red: #e5534b;
  --felt: #2f8a5b;
  --felt-d: #1c5a3b;
  --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: #3c4f31; }
.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: green felt, 8×8 cells, discs that flip. */
.board { background: var(--felt-d); padding: calc(var(--board) / 80); }
.cells {
  position: absolute;
  inset: calc(var(--board) / 80);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: calc(var(--board) / 160);
}
.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-radius: calc(var(--board) / 120);
  background: var(--felt);
  box-shadow: inset 0 -1px 0 #0003;
}
.cell::before { content: ''; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; }
.cell.last::before { box-shadow: inset 0 0 0 calc(var(--board) / 140) #ffe033cc; }
.cell.legal::after {
  content: '';
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: #0b221b66;
  box-shadow: inset 0 0 0 2px #ffffff22;
}
.cell.legal.mine::after { background: #ffffff55; animation: breathe-dot 1.6s ease-in-out infinite; }
@keyframes breathe-dot { 50% { transform: scale(1.18); } }
.coord {
  position: absolute;
  font-size: calc(var(--board) / 42);
  font-weight: 700;
  line-height: 1;
  color: #ffffff66;
  pointer-events: none;
}
.coord.row { top: 6%; left: 7%; }
.coord.col { bottom: 6%; right: 8%; }

/* Discs: two faces, the turn shows the colour. */
.disc {
  display: inline-block;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex: none;
}
.disc.b { background: radial-gradient(circle at 35% 30%, #4a4f55, #0b0c0e 70%); box-shadow: 0 2px 3px #0008; }
.disc.w { background: radial-gradient(circle at 35% 30%, #ffffff, #cfd4d9 75%); box-shadow: 0 2px 3px #0006; }
.cell .stone {
  position: relative;
  width: 84%;
  height: 84%;
  transform-style: preserve-3d;
  transition: transform .42s cubic-bezier(.3, .7, .3, 1);
}
.cell .stone i {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  backface-visibility: hidden;
}
.cell .stone .fb { background: radial-gradient(circle at 35% 30%, #4a4f55, #0b0c0e 70%); box-shadow: 0 3px 4px #0009; }
.cell .stone .fw { background: radial-gradient(circle at 35% 30%, #ffffff, #cfd4d9 75%); box-shadow: 0 3px 4px #0007; transform: rotateY(180deg); }
.cell .stone.w { transform: rotateY(180deg); }
.cell .stone.noanim { transition: none; }
.cell .stone.drop { animation: drop .28s cubic-bezier(.3, 1.5, .6, 1); }
@keyframes drop { from { scale: 1.5; opacity: 0; } }
.cell.nope { animation: nope .32s; }
@keyframes nope { 20% { translate: -4px 0; } 50% { translate: 4px 0; } 80% { translate: -2px 0; } }

/* 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: #08140f99;
}
.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; 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: 26px; font-weight: 800; 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; }
.mv-s .disc { width: 11px; height: 11px; margin-right: 4px; vertical-align: -1px; box-shadow: none; }

/* 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 .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; }

/* 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: #1c5a3b; }
.dgm-cell { background: #2f8a5b; color: #fff; }
.dgm-cell .pc-d.b { background: radial-gradient(circle at 35% 30%, #4a4f55, #0b0c0e 70%); box-shadow: 0 2px 3px #0008; }
.dgm-cell .pc-d.w { background: radial-gradient(circle at 35% 30%, #ffffff, #cfd4d9 75%); box-shadow: 0 2px 3px #0006; }
.dgm-cell.flip .pc-d { box-shadow: 0 0 0 3px var(--accent); }
.dgm-cell.dot::after { background: #0b221b88; }
.play.wide .tool { flex: 1 1 28%; }
.dgm-cell .newdot { position: absolute; width: 26%; aspect-ratio: 1; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 2px #0006; }
