@layer reset, paper, reading, desks, controls, responsive;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; scroll-padding-block: 24px; }
  body, h1, h2, h3, p, figure { margin: 0; }
  img { display: block; max-width: 100%; height: auto; }
  button, textarea { font: inherit; }
  [hidden] { display: none !important; }
}

@layer paper {
  :root {
    color-scheme: light;
    --paper: #f7f4ed;
    --surface: #fcfbf8;
    --ink: #1c1c1c;
    --muted: #5f5f5d;
    --edge: #eceae4;
    --line: rgb(28 28 28 / 18%);
    --blue: #3b82f6;
    --link: #164ea1;
    --unit: 8px;
    --corner: 6px;
    --detail: "JetBrains Mono", ui-monospace, "SFMono-Regular", Consolas, monospace;
  }
  body {
    background: var(--paper);
    color: var(--ink);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 16px;
    font-weight: 400;
    line-height: 1.6;
  }
  .page-shell { max-width: 1280px; margin-inline: auto; padding-inline: 24px; }
  .guide-masthead {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    min-height: 104px;
    border-bottom: 1px solid var(--line);
  }
  .guide-name { display: flex; align-items: center; gap: 16px; font-weight: 650; }
  .paper-tab {
    width: 32px;
    height: 32px;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--edge);
    box-shadow: inset 0 2px 3px rgb(28 28 28 / 8%);
    position: relative;
    flex: 0 0 32px;
  }
  .paper-tab::after {
    content: "";
    position: absolute;
    inset: 8px 12px 7px 10px;
    background: var(--blue);
    border-radius: 2px 2px 0 0;
    transform: rotate(-10deg);
  }
  a { color: var(--link); text-decoration-thickness: 1px; text-underline-offset: 4px; }
  a:hover { text-decoration-thickness: 2px; }
  a:focus-visible, button:focus-visible, textarea:focus-visible {
    outline: 3px solid var(--link);
    outline-offset: 4px;
  }
  [tabindex="-1"]:focus { outline: none; }
  .skip-reading {
    position: fixed;
    top: 8px;
    left: 24px;
    z-index: 5;
    padding: 12px 16px;
    border-radius: var(--corner);
    color: var(--ink);
    background: var(--surface);
    border: 2px solid var(--ink);
    transform: translateY(-160%);
  }
  .skip-reading:focus { transform: translateY(0); }
}

@layer reading {
  .page-opening {
    display: grid;
    grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr);
    gap: 64px;
    padding-block: 64px 48px;
    align-items: start;
  }
  .desk-label, .misconception-label, .answer-label {
    font-family: var(--detail);
    color: var(--muted);
    font-size: 16px;
    line-height: 1.6;
  }
  h1 {
    font-size: clamp(2.5rem, 4.6vw, 4rem);
    line-height: 1.08;
    letter-spacing: -0.045em;
    font-weight: 700;
    margin-block: 24px;
    overflow-wrap: break-word;
    max-width: 19ch;
  }
  .heading-turn { display: block; }
  .opening-line { max-width: 60ch; color: var(--muted); }
  .correction {
    margin-top: 32px;
    padding: 24px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-left: 4px solid var(--blue);
    border-radius: var(--corner);
    box-shadow: 0 2px 0 rgb(28 28 28 / 4%);
  }
  .misconception { margin-block: 8px 24px; font-weight: 650; }
  .answer-label { color: var(--link); margin-bottom: 8px; }
  .correction p + p { margin-top: 8px; }
  .correction .answer-label { margin-top: 24px; }
  .desk-art {
    margin-top: 48px;
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: var(--corner);
    background: var(--edge);
    overflow: hidden;
    box-shadow: 8px 8px 0 rgb(28 28 28 / 4%);
  }
  .desk-art img { width: 100%; aspect-ratio: 5 / 3; background: var(--edge); color: var(--muted); font-size: 16px; line-height: 1.6; }
  .desk-art img[src="/visit-desk.webp"] { aspect-ratio: 5 / 4; }
  .reading-column { width: 100%; max-width: 72ch; }
  .reading-section { margin-bottom: 48px; padding-top: 32px; border-top: 1px solid var(--line); }
  h2 { font-size: 1.75rem; line-height: 1.25; letter-spacing: -0.025em; font-weight: 700; margin-bottom: 24px; }
  h3 { font-size: 1.125rem; line-height: 1.5; font-weight: 700; margin-bottom: 8px; }
  .reading-section p + p, .urgent-note p + p { margin-top: 16px; }
  .answer-note + .answer-note { margin-top: 32px; }
  .evidence-route { margin-block: 32px; }
  .evidence-route a { display: inline-flex; align-items: center; min-height: 48px; padding-block: 8px; }
  .urgent-note {
    padding: 24px;
    margin-block: 48px;
    border: 2px solid var(--ink);
    border-left-width: 6px;
    border-radius: var(--corner);
    background: var(--surface);
  }
  .urgent-title { font-size: 1.5rem; }
  .urgent-action { font-size: 1.125rem; font-weight: 700; }
  .source-link { display: flex; justify-content: space-between; align-items: center; gap: 24px; min-height: 56px; padding-block: 12px; border-bottom: 1px solid var(--line); }
  .source-link span { flex-shrink: 0; }
  .source-link + p { margin-top: 24px; }
  .reading-section p + .source-link { margin-top: 16px; }
  .next-desk { padding: 24px 0 48px; border-top: 1px solid var(--line); }
  .next-desk p { color: var(--muted); }
  .next-desk a { display: flex; width: fit-content; max-width: 100%; min-height: 56px; align-items: center; gap: 24px; font-weight: 650; }
  .missing-page { padding-block: 80px; }
  .return-link { display: inline-flex; min-height: 48px; align-items: center; margin-top: 24px; }
}

