:root {
  color-scheme: light;
  --ink: #171b19;
  --ink-soft: #303733;
  --muted: #68716c;
  --canvas: #e8ebe6;
  --paper: #f8f8f5;
  --surface: #ffffff;
  --line: #d5dad4;
  --line-strong: #b8c0b9;
  --green: #2f6d57;
  --green-soft: #dcebe3;
  --amber: #b77925;
  --amber-soft: #f5e8d3;
  --red: #a3463c;
  --red-soft: #f3dfdc;
  --blue: #426a87;
  --light-square: #ece5d4;
  --dark-square: #668675;
  --board-edge: #202622;
  --shadow: 0 16px 40px rgba(25, 33, 28, 0.12);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--canvas);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--canvas);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.46;
}

input {
  width: 100%;
  min-width: 0;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 12px;
  outline: none;
}

input:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(47, 109, 87, 0.12);
}

input[readonly] {
  background: #f0f2ef;
  color: var(--ink-soft);
}

label,
.section-label {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
  letter-spacing: 0;
}

.app-shell {
  width: min(1320px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 14px 0 22px;
}

.app-bar {
  display: flex;
  min-height: 58px;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 14px;
  padding: 0 4px;
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  place-items: center;
  border: 1px solid #080a09;
  border-radius: 7px;
  background: var(--ink);
  color: #ffffff;
  font-family: Georgia, serif;
  font-size: 1.25rem;
  font-weight: 800;
}

.brand-lockup p {
  color: var(--green);
  font-size: 0.66rem;
  font-weight: 850;
  text-transform: uppercase;
}

.brand-lockup h1 {
  overflow: hidden;
  font-size: 1.3rem;
  line-height: 1.14;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.system-status {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 7px;
}

.status-chip {
  display: inline-flex;
  min-height: 31px;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--muted);
  padding: 5px 10px;
  font-size: 0.74rem;
  font-weight: 800;
  white-space: nowrap;
}

.status-chip i,
.connection-list i {
  width: 7px;
  height: 7px;
  flex: 0 0 7px;
  border-radius: 50%;
  background: var(--line-strong);
}

.status-chip.ok {
  border-color: #b8d3c5;
  background: var(--green-soft);
  color: var(--green);
}

.status-chip.ok i,
.connection-list i.ok {
  background: var(--green);
}

.status-chip.warn {
  border-color: #e2cda8;
  background: var(--amber-soft);
  color: #805112;
}

.status-chip.bad {
  border-color: #dfb9b4;
  background: var(--red-soft);
  color: var(--red);
}

.status-chip.bad i,
.connection-list i.bad {
  background: var(--red);
}

.turn-chip {
  border-color: var(--ink);
  background: var(--ink);
  color: #ffffff;
}

.turn-chip.my-turn {
  border-color: var(--amber);
  background: var(--amber-soft);
  color: #77490c;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(500px, 760px) minmax(300px, 350px);
  justify-content: center;
  gap: 18px;
  align-items: start;
}

.play-area {
  display: flex;
  min-width: 0;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.player-strip {
  display: grid;
  width: min(100%, 66dvh, 700px);
  min-height: 48px;
  grid-template-columns: 36px minmax(120px, auto) minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px;
  border-bottom: 1px solid var(--line);
  padding: 4px 2px 8px;
}

.player-strip.local {
  border-top: 1px solid var(--line);
  border-bottom: 0;
  padding: 8px 2px 4px;
}

.player-strip.active {
  border-color: var(--amber);
}

.player-avatar {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border-radius: 50%;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.35rem;
  line-height: 1;
}

.white-avatar {
  border: 1px solid var(--line-strong);
  background: #ffffff;
  color: #ffffff;
  -webkit-text-stroke: 1px var(--ink);
}

.black-avatar {
  background: var(--ink);
  color: var(--ink);
  -webkit-text-stroke: 1px #ffffff;
}

.player-copy {
  display: grid;
  min-width: 0;
  gap: 2px;
}

.player-copy strong {
  overflow: hidden;
  font-size: 0.88rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-role {
  color: var(--muted);
  font-size: 0.66rem;
  font-weight: 750;
  text-transform: uppercase;
}

.captured-pieces {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.05rem;
  text-align: right;
  text-overflow: clip;
  white-space: nowrap;
}

.seat-state {
  border-radius: 999px;
  background: #eef0ed;
  color: var(--muted);
  padding: 4px 8px;
  font-size: 0.67rem;
  font-weight: 800;
  white-space: nowrap;
}

.seat-state.online {
  background: var(--green-soft);
  color: var(--green);
}

.board-shell {
  position: relative;
  width: min(100%, 66dvh, 700px);
  aspect-ratio: 1;
  flex: 0 0 auto;
  container-type: inline-size;
  border: 7px solid var(--board-edge);
  border-radius: 7px;
  background: var(--board-edge);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.board-surface {
  display: grid;
  width: 100%;
  height: 100%;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
}

.square {
  position: relative;
  display: grid;
  width: 100%;
  min-width: 0;
  min-height: 0;
  place-items: center;
  border-radius: 0;
  padding: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.7rem, 8.4cqi, 4.7rem);
  line-height: 1;
  transition: filter 120ms ease, box-shadow 120ms ease;
}

.square.light {
  background: var(--light-square);
}

.square.dark {
  background: var(--dark-square);
}

.square:not(:disabled):hover {
  filter: brightness(1.06);
}

.piece {
  position: relative;
  z-index: 2;
  transform: translateY(-1%);
  user-select: none;
}

.piece.white-piece {
  color: #fffdf6;
  -webkit-text-stroke: max(1px, 0.18cqi) #1b211d;
  text-shadow: 0 2px 1px rgba(0, 0, 0, 0.16);
}

.piece.black-piece {
  color: #171b19;
  -webkit-text-stroke: max(0.5px, 0.08cqi) #0a0c0b;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.14);
}

.coord {
  position: absolute;
  z-index: 3;
  color: rgba(23, 27, 25, 0.64);
  font-family: Inter, ui-sans-serif, system-ui, sans-serif;
  font-size: clamp(0.54rem, 1.45cqi, 0.72rem);
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
}

.coord.file {
  right: 5px;
  bottom: 4px;
}

.coord.rank {
  top: 5px;
  left: 5px;
}

.square.selected {
  box-shadow: inset 0 0 0 max(3px, 0.48cqi) var(--amber);
}

.square.last-move {
  box-shadow: inset 0 0 0 max(3px, 0.42cqi) rgba(183, 121, 37, 0.72);
}

.square.selected.last-move {
  box-shadow: inset 0 0 0 max(4px, 0.55cqi) var(--amber);
}

.square.target::after {
  content: "";
  position: absolute;
  z-index: 1;
  width: 25%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(20, 27, 23, 0.32);
}

.square.capture-target::after {
  width: 72%;
  border: max(3px, 0.45cqi) solid rgba(20, 27, 23, 0.36);
  background: transparent;
}

.square.in-check {
  background: #c66d61;
}

.board-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  background: rgba(241, 242, 238, 0.78);
  backdrop-filter: blur(4px);
  text-align: center;
}

.board-overlay[hidden] {
  display: none;
}

.board-overlay strong {
  font-size: clamp(1rem, 3cqi, 1.45rem);
}

.check-banner {
  position: absolute;
  top: 9px;
  left: 50%;
  z-index: 6;
  transform: translateX(-50%);
  border-radius: 999px;
  background: var(--red);
  color: #ffffff;
  padding: 5px 11px;
  font-size: 0.72rem;
  font-weight: 850;
}

.move-dock {
  display: grid;
  width: min(100%, 66dvh, 700px);
  grid-template-columns: auto minmax(110px, 1fr) auto;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}

.move-dock label {
  padding-right: 3px;
}

.primary-button,
.secondary-button,
.danger-button,
.text-button,
.copy-button {
  min-height: 40px;
  border-radius: 6px;
  padding: 0 13px;
  font-size: 0.78rem;
  font-weight: 850;
}

.primary-button {
  background: var(--ink);
  color: #ffffff;
}

.primary-button:hover:not(:disabled) {
  background: var(--green);
}

.secondary-button {
  border: 1px solid var(--line);
  background: #f3f5f2;
  color: var(--ink);
}

.danger-button {
  border: 1px solid #dfb9b4;
  background: var(--red-soft);
  color: var(--red);
}

.text-button {
  min-height: 34px;
  background: transparent;
  color: var(--green);
  padding: 0 5px;
}

.control-panel {
  display: flex;
  max-height: calc(100dvh - 92px);
  min-height: 580px;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.match-heading {
  display: flex;
  min-height: 70px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding: 14px 15px;
}

.match-heading h2 {
  max-width: 230px;
  margin-top: 3px;
  overflow: hidden;
  font-size: 1.05rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button {
  display: grid;
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f5f6f3;
  color: var(--ink);
  font-size: 1.25rem;
  font-weight: 600;
}

.panel-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-bottom: 1px solid var(--line);
  background: #f1f3f0;
  padding: 5px;
}

.tab-button {
  min-height: 34px;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 800;
}

.tab-button.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 4px rgba(25, 33, 28, 0.1);
}

.tab-panels {
  min-height: 0;
  flex: 1;
  overflow: hidden;
}

.tab-panel {
  height: 100%;
  min-height: 0;
  overflow: auto;
  padding: 14px 15px;
}

.tab-panel[hidden] {
  display: none;
}

.turn-card {
  display: grid;
  gap: 3px;
  border-left: 4px solid var(--line-strong);
  background: #f2f4f1;
  padding: 12px 13px;
}

.turn-card.my-turn {
  border-color: var(--amber);
  background: var(--amber-soft);
}

.turn-card.waiting {
  border-color: var(--green);
  background: var(--green-soft);
}

.turn-card.finished {
  border-color: var(--blue);
  background: #e1e9ee;
}

.turn-card span,
.turn-card small {
  color: var(--muted);
  font-size: 0.72rem;
}

.turn-card strong {
  font-size: 1.22rem;
  line-height: 1.2;
}

.match-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin: 15px 0 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.match-stats div {
  padding: 11px 9px;
  text-align: center;
}

.match-stats div + div {
  border-left: 1px solid var(--line);
}

.match-stats dt {
  color: var(--muted);
  font-size: 0.66rem;
  font-weight: 750;
  text-transform: uppercase;
}

.match-stats dd {
  margin: 3px 0 0;
  font-size: 1rem;
  font-weight: 850;
}

.game-identity {
  display: grid;
  gap: 1px;
  margin-top: 14px;
  background: var(--line);
}

.game-identity div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  padding: 9px 1px;
}

.game-identity span {
  color: var(--muted);
  font-size: 0.76rem;
}

.game-identity strong {
  font-size: 0.8rem;
}

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 15px;
}

