:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-faint: #94a3b8;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --dark: #0f172a;
  --dark-2: #1e293b;
  --msk: #dc2626;
  --msk-soft: #fee2e2;
  --obl: #059669;
  --obl-soft: #d1fae5;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.1);
  --shadow-lg: 0 16px 48px rgba(15, 23, 42, 0.18);
  --radius: 12px;
  --radius-sm: 8px;
  --header-h: 64px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, "Noto Sans", sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  font-size: 14px;
  line-height: 1.45;
}

.hidden {
  display: none !important;
}

/* ---------- Login ---------- */
.login-screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(1200px 600px at 50% -10%, #1e3a8a 0%, #0f172a 60%, #020617 100%);
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: 18px;
  padding: 36px 30px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.login-logo {
  font-size: 44px;
  line-height: 1;
}
.login-card h1 {
  margin: 6px 0 0;
  font-size: 22px;
  letter-spacing: -0.02em;
}
.login-sub {
  margin: 0 0 8px;
  color: var(--text-muted);
  font-size: 13px;
}
.login-card input {
  width: 100%;
  padding: 13px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.login-card input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.login-card button {
  width: 100%;
  padding: 13px 14px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.login-card button:hover {
  background: var(--primary-hover);
}
.login-error {
  color: var(--msk);
  font-size: 13px;
  min-height: 18px;
}

/* ---------- App layout ---------- */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 18px;
  height: var(--header-h);
  background: var(--dark);
  color: #fff;
  box-shadow: var(--shadow);
  z-index: 30;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}
.brand-icon {
  font-size: 24px;
}
.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}
.brand-title {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.01em;
}
.brand-sub {
  font-size: 11px;
  color: #94a3b8;
}
.topbar-search {
  flex: 1 1 auto;
  position: relative;
  max-width: 520px;
}
.topbar-search .search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 17px;
  height: 17px;
  fill: none;
  stroke: #94a3b8;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
#global-search {
  width: 100%;
  padding: 9px 12px 9px 36px;
  border-radius: 9px;
  border: 1px solid #334155;
  background: #1e293b;
  color: #fff;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
#global-search::placeholder {
  color: #64748b;
}
#global-search:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.35);
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 9px 13px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  background: #1e293b;
  color: #e2e8f0;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn:hover {
  background: #334155;
}
.btn:active {
  transform: translateY(1px);
}
.btn .ic {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--primary-hover);
}
.btn-ghost {
  background: transparent;
  border-color: #334155;
  color: #cbd5e1;
}
.btn-ghost:hover {
  background: #1e293b;
}
.btn[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
}
.btn-sm {
  padding: 6px 10px;
  font-size: 13px;
}

/* ---------- Chips ---------- */
.chips {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.chips:not(:empty) {
  padding: 11px 18px;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: var(--primary-soft);
  color: #1e40af;
  border: 1px solid #bfdbfe;
  border-radius: 999px;
  padding: 4px 6px 4px 12px;
  font-size: 12.5px;
  max-width: 320px;
}
.chip b {
  font-weight: 600;
}
.chip > span.chip-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chip button {
  border: none;
  background: rgba(30, 64, 175, 0.12);
  color: #1e40af;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}
.chip button:hover {
  background: rgba(30, 64, 175, 0.25);
}

/* ---------- Table ---------- */
.table-wrap {
  flex: 1 1 auto;
  position: relative;
  overflow: auto;
  background: var(--surface);
}
table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-variant-numeric: tabular-nums;
}
thead th {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface-2);
  border-bottom: 2px solid var(--border-strong);
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0;
  white-space: nowrap;
}
.th-inner {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 11px 14px;
}
.th-label {
  flex: 1 1 auto;
}
.th-sort {
  color: var(--primary);
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  width: 14px;
  text-align: center;
}
.th-filter {
  flex: 0 0 auto;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  width: 30px;
  height: 28px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  display: grid;
  place-items: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.th-filter:hover {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: #bfdbfe;
}
.th-filter.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

tbody td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  max-width: 360px;
}
tbody tr:hover td {
  background: var(--surface-2);
}
.muted {
  color: var(--text-faint);
}
.cell-region {
  white-space: nowrap;
}
.badge {
  display: inline-flex;
  align-items: center;
  font-size: 11.5px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  letter-spacing: 0.01em;
}
.badge.msk {
  background: var(--msk-soft);
  color: var(--msk);
}
.badge.obl {
  background: var(--obl-soft);
  color: var(--obl);
}
.cell-primary {
  font-weight: 600;
  color: var(--text);
}
.cell-type {
  color: var(--text-faint);
  font-weight: 500;
  font-size: 12.5px;
}
.houses {
  font-size: 13px;
  color: var(--text-muted);
}
.houses b {
  display: inline-block;
  min-width: 22px;
  padding: 1px 7px;
  margin-right: 6px;
  background: var(--primary-soft);
  color: #1e40af;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
}
.houses .more {
  color: var(--primary);
  cursor: pointer;
  white-space: nowrap;
  font-weight: 500;
}
.houses .more:hover {
  text-decoration: underline;
}

.loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(1px);
  z-index: 20;
}
.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
.empty {
  padding: 60px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 15px;
}

/* ---------- Footer / pager ---------- */
.footer {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 18px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.summary {
  color: var(--text-muted);
  font-size: 13px;
}
.pager {
  display: flex;
  align-items: center;
  gap: 14px;
}
.page-size-label {
  color: var(--text-muted);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}
#page-size {
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 5px 8px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.pager-btns {
  display: flex;
  align-items: center;
  gap: 4px;
}
.pager-btns .btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  min-width: 34px;
  justify-content: center;
}
.pager-btns .btn:hover:not([disabled]) {
  background: var(--surface-2);
  border-color: var(--primary);
  color: var(--primary);
}
.page-info {
  font-size: 13px;
  color: var(--text-muted);
  min-width: 120px;
  text-align: center;
}

/* ---------- Filter / sort popover ---------- */
.filter-pop {
  position: fixed;
  width: 296px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 100;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.fp-sort {
  display: flex;
  flex-direction: column;
  padding: 6px;
  gap: 2px;
}
.fp-sort-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 8px 10px;
  border-radius: 7px;
  font-size: 13.5px;
  color: var(--text);
  cursor: pointer;
}
.fp-sort-btn:hover {
  background: var(--surface-2);
}
.fp-sort-btn.active {
  background: var(--primary-soft);
  color: #1e40af;
  font-weight: 600;
}
.fp-sort-ic {
  color: var(--primary);
  font-weight: 700;
  width: 14px;
  text-align: center;
}
.fp-divider {
  height: 1px;
  background: var(--border);
  margin: 0 6px;
}
.fp-search {
  padding: 10px 10px 6px;
}
.fp-search input {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  font-size: 13.5px;
  outline: none;
}
.fp-search input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.fp-all {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.fp-list {
  max-height: 280px;
  overflow-y: auto;
  padding: 4px 0;
}
.fp-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 6px 12px;
  font-size: 13.5px;
  cursor: pointer;
}
.fp-item:hover {
  background: var(--surface-2);
}
.fp-item input {
  flex: 0 0 auto;
}
.fp-item .v {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.fp-item .c {
  flex: 0 0 auto;
  color: var(--text-faint);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.fp-empty {
  padding: 18px;
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
}
.fp-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}
.fp-spacer {
  flex: 1 1 auto;
}
.filter-pop .btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
}
.filter-pop .btn:hover {
  background: var(--surface-2);
}
.filter-pop .btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.filter-pop .btn-primary:hover {
  background: var(--primary-hover);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--dark);
  color: #fff;
  padding: 12px 20px;
  border-radius: 10px;
  font-size: 13.5px;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  z-index: 200;
  pointer-events: none;
  max-width: 90vw;
  text-align: center;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Responsive: tablet ---------- */
@media (max-width: 920px) {
  .brand-text {
    display: none;
  }
  .btn-label {
    display: none;
  }
  #export .btn-label {
    display: inline;
  }
}

/* ---------- Responsive: mobile (card layout) ---------- */
@media (max-width: 680px) {
  :root {
    --header-h: 56px;
  }
  .topbar {
    flex-wrap: wrap;
    height: auto;
    padding: 10px 12px;
    gap: 10px;
  }
  .topbar-search {
    order: 3;
    flex-basis: 100%;
    max-width: none;
  }
  .brand {
    flex: 1 1 auto;
  }
  .brand-text {
    display: flex;
  }

  .table-wrap {
    background: var(--bg);
    padding: 10px;
  }
  table,
  thead,
  tbody,
  th,
  td,
  tr {
    display: block;
  }
  thead {
    position: sticky;
    top: 0;
    z-index: 10;
  }
  thead tr {
    display: flex;
    overflow-x: auto;
    gap: 6px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 6px;
    margin-bottom: 10px;
    box-shadow: var(--shadow-sm);
  }
  thead th {
    position: static;
    border: none;
    background: var(--surface-2);
    border-radius: 7px;
    flex: 0 0 auto;
  }
  .th-inner {
    padding: 7px 10px;
  }
  tbody tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 6px 4px;
    box-shadow: var(--shadow-sm);
  }
  tbody td {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    border-bottom: 1px dashed var(--border);
    max-width: none;
    padding: 8px 14px;
    text-align: right;
  }
  tbody tr td:last-child {
    border-bottom: none;
  }
  tbody td::before {
    content: attr(data-label);
    flex: 0 0 42%;
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--text-faint);
    align-self: center;
  }
  tbody tr:hover td {
    background: transparent;
  }
  .footer {
    justify-content: center;
  }
  .filter-pop {
    width: calc(100vw - 24px);
    max-width: 340px;
  }
}
