/* Pole Map - dark tool chrome, chosen deliberately: the map is satellite
   imagery, and dark panels stop the UI competing with it. */

:root {
  color-scheme: dark;

  --surface-0: #121311;
  --surface-1: #1a1a19;
  --surface-2: #232322;
  --surface-3: #2d2d2b;
  --border: #3a3a37;
  --border-soft: #2e2e2c;

  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8d8c84;

  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, 0.16);
  --warn: #fab219;

  --sidebar-width: 320px;
  --radius: 8px;
  --radius-sm: 5px;

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

* { box-sizing: border-box; }

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

body {
  font: 14px/1.45 var(--font);
  color: var(--text-primary);
  background: var(--surface-0);
  overflow: hidden;
}

.app {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  /* An auto row would size to the sidebar content and push the map past the
     viewport; pin the row to the container height instead. */
  grid-template-rows: minmax(0, 1fr);
  height: 100%;
}

body.sidebar-hidden .app { grid-template-columns: 0 1fr; }
body.sidebar-hidden .sidebar { transform: translateX(-100%); border-right: 0; }

/* ------------------------------------------------------------- sidebar */

.sidebar {
  position: relative;
  z-index: 1100; /* above Leaflet control corners (1000) so they never float over the panel */
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  /* The collapsed column is 0px wide - clip the content instead of letting it
     spill across the map. */
  overflow: hidden;
  background: var(--surface-1);
  border-right: 1px solid var(--border);
  transition: transform 0.2s ease;
}

.sidebar-head {
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--border-soft);
}

.title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0 0 2px;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.title .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  flex: none;
  align-self: center;
}

.tally {
  font-size: 12.5px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.tally strong { color: var(--text-primary); font-weight: 600; }

.source {
  display: block;
  margin-top: 2px;
  font: 11px/1.4 var(--mono);
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.warn-line {
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--warn);
}

.sidebar-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.block {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-soft);
}

.block-title {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted);
}

/* ------------------------------------------------------------- controls */

.field { margin-bottom: 10px; }
.field:last-child { margin-bottom: 0; }

.field-label {
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  color: var(--text-secondary);
}

input[type="text"],
input[type="search"],
select {
  width: 100%;
  padding: 6px 8px;
  font: inherit;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

input[type="search"] { -webkit-appearance: none; appearance: none; }

input:focus-visible,
select:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.radio-row {
  /* Grid rather than flex so a fourth or fifth basemap wraps to a tidy second
     row instead of squeezing the labels. */
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(58px, 1fr));
  gap: 3px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
}

