@layer reset, tokens, base, layout, components, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { text-size-adjust: 100%; }
  body { margin: 0; }
  img { display: block; max-width: 100%; }
  button, input, select { font: inherit; }
}

@layer tokens {
  :root {
    color-scheme: light dark;
    --paper: #f3efe6;
    --surface: #fffdf8;
    --surface-2: #ece6da;
    --ink: #24231f;
    --muted: #676258;
    --line: #d7cfc0;
    --accent: #9d3f2f;
    --accent-deep: #733024;
    --accent-soft: #f2ddd6;
    --blue: #315f77;
    --green: #446b54;
    --shadow: 0 12px 35px rgb(52 42 24 / 9%);
    --radius: 0.85rem;
    --reading: 54rem;
    --page: 86rem;
  }

  [data-theme="dark"] {
    --paper: #171815;
    --surface: #20221e;
    --surface-2: #2a2d27;
    --ink: #eee9df;
    --muted: #b8b1a4;
    --line: #41453d;
    --accent: #e18a74;
    --accent-deep: #f2a18d;
    --accent-soft: #422a25;
    --blue: #8bb9d0;
    --green: #8fbea0;
    --shadow: 0 15px 40px rgb(0 0 0 / 25%);
  }
}

@layer base {
  html { background: var(--paper); scroll-behavior: smooth; }
  body {
    min-height: 100vh;
    color: var(--ink);
    background:
      radial-gradient(circle at 5% 0%, color-mix(in srgb, var(--accent-soft) 65%, transparent), transparent 28rem),
      var(--paper);
    font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    line-height: 1.55;
  }
  a { color: var(--accent-deep); text-underline-offset: 0.18em; }
  button, select, input {
    color: var(--ink);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 0.55rem;
  }
  button, .button-link { min-height: 2.75rem; }
  button { cursor: pointer; padding: 0.65rem 0.9rem; }
  button:hover, .button-link:hover { border-color: var(--accent); }
  :focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 65%, white); outline-offset: 3px; }
  h1, h2, h3 { line-height: 1.15; text-wrap: balance; }
  h1 { font-family: ui-serif, Georgia, Cambria, "Times New Roman", serif; font-size: clamp(2.1rem, 5vw, 4.6rem); letter-spacing: -0.035em; margin: 0.15em 0 0.3em; }
  h2 { font-size: clamp(1.35rem, 2.5vw, 2rem); }
  p { text-wrap: pretty; }
  code { font-size: 0.92em; }
  time { font-variant-numeric: tabular-nums; }
}

@layer layout {
  .site-header {
    width: min(calc(100% - 2rem), var(--page));
    min-height: 4.5rem;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: 1.25rem;
  }
  .site-brand { color: var(--ink); font-family: ui-serif, Georgia, serif; font-size: 1.15rem; font-weight: 750; text-decoration: none; margin-right: auto; }
  .site-header nav { display: flex; align-items: center; gap: 1rem; }
  .site-header nav a { color: var(--muted); font-weight: 650; text-decoration: none; }
  .site-header nav a[aria-current="page"] { color: var(--accent-deep); }
  .theme-toggle { border: 0; background: transparent; min-width: 2.75rem; }
  main { min-height: 70vh; }
  .content-width, .page-shell { width: min(calc(100% - 2rem), var(--page)); margin-inline: auto; }
  .content-width-narrow { width: min(calc(100% - 2rem), var(--reading)); margin-inline: auto; }
  .page-shell, .reading-page { padding-block: clamp(2rem, 5vw, 4.5rem); }
  .section-block { padding-block: clamp(2rem, 5vw, 4rem); }
  .site-footer { width: min(calc(100% - 2rem), var(--page)); margin: 3rem auto 0; padding: 1.5rem 0 2.5rem; border-top: 1px solid var(--line); color: var(--muted); font-size: 0.9rem; }
  .site-footer p { display: inline; margin: 0 1rem 0 0; }
  .browse-layout { display: grid; grid-template-columns: minmax(14rem, 19rem) minmax(0, 1fr); gap: clamp(1.5rem, 4vw, 4rem); align-items: start; }
  .filter-shell { position: sticky; top: 1rem; }
  .results-column { min-width: 0; }
  .exam-grid, .course-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr)); gap: 1rem; }
}

