:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --ink: #161a22;
  --ink-2: #4b5363;
  --muted: #8a91a0;
  --line: #e4e6eb;
  --line-2: #eef0f3;
  --side: #14181f;
  --side-ink: #c9ced8;
  --accent: #d9481e;
  --accent-ink: #ffffff;
  --accent-soft: #fdeee8;
  --green: #1f7a4d; --green-soft: #e5f4ec;
  --amber: #a15c00; --amber-soft: #fff3dc;
  --red: #b42318; --red-soft: #fdecea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 var(--font); color: var(--ink); background: var(--bg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; line-height: 1.2; margin: 0; letter-spacing: -.01em; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h2 { font-size: 16px; margin: 0 0 12px; }
code { font: 13px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--line-2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.text-red { color: var(--red); font-weight: 600; }
.text-green { color: var(--green); font-weight: 600; }
.eyebrow { margin: 0 0 4px; font-size: 13px; color: var(--muted); }
.eyebrow a { color: var(--muted); }

/* ---------- Estructura ---------- */
.shell { display: flex; min-height: 100vh; }
.sidebar { width: 240px; flex: none; background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; padding: 20px 18px; color: #fff; }
.brand:hover { text-decoration: none; }
.brand-mark { width: 36px; height: 36px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 800; font-size: 14px; letter-spacing: .02em; }
.brand-text { font-weight: 700; line-height: 1.1; }
.brand-text small { display: block; font-weight: 500; color: var(--muted); font-size: 12px; }
.nav { display: flex; flex-direction: column; padding: 8px 10px; gap: 2px; flex: 1; }
.nav a, .me { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 7px; color: var(--side-ink); }
.nav a:hover, .me:hover { background: rgba(255,255,255,.06); text-decoration: none; }
.nav a.active, .me.active { background: rgba(255,255,255,.1); color: #fff; }
.nav svg, .me svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.count { margin-left: auto; background: var(--accent); color: #fff; font-size: 12px; min-width: 22px; height: 22px; border-radius: 11px; display: grid; place-items: center; padding: 0 6px; }
.sidebar-foot { padding: 10px; border-top: 1px solid rgba(255,255,255,.08); }
.me span { line-height: 1.2; }
.me small { display: block; color: var(--muted); font-size: 12px; }
.link-btn { background: none; border: 0; color: var(--muted); font: inherit; font-size: 13px; padding: 6px 10px; cursor: pointer; }
.link-btn:hover { color: #fff; }

.main { flex: 1; min-width: 0; }
.topbar { display: none; }
.scrim { display: none; }
.content { max-width: 1180px; margin: 0 auto; padding: 28px 28px 80px; }

/* ---------- Componentes ---------- */
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.head-actions form { margin: 0; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 18px; box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.empty-state { max-width: 520px; margin: 40px auto; text-align: center; }
.empty-state h1 { justify-content: center; margin-bottom: 8px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 600; font-size: 14px; padding: 9px 15px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; white-space: nowrap; min-height: 40px; }
.btn:hover { background: var(--line-2); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #c23d15; }
.btn-danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn-danger:hover { background: #962015; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-danger-text { color: var(--red); }
.btn-sm { padding: 5px 10px; min-height: 32px; font-size: 13px; }
.btn-block { width: 100%; }
.icon-btn { width: 36px; height: 36px; border-radius: 8px; border: 0; background: transparent; color: var(--muted); font-size: 22px; cursor: pointer; line-height: 1; }
.icon-btn:hover { background: var(--red-soft); color: var(--red); }

.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; letter-spacing: .01em; vertical-align: middle; white-space: nowrap; }
.badge-gray { background: var(--line-2); color: var(--ink-2); }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-red { background: var(--red-soft); color: var(--red); }
.badge-muted { background: var(--line-2); color: var(--muted); text-decoration: line-through; }

.flash { padding: 12px 14px; border-radius: 8px; margin-bottom: 16px; border: 1px solid; font-size: 14px; }
.flash ul { margin: 6px 0 0; padding-left: 20px; }
.flash-ok { background: var(--green-soft); border-color: #bfe3cf; color: #135c38; }
.flash-error { background: var(--red-soft); border-color: #f5c6c1; color: #8c1c13; }
.flash-warn { background: var(--amber-soft); border-color: #f3d79b; color: #7a4600; }

/* Estadísticas */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; display: flex; flex-direction: column; color: var(--ink); box-shadow: var(--shadow); }
a.stat:hover { border-color: #cfd3da; text-decoration: none; }
.stat-label { font-size: 13px; color: var(--ink-2); }
.stat-value { font-size: 30px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat-value.small-value { font-size: 22px; padding-top: 6px; }
.stat-sub { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.stat-alert { border-color: #f0b7a3; background: var(--accent-soft); }
.stat-alert .stat-value { color: var(--accent); }
.stat-warn .stat-value { color: var(--red); }

/* Tablas */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 11px 10px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tfoot td { border: 0; padding: 6px 10px; color: var(--ink-2); }
.table tfoot tr.total td { font-weight: 700; font-size: 16px; color: var(--ink); border-top: 1px solid var(--line); padding-top: 10px; }
.row-link { cursor: pointer; }
.row-link:hover td { background: #fafafb; }
.row-off td { opacity: .55; }
.table td.actions { display: flex; gap: 6px; justify-content: flex-end; }
.table td.actions form { margin: 0; }

.meter { height: 8px; background: var(--line-2); border-radius: 4px; overflow: hidden; min-width: 90px; }
.meter span { display: block; height: 100%; background: var(--green); }
.meter-amber span { background: #e09b1b; }
.meter-red span { background: var(--red); }

/* Pestañas y filtros */
.tabs { display: flex; gap: 4px; margin-bottom: 14px; overflow-x: auto; border-bottom: 1px solid var(--line); }
.tabs a { padding: 8px 12px; color: var(--ink-2); font-weight: 600; font-size: 14px; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a:hover { color: var(--ink); text-decoration: none; }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }
.filters { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; padding: 14px 16px; }
.filters .field { margin: 0; flex: 1 1 180px; }
.filters-actions { display: flex; gap: 6px; }

/* Formularios */
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--ink-2); }
input, select, textarea { font: inherit; font-size: 15px; color: var(--ink); background: #fff; border: 1px solid #d3d7de; border-radius: 8px; padding: 9px 11px; width: 100%; min-height: 42px; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(217, 72, 30, .15); }
input:disabled { background: var(--line-2); color: var(--ink-2); }
input.num { text-align: right; }
.has-error input, .has-error select { border-color: var(--red); }
.error { color: var(--red); font-size: 13px; }
.hint { color: var(--muted); font-size: 12.5px; }
.check { display: flex; align-items: flex-start; gap: 8px; font-size: 14px; margin: 4px 0 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; min-height: 0; margin: 2px 0 0; accent-color: var(--accent); flex: none; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 16px; }
.form-narrow { max-width: 760px; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 8px; }
.form-narrow h2 { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line-2); }

/* Partidas de la orden */
.lines { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.line { display: grid; grid-template-columns: 130px 1fr 100px 130px 120px 36px; gap: 8px; align-items: center; }
.line-head { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.line label { margin: 0; }
.line-label { display: none; }
.line-total { font-weight: 600; padding-right: 4px; }
.totals { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; flex-wrap: wrap; border-top: 1px solid var(--line); margin-top: 14px; padding-top: 14px; }
.totals .check { max-width: 420px; color: var(--ink-2); }
.totals dl { display: grid; grid-template-columns: auto 140px; gap: 4px 20px; margin: 0; font-variant-numeric: tabular-nums; }
.totals dt { color: var(--ink-2); text-align: right; }
.totals dd { margin: 0; text-align: right; }
.totals .total { font-size: 20px; font-weight: 700; color: var(--ink); }
.sticky-actions { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); padding: 18px 0 14px; }

/* Detalle */
.grid-main { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 18px; align-items: start; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; margin: 0; }
.facts dt { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.facts dd { margin: 2px 0 0; font-weight: 600; }
.mini { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 0; font-size: 14px; }
.mini dt { color: var(--muted); }
.mini dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.mini.roles dd { text-align: left; }
.note { background: var(--line-2); border-radius: 8px; padding: 10px 12px; font-size: 14px; margin: 14px 0 0; }
.decision { border-color: #f0b7a3; background: linear-gradient(var(--accent-soft), #fff 60%); }
.decision-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.decision-grid form { margin: 0; }
.timeline { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.timeline li { padding: 0 0 12px 16px; border-left: 2px solid var(--line); position: relative; }
.timeline li::before { content: ""; position: absolute; left: -6px; top: 6px; width: 10px; height: 10px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); }
.timeline li:last-child { padding-bottom: 0; }
.danger-zone form { margin: 0; }

/* Login */
body.simple { display: grid; place-items: center; min-height: 100vh; padding: 20px; background: var(--side); }
.simple-wrap { width: 100%; max-width: 400px; }
.simple-brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; margin-bottom: 18px; justify-content: center; }
.simple-brand small { color: var(--muted); font-weight: 500; }
.login-card { padding: 28px; }
.login-card h1 { margin-bottom: 4px; }
.login-card > .muted { margin: 0 0 20px; }

/* ---------- Celular y tableta ---------- */
@media (max-width: 960px) {
  .grid-main { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .sidebar { position: fixed; z-index: 30; left: 0; top: 0; transform: translateX(-100%); transition: transform .2s ease; }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 20; }
  .topbar { display: flex; align-items: center; gap: 8px; position: sticky; top: 0; z-index: 10; background: var(--side); color: #fff; padding: 8px 12px; }
  .menu-btn { width: 40px; height: 40px; border: 0; background: transparent; color: #fff; cursor: pointer; padding: 8px; }
  .menu-btn svg { width: 24px; height: 24px; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
  .topbar-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .content { padding: 18px 16px 80px; }
  h1 { font-size: 21px; }
}
@media (max-width: 720px) {
  .grid-2, .grid-3, .decision-grid { grid-template-columns: 1fr; }
  .card { padding: 16px; }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .line-head { display: none; }
  .line { grid-template-columns: 1fr 1fr; gap: 8px; padding: 12px; border: 1px solid var(--line); border-radius: 8px; position: relative; }
  .line label:nth-child(2) { grid-column: 1 / -1; }
  .line-label { display: block; font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 3px; }
  .line-total { grid-column: 1 / -1; text-align: right; }
  .line .js-remove { position: absolute; top: 4px; right: 4px; }
  .line label:first-child { padding-right: 36px; }
  .totals dl { width: 100%; grid-template-columns: 1fr auto; }
  .page-head .btn-primary { width: 100%; }
  .stats { grid-template-columns: 1fr 1fr; }
  .stat-value { font-size: 24px; }
}
