/* Deep Time — a stratigraphic instrument for 13.8 billion years.
   Palette: cool mineral neutrals (limestone / basalt). No UI accent hue —
   the seven category colours own all colour on the page. */

:root {
  color-scheme: light;

  --ground:       #eef0ec;
  --surface:      #fafbf9;
  --surface-2:    #f2f4f0;
  --surface-3:    #e7eae5;

  --ink:          #10120f;
  --ink-2:        #545a52;
  --ink-3:        #8a9187;
  --ink-4:        #aab1a6;

  --rule:         #dde0da;
  --rule-strong:  #c6cbc2;

  /* Categorical series — validated (dataviz six checks) against #fafbf9 */
  --c1: #2a78d6;  /* Cosmos & Earth       */
  --c2: #008300;  /* Life & Extinctions   */
  --c3: #e87ba4;  /* Human Origins        */
  --c4: #eda100;  /* Empires & Politics   */
  --c5: #1baf7a;  /* Religion & Thought   */
  --c6: #eb6834;  /* Culture & Art        */
  --c7: #4a3aa7;  /* Science & Technology */

  --shadow: 0 1px 2px rgba(16, 18, 15, .06), 0 8px 24px rgba(16, 18, 15, .07);

  --font-ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
             Helvetica, Arial, sans-serif;
  --font-data: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
               "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --ground:      #0a0c0b;
    --surface:     #171a18;
    --surface-2:   #1f231f;
    --surface-3:   #272c27;
    --ink:         #f2f4f0;
    --ink-2:       #a8b0a6;
    --ink-3:       #6e766c;
    --ink-4:       #565e55;
    --rule:        #262a26;
    --rule-strong: #363c36;
    --c1: #3987e5;
    --c2: #008300;
    --c3: #d55181;
    --c4: #c98500;
    --c5: #199e70;
    --c6: #d95926;
    --c7: #9085e9;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 8px 28px rgba(0, 0, 0, .45);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ground:      #0a0c0b;
  --surface:     #171a18;
  --surface-2:   #1f231f;
  --surface-3:   #272c27;
  --ink:         #f2f4f0;
  --ink-2:       #a8b0a6;
  --ink-3:       #6e766c;
  --ink-4:       #565e55;
  --rule:        #262a26;
  --rule-strong: #363c36;
  --c1: #3987e5;
  --c2: #008300;
  --c3: #d55181;
  --c4: #c98500;
  --c5: #199e70;
  --c6: #d95926;
  --c7: #9085e9;
  --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 8px 28px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

/* ── Masthead ─────────────────────────────────────────────────────────── */

.masthead {
  display: flex;
  align-items: baseline;
  gap: 18px;
  padding: 14px 22px 12px;
  border-bottom: 1px solid var(--rule);
  background: var(--surface);
  flex: none;
}

.masthead h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 660;
  letter-spacing: -.02em;
  white-space: nowrap;
}

.masthead .lede {
  margin: 0;
  color: var(--ink-2);
  font-size: 12.5px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.masthead .spacer { flex: 1 1 auto; }

.eyebrow {
  font-family: var(--font-data);
  font-size: 10px;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--ink-3);
}

/* ── Control rail ─────────────────────────────────────────────────────── */

.rail {
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 10px 22px;
  border-bottom: 1px solid var(--rule);
  background: var(--surface);
  flex: none;
  flex-wrap: wrap;
  row-gap: 10px;
}

.field {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}

.field > .eyebrow { flex: none; }

/* Segmented control — the scale toggle, the thesis of the page */
.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--rule);
  border-radius: 7px;
  padding: 2px;
  gap: 2px;
}

.segmented button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 12px;
  font-weight: 560;
  letter-spacing: .01em;
  padding: 5px 13px;
  border-radius: 5px;
  cursor: pointer;
  transition: background .16s ease, color .16s ease;
}

.segmented button:hover { color: var(--ink); }

.segmented button[aria-pressed="true"] {
  background: var(--ink);
  color: var(--surface);
}

