:root {
  color-scheme: light;
  --bg: #f6f7f8;
  --ink: #1d2329;
  --muted: #5f6b76;
  --line: #d7dde3;
  --panel: #ffffff;
  --accent: #23615d;
  --accent-2: #9a5b25;
  --light-square: #e8d8bd;
  --dark-square: #7b8f72;
  --selected: #f1c84b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.auth-locked {
  overflow: hidden;
}

.auth-gate {
  position: fixed;
  z-index: 1000;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(246, 247, 248, 0.96);
}

.auth-gate[hidden] {
  display: none;
}

.auth-card {
  display: grid;
  width: min(100%, 360px);
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 20px;
  box-shadow: 0 18px 50px rgba(29, 35, 41, 0.14);
}

.auth-card h2 {
  margin: 0 0 4px;
  font-size: 20px;
}

.topbar {
  min-height: 78px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}

.brand {
  display: grid;
  gap: 4px;
  min-width: 190px;
}

.top-controls {
  display: flex;
  flex: 1;
  align-items: end;
  justify-content: flex-end;
  gap: 10px;
  min-width: 0;
}

.top-controls label {
  width: min(26vw, 230px);
  min-width: 150px;
  margin-bottom: 0;
}

.top-controls button {
  min-width: max-content;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 26px;
  font-weight: 760;
}

h2 {
  font-size: 15px;
  margin: 18px 0 10px;
}

.topbar p,
.hint,
.feedback,
.small,
label {
  color: var(--muted);
  font-size: 13px;
}

.layout {
  display: grid;
  grid-template-columns: minmax(230px, 300px) minmax(500px, 1fr) minmax(360px, 430px);
  gap: 18px;
  padding: 18px 20px;
  align-items: start;
}

.controls {
  grid-column: 1;
  grid-row: 1;
  width: 100%;
}

.explorer {
  grid-column: 3;
  grid-row: 1;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

label {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
  font-weight: 640;
}

input,
select,
button {
  min-height: 38px;
  border-radius: 6px;
  border: 1px solid var(--line);
  font: inherit;
}

input,
select {
  width: 100%;
  padding: 0 10px;
  background: #fff;
  color: var(--ink);
}

button {
  padding: 0 12px;
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  cursor: pointer;
  font-weight: 700;
}

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

.controls > button,
.explorer > button {
  width: 100%;
  margin-top: 8px;
}

.question {
  min-height: 44px;
  font-size: 18px;
  line-height: 1.35;
}

.hint,
.feedback {
  min-height: 38px;
  line-height: 1.35;
  margin-top: 10px;
}

.feedback {
  color: var(--ink);
}

.hint.has-comments {
  display: grid;
  gap: 8px;
}

.position-comments {
  display: grid;
  gap: 8px;
}

.position-comment {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 8px;
  align-items: start;
  padding: 8px;
  border: 1px solid rgba(154, 91, 37, 0.28);
  border-radius: 6px;
  background: #fff6e8;
  color: var(--ink);
}

.position-comment.comment-danger,
.position-comment.comment-trap {
  border-color: rgba(147, 51, 51, 0.32);
  background: #fff0ee;
}

.position-comment.comment-plan {
  border-color: rgba(35, 97, 93, 0.28);
  background: #edf5f2;
}

.comment-label {
  min-height: 20px;
  border-radius: 999px;
  background: rgba(154, 91, 37, 0.14);
  color: var(--accent-2);
  padding: 2px 7px;
  font-size: 10px;
  font-weight: 900;
  text-transform: uppercase;
  white-space: nowrap;
}

.comment-move {
  color: var(--accent);
  font-weight: 850;
  justify-self: end;
}

.comment-text {
  grid-column: 1 / -1;
  min-width: 0;
  line-height: 1.35;
}

.move-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-top: 12px;
}

.hidden-move-row {
  display: none !important;
}

.board-area {
  grid-column: 2;
  grid-row: 1;
  display: grid;
  width: min(100%, 88vh, 900px);
  justify-self: center;
  gap: 10px;
  min-width: 0;
}

.board-toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
}

.turn-badge {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 800;
}

.turn-badge.black {
  background: #263036;
  color: #fff;
  border-color: #263036;
}

