:root {
  --ink: #202019;
  --muted: #777267;
  --paper: #eee8d9;
  --paper-light: #f7f3e9;
  --paper-deep: #ddd2bd;
  --red: #a8392f;
  --red-dark: #762820;
  --gold: #ad8650;
  --black: #161713;
  --line: rgba(60, 49, 35, 0.16);
  /* 不依赖 Google Fonts（国内加载不出来）：优先本机已安装字体，否则回退系统中文衬线/楷体 */
  --serif: "Noto Serif SC", "Source Han Serif SC", "Source Han Serif CN", "Songti SC", "SimSun", "STSong", serif;
  --display: "ZCOOL XiaoWei", "KaiTi", "STKaiti", "Kaiti SC", "FangSong", "STFangsong", serif;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: #171713;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  font-family: var(--serif);
  background:
    radial-gradient(circle at 16% 0%, rgba(174, 136, 81, 0.17), transparent 31rem),
    linear-gradient(135deg, #181814 0%, #22211b 48%, #151612 100%);
}

button,
input,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 3px;
}

.paper-texture {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  opacity: 0.22;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 180 180' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.17'/%3E%3C/svg%3E");
}

.topbar {
  height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 clamp(20px, 4vw, 68px);
  border-bottom: 1px solid rgba(234, 223, 197, 0.1);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #eee8d9;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 1px solid rgba(221, 189, 134, 0.55);
  color: #e4c58c;
  font-family: var(--display);
  font-size: 25px;
  transform: rotate(-2deg);
}

.brand strong {
  display: block;
  font-family: var(--display);
  font-size: 23px;
  letter-spacing: 0.16em;
}

.brand small {
  display: block;
  margin-top: 1px;
  color: #8f8b7f;
  font-size: 8px;
  letter-spacing: 0.28em;
}

.topbar-actions,
.api-status {
  display: flex;
  align-items: center;
}

.topbar-actions {
  gap: 14px;
}

.api-status {
  gap: 8px;
  color: #9b978b;
  font-size: 12px;
}

.api-status i,
.online-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #bd9d61;
  box-shadow: 0 0 0 4px rgba(189, 157, 97, 0.1);
}

.api-status.connected i,
.online-dot {
  background: #769b6d;
}

.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid rgba(239, 230, 208, 0.15);
  border-radius: 50%;
  color: #c8c0ad;
  background: rgba(255, 255, 255, 0.025);
  cursor: pointer;
  transition: 180ms ease;
}

.icon-button:hover {
  border-color: rgba(224, 196, 142, 0.5);
  color: #e0c48e;
  transform: rotate(12deg);
}

.game-shell {
  display: grid;
  grid-template-columns: minmax(540px, 1.45fr) minmax(330px, 0.72fr);
  width: min(1420px, calc(100% - 48px));
  min-height: calc(100vh - 112px);
  margin: 18px auto;
  overflow: hidden;
  border: 1px solid rgba(221, 207, 178, 0.12);
  border-radius: 5px;
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.28);
}

.board-panel {
  min-width: 0;
  padding: clamp(24px, 3vw, 48px);
  background:
    linear-gradient(rgba(247, 243, 233, 0.93), rgba(237, 230, 214, 0.95)),
    radial-gradient(circle at top left, #fff, #ded4bd);
}

.match-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  max-width: 780px;
  margin: 0 auto 28px;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--red);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.25em;
  text-transform: uppercase;
}

.match-heading h1 {
  margin: 0;
  font-family: var(--display);
  font-size: clamp(28px, 3vw, 43px);
  font-weight: 500;
  letter-spacing: 0.04em;
}

.match-heading h1 em {
  color: var(--red);
  font-style: normal;
}

.turn-card {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 130px;
  padding: 10px 13px;
  border-left: 2px solid var(--red);
  background: rgba(255, 255, 255, 0.38);
}

.turn-card small,
.turn-card strong {
  display: block;
}

.turn-card small {
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 9px;
}

