:root {
  --bg: #0f1117;
  --surface: #171a22;
  --surface2: #1e2230;
  --border: #2a3042;
  --text: #e8ecf4;
  --muted: #8b95a8;
  --accent: #fc3f1d;
  --accent-hover: #ff5533;
  --link: #7db7ff;
  --link-hover: #a8d0ff;
  --success: #3dd68c;
  --radius: 10px;
  --font: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.app {
  max-width: 1600px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.header h1 { font-size: 1.5rem; font-weight: 600; }

.subtitle {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 4px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.stat-label {
  color: var(--muted);
  font-size: 0.78rem;
  margin-bottom: 6px;
}

.stat-value {
  font-size: 1.4rem;
  font-weight: 600;
}

.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.tab {
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  font: inherit;
}

.tab.active {
  background: rgba(252, 63, 29, 0.15);
  border-color: var(--accent);
  color: var(--text);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.filters { margin-bottom: 14px; }

.filters-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 160px;
}

.field-grow { flex: 1; min-width: 220px; }

.field-small { min-width: 110px; }

.field-check {
  min-width: auto;
  justify-content: flex-end;
  gap: 8px;
  padding-bottom: 10px;
}

.field-check input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.field-toggle {
  min-width: auto;
  justify-content: flex-end;
  gap: 8px;
  padding-bottom: 6px;
  cursor: pointer;
  user-select: none;
}

.field-toggle-label {
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.2;
}

.field-toggle:hover .field-toggle-label {
  color: var(--text);
}

.toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 40px;
  height: 22px;
  flex-shrink: 0;
}

.toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}

.toggle-track {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 999px;
  background: #2a3144;
  border: 1px solid var(--border);
  transition: background 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.25);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #c5cddc;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  transition: transform 0.18s ease, background 0.18s ease;
  pointer-events: none;
}

.toggle input:checked + .toggle-track {
  background: rgba(252, 63, 29, 0.85);
  border-color: var(--accent);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(252, 63, 29, 0.25);
}

.toggle input:checked + .toggle-track .toggle-thumb {
  transform: translateX(18px);
  background: #fff;
}

.toggle input:focus-visible + .toggle-track {
  outline: 2px solid var(--link);
  outline-offset: 2px;
}

.field-toggle:hover .toggle-track {
  border-color: #3d4660;
}

.field span {
  font-size: 0.78rem;
  color: var(--muted);
}

.field input,
.field select {
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font: inherit;
}

.search-select {
  position: relative;
}

.search-select-input {
  width: 100%;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font: inherit;
}

.search-select-input:focus {
  outline: none;
  border-color: #3d4660;
}

.search-select-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 20;
  max-height: 240px;
  overflow-y: auto;
  margin: 0;
  padding: 4px;
  list-style: none;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.search-select-list.hidden {
  display: none;
}

.search-select-option {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.92rem;
  color: var(--text);
}

.search-select-option:hover,
.search-select-option.is-active {
  background: rgba(252, 63, 29, 0.12);
}

.search-select-option.is-selected {
  color: var(--accent);
}

.search-select-empty {
  padding: 8px 10px;
  color: var(--muted);
  font-size: 0.88rem;
}

.btn {
  padding: 9px 16px;
  border-radius: 8px;
  border: none;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}

.btn.primary {
  background: var(--accent);
  color: #fff;
}

.btn.primary:hover { background: var(--accent-hover); }

.btn.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.btn.ghost:hover {
  color: var(--text);
  border-color: #3d4660;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.cards-panel { padding: 0; overflow: hidden; }

.cards-panel-body {
  position: relative;
  min-height: 120px;
}

.list-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 17, 23, 0.55);
  color: var(--muted);
  font-size: 0.9rem;
  z-index: 3;
}

.list-loading.hidden { display: none; }

.cards-container.is-loading {
  opacity: 0.55;
  pointer-events: none;
}

.list-meta {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
}

.cards-container {
  padding: 16px;
  max-height: calc(100vh - 380px);
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.date-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.date-group-head {
  display: flex;
  align-items: center;
  gap: 10px;
  position: sticky;
  top: 0;
  z-index: 2;
  background: linear-gradient(to bottom, var(--surface) 70%, transparent);
  padding: 4px 0 8px;
}

.date-group-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.date-group-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 24px;
  height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.75rem;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 14px;
}

