/* ---- design tokens (light) ---- */
:root {
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --hairline: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --hover-wash: rgba(11, 11, 11, 0.04);
  --sel-wash: rgba(42, 120, 214, 0.10);
  --match-wash: rgba(12, 163, 12, 0.09);
  /* AES endangerment scale, light mode (validated) */
  --aes-0: #b6b4ac;
  --aes-1: #0ca30c;
  --aes-2: #eda100;
  --aes-3: #eb6834;
  --aes-4: #d03b3b;
  --aes-5: #a02828;
  --aes-6: #898781;
  --shadow: 0 8px 30px rgba(11, 11, 11, 0.14);
}
:root[data-theme="dark"] {
  --page: #0d0d0d;
  --surface: #1a1a19;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --hairline: #2c2c2a;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --hover-wash: rgba(255, 255, 255, 0.05);
  --sel-wash: rgba(57, 135, 229, 0.16);
  --match-wash: rgba(25, 158, 112, 0.14);
  /* AES endangerment scale, dark mode (validated) */
  --aes-0: #55544f;
  --aes-1: #199e70;
  --aes-2: #c98500;
  --aes-3: #e06a2e;
  --aes-4: #e05252;
  --aes-5: #b23d3d;
  --aes-6: #898781;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  display: flex;
  flex-direction: column;
}

a { color: var(--accent); }

/* ---- header ---- */
.site-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px 10px;
}
h1 { margin: 0; font-size: 22px; letter-spacing: -0.01em; }
.subtitle { margin: 3px 0 0; color: var(--ink-2); font-size: 13px; max-width: 720px; }

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  border-radius: 8px;
  width: 32px; height: 32px;
  font-size: 15px;
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: var(--hover-wash); }

.header-controls { display: flex; gap: 8px; align-items: center; flex: none; }
.lang-select {
  height: 32px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
}

/* ---- toolbar ---- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 20px;
}
.toolbar input[type="search"] {
  flex: 2 1 240px;
  min-width: 200px;
}
.toolbar select { flex: 1 1 150px; min-width: 130px; }
.toolbar input, .toolbar select, .ghost-btn {
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
}
.toolbar input:focus, .toolbar select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.ghost-btn { cursor: pointer; color: var(--ink-2); flex: none; }
.ghost-btn:hover { background: var(--hover-wash); }

/* ---- status chips ---- */
.status-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 2px 20px 10px;
}
.status-row-label { color: var(--muted); font-size: 12.5px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-2);
  font-size: 12.5px;
  cursor: pointer;
  user-select: none;
}
.chip .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); flex: none; }
.chip:hover { background: var(--hover-wash); }
.chip.off { opacity: 0.38; }
.chip.off .dot { background: transparent; border: 1.5px solid var(--c); }
.result-count { margin-left: auto; color: var(--ink-2); font-size: 12.5px; white-space: nowrap; }

/* ---- main split ---- */
.content {
  flex: 1;
  display: flex;
  gap: 12px;
  padding: 0 20px 12px;
  min-height: 0;
}
.table-pane {
  flex: 11;
  min-width: 0;
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.map-pane {
  flex: 9;
  min-width: 0;
  position: relative;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface);
}
#map { position: absolute; inset: 0; background: var(--surface); }

/* ---- table ---- */
.table-scroll { flex: 1; overflow: auto; }
table { width: 100%; border-collapse: collapse; }
thead th {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--surface);
  box-shadow: inset 0 -1px 0 var(--hairline);
  text-align: start;
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  padding: 9px 12px;
  white-space: nowrap;
  cursor: pointer;
}
thead th.num { text-align: end; }
thead th:hover { color: var(--ink-2); }
.sort-arrow { display: inline-block; width: 1em; color: var(--accent); }
tbody td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--hairline);
  vertical-align: middle;
}
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--hover-wash); }
tbody tr.selected { background: var(--sel-wash); }
.lang-name { font-weight: 600; }
.lang-name-en { color: var(--muted); font-size: 12px; font-weight: 400; }
td.fam, td.ma { color: var(--ink-2); }
td.num {
  text-align: end;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
td .na { color: var(--muted); }
.status-cell { display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; color: var(--ink-2); font-size: 12.5px; }
.status-cell .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); flex: none; }
#table-sentinel { height: 8px; }