.turn-card strong {
  font-size: 13px;
}

.turn-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--red);
  box-shadow: 0 0 0 4px rgba(168, 57, 47, 0.12);
}

.players-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 14px;
  max-width: 780px;
  margin: 0 auto 15px;
}

.player-card {
  display: flex;
  align-items: center;
  gap: 11px;
}

.human-player {
  justify-content: flex-end;
  text-align: right;
}

.player-card strong,
.player-card span {
  display: block;
}

.player-card strong {
  margin-bottom: 4px;
  font-size: 13px;
}

.player-card span {
  color: var(--muted);
  font-size: 9px;
}

.player-card span .online-dot {
  display: inline-block;
  width: 5px;
  height: 5px;
  margin-right: 5px;
}

.avatar {
  display: grid;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  place-items: center;
  border-radius: 50%;
  font-family: var(--display);
}

.ai-avatar {
  border: 1px solid #37372f;
  color: #ded6c3;
  background: #25251f;
}

.human-avatar {
  border: 1px solid rgba(158, 50, 43, 0.45);
  color: var(--red);
  background: rgba(168, 57, 47, 0.07);
}

.side-piece {
  display: grid;
  width: 29px;
  height: 29px;
  place-items: center;
  border: 1px solid;
  border-radius: 50%;
  font-size: 13px;
  font-weight: 600;
  background: #eee5cf;
}

.black-piece {
  border-color: #48473d;
  color: #33332c;
}

.red-piece {
  border-color: #a94339;
  color: #9f3029;
}

.versus {
  color: #9a9384;
  font-size: 9px;
  letter-spacing: 0.18em;
}

.board-wrap {
  position: relative;
  width: min(100%, 620px);
  margin: 0 auto;
}

#chessboard {
  display: block;
  width: 100%;
  max-width: 100%;
  aspect-ratio: 9 / 10;
  border-radius: 3px;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow:
    0 16px 35px rgba(67, 47, 25, 0.18),
    0 0 0 1px rgba(86, 54, 25, 0.25);
}

.thinking-badge {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 3;
  align-items: center;
  gap: 4px;
  padding: 10px 14px;
  border: 1px solid rgba(238, 224, 193, 0.16);
  border-radius: 30px;
  color: #eee7d7;
  background: rgba(27, 27, 23, 0.92);
  box-shadow: 0 9px 30px rgba(0, 0, 0, 0.3);
  transform: translate(-50%, -50%);
  backdrop-filter: blur(8px);
}

.thinking-badge:not([hidden]) {
  display: flex;
}

.thinking-badge span {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: #d6b67a;
  animation: thinking 1s infinite ease-in-out;
}

.thinking-badge span:nth-child(2) {
  animation-delay: 120ms;
}

.thinking-badge span:nth-child(3) {
  margin-right: 5px;
  animation-delay: 240ms;
}

.thinking-badge b {
  font-size: 10px;
  font-weight: 500;
}

@keyframes thinking {
  0%,
  100% {
    transform: translateY(1px);
    opacity: 0.4;
  }
  50% {
    transform: translateY(-2px);
    opacity: 1;
  }
}

.game-toolbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  max-width: 620px;
  margin: 17px auto 0;
}

.toolbar-button {
  min-width: 0;
  padding: 10px 8px;
  border: 1px solid rgba(64, 56, 45, 0.15);
  border-radius: 2px;
  color: #514d44;
  background: rgba(255, 255, 255, 0.32);
  font-size: 10px;
  cursor: pointer;
  transition: 160ms ease;
}

.toolbar-button span {
  display: inline-grid;
  width: 22px;
  height: 22px;
  margin-right: 5px;
  place-items: center;
  border: 1px solid currentColor;
  border-radius: 50%;
  font-size: 9px;
}

.toolbar-button:hover:not(:disabled) {
  border-color: rgba(154, 58, 47, 0.45);
  color: var(--red);
  background: rgba(255, 255, 255, 0.6);
  transform: translateY(-1px);
}

