:root {
  --bg-main: #0c0e12;
  --bg-surface: #14171d;
  --bg-input: #0e1015;
  --border: #232834;
  --border-strong: #323949;
  --text-main: #eceff4;
  --text-muted: #8b94a3;
  --accent: #d4a34f;
  --accent-hover: #e0b263;
  --accent-2: #5b8def;
  --sq-light: #e5e0d5;
  --sq-dark: #5f6c78;
  --danger: #d9534f;
  --success: #3ea872;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg-main);
  color: var(--text-main);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.surface {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

/* Topbar */
.topbar {
  max-width: 1180px;
  margin: 0 auto;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text-main);
}

.brand-icon {
  width: 22px;
  height: 22px;
  color: var(--accent);
}

.brand-name {
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.room-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px 4px 12px;
  border-radius: 8px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  font-size: 0.82rem;
}

.room-label {
  color: var(--text-muted);
}

.room-badge strong {
  font-family: 'JetBrains Mono', monospace;
  color: var(--accent);
  letter-spacing: 0.08em;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.icon-btn:hover {
  color: var(--text-main);
  border-color: var(--border-strong);
}

.icon-btn svg {
  width: 16px;
  height: 16px;
}

.room-badge .icon-btn {
  width: 26px;
  height: 26px;
  border: none;
  background: transparent;
}

/* Lobby */
.lobby-wrap {
  max-width: 920px;
  margin: 36px auto 56px;
  padding: 0 20px;
}

.lobby-header {
  margin-bottom: 24px;
}

.lobby-header h1 {
  font-size: clamp(1.35rem, 2.5vw, 1.75rem);
  font-weight: 700;
  letter-spacing: -0.025em;
  margin-bottom: 6px;
}

.lobby-header p {
  color: var(--text-muted);
  font-size: 0.94rem;
}

.lobby-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
  align-items: stretch;
}

.lobby-card {
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.card-head h2 {
  font-size: 1rem;
  font-weight: 600;
}

.tag-subtle {
  font-size: 0.74rem;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.field label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
}

.mono-val {
  font-family: 'JetBrains Mono', monospace;
  color: var(--accent);
  font-size: 0.86rem;
}

input[type='text'],
select,
textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-main);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
}

input[type='text']:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
}

input[type='range'] {
  accent-color: var(--accent);
  cursor: pointer;
}

.code-input {
  font-family: 'JetBrains Mono', monospace !important;
  font-size: 1.2rem !important;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  text-align: center;
  font-weight: 600;
}

.two-cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.btn-primary,
.btn-secondary,
.btn-ghost,
.btn-danger-ghost {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px 16px;
  font-family: inherit;
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn-primary {
  background: var(--accent);
  color: #111318;
}

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

.btn-secondary {
  background: #232936;
  color: var(--text-main);
  border-color: var(--border-strong);
}

.btn-secondary:hover {
  background: #2b3242;
}

.btn-ghost {
  background: transparent;
  color: var(--text-main);
  border-color: var(--border);
}

.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.04);
  border-color: var(--border-strong);
}

.btn-danger-ghost {
  background: transparent;
  color: #e2807d;
  border-color: rgba(217, 83, 79, 0.35);
}

.btn-danger-ghost:hover {
  background: rgba(217, 83, 79, 0.1);
}

.btn-primary:active,
.btn-secondary:active,
.btn-ghost:active {
  transform: translateY(1px);
}

.btn-sm {
  padding: 8px 12px;
  font-size: 0.82rem;
}

.rules-note {
  margin-top: auto;
  padding: 13px 14px;
  border-radius: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rules-note-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-main);
}

.rules-note p {
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.45;
}

/* Waiting */
.waiting-wrap {
  max-width: 460px;
  margin: 72px auto;
  padding: 0 20px;
}

.waiting-card {
  padding: 28px 24px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.waiting-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 rgba(212, 163, 79, 0.5);
  animation: waitPulse 1.8s infinite;
}

@keyframes waitPulse {
  0% { box-shadow: 0 0 0 0 rgba(212, 163, 79, 0.5); }
  70% { box-shadow: 0 0 0 12px rgba(212, 163, 79, 0); }
  100% { box-shadow: 0 0 0 0 rgba(212, 163, 79, 0); }
}

.waiting-card h2 {
  font-size: 1.15rem;
}

.waiting-card p {
  color: var(--text-muted);
  font-size: 0.88rem;
  line-height: 1.45;
}

.invite-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 8px 14px;
  border-radius: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
}

.big-code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: 0.18em;
  color: var(--accent);
}

.settings-pills {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.settings-pills span {
  font-size: 0.76rem;
  padding: 4px 9px;
  border-radius: 6px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

/* Game Layout */
.game-layout {
  max-width: 1140px;
  margin: 18px auto 36px;
  padding: 0 20px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 22px;
  align-items: start;
}

.arena {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.ai-banner {
  width: min(600px, 100%);
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--bg-surface);
  border: 1px solid var(--accent);
  text-align: center;
  font-weight: 600;
  font-size: 0.86rem;
}

.player-bar {
  width: min(600px, 100%);
  padding: 9px 12px;
  border-radius: 10px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.player-bar.active-turn {
  border-color: var(--accent);
}

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

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  flex-shrink: 0;
}

.avatar.w {
  background: #eceae4;
}

.avatar.b {
  background: #232730;
}

.player-name-row {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-wrap: wrap;
}

.player-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.player-tag {
  font-size: 0.72rem;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 5px;
  border: 1px solid currentColor;
  background: rgba(255, 255, 255, 0.04);
}

.online-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--success);
}

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

