/* Layout: barra lateral fija con módulos + área de trabajo; en celular la barra pasa arriba y se desplaza horizontalmente */
:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --ink: #16202b;
  --muted: #5d6b7a;
  --line: #d9dfe6;
  --accent: #1d4fc4;
  --accent-ink: #ffffff;
  --accent-soft: #e4ebfb;
  --ok: #1f7a4d;   --ok-soft: #e1f3ea;
  --warn: #9a5b00; --warn-soft: #fbefd9;
  --bad: #b3261e;  --bad-soft: #fbe3e1;
  --shadow: 0 1px 2px rgba(22,32,43,.06), 0 4px 16px rgba(22,32,43,.06);
  --font-ui: "Manrope", "Segoe UI", system-ui, sans-serif;
  --font-num: "IBM Plex Mono", ui-monospace, "Consolas", monospace;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151c; --surface: #17202a; --surface-2: #1e2935; --ink: #e6ebf1; --muted: #93a2b3;
    --line: #2c3a48; --accent: #7aa2ff; --accent-ink: #0b1220; --accent-soft: #1d2c4d;
    --ok: #5cc98f; --ok-soft: #173427; --warn: #f0b456; --warn-soft: #3a2c12; --bad: #ff8a80; --bad-soft: #3d1a18;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font-ui); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.2; text-wrap: balance; }
h1 { font-size: 1.5rem; font-weight: 800; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; font-weight: 700; }
h3 { font-size: .95rem; font-weight: 700; }
a { color: var(--accent); }
.num, td.n, .rx td { font-family: var(--font-num); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.cargando { padding: 40px; color: var(--muted); }

/* ---------- estructura ---------- */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100%; }
.side { background: var(--surface); border-right: 1px solid var(--line); padding: 20px 12px; display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 18px; }
.brand-mark { width: 30px; height: 30px; border-radius: 50%; border: 3px solid var(--accent); position: relative; flex: none; }
.brand-mark::after { content: ""; position: absolute; inset: 6px; border-radius: 50%; background: var(--accent); }
.brand b { font-size: 1.05rem; font-weight: 800; white-space: nowrap; }
.brand span { display: block; font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.nav a { display: block; padding: 9px 12px; border-radius: 8px; color: var(--ink); text-decoration: none; font-weight: 600; }
.nav a:hover { background: var(--surface-2); }
.nav a.on { background: var(--accent-soft); color: var(--accent); }
.side-foot { margin-top: auto; padding: 12px 8px 0; border-top: 1px solid var(--line); font-size: .85rem; }
.side-foot .rol { display: inline-block; font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.main { padding: 24px 28px 60px; min-width: 0; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.page-head .acciones { display: flex; flex-wrap: wrap; gap: 8px; }

@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: sticky; top: env(safe-area-inset-top, 0px); height: auto; flex-direction: row; align-items: center; padding: 8px 12px; overflow-x: auto; z-index: 5; border-right: 0; border-bottom: 1px solid var(--line); }
  .brand { padding: 0 8px 0 0; }
  .brand span { display: none; }
  .nav { display: flex; gap: 2px; }
  .nav a { white-space: nowrap; padding: 7px 10px; }
  .side-foot { margin: 0 0 0 8px; border: 0; padding: 0; white-space: nowrap; }
  .side-foot .quien { display: none; }
  .main { padding: 16px 16px 60px; }
}

/* ---------- componentes ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card > h2, .card > h3 { margin-bottom: 12px; }
.grid { display: grid; gap: 16px; }
.grid.dos { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.spacer { flex: 1; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.kpi .lbl { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 700; }
.kpi .val { font: 500 1.6rem/1.2 var(--font-num); margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi.alerta { border-color: var(--warn); background: var(--warn-soft); }

button, .btn { font: 600 .9rem var(--font-ui); border: 1px solid var(--line); background: var(--surface); color: var(--ink); border-radius: 8px; padding: 8px 14px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
button:hover, .btn:hover { background: var(--surface-2); }
button.pri, .btn.pri { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.pri:hover { filter: brightness(1.08); }
button.peligro { color: var(--bad); border-color: var(--bad); }
button.wa { color: var(--ok); border-color: var(--ok); }
button.mini { padding: 4px 9px; font-size: .8rem; }
button:disabled { opacity: .55; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

label { display: flex; flex-direction: column; gap: 4px; font-size: .8rem; font-weight: 600; color: var(--muted); min-width: 0; }
input, select, textarea { font: 400 .95rem var(--font-ui); color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0; }
textarea { min-height: 70px; resize: vertical; }
input[type=checkbox] { width: auto; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); font-weight: 500; font-size: .9rem; }
.form { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.form .full { grid-column: 1 / -1; }
.buscar { max-width: 360px; }

.tabla-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th { text-align: left; font-size: .72rem; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); font-weight: 700; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
td.n, th.n { text-align: right; white-space: nowrap; }
tr.clic { cursor: pointer; }
tr.clic:hover td { background: var(--surface-2); }
.vacio { padding: 28px 12px; text-align: center; color: var(--muted); }

.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .75rem; font-weight: 700; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.info { background: var(--accent-soft); color: var(--accent); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border-radius: 999px; padding: 5px 12px; font-size: .82rem; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* Receta (grilla de refracción) */
.rx { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.rx table th, .rx table td { text-align: center; }
.rx table td:first-child, .rx table th:first-child { text-align: left; font-family: var(--font-ui); font-weight: 700; }
.rx-form { display: grid; grid-template-columns: 36px repeat(4, minmax(52px, 1fr)); gap: 6px; align-items: center; }
.rx-form .h { font-size: .7rem; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); font-weight: 700; text-align: center; }
.rx-form .ojo { font-weight: 800; }
.rx-form input { font-family: var(--font-num); text-align: center; }

/* Tablero de órdenes */
.kanban { display: grid; grid-template-columns: repeat(4, minmax(230px, 1fr)); gap: 12px; overflow-x: auto; }
.col { background: var(--surface-2); border-radius: var(--radius); padding: 10px; min-width: 230px; }
.col h3 { display: flex; justify-content: space-between; padding: 4px 4px 10px; }
.ot { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 8px; display: flex; flex-direction: column; gap: 6px; }
.ot.atrasada { border-left: 4px solid var(--bad); }

/* Buscador con resultados */
.picker { position: relative; }
.picker-res { position: absolute; left: 0; right: 0; top: 100%; z-index: 20; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); max-height: 260px; overflow-y: auto; }
.picker-res div { padding: 8px 10px; cursor: pointer; border-bottom: 1px solid var(--line); }
.picker-res div:hover { background: var(--surface-2); }
.elegido { display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--accent-soft); border-radius: 8px; font-weight: 600; }

