/*
 * site.css: the design system (docs/design.md). Tokens are defined once, on :root, and lesson.css and admin.css
 * use the variables. Light mode only. WCAG 2.1 AA: every status color comes with a word, focus is always visible,
 * and nothing scrolls sideways at 320px except code blocks and wide tables, inside their own boxes.
 *
 * Shared class names (other stylesheets and templates rely on these):
 *   layout      .site-header .site-header-inner .site-home .site-nav .site-signout  main#main.page  .skip-link
 *   week page   .week .week-title .week-video .video-frame .video-link .week-lesson .lesson-contents .lesson-tally
 *               .week-part .week-trace .week-code .week-measure .week-score
 *   components  .panel  .btn .btn-primary .btn-secondary  .field .hint  .status (.is-pass .is-fail .is-caution
 *               .is-running)  ul.feedback  .table-wrap table.data (.num)  figure.chart  pre.code  pre.console
 *               .visually-hidden
 *   pages       .signin .week-list .error-page
 */

/* ---- Tokens ------------------------------------------------------------------------------------------------ */

:root {
  --page: #FAF9F5;
  --paper: #FFFFFF;
  --ink: #17284A;
  --ink-2: #4A5468;
  --line: #767D8C;
  --line-soft: #E3E0D6;
  --gold: #E9B116;
  --cream: #FBEEB8;
  --code-bg: #EEF1F6;
  --pass: #1B6B45;
  --pass-bg: #E4F2EA;
  --fail: #9E2A2B;
  --fail-bg: #F9EAEA;
  --caution: #7A5C00;
  --caution-bg: #FDF6DC;
  --steel: #2B5F75;
  --plum: #5B2C6F;

  --font-head: "Source Serif 4", Georgia, "Times New Roman", serif;
  --font-body: "Atkinson Hyperlegible Next", "Atkinson Hyperlegible", system-ui, sans-serif;
  --font-code: "JetBrains Mono", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;

  --gutter: clamp(16px, 4vw, 48px);
  --page-max: 1440px;
  --measure: 70ch;
  --radius: 12px;
  --radius-control: 8px;
  --panel-pad: clamp(16px, 2vw, 28px);
  --section-gap: clamp(2.5rem, 5vw, 4rem);

  color-scheme: light;
}

/* ---- Base -------------------------------------------------------------------------------------------------- */

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

html {
  font-size: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--page);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 1.25rem;
  line-height: 1.6;
  font-kerning: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow-wrap: break-word;
}

h1,
h2,
h3 {
  margin: 0 0 0.5em;
  color: var(--ink);
  font-family: var(--font-head);
  font-optical-sizing: auto;
  text-wrap: balance;
}

h1 {
  font-size: clamp(2.1rem, 1.6rem + 2vw, 3rem);
  font-weight: 500;
  line-height: 1.1;
  letter-spacing: -0.01em;
  margin-bottom: 0.6em;
}

h2 {
  font-size: clamp(1.6rem, 1.35rem + 1vw, 2.1rem);
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

h3 {
  font-size: clamp(1.3rem, 1.2rem + 0.4vw, 1.5rem);
  font-weight: 600;
  line-height: 1.3;
}

h4 {
  margin: 0 0 0.5em;
  font-family: var(--font-body);
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.4;
}

p {
  margin: 0 0 1em;
  max-width: var(--measure);
  text-wrap: pretty;
}

ul,
ol {
  margin: 0 0 1em;
  padding-left: 1.4em;
}

li + li {
  margin-top: 0.25em;
}

strong,
b {
  font-weight: 700;
}

hr {
  height: 0;
  margin: 2.5rem 0;
  border: 0;
  border-top: 1px solid var(--line-soft);
}

img,
svg,
video,
iframe {
  max-width: 100%;
}

a {
  color: var(--ink);
  text-decoration-line: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.18em;
}

a:hover {
  text-decoration-thickness: 2px;
}

::placeholder {
  color: var(--ink-2);
  opacity: 1;
}

/* ---- Focus ------------------------------------------------------------------------------------------------- */

:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 2px;
}

/* On navy fills the ring is gold (7.5:1 against navy). */
pre.console:focus-visible,
.on-navy :focus-visible {
  outline-color: var(--gold);
}