/* Blend slider — scrub continuously between the two scales */
.blend {
  appearance: none;
  width: 116px;
  height: 18px;
  background: transparent;
  cursor: ew-resize;
}
.blend::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 2px;
  background: var(--rule-strong);
}
.blend::-moz-range-track {
  height: 3px;
  border-radius: 2px;
  background: var(--rule-strong);
}
.blend::-webkit-slider-thumb {
  appearance: none;
  width: 13px;
  height: 13px;
  margin-top: -5px;
  border-radius: 50%;
  background: var(--ink);
  border: 2px solid var(--surface);
}
.blend::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--ink);
  border: 2px solid var(--surface);
}

.field-window { flex: 1 1 320px; min-width: 240px; }

/* Zoom ladder */
.ladder {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: thin;
  padding-bottom: 1px;
}

.ladder button,
.ghost-btn {
  appearance: none;
  flex: none;
  background: transparent;
  border: 1px solid var(--rule);
  color: var(--ink-2);
  font-family: var(--font-data);
  font-size: 11px;
  letter-spacing: .02em;
  padding: 4px 9px;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .14s ease, color .14s ease, border-color .14s ease;
}

.ladder button:hover,
.ghost-btn:hover {
  background: var(--surface-3);
  color: var(--ink);
  border-color: var(--rule-strong);
}

.ladder button[aria-pressed="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--surface);
}

/* ── Search ───────────────────────────────────────────────────────────── */

.search-trigger {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--rule);
  border-radius: 6px;
  color: var(--ink-3);
  font-family: var(--font-ui);
  font-size: 12.5px;
  padding: 6px 8px 6px 10px;
  width: 230px;
  cursor: pointer;
  transition: border-color .14s ease, color .14s ease;
}
.search-trigger:hover { border-color: var(--rule-strong); color: var(--ink-2); }
.search-trigger > span:nth-child(2) { flex: 1; text-align: left; }
.search-trigger kbd {
  font-family: var(--font-data);
  font-size: 10px;
  border: 1px solid var(--rule-strong);
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--ink-3);
}

/* Drawn rather than an icon font, so it inherits colour and needs no asset. */
.search-icon {
  position: relative;
  flex: none;
  width: 12px;
  height: 12px;
  border: 1.5px solid currentColor;
  border-radius: 50%;
  opacity: .75;
}
.search-icon::after {
  content: "";
  position: absolute;
  right: -4px;
  bottom: -3px;
  width: 6px;
  height: 1.5px;
  background: currentColor;
  transform: rotate(45deg);
  border-radius: 1px;
}

/* ── Command palette ──────────────────────────────────────────────────── */

.palette {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  /* start, not stretch — otherwise the box fills the row and a three-result
     search leaves a tall empty void below the last row. */
  justify-items: center;
  align-items: start;
  padding: 12vh 20px 20px;
  visibility: hidden;
  opacity: 0;
  transition: opacity .14s ease, visibility 0s linear .14s;
}
.palette[data-open="1"] {
  visibility: visible;
  opacity: 1;
  transition: opacity .14s ease;
}

/* Light on purpose — the timeline filters live behind the palette as you
   type, so matches should stay visible through the scrim. */
.palette-scrim {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--ground) 62%, transparent);
  backdrop-filter: blur(2px);
}

.palette-box {
  position: relative;
  width: min(620px, 100%);
  max-height: min(60vh, 560px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  transform: translateY(-6px) scale(.99);
  transition: transform .16s cubic-bezier(.22, .61, .36, 1);
}
.palette[data-open="1"] .palette-box { transform: none; }

.palette-field {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 14px;
  border-bottom: 1px solid var(--rule);
  color: var(--ink-3);
  flex: none;
}
.palette-field input {
  appearance: none;
  flex: 1;
  min-width: 0;
  background: transparent;
  border: 0;
  outline: none;
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
}
.palette-field input::placeholder { color: var(--ink-4); }

.palette-clear {
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--ink-3);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 4px;
}
.palette-clear:hover { color: var(--ink); }

.palette-list {
  list-style: none;
  margin: 0;
  padding: 5px;
  overflow-y: auto;
  flex: 0 1 auto;      /* take only what the results need, up to the max */
  scrollbar-width: thin;
}

