:root {
  --honey: #f2b705;
  --honey-dark: #b88600;
  --honey-soft: #fff6d6;
  --local: #2e8b57;
  --removed: #8a8a8a;
  --danger: #b3261e;
  --ink: #1f1a10;
  --muted: #6b6458;
  --line: #e8e2d4;
  --surface: #fffdf8;
  --bg: #f6f2e8;
  --radius: 10px;
  --shadow: 0 6px 24px rgba(31, 26, 16, 0.14);
  --panel-width: 340px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--ink);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
}

body {
  display: flex;
}

#map {
  flex: 1;
  height: 100%;
}

body.adding #map {
  cursor: crosshair;
}

/* Visibility depends on admin state */
body:not(.is-admin) .admin-only,
body.is-admin .public-only {
  display: none !important;
}

/* Panel */
.panel {
  width: var(--panel-width);
  height: 100%;
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  z-index: 1000;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  width: 36px;
  height: 36px;
}

.logo polygon {
  fill: var(--honey);
  stroke: var(--honey-dark);
  stroke-width: 2;
}

h1 {
  font-size: 1.25rem;
  margin: 0;
  letter-spacing: -0.01em;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.panel-toggle {
  display: none;
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 4px 20px 20px;
  display: flex;
  flex-direction: column;
}

section {
  padding: 16px 0;
  border-bottom: 1px solid var(--line);
}

h2 {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 10px;
}

h3 {
  font-size: 0.9rem;
  margin: 18px 0 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 10px;
}

/* Stats */
.stat-main {
  display: flex;
  flex-direction: column;
  margin-bottom: 12px;
}

.stat-number {
  font-size: 2.6rem;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  color: var(--muted);
  font-size: 0.9rem;
  margin-top: 4px;
}

.stat-breakdown {
  margin: 0;
  display: grid;
  gap: 4px;
  font-size: 0.9rem;
}

.stat-breakdown div {
  display: flex;
  justify-content: space-between;
}

.stat-breakdown dt {
  color: var(--muted);
}

.stat-breakdown dd {
  margin: 0;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Municipality filter */
.municipalities {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2px;
}

.municipalities label {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  margin: 0 -8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.92rem;
}

.municipalities label:hover {
  background: var(--bg);
}

.municipalities input {
  accent-color: var(--honey-dark);
  width: 16px;
  height: 16px;
  margin: 0;
}

.municipalities .name {
  flex: 1;
}

.municipalities .count {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
}

/* Legend */
.legend {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
  font-size: 0.9rem;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 10px;
}

.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex: none;
  border: 2px solid;
}

.dot-upstream {
  background: var(--honey);
  border-color: var(--honey-dark);
}

.dot-noted {
  background: #3b82f6;
  border-color: #1d4ed8;
}

.dot-local {
  background: var(--local);
  border-color: #1d5c39;
}

.dot-removed {
  background: transparent;
  border-color: var(--removed);
  border-style: dashed;
}

/* Buttons */
.button {
  appearance: none;
  border: 1px solid var(--honey-dark);
  background: var(--honey);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.button:hover {
  filter: brightness(1.05);
}

.button:disabled {
  opacity: 0.6;
  cursor: wait;
}

.button.secondary {
  background: var(--surface);
  border-color: var(--line);
}

.button.secondary:hover {
  background: var(--honey-soft);
}

.button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}

.button.ghost:hover {
  color: var(--ink);
  background: var(--bg);
}

.button.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.button.small {
  padding: 5px 10px;
  font-size: 0.82rem;
}

.button-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.button-row.end {
  justify-content: flex-end;
}

.button-col {
  display: grid;
  gap: 10px;
}

body.adding #add-mode {
  outline: 3px solid var(--honey-soft);
}

.switch {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  cursor: pointer;
}

.switch input {
  accent-color: var(--honey-dark);
  width: 16px;
  height: 16px;
}

.badge {
  background: var(--honey-soft);
  color: var(--honey-dark);
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 0.75rem;
}

.badge.warning {
  background: #ffedd5;
  color: #c2410c;
}

.dot-duplicate {
  background: #c2410c;
  border-color: #9a3412;
}

