/*
 * week.css: the week page's interactive parts (templates/week/*.html): Trace, Code (tabs, editor, Run and Submit,
 * results), the Big-O charts shared by Code and Measure, Measure, and Score. Uses the tokens and components in
 * site.css (docs/design.md); the two-column sticky layout itself lives there. Light mode only.
 *
 * Classes:
 *   code     .code-form(.is-enhanced) .code-tabs .code-tab(.is-editable|.is-test) .code-panel .code-file-name
 *            .code-source (the textarea under each editor) .code-editor (week.js mounts CodeMirror here)
 *            .code-test .code-test-note .code-escape .code-actions .code-saved .code-notice .code-results
 *   results  .job .job-where .compile-message .failing-tests .job-output .bigo-case .job-points
 *   charts   figure.chart.bigo-chart > .chart-body (--chart-gutter) > .chart-y-title .chart-y-labels .chart-plot
 *            .chart-x-labels (> .is-minor) .chart-x-title; .bigo-table-wrap .bigo-table .bigo-verdict
 *   trace    .trace-feedback .trace-steps .trace-earned .trace-next .trace-form .trace-prompt .trace-given
 *   measure  .measure-source .measure-feedback .measure-detail .measure-earned .measure-form .measure-choices
 *            .measure-choice
 *   score    .score-table .score-total
 */

.week-part [hidden] {
  display: none !important;
}

.week-part > h2 + * {
  margin-top: 0;
}

/* ---- Code: files and tabs ----------------------------------------------------------------------------------- */

.week-code {
  /* One column (below 1200px): the page scrolls, so the editor is at least 420px and at most about 70% of the
     window. */
  --editor-height: max(420px, min(70vh, 100vh - 26rem));
}

/*
 * Two columns: the Code section is a sticky panel no taller than the window, so the editor leaves room for the
 * heading, tabs, and Run and Submit in that height. The usual laptop viewport after the taskbar and browser bars is
 * 590 to 680px tall (a 1366x768 screen, or 1920x1080 at 150%); a 420px floor pushed Run and Submit out of the panel
 * there. Run and Submit also stick to the panel's bottom edge while the form is in view.
 */
@media (min-width: 1200px) {
  .week-code {
    --editor-height: clamp(240px, 100vh - 24rem, 70vh);
  }

  .code-source {
    min-height: 240px;
  }

  .code-actions {
    position: sticky;
    bottom: 0;
    z-index: 1;
    padding: 0.5rem 0;
    background: var(--paper);
  }
}

.code-form {
  margin: 0;
}

.code-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin: 0 0 0.75rem;
  border-bottom: 1px solid var(--line-soft);
}

/* A file name breaks only where the template put a <wbr> (between words and before ".java"). */
.code-tab {
  max-width: 100%;
  overflow-wrap: normal;
  text-align: start;
  min-height: 44px;
  margin: 0;
  padding: 0.5rem 0.85rem;
  background: transparent;
  color: var(--ink);
  border: 0;
  border-radius: var(--radius-control) var(--radius-control) 0 0;
  font-family: var(--font-code);
  font-size: 1rem;
  font-variant-ligatures: none;
  line-height: 1.3;
  cursor: pointer;
}

/* Every tab has an edge, so a tab that wrapped to a second row still looks like one. */
.code-tab {
  border: 1px solid var(--line-soft);
  border-bottom: 0;
}

.code-tab:hover {
  background: var(--code-bg);
}

.code-tab.is-test {
  color: var(--ink-2);
}

/* The open tab: bold plus a thick gold bar, a shape cue as well as a color (like the site nav). */
.code-tab[aria-selected="true"] {
  color: var(--ink);
  font-weight: 700;
  box-shadow: inset 0 -4px 0 var(--gold);
}

.code-tab:focus-visible {
  outline-offset: -3px;
}

.code-panel + .code-panel {
  margin-top: 1.25rem;
}

.code-form.is-enhanced .code-panel + .code-panel {
  margin-top: 0;
}

.code-file-name {
  display: block;
  margin: 0 0 0.35rem;
  font-family: var(--font-code);
  font-size: 1rem;
  font-variant-ligatures: none;
  font-weight: 700;
}

/* Once the tabs are showing, the tab names the file. */
.code-form.is-enhanced .code-file-name {
  display: none;
}

