/* The law explorer, built on the demo's design system (/static/css/main.css): its tokens, line
   rhythm, reset and base styles. This file adds only the explorer's pages, in its own layer,
   which comes after the demo's layers. So no rule here needs extra specificity to win.
   Class names follow the domain vocabulary (docs/tasks/law-site-domain.md §1). */
@layer explorer {

/* ---- tokens ---- */
.explorer {
  --hang: calc(var(--line) * 4);      /* the hang column: numbers, letters, labels; one line narrower than the demo's --labels */
  --indent: calc(var(--hang) + var(--gap)); /* where text starts on a page with a hang column */
  --page: calc(var(--line) * 46);
  --measure: calc(var(--line) * 40);
  --radius: .2em;

  container: explorer / inline-size; /* the phone layout follows the page's own width, so an embed in a narrow pane gets it too */
  width: 100%; /* inline-size containment drops content width, so a shrink-to-fit parent (a grid with auto margins) would collapse it */
  max-width: var(--page);
  margin: 0 auto;
  padding: var(--gap);
  font: var(--small)/var(--line) var(--ui);
  color: var(--ink);
}

/* ---- primitives ---- */
/* A label column and a text column. The article, its provenance, the home groups and the index all hang on it. */
:is(.source-pane, .home-group, .index-decade) {
  display: grid;
  grid-template-columns: var(--hang) minmax(0, 1fr);
  column-gap: var(--gap);
}
/* Rows that pass the two columns down, so a hang and its run share them. A subgrid, not display: contents:
   a paragraph needs a box to be a scroll target. */
:is(.proof, .law-text, .law-numero, .law-alinea, .law-subalinea, .law-info-block) {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: subgrid;
}
/* base.css puts every text block on the baseline grid; these are the spans that act as blocks. */
:is(.index-band, .law-hang, .law-run, .law-info-main, .law-info-col) {
  text-box: trim-both cap alphabetic;
  padding-top: calc(var(--line) - 1cap);
}
/* Labels in the hang column line up on its trailing edge. */
:where(.index-band, .law-hang, .proof dt, .law-info, .home-hang) { /* :where keeps it overridable: :is would weigh as much as .proof dt */
  grid-column: 1;
  text-align: end;
}
:is(.index-band, .law-hang, .law-info-main, .law-info-col, .index-articles, .home-meta) { font-variant-numeric: tabular-nums; }
/* A table cell holding only a figure: digits of one width, aligned on the last one, never split. */
.num { font-variant-numeric: tabular-nums; text-align: end; white-space: nowrap; }
/* Code set apart: a diff, an example. */
:is(.diff, .about-example) {
  padding: var(--s2) var(--s3);
  overflow-x: auto;
  border-radius: var(--radius);
  background: var(--said);
  font: var(--small)/calc(var(--line) * .75) var(--mono);
}
.diff { white-space: pre-wrap; }

/* ---- page chrome ---- */
.explorer h1 { font: inherit; }
.explorer-nav {
  display: flex;
  flex-wrap: wrap;
  column-gap: var(--s2);
  margin-bottom: var(--line);
}

/* ---- home ---- */
.home-top {
  margin: 0 0 var(--s4) var(--indent);
  & h1 { font: var(--big)/calc(var(--line) * 1.5) var(--serif); }
  & p { max-width: calc(var(--line) * 17); }
}
.home-group { align-items: baseline; margin-top: var(--s4); }
.home-hang { font: inherit; color: var(--quiet); }
.home-norms {
  display: grid;
  gap: var(--line);
  & a { font: var(--prose)/var(--line) var(--serif); text-decoration: none; }
  & a:hover { text-decoration: underline; }
}
.home-meta { display: block; color: var(--quiet); }
.home-foot { margin: var(--s4) 0 0 var(--indent); }

/* ---- code index: article numbers by decade ---- */
.index-blocks { margin-top: var(--s2); }
.index-decade {
  --hang: calc(var(--line) * 2); /* bands are short; the article grid gets the room */
  align-items: start;
  scroll-margin-top: var(--s4);
  & + & { margin-top: calc(var(--line) * 1.5); }
}
.index-band { justify-self: end; }
.index-articles {
  grid-column: 2;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(calc(var(--line) * 2), 1fr));
  column-gap: var(--s2);
  & a { display: block; white-space: nowrap; text-decoration: none; }
  & a:hover { text-decoration: underline; }
}