/* Change list */
.changes {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.changes button {
  width: 100%;
  text-align: left;
  background: var(--bg);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 8px;
  align-items: center;
}

.changes button:hover {
  border-color: var(--line);
  background: var(--surface);
}

.changes .dot {
  width: 10px;
  height: 10px;
  grid-row: span 2;
}

.changes .change-title {
  font-weight: 600;
}

.changes .change-note {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Footer */
.panel-footer {
  margin-top: auto;
  padding-top: 16px;
  display: grid;
  gap: 8px;
  justify-items: start;
}

.source {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.source a {
  color: inherit;
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
  margin: 0;
}

/* Popups */
.leaflet-popup-content-wrapper {
  border-radius: var(--radius);
}

.leaflet-popup-content {
  margin: 14px 16px;
  min-width: 230px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

.popup h3 {
  margin: 0 0 2px;
  font-size: 1rem;
}

.popup .tag {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 1px 8px;
  border-radius: 999px;
  margin-bottom: 8px;
}

.tag-upstream {
  background: var(--honey-soft);
  color: var(--honey-dark);
}

.tag-local {
  background: #e3f3ea;
  color: var(--local);
}

.tag-removed {
  background: #eee;
  color: #555;
}

.popup dl {
  margin: 0 0 10px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 10px;
  font-size: 0.82rem;
}

.popup dt {
  color: var(--muted);
}

.popup dd {
  margin: 0;
  word-break: break-word;
}

.popup .notice {
  background: var(--honey-soft);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.8rem;
  margin: 0 0 10px;
}

.popup .notice.warning {
  background: #ffedd5;
  color: #7c2d12;
}

.popup .popup-text {
  font-size: 0.85rem;
  margin: 0 0 10px;
}

.popup .multiline {
  white-space: pre-line;
}

.tag-duplicate {
  background: #ffedd5;
  color: #c2410c;
}

.link-button {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  font-weight: 600;
  color: inherit;
  text-decoration: underline;
  cursor: pointer;
}

.popup textarea,
dialog input {
  width: 100%;
  font: inherit;
  font-size: 0.85rem;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.popup textarea {
  resize: vertical;
  min-height: 52px;
  margin-bottom: 2px;
}

.popup .note-footer {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 10px;
}

.popup .save-status {
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0;
  min-height: 1em;
}

.popup .clear-note {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--danger);
  white-space: nowrap;
}

.popup .save-status.error {
  color: var(--danger);
}

.popup label,
dialog label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 4px;
}

/* Search */
.search {
  position: fixed;
  top: 14px;
  left: calc(var(--panel-width) + 14px);
  width: min(360px, calc(100vw - var(--panel-width) - 90px));
  z-index: 1050;
}

.search input {
  width: 100%;
  font: inherit;
  font-size: 0.95rem;
  padding: 11px 38px 11px 40px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  appearance: none;
}

.search input:focus {
  outline: 2px solid var(--honey);
  outline-offset: 0;
}

.search input::-webkit-search-cancel-button {
  display: none;
}

.search-icon {
  position: absolute;
  left: 13px;
  top: 50%;
  width: 18px;
  height: 18px;
  transform: translateY(-50%);
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-linecap: round;
  pointer-events: none;
}

.search-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}

.search-clear:hover {
  background: var(--bg);
  color: var(--ink);
}

.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  list-style: none;
  margin: 0;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-height: 60vh;
  overflow-y: auto;
}

.search-results li {
  display: flex;
  flex-direction: column;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
}

.search-results li.active {
  background: var(--honey-soft);
}

.search-results .search-message {
  color: var(--muted);
  font-size: 0.9rem;
  cursor: default;
}

.result-title {
  font-weight: 600;
  font-size: 0.92rem;
}

.result-meta {
  color: var(--muted);
  font-size: 0.8rem;
  margin: 0;
}

.popup .surroundings {
  margin: 8px 0 0;
  font-size: 0.85rem;
}

.search-pin {
  background: none;
  border: none;
}

.search-pin::before {
  content: '';
  position: absolute;
  left: 1px;
  top: 0;
  width: 24px;
  height: 24px;
  border-radius: 50% 50% 50% 0;
  background: var(--ink);
  border: 3px solid #fff;
  transform: rotate(-45deg);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  box-sizing: border-box;
}

/* Banner, toast, dialog */
.add-banner {
  position: fixed;
  top: 72px;
  left: calc(50% + var(--panel-width) / 2);
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 8px 8px 8px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 1100;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
}

.add-banner .button.ghost {
  color: #ddd;
}

.add-banner .button.ghost:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: calc(50% + var(--panel-width) / 2);
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 1100;
  font-size: 0.9rem;
}

.toast.error {
  background: var(--danger);
}

dialog {
  border: none;
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 22px;
  width: min(360px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--ink);
}

.show-all {
  margin-top: 8px;
  width: 100%;
}

.changes-dialog[open] {
  width: min(520px, calc(100vw - 32px));
  max-height: min(80vh, 720px);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-header h2 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
}

.dialog-header .button {
  font-size: 1.3rem;
  line-height: 1;
  padding: 4px 10px;
}

.changes-dialog input {
  margin: 0;
}

.changes-dialog .changes {
  overflow-y: auto;
  min-height: 0;
}

.changes-dialog .hint {
  margin: 0;
}

dialog::backdrop {
  background: rgba(31, 26, 16, 0.4);
}

dialog h2 {
  font-size: 1.1rem;
  text-transform: none;
  letter-spacing: 0;
  color: var(--ink);
  margin: 0 0 6px;
}

dialog input {
  margin-bottom: 12px;
  font-size: 1rem;
}

dialog .error {
  margin-bottom: 12px;
}

/* Mobile: panel becomes a bottom sheet */
@media (max-width: 720px) {
  body {
    flex-direction: column-reverse;
  }

  .panel {
    width: 100%;
    height: auto;
    max-height: 45vh;
    border-right: none;
    border-top: 1px solid var(--line);
    box-shadow: 0 -4px 16px rgba(31, 26, 16, 0.1);
  }

  .panel.collapsed .panel-body {
    display: none;
  }

  .panel-header {
    padding: 12px 16px;
  }

  .panel-body {
    padding: 0 16px 16px;
  }

  .panel-toggle {
    display: block;
    width: 36px;
    height: 36px;
    border: none;
    background: var(--bg);
    border-radius: 8px;
    cursor: pointer;
    position: relative;
  }

  .panel-toggle span {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 10px;
    height: 10px;
    border-right: 2px solid var(--ink);
    border-bottom: 2px solid var(--ink);
    transform: translate(-50%, -70%) rotate(45deg);
  }

  .panel.collapsed .panel-toggle span {
    transform: translate(-50%, -30%) rotate(-135deg);
  }

  #map {
    flex: 1;
    min-height: 0;
  }

  .add-banner,
  .toast {
    left: 50%;
  }

  .search {
    left: 12px;
    top: 12px;
    width: calc(100vw - 80px);
  }

  .search-results {
    max-height: 40vh;
  }
}
