:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --ink: #1d1f1c;
  --muted: #6b6f68;
  --line: #e2ddd2;
  --accent: #2f5d46;
  --accent-ink: #ffffff;
  --light-sq: #ebe3d1;
  --dark-sq: #9b7b5a;
  --hl: rgba(214, 178, 52, 0.55);
  --top: #1f2a24;
  --top-ink: #f4f1ea;
  --res-w: #ffffff;
  --res-d: #a9a497;
  --res-b: #34382f;
  --bar-edge: #d6d0c3;
  --hover: #ece7dc;
  --tag-bg: #f3e6bf;
  --tag-ink: #5e4510;
  --gut: 16px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141714;
    --surface: #1e221e;
    --ink: #ece9e1;
    --muted: #9aa096;
    --line: #2f352f;
    --accent: #6fae8b;
    --accent-ink: #0f1410;
    --light-sq: #c9bfa8;
    --dark-sq: #7d6248;
    --top: #0e110e;
    --res-w: #ece9e1;
    --res-d: #8d8a80;
    --res-b: #0b0d0b;
    --bar-edge: #3d443d;
    --hover: #283028;
    --tag-bg: #3b3322;
    --tag-ink: #ead39a;
  }
}
* { box-sizing: border-box; }
html { background: var(--top); }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
button { font: inherit; color: inherit; }

/* Native-style press feedback instead of hover */
.chip, .games li button, .controls button, .strip .mv, .icon-btn {
  transition: transform .08s ease, background-color .08s ease, opacity .08s ease;
}
.chip:active, .games li button:active, .controls button:active, .seg button:active { transform: scale(.97); }
.icon-btn:active { opacity: .5; }

