:root {
  --bg: #10171b;
  --panel-bg: rgba(16, 23, 27, 0.92);
  --panel-border: rgba(255, 255, 255, 0.12);
  --text: #f3f5f4;
  --text-dim: #b8c4c6;
  --accent: #2e7d32;
  --radius: 12px;
  --touch-min: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

/* Must win over any later `display:` rule on the same element (equal
   specificity, author origin) so toggling the `hidden` attribute in JS
   reliably hides backdrops/panels. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

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

#map {
  position: fixed;
  inset: 0;
  background: #0b1013;
}

button, .btn, input, select, textarea {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 3px solid #ffffff;
  outline-offset: 2px;
}

/* ---------- Status pill ---------- */
.status-pill {
  position: fixed;
  top: calc(12px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1200;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  color: var(--text-dim);
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 13px;
  backdrop-filter: blur(6px);
  pointer-events: none;
  transition: opacity 0.3s;
}
.status-pill.state-saving { color: #f9a825; }
.status-pill.state-offline { color: #d32f2f; }
.status-pill.state-saved { color: #66bb6a; }
.status-pill:empty { display: none; }

/* ---------- Sidebar toggle button ---------- */
.sidebar-toggle {
  position: fixed;
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
  z-index: 1100;
  width: var(--touch-min);
  height: var(--touch-min);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  background: var(--panel-bg);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(6px);
  cursor: pointer;
}

.icon-btn {
  border: none;
  background: transparent;
  color: var(--text);
  width: var(--touch-min);
  height: var(--touch-min);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  border-radius: var(--radius);
}
.icon-btn:hover { background: rgba(255,255,255,0.08); }

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 1300;
}

/* ---------- Sidebar (desktop) ---------- */
.sidebar {
  position: fixed;
  z-index: 1400;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  color: var(--text);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s ease;
}

@media (min-width: 801px) {
  .sidebar {
    top: calc(12px + var(--safe-top));
    left: calc(12px + var(--safe-left));
    bottom: calc(12px + var(--safe-bottom));
    width: 320px;
    border-radius: var(--radius);
  }
  .sidebar.collapsed { transform: translateX(calc(-100% - 24px)); }
  .sidebar-backdrop { display: none !important; }
  .sidebar-handle { display: none; }
  .sidebar-toggle { display: none; }
  #sidebar-close { display: none; }
}

/* ---------- Sidebar (mobile: bottom sheet) ---------- */
@media (max-width: 800px) {
  .sidebar-toggle { display: none; }
  .sidebar {
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 78vh;
    border-top-left-radius: var(--radius);
    border-top-right-radius: var(--radius);
    padding-bottom: var(--safe-bottom);
    transform: translateY(calc(100% - 64px));
  }
  .sidebar.expanded { transform: translateY(0); }
  .sidebar-handle {
    width: 40px;
    height: 5px;
    background: var(--panel-border);
    border-radius: 3px;
    margin: 10px auto 4px;
    flex: none;
  }
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 16px 8px;
  flex: none;
}

.sidebar-header h1 {
  font-size: 17px;
  font-weight: 700;
  margin: 0;
  line-height: 1.25;
}

.sidebar-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: none;
}

.lang-toggle {
  border: 1px solid var(--panel-border);
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: var(--touch-min);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}

.sidebar-scroll {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 16px 16px;
  flex: 1 1 auto;
}

.panel-section {
  padding: 12px 0;
  border-top: 1px solid var(--panel-border);
}
.panel-section:first-child { border-top: none; padding-top: 4px; }

.section-title {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  margin: 0 0 8px;
}

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.btn {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  min-height: var(--touch-min);
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
}
.btn:hover { background: rgba(255,255,255,0.1); }
.btn-icon { font-size: 16px; }

.btn-primary { background: var(--accent); border-color: var(--accent); }
.btn-primary:hover { background: #256a29; }
.btn-secondary { background: rgba(255,255,255,0.04); }

.search-wrap input {
  width: 100%;
  min-height: var(--touch-min);
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  padding: 8px 12px;
}
.search-wrap input::placeholder { color: var(--text-dim); }

.legend-list, .feature-list, .history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  padding: 4px 4px;
  border-radius: 8px;
  cursor: pointer;
  background: transparent;
  border: none;
  color: var(--text);
  width: 100%;
  text-align: left;
}
.legend-item:hover { background: rgba(255,255,255,0.06); }
.legend-item.disabled { opacity: 0.4; }

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex: none;
  border: 2px solid rgba(255,255,255,0.6);
}

.legend-label { font-size: 14px; }

.feature-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 6px;
  border-radius: 8px;
  cursor: pointer;
  min-height: var(--touch-min);
  justify-content: center;
}
.feature-item:hover { background: rgba(255,255,255,0.06); }
.feature-item-top { display: flex; align-items: center; gap: 8px; }
.feature-item-name { font-size: 14px; font-weight: 600; }
.feature-item-date { font-size: 12px; color: var(--text-dim); }
.empty-note { color: var(--text-dim); font-size: 13px; padding: 6px 2px; }

.footer-note {
  color: var(--text-dim);
  font-size: 12px;
  margin: 16px 0 4px;
  text-align: center;
}

/* ---------- Feature form panel ---------- */
.form-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 1500;
}

