:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --surface-2: #ece9e3;
  --text: #24221f;
  --muted: #6e6a63;
  --border: #dcd7ce;
  --primary: #3f7d3a;
  --primary-text: #fff;
  --accent: #2f6db3;
  --danger: #b33a2f;
  --warn: #b87a12;
  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --hl-last: rgba(155, 199, 0, 0.42);
  --hl-hint: rgba(40, 140, 255, 0.35);
  --hl-good: rgba(60, 170, 60, 0.55);
  --hl-bad: rgba(220, 50, 40, 0.5);
  --arrow-g: #15781b;
  --arrow-r: #c3281e;
  --arrow-b: #1f5fbf;
  --arrow-y: #d08a00;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1b19;
    --surface: #2a2724;
    --surface-2: #35312d;
    --text: #ebe7e1;
    --muted: #a39d94;
    --border: #45403a;
    --primary: #5a9e4f;
    --accent: #4f8fd8;
    --danger: #e0645a;
    --warn: #e0a640;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1d1b19;
  --surface: #2a2724;
  --surface-2: #35312d;
  --text: #ebe7e1;
  --muted: #a39d94;
  --border: #45403a;
  --primary: #5a9e4f;
  --accent: #4f8fd8;
  --danger: #e0645a;
  --warn: #e0a640;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* --- nagłówek */
header {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: .9rem; font-variant-caps: small-caps; white-space: nowrap; }
.brand img { width: 23px; height: 23px; }
nav { display: flex; gap: 2px; flex: 1; min-width: 0; overflow-x: auto; }
header nav button { background: none; border: 0; color: var(--muted); padding: 6px 8px; font-size: .8rem; font-variant-caps: small-caps; letter-spacing: .02em; white-space: nowrap; }
nav button.active { color: var(--text); background: var(--surface-2); }
header nav .lang-flag { font-size: 1.15rem; line-height: 1; padding: 4px 6px; font-variant-caps: normal; }
header nav .lang-flag::after { content: "▾"; font-size: .7rem; margin-left: 2px; color: var(--muted); vertical-align: middle; }
.lang-menu { position: fixed; z-index: 60; min-width: 180px; display: flex; flex-direction: column; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.lang-menu[hidden] { display: none; }
.lang-menu button { display: flex; align-items: center; justify-content: flex-start; gap: 10px; text-align: left; background: none; border: 0; padding: 10px 12px; border-radius: 8px; font-size: 1rem; color: var(--text); }
.lang-menu button:hover, .lang-menu button:focus-visible { background: var(--surface-2); }
.lang-menu button[aria-checked="true"] { font-weight: 700; }
.lang-menu .flag { font-size: 1.3rem; line-height: 1; }

#peg-btn { flex: none; min-width: 44px; padding: 6px 9px; white-space: nowrap; font-size: .85rem; font-weight: 700; }
#peg-btn .peg-board-icon { width: 20px; height: 20px; flex: none; border: 2px solid currentColor; border-radius: 2px; background: conic-gradient(currentColor 25%, transparent 0 50%, currentColor 0 75%, transparent 0) 0 0 / 8px 8px; }
#peg-btn[data-state="on"] .peg-board-icon { display: none; }
#peg-label { line-height: 1; }
#peg-btn[data-state="on"] { color: var(--primary); border-color: var(--primary); }
#peg-btn[data-state="connecting"], #peg-btn[data-state="lost"] { color: var(--warn); border-color: var(--warn); }
@media (max-width: 650px) { .brand-name { display: none; } }
@media (max-width: 400px) {
  header { gap: 4px; padding-inline: 6px; }
  .brand { display: none; }
  nav { gap: 0; }
  header nav button { padding-inline: 5px; font-size: .76rem; letter-spacing: 0; }
}

main { max-width: 1400px; margin: 0 auto; padding: 16px; }
.page-head { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head select { margin-left: auto; }

/* --- przyciski i formularze */
button, .button, select, input, textarea {
  font: inherit; color: inherit;
}
button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer;
}
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
button.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
button.ghost { background: transparent; }
button.danger { color: var(--danger); }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px;
}
.form label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; font-size: 0.9rem; color: var(--muted); }
.form label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text); font-size: 1rem; }
.form label.check input { width: 22px; height: 22px; }
.form h3 { margin-bottom: 8px; }
input:not([type=checkbox]):not([type=radio]), select, textarea {
  width: 100%; min-height: 44px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--bg);
}
textarea { font-family: ui-monospace, monospace; font-size: 0.9rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
@media (max-width: 600px) { .grid2 { grid-template-columns: 1fr; } }
fieldset.seg { border: 0; padding: 0; margin: 4px 0 12px; display: flex; gap: 8px; flex-wrap: wrap; }
fieldset.seg label { flex-direction: row; align-items: center; gap: 6px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px; margin: 0; color: var(--text); }
fieldset.seg label:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.ok-card { border-color: var(--primary); }
.bad-card { border-color: var(--danger); color: var(--danger); }

/* --- lista repertuarów */
.rep-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 16px; }
.rep-list .card { margin: 0; }
.rep-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.color-dot { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--muted); flex: none; align-self: center; }
.color-dot.w { background: #fff; }
.color-dot.b { background: #222; }
.bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-bottom: 6px; }
.bar span { display: block; height: 100%; background: var(--primary); }
.focus { margin-top: 10px; padding: 10px 12px; border-left: 3px solid var(--accent); background: var(--surface-2); border-radius: 10px; }
.focus-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.focus .row { margin-top: 8px; }
.focus-head .focus-del { margin-left: auto; padding: 4px 10px; }
.more-menu { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--border); }
.more-menu[hidden] { display: none; }
.add-card { min-height: 140px; border-style: dashed; color: var(--muted); font-size: 1.05rem; }
.empty { grid-column: 1 / -1; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); font-size: 0.8rem; font-weight: 600; }
.pill.ok { color: var(--primary); }
.pill.bad { color: var(--danger); }

