:root {
  --bg: #f4f1ec;
  --surface: #ffffff;
  --surface-2: #f8f6f2;
  --line: #e2ddd4;
  --text: #24211d;
  --muted: #6f6a62;
  --accent: #3b7a57;
  --accent-ink: #ffffff;
  --good: #2f8a4c;
  --good-bg: #e5f4e9;
  --bad: #b8402f;
  --bad-bg: #fbe9e5;
  --info: #8a6416;
  --info-bg: #fbf2dc;
  --lichess: #222;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 5%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161512;
    --surface: #211f1c;
    --surface-2: #2a2723;
    --line: #3a3631;
    --text: #e8e4dc;
    --muted: #9c968c;
    --accent: #5fae80;
    --accent-ink: #10130f;
    --good: #6cc68a;
    --good-bg: #1f3426;
    --bad: #ee8170;
    --bad-bg: #3d2420;
    --info: #e2bb62;
    --info-bg: #3a3020;
    --lichess: #000;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  overflow-x: clip;
}
h1, h2, h3 { line-height: 1.25; margin: 0 0 .4em; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }

[hidden] { display: none !important; }
.pill { font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 99px; background: var(--info-bg); color: var(--info); }
.note { background: var(--info-bg); color: var(--info) !important; padding: 10px 12px; border-radius: 8px; font-size: 14px; }