/* The skip link's target: focus moves there, but a ring around the whole page would only be noise. */
main:focus {
  outline: none;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  margin: -1px !important;
  padding: 0 !important;
  overflow: hidden !important;
  clip: rect(0 0 0 0) !important;
  clip-path: inset(50%) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

/* ---- Skip link and header ---------------------------------------------------------------------------------- */

.skip-link {
  position: absolute;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 100;
  padding: 0.6rem 1rem;
  background: var(--ink);
  color: var(--paper);
  border-radius: var(--radius-control);
  font-weight: 700;
  text-decoration: none;
  transform: translateY(-200%);
}

.skip-link:focus {
  transform: none;
  box-shadow: inset 0 0 0 3px var(--gold);
}

.site-header {
  border-bottom: 1px solid var(--line-soft);
}

.site-header-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem clamp(1rem, 3vw, 2.5rem);
  max-width: var(--page-max);
  min-height: 4.5rem;
  margin-inline: auto;
  padding: 0.75rem var(--gutter);
}

.site-home {
  color: var(--ink);
  font-family: var(--font-head);
  font-size: 1.4rem;
  font-weight: 600;
  letter-spacing: -0.005em;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
}

.site-home:hover {
  text-decoration: underline;
  text-decoration-thickness: 2px;
}

.site-nav {
  margin-right: auto;
}

.site-nav ul {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 1.5rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.site-nav li + li {
  margin-top: 0;
}

.site-nav a {
  display: inline-block;
  padding-block: 0.4rem;
  font-size: 1.1rem;
  line-height: 1.4;
  text-decoration: none;
}

.site-nav a:hover {
  text-decoration: underline;
  text-decoration-thickness: 2px;
}

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

.site-signout {
  margin: 0;
}

/* The header's lone form sits at the far right, even for a pending user who has no nav. */
.site-home:has(+ .site-signout) {
  margin-right: auto;
}

@media (max-width: 40rem) {
  .site-home {
    margin-right: auto;
  }

  .site-nav {
    order: 3;
    flex-basis: 100%;
    margin-right: 0;
  }
}

/* ---- Page frame -------------------------------------------------------------------------------------------- */

.page {
  max-width: var(--page-max);
  margin-inline: auto;
  padding: clamp(2rem, 5vw, 4rem) var(--gutter) clamp(4rem, 8vw, 6rem);
}

.page > ul,
.page > ol {
  max-width: var(--measure);
}

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

/* ---- Panels ------------------------------------------------------------------------------------------------ */

.panel {
  padding: var(--panel-pad);
  background: var(--paper);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
}

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

.panel > :last-child {
  margin-bottom: 0;
}

/* ---- Buttons ----------------------------------------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  min-height: 44px;
  margin: 0;
  padding: 0.5em 1.1em;
  border: 2px solid transparent;
  border-radius: var(--radius-control);
  font-family: var(--font-body);
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 120ms ease, border-color 120ms ease;
}

.btn-primary {
  background: var(--ink);
  color: var(--paper);
}

.btn-primary:hover {
  background: color-mix(in srgb, var(--ink) 86%, var(--paper));
}

.btn-secondary {
  background: var(--paper);
  color: var(--ink);
  border-color: var(--line);
}

.btn-secondary:hover {
  border-color: var(--ink);
  background: var(--code-bg);
}

.btn[disabled],
.btn[aria-disabled="true"] {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary[disabled]:hover,
.btn-primary[aria-disabled="true"]:hover {
  background: var(--ink);
}

.btn-secondary[disabled]:hover,
.btn-secondary[aria-disabled="true"]:hover {
  border-color: var(--line);
  background: var(--paper);
}

/* A row of buttons, e.g. Run and Submit. */
.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
}

/* ---- Form fields ------------------------------------------------------------------------------------------- */

.field {
  margin: 0 0 1.25rem;
}

.field > label,
.field > .label {
  display: block;
  margin-bottom: 0.35rem;
  font-weight: 700;
  line-height: 1.4;
}

.field input,
.field textarea,
.field select {
  display: block;
  width: 100%;
  max-width: 32rem;
  min-height: 44px;
  margin: 0;
  padding: 0.45rem 0.75rem;
  background: var(--paper);
  color: var(--ink);
  border: 2px solid var(--line);
  border-radius: var(--radius-control);
  font-family: var(--font-body);
  font-size: 1.1rem;
  line-height: 1.4;
}