/* --- widok z szachownicą */
.split { display: grid; grid-template-columns: minmax(0, auto) minmax(300px, 1fr); gap: 20px; align-items: start; }
.train-tools { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.train-options { display: flex; align-items: center; }
.board-wrap { width: min(calc(100vh - 110px), 64vw); max-width: 100%; }
@media (orientation: portrait), (max-width: 800px) {
  .split { grid-template-columns: 1fr; }
  .board-wrap { width: min(100%, calc(100vh - 340px)); min-width: min(100%, 320px); margin: 0 auto; }
}
.train-analysis { grid-column: 1 / -1; min-width: 0; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; padding-bottom: 16px; }
.train-analysis .db { overflow-x: auto; }
.train-analysis-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.train-explore-controls { display: flex; gap: 8px; flex-wrap: wrap; }
.train-analysis-head strong { margin-right: auto; }
.train-analysis-head button[aria-pressed="true"] { background: var(--surface-2); border-color: var(--accent); }
.split.analysis-open { height: calc(100dvh - var(--hdr, 61px) - 32px); grid-template-columns: auto minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr); gap: 12px 16px; }
.split.analysis-open .board-wrap { width: min(50vw - 24px, 46dvh); min-width: 0; margin: 0; }
.split.analysis-open .panel { min-width: 0; max-height: 46dvh; overflow-y: auto; }
.split.analysis-open .panel > * { flex-shrink: 0; }
.split.analysis-open .train-analysis { align-self: stretch; }
.split.analysis-open .train-analysis > * { flex-shrink: 0; }
@media (max-width: 550px) {
  .split.analysis-open .panel-head h2 { font-size: 1rem; }
  .split.analysis-open .status-box { padding: 10px; }
  .split.analysis-open .status-head { display: block; }
  .split.analysis-open .status { font-size: 1.15rem; }
  .split.analysis-open #t-hint { margin-top: 6px; }
}
@media (orientation: landscape) and (min-width: 801px) {
  .split.analysis-open { grid-template-rows: auto auto minmax(0, 1fr); }
  .split.analysis-open .board-wrap { grid-row: 2 / 4; width: min(50vw, calc(100dvh - var(--hdr, 61px) - 88px)); }
  .split.analysis-open .train-analysis { grid-column: 2; }
}
.panel { display: flex; flex-direction: column; gap: 12px; }
.panel-head { display: flex; flex-direction: column; gap: 2px; }
.panel-head .back { align-self: flex-start; margin-bottom: 4px; }
.status-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-height: 96px; }
.status-head { display: flex; align-items: flex-start; gap: 10px; }
.status { min-width: 0; flex: 1; font-size: 1.6rem; font-weight: 700; }
.status-head #t-hint { flex: none; padding: 8px 10px; font-size: .9rem; }
.status[data-tone="bad"] { color: var(--danger); }
.status[data-tone="good"] { color: var(--primary); }
.status[data-tone="warn"] { color: var(--warn); }
.status[data-tone="new"] { color: var(--accent); }
.sub { color: var(--muted); margin-top: 4px; }
.status-box #t-back-rep, .status-box #t-next { margin-top: 12px; }
.status-box #t-next { min-width: 160px; min-height: 48px; font-size: 1.1rem; }
/* po zakończeniu treningu przycisk zawsze widoczny, nawet gdy panel wychodzi poza ekran */
#t-back-rep:not([hidden]) {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 40;
  bottom: max(16px, env(safe-area-inset-bottom)); min-width: 260px; min-height: 56px; font-size: 1.15rem;
  box-shadow: 0 4px 18px rgba(0,0,0,.35);
}
.comment { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; font-style: italic; }
.comment:empty { display: none; }
.line { font-family: ui-monospace, monospace; font-size: 0.95rem; color: var(--muted); }
.progress { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.moves { display: flex; flex-direction: column; gap: 6px; }
.move-btn { justify-content: flex-start; gap: 10px; flex: 1; }
.move-row { display: flex; gap: 6px; }
.move-row .del { min-width: 44px; padding: 8px; color: var(--danger); }
.comment-edit { display: flex; flex-direction: column; gap: 4px; }
.comment-edit textarea { font-family: inherit; font-size: 1rem; }
.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100vw - 32px); padding: 10px 18px; border-radius: 10px; z-index: 50;
  background: var(--text); color: var(--bg); box-shadow: 0 4px 16px rgba(0,0,0,.25);
}

