:root {
  --wood: #d4a574;
  --wood-dark: #b8895a;
  --wood-line: #5c3d1e;
  --ink: #1a1410;
  --paper: #f3e6d2;
  --panel: rgba(26, 20, 16, 0.82);
  --accent: #2f6f4e;
  --danger: #a33b2c;
  --muted: #7a6550;
  --radius: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
    sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(ellipse at top, #efe0c8 0%, transparent 55%),
    linear-gradient(160deg, #e8d2b0 0%, #c9a57a 48%, #a67c52 100%);
  color: var(--ink);
}

.view[hidden] {
  display: none !important;
}

/* —— Lobby —— */
#lobby {
  height: 100%;
  overflow: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  padding: calc(1.5rem + var(--safe-top)) 1.25rem calc(1.5rem + var(--safe-bottom));
}

.lobby-header {
  text-align: center;
  margin-bottom: 0.5rem;
}

.brand {
  margin: 0;
  font-size: clamp(2.4rem, 8vw, 3.6rem);
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--ink);
}

.tagline {
  margin: 0.4rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.lobby-panel {
  width: min(22rem, 100%);
  background: rgba(255, 248, 235, 0.72);
  border: 1px solid rgba(92, 61, 30, 0.18);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem 1.25rem;
  backdrop-filter: blur(6px);
}

.lobby-panel h2 {
  margin: 0 0 0.85rem;
  font-size: 1rem;
  font-weight: 600;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.85rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.field input,
.field select {
  appearance: none;
  border: 1px solid rgba(92, 61, 30, 0.28);
  background: #fffaf2;
  color: var(--ink);
  border-radius: 8px;
  padding: 0.7rem 0.8rem;
  font-size: 1rem;
  font-family: inherit;
}

.field input {
  letter-spacing: 0.18em;
  text-transform: uppercase;
  font-weight: 600;
}

.error {
  color: var(--danger);
  font-size: 0.9rem;
  margin: 0;
}

/* —— Buttons —— */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: 1px solid rgba(92, 61, 30, 0.25);
  background: #fff8ee;
  color: var(--ink);
  border-radius: 8px;
  padding: 0.65rem 1rem;
  font-size: 0.95rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  transition: transform 0.08s ease, background 0.15s ease;
}

.btn:hover:not(:disabled) {
  background: #fff;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--accent);
  border-color: transparent;
  color: #f5fff8;
}

.btn.primary:hover:not(:disabled) {
  background: #265c40;
}

.btn.danger {
  background: var(--danger);
  border-color: transparent;
  color: #fff5f3;
}

.btn.danger:hover:not(:disabled) {
  background: #8a2f23;
}

.btn.ghost {
  background: transparent;
  border-color: transparent;
  width: auto;
  padding: 0.35rem 0.5rem;
  font-weight: 500;
}

/* —— Game —— */
#game {
  height: 100%;
  max-height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: calc(0.45rem + var(--safe-top)) 0.75rem 0.45rem;
  background: var(--panel);
  color: #f6ebdc;
  font-size: 0.85rem;
  flex-wrap: nowrap;
  flex-shrink: 0;
}

.topbar-left,
.topbar-center,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.brand-mini {
  font-weight: 700;
  letter-spacing: 0.04em;
  opacity: 0.9;
}

#btn-copy {
  color: #f6ebdc;
  font-size: 0.85rem;
}

.pill {
  display: inline-block;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  font-size: 0.8rem;
}

.turn {
  font-weight: 600;
}

.cap {
  opacity: 0.9;
  white-space: nowrap;
}

.cap b {
  font-variant-numeric: tabular-nums;
}

.board-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  padding: 0.5rem;
}

#board {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1;
  border-radius: 4px;
  box-shadow: 0 10px 28px rgba(40, 24, 10, 0.28);
  touch-action: none;
  cursor: crosshair;
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: 0.55rem 0.75rem calc(0.65rem + var(--safe-bottom));
  background: var(--panel);
  color: #f6ebdc;
  flex-shrink: 0;
}

.status-msg {
  margin: 0;
  font-size: 0.85rem;
  opacity: 0.92;
  text-align: center;
  min-height: 1.2em;
}

.toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  justify-content: stretch;
}

.toolbar-actions .btn {
  flex: 1 1 calc(50% - 0.45rem);
  width: auto;
  min-width: 5.5rem;
  padding: 0.7rem 0.4rem;
  font-size: 0.9rem;
}

.toolbar-actions .btn.ghost {
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #f6ebdc;
}

@media (max-width: 480px) {
  .topbar {
    font-size: 0.78rem;
    flex-wrap: wrap;
  }

  .topbar-right {
    width: 100%;
    justify-content: center;
    order: 3;
  }

  .toolbar-actions .btn {
    flex: 1 1 calc(50% - 0.45rem);
  }
}

@media (min-width: 900px) {
  .toolbar {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .status-msg {
    text-align: left;
    flex: 1;
  }

  .toolbar-actions {
    width: min(28rem, 55%);
    flex-wrap: nowrap;
  }

  .toolbar-actions .btn {
    flex: 1 1 0;
  }
}
