:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --ink: #1b1d1f;
  --ink-2: #4a4f55;
  --ink-3: #80868d;
  --line: #e2e0d8;
  --line-2: #cfccc1;
  --side: #16191c;
  --side-ink: #b9bec4;
  --accent: #e8a317;
  --accent-ink: #1b1d1f;
  --accent-soft: #fbf0d4;
  --ok: #1f7a3f;
  --ok-soft: #e3f2e7;
  --warn: #9a5b00;
  --warn-soft: #fdf0d9;
  --bad: #b3261e;
  --bad-soft: #fbe5e3;
  --info: #2a78d6;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --radius: 8px;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--sans); font-size: 14px; color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
a { color: inherit; }
.num, td.n, th.n { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }

/* Layout */
.app { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.side { background: var(--side); color: var(--side-ink); padding: 18px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; color: #fff; font-weight: 600; letter-spacing: .2px; }
.brand-mark { width: 26px; height: 26px; border-radius: 50%; background: radial-gradient(circle, var(--accent) 0 3px, transparent 4px), conic-gradient(from 0deg, #2c3136, #3b4249, #2c3136); border: 1px solid #444b52; }
.brand small { display: block; font-weight: 400; color: var(--side-ink); font-size: 11.5px; }
.nav-group { margin: 14px 0 4px; padding: 0 10px; font-size: 10.5px; text-transform: uppercase; letter-spacing: 1.1px; color: #6c737a; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 6px; text-decoration: none; color: var(--side-ink); }
.nav a:hover { background: #22272b; color: #fff; }
.nav a.active { background: #262c31; color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.nav a .k { margin-left: auto; font-size: 11px; color: #6c737a; }
.app > div { min-width: 0; }
.main { min-width: 0; padding: 22px 28px 60px; }
.topbar { display: none; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 22px; font-weight: 600; letter-spacing: -.2px; }
.page-head .sub { color: var(--ink-3); margin-top: 3px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.card h2 { font-size: 14px; margin: 0 0 12px; font-weight: 600; }
.card h2 .muted { font-weight: 400; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g12 { grid-template-columns: minmax(0, 1fr) minmax(0, 2fr); }
table.t td code { white-space: nowrap; }
table.t th.n { text-align: right; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.kpi .l { color: var(--ink-3); font-size: 12px; }
.kpi .v { font-size: 24px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.3px; }
.kpi .s { color: var(--ink-3); font-size: 12px; margin-top: 2px; }
.kpi.alert .v { color: var(--bad); }
.muted { color: var(--ink-3); }
.small { font-size: 12px; }

/* Buttons & forms */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 13px; border-radius: 6px; border: 1px solid var(--line-2); background: var(--surface); color: var(--ink); cursor: pointer; font: inherit; font-weight: 500; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: var(--ink-3); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(.96); }
.btn.dark { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 4px 9px; font-size: 12.5px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ink-2); font-weight: 500; }
input, select, textarea { font: inherit; color: var(--ink); background: #fff; border: 1px solid var(--line-2); border-radius: 6px; padding: 7px 9px; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
textarea { resize: vertical; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.form-grid .wide { grid-column: 1 / -1; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 14px; }
.filters label.f { min-width: 150px; }
.check { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; }

/* Tables */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; }
table.t th { text-align: left; font-size: 11.5px; font-weight: 600; color: var(--ink-3); text-transform: uppercase; letter-spacing: .5px; padding: 8px 10px; border-bottom: 1px solid var(--line-2); white-space: nowrap; background: var(--surface); position: sticky; top: 0; }
table.t td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: var(--surface-2); }
table.t tfoot td { font-weight: 600; border-top: 1px solid var(--line-2); border-bottom: none; }
table.t tr.group td { background: var(--surface-2); font-weight: 600; font-size: 12.5px; }
.empty { text-align: center; color: var(--ink-3); padding: 28px 10px; }

.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 500; background: #eeede7; color: var(--ink-2); white-space: nowrap; }
.tag.recepcion { background: #e3effc; color: #1d5ea8; }
.tag.salida { background: var(--accent-soft); color: #7a5300; }
.tag.devolucion { background: var(--ok-soft); color: var(--ok); }
.tag.traspaso { background: #ece9fb; color: #4b3cb3; }
.tag.ajuste { background: #eeede7; color: var(--ink-2); }
.tag.ok { background: var(--ok-soft); color: var(--ok); }
.tag.bad { background: var(--bad-soft); color: var(--bad); }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.pos { color: var(--ok); }
.neg { color: var(--bad); }

.bar { height: 8px; background: #eeede7; border-radius: 4px; overflow: hidden; min-width: 80px; }
.bar > span { display: block; height: 100%; background: var(--ok); border-radius: 4px; }
.bar.warn > span { background: var(--accent); }
.bar.bad > span { background: var(--bad); }

/* Movement lines */
.lines { display: flex; flex-direction: column; gap: 10px; }
.line { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; background: var(--surface-2); }
.line-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.line-head .title { font-weight: 600; }
.line-head .avail { margin-left: auto; font-size: 12.5px; color: var(--ink-2); }
.line-body { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.line-body label.f { min-width: 130px; }
.serial-box { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 4px 12px; max-height: 220px; overflow-y: auto; padding: 8px; border: 1px solid var(--line); background: #fff; border-radius: 6px; width: 100%; }
.reel-rows { display: flex; flex-direction: column; gap: 6px; }
.reel-rows .rr { display: flex; gap: 6px; }
.add-line { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; padding: 12px; border: 1px dashed var(--line-2); border-radius: var(--radius); }
.add-line label.f { flex: 1; min-width: 220px; }
.scan { background: #fffdf5; }
.submit-bar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }

/* Modal & toast */
.modal-bg { position: fixed; inset: 0; background: rgba(20, 22, 24, .45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 90; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 10px; width: min(640px, 100%); padding: 20px; box-shadow: 0 20px 50px rgba(0,0,0,.2); }
.modal.wide { width: min(980px, 100%); }
.modal h3 { margin: 0 0 14px; font-size: 17px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
#toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.25); max-width: 90vw; }
.toast.err { background: var(--bad); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; background: var(--side); padding: 16px; }
.login form { background: var(--surface); padding: 28px; border-radius: 12px; width: min(360px, 100%); display: flex; flex-direction: column; gap: 14px; }
.login h1 { margin: 0; font-size: 20px; }

/* Chart */
.chart svg { width: 100%; height: auto; display: block; }
.chart .legend { display: flex; gap: 16px; font-size: 12px; color: var(--ink-2); margin-bottom: 6px; }
.chart .legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.chart .tip { position: fixed; pointer-events: none; background: var(--ink); color: #fff; font-size: 12px; padding: 6px 9px; border-radius: 6px; z-index: 70; white-space: nowrap; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; flex-wrap: wrap; }
.tabs a { padding: 8px 12px; text-decoration: none; color: var(--ink-2); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--ink); border-color: var(--accent); font-weight: 600; }
dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.kv dt { color: var(--ink-3); }
dl.kv dd { margin: 0; }
.print-only { display: none; }
.show-m { display: none; }
table.pdf-rev tr.off td { opacity: .55; }
table.pdf-rev td { vertical-align: middle; }
.pdf-mode { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 12px; }
.line { position: relative; }

/* Escáner con cámara */
.cam-btn { width: 100%; justify-content: center; padding: 12px; font-size: 15px; margin-top: 10px; }
.inline { display: flex; gap: 6px; }
.inline input { flex: 1; }
.code-big { font-family: var(--mono); font-size: 18px; padding: 10px 12px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 6px; word-break: break-all; margin-bottom: 10px; }
body.no-scroll { overflow: hidden; }
.scanner { position: fixed; inset: 0; z-index: 80; background: #0d0f11; color: #fff; display: flex; flex-direction: column; overflow-x: hidden; width: 100vw; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); overflow-y: auto; }
.sc-top { display: flex; align-items: center; gap: 10px; padding: 12px 16px; }
.sc-top strong { font-size: 16px; }
.sc-sum { margin-left: auto; color: #9aa1a8; font-size: 13px; }
.sc-view { position: relative; margin: 0 12px; min-width: 0; flex: none; border-radius: 12px; overflow: hidden; background: #000; min-height: 240px; border: 3px solid transparent; transition: border-color .25s; }
.sc-view.hit-ok { animation: hitok .6s; }
.sc-view.hit-bad { animation: hitbad .8s; }
@keyframes hitok { 0% { border-color: #2fbf5b; } 100% { border-color: transparent; } }
@keyframes hitbad { 0% { border-color: #e5484d; } 100% { border-color: transparent; } }
#sc-reader { width: 100% !important; max-width: 100%; overflow: hidden; }
#sc-reader video { width: 100% !important; max-width: 100%; height: auto !important; display: block; object-fit: cover; }
#sc-reader img, #sc-reader canvas, #sc-reader #qr-shaded-region, #sc-reader > div:not(:has(video)) { display: none !important; }
.sc-frame { position: absolute; inset: 0; pointer-events: none; display: grid; place-items: center; }
.sc-frame span { width: 86%; height: 42%; border: 2px solid rgba(232,163,23,.9); border-radius: 10px; box-shadow: 0 0 0 9999px rgba(0,0,0,.28); }
.sc-msg { position: absolute; left: 0; right: 0; bottom: 10px; text-align: center; font-size: 13px; color: #fff; text-shadow: 0 1px 3px #000; padding: 0 12px; }
.sc-zoom { display: flex; align-items: center; gap: 10px; padding: 10px 16px 0; font-size: 13px; color: #9aa1a8; }
.sc-zoom[hidden] { display: none; }
.sc-zoom input { flex: 1; accent-color: var(--accent); }
.sc-status { padding: 12px 16px 0; font-size: 13.5px; color: #d6dade; line-height: 1.5; }
.sc-status .btn { margin-left: 6px; }
.sc-manual { display: flex; gap: 8px; padding: 12px 16px 0; }
.sc-manual input { flex: 1; background: #1b1f23; color: #fff; border-color: #33393f; }
.sc-log { list-style: none; margin: 10px 16px 16px; padding: 0; font-size: 14px; }
.sc-log li { padding: 8px 10px; border-radius: 6px; margin-bottom: 5px; background: #1b1f23; font-family: var(--mono); font-size: 13px; word-break: break-all; }
.sc-log li.ok { border-left: 3px solid #2fbf5b; }
.sc-log li.bad { border-left: 3px solid #e5484d; color: #ffb4b4; }
.side-shade { display: none; }
.nav a svg { flex: none; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; z-index: 45; left: 0; top: 0; bottom: 0; height: auto; width: 270px; transform: translateX(-100%); transition: transform .2s; padding-top: calc(18px + env(safe-area-inset-top)); }
  body.menu-open .side { transform: none; }
  body.menu-open .side-shade { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 44; }
  .topbar { display: flex; align-items: center; gap: 8px; padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top)); background: var(--side); color: #fff; position: sticky; top: 0; z-index: 30; }
  .topbar strong { margin-right: auto; font-size: 15px; }
  .topbar .btn { padding: 7px 10px; }
  .main { padding: 14px 12px calc(24px + env(safe-area-inset-bottom)); }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .g2, .g3, .g12 { grid-template-columns: 1fr; }
  .kpi { padding: 12px; }
  .kpi .v { font-size: 19px; }
  .page-head { margin-bottom: 12px; }
  .page-head h1 { font-size: 20px; }
  .page-head .actions { width: 100%; }
  .card { padding: 12px; overflow-x: auto; }
  /* iOS hace zoom en campos con letra < 16px */
  input, select, textarea { font-size: 16px; padding: 9px 10px; }
  .btn { padding: 9px 13px; }
  .btn.sm { padding: 6px 10px; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  label.f input, label.f select, label.f textarea { width: 100%; }
  .form-grid > label.f:has(select), .form-grid .wide { grid-column: 1 / -1; }
  .filters label.f { min-width: calc(50% - 5px); flex: 1; }
  .line { padding: 10px; }
  .line-head { flex-wrap: wrap; gap: 6px 8px; }
  .line-head .avail { margin-left: 0; width: 100%; order: 3; }
  .line-head { padding-right: 34px; }
  .line-head [data-act=del] { position: absolute; top: 6px; right: 6px; }
  .hide-m { display: none; }
  .show-m { display: revert; }
  .submit-bar .btn.show-m { display: inline-flex; flex: none; padding: 12px; }
  .line-body { gap: 10px; }
  .line-body > label.f { flex: 1 1 130px; min-width: 0; }
  .line-body > .btn { width: 100%; justify-content: center; }
  .serial-box { grid-template-columns: 1fr; max-height: 260px; }
  .serial-box .check { padding: 6px 0; font-size: 15px; }
  .serial-box input[type=checkbox] { width: 20px; height: 20px; }
  .add-line { padding: 10px; }
  .add-line label.f { min-width: 100%; }
  .submit-bar { position: sticky; bottom: 0; background: var(--surface); margin: 12px -12px -12px; padding: 12px 12px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); border-radius: 0 0 var(--radius) var(--radius); z-index: 5; }
  .submit-bar .btn { flex: 1; justify-content: center; padding: 12px; }
  .submit-bar #totals { white-space: nowrap; }
  table.t th, table.t td { padding: 7px 8px; }
  .modal-bg { padding: 12px; padding-top: calc(12px + env(safe-area-inset-top)); }
  .modal { padding: 16px; }
  .modal .foot { flex-wrap: wrap; }
  .modal .foot .btn { flex: 1; justify-content: center; }
  .tabs { flex-wrap: nowrap; overflow-x: auto; }
  .tabs a { white-space: nowrap; }
  #toast { bottom: calc(80px + env(safe-area-inset-bottom)); }
}

@media print {
  body { background: #fff; font-size: 11px; }
  .scanner, .cam-btn, .side, .topbar, .no-print, .actions, .filters, .tabs { display: none !important; }
  .app { display: block; }
  .main { padding: 0; }
  .card { border: none; padding: 0; margin-bottom: 14px; }
  .print-only { display: block; }
  table.t th { position: static; }
  table.t td, table.t th { padding: 4px 6px; }
  .kpi { border-color: #ccc; }
}
