:root {
  --paper: #fbfaf7;
  --surface: #ffffff;
  --ink: #1c1d21;
  --muted: #676b73;
  --line: #e4e1da;
  --line-strong: #cfcbc2;
  --overprint: #a02cb3;       /* morado de trazado IOF */
  --overprint-ink: #ffffff;
  --overprint-soft: #f6e9f8;
  --contour: #b86a2c;         /* marrón de curvas de nivel */
  --veg: #2f8f46;
  --veg-soft: #e6f3e8;
  --water: #1f7fc4;
  --water-soft: #e3f0fa;
  --warn: #9a6400;
  --warn-soft: #fff3d6;
  --danger: #c0352b;
  --shadow: 0 1px 2px rgba(28, 29, 33, .06), 0 4px 16px rgba(28, 29, 33, .05);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #15161a;
    --surface: #1d1f24;
    --ink: #ecebe8;
    --muted: #a0a3aa;
    --line: #2d3037;
    --line-strong: #3b3f47;
    --overprint: #d27ae0;
    --overprint-ink: #1a0d1d;
    --overprint-soft: #3a2340;
    --contour: #e0a068;
    --veg: #7fcf8f;
    --veg-soft: #1f3525;
    --water: #7ab8ea;
    --water-soft: #1b2e3f;
    --warn: #f0c060;
    --warn-soft: #3b2f14;
    --danger: #f07a70;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--paper);
  color: var(--ink);
  min-height: 100vh;
}
h1, h2, h3 { line-height: 1.2; margin: 0; }
a { color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- barra superior ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 700; font-size: 18px; letter-spacing: -.01em; }
.user { display: flex; align-items: center; gap: 12px; font-size: 14px; color: var(--muted); min-width: 0; }
#user-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.role-pill { background: var(--overprint-soft); color: var(--overprint); font-weight: 600; font-size: 12px; padding: 2px 8px; border-radius: 99px; }
.link { color: var(--ink); font-weight: 500; }

main { padding: clamp(16px, 4vw, 40px); max-width: 1240px; margin: 0 auto; }

/* ---------- vistas centradas ---------- */
.center-view { min-height: 60vh; display: grid; place-items: center; }
.hero { max-width: 460px; text-align: center; display: grid; gap: 14px; justify-items: center; }
.hero h1 { font-size: clamp(24px, 4vw, 32px); letter-spacing: -.02em; }
.hero p { color: var(--muted); margin: 0; }
.hero-art { width: 112px; height: 112px; }
.spinner { width: 32px; height: 32px; border: 3px solid var(--line); border-top-color: var(--overprint); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: 14px;
  padding: 9px 16px; border-radius: 8px;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: var(--ink); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--overprint); outline-offset: 2px; }
.btn-primary { background: var(--overprint); border-color: var(--overprint); color: var(--overprint-ink); }
.btn-primary:hover { filter: brightness(1.08); border-color: var(--overprint); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { border-color: var(--danger); filter: brightness(1.08); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--paper); border-color: var(--line); }
.btn-danger-text { color: var(--danger); }
.btn-sm { padding: 6px 11px; font-size: 13px; }
.btn-lg { padding: 12px 22px; font-size: 16px; }
.icon-btn { border: 0; background: none; font-size: 26px; line-height: 1; cursor: pointer; color: var(--muted); padding: 2px 6px; border-radius: 6px; }
.icon-btn:hover { color: var(--ink); background: var(--paper); }

/* ---------- catálogo ---------- */
.catalog-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 18px; }
.catalog-head h1 { font-size: 28px; letter-spacing: -.02em; }
.catalog-head p { margin: 4px 0 0; }

