*, *::before, *::after { box-sizing: border-box; }

html { min-width: 320px; background: var(--background); }
body.grades-page {
  --grid: #e1e4ea;
  --grid-strong: #c4cbd8;
  --head: #f3f4f7;
  --stripe: #fafbfc;
  --average: #f2f6fd;
  --foot: #eaf0fb;
  --low: #b42318;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-width: 320px;
  margin: 0;
  overflow: hidden;
  background: var(--background);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

button, a { -webkit-tap-highlight-color: transparent; }
button { color: inherit; cursor: pointer; }
button:disabled, select:disabled { cursor: default; opacity: .5; }
a { color: inherit; }
h1, p { margin: 0; }
[hidden] { display: none !important; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 20000;
  padding: 10px 13px;
  border-radius: 5px;
  background: #fff;
  color: var(--blue);
  transform: translateY(-160%);
}
.skip-link:focus { transform: none; }
:is(button, a, select, input):focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }

.grades-main {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 16px 24px 20px;
}

/* ---------- Toolbar ---------- */
.grades-toolbar {
  display: flex;
  flex: none;
  align-items: center;
  gap: 16px;
}
.grades-help {
  flex: 1;
  min-width: 0;
  color: var(--muted);
}
body.grades-page .grades-help { font-size: 12px !important; line-height: 18px !important; }
.grades-help kbd {
  padding: 0 4px;
  border: 1px solid #c7ccd6;
  border-bottom-width: 2px;
  border-radius: 4px;
  background: #fff;
  font: inherit;
}
.grades-help strong { color: var(--ink); }
.grades-toolbar-end { display: flex; flex: none; align-items: center; gap: 8px; }
.grades-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid #c7ccd6;
  border-radius: 6px;
  background: #fff;
  text-decoration: none;
  white-space: nowrap;
  transition: background 150ms, border-color 150ms, transform 150ms;
}
.grades-button:hover:not(:disabled) { border-color: #8ea9d6; background: #eaf0fb; }
.grades-button:active:not(:disabled) { transform: translateY(1px); }
.grades-button svg { flex: none; width: 10px; height: 10px; }
.grades-button-primary { flex: none; border-color: var(--blue); background: var(--blue); color: #fff; }
.grades-button-primary:hover:not(:disabled) { border-color: #003da3; background: #003da3; }
.grades-icon-button { width: 40px; padding: 0; }
.grades-icon-button svg { width: 16px; height: 16px; }
.grades-button[aria-expanded="true"] { border-color: #8ea9d6; background: #eaf0fb; color: var(--blue); }
.grades-sort {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 4px 0 12px;
  border: 1px solid #c7ccd6;
  border-radius: 6px;
  background: #fff;
  color: var(--muted);
}
.grades-sort select {
  min-height: 36px;
  max-width: 230px;
  padding: 4px 6px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}
.grades-sort:focus-within { border-color: var(--blue); box-shadow: 0 0 0 2px #024bc833; }
.grades-sort select:focus-visible { outline: none; }
.grades-menu-wrap { position: relative; }

/* ---------- Popovers ---------- */
.grades-popover {
  z-index: 40;
  margin: 0;
  padding: 12px;
  border: 1px solid #d3dae7;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 12px 32px #152c552b, 0 2px 6px #152c5510;
}
.grades-stats-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 300px;
  min-width: 0;
}
.grades-stats-panel legend {
  float: left;
  width: 100%;
  margin-bottom: 6px;
  padding: 0 4px;
  color: var(--muted);
}
body.grades-page .grades-stats-panel legend { font-size: 11px !important; letter-spacing: .8px !important; text-transform: uppercase; }
.grades-stat-option {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 7px 6px;
  border-radius: 7px;
  cursor: pointer;
}
.grades-stat-option:hover { background: #f1f5fc; }
.grades-stat-option input { flex: none; width: 16px; height: 16px; margin: 2px 0 0; accent-color: var(--blue); cursor: pointer; }
.grades-stat-option span { display: grid; gap: 1px; min-width: 0; }
.grades-stat-option small { color: var(--muted); }
body.grades-page .grades-stat-option small { font-size: 11.5px !important; line-height: 16px !important; letter-spacing: .2px !important; }

.grades-eval-menu {
  position: fixed;
  display: grid;
  gap: 10px;
  width: 280px;
}
body.grades-page .grades-eval-menu-title {
  overflow: hidden;
  font-family: 'OffBit', monospace;
  font-size: 19px !important;
  font-weight: 700;
  line-height: 24px !important;
  letter-spacing: 0 !important;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.grades-eval-menu-date {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
}
.grades-eval-menu-date input {
  min-height: 36px;
  padding: 4px 8px;
  border: 1px solid #c7ccd6;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}
.grades-eval-menu-moves { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.grades-eval-menu button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 6px 8px;
  border: 1px solid #d6dbe5;
  border-radius: 6px;
  background: #f7f9fc;
}
.grades-eval-menu button:hover:not(:disabled) { border-color: #8ea9d6; background: #eaf0fb; }
.grades-eval-menu .grades-eval-menu-delete { border-color: #f0c7c1; background: #fff; color: #a1281a; }
.grades-eval-menu .grades-eval-menu-delete:hover { border-color: #e39d93; background: #fbe9e6; }
.grades-eval-menu-delete svg { width: 14px; height: 14px; }

/* ---------- Sheet ---------- */
.grades-sheet {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  border: 1px solid #d0d4dc;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 4px 18px #15243d08;
}
.grades-scroller {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
}
.grades-scroller:focus { outline: none; }

.grades-table {
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-variant-numeric: tabular-nums;
}
body.grades-page .grades-table :is(th, td) {
  height: 34px;
  padding: 0;
  border-right: 1px solid var(--grid);
  border-bottom: 1px solid var(--grid);
  background: #fff;
  font-weight: 400;
  text-align: center;
  vertical-align: middle;
  white-space: nowrap;
}
.grades-table .col-student {
  position: sticky;
  left: 0;
  z-index: 1;
  width: 230px;
  min-width: 230px;
  max-width: 230px;
  border-right-color: var(--grid-strong) !important;
  text-align: left !important;
}
.grades-table .col-eval { width: 104px; min-width: 104px; max-width: 104px; }
/* The add column takes the remaining width so the averages stay against the right edge. */
.grades-table .col-add { width: 100%; min-width: 56px; }
.grades-table .col-average {
  position: sticky;
  right: 0;
  z-index: 1;
  width: 108px;
  min-width: 108px;
  max-width: 108px;
  border-right: 0 !important;
  box-shadow: inset 1px 0 var(--grid-strong);
}

/* Frozen header */
.grades-table thead { position: sticky; top: 0; z-index: 3; }
body.grades-page .grades-table thead :is(th, td) { background: var(--head); }
body.grades-page .grades-table thead tr:last-child > * { border-bottom-color: var(--grid-strong); }
body.grades-page .grades-table .row-titles > * { height: 66px; vertical-align: top; }
.corner { padding: 10px 14px !important; }
.corner-label,
.row-label { color: var(--muted); }
body.grades-page .corner-label {
  display: block;
  font-size: 11px !important;
  font-weight: 700;
  letter-spacing: 1.2px !important;
  line-height: 16px !important;
  text-transform: uppercase;
}
body.grades-page .corner-count { display: block; margin-top: 2px; color: var(--muted); font-size: 12px !important; }
body.grades-page .grades-table .row-label { padding: 0 14px; overflow: hidden; font-size: 12px !important; text-overflow: ellipsis; }
/* Two lines, so most titles show in full in a narrow column. */
body.grades-page .eval-title {
  display: block;
  width: calc(100% - 8px);
  height: 38px;
  margin: 5px 4px 0;
  padding: 2px 4px;
  overflow: hidden;
  resize: none;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--ink);
  font-size: 13px !important;
  font-weight: 700;
  line-height: 16px !important;
  letter-spacing: .2px !important;
  white-space: normal;
  overflow-wrap: break-word;
}
.eval-title:hover { border-color: #d6dbe5; background: #fff; }
.eval-title:focus { border-color: var(--blue); background: #fff; outline: none; }
.eval-meta { display: flex; align-items: center; justify-content: space-between; gap: 2px; height: 20px; padding: 0 3px 0 9px; }
.eval-menu-button {
  display: grid;
  place-items: center;
  flex: none;
  width: 26px;
  height: 20px;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: #6b7280;
}
.eval-menu-button svg { width: 14px; height: 14px; }
.eval-menu-button:hover,
.eval-menu-button[aria-expanded="true"] { background: #e2e8f3; color: var(--blue); }
body.grades-page .eval-date {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px !important;
  line-height: 16px !important;
  text-align: left;
}
body.grades-page .eval-number {
  width: 100%;
  height: 33px;
  padding: 0 6px;
  border: 0;
  background: transparent;
  color: var(--ink);
  font-size: 13px !important;
  text-align: center;
}
.eval-number:hover { background: #fff; }
.eval-number:focus { background: #fff; outline: 2px solid var(--blue); outline-offset: -2px; }
body.grades-page .grades-table thead .is-active-col { background: #e3ebfa; }
body.grades-page .grades-table .row-titles .is-active-col { box-shadow: inset 0 3px var(--blue); }
.is-active-col .eval-title { color: var(--blue); }

.add-column-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 40px;
  height: 40px;
  margin: 11px 8px;
  padding: 0 10px;
  border: 1px dashed #a9b3c4;
  border-radius: 8px;
  background: transparent;
  color: var(--blue);
}
.add-column-button:hover { border-style: solid; background: #fff; }
.add-column-plus { font: 700 22px/1 'OffBit', monospace; }
.has-no-evaluation .add-column-button { min-height: 92px; margin-left: 14px; padding: 0 22px; }
body.grades-page .add-column-text { font-size: 14px !important; font-weight: 700; }
body.grades-page .grades-table thead .col-add { vertical-align: top; text-align: left; }

body.grades-page .grades-table thead .col-average { background: #e7eefa; }
.average-label { display: block; padding-top: 10px; font: 700 18px/1 'OffBit', monospace; color: var(--blue); }
body.grades-page .average-scale {
  display: block;
  margin-top: 4px;
  padding: 0 8px;
  color: var(--muted);
  font-size: 10.5px !important;
  line-height: 13px !important;
  letter-spacing: .2px !important;
  white-space: normal;
}

/* Students */
body.grades-page .grades-table tbody tr:nth-child(even) > * { background: var(--stripe); }
body.grades-page .grades-table tbody tr:hover > * { background: #f4f7fc; }
body.grades-page .grades-table tbody .col-student { padding: 0 12px 0 0; }
.student-number {
  display: inline-block;
  width: 36px;
  color: #9aa1ad;
  text-align: right;
  padding-right: 10px;
}
body.grades-page .student-number { font-size: 11px !important; }
body.grades-page .student-name { font-size: 14px !important; }
.student-name {
  display: inline-block;
  max-width: calc(100% - 36px);
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: bottom;
}
body.grades-page .grades-table tbody tr:focus-within > .col-student { background: #e8effc; }
body.grades-page .grades-table tbody tr:focus-within .student-name { color: var(--blue); font-weight: 700; }
body.grades-page .grade-input {
  display: block;
  width: 100%;
  height: 33px;
  padding: 0 6px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  font-size: 15px !important;
  letter-spacing: .3px !important;
  text-align: center;
}
.grade-input:focus { position: relative; background: #fff; outline: 2px solid var(--blue); outline-offset: -2px; }
body.grades-page .grade-input.is-low { color: var(--low); }
body.grades-page .grade-input.is-status { color: #7a818d; font-size: 13px !important; font-style: italic; }
body.grades-page :is(.grade-input, .eval-number).is-invalid { background: #fdecea !important; color: var(--low); outline: 2px solid var(--low) !important; outline-offset: -2px; }

body.grades-page .grades-table tbody .col-average { background: var(--average); }
body.grades-page .grades-table tbody tr:nth-child(even) > .col-average { background: #edf2fc; }
body.grades-page .grades-table .average-value { font-family: 'OffBit', monospace !important; font-size: 17px !important; font-weight: 700; letter-spacing: 0 !important; }
.average-value.is-low { color: var(--low); }
body.grades-page .grades-table .average-value.is-empty { color: #9aa1ad; font-weight: 400; }

/* Frozen statistics */
.grades-table tfoot { position: sticky; bottom: 0; z-index: 3; }
body.grades-page .grades-table tfoot :is(th, td) { height: 27px; background: var(--foot); }
body.grades-page .grades-table tfoot tr:first-child > * { border-top: 2px solid #9db9ea; }
body.grades-page .grades-table tfoot .row-label { color: #2c4f8f; font-weight: 700; cursor: help; }
body.grades-page .grades-table tfoot .col-average { background: #dde7f8; }
body.grades-page .grades-table .stat-value { color: #1d2f55; font-family: 'OffBit', monospace !important; font-size: 15px !important; font-weight: 700; letter-spacing: 0 !important; }

/* ---------- Empty state ---------- */
.grades-empty {
  display: grid;
  flex: 1;
  place-content: center;
  justify-items: center;
  gap: 10px;
  padding: 48px 24px;
  text-align: center;
}
.grades-empty-icon {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: #e9effb;
  color: var(--blue);
}
.grades-empty-icon svg { width: 26px; height: 26px; }
.grades-empty strong { font: 700 25px/1.1 'OffBit', monospace; }
.grades-empty p { max-width: 420px; color: var(--muted); }
.grades-empty .grades-button { margin-top: 6px; }

/* ---------- Toast ---------- */
.grades-toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: calc(100% - 32px);
  padding: 10px 12px 10px 16px;
  border-radius: 8px;
  background: #17243d;
  color: #fff;
  box-shadow: 0 8px 24px #17243d40;
  transform: translateX(-50%);
}
.grades-toast button {
  flex: none;
  padding: 6px 10px;
  border: 1px solid #ffffff55;
  border-radius: 5px;
  background: transparent;
  color: #fff;
  font-weight: 700;
}
.grades-toast button:hover { background: #ffffff1a; }
.grades-toast button:focus-visible { outline-color: #fff; }

@media (max-width: 1100px) {
  .grades-toolbar { flex-wrap: wrap; row-gap: 8px; }
  .grades-help { order: 3; flex-basis: 100%; }
  .grades-toolbar-end { margin-left: auto; }
}
@media (max-width: 760px) {
  .grades-main { gap: 10px; padding: 10px 10px 12px; }
  .grades-sheet { border-radius: 10px; }
  .grades-table .col-student { width: 136px; min-width: 136px; max-width: 136px; }
  .grades-table .col-eval { width: 84px; min-width: 84px; max-width: 84px; }
  .grades-table .col-average { width: 76px; min-width: 76px; max-width: 76px; }
  .student-number { width: 26px; padding-right: 6px; }
  .student-name { max-width: calc(100% - 26px); }
  .corner { padding: 8px 10px !important; white-space: normal !important; }
  body.grades-page .corner-label { letter-spacing: .6px !important; }
  body.grades-page .average-label { font-size: 15px; }
  body.grades-page .grades-table .row-label { padding: 0 10px; }
  body.grades-page .average-scale { display: none; }
  .has-no-evaluation .add-column-button { min-height: 60px; padding: 0 12px; }
}
@media (max-width: 560px) {
  .grades-toolbar-end { flex: 1 1 100%; margin-left: 0; }
  .grades-sort { flex: 1; min-width: 0; }
  .grades-sort > span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .grades-sort select { width: 100%; max-width: none; min-width: 0; }
  .grades-toast { right: 16px; left: 16px; justify-content: space-between; transform: none; max-width: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; }
}
@media print {
  @page { size: landscape; margin: 12mm; }
  body.grades-page { display: block; height: auto; overflow: visible; background: #fff; }
  .compact-header, .grades-toolbar, .grades-toast, .grades-popover, .eval-menu-button,
  .grades-table .col-add { display: none !important; }
  .grades-main { padding: 0; }
  .grades-sheet { display: block; overflow: visible; border: 0; box-shadow: none; }
  .grades-scroller { overflow: visible; }
  .grades-table { min-width: 0; }
  .grades-table thead, .grades-table tfoot, .grades-table .col-student, .grades-table .col-average { position: static; }
  body.grades-page .grades-table :is(th, td) { border-color: #b9bec8; }
  .grades-table tbody tr { break-inside: avoid; }
}