.data-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 7px;
}

.moves-header,
.move-list li {
  display: grid;
  grid-template-columns: 34px 1fr 1fr;
  gap: 8px;
  align-items: center;
}

.moves-header {
  position: sticky;
  top: -14px;
  z-index: 2;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  padding: 10px 8px;
  font-size: 0.68rem;
  font-weight: 800;
  text-transform: uppercase;
}

.move-list {
  display: grid;
  gap: 2px;
  margin: 6px 0 0;
  padding: 0;
  list-style: none;
}

.move-list li {
  min-height: 34px;
  border-radius: 5px;
  padding: 5px 8px;
  font-size: 0.82rem;
}

.move-list li:nth-child(odd) {
  background: #f1f3f0;
}

.move-list .move-number {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.move-cell {
  display: flex;
  min-width: 0;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  font-weight: 800;
}

.move-source {
  width: 5px;
  height: 5px;
  flex: 0 0 5px;
  border-radius: 50%;
  background: var(--green);
}

.move-source.web {
  background: var(--amber);
}

.empty-panel {
  color: var(--muted);
  padding: 34px 0;
  font-size: 0.8rem;
  text-align: center;
}

.connection-list {
  display: grid;
  gap: 1px;
  background: var(--line);
}

.connection-list div {
  display: flex;
  min-height: 42px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
}

.connection-list span {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.76rem;
}

.connection-list strong {
  font-size: 0.76rem;
  text-align: right;
}

.endpoint-box {
  display: grid;
  gap: 6px;
  margin-top: 16px;
}

.endpoint-box > div {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
}

.endpoint-box input {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.69rem;
}

.copy-button {
  border: 1px solid var(--line);
  background: #f3f5f2;
  color: var(--ink);
}

.wide-button {
  width: 100%;
  margin-top: 10px;
}

.diagnostic-block {
  display: grid;
  gap: 6px;
  margin-top: 15px;
}

.diagnostic-block span {
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 800;
  text-transform: uppercase;
}

.diagnostic-block code {
  min-height: 50px;
  overflow-wrap: anywhere;
  border-left: 3px solid var(--line-strong);
  background: #f1f3f0;
  color: var(--ink-soft);
  padding: 9px;
  font-size: 0.68rem;
  line-height: 1.4;
}

.event-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 18px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
  font-size: 0.78rem;
}

