:root {
  --bg: #f5f3ef; --surface: #ffffff; --surface2: #efece6; --text: #1f2328; --muted: #6b6f76; --border: #ddd8cf;
  --accent: #2f6f4f; --accent-text: #fff; --good: #1f8a4c; --bad: #c0392b; --warn: #b7791f;
  --light-sq: #f0d9b5; --dark-sq: #b58863; --last: rgba(155, 199, 0, .41); --sel: rgba(20, 85, 30, .5);
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161512; --surface: #22201c; --surface2: #2b2924; --text: #e8e6e3; --muted: #9a968f; --border: #3a3732;
    --accent: #5fb47f; --accent-text: #0f1a13; --good: #5fcf8a; --bad: #ef6b5c; --warn: #e5b45a;
    --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: .2rem 0 .6rem; } h2 { font-size: 1.15rem; margin: 0 0 .6rem; } h3 { font-size: .95rem; margin: 1rem 0 .3rem; color: var(--muted); }
.muted { color: var(--muted); } .small { font-size: .85rem; }
.bad-text { color: var(--bad); }

.top { position: sticky; top: 0; z-index: 5; display: flex; gap: 1rem; align-items: center; padding: .5rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: .4rem; font-weight: 700; text-decoration: none; color: var(--text); }
.brand img { width: 28px; height: 28px; }
nav { display: flex; gap: .2rem; flex-wrap: wrap; }
nav a { padding: .35rem .6rem; border-radius: 6px; text-decoration: none; color: var(--text); font-size: .92rem; }
nav a:hover { background: var(--surface2); }
nav a.active { background: var(--accent); color: var(--accent-text); }
main { max-width: 1200px; margin: 0 auto; padding: 1rem; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 1rem 1.2rem; margin-bottom: 1rem; box-shadow: var(--shadow); }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.cards .card { margin: 0; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 1rem; }
.grid2 .card { margin-bottom: 1rem; }
.stat .label { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.big { font-size: 2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.meter { height: 8px; background: var(--surface2); border-radius: 4px; margin-top: .5rem; overflow: hidden; }
.meter span { display: block; height: 100%; background: var(--accent); }
.hero { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.streak { font-weight: 700; background: var(--surface); border: 1px solid var(--border); padding: .4rem .8rem; border-radius: 99px; }

.tasks { list-style: none; padding: 0; margin: 0; }
.tasks li { display: flex; align-items: center; gap: .6rem; padding: .5rem 0; border-bottom: 1px solid var(--border); }
.tasks li:last-child { border: 0; }
.tasks .tick { flex: none; width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--border); display: grid; place-items: center; font-size: .8rem; color: var(--accent-text); }
.tasks li.done .tick { background: var(--good); border-color: var(--good); }
.tasks li.done a { color: var(--muted); text-decoration: line-through; }
.tasks a { flex: 1; text-decoration: none; color: var(--text); }
.bars { list-style: none; padding: 0; } .bars li { display: grid; grid-template-columns: 130px 1fr 36px; gap: .5rem; align-items: center; margin: .3rem 0; }
.hbar { background: var(--surface2); height: 10px; border-radius: 5px; overflow: hidden; } .hbar i { display: block; height: 100%; background: var(--bad); }

.btn { display: inline-flex; align-items: center; gap: .3rem; padding: .45rem .85rem; border-radius: 7px; border: 1px solid var(--border); background: var(--surface2); color: var(--text); font: inherit; cursor: pointer; text-decoration: none; }
.btn:hover { filter: brightness(.97); } .btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.ghost { background: transparent; } .btn.danger { color: var(--bad); }
.btn.small { padding: .2rem .55rem; font-size: .85rem; }
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin: .5rem 0; }
label { display: inline-flex; flex-direction: column; gap: .2rem; font-size: .85rem; color: var(--muted); margin: .2rem .6rem .2rem 0; }
label.check { flex-direction: row; align-items: center; color: var(--text); font-size: .95rem; }
input, select { font: inherit; padding: .35rem .5rem; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); }
.pill { display: inline-block; padding: .05rem .5rem; border-radius: 99px; background: var(--surface2); font-size: .8rem; }
.pill.accent { background: var(--accent); color: var(--accent-text); } .pill.good { background: var(--good); color: #fff; }
.dot { display: inline-block; width: 8px; height: 8px; background: var(--warn); border-radius: 50%; }

.stage-head { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin-bottom: .3rem; } .stage-head h2 { margin: 0; }
.stage.current { border-color: var(--accent); border-width: 2px; }
.lessons li { margin: .25rem 0; } .lessons li.done a { color: var(--muted); }
.lesson { max-width: 760px; } .prose li { margin: .3rem 0; }

/* Trainer layout */
.trainer { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 1.2rem; align-items: start; }
@media (max-width: 900px) { .trainer { grid-template-columns: 1fr; } }
.board-wrap { width: 100%; max-width: min(78vh, 720px); }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 1rem; box-shadow: var(--shadow); }
.status { margin: .6rem 0; padding: .6rem .8rem; border-radius: 8px; background: var(--surface2); min-height: 2.4rem; }
.status.good { background: color-mix(in srgb, var(--good) 15%, var(--surface)); }
.status.bad { background: color-mix(in srgb, var(--bad) 15%, var(--surface)); }
.status.warn { background: color-mix(in srgb, var(--warn) 18%, var(--surface)); }
.info, .note { font-size: .9rem; margin: .4rem 0; } .note { color: var(--text); }
.coachbox { border: 2px solid var(--bad); border-radius: 8px; padding: .7rem; margin: .5rem 0; background: color-mix(in srgb, var(--bad) 8%, var(--surface)); }
.movelist { max-height: 180px; overflow: auto; font-family: ui-monospace, Consolas, monospace; font-size: .9rem; columns: 2; padding-left: 2rem; }
.checklist { font-size: .9rem; margin-top: .5rem; } .checklist summary { font-weight: 600; cursor: pointer; }
.lines ul, .drills { padding-left: 1.1rem; margin: 0; } .lines li, .drills li { margin: .2rem 0; }
.lines a.active, .drills a.active { font-weight: 700; }
.target { font-size: 3rem; font-weight: 800; text-align: center; margin: .5rem 0; font-variant-numeric: tabular-nums; }

