:root {
  --color-bg: #161826;
  --color-surface: #1c1e2e;
  --color-surface-2: #222435;
  --color-surface-3: #2a2c40;
  --color-border: #2f3247;
  --color-border-strong: #3d4160;
  --color-text: #e9e9ed;
  --color-text-muted: #9a9db0;
  --color-text-dim: #6c6f85;
  --color-accent: #9184d9;
  --color-accent-100: rgba(145, 132, 217, 0.12);
  --color-accent-200: rgba(145, 132, 217, 0.22);
  --color-danger: #d97a7a;
  --color-danger-bg: rgba(217, 122, 122, 0.1);
  --color-success: #7ad9a0;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;
  --space-12: 48px;

  --radius-sm: 4px;
  --radius: 8px;
  --radius-lg: 12px;
  --radius-pill: 999px;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-2: 0 8px 24px rgba(0, 0, 0, 0.4);

  --font-sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--color-bg); color: var(--color-text); font-family: var(--font-sans); font-size: 14px; line-height: 1.45; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--color-accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ========== Chrome ========== */
.chrome {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
}
.chrome__inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 32px;
}
.brand {
  font-weight: 600;
  letter-spacing: 0.1px;
}
.tabs {
  display: flex;
  gap: 4px;
}
.tab {
  padding: 6px 12px;
  border-radius: var(--radius);
  color: var(--color-text-muted);
  font-weight: 500;
  border: 1px solid transparent;
}
.tab:hover { color: var(--color-text); text-decoration: none; background: var(--color-surface); }
.tab.is-active {
  color: var(--color-accent);
  border-color: var(--color-accent);
  background: transparent;
}

.view {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
  min-height: calc(100vh - 260px);
}

.app-footer {
  border-top: 1px solid var(--color-border);
  margin-top: 48px;
  background: var(--color-bg);
}
.app-footer__inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 18px 24px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  align-items: baseline;
  color: var(--color-text-dim);
  font-size: 11.5px;
  letter-spacing: 0.3px;
}
.app-footer__version {
  margin-left: auto;
  color: var(--color-accent);
  font-weight: 500;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  font-size: 11px;
}
@media (max-width: 600px) {
  .app-footer__version { margin-left: 0; }
}

/* ========== Common form elements ========== */
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { color: var(--color-text-muted); font-size: 12px; font-weight: 500; letter-spacing: 0.2px; }
.field .required { color: var(--color-accent); }
.input, .select, .textarea {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 8px 12px;
  color: var(--color-text);
  outline: none;
  transition: border-color 120ms ease, background 120ms ease;
  width: 100%;
}
.input:focus, .select:focus, .textarea:focus { border-color: var(--color-accent); }
.textarea { min-height: 84px; resize: vertical; }
.input.is-invalid, .select.is-invalid, .textarea.is-invalid { border-color: var(--color-danger); }
.field.is-dirty .input,
.field.is-dirty .select,
.field.is-dirty .textarea { border-color: var(--color-accent); }
.hint { color: var(--color-text-dim); font-size: 12px; }
.error-text { color: var(--color-danger); font-size: 12px; }

.authors-input { display: flex; flex-direction: column; gap: 8px; }
.authors-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: start;
}
.authors-row:has(> .authors-row__field:only-child) { grid-template-columns: 1fr; }
.authors-row__field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.authors-row__remove {
  align-self: start;
  padding: 6px 10px;
  font-size: 16px;
  line-height: 1;
  color: var(--color-text-dim);
}
.authors-row__remove:hover { color: var(--color-danger); border-color: var(--color-danger); }
.authors-actions { display: flex; align-items: center; margin-top: 2px; }
.select--sm { padding: 6px 10px; font-size: 12px; width: auto; min-width: 130px; }
.detail-autores { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }

.author-input { display: flex; flex-direction: column; gap: 4px; }
.author-hint {
  font-size: 11.5px;
  line-height: 1.4;
  min-height: 14px;
  color: var(--color-text-dim);
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.author-hint--info { color: var(--color-accent); }
.author-hint--warn { color: #d9c07a; font-family: var(--font-sans); }
.author-hint--err { color: var(--color-danger); font-family: var(--font-sans); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--color-border-strong);
  background: transparent;
  color: var(--color-text);
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
  font-weight: 500;
}
.btn:hover { background: var(--color-surface); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn--primary { border-color: var(--color-accent); color: var(--color-accent); }
.btn--primary:hover { background: var(--color-accent-100); }
.btn--danger { border-color: var(--color-danger); color: var(--color-danger); }
.btn--danger:hover { background: var(--color-danger-bg); }
.btn--ghost { border-color: transparent; color: var(--color-text-muted); }
.btn--ghost:hover { color: var(--color-text); background: var(--color-surface); }
.btn--sm { padding: 4px 10px; font-size: 12px; }

/* ========== Chips ========== */
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  font-size: 12px;
  color: var(--color-text);
}
.chip--accent { background: var(--color-accent-100); border-color: var(--color-accent); color: var(--color-accent); }
.chip--dashed { border-style: dashed; color: var(--color-text-muted); background: transparent; }
.chip__x { cursor: pointer; color: var(--color-text-dim); font-size: 14px; line-height: 1; }
.chip__x:hover { color: var(--color-text); }
.chip-input {
  display: flex; gap: 6px; flex-wrap: wrap; align-items: center;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 6px 8px;
  min-height: 38px;
}
.chip-input:focus-within { border-color: var(--color-accent); }
.chip-input input {
  background: transparent; border: none; outline: none; flex: 1;
  min-width: 100px; padding: 2px 4px; color: var(--color-text);
}
.autocomplete { position: relative; }
.autocomplete__list {
  position: absolute; top: 100%; left: 0; right: 0;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  margin-top: 4px;
  z-index: 5;
  max-height: 180px;
  overflow-y: auto;
  box-shadow: var(--shadow-2);
}
.autocomplete__item {
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
}
.autocomplete__item:hover, .autocomplete__item.is-active { background: var(--color-accent-100); color: var(--color-accent); }

/* ========== Catalogar (1b split view) ========== */
.catalog {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 24px;
  align-items: start;
}
.preview {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  position: sticky; top: 76px;
}
.preview__pane {
  aspect-ratio: 1 / 1.29;
  background: var(--color-surface-2);
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  color: var(--color-text-dim);
  font-size: 12px;
  padding: 16px;
  text-align: center;
}
.preview__pane.is-drop {
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease;
}
.preview__pane.is-drop:hover, .preview__pane.is-dragover {
  background: var(--color-accent-100);
  border-color: var(--color-accent);
  color: var(--color-accent);
}
.preview__meta { font-size: 12px; color: var(--color-text-muted); line-height: 1.5; }
.preview__meta strong { color: var(--color-text); font-weight: 500; }
.pager { display: flex; align-items: center; justify-content: center; gap: 12px; font-size: 12px; color: var(--color-text-muted); }
.pager button {
  background: transparent; border: 1px solid var(--color-border); border-radius: var(--radius-sm);
  color: var(--color-text-muted); width: 24px; height: 24px; cursor: pointer;
}
.pager button:hover { color: var(--color-text); border-color: var(--color-border-strong); }

.form-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
}
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px 20px;
}
.form-grid .field.full { grid-column: 1 / -1; }
.section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--color-text-dim);
  margin: 8px 0 4px;
  grid-column: 1 / -1;
}