.totales { margin-left: auto; min-width: 240px; }
.totales div { display: flex; justify-content: space-between; padding: 3px 0; font-family: var(--font-num); }
.totales .grande { font-size: 1.2rem; font-weight: 500; border-top: 1px solid var(--line); margin-top: 4px; padding-top: 6px; }

.aviso { padding: 10px 12px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); font-size: .88rem; }
.aviso.ok { background: var(--ok-soft); color: var(--ok); }

/* Modal */
.modal { border: 0; border-radius: 14px; padding: 0; width: min(760px, calc(100vw - 32px)); max-height: calc(100vh - 40px); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal::backdrop { background: rgba(10,16,24,.45); }
.modal form { display: flex; flex-direction: column; max-height: calc(100vh - 40px); }
.modal .m-head { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.modal .m-body { padding: 16px 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.modal .m-foot { padding: 12px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.modal .error { color: var(--bad); font-size: .88rem; }

.toast { position: fixed; bottom: calc(20px + env(safe-area-inset-bottom, 0px)); left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 8px; font-weight: 600; z-index: 100; max-width: calc(100vw - 32px); }
.toast.error { background: var(--bad); color: #fff; }

/* Acceso */
.acceso { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.acceso .card { width: min(400px, 100%); padding: 28px; }
.acceso .brand { padding: 0 0 20px; }

/* ---------- impresión ---------- */
.print-area { display: none; }
@media print {
  @page { size: A5; margin: 12mm; }
  body { background: #fff; color: #000; }
  .app, .modal, .toast { display: none !important; }
  .print-area { display: block; font: 11pt/1.4 var(--font-ui); color: #000; }
  .print-area h1 { font-size: 16pt; }
  .print-area table { font-size: 10pt; }
  .print-area th, .print-area td { border: 1px solid #999; padding: 4px 6px; color: #000; }
  .print-area .enc { display: flex; justify-content: space-between; border-bottom: 2px solid #000; padding-bottom: 6px; margin-bottom: 10px; }
  .print-area .firma { margin-top: 40px; border-top: 1px solid #000; width: 60%; text-align: center; padding-top: 4px; }
}

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