/* Board */
.board { position: relative; width: 100%; aspect-ratio: 1; user-select: none; touch-action: none; border-radius: 4px; overflow: hidden; box-shadow: var(--shadow); }
.board-grid { display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); width: 100%; height: 100%; }
.sq { position: relative; }
.sq.light { background: var(--light-sq); } .sq.dark { background: var(--dark-sq); }
.sq.last::before, .sq.selected::before, .sq.check::before, .sq.mark-good::before, .sq.mark-bad::before, .sq.mark-hint::before { content: ""; position: absolute; inset: 0; }
.sq.last::before { background: var(--last); } .sq.selected::before { background: var(--sel); }
.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.mark-good::before { background: rgba(31, 138, 76, .55); } .sq.mark-bad::before { background: rgba(192, 57, 43, .55); }
.sq.mark-hint::before { box-shadow: inset 0 0 0 5px #2563eb; }
.sq.dest::after { content: ""; position: absolute; left: 36%; top: 36%; width: 28%; height: 28%; border-radius: 50%; background: rgba(20, 85, 30, .45); z-index: 2; pointer-events: none; }
.sq.dest-capture::after { left: 0; top: 0; width: 100%; height: 100%; background: radial-gradient(transparent 0%, transparent 79%, rgba(20, 85, 30, .45) 80%); }
.piece { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; cursor: grab; }
.piece.dragging { opacity: .3; }
.piece.ghost { position: fixed; z-index: 100; pointer-events: none; inset: auto; cursor: grabbing; }
.coord { position: absolute; font-size: clamp(8px, 1.3vw, 12px); font-weight: 700; z-index: 1; pointer-events: none; }
.coord.file { right: 3px; bottom: 1px; } .coord.rank { left: 3px; top: 1px; }
.sq.light .coord { color: var(--dark-sq); } .sq.dark .coord { color: var(--light-sq); }
.sqname { display: none; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); font: 700 clamp(10px, 1.8vw, 17px) ui-monospace, Consolas, monospace; z-index: 2; pointer-events: none; color: rgba(0,0,0,.55); }
.board.labels .sqname, body.show-labels .board:not(.nolabels) .sqname { display: block; }
.sq .piece ~ .sqname, .sq:has(.piece) .sqname { top: auto; bottom: 1px; left: auto; right: 3px; transform: none; font-size: clamp(8px, 1.1vw, 11px); background: rgba(255,255,255,.75); padding: 0 2px; border-radius: 3px; }
.sq.dark .sqname { color: rgba(255,255,255,.8); } .sq.dark:has(.piece) .sqname { color: rgba(0,0,0,.7); }
.board.labels .coord, body.show-labels .board:not(.nolabels) .coord { display: none; }
.labeltoggle { margin-left: auto; font-size: .85rem; }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin: .6rem 0; }
.choices .btn { justify-content: center; font: 700 1.15rem ui-monospace, Consolas, monospace; padding: .7rem; }
.choices .btn.right { background: var(--good); color: #fff; border-color: var(--good); }
.choices .btn.wrong { background: var(--bad); color: #fff; border-color: var(--bad); }
.tabs { display: flex; flex-wrap: wrap; gap: .3rem; margin-bottom: .6rem; }
.san { font: 700 1.6rem ui-monospace, Consolas, monospace; }
.say { color: var(--muted); font-size: .95rem; }
.legendtbl td { padding: .15rem .5rem; } .legendtbl td:first-child { font: 700 1.05rem ui-monospace, Consolas, monospace; white-space: nowrap; }
.legendtbl img { width: 26px; vertical-align: middle; }
.step-nav { display: flex; justify-content: space-between; align-items: center; margin-top: .6rem; }
.board-arrows { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }
.promo { position: absolute; inset: 0; z-index: 10; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; gap: 8px; }
.promo button { width: 18%; aspect-ratio: 1; border-radius: 50%; border: 0; background: #eee; cursor: pointer; }
.promo img { width: 90%; }

/* Charts & tables */
.chart { width: 100%; height: auto; }
.chart .bar.win { fill: var(--good); } .chart .bar.loss { fill: var(--bad); } .chart .bar.draw { fill: var(--muted); }
.chart .avg { fill: none; stroke: var(--text); stroke-width: 2; }
.chart .grid { stroke: var(--border); } .chart .axis { fill: var(--muted); font-size: 11px; }
.legend { display: flex; gap: 1rem; font-size: .8rem; color: var(--muted); flex-wrap: wrap; }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
.legend .sw.win { background: var(--good); } .legend .sw.loss { background: var(--bad); } .legend .sw.draw { background: var(--muted); }
.legend .ln { display: inline-block; width: 16px; height: 2px; background: var(--text); margin-right: 4px; vertical-align: 3px; }
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th, .tbl td { text-align: left; padding: .35rem .4rem; border-bottom: 1px solid var(--border); }
.tbl td.win { color: var(--good); } .tbl td.loss { color: var(--bad); }
.res { list-style: none; padding: 0; } .res li { display: grid; grid-template-columns: 230px 1fr; gap: 1rem; padding: .45rem 0; border-bottom: 1px solid var(--border); }
@media (max-width: 640px) { .res li { grid-template-columns: 1fr; gap: .1rem; } main { padding: .75rem 16px; } }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px); opacity: 0; background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 8px; transition: .25s; z-index: 200; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.good { background: var(--good); color: #fff; } .toast.bad { background: var(--bad); color: #fff; }

/* Sync card */
.syncnote { display: block; text-decoration: none; color: var(--text); border-color: var(--accent); }
.syncgrid { display: grid; grid-template-columns: 180px 1fr; gap: 1.2rem; align-items: start; }
.qr { background: #fff; border-radius: 8px; padding: 6px; line-height: 0; }
.qr svg { width: 100%; height: auto; }
.synckey { font: 600 1.05rem/1.4 ui-monospace, Consolas, monospace; letter-spacing: .02em; background: var(--surface2); padding: .4rem .6rem; border-radius: 6px; overflow-wrap: anywhere; }
@media (max-width: 560px) { .syncgrid { grid-template-columns: 1fr; } .qr { max-width: 200px; } }

/* Phone: one swipeable row of tabs, respect the notch when installed as an app */
.top { padding-top: max(.5rem, env(safe-area-inset-top)); padding-left: max(1rem, env(safe-area-inset-left)); padding-right: max(1rem, env(safe-area-inset-right)); }
@media (max-width: 700px) {
  .top { gap: .4rem .8rem; padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
  nav { order: 3; flex: 1 0 100%; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; margin: 0 -16px; padding: 0 16px .2rem; }
  nav::-webkit-scrollbar { display: none; }
  nav a { flex: none; padding: .45rem .7rem; }
  main { padding-bottom: max(1rem, env(safe-area-inset-bottom)); }
  .big { font-size: 1.6rem; }
}
