:root {
  color-scheme: light;
  --paper: #f7f2e7;
  --ink: #1f2933;
  --muted: #697386;
  --line: #8b6f47;
  --wood: #d7b57c;
  --wood-dark: #b8894f;
  --blue: #2563eb;
  --green: #059669;
  --red: #dc2626;
  --panel: #fffaf0;
  --border: #d6c5a8;
  --shadow: 0 14px 38px rgba(54, 41, 20, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: linear-gradient(180deg, #fbf7ef, #ece0c9);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  transition: background 160ms ease;
}

body.exploring-position {
  background: linear-gradient(180deg, #eef4f7, #d8d0bd);
}

body.exploring-position .play-area,
body.exploring-position .side-card {
  background: rgba(246, 251, 252, 0.94);
}

button,
select,
input {
  font: inherit;
}

button,
select,
input {
  border: 1px solid var(--border);
  background: #fffdf8;
  color: var(--ink);
  border-radius: 6px;
}

button {
  min-height: 36px;
  padding: 0 12px;
  cursor: pointer;
  font-weight: 650;
}

.button-link {
  min-height: 36px;
  padding: 0 12px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--border);
  background: #fffdf8;
  color: var(--ink);
  border-radius: 6px;
  font-weight: 650;
  text-decoration: none;
}

button:hover:not(:disabled) {
  border-color: #a98a58;
  background: #fff7e6;
}

.button-link:hover {
  border-color: #a98a58;
  background: #fff7e6;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

select,
input {
  min-height: 36px;
  padding: 0 10px;
}

.app-shell {
  width: min(1600px, 100%);
  margin: 0 auto;
  padding: 18px;
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 390px;
  gap: 18px;
  align-items: start;
}

.play-area,
.side-card {
  background: rgba(255, 250, 240, 0.92);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.play-area {
  min-width: 0;
  padding: 16px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 1.85rem;
}

h2 {
  font-size: 1rem;
}

.status {
  margin-top: 5px;
  color: var(--muted);
  min-height: 24px;
}

.status.error {
  color: var(--red);
  font-weight: 700;
}

.controls,
.names {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 10px;
  margin-top: 14px;
}

.controls label,
.names label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.name-action {
  display: grid;
  gap: 5px;
  margin-left: auto;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  min-width: 222px;
}

.name-action button {
  width: 100%;
}

.name-action button.active {
  background: #26384f;
  border-color: #26384f;
  color: #fff;
}

.controls select {
  max-width: 240px;
}

.online-panel {
  display: none;
  margin-top: 14px;
  padding: 12px;
  border: 1px solid var(--border);
  background: #fffdf8;
  border-radius: 6px;
}

.online-panel.show {
  display: grid;
  gap: 10px;
}

.online-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 10px;
}

.online-actions label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

#online-code-input {
  width: 120px;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  text-align: center;
  font-weight: 800;
}

.online-status {
  color: var(--muted);
  font-size: 0.9rem;
}

.turn-glance {
  margin: 64px 14px 14px;
  padding: 16px;
  border: 1px solid var(--border);
  background: #fffdf8;
  box-shadow: 0 10px 24px rgba(54, 41, 20, 0.08);
}

.turn-glance span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.turn-glance strong {
  display: block;
  margin-top: 6px;
  font-size: 1.12rem;
  line-height: 1.25;
}

.stepper {
  display: grid;
  grid-template-columns: 34px 42px 34px;
  align-items: center;
}

.stepper strong {
  min-height: 36px;
  display: grid;
  place-items: center;
  background: #fffdf8;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.stepper button {
  padding: 0;
  border-radius: 0;
}

.stepper button:first-child {
  border-radius: 6px 0 0 6px;
}

.stepper button:last-child {
  border-radius: 0 6px 6px 0;
}

#setup-button.active {
  background: #fef3c7;
  border-color: #d97706;
}

.board-wrap {
  margin: 16px auto 0;
  width: max-content;
  max-width: 100%;
  overflow: auto;
  padding: 28px 34px 34px;
}

.goal {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0 4px;
}

.goal-top {
  margin-bottom: 16px;
}

.goal-bottom {
  margin-top: 8px;
}

.board {
  --gap: 34px;
  --pad: 26px;
  --rows: 19;
  --cols: 19;
  position: relative;
  width: calc((var(--cols) - 1) * var(--gap) + var(--pad) * 2);
  height: calc((var(--rows) - 1) * var(--gap) + var(--pad) * 2);
  min-width: calc((var(--cols) - 1) * var(--gap) + var(--pad) * 2);
  min-height: calc((var(--rows) - 1) * var(--gap) + var(--pad) * 2);
  background: transparent;
  border: 0;
  border-radius: 6px;
  touch-action: none;
  user-select: none;
}

.board::before {
  content: "";
  position: absolute;
  inset: 8px;
  background: var(--wood);
  border: 2px solid #7b5b34;
  border-radius: 6px;
  pointer-events: none;
  z-index: 0;
}

.grid-line {
  position: absolute;
  background: rgba(80, 55, 24, 0.62);
  pointer-events: none;
  z-index: 1;
}

.grid-line.h {
  left: var(--pad);
  width: calc((var(--cols) - 1) * var(--gap));
  height: 1px;
}

.grid-line.v {
  top: var(--pad);
  width: 1px;
  height: calc((var(--rows) - 1) * var(--gap));
}

.goal-grid-line {
  height: 3px;
  background: rgba(58, 40, 18, 0.78);
}

.coord-label {
  position: absolute;
  z-index: 4;
  color: #111827;
  font-size: 0.72rem;
  font-weight: 900;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  user-select: none;
}

.col-label {
  top: -6px;
  transform: translateX(-50%);
}

.row-label {
  left: -14px;
  transform: translateY(-50%);
  min-width: 16px;
  text-align: right;
}

.point,
.off-point {
  position: absolute;
  z-index: 3;
  width: 30px;
  height: 30px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  display: grid;
  place-items: center;
}

.point {
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.point:hover:not(:disabled) {
  border: 0;
  background: transparent;
}

.point::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(94, 65, 31, 0.5);
  display: none;
}

.point.valid-jump::before,
.off-point.valid-jump::before {
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.82);
}

