:root {
  --bg: #1f2125;
  --bg-2: #272a30;
  --card: #2d3037;
  --card-2: #353943;
  --border: #3d414b;
  --text: #e9eaec;
  --muted: #9ea3ad;
  --accent: #7fb34a;
  --accent-2: #94c95d;
  --danger: #d9534f;

  --light: #ebecd0;
  --dark: #739552;
  --coord-light: #739552;
  --coord-dark: #ebecd0;
  --last-light: #f5f682;
  --last-dark: #b9ca43;
  --select: rgba(20, 85, 30, 0.5);
  --hint: rgba(60, 140, 255, 0.55);
  --dot: rgba(0, 0, 0, 0.18);

  --radius: 10px;
  --board-size: min(calc(100vh - 222px), calc(100vw - 440px), 820px);
}

[data-theme="wood"] {
  --light: #f0d9b5; --dark: #b58863;
  --coord-light: #b58863; --coord-dark: #f0d9b5;
  --last-light: #f7ec74; --last-dark: #dac34b;
}
[data-theme="blue"] {
  --light: #dee3e6; --dark: #8ca2ad;
  --coord-light: #788a94; --coord-dark: #dee3e6;
  --last-light: #c3d888; --last-dark: #92b166;
}
[data-theme="gray"] {
  --light: #c8ccd1; --dark: #6f7a86;
  --coord-light: #6f7a86; --coord-dark: #c8ccd1;
  --last-light: #e3d98a; --last-dark: #b3a95a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  user-select: none; -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
  cursor: default;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  min-height: 100%;
}

button { font: inherit; color: inherit; }

.hidden { display: none !important; }

/* ------------------------------------------------------------------ layout */

.app {
  min-height: 100vh; display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.top-actions { display: flex; gap: 8px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 24px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}
.brand { font-size: 22px; font-weight: 700; letter-spacing: .3px; display: flex; align-items: center; gap: 8px; }
.brand-icon { font-family: "Segoe UI Symbol", "Noto Sans Symbols2", "DejaVu Sans", serif; color: var(--accent-2); font-size: 28px; line-height: 1; }
.brand-sub { color: var(--muted); font-weight: 500; font-size: 16px; }

.layout {
  flex: 1;
  display: flex; gap: 24px;
  justify-content: center; align-items: flex-start;
  padding: 16px 24px 24px;
}

.board-area { display: flex; flex-direction: column; gap: 8px; }

.board-wrap { display: flex; gap: 10px; align-items: stretch; }

.board-frame {
  position: relative;
  width: var(--board-size); height: var(--board-size);
  min-width: 280px; min-height: 280px;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0,0,0,.45);
}

/* -------------------------------------------------------------- player strip */

.player-strip {
  display: flex; align-items: center; gap: 10px;
  height: 46px;
  padding-left: 30px;
}
.avatar {
  width: 38px; height: 38px; border-radius: 8px;
  display: grid; place-items: center;
  font-size: 22px;
  background: var(--card-2);
  border: 1px solid var(--border);
}
.player-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.player-name { font-weight: 600; }
.player-name small { color: var(--muted); font-weight: 400; margin-left: 6px; }
.captured { display: flex; align-items: center; min-height: 18px; color: var(--muted); font-size: 13px; }
.captured .cap-piece { width: 20px; height: 20px; margin-right: -5px; display: inline-block; }
.captured .cap-piece svg { width: 100%; height: 100%; display: block; }
.captured .adv { margin-left: 10px; font-weight: 600; color: var(--muted); }

.thinking { margin-left: auto; display: none; gap: 4px; padding-right: 4px; }
.thinking.on { display: flex; }
.thinking span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent-2);
  animation: bounce 1s infinite ease-in-out;
}
.thinking span:nth-child(2) { animation-delay: .15s; }
.thinking span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 80%, 100% { transform: scale(.5); opacity: .4; } 40% { transform: scale(1); opacity: 1; } }

/* --------------------------------------------------------------- eval bar */

