:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #fcfcfb;
  --surface-2: #f0efeb;
  --border: rgba(11, 11, 11, .10);
  --grid: #e1e0d9;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, .10);
  --band: rgba(12, 163, 12, .10);
  --alto: #d03b3b;
  --bajo: #2a78d6;
  --ok: #0ca30c;
  --warn-bg: #fff4dc;
  --error-bg: #fde8e8;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: rgba(255, 255, 255, .10);
    --grid: #2c2c2a; --text: #ffffff; --text-2: #c3c2b7; --text-3: #8f8e86;
    --accent: #3987e5; --accent-wash: rgba(57, 135, 229, .16); --band: rgba(12, 163, 12, .16);
    --alto: #e66767; --bajo: #3987e5; --warn-bg: #3a3014; --error-bg: #3d1d1d;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: rgba(255, 255, 255, .10);
  --grid: #2c2c2a; --text: #ffffff; --text-2: #c3c2b7; --text-3: #8f8e86;
  --accent: #3987e5; --accent-wash: rgba(57, 135, 229, .16); --band: rgba(12, 163, 12, .16);
  --alto: #e66767; --bajo: #3987e5; --warn-bg: #3a3014; --error-bg: #3d1d1d;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1rem 0 .5rem; }
.muted { color: var(--text-2); }
.small { font-size: .85rem; }
.tnum { font-variant-numeric: tabular-nums; }

