@layer reset, tokens, layout, editorial, components, utilities, responsive;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { text-size-adjust: 100%; -webkit-text-size-adjust: 100%; }
  body { margin: 0; }
  h1, h2, h3, p, ul, ol, dl, dd { margin: 0; }
  button { font: inherit; }
  a { color: inherit; }
  [hidden] { display: none !important; }
}

@layer tokens {
  :root {
    color-scheme: light;
    --canvas: #ffffff;
    --ink: #111827;
    --blue: #3b82f6;
    --violet: #8b5cf6;
    --positive: #16a34a;
    --caution: #d97706;
    --error: #dc2626;
    --link: #1e40af;
    --violet-ink: #6d28d9;
    --muted: #4b5563;
    --rule: #d1d5db;
    --paper: #f8fafc;
    --unit: 4px;
    font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 18px;
    color: var(--ink);
    background: var(--canvas);
  }
}

@layer layout {
  .notebook-shell { display: grid; grid-template-columns: 284px minmax(0, 1fr); max-width: 1600px; margin-inline: auto; }
  .side-index { align-self: start; position: sticky; top: 0; padding: 36px 24px 28px; min-height: 100vh; max-height: 100vh; overflow-y: auto; overscroll-behavior: contain; border-right: 1px solid var(--rule); background: var(--paper); }
  main { min-width: 0; padding: 32px clamp(32px, 5vw, 88px) 0; }
  .note { max-width: 900px; margin-inline: auto; padding-top: 24px; }
  .note-section { margin-top: 40px; max-width: 68ch; }
  .note-footer { max-width: 900px; margin: 64px auto 0; padding-block: 28px 40px; border-top: 1px solid var(--rule); }
  .home-opening { display: grid; grid-template-columns: minmax(0, 1fr) 212px; gap: 44px; align-items: center; }
  .compact-index { display: none; }
}

