:root {
  --app-viewport-height: 100dvh;
  --panel: #f8fafc;
  --panel-border: #cbd5e1;
  --text: #172033;
  --muted: #64748b;
  --accent: #0f766e;
  --accent-dark: #115e59;
  --danger: #b91c1c;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  max-width: 100%;
  height: 100%;
  margin: 0;
  overflow-x: hidden;
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  width: 100%;
  max-width: 100vw;
  min-height: 100vh;
  min-height: 100dvh;
  min-height: var(--app-viewport-height);
  background: #e2e8f0;
}

body.auth-locked {
  display: grid;
  grid-template-columns: 1fr;
  background: #0f172a;
}

body.auth-locked .filters,
body.auth-locked #map {
  display: none;
}

body.auth-locked .settings-page {
  display: none;
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(circle at 20% 20%, rgb(20 184 166 / 0.20), transparent 32%),
    linear-gradient(135deg, #0f172a 0%, #1e293b 52%, #263447 100%);
}

.login-screen.hidden {
  display: none;
}

.login-box {
  display: grid;
  gap: 14px;
  width: min(360px, 100%);
  padding: 22px;
  background: #ffffff;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  box-shadow: 0 18px 50px rgb(15 23 42 / 0.24);
}

.login-box h1 {
  font-size: 22px;
}

.form-error {
  min-height: 18px;
  margin: 0;
  color: var(--danger);
  font-size: 13px;
  font-weight: 650;
}

.filters {
  z-index: 500;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  max-width: 100%;
  padding: 16px;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--panel-border);
  box-shadow: 0 10px 30px rgb(15 23 42 / 0.12);
}

.filters header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--panel-border);
}

.filters-toggle {
  display: none;
}

.filters-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.user-panel {
  display: grid;
  gap: 8px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--panel-border);
}

.user-panel > div {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
}

#current-user {
  min-width: 0;
  color: #334155;
  font-size: 13px;
  font-weight: 750;
  overflow-wrap: anywhere;
}

#logout {
  width: auto;
  height: 30px;
  padding: 0 10px;
  color: #172033;
  background: #dbe4ee;
}

#settings-link {
  width: 34px;
  height: 30px;
  padding: 0;
  color: #172033;
  font-size: 17px;
  line-height: 1;
  background: #dbe4ee;
  border: 1px solid #cbd5e1;
}

#settings-link:hover {
  background: #cbd5e1;
}

#settings-link.active {
  color: #ffffff;
  background: #0f766e;
  border-color: #0f766e;
  box-shadow:
    0 0 0 3px rgb(20 184 166 / 0.24),
    inset 0 -2px 0 rgb(15 23 42 / 0.22);
}

#settings-link.active:hover {
  background: #115e59;
  border-color: #115e59;
}

.users-section summary {
  cursor: pointer;
  color: #334155;
  font-size: 13px;
  font-weight: 750;
}

.users-section.hidden {
  display: none;
}

.create-user-form {
  display: grid;
  gap: 7px;
  margin-top: 8px;
}

.create-user-form input {
  height: 32px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-weight: 650;
}

.checkbox-label input {
  width: 14px;
  height: 14px;
}

.users-list {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}

.user-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 6px;
  align-items: center;
  padding: 6px;
  background: #eef2f7;
  border: 1px solid #d6dee9;
  border-radius: 6px;
}

.user-row strong {
  min-width: 0;
  font-size: 12px;
  overflow-wrap: anywhere;
}

.user-row span {
  color: var(--muted);
  font-size: 11px;
}

.user-row button {
  width: auto;
  height: 26px;
  padding: 0 8px;
  font-size: 12px;
}

h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.1;
}

#status {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

label {
  display: grid;
  gap: 5px;
  min-width: 0;
  color: #334155;
  font-size: 13px;
  font-weight: 650;
}

.hidden {
  display: none;
}

select,
input {
  width: 100%;
  min-width: 0;
  height: 34px;
  padding: 6px 8px;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid #94a3b8;
  border-radius: 6px;
  background: #ffffff;
}

select:focus,
input:focus {
  outline: 2px solid rgb(15 118 110 / 0.24);
  border-color: var(--accent);
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 4px;
}

.measure-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: stretch;
  padding: 8px;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgb(15 23 42 / 0.08);
}

button {
  height: 36px;
  color: #ffffff;
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  cursor: pointer;
}

.icon-button {
  display: inline-grid;
  place-items: center;
}

button:hover {
  background: var(--accent-dark);
}

