:root {
  --bg: #f5efe4;
  --surface: #fbf8f1;
  --surface-strong: #ffffff;
  --border: #dfd4c2;
  --text: #1f1a15;
  --muted: #6e6357;
  --accent: #b84a27;
  --accent-hover: #9c3b1d;
  --accent-soft: #f5e2da;
  --wood-light: #e5be7e;
  --wood-dark: #cfa059;
  --wood-edge: #9b6c30;
  --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: "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 Page */
.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: 8px;
  margin-bottom: 12px;
}

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

.stone-dot.black {
  background: radial-gradient(circle at 32% 30%, #555 0%, #141414 68%, #000 100%);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.35);
}

.stone-dot.white {
  background: radial-gradient(circle at 32% 30%, #fff 0%, #ece7dd 70%, #cbc3b6 100%);
  border: 1px solid rgba(0, 0, 0, 0.14);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.16);
}

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

.lobby-subtitle {
  margin: 0 0 28px;
  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: 12px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.08s, opacity 0.15s;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

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

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #fff;
}

.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) {
  background: #f0e8da;
}

.btn-danger {
  background: #fff5f3;
  color: #b43624;
  border: 1px solid #ecc6bf;
}

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

.btn-bot {
  background: #eef4ff;
  color: #1f4ea8;
  border: 1px solid #c6dafc;
}

.btn-bot:hover:not(:disabled) {
  background: #dfebff;
}

/* Victory / Settlement Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(24, 18, 12, 0.52);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
  z-index: 90;
}

.modal-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px 26px 24px;
  box-shadow: 0 20px 50px rgba(24, 16, 8, 0.28);
  text-align: center;
  transform: translateY(14px) scale(0.97);
  transition: transform 0.22s ease;
}

.modal-backdrop.open .modal-card {
  transform: translateY(0) scale(1);
}

.result-badge {
  width: 64px;
  height: 64px;
  margin: 0 auto 12px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  background: #fff4d6;
  border: 2px solid #f0cb73;
  box-shadow: 0 6px 16px rgba(184, 134, 11, 0.18);
}

.result-badge.lose {
  background: #f3ece2;
  border-color: var(--border);
  box-shadow: none;
}

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

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

.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;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 11px;
  color: var(--muted);
}

.stat-value {
  font-size: 15px;
  color: var(--text);
}

.result-players {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  margin-bottom: 20px;
  border-radius: 10px;
  background: var(--surface-strong);
  border: 1px solid var(--border);
  font-size: 13px;
}

.result-player-side {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
}

.result-player-side.winner {
  color: var(--accent);
}

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

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

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 22px 0;
  color: var(--muted);
  font-size: 12px;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

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

.join-row .text-input {
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.lobby-footnote {
  margin-top: 20px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.6;
  text-align: center;
}

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

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

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

.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 {
  background: #efe6d7;
}

.room-badge {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.08em;
  padding: 6px 12px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
}

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

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

.status-banner.finished {
  background: #e6f4ec;
  border-color: #8cc9a6;
  color: #1f6b41;
}

.room-grid {
  display: grid;
  grid-template-columns: minmax(300px, 640px) minmax(280px, 1fr);
  gap: 24px;
  align-items: start;
}

.board-card {
  background: linear-gradient(145deg, var(--wood-light), var(--wood-dark));
  border: 2px solid var(--wood-edge);
  border-radius: 16px;
  padding: 12px;
  box-shadow: var(--shadow-md);
  position: relative;
}

#boardCanvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  cursor: pointer;
  touch-action: manipulation;
  border-radius: 8px;
}

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

.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);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

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

.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: border-color 0.15s, background 0.15s;
}

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

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

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

.tag {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
  background: #efe7d8;
  color: #6b563c;
}

.tag.agent {
  background: #e3ecff;
  color: #2554b0;
}

.tag.me {
  background: var(--accent-soft);
  color: var(--accent);
}

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

.presence-dot.online {
  background: var(--online);
}

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

.share-actions .btn {
  width: 100%;
  font-size: 14px;
  padding: 10px 12px;
}

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

.control-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.moves-box {
  max-height: 220px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.move-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 7px;
  background: var(--surface-strong);
  border: 1px solid #ece3d4;
}

.move-chip.latest {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.empty-note {
  color: var(--muted);
  font-size: 13px;
  font-family: var(--font-sans);
  grid-column: 1 / -1;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #231e19;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
}

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

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