:root {
  --ink: #1a1612;
  --ink-soft: #3d342b;
  --paper: #f3e6cf;
  --paper-deep: #e4d0a8;
  --wood: #c9a05a;
  --wood-dark: #8b6232;
  --jade: #2f6b5a;
  --jade-bright: #3d8a74;
  --lacquer: #b8432f;
  --mist: rgba(26, 22, 18, 0.06);
  --shadow: rgba(26, 22, 18, 0.28);
  --chat-bg: #221c17;
  --chat-panel: #2c241d;
  --line: rgba(243, 230, 207, 0.12);
  --radius: 14px;
  --font-display: "ZCOOL XiaoWei", "Songti SC", serif;
  --font-body: "Noto Sans SC", "PingFang SC", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: var(--font-body);
  color: var(--ink);
  background: #1e1813;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
}

.bg-grain {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 80% 60% at 15% 20%, rgba(61, 138, 116, 0.22), transparent 55%),
    radial-gradient(ellipse 70% 50% at 85% 75%, rgba(184, 67, 47, 0.16), transparent 50%),
    linear-gradient(160deg, #2a211a 0%, #1a1612 45%, #241c16 100%);
}

.bg-grain::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.35;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.55'/%3E%3C/svg%3E");
  mix-blend-mode: soft-light;
}

button,
input {
  font: inherit;
}

[hidden] {
  display: none !important;
}

/* Lobby */
.lobby {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 2rem 1.25rem;
  overflow: auto;
}

.lobby-inner {
  width: min(420px, 100%);
  animation: rise-in 0.7s ease both;
}

.brand {
  text-align: center;
  margin-bottom: 2rem;
  color: var(--paper);
}

.brand-mark {
  margin: 0 0 0.5rem;
  font-size: 0.75rem;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--jade-bright);
}

.brand h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(2.6rem, 8vw, 3.4rem);
  font-weight: 400;
  letter-spacing: 0.08em;
  line-height: 1.1;
}

.brand-desc {
  margin: 0.85rem 0 0;
  color: rgba(243, 230, 207, 0.65);
  font-size: 0.95rem;
}

.lobby-form {
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  padding: 1.5rem;
  background: rgba(34, 28, 23, 0.72);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  backdrop-filter: blur(10px);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  color: rgba(243, 230, 207, 0.75);
  font-size: 0.85rem;
}

.field input,
.join-row input,
.chat-form input {
  width: 100%;
  padding: 0.75rem 0.9rem;
  border: 1px solid rgba(243, 230, 207, 0.18);
  border-radius: 10px;
  background: rgba(26, 22, 18, 0.65);
  color: var(--paper);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.field input:focus,
.join-row input:focus,
.chat-form input:focus {
  border-color: var(--jade-bright);
  box-shadow: 0 0 0 3px rgba(61, 138, 116, 0.2);
}

.lobby-actions {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.lobby-actions-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
}

.join-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.55rem;
}

.join-row input,
#code-input {
  text-transform: uppercase;
  letter-spacing: 0.12em;
}

.btn {
  appearance: none;
  border: none;
  cursor: pointer;
  border-radius: 10px;
  padding: 0.75rem 1.1rem;
  font-weight: 500;
  transition: transform 0.15s ease, background 0.2s, opacity 0.2s;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, var(--jade-bright), var(--jade));
  color: #f5faf8;
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
}

.btn-secondary {
  background: rgba(243, 230, 207, 0.12);
  color: var(--paper);
  border: 1px solid rgba(243, 230, 207, 0.2);
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(243, 230, 207, 0.18);
}

.btn-ghost {
  background: transparent;
  color: rgba(243, 230, 207, 0.7);
  border: 1px solid transparent;
}

.btn-ghost:hover:not(:disabled) {
  color: var(--paper);
  border-color: rgba(243, 230, 207, 0.2);
}

.error-text {
  margin: 1rem 0 0;
  text-align: center;
  color: #f0a090;
  font-size: 0.9rem;
}

/* Game layout */
.game {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(260px, 320px) 1fr;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas:
    "meta board"
    "chat board";
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  animation: fade-in 0.45s ease both;
}

.game-meta {
  grid-area: meta;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem;
  background: linear-gradient(180deg, var(--chat-panel), var(--chat-bg));
  border-right: 1px solid var(--line);
  color: var(--paper);
  min-height: 0;
  overflow: hidden;
}

