:root {
  --bg: #16201b;
  --panel: #1f2a24;
  --text: #e9efe9;
  --muted: #9fb0a5;
  --accent: #5fb36b;
  --accent-text: #0d1a10;
  --light: #eed9b5;
  --dark: #b48763;
  --warn: #ffd34d;
  --error: #ff8a80;
  color-scheme: dark;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* {
  box-sizing: border-box;
}

/* Native feel: the page itself never scrolls, bounces or zooms; the board
   shrinks to whatever space the tool panel leaves. */
html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body {
  position: fixed;
  inset: 0;
  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);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

input {
  user-select: text;
  -webkit-user-select: text;
}

.top {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 3.25rem;
  padding: 0.5rem 1rem;
}

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

main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 34rem;
  margin: 0 auto;
  padding: 0 1rem 0.75rem;
}

.view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

#view-start {
  position: relative;
  justify-content: center;
}

#legal {
  position: absolute;
  inset: auto 0 0;
}

.legal {
  text-align: center;
}

.legal a {
  color: inherit;
}

.view[hidden],
[hidden] {
  display: none !important;
}

.lead {
  color: var(--muted);
  line-height: 1.45;
}

button,
.big {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
}

button:disabled {
  opacity: 0.4;
  cursor: default;
}

.big {
  display: block;
  width: 100%;
  padding: 0.95rem;
  border-radius: 0.9rem;
  background: var(--panel);
  text-align: center;
  font-size: 1.1rem;
  font-weight: 600;
}

.primary {
  background: var(--accent);
  color: var(--accent-text);
}

.ghost {
  padding: 0.55rem 0.9rem;
  border-radius: 0.7rem;
  background: var(--panel);
}

.link {
  background: none;
  color: var(--accent);
  padding: 0.5rem;
}

.row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.wrap {
  flex-wrap: wrap;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.note {
  flex: none;
  margin: 0;
  padding: 0.5rem 0.8rem;
  border-radius: 0.7rem;
  background: #3a3418;
  color: var(--warn);
  font-size: 0.9rem;
}

.error {
  margin: 0;
  color: var(--error);
}

/* ---------- board ---------- */

.board-wrap {
  flex: 1 1 0;
  min-height: 8rem;
  /* Never taller than wide, so spare height ends up below the tools. */
  max-height: min(100vw - 2rem, 32rem);
  container-type: size;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: min(100cqw, 100cqh);
  height: min(100cqw, 100cqh);
  border-radius: 0.4rem;
  overflow: hidden;
  touch-action: none;
}

.sq {
  position: relative;
  padding: 0;
  border-radius: 0;
}

.sq.light {
  background: var(--light);
}

.sq.dark {
  background: var(--dark);
}

.sq.marked::after {
  content: '';
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 3px var(--warn);
  background: rgb(255 211 77 / 0.3);
}

.sq.last::before,
.sq.picked::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgb(155 199 0 / 0.42);
}

.sq.picked::before {
  background: rgb(20 85 30 / 0.5);
}

.sq.target::after {
  content: '';
  position: absolute;
  inset: 36%;
  border-radius: 50%;
  background: rgb(20 85 30 / 0.5);
}

.sq.capture::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 50%;
  box-shadow: inset 0 0 0 0.35rem rgb(20 85 30 / 0.5);
}

.coord {
  position: absolute;
  z-index: 3;
  font-size: max(0.65rem, 3.2cqmin);
  font-weight: 700;
  line-height: 1;
  pointer-events: none;
}

.coord.rank {
  top: 0.15em;
  left: 0.2em;
}

.coord.file {
  right: 0.2em;
  bottom: 0.1em;
}

.sq.light .coord {
  color: var(--dark);
}

.sq.dark .coord {
  color: var(--light);
}

.sq img {
  position: absolute;
  inset: 4%;
  width: 92%;
  height: 92%;
  pointer-events: none;
  z-index: 1;
}

.sq img.lifted {
  opacity: 0.3;
}

.drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 20;
  pointer-events: none;
  filter: drop-shadow(0 6px 6px rgb(0 0 0 / 0.45));
}

