:root {
  color-scheme: dark;
  --bg: #0d1117;
  --bg-soft: #101722;
  --panel: #161f2b;
  --panel-strong: #1c2735;
  --panel-hover: #223044;
  --border: rgba(148, 173, 205, 0.16);
  --border-strong: rgba(148, 173, 205, 0.3);
  --text: #e8eef7;
  --text-soft: #c3ccda;
  --muted: #8e9bad;
  --accent: #79b8ff;
  --accent-strong: #3d7dff;
  --accent-ink: #07101f;
  --green: #4cc38a;
  --red: #ff7171;
  --orange: #ffb454;
  --yellow: #f0c860;
  --violet: #c39bff;
  --radius: 16px;
  --radius-small: 10px;
  --shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
  --safe-top: 0px;
  --safe-right: 0px;
  --safe-bottom: 0px;
  --safe-left: 0px;
  --tg-height: 100dvh;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  min-height: var(--tg-height, 100dvh);
  margin: 0;
  background:
    radial-gradient(circle at 10% -10%, rgba(61, 125, 255, 0.16), transparent 45%),
    radial-gradient(circle at 110% 0%, rgba(76, 195, 138, 0.1), transparent 40%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

button,
input,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

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

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 16px;
  font-weight: 650;
  letter-spacing: 0.01em;
}

h2 {
  font-size: 16px;
  font-weight: 650;
}

h3 {
  font-size: 14px;
  font-weight: 650;
}

.muted {
  color: var(--muted);
  font-size: 13px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding:
    calc(10px + max(env(safe-area-inset-top, 0px), var(--safe-top)))
    calc(16px + max(env(safe-area-inset-right, 0px), var(--safe-right)))
    10px
    calc(16px + max(env(safe-area-inset-left, 0px), var(--safe-left)));
  background: rgba(13, 17, 23, 0.82);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

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

.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: linear-gradient(160deg, var(--panel-strong), #121a24);
  color: var(--accent);
  font-size: 25px;
  line-height: 1;
  padding-bottom: 2px;
}

.brand-text {
  min-width: 0;
}

.status-line {
  margin-top: 1px;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 8px;
}

.app-main {
  width: min(1240px, 100%);
  margin: 0 auto;
  padding:
    16px
    calc(16px + max(env(safe-area-inset-right, 0px), var(--safe-right)))
    calc(28px + max(env(safe-area-inset-bottom, 0px), var(--safe-bottom)))
    calc(16px + max(env(safe-area-inset-left, 0px), var(--safe-left)));
}

.view {
  animation: view-in 180ms ease-out;
}

@keyframes view-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.library-grid {
  display: grid;
  gap: 16px;
}

.library-side {
  display: grid;
  gap: 16px;
  align-content: start;
}

.card {
  position: relative;
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.025), transparent 45%), var(--panel);
  box-shadow: var(--shadow);
}

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

.card > h2 {
  margin-bottom: 8px;
}

.card > p.muted + .input,
.card > p.muted + textarea {
  margin-top: 10px;
}

.games-card {
  padding-bottom: 10px;
}

.game-list {
  display: grid;
  gap: 8px;
}

.game-card {
  display: block;
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.025);
  text-align: left;
  cursor: pointer;
  transition: border-color 140ms ease, background 140ms ease, transform 140ms ease;
}

.game-card:hover,
.game-card:focus-visible {
  border-color: var(--border-strong);
  background: var(--panel-hover);
}

.game-card:active {
  transform: scale(0.995);
}

.game-card-players {
  display: flex;
  align-items: baseline;
  gap: 7px;
  min-width: 0;
  font-weight: 600;
}

.game-card-white,
.game-card-black {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.game-card-dash {
  flex: 0 0 auto;
  color: var(--muted);
  font-weight: 400;
}

.game-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 5px 12px;
  margin-top: 6px;
  color: var(--muted);
  font-size: 12px;
}

.game-card-result {
  color: var(--text-soft);
  font-weight: 600;
}

.game-card-opening {
  display: block;
  margin-top: 4px;
  overflow: hidden;
  color: var(--text-soft);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.game-card-termination {
  color: var(--text-soft);
}

.game-subtitle {
  margin: -6px 2px 12px;
  font-size: 13px;
}

.empty-state {
  padding: 20px 12px;
  border: 1px dashed var(--border-strong);
  border-radius: 12px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.input {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-small);
  outline: none;
  background: #0d141e;
  color: var(--text);
  resize: vertical;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}

.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(121, 184, 255, 0.14);
}

.input::placeholder {
  color: #6f7d90;
}

.pgn-input {
  min-height: 150px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 13px;
  line-height: 1.5;
}

.file-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 9px 0 12px;
}