/* ---- article: heading, provenance, law text, info blocks ---- */
.source-pane { max-width: var(--measure); }
.source-head {
  grid-column: 2;
  position: static;
  margin: 0;
  padding: 0;
  box-shadow: none;
  & h2 { font: var(--prose)/var(--line) var(--ui); text-wrap: balance; }
}
.proof {
  align-items: baseline;
  margin-top: var(--line);
  & > div { display: contents; } /* the demo's layout makes it a subgrid, which collapses off a grid page */
  & dt { white-space: nowrap; }
  & dd { grid-column: 2; min-width: 0; overflow-wrap: break-word; } /* the demo's anywhere splits a host mid-word; hosts carry <wbr> before dots */
}
.law-text {
  align-items: start;
  margin-top: var(--s4);
  hanging-punctuation: first allow-end;
  font: var(--prose)/var(--line) var(--serif);
  hyphens: auto; /* the page is lang="pt-PT"; long words ("intracomunitário") would leave a phone line half empty */
  hyphenate-limit-chars: 8 4 3; /* only long words, never leaving fewer than 4 letters before or 3 after the hyphen */
  & a { color: inherit; }
  & [data-outcome="unresolved"] { text-decoration: underline dotted; text-underline-offset: .15em; }
}
:is(.law-heading, .law-epigrafe, .law-plain, .law-run, .law-wording-note) { grid-column: 2; min-width: 0; }
:is(.law-heading, .law-epigrafe) { text-wrap: balance; hyphens: manual; }
.law-heading { hanging-punctuation: first; }
.law-epigrafe { font: inherit; }
:is(.law-numero, .law-alinea, .law-subalinea) {
  align-items: start;
  scroll-margin-top: var(--s4);
}
.law-numero > * { margin-top: var(--line); }
.law-wording-note { font: var(--small)/var(--line) var(--ui); hanging-punctuation: first; }
.law-hang { justify-self: end; }

/* The run is trimmed to cap height and baseline and padded by a line gap above, so its own background
   would start a gap above the text and cut the descenders. The marker sits behind it instead, with an
   even margin around cap height and baseline. */
:is(:target, .law-hit) > .law-run {
  position: relative;
  isolation: isolate;
  &::before {
    content: "";
    position: absolute;
    z-index: -1;
    inset: calc(var(--line) - 1cap - var(--s1)) calc(var(--s1) * -1) calc(var(--s1) * -1);
    border-radius: var(--radius);
    background: var(--marker);
  }
}

.law-info-block {
  align-items: start;
  margin-top: var(--s4);
  padding-block: var(--line);
  box-shadow: inset 0 1px 0 var(--ink);
  font: var(--small)/var(--line) var(--ui);
  & + & { margin-top: var(--line); padding-top: 0; box-shadow: none; }
  & a { color: inherit; }
}
.law-info { text-wrap: balance; }
.law-info-rows {
  grid-column: 2;
  display: grid;
  grid-template-columns: max-content minmax(0, max-content); /* the run wraps when the pane is narrower than it */
  column-gap: var(--s1);
  align-items: baseline;
  width: max-content;
  max-width: 100%;
}
.law-info-row { display: contents; }
.law-info-main { text-align: end; }