.palette-list li {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 7px;
  cursor: pointer;
  scroll-margin: 6px;
}
.palette-list li[aria-selected="true"] { background: var(--surface-3); }

.palette-list .p-chip {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 2px;
  background: var(--sw);
  transform: translateY(-1px);
}
.palette-list .p-name {
  flex: 1;
  min-width: 0;
  color: var(--ink);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.palette-list .p-name mark {
  background: transparent;
  color: var(--ink);
  font-weight: 680;
}
.palette-list .p-cat {
  flex: none;
  color: var(--ink-4);
  font-size: 10.5px;
  font-family: var(--font-data);
  letter-spacing: .04em;
}
.palette-list .p-when {
  flex: none;
  color: var(--ink-3);
  font-family: var(--font-data);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.palette-empty {
  padding: 26px 14px 30px;
  text-align: center;
  color: var(--ink-3);
  font-size: 13px;
}
.palette-empty b { color: var(--ink); font-weight: 600; }

.palette-foot {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 13px;
  border-top: 1px solid var(--rule);
  background: var(--surface-2);
  color: var(--ink-4);
  font-size: 11px;
  flex: none;
}
.palette-foot kbd {
  font-family: var(--font-data);
  font-size: 10px;
  border: 1px solid var(--rule-strong);
  border-radius: 3px;
  padding: 0 4px;
  margin-right: 2px;
  color: var(--ink-3);
}
#palette-count { font-family: var(--font-data); font-variant-numeric: tabular-nums; }

@media (max-width: 620px) {
  .palette { padding-top: 6vh; }
  .palette-list .p-cat { display: none; }
}

/* ── Legend / category filters ────────────────────────────────────────── */

.legend {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}

.legend button {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: 1px solid var(--rule);
  border-radius: 20px;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 11.5px;
  font-weight: 500;
  padding: 4px 11px 4px 8px;
  cursor: pointer;
  transition: background .14s ease, color .14s ease, opacity .14s ease;
}

.legend button:hover { background: var(--surface-3); color: var(--ink); }

.legend button[aria-pressed="false"] {
  opacity: .42;
  text-decoration: line-through;
  text-decoration-thickness: 1px;
}

.legend .swatch {
  width: 9px;
  height: 9px;
  border-radius: 2.5px;
  flex: none;
  background: var(--sw);
}

.legend .count {
  font-family: var(--font-data);
  font-size: 10px;
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}

/* ── Plot ─────────────────────────────────────────────────────────────── */

.stage {
  position: relative;
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  background: var(--surface);
  margin: 0;
  overflow: hidden;
}

/* The plot claims whatever width the panel leaves it. min-width:0 is what
   actually lets a flex child shrink below its content size. */
.plot-wrap {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
}

#plot {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}
#plot.dragging { cursor: grabbing; }

/* Readout — current window, pinned over the plot */
.readout {
  position: absolute;
  left: 16px;
  bottom: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 5px 10px;
  font-family: var(--font-data);
  font-size: 11px;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  backdrop-filter: blur(6px);
}
.readout b { color: var(--ink); font-weight: 600; }

/* Tooltip */
.tip {
  position: absolute;
  z-index: 20;
  max-width: 320px;
  background: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 10px 12px;
  pointer-events: none;
  opacity: 0;
  transform: translateY(3px);
  transition: opacity .12s ease, transform .12s ease;
}
.tip[data-show="1"] { opacity: 1; transform: translateY(0); }

.tip .tip-cat {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-family: var(--font-data);
  font-size: 9.5px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin-bottom: 4px;
}
.tip .tip-cat i {
  width: 8px; height: 8px; border-radius: 2px; background: var(--sw);
  display: inline-block;
}
.tip h3 {
  margin: 0 0 2px;
  font-size: 13.5px;
  font-weight: 620;
  letter-spacing: -.01em;
  text-wrap: balance;
}
.tip .tip-when {
  font-family: var(--font-data);
  font-size: 11.5px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  margin-bottom: 6px;
}
.tip p {
  margin: 0 0 5px;
  font-size: 12px;
  color: var(--ink-2);
  line-height: 1.45;
}
.tip .tip-sig {
  font-size: 11.5px;
  color: var(--ink-2);
  border-left: 2px solid var(--rule-strong);
  padding-left: 8px;
  margin: 0;
}

/* ── Detail panel ─────────────────────────────────────────────────────── */

/* Animating width rather than sliding over the plot: the timeline keeps all
   its content, just narrower. The fixed-width inner wrapper stops the text
   from reflowing on every frame of the transition. */
.panel {
  flex: 0 0 auto;
  width: 0;
  background: var(--surface);
  overflow: hidden;
  transition: width .28s cubic-bezier(.22, .61, .36, 1);
}

.panel[data-open="1"] {
  width: 340px;
  border-right: 1px solid var(--rule);
}

.panel-inner {
  position: relative;
  width: 340px;
  height: 100%;
  padding: 18px 20px;
  overflow-y: auto;
}
.panel .close {
  position: absolute;
  top: 12px;
  right: 14px;
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--ink-3);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  padding: 4px;
}
.panel .close:hover { color: var(--ink); }

.panel h2 {
  margin: 6px 0 4px;
  font-size: 18px;
  font-weight: 660;
  letter-spacing: -.02em;
  text-wrap: balance;
  padding-right: 26px;
}

.panel .when {
  font-family: var(--font-data);
  font-size: 13px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.panel .meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 12px 0 14px;
}
.panel .meta span {
  font-family: var(--font-data);
  font-size: 10px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--ink-2);
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 3px 7px;
}