.file-label {
  cursor: pointer;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  cursor: pointer;
  transition: background 140ms ease, border-color 140ms ease, color 140ms ease, opacity 140ms ease;
  -webkit-tap-highlight-color: transparent;
}

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.btn-primary {
  background: linear-gradient(180deg, #8ac2ff, #5aa3ff);
  color: var(--accent-ink);
  box-shadow: 0 8px 20px rgba(61, 125, 255, 0.18);
}

.btn-primary:not(:disabled):hover {
  background: linear-gradient(180deg, #9dccff, #71b2ff);
}

.btn-ghost {
  border-color: var(--border-strong);
  background: rgba(255, 255, 255, 0.025);
  color: var(--text-soft);
}

.btn-ghost:not(:disabled):hover {
  border-color: rgba(255, 255, 255, 0.28);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
}

.btn-small {
  min-height: 32px;
  padding: 5px 10px;
  border-radius: 8px;
  font-size: 13px;
}

.btn:focus-visible,
.game-card:focus-visible,
.move-btn:focus-visible,
.moment:focus-visible,
.promo-option:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.inline-error {
  margin-top: 10px;
  padding: 9px 11px;
  border: 1px solid rgba(255, 113, 113, 0.35);
  border-radius: 9px;
  background: rgba(255, 113, 113, 0.09);
  color: #ffb0b0;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}

.alert {
  position: fixed;
  top: calc(68px + max(env(safe-area-inset-top, 0px), var(--safe-top)));
  left: 50%;
  z-index: 60;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  width: min(560px, calc(100% - 24px));
  padding: 11px 12px 11px 15px;
  border: 1px solid rgba(255, 113, 113, 0.42);
  border-radius: 12px;
  background: #2a1a20;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.45);
  color: #ffc7c7;
  font-size: 13px;
  transform: translateX(-50%);
}

.alert[data-kind="success"] {
  border-color: rgba(76, 195, 138, 0.45);
  background: #12261f;
  color: #b6f0d2;
}

.alert[data-kind="info"] {
  border-color: rgba(121, 184, 255, 0.45);
  background: #132030;
  color: #c6e1ff;
}

.alert-text {
  flex: 1 1 auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.alert-close {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: inherit;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.7;
}

.alert-close:hover {
  background: rgba(255, 255, 255, 0.08);
  opacity: 1;
}

.game-grid {
  display: grid;
  gap: 16px;
}

.board-column {
  display: grid;
  gap: 16px;
  min-width: 0;
  align-content: start;
}

.board-card {
  padding: 12px;
}

.game-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 2px 2px 12px;
}

.players {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 7px;
  min-width: 0;
  font-weight: 650;
}

.player-name {
  max-width: 42vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.vs {
  color: var(--muted);
  font-weight: 400;
}

.result-badge {
  flex: 0 0 auto;
  padding: 2px 7px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  color: var(--text-soft);
  font-size: 11px;
  font-weight: 600;
}

.board-frame {
  position: relative;
  width: 100%;
  max-width: min(100%, 72vh);
  max-width: min(100%, 72dvh);
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  overflow: hidden;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: #b58863;
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.32);
}

.board {
  width: 100%;
  height: 100%;
  touch-action: none;
}

.board.cg-wrap {
  width: 100%;
  height: 100%;
}

.board-overlay {
  position: absolute;
  inset: 0;
  z-index: 12;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 20px;
  background: rgba(8, 12, 18, 0.72);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

.spinner {
  display: inline-block;
  width: 22px;
  height: 22px;
  border: 2px solid rgba(255, 255, 255, 0.22);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 760ms linear infinite;
}

.spinner-small {
  width: 14px;
  height: 14px;
  border-width: 2px;
  vertical-align: -2px;
}

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

.nav-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 12px;
}

.nav-btn {
  width: 44px;
  min-height: 38px;
  padding: 4px;
  font-size: 14px;
}

.ply-label {
  min-width: 106px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.025);
  color: var(--text-soft);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.mode-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 7px 10px;
  margin-top: 10px;
  text-align: center;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 2px 9px;
  border: 1px solid rgba(121, 184, 255, 0.35);
  border-radius: 999px;
  background: rgba(121, 184, 255, 0.11);
  color: #b9dcff;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.badge.is-branch {
  border-color: rgba(195, 155, 255, 0.38);
  background: rgba(195, 155, 255, 0.12);
  color: #dfc9ff;
}

.mode-row .muted {
  max-width: 420px;
}

.moves-card {
  padding-bottom: 14px;
}

.moves {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 3px;
  max-height: 220px;
  overflow: auto;
}

.move-num {
  margin-left: 6px;
  color: #77859a;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.move-num:first-child {
  margin-left: 0;
}

.move-btn {
  min-width: 34px;
  min-height: 30px;
  padding: 4px 7px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--text-soft);
  font-size: 13px;
  font-weight: 550;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}

.move-btn:hover {
  border-color: var(--border);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
}

.move-btn.active {
  border-color: rgba(121, 184, 255, 0.45);
  background: rgba(121, 184, 255, 0.18);
  color: #d9ecff;
}

.move-btn.branch-origin {
  box-shadow: inset 0 -2px 0 rgba(195, 155, 255, 0.8);
}

.branch-block {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.branch-head {
  margin-bottom: 8px;
}

.branch-head h3 {
  color: var(--violet);
}

.review-status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
  min-height: 22px;
  color: var(--text-soft);
  font-size: 13px;
}

.review-status .muted {
  flex-basis: 100%;
}

.review-summary {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.025);
  color: var(--text-soft);
  font-size: 13px;
  white-space: pre-wrap;
}

