:root {
  --bg: #1d2733;
  --panel: #2a3644;
  --panel2: #364556;
  --text: #eef2f6;
  --muted: #9fb0c0;
  --accent: #f2b544;
  --accent-d: #b9831f;
  --green: #4cc38a;
  --red: #e5534b;
  --light: #f0d9b5;
  --dark: #b58863;
  --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 img { width: 40px; height: 40px; }
.seg button[aria-checked="true"] { border-color: var(--accent); background: #3e4b3a; }
.dice { display: flex; height: 40px; }
.dice img { width: 30px !important; height: 30px !important; }
.dice img + img { 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;
}
.squares {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
}
.sq { position: relative; }
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }
.sq::before, .sq::after { content: ''; position: absolute; inset: 0; pointer-events: none; }
.sq.last::before { background: #ffe03366; }
.sq.sel::before { background: #4fc77a80; }
.sq.check::before { background: radial-gradient(circle, #ff2a1a 0%, #ff2a1acc 30%, #ff2a1a00 72%); }
.sq.hover::before { box-shadow: inset 0 0 0 calc(var(--board) / 100) #ffffffcc; }
.sq.dest::after { background: radial-gradient(circle, #1a2a1a44 0 16%, transparent 17%); }
.sq.take::after { background: radial-gradient(circle, transparent 0 66%, #1a2a1a55 67%); }
.coord {
  position: absolute;
  font-size: calc(var(--board) / 38);
  font-weight: 700;
  line-height: 1;
  pointer-events: none;
}
.sq.light .coord { color: var(--dark); }
.sq.dark .coord { color: var(--light); }
.coord.rank { top: 4%; left: 5%; }
.coord.file { bottom: 4%; right: 6%; }

.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.slide { transition: transform .24s cubic-bezier(.3, .7, .3, 1); }
.pc.noanim { transition: none !important; }
.pc.dragging { z-index: 5; filter: drop-shadow(0 6px 6px #0006); transition: none; }
.pc.nope { animation: nope .32s; }
@keyframes nope { 20% { rotate: -10deg; } 50% { rotate: 9deg; } 80% { rotate: -5deg; } }

/* 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: #0d131a99;
}
.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-kings { display: flex; gap: 4px; }
.over-kings img { width: 64px; height: 64px; animation: pop .5s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes pop { from { transform: scale(.3); opacity: 0; } }
.promo-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; width: 100%; }
.promo-btn { aspect-ratio: 1; border-radius: 14px; background: var(--light); padding: 4px; }
.promo-btn:active { background: var(--accent); }
.promo-btn img { width: 100%; height: 100%; display: block; }

/* 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-king { width: 34px; height: 34px; flex: none; }
.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-caps { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; justify-content: flex-end; align-items: center; row-gap: 2px; }
.p-caps img { width: 20px; height: 20px; margin-left: -5px; }
.p-caps .adv { margin-left: 6px; font-size: 13px; font-weight: 700; color: var(--muted); }

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

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