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

html, body {
  margin: 0;
  height: 100%;
  /* Tapping a chip was selecting its label, leaving grey highlight boxes over
     the controls. Text in the panel and year list stays selectable below. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  max-width: 100%;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
}

body {
  display: grid;
  /* The minimap row is auto rather than fixed so it can carry the bottom
     safe-area inset without the canvas losing height to it. */
  grid-template-rows: var(--header-h) 1fr auto;
  overflow: hidden;
  /* 100% resolves against a viewport that excludes mobile browser chrome, so
     the last row ends up underneath the home indicator and the app switcher.
     dvh tracks the chrome as it shows and hides. */
  height: 100dvh;
}

/* ---------------------------------------------------------------- header */

.header {
  display: flex;
  align-items: center;
  gap: var(--step-4);
  padding: 0 var(--step-4);
  border-bottom: 1px solid var(--rule);
  background: var(--bg-raised);
  /* min-width: 0 is what actually lets the header shrink; without it the
     header forces the page wider than the screen, which widens the layout
     viewport and stops every max-width query below from ever matching.
     Overflow must stay visible - the search and date dropdowns are absolutely
     positioned children, and clipping here cuts them off entirely. */
  min-width: 0;
  overflow: visible;
}

.header > * { min-width: 0; }

.header__title {
  flex: 0 1 auto;
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------------------------------------------------------------- search */

.search {
  position: relative;
  /* Pushes itself and the date range to the right edge, leaving the era jumps
     next to the title on the left. */
  margin-left: auto;
  flex: 0 1 260px;
  max-width: 260px;
  min-width: 110px;
}

.search__input {
  width: 100%;
  min-height: var(--control-h);
  padding: 5px var(--step-5) 5px var(--step-3);
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--bg-sunken);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
}

.search__input:focus-visible { outline: 2px solid var(--select); outline-offset: 1px; }

.search__key {
  position: absolute;
  right: var(--step-2);
  top: 50%;
  transform: translateY(-50%);
  padding: 0 5px;
  border: 1px solid var(--rule);
  border-radius: 4px;
  color: var(--ink-faint);
  font: inherit;
  font-size: 11px;
  pointer-events: none;
}

.search__input:focus + .search__key { display: none; }

.results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 30;
  margin: 0;
  padding: var(--step-1);
  list-style: none;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: var(--bg-raised);
  box-shadow: 0 10px 28px -14px rgb(0 0 0 / 0.5);
  max-height: 60vh;
  overflow-y: auto;
}

.results__item {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--step-3);
  padding: 5px var(--step-2);
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
}

.results__item[data-active="true"],
.results__item:hover { background: var(--bg-sunken); }

.results__name { color: var(--ink); }

.results__when {
  color: var(--ink-faint);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ----------------------------------------------------------- date range */

.range {
  display: flex;
  align-items: center;
  gap: var(--step-1);
  flex: 0 0 auto;
}

.range__input {
  width: 94px;
  min-height: var(--control-h);
  padding: 4px var(--step-2);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--bg-sunken);
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.range__input:focus-visible { outline: 2px solid var(--select); outline-offset: 1px; }

.range__field { position: relative; display: flex; align-items: center; }

.range__input { padding-right: var(--step-5); }

.range__toggle {
  position: absolute;
  right: 2px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--ink-faint);
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
}

.range__toggle:hover { background: var(--rule); color: var(--ink); }

.range__list {
  position: absolute;
  top: calc(100% + 5px);
  left: 0;
  z-index: 40;
  min-width: 210px;
  margin: 0;
  padding: var(--step-1);
  list-style: none;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: var(--bg-raised);
  box-shadow: 0 10px 28px -14px rgb(0 0 0 / 0.5);
  max-height: 52vh;
  overflow-y: auto;
}

/* The end field's list hangs from its right edge. Anchored left it ran off
   a 375px screen - the field starts two thirds of the way across the row, and
   210px of list does not fit in what is left - so the labels that explain the
   dates ("Cambrian explosion", "Iron Age") were the part cut off. */
.range__field:last-of-type .range__list { left: auto; right: 0; }

