:root {
  --bg: #111418;
  --panel: #1a1f26;
  --panel-2: #222832;
  --line: #2e3642;
  --text: #e8e4dc;
  --muted: #9aa3ad;
  --accent: #d9a441;
  --accent-ink: #1b1406;
  --best: #34d399;
  --good: #86c06c;
  --inaccuracy: #e9c46a;
  --mistake: #f4a261;
  --blunder: #ef4444;
  --tip: #60a5fa;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: #4a5566; background: #2a313c; }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover:not(:disabled) { background: #e8b553; }
button.link { background: none; border: none; color: var(--tip); padding: 4px 0; text-decoration: underline; }

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 18px; letter-spacing: 0.2px; display: flex; align-items: center; gap: 8px; }
.logo { color: var(--accent); font-size: 24px; }
.top nav { display: flex; gap: 8px; flex-wrap: wrap; }

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 390px;
}

#stage { position: relative; min-height: 0; overflow: hidden; }
#stage canvas { display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; }

#loading {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(17, 20, 24, 0.85); font-size: 18px; color: var(--muted);
}

#evalbar {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  width: 18px; height: min(60%, 420px);
  background: #2b2b2b; border: 1px solid #444; border-radius: 9px; overflow: hidden;
}
#evalbar .fill {
  position: absolute; left: 0; right: 0; bottom: 0; height: 50%;
  background: #ece6d8; transition: height 0.6s ease;
}
#evalbar .label {
  position: absolute; left: 24px; top: 50%; transform: translateY(-50%);
  white-space: nowrap; font-size: 12px; color: var(--muted);
}
#evalbar { overflow: visible; }
#evalbar .fill { border-radius: 0 0 8px 8px; }

#thinking {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%);
  background: rgba(26, 31, 38, 0.92); border: 1px solid var(--line);
  padding: 6px 14px; border-radius: 20px; font-size: 14px; color: var(--muted);
}
#thinking .dots::before { content: "♟"; display: inline-block; animation: bob 0.9s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-3px); } }

#toast {
  position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
  max-width: min(520px, calc(100% - 32px));
  background: rgba(26, 31, 38, 0.95); border: 1px solid var(--line); border-left: 4px solid var(--tip);
  padding: 10px 14px; border-radius: var(--radius); font-size: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

/* ---- side panel ---- */
#panel {
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  padding: 14px 16px 24px;
}
.status-row { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
#status { font-size: 17px; font-weight: 600; }
#status.check { color: var(--blunder); }
#material { color: var(--muted); font-size: 13px; text-align: right; }
.actions { display: flex; gap: 8px; margin: 12px 0; }
.actions button { flex: 1; padding: 9px; }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 18px 0 8px; }

#piece-card {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 12px; font-size: 14px;
}
#piece-card h4 { margin: 0 0 4px; font-size: 15px; display: flex; justify-content: space-between; }
#piece-card h4 small { color: var(--muted); font-weight: 400; }
#piece-card p { margin: 4px 0; }
#piece-card .tip { color: var(--muted); }

