:root {
  color-scheme: light;
  --ink: #171c1b;
  --muted: #66716d;
  --panel: #f6f1e7;
  --surface: rgba(255, 252, 245, 0.92);
  --card: #fffaf0;
  --button: #fffdf8;
  --line: #d3c8b8;
  --green: #356f56;
  --blue: #1f6e88;
  --red: #a94635;
  --gold: #d0a23c;
  --dark-square: #557066;
  --light-square: #efe2c7;
  --focus: #e5ad3d;
  --shadow: rgba(44, 36, 22, 0.16);
  --page:
    linear-gradient(135deg, rgba(53, 111, 86, 0.18), transparent 34%),
    linear-gradient(315deg, rgba(169, 70, 53, 0.13), transparent 31%),
    #e7dfd2;
}

html.dark-theme {
  color-scheme: dark;
  --ink: #f0ede4;
  --muted: #c2bbb0;
  --panel: #1f2622;
  --surface: rgba(30, 37, 33, 0.95);
  --card: #26312d;
  --button: #202a27;
  --line: #485249;
  --green: #68a66f;
  --blue: #6fb4d4;
  --red: #d47c6a;
  --gold: #e8b34c;
  --dark-square: #2a3838;
  --light-square: #c9ba99;
  --focus: #f0bc55;
  --shadow: rgba(0, 0, 0, 0.32);
  --page:
    linear-gradient(135deg, rgba(53, 111, 86, 0.2), transparent 34%),
    linear-gradient(315deg, rgba(169, 70, 53, 0.16), transparent 31%),
    #141917;
}

* {
  box-sizing: border-box;
}

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

button,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.site-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  width: min(1240px, calc(100vw - 32px));
  margin: 18px auto 0;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 18px 42px var(--shadow);
}

a {
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: max-content;
  font-size: 1.1rem;
  font-weight: 950;
}
.brand-mark img {
  width: 38px;
  height: 38px;
  object-fit: contain;
}

.nav-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.nav-links a,
.small-button,
.action-button,
.reset-button,
.modal-actions button {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #bfb5a4;
  border-radius: 8px;
  background: var(--button);
  color: var(--ink);
  padding: 0 12px;
  font-weight: 850;
}

.nav-links a:hover,
button:hover,
select:hover {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px rgba(229, 173, 61, 0.22);
}

.game-shell {
  width: min(1240px, calc(100vw - 32px));
  margin: 16px auto 32px;
}

.topbar,
.scorebar,
.workspace,
.move-log-panel,
.library-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 18px 42px var(--shadow);
}

.topbar {
  display: grid;
  grid-template-columns: minmax(260px, 0.85fr) minmax(420px, 1.15fr);
  gap: 20px;
  padding: 20px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 850;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(3.5rem, 10vw, 8rem);
  line-height: 0.86;
}

.rules-summary {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.rules-summary div,
.status-card,
.spell-slot {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  padding: 12px;
}

.rules-summary strong,
.rules-summary span {
  display: block;
}

.rules-summary span {
  margin-top: 4px;
  color: var(--muted);
  line-height: 1.35;
}

.scorebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 16px;
  padding: 14px 18px;
}

.scorebar h2 {
  margin: 0;
  font-size: clamp(1.55rem, 3vw, 2.5rem);
}

.score-pair {
  display: flex;
  gap: 8px;
}

.score-card {
  display: grid;
  place-items: center;
  min-width: 58px;
  min-height: 58px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
}

.score-card span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 900;
}

.score-card strong {
  font-size: 1.35rem;
}

.white-score strong {
  color: var(--blue);
}

.black-score strong {
  color: var(--red);
}

.workspace {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 18px;
  margin-top: 16px;
  padding: 18px;
}

.panel {
  display: grid;
  align-content: start;
  gap: 12px;
}

.control-card {
  display: grid;
  gap: 7px;
}

label {
  color: var(--muted);
  font-weight: 850;
}

select {
  min-height: 38px;
  border: 1px solid #bfb5a4;
  border-radius: 8px;
  background: var(--button);
  padding: 0 10px;
}

.action-group {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.action-icon {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  margin-right: 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f0e6d5;
  font-weight: 950;
}

.reset-button {
  width: 100%;
  min-height: 46px;
  background: #26352f;
  color: white;
}

.board-wrap {
  display: grid;
  gap: 12px;
  justify-items: center;
  min-width: 0;
}

.board-shell {
  width: min(100%, 720px);
}

.rank-file-grid {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) 24px;
  align-items: center;
  gap: 7px;
}

.files,
.ranks {
  display: grid;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 900;
}

.files {
  grid-template-columns: repeat(8, 1fr);
  padding: 0 31px;
  text-align: center;
}

.top-files {
  margin-bottom: 5px;
}

