* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Arial, sans-serif;
  background: #f5f6f8;
  color: #1f2937;
}
.app { max-width: 1500px; margin: 0 auto; padding: 20px; }
.topbar {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: flex-start;
  margin-bottom: 18px;
}
h1 { margin: 0 0 4px; font-size: 28px; }
h2 { margin: 0 0 14px; font-size: 18px; }
p { margin: 0; }
.keybox {
  display: flex;
  gap: 8px;
  min-width: 420px;
}
input, textarea, button {
  font: inherit;
}
input, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  background: #fff;
}
textarea { resize: vertical; }
button {
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  background: #2563eb;
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { opacity: .9; }
button.secondary { background: #6b7280; }
button.danger { background: #dc2626; }
.grid.stats {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  margin-bottom: 18px;
}
.card {
  background: #fff;
  padding: 16px;
  border-radius: 16px;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.card span { display: block; color: #6b7280; font-size: 13px; margin-bottom: 8px; }
.card b { font-size: 22px; }
.layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 18px;
}
.panel, .content {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.panel { padding: 18px; }
.panel label {
  display: block;
  margin-top: 12px;
  margin-bottom: 6px;
  font-size: 13px;
  color: #4b5563;
}
.panel hr {
  border: 0;
  border-top: 1px solid #e5e7eb;
  margin: 20px 0;
}
.content { padding: 0; overflow: hidden; }
.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid #e5e7eb;
  padding: 10px;
  overflow-x: auto;
}
.tab {
  background: #e5e7eb;
  color: #111827;
}
.tab.active {
  background: #2563eb;
  color: #fff;
}
.tabpage {
  display: none;
  padding: 18px;
}
.tabpage.active { display: block; }
.row {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}
.formgrid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 12px;
}
.formgrid label {
  display: block;
  color: #4b5563;
  font-size: 13px;
}
.formgrid input {
  margin-top: 6px;
}
.check {
  display: flex !important;
  align-items: center;
  gap: 8px;
  padding-top: 24px;
}
.check input { width: auto; margin: 0; }
.hint {
  color: #6b7280;
  font-size: 13px;
  margin-bottom: 10px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
th, td {
  border-bottom: 1px solid #e5e7eb;
  text-align: left;
  vertical-align: top;
  padding: 9px;
}
th {
  color: #374151;
  background: #f9fafb;
  position: sticky;
  top: 0;
}
td small {
  display: block;
  color: #6b7280;
  word-break: break-all;
}
pre {
  background: #111827;
  color: #e5e7eb;
  padding: 14px;
  border-radius: 12px;
  overflow: auto;
  max-height: 560px;
  white-space: pre-wrap;
}
.progress {
  margin-top: 12px;
  color: #374151;
}
.chunk {
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 12px;
  background: #f9fafb;
}
.chunk h3 {
  margin: 0 0 8px;
  font-size: 15px;
}
.chunk p {
  white-space: pre-wrap;
  line-height: 1.45;
}
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  background: #e0e7ff;
  color: #3730a3;
  font-size: 12px;
}
.actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.actions button {
  padding: 7px 10px;
  font-size: 13px;
}
@media (max-width: 1000px) {
  .topbar, .layout { display: block; }
  .keybox { min-width: 0; margin-top: 12px; }
  .panel { margin-bottom: 18px; }
  .grid.stats { grid-template-columns: repeat(2, 1fr); }
  .formgrid { grid-template-columns: 1fr; }
}


/* Улучшение таблиц и мобильной версии */
html, body {
  max-width: 100%;
  overflow-x: hidden;
}

.table-scroll {
  width: 100%;
  overflow-x: auto;
  overflow-y: visible;
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  background: #fff;
}

.table-scroll table {
  margin: 0;
}

.docs-table {
  min-width: 1180px;
}

.docs-table th,
.docs-table td {
  white-space: nowrap;
}

.docs-table .doc-title-col {
  min-width: 420px;
  max-width: 520px;
  white-space: normal;
  word-break: break-word;
}

.docs-table .doc-title-col b {
  display: block;
  line-height: 1.25;
}

.docs-table .doc-title-col small {
  margin-top: 4px;
}

.docs-table .actions-col {
  min-width: 155px;
  width: 155px;
}

.docs-table .num-col {
  text-align: center;
  width: 80px;
}

.docs-table .date-col {
  min-width: 115px;
  width: 115px;
}

.docs-table td[title] small {
  display: inline-block;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
}

#documentsTable {
  max-width: 100%;
}

#librariesTable,
#categoriesTable {
  max-width: 100%;
  overflow-x: auto;
}

@media (max-width: 1000px) {
  .app {
    padding: 12px;
  }

  .topbar {
    display: block;
  }

  .keybox {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr;
    margin-top: 12px;
  }

  .grid.stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .layout {
    display: block;
  }

  .panel {
    margin-bottom: 14px;
  }

  .tabs {
    padding: 8px;
  }

  .tab {
    padding: 9px 12px;
  }

  .docs-table {
    min-width: 980px;
  }

  .docs-table .doc-title-col {
    min-width: 300px;
    max-width: 360px;
  }
}

@media (max-width: 560px) {
  .grid.stats {
    grid-template-columns: 1fr;
  }

  .row {
    flex-direction: column;
  }

  button {
    width: 100%;
  }

  .actions button {
    width: auto;
  }

  .docs-table {
    min-width: 900px;
  }
}


/* Удобные фильтры */
.filter-field {
  margin-bottom: 12px;
}

.filter-field label {
  margin-top: 0;
}

.filter-line {
  display: grid;
  grid-template-columns: 1fr 72px;
  gap: 8px;
  align-items: center;
}

button.mini {
  padding: 10px 10px;
  font-size: 14px;
}

.panel .hint {
  line-height: 1.25;
}

@media (max-width: 560px) {
  .filter-line {
    grid-template-columns: 1fr;
  }

  button.mini {
    width: 100%;
  }
}


/* Custom filter dropdowns */
.filter-field {
  position: relative;
}

.filter-menu {
  display: none;
  margin-top: 6px;
  border: 1px solid #d1d5db;
  border-radius: 12px;
  background: #fff;
  overflow: hidden;
  max-height: 220px;
  overflow-y: auto;
  box-shadow: 0 8px 22px rgba(0,0,0,.12);
  z-index: 20;
}

.filter-menu.open {
  display: block;
}

.filter-menu button {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 0;
  background: #fff;
  color: #111827;
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid #f3f4f6;
}

.filter-menu button:hover {
  background: #eef2ff;
}

.filter-menu-empty {
  padding: 10px 12px;
  color: #6b7280;
  font-size: 14px;
}

.filter-line {
  display: grid;
  grid-template-columns: 1fr 72px;
  gap: 8px;
  align-items: center;
}

button.mini {
  padding: 10px 10px;
  font-size: 14px;
}


/* Logs */
#logBox {
  min-height: 360px;
  max-height: 720px;
  overflow: auto;
}

#apiLogBox {
  max-width: 100%;
}

.api-log-table {
  min-width: 1500px;
  font-size: 13px;
}

.api-log-table th,
.api-log-table td {
  white-space: nowrap;
}

.api-log-table .query-col {
  min-width: 320px;
  max-width: 520px;
  white-space: normal;
  word-break: break-word;
}
