* {
  box-sizing: border-box;
}

:root {
  --paper: #f6f1e7;
  --paper-raised: #fbf8f1;
  --ink: #2a2420;
  --ink-muted: #5b5045;
  --ink-faint: #8a7f70;
  --rule: #d8cdb8;
  --rule-soft: #e6dccb;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
}

body {
  margin: 0;
  font-family: var(--serif);
  color: var(--ink);
  background: var(--paper);
  height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  padding: 12px 24px 10px;
  background: var(--paper-raised);
  border-bottom: 1px solid var(--rule);
}

.masthead {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 0.005em;
}

.help {
  font-size: 0.85rem;
  color: var(--ink-muted);
}

.help summary {
  cursor: pointer;
  color: var(--ink-faint);
  font-style: italic;
  user-select: none;
}

.help summary:hover {
  color: var(--ink);
}

.help-body {
  margin-top: 8px;
  max-width: 70ch;
  line-height: 1.5;
}

.help-body p {
  margin: 0 0 8px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  font-size: 0.8rem;
  color: var(--ink-muted);
}

.family-legend {
  margin-top: 6px;
}

.dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
}

.legend .toggle {
  cursor: pointer;
  user-select: none;
}

.legend .toggle.off {
  opacity: 0.4;
  text-decoration: line-through;
}

.line {
  display: inline-block;
  width: 28px;
  height: 0;
  vertical-align: middle;
  margin-right: 4px;
  border-top: 3px solid;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

[hidden] {
  display: none !important;
}

.tabs {
  display: flex;
  gap: 4px;
  margin-left: auto;
  margin-right: 16px;
}

.tabs button {
  font: inherit;
  font-size: 0.85rem;
  padding: 4px 14px;
  color: var(--ink-muted);
  background: transparent;
  border: 1px solid var(--rule);
  border-radius: 4px;
  cursor: pointer;
}

.tabs button[aria-selected="true"] {
  color: var(--ink);
  background: var(--paper);
  border-color: var(--ink-faint);
  font-weight: 600;
}

#map-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #dfe8ea;
}

#map {
  position: absolute;
  inset: 0;
  touch-action: none;
}

/* The land underlay is drawn in map units and transformed to follow the graph's pan and zoom. */
#map-base {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  pointer-events: none;
}

.map-sphere {
  fill: #dfe8ea;
}

.map-graticule {
  fill: none;
  stroke: #c9d6d9;
  stroke-width: calc(0.5px * var(--inv-zoom, 1));
}

.map-land {
  fill: #f1ead9;
  stroke: #b9ad94;
  stroke-width: calc(0.7px * var(--inv-zoom, 1));
}

.map-borders {
  fill: none;
  stroke: #d3c8ae;
  stroke-width: calc(0.5px * var(--inv-zoom, 1));
}

#graph-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--paper-raised);
}

#graph {
  position: absolute;
  inset: 0;
  touch-action: none;
}

/* Lane and timeline shapes live in world coordinates and are transformed to match the graph's pan and zoom. */
#lanes {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  pointer-events: none;
}

/* Fixed-size labels that sit above the graph, so they stay readable at any zoom. */
#labels {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.axis-bar {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  background: var(--paper-raised);
  border-bottom: 1px solid var(--rule);
}

.lane {
  position: absolute;
  left: 0;
  background: #f2ecdf;
  border-bottom: 1px solid var(--rule-soft);
}

.lane-label {
  position: absolute;
  left: 0;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  font-style: italic;
  color: #3d342c;
  background: #ebe3d1;
  border-right: 1px solid var(--rule);
  overflow: hidden;
}

.gridline {
  position: absolute;
  top: 0;
  width: 1px;
  background: var(--rule-soft);
}

.tick {
  position: absolute;
  top: 10px;
  font-size: 11px;
  color: var(--ink-muted);
  font-variant-numeric: oldstyle-nums;
  white-space: nowrap;
}

#details {
  flex: 0 0 auto;
  height: clamp(170px, 34vh, 300px);
  padding: 16px 24px 18px;
  overflow-y: auto;
  background: var(--paper-raised);
  border-top: 1px solid var(--rule);
  display: grid;
  grid-template-columns: minmax(200px, 1fr) minmax(280px, 1.8fr) minmax(180px, 1fr) minmax(180px, 1fr);
  gap: 0 28px;
  align-content: start;
}

.placeholder {
  grid-column: 1 / -1;
  align-self: center;
  margin: 0;
  color: var(--ink-faint);
  font-style: italic;
  text-align: center;
}

#details:has(> .placeholder) {
  height: auto;
  padding: 12px 24px;
}

.details-head {
  grid-row: 1 / span 2;
}

#details h2 {
  margin: 0 0 4px;
  font-size: 1.35rem;
  font-weight: 600;
  line-height: 1.2;
}

.era {
  color: var(--ink-muted);
  font-size: 0.85rem;
  margin: 0 0 10px;
  font-style: italic;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.tag {
  font-size: 0.72rem;
  letter-spacing: 0.02em;
  background: #ebe3d1;
  color: #4a3f35;
  padding: 2px 9px;
  border-radius: 2px;
}

#details section {
  min-width: 0;
}

#details h3 {
  font-size: 0.75rem;
  font-weight: 600;
  margin: 0 0 6px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-faint);
}

#details p {
  line-height: 1.5;
  margin: 0;
  font-size: 0.95rem;
}

.group-label {
  margin: 0 0 4px !important;
  color: var(--ink-muted);
  font-size: 0.85rem;
  font-style: italic;
}

#details ul {
  padding-left: 18px;
  margin: 0;
}

#details li {
  margin-bottom: 4px;
  font-size: 0.95rem;
}

#details .years {
  color: var(--ink-faint);
  font-size: 0.85rem;
}

#details .related {
  padding-left: 0;
  list-style: none;
}

#details .related button {
  background: none;
  border: none;
  padding: 0;
  color: #4a5d7a;
  cursor: pointer;
  font: inherit;
  text-align: left;
  text-decoration: underline;
  text-decoration-color: var(--rule);
  text-underline-offset: 3px;
}

#details .related button:hover {
  text-decoration-color: currentColor;
}

@media (max-width: 760px) {
  body {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  header {
    flex: 0 0 auto;
    padding: 10px 14px 8px;
  }

  header h1 {
    font-size: 1.2rem;
    line-height: 1.2;
  }

  .masthead {
    gap: 8px;
  }

  .help-body {
    position: absolute;
    z-index: 20;
    left: 10px;
    right: 10px;
    padding: 12px;
    background: var(--paper-raised);
    border: 1px solid var(--rule);
    box-shadow: 0 4px 12px rgb(42 36 32 / 12%);
  }

  .legend {
    flex-wrap: nowrap;
    gap: 14px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
  }

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

  .family-legend {
    flex-wrap: wrap;
    overflow: visible;
    margin-top: 8px;
    padding-bottom: 2px;
    white-space: normal;
  }

  .line-legend {
    margin-top: 5px;
  }

  main {
    flex: 1 1 auto;
    height: auto;
    min-height: 0;
  }

  #details {
    height: auto;
    max-height: 42vh;
    max-height: 42dvh;
    padding: 14px 16px;
    grid-template-columns: 1fr;
    gap: 12px;
  }

  #details:has(> .placeholder) {
    padding: 10px 16px;
  }

  .details-head {
    grid-row: auto;
  }

  #details h2 {
    font-size: 1.2rem;
  }

  #details p,
  #details li {
    font-size: 0.9rem;
  }
}