.range__option {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--step-3);
  padding: 4px var(--step-2);
  border-radius: 4px;
  cursor: pointer;
  text-align: left;
}

.range__option[data-active="true"],
.range__option:hover { background: var(--bg-sunken); }

.range__option-value {
  color: var(--ink);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.range__option-label { color: var(--ink-faint); font-size: 11px; white-space: nowrap; }

.range__dash { color: var(--ink-faint); }

/* Shown only where the date row would otherwise cost its own strip. */
.range__summary {
  display: none;
  flex: 0 0 auto;
  align-items: center;
  gap: var(--step-1);
  min-height: var(--control-h);
  padding: 5px var(--step-3);
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--bg-sunken);
  color: var(--ink-soft);
  font: inherit;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  cursor: pointer;
}

.range__summary::after { content: '\25be'; color: var(--ink-faint); }
.range__summary[aria-expanded="true"] { border-color: var(--select); color: var(--select); }
.range__summary:focus-visible { outline: 2px solid var(--select); outline-offset: 1px; }

.range__go {
  min-height: var(--control-h);
  padding: 4px var(--step-2);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink-soft);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.range__go:hover { border-color: var(--rule-strong); color: var(--ink); }

/* ------------------------------------------------------------------ eras */

.eras {
  display: flex;
  gap: var(--step-1);
  min-width: 0;
  flex: 0 1 auto;
  overflow-x: auto;
  scrollbar-width: none;
}

.eras::-webkit-scrollbar { display: none; }

.eras button {
  min-height: var(--control-h);
  padding: 3px var(--step-2);
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: transparent;
  color: var(--ink-soft);
  font: inherit;
  font-size: 12px;
  white-space: nowrap;
  cursor: pointer;
}

.eras__rule { display: none; }

.eras button:hover { border-color: var(--rule-strong); color: var(--ink); }
.eras button[aria-current="true"] { border-color: var(--select); color: var(--select); }
.eras button:focus-visible { outline: 2px solid var(--select); outline-offset: 1px; }

/* ----------------------------------------------------------------- stage */

.stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
}

