@layer base, utilities, structure, atmosphere, access;

@layer base {
  :root {
    color-scheme: light dark;
    --cream: #FFF8E7;
    --sky: #87CEEB;
    --twilight: #5B3A6B;
    --dream-blue: #4A6FA5;
    --gold: #C9A84C;
    --primary-text: #E8B4A0;
    --midnight: #1A1A2E;
    --olive-decoration: #4A5A2A;
    --ink: #1A1A2E;
    --surface: #FFF8E7;
    --quiet-surface: #F5EEDC;
    --link: #4A6FA5;
    --emphasis: #5B3A6B;
    --rule: #C4B8AB;
    --focus: #4A6FA5;
    --opening-surface: #87CEEB;
    --unit: 8px;
    --section-gap: clamp(4rem, 8vw, 8rem);
    --radius: 8px;
    /* Damped spring: mass 1, stiffness 120, damping 20; sampled over 480 ms. */
    --spring: linear(0, .1018 10%, .2987 20%, .4979 30%, .6633 40%, .7871 50%, .8734 60%, .9303 70%, .9662 80%, .9877 90%, 1);
    font-synthesis: none;
  }
  *, *::before, *::after { box-sizing: border-box; }
  html { scroll-behavior: smooth; scroll-padding-top: 2rem; }
  body {
    margin: 0;
    color: #1A1A2E;
    background: linear-gradient(180deg, #87CEEB 0, #FFF8E7 42rem, #FFF8E7 67%, #5B3A6B 100%);
    font-family: "Playfair Display", Georgia, "Times New Roman", serif;
    font-weight: 400;
    font-size: 16px;
    line-height: 1.6;
    min-height: 100vh;
    overflow-wrap: break-word;
  }
  h1, h2, h3, h4, p, figure, ul, ol { margin: 0; }
  h1, h2, h3, h4 { font-weight: 700; letter-spacing: -.025em; text-wrap: balance; }
  h1 { font-size: 2.25rem; line-height: 1.15; max-width: 23ch; }
  h2 { font-size: 1.5rem; line-height: 1.3; max-width: 36ch; }
  h3 { font-size: 1.25rem; line-height: 1.4; max-width: 40ch; }
  h4 { font-size: 1.125rem; line-height: 1.4; max-width: 40ch; }
  p { text-wrap: pretty; }
  a { color: var(--link); text-decoration-thickness: 1px; text-underline-offset: .24em; }
  a:hover { text-decoration-thickness: 2px; }
  a:focus-visible, summary:focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 4px;
    box-shadow: 0 0 0 7px var(--surface);
    border-radius: 3px;
  }
  em { font-weight: inherit; }
  img { display: block; max-width: 100%; }
  main:focus { outline: none; }
  summary { cursor: pointer; }
  progress { accent-color: var(--emphasis); }
}

/* These reusable utilities govern rhythm, surfaces, and layout throughout the guide. */
@layer utilities {
  .flow > * + * { margin-block-start: 1.25rem; }
  .measure { max-width: 72ch; }
  .surface { background: var(--surface); border-radius: var(--radius); }
  .split-screen { display: grid; grid-template-columns: 1.18fr 1fr; gap: 2rem; }
  .eyebrow { font-size: 1rem; font-weight: 500; letter-spacing: .04em; line-height: 1.5; color: var(--emphasis); }
  .lead { font-size: 1.125rem; line-height: 1.6; }
  .technical-value { font-family: "JetBrains Mono", monospace; font-size: 1rem; font-weight: 500; color: var(--emphasis); }
  .text-link { display: inline-flex; align-items: center; min-height: 44px; gap: .75rem; font-size: 1rem; }
  .primary-link {
    display: inline-flex;
    justify-content: space-between;
    align-items: center;
    gap: 1.5rem;
    min-height: 56px;
    padding: .75rem 1.25rem;
    background: var(--twilight);
    color: var(--cream);
    border-radius: var(--radius);
    font-size: 1rem;
    text-decoration: none;
    border: 1px solid var(--twilight);
    transition: transform 480ms var(--spring), box-shadow 480ms var(--spring);
  }
  .primary-link:hover { transform: translateY(-2px); box-shadow: -6px 7px 0 var(--gold); }
  .primary-link span { font-family: "JetBrains Mono", monospace; font-size: 1.5rem; line-height: 1; }
}