.filters { display: grid; grid-template-columns: 2fr repeat(4, 1fr); gap: 10px; margin-bottom: 22px; }
input, select, textarea {
  font: inherit; font-size: 14px; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 8px;
  padding: 9px 11px; width: 100%; min-width: 0; font-weight: 400;
}
textarea { resize: vertical; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow);
  position: relative; overflow: hidden;
}
.card::before { /* línea de "curva de nivel" decorativa */
  content: ""; position: absolute; inset: 0 0 auto 0; height: 4px;
  background: repeating-linear-gradient(90deg, var(--contour) 0 14px, transparent 14px 20px); opacity: .55;
}
.card-thumb {
  display: block; margin: -18px -18px 0; width: calc(100% + 36px); max-width: none; height: auto;
  aspect-ratio: 2 / 1; object-fit: cover; background: #fff; border-bottom: 1px solid var(--line);
}
.card:has(.card-thumb)::before { display: none; }
.thumb-preview { margin: 10px 0 0; }
.thumb-preview img { display: block; width: 100%; max-width: 420px; aspect-ratio: 2 / 1; object-fit: cover; border-radius: 8px; border: 1px solid var(--line-strong); background: #fff; }
.thumb-preview figcaption { margin-top: 4px; font-size: 12.5px; color: var(--muted); }
.card-top { display: flex; justify-content: space-between; align-items: baseline; }
.scale { font-variant-numeric: tabular-nums; font-weight: 800; font-size: 22px; letter-spacing: -.02em; color: var(--overprint); }
.card-version { font-size: 12px; font-weight: 700; color: var(--muted); border: 1px solid var(--line-strong); border-radius: 6px; padding: 1px 7px; }
.card h3 { font-size: 17px; }
.card-meta { margin: -4px 0 0; color: var(--muted); font-size: 13px; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.badge { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; background: var(--paper); border: 1px solid var(--line); color: var(--ink); }
.badge-norm { background: var(--water-soft); border-color: transparent; color: var(--water); }
.badge-geo { background: var(--veg-soft); border-color: transparent; color: var(--veg); }
.badge-nogeo { color: var(--muted); }
.badge-warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.card-notes { margin: 0; font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.card-warn { margin: 0; font-size: 12px; color: var(--warn); background: var(--warn-soft); padding: 6px 9px; border-radius: 6px; }
.card-foot { margin-top: auto; padding-top: 8px; display: flex; align-items: center; justify-content: space-between; gap: 8px; border-top: 1px dashed var(--line); }
.file-info { font-size: 12px; color: var(--muted); }
.card-admin { display: flex; flex-wrap: wrap; gap: 0; margin: 0 -8px -8px; }
.card-admin .btn-sm { padding: 6px 8px; font-size: 12.5px; }
.card-admin .btn-danger-text { margin-left: auto; }
.empty { text-align: center; color: var(--muted); padding: 60px 16px; border: 1px dashed var(--line-strong); border-radius: var(--radius); }

/* ---------- diálogos ---------- */
.dialog {
  border: 0; padding: 0; border-radius: 14px; width: min(640px, calc(100vw - 24px));
  max-height: calc(100vh - 32px); background: var(--surface); color: var(--ink);
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.dialog-sm { width: min(440px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(20, 16, 24, .45); }
.dialog form { display: flex; flex-direction: column; max-height: inherit; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.dialog-head h2 { font-size: 18px; }
.dialog-body { padding: 18px 20px; overflow: auto; }
.dialog-body p { margin: 0; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); }

.form-context { font-size: 13px; background: var(--overprint-soft); color: var(--ink); padding: 10px 12px; border-radius: 8px; margin-bottom: 14px !important; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: grid; gap: 5px; font-size: 13px; font-weight: 600; min-width: 0; }
.field.full { grid-column: 1 / -1; }
#file-field { margin-bottom: 12px; }
fieldset.field { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px; }
fieldset.field legend { padding: 0; margin-bottom: 6px; }
.radio { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; font-size: 14px; }
.radio input { width: auto; }
.georref input[name="sistemaRef"] { flex: 1 1 220px; }

.dropzone { position: relative; border: 2px dashed var(--line-strong); border-radius: 10px; padding: 20px; text-align: center; background: var(--paper); transition: border-color .15s, background .15s; }
.dropzone.over, .dropzone:hover { border-color: var(--overprint); background: var(--overprint-soft); }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.dz-text { font-weight: 500; font-size: 14px; }
.dz-text small { color: var(--muted); font-weight: 400; }

.progress { display: grid; gap: 6px; margin-top: 14px; font-size: 13px; color: var(--muted); }
.progress-bar { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
#progress-fill { height: 100%; width: 0; background: var(--overprint); transition: width .2s; }
.form-error { color: var(--danger); font-size: 14px; margin-top: 12px !important; }

.history { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.history li { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 4px; border-bottom: 1px solid var(--line); }
.history li > div:first-child { display: grid; min-width: 0; }
.history small { color: var(--muted); overflow-wrap: anywhere; }
.history-current strong { color: var(--veg); }
.history-actions { display: flex; gap: 2px; flex-shrink: 0; }

/* ---------- avisos ---------- */
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; width: min(480px, calc(100vw - 32px)); }
.toast { padding: 12px 16px; border-radius: 10px; background: var(--ink); color: var(--paper); font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.2); transition: opacity .4s, transform .4s; }
.toast-error { background: var(--danger); color: #fff; }
.toast.out { opacity: 0; transform: translateY(8px); }

/* ---------- móvil ---------- */
@media (max-width: 820px) {
  .filters { grid-template-columns: 1fr 1fr; }
  .filters input[type="search"] { grid-column: 1 / -1; }
}
@media (max-width: 520px) {
  .form-grid { grid-template-columns: 1fr; }
  .catalog-head { align-items: center; }
  .grid { grid-template-columns: 1fr; }
  #user-email { max-width: 140px; }
  .history li { flex-direction: column; align-items: flex-start; }
}

/* ---------- vista de mapa ---------- */
.head-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.segmented { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 9px; padding: 3px; background: var(--surface); }
.segmented button {
  display: inline-flex; align-items: center; gap: 6px; border: 0; background: none; color: var(--muted);
  font: inherit; font-size: 14px; font-weight: 600; padding: 6px 12px; border-radius: 6px; cursor: pointer;
}
.segmented button[aria-selected="true"] { background: var(--overprint-soft); color: var(--overprint); }
.catalog-map {
  height: min(68vh, 640px); min-height: 360px; border-radius: var(--radius); border: 1px solid var(--line);
  box-shadow: var(--shadow); background: var(--paper); z-index: 0;
}
.map-legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin: 10px 2px 0; font-size: 13px; color: var(--muted); }
.map-legend span { display: inline-flex; align-items: center; gap: 7px; }
.dot { width: 13px; height: 13px; border-radius: 50%; border: 3px solid var(--overprint); display: inline-block; }
.dot-geo { background: var(--overprint); }
.dot-nogeo { background: var(--surface); }
.box { width: 16px; height: 11px; border: 2px solid var(--overprint); background: var(--overprint-soft); display: inline-block; }
.no-location { margin-top: 14px; font-size: 13px; color: var(--muted); background: var(--surface); border: 1px dashed var(--line-strong); border-radius: 10px; padding: 10px 14px; }
.no-location strong { color: var(--ink); }
.linklike { border: 0; background: none; padding: 0; font: inherit; color: var(--overprint); text-decoration: underline; cursor: pointer; }

.card-top-right { display: flex; align-items: center; gap: 4px; }
.icon-btn.pin { font-size: 0; color: var(--overprint); padding: 4px; display: inline-flex; }

.popup { display: grid; gap: 4px; font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.popup strong { font-size: 15px; }
.popup-meta { color: var(--muted); font-size: 13px; }
.popup .btn { margin-top: 6px; justify-self: start; color: var(--overprint-ink); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--ink); }
.leaflet-container { font: 12px/1.4 system-ui, sans-serif; }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }

/* ---------- selector de ubicación ---------- */
.picker { height: 240px; border-radius: 10px; border: 1px solid var(--line-strong); background: var(--paper); cursor: crosshair; z-index: 0; }
.picker.leaflet-container { cursor: crosshair; }
.picker-info { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.loc-text { font-weight: 400; color: var(--muted); font-size: 13px; }
.loc-warn { margin: 0; font-weight: 500; font-size: 13px; color: var(--warn); background: var(--warn-soft); padding: 6px 9px; border-radius: 6px; }

@media (max-width: 520px) {
  .head-actions { width: 100%; justify-content: space-between; }
  .catalog-head { flex-wrap: wrap; }
  .picker { height: 200px; }
}