.arrows {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

.arrows line {
  stroke-width: 0.17;
  stroke-linecap: round;
}

.arrows .arrow-0 line {
  stroke: rgb(21 120 27 / 0.85);
  stroke-width: 0.22;
}

.arrows .arrow-0 polygon {
  fill: rgb(21 120 27 / 0.85);
}

.arrows .arrow-1 line,
.arrows .arrow-2 line {
  stroke: rgb(0 48 136 / 0.55);
}

.arrows .arrow-1 polygon,
.arrows .arrow-2 polygon {
  fill: rgb(0 48 136 / 0.55);
}

/* ---------- tool panels ---------- */

#edit-tools,
#analysis-tools {
  flex: 0 1 auto;
  min-height: 0;
  /* Room so focus rings and shadows at the edge are not clipped by the scroller. */
  padding: 3px;
  margin: -3px;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.palette {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.3rem;
}

.palette button {
  min-width: 0;
  height: 2.7rem;
  padding: 0.15rem;
  border-radius: 0.5rem;
  background: var(--panel);
}

.palette button img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.palette button[aria-checked='true'] {
  box-shadow: inset 0 0 0 3px var(--accent);
  background: #2f4236;
}

.palette button.used-up {
  opacity: 0.3;
}

.palette .eraser {
  font-size: 0.8rem;
  color: var(--muted);
}

.seg {
  display: flex;
  flex: 1;
  padding: 0.2rem;
  border-radius: 0.8rem;
  background: var(--panel);
}

.seg button {
  flex: 1;
  padding: 0.6rem 0.4rem;
  border-radius: 0.6rem;
  background: none;
}

.seg button[aria-checked='true'] {
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.more summary {
  cursor: pointer;
  color: var(--muted);
  padding: 0.2rem 0;
}

.more[open] {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.castling {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  border: 1px solid #344339;
  border-radius: 0.7rem;
  margin: 0;
}

.castling legend {
  color: var(--muted);
  font-size: 0.85rem;
}

/* ---------- play mode ---------- */

.status {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
}

.status span {
  color: var(--muted);
  font-size: 0.8rem;
  white-space: nowrap;
}

.lines {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.line {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: baseline;
  gap: 0.5rem;
  width: 100%;
  padding: 0.55rem 0.8rem;
  border-radius: 0.7rem;
  background: var(--panel);
  text-align: left;
}

.lines li:first-child .line {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.line strong {
  font-size: 1.15rem;
}

.line .score {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.3rem;
}

.toolbar button {
  min-height: 2.8rem;
  padding: 0.3rem 0.2rem;
  border-radius: 0.6rem;
  background: var(--panel);
  font-size: 0.75rem;
  line-height: 1.15;
}

/* ---------- busy ---------- */

#view-busy {
  align-items: center;
  justify-content: center;
  text-align: center;
}

#busy-photo {
  max-width: 100%;
  max-height: 45dvh;
  border-radius: 0.8rem;
}

.spinner {
  width: 2.5rem;
  height: 2.5rem;
  border: 4px solid #344339;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- dialogs ---------- */

dialog {
  max-width: 22rem;
  border: 0;
  border-radius: 1rem;
  background: var(--panel);
  color: var(--text);
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.6);
}

dialog form {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

dialog h2 {
  margin: 0;
  font-size: 1.1rem;
}

dialog input {
  font: inherit;
  font-size: 16px;
  padding: 0.6rem;
  border-radius: 0.6rem;
  border: 1px solid #344339;
  background: var(--bg);
  color: var(--text);
}

dialog .row button {
  flex: 1;
  padding: 0.6rem;
  border-radius: 0.6rem;
}

.promo {
  display: grid;
  grid-template-columns: repeat(4, 4rem);
  gap: 0.4rem;
}

.promo button {
  aspect-ratio: 1;
  padding: 0.2rem;
  border-radius: 0.6rem;
  background: var(--light);
}

.promo img {
  width: 100%;
  height: 100%;
}
