@layer foundation, components, utilities, responsive;

@layer foundation {
  :root {
    color-scheme: light;
    --royal: #0250cc;
    --royal-deep: #003488;
    --gold: #fdc800;
    --paper: #ffffff;
    --ink: #111827;
    --muted: #4b5563;
    --success: #16a34a;
    --amber: #d97706;
    --danger: #dc2626;
    --wash: #f4f7fd;
    --rule: #d5e0f3;
    --display: "Grenze Gotisch", Georgia, serif;
    --reading: Georgia, "Times New Roman", serif;
    --plain: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --data: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Consolas, monospace;
  }

  *, *::before, *::after { box-sizing: border-box; }
  html { scroll-padding-top: 32px; }
  body { margin: 0; background: var(--paper); color: var(--ink); font: 400 20px/1.55 var(--reading); }
  h1, h2, h3, p, figure, dl, dd, ol { margin: 0; }
  h1, h2, h3 { color: var(--royal); font-family: var(--display); font-weight: 700; text-wrap: balance; }
  h1 { font-size: clamp(40px, 4.2vw, 60px); line-height: 1.08; letter-spacing: -.035em; }
  h2 { font-size: 32px; line-height: 1.18; letter-spacing: -.018em; }
  h3 { font-size: 24px; line-height: 1.25; }
  p, dd { max-width: 65ch; }
  a { color: var(--royal); text-decoration-thickness: 1px; text-underline-offset: 5px; }
  a:hover { color: var(--royal-deep); text-decoration-thickness: 2px; }
  a:focus-visible, summary:focus-visible { outline: 4px solid var(--royal); outline-offset: 5px; }
  a:active { color: var(--royal-deep); }
  ::selection { color: var(--ink); background: var(--gold); }
  img { display: block; max-width: 100%; height: auto; }
  strong { font-weight: 700; }
  section[id] { scroll-margin-top: 32px; }
}