/* ---- map legend ---- */
.legend {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 800;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 11px;
  font-size: 11.5px;
  color: var(--ink-2);
  display: grid;
  gap: 3px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.12);
}
.legend .row { display: flex; align-items: center; gap: 7px; }
.legend .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); flex: none; }

/* ---- detail drawer ---- */
.detail {
  position: fixed;
  top: 0; inset-inline-end: 0; bottom: 0;
  width: min(430px, 92vw);
  background: var(--surface);
  border-inline-start: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 2000;
  overflow-y: auto;
  padding: 20px 22px 30px;
}
.detail-close { position: absolute; top: 14px; inset-inline-end: 14px; }
.detail h2 { margin: 4px 0 0; font-size: 21px; padding-inline-end: 40px; }
.detail .en-name { color: var(--ink-2); margin: 2px 0 0; font-size: 14px; }
.detail .code-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.code-chip {
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--hover-wash);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 8px;
  color: var(--ink-2);
}
.detail .status-line {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0 14px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 13px;
}
.detail .status-line .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); }
.detail dl { margin: 0 0 18px; display: grid; grid-template-columns: 130px 1fr; gap: 7px 12px; font-size: 13.5px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; color: var(--ink); }
.detail h3 {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 18px 0 8px;
}
.links { display: grid; gap: 6px; margin: 0; padding: 0; list-style: none; }
.links a {
  display: block;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  text-decoration: none;
  color: var(--ink);
}
.links a:hover { background: var(--hover-wash); }
.links .link-note { display: block; color: var(--muted); font-size: 12px; margin-top: 1px; }
.detail .foot-note { color: var(--muted); font-size: 12px; margin-top: 16px; }

/* ---- dialect chips & compare button ---- */
.status-row-line { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 4px 0 14px; }
.status-row-line .status-line { margin: 0; }
.compare-btn { height: 30px; border-radius: 999px; font-size: 12.5px; }
.dial-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 170px;
  overflow-y: auto;
  margin-bottom: 4px;
}
.dial-chips a {
  font-size: 12.5px;
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--ink-2);
  text-decoration: none;
}
.dial-chips a:hover { background: var(--hover-wash); color: var(--ink); }

/* ---- modal overlays (compare, stats) ---- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 3000;
  background: rgba(0, 0, 0, 0.45);
  overflow-y: auto;
  padding: 4vh 16px;
}
.panel {
  position: relative;
  max-width: 860px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 22px 24px 26px;
}
.panel-narrow { max-width: 560px; }
.panel h2 { margin: 0 40px 12px 0; font-size: 19px; }
[dir="rtl"] .panel h2 { margin: 0 0 12px 40px; }
.panel-close { position: absolute; top: 14px; right: 14px; }
[dir="rtl"] .panel-close { right: auto; left: 14px; }
.compare-pick { position: relative; margin-bottom: 14px; }
.compare-pick input {
  width: 100%;
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--page);
  color: var(--ink);
  font: inherit;
}
.cmp-results {
  position: absolute;
  top: 40px; left: 0; right: 0;
  z-index: 10;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  max-height: 300px;
  overflow-y: auto;
}
.cmp-results button {
  display: block;
  width: 100%;
  text-align: start;
  padding: 8px 12px;
  border: 0;
  background: none;
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}
.cmp-results button:hover { background: var(--hover-wash); }
.cmp-results button span { color: var(--muted); font-size: 12px; }
.cmp-heads {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  font-weight: 700;
  font-size: 16px;
  margin: 4px 0 6px;
}
.panel h3 {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 18px 0 8px;
}
/* a wide table scrolls inside itself instead of stretching the panel */
.cmp-scroll { overflow-x: auto; margin-bottom: 4px; }
.cmp-table { width: 100%; min-width: 420px; border-collapse: collapse; font-size: 13.5px; }
.cmp-table td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--hairline);
  vertical-align: top;
}
.cmp-table td:first-child { color: var(--muted); width: 38%; }
.cmp-table td:not(:first-child) { width: 31%; }
.cmp-table tr.match td:not(:first-child) { background: var(--match-wash); }
.cmp-summary { color: var(--ink-2); font-size: 13px; margin: 4px 0 8px; }
.cmp-note { color: var(--muted); font-size: 12px; margin: 8px 0 0; line-height: 1.5; }