@layer editorial {
  body { font-size: 18px; line-height: 1.65; }
  h1 { font-size: clamp(32px, 3.4vw, 48px); line-height: 1.14; font-weight: 650; letter-spacing: -0.04em; max-width: 24ch; text-wrap: balance; }
  h2 { font-size: 28px; line-height: 1.3; font-weight: 650; letter-spacing: -0.025em; margin-bottom: 20px; max-width: 34ch; text-wrap: balance; }
  h3 { font-size: 20px; line-height: 1.4; font-weight: 650; margin-bottom: 12px; }
  p { max-width: 68ch; overflow-wrap: break-word; }
  .note-section > p + p, .definition-list dd > p + p { margin-top: 16px; }
  .eyebrow { color: var(--muted); font-size: 16px; line-height: 1.5; }
  .note-header { border-bottom: 1px solid var(--rule); padding-bottom: 32px; }
  .note-tools { display: flex; gap: 16px; align-items: center; justify-content: space-between; margin-bottom: 24px; flex-wrap: wrap; }
  .situation { border-left: 3px solid var(--violet); padding-left: 20px; font-size: 20px; line-height: 1.6; margin-top: 28px; margin-bottom: 32px; max-width: 62ch; }
  .role-line { font-size: 22px; line-height: 1.5; font-weight: 550; }
  .annotation { font-size: 16px; color: var(--muted); line-height: 1.65; }
  .term-line { margin-bottom: 16px; }
  .term-key { font-size: 16px; display: inline-block; color: var(--violet-ink); background: #f5f3ff; border-left: 2px solid var(--violet); padding: 8px 12px; line-height: 1.5; }
  .term-key[data-mode="record"] { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
  .note-footer p { font-size: 16px; line-height: 1.65; color: var(--muted); }
}

@layer components {
  a, button, summary { -webkit-tap-highlight-color: #dbeafe; }
  a { text-decoration-thickness: 1px; text-underline-offset: 4px; }
  a:hover { text-decoration-thickness: 2px; }
  a:focus-visible, button:focus-visible, summary:focus-visible { outline: 3px solid var(--link); outline-offset: 4px; border-radius: 2px; }
  main:focus { outline: none; }
  .skip-link { position: fixed; z-index: 10; left: 16px; top: -100px; min-height: 48px; padding: 12px 20px; background: var(--ink); color: white; }
  .skip-link:focus { top: 16px; }
  .notebook-name { display: flex; flex-direction: column; justify-content: center; font-size: 24px; line-height: 1.2; letter-spacing: -0.035em; font-weight: 650; text-decoration: none; min-height: 64px; }
  .notebook-name > span:last-child { font-size: 18px; font-weight: 450; letter-spacing: -0.02em; margin-top: 4px; }
  .name-dot { display: inline-block; width: 8px; height: 8px; background: var(--violet); margin-left: 12px; border-radius: 50%; }
  .index-caption { font-size: 16px; margin-top: 16px; color: var(--muted); }
  .wide-index { margin-top: 32px; }
  .index-group + .index-group { margin-top: 24px; }
  .index-group > p { font-size: 16px; font-weight: 600; margin: 0 0 8px 12px; color: var(--muted); }
  .index-group ul { list-style: none; padding: 0; }
  .index-group a { display: flex; gap: 12px; align-items: center; min-height: 48px; padding: 10px 12px; font-size: 16px; line-height: 1.5; text-decoration: none; border-left: 2px solid transparent; }
  .index-group a:hover { background: #eef2ff; }
  .index-group a[aria-current] { color: var(--link); background: #eff6ff; border-left-color: var(--blue); font-weight: 600; }
  .index-mark { width: 6px; height: 6px; border: 1px solid #6b7280; flex: 0 0 6px; }
  .index-group a[aria-current] .index-mark { background: var(--blue); border-color: var(--blue); }
  .index-decoration { display: flex; gap: 8px; margin: 40px 12px 0; }
  .index-decoration span { display: block; height: 4px; width: 28px; background: var(--blue); }
  .index-decoration span:nth-child(2) { width: 16px; background: var(--violet); }
  .index-decoration span:nth-child(3) { width: 8px; background: var(--positive); }
  .breadcrumbs { max-width: 900px; margin-inline: auto; font-size: 16px; }
  .breadcrumbs ol { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; padding: 0; list-style: none; }
  .breadcrumbs li { display: flex; align-items: center; }
  .breadcrumbs li + li::before { content: "/"; padding-right: 12px; color: var(--muted); }
  .breadcrumbs a { min-height: 44px; display: inline-flex; align-items: center; padding-right: 8px; color: var(--link); }
  .breadcrumbs [aria-current] { color: var(--muted); line-height: 1.5; }
  #plain-language { font-size: 16px; line-height: 1.5; min-height: 44px; padding: 8px 12px; color: var(--violet-ink); border: 1px solid #c4b5fd; background: white; border-radius: 2px; cursor: pointer; }
  #plain-language:hover { background: #f5f3ff; }
  #plain-language[aria-pressed="false"] { color: var(--ink); border-color: var(--rule); }
  .entry-list { border-top: 1px solid var(--rule); }
  .entry-list > div { padding: 20px 0; border-bottom: 1px solid var(--rule); }
  .entry-list a { display: flex; justify-content: space-between; gap: 24px; align-items: center; color: var(--link); min-height: 48px; font-size: 20px; line-height: 1.4; text-decoration: none; }
  .entry-list dd { margin-top: 8px; font-size: 18px; color: var(--muted); max-width: 64ch; }
  .entry-list a span, .source-link span, .next-note a span, .note-footer a span { flex-shrink: 0; }
  .definition-list > div { display: grid; grid-template-columns: 192px minmax(0, 1fr); gap: 24px; padding-block: 24px; border-top: 1px solid var(--rule); }
  .definition-list > div:last-child { border-bottom: 1px solid var(--rule); }
  .definition-list dt { font-weight: 600; font-size: 18px; }
  .definition-list dd { font-size: 18px; }
  .definition-list .term-key { font-weight: 500; }
  .definition-list.compact > div { padding-block: 16px; }
  .paired-notes { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 32px; }
  .paired-notes > div { border-top: 2px solid var(--blue); padding-top: 20px; }
  .paired-notes > div + div { border-top-color: var(--violet); }
  .question-list { padding-left: 24px; max-width: 68ch; }
  .question-list li { padding-left: 4px; margin-top: 16px; font-size: 18px; }
  .question-list li::marker { color: var(--violet-ink); }
  .question-list a { min-height: 44px; display: inline-flex; align-items: center; color: var(--link); }
  .question-sheet { padding: 8px 24px 24px 12px; border: 1px solid var(--rule); border-top: 3px solid var(--blue); background: var(--paper); }
  .question-sheet li { border-bottom: 1px dashed var(--rule); padding-bottom: 20px; }
  .question-sheet li:last-child { border-bottom: 0; padding-bottom: 0; }
  .reading-list { list-style: none; padding: 0; }
  .reading-list > li { padding: 16px 0 20px; border-top: 1px solid var(--rule); }
  .reading .note-section { margin-top: 16px; }
  .source-link { color: var(--link); min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 24px; padding-block: 8px; font-size: 18px; line-height: 1.5; text-decoration: none; }
  .source-link:hover { text-decoration: underline; }
  .reading-list .annotation { margin-top: 4px; padding-right: 24px; }
  .urgent-note { border-left: 4px solid var(--error); background: #fef2f2; padding: 24px; }
  .urgent-note p + p { margin-top: 12px; }
  .urgent-action { font-size: 22px; line-height: 1.5; font-weight: 650; color: #991b1b; }
  .urgent-note .annotation { color: #7f1d1d; }
  .next-note { max-width: 68ch; margin-top: 40px; border-top: 2px solid var(--caution); padding-top: 24px; }
  .next-note p { font-size: 16px; color: var(--muted); }
  .next-note a { display: flex; align-items: center; justify-content: space-between; gap: 24px; min-height: 52px; color: var(--link); font-weight: 550; line-height: 1.5; padding-block: 12px; }
  .note-footer nav { display: flex; flex-wrap: wrap; gap: 8px 32px; margin-top: 16px; }
  .note-footer a { display: inline-flex; align-items: center; gap: 12px; min-height: 48px; padding-block: 12px; font-size: 16px; line-height: 1.5; color: var(--link); }
  .paper-geometry { width: 212px; height: 272px; position: relative; isolation: isolate; }
  .paper-geometry::before { content: ""; position: absolute; width: 160px; height: 224px; top: 24px; left: 24px; background: #eff6ff; border: 1px solid #bfdbfe; transform: rotate(-6deg); }
  .shape-ring { position: absolute; width: 144px; height: 144px; top: 0; right: 0; border: 2px solid var(--blue); border-radius: 50%; }
  .shape-plane { position: absolute; top: 64px; left: 0; width: 160px; height: 184px; border: 1px solid var(--ink); background: white; transform: rotate(3deg); }
  .shape-fold { position: absolute; right: 12px; bottom: 8px; width: 120px; height: 128px; background: #ede9fe; border: 1px solid #c4b5fd; clip-path: polygon(0 0, 100% 0, 100% 100%); }
  .shape-line { position: absolute; height: 1px; width: 128px; background: var(--violet); left: 28px; top: 164px; transform: rotate(-32deg); }
}

@layer utilities {
  .flow > p + p { margin-top: 16px; }
  .flow > .note-section { margin-top: 40px; }
  .flow > .home-opening { margin-top: 32px; }
}

@layer responsive {
  @media (max-width: 1100px) {
    .notebook-shell { grid-template-columns: 252px minmax(0, 1fr); }
    .side-index { padding-inline: 20px; }
    main { padding-inline: 36px; }
    .home-opening { grid-template-columns: minmax(0, 1fr); }
    .paper-geometry { display: none; }
    .definition-list > div { grid-template-columns: minmax(0, 1fr); gap: 16px; }
    .note-tools { align-items: flex-start; }
  }
  @media (max-width: 760px) {
    .notebook-shell { display: block; }
    .side-index { position: static; min-height: 0; max-height: none; overflow: visible; overscroll-behavior: auto; padding: 20px 24px 12px; border-right: 0; border-bottom: 1px solid var(--rule); }
    .notebook-name { font-size: 24px; min-height: 56px; }
    .index-caption { margin-top: 8px; }
    .wide-index, .index-decoration { display: none; }
    .compact-index { display: block; margin-top: 12px; }
    .compact-index summary { list-style: none; display: flex; align-items: center; justify-content: space-between; min-height: 48px; font-size: 16px; color: var(--link); cursor: pointer; }
    .compact-index summary::-webkit-details-marker { display: none; }
    .compact-index[open] > summary { border-bottom: 1px solid var(--rule); margin-bottom: 20px; }
    .compact-index[open] > summary > span { transform: rotate(45deg); }
    .compact-index nav { padding-bottom: 16px; }
    .index-group + .index-group { margin-top: 16px; }
    main { padding: 20px 24px 0; }
    .note { padding-top: 12px; }
    h1 { font-size: 34px; max-width: 23ch; }
    h2 { font-size: 26px; }
    .note-header { padding-bottom: 24px; }
    .note-tools { margin-bottom: 20px; gap: 12px; }
    .eyebrow { flex-basis: 100%; }
    .situation { font-size: 18px; padding-left: 16px; margin-top: 24px; margin-bottom: 24px; }
    .role-line { font-size: 20px; }
    .home-opening { gap: 0; }
    .paired-notes { grid-template-columns: minmax(0, 1fr); gap: 24px; }
    .note-section, .flow > .note-section { margin-top: 32px; }
    .definition-list > div { padding-block: 20px; }
    .entry-list a { font-size: 18px; gap: 16px; }
    .entry-list dd { font-size: 18px; }
    .source-link { gap: 16px; }
    .urgent-note { padding: 20px 16px; }
    .question-sheet { padding-right: 16px; }
    .note-footer { margin-top: 40px; }
  }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
  }
  @media print {
    .side-index, .skip-link, .note-tools button, .next-note, .note-footer nav, .paper-geometry { display: none !important; }
    .notebook-shell { display: block; }
    main { padding: 0; }
    .note { max-width: none; padding: 0; }
    body { font-size: 16px; }
    .note-section { break-inside: avoid; }
    h1 { font-size: 32px; }
    h2 { font-size: 24px; }
    .situation { margin-top: 20px; }
    .note-footer { margin-top: 24px; }
  }
}
