/*
  Component library. Build screens from these classes; do not write one-off styles.
  Layout:  .container .stack .cluster .grid .split      Surfaces: .card .panel .alert
  Controls: .btn(.btn-primary|-secondary|-ghost|-danger|-sm|-lg|-icon) .field .input .select .textarea .check
  Data:    .table .badge(.badge-success|-warning|-danger|-info) .stat .list .avatar .tabs
  States:  .skeleton .spinner .empty .toast  Overlay: dialog.dialog
*/

/* ---------- layout ---------- */
.container {
  width: 100%;
  max-width: var(--container);
  margin-inline: auto;
  padding-inline: var(--space-4);
}
.page {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  padding-block: var(--space-6) var(--space-8);
  outline: none;
}
.stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.stack-sm {
  gap: var(--space-2);
}
.stack-lg {
  gap: var(--space-6);
}
.cluster {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
}
.cluster-between {
  justify-content: space-between;
}
.grid {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 16rem), 1fr));
}
.split {
  display: grid;
  gap: var(--space-5);
  grid-template-columns: minmax(0, 1fr);
}
@media (min-width: 52rem) {
  .split {
    grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  }
}
.grow {
  flex: 1 1 auto;
}
.muted {
  color: var(--text-muted);
}
.small {
  font-size: var(--text-sm);
}
.center {
  text-align: center;
}
.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.skip-link {
  position: absolute;
  left: var(--space-3);
  top: -4rem;
  z-index: 100;
  background: var(--surface);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.skip-link:focus {
  top: var(--space-3);
}

/* ---------- app shell ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--header-h);
  background: color-mix(in oklab, var(--bg) 82%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
}
.app-header-inner {
  display: flex;
  align-items: center;
  gap: var(--space-5);
  height: 100%;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text);
  font-weight: 750;
  letter-spacing: -0.02em;
  text-decoration: none;
}
.brand:hover {
  color: var(--text);
}
.brand-mark {
  width: 1.4rem;
  height: 1.4rem;
  border-radius: 0.45rem;
  background: var(--brand-gradient);
  box-shadow: var(--shadow-sm);
}
.nav {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-inline-end: auto;
  overflow-x: auto;
}
.nav a {
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 550;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  text-decoration: none;
  white-space: nowrap;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.nav a:hover {
  background: var(--surface-2);
  color: var(--text);
}
.nav a[aria-current='page'] {
  background: var(--brand-soft);
  color: var(--brand);
}

/* ---------- surfaces ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-5);
}
.card-interactive {
  transition: transform var(--duration) var(--ease), box-shadow var(--duration) var(--ease), border-color var(--duration) var(--ease);
}
.card-interactive:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow);
  border-color: var(--border-strong);
}
.card > :last-child,
.panel > :last-child {
  margin-bottom: 0;
}
.panel {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-4);
}
.hero {
  padding-block: var(--space-6);
}
.hero h1 {
  font-size: clamp(2rem, 5vw, 3.25rem);
}
.hero p {
  max-width: 40rem;
  font-size: var(--text-lg);
  color: var(--text-muted);
}
.eyebrow {
  display: inline-block;
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--brand);
  margin-bottom: var(--space-2);
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 2.5rem;
  padding: 0 var(--space-4);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: var(--text-sm);
  font-weight: 600;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
  transition: background var(--duration) var(--ease), border-color var(--duration) var(--ease), transform var(--duration) var(--ease);
}
.btn:hover {
  background: var(--surface-2);
  color: var(--text);
}
.btn:active {
  transform: translateY(1px);
}
.btn[disabled],
.btn[aria-disabled='true'] {
  opacity: 0.55;
  cursor: not-allowed;
  pointer-events: none;
}
.btn-primary {
  background: var(--brand);
  border-color: var(--brand);
  color: var(--on-brand);
}
.btn-primary:hover {
  background: var(--brand-hover);
  border-color: var(--brand-hover);
  color: var(--on-brand);
}
.btn-secondary {
  background: var(--brand-soft);
  border-color: var(--brand-border);
  color: var(--brand);
}
.btn-secondary:hover {
  background: var(--brand-soft);
  border-color: var(--brand);
  color: var(--brand);
}
.btn-ghost {
  background: transparent;
  border-color: transparent;
}
.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: var(--on-brand);
}
.btn-danger:hover {
  background: var(--danger);
  color: var(--on-brand);
  filter: brightness(0.92);
}
.btn-sm {
  min-height: 2rem;
  padding: 0 var(--space-3);
  font-size: var(--text-xs);
}
.btn-lg {
  min-height: 3rem;
  padding: 0 var(--space-5);
  font-size: var(--text-base);
}
.btn-icon {
  width: 2.5rem;
  padding: 0;
}
.btn svg {
  width: 1.1em;
  height: 1.1em;
}

/* ---------- forms ---------- */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.field label,
.label {
  font-size: var(--text-sm);
  font-weight: 600;
}
.field .hint {
  font-size: var(--text-xs);
  color: var(--text-muted);
}
.field .error {
  font-size: var(--text-xs);
  color: var(--danger);
}
.input,
.select,
.textarea {
  width: 100%;
  min-height: 2.5rem;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: var(--text-sm);
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.textarea {
  min-height: 6rem;
  resize: vertical;
}
.input::placeholder,
.textarea::placeholder {
  color: var(--text-muted);
}
.input:hover,
.select:hover,
.textarea:hover {
  border-color: var(--text-muted);
}
.input:focus-visible,
.select:focus-visible,
.textarea:focus-visible {
  border-color: var(--brand);
  box-shadow: var(--focus-ring);
}
.input[aria-invalid='true'],
.textarea[aria-invalid='true'] {
  border-color: var(--danger);
}
.check {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  cursor: pointer;
}
.check input {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: var(--brand);
}

/* ---------- data display ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 0.15rem 0.6rem;
  border-radius: var(--radius-full);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 650;
  white-space: nowrap;
}
.badge-brand {
  background: var(--brand-soft);
  border-color: var(--brand-border);
  color: var(--brand);
}
.badge-success {
  background: var(--success-soft);
  border-color: transparent;
  color: var(--success);
}
.badge-warning {
  background: var(--warning-soft);
  border-color: transparent;
  color: var(--warning);
}
.badge-danger {
  background: var(--danger-soft);
  border-color: transparent;
  color: var(--danger);
}
.badge-info {
  background: var(--info-soft);
  border-color: transparent;
  color: var(--info);
}
.table-wrap {
  overflow-x: auto;
  /* A grid or flex parent gives its items an automatic minimum, which would let a wide table push the
     page sideways instead of scrolling inside this box. */
  min-width: 0;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
}
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}
.table th,
.table td {
  padding: var(--space-3) var(--space-4);
  text-align: start;
  border-bottom: 1px solid var(--border);
}
.table th {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  background: var(--surface-2);
}
.table tbody tr:last-child td {
  border-bottom: 0;
}
.table tbody tr:hover {
  background: var(--surface-2);
}
.stat {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.stat-value {
  font-size: var(--text-2xl);
  font-weight: 750;
  letter-spacing: -0.02em;
}
.stat-label {
  font-size: var(--text-sm);
  color: var(--text-muted);
}
.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}
.list > li {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--border);
}
.list > li:last-child {
  border-bottom: 0;
}
.avatar {
  display: inline-grid;
  place-items: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--radius-full);
  background: var(--brand-soft);
  color: var(--brand);
  font-size: var(--text-sm);
  font-weight: 700;
}
.tabs {
  display: flex;
  gap: var(--space-1);
  border-bottom: 1px solid var(--border);
}
.tabs button {
  padding: var(--space-2) var(--space-4);
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.tabs button[aria-selected='true'] {
  color: var(--brand);
  border-bottom-color: var(--brand);
}

/* ---------- feedback ---------- */
.alert {
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: var(--text-sm);
}
.alert-info {
  background: var(--info-soft);
  border-color: transparent;
}
.alert-success {
  background: var(--success-soft);
  border-color: transparent;
}
.alert-warning {
  background: var(--warning-soft);
  border-color: transparent;
}
.alert-danger {
  background: var(--danger-soft);
  border-color: transparent;
}
.empty {
  display: grid;
  justify-items: center;
  gap: var(--space-2);
  padding: var(--space-7) var(--space-4);
  text-align: center;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
}
.empty h3 {
  margin: 0;
}
.empty p {
  margin: 0 0 var(--space-3);
  color: var(--text-muted);
  max-width: 28rem;
}
.empty-icon {
  display: grid;
  place-items: center;
  width: 3rem;
  height: 3rem;
  border-radius: var(--radius-full);
  background: var(--brand-soft);
  color: var(--brand);
  font-size: var(--text-xl);
}
.skeleton {
  display: block;
  height: 1rem;
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2));
  background-size: 200% 100%;
  animation: shimmer 1.4s linear infinite;
}
.skeleton-block {
  height: 5rem;
  border-radius: var(--radius-lg);
}
@keyframes shimmer {
  to {
    background-position: -200% 0;
  }
}
.spinner {
  display: inline-block;
  width: 1.1rem;
  height: 1.1rem;
  border: 2px solid var(--border-strong);
  border-top-color: var(--brand);
  border-radius: var(--radius-full);
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- toast + dialog ---------- */
.toasts {
  position: fixed;
  inset: auto var(--space-4) var(--space-4) auto;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: min(24rem, calc(100vw - 2rem));
}
.toast {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  animation: toast-in 220ms var(--ease);
}
.toast-success {
  background: var(--success);
  color: var(--on-brand);
}
.toast-error {
  background: var(--danger);
  color: var(--on-brand);
}
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}
dialog.dialog {
  width: min(28rem, calc(100vw - 2rem));
  padding: var(--space-5);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
dialog.dialog::backdrop {
  background: var(--overlay);
  backdrop-filter: blur(2px);
}
dialog.dialog h3 {
  margin-bottom: var(--space-2);
}

/* ---------- file preview ---------- */
dialog.dialog-wide {
  width: min(58rem, calc(100vw - 2rem));
  max-height: calc(100vh - 3rem);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: var(--space-3);
}
.preview-head h3 {
  margin: 0;
}
.preview-body {
  overflow: auto;
  overscroll-behavior: contain;
  display: grid;
  gap: var(--space-3);
  justify-items: center;
  background: var(--surface-2);
  padding: var(--space-3);
  border: 1px solid var(--border);
}
.preview-pages {
  display: grid;
  gap: var(--space-4);
  justify-items: center;
}
.preview-page {
  max-width: 100%;
  height: auto;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.preview-image {
  max-width: 100%;
  height: auto;
}
.preview-text {
  width: 100%;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
}
.preview-count {
  position: sticky;
  top: 0;
}
.preview-error {
  color: var(--danger);
}

/* ---------- draft review ---------- */
.draft-card {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-4);
  border: 1px solid var(--border);
  background: var(--surface-2);
}
.draft-front {
  font-weight: 600;
  margin: 0;
}
.draft-back {
  margin: 0;
}
.draft-options {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--space-1);
}
.draft-options li {
  padding: var(--space-1) var(--space-2);
  border: 1px solid var(--border);
  background: var(--surface);
}
.draft-options li.is-answer {
  border-color: var(--success);
  font-weight: 600;
}
.draft-quote {
  margin: 0;
  padding-left: var(--space-3);
  border-left: 3px solid var(--brand-border);
}
.draft-quote blockquote {
  margin: 0 0 var(--space-1);
  font-style: italic;
}
.link-btn {
  padding: 0;
  border: 0;
  background: none;
  color: var(--brand);
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
}

