/* DHCraft shared building blocks: base text, header, footer, card, button, code block with
   copy button, diagram frame. Load after dhcraft-tokens.css.
   Everything sits in the layer dh, so any unlayered rule of a site wins without a
   specificity fight. Sites that use their own layers name dh first in their layer order. */

@layer dh {
  /* Base. Only what every page needs; a site keeps its own reset. */
  body {
    margin: 0;
    background-color: var(--dh-ground);
    color: var(--dh-ink);
    font-family: var(--dh-font-text);
    font-size: var(--dh-step-0);
    line-height: var(--dh-leading);
  }
  /* A dark island (dh-dark) re-reads the role tokens in its own scheme. */
  .dh-dark, .dh-light { background-color: var(--dh-ground); color: var(--dh-ink); }

  h1, h2, h3 { font-family: var(--dh-font-display); font-weight: 700; line-height: var(--dh-leading-tight); letter-spacing: -0.01em; text-wrap: balance; }
  h1 { font-size: var(--dh-step-3); letter-spacing: -0.02em; }
  h2 { font-size: var(--dh-step-2); }
  h3 { font-size: var(--dh-step-1); font-weight: 600; }

  a { color: var(--dh-link); text-decoration-thickness: 1px; text-underline-offset: 0.18em; }
  a:hover { color: var(--dh-accent); }
  code, kbd, pre { font-family: var(--dh-font-mono); font-size: 0.9em; }
  :focus-visible { outline: var(--dh-focus-ring); outline-offset: var(--dh-focus-offset); }

  .dh-prose { max-inline-size: var(--dh-measure); }
  .dh-page { max-inline-size: var(--dh-page); margin-inline: auto; padding-inline: var(--dh-gutter); }

  .dh-skip { position: absolute; inset-inline-start: var(--dh-space-4); inset-block-start: -4rem; z-index: 50; padding: var(--dh-space-2) var(--dh-space-4); background: var(--dh-ink); color: var(--dh-ground); border-radius: var(--dh-radius-s); }
  .dh-skip:focus { inset-block-start: var(--dh-space-2); }

  /* Header: brand mark and site name, then the navigation. Solid background, the current
     page carries aria-current and an accent underline. On narrow screens the navigation
     moves to its own line and scrolls sideways inside itself. */
  .dh-head {
    display: flex; flex-wrap: wrap; align-items: center; gap: var(--dh-space-2) var(--dh-space-6);
    padding: var(--dh-space-2) var(--dh-gutter);
    background-color: var(--dh-ground); color: var(--dh-ink);
    border-block-end: var(--dh-hairline) solid var(--dh-line);
  }
  .dh-brand { display: inline-flex; align-items: center; gap: var(--dh-space-3); min-block-size: var(--dh-tap); color: var(--dh-ink); font-family: var(--dh-font-display); font-weight: 700; text-decoration: none; }
  .dh-brand img { inline-size: auto; block-size: 2rem; }
  .dh-nav { flex: 1; min-inline-size: 0; }
  .dh-nav ul { display: flex; gap: var(--dh-space-1); margin: 0; padding: 0; list-style: none; overflow-x: auto; scrollbar-width: none; }
  .dh-nav a { display: block; padding: var(--dh-space-2) var(--dh-space-3); color: var(--dh-ink-soft); text-decoration: none; white-space: nowrap; border-block-end: 2px solid transparent; }
  .dh-nav a:hover, .dh-nav a:focus-visible { color: var(--dh-ink); text-decoration: underline; }
  .dh-nav a[aria-current] { color: var(--dh-ink); border-block-end-color: var(--dh-accent); }
  @media (max-width: 47.99rem) {
    .dh-nav { flex-basis: 100%; }
    .dh-nav a { padding-inline: var(--dh-space-2); }
  }

  /* Footer: publisher, licence, source. One row on wide screens, stacked on narrow ones. */
  .dh-foot {
    display: flex; flex-wrap: wrap; align-items: center; gap: var(--dh-space-3) var(--dh-space-6);
    margin-block-start: var(--dh-space-7); padding: var(--dh-space-6) var(--dh-gutter);
    background-color: var(--dh-ground-alt); color: var(--dh-ink-soft);
    font-size: var(--dh-step--1);
  }
  .dh-foot p { margin: 0; }
  .dh-foot a { color: var(--dh-link); }

  /* The footer with a frame follows dhcraft.org: a dark band over the full width whose content
     stands in the page frame, so it lines up with the page. The mark and the name of the site
     stand left, the links of the project right, and the licence in one quiet line below. The
     soft colour is the secondary text colour of the dark scheme. A footer of three plain
     paragraphs keeps the form above. */
  .dh-foot:has(.dh-foot__frame) { display: block; padding: 0; background-color: var(--dh-ink-deep); color: var(--dh-paper); }
  .dh-foot__frame {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: var(--dh-space-4) var(--dh-space-6);
    max-inline-size: var(--dh-page); margin-inline: auto; padding: var(--dh-space-6) var(--dh-gutter) var(--dh-space-5);
  }
  .dh-foot__frame a { color: inherit; }
  .dh-foot__frame a:focus-visible { outline-color: currentColor; }
  .dh-foot__brand { display: flex; align-items: center; gap: var(--dh-space-3); line-height: 1.4; }
  .dh-foot__brand img { inline-size: auto; block-size: 2.75rem; }
  .dh-foot__brand strong { font-family: var(--dh-font-display); font-size: var(--dh-step-0); }
  .dh-foot__links { display: flex; flex-wrap: wrap; gap: var(--dh-space-2) var(--dh-space-5); }
  .dh-foot__links a { text-decoration: none; }
  .dh-foot__links a:hover, .dh-foot__links a:focus-visible { text-decoration: underline; }
  /* A fine line sets the licence apart, as the bar under the footer of dhcraft.org does. */
  .dh-foot__licence { grid-column: 1 / -1; padding-block-start: var(--dh-space-4); border-block-start: var(--dh-hairline) solid rgb(255 255 255 / 0.14); color: #b6bdd2; }
  @media (max-width: 47.99rem) {
    .dh-foot__frame { grid-template-columns: minmax(0, 1fr); }
  }

  /* Icon: an inline SVG drawn in lines of the text colour, always beside the word it marks. */
  .dh-icon { flex: none; inline-size: 1.15em; block-size: 1.15em; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .dh-icon-link { display: inline-flex; align-items: center; gap: var(--dh-space-2); }

  /* Card: one self-contained object with comparable siblings. No shadow, no nested frame.
     A card that is a link as a whole puts the link on its heading and stretches it. */
  .dh-card {
    position: relative; display: grid; gap: var(--dh-space-2); align-content: start;
    padding: var(--dh-space-5);
    background-color: var(--dh-surface); color: var(--dh-ink);
    border: var(--dh-hairline) solid var(--dh-line); border-radius: var(--dh-radius-m);
  }
  .dh-cards { display: grid; gap: var(--dh-space-4); grid-template-columns: repeat(auto-fill, minmax(min(100%, 16rem), 1fr)); }
  .dh-card > * { margin: 0; }
  .dh-card h3 a { color: inherit; text-decoration: none; }
  .dh-card h3 a::after { content: ""; position: absolute; inset: 0; border-radius: inherit; }
  .dh-card:has(h3 a:hover) { border-color: var(--dh-accent); }
  .dh-card:has(h3 a:focus-visible) { outline: var(--dh-focus-ring); outline-offset: var(--dh-focus-offset); }
  .dh-card h3 a:focus-visible { outline: none; }

  /* Button: pill, at least the tap height. Primary fills with the accent. */
  .dh-btn {
    display: inline-flex; align-items: center; justify-content: center; gap: var(--dh-space-2);
    min-block-size: var(--dh-tap); padding: var(--dh-space-2) var(--dh-space-5);
    border: var(--dh-hairline) solid var(--dh-line-strong); border-radius: var(--dh-radius-pill);
    background: transparent; color: var(--dh-ink);
    font: inherit; font-weight: 600; line-height: 1.3; text-decoration: none; cursor: pointer;
  }
  .dh-btn:hover { border-color: var(--dh-accent); color: var(--dh-ink); }
  .dh-btn--primary { background-color: var(--dh-accent); border-color: var(--dh-accent); color: var(--dh-on-accent); }
  .dh-btn--primary:hover { background-color: var(--dh-link); border-color: var(--dh-link); color: var(--dh-on-accent); }
  .dh-btn:disabled { opacity: 0.5; cursor: default; }

  /* Copy field: a figure.dh-code holding one pre, for a prompt, a command or code. It follows
     the rule of the design document: a dashed border, slightly indented, the monospace face,
     and copying as an icon inside the field. dhcraft-copy.js adds the button only where the
     Clipboard API exists and gives it its name as text, which stays the accessible name
     while the icon shows. After copying the icon becomes a tick. */
  .dh-code { display: grid; grid-template-columns: minmax(0, 1fr); margin: var(--dh-space-4) 0 var(--dh-space-4) var(--dh-space-4); }
  .dh-code figcaption { grid-row: 1; margin-block-end: var(--dh-space-1); color: var(--dh-ink-soft); font-size: var(--dh-step--1); }
  .dh-code pre {
    grid-column: 1; grid-row: 2;
    margin: 0; padding: var(--dh-space-4); padding-inline-end: calc(var(--dh-space-4) + 2.25rem);
    overflow-x: auto; background: none; color: var(--dh-ink);
    border: 0.09375rem dashed var(--dh-line-strong); border-radius: var(--dh-radius-m);
    line-height: 1.5; tab-size: 2;
  }
  .dh-copy {
    grid-column: 1; grid-row: 2; justify-self: end; align-self: start;
    inline-size: 2.25rem; block-size: 2.25rem; margin: var(--dh-space-1); padding: 0;
    border: 0; border-radius: var(--dh-radius-s); background: none; color: var(--dh-ink-soft);
    font-size: 0; cursor: pointer;
  }
  .dh-copy::before {
    content: ""; display: block; inline-size: 1.25rem; block-size: 1.25rem; margin: auto; background-color: currentColor;
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='9' y='9' width='11' height='11' rx='2'/%3E%3Cpath d='M5 15V6a2 2 0 0 1 2-2h9'/%3E%3C/svg%3E") center / contain no-repeat;
  }
  .dh-copy[data-state="copied"] { color: var(--dh-accent); }
  .dh-copy[data-state="copied"]::before {
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m5 12.5 4.5 4.5L19 7.5'/%3E%3C/svg%3E");
  }
  .dh-copy:hover { color: var(--dh-ink); background-color: var(--dh-ground-alt); }
  /* A failure shows its words, so the reader knows to select the text by hand. */
  .dh-copy[data-state="failed"] { inline-size: auto; padding-inline: var(--dh-space-2); font-size: var(--dh-step--1); }
  .dh-copy[data-state="failed"]::before { display: none; }

  /* Diagram frame: a figure.dh-figure with an SVG, image or HTML diagram and a caption.
     A diagram wider than the screen scrolls inside the frame, never the page. */
  .dh-figure { display: grid; gap: var(--dh-space-3); margin: var(--dh-space-5) 0; }
  .dh-figure__canvas {
    overflow-x: auto; padding: var(--dh-space-4);
    background-color: var(--dh-ground-alt);
    border: var(--dh-hairline) solid var(--dh-line); border-radius: var(--dh-radius-m);
  }
  .dh-figure__canvas > svg, .dh-figure__canvas > img { display: block; max-inline-size: 100%; block-size: auto; margin-inline: auto; }
  .dh-figure figcaption { max-inline-size: var(--dh-measure); color: var(--dh-ink-soft); font-size: var(--dh-step--1); }

  @media (prefers-reduced-motion: no-preference) {
    .dh-btn, .dh-copy, .dh-card { transition: border-color 0.15s ease, background-color 0.15s ease; }
  }
}
