:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --btn-border: #b6bec9;
  --text: #16191d;
  --text-muted: #5c6672;
  --accent: #1f6feb;
  --accent-on: #ffffff;
  --accent-soft: #e8f0fe;
  --warn-bg: #fff4e5;
  --warn-border: #f0b429;
  --warn-text: #6b4708;
  --error-bg: #fdecec;
  --error-border: #d64545;
  --error-text: #8a2020;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1c2024;
    --surface-2: #23282e;
    --border: #333a42;
    --btn-border: #59616b;
    --text: #e6e9ed;
    --text-muted: #9aa4b0;
    --accent: #58a6ff;
    --accent-on: #0a1622;
    --accent-soft: #16324f;
    --warn-bg: #3a2d12;
    --warn-border: #b98a1e;
    --warn-text: #f0cd82;
    --error-bg: #3a1d1d;
    --error-border: #b04b4b;
    --error-text: #f2b0b0;
  }
}

* { box-sizing: border-box; }

/* Any element given `display` by a class beats the user-agent's [hidden]
   rule, because a class selector outranks it. Without this, .selection-bar
   and .active-filters stayed visible while marked hidden -- the selection bar
   sat there reading "0 selected" with nothing selected. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.mono {
  font-family: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
  font-size: 0.92em;
  font-variant-numeric: tabular-nums;
}

.spacer { flex: 1; }

/* --- top bar ------------------------------------------------------------ */

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 14px 20px;
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
}

.topbar h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: -0.01em;
}

.sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
}

.badge {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid transparent;
  white-space: nowrap;
}

.badge-muted {
  background: var(--surface-2);
  color: var(--text-muted);
}

/* --- banners ------------------------------------------------------------ */

.banner {
  max-width: 1400px;
  margin: 12px auto 0;
  padding: 10px 14px;
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  border-radius: var(--radius);
  color: var(--warn-text);
  font-size: 14px;
}

.error {
  margin: 12px 0;
  padding: 10px 14px;
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  border-radius: var(--radius);
  color: var(--error-text);
  font-size: 14px;
}

/* --- layout ------------------------------------------------------------- */

main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

/* --- filters ------------------------------------------------------------ */

.filters {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  align-items: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 150px;
}

.field-grow {
  flex: 1 1 320px;
}

.field-actions {
  min-width: 0;
  align-self: flex-end;
}

label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

input,
select,
button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
}

input:focus-visible,
select:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn-secondary {
  cursor: pointer;
  background: var(--surface);
  border-color: var(--btn-border);
  color: var(--text);
  font-weight: 500;
  padding: 9px 16px;
}

.btn-secondary:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

.btn-secondary:disabled {
  opacity: 0.45;
  cursor: default;
}

/* --- result bar --------------------------------------------------------- */

.resultbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 18px 2px 10px;
  flex-wrap: wrap;
}

.result-count {
  font-weight: 600;
}

.timing,
.perpage {
  font-size: 12.5px;
  color: var(--text-muted);
}

.perpage select {
  padding: 4px 6px;
  margin-left: 4px;
}

/* --- table -------------------------------------------------------------- */

.table-scroll {
  /* Both axes deliberately. `overflow-x: auto` alone still computes
     `overflow-y` to `auto`, which makes this div -- not the viewport -- the
     scroll container for the sticky header inside it. Being explicit and
     giving it a height makes the header stick to this box, which is what we
     actually want: filters stay on screen while the rows scroll. */
  overflow: auto;
  max-height: calc(100vh - var(--topbar-h, 58px) - 90px);
  min-height: 240px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overscroll-behavior: contain;
}

table {
  width: 100%;
  border-collapse: collapse;
  /* Eleven columns; below this the wrapper scrolls horizontally. */
  min-width: 1600px;
}

thead th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  position: sticky;
  /* Relative to .table-scroll, not the viewport -- see the note there. */
  top: 0;
  z-index: 1;
}

tbody td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

tbody tr:last-child td {
  border-bottom: none;
}

tbody tr:hover {
  background: var(--surface-2);
}

tbody[aria-busy='true'] {
  opacity: 0.5;
}

.placeholder td {
  text-align: center;
  color: var(--text-muted);
  padding: 32px 14px;
}

.col-tin { width: 110px; white-space: nowrap; }
.col-name { min-width: 230px; font-weight: 500; }
.col-addr { min-width: 200px; color: var(--text-muted); }
.col-region { width: 150px; font-size: 12.5px; }
.col-district { width: 150px; font-size: 12.5px; color: var(--text-muted); }
.col-oked { width: 74px; }
.col-tax { width: 130px; }
/* Bank names are long branch descriptions ("ТОШКЕНТ Ш., \"ТИФ МИЛЛИЙ
   БАНКИ\" АЖ БОШ ОФИСИ"). Truncate to one line with an ellipsis; the full
   text is in the title attribute on hover.
   NOT -webkit-line-clamp: that needs display:-webkit-box, which stops the
   element being a table cell and breaks the row layout -- it rendered a
   sliced third line instead of an ellipsis. */
