/* ---------- figures: they always take the full width of the text column ---------- */
.figure-image { margin: 1.6rem 0 1.8rem; }
.figure-image img { display: block; width: 100%; height: auto; border-radius: var(--radius); background: #fff; border: 1px solid var(--line); }
.figure-image figcaption { font-family: var(--sans); font-size: 0.84rem; line-height: 1.5; color: var(--muted); margin-top: 0.6rem; }

/* every interactive figure sits in a card exactly as wide as the text */
.widget-slot { width: 100%; margin: 1.6rem 0 1.8rem; }
.widget-slot.placeholder {
  border: 2px dashed #cfc8b8;
  border-radius: var(--radius);
  background: repeating-linear-gradient(135deg, #f6f3ec, #f6f3ec 12px, #f1ede4 12px, #f1ede4 24px);
  padding: 2.2rem 1.2rem;
  text-align: center;
  font-family: var(--sans);
  color: var(--muted);
}
.widget-slot.placeholder strong { display: block; color: var(--ink); margin-bottom: 0.25rem; }
.widget-slot.placeholder span { font-size: 0.85rem; }

/* ---------- interactive figure card (as wide as the text) ---------- */
.figure-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: clamp(14px, 3vw, 28px);
  font-family: var(--sans);
}
.figure-area { display: flex; justify-content: center; align-items: center; min-height: calc(var(--cell) * 4 + 3 * var(--gap)); }

/* the size of one cell: grows with the screen, but never beyond 5.5rem */
.figure-card { --cell: min(4.7rem, 15vw); --gap: 4px; }

.cell-grid {
  display: grid;
  grid-template-columns: repeat(var(--columns), var(--cell));
  grid-auto-rows: var(--cell);
  gap: var(--gap);
}
.cell {
  display: flex; align-items: center; justify-content: center;
  border-radius: 6px;
  font-size: calc(var(--cell) * 0.4);      /* the number always fits inside its cell */
  font-weight: 600;
  color: #fff;
  user-select: none;
}
.cell-dense  { background: var(--dense); }
.cell-coset0 { background: var(--coset0); }
.cell-coset1 { background: var(--coset1); }
.cell-empty  { background: var(--non-lattice); }

/* stage three: two grids side by side */
.split { display: flex; gap: clamp(16px, 5vw, 48px); align-items: flex-start; justify-content: center; }
.split-label { font-size: 0.9rem; font-weight: 600; color: var(--muted); margin-bottom: 8px; text-align: center; }

.figure-button {
  display: block;
  margin: 1.2rem auto 0;
  padding: 0.65rem 1.3rem;
  font: 600 0.95rem var(--sans);
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}
.figure-button:hover { filter: brightness(1.08); }
.figure-button:focus-visible { outline: 3px solid var(--winner); outline-offset: 2px; }

.figure-description { margin: 1.4rem 0 0; font-size: 1rem; line-height: 1.55; color: var(--ink); }

/* ---------- Section 1: generating the lattice ---------- */
.range-row { display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem 1.2rem; font-size: 0.95rem; }
.range-row label strong { display: inline-block; min-width: 1ch; color: var(--accent); }
.range-row input[type="range"] { flex: 1 1 10rem; max-width: 18rem; accent-color: var(--accent); }

.figure-formula { margin: 0.9rem 0 0; font-family: var(--mono); font-size: 0.95rem; color: var(--muted); }

.s01-body { display: grid; grid-template-columns: minmax(0, 21rem) minmax(0, 1fr); max-width: 40rem; margin-left: auto; margin-right: auto; gap: 1.2rem; align-items: stretch; margin-top: 1rem; }
.s01-plot svg { display: block; width: 100%; height: auto; background: #fbfaf6; border: 1px solid var(--line); border-radius: 8px; }
.sites-plot .axis { stroke: #c9c3b4; stroke-width: 1.5; }
.sites-plot .grid-dot { fill: #d9d4c6; }
.sites-plot .site-dot { fill: var(--dense); }
.sites-plot .site-dot:hover { fill: var(--accent); }

.s01-listwrap { position: relative; }
.s01-list {
  position: absolute; inset: 0; margin: 0; padding: 0.8rem 1rem; overflow: auto;
  font: 0.85rem/1.5 var(--mono); color: var(--ink);
  background: #f4f1ea; border: 1px solid var(--line); border-radius: 8px;
  white-space: pre-wrap;
}
@media (max-width: 640px) { .s01-body { grid-template-columns: 1fr; } .s01-listwrap { height: 14rem; } }

/* ---------- lattice figures (Sections 3-8): cells shrink so the figure always fits the text width ---------- */
.lattice-fig { container-type: inline-size; --gap: 3px; --pad-between: 2.5rem; }
/* cell = (available width - gaps between the grids) / number of columns across, never more than 3.3rem.
   The size is set on the children of the card so that the narrow-screen rule below can change it. */
.lattice-fig > * {
  --cell: clamp(1.1rem, calc((100cqw - var(--pad-between) - var(--across, 8) * var(--gap)) / var(--across, 8)), 2.8rem);
}
/* narrow screens (phones): the two views stack one above the other, so each one gets the full width */
@container (max-width: 560px) {
  .lattice-fig > * {
    --cell: clamp(1.1rem, calc((100cqw - 1rem - var(--across, 8) * var(--gap) / 2) / (var(--across, 8) / 2)), 2.8rem);
  }
  .two-views { flex-direction: column; align-items: center; gap: 1.4rem; }
}
.lattice-fig .cell { font-size: calc(var(--cell) * 0.42); }
.lattice-fig .cell.small { font-size: calc(var(--cell) * 0.27); }
.cell.clickable { cursor: pointer; }
.cell.clickable:hover { filter: brightness(0.93); }
.cell.clickable:focus-visible { outline: 3px solid var(--winner); outline-offset: 1px; }

.two-views { display: flex; justify-content: center; align-items: flex-start; gap: 1.2rem; flex-wrap: nowrap; }
.coset-pair { display: flex; gap: 0.8rem; align-items: flex-start; }
.panel { min-width: 0; }
.panel-label { font-size: 0.85rem; font-weight: 600; color: var(--muted); margin-bottom: 6px; text-align: center; }
.figure-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 1.2rem; align-items: flex-start; margin-top: 1rem; }
.controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.6rem; margin-top: 1rem; }
.controls .figure-button, .input-row .figure-button { margin: 0; }
.figure-button.secondary { background: #fff; color: var(--ink); border: 1px solid var(--line); }
.figure-button:disabled { opacity: 0.45; cursor: default; filter: none; }

.readout {
  margin: 1.1rem 0 0; padding: 0.8rem 1rem; white-space: pre-wrap; overflow-wrap: anywhere;
  font: 0.85rem/1.5 var(--mono); color: var(--ink); background: #f4f1ea; border: 1px solid var(--line); border-radius: 8px;
}
.sliders { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.6rem 2rem; margin-top: 1rem; }
.slider { display: flex; align-items: center; gap: 0.6rem; font-size: 0.92rem; }
.slider input[type="range"] { width: 11rem; max-width: 40vw; accent-color: var(--accent); }
.slider output { min-width: 2.6ch; font-weight: 600; color: var(--accent); font-variant-numeric: tabular-nums; }

/* ---------- Section 9: model diagram and runner ---------- */
.scroll-x { overflow-x: auto; }
.diagram9 { display: block; min-width: 640px; width: 100%; height: auto; }
.dg-bold { font: 600 14px var(--sans); fill: var(--ink); }
.dg-axis { font: italic 16px var(--serif, serif); fill: var(--muted); }
.dg-op { font: 500 12px var(--sans); fill: var(--muted); stroke: #fff; stroke-width: 4px; paint-order: stroke; }

.input-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.4rem; align-items: start; }
.input-row .panel { display: flex; flex-direction: column; align-items: center; }
.file-input { max-width: 100%; font: 0.9rem var(--sans); }
.hint { text-align: center; font-size: 0.85rem; line-height: 1.5; color: var(--muted); margin: 0.6rem 0 0; }
.pad { width: min(280px, 100%); aspect-ratio: 1; background: #000; border-radius: 8px; touch-action: none; cursor: crosshair; }
.stage-title { font: 600 1rem var(--sans); margin: 1.2rem 0 0.6rem; text-align: center; }
.thumbs { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; max-width: 30rem; margin: 0 auto; }
.thumbs.single { grid-template-columns: minmax(0, 10rem); justify-content: center; }
.thumb { margin: 0; text-align: center; font-size: 0.72rem; color: var(--muted); }
.thumb canvas { width: 100%; height: auto; aspect-ratio: 1; image-rendering: pixelated; border: 1px solid var(--line); display: block; }
@media (max-width: 640px) { .input-row { grid-template-columns: 1fr; } }

/* ---------- Section 10: BCC in 3D ---------- */
.bcc-wrap { position: relative; width: 100%; min-height: 12rem; }
.bcc-wrap canvas { display: block; border: 1px solid var(--line); border-radius: 8px; touch-action: none; }
.bcc-label { position: absolute; font: 600 0.78rem var(--sans); color: var(--ink); background: rgba(255,255,255,0.85); padding: 2px 6px; border-radius: 4px; pointer-events: none; }

.swipe-hint { display: none; margin: 0.5rem 0 0; font-size: 0.8rem; color: var(--muted); text-align: center; }
@media (max-width: 760px) { .swipe-hint { display: block; } .diagram9 { min-width: 600px; } }

/* ---------- Section 2 figure: its size is set here (js/figures/s02.js draws it) ----------
   --cell is the width and height of one square. Make the number smaller for a smaller figure. */
.s02-fig { --cell: min(3.2rem, 11vw); --gap: 3px; }
.s02-fig .figure-area { min-height: 0; }
.s02-fig .figure-button { margin-top: 0.9rem; }
.s02-fig .figure-description { margin-top: 0.9rem; }
