@layer base, layout, components, pages, responsive;

@layer base {
  :root {
    color-scheme: light;
    --paper: #f5f3ef;
    --surface: #fffefa;
    --ink: #242624;
    --muted: #626760;
    --line: #d6d8d0;
    --accent: #295d4b;
    --hover: #e9eee7;
    --texture-panel: rgb(245 243 239 / 94%);
    --texture-wash: rgb(245 243 239 / 76%);
    --font-text: Arial, Helvetica, sans-serif;
    --font-display: 'Comic Sans MS', cursive;
    --radius: 6px;
  }
  :root[data-theme='dark'] {
    color-scheme: dark;
    --paper: #101211;
    --surface: #191c1a;
    --ink: #f4f3ee;
    --muted: #b5bcb3;
    --line: #363e37;
    --accent: #bad3b4;
    --hover: #242c26;
    --texture-panel: rgb(16 18 17 / 94%);
    --texture-wash: rgb(16 18 17 / 76%);
  }
  :root[data-theme='blue'] {
    color-scheme: dark;
    --paper: #122d3a;
    --surface: #193a49;
    --ink: #f4f6f4;
    --muted: #c1cdd1;
    --line: #426170;
    --accent: #cee4d1;
    --hover: #254b5b;
    --texture-panel: rgb(18 45 58 / 94%);
    --texture-wash: rgb(18 45 58 / 76%);
  }
  *, *::before, *::after { box-sizing: border-box; }
  html { scroll-behavior: smooth; }
  body { margin: 0; background: var(--paper); color: var(--ink); font: 1.0625rem/1.75 var(--font-text); overflow-wrap: break-word; }
  a { color: var(--accent); text-underline-offset: .22em; text-decoration-thickness: 1px; }
  a:hover { text-decoration-thickness: 2px; }
  a:focus-visible, button:focus-visible { outline: 3px solid var(--accent); outline-offset: 5px; border-radius: 2px; }
  img { max-width: 100%; height: auto; }
  h1, h2, h3 { line-height: 1.25; text-wrap: balance; }
  h1 { margin: 0; font-size: clamp(1.5rem, 3.2vw, 2.4rem); font-weight: 600; letter-spacing: -.035em; }
  h2 { margin: 2.5rem 0 1rem; font-size: clamp(1.3rem, 3vw, 1.65rem); }
  h3 { margin: 1.75rem 0 .75rem; font-size: 1.15rem; }
  p { margin: 0 0 1.15em; }
  ul, ol { padding-left: 1.4em; }
  small { font-size: .86em; }
  blockquote { margin: 1.75rem 0; padding: .2rem 0 .2rem 1.4rem; border-left: 2px solid var(--line); }
  hr { margin: 2rem 0; border: 0; border-top: 1px solid var(--line); }
  button { font: inherit; }
  [hidden] { display: none !important; }
}

@layer layout {
  body { min-height: 100vh; display: flex; flex-direction: column; }
  .site-header { border-bottom: 1px solid var(--line); }
  .header-inner { max-width: 1180px; margin: auto; padding: 1.2rem 2rem .9rem; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: .8rem 1rem; }
  .brand { color: var(--ink); font-size: 1.12rem; letter-spacing: -.04em; text-decoration: none; font-weight: 700; width: fit-content; }
  .brand span { font-weight: 400; }
  .header-utilities { display: flex; align-items: center; justify-content: end; flex-wrap: wrap; gap: .5rem 1rem; }
  .header-utilities a { color: var(--muted); font-size: .75rem; }
  .site-header nav { grid-column: 1 / -1; }
  .site-header nav ul { display: flex; flex-wrap: wrap; gap: .25rem 1.6rem; list-style: none; margin: 0; padding: 0; }
  .site-header nav a { display: inline-block; padding: .45rem 0; font-size: .86rem; text-decoration: none; color: var(--muted); }
  .site-header nav a:hover, .site-header nav a[aria-current], .site-header nav a.section-active { color: var(--ink); }
  .site-header nav a[aria-current], .site-header nav a.section-active { box-shadow: inset 0 -2px var(--accent); }
  .main-content { width: min(100% - 4rem, 920px); margin: 0 auto; padding: 3.2rem 0 5rem; flex: 1; }
  .page-heading { margin-bottom: 2.6rem; padding-bottom: 1.8rem; border-bottom: 1px solid var(--line); }
  .article-body { max-width: 75ch; margin-inline: auto; }
  .article-body p { text-align: start; }
}