.point.valid-place::before {
  display: none;
}

.point.valid-jump::before,
.off-point.valid-jump::before {
  display: block;
  background: rgba(5, 150, 105, 0.62);
  box-shadow: 0 0 0 3px rgba(5, 150, 105, 0.22);
}

.point.setup-target {
  cursor: crosshair;
}

.piece {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  pointer-events: none;
}

.man {
  width: 22px;
  height: 22px;
  background: radial-gradient(circle at 33% 28%, #5b6472, #151b24 68%);
  box-shadow: 0 3px 8px rgba(22, 17, 10, 0.42);
}

.ball {
  width: 25px;
  height: 25px;
  background: #f8fafc;
  border: 0;
  box-shadow:
    inset 4px 4px 0 rgba(255, 255, 255, 0.8),
    inset -4px -4px 0 rgba(148, 163, 184, 0.38),
    0 3px 9px rgba(22, 17, 10, 0.36);
}

.ball.clickable {
  box-shadow:
    0 0 0 4px rgba(245, 158, 11, 0.35),
    inset 4px 4px 0 rgba(255, 255, 255, 0.8),
    inset -4px -4px 0 rgba(148, 163, 184, 0.38),
    0 3px 9px rgba(22, 17, 10, 0.36);
}

.ball.dragging {
  box-shadow:
    0 0 0 5px rgba(37, 99, 235, 0.32),
    inset 4px 4px 0 rgba(255, 255, 255, 0.8),
    inset -4px -4px 0 rgba(148, 163, 184, 0.38),
    0 3px 9px rgba(22, 17, 10, 0.36);
}

.visual-landing {
  z-index: 5;
  pointer-events: none;
}

.move-hover-target {
  position: absolute;
  z-index: 4;
  width: 24px;
  height: 24px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(250, 204, 21, 0.42);
  box-shadow: 0 0 0 5px rgba(250, 204, 21, 0.18);
  pointer-events: none;
}

.off-point {
  padding: 0;
  background: rgba(236, 253, 245, 0.72);
  border: 1px dashed rgba(5, 150, 105, 0.7);
  color: var(--green);
  font-size: 0.62rem;
  font-weight: 900;
  cursor: pointer;
}

.off-point:hover:not(:disabled) {
  background: rgba(209, 250, 229, 0.9);
  border-color: var(--green);
}

.off-point::before {
  content: "WIN";
}

.end-turn {
  display: none;
  margin: 12px auto 0;
  background: #ecfdf5;
  border-color: #34d399;
  color: #065f46;
}

.end-turn.show {
  display: block;
}

.left-column {
  position: sticky;
  top: 16px;
  display: grid;
  gap: 14px;
  min-width: 0;
}

.side-card {
  padding: 14px;
  min-width: 0;
}

.return-current-box {
  display: none;
}

.return-current-box.show {
  display: block;
}

.return-current-box button {
  width: 100%;
  text-align: center;
}

.ai-assist-card {
  display: block;
}

.ai-assist-status {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.35;
}

.ai-assist-status.error {
  color: var(--red);
  font-weight: 700;
}

.ai-download-box {
  display: none;
  gap: 8px;
  margin-top: 10px;
}

.ai-download-box.show {
  display: grid;
}

.ai-help-text {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
}

.ai-download-options {
  display: grid;
  gap: 6px;
}

.ai-model-row {
  display: grid;
  gap: 5px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.ai-model-row select,
.ai-download-box .button-link {
  width: 100%;
}

.ai-think-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.ai-think-row button {
  flex: 1;
}

.ai-think-row input {
  min-width: 84px;
  width: 96px;
}

.ai-analysis {
  display: grid;
  gap: 7px;
  margin-top: 12px;
}

.ai-analysis-muted {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.35;
}

.ai-analysis-line {
  min-height: 30px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  background: #fffdf8;
  color: var(--muted);
  border-radius: 6px;
  font-size: 0.86rem;
}

.ai-analysis-line span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.ai-analysis-line strong {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.ai-move-line {
  width: 100%;
  text-align: left;
  cursor: pointer;
}

.online-chat-card {
  display: none;
}

.online-chat-card.show {
  display: block;
}

.online-chat-messages {
  max-height: 240px;
  overflow-y: auto;
  display: grid;
  gap: 8px;
  padding: 8px 0;
}

.chat-message {
  display: grid;
  gap: 2px;
  padding: 8px;
  border: 1px solid var(--border);
  background: #fffdf8;
}

.chat-message strong {
  color: var(--ink);
  font-size: 0.82rem;
}

.chat-message span {
  color: var(--muted);
  font-size: 0.86rem;
  overflow-wrap: anywhere;
}

.online-chat-empty {
  color: var(--muted);
  font-size: 0.9rem;
}

.online-chat-compose {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  margin-top: 8px;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  margin-bottom: 10px;
}

.panel-heading h2 {
  color: var(--ink);
}

.panel-move-count {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  margin-left: auto;
  padding: 4px 8px;
  border: 1px solid var(--border);
  background: #fffdf8;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1;
}

.panel-move-count strong {
  color: var(--ink);
  font-size: 0.95rem;
}

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

.panel-toggle {
  min-height: 28px;
  padding: 0 8px;
  font-size: 0.78rem;
}

.panel-toggle.active {
  background: #26384f;
  border-color: #26384f;
  color: #fff;
}

.panel-body[hidden] {
  display: none;
}

.move-tree {
  display: grid;
  gap: 7px;
  max-height: 72vh;
  overflow: auto;
  padding-right: 2px;
}

.move-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
  min-width: 0;
  padding: 2px 0;
}

.move-node {
  min-height: 28px;
  padding: 3px 8px;
  background: #fffdf8;
  border: 1px solid var(--border);
  border-radius: 6px;
  max-width: 100%;
  overflow-wrap: anywhere;
  text-align: left;
  font-size: 0.88rem;
}

.move-node.current {
  border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.16);
}

.move-node.alternative {
  background: #efe8db;
  border-color: #d2c2a6;
  color: #756650;
}

.move-node.setup {
  border-style: dashed;
}

.alternatives {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 0.78rem;
  padding-left: 4px;
}

.root-alternatives {
  border-top: 1px solid var(--border);
  margin-top: 4px;
  padding-top: 8px;
}

.empty-tree {
  color: var(--muted);
}

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .side-card {
    position: static;
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 10px;
  }

  .play-area,
  .side-card {
    padding: 12px;
  }

  .topbar {
    align-items: start;
  }

  .controls label,
  .controls button,
  .names label,
  .name-action {
    flex: 1 1 140px;
  }

  .controls select,
  .names input,
  .name-action button {
    width: 100%;
  }

  .name-action {
    margin-left: 0;
  }

  .board {
    --gap: 28px;
    --pad: 22px;
  }
}
