:root {
  --ink: #14201c;
  --ink-soft: #24352e;
  --paper: #f3efe6;
  --sage: #6f8f7c;
  --leaf: #2f6b52;
  --gold: #c4a35a;
  --danger: #b54a3c;
  --warn: #c47d2e;
  --board-light: #f0d9b5;
  --board-dark: #b58863;
  --shadow: 0 18px 50px rgba(20, 32, 28, 0.16);
  --radius: 18px;
  --font-display: "Fraunces", Georgia, serif;
  --font-ui: "Sora", sans-serif;
  --rail: 4.25rem;
  --coord: 1.1rem;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  min-height: 100%;
  font-family: var(--font-ui);
  color: var(--ink);
  background:
    radial-gradient(ellipse 80% 55% at 10% -10%, rgba(196, 163, 90, 0.18), transparent 50%),
    radial-gradient(ellipse 70% 50% at 100% 0%, rgba(47, 107, 82, 0.16), transparent 45%),
    linear-gradient(165deg, #eef3ef 0%, #e4ebe5 48%, #d7e0d8 100%);
}

body {
  display: grid;
  place-items: start center;
  padding: 1.25rem 1rem 2.5rem;
}

.app {
  width: min(100%, 640px);
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  animation: rise 0.55s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: translateY(0); }
}

.top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}

.brand-mark {
  font-family: var(--font-display);
  font-size: clamp(1.6rem, 5vw, 2rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.brand-sub {
  margin-top: 0.35rem;
  font-size: 0.82rem;
  color: var(--sage);
}

.engine-pill {
  flex-shrink: 0;
  margin-top: 0.25rem;
  padding: 0.45rem 0.7rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.55);
  border: 1px solid rgba(20, 32, 28, 0.08);
  color: var(--sage);
}

.engine-pill[data-state="ready"] {
  color: var(--leaf);
  border-color: rgba(47, 107, 82, 0.25);
  background: rgba(47, 107, 82, 0.08);
}

.engine-pill[data-state="error"] {
  color: var(--danger);
  border-color: rgba(181, 74, 60, 0.25);
  background: rgba(181, 74, 60, 0.08);
}

.modes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  padding: 0.3rem;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.45);
  border: 1px solid rgba(20, 32, 28, 0.06);
}

.mode {
  appearance: none;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 0.7rem 0.5rem;
  border-radius: 11px;
  color: var(--sage);
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
  text-align: center;
  text-decoration: none;
  display: grid;
  place-items: center;
}

.mode:hover { color: var(--ink); }
.mode.active {
  background: var(--ink);
  color: var(--paper);
}
.mode-link:hover {
  background: rgba(20, 32, 28, 0.06);
}

.setup {
  display: grid;
  grid-template-columns: 1.4fr 0.8fr;
  gap: 0.65rem;
  animation: fade 0.35s ease both;
}

.setup.hidden, .toolbar.hidden, .progress-card.hidden { display: none; }

.progress-card {
  background: rgba(255, 255, 255, 0.62);
  border: 1px solid rgba(20, 32, 28, 0.07);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow);
  animation: fade 0.35s ease both;
}

.progress-top {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: baseline;
}

.progress-title {
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.progress-meta {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--leaf);
  white-space: nowrap;
}

.progress-bar {
  margin-top: 0.65rem;
  height: 0.45rem;
  border-radius: 999px;
  background: rgba(20, 32, 28, 0.08);
  overflow: hidden;
}

.progress-bar span {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--leaf), #4a9a6e);
  transition: width 0.35s ease;
}

.progress-sub {
  margin-top: 0.55rem;
  font-size: 0.8rem;
  line-height: 1.45;
  color: var(--sage);
}

@keyframes fade {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

#freestyleSetup { grid-template-columns: 1fr; }

.setup-note {
  font-size: 0.86rem;
  line-height: 1.55;
  color: var(--ink-soft);
}

.freestyle-row {
  display: flex;
  gap: 0.65rem;
  align-items: end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--sage);
}

.field.grow { flex: 1; }

