:root {
  color-scheme: light;
  --ink: #18251f;
  --muted: #66736d;
  --forest: #245f46;
  --forest-dark: #174332;
  --mint: #e9f3ed;
  --cream: #f7f6f0;
  --line: #dfe5df;
  --white: #ffffff;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background: var(--cream);
}

button, input { font: inherit; }

.app-shell {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  position: relative;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 78px;
  padding: 0 clamp(20px, 4vw, 64px);
  background: rgba(255, 255, 255, .94);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 6px 24px rgba(31, 53, 43, .05);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  color: var(--white);
  background: var(--forest);
  border-radius: 12px 12px 12px 3px;
  font-weight: 800;
  letter-spacing: -.04em;
}

.brand strong, .brand small { display: block; }
.brand strong { font-size: 15px; letter-spacing: .01em; }
.brand small { margin-top: 3px; color: var(--muted); font-size: 11px; }

.logout-form { margin: 0; }
.logout-form button {
  min-height: 36px;
  padding: 0 14px;
  color: var(--forest-dark);
  background: var(--mint);
  border: 1px solid #bfd1c5;
  border-radius: 9px;
  font-size: 12px;
  font-weight: 750;
  cursor: pointer;
  transition: background .15s, border-color .15s, box-shadow .15s;
}
.logout-form button:hover { background: #deede3; border-color: #8fb39d; }
.logout-form button:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(36, 95, 70, .18); }

.login-page {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 18% 16%, rgba(72, 167, 116, .14), transparent 32%),
    var(--cream);
}
.login-shell { width: min(100%, 410px); }
.login-brand { margin: 0 0 18px 8px; }
.login-card {
  padding: clamp(28px, 6vw, 40px);
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: 0 18px 54px rgba(44, 65, 54, .12);
}
.login-card h1 { font-size: clamp(27px, 6vw, 34px); }
.login-description { margin: 12px 0 22px; color: var(--muted); font-size: 14px; line-height: 1.5; }
.login-error {
  margin: 0 0 16px;
  padding: 10px 12px;
  color: #8d302a;
  background: #fff3f1;
  border: 1px solid #efcbc6;
  border-radius: 8px;
  font-size: 12px;
  line-height: 1.4;
}
.login-form { display: grid; gap: 9px; }
.login-form label:not(:first-child) { margin-top: 6px; }
.login-form input { height: 48px; }
.login-form button {
  height: 48px;
  margin-top: 8px;
  color: var(--white);
  background: var(--forest);
  border: 0;
  border-radius: 10px;
  font-weight: 750;
  cursor: pointer;
  transition: background .15s, box-shadow .15s;
}
.login-form button:hover { background: var(--forest-dark); }
.login-form button:focus-visible { outline: none; box-shadow: 0 0 0 4px rgba(36, 95, 70, .18); }

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  padding: 20px;
  gap: 20px;
  overflow: hidden;
}

.search-panel {
  display: flex;
  min-height: 0;
  flex-direction: column;
  gap: 22px;
  padding: clamp(24px, 2.5vw, 36px);
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: 0 16px 50px rgba(44, 65, 54, .08);
  overflow-y: auto;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--forest);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(25px, 2.7vw, 35px);
  line-height: 1.06;
  letter-spacing: -.04em;
}

.description {
  max-width: 34ch;
  margin: 13px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}

#parcel-form { display: grid; gap: 8px; }
label { font-size: 13px; font-weight: 700; }

input {
  width: 100%;
  height: 54px;
  padding: 0 16px;
  color: var(--ink);
  background: #fbfcfa;
  border: 1px solid #cfd8d1;
  border-radius: 11px;
  outline: none;
  transition: border-color .2s, box-shadow .2s;
}

input:focus {
  border-color: var(--forest);
  box-shadow: 0 0 0 4px rgba(36, 95, 70, .12);
}

#cadastral-number {
  height: 48px;
  padding-inline: 14px;
}

#parcel-form > button {
  display: grid;
  place-items: center;
  height: 48px;
  margin-top: 2px;
  color: var(--white);
  background: var(--forest);
  border: 0;
  border-radius: 11px;
  font-weight: 750;
  cursor: pointer;
  transition: background .2s, transform .2s;
}

#parcel-form > button:hover { background: var(--forest-dark); }
#parcel-form > button:active { transform: translateY(1px); }
#parcel-form > button:disabled { opacity: .7; cursor: wait; }