.event-heading span {
  color: var(--muted);
}

.event-log {
  display: grid;
  gap: 1px;
  background: var(--line);
}

.event {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 7px;
  background: var(--surface);
  padding: 8px 1px;
}

.event time {
  color: var(--muted);
  font-size: 0.65rem;
  font-variant-numeric: tabular-nums;
}

.event strong {
  font-size: 0.72rem;
  line-height: 1.35;
}

.event.robot strong,
.event.esp32 strong {
  color: var(--green);
}

.event.move strong {
  color: #825315;
}

.panel-footer {
  display: flex;
  min-height: 36px;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-top: 1px solid var(--line);
  background: #f1f3f0;
  color: var(--muted);
  padding: 0 15px;
  font-size: 0.64rem;
  font-weight: 750;
}

.app-dialog {
  width: min(440px, calc(100vw - 28px));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 24px 80px rgba(14, 18, 16, 0.28);
  padding: 0;
}

.app-dialog::backdrop {
  background: rgba(18, 23, 20, 0.58);
  backdrop-filter: blur(3px);
}

.app-dialog form {
  display: grid;
  gap: 9px;
  padding: 18px;
}

.dialog-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 5px;
}

.dialog-heading h2,
.compact-dialog h2 {
  margin-top: 3px;
  font-size: 1.18rem;
}