/* The plain form without JavaScript, and the fallback if the editor can't load. */
.code-source {
  display: block;
  width: 100%;
  height: var(--editor-height);
  min-height: 420px;
  margin: 0;
  padding: 0.6rem 0.75rem;
  background: var(--paper);
  color: var(--ink);
  border: 2px solid var(--line);
  border-radius: var(--radius-control);
  font-family: var(--font-code);
  font-size: 18px;
  font-variant-ligatures: none;
  line-height: 1.5;
  tab-size: 4;
  white-space: pre;
  overflow: auto;
  resize: vertical;
}

.code-editor .cm-editor {
  height: var(--editor-height);
}

.code-test-note {
  margin: 0 0 0.5rem;
}

pre.code-test {
  max-height: var(--editor-height);
  margin: 0;
  overflow: auto;
  font-size: 1rem;
}

.code-escape {
  margin: 0.5rem 0 0;
}

.code-actions {
  margin: 0.9rem 0 0;
}

.code-saved {
  margin: 0 0 0 auto;
  color: var(--ink-2);
  font-size: 1rem;
  line-height: 1.5;
}

.code-notice > .status {
  margin: 0.75rem 0 0;
}

/* ---- Code: results ------------------------------------------------------------------------------------------ */

.code-results {
  margin-top: 1rem;
  overscroll-behavior: contain;
}

.code-results:empty {
  margin-top: 0;
}

.job > :first-child {
  margin-top: 0;
}

.job > :last-child,
.job > .bigo-case:last-child > :last-child {
  margin-bottom: 0;
}

.job h3 {
  margin: 1.5rem 0 0.5rem;
  font-size: 1.3rem;
}

.job .status {
  margin-bottom: 0.5rem;
}

.job .feedback {
  margin-bottom: 0.75rem;
}

.job-where {
  margin: 0 0 0.5rem;
  font-family: var(--font-code);
  font-size: 1rem;
  font-variant-ligatures: none;
}

pre.compile-message {
  margin: 0 0 1rem;
  white-space: pre-wrap;
}

.failing-tests > li {
  font-size: 1.1rem;
}

pre.job-output {
  max-height: 16rem;
  margin: 0 0 1rem;
  overflow: auto;
}

.bigo-case + .bigo-case {
  margin-top: 2rem;
}

.job-points {
  margin: 1.25rem 0 0;
  font-weight: 700;
}

/* In the sticky right-hand column the results scroll with the panel itself: one scroll box, not one inside another. */

/* ---- Big-O charts (Code results and Measure) ---------------------------------------------------------------- */

.bigo-chart {
  max-width: 40rem;
  margin-bottom: 1rem;
}

/*
 * The plot is an SVG that scales with the column; its labels are text around it, placed by percentage, so they keep
 * their size at every width: counts in a gutter on the left, sizes under the plot, the axis names above and below.
 */
.chart-body {
  display: grid;
  grid-template-columns: var(--chart-gutter, 6ch) minmax(0, 1fr);
  grid-template-areas:
    "y-title ."
    "y-labels plot"
    ". x-labels"
    ". x-title";
  column-gap: 0.6rem;
  padding-right: 2rem;
  color: var(--ink-2);
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.chart-y-title,
.chart-x-title {
  margin: 0;
  max-width: none;
  color: var(--ink);
  font-weight: 700;
}

.chart-y-title {
  grid-area: y-title;
  margin-bottom: 0.6rem;
  text-align: end;
}

.chart-x-title {
  grid-area: x-title;
  margin-top: 0.35rem;
  text-align: center;
}

.chart-y-labels {
  grid-area: y-labels;
  position: relative;
}

.chart-y-labels > span {
  position: absolute;
  right: 0;
  transform: translateY(-50%);
  white-space: nowrap;
}

.chart-plot {
  grid-area: plot;
}

.chart-plot > svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
}

/* Lines keep their width however the plot is scaled (and stretched on a phone, below). */
.chart .grid line,
.chart .axis-base,
.chart .series-student,
.chart .series-expected {
  vector-effect: non-scaling-stroke;
}

/* On a phone a 2:1 plot is a sliver; a 4:3 one keeps the curve's shape readable (the SVG stretches to fit). */
@media (max-width: 40rem) {
  .chart-plot > svg {
    aspect-ratio: 4 / 3;
  }
}

.chart-x-labels {
  grid-area: x-labels;
  position: relative;
  height: 1.6em;
}

.chart-x-labels > span {
  position: absolute;
  top: 0.35rem;
  transform: translateX(-50%);
  white-space: nowrap;
}

