:root {
  --bg: #f3f4f0;
  --surface: #ffffff;
  --surface-2: #e9ece4;
  --ink: #1c211b;
  --ink-2: #4c5548;
  --line: #d3d8cc;
  --brand: #2f4a35;
  --brand-ink: #ffffff;
  --accent: #b8862b;
  --ok: #2e7d32;   --ok-bg: #dcefdc;
  --warn: #8a6100; --warn-bg: #fbefc9;
  --bad: #b3261e;  --bad-bg: #f9dedc;
  --mute-bg: #e4e6e1;
  --n-bajo: #c8e6c9; --n-normal: #4caf50; --n-alto: #ffb74d; --n-muyalto: #e57373; --n-nr: #9e9e9e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121511; --surface: #1b1f1a; --surface-2: #252a23; --ink: #e8ebe4; --ink-2: #a9b2a3;
    --line: #333a30; --brand: #3d5e44; --accent: #d4a24a;
    --ok: #81c784; --ok-bg: #1f3320; --warn: #f0c35a; --warn-bg: #3a3014; --bad: #ef9a9a; --bad-bg: #3d1d1b;
    --mute-bg: #2a2e28; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121511; --surface: #1b1f1a; --surface-2: #252a23; --ink: #e8ebe4; --ink-2: #a9b2a3;
  --line: #333a30; --brand: #3d5e44; --accent: #d4a24a;
  --ok: #81c784; --ok-bg: #1f3320; --warn: #f0c35a; --warn-bg: #3a3014; --bad: #ef9a9a; --bad-bg: #3d1d1b;
  --mute-bg: #2a2e28; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  padding-bottom: calc(68px + env(safe-area-inset-bottom));
}
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }

/* ---- barra superior ---- */
.top {
  position: sticky; top: 0; z-index: 20;
  background: var(--brand); color: var(--brand-ink);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  display: flex; align-items: center; gap: 10px;
}
.top .marca { font-weight: 800; letter-spacing: .14em; font-size: 15px; }
.top .titulo { font-size: 13px; opacity: .85; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .estado { font-size: 11px; padding: 3px 8px; border-radius: 999px; background: rgba(255,255,255,.16); }
.top .estado.off { background: var(--accent); color: #1c1c1c; }
.top button.atras { background: none; border: 0; color: inherit; font-size: 22px; padding: 0 6px 0 0; cursor: pointer; }

main { max-width: 860px; margin: 0 auto; padding: 14px 16px 24px; }

/* ---- navegación inferior ---- */
nav.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
nav.tabs a {
  text-decoration: none; text-align: center; padding: 8px 2px 9px; font-size: 11px; color: var(--ink-2);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
nav.tabs a svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.8; }
nav.tabs a.on { color: var(--brand); font-weight: 700; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) nav.tabs a.on { color: var(--accent); } }
:root[data-theme="dark"] nav.tabs a.on { color: var(--accent); }

/* ---- bloques ---- */
h1 { font-size: 20px; margin: 4px 0 12px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-2); margin: 20px 0 8px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px; }
.card.flat { box-shadow: none; border: 1px solid var(--line); }
.muted { color: var(--ink-2); }
.small { font-size: 12.5px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 520px) { .grid2 { grid-template-columns: 1fr; } }
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.kpi { background: var(--surface); border-radius: var(--radius); padding: 10px; box-shadow: var(--shadow); }
.kpi b { display: block; font-size: 24px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi span { font-size: 11.5px; color: var(--ink-2); }

/* ---- listas ---- */
.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 12px; }
.item { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-top: 1px solid var(--line); text-decoration: none; color: inherit; cursor: pointer; }
.item:first-child { border-top: 0; }
.item:active { background: var(--surface-2); }
.item .main { flex: 1; min-width: 0; }
.item .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .s { font-size: 12.5px; color: var(--ink-2); }
.grupo-h { padding: 8px 14px; font-size: 12px; font-weight: 700; background: var(--surface-2); color: var(--ink-2); letter-spacing: .04em; }

