* { box-sizing: border-box; }
:root {
  --primary: #764ba2;
  --primary-d: #5e3a85;
  --bg: #f4f5fa;
  --card: #fff;
  --text: #333;
  --muted: #888;
  --border: #e2e2ea;
  --ok: #1a7f37;
  --err: #cf222e;
}
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: #fff; padding: 0.8rem 1.5rem;
}
.brand { font-size: 1.2rem; font-weight: 700; }
.brand span { font-weight: 400; opacity: 0.8; font-size: 0.9rem; }
.topbar nav a { color: #fff; text-decoration: none; margin-left: 1rem; font-weight: 600; }

.container { max-width: 1100px; margin: 1.5rem auto; padding: 0 1.5rem; }
.cargando { color: var(--muted); }

/* Cards & panels */
.panel { background: var(--card); border-radius: 12px; padding: 1.5rem; box-shadow: 0 4px 16px rgba(0,0,0,0.06); margin-bottom: 1.2rem; }
.panel h2 { margin-top: 0; }

/* Breadcrumb + headers */
.crumbs { font-size: 0.85rem; color: var(--muted); margin-bottom: 0.8rem; }
.crumbs a { color: var(--primary); text-decoration: none; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }

/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 0.4rem; padding: 0.55rem 0.9rem; border: none; border-radius: 8px;
  background: var(--primary); color: #fff; font-size: 0.9rem; font-weight: 600; cursor: pointer; text-decoration: none; }
.btn:hover { background: var(--primary-d); }
.btn.sec { background: #eee; color: #444; }
.btn.sec:hover { background: #ddd; }
.btn.danger { background: #fde8e8; color: var(--err); }
.btn.danger:hover { background: #fbd5d5; }
.btn.sm { padding: 0.35rem 0.6rem; font-size: 0.8rem; }

/* Search */
.search { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.search input { flex: 1; padding: 0.6rem 0.8rem; border: 1px solid var(--border); border-radius: 8px; font-size: 1rem; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.6rem 0.7rem; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
th { color: var(--muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; }
tr:hover td { background: #faf9fc; }
.acciones { display: flex; gap: 0.4rem; }

/* Pagination */
.paginador { display: flex; align-items: center; gap: 0.5rem; justify-content: center; margin-top: 1rem; }
.paginador span { color: var(--muted); font-size: 0.9rem; }

/* Forms */
form .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; }
label.field { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; font-weight: 600; color: #555; }
label.field input, label.field select, label.field textarea {
  padding: 0.55rem 0.7rem; border: 1px solid var(--border); border-radius: 8px; font-size: 0.95rem; font-family: inherit; }
label.field textarea { min-height: 60px; resize: vertical; }
label.field.req > span::after { content: " *"; color: var(--err); }
fieldset { border: 1px solid var(--border); border-radius: 10px; margin: 0 0 1.2rem; padding: 1rem 1.2rem; }
fieldset legend { font-weight: 700; color: var(--primary); padding: 0 0.5rem; }
.form-actions { display: flex; gap: 0.6rem; margin-top: 1rem; }

/* Definition list (vista) */
.dl { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 0.6rem 1.5rem; }
.dl div { border-bottom: 1px dashed var(--border); padding-bottom: 0.3rem; }
.dl dt { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; }
.dl dd { margin: 0; font-size: 0.95rem; }

/* Badges */
.badge { display: inline-block; padding: 0.15rem 0.5rem; border-radius: 999px; font-size: 0.75rem; font-weight: 600; }
.badge.on { background: #e6f4ea; color: var(--ok); }
.badge.off { background: #fde8e8; color: var(--err); }
.badge.hist { background: #fff3cd; color: #8a6d3b; }

.vacio { color: var(--muted); text-align: center; padding: 1.5rem; }

/* Toast */
.toast { position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%) translateY(2rem);
  background: #333; color: #fff; padding: 0.8rem 1.2rem; border-radius: 8px; opacity: 0; pointer-events: none;
  transition: all 0.25s; font-size: 0.9rem; z-index: 100; max-width: 90%; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok { background: #1a7f37; }
.toast.err { background: #cf222e; }

/* Barra de usuario */
.userbox { display: inline-flex; align-items: center; gap: 0.7rem; margin-left: 1rem; }
.userinfo { font-size: 0.85rem; font-weight: 400; opacity: 0.95; }
.userinfo small { opacity: 0.7; }

/* ===== Layout dashboard (sidebar + main) ===== */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 235px; flex-shrink: 0; background: linear-gradient(180deg, #3a2c63, #2a1f48); color: #fff; display: flex; flex-direction: column; }
.side-brand { font-size: 1.3rem; font-weight: 700; padding: 1.1rem 1.2rem; border-bottom: 1px solid rgba(255,255,255,0.12); }
.menu { display: flex; flex-direction: column; padding: 0.6rem 0.5rem; gap: 0.12rem; }
.menu-item { display: flex; align-items: center; gap: 0.6rem; padding: 0.6rem 0.8rem; border-radius: 8px; color: #d9d4ea; text-decoration: none; font-size: 0.92rem; }
.menu-item:hover { background: rgba(255,255,255,0.08); color: #fff; }
.menu-item.activo { background: rgba(255,255,255,0.16); color: #fff; font-weight: 600; }
.menu-item .mi-ico { width: 1.3rem; text-align: center; }
.menu-item.soon { opacity: 0.55; }
.mi-hint { opacity: 0.6; font-size: 0.7rem; font-weight: 400; }
.soon-badge { margin-left: auto; font-size: 0.62rem; background: rgba(255,255,255,0.18); padding: 0.1rem 0.45rem; border-radius: 999px; text-transform: uppercase; }
.menu-sep { font-size: 0.68rem; text-transform: uppercase; opacity: 0.5; padding: 0.9rem 0.8rem 0.3rem; letter-spacing: 0.06em; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* Barra superior (sobrescribe la antigua) */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  background: #fff; border-bottom: 1px solid var(--border); padding: 0.7rem 1.2rem; flex-wrap: wrap; }
.content { padding: 1.5rem; width: 100%; max-width: 1120px; }

.ctx { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: #555; position: relative; flex-wrap: wrap; }
.ctx-emp { font-weight: 600; }
.ctx-sep { opacity: 0.4; }
.ctx-empty { color: var(--err); font-weight: 600; }
.ctx-panel { display: none; position: absolute; top: 130%; left: 0; background: #fff; border: 1px solid var(--border);
  border-radius: 10px; padding: 1rem; box-shadow: 0 8px 24px rgba(0,0,0,0.15); z-index: 50; min-width: 240px; }
.ctx-panel.show { display: block; }
.ctx-panel .field { margin-bottom: 0.7rem; }
.userarea { display: flex; align-items: center; gap: 0.8rem; }
.userinfo { font-size: 0.85rem; }
.userinfo small { color: var(--muted); }

/* Dashboard */
.page-title { margin: 0 0 0.2rem; font-size: 1.5rem; }
.page-sub { margin: 0 0 1.3rem; color: var(--muted); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 1rem; margin-bottom: 1.3rem; }
.kpi { background: #fff; border-radius: 12px; padding: 1.1rem; box-shadow: 0 4px 16px rgba(0,0,0,0.06); text-align: center; }
.kpi.clic { cursor: pointer; transition: transform 0.15s, box-shadow 0.15s; }
.kpi.clic:hover { transform: translateY(-3px); box-shadow: 0 8px 22px rgba(0,0,0,0.12); }
.kpi-ico { font-size: 1.6rem; }
.kpi-num { font-size: 1.8rem; font-weight: 700; color: var(--primary); line-height: 1.2; }
.kpi-lab { color: var(--muted); font-size: 0.85rem; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 0.8rem; }
.tile { display: flex; flex-direction: column; align-items: center; gap: 0.4rem; padding: 1.2rem; background: #f6f6fb;
  border-radius: 10px; text-decoration: none; color: var(--text); font-weight: 600; font-size: 0.9rem; border: 1px solid transparent; }
.tile:hover { border-color: var(--primary); background: #f0ecf6; }
.tile-ico { font-size: 1.6rem; }
.tile.soon { opacity: 0.55; }
.tile.soon .soon-badge { background: #ddd; color: #666; }

/* Responsive: menú arriba en pantallas angostas */
@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; }
  .menu { flex-direction: row; flex-wrap: wrap; }
  .menu-sep { width: 100%; }
  .soon-badge { display: none; }
}

/* Login */
.login-brand { font-size: 1.6rem; font-weight: 700; color: var(--primary); margin-bottom: 0.5rem; }
.login-wrap { display: flex; justify-content: center; align-items: flex-start; padding-top: 6vh; min-height: 100vh; background: linear-gradient(135deg, #667eea, #764ba2); }
.login-card { max-width: 380px; width: 100%; }
.login-card h2 { margin: 0 0 0.3rem; }
.login-sub { color: var(--muted); margin: 0 0 1.2rem; font-size: 0.9rem; }
.login-card .field { margin-bottom: 1rem; }
.login-card .btn { width: 100%; justify-content: center; }
.login-msg { color: var(--err); font-size: 0.9rem; min-height: 1.2rem; margin: 0.8rem 0 0; text-align: center; }

/* Texto atenuado */
.muted { color: var(--muted); }

/* Logo de empresa */
.logo-mini { height: 32px; max-width: 90px; object-fit: contain; vertical-align: middle; }
.logo-grande { max-height: 120px; max-width: 260px; object-fit: contain; border: 1px solid var(--border); border-radius: 8px; padding: 6px; background: #fff; }
.logo-box { display: flex; gap: 1.5rem; align-items: center; flex-wrap: wrap; }
#logoPrev { min-width: 140px; }

/* Stock inventario */
.stock { font-weight: 700; }
.stock.cero { color: var(--err); }

/* Totales de factura */
.totales { display: flex; gap: 1.5rem; justify-content: flex-end; flex-wrap: wrap; margin-top: 1rem;
  padding-top: 0.8rem; border-top: 1px solid var(--border); font-size: 0.95rem; }
.totales .tot-grande { font-size: 1.2rem; color: var(--primary); }
.saldo-pend { color: var(--err); }

/* Casillas de permisos */
.checks { display: flex; flex-wrap: wrap; gap: 0.8rem 1.5rem; }
.check { display: flex; align-items: center; gap: 0.4rem; font-size: 0.92rem; font-weight: 500; cursor: pointer; }
.check input { width: 1.1rem; height: 1.1rem; }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.45); display: flex; align-items: center; justify-content: center; z-index: 200; padding: 1rem; }
.modal-card { background: #fff; border-radius: 12px; padding: 1.5rem; width: 100%; max-width: 380px; box-shadow: 0 16px 48px rgba(0,0,0,0.3); }
.modal-card h3 { margin-top: 0; }

/* Combobox municipio */
.combo { position: relative; }
.combo-list { position: absolute; top: 100%; left: 0; right: 0; background: #fff; border: 1px solid var(--border);
  border-radius: 8px; max-height: 200px; overflow-y: auto; z-index: 20; box-shadow: 0 6px 18px rgba(0,0,0,0.12); display: none; }
.combo-list.show { display: block; }
.combo-list div { padding: 0.5rem 0.7rem; cursor: pointer; font-size: 0.9rem; font-weight: 400; }
.combo-list div:hover { background: #f0ecf6; }
