:root {
  --bg: #14171f;
  --bg-grad: radial-gradient(1200px 800px at 50% -10%, #1d2331, #14171f 60%);
  --surface: #1f2430;
  --surface-2: #272d3b;
  --surface-3: #313849;
  --text: #e7eaf0;
  --muted: #97a0b2;
  --accent: #7c93ff;
  --accent-2: #5b73e8;
  --danger: #e5604d;
  --ok: #58c98a;
  --sq-light: #eeeed2;
  --sq-dark: #6f9456;
  --hl: rgba(255, 224, 71, 0.45);
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  font-synthesis: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  background-image: var(--bg-grad);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 12px 16px; max-width: 1100px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo {
  font-size: 30px; line-height: 1; color: var(--accent);
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, .4));
}
.brand-text h1 { margin: 0; font-size: 20px; letter-spacing: .3px; }
.tagline { margin: 0; font-size: 12px; color: var(--muted); }

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

.lang-switch { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.lang-switch.big { padding: 4px; }
.lang-btn {
  border: 0; background: transparent; color: var(--muted);
  padding: 6px 12px; border-radius: 999px; cursor: pointer; font-weight: 600;
  font-size: 13px; transition: .15s;
}
.lang-switch.big .lang-btn { padding: 8px 16px; }
.lang-btn.active { background: var(--accent); color: #fff; }

.icon-btn {
  border: 0; background: var(--surface-2); color: var(--text);
  width: 40px; height: 40px; border-radius: 12px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px; transition: .15s;
}
.icon-btn:hover { background: var(--surface-3); }
.install-btn {
  width: auto; padding: 0 14px; gap: 8px; font-weight: 600; font-size: 14px;
  background: var(--accent); color: #fff;
}
.install-btn:hover { background: var(--accent-2); }
.ico { font-size: 18px; line-height: 1; }

/* ---------- layout ---------- */
.layout {
  display: grid; grid-template-columns: 1fr; gap: 18px;
  max-width: 1100px; margin: 0 auto; padding: 8px 16px 40px;
  align-items: start;
}
@media (min-width: 920px) {
  .layout { grid-template-columns: minmax(320px, 1fr) 330px; }
}

/* ---------- board area ---------- */
.board-area { display: flex; flex-direction: column; gap: 10px; }

.player {
  display: flex; align-items: center; gap: 10px; min-height: 34px;
  padding: 0 4px;
}
.player-name {
  font-weight: 700; font-size: 15px; padding: 4px 12px; border-radius: 999px;
  display: inline-flex; align-items: center; gap: 6px;
}
.player-name.white { background: #f3f4f6; color: #1a1a1a; }
.player-name.black { background: #2b3140; color: #e7eaf0; border: 1px solid #3a4254; }
.captured { display: flex; flex-wrap: wrap; align-items: center; gap: 0; flex: 1; min-height: 22px; }
.cap { font-size: 20px; line-height: 1; margin-right: -3px; }
.cap.white { color: #fbfbfb; text-shadow: 0 0 1px #555; }
.cap.black { color: #11151c; text-shadow: 0 0 1px #aaa; }
.adv { font-size: 12px; color: var(--muted); margin-left: 6px; font-weight: 700; }
.clock {
  font-variant-numeric: tabular-nums; font-weight: 700; font-size: 18px;
  background: var(--surface-2); padding: 4px 12px; border-radius: 10px; min-width: 64px;
  text-align: center; color: var(--muted);
}
.clock:empty { display: none; }
.clock.active { color: var(--text); background: var(--accent-2); }

.board-wrap {
  position: relative; width: 100%;
  max-width: min(96vw, calc(100vh - 250px), 600px);
  margin: 0 auto; border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow);
}

.board {
  width: 100%; aspect-ratio: 1 / 1;
  display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr);
  container-type: inline-size;
}

.square { position: relative; display: flex; align-items: center; justify-content: center; }
.square.light { background: var(--sq-light); }
.square.dark { background: var(--sq-dark); }
.square > * { position: relative; z-index: 2; }

.piece {
  font-size: 8vmin;
  font-size: 10cqi;
  line-height: 1; user-select: none; cursor: pointer;
  filter: drop-shadow(0 2px 1px rgba(0, 0, 0, .35));
}
.piece.white { color: #ffffff; -webkit-text-stroke: 1.4px #44484f; }
.piece.black { color: #20242c; -webkit-text-stroke: 1.1px #c6ccd6; }

/* highlights */
.square.selected::after,
.square.last::after {
  content: ''; position: absolute; inset: 0; background: var(--hl); z-index: 1;
}
.square.selected::after { background: rgba(124, 147, 255, .5); }
.square.in-check::after {
  content: ''; position: absolute; inset: 0; z-index: 1;
  background: radial-gradient(circle, rgba(229, 96, 77, .9), rgba(229, 96, 77, 0) 72%);
}
body.show-hints .square.dest::before {
  content: ''; position: absolute; width: 30%; height: 30%; border-radius: 50%;
  background: rgba(20, 28, 16, .28); z-index: 1;
}
body.show-hints .square.dest-capture::before {
  content: ''; position: absolute; inset: 7%; border-radius: 50%;
  border: clamp(3px, 1.4cqi, 7px) solid rgba(20, 28, 16, .3); z-index: 1;
}

/* coordinates */
.coord { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
.coord::before { content: attr(data-rank); position: absolute; top: 2px; left: 3px; font-size: 2.6cqi; font-weight: 700; }
.coord::after { content: attr(data-file); position: absolute; bottom: 1px; right: 3px; font-size: 2.6cqi; font-weight: 700; }
body:not(.show-coords) .coord { display: none; }
.square.light .coord::before, .square.light .coord::after { color: rgba(90, 110, 70, .9); }
.square.dark .coord::before, .square.dark .coord::after { color: rgba(238, 238, 210, .9); }

/* result overlay */
.overlay {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  background: rgba(10, 12, 18, .62); backdrop-filter: blur(3px); z-index: 5; padding: 20px;
}
.overlay.open { display: flex; }
.overlay-card {
  background: var(--surface); border: 1px solid var(--surface-3); border-radius: var(--radius);
  padding: 24px 28px; text-align: center; box-shadow: var(--shadow); max-width: 88%;
  animation: pop .2s ease;
}
.overlay-result { font-size: 26px; font-weight: 800; margin-bottom: 6px; }
.overlay-result.white { color: #fff; }
.overlay-result.black { color: var(--muted); }
.overlay-result.draw { color: var(--accent); }
.overlay-reason { color: var(--muted); font-size: 14px; margin-bottom: 18px; }
@keyframes pop { from { transform: scale(.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* status banner */
.status {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--surface-2); border-radius: 12px;
  padding: 10px 16px; font-weight: 600; min-height: 44px;
}
.turn-dot { width: 14px; height: 14px; border-radius: 50%; display: inline-block; }
.turn-dot.white { background: #f3f4f6; box-shadow: 0 0 0 1px #888; }
.turn-dot.black { background: #2b3140; box-shadow: 0 0 0 1px #5a6276; }
.check-flag {
  color: var(--danger); font-weight: 800; background: rgba(229, 96, 77, .12);
  padding: 2px 10px; border-radius: 999px;
}

/* ---------- sidebar ---------- */
.sidebar { display: flex; flex-direction: column; gap: 14px; }
.toolbar { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
@media (min-width: 920px) { .toolbar { grid-template-columns: repeat(2, 1fr); } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--surface-3); background: var(--surface-2); color: var(--text);
  padding: 11px 14px; border-radius: 12px; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: .15s;
}
.btn:hover { background: var(--surface-3); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-2); }
.btn-danger { color: #ffd9d2; border-color: rgba(229, 96, 77, .4); }
.btn-danger:hover { background: rgba(229, 96, 77, .18); }
.btn .ico { font-size: 16px; }
#btnResign { grid-column: span 2; }

.panel {
  background: var(--surface); border: 1px solid var(--surface-2);
  border-radius: var(--radius); padding: 14px;
}
.panel h2 { margin: 0 0 10px; font-size: 14px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.move-list {
  max-height: 260px; overflow-y: auto; font-variant-numeric: tabular-nums;
  font-size: 14px; line-height: 1.6;
}
@media (min-width: 920px) { .move-list { max-height: 420px; } }
.move-row { display: grid; grid-template-columns: 32px 1fr 1fr; gap: 6px; padding: 2px 4px; border-radius: 6px; }
.move-row:nth-child(odd) { background: rgba(255, 255, 255, .025); }
.move-num { color: var(--muted); }
.move-w, .move-b { font-weight: 600; }
.move-empty { color: var(--muted); font-style: italic; padding: 8px 4px; }

/* ---------- settings drawer ---------- */
.drawer { position: fixed; inset: 0; z-index: 50; pointer-events: none; }
.drawer-backdrop {
  position: absolute; inset: 0; background: rgba(8, 10, 15, .5); opacity: 0;
  transition: opacity .25s;
}
.drawer-content {
  position: absolute; top: 0; right: 0; height: 100%; width: min(360px, 88vw);
  background: var(--surface); border-left: 1px solid var(--surface-3);
  transform: translateX(100%); transition: transform .28s cubic-bezier(.4, 0, .2, 1);
  padding: 18px; overflow-y: auto; box-shadow: var(--shadow);
}
.drawer.open { pointer-events: auto; }
.drawer.open .drawer-backdrop { opacity: 1; }
.drawer.open .drawer-content { transform: translateX(0); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.drawer-head h2 { margin: 0; font-size: 18px; }

.setting-group { margin-bottom: 18px; }
.setting-label { display: block; color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.setting-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 4px; border-bottom: 1px solid var(--surface-2); font-size: 15px; cursor: pointer;
}

/* clock duration selector */
.setting-sub { padding: 12px 4px 14px; border-bottom: 1px solid var(--surface-2); transition: opacity .2s; }
.setting-sub .setting-label { margin-bottom: 8px; }
.setting-sub.disabled { opacity: .4; }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.seg-btn {
  border: 0; background: transparent; color: var(--muted); padding: 9px 0;
  border-radius: 9px; cursor: pointer; font-weight: 700; font-size: 13px;
  font-variant-numeric: tabular-nums; transition: .15s;
}
.seg-btn:hover { color: var(--text); background: var(--surface-3); }
.seg-btn.active { background: var(--accent); color: #fff; }
.seg-btn.active:hover { background: var(--accent); }

/* toggle switch */
.switch {
  appearance: none; -webkit-appearance: none; width: 46px; height: 26px;
  background: var(--surface-3); border-radius: 999px; position: relative; cursor: pointer;
  transition: background .2s; flex: none;
}
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px;
  background: #fff; border-radius: 50%; transition: transform .2s;
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(20px); }

.help { margin-top: 18px; padding: 14px; background: var(--surface-2); border-radius: 12px; }
.help h3 { margin: 0 0 6px; font-size: 14px; }
.help p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; z-index: 60; display: none; align-items: center; justify-content: center; padding: 20px; }
.modal.open { display: flex; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(8, 10, 15, .6); }
.modal-card {
  position: relative; background: var(--surface); border: 1px solid var(--surface-3);
  border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow);
  max-width: 360px; width: 100%; text-align: center; animation: pop .18s ease;
}
.modal-card h3 { margin: 0 0 16px; font-size: 17px; }
.confirm-msg { margin: 0 0 20px; line-height: 1.5; }
.confirm-actions { display: flex; gap: 10px; }
.confirm-actions .btn { flex: 1; }

.promo-choices { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.promo-choice {
  border: 1px solid var(--surface-3); background: var(--surface-2); border-radius: 12px;
  padding: 8px 0; cursor: pointer; transition: .15s;
}
.promo-choice:hover { background: var(--surface-3); transform: translateY(-2px); }
.promo-choice .piece { font-size: 38px; filter: none; }
.promo-choice.white .piece { color: #fff; -webkit-text-stroke: 1.4px #44484f; }
.promo-choice.black .piece { color: #20242c; -webkit-text-stroke: 1.1px #c6ccd6; }

/* small screens */
@media (max-width: 380px) {
  .install-label { display: none; }
  .brand-text h1 { font-size: 18px; }
  .tagline { display: none; }
}