.col-bank {
  width: 190px;
  max-width: 190px;
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.col-phone { width: 130px; white-space: nowrap; }
.col-date { width: 118px; white-space: nowrap; }
.col-status { width: 150px; }

.note {
  display: block;
  font-size: 12px;
  font-weight: 400;
  color: var(--text-muted);
  margin-top: 2px;
}

.muted { color: var(--text-muted); }

.pill-quiet {
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 11.5px;
}

.pill {
  display: inline-block;
  font-size: 12px;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  white-space: nowrap;
}

/* --- pager -------------------------------------------------------------- */

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin: 18px 0 8px;
}

.page-label {
  font-size: 13px;
  color: var(--text-muted);
  min-width: 150px;
  text-align: center;
}

/* --- footer ------------------------------------------------------------- */

footer {
  max-width: 1400px;
  margin: 0 auto;
  padding: 8px 20px 32px;
  font-size: 12.5px;
  color: var(--text-muted);
}

@media (max-width: 700px) {
  .field { min-width: 100%; }
  .table-scroll { max-height: none; }
  thead th { position: static; }
}

/* --- filter builder ----------------------------------------------------- */

.btn-filters {
  cursor: pointer;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.btn-filters:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-filters:disabled { opacity: 0.5; cursor: default; }

.btn-filters.has-filters {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

.count-badge {
  background: var(--accent);
  color: var(--accent-on);
  border-radius: 999px;
  font-size: 11px;
  line-height: 1;
  padding: 3px 6px;
  min-width: 18px;
  text-align: center;
}

.btn-primary {
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-on);
  border-color: var(--accent);
  font-weight: 600;
  padding: 9px 18px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

.btn-primary:hover { filter: brightness(1.08); }

.icon-btn {
  cursor: pointer;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
}

.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* active filter chips */

.active-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 14px 2px 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 4px 6px 4px 12px;
  font-size: 12.5px;
  max-width: 420px;
}

.chip > span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chip-x {
  cursor: pointer;
  background: transparent;
  border: none;
  color: inherit;
  font-size: 11px;
  padding: 2px 5px;
  border-radius: 999px;
  line-height: 1;
}

.chip-x:hover { background: rgba(127, 127, 127, 0.25); }

.chip-clear {
  cursor: pointer;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 12.5px;
  text-decoration: underline;
  padding: 4px 2px;
}

.chip-clear:hover { color: var(--accent); }

/* dialog */

.dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  background: var(--surface);
  color: var(--text);
  width: min(920px, calc(100vw - 32px));
  max-height: calc(100vh - 80px);
}

.dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.dialog-inner {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 80px);
}

.dialog-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.dialog-head h2 {
  margin: 0;
  font-size: 16px;
  flex: 1;
}

.dialog-body {
  padding: 16px 18px;
  overflow-y: auto;
}

.rule-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 14px;
}

.rule {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.rule-column { min-width: 165px; }
.rule-op { min-width: 155px; }

.rule-values {
  display: flex;
  gap: 8px;
  flex: 1 1 220px;
}

.rule-value { width: 100%; min-width: 120px; }

.rule-novalue {
  color: var(--text-muted);
  font-size: 13px;
  padding: 8px 2px;
}

.rule-empty {
  margin: 0;
  padding: 18px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.scan-flag {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--warn-bg);
  color: var(--warn-text);
  border: 1px solid var(--warn-border);
  border-radius: 4px;
  padding: 1px 5px;
}

.dialog-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-top: 1px solid var(--border);
}

@media (max-width: 700px) {
  .rule-column,
  .rule-op,
  .rule-values { flex: 1 1 100%; min-width: 0; }
}

/* --- selection, columns, export ----------------------------------------- */

.col-select { width: 36px; text-align: center; }
.col-select input { cursor: pointer; }
.col-mfo { width: 96px; }
.col-comments { min-width: 180px; color: var(--text-muted); }

.selection-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 14px 0 0;
  padding: 10px 14px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  font-size: 13px;
}

.linklike {
  cursor: pointer;
  background: none;
  border: none;
  color: var(--accent);
  font: inherit;
  text-decoration: underline;
  padding: 2px 0;
}

.linklike:hover { filter: brightness(1.15); }

.btn-danger {
  cursor: pointer;
  background: var(--error-bg);
  color: var(--error-text);
  border: 1px solid var(--error-border);
  border-radius: 6px;
  padding: 9px 16px;
  font: inherit;
  font-weight: 600;
}

.btn-danger:hover:not(:disabled) { filter: brightness(1.05); }
.btn-danger:disabled { opacity: 0.5; cursor: not-allowed; }

.dialog-narrow { width: min(560px, calc(100vw - 32px)); }

.column-list { display: flex; flex-direction: column; gap: 2px; }

.col-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border-radius: 6px;
}

.col-row:hover { background: var(--surface-2); }
.col-row.is-hidden .col-row-label { color: var(--text-muted); }

.col-row-label {
  flex: 1;
  cursor: pointer;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
  font-size: 14px;
  color: var(--text);
}

.delete-summary { margin: 0 0 12px; font-size: 14px; }

.banner-warn {
  margin: 0;
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-text);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.5;
}

.rule-multi { display: flex; flex-direction: column; gap: 4px; width: 100%; }
.rule-value-multi { min-height: 120px; }
.rule-multi-actions { display: flex; gap: 12px; }
.rule-multi-summary { font-size: 12px; color: var(--text-muted); }
.rule-multi-summary.is-empty { color: var(--warn-text); }