@layer components {
  .skip-link { position: fixed; z-index: 100; left: 1rem; top: 0.5rem; transform: translateY(-160%); padding: 0.75rem 1rem; background: var(--ink); color: var(--paper); border-radius: 0.4rem; }
  .skip-link:focus { transform: none; }
  .eyebrow { color: var(--accent-deep); font-size: 0.78rem; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; }
  .lead { max-width: 48rem; color: var(--muted); font-size: clamp(1.05rem, 2vw, 1.35rem); }
  .home-intro { padding-block: clamp(3rem, 9vw, 7rem); }
  .hero-search { max-width: 44rem; margin-top: 2rem; }
  .hero-search label { display: block; font-weight: 700; margin-bottom: 0.45rem; }
  .search-row, .browse-search { display: flex; gap: 0.55rem; }
  .search-row input, .browse-search input { flex: 1; min-width: 0; padding: 0.8rem 0.9rem; }
  .search-row button, .browse-search button, .filter-actions button[type="submit"] { color: white; background: var(--accent-deep); border-color: var(--accent-deep); font-weight: 700; }
  .stat-row { display: flex; flex-wrap: wrap; gap: 1rem; margin-top: 2.25rem; }
  .stat-row div { min-width: 8rem; padding-right: 1.5rem; border-right: 1px solid var(--line); }
  .stat-row strong, .stat-row span { display: block; }
  .stat-row strong { font: 750 1.7rem/1 ui-serif, Georgia, serif; }
  .stat-row span { color: var(--muted); font-size: 0.85rem; margin-top: 0.35rem; }
  .section-heading, .reading-heading, .results-heading { display: flex; justify-content: space-between; align-items: end; gap: 1rem; }
  .section-heading h2, .results-heading h2 { margin-block: 0.2rem; }
  .course-card, .exam-card { display: flex; flex-direction: column; gap: 0.45rem; min-height: 10rem; padding: 1.25rem; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); text-decoration: none; }
  .course-card { border-top: 4px solid var(--accent); }
  .course-card:nth-child(2n) { border-top-color: var(--blue); }
  .course-card:nth-child(3n) { border-top-color: var(--green); }
  .course-card h3 { margin: auto 0 0; font-family: ui-serif, Georgia, serif; font-size: 1.35rem; }
  .course-card > span:last-child, .exam-card > span { color: var(--muted); font-size: 0.9rem; }
  .course-code { color: var(--muted); font-size: 0.75rem; font-weight: 800; letter-spacing: 0.1em; }
  .page-heading { max-width: 50rem; margin-bottom: 2rem; }
  .page-heading p { color: var(--muted); }
  .browse-toolbar { display: flex; align-items: end; gap: 0.75rem; margin-bottom: 0.8rem; }
  .browse-search { flex: 1; max-width: 46rem; }
  .sort-control { display: grid; gap: 0.25rem; color: var(--muted); font-size: 0.82rem; }
  .sort-control select { min-height: 2.75rem; padding: 0.55rem; }
  .filter-open { display: none; }
  .active-filters { display: flex; flex-wrap: wrap; gap: 0.4rem; min-height: 2.2rem; margin-bottom: 1rem; }
  .filter-chip { display: inline-flex; align-items: center; gap: 0.35rem; min-height: 2rem; padding: 0.25rem 0.6rem; color: var(--accent-deep); background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line)); border-radius: 999px; font-size: 0.82rem; }
  .filter-panel { padding: 1rem; background: color-mix(in srgb, var(--surface) 92%, transparent); border: 1px solid var(--line); border-radius: var(--radius); }
  .filter-panel fieldset, .filter-panel details { margin: 0; padding: 0.85rem 0; border: 0; border-bottom: 1px solid var(--line); }
  .filter-panel legend, .filter-panel summary { font-weight: 750; cursor: pointer; }
  .facet-list { max-height: 18rem; overflow: auto; padding-top: 0.5rem; }
  .check-row { display: grid; grid-template-columns: auto 1fr auto; gap: 0.5rem; align-items: center; min-height: 2.1rem; color: var(--muted); font-size: 0.88rem; }
  .check-row input { width: 1.05rem; height: 1.05rem; accent-color: var(--accent); }
  .year-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
  .year-fields legend { grid-column: 1 / -1; }
  .year-fields label { display: grid; gap: 0.2rem; color: var(--muted); font-size: 0.82rem; }
  .year-fields input { width: 100%; padding: 0.45rem; }
  .filter-actions { display: flex; gap: 0.5rem; padding-top: 1rem; }
  .mobile-filter-heading { display: none; }
  .status-note { color: var(--muted); font-size: 0.85rem; }
  .problem-results { display: grid; gap: 1rem; }
  .problem-card, .problem-detail, .exam-problem { padding: clamp(1rem, 3vw, 1.7rem); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
  .problem-card-header { display: flex; justify-content: space-between; align-items: start; gap: 1rem; margin-bottom: 0.5rem; }
  .problem-card-header > div { min-width: 0; }
  .problem-card-header h3 { margin: 0.2rem 0; font-family: ui-serif, Georgia, serif; }
  .problem-card-header h3 a { color: var(--ink); text-decoration: none; }
  .problem-card-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 0.3rem 0.55rem; color: var(--muted); font-size: 0.82rem; }
  .card-exam-link { flex: 0 0 auto; color: var(--muted); font-size: 0.78rem; font-weight: 700; white-space: nowrap; }
  .question-heading, .part-heading { display: flex; justify-content: space-between; gap: 1rem; align-items: baseline; font-weight: 750; }
  .problem-card .question-heading .question-label,
  .problem-detail .question-heading .question-label { display: none; }
  .problem-part { margin: 1rem 0 0 0.85rem; padding-left: 1rem; border-left: 2px solid var(--line); }
  .part-label { color: var(--accent-deep); }
  .problem-prose { overflow-x: auto; }
  .problem-prose > :first-child { margin-top: 0; }
  .problem-prose > :last-child { margin-bottom: 0; }
  .problem-prose .katex-display { overflow-x: auto; overflow-y: hidden; padding-block: 0.25rem; }
  .problem-prose blockquote { margin: 1rem 0; padding: 0.8rem 1rem; background: var(--surface-2); border-left: 3px solid var(--accent); }
  .centered { text-align: center; }
  .table-scroll { max-width: 100%; overflow-x: auto; }
  table { width: 100%; border-collapse: collapse; text-align: left; }
  th, td { padding: 0.45rem 0.65rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
  .answer-blank { display: inline-block; width: calc(var(--blank-ch) * 0.6em); min-width: 2em; border-bottom: 1px solid currentColor; }
  .text-icon { display: inline-block; width: 0.9em; height: 1em; object-fit: contain; vertical-align: -0.12em; }
  .problem-figure { margin: 1.2rem auto; max-width: 42rem; }
  .problem-figure[data-figure-id="mm2001-discrete-mathematics-2025-10-22-p5-figure-1"] { max-width: 18rem; }
  .figure-open { width: 100%; padding: 0.5rem; background: transparent; }
  .figure-open img { width: 100%; max-height: 32rem; object-fit: contain; }
  .tag-row { display: flex; flex-wrap: wrap; gap: 0.3rem; margin-top: 0.85rem; }
  .tag-row-inline { display: inline-flex; margin-top: 0; }
  .tag { display: inline-flex; align-items: center; min-height: 1.5rem; padding: 0.1rem 0.42rem; border-radius: 999px; font-size: 0.68rem; font-weight: 700; text-decoration: none; }
  .tag { color: var(--blue); background: color-mix(in srgb, var(--blue) 12%, transparent); }
  .solution-panel { margin-top: 1rem; border: 1px solid var(--line); border-radius: 0.65rem; background: color-mix(in srgb, var(--surface-2) 72%, transparent); }
  .solution-panel > summary { display: flex; align-items: center; gap: 0.5rem; min-height: 2.75rem; padding: 0.65rem 0.85rem; color: var(--accent-deep); font-weight: 750; cursor: pointer; list-style: none; }
  .solution-panel > summary::-webkit-details-marker { display: none; }
  .solution-panel > summary::before { content: "＋"; width: 1.1rem; color: var(--muted); }
  .solution-panel[open] > summary::before { content: "−"; }
  .solution-hide, .solution-panel[open] .solution-show { display: none; }
  .solution-panel[open] .solution-hide { display: inline; }
  .solution-info { margin-left: auto; color: var(--muted); font-weight: 500; cursor: help; }
  .solution-content { padding: 1rem; border-top: 1px solid var(--line); }
  .solution-loading { color: var(--muted); }
  .solution-prose { overflow-x: auto; }
  .solution-prose > :first-child { margin-top: 0; }
  .solution-prose > :last-child { margin-bottom: 0; }
  .solution-prose .katex-display { overflow-x: auto; overflow-y: hidden; padding-block: 0.25rem; }
  .solution-list { padding-left: 1.6rem; }
  .solution-list-alpha { list-style-type: lower-alpha; }
  .solution-code { max-width: 100%; overflow-x: auto; padding: 0.85rem; background: var(--paper); border: 1px solid var(--line); border-radius: 0.45rem; }
  .problem-detail + .solution-panel { margin-top: 1rem; }
  .load-more { display: block; margin: 1.25rem auto 0; }
  .empty-state { padding: 2rem; text-align: center; color: var(--muted); background: var(--surface); border-radius: var(--radius); }
  .breadcrumbs { display: flex; flex-wrap: wrap; gap: 0.55rem; color: var(--muted); font-size: 0.86rem; }
  .reading-heading { margin: 1rem 0; align-items: start; }
  .reading-heading h1 { font-size: clamp(2rem, 5vw, 3.6rem); }
  .metadata-row { display: flex; flex-wrap: wrap; gap: 0.8rem; margin-bottom: 1.5rem; color: var(--muted); font-size: 0.9rem; }
  .page-actions { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 1.5rem; }
  .button-link, .button-secondary { display: inline-flex; align-items: center; justify-content: center; padding: 0.6rem 0.85rem; background: var(--surface); border: 1px solid var(--line); border-radius: 0.55rem; color: var(--ink); font-weight: 700; text-decoration: none; }
  .button-link { color: white; background: var(--accent-deep); border-color: var(--accent-deep); }
  .exam-group { margin-top: 3rem; }
  .exam-card { min-height: 9rem; }
  .exam-card strong { font-family: ui-serif, Georgia, serif; font-size: 1.15rem; }
  .exam-problems { display: grid; gap: 1.25rem; margin-top: 1.5rem; }
  .exam-problem .question-heading { display: none; }
  .exam-problem-title { display: flex; gap: 0.45rem; margin-bottom: 1rem; font-weight: 750; }
  .heading-permalink { color: var(--muted); text-decoration: none; }
  .heading-permalink:hover { color: var(--accent-deep); text-decoration: underline; }
  .prose-page h2 { margin-top: 2.2rem; }
  .figure-dialog { width: min(94vw, 70rem); max-height: 92vh; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); color: var(--ink); }
  .figure-dialog::backdrop { background: rgb(0 0 0 / 68%); }
  .figure-dialog img { width: 100%; max-height: calc(92vh - 4rem); object-fit: contain; padding: 1rem; }
  .dialog-bar { display: flex; justify-content: space-between; align-items: center; padding: 0.55rem 0.75rem; border-bottom: 1px solid var(--line); }
  .practice-layout { display: grid; grid-template-columns: minmax(14rem, 19rem) minmax(0, 1fr); gap: clamp(1.5rem, 4vw, 4rem); align-items: start; }
  .practice-controls { display: grid; gap: 0.55rem; padding: 1rem; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
  .practice-controls label, .answer-form > label { font-weight: 750; }
  .practice-controls select { width: 100%; min-height: 2.75rem; padding: 0.55rem; margin-bottom: 0.55rem; }
  .practice-controls button, .answer-row button { color: white; background: var(--accent-deep); border-color: var(--accent-deep); font-weight: 700; }
  .practice-card { min-width: 0; padding: clamp(1rem, 3vw, 1.7rem); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
  .practice-card > header { display: flex; justify-content: space-between; align-items: start; gap: 1rem; }
  .practice-card h2 { margin-top: 0.2rem; }
  .difficulty { flex: 0 0 auto; padding: 0.2rem 0.55rem; color: var(--muted); background: var(--surface-2); border-radius: 999px; font-size: 0.78rem; }
  .practice-question { margin: 1.5rem 0; font-size: 1.05rem; }
  .practice-figure { max-width: 19rem; margin: 1rem auto 1.5rem; }
  .practice-figure img { display: block; width: 100%; max-height: 15rem; object-fit: contain; }
  .answer-form { padding: 1rem; background: var(--surface-2); border-radius: 0.65rem; }
  .answer-row { display: flex; gap: 0.55rem; margin-top: 0.45rem; }
  .answer-row input { flex: 1; min-width: 0; padding: 0.7rem 0.8rem; }
  .answer-hint, .answer-feedback { margin: 0.45rem 0 0; color: var(--muted); font-size: 0.85rem; }
  .answer-feedback.is-correct { color: var(--green); font-weight: 750; }
  .answer-feedback.is-incorrect { color: var(--accent-deep); font-weight: 750; }
  .practice-solution ol { padding-left: 1.4rem; }
  .practice-solution li + li { margin-top: 0.7rem; }
  .practice-final-answer { font-weight: 750; }
  .practice-inspiration { margin-top: 1.5rem; padding-top: 0.5rem; border-top: 1px solid var(--line); }
  .practice-inspiration h3 { font-size: 1rem; }
  .practice-inspiration ul { display: flex; flex-wrap: wrap; gap: 0.5rem 1.2rem; padding-left: 1.2rem; }
  .practice-callout { display: flex; justify-content: space-between; align-items: center; gap: 1.5rem; padding: clamp(1.2rem, 4vw, 2rem); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
  .practice-callout h2 { margin: 0.2rem 0; }
  .practice-callout p:last-child { margin-bottom: 0; color: var(--muted); }
  .practice-callout .button-link { flex: 0 0 auto; }
}

@layer utilities {
  .sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
}

@media (max-width: 799px) {
  .site-header { flex-wrap: wrap; padding-block: 0.7rem; gap: 0.55rem; }
  .site-header nav { order: 3; width: 100%; flex-wrap: wrap; justify-content: flex-start; gap: 0.55rem 1rem; border-top: 1px solid var(--line); padding-top: 0.6rem; }
  .browse-toolbar { position: sticky; z-index: 10; top: 0; flex-wrap: wrap; padding-block: 0.6rem; background: color-mix(in srgb, var(--paper) 92%, transparent); backdrop-filter: blur(8px); }
  .browse-search { order: -1; flex-basis: 100%; }
  .filter-open { display: inline-flex; }
  .browse-layout { display: block; }
  .filter-shell { display: none; position: fixed; z-index: 50; inset: 0; padding: 0; background: rgb(0 0 0 / 48%); }
  .filter-shell.is-open { display: flex; align-items: end; }
  .filter-panel { width: 100%; max-height: 94vh; overflow: auto; padding: 1rem; border-radius: 1rem 1rem 0 0; background: var(--surface); }
  .mobile-filter-heading { position: sticky; z-index: 2; top: -1rem; display: flex; justify-content: space-between; align-items: center; padding-block: 0.65rem; background: var(--surface); border-bottom: 1px solid var(--line); }
  .mobile-filter-heading h2 { margin: 0; }
  .filter-actions { position: sticky; bottom: -1rem; padding-block: 0.75rem; background: var(--surface); }
  .filter-actions > * { flex: 1; }
  .problem-card { padding: 1rem; }
  .problem-card-header, .reading-heading { align-items: start; }
  .problem-part { margin-left: 0.25rem; padding-left: 0.75rem; }
  .section-heading { align-items: start; }
  .section-heading .text-link { display: none; }
  .practice-layout { display: block; }
  .practice-controls { margin-bottom: 1rem; }
  .answer-row { align-items: stretch; flex-direction: column; }
  .practice-callout { align-items: start; flex-direction: column; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

@media print {
  :root { --paper: white; --surface: white; --ink: black; --muted: #333; --line: #bbb; }
  .site-header, .site-footer, .browse-toolbar, .filter-shell, .active-filters, .page-actions, .figure-dialog, .heading-permalink { display: none !important; }
  .problem-card, .problem-detail, .exam-problem { box-shadow: none; break-inside: avoid; }
  a { color: inherit; text-decoration: none; }
}