/* ---- chips ---- */
.chips { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.chip { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 6px; white-space: nowrap; background: var(--mute-bg); color: var(--ink-2); }
.chip.ok { background: var(--ok-bg); color: var(--ok); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.chip.bad { background: var(--bad-bg); color: var(--bad); }
.chip.brand { background: var(--brand); color: var(--brand-ink); }
.nivel { display: inline-block; padding: 3px 9px; border-radius: 6px; font-weight: 800; font-size: 12px; color: #1a1a1a; }
.nivel.BAJO { background: var(--n-bajo); }
.nivel.NORMAL { background: var(--n-normal); color: #fff; }
.nivel.ALTO { background: var(--n-alto); }
.nivel.MUYALTO { background: var(--n-muyalto); }
.nivel.NR { background: var(--n-nr); color: #fff; }
.nivel.INC { background: var(--mute-bg); color: var(--ink-2); }

/* ---- formularios ---- */
label.f { display: block; margin-bottom: 10px; }
label.f > span { display: block; font-size: 12px; font-weight: 600; color: var(--ink-2); margin-bottom: 4px; }
input[type=text], input[type=password], input[type=date], input[type=number], input[type=time], select, textarea {
  width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); min-height: 44px; font-size: 16px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.check { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.check input { width: 22px; height: 22px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 700; cursor: pointer; text-decoration: none;
}
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.accent { background: var(--accent); color: #1a1a1a; border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; }
.btn.sm { min-height: 34px; padding: 0 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.acciones { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* ---- ORM ---- */
.factor { padding: 12px 14px; border-top: 1px solid var(--line); }
.factor:first-child { border-top: 0; }
.factor .nom { font-weight: 600; font-size: 14px; margin-bottom: 6px; display: flex; gap: 8px; align-items: baseline; }
.factor .nom .v { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 800; }
.factor .sug { font-size: 12.5px; color: var(--ink-2); margin: 6px 0; }
.factor .alerta { font-size: 12.5px; font-weight: 700; color: var(--bad); margin-top: 6px; }
.factor.auto .nom::after { content: "auto"; font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 4px; background: var(--surface-2); color: var(--ink-2); }
.factor.pend { box-shadow: inset 4px 0 0 var(--accent); }
.factor.crit { box-shadow: inset 4px 0 0 var(--bad); }
.barra-orm {
  position: fixed; left: 0; right: 0; bottom: calc(58px + env(safe-area-inset-bottom)); z-index: 25;
  background: var(--surface); border-top: 1px solid var(--line); box-shadow: 0 -4px 12px rgba(0,0,0,.08);
  padding: 8px 16px;
}
.barra-orm .in { max-width: 860px; margin: 0 auto; display: flex; gap: 10px; align-items: center; }
.barra-orm .tot { font-variant-numeric: tabular-nums; font-size: 13px; }
.barra-orm .tot b { font-size: 18px; }
.barra-orm .aut { font-size: 11.5px; color: var(--ink-2); }
.con-barra { padding-bottom: 96px; }

canvas.firma { width: 100%; height: 150px; border: 2px dashed var(--line); border-radius: 10px; background: #fff; touch-action: none; }

/* ---- tablas compactas ---- */
.tabla { width: 100%; border-collapse: collapse; font-size: 13px; }
.tabla th, .tabla td { padding: 7px 6px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.tabla th { font-size: 11.5px; color: var(--ink-2); font-weight: 700; }
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.aviso { padding: 10px 12px; border-radius: 10px; font-size: 13.5px; margin-bottom: 10px; }
.aviso.warn { background: var(--warn-bg); color: var(--warn); }
.aviso.bad { background: var(--bad-bg); color: var(--bad); }
.aviso.ok { background: var(--ok-bg); color: var(--ok); }
.aviso.info { background: var(--surface-2); color: var(--ink); }

.login { max-width: 380px; margin: 9vh auto 0; }
.login .logo { text-align: center; margin-bottom: 18px; }
.login .logo b { display: block; font-size: 30px; letter-spacing: .3em; color: var(--brand); }
.login .logo span { font-size: 12.5px; color: var(--ink-2); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(80px + env(safe-area-inset-bottom));
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60;
  font-size: 14px; max-width: 90vw; box-shadow: var(--shadow);
}
.empty { text-align: center; color: var(--ink-2); padding: 26px 10px; }
details > summary { cursor: pointer; font-weight: 600; padding: 4px 0; }
.dia { font-size: 10px; display: inline-block; width: 18px; height: 18px; line-height: 18px; text-align: center; border-radius: 4px; margin: 1px; font-weight: 700; }
.dia.APTO { background: var(--ok-bg); color: var(--ok); }
.dia.APTO_CON_CR2 { background: var(--warn-bg); color: var(--warn); }
.dia.NO_APTO { background: var(--bad-bg); color: var(--bad); }
.barra-orm .tot { white-space: nowrap; }
.barra-orm .aut { max-width: 52vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
[hidden] { display: none !important; }
