/* ===== Panel de Gestión · Compras y Ventas ===== */
:root {
  --bg: #0f172a;
  --bg-2: #111c34;
  --surface: #ffffff;
  --surface-2: #f6f8fc;
  --border: #e6eaf2;
  --text: #1e293b;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-soft: #eef2ff;
  --success: #10b981;
  --success-soft: #ecfdf5;
  --danger: #ef4444;
  --danger-soft: #fef2f2;
  --warning: #f59e0b;
  --warning-soft: #fffbeb;
  --info: #06b6d4;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 8px 24px rgba(16,24,40,.06);
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--surface-2);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

.app { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 248px;
  flex-shrink: 0;
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg-2) 100%);
  color: #cbd5e1;
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 4px 8px 22px; }
.brand .logo {
  width: 40px; height: 40px; border-radius: 11px;
  background: linear-gradient(135deg, var(--primary), var(--info));
  display: grid; place-items: center; flex-shrink: 0; color: #fff;
}
.brand .logo svg { width: 22px; height: 22px; }
.brand b { color: #fff; font-size: 15px; display: block; line-height: 1.2; }
.brand span { font-size: 11px; color: #94a3b8; }

.nav { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.nav button {
  display: flex; align-items: center; gap: 12px;
  background: transparent; border: 0; color: #cbd5e1;
  padding: 11px 14px; border-radius: 10px; cursor: pointer;
  font-size: 14px; text-align: left; width: 100%; transition: .15s;
}
.nav button .ic { width: 20px; display: inline-flex; align-items: center; justify-content: center; }
.nav button .ic svg { width: 18px; height: 18px; }
.nav button:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav button.active { background: var(--primary); color: #fff; box-shadow: var(--shadow); }

.sidebar-foot { margin-top: auto; font-size: 11px; color: #64748b; padding: 12px 8px 0; line-height: 1.5; }

/* ---------- Content ---------- */
.content { flex: 1; min-width: 0; padding: 26px 32px 60px; }
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 22px; gap: 16px; flex-wrap: wrap; }
.topbar h1 { font-size: 22px; margin: 0 0 2px; }
.topbar .sub { color: var(--muted); font-size: 13px; }
.topbar .today { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 14px; font-size: 13px; color: var(--muted); box-shadow: var(--shadow); }

.view { display: none; }
.view.active { display: block; animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---------- Filter bar ---------- */
.filterbar {
  display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 18px; box-shadow: var(--shadow); margin-bottom: 20px;
}
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.field input, .field select {
  border: 1px solid var(--border); border-radius: 9px; padding: 9px 11px;
  font-size: 14px; background: #fff; color: var(--text); min-width: 150px; font-family: inherit;
}
.field input:focus, .field select:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border); background: #fff; color: var(--text);
  padding: 9px 16px; border-radius: 9px; cursor: pointer; font-size: 14px; font-weight: 500;
  display: inline-flex; align-items: center; gap: 7px; transition: .15s; font-family: inherit;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: #4338ca; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #f3c7c7; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.sm { padding: 6px 10px; font-size: 12.5px; }

/* ---------- KPI cards ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; margin-bottom: 22px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); position: relative; overflow: hidden;
}
.kpi .top { display: flex; align-items: center; justify-content: space-between; }
.kpi .label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.kpi .icon { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; }
.kpi .icon svg { width: 18px; height: 18px; }
.kpi .value { font-size: 25px; font-weight: 700; margin-top: 10px; letter-spacing: -.5px; }
.kpi .delta { font-size: 12px; margin-top: 5px; color: var(--muted); }
.kpi .delta.pos { color: var(--success); }
.kpi .delta.neg { color: var(--danger); }
.i-blue { background: var(--primary-soft); color: var(--primary); }
.i-green { background: var(--success-soft); color: var(--success); }
.i-amber { background: var(--warning-soft); color: var(--warning); }
.i-cyan { background: #ecfeff; color: var(--info); }
.i-red { background: var(--danger-soft); color: var(--danger); }

/* ---------- Panels / cards ---------- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-bottom: 18px; }
.grid-3 { display: grid; grid-template-columns: 2fr 1fr; gap: 18px; margin-bottom: 18px; }
@media (max-width: 1100px){ .grid-2, .grid-3 { grid-template-columns: 1fr; } }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 4px; font-size: 15px; }
.card .hint { color: var(--muted); font-size: 12.5px; margin: 0 0 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; gap: 12px; flex-wrap: wrap; }
.card-head h3 { margin: 0; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: right; padding: 11px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th { font-size: 11px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
tbody tr:hover { background: var(--surface-2); }
tfoot td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: 0; }
.pos { color: var(--success); font-weight: 600; }
.neg { color: var(--danger); font-weight: 600; }
.muted { color: var(--muted); }
.tag { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }

.icon-btn { border: 0; background: transparent; cursor: pointer; padding: 5px 7px; border-radius: 6px; color: var(--muted); display: inline-flex; align-items: center; vertical-align: middle; }
.icon-btn svg { width: 16px; height: 16px; display: block; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.del:hover { color: var(--danger); background: var(--danger-soft); }

/* ---------- Form ---------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; align-items: end; }
.form-grid .field input, .form-grid .field select { min-width: 0; width: 100%; }
.form-actions { display: flex; gap: 10px; margin-top: 16px; align-items: center; }
.editing-badge { background: var(--warning-soft); color: var(--warning); padding: 5px 11px; border-radius: 20px; font-size: 12px; font-weight: 600; display: none; }
.editing .editing-badge { display: inline-block; }

/* ---------- Recommendation ---------- */
.reco { background: linear-gradient(135deg, #4f46e5, #06b6d4); color: #fff; border: 0; }
.reco h3 { color: #fff; }
.reco .hint { color: rgba(255,255,255,.85); }
.reco .big { font-size: 30px; font-weight: 800; margin: 6px 0; }
.reco .row { display: flex; justify-content: space-between; padding: 7px 0; border-top: 1px solid rgba(255,255,255,.18); font-size: 13.5px; }
.bar-prog { height: 9px; background: rgba(255,255,255,.25); border-radius: 20px; overflow: hidden; margin: 12px 0 6px; }
.bar-prog > span { display: block; height: 100%; background: #fff; border-radius: 20px; }
.bar-prog.light { background: #eef0f5; }
.bar-prog.light > span { background: linear-gradient(90deg, #4f46e5, #06b6d4); }

/* Tarjeta de comparación Compras vs Tarjeta */
.cmp-nums { display: flex; gap: 18px; margin: 8px 0 4px; }
.cmp-nums .n { flex: 1; }
.cmp-nums .n .lbl { font-size: 12px; color: var(--muted); }
.cmp-nums .n .v { font-size: 24px; font-weight: 800; letter-spacing: -.5px; }
.cmp-row { display: flex; justify-content: space-between; padding: 8px 0; border-top: 1px solid var(--border); font-size: 13.5px; }

/* ---------- Legend ---------- */
.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 12px; font-size: 12.5px; }
.legend .item { display: flex; align-items: center; gap: 7px; color: var(--muted); }

/* Distribuidores */
.inline-add { display: flex; gap: 10px; align-items: flex-end; margin-bottom: 16px; flex-wrap: wrap; }
.dist-item { display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 13px; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 8px; }
.dist-item .nm { font-weight: 600; display: inline-flex; align-items: center; gap: 8px; }

.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .em { display: block; margin-bottom: 10px; color: #cbd5e1; }
.empty .em svg { width: 34px; height: 34px; display: inline-block; }

svg.chart { width: 100%; height: auto; display: block; }

.toast {
  position: fixed; bottom: 22px; right: 22px; background: var(--text); color: #fff;
  padding: 12px 18px; border-radius: 10px; box-shadow: var(--shadow); font-size: 13.5px;
  opacity: 0; transform: translateY(10px); transition: .25s; pointer-events: none; z-index: 50;
}
.toast.show { opacity: 1; transform: none; }

.section-title { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 26px 0 12px; }
.note { background: var(--info); background: #ecfeff; border: 1px solid #bae6fd; color: #0e7490; padding: 12px 16px; border-radius: 10px; font-size: 13px; margin-bottom: 18px; line-height: 1.5; }

/* ===================== MÓVIL / TABLET ===================== */
.mobile-bar { display: none; }
.overlay { display: none; }

@media (max-width: 820px) {
  /* El menú lateral pasa a ser un panel deslizable */
  .sidebar {
    position: fixed; top: 0; left: 0; height: 100%; width: 264px;
    transform: translateX(-100%); transition: transform .25s ease;
    z-index: 60; box-shadow: 0 0 50px rgba(0, 0, 0, .35); overflow-y: auto;
  }
  .sidebar.open { transform: translateX(0); }

  .overlay {
    display: block; position: fixed; inset: 0; background: rgba(15, 23, 42, .55);
    opacity: 0; pointer-events: none; transition: opacity .25s; z-index: 55;
  }
  .overlay.show { opacity: 1; pointer-events: auto; }

  /* Barra superior con botón de menú */
  .mobile-bar {
    display: flex; align-items: center; gap: 12px;
    position: fixed; top: 0; left: 0; right: 0; height: 56px; z-index: 45;
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-2) 100%);
    color: #fff; padding: 0 14px;
  }
  .hamburger {
    background: rgba(255, 255, 255, .1); border: 0; color: #fff;
    width: 40px; height: 40px; border-radius: 9px; display: grid; place-items: center; cursor: pointer;
  }
  .hamburger svg { width: 22px; height: 22px; }
  .mb-title { font-weight: 700; font-size: 16px; }

  .content { padding: 74px 16px 36px; }
  .topbar { margin-bottom: 18px; }
  .topbar h1 { font-size: 19px; }
  .today { font-size: 12px; padding: 7px 11px; }

  .kpis { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .kpi { padding: 14px; }
  .kpi .value { font-size: 20px; }
  .kpi .icon { width: 30px; height: 30px; }

  .grid-2, .grid-3 { grid-template-columns: 1fr; gap: 14px; }
  .card { padding: 16px; }

  /* Filtros apilados y a ancho completo */
  .filterbar { padding: 12px 14px; gap: 12px; }
  .filterbar .field { flex: 1 1 100%; }
  /* 16px evita que el navegador móvil haga zoom al enfocar un campo */
  .field input, .field select { min-width: 0; width: 100%; font-size: 16px; }
  .filterbar .btn { width: 100%; justify-content: center; }

  /* Formularios a una columna */
  .form-grid { grid-template-columns: 1fr; }
  .form-actions .btn { flex: 1; justify-content: center; }
  .inline-add { flex-direction: column; align-items: stretch; }
  .inline-add .btn { width: 100%; justify-content: center; }

  th, td { padding: 9px 10px; font-size: 13px; }
  .reco .big { font-size: 26px; }

  .toast { left: 16px; right: 16px; bottom: 16px; text-align: center; }

  /* Pantalla de acceso: anclada arriba y con scroll para que el teclado no tape los campos */
  .auth-screen { align-items: flex-start; overflow-y: auto; padding: 40px 16px; }
  .auth-card { padding: 24px 20px; }

  /* Cuadro de sesión del menú lateral */
  .sidebar-foot { padding-bottom: 20px; }
}

@media (max-width: 440px) {
  .content { padding: 70px 12px 32px; }
  .kpis { grid-template-columns: 1fr; }
  .cmp-nums { flex-direction: column; gap: 12px; }
  .topbar .sub { font-size: 12px; }
}

/* ===================== ACCESO / SESIÓN / ROLES ===================== */
.auth-screen {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 20px;
  background: linear-gradient(135deg, var(--bg) 0%, var(--bg-2) 100%);
}
.auth-screen.hidden { display: none; }
.auth-card {
  background: var(--surface); border-radius: 16px; padding: 30px 28px;
  width: 100%; max-width: 360px; box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
  display: flex; flex-direction: column; gap: 12px;
}
.auth-logo {
  width: 52px; height: 52px; border-radius: 13px; margin: 0 auto 4px;
  background: linear-gradient(135deg, var(--primary), var(--info));
  display: grid; place-items: center; color: #fff;
}
.auth-logo svg { width: 28px; height: 28px; }
.auth-card h2 { margin: 0; text-align: center; font-size: 20px; }
.auth-sub { margin: 0 0 6px; text-align: center; color: var(--muted); font-size: 13px; }
.auth-card .field label { font-size: 12px; }
.auth-card .field input { width: 100%; }
.auth-card .btn { justify-content: center; margin-top: 4px; padding: 11px; }
.auth-msg { color: var(--danger); font-size: 13px; min-height: 4px; text-align: center; }

.sesion-box { display: flex; flex-direction: column; gap: 8px; }
.sesion-info { font-size: 12px; color: #cbd5e1; line-height: 1.4; }
.sesion-info b { color: #fff; }
.sidebar-foot .btn { background: rgba(255, 255, 255, .08); border-color: rgba(255, 255, 255, .15); color: #e2e8f0; }
.sidebar-foot .btn:hover { background: rgba(255, 255, 255, .16); }

.rol-pill { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.rol-pill.dueno { background: var(--primary-soft); color: var(--primary); }
.rol-pill.empleado { background: var(--surface-2); color: var(--muted); }

/* Empleado: ocultar todo lo marcado como solo-admin */
body.rol-empleado .solo-admin { display: none !important; }
