:root {
  color-scheme: dark;
  --bg: #0a0a0a;
  --bg-2: #111;
  --bg-3: #181818;
  --border: #222;
  --text: #eaeaea;
  --muted: #888;
  --accent: #3b82f6;
  --safe: #10b981;
  --mid: #eab308;
  --bad: #ef4444;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 -apple-system, system-ui, "Segoe UI", sans-serif; }
.hidden { display: none !important; }

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 12px 20px; background: var(--bg-2); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; font-size: 16px; }
.meta { color: var(--muted); font-size: 12px; }
.link-btn {
  margin-left: auto;
  background: transparent; color: var(--accent); border: 1px solid var(--border);
  padding: 6px 12px; border-radius: 6px; cursor: pointer; font: inherit;
}
.link-btn:hover { background: var(--bg-3); }

/* Layout */
.layout { display: grid; grid-template-columns: 280px 1fr; gap: 0; min-height: calc(100vh - 49px); }
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .filters { position: static !important; max-height: none !important; }
}

.filters {
  background: var(--bg-2); border-right: 1px solid var(--border);
  padding: 16px; position: sticky; top: 49px; max-height: calc(100vh - 49px); overflow-y: auto;
}
.filter-block { margin-bottom: 18px; }
.filter-block label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); margin-bottom: 6px; }
.filter-block label.chk { display: flex; gap: 8px; align-items: center; text-transform: none; letter-spacing: normal; color: var(--text); font-size: 13px; margin-bottom: 6px; }
.filter-block .search-label { font-size: 12px; }
.filter-block input[type="number"],
.filter-block input[type="search"],
.filter-block select {
  width: 100%; background: var(--bg-3); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 7px 10px; font: inherit;
}
.filter-block input[type="range"] { width: 100%; }
.filter-block .row { display: flex; gap: 6px; }
.range-value { color: var(--muted); font-size: 12px; margin-top: 4px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--bg-3); border: 1px solid var(--border); border-radius: 12px;
  padding: 4px 10px; font-size: 12px; cursor: pointer; user-select: none;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: white; }
.chip.chip-sm { padding: 2px 8px; font-size: 11px; }
.chip .chip-n { color: var(--muted); margin-left: 4px; font-size: 10px; }
.chip.on .chip-n { color: rgba(255,255,255,0.6); }
.attr-row { margin-bottom: 10px; }
.attr-row .attr-label { font-size: 11px; color: var(--muted); margin-bottom: 4px; }

