/*
  Module 2: Attention Mechanisms & KV-Cache Internals.
  Shared layout and component styles. Colours come only from shared semantic tokens in
  assets/theme.css; tensor roles combine a token shade with a distinct border style and are
  always paired with a text label (Q, K, V, A, O, M).
*/

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: 1.5rem;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--body-font);
  overflow-wrap: break-word;
}

a {
  color: inherit;
  text-underline-offset: 3px;
}

a:hover {
  color: var(--accent);
}

:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

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

.skip-link {
  position: absolute;
  left: 16px;
  top: -100px;
  padding: 8px 12px;
  background: var(--accent);
  color: var(--on-accent);
  z-index: 10;
}

.skip-link:focus {
  top: 12px;
}

/* ---------------------------------------------------------------- Header and footer */

.site-header,
.site-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px 24px;
  max-width: 1240px;
  margin: 0 auto;
  padding: 20px 24px;
}

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

.site-header nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 20px;
  font-size: 14px;
}

.site-header nav a {
  text-decoration: none;
}

.site-header nav a[aria-current="page"] {
  font-weight: 650;
  text-decoration: underline;
}

.wordmark {
  font-weight: 650;
  text-decoration: none;
  white-space: nowrap;
}

.wordmark span {
  color: var(--muted);
  font-weight: 400;
}

.site-footer {
  border-top: 1px solid var(--line);
  margin-top: 48px;
  color: var(--muted);
  font-size: 13px;
}

/* ---------------------------------------------------------------- Layout */

.lesson-layout {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  gap: 48px;
  max-width: 1240px;
  margin: 0 auto;
  padding: 32px 24px 0;
}

main {
  min-width: 0;
}

main.overview {
  max-width: 880px;
  margin: 0 auto;
  padding: 32px 24px 0;
}

/* Prose stays in a readable column; diagrams, labs and tables may use the full width. */
main :where(section, .lesson-header) > :where(p, ul, ol, dl, h2, h3, h4, blockquote, details, .callout) {
  max-width: 72ch;
}

main > section {
  padding: 28px 0 36px;
  border-top: 1px solid var(--line);
  scroll-margin-top: 16px;
}

h1 {
  font-family: var(--heading-font);
  font-weight: 400;
  font-size: clamp(2rem, 5vw, 3rem);
  line-height: 1.15;
  letter-spacing: -0.02em;
  margin: 0 0 16px;
}

h2 {
  font-size: 1.45rem;
  line-height: 1.3;
  margin: 0 0 14px;
}

h3 {
  font-size: 1.1rem;
  margin: 24px 0 8px;
}

.eyebrow {
  color: var(--accent);
  font-size: 13px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin: 0 0 12px;
}

.lede {
  font-size: 1.12rem;
  color: var(--muted);
}

.small {
  font-size: 14px;
  color: var(--muted);
}

.lesson-header {
  padding-bottom: 20px;
}

.resume {
  padding: 10px 14px;
  border-left: 3px solid var(--accent);
  background: var(--surface-alt);
}

/* ---------------------------------------------------------------- Lesson nav and TOC */

.sidebar {
  position: sticky;
  top: 16px;
  align-self: start;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  font-size: 14px;
}