@layer structure {
  .site-grid {
    display: grid;
    grid-template-columns: 216px minmax(0, 1fr);
    gap: 40px;
    width: 100%;
    max-width: 1280px;
    padding: 24px;
    margin-inline: auto;
    align-items: start;
  }
  .reading-column { min-width: 0; padding-top: 32px; }
  .side-index {
    position: sticky;
    top: 24px;
    height: calc(100dvh - 48px);
    min-height: 400px;
    display: flex;
    flex-direction: column;
    min-width: 0;
    padding-bottom: 8px;
    background: var(--surface);
    padding: 16px;
    border-radius: var(--radius);
  }
  .guide-mark {
    display: flex;
    gap: 12px;
    align-items: center;
    min-height: 72px;
    flex-shrink: 0;
    font-size: 1.375rem;
    font-weight: 700;
    letter-spacing: -.035em;
    line-height: 1.2;
    text-decoration: none;
    color: var(--ink);
  }
  .guide-mark em { font-size: 1.75rem; color: var(--emphasis); }
  .index-description { font-size: 1rem; margin: 16px 0 24px; }
  .desktop-index { overflow-y: auto; min-height: 0; padding: 4px 8px 4px 4px; scrollbar-width: thin; scrollbar-color: var(--emphasis) transparent; }
  .index-group + .index-group { margin-top: 24px; }
  .index-heading { font-size: 1rem; font-weight: 500; color: var(--emphasis); padding-left: 12px; margin-bottom: 4px; }
  .index-group ul { list-style: none; padding: 0; }
  .index-group li + li { margin-top: 2px; }
  .index-group a {
    display: flex;
    align-items: center;
    min-height: 44px;
    padding: 8px 12px;
    line-height: 1.4;
    font-size: 1rem;
    color: var(--ink);
    border-radius: var(--radius);
    text-decoration: none;
    border-left: 2px solid transparent;
  }
  .index-group a:hover { background: var(--surface); color: var(--emphasis); }
  .index-group a[aria-current="page"] { background: var(--quiet-surface); border-left-color: var(--twilight); color: var(--emphasis); font-weight: 700; }
  .mobile-index { display: none; }
  .reading-position { display: none; margin-top: 24px; padding: 8px 16px; flex-shrink: 0; }
  [data-progress-ready] .reading-position { display: block; }
  .reading-position label { display: block; font-size: 1rem; margin-bottom: 8px; }
  .reading-position progress { display: block; width: 100%; height: 6px; border: none; border-radius: 8px; overflow: hidden; color: var(--emphasis); background: var(--rule); }
  .reading-position progress::-webkit-progress-bar { background: var(--rule); }
  .reading-position progress::-webkit-progress-value { background: var(--emphasis); }
  .reading-position progress::-moz-progress-bar { background: var(--emphasis); }
  .hero-copy, .page-opening, .breadcrumb, .dream-scene figcaption { background: var(--opening-surface); }
  .hero { align-items: center; margin-bottom: var(--section-gap); }
  .hero-copy { position: relative; z-index: 2; padding-block: 24px; }
  .hero-title { font-size: clamp(2.5rem, 5vw, 4rem); line-height: 1.02; letter-spacing: -.045em; margin-top: 24px; }
  .hero-title em { color: var(--emphasis); }
  .hero-copy .lead { max-width: 38ch; margin-block: 24px; }
  .hero-aside { font-size: 1rem; padding-top: 16px; max-width: 35ch; }
  .hero-aside span { font-style: italic; color: var(--emphasis); }
  .breadcrumb ol { display: flex; align-items: center; flex-wrap: wrap; gap: 0 16px; list-style: none; padding: 0; font-size: 1rem; }
  .breadcrumb a { display: inline-flex; align-items: center; min-height: 44px; color: var(--emphasis); }
  .breadcrumb li + li::before { content: " / "; margin-right: 16px; color: var(--emphasis); }
  .page-opening { position: relative; margin: 32px 0 var(--section-gap); }
  .page-opening h1 { margin-top: 16px; }
  .question-slip { margin: 40px 0 0 32px; padding: 24px 32px; width: fit-content; max-width: calc(100% - 32px); border-left: 2px solid var(--gold); }
  .question-slip > p + p { margin-top: 8px; font-size: 1.25rem; line-height: 1.45; font-style: italic; }
  .chapters { display: flex; flex-direction: column; gap: var(--section-gap); }
  .chapter-section, .chapter-group { width: 100%; background: var(--surface); border-radius: var(--radius); padding: 24px 32px; }
  .chapter-section h2, .chapter-group > h2 { padding-bottom: 8px; border-bottom: 1px solid var(--rule); }
  .chapter-section:nth-of-type(even), .chapter-group:nth-of-type(even) { align-self: flex-end; }
  .topic-block + .topic-block, .topic-block + .topic-group { margin-block-start: 2.75rem; }
  .chapter-group > .path-row { margin-block-start: 2rem; padding: 24px 0; }
  .visit-questions { padding-inline-start: 1.5rem; }
  .visit-questions li + li { margin-block-start: .75rem; }
  .path-row { display: grid; grid-template-columns: 1fr 2fr; align-items: center; gap: 32px; padding: 32px; max-width: 100%; }
  .path-row h2 { border: none; }
  .path-row-reverse { grid-template-columns: 2fr 1fr; margin-left: 24px; }
  .path-row-reverse > .path-word { grid-column: 2; grid-row: 1; }
  .path-row-reverse > div { grid-column: 1; grid-row: 1; }
  .path-word { font-size: clamp(3rem, 7vw, 6rem); line-height: 1; letter-spacing: -.07em; font-style: italic; color: var(--emphasis); }
  .page-turn { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 16px 32px; margin-top: var(--section-gap); padding: 24px 32px; border-top: 2px solid var(--gold); }
  .page-turn > div > a { display: inline-flex; align-items: center; min-height: 44px; font-size: 1.25rem; line-height: 1.4; }
  .reading-link { display: inline-flex; align-items: center; min-height: 44px; font-size: 1rem; }
  .guide-footer { margin-top: 48px; padding: 32px; background: var(--midnight); color: var(--primary-text); border-radius: var(--radius); }
  .guide-footer p { font-size: 1rem; max-width: 72ch; }
  .footer-links { display: flex; flex-wrap: wrap; gap: 8px 32px; margin-top: 16px; }
  .footer-links a { display: inline-flex; align-items: center; min-height: 44px; color: var(--cream); font-size: 1rem; }
  .source-links { list-style: none; padding: 0; border-left: 2px solid var(--gold); }
  .source-links a { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 48px; padding: 10px 16px; font-size: 1rem; }
  .source-links a:hover { background: var(--surface); }
}