/* --- szachownica */
.board { position: relative; width: 100%; aspect-ratio: 1; user-select: none; touch-action: none; border-radius: 4px; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,.25); }
.board-grid { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); }
.board-pieces { position: absolute; inset: 0; pointer-events: none; }
.board-pieces .piece {
  position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%;
  transition: transform .28s cubic-bezier(.2,.7,.3,1), opacity .25s;
  will-change: transform;
}
.board-pieces.no-anim .piece { transition: none; }
.board-pieces .piece.appear, .board-pieces .piece.gone { opacity: 0; }
@media (prefers-reduced-motion: reduce) { .board-pieces .piece { transition: none; } }
.board-arrows { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.sq { position: relative; background-size: 100% 100%; background-repeat: no-repeat; }
.sq.light { background-color: var(--sq-light); }
.sq.dark { background-color: var(--sq-dark); }
.sq::before { content: ""; position: absolute; inset: 0; pointer-events: none; }
.sq.last::before { background: var(--hl-last); }
.sq.hint::before { background: var(--hl-hint); }
.sq.good::before { background: var(--hl-good); }
.sq.bad::before { background: var(--hl-bad); }
.sq.selected::before { background: rgba(20, 85, 30, 0.5); }
.sq.check::before { background: radial-gradient(ellipse at center, rgba(255,0,0,1) 0%, rgba(231,0,0,1) 25%, rgba(169,0,0,0) 89%); }
.sq.dest::after { content: ""; position: absolute; inset: 36%; border-radius: 50%; background: rgba(20, 85, 30, 0.45); }
.sq.dest.has-piece::after { inset: 0; border-radius: 0; background: radial-gradient(transparent 0 62%, rgba(20, 85, 30, 0.45) 63%); }
.sq.missing { box-shadow: inset 0 0 0 4px var(--arrow-b); }
.sq.extra { box-shadow: inset 0 0 0 4px var(--arrow-r); }
.sq[data-rank]:not([data-rank=""])::after { content: attr(data-rank); position: absolute; top: 2px; left: 3px; font-size: 11px; font-weight: 700; opacity: .7; color: #5a4632; }
.sq[data-file]:not([data-file=""]) { }
.sq.light[data-rank]:not([data-rank=""])::after { color: var(--sq-dark); }
.sq.dark[data-rank]:not([data-rank=""])::after { color: var(--sq-light); }
.sq.dest[data-rank]:not([data-rank=""])::after { content: ""; color: transparent; }
.board.interactive .sq.has-piece { cursor: pointer; }

/* --- statystyki */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.tile-v { font-size: 1.8rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile-k { color: var(--muted); font-size: 0.85rem; }
.chart { display: flex; align-items: flex-end; gap: 4px; height: 170px; margin-top: 12px; }
.chart .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; min-width: 0; }
.chart .b { width: 100%; max-width: 36px; background: var(--primary); border-radius: 4px 4px 0 0; min-height: 0; }
.chart .stack { width: 100%; max-width: 36px; display: flex; flex-direction: column; border-radius: 4px 4px 0 0; overflow: hidden; }
.chart .stack .b { max-width: none; border-radius: 0; }
.chart .b.bad { background: var(--danger); }
.chart .v { font-size: 0.75rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.chart .k { font-size: 0.7rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; margin-top: 4px; }
.legend { font-size: 0.8rem; font-weight: 400; color: var(--muted); margin-left: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; background: var(--primary); margin: 0 2px 0 8px; }
.legend i.bad { background: var(--danger); }
.weak { margin: 8px 0 0; padding-left: 22px; }
.weak li { margin-bottom: 8px; }

/* trening na całym ekranie bez rozpraszaczy */
body[data-view="train"] main { padding-top: 12px; }

/* --- okno łączenia z planszą */
dialog#peg-dialog {
  width: min(520px, calc(100vw - 32px)); border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); padding: 20px;
}
dialog#peg-dialog::backdrop { background: rgba(0,0,0,.45); }
.peg-log { margin: 12px 0 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
.peg-log li[data-tone="bad"] { color: var(--danger); }
.peg-log li[data-tone="muted"] { color: var(--muted); font-size: .8rem; word-break: break-all; }

/* --- edytor: mała szachownica w miejscu, przewijana lista ruchów */
.editor {
  display: grid; gap: 12px 16px;
  height: calc(100dvh - var(--hdr, 61px) - 32px);
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "board side" "scroll scroll";
}
.ed-board { grid-area: board; width: min(50vw - 24px, 46dvh); }
.ed-side { grid-area: side; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.ed-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ed-head h2 { font-size: 1.1rem; }
.ed-scroll { grid-area: scroll; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; padding-bottom: 16px; }
.nav-row { margin-top: 0; }
.nav-row button[aria-pressed="true"] { background: var(--surface-2); border-color: var(--accent); }
@media (orientation: landscape) and (min-width: 801px) {
  .editor { grid-template-columns: auto minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "board side" "board scroll"; }
  .ed-board { width: min(50vw, calc(100dvh - var(--hdr, 61px) - 32px)); }
}
.eval { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: .9rem; display: flex; flex-direction: column; gap: 4px; }
.eval-src { color: var(--muted); font-size: .78rem; }
.eval-line { display: flex; gap: 8px; align-items: baseline; text-align: left; justify-content: flex-start; min-height: 36px; padding: 4px 8px; border: 0; background: none; width: 100%; }
.eval-line:hover { background: var(--surface-2); }
.eval-score { font-weight: 700; font-variant-numeric: tabular-nums; min-width: 3.4em; }
.eval-pv { font-family: ui-monospace, monospace; font-size: .85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.eval-pv b { color: var(--text); }

/* --- baza partii Lichess */
.db { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; }
.db-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.small-seg { margin: 0; }
.small-seg label { padding: 4px 10px; font-size: .9rem; min-height: 36px; }
.db-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.db-table td { padding: 4px 6px; border-top: 1px solid var(--border); vertical-align: middle; }
.db-table tr[data-san] { cursor: pointer; }
.db-table tr[data-san]:hover { background: var(--surface-2); }
.db-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.db-table .san { font-weight: 700; white-space: nowrap; }
.wdl { display: flex; height: 16px; border-radius: 3px; overflow: hidden; min-width: 90px; font-size: .68rem; line-height: 16px; }
.wdl span { text-align: center; overflow: hidden; white-space: nowrap; }
.wdl .w { background: #f2f2f2; color: #333; }
.wdl .d { background: #a0a0a0; color: #fff; }
.wdl .b { background: #333; color: #eee; }
.db-total td { color: var(--muted); }

.coffee { text-align: center; margin: 24px 0 8px; }
.coffee a { color: var(--accent); font-weight: 600; }