.ghost-button {
  min-height: 34px;
  background: #fff;
  color: var(--accent);
  border-color: var(--line);
}

.board-shell {
  border: 2px solid #82a875;
  border-radius: 18px;
  background: #82a875;
  box-shadow: 0 7px 0 #61865a;
  padding: 8px;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  overflow: hidden;
  border-radius: 12px;
  background: #263036;
  touch-action: none;
  user-select: none;
}

.square {
  position: relative;
  display: grid;
  aspect-ratio: 1;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 0;
  color: #111;
  line-height: 1;
  cursor: default;
  touch-action: none;
  user-select: none;
}

.square.movable {
  cursor: grab;
}

.square.movable:active,
.square.drag-source {
  cursor: grabbing;
}

.square.move-target,
.square.capture-target {
  cursor: pointer;
}

.square.light {
  background: #f4f8e9;
}

.square.dark {
  background: #a7c985;
}

.square.selected-square::after,
.square.drag-source::after,
.square.last-move::after,
.square.preview-move::after {
  position: absolute;
  z-index: 1;
  inset: 2%;
  border-radius: 6px;
  background: rgba(241, 200, 75, 0.46);
  box-shadow: inset 0 0 0 1px rgba(121, 95, 21, 0.36);
  content: "";
}

.square.preview-move::after {
  background: rgba(35, 97, 93, 0.34);
  box-shadow: inset 0 0 0 2px rgba(35, 97, 93, 0.42);
}

.square.move-target::before {
  position: absolute;
  z-index: 1;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(241, 200, 75, 0.58);
  content: "";
}

.square.capture-target::before {
  width: 76%;
  height: 76%;
  border: 3px solid rgba(241, 200, 75, 0.72);
  background: transparent;
}

.square.invalid-move-square::after {
  position: absolute;
  z-index: 2;
  inset: 2%;
  border-radius: 6px;
  background: rgba(220, 55, 55, 0.34);
  box-shadow: inset 0 0 0 3px rgba(180, 33, 33, 0.64);
  content: "";
}

.piece {
  position: relative;
  z-index: 2;
  display: block;
  width: 92%;
  height: 92%;
  object-fit: contain;
  pointer-events: none;
  filter: contrast(1.08) drop-shadow(0 1px 1px rgba(0, 0, 0, 0.16));
  transition:
    filter 120ms ease,
    opacity 80ms ease,
    transform 120ms ease;
  user-select: none;
}

.square.selected-square .piece,
.square.drag-source .piece {
  filter: contrast(1.12) drop-shadow(0 2px 2px rgba(0, 0, 0, 0.18));
  transform: scale(1.05);
}

.square.drag-source .piece {
  opacity: 0;
}

.drag-piece-preview {
  position: absolute;
  z-index: 20;
  width: 14.375%;
  height: 14.375%;
  object-fit: contain;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(1.24);
  filter: contrast(1.12) drop-shadow(0 8px 9px rgba(0, 0, 0, 0.34));
}

.invalid-move-x {
  position: absolute;
  z-index: 5;
  display: grid;
  width: 54%;
  height: 54%;
  place-items: center;
  border: 3px solid #fff;
  border-radius: 999px;
  background: rgba(220, 55, 55, 0.92);
  color: #fff;
  font-size: 24px;
  font-weight: 900;
  pointer-events: none;
}

.coordinate {
  position: absolute;
  z-index: 1;
  color: rgba(60, 60, 60, 0.58);
  font-size: clamp(0.55rem, 1.4vw, 0.78rem);
  font-weight: 900;
  pointer-events: none;
}

.file-label {
  right: 6px;
  bottom: 4px;
}

.rank-label {
  top: 4px;
  left: 6px;
}

.move-list,
.gap-list,
.move-history {
  display: grid;
  gap: 6px;
}

.history-toolbar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 8px;
}

.history-move {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  min-height: 24px;
  border-color: var(--line);
  background: #fff;
  color: var(--ink);
  padding: 0 7px;
  text-align: left;
  font-size: 12px;
  font-weight: 750;
}

.history-row {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) minmax(0, 1fr);
  gap: 4px;
  align-items: center;
}