.radio-row label {
  text-align: center;
  font-size: 12px;
  padding: 5px 2px;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.radio-row input { position: absolute; opacity: 0; pointer-events: none; }
.radio-row input:checked + span {
  color: var(--text-primary);
}
.radio-row label:has(input:checked) {
  background: var(--surface-3);
  color: var(--text-primary);
  box-shadow: inset 0 0 0 1px var(--border);
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
}

.check input { accent-color: var(--accent); }

.field.is-disabled .check {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn {
  padding: 6px 11px;
  font: inherit;
  font-size: 12.5px;
  color: var(--text-primary);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.btn:hover { background: var(--surface-3); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

/* Small text, but a hit target worth aiming at. */
.link-btn {
  padding: 4px 6px;
  margin: -4px 0;
  font: inherit;
  font-size: 11.5px;
  color: var(--text-muted);
  background: none;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
}

.link-btn:hover { color: var(--accent); background: var(--surface-2); }

.sep { color: var(--border); font-size: 11px; }

.file-label { display: inline-block; }
.file-label input { display: none; }

.note {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

.basemap-note {
  margin-top: 6px;
  padding: 5px 7px;
  font-size: 11.5px;
  line-height: 1.35;
  color: #f4d79a;
  background: rgba(250, 178, 25, 0.1);
  border-left: 2px solid var(--warn);
  border-radius: 3px;
}

.basemap-note[hidden] { display: none; }

/* --------------------------------------------------------------- legend */

.legend { display: grid; gap: 3px; }

.legend-item {
  display: grid;
  grid-template-columns: 16px 1fr auto;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text-secondary);
}

.legend-item.is-dim { opacity: 0.4; }
.legend-item canvas { display: block; }
.legend-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-count { font-variant-numeric: tabular-nums; color: var(--text-muted); font-size: 11.5px; }

/* --------------------------------------------------------------- facets */

.facet { border-bottom: 1px solid var(--border-soft); }
.facet:last-child { border-bottom: 0; }

.facet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 16px;
}

.facet-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
  padding: 0;
  font: inherit;
  color: var(--text-primary);
  background: none;
  border: 0;
  cursor: pointer;
  text-align: left;
}

.facet-name {
  font-size: 12.5px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.facet-chev {
  font-size: 10px;
  color: var(--text-muted);
  transition: transform 0.15s ease;
}

.facet.collapsed .facet-chev { transform: rotate(-90deg); }
.facet.collapsed .facet-list { display: none; }

.facet-badge {
  flex: none;
  padding: 1px 6px;
  font: 11px/1.5 var(--mono);
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: 999px;
}

.facet-badge.is-filtered {
  color: #cfe1fb;
  background: var(--accent-soft);
}

.facet-actions { display: flex; align-items: center; gap: 1px; flex: none; }

.facet-list { padding: 0 16px 10px; }

.opt {
  display: grid;
  grid-template-columns: auto auto 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
}

.opt:hover { color: var(--text-primary); }
.opt input { accent-color: var(--accent); }
.opt-swatch { display: flex; width: 14px; height: 14px; }
.opt-swatch[hidden] { display: none; }
.opt-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.opt-count {
  font-variant-numeric: tabular-nums;
  font-size: 11.5px;
  color: var(--text-muted);
}

.opt.is-zero { opacity: 0.45; }

/* ------------------------------------------------------------------ map */

.map-wrap { position: relative; min-width: 0; min-height: 0; }
#map { position: absolute; inset: 0; background: #0d0f10; }

.sidebar-toggle {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 1100;
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  font-size: 15px;
  color: var(--text-primary);
  background: rgba(26, 26, 25, 0.92);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  backdrop-filter: blur(4px);
}

.sidebar-toggle:hover { background: var(--surface-3); }

.map-tooltip {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 650;
  display: grid;
  gap: 1px;
  max-width: 240px;
  padding: 5px 8px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--text-primary);
  background: rgba(18, 19, 17, 0.94);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  pointer-events: none;
  white-space: nowrap;
}

.map-tooltip span { color: var(--text-secondary); font-size: 11.5px; }
.map-tooltip[hidden] { display: none; }

.map-notice {
  position: absolute;
  left: 50%;
  top: 16px;
  z-index: 620;
  transform: translateX(-50%);
  max-width: min(520px, calc(100% - 32px));
  padding: 10px 12px;
  background: rgba(26, 26, 25, 0.96);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}

.map-notice[hidden] { display: none; }

.map-notice.error { border-color: #7a3b3b; }
.map-notice p { margin: 0; font-size: 13px; }
.map-notice .detail { margin-top: 4px; font-size: 12px; color: var(--text-secondary); }
.map-notice .close {
  position: absolute;
  top: 4px;
  right: 6px;
  padding: 2px 5px;
  font-size: 14px;
  color: var(--text-muted);
  background: none;
  border: 0;
  cursor: pointer;
}
.map-notice .close:hover { color: var(--text-primary); }

.drop-hint {
  position: absolute;
  inset: 0;
  z-index: 700;
  display: none;
  place-items: center;
  font-size: 15px;
  color: var(--text-primary);
  background: rgba(18, 19, 17, 0.72);
  border: 2px dashed var(--accent);
  pointer-events: none;
}

body.dragging .drop-hint { display: grid; }

/* --------------------------------------------------- leaflet overrides */

.leaflet-container {
  font: inherit;
  background: #0d0f10;
}

.leaflet-control-attribution {
  font-size: 10.5px;
  background: rgba(18, 19, 17, 0.78) !important;
  color: var(--text-muted);
}

.leaflet-control-attribution a { color: var(--text-secondary); }

.leaflet-bar,
.leaflet-control-scale-line {
  border-color: var(--border) !important;
}

.leaflet-bar a {
  background: rgba(26, 26, 25, 0.94);
  color: var(--text-primary);
  border-bottom-color: var(--border);
}

.leaflet-bar a:hover { background: var(--surface-3); color: var(--text-primary); }

.leaflet-control-scale-line {
  background: rgba(18, 19, 17, 0.78);
  color: var(--text-secondary);
}

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--surface-1);
  color: var(--text-primary);
  border: 1px solid var(--border);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
}

.leaflet-popup-content-wrapper { border-radius: var(--radius); }
.leaflet-popup-content { margin: 10px 12px; font-size: 13px; }
.leaflet-popup-close-button { color: var(--text-muted) !important; }

.pp-head { margin-bottom: 8px; }
.pp-head strong { font-size: 14px; }
.pp-sub { display: block; font-size: 12px; color: var(--text-secondary); }

.pp-body {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 12px;
  margin: 0;
  max-height: 240px;
  overflow-y: auto;
}

.pp-body dt { font-size: 11.5px; color: var(--text-muted); }
.pp-body dd { margin: 0; font-size: 12.5px; }

.pp-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 9px;
  padding-top: 7px;
  border-top: 1px solid var(--border-soft);
}