.field select {
  appearance: none;
  width: 100%;
  font: inherit;
  font-size: 0.92rem;
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.72) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%236f8f7c' d='M1 1l5 5 5-5'/%3E%3C/svg%3E") right 0.85rem center / 12px no-repeat;
  border: 1px solid rgba(20, 32, 28, 0.1);
  border-radius: 12px;
  padding: 0.75rem 2.2rem 0.75rem 0.85rem;
  outline: none;
}

.field select:focus {
  border-color: rgba(47, 107, 82, 0.45);
  box-shadow: 0 0 0 3px rgba(47, 107, 82, 0.12);
}

.coach {
  background: rgba(255, 255, 255, 0.62);
  border: 1px solid rgba(20, 32, 28, 0.07);
  border-radius: var(--radius);
  padding: 0.8rem 0.95rem;
  box-shadow: var(--shadow);
  min-height: 4.5rem;
  max-height: 7.5rem;
  overflow: auto;
}

.coach[data-tone="ok"] {
  background: rgba(47, 107, 82, 0.08);
  border-color: rgba(47, 107, 82, 0.22);
}

.coach[data-tone="bad"] {
  background: rgba(181, 74, 60, 0.07);
  border-color: rgba(181, 74, 60, 0.2);
}

.coach-kicker {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--sage);
  margin-bottom: 0.45rem;
}

.coach-body {
  font-size: 0.95rem;
  line-height: 1.55;
  color: var(--ink-soft);
}

.coach-body p + p { margin-top: 0.65rem; }
.coach-body strong { color: var(--ink); font-weight: 600; }
.coach-body .mv {
  font-family: var(--font-display);
  color: var(--leaf);
  font-weight: 700;
}
.coach-body .muted { color: var(--sage); font-size: 0.88rem; }

/* ---- Board layout ---- */

.board-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--rail);
  gap: 0.65rem;
  align-items: stretch;
}

.board-frame {
  --board-edge: 3px;
  display: grid;
  grid-template-columns: var(--coord) minmax(0, 1fr);
  grid-template-rows: auto auto;
  gap: 0.2rem 0.25rem;
  align-items: stretch;
  justify-items: stretch;
  width: min(100%, calc(480px + var(--coord) + 0.25rem), calc(100dvh - 22rem + var(--coord) + 0.25rem));
  min-width: 0;
}

.board-frame.hide-coords {
  grid-template-columns: minmax(0, 1fr);
  width: min(100%, 480px, calc(100dvh - 22rem));
}

.board-frame.hide-coords .coords,
.board-frame.hide-coords .coord-corner {
  display: none;
}

.coords.ranks {
  grid-column: 1;
  grid-row: 1;
}

.board-shell {
  grid-column: 2;
  grid-row: 1;
  width: 100%;
  aspect-ratio: 1 / 1;
  position: relative;
  min-width: 0;
}

.coord-corner {
  grid-column: 1;
  grid-row: 2;
}

.coords.files {
  grid-column: 2;
  grid-row: 2;
}

.board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: var(--shadow);
  touch-action: manipulation;
  background: #6b4f35;
  border: var(--board-edge) solid #5a412c;
}

.sq {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  background: var(--board-light);
  overflow: hidden;
}

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

.sq.selected {
  box-shadow: inset 0 0 0 3px rgba(196, 163, 90, 0.95);
  z-index: 1;
}

.sq.last {
  background-image: linear-gradient(rgba(196, 163, 90, 0.38), rgba(196, 163, 90, 0.38));
}

.sq.dark.last {
  background-image: linear-gradient(rgba(196, 163, 90, 0.42), rgba(196, 163, 90, 0.42));
}

.sq.best {
  box-shadow: inset 0 0 0 3px rgba(47, 107, 82, 0.9);
}

.sq.move::after,
.sq.capture::after {
  content: "";
  position: absolute;
  pointer-events: none;
  border-radius: 50%;
  z-index: 1;
}

.sq.move::after {
  top: 50%;
  left: 50%;
  width: 26%;
  height: 26%;
  transform: translate(-50%, -50%);
  background: rgba(20, 32, 28, 0.28);
}