@layer desks {
  .desk-directory { border-top: 2px solid var(--ink); padding-block: 32px 24px; margin-top: 32px; }
  .directory-heading { display: flex; align-items: center; justify-content: space-between; gap: 24px; margin-bottom: 24px; }
  .directory-heading h2 { font-size: 1.5rem; margin: 0; }
  .directory-heading a { display: inline-flex; min-height: 48px; align-items: center; padding-block: 8px; }
  .desk-directory nav { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: var(--corner); background: var(--surface); overflow: hidden; }
  .desk-directory nav a {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 32px;
    gap: 8px 16px;
    align-items: start;
    padding: 24px;
    min-height: 140px;
    color: var(--ink);
    text-decoration: none;
    border-right: 1px solid var(--line);
  }
  .desk-directory nav a:last-child { border-right: 0; }
  .desk-directory nav a:hover, .desk-directory nav a[aria-current="page"] { background: var(--edge); }
  .desk-directory nav a:focus-visible { outline-offset: -5px; }
  .desk-name { grid-column: 1; font-weight: 700; }
  .desk-purpose { grid-column: 1; color: var(--muted); font-size: 16px; }
  .desk-arrow { grid-column: 2; grid-row: 1 / 3; text-align: center; width: 32px; height: 32px; border-radius: 50%; background: var(--edge); box-shadow: inset 0 1px 2px rgb(28 28 28 / 10%); }
  .footer-colophon { display: flex; align-items: start; justify-content: space-between; gap: 32px; padding-top: 24px; }
  .footer-colophon p { font-size: 16px; max-width: 72ch; }
  .domain-name { color: var(--muted); overflow-wrap: anywhere; }
}

@layer controls {
  .directory-jump { display: inline-flex; align-items: center; gap: 16px; min-height: 48px; padding: 8px 16px; color: var(--ink); border: 1px solid var(--line); border-radius: var(--corner); background: var(--surface); box-shadow: inset 0 2px 2px rgb(28 28 28 / 5%), 0 1px 0 rgb(28 28 28 / 5%); text-decoration: none; }
  .directory-jump span { color: var(--link); }
  .reader-notes { margin-block: 48px; padding: 24px; border: 1px solid var(--line); border-radius: var(--corner); background: var(--surface); }
  .reader-notes label { display: block; margin-block: 24px 8px; font-weight: 650; }
  textarea { width: 100%; min-height: 192px; padding: 16px; resize: vertical; border: 1px solid var(--muted); border-radius: var(--corner); background: var(--paper); color: var(--ink); font-size: 16px; line-height: 1.6; box-shadow: inset 0 2px 3px rgb(28 28 28 / 7%); }
  #notes-help { color: var(--muted); margin-top: 16px; }
  .note-controls { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 24px; }
  button { min-height: 48px; padding: 12px 16px; cursor: pointer; border: 1px solid var(--ink); border-radius: var(--corner); background: var(--ink); color: var(--surface); font-size: 16px; line-height: 1.5; font-weight: 650; box-shadow: inset 0 1px 0 rgb(255 255 255 / 18%), 0 2px 0 rgb(28 28 28 / 12%); }
  #clear-note { background: var(--surface); color: var(--ink); border-color: var(--muted); }
  button:hover { box-shadow: inset 0 -2px 0 rgb(255 255 255 / 12%), 0 2px 0 rgb(28 28 28 / 20%); }
  button:disabled { cursor: default; background: var(--edge); color: var(--muted); border-color: var(--muted); box-shadow: none; }
  .notes-status { min-height: 26px; margin-top: 16px; color: var(--link); }
}

@layer responsive {
  @media (max-width: 1000px) {
    .page-opening { gap: 32px; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); }
    .desk-directory nav { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .desk-directory nav a:nth-child(2) { border-right: 0; }
    .desk-directory nav a:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
    .desk-directory nav a { min-height: 120px; }
    .footer-colophon { flex-direction: column; gap: 16px; }
  }
  @media (max-width: 720px) {
    .guide-masthead { flex-wrap: wrap; gap: 8px; padding-block: 16px; min-height: auto; }
    .guide-name { gap: 8px; }
    .directory-jump { padding-inline: 8px; gap: 8px; }
    .page-opening { display: block; padding-block: 40px 32px; }
    h1 { font-size: 2.5rem; max-width: 20ch; }
    .correction { padding: 24px 20px; }
    .desk-art { margin-top: 32px; max-width: 480px; }
    .reading-section { padding-top: 24px; margin-bottom: 40px; }
    h2 { font-size: 1.5rem; }
    .directory-heading { display: block; }
    .directory-heading a { margin-top: 8px; }
    .desk-directory nav { grid-template-columns: minmax(0, 1fr); }
    .desk-directory nav a { min-height: 100px; padding: 20px; border-right: 0; border-bottom: 1px solid var(--line); }
    .desk-directory nav a:last-child { border-bottom: 0; }
    .reader-notes, .urgent-note { padding: 20px; }
    .note-controls { flex-direction: column; }
    .note-controls button { width: 100%; }
    .source-link { gap: 16px; }
  }
  @media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    *, *::before, *::after { animation: none !important; transition: none !important; }
  }
  @media print {
    body { background: #fff; }
    .page-shell { max-width: none; padding: 0; }
    .page-opening { display: block; padding-top: 24px; }
    .desk-art, .directory-jump, .skip-reading, .desk-directory, .note-controls, .notes-status, .next-desk, .evidence-route { display: none; }
    .reading-column { max-width: 72ch; }
    a { color: var(--ink); }
    .correction, .urgent-note, .reader-notes { break-inside: avoid; box-shadow: none; }
    h2, h3 { break-after: avoid; }
    textarea { box-shadow: none; }
  }
}
