:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --ink: #16202e;
  --muted: #637083;
  --line: #e2e7ef;
  --brand: #1d4ed8;
  --brand-ink: #ffffff;
  --brand-soft: #e8efff;
  --accent: #0f766e;
  --warn: #b45309;
  --warn-soft: #fff4e0;
  --error: #b91c1c;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
}
body.noscroll { overflow: hidden; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; }
button { font: inherit; color: inherit; }
a { color: var(--brand); }
code, .mono { font-family: var(--mono); font-size: .92em; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.pre { white-space: pre-wrap; word-break: break-word; }
.spacer { flex: 1; }

/* ---------- barra superior ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px;
  background: #0f1b33; color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  display: grid; place-items: center; width: 38px; height: 38px; border-radius: 9px;
  background: var(--brand); font-weight: 800; font-size: 12px; letter-spacing: .5px;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text small { color: #9fb0cc; }
.fileinfo { flex: 1; min-width: 0; color: #c9d4e6; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fileinfo strong { color: #fff; }
.actions { display: flex; gap: 8px; align-items: center; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line);
  background: #fff; color: var(--ink); cursor: pointer; text-decoration: none; font-weight: 500;
}
.btn:hover { border-color: #b9c3d3; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { background: #1e40af; }
.btn.big { padding: 12px 22px; font-size: 15px; }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.topbar .btn:not(.primary) { background: transparent; color: #fff; border-color: #34466a; }

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 260px;
  background: #fff; color: var(--ink); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.18);
  padding: 6px; display: flex; flex-direction: column;
}
.menu-list button { text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 6px; cursor: pointer; }
.menu-list button:hover { background: var(--brand-soft); }

main { padding-inline: 24px; padding-block: 20px 60px; max-width: 1500px; margin: 0 auto; }

/* ---------- zona de carga ---------- */
.drop { display: grid; place-items: center; min-height: calc(100vh - 140px); }
.dropzone {
  width: min(640px, 100%); text-align: center; padding: 48px 28px;
  background: var(--panel); border: 2px dashed #b8c4d8; border-radius: 18px;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  transition: border-color .15s, background .15s;
}
body.dragging .dropzone { border-color: var(--brand); background: var(--brand-soft); }
body.dragging #viewer { outline: 3px dashed var(--brand); outline-offset: 6px; border-radius: 12px; }
.drop-icon { width: 54px; height: 54px; color: var(--brand); }
.dropzone h1 { font-size: 22px; font-weight: 600; }
.privacy { margin-top: 14px; font-size: 13px; color: var(--accent); }
.status { min-height: 1.5em; margin: 0; color: var(--muted); }
.status.error { color: var(--error); font-weight: 500; }

/* ---------- cabecera del expediente ---------- */
.hero {
  display: grid; grid-template-columns: auto 1fr auto; gap: 20px; align-items: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 16px; box-shadow: var(--shadow);
}
.hero-img { padding: 0; border: 0; background: none; cursor: zoom-in; border-radius: 10px; overflow: hidden; width: 150px; height: 112px; }
.hero-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.eyebrow { margin: 0 0 2px; font-size: 12px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--brand); }
.hero h2 { font-size: 21px; margin-bottom: 10px; }
.hero-chips, .hero-links, .chips { display: flex; flex-wrap: wrap; gap: 6px; }
.hero-links { margin-top: 8px; }
.pill {
  display: inline-flex; gap: 6px; align-items: baseline; padding: 3px 10px; border-radius: 999px;
  background: #eef2f8; font-size: 13px; white-space: nowrap;
}
.pill small { color: var(--muted); font-size: 11px; }
.pill.alt { background: var(--brand-soft); color: #1e3a8a; }
.hero-value { text-align: right; display: flex; flex-direction: column; gap: 2px; padding: 8px 12px; border-left: 1px solid var(--line); }
.hero-value small { color: var(--muted); }
.hero-value strong { font-size: 28px; color: var(--accent); white-space: nowrap; }
.hero-value span { font-size: 12.5px; color: var(--muted); }

/* ---------- pestañas ---------- */
.tabs {
  position: sticky; top: 58px; z-index: 10;
  display: flex; gap: 2px; overflow-x: auto; margin: 16px 0; padding: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  scrollbar-width: thin;
}
.tab {
  flex: none; border: 0; background: none; padding: 8px 12px; border-radius: 8px; cursor: pointer;
  color: var(--muted); font-weight: 500; white-space: nowrap; display: inline-flex; gap: 6px; align-items: center;
}
.tab:hover { background: #f1f4f9; color: var(--ink); }
.tab.active { background: var(--brand); color: #fff; }
.badge {
  display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px; background: #e7ecf4; color: #334155;
  font-size: 11.5px; font-weight: 600; text-align: center; line-height: 18px;
}
.tab.active .badge { background: rgba(255,255,255,.25); color: #fff; }
.badge.zero { opacity: .5; }

.tabpanel { display: flex; flex-direction: column; gap: 16px; }

/* ---------- tarjetas ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; align-items: start; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow);
  min-width: 0;
}
.card > h3 { font-size: 15px; margin-bottom: 12px; }
.card > summary { cursor: pointer; font-weight: 600; }
.card.error { color: var(--error); }
.card h4 { font-size: 13px; margin: 14px 0 6px; color: #334155; }

.kv { display: grid; grid-template-columns: minmax(120px, max-content) 1fr; gap: 6px 16px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.kv + .kv { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }

.memo { margin-top: 10px; }
.memo:first-child { margin-top: 0; }
.memo h4 { margin: 0 0 3px; }
.memo p { margin: 0; }
details.memo > summary { cursor: pointer; color: var(--muted); }
details.memo[open] > summary { margin-bottom: 6px; }

.sources { margin: 12px 0 0; padding-top: 8px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }

.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px;
  border: 1px solid #cdd7e6; background: #f7f9fc; color: #1e3a8a; font-size: 12px; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.chip:hover { background: var(--brand-soft); border-color: #a9bdf2; }
.chip.warn { color: var(--warn); background: var(--warn-soft); border-color: #f5d49a; cursor: default; }
.link { background: none; border: 0; padding: 0; color: var(--brand); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }

.cee { display: inline-flex; align-items: center; gap: 4px; }
.cee b { display: inline-grid; place-items: center; min-width: 26px; height: 22px; padding: 0 6px; border-radius: 4px; color: #fff; text-shadow: 0 1px 1px rgba(0,0,0,.35); }

/* ---------- tablas ---------- */
.table-wrap { overflow-x: auto; margin-top: 4px; }
table.data, table.fields { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; background: #f7f9fc; border-bottom: 1px solid var(--line); padding: 7px 10px; white-space: nowrap; }
table.data td { padding: 7px 10px; border-bottom: 1px solid #edf1f6; vertical-align: top; }
table.data.num td { font-variant-numeric: tabular-nums; }
table.data tr.total td { font-weight: 700; background: #f0fdf9; border-top: 2px solid #a7e3d6; }
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: #f5f8ff; }
table.data tr.clickable.open td { background: var(--brand-soft); }
table.data tr.detail > td { background: #fafbfd; padding: 14px; }
table.testigos td:nth-child(n+3) { white-space: nowrap; }
table.fields th { text-align: left; font-weight: 500; color: var(--muted); width: 34%; padding: 5px 10px 5px 0; border-bottom: 1px solid #edf1f6; vertical-align: top; }
table.fields th small { display: block; font-family: var(--mono); font-size: 10.5px; color: #97a3b6; }
table.fields td { padding: 5px 0; border-bottom: 1px solid #edf1f6; word-break: break-word; }

/* ---------- unidades ---------- */
.unit { display: flex; flex-direction: column; gap: 16px; }
.unit + .unit { margin-top: 18px; padding-top: 22px; border-top: 3px solid var(--line); }
.unit-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.unit-head h2 { font-size: 19px; margin-right: 4px; }
.unit-value { font-size: 20px; color: var(--accent); }
.address { margin: 0 0 12px; font-weight: 500; }

.stats { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 10px; }
.stat { background: #f7f9fc; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; display: flex; flex-direction: column; }
.stat small { color: var(--muted); font-size: 11.5px; }
.stat strong { font-size: 15px; }

/* ---------- fotos ---------- */
.gallery-group h3 { font-size: 15px; margin: 4px 0 10px; display: flex; gap: 8px; align-items: center; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.thumb {
  position: relative; padding: 0; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #fff;
  cursor: zoom-in; text-align: left; box-shadow: var(--shadow); display: flex; flex-direction: column;
}
.thumb img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; background: #e5e9f0; }
.thumb span { padding: 6px 10px; font-size: 12.5px; font-weight: 500; }
.thumb em { position: absolute; top: 8px; left: 8px; background: var(--accent); color: #fff; font-style: normal; font-size: 11px; padding: 1px 7px; border-radius: 999px; }
.thumb:hover { border-color: #a9bdf2; }

.lightbox { position: fixed; inset: 0; z-index: 100; background: rgba(8, 12, 22, .92); display: grid; grid-template-columns: 64px 1fr 64px; align-items: center; }
.lightbox figure { margin: 0; display: flex; flex-direction: column; align-items: center; gap: 10px; max-height: 100vh; padding: 20px 0; min-width: 0; }
.lightbox img { max-width: 100%; max-height: calc(100vh - 130px); object-fit: contain; border-radius: 6px; box-shadow: 0 10px 40px rgba(0,0,0,.5); }
.lightbox figcaption { color: #fff; text-align: center; }
.lightbox figcaption span { color: #9fb0cc; }
.lb-meta { color: #c9d4e6; font-size: 12.5px; margin-top: 4px; display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; align-items: center; }
.lb-nav, .lb-close { background: rgba(255,255,255,.08); border: 0; color: #fff; cursor: pointer; border-radius: 50%; }
.lb-nav { width: 48px; height: 48px; font-size: 30px; line-height: 1; justify-self: center; }
.lb-nav:hover, .lb-close:hover { background: rgba(255,255,255,.2); }
.lb-close { position: absolute; top: 14px; right: 16px; width: 40px; height: 40px; font-size: 26px; }

/* ---------- documentos ---------- */
.docs { display: grid; grid-template-columns: 300px 1fr; gap: 16px; align-items: start; }
.doc-list { display: flex; flex-direction: column; gap: 6px; position: sticky; top: 120px; }
.doc {
  display: flex; gap: 10px; align-items: center; text-align: left; padding: 10px; cursor: pointer;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
}
.doc:hover { border-color: #a9bdf2; }
.doc.active { border-color: var(--brand); background: var(--brand-soft); }
.doc-ext { flex: none; width: 38px; height: 38px; border-radius: 8px; display: grid; place-items: center; background: #fee2e2; color: #b91c1c; font-size: 11px; font-weight: 700; }
.doc-name { display: flex; flex-direction: column; min-width: 0; }
.doc-name small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-viewer { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); min-width: 0; }
.doc-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.doc-bar small { display: block; color: var(--muted); }
.doc-viewer iframe { display: block; width: 100%; height: calc(100vh - 230px); min-height: 520px; border: 0; background: #525659; }
.doc-text { margin: 0; padding: 14px; max-height: 70vh; overflow: auto; }
.pad { padding: 20px; }

/* ---------- árbol estructurado ---------- */
details.tree { border: 1px solid var(--line); border-radius: 10px; margin: 6px 0; background: #fff; }
details.tree > summary { cursor: pointer; padding: 8px 12px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; list-style: none; }
details.tree > summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
details.tree[open] > summary::before { transform: rotate(90deg); }
details.tree[open] > summary { border-bottom: 1px solid var(--line); background: #f9fbfe; border-radius: 10px 10px 0 0; }
details.tree > summary::-webkit-details-marker { display: none; }
.tb { font-family: var(--mono); font-weight: 700; color: #1e3a8a; }
.idx { font-family: var(--mono); color: var(--muted); font-size: 12px; }
.tree-body { padding: 10px 12px 12px 22px; }
.tree-group { margin-top: 10px; padding-left: 12px; border-left: 2px solid #dbe4f3; }
.tree-rel { margin: 4px 0; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12.5px; }
.tree-ref { margin: 4px 0; font-size: 12.5px; }
.tree-rec + .tree-rec { margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--line); }
.dir { font-size: 11px; font-weight: 600; padding: 0 7px; border-radius: 999px; }
.dir.out { background: #e0f2fe; color: #075985; }
.dir.in { background: #dcfce7; color: #166534; }

.ttree, .ttree ul { list-style: none; margin: 0; padding-left: 18px; }
.ttree { padding-left: 0; }
.ttree li { position: relative; padding: 3px 0; }
.ttree ul { border-left: 1px dashed #c6d2e4; margin-left: 6px; }
.ttree li > .dir { margin-right: 6px; }
.ttree .badge { margin-left: 6px; }

/* ---------- explorador de tablas ---------- */
.explorer { display: grid; grid-template-columns: 260px 1fr; gap: 16px; align-items: start; }
.explorer aside { position: sticky; top: 120px; display: flex; flex-direction: column; gap: 8px; max-height: calc(100vh - 140px); }
.input { width: 100%; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; font: inherit; background: #fff; }
.input:focus { outline: 2px solid #bfd0fb; border-color: var(--brand); }
.check { font-size: 12.5px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.tlist { overflow-y: auto; display: flex; flex-direction: column; gap: 1px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 4px; }
.titem { display: flex; justify-content: space-between; gap: 8px; border: 0; background: none; text-align: left; padding: 5px 8px; border-radius: 6px; cursor: pointer; font-family: var(--mono); font-size: 12.5px; }
.titem:hover { background: #f1f4f9; }
.titem.active { background: var(--brand); color: #fff; }
.titem.active .badge { background: rgba(255,255,255,.25); color: #fff; }
.tdetail { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); min-width: 0; scroll-margin-top: 120px; }
.thead h3 { font-family: var(--mono); font-size: 18px; }
.thead p { margin: 2px 0 10px; }
details.schema { margin: 10px 0; }
details.schema > summary { cursor: pointer; color: var(--brand); }
details.rec { border: 1px solid var(--line); border-radius: 10px; margin: 8px 0; scroll-margin-top: 130px; transition: box-shadow .3s; }
details.rec > summary { cursor: pointer; padding: 8px 12px; }
details.rec[open] > summary { border-bottom: 1px solid var(--line); background: #f9fbfe; border-radius: 10px 10px 0 0; }
details.rec.flash { box-shadow: 0 0 0 3px #93b4fa; }
.recbody { padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.recbody > div { width: 100%; }
.incoming { margin-top: 10px; }
.incoming p { margin: 4px 0; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.results { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; margin-bottom: 12px; max-height: 320px; overflow-y: auto; }
.result { display: flex; gap: 8px; align-items: baseline; width: 100%; text-align: left; border: 0; background: none; padding: 5px 6px; border-radius: 6px; cursor: pointer; font-size: 12.5px; }
.result:hover { background: var(--brand-soft); }
.result strong { font-family: var(--mono); white-space: nowrap; }
.result code { color: var(--muted); }
.result span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 120; background: #0f1b33; color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.25); max-width: calc(100vw - 32px); }
.toast.error { background: var(--error); }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  main { padding-inline: 16px; }
  .topbar { padding: 10px 16px; }
  .fileinfo { order: 3; flex-basis: 100%; }
  .tabs { top: 0; position: relative; }
  .hero { grid-template-columns: 1fr; }
  .hero-img { width: 100%; height: 180px; }
  .hero-value { text-align: left; border-left: 0; border-top: 1px solid var(--line); padding: 10px 0 0; }
  .docs, .explorer { grid-template-columns: 1fr; }
  .doc-list, .explorer aside { position: static; max-height: none; }
  .tlist { max-height: 240px; }
  .grid { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr; gap: 0 0; }
  .kv dd { margin-bottom: 6px; }
  .lightbox { grid-template-columns: 44px 1fr 44px; }
  .lb-nav { width: 38px; height: 38px; font-size: 24px; }
}
