:root {
  --bg: #f6f7f5;
  --panel: #ffffff;
  --panel-alt: #f0f4f1;
  --text: #17201b;
  --muted: #65706a;
  --line: #d8ded9;
  --line-strong: #b9c4bd;
  --accent: #0f7b5c;
  --accent-dark: #095a43;
  --accent-soft: #dceee7;
  --blue: #2866a6;
  --amber: #a45d0f;
  --danger: #a83f3f;
  --radius: 8px;
  --topbar-height: 72px;
  --z-canvas: 10;
  --z-legend: 20;
  --z-sticky: 30;
  --z-backdrop: 40;
  --z-drawer: 50;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { min-height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); }
button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .58; }
a { color: inherit; text-decoration: none; }
h1, h2, h3, p { text-wrap: pretty; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.app-shell { min-height: 100vh; }

.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  display: flex;
  min-height: var(--topbar-height);
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(246, 247, 245, .94);
  backdrop-filter: blur(18px);
}

.brand, .topbar-actions, .playback, .map-summary { display: flex; align-items: center; }
.brand { min-width: 0; gap: 12px; }
.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid #b9d7cb;
  border-radius: var(--radius);
  background: #e8f4ee;
  color: var(--accent-dark);
}
.brand-mark svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.brand h1, .panel-head h2, .map-toolbar h2, .timeline-head h2 { margin: 0; letter-spacing: 0; }
.brand h1 { font-size: 18px; line-height: 1.1; }
.eyebrow { margin: 0 0 4px; color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; }
.topbar-actions { gap: 8px; }

