:root {
  --paper: #eef2f4;
  --surface: #ffffff;
  --ink: #17222b;
  --muted: #566672;
  --line: #d3dbe1;
  --field: #f7f9fa;
  --brand: #0f5e78;
  --brand-ink: #ffffff;
  --crit: #c4271e;
  --high: #dd6500;
  --med: #e2b203;
  --low: #2f7d4a;
  --pend: #8a99a6;
  --on-med: #1d1a05;
  --focus: #1f8fb5;
  --radius: 6px;
  --cond: "IBM Plex Sans Condensed", "Arial Narrow", system-ui, sans-serif;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #10181e; --surface: #18232b; --ink: #e5ecf0; --muted: #9caebb; --line: #2b3a45; --field: #1f2c35;
    --brand: #3fa3c6; --brand-ink: #06202a; --pend: #6f8190; --focus: #6cc3e0; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #10181e; --surface: #18232b; --ink: #e5ecf0; --muted: #9caebb; --line: #2b3a45; --field: #1f2c35;
  --brand: #3fa3c6; --brand-ink: #06202a; --pend: #6f8190; --focus: #6cc3e0; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--paper); color: var(--ink); font: 15px/1.5 var(--sans); }
h2, h3 { font-family: var(--cond); letter-spacing: .005em; margin: 0; }
h2 { font-size: 1.3rem; font-weight: 600; display: flex; align-items: center; gap: .55rem; }
.step { display: inline-grid; place-items: center; width: 1.6rem; height: 1.6rem; border-radius: 50%; background: var(--ink); color: var(--paper); font-size: .9rem; font-weight: 700; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* Encabezado */
.top { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; padding: .9rem clamp(16px, 4vw, 40px); background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { display: flex; gap: .7rem; align-items: center; }
.brand .mark { width: 36px; height: 36px; flex: none; }
.brand strong { display: block; font: 700 1.25rem/1.1 var(--cond); }
.brand span { color: var(--muted); font-size: .82rem; }
.engine { font-size: .82rem; color: var(--muted); padding: .3rem .7rem; border: 1px solid var(--line); border-radius: 999px; }
.engine.ok::before, .engine.demo::before, .engine.off::before { content: ""; display: inline-block; width: .55rem; height: .55rem; border-radius: 50%; margin-right: .45rem; vertical-align: .05rem; }
.engine.ok::before { background: var(--low); }
.engine.demo::before { background: var(--med); }
.engine.off::before { background: var(--crit); }

/* Estructura */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 20px; padding: 20px clamp(16px, 4vw, 40px) 120px; max-width: 1280px; margin: 0 auto; align-items: start; }
.work { display: grid; gap: 20px; min-width: 0; }
.block, .panel, .report { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 20px; }
.block-head, .report-head { display: flex; justify-content: space-between; align-items: center; gap: .8rem; flex-wrap: wrap; margin-bottom: 1rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.hint { color: var(--muted); font-size: .86rem; margin: -.4rem 0 1rem; }

/* Campos */
.grid { display: grid; gap: 12px; }
.g1 { grid-template-columns: 1fr; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
label { display: grid; gap: 4px; font-size: .8rem; font-weight: 500; color: var(--muted); }
input, select, textarea { font: inherit; font-size: .93rem; color: var(--ink); background: var(--field); border: 1px solid var(--line); border-radius: var(--radius); padding: .5rem .6rem; width: 100%; min-width: 0; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent); }
details.more { margin-top: 12px; }
details.more > summary { cursor: pointer; font-size: .86rem; color: var(--brand); font-weight: 500; padding: .2rem 0; }
details.more[open] > summary { margin-bottom: 10px; }

/* Botones */
.btn { font: 600 .88rem/1 var(--sans); padding: .62rem .9rem; border-radius: var(--radius); border: 1px solid var(--brand); background: var(--brand); color: var(--brand-ink); cursor: pointer; white-space: nowrap; }
.btn.ghost { background: transparent; color: var(--brand); border-color: var(--line); }
.btn.ghost:hover { border-color: var(--brand); }
.btn.primary { width: 100%; padding: .9rem; font-size: 1rem; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.file { position: relative; overflow: hidden; display: inline-flex; align-items: center; color: var(--brand); font-size: .88rem; font-weight: 600; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.btn.file:focus-within { outline: 3px solid var(--focus); outline-offset: 2px; }
.icon { border: 0; background: transparent; color: var(--muted); font-size: 1.5rem; line-height: 1; width: 2rem; height: 2rem; border-radius: var(--radius); cursor: pointer; }
.icon:hover { background: var(--field); color: var(--crit); }

/* Hallazgos */
.findings { list-style: none; margin: 0; padding: 0; display: grid; gap: 14px; }
.finding { border: 1px solid var(--line); border-left: 6px solid var(--lv, var(--pend)); border-radius: 8px; padding: 14px; display: grid; gap: 12px; background: var(--surface); }
.f-head { display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; gap: 10px; align-items: center; }
.f-num { font: 700 1rem var(--cond); color: var(--muted); min-width: 2.4rem; }
.f-name { font-weight: 600; font-size: 1rem; }
.empty { border: 1.5px dashed var(--line); border-radius: 8px; padding: 22px; text-align: center; color: var(--muted); }
.empty p { margin: .2rem 0; }

/* Etiqueta de riesgo: el elemento protagonista */
.tag { display: grid; grid-template-columns: auto; justify-items: center; min-width: 5.6rem; padding: .3rem .6rem; border-radius: var(--radius); background: var(--lv, var(--pend)); color: var(--on-lv, #fff); text-align: center; }
.tag-rpi { font: 700 1.45rem/1 var(--cond); font-variant-numeric: tabular-nums; }
.tag-lv { font: 600 .72rem/1.2 var(--cond); letter-spacing: .02em; }
.lv-CRITICO { --lv: var(--crit); }
.lv-ALTO { --lv: var(--high); }
.lv-MEDIO { --lv: var(--med); --on-lv: var(--on-med); }
.lv-BAJO { --lv: var(--low); }
.lv-CRITICO .tag { background-image: repeating-linear-gradient(-45deg, transparent 0 8px, rgb(0 0 0 / .12) 8px 14px); }

/* Puntuación 1–5 */
.scores { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
legend { font-size: .8rem; font-weight: 500; color: var(--muted); margin-bottom: 4px; padding: 0; }
.seg { display: grid; grid-template-columns: repeat(5, 1fr); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label { display: grid; place-items: center; height: 2.3rem; font: 600 .95rem var(--cond); color: var(--ink); background: var(--field); cursor: pointer; border-left: 1px solid var(--line); }
.seg label:first-of-type { border-left: 0; }
.seg input:checked + label { background: var(--ink); color: var(--surface); }
.seg input:focus-visible + label { outline: 3px solid var(--focus); outline-offset: -3px; }

/* Panel lateral */
.panel { position: sticky; top: 16px; display: grid; gap: 14px; }
.maxrpi { display: flex; align-items: baseline; gap: .6rem; }
.maxrpi span { font: 700 2.8rem/1 var(--cond); font-variant-numeric: tabular-nums; }
.maxrpi small { color: var(--muted); }
.dist { display: flex; height: 14px; border-radius: 3px; overflow: hidden; background: var(--field); gap: 2px; }
.dist i { display: block; background: var(--lv); min-width: 4px; }
.legend { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; font-size: .88rem; }
.legend li { display: grid; grid-template-columns: 12px 1fr auto; gap: 8px; align-items: center; }
.legend li::before { content: ""; width: 12px; height: 12px; border-radius: 2px; background: var(--lv, var(--pend)); }
.legend b { font: 600 .95rem var(--cond); font-variant-numeric: tabular-nums; }
.legend small { grid-column: 2 / -1; color: var(--muted); font-size: .76rem; margin-top: -4px; }
.code input { font-size: .95rem; }
.fine { font-size: .78rem; color: var(--muted); margin: 0; }

/* Informe */
.report { grid-column: 1; min-width: 0; }
.report-meta { color: var(--muted); font-size: .84rem; margin: -.4rem 0 1rem; }
.doc { max-width: 78ch; font-size: .95rem; line-height: 1.6; }
.doc h2.sec { font-size: 1.25rem; margin: 2rem 0 .6rem; padding: .45rem .7rem; background: var(--ink); color: var(--surface); border-radius: var(--radius); }
.doc h2.sec:first-child { margin-top: 0; }
.doc h3 { font-size: 1.05rem; margin: 1.3rem 0 .3rem; color: var(--brand); }
.doc p { margin: .35rem 0; }
.doc ul, .doc ol { margin: .3rem 0 .6rem; padding-left: 1.3rem; }
.doc .kv b { font-weight: 600; }
.doc .note { border-left: 4px solid var(--med); background: var(--field); padding: .5rem .8rem; margin: .5rem 0; }
.doc .tbl { overflow-x: auto; margin: .5rem 0 1rem; }
.doc table { border-collapse: collapse; font-size: .86rem; min-width: 100%; }
.doc th, .doc td { border: 1px solid var(--line); padding: .35rem .5rem; text-align: left; vertical-align: top; }
.doc th { background: var(--field); font-weight: 600; }
.doc mark { background: transparent; color: inherit; font-weight: 600; border-bottom: 2px solid var(--med); }
.doc mark.val { border-color: var(--focus); }
.doc .lvw { font: 700 .8em var(--cond); padding: .05rem .35rem; border-radius: 3px; background: var(--lv); color: var(--on-lv, #fff); }
.doc.loading::after { content: "▍"; color: var(--brand); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Diálogo y aviso */
dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink); width: min(820px, calc(100vw - 32px)); padding: 20px; }
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog form { display: grid; gap: 10px; }
dialog textarea { font: .8rem/1.45 ui-monospace, Consolas, monospace; }
dialog .hint { margin: 0; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; background: var(--ink); color: var(--surface); padding: .7rem 1rem; border-radius: var(--radius); font-size: .9rem; transition: .2s; pointer-events: none; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--crit); color: #fff; }

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding-bottom: 150px; }
  .panel { position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 5; border-radius: 12px 12px 0 0; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); grid-template-columns: auto 1fr; gap: 8px 14px; align-items: center; box-shadow: 0 -6px 24px rgb(0 0 0 / .12); }
  .panel h2, .panel .legend, .panel .fine { display: none; }
  .maxrpi span { font-size: 2rem; }
  .maxrpi small { font-size: .72rem; }
  .panel .dist { grid-column: 2; }
  .panel .code, .panel .btn.primary { grid-column: 1 / -1; }
}
@media (max-width: 640px) {
  .g3, .g2 { grid-template-columns: minmax(0, 1fr); }
  .span2 { grid-column: auto; }
  .scores { grid-template-columns: minmax(0, 1fr); }
  .f-head { grid-template-columns: minmax(0, 1fr) auto auto; }
  .f-num { display: none; }
  .block, .report { padding: 16px; }
  .block-head .actions, .report-head .actions { width: 100%; }
  .block-head .actions .btn { flex: 1 1 auto; justify-content: center; text-align: center; }
}
@media (prefers-reduced-motion: reduce) { .toast { transition: none; } .doc.loading::after { animation: none; } }

@media print {
  body { background: #fff; color: #000; }
  .top .engine, .work, .panel, .report-head .actions, .toast, dialog { display: none !important; }
  .layout { display: block; padding: 0; }
  .report { border: 0; padding: 0; }
  .doc { max-width: none; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .doc h2.sec { break-after: avoid; background: none; color: #000; padding: .2rem 0; border-bottom: 2.5px solid #000; border-radius: 0; }
  .doc h3 { break-after: avoid; color: #0f5e78; }
  .doc .lvw { border: 1px solid var(--lv); }
  .doc.loading::after { content: none; }
  .doc tr, .doc li { break-inside: avoid; }
}
.panel .btn.ghost { width: 100%; }
@media (max-width: 960px) { .panel #showPrompt2 { display: none; } }

/* Cuestionario estratégico */
.est-fuente { display: block; margin-bottom: 4px; }
.est-bloque { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 10px; }
.est-bloque > summary { cursor: pointer; font-weight: 600; color: var(--ink); display: flex; justify-content: space-between; gap: .6rem; }
.est-bloque > summary small { color: var(--muted); font-weight: 500; }
.est-bloque[open] > summary { margin-bottom: 10px; }
.est-q { display: grid; gap: 4px; margin-bottom: 12px; }
.est-q small { color: var(--muted); font-size: .8rem; }

/* Gemba Maestro */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip { font: 500 .82rem/1 var(--sans); padding: .45rem .7rem; border-radius: 999px; border: 1px solid var(--line); background: transparent; color: var(--ink); cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.chip small { opacity: .75; margin-left: 4px; }
.gemba { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.gq { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; gap: 6px; }
.gq.r-no { border-left: 4px solid #c0392b; }
.gq.r-si { border-left: 4px solid #2e7d4f; }
.gq.r-na { opacity: .7; }
.gq-meta { font-size: .76rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: .4rem .8rem; }
.gq-meta b { color: var(--ink); }
.gq p { margin: 0; }
.gq-q { font-weight: 600; }
.gq-v { font-size: .84rem; color: var(--muted); }
.gq-ans { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.gq-ans .chip { min-width: 52px; }
.gq-ans .chip[data-r="no"][aria-pressed="true"] { background: #c0392b; border-color: #c0392b; color: #fff; }
.gq-ans .chip[data-r="si"][aria-pressed="true"] { background: #2e7d4f; border-color: #2e7d4f; color: #fff; }
.gq textarea { min-height: 2.4rem; }
.gq .btn { justify-self: start; }

/* Fotos de evidencia */
.fotos { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.foto { position: relative; width: 72px; height: 72px; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); background: var(--field); }
.foto img { width: 100%; height: 100%; object-fit: cover; display: block; }
.foto-del { position: absolute; top: 2px; right: 2px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: rgba(0,0,0,.65); color: #fff; font-size: 14px; line-height: 22px; padding: 0; cursor: pointer; }
.foto-add { width: 72px; height: 72px; border: 1.5px dashed var(--line); border-radius: var(--radius); display: grid; place-content: center; text-align: center; cursor: pointer; color: var(--brand); position: relative; }
.foto-add:hover { border-color: var(--brand); }
.foto-add input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.foto-add span { font-size: 1.4rem; line-height: 1; }
.foto-add small { font-size: .62rem; line-height: 1.1; padding: 0 4px; }
.fotos.busy { opacity: .55; pointer-events: none; }
.f-fotos { margin-top: 10px; }
.annex { margin-top: 24px; }
.annex h3 { font-size: 1.05rem; }
.annex-item { break-inside: avoid; margin-bottom: 14px; }
.annex-item p { margin: 0 0 6px; font-weight: 600; }
.annex-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 8px; }
.annex-grid img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: var(--radius); border: 1px solid var(--line); }
@media print { .annex-grid { grid-template-columns: repeat(3, 1fr); } .foto-del, .foto-add { display: none !important; } }
.lang-btn { font: 600 .8rem/1 var(--sans); padding: .45rem .6rem; border-radius: var(--radius); border: 1px solid var(--line); background: transparent; color: var(--ink); cursor: pointer; margin-left: 8px; }
.lang-btn:hover { border-color: var(--brand); color: var(--brand); }

/* Diagnósticos en la nube */
.cloudbar { display: flex; justify-content: space-between; align-items: center; gap: 10px 16px; flex-wrap: wrap; background: var(--surface); border: 1px dashed var(--line); border-radius: 10px; padding: 12px 16px; }
.cloudbar.on { border-style: solid; border-left: 4px solid #2e7d4f; }
.cloudbar.warn { border-left-color: #c0392b; }
.cloud-status { display: grid; gap: 2px; }
.cloud-status b { font-size: .92rem; }
.cloud-status small { color: var(--muted); font-size: .8rem; }
#cloudDlg { width: min(520px, calc(100vw - 24px)); box-sizing: border-box; }
.cloud-dlg { display: grid; gap: 12px; min-width: 0; }
.clist li > div { flex: 1 1 auto; }
.clist { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; max-height: 50vh; overflow: auto; }
.clist li { display: flex; justify-content: space-between; align-items: center; gap: 10px; border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 10px; }
.clist li div { display: grid; gap: 2px; min-width: 0; }
.clist small { color: var(--muted); font-size: .78rem; }
.badge-on { font-size: .75rem; font-weight: 600; color: #2e7d4f; }
@media (max-width: 640px) { .cloudbar .actions { width: 100%; } .cloudbar .actions .btn { flex: 1 1 auto; } }
@media print { .cloudbar { display: none !important; } }