/* ---------- the course slide beside the lesson ---------- */
.guide-stage {
  display: grid;
  gap: var(--space-4);
  /* minmax(0) everywhere: a canvas has an intrinsic width and would otherwise push the page sideways. */
  grid-template-columns: minmax(0, 1fr);
}
.guide-stage > *,
.guide-stage .stack,
.guide-stage .slide-split > * {
  min-width: 0;
}
/* `1fr` keeps an automatic minimum, so on a narrow screen the lesson column has to be told it may shrink. */
.guide-stage .slide-split {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr);
}
@media (max-width: 52rem) {
  .guide-stage .slide-split {
    grid-template-columns: minmax(0, 1fr);
  }
}
@media (min-width: 60rem) {
  .guide-stage.has-source {
    grid-template-columns: minmax(0, 1fr) minmax(0, 0.9fr);
    align-items: start;
  }
  /* With the real slide beside it, the lesson keeps one column and its diagram sits under the text. */
  .guide-stage.has-source .slide-split {
    grid-template-columns: minmax(0, 1fr);
  }
}
.source-pane {
  padding: var(--space-3);
  min-width: 0;
}
@media (min-width: 60rem) {
  .source-pane {
    position: sticky;
    top: var(--space-4);
  }
}
.source-pane-figure {
  margin: 0;
  display: grid;
  gap: var(--space-2);
  min-width: 0;
}
.source-pane-canvas {
  display: grid;
  justify-items: center;
  min-width: 0;
  overflow: hidden;
  background: var(--surface-2);
  padding: var(--space-2);
  min-height: 12rem;
}
.source-pane-canvas .preview-page {
  max-width: 100%;
  height: auto;
  box-shadow: var(--shadow);
}
.source-pane-foot {
  gap: var(--space-2);
}
.pane-status {
  padding: var(--space-4);
  text-align: center;
}
.chip-open {
  cursor: pointer;
  border-style: dashed;
}
.chip-open:hover,
.chip-open:focus-visible {
  border-style: solid;
  border-color: var(--brand);
  color: var(--brand);
}
.chip-page {
  opacity: 0.75;
}
.preview-more {
  gap: var(--space-2);
  padding: var(--space-2) 0;
}

/* A diagram host holds whatever the diagram draws - a wide table, a chart - and must be allowed to shrink
   so that what it holds scrolls inside it instead of widening the page. */
.diagram-host {
  min-width: 0;
  max-width: 100%;
}
.source-pane-text {
  max-height: 60vh;
  overflow: auto;
  background: var(--surface-2);
  padding: var(--space-3);
}