.panel p { font-size: 13px; color: var(--ink-2); line-height: 1.55; }

/* A caveat on a contested date is worth reading before the date is quoted, so
   it sits with the description rather than in a footnote. */
.panel p.caveat {
  font-size: 12px;
  color: var(--ink-3);
  border-left: 2px solid var(--rule-strong);
  padding-left: 9px;
}

.panel .source {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  margin-top: 14px;
  padding: 7px 12px;
  border: 1px solid var(--rule-strong);
  border-radius: 6px;
  color: var(--ink);
  font-size: 12.5px;
  font-weight: 550;
  text-decoration: none;
}

.panel .source:hover {
  background: var(--surface-2);
  border-color: var(--ink-3);
}

/* Available to screen readers, out of the visual flow. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.panel h4 {
  margin: 18px 0 7px;
  font-family: var(--font-data);
  font-size: 10px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-3);
  font-weight: 500;
}

.neighbours { list-style: none; margin: 0; padding: 0; display: grid; gap: 1px; }
.neighbours li { display: block; }
.neighbours button {
  appearance: none;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 5px;
  padding: 6px 8px;
  cursor: pointer;
  color: var(--ink-2);
  font-family: var(--font-ui);
  font-size: 12px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: baseline;
}
.neighbours button:hover { background: var(--surface-2); color: var(--ink); }
.neighbours .n-when {
  font-family: var(--font-data);
  font-size: 10.5px;
  color: var(--ink-3);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* ── Table view (the accessible path to every datum) ──────────────────── */

.table-wrap {
  position: absolute;
  inset: 0;
  background: var(--surface);
  z-index: 25;
  overflow: auto;
  padding: 18px 22px 40px;
  display: none;
}
.table-wrap[data-open="1"] { display: block; }

table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  text-align: left;
  font-family: var(--font-data);
  font-size: 10px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--ink-3);
  font-weight: 500;
  padding: 8px 10px;
  border-bottom: 1px solid var(--rule-strong);
  white-space: nowrap;
}
tbody td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--rule);
  color: var(--ink-2);
  vertical-align: top;
}
tbody tr:hover td { background: var(--surface-2); }
td.t-name { color: var(--ink); font-weight: 520; }
td.t-name a { color: inherit; text-decoration-color: var(--ink-4); text-underline-offset: 2px; }
td.t-name a:hover { text-decoration-color: var(--ink); }
td.t-when {
  font-family: var(--font-data);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
td.t-cat { white-space: nowrap; }
td.t-cat i {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 2px;
  background: var(--sw);
  margin-right: 6px;
}

/* ── Footnote ─────────────────────────────────────────────────────────── */

.footnote {
  flex: none;
  border-top: 1px solid var(--rule);
  background: var(--surface);
  padding: 8px 22px 9px;
  color: var(--ink-3);
  font-size: 11.5px;
  display: flex;
  gap: 18px;
  align-items: center;
  flex-wrap: wrap;
}
.footnote kbd {
  font-family: var(--font-data);
  font-size: 10px;
  border: 1px solid var(--rule-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--ink-2);
}

.empty-note {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  color: var(--ink-3);
  font-size: 13px;
  pointer-events: none;
}
.empty-note[data-show="1"] { display: grid; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    transition-duration: .001ms !important;
  }
}