.toolbar-button.primary {
  border-color: #2e302a;
  color: #eee8d9;
  background: #292a24;
}

.toolbar-button.danger {
  color: #93443d;
}

.toolbar-button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.companion-panel {
  display: flex;
  min-width: 0;
  min-height: 0;
  flex-direction: column;
  padding: clamp(22px, 2.3vw, 36px);
  color: #e7e0d1;
  background:
    linear-gradient(160deg, rgba(36, 36, 30, 0.98), rgba(24, 25, 21, 0.99)),
    #1c1d19;
}

.companion-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 22px;
  border-bottom: 1px solid rgba(237, 226, 203, 0.09);
}

.companion-identity {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar.large {
  width: 46px;
  height: 46px;
  border-color: rgba(221, 191, 137, 0.35);
  color: #ddc18d;
}

.companion-identity p {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 3px;
}

.companion-identity strong {
  font-family: var(--display);
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 0.08em;
}

.companion-identity small {
  color: #8d8b82;
  font-size: 9px;
}

.text-button {
  padding: 6px 0;
  border: 0;
  border-bottom: 1px solid #8b734d;
  color: #bba171;
  background: none;
  font-size: 9px;
  cursor: pointer;
}

.insight-card {
  margin: 20px 0 10px;
  padding: 16px;
  border: 1px solid rgba(215, 190, 142, 0.16);
  border-left: 2px solid #9c7546;
  background: rgba(255, 255, 255, 0.025);
}

.insight-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.insight-title span {
  color: #c5a66f;
  font-size: 10px;
  letter-spacing: 0.16em;
}

.insight-title small {
  color: #75746e;
  font-size: 8px;
}

.insight-card p {
  min-height: 38px;
  margin: 11px 0 14px;
  color: #c8c3b7;
  font-size: 11px;
  line-height: 1.75;
}

.advantage-meter {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  color: #77766f;
  font-size: 8px;
}

.emotion-strip {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 16px;
  color: #8b887e;
  font-size: 10px;
}

.emotion-strip b {
  color: #a8392f;
  font-size: 11px;
  font-weight: 600;
}

.emotion-strip small {
  margin-left: auto;
  color: #aaa59a;
  font-size: 9px;
}

.advantage-meter div {
  height: 2px;
  overflow: hidden;
  background: #45453e;
}

.advantage-meter i {
  display: block;
  width: 50%;
  height: 100%;
  background: linear-gradient(90deg, #a63c33, #c39258);
  transition: width 400ms ease;
}

.chat-messages {
  min-height: 180px;
  max-height: 360px;
  flex: 1;
  overflow-y: auto;
  padding: 12px 5px 12px 0;
  scrollbar-color: #4a493f transparent;
  scrollbar-width: thin;
}

.message {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 10px 0 18px;
}

.message.user-message {
  flex-direction: row-reverse;
}

.mini-avatar {
  display: grid;
  width: 25px;
  height: 25px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid #696250;
  border-radius: 50%;
  color: #cfb985;
  font-family: var(--display);
  font-size: 11px;
}

.user-message .mini-avatar {
  border-color: rgba(167, 65, 56, 0.75);
  color: #d28a80;
}

.message > div:last-child {
  max-width: 84%;
}

.message small {
  display: block;
  margin-bottom: 5px;
  color: #6f6e68;
  font-size: 8px;
}

.user-message small {
  text-align: right;
}

.message p {
  margin: 0;
  padding: 9px 11px;
  border-radius: 1px 8px 8px 8px;
  color: #c9c5bb;
  background: rgba(255, 255, 255, 0.045);
  font-size: 11px;
  line-height: 1.7;
  white-space: pre-wrap;
}

.user-message p {
  border-radius: 8px 1px 8px 8px;
  color: #d5cdc0;
  background: rgba(152, 57, 48, 0.18);
}

.ai-sticker {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 8px;
  padding: 7px 10px;
  border: 1px solid rgba(224, 196, 142, 0.28);
  border-radius: 9px;
  color: #ead3a5;
  background: linear-gradient(135deg, rgba(174, 136, 81, 0.22), rgba(255, 255, 255, 0.05));
  box-shadow: 0 5px 14px rgba(0, 0, 0, 0.12);
}

.ai-sticker b {
  display: grid;
  width: 24px;
  height: 24px;
  place-items: center;
  border: 1px solid rgba(224, 196, 142, 0.55);
  border-radius: 50%;
  color: #f2d391;
  font-family: var(--display);
  font-size: 12px;
}

.ai-sticker span {
  font-size: 10px;
  letter-spacing: 0.04em;
}

.chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 8px;
  padding: 8px 8px 8px 14px;
  border: 1px solid rgba(235, 223, 198, 0.13);
  background: rgba(255, 255, 255, 0.025);
}

.chat-form textarea {
  width: 100%;
  max-height: 90px;
  resize: none;
  border: 0;
  outline: 0;
  color: #d6d0c3;
  background: transparent;
  font-size: 10px;
  line-height: 1.6;
}

.chat-form textarea::placeholder {
  color: #6f6e67;
}

.chat-form button {
  display: grid;
  width: 32px;
  height: 32px;
  place-items: center;
  border: 0;
  border-radius: 2px;
  color: #25251f;
  background: #b6955b;
  cursor: pointer;
}

.chat-form button:disabled {
  opacity: 0.45;
  cursor: wait;
}

.move-history {
  margin-top: 13px;
  border-top: 1px solid rgba(235, 223, 198, 0.08);
}

.move-history summary {
  display: flex;
  justify-content: space-between;
  padding: 13px 0 5px;
  color: #8c897f;
  font-size: 9px;
  cursor: pointer;
  list-style: none;
}

.move-history summary::-webkit-details-marker {
  display: none;
}

.move-history ol {
  display: grid;
  max-height: 96px;
  grid-template-columns: 1fr 1fr;
  gap: 4px 18px;
  overflow-y: auto;
  margin: 8px 0 0;
  padding: 0 0 0 24px;
  color: #8b887e;
  font-size: 9px;
  line-height: 1.65;
}

dialog {
  width: min(480px, calc(100% - 32px));
  /* 人设输入框变高后弹窗可能超出屏幕，这里允许整体滚动 */
  max-height: calc(100% - 32px);
  overflow-y: auto;
  padding: 0;
  border: 1px solid rgba(201, 171, 116, 0.3);
  border-radius: 4px;
  color: #282720;
  background: #eee8d9;
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.48);
}

