@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  html {
    -webkit-text-size-adjust: 100%;
    scroll-padding-block-start: calc(var(--header-height) + 1.5rem);
  }

  body {
    margin: 0;
  }

  h1, h2, h3, h4, h5, h6,
  p, figure, blockquote, dl, dd {
    margin-block: 0;
  }

  img, picture, video, canvas, svg {
    display: block;
    max-inline-size: 100%;
  }

  img {
    height: auto;
  }

  input, button, textarea, select {
    font: inherit;
    color: inherit;
  }
}

@layer base {
  body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: 1.65;
    color: var(--text-primary);
    background-color: var(--bg-base);
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
  }

  .page-wrapper {
    min-block-size: 100dvh;
    display: flex;
    flex-direction: column;
  }

  .site-main {
    flex: 1 0 auto;
  }

  .site-main:focus {
    outline: none;
  }

  h1, h2, h3, h4, h5, h6 {
    color: var(--text-primary);
    font-weight: 650;
    line-height: 1.2;
    letter-spacing: -0.02em;
    text-wrap: balance;
  }

  h1 { font-size: var(--text-4xl); }
  h2 { font-size: var(--text-3xl); }
  h3 { font-size: var(--text-2xl); }
  h4 { font-size: var(--text-xl); }
  h5 { font-size: var(--text-lg); }
  h6 { font-size: var(--text-base); }

  p {
    text-wrap: pretty;
  }

  a {
    color: var(--accent);
    text-decoration-thickness: 1.5px;
    text-underline-offset: 0.18em;
  }

  a:hover {
    color: var(--accent-hover);
  }

  :focus-visible {
    outline: 3px solid var(--focus-ring);
    outline-offset: 2px;
  }

  /* Accessibility helpers */
  .visually-hidden {
    position: absolute;
    clip-path: inset(50%);
    overflow: hidden;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    border: 0;
    white-space: nowrap;
  }

  .skip-link {
    position: fixed;
    inset-block-start: -100%;
    inset-inline-start: 1rem;
    z-index: 100;
    padding: 0.625rem 1rem;
    border-radius: var(--radius-md);
    background: var(--accent);
    color: var(--accent-contrast);
    font-weight: 600;
    text-decoration: none;
    box-shadow: var(--shadow-md);
  }

  .skip-link:focus {
    inset-block-start: 1rem;
  }

  /* Shared container */
  .container {
    inline-size: min(var(--container-max), 100% - (var(--gutter) * 2));
    margin-inline: auto;
  }

  /* Editorial prose typography */
  .prose {
    color: var(--text-secondary);
    font-size: 1.03125rem;
    line-height: 1.75;
  }

  .prose > * + * {
    margin-block-start: 1.25em;
  }

  .prose h2 {
    margin-block-start: 2.25em;
    margin-block-end: 0.7em;
    padding-block-end: 0.4rem;
    border-block-end: 1px solid var(--border-subtle);
    font-size: clamp(1.375rem, 1.2rem + 0.7vw, 1.65rem);
  }

  .prose h3 {
    margin-block-start: 1.75em;
    margin-block-end: 0.55em;
    font-size: clamp(1.125rem, 1.05rem + 0.35vw, 1.3rem);
  }

  .prose h4 {
    margin-block-start: 1.5em;
    margin-block-end: 0.45em;
  }

  .prose h2:first-child,
  .prose h3:first-child {
    margin-block-start: 0;
  }

  .heading-anchor {
    margin-inline-start: 0.4rem;
    color: var(--text-muted);
    font-weight: 400;
    text-decoration: none;
    opacity: 0;
    transition: opacity 0.15s ease, color 0.15s ease;
  }

  h2:hover .heading-anchor,
  h3:hover .heading-anchor,
  .heading-anchor:focus-visible {
    opacity: 1;
    color: var(--accent);
  }

  .prose ul,
  .prose ol {
    padding-inline-start: 1.5rem;
  }

  .prose li + li {
    margin-block-start: 0.45rem;
  }

  .prose blockquote {
    margin-inline: 0;
    padding: 0.85rem 1.25rem;
    border-inline-start: 3px solid var(--accent);
    background: var(--surface-muted);
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    color: var(--text-primary);
  }

  .prose hr {
    border: 0;
    border-block-start: 1px solid var(--border-subtle);
    margin-block: 2.25rem;
  }

  .prose img {
    border-radius: var(--radius-md);
    border: 1px solid var(--border-subtle);
  }

  .prose code {
    font-family: var(--font-mono);
    font-size: 0.875em;
    padding: 0.15em 0.4em;
    border-radius: var(--radius-sm);
    background: var(--code-inline-bg);
    color: var(--text-primary);
  }

  .prose pre {
    position: relative;
    overflow-x: auto;
    padding: 1.15rem 1.25rem;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    background: var(--code-bg);
    color: var(--text-primary);
    font-size: 0.875rem;
    line-height: 1.6;
  }

  .prose pre code {
    padding: 0;
    border-radius: 0;
    background: transparent;
    font-size: inherit;
  }

  /* Grav Markdown Notices callouts */
  .prose .notices {
    margin-block: 1.5rem;
    padding: 1rem 1.25rem;
    border: 1px solid var(--info-border);
    border-inline-start-width: 4px;
    border-radius: var(--radius-md);
    background: var(--info-bg);
    color: var(--text-primary);
  }

  .prose .notices > p:first-child {
    margin-block-start: 0;
  }

  .prose .notices > p:last-child {
    margin-block-end: 0;
  }

  .prose .notices.blue,
  .prose .notices.info {
    border-color: var(--info-border);
    border-inline-start-color: var(--info-fg);
    background: var(--info-bg);
  }

  .prose .notices.green,
  .prose .notices.tip {
    border-color: var(--success-border);
    border-inline-start-color: var(--success-fg);
    background: var(--success-bg);
  }

  .prose .notices.yellow,
  .prose .notices.warning {
    border-color: var(--warning-border);
    border-inline-start-color: var(--warning-fg);
    background: var(--warning-bg);
  }

  .prose .notices.red,
  .prose .notices.danger {
    border-color: var(--danger-border);
    border-inline-start-color: var(--danger-fg);
    background: var(--danger-bg);
  }

  /* Accessible Responsive Tables */
  .table-wrapper {
    overflow: auto;
    max-inline-size: 100%;
    max-block-size: min(560px, 80vh);
    container-type: inline-size;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    background: var(--surface-elevated);
  }

  .table-wrapper table {
    border-collapse: separate;
    border-spacing: 0;
    inline-size: 100%;
    font-size: var(--text-sm);
    text-align: start;
  }

  .table-wrapper caption {
    padding: 0.75rem 1rem;
    font-weight: 600;
    color: var(--text-primary);
    text-align: start;
    border-block-end: 1px solid var(--border-subtle);
  }

  .table-wrapper th,
  .table-wrapper td {
    padding: 0.75rem 1rem;
    border-block-end: 1px solid var(--border-subtle);
    vertical-align: top;
  }

  .table-wrapper thead {
    position: sticky;
    inset-block-start: 0;
    z-index: 2;
    background: var(--surface-muted);
  }

  .table-wrapper thead th {
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
  }

  .table-wrapper tbody th {
    position: sticky;
    inset-inline-start: 0;
    z-index: 1;
    background: var(--surface-elevated);
    font-weight: 600;
    color: var(--text-primary);
  }

  .table-wrapper tbody tr:last-child th,
  .table-wrapper tbody tr:last-child td {
    border-block-end: 0;
  }

  @container (width < 560px) {
    .table-wrapper {
      border: 0;
      background: transparent;
      max-block-size: none;
    }

    .table-wrapper table.is-stackable,
    .table-wrapper table.is-stackable thead,
    .table-wrapper table.is-stackable tbody,
    .table-wrapper table.is-stackable tr,
    .table-wrapper table.is-stackable th,
    .table-wrapper table.is-stackable td {
      display: block;
    }

    .table-wrapper table.is-stackable thead {
      display: none;
    }

    .table-wrapper table.is-stackable tr {
      margin-block-end: 1rem;
      border: 1px solid var(--border-subtle);
      border-radius: var(--radius-md);
      background: var(--surface-elevated);
      overflow: hidden;
    }

    .table-wrapper table.is-stackable tbody th {
      position: sticky;
      inset-block-start: 0;
      z-index: 2;
      background: var(--surface-muted);
      border-block-end: 1px solid var(--border-subtle);
    }

    .table-wrapper table.is-stackable td {
      display: flex;
      justify-content: space-between;
      gap: 1rem;
      border-block-end: 1px solid var(--border-subtle);
    }

    .table-wrapper table.is-stackable td::before {
      content: var(--cell-label, "") ": ";
      font-weight: 600;
      color: var(--text-muted);
      flex-shrink: 0;
    }

    .table-wrapper table.is-stackable td:last-child {
      border-block-end: 0;
    }
  }
}
