:root {
  color-scheme: dark;
  --bg: #111315;
  --panel: #1b1f22;
  --panel-2: #23282c;
  --border: #343b42;
  --text: #eef2f5;
  --muted: #aab4bc;
  --accent: #72b56b;
  --bad: #e46b6b;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
textarea,
input {
  font: inherit;
}

button,
.file-button {
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--text);
  padding: 0.55rem 0.75rem;
}

button:active,
.file-button:active {
  transform: translateY(1px);
}

textarea,
input,
select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #111518;
  color: var(--text);
  padding: 0.65rem;
}

textarea {
  resize: vertical;
}

.app {
  width: min(1220px, 100%);
  margin: 0 auto;
  padding: 0.85rem;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 1rem;
  margin-bottom: 0.85rem;
}

.topbar h1 {
  margin: 0;
  font-size: 1.25rem;
}

.topbar p {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.icon-button {
  width: 42px;
  min-height: 42px;
  padding: 0;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 0.85rem;
  align-items: start;
}

.board-panel,
.side-panel,
.settings {
  min-width: 0;
  max-width: calc(100vw - 1.3rem);
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 0.75rem;
}

.board {
  width: min(100%, calc(100vw - 2.4rem));
  min-width: 0;
  aspect-ratio: 1;
  max-height: calc(100vh - 150px);
}

.board.cg-wrap {
  width: min(100%, calc(100vw - 2.4rem)) !important;
  height: auto !important;
  aspect-ratio: 1;
}

.nav-row,
.button-row,
.engine-head,
.position-head,
.notation-head {
  display: flex;
  gap: 0.5rem;
}

.nav-row {
  margin-top: 0.75rem;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.nav-row button {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-row button,
.button-row button {
  white-space: nowrap;
}

.side-panel {
  display: grid;
  gap: 0.75rem;
}

.pane {
  display: grid;
  gap: 0.65rem;
}

.file-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex: 1;
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
}

.button-row button {
  flex: 1;
}

.side-panel {
  background: transparent;
  border-color: transparent;
  padding: 0;
}

.position-box,
.engine-box,
.moves-box,
.annotation-box,
.import-box {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #14181b;
  padding: 0.7rem;
}

.import-box summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 650;
}

.import-box .pane {
  margin-top: 0.65rem;
}

.position-head,
.engine-head,
.annotation-head {
  align-items: center;
  justify-content: space-between;
}

.annotation-head {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}

.annotation-head select {
  width: auto;
  min-width: 92px;
}

.variation-list {
  display: grid;
  gap: 0.35rem;
  margin-top: 0.6rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.variation-list div {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.45rem 0.55rem;
  background: #111518;
}

.position-head span {
  color: var(--muted);
  font-size: 0.9rem;
}

.fen-edit {
  min-height: 36px;
  margin-top: 0.65rem;
  color: #d8e2e8;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.84rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.move-list {
  display: block;
  margin-top: 0.65rem;
  max-height: 220px;
  overflow: auto;
  line-height: 1.85;
}

.notation-head {
  align-items: center;
  justify-content: space-between;
}

.notation-head span {
  color: var(--muted);
  font-size: 0.82rem;
}

.move-number {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  margin-right: 0.25rem;
}

.move-list button {
  min-height: 0;
  margin: 0 0.15rem 0.2rem 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  padding: 0.08rem 0.18rem;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.move-list button.current {
  background: #20301f;
  color: #efffed;
}

.move-variation {
  color: var(--muted);
  font-size: 0.92em;
}

.move-nag {
  margin: 0 0.16rem 0 -0.18rem;
  font-weight: 800;
}

.nag-brilliant {
  color: #37c8ff;
}

.move-list button.nag-brilliant {
  color: #37c8ff;
}

.nag-good {
  color: #2f80ff;
}

.move-list button.nag-good {
  color: #2f80ff;
}

.nag-interesting {
  color: #58c878;
}

.move-list button.nag-interesting {
  color: #58c878;
}

.nag-dubious {
  color: #f0c84b;
}

.move-list button.nag-dubious {
  color: #f0c84b;
}

.nag-mistake {
  color: #ff9f43;
}

.move-list button.nag-mistake {
  color: #ff9f43;
}

.nag-blunder {
  color: #ff5a5f;
}

.move-list button.nag-blunder {
  color: #ff5a5f;
}

.move-comment {
  margin-right: 0.22rem;
  color: #c7d0d7;
  font-style: italic;
}

#analyzeBtn.active {
  border-color: var(--accent);
  background: #20301f;
  color: #efffed;
}

.analysis-lines {
  margin: 0.7rem 0 0;
  padding-left: 1.25rem;
}

.analysis-lines li {
  margin: 0.45rem 0;
}

.analysis-lines .score {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.analysis-lines .error {
  color: var(--bad);
}

.settings {
  position: relative;
  z-index: 2;
  width: min(520px, calc(100vw - 1.5rem));
  max-width: none;
  margin: 8vh auto 0;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.55);
}

.settings-overlay:not([hidden]) {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: block;
  padding: 0.75rem;
}

.settings-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.62);
}

.settings {
  display: grid;
}

.settings-head {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-head h2 {
  margin: 0;
  font-size: 1.05rem;
}

@media (min-width: 821px) {
  .topbar,
  .workspace {
    grid-template-columns: minmax(420px, min(72vh, 680px)) minmax(360px, 600px);
    width: max-content;
    max-width: 100%;
    margin-inline: auto;
  }

  .topbar h1 {
    grid-column: 1;
  }

  .topbar .icon-button {
    grid-column: 2;
    justify-self: end;
  }

  .board-panel {
    width: min(72vh, 680px);
  }

  .board,
  .board.cg-wrap {
    width: 100% !important;
    max-height: none;
  }
}

@media (max-width: 820px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }

  .board-panel,
  .side-panel {
    width: min(100%, 352px);
  }

  .board {
    max-height: none;
  }

  .board,
  .board.cg-wrap {
    width: min(100%, 340px) !important;
  }

  .nav-row {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .nav-row button {
    padding-inline: 0.35rem;
  }

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

@media (max-width: 420px) {
  .app {
    padding: 0.65rem;
  }

  .board-panel,
  .side-panel {
    padding: 0.55rem;
  }

  .nav-row {
    gap: 0.35rem;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .nav-row button {
    min-height: 38px;
    padding-inline: 0.25rem;
    font-size: 0.9rem;
  }
}