.game-chat {
  grid-area: chat;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0 1rem 1rem;
  background: linear-gradient(180deg, var(--chat-bg), #1a1511);
  border-right: 1px solid var(--line);
  color: var(--paper);
}

.status-card {
  padding: 0.85rem 0.95rem;
  background: rgba(26, 22, 18, 0.45);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.status-text {
  margin: 0 0 0.65rem;
  font-size: 0.92rem;
  line-height: 1.35;
}

.players {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.player-chip {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.88rem;
  color: rgba(243, 230, 207, 0.85);
  min-width: 0;
}

.player-chip span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-chip.active {
  color: #fff;
}

.player-chip.active span:last-child::after {
  content: " · 行棋中";
  color: var(--jade-bright);
  font-size: 0.78rem;
}

.stone {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}

.stone-black {
  background: radial-gradient(circle at 35% 30%, #555, #111 60%);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

.stone-white {
  background: radial-gradient(circle at 35% 30%, #fff, #d8d0c4 70%);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.you-role {
  margin: 0.55rem 0 0;
  font-size: 0.78rem;
  color: rgba(243, 230, 207, 0.55);
}

.chat {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: rgba(26, 22, 18, 0.35);
}

.chat-header {
  flex-shrink: 0;
  padding: 0.55rem 0.85rem;
  border-bottom: 1px solid var(--line);
}

.chat-header h2 {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  color: rgba(243, 230, 207, 0.7);
}

.chat-log {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 0.65rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.chat-log::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}

.chat-msg {
  max-width: 88%;
  font-size: 0.84rem;
  line-height: 1.4;
  animation: msg-in 0.25s ease both;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.chat-msg.mine {
  align-self: flex-end;
  align-items: flex-end;
  text-align: right;
}

.chat-msg.theirs {
  align-self: flex-start;
  align-items: flex-start;
  text-align: left;
}

.chat-msg .who {
  font-weight: 500;
  font-size: 0.7rem;
  opacity: 0.85;
}

.chat-msg.black .who {
  color: #9ad0c0;
}

.chat-msg.white .who {
  color: #f0d9a8;
}

.chat-msg .body {
  display: inline-block;
  padding: 0.4rem 0.65rem;
  border-radius: 12px;
  color: rgba(243, 230, 207, 0.92);
  word-break: break-word;
  background: rgba(243, 230, 207, 0.08);
  border: 1px solid rgba(243, 230, 207, 0.12);
}

.chat-msg.mine .body {
  background: rgba(61, 138, 116, 0.28);
  border-color: rgba(61, 138, 116, 0.35);
  border-radius: 12px 12px 4px 12px;
}

.chat-msg.theirs .body {
  border-radius: 12px 12px 12px 4px;
}

.chat-msg.system {
  align-self: center;
  max-width: 100%;
  color: rgba(243, 230, 207, 0.45);
  font-size: 0.76rem;
  text-align: center;
}

.chat-form {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.4rem;
  padding: 0.55rem;
  border-top: 1px solid var(--line);
}

.btn-send {
  padding-inline: 0.85rem;
}

.side-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
}

.board-area {
  grid-area: board;
  display: grid;
  place-items: center;
  min-height: 0;
  min-width: 0;
  padding: 1.25rem;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 40%, rgba(201, 160, 90, 0.12), transparent 55%),
    linear-gradient(180deg, rgba(40, 32, 24, 0.4), rgba(20, 16, 12, 0.65));
}

.board-wrap {
  position: relative;
  width: min(100%, calc(100vh - 2.5rem), 640px);
  width: min(100%, calc(100dvh - 2.5rem), 640px);
  aspect-ratio: 1;
  max-height: 100%;
}

.chat-float {
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  overflow: hidden;
  border-radius: 8px;
}

.chat-bubble {
  position: absolute;
  top: var(--float-y, 16px);
  max-width: min(70%, 280px);
  padding: 0.55rem 0.85rem;
  border-radius: 14px;
  background: rgba(34, 28, 23, 0.92);
  border: 1px solid rgba(243, 230, 207, 0.22);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.35);
  color: var(--paper);
  opacity: 0;
  transform: translateY(10px) scale(0.96);
  transition:
    opacity 0.35s ease,
    transform 0.35s ease;
}

.chat-bubble.show {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.chat-bubble.hide {
  opacity: 0;
  transform: translateY(-8px) scale(0.98);
}

.chat-bubble.mine {
  right: 14px;
  border-color: rgba(61, 138, 116, 0.45);
  background: rgba(36, 48, 42, 0.94);
}

.chat-bubble.theirs {
  left: 14px;
}

.chat-bubble .who {
  display: block;
  font-size: 0.72rem;
  font-weight: 500;
  margin-bottom: 0.15rem;
  opacity: 0.8;
}

.chat-bubble.black .who {
  color: #9ad0c0;
}

.chat-bubble.white .who {
  color: #f0d9a8;
}

.chat-bubble .body {
  display: block;
  font-size: 0.92rem;
  line-height: 1.4;
  word-break: break-word;
}

#board {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 8px;
  box-shadow:
    0 24px 48px var(--shadow),
    0 0 0 1px rgba(139, 98, 50, 0.35);
  cursor: crosshair;
  touch-action: none;
}

.result-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(26, 22, 18, 0.45);
  border-radius: 8px;
  backdrop-filter: blur(2px);
  pointer-events: none;
  animation: fade-in 0.35s ease both;
}

.result-overlay p {
  margin: 0;
  padding: 0.9rem 1.4rem;
  font-family: var(--font-display);
  font-size: 1.8rem;
  letter-spacing: 0.12em;
  color: var(--paper);
  background: rgba(34, 28, 23, 0.88);
  border: 1px solid rgba(243, 230, 207, 0.25);
  border-radius: 12px;
}

.toast {
  position: fixed;
  z-index: 20;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%);
  padding: 0.7rem 1.1rem;
  background: rgba(34, 28, 23, 0.95);
  color: var(--paper);
  border: 1px solid rgba(243, 230, 207, 0.2);
  border-radius: 999px;
  font-size: 0.88rem;
  animation: rise-in 0.3s ease both;
}

@keyframes rise-in {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes msg-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 900px) {
  .lobby {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .game {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr) clamp(11.5rem, 28dvh, 14rem);
    grid-template-areas:
      "meta"
      "board"
      "chat";
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .game-meta {
    border-right: none;
    border-bottom: 1px solid var(--line);
    padding: 0.55rem 0.7rem 0.65rem;
    gap: 0.45rem;
    padding-top: max(0.55rem, env(safe-area-inset-top));
  }

  .status-card {
    padding: 0.55rem 0.7rem;
  }

  .status-text {
    margin-bottom: 0.45rem;
    font-size: 0.84rem;
  }

  .players {
    flex-direction: row;
    gap: 0.55rem;
  }

  .player-chip {
    flex: 1;
    min-width: 0;
    font-size: 0.8rem;
  }

  .player-chip.active span:last-child::after {
    content: "";
  }

  .you-role {
    margin-top: 0.35rem;
    font-size: 0.72rem;
  }

  .side-actions {
    gap: 0.35rem;
  }

  .side-actions .btn {
    padding: 0.5rem 0.6rem;
    font-size: 0.8rem;
  }

  .board-area {
    padding: 0.35rem 0.6rem;
    min-height: 0;
    overflow: hidden;
    container-type: size;
  }

  .board-wrap {
    width: min(100cqw, 100cqh);
    height: min(100cqw, 100cqh);
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: 1;
  }

  .result-overlay p {
    font-size: 1.25rem;
    padding: 0.65rem 1rem;
    letter-spacing: 0.08em;
  }

  .game-chat {
    border-right: none;
    border-top: 1px solid var(--line);
    padding: 0.4rem 0.65rem;
    padding-bottom: max(0.4rem, env(safe-area-inset-bottom));
    height: auto;
    max-height: none;
    min-height: 0;
    overflow: hidden;
    background: linear-gradient(180deg, var(--chat-panel), var(--chat-bg));
  }

  .chat {
    border-radius: 10px;
  }

  .chat-header {
    padding: 0.35rem 0.65rem;
  }

  .chat-header h2 {
    font-size: 0.75rem;
  }

  .chat-log {
    padding: 0.4rem 0.55rem;
    gap: 0.3rem;
  }

  .chat-msg {
    font-size: 0.78rem;
  }

  .chat-msg .body {
    padding: 0.3rem 0.5rem;
  }

  .chat-form {
    padding: 0.35rem;
    gap: 0.3rem;
  }

  .chat-form input {
    padding: 0.5rem 0.65rem;
    font-size: 0.84rem;
  }

  .btn-send {
    padding-inline: 0.7rem;
    font-size: 0.8rem;
  }

  .toast {
    bottom: max(1rem, env(safe-area-inset-bottom));
  }
}