.button-label, .button-loader { grid-area: 1 / 1; }
.button-loader {
  display: none;
  width: 21px;
  height: 21px;
  border: 2px solid rgba(255, 255, 255, .35);
  border-top-color: var(--white);
  border-radius: 50%;
  animation: spin .75s linear infinite;
}

#parcel-form.is-loading .button-label { visibility: hidden; }
#parcel-form.is-loading .button-loader { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }

.form-message {
  min-height: 20px;
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}
.form-message.error { color: #a53b32; }
.form-message.success { color: var(--forest); }

.saved-parcels {
  display: flex;
  min-height: 0;
  flex: 0 0 auto;
  flex-direction: column;
  gap: 10px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}
.section-heading { display: flex; align-items: center; justify-content: space-between; }
.section-heading h2 { margin: 0; font-size: 14px; }
.section-heading span {
  display: grid;
  place-items: center;
  min-width: 24px;
  height: 24px;
  padding: 0 7px;
  color: var(--forest-dark);
  background: var(--mint);
  border-radius: 20px;
  font-size: 11px;
  font-weight: 800;
}
.parcel-list { min-height: 45px; max-height: 170px; overflow-y: auto; padding-right: 3px; }
.parcel-filters { display: grid; gap: 6px; }
.parcel-filters label { color: var(--muted); font-size: 10px; }
.parcel-filters > input,
.area-filters input {
  height: 38px;
  padding: 0 10px;
  border-radius: 8px;
  font-size: 12px;
}
.area-filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px 7px;
  margin-top: 3px;
}
.empty-list { margin: 10px 0; color: var(--muted); font-size: 13px; }
.parcel-list-item {
  display: block;
  width: 100%;
  margin-bottom: 7px;
  padding: 11px 12px;
  color: var(--ink);
  background: #fafbf9;
  border: 1px solid var(--line);
  border-radius: 10px;
  text-align: left;
  cursor: pointer;
  transition: border-color .2s, background .2s, box-shadow .2s;
}
.parcel-list-item:hover { border-color: #9ab4a5; background: #f5f9f6; }
.parcel-list-item.active {
  background: var(--mint);
  border-color: var(--forest);
  box-shadow: inset 3px 0 0 var(--forest);
}
.parcel-list-item strong {
  display: block;
  overflow: hidden;
  font-size: 12px;
  line-height: 1.4;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.parcel-list-item small { display: block; margin-top: 4px; color: var(--muted); font-size: 10px; }

.hint {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-top: auto;
  padding: 16px;
  color: var(--muted);
  background: var(--mint);
  border-radius: 12px;
  font-size: 12px;
  line-height: 1.5;
}

.hint > span {
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  width: 20px;
  height: 20px;
  color: var(--white);
  background: var(--forest);
  border-radius: 50%;
  font-weight: 800;
}

.hint p { margin: 0; }
.hint button {
  padding: 0;
  color: var(--forest-dark);
  background: none;
  border: 0;
  border-bottom: 1px dashed currentColor;
  font-weight: 750;
  cursor: pointer;
}

.map-panel {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background: #dce7df;
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: 0 16px 50px rgba(44, 65, 54, .08);
}

#map { width: 100%; height: 100%; }

.map-caption {
  position: absolute;
  z-index: 500;
  right: 12px;
  bottom: 10px;
  padding: 5px 8px;
  color: #43524a;
  background: rgba(255, 255, 255, .84);
  border-radius: 5px;
  font-size: 10px;
  pointer-events: none;
}

.measurement-control {
  position: absolute;
  z-index: 800;
  top: 12px;
  left: 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  max-width: calc(100% - 76px);
}

.measurement-control button,
.measurement-control > span {
  min-height: 36px;
  padding: 0 12px;
  color: var(--forest-dark);
  background: rgba(255, 255, 255, .96);
  border: 1px solid #cbd8d0;
  border-radius: 9px;
  box-shadow: 0 4px 14px rgba(26, 54, 40, .16);
  font-size: 12px;
  font-weight: 750;
}

.measurement-control button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  transition: color .15s, background .15s, border-color .15s, box-shadow .15s;
}

.measurement-control button:hover {
  background: var(--mint);
  border-color: #8fb39d;
}

.measurement-control button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(36, 95, 70, .2), 0 4px 14px rgba(26, 54, 40, .16);
}

#measurement-toggle.active {
  color: var(--white);
  background: var(--forest);
  border-color: var(--forest);
}

#measurement-status {
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}

#measurement-status[hidden],
#measurement-clear[hidden] { display: none; }

#map.measurement-mode { cursor: crosshair; }
#map.measurement-mode .leaflet-interactive,
#map.measurement-mode .parcel-marker { pointer-events: none !important; }