.nav-title {
  margin: 0 0 8px;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.lesson-nav ol,
.toc ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

.lesson-nav li a,
.toc li a {
  display: block;
  padding: 6px 0 6px 12px;
  border-left: 3px solid transparent;
  text-decoration: none;
  color: var(--muted);
}

.lesson-nav a[aria-current="page"],
.toc a[aria-current="location"] {
  border-left-color: var(--accent);
  color: var(--ink);
  font-weight: 650;
}

.lesson-nav a[aria-current="page"]::after {
  content: " · current";
  font-weight: 400;
  color: var(--muted);
}

.lesson-num {
  font-variant-numeric: tabular-nums;
  margin-right: 4px;
}

.visited-marker {
  color: var(--muted);
  font-size: 12px;
}

.toc-details {
  margin: 0;
  border: 0;
  background: none;
}

.toc-details > summary {
  padding: 0;
  border: 0;
}

.toc-details > ol {
  margin: 8px 0 0;
}

.toc {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.lesson-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.lesson-list li {
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}

.lesson-list li a {
  font-weight: 650;
  text-decoration: none;
}

.lesson-list p {
  margin: 4px 0 0;
  color: var(--muted);
}

.pager {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 24px 0;
  border-top: 1px solid var(--line);
}

.pager a {
  display: flex;
  flex-direction: column;
  max-width: 48%;
  text-decoration: none;
}

.pager a span {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.pager-next {
  margin-left: auto;
  text-align: right;
}

/* ---------------------------------------------------------------- Equations and notation */

var {
  font-style: italic;
}

.eq {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  margin: 16px 0;
  padding: 12px 16px;
  border-left: 3px solid var(--line);
  background: var(--surface-alt);
  font-size: 1.1rem;
  line-height: 1.8;
  white-space: nowrap;
}

.eq-boxed {
  border: 2px solid var(--accent);
  background: var(--card);
}

.eq-vars {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 4px 16px;
  margin: 8px 0 16px;
  font-size: 15px;
}

.eq-vars dt {
  font-weight: 600;
}

.eq-vars dd {
  margin: 0;
}

.shape,
.num {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

code,
kbd {
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  padding: 1px 4px;
  border-radius: 3px;
  background: var(--surface-alt);
}

pre {
  overflow-x: auto;
  padding: 12px 16px;
  background: var(--surface-alt);
  border-radius: 6px;
}

pre code {
  padding: 0;
  background: none;
}

/* ---------------------------------------------------------------- Tensor roles
   Each role sets shade + border style. Always pair with a letter label. */

.t-q {
  --role-bg: var(--accent-soft);
  --role-border: 2px solid var(--accent);
  --role-fill: var(--accent);
}

.t-k {
  --role-bg: var(--surface-alt);
  --role-border: 2px dashed var(--accent);
  --role-fill: var(--diagram-surface);
}

.t-v {
  --role-bg: var(--card);
  --role-border: 2px dotted var(--ink);
  --role-fill: var(--muted);
}

.t-w {
  --role-bg: var(--success-soft);
  --role-border: 3px double var(--accent);
  --role-fill: var(--success);
}

.t-o {
  --role-bg: var(--diagram);
  --role-border: 2px solid var(--diagram);
  --role-ink: var(--on-diagram);
  --role-fill: var(--diagram);
}

.t-mask {
  --role-bg: var(--danger-soft);
  --role-border: 2px dashed var(--muted);
  --role-ink: var(--muted);
  --role-fill: var(--danger-soft);
}

.tensor-tag {
  display: inline-block;
  min-width: 1.8em;
  padding: 0 6px;
  border: var(--role-border, 1px solid var(--line));
  border-radius: 4px;
  background: var(--role-bg, var(--card));
  color: var(--role-ink, var(--ink));
  font-weight: 700;
  font-style: normal;
  text-align: center;
  line-height: 1.6;
}

/* Cells and blocks carrying a role (e.g. <td class="t-mask">). */
td.t-q,
td.t-k,
td.t-v,
td.t-w,
td.t-o,
td.t-mask,
.tensor-block {
  background: var(--role-bg, var(--card));
  color: var(--role-ink, var(--ink));
}

.tensor-block {
  border: var(--role-border, 1px solid var(--line));
  border-radius: 6px;
  padding: 8px 12px;
}

/* ---------------------------------------------------------------- Matrices and tables */

.matrix-scroll,
.table-scroll {
  position: relative; /* contains visually hidden labels inside the scroll box */
  max-width: 100%;
  overflow-x: auto;
  margin: 12px 0;
}

table {
  border-collapse: collapse;
  background: var(--card);
}

caption {
  caption-side: top;
  text-align: left;
  font-weight: 600;
  padding: 0 0 6px;
}

th,
td {
  padding: 6px 10px;
  border: 1px solid var(--line);
  text-align: left;
}

thead th,
tbody th {
  background: var(--surface-alt);
  font-weight: 600;
}

table.matrix {
  border: var(--role-border, 1px solid var(--line));
}

table.matrix td {
  text-align: right;
  font-variant-numeric: tabular-nums;
  min-width: 3.5em;
  white-space: nowrap;
}

.matrix-corner {
  border: 0;
  background: transparent;
}

.is-highlight,
tr.is-highlight > td {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  font-weight: 700;
}

.key-table td:first-child {
  white-space: nowrap;
}

/* Cache rows: reused vs newly computed. Always include a text status column/label too. */
.is-new {
  font-weight: 700;
  background: var(--accent-soft);
}

.is-reused {
  color: var(--muted);
}

.is-masked {
  background: var(--danger-soft);
  color: var(--muted);
}

/* ---------------------------------------------------------------- Bars */

.bars {
  list-style: none;
  margin: 8px 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.bars li {
  position: relative;
  display: grid;
  grid-template-columns: minmax(4em, max-content) minmax(60px, 1fr) minmax(4em, max-content);
  align-items: center;
  gap: 10px;
  font-size: 14px;
}

.bar-track {
  display: block;
  height: 14px;
  background: var(--surface-alt);
  border: 1px solid var(--line);
}

.bar-fill {
  display: block;
  height: 100%;
  background: var(--role-fill, var(--accent));
}

.bar-fill.is-negative {
  background: var(--muted);
}

.bar-value {
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* ---------------------------------------------------------------- Labs (interactive widgets)
   Markup: <div class="lab" role="group" aria-labelledby="ID">
             <div class="lab-header"><h3 id="ID">Title</h3>
               <dl class="lab-guide"><div><dt>What this shows</dt><dd>…</dd></div>
               <div><dt>Try</dt><dd>…</dd></div><div><dt>What to notice</dt><dd>…</dd></div></dl></div>
             <div class="lab-controls">…</div><div class="lab-body">…</div>
             <div class="lab-output" aria-live="polite">…</div>
             <p class="lab-assumptions">Assumptions: …</p></div> */

.lab {
  margin: 20px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  max-width: 100%;
}

.lab-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.lab-header h3 {
  margin: 0 0 8px;
}

.lab-guide {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 0;
  font-size: 14px;
}

.lab-guide dt {
  font-weight: 650;
}

.lab-guide dd {
  margin: 2px 0 0;
  color: var(--muted);
}

.lab-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 12px 16px;
  padding: 14px 16px;
  background: var(--surface-alt);
  border-bottom: 1px solid var(--line);
}

.lab-body,
.lab-output {
  padding: 14px 16px;
  min-width: 0;
}

.lab-output {
  border-top: 1px solid var(--line);
}

.lab-assumptions {
  margin: 0;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  font-size: 13px;
  color: var(--muted);
}

.lab-error {
  padding: 8px 12px;
  border-left: 3px solid var(--muted);
  background: var(--danger-soft);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 14px;
}

.field > span,
.field > label {
  font-weight: 600;
}

input[type="text"],
input[type="number"],
select {
  font: inherit;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
  max-width: 100%;
}

input[type="number"] {
  width: 8em;
}

.button,
button:not(.theme-toggle) {
  font: inherit;
  font-weight: 600;
  padding: 7px 14px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
}

.button:hover,
button:not(.theme-toggle):hover {
  background: var(--accent-hover);
  color: var(--on-accent);
}

.button-secondary,
button.button-secondary {
  background: var(--card);
  color: var(--ink);
  border-color: var(--line);
}

button.button-secondary:hover {
  background: var(--surface-alt);
  color: var(--ink);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* Segmented radio toggle: <fieldset class="segmented"><legend>…</legend><label><input type="radio">…</label>… */
.segmented {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0;
  border: 0;
  padding: 0;
  margin: 0;
}

.segmented legend {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 4px;
}

.segmented label {
  padding: 6px 12px;
  border: 1px solid var(--line);
  background: var(--card);
  cursor: pointer;
}

.segmented label:has(input:checked) {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
  font-weight: 650;
}

.segmented input {
  margin: 0 6px 0 0;
}

/* ---------------------------------------------------------------- Static diagrams */

.diagram {
  margin: 20px 0;
  padding: 16px;
  border: 2px solid var(--line-strong);
  border-radius: 8px;
  background: var(--diagram-surface);
  color: var(--diagram-ink);
  max-width: 100%;
  overflow-x: auto;
}

.diagram figcaption {
  margin-top: 10px;
  color: var(--diagram-muted);
  font-size: 14px;
}

.flow-steps {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.flow-steps li {
  padding: 6px 10px;
  border-radius: 4px;
  background: var(--diagram-surface);
  color: var(--diagram-ink);
}

.flow-steps li + li::before {
  content: "→";
  margin-right: 8px;
  color: var(--diagram-muted);
}

svg text {
  fill: currentcolor;
}

/* ---------------------------------------------------------------- Callouts and details */

.callout {
  margin: 16px 0;
  padding: 12px 16px;
  border-left: 3px solid var(--accent);
  background: var(--surface-alt);
}

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

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

.callout-label {
  font-weight: 700;
}

.callout.key-takeaway {
  border-left-width: 6px;
  background: var(--accent-soft);
}

.callout.limit {
  border-left-style: dashed;
  border-left-color: var(--muted);
}

details {
  margin: 16px 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--card);
}

summary {
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
}

details > :not(summary) {
  margin-left: 14px;
  margin-right: 14px;
}

details[open] > summary {
  border-bottom: 1px solid var(--line);
}

/* ---------------------------------------------------------------- Knowledge checks */

.check {
  margin: 16px 0;
  max-width: 72ch;
}

.check fieldset {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  padding: 14px 16px;
  margin: 0 0 10px;
}

.check legend {
  font-weight: 650;
  padding: 0 4px;
}

.check-option {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 4px 0;
}

.check-numeric {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.check-feedback[data-status] {
  margin-top: 10px;
  padding: 10px 14px;
  border-left: 3px solid var(--line);
  background: var(--surface-alt);
}

.check-feedback[data-status="correct"] {
  border-left-color: var(--success);
  background: var(--success-soft);
}

.check-feedback[data-status="incorrect"] {
  border-left-style: dashed;
  border-left-color: var(--muted);
  background: var(--danger-soft);
}

.check-verdict {
  font-weight: 700;
  margin: 0 0 6px;
}

.references li {
  margin: 6px 0;
}

/* ---------------------------------------------------------------- Responsive */

@media (width <= 900px) {
  .lesson-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 16px;
    padding: 20px 16px 0;
  }

  .sidebar {
    position: static;
    max-height: none;
    overflow: visible;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--line);
  }

  .lab-guide {
    grid-template-columns: minmax(0, 1fr);
  }

  .sidebar .lesson-nav ol {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 16px;
  }

  .sidebar .lesson-nav li a {
    padding: 4px 0;
    border-left: 0;
    border-bottom: 3px solid transparent;
  }

  .sidebar .lesson-nav a[aria-current="page"] {
    border-bottom-color: var(--accent);
  }

  .toc {
    margin-top: 12px;
    padding-top: 12px;
  }
}

@media (width <= 520px) {
  .site-header,
  .site-footer {
    padding: 16px;
  }

  main.overview {
    padding: 20px 16px 0;
  }

  .pager a {
    max-width: 100%;
  }

  .bars li {
    grid-template-columns: minmax(3em, max-content) minmax(40px, 1fr) minmax(3.5em, max-content);
    gap: 6px;
  }

  .eq {
    font-size: 1rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
  }
}