#feed { display: flex; flex-direction: column; gap: 8px; }
.msg {
  background: var(--panel-2); border: 1px solid var(--line); border-left: 4px solid var(--muted);
  border-radius: var(--radius); padding: 9px 12px; font-size: 14px;
}
.msg.old { opacity: 0.6; }
.msg .head { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 3px; }
.msg .head .san { margin-left: auto; color: var(--muted); font-weight: 500; font-size: 13px; }
.msg .aside { color: var(--muted); }
.msg .btns { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.msg .btns button { padding: 5px 10px; font-size: 13px; }
.msg.tip { border-left-color: var(--tip); }
.msg.warn { border-left-color: var(--mistake); }
.msg.best { border-left-color: var(--best); }
.msg.good { border-left-color: var(--good); }
.msg.inaccuracy { border-left-color: var(--inaccuracy); }
.msg.mistake { border-left-color: var(--mistake); }
.msg.blunder { border-left-color: var(--blunder); }

.badge {
  display: inline-grid; place-items: center; min-width: 24px; height: 20px; padding: 0 5px;
  border-radius: 10px; font-size: 12px; font-weight: 700; color: #111;
}
.badge.best { background: var(--best); }
.badge.good { background: var(--good); }
.badge.inaccuracy { background: var(--inaccuracy); }
.badge.mistake { background: var(--mistake); }
.badge.blunder { background: var(--blunder); color: #fff; }

details { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 10px; }
summary { cursor: pointer; font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
#settings label { display: flex; gap: 8px; align-items: flex-start; margin: 8px 0; font-size: 14px; cursor: pointer; }
#settings input { margin-top: 3px; accent-color: var(--accent); }

#moves, #review-moves {
  list-style: none; padding: 0; margin: 8px 0 0;
  display: grid; grid-template-columns: 34px 1fr 1fr; gap: 2px 6px; font-size: 14px;
}
#moves li, #review-moves li { display: contents; }
.mv-num { color: var(--muted); text-align: right; padding: 3px 0; }
.mv { padding: 3px 6px; border-radius: 6px; display: flex; align-items: center; gap: 6px; }
.mv .dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot.best { background: var(--best); } .dot.good { background: var(--good); }
.dot.inaccuracy { background: var(--inaccuracy); } .dot.mistake { background: var(--mistake); }
.dot.blunder { background: var(--blunder); }
#review-moves .mv { cursor: pointer; }
#review-moves .mv:hover { background: var(--panel-2); }
#review-moves .mv.current { background: #3a3220; outline: 1px solid var(--accent); }

/* ---- review ---- */
.review-head { display: flex; justify-content: space-between; align-items: center; }
.review-head h2 { margin: 0; font-size: 19px; }
#review-progress { margin: 12px 0; position: relative; height: 26px; background: var(--panel-2); border-radius: 13px; overflow: hidden; }
#review-progress .bar { position: absolute; inset: 0 auto 0 0; width: 0; background: #3b4a2f; transition: width 0.2s; }
#review-progress span { position: relative; display: block; text-align: center; line-height: 26px; font-size: 13px; }
#review-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0; }
.sum-card { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; font-size: 13px; }
.sum-card h4 { margin: 0 0 4px; font-size: 14px; }
.sum-card .acc { font-size: 24px; font-weight: 700; }
.sum-card .row { display: flex; justify-content: space-between; color: var(--muted); }
#review-graph { width: 100%; height: 110px; display: block; background: var(--panel-2); border-radius: var(--radius); cursor: pointer; }
.review-nav { display: flex; align-items: center; gap: 6px; margin: 10px 0; }
.review-nav button { flex: 0 0 auto; }
#review-pos { flex: 1; text-align: center; color: var(--muted); font-size: 13px; }
#review-note { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; min-height: 64px; font-size: 14px; }
#review-note .head { font-weight: 600; margin-bottom: 4px; display: flex; gap: 8px; align-items: center; }
#review-note .aside { color: var(--muted); }
#review-key { display: flex; flex-direction: column; gap: 6px; }
#review-key button { text-align: left; display: flex; gap: 8px; align-items: center; }
#review-key .none { color: var(--muted); font-size: 14px; }

/* ---- lessons ---- */
.lesson-steps { display: flex; align-items: center; gap: 6px; margin: 14px 0 10px; }
.lesson-steps span { width: 22px; height: 6px; border-radius: 3px; background: var(--line); }
.lesson-steps span.current { background: var(--accent); }
.lesson-steps span.done { background: var(--best); }
.lesson-steps { flex-wrap: wrap; }
.lesson-steps span.puzzle { width: 12px; }
.lesson-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 12px; }
.lesson-tabs button { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; font-weight: 600; text-align: left; }
.lesson-tabs button small { font-weight: 400; color: var(--muted); font-size: 12px; }
.lesson-tabs button.active { border-color: var(--accent); background: #3a3220; }
.lesson-text .aside { color: var(--muted); font-size: 14px; }
.lesson-feedback .aside { color: var(--muted); }
.lesson-steps em { margin-left: auto; font-style: normal; font-size: 12px; color: var(--muted); }
.lesson-text { font-size: 16px; line-height: 1.55; background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.lesson-feedback { margin-top: 10px; padding: 10px 14px; border-radius: var(--radius); border-left: 4px solid var(--tip); background: var(--panel-2); font-size: 14px; }
.lesson-feedback.good { border-left-color: var(--best); }
.lesson-feedback.warn { border-left-color: var(--mistake); }
#lesson-next button { flex: 1; padding: 11px; }
.piece-info { margin-top: 14px; background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; font-size: 14px; }
.piece-info h4 { margin: 0 0 4px; font-size: 15px; display: flex; justify-content: space-between; }
.piece-info h4 small { color: var(--muted); font-weight: 400; }
.piece-info p { margin: 4px 0; }
#dlg-lessons { width: min(560px, calc(100vw - 32px)); }
#lesson-list { display: flex; flex-direction: column; gap: 6px; max-height: min(60vh, 560px); overflow-y: auto; margin: 12px 0; }
.lesson-item { display: flex; align-items: center; gap: 12px; text-align: left; padding: 9px 12px; }
.lesson-item .l-icon { width: 30px; font-size: 22px; text-align: center; color: var(--accent); }
.lesson-item .l-text { flex: 1; }
.lesson-item .l-text b { display: block; }
.lesson-item .l-text small { color: var(--muted); }
.lesson-item .l-state { font-size: 12px; color: var(--muted); white-space: nowrap; }
.lesson-item.done .l-state { color: var(--best); font-weight: 600; }
.new-to-chess { margin: 0 0 10px; }

/* ---- dialogs ---- */
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
  border-radius: 14px; padding: 20px 22px; width: min(480px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog h2 { margin: 0 0 12px; font-size: 20px; }
fieldset { border: none; padding: 0; margin: 0 0 14px; }
legend { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin-bottom: 6px; }
.fine { color: var(--muted); font-size: 13px; margin: 6px 0 0; }
.seg { display: flex; gap: 6px; }
.seg label { flex: 1; }
.seg input { display: none; }
.seg span { display: block; text-align: center; padding: 8px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: var(--panel-2); }
.seg input:checked + span { border-color: var(--accent); background: #3a3220; }
#levels { display: flex; flex-direction: column; gap: 6px; }
#levels label { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: var(--panel-2); }
#levels label:has(input:checked) { border-color: var(--accent); background: #3a3220; }
#levels input { margin-top: 4px; accent-color: var(--accent); }
#levels b { display: block; }
#levels small { color: var(--muted); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.promo-choices { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 12px; }
.promo-choices button { font-size: 34px; padding: 8px 0; display: flex; flex-direction: column; align-items: center; }
.promo-choices button span { font-size: 12px; color: var(--muted); }
.promo-choices button[value="q"] { border-color: var(--accent); }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 58vh auto; }
  #panel { border-left: none; border-top: 1px solid var(--line); overflow: visible; }
  body { height: auto; min-height: 100%; }
  html, body { height: auto; }
  .top { padding: 8px 16px; }
  #evalbar { left: 8px; width: 10px; }
  #evalbar .label { display: none; }
}