@media (max-width: 860px) {
  .masthead .lede { display: none; }
  .search { width: 140px; }

  /* Below this width there isn't enough room to split: taking 340px from the
     plot would leave nothing worth looking at, so the panel goes back to
     overlaying — the behaviour the wide layout exists to avoid. */
  .panel {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 30;
    width: 0;
    border-right: 1px solid var(--rule);
    box-shadow: var(--shadow);
  }
  .panel[data-open="1"] { width: min(340px, 86vw); }
  .panel-inner { width: min(340px, 86vw); }
}

/* ── Icon buttons (mobile header) ─────────────────────────────────────── */

.icon-btn {
  display: none;              /* desktop has room for labelled controls */
  appearance: none;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  flex: none;
  background: transparent;
  border: 1px solid var(--rule);
  border-radius: 9px;
  color: var(--ink-2);
  cursor: pointer;
}
.icon-btn:active { background: var(--surface-3); }

.menu-icon,
.menu-icon::before,
.menu-icon::after {
  display: block;
  width: 15px;
  height: 1.5px;
  background: currentColor;
  border-radius: 1px;
}
.menu-icon { position: relative; }
.menu-icon::before,
.menu-icon::after { content: ""; position: absolute; left: 0; }
.menu-icon::before { top: -5px; }
.menu-icon::after { top: 5px; }

/* ── Mobile ───────────────────────────────────────────────────────────── */
/* The desktop layout assumes horizontal room it does not have on a phone: a
   150px lane gutter is 38% of a 390px screen, and two control rails plus a
   masthead eat a third of the height. Below 720px the chrome collapses to one
   compact row plus a sheet, and the canvas reclaims the rest. */