/* Only a few sizes fit under a narrow plot; the table below lists them all. */
@media (max-width: 40rem) {
  .chart-x-labels > .is-minor {
    display: none;
  }
}

.chart .axis .axis-base {
  stroke: var(--line);
}

.bigo-table-wrap {
  margin-bottom: 0.75rem;
}

.bigo-table {
  min-width: 16rem;
}

.bigo-verdict {
  margin: 0 0 0.5rem;
}

/* ---- Trace ---------------------------------------------------------------------------------------------------- */

.trace-feedback {
  margin: 0 0 1.5rem;
}

.trace-feedback .status,
.measure-feedback .status {
  width: fit-content;
  margin-bottom: 0.5rem;
}

.trace-steps-caption {
  margin: 0 0 0.75rem;
  font-weight: 700;
}

.trace-steps td {
  font-family: var(--font-code);
  font-size: 1rem;
  font-variant-ligatures: none;
  white-space: nowrap;
}

/* A phone: each pass is a small block of "column: value" lines instead of a table wider than the screen. */
@media (max-width: 37.5rem) {
  table.trace-steps,
  .trace-steps tbody,
  .trace-steps tr,
  .trace-steps td {
    display: block;
  }

  table.trace-steps {
    width: 100%;
  }

  .trace-steps thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  table.data.trace-steps tr {
    padding: 0.5rem 0.75rem;
  }

  table.data.trace-steps tr + tr {
    border-top: 1px solid var(--line);
  }

  table.data.trace-steps td {
    padding: 0.1rem 0;
    border: 0;
    white-space: normal;
  }

  .trace-steps td[data-label]::before {
    content: attr(data-label) ": ";
    font-family: var(--font-body);
    font-weight: 700;
  }
}

.trace-earned,
.trace-next,
.measure-earned {
  margin: 0 0 1rem;
}

.trace-prompt {
  margin: 0 0 0.75rem;
}

/* A call like indexOf(a, x) stays on one line where there's room; on a phone a long one
   (LinearSearch.indexOf(a, x)) may wrap rather than widen the page. */
@media (min-width: 30em) {
  .trace-prompt code {
    white-space: nowrap;
  }
}

.trace-given {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.25rem;
  margin: 0 0 1.25rem;
  padding: 0;
  list-style: none;
}

.trace-given li + li {
  margin-top: 0;
}

.trace-given code {
  padding: 0.2em 0.5em;
  font-size: 1.05rem;
}

/* ---- Measure ------------------------------------------------------------------------------------------------ */

.measure-source {
  color: var(--ink-2);
  font-size: 1rem;
  line-height: 1.5;
}

.measure-feedback {
  margin: 1rem 0;
}

.measure-detail {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.measure-form fieldset {
  min-width: 0;
  margin: 0 0 1.25rem;
  padding: 0;
  border: 0;
}

.measure-form legend {
  padding: 0 0 0.75rem;
  font-weight: 700;
  line-height: 1.45;
}

.measure-choices {
  display: grid;
  gap: 0.5rem;
  max-width: 32rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.measure-choice {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 0.75rem;
  align-items: start;
  margin: 0;
  padding: 0.5rem 0.875rem;
  background: var(--paper);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-control);
}

.measure-choices li + li {
  margin-top: 0;
}

.measure-choice:has(input:checked) {
  border-color: var(--line);
}

.measure-choice input {
  width: 1.25rem;
  height: 1.25rem;
  margin: 0.375rem 0 0;
  accent-color: var(--ink);
  cursor: pointer;
}

.measure-choice label {
  min-height: 2rem;
  cursor: pointer;
}

/* ---- Score -------------------------------------------------------------------------------------------------- */

.score-table {
  min-width: 16rem;
}

.score-table tfoot th,
.score-table tfoot td {
  border-top: 1px solid var(--line);
  font-weight: 700;
}

/* ---- Windows high contrast ---------------------------------------------------------------------------------- */

@media (forced-colors: active) {
  .code-tab[aria-selected="true"] {
    text-decoration: underline;
    text-decoration-thickness: 3px;
  }

  .chart .series-student,
  .chart .axis .axis-base {
    stroke: CanvasText;
  }

  .chart .series-student-point {
    fill: CanvasText;
  }

  .chart .series-expected {
    stroke: LinkText;
  }

  .chart-body,
  .chart-y-title,
  .chart-x-title {
    color: CanvasText;
  }

  .chart-swatch {
    border-top-color: CanvasText;
  }

  .chart-swatch.is-expected {
    border-top-color: LinkText;
  }
}
