:root {
  /* Paleta NNR: azul marino + verde (del logo) */
  --bg: #141b3a;
  --bg-2: #1b2344;
  --sidebar: #141b3a;
  --card: #ffffff;
  --ink: #1e293b;
  --ink-soft: #64748b;
  --line: #e2e8f0;
  --primary: #2e3a66;
  --primary-dark: #243056;
  --primary-soft: #eef1f8;
  --green: #5fa233;
  --green-dark: #4d8a29;
  --green-soft: #e8f3dd;
  --amber: #d97706;
  --amber-soft: #fef3c7;
  --red: #dc2626;
  --red-soft: #fee2e2;
  --gray-soft: #f1f5f9;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15,23,42,.08), 0 1px 2px rgba(15,23,42,.06);
  --shadow-lg: 0 10px 25px rgba(15,23,42,.15);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  background: #f8fafc;
  color: var(--ink);
  font-size: 14px;
}
.hidden { display: none !important; }
a { color: inherit; text-decoration: none; cursor: pointer; }

/* ---------- LOGIN ---------- */
.login-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1200px 600px at 20% -10%, #2e3a66, #10152e);
  padding: 20px;
}
.login-card {
  background: var(--card); width: 100%; max-width: 380px;
  padding: 32px; border-radius: 16px; box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; gap: 14px;
}
.login-brand { text-align: center; margin-bottom: 8px; }
.login-brand p { color: var(--ink-soft); margin: 8px 0 0; font-size: 13px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 13px; }
.login-error { color: var(--red); font-size: 13px; min-height: 18px; margin: 0; text-align: center; }
.login-hint { text-align: center; color: var(--ink-soft); font-size: 12px; margin: 0; }

.logo-badge {
  font-size: 26px; font-weight: 800; letter-spacing: -.5px; color: var(--primary);
  display: inline-block;
}
.logo-badge span { color: var(--ink); }