.moments {
  display: grid;
  gap: 7px;
  margin-top: 11px;
}

.moment {
  position: relative;
  display: grid;
  gap: 2px;
  width: 100%;
  padding: 9px 12px 9px 15px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.025);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  overflow: hidden;
  transition: background 130ms ease, border-color 130ms ease;
}

.moment::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 3px;
  background: var(--muted);
}

.moment:hover {
  border-color: var(--border-strong);
  background: var(--panel-hover);
}

.moment-main {
  font-size: 13px;
  font-weight: 650;
}

.moment-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 11px;
}

.moment-cat {
  color: var(--text-soft);
  font-weight: 600;
}

.moment-best {
  color: var(--muted);
  font-size: 11px;
}

.category-best::before,
.category-good::before {
  background: var(--green);
}

.category-inaccuracy::before {
  background: var(--yellow);
}

.category-mistake::before {
  background: var(--orange);
}

.category-blunder::before {
  background: var(--red);
}

.category-missed_opportunity::before,
.category-missed_rescue::before {
  background: var(--violet);
}

.category-other::before {
  background: var(--muted);
}

.engine-status {
  min-height: 18px;
}

.engine-lines {
  display: grid;
  gap: 7px;
  margin-top: 9px;
}

.engine-line {
  display: grid;
  grid-template-columns: 62px minmax(0, 1fr);
  gap: 9px;
  align-items: start;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.022);
}

.engine-line.best {
  border-color: rgba(76, 195, 138, 0.28);
  background: rgba(76, 195, 138, 0.055);
}

.engine-eval {
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.engine-pv {
  color: var(--text-soft);
  font-size: 12px;
  line-height: 1.4;
  word-break: break-word;
}

.engine-pv .engine-rank {
  margin-right: 5px;
  color: var(--muted);
}

.chat-card {
  display: flex;
  flex-direction: column;
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 330px;
  min-height: 58px;
  overflow: auto;
  padding-right: 2px;
}

.chat-msg {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.025);
}

.chat-msg.user {
  align-self: flex-end;
  max-width: 88%;
  border-color: rgba(121, 184, 255, 0.26);
  background: rgba(121, 184, 255, 0.1);
}

.chat-msg.assistant {
  align-self: flex-start;
  max-width: 96%;
}

.chat-role {
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.chat-text {
  color: var(--text-soft);
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-time {
  color: #6d7a8d;
  font-size: 10px;
}

.chat-pending {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
}

.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-top: 11px;
}

.chat-input {
  flex: 1 1 auto;
  min-height: 44px;
  max-height: 140px;
}

.chat-form .btn {
  flex: 0 0 auto;
  min-height: 44px;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(4, 8, 14, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-box {
  width: min(360px, 100%);
  padding: 18px;
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
}

.modal-box h2 {
  margin-bottom: 13px;
}

.promo-options {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 13px;
}

.promo-option {
  display: grid;
  gap: 2px;
  place-items: center;
  padding: 10px 4px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.03);
  line-height: 1;
  cursor: pointer;
}

.promo-symbol {
  font-size: 27px;
}

.promo-label {
  color: var(--muted);
  font-size: 10px;
}

.promo-option:hover {
  border-color: var(--accent);
  background: rgba(121, 184, 255, 0.1);
}

@media (min-width: 760px) {
  .library-grid {
    grid-template-columns: minmax(0, 1.25fr) minmax(300px, 0.75fr);
    align-items: start;
  }

  .game-list {
    max-height: 60vh;
    overflow: auto;
    padding-right: 2px;
  }
}

@media (min-width: 960px) {
  .game-grid {
    grid-template-columns: minmax(0, 1.16fr) minmax(330px, 0.84fr);
    align-items: start;
  }

  .sidebar {
    position: sticky;
    top: calc(70px + max(env(safe-area-inset-top, 0px), var(--safe-top)));
    display: grid;
    gap: 16px;
    max-height: calc(100dvh - 86px - max(env(safe-area-inset-top, 0px), var(--safe-top)));
    overflow: auto;
    padding-right: 2px;
  }
}

@media (max-width: 559px) {
  .app-main {
    padding-top: 12px;
  }

  .card {
    padding: 13px;
  }

  .board-card {
    padding: 10px;
  }

  .player-name {
    max-width: 36vw;
  }

  .chat-log {
    max-height: 260px;
  }

  .alert {
    top: calc(62px + max(env(safe-area-inset-top, 0px), var(--safe-top)));
  }

  .mode-row .muted {
    flex-basis: 100%;
    order: 3;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