.top {
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--top); color: var(--top-ink);
}
/* Installed iPhone app: iOS blurs a band just below the status bar, so keep the titles clear of it */
:root.standalone .top { padding-top: calc(env(safe-area-inset-top) + 26px); }
.top-row { display: flex; align-items: center; gap: 8px; }
.top h1 { font-size: 18px; margin: 0; font-weight: 650; letter-spacing: .2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: none; border: 0; font-size: 30px; line-height: 1; padding: 0 8px 0 0; color: var(--top-ink); }
.back-btn { min-width: 36px; min-height: 44px; }
.logo { flex: none; width: 28px; height: 28px; border-radius: 7px; }
.logo[hidden] { display: none; }
.kicker { margin: 0 0 1px; font-size: 12px; opacity: .72; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.titles { flex: 1; min-width: 0; }
.subtitle { margin: 1px 0 0; font-size: 12px; opacity: .7; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hdr-result {
  flex: none; padding: 3px 10px; border-radius: 999px; font-size: 14px; font-weight: 700;
  font-variant-numeric: tabular-nums; background: rgba(255,255,255,.12); color: var(--top-ink);
}

main { background: var(--bg); }
main > section { max-width: 560px; margin: 0 auto; padding: 12px 16px 8px; }

/* Game screen: one fixed screen, nothing scrolls except the move strip sideways */
#game-view { height: 100%; display: flex; flex-direction: column; padding: 4px 12px 0; }
/* Board area takes the spare height; names stay attached above and below the board */
.board-wrap {
  flex: 1; min-height: 0; container-type: size;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.board-wrap > * { width: min(100cqw, calc(100cqh - 72px)); flex: none; }

/* ---------- Library: sticky filter chips over the game list ---------- */
.filter-bar {
  position: sticky; top: -1px; z-index: 2; display: flex; align-items: center; gap: 8px;
  margin: -13px -16px 0; padding: 11px 16px 10px; background: var(--bg); border-bottom: 1px solid var(--line);
  /* Sit 1px under the header and paint a little above it, so phones that round the header's height
     to a fraction of a pixel don't show list rows scrolling through a hairline gap */
  box-shadow: 0 -6px 0 var(--bg);
}
/* Phone: Filters button and Clear all on the first line, the chosen pills on one line below that scrolls
   sideways edge to edge */
.filter-bar { flex-wrap: wrap; }
.pills {
  order: 1; flex: 1 0 100%; display: flex; gap: 8px; margin: 0 -16px; padding: 0 16px;
  overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain; touch-action: pan-x;
}
.pills::-webkit-scrollbar { display: none; }
.pills:empty { display: none; }
.fbtn {
  flex: none; display: flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px;
  border: 1.5px solid var(--ink); border-radius: 999px; background: var(--surface); font-size: 15px; font-weight: 650;
}
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent); color: var(--accent-ink);
  font-size: 12px; font-weight: 700; display: flex; align-items: center; justify-content: center; font-variant-numeric: tabular-nums;
}
.clear-all {
  flex: none; margin-left: auto; min-height: 36px; padding: 0 2px 0 4px; border: 0; background: none;
  color: var(--accent); font-size: 15px; font-weight: 650; white-space: nowrap;
}
.fbtn:active, .clear-all:active { transform: scale(.97); }
.chip {
  flex: none; display: flex; align-items: center; gap: 6px; min-height: 36px;
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 14px; font-size: 15px;
}
.chip.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); padding-right: 4px; }
.chip.active span:first-child { max-width: 190px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .x { width: 26px; height: 26px; display: flex; align-items: center; justify-content: center; font-size: 13px; opacity: .85; }
/* Phone pills: the ✕ takes the pill's full height and a 44px-wide strip at its end, so it's easy to hit */
[data-pill] .x { flex: none; align-self: stretch; width: 44px; height: auto; margin: -7px -4px -7px -12px; font-size: 15px; border-radius: 0 999px 999px 0; }
[data-pill] .x:active { background: rgba(0, 0, 0, .15); }
.summary { display: flex; gap: 12px; align-items: center; padding: 14px 0 2px; }
.summary .mini-board { width: 64px; }
.summary-info { flex: 1; min-width: 0; display: grid; gap: 4px; }
.summary-name { font-size: 16px; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.summary .meta { font-size: 13px; font-variant-numeric: tabular-nums; }
.list-head { display: flex; justify-content: space-between; align-items: center; margin: 10px 2px 8px; }
.list-head .count { margin: 0; }
.link-btn { background: none; border: 0; padding: 8px 0; color: var(--accent); font-size: 14px; font-weight: 600; }
.link-btn:active { opacity: .5; }

.count { color: var(--muted); font-size: 13px; margin: 14px 2px 8px; }
.games { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.games li button {
  width: 100%; text-align: left; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
  display: grid; gap: 3px;
}
.games .names { font-weight: 600; }
.games .names .vs { color: var(--muted); font-weight: 400; }
.games .sub { color: var(--muted); font-size: 13px; }
.games .row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
/* Game rows: the final position beside the names; famous games carry their nickname */
.games li button.grow { display: flex; align-items: center; gap: 12px; padding: 10px 12px 10px 10px; }
.grow .mini-board { width: 56px; border-radius: 4px; }
.ginfo { flex: 1; min-width: 0; display: grid; gap: 3px; }
.tag {
  justify-self: start; max-width: 100%; padding: 1px 8px; border-radius: 999px; background: var(--tag-bg); color: var(--tag-ink);
  font-size: 12px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Famous games: a sideways row of cards above the unfiltered list */
.famous { margin: -2px calc(-1 * var(--gut)) 12px; }
.famous:not([hidden]) + .filter-bar { margin-top: 0; }
.famous h2 { margin: 4px var(--gut) 10px; font-size: 20px; font-weight: 750; letter-spacing: -.2px; }
.famous-list {
  list-style: none; margin: 0; padding: 0 var(--gut) 4px; display: flex; gap: 12px;
  overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: none;
  scroll-snap-type: x mandatory; scroll-padding: 0 var(--gut);
}
.famous-list::-webkit-scrollbar { display: none; }
.famous-list li { flex: none; scroll-snap-align: start; }
.fcard {
  width: 180px; height: 100%; padding: 8px 8px 10px; display: flex; flex-direction: column; gap: 3px; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  transition: transform .08s ease, background-color .08s ease;
}
.fcard:active { transform: scale(.98); }
.fcard .mini-board { width: 162px; border-radius: 8px; margin-bottom: 6px; }
.fname { font-size: 15px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fplayers, .fhook {
  font-size: 13px; line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.fhook { color: var(--muted); }

.result { font-variant-numeric: tabular-nums; font-size: 13px; font-weight: 600; white-space: nowrap; }
.empty { color: var(--muted); text-align: center; padding: 24px 0; }
/* Loading placeholders: card-shaped grey blocks with a soft shimmer */
.skel { list-style: none; border-radius: 14px; background: var(--line); }
.skel.mskel { height: 150px; }
.skel.oskel { height: 76px; border-radius: 12px; }
@media (prefers-reduced-motion: no-preference) {
  .skel { background: linear-gradient(100deg, var(--line) 30%, var(--surface) 50%, var(--line) 70%) 0 0 / 300% 100%; animation: shimmer 1.2s linear infinite; }
  @keyframes shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } }
}

.board {
  display: grid; grid-template-columns: repeat(8, 1fr);
  aspect-ratio: 1; border-radius: 6px; overflow: hidden;
  touch-action: none; user-select: none;   /* the board handles its own drags and swipes */
}
.sq { position: relative; }
.sq.l { background: var(--light-sq); }
.sq.d { background: var(--dark-sq); }
.sq.hl::before { content: ""; position: absolute; inset: 0; background: var(--hl); }
.sq piece { position: absolute; inset: 0; background-size: 100%; background-repeat: no-repeat; }
.sq .coord { position: absolute; font-size: 10px; font-weight: 600; opacity: .75; pointer-events: none; }
.sq .coord.file { right: 3px; bottom: 1px; }
.sq .coord.rank { left: 3px; top: 1px; }
.sq.l .coord { color: var(--dark-sq); }
.sq.d .coord { color: var(--light-sq); }

.players { flex: none; height: 36px; display: flex; align-items: center; justify-content: space-between; padding: 0 2px; font-size: 14px; }
.players .pl { flex: 1; display: flex; align-items: center; gap: 6px; min-width: 0; font-weight: 600; }
.pl .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl .side { color: var(--muted); font-weight: 400; font-size: 12px; flex: none; }
.pl .crown { flex: none; width: 18px; height: 18px; fill: #d4a017; stroke: #9a7410; stroke-width: 1; stroke-linejoin: round; }

/* Captured pieces and material lead, at the right end of each player row (like Chess.com) */
.pl .mat { flex: none; margin-left: auto; padding-left: 4px; display: flex; align-items: center; gap: 4px; }
.pl .caps { display: flex; align-items: center; }
.pl .caps piece {
  flex: none; display: block; width: 18px; height: 18px;
  background-size: 100%; background-repeat: no-repeat;
}
.pl .caps piece + piece { margin-left: 2px; }       /* small gap between piece types */
.pl .caps piece.same { margin-left: -10px; }        /* same type stacks, overlapping */
.pl .adv { color: var(--muted); font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
@media (prefers-color-scheme: dark) {
  .pl .caps piece.black { filter: drop-shadow(0 0 1px rgba(236, 233, 225, .75)); }
}

/* ---------- Results bar: white wins / draws / black wins ---------- */
.bar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; border: 1px solid var(--bar-edge); }
.bar.sm { height: 6px; }
.bar.lg { height: 10px; }
.bar.row { height: 18px; font-size: 10px; font-weight: 700; line-height: 16px; font-variant-numeric: tabular-nums; }
.bar > span { display: block; text-align: center; overflow: hidden; }
.bar .bw { background: var(--res-w); color: #1d1f1c; }
.bar .bd { background: var(--res-d); color: #1d1f1c; }
.bar .bb { background: var(--res-b); color: #ffffff; }
.legend { font-size: 11px; color: var(--muted); }
.meta { font-size: 12px; color: var(--muted); }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.chev { flex: none; color: var(--muted); font-size: 22px; line-height: 1; padding-left: 2px; }

/* Small board in the library summary */
.mini-board {
  flex: none; width: 76px; aspect-ratio: 1; display: grid; grid-template-columns: repeat(8, 1fr);
  border-radius: 6px; overflow: hidden;
}
.mini-board .sq { position: relative; }
.mini-board .sq.l { background: var(--light-sq); }
.mini-board .sq.d { background: var(--dark-sq); }
.mini-board .sq.hl::before { content: ""; position: absolute; inset: 0; background: var(--hl); }
.mini-board piece { position: absolute; inset: 0; background-size: 100%; background-repeat: no-repeat; }

/* Move strip: one swipeable row, current move kept in view */
.strip {
  flex: none; position: relative; display: flex; align-items: center; gap: 2px;
  overflow-x: auto; overflow-y: hidden; overscroll-behavior-x: contain; scrollbar-width: none;
  white-space: nowrap; padding: 6px 2px; margin: 2px 0 4px;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-variant-numeric: tabular-nums; touch-action: pan-x;
}
.strip::-webkit-scrollbar { display: none; }
.strip .num { flex: none; color: var(--muted); font-size: 14px; padding: 0 2px 0 8px; }
.strip .num:first-child { padding-left: 2px; }
.strip .mv { flex: none; position: relative; background: none; border: 0; padding: 6px 8px; border-radius: 7px; font-size: 15px; font-weight: 600; }
.strip .mv.cur { color: var(--accent-ink); }
/* The current move's highlight: one pill that glides from move to move (placed by app.js) */
.strip .cur-pill {
  position: absolute; left: 0; top: 0; border-radius: 7px; background: var(--accent); pointer-events: none;
  transition: transform .2s cubic-bezier(.2, .8, .2, 1), width .2s cubic-bezier(.2, .8, .2, 1), background-color .45s ease;
}
/* Check: the king's square pulses red once */
.sq .check-fx { position: absolute; inset: 0; pointer-events: none; background: radial-gradient(circle, rgba(225, 40, 30, .95) 0%, rgba(225, 40, 30, .55) 45%, rgba(225, 40, 30, 0) 75%); }
.strip .res { flex: none; color: var(--muted); font-weight: 700; padding: 0 12px 0 10px; }

/* Control bar pinned to the bottom, clear of the home indicator */
.controls {
  flex: none; display: grid; grid-template-columns: 1fr 1.5fr 1.5fr 1fr 1fr; gap: 8px;
  padding: 4px 0 calc(env(safe-area-inset-bottom) + 10px);
}
.controls button {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 0; font-size: 18px;
}
.controls button:active { background: var(--line); }

/* Long lists end clear of the home indicator and the back-to-top button */
#masters-view, #openings-view, #library-view { padding-bottom: calc(env(safe-area-inset-bottom) + 72px); }

/* ---------- Bottom sheet for picking a filter ---------- */
.sheet-scrim { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .38); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 560px; max-height: 82%; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 16px 16px 0 0; padding-bottom: env(safe-area-inset-bottom); position: relative;
}
/* Grab handle: drawers close with a swipe down */
.sheet::before { content: ""; position: absolute; top: 6px; left: 50%; width: 36px; height: 5px; margin-left: -18px; border-radius: 3px; background: var(--muted); opacity: .45; }
@media (prefers-reduced-motion: no-preference) {
  .sheet { animation: sheet-up .2s ease-out; }
  @keyframes sheet-up { from { transform: translateY(48px); opacity: .5; } }
  .sheet-scrim:not(.pop) { animation: scrim-in .2s ease-out; }   /* the backdrop fades in with the drawer */
  @keyframes scrim-in { from { background-color: rgba(0, 0, 0, 0); } }
  /* A ticked choice: the box pops and its check draws in */
  .fs-opt.just > .box { animation: tick-pop .22s cubic-bezier(.2, .8, .2, 1); }
  .fs-opt.just.on > .box.check::after { animation: tick-draw .2s ease-out .05s both; }
  @keyframes tick-pop { 0% { transform: scale(.7); } 60% { transform: scale(1.12); } }
  @keyframes tick-draw { from { clip-path: inset(0 100% 100% 0); } }
}
.sheet-head { flex: none; display: flex; justify-content: space-between; align-items: center; padding: 18px 16px 4px; }
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 650; }
.sheet-find { flex: none; display: flex; gap: 8px; align-items: center; padding: 6px 16px 10px; }
.sheet-find input {
  flex: 1; min-width: 0; font: inherit; font-size: 16px; color: var(--ink); user-select: text; -webkit-user-select: text;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
}
.seg { flex: none; display: flex; gap: 2px; padding: 2px; border-radius: 10px; background: var(--line); }
.seg button { min-height: 34px; border: 0; border-radius: 8px; background: none; padding: 0 10px; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.15); }
.sheet-list { list-style: none; margin: 0; padding: 0 0 8px; border-top: 1px solid var(--line); min-height: 0; }
.sheet-list button {
  width: 100%; min-height: 48px; display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 16px; background: none; border: 0; border-bottom: 1px solid var(--line); text-align: left; font-size: 16px;
}
.sheet-list button:active { background: var(--line); }
.sheet-list button.on { color: var(--accent); font-weight: 650; }
.sheet-list button.other { color: var(--muted); }
.sheet-list .lbl { flex: 1; min-width: 0; }
.sheet-list .n { flex: none; color: var(--muted); font-size: 14px; font-weight: 400; font-variant-numeric: tabular-nums; }

/* Score bar from a player's side: won / drew / lost */
.bar.score .sw { background: var(--accent); }
.bar.score .sd { background: var(--res-d); }
.bar.score .sl { background: #b5523b; }

/* ---------- Explore mode: the reader is playing their own moves in a game ----------
   A calm blue tint over the whole game screen (the board keeps its colours), faded in and out. */
:root.exploring {
  --bg: #e7edf5; --surface: #f8fafd; --line: #d2dbe8;
  --accent: #3a64a0; --accent-ink: #ffffff; --top: #26354d; --hover: #dde5f0;
}
@media (prefers-color-scheme: dark) {
  :root.exploring {
    --bg: #15213a; --surface: #1c2b47; --line: #2d3f60;
    --accent: #8db1e6; --accent-ink: #0b111a; --top: #101a30; --hover: #243656;
  }
}
html, body, main, .top, .strip, .strip .mv, .controls button, .resume {
  transition: background-color .45s ease, border-color .45s ease, color .45s ease, transform .08s ease;
}
@media (prefers-reduced-motion: reduce) {
  html, body, main, .top, .strip, .strip .mv, .controls button, .resume { transition: none; }
}
/* Pinned at the left of the move strip, so it can be tapped at any time */
.resume {
  flex: none; position: sticky; left: 0; z-index: 1; margin-right: 6px;
  border: 0; border-radius: 999px; padding: 7px 12px; font-size: 14px; font-weight: 700; white-space: nowrap;
  background: var(--accent); color: var(--accent-ink); box-shadow: 4px 0 8px var(--bg);
}
.resume:active { transform: scale(.96); }

/* Picking up a piece: its square, dots on legal targets, rings on captures */
.sq.sel::before { content: ""; position: absolute; inset: 0; background: rgba(58, 100, 160, .45); }
.sq.dest::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 30%; height: 30%; transform: translate(-50%, -50%);
  border-radius: 50%; background: rgba(20, 30, 45, .28); pointer-events: none; z-index: 2;
}
.sq.dest.cap::after { width: 92%; height: 92%; background: none; border: 4px solid rgba(20, 30, 45, .3); box-sizing: border-box; }
/* The piece under the finger while dragging */
.cg-wrap.ghost {
  position: fixed; z-index: 50; pointer-events: none; transform: translate(-50%, -60%) scale(1.25);
}
.ghost piece { position: absolute; inset: 0; background-size: 100%; background-repeat: no-repeat; }

/* ---------- With a mouse: hover feedback on everything clickable (the hand pointer comes from native.css) ---------- */
.thead { display: none; }
@media (hover: hover) and (pointer: fine) {
  .chip, .games li button, .controls button, .strip .mv, .sheet-list button, .seg button, .thead button {
    transition: background-color .12s ease, border-color .12s ease, color .12s ease;
  }
  .chip:hover { background: var(--hover); border-color: var(--muted); }
  .chip.active:hover { background: var(--accent); filter: brightness(1.1); }
  .chip .x { border-radius: 50%; }
  .chip .x:hover { background: rgba(255, 255, 255, .22); opacity: 1; }
  .games li button:hover { background: var(--hover); }
  .games li button:hover .name, .games li button:hover .names { color: var(--accent); }
  .controls button:hover { background: var(--hover); border-color: var(--muted); }
  .strip .mv:hover:not(.cur) { background: var(--hover); }
  .resume:hover { filter: brightness(1.1); }
  .sheet-list button:hover { background: var(--hover); }
  .seg button:hover:not([aria-pressed="true"]) { color: var(--ink); }
  .link-btn:hover { text-decoration: underline; }
  .icon-btn:hover { opacity: .7; }
  .board[data-turn="w"] piece.white, .board[data-turn="b"] piece.black { cursor: grab; }
  .sq.dest { cursor: pointer; }
}

/* ---------- Desktop layout (mouse and a wide screen): wrapping pills, popovers, a table, a side move list ---------- */
@media (hover: hover) and (pointer: fine) and (min-width: 900px) {
  main > #library-view, main > #masters-view, main > #openings-view { max-width: 1200px; padding: 12px 24px 8px; --gut: 24px; }
  .fcard:hover { background: var(--hover); }
  .fcard:hover .fname { color: var(--accent); }

  .filter-bar { align-items: flex-start; margin: -13px -24px 0; padding: 11px 24px 10px; border-bottom: 0; }
  .fbtn { display: none; }
  .filter-bar { flex-wrap: nowrap; }
  .pills { order: 0; flex: 1 1 auto; min-width: 0; flex-wrap: wrap; margin: 0; padding: 0; overflow: visible; touch-action: auto; }
  .clear-all { min-height: 32px; font-size: 14px; }
  .clear-all:hover { text-decoration: underline; }
  .chip { min-height: 32px; padding: 5px 12px; font-size: 14px; }
  .chip.active { padding-right: 4px; }
  .chip .x { width: 22px; height: 22px; }
  .summary { max-width: 560px; }
  #sort { display: none; }

  /* The game list as a table; the header row sticks under the pills */
  #library-view { --cols: 36px minmax(0, 1.3fr) minmax(0, 1.3fr) 64px 56px minmax(0, 2fr) minmax(0, 1.6fr); }
  .thead {
    display: grid; grid-template-columns: var(--cols); gap: 12px; padding: 0 12px;
    position: sticky; top: calc(var(--chips-h, 54px) - 1px); z-index: 1;
    background: var(--surface); border: 1px solid var(--line); border-radius: 10px 10px 0 0;
    box-shadow: 0 -8px 0 var(--bg);
  }
  .thead button {
    background: none; border: 0; padding: 10px 0; text-align: left; font-size: 12px; font-weight: 650;
    text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  }
  .thead button:hover { color: var(--ink); }
  .thead .arr { margin-left: 4px; color: var(--accent); }
  .games { gap: 0; border: 1px solid var(--line); border-top: 0; border-radius: 0 0 10px 10px; overflow: hidden; background: var(--surface); }
  .games li button.trow {
    display: grid; grid-template-columns: var(--cols); gap: 12px; align-items: center;
    border: 0; border-bottom: 1px solid var(--line); border-radius: 0; padding: 9px 12px; font-size: 14px;
  }
  .games li:last-child button.trow { border-bottom: 0; }
  .trow > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .trow .name { font-weight: 600; }
  .trow .mini-board { width: 36px; border-radius: 3px; }
  .trow .year { color: var(--muted); font-variant-numeric: tabular-nums; }
  .games .empty { background: var(--surface); }

  /* Filter choices as a popover under the chip, not a bottom sheet */
  .sheet-scrim.pop { background: transparent; display: block; }
  .sheet-scrim.pop .sheet {
    position: absolute; width: 340px; max-height: min(440px, 70vh); border-radius: 10px; padding-bottom: 0;
    background: var(--surface); border: 1px solid var(--line); box-shadow: 0 10px 30px rgba(0, 0, 0, .18); animation: none;
  }
  .sheet-scrim.pop .sheet-head, .sheet-scrim.pop .sheet::before { display: none; }
  .sheet-scrim.pop .sheet-find { padding: 8px; }
  .sheet-scrim.pop .sheet-find input { font-size: 15px; padding: 6px 9px; background: var(--bg); }
  .sheet-scrim.pop .seg button { min-height: 28px; }
  .sheet-scrim.pop .sheet-list { padding: 4px; border-top: 1px solid var(--line); }
  .sheet-scrim.pop .sheet-list button { min-height: 0; padding: 7px 10px; border: 0; border-radius: 6px; font-size: 15px; }
  .sheet-scrim.pop .sheet-list button:focus-visible { outline: none; background: var(--hover); }

  /* Game: board on the left, the full move list and controls on the right */
  main > #game-view {
    max-width: 1180px; display: grid; grid-template-columns: minmax(0, 1fr) 300px; grid-template-rows: minmax(0, 1fr) auto;
    gap: 12px 28px; padding: 16px 24px 20px;
  }
  .board-wrap { grid-row: 1 / 3; }
  .strip {
    display: grid; grid-template-columns: 40px 1fr 1fr; align-content: start; gap: 2px;
    overflow-x: hidden; overflow-y: auto; white-space: normal; touch-action: auto;
    margin: 0; padding: 6px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  }
  .strip .num, .strip .num:first-child { padding: 6px 6px 6px 0; }
  .strip .mv { text-align: left; font-size: 14px; }
  .strip .res { grid-column: 1 / -1; padding: 8px 6px; }
  .resume { grid-column: 1 / -1; position: sticky; top: 0; left: auto; margin: 0 0 6px; box-shadow: 0 4px 8px var(--surface); }
  .controls { padding: 0; }
  .controls button { padding: 9px 0; font-size: 16px; }
}

/* ---------- Home: tabs in the header ---------- */
.home-bar { display: grid; gap: 10px; padding-top: 10px; }
.tabs { position: relative; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 2px; padding: 3px; border-radius: 10px; background: rgba(255, 255, 255, .1); }
/* One pill slides under the chosen tab (--i is its index, set by app.js) */
.tabs::before {
  content: ""; position: absolute; top: 3px; bottom: 3px; left: 3px; width: calc((100% - 10px) / 3);
  border-radius: 8px; background: var(--top-ink); transform: translateX(calc(var(--i, 0) * (100% + 2px)));
}
.tabs[data-ready]::before { transition: transform .25s cubic-bezier(.2, .8, .2, 1); }
.tabs button {
  position: relative; min-height: 32px; border: 0; border-radius: 8px; background: none; font-size: 14px; font-weight: 650;
  color: rgba(244, 241, 234, .72); transition: color .2s ease;
}
.tabs button[aria-selected="true"] { color: var(--top); }
@media (prefers-reduced-motion: reduce) { .tabs[data-ready]::before, .tabs button { transition: none; } }

/* Screen changes: forward slides the new screen in from the right over the old one, which drifts left and
   dims; back is the reverse (see transition() in app.js) */
@media (prefers-reduced-motion: no-preference) {
  :root[data-nav]::view-transition-old(root), :root[data-nav]::view-transition-new(root) {
    animation-duration: .25s; animation-timing-function: cubic-bezier(.2, .8, .2, 1); animation-fill-mode: both;
  }
  :root[data-nav="forward"]::view-transition-old(root) { animation-name: nav-under-out; }
  :root[data-nav="forward"]::view-transition-new(root) { animation-name: nav-over-in; box-shadow: 0 0 24px rgba(0, 0, 0, .25); }
  :root[data-nav="back"]::view-transition-old(root) { animation-name: nav-over-out; z-index: 1; box-shadow: 0 0 24px rgba(0, 0, 0, .25); }
  :root[data-nav="back"]::view-transition-new(root) { animation-name: nav-under-in; }
  @keyframes nav-over-in { from { transform: translateX(100%); } }
  @keyframes nav-over-out { to { transform: translateX(100%); } }
  @keyframes nav-under-out { to { transform: translateX(-28%); filter: brightness(.82); } }
  @keyframes nav-under-in { from { transform: translateX(-28%); filter: brightness(.82); } }
}
.tabs button:active { opacity: .7; }

/* A master's photo in a circle; initials coloured by era when there is none */
.av {
  flex: none; width: 44px; height: 44px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 15px; font-weight: 700; letter-spacing: .3px;
}
.av { overflow: hidden; }
.av img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 20%; }
.av.lg { width: 72px; height: 72px; font-size: 22px; }
.av0 { background: #7a5c3e; } .av1 { background: #5b6b4a; } .av2 { background: #2f5d46; } .av3 { background: #3a5a78; }

/* Masters tab: a grid of cards */
#masters-view .count { margin-top: 2px; }
.masters-grid { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.mcard {
  width: 100%; height: 100%; display: flex; flex-direction: column; align-items: flex-start; gap: 6px; text-align: left;
  padding: 12px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface);
}
.mcard .bar { width: 100%; margin-top: auto; }
.mname { font-size: 15px; font-weight: 650; line-height: 1.25; }
.msub { font-size: 13px; color: var(--muted); }

/* Openings tab: first-move chips, then each opening with its position */
.openings-head { margin-top: 0; }
.openings-head .count { margin: 0; }
.openings-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
/* Each row draws a mini board, so rows off screen skip rendering and scrolling stays light */
.openings-list > li { content-visibility: auto; contain-intrinsic-size: auto 85px; }
.orow {
  width: 100%; display: flex; align-items: center; gap: 12px; text-align: left;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
}
.orow .mini-board, .srow .mini-board { width: 52px; border-radius: 4px; }
.oinfo, .si { flex: 1; min-width: 0; display: grid; gap: 2px; }
.oname { font-size: 16px; font-weight: 650; }
.omoves { font-size: 13px; color: var(--accent); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.osub { font-size: 13px; color: var(--muted); }
.mcard:active, .orow:active, .lrow:active, .person:active, .cta:active, .fs-row:active, .fs-opt:active { transform: scale(.98); }
.mcard, .orow, .lrow, .person, .cta { transition: transform .08s ease, background-color .08s ease; }

/* Master and opening pages: one screen; hero and record on top, lists scroll, the main button pinned below */
main > .detail { height: 100%; display: flex; flex-direction: column; padding-bottom: 0; }
.hero { flex: none; display: flex; align-items: center; gap: 14px; padding: 6px 0 12px; }
.hero-info { min-width: 0; display: grid; gap: 3px; }
.hero-name { font-size: 20px; font-weight: 700; line-height: 1.2; }
.hero .meta { font-size: 14px; }
.hero-board { width: 96px; }
.record { flex: none; display: grid; gap: 5px; padding-bottom: 6px; }
.record .legend { font-size: 13px; }
.detail-scroll { flex: 1; min-height: 0; margin: 0 -16px; padding: 0 16px 12px; border-top: 1px solid var(--line); }
.sec { margin: 16px 2px 8px; font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.lrows { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--surface); }
.lrows li + li { border-top: 1px solid var(--line); }
.lrow { width: 100%; min-height: 46px; display: flex; align-items: center; gap: 10px; padding: 8px 12px; border: 0; background: none; text-align: left; font-size: 15px; }
.lrow .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lrow .bar { width: 64px; flex: none; }
.lrow .n { flex: none; min-width: 30px; text-align: right; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.lrow.more { color: var(--accent); font-weight: 650; }
.srow .sub { font-size: 13px; color: var(--muted); }
.srow .nm { white-space: normal; font-weight: 600; }
.people { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
.people::-webkit-scrollbar { display: none; }
.person { flex: none; width: 76px; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 2px; border: 0; border-radius: 10px; background: none; }
.person .pn { font-size: 13px; font-weight: 650; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person .pc { font-size: 12px; color: var(--muted); }
.cta {
  flex: none; width: 100%; min-height: 50px; margin: 10px 0 calc(env(safe-area-inset-bottom) + 12px);
  border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-ink); font-size: 16px; font-weight: 650;
}

/* Phone filter sheet */
.sheet.fsheet { height: 92%; max-height: none; padding-bottom: 0; background: var(--surface); }
.fs-head { flex: none; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 18px 12px 8px; }
.fs-head h2 { margin: 0; font-size: 17px; font-weight: 650; }
.fs-left { justify-self: start; min-height: 44px; padding: 0 6px; border: 0; background: none; color: var(--accent); font-size: 16px; font-weight: 600; }
.fs-left.round {
  width: 40px; height: 40px; min-height: 0; padding: 0 0 3px; border: 1px solid var(--line); border-radius: 50%;
  color: var(--ink); font-size: 26px; line-height: 1; display: flex; align-items: center; justify-content: center;
}
.fsheet .sheet-find { padding: 0 16px 10px; }
.fsheet .sheet-find input { background: var(--bg); }
.fs-list { flex: 1; list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); min-height: 0; }
.fs-list li { border-bottom: 1px solid var(--line); }
.fs-row { width: 100%; min-height: 62px; display: flex; align-items: center; gap: 10px; padding: 8px 16px; border: 0; background: none; text-align: left; }
.fs-lab { flex: 1; min-width: 0; display: grid; gap: 2px; }
.fs-k { font-size: 17px; font-weight: 650; }
.fs-v { font-size: 14px; font-weight: 600; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fs-v.any { color: var(--muted); font-weight: 400; }
.fs-opt { width: 100%; min-height: 52px; display: flex; align-items: center; gap: 14px; padding: 8px 16px; border: 0; background: none; text-align: left; font-size: 17px; }
.fs-opt.other { color: var(--muted); }
.fs-name { flex: 1; min-width: 0; }
.fs-opt .n, .fs-list .empty { color: var(--muted); font-size: 15px; font-variant-numeric: tabular-nums; }
.box { flex: none; width: 22px; height: 22px; border: 1.5px solid var(--muted); border-radius: 5px; position: relative; }
.box.radio { border-radius: 50%; }
.on > .box { border-color: var(--accent); background: var(--accent); }
.on > .box.check::after {
  content: ""; position: absolute; left: 6px; top: 2px; width: 6px; height: 11px;
  border: solid var(--accent-ink); border-width: 0 2.5px 2.5px 0; transform: rotate(45deg);
}
.on > .box.radio::after { content: ""; position: absolute; inset: 5px; border-radius: 50%; background: var(--accent-ink); }
.sheet-list .box { width: 18px; height: 18px; }
.sheet-list .on > .box.check::after { left: 5px; top: 1px; width: 5px; height: 10px; }
.fs-foot {
  flex: none; display: grid; grid-template-columns: 1fr 2fr; gap: 12px;
  padding: 12px 16px calc(env(safe-area-inset-bottom) + 12px); border-top: 1px solid var(--line); background: var(--surface);
}
.btn { min-height: 50px; border-radius: 12px; font-size: 16px; font-weight: 650; }
.btn-line { border: 1.5px solid var(--ink); background: var(--surface); }
.btn-solid { border: 0; background: var(--top); color: var(--top-ink); }
@media (prefers-color-scheme: dark) { .btn-solid { background: var(--accent); color: var(--accent-ink); } }
.btn:active { transform: scale(.98); }

/* Desktop: wider home grids, hover feedback */
@media (hover: hover) and (pointer: fine) {
  .mcard:hover, .orow:hover, .lrow:hover, .person:hover { background: var(--hover); }
  .tabs button:hover:not([aria-selected="true"]) { color: var(--top-ink); }
}
@media (hover: hover) and (pointer: fine) and (min-width: 900px) {
  .home-bar { max-width: 1152px; margin: 0 auto; }
  .masters-grid { grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
  .openings-list { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); }
}

/* Back to top: floats over lists once they're scrolled down and fades in and out on every screen.
   High contrast: white arrow on black in light mode, black arrow on white in dark mode. */
.to-top {
  position: fixed; z-index: 10; right: 16px; bottom: calc(env(safe-area-inset-bottom) + 16px);
  width: 48px; height: 48px; border-radius: 50%; border: 0; display: flex; align-items: center; justify-content: center;
  background: #000; color: #fff; box-shadow: 0 4px 14px rgba(0, 0, 0, .3);
  opacity: 0; transform: translateY(12px) scale(.9); pointer-events: none;
  transition: opacity .25s ease, transform .25s ease; will-change: opacity, transform;
}
.to-top.show { opacity: 1; transform: none; pointer-events: auto; }
.to-top.show:active { transform: scale(.92); }
@media (prefers-reduced-motion: reduce) { .to-top { transition: none; } }
@media (prefers-color-scheme: dark) { .to-top { background: #fff; color: #000; box-shadow: 0 4px 14px rgba(0, 0, 0, .6); } }
@media (hover: hover) and (pointer: fine) { .to-top.show:hover { filter: brightness(1.15); } }