.data-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: border-color 0.15s, transform 0.15s, box-shadow 0.15s;
}

.data-card:hover {
  border-color: #3d4660;
}

.org-card {
  cursor: pointer;
}

.org-card:hover {
  border-color: rgba(125, 183, 255, 0.45);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  transform: translateY(-1px);
}

.org-card:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 2px;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.card-title-wrap {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  min-width: 0;
}

.card-logo {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  object-fit: cover;
  background: var(--surface2);
  border: 1px solid var(--border);
}

.card-title {
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.35;
  word-break: break-word;
}

.card-subline {
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
  margin-top: -4px;
}

.card-time {
  flex-shrink: 0;
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(125, 183, 255, 0.1);
  border: 1px solid rgba(125, 183, 255, 0.18);
  color: #c8ddff;
  font-size: 0.74rem;
}

.tag-muted {
  background: rgba(139, 149, 168, 0.12);
  border-color: rgba(139, 149, 168, 0.2);
  color: var(--muted);
}

.muted {
  color: var(--muted);
  font-size: 0.9em;
}

.card-body {
  display: grid;
  gap: 8px;
}

.card-field {
  display: grid;
  grid-template-columns: 88px 1fr;
  gap: 8px;
  font-size: 0.84rem;
}

.card-field-label {
  color: var(--muted);
}

.card-field-value {
  word-break: break-word;
}

.card-link,
.detail-value a {
  color: var(--link);
  text-decoration: none;
  word-break: break-all;
}

.card-link:hover,
.detail-value a:hover {
  color: var(--link-hover);
  text-decoration: underline;
}

.advert-block {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.advert-badge {
  background: rgba(255, 196, 86, 0.18);
  color: #ffc456;
}

.advert-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.advert-products {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
}

.advert-product {
  display: flex;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface2);
}

.advert-product-img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
}

.advert-product-body {
  min-width: 0;
}

.advert-product-title {
  font-weight: 600;
  margin-bottom: 4px;
}

.advert-product-price {
  color: var(--success);
  margin-bottom: 4px;
}

.advert-product-url {
  font-size: 0.85rem;
}

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

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(61, 214, 140, 0.12);
  color: var(--success);
  font-size: 0.75rem;
}

.badge-verified {
  background: rgba(125, 183, 255, 0.15);
  color: var(--link);
  font-size: 0.7rem;
  padding: 1px 6px;
  vertical-align: middle;
  margin-left: 4px;
}

.badge-advert {
  background: rgba(255, 176, 72, 0.18);
  color: #e09a2f;
  font-size: 0.7rem;
  padding: 1px 6px;
  vertical-align: middle;
  margin-left: 4px;
}

.messenger-links {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 4px 8px;
}

.aspects-list,
.hours-list {
  margin: 0;
  padding-left: 1.1rem;
}