@layer components {
  .skip-link { position: absolute; top: -100px; left: 1rem; padding: .6rem 1rem; background: var(--surface); color: var(--ink); z-index: 5; }
  .skip-link:focus { top: 1rem; }
  .theme-toggle { color: var(--muted); background: transparent; border: 1px solid var(--line); border-radius: 100px; padding: .35rem .85rem; font-size: .75rem; cursor: pointer; min-height: 36px; }
  .theme-toggle:hover { background: var(--hover); color: var(--ink); }
  .back-link { display: inline-block; margin: 0 0 1.5rem; font-size: .82rem; text-decoration: none; }
  .text-center { text-align: center !important; }
  .paw { display: block; width: clamp(70px, 13vw, 115px); height: auto; margin: 2.5rem auto; }
  :root[data-theme='light'] .paw[src*='ZampaBianca'], :root:not([data-theme='light']) .paw[src*='ZampaNera'] { filter: invert(1); }
  .article-image { display: block; width: auto; max-width: min(100%, 620px); max-height: 550px; object-fit: contain; margin: 2rem auto; }
  .image-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
  .image-pair .article-image { height: 100%; width: 100%; object-fit: cover; margin: 0; }
  .image-pair p { margin: 0; }
  .legacy-row { display: flex; gap: 2rem; flex-wrap: wrap; align-items: start; margin-block: 1.5rem; }
  .legacy-cell { flex: 1 1 220px; min-width: 0; }
  .legacy-cell > :last-child { margin-bottom: 0; }
  .legacy-cell .article-image { margin-top: 0; }
  .verse { margin-block: 1.2rem; }
  .verse .verse { margin-block: 0; }
  .verse-line { margin: 0; }
  .stanza-break { height: 1.2em; }
  .section-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
  .section-card { padding: 1.5rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
  .section-card h2 { margin: 0 0 .5rem; font-size: 1.5rem; }
  .section-card a { color: var(--ink); }
  .section-card p { color: var(--muted); font-size: .9rem; margin: 0; }
  .section-intro { color: var(--muted); margin-bottom: 2rem; }
  .catalog { list-style: none; padding: 0; margin: 0; }
  .catalog li { padding-block: .6rem; border-bottom: 1px solid var(--line); }
  .catalog-group { break-inside: avoid; margin-bottom: 2rem; }
  .catalog-group h2 { margin: 0 0 .7rem; font-size: 1.1rem; color: var(--muted); }
  .catalog-columns { columns: 2; column-gap: 3rem; }
  .quote-collection { border-top: 1px solid var(--line); margin-top: 3rem; padding-top: 1.5rem; }
  .quote-collection p { text-align: start !important; }
  .quote-collection blockquote { color: var(--muted); }
  .library-quote { margin: 0 0 2rem; }
  .library-quote:last-child { margin-bottom: 0; }
  .library-quote blockquote { margin: 0; padding: 0; border: 0; color: var(--ink); }
  .library-quote blockquote p { margin: 0; }
  .library-quote figcaption { margin-top: .35rem; text-align: left; font-style: italic; }
  .library-quote cite { font-style: inherit; font-weight: 400; }
  .audio-list { display: grid; gap: 1.3rem; }
  .audio-entry { padding: 1.4rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
  .audio-entry h3 { margin-top: 0; }
  .audio-entry p { margin-bottom: .65rem; }
  .audio-entry time { color: var(--muted); font-size: .8rem; }
  audio { display: block; width: 100%; margin-top: .8rem; }
  .book-entry { border-bottom: 1px solid var(--line); margin-bottom: 1.5rem; padding-bottom: 1.5rem; }
  .book-entry h2 { margin-top: 0; }
  .reviews-illustration { width: 220px; max-height: 180px; margin-block: 1rem 2rem; }
  .reviews-tools { margin-bottom: 2rem; padding: 1.25rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
  .reviews-tools label { display: block; font-size: .9rem; margin-bottom: .6rem; }
  .reviews-tools input { display: block; width: 100%; min-width: 0; padding: .75rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--paper); color: var(--ink); font: inherit; }
  .reviews-tools input:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
  #reviews-count { margin: .65rem 0 0; color: var(--muted); font-size: .85rem; }
  .book-review { padding-block: 2rem; border-top: 1px solid var(--line); scroll-margin-top: 1.5rem; }
  .book-review h2 { margin: 0 0 .5rem; }
  .review-authors { margin-bottom: .6rem; }
  .review-meta { display: flex; flex-wrap: wrap; align-items: baseline; gap: .5rem 1.3rem; color: var(--muted); font-size: .85rem; margin-bottom: .75rem; }
  .review-rating { color: var(--accent); font-weight: 700; }
  .review-edition { color: var(--muted); font-size: .8rem; }
  .book-review .review-title { font-size: 1.1rem; margin: 1.2rem 0 .75rem; }
  .review-text { white-space: pre-line; }
  .review-source { margin: 1rem 0 0; font-size: .8rem; }
}

@layer pages {
  .page-home .main-content { max-width: 960px; padding-top: 2rem; }
  .page-home .article-body, .page-section .article-body { max-width: none; }
  .home-intro { text-align: center; padding: .5rem 0 2rem; }
  .home-intro .paw { width: 108px; margin: 1.1rem auto 1.5rem; }
  .home-quotes { max-width: 660px; margin: 1.8rem auto 0; }
  .home-quotes blockquote { padding: 0; margin: 1.35rem 0; border: 0; }
  .home-quotes p { text-align: center; margin: 0; font-family: var(--font-display); font-size: 1.05rem; }
  .home-quotes cite { display: block; color: var(--muted); font-size: .77rem; margin-top: .35rem; font-style: normal; }
  .home-links { border-top: 1px solid var(--line); padding-top: 1.75rem; font-family: Arial, sans-serif; font-size: clamp(1.1rem, 2.6vw, 1.5625rem); }
  .home-links a { color: var(--ink); }
  .home-links .home-profile-link { margin: 0 0 2rem; font: bold .8em/1.3 Arial, sans-serif; text-align: center; letter-spacing: normal; }
  .home-link-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem 2rem; margin-block: 1.5rem; }
  .home-link-pair > div { text-align: center; }
  .home-link-pair p { text-align: center; font-size: .7em; margin: .25rem 0 0; }
  .home-links .home-link-center { text-align: center; margin: 1.5rem 0; }
  .home-links .home-link-spaced { margin-top: 3rem; }
  .page-profile { font-family: Arial, sans-serif; font-size: 13px; font-weight: bold; }
  .page-profile .page-heading { text-align: center; border: 0; margin-bottom: 1rem; }
  .page-profile .page-heading h1 { font-family: 'Comic Sans MS', cursive; font-size: 24px; font-weight: bold; font-style: italic; color: #0000ff; letter-spacing: normal; }
  :root:not([data-theme='light']) .page-profile .page-heading h1 { color: #a9c8ff; }
  .page-profile .profile-text { max-width: 510px; margin-inline: auto; padding: 5px; }
  .page-profile .profile-text p { text-align: justify; }
  .page-profile .profile-text h2 { font-family: Arial, sans-serif; font-size: 20px; font-weight: bold; margin-top: 2rem; }
  .page-profile .profile-text h2:last-of-type { color: #000080; }
  :root:not([data-theme='light']) .page-profile .profile-text h2:last-of-type { color: #a9c8ff; }
  .page-profile .site-header nav { font-family: Tahoma, sans-serif; }
  .closing-emphasis { color: #c40000; font-size: 1.2em; font-weight: 700; }
  :root:not([data-theme='light']) .closing-emphasis { color: #ff6b6b; }
  .page-poem .article-body { max-width: 70ch; }
  .page-poem .article-body strong { font-weight: 600; }
  .verse-paragraphs .text-content p { margin-bottom: .35rem; }
  .page-quando .main-content { display: flex; flex-direction: column; justify-content: center; }
  .page-quando .page-heading { text-align: center; }
  .page-quando .article-body { text-align: center; font-family: Tahoma, sans-serif; font-weight: 700; font-size: clamp(1.15rem, 3vw, 1.6rem); }
  .page-quando .article-body p { text-align: center; }
  .has-page-background { background-image: var(--page-background); background-repeat: repeat; background-position: top left; }
  .has-page-background .site-header { background: var(--paper); }
  .has-page-background .main-content { background: var(--texture-panel); padding-inline: clamp(1rem, 3vw, 2rem); margin-block: 2rem; border-radius: var(--radius); border: 1px solid var(--line); }
  .background-library { --page-background: url('../img/sfondoquadretti.gif'); }
  .background-escher { --page-background: url('../img/SfondoEscher.gif'); }
  .background-vatican { --page-background: url('../img/vaticano.jpg'); }
  .background-wall { --page-background: url('../img/MartelliTheWall.gif'); }
  .background-barbed-wire { --page-background: url('../img/Blackspina2.gif'); }
  .page-homages .quote-collection { padding: 1.5rem; background-image: linear-gradient(var(--texture-wash), var(--texture-wash)), url('../img/Skybianco.gif'); border-radius: var(--radius); }
  .page-duwanish .article-body { padding: clamp(.75rem, 4vw, 2.5rem); background-image: url('../img/sfondo05c.jpg'); border-radius: var(--radius); }
  .page-duwanish .article-body div.prose { padding: clamp(1rem, 3vw, 1.75rem); background-image: linear-gradient(var(--texture-panel), var(--texture-panel)), url('../img/sfondo03c.jpg'); }
  .page-duwanish .article-body::after { content: ''; display: block; height: 80px; margin-top: 1rem; background-image: url('../img/wolf2.gif'); background-repeat: repeat-x; background-position: center; }
  .page-dedication .article-body { background-image: linear-gradient(var(--texture-wash), var(--texture-wash)), url('../Sfondi/carta2.jpg'); color: var(--ink); padding: clamp(1.1rem, 3vw, 2rem); border-radius: var(--radius); font-family: var(--font-text); }
  .page-dedication .article-body small { font-size: inherit; }
  .page-dedication .article-body a { color: var(--accent); }
  .page-dedication .legacy-cell { background: var(--texture-panel); padding: 1rem; border-radius: var(--radius); }
  .dedication-title { text-align: center !important; font-family: var(--font-text); }
  .page-dedication h1.dedication-title { font-size: clamp(1.3rem, 2.4vw, 1.6rem); font-style: italic; margin-bottom: 2rem; letter-spacing: normal; }
  .dedication-grid { display: grid; margin: 2rem auto; gap: 4px; font-family: inherit; font-size: inherit; line-height: 1.65; }
  .dedication-grid-1 { max-width: 652px; grid-template-columns: minmax(0,425fr) minmax(0,506fr) minmax(0,510fr) minmax(0,585fr); }
  .dedication-grid-2 { width: 90%; grid-template-columns: minmax(0,37fr) minmax(0,31fr) minmax(0,32fr); gap: 6px; margin-block: 3rem; }
  .dedication-grid-3 { max-width: 637px; grid-template-columns: minmax(0,454fr) minmax(0,387fr) minmax(0,643fr) minmax(0,527fr); }
  .dedication-cell { min-width: 0; text-align: center; padding: .5rem; }
  .dedication-cell p { text-align: inherit; }
  .dedication-cell > :last-child { margin-bottom: 0; }
  .dedication-grid-2 .dedication-cell { align-self: center; }
  .dedication-grid-2 .dedication-cell.col-1.span-3 { text-align: start; }
  .dedication-grid-3 .dedication-cell { text-align: start; }
  .dedication-grid-3 .dedication-cell.row-1.col-1 { align-self: center; }
  .dedication-grid-3 .dedication-cell.row-3.col-2 { grid-column: 2 / span 2; }
  .dedication-cell.row-1 { grid-row: 1; }
  .dedication-cell.row-2 { grid-row: 2; }
  .dedication-cell.row-3 { grid-row: 3; }
  .dedication-cell.col-1 { grid-column: 1; }
  .dedication-cell.col-2 { grid-column: 2; }
  .dedication-cell.col-3 { grid-column: 3; }
  .dedication-cell.col-4 { grid-column: 4; }
  .dedication-cell.col-1.span-3 { grid-column: 1 / span 3; }
  .dedication-cell.verse-on-dark { background: #000; color: #fff; }
  .dedication-cell.verse-on-light { background: #fff; color: #400040; }
  .story-alchemist, .story-ship { background-attachment: fixed; background-size: cover; background-position: center; }
  .story-alchemist { background-image: linear-gradient(#101211b8, #101211d9), url('../img/optimized/Alchimista3.webp'); }
  .story-ship { background-image: linear-gradient(#101211b8, #101211d9), url('../img/optimized/VelieroFantasma.webp'); }
  .story-alchemist .main-content, .story-ship .main-content { padding-inline: 2rem; background: var(--surface); margin-block: 2rem; border-radius: var(--radius); }
  .story-alchemist .site-header, .story-ship .site-header, .story-alchemist .site-footer, .story-ship .site-footer { background: var(--paper); }
}

@layer responsive {
  @media (max-width: 640px) {
    .header-inner { padding: 1rem 1.15rem .6rem; gap: .65rem; }
    .header-utilities { grid-column: 1 / -1; justify-content: start; }
    .site-header nav ul { gap: 0 1.25rem; }
    .site-header nav a { font-size: .8rem; padding-block: .65rem; }
    .main-content { width: calc(100% - 2.3rem); padding: 2rem 0 3rem; }
    .page-heading { margin-bottom: 1.7rem; padding-bottom: 1.3rem; }
    .section-grid, .image-pair { grid-template-columns: 1fr; }
    .section-card { padding: 1.25rem; }
    .catalog-columns { columns: 1; }
    .legacy-row { gap: 1rem; }
    .legacy-cell { flex-basis: 100%; }
    .story-alchemist, .story-ship { background-attachment: scroll; }
    .story-alchemist .main-content, .story-ship .main-content { padding-inline: 1.2rem; }
    .page-profile .profile-text p { text-align: start; }
    .article-image { max-height: none; }
    .dedication-grid { display: block; width: 100%; }
    .dedication-cell { margin-block: 1rem; }
  }
  @media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
  @media print {
    :root, :root[data-theme] { --paper: white; --surface: white; --ink: black; --muted: #333; --line: #aaa; --accent: black; }
    body { background: white !important; font-size: 11pt; }
    .site-header, .back-link, .theme-toggle, audio { display: none; }
    .main-content { width: 100%; max-width: none; padding: 0; }
    .article-body { max-width: none; }
    .page-heading { margin-top: 0; }
    h1, h2, h3 { break-after: avoid; }
    img { max-height: 12cm; }
    .page-dedication .article-body { background: white; }
    .has-page-background .main-content { background: white; margin: 0; border: 0; padding: 0; }
    .page-homages .quote-collection, .page-duwanish .article-body, .page-duwanish .article-body div.prose { background: white; }
    .page-duwanish .article-body::after { display: none; }
  }
}
