:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --ok: #16a34a;
  --danger: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 1px 2px rgba(0, 0, 0, .04);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #f1f5f9;
  color: var(--ink);
}

.topbar {
  background: var(--bg);
  color: #fff;
  padding: 16px 24px;
}
.brand { display: flex; align-items: center; gap: 14px; max-width: 1100px; margin: 0 auto; }
.logo { font-size: 32px; }
.topbar h1 { margin: 0; font-size: 20px; letter-spacing: .2px; }
.tagline { margin: 2px 0 0; color: #94a3b8; font-size: 13px; }

main { max-width: 1100px; margin: 24px auto; padding: 0 24px; }

/* Dropzone */
.dropzone {
  border: 2px dashed #cbd5e1;
  border-radius: var(--radius);
  background: #fff;
  transition: border-color .2s, background .2s, transform .1s;
}
.dropzone.dragover {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.dropzone-inner {
  padding: 40px 20px;
  text-align: center;
}
.drop-icon { font-size: 40px; }
.drop-title { font-size: 18px; font-weight: 600; margin: 12px 0 4px; }
.drop-sub { color: var(--muted); margin: 0 0 14px; }

.btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { background: #1d4ed8; }
.btn:disabled { opacity: .6; cursor: default; }

.upload-status { margin: 12px 0; }
.upload-status .line { padding: 8px 12px; border-radius: 8px; margin-bottom: 6px; font-size: 14px; }
.upload-status .ok { background: #dcfce7; color: #166534; }
.upload-status .err { background: #fee2e2; color: #991b1b; }
.upload-status .info { background: #e0f2fe; color: #075985; }

/* Search */
.search-section { margin-top: 28px; }
.search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 14px;
  box-shadow: var(--shadow);
}
.search-icon { font-size: 18px; }
#searchInput {
  flex: 1;
  border: none;
  outline: none;
  font-size: 16px;
  padding: 8px 4px;
  background: transparent;
}
#datasetFilter {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  font-size: 14px;
  background: #fff;
}
.search-controls {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  margin: 12px 2px 0;
}
.search-controls select {
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px;
  font-size: 13px; background: #fff;
}
.search-controls select:disabled { background: #f1f5f9; color: #94a3b8; }
.check { font-size: 13px; color: #334155; display: flex; align-items: center; gap: 6px; cursor: pointer; }
.search-meta { color: var(--muted); font-size: 13px; margin: 10px 2px; }

.pager { display: flex; align-items: center; gap: 12px; justify-content: center; margin: 16px 0; }
.pager-info { font-size: 13px; color: var(--muted); }
.link-btn:disabled { opacity: .45; cursor: default; }

.results { display: grid; gap: 10px; }
.result-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
}
.result-head {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
}
.result-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.result-table td { padding: 3px 8px; border-bottom: 1px solid #f1f5f9; vertical-align: top; }
.result-table .k { color: var(--muted); white-space: nowrap; font-weight: 600; width: 1%; }
mark { background: #fef08a; padding: 0 2px; border-radius: 3px; }

/* Datasets */
.datasets-section { margin-top: 36px; }
.datasets-section h2 { font-size: 18px; display: flex; align-items: center; gap: 8px; }
.pill {
  background: var(--bg-soft); color: #fff; font-size: 12px;
  border-radius: 999px; padding: 2px 10px; font-weight: 600;
}
.dataset-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.dataset-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  position: relative;
}
.dataset-card h3 { margin: 0 0 6px; font-size: 15px; word-break: break-word; }
.dataset-card .meta { font-size: 12px; color: var(--muted); margin-bottom: 10px; }
.dataset-card .cols { font-size: 12px; color: #334155; max-height: 60px; overflow: hidden; }
.dataset-card .tag {
  display: inline-block; background: #f1f5f9; border-radius: 6px;
  padding: 1px 7px; margin: 2px 3px 0 0; font-size: 11px;
}
.card-actions { margin-top: 12px; display: flex; gap: 8px; }
.link-btn {
  background: none; border: 1px solid var(--line); border-radius: 7px;
  padding: 5px 10px; font-size: 12px; cursor: pointer; color: #334155;
}
.link-btn.danger { color: var(--danger); border-color: #fecaca; }
.link-btn:hover { background: #f8fafc; }

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

.footer { text-align: center; color: var(--muted); font-size: 12px; padding: 30px 0; }

/* Modal */
.modal {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .55);
  display: flex; align-items: center; justify-content: center; padding: 24px; z-index: 50;
}
.modal.hidden { display: none; }
.modal-box {
  background: #fff; border-radius: var(--radius); width: 100%; max-width: 1000px;
  max-height: 88vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal-head, .modal-foot {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 18px; border-bottom: 1px solid var(--line);
}
.modal-foot { border-bottom: none; border-top: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { overflow: auto; padding: 0; }
.table-wrap { overflow: auto; }
.full-table { border-collapse: collapse; width: 100%; font-size: 13px; }
.full-table th, .full-table td {
  border-bottom: 1px solid var(--line); padding: 7px 12px; text-align: left;
  white-space: nowrap; max-width: 320px; overflow: hidden; text-overflow: ellipsis;
}
.full-table thead th {
  position: sticky; top: 0; background: #f8fafc; cursor: pointer; user-select: none;
  font-weight: 600; color: #334155;
}
.full-table thead th:hover { background: #eef2f7; }
.full-table thead th.sorted { color: var(--accent); }
.full-table tbody tr:hover { background: #f8fafc; }

/* Spinner */
.spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid #cbd5e1; border-top-color: var(--accent);
  border-radius: 50%; animation: spin .8s linear infinite; vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Pipeline PSG ---------------------------------------------------------- */
.pipeline-section {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 24px;
  margin: 20px 0;
}
.pipeline-section h2 { margin: 0 0 4px; font-size: 18px; }
.pipe-sub { margin: 0 0 8px; color: var(--muted); font-size: 14px; }
.pipe-note {
  margin: 0 0 16px; font-size: 13px; color: #92400e;
  background: #fffbeb; border: 1px solid #fde68a; border-radius: 8px; padding: 8px 12px;
}
.pipe-note code { background: #fef3c7; padding: 1px 5px; border-radius: 4px; }

/* Fase 2: panel del Cloud Run Job */
.job-upload { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.btn-sec {
  background: #eef2ff; color: #3730a3; border: 1px solid #c7d2fe; border-radius: 8px;
  padding: 8px 14px; font-size: 13px; font-weight: 600; cursor: pointer;
}
.btn-sec:hover { background: #e0e7ff; }
.pipe-note-inline { font-size: 12px; color: var(--muted); }
.err-text { color: var(--danger); }
.muted { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
#jobSection select { width: 100%; margin-top: 4px; padding: 7px 8px; border: 1px solid var(--line); border-radius: 8px; }
.health { font-size: 11px; margin-top: 6px; }
.health .h-ok { color: var(--ok); }
.health .h-bad { color: var(--danger); }
.per-list { margin: 8px 0 14px; overflow-x: auto; }
.per-table { border-collapse: collapse; width: 100%; font-size: 13px; }
.per-table th, .per-table td { border-bottom: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.per-table thead th { color: #334155; font-weight: 600; background: #f8fafc; }
#periodosSection select { border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; font-size: 13px; }
#categSection select { border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; font-size: 13px; min-width: 220px; }
.pipe-num { font-size: 13px; color: #334155; }
.pipe-num input { width: 80px; margin-left: 4px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; }
.reglas-grid { display: grid; grid-template-columns: 1.3fr 1fr; gap: 18px; align-items: start; }
@media (max-width: 720px) { .reglas-grid { grid-template-columns: 1fr; } }
.otros-list { max-height: 320px; overflow: auto; }
.otros-list h4, .reglas-extra h4 { margin: 4px 0 8px; font-size: 14px; }
#reglasSection select, #reglaTermino { width: 100%; margin-top: 4px; padding: 8px 9px; border: 1px solid var(--line); border-radius: 8px; font-size: 13px; }
.cob-bar { margin: 6px 0 14px; }
.reglas-extra { margin-top: 14px; }
.rule { font-size: 12px; margin: 4px 0; }
#optSection select { border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; font-size: 13px; }
.opt-svg { width: 100%; max-width: 480px; height: auto; background: #f8fafc; border: 1px solid var(--line); border-radius: 8px; margin: 10px 0; }
.opt-controls { margin: 8px 0 12px; }
#optSuelo { vertical-align: middle; }
.pipe-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}
.pipe-file {
  display: flex; flex-direction: column; gap: 6px;
  border: 1px solid var(--line); border-radius: 10px;
  padding: 12px; font-size: 13px; font-weight: 600; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.pipe-file.has-file { border-color: var(--accent); background: var(--accent-soft); }
.pipe-file .req { color: var(--danger); }
.pipe-file input[type=file] { font-weight: 400; font-size: 12px; }
.pipe-file .fname { color: var(--muted); font-weight: 400; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pipe-actions { display: flex; align-items: center; gap: 14px; margin-top: 16px; }
.pipe-status { color: var(--muted); font-size: 14px; }
.pipe-result { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 16px; }
.pipe-result.hidden { display: none; }
.pipe-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; font-size: 13px; }
.btn.dl { background: var(--ok); text-decoration: none; }
.src-chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.src-chip.real { background: #dcfce7; color: #166534; }
.src-chip.synt { background: #fef9c3; color: #854d0e; }
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.kpi {
  display: flex; flex-direction: column; gap: 2px;
  background: #f8fafc; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  font-size: 12px; color: var(--muted);
}
.kpi span { font-size: 19px; font-weight: 700; color: var(--ink); }
.incid { margin: 14px 0 0; padding-left: 18px; color: #92400e; font-size: 13px; }
.ok-line { margin: 14px 0 0; color: var(--ok); font-size: 13px; }
.pipe-error { color: var(--danger); font-weight: 600; }