.field textarea {
  max-width: none;
  min-height: 8rem;
  resize: vertical;
}

.field input:hover,
.field textarea:hover,
.field select:hover {
  border-color: var(--ink);
}

.field input[aria-invalid="true"],
.field textarea[aria-invalid="true"] {
  border-color: var(--fail);
}

/* Trace answers and anything else typed as code. */
.field input.code-input,
.field .code-input {
  font-family: var(--font-code);
  font-variant-ligatures: none;
}

.field .hint {
  margin: 0.4rem 0 0;
}

/* ---- Status lines and feedback ----------------------------------------------------------------------------- */

.status {
  margin: 0 0 1em;
  font-weight: 700;
  line-height: 1.45;
}

.status.is-pass {
  color: var(--pass);
}

.status.is-fail {
  color: var(--fail);
}

.status.is-caution {
  color: var(--caution);
}

.status.is-running {
  color: var(--ink-2);
}

ul.feedback {
  margin: 0 0 1em;
  padding: 0;
  list-style: none;
}

ul.feedback > li {
  margin: 0;
  padding: 0.35rem 0;
  line-height: 1.45;
}

ul.feedback > li + li {
  border-top: 1px solid var(--line-soft);
}

ul.feedback > li.is-pass {
  color: var(--pass);
}

ul.feedback > li.is-fail {
  color: var(--fail);
}

ul.feedback > li.is-caution {
  color: var(--caution);
}

/* ---- Code -------------------------------------------------------------------------------------------------- */

code,
kbd,
samp,
pre {
  font-family: var(--font-code);
  font-variant-ligatures: none;
}

:not(pre) > code {
  padding: 0.1em 0.3em;
  background: var(--code-bg);
  border-radius: 4px;
  font-size: 0.9em;
  overflow-wrap: anywhere; /* a long call like LinearSearch.indexOf(a, x) must not widen a 320px page */
}

pre.code,
pre.console {
  margin: 1.5rem 0;
  padding: 1rem 1.25rem;
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: 1.05rem;
  line-height: 1.55;
  tab-size: 4;
}

pre.code {
  background: var(--code-bg);
  color: var(--ink);
}

pre.console {
  background: var(--ink);
  color: var(--gold);
}

pre.code code,
pre.console code {
  padding: 0;
  background: none;
  font-size: inherit;
}

/* ---- Tables ------------------------------------------------------------------------------------------------ */

/* position: relative keeps .visually-hidden text inside (absolutely positioned) within the scroll box, so it can't
   widen the page. */
.table-wrap {
  position: relative;
  max-width: 100%;
  margin: 0 0 1.5rem;
  overflow-x: auto;
}

table.data {
  border-collapse: separate;
  border-spacing: 0;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 1.1rem;
  line-height: 1.45;
}

table.data caption {
  padding: 0 0 0.75rem;
  caption-side: top;
  color: var(--ink);
  font-weight: 700;
  text-align: start;
}

table.data th,
table.data td {
  padding: 0.65rem 1rem;
  text-align: start;
  vertical-align: top;
}

table.data thead th {
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  font-weight: 700;
  white-space: nowrap;
}

table.data tbody tr + tr > * {
  border-top: 1px solid var(--line-soft);
}

table.data tbody th {
  font-weight: 700;
}