.status-pill {
  display: inline-flex;
  min-height: 24px;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.status-pill.is-ready { border-color: #afd9c9; background: var(--accent-soft); color: var(--accent-dark); }
.status-pill.is-error { border-color: #e1aaaa; background: #fff0f0; color: var(--danger); }

.button, .icon-button {
  display: inline-flex;
  min-height: 38px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  font-weight: 700;
}
.button { padding: 0 13px; }
.button-compact { min-height: 32px; padding: 0 10px; font-size: 12px; }
.icon-button { width: 38px; padding: 0; font-size: 24px; line-height: 1; }
.button-primary { border-color: var(--accent); background: var(--accent); color: #fff; }
.button-primary:hover, .button-primary:focus-visible { border-color: var(--accent-dark); background: var(--accent-dark); }
.button-secondary:hover, .button-secondary:focus-visible, .icon-button:hover, .icon-button:focus-visible { border-color: #9eb0a6; background: #eef3f0; }
.button:focus-visible, .icon-button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid rgba(15, 123, 92, .2); outline-offset: 1px; }
.loop-button[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-dark); }
.mobile-filter-button, .close-filters { display: none; }

.layout {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  gap: 14px;
  height: calc(100vh - var(--topbar-height));
  padding: 14px;
}

.filters-panel, .map-panel, .timeline-panel {
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: 0 1px 0 rgba(27, 40, 34, .04);
}
.filters-panel { display: flex; flex-direction: column; overflow: hidden; }
.panel-head, .map-toolbar, .timeline-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.panel-head h2, .map-toolbar h2, .timeline-head h2 { font-size: 15px; }

.filters-form {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
  padding: 14px;
}
.filter-group { margin: 0; border-bottom: 1px solid var(--line); padding: 0 0 8px; }
.filter-group summary { color: var(--muted); cursor: pointer; font-size: 12px; font-weight: 800; }
.filter-group summary:focus-visible { border-radius: 4px; outline: 3px solid rgba(15, 123, 92, .2); outline-offset: 2px; }
.filter-content { display: grid; gap: 9px; padding-top: 8px; }
.filter-help { margin: 0; color: var(--muted); font-size: 11px; line-height: 1.35; }
.check-row { display: flex; min-height: 30px; align-items: center; gap: 8px; color: var(--text); font-size: 12px; font-weight: 700; }
.check-row input { width: 15px; height: 15px; flex: 0 0 auto; accent-color: var(--accent); }
.type-dot { display: inline-block; width: 9px; height: 9px; flex: 0 0 auto; border-radius: 50%; }
.type-hab { background: var(--accent); }
.type-pes { background: var(--blue); }
.type-ges { background: var(--amber); }
.size-filter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 8px; }
.size-filter-grid .check-row { min-height: 26px; }
.field { display: grid; gap: 6px; }
.field > span { color: var(--muted); font-size: 12px; font-weight: 800; }
.field input, .field select { width: 100%; min-height: 38px; border: 1px solid var(--line-strong); border-radius: var(--radius); background: #fff; color: var(--text); padding: 7px 9px; }
.field select:disabled { background: var(--panel-alt); color: var(--muted); }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.form-message { margin: 0; border-radius: var(--radius); background: #fff0f0; color: var(--danger); padding: 8px 10px; font-size: 12px; font-weight: 700; }
.filter-actions { position: sticky; bottom: -14px; display: flex; justify-content: flex-end; gap: 8px; margin: auto -14px -14px; padding: 12px 14px; border-top: 1px solid var(--line); background: rgba(255, 255, 255, .96); }

.checkbox-tree {
  display: grid;
  max-height: 270px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfb;
  padding: 5px;
}
.geo-group { border-bottom: 1px solid var(--line); padding: 2px 0; }
.geo-group:last-child { border-bottom: 0; }
.geo-head { display: flex; min-width: 0; align-items: center; gap: 4px; }
.geo-toggle {
  display: inline-flex;
  width: 23px;
  height: 23px;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--muted);
  padding: 0;
  font-size: 15px;
  font-weight: 800;
  line-height: 1;
}
.geo-head .check-row { min-width: 0; flex: 1; min-height: 27px; padding: 1px 3px; }
.geo-head .check-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.geo-children { display: grid; padding-left: 22px; }
.geo-children > .geo-group { border-bottom: 0; }
.geo-parish { color: var(--muted); min-height: 25px; padding-left: 27px; font-size: 11px; }

.content { display: grid; min-width: 0; min-height: 0; grid-template-rows: minmax(320px, 1fr) auto; gap: 14px; }
.map-panel { display: grid; grid-template-rows: auto minmax(0, 1fr); overflow: hidden; }
.map-summary { gap: 7px; color: var(--muted); font-size: 12px; font-weight: 700; }
.map-summary strong { color: var(--accent-dark); font-size: 17px; }
.map-actions { display: flex; align-items: center; gap: 10px; }
.map-wrap { position: relative; min-height: 300px; }
#certificate-map { position: absolute; inset: 0; z-index: 0; }
.leaflet-container { background: #dbe4dd; font: inherit; }
.leaflet-control-zoom { border: 1px solid var(--line-strong) !important; box-shadow: 0 4px 8px rgba(27, 40, 34, .14) !important; }
.certificate-canvas { position: absolute; z-index: var(--z-canvas); pointer-events: none; }

.map-loading, .map-error {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: var(--z-legend);
  display: flex;
  min-width: 230px;
  max-width: calc(100% - 32px);
  align-items: center;
  gap: 10px;
  transform: translate(-50%, -50%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, .96);
  padding: 12px 14px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}
.map-error { color: var(--danger); }
.skeleton { display: block; background: var(--line); animation: skeleton 1.2s ease-in-out infinite alternate; }
.skeleton-line { width: 42px; height: 8px; border-radius: 999px; }
@keyframes skeleton { to { opacity: .4; } }

.legend {
  position: absolute;
  left: 10px;
  bottom: 24px;
  z-index: var(--z-legend);
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, .94);
  padding: 7px 9px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}
.legend div { display: flex; align-items: center; gap: 5px; }

.source-control {
  position: absolute;
  right: 10px;
  bottom: 24px;
  z-index: var(--z-legend);
  display: flex;
  align-items: center;
  gap: 5px;
}
.source-control > a {
  border-radius: 5px;
  background: rgba(255, 255, 255, .94);
  padding: 3px 5px;
  color: #38443d;
  font-size: 10px;
  font-weight: 700;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}
.map-info-button {
  display: inline-grid;
  width: 25px;
  height: 25px;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: rgba(255, 255, 255, .96);
  color: var(--text);
  padding: 0;
  font-family: Georgia, serif;
  font-size: 15px;
  font-weight: 700;
}
.map-info-button:hover, .map-info-button:focus-visible, .map-info-button[aria-expanded="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-dark); }
.source-popover {
  position: absolute;
  right: 0;
  bottom: 34px;
  width: min(330px, calc(100vw - 42px));
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: #fff;
  padding: 12px;
  color: var(--text);
  box-shadow: 0 4px 8px rgba(27, 40, 34, .16);
}
.source-popover h3 { margin: 0 0 7px; font-size: 13px; }
.source-popover ul { display: grid; gap: 5px; margin: 0; padding-left: 18px; font-size: 11px; line-height: 1.4; }
.source-popover a { color: var(--accent-dark); text-decoration: underline; text-underline-offset: 2px; }

.timeline-panel { display: grid; overflow: hidden; }
.timeline-head { align-items: end; }
.timeline-stats { display: flex; gap: 22px; margin: 0; }
.timeline-stats div { display: grid; gap: 2px; text-align: right; }
.timeline-stats dt { color: var(--muted); font-size: 11px; font-weight: 700; }
.timeline-stats dd { margin: 0; font-size: 15px; font-weight: 800; }
.playback { gap: 12px; padding: 12px; }
.play-button { min-width: 122px; }
.progress-field { display: grid; min-width: 0; flex: 1; gap: 4px; }
.progress-field input { width: 100%; accent-color: var(--accent); }
.progress-dates { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; font-weight: 700; }
.sheet-backdrop { display: none; }

.leaflet-popup-content { margin: 11px 13px; font-size: 12px; line-height: 1.45; }
.popup-title { margin: 0 0 6px; font-size: 13px; }
.popup-list { display: grid; grid-template-columns: auto 1fr; gap: 3px 9px; margin: 0; }
.popup-list dt { color: var(--muted); font-weight: 700; }
.popup-list dd { margin: 0; font-weight: 700; }
.popup-navigation { display: grid; grid-template-columns: 1fr 30px 30px; align-items: center; gap: 5px; min-height: 30px; margin-top: 9px; }
.popup-position { color: var(--muted); font-size: 11px; font-weight: 700; }
.popup-nav-slot { width: 30px; height: 30px; }
.popup-nav-button {
  display: inline-grid;
  width: 30px;
  height: 30px;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: #edf0ee;
  color: #59635d;
  padding: 0;
}
.popup-nav-button svg { display: block; width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.popup-nav-button:hover, .popup-nav-button:focus-visible { background: #dde2df; color: #303a34; }
.popup-nav-button:focus-visible { outline: 3px solid rgba(15, 123, 92, .2); outline-offset: 1px; }

@media (max-width: 920px) {
  .layout { grid-template-columns: 280px minmax(0, 1fr); }
  .site-link { display: none; }
}

@media (max-width: 720px) {
  :root { --topbar-height: 66px; }
  .topbar { padding: 10px 12px; }
  .brand-mark { width: 38px; height: 38px; }
  .brand h1 { font-size: 16px; }
  .brand .eyebrow { max-width: 190px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .status-pill { display: none; }
  .mobile-filter-button, .close-filters { display: inline-flex; }
  .layout { display: block; height: calc(100vh - var(--topbar-height)); padding: 8px; }
  .content { height: 100%; grid-template-rows: minmax(300px, 1fr) auto; gap: 8px; }
  .filters-panel { position: fixed; inset: var(--topbar-height) auto 0 0; z-index: var(--z-drawer); width: min(340px, calc(100vw - 36px)); border-radius: 0 var(--radius) var(--radius) 0; transform: translateX(-102%); transition: transform 180ms cubic-bezier(.22, 1, .36, 1); }
  .filters-panel.is-open { transform: translateX(0); }
  .sheet-backdrop { position: fixed; inset: var(--topbar-height) 0 0; z-index: var(--z-backdrop); display: block; background: rgba(23, 32, 27, .38); }
  .map-toolbar, .timeline-head { padding: 9px 10px; }
  .map-summary span { display: none; }
  .playback { flex-wrap: wrap; gap: 8px; padding: 10px; }
  .play-button { min-width: 112px; }
  .progress-field { order: 3; flex-basis: 100%; }
  .loop-button { margin-left: auto; }
  .legend { left: 8px; right: auto; bottom: 22px; max-width: calc(100% - 125px); }
  .source-control { right: 8px; bottom: 22px; }
}

@media (max-width: 430px) {
  .brand-mark { display: none; }
  .field-grid { grid-template-columns: 1fr; }
  .timeline-stats { gap: 12px; }
  .timeline-stats div:first-child { display: none; }
  .map-summary { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