/* A data table from the law (rates, brackets): in the text column, scrolling sideways when wider. */
.law-table {
  grid-column: 2;
  display: block;
  max-width: max-content;
  overflow-x: auto;
  margin-top: var(--line);
  font: var(--small)/var(--line) var(--ui);
  font-variant-numeric: tabular-nums;
  hyphenate-limit-chars: 10 5 5; /* a narrow header splits a long word once ("percen-tagens"), not in three */
  & td { text-wrap: balance; padding: var(--s1) var(--s2); vertical-align: top; box-shadow: inset 0 -1px 0 var(--said), inset -1px 0 0 var(--said); }
}

/* ---- history, amendments, report ---- */
.report td { padding-inline-end: var(--s2); vertical-align: top; font-variant-numeric: tabular-nums; }

/* ---- about: the article layout with section headings, prose, examples and tables ---- */
.about {
  & .law-epigrafe { margin: 0; }
  & .law-epigrafe ~ .law-epigrafe { margin-top: var(--s4); }
  & .law-plain { margin-top: var(--line); }
  & .law-alinea > * { margin-top: var(--s2); }
  & :is(.about-label, .about-example, .about-table) { grid-column: 2; min-width: 0; }
  & .about-label { margin-top: calc(var(--line) * 1.5); font: var(--small)/var(--line) var(--ui); color: var(--quiet); }
  & :is(.about-example, .about-table) { margin-top: var(--line); }
  & .about-label + * { margin-top: var(--s1); }
  /* mono runs large beside the serif */
  & .law-text :not(pre) > code { font: .85em/1 var(--mono); padding: .1em .3em; border-radius: var(--radius); background: var(--said); }
}
.about-table {
  font: var(--small)/var(--line) var(--ui);
  & :is(th, td) { padding: var(--s1) var(--s3) var(--s1) 0; vertical-align: top; box-shadow: inset 0 -1px 0 var(--said); }
  & td:first-child { white-space: nowrap; }
  & code { font-size: .95em; }
}

/* ---- phone: no room for a fixed hang, so each hang is as wide as what hangs in it ---- */
/* Denser on a phone: half-line gutters, and the big vertical step drops to one line. A container can't query
   itself, so the page's own padding follows the viewport and everything inside follows the container. */
@media (width <= 30rem) { .explorer { --gap: var(--s2); } }
@container explorer (width <= 28.5rem) { /* 30rem less the phone padding, so phones switch where they did */
  .explorer > * { --hang: max-content; --gap: var(--s2); --s4: var(--line); }
  /* Terms (provenance, info headings) and markers (numbers, letters) differ several times in width, so they
     get a hang each: the page's holds the terms, the law text, a grid of its own here, holds the markers.
     Both start at the leading edge. */
  .law-text { grid-template-columns: var(--hang) minmax(0, 1fr); column-gap: var(--gap); }
  .law-info { contain: inline-size; } /* a heading wraps to the terms' width instead of setting it */
  .law-info-rows { grid-template-columns: minmax(0, max-content) max-content; } /* the act wraps, its date stays whole */
  .index-decade { --hang: 3ch; } /* each decade is its own grid: one width, "100" in tabular digits, keeps them aligned */
  .index-decade + .index-decade { margin-top: var(--line); }
  .index-articles { grid-template-columns: repeat(auto-fill, minmax(calc(var(--line) * 1.75), 1fr)); column-gap: var(--s1); } /* 42px; the widest label, "101-D.º", is 48px and spills into the gap */
  :is(.source-head, .law-heading, .law-epigrafe, .law-plain, .law-table) { grid-column: 1 / -1; }
  .law-table td { padding-inline: var(--s1); } /* figures don't wrap, so the columns give up their padding instead */
  .home-group { grid-template-columns: minmax(0, 1fr); }
  .home-hang { text-align: start; margin-bottom: var(--line); }
  .home-top, .home-foot { margin-inline-start: 0; }
  .about :is(.about-label, .about-example, .about-table) { grid-column: 1 / -1; }
  .about-table { display: block; overflow-x: auto; }
}

}