.action-bar {
  position: sticky;
  bottom: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 20px;
  box-shadow: var(--shadow-2);
}
.action-bar__lines {
  display: flex; flex-direction: column; gap: 4px; min-width: 0; flex: 1;
}
.action-bar__name, .action-bar__folio {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12.5px;
  color: var(--color-text-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.action-bar__folio strong { color: var(--color-accent); font-weight: 500; letter-spacing: 0.4px; }
.action-bar__name strong { color: var(--color-text); font-weight: 500; }
.action-bar__actions { display: flex; gap: 10px; flex-shrink: 0; }

.folio-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 10px 14px;
  margin-bottom: 20px;
}
.folio-bar__label { color: var(--color-text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: 1px; }
.folio-bar__code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 14px;
  letter-spacing: 0.6px;
  color: var(--color-accent);
  background: var(--color-accent-100);
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-accent);
  flex: 1;
  min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.detail-folio {
  display: inline-block;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11px;
  color: var(--color-accent);
  background: var(--color-accent-100);
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  letter-spacing: 0.4px;
  margin-bottom: 6px;
}

/* ========== Buscar (1f dense table + detail) ========== */
.search-layout {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 20px;
  align-items: start;
}
.search-main { min-width: 0; }
.search-bar {
  display: flex; gap: 10px; align-items: center; margin-bottom: 12px;
}
.search-bar .input { flex: 1; }
.filter-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 12px;
}
.results-info {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 4px; color: var(--color-text-muted); font-size: 12px;
}
.results-info select { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-sm); padding: 3px 8px; font-size: 12px; }
.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.table th, .table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  font-size: 13px;
}
.table th {
  color: var(--color-text-muted);
  font-weight: 500;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--color-surface-2);
}
.table tbody tr { cursor: pointer; transition: background 100ms ease; }
.table tbody tr:hover { background: var(--color-surface-2); }
.table tbody tr.is-selected { background: var(--color-accent-100); }
.table tbody tr.is-selected td:first-child { box-shadow: inset 3px 0 0 var(--color-accent); }
.table tbody tr:last-child td { border-bottom: none; }
.col-title { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.col-year { width: 70px; color: var(--color-text-muted); }
.col-tags .chip { font-size: 11px; padding: 1px 8px; }

.detail-panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 16px;
  position: sticky;
  top: 76px;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
}
.detail-cover {
  aspect-ratio: 3 / 4;
  background: var(--color-surface-2);
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius);
  margin-bottom: 12px;
  display: flex; align-items: center; justify-content: center;
  color: var(--color-text-dim); font-size: 11px;
}
.detail-panel h3 { margin: 0 0 4px; font-size: 15px; }
.detail-panel .metaline { color: var(--color-text-muted); font-size: 12px; margin-bottom: 12px; }
.detail-panel dl { display: grid; grid-template-columns: 80px 1fr; gap: 4px 12px; font-size: 12px; margin: 0 0 12px; }
.detail-panel dt { color: var(--color-text-muted); }
.detail-panel dd { margin: 0; color: var(--color-text); }
.detail-panel .resumen { font-size: 12.5px; line-height: 1.5; color: var(--color-text); margin: 8px 0 12px; }
.detail-actions { display: flex; gap: 8px; margin-top: 12px; }
.detail-empty { text-align: center; color: var(--color-text-dim); font-size: 13px; padding: 40px 0; }