.card .badge.tier-badge { top: 8px; right: 56px; left: auto; font-weight: 600; }
.card .badge.vision-badge { top: 38px; left: 8px; color: #60a5fa; }

.tier-row { display: flex; gap: 10px; align-items: baseline; margin: 6px 0 12px; flex-wrap: wrap; }
.tier-pill { display: inline-block; padding: 3px 10px; border: 1px solid; border-radius: 14px; font-weight: 600; font-size: 13px; }
.tier-reason { color: var(--muted); font-size: 12px; }

.attr-box { background: var(--bg-3); border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; margin: 12px 0; }
.attr-box-title { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.6px; margin-bottom: 8px; }
.attr-box .specs-table { margin: 0; font-size: 12px; }
.attr-src { color: var(--muted); font-size: 10px; }

.reset-btn {
  width: 100%; background: var(--bg-3); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px; cursor: pointer; font: inherit;
}
.reset-btn:hover { background: #222; }

/* Results */
main { padding: 16px 20px 80px; }
.results-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 12px; color: var(--muted); font-size: 13px;
}
.legend { display: flex; gap: 6px; }
.safety-pill {
  font-size: 11px; padding: 2px 8px; border-radius: 12px;
}
.safety-pill.safe { background: rgba(16,185,129,0.18); color: var(--safe); }
.safety-pill.mid { background: rgba(234,179,8,0.18); color: var(--mid); }
.safety-pill.bad { background: rgba(239,68,68,0.18); color: var(--bad); }

.grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
}
.card {
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px;
  overflow: hidden; cursor: pointer; display: flex; flex-direction: column;
  transition: transform .12s, border-color .12s;
}
.card:hover { transform: translateY(-2px); border-color: #333; }
.card .thumb {
  width: 100%; aspect-ratio: 4/3; background: #000 center/cover no-repeat;
  position: relative;
}
.card .badge {
  position: absolute; top: 8px; left: 8px;
  font-size: 11px; padding: 3px 8px; border-radius: 12px;
  background: rgba(0,0,0,0.6); color: white;
  backdrop-filter: blur(4px);
}
.card .src {
  position: absolute; top: 8px; right: 8px;
  font-size: 10px; padding: 2px 6px; border-radius: 4px;
  background: rgba(0,0,0,0.6); color: var(--muted);
}
.card .body { padding: 10px 12px; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.card .price { font-size: 18px; font-weight: 600; }
.card .title { font-size: 13px; color: #ccc; line-height: 1.3; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; min-height: 34px; }
.card .loc { font-size: 12px; color: var(--muted); }
.card .specs { font-size: 12px; color: var(--muted); margin-top: 4px; display: flex; gap: 8px; flex-wrap: wrap; }
.card .specs .ico { color: #aaa; }

.empty { text-align: center; padding: 60px 0; color: var(--muted); }
.load-more-wrap { text-align: center; margin-top: 24px; }
#load-more { background: var(--bg-3); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 10px 24px; cursor: pointer; font: inherit; }

/* Modal — desktop popup */
body.modal-open { overflow: hidden; }
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.7); backdrop-filter: blur(6px); }
.modal-content {
  position: relative; background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px;
  max-width: 720px; width: 100%; max-height: 92vh; overflow-y: auto;
}
.modal-content.wide { max-width: 1100px; }
.modal-close {
  position: sticky; top: 10px; margin-left: auto; margin-right: 10px; z-index: 2;
  display: block; float: right;
  background: rgba(0,0,0,0.8); border: 1px solid var(--border); color: white;
  width: 36px; height: 36px; border-radius: 50%; cursor: pointer; font-size: 22px; line-height: 1;
}
.modal-close:hover { background: #222; }

/* Mobile: full-page detail */
@media (max-width: 768px) {
  .modal { padding: 0; align-items: stretch; }
  .modal-backdrop { display: none; }
  .modal-content {
    max-width: 100%; max-height: 100%; min-height: 100vh; min-height: 100dvh;
    border-radius: 0; border: 0;
  }
  .modal-close {
    position: fixed; top: max(12px, env(safe-area-inset-top)); left: 12px; right: auto; margin: 0;
    background: rgba(0,0,0,0.75); backdrop-filter: blur(8px);
    width: 40px; height: 40px; font-size: 20px;
  }
  .modal-close::before { content: '←'; }
  .modal-close { color: transparent; }
  .modal-close::before { color: white; }
  .gallery { aspect-ratio: 4/3; max-height: 50vh; }
  .detail-body { padding: 16px; }
}

.gallery { position: relative; background: #000; aspect-ratio: 4/3; max-height: 60vh; }
.gallery img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.gallery .gbtn {
  position: absolute; top: 50%; transform: translateY(-50%);
  background: rgba(0,0,0,0.5); color: white; border: none;
  width: 40px; height: 40px; border-radius: 50%; cursor: pointer; font-size: 22px;
}
.gallery .gbtn.prev { left: 10px; }
.gallery .gbtn.next { right: 10px; }
.gallery .gcount { position: absolute; bottom: 10px; right: 10px; color: white; font-size: 12px; background: rgba(0,0,0,0.6); padding: 3px 8px; border-radius: 12px; }
.gallery .thumbs { position: absolute; bottom: 10px; left: 10px; display: flex; gap: 4px; }
.gallery .thumbs div { width: 8px; height: 8px; border-radius: 50%; background: rgba(255,255,255,0.4); }
.gallery .thumbs div.active { background: white; }

.detail-body { padding: 20px 24px 28px; }
.detail-body h2 { margin: 0 0 6px; font-size: 18px; }
.detail-body .row { display: flex; gap: 14px; align-items: baseline; margin: 4px 0; }
.detail-body .price-big { font-size: 28px; font-weight: 700; }
.detail-body .loc { color: var(--muted); }
.detail-body .specs-table { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 14px 0; font-size: 13px; }
.detail-body .specs-table .k { color: var(--muted); }
.detail-body .desc { background: var(--bg-3); padding: 12px 14px; border-radius: 8px; margin-top: 12px; white-space: pre-wrap; font-size: 13px; line-height: 1.6; color: #ccc; }
.detail-body .actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.detail-body .actions a { background: var(--bg-3); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 8px 14px; text-decoration: none; font-size: 13px; }
.detail-body .actions a:hover { background: #222; }
.detail-body .vision-box { background: rgba(59,130,246,0.08); border: 1px solid rgba(59,130,246,0.3); border-radius: 8px; padding: 12px 14px; margin: 12px 0; font-size: 13px; }
.detail-body .vision-box .v-head { color: var(--accent); font-weight: 600; margin-bottom: 6px; font-size: 14px; }
.detail-body .vision-box .v-notes { color: #ddd; line-height: 1.5; margin: 4px 0; }
.detail-body .vision-box .v-flags { margin-top: 8px; display: flex; gap: 4px; flex-wrap: wrap; align-items: center; color: #eab308; font-size: 12px; }
.detail-body .vision-box .v-flags .flag-pill { background: rgba(234,179,8,0.15); border: 1px solid rgba(234,179,8,0.3); border-radius: 10px; padding: 2px 8px; font-size: 11px; }
.detail-body .vision-box .v-stamp { color: var(--muted); font-size: 11px; margin-top: 6px; }

.card .badge.vision-badge { top: 38px; left: 8px; color: #60a5fa; }

/* Map block */
.map-block { margin: 14px 0; }
.map-toggle {
  width: 100%; background: var(--bg-3); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 14px; cursor: pointer; font: inherit; text-align: left;
}
.map-toggle:hover { background: #222; }
.map-coords { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.map-frame { margin-top: 8px; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.map-frame iframe { display: block; width: 100%; height: 360px; border: 0; }
.map-links { display: flex; gap: 12px; padding: 8px 12px; background: var(--bg-3); font-size: 12px; }
.map-links a { color: var(--accent); text-decoration: none; }
.map-links a:hover { text-decoration: underline; }

.safety-badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 12px; font-size: 12px; font-weight: 600; }
.safety-badge.safe { background: rgba(16,185,129,0.18); color: var(--safe); }
.safety-badge.mid { background: rgba(234,179,8,0.18); color: var(--mid); }
.safety-badge.bad { background: rgba(239,68,68,0.18); color: var(--bad); }
.safety-badge.none { background: var(--bg-3); color: var(--muted); }

/* Safety report */
#safety-body { padding: 20px 24px; }
#safety-body h2 { margin-top: 0; }
.safety-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
.safety-table th, .safety-table td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); font-size: 13px; }
.safety-table th { color: var(--muted); font-weight: 500; text-transform: uppercase; font-size: 11px; letter-spacing: 0.5px; }
.safety-table td.num { text-align: left; font-variant-numeric: tabular-nums; }
.safety-table .bar { display: inline-block; height: 8px; border-radius: 2px; vertical-align: middle; min-width: 2px; margin-right: 6px; }

/* Safety report: oblast cards + grouped tables */
.oblast-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; margin: 16px 0 24px; }
.oblast-card { background: var(--bg-3); border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; }
.oblast-card .oblast-name { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.oblast-card .oblast-meta { font-size: 11px; color: var(--muted); margin-bottom: 8px; }
.oblast-card .oblast-bar-row { display: flex; align-items: center; gap: 8px; }
.oblast-card .oblast-bar-row .bar { display: inline-block; height: 8px; border-radius: 2px; }
.oblast-card .oblast-score { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }

.oblast-block { margin: 14px 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.oblast-block > summary { padding: 10px 14px; cursor: pointer; background: var(--bg-3); list-style: none; font-size: 14px; }
.oblast-block > summary::-webkit-details-marker { display: none; }
.oblast-block > summary::before { content: '▸'; display: inline-block; margin-right: 8px; transition: transform 0.15s; color: var(--muted); }
.oblast-block[open] > summary::before { transform: rotate(90deg); }
.oblast-block > summary > .oblast-summary-meta { color: var(--muted); font-size: 12px; margin-left: 6px; }
.oblast-block > .safety-table { margin: 0; }
.oblast-block > .safety-table th { background: var(--bg-2); }
.oblast-block > .safety-table td, .oblast-block > .safety-table th { padding-left: 14px; }
