:root {
  --cream: #fbf5e8;
  --cream-lift: #fffaf0;
  --cream-deep: #f5ecd6;
  --ink: #3d2f1e;
  --ink-soft: #5c4a33;
  --dim: #967d54;
  --peach: #e0a182;
  --peach-soft: #fde6d2;
  --peach-deep: #c88564;
  --sage: #91ad87;
  --teal: #4b9d8c;
  --gold: #c59156;
  --line: #e8d9bf;
  --shadow: rgba(140, 100, 60, 0.12);
  --danger: #b96f62;
  --board: #dfb16d;
  --board-line: #745a37;
}

* { box-sizing: border-box; }

html { min-width: 320px; }

body {
  margin: 0;
  min-height: 100vh;
  padding: 14px 16px;
  color: var(--ink);
  background: var(--cream);
  background-image:
    radial-gradient(ellipse 760px 360px at top right, var(--peach-soft) 0%, transparent 70%),
    radial-gradient(ellipse 620px 300px at bottom left, rgba(145, 173, 135, 0.18) 0%, transparent 70%);
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Serif SC", serif;
}

button, textarea { font: inherit; }
button { touch-action: manipulation; }

.game-shell { position: relative; width: min(1460px, 100%); height: calc(100vh - 28px); margin: 0 auto; display: flex; flex-direction: column; }
.back-link {
  position: absolute;
  right: 0;
  top: 8px;
  display: inline-flex;
  color: var(--dim);
  font-size: 13px;
  text-decoration: none;
  z-index: 50;
  pointer-events: auto;
}
.back-link:hover { color: var(--peach-deep); }
.mobile-chat-toggle { display: none; }