@layer components {
  .wrap { width: min(1200px, calc(100% - 80px)); margin-inline: auto; }
  .skip-link { position: absolute; top: 12px; left: 20px; z-index: 10; padding: 12px 16px; min-height: 48px; color: var(--paper); background: var(--royal); font: 600 16px/1.5 var(--plain); transform: translateY(-180%); }
  .skip-link:focus { transform: translateY(0); }
  .skip-link:focus-visible { outline-offset: 4px; }

  .masthead { min-height: 112px; display: flex; align-items: center; justify-content: space-between; gap: 32px; border-bottom: 2px solid var(--royal); }
  .wordmark { display: inline-flex; align-items: center; gap: 16px; min-height: 48px; padding-block: 8px; font: 700 32px/1.2 var(--display); text-decoration: none; letter-spacing: -.035em; }
  .book-mark { position: relative; display: inline-block; width: 32px; height: 32px; flex: none; border: 2px solid var(--royal); border-radius: 2px 2px 12px 12px; }
  .book-mark::before { content: ""; position: absolute; inset: 5px 7px; border-inline: 2px solid var(--royal); }
  .book-mark::after { content: ""; position: absolute; width: 6px; height: 6px; top: -5px; left: 11px; background: var(--gold); transform: rotate(45deg); }
  .page-nav { display: flex; align-items: stretch; gap: 8px; font: 500 16px/1.5 var(--plain); }
  .page-nav a { display: inline-flex; align-items: center; justify-content: center; gap: 12px; min-height: 48px; padding: 12px 16px; text-decoration: none; border-bottom: 3px solid transparent; }
  .page-nav a:hover { background: var(--wash); border-bottom-color: var(--royal); }
  .page-nav a[aria-current] { border-bottom-color: var(--royal); font-weight: 700; }

  .opening { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); align-items: center; gap: 48px; padding-block: 64px 56px; }
  .opening-copy { padding-block: 24px; }
  .opening-copy .lead { max-width: 41ch; }
  .eyebrow { display: flex; align-items: center; gap: 12px; color: var(--royal); font: 600 16px/1.5 var(--plain); }
  .gold-dot { display: inline-block; flex: none; width: 10px; height: 10px; background: var(--gold); transform: rotate(45deg); }
  .lead { font-size: 22px; line-height: 1.5; }
  .steel-link { width: fit-content; min-height: 56px; display: inline-flex; align-items: center; justify-content: space-between; gap: 32px; padding: 14px 24px; border: 2px solid var(--royal-deep); border-top-color: #6398ec; color: var(--paper); background: var(--royal); box-shadow: 0 5px 0 var(--royal-deep); font: 600 16px/1.5 var(--plain); text-decoration: none; }
  .steel-link span { font-size: 22px; line-height: 1; }
  .steel-link:hover { background: var(--royal-deep); color: var(--paper); border-top-color: var(--royal); }
  .steel-link:active { transform: translateY(3px); box-shadow: 0 2px 0 var(--royal-deep); color: var(--paper); }
  .steel-link[aria-disabled="true"] { color: #4b5563; background: #e5e7eb; border-color: #6b7280; box-shadow: none; cursor: default; }

  .paper-scene { position: relative; width: 100%; max-width: 480px; aspect-ratio: 1; justify-self: end; isolation: isolate; }
  .scene-art { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; color: transparent; font-size: 0; }
  .paper-assembly { position: absolute; inset: 16px; overflow: hidden; background: var(--wash); border-radius: 48% 48% 3px 3px; }
  .paper-arch { position: absolute; top: 10%; bottom: 12%; left: 14%; right: 14%; border: 30px solid var(--royal); border-radius: 50% 50% 0 0; box-shadow: 12px 10px 0 #d8e5fb; }
  .paper-sun { position: absolute; width: 76px; height: 76px; top: 24%; left: 42%; border-radius: 50%; background: var(--gold); box-shadow: 7px 7px 0 #e6b700; }
  .paper-leaf { position: absolute; top: 52%; width: 40%; height: 36%; background: white; border: 1px solid #d7e0ee; box-shadow: 0 9px 0 #ccd8eb, 8px 15px 15px #11182718; }
  .leaf-left { left: 11%; transform: skewY(10deg) rotate(-6deg); clip-path: polygon(0 0, 100% 7%, 98% 100%, 8% 96%, 0 91%, 2% 16%); }
  .leaf-right { right: 10%; transform: skewY(-10deg) rotate(6deg); clip-path: polygon(0 7%, 100% 0, 98% 89%, 91% 99%, 2% 100%); }
  .paper-ribbon { position: absolute; width: 16px; height: 38%; bottom: 4%; left: 49%; background: var(--royal); clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 91%, 0 100%); }
  .paper-scene-notes { max-width: 400px; aspect-ratio: 4 / 3; }
  .paper-scene-notes .paper-assembly { border-radius: 0; background: white; border: 1px solid var(--rule); }
  .paper-scene-notes .paper-arch { top: 18%; bottom: 14%; border-radius: 0; border-width: 2px; transform: rotate(-6deg); background: var(--wash); }
  .paper-scene-notes .paper-sun { width: 48px; height: 48px; left: 57%; top: 28%; }
  .paper-scene-notes .paper-leaf { top: 31%; height: 48%; }
  .paper-scene-notes .paper-ribbon { height: 56%; }

  .chapter-path { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border-block: 1px solid var(--royal); }
  .chapter-path p { display: flex; align-items: center; justify-content: center; gap: 12px; min-height: 72px; padding: 16px 8px; font: 500 16px/1.5 var(--plain); color: var(--royal); }
  .chapter-path p + p { border-left: 1px solid var(--rule); }
  .chapter-path span { color: var(--royal); font-size: 22px; }

  .notebook-layout { display: grid; grid-template-columns: 168px minmax(0, 1fr); align-items: start; gap: 56px; padding-block: 64px 96px; }
  .anchor-rail { position: sticky; top: 32px; border-left: 2px solid var(--royal); padding-block: 8px; font: 500 16px/1.5 var(--plain); }
  .rail-label { margin: 0 0 16px 16px; color: var(--muted); font: 400 16px/1.4 var(--plain); }
  .anchor-rail a { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 48px; padding: 12px 16px; text-decoration: none; }
  .anchor-rail a:hover { background: var(--wash); box-shadow: -2px 0 0 var(--royal); }
  .anchor-rail .rail-next { margin-top: 16px; border-top: 1px solid var(--rule); padding-top: 16px; }
  .notebook-pages { min-width: 0; }
  .chapter + .chapter { margin-top: 80px; padding-top: 56px; border-top: 2px solid var(--royal); }
  .chapter-heading { display: grid; gap: 16px; margin-bottom: 32px; }
  .section-lead { font-size: 22px; line-height: 1.45; }

  .entry { display: grid; grid-template-columns: 48px minmax(0, 1fr); gap: 24px; padding-block: 32px; border-top: 1px solid var(--rule); }
  .entry-letter { color: var(--royal); font: 700 52px/1 var(--display); text-align: center; }
  .ask-line { padding-left: 16px; border-left: 2px solid var(--royal); color: var(--royal-deep); font-style: italic; }
  .source-pair { display: flex; align-items: flex-start; flex-wrap: wrap; gap: 0 16px; max-width: 100%; font: 400 16px/1.5 var(--plain); }
  .source-label { flex: 0 0 auto; min-height: 44px; display: flex; align-items: center; color: var(--muted); }
  .source-pair a { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding-block: 10px; }
  .source-pair a span { flex-shrink: 0; }
  .source-caption { font: 400 16px/1.5 var(--plain); color: var(--muted); }

  .deeper-note { margin-top: 32px; border: 1px solid var(--royal); box-shadow: 4px 4px 0 var(--wash); }
  .deeper-note summary { display: flex; align-items: center; justify-content: space-between; gap: 24px; min-height: 64px; padding: 16px 24px; color: var(--royal); background: var(--paper); font: 600 18px/1.4 var(--plain); cursor: pointer; list-style: none; }
  .deeper-note summary::-webkit-details-marker { display: none; }
  .deeper-note summary:hover { background: var(--wash); }
  .deeper-note summary:active { background: #e5edfb; }
  .detail-sign { position: relative; flex: none; width: 20px; height: 20px; }
  .detail-sign::before, .detail-sign::after { content: ""; position: absolute; background: currentColor; }
  .detail-sign::before { width: 18px; height: 2px; left: 1px; top: 9px; }
  .detail-sign::after { height: 18px; width: 2px; left: 9px; top: 1px; }
  .deeper-note[open] .detail-sign::after { display: none; }
  .deeper-note[open] summary { border-bottom: 1px solid var(--rule); }
  .detail-copy { padding: 24px; }
  .summary-slip { padding: 32px; background: var(--wash); border-left: 4px solid var(--royal); }
  .check-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 32px; margin-block: 32px; }
  .check-note { border-top: 3px solid var(--royal); padding-top: 24px; }
  .lookup-link { display: inline-flex; align-items: center; justify-content: space-between; gap: 16px; width: fit-content; min-height: 48px; padding: 10px 0; font: 600 16px/1.5 var(--plain); }
  .lookup-link span { flex: none; }
  .margin-note { font-style: italic; padding: 8px 0 8px 24px; border-left: 2px solid var(--royal); }
  .caution-list { border-top: 1px solid var(--rule); }
  .caution-note { position: relative; padding: 32px 24px 32px 40px; border-bottom: 1px solid var(--rule); }
  .caution-note::before { content: ""; position: absolute; top: 39px; left: 8px; width: 12px; height: 12px; border: 2px solid var(--amber); transform: rotate(45deg); }
  .caution-note:first-child::before { border-color: var(--danger); }
  .next-leaf { margin-top: 64px; padding: 40px; display: grid; gap: 24px; border: 2px solid var(--royal); background: var(--wash); box-shadow: 8px 8px 0 var(--rule); }

  .breadcrumb { padding-top: 24px; font: 400 16px/1.5 var(--plain); }
  .breadcrumb ol { padding: 0; display: flex; align-items: center; flex-wrap: wrap; gap: 12px; list-style: none; }
  .breadcrumb li { display: flex; align-items: center; gap: 12px; min-height: 44px; }
  .breadcrumb li + li::before { content: "/"; color: var(--royal); }
  .breadcrumb a { display: inline-flex; align-items: center; min-height: 44px; padding-block: 10px; }
  .opening-notes { padding-block: 24px 48px; }
  .opening-notes h1 { max-width: 17ch; }
  .question-sheet { border: 1px solid var(--royal); background: var(--paper); box-shadow: 6px 6px 0 var(--wash); }
  .question-row { padding: 24px 32px; }
  .question-row + .question-row { border-top: 1px solid var(--rule); }
  .question-row dt { font: 700 24px/1.3 var(--display); color: var(--royal); margin-bottom: 8px; }
  .answer-space { display: block; height: 64px; margin-top: 8px; background: repeating-linear-gradient(to bottom, transparent 0, transparent 31px, var(--rule) 31px, var(--rule) 32px); }
  .evidence-note { margin-top: 32px; padding: 24px 32px; border-left: 4px solid var(--royal); background: var(--wash); }
  .evidence-caution { border-left-color: var(--amber); }
  .budget-sheet { border-top: 3px solid var(--royal); }
  .budget-lines > div { display: grid; grid-template-columns: minmax(0, .7fr) minmax(0, 1.3fr); gap: 24px; padding: 24px 16px; border-bottom: 1px solid var(--rule); }
  .budget-lines dt { color: var(--royal); font: 600 16px/1.5 var(--data); }
  .budget-lines dd { font: 400 18px/1.5 var(--plain); }
  .budget-space { display: block; height: 32px; border-bottom: 1px solid var(--rule); margin-top: 8px; }
  .price-note { position: relative; margin-top: 40px; padding: 32px; border: 1px solid var(--royal); border-top: 8px solid var(--gold); box-shadow: 6px 6px 0 var(--wash); }
  .price-note strong { color: var(--royal); font-family: var(--data); font-size: 20px; }
  .closing-note { position: relative; margin-top: 40px; padding: 32px 32px 32px 88px; border-top: 1px solid var(--royal); }
  .gold-seal { position: absolute; top: 32px; left: 16px; display: grid; place-items: center; width: 48px; height: 48px; color: var(--ink); background: var(--gold); font-size: 28px; clip-path: polygon(50% 0, 62% 9%, 77% 5%, 84% 20%, 98% 26%, 94% 43%, 100% 56%, 88% 67%, 86% 83%, 70% 86%, 57% 100%, 43% 91%, 26% 97%, 19% 81%, 3% 75%, 8% 58%, 0 45%, 11% 32%, 12% 17%, 31% 15%); }
  .site-footer { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: start; gap: 32px 64px; padding-block: 32px 48px; border-top: 2px solid var(--royal); }
  .site-footer p { max-width: 54ch; font-size: 18px; }
  .site-footer nav { display: flex; flex-wrap: wrap; gap: 8px 24px; font: 500 16px/1.5 var(--plain); }
  .site-footer a { display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 12px 0; }
  .missing-page { max-width: 720px; min-height: 50vh; padding-block: 80px; }
}

@layer utilities {
  .u-stack { display: grid; align-content: start; gap: 16px; }
  .u-measure { max-width: 60ch; }
  .u-mono { font-family: var(--data); }
}

@layer responsive {
  @media (max-width: 1050px) {
    .wrap { width: calc(100% - 48px); }
    .masthead { gap: 16px; }
    .wordmark { font-size: 28px; gap: 12px; }
    .opening { gap: 24px; }
    .notebook-layout { grid-template-columns: 152px minmax(0, 1fr); gap: 32px; }
    .check-pair { grid-template-columns: minmax(0, 1fr); }
    .site-footer { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  }

  @media (max-width: 720px) {
    html { scroll-padding-top: 24px; }
    .wrap { width: calc(100% - 40px); }
    body { font-size: 18px; line-height: 1.6; }
    h1 { font-size: 40px; line-height: 1.12; }
    h2 { font-size: 32px; }
    .masthead { min-height: 0; padding-top: 16px; flex-direction: column; align-items: flex-start; gap: 8px; }
    .wordmark { font-size: 30px; }
    .page-nav { width: 100%; gap: 16px; }
    .page-nav a { padding-inline: 0; }
    .opening { grid-template-columns: minmax(0, 1fr); gap: 24px; padding-block: 40px; }
    .opening-copy { padding-block: 0; }
    .opening-copy .lead { max-width: 34ch; }
    .lead, .section-lead { font-size: 20px; }
    .steel-link { margin-top: 8px; max-width: 100%; }
    .paper-scene { max-width: 350px; justify-self: center; }
    .paper-assembly { inset: 8px; }
    .paper-arch { border-width: 24px; }
    .paper-sun { width: 56px; height: 56px; }
    .chapter-path { grid-template-columns: minmax(0, 1fr); padding-block: 8px; }
    .chapter-path p { min-height: 48px; justify-content: flex-start; padding: 8px 16px; }
    .chapter-path p + p { border-left: 0; }
    .notebook-layout { grid-template-columns: minmax(0, 1fr); gap: 40px; padding-block: 32px 64px; }
    .anchor-rail { position: static; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); padding-block: 0; }
    .rail-label { grid-column: 1 / -1; margin-bottom: 8px; }
    .anchor-rail a { padding: 10px 16px; min-height: 48px; }
    .anchor-rail .rail-next { margin-top: 0; padding-top: 10px; border-top: 0; }
    .chapter + .chapter { margin-top: 56px; padding-top: 40px; }
    .chapter-heading { gap: 16px; margin-bottom: 24px; }
    .entry { grid-template-columns: 32px minmax(0, 1fr); gap: 16px; padding-block: 24px; }
    .entry-letter { font-size: 40px; }
    .source-pair { gap: 0 12px; }
    .source-label { flex-basis: 100%; min-height: 24px; margin-top: 8px; }
    .source-pair a { min-height: 48px; padding-block: 12px; }
    .deeper-note summary { padding: 16px; gap: 16px; }
    .detail-copy { padding: 24px 16px; }
    .summary-slip { padding: 24px 20px; }
    .check-pair { gap: 32px; }
    .caution-note { padding: 24px 0 24px 32px; }
    .caution-note::before { top: 31px; left: 4px; }
    .next-leaf { padding: 24px; margin-top: 48px; box-shadow: 5px 5px 0 var(--rule); }
    .breadcrumb { padding-top: 16px; }
    .opening-notes { padding-top: 24px; }
    .paper-scene-notes { max-width: 320px; }
    .paper-scene-notes .paper-arch { border-width: 2px; }
    .question-row { padding: 24px 20px; }
    .evidence-note { padding: 24px 20px; }
    .budget-lines > div { grid-template-columns: minmax(0, 1fr); gap: 8px; padding: 24px 8px; }
    .budget-lines dd { font-size: 16px; }
    .price-note { padding: 24px 20px; }
    .closing-note { padding: 88px 0 0; }
    .gold-seal { top: 24px; left: 0; }
    .site-footer { padding-block: 24px 32px; gap: 16px; }
    .site-footer p { font-size: 16px; }
    .site-footer nav { gap: 8px 24px; }
    .missing-page { padding-block: 56px; }
  }

  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto; transition: none; animation: none; }
  }

  @media print {
    @page { margin: 18mm; }
    body { color: #111827; background: white; font-size: 16px; line-height: 1.45; }
    .wrap { width: 100%; }
    .masthead { min-height: 64px; margin-bottom: 24px; }
    .wordmark { font-size: 28px; }
    .page-nav, .anchor-rail, .paper-scene, .chapter-path, .steel-link, .site-footer nav, .breadcrumb, .skip-link, .answer-space::after { display: none; }
    .opening { display: block; padding: 0 0 24px; }
    .opening-copy { padding: 0; }
    h1 { font-size: 32px; }
    h2 { font-size: 28px; }
    h3, .question-row dt { font-size: 22px; }
    .eyebrow, .source-caption, .source-pair, .budget-lines dt, .budget-lines dd, .site-footer p { font-size: 16px; }
    .notebook-layout { display: block; padding: 0; }
    .chapter + .chapter { margin-top: 32px; padding-top: 24px; }
    .chapter-heading { margin-bottom: 16px; }
    .question-sheet, .deeper-note, .price-note, .next-leaf { box-shadow: none; }
    .question-row { padding: 16px 24px; break-inside: avoid; }
    .answer-space { height: 64px; border-bottom: 1px solid #9ca3af; }
    .budget-lines > div { padding: 16px; break-inside: avoid; }
    .summary-slip, .evidence-note { background: white; padding: 16px 24px; }
    .check-note, .entry, .caution-note, .price-note, .closing-note { break-inside: avoid; }
    .deeper-note::details-content { content-visibility: visible; display: contents; }
    .deeper-note > :not(summary) { display: block; }
    .deeper-note summary { font-size: 16px; padding: 12px 16px; min-height: 44px; }
    .detail-copy { padding: 16px 24px; }
    .detail-copy p + p { margin-top: 12px; }
    .detail-sign { display: none; }
    .source-pair { break-inside: avoid; }
    .site-footer { margin-top: 32px; padding-top: 16px; padding-bottom: 0; display: block; }
  }
}