dialog::backdrop {
  background: rgba(13, 14, 12, 0.76);
  backdrop-filter: blur(4px);
}

.dialog-card {
  padding: 28px;
}

.dialog-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 24px;
}

.dialog-heading h2 {
  margin: 0;
  font-family: var(--display);
  font-size: 28px;
  font-weight: 500;
}

.close-button {
  border: 0;
  color: #777064;
  background: none;
  font-size: 24px;
  cursor: pointer;
}

.dialog-card label {
  display: block;
  margin: 15px 0;
  color: #4c483f;
  font-size: 11px;
  font-weight: 700;
}

.dialog-card input,
.dialog-card textarea {
  width: 100%;
  margin-top: 8px;
  padding: 11px 12px;
  resize: vertical;
  border: 1px solid rgba(76, 62, 41, 0.2);
  border-radius: 2px;
  outline: none;
  color: #333029;
  background: rgba(255, 255, 255, 0.4);
  font-size: 12px;
  line-height: 1.7;
}

.dialog-card input:focus,
.dialog-card textarea:focus {
  border-color: #a97745;
}

.field-help {
  color: #81796b;
  font-size: 9px;
  line-height: 1.65;
}

/* 设置面板里的分组卡片：用来承载"和谁下棋""访问口令"这类选项 */
.choice-block {
  display: grid;
  gap: 4px;
  margin: 0 0 18px;
  padding: 15px;
  border: 1px solid rgba(76, 62, 41, 0.14);
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.34);
}