/* ---- stats panel ---- */
.stat-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 6px 0 4px; }
.stat-card {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--page);
}
.stat-num { font-size: 23px; font-weight: 700; letter-spacing: -0.01em; }
.stat-cap { color: var(--muted); font-size: 12px; }
.stat-bars { display: grid; gap: 5px; }
.stat-bar { display: grid; grid-template-columns: minmax(90px, 34%) 1fr auto; align-items: center; gap: 10px; font-size: 13px; }
.stat-bar-label { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-bar-track { background: var(--hover-wash); border-radius: 999px; height: 9px; overflow: hidden; }
.stat-bar-fill { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.stat-bar-value { color: var(--ink-2); font-variant-numeric: tabular-nums; font-size: 12.5px; }
.spark { display: flex; align-items: flex-end; gap: 2px; height: 56px; padding: 4px 0; }
.spark-bar {
  flex: 1 1 0;
  max-width: 22px;   /* so that one or two days do not stretch across the full width */
  background: var(--accent);
  border-radius: 2px 2px 0 0;
  opacity: 0.85;
  min-width: 3px;
}
.spark-bar:hover { opacity: 1; }

/* ---- empty search state ---- */
.empty-state { padding: 26px 22px; }
.empty-state h3 { margin: 0 0 6px; font-size: 15px; }
.empty-state p { margin: 0 0 10px; color: var(--ink-2); font-size: 13.5px; max-width: 46em; }
.empty-links { display: flex; flex-wrap: wrap; gap: 8px; }
.empty-links a {
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  text-decoration: none;
  font-size: 13px;
}
.empty-links a:hover { background: var(--hover-wash); }
.alt-names { color: var(--ink-2); font-size: 12.5px; margin: 0 0 4px; line-height: 1.6; }

/* ---- language connections ---- */
.rel-box:empty { display: none; }
.rel-line {
  display: grid;
  grid-template-columns: 150px 1fr;
  gap: 4px 10px;
  align-items: baseline;
  margin-bottom: 7px;
  font-size: 13px;
}
.rel-cap { color: var(--muted); font-size: 12.5px; }
.rel-branch { color: var(--ink-2); line-height: 1.5; }
.rel-links { display: flex; flex-wrap: wrap; gap: 4px 9px; }
.rel-links a { color: var(--ink); text-decoration: none; white-space: nowrap; }
.rel-links a:hover { color: var(--accent); text-decoration: underline; }
.rel-pct { color: var(--muted); font-size: 11.5px; margin-inline-start: 3px; }
@media (max-width: 560px) {
  .rel-line { grid-template-columns: 1fr; }
}

/* ---- geo hint in the empty state ---- */
.geo-hint { color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.geo-place {
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 11px 13px;
  margin-bottom: 8px;
  background: var(--page);
  max-width: 46em;
}
.geo-place-name { color: var(--ink-2); font-size: 13px; margin-bottom: 9px; }
.geo-primary {
  display: block;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  text-decoration: none;
  margin-bottom: 9px;
}
.geo-primary:hover { background: var(--hover-wash); border-color: var(--accent); }
.geo-primary-cap { display: block; color: var(--muted); font-size: 12px; margin-bottom: 2px; }
.geo-primary-name { display: block; color: var(--ink); font-size: 17px; font-weight: 700; }
.geo-arrow { color: var(--accent); font-weight: 400; }
.geo-primary-sub { display: block; color: var(--ink-2); font-size: 12.5px; margin-top: 3px; }
.geo-langs { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 9px; }
.geo-cap { color: var(--muted); font-size: 12.5px; }
.geo-langs a { font-size: 12.5px; text-decoration: none; color: var(--ink-2); }
.geo-langs a:hover { text-decoration: underline; color: var(--accent); }

/* ---- footer ---- */
.site-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 24px;
  justify-content: space-between;
  padding: 8px 20px 14px;
  color: var(--muted);
  font-size: 12px;
}
.site-footer a { color: var(--ink-2); }
.site-footer .donations { white-space: nowrap; }

/* ---- leaflet theme fixes ---- */
.leaflet-container { font: inherit; }
.leaflet-control-attribution {
  background: color-mix(in srgb, var(--surface) 85%, transparent) !important;
  color: var(--muted) !important;
  font-size: 10px !important;
}
.leaflet-control-attribution a { color: var(--ink-2) !important; }
.leaflet-control-zoom a {
  background: var(--surface) !important;
  color: var(--ink) !important;
  border-color: var(--border) !important;
}
.leaflet-tooltip.lang-tip {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px rgba(0,0,0,0.18);
  border-radius: 8px;
  padding: 5px 9px;
  font-size: 12.5px;
}
.leaflet-tooltip.lang-tip::before { display: none; }
.lang-tip .tip-sub { color: var(--ink-2); font-size: 11.5px; }

/* ---- responsive ---- */
/* ---- tablet: map and table stacked ---- */
@media (max-width: 900px) {
  html, body { height: auto; }
  body { min-height: 100vh; }
  .content { flex-direction: column; }
  .map-pane { order: -1; min-height: 44vh; flex: none; }
  .table-pane { min-height: 50vh; max-height: 72vh; }
  .hide-narrow { display: none; }
  .result-count { margin-left: 0; width: 100%; }
}

/* ---- phone ---- */
@media (max-width: 700px) {
  /* the header doesn't fit on one line: the title and buttons split across
     rows, otherwise the control panel runs off the edge of the screen */
  .site-header {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 14px 14px 8px;
  }
  h1 { font-size: 19px; }
  .subtitle {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-size: 12.5px;
  }
  .header-controls { flex-wrap: wrap; }
  .header-controls .ghost-btn { flex: 1 1 auto; }
  .lang-select { flex: 1 1 100px; min-width: 0; }

  .toolbar, .status-row, .content { padding-inline: 14px; }
  .toolbar { gap: 6px; }
  .toolbar input[type="search"] { flex-basis: 100%; }
  .toolbar select { flex: 1 1 calc(50% - 3px); min-width: 0; }
  .status-row-label { display: none; }

  /* map on top, as on tablet */
  .map-pane { min-height: 40vh; }
  .table-pane { max-height: none; }
  /* the legend covered half the map, and the same colours with labels
     sit in the status row right above it */
  .legend { display: none; }

  .detail { width: 100%; padding: 18px 16px 26px; }
  .detail dl { grid-template-columns: 110px 1fr; }
  .dial-chips { max-height: 40vh; }
  .overlay { padding: 0; }
  .panel { border-radius: 0; min-height: 100vh; padding: 18px 16px 24px; }
  .stat-cards { gap: 6px; }
  .stat-card { padding: 10px; }
  .stat-num { font-size: 19px; }
  .stat-bar { grid-template-columns: minmax(70px, 40%) 1fr auto; font-size: 12.5px; }
}

/* ---- narrow phones ---- */
@media (max-width: 560px) {
  /* the family does not fit next to speakers and status — it is on the card */
  td.fam, th[data-sort="fam"] { display: none; }
  tbody td, thead th { padding: 8px 8px; }
  .cmp-heads { font-size: 14px; }
  /* the comparison fits whole: scrolling a table sideways on a phone is awkward */
  .cmp-table { min-width: 0; font-size: 12px; }
  .cmp-table td { padding: 5px 6px; }
  .cmp-table td:first-child { width: 34%; }
  .cmp-table td:not(:first-child) { width: 33%; }
}
.rel-clade { color: var(--muted); font-weight: 400; }
.fb-text { color: var(--ink-2); font-size: 13.5px; margin: 0 0 14px; line-height: 1.5; }