.consent-banner {
  position: sticky;
  top: 10px;
  z-index: 30;
  width: min(920px, calc(100% - 24px));
  margin: 0 auto 18px;
  padding: 14px 16px;
  border: 1px solid var(--peach);
  border-radius: 10px;
  background: rgba(255, 250, 240, 0.97);
  box-shadow: 0 8px 26px var(--shadow);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.consent-banner[hidden] { display: none; }
.consent-banner p { margin: 0; color: var(--ink-soft); font-size: 13px; line-height: 1.7; }
.consent-actions { display: flex; flex: 0 0 auto; gap: 8px; }
.consent-btn {
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 12px;
  color: var(--ink-soft);
  background: var(--cream-lift);
  cursor: pointer;
}
.consent-btn.primary { color: #fff; border-color: var(--peach); background: var(--peach); }

.agent-corner {
  position: relative;
  min-height: 58px;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}
.agent-avatar {
  position: relative;
  width: 52px;
  height: 52px;
  overflow: hidden;
  border: 2px solid rgba(224, 161, 130, 0.62);
  border-radius: 50%;
  background: var(--cream-lift);
  box-shadow: 0 4px 16px var(--shadow);
}
.agent-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-light {
  position: absolute;
  right: 2px;
  bottom: 3px;
  width: 11px;
  height: 11px;
  border: 2px solid var(--cream-lift);
  border-radius: 50%;
  background: #4aa88c;
}
.agent-identity { display: grid; gap: 4px; }
.agent-identity strong { font-size: 15px; letter-spacing: 1px; }
.agent-identity span { color: var(--dim); font-size: 11px; }
.agent-bubble {
  position: static;
  z-index: 12;
  width: min(420px, calc(100vw - 430px));
  margin-left: 18px;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 4px 10px 10px 10px;
  color: var(--ink-soft);
  background: var(--cream-lift);
  box-shadow: 0 8px 22px var(--shadow);
  font-size: 13px;
  line-height: 1.6;
}
.bubble-pop { animation: bubble-pop .24s ease-out; }
@keyframes bubble-pop { from { opacity: 0; transform: translateY(-5px); } }

#status { display: none !important; }
.settings-fold { flex: 0 0 auto; margin-bottom: 10px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.settings-fold summary {
  width: fit-content;
  padding: 8px 0;
  color: var(--dim);
  font-size: 12px;
  cursor: pointer;
  list-style-position: outside;
}
.settings-fold summary:hover { color: var(--peach-deep); }
.status {
  max-width: 260px;
  padding: 8px 14px;
  border-left: 3px solid var(--sage);
  color: var(--ink-soft);
  background: rgba(255, 250, 240, 0.72);
  font-size: 12px;
}
.status.thinking { border-color: var(--gold); }
.status.game-ended { border-color: var(--peach-deep); }

.settings-bar {
  display: flex;
  align-items: center;
  gap: 22px;
  min-height: 62px;
  padding: 12px 0;
  border-top: 1px solid var(--line);
  flex-wrap: wrap;
}
.setting-group { display: flex; align-items: center; gap: 9px; }
.setting-label { color: var(--dim); font-size: 11px; }
.diff-seg { display: flex; padding: 2px; border: 1px solid var(--line); border-radius: 8px; background: var(--cream-deep); }
.diff-btn, .go-easy-btn, .controls button, .chat-actions button, .memory-btn {
  min-height: 36px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 13px;
  color: var(--ink-soft);
  background: transparent;
  cursor: pointer;
}
.diff-btn:hover, .diff-btn.active { color: #fff; background: var(--peach); }
.first-switch { display: flex; align-items: center; gap: 9px; cursor: pointer; font-size: 12px; }
.first-switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch-track { width: 40px; height: 22px; padding: 3px; border-radius: 11px; background: #d8ccb8; transition: background .2s; }
.switch-thumb { display: block; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .2s; }
.first-switch input:checked + .switch-track { background: var(--sage); }
.first-switch input:checked + .switch-track .switch-thumb { transform: translateX(18px); }
.go-easy-btn { border-color: var(--gold); color: #8e6232; background: rgba(197,145,86,.08); }
.go-easy-btn.armed { color: #fff; background: var(--gold); }

.game-layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(330px, 1fr);
  align-items: stretch;
  gap: 22px;
  padding-top: 0;
  height: calc(100vh - 112px);
}
.board-panel { min-width: 0; min-height: 0; display: grid; grid-template-rows: minmax(0, 1fr) auto; justify-items: center; align-self: center; }
.board-wrap {
  width: calc(100vh - 180px);
  height: calc(100vh - 180px);
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1;
  padding: 16px;
  border: 1px solid #c79a5d;
  border-radius: 5px;
  background: var(--board);
  box-shadow: 0 5px 18px rgba(100,65,28,.16);
}
#board { display: block; width: 100%; height: 100%; touch-action: none; }
.controls { display: flex; gap: 10px; margin-top: 12px; }
.controls button { border-color: var(--line); background: var(--cream-lift); }
.controls button { white-space: nowrap; min-width: 92px; }
.controls button:hover:not(:disabled) { border-color: var(--peach); color: var(--peach-deep); }
.controls button:disabled { opacity: .45; cursor: default; }

.decision-panel, .chat-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255,250,240,.82);
  box-shadow: 0 2px 12px var(--shadow);
}
.decision-panel[hidden] { display: none !important; }
.decision-panel { padding: 16px; }
.legend { display: flex; gap: 14px; padding-bottom: 14px; border-bottom: 1px solid var(--line); color: var(--dim); font-size: 11px; }
.legend-dot { display: inline-block; width: 7px; height: 7px; margin-right: 5px; border-radius: 50%; }
.legend-dot.algorithm { background: var(--sage); }
.legend-dot.agent { background: var(--gold); }
.metric-row { display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid var(--line); }
.metric-row div { display: grid; gap: 8px; padding: 16px 8px; }
.metric-row div + div { border-left: 1px solid var(--line); }
.metric-row span { color: var(--dim); font-size: 10px; }
.metric-row strong { font: 500 24px Georgia, serif; }
.decision-log { padding-top: 16px; }
.decision-log h2, .chat-head h2 { margin: 0; font-size: 13px; letter-spacing: 1px; }
.decision-log ol { margin: 12px 0 0; padding: 0; list-style: none; }
.decision-log li { padding: 9px 0; border-bottom: 1px solid var(--line); }
.decision-log li strong, .decision-log li span { display: block; }
.decision-log li strong { font-size: 11px; }
.decision-log li span, .empty-log { color: var(--dim); font-size: 10px; line-height: 1.6; }

.chat-panel { display: flex; width: min(100%, 400px); height: min(100%, 480px); min-height: 0; max-height: none; justify-self: center; align-self: center; flex-direction: column; overflow: hidden; }
.chat-head { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.chat-title-row { display: flex; align-items: center; gap: 10px; }
.chat-title-row img { width: 30px; height: 30px; border-radius: 50%; object-fit: cover; }
.chat-title-row h2 { font-size: 13px; }
.chat-hint { margin: 10px 0 0; color: var(--dim); font-size: 10px; line-height: 1.5; }
.memory-actions { display: flex; gap: 6px; margin-top: 9px; }
.memory-btn { min-height: 30px; padding: 5px 9px; border-color: var(--line); font-size: 10px; background: var(--cream); }
.memory-btn.danger { color: var(--danger); }
.chat-log { flex: 1; min-height: 0; max-height: 230px; overflow-y: auto; padding: 10px 12px; scrollbar-color: var(--line) transparent; }
.chat-row { display: flex; margin-bottom: 12px; flex-direction: column; align-items: flex-start; }
.chat-row .chat-bubble { position: relative; max-width: 88%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; color: var(--ink-soft); background: var(--cream-lift); font-size: 12px; line-height: 1.6; }
.chat-row.user { align-items: flex-end; }
.chat-row.user .chat-bubble { border-color: #b9d0bb; background: #e9f1e6; }
.chat-meta { margin-top: 3px; color: var(--dim); font-size: 9px; }
.chat-date-divider { margin: 2px auto 16px; color: var(--dim); font-size: 10px; text-align: center; }
.chat-hover-time { position: absolute; left: calc(100% + 8px); top: 50%; opacity: 0; transform: translateY(-50%); color: var(--dim); font-size: 9px; white-space: nowrap; pointer-events: none; transition: opacity .15s; }
.chat-row.user .chat-hover-time { right: calc(100% + 8px); left: auto; }
.chat-bubble:hover .chat-hover-time { opacity: 1; }
.chat-status, .chat-fail { min-height: 16px; margin: 0; padding: 0 14px; color: var(--dim); font-size: 10px; }
.chat-fail { color: var(--danger); }
.chat-form { padding: 8px 10px 9px; border-top: 1px solid var(--line); }
#chat-input {
  width: 100%;
  min-height: 44px;
  height: 44px;
  resize: none;
  outline: none;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--ink);
  background: var(--cream);
  font-size: 12px;
  line-height: 1.5;
}
#chat-input:focus { border-color: var(--peach); background: var(--cream-lift); }
.chat-actions { display: flex; justify-content: flex-end; gap: 6px; margin-top: 6px; }
.chat-actions button { position: relative; min-width: 58px; min-height: 36px; padding: 5px 10px; border-color: var(--line); background: var(--cream-lift); font-size: 12px; }
.chat-actions button::before { content: ''; position: absolute; inset: -4px 0; }
#chat-send { color: #fff; border-color: var(--sage); background: var(--sage); }

.result-dialog {
  width: min(390px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 26px;
  color: var(--ink);
  background: var(--cream-lift);
  box-shadow: 0 20px 60px rgba(80, 50, 24, .24);
  text-align: center;
}
.result-dialog::backdrop { background: rgba(61, 47, 30, .42); backdrop-filter: blur(2px); }
.result-emblem img { width: 58px; height: 58px; border: 2px solid var(--peach); border-radius: 50%; object-fit: cover; }
.result-kicker { margin: 12px 0 5px; color: var(--peach-deep); font-size: 10px; letter-spacing: 3px; }
.result-dialog h2 { margin: 0; font-size: 28px; font-weight: 500; letter-spacing: 3px; }
.result-copy { margin: 12px 0 20px; color: var(--ink-soft); font-size: 13px; line-height: 1.7; }
.result-actions { display: flex; justify-content: center; gap: 10px; }
.result-actions button { min-width: 110px; min-height: 40px; border: 1px solid var(--line); border-radius: 7px; color: var(--ink-soft); background: var(--cream); cursor: pointer; }
.result-actions .result-primary { border-color: var(--sage); color: #fff; background: var(--sage); }

@media (max-width: 1080px) {
  .game-layout { grid-template-columns: minmax(0, 2fr) minmax(310px, 1fr); }
  .chat-panel { grid-column: 2; grid-row: 1; }
}

@media (max-width: 760px) {
  body { padding: 16px 12px 36px; }
  .game-shell { height: auto; min-height: calc(100vh - 32px); display: block; }
  .back-link { position: static; margin-bottom: 12px; }
  .mobile-chat-toggle {
    position: fixed;
    top: 58px;
    right: 12px;
    z-index: 42;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 46px;
    min-height: 32px;
    border: 1px solid var(--peach);
    border-radius: 16px;
    color: var(--ink-soft);
    background: rgba(255, 250, 240, .88);
    box-shadow: 0 4px 16px var(--shadow);
    cursor: pointer;
    font-size: 12px;
    line-height: 1;
  }
  .mobile-chat-toggle::before { content: ''; position: absolute; inset: -6px 0; }
  .agent-corner { min-height: 62px; }
  .agent-avatar { width: 52px; height: 52px; }
  .agent-bubble { position: absolute; left: 58px; top: 58px; width: calc(100vw - 94px); margin-left: 0; }
  .settings-bar { gap: 12px 16px; }
  .game-layout { width: 100%; height: auto; grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .board-panel, .chat-panel { grid-column: 1; width: 100%; }
  .board-panel { grid-row: 1; }
  .chat-panel {
    position: fixed;
    top: 50%;
    right: 12px;
    bottom: auto;
    left: 12px;
    transform: translateY(-50%);
    z-index: 40;
    display: none;
    width: calc(100vw - 24px);
    height: clamp(320px, 58svh, 560px);
    min-height: 0;
    max-height: none;
    border-color: rgba(224, 161, 130, .72);
    background: rgba(255, 250, 240, .60);
    box-shadow: 0 18px 50px rgba(80, 50, 24, .22);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }
  .chat-panel.mobile-chat-open { display: flex; }
  .chat-log { max-height: none; }
  .chat-head { padding: 8px 10px; }
  .chat-title-row img { width: 28px; height: 28px; }
  .chat-form { padding: 7px 8px 8px; }
  .chat-actions button { min-width: 54px; min-height: 36px; }
  .chat-head, .chat-form { background: rgba(255, 250, 240, .62); }
  .chat-log { background: rgba(251, 245, 232, .24); }
  .board-panel { min-width: 0; }
  .board-wrap { width: min(100%, calc(100vw - 24px)); height: auto; padding: 9px; }
  .controls button { flex: 1; min-width: 0; white-space: nowrap; }
  .controls #restart { flex: 0 0 104px; min-width: 104px; }
  .consent-banner { align-items: stretch; flex-direction: column; }
  .consent-actions button { flex: 1; }
}

@media (max-width: 430px) {
  .header-copy { font-size: 11px; }
  .settings-bar { display: grid; grid-template-columns: 1fr; }
  .setting-group { justify-content: space-between; }
  .diff-seg { flex: 1; }
  .diff-btn { flex: 1; padding-inline: 8px; }
}
