:root {
  --bg: #f5efe4;
  --surface: #fbf8f1;
  --surface-strong: #ffffff;
  --border: #dfd4c2;
  --text: #1f1a15;
  --muted: #6e6357;
  --accent: #b83324;
  --accent-hover: #96271b;
  --accent-soft: #f7e1dc;
  --red-piece: #b8221e;
  --black-piece: #1d242b;
  --online: #2e8b57;
  --offline: #b3a89a;
  --shadow-sm: 0 2px 8px rgba(45, 32, 16, 0.06);
  --shadow-md: 0 10px 30px rgba(45, 32, 16, 0.12);
  --radius: 14px;
  --font-sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  --font-serif: "Kaiti SC", "STKaiti", "Songti SC", "STSong", "Noto Serif CJK SC", Georgia, serif;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: radial-gradient(circle at top, #faf5ec 0%, var(--bg) 70%);
  color: var(--text);
  font-family: var(--font-sans);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
  text-decoration: none;
}

button, input {
  font-family: inherit;
}

/* Lobby */
.lobby-wrap {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.lobby-card {
  width: 100%;
  max-width: 460px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 32px;
  box-shadow: var(--shadow-md);
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.piece-badge {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-serif);
  font-size: 18px;
  font-weight: 700;
  background: radial-gradient(circle at 35% 30%, #fffdf7 0%, #f1dfc0 72%, #d8bc8e 100%);
  box-shadow: 0 3px 6px rgba(45, 32, 16, 0.22), inset 0 0 0 2px rgba(255, 255, 255, 0.55);
}

.piece-badge.red {
  color: var(--red-piece);
  border: 1.5px solid var(--red-piece);
}

.piece-badge.black {
  color: var(--black-piece);
  border: 1.5px solid var(--black-piece);
}

.side-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.side-dot.red {
  background: radial-gradient(circle at 32% 30%, #e65545 0%, #b8221e 72%, #7f1411 100%);
  box-shadow: 0 2px 4px rgba(184, 34, 30, 0.3);
}

.side-dot.black {
  background: radial-gradient(circle at 32% 30%, #4b5563 0%, #1d242b 72%, #0a0d10 100%);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.32);
}

.lobby-title {
  margin: 0 0 8px;
  font-family: var(--font-serif);
  font-size: 32px;
  letter-spacing: 0.06em;
}

.lobby-subtitle {
  margin: 0 0 26px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

.field {
  margin-bottom: 20px;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 8px;
}

.text-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-strong);
  font-size: 15px;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

.text-input:focus {
  border-color: var(--accent);
}

.color-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.color-opt {
  padding: 10px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-strong);
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.15s;
}

.color-opt.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: 10px;
  padding: 11px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 12px rgba(184, 51, 36, 0.24);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface-strong);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-bot {
  width: 100%;
  background: linear-gradient(135deg, #2e6f56 0%, #1f513e 100%);
  color: #fff;
  box-shadow: 0 4px 12px rgba(31, 81, 62, 0.22);
}

.btn-bot:hover:not(:disabled) {
  filter: brightness(1.06);
}

.btn-danger {
  background: #fdf2f0;
  color: #b42318;
  border: 1px solid #f2c6c0;
}

.btn-danger:hover:not(:disabled) {
  background: #fbe4e0;
}

.divider {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  margin: 22px 0 14px;
  position: relative;
}

.divider::before,
.divider::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 26%;
  height: 1px;
  background: var(--border);
}

.divider::before { left: 0; }
.divider::after { right: 0; }

.join-row {
  display: flex;
  gap: 8px;
}

.join-row .btn-secondary {
  flex-shrink: 0;
  padding: 0 20px;
}

.lobby-footnote {
  margin: 22px 0 0;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}

/* Room Layout */
.room-shell {
  max-width: 1120px;
  margin: 0 auto;
  padding: 18px 20px 36px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.back-link {
  font-size: 14px;
  color: var(--muted);
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.back-link:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.room-badge {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.04em;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 8px;
}

.tag {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}

.tag.agent {
  background: #e6f2ec;
  color: #1f5c45;
}

.tag.me {
  background: #efe6d5;
  color: #6b4b24;
}

.status-banner {
  padding: 8px 16px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

.status-banner.your-turn {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.status-banner.in-check {
  background: #fde2df;
  border-color: #c81e1e;
  color: #b42318;
  animation: pulseCheck 1.2s ease-in-out infinite;
}

@keyframes pulseCheck {
  0%, 100% { box-shadow: 0 0 0 0 rgba(200, 30, 30, 0.28); }
  50% { box-shadow: 0 0 0 6px rgba(200, 30, 30, 0); }
}

.room-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  align-items: start;
}

.board-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  box-shadow: var(--shadow-md);
  display: flex;
  justify-content: center;
  align-items: center;
}

#boardCanvas {
  width: 100%;
  max-width: 660px;
  height: auto;
  display: block;
  border-radius: 10px;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 6px 20px rgba(72, 46, 16, 0.18);
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}

.panel-title {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.players-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-strong);
  border: 1.5px solid transparent;
  transition: all 0.15s;
}

.player-row.active-turn {
  border-color: var(--accent);
  background: #fff9f7;
}

.player-info {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.player-name {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.presence-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--offline);
  flex-shrink: 0;
}

.presence-dot.online {
  background: var(--online);
  box-shadow: 0 0 0 3px rgba(46, 139, 87, 0.18);
}

.share-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.share-actions .btn {
  width: 100%;
}

.share-hint {
  margin: 10px 0 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

.control-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.control-row .btn {
  padding: 9px 8px;
  font-size: 13px;
}

.moves-box {
  max-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  padding-right: 4px;
}

.move-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 8px;
  border-radius: 7px;
  background: var(--surface-strong);
  border: 1px solid #ede5d8;
}

.move-item.latest {
  border-color: var(--accent);
  background: #fff6f4;
}

.move-left {
  display: flex;
  align-items: center;
  gap: 7px;
}

.move-seq {
  color: var(--muted);
  font-size: 12px;
  min-width: 24px;
}

.move-zh {
  font-weight: 700;
  font-family: var(--font-serif);
  font-size: 14px;
}

.move-zh.red { color: var(--red-piece); }
.move-zh.black { color: var(--black-piece); }

.move-ucci {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: var(--muted);
}

.empty-note {
  color: var(--muted);
  font-size: 13px;
}

/* Result Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(25, 18, 10, 0.52);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 40;
}

.modal-backdrop.open {
  display: flex;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}

.result-badge {
  font-size: 42px;
  margin-bottom: 6px;
}

.result-title {
  margin: 0 0 6px;
  font-family: var(--font-serif);
  font-size: 26px;
}

.result-subtitle {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 14px;
}

.result-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.stat-item {
  background: var(--surface-strong);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 6px;
}

.stat-label {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 4px;
}

.stat-value {
  font-size: 15px;
}

.result-players {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 20px;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.modal-actions .btn {
  width: 100%;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(16px);
  background: #231d17;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: all 0.2s ease;
  z-index: 60;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.24);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 900px) {
  .room-grid {
    grid-template-columns: 1fr;
  }
  .board-card {
    padding: 10px;
  }
}