@media (max-width: 720px) {
  html, body { height: 100svh; }

  .icon-btn { display: inline-flex; }

  .masthead {
    gap: 10px;
    padding: 10px 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    align-items: center;
  }
  .masthead h1 { font-size: 16px; }
  .masthead .lede { display: none; }
  #stat-count { font-size: 9px; letter-spacing: .1em; text-align: right; line-height: 1.25; }

  /* Relocated into the sheet by JS on mobile, so they are not lost. */
  .masthead .view-switch,
  .masthead #btn-theme { display: none; }

  .rail-primary {
    gap: 12px;
    padding: 8px 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .rail-primary::-webkit-scrollbar { display: none; }
  /* The scale field must not shrink: it was collapsing to 51px while its
     176px segmented control overflowed on top of the ladder. Fields keep
     their intrinsic width; only the ladder flexes, and it scrolls. */
  .rail-primary > .field { flex: none; gap: 0; }
  .rail-primary > .field-window { flex: 1 1 auto; min-width: 0; }
  .rail-primary .segmented { flex: none; }
  .rail-primary .eyebrow,
  .rail-primary .blend,
  .rail-primary .search-trigger { display: none; }
  .rail-primary .segmented button { padding: 7px 14px; font-size: 12.5px; }
  .ladder button { padding: 7px 11px; font-size: 11.5px; }

  /* Layers become a sheet under the header rather than a permanent row. */
  .rail-layers {
    position: absolute;
    z-index: 60;
    top: 0;
    left: 0;
    right: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
    padding: 14px;
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
    background: var(--surface);
    border-bottom: 1px solid var(--rule-strong);
    box-shadow: var(--shadow);
    transform: translateY(-101%);
    transition: transform .24s cubic-bezier(.22, .61, .36, 1);
  }
  .rail-layers[data-open="1"] { transform: none; }
  .rail-layers .field { flex-direction: column; align-items: stretch; gap: 9px; }
  .rail-layers .legend button { padding: 8px 13px 8px 10px; font-size: 12.5px; }

  .sheet-extras {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    padding-top: 12px;
    border-top: 1px solid var(--rule);
  }
  .sheet-extras .segmented button { padding: 7px 14px; }

  .stage { position: relative; }

  /* Detail becomes a bottom sheet — the phone convention, and it leaves the
     timeline visible above it rather than covering the whole screen. */
  .panel {
    position: absolute;
    z-index: 40;
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    /* Flex column so the inner element can be bounded by the sheet and scroll
       inside it. With the panel at auto height, `height:100%` on the inner
       resolved to auto, so it grew to its full content height and the panel's
       overflow:hidden simply clipped the overspill with no way to reach it. */
    display: flex;
    flex-direction: column;
    max-height: 72%;
    border-right: 0;
    border-top: 1px solid var(--rule-strong);
    border-radius: 14px 14px 0 0;
    box-shadow: var(--shadow);
    transform: translateY(101%);
    transition: transform .26s cubic-bezier(.22, .61, .36, 1);
  }
  .panel[data-open="1"] { width: 100%; transform: none; }
  .panel-inner {
    width: 100%;
    height: auto;
    flex: 1 1 auto;
    min-height: 0;          /* lets the flex child shrink so it can scroll */
    overflow-y: auto;
    overscroll-behavior: contain;   /* scrolling the sheet must not pan the page */
    /* Top padding clears the grab handle. */
    padding: 22px 16px calc(24px + env(safe-area-inset-bottom));
    -webkit-overflow-scrolling: touch;
  }
  /* A grab handle, so the sheet reads as a sheet. */
  .panel::before {
    content: "";
    position: absolute;
    top: 7px;
    left: 50%;
    width: 34px;
    height: 4px;
    margin-left: -17px;
    border-radius: 2px;
    background: var(--rule-strong);
  }
  .panel .close { top: 14px; right: 12px; font-size: 24px; }
  .panel h2 { margin-top: 12px; font-size: 17px; }

  /* Bottom-left collides with the density rail once the gutter is gone. */
  .readout { left: auto; right: 8px; top: 6px; bottom: auto; font-size: 10px; padding: 4px 8px; }

  .palette { padding: 0; }
  .palette-box {
    width: 100%;
    max-height: 100svh;
    height: 100svh;
    border: 0;
    border-radius: 0;
  }
  .palette-field { padding: calc(12px + env(safe-area-inset-top)) 14px 12px; }
  .palette-list { flex: 1 1 auto; }
  .palette-list li { padding: 11px 10px; }
  .palette-foot {
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    justify-content: flex-end;
  }
  /* Keyboard hints mean nothing on a touch device; keep only the count. */
  .palette-foot > span:not(#palette-count) { display: none; }

  .table-wrap { padding: 14px 12px 40px; }
  table { font-size: 12px; }
  thead th:nth-child(3), tbody td:nth-child(3),
  thead th:nth-child(4), tbody td:nth-child(4) { display: none; }

  .footnote {
    padding: 7px 12px calc(7px + env(safe-area-inset-bottom));
    font-size: 10.5px;
  }
  .footnote > span:first-child { display: none; }   /* keyboard hints */
  #foot-note { font-size: 10px; line-height: 1.35; }
}

/* Very narrow: drop the count to buy back header room. */
@media (max-width: 400px) {
  #stat-count { display: none; }
  .masthead h1 { font-size: 15px; }
}

/* ── Reckoning switch ─────────────────────────────────────────────────── */

.atlas-switch[hidden] { display: none; }
.atlas-switch button { white-space: nowrap; }
.atlas-switch button { font-size: 11.5px; padding: 5px 11px; }

/* A landmark line: where another tradition's whole timeline would sit. */
.readout .landmark { color: var(--ink-4); }

@media (max-width: 720px) {
  .masthead .atlas-switch { display: none; }   /* lives in the sheet on mobile */
}

/* ── Units sheet ──────────────────────────────────────────────────────── */

.units-sheet {
  position: fixed;
  inset: 0;
  z-index: 210;
  display: grid;
  justify-items: center;
  align-items: start;
  padding: 8vh 20px 20px;
  visibility: hidden;
  opacity: 0;
  transition: opacity .16s ease, visibility 0s linear .16s;
}
.units-sheet[data-open="1"] { visibility: visible; opacity: 1; transition: opacity .16s ease; }

.units-scrim {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--ground) 82%, transparent);
  backdrop-filter: blur(3px);
}