.side-choice {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 11px 0;
}

.black-disc {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: var(--ink);
}

.side-choice div {
  display: grid;
  gap: 2px;
}

.side-choice strong {
  font-size: 0.82rem;
}

.side-choice small,
.side-choice > span:last-child {
  color: var(--muted);
  font-size: 0.7rem;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 7px;
}

.compact-dialog {
  width: min(380px, calc(100vw - 28px));
}

.compact-dialog p {
  color: var(--muted);
  font-size: 0.83rem;
  line-height: 1.5;
}

.promotion-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
  margin-top: 9px;
}

.promotion-grid button {
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f1f3f0;
  color: var(--ink);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 2.2rem;
}

.toast-stack {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 50;
  display: grid;
  width: min(340px, calc(100vw - 32px));
  gap: 7px;
  pointer-events: none;
}

.toast {
  border-left: 4px solid var(--green);
  border-radius: 6px;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--ink);
  padding: 11px 13px;
  font-size: 0.78rem;
  font-weight: 800;
}

.toast.error {
  border-left-color: var(--red);
}

@media (max-width: 900px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .player-strip,
  .board-shell,
  .move-dock {
    width: min(100%, 680px);
  }

  .control-panel {
    width: min(100%, 680px);
    max-height: none;
    min-height: 560px;
    margin: 6px auto 0;
  }

  .tab-panels {
    min-height: 470px;
  }
}

@media (max-width: 620px) {
  .app-shell {
    width: min(100vw - 16px, 620px);
    padding-top: 8px;
  }

  .app-bar {
    align-items: flex-start;
    flex-direction: column;
    gap: 9px;
  }

  .system-status {
    width: 100%;
    justify-content: flex-start;
  }

  .status-chip {
    min-height: 28px;
    padding: 4px 8px;
  }

  .turn-chip {
    margin-left: auto;
  }

  .player-strip {
    grid-template-columns: 32px minmax(110px, 1fr) auto;
  }

  .captured-pieces {
    display: none;
  }

  .board-shell {
    border-width: 5px;
  }

  .move-dock {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .move-dock label {
    display: none;
  }

  .control-panel {
    min-height: 540px;
  }
}

@media (max-width: 390px) {
  .brand-lockup h1 {
    font-size: 1rem;
  }

  .status-chip {
    font-size: 0.66rem;
  }

  .seat-state {
    max-width: 72px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .action-grid {
    grid-template-columns: 1fr;
  }
}