/* ---------- APP SHELL ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; background: var(--sidebar); color: #cbd5e1;
  display: flex; flex-direction: column; padding: 18px 12px; flex-shrink: 0;
}
.sidebar-logo { background: #fff; border-radius: 10px; padding: 12px 10px 10px; margin-bottom: 10px; text-align: center; }
.sidebar-logo img { height: 56px; width: auto; }
.brand-sub { color: var(--primary); font-weight: 700; font-size: 14px; line-height: 1.2; margin-top: 4px; }
.login-logo-img { height: 78px; width: auto; }
.brand-sub-login { font-size: 20px; margin-top: 6px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: #64748b; padding: 16px 12px 6px; }
.nav-item { padding: 9px 12px; border-radius: 8px; color: #cbd5e1; font-weight: 500; }
.nav-item:hover { background: rgba(148,163,184,.12); color: #fff; }
.nav-item.active { background: var(--green); color: #fff; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 60px; background: #fff; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 14px; padding: 0 22px;
}
.topbar h1 { font-size: 18px; margin: 0; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.user-name { color: var(--ink-soft); font-weight: 600; }
.menu-toggle { display: none; background: none; border: none; font-size: 20px; cursor: pointer; }
.view { padding: 22px; overflow: auto; }

/* ---------- BUTTONS ---------- */
.btn {
  border: 1px solid transparent; border-radius: 8px; padding: 8px 14px;
  font-size: 13px; font-weight: 600; cursor: pointer; background: #fff; color: var(--ink);
  display: inline-flex; align-items: center; gap: 6px; transition: .12s;
}
.btn:hover { filter: brightness(.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; border-color: var(--line); }
.btn-danger { background: var(--red); color: #fff; }
.btn-success { background: var(--green); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-block { width: 100%; justify-content: center; }
.btn-icon { padding: 5px 8px; background: transparent; border-color: var(--line); }

/* ---------- CARDS / GRID ---------- */
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar .spacer { flex: 1; }
.search-input { padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; min-width: 220px; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 16px; margin-bottom: 22px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.stat-label { color: var(--ink-soft); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.stat-value { font-size: 26px; font-weight: 800; margin-top: 6px; }
.stat-sub { color: var(--ink-soft); font-size: 12px; margin-top: 4px; }

.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.panel-head { padding: 14px 18px; border-bottom: 1px solid var(--line); font-weight: 700; display: flex; align-items: center; gap: 10px; }
.panel-body { padding: 18px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }

/* ---------- TABLE ---------- */
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); }
table.data th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-soft); background: var(--gray-soft); }
table.data tbody tr:hover { background: #f8fafc; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.muted { color: var(--ink-soft); }
.empty { padding: 40px; text-align: center; color: var(--ink-soft); }

/* ---------- BADGES ---------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.badge-gray { background: var(--gray-soft); color: var(--ink-soft); }
.badge-blue { background: var(--primary-soft); color: var(--primary-dark); }
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.badge-red { background: var(--red-soft); color: var(--red); }

/* ---------- FORMS ---------- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; font-weight: 600; color: var(--ink-soft); }
.field input, .field select, .field textarea {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; background: #fff;
}
.field textarea { resize: vertical; min-height: 60px; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* ---------- ITEM EDITOR ---------- */
.items-table { width: 100%; border-collapse: collapse; margin-top: 8px; }
.items-table th, .items-table td { padding: 6px; border-bottom: 1px solid var(--line); font-size: 13px; }
.items-table input, .items-table select { width: 100%; padding: 6px; border: 1px solid var(--line); border-radius: 6px; }
.items-table .num input { text-align: right; }
.totals { margin-top: 14px; margin-left: auto; width: 280px; }
.totals-row { display: flex; justify-content: space-between; padding: 4px 0; }
.totals-row.total { font-size: 18px; font-weight: 800; border-top: 2px solid var(--line); margin-top: 6px; padding-top: 10px; }

/* ---------- MODAL ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: flex;
  align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 50; overflow: auto; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 820px; box-shadow: var(--shadow-lg); }
.modal.narrow { max-width: 480px; }
.modal-header { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-header h2 { margin: 0; font-size: 17px; }
.modal-close { margin-left: auto; background: none; border: none; font-size: 24px; cursor: pointer; color: var(--ink-soft); line-height: 1; }
.modal-body { padding: 20px; }

/* ---------- TOAST ---------- */
.toast { position: fixed; bottom: 24px; right: 24px; background: var(--ink); color: #fff;
  padding: 12px 18px; border-radius: 10px; box-shadow: var(--shadow-lg); z-index: 60; font-weight: 600; }
.toast.error { background: var(--red); }
.toast.success { background: var(--green); }

/* ---------- DOC PRINT / VIEW ---------- */
.doc { background: #fff; }
.doc-header { display: flex; justify-content: space-between; gap: 20px; }
.doc-box { border: 2px solid var(--red); border-radius: 8px; padding: 12px 18px; text-align: center; color: var(--red); min-width: 220px; }
.doc-box .rut { font-weight: 800; font-size: 16px; }
.doc-box .tipo { font-weight: 700; margin: 6px 0; }
.doc-meta { margin: 18px 0; }

/* ---------- LISTADOS ---------- */
.listado-print-head { padding: 12px 14px; border-bottom: 2px solid var(--primary); font-size: 13px; }
.listado-print-head strong { color: var(--primary); }
table.data tfoot th { background: var(--gray-soft); border-top: 2px solid var(--line); font-size: 13px; }

/* ---------- PRESUPUESTO (cotizacion imprimible) ---------- */
.presupuesto { color: #1e293b; font-size: 13px; }
.pp-header { display: flex; align-items: flex-start; gap: 18px; border-bottom: 3px solid #2e3a66; padding-bottom: 14px; }
.pp-logo { height: 66px; width: auto; object-fit: contain; }
.pp-emisor { flex: 1; line-height: 1.5; }
.pp-emisor strong { font-size: 16px; color: #2e3a66; display: block; margin-bottom: 2px; }
.pp-num { text-align: right; min-width: 150px; }
.pp-num-label { font-size: 11px; letter-spacing: .12em; color: #64748b; }
.pp-num-val { font-size: 20px; font-weight: 800; color: #2e3a66; }
.pp-cliente { margin: 16px 0; line-height: 1.7; }
.pp-intro { margin: 12px 0; }
.pp-items { margin-top: 8px; }
.pp-items thead th { background: #2e3a66; color: #fff; border-color: #2e3a66; }
.pp-items td { vertical-align: top; }
.pp-items tbody tr:hover { background: transparent; }
.pp-tot { width: 300px; margin-left: auto; margin-top: 14px; }
.pp-cond { margin-top: 22px; line-height: 1.7; font-size: 12.5px; }
.pp-cond strong { display: block; color: #2e3a66; margin-bottom: 4px; font-size: 14px; }
.pp-firma { margin-top: 28px; line-height: 1.5; }
.pp-firma strong { color: #2e3a66; }

/* ---------- BAR CHART ---------- */
.bars { display: flex; align-items: flex-end; gap: 12px; height: 180px; padding: 10px 4px 0; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.bar { width: 100%; max-width: 46px; background: linear-gradient(180deg, #8cc63f, #5fa233); border-radius: 6px 6px 0 0; min-height: 3px; }
.bar-label { font-size: 11px; color: var(--ink-soft); }
.bar-val { font-size: 11px; font-weight: 700; }

@media (max-width: 900px) {
  .sidebar { position: fixed; z-index: 40; height: 100%; transform: translateX(-100%); transition: .2s; }
  .sidebar.open { transform: translateX(0); }
  .menu-toggle { display: block; }
  .grid-2 { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
}
@media print {
  .sidebar, .topbar, .modal-close, .no-print { display: none !important; }
  .modal-backdrop { position: static; background: none; padding: 0; }
  .modal { box-shadow: none; max-width: 100%; }
}

/* Tarjetas de permisos (pantalla de Roles) */
.perm-grupo { font-weight: 600; margin: 0 0 6px; color: var(--ink-soft); }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px; }
.perm-card {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border: 1px solid var(--line, #d8dbe6); border-radius: 8px;
  cursor: pointer; background: #fff; transition: background .12s, border-color .12s;
}
.perm-card:hover { border-color: var(--primary, #2e3a66); }
.perm-card input { position: absolute; opacity: 0; width: 0; height: 0; }
.perm-card .perm-check {
  flex: 0 0 auto; width: 20px; height: 20px; border-radius: 5px;
  border: 2px solid var(--line, #c3c8d6); background: #fff; position: relative;
}
.perm-card.sel { background: var(--primary-soft, #e8ecf7); border-color: var(--primary, #2e3a66); }
.perm-card.sel .perm-check { background: var(--primary, #2e3a66); border-color: var(--primary, #2e3a66); }
.perm-card.sel .perm-check::after {
  content: ""; position: absolute; left: 6px; top: 2px;
  width: 5px; height: 10px; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg);
}
.perm-card .perm-nombre { font-size: 14px; }

/* Chips de permiso clicables en la fila de usuario */
.chips-perm { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-perm {
  font: inherit; font-size: 12.5px; cursor: pointer; user-select: none;
  padding: 5px 11px; border-radius: 999px; border: 1px solid var(--line, #c3c8d6);
  background: #fff; color: var(--ink-soft, #55607a); transition: all .12s;
}
.chip-perm:hover { border-color: var(--primary, #2e3a66); color: var(--primary, #2e3a66); }
.chip-perm.sel { background: var(--primary, #2e3a66); border-color: var(--primary, #2e3a66); color: #fff; }
.chip-perm.chip-admin { border-color: var(--green, #5fa233); color: var(--green, #5fa233); }
.chip-perm.chip-admin.sel { background: var(--green, #5fa233); border-color: var(--green, #5fa233); color: #fff; }