/* ========== Estadísticas (1h narrative) ========== */
.stats { max-width: 720px; margin: 0 auto; }
.stats__hero {
  text-align: left;
  padding: 32px 0 20px;
}
.stats__number {
  font-size: 96px;
  font-weight: 300;
  color: var(--color-accent);
  line-height: 1;
  letter-spacing: -3px;
}
.stats__caption { font-size: 14px; color: var(--color-text-muted); margin-top: 12px; }
.stats__section { padding: 32px 0; border-top: 1px solid var(--color-border); }
.stats__section h2 { font-size: 14px; text-transform: uppercase; letter-spacing: 1.5px; color: var(--color-text-muted); margin: 0 0 20px; font-weight: 500; }
.hist { display: flex; align-items: flex-end; gap: 4px; height: 120px; }
.hist__bar {
  flex: 1;
  background: var(--color-accent-200);
  border-radius: 2px;
  min-height: 2px;
  position: relative;
  transition: background 120ms ease;
}
.hist__bar:hover { background: var(--color-accent); }
.hist__bar[data-max="true"] { background: var(--color-accent); }
.hist__labels { display: flex; gap: 4px; margin-top: 8px; }
.hist__labels span { flex: 1; text-align: center; font-size: 11px; color: var(--color-text-dim); }
.stats__interp { font-size: 13px; color: var(--color-text-muted); margin-top: 16px; line-height: 1.6; }
.tag-cloud { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.tag-cloud .chip { border-color: var(--color-accent); color: var(--color-accent); background: var(--color-accent-100); }
.quality-note {
  padding: 20px 0;
  color: var(--color-text-dim);
  font-size: 12px;
  border-top: 1px solid var(--color-border);
  margin-top: 24px;
}

/* ========== Editar (2a full ficha) ========== */
.edit-layout {
  display: grid;
  grid-template-columns: 280px 1fr 260px;
  gap: 24px;
  align-items: start;
}
.crumbs {
  display: flex; justify-content: space-between; align-items: baseline;
  margin-bottom: 20px; font-size: 12px; color: var(--color-text-muted);
}
.crumbs a { color: var(--color-text-muted); }
.crumbs a:hover { color: var(--color-accent); }
.file-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 16px;
  position: sticky;
  top: 76px;
}
.file-card__cover {
  aspect-ratio: 3 / 4;
  background: var(--color-surface-2);
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  color: var(--color-text-dim); font-size: 11px;
  margin-bottom: 12px;
}
.file-card__meta { font-size: 12px; color: var(--color-text-muted); line-height: 1.6; margin-bottom: 12px; }
.file-card__meta strong { color: var(--color-text); font-weight: 500; }
.file-card__actions { display: flex; flex-direction: column; gap: 6px; }
.rename-toggle {
  display: flex; align-items: flex-start; gap: 8px; margin: 20px 0;
  padding: 12px; background: var(--color-surface-2); border-radius: var(--radius); border: 1px solid var(--color-border);
}
.rename-toggle input { margin-top: 3px; }
.rename-toggle label { font-size: 13px; cursor: pointer; }
.rename-toggle .preview-name { display: block; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; color: var(--color-text-muted); margin-top: 6px; word-break: break-all; }
.edit-actions {
  display: flex; gap: 10px; padding-top: 20px; border-top: 1px solid var(--color-border);
  margin-top: 20px; justify-content: space-between; align-items: center;
}
.edit-actions__group { display: flex; gap: 10px; }
.history {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 16px;
  position: sticky;
  top: 76px;
}
.history h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 1.5px; color: var(--color-text-muted); margin: 0 0 12px; font-weight: 500; }
.history__list { list-style: none; padding: 0; margin: 0; font-size: 12px; }
.history__list li { padding: 8px 0; border-bottom: 1px solid var(--color-border); color: var(--color-text); }
.history__list li:last-child { border-bottom: none; }
.history__date { color: var(--color-text-dim); display: block; font-size: 11px; margin-bottom: 2px; }
.history__revert { margin-top: 12px; }

/* ========== Modal ========== */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 50; padding: 20px;
  animation: fadeIn 120ms ease;
}
.modal {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 24px;
  max-width: 460px;
  width: 100%;
  box-shadow: var(--shadow-2);
  animation: pop 160ms ease;
}
.modal h2 { margin: 0 0 12px; font-size: 17px; }
.modal p { margin: 0 0 8px; color: var(--color-text-muted); font-size: 13.5px; line-height: 1.5; }
.modal .filename { font-family: ui-monospace, "SF Mono", Menlo, monospace; color: var(--color-text); background: var(--color-surface-2); padding: 2px 6px; border-radius: 4px; font-size: 12.5px; }
.modal .warn { color: var(--color-danger); font-size: 12.5px; margin-top: 12px; }
.modal__actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { transform: scale(0.96); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ========== Toasts ========== */
.toasts { position: fixed; bottom: 20px; right: 20px; z-index: 40; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
  box-shadow: var(--shadow-2);
  min-width: 220px;
  animation: slideIn 160ms ease;
}
.toast--success { border-color: var(--color-success); color: var(--color-success); }
.toast--error { border-color: var(--color-danger); color: var(--color-danger); }
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ========== Empty state ========== */
.empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--color-text-muted);
}
.empty h2 { color: var(--color-text); margin: 0 0 8px; font-size: 18px; }
.empty p { margin: 0 0 16px; font-size: 13px; }

/* ========== Responsive (<= 900px collapse) ========== */
@media (max-width: 900px) {
  .catalog, .search-layout, .edit-layout {
    grid-template-columns: 1fr;
  }
  .filter-row { grid-template-columns: 1fr 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .preview, .detail-panel, .file-card, .history { position: static; max-height: none; }
  .chrome__inner { padding: 12px 16px; gap: 16px; }
  .view { padding: 16px; }
}
