:root {
  --verde: #2f7d3e;
  --verde-oscuro: #1f5c2c;
  --verde-claro: #e7f4ea;
  --cielo: #eef6f9;
  --ambar: #d98c1f;
  --rojo: #c0392b;
  --gris-100: #f6f7f8;
  --gris-200: #e7e9ec;
  --gris-400: #b6bcc4;
  --gris-600: #6b7280;
  --gris-800: #2c3138;
  --borde: #dfe3e8;
  --radio: 10px;
  --sombra: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  font-size: 15px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--gris-100);
  color: var(--gris-800);
}
a { color: var(--verde-oscuro); text-decoration: none; }
.hidden { display: none !important; }

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--verde-oscuro), var(--verde) 60%, #8fbf6a);
  padding: 20px;
}
.login-card {
  background: #fff;
  border-radius: 16px;
  padding: 32px 28px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.25);
  text-align: center;
}
.login-logo { margin-bottom: 4px; color: var(--verde-oscuro); display: flex; justify-content: center; }
.login-card h1 { margin: 4px 0 2px; font-size: 22px; color: var(--verde-oscuro); }
.login-sub { color: var(--gris-600); margin: 0 0 20px; font-size: 13px; }
.login-card form { text-align: left; }
.login-card label { display: block; font-size: 13px; font-weight: 600; margin: 12px 0 4px; }
.login-card input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--borde); border-radius: 8px; font-size: 15px;
}
.login-card .btn-block { margin-top: 20px; width: 100%; }