#reset {
  color: #172033;
  background: #dbe4ee;
}

#reset:hover {
  background: #cbd5e1;
}

#measure-clear {
  width: auto;
  height: auto;
  min-height: 32px;
  padding: 0 12px;
  color: #172033;
  background: #dbe4ee;
}

#measure-clear:hover {
  background: #cbd5e1;
}

#measure-clear:disabled {
  color: #64748b;
  background: #e2e8f0;
  cursor: default;
}

.measure-result {
  display: flex;
  align-items: center;
  min-width: 0;
  min-height: 32px;
  padding: 6px 8px;
  color: #334155;
  font-size: 12px;
  font-weight: 650;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

#map {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  height: var(--app-viewport-height);
  min-width: 0;
  min-height: 0;
}

#map.measure-active {
  cursor: crosshair;
}

.settings-page {
  min-width: 0;
  min-height: 100vh;
  min-height: 100dvh;
  min-height: var(--app-viewport-height);
  padding: 24px;
  overflow-y: auto;
  background: #ffffff;
}

.settings-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 16px;
  align-items: start;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--panel-border);
}

.settings-header h1 {
  font-size: 24px;
}

.settings-header p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
}

.settings-page .users-section {
  display: grid;
  gap: 14px;
  width: min(760px, 100%);
  margin-top: 18px;
}

.settings-page .users-section.hidden {
  display: none;
}

.settings-page .create-user-form {
  grid-template-columns: minmax(130px, 1fr) minmax(150px, 1fr) auto auto;
  align-items: center;
  padding: 12px;
  background: #f8fafc;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
}

.settings-page .create-user-form .form-error {
  grid-column: 1 / -1;
}

.settings-page .users-list {
  gap: 8px;
  margin-top: 0;
}

.settings-page .user-row {
  padding: 10px;
}

.popup {
  min-width: 230px;
  max-width: 320px;
}

.popup h2 {
  margin: 0 0 8px;
  font-size: 15px;
}

.popup dl {
  display: grid;
  grid-template-columns: 110px minmax(0, 1fr);
  gap: 4px 8px;
  margin: 0;
  font-size: 12px;
}

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

.popup dd {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
}

.leaflet-control-layers {
  overflow: hidden;
  border-radius: 6px;
  border: 1px solid #94a3b8;
  box-shadow: 0 4px 14px rgb(15 23 42 / 0.18);
}

.leaflet-control-layers-toggle,
.measure-control-button {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  color: #172033;
  background-color: #ffffff;
  background-image: none;
  border: 0;
  border-radius: 0;
}

.leaflet-touch .leaflet-control-layers-toggle,
.leaflet-touch .measure-control-button {
  width: 34px;
  height: 34px;
}