.ranks {
  grid-template-rows: repeat(8, 1fr);
  height: 100%;
  text-align: center;
}

.ranks span {
  display: grid;
  place-items: center;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1;
  overflow: hidden;
  border: 3px solid #26352f;
  border-radius: 8px;
  background: #26352f;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  border: 0;
  padding: 0;
  font-size: clamp(1.6rem, 6vw, 3.95rem);
  line-height: 1;
}

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

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

.piece.white {
  color: #fffdf8;
  text-shadow: 0 2px 0 #31413b, 0 0 10px rgba(0, 0, 0, 0.22);
}

.piece.black {
  color: #17201d;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.45);
}

.piece {
  position: relative;
  display: grid;
  width: 100%;
  height: 100%;
  place-items: center;
}

.piece.has-art {
  color: transparent;
  text-shadow: none;
}

.piece-art {
  position: absolute;
  width: 86%;
  height: 86%;
  object-fit: contain;
  pointer-events: none;
}

.square.selected::after,
.square.legal::after,
.square.last-move::after,
.square.in-check::after {
  content: "";
  position: absolute;
  inset: 6px;
  border-radius: 8px;
  pointer-events: none;
}

.square.selected::after {
  border: 4px solid var(--gold);
}

.square.legal::after {
  background: rgba(31, 110, 136, 0.28);
  box-shadow: inset 0 0 0 3px rgba(31, 110, 136, 0.45);
}

.square.last-move::after {
  border: 3px solid rgba(169, 70, 53, 0.62);
}

.square.in-check::after {
  background: rgba(169, 70, 53, 0.24);
  box-shadow: inset 0 0 0 4px rgba(169, 70, 53, 0.7);
}

.message {
  width: min(100%, 720px);
  min-height: 48px;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  padding: 12px 14px;
  color: var(--muted);
  font-weight: 750;
  line-height: 1.35;
}

.spell-card {
  display: grid;
  gap: 7px;
  border: 1px solid #bfb5a4;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(31, 110, 136, 0.1), transparent 45%),
    #fffdf8;
  padding: 12px;
}

.spell-card.empty {
  color: var(--muted);
  font-weight: 750;
}

.spell-title {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.05;
}

.spell-piece {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 900;
  text-transform: uppercase;
}

.spell-text {
  margin: 0;
  color: #25312e;
  line-height: 1.32;
}

.spell-flavor {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-style: italic;
  line-height: 1.3;
}

.spell-badge {
  width: max-content;
  border-radius: 6px;
  background: rgba(53, 111, 86, 0.12);
  color: var(--green);
  padding: 3px 7px;
  font-size: 0.72rem;
  font-weight: 900;
}

.spell-badge.teaching {
  background: rgba(169, 70, 53, 0.12);
  color: var(--red);
}

.large {
  min-height: 270px;
}

.lower-grid {
  display: grid;
  grid-template-columns: minmax(280px, 0.8fr) minmax(320px, 1.2fr);
  gap: 16px;
  margin-top: 16px;
}

.move-log-panel,
.library-panel {
  min-height: 260px;
  padding: 16px;
}

.move-log-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.move-log-header h2 {
  margin: 0;
}

.move-log {
  display: grid;
  gap: 8px;
  max-height: 420px;
  margin: 0;
  padding-left: 20px;
  overflow: auto;
}

.move-log li {
  color: var(--muted);
  line-height: 1.35;
}

.card-library {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 10px;
  max-height: 520px;
  overflow: auto;
  padding-right: 4px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(23, 28, 27, 0.56);
}

.modal-backdrop.hidden {
  display: none;
}

.modal {
  width: min(440px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 18px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.34);
}

.modal-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.modal-actions button {
  min-height: 48px;
}

@media (max-width: 920px) {
  .site-nav,
  .game-shell {
    width: min(100vw - 18px, 720px);
  }

  .site-nav,
  .topbar,
  .scorebar,
  .workspace,
  .lower-grid {
    grid-template-columns: 1fr;
  }

  .site-nav,
  .scorebar {
    align-items: stretch;
    flex-direction: column;
  }

  .nav-links {
    justify-content: flex-start;
  }

  .rules-summary {
    grid-template-columns: 1fr;
  }

  .workspace {
    padding: 12px;
  }

  .panel {
    order: 2;
  }

  .board-wrap {
    order: 1;
  }

  .rank-file-grid {
    grid-template-columns: 18px minmax(0, 1fr) 18px;
    gap: 4px;
  }

  .files {
    padding: 0 22px;
  }
}

@media (max-width: 460px) {
  .nav-links,
  .score-pair,
  .action-group,
  .modal-actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .nav-links a {
    justify-content: center;
  }

  h1 {
    font-size: 3.2rem;
  }
}