/* ---------- App shell ---------- */
.app-screen { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  height: 56px; background: var(--verde-oscuro); color: #fff;
  display: flex; align-items: center; padding: 0 12px; gap: 10px;
  position: sticky; top: 0; z-index: 20;
}
.topbar-title { font-weight: 700; flex: 1; font-size: 16px; display: flex; align-items: center; }
.topbar-user { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.icon-btn {
  background: transparent; border: none; color: #fff; font-size: 22px; cursor: pointer; padding: 4px 8px;
}

.layout { flex: 1; display: flex; min-height: 0; position: relative; }

.sidebar {
  width: 220px; background: #fff; border-right: 1px solid var(--borde);
  display: flex; flex-direction: column; padding: 10px 0;
  position: fixed; top: 56px; bottom: 0; left: -240px; transition: left .2s ease; z-index: 30;
  overflow-y: auto;
}
.sidebar.open { left: 0; }
.sidebar a {
  padding: 12px 20px; color: var(--gris-800); font-size: 14.5px; font-weight: 500;
  border-left: 3px solid transparent;
}
.sidebar a.active { background: var(--verde-claro); color: var(--verde-oscuro); border-left-color: var(--verde); font-weight: 700; }
.sidebar-backdrop {
  position: fixed; inset: 56px 0 0 0; background: rgba(0,0,0,0.35); z-index: 25;
}

.content { flex: 1; padding: 16px; max-width: 1200px; margin: 0 auto; width: 100%; }

@media (min-width: 900px) {
  .sidebar { left: 0; position: sticky; height: calc(100vh - 56px); }
  .content { margin-left: 0; padding: 24px; }
  .icon-btn#menu-toggle { display: none; }
  .sidebar-backdrop { display: none !important; }
}

/* ---------- Componentes generales ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: none; border-radius: 8px; padding: 9px 16px; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: filter .15s ease;
}
.btn:hover { filter: brightness(0.95); }
.btn-primary { background: var(--verde); color: #fff; }
.btn-secondary { background: var(--gris-200); color: var(--gris-800); }
.btn-danger { background: var(--rojo); color: #fff; }
.btn-ghost { background: transparent; color: inherit; border: 1px solid rgba(255,255,255,0.4); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-block { width: 100%; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.card { background: #fff; border-radius: var(--radio); box-shadow: var(--sombra); padding: 16px; margin-bottom: 16px; }
.card h2, .card h3 { margin-top: 0; }

.page-header {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px;
  margin-bottom: 16px;
}
.page-header h1 { font-size: 20px; margin: 0; }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 20px; }
.kpi-card { background: #fff; border-radius: var(--radio); box-shadow: var(--sombra); padding: 16px; }
.kpi-card .kpi-label { font-size: 12px; color: var(--gris-600); text-transform: uppercase; letter-spacing: .03em; }
.kpi-card .kpi-value { font-size: 26px; font-weight: 800; color: var(--verde-oscuro); margin-top: 4px; }

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--gris-200); white-space: nowrap; }
th { color: var(--gris-600); font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
tr:hover td { background: var(--gris-100); }

.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 700; }
.badge-borrador { background: #eee; color: #666; }
.badge-pendiente_revision { background: #fdf1dc; color: var(--ambar); }
.badge-aprobado { background: var(--verde-claro); color: var(--verde-oscuro); }
.badge-facturado { background: #e3ecfb; color: #2854a8; }
.badge-anulado { background: #fbe4e1; color: var(--rojo); }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group label { font-size: 12.5px; font-weight: 600; color: var(--gris-800); }
.form-group input, .form-group select, .form-group textarea {
  padding: 9px 10px; border: 1px solid var(--borde); border-radius: 8px; font-size: 14.5px; font-family: inherit;
  width: 100%;
}
.form-group textarea { resize: vertical; min-height: 60px; }
fieldset { border: 1px solid var(--borde); border-radius: var(--radio); margin: 0 0 16px; padding: 14px; }
legend { font-weight: 700; color: var(--verde-oscuro); padding: 0 6px; font-size: 13.5px; }

.productos-editor .fila-producto {
  display: grid; grid-template-columns: 2fr 1fr 1fr auto; gap: 8px; margin-bottom: 8px; align-items: end;
}
@media (max-width: 600px) {
  .productos-editor .fila-producto { grid-template-columns: 1fr 1fr; }
}

.error-msg { background: #fbe4e1; color: var(--rojo); padding: 8px 10px; border-radius: 8px; font-size: 13px; margin: 10px 0; }
.help-text { color: var(--gris-600); font-size: 12.5px; margin-top: 4px; }
.empty-state { text-align: center; padding: 40px 20px; color: var(--gris-600); }

.toast-root { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--gris-800); color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13.5px; box-shadow: var(--sombra); }
.toast.error { background: var(--rojo); }
.toast.success { background: var(--verde-oscuro); }

.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: flex; align-items: flex-end; justify-content: center; z-index: 50; }
.modal-box { background: #fff; border-radius: 16px 16px 0 0; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; padding: 20px; }
@media (min-width: 700px) {
  .modal-overlay { align-items: center; }
  .modal-box { border-radius: 16px; }
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-header h3 { margin: 0; }
.modal-close { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--gris-600); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

.filters-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.filters-bar select, .filters-bar input { padding: 8px 10px; border: 1px solid var(--borde); border-radius: 8px; font-size: 13.5px; }

#mapa-container { height: 60vh; min-height: 360px; border-radius: var(--radio); overflow: hidden; box-shadow: var(--sombra); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--borde); margin-bottom: 16px; }
.tab-btn { background: none; border: none; padding: 10px 14px; font-size: 14px; font-weight: 600; color: var(--gris-600); cursor: pointer; border-bottom: 2px solid transparent; }
.tab-btn.active { color: var(--verde-oscuro); border-bottom-color: var(--verde); }

.spinner { text-align: center; padding: 40px; color: var(--gris-600); }
.action-icons { display: flex; gap: 6px; }
.link-btn { background: none; border: none; color: var(--verde-oscuro); font-weight: 600; cursor: pointer; padding: 0; font-size: 13.5px; }
.text-right { text-align: right; }
.mt-0 { margin-top: 0; }
.muted { color: var(--gris-600); }