/* .choice-block 自身是 grid，会盖掉浏览器默认的 [hidden]{display:none}，
   所以必须显式声明，否则未启用的口令输入框会一直显示。 */
.choice-block[hidden] {
  display: none;
}

.choice-title {
  color: #4c483f;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.dialog-card .radio-line {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 8px 0 0;
  font-size: 11px;
  font-weight: 400;
  cursor: pointer;
}

.dialog-card .radio-line input {
  width: auto;
  margin: 0;
  accent-color: #a8392f;
}

.choice-help {
  margin: 8px 0 0;
  color: #81796b;
  font-size: 9px;
  line-height: 1.65;
}

.choice-block .secondary-button {
  justify-self: start;
  min-width: 0;
  margin-top: 10px;
}

.dialog-card menu {
  display: flex;
  justify-content: flex-end;
  gap: 9px;
  margin: 24px 0 0;
  padding: 0;
}

.save-button,
.secondary-button {
  min-width: 90px;
  padding: 10px 17px;
  border: 1px solid #292a24;
  border-radius: 2px;
  cursor: pointer;
}

.save-button {
  color: #f0eadc;
  background: #292a24;
}

.secondary-button {
  border-color: rgba(45, 43, 36, 0.2);
  background: transparent;
}

.config-status-card {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 13px;
  background: rgba(255, 255, 255, 0.42);
}

.config-status-card > span {
  color: #9c7447;
  font-size: 23px;
}

.config-status-card strong {
  font-size: 12px;
}

.config-status-card p {
  margin: 3px 0 0;
  color: #7b7468;
  font-size: 9px;
}

.config-code {
  margin-top: 17px;
  font-size: 10px;
}

.config-code pre {
  overflow-x: auto;
  padding: 13px;
  color: #d8ceb8;
  background: #282922;
  font: 10px/1.7 Consolas, monospace;
}

.learning-export {
  display: grid;
  gap: 9px;
}

.learning-export p {
  margin: 0;
  color: #8b887e;
  font-size: 10px;
  line-height: 1.6;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  z-index: 20;
  padding: 10px 17px;
  border: 1px solid rgba(226, 203, 158, 0.25);
  border-radius: 2px;
  color: #eee7d8;
  background: rgba(28, 29, 25, 0.96);
  box-shadow: 0 12px 35px rgba(0, 0, 0, 0.3);
  font-size: 10px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: 200ms ease;
}

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

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 1000px) {
  .game-shell {
    grid-template-columns: 1fr;
    width: min(760px, calc(100% - 30px));
  }

  .companion-panel {
    min-height: 600px;
  }

  .chat-messages {
    max-height: 320px;
  }
}

@media (max-width: 600px) {
  .topbar {
    height: 64px;
    padding: 0 16px;
  }

  .api-status > span {
    display: none;
  }

  .game-shell {
    display: block;
    width: 100%;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .board-panel {
    overflow: hidden;
    padding: 24px 12px 28px;
  }

  .match-heading {
    align-items: center;
    margin-bottom: 20px;
  }

  .match-heading h1 {
    font-size: 27px;
  }

  .turn-card {
    min-width: auto;
    padding: 8px 9px;
  }

  .turn-card small {
    display: none;
  }

  .players-row {
    padding: 0 5px;
  }

  .board-wrap,
  .game-toolbar {
    width: calc(100vw - 24px);
    max-width: calc(100vw - 24px);
  }

  .side-piece,
  .versus {
    display: none;
  }

  .game-toolbar {
    gap: 5px;
  }

  .toolbar-button {
    display: flex;
    min-width: 0;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 8px 3px;
    overflow: hidden;
    font-size: 9px;
    white-space: nowrap;
  }

  .toolbar-button span {
    margin: 0;
  }

  .companion-panel {
    min-height: 620px;
    padding: 25px 18px;
  }
}
