:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1c2430;
  --muted: #637083;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e0f2ef;
  --danger: #c2410c;
  --good: #15803d;
  --fav: #e11d48;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 12px;
  --st-nuevo: #64748b;
  --st-interesa: #2563eb;
  --st-contactado: #7c3aed;
  --st-visita: #d97706;
  --st-visitado: #0891b2;
  --st-oferta: #16a34a;
  --st-descartado: #9f1239;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #171d24;
    --surface-2: #1f2730;
    --border: #2b3540;
    --text: #e6eaef;
    --muted: #94a0ae;
    --accent: #2dd4bf;
    --accent-ink: #062a27;
    --accent-soft: #123a36;
    --danger: #fb923c;
    --good: #4ade80;
    --fav: #fb7185;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --st-nuevo: #94a3b8;
    --st-interesa: #60a5fa;
    --st-contactado: #a78bfa;
    --st-visita: #fbbf24;
    --st-visitado: #22d3ee;
    --st-oferta: #4ade80;
    --st-descartado: #fb7185;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }
.nowrap { white-space: nowrap; }

/* Barra superior */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.1rem; color: var(--text); }
.brand img { width: 28px; height: 28px; }
.brand:hover { text-decoration: none; }
.nav { display: flex; gap: 4px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a {
  padding: 6px 12px; border-radius: 999px; color: var(--muted); white-space: nowrap; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav .count { font-size: .75rem; background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 0 6px; margin-left: 4px; }
.user { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: .9rem; white-space: nowrap; }

main { max-width: 1280px; margin: 0 auto; padding: 16px; overflow-wrap: anywhere; }

/* Controles */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 500; white-space: nowrap;
  min-height: 38px;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: .85rem; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.icon-btn { border: 0; background: transparent; cursor: pointer; padding: 6px; border-radius: 8px; line-height: 1; }
.icon-btn:hover { background: var(--surface-2); }

input[type=text], input[type=number], input[type=url], input[type=password], input[type=search],
input[type=datetime-local], select, textarea {
  width: 100%; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); min-height: 38px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
label.field > span { font-weight: 500; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.panel + .panel { margin-top: 16px; }
.stack > * + * { margin-top: 12px; }

/* Pestañas y filtros */
.tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; scrollbar-width: thin; }
.tab {
  border: 1px solid var(--border); background: var(--surface); padding: 6px 12px; border-radius: 999px;
  cursor: pointer; white-space: nowrap; font-weight: 500;
}
.tab.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.tab .n { opacity: .7; margin-left: 4px; font-variant-numeric: tabular-nums; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.filters > * { flex: 1 1 110px; min-width: 0; }
.filters > select { flex: 0 1 auto; width: auto; }
.filters > .q { flex: 3 1 220px; }
.row > select, .search-item select { width: auto; }
.toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }

/* Tarjetas */
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); }
.card {
  position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); display: flex; flex-direction: column;
}
.card.removed .card-img img { filter: grayscale(1); opacity: .6; }
.card.selected { outline: 2px solid var(--accent); }
.card-img { position: relative; display: block; aspect-ratio: 4 / 3; background: var(--surface-2); }
.card-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-img .noimg { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 2rem; }
.badge {
  position: absolute; font-size: .72rem; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: rgba(15, 20, 25, .72); color: #fff; backdrop-filter: blur(4px);
}
.badge.portal { left: 8px; top: 8px; }
.badge.photos { left: 8px; bottom: 8px; }
.badge.gone { right: 8px; bottom: 8px; background: #9f1239; }
.fav-btn {
  position: absolute; right: 8px; top: 8px; width: 36px; height: 36px; border-radius: 50%; border: 0;
  background: rgba(255, 255, 255, .92); cursor: pointer; font-size: 18px; line-height: 1; color: #9aa3ae;
  display: grid; place-items: center;
}
.fav-btn.on { color: var(--fav); }
.card-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.price-row { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.price { font-size: 1.2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.ppm { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
.drop { color: var(--good); font-size: .8rem; font-weight: 600; }
.rise { color: var(--danger); font-size: .8rem; font-weight: 600; }
.specs { font-size: .9rem; }
.card .title { color: var(--text); font-weight: 500; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.zone { color: var(--muted); font-size: .85rem; }
.card-foot { display: flex; align-items: center; gap: 6px; margin-top: auto; padding-top: 8px; flex-wrap: wrap; }
.pill {
  display: inline-flex; align-items: center; gap: 4px; font-size: .75rem; font-weight: 600;
  padding: 2px 8px; border-radius: 999px; border: 1px solid currentColor; white-space: nowrap;
}
.st-nuevo { color: var(--st-nuevo); } .st-interesa { color: var(--st-interesa); }
.st-contactado { color: var(--st-contactado); } .st-visita { color: var(--st-visita); }
.st-visitado { color: var(--st-visitado); } .st-oferta { color: var(--st-oferta); }
.st-descartado { color: var(--st-descartado); }
.stars { color: #f59e0b; letter-spacing: 1px; font-size: .85rem; white-space: nowrap; }
.compare-check { display: inline-flex; align-items: center; gap: 4px; font-size: .8rem; color: var(--muted); cursor: pointer; margin-left: auto; }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty h2 { color: var(--text); }

.selbar {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 1100;
  background: var(--text); color: var(--bg); border-radius: 999px; padding: 8px 8px 8px 16px;
  display: flex; gap: 8px; align-items: center; box-shadow: 0 6px 24px rgba(0,0,0,.25);
  max-width: calc(100vw - 32px); overflow-x: auto;
}
.selbar .btn { background: transparent; color: inherit; border-color: rgba(127,127,127,.5); }

/* Detalle */
.detail-head { display: flex; gap: 12px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 12px; }
.detail-grid { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(300px, 1fr); gap: 16px; align-items: start; }
@media (max-width: 900px) { .detail-grid { grid-template-columns: minmax(0, 1fr); } }
.head-price { display: none; margin-top: 6px; }
@media (max-width: 900px) { .head-price { display: block; } }
.detail-grid > * { min-width: 0; }
.gallery-main { position: relative; aspect-ratio: 4 / 3; background: var(--surface-2); border-radius: var(--radius); overflow: hidden; cursor: zoom-in; }
.gallery-main img { width: 100%; height: 100%; object-fit: cover; display: block; }
.gallery-main .nav-arrow { cursor: pointer; }
.thumbs { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0; }
.thumbs img { width: 84px; height: 63px; object-fit: cover; border-radius: 6px; cursor: pointer; opacity: .7; flex: none; border: 2px solid transparent; }
.thumbs img.on { opacity: 1; border-color: var(--accent); }
.nav-arrow {
  position: absolute; top: 50%; transform: translateY(-50%); width: 40px; height: 40px; border-radius: 50%;
  border: 0; background: rgba(255,255,255,.9); color: #111; font-size: 20px; display: grid; place-items: center;
}
.nav-arrow.prev { left: 8px; } .nav-arrow.next { right: 8px; }
.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.fact { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.fact .k { font-size: .75rem; color: var(--muted); }
.fact .v { font-weight: 600; font-variant-numeric: tabular-nums; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { font-size: .8rem; padding: 3px 10px; border-radius: 999px; background: var(--surface-2); }
.chip.no { text-decoration: line-through; opacity: .7; }
.status-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.status-picker button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 10px;
  cursor: pointer; font-size: .85rem; font-weight: 500;
}
.status-picker button.on { color: #fff; border-color: transparent; }
.status-picker button.on.st-nuevo { background: var(--st-nuevo); } .status-picker button.on.st-interesa { background: var(--st-interesa); }
.status-picker button.on.st-contactado { background: var(--st-contactado); } .status-picker button.on.st-visita { background: var(--st-visita); }
.status-picker button.on.st-visitado { background: var(--st-visitado); } .status-picker button.on.st-oferta { background: var(--st-oferta); }
.status-picker button.on.st-descartado { background: var(--st-descartado); }
@media (prefers-color-scheme: dark) { .status-picker button.on { color: #0f1419; } }
.rating-input { display: inline-flex; gap: 2px; }
.rating-input button { border: 0; background: none; font-size: 22px; cursor: pointer; color: #cbd2da; padding: 0 1px; }
.rating-input button.on { color: #f59e0b; }
.description { white-space: pre-line; }
.description.clamped { max-height: 12em; overflow: hidden; mask-image: linear-gradient(#000 70%, transparent); }
.note { border-left: 3px solid var(--accent); padding: 4px 10px; }
.note .meta { font-size: .78rem; color: var(--muted); display: flex; gap: 8px; align-items: center; }
.note .body { white-space: pre-wrap; }
.banner { padding: 10px 14px; border-radius: 10px; background: #fff7ed; color: #9a3412; border: 1px solid #fed7aa; margin-bottom: 12px; }
@media (prefers-color-scheme: dark) { .banner { background: #2a1a0e; color: #fdba74; border-color: #7c2d12; } }
.banner.info { background: var(--accent-soft); color: var(--text); border-color: transparent; }
.mini-map { height: 240px; border-radius: 10px; overflow: hidden; }
.calc { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.calc-out { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; margin-top: 10px; font-variant-numeric: tabular-nums; }
.calc-out .total { font-weight: 700; }
.history { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; font-size: .85rem; font-variant-numeric: tabular-nums; }
.edit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }

/* Visor de fotos */
.lightbox {
  position: fixed; inset: 0; z-index: 2000; background: rgba(0,0,0,.92); display: grid; place-items: center;
}
.lightbox img { max-width: 96vw; max-height: 90vh; object-fit: contain; }
.lightbox .close { position: absolute; top: 12px; right: 12px; background: rgba(255,255,255,.9); }
.lightbox .counter { position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); color: #fff; font-size: .9rem; }

/* Mapa */
.map-full { height: calc(100vh - 190px); min-height: 380px; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); }
.map-pop { width: 220px; }
.map-pop img { width: 100%; height: 120px; object-fit: cover; border-radius: 6px; display: block; margin-bottom: 6px; }
.leaflet-container { font: inherit; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .85rem; margin-bottom: 8px; }
.legend .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; background: currentColor; }

/* Comparar */
.compare-wrap { overflow-x: auto; }
table.compare { border-collapse: collapse; min-width: 100%; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
table.compare th, table.compare td { padding: 8px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; min-width: 180px; }
table.compare th { color: var(--muted); font-weight: 500; min-width: 120px; position: sticky; left: 0; background: var(--surface); }
table.compare img { width: 180px; height: 130px; object-fit: cover; border-radius: 8px; }
table.compare .best { color: var(--good); font-weight: 700; }

/* Búsquedas */
.search-item { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: start; }
.search-item + .search-item { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 12px; }
.url-cell { word-break: break-all; font-size: .8rem; }

.bookmarklet {
  display: inline-flex; align-items: center; gap: 8px; padding: 10px 16px; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; cursor: grab; border: 2px dashed transparent;
}
.bookmarklet:hover { text-decoration: none; border-color: var(--accent-ink); }
code, .code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .85em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

/* Login */
.login { max-width: 360px; margin: 10vh auto; }
.login .brand { justify-content: center; margin-bottom: 16px; font-size: 1.4rem; }

.toast {
  position: fixed; left: 50%; top: 64px; transform: translateX(-50%); z-index: 3000;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.25);
  max-width: calc(100vw - 32px);
}
.toast.error { background: var(--danger); color: #fff; }
.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 640px) {
  main { padding: 12px; }
  .topbar { padding: 8px 12px 4px; gap: 4px 8px; flex-wrap: wrap; }
  .topbar .user { margin-left: auto; }
  .nav { order: 3; flex-basis: 100%; margin: 0 -4px; }
  .user .name { display: none; }
  .grid { grid-template-columns: minmax(0, 1fr); }
  .calc { grid-template-columns: 1fr 1fr; }
}

/* Zona / prioridad */
.tier { display: inline-block; font-size: .72rem; font-weight: 700; padding: 1px 7px; border-radius: 999px; vertical-align: 1px; white-space: nowrap; }
.tier-1 { background: var(--accent); color: var(--accent-ink); }
.tier-2 { background: var(--accent-soft); color: var(--accent); }
.tier-3 { background: var(--surface-2); color: var(--text); }
.tier-4 { background: var(--surface-2); color: var(--muted); text-decoration: line-through; }
progress { accent-color: var(--accent); height: 8px; }
details > summary { cursor: pointer; font-weight: 600; }