/* top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; border-bottom: 1px solid var(--line); background: var(--surface);
}
.brand { font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.brand-mark { color: var(--accent); font-size: 20px; }
.account { display: flex; align-items: center; gap: 8px; }
.user { font-weight: 600; }

/* buttons */
.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid transparent; border-radius: 8px;
  padding: 8px 14px; background: var(--accent); color: var(--accent-ink);
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { filter: brightness(1.06); }
.btn:disabled { opacity: .45; cursor: default; filter: none; }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-lg { padding: 12px 20px; font-size: 16px; }
.btn-lichess { background: var(--lichess); color: #fff; }
.icon-btn { border: 0; background: none; color: var(--muted); font-size: 16px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.link-btn { border: 0; background: none; color: var(--accent); cursor: pointer; font: inherit; font-size: 13px; padding: 0; text-decoration: underline; }
.link-btn.danger { color: var(--bad); }
kbd { font: 11px ui-monospace, monospace; opacity: .7; border: 1px solid currentColor; border-radius: 4px; padding: 0 4px; }
input, textarea {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%;
}
textarea { resize: vertical; font-family: ui-monospace, monospace; font-size: 13px; }

/* layout */
.layout {
  display: grid; grid-template-columns: minmax(280px, 360px) 1fr; gap: 20px;
  padding: 20px; max-width: 1400px; margin: 0 auto;
}
.library, .panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.library { padding: 16px; align-self: start; max-height: calc(100vh - 100px); overflow: auto; position: sticky; top: 20px; }
.trainer { display: grid; grid-template-columns: minmax(0, 600px) minmax(260px, 1fr); gap: 20px; align-items: start; }
.board-wrap { width: 100%; aspect-ratio: 1; }
#board { width: 100%; height: 100%; }
.panel { padding: 18px; min-height: 200px; }

@media (max-width: 1200px) {
  .trainer { grid-template-columns: 1fr; }
  .board-col { max-width: 600px; width: 100%; margin: 0 auto; }
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 16px; gap: 16px; }
  .library { position: static; max-height: none; }
  .topbar { padding: 10px 16px; }
}

/* library */
.welcome { padding: 8px 4px; }
.welcome h1 { font-size: 22px; }
.welcome p { color: var(--muted); }
.welcome .btn { margin: 6px 6px 0 0; }
.lib-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.lib-head h2 { font-size: 18px; margin: 0; }
.lib-tools { display: flex; gap: 8px; margin-bottom: 12px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; flex-shrink: 0; }
.seg button { font: inherit; font-size: 13px; border: 0; background: none; color: var(--muted); padding: 0 10px; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.busy { color: var(--info); font-weight: 600; }

.group { border-top: 1px solid var(--line); }
.group summary {
  display: flex; align-items: center; gap: 8px; padding: 10px 2px; cursor: pointer; list-style: none;
}
.group summary::-webkit-details-marker { display: none; }
.group summary::before { content: "▸"; color: var(--muted); font-size: 12px; transition: transform .15s; }
.group[open] summary::before { transform: rotate(90deg); }
.g-name { font-weight: 650; flex: 1; min-width: 0; }
.g-meta { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.chapters { list-style: none; margin: 0 0 10px; padding: 0; }
.chapter {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  font: inherit; color: inherit; background: none; border: 0; border-radius: 8px;
  padding: 7px 8px; cursor: pointer;
}
.chapter:hover { background: var(--surface-2); }
.chapter.active { background: var(--good-bg); }
.c-side { width: 12px; height: 12px; border-radius: 3px; border: 1px solid var(--muted); flex-shrink: 0; }
.c-side.white { background: #fff; }
.c-side.black { background: #222; }
.c-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.c-name { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-meta { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.bar { width: 44px; height: 5px; border-radius: 3px; background: var(--line); overflow: hidden; display: inline-block; }
.bar span { display: block; height: 100%; background: var(--good); }

/* panel */
.idle h2, .summary h2 { font-size: 19px; }
.big-stat { margin-top: 18px; display: flex; align-items: baseline; gap: 10px; }
.big-stat strong { font-size: 34px; color: var(--accent); }
.session-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.crumbs { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.ch-title { font-size: 20px; margin: 4px 0 2px; }
.progress-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin: 12px 0; }
.side-tag { padding: 1px 8px; border-radius: 99px; border: 1px solid var(--line); color: var(--text); }
.side-tag.black { background: #222; color: #eee; border-color: #222; }
.status { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.turn { font-weight: 700; font-size: 16px; }
.turn.opponent { color: var(--muted); font-weight: 600; }
.tries { display: flex; gap: 6px; }
.pip { width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--muted); }
.pip.on { background: var(--accent); border-color: var(--accent); }
.feedback { margin: 10px 0; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); font-weight: 600; min-height: 44px; }
.feedback.none { color: var(--muted); font-weight: 400; }
.feedback.good { background: var(--good-bg); color: var(--good); }
.feedback.bad { background: var(--bad-bg); color: var(--bad); animation: shake .3s; }
.feedback.info { background: var(--info-bg); color: var(--info); }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.moves {
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 14px; line-height: 1.9;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; max-height: 180px; overflow: auto;
}
.moves .num { color: var(--muted); }
.moves .mv { padding: 1px 3px; border-radius: 4px; }
.moves .mv.ok { color: var(--good); font-weight: 700; }
.moves .mv.shaky { color: var(--info); font-weight: 700; }
.moves .mv.fail { color: var(--bad); font-weight: 700; text-decoration: underline; }
.moves .mv.pending { background: var(--surface-2); color: var(--muted); }
.comment { margin-top: 10px; padding: 10px 12px; border-left: 3px solid var(--accent); background: var(--surface-2); border-radius: 0 8px 8px 0; font-size: 14px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }

/* dialog */
.dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 32px));
  background: var(--surface); color: var(--text);
}
.dialog::backdrop { background: rgb(0 0 0 / 45%); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.dialog-head h2 { margin: 0; font-size: 18px; }
.dialog-body { padding: 4px 18px 18px; max-height: 75vh; overflow: auto; }
.import-block { padding: 14px 0; border-bottom: 1px solid var(--line); }
.import-block:last-child { border-bottom: 0; }
.import-block h3 { font-size: 14px; }
.import-block p { margin: 0 0 8px; font-size: 13px; }
.row { display: flex; gap: 8px; align-items: center; margin-top: 8px; justify-content: space-between; }
.row input { flex: 1; }
.file-btn { cursor: pointer; }
.sources { list-style: none; padding: 0; margin: 0; }
.sources li { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; font-size: 14px; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 600;
  transition: .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* chessground board fills its wrapper */
cg-wrap, .cg-wrap { width: 100%; height: 100%; }