.sq.capture::after {
  inset: 10%;
  background: transparent;
  border: 3px solid rgba(181, 74, 60, 0.75);
}

.piece-img {
  position: relative;
  z-index: 2;
  display: block;
  width: 78%;
  height: 78%;
  max-width: 78%;
  max-height: 78%;
  object-fit: contain;
  object-position: center center;
  pointer-events: none;
  user-select: none;
  border: 0;
  transition: transform 0.15s cubic-bezier(0.22, 1, 0.36, 1);
}

.sq:active .piece-img { transform: scale(0.94); }

.coords {
  display: grid;
  color: var(--sage);
  font-size: 0.68rem;
  font-weight: 600;
  user-select: none;
  line-height: 1;
}

.coords.ranks {
  grid-template-rows: repeat(8, minmax(0, 1fr));
  align-self: stretch;
  height: auto;
  /* Match .board border so labels sit on square centers */
  padding: var(--board-edge) 0;
  box-sizing: border-box;
}

.coords.ranks span {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 0.15rem;
}

.coords.files {
  grid-template-columns: repeat(8, minmax(0, 1fr));
  width: 100%;
  height: var(--coord);
  /* Match .board border so a–h sit under square centers */
  padding: 0 var(--board-edge);
  box-sizing: border-box;
}

.coords.files span {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 0.15rem;
}

.board-rail {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: 0.35rem 0;
}

.rail-btn {
  appearance: none;
  border: 1px solid rgba(20, 32, 28, 0.1);
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink-soft);
  border-radius: 12px;
  padding: 0.7rem 0.35rem;
  font: inherit;
  font-size: 0.68rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  transition: background 0.2s ease, transform 0.15s ease, border-color 0.2s ease;
}

.rail-btn:hover {
  background: #fff;
  border-color: rgba(47, 107, 82, 0.28);
  transform: translateY(-1px);
}

.rail-btn.active {
  background: rgba(47, 107, 82, 0.1);
  border-color: rgba(47, 107, 82, 0.35);
  color: var(--leaf);
}

.rail-icon {
  font-size: 1rem;
  line-height: 1;
}

.rail-note {
  margin-top: auto;
  font-size: 0.62rem;
  line-height: 1.35;
  color: var(--sage);
  text-align: center;
  padding: 0.2rem;
}

.status {
  text-align: center;
  font-size: 0.86rem;
  font-weight: 500;
  color: var(--sage);
  min-height: 1.25rem;
}

.app.thinking .status {
  color: var(--leaf);
  animation: pulse 1.1s ease-in-out infinite;
}

@keyframes pulse {
  50% { opacity: 0.45; }
}

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

.btn {
  appearance: none;
  border: 0;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 0.85rem 0.7rem;
  border-radius: 12px;
  cursor: pointer;
  background: var(--ink);
  color: var(--paper);
  transition: transform 0.15s ease, background 0.2s ease;
}

.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }
.btn.soft {
  background: rgba(255, 255, 255, 0.7);
  color: var(--ink);
  border: 1px solid rgba(20, 32, 28, 0.08);
}
.btn.ghost {
  background: var(--leaf);
  color: #fff;
  white-space: nowrap;
  padding-inline: 1rem;
  height: 2.85rem;
}
.btn.warn { background: var(--warn); color: #fff; }
.btn.danger { background: var(--danger); color: #fff; }

@media (max-width: 520px) {
  .board-layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto;
  }

  .board-rail {
    flex-direction: row;
    align-items: stretch;
    padding: 0;
  }

  .rail-btn {
    flex: 1;
    flex-direction: row;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.7rem 0.5rem;
  }

  .rail-note {
    display: none;
  }

  .setup { grid-template-columns: 1fr; }
  .freestyle-row { flex-direction: column; align-items: stretch; }
  .btn.ghost { height: auto; }
}

.site-nav {
  margin: 0 0 0.35rem;
  font-size: 0.82rem;
}
.site-nav a {
  color: var(--sage);
  text-decoration: none;
  font-weight: 500;
}
.site-nav a:hover { color: var(--ink); }
