.study-trainer {
  --text: var(--map-text);
  --muted: var(--map-muted);
  --line: var(--map-border);
  --line-strong: var(--map-border-strong);
  --accent: var(--map-accent);
  --study-progress-color: color-mix(in srgb, #4e83aa 72%, var(--map-text));
  --landing-map-bg: var(--map-bg);
  --landing-map-grid-strong: var(--map-border-strong);
}

.study-collection-shell {
  display: flex;
  align-items: center;
  width: min(680px, calc(100% - 48px));
  min-height: 100dvh;
  margin: 0 auto;
  padding: 72px 0;
  box-sizing: border-box;
}
.study-collection-header { display: flex; gap: 16px; }
.study-collection-shell p { margin: 0; }
.study-collection-shell a { color: var(--map-text); }
#study-error-back { display: inline-block; margin-top: 16px; }

.study-trainer-header { grid-template-columns: auto minmax(0, 1fr) minmax(0, 1fr); }
.study-trainer-header h1 { grid-column: 2; text-align: left; }
.study-practice-titles { display: contents; }
.study-progress-circle > .study-chapter-metrics[hidden] { display: none; }
.study-sidebar, .study-sidebar-content, .study-sidebar-toolbar { display: contents; }
#study-chapter-title { display: none; }
.study-chapter-picker { position: relative; min-width: 0; justify-self: stretch; }
#study-chapter-toggle {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  width: 100%;
  min-height: 36px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--map-muted);
  font: inherit;
  cursor: pointer;
}
#study-chapter-name { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#study-chapter-toggle img { flex: 0 0 16px; filter: var(--map-icon-filter); transform: rotate(90deg); }
#study-chapter-toggle:hover { color: var(--map-text); }
.study-chapters {
  position: absolute;
  z-index: 20;
  top: calc(100% + 6px);
  right: 0;
  width: min(380px, calc(100vw - 48px));
  max-height: min(480px, 60dvh);
  overflow: auto;
  padding: 6px;
  box-sizing: border-box;
  border: 1px solid var(--map-border-strong);
  background: var(--map-panel-solid);
}
.study-chapters button {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 10px;
  width: 100%;
  min-height: 40px;
  padding: 9px 10px;
  border: 0;
  background: transparent;
  color: var(--map-text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.study-chapters button:is(:hover, [aria-current="true"]) { background: var(--map-control-hover); }
.study-chapters button[aria-current="true"] { color: var(--map-accent); font-weight: 500; }
.study-chapter-progress {
  display: flex;
  grid-column: 1;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--map-muted);
  font-weight: 400;
}
.study-chapter-progress-track { flex: 1 1 auto; height: 4px; background: var(--map-border); }
.study-chapter-progress-fill { display: block; height: 100%; background: var(--map-accent); }
.study-chapter-progress-count { flex: 0 0 auto; font-size: 15px; font-variant-numeric: tabular-nums; }
.study-chapter-node, .study-chapter-metrics, #study-chapter-expand { display: none; }
.study-trainer :is(#study-back, #study-chapter-toggle, .study-chapters button):focus-visible { outline: none; }
#study-status { flex: 1 1 auto; min-height: 23px; color: var(--map-muted); }
#study-restart { display: none; }
#study-flip { display: none; }
#study-resume { display: none; }
.study-mode-line, .study-browsing-controls { display: none; }
.study-practice-controls { display: contents; }
.study-panel-resize { display: none; }
#study-repeat, #study-skip { display: grid; place-items: center; width: 36px; height: 36px; padding: 7px; }
.study-repeat-icon, .study-skip-icon {
  display: block;
  width: 20px;
  height: 20px;
  background: currentColor;
}
.study-repeat-icon { mask: url('/static/img/reload.svg') center / contain no-repeat; }
.study-skip-icon { mask: url('/static/img/player-skip-forward.svg') center / contain no-repeat; }

@media (min-width: 821px) {
  #study-practice {
    --study-panel-top: 88px;
    --study-panel-width: min(304px, calc((100vw - 280px) / 2 - 40px));
    --study-chapter-panel-width: var(--study-panel-width);
    --study-board-left-space: calc(var(--study-chapter-panel-width) + 40px);
    --study-board-right-space: 168px;
    --study-board-available-width: calc(100vw - var(--study-board-left-space) - var(--study-board-right-space));
    --study-board-vertical-space: calc(var(--study-panel-top) + 224px);
    --study-title-max-width: 560px;
    --study-chapter-node-size: 56px;
    --exam-board-size: min(var(--exam-board-preferred-size), max(280px, calc(100dvh - var(--study-board-vertical-space))), var(--study-board-available-width));
    width: var(--study-board-available-width);
    position: relative;
    left: calc((var(--study-board-left-space) - var(--study-board-right-space)) / 2);
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: max-content var(--exam-board-size) minmax(112px, 1fr);
    column-gap: 0;
  }
  #study-practice:has(#study-chapters[data-expanded="false"]) {
    --study-chapter-panel-width: 128px;
  }

  .study-trainer-header { display: contents; }
  .study-sidebar {
    position: fixed;
    z-index: 3;
    top: var(--study-panel-top);
    bottom: 64px;
    left: 16px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    width: var(--study-chapter-panel-width);
  }
  .study-sidebar-content {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 0;
    height: 100%;
    max-height: 100%;
    box-sizing: border-box;
    padding: 6px;
    border: 1px solid transparent;
  }
  .study-sidebar-content:has(.study-chapters[data-expanded="true"]) {
    border-color: var(--map-border-strong);
    background: var(--map-panel-solid);
  }
  .study-panel-resize {
    display: block;
    position: absolute;
    z-index: 1;
    top: -1px;
    bottom: -1px;
    width: 11px;
    cursor: ew-resize;
    touch-action: none;
    user-select: none;
  }
  .study-panel-resize::before {
    content: '';
    position: absolute;
    inset: 0 5px;
    background: transparent;
  }
  .study-panel-resize:is(:hover, [data-resizing="true"])::before { background: var(--map-accent); }
  .study-chapters-resize { display: none; right: -6px; }
  .study-sidebar-content:has(.study-chapters[data-expanded="true"]) > .study-chapters-resize { display: block; }
  :is(.study-sidebar-content, .study-moves-panel):has(.study-panel-resize[data-resizing="true"]) { user-select: none; }
  .study-chapter-picker { position: relative; display: flex; order: 2; flex: 1 1 auto; min-height: 0; width: 100%; }
  #study-back { display: flex; align-self: center; margin-bottom: 0; }
  .study-practice-titles {
    position: relative;
    display: block;
    grid-column: 1;
    grid-row: 1;
    align-self: start;
    justify-self: center;
    width: fit-content;
    max-width: min(var(--study-title-max-width), 100%);
    box-sizing: border-box;
    margin: var(--study-panel-top) 0 24px;
    padding: 14px 24px;
    background: color-mix(in srgb, var(--map-accent) 8%, transparent);
    text-align: center;
  }
  .study-practice-titles::before,
  .study-practice-titles::after {
    content: '';
    position: absolute;
    width: 14px;
    height: 14px;
    border-color: var(--map-border-strong);
    pointer-events: none;
  }
  .study-practice-titles::before {
    top: -5px;
    left: -5px;
    border-top: 1px solid var(--map-border-strong);
    border-left: 1px solid var(--map-border-strong);
  }
  .study-practice-titles::after {
    bottom: -5px;
    right: -5px;
    border-bottom: 1px solid var(--map-border-strong);
    border-right: 1px solid var(--map-border-strong);
  }
  #study-name {
    margin: 0;
    white-space: normal;
    overflow: visible;
    overflow-wrap: anywhere;
    text-wrap: balance;
    text-align: center;
    color: var(--map-text);
    font: 400 28px/1.15 var(--map-display-font);
  }
  #study-chapter-title {
    display: block;
    margin: 8px 0 0;
    white-space: normal;
    overflow: visible;
    overflow-wrap: anywhere;
    text-wrap: balance;
    text-align: center;
    color: var(--map-accent);
    font: 400 22px/1.2 var(--map-display-font);
  }
  #study-chapter-title:empty { display: none; }
  #study-chapter-toggle { display: none; }
  .study-mode-line {
    display: flex;
    grid-column: 1 / -1;
    grid-row: 2;
    align-items: center;
    justify-content: center;
    gap: 7px;
    width: 100%;
    min-width: 0;
    height: 28px;
    box-sizing: border-box;
    padding-top: 4px;
    justify-self: center;
    color: var(--map-good);
    font-size: 15px;
    line-height: 24px;
  }
  .study-mode-indicator {
    flex: 0 0 14px;
    width: 14px;
    height: 14px;
    background: currentColor;
    mask: url('/static/img/circle-filled.svg') center / contain no-repeat;
  }
  .study-mode-label { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-weight: 500; }
  #study-practice[data-inspection="true"] .study-mode-line { color: var(--study-progress-color); }
  #study-practice[data-inspection="true"] .study-mode-indicator { mask-image: url('/static/img/circle.svg'); }
  .study-mode-label .study-notation-piece { font-size: 1.4em; font-weight: 400; line-height: 0; vertical-align: baseline; }
  #study-board { grid-column: 1; grid-row: 2; width: var(--exam-board-size); justify-self: center; }
  #study-practice .paper-exam-action-row button:focus-visible { outline: 1px solid var(--map-accent); outline-offset: 2px; }
  .study-flip-icon {
    width: 20px;
    height: 20px;
    background: currentColor;
    mask: url('/static/img/arrows-up-down.svg') center / contain no-repeat;
    pointer-events: none;
  }
  #study-practice > .paper-exam-controls {
    grid-column: 1;
    grid-row: 3;
    width: var(--exam-board-size);
    justify-self: center;
    padding-bottom: 8px;
    container: study-controls / inline-size;
  }
  #study-practice .paper-exam-action-row {
    --study-action-row-height: 37px;
    display: grid;
    align-items: start;
    grid-template-columns: 36px minmax(0, 1fr) 36px;
    grid-template-rows: var(--study-action-row-height) 28px minmax(23px, max-content);
    width: 100%;
    min-width: 0;
    column-gap: 8px;
    row-gap: 4px;
  }
  #study-flip, #study-restart {
    display: grid;
    grid-row: 1;
    place-items: center;
    width: 36px;
    height: 36px;
    padding: 7px;
  }
  #study-flip { grid-column: 1; justify-self: start; }
  .study-practice-controls { display: flex; grid-column: 2; grid-row: 1; justify-self: center; gap: 8px; }
  .study-browsing-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
    gap: 8px;
  }
  .study-browsing-controls .paper-exam-action-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 36px;
    padding: 6px 4px;
    font-size: 15px;
    white-space: nowrap;
  }
  #study-resume { display: inline-flex; width: auto; height: auto; border-color: transparent; color: var(--map-muted); }
  #study-resume:disabled { color: var(--map-quiet); }
  .study-resume-icon, .study-back-to-practice-icon {
    display: block;
    flex: 0 0 20px;
    width: 20px;
    height: 20px;
    background: currentColor;
    pointer-events: none;
  }
  .study-resume-icon { mask: url('/static/img/arrow-narrow-right.svg') center / contain no-repeat; }
  .study-back-to-practice-icon { mask: url('/static/img/arrow-left.svg') center / contain no-repeat; }
  .study-restart-icon {
    width: 20px;
    height: 20px;
    background: currentColor;
    mask: url('/static/img/player-skip-forward.svg') center / contain no-repeat;
    transform: rotate(180deg);
  }
  #study-status { grid-column: 1 / -1; grid-row: 3; text-align: center; }

  @container study-controls (max-width: 408px) {
    #study-practice .paper-exam-action-row { --study-action-row-height: 82px; }
    .study-browsing-controls { flex-direction: column; align-items: center; }
  }

  .study-chapters {
    position: static;
    z-index: auto;
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    height: auto;
    max-height: none;
    overflow: hidden;
    padding: 0;
    border: 0;
    background: transparent;
  }
  .study-chapter-options {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: thin;
    scrollbar-gutter: stable;
    overscroll-behavior-y: contain;
  }
  .study-chapter-options button {
    position: relative;
    flex: 0 0 auto;
    min-height: 76px;
    grid-template-columns: var(--study-chapter-node-size) minmax(0, 1fr);
    align-items: center;
    align-content: center;
    column-gap: 18px;
    row-gap: 6px;
    padding: 4px 10px;
    border: 1px solid transparent;
    border-radius: 3px;
    line-height: 1.35;
    overflow-wrap: anywhere;
    isolation: isolate;
  }
  .study-chapter-options > button::before,
  .study-chapter-options > button::after {
    content: '';
    position: absolute;
    z-index: -1;
    left: 38px;
    width: 1px;
    height: calc(50% - 24px);
    background: var(--map-border-strong);
    pointer-events: none;
  }
  .study-chapter-options > button::before { top: -1px; }
  .study-chapter-options > button::after { bottom: -1px; }
  .study-chapter-options > button:first-child::before,
  .study-chapter-options > button:last-of-type::after { display: none; }
  .study-chapter-options button:is(:hover, [aria-current="true"]) { background: transparent; }
  .study-chapter-node {
    position: relative;
    display: grid;
    grid-column: 1;
    grid-row: 1 / 3;
    place-items: center;
    width: var(--study-chapter-node-size);
    height: var(--study-chapter-node-size);
    pointer-events: none;
  }
  .study-chapter-ring { width: 100%; height: 100%; overflow: visible; }
  .study-chapter-ring-track { fill: none; stroke: var(--map-border-strong); stroke-width: 2.5; }
  .study-chapter-ring-fill {
    fill: none;
    stroke: var(--study-progress-color);
    stroke-width: 2.6;
  }
  .study-chapter-center-track { fill: color-mix(in srgb, var(--map-border) 55%, var(--map-bg)); }
  .study-chapter-center-fill { fill: var(--map-good); }
  .study-chapter-options button[aria-current="true"] { color: var(--map-text); font-weight: 500; }
  .study-chapters[data-expanded="true"] .study-chapter-option:hover:not(:disabled) {
    border-color: var(--map-border-strong);
  }
  .study-chapters[data-expanded="true"] button[aria-current="true"] {
    border-color: var(--map-border-strong);
    background: var(--map-control-hover);
  }
  .study-chapter-label {
    grid-column: 2;
    grid-row: 1;
    align-self: center;
    visibility: hidden;
    color: var(--map-text);
    font-weight: 500;
    line-height: 1.2;
    pointer-events: none;
  }
  .study-chapters[data-expanded="true"] .study-chapter-label {
    visibility: visible;
  }
  .study-chapter-label .study-notation-piece {
    font-size: 1.4em;
    font-weight: 400;
    line-height: 0;
    vertical-align: baseline;
  }
  .study-chapter-progress { display: none; }
  .study-chapter-metrics {
    display: flex;
    grid-column: 2;
    grid-row: 2;
    align-self: start;
    align-items: center;
    gap: 10px;
    visibility: hidden;
    font-weight: 400;
    pointer-events: none;
  }
  .study-chapter-options > button > .study-chapter-node { grid-row: 1; pointer-events: auto; }
  .study-chapter-options .study-chapter-metrics { display: none; }
  .study-chapter-metric-progress { color: var(--study-progress-color); }
  .study-chapter-metric-separator { color: var(--map-muted); }
  .study-chapter-metric-mastery { color: var(--map-good); }
  .study-chapter-metric-value {
    font-size: 16px;
    line-height: 22px;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }
  .study-chapters[data-expanded="false"] :is(.study-chapter-label, .study-chapter-metrics) { display: none; }
  .study-chapters[data-expanded="false"] .study-chapter-options > button {
    grid-template-columns: var(--study-chapter-node-size);
    justify-content: center;
    gap: 0;
    padding: 4px;
  }
  .study-chapters[data-expanded="false"] .study-chapter-options > button::before,
  .study-chapters[data-expanded="false"] .study-chapter-options > button::after { left: calc(50% - 1px); }
  .study-chapters[data-expanded="false"] .study-chapter-options button {
    --study-node-corner-color: transparent;
    border-color: transparent;
    background: transparent;
  }
  .study-chapters[data-expanded="false"] button[aria-current="true"] {
    --study-node-corner-color: var(--map-muted);
  }
  .study-chapters[data-expanded="false"] button[aria-current="true"][data-scope="true"] {
    --study-node-corner-color: var(--map-accent);
  }
  .study-chapters[data-expanded="false"] .study-chapter-node { --study-node-corner-size: 8px; }
  .study-chapters[data-expanded="false"] .study-chapter-node::before {
    --study-node-corner-stroke: linear-gradient(var(--study-node-corner-color), var(--study-node-corner-color));
    content: '';
    position: absolute;
    inset: 0;
    background:
      var(--study-node-corner-stroke) left top / var(--study-node-corner-size) 1px no-repeat,
      var(--study-node-corner-stroke) left top / 1px var(--study-node-corner-size) no-repeat,
      var(--study-node-corner-stroke) right top / var(--study-node-corner-size) 1px no-repeat,
      var(--study-node-corner-stroke) right top / 1px var(--study-node-corner-size) no-repeat,
      var(--study-node-corner-stroke) left bottom / var(--study-node-corner-size) 1px no-repeat,
      var(--study-node-corner-stroke) left bottom / 1px var(--study-node-corner-size) no-repeat,
      var(--study-node-corner-stroke) right bottom / var(--study-node-corner-size) 1px no-repeat,
      var(--study-node-corner-stroke) right bottom / 1px var(--study-node-corner-size) no-repeat;
    pointer-events: none;
  }
  #study-chapter-expand {
    grid-column: 2;
    grid-row: 1;
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    margin: 0;
    padding: 7px;
  }
  #study-chapter-expand:focus-visible { outline: 1px solid var(--map-accent); outline-offset: 2px; }
  .study-chapter-expand-icon {
    width: 20px;
    height: 20px;
    background: currentColor;
    mask: url('/static/img/chevron-right.svg') center / contain no-repeat;
  }
  #study-chapter-expand[aria-expanded="true"] .study-chapter-expand-icon {
    mask-image: url('/static/img/x.svg');
  }
  .study-trainer .map-bottom-control-tooltip[data-progress-metrics="true"] {
    width: 310px;
    max-width: calc(100vw - 16px);
    box-sizing: border-box;
    padding: 12px 14px;
    border-radius: 3px;
    white-space: normal;
    text-align: left;
  }
  .study-trainer .map-bottom-control-tooltip[data-progress-metrics="true"] .study-chapter-metrics {
    display: grid;
    visibility: visible;
    gap: 6px;
  }
  .study-trainer .map-bottom-control-tooltip[data-progress-metrics="true"] .study-chapter-metric-separator { display: none; }
  .study-trainer .map-bottom-control-tooltip[data-progress-metrics="true"] [data-map-bottom-control-tooltip-label]:not([hidden]) {
    margin-bottom: 10px;
    line-height: 1.35;
  }
  .study-trainer .map-bottom-control-tooltip[data-progress-metrics="true"] .study-chapter-metric-value {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
  }
  .study-progress-metric-label {
    display: inline-flex;
    align-items: center;
    gap: 8px;
  }
  .study-progress-metric-label::before {
    content: '';
    width: 10px;
    height: 10px;
    box-sizing: border-box;
    border: 2px solid currentColor;
    border-radius: 50%;
  }
  .study-chapter-metric-mastery .study-progress-metric-label::before { background: currentColor; }
  .study-progress-explanation {
    display: block;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--map-border);
    color: var(--map-muted);
    font-size: 15px;
    line-height: 1.4;
  }
}

@media (min-width: 821px) and (prefers-reduced-motion: no-preference) {
  .study-sidebar-content, .study-moves-panel {
    transition: background-color 160ms ease, border-color 160ms ease;
  }
}