.pp-foot code { font: 11px var(--mono); color: var(--text-secondary); }
.pp-foot a { font-size: 12px; color: var(--accent); }

/* ---------------------------------------------------------- table drawer */

.btn.is-active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #dce9fb;
}

.table-drawer {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1050; /* over the map and its controls, under the side panel */
  display: flex;
  flex-direction: column;
  height: 42%;
  min-height: 180px;
  background: var(--surface-1);
  border-top: 1px solid var(--border);
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.45);
}

.table-drawer[hidden] { display: none; }

.table-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  flex: none;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-soft);
}

.btn-sm {
  padding: 3px 8px;
  font-size: 11.5px;
}

.dirty-badge {
  padding: 2px 7px;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  color: #f4d79a;
  background: rgba(250, 178, 25, 0.14);
  border-radius: 999px;
}

.dirty-badge[hidden] { display: none; }

.table-head h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.table-tally {
  font-size: 12.5px;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.table-hint {
  margin-left: auto;
  font-size: 11.5px;
  color: var(--text-muted);
}

.table-head .close {
  position: static;
  flex: none;
  padding: 2px 6px;
  font-size: 16px;
  line-height: 1;
  color: var(--text-muted);
  background: none;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
}

.table-head .close:hover { color: var(--text-primary); background: var(--surface-2); }

.table-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
}

.pole-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 12.5px;
  white-space: nowrap;
}

.pole-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 0;
  text-align: left;
  font-weight: 600;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.pole-table th.is-sorted { color: var(--text-primary); }

.th-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  width: 100%;
  padding: 7px 10px;
  font: inherit;
  font-weight: 600;
  color: var(--text-secondary);
  background: none;
  border: 0;
  cursor: pointer;
  text-align: left;
}

.th-btn:hover { color: var(--text-primary); background: var(--surface-3); }
.is-sorted .th-btn { color: var(--text-primary); }

.th-caret {
  width: 8px;
  font-size: 10px;
  color: var(--accent);
}

.pole-table tbody td {
  height: 30px; /* ROW_H in app.js depends on this */
  padding: 0 10px;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-soft);
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pole-table tbody tr:not(.spacer):not(.is-empty) { cursor: pointer; }
.pole-table tbody tr:not(.spacer):not(.is-empty):hover td { background: var(--surface-2); color: var(--text-primary); }

.pole-table tbody tr.is-selected td {
  background: var(--accent-soft);
  color: var(--text-primary);
}

.pole-table td.is-blank { color: var(--border); }

.pole-table tr.spacer td {
  height: auto;
  padding: 0;
  border: 0;
}

.pole-table tr.is-empty td {
  height: 60px;
  text-align: center;
  color: var(--text-muted);
}

.col-swatch {
  width: 30px;
  padding: 0 0 0 12px !important;
}

.col-swatch img { display: block; }

/* Keep the zoom and scale controls clear of the open drawer. */
body.table-open .leaflet-bottom { bottom: 42%; }

/* ------------------------------------------------------------ editing mode */

.pole-table td.is-editable { cursor: text; }