.feature-form-panel {
  position: fixed;
  z-index: 1600;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  color: var(--text);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
}

@media (min-width: 801px) {
  .feature-form-panel {
    top: calc(12px + var(--safe-top));
    right: calc(12px + var(--safe-right));
    bottom: calc(12px + var(--safe-bottom));
    width: 360px;
    border-radius: var(--radius);
  }
  .feature-form-panel .form-handle { display: none; }
}

@media (max-width: 800px) {
  .feature-form-panel {
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 90vh;
    border-top-left-radius: var(--radius);
    border-top-right-radius: var(--radius);
    padding-bottom: var(--safe-bottom);
  }
  .feature-form-panel .form-handle {
    width: 40px;
    height: 5px;
    background: var(--panel-border);
    border-radius: 3px;
    margin: 10px auto 4px;
  }
}

.form-scroll {
  overflow-y: auto;
  padding: 8px 18px 18px;
}

.feature-form-panel h2 { font-size: 17px; margin: 4px 0 12px; }

.form-remote-notice {
  background: rgba(249, 168, 37, 0.15);
  border: 1px solid #f9a825;
  color: #f9a825;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  margin-bottom: 10px;
}

.field { margin-bottom: 12px; display: flex; flex-direction: column; gap: 4px; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

label { font-size: 13px; color: var(--text-dim); font-weight: 600; }

input[type="text"], input[type="date"], input[type="search"], select, textarea {
  min-height: var(--touch-min);
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  padding: 8px 10px;
  width: 100%;
}
textarea { min-height: 72px; resize: vertical; }

.warning-box {
  background: rgba(211, 47, 47, 0.12);
  border: 1px solid #d32f2f;
  border-radius: 10px;
  padding: 10px 12px;
  margin: 14px 0;
  font-size: 12.5px;
  color: var(--text);
}
.warning-box strong { display: block; margin-bottom: 4px; color: #ef5350; }
.warning-box p { margin: 4px 0; }

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}
.form-actions .btn { flex: 1; }

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.history-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 6px;
  border-bottom: 1px solid var(--panel-border);
  font-size: 13px;
}
.history-item-top { display: flex; justify-content: space-between; gap: 8px; }
.history-op { text-transform: uppercase; font-size: 11px; color: var(--text-dim); }
.history-item .btn { margin-top: 4px; align-self: flex-start; padding: 6px 10px; min-height: 36px; }

/* ---------- Leaflet popup content ---------- */
.sh-popup { font-size: 14px; min-width: 180px; }
.sh-popup-title { font-weight: 700; font-size: 15px; margin-bottom: 4px; }
.sh-popup-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  margin-bottom: 6px;
}
.sh-popup-badge .swatch { width: 10px; height: 10px; }
.sh-popup-meta { color: #555; font-size: 12px; margin-bottom: 6px; }
.sh-popup-notes { white-space: pre-wrap; margin-bottom: 8px; font-size: 13px; }
.sh-popup-actions { display: flex; gap: 8px; }
.sh-popup-actions button {
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid #ccc;
  background: #f2f2f2;
  cursor: pointer;
  font-size: 13px;
}
.sh-popup-actions button.danger { background: #fdecea; border-color: #d32f2f; color: #b71c1c; }

/* leaflet popup dark-friendly tweak */
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: #f8f8f6;
  color: #1a1a1a;
}

/* ---------- Leaflet control layers a11y sizing ---------- */
.leaflet-touch .leaflet-bar a {
  width: var(--touch-min);
  height: var(--touch-min);
  line-height: var(--touch-min);
}

/* Desktop: Leaflet controls on the left (Geoman toolbar) must not hide behind the sidebar */
@media (min-width: 801px) {
  .leaflet-left { left: calc(12px + var(--safe-left) + 320px + 12px); }
  body:has(.sidebar.collapsed) .leaflet-left { left: 0; }
}