@layer atmosphere {
  .mark-orbit { width: 28px; height: 48px; border: 1px solid var(--twilight); border-radius: 50%; transform: rotate(-18deg); position: relative; flex-shrink: 0; }
  .mark-orbit::after { content: ""; position: absolute; width: 12px; height: 12px; background: var(--gold); border-radius: 50%; top: -4px; right: -4px; }
  .dream-scene { position: relative; isolation: isolate; min-width: 0; }
  .hero-scene { margin: 24px 8px 0 -8px; transform: perspective(800px) rotateY(-4deg) skew(-2deg); animation: float-scene 4s ease-in-out infinite; }
  .place-atmosphere { position: relative; overflow: hidden; border-radius: 50% 50% 8px 8px / 28% 28% 8px 8px; background: linear-gradient(165deg, var(--sky), var(--cream) 55%, var(--twilight)); box-shadow: -10px 20px 30px #1A1A2E26; }
  .place-atmosphere img { position: relative; z-index: 2; width: 100%; height: auto; aspect-ratio: 4 / 5; object-fit: cover; color: transparent; font-size: 0; }
  .place-atmosphere::before { content: ""; position: absolute; z-index: 1; width: 160%; height: 42%; left: -30%; bottom: -8%; border-radius: 50%; background: linear-gradient(160deg, #87CEEB99, #5B3A6BB3); transform: rotate(-8deg); filter: blur(12px); }
  .place-atmosphere::after { content: ""; position: absolute; z-index: 3; inset: 16px; border: 1px solid #C9A84C80; border-radius: inherit; pointer-events: none; }
  .dream-sun { position: absolute; width: 36%; aspect-ratio: 1; border: 1px solid var(--gold); background: #FFF8E759; border-radius: 50%; top: 12%; left: -8%; filter: blur(.5px); }
  .dream-door { position: absolute; width: 27%; height: 35%; border: 14px solid #FFF8E7A6; border-bottom: 0; border-radius: 48% 48% 0 0; right: 18%; bottom: 22%; transform: skew(-2deg); box-shadow: -10px 20px 30px #1A1A2E26; }
  .dream-door::after { content: ""; position: absolute; width: 52%; height: 22%; bottom: -30%; right: -70%; border-radius: 50%; background: var(--olive-decoration); opacity: .2; filter: blur(9px); }
  .dream-scene figcaption { margin: 20px 0 0 12px; font-size: 1rem; line-height: 1.5; color: var(--ink); transform: skew(2deg); }
  .wide-scene { max-width: 90%; align-self: center; transform: perspective(800px) rotateX(2deg) skew(-2deg); }
  .wide-scene .place-atmosphere { border-radius: 8px 8px 28% 8px; }
  .wide-scene img { aspect-ratio: 3 / 2; }
  .path-row { box-shadow: -10px 20px 30px #1A1A2E0C; }
  .path-row-reverse { transform: skew(-2deg); }
  .path-row-reverse > * { transform: skew(2deg); }
  .question-slip { box-shadow: -10px 20px 30px #1A1A2E0C; transform: perspective(800px) rotateX(1deg) skew(-2deg); }
  .question-slip > * { transform: skew(2deg); }
  .page-opening, .hero-copy { animation: fade-rise 480ms var(--spring) both; }
  .chapter-section, .chapter-group { position: relative; }
  .chapter-section::before, .chapter-group::before { content: ""; position: absolute; width: 32%; height: 36px; top: -40px; right: 8%; background: var(--sky); opacity: .12; filter: blur(24px); pointer-events: none; z-index: -1; }
  @keyframes float-scene { 0%, 100% { translate: 0 0; } 50% { translate: 0 -8px; } }
  @keyframes fade-rise { from { opacity: .96; translate: 0 12px; } to { opacity: 1; translate: 0 0; } }
}

@layer access {
  .skip-link { position: fixed; top: 8px; left: 24px; z-index: 20; min-height: 48px; padding: 12px 16px; background: var(--surface); transform: translateY(-200%); font-size: 1rem; }
  .skip-link:focus { transform: translateY(0); }
  @media (prefers-color-scheme: dark) {
    :root { --ink: #E8B4A0; --surface: #252238; --quiet-surface: #30263F; --link: #87CEEB; --emphasis: #FFF8E7; --rule: #665970; --focus: #87CEEB; --opening-surface: #213C56; }
    body { color: var(--primary-text); background: linear-gradient(180deg, #213C56 0, #1A1A2E 42rem, #1A1A2E 68%, #5B3A6B 100%); }
    .index-group a[aria-current="page"] { border-left-color: var(--gold); }
    .mark-orbit { border-color: var(--sky); }
    .primary-link { background: var(--primary-text); color: var(--midnight); border-color: var(--primary-text); }
    .guide-footer { border: 1px solid var(--rule); }
    .chapter-section::before, .chapter-group::before { opacity: .07; }
  }
  @media (max-width: 1099px) {
    .site-grid { grid-template-columns: 192px minmax(0, 1fr); gap: 24px; }
    .split-screen { grid-template-columns: 1fr .8fr; gap: 24px; }
    .hero-title { font-size: clamp(2.5rem, 4.8vw, 3.4rem); }
    .index-group a { padding-inline: 8px; }
  }
  @media (max-width: 899px) {
    .site-grid { display: block; padding: 24px; }
    .side-index { position: relative; top: auto; height: auto; min-height: 0; display: grid; grid-template-columns: 1fr auto; gap: 0 16px; padding: 16px; border-bottom: 1px solid var(--rule); }
    .guide-mark { min-height: 64px; }
    .guide-mark > span:last-child { font-size: 1.125rem; }
    .guide-mark em { font-size: 1.5rem; }
    .index-description { grid-column: 1; font-size: 1rem; margin: 8px 0 16px; }
    .desktop-index { display: none; }
    .mobile-index { display: block; grid-column: 1 / -1; background: var(--surface); border-radius: var(--radius); }
    .mobile-index summary { display: flex; align-items: center; justify-content: space-between; min-height: 48px; padding: 12px 16px; color: var(--emphasis); font-size: 1rem; list-style: none; }
    .mobile-index summary::-webkit-details-marker { display: none; }
    .mobile-index summary span { font-family: "JetBrains Mono", monospace; font-size: 1.25rem; }
    .mobile-index[open] summary span { transform: rotate(45deg); }
    .mobile-index nav { padding: 16px; border-top: 1px solid var(--rule); }
    .index-group a { padding-inline: 12px; }
    .index-group a[aria-current="page"] { background: var(--quiet-surface); }
    .reading-position { grid-column: 2; grid-row: 1 / 3; width: 128px; margin: 0; padding: 16px 0; align-self: center; }
    .reading-position label { font-size: 1rem; line-height: 1.4; }
    .reading-column { padding-top: 32px; }
    .hero { gap: 32px; }
    .hero-title { font-size: clamp(2.5rem, 6vw, 4rem); }
    .hero-aside { padding-top: 8px; }
    .question-slip { margin-left: 16px; max-width: calc(100% - 16px); }
  }
  @media (max-width: 599px) {
    .split-screen { grid-template-columns: 1fr; }
    .hero-title { font-size: clamp(2.5rem, 10vw, 3.5rem); }
    .hero-copy { padding: 0; }
    .hero-copy .lead { max-width: 100%; }
    .hero-scene { max-width: 84%; width: 100%; justify-self: end; margin: 8px 4px 0 0; }
    .hero-scene .place-atmosphere img { aspect-ratio: 4 / 3; }
    .hero-scene figcaption { margin-left: 0; }
    .page-opening { margin-top: 24px; }
    h1 { font-size: 2.25rem; }
    .breadcrumb ol { gap: 0 8px; }
    .breadcrumb li + li::before { margin-right: 8px; }
    .question-slip { padding: 24px; margin-top: 32px; }
    .question-slip > p + p { font-size: 1.125rem; }
    .chapter-section:nth-of-type(even), .chapter-group:nth-of-type(even) { align-self: start; }
    .chapter-section, .chapter-group { padding: 24px; }
    .wide-scene { max-width: 96%; }
    .path-row, .path-row-reverse { grid-template-columns: 1fr; gap: 24px; padding: 24px; margin-left: 0; }
    .path-row-reverse > .path-word, .path-row-reverse > div { grid-column: auto; grid-row: auto; }
    .path-word { font-size: 4rem; }
    .page-turn, .guide-footer { padding: 24px; }
    .page-turn { gap: 24px; }
    .reading-position { width: 112px; }
    .eyebrow, .index-heading { font-size: 1rem; }
  }
  @media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    *, *::before, *::after { animation: none !important; transition: none !important; }
  }
  @media print {
    body { background: #fff; color: #1A1A2E; }
    .site-grid { display: block; padding: 0; }
    .side-index, .dream-scene, .page-turn, .guide-footer, .skip-link, .breadcrumb, .question-slip { display: none; }
    .reading-column { padding: 0; }
    .page-opening { margin-bottom: 32px; }
    .chapters { gap: 32px; }
    .measure { max-width: none; }
    .chapter-section, .topic-block { break-inside: avoid; }
    .chapter-group { break-inside: auto; }
    a { color: #1A1A2E; }
  }
}