.leaflet-control-layers-toggle::before {
  width: 18px;
  height: 18px;
  content: "";
  background:
    linear-gradient(135deg, transparent 45%, #172033 46% 54%, transparent 55%) 0 1px / 18px 18px,
    linear-gradient(#ffffff, #ffffff) 4px 2px / 12px 8px,
    linear-gradient(#ffffff, #ffffff) 2px 6px / 12px 8px,
    linear-gradient(#ffffff, #ffffff) 0 10px / 12px 8px;
  background-repeat: no-repeat;
  border: 2px solid #172033;
  box-shadow:
    -2px 4px 0 -1px #ffffff,
    -2px 4px 0 1px #172033,
    -4px 8px 0 -3px #ffffff,
    -4px 8px 0 -1px #172033;
  transform: translate(2px, -3px);
}

.leaflet-control-layers-toggle:hover,
.measure-control-button:hover {
  color: #172033;
  background-color: #f1f5f9;
}

.measure-control {
  overflow: visible;
  border-radius: 6px;
  border: 1px solid #94a3b8;
  box-shadow: 0 4px 14px rgb(15 23 42 / 0.18);
}

.measure-control-button svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.measure-control-button.active {
  color: #065f46;
  background: #ecfdf5;
  animation: measure-pulse 1.35s ease-in-out infinite;
}

.measure-control-button.active:hover {
  background: #d1fae5;
}

@keyframes measure-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgb(34 197 94 / 0.55);
  }

  50% {
    box-shadow: 0 0 0 8px rgb(34 197 94 / 0);
  }
}

.leaflet-control-layers-expanded {
  max-width: 210px;
  padding: 6px 8px;
  color: #263447;
  font-size: 12px;
  line-height: 1.15;
}

.leaflet-control-layers label {
  display: block;
  margin: 2px 0;
  color: #263447;
  font-size: 12px;
  font-weight: 650;
}

.leaflet-control-layers input[type="checkbox"] {
  width: 13px;
  height: 13px;
  margin: 0 5px 0 0;
  vertical-align: -2px;
}

.leaflet-control-layers-separator {
  margin: 5px 0;
}

.map-marker {
  display: block;
  width: 14px;
  height: 14px;
  border: 2px solid #0f172a;
  box-shadow: 0 1px 4px rgb(15 23 42 / 0.32);
}

.map-marker--circle {
  border-radius: 50%;
}

.map-marker--square {
  border-radius: 2px;
}

.map-marker--diamond {
  transform: rotate(45deg);
  border-radius: 2px;
}

.map-marker--triangle {
  width: 0;
  height: 0;
  border-top: 0;
  border-right: 8px solid transparent;
  border-bottom: 15px solid #0f172a;
  border-left: 8px solid transparent;
  background: transparent;
  box-shadow: none;
}

.map-marker--pso {
  border-color: #0f766e;
  background: #14b8a6;
}

.map-marker--ver2 {
  border-color: #7c2d12;
  background: #f97316;
}

.map-marker--kanalizacja {
  border-color: #1d4ed8;
  background: #60a5fa;
}

.map-marker--mufy {
  border-color: #be185d;
  background: #f472b6;
}

.map-marker--slupy {
  border-right-color: transparent;
  border-bottom-color: #ca8a04;
  border-left-color: transparent;
}

.map-marker--studnie {
  border-color: #0e7490;
  background: #22d3ee;
}

.map-marker--kable-abonenckie {
  border-color: #15803d;
  background: #86efac;
}

.map-marker--kable-optikabel {
  border-color: #7e22ce;
  background: #c084fc;
}

.map-marker--mufy-cieszynski {
  border-color: #b91c1c;
  background: #fca5a5;
}

.map-marker--kable-sfn {
  border-right-color: transparent;
  border-bottom-color: #475569;
  border-left-color: transparent;
}

.measure-point {
  display: grid;
  place-items: center;
}

.measure-point span {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  color: #ffffff;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  background: #dc2626;
  border: 2px solid #ffffff;
  border-radius: 50%;
  box-shadow: 0 2px 8px rgb(15 23 42 / 0.35);
}

.truncated {
  color: var(--danger);
}

@media (max-width: 768px) {
  html,
  body {
    width: 100%;
    max-width: 100vw;
    height: 100%;
    overflow-x: hidden;
  }

  body {
    position: relative;
    left: 0;
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    width: 100%;
    min-height: 100vh;
    min-height: 100dvh;
    min-height: var(--app-viewport-height);
    overflow: hidden;
  }

  .filters {
    grid-column: 1;
    width: 100%;
    max-width: 100vw;
    min-height: 0;
    padding: calc(10px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right))
      calc(10px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
    overflow: visible;
    border-right: 0;
    border-bottom: 1px solid var(--panel-border);
    box-shadow: 0 8px 22px rgb(15 23 42 / 0.14);
  }

  .filters header {
    align-items: center;
    gap: 8px;
    padding-bottom: 6px;
  }

  .filters header h1 {
    min-width: 0;
    font-size: 18px;
  }

  #status {
    max-width: 45vw;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .filters-toggle {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
    width: 100%;
    height: 38px;
    padding: 0 12px;
    text-align: left;
  }

  .filters-toggle span:first-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .filters-toggle-icon {
    font-size: 18px;
    line-height: 1;
    transition: transform 160ms ease;
  }

  body.filters-open .filters-toggle-icon {
    transform: rotate(180deg);
  }

  .filters-content {
    max-height: 0;
    padding-right: 2px;
    overflow-y: auto;
    overscroll-behavior: contain;
    visibility: hidden;
    transition: max-height 180ms ease;
    -webkit-overflow-scrolling: touch;
  }

  body.filters-open .filters-content {
    max-height: min(40vh, max(120px, calc(var(--app-viewport-height) - 190px)));
    visibility: visible;
  }

  .user-panel > div {
    grid-template-columns: minmax(0, 1fr) auto auto;
  }

  label,
  .actions,
  .measure-panel {
    width: 100%;
    min-width: 0;
  }

  #map {
    grid-column: 1;
    width: 100%;
    max-width: 100vw;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  .settings-page {
    grid-column: 1;
    width: 100%;
    max-width: 100vw;
    min-height: 0;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }

  .settings-page .create-user-form {
    grid-template-columns: 1fr;
  }
}