table.data .num {
  text-align: end;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

table.data .nowrap {
  white-space: nowrap;
}

/* Dense tables (admin) may drop to the 16px minimum. */
table.data.dense {
  font-size: 1rem;
}

table.data.dense th,
table.data.dense td {
  padding: 0.45rem 0.75rem;
}

/* ---- Charts ------------------------------------------------------------------------------------------------ */

figure.chart {
  margin: 0 0 1.5rem;
}

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

figure.chart figcaption {
  margin: 0 0 0.75rem;
  font-weight: 700;
  line-height: 1.45;
}

.chart .axis line,
.chart .axis path,
.chart .grid line {
  stroke: var(--line-soft);
  stroke-width: 1;
}

.chart text {
  fill: var(--ink-2);
  font-family: var(--font-body);
  font-size: 14px;
}

.chart .series-student {
  fill: none;
  stroke: var(--ink);
  stroke-width: 3;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.chart .series-student-point {
  fill: var(--ink);
  stroke: var(--paper);
  stroke-width: 2;
}

.chart .series-expected {
  fill: none;
  stroke: var(--steel);
  stroke-width: 2;
  stroke-dasharray: 6 5;
}

/* The legend is text; each entry can start with a small drawn sample of its line (decorative, aria-hidden). */
.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 1.5rem;
  margin: 0.75rem 0 0;
  padding: 0;
  list-style: none;
  color: var(--ink);
  font-size: 1rem;
}

.chart-legend li + li {
  margin-top: 0;
}

.chart-swatch {
  display: inline-block;
  width: 1.75rem;
  margin-right: 0.5rem;
  vertical-align: middle;
  border-top: 3px solid var(--ink);
}

.chart-swatch.is-expected {
  border-top: 2px dashed var(--steel);
}

/* ---- Sign-in, pending, and error pages ---------------------------------------------------------------------- */

.signin,
.error-page {
  max-width: 38rem;
  padding-top: clamp(1rem, 6vh, 4rem);
}

.signin-lead {
  font-size: 1.25rem;
}

.signin-action {
  margin: 1.75rem 0 0;
}

.signin-dev {
  margin-top: 3rem;
}

.signin-dev h2 {
  font-size: 1.5rem;
}

/* ---- Home: open weeks -------------------------------------------------------------------------------------- */

.week-list {
  max-width: 48rem;
  margin: 0 0 2rem;
  padding: 0;
  list-style: none;
  border-top: 1px solid var(--line-soft);
}

.week-list-item {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.25rem 1.5rem;
  margin: 0;
  padding: 1.1rem 0;
  border-bottom: 1px solid var(--line-soft);
}

.week-list-item + .week-list-item {
  margin-top: 0;
}

.week-list-link {
  font-family: var(--font-head);
  font-size: clamp(1.3rem, 1.2rem + 0.4vw, 1.5rem);
  font-weight: 600;
  line-height: 1.3;
  text-decoration: none;
}

.week-list-link:hover {
  text-decoration: underline;
  text-decoration-thickness: 2px;
}

.week-list-score {
  color: var(--ink-2);
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.week-list-next {
  color: var(--ink-2);
}

/* ---- Week page --------------------------------------------------------------------------------------------- */

.week > * {
  min-width: 0;
  margin-top: 0;
  margin-bottom: var(--section-gap);
}

.week > :last-child {
  margin-bottom: 0;
}

.week-title {
  margin-bottom: clamp(1.5rem, 3vw, 2.5rem);
}

.video-frame {
  position: relative;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background: var(--ink);
  border-radius: var(--radius);
}

.video-frame iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
}

.video-link {
  margin: 0.75rem 0 0;
  font-size: 1rem;
}

.week-lesson {
  max-width: var(--measure);
}

.lesson-contents {
  margin: 0 0 2rem;
  padding: 0.25rem 0 0.25rem 1rem;
  border-left: 3px solid var(--line-soft);
  font-size: 1.05rem;
}

.lesson-contents summary {
  width: fit-content;
  padding-block: 0.25rem;
  font-weight: 700;
  cursor: pointer;
}

.lesson-contents ol {
  margin: 0.5rem 0 0.25rem;
  padding-left: 1.4em;
}

.lesson-tally {
  margin: 2rem 0 0;
  color: var(--ink-2);
  font-size: 1rem;
}

.lesson-tally[hidden] {
  display: none;
}

.week-part h2 {
  margin-bottom: 0.75rem;
}

/*
 * Two columns from 1200px: the code section takes the right column, sticky at the top and spanning every row;
 * title across both, then video, lesson, trace, measure, and score down the left. DOM order is unchanged.
 */
@media (min-width: 1200px) {
  .week {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.08fr);
    column-gap: clamp(2rem, 3vw, 3.5rem);
    align-items: start;
  }

  .week > * {
    grid-column: 1;
  }

  .week > .week-title {
    grid-column: 1 / -1;
  }

  .week > .week-code {
    grid-column: 2;
    grid-row: 2 / span 12;
    position: sticky;
    top: 1.5rem;
    max-height: calc(100vh - 3rem);
    margin-bottom: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
}

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

@media (forced-colors: active) {
  .site-nav a[aria-current="page"] {
    text-decoration: underline;
    text-decoration-thickness: 3px;
  }

  .panel,
  table.data {
    border-color: CanvasText;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