.history-number {
  color: var(--muted);
  font-size: 12px;
  font-weight: 850;
}

.history-cell {
  min-width: 0;
}

.move-history {
  max-height: 190px;
  overflow-y: auto;
  padding-right: 3px;
}

.history-start {
  justify-content: center;
}

.history-move:hover,
.history-move:focus {
  border-color: rgba(35, 97, 93, 0.42);
  background: #f2f6f4;
  outline: none;
}

.history-move.active {
  border-color: rgba(35, 97, 93, 0.54);
  background: #edf5f2;
  color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}

.move-table {
  display: grid;
  gap: 4px;
}

.move-table-head,
.move-row-item {
  display: grid;
  grid-template-columns: minmax(58px, 0.75fr) minmax(58px, 0.7fr) minmax(54px, 0.62fr) minmax(132px, 1.35fr);
  gap: 6px;
  align-items: center;
}

.move-table-head {
  padding: 0 6px 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.move-row-item {
  width: 100%;
  min-height: 36px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  padding: 5px 6px;
  text-align: left;
  font-size: 12px;
  font-weight: 650;
}

.move-row-item:hover,
.move-row-item:focus {
  border-color: var(--line);
  background: #f2f6f4;
  outline: none;
}

.move-row-item.repertoire-move {
  border-color: rgba(35, 97, 93, 0.34);
  background: #edf5f2;
  box-shadow: inset 3px 0 0 var(--accent);
}

.move-row-item.comment-direct-move {
  border-color: rgba(154, 91, 37, 0.48);
  background: #fff6e8;
  box-shadow: inset 3px 0 0 var(--accent-2);
}

.move-row-item.comment-future-move {
  border-color: rgba(84, 93, 171, 0.36);
  background: #f1f2fb;
  box-shadow: inset 3px 0 0 #5b63ad;
}

.move-row-item.repertoire-move:hover,
.move-row-item.repertoire-move:focus,
.move-row-item.comment-direct-move:hover,
.move-row-item.comment-direct-move:focus,
.move-row-item.comment-future-move:hover,
.move-row-item.comment-future-move:focus {
  border-color: rgba(35, 97, 93, 0.54);
}

.move-san {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--accent);
  font-weight: 850;
}

.notation-inline {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.notation-piece {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  object-fit: contain;
}

.notation-text {
  line-height: 1;
  white-space: nowrap;
}

.move-games,
.move-frequency {
  color: var(--muted);
  white-space: nowrap;
}

.wdl-wrap {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.wdl-bar {
  display: flex;
  width: 100%;
  min-width: 0;
  overflow: hidden;
  height: 18px;
  border-radius: 999px;
  background: #d8dde2;
}

.wdl-white,
.wdl-draw,
.wdl-black {
  display: flex;
  align-items: center;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  font-size: 10px;
  font-weight: 900;
  line-height: 1;
}

.wdl-white {
  justify-content: flex-start;
  background: #f9fafb;
  color: #1d2329;
  padding-left: 5px;
}

.wdl-draw {
  background: #aab3bc;
}

.wdl-black {
  justify-content: flex-end;
  background: #2d3339;
  color: #fff;
  padding-right: 5px;
}

.row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: #eef3f2;
  color: var(--accent);
  font-weight: 750;
}

.warn {
  color: var(--accent-2);
  font-weight: 750;
}

@media (max-width: 1040px), (orientation: portrait) {
  .topbar,
  .top-controls {
    align-items: stretch;
    flex-direction: column;
  }

  .top-controls label,
  .top-controls button {
    width: 100%;
  }

  .layout {
    grid-template-columns: 1fr;
  }

  .controls,
  .explorer,
  .board-area {
    grid-column: 1;
  }

  .board-area {
    grid-row: 1;
  }

  .controls {
    grid-row: 2;
  }

  .explorer {
    grid-row: 3;
  }

  .board {
    width: 100%;
  }

  .board-area {
    width: min(96vw, 720px);
  }

  .controls {
    width: min(100%, 720px);
    justify-self: center;
  }
}

@media (max-width: 620px) {
  .topbar {
    padding: 16px;
  }

  .layout {
    padding: 14px;
  }

  .move-row {
    grid-template-columns: 1fr;
  }
}