.minimap {
  border-top: 1px solid var(--rule);
  background: var(--bg-sunken);
  /* Clears the iOS home indicator and Android gesture bar. Needs
     viewport-fit=cover on the viewport meta, which index.html sets. */
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/*
 * A year is its own screen, and the top bar belongs to the timeline.
 *
 * Search, the date range and the era chips all move a window that the year
 * view is covering - clicking an era chip while a year is open changes
 * nothing you can see and does not even update the address. The minimap has
 * no window to draw. The year view carries its own header, with the year, the
 * steppers, "View on timeline" and a close button, so none of this is a way
 * out that is being taken away.
 *
 * Hiding .header covers the search, the range and the chips in one go: they
 * are its children, and absolute positioning does not escape display:none.
 */
body[data-year-view="true"] .header,
body[data-year-view="true"] .minimap { display: none; }

/* A display:none child stops being a grid item, but the row it sat in is
   still declared, so without this the header leaves a 52px gap behind. */
body[data-year-view="true"] { grid-template-rows: 1fr; }

/* The padding reserves room for strips that are no longer there. */
body[data-year-view="true"] .stage { padding-top: 0; }

.minimap__canvas {
  display: block;
  width: 100%;
  height: var(--minimap-h);
  cursor: pointer;
  touch-action: none;
}

/* ----------------------------------------------------------------- error */

.error {
  margin: var(--step-6) auto;
  max-width: 34rem;
  padding: var(--step-4);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: var(--bg-raised);
}

.error h2 { margin: 0 0 var(--step-2); font-size: 15px; }
.error code { color: var(--ink-soft); font-size: 13px; word-break: break-all; }

.error button {
  margin-top: var(--step-3);
  padding: var(--step-2) var(--step-3);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: var(--bg-sunken);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

/*
 * Selected text, in the site's own colours.
 *
 * The browser default is a blue block with black text, which on a dark field
 * reads as a rendering fault - most visible in the date inputs, which select
 * their contents on focus, so the first thing you see when you tap one is the
 * selection itself.
 */
::selection {
  /* A wash rather than a solid block. At full strength this turned a selected
     date into a bright slab - most of what you see on tapping the field, since
     it selects its own contents on focus. The text keeps its normal colour so
     it stays legible over the tint: 9.4:1 light, 6.2:1 dark, with the wash
     itself about 1.6:1 against the field so it is clearly there. */
  background: color-mix(in srgb, var(--select) 34%, transparent);
  color: var(--ink);
}

/* Prose is for reading, so it keeps normal selection. */
.panel__body,
.panel__title,
.yearview__body,
.yearview__year {
  -webkit-user-select: text;
  user-select: text;
}

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

/* ----------------------------------------------------------- responsive */

@media (max-width: 1280px) {
  /* Below this the header cannot hold the title and the seven era chips
     both. The title is the least useful thing in the bar - the tab already
     says what this is - so it goes first, which buys the chips another
     180px of inline life before they move to a strip of their own. */
  .header__title { display: none; }
}

@media (max-width: 1100px) {
  /* The era chips get their own scrollable strip rather than being hidden or
     folded into a menu. Jumping eras is the main way to move across 4.5
     billion years, so burying it would cost two taps and make the primary
     navigation undiscoverable. The row already scrolls horizontally.

     This was 980px, which was measured short. Seven chips need 498px, and
     what the header can spare beside a 238px search field and a 243px date
     row runs out at about 1063 - so between 980 and 1063 the row overflowed
     and sliced its last chip mid-word: "Mod" at 1024, "M" at 1000. It still
     scrolled, but scrollbar-width:none hides the only clue that it does, so
     it read as broken text rather than as a strip with more in it. 1100
     leaves headroom for a longer era name or a wider font. */
  .eras {
    position: absolute;
    top: var(--header-h);
    left: 0;
    right: 0;
    z-index: 15;
    height: var(--strip-h);
    align-items: center;
    padding: 0 var(--step-3);
    border-bottom: 1px solid var(--rule);
    background: var(--bg-raised);
    -webkit-overflow-scrolling: touch;
  }

  .stage { padding-top: var(--strip-h); }
}

@media (max-width: 760px) {
  .header { gap: var(--step-2); padding: 0 var(--step-3); }
  .header__title { font-size: 13px; }
  .range__input { width: 70px; }
  .range__go { display: none; }   /* Enter submits; the button is redundant */
}

@media (max-width: 560px) {
  /* Era chips become tabs. Seven pills will not sit on one line - at 12px the
     labels alone are 348px of a 375px screen, and the borders and pill padding
     push them well past it - so the chrome goes and the labels do the work.
     At 11px they total 323px and fit with room to spare. overflow-x stays as a
     fallback for a 320px screen. */
  .eras {
    /* Justified: the row spans the bar, and the leftover width is shared
       evenly around the labels and the dividers between them. */
    gap: 2px;
    justify-content: space-between;
    padding-inline: 6px;
  }

  .eras__rule {
    display: block;
    flex: 0 0 auto;
    align-self: center;
    width: 1px;
    height: 14px;
    background: var(--rule-strong);
  }

  /* No horizontal padding: seven labels are 323px of a 363px row, so padding
     would leave nothing for space-between to spread between them. The tap
     target is unaffected - it stays 44px tall. */
  .eras button {
    position: relative;
    /* Never shrink. Allowing it let the row squeeze seven labels into a width
       that could not hold them, so the text spilled past its own box and the
       dividers landed on top of it. Below about 370px the row scrolls
       instead, which is honest about not fitting. */
    flex: 0 0 auto;
    padding: 0;
    border: 0;
    border-radius: 0;
    border-bottom: 2px solid transparent;
    background: transparent;
    /* Sized to the screen rather than pinned at 11px, which is legible on a
       375px phone and too small on a 412px one. The ceiling is where seven
       labels, six dividers and their gaps stop fitting on one line: about
       12.6px at 412px wide, 11.3px at 375px. */
    font-size: clamp(11px, calc(3.4vw - 1.44px), 12.6px);
  }

  .eras button:hover { border-color: transparent; }

  .eras button[aria-current="true"] {
    border-bottom-color: var(--select);
    color: var(--select);
  }

  /* The slash hint is for a keyboard nobody has here, and it costs the search
     field the width of its own badge. */
  .search__key { display: none; }
  .search__input { padding-right: var(--step-3); }

  /* Three stacked strips - search, dates, eras - cost about 156px on a phone,
     a fifth of the screen, before any history is drawn. The date row is the
     one to fold: it is read far more often than it is set, and the reading is
     a dozen characters. A summary button carries the reading in the header,
     and the row itself appears over the era chips only while in use, so
     opening it shifts nothing below. */
  .header__title { display: none; }
  /* The search field had the lion's share of the bar while the date button -
     which is read far more often than search is typed - was squeezed. */
  .search { max-width: none; flex: 1 1 auto; min-width: 0; margin-left: 0; }

  .range__summary {
    display: inline-flex;
    flex: 0 0 auto;
    justify-content: center;
    min-width: 150px;
  }

  .range {
    display: none;
    position: absolute;
    top: var(--header-h);
    left: 0;
    right: 0;
    /* Above the era strip it covers, so the timeline never moves. */
    z-index: 25;
    height: var(--strip-h);
    justify-content: center;
    padding: 0 var(--step-3);
    border-bottom: 1px solid var(--rule);
    background: var(--bg-raised);
  }

  .range[data-open="true"] { display: flex; }

  .eras { top: var(--header-h); }

  .range__input { width: 96px; }
  /* An open card takes the minimap's row. The minimap answers "where in all
     of time is this window", which the card is not asking - and on a phone
     the card wants the height more. The timeline behind it keeps its own
     window, so nothing is lost but the strip. */
  body[data-panel="open"] .minimap { display: none; }

  .stage { padding-top: var(--strip-h); }
  .hint { display: none; }
}

/* ------------------------------------------------- touch input ergonomics */

/*
 * Keyed to pointer type rather than screen width, because the constraint is
 * the finger, not the viewport - a touchscreen laptop needs these too, and a
 * narrow desktop window does not.
 *
 * Apple asks for 44pt and Android for 48dp. Everything here was between 20 and
 * 32px, which looks correct in an emulated viewport and is annoying to hit on
 * an actual phone.
 */
@media (pointer: coarse) {
  :root {
    --strip-h: 52px;
    /* Taller on a phone: it is a drag target here, not just a picture, and
       the marks now grow to fill it. */
    --minimap-h: 74px;
    /* Raising the token covers every control that uses it. Listing them by
       name here is what left the date summary at 30px against a 44px search
       field, and below the target size this block exists to enforce. */
    --control-h: 44px;
  }

  /* Chips only. The phone layout turns these into tabs with no horizontal
     padding, and this rule used to override that from further down the file -
     the buttons were then shrunk below their own text width, which ran
     "Bronze Age" into "Classical".

     The floor is 600 rather than 561 because this padding is not free: 12px
     either side of seven chips is 56px more than the 8px the chips carry
     otherwise, and it took the row from 522px to 578px. Gated at 561 that
     landed one pixel above the tab layout, so a touch screen 561 to 577px
     wide overflowed and sliced "Modern" by 17px - the same fault as the
     desktop band, a breakpoint down. Below 600 the chips keep the ordinary
     padding; they are still 44px tall, which is the target size that
     matters. */
  @media (min-width: 600px) {
    .eras button { padding-inline: var(--step-3); }
  }

  .range__toggle {
    width: 44px;
    height: 44px;
    right: 0;
  }

  /* The 44px arrow eats into the text, so the field has to grow with it or
     "3600 BCE" truncates to "3600 B". */
  .range__input {
    padding-right: 40px;
    padding-left: var(--step-2);
    width: 132px;
  }

  /* Menu rows need the same treatment; a 26px row in a dropdown is worse than
     a 26px button, because the neighbours are one mistake away. */
  .range__option,
  .results__item { min-height: 44px; align-items: center; }

  .panel__close { width: 44px; height: 44px; font-size: 24px; }
  .panel__chip { min-height: 36px; display: inline-flex; align-items: center; }
}