.units-box {
  position: relative;
  width: min(680px, 100%);
  max-height: min(78vh, 720px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  transform: translateY(-6px);
  transition: transform .18s cubic-bezier(.22, .61, .36, 1);
}
.units-sheet[data-open="1"] .units-box { transform: none; }

.units-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 16px 18px 13px;
  border-bottom: 1px solid var(--rule);
  flex: none;
}
.units-eyebrow {
  margin: 0 0 3px;
  font-family: var(--font-data);
  font-size: 10px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-3);
}
.units-head h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -.02em;
  text-wrap: balance;
}
.units-close {
  margin-left: auto;
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--ink-3);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 4px;
}
.units-close:hover { color: var(--ink); }

.units-body {
  padding: 16px 18px 22px;
  overflow-y: auto;
  overscroll-behavior: contain;
  flex: 1 1 auto;
  min-height: 0;
}

.units-lede {
  margin: 0 0 6px;
  font-size: 14.5px;
  font-weight: 560;
  color: var(--ink);
  line-height: 1.45;
  text-wrap: balance;
}
.units-intro { margin: 0 0 18px; font-size: 13px; color: var(--ink-2); line-height: 1.55; }

.units-h {
  margin: 20px 0 9px;
  font-family: var(--font-data);
  font-size: 10px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-3);
}

/* The ladder of units. Each step shows what it is built from. */
.units-chain { display: grid; gap: 1px; }
.chain-step {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 14px;
  padding: 9px 11px;
  border-radius: 7px;
  background: var(--surface-2);
}
.chain-term { color: var(--ink); font-size: 13px; font-weight: 570; }
.chain-native {
  color: var(--ink-3);
  font-size: 11.5px;
  font-style: italic;
  margin-left: 6px;
  font-weight: 400;
}
.chain-value {
  font-family: var(--font-data);
  font-size: 12px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  text-align: right;
}
.chain-deriv {
  grid-column: 1 / -1;
  font-family: var(--font-data);
  font-size: 10.5px;
  color: var(--ink-3);
}
.chain-note { grid-column: 1 / -1; font-size: 12px; color: var(--ink-2); line-height: 1.45; }

/* The tradition's own image for the size of its unit. */
.units-story {
  margin-bottom: 10px;
  padding: 12px 14px;
  border-left: 2px solid var(--rule-strong);
  background: var(--surface-2);
  border-radius: 0 7px 7px 0;
}
.units-story h4 { margin: 0 0 2px; font-size: 13px; font-weight: 620; }
.units-story .ref {
  font-family: var(--font-data);
  font-size: 10px;
  color: var(--ink-3);
  letter-spacing: .06em;
}
.units-story p { margin: 5px 0 0; font-size: 12.5px; color: var(--ink-2); line-height: 1.55; }

.units-conv { display: grid; gap: 7px; }
.units-conv div { font-size: 12.5px; color: var(--ink-2); line-height: 1.5; }
.units-conv b { color: var(--ink); font-weight: 600; }
.units-conv .caveat { color: var(--ink-3); font-size: 12px; }

.units-landmark {
  margin-top: 18px;
  padding: 12px 14px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--ink-2);
  line-height: 1.55;
}

@media (max-width: 720px) {
  .units-sheet { padding: 0; }
  .units-box { width: 100%; height: 100svh; max-height: 100svh; border: 0; border-radius: 0; }
  .units-head { padding-top: calc(14px + env(safe-area-inset-top)); }
  .units-body { padding-bottom: calc(28px + env(safe-area-inset-bottom)); }
  .masthead #btn-units { display: none; }   /* lives in the sheet on mobile */
}