/* Layout */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar .in { max-width: 1200px; margin: 0 auto; padding: 0 16px; display: flex; align-items: center; gap: 1.25rem; height: 56px; }
.marca { font-weight: 700; color: var(--text); white-space: nowrap; }
.nav { display: flex; gap: .25rem; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav a { color: var(--text-2); padding: .4rem .7rem; border-radius: 8px; white-space: nowrap; }
.nav a.activo, .nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
main { max-width: 1200px; margin: 0 auto; padding: 24px 16px 64px; }
.cabecera { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-stats { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; min-width: 0; }
.stat .n { font-size: 1.8rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat .l { color: var(--text-2); font-size: .85rem; }

/* Forms */
input, select, textarea, button { font: inherit; color: inherit; }
input[type=text], input[type=password], input[type=date], input[type=search], select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .45rem .6rem; width: 100%;
}
textarea { min-height: 5rem; resize: vertical; }
label { display: block; font-size: .85rem; color: var(--text-2); margin: .6rem 0 .2rem; }
.btn { display: inline-flex; align-items: center; gap: .4rem; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: .45rem .9rem; cursor: pointer; color: var(--text); white-space: nowrap; }
.btn:hover { text-decoration: none; filter: brightness(.97); }
.btn.primario { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.peligro { color: var(--alto); }
.btn.chico { padding: .2rem .55rem; font-size: .85rem; }
.btn[disabled] { opacity: .6; cursor: wait; }
.filtros { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: 1rem; }
.filtros > *, .filtros input, .filtros select { width: auto; }
.filtros input[type=search] { flex: 1; min-width: 180px; }
.acciones { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
form.inline { display: inline; }

/* Flash */
.flash { padding: .6rem .9rem; border-radius: 8px; margin-bottom: 1rem; background: var(--accent-wash); }
.flash.error { background: var(--error-bg); }

/* Tables */
.tabla-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; }
th, td { padding: .5rem .75rem; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { font-weight: 600; font-size: .8rem; color: var(--text-2); background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--surface-2); }

/* Lab flowsheet */
.flowsheet th:first-child, .flowsheet td:first-child { position: sticky; left: 0; background: var(--surface); z-index: 1; min-width: 190px; }
.flowsheet th:first-child { background: var(--surface-2); }
.flowsheet td.cat { background: var(--surface-2); font-weight: 600; font-size: .8rem; color: var(--text-2); }
.flowsheet th.fecha { writing-mode: initial; font-variant-numeric: tabular-nums; }
.v-H { color: var(--alto); font-weight: 600; }
.v-L { color: var(--bajo); font-weight: 600; }
.flecha { font-size: .75em; margin-left: 1px; }

/* Badges */
.badge { display: inline-block; font-size: .75rem; padding: .1rem .5rem; border-radius: 999px; background: var(--surface-2);
  color: var(--text-2); border: 1px solid var(--border); white-space: nowrap; }
.badge.laboratorio { background: var(--accent-wash); color: var(--accent); border-color: transparent; }
.badge.estado-error { background: var(--error-bg); color: var(--alto); }
.badge.estado-procesando, .badge.estado-pendiente, .badge.estado-en_lote { background: var(--warn-bg); }

/* Study detail */
.detalle { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 900px) { .detalle { grid-template-columns: 1fr; } }
.visor { position: sticky; top: 72px; height: calc(100vh - 96px); padding: 0; overflow: hidden; }
.visor iframe, .visor img { width: 100%; height: 100%; border: 0; object-fit: contain; background: var(--surface-2); }
@media (max-width: 900px) { .visor { position: static; height: 70vh; } }
dl.meta { display: grid; grid-template-columns: auto 1fr; gap: .3rem 1rem; margin: 0; }
dl.meta dt { color: var(--text-2); font-size: .85rem; }
dl.meta dd { margin: 0; }
.conclusion { border-left: 3px solid var(--accent); padding: .25rem .9rem; margin: .75rem 0; background: var(--accent-wash); border-radius: 0 8px 8px 0; }
details > summary { cursor: pointer; color: var(--text-2); margin: .5rem 0; }
pre.texto { white-space: pre-wrap; font-size: .75rem; max-height: 400px; overflow: auto; background: var(--surface-2); padding: .75rem; border-radius: 8px; }

/* Timeline */
.anio { font-size: 1.2rem; font-weight: 700; margin: 1.5rem 0 .5rem; }
.timeline { border-left: 2px solid var(--grid); margin-left: 6px; padding-left: 18px; }
.item { position: relative; padding: .5rem 0 .75rem; }
.item::before { content: ""; position: absolute; left: -25px; top: .95rem; width: 10px; height: 10px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--text-3); }
.item.evento::before { background: var(--accent); border-color: var(--accent); }
.item.sugerido { opacity: .8; }
.item .f { font-size: .8rem; color: var(--text-2); font-variant-numeric: tabular-nums; }

/* Chart */
.grafico { position: relative; height: 340px; }
.grafico svg { width: 100%; height: 100%; display: block; overflow: visible; }
.tooltip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: .4rem .6rem; font-size: .8rem; box-shadow: 0 4px 16px rgba(0,0,0,.12); white-space: nowrap; display: none; }
.leyenda { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .8rem; color: var(--text-2); margin-top: .5rem; }
.leyenda i { display: inline-block; width: 14px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }

/* IA */
.respuesta { line-height: 1.6; }
.respuesta table { margin: .5rem 0; font-size: .9rem; }
.respuesta h2, .respuesta h3 { margin-top: 1.25rem; }
.aviso { font-size: .8rem; color: var(--text-2); border-top: 1px solid var(--grid); margin-top: 1rem; padding-top: .5rem; }

/* Login */
.login { max-width: 360px; margin: 12vh auto; }

/* Drop zone */
.drop { border: 2px dashed var(--grid); border-radius: var(--radius); padding: 1.25rem; text-align: center; color: var(--text-2); }
.drop.sobre { border-color: var(--accent); background: var(--accent-wash); }

@media print {
  .topbar, .no-print { display: none !important; }
  body { background: #fff; color: #000; }
  main { padding: 0; }
  .card { border: 0; padding: 0; }
}

/* Certeza de medicamentos */
.badge.cert-documentado { background: rgba(12, 163, 12, .12); color: var(--ok); border-color: transparent; }
.badge.cert-protocolo { background: var(--accent-wash); color: var(--accent); border-color: transparent; }
.badge.cert-estimado { background: var(--warn-bg); }