.captured-row {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  margin-top: 2px;
  min-height: 15px;
}

.captured-mini {
  width: 14px;
  height: 14px;
  opacity: 0.9;
}

.turn-pill {
  font-size: 0.74rem;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 6px;
  background: rgba(212, 163, 79, 0.14);
  color: var(--accent);
  white-space: nowrap;
}

.board-stage {
  position: relative;
  width: min(600px, calc(100vw - 32px));
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  transition: transform 0.55s cubic-bezier(0.22, 1, 0.36, 1);
}

.board-grid {
  width: 100%;
  height: 100%;
  display: grid;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border-strong);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.45);
  user-select: none;
}

.board-overlays {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 6;
  display: grid;
  place-items: center;
}

.overlay-item {
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  cursor: pointer;
}

.square.light {
  background: var(--sq-light);
  color: #3a424c;
}

.square.dark {
  background: var(--sq-dark);
  color: #e5e0d5;
}

.square.last-move::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(212, 163, 79, 0.34);
  pointer-events: none;
}

.square.selected {
  box-shadow: inset 0 0 0 3px var(--accent);
}

.square.in-check {
  background: #b83b38 !important;
}

.coord-rank,
.coord-file {
  position: absolute;
  font-family: 'JetBrains Mono', monospace;
  font-size: 9px;
  font-weight: 600;
  opacity: 0.65;
  pointer-events: none;
  z-index: 2;
}

.coord-rank {
  top: 2px;
  left: 3px;
}

.coord-file {
  bottom: 1px;
  right: 3px;
}

.legal-dot {
  position: absolute;
  width: 26%;
  height: 26%;
  border-radius: 50%;
  background: rgba(15, 20, 28, 0.3);
  z-index: 3;
  pointer-events: none;
}

.square.dark .legal-dot {
  background: rgba(255, 255, 255, 0.3);
}

.legal-ring {
  position: absolute;
  inset: 7%;
  border-radius: 50%;
  border: 3px solid rgba(212, 163, 79, 0.9);
  z-index: 3;
  pointer-events: none;
}

.piece-wrap {
  width: 84%;
  height: 84%;
  display: grid;
  place-items: center;
  z-index: 4;
  transition: transform 0.12s ease;
}

.piece-wrap svg {
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.32));
}

.custom-piece-badge {
  width: 86%;
  height: 86%;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 1.1rem;
  border: 2px solid currentColor;
}

.custom-piece-badge.w {
  background: #f5f3ee;
  color: #161920;
}

.custom-piece-badge.b {
  background: #1f242d;
  color: #f5f3ee;
}

.loot-orb {
  position: absolute;
  width: 58%;
  height: 58%;
  z-index: 5;
  pointer-events: none;
  color: #e6b44c;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.55));
  animation: lootPulse 2s ease-in-out infinite;
}

@keyframes lootPulse {
  0%, 100% { transform: scale(0.94); }
  50% { transform: scale(1.06); }
}

/* Sidebar */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-card {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-head h3 {
  font-size: 0.88rem;
  font-weight: 600;
}

.mono-pill {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.72rem;
  padding: 2px 7px;
  border-radius: 5px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.status-line {
  padding: 9px 11px;
  border-radius: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 600;
}

.status-line.check {
  border-color: var(--danger);
  color: #f09593;
}

.status-line.wish {
  border-color: var(--accent);
  color: #f0cf8b;
}

.status-line.win {
  border-color: var(--success);
  color: #8ce0b3;
}

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

.rules-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 140px;
  overflow-y: auto;
}

.rules-list li {
  font-size: 0.79rem;
  padding: 6px 9px;
  border-radius: 6px;
  background: var(--bg-input);
  border-left: 2px solid var(--accent-2);
}

.rules-list li.empty-note {
  background: transparent;
  border-left: none;
  color: var(--text-muted);
  padding: 0;
}

.logs-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-height: 260px;
  overflow-y: auto;
}

.log-item {
  font-size: 0.78rem;
  line-height: 1.38;
  padding: 5px 8px;
  border-radius: 6px;
  background: var(--bg-input);
  color: var(--text-muted);
}

.log-item.wish {
  color: #f1e3c4;
  border-left: 2px solid var(--accent);
}

.log-item.loot,
.log-item.loot_spawn {
  color: #e4c17a;
}

.log-item.end {
  color: #8ce0b3;
  font-weight: 600;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 10, 14, 0.76);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 16px;
}

.modal-card {
  width: min(500px, 100%);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-head h2 {
  font-size: 1.05rem;
  font-weight: 600;
}

.modal-head p {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: 3px;
}

.chips-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.wish-chip {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-main);
  padding: 5px 9px;
  border-radius: 6px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: border-color 0.15s;
}

.wish-chip:hover {
  border-color: var(--accent);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.error-text {
  color: #f09593;
  font-size: 0.82rem;
}

.promo-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.promo-btn {
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-main);
  font-weight: 600;
  cursor: pointer;
}

.promo-btn:hover {
  border-color: var(--accent);
}

/* Toasts */
.toast-container {
  position: fixed;
  bottom: 18px;
  right: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 100;
}

.toast {
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  color: var(--text-main);
  font-size: 0.84rem;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45);
}

@media (max-width: 920px) {
  .lobby-grid,
  .game-layout,
  .two-cols {
    grid-template-columns: 1fr;
  }
}