.eval-bar {
  position: relative; width: 20px;
  border-radius: 4px; overflow: hidden;
  background: #403d39;
}
.eval-fill {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 50%;
  background: #f4f4f4;
  transition: height .6s ease;
}
.eval-bar.flipped .eval-fill { bottom: auto; top: 0; }
.eval-text {
  position: absolute; left: 0; right: 0; bottom: 4px;
  text-align: center; font-size: 9px; font-weight: 700; color: #333;
}
.eval-bar.black-ahead .eval-text { bottom: auto; top: 4px; color: #eee; }
.eval-bar.flipped .eval-text { bottom: auto; top: 4px; }
.eval-bar.flipped.black-ahead .eval-text { top: auto; bottom: 4px; }

/* ------------------------------------------------------------------ board */

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%; height: 100%;
  user-select: none; -webkit-user-select: none;
  touch-action: none;
}

.sq { position: relative; }
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }
.sq.last::before, .sq.sel::before, .sq.hint::before, .sq.check::before {
  content: ""; position: absolute; inset: 0; pointer-events: none;
}
.sq.light.last::before { background: var(--last-light); opacity: .75; }
.sq.dark.last::before { background: var(--last-dark); opacity: .75; }
.sq.sel::before { background: var(--select); }
.sq.hint::before { background: var(--hint); }
.sq.check::before {
  background: radial-gradient(ellipse at center, rgba(255,0,0,1) 0%, rgba(231,0,0,1) 25%, rgba(169,0,0,0) 89%, rgba(158,0,0,0) 100%);
}
.sq.drop-target { box-shadow: inset 0 0 0 4px rgba(255,255,255,.65); }

.sq .dot {
  position: absolute; left: 50%; top: 50%;
  width: 30%; height: 30%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: var(--dot);
  pointer-events: none;
  z-index: 3;
}
.sq .ring {
  position: absolute; inset: 0;
  border-radius: 50%;
  box-shadow: inset 0 0 0 calc(var(--board-size) / 8 * 0.08) var(--dot);
  pointer-events: none;
  z-index: 3;
}

.coord {
  position: absolute; font-size: calc(var(--board-size) / 8 * 0.17); font-weight: 700;
  pointer-events: none; z-index: 1; line-height: 1;
}
.coord.rank { top: 4%; left: 5%; }
.coord.file { bottom: 4%; right: 6%; }
.sq.light .coord { color: var(--coord-light); }
.sq.dark .coord { color: var(--coord-dark); }

.piece {
  position: absolute; inset: 0;
  z-index: 2;
  cursor: default;
  will-change: transform;
}
.piece.movable { cursor: grab; }
.piece.dragging { z-index: 50; cursor: grabbing; transition: none !important; pointer-events: none; }
.piece.dragging svg { transform: scale(1.12); }
.piece.animating { z-index: 40; transition: transform .2s cubic-bezier(.3, .7, .4, 1); }
.piece svg { width: 100%; height: 100%; display: block; overflow: visible; filter: drop-shadow(0 2px 0 rgba(0,0,0,.12)); }


/* ------------------------------------------------------- promotion picker */

.promo-picker {
  position: absolute; inset: 0; z-index: 100;
  background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center;
}
.promo-picker .promo-box {
  display: flex; gap: 6px; padding: 10px;
  background: var(--card); border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,.5);
}
.promo-picker button {
  width: calc(var(--board-size) / 8); height: calc(var(--board-size) / 8);
  min-width: 48px; min-height: 48px;
  border: none; border-radius: 8px; background: var(--light);
  cursor: pointer; position: relative; padding: 0;
}
.promo-picker button:hover { background: var(--last-light); }
.promo-picker button .piece { position: absolute; inset: 0; }

/* ------------------------------------------------------------------ panel */