.pole-table td.is-editable:hover {
  box-shadow: inset 0 0 0 1px var(--border);
  color: var(--text-primary);
}

.pole-table td.is-editing-cell {
  padding: 0;
  box-shadow: inset 0 0 0 2px var(--accent);
}

.cell-input {
  width: 100%;
  height: 28px;
  padding: 0 8px;
  font: inherit;
  font-size: 12.5px;
  color: var(--text-primary);
  background: var(--surface-3);
  border: 0;
  border-radius: 0;
}

.cell-input:focus { outline: none; }

select.cell-input {
  height: 28px;
  padding: 0 4px;
  -webkit-appearance: none;
  appearance: none;
}

input[type="date"].cell-input::-webkit-calendar-picker-indicator,
input[type="month"].cell-input::-webkit-calendar-picker-indicator {
  filter: invert(0.8);
  cursor: pointer;
}

/* Checkbox panel for a multi-valued column, floated over its cell. */
/* Positioned from script against the cell, and parented to <body> so neither
   the table's scrolling nor the window edge can clip it. */
.float-editor {
  position: fixed;
  z-index: 1200;
  max-height: calc(100vh - 16px);
  overflow-y: auto;
  padding: 6px;
  background: var(--surface-2);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.5);
}

.multi-editor { min-width: 150px; }

/* Year and month side by side, free of the column's width. */
.month-editor {
  display: flex;
  gap: 6px;
}

.month-field {
  display: grid;
  gap: 3px;
}

.month-field > span {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.month-select {
  height: 26px;
  padding: 0 4px;
  font: inherit;
  font-size: 12.5px;
  color: var(--text-primary);
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 4px;
}

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

.multi-opt {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 3px 4px;
  font-size: 12.5px;
  color: var(--text-secondary);
  white-space: nowrap;
  cursor: pointer;
  border-radius: 3px;
}

.multi-opt:hover { background: var(--surface-3); color: var(--text-primary); }
.multi-opt input { accent-color: var(--accent); }

.multi-done {
  width: 100%;
  margin-top: 4px;
  padding: 3px 0;
  font: inherit;
  font-size: 11.5px;
  color: var(--text-primary);
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}

.multi-done:hover { background: var(--accent); border-color: var(--accent); }

/* The panel escapes the cell, so the cell must not clip it. */
.pole-table td.is-editing-cell { position: relative; overflow: visible; }

.cell-input.is-invalid {
  background: rgba(208, 59, 59, 0.28);
  box-shadow: inset 0 0 0 2px #d03b3b;
}

.col-swatch.is-locator { cursor: pointer; }
.col-swatch.is-locator:hover { background: var(--surface-3); }

.col-actions {
  width: 34px;
  padding: 0 8px 0 0 !important;
  text-align: right;
}

.row-del {
  padding: 1px 6px;
  font: inherit;
  font-size: 13px;
  line-height: 1.3;
  color: var(--text-muted);
  background: none;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
}

.row-del:hover {
  color: #f0b4b4;
  border-color: #7a3b3b;
  background: rgba(208, 59, 59, 0.16);
}

.row-del.is-confirming {
  font-size: 11px;
  color: #fff;
  background: #d03b3b;
  border-color: #d03b3b;
}

.map-notice.info {
  border-color: var(--accent);
  max-width: min(460px, calc(100% - 32px));
}

.map-notice.info { font-size: 12.5px; }

/* -------------------------------------------------------------- narrow */

@media (max-width: 720px) {
  :root { --sidebar-width: 84vw; }

  /* The panel is absolutely positioned here, so the map is the only grid item
     and must own the full width whether the panel is open or collapsed. */
  .app,
  body.sidebar-hidden .app { grid-template-columns: 1fr; }

  .sidebar {
    position: absolute;
    inset: 0 auto 0 0;
    width: var(--sidebar-width);
    box-shadow: 0 0 32px rgba(0, 0, 0, 0.5);
  }

  .sidebar-toggle { left: auto; right: 10px; }
  body.sidebar-hidden .sidebar { transform: translateX(-101%); }

  .table-drawer { height: 60%; }
  .table-hint { display: none; }
  body.table-open .leaflet-bottom { bottom: 60%; }
}
