:root {
  color-scheme: light;
  --canvas: #f5f5f7;
  --panel: #ffffff;
  --panel-soft: #fbfbfd;
  --line: #d8d8de;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --accent: #0066cc;
  --accent-strong: #0057b8;
  --accent-soft: #eaf3ff;
  --gold: #8a6500;
  --gold-soft: #fff7dc;
  --danger: #a23b34;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.07);
}

* {
  box-sizing: border-box;
}

html {
  height: 100%;
  min-width: 320px;
  background: var(--canvas);
}

body {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--canvas);
  letter-spacing: 0;
  overflow: hidden;
}

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

button,
input {
  font: inherit;
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: min(1500px, 100%);
  height: 100dvh;
  margin: 0 auto;
}

.topbar {
  flex: 0 0 auto;
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(220px, 320px) minmax(240px, 1fr);
  gap: 16px;
  align-items: center;
  padding: 14px 24px;
  background: rgba(245, 245, 247, 0.82);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(22px);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 10px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  color: white;
  background: linear-gradient(180deg, #0a84ff, #0057b8);
  border-radius: 8px;
  font-size: 22px;
}

.brand strong,
.brand small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand strong {
  font-size: 16px;
}

.brand small {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.search-box {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 10px;
  height: 44px;
  padding: 0 14px;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.search-box span {
  color: var(--accent);
  font-size: 20px;
}

.search-box input {
  width: 100%;
  min-width: 0;
  color: var(--text);
  background: transparent;
  border: 0;
  outline: 0;
}

.search-box input::placeholder {
  color: #737a75;
}

.stage-tabs button,
.action-link,
.primary-action,
.tool-button {
  min-height: 34px;
  padding: 7px 10px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.stage-tabs button:hover,
.action-link:hover,
.tool-button:hover {
  border-color: var(--accent);
}

.primary-action {
  color: #ffffff;
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  cursor: pointer;
}

.tool-button {
  cursor: pointer;
}

.primary-action:disabled,
.tool-button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(300px, 360px) minmax(0, 1fr);
  flex: 1 1 auto;
  gap: 20px;
  min-height: 0;
  overflow: hidden;
  padding: 22px 24px 32px;
}

.content-pane {
  position: static;
  min-width: 0;
  max-height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  align-self: stretch;
}

.metric-label {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.section-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
}

.section-heading h1,
.section-heading h2 {
  margin: 0;
  color: var(--text);
  font-size: 20px;
  letter-spacing: 0;
}

.section-heading span {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.stage-tabs {
  display: grid;
  gap: 7px;
  margin-bottom: 10px;
}

.stage-tab-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.stage-tab-row:last-child {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.stage-tabs button {
  width: 100%;
  cursor: pointer;
}

.stage-tabs button[aria-selected="true"] {
  color: #ffffff;
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.leaderboard-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.leaderboard-card {
  min-width: 0;
  overflow: hidden;
  padding: 14px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  animation: paneIn 360ms ease both;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.stage-title {
  margin: 0;
  color: var(--text);
  font-size: 28px;
  line-height: 1;
}

.stage-range {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.leaderboard-table tr {
  cursor: pointer;
}

.leaderboard-table tr + tr {
  border-top: 1px solid rgba(217, 216, 207, 0.82);
}

.leaderboard-table tr:hover {
  background: rgba(228, 242, 238, 0.7);
}

.leaderboard-table td {
  padding: 9px 4px;
  vertical-align: middle;
}

.rank-cell {
  width: 34px;
}

.rank-badge {
  display: inline-grid;
  place-items: center;
  width: 25px;
  height: 25px;
  color: #ffffff;
  background: #1d1d1f;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.player-name {
  overflow-wrap: anywhere;
  font-weight: 650;
}

.player-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.rating-cell {
  width: 66px;
  text-align: right;
}

.rating-value {
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.rating-kind {
  margin-top: 2px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
}

.note-pill {
  display: inline-flex;
  margin-top: 6px;
  padding: 3px 7px;
  color: var(--gold);
  background: var(--gold-soft);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 650;
}

.bar-track {
  position: relative;
  height: 5px;
  margin-top: 7px;
  overflow: hidden;
  background: rgba(0, 102, 204, 0.12);
  border-radius: 999px;
}

.bar-fill {
  height: 100%;
  width: var(--bar-width);
  background: var(--accent);
  border-radius: inherit;
  transform-origin: left center;
  animation: growBar 620ms ease-out both;
}

.ranking-note {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.search-results {
  margin-bottom: 20px;
}

.bulk-player-hit {
  display: grid;
  gap: 4px;
  padding: 12px 14px;
  color: var(--text);
  background: #edf8f5;
  border: 1px solid rgba(18, 119, 111, 0.22);
  border-radius: 8px;
}

.bulk-player-hit strong {
  font-size: 0.92rem;
}

.bulk-player-hit span {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.static-player-hit {
  display: grid;
  gap: 10px;
  padding: 12px 14px;
  color: var(--text);
  background: #eef7ff;
  border: 1px solid rgba(42, 110, 178, 0.2);
  border-radius: 8px;
}

.static-player-hit strong,
.static-player-hit span {
  display: block;
}

.static-player-hit strong {
  font-size: 0.94rem;
}

.static-player-hit span {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.pgn-package-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
}

.pgn-package-button {
  display: grid;
  gap: 3px;
  min-width: 0;
  padding: 9px 10px;
  color: #174b7c;
  text-align: left;
  background: #fff;
  border: 1px solid rgba(42, 110, 178, 0.22);
  border-radius: 8px;
  cursor: pointer;
}

.pgn-package-button strong,
.pgn-package-button span {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pgn-package-button strong {
  font-size: 0.82rem;
  font-weight: 650;
}

.pgn-package-button span {
  color: var(--muted);
  font-size: 0.76rem;
}

.pgn-package-button:hover {
  border-color: #2a6eb2;
}

.pgn-package-button[aria-pressed="true"] {
  color: #ffffff;
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.pgn-package-button[aria-pressed="true"] span {
  color: rgba(255, 255, 255, 0.78);
}

.result-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 10px;
}

.result-button {
  width: 100%;
  min-width: 0;
  padding: 12px;
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.result-button:hover {
  border-color: var(--accent);
}

.result-button[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.detail-pane {
  align-self: stretch;
  min-width: 0;
  max-height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  padding: 22px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  animation: paneIn 420ms ease both;
}

.detail-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.detail-title h2 {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: clamp(26px, 3vw, 42px);
  line-height: 1.06;
  letter-spacing: 0;
}

.detail-title p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.detail-cn-name {
  margin-top: 6px;
  color: var(--text);
  font-size: 18px;
  font-weight: 650;
}

.detail-title-actions {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.stage-chip {
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  min-height: 30px;
  padding: 6px 9px;
  color: #ffffff;
  background: #1d1d1f;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

.rating-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 14px 0;
}

.rating-card {
  min-width: 0;
  padding: 10px;
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.rating-card span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.rating-card strong {
  display: block;
  margin-top: 6px;
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.metric-tile {
  min-width: 0;
  padding: 14px;
  background: #f5f5f7;
  border: 1px solid #e5e5ea;
  border-radius: 8px;
}

.metric-tile span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.metric-tile strong {
  display: block;
  margin-top: 8px;
  font-size: clamp(22px, 2vw, 34px);
  font-variant-numeric: tabular-nums;
}

.action-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}

.download-status {
  margin-bottom: 12px;
  padding: 9px 10px;
  color: var(--muted);
  background: var(--panel-soft);
  border-radius: 8px;
  font-size: 12px;
  line-height: 1.45;
}

.pgn-viewer {
  display: grid;
  gap: 10px;
  margin-bottom: 16px;
  padding: 12px;
  color: var(--text);
  background: #f5f5f7;
  border: 1px solid #e5e5ea;
  border-radius: 8px;
}

.pgn-download-link {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  max-width: 100%;
  min-height: 34px;
  overflow-wrap: anywhere;
  padding: 7px 10px;
  color: var(--accent-strong);
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 650;
}

.pgn-download-link:hover {
  border-color: var(--accent);
}

.pgn-viewer.is-empty,
.pgn-viewer.is-loading {
  min-height: 92px;
}

.pgn-viewer-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
}

.pgn-viewer-head h3 {
  margin: 0;
  color: var(--text);
  font-size: 18px;
  line-height: 1.25;
}

.pgn-viewer-head span {
  display: block;
  margin-top: 4px;
  overflow-wrap: anywhere;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.viewer-pulse {
  flex: 0 0 auto;
  width: 12px;
  height: 12px;
  margin-top: 5px;
  background: var(--accent);
  border-radius: 50%;
  animation: viewerPulse 1.1s ease-in-out infinite;
}

.viewer-select {
  display: grid;
  gap: 6px;
}

.viewer-select span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.viewer-select select {
  width: 100%;
  min-width: 0;
  height: 36px;
  padding: 0 10px;
  color: var(--text);
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: 0;
}

.lichess-viewer-shell {
  min-width: 0;
  max-height: min(64vh, 560px);
  overflow: auto;
  overscroll-behavior: contain;
}

.lichess-viewer-host {
  min-width: 0;
}

.lichess-viewer-shell .lpv {
  --c-lpv-bg: #f5f5f7;
  --c-lpv-bg-player: #ffffff;
  --c-lpv-bg-controls: #ffffff;
  --c-lpv-bg-movelist: #ffffff;
  --c-lpv-bg-variation: #f5f5f7;
  --c-lpv-bg-pane: #ffffff;
  --c-lpv-side-border: #e5e5ea;
  --c-lpv-border: #d8d8de;
  --c-lpv-font: #1d1d1f;
  --c-lpv-font-shy: #86868b;
  --c-lpv-font-bg: #d2d2d7;
  --c-lpv-accent: #0066cc;
  --c-lpv-accent-over: #ffffff;
  --c-lpv-current-move: #0066cc;
  --c-lpv-move-hover: #eaf3ff;
  width: 100%;
  border: 1px solid #d8d8de;
  border-radius: 8px;
  box-shadow: none;
}

.lichess-viewer-shell .lpv--players.lpv--moves-auto {
  grid-template-columns: minmax(220px, min(42vh, 400px)) minmax(210px, 1fr);
}

.lichess-viewer-shell .lpv__player,
.lichess-viewer-shell .lpv__controls {
  min-height: 34px;
}

.lichess-viewer-shell .lpv__moves {
  font-size: 0.92rem;
}

.lichess-viewer-shell .lpv__controls .lpv__fbt {
  color: var(--text);
}

.lichess-viewer-shell .lpv__controls .lpv__fbt:hover {
  color: #ffffff;
  background: var(--accent);
}

.viewer-select select:focus {
  border-color: var(--accent);
}

.viewer-flip {
  display: inline-grid;
  place-items: center;
  min-width: 34px;
  padding-inline: 8px;
}

.viewer-error,
.move-empty {
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.viewer-error {
  margin-top: 0;
  padding: 10px;
  color: var(--danger);
  background: rgba(162, 59, 52, 0.08);
  border-radius: 8px;
}

.event-list {
  border-top: 1px solid var(--line);
}

.event-row {
  display: flex;
  gap: 9px;
  align-items: flex-start;
  justify-content: space-between;
  padding: 11px 0;
  border-bottom: 1px solid var(--line);
}

.event-copy {
  flex: 1 1 auto;
  min-width: 0;
}

.event-row strong {
  display: block;
  font-size: 13px;
}

.event-row span,
.event-row em {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.event-row em {
  color: var(--accent);
  font-style: normal;
  font-weight: 650;
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: 220px;
  color: var(--muted);
  text-align: center;
}

.empty-state.compact {
  min-height: 120px;
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

@keyframes growBar {
  from {
    transform: scaleX(0);
  }
  to {
    transform: scaleX(1);
  }
}

@keyframes viewerPulse {
  0%,
  100% {
    opacity: 0.45;
    transform: scale(0.75);
  }
  50% {
    opacity: 1;
    transform: scale(1);
  }
}

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

@media (max-width: 900px) {
  body {
    overflow: auto;
  }

  .app-shell {
    height: auto;
    min-height: 100dvh;
  }

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

  .workspace {
    grid-template-columns: 1fr;
    overflow: visible;
  }

  .content-pane,
  .detail-pane {
    max-height: none;
    overflow: visible;
  }
}

@media (max-width: 720px) {
  .topbar {
    padding: 12px;
  }

  .workspace {
    padding: 14px 12px 24px;
  }

  .rating-grid,
  .dashboard-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .leaderboard-grid,
  .result-list {
    grid-template-columns: 1fr;
  }

  .section-heading {
    align-items: flex-start;
    flex-direction: column;
  }

  .event-row {
    flex-direction: column;
  }

  .lichess-viewer-shell {
    max-height: none;
  }

  .lichess-viewer-shell .lpv--players.lpv--moves-auto {
    grid-template-areas:
      "player-top"
      "board"
      "player-bot"
      "controls"
      "side";
    grid-template-columns: minmax(200px, min(82vw, 400px));
    grid-template-rows: 2em auto 2em var(--controls-height) 7.5em;
  }
}

@media (max-width: 440px) {
  .rating-grid,
  .dashboard-grid {
    grid-template-columns: 1fr;
  }

  .leaderboard-card,
  .detail-pane {
    padding: 14px;
  }

  .stage-title {
    font-size: 26px;
  }
}