.panel {
  width: 330px; flex-shrink: 0;
  display: flex; flex-direction: column; gap: 12px;
  margin-top: 54px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.card-title { display: flex; justify-content: space-between; align-items: center; font-weight: 600; margin-bottom: 10px; gap: 8px; }

.status-card { padding: 12px 14px; }
.status { font-size: 17px; font-weight: 600; }
.substatus { color: var(--muted); font-size: 13px; margin-top: 3px; min-height: 17px; }

.level-badge {
  background: var(--accent); color: #fff;
  border-radius: 999px; min-width: 28px; height: 28px; padding: 0 8px;
  display: grid; place-items: center; font-weight: 700;
}
.level-name { margin-top: 10px; font-weight: 600; }
.level-desc { color: var(--muted); font-size: 13px; margin-top: 3px; line-height: 1.4; }
.level-scale { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 4px; }

input[type="range"] {
  width: 100%; accent-color: var(--accent);
  cursor: pointer;
}

.moves-card { display: flex; flex-direction: column; }
.opening { font-weight: 400; color: var(--muted); font-size: 12px; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.moves {
  height: clamp(110px, calc(100vh - 745px), 320px); overflow-y: auto;
  display: grid; grid-template-columns: 40px 1fr 1fr;
  align-content: start;
  background: var(--bg-2);
  border-radius: 6px;
  font-size: 14px;
}
.moves .num { color: var(--muted); padding: 4px 8px; background: rgba(255,255,255,.02); }
.moves .mv { padding: 4px 8px; cursor: pointer; border-radius: 4px; font-weight: 500; }
.moves .mv:hover { background: var(--card-2); }
.moves .mv.current { background: var(--accent); color: #fff; }
.moves .empty { grid-column: 1 / -1; color: var(--muted); padding: 12px; text-align: center; font-size: 13px; }
.moves .result { grid-column: 1 / -1; text-align: center; padding: 8px; font-weight: 700; color: var(--accent-2); }

.nav { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 8px; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.btn {
  background: var(--card-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 12px;
  cursor: pointer;
  font-weight: 600;
  transition: background .15s, transform .05s, border-color .15s;
}
.btn:hover:not(:disabled) { background: #40444f; border-color: #50555f; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-2); border-color: var(--accent-2); }
.btn.danger:hover:not(:disabled) { background: var(--danger); border-color: var(--danger); }
.btn.icon { padding: 6px; font-size: 13px; }
.btn.small { padding: 6px 10px; font-size: 13px; }

.settings summary { cursor: pointer; font-weight: 600; user-select: none; }
.settings[open] summary { margin-bottom: 8px; }
.settings .row { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; font-size: 14px; gap: 8px; }
.settings select {
  background: var(--card-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 8px; font: inherit;
}
.settings input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--accent); cursor: pointer; }
.export { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }

/* ----------------------------------------------------------------- modals */

.modal {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal-box {
  width: 100%; max-width: 420px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
  animation: pop .18s ease;
}
@keyframes pop { from { transform: scale(.95); } to { transform: scale(1); } }
.modal-box.center { text-align: center; }
.modal-box h2 { margin: 0 0 14px; font-size: 22px; }
.modal-box p { color: var(--muted); margin: 0 0 18px; }
.over-icon { font-size: 46px; margin-bottom: 6px; }
.field-label { color: var(--muted); font-size: 13px; margin: 14px 0 8px; }
.field-label strong { color: var(--text); }
.color-choice { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.choice {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  background: var(--card-2); border: 2px solid var(--border); border-radius: 10px;
  padding: 10px 6px; cursor: pointer; font-weight: 600; font-size: 14px;
}
.choice:hover { border-color: #5a606b; }
.choice.active { border-color: var(--accent); background: #3a4434; }
.choice-piece { width: 44px; height: 44px; display: grid; place-items: center; font-size: 34px; line-height: 1; }
.choice-piece svg { width: 100%; height: 100%; }
.random-piece { font-weight: 800; color: var(--accent-2); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 22px; }
.modal-box.center .modal-actions { justify-content: center; }

.toast {
  position: fixed; left: 50%; bottom: 24px;
  transform: translate(-50%, 20px);
  background: #111; color: #fff; border: 1px solid var(--border);
  padding: 10px 16px; border-radius: 8px;
  opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 2000;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ------------------------------------------------------------- responsive */

@media (max-width: 900px) {
  :root { --board-size: min(calc(100vw - 62px), calc(100vh - 200px), 640px); }
  .layout { flex-direction: column; align-items: center; padding: 12px 16px 24px; }
  .panel { width: 100%; max-width: calc(var(--board-size) + 30px); margin-top: 0; }
  .moves { height: 160px; }
  .status-card { order: -2; }
  .actions { order: -1; grid-template-columns: repeat(4, 1fr); }
  .actions .btn { padding: 9px 4px; font-size: 14px; }
}
@media (max-width: 900px) and (max-height: 700px) {
  :root { --board-size: min(calc(100vw - 62px), 640px); }
}
@media (max-width: 480px) {
  .topbar { padding: 8px 16px; }
  .brand { font-size: 19px; }
  .player-strip { padding-left: 0; }
  .eval-bar { width: 14px; }
  :root { --board-size: calc(100vw - 56px); }
}
