/*
 * Theme tokens.
 * Uses the site's variables when present (--primary, --surface, --text, ...),
 * with fallbacks for sites that do not define them. html[data-theme="dark"]
 * switches the fallbacks; the site's own dark variables take priority.
 */

.grade-checker,
.grade-print-root {
  --gc-primary: var(--primary, #0d6efd);
  --gc-primary-hover: var(--primary-hover, #0b5ed7);
  --gc-primary-soft: var(--primary-soft, #e7f0ff);
  --gc-surface: var(--surface, #fff);
  --gc-surface-alt: var(--surface-alt, #fafbfc);
  --gc-text: var(--text, #263238);
  --gc-muted: var(--text-muted, #6b7280);
  --gc-border: var(--border, #dfe3e8);
  --gc-danger-bg: #f8d7da;
  --gc-danger-border: #f1aeb5;
  --gc-danger-text: #842029;
  --gc-note-bg: #fff8f8;
  --gc-note-border: #f1d5d8;
  --gc-radius: 12px;
  width: 100%;
  box-sizing: border-box;
}

html[data-theme="dark"] .grade-checker,
html[data-theme="dark"] .grade-print-root {
  --gc-primary: var(--primary, #6ea8fe);
  --gc-primary-hover: var(--primary-hover, #8bb9fe);
  --gc-primary-soft: var(--primary-soft, #1e2a44);
  --gc-surface: var(--surface, #1b1e27);
  --gc-surface-alt: var(--surface-alt, #232733);
  --gc-text: var(--text, #f1f3f7);
  --gc-muted: var(--text-muted, #aeb6c5);
  --gc-border: var(--border, #343a49);
  --gc-danger-bg: #3b1d22;
  --gc-danger-border: #7a2e37;
  --gc-danger-text: #f5b5bc;
  --gc-note-bg: #2d1a1e;
  --gc-note-border: #5a2a31;
}

.grade-checker *,
.grade-checker *::before,
.grade-checker *::after,
.grade-print-root *,
.grade-print-root *::before,
.grade-print-root *::after {
  box-sizing: border-box;
}

/* Search form */

.grade-form {
  display: grid;
  grid-template-columns: 170px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: end;
  width: 100%;
  max-width: 640px;
  margin: 0 auto 24px;
}

.grade-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.grade-field label {
  font-size: .875rem;
  font-weight: 600;
  color: var(--gc-text);
}

.grade-form .form-select,
.grade-form .form-control,
.grade-form .grade-submit {
  width: 100%;
  min-height: 44px;
  margin: 0;
  transition: border-color .15s ease, box-shadow .15s ease, background-color .15s ease;
}

.grade-form .form-select,
.grade-form .form-control {
  background-color: var(--gc-surface);
  color: var(--gc-text);
  border-color: var(--gc-border);
}

.grade-form .form-select option {
  background: var(--gc-surface);
  color: var(--gc-text);
}

.grade-form .form-control::placeholder {
  color: var(--gc-muted);
  opacity: 1;
}

.grade-form .form-select:focus,
.grade-form .form-control:focus {
  border-color: var(--gc-primary);
  box-shadow: 0 0 0 .2rem var(--gc-primary-soft);
  box-shadow: 0 0 0 .2rem color-mix(in srgb, var(--gc-primary) 25%, transparent);
}

.grade-form .grade-submit {
  min-width: 104px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  background-color: var(--gc-primary);
  border-color: var(--gc-primary);
  color: #fff;
}

.grade-form .grade-submit:disabled {
  opacity: .7;
  cursor: wait;
}

@media (hover: hover) {
  .grade-form .form-select:hover:not(:focus),
  .grade-form .form-control:hover:not(:focus) {
    border-color: var(--gc-primary);
    box-shadow: 0 0 0 .15rem var(--gc-primary-soft);
  }

  .grade-form .grade-submit:not(:disabled):hover {
    background-color: var(--gc-primary-hover);
    border-color: var(--gc-primary-hover);
    box-shadow: 0 2px 8px rgba(0, 0, 0, .2);
  }

  .grade-report .grade-print button:hover {
    background-color: var(--gc-primary);
    color: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
  }
}

.grade-form .grade-submit:focus-visible,
.grade-report .grade-print button:focus-visible {
  outline: 2px solid var(--gc-primary);
  outline-offset: 2px;
}

.grade-form .grade-submit:not(:disabled):active {
  filter: brightness(.9);
}

.grade-error {
  grid-column: 1 / -1;
  padding: 10px 14px;
  border: 1px solid var(--gc-danger-border);
  border-radius: 8px;
  background: var(--gc-danger-bg);
  color: var(--gc-danger-text);
  font-size: 14px;
}

.grade-error[hidden] {
  display: none;
}

.grade-button-spinner {
  width: 15px;
  height: 15px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: grade-spin .7s linear infinite;
}

/* Result + loading */

.grade-result {
  width: 100%;
}

.grade-loading {
  min-height: 180px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 32px 16px;
  text-align: center;
  border: 1px solid var(--gc-border);
  border-radius: 14px;
  background: var(--gc-surface);
  color: var(--gc-text);
}

.grade-loading-spinner {
  width: 42px;
  height: 42px;
  margin-bottom: 8px;
  border: 4px solid var(--gc-border);
  border-top-color: var(--gc-primary);
  border-radius: 50%;
  animation: grade-spin .75s linear infinite;
}

.grade-loading-title {
  font-size: 16px;
  font-weight: 600;
}

.grade-loading-text {
  font-size: 14px;
  color: var(--gc-muted);
}

.grade-report {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  color: var(--gc-text);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .3s ease, transform .3s ease;
}

.grade-report.grade-report-visible {
  opacity: 1;
  transform: none;
}

.grade-report img.grade-img {
  display: block;
  width: auto;
  height: auto;
  max-width: 128px;
  max-height: 128px;
  margin: 0 auto 16px;
}

/* Tables: a scroll wrapper gives rounded borders and mobile scrolling */

.grade-table-wrap {
  margin: 0 0 18px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
  border: 1px solid var(--gc-border);
  border-radius: var(--gc-radius);
  background: var(--gc-surface);
}

.grade-table-wrap > table {
  min-width: 100%;
}

.grade-report table {
  width: 100%;
  margin: 0;
  border-collapse: separate;
  border-spacing: 0;
  font-family: 'Sarabun', sans-serif;
  font-size: 14px;
  line-height: 1.45;
  background: transparent;
}

.grade-report .grade-page-table,
.grade-report .grade-page-table > tbody > tr > td {
  border: 0;
  padding: 0;
}

.grade-report th,
.grade-report td {
  padding: 10px 12px;
  border: 0;
  border-bottom: 1px solid var(--gc-border);
  font-family: inherit;
  text-align: left;
  vertical-align: middle;
  background: var(--gc-surface);
  color: var(--gc-text);
}

.grade-report tr:last-child > td {
  border-bottom: 0;
}

/* Info table: values are plain text, never links */

.grade-report .grade-info-table > tbody > tr > td {
  padding: 9px 14px;
  text-align: left;
}

.grade-report .grade-info-table a {
  color: inherit;
  text-decoration: none;
  cursor: text;
  pointer-events: none;
}

/* Course + activity tables */

.grade-report .grade-course-table,
.grade-report .grade-activity-table {
  min-width: 100%;
}

.grade-report .grade-course-table th,
.grade-report .grade-activity-table th {
  padding: 11px 10px;
  border-bottom: 2px solid var(--gc-border);
  background: var(--gc-primary-soft);
  color: var(--gc-text);
  font-weight: 700;
  text-align: center;
  white-space: nowrap;
}

.grade-report .grade-course-table td + td,
.grade-report .grade-activity-table td + td,
.grade-report .grade-course-table th + th,
.grade-report .grade-activity-table th + th {
  border-left: 1px solid var(--gc-border);
}

.grade-report .grade-course-table tbody tr:nth-child(even) > td,
.grade-report .grade-activity-table tbody tr:nth-child(even) > td {
  background: var(--gc-surface-alt);
}

.grade-report .grade-course-table td.grade-c,
.grade-report .grade-activity-table td.grade-c {
  text-align: center;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

@media (hover: hover) {
  .grade-report .grade-course-table tbody tr:hover > td,
  .grade-report .grade-activity-table tbody tr:hover > td {
    background: var(--gc-primary-soft);
  }
}

.grade-report .grade-activity-table tbody tr:last-child > td {
  background: var(--gc-primary-soft);
  font-weight: 700;
}

/* GPA + notice: boxes for div/p, tinted cell for td */

.grade-report .grade-gpa {
  color: var(--gc-text);
  font-weight: 700;
}

.grade-report td.grade-gpa {
  background: var(--gc-primary-soft);
}

.grade-report div.grade-gpa,
.grade-report p.grade-gpa {
  margin: 12px 0 24px;
  padding: 13px 16px;
  border: 1px solid var(--gc-border);
  border-radius: 10px;
  background: var(--gc-primary-soft);
  font-size: 15px;
}

.grade-report .grade-note,
.grade-report .grade-note * {
  text-align: left;
  text-indent: 0;
}

.grade-report .grade-note {
  color: var(--gc-danger-text);
  font-size: 14px;
  line-height: 1.5;
}

.grade-report .grade-note p,
.grade-report .grade-note br:last-child {
  margin: 0;
}

.grade-report .grade-note ul,
.grade-report .grade-note ol {
  margin: 0;
  padding-left: 1.2em;
}

/* Note inside a table: wrap normally, even where the table cells do not */
.grade-report td.grade-note {
  min-width: 260px;
  padding: 8px 12px;
  background: var(--gc-note-bg);
  white-space: normal;
}

.grade-report div.grade-note,
.grade-report p.grade-note {
  margin: 12px 0;
  padding: 8px 12px;
  border: 1px solid var(--gc-note-border);
  border-left: 4px solid #dc3545;
  border-radius: 8px;
  background: var(--gc-note-bg);
}

.grade-report .grade-print {
  display: flex;
  justify-content: flex-end;
  margin-top: 18px;
}

/* Mobile
 * The table wrappers scroll sideways, so cells keep their text on one line
 * instead of wrapping into narrow columns. */

@media screen and (max-width: 767.98px) {
  .grade-form {
    grid-template-columns: 1fr;
    gap: 10px;
    max-width: 100%;
  }

  .grade-report {
    font-size: 13px;
  }

  .grade-report table {
    width: max-content;
    min-width: 100%;
    font-size: 13px;
  }

  .grade-report th,
  .grade-report td {
    padding: 9px 10px;
    white-space: nowrap;
    word-break: keep-all;
    overflow-wrap: normal;
  }

  .grade-report td.grade-note {
    white-space: normal;
  }

  .grade-report .grade-course-table,
  .grade-report .grade-activity-table {
    min-width: 100%;
  }

  .grade-report .grade-print {
    justify-content: stretch;
  }

  .grade-report .grade-print button {
    width: 100%;
  }
}

@media screen and (max-width: 480px) {
  .grade-loading {
    min-height: 150px;
    padding: 24px 12px;
  }

  .grade-report img.grade-img {
    max-width: 96px;
    max-height: 96px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .grade-report {
    transition: none;
    transform: none;
  }

  .grade-button-spinner,
  .grade-loading-spinner {
    animation-duration: 1.6s;
  }
}

@keyframes grade-spin {
  to {
    transform: rotate(360deg);
  }
}

/* Print
 *
 * The JS copies the report into .grade-print-root (a direct child of <body>)
 * while printing, so it never depends on the module position or on
 * ancestors with overflow/transform/height set by the template.
 * Paper is always light, whatever data-theme is active.
 */

@media screen {
  .grade-print-root {
    display: none !important;
  }
}

@media print {
  @page {
    margin: 12mm;
  }

  body.grade-printing > *:not(.grade-print-root) {
    display: none !important;
  }

  html,
  body.grade-printing {
    height: auto !important;
    overflow: visible !important;
    background: #fff !important;
  }

  .grade-print-root,
  html[data-theme] .grade-print-root {
    --gc-primary: #0d6efd;
    --gc-primary-hover: #0b5ed7;
    --gc-primary-soft: #f3f6fa;
    --gc-surface: #fff;
    --gc-surface-alt: #fafbfc;
    --gc-text: #263238;
    --gc-muted: #6b7280;
    --gc-border: #dfe3e8;
    --gc-danger-text: #842029;
    --gc-note-bg: #fff8f8;
    --gc-note-border: #f1d5d8;
    display: block;
    width: 100%;
    color: #263238;
  }

  .grade-print-root .grade-report {
    position: static;
    width: 100%;
    max-width: none;
    margin: 0;
    opacity: 1;
    transform: none;
    background: #fff;
  }

  .grade-print-root .grade-print {
    display: none !important;
  }

  /*
   * Print layout: A4 is only ~718px wide, so use a smaller type size and
   * tighter cells. Headers and short values (.grade-c) stay on one line;
   * only free-text cells (subject names, info values) may wrap.
   * Selectors carry .grade-print-root so no screen rule can win.
   */
  .grade-print-root .grade-report {
    font-size: 12px;
    line-height: 1.4;
  }

  .grade-print-root .grade-table-wrap {
    overflow: visible;
    margin-bottom: 12px;
    border-radius: 8px;
  }

  .grade-print-root .grade-report table {
    width: 100%;
    min-width: 0;
    font-size: 12px;
  }

  .grade-print-root .grade-report th,
  .grade-print-root .grade-report td {
    padding: 5px 7px;
    white-space: normal;
    word-break: normal;
    overflow-wrap: normal;
  }

  .grade-print-root .grade-report th,
  .grade-print-root .grade-report td.grade-c {
    white-space: nowrap;
  }

  .grade-print-root .grade-report .grade-info-table > tbody > tr > td {
    padding: 5px 8px;
  }

  /* Info labels (odd cells) stay on one line; values may wrap if really needed */
  .grade-print-root .grade-report .grade-info-table > tbody > tr > td:nth-child(odd) {
    white-space: nowrap;
  }

  /* The row that holds the nested course table: no padding, may split across pages */
  .grade-print-root .grade-report .grade-info-table > tbody > tr > td[colspan="6"] {
    padding: 0;
  }

  .grade-print-root .grade-report .grade-info-table > tbody > tr > td[colspan="6"].grade-gpa {
    padding: 6px 8px;
  }

  .grade-print-root .grade-report .grade-info-table > tbody > tr {
    break-inside: auto;
  }

  .grade-print-root .grade-report .grade-info-table .grade-table-wrap {
    margin: 0;
    border-width: 0 0 1px;
    border-radius: 0;
  }

  .grade-print-root .grade-report .grade-activity-table {
    break-inside: avoid;
  }

  .grade-print-root .grade-report td.grade-note,
  .grade-print-root .grade-report div.grade-note,
  .grade-print-root .grade-report p.grade-note {
    min-width: 0;
    white-space: normal;
    break-inside: avoid;
  }

  .grade-print-root .grade-report .grade-img {
    max-width: 96px;
    max-height: 96px;
  }

  .grade-report tr {
    break-inside: avoid;
  }

  .grade-report thead {
    display: table-header-group;
  }

  /* GPA cell: force background to print, plus a border in case backgrounds are disabled */
  .grade-report td.grade-gpa {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    background: #eaf1fb !important;
    color: #1f3b5b !important;
    font-weight: 700;
    border-top: 1px solid #1f3b5b;
    border-bottom: 1px solid #1f3b5b;
  }
}
