.study-moves-rail, .study-moves-toggle, .study-moves-panel, #study-moves-header-measure { display: none; }

@media (min-width: 821px) {
  #study-practice {
    --study-moves-width: min(max(304px, 30vw), calc(100vw - var(--study-chapter-panel-width) - 360px));
  }
  #study-practice:has(#study-moves-panel:not([hidden])) {
    --study-board-right-space: calc(var(--study-moves-width) + 40px);
  }
  #study-practice:has(#study-moves-panel:not([hidden])) #study-chapter-title {
    display: none;
  }
  .study-moves-rail {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    position: fixed;
    top: var(--study-panel-top);
    right: 16px;
    width: 128px;
    box-sizing: border-box;
    padding: 6px;
    border: 1px solid transparent;
  }
  .study-moves-rail:has(#study-moves-toggle[hidden]) { visibility: hidden; }
  .study-moves-rail .study-panel-toolbar {
    grid-template-columns: minmax(0, 1fr);
    border-bottom: 0;
    justify-self: end;
    width: max-content;
    min-width: 100%;
    /* Grow into the unused gutter without changing the rail's layout reservation.
       Keep 24px clear of the board and 8px clear of the title's widest footprint.
       Subtract the 16px page edge and 7px rail inset from either allowance. */
    max-width: min(
      calc(var(--study-board-right-space) + (var(--study-board-available-width) - var(--exam-board-size)) / 2 - 47px),
      calc(var(--study-board-right-space) + (var(--study-board-available-width) - min(var(--study-title-max-width), var(--study-board-available-width))) / 2 - 31px)
    );
  }
  .study-moves-toggle {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    padding: 7px;
    margin: 0;
  }
  .study-moves-toggle { justify-self: center; }
  #study-moves-toggle .study-panel-expand-label {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .study-moves-expand-icon, .study-moves-collapse-icon, .study-moves-fold-all-icon, .study-moves-visibility-icon, .study-moves-heatmap-icon {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    background: currentColor;
  }
  .study-moves-expand-icon { mask: url('/static/img/chevron-left.svg') center / contain no-repeat; }
  .study-moves-collapse-icon { mask: url('/static/img/x.svg') center / contain no-repeat; }
  #study-moves-fold-all[data-action="expand"] .study-moves-fold-all-icon { mask: url('/static/img/circle-dashed-plus.svg') center / contain no-repeat; }
  #study-moves-fold-all[data-action="collapse"] .study-moves-fold-all-icon { mask: url('/static/img/circle-dashed-minus.svg') center / contain no-repeat; }
  #study-moves-visibility[data-action="show"] .study-moves-visibility-icon { mask: url('/static/img/eye-off.svg') center / contain no-repeat; }
  #study-moves-visibility[data-action="hide"] .study-moves-visibility-icon { mask: url('/static/img/eye.svg') center / contain no-repeat; }
  .study-moves-heatmap-icon { mask: url('/static/img/bolt.svg') center / contain no-repeat; }
  #study-moves-heatmap[data-active="true"] { color: var(--map-good); }
  .study-moves-panel {
    display: flex;
    flex-direction: column;
    position: fixed;
    z-index: 3;
    top: var(--study-panel-top);
    bottom: 64px;
    right: 16px;
    width: var(--study-moves-width);
    box-sizing: border-box;
    padding: 6px;
    gap: 8px;
    border: 1px solid var(--map-border-strong);
    background: var(--map-panel-solid);
    color: var(--map-text);
    text-align: left;
  }
  .study-moves-header {
    grid-template-columns: 36px minmax(0, 1fr) 36px;
    grid-template-rows: var(--study-panel-header-row-height, 28px) auto;
    border-bottom: 0;
  }
  .study-moves-title {
    position: relative;
    grid-column: 2;
    grid-row: 1;
    display: block;
    justify-self: center;
    width: fit-content;
    max-width: 100%;
    min-width: 0;
    padding: 0;
  }
  #study-moves-progress, .study-moves-progress-placeholder {
    position: absolute;
    right: calc(100% + 7px);
    top: 50%;
    transform: translateY(-50%);
    width: 28px;
    --study-chapter-node-size: 28px;
  }
  #study-moves-progress[hidden] { display: none; }
  .study-moves-panel .study-progress-circle { cursor: pointer; }
  .study-moves-header .study-panel-heading {
    min-width: 0;
    padding: 10px 0;
    color: var(--map-text);
    font-size: 20px;
    text-align: center;
    text-wrap: balance;
    overflow-wrap: anywhere;
  }
  /* Match the expanded header's inner width even while the panel is closed.
     Its natural text height sizes every header row without clipping long names. */
  #study-moves-header-measure {
    display: grid;
    position: fixed;
    visibility: hidden;
    pointer-events: none;
    top: var(--study-panel-top);
    right: 23px;
    width: calc(var(--study-moves-header-width) - 14px);
  }
  #study-moves-heading-measure { min-height: 1.2em; }
  #study-moves-close {
    grid-column: 3;
    grid-row: 1;
    align-self: start;
    justify-self: end;
    display: grid;
    place-items: center;
    width: 28px;
    height: 36px;
    padding: 8px 4px;
    margin: 0;
  }
  .study-moves-actions {
    grid-column: 2;
    grid-row: 2;
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    gap: 0;
    margin-top: -6px;
  }
  .study-moves-actions > .paper-exam-action-button {
    display: grid;
    place-items: center;
    width: 28px;
    height: 36px;
    padding: 8px 4px;
    margin: 0;
    border: 0;
  }
  .study-moves-content {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    padding: 2px 6px 10px;
    font-size: 16px;
    line-height: 1.8;
  }
  #study-moves-heading:focus { outline: none; }
  .study-moves-chapter-navigation {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    margin: 24px 3px 0;
  }
  .study-moves-chapter-navigation[hidden] { display: none; }
  .study-moves-chapter-navigation > button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 15px;
    line-height: 1.3;
  }
  .study-moves-chapter-navigation > button:focus-visible { outline: 1px solid var(--map-accent); outline-offset: 2px; }
  .study-moves-previous-icon, .study-moves-next-icon {
    width: 18px;
    height: 18px;
    background: currentColor;
  }
  .study-moves-previous-icon { mask: url('/static/img/arrow-left.svg') center / contain no-repeat; }
  .study-moves-next-icon { mask: url('/static/img/arrow-left.svg') center / contain no-repeat; transform: rotate(180deg); }
  .study-moves-line { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 2px; }
  .study-moves-comment {
    margin: 6px 3px 8px;
    color: var(--map-muted);
    font-size: 15px;
    line-height: 1.55;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }
  .study-comment-reference { color: var(--map-text); }
  .study-move {
    display: inline-flex;
    align-items: baseline;
    gap: 2px;
    padding: 1px 3px;
    border: 1px solid transparent;
    border-radius: 2px;
    background: transparent;
    color: inherit;
    font: inherit;
    line-height: 1.6;
    white-space: nowrap;
    cursor: pointer;
  }
  .study-move-number { color: var(--map-muted); margin-right: 1px; }
  .study-moves-content .study-notation-piece {
    font-size: 1.4em;
    font-weight: 400;
    line-height: 0;
    vertical-align: baseline;
  }
  .study-move-symbol { color: var(--map-accent); }
  .study-move-notation {
    position: relative;
    display: inline-flex;
    align-items: baseline;
    gap: 2px;
    white-space: nowrap;
  }
  /* Keep the original glyphs in layout so covering a move never reflows a line. */
  .study-move-notation[data-concealed="true"] > :not(.study-move-mask) { visibility: hidden; }
  .study-move-mask {
    position: absolute;
    inset-inline: 0;
    top: 50%;
    transform: translateY(-50%);
    height: .85em;
    box-sizing: border-box;
    border: 1px solid var(--map-border-strong);
    border-radius: 2px;
    background: color-mix(in srgb, var(--map-text) 12%, var(--map-panel-solid));
  }
  .study-move-mask[hidden] { display: none; }
  .study-move-notation[data-mastery="low"] { --study-move-mastery-color: var(--map-bad); }
  .study-move-notation[data-mastery="medium"] { --study-move-mastery-color: var(--map-key-moment-partial); }
  .study-move-notation[data-mastery="high"] { --study-move-mastery-color: var(--map-good); }
  .study-move-notation:is([data-mastery="low"], [data-mastery="medium"], [data-mastery="high"]) {
    border-radius: 2px;
    background: color-mix(in srgb, var(--study-move-mastery-color) 20%, transparent);
  }
  .study-move-notation[data-concealed="true"] { background: transparent; }
  .study-move-notation:is([data-mastery="low"], [data-mastery="medium"], [data-mastery="high"]) > .study-move-mask {
    background: color-mix(in srgb, var(--study-move-mastery-color) 27%, var(--map-panel-solid));
    border-color: color-mix(in srgb, var(--study-move-mastery-color) 55%, var(--map-border));
  }
  .study-move:hover { background: var(--map-control-hover); border-color: var(--map-border-strong); }
  .study-moves-content[data-selection-input="keyboard"] .study-move:hover:not([aria-current="step"]) {
    background: transparent;
    border-color: transparent;
  }
  .study-move[aria-current="step"] {
    background: color-mix(in srgb, var(--map-accent) 13%, transparent);
    color: var(--map-accent);
    border-color: var(--map-accent);
  }
  .study-move:disabled { cursor: default; opacity: .5; }
  .study-move:focus-visible,
  .study-moves-toggle:focus-visible, #study-moves-close:focus-visible, #study-moves-fold-all:focus-visible,
  #study-moves-visibility:focus-visible, #study-moves-heatmap:focus-visible {
    outline: 1px solid var(--map-accent);
    outline-offset: 2px;
  }
  .study-move[aria-current="step"]:focus-visible { outline: none; }
  .study-moves-variation {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 0 3px;
    margin: 5px 0 5px 3px;
    color: var(--map-muted);
  }
  .study-moves-variation[data-collapsible="true"]::before { display: none; }
  .study-moves-variation::before {
    content: '';
    position: absolute;
    left: 3px;
    top: calc(1lh + 2px);
    bottom: calc(1lh + 2px);
    width: 1px;
    background: var(--map-border);
    pointer-events: none;
  }
  .study-moves-variation-content {
    flex: 0 0 auto;
    min-width: 0;
    width: max-content;
    max-width: calc(100% - 11px);
  }
  /* A single move row can keep its closing bracket beside the last move.
     Wrapped rows and structural blocks leave the closing bracket on a new
     row in the opening bracket's column. The guide has no height on one row. */
  .study-moves-variation-content:not(:has(> .study-moves-line:only-child)) {
    width: calc(100% - 11px);
  }
  .study-moves-bracket {
    display: flex;
    flex: 0 0 8px;
    color: var(--map-border-strong);
  }
  .study-moves-bracket-glyph { flex: 0 0 8px; text-align: center; }
  .study-moves-variation-toggle,
  .study-moves-variation-guide {
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 2px;
    background: transparent;
    font: inherit;
    cursor: pointer;
  }
  .study-moves-variation-toggle {
    position: relative;
    align-items: center;
    min-height: 1lh;
  }
  .study-moves-variation-toggle::after {
    content: '';
    position: absolute;
    inset: 0 -3px;
  }
  .study-moves-variation-guide {
    position: absolute;
    left: -3px;
    top: calc(1lh + 2px);
    bottom: calc(1lh + 2px);
    width: 14px;
    min-height: 0;
    color: var(--map-border);
  }
  .study-moves-variation-guide::before {
    content: '';
    position: absolute;
    left: 6px;
    top: 0;
    bottom: 0;
    width: 1px;
    background: currentColor;
  }
  .study-moves-variation:has(> :is(.study-moves-variation-toggle, .study-moves-variation-guide):is(:hover, :focus-visible):not(:disabled))
    > :is(.study-moves-variation-toggle, .study-moves-variation-guide) { color: var(--map-accent); }
  .study-moves-variation-preview {
    /* Let a deeply nested summary wrap without squeezing its live move. */
    display: contents;
  }
  .study-moves-variation-summary {
    display: inline-flex;
    align-items: baseline;
    gap: 4px;
    margin: 0;
    padding: 1px 3px;
    border: 1px solid transparent;
    border-radius: 2px;
    background: transparent;
    color: var(--map-muted);
    font: inherit;
    font-size: 15px;
    line-height: 1.6;
    white-space: nowrap;
    cursor: pointer;
  }
  .study-moves-hidden-count { color: var(--map-muted); }
  .study-moves-variation-summary:hover:not(:disabled) {
    background: var(--map-control-hover);
    color: var(--map-text);
  }
  .study-moves-variation-summary:hover:not(:disabled) { border-color: var(--map-border-strong); }
  .study-moves-variation-toggle:focus-visible, .study-moves-variation-guide:focus-visible,
  .study-moves-variation-summary:focus-visible {
    outline: 1px solid var(--map-accent);
    outline-offset: 2px;
  }
  .study-moves-variation-toggle:disabled, .study-moves-variation-guide:disabled,
  .study-moves-variation-summary:disabled { cursor: default; opacity: .5; }
  .study-moves-variation-content[hidden], .study-moves-variation-preview[hidden],
  .study-moves-variation-guide[hidden] { display: none; }
  .study-moves-section { margin: 16px 0 4px; }
  .study-moves-section > h3 {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    width: fit-content;
    max-width: 100%;
    box-sizing: border-box;
    margin: 0 auto 9px;
    color: var(--map-muted);
    font: inherit;
    font-size: 15px;
    font-weight: 500;
    line-height: 1.5;
    text-align: center;
    text-wrap: balance;
    overflow-wrap: anywhere;
  }
  .study-moves-section[data-active="true"] > h3 { color: var(--map-accent); }
  .study-moves-section-label {
    min-width: 0;
    padding: 5px 10px;
    border: 1px solid var(--map-border-strong);
    background: color-mix(in srgb, var(--map-accent) 6%, transparent);
  }
  .study-moves-section[data-active="true"] > h3 > .study-moves-section-label { border-color: var(--map-accent); }
  .study-moves-section-progress {
    display: block;
    flex: 0 0 28px;
    --study-chapter-node-size: 28px;
  }
  .study-moves-section-progress[hidden] { display: none; }
  .study-moves-section .study-moves-section { margin-left: 12px; }
  .study-moves-resize { left: -6px; }
  .study-moves-toggle[hidden], .study-moves-panel[hidden] { display: none; }
}

@media (min-width: 821px) and (prefers-reduced-motion: no-preference) {
  @starting-style {
    .study-moves-panel {
      background-color: transparent;
      border-color: transparent;
    }
  }
}