.measurement-distance-label,
.measurement-point-label {
  color: var(--white);
  background: #9f382f;
  border: 0;
  box-shadow: 0 3px 10px rgba(73, 24, 20, .28);
  font-weight: 800;
}

.measurement-distance-label::before,
.measurement-point-label::before { border-top-color: #9f382f !important; }

.measurement-point-label {
  padding: 2px 5px;
  font-size: 10px;
}

.measurement-distance-label {
  padding: 5px 8px;
  font-size: 12px;
}

.leaflet-control-attribution { display: none; }
.leaflet-bar { border: 0 !important; box-shadow: 0 4px 18px rgba(26, 54, 40, .16) !important; }
.leaflet-bar a { color: var(--forest-dark) !important; }
.leaflet-popup-content-wrapper { border-radius: 12px; box-shadow: 0 9px 28px rgba(28, 49, 39, .2); }
.parcel-popup { min-width: 280px; max-height: 450px; overflow-y: auto; padding-right: 3px; }
.parcel-popup h2 { margin: 0 0 10px; color: var(--forest-dark); font-size: 16px; }
.popup-cadastral { margin: -5px 0 12px; color: var(--muted); font-size: 11px; }
.parcel-popup dl { display: grid; gap: 8px; margin: 0; }
.parcel-popup dt { color: var(--muted); font-size: 11px; }
.parcel-popup dd { margin: 2px 0 0; font-size: 13px; line-height: 1.35; }
.popup-comment { margin: 12px 0; padding: 10px; background: var(--mint); border-radius: 8px; }
.popup-comment strong { display: block; margin-bottom: 4px; font-size: 11px; }
.popup-comment p { margin: 0; font-size: 12px; line-height: 1.45; white-space: pre-wrap; }
.popup-custom-fields { margin: 12px 0; padding: 10px; background: #f7f7f2; border-radius: 8px; }
.popup-custom-fields h3,
.custom-fields-editor h3 { margin: 0 0 8px; font-size: 12px; }
.popup-custom-fields dl { gap: 7px; }
.popup-editor { display: grid; gap: 7px; padding-top: 12px; border-top: 1px solid var(--line); }
.popup-editor label { font-size: 11px; }
.popup-editor input, .popup-editor textarea {
  width: 100%;
  padding: 8px 9px;
  color: var(--ink);
  background: var(--white);
  border: 1px solid #cfd8d1;
  border-radius: 7px;
  font: inherit;
  font-size: 12px;
}
.popup-editor input { height: 36px; }
.popup-editor textarea { min-height: 66px; resize: vertical; }
.popup-actions { margin-top: 3px; }
.popup-actions button { padding: 8px 10px; border-radius: 7px; font-size: 11px; font-weight: 750; cursor: pointer; }
.save-parcel { color: var(--white); background: var(--forest); border: 1px solid var(--forest); }
.save-parcel:disabled { color: #587066; background: #e5ebe7; border-color: #d2dbd5; cursor: default; }
.popup-danger-zone {
  margin-top: 16px;
  padding: 11px;
  background: #fff8f6;
  border: 1px solid #efd2cd;
  border-radius: 8px;
}
.popup-danger-zone h3 { margin: 0 0 5px; color: #8d302a; font-size: 12px; }
.popup-danger-zone p { margin: 0 0 9px; color: #765c58; font-size: 11px; line-height: 1.4; }
.delete-parcel {
  padding: 7px 10px;
  color: #9c352e;
  background: #fff;
  border: 1px solid #dba9a3;
  border-radius: 7px;
  font-size: 11px;
  font-weight: 750;
  cursor: pointer;
}
.delete-parcel:hover { color: #76251f; background: #fdecea; border-color: #c98179; }
.delete-parcel:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(156, 53, 46, .16); }
.delete-parcel:disabled { opacity: .55; cursor: wait; }
.custom-fields-editor { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.custom-field-row {
  display: grid;
  grid-template-columns: minmax(0, .8fr) minmax(0, 1.2fr) 30px 30px;
  align-items: center;
  gap: 5px;
  margin-bottom: 6px;
}
.custom-field-row input,
.add-custom-field input {
  min-width: 0;
  width: 100%;
  height: 34px;
  padding: 0 7px;
  border: 1px solid #cfd8d1;
  border-radius: 6px;
  font: inherit;
  font-size: 11px;
}
.custom-field-row button {
  display: grid;
  place-items: center;
  align-self: center;
  width: 30px;
  height: 30px;
  min-width: 30px;
  margin: 0;
  padding: 0;
  border-radius: 6px;
  font-size: 15px;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  transition: color .15s, background .15s, border-color .15s, box-shadow .15s;
}
.save-custom-field { color: var(--white); background: var(--forest); border: 1px solid var(--forest); }
.delete-custom-field { color: #9c352e; background: #fff7f5; border: 1px solid #e5bdb8; }
.save-custom-field:hover { background: var(--forest-dark); border-color: var(--forest-dark); }
.delete-custom-field:hover { color: #7d2822; background: #fdecea; border-color: #d99b94; }
.save-custom-field:focus-visible,
.delete-custom-field:focus-visible,
.add-custom-field button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(36, 95, 70, .18);
}
.custom-field-row button:disabled { opacity: .48; cursor: default; }
.custom-field-row button.delete-custom-field:disabled { cursor: wait; }
.add-custom-field { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 9px; }
.add-custom-field button {
  grid-column: 1 / -1;
  justify-self: start;
  width: auto;
  height: 34px;
  margin: 0;
  padding: 0 13px;
  color: var(--forest-dark);
  background: var(--mint);
  border: 1px solid #b7cebf;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 750;
  line-height: 1;
  cursor: pointer;
  transition: background .15s, border-color .15s, box-shadow .15s;
}
.add-custom-field button:hover { color: var(--forest-dark); background: #deede3; border-color: #8fb39d; }
.add-custom-field button:disabled { opacity: .5; cursor: default; }
.parcel-marker {
  display: grid;
  place-items: center;
  color: var(--white);
  background: var(--forest);
  border: 2px solid var(--white);
  border-radius: 50% 50% 50% 6px;
  box-shadow: 0 3px 10px rgba(25, 55, 41, .32);
  transform: rotate(-45deg);
  transition: width .15s, height .15s, background .15s, box-shadow .15s;
}
.parcel-marker span { width: 6px; height: 6px; background: var(--white); border-radius: 50%; }
.parcel-marker.selected {
  background: #d64a3d;
  border-width: 3px;
  box-shadow: 0 5px 16px rgba(94, 32, 27, .45);
}
.parcel-marker.selected span { width: 8px; height: 8px; }

.toast-region {
  position: fixed;
  z-index: 2000;
  right: 22px;
  bottom: 22px;
  display: grid;
  gap: 8px;
  width: min(320px, calc(100vw - 28px));
  pointer-events: none;
}
.toast {
  padding: 11px 13px;
  color: var(--white);
  background: var(--forest-dark);
  border: 1px solid rgba(255, 255, 255, .22);
  border-radius: 9px;
  box-shadow: 0 8px 24px rgba(24, 50, 38, .24);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.35;
  animation: toast-in .18s ease-out;
}
.toast.error { background: #8d302a; }
.toast.leaving { animation: toast-out .18s ease-in forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(6px); } }

@media (max-width: 820px) {
  .app-shell { height: auto; min-height: 100%; }
  .topbar { height: 68px; }
  .workspace { grid-template-columns: 1fr; padding: 12px; gap: 12px; overflow: visible; }
  .search-panel { gap: 24px; padding: 26px 22px; border-radius: 16px; }
  .saved-parcels { flex-basis: auto; max-height: 240px; }
  .hint { margin-top: 0; }
  .map-panel, #map { min-height: 52vh; border-radius: 16px; }
}

@media (max-width: 480px) {
  .login-page { padding: 16px; }
  .login-card { border-radius: 16px; }
  .logout-form button { min-height: 34px; padding-inline: 11px; }
  .toast-region { right: 14px; bottom: 14px; }
  .measurement-control {
    right: 56px;
    flex-wrap: wrap;
    max-width: none;
  }
  .measurement-control button,
  .measurement-control > span { min-height: 34px; padding: 0 9px; font-size: 11px; }
  .leaflet-popup,
  .leaflet-popup-content-wrapper { max-width: calc(100vw - 24px); }
  .leaflet-popup-content {
    width: min(260px, calc(100vw - 54px)) !important;
    margin: 14px;
  }
  .parcel-popup {
    min-width: 0;
    width: min(260px, calc(100vw - 86px));
    max-height: min(450px, calc(52vh - 30px));
  }
  .custom-field-row {
    grid-template-columns: minmax(0, .8fr) minmax(0, 1.2fr) 28px 28px;
    gap: 4px;
  }
  .custom-field-row button { width: 28px; height: 28px; min-width: 28px; }
  .custom-field-row input,
  .add-custom-field input { font-size: 10px; }
}
