:root {
  color-scheme: light;
  --ink: #172b25;
  --muted: #6b7d76;
  --line: #e2eae6;
  --surface: #ffffff;
  --canvas: #f4f8f5;
  --green: #9caa17;
  --green-dark: #707d0c;
  --green-soft: #f3f5df;
  --orange: #f67300;
  --orange-dark: #cf5e00;
  --red: #b93838;
  --amber: #9b6500;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--ink); background: var(--canvas); }
a { color: var(--green-dark); text-decoration: none; transition: color .15s ease; }
a:hover { color: var(--orange-dark); }

/* Layout principal */
.app-shell { min-height: 100vh; }
.sidebar {
  position: fixed;
  z-index: 20;
  inset: 0 auto 0 0;
  display: flex;
  width: 256px;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
  padding: 18px 14px;
  border-right: 1px solid var(--line);
  background: var(--surface);
}
.sidebar-brand {
  display: flex;
  min-height: 76px;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 4px;
  margin: -18px -14px 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.sidebar-brand:hover { text-decoration: none; }
.sidebar-brand img { display: block; width: 186px; height: auto; }
.sidebar-brand span { color: var(--orange-dark); font-size: 12px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; }
.sidebar-label { margin: 16px 10px 6px; color: #89958e; font-size: 10px; font-weight: 800; letter-spacing: .11em; text-transform: uppercase; }
.sidebar-link {
  display: flex;
  min-height: 40px;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: #52645b;
  font: inherit;
  font-size: 13.5px;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition: background-color .15s ease, color .15s ease;
}
.sidebar-link:hover,
.sidebar-link.active {
  background: var(--green-soft);
  color: var(--green-dark);
  text-decoration: none;
}
.sidebar-link.active { font-weight: 700; box-shadow: inset 3px 0 0 var(--green); }
.sidebar-icon { display: inline-flex; width: 18px; height: 18px; align-items: center; justify-content: center; color: var(--orange-dark); flex-shrink: 0; }
.sidebar-footer { display: grid; gap: 5px; margin-top: auto; padding-top: 14px; border-top: 1px solid var(--line); }
.sidebar-footer form { margin: 0; }
.sidebar-logout { width: 100%; }

.app-main { min-height: 100vh; margin-left: 256px; display: flex; flex-direction: column; }
.app-main.full-main { margin-left: 0; }
.topbar {
  position: sticky;
  z-index: 10;
  top: 0;
  min-height: 62px;
  padding: 0 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
  color: #53635a;
  font-size: 13px;
}
.portal-container {
  width: 100%;
  padding: 24px 28px 56px;
}

/* Tipografía y encabezados */
.page-heading, .section-heading { display: flex; align-items: center; justify-content: space-between; gap: 18px; flex-wrap: wrap; }
.page-heading { margin-bottom: 24px; }
h1 { margin: 0 0 6px; font-size: clamp(25px, 3.2vw, 33px); font-weight: 800; letter-spacing: -.03em; color: var(--ink); }
h2 { margin: 0 0 6px; font-size: 19px; font-weight: 700; letter-spacing: -.02em; color: var(--ink); }
.muted { color: var(--muted); }
.eyebrow { margin: 0 0 6px; color: var(--green-dark); font-size: 11px; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }

/* Tarjetas y contenedores */
.stat-card, .panel, .login-card, .form-card {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 4px 14px rgba(24, 58, 43, 0.035);
}
.stats-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; margin-bottom: 22px; }
.stat-card {
  min-height: 136px;
  padding: 20px 22px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: var(--ink);
  transition: border-color .16s ease, transform .16s ease, box-shadow .16s ease;
}
a.stat-card:hover { border-color: #b7d7c6; transform: translateY(-2px); box-shadow: 0 8px 20px rgba(24, 58, 43, 0.06); text-decoration: none; color: var(--ink); }
.stat-label, .stat-foot { color: var(--muted); font-size: 13px; }
.stat-card strong { margin: 6px 0 4px; font-size: 30px; font-weight: 800; letter-spacing: -.02em; }
.panel { margin-bottom: 20px; padding: 22px; }
.section-heading { margin-bottom: 16px; }
.section-heading h2 { margin: 0; }

/* Tablas */
.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; text-align: left; font-size: 14px; }
th { padding: 11px 12px; color: var(--muted); font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; white-space: nowrap; border-bottom: 1px solid var(--line); }
td { padding: 13px 12px; border-top: 1px solid #edf1ef; vertical-align: middle; }
tbody tr { transition: background-color .12s ease; }
tbody tr:hover { background-color: #fafcfa; }
tfoot th, tfoot td { padding: 11px 12px; border-top: 1px solid var(--line); background: #fafcfa; }

/* Badges semánticos y existencias */
.stock { display: inline-block; min-width: 36px; padding: 4px 9px; border-radius: 8px; background: #e9f6ef; color: #166534; font-weight: 700; font-size: 13px; text-align: center; }
.stock.low { background: #fff2d9; color: var(--amber); }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  background: #edf1ef;
  color: #465751;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.badge.pendiente { background: #fff2d9; color: var(--amber); }
.badge.preparado, .badge.investigando { background: #e5f0ff; color: #265394; }
.badge.en_ruta { background: #ede9fe; color: #5b21b6; }
.badge.entregado, .badge.aprobado, .badge.pagado, .badge.resuelto { background: #dcfce7; color: #166534; }
.badge.cancelado, .badge.desestimada { background: #fee2e2; color: #991b1b; }

/* Formularios y controles */
.filter-bar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; margin-bottom: 18px; padding: 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.field { display: flex; flex: 1; flex-direction: column; gap: 6px; color: #40554c; font-size: 13px; font-weight: 600; }
.field.compact { min-width: 180px; }
.field-checkbox { flex-direction: row; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid #e5ede8; border-radius: 9px; background: #fafcfa; }

input:not([type="checkbox"]):not([type="hidden"]),
select,
textarea {
  width: 100%;
  min-height: 42px;
  padding: 9px 12px;
  border: 1px solid #d6e1db;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  transition: border-color .15s ease, box-shadow .15s ease;
}
textarea { min-height: 96px; resize: vertical; }
input[type="checkbox"] { width: 17px; height: 17px; padding: 0; border-radius: 4px; accent-color: var(--green); cursor: pointer; flex-shrink: 0; }
input:not([type="checkbox"]):focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(156, 170, 23, 0.18);
}

/* Botones */
.button {
  display: inline-flex;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border: 1px solid transparent;
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .16s ease, border-color .16s ease, box-shadow .16s ease, color .16s ease, transform .16s ease;
}
.button:hover { text-decoration: none; transform: translateY(-1px); box-shadow: 0 3px 8px rgba(24, 58, 43, 0.1); }
.button.primary { background: var(--orange); color: #fff; }
.button.secondary { border-color: #d6e1db; background: #fff; color: #315347; }
.button.danger { border-color: #f2d5d2; background: #fff7f6; color: var(--red); }
.button.primary:hover, .button.primary:focus-visible { border-color: var(--orange-dark); background: var(--orange-dark); color: #fff; }
.button.secondary:hover, .button.secondary:focus-visible { border-color: var(--orange); background: var(--orange); color: #fff; }
.button.danger:hover, .button.danger:focus-visible { border-color: var(--red); background: var(--red); color: #fff; }
.button:focus-visible { outline: 3px solid rgba(156, 170, 23, 0.3); outline-offset: 2px; }
.button:active { transform: translateY(0); box-shadow: none; }
.button.small, .button.compact { min-height: 33px; padding: 6px 11px; font-size: 12px; }
.button.full { width: 100%; }

/* Utilidades de estructura */
.empty { margin: 0; padding: 24px 8px; color: var(--muted); text-align: center; }
.pagination { display: flex; align-items: center; justify-content: center; gap: 16px; padding-top: 18px; border-top: 1px solid #edf1ef; margin-top: 8px; color: var(--muted); font-size: 13px; }
.breadcrumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 18px; color: var(--muted); font-size: 13px; }
.detail-grid { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(270px, .85fr); gap: 18px; }
.detail-grid .panel { margin-bottom: 18px; }
hr { margin: 16px 0; border: 0; border-top: 1px solid var(--line); }
.actions-grid { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.actions-grid form { margin: 0; }
.assign-form { display: flex; align-items: flex-end; gap: 10px; }
.assign-form .field { min-width: 240px; }

/* Alertas y acceso */
.messages { display: grid; gap: 8px; margin-bottom: 18px; }
.message { margin: 0; padding: 12px 16px; border: 1px solid #dce6b5; border-radius: 10px; background: #f8fae9; color: #4f5b0a; font-size: 14px; font-weight: 500; }
.message.error { border-color: #f0cfcb; background: #fff5f4; color: #9e322e; }

.login-wrap { min-height: calc(100vh - 150px); display: grid; place-items: center; padding: 16px 0; }
.login-card, .form-card { width: min(100%, 460px); padding: 32px; }
.form-card { margin: 0 auto; }
.form-stack { display: grid; gap: 16px; margin-top: 20px; }
.form-errors, .field-error { color: var(--red); font-size: 13px; font-weight: 500; }
.form-actions { display: flex; flex-wrap: wrap; gap: 10px; padding-top: 4px; }
.portal-form-panel { max-width: 760px; }
.portal-actions { display: flex; flex-wrap: wrap; gap: 7px; min-width: 170px; }
.portal-actions form { margin: 0; }

/* Rastreo GPS en vivo (Leaflet + OpenStreetMap) */
.mm-tracking-map {
  width: 100%;
  height: 360px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #e9f0ec;
  z-index: 1;
}
.mm-leaflet-div-icon { background: transparent; border: 0; }
.mm-pin {
  position: relative;
  display: flex;
  width: 38px;
  height: 38px;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  border: 2.5px solid #fff;
  box-shadow: 0 4px 12px rgba(23, 43, 37, 0.25);
  font-size: 18px;
}
.mm-pin-tienda { background: #707d0c; color: #fff; }
.mm-pin-casa { background: #f67300; color: #fff; }
.mm-pin-moto { background: #0284c7; color: #fff; }
.mm-pin-flecha {
  position: absolute;
  top: -10px;
  left: 50%;
  margin-left: -6px;
  font-size: 11px;
  line-height: 1;
  color: #0284c7;
  text-shadow: 0 0 3px #fff;
  transform-origin: 50% 24px;
  transition: transform .35s ease;
}

@media (max-width: 900px) {
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .sidebar {
    position: sticky;
    inset: auto;
    width: 100%;
    min-height: 0;
    flex-direction: row;
    align-items: center;
    gap: 6px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 8px 12px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .sidebar-brand { flex: 0 0 auto; min-height: 0; margin: 0 6px 0 0; padding: 4px 8px 4px 0; border-bottom: 0; }
  .sidebar-brand img { width: 155px; }
  .sidebar-label { display: none; }
  .sidebar-link { flex: 0 0 auto; min-height: 38px; padding: 8px 10px; font-size: 12px; white-space: nowrap; }
  .sidebar-link.active { box-shadow: inset 0 -2px 0 var(--green); }
  .sidebar-footer { flex: 0 0 auto; margin-top: 0; padding-top: 0; border-top: 0; }
  .app-main { margin-left: 0; }
  .topbar { min-height: 54px; padding: 0 16px; }
  .portal-container { padding: 18px 16px 40px; }
  .stats-grid, .detail-grid { grid-template-columns: 1fr; }
  .stat-card { min-height: auto; }
  .filter-bar, .assign-form, .actions-grid { align-items: stretch; flex-direction: column; }
  .assign-form { width: 100%; }
  .assign-form .field, .portal-actions { min-width: 0; }
  .actions-grid > form, .actions-grid .button, .portal-actions form, .portal-actions .button { width: 100%; }
  .panel { padding: 16px; }
}

@media print {
  body { background: #fff; }
  .sidebar, .topbar, .filter-bar, .pagination, .actions-panel, .portal-actions, .button { display: none !important; }
  .app-main { margin-left: 0 !important; }
  .portal-container { padding: 0 !important; }
  .panel, .stat-card { box-shadow: none !important; break-inside: avoid; }
}