.aspects-list li,
.hours-list li {
  margin: 2px 0;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

.pagination-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.page-jump input {
  width: 72px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font: inherit;
  text-align: center;
}

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

.detail-dialog {
  position: fixed;
  inset: 0 auto 0 0;
  width: 50%;
  min-width: 300px;
  max-width: 920px;
  height: 100%;
  max-height: 100%;
  margin: 0;
  border: none;
  border-right: 1px solid var(--border);
  border-radius: 0;
  background: var(--surface);
  color: var(--text);
  padding: 0;
  overflow: hidden;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.icon-btn:hover {
  color: var(--text);
  border-color: #3d4660;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
  display: block;
}

.card-fields-dialog {
  width: min(520px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
}

.card-fields-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.phones-list,
.social-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.phone-item,
.social-item {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 10px;
}

.phone-type,
.social-type {
  color: var(--muted);
  font-size: 0.82em;
  text-transform: lowercase;
}

.detail-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.detail-form {
  padding: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.detail-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.detail-head h2 {
  margin: 0;
  font-size: 1.1rem;
  line-height: 1.3;
  min-width: 0;
  flex: 1;
}

.detail-head-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.detail-empty-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 160px;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.25;
  cursor: pointer;
  user-select: none;
}

.detail-empty-toggle:hover {
  color: var(--text);
  border-color: #3d4660;
}

.detail-empty-toggle input {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.detail-nav {
  display: flex;
  align-items: center;
  gap: 6px;
}

.detail-nav .btn {
  min-width: 36px;
  padding-inline: 10px;
}

.detail-nav-meta {
  min-width: 4.5ch;
  text-align: center;
  font-size: 0.82rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.detail-body {
  padding: 16px 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  min-height: 0;
  overflow: auto;
}

.detail-row {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 10px;
  font-size: 0.88rem;
}

.detail-label {
  color: var(--muted);
}

.detail-value {
  word-break: break-word;
}

.detail-value pre {
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.8rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}

.detail-section {
  margin: 12px 0 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface2);
}

.detail-section-title {
  margin: 0;
  padding: 12px 14px;
  font-size: 1rem;
  font-weight: 650;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.detail-section-title::-webkit-details-marker {
  display: none;
}

.detail-section-title::before {
  content: "▸";
  display: inline-block;
  margin-right: 8px;
  color: var(--muted);
  transition: transform 0.15s ease;
}

.detail-section[open] > .detail-section-title::before {
  transform: rotate(90deg);
}

.detail-section-body {
  padding: 0 14px 10px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.detail-list {
  margin: 0;
  padding-left: 1.1rem;
}

.detail-list li + li {
  margin-top: 4px;
}

.legal-raw-json {
  margin: 8px 0 14px;
}

.legal-raw-json summary {
  cursor: pointer;
  color: var(--muted);
  margin-bottom: 8px;
}

.legal-raw-json pre {
  max-height: 320px;
  overflow: auto;
}

.serp-breadcrumbs {
  line-height: 1.45;
}

.breadcrumb-sep {
  color: var(--muted);
  margin: 0 0.15em;
}

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

.export-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.export-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 0 0 16px;
}

.export-hint {
  padding: 0 18px;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.45;
}

.export-actions-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 18px;
}

.export-fields {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
  padding: 0 18px;
  max-height: 340px;
  overflow: auto;
}

.export-field {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  cursor: pointer;
  font-size: 0.86rem;
}

.export-field input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

.export-field code {
  grid-column: 2;
  color: var(--muted);
  font-size: 0.72rem;
}

.export-footer {
  display: flex;
  justify-content: flex-end;
  padding: 0 18px;
}

@media (max-width: 760px) {
  .detail-dialog {
    width: 100%;
    max-width: 100vw;
    min-width: 0;
    border-right: none;
  }
  .detail-row,
  .card-field { grid-template-columns: 1fr; }
  .field { min-width: 100%; }
  .cards-grid { grid-template-columns: 1fr; }
  .detail-head {
    flex-wrap: wrap;
  }
  .detail-head-actions {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .detail-empty-toggle {
    max-width: none;
    order: 3;
    flex: 1 1 100%;
  }
}

.viewer-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 360px);
  gap: 16px;
  align-items: start;
}

.viewer-main {
  min-width: 0;
}

.agent-panel {
  position: sticky;
  top: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: calc(100vh - 40px);
  min-height: 420px;
}

.agent-panel .panel-head h2 {
  font-size: 1.05rem;
}

.agent-panel .hint {
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.35;
}

.agent-token-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.82rem;
  color: var(--muted);
}

.agent-token-field input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 10px;
  font: inherit;
}

.agent-messages {
  flex: 1;
  min-height: 200px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.agent-msg {
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 0.88rem;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

.agent-msg--user {
  align-self: flex-end;
  background: rgba(252, 63, 29, 0.18);
  border: 1px solid rgba(252, 63, 29, 0.35);
  max-width: 95%;
}

.agent-msg--assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  max-width: 95%;
}

.agent-msg--tool {
  align-self: stretch;
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--muted);
  font-size: 0.78rem;
}

.agent-msg--error {
  align-self: stretch;
  background: rgba(255, 107, 107, 0.12);
  border: 1px solid rgba(255, 107, 107, 0.4);
  color: #ff6b6b;
}

.agent-chat-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.agent-chat-form textarea {
  resize: vertical;
  min-height: 72px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 10px;
  font: inherit;
}

.agent-status {
  font-size: 0.8rem;
  color: var(--muted);
}

@media (max-width: 1100px) {
  .viewer-layout {
    grid-template-columns: 1fr;
  }

  .agent-panel {
    position: static;
    max-height: none;
  }
}
